* configure: Rebuilt.
[official-gcc.git] / gcc / loop.c
blob55f52bf6c51ecd32955a7eacc8291f541c8ef790
1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 88, 89, 91-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This is the loop optimization pass of the compiler.
23 It finds invariant computations within loops and moves them
24 to the beginning of the loop. Then it identifies basic and
25 general induction variables. Strength reduction is applied to the general
26 induction variables, and induction variable elimination is applied to
27 the basic induction variables.
29 It also finds cases where
30 a register is set within the loop by zero-extending a narrower value
31 and changes these to zero the entire register once before the loop
32 and merely copy the low part within the loop.
34 Most of the complexity is in heuristics to decide when it is worth
35 while to do these things. */
37 #include "config.h"
38 #include "system.h"
39 #include "rtl.h"
40 #include "obstack.h"
41 #include "expr.h"
42 #include "insn-config.h"
43 #include "insn-flags.h"
44 #include "regs.h"
45 #include "hard-reg-set.h"
46 #include "recog.h"
47 #include "flags.h"
48 #include "real.h"
49 #include "loop.h"
50 #include "except.h"
51 #include "toplev.h"
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. */
57 int *uid_luid;
59 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
60 number the insn is contained in. */
62 int *uid_loop_num;
64 /* 1 + largest uid of any insn. */
66 int max_uid_for_loop;
68 /* 1 + luid of last insn. */
70 static int max_luid;
72 /* Number of loops detected in current function. Used as index to the
73 next few tables. */
75 static int max_loop_num;
77 /* Indexed by loop number, contains the first and last insn of each loop. */
79 static rtx *loop_number_loop_starts, *loop_number_loop_ends;
81 /* Likewise for the continue insn */
82 static rtx *loop_number_loop_cont;
84 /* The first code_label that is reached in every loop iteration.
85 0 when not computed yet, initially const0_rtx if a jump couldn't be
86 followed.
87 Also set to 0 when there is no such label before the NOTE_INSN_LOOP_CONT
88 of this loop, or in verify_dominator, if a jump couldn't be followed. */
89 static rtx *loop_number_cont_dominator;
91 /* For each loop, gives the containing loop number, -1 if none. */
93 int *loop_outer_loop;
95 #ifdef HAVE_decrement_and_branch_on_count
96 /* Records whether resource in use by inner loop. */
98 int *loop_used_count_register;
99 #endif /* HAVE_decrement_and_branch_on_count */
101 /* Indexed by loop number, contains a nonzero value if the "loop" isn't
102 really a loop (an insn outside the loop branches into it). */
104 static char *loop_invalid;
106 /* Indexed by loop number, links together all LABEL_REFs which refer to
107 code labels outside the loop. Used by routines that need to know all
108 loop exits, such as final_biv_value and final_giv_value.
110 This does not include loop exits due to return instructions. This is
111 because all bivs and givs are pseudos, and hence must be dead after a
112 return, so the presense of a return does not affect any of the
113 optimizations that use this info. It is simpler to just not include return
114 instructions on this list. */
116 rtx *loop_number_exit_labels;
118 /* Indexed by loop number, counts the number of LABEL_REFs on
119 loop_number_exit_labels for this loop and all loops nested inside it. */
121 int *loop_number_exit_count;
123 /* Nonzero if there is a subroutine call in the current loop. */
125 static int loop_has_call;
127 /* Nonzero if there is a volatile memory reference in the current
128 loop. */
130 static int loop_has_volatile;
132 /* Nonzero if there is a tablejump in the current loop. */
134 static int loop_has_tablejump;
136 /* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
137 current loop. A continue statement will generate a branch to
138 NEXT_INSN (loop_continue). */
140 static rtx loop_continue;
142 /* Indexed by register number, contains the number of times the reg
143 is set during the loop being scanned.
144 During code motion, a negative value indicates a reg that has been
145 made a candidate; in particular -2 means that it is an candidate that
146 we know is equal to a constant and -1 means that it is an candidate
147 not known equal to a constant.
148 After code motion, regs moved have 0 (which is accurate now)
149 while the failed candidates have the original number of times set.
151 Therefore, at all times, == 0 indicates an invariant register;
152 < 0 a conditionally invariant one. */
154 static varray_type set_in_loop;
156 /* Original value of set_in_loop; same except that this value
157 is not set negative for a reg whose sets have been made candidates
158 and not set to 0 for a reg that is moved. */
160 static varray_type n_times_set;
162 /* Index by register number, 1 indicates that the register
163 cannot be moved or strength reduced. */
165 static varray_type may_not_optimize;
167 /* Contains the insn in which a register was used if it was used
168 exactly once; contains const0_rtx if it was used more than once. */
170 static varray_type reg_single_usage;
172 /* Nonzero means reg N has already been moved out of one loop.
173 This reduces the desire to move it out of another. */
175 static char *moved_once;
177 /* List of MEMs that are stored in this loop. */
179 static rtx loop_store_mems;
181 /* The insn where the first of these was found. */
182 static rtx first_loop_store_insn;
184 typedef struct loop_mem_info {
185 rtx mem; /* The MEM itself. */
186 rtx reg; /* Corresponding pseudo, if any. */
187 int optimize; /* Nonzero if we can optimize access to this MEM. */
188 } loop_mem_info;
190 /* Array of MEMs that are used (read or written) in this loop, but
191 cannot be aliased by anything in this loop, except perhaps
192 themselves. In other words, if loop_mems[i] is altered during the
193 loop, it is altered by an expression that is rtx_equal_p to it. */
195 static loop_mem_info *loop_mems;
197 /* The index of the next available slot in LOOP_MEMS. */
199 static int loop_mems_idx;
201 /* The number of elements allocated in LOOP_MEMs. */
203 static int loop_mems_allocated;
205 /* Nonzero if we don't know what MEMs were changed in the current loop.
206 This happens if the loop contains a call (in which case `loop_has_call'
207 will also be set) or if we store into more than NUM_STORES MEMs. */
209 static int unknown_address_altered;
211 /* Count of movable (i.e. invariant) instructions discovered in the loop. */
212 static int num_movables;
214 /* Count of memory write instructions discovered in the loop. */
215 static int num_mem_sets;
217 /* Number of loops contained within the current one, including itself. */
218 static int loops_enclosed;
220 /* Bound on pseudo register number before loop optimization.
221 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
222 int max_reg_before_loop;
224 /* This obstack is used in product_cheap_p to allocate its rtl. It
225 may call gen_reg_rtx which, in turn, may reallocate regno_reg_rtx.
226 If we used the same obstack that it did, we would be deallocating
227 that array. */
229 static struct obstack temp_obstack;
231 /* This is where the pointer to the obstack being used for RTL is stored. */
233 extern struct obstack *rtl_obstack;
235 #define obstack_chunk_alloc xmalloc
236 #define obstack_chunk_free free
238 /* During the analysis of a loop, a chain of `struct movable's
239 is made to record all the movable insns found.
240 Then the entire chain can be scanned to decide which to move. */
242 struct movable
244 rtx insn; /* A movable insn */
245 rtx set_src; /* The expression this reg is set from. */
246 rtx set_dest; /* The destination of this SET. */
247 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
248 of any registers used within the LIBCALL. */
249 int consec; /* Number of consecutive following insns
250 that must be moved with this one. */
251 int regno; /* The register it sets */
252 short lifetime; /* lifetime of that register;
253 may be adjusted when matching movables
254 that load the same value are found. */
255 short savings; /* Number of insns we can move for this reg,
256 including other movables that force this
257 or match this one. */
258 unsigned int cond : 1; /* 1 if only conditionally movable */
259 unsigned int force : 1; /* 1 means MUST move this insn */
260 unsigned int global : 1; /* 1 means reg is live outside this loop */
261 /* If PARTIAL is 1, GLOBAL means something different:
262 that the reg is live outside the range from where it is set
263 to the following label. */
264 unsigned int done : 1; /* 1 inhibits further processing of this */
266 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
267 In particular, moving it does not make it
268 invariant. */
269 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
270 load SRC, rather than copying INSN. */
271 unsigned int move_insn_first:1;/* Same as above, if this is necessary for the
272 first insn of a consecutive sets group. */
273 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
274 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
275 that we should avoid changing when clearing
276 the rest of the reg. */
277 struct movable *match; /* First entry for same value */
278 struct movable *forces; /* An insn that must be moved if this is */
279 struct movable *next;
282 static struct movable *the_movables;
284 FILE *loop_dump_stream;
286 /* Forward declarations. */
288 static void verify_dominator PROTO((int));
289 static void find_and_verify_loops PROTO((rtx));
290 static void mark_loop_jump PROTO((rtx, int));
291 static void prescan_loop PROTO((rtx, rtx));
292 static int reg_in_basic_block_p PROTO((rtx, rtx));
293 static int consec_sets_invariant_p PROTO((rtx, int, rtx));
294 static int labels_in_range_p PROTO((rtx, int));
295 static void count_one_set PROTO((rtx, rtx, varray_type, rtx *));
297 static void count_loop_regs_set PROTO((rtx, rtx, varray_type, varray_type,
298 int *, int));
299 static void note_addr_stored PROTO((rtx, rtx));
300 static int loop_reg_used_before_p PROTO((rtx, rtx, rtx, rtx, rtx));
301 static void scan_loop PROTO((rtx, rtx, rtx, int, int));
302 #if 0
303 static void replace_call_address PROTO((rtx, rtx, rtx));
304 #endif
305 static rtx skip_consec_insns PROTO((rtx, int));
306 static int libcall_benefit PROTO((rtx));
307 static void ignore_some_movables PROTO((struct movable *));
308 static void force_movables PROTO((struct movable *));
309 static void combine_movables PROTO((struct movable *, int));
310 static int regs_match_p PROTO((rtx, rtx, struct movable *));
311 static int rtx_equal_for_loop_p PROTO((rtx, rtx, struct movable *));
312 static void add_label_notes PROTO((rtx, rtx));
313 static void move_movables PROTO((struct movable *, int, int, rtx, rtx, int));
314 static int count_nonfixed_reads PROTO((rtx));
315 static void strength_reduce PROTO((rtx, rtx, rtx, int, rtx, rtx, rtx, int, int));
316 static void find_single_use_in_loop PROTO((rtx, rtx, varray_type));
317 static int valid_initial_value_p PROTO((rtx, rtx, int, rtx));
318 static void find_mem_givs PROTO((rtx, rtx, int, rtx, rtx));
319 static void record_biv PROTO((struct induction *, rtx, rtx, rtx, rtx, rtx *, int, int));
320 static void check_final_value PROTO((struct induction *, rtx, rtx,
321 unsigned HOST_WIDE_INT));
322 static void record_giv PROTO((struct induction *, rtx, rtx, rtx, rtx, rtx, int, enum g_types, int, rtx *, rtx, rtx));
323 static void update_giv_derive PROTO((rtx));
324 static int basic_induction_var PROTO((rtx, enum machine_mode, rtx, rtx, rtx *, rtx *, rtx **));
325 static rtx simplify_giv_expr PROTO((rtx, int *));
326 static int general_induction_var PROTO((rtx, rtx *, rtx *, rtx *, int, int *));
327 static int consec_sets_giv PROTO((int, rtx, rtx, rtx, rtx *, rtx *, rtx *));
328 static int check_dbra_loop PROTO((rtx, int, rtx, struct loop_info *));
329 static rtx express_from_1 PROTO((rtx, rtx, rtx));
330 static rtx combine_givs_p PROTO((struct induction *, struct induction *));
331 static void combine_givs PROTO((struct iv_class *));
332 struct recombine_givs_stats;
333 static int find_life_end PROTO((rtx, struct recombine_givs_stats *, rtx, rtx));
334 static void recombine_givs PROTO((struct iv_class *, rtx, rtx, int));
335 static int product_cheap_p PROTO((rtx, rtx));
336 static int maybe_eliminate_biv PROTO((struct iv_class *, rtx, rtx, int, int, int));
337 static int maybe_eliminate_biv_1 PROTO((rtx, rtx, struct iv_class *, int, rtx));
338 static int last_use_this_basic_block PROTO((rtx, rtx));
339 static void record_initial PROTO((rtx, rtx));
340 static void update_reg_last_use PROTO((rtx, rtx));
341 static rtx next_insn_in_loop PROTO((rtx, rtx, rtx, rtx));
342 static void load_mems_and_recount_loop_regs_set PROTO((rtx, rtx, rtx,
343 rtx, int *));
344 static void load_mems PROTO((rtx, rtx, rtx, rtx));
345 static int insert_loop_mem PROTO((rtx *, void *));
346 static int replace_loop_mem PROTO((rtx *, void *));
347 static int replace_label PROTO((rtx *, void *));
349 typedef struct rtx_and_int {
350 rtx r;
351 int i;
352 } rtx_and_int;
354 typedef struct rtx_pair {
355 rtx r1;
356 rtx r2;
357 } rtx_pair;
359 /* Nonzero iff INSN is between START and END, inclusive. */
360 #define INSN_IN_RANGE_P(INSN, START, END) \
361 (INSN_UID (INSN) < max_uid_for_loop \
362 && INSN_LUID (INSN) >= INSN_LUID (START) \
363 && INSN_LUID (INSN) <= INSN_LUID (END))
365 #ifdef HAVE_decrement_and_branch_on_count
366 /* Test whether BCT applicable and safe. */
367 static void insert_bct PROTO((rtx, rtx, struct loop_info *));
369 /* Auxiliary function that inserts the BCT pattern into the loop. */
370 static void instrument_loop_bct PROTO((rtx, rtx, rtx));
371 #endif /* HAVE_decrement_and_branch_on_count */
373 /* Indirect_jump_in_function is computed once per function. */
374 int indirect_jump_in_function = 0;
375 static int indirect_jump_in_function_p PROTO((rtx));
377 static int compute_luids PROTO((rtx, rtx, int));
379 static int biv_elimination_giv_has_0_offset PROTO((struct induction *,
380 struct induction *, rtx));
382 /* Relative gain of eliminating various kinds of operations. */
383 static int add_cost;
384 #if 0
385 static int shift_cost;
386 static int mult_cost;
387 #endif
389 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
390 copy the value of the strength reduced giv to its original register. */
391 static int copy_cost;
393 /* Cost of using a register, to normalize the benefits of a giv. */
394 static int reg_address_cost;
397 void
398 init_loop ()
400 char *free_point = (char *) oballoc (1);
401 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
403 add_cost = rtx_cost (gen_rtx_PLUS (word_mode, reg, reg), SET);
405 #ifdef ADDRESS_COST
406 reg_address_cost = ADDRESS_COST (reg);
407 #else
408 reg_address_cost = rtx_cost (reg, MEM);
409 #endif
411 /* We multiply by 2 to reconcile the difference in scale between
412 these two ways of computing costs. Otherwise the cost of a copy
413 will be far less than the cost of an add. */
415 copy_cost = 2 * 2;
417 /* Free the objects we just allocated. */
418 obfree (free_point);
420 /* Initialize the obstack used for rtl in product_cheap_p. */
421 gcc_obstack_init (&temp_obstack);
424 /* Compute the mapping from uids to luids.
425 LUIDs are numbers assigned to insns, like uids,
426 except that luids increase monotonically through the code.
427 Start at insn START and stop just before END. Assign LUIDs
428 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
429 static int
430 compute_luids (start, end, prev_luid)
431 rtx start, end;
432 int prev_luid;
434 int i;
435 rtx insn;
437 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
439 if (INSN_UID (insn) >= max_uid_for_loop)
440 continue;
441 /* Don't assign luids to line-number NOTEs, so that the distance in
442 luids between two insns is not affected by -g. */
443 if (GET_CODE (insn) != NOTE
444 || NOTE_LINE_NUMBER (insn) <= 0)
445 uid_luid[INSN_UID (insn)] = ++i;
446 else
447 /* Give a line number note the same luid as preceding insn. */
448 uid_luid[INSN_UID (insn)] = i;
450 return i + 1;
453 /* Entry point of this file. Perform loop optimization
454 on the current function. F is the first insn of the function
455 and DUMPFILE is a stream for output of a trace of actions taken
456 (or 0 if none should be output). */
458 void
459 loop_optimize (f, dumpfile, unroll_p, bct_p)
460 /* f is the first instruction of a chain of insns for one function */
461 rtx f;
462 FILE *dumpfile;
463 int unroll_p, bct_p;
465 register rtx insn;
466 register int i;
468 loop_dump_stream = dumpfile;
470 init_recog_no_volatile ();
472 max_reg_before_loop = max_reg_num ();
474 moved_once = (char *) alloca (max_reg_before_loop);
475 bzero (moved_once, max_reg_before_loop);
477 regs_may_share = 0;
479 /* Count the number of loops. */
481 max_loop_num = 0;
482 for (insn = f; insn; insn = NEXT_INSN (insn))
484 if (GET_CODE (insn) == NOTE
485 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
486 max_loop_num++;
489 /* Don't waste time if no loops. */
490 if (max_loop_num == 0)
491 return;
493 /* Get size to use for tables indexed by uids.
494 Leave some space for labels allocated by find_and_verify_loops. */
495 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
497 uid_luid = (int *) alloca (max_uid_for_loop * sizeof (int));
498 uid_loop_num = (int *) alloca (max_uid_for_loop * sizeof (int));
500 bzero ((char *) uid_luid, max_uid_for_loop * sizeof (int));
501 bzero ((char *) uid_loop_num, max_uid_for_loop * sizeof (int));
503 /* Allocate tables for recording each loop. We set each entry, so they need
504 not be zeroed. */
505 loop_number_loop_starts = (rtx *) alloca (max_loop_num * sizeof (rtx));
506 loop_number_loop_ends = (rtx *) alloca (max_loop_num * sizeof (rtx));
507 loop_number_loop_cont = (rtx *) alloca (max_loop_num * sizeof (rtx));
508 loop_number_cont_dominator = (rtx *) alloca (max_loop_num * sizeof (rtx));
509 loop_outer_loop = (int *) alloca (max_loop_num * sizeof (int));
510 loop_invalid = (char *) alloca (max_loop_num * sizeof (char));
511 loop_number_exit_labels = (rtx *) alloca (max_loop_num * sizeof (rtx));
512 loop_number_exit_count = (int *) alloca (max_loop_num * sizeof (int));
514 #ifdef HAVE_decrement_and_branch_on_count
515 /* Allocate for BCT optimization */
516 loop_used_count_register = (int *) alloca (max_loop_num * sizeof (int));
517 bzero ((char *) loop_used_count_register, max_loop_num * sizeof (int));
518 #endif /* HAVE_decrement_and_branch_on_count */
520 /* Find and process each loop.
521 First, find them, and record them in order of their beginnings. */
522 find_and_verify_loops (f);
524 /* Now find all register lifetimes. This must be done after
525 find_and_verify_loops, because it might reorder the insns in the
526 function. */
527 reg_scan (f, max_reg_num (), 1);
529 /* This must occur after reg_scan so that registers created by gcse
530 will have entries in the register tables.
532 We could have added a call to reg_scan after gcse_main in toplev.c,
533 but moving this call to init_alias_analysis is more efficient. */
534 init_alias_analysis ();
536 /* See if we went too far. Note that get_max_uid already returns
537 one more that the maximum uid of all insn. */
538 if (get_max_uid () > max_uid_for_loop)
539 abort ();
540 /* Now reset it to the actual size we need. See above. */
541 max_uid_for_loop = get_max_uid ();
543 /* find_and_verify_loops has already called compute_luids, but it might
544 have rearranged code afterwards, so we need to recompute the luids now. */
545 max_luid = compute_luids (f, NULL_RTX, 0);
547 /* Don't leave gaps in uid_luid for insns that have been
548 deleted. It is possible that the first or last insn
549 using some register has been deleted by cross-jumping.
550 Make sure that uid_luid for that former insn's uid
551 points to the general area where that insn used to be. */
552 for (i = 0; i < max_uid_for_loop; i++)
554 uid_luid[0] = uid_luid[i];
555 if (uid_luid[0] != 0)
556 break;
558 for (i = 0; i < max_uid_for_loop; i++)
559 if (uid_luid[i] == 0)
560 uid_luid[i] = uid_luid[i - 1];
562 /* Create a mapping from loops to BLOCK tree nodes. */
563 if (unroll_p && write_symbols != NO_DEBUG)
564 find_loop_tree_blocks ();
566 /* Determine if the function has indirect jump. On some systems
567 this prevents low overhead loop instructions from being used. */
568 indirect_jump_in_function = indirect_jump_in_function_p (f);
570 /* Now scan the loops, last ones first, since this means inner ones are done
571 before outer ones. */
572 for (i = max_loop_num-1; i >= 0; i--)
573 if (! loop_invalid[i] && loop_number_loop_ends[i])
574 scan_loop (loop_number_loop_starts[i], loop_number_loop_ends[i],
575 loop_number_loop_cont[i], unroll_p, bct_p);
577 /* If debugging and unrolling loops, we must replicate the tree nodes
578 corresponding to the blocks inside the loop, so that the original one
579 to one mapping will remain. */
580 if (unroll_p && write_symbols != NO_DEBUG)
581 unroll_block_trees ();
583 end_alias_analysis ();
586 /* Returns the next insn, in execution order, after INSN. START and
587 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
588 respectively. LOOP_TOP, if non-NULL, is the top of the loop in the
589 insn-stream; it is used with loops that are entered near the
590 bottom. */
592 static rtx
593 next_insn_in_loop (insn, start, end, loop_top)
594 rtx insn;
595 rtx start;
596 rtx end;
597 rtx loop_top;
599 insn = NEXT_INSN (insn);
601 if (insn == end)
603 if (loop_top)
604 /* Go to the top of the loop, and continue there. */
605 insn = loop_top;
606 else
607 /* We're done. */
608 insn = NULL_RTX;
611 if (insn == start)
612 /* We're done. */
613 insn = NULL_RTX;
615 return insn;
618 /* Optimize one loop whose start is LOOP_START and end is END.
619 LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
620 NOTE_INSN_LOOP_END.
621 LOOP_CONT is the NOTE_INSN_LOOP_CONT. */
623 /* ??? Could also move memory writes out of loops if the destination address
624 is invariant, the source is invariant, the memory write is not volatile,
625 and if we can prove that no read inside the loop can read this address
626 before the write occurs. If there is a read of this address after the
627 write, then we can also mark the memory read as invariant. */
629 static void
630 scan_loop (loop_start, end, loop_cont, unroll_p, bct_p)
631 rtx loop_start, end, loop_cont;
632 int unroll_p, bct_p;
634 register int i;
635 rtx p;
636 /* 1 if we are scanning insns that could be executed zero times. */
637 int maybe_never = 0;
638 /* 1 if we are scanning insns that might never be executed
639 due to a subroutine call which might exit before they are reached. */
640 int call_passed = 0;
641 /* For a rotated loop that is entered near the bottom,
642 this is the label at the top. Otherwise it is zero. */
643 rtx loop_top = 0;
644 /* Jump insn that enters the loop, or 0 if control drops in. */
645 rtx loop_entry_jump = 0;
646 /* Place in the loop where control enters. */
647 rtx scan_start;
648 /* Number of insns in the loop. */
649 int insn_count;
650 int in_libcall = 0;
651 int tem;
652 rtx temp;
653 /* The SET from an insn, if it is the only SET in the insn. */
654 rtx set, set1;
655 /* Chain describing insns movable in current loop. */
656 struct movable *movables = 0;
657 /* Last element in `movables' -- so we can add elements at the end. */
658 struct movable *last_movable = 0;
659 /* Ratio of extra register life span we can justify
660 for saving an instruction. More if loop doesn't call subroutines
661 since in that case saving an insn makes more difference
662 and more registers are available. */
663 int threshold;
664 /* Nonzero if we are scanning instructions in a sub-loop. */
665 int loop_depth = 0;
666 int nregs;
668 /* Determine whether this loop starts with a jump down to a test at
669 the end. This will occur for a small number of loops with a test
670 that is too complex to duplicate in front of the loop.
672 We search for the first insn or label in the loop, skipping NOTEs.
673 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
674 (because we might have a loop executed only once that contains a
675 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
676 (in case we have a degenerate loop).
678 Note that if we mistakenly think that a loop is entered at the top
679 when, in fact, it is entered at the exit test, the only effect will be
680 slightly poorer optimization. Making the opposite error can generate
681 incorrect code. Since very few loops now start with a jump to the
682 exit test, the code here to detect that case is very conservative. */
684 for (p = NEXT_INSN (loop_start);
685 p != end
686 && GET_CODE (p) != CODE_LABEL && GET_RTX_CLASS (GET_CODE (p)) != 'i'
687 && (GET_CODE (p) != NOTE
688 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
689 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
690 p = NEXT_INSN (p))
693 scan_start = p;
695 /* Set up variables describing this loop. */
696 prescan_loop (loop_start, end);
697 threshold = (loop_has_call ? 1 : 2) * (1 + n_non_fixed_regs);
699 /* If loop has a jump before the first label,
700 the true entry is the target of that jump.
701 Start scan from there.
702 But record in LOOP_TOP the place where the end-test jumps
703 back to so we can scan that after the end of the loop. */
704 if (GET_CODE (p) == JUMP_INSN)
706 loop_entry_jump = p;
708 /* Loop entry must be unconditional jump (and not a RETURN) */
709 if (simplejump_p (p)
710 && JUMP_LABEL (p) != 0
711 /* Check to see whether the jump actually
712 jumps out of the loop (meaning it's no loop).
713 This case can happen for things like
714 do {..} while (0). If this label was generated previously
715 by loop, we can't tell anything about it and have to reject
716 the loop. */
717 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, end))
719 loop_top = next_label (scan_start);
720 scan_start = JUMP_LABEL (p);
724 /* If SCAN_START was an insn created by loop, we don't know its luid
725 as required by loop_reg_used_before_p. So skip such loops. (This
726 test may never be true, but it's best to play it safe.)
728 Also, skip loops where we do not start scanning at a label. This
729 test also rejects loops starting with a JUMP_INSN that failed the
730 test above. */
732 if (INSN_UID (scan_start) >= max_uid_for_loop
733 || GET_CODE (scan_start) != CODE_LABEL)
735 if (loop_dump_stream)
736 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
737 INSN_UID (loop_start), INSN_UID (end));
738 return;
741 /* Count number of times each reg is set during this loop.
742 Set VARRAY_CHAR (may_not_optimize, I) if it is not safe to move out
743 the setting of register I. Set VARRAY_RTX (reg_single_usage, I). */
745 /* Allocate extra space for REGS that might be created by
746 load_mems. We allocate a little extra slop as well, in the hopes
747 that even after the moving of movables creates some new registers
748 we won't have to reallocate these arrays. However, we do grow
749 the arrays, if necessary, in load_mems_recount_loop_regs_set. */
750 nregs = max_reg_num () + loop_mems_idx + 16;
751 VARRAY_INT_INIT (set_in_loop, nregs, "set_in_loop");
752 VARRAY_INT_INIT (n_times_set, nregs, "n_times_set");
753 VARRAY_CHAR_INIT (may_not_optimize, nregs, "may_not_optimize");
754 VARRAY_RTX_INIT (reg_single_usage, nregs, "reg_single_usage");
756 count_loop_regs_set (loop_top ? loop_top : loop_start, end,
757 may_not_optimize, reg_single_usage, &insn_count, nregs);
759 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
761 VARRAY_CHAR (may_not_optimize, i) = 1;
762 VARRAY_INT (set_in_loop, i) = 1;
765 #ifdef AVOID_CCMODE_COPIES
766 /* Don't try to move insns which set CC registers if we should not
767 create CCmode register copies. */
768 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
769 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
770 VARRAY_CHAR (may_not_optimize, i) = 1;
771 #endif
773 bcopy ((char *) &set_in_loop->data,
774 (char *) &n_times_set->data, nregs * sizeof (int));
776 if (loop_dump_stream)
778 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
779 INSN_UID (loop_start), INSN_UID (end), insn_count);
780 if (loop_continue)
781 fprintf (loop_dump_stream, "Continue at insn %d.\n",
782 INSN_UID (loop_continue));
785 /* Scan through the loop finding insns that are safe to move.
786 Set set_in_loop negative for the reg being set, so that
787 this reg will be considered invariant for subsequent insns.
788 We consider whether subsequent insns use the reg
789 in deciding whether it is worth actually moving.
791 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
792 and therefore it is possible that the insns we are scanning
793 would never be executed. At such times, we must make sure
794 that it is safe to execute the insn once instead of zero times.
795 When MAYBE_NEVER is 0, all insns will be executed at least once
796 so that is not a problem. */
798 for (p = next_insn_in_loop (scan_start, scan_start, end, loop_top);
799 p != NULL_RTX;
800 p = next_insn_in_loop (p, scan_start, end, loop_top))
802 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
803 && find_reg_note (p, REG_LIBCALL, NULL_RTX))
804 in_libcall = 1;
805 else if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
806 && find_reg_note (p, REG_RETVAL, NULL_RTX))
807 in_libcall = 0;
809 if (GET_CODE (p) == INSN
810 && (set = single_set (p))
811 && GET_CODE (SET_DEST (set)) == REG
812 && ! VARRAY_CHAR (may_not_optimize, REGNO (SET_DEST (set))))
814 int tem1 = 0;
815 int tem2 = 0;
816 int move_insn = 0;
817 rtx src = SET_SRC (set);
818 rtx dependencies = 0;
820 /* Figure out what to use as a source of this insn. If a REG_EQUIV
821 note is given or if a REG_EQUAL note with a constant operand is
822 specified, use it as the source and mark that we should move
823 this insn by calling emit_move_insn rather that duplicating the
824 insn.
826 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
827 is present. */
828 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
829 if (temp)
830 src = XEXP (temp, 0), move_insn = 1;
831 else
833 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
834 if (temp && CONSTANT_P (XEXP (temp, 0)))
835 src = XEXP (temp, 0), move_insn = 1;
836 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
838 src = XEXP (temp, 0);
839 /* A libcall block can use regs that don't appear in
840 the equivalent expression. To move the libcall,
841 we must move those regs too. */
842 dependencies = libcall_other_reg (p, src);
846 /* Don't try to optimize a register that was made
847 by loop-optimization for an inner loop.
848 We don't know its life-span, so we can't compute the benefit. */
849 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
851 else if (/* The register is used in basic blocks other
852 than the one where it is set (meaning that
853 something after this point in the loop might
854 depend on its value before the set). */
855 ! reg_in_basic_block_p (p, SET_DEST (set))
856 /* And the set is not guaranteed to be executed one
857 the loop starts, or the value before the set is
858 needed before the set occurs...
860 ??? Note we have quadratic behaviour here, mitigated
861 by the fact that the previous test will often fail for
862 large loops. Rather than re-scanning the entire loop
863 each time for register usage, we should build tables
864 of the register usage and use them here instead. */
865 && (maybe_never
866 || loop_reg_used_before_p (set, p, loop_start,
867 scan_start, end)))
868 /* It is unsafe to move the set.
870 This code used to consider it OK to move a set of a variable
871 which was not created by the user and not used in an exit test.
872 That behavior is incorrect and was removed. */
874 else if ((tem = invariant_p (src))
875 && (dependencies == 0
876 || (tem2 = invariant_p (dependencies)) != 0)
877 && (VARRAY_INT (set_in_loop,
878 REGNO (SET_DEST (set))) == 1
879 || (tem1
880 = consec_sets_invariant_p
881 (SET_DEST (set),
882 VARRAY_INT (set_in_loop, REGNO (SET_DEST (set))),
883 p)))
884 /* If the insn can cause a trap (such as divide by zero),
885 can't move it unless it's guaranteed to be executed
886 once loop is entered. Even a function call might
887 prevent the trap insn from being reached
888 (since it might exit!) */
889 && ! ((maybe_never || call_passed)
890 && may_trap_p (src)))
892 register struct movable *m;
893 register int regno = REGNO (SET_DEST (set));
895 /* A potential lossage is where we have a case where two insns
896 can be combined as long as they are both in the loop, but
897 we move one of them outside the loop. For large loops,
898 this can lose. The most common case of this is the address
899 of a function being called.
901 Therefore, if this register is marked as being used exactly
902 once if we are in a loop with calls (a "large loop"), see if
903 we can replace the usage of this register with the source
904 of this SET. If we can, delete this insn.
906 Don't do this if P has a REG_RETVAL note or if we have
907 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
909 if (loop_has_call
910 && VARRAY_RTX (reg_single_usage, regno) != 0
911 && VARRAY_RTX (reg_single_usage, regno) != const0_rtx
912 && REGNO_FIRST_UID (regno) == INSN_UID (p)
913 && (REGNO_LAST_UID (regno)
914 == INSN_UID (VARRAY_RTX (reg_single_usage, regno)))
915 && VARRAY_INT (set_in_loop, regno) == 1
916 && ! side_effects_p (SET_SRC (set))
917 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
918 && (! SMALL_REGISTER_CLASSES
919 || (! (GET_CODE (SET_SRC (set)) == REG
920 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
921 /* This test is not redundant; SET_SRC (set) might be
922 a call-clobbered register and the life of REGNO
923 might span a call. */
924 && ! modified_between_p (SET_SRC (set), p,
925 VARRAY_RTX
926 (reg_single_usage, regno))
927 && no_labels_between_p (p, VARRAY_RTX (reg_single_usage, regno))
928 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
929 VARRAY_RTX
930 (reg_single_usage, regno)))
932 /* Replace any usage in a REG_EQUAL note. Must copy the
933 new source, so that we don't get rtx sharing between the
934 SET_SOURCE and REG_NOTES of insn p. */
935 REG_NOTES (VARRAY_RTX (reg_single_usage, regno))
936 = replace_rtx (REG_NOTES (VARRAY_RTX
937 (reg_single_usage, regno)),
938 SET_DEST (set), copy_rtx (SET_SRC (set)));
940 PUT_CODE (p, NOTE);
941 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
942 NOTE_SOURCE_FILE (p) = 0;
943 VARRAY_INT (set_in_loop, regno) = 0;
944 continue;
947 m = (struct movable *) alloca (sizeof (struct movable));
948 m->next = 0;
949 m->insn = p;
950 m->set_src = src;
951 m->dependencies = dependencies;
952 m->set_dest = SET_DEST (set);
953 m->force = 0;
954 m->consec = VARRAY_INT (set_in_loop,
955 REGNO (SET_DEST (set))) - 1;
956 m->done = 0;
957 m->forces = 0;
958 m->partial = 0;
959 m->move_insn = move_insn;
960 m->move_insn_first = 0;
961 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
962 m->savemode = VOIDmode;
963 m->regno = regno;
964 /* Set M->cond if either invariant_p or consec_sets_invariant_p
965 returned 2 (only conditionally invariant). */
966 m->cond = ((tem | tem1 | tem2) > 1);
967 m->global = (uid_luid[REGNO_LAST_UID (regno)] > INSN_LUID (end)
968 || uid_luid[REGNO_FIRST_UID (regno)] < INSN_LUID (loop_start));
969 m->match = 0;
970 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
971 - uid_luid[REGNO_FIRST_UID (regno)]);
972 m->savings = VARRAY_INT (n_times_set, regno);
973 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
974 m->savings += libcall_benefit (p);
975 VARRAY_INT (set_in_loop, regno) = move_insn ? -2 : -1;
976 /* Add M to the end of the chain MOVABLES. */
977 if (movables == 0)
978 movables = m;
979 else
980 last_movable->next = m;
981 last_movable = m;
983 if (m->consec > 0)
985 /* It is possible for the first instruction to have a
986 REG_EQUAL note but a non-invariant SET_SRC, so we must
987 remember the status of the first instruction in case
988 the last instruction doesn't have a REG_EQUAL note. */
989 m->move_insn_first = m->move_insn;
991 /* Skip this insn, not checking REG_LIBCALL notes. */
992 p = next_nonnote_insn (p);
993 /* Skip the consecutive insns, if there are any. */
994 p = skip_consec_insns (p, m->consec);
995 /* Back up to the last insn of the consecutive group. */
996 p = prev_nonnote_insn (p);
998 /* We must now reset m->move_insn, m->is_equiv, and possibly
999 m->set_src to correspond to the effects of all the
1000 insns. */
1001 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
1002 if (temp)
1003 m->set_src = XEXP (temp, 0), m->move_insn = 1;
1004 else
1006 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
1007 if (temp && CONSTANT_P (XEXP (temp, 0)))
1008 m->set_src = XEXP (temp, 0), m->move_insn = 1;
1009 else
1010 m->move_insn = 0;
1013 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
1016 /* If this register is always set within a STRICT_LOW_PART
1017 or set to zero, then its high bytes are constant.
1018 So clear them outside the loop and within the loop
1019 just load the low bytes.
1020 We must check that the machine has an instruction to do so.
1021 Also, if the value loaded into the register
1022 depends on the same register, this cannot be done. */
1023 else if (SET_SRC (set) == const0_rtx
1024 && GET_CODE (NEXT_INSN (p)) == INSN
1025 && (set1 = single_set (NEXT_INSN (p)))
1026 && GET_CODE (set1) == SET
1027 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
1028 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
1029 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
1030 == SET_DEST (set))
1031 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
1033 register int regno = REGNO (SET_DEST (set));
1034 if (VARRAY_INT (set_in_loop, regno) == 2)
1036 register struct movable *m;
1037 m = (struct movable *) alloca (sizeof (struct movable));
1038 m->next = 0;
1039 m->insn = p;
1040 m->set_dest = SET_DEST (set);
1041 m->dependencies = 0;
1042 m->force = 0;
1043 m->consec = 0;
1044 m->done = 0;
1045 m->forces = 0;
1046 m->move_insn = 0;
1047 m->move_insn_first = 0;
1048 m->partial = 1;
1049 /* If the insn may not be executed on some cycles,
1050 we can't clear the whole reg; clear just high part.
1051 Not even if the reg is used only within this loop.
1052 Consider this:
1053 while (1)
1054 while (s != t) {
1055 if (foo ()) x = *s;
1056 use (x);
1058 Clearing x before the inner loop could clobber a value
1059 being saved from the last time around the outer loop.
1060 However, if the reg is not used outside this loop
1061 and all uses of the register are in the same
1062 basic block as the store, there is no problem.
1064 If this insn was made by loop, we don't know its
1065 INSN_LUID and hence must make a conservative
1066 assumption. */
1067 m->global = (INSN_UID (p) >= max_uid_for_loop
1068 || (uid_luid[REGNO_LAST_UID (regno)]
1069 > INSN_LUID (end))
1070 || (uid_luid[REGNO_FIRST_UID (regno)]
1071 < INSN_LUID (p))
1072 || (labels_in_range_p
1073 (p, uid_luid[REGNO_FIRST_UID (regno)])));
1074 if (maybe_never && m->global)
1075 m->savemode = GET_MODE (SET_SRC (set1));
1076 else
1077 m->savemode = VOIDmode;
1078 m->regno = regno;
1079 m->cond = 0;
1080 m->match = 0;
1081 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
1082 - uid_luid[REGNO_FIRST_UID (regno)]);
1083 m->savings = 1;
1084 VARRAY_INT (set_in_loop, regno) = -1;
1085 /* Add M to the end of the chain MOVABLES. */
1086 if (movables == 0)
1087 movables = m;
1088 else
1089 last_movable->next = m;
1090 last_movable = m;
1094 /* Past a call insn, we get to insns which might not be executed
1095 because the call might exit. This matters for insns that trap.
1096 Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
1097 so they don't count. */
1098 else if (GET_CODE (p) == CALL_INSN && ! in_libcall)
1099 call_passed = 1;
1100 /* Past a label or a jump, we get to insns for which we
1101 can't count on whether or how many times they will be
1102 executed during each iteration. Therefore, we can
1103 only move out sets of trivial variables
1104 (those not used after the loop). */
1105 /* Similar code appears twice in strength_reduce. */
1106 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1107 /* If we enter the loop in the middle, and scan around to the
1108 beginning, don't set maybe_never for that. This must be an
1109 unconditional jump, otherwise the code at the top of the
1110 loop might never be executed. Unconditional jumps are
1111 followed a by barrier then loop end. */
1112 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop_top
1113 && NEXT_INSN (NEXT_INSN (p)) == end
1114 && simplejump_p (p)))
1115 maybe_never = 1;
1116 else if (GET_CODE (p) == NOTE)
1118 /* At the virtual top of a converted loop, insns are again known to
1119 be executed: logically, the loop begins here even though the exit
1120 code has been duplicated. */
1121 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1122 maybe_never = call_passed = 0;
1123 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1124 loop_depth++;
1125 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1126 loop_depth--;
1130 /* If one movable subsumes another, ignore that other. */
1132 ignore_some_movables (movables);
1134 /* For each movable insn, see if the reg that it loads
1135 leads when it dies right into another conditionally movable insn.
1136 If so, record that the second insn "forces" the first one,
1137 since the second can be moved only if the first is. */
1139 force_movables (movables);
1141 /* See if there are multiple movable insns that load the same value.
1142 If there are, make all but the first point at the first one
1143 through the `match' field, and add the priorities of them
1144 all together as the priority of the first. */
1146 combine_movables (movables, nregs);
1148 /* Now consider each movable insn to decide whether it is worth moving.
1149 Store 0 in set_in_loop for each reg that is moved.
1151 Generally this increases code size, so do not move moveables when
1152 optimizing for code size. */
1154 if (! optimize_size)
1155 move_movables (movables, threshold,
1156 insn_count, loop_start, end, nregs);
1158 /* Now candidates that still are negative are those not moved.
1159 Change set_in_loop to indicate that those are not actually invariant. */
1160 for (i = 0; i < nregs; i++)
1161 if (VARRAY_INT (set_in_loop, i) < 0)
1162 VARRAY_INT (set_in_loop, i) = VARRAY_INT (n_times_set, i);
1164 /* Now that we've moved some things out of the loop, we might be able to
1165 hoist even more memory references. */
1166 load_mems_and_recount_loop_regs_set (scan_start, end, loop_top,
1167 loop_start, &insn_count);
1169 if (flag_strength_reduce)
1171 the_movables = movables;
1172 strength_reduce (scan_start, end, loop_top,
1173 insn_count, loop_start, end, loop_cont, unroll_p, bct_p);
1176 VARRAY_FREE (reg_single_usage);
1177 VARRAY_FREE (set_in_loop);
1178 VARRAY_FREE (n_times_set);
1179 VARRAY_FREE (may_not_optimize);
1182 /* Add elements to *OUTPUT to record all the pseudo-regs
1183 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1185 void
1186 record_excess_regs (in_this, not_in_this, output)
1187 rtx in_this, not_in_this;
1188 rtx *output;
1190 enum rtx_code code;
1191 char *fmt;
1192 int i;
1194 code = GET_CODE (in_this);
1196 switch (code)
1198 case PC:
1199 case CC0:
1200 case CONST_INT:
1201 case CONST_DOUBLE:
1202 case CONST:
1203 case SYMBOL_REF:
1204 case LABEL_REF:
1205 return;
1207 case REG:
1208 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1209 && ! reg_mentioned_p (in_this, not_in_this))
1210 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1211 return;
1213 default:
1214 break;
1217 fmt = GET_RTX_FORMAT (code);
1218 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1220 int j;
1222 switch (fmt[i])
1224 case 'E':
1225 for (j = 0; j < XVECLEN (in_this, i); j++)
1226 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1227 break;
1229 case 'e':
1230 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1231 break;
1236 /* Check what regs are referred to in the libcall block ending with INSN,
1237 aside from those mentioned in the equivalent value.
1238 If there are none, return 0.
1239 If there are one or more, return an EXPR_LIST containing all of them. */
1242 libcall_other_reg (insn, equiv)
1243 rtx insn, equiv;
1245 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1246 rtx p = XEXP (note, 0);
1247 rtx output = 0;
1249 /* First, find all the regs used in the libcall block
1250 that are not mentioned as inputs to the result. */
1252 while (p != insn)
1254 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1255 || GET_CODE (p) == CALL_INSN)
1256 record_excess_regs (PATTERN (p), equiv, &output);
1257 p = NEXT_INSN (p);
1260 return output;
1263 /* Return 1 if all uses of REG
1264 are between INSN and the end of the basic block. */
1266 static int
1267 reg_in_basic_block_p (insn, reg)
1268 rtx insn, reg;
1270 int regno = REGNO (reg);
1271 rtx p;
1273 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1274 return 0;
1276 /* Search this basic block for the already recorded last use of the reg. */
1277 for (p = insn; p; p = NEXT_INSN (p))
1279 switch (GET_CODE (p))
1281 case NOTE:
1282 break;
1284 case INSN:
1285 case CALL_INSN:
1286 /* Ordinary insn: if this is the last use, we win. */
1287 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1288 return 1;
1289 break;
1291 case JUMP_INSN:
1292 /* Jump insn: if this is the last use, we win. */
1293 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1294 return 1;
1295 /* Otherwise, it's the end of the basic block, so we lose. */
1296 return 0;
1298 case CODE_LABEL:
1299 case BARRIER:
1300 /* It's the end of the basic block, so we lose. */
1301 return 0;
1303 default:
1304 break;
1308 /* The "last use" doesn't follow the "first use"?? */
1309 abort ();
1312 /* Compute the benefit of eliminating the insns in the block whose
1313 last insn is LAST. This may be a group of insns used to compute a
1314 value directly or can contain a library call. */
1316 static int
1317 libcall_benefit (last)
1318 rtx last;
1320 rtx insn;
1321 int benefit = 0;
1323 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1324 insn != last; insn = NEXT_INSN (insn))
1326 if (GET_CODE (insn) == CALL_INSN)
1327 benefit += 10; /* Assume at least this many insns in a library
1328 routine. */
1329 else if (GET_CODE (insn) == INSN
1330 && GET_CODE (PATTERN (insn)) != USE
1331 && GET_CODE (PATTERN (insn)) != CLOBBER)
1332 benefit++;
1335 return benefit;
1338 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1340 static rtx
1341 skip_consec_insns (insn, count)
1342 rtx insn;
1343 int count;
1345 for (; count > 0; count--)
1347 rtx temp;
1349 /* If first insn of libcall sequence, skip to end. */
1350 /* Do this at start of loop, since INSN is guaranteed to
1351 be an insn here. */
1352 if (GET_CODE (insn) != NOTE
1353 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1354 insn = XEXP (temp, 0);
1356 do insn = NEXT_INSN (insn);
1357 while (GET_CODE (insn) == NOTE);
1360 return insn;
1363 /* Ignore any movable whose insn falls within a libcall
1364 which is part of another movable.
1365 We make use of the fact that the movable for the libcall value
1366 was made later and so appears later on the chain. */
1368 static void
1369 ignore_some_movables (movables)
1370 struct movable *movables;
1372 register struct movable *m, *m1;
1374 for (m = movables; m; m = m->next)
1376 /* Is this a movable for the value of a libcall? */
1377 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1378 if (note)
1380 rtx insn;
1381 /* Check for earlier movables inside that range,
1382 and mark them invalid. We cannot use LUIDs here because
1383 insns created by loop.c for prior loops don't have LUIDs.
1384 Rather than reject all such insns from movables, we just
1385 explicitly check each insn in the libcall (since invariant
1386 libcalls aren't that common). */
1387 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1388 for (m1 = movables; m1 != m; m1 = m1->next)
1389 if (m1->insn == insn)
1390 m1->done = 1;
1395 /* For each movable insn, see if the reg that it loads
1396 leads when it dies right into another conditionally movable insn.
1397 If so, record that the second insn "forces" the first one,
1398 since the second can be moved only if the first is. */
1400 static void
1401 force_movables (movables)
1402 struct movable *movables;
1404 register struct movable *m, *m1;
1405 for (m1 = movables; m1; m1 = m1->next)
1406 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1407 if (!m1->partial && !m1->done)
1409 int regno = m1->regno;
1410 for (m = m1->next; m; m = m->next)
1411 /* ??? Could this be a bug? What if CSE caused the
1412 register of M1 to be used after this insn?
1413 Since CSE does not update regno_last_uid,
1414 this insn M->insn might not be where it dies.
1415 But very likely this doesn't matter; what matters is
1416 that M's reg is computed from M1's reg. */
1417 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1418 && !m->done)
1419 break;
1420 if (m != 0 && m->set_src == m1->set_dest
1421 /* If m->consec, m->set_src isn't valid. */
1422 && m->consec == 0)
1423 m = 0;
1425 /* Increase the priority of the moving the first insn
1426 since it permits the second to be moved as well. */
1427 if (m != 0)
1429 m->forces = m1;
1430 m1->lifetime += m->lifetime;
1431 m1->savings += m->savings;
1436 /* Find invariant expressions that are equal and can be combined into
1437 one register. */
1439 static void
1440 combine_movables (movables, nregs)
1441 struct movable *movables;
1442 int nregs;
1444 register struct movable *m;
1445 char *matched_regs = (char *) alloca (nregs);
1446 enum machine_mode mode;
1448 /* Regs that are set more than once are not allowed to match
1449 or be matched. I'm no longer sure why not. */
1450 /* Perhaps testing m->consec_sets would be more appropriate here? */
1452 for (m = movables; m; m = m->next)
1453 if (m->match == 0 && VARRAY_INT (n_times_set, m->regno) == 1 && !m->partial)
1455 register struct movable *m1;
1456 int regno = m->regno;
1458 bzero (matched_regs, nregs);
1459 matched_regs[regno] = 1;
1461 /* We want later insns to match the first one. Don't make the first
1462 one match any later ones. So start this loop at m->next. */
1463 for (m1 = m->next; m1; m1 = m1->next)
1464 if (m != m1 && m1->match == 0 && VARRAY_INT (n_times_set, m1->regno) == 1
1465 /* A reg used outside the loop mustn't be eliminated. */
1466 && !m1->global
1467 /* A reg used for zero-extending mustn't be eliminated. */
1468 && !m1->partial
1469 && (matched_regs[m1->regno]
1472 /* Can combine regs with different modes loaded from the
1473 same constant only if the modes are the same or
1474 if both are integer modes with M wider or the same
1475 width as M1. The check for integer is redundant, but
1476 safe, since the only case of differing destination
1477 modes with equal sources is when both sources are
1478 VOIDmode, i.e., CONST_INT. */
1479 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1480 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1481 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1482 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1483 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1484 /* See if the source of M1 says it matches M. */
1485 && ((GET_CODE (m1->set_src) == REG
1486 && matched_regs[REGNO (m1->set_src)])
1487 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1488 movables))))
1489 && ((m->dependencies == m1->dependencies)
1490 || rtx_equal_p (m->dependencies, m1->dependencies)))
1492 m->lifetime += m1->lifetime;
1493 m->savings += m1->savings;
1494 m1->done = 1;
1495 m1->match = m;
1496 matched_regs[m1->regno] = 1;
1500 /* Now combine the regs used for zero-extension.
1501 This can be done for those not marked `global'
1502 provided their lives don't overlap. */
1504 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1505 mode = GET_MODE_WIDER_MODE (mode))
1507 register struct movable *m0 = 0;
1509 /* Combine all the registers for extension from mode MODE.
1510 Don't combine any that are used outside this loop. */
1511 for (m = movables; m; m = m->next)
1512 if (m->partial && ! m->global
1513 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1515 register struct movable *m1;
1516 int first = uid_luid[REGNO_FIRST_UID (m->regno)];
1517 int last = uid_luid[REGNO_LAST_UID (m->regno)];
1519 if (m0 == 0)
1521 /* First one: don't check for overlap, just record it. */
1522 m0 = m;
1523 continue;
1526 /* Make sure they extend to the same mode.
1527 (Almost always true.) */
1528 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1529 continue;
1531 /* We already have one: check for overlap with those
1532 already combined together. */
1533 for (m1 = movables; m1 != m; m1 = m1->next)
1534 if (m1 == m0 || (m1->partial && m1->match == m0))
1535 if (! (uid_luid[REGNO_FIRST_UID (m1->regno)] > last
1536 || uid_luid[REGNO_LAST_UID (m1->regno)] < first))
1537 goto overlap;
1539 /* No overlap: we can combine this with the others. */
1540 m0->lifetime += m->lifetime;
1541 m0->savings += m->savings;
1542 m->done = 1;
1543 m->match = m0;
1545 overlap: ;
1550 /* Return 1 if regs X and Y will become the same if moved. */
1552 static int
1553 regs_match_p (x, y, movables)
1554 rtx x, y;
1555 struct movable *movables;
1557 int xn = REGNO (x);
1558 int yn = REGNO (y);
1559 struct movable *mx, *my;
1561 for (mx = movables; mx; mx = mx->next)
1562 if (mx->regno == xn)
1563 break;
1565 for (my = movables; my; my = my->next)
1566 if (my->regno == yn)
1567 break;
1569 return (mx && my
1570 && ((mx->match == my->match && mx->match != 0)
1571 || mx->match == my
1572 || mx == my->match));
1575 /* Return 1 if X and Y are identical-looking rtx's.
1576 This is the Lisp function EQUAL for rtx arguments.
1578 If two registers are matching movables or a movable register and an
1579 equivalent constant, consider them equal. */
1581 static int
1582 rtx_equal_for_loop_p (x, y, movables)
1583 rtx x, y;
1584 struct movable *movables;
1586 register int i;
1587 register int j;
1588 register struct movable *m;
1589 register enum rtx_code code;
1590 register char *fmt;
1592 if (x == y)
1593 return 1;
1594 if (x == 0 || y == 0)
1595 return 0;
1597 code = GET_CODE (x);
1599 /* If we have a register and a constant, they may sometimes be
1600 equal. */
1601 if (GET_CODE (x) == REG && VARRAY_INT (set_in_loop, REGNO (x)) == -2
1602 && CONSTANT_P (y))
1604 for (m = movables; m; m = m->next)
1605 if (m->move_insn && m->regno == REGNO (x)
1606 && rtx_equal_p (m->set_src, y))
1607 return 1;
1609 else if (GET_CODE (y) == REG && VARRAY_INT (set_in_loop, REGNO (y)) == -2
1610 && CONSTANT_P (x))
1612 for (m = movables; m; m = m->next)
1613 if (m->move_insn && m->regno == REGNO (y)
1614 && rtx_equal_p (m->set_src, x))
1615 return 1;
1618 /* Otherwise, rtx's of different codes cannot be equal. */
1619 if (code != GET_CODE (y))
1620 return 0;
1622 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1623 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1625 if (GET_MODE (x) != GET_MODE (y))
1626 return 0;
1628 /* These three types of rtx's can be compared nonrecursively. */
1629 if (code == REG)
1630 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1632 if (code == LABEL_REF)
1633 return XEXP (x, 0) == XEXP (y, 0);
1634 if (code == SYMBOL_REF)
1635 return XSTR (x, 0) == XSTR (y, 0);
1637 /* Compare the elements. If any pair of corresponding elements
1638 fail to match, return 0 for the whole things. */
1640 fmt = GET_RTX_FORMAT (code);
1641 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1643 switch (fmt[i])
1645 case 'w':
1646 if (XWINT (x, i) != XWINT (y, i))
1647 return 0;
1648 break;
1650 case 'i':
1651 if (XINT (x, i) != XINT (y, i))
1652 return 0;
1653 break;
1655 case 'E':
1656 /* Two vectors must have the same length. */
1657 if (XVECLEN (x, i) != XVECLEN (y, i))
1658 return 0;
1660 /* And the corresponding elements must match. */
1661 for (j = 0; j < XVECLEN (x, i); j++)
1662 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
1663 return 0;
1664 break;
1666 case 'e':
1667 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
1668 return 0;
1669 break;
1671 case 's':
1672 if (strcmp (XSTR (x, i), XSTR (y, i)))
1673 return 0;
1674 break;
1676 case 'u':
1677 /* These are just backpointers, so they don't matter. */
1678 break;
1680 case '0':
1681 break;
1683 /* It is believed that rtx's at this level will never
1684 contain anything but integers and other rtx's,
1685 except for within LABEL_REFs and SYMBOL_REFs. */
1686 default:
1687 abort ();
1690 return 1;
1693 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1694 insns in INSNS which use thet reference. */
1696 static void
1697 add_label_notes (x, insns)
1698 rtx x;
1699 rtx insns;
1701 enum rtx_code code = GET_CODE (x);
1702 int i, j;
1703 char *fmt;
1704 rtx insn;
1706 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1708 /* This code used to ignore labels that referred to dispatch tables to
1709 avoid flow generating (slighly) worse code.
1711 We no longer ignore such label references (see LABEL_REF handling in
1712 mark_jump_label for additional information). */
1713 for (insn = insns; insn; insn = NEXT_INSN (insn))
1714 if (reg_mentioned_p (XEXP (x, 0), insn))
1715 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, XEXP (x, 0),
1716 REG_NOTES (insn));
1719 fmt = GET_RTX_FORMAT (code);
1720 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1722 if (fmt[i] == 'e')
1723 add_label_notes (XEXP (x, i), insns);
1724 else if (fmt[i] == 'E')
1725 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1726 add_label_notes (XVECEXP (x, i, j), insns);
1730 /* Scan MOVABLES, and move the insns that deserve to be moved.
1731 If two matching movables are combined, replace one reg with the
1732 other throughout. */
1734 static void
1735 move_movables (movables, threshold, insn_count, loop_start, end, nregs)
1736 struct movable *movables;
1737 int threshold;
1738 int insn_count;
1739 rtx loop_start;
1740 rtx end;
1741 int nregs;
1743 rtx new_start = 0;
1744 register struct movable *m;
1745 register rtx p;
1746 /* Map of pseudo-register replacements to handle combining
1747 when we move several insns that load the same value
1748 into different pseudo-registers. */
1749 rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
1750 char *already_moved = (char *) alloca (nregs);
1752 bzero (already_moved, nregs);
1753 bzero ((char *) reg_map, nregs * sizeof (rtx));
1755 num_movables = 0;
1757 for (m = movables; m; m = m->next)
1759 /* Describe this movable insn. */
1761 if (loop_dump_stream)
1763 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1764 INSN_UID (m->insn), m->regno, m->lifetime);
1765 if (m->consec > 0)
1766 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1767 if (m->cond)
1768 fprintf (loop_dump_stream, "cond ");
1769 if (m->force)
1770 fprintf (loop_dump_stream, "force ");
1771 if (m->global)
1772 fprintf (loop_dump_stream, "global ");
1773 if (m->done)
1774 fprintf (loop_dump_stream, "done ");
1775 if (m->move_insn)
1776 fprintf (loop_dump_stream, "move-insn ");
1777 if (m->match)
1778 fprintf (loop_dump_stream, "matches %d ",
1779 INSN_UID (m->match->insn));
1780 if (m->forces)
1781 fprintf (loop_dump_stream, "forces %d ",
1782 INSN_UID (m->forces->insn));
1785 /* Count movables. Value used in heuristics in strength_reduce. */
1786 num_movables++;
1788 /* Ignore the insn if it's already done (it matched something else).
1789 Otherwise, see if it is now safe to move. */
1791 if (!m->done
1792 && (! m->cond
1793 || (1 == invariant_p (m->set_src)
1794 && (m->dependencies == 0
1795 || 1 == invariant_p (m->dependencies))
1796 && (m->consec == 0
1797 || 1 == consec_sets_invariant_p (m->set_dest,
1798 m->consec + 1,
1799 m->insn))))
1800 && (! m->forces || m->forces->done))
1802 register int regno;
1803 register rtx p;
1804 int savings = m->savings;
1806 /* We have an insn that is safe to move.
1807 Compute its desirability. */
1809 p = m->insn;
1810 regno = m->regno;
1812 if (loop_dump_stream)
1813 fprintf (loop_dump_stream, "savings %d ", savings);
1815 if (moved_once[regno] && loop_dump_stream)
1816 fprintf (loop_dump_stream, "halved since already moved ");
1818 /* An insn MUST be moved if we already moved something else
1819 which is safe only if this one is moved too: that is,
1820 if already_moved[REGNO] is nonzero. */
1822 /* An insn is desirable to move if the new lifetime of the
1823 register is no more than THRESHOLD times the old lifetime.
1824 If it's not desirable, it means the loop is so big
1825 that moving won't speed things up much,
1826 and it is liable to make register usage worse. */
1828 /* It is also desirable to move if it can be moved at no
1829 extra cost because something else was already moved. */
1831 if (already_moved[regno]
1832 || flag_move_all_movables
1833 || (threshold * savings * m->lifetime) >=
1834 (moved_once[regno] ? insn_count * 2 : insn_count)
1835 || (m->forces && m->forces->done
1836 && VARRAY_INT (n_times_set, m->forces->regno) == 1))
1838 int count;
1839 register struct movable *m1;
1840 rtx first;
1842 /* Now move the insns that set the reg. */
1844 if (m->partial && m->match)
1846 rtx newpat, i1;
1847 rtx r1, r2;
1848 /* Find the end of this chain of matching regs.
1849 Thus, we load each reg in the chain from that one reg.
1850 And that reg is loaded with 0 directly,
1851 since it has ->match == 0. */
1852 for (m1 = m; m1->match; m1 = m1->match);
1853 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1854 SET_DEST (PATTERN (m1->insn)));
1855 i1 = emit_insn_before (newpat, loop_start);
1857 /* Mark the moved, invariant reg as being allowed to
1858 share a hard reg with the other matching invariant. */
1859 REG_NOTES (i1) = REG_NOTES (m->insn);
1860 r1 = SET_DEST (PATTERN (m->insn));
1861 r2 = SET_DEST (PATTERN (m1->insn));
1862 regs_may_share
1863 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1864 gen_rtx_EXPR_LIST (VOIDmode, r2,
1865 regs_may_share));
1866 delete_insn (m->insn);
1868 if (new_start == 0)
1869 new_start = i1;
1871 if (loop_dump_stream)
1872 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1874 /* If we are to re-generate the item being moved with a
1875 new move insn, first delete what we have and then emit
1876 the move insn before the loop. */
1877 else if (m->move_insn)
1879 rtx i1, temp;
1881 for (count = m->consec; count >= 0; count--)
1883 /* If this is the first insn of a library call sequence,
1884 skip to the end. */
1885 if (GET_CODE (p) != NOTE
1886 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1887 p = XEXP (temp, 0);
1889 /* If this is the last insn of a libcall sequence, then
1890 delete every insn in the sequence except the last.
1891 The last insn is handled in the normal manner. */
1892 if (GET_CODE (p) != NOTE
1893 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1895 temp = XEXP (temp, 0);
1896 while (temp != p)
1897 temp = delete_insn (temp);
1900 temp = p;
1901 p = delete_insn (p);
1903 /* simplify_giv_expr expects that it can walk the insns
1904 at m->insn forwards and see this old sequence we are
1905 tossing here. delete_insn does preserve the next
1906 pointers, but when we skip over a NOTE we must fix
1907 it up. Otherwise that code walks into the non-deleted
1908 insn stream. */
1909 while (p && GET_CODE (p) == NOTE)
1910 p = NEXT_INSN (temp) = NEXT_INSN (p);
1913 start_sequence ();
1914 emit_move_insn (m->set_dest, m->set_src);
1915 temp = get_insns ();
1916 end_sequence ();
1918 add_label_notes (m->set_src, temp);
1920 i1 = emit_insns_before (temp, loop_start);
1921 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1922 REG_NOTES (i1)
1923 = gen_rtx_EXPR_LIST (m->is_equiv ? REG_EQUIV : REG_EQUAL,
1924 m->set_src, REG_NOTES (i1));
1926 if (loop_dump_stream)
1927 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1929 /* The more regs we move, the less we like moving them. */
1930 threshold -= 3;
1932 else
1934 for (count = m->consec; count >= 0; count--)
1936 rtx i1, temp;
1938 /* If first insn of libcall sequence, skip to end. */
1939 /* Do this at start of loop, since p is guaranteed to
1940 be an insn here. */
1941 if (GET_CODE (p) != NOTE
1942 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1943 p = XEXP (temp, 0);
1945 /* If last insn of libcall sequence, move all
1946 insns except the last before the loop. The last
1947 insn is handled in the normal manner. */
1948 if (GET_CODE (p) != NOTE
1949 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1951 rtx fn_address = 0;
1952 rtx fn_reg = 0;
1953 rtx fn_address_insn = 0;
1955 first = 0;
1956 for (temp = XEXP (temp, 0); temp != p;
1957 temp = NEXT_INSN (temp))
1959 rtx body;
1960 rtx n;
1961 rtx next;
1963 if (GET_CODE (temp) == NOTE)
1964 continue;
1966 body = PATTERN (temp);
1968 /* Find the next insn after TEMP,
1969 not counting USE or NOTE insns. */
1970 for (next = NEXT_INSN (temp); next != p;
1971 next = NEXT_INSN (next))
1972 if (! (GET_CODE (next) == INSN
1973 && GET_CODE (PATTERN (next)) == USE)
1974 && GET_CODE (next) != NOTE)
1975 break;
1977 /* If that is the call, this may be the insn
1978 that loads the function address.
1980 Extract the function address from the insn
1981 that loads it into a register.
1982 If this insn was cse'd, we get incorrect code.
1984 So emit a new move insn that copies the
1985 function address into the register that the
1986 call insn will use. flow.c will delete any
1987 redundant stores that we have created. */
1988 if (GET_CODE (next) == CALL_INSN
1989 && GET_CODE (body) == SET
1990 && GET_CODE (SET_DEST (body)) == REG
1991 && (n = find_reg_note (temp, REG_EQUAL,
1992 NULL_RTX)))
1994 fn_reg = SET_SRC (body);
1995 if (GET_CODE (fn_reg) != REG)
1996 fn_reg = SET_DEST (body);
1997 fn_address = XEXP (n, 0);
1998 fn_address_insn = temp;
2000 /* We have the call insn.
2001 If it uses the register we suspect it might,
2002 load it with the correct address directly. */
2003 if (GET_CODE (temp) == CALL_INSN
2004 && fn_address != 0
2005 && reg_referenced_p (fn_reg, body))
2006 emit_insn_after (gen_move_insn (fn_reg,
2007 fn_address),
2008 fn_address_insn);
2010 if (GET_CODE (temp) == CALL_INSN)
2012 i1 = emit_call_insn_before (body, loop_start);
2013 /* Because the USAGE information potentially
2014 contains objects other than hard registers
2015 we need to copy it. */
2016 if (CALL_INSN_FUNCTION_USAGE (temp))
2017 CALL_INSN_FUNCTION_USAGE (i1)
2018 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
2020 else
2021 i1 = emit_insn_before (body, loop_start);
2022 if (first == 0)
2023 first = i1;
2024 if (temp == fn_address_insn)
2025 fn_address_insn = i1;
2026 REG_NOTES (i1) = REG_NOTES (temp);
2027 delete_insn (temp);
2029 if (new_start == 0)
2030 new_start = first;
2032 if (m->savemode != VOIDmode)
2034 /* P sets REG to zero; but we should clear only
2035 the bits that are not covered by the mode
2036 m->savemode. */
2037 rtx reg = m->set_dest;
2038 rtx sequence;
2039 rtx tem;
2041 start_sequence ();
2042 tem = expand_binop
2043 (GET_MODE (reg), and_optab, reg,
2044 GEN_INT ((((HOST_WIDE_INT) 1
2045 << GET_MODE_BITSIZE (m->savemode)))
2046 - 1),
2047 reg, 1, OPTAB_LIB_WIDEN);
2048 if (tem == 0)
2049 abort ();
2050 if (tem != reg)
2051 emit_move_insn (reg, tem);
2052 sequence = gen_sequence ();
2053 end_sequence ();
2054 i1 = emit_insn_before (sequence, loop_start);
2056 else if (GET_CODE (p) == CALL_INSN)
2058 i1 = emit_call_insn_before (PATTERN (p), loop_start);
2059 /* Because the USAGE information potentially
2060 contains objects other than hard registers
2061 we need to copy it. */
2062 if (CALL_INSN_FUNCTION_USAGE (p))
2063 CALL_INSN_FUNCTION_USAGE (i1)
2064 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
2066 else if (count == m->consec && m->move_insn_first)
2068 /* The SET_SRC might not be invariant, so we must
2069 use the REG_EQUAL note. */
2070 start_sequence ();
2071 emit_move_insn (m->set_dest, m->set_src);
2072 temp = get_insns ();
2073 end_sequence ();
2075 add_label_notes (m->set_src, temp);
2077 i1 = emit_insns_before (temp, loop_start);
2078 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2079 REG_NOTES (i1)
2080 = gen_rtx_EXPR_LIST ((m->is_equiv ? REG_EQUIV
2081 : REG_EQUAL),
2082 m->set_src, REG_NOTES (i1));
2084 else
2085 i1 = emit_insn_before (PATTERN (p), loop_start);
2087 if (REG_NOTES (i1) == 0)
2089 REG_NOTES (i1) = REG_NOTES (p);
2091 /* If there is a REG_EQUAL note present whose value
2092 is not loop invariant, then delete it, since it
2093 may cause problems with later optimization passes.
2094 It is possible for cse to create such notes
2095 like this as a result of record_jump_cond. */
2097 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2098 && ! invariant_p (XEXP (temp, 0)))
2099 remove_note (i1, temp);
2102 if (new_start == 0)
2103 new_start = i1;
2105 if (loop_dump_stream)
2106 fprintf (loop_dump_stream, " moved to %d",
2107 INSN_UID (i1));
2109 /* If library call, now fix the REG_NOTES that contain
2110 insn pointers, namely REG_LIBCALL on FIRST
2111 and REG_RETVAL on I1. */
2112 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
2114 XEXP (temp, 0) = first;
2115 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2116 XEXP (temp, 0) = i1;
2119 temp = p;
2120 delete_insn (p);
2121 p = NEXT_INSN (p);
2123 /* simplify_giv_expr expects that it can walk the insns
2124 at m->insn forwards and see this old sequence we are
2125 tossing here. delete_insn does preserve the next
2126 pointers, but when we skip over a NOTE we must fix
2127 it up. Otherwise that code walks into the non-deleted
2128 insn stream. */
2129 while (p && GET_CODE (p) == NOTE)
2130 p = NEXT_INSN (temp) = NEXT_INSN (p);
2133 /* The more regs we move, the less we like moving them. */
2134 threshold -= 3;
2137 /* Any other movable that loads the same register
2138 MUST be moved. */
2139 already_moved[regno] = 1;
2141 /* This reg has been moved out of one loop. */
2142 moved_once[regno] = 1;
2144 /* The reg set here is now invariant. */
2145 if (! m->partial)
2146 VARRAY_INT (set_in_loop, regno) = 0;
2148 m->done = 1;
2150 /* Change the length-of-life info for the register
2151 to say it lives at least the full length of this loop.
2152 This will help guide optimizations in outer loops. */
2154 if (uid_luid[REGNO_FIRST_UID (regno)] > INSN_LUID (loop_start))
2155 /* This is the old insn before all the moved insns.
2156 We can't use the moved insn because it is out of range
2157 in uid_luid. Only the old insns have luids. */
2158 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2159 if (uid_luid[REGNO_LAST_UID (regno)] < INSN_LUID (end))
2160 REGNO_LAST_UID (regno) = INSN_UID (end);
2162 /* Combine with this moved insn any other matching movables. */
2164 if (! m->partial)
2165 for (m1 = movables; m1; m1 = m1->next)
2166 if (m1->match == m)
2168 rtx temp;
2170 /* Schedule the reg loaded by M1
2171 for replacement so that shares the reg of M.
2172 If the modes differ (only possible in restricted
2173 circumstances, make a SUBREG. */
2174 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2175 reg_map[m1->regno] = m->set_dest;
2176 else
2177 reg_map[m1->regno]
2178 = gen_lowpart_common (GET_MODE (m1->set_dest),
2179 m->set_dest);
2181 /* Get rid of the matching insn
2182 and prevent further processing of it. */
2183 m1->done = 1;
2185 /* if library call, delete all insn except last, which
2186 is deleted below */
2187 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2188 NULL_RTX)))
2190 for (temp = XEXP (temp, 0); temp != m1->insn;
2191 temp = NEXT_INSN (temp))
2192 delete_insn (temp);
2194 delete_insn (m1->insn);
2196 /* Any other movable that loads the same register
2197 MUST be moved. */
2198 already_moved[m1->regno] = 1;
2200 /* The reg merged here is now invariant,
2201 if the reg it matches is invariant. */
2202 if (! m->partial)
2203 VARRAY_INT (set_in_loop, m1->regno) = 0;
2206 else if (loop_dump_stream)
2207 fprintf (loop_dump_stream, "not desirable");
2209 else if (loop_dump_stream && !m->match)
2210 fprintf (loop_dump_stream, "not safe");
2212 if (loop_dump_stream)
2213 fprintf (loop_dump_stream, "\n");
2216 if (new_start == 0)
2217 new_start = loop_start;
2219 /* Go through all the instructions in the loop, making
2220 all the register substitutions scheduled in REG_MAP. */
2221 for (p = new_start; p != end; p = NEXT_INSN (p))
2222 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2223 || GET_CODE (p) == CALL_INSN)
2225 replace_regs (PATTERN (p), reg_map, nregs, 0);
2226 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2227 INSN_CODE (p) = -1;
2231 #if 0
2232 /* Scan X and replace the address of any MEM in it with ADDR.
2233 REG is the address that MEM should have before the replacement. */
2235 static void
2236 replace_call_address (x, reg, addr)
2237 rtx x, reg, addr;
2239 register enum rtx_code code;
2240 register int i;
2241 register char *fmt;
2243 if (x == 0)
2244 return;
2245 code = GET_CODE (x);
2246 switch (code)
2248 case PC:
2249 case CC0:
2250 case CONST_INT:
2251 case CONST_DOUBLE:
2252 case CONST:
2253 case SYMBOL_REF:
2254 case LABEL_REF:
2255 case REG:
2256 return;
2258 case SET:
2259 /* Short cut for very common case. */
2260 replace_call_address (XEXP (x, 1), reg, addr);
2261 return;
2263 case CALL:
2264 /* Short cut for very common case. */
2265 replace_call_address (XEXP (x, 0), reg, addr);
2266 return;
2268 case MEM:
2269 /* If this MEM uses a reg other than the one we expected,
2270 something is wrong. */
2271 if (XEXP (x, 0) != reg)
2272 abort ();
2273 XEXP (x, 0) = addr;
2274 return;
2276 default:
2277 break;
2280 fmt = GET_RTX_FORMAT (code);
2281 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2283 if (fmt[i] == 'e')
2284 replace_call_address (XEXP (x, i), reg, addr);
2285 if (fmt[i] == 'E')
2287 register int j;
2288 for (j = 0; j < XVECLEN (x, i); j++)
2289 replace_call_address (XVECEXP (x, i, j), reg, addr);
2293 #endif
2295 /* Return the number of memory refs to addresses that vary
2296 in the rtx X. */
2298 static int
2299 count_nonfixed_reads (x)
2300 rtx x;
2302 register enum rtx_code code;
2303 register int i;
2304 register char *fmt;
2305 int value;
2307 if (x == 0)
2308 return 0;
2310 code = GET_CODE (x);
2311 switch (code)
2313 case PC:
2314 case CC0:
2315 case CONST_INT:
2316 case CONST_DOUBLE:
2317 case CONST:
2318 case SYMBOL_REF:
2319 case LABEL_REF:
2320 case REG:
2321 return 0;
2323 case MEM:
2324 return ((invariant_p (XEXP (x, 0)) != 1)
2325 + count_nonfixed_reads (XEXP (x, 0)));
2327 default:
2328 break;
2331 value = 0;
2332 fmt = GET_RTX_FORMAT (code);
2333 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2335 if (fmt[i] == 'e')
2336 value += count_nonfixed_reads (XEXP (x, i));
2337 if (fmt[i] == 'E')
2339 register int j;
2340 for (j = 0; j < XVECLEN (x, i); j++)
2341 value += count_nonfixed_reads (XVECEXP (x, i, j));
2344 return value;
2348 #if 0
2349 /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2350 Replace it with an instruction to load just the low bytes
2351 if the machine supports such an instruction,
2352 and insert above LOOP_START an instruction to clear the register. */
2354 static void
2355 constant_high_bytes (p, loop_start)
2356 rtx p, loop_start;
2358 register rtx new;
2359 register int insn_code_number;
2361 /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2362 to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
2364 new = gen_rtx_SET (VOIDmode,
2365 gen_rtx_STRICT_LOW_PART (VOIDmode,
2366 gen_rtx_SUBREG (GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
2367 SET_DEST (PATTERN (p)),
2368 0)),
2369 XEXP (SET_SRC (PATTERN (p)), 0));
2370 insn_code_number = recog (new, p);
2372 if (insn_code_number)
2374 register int i;
2376 /* Clear destination register before the loop. */
2377 emit_insn_before (gen_rtx_SET (VOIDmode, SET_DEST (PATTERN (p)),
2378 const0_rtx),
2379 loop_start);
2381 /* Inside the loop, just load the low part. */
2382 PATTERN (p) = new;
2385 #endif
2387 /* Scan a loop setting the variables `unknown_address_altered',
2388 `num_mem_sets', `loop_continue', `loops_enclosed', `loop_has_call',
2389 `loop_has_volatile', and `loop_has_tablejump'.
2390 Also, fill in the array `loop_mems' and the list `loop_store_mems'. */
2392 static void
2393 prescan_loop (start, end)
2394 rtx start, end;
2396 register int level = 1;
2397 rtx insn;
2398 int loop_has_multiple_exit_targets = 0;
2399 /* The label after END. Jumping here is just like falling off the
2400 end of the loop. We use next_nonnote_insn instead of next_label
2401 as a hedge against the (pathological) case where some actual insn
2402 might end up between the two. */
2403 rtx exit_target = next_nonnote_insn (end);
2404 if (exit_target == NULL_RTX || GET_CODE (exit_target) != CODE_LABEL)
2405 loop_has_multiple_exit_targets = 1;
2407 unknown_address_altered = 0;
2408 loop_has_call = 0;
2409 loop_has_volatile = 0;
2410 loop_has_tablejump = 0;
2411 loop_store_mems = NULL_RTX;
2412 first_loop_store_insn = NULL_RTX;
2413 loop_mems_idx = 0;
2415 num_mem_sets = 0;
2416 loops_enclosed = 1;
2417 loop_continue = 0;
2419 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2420 insn = NEXT_INSN (insn))
2422 if (GET_CODE (insn) == NOTE)
2424 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2426 ++level;
2427 /* Count number of loops contained in this one. */
2428 loops_enclosed++;
2430 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2432 --level;
2433 if (level == 0)
2435 end = insn;
2436 break;
2439 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2441 if (level == 1)
2442 loop_continue = insn;
2445 else if (GET_CODE (insn) == CALL_INSN)
2447 if (! CONST_CALL_P (insn))
2448 unknown_address_altered = 1;
2449 loop_has_call = 1;
2451 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
2453 rtx label1 = NULL_RTX;
2454 rtx label2 = NULL_RTX;
2456 if (volatile_refs_p (PATTERN (insn)))
2457 loop_has_volatile = 1;
2459 if (GET_CODE (insn) == JUMP_INSN
2460 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2461 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2462 loop_has_tablejump = 1;
2464 note_stores (PATTERN (insn), note_addr_stored);
2465 if (! first_loop_store_insn && loop_store_mems)
2466 first_loop_store_insn = insn;
2468 if (! loop_has_multiple_exit_targets
2469 && GET_CODE (insn) == JUMP_INSN
2470 && GET_CODE (PATTERN (insn)) == SET
2471 && SET_DEST (PATTERN (insn)) == pc_rtx)
2473 if (GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE)
2475 label1 = XEXP (SET_SRC (PATTERN (insn)), 1);
2476 label2 = XEXP (SET_SRC (PATTERN (insn)), 2);
2478 else
2480 label1 = SET_SRC (PATTERN (insn));
2483 do {
2484 if (label1 && label1 != pc_rtx)
2486 if (GET_CODE (label1) != LABEL_REF)
2488 /* Something tricky. */
2489 loop_has_multiple_exit_targets = 1;
2490 break;
2492 else if (XEXP (label1, 0) != exit_target
2493 && LABEL_OUTSIDE_LOOP_P (label1))
2495 /* A jump outside the current loop. */
2496 loop_has_multiple_exit_targets = 1;
2497 break;
2501 label1 = label2;
2502 label2 = NULL_RTX;
2503 } while (label1);
2506 else if (GET_CODE (insn) == RETURN)
2507 loop_has_multiple_exit_targets = 1;
2510 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2511 if (/* We can't tell what MEMs are aliased by what. */
2512 !unknown_address_altered
2513 /* An exception thrown by a called function might land us
2514 anywhere. */
2515 && !loop_has_call
2516 /* We don't want loads for MEMs moved to a location before the
2517 one at which their stack memory becomes allocated. (Note
2518 that this is not a problem for malloc, etc., since those
2519 require actual function calls. */
2520 && !current_function_calls_alloca
2521 /* There are ways to leave the loop other than falling off the
2522 end. */
2523 && !loop_has_multiple_exit_targets)
2524 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2525 insn = NEXT_INSN (insn))
2526 for_each_rtx (&insn, insert_loop_mem, 0);
2529 /* LOOP_NUMBER_CONT_DOMINATOR is now the last label between the loop start
2530 and the continue note that is a the destination of a (cond)jump after
2531 the continue note. If there is any (cond)jump between the loop start
2532 and what we have so far as LOOP_NUMBER_CONT_DOMINATOR that has a
2533 target between LOOP_DOMINATOR and the continue note, move
2534 LOOP_NUMBER_CONT_DOMINATOR forward to that label; if a jump's
2535 destination cannot be determined, clear LOOP_NUMBER_CONT_DOMINATOR. */
2537 static void
2538 verify_dominator (loop_number)
2539 int loop_number;
2541 rtx insn;
2543 if (! loop_number_cont_dominator[loop_number])
2544 /* This can happen for an empty loop, e.g. in
2545 gcc.c-torture/compile/920410-2.c */
2546 return;
2547 if (loop_number_cont_dominator[loop_number] == const0_rtx)
2549 loop_number_cont_dominator[loop_number] = 0;
2550 return;
2552 for (insn = loop_number_loop_starts[loop_number];
2553 insn != loop_number_cont_dominator[loop_number];
2554 insn = NEXT_INSN (insn))
2556 if (GET_CODE (insn) == JUMP_INSN
2557 && GET_CODE (PATTERN (insn)) != RETURN)
2559 rtx label = JUMP_LABEL (insn);
2560 int label_luid = INSN_LUID (label);
2562 if (! condjump_p (insn)
2563 && ! condjump_in_parallel_p (insn))
2565 loop_number_cont_dominator[loop_number] = NULL_RTX;
2566 return;
2568 if (label_luid < INSN_LUID (loop_number_loop_cont[loop_number])
2569 && (label_luid
2570 > INSN_LUID (loop_number_cont_dominator[loop_number])))
2571 loop_number_cont_dominator[loop_number] = label;
2576 /* Scan the function looking for loops. Record the start and end of each loop.
2577 Also mark as invalid loops any loops that contain a setjmp or are branched
2578 to from outside the loop. */
2580 static void
2581 find_and_verify_loops (f)
2582 rtx f;
2584 rtx insn, label;
2585 int current_loop = -1;
2586 int next_loop = -1;
2587 int loop;
2589 compute_luids (f, NULL_RTX, 0);
2591 /* If there are jumps to undefined labels,
2592 treat them as jumps out of any/all loops.
2593 This also avoids writing past end of tables when there are no loops. */
2594 uid_loop_num[0] = -1;
2596 /* Find boundaries of loops, mark which loops are contained within
2597 loops, and invalidate loops that have setjmp. */
2599 for (insn = f; insn; insn = NEXT_INSN (insn))
2601 if (GET_CODE (insn) == NOTE)
2602 switch (NOTE_LINE_NUMBER (insn))
2604 case NOTE_INSN_LOOP_BEG:
2605 loop_number_loop_starts[++next_loop] = insn;
2606 loop_number_loop_ends[next_loop] = 0;
2607 loop_number_loop_cont[next_loop] = 0;
2608 loop_number_cont_dominator[next_loop] = 0;
2609 loop_outer_loop[next_loop] = current_loop;
2610 loop_invalid[next_loop] = 0;
2611 loop_number_exit_labels[next_loop] = 0;
2612 loop_number_exit_count[next_loop] = 0;
2613 current_loop = next_loop;
2614 break;
2616 case NOTE_INSN_SETJMP:
2617 /* In this case, we must invalidate our current loop and any
2618 enclosing loop. */
2619 for (loop = current_loop; loop != -1; loop = loop_outer_loop[loop])
2621 loop_invalid[loop] = 1;
2622 if (loop_dump_stream)
2623 fprintf (loop_dump_stream,
2624 "\nLoop at %d ignored due to setjmp.\n",
2625 INSN_UID (loop_number_loop_starts[loop]));
2627 break;
2629 case NOTE_INSN_LOOP_CONT:
2630 loop_number_loop_cont[current_loop] = insn;
2631 break;
2632 case NOTE_INSN_LOOP_END:
2633 if (current_loop == -1)
2634 abort ();
2636 loop_number_loop_ends[current_loop] = insn;
2637 verify_dominator (current_loop);
2638 current_loop = loop_outer_loop[current_loop];
2639 break;
2641 default:
2642 break;
2644 /* If for any loop, this is a jump insn between the NOTE_INSN_LOOP_CONT
2645 and NOTE_INSN_LOOP_END notes, update loop_number_loop_dominator. */
2646 else if (GET_CODE (insn) == JUMP_INSN
2647 && GET_CODE (PATTERN (insn)) != RETURN
2648 && current_loop >= 0)
2650 int this_loop;
2651 rtx label = JUMP_LABEL (insn);
2653 if (! condjump_p (insn) && ! condjump_in_parallel_p (insn))
2654 label = NULL_RTX;
2656 this_loop = current_loop;
2659 /* First see if we care about this loop. */
2660 if (loop_number_loop_cont[this_loop]
2661 && loop_number_cont_dominator[this_loop] != const0_rtx)
2663 /* If the jump destination is not known, invalidate
2664 loop_number_const_dominator. */
2665 if (! label)
2666 loop_number_cont_dominator[this_loop] = const0_rtx;
2667 else
2668 /* Check if the destination is between loop start and
2669 cont. */
2670 if ((INSN_LUID (label)
2671 < INSN_LUID (loop_number_loop_cont[this_loop]))
2672 && (INSN_LUID (label)
2673 > INSN_LUID (loop_number_loop_starts[this_loop]))
2674 /* And if there is no later destination already
2675 recorded. */
2676 && (! loop_number_cont_dominator[this_loop]
2677 || (INSN_LUID (label)
2678 > INSN_LUID (loop_number_cont_dominator
2679 [this_loop]))))
2680 loop_number_cont_dominator[this_loop] = label;
2682 this_loop = loop_outer_loop[this_loop];
2684 while (this_loop >= 0);
2687 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2688 enclosing loop, but this doesn't matter. */
2689 uid_loop_num[INSN_UID (insn)] = current_loop;
2692 /* Any loop containing a label used in an initializer must be invalidated,
2693 because it can be jumped into from anywhere. */
2695 for (label = forced_labels; label; label = XEXP (label, 1))
2697 int loop_num;
2699 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2700 loop_num != -1;
2701 loop_num = loop_outer_loop[loop_num])
2702 loop_invalid[loop_num] = 1;
2705 /* Any loop containing a label used for an exception handler must be
2706 invalidated, because it can be jumped into from anywhere. */
2708 for (label = exception_handler_labels; label; label = XEXP (label, 1))
2710 int loop_num;
2712 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2713 loop_num != -1;
2714 loop_num = loop_outer_loop[loop_num])
2715 loop_invalid[loop_num] = 1;
2718 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2719 loop that it is not contained within, that loop is marked invalid.
2720 If any INSN or CALL_INSN uses a label's address, then the loop containing
2721 that label is marked invalid, because it could be jumped into from
2722 anywhere.
2724 Also look for blocks of code ending in an unconditional branch that
2725 exits the loop. If such a block is surrounded by a conditional
2726 branch around the block, move the block elsewhere (see below) and
2727 invert the jump to point to the code block. This may eliminate a
2728 label in our loop and will simplify processing by both us and a
2729 possible second cse pass. */
2731 for (insn = f; insn; insn = NEXT_INSN (insn))
2732 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2734 int this_loop_num = uid_loop_num[INSN_UID (insn)];
2736 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2738 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2739 if (note)
2741 int loop_num;
2743 for (loop_num = uid_loop_num[INSN_UID (XEXP (note, 0))];
2744 loop_num != -1;
2745 loop_num = loop_outer_loop[loop_num])
2746 loop_invalid[loop_num] = 1;
2750 if (GET_CODE (insn) != JUMP_INSN)
2751 continue;
2753 mark_loop_jump (PATTERN (insn), this_loop_num);
2755 /* See if this is an unconditional branch outside the loop. */
2756 if (this_loop_num != -1
2757 && (GET_CODE (PATTERN (insn)) == RETURN
2758 || (simplejump_p (insn)
2759 && (uid_loop_num[INSN_UID (JUMP_LABEL (insn))]
2760 != this_loop_num)))
2761 && get_max_uid () < max_uid_for_loop)
2763 rtx p;
2764 rtx our_next = next_real_insn (insn);
2765 int dest_loop;
2766 int outer_loop = -1;
2768 /* Go backwards until we reach the start of the loop, a label,
2769 or a JUMP_INSN. */
2770 for (p = PREV_INSN (insn);
2771 GET_CODE (p) != CODE_LABEL
2772 && ! (GET_CODE (p) == NOTE
2773 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2774 && GET_CODE (p) != JUMP_INSN;
2775 p = PREV_INSN (p))
2778 /* Check for the case where we have a jump to an inner nested
2779 loop, and do not perform the optimization in that case. */
2781 if (JUMP_LABEL (insn))
2783 dest_loop = uid_loop_num[INSN_UID (JUMP_LABEL (insn))];
2784 if (dest_loop != -1)
2786 for (outer_loop = dest_loop; outer_loop != -1;
2787 outer_loop = loop_outer_loop[outer_loop])
2788 if (outer_loop == this_loop_num)
2789 break;
2793 /* Make sure that the target of P is within the current loop. */
2795 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2796 && uid_loop_num[INSN_UID (JUMP_LABEL (p))] != this_loop_num)
2797 outer_loop = this_loop_num;
2799 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2800 we have a block of code to try to move.
2802 We look backward and then forward from the target of INSN
2803 to find a BARRIER at the same loop depth as the target.
2804 If we find such a BARRIER, we make a new label for the start
2805 of the block, invert the jump in P and point it to that label,
2806 and move the block of code to the spot we found. */
2808 if (outer_loop == -1
2809 && GET_CODE (p) == JUMP_INSN
2810 && JUMP_LABEL (p) != 0
2811 /* Just ignore jumps to labels that were never emitted.
2812 These always indicate compilation errors. */
2813 && INSN_UID (JUMP_LABEL (p)) != 0
2814 && condjump_p (p)
2815 && ! simplejump_p (p)
2816 && next_real_insn (JUMP_LABEL (p)) == our_next)
2818 rtx target
2819 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2820 int target_loop_num = uid_loop_num[INSN_UID (target)];
2821 rtx loc;
2823 for (loc = target; loc; loc = PREV_INSN (loc))
2824 if (GET_CODE (loc) == BARRIER
2825 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2826 break;
2828 if (loc == 0)
2829 for (loc = target; loc; loc = NEXT_INSN (loc))
2830 if (GET_CODE (loc) == BARRIER
2831 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2832 break;
2834 if (loc)
2836 rtx cond_label = JUMP_LABEL (p);
2837 rtx new_label = get_label_after (p);
2839 /* Ensure our label doesn't go away. */
2840 LABEL_NUSES (cond_label)++;
2842 /* Verify that uid_loop_num is large enough and that
2843 we can invert P. */
2844 if (invert_jump (p, new_label))
2846 rtx q, r;
2848 /* If no suitable BARRIER was found, create a suitable
2849 one before TARGET. Since TARGET is a fall through
2850 path, we'll need to insert an jump around our block
2851 and a add a BARRIER before TARGET.
2853 This creates an extra unconditional jump outside
2854 the loop. However, the benefits of removing rarely
2855 executed instructions from inside the loop usually
2856 outweighs the cost of the extra unconditional jump
2857 outside the loop. */
2858 if (loc == 0)
2860 rtx temp;
2862 temp = gen_jump (JUMP_LABEL (insn));
2863 temp = emit_jump_insn_before (temp, target);
2864 JUMP_LABEL (temp) = JUMP_LABEL (insn);
2865 LABEL_NUSES (JUMP_LABEL (insn))++;
2866 loc = emit_barrier_before (target);
2869 /* Include the BARRIER after INSN and copy the
2870 block after LOC. */
2871 new_label = squeeze_notes (new_label, NEXT_INSN (insn));
2872 reorder_insns (new_label, NEXT_INSN (insn), loc);
2874 /* All those insns are now in TARGET_LOOP_NUM. */
2875 for (q = new_label; q != NEXT_INSN (NEXT_INSN (insn));
2876 q = NEXT_INSN (q))
2877 uid_loop_num[INSN_UID (q)] = target_loop_num;
2879 /* The label jumped to by INSN is no longer a loop exit.
2880 Unless INSN does not have a label (e.g., it is a
2881 RETURN insn), search loop_number_exit_labels to find
2882 its label_ref, and remove it. Also turn off
2883 LABEL_OUTSIDE_LOOP_P bit. */
2884 if (JUMP_LABEL (insn))
2886 int loop_num;
2888 for (q = 0,
2889 r = loop_number_exit_labels[this_loop_num];
2890 r; q = r, r = LABEL_NEXTREF (r))
2891 if (XEXP (r, 0) == JUMP_LABEL (insn))
2893 LABEL_OUTSIDE_LOOP_P (r) = 0;
2894 if (q)
2895 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2896 else
2897 loop_number_exit_labels[this_loop_num]
2898 = LABEL_NEXTREF (r);
2899 break;
2902 for (loop_num = this_loop_num;
2903 loop_num != -1 && loop_num != target_loop_num;
2904 loop_num = loop_outer_loop[loop_num])
2905 loop_number_exit_count[loop_num]--;
2907 /* If we didn't find it, then something is wrong. */
2908 if (! r)
2909 abort ();
2912 /* P is now a jump outside the loop, so it must be put
2913 in loop_number_exit_labels, and marked as such.
2914 The easiest way to do this is to just call
2915 mark_loop_jump again for P. */
2916 mark_loop_jump (PATTERN (p), this_loop_num);
2918 /* If INSN now jumps to the insn after it,
2919 delete INSN. */
2920 if (JUMP_LABEL (insn) != 0
2921 && (next_real_insn (JUMP_LABEL (insn))
2922 == next_real_insn (insn)))
2923 delete_insn (insn);
2926 /* Continue the loop after where the conditional
2927 branch used to jump, since the only branch insn
2928 in the block (if it still remains) is an inter-loop
2929 branch and hence needs no processing. */
2930 insn = NEXT_INSN (cond_label);
2932 if (--LABEL_NUSES (cond_label) == 0)
2933 delete_insn (cond_label);
2935 /* This loop will be continued with NEXT_INSN (insn). */
2936 insn = PREV_INSN (insn);
2943 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2944 loops it is contained in, mark the target loop invalid.
2946 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2948 static void
2949 mark_loop_jump (x, loop_num)
2950 rtx x;
2951 int loop_num;
2953 int dest_loop;
2954 int outer_loop;
2955 int i;
2957 switch (GET_CODE (x))
2959 case PC:
2960 case USE:
2961 case CLOBBER:
2962 case REG:
2963 case MEM:
2964 case CONST_INT:
2965 case CONST_DOUBLE:
2966 case RETURN:
2967 return;
2969 case CONST:
2970 /* There could be a label reference in here. */
2971 mark_loop_jump (XEXP (x, 0), loop_num);
2972 return;
2974 case PLUS:
2975 case MINUS:
2976 case MULT:
2977 mark_loop_jump (XEXP (x, 0), loop_num);
2978 mark_loop_jump (XEXP (x, 1), loop_num);
2979 return;
2981 case LO_SUM:
2982 /* This may refer to a LABEL_REF or SYMBOL_REF. */
2983 mark_loop_jump (XEXP (x, 1), loop_num);
2984 return;
2986 case SIGN_EXTEND:
2987 case ZERO_EXTEND:
2988 mark_loop_jump (XEXP (x, 0), loop_num);
2989 return;
2991 case LABEL_REF:
2992 dest_loop = uid_loop_num[INSN_UID (XEXP (x, 0))];
2994 /* Link together all labels that branch outside the loop. This
2995 is used by final_[bg]iv_value and the loop unrolling code. Also
2996 mark this LABEL_REF so we know that this branch should predict
2997 false. */
2999 /* A check to make sure the label is not in an inner nested loop,
3000 since this does not count as a loop exit. */
3001 if (dest_loop != -1)
3003 for (outer_loop = dest_loop; outer_loop != -1;
3004 outer_loop = loop_outer_loop[outer_loop])
3005 if (outer_loop == loop_num)
3006 break;
3008 else
3009 outer_loop = -1;
3011 if (loop_num != -1 && outer_loop == -1)
3013 LABEL_OUTSIDE_LOOP_P (x) = 1;
3014 LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
3015 loop_number_exit_labels[loop_num] = x;
3017 for (outer_loop = loop_num;
3018 outer_loop != -1 && outer_loop != dest_loop;
3019 outer_loop = loop_outer_loop[outer_loop])
3020 loop_number_exit_count[outer_loop]++;
3023 /* If this is inside a loop, but not in the current loop or one enclosed
3024 by it, it invalidates at least one loop. */
3026 if (dest_loop == -1)
3027 return;
3029 /* We must invalidate every nested loop containing the target of this
3030 label, except those that also contain the jump insn. */
3032 for (; dest_loop != -1; dest_loop = loop_outer_loop[dest_loop])
3034 /* Stop when we reach a loop that also contains the jump insn. */
3035 for (outer_loop = loop_num; outer_loop != -1;
3036 outer_loop = loop_outer_loop[outer_loop])
3037 if (dest_loop == outer_loop)
3038 return;
3040 /* If we get here, we know we need to invalidate a loop. */
3041 if (loop_dump_stream && ! loop_invalid[dest_loop])
3042 fprintf (loop_dump_stream,
3043 "\nLoop at %d ignored due to multiple entry points.\n",
3044 INSN_UID (loop_number_loop_starts[dest_loop]));
3046 loop_invalid[dest_loop] = 1;
3048 return;
3050 case SET:
3051 /* If this is not setting pc, ignore. */
3052 if (SET_DEST (x) == pc_rtx)
3053 mark_loop_jump (SET_SRC (x), loop_num);
3054 return;
3056 case IF_THEN_ELSE:
3057 mark_loop_jump (XEXP (x, 1), loop_num);
3058 mark_loop_jump (XEXP (x, 2), loop_num);
3059 return;
3061 case PARALLEL:
3062 case ADDR_VEC:
3063 for (i = 0; i < XVECLEN (x, 0); i++)
3064 mark_loop_jump (XVECEXP (x, 0, i), loop_num);
3065 return;
3067 case ADDR_DIFF_VEC:
3068 for (i = 0; i < XVECLEN (x, 1); i++)
3069 mark_loop_jump (XVECEXP (x, 1, i), loop_num);
3070 return;
3072 default:
3073 /* Strictly speaking this is not a jump into the loop, only a possible
3074 jump out of the loop. However, we have no way to link the destination
3075 of this jump onto the list of exit labels. To be safe we mark this
3076 loop and any containing loops as invalid. */
3077 if (loop_num != -1)
3079 for (outer_loop = loop_num; outer_loop != -1;
3080 outer_loop = loop_outer_loop[outer_loop])
3082 if (loop_dump_stream && ! loop_invalid[outer_loop])
3083 fprintf (loop_dump_stream,
3084 "\nLoop at %d ignored due to unknown exit jump.\n",
3085 INSN_UID (loop_number_loop_starts[outer_loop]));
3086 loop_invalid[outer_loop] = 1;
3089 return;
3093 /* Return nonzero if there is a label in the range from
3094 insn INSN to and including the insn whose luid is END
3095 INSN must have an assigned luid (i.e., it must not have
3096 been previously created by loop.c). */
3098 static int
3099 labels_in_range_p (insn, end)
3100 rtx insn;
3101 int end;
3103 while (insn && INSN_LUID (insn) <= end)
3105 if (GET_CODE (insn) == CODE_LABEL)
3106 return 1;
3107 insn = NEXT_INSN (insn);
3110 return 0;
3113 /* Record that a memory reference X is being set. */
3115 static void
3116 note_addr_stored (x, y)
3117 rtx x;
3118 rtx y ATTRIBUTE_UNUSED;
3120 if (x == 0 || GET_CODE (x) != MEM)
3121 return;
3123 /* Count number of memory writes.
3124 This affects heuristics in strength_reduce. */
3125 num_mem_sets++;
3127 /* BLKmode MEM means all memory is clobbered. */
3128 if (GET_MODE (x) == BLKmode)
3129 unknown_address_altered = 1;
3131 if (unknown_address_altered)
3132 return;
3134 loop_store_mems = gen_rtx_EXPR_LIST (VOIDmode, x, loop_store_mems);
3137 /* Return nonzero if the rtx X is invariant over the current loop.
3139 The value is 2 if we refer to something only conditionally invariant.
3141 If `unknown_address_altered' is nonzero, no memory ref is invariant.
3142 Otherwise, a memory ref is invariant if it does not conflict with
3143 anything stored in `loop_store_mems'. */
3146 invariant_p (x)
3147 register rtx x;
3149 register int i;
3150 register enum rtx_code code;
3151 register char *fmt;
3152 int conditional = 0;
3153 rtx mem_list_entry;
3155 if (x == 0)
3156 return 1;
3157 code = GET_CODE (x);
3158 switch (code)
3160 case CONST_INT:
3161 case CONST_DOUBLE:
3162 case SYMBOL_REF:
3163 case CONST:
3164 return 1;
3166 case LABEL_REF:
3167 /* A LABEL_REF is normally invariant, however, if we are unrolling
3168 loops, and this label is inside the loop, then it isn't invariant.
3169 This is because each unrolled copy of the loop body will have
3170 a copy of this label. If this was invariant, then an insn loading
3171 the address of this label into a register might get moved outside
3172 the loop, and then each loop body would end up using the same label.
3174 We don't know the loop bounds here though, so just fail for all
3175 labels. */
3176 if (flag_unroll_loops)
3177 return 0;
3178 else
3179 return 1;
3181 case PC:
3182 case CC0:
3183 case UNSPEC_VOLATILE:
3184 return 0;
3186 case REG:
3187 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3188 since the reg might be set by initialization within the loop. */
3190 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3191 || x == arg_pointer_rtx)
3192 && ! current_function_has_nonlocal_goto)
3193 return 1;
3195 if (loop_has_call
3196 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3197 return 0;
3199 if (VARRAY_INT (set_in_loop, REGNO (x)) < 0)
3200 return 2;
3202 return VARRAY_INT (set_in_loop, REGNO (x)) == 0;
3204 case MEM:
3205 /* Volatile memory references must be rejected. Do this before
3206 checking for read-only items, so that volatile read-only items
3207 will be rejected also. */
3208 if (MEM_VOLATILE_P (x))
3209 return 0;
3211 /* Read-only items (such as constants in a constant pool) are
3212 invariant if their address is. */
3213 if (RTX_UNCHANGING_P (x))
3214 break;
3216 /* If we had a subroutine call, any location in memory could have been
3217 clobbered. */
3218 if (unknown_address_altered)
3219 return 0;
3221 /* See if there is any dependence between a store and this load. */
3222 mem_list_entry = loop_store_mems;
3223 while (mem_list_entry)
3225 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3226 x, rtx_varies_p))
3227 return 0;
3228 mem_list_entry = XEXP (mem_list_entry, 1);
3231 /* It's not invalidated by a store in memory
3232 but we must still verify the address is invariant. */
3233 break;
3235 case ASM_OPERANDS:
3236 /* Don't mess with insns declared volatile. */
3237 if (MEM_VOLATILE_P (x))
3238 return 0;
3239 break;
3241 default:
3242 break;
3245 fmt = GET_RTX_FORMAT (code);
3246 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3248 if (fmt[i] == 'e')
3250 int tem = invariant_p (XEXP (x, i));
3251 if (tem == 0)
3252 return 0;
3253 if (tem == 2)
3254 conditional = 1;
3256 else if (fmt[i] == 'E')
3258 register int j;
3259 for (j = 0; j < XVECLEN (x, i); j++)
3261 int tem = invariant_p (XVECEXP (x, i, j));
3262 if (tem == 0)
3263 return 0;
3264 if (tem == 2)
3265 conditional = 1;
3271 return 1 + conditional;
3275 /* Return nonzero if all the insns in the loop that set REG
3276 are INSN and the immediately following insns,
3277 and if each of those insns sets REG in an invariant way
3278 (not counting uses of REG in them).
3280 The value is 2 if some of these insns are only conditionally invariant.
3282 We assume that INSN itself is the first set of REG
3283 and that its source is invariant. */
3285 static int
3286 consec_sets_invariant_p (reg, n_sets, insn)
3287 int n_sets;
3288 rtx reg, insn;
3290 register rtx p = insn;
3291 register int regno = REGNO (reg);
3292 rtx temp;
3293 /* Number of sets we have to insist on finding after INSN. */
3294 int count = n_sets - 1;
3295 int old = VARRAY_INT (set_in_loop, regno);
3296 int value = 0;
3297 int this;
3299 /* If N_SETS hit the limit, we can't rely on its value. */
3300 if (n_sets == 127)
3301 return 0;
3303 VARRAY_INT (set_in_loop, regno) = 0;
3305 while (count > 0)
3307 register enum rtx_code code;
3308 rtx set;
3310 p = NEXT_INSN (p);
3311 code = GET_CODE (p);
3313 /* If library call, skip to end of it. */
3314 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3315 p = XEXP (temp, 0);
3317 this = 0;
3318 if (code == INSN
3319 && (set = single_set (p))
3320 && GET_CODE (SET_DEST (set)) == REG
3321 && REGNO (SET_DEST (set)) == regno)
3323 this = invariant_p (SET_SRC (set));
3324 if (this != 0)
3325 value |= this;
3326 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3328 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3329 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3330 notes are OK. */
3331 this = (CONSTANT_P (XEXP (temp, 0))
3332 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3333 && invariant_p (XEXP (temp, 0))));
3334 if (this != 0)
3335 value |= this;
3338 if (this != 0)
3339 count--;
3340 else if (code != NOTE)
3342 VARRAY_INT (set_in_loop, regno) = old;
3343 return 0;
3347 VARRAY_INT (set_in_loop, regno) = old;
3348 /* If invariant_p ever returned 2, we return 2. */
3349 return 1 + (value & 2);
3352 #if 0
3353 /* I don't think this condition is sufficient to allow INSN
3354 to be moved, so we no longer test it. */
3356 /* Return 1 if all insns in the basic block of INSN and following INSN
3357 that set REG are invariant according to TABLE. */
3359 static int
3360 all_sets_invariant_p (reg, insn, table)
3361 rtx reg, insn;
3362 short *table;
3364 register rtx p = insn;
3365 register int regno = REGNO (reg);
3367 while (1)
3369 register enum rtx_code code;
3370 p = NEXT_INSN (p);
3371 code = GET_CODE (p);
3372 if (code == CODE_LABEL || code == JUMP_INSN)
3373 return 1;
3374 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3375 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3376 && REGNO (SET_DEST (PATTERN (p))) == regno)
3378 if (!invariant_p (SET_SRC (PATTERN (p)), table))
3379 return 0;
3383 #endif /* 0 */
3385 /* Look at all uses (not sets) of registers in X. For each, if it is
3386 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3387 a different insn, set USAGE[REGNO] to const0_rtx. */
3389 static void
3390 find_single_use_in_loop (insn, x, usage)
3391 rtx insn;
3392 rtx x;
3393 varray_type usage;
3395 enum rtx_code code = GET_CODE (x);
3396 char *fmt = GET_RTX_FORMAT (code);
3397 int i, j;
3399 if (code == REG)
3400 VARRAY_RTX (usage, REGNO (x))
3401 = (VARRAY_RTX (usage, REGNO (x)) != 0
3402 && VARRAY_RTX (usage, REGNO (x)) != insn)
3403 ? const0_rtx : insn;
3405 else if (code == SET)
3407 /* Don't count SET_DEST if it is a REG; otherwise count things
3408 in SET_DEST because if a register is partially modified, it won't
3409 show up as a potential movable so we don't care how USAGE is set
3410 for it. */
3411 if (GET_CODE (SET_DEST (x)) != REG)
3412 find_single_use_in_loop (insn, SET_DEST (x), usage);
3413 find_single_use_in_loop (insn, SET_SRC (x), usage);
3415 else
3416 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3418 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3419 find_single_use_in_loop (insn, XEXP (x, i), usage);
3420 else if (fmt[i] == 'E')
3421 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3422 find_single_use_in_loop (insn, XVECEXP (x, i, j), usage);
3426 /* Count and record any set in X which is contained in INSN. Update
3427 MAY_NOT_MOVE and LAST_SET for any register set in X. */
3429 static void
3430 count_one_set (insn, x, may_not_move, last_set)
3431 rtx insn, x;
3432 varray_type may_not_move;
3433 rtx *last_set;
3435 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3436 /* Don't move a reg that has an explicit clobber.
3437 It's not worth the pain to try to do it correctly. */
3438 VARRAY_CHAR (may_not_move, REGNO (XEXP (x, 0))) = 1;
3440 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3442 rtx dest = SET_DEST (x);
3443 while (GET_CODE (dest) == SUBREG
3444 || GET_CODE (dest) == ZERO_EXTRACT
3445 || GET_CODE (dest) == SIGN_EXTRACT
3446 || GET_CODE (dest) == STRICT_LOW_PART)
3447 dest = XEXP (dest, 0);
3448 if (GET_CODE (dest) == REG)
3450 register int regno = REGNO (dest);
3451 /* If this is the first setting of this reg
3452 in current basic block, and it was set before,
3453 it must be set in two basic blocks, so it cannot
3454 be moved out of the loop. */
3455 if (VARRAY_INT (set_in_loop, regno) > 0
3456 && last_set[regno] == 0)
3457 VARRAY_CHAR (may_not_move, regno) = 1;
3458 /* If this is not first setting in current basic block,
3459 see if reg was used in between previous one and this.
3460 If so, neither one can be moved. */
3461 if (last_set[regno] != 0
3462 && reg_used_between_p (dest, last_set[regno], insn))
3463 VARRAY_CHAR (may_not_move, regno) = 1;
3464 if (VARRAY_INT (set_in_loop, regno) < 127)
3465 ++VARRAY_INT (set_in_loop, regno);
3466 last_set[regno] = insn;
3471 /* Increment SET_IN_LOOP at the index of each register
3472 that is modified by an insn between FROM and TO.
3473 If the value of an element of SET_IN_LOOP becomes 127 or more,
3474 stop incrementing it, to avoid overflow.
3476 Store in SINGLE_USAGE[I] the single insn in which register I is
3477 used, if it is only used once. Otherwise, it is set to 0 (for no
3478 uses) or const0_rtx for more than one use. This parameter may be zero,
3479 in which case this processing is not done.
3481 Store in *COUNT_PTR the number of actual instruction
3482 in the loop. We use this to decide what is worth moving out. */
3484 /* last_set[n] is nonzero iff reg n has been set in the current basic block.
3485 In that case, it is the insn that last set reg n. */
3487 static void
3488 count_loop_regs_set (from, to, may_not_move, single_usage, count_ptr, nregs)
3489 register rtx from, to;
3490 varray_type may_not_move;
3491 varray_type single_usage;
3492 int *count_ptr;
3493 int nregs;
3495 register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
3496 register rtx insn;
3497 register int count = 0;
3499 bzero ((char *) last_set, nregs * sizeof (rtx));
3500 for (insn = from; insn != to; insn = NEXT_INSN (insn))
3502 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3504 ++count;
3506 /* Record registers that have exactly one use. */
3507 find_single_use_in_loop (insn, PATTERN (insn), single_usage);
3509 /* Include uses in REG_EQUAL notes. */
3510 if (REG_NOTES (insn))
3511 find_single_use_in_loop (insn, REG_NOTES (insn), single_usage);
3513 if (GET_CODE (PATTERN (insn)) == SET
3514 || GET_CODE (PATTERN (insn)) == CLOBBER)
3515 count_one_set (insn, PATTERN (insn), may_not_move, last_set);
3516 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3518 register int i;
3519 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3520 count_one_set (insn, XVECEXP (PATTERN (insn), 0, i),
3521 may_not_move, last_set);
3525 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
3526 bzero ((char *) last_set, nregs * sizeof (rtx));
3528 *count_ptr = count;
3531 /* Given a loop that is bounded by LOOP_START and LOOP_END
3532 and that is entered at SCAN_START,
3533 return 1 if the register set in SET contained in insn INSN is used by
3534 any insn that precedes INSN in cyclic order starting
3535 from the loop entry point.
3537 We don't want to use INSN_LUID here because if we restrict INSN to those
3538 that have a valid INSN_LUID, it means we cannot move an invariant out
3539 from an inner loop past two loops. */
3541 static int
3542 loop_reg_used_before_p (set, insn, loop_start, scan_start, loop_end)
3543 rtx set, insn, loop_start, scan_start, loop_end;
3545 rtx reg = SET_DEST (set);
3546 rtx p;
3548 /* Scan forward checking for register usage. If we hit INSN, we
3549 are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
3550 for (p = scan_start; p != insn; p = NEXT_INSN (p))
3552 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
3553 && reg_overlap_mentioned_p (reg, PATTERN (p)))
3554 return 1;
3556 if (p == loop_end)
3557 p = loop_start;
3560 return 0;
3563 /* A "basic induction variable" or biv is a pseudo reg that is set
3564 (within this loop) only by incrementing or decrementing it. */
3565 /* A "general induction variable" or giv is a pseudo reg whose
3566 value is a linear function of a biv. */
3568 /* Bivs are recognized by `basic_induction_var';
3569 Givs by `general_induction_var'. */
3571 /* Indexed by register number, indicates whether or not register is an
3572 induction variable, and if so what type. */
3574 varray_type reg_iv_type;
3576 /* Indexed by register number, contains pointer to `struct induction'
3577 if register is an induction variable. This holds general info for
3578 all induction variables. */
3580 varray_type reg_iv_info;
3582 /* Indexed by register number, contains pointer to `struct iv_class'
3583 if register is a basic induction variable. This holds info describing
3584 the class (a related group) of induction variables that the biv belongs
3585 to. */
3587 struct iv_class **reg_biv_class;
3589 /* The head of a list which links together (via the next field)
3590 every iv class for the current loop. */
3592 struct iv_class *loop_iv_list;
3594 /* Givs made from biv increments are always splittable for loop unrolling.
3595 Since there is no regscan info for them, we have to keep track of them
3596 separately. */
3597 int first_increment_giv, last_increment_giv;
3599 /* Communication with routines called via `note_stores'. */
3601 static rtx note_insn;
3603 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3605 static rtx addr_placeholder;
3607 /* ??? Unfinished optimizations, and possible future optimizations,
3608 for the strength reduction code. */
3610 /* ??? The interaction of biv elimination, and recognition of 'constant'
3611 bivs, may cause problems. */
3613 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3614 performance problems.
3616 Perhaps don't eliminate things that can be combined with an addressing
3617 mode. Find all givs that have the same biv, mult_val, and add_val;
3618 then for each giv, check to see if its only use dies in a following
3619 memory address. If so, generate a new memory address and check to see
3620 if it is valid. If it is valid, then store the modified memory address,
3621 otherwise, mark the giv as not done so that it will get its own iv. */
3623 /* ??? Could try to optimize branches when it is known that a biv is always
3624 positive. */
3626 /* ??? When replace a biv in a compare insn, we should replace with closest
3627 giv so that an optimized branch can still be recognized by the combiner,
3628 e.g. the VAX acb insn. */
3630 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3631 was rerun in loop_optimize whenever a register was added or moved.
3632 Also, some of the optimizations could be a little less conservative. */
3634 /* Perform strength reduction and induction variable elimination.
3636 Pseudo registers created during this function will be beyond the last
3637 valid index in several tables including n_times_set and regno_last_uid.
3638 This does not cause a problem here, because the added registers cannot be
3639 givs outside of their loop, and hence will never be reconsidered.
3640 But scan_loop must check regnos to make sure they are in bounds.
3642 SCAN_START is the first instruction in the loop, as the loop would
3643 actually be executed. END is the NOTE_INSN_LOOP_END. LOOP_TOP is
3644 the first instruction in the loop, as it is layed out in the
3645 instruction stream. LOOP_START is the NOTE_INSN_LOOP_BEG.
3646 LOOP_CONT is the NOTE_INSN_LOOP_CONT. */
3648 static void
3649 strength_reduce (scan_start, end, loop_top, insn_count,
3650 loop_start, loop_end, loop_cont, unroll_p, bct_p)
3651 rtx scan_start;
3652 rtx end;
3653 rtx loop_top;
3654 int insn_count;
3655 rtx loop_start;
3656 rtx loop_end;
3657 rtx loop_cont;
3658 int unroll_p, bct_p ATTRIBUTE_UNUSED;
3660 rtx p;
3661 rtx set;
3662 rtx inc_val;
3663 rtx mult_val;
3664 rtx dest_reg;
3665 rtx *location;
3666 /* This is 1 if current insn is not executed at least once for every loop
3667 iteration. */
3668 int not_every_iteration = 0;
3669 /* This is 1 if current insn may be executed more than once for every
3670 loop iteration. */
3671 int maybe_multiple = 0;
3672 /* Temporary list pointers for traversing loop_iv_list. */
3673 struct iv_class *bl, **backbl;
3674 /* Ratio of extra register life span we can justify
3675 for saving an instruction. More if loop doesn't call subroutines
3676 since in that case saving an insn makes more difference
3677 and more registers are available. */
3678 /* ??? could set this to last value of threshold in move_movables */
3679 int threshold = (loop_has_call ? 1 : 2) * (3 + n_non_fixed_regs);
3680 /* Map of pseudo-register replacements. */
3681 rtx *reg_map;
3682 int reg_map_size;
3683 int call_seen;
3684 rtx test;
3685 rtx end_insert_before;
3686 int loop_depth = 0;
3687 int n_extra_increment;
3688 struct loop_info loop_iteration_info;
3689 struct loop_info *loop_info = &loop_iteration_info;
3691 /* If scan_start points to the loop exit test, we have to be wary of
3692 subversive use of gotos inside expression statements. */
3693 if (prev_nonnote_insn (scan_start) != prev_nonnote_insn (loop_start))
3694 maybe_multiple = back_branch_in_range_p (scan_start, loop_start, loop_end);
3696 VARRAY_INT_INIT (reg_iv_type, max_reg_before_loop, "reg_iv_type");
3697 VARRAY_GENERIC_PTR_INIT (reg_iv_info, max_reg_before_loop, "reg_iv_info");
3698 reg_biv_class = (struct iv_class **)
3699 alloca (max_reg_before_loop * sizeof (struct iv_class *));
3700 bzero ((char *) reg_biv_class, (max_reg_before_loop
3701 * sizeof (struct iv_class *)));
3703 loop_iv_list = 0;
3704 addr_placeholder = gen_reg_rtx (Pmode);
3706 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3707 must be put before this insn, so that they will appear in the right
3708 order (i.e. loop order).
3710 If loop_end is the end of the current function, then emit a
3711 NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3712 dummy note insn. */
3713 if (NEXT_INSN (loop_end) != 0)
3714 end_insert_before = NEXT_INSN (loop_end);
3715 else
3716 end_insert_before = emit_note_after (NOTE_INSN_DELETED, loop_end);
3718 /* Scan through loop to find all possible bivs. */
3720 for (p = next_insn_in_loop (scan_start, scan_start, end, loop_top);
3721 p != NULL_RTX;
3722 p = next_insn_in_loop (p, scan_start, end, loop_top))
3724 if (GET_CODE (p) == INSN
3725 && (set = single_set (p))
3726 && GET_CODE (SET_DEST (set)) == REG)
3728 dest_reg = SET_DEST (set);
3729 if (REGNO (dest_reg) < max_reg_before_loop
3730 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
3731 && REG_IV_TYPE (REGNO (dest_reg)) != NOT_BASIC_INDUCT)
3733 if (basic_induction_var (SET_SRC (set), GET_MODE (SET_SRC (set)),
3734 dest_reg, p, &inc_val, &mult_val,
3735 &location))
3737 /* It is a possible basic induction variable.
3738 Create and initialize an induction structure for it. */
3740 struct induction *v
3741 = (struct induction *) alloca (sizeof (struct induction));
3743 record_biv (v, p, dest_reg, inc_val, mult_val, location,
3744 not_every_iteration, maybe_multiple);
3745 REG_IV_TYPE (REGNO (dest_reg)) = BASIC_INDUCT;
3747 else if (REGNO (dest_reg) < max_reg_before_loop)
3748 REG_IV_TYPE (REGNO (dest_reg)) = NOT_BASIC_INDUCT;
3752 /* Past CODE_LABEL, we get to insns that may be executed multiple
3753 times. The only way we can be sure that they can't is if every
3754 jump insn between here and the end of the loop either
3755 returns, exits the loop, is a jump to a location that is still
3756 behind the label, or is a jump to the loop start. */
3758 if (GET_CODE (p) == CODE_LABEL)
3760 rtx insn = p;
3762 maybe_multiple = 0;
3764 while (1)
3766 insn = NEXT_INSN (insn);
3767 if (insn == scan_start)
3768 break;
3769 if (insn == end)
3771 if (loop_top != 0)
3772 insn = loop_top;
3773 else
3774 break;
3775 if (insn == scan_start)
3776 break;
3779 if (GET_CODE (insn) == JUMP_INSN
3780 && GET_CODE (PATTERN (insn)) != RETURN
3781 && (! condjump_p (insn)
3782 || (JUMP_LABEL (insn) != 0
3783 && JUMP_LABEL (insn) != scan_start
3784 && ! loop_insn_first_p (p, JUMP_LABEL (insn)))))
3786 maybe_multiple = 1;
3787 break;
3792 /* Past a jump, we get to insns for which we can't count
3793 on whether they will be executed during each iteration. */
3794 /* This code appears twice in strength_reduce. There is also similar
3795 code in scan_loop. */
3796 if (GET_CODE (p) == JUMP_INSN
3797 /* If we enter the loop in the middle, and scan around to the
3798 beginning, don't set not_every_iteration for that.
3799 This can be any kind of jump, since we want to know if insns
3800 will be executed if the loop is executed. */
3801 && ! (JUMP_LABEL (p) == loop_top
3802 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3803 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3805 rtx label = 0;
3807 /* If this is a jump outside the loop, then it also doesn't
3808 matter. Check to see if the target of this branch is on the
3809 loop_number_exits_labels list. */
3811 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3812 label;
3813 label = LABEL_NEXTREF (label))
3814 if (XEXP (label, 0) == JUMP_LABEL (p))
3815 break;
3817 if (! label)
3818 not_every_iteration = 1;
3821 else if (GET_CODE (p) == NOTE)
3823 /* At the virtual top of a converted loop, insns are again known to
3824 be executed each iteration: logically, the loop begins here
3825 even though the exit code has been duplicated.
3827 Insns are also again known to be executed each iteration at
3828 the LOOP_CONT note. */
3829 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
3830 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
3831 && loop_depth == 0)
3832 not_every_iteration = 0;
3833 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3834 loop_depth++;
3835 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3836 loop_depth--;
3839 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3840 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3841 or not an insn is known to be executed each iteration of the
3842 loop, whether or not any iterations are known to occur.
3844 Therefore, if we have just passed a label and have no more labels
3845 between here and the test insn of the loop, we know these insns
3846 will be executed each iteration. */
3848 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3849 && no_labels_between_p (p, loop_end)
3850 && loop_insn_first_p (p, loop_cont))
3851 not_every_iteration = 0;
3854 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3855 Make a sanity check against n_times_set. */
3856 for (backbl = &loop_iv_list, bl = *backbl; bl; bl = bl->next)
3858 if (REG_IV_TYPE (bl->regno) != BASIC_INDUCT
3859 /* Above happens if register modified by subreg, etc. */
3860 /* Make sure it is not recognized as a basic induction var: */
3861 || VARRAY_INT (n_times_set, bl->regno) != bl->biv_count
3862 /* If never incremented, it is invariant that we decided not to
3863 move. So leave it alone. */
3864 || ! bl->incremented)
3866 if (loop_dump_stream)
3867 fprintf (loop_dump_stream, "Reg %d: biv discarded, %s\n",
3868 bl->regno,
3869 (REG_IV_TYPE (bl->regno) != BASIC_INDUCT
3870 ? "not induction variable"
3871 : (! bl->incremented ? "never incremented"
3872 : "count error")));
3874 REG_IV_TYPE (bl->regno) = NOT_BASIC_INDUCT;
3875 *backbl = bl->next;
3877 else
3879 backbl = &bl->next;
3881 if (loop_dump_stream)
3882 fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
3886 /* Exit if there are no bivs. */
3887 if (! loop_iv_list)
3889 /* Can still unroll the loop anyways, but indicate that there is no
3890 strength reduction info available. */
3891 if (unroll_p)
3892 unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
3893 loop_info, 0);
3895 return;
3898 /* Find initial value for each biv by searching backwards from loop_start,
3899 halting at first label. Also record any test condition. */
3901 call_seen = 0;
3902 for (p = loop_start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3904 note_insn = p;
3906 if (GET_CODE (p) == CALL_INSN)
3907 call_seen = 1;
3909 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3910 || GET_CODE (p) == CALL_INSN)
3911 note_stores (PATTERN (p), record_initial);
3913 /* Record any test of a biv that branches around the loop if no store
3914 between it and the start of loop. We only care about tests with
3915 constants and registers and only certain of those. */
3916 if (GET_CODE (p) == JUMP_INSN
3917 && JUMP_LABEL (p) != 0
3918 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end)
3919 && (test = get_condition_for_loop (p)) != 0
3920 && GET_CODE (XEXP (test, 0)) == REG
3921 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3922 && (bl = reg_biv_class[REGNO (XEXP (test, 0))]) != 0
3923 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop_start)
3924 && bl->init_insn == 0)
3926 /* If an NE test, we have an initial value! */
3927 if (GET_CODE (test) == NE)
3929 bl->init_insn = p;
3930 bl->init_set = gen_rtx_SET (VOIDmode,
3931 XEXP (test, 0), XEXP (test, 1));
3933 else
3934 bl->initial_test = test;
3938 /* Look at the each biv and see if we can say anything better about its
3939 initial value from any initializing insns set up above. (This is done
3940 in two passes to avoid missing SETs in a PARALLEL.) */
3941 for (backbl = &loop_iv_list; (bl = *backbl); backbl = &bl->next)
3943 rtx src;
3944 rtx note;
3946 if (! bl->init_insn)
3947 continue;
3949 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
3950 is a constant, use the value of that. */
3951 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
3952 && CONSTANT_P (XEXP (note, 0)))
3953 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
3954 && CONSTANT_P (XEXP (note, 0))))
3955 src = XEXP (note, 0);
3956 else
3957 src = SET_SRC (bl->init_set);
3959 if (loop_dump_stream)
3960 fprintf (loop_dump_stream,
3961 "Biv %d initialized at insn %d: initial value ",
3962 bl->regno, INSN_UID (bl->init_insn));
3964 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
3965 || GET_MODE (src) == VOIDmode)
3966 && valid_initial_value_p (src, bl->init_insn, call_seen, loop_start))
3968 bl->initial_value = src;
3970 if (loop_dump_stream)
3972 if (GET_CODE (src) == CONST_INT)
3974 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (src));
3975 fputc ('\n', loop_dump_stream);
3977 else
3979 print_rtl (loop_dump_stream, src);
3980 fprintf (loop_dump_stream, "\n");
3984 else
3986 struct iv_class *bl2 = 0;
3987 rtx increment;
3989 /* Biv initial value is not a simple move. If it is the sum of
3990 another biv and a constant, check if both bivs are incremented
3991 in lockstep. Then we are actually looking at a giv.
3992 For simplicity, we only handle the case where there is but a
3993 single increment, and the register is not used elsewhere. */
3994 if (bl->biv_count == 1
3995 && bl->regno < max_reg_before_loop
3996 && uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
3997 && GET_CODE (src) == PLUS
3998 && GET_CODE (XEXP (src, 0)) == REG
3999 && CONSTANT_P (XEXP (src, 1))
4000 && ((increment = biv_total_increment (bl, loop_start, loop_end))
4001 != NULL_RTX))
4003 int regno = REGNO (XEXP (src, 0));
4005 for (bl2 = loop_iv_list; bl2; bl2 = bl2->next)
4006 if (bl2->regno == regno)
4007 break;
4010 /* Now, can we transform this biv into a giv? */
4011 if (bl2
4012 && bl2->biv_count == 1
4013 && rtx_equal_p (increment,
4014 biv_total_increment (bl2, loop_start, loop_end))
4015 /* init_insn is only set to insns that are before loop_start
4016 without any intervening labels. */
4017 && ! reg_set_between_p (bl2->biv->src_reg,
4018 PREV_INSN (bl->init_insn), loop_start)
4019 /* The register from BL2 must be set before the register from
4020 BL is set, or we must be able to move the latter set after
4021 the former set. Currently there can't be any labels
4022 in-between when biv_toal_increment returns nonzero both times
4023 but we test it here in case some day some real cfg analysis
4024 gets used to set always_computable. */
4025 && ((loop_insn_first_p (bl2->biv->insn, bl->biv->insn)
4026 && no_labels_between_p (bl2->biv->insn, bl->biv->insn))
4027 || (! reg_used_between_p (bl->biv->src_reg, bl->biv->insn,
4028 bl2->biv->insn)
4029 && no_jumps_between_p (bl->biv->insn, bl2->biv->insn)))
4030 && validate_change (bl->biv->insn,
4031 &SET_SRC (single_set (bl->biv->insn)),
4032 copy_rtx (src), 0))
4034 int loop_num = uid_loop_num[INSN_UID (loop_start)];
4035 rtx dominator = loop_number_cont_dominator[loop_num];
4036 rtx giv = bl->biv->src_reg;
4037 rtx giv_insn = bl->biv->insn;
4038 rtx after_giv = NEXT_INSN (giv_insn);
4040 if (loop_dump_stream)
4041 fprintf (loop_dump_stream, "is giv of biv %d\n", bl2->regno);
4042 /* Let this giv be discovered by the generic code. */
4043 REG_IV_TYPE (bl->regno) = UNKNOWN_INDUCT;
4044 /* We can get better optimization if we can move the giv setting
4045 before the first giv use. */
4046 if (dominator
4047 && ! loop_insn_first_p (dominator, scan_start)
4048 && ! reg_set_between_p (bl2->biv->src_reg, loop_start,
4049 dominator)
4050 && ! reg_used_between_p (giv, loop_start, dominator)
4051 && ! reg_used_between_p (giv, giv_insn, loop_end))
4053 rtx p;
4054 rtx next;
4056 for (next = NEXT_INSN (dominator); ; next = NEXT_INSN (next))
4058 if ((GET_RTX_CLASS (GET_CODE (next)) == 'i'
4059 && (reg_mentioned_p (giv, PATTERN (next))
4060 || reg_set_p (bl2->biv->src_reg, next)))
4061 || GET_CODE (next) == JUMP_INSN)
4062 break;
4063 #ifdef HAVE_cc0
4064 if (GET_RTX_CLASS (GET_CODE (next)) != 'i'
4065 || ! sets_cc0_p (PATTERN (next)))
4066 #endif
4067 dominator = next;
4069 if (loop_dump_stream)
4070 fprintf (loop_dump_stream, "move after insn %d\n",
4071 INSN_UID (dominator));
4072 /* Avoid problems with luids by actually moving the insn
4073 and adjusting all luids in the range. */
4074 reorder_insns (giv_insn, giv_insn, dominator);
4075 for (p = dominator; INSN_UID (p) >= max_uid_for_loop; )
4076 p = PREV_INSN (p);
4077 compute_luids (giv_insn, after_giv, INSN_LUID (p));
4078 /* If the only purpose of the init insn is to initialize
4079 this giv, delete it. */
4080 if (single_set (bl->init_insn)
4081 && ! reg_used_between_p (giv, bl->init_insn, loop_start))
4082 delete_insn (bl->init_insn);
4084 else if (! loop_insn_first_p (bl2->biv->insn, bl->biv->insn))
4086 rtx p = PREV_INSN (giv_insn);
4087 while (INSN_UID (p) >= max_uid_for_loop)
4088 p = PREV_INSN (p);
4089 reorder_insns (giv_insn, giv_insn, bl2->biv->insn);
4090 compute_luids (after_giv, NEXT_INSN (giv_insn),
4091 INSN_LUID (p));
4093 /* Remove this biv from the chain. */
4094 if (bl->next)
4095 *bl = *bl->next;
4096 else
4098 *backbl = 0;
4099 break;
4103 /* If we can't make it a giv,
4104 let biv keep initial value of "itself". */
4105 else if (loop_dump_stream)
4106 fprintf (loop_dump_stream, "is complex\n");
4110 /* If a biv is unconditionally incremented several times in a row, convert
4111 all but the last increment into a giv. */
4113 /* Get an upper bound for the number of registers
4114 we might have after all bivs have been processed. */
4115 first_increment_giv = max_reg_num ();
4116 for (n_extra_increment = 0, bl = loop_iv_list; bl; bl = bl->next)
4117 n_extra_increment += bl->biv_count - 1;
4119 /* If the loop contains volatile memory references do not allow any
4120 replacements to take place, since this could loose the volatile markers. */
4121 if (n_extra_increment && ! loop_has_volatile)
4123 int nregs = first_increment_giv + n_extra_increment;
4125 /* Reallocate reg_iv_type and reg_iv_info. */
4126 VARRAY_GROW (reg_iv_type, nregs);
4127 VARRAY_GROW (reg_iv_info, nregs);
4129 for (bl = loop_iv_list; bl; bl = bl->next)
4131 struct induction **vp, *v, *next;
4132 int biv_dead_after_loop = 0;
4134 /* The biv increments lists are in reverse order. Fix this first. */
4135 for (v = bl->biv, bl->biv = 0; v; v = next)
4137 next = v->next_iv;
4138 v->next_iv = bl->biv;
4139 bl->biv = v;
4142 /* We must guard against the case that an early exit between v->insn
4143 and next->insn leaves the biv live after the loop, since that
4144 would mean that we'd be missing an increment for the final
4145 value. The following test to set biv_dead_after_loop is like
4146 the first part of the test to set bl->eliminable.
4147 We don't check here if we can calculate the final value, since
4148 this can't succeed if we already know that there is a jump
4149 between v->insn and next->insn, yet next->always_executed is
4150 set and next->maybe_multiple is cleared. Such a combination
4151 implies that the jump destination is outside the loop.
4152 If we want to make this check more sophisticated, we should
4153 check each branch between v->insn and next->insn individually
4154 to see if the biv is dead at its destination. */
4156 if (uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
4157 && bl->init_insn
4158 && INSN_UID (bl->init_insn) < max_uid_for_loop
4159 && (uid_luid[REGNO_FIRST_UID (bl->regno)]
4160 >= INSN_LUID (bl->init_insn))
4161 #ifdef HAVE_decrement_and_branch_until_zero
4162 && ! bl->nonneg
4163 #endif
4164 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
4165 biv_dead_after_loop = 1;
4167 for (vp = &bl->biv, next = *vp; v = next, next = v->next_iv;)
4169 HOST_WIDE_INT offset;
4170 rtx set, add_val, old_reg, dest_reg, last_use_insn;
4171 int old_regno, new_regno;
4173 if (! v->always_executed
4174 || v->maybe_multiple
4175 || GET_CODE (v->add_val) != CONST_INT
4176 || ! next->always_executed
4177 || next->maybe_multiple
4178 || ! CONSTANT_P (next->add_val)
4179 || ! (biv_dead_after_loop
4180 || no_jumps_between_p (v->insn, next->insn)))
4182 vp = &v->next_iv;
4183 continue;
4185 offset = INTVAL (v->add_val);
4186 set = single_set (v->insn);
4187 add_val = plus_constant (next->add_val, offset);
4188 old_reg = v->dest_reg;
4189 dest_reg = gen_reg_rtx (v->mode);
4191 /* Unlike reg_iv_type / reg_iv_info, the other three arrays
4192 have been allocated with some slop space, so we may not
4193 actually need to reallocate them. If we do, the following
4194 if statement will be executed just once in this loop. */
4195 if ((unsigned) max_reg_num () > n_times_set->num_elements)
4197 /* Grow all the remaining arrays. */
4198 VARRAY_GROW (set_in_loop, nregs);
4199 VARRAY_GROW (n_times_set, nregs);
4200 VARRAY_GROW (may_not_optimize, nregs);
4203 if (! validate_change (next->insn, next->location, add_val, 0))
4205 vp = &v->next_iv;
4206 continue;
4209 /* Here we can try to eliminate the increment by combining
4210 it into the uses. */
4212 /* Set last_use_insn so that we can check against it. */
4214 for (last_use_insn = v->insn, p = NEXT_INSN (v->insn);
4215 p != next->insn;
4216 p = next_insn_in_loop (p, scan_start, end, loop_top))
4218 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
4219 continue;
4220 if (reg_mentioned_p (old_reg, PATTERN (p)))
4222 last_use_insn = p;
4226 /* If we can't get the LUIDs for the insns, we can't
4227 calculate the lifetime. This is likely from unrolling
4228 of an inner loop, so there is little point in making this
4229 a DEST_REG giv anyways. */
4230 if (INSN_UID (v->insn) >= max_uid_for_loop
4231 || INSN_UID (last_use_insn) >= max_uid_for_loop
4232 || ! validate_change (v->insn, &SET_DEST (set), dest_reg, 0))
4234 /* Change the increment at NEXT back to what it was. */
4235 if (! validate_change (next->insn, next->location,
4236 next->add_val, 0))
4237 abort ();
4238 vp = &v->next_iv;
4239 continue;
4241 next->add_val = add_val;
4242 v->dest_reg = dest_reg;
4243 v->giv_type = DEST_REG;
4244 v->location = &SET_SRC (set);
4245 v->cant_derive = 0;
4246 v->combined_with = 0;
4247 v->maybe_dead = 0;
4248 v->derive_adjustment = 0;
4249 v->same = 0;
4250 v->ignore = 0;
4251 v->new_reg = 0;
4252 v->final_value = 0;
4253 v->same_insn = 0;
4254 v->auto_inc_opt = 0;
4255 v->unrolled = 0;
4256 v->shared = 0;
4257 v->derived_from = 0;
4258 v->always_computable = 1;
4259 v->always_executed = 1;
4260 v->replaceable = 1;
4261 v->no_const_addval = 0;
4263 old_regno = REGNO (old_reg);
4264 new_regno = REGNO (dest_reg);
4265 VARRAY_INT (set_in_loop, old_regno)--;
4266 VARRAY_INT (set_in_loop, new_regno) = 1;
4267 VARRAY_INT (n_times_set, old_regno)--;
4268 VARRAY_INT (n_times_set, new_regno) = 1;
4269 VARRAY_CHAR (may_not_optimize, new_regno) = 0;
4271 REG_IV_TYPE (new_regno) = GENERAL_INDUCT;
4272 REG_IV_INFO (new_regno) = v;
4274 /* Remove the increment from the list of biv increments,
4275 and record it as a giv. */
4276 *vp = next;
4277 bl->biv_count--;
4278 v->next_iv = bl->giv;
4279 bl->giv = v;
4280 bl->giv_count++;
4281 v->benefit = rtx_cost (SET_SRC (set), SET);
4282 bl->total_benefit += v->benefit;
4284 /* Now replace the biv with DEST_REG in all insns between
4285 the replaced increment and the next increment, and
4286 remember the last insn that needed a replacement. */
4287 for (last_use_insn = v->insn, p = NEXT_INSN (v->insn);
4288 p != next->insn;
4289 p = next_insn_in_loop (p, scan_start, end, loop_top))
4291 rtx note;
4293 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
4294 continue;
4295 if (reg_mentioned_p (old_reg, PATTERN (p)))
4297 last_use_insn = p;
4298 if (! validate_replace_rtx (old_reg, dest_reg, p))
4299 abort ();
4301 for (note = REG_NOTES (p); note; note = XEXP (note, 1))
4303 if (GET_CODE (note) == EXPR_LIST)
4304 XEXP (note, 0)
4305 = replace_rtx (XEXP (note, 0), old_reg, dest_reg);
4309 v->last_use = last_use_insn;
4310 v->lifetime = INSN_LUID (v->insn) - INSN_LUID (last_use_insn);
4311 /* If the lifetime is zero, it means that this register is really
4312 a dead store. So mark this as a giv that can be ignored.
4313 This will not prevent the biv from being eliminated. */
4314 if (v->lifetime == 0)
4315 v->ignore = 1;
4317 if (loop_dump_stream)
4318 fprintf (loop_dump_stream,
4319 "Increment %d of biv %d converted to giv %d.\n\n",
4320 INSN_UID (v->insn), old_regno, new_regno);
4324 last_increment_giv = max_reg_num () - 1;
4326 /* Search the loop for general induction variables. */
4328 /* A register is a giv if: it is only set once, it is a function of a
4329 biv and a constant (or invariant), and it is not a biv. */
4331 not_every_iteration = 0;
4332 loop_depth = 0;
4333 p = scan_start;
4334 while (1)
4336 p = NEXT_INSN (p);
4337 /* At end of a straight-in loop, we are done.
4338 At end of a loop entered at the bottom, scan the top. */
4339 if (p == scan_start)
4340 break;
4341 if (p == end)
4343 if (loop_top != 0)
4344 p = loop_top;
4345 else
4346 break;
4347 if (p == scan_start)
4348 break;
4351 /* Look for a general induction variable in a register. */
4352 if (GET_CODE (p) == INSN
4353 && (set = single_set (p))
4354 && GET_CODE (SET_DEST (set)) == REG
4355 && ! VARRAY_CHAR (may_not_optimize, REGNO (SET_DEST (set))))
4357 rtx src_reg;
4358 rtx add_val;
4359 rtx mult_val;
4360 int benefit;
4361 rtx regnote = 0;
4362 rtx last_consec_insn;
4364 dest_reg = SET_DEST (set);
4365 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
4366 continue;
4368 if (/* SET_SRC is a giv. */
4369 (general_induction_var (SET_SRC (set), &src_reg, &add_val,
4370 &mult_val, 0, &benefit)
4371 /* Equivalent expression is a giv. */
4372 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
4373 && general_induction_var (XEXP (regnote, 0), &src_reg,
4374 &add_val, &mult_val, 0,
4375 &benefit)))
4376 /* Don't try to handle any regs made by loop optimization.
4377 We have nothing on them in regno_first_uid, etc. */
4378 && REGNO (dest_reg) < max_reg_before_loop
4379 /* Don't recognize a BASIC_INDUCT_VAR here. */
4380 && dest_reg != src_reg
4381 /* This must be the only place where the register is set. */
4382 && (VARRAY_INT (n_times_set, REGNO (dest_reg)) == 1
4383 /* or all sets must be consecutive and make a giv. */
4384 || (benefit = consec_sets_giv (benefit, p,
4385 src_reg, dest_reg,
4386 &add_val, &mult_val,
4387 &last_consec_insn))))
4389 struct induction *v
4390 = (struct induction *) alloca (sizeof (struct induction));
4392 /* If this is a library call, increase benefit. */
4393 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
4394 benefit += libcall_benefit (p);
4396 /* Skip the consecutive insns, if there are any. */
4397 if (VARRAY_INT (n_times_set, REGNO (dest_reg)) != 1)
4398 p = last_consec_insn;
4400 record_giv (v, p, src_reg, dest_reg, mult_val, add_val, benefit,
4401 DEST_REG, not_every_iteration, NULL_PTR, loop_start,
4402 loop_end);
4407 #ifndef DONT_REDUCE_ADDR
4408 /* Look for givs which are memory addresses. */
4409 /* This resulted in worse code on a VAX 8600. I wonder if it
4410 still does. */
4411 if (GET_CODE (p) == INSN)
4412 find_mem_givs (PATTERN (p), p, not_every_iteration, loop_start,
4413 loop_end);
4414 #endif
4416 /* Update the status of whether giv can derive other givs. This can
4417 change when we pass a label or an insn that updates a biv. */
4418 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4419 || GET_CODE (p) == CODE_LABEL)
4420 update_giv_derive (p);
4422 /* Past a jump, we get to insns for which we can't count
4423 on whether they will be executed during each iteration. */
4424 /* This code appears twice in strength_reduce. There is also similar
4425 code in scan_loop. */
4426 if (GET_CODE (p) == JUMP_INSN
4427 /* If we enter the loop in the middle, and scan around to the
4428 beginning, don't set not_every_iteration for that.
4429 This can be any kind of jump, since we want to know if insns
4430 will be executed if the loop is executed. */
4431 && ! (JUMP_LABEL (p) == loop_top
4432 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
4433 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
4435 rtx label = 0;
4437 /* If this is a jump outside the loop, then it also doesn't
4438 matter. Check to see if the target of this branch is on the
4439 loop_number_exits_labels list. */
4441 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
4442 label;
4443 label = LABEL_NEXTREF (label))
4444 if (XEXP (label, 0) == JUMP_LABEL (p))
4445 break;
4447 if (! label)
4448 not_every_iteration = 1;
4451 else if (GET_CODE (p) == NOTE)
4453 /* At the virtual top of a converted loop, insns are again known to
4454 be executed each iteration: logically, the loop begins here
4455 even though the exit code has been duplicated.
4457 Insns are also again known to be executed each iteration at
4458 the LOOP_CONT note. */
4459 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
4460 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
4461 && loop_depth == 0)
4462 not_every_iteration = 0;
4463 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
4464 loop_depth++;
4465 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
4466 loop_depth--;
4469 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4470 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4471 or not an insn is known to be executed each iteration of the
4472 loop, whether or not any iterations are known to occur.
4474 Therefore, if we have just passed a label and have no more labels
4475 between here and the test insn of the loop, we know these insns
4476 will be executed each iteration. */
4478 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
4479 && no_labels_between_p (p, loop_end)
4480 && loop_insn_first_p (p, loop_cont))
4481 not_every_iteration = 0;
4484 /* Try to calculate and save the number of loop iterations. This is
4485 set to zero if the actual number can not be calculated. This must
4486 be called after all giv's have been identified, since otherwise it may
4487 fail if the iteration variable is a giv. */
4489 loop_iterations (loop_start, loop_end, loop_info);
4491 /* Now for each giv for which we still don't know whether or not it is
4492 replaceable, check to see if it is replaceable because its final value
4493 can be calculated. This must be done after loop_iterations is called,
4494 so that final_giv_value will work correctly. */
4496 for (bl = loop_iv_list; bl; bl = bl->next)
4498 struct induction *v;
4500 for (v = bl->giv; v; v = v->next_iv)
4501 if (! v->replaceable && ! v->not_replaceable)
4502 check_final_value (v, loop_start, loop_end, loop_info->n_iterations);
4505 /* Try to prove that the loop counter variable (if any) is always
4506 nonnegative; if so, record that fact with a REG_NONNEG note
4507 so that "decrement and branch until zero" insn can be used. */
4508 check_dbra_loop (loop_end, insn_count, loop_start, loop_info);
4510 /* Create reg_map to hold substitutions for replaceable giv regs.
4511 Some givs might have been made from biv increments, so look at
4512 reg_iv_type for a suitable size. */
4513 reg_map_size = reg_iv_type->num_elements;
4514 reg_map = (rtx *) alloca (reg_map_size * sizeof (rtx));
4515 bzero ((char *) reg_map, reg_map_size * sizeof (rtx));
4517 /* Examine each iv class for feasibility of strength reduction/induction
4518 variable elimination. */
4520 for (bl = loop_iv_list; bl; bl = bl->next)
4522 struct induction *v;
4523 int benefit;
4524 int all_reduced;
4525 rtx final_value = 0;
4526 unsigned nregs;
4528 /* Test whether it will be possible to eliminate this biv
4529 provided all givs are reduced. This is possible if either
4530 the reg is not used outside the loop, or we can compute
4531 what its final value will be.
4533 For architectures with a decrement_and_branch_until_zero insn,
4534 don't do this if we put a REG_NONNEG note on the endtest for
4535 this biv. */
4537 /* Compare against bl->init_insn rather than loop_start.
4538 We aren't concerned with any uses of the biv between
4539 init_insn and loop_start since these won't be affected
4540 by the value of the biv elsewhere in the function, so
4541 long as init_insn doesn't use the biv itself.
4542 March 14, 1989 -- self@bayes.arc.nasa.gov */
4544 if ((uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
4545 && bl->init_insn
4546 && INSN_UID (bl->init_insn) < max_uid_for_loop
4547 && uid_luid[REGNO_FIRST_UID (bl->regno)] >= INSN_LUID (bl->init_insn)
4548 #ifdef HAVE_decrement_and_branch_until_zero
4549 && ! bl->nonneg
4550 #endif
4551 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
4552 || ((final_value = final_biv_value (bl, loop_start, loop_end,
4553 loop_info->n_iterations))
4554 #ifdef HAVE_decrement_and_branch_until_zero
4555 && ! bl->nonneg
4556 #endif
4558 bl->eliminable = maybe_eliminate_biv (bl, loop_start, end, 0,
4559 threshold, insn_count);
4560 else
4562 if (loop_dump_stream)
4564 fprintf (loop_dump_stream,
4565 "Cannot eliminate biv %d.\n",
4566 bl->regno);
4567 fprintf (loop_dump_stream,
4568 "First use: insn %d, last use: insn %d.\n",
4569 REGNO_FIRST_UID (bl->regno),
4570 REGNO_LAST_UID (bl->regno));
4574 /* Combine all giv's for this iv_class. */
4575 combine_givs (bl);
4577 /* This will be true at the end, if all givs which depend on this
4578 biv have been strength reduced.
4579 We can't (currently) eliminate the biv unless this is so. */
4580 all_reduced = 1;
4582 /* Check each giv in this class to see if we will benefit by reducing
4583 it. Skip giv's combined with others. */
4584 for (v = bl->giv; v; v = v->next_iv)
4586 struct induction *tv;
4588 if (v->ignore || v->same)
4589 continue;
4591 benefit = v->benefit;
4593 /* Reduce benefit if not replaceable, since we will insert
4594 a move-insn to replace the insn that calculates this giv.
4595 Don't do this unless the giv is a user variable, since it
4596 will often be marked non-replaceable because of the duplication
4597 of the exit code outside the loop. In such a case, the copies
4598 we insert are dead and will be deleted. So they don't have
4599 a cost. Similar situations exist. */
4600 /* ??? The new final_[bg]iv_value code does a much better job
4601 of finding replaceable giv's, and hence this code may no longer
4602 be necessary. */
4603 if (! v->replaceable && ! bl->eliminable
4604 && REG_USERVAR_P (v->dest_reg))
4605 benefit -= copy_cost;
4607 /* Decrease the benefit to count the add-insns that we will
4608 insert to increment the reduced reg for the giv. */
4609 benefit -= add_cost * bl->biv_count;
4611 /* Decide whether to strength-reduce this giv or to leave the code
4612 unchanged (recompute it from the biv each time it is used).
4613 This decision can be made independently for each giv. */
4615 #ifdef AUTO_INC_DEC
4616 /* Attempt to guess whether autoincrement will handle some of the
4617 new add insns; if so, increase BENEFIT (undo the subtraction of
4618 add_cost that was done above). */
4619 if (v->giv_type == DEST_ADDR
4620 && GET_CODE (v->mult_val) == CONST_INT)
4622 if (HAVE_POST_INCREMENT
4623 && INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4624 benefit += add_cost * bl->biv_count;
4625 else if (HAVE_PRE_INCREMENT
4626 && INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4627 benefit += add_cost * bl->biv_count;
4628 else if (HAVE_POST_DECREMENT
4629 && -INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4630 benefit += add_cost * bl->biv_count;
4631 else if (HAVE_PRE_DECREMENT
4632 && -INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4633 benefit += add_cost * bl->biv_count;
4635 #endif
4637 /* If an insn is not to be strength reduced, then set its ignore
4638 flag, and clear all_reduced. */
4640 /* A giv that depends on a reversed biv must be reduced if it is
4641 used after the loop exit, otherwise, it would have the wrong
4642 value after the loop exit. To make it simple, just reduce all
4643 of such giv's whether or not we know they are used after the loop
4644 exit. */
4646 if ( ! flag_reduce_all_givs && v->lifetime * threshold * benefit < insn_count
4647 && ! bl->reversed )
4649 if (loop_dump_stream)
4650 fprintf (loop_dump_stream,
4651 "giv of insn %d not worth while, %d vs %d.\n",
4652 INSN_UID (v->insn),
4653 v->lifetime * threshold * benefit, insn_count);
4654 v->ignore = 1;
4655 all_reduced = 0;
4657 else
4659 /* Check that we can increment the reduced giv without a
4660 multiply insn. If not, reject it. */
4662 for (tv = bl->biv; tv; tv = tv->next_iv)
4663 if (tv->mult_val == const1_rtx
4664 && ! product_cheap_p (tv->add_val, v->mult_val))
4666 if (loop_dump_stream)
4667 fprintf (loop_dump_stream,
4668 "giv of insn %d: would need a multiply.\n",
4669 INSN_UID (v->insn));
4670 v->ignore = 1;
4671 all_reduced = 0;
4672 break;
4677 /* Check for givs whose first use is their definition and whose
4678 last use is the definition of another giv. If so, it is likely
4679 dead and should not be used to derive another giv nor to
4680 eliminate a biv. */
4681 for (v = bl->giv; v; v = v->next_iv)
4683 if (v->ignore
4684 || (v->same && v->same->ignore))
4685 continue;
4687 if (v->last_use)
4689 struct induction *v1;
4691 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4692 if (v->last_use == v1->insn)
4693 v->maybe_dead = 1;
4695 else if (v->giv_type == DEST_REG
4696 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4698 struct induction *v1;
4700 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4701 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4702 v->maybe_dead = 1;
4706 /* Now that we know which givs will be reduced, try to rearrange the
4707 combinations to reduce register pressure.
4708 recombine_givs calls find_life_end, which needs reg_iv_type and
4709 reg_iv_info to be valid for all pseudos. We do the necessary
4710 reallocation here since it allows to check if there are still
4711 more bivs to process. */
4712 nregs = max_reg_num ();
4713 if (nregs > reg_iv_type->num_elements)
4715 /* If there are still more bivs to process, allocate some slack
4716 space so that we're not constantly reallocating these arrays. */
4717 if (bl->next)
4718 nregs += nregs / 4;
4719 /* Reallocate reg_iv_type and reg_iv_info. */
4720 VARRAY_GROW (reg_iv_type, nregs);
4721 VARRAY_GROW (reg_iv_info, nregs);
4723 recombine_givs (bl, loop_start, loop_end, unroll_p);
4725 /* Reduce each giv that we decided to reduce. */
4727 for (v = bl->giv; v; v = v->next_iv)
4729 struct induction *tv;
4730 if (! v->ignore && v->same == 0)
4732 int auto_inc_opt = 0;
4734 /* If the code for derived givs immediately below has already
4735 allocated a new_reg, we must keep it. */
4736 if (! v->new_reg)
4737 v->new_reg = gen_reg_rtx (v->mode);
4739 if (v->derived_from)
4741 struct induction *d = v->derived_from;
4743 /* In case d->dest_reg is not replaceable, we have
4744 to replace it in v->insn now. */
4745 if (! d->new_reg)
4746 d->new_reg = gen_reg_rtx (d->mode);
4747 PATTERN (v->insn)
4748 = replace_rtx (PATTERN (v->insn), d->dest_reg, d->new_reg);
4749 PATTERN (v->insn)
4750 = replace_rtx (PATTERN (v->insn), v->dest_reg, v->new_reg);
4751 if (bl->biv_count != 1)
4753 /* For each place where the biv is incremented, add an
4754 insn to set the new, reduced reg for the giv. */
4755 for (tv = bl->biv; tv; tv = tv->next_iv)
4757 /* We always emit reduced giv increments before the
4758 biv increment when bl->biv_count != 1. So by
4759 emitting the add insns for derived givs after the
4760 biv increment, they pick up the updated value of
4761 the reduced giv. */
4762 emit_insn_after (copy_rtx (PATTERN (v->insn)),
4763 tv->insn);
4767 continue;
4770 #ifdef AUTO_INC_DEC
4771 /* If the target has auto-increment addressing modes, and
4772 this is an address giv, then try to put the increment
4773 immediately after its use, so that flow can create an
4774 auto-increment addressing mode. */
4775 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4776 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4777 /* We don't handle reversed biv's because bl->biv->insn
4778 does not have a valid INSN_LUID. */
4779 && ! bl->reversed
4780 && v->always_executed && ! v->maybe_multiple
4781 && INSN_UID (v->insn) < max_uid_for_loop)
4783 /* If other giv's have been combined with this one, then
4784 this will work only if all uses of the other giv's occur
4785 before this giv's insn. This is difficult to check.
4787 We simplify this by looking for the common case where
4788 there is one DEST_REG giv, and this giv's insn is the
4789 last use of the dest_reg of that DEST_REG giv. If the
4790 increment occurs after the address giv, then we can
4791 perform the optimization. (Otherwise, the increment
4792 would have to go before other_giv, and we would not be
4793 able to combine it with the address giv to get an
4794 auto-inc address.) */
4795 if (v->combined_with)
4797 struct induction *other_giv = 0;
4799 for (tv = bl->giv; tv; tv = tv->next_iv)
4800 if (tv->same == v)
4802 if (other_giv)
4803 break;
4804 else
4805 other_giv = tv;
4807 if (! tv && other_giv
4808 && REGNO (other_giv->dest_reg) < max_reg_before_loop
4809 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4810 == INSN_UID (v->insn))
4811 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4812 auto_inc_opt = 1;
4814 /* Check for case where increment is before the address
4815 giv. Do this test in "loop order". */
4816 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4817 && (INSN_LUID (v->insn) < INSN_LUID (scan_start)
4818 || (INSN_LUID (bl->biv->insn)
4819 > INSN_LUID (scan_start))))
4820 || (INSN_LUID (v->insn) < INSN_LUID (scan_start)
4821 && (INSN_LUID (scan_start)
4822 < INSN_LUID (bl->biv->insn))))
4823 auto_inc_opt = -1;
4824 else
4825 auto_inc_opt = 1;
4827 #ifdef HAVE_cc0
4829 rtx prev;
4831 /* We can't put an insn immediately after one setting
4832 cc0, or immediately before one using cc0. */
4833 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4834 || (auto_inc_opt == -1
4835 && (prev = prev_nonnote_insn (v->insn)) != 0
4836 && GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4837 && sets_cc0_p (PATTERN (prev))))
4838 auto_inc_opt = 0;
4840 #endif
4842 if (auto_inc_opt)
4843 v->auto_inc_opt = 1;
4845 #endif
4847 /* For each place where the biv is incremented, add an insn
4848 to increment the new, reduced reg for the giv. */
4849 for (tv = bl->biv; tv; tv = tv->next_iv)
4851 rtx insert_before;
4853 if (! auto_inc_opt)
4854 insert_before = tv->insn;
4855 else if (auto_inc_opt == 1)
4856 insert_before = NEXT_INSN (v->insn);
4857 else
4858 insert_before = v->insn;
4860 if (tv->mult_val == const1_rtx)
4861 emit_iv_add_mult (tv->add_val, v->mult_val,
4862 v->new_reg, v->new_reg, insert_before);
4863 else /* tv->mult_val == const0_rtx */
4864 /* A multiply is acceptable here
4865 since this is presumed to be seldom executed. */
4866 emit_iv_add_mult (tv->add_val, v->mult_val,
4867 v->add_val, v->new_reg, insert_before);
4870 /* Add code at loop start to initialize giv's reduced reg. */
4872 emit_iv_add_mult (bl->initial_value, v->mult_val,
4873 v->add_val, v->new_reg, loop_start);
4877 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4878 as not reduced.
4880 For each giv register that can be reduced now: if replaceable,
4881 substitute reduced reg wherever the old giv occurs;
4882 else add new move insn "giv_reg = reduced_reg". */
4884 for (v = bl->giv; v; v = v->next_iv)
4886 if (v->same && v->same->ignore)
4887 v->ignore = 1;
4889 if (v->ignore)
4890 continue;
4892 /* Update expression if this was combined, in case other giv was
4893 replaced. */
4894 if (v->same)
4895 v->new_reg = replace_rtx (v->new_reg,
4896 v->same->dest_reg, v->same->new_reg);
4898 if (v->giv_type == DEST_ADDR)
4899 /* Store reduced reg as the address in the memref where we found
4900 this giv. */
4901 validate_change (v->insn, v->location, v->new_reg, 0);
4902 else if (v->replaceable)
4904 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4906 #if 0
4907 /* I can no longer duplicate the original problem. Perhaps
4908 this is unnecessary now? */
4910 /* Replaceable; it isn't strictly necessary to delete the old
4911 insn and emit a new one, because v->dest_reg is now dead.
4913 However, especially when unrolling loops, the special
4914 handling for (set REG0 REG1) in the second cse pass may
4915 make v->dest_reg live again. To avoid this problem, emit
4916 an insn to set the original giv reg from the reduced giv.
4917 We can not delete the original insn, since it may be part
4918 of a LIBCALL, and the code in flow that eliminates dead
4919 libcalls will fail if it is deleted. */
4920 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4921 v->insn);
4922 #endif
4924 else
4926 /* Not replaceable; emit an insn to set the original giv reg from
4927 the reduced giv, same as above. */
4928 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4929 v->insn);
4932 /* When a loop is reversed, givs which depend on the reversed
4933 biv, and which are live outside the loop, must be set to their
4934 correct final value. This insn is only needed if the giv is
4935 not replaceable. The correct final value is the same as the
4936 value that the giv starts the reversed loop with. */
4937 if (bl->reversed && ! v->replaceable)
4938 emit_iv_add_mult (bl->initial_value, v->mult_val,
4939 v->add_val, v->dest_reg, end_insert_before);
4940 else if (v->final_value)
4942 rtx insert_before;
4944 /* If the loop has multiple exits, emit the insn before the
4945 loop to ensure that it will always be executed no matter
4946 how the loop exits. Otherwise, emit the insn after the loop,
4947 since this is slightly more efficient. */
4948 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4949 insert_before = loop_start;
4950 else
4951 insert_before = end_insert_before;
4952 emit_insn_before (gen_move_insn (v->dest_reg, v->final_value),
4953 insert_before);
4955 #if 0
4956 /* If the insn to set the final value of the giv was emitted
4957 before the loop, then we must delete the insn inside the loop
4958 that sets it. If this is a LIBCALL, then we must delete
4959 every insn in the libcall. Note, however, that
4960 final_giv_value will only succeed when there are multiple
4961 exits if the giv is dead at each exit, hence it does not
4962 matter that the original insn remains because it is dead
4963 anyways. */
4964 /* Delete the insn inside the loop that sets the giv since
4965 the giv is now set before (or after) the loop. */
4966 delete_insn (v->insn);
4967 #endif
4970 if (loop_dump_stream)
4972 fprintf (loop_dump_stream, "giv at %d reduced to ",
4973 INSN_UID (v->insn));
4974 print_rtl (loop_dump_stream, v->new_reg);
4975 fprintf (loop_dump_stream, "\n");
4979 /* All the givs based on the biv bl have been reduced if they
4980 merit it. */
4982 /* For each giv not marked as maybe dead that has been combined with a
4983 second giv, clear any "maybe dead" mark on that second giv.
4984 v->new_reg will either be or refer to the register of the giv it
4985 combined with.
4987 Doing this clearing avoids problems in biv elimination where a
4988 giv's new_reg is a complex value that can't be put in the insn but
4989 the giv combined with (with a reg as new_reg) is marked maybe_dead.
4990 Since the register will be used in either case, we'd prefer it be
4991 used from the simpler giv. */
4993 for (v = bl->giv; v; v = v->next_iv)
4994 if (! v->maybe_dead && v->same)
4995 v->same->maybe_dead = 0;
4997 /* Try to eliminate the biv, if it is a candidate.
4998 This won't work if ! all_reduced,
4999 since the givs we planned to use might not have been reduced.
5001 We have to be careful that we didn't initially think we could eliminate
5002 this biv because of a giv that we now think may be dead and shouldn't
5003 be used as a biv replacement.
5005 Also, there is the possibility that we may have a giv that looks
5006 like it can be used to eliminate a biv, but the resulting insn
5007 isn't valid. This can happen, for example, on the 88k, where a
5008 JUMP_INSN can compare a register only with zero. Attempts to
5009 replace it with a compare with a constant will fail.
5011 Note that in cases where this call fails, we may have replaced some
5012 of the occurrences of the biv with a giv, but no harm was done in
5013 doing so in the rare cases where it can occur. */
5015 if (all_reduced == 1 && bl->eliminable
5016 && maybe_eliminate_biv (bl, loop_start, end, 1,
5017 threshold, insn_count))
5020 /* ?? If we created a new test to bypass the loop entirely,
5021 or otherwise drop straight in, based on this test, then
5022 we might want to rewrite it also. This way some later
5023 pass has more hope of removing the initialization of this
5024 biv entirely. */
5026 /* If final_value != 0, then the biv may be used after loop end
5027 and we must emit an insn to set it just in case.
5029 Reversed bivs already have an insn after the loop setting their
5030 value, so we don't need another one. We can't calculate the
5031 proper final value for such a biv here anyways. */
5032 if (final_value != 0 && ! bl->reversed)
5034 rtx insert_before;
5036 /* If the loop has multiple exits, emit the insn before the
5037 loop to ensure that it will always be executed no matter
5038 how the loop exits. Otherwise, emit the insn after the
5039 loop, since this is slightly more efficient. */
5040 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
5041 insert_before = loop_start;
5042 else
5043 insert_before = end_insert_before;
5045 emit_insn_before (gen_move_insn (bl->biv->dest_reg, final_value),
5046 end_insert_before);
5049 #if 0
5050 /* Delete all of the instructions inside the loop which set
5051 the biv, as they are all dead. If is safe to delete them,
5052 because an insn setting a biv will never be part of a libcall. */
5053 /* However, deleting them will invalidate the regno_last_uid info,
5054 so keeping them around is more convenient. Final_biv_value
5055 will only succeed when there are multiple exits if the biv
5056 is dead at each exit, hence it does not matter that the original
5057 insn remains, because it is dead anyways. */
5058 for (v = bl->biv; v; v = v->next_iv)
5059 delete_insn (v->insn);
5060 #endif
5062 if (loop_dump_stream)
5063 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
5064 bl->regno);
5068 /* Go through all the instructions in the loop, making all the
5069 register substitutions scheduled in REG_MAP. */
5071 for (p = loop_start; p != end; p = NEXT_INSN (p))
5072 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5073 || GET_CODE (p) == CALL_INSN)
5075 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
5076 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
5077 INSN_CODE (p) = -1;
5080 /* Unroll loops from within strength reduction so that we can use the
5081 induction variable information that strength_reduce has already
5082 collected. */
5084 if (unroll_p)
5085 unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
5086 loop_info, 1);
5088 #ifdef HAVE_decrement_and_branch_on_count
5089 /* Instrument the loop with BCT insn. */
5090 if (HAVE_decrement_and_branch_on_count && bct_p
5091 && flag_branch_on_count_reg)
5092 insert_bct (loop_start, loop_end, loop_info);
5093 #endif /* HAVE_decrement_and_branch_on_count */
5095 if (loop_dump_stream)
5096 fprintf (loop_dump_stream, "\n");
5097 VARRAY_FREE (reg_iv_type);
5098 VARRAY_FREE (reg_iv_info);
5101 /* Return 1 if X is a valid source for an initial value (or as value being
5102 compared against in an initial test).
5104 X must be either a register or constant and must not be clobbered between
5105 the current insn and the start of the loop.
5107 INSN is the insn containing X. */
5109 static int
5110 valid_initial_value_p (x, insn, call_seen, loop_start)
5111 rtx x;
5112 rtx insn;
5113 int call_seen;
5114 rtx loop_start;
5116 if (CONSTANT_P (x))
5117 return 1;
5119 /* Only consider pseudos we know about initialized in insns whose luids
5120 we know. */
5121 if (GET_CODE (x) != REG
5122 || REGNO (x) >= max_reg_before_loop)
5123 return 0;
5125 /* Don't use call-clobbered registers across a call which clobbers it. On
5126 some machines, don't use any hard registers at all. */
5127 if (REGNO (x) < FIRST_PSEUDO_REGISTER
5128 && (SMALL_REGISTER_CLASSES
5129 || (call_used_regs[REGNO (x)] && call_seen)))
5130 return 0;
5132 /* Don't use registers that have been clobbered before the start of the
5133 loop. */
5134 if (reg_set_between_p (x, insn, loop_start))
5135 return 0;
5137 return 1;
5140 /* Scan X for memory refs and check each memory address
5141 as a possible giv. INSN is the insn whose pattern X comes from.
5142 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5143 every loop iteration. */
5145 static void
5146 find_mem_givs (x, insn, not_every_iteration, loop_start, loop_end)
5147 rtx x;
5148 rtx insn;
5149 int not_every_iteration;
5150 rtx loop_start, loop_end;
5152 register int i, j;
5153 register enum rtx_code code;
5154 register char *fmt;
5156 if (x == 0)
5157 return;
5159 code = GET_CODE (x);
5160 switch (code)
5162 case REG:
5163 case CONST_INT:
5164 case CONST:
5165 case CONST_DOUBLE:
5166 case SYMBOL_REF:
5167 case LABEL_REF:
5168 case PC:
5169 case CC0:
5170 case ADDR_VEC:
5171 case ADDR_DIFF_VEC:
5172 case USE:
5173 case CLOBBER:
5174 return;
5176 case MEM:
5178 rtx src_reg;
5179 rtx add_val;
5180 rtx mult_val;
5181 int benefit;
5183 /* This code used to disable creating GIVs with mult_val == 1 and
5184 add_val == 0. However, this leads to lost optimizations when
5185 it comes time to combine a set of related DEST_ADDR GIVs, since
5186 this one would not be seen. */
5188 if (general_induction_var (XEXP (x, 0), &src_reg, &add_val,
5189 &mult_val, 1, &benefit))
5191 /* Found one; record it. */
5192 struct induction *v
5193 = (struct induction *) oballoc (sizeof (struct induction));
5195 record_giv (v, insn, src_reg, addr_placeholder, mult_val,
5196 add_val, benefit, DEST_ADDR, not_every_iteration,
5197 &XEXP (x, 0), loop_start, loop_end);
5199 v->mem_mode = GET_MODE (x);
5202 return;
5204 default:
5205 break;
5208 /* Recursively scan the subexpressions for other mem refs. */
5210 fmt = GET_RTX_FORMAT (code);
5211 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5212 if (fmt[i] == 'e')
5213 find_mem_givs (XEXP (x, i), insn, not_every_iteration, loop_start,
5214 loop_end);
5215 else if (fmt[i] == 'E')
5216 for (j = 0; j < XVECLEN (x, i); j++)
5217 find_mem_givs (XVECEXP (x, i, j), insn, not_every_iteration,
5218 loop_start, loop_end);
5221 /* Fill in the data about one biv update.
5222 V is the `struct induction' in which we record the biv. (It is
5223 allocated by the caller, with alloca.)
5224 INSN is the insn that sets it.
5225 DEST_REG is the biv's reg.
5227 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5228 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
5229 being set to INC_VAL.
5231 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5232 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5233 can be executed more than once per iteration. If MAYBE_MULTIPLE
5234 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5235 executed exactly once per iteration. */
5237 static void
5238 record_biv (v, insn, dest_reg, inc_val, mult_val, location,
5239 not_every_iteration, maybe_multiple)
5240 struct induction *v;
5241 rtx insn;
5242 rtx dest_reg;
5243 rtx inc_val;
5244 rtx mult_val;
5245 rtx *location;
5246 int not_every_iteration;
5247 int maybe_multiple;
5249 struct iv_class *bl;
5251 v->insn = insn;
5252 v->src_reg = dest_reg;
5253 v->dest_reg = dest_reg;
5254 v->mult_val = mult_val;
5255 v->add_val = inc_val;
5256 v->location = location;
5257 v->mode = GET_MODE (dest_reg);
5258 v->always_computable = ! not_every_iteration;
5259 v->always_executed = ! not_every_iteration;
5260 v->maybe_multiple = maybe_multiple;
5262 /* Add this to the reg's iv_class, creating a class
5263 if this is the first incrementation of the reg. */
5265 bl = reg_biv_class[REGNO (dest_reg)];
5266 if (bl == 0)
5268 /* Create and initialize new iv_class. */
5270 bl = (struct iv_class *) oballoc (sizeof (struct iv_class));
5272 bl->regno = REGNO (dest_reg);
5273 bl->biv = 0;
5274 bl->giv = 0;
5275 bl->biv_count = 0;
5276 bl->giv_count = 0;
5278 /* Set initial value to the reg itself. */
5279 bl->initial_value = dest_reg;
5280 /* We haven't seen the initializing insn yet */
5281 bl->init_insn = 0;
5282 bl->init_set = 0;
5283 bl->initial_test = 0;
5284 bl->incremented = 0;
5285 bl->eliminable = 0;
5286 bl->nonneg = 0;
5287 bl->reversed = 0;
5288 bl->total_benefit = 0;
5290 /* Add this class to loop_iv_list. */
5291 bl->next = loop_iv_list;
5292 loop_iv_list = bl;
5294 /* Put it in the array of biv register classes. */
5295 reg_biv_class[REGNO (dest_reg)] = bl;
5298 /* Update IV_CLASS entry for this biv. */
5299 v->next_iv = bl->biv;
5300 bl->biv = v;
5301 bl->biv_count++;
5302 if (mult_val == const1_rtx)
5303 bl->incremented = 1;
5305 if (loop_dump_stream)
5307 fprintf (loop_dump_stream,
5308 "Insn %d: possible biv, reg %d,",
5309 INSN_UID (insn), REGNO (dest_reg));
5310 if (GET_CODE (inc_val) == CONST_INT)
5312 fprintf (loop_dump_stream, " const =");
5313 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (inc_val));
5314 fputc ('\n', loop_dump_stream);
5316 else
5318 fprintf (loop_dump_stream, " const = ");
5319 print_rtl (loop_dump_stream, inc_val);
5320 fprintf (loop_dump_stream, "\n");
5325 /* Fill in the data about one giv.
5326 V is the `struct induction' in which we record the giv. (It is
5327 allocated by the caller, with alloca.)
5328 INSN is the insn that sets it.
5329 BENEFIT estimates the savings from deleting this insn.
5330 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5331 into a register or is used as a memory address.
5333 SRC_REG is the biv reg which the giv is computed from.
5334 DEST_REG is the giv's reg (if the giv is stored in a reg).
5335 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5336 LOCATION points to the place where this giv's value appears in INSN. */
5338 static void
5339 record_giv (v, insn, src_reg, dest_reg, mult_val, add_val, benefit,
5340 type, not_every_iteration, location, loop_start, loop_end)
5341 struct induction *v;
5342 rtx insn;
5343 rtx src_reg;
5344 rtx dest_reg;
5345 rtx mult_val, add_val;
5346 int benefit;
5347 enum g_types type;
5348 int not_every_iteration;
5349 rtx *location;
5350 rtx loop_start, loop_end;
5352 struct induction *b;
5353 struct iv_class *bl;
5354 rtx set = single_set (insn);
5356 v->insn = insn;
5357 v->src_reg = src_reg;
5358 v->giv_type = type;
5359 v->dest_reg = dest_reg;
5360 v->mult_val = mult_val;
5361 v->add_val = add_val;
5362 v->benefit = benefit;
5363 v->location = location;
5364 v->cant_derive = 0;
5365 v->combined_with = 0;
5366 v->maybe_multiple = 0;
5367 v->maybe_dead = 0;
5368 v->derive_adjustment = 0;
5369 v->same = 0;
5370 v->ignore = 0;
5371 v->new_reg = 0;
5372 v->final_value = 0;
5373 v->same_insn = 0;
5374 v->auto_inc_opt = 0;
5375 v->unrolled = 0;
5376 v->shared = 0;
5377 v->derived_from = 0;
5378 v->last_use = 0;
5380 /* The v->always_computable field is used in update_giv_derive, to
5381 determine whether a giv can be used to derive another giv. For a
5382 DEST_REG giv, INSN computes a new value for the giv, so its value
5383 isn't computable if INSN insn't executed every iteration.
5384 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5385 it does not compute a new value. Hence the value is always computable
5386 regardless of whether INSN is executed each iteration. */
5388 if (type == DEST_ADDR)
5389 v->always_computable = 1;
5390 else
5391 v->always_computable = ! not_every_iteration;
5393 v->always_executed = ! not_every_iteration;
5395 if (type == DEST_ADDR)
5397 v->mode = GET_MODE (*location);
5398 v->lifetime = 1;
5400 else /* type == DEST_REG */
5402 v->mode = GET_MODE (SET_DEST (set));
5404 v->lifetime = (uid_luid[REGNO_LAST_UID (REGNO (dest_reg))]
5405 - uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))]);
5407 /* If the lifetime is zero, it means that this register is
5408 really a dead store. So mark this as a giv that can be
5409 ignored. This will not prevent the biv from being eliminated. */
5410 if (v->lifetime == 0)
5411 v->ignore = 1;
5413 REG_IV_TYPE (REGNO (dest_reg)) = GENERAL_INDUCT;
5414 REG_IV_INFO (REGNO (dest_reg)) = v;
5417 /* Add the giv to the class of givs computed from one biv. */
5419 bl = reg_biv_class[REGNO (src_reg)];
5420 if (bl)
5422 v->next_iv = bl->giv;
5423 bl->giv = v;
5424 /* Don't count DEST_ADDR. This is supposed to count the number of
5425 insns that calculate givs. */
5426 if (type == DEST_REG)
5427 bl->giv_count++;
5428 bl->total_benefit += benefit;
5430 else
5431 /* Fatal error, biv missing for this giv? */
5432 abort ();
5434 if (type == DEST_ADDR)
5435 v->replaceable = 1;
5436 else
5438 /* The giv can be replaced outright by the reduced register only if all
5439 of the following conditions are true:
5440 - the insn that sets the giv is always executed on any iteration
5441 on which the giv is used at all
5442 (there are two ways to deduce this:
5443 either the insn is executed on every iteration,
5444 or all uses follow that insn in the same basic block),
5445 - the giv is not used outside the loop
5446 - no assignments to the biv occur during the giv's lifetime. */
5448 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
5449 /* Previous line always fails if INSN was moved by loop opt. */
5450 && uid_luid[REGNO_LAST_UID (REGNO (dest_reg))] < INSN_LUID (loop_end)
5451 && (! not_every_iteration
5452 || last_use_this_basic_block (dest_reg, insn)))
5454 /* Now check that there are no assignments to the biv within the
5455 giv's lifetime. This requires two separate checks. */
5457 /* Check each biv update, and fail if any are between the first
5458 and last use of the giv.
5460 If this loop contains an inner loop that was unrolled, then
5461 the insn modifying the biv may have been emitted by the loop
5462 unrolling code, and hence does not have a valid luid. Just
5463 mark the biv as not replaceable in this case. It is not very
5464 useful as a biv, because it is used in two different loops.
5465 It is very unlikely that we would be able to optimize the giv
5466 using this biv anyways. */
5468 v->replaceable = 1;
5469 for (b = bl->biv; b; b = b->next_iv)
5471 if (INSN_UID (b->insn) >= max_uid_for_loop
5472 || ((uid_luid[INSN_UID (b->insn)]
5473 >= uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))])
5474 && (uid_luid[INSN_UID (b->insn)]
5475 <= uid_luid[REGNO_LAST_UID (REGNO (dest_reg))])))
5477 v->replaceable = 0;
5478 v->not_replaceable = 1;
5479 break;
5483 /* If there are any backwards branches that go from after the
5484 biv update to before it, then this giv is not replaceable. */
5485 if (v->replaceable)
5486 for (b = bl->biv; b; b = b->next_iv)
5487 if (back_branch_in_range_p (b->insn, loop_start, loop_end))
5489 v->replaceable = 0;
5490 v->not_replaceable = 1;
5491 break;
5494 else
5496 /* May still be replaceable, we don't have enough info here to
5497 decide. */
5498 v->replaceable = 0;
5499 v->not_replaceable = 0;
5503 /* Record whether the add_val contains a const_int, for later use by
5504 combine_givs. */
5506 rtx tem = add_val;
5508 v->no_const_addval = 1;
5509 if (tem == const0_rtx)
5511 else if (GET_CODE (tem) == CONST_INT)
5512 v->no_const_addval = 0;
5513 else if (GET_CODE (tem) == PLUS)
5515 while (1)
5517 if (GET_CODE (XEXP (tem, 0)) == PLUS)
5518 tem = XEXP (tem, 0);
5519 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
5520 tem = XEXP (tem, 1);
5521 else
5522 break;
5524 if (GET_CODE (XEXP (tem, 1)) == CONST_INT)
5525 v->no_const_addval = 0;
5529 if (loop_dump_stream)
5531 if (type == DEST_REG)
5532 fprintf (loop_dump_stream, "Insn %d: giv reg %d",
5533 INSN_UID (insn), REGNO (dest_reg));
5534 else
5535 fprintf (loop_dump_stream, "Insn %d: dest address",
5536 INSN_UID (insn));
5538 fprintf (loop_dump_stream, " src reg %d benefit %d",
5539 REGNO (src_reg), v->benefit);
5540 fprintf (loop_dump_stream, " lifetime %d",
5541 v->lifetime);
5543 if (v->replaceable)
5544 fprintf (loop_dump_stream, " replaceable");
5546 if (v->no_const_addval)
5547 fprintf (loop_dump_stream, " ncav");
5549 if (GET_CODE (mult_val) == CONST_INT)
5551 fprintf (loop_dump_stream, " mult ");
5552 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (mult_val));
5554 else
5556 fprintf (loop_dump_stream, " mult ");
5557 print_rtl (loop_dump_stream, mult_val);
5560 if (GET_CODE (add_val) == CONST_INT)
5562 fprintf (loop_dump_stream, " add ");
5563 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (add_val));
5565 else
5567 fprintf (loop_dump_stream, " add ");
5568 print_rtl (loop_dump_stream, add_val);
5572 if (loop_dump_stream)
5573 fprintf (loop_dump_stream, "\n");
5578 /* All this does is determine whether a giv can be made replaceable because
5579 its final value can be calculated. This code can not be part of record_giv
5580 above, because final_giv_value requires that the number of loop iterations
5581 be known, and that can not be accurately calculated until after all givs
5582 have been identified. */
5584 static void
5585 check_final_value (v, loop_start, loop_end, n_iterations)
5586 struct induction *v;
5587 rtx loop_start, loop_end;
5588 unsigned HOST_WIDE_INT n_iterations;
5590 struct iv_class *bl;
5591 rtx final_value = 0;
5593 bl = reg_biv_class[REGNO (v->src_reg)];
5595 /* DEST_ADDR givs will never reach here, because they are always marked
5596 replaceable above in record_giv. */
5598 /* The giv can be replaced outright by the reduced register only if all
5599 of the following conditions are true:
5600 - the insn that sets the giv is always executed on any iteration
5601 on which the giv is used at all
5602 (there are two ways to deduce this:
5603 either the insn is executed on every iteration,
5604 or all uses follow that insn in the same basic block),
5605 - its final value can be calculated (this condition is different
5606 than the one above in record_giv)
5607 - no assignments to the biv occur during the giv's lifetime. */
5609 #if 0
5610 /* This is only called now when replaceable is known to be false. */
5611 /* Clear replaceable, so that it won't confuse final_giv_value. */
5612 v->replaceable = 0;
5613 #endif
5615 if ((final_value = final_giv_value (v, loop_start, loop_end, n_iterations))
5616 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
5618 int biv_increment_seen = 0;
5619 rtx p = v->insn;
5620 rtx last_giv_use;
5622 v->replaceable = 1;
5624 /* When trying to determine whether or not a biv increment occurs
5625 during the lifetime of the giv, we can ignore uses of the variable
5626 outside the loop because final_value is true. Hence we can not
5627 use regno_last_uid and regno_first_uid as above in record_giv. */
5629 /* Search the loop to determine whether any assignments to the
5630 biv occur during the giv's lifetime. Start with the insn
5631 that sets the giv, and search around the loop until we come
5632 back to that insn again.
5634 Also fail if there is a jump within the giv's lifetime that jumps
5635 to somewhere outside the lifetime but still within the loop. This
5636 catches spaghetti code where the execution order is not linear, and
5637 hence the above test fails. Here we assume that the giv lifetime
5638 does not extend from one iteration of the loop to the next, so as
5639 to make the test easier. Since the lifetime isn't known yet,
5640 this requires two loops. See also record_giv above. */
5642 last_giv_use = v->insn;
5644 while (1)
5646 p = NEXT_INSN (p);
5647 if (p == loop_end)
5648 p = NEXT_INSN (loop_start);
5649 if (p == v->insn)
5650 break;
5652 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5653 || GET_CODE (p) == CALL_INSN)
5655 if (biv_increment_seen)
5657 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
5659 v->replaceable = 0;
5660 v->not_replaceable = 1;
5661 break;
5664 else if (reg_set_p (v->src_reg, PATTERN (p)))
5665 biv_increment_seen = 1;
5666 else if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
5667 last_giv_use = p;
5671 /* Now that the lifetime of the giv is known, check for branches
5672 from within the lifetime to outside the lifetime if it is still
5673 replaceable. */
5675 if (v->replaceable)
5677 p = v->insn;
5678 while (1)
5680 p = NEXT_INSN (p);
5681 if (p == loop_end)
5682 p = NEXT_INSN (loop_start);
5683 if (p == last_giv_use)
5684 break;
5686 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
5687 && LABEL_NAME (JUMP_LABEL (p))
5688 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
5689 && loop_insn_first_p (loop_start, JUMP_LABEL (p)))
5690 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
5691 && loop_insn_first_p (JUMP_LABEL (p), loop_end))))
5693 v->replaceable = 0;
5694 v->not_replaceable = 1;
5696 if (loop_dump_stream)
5697 fprintf (loop_dump_stream,
5698 "Found branch outside giv lifetime.\n");
5700 break;
5705 /* If it is replaceable, then save the final value. */
5706 if (v->replaceable)
5707 v->final_value = final_value;
5710 if (loop_dump_stream && v->replaceable)
5711 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
5712 INSN_UID (v->insn), REGNO (v->dest_reg));
5715 /* Update the status of whether a giv can derive other givs.
5717 We need to do something special if there is or may be an update to the biv
5718 between the time the giv is defined and the time it is used to derive
5719 another giv.
5721 In addition, a giv that is only conditionally set is not allowed to
5722 derive another giv once a label has been passed.
5724 The cases we look at are when a label or an update to a biv is passed. */
5726 static void
5727 update_giv_derive (p)
5728 rtx p;
5730 struct iv_class *bl;
5731 struct induction *biv, *giv;
5732 rtx tem;
5733 int dummy;
5735 /* Search all IV classes, then all bivs, and finally all givs.
5737 There are three cases we are concerned with. First we have the situation
5738 of a giv that is only updated conditionally. In that case, it may not
5739 derive any givs after a label is passed.
5741 The second case is when a biv update occurs, or may occur, after the
5742 definition of a giv. For certain biv updates (see below) that are
5743 known to occur between the giv definition and use, we can adjust the
5744 giv definition. For others, or when the biv update is conditional,
5745 we must prevent the giv from deriving any other givs. There are two
5746 sub-cases within this case.
5748 If this is a label, we are concerned with any biv update that is done
5749 conditionally, since it may be done after the giv is defined followed by
5750 a branch here (actually, we need to pass both a jump and a label, but
5751 this extra tracking doesn't seem worth it).
5753 If this is a jump, we are concerned about any biv update that may be
5754 executed multiple times. We are actually only concerned about
5755 backward jumps, but it is probably not worth performing the test
5756 on the jump again here.
5758 If this is a biv update, we must adjust the giv status to show that a
5759 subsequent biv update was performed. If this adjustment cannot be done,
5760 the giv cannot derive further givs. */
5762 for (bl = loop_iv_list; bl; bl = bl->next)
5763 for (biv = bl->biv; biv; biv = biv->next_iv)
5764 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
5765 || biv->insn == p)
5767 for (giv = bl->giv; giv; giv = giv->next_iv)
5769 /* If cant_derive is already true, there is no point in
5770 checking all of these conditions again. */
5771 if (giv->cant_derive)
5772 continue;
5774 /* If this giv is conditionally set and we have passed a label,
5775 it cannot derive anything. */
5776 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
5777 giv->cant_derive = 1;
5779 /* Skip givs that have mult_val == 0, since
5780 they are really invariants. Also skip those that are
5781 replaceable, since we know their lifetime doesn't contain
5782 any biv update. */
5783 else if (giv->mult_val == const0_rtx || giv->replaceable)
5784 continue;
5786 /* The only way we can allow this giv to derive another
5787 is if this is a biv increment and we can form the product
5788 of biv->add_val and giv->mult_val. In this case, we will
5789 be able to compute a compensation. */
5790 else if (biv->insn == p)
5792 tem = 0;
5794 if (biv->mult_val == const1_rtx)
5795 tem = simplify_giv_expr (gen_rtx_MULT (giv->mode,
5796 biv->add_val,
5797 giv->mult_val),
5798 &dummy);
5800 if (tem && giv->derive_adjustment)
5801 tem = simplify_giv_expr (gen_rtx_PLUS (giv->mode, tem,
5802 giv->derive_adjustment),
5803 &dummy);
5804 if (tem)
5805 giv->derive_adjustment = tem;
5806 else
5807 giv->cant_derive = 1;
5809 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5810 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
5811 giv->cant_derive = 1;
5816 /* Check whether an insn is an increment legitimate for a basic induction var.
5817 X is the source of insn P, or a part of it.
5818 MODE is the mode in which X should be interpreted.
5820 DEST_REG is the putative biv, also the destination of the insn.
5821 We accept patterns of these forms:
5822 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5823 REG = INVARIANT + REG
5825 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5826 store the additive term into *INC_VAL, and store the place where
5827 we found the additive term into *LOCATION.
5829 If X is an assignment of an invariant into DEST_REG, we set
5830 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5832 We also want to detect a BIV when it corresponds to a variable
5833 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5834 of the variable may be a PLUS that adds a SUBREG of that variable to
5835 an invariant and then sign- or zero-extends the result of the PLUS
5836 into the variable.
5838 Most GIVs in such cases will be in the promoted mode, since that is the
5839 probably the natural computation mode (and almost certainly the mode
5840 used for addresses) on the machine. So we view the pseudo-reg containing
5841 the variable as the BIV, as if it were simply incremented.
5843 Note that treating the entire pseudo as a BIV will result in making
5844 simple increments to any GIVs based on it. However, if the variable
5845 overflows in its declared mode but not its promoted mode, the result will
5846 be incorrect. This is acceptable if the variable is signed, since
5847 overflows in such cases are undefined, but not if it is unsigned, since
5848 those overflows are defined. So we only check for SIGN_EXTEND and
5849 not ZERO_EXTEND.
5851 If we cannot find a biv, we return 0. */
5853 static int
5854 basic_induction_var (x, mode, dest_reg, p, inc_val, mult_val, location)
5855 register rtx x;
5856 enum machine_mode mode;
5857 rtx p;
5858 rtx dest_reg;
5859 rtx *inc_val;
5860 rtx *mult_val;
5861 rtx **location;
5863 register enum rtx_code code;
5864 rtx *argp, arg;
5865 rtx insn, set = 0;
5867 code = GET_CODE (x);
5868 switch (code)
5870 case PLUS:
5871 if (rtx_equal_p (XEXP (x, 0), dest_reg)
5872 || (GET_CODE (XEXP (x, 0)) == SUBREG
5873 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5874 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
5876 argp = &XEXP (x, 1);
5878 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
5879 || (GET_CODE (XEXP (x, 1)) == SUBREG
5880 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5881 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
5883 argp = &XEXP (x, 0);
5885 else
5886 return 0;
5888 arg = *argp;
5889 if (invariant_p (arg) != 1)
5890 return 0;
5892 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
5893 *mult_val = const1_rtx;
5894 *location = argp;
5895 return 1;
5897 case SUBREG:
5898 /* If this is a SUBREG for a promoted variable, check the inner
5899 value. */
5900 if (SUBREG_PROMOTED_VAR_P (x))
5901 return basic_induction_var (SUBREG_REG (x), GET_MODE (SUBREG_REG (x)),
5902 dest_reg, p, inc_val, mult_val, location);
5903 return 0;
5905 case REG:
5906 /* If this register is assigned in a previous insn, look at its
5907 source, but don't go outside the loop or past a label. */
5909 insn = p;
5910 while (1)
5912 do {
5913 insn = PREV_INSN (insn);
5914 } while (insn && GET_CODE (insn) == NOTE
5915 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5917 if (!insn)
5918 break;
5919 set = single_set (insn);
5920 if (set == 0)
5921 break;
5923 if ((SET_DEST (set) == x
5924 || (GET_CODE (SET_DEST (set)) == SUBREG
5925 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
5926 <= UNITS_PER_WORD)
5927 && SUBREG_REG (SET_DEST (set)) == x))
5928 && basic_induction_var (SET_SRC (set),
5929 (GET_MODE (SET_SRC (set)) == VOIDmode
5930 ? GET_MODE (x)
5931 : GET_MODE (SET_SRC (set))),
5932 dest_reg, insn,
5933 inc_val, mult_val, location))
5934 return 1;
5936 /* ... fall through ... */
5938 /* Can accept constant setting of biv only when inside inner most loop.
5939 Otherwise, a biv of an inner loop may be incorrectly recognized
5940 as a biv of the outer loop,
5941 causing code to be moved INTO the inner loop. */
5942 case MEM:
5943 if (invariant_p (x) != 1)
5944 return 0;
5945 case CONST_INT:
5946 case SYMBOL_REF:
5947 case CONST:
5948 /* convert_modes aborts if we try to convert to or from CCmode, so just
5949 exclude that case. It is very unlikely that a condition code value
5950 would be a useful iterator anyways. */
5951 if (loops_enclosed == 1
5952 && GET_MODE_CLASS (mode) != MODE_CC
5953 && GET_MODE_CLASS (GET_MODE (dest_reg)) != MODE_CC)
5955 /* Possible bug here? Perhaps we don't know the mode of X. */
5956 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
5957 *mult_val = const0_rtx;
5958 return 1;
5960 else
5961 return 0;
5963 case SIGN_EXTEND:
5964 return basic_induction_var (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5965 dest_reg, p, inc_val, mult_val, location);
5967 case ASHIFTRT:
5968 /* Similar, since this can be a sign extension. */
5969 for (insn = PREV_INSN (p);
5970 (insn && GET_CODE (insn) == NOTE
5971 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5972 insn = PREV_INSN (insn))
5975 if (insn)
5976 set = single_set (insn);
5978 if (set && SET_DEST (set) == XEXP (x, 0)
5979 && GET_CODE (XEXP (x, 1)) == CONST_INT
5980 && INTVAL (XEXP (x, 1)) >= 0
5981 && GET_CODE (SET_SRC (set)) == ASHIFT
5982 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
5983 return basic_induction_var (XEXP (SET_SRC (set), 0),
5984 GET_MODE (XEXP (x, 0)),
5985 dest_reg, insn, inc_val, mult_val,
5986 location);
5987 return 0;
5989 default:
5990 return 0;
5994 /* A general induction variable (giv) is any quantity that is a linear
5995 function of a basic induction variable,
5996 i.e. giv = biv * mult_val + add_val.
5997 The coefficients can be any loop invariant quantity.
5998 A giv need not be computed directly from the biv;
5999 it can be computed by way of other givs. */
6001 /* Determine whether X computes a giv.
6002 If it does, return a nonzero value
6003 which is the benefit from eliminating the computation of X;
6004 set *SRC_REG to the register of the biv that it is computed from;
6005 set *ADD_VAL and *MULT_VAL to the coefficients,
6006 such that the value of X is biv * mult + add; */
6008 static int
6009 general_induction_var (x, src_reg, add_val, mult_val, is_addr, pbenefit)
6010 rtx x;
6011 rtx *src_reg;
6012 rtx *add_val;
6013 rtx *mult_val;
6014 int is_addr;
6015 int *pbenefit;
6017 rtx orig_x = x;
6018 char *storage;
6020 /* If this is an invariant, forget it, it isn't a giv. */
6021 if (invariant_p (x) == 1)
6022 return 0;
6024 /* See if the expression could be a giv and get its form.
6025 Mark our place on the obstack in case we don't find a giv. */
6026 storage = (char *) oballoc (0);
6027 *pbenefit = 0;
6028 x = simplify_giv_expr (x, pbenefit);
6029 if (x == 0)
6031 obfree (storage);
6032 return 0;
6035 switch (GET_CODE (x))
6037 case USE:
6038 case CONST_INT:
6039 /* Since this is now an invariant and wasn't before, it must be a giv
6040 with MULT_VAL == 0. It doesn't matter which BIV we associate this
6041 with. */
6042 *src_reg = loop_iv_list->biv->dest_reg;
6043 *mult_val = const0_rtx;
6044 *add_val = x;
6045 break;
6047 case REG:
6048 /* This is equivalent to a BIV. */
6049 *src_reg = x;
6050 *mult_val = const1_rtx;
6051 *add_val = const0_rtx;
6052 break;
6054 case PLUS:
6055 /* Either (plus (biv) (invar)) or
6056 (plus (mult (biv) (invar_1)) (invar_2)). */
6057 if (GET_CODE (XEXP (x, 0)) == MULT)
6059 *src_reg = XEXP (XEXP (x, 0), 0);
6060 *mult_val = XEXP (XEXP (x, 0), 1);
6062 else
6064 *src_reg = XEXP (x, 0);
6065 *mult_val = const1_rtx;
6067 *add_val = XEXP (x, 1);
6068 break;
6070 case MULT:
6071 /* ADD_VAL is zero. */
6072 *src_reg = XEXP (x, 0);
6073 *mult_val = XEXP (x, 1);
6074 *add_val = const0_rtx;
6075 break;
6077 default:
6078 abort ();
6081 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6082 unless they are CONST_INT). */
6083 if (GET_CODE (*add_val) == USE)
6084 *add_val = XEXP (*add_val, 0);
6085 if (GET_CODE (*mult_val) == USE)
6086 *mult_val = XEXP (*mult_val, 0);
6088 if (is_addr)
6090 #ifdef ADDRESS_COST
6091 *pbenefit += ADDRESS_COST (orig_x) - reg_address_cost;
6092 #else
6093 *pbenefit += rtx_cost (orig_x, MEM) - reg_address_cost;
6094 #endif
6096 else
6097 *pbenefit += rtx_cost (orig_x, SET);
6099 /* Always return true if this is a giv so it will be detected as such,
6100 even if the benefit is zero or negative. This allows elimination
6101 of bivs that might otherwise not be eliminated. */
6102 return 1;
6105 /* Given an expression, X, try to form it as a linear function of a biv.
6106 We will canonicalize it to be of the form
6107 (plus (mult (BIV) (invar_1))
6108 (invar_2))
6109 with possible degeneracies.
6111 The invariant expressions must each be of a form that can be used as a
6112 machine operand. We surround then with a USE rtx (a hack, but localized
6113 and certainly unambiguous!) if not a CONST_INT for simplicity in this
6114 routine; it is the caller's responsibility to strip them.
6116 If no such canonicalization is possible (i.e., two biv's are used or an
6117 expression that is neither invariant nor a biv or giv), this routine
6118 returns 0.
6120 For a non-zero return, the result will have a code of CONST_INT, USE,
6121 REG (for a BIV), PLUS, or MULT. No other codes will occur.
6123 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
6125 static rtx sge_plus PROTO ((enum machine_mode, rtx, rtx));
6126 static rtx sge_plus_constant PROTO ((rtx, rtx));
6128 static rtx
6129 simplify_giv_expr (x, benefit)
6130 rtx x;
6131 int *benefit;
6133 enum machine_mode mode = GET_MODE (x);
6134 rtx arg0, arg1;
6135 rtx tem;
6137 /* If this is not an integer mode, or if we cannot do arithmetic in this
6138 mode, this can't be a giv. */
6139 if (mode != VOIDmode
6140 && (GET_MODE_CLASS (mode) != MODE_INT
6141 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
6142 return NULL_RTX;
6144 switch (GET_CODE (x))
6146 case PLUS:
6147 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
6148 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
6149 if (arg0 == 0 || arg1 == 0)
6150 return NULL_RTX;
6152 /* Put constant last, CONST_INT last if both constant. */
6153 if ((GET_CODE (arg0) == USE
6154 || GET_CODE (arg0) == CONST_INT)
6155 && ! ((GET_CODE (arg0) == USE
6156 && GET_CODE (arg1) == USE)
6157 || GET_CODE (arg1) == CONST_INT))
6158 tem = arg0, arg0 = arg1, arg1 = tem;
6160 /* Handle addition of zero, then addition of an invariant. */
6161 if (arg1 == const0_rtx)
6162 return arg0;
6163 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
6164 switch (GET_CODE (arg0))
6166 case CONST_INT:
6167 case USE:
6168 /* Adding two invariants must result in an invariant, so enclose
6169 addition operation inside a USE and return it. */
6170 if (GET_CODE (arg0) == USE)
6171 arg0 = XEXP (arg0, 0);
6172 if (GET_CODE (arg1) == USE)
6173 arg1 = XEXP (arg1, 0);
6175 if (GET_CODE (arg0) == CONST_INT)
6176 tem = arg0, arg0 = arg1, arg1 = tem;
6177 if (GET_CODE (arg1) == CONST_INT)
6178 tem = sge_plus_constant (arg0, arg1);
6179 else
6180 tem = sge_plus (mode, arg0, arg1);
6182 if (GET_CODE (tem) != CONST_INT)
6183 tem = gen_rtx_USE (mode, tem);
6184 return tem;
6186 case REG:
6187 case MULT:
6188 /* biv + invar or mult + invar. Return sum. */
6189 return gen_rtx_PLUS (mode, arg0, arg1);
6191 case PLUS:
6192 /* (a + invar_1) + invar_2. Associate. */
6193 return simplify_giv_expr (
6194 gen_rtx_PLUS (mode, XEXP (arg0, 0),
6195 gen_rtx_PLUS (mode, XEXP (arg0, 1), arg1)),
6196 benefit);
6198 default:
6199 abort ();
6202 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
6203 MULT to reduce cases. */
6204 if (GET_CODE (arg0) == REG)
6205 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
6206 if (GET_CODE (arg1) == REG)
6207 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
6209 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6210 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6211 Recurse to associate the second PLUS. */
6212 if (GET_CODE (arg1) == MULT)
6213 tem = arg0, arg0 = arg1, arg1 = tem;
6215 if (GET_CODE (arg1) == PLUS)
6216 return simplify_giv_expr (gen_rtx_PLUS (mode,
6217 gen_rtx_PLUS (mode, arg0,
6218 XEXP (arg1, 0)),
6219 XEXP (arg1, 1)),
6220 benefit);
6222 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
6223 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
6224 return NULL_RTX;
6226 if (!rtx_equal_p (arg0, arg1))
6227 return NULL_RTX;
6229 return simplify_giv_expr (gen_rtx_MULT (mode,
6230 XEXP (arg0, 0),
6231 gen_rtx_PLUS (mode,
6232 XEXP (arg0, 1),
6233 XEXP (arg1, 1))),
6234 benefit);
6236 case MINUS:
6237 /* Handle "a - b" as "a + b * (-1)". */
6238 return simplify_giv_expr (gen_rtx_PLUS (mode,
6239 XEXP (x, 0),
6240 gen_rtx_MULT (mode, XEXP (x, 1),
6241 constm1_rtx)),
6242 benefit);
6244 case MULT:
6245 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
6246 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
6247 if (arg0 == 0 || arg1 == 0)
6248 return NULL_RTX;
6250 /* Put constant last, CONST_INT last if both constant. */
6251 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
6252 && GET_CODE (arg1) != CONST_INT)
6253 tem = arg0, arg0 = arg1, arg1 = tem;
6255 /* If second argument is not now constant, not giv. */
6256 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
6257 return NULL_RTX;
6259 /* Handle multiply by 0 or 1. */
6260 if (arg1 == const0_rtx)
6261 return const0_rtx;
6263 else if (arg1 == const1_rtx)
6264 return arg0;
6266 switch (GET_CODE (arg0))
6268 case REG:
6269 /* biv * invar. Done. */
6270 return gen_rtx_MULT (mode, arg0, arg1);
6272 case CONST_INT:
6273 /* Product of two constants. */
6274 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
6276 case USE:
6277 /* invar * invar. It is a giv, but very few of these will
6278 actually pay off, so limit to simple registers. */
6279 if (GET_CODE (arg1) != CONST_INT)
6280 return NULL_RTX;
6282 arg0 = XEXP (arg0, 0);
6283 if (GET_CODE (arg0) == REG)
6284 tem = gen_rtx_MULT (mode, arg0, arg1);
6285 else if (GET_CODE (arg0) == MULT
6286 && GET_CODE (XEXP (arg0, 0)) == REG
6287 && GET_CODE (XEXP (arg0, 1)) == CONST_INT)
6289 tem = gen_rtx_MULT (mode, XEXP (arg0, 0),
6290 GEN_INT (INTVAL (XEXP (arg0, 1))
6291 * INTVAL (arg1)));
6293 else
6294 return NULL_RTX;
6295 return gen_rtx_USE (mode, tem);
6297 case MULT:
6298 /* (a * invar_1) * invar_2. Associate. */
6299 return simplify_giv_expr (gen_rtx_MULT (mode, XEXP (arg0, 0),
6300 gen_rtx_MULT (mode,
6301 XEXP (arg0, 1),
6302 arg1)),
6303 benefit);
6305 case PLUS:
6306 /* (a + invar_1) * invar_2. Distribute. */
6307 return simplify_giv_expr (gen_rtx_PLUS (mode,
6308 gen_rtx_MULT (mode,
6309 XEXP (arg0, 0),
6310 arg1),
6311 gen_rtx_MULT (mode,
6312 XEXP (arg0, 1),
6313 arg1)),
6314 benefit);
6316 default:
6317 abort ();
6320 case ASHIFT:
6321 /* Shift by constant is multiply by power of two. */
6322 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6323 return 0;
6325 return simplify_giv_expr (gen_rtx_MULT (mode,
6326 XEXP (x, 0),
6327 GEN_INT ((HOST_WIDE_INT) 1
6328 << INTVAL (XEXP (x, 1)))),
6329 benefit);
6331 case NEG:
6332 /* "-a" is "a * (-1)" */
6333 return simplify_giv_expr (gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
6334 benefit);
6336 case NOT:
6337 /* "~a" is "-a - 1". Silly, but easy. */
6338 return simplify_giv_expr (gen_rtx_MINUS (mode,
6339 gen_rtx_NEG (mode, XEXP (x, 0)),
6340 const1_rtx),
6341 benefit);
6343 case USE:
6344 /* Already in proper form for invariant. */
6345 return x;
6347 case REG:
6348 /* If this is a new register, we can't deal with it. */
6349 if (REGNO (x) >= max_reg_before_loop)
6350 return 0;
6352 /* Check for biv or giv. */
6353 switch (REG_IV_TYPE (REGNO (x)))
6355 case BASIC_INDUCT:
6356 return x;
6357 case GENERAL_INDUCT:
6359 struct induction *v = REG_IV_INFO (REGNO (x));
6361 /* Form expression from giv and add benefit. Ensure this giv
6362 can derive another and subtract any needed adjustment if so. */
6363 *benefit += v->benefit;
6364 if (v->cant_derive)
6365 return 0;
6367 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode, v->src_reg,
6368 v->mult_val),
6369 v->add_val);
6370 if (v->derive_adjustment)
6371 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
6372 return simplify_giv_expr (tem, benefit);
6375 default:
6376 /* If it isn't an induction variable, and it is invariant, we
6377 may be able to simplify things further by looking through
6378 the bits we just moved outside the loop. */
6379 if (invariant_p (x) == 1)
6381 struct movable *m;
6383 for (m = the_movables; m ; m = m->next)
6384 if (rtx_equal_p (x, m->set_dest))
6386 /* Ok, we found a match. Substitute and simplify. */
6388 /* If we match another movable, we must use that, as
6389 this one is going away. */
6390 if (m->match)
6391 return simplify_giv_expr (m->match->set_dest, benefit);
6393 /* If consec is non-zero, this is a member of a group of
6394 instructions that were moved together. We handle this
6395 case only to the point of seeking to the last insn and
6396 looking for a REG_EQUAL. Fail if we don't find one. */
6397 if (m->consec != 0)
6399 int i = m->consec;
6400 tem = m->insn;
6401 do { tem = NEXT_INSN (tem); } while (--i > 0);
6403 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
6404 if (tem)
6405 tem = XEXP (tem, 0);
6407 else
6409 tem = single_set (m->insn);
6410 if (tem)
6411 tem = SET_SRC (tem);
6414 if (tem)
6416 /* What we are most interested in is pointer
6417 arithmetic on invariants -- only take
6418 patterns we may be able to do something with. */
6419 if (GET_CODE (tem) == PLUS
6420 || GET_CODE (tem) == MULT
6421 || GET_CODE (tem) == ASHIFT
6422 || GET_CODE (tem) == CONST_INT
6423 || GET_CODE (tem) == SYMBOL_REF)
6425 tem = simplify_giv_expr (tem, benefit);
6426 if (tem)
6427 return tem;
6429 else if (GET_CODE (tem) == CONST
6430 && GET_CODE (XEXP (tem, 0)) == PLUS
6431 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
6432 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
6434 tem = simplify_giv_expr (XEXP (tem, 0), benefit);
6435 if (tem)
6436 return tem;
6439 break;
6442 break;
6445 /* Fall through to general case. */
6446 default:
6447 /* If invariant, return as USE (unless CONST_INT).
6448 Otherwise, not giv. */
6449 if (GET_CODE (x) == USE)
6450 x = XEXP (x, 0);
6452 if (invariant_p (x) == 1)
6454 if (GET_CODE (x) == CONST_INT)
6455 return x;
6456 if (GET_CODE (x) == CONST
6457 && GET_CODE (XEXP (x, 0)) == PLUS
6458 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6459 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
6460 x = XEXP (x, 0);
6461 return gen_rtx_USE (mode, x);
6463 else
6464 return 0;
6468 /* This routine folds invariants such that there is only ever one
6469 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6471 static rtx
6472 sge_plus_constant (x, c)
6473 rtx x, c;
6475 if (GET_CODE (x) == CONST_INT)
6476 return GEN_INT (INTVAL (x) + INTVAL (c));
6477 else if (GET_CODE (x) != PLUS)
6478 return gen_rtx_PLUS (GET_MODE (x), x, c);
6479 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6481 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6482 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
6484 else if (GET_CODE (XEXP (x, 0)) == PLUS
6485 || GET_CODE (XEXP (x, 1)) != PLUS)
6487 return gen_rtx_PLUS (GET_MODE (x),
6488 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
6490 else
6492 return gen_rtx_PLUS (GET_MODE (x),
6493 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
6497 static rtx
6498 sge_plus (mode, x, y)
6499 enum machine_mode mode;
6500 rtx x, y;
6502 while (GET_CODE (y) == PLUS)
6504 rtx a = XEXP (y, 0);
6505 if (GET_CODE (a) == CONST_INT)
6506 x = sge_plus_constant (x, a);
6507 else
6508 x = gen_rtx_PLUS (mode, x, a);
6509 y = XEXP (y, 1);
6511 if (GET_CODE (y) == CONST_INT)
6512 x = sge_plus_constant (x, y);
6513 else
6514 x = gen_rtx_PLUS (mode, x, y);
6515 return x;
6518 /* Help detect a giv that is calculated by several consecutive insns;
6519 for example,
6520 giv = biv * M
6521 giv = giv + A
6522 The caller has already identified the first insn P as having a giv as dest;
6523 we check that all other insns that set the same register follow
6524 immediately after P, that they alter nothing else,
6525 and that the result of the last is still a giv.
6527 The value is 0 if the reg set in P is not really a giv.
6528 Otherwise, the value is the amount gained by eliminating
6529 all the consecutive insns that compute the value.
6531 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
6532 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
6534 The coefficients of the ultimate giv value are stored in
6535 *MULT_VAL and *ADD_VAL. */
6537 static int
6538 consec_sets_giv (first_benefit, p, src_reg, dest_reg,
6539 add_val, mult_val, last_consec_insn)
6540 int first_benefit;
6541 rtx p;
6542 rtx src_reg;
6543 rtx dest_reg;
6544 rtx *add_val;
6545 rtx *mult_val;
6546 rtx *last_consec_insn;
6548 int count;
6549 enum rtx_code code;
6550 int benefit;
6551 rtx temp;
6552 rtx set;
6554 /* Indicate that this is a giv so that we can update the value produced in
6555 each insn of the multi-insn sequence.
6557 This induction structure will be used only by the call to
6558 general_induction_var below, so we can allocate it on our stack.
6559 If this is a giv, our caller will replace the induct var entry with
6560 a new induction structure. */
6561 struct induction *v
6562 = (struct induction *) alloca (sizeof (struct induction));
6563 v->src_reg = src_reg;
6564 v->mult_val = *mult_val;
6565 v->add_val = *add_val;
6566 v->benefit = first_benefit;
6567 v->cant_derive = 0;
6568 v->derive_adjustment = 0;
6570 REG_IV_TYPE (REGNO (dest_reg)) = GENERAL_INDUCT;
6571 REG_IV_INFO (REGNO (dest_reg)) = v;
6573 count = VARRAY_INT (n_times_set, REGNO (dest_reg)) - 1;
6575 while (count > 0)
6577 p = NEXT_INSN (p);
6578 code = GET_CODE (p);
6580 /* If libcall, skip to end of call sequence. */
6581 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
6582 p = XEXP (temp, 0);
6584 if (code == INSN
6585 && (set = single_set (p))
6586 && GET_CODE (SET_DEST (set)) == REG
6587 && SET_DEST (set) == dest_reg
6588 && (general_induction_var (SET_SRC (set), &src_reg,
6589 add_val, mult_val, 0, &benefit)
6590 /* Giv created by equivalent expression. */
6591 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
6592 && general_induction_var (XEXP (temp, 0), &src_reg,
6593 add_val, mult_val, 0, &benefit)))
6594 && src_reg == v->src_reg)
6596 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
6597 benefit += libcall_benefit (p);
6599 count--;
6600 v->mult_val = *mult_val;
6601 v->add_val = *add_val;
6602 v->benefit = benefit;
6604 else if (code != NOTE)
6606 /* Allow insns that set something other than this giv to a
6607 constant. Such insns are needed on machines which cannot
6608 include long constants and should not disqualify a giv. */
6609 if (code == INSN
6610 && (set = single_set (p))
6611 && SET_DEST (set) != dest_reg
6612 && CONSTANT_P (SET_SRC (set)))
6613 continue;
6615 REG_IV_TYPE (REGNO (dest_reg)) = UNKNOWN_INDUCT;
6616 return 0;
6620 *last_consec_insn = p;
6621 return v->benefit;
6624 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6625 represented by G1. If no such expression can be found, or it is clear that
6626 it cannot possibly be a valid address, 0 is returned.
6628 To perform the computation, we note that
6629 G1 = x * v + a and
6630 G2 = y * v + b
6631 where `v' is the biv.
6633 So G2 = (y/b) * G1 + (b - a*y/x).
6635 Note that MULT = y/x.
6637 Update: A and B are now allowed to be additive expressions such that
6638 B contains all variables in A. That is, computing B-A will not require
6639 subtracting variables. */
6641 static rtx
6642 express_from_1 (a, b, mult)
6643 rtx a, b, mult;
6645 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
6647 if (mult == const0_rtx)
6648 return b;
6650 /* If MULT is not 1, we cannot handle A with non-constants, since we
6651 would then be required to subtract multiples of the registers in A.
6652 This is theoretically possible, and may even apply to some Fortran
6653 constructs, but it is a lot of work and we do not attempt it here. */
6655 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
6656 return NULL_RTX;
6658 /* In general these structures are sorted top to bottom (down the PLUS
6659 chain), but not left to right across the PLUS. If B is a higher
6660 order giv than A, we can strip one level and recurse. If A is higher
6661 order, we'll eventually bail out, but won't know that until the end.
6662 If they are the same, we'll strip one level around this loop. */
6664 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
6666 rtx ra, rb, oa, ob, tmp;
6668 ra = XEXP (a, 0), oa = XEXP (a, 1);
6669 if (GET_CODE (ra) == PLUS)
6670 tmp = ra, ra = oa, oa = tmp;
6672 rb = XEXP (b, 0), ob = XEXP (b, 1);
6673 if (GET_CODE (rb) == PLUS)
6674 tmp = rb, rb = ob, ob = tmp;
6676 if (rtx_equal_p (ra, rb))
6677 /* We matched: remove one reg completely. */
6678 a = oa, b = ob;
6679 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
6680 /* An alternate match. */
6681 a = oa, b = rb;
6682 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
6683 /* An alternate match. */
6684 a = ra, b = ob;
6685 else
6687 /* Indicates an extra register in B. Strip one level from B and
6688 recurse, hoping B was the higher order expression. */
6689 ob = express_from_1 (a, ob, mult);
6690 if (ob == NULL_RTX)
6691 return NULL_RTX;
6692 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
6696 /* Here we are at the last level of A, go through the cases hoping to
6697 get rid of everything but a constant. */
6699 if (GET_CODE (a) == PLUS)
6701 rtx ra, oa;
6703 ra = XEXP (a, 0), oa = XEXP (a, 1);
6704 if (rtx_equal_p (oa, b))
6705 oa = ra;
6706 else if (!rtx_equal_p (ra, b))
6707 return NULL_RTX;
6709 if (GET_CODE (oa) != CONST_INT)
6710 return NULL_RTX;
6712 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
6714 else if (GET_CODE (a) == CONST_INT)
6716 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
6718 else if (GET_CODE (b) == PLUS)
6720 if (rtx_equal_p (a, XEXP (b, 0)))
6721 return XEXP (b, 1);
6722 else if (rtx_equal_p (a, XEXP (b, 1)))
6723 return XEXP (b, 0);
6724 else
6725 return NULL_RTX;
6727 else if (rtx_equal_p (a, b))
6728 return const0_rtx;
6730 return NULL_RTX;
6734 express_from (g1, g2)
6735 struct induction *g1, *g2;
6737 rtx mult, add;
6739 /* The value that G1 will be multiplied by must be a constant integer. Also,
6740 the only chance we have of getting a valid address is if b*c/a (see above
6741 for notation) is also an integer. */
6742 if (GET_CODE (g1->mult_val) == CONST_INT
6743 && GET_CODE (g2->mult_val) == CONST_INT)
6745 if (g1->mult_val == const0_rtx
6746 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
6747 return NULL_RTX;
6748 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
6750 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
6751 mult = const1_rtx;
6752 else
6754 /* ??? Find out if the one is a multiple of the other? */
6755 return NULL_RTX;
6758 add = express_from_1 (g1->add_val, g2->add_val, mult);
6759 if (add == NULL_RTX)
6760 return NULL_RTX;
6762 /* Form simplified final result. */
6763 if (mult == const0_rtx)
6764 return add;
6765 else if (mult == const1_rtx)
6766 mult = g1->dest_reg;
6767 else
6768 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
6770 if (add == const0_rtx)
6771 return mult;
6772 else
6774 if (GET_CODE (add) == PLUS
6775 && CONSTANT_P (XEXP (add, 1)))
6777 rtx tem = XEXP (add, 1);
6778 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
6779 add = tem;
6782 return gen_rtx_PLUS (g2->mode, mult, add);
6787 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6788 represented by G1. This indicates that G2 should be combined with G1 and
6789 that G2 can use (either directly or via an address expression) a register
6790 used to represent G1. */
6792 static rtx
6793 combine_givs_p (g1, g2)
6794 struct induction *g1, *g2;
6796 rtx tem = express_from (g1, g2);
6798 /* If these givs are identical, they can be combined. We use the results
6799 of express_from because the addends are not in a canonical form, so
6800 rtx_equal_p is a weaker test. */
6801 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
6802 combination to be the other way round. */
6803 if (tem == g1->dest_reg
6804 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
6806 return g1->dest_reg;
6809 /* If G2 can be expressed as a function of G1 and that function is valid
6810 as an address and no more expensive than using a register for G2,
6811 the expression of G2 in terms of G1 can be used. */
6812 if (tem != NULL_RTX
6813 && g2->giv_type == DEST_ADDR
6814 && memory_address_p (g2->mem_mode, tem)
6815 /* ??? Looses, especially with -fforce-addr, where *g2->location
6816 will always be a register, and so anything more complicated
6817 gets discarded. */
6818 #if 0
6819 #ifdef ADDRESS_COST
6820 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location)
6821 #else
6822 && rtx_cost (tem, MEM) <= rtx_cost (*g2->location, MEM)
6823 #endif
6824 #endif
6827 return tem;
6830 return NULL_RTX;
6833 struct combine_givs_stats
6835 int giv_number;
6836 int total_benefit;
6839 static int
6840 cmp_combine_givs_stats (x, y)
6841 struct combine_givs_stats *x, *y;
6843 int d;
6844 d = y->total_benefit - x->total_benefit;
6845 /* Stabilize the sort. */
6846 if (!d)
6847 d = x->giv_number - y->giv_number;
6848 return d;
6851 /* Check all pairs of givs for iv_class BL and see if any can be combined with
6852 any other. If so, point SAME to the giv combined with and set NEW_REG to
6853 be an expression (in terms of the other giv's DEST_REG) equivalent to the
6854 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
6856 static void
6857 combine_givs (bl)
6858 struct iv_class *bl;
6860 /* Additional benefit to add for being combined multiple times. */
6861 const int extra_benefit = 3;
6863 struct induction *g1, *g2, **giv_array;
6864 int i, j, k, giv_count;
6865 struct combine_givs_stats *stats;
6866 rtx *can_combine;
6868 /* Count givs, because bl->giv_count is incorrect here. */
6869 giv_count = 0;
6870 for (g1 = bl->giv; g1; g1 = g1->next_iv)
6871 if (!g1->ignore)
6872 giv_count++;
6874 giv_array
6875 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
6876 i = 0;
6877 for (g1 = bl->giv; g1; g1 = g1->next_iv)
6878 if (!g1->ignore)
6879 giv_array[i++] = g1;
6881 stats = (struct combine_givs_stats *) alloca (giv_count * sizeof (*stats));
6882 bzero ((char *) stats, giv_count * sizeof (*stats));
6884 can_combine = (rtx *) alloca (giv_count * giv_count * sizeof(rtx));
6885 bzero ((char *) can_combine, giv_count * giv_count * sizeof(rtx));
6887 for (i = 0; i < giv_count; i++)
6889 int this_benefit;
6890 rtx single_use;
6892 g1 = giv_array[i];
6893 stats[i].giv_number = i;
6895 /* If a DEST_REG GIV is used only once, do not allow it to combine
6896 with anything, for in doing so we will gain nothing that cannot
6897 be had by simply letting the GIV with which we would have combined
6898 to be reduced on its own. The losage shows up in particular with
6899 DEST_ADDR targets on hosts with reg+reg addressing, though it can
6900 be seen elsewhere as well. */
6901 if (g1->giv_type == DEST_REG
6902 && (single_use = VARRAY_RTX (reg_single_usage, REGNO (g1->dest_reg)))
6903 && single_use != const0_rtx)
6904 continue;
6906 this_benefit = g1->benefit;
6907 /* Add an additional weight for zero addends. */
6908 if (g1->no_const_addval)
6909 this_benefit += 1;
6911 for (j = 0; j < giv_count; j++)
6913 rtx this_combine;
6915 g2 = giv_array[j];
6916 if (g1 != g2
6917 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
6919 can_combine[i*giv_count + j] = this_combine;
6920 this_benefit += g2->benefit + extra_benefit;
6923 stats[i].total_benefit = this_benefit;
6926 /* Iterate, combining until we can't. */
6927 restart:
6928 qsort (stats, giv_count, sizeof(*stats), cmp_combine_givs_stats);
6930 if (loop_dump_stream)
6932 fprintf (loop_dump_stream, "Sorted combine statistics:\n");
6933 for (k = 0; k < giv_count; k++)
6935 g1 = giv_array[stats[k].giv_number];
6936 if (!g1->combined_with && !g1->same)
6937 fprintf (loop_dump_stream, " {%d, %d}",
6938 INSN_UID (giv_array[stats[k].giv_number]->insn),
6939 stats[k].total_benefit);
6941 putc ('\n', loop_dump_stream);
6944 for (k = 0; k < giv_count; k++)
6946 int g1_add_benefit = 0;
6948 i = stats[k].giv_number;
6949 g1 = giv_array[i];
6951 /* If it has already been combined, skip. */
6952 if (g1->combined_with || g1->same)
6953 continue;
6955 for (j = 0; j < giv_count; j++)
6957 g2 = giv_array[j];
6958 if (g1 != g2 && can_combine[i*giv_count + j]
6959 /* If it has already been combined, skip. */
6960 && ! g2->same && ! g2->combined_with)
6962 int l;
6964 g2->new_reg = can_combine[i*giv_count + j];
6965 g2->same = g1;
6966 g1->combined_with++;
6967 g1->lifetime += g2->lifetime;
6969 g1_add_benefit += g2->benefit;
6971 /* ??? The new final_[bg]iv_value code does a much better job
6972 of finding replaceable giv's, and hence this code may no
6973 longer be necessary. */
6974 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
6975 g1_add_benefit -= copy_cost;
6977 /* To help optimize the next set of combinations, remove
6978 this giv from the benefits of other potential mates. */
6979 for (l = 0; l < giv_count; ++l)
6981 int m = stats[l].giv_number;
6982 if (can_combine[m*giv_count + j])
6983 stats[l].total_benefit -= g2->benefit + extra_benefit;
6986 if (loop_dump_stream)
6987 fprintf (loop_dump_stream,
6988 "giv at %d combined with giv at %d\n",
6989 INSN_UID (g2->insn), INSN_UID (g1->insn));
6993 /* To help optimize the next set of combinations, remove
6994 this giv from the benefits of other potential mates. */
6995 if (g1->combined_with)
6997 for (j = 0; j < giv_count; ++j)
6999 int m = stats[j].giv_number;
7000 if (can_combine[m*giv_count + i])
7001 stats[j].total_benefit -= g1->benefit + extra_benefit;
7004 g1->benefit += g1_add_benefit;
7006 /* We've finished with this giv, and everything it touched.
7007 Restart the combination so that proper weights for the
7008 rest of the givs are properly taken into account. */
7009 /* ??? Ideally we would compact the arrays at this point, so
7010 as to not cover old ground. But sanely compacting
7011 can_combine is tricky. */
7012 goto restart;
7017 struct recombine_givs_stats
7019 int giv_number;
7020 int start_luid, end_luid;
7023 /* Used below as comparison function for qsort. We want a ascending luid
7024 when scanning the array starting at the end, thus the arguments are
7025 used in reverse. */
7026 static int
7027 cmp_recombine_givs_stats (x, y)
7028 struct recombine_givs_stats *x, *y;
7030 int d;
7031 d = y->start_luid - x->start_luid;
7032 /* Stabilize the sort. */
7033 if (!d)
7034 d = y->giv_number - x->giv_number;
7035 return d;
7038 /* Scan X, which is a part of INSN, for the end of life of a giv. Also
7039 look for the start of life of a giv where the start has not been seen
7040 yet to unlock the search for the end of its life.
7041 Only consider givs that belong to BIV.
7042 Return the total number of lifetime ends that have been found. */
7043 static int
7044 find_life_end (x, stats, insn, biv)
7045 rtx x, insn, biv;
7046 struct recombine_givs_stats *stats;
7048 enum rtx_code code;
7049 char *fmt;
7050 int i, j;
7051 int retval;
7053 code = GET_CODE (x);
7054 switch (code)
7056 case SET:
7058 rtx reg = SET_DEST (x);
7059 if (GET_CODE (reg) == REG)
7061 int regno = REGNO (reg);
7062 struct induction *v = REG_IV_INFO (regno);
7064 if (REG_IV_TYPE (regno) == GENERAL_INDUCT
7065 && ! v->ignore
7066 && v->src_reg == biv
7067 && stats[v->ix].end_luid <= 0)
7069 /* If we see a 0 here for end_luid, it means that we have
7070 scanned the entire loop without finding any use at all.
7071 We must not predicate this code on a start_luid match
7072 since that would make the test fail for givs that have
7073 been hoisted out of inner loops. */
7074 if (stats[v->ix].end_luid == 0)
7076 stats[v->ix].end_luid = stats[v->ix].start_luid;
7077 return 1 + find_life_end (SET_SRC (x), stats, insn, biv);
7079 else if (stats[v->ix].start_luid == INSN_LUID (insn))
7080 stats[v->ix].end_luid = 0;
7082 return find_life_end (SET_SRC (x), stats, insn, biv);
7084 break;
7086 case REG:
7088 int regno = REGNO (x);
7089 struct induction *v = REG_IV_INFO (regno);
7091 if (REG_IV_TYPE (regno) == GENERAL_INDUCT
7092 && ! v->ignore
7093 && v->src_reg == biv
7094 && stats[v->ix].end_luid == 0)
7096 while (INSN_UID (insn) >= max_uid_for_loop)
7097 insn = NEXT_INSN (insn);
7098 stats[v->ix].end_luid = INSN_LUID (insn);
7099 return 1;
7101 return 0;
7103 case LABEL_REF:
7104 case CONST_DOUBLE:
7105 case CONST_INT:
7106 case CONST:
7107 return 0;
7108 default:
7109 break;
7111 fmt = GET_RTX_FORMAT (code);
7112 retval = 0;
7113 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7115 if (fmt[i] == 'e')
7116 retval += find_life_end (XEXP (x, i), stats, insn, biv);
7118 else if (fmt[i] == 'E')
7119 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7120 retval += find_life_end (XVECEXP (x, i, j), stats, insn, biv);
7122 return retval;
7125 /* For each giv that has been combined with another, look if
7126 we can combine it with the most recently used one instead.
7127 This tends to shorten giv lifetimes, and helps the next step:
7128 try to derive givs from other givs. */
7129 static void
7130 recombine_givs (bl, loop_start, loop_end, unroll_p)
7131 struct iv_class *bl;
7132 rtx loop_start, loop_end;
7133 int unroll_p;
7135 struct induction *v, **giv_array, *last_giv;
7136 struct recombine_givs_stats *stats;
7137 int giv_count;
7138 int i, rescan;
7139 int ends_need_computing;
7141 for (giv_count = 0, v = bl->giv; v; v = v->next_iv)
7143 if (! v->ignore)
7144 giv_count++;
7146 giv_array
7147 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
7148 stats = (struct recombine_givs_stats *) alloca (giv_count * sizeof *stats);
7150 /* Initialize stats and set up the ix field for each giv in stats to name
7151 the corresponding index into stats. */
7152 for (i = 0, v = bl->giv; v; v = v->next_iv)
7154 rtx p;
7156 if (v->ignore)
7157 continue;
7158 giv_array[i] = v;
7159 stats[i].giv_number = i;
7160 /* If this giv has been hoisted out of an inner loop, use the luid of
7161 the previous insn. */
7162 for (p = v->insn; INSN_UID (p) >= max_uid_for_loop; )
7163 p = PREV_INSN (p);
7164 stats[i].start_luid = INSN_LUID (p);
7165 v->ix = i;
7166 i++;
7169 qsort (stats, giv_count, sizeof(*stats), cmp_recombine_givs_stats);
7171 /* Do the actual most-recently-used recombination. */
7172 for (last_giv = 0, i = giv_count - 1; i >= 0; i--)
7174 v = giv_array[stats[i].giv_number];
7175 if (v->same)
7177 struct induction *old_same = v->same;
7178 rtx new_combine;
7180 /* combine_givs_p actually says if we can make this transformation.
7181 The other tests are here only to avoid keeping a giv alive
7182 that could otherwise be eliminated. */
7183 if (last_giv
7184 && ((old_same->maybe_dead && ! old_same->combined_with)
7185 || ! last_giv->maybe_dead
7186 || last_giv->combined_with)
7187 && (new_combine = combine_givs_p (last_giv, v)))
7189 old_same->combined_with--;
7190 v->new_reg = new_combine;
7191 v->same = last_giv;
7192 last_giv->combined_with++;
7193 /* No need to update lifetimes / benefits here since we have
7194 already decided what to reduce. */
7196 if (loop_dump_stream)
7198 fprintf (loop_dump_stream,
7199 "giv at %d recombined with giv at %d as ",
7200 INSN_UID (v->insn), INSN_UID (last_giv->insn));
7201 print_rtl (loop_dump_stream, v->new_reg);
7202 putc ('\n', loop_dump_stream);
7204 continue;
7206 v = v->same;
7208 else if (v->giv_type != DEST_REG)
7209 continue;
7210 if (! last_giv
7211 || (last_giv->maybe_dead && ! last_giv->combined_with)
7212 || ! v->maybe_dead
7213 || v->combined_with)
7214 last_giv = v;
7217 ends_need_computing = 0;
7218 /* For each DEST_REG giv, compute lifetime starts, and try to compute
7219 lifetime ends from regscan info. */
7220 for (i = 0, v = bl->giv; v; v = v->next_iv)
7222 if (v->ignore)
7223 continue;
7224 if (v->giv_type == DEST_ADDR)
7226 /* Loop unrolling of an inner loop can even create new DEST_REG
7227 givs. */
7228 rtx p;
7229 for (p = v->insn; INSN_UID (p) >= max_uid_for_loop; )
7230 p = PREV_INSN (p);
7231 stats[i].start_luid = stats[i].end_luid = INSN_LUID (p);
7232 if (p != v->insn)
7233 stats[i].end_luid++;
7235 else /* v->giv_type == DEST_REG */
7237 if (v->last_use)
7239 stats[i].start_luid = INSN_LUID (v->insn);
7240 stats[i].end_luid = INSN_LUID (v->last_use);
7242 else if (INSN_UID (v->insn) >= max_uid_for_loop)
7244 rtx p;
7245 /* This insn has been created by loop optimization on an inner
7246 loop. We don't have a proper start_luid that will match
7247 when we see the first set. But we do know that there will
7248 be no use before the set, so we can set end_luid to 0 so that
7249 we'll start looking for the last use right away. */
7250 for (p = PREV_INSN (v->insn); INSN_UID (p) >= max_uid_for_loop; )
7251 p = PREV_INSN (p);
7252 stats[i].start_luid = INSN_LUID (p);
7253 stats[i].end_luid = 0;
7254 ends_need_computing++;
7256 else
7258 int regno = REGNO (v->dest_reg);
7259 int count = VARRAY_INT (n_times_set, regno) - 1;
7260 rtx p = v->insn;
7262 /* Find the first insn that sets the giv, so that we can verify
7263 if this giv's lifetime wraps around the loop. We also need
7264 the luid of the first setting insn in order to detect the
7265 last use properly. */
7266 while (count)
7268 p = prev_nonnote_insn (p);
7269 if (reg_set_p (v->dest_reg, p))
7270 count--;
7273 stats[i].start_luid = INSN_LUID (p);
7274 if (stats[i].start_luid > uid_luid[REGNO_FIRST_UID (regno)])
7276 stats[i].end_luid = -1;
7277 ends_need_computing++;
7279 else
7281 stats[i].end_luid = uid_luid[REGNO_LAST_UID (regno)];
7282 if (stats[i].end_luid > INSN_LUID (loop_end))
7284 stats[i].end_luid = -1;
7285 ends_need_computing++;
7290 i++;
7293 /* If the regscan information was unconclusive for one or more DEST_REG
7294 givs, scan the all insn in the loop to find out lifetime ends. */
7295 if (ends_need_computing)
7297 rtx biv = bl->biv->src_reg;
7298 rtx p = loop_end;
7302 if (p == loop_start)
7303 p = loop_end;
7304 p = PREV_INSN (p);
7305 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
7306 continue;
7307 ends_need_computing -= find_life_end (PATTERN (p), stats, p, biv);
7309 while (ends_need_computing);
7312 /* Set start_luid back to the last insn that sets the giv. This allows
7313 more combinations. */
7314 for (i = 0, v = bl->giv; v; v = v->next_iv)
7316 if (v->ignore)
7317 continue;
7318 if (INSN_UID (v->insn) < max_uid_for_loop)
7319 stats[i].start_luid = INSN_LUID (v->insn);
7320 i++;
7323 /* Now adjust lifetime ends by taking combined givs into account. */
7324 for (i = 0, v = bl->giv; v; v = v->next_iv)
7326 unsigned luid;
7327 int j;
7329 if (v->ignore)
7330 continue;
7331 if (v->same && ! v->same->ignore)
7333 j = v->same->ix;
7334 luid = stats[i].start_luid;
7335 /* Use unsigned arithmetic to model loop wrap-around. */
7336 if (luid - stats[j].start_luid
7337 > (unsigned) stats[j].end_luid - stats[j].start_luid)
7338 stats[j].end_luid = luid;
7340 i++;
7343 qsort (stats, giv_count, sizeof(*stats), cmp_recombine_givs_stats);
7345 /* Try to derive DEST_REG givs from previous DEST_REG givs with the
7346 same mult_val and non-overlapping lifetime. This reduces register
7347 pressure.
7348 Once we find a DEST_REG giv that is suitable to derive others from,
7349 we set last_giv to this giv, and try to derive as many other DEST_REG
7350 givs from it without joining overlapping lifetimes. If we then
7351 encounter a DEST_REG giv that we can't derive, we set rescan to the
7352 index for this giv (unless rescan is already set).
7353 When we are finished with the current LAST_GIV (i.e. the inner loop
7354 terminates), we start again with rescan, which then becomes the new
7355 LAST_GIV. */
7356 for (i = giv_count - 1; i >= 0; i = rescan)
7358 int life_start, life_end;
7360 for (last_giv = 0, rescan = -1; i >= 0; i--)
7362 rtx sum;
7364 v = giv_array[stats[i].giv_number];
7365 if (v->giv_type != DEST_REG || v->derived_from || v->same)
7366 continue;
7367 if (! last_giv)
7369 /* Don't use a giv that's likely to be dead to derive
7370 others - that would be likely to keep that giv alive. */
7371 if (! v->maybe_dead || v->combined_with)
7373 last_giv = v;
7374 life_start = stats[i].start_luid;
7375 life_end = stats[i].end_luid;
7377 continue;
7379 /* Use unsigned arithmetic to model loop wrap around. */
7380 if (((unsigned) stats[i].start_luid - life_start
7381 >= (unsigned) life_end - life_start)
7382 && ((unsigned) stats[i].end_luid - life_start
7383 > (unsigned) life_end - life_start)
7384 /* Check that the giv insn we're about to use for deriving
7385 precedes all uses of that giv. Note that initializing the
7386 derived giv would defeat the purpose of reducing register
7387 pressure.
7388 ??? We could arrange to move the insn. */
7389 && ((unsigned) stats[i].end_luid - INSN_LUID (loop_start)
7390 > (unsigned) stats[i].start_luid - INSN_LUID (loop_start))
7391 && rtx_equal_p (last_giv->mult_val, v->mult_val)
7392 /* ??? Could handle libcalls, but would need more logic. */
7393 && ! find_reg_note (v->insn, REG_RETVAL, NULL_RTX)
7394 /* We would really like to know if for any giv that v
7395 is combined with, v->insn or any intervening biv increment
7396 dominates that combined giv. However, we
7397 don't have this detailed control flow information.
7398 N.B. since last_giv will be reduced, it is valid
7399 anywhere in the loop, so we don't need to check the
7400 validity of last_giv.
7401 We rely here on the fact that v->always_executed implies that
7402 there is no jump to someplace else in the loop before the
7403 giv insn, and hence any insn that is executed before the
7404 giv insn in the loop will have a lower luid. */
7405 && (v->always_executed || ! v->combined_with)
7406 && (sum = express_from (last_giv, v))
7407 /* Make sure we don't make the add more expensive. ADD_COST
7408 doesn't take different costs of registers and constants into
7409 account, so compare the cost of the actual SET_SRCs. */
7410 && (rtx_cost (sum, SET)
7411 <= rtx_cost (SET_SRC (single_set (v->insn)), SET))
7412 /* ??? unroll can't understand anything but reg + const_int
7413 sums. It would be cleaner to fix unroll. */
7414 && ((GET_CODE (sum) == PLUS
7415 && GET_CODE (XEXP (sum, 0)) == REG
7416 && GET_CODE (XEXP (sum, 1)) == CONST_INT)
7417 || ! unroll_p)
7418 && validate_change (v->insn, &PATTERN (v->insn),
7419 gen_rtx_SET (VOIDmode, v->dest_reg, sum), 0))
7421 v->derived_from = last_giv;
7422 life_end = stats[i].end_luid;
7424 if (loop_dump_stream)
7426 fprintf (loop_dump_stream,
7427 "giv at %d derived from %d as ",
7428 INSN_UID (v->insn), INSN_UID (last_giv->insn));
7429 print_rtl (loop_dump_stream, sum);
7430 putc ('\n', loop_dump_stream);
7433 else if (rescan < 0)
7434 rescan = i;
7439 /* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
7441 void
7442 emit_iv_add_mult (b, m, a, reg, insert_before)
7443 rtx b; /* initial value of basic induction variable */
7444 rtx m; /* multiplicative constant */
7445 rtx a; /* additive constant */
7446 rtx reg; /* destination register */
7447 rtx insert_before;
7449 rtx seq;
7450 rtx result;
7452 /* Prevent unexpected sharing of these rtx. */
7453 a = copy_rtx (a);
7454 b = copy_rtx (b);
7456 /* Increase the lifetime of any invariants moved further in code. */
7457 update_reg_last_use (a, insert_before);
7458 update_reg_last_use (b, insert_before);
7459 update_reg_last_use (m, insert_before);
7461 start_sequence ();
7462 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 0);
7463 if (reg != result)
7464 emit_move_insn (reg, result);
7465 seq = gen_sequence ();
7466 end_sequence ();
7468 emit_insn_before (seq, insert_before);
7470 /* It is entirely possible that the expansion created lots of new
7471 registers. Iterate over the sequence we just created and
7472 record them all. */
7474 if (GET_CODE (seq) == SEQUENCE)
7476 int i;
7477 for (i = 0; i < XVECLEN (seq, 0); ++i)
7479 rtx set = single_set (XVECEXP (seq, 0, i));
7480 if (set && GET_CODE (SET_DEST (set)) == REG)
7481 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
7484 else if (GET_CODE (seq) == SET
7485 && GET_CODE (SET_DEST (seq)) == REG)
7486 record_base_value (REGNO (SET_DEST (seq)), SET_SRC (seq), 0);
7489 /* Test whether A * B can be computed without
7490 an actual multiply insn. Value is 1 if so. */
7492 static int
7493 product_cheap_p (a, b)
7494 rtx a;
7495 rtx b;
7497 int i;
7498 rtx tmp;
7499 struct obstack *old_rtl_obstack = rtl_obstack;
7500 char *storage = (char *) obstack_alloc (&temp_obstack, 0);
7501 int win = 1;
7503 /* If only one is constant, make it B. */
7504 if (GET_CODE (a) == CONST_INT)
7505 tmp = a, a = b, b = tmp;
7507 /* If first constant, both constant, so don't need multiply. */
7508 if (GET_CODE (a) == CONST_INT)
7509 return 1;
7511 /* If second not constant, neither is constant, so would need multiply. */
7512 if (GET_CODE (b) != CONST_INT)
7513 return 0;
7515 /* One operand is constant, so might not need multiply insn. Generate the
7516 code for the multiply and see if a call or multiply, or long sequence
7517 of insns is generated. */
7519 rtl_obstack = &temp_obstack;
7520 start_sequence ();
7521 expand_mult (GET_MODE (a), a, b, NULL_RTX, 0);
7522 tmp = gen_sequence ();
7523 end_sequence ();
7525 if (GET_CODE (tmp) == SEQUENCE)
7527 if (XVEC (tmp, 0) == 0)
7528 win = 1;
7529 else if (XVECLEN (tmp, 0) > 3)
7530 win = 0;
7531 else
7532 for (i = 0; i < XVECLEN (tmp, 0); i++)
7534 rtx insn = XVECEXP (tmp, 0, i);
7536 if (GET_CODE (insn) != INSN
7537 || (GET_CODE (PATTERN (insn)) == SET
7538 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
7539 || (GET_CODE (PATTERN (insn)) == PARALLEL
7540 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
7541 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
7543 win = 0;
7544 break;
7548 else if (GET_CODE (tmp) == SET
7549 && GET_CODE (SET_SRC (tmp)) == MULT)
7550 win = 0;
7551 else if (GET_CODE (tmp) == PARALLEL
7552 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
7553 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
7554 win = 0;
7556 /* Free any storage we obtained in generating this multiply and restore rtl
7557 allocation to its normal obstack. */
7558 obstack_free (&temp_obstack, storage);
7559 rtl_obstack = old_rtl_obstack;
7561 return win;
7564 /* Check to see if loop can be terminated by a "decrement and branch until
7565 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
7566 Also try reversing an increment loop to a decrement loop
7567 to see if the optimization can be performed.
7568 Value is nonzero if optimization was performed. */
7570 /* This is useful even if the architecture doesn't have such an insn,
7571 because it might change a loops which increments from 0 to n to a loop
7572 which decrements from n to 0. A loop that decrements to zero is usually
7573 faster than one that increments from zero. */
7575 /* ??? This could be rewritten to use some of the loop unrolling procedures,
7576 such as approx_final_value, biv_total_increment, loop_iterations, and
7577 final_[bg]iv_value. */
7579 static int
7580 check_dbra_loop (loop_end, insn_count, loop_start, loop_info)
7581 rtx loop_end;
7582 int insn_count;
7583 rtx loop_start;
7584 struct loop_info *loop_info;
7586 struct iv_class *bl;
7587 rtx reg;
7588 rtx jump_label;
7589 rtx final_value;
7590 rtx start_value;
7591 rtx new_add_val;
7592 rtx comparison;
7593 rtx before_comparison;
7594 rtx p;
7595 rtx jump;
7596 rtx first_compare;
7597 int compare_and_branch;
7599 /* If last insn is a conditional branch, and the insn before tests a
7600 register value, try to optimize it. Otherwise, we can't do anything. */
7602 jump = PREV_INSN (loop_end);
7603 comparison = get_condition_for_loop (jump);
7604 if (comparison == 0)
7605 return 0;
7607 /* Try to compute whether the compare/branch at the loop end is one or
7608 two instructions. */
7609 get_condition (jump, &first_compare);
7610 if (first_compare == jump)
7611 compare_and_branch = 1;
7612 else if (first_compare == prev_nonnote_insn (jump))
7613 compare_and_branch = 2;
7614 else
7615 return 0;
7617 /* Check all of the bivs to see if the compare uses one of them.
7618 Skip biv's set more than once because we can't guarantee that
7619 it will be zero on the last iteration. Also skip if the biv is
7620 used between its update and the test insn. */
7622 for (bl = loop_iv_list; bl; bl = bl->next)
7624 if (bl->biv_count == 1
7625 && bl->biv->dest_reg == XEXP (comparison, 0)
7626 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
7627 first_compare))
7628 break;
7631 if (! bl)
7632 return 0;
7634 /* Look for the case where the basic induction variable is always
7635 nonnegative, and equals zero on the last iteration.
7636 In this case, add a reg_note REG_NONNEG, which allows the
7637 m68k DBRA instruction to be used. */
7639 if (((GET_CODE (comparison) == GT
7640 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
7641 && INTVAL (XEXP (comparison, 1)) == -1)
7642 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
7643 && GET_CODE (bl->biv->add_val) == CONST_INT
7644 && INTVAL (bl->biv->add_val) < 0)
7646 /* Initial value must be greater than 0,
7647 init_val % -dec_value == 0 to ensure that it equals zero on
7648 the last iteration */
7650 if (GET_CODE (bl->initial_value) == CONST_INT
7651 && INTVAL (bl->initial_value) > 0
7652 && (INTVAL (bl->initial_value)
7653 % (-INTVAL (bl->biv->add_val))) == 0)
7655 /* register always nonnegative, add REG_NOTE to branch */
7656 REG_NOTES (PREV_INSN (loop_end))
7657 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
7658 REG_NOTES (PREV_INSN (loop_end)));
7659 bl->nonneg = 1;
7661 return 1;
7664 /* If the decrement is 1 and the value was tested as >= 0 before
7665 the loop, then we can safely optimize. */
7666 for (p = loop_start; p; p = PREV_INSN (p))
7668 if (GET_CODE (p) == CODE_LABEL)
7669 break;
7670 if (GET_CODE (p) != JUMP_INSN)
7671 continue;
7673 before_comparison = get_condition_for_loop (p);
7674 if (before_comparison
7675 && XEXP (before_comparison, 0) == bl->biv->dest_reg
7676 && GET_CODE (before_comparison) == LT
7677 && XEXP (before_comparison, 1) == const0_rtx
7678 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
7679 && INTVAL (bl->biv->add_val) == -1)
7681 REG_NOTES (PREV_INSN (loop_end))
7682 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
7683 REG_NOTES (PREV_INSN (loop_end)));
7684 bl->nonneg = 1;
7686 return 1;
7690 else if (INTVAL (bl->biv->add_val) > 0)
7692 /* Try to change inc to dec, so can apply above optimization. */
7693 /* Can do this if:
7694 all registers modified are induction variables or invariant,
7695 all memory references have non-overlapping addresses
7696 (obviously true if only one write)
7697 allow 2 insns for the compare/jump at the end of the loop. */
7698 /* Also, we must avoid any instructions which use both the reversed
7699 biv and another biv. Such instructions will fail if the loop is
7700 reversed. We meet this condition by requiring that either
7701 no_use_except_counting is true, or else that there is only
7702 one biv. */
7703 int num_nonfixed_reads = 0;
7704 /* 1 if the iteration var is used only to count iterations. */
7705 int no_use_except_counting = 0;
7706 /* 1 if the loop has no memory store, or it has a single memory store
7707 which is reversible. */
7708 int reversible_mem_store = 1;
7710 if (bl->giv_count == 0
7711 && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
7713 rtx bivreg = regno_reg_rtx[bl->regno];
7715 /* If there are no givs for this biv, and the only exit is the
7716 fall through at the end of the loop, then
7717 see if perhaps there are no uses except to count. */
7718 no_use_except_counting = 1;
7719 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7720 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7722 rtx set = single_set (p);
7724 if (set && GET_CODE (SET_DEST (set)) == REG
7725 && REGNO (SET_DEST (set)) == bl->regno)
7726 /* An insn that sets the biv is okay. */
7728 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
7729 || p == prev_nonnote_insn (loop_end))
7730 /* Don't bother about the end test. */
7732 else if (reg_mentioned_p (bivreg, PATTERN (p)))
7734 no_use_except_counting = 0;
7735 break;
7740 if (no_use_except_counting)
7741 ; /* no need to worry about MEMs. */
7742 else if (num_mem_sets <= 1)
7744 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7745 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7746 num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
7748 /* If the loop has a single store, and the destination address is
7749 invariant, then we can't reverse the loop, because this address
7750 might then have the wrong value at loop exit.
7751 This would work if the source was invariant also, however, in that
7752 case, the insn should have been moved out of the loop. */
7754 if (num_mem_sets == 1)
7756 struct induction *v;
7758 reversible_mem_store
7759 = (! unknown_address_altered
7760 && ! invariant_p (XEXP (loop_store_mems, 0)));
7762 /* If the store depends on a register that is set after the
7763 store, it depends on the initial value, and is thus not
7764 reversible. */
7765 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
7767 if (v->giv_type == DEST_REG
7768 && reg_mentioned_p (v->dest_reg,
7769 XEXP (loop_store_mems, 0))
7770 && loop_insn_first_p (first_loop_store_insn, v->insn))
7771 reversible_mem_store = 0;
7775 else
7776 return 0;
7778 /* This code only acts for innermost loops. Also it simplifies
7779 the memory address check by only reversing loops with
7780 zero or one memory access.
7781 Two memory accesses could involve parts of the same array,
7782 and that can't be reversed.
7783 If the biv is used only for counting, than we don't need to worry
7784 about all these things. */
7786 if ((num_nonfixed_reads <= 1
7787 && !loop_has_call
7788 && !loop_has_volatile
7789 && reversible_mem_store
7790 && (bl->giv_count + bl->biv_count + num_mem_sets
7791 + num_movables + compare_and_branch == insn_count)
7792 && (bl == loop_iv_list && bl->next == 0))
7793 || no_use_except_counting)
7795 rtx tem;
7797 /* Loop can be reversed. */
7798 if (loop_dump_stream)
7799 fprintf (loop_dump_stream, "Can reverse loop\n");
7801 /* Now check other conditions:
7803 The increment must be a constant, as must the initial value,
7804 and the comparison code must be LT.
7806 This test can probably be improved since +/- 1 in the constant
7807 can be obtained by changing LT to LE and vice versa; this is
7808 confusing. */
7810 if (comparison
7811 /* for constants, LE gets turned into LT */
7812 && (GET_CODE (comparison) == LT
7813 || (GET_CODE (comparison) == LE
7814 && no_use_except_counting)))
7816 HOST_WIDE_INT add_val, add_adjust, comparison_val;
7817 rtx initial_value, comparison_value;
7818 int nonneg = 0;
7819 enum rtx_code cmp_code;
7820 int comparison_const_width;
7821 unsigned HOST_WIDE_INT comparison_sign_mask;
7823 add_val = INTVAL (bl->biv->add_val);
7824 comparison_value = XEXP (comparison, 1);
7825 if (GET_MODE (comparison_value) == VOIDmode)
7826 comparison_const_width
7827 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
7828 else
7829 comparison_const_width
7830 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
7831 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
7832 comparison_const_width = HOST_BITS_PER_WIDE_INT;
7833 comparison_sign_mask
7834 = (unsigned HOST_WIDE_INT)1 << (comparison_const_width - 1);
7836 /* If the comparison value is not a loop invariant, then we
7837 can not reverse this loop.
7839 ??? If the insns which initialize the comparison value as
7840 a whole compute an invariant result, then we could move
7841 them out of the loop and proceed with loop reversal. */
7842 if (!invariant_p (comparison_value))
7843 return 0;
7845 if (GET_CODE (comparison_value) == CONST_INT)
7846 comparison_val = INTVAL (comparison_value);
7847 initial_value = bl->initial_value;
7849 /* Normalize the initial value if it is an integer and
7850 has no other use except as a counter. This will allow
7851 a few more loops to be reversed. */
7852 if (no_use_except_counting
7853 && GET_CODE (comparison_value) == CONST_INT
7854 && GET_CODE (initial_value) == CONST_INT)
7856 comparison_val = comparison_val - INTVAL (bl->initial_value);
7857 /* The code below requires comparison_val to be a multiple
7858 of add_val in order to do the loop reversal, so
7859 round up comparison_val to a multiple of add_val.
7860 Since comparison_value is constant, we know that the
7861 current comparison code is LT. */
7862 comparison_val = comparison_val + add_val - 1;
7863 comparison_val
7864 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
7865 /* We postpone overflow checks for COMPARISON_VAL here;
7866 even if there is an overflow, we might still be able to
7867 reverse the loop, if converting the loop exit test to
7868 NE is possible. */
7869 initial_value = const0_rtx;
7872 /* First check if we can do a vanilla loop reversal. */
7873 if (initial_value == const0_rtx
7874 /* If we have a decrement_and_branch_on_count, prefer
7875 the NE test, since this will allow that instruction to
7876 be generated. Note that we must use a vanilla loop
7877 reversal if the biv is used to calculate a giv or has
7878 a non-counting use. */
7879 #if ! defined (HAVE_decrement_and_branch_until_zero) && defined (HAVE_decrement_and_branch_on_count)
7880 && (! (add_val == 1 && loop_info->vtop
7881 && (bl->biv_count == 0
7882 || no_use_except_counting)))
7883 #endif
7884 && GET_CODE (comparison_value) == CONST_INT
7885 /* Now do postponed overflow checks on COMPARISON_VAL. */
7886 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
7887 & comparison_sign_mask))
7889 /* Register will always be nonnegative, with value
7890 0 on last iteration */
7891 add_adjust = add_val;
7892 nonneg = 1;
7893 cmp_code = GE;
7895 else if (add_val == 1 && loop_info->vtop
7896 && (bl->biv_count == 0
7897 || no_use_except_counting))
7899 add_adjust = 0;
7900 cmp_code = NE;
7902 else
7903 return 0;
7905 if (GET_CODE (comparison) == LE)
7906 add_adjust -= add_val;
7908 /* If the initial value is not zero, or if the comparison
7909 value is not an exact multiple of the increment, then we
7910 can not reverse this loop. */
7911 if (initial_value == const0_rtx
7912 && GET_CODE (comparison_value) == CONST_INT)
7914 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
7915 return 0;
7917 else
7919 if (! no_use_except_counting || add_val != 1)
7920 return 0;
7923 final_value = comparison_value;
7925 /* Reset these in case we normalized the initial value
7926 and comparison value above. */
7927 if (GET_CODE (comparison_value) == CONST_INT
7928 && GET_CODE (initial_value) == CONST_INT)
7930 comparison_value = GEN_INT (comparison_val);
7931 final_value
7932 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
7934 bl->initial_value = initial_value;
7936 /* Save some info needed to produce the new insns. */
7937 reg = bl->biv->dest_reg;
7938 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
7939 if (jump_label == pc_rtx)
7940 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
7941 new_add_val = GEN_INT (- INTVAL (bl->biv->add_val));
7943 /* Set start_value; if this is not a CONST_INT, we need
7944 to generate a SUB.
7945 Initialize biv to start_value before loop start.
7946 The old initializing insn will be deleted as a
7947 dead store by flow.c. */
7948 if (initial_value == const0_rtx
7949 && GET_CODE (comparison_value) == CONST_INT)
7951 start_value = GEN_INT (comparison_val - add_adjust);
7952 emit_insn_before (gen_move_insn (reg, start_value),
7953 loop_start);
7955 else if (GET_CODE (initial_value) == CONST_INT)
7957 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
7958 enum machine_mode mode = GET_MODE (reg);
7959 enum insn_code icode
7960 = add_optab->handlers[(int) mode].insn_code;
7961 if (! (*insn_operand_predicate[icode][0]) (reg, mode)
7962 || ! ((*insn_operand_predicate[icode][1])
7963 (comparison_value, mode))
7964 || ! (*insn_operand_predicate[icode][2]) (offset, mode))
7965 return 0;
7966 start_value
7967 = gen_rtx_PLUS (mode, comparison_value, offset);
7968 emit_insn_before ((GEN_FCN (icode)
7969 (reg, comparison_value, offset)),
7970 loop_start);
7971 if (GET_CODE (comparison) == LE)
7972 final_value = gen_rtx_PLUS (mode, comparison_value,
7973 GEN_INT (add_val));
7975 else if (! add_adjust)
7977 enum machine_mode mode = GET_MODE (reg);
7978 enum insn_code icode
7979 = sub_optab->handlers[(int) mode].insn_code;
7980 if (! (*insn_operand_predicate[icode][0]) (reg, mode)
7981 || ! ((*insn_operand_predicate[icode][1])
7982 (comparison_value, mode))
7983 || ! ((*insn_operand_predicate[icode][2])
7984 (initial_value, mode)))
7985 return 0;
7986 start_value
7987 = gen_rtx_MINUS (mode, comparison_value, initial_value);
7988 emit_insn_before ((GEN_FCN (icode)
7989 (reg, comparison_value, initial_value)),
7990 loop_start);
7992 else
7993 /* We could handle the other cases too, but it'll be
7994 better to have a testcase first. */
7995 return 0;
7997 /* We may not have a single insn which can increment a reg, so
7998 create a sequence to hold all the insns from expand_inc. */
7999 start_sequence ();
8000 expand_inc (reg, new_add_val);
8001 tem = gen_sequence ();
8002 end_sequence ();
8004 p = emit_insn_before (tem, bl->biv->insn);
8005 delete_insn (bl->biv->insn);
8007 /* Update biv info to reflect its new status. */
8008 bl->biv->insn = p;
8009 bl->initial_value = start_value;
8010 bl->biv->add_val = new_add_val;
8012 /* Update loop info. */
8013 loop_info->initial_value = reg;
8014 loop_info->initial_equiv_value = reg;
8015 loop_info->final_value = const0_rtx;
8016 loop_info->final_equiv_value = const0_rtx;
8017 loop_info->comparison_value = const0_rtx;
8018 loop_info->comparison_code = cmp_code;
8019 loop_info->increment = new_add_val;
8021 /* Inc LABEL_NUSES so that delete_insn will
8022 not delete the label. */
8023 LABEL_NUSES (XEXP (jump_label, 0)) ++;
8025 /* Emit an insn after the end of the loop to set the biv's
8026 proper exit value if it is used anywhere outside the loop. */
8027 if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
8028 || ! bl->init_insn
8029 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
8030 emit_insn_after (gen_move_insn (reg, final_value),
8031 loop_end);
8033 /* Delete compare/branch at end of loop. */
8034 delete_insn (PREV_INSN (loop_end));
8035 if (compare_and_branch == 2)
8036 delete_insn (first_compare);
8038 /* Add new compare/branch insn at end of loop. */
8039 start_sequence ();
8040 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
8041 GET_MODE (reg), 0, 0,
8042 XEXP (jump_label, 0));
8043 tem = gen_sequence ();
8044 end_sequence ();
8045 emit_jump_insn_before (tem, loop_end);
8047 for (tem = PREV_INSN (loop_end);
8048 tem && GET_CODE (tem) != JUMP_INSN;
8049 tem = PREV_INSN (tem))
8052 if (tem)
8053 JUMP_LABEL (tem) = XEXP (jump_label, 0);
8055 if (nonneg)
8057 if (tem)
8059 /* Increment of LABEL_NUSES done above. */
8060 /* Register is now always nonnegative,
8061 so add REG_NONNEG note to the branch. */
8062 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
8063 REG_NOTES (tem));
8065 bl->nonneg = 1;
8068 /* Mark that this biv has been reversed. Each giv which depends
8069 on this biv, and which is also live past the end of the loop
8070 will have to be fixed up. */
8072 bl->reversed = 1;
8074 if (loop_dump_stream)
8076 fprintf (loop_dump_stream, "Reversed loop");
8077 if (bl->nonneg)
8078 fprintf (loop_dump_stream, " and added reg_nonneg\n");
8079 else
8080 fprintf (loop_dump_stream, "\n");
8083 return 1;
8088 return 0;
8091 /* Verify whether the biv BL appears to be eliminable,
8092 based on the insns in the loop that refer to it.
8093 LOOP_START is the first insn of the loop, and END is the end insn.
8095 If ELIMINATE_P is non-zero, actually do the elimination.
8097 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8098 determine whether invariant insns should be placed inside or at the
8099 start of the loop. */
8101 static int
8102 maybe_eliminate_biv (bl, loop_start, end, eliminate_p, threshold, insn_count)
8103 struct iv_class *bl;
8104 rtx loop_start;
8105 rtx end;
8106 int eliminate_p;
8107 int threshold, insn_count;
8109 rtx reg = bl->biv->dest_reg;
8110 rtx p;
8112 /* Scan all insns in the loop, stopping if we find one that uses the
8113 biv in a way that we cannot eliminate. */
8115 for (p = loop_start; p != end; p = NEXT_INSN (p))
8117 enum rtx_code code = GET_CODE (p);
8118 rtx where = threshold >= insn_count ? loop_start : p;
8120 /* If this is a libcall that sets a giv, skip ahead to its end. */
8121 if (GET_RTX_CLASS (code) == 'i')
8123 rtx note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
8125 if (note)
8127 rtx last = XEXP (note, 0);
8128 rtx set = single_set (last);
8130 if (set && GET_CODE (SET_DEST (set)) == REG)
8132 int regno = REGNO (SET_DEST (set));
8134 if (REG_IV_TYPE (regno) == GENERAL_INDUCT
8135 && REG_IV_INFO (regno)->src_reg == bl->biv->src_reg)
8136 p = last;
8140 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
8141 && reg_mentioned_p (reg, PATTERN (p))
8142 && ! maybe_eliminate_biv_1 (PATTERN (p), p, bl, eliminate_p, where))
8144 if (loop_dump_stream)
8145 fprintf (loop_dump_stream,
8146 "Cannot eliminate biv %d: biv used in insn %d.\n",
8147 bl->regno, INSN_UID (p));
8148 break;
8152 if (p == end)
8154 if (loop_dump_stream)
8155 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
8156 bl->regno, eliminate_p ? "was" : "can be");
8157 return 1;
8160 return 0;
8163 /* INSN and REFERENCE are instructions in the same insn chain.
8164 Return non-zero if INSN is first. */
8167 loop_insn_first_p (insn, reference)
8168 rtx insn, reference;
8170 rtx p, q;
8172 for (p = insn, q = reference; ;)
8174 /* Start with test for not first so that INSN == REFERENCE yields not
8175 first. */
8176 if (q == insn || ! p)
8177 return 0;
8178 if (p == reference || ! q)
8179 return 1;
8181 if (INSN_UID (p) < max_uid_for_loop
8182 && INSN_UID (q) < max_uid_for_loop)
8183 return INSN_LUID (p) < INSN_LUID (q);
8185 if (INSN_UID (p) >= max_uid_for_loop)
8186 p = NEXT_INSN (p);
8187 if (INSN_UID (q) >= max_uid_for_loop)
8188 q = NEXT_INSN (q);
8192 /* We are trying to eliminate BIV in INSN using GIV. Return non-zero if
8193 the offset that we have to take into account due to auto-increment /
8194 div derivation is zero. */
8195 static int
8196 biv_elimination_giv_has_0_offset (biv, giv, insn)
8197 struct induction *biv, *giv;
8198 rtx insn;
8200 /* If the giv V had the auto-inc address optimization applied
8201 to it, and INSN occurs between the giv insn and the biv
8202 insn, then we'd have to adjust the value used here.
8203 This is rare, so we don't bother to make this possible. */
8204 if (giv->auto_inc_opt
8205 && ((loop_insn_first_p (giv->insn, insn)
8206 && loop_insn_first_p (insn, biv->insn))
8207 || (loop_insn_first_p (biv->insn, insn)
8208 && loop_insn_first_p (insn, giv->insn))))
8209 return 0;
8211 /* If the giv V was derived from another giv, and INSN does
8212 not occur between the giv insn and the biv insn, then we'd
8213 have to adjust the value used here. This is rare, so we don't
8214 bother to make this possible. */
8215 if (giv->derived_from
8216 && ! (giv->always_executed
8217 && loop_insn_first_p (giv->insn, insn)
8218 && loop_insn_first_p (insn, biv->insn)))
8219 return 0;
8220 if (giv->same
8221 && giv->same->derived_from
8222 && ! (giv->same->always_executed
8223 && loop_insn_first_p (giv->same->insn, insn)
8224 && loop_insn_first_p (insn, biv->insn)))
8225 return 0;
8227 return 1;
8230 /* If BL appears in X (part of the pattern of INSN), see if we can
8231 eliminate its use. If so, return 1. If not, return 0.
8233 If BIV does not appear in X, return 1.
8235 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
8236 where extra insns should be added. Depending on how many items have been
8237 moved out of the loop, it will either be before INSN or at the start of
8238 the loop. */
8240 static int
8241 maybe_eliminate_biv_1 (x, insn, bl, eliminate_p, where)
8242 rtx x, insn;
8243 struct iv_class *bl;
8244 int eliminate_p;
8245 rtx where;
8247 enum rtx_code code = GET_CODE (x);
8248 rtx reg = bl->biv->dest_reg;
8249 enum machine_mode mode = GET_MODE (reg);
8250 struct induction *v;
8251 rtx arg, tem;
8252 #ifdef HAVE_cc0
8253 rtx new;
8254 #endif
8255 int arg_operand;
8256 char *fmt;
8257 int i, j;
8259 switch (code)
8261 case REG:
8262 /* If we haven't already been able to do something with this BIV,
8263 we can't eliminate it. */
8264 if (x == reg)
8265 return 0;
8266 return 1;
8268 case SET:
8269 /* If this sets the BIV, it is not a problem. */
8270 if (SET_DEST (x) == reg)
8271 return 1;
8273 /* If this is an insn that defines a giv, it is also ok because
8274 it will go away when the giv is reduced. */
8275 for (v = bl->giv; v; v = v->next_iv)
8276 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
8277 return 1;
8279 #ifdef HAVE_cc0
8280 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
8282 /* Can replace with any giv that was reduced and
8283 that has (MULT_VAL != 0) and (ADD_VAL == 0).
8284 Require a constant for MULT_VAL, so we know it's nonzero.
8285 ??? We disable this optimization to avoid potential
8286 overflows. */
8288 for (v = bl->giv; v; v = v->next_iv)
8289 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
8290 && v->add_val == const0_rtx
8291 && ! v->ignore && ! v->maybe_dead && v->always_computable
8292 && v->mode == mode
8293 && 0)
8295 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8296 continue;
8298 if (! eliminate_p)
8299 return 1;
8301 /* If the giv has the opposite direction of change,
8302 then reverse the comparison. */
8303 if (INTVAL (v->mult_val) < 0)
8304 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
8305 const0_rtx, v->new_reg);
8306 else
8307 new = v->new_reg;
8309 /* We can probably test that giv's reduced reg. */
8310 if (validate_change (insn, &SET_SRC (x), new, 0))
8311 return 1;
8314 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8315 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
8316 Require a constant for MULT_VAL, so we know it's nonzero.
8317 ??? Do this only if ADD_VAL is a pointer to avoid a potential
8318 overflow problem. */
8320 for (v = bl->giv; v; v = v->next_iv)
8321 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
8322 && ! v->ignore && ! v->maybe_dead && v->always_computable
8323 && v->mode == mode
8324 && (GET_CODE (v->add_val) == SYMBOL_REF
8325 || GET_CODE (v->add_val) == LABEL_REF
8326 || GET_CODE (v->add_val) == CONST
8327 || (GET_CODE (v->add_val) == REG
8328 && REGNO_POINTER_FLAG (REGNO (v->add_val)))))
8330 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8331 continue;
8333 if (! eliminate_p)
8334 return 1;
8336 /* If the giv has the opposite direction of change,
8337 then reverse the comparison. */
8338 if (INTVAL (v->mult_val) < 0)
8339 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
8340 v->new_reg);
8341 else
8342 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
8343 copy_rtx (v->add_val));
8345 /* Replace biv with the giv's reduced register. */
8346 update_reg_last_use (v->add_val, insn);
8347 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8348 return 1;
8350 /* Insn doesn't support that constant or invariant. Copy it
8351 into a register (it will be a loop invariant.) */
8352 tem = gen_reg_rtx (GET_MODE (v->new_reg));
8354 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
8355 where);
8357 /* Substitute the new register for its invariant value in
8358 the compare expression. */
8359 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
8360 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8361 return 1;
8364 #endif
8365 break;
8367 case COMPARE:
8368 case EQ: case NE:
8369 case GT: case GE: case GTU: case GEU:
8370 case LT: case LE: case LTU: case LEU:
8371 /* See if either argument is the biv. */
8372 if (XEXP (x, 0) == reg)
8373 arg = XEXP (x, 1), arg_operand = 1;
8374 else if (XEXP (x, 1) == reg)
8375 arg = XEXP (x, 0), arg_operand = 0;
8376 else
8377 break;
8379 if (CONSTANT_P (arg))
8381 /* First try to replace with any giv that has constant positive
8382 mult_val and constant add_val. We might be able to support
8383 negative mult_val, but it seems complex to do it in general. */
8385 for (v = bl->giv; v; v = v->next_iv)
8386 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
8387 && (GET_CODE (v->add_val) == SYMBOL_REF
8388 || GET_CODE (v->add_val) == LABEL_REF
8389 || GET_CODE (v->add_val) == CONST
8390 || (GET_CODE (v->add_val) == REG
8391 && REGNO_POINTER_FLAG (REGNO (v->add_val))))
8392 && ! v->ignore && ! v->maybe_dead && v->always_computable
8393 && v->mode == mode)
8395 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8396 continue;
8398 if (! eliminate_p)
8399 return 1;
8401 /* Replace biv with the giv's reduced reg. */
8402 XEXP (x, 1-arg_operand) = v->new_reg;
8404 /* If all constants are actually constant integers and
8405 the derived constant can be directly placed in the COMPARE,
8406 do so. */
8407 if (GET_CODE (arg) == CONST_INT
8408 && GET_CODE (v->mult_val) == CONST_INT
8409 && GET_CODE (v->add_val) == CONST_INT
8410 && validate_change (insn, &XEXP (x, arg_operand),
8411 GEN_INT (INTVAL (arg)
8412 * INTVAL (v->mult_val)
8413 + INTVAL (v->add_val)), 0))
8414 return 1;
8416 /* Otherwise, load it into a register. */
8417 tem = gen_reg_rtx (mode);
8418 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
8419 if (validate_change (insn, &XEXP (x, arg_operand), tem, 0))
8420 return 1;
8422 /* If that failed, put back the change we made above. */
8423 XEXP (x, 1-arg_operand) = reg;
8426 /* Look for giv with positive constant mult_val and nonconst add_val.
8427 Insert insns to calculate new compare value.
8428 ??? Turn this off due to possible overflow. */
8430 for (v = bl->giv; v; v = v->next_iv)
8431 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
8432 && ! v->ignore && ! v->maybe_dead && v->always_computable
8433 && v->mode == mode
8434 && 0)
8436 rtx tem;
8438 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8439 continue;
8441 if (! eliminate_p)
8442 return 1;
8444 tem = gen_reg_rtx (mode);
8446 /* Replace biv with giv's reduced register. */
8447 validate_change (insn, &XEXP (x, 1 - arg_operand),
8448 v->new_reg, 1);
8450 /* Compute value to compare against. */
8451 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
8452 /* Use it in this insn. */
8453 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8454 if (apply_change_group ())
8455 return 1;
8458 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
8460 if (invariant_p (arg) == 1)
8462 /* Look for giv with constant positive mult_val and nonconst
8463 add_val. Insert insns to compute new compare value.
8464 ??? Turn this off due to possible overflow. */
8466 for (v = bl->giv; v; v = v->next_iv)
8467 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
8468 && ! v->ignore && ! v->maybe_dead && v->always_computable
8469 && v->mode == mode
8470 && 0)
8472 rtx tem;
8474 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8475 continue;
8477 if (! eliminate_p)
8478 return 1;
8480 tem = gen_reg_rtx (mode);
8482 /* Replace biv with giv's reduced register. */
8483 validate_change (insn, &XEXP (x, 1 - arg_operand),
8484 v->new_reg, 1);
8486 /* Compute value to compare against. */
8487 emit_iv_add_mult (arg, v->mult_val, v->add_val,
8488 tem, where);
8489 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8490 if (apply_change_group ())
8491 return 1;
8495 /* This code has problems. Basically, you can't know when
8496 seeing if we will eliminate BL, whether a particular giv
8497 of ARG will be reduced. If it isn't going to be reduced,
8498 we can't eliminate BL. We can try forcing it to be reduced,
8499 but that can generate poor code.
8501 The problem is that the benefit of reducing TV, below should
8502 be increased if BL can actually be eliminated, but this means
8503 we might have to do a topological sort of the order in which
8504 we try to process biv. It doesn't seem worthwhile to do
8505 this sort of thing now. */
8507 #if 0
8508 /* Otherwise the reg compared with had better be a biv. */
8509 if (GET_CODE (arg) != REG
8510 || REG_IV_TYPE (REGNO (arg)) != BASIC_INDUCT)
8511 return 0;
8513 /* Look for a pair of givs, one for each biv,
8514 with identical coefficients. */
8515 for (v = bl->giv; v; v = v->next_iv)
8517 struct induction *tv;
8519 if (v->ignore || v->maybe_dead || v->mode != mode)
8520 continue;
8522 for (tv = reg_biv_class[REGNO (arg)]->giv; tv; tv = tv->next_iv)
8523 if (! tv->ignore && ! tv->maybe_dead
8524 && rtx_equal_p (tv->mult_val, v->mult_val)
8525 && rtx_equal_p (tv->add_val, v->add_val)
8526 && tv->mode == mode)
8528 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8529 continue;
8531 if (! eliminate_p)
8532 return 1;
8534 /* Replace biv with its giv's reduced reg. */
8535 XEXP (x, 1-arg_operand) = v->new_reg;
8536 /* Replace other operand with the other giv's
8537 reduced reg. */
8538 XEXP (x, arg_operand) = tv->new_reg;
8539 return 1;
8542 #endif
8545 /* If we get here, the biv can't be eliminated. */
8546 return 0;
8548 case MEM:
8549 /* If this address is a DEST_ADDR giv, it doesn't matter if the
8550 biv is used in it, since it will be replaced. */
8551 for (v = bl->giv; v; v = v->next_iv)
8552 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
8553 return 1;
8554 break;
8556 default:
8557 break;
8560 /* See if any subexpression fails elimination. */
8561 fmt = GET_RTX_FORMAT (code);
8562 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8564 switch (fmt[i])
8566 case 'e':
8567 if (! maybe_eliminate_biv_1 (XEXP (x, i), insn, bl,
8568 eliminate_p, where))
8569 return 0;
8570 break;
8572 case 'E':
8573 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8574 if (! maybe_eliminate_biv_1 (XVECEXP (x, i, j), insn, bl,
8575 eliminate_p, where))
8576 return 0;
8577 break;
8581 return 1;
8584 /* Return nonzero if the last use of REG
8585 is in an insn following INSN in the same basic block. */
8587 static int
8588 last_use_this_basic_block (reg, insn)
8589 rtx reg;
8590 rtx insn;
8592 rtx n;
8593 for (n = insn;
8594 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
8595 n = NEXT_INSN (n))
8597 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
8598 return 1;
8600 return 0;
8603 /* Called via `note_stores' to record the initial value of a biv. Here we
8604 just record the location of the set and process it later. */
8606 static void
8607 record_initial (dest, set)
8608 rtx dest;
8609 rtx set;
8611 struct iv_class *bl;
8613 if (GET_CODE (dest) != REG
8614 || REGNO (dest) >= max_reg_before_loop
8615 || REG_IV_TYPE (REGNO (dest)) != BASIC_INDUCT)
8616 return;
8618 bl = reg_biv_class[REGNO (dest)];
8620 /* If this is the first set found, record it. */
8621 if (bl->init_insn == 0)
8623 bl->init_insn = note_insn;
8624 bl->init_set = set;
8628 /* If any of the registers in X are "old" and currently have a last use earlier
8629 than INSN, update them to have a last use of INSN. Their actual last use
8630 will be the previous insn but it will not have a valid uid_luid so we can't
8631 use it. */
8633 static void
8634 update_reg_last_use (x, insn)
8635 rtx x;
8636 rtx insn;
8638 /* Check for the case where INSN does not have a valid luid. In this case,
8639 there is no need to modify the regno_last_uid, as this can only happen
8640 when code is inserted after the loop_end to set a pseudo's final value,
8641 and hence this insn will never be the last use of x. */
8642 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
8643 && INSN_UID (insn) < max_uid_for_loop
8644 && uid_luid[REGNO_LAST_UID (REGNO (x))] < uid_luid[INSN_UID (insn)])
8645 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
8646 else
8648 register int i, j;
8649 register char *fmt = GET_RTX_FORMAT (GET_CODE (x));
8650 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8652 if (fmt[i] == 'e')
8653 update_reg_last_use (XEXP (x, i), insn);
8654 else if (fmt[i] == 'E')
8655 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8656 update_reg_last_use (XVECEXP (x, i, j), insn);
8661 /* Given a jump insn JUMP, return the condition that will cause it to branch
8662 to its JUMP_LABEL. If the condition cannot be understood, or is an
8663 inequality floating-point comparison which needs to be reversed, 0 will
8664 be returned.
8666 If EARLIEST is non-zero, it is a pointer to a place where the earliest
8667 insn used in locating the condition was found. If a replacement test
8668 of the condition is desired, it should be placed in front of that
8669 insn and we will be sure that the inputs are still valid.
8671 The condition will be returned in a canonical form to simplify testing by
8672 callers. Specifically:
8674 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
8675 (2) Both operands will be machine operands; (cc0) will have been replaced.
8676 (3) If an operand is a constant, it will be the second operand.
8677 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
8678 for GE, GEU, and LEU. */
8681 get_condition (jump, earliest)
8682 rtx jump;
8683 rtx *earliest;
8685 enum rtx_code code;
8686 rtx prev = jump;
8687 rtx set;
8688 rtx tem;
8689 rtx op0, op1;
8690 int reverse_code = 0;
8691 int did_reverse_condition = 0;
8692 enum machine_mode mode;
8694 /* If this is not a standard conditional jump, we can't parse it. */
8695 if (GET_CODE (jump) != JUMP_INSN
8696 || ! condjump_p (jump) || simplejump_p (jump))
8697 return 0;
8699 code = GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 0));
8700 mode = GET_MODE (XEXP (SET_SRC (PATTERN (jump)), 0));
8701 op0 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 0);
8702 op1 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 1);
8704 if (earliest)
8705 *earliest = jump;
8707 /* If this branches to JUMP_LABEL when the condition is false, reverse
8708 the condition. */
8709 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
8710 && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
8711 code = reverse_condition (code), did_reverse_condition ^= 1;
8713 /* If we are comparing a register with zero, see if the register is set
8714 in the previous insn to a COMPARE or a comparison operation. Perform
8715 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
8716 in cse.c */
8718 while (GET_RTX_CLASS (code) == '<' && op1 == CONST0_RTX (GET_MODE (op0)))
8720 /* Set non-zero when we find something of interest. */
8721 rtx x = 0;
8723 #ifdef HAVE_cc0
8724 /* If comparison with cc0, import actual comparison from compare
8725 insn. */
8726 if (op0 == cc0_rtx)
8728 if ((prev = prev_nonnote_insn (prev)) == 0
8729 || GET_CODE (prev) != INSN
8730 || (set = single_set (prev)) == 0
8731 || SET_DEST (set) != cc0_rtx)
8732 return 0;
8734 op0 = SET_SRC (set);
8735 op1 = CONST0_RTX (GET_MODE (op0));
8736 if (earliest)
8737 *earliest = prev;
8739 #endif
8741 /* If this is a COMPARE, pick up the two things being compared. */
8742 if (GET_CODE (op0) == COMPARE)
8744 op1 = XEXP (op0, 1);
8745 op0 = XEXP (op0, 0);
8746 continue;
8748 else if (GET_CODE (op0) != REG)
8749 break;
8751 /* Go back to the previous insn. Stop if it is not an INSN. We also
8752 stop if it isn't a single set or if it has a REG_INC note because
8753 we don't want to bother dealing with it. */
8755 if ((prev = prev_nonnote_insn (prev)) == 0
8756 || GET_CODE (prev) != INSN
8757 || FIND_REG_INC_NOTE (prev, 0)
8758 || (set = single_set (prev)) == 0)
8759 break;
8761 /* If this is setting OP0, get what it sets it to if it looks
8762 relevant. */
8763 if (rtx_equal_p (SET_DEST (set), op0))
8765 enum machine_mode inner_mode = GET_MODE (SET_SRC (set));
8767 /* ??? We may not combine comparisons done in a CCmode with
8768 comparisons not done in a CCmode. This is to aid targets
8769 like Alpha that have an IEEE compliant EQ instruction, and
8770 a non-IEEE compliant BEQ instruction. The use of CCmode is
8771 actually artificial, simply to prevent the combination, but
8772 should not affect other platforms.
8774 However, we must allow VOIDmode comparisons to match either
8775 CCmode or non-CCmode comparison, because some ports have
8776 modeless comparisons inside branch patterns.
8778 ??? This mode check should perhaps look more like the mode check
8779 in simplify_comparison in combine. */
8781 if ((GET_CODE (SET_SRC (set)) == COMPARE
8782 || (((code == NE
8783 || (code == LT
8784 && GET_MODE_CLASS (inner_mode) == MODE_INT
8785 && (GET_MODE_BITSIZE (inner_mode)
8786 <= HOST_BITS_PER_WIDE_INT)
8787 && (STORE_FLAG_VALUE
8788 & ((HOST_WIDE_INT) 1
8789 << (GET_MODE_BITSIZE (inner_mode) - 1))))
8790 #ifdef FLOAT_STORE_FLAG_VALUE
8791 || (code == LT
8792 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
8793 && FLOAT_STORE_FLAG_VALUE < 0)
8794 #endif
8796 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
8797 && (((GET_MODE_CLASS (mode) == MODE_CC)
8798 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
8799 || mode == VOIDmode || inner_mode == VOIDmode))
8800 x = SET_SRC (set);
8801 else if (((code == EQ
8802 || (code == GE
8803 && (GET_MODE_BITSIZE (inner_mode)
8804 <= HOST_BITS_PER_WIDE_INT)
8805 && GET_MODE_CLASS (inner_mode) == MODE_INT
8806 && (STORE_FLAG_VALUE
8807 & ((HOST_WIDE_INT) 1
8808 << (GET_MODE_BITSIZE (inner_mode) - 1))))
8809 #ifdef FLOAT_STORE_FLAG_VALUE
8810 || (code == GE
8811 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
8812 && FLOAT_STORE_FLAG_VALUE < 0)
8813 #endif
8815 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
8816 && (((GET_MODE_CLASS (mode) == MODE_CC)
8817 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
8818 || mode == VOIDmode || inner_mode == VOIDmode))
8821 /* We might have reversed a LT to get a GE here. But this wasn't
8822 actually the comparison of data, so we don't flag that we
8823 have had to reverse the condition. */
8824 did_reverse_condition ^= 1;
8825 reverse_code = 1;
8826 x = SET_SRC (set);
8828 else
8829 break;
8832 else if (reg_set_p (op0, prev))
8833 /* If this sets OP0, but not directly, we have to give up. */
8834 break;
8836 if (x)
8838 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
8839 code = GET_CODE (x);
8840 if (reverse_code)
8842 code = reverse_condition (code);
8843 did_reverse_condition ^= 1;
8844 reverse_code = 0;
8847 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
8848 if (earliest)
8849 *earliest = prev;
8853 /* If constant is first, put it last. */
8854 if (CONSTANT_P (op0))
8855 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
8857 /* If OP0 is the result of a comparison, we weren't able to find what
8858 was really being compared, so fail. */
8859 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
8860 return 0;
8862 /* Canonicalize any ordered comparison with integers involving equality
8863 if we can do computations in the relevant mode and we do not
8864 overflow. */
8866 if (GET_CODE (op1) == CONST_INT
8867 && GET_MODE (op0) != VOIDmode
8868 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
8870 HOST_WIDE_INT const_val = INTVAL (op1);
8871 unsigned HOST_WIDE_INT uconst_val = const_val;
8872 unsigned HOST_WIDE_INT max_val
8873 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
8875 switch (code)
8877 case LE:
8878 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
8879 code = LT, op1 = GEN_INT (const_val + 1);
8880 break;
8882 /* When cross-compiling, const_val might be sign-extended from
8883 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
8884 case GE:
8885 if ((HOST_WIDE_INT) (const_val & max_val)
8886 != (((HOST_WIDE_INT) 1
8887 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
8888 code = GT, op1 = GEN_INT (const_val - 1);
8889 break;
8891 case LEU:
8892 if (uconst_val < max_val)
8893 code = LTU, op1 = GEN_INT (uconst_val + 1);
8894 break;
8896 case GEU:
8897 if (uconst_val != 0)
8898 code = GTU, op1 = GEN_INT (uconst_val - 1);
8899 break;
8901 default:
8902 break;
8906 /* If this was floating-point and we reversed anything other than an
8907 EQ or NE, return zero. */
8908 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
8909 && did_reverse_condition && code != NE && code != EQ
8910 && ! flag_fast_math
8911 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
8912 return 0;
8914 #ifdef HAVE_cc0
8915 /* Never return CC0; return zero instead. */
8916 if (op0 == cc0_rtx)
8917 return 0;
8918 #endif
8920 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
8923 /* Similar to above routine, except that we also put an invariant last
8924 unless both operands are invariants. */
8927 get_condition_for_loop (x)
8928 rtx x;
8930 rtx comparison = get_condition (x, NULL_PTR);
8932 if (comparison == 0
8933 || ! invariant_p (XEXP (comparison, 0))
8934 || invariant_p (XEXP (comparison, 1)))
8935 return comparison;
8937 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
8938 XEXP (comparison, 1), XEXP (comparison, 0));
8941 #ifdef HAVE_decrement_and_branch_on_count
8942 /* Instrument loop for insertion of bct instruction. We distinguish between
8943 loops with compile-time bounds and those with run-time bounds.
8944 Information from loop_iterations() is used to compute compile-time bounds.
8945 Run-time bounds should use loop preconditioning, but currently ignored.
8948 static void
8949 insert_bct (loop_start, loop_end, loop_info)
8950 rtx loop_start, loop_end;
8951 struct loop_info *loop_info;
8953 int i;
8954 unsigned HOST_WIDE_INT n_iterations;
8956 int increment_direction, compare_direction;
8958 /* If the loop condition is <= or >=, the number of iteration
8959 is 1 more than the range of the bounds of the loop. */
8960 int add_iteration = 0;
8962 enum machine_mode loop_var_mode = word_mode;
8964 int loop_num = uid_loop_num [INSN_UID (loop_start)];
8966 /* It's impossible to instrument a competely unrolled loop. */
8967 if (loop_info->unroll_number == -1)
8968 return;
8970 /* Make sure that the count register is not in use. */
8971 if (loop_used_count_register [loop_num])
8973 if (loop_dump_stream)
8974 fprintf (loop_dump_stream,
8975 "insert_bct %d: BCT instrumentation failed: count register already in use\n",
8976 loop_num);
8977 return;
8980 /* Make sure that the function has no indirect jumps. */
8981 if (indirect_jump_in_function)
8983 if (loop_dump_stream)
8984 fprintf (loop_dump_stream,
8985 "insert_bct %d: BCT instrumentation failed: indirect jump in function\n",
8986 loop_num);
8987 return;
8990 /* Make sure that the last loop insn is a conditional jump. */
8991 if (GET_CODE (PREV_INSN (loop_end)) != JUMP_INSN
8992 || ! condjump_p (PREV_INSN (loop_end))
8993 || simplejump_p (PREV_INSN (loop_end)))
8995 if (loop_dump_stream)
8996 fprintf (loop_dump_stream,
8997 "insert_bct %d: BCT instrumentation failed: invalid jump at loop end\n",
8998 loop_num);
8999 return;
9002 /* Make sure that the loop does not contain a function call
9003 (the count register might be altered by the called function). */
9004 if (loop_has_call)
9006 if (loop_dump_stream)
9007 fprintf (loop_dump_stream,
9008 "insert_bct %d: BCT instrumentation failed: function call in loop\n",
9009 loop_num);
9010 return;
9013 /* Make sure that the loop does not jump via a table.
9014 (the count register might be used to perform the branch on table). */
9015 if (loop_has_tablejump)
9017 if (loop_dump_stream)
9018 fprintf (loop_dump_stream,
9019 "insert_bct %d: BCT instrumentation failed: computed branch in the loop\n",
9020 loop_num);
9021 return;
9024 /* Account for loop unrolling in instrumented iteration count. */
9025 if (loop_info->unroll_number > 1)
9026 n_iterations = loop_info->n_iterations / loop_info->unroll_number;
9027 else
9028 n_iterations = loop_info->n_iterations;
9030 if (n_iterations != 0 && n_iterations < 3)
9032 /* Allow an enclosing outer loop to benefit if possible. */
9033 if (loop_dump_stream)
9034 fprintf (loop_dump_stream,
9035 "insert_bct %d: Too few iterations to benefit from BCT optimization\n",
9036 loop_num);
9037 return;
9040 /* Try to instrument the loop. */
9042 /* Handle the simpler case, where the bounds are known at compile time. */
9043 if (n_iterations > 0)
9045 /* Mark all enclosing loops that they cannot use count register. */
9046 for (i = loop_num; i != -1; i = loop_outer_loop[i])
9047 loop_used_count_register[i] = 1;
9048 instrument_loop_bct (loop_start, loop_end, GEN_INT (n_iterations));
9049 return;
9052 /* Handle the more complex case, that the bounds are NOT known
9053 at compile time. In this case we generate run_time calculation
9054 of the number of iterations. */
9056 if (loop_info->iteration_var == 0)
9058 if (loop_dump_stream)
9059 fprintf (loop_dump_stream,
9060 "insert_bct %d: BCT Runtime Instrumentation failed: no loop iteration variable found\n",
9061 loop_num);
9062 return;
9065 if (GET_MODE_CLASS (GET_MODE (loop_info->iteration_var)) != MODE_INT
9066 || GET_MODE_SIZE (GET_MODE (loop_info->iteration_var)) != UNITS_PER_WORD)
9068 if (loop_dump_stream)
9069 fprintf (loop_dump_stream,
9070 "insert_bct %d: BCT Runtime Instrumentation failed: loop variable not integer\n",
9071 loop_num);
9072 return;
9075 /* With runtime bounds, if the compare is of the form '!=' we give up */
9076 if (loop_info->comparison_code == NE)
9078 if (loop_dump_stream)
9079 fprintf (loop_dump_stream,
9080 "insert_bct %d: BCT Runtime Instrumentation failed: runtime bounds with != comparison\n",
9081 loop_num);
9082 return;
9084 /* Use common loop preconditioning code instead. */
9085 #if 0
9086 else
9088 /* We rely on the existence of run-time guard to ensure that the
9089 loop executes at least once. */
9090 rtx sequence;
9091 rtx iterations_num_reg;
9093 unsigned HOST_WIDE_INT increment_value_abs
9094 = INTVAL (increment) * increment_direction;
9096 /* make sure that the increment is a power of two, otherwise (an
9097 expensive) divide is needed. */
9098 if (exact_log2 (increment_value_abs) == -1)
9100 if (loop_dump_stream)
9101 fprintf (loop_dump_stream,
9102 "insert_bct: not instrumenting BCT because the increment is not power of 2\n");
9103 return;
9106 /* compute the number of iterations */
9107 start_sequence ();
9109 rtx temp_reg;
9111 /* Again, the number of iterations is calculated by:
9113 ; compare-val - initial-val + (increment -1) + additional-iteration
9114 ; num_iterations = -----------------------------------------------------------------
9115 ; increment
9117 /* ??? Do we have to call copy_rtx here before passing rtx to
9118 expand_binop? */
9119 if (compare_direction > 0)
9121 /* <, <= :the loop variable is increasing */
9122 temp_reg = expand_binop (loop_var_mode, sub_optab,
9123 comparison_value, initial_value,
9124 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9126 else
9128 temp_reg = expand_binop (loop_var_mode, sub_optab,
9129 initial_value, comparison_value,
9130 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9133 if (increment_value_abs - 1 + add_iteration != 0)
9134 temp_reg = expand_binop (loop_var_mode, add_optab, temp_reg,
9135 GEN_INT (increment_value_abs - 1
9136 + add_iteration),
9137 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9139 if (increment_value_abs != 1)
9141 /* ??? This will generate an expensive divide instruction for
9142 most targets. The original authors apparently expected this
9143 to be a shift, since they test for power-of-2 divisors above,
9144 but just naively generating a divide instruction will not give
9145 a shift. It happens to work for the PowerPC target because
9146 the rs6000.md file has a divide pattern that emits shifts.
9147 It will probably not work for any other target. */
9148 iterations_num_reg = expand_binop (loop_var_mode, sdiv_optab,
9149 temp_reg,
9150 GEN_INT (increment_value_abs),
9151 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9153 else
9154 iterations_num_reg = temp_reg;
9156 sequence = gen_sequence ();
9157 end_sequence ();
9158 emit_insn_before (sequence, loop_start);
9159 instrument_loop_bct (loop_start, loop_end, iterations_num_reg);
9162 return;
9163 #endif /* Complex case */
9166 /* Instrument loop by inserting a bct in it as follows:
9167 1. A new counter register is created.
9168 2. In the head of the loop the new variable is initialized to the value
9169 passed in the loop_num_iterations parameter.
9170 3. At the end of the loop, comparison of the register with 0 is generated.
9171 The created comparison follows the pattern defined for the
9172 decrement_and_branch_on_count insn, so this insn will be generated.
9173 4. The branch on the old variable are deleted. The compare must remain
9174 because it might be used elsewhere. If the loop-variable or condition
9175 register are used elsewhere, they will be eliminated by flow. */
9177 static void
9178 instrument_loop_bct (loop_start, loop_end, loop_num_iterations)
9179 rtx loop_start, loop_end;
9180 rtx loop_num_iterations;
9182 rtx counter_reg;
9183 rtx start_label;
9184 rtx sequence;
9186 if (HAVE_decrement_and_branch_on_count)
9188 if (loop_dump_stream)
9190 fputs ("instrument_bct: Inserting BCT (", loop_dump_stream);
9191 if (GET_CODE (loop_num_iterations) == CONST_INT)
9192 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC,
9193 INTVAL (loop_num_iterations));
9194 else
9195 fputs ("runtime", loop_dump_stream);
9196 fputs (" iterations)", loop_dump_stream);
9199 /* Discard original jump to continue loop. Original compare result
9200 may still be live, so it cannot be discarded explicitly. */
9201 delete_insn (PREV_INSN (loop_end));
9203 /* Insert the label which will delimit the start of the loop. */
9204 start_label = gen_label_rtx ();
9205 emit_label_after (start_label, loop_start);
9207 /* Insert initialization of the count register into the loop header. */
9208 start_sequence ();
9209 counter_reg = gen_reg_rtx (word_mode);
9210 emit_insn (gen_move_insn (counter_reg, loop_num_iterations));
9211 sequence = gen_sequence ();
9212 end_sequence ();
9213 emit_insn_before (sequence, loop_start);
9215 /* Insert new comparison on the count register instead of the
9216 old one, generating the needed BCT pattern (that will be
9217 later recognized by assembly generation phase). */
9218 emit_jump_insn_before (gen_decrement_and_branch_on_count (counter_reg,
9219 start_label),
9220 loop_end);
9221 LABEL_NUSES (start_label)++;
9225 #endif /* HAVE_decrement_and_branch_on_count */
9227 /* Scan the function and determine whether it has indirect (computed) jumps.
9229 This is taken mostly from flow.c; similar code exists elsewhere
9230 in the compiler. It may be useful to put this into rtlanal.c. */
9231 static int
9232 indirect_jump_in_function_p (start)
9233 rtx start;
9235 rtx insn;
9237 for (insn = start; insn; insn = NEXT_INSN (insn))
9238 if (computed_jump_p (insn))
9239 return 1;
9241 return 0;
9244 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
9245 documentation for LOOP_MEMS for the definition of `appropriate'.
9246 This function is called from prescan_loop via for_each_rtx. */
9248 static int
9249 insert_loop_mem (mem, data)
9250 rtx *mem;
9251 void *data ATTRIBUTE_UNUSED;
9253 int i;
9254 rtx m = *mem;
9256 if (m == NULL_RTX)
9257 return 0;
9259 switch (GET_CODE (m))
9261 case MEM:
9262 break;
9264 case CONST_DOUBLE:
9265 /* We're not interested in the MEM associated with a
9266 CONST_DOUBLE, so there's no need to traverse into this. */
9267 return -1;
9269 default:
9270 /* This is not a MEM. */
9271 return 0;
9274 /* See if we've already seen this MEM. */
9275 for (i = 0; i < loop_mems_idx; ++i)
9276 if (rtx_equal_p (m, loop_mems[i].mem))
9278 if (GET_MODE (m) != GET_MODE (loop_mems[i].mem))
9279 /* The modes of the two memory accesses are different. If
9280 this happens, something tricky is going on, and we just
9281 don't optimize accesses to this MEM. */
9282 loop_mems[i].optimize = 0;
9284 return 0;
9287 /* Resize the array, if necessary. */
9288 if (loop_mems_idx == loop_mems_allocated)
9290 if (loop_mems_allocated != 0)
9291 loop_mems_allocated *= 2;
9292 else
9293 loop_mems_allocated = 32;
9295 loop_mems = (loop_mem_info*)
9296 xrealloc (loop_mems,
9297 loop_mems_allocated * sizeof (loop_mem_info));
9300 /* Actually insert the MEM. */
9301 loop_mems[loop_mems_idx].mem = m;
9302 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9303 because we can't put it in a register. We still store it in the
9304 table, though, so that if we see the same address later, but in a
9305 non-BLK mode, we'll not think we can optimize it at that point. */
9306 loop_mems[loop_mems_idx].optimize = (GET_MODE (m) != BLKmode);
9307 loop_mems[loop_mems_idx].reg = NULL_RTX;
9308 ++loop_mems_idx;
9310 return 0;
9313 /* Like load_mems, but also ensures that SET_IN_LOOP,
9314 MAY_NOT_OPTIMIZE, REG_SINGLE_USAGE, and INSN_COUNT have the correct
9315 values after load_mems. */
9317 static void
9318 load_mems_and_recount_loop_regs_set (scan_start, end, loop_top, start,
9319 insn_count)
9320 rtx scan_start;
9321 rtx end;
9322 rtx loop_top;
9323 rtx start;
9324 int *insn_count;
9326 int nregs = max_reg_num ();
9328 load_mems (scan_start, end, loop_top, start);
9330 /* Recalculate set_in_loop and friends since load_mems may have
9331 created new registers. */
9332 if (max_reg_num () > nregs)
9334 int i;
9335 int old_nregs;
9337 old_nregs = nregs;
9338 nregs = max_reg_num ();
9340 if ((unsigned) nregs > set_in_loop->num_elements)
9342 /* Grow all the arrays. */
9343 VARRAY_GROW (set_in_loop, nregs);
9344 VARRAY_GROW (n_times_set, nregs);
9345 VARRAY_GROW (may_not_optimize, nregs);
9346 VARRAY_GROW (reg_single_usage, nregs);
9348 /* Clear the arrays */
9349 bzero ((char *) &set_in_loop->data, nregs * sizeof (int));
9350 bzero ((char *) &may_not_optimize->data, nregs * sizeof (char));
9351 bzero ((char *) &reg_single_usage->data, nregs * sizeof (rtx));
9353 count_loop_regs_set (loop_top ? loop_top : start, end,
9354 may_not_optimize, reg_single_usage,
9355 insn_count, nregs);
9357 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9359 VARRAY_CHAR (may_not_optimize, i) = 1;
9360 VARRAY_INT (set_in_loop, i) = 1;
9363 #ifdef AVOID_CCMODE_COPIES
9364 /* Don't try to move insns which set CC registers if we should not
9365 create CCmode register copies. */
9366 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
9367 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
9368 VARRAY_CHAR (may_not_optimize, i) = 1;
9369 #endif
9371 /* Set n_times_set for the new registers. */
9372 bcopy ((char *) (&set_in_loop->data.i[0] + old_nregs),
9373 (char *) (&n_times_set->data.i[0] + old_nregs),
9374 (nregs - old_nregs) * sizeof (int));
9378 /* Move MEMs into registers for the duration of the loop. SCAN_START
9379 is the first instruction in the loop (as it is executed). The
9380 other parameters are as for next_insn_in_loop. */
9382 static void
9383 load_mems (scan_start, end, loop_top, start)
9384 rtx scan_start;
9385 rtx end;
9386 rtx loop_top;
9387 rtx start;
9389 int maybe_never = 0;
9390 int i;
9391 rtx p;
9392 rtx label = NULL_RTX;
9393 rtx end_label;
9395 if (loop_mems_idx > 0)
9397 /* Nonzero if the next instruction may never be executed. */
9398 int next_maybe_never = 0;
9400 /* Check to see if it's possible that some instructions in the
9401 loop are never executed. */
9402 for (p = next_insn_in_loop (scan_start, scan_start, end, loop_top);
9403 p != NULL_RTX && !maybe_never;
9404 p = next_insn_in_loop (p, scan_start, end, loop_top))
9406 if (GET_CODE (p) == CODE_LABEL)
9407 maybe_never = 1;
9408 else if (GET_CODE (p) == JUMP_INSN
9409 /* If we enter the loop in the middle, and scan
9410 around to the beginning, don't set maybe_never
9411 for that. This must be an unconditional jump,
9412 otherwise the code at the top of the loop might
9413 never be executed. Unconditional jumps are
9414 followed a by barrier then loop end. */
9415 && ! (GET_CODE (p) == JUMP_INSN
9416 && JUMP_LABEL (p) == loop_top
9417 && NEXT_INSN (NEXT_INSN (p)) == end
9418 && simplejump_p (p)))
9420 if (!condjump_p (p))
9421 /* Something complicated. */
9422 maybe_never = 1;
9423 else
9424 /* If there are any more instructions in the loop, they
9425 might not be reached. */
9426 next_maybe_never = 1;
9428 else if (next_maybe_never)
9429 maybe_never = 1;
9432 /* Actually move the MEMs. */
9433 for (i = 0; i < loop_mems_idx; ++i)
9435 int written = 0;
9436 rtx reg;
9437 rtx mem = loop_mems[i].mem;
9438 rtx mem_list_entry;
9440 if (MEM_VOLATILE_P (mem)
9441 || invariant_p (XEXP (mem, 0)) != 1)
9442 /* There's no telling whether or not MEM is modified. */
9443 loop_mems[i].optimize = 0;
9445 /* Go through the MEMs written to in the loop to see if this
9446 one is aliased by one of them. */
9447 mem_list_entry = loop_store_mems;
9448 while (mem_list_entry)
9450 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
9451 written = 1;
9452 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
9453 mem, rtx_varies_p))
9455 /* MEM is indeed aliased by this store. */
9456 loop_mems[i].optimize = 0;
9457 break;
9459 mem_list_entry = XEXP (mem_list_entry, 1);
9462 /* If this MEM is written to, we must be sure that there
9463 are no reads from another MEM that aliases this one. */
9464 if (loop_mems[i].optimize && written)
9466 int j;
9468 for (j = 0; j < loop_mems_idx; ++j)
9470 if (j == i)
9471 continue;
9472 else if (true_dependence (mem,
9473 VOIDmode,
9474 loop_mems[j].mem,
9475 rtx_varies_p))
9477 /* It's not safe to hoist loop_mems[i] out of
9478 the loop because writes to it might not be
9479 seen by reads from loop_mems[j]. */
9480 loop_mems[i].optimize = 0;
9481 break;
9486 if (maybe_never && may_trap_p (mem))
9487 /* We can't access the MEM outside the loop; it might
9488 cause a trap that wouldn't have happened otherwise. */
9489 loop_mems[i].optimize = 0;
9491 if (!loop_mems[i].optimize)
9492 /* We thought we were going to lift this MEM out of the
9493 loop, but later discovered that we could not. */
9494 continue;
9496 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
9497 order to keep scan_loop from moving stores to this MEM
9498 out of the loop just because this REG is neither a
9499 user-variable nor used in the loop test. */
9500 reg = gen_reg_rtx (GET_MODE (mem));
9501 REG_USERVAR_P (reg) = 1;
9502 loop_mems[i].reg = reg;
9504 /* Now, replace all references to the MEM with the
9505 corresponding pesudos. */
9506 for (p = next_insn_in_loop (scan_start, scan_start, end, loop_top);
9507 p != NULL_RTX;
9508 p = next_insn_in_loop (p, scan_start, end, loop_top))
9510 rtx_and_int ri;
9511 ri.r = p;
9512 ri.i = i;
9513 for_each_rtx (&p, replace_loop_mem, &ri);
9516 if (!apply_change_group ())
9517 /* We couldn't replace all occurrences of the MEM. */
9518 loop_mems[i].optimize = 0;
9519 else
9521 rtx set;
9523 /* Load the memory immediately before START, which is
9524 the NOTE_LOOP_BEG. */
9525 set = gen_move_insn (reg, mem);
9526 emit_insn_before (set, start);
9528 if (written)
9530 if (label == NULL_RTX)
9532 /* We must compute the former
9533 right-after-the-end label before we insert
9534 the new one. */
9535 end_label = next_label (end);
9536 label = gen_label_rtx ();
9537 emit_label_after (label, end);
9540 /* Store the memory immediately after END, which is
9541 the NOTE_LOOP_END. */
9542 set = gen_move_insn (copy_rtx (mem), reg);
9543 emit_insn_after (set, label);
9546 if (loop_dump_stream)
9548 fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
9549 REGNO (reg), (written ? "r/w" : "r/o"));
9550 print_rtl (loop_dump_stream, mem);
9551 fputc ('\n', loop_dump_stream);
9557 if (label != NULL_RTX)
9559 /* Now, we need to replace all references to the previous exit
9560 label with the new one. */
9561 rtx_pair rr;
9562 rr.r1 = end_label;
9563 rr.r2 = label;
9565 for (p = start; p != end; p = NEXT_INSN (p))
9567 for_each_rtx (&p, replace_label, &rr);
9569 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
9570 field. This is not handled by for_each_rtx because it doesn't
9571 handle unprinted ('0') fields. We need to update JUMP_LABEL
9572 because the immediately following unroll pass will use it.
9573 replace_label would not work anyways, because that only handles
9574 LABEL_REFs. */
9575 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == end_label)
9576 JUMP_LABEL (p) = label;
9581 /* Replace MEM with its associated pseudo register. This function is
9582 called from load_mems via for_each_rtx. DATA is actually an
9583 rtx_and_int * describing the instruction currently being scanned
9584 and the MEM we are currently replacing. */
9586 static int
9587 replace_loop_mem (mem, data)
9588 rtx *mem;
9589 void *data;
9591 rtx_and_int *ri;
9592 rtx insn;
9593 int i;
9594 rtx m = *mem;
9596 if (m == NULL_RTX)
9597 return 0;
9599 switch (GET_CODE (m))
9601 case MEM:
9602 break;
9604 case CONST_DOUBLE:
9605 /* We're not interested in the MEM associated with a
9606 CONST_DOUBLE, so there's no need to traverse into one. */
9607 return -1;
9609 default:
9610 /* This is not a MEM. */
9611 return 0;
9614 ri = (rtx_and_int*) data;
9615 i = ri->i;
9617 if (!rtx_equal_p (loop_mems[i].mem, m))
9618 /* This is not the MEM we are currently replacing. */
9619 return 0;
9621 insn = ri->r;
9623 /* Actually replace the MEM. */
9624 validate_change (insn, mem, loop_mems[i].reg, 1);
9626 return 0;
9629 /* Replace occurrences of the old exit label for the loop with the new
9630 one. DATA is an rtx_pair containing the old and new labels,
9631 respectively. */
9633 static int
9634 replace_label (x, data)
9635 rtx *x;
9636 void *data;
9638 rtx l = *x;
9639 rtx old_label = ((rtx_pair*) data)->r1;
9640 rtx new_label = ((rtx_pair*) data)->r2;
9642 if (l == NULL_RTX)
9643 return 0;
9645 if (GET_CODE (l) != LABEL_REF)
9646 return 0;
9648 if (XEXP (l, 0) != old_label)
9649 return 0;
9651 XEXP (l, 0) = new_label;
9652 ++LABEL_NUSES (new_label);
9653 --LABEL_NUSES (old_label);
9655 return 0;