* pa.h (CPP_SPEC): Add whitespace after -D__STDC_EXT__.
[official-gcc.git] / gcc / loop.c
blob066ef931111dc3a40eb6c70170da09ebf0f4a0be
1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This is the loop optimization pass of the compiler.
24 It finds invariant computations within loops and moves them
25 to the beginning of the loop. Then it identifies basic and
26 general induction variables. Strength reduction is applied to the general
27 induction variables, and induction variable elimination is applied to
28 the basic induction variables.
30 It also finds cases where
31 a register is set within the loop by zero-extending a narrower value
32 and changes these to zero the entire register once before the loop
33 and merely copy the low part within the loop.
35 Most of the complexity is in heuristics to decide when it is worth
36 while to do these things. */
38 #include "config.h"
39 #include "system.h"
40 #include "rtl.h"
41 #include "tm_p.h"
42 #include "obstack.h"
43 #include "function.h"
44 #include "expr.h"
45 #include "hard-reg-set.h"
46 #include "basic-block.h"
47 #include "insn-config.h"
48 #include "insn-flags.h"
49 #include "regs.h"
50 #include "recog.h"
51 #include "flags.h"
52 #include "real.h"
53 #include "loop.h"
54 #include "cselib.h"
55 #include "except.h"
56 #include "toplev.h"
58 /* Vector mapping INSN_UIDs to luids.
59 The luids are like uids but increase monotonically always.
60 We use them to see whether a jump comes from outside a given loop. */
62 int *uid_luid;
64 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
65 number the insn is contained in. */
67 struct loop **uid_loop;
69 /* 1 + largest uid of any insn. */
71 int max_uid_for_loop;
73 /* 1 + luid of last insn. */
75 static int max_luid;
77 /* Number of loops detected in current function. Used as index to the
78 next few tables. */
80 static int max_loop_num;
82 /* Indexed by register number, contains the number of times the reg
83 is set during the loop being scanned.
84 During code motion, a negative value indicates a reg that has been
85 made a candidate; in particular -2 means that it is an candidate that
86 we know is equal to a constant and -1 means that it is an candidate
87 not known equal to a constant.
88 After code motion, regs moved have 0 (which is accurate now)
89 while the failed candidates have the original number of times set.
91 Therefore, at all times, == 0 indicates an invariant register;
92 < 0 a conditionally invariant one. */
94 static varray_type set_in_loop;
96 /* Original value of set_in_loop; same except that this value
97 is not set negative for a reg whose sets have been made candidates
98 and not set to 0 for a reg that is moved. */
100 static varray_type n_times_set;
102 /* Index by register number, 1 indicates that the register
103 cannot be moved or strength reduced. */
105 static varray_type may_not_optimize;
107 /* Contains the insn in which a register was used if it was used
108 exactly once; contains const0_rtx if it was used more than once. */
110 static varray_type reg_single_usage;
112 /* Nonzero means reg N has already been moved out of one loop.
113 This reduces the desire to move it out of another. */
115 static char *moved_once;
117 /* List of MEMs that are stored in this loop. */
119 static rtx loop_store_mems;
121 /* The insn where the first of these was found. */
122 static rtx first_loop_store_insn;
124 typedef struct loop_mem_info {
125 rtx mem; /* The MEM itself. */
126 rtx reg; /* Corresponding pseudo, if any. */
127 int optimize; /* Nonzero if we can optimize access to this MEM. */
128 } loop_mem_info;
130 /* Array of MEMs that are used (read or written) in this loop, but
131 cannot be aliased by anything in this loop, except perhaps
132 themselves. In other words, if loop_mems[i] is altered during the
133 loop, it is altered by an expression that is rtx_equal_p to it. */
135 static loop_mem_info *loop_mems;
137 /* The index of the next available slot in LOOP_MEMS. */
139 static int loop_mems_idx;
141 /* The number of elements allocated in LOOP_MEMs. */
143 static int loop_mems_allocated;
145 /* Nonzero if we don't know what MEMs were changed in the current
146 loop. This happens if the loop contains a call (in which case
147 `loop_info->has_call' will also be set) or if we store into more
148 than NUM_STORES MEMs. */
150 static int unknown_address_altered;
152 /* The above doesn't count any readonly memory locations that are stored.
153 This does. */
155 static int unknown_constant_address_altered;
157 /* Count of movable (i.e. invariant) instructions discovered in the loop. */
158 static int num_movables;
160 /* Count of memory write instructions discovered in the loop. */
161 static int num_mem_sets;
163 /* Bound on pseudo register number before loop optimization.
164 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
165 unsigned int max_reg_before_loop;
167 /* The value to pass to the next call of reg_scan_update. */
168 static int loop_max_reg;
170 /* This obstack is used in product_cheap_p to allocate its rtl. It
171 may call gen_reg_rtx which, in turn, may reallocate regno_reg_rtx.
172 If we used the same obstack that it did, we would be deallocating
173 that array. */
175 static struct obstack temp_obstack;
177 /* This is where the pointer to the obstack being used for RTL is stored. */
179 extern struct obstack *rtl_obstack;
181 #define obstack_chunk_alloc xmalloc
182 #define obstack_chunk_free free
184 /* During the analysis of a loop, a chain of `struct movable's
185 is made to record all the movable insns found.
186 Then the entire chain can be scanned to decide which to move. */
188 struct movable
190 rtx insn; /* A movable insn */
191 rtx set_src; /* The expression this reg is set from. */
192 rtx set_dest; /* The destination of this SET. */
193 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
194 of any registers used within the LIBCALL. */
195 int consec; /* Number of consecutive following insns
196 that must be moved with this one. */
197 unsigned int regno; /* The register it sets */
198 short lifetime; /* lifetime of that register;
199 may be adjusted when matching movables
200 that load the same value are found. */
201 short savings; /* Number of insns we can move for this reg,
202 including other movables that force this
203 or match this one. */
204 unsigned int cond : 1; /* 1 if only conditionally movable */
205 unsigned int force : 1; /* 1 means MUST move this insn */
206 unsigned int global : 1; /* 1 means reg is live outside this loop */
207 /* If PARTIAL is 1, GLOBAL means something different:
208 that the reg is live outside the range from where it is set
209 to the following label. */
210 unsigned int done : 1; /* 1 inhibits further processing of this */
212 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
213 In particular, moving it does not make it
214 invariant. */
215 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
216 load SRC, rather than copying INSN. */
217 unsigned int move_insn_first:1;/* Same as above, if this is necessary for the
218 first insn of a consecutive sets group. */
219 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
220 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
221 that we should avoid changing when clearing
222 the rest of the reg. */
223 struct movable *match; /* First entry for same value */
224 struct movable *forces; /* An insn that must be moved if this is */
225 struct movable *next;
228 static struct movable *the_movables;
230 FILE *loop_dump_stream;
232 /* Forward declarations. */
234 static void verify_dominator PARAMS ((struct loop *));
235 static void find_and_verify_loops PARAMS ((rtx, struct loops *));
236 static void mark_loop_jump PARAMS ((rtx, struct loop *));
237 static void prescan_loop PARAMS ((struct loop *));
238 static int reg_in_basic_block_p PARAMS ((rtx, rtx));
239 static int consec_sets_invariant_p PARAMS ((const struct loop *,
240 rtx, int, rtx));
241 static int labels_in_range_p PARAMS ((rtx, int));
242 static void count_one_set PARAMS ((rtx, rtx, varray_type, rtx *));
244 static void count_loop_regs_set PARAMS ((rtx, rtx, varray_type, varray_type,
245 int *, int));
246 static void note_addr_stored PARAMS ((rtx, rtx, void *));
247 static void note_set_pseudo_multiple_uses PARAMS ((rtx, rtx, void *));
248 static int loop_reg_used_before_p PARAMS ((const struct loop *, rtx, rtx));
249 static void scan_loop PARAMS ((struct loop*, int));
250 #if 0
251 static void replace_call_address PARAMS ((rtx, rtx, rtx));
252 #endif
253 static rtx skip_consec_insns PARAMS ((rtx, int));
254 static int libcall_benefit PARAMS ((rtx));
255 static void ignore_some_movables PARAMS ((struct movable *));
256 static void force_movables PARAMS ((struct movable *));
257 static void combine_movables PARAMS ((struct movable *, int));
258 static int regs_match_p PARAMS ((rtx, rtx, struct movable *));
259 static int rtx_equal_for_loop_p PARAMS ((rtx, rtx, struct movable *));
260 static void add_label_notes PARAMS ((rtx, rtx));
261 static void move_movables PARAMS ((struct loop *loop, struct movable *,
262 int, int, int));
263 static int count_nonfixed_reads PARAMS ((const struct loop *, rtx));
264 static void strength_reduce PARAMS ((struct loop *, int, int));
265 static void find_single_use_in_loop PARAMS ((rtx, rtx, varray_type));
266 static int valid_initial_value_p PARAMS ((rtx, rtx, int, rtx));
267 static void find_mem_givs PARAMS ((const struct loop *, rtx, rtx, int, int));
268 static void record_biv PARAMS ((struct induction *, rtx, rtx, rtx, rtx, rtx *,
269 int, int));
270 static void check_final_value PARAMS ((const struct loop *,
271 struct induction *));
272 static void record_giv PARAMS ((const struct loop *, struct induction *,
273 rtx, rtx, rtx, rtx, rtx, int, enum g_types,
274 int, int, rtx *));
275 static void update_giv_derive PARAMS ((const struct loop *, rtx));
276 static int basic_induction_var PARAMS ((const struct loop *, rtx,
277 enum machine_mode, rtx, rtx,
278 rtx *, rtx *, rtx **));
279 static rtx simplify_giv_expr PARAMS ((const struct loop *, rtx, int *));
280 static int general_induction_var PARAMS ((const struct loop *loop, rtx, rtx *,
281 rtx *, rtx *, int, int *, enum machine_mode));
282 static int consec_sets_giv PARAMS ((const struct loop *, int, rtx,
283 rtx, rtx, rtx *, rtx *, rtx *));
284 static int check_dbra_loop PARAMS ((struct loop *, int));
285 static rtx express_from_1 PARAMS ((rtx, rtx, rtx));
286 static rtx combine_givs_p PARAMS ((struct induction *, struct induction *));
287 static void combine_givs PARAMS ((struct iv_class *));
288 struct recombine_givs_stats;
289 static int find_life_end PARAMS ((rtx, struct recombine_givs_stats *,
290 rtx, rtx));
291 static void recombine_givs PARAMS ((const struct loop *, struct iv_class *,
292 int));
293 static int product_cheap_p PARAMS ((rtx, rtx));
294 static int maybe_eliminate_biv PARAMS ((const struct loop *, struct iv_class *,
295 int, int, int));
296 static int maybe_eliminate_biv_1 PARAMS ((const struct loop *, rtx, rtx,
297 struct iv_class *, int, rtx));
298 static int last_use_this_basic_block PARAMS ((rtx, rtx));
299 static void record_initial PARAMS ((rtx, rtx, void *));
300 static void update_reg_last_use PARAMS ((rtx, rtx));
301 static rtx next_insn_in_loop PARAMS ((const struct loop *, rtx));
302 static void load_mems_and_recount_loop_regs_set PARAMS ((const struct loop*,
303 int *));
304 static void load_mems PARAMS ((const struct loop *));
305 static int insert_loop_mem PARAMS ((rtx *, void *));
306 static int replace_loop_mem PARAMS ((rtx *, void *));
307 static int replace_loop_reg PARAMS ((rtx *, void *));
308 static void note_reg_stored PARAMS ((rtx, rtx, void *));
309 static void try_copy_prop PARAMS ((const struct loop *, rtx, unsigned int));
310 static int replace_label PARAMS ((rtx *, void *));
311 static rtx check_insn_for_givs PARAMS((struct loop *, rtx, int, int));
312 static rtx check_insn_for_bivs PARAMS((struct loop *, rtx, int, int));
314 typedef struct rtx_and_int {
315 rtx r;
316 int i;
317 } rtx_and_int;
319 typedef struct rtx_pair {
320 rtx r1;
321 rtx r2;
322 } rtx_pair;
324 /* Nonzero iff INSN is between START and END, inclusive. */
325 #define INSN_IN_RANGE_P(INSN, START, END) \
326 (INSN_UID (INSN) < max_uid_for_loop \
327 && INSN_LUID (INSN) >= INSN_LUID (START) \
328 && INSN_LUID (INSN) <= INSN_LUID (END))
330 #ifdef HAVE_decrement_and_branch_on_count
331 /* Test whether BCT applicable and safe. */
332 static void insert_bct PARAMS ((struct loop *));
334 /* Auxiliary function that inserts the BCT pattern into the loop. */
335 static void instrument_loop_bct PARAMS ((rtx, rtx, rtx));
336 #endif /* HAVE_decrement_and_branch_on_count */
338 /* Indirect_jump_in_function is computed once per function. */
339 int indirect_jump_in_function = 0;
340 static int indirect_jump_in_function_p PARAMS ((rtx));
342 static int compute_luids PARAMS ((rtx, rtx, int));
344 static int biv_elimination_giv_has_0_offset PARAMS ((struct induction *,
345 struct induction *, rtx));
347 /* Relative gain of eliminating various kinds of operations. */
348 static int add_cost;
349 #if 0
350 static int shift_cost;
351 static int mult_cost;
352 #endif
354 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
355 copy the value of the strength reduced giv to its original register. */
356 static int copy_cost;
358 /* Cost of using a register, to normalize the benefits of a giv. */
359 static int reg_address_cost;
362 void
363 init_loop ()
365 char *free_point = (char *) oballoc (1);
366 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
368 add_cost = rtx_cost (gen_rtx_PLUS (word_mode, reg, reg), SET);
370 reg_address_cost = address_cost (reg, SImode);
372 /* We multiply by 2 to reconcile the difference in scale between
373 these two ways of computing costs. Otherwise the cost of a copy
374 will be far less than the cost of an add. */
376 copy_cost = 2 * 2;
378 /* Free the objects we just allocated. */
379 obfree (free_point);
381 /* Initialize the obstack used for rtl in product_cheap_p. */
382 gcc_obstack_init (&temp_obstack);
385 /* Compute the mapping from uids to luids.
386 LUIDs are numbers assigned to insns, like uids,
387 except that luids increase monotonically through the code.
388 Start at insn START and stop just before END. Assign LUIDs
389 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
390 static int
391 compute_luids (start, end, prev_luid)
392 rtx start, end;
393 int prev_luid;
395 int i;
396 rtx insn;
398 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
400 if (INSN_UID (insn) >= max_uid_for_loop)
401 continue;
402 /* Don't assign luids to line-number NOTEs, so that the distance in
403 luids between two insns is not affected by -g. */
404 if (GET_CODE (insn) != NOTE
405 || NOTE_LINE_NUMBER (insn) <= 0)
406 uid_luid[INSN_UID (insn)] = ++i;
407 else
408 /* Give a line number note the same luid as preceding insn. */
409 uid_luid[INSN_UID (insn)] = i;
411 return i + 1;
414 /* Entry point of this file. Perform loop optimization
415 on the current function. F is the first insn of the function
416 and DUMPFILE is a stream for output of a trace of actions taken
417 (or 0 if none should be output). */
419 void
420 loop_optimize (f, dumpfile, flags)
421 /* f is the first instruction of a chain of insns for one function */
422 rtx f;
423 FILE *dumpfile;
424 int flags;
426 register rtx insn;
427 register int i;
428 struct loops loops_data;
429 struct loops *loops = &loops_data;
430 struct loop_info *loops_info;
432 loop_dump_stream = dumpfile;
434 init_recog_no_volatile ();
436 max_reg_before_loop = max_reg_num ();
437 loop_max_reg = max_reg_before_loop;
439 regs_may_share = 0;
441 /* Count the number of loops. */
443 max_loop_num = 0;
444 for (insn = f; insn; insn = NEXT_INSN (insn))
446 if (GET_CODE (insn) == NOTE
447 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
448 max_loop_num++;
451 /* Don't waste time if no loops. */
452 if (max_loop_num == 0)
453 return;
455 loops->num = max_loop_num;
457 moved_once = (char *) xcalloc (max_reg_before_loop, sizeof (char));
459 /* Get size to use for tables indexed by uids.
460 Leave some space for labels allocated by find_and_verify_loops. */
461 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
463 uid_luid = (int *) xcalloc (max_uid_for_loop, sizeof (int));
464 uid_loop = (struct loop **) xcalloc (max_uid_for_loop,
465 sizeof (struct loop *));
467 /* Allocate storage for array of loops. */
468 loops->array = (struct loop *)
469 xcalloc (loops->num, sizeof (struct loop));
471 /* Find and process each loop.
472 First, find them, and record them in order of their beginnings. */
473 find_and_verify_loops (f, loops);
475 /* Allocate and initialize auxiliary loop information. */
476 loops_info = xcalloc (loops->num, sizeof (struct loop_info));
477 for (i = 0; i < loops->num; i++)
478 loops->array[i].aux = loops_info + i;
480 /* Now find all register lifetimes. This must be done after
481 find_and_verify_loops, because it might reorder the insns in the
482 function. */
483 reg_scan (f, max_reg_before_loop, 1);
485 /* This must occur after reg_scan so that registers created by gcse
486 will have entries in the register tables.
488 We could have added a call to reg_scan after gcse_main in toplev.c,
489 but moving this call to init_alias_analysis is more efficient. */
490 init_alias_analysis ();
492 /* See if we went too far. Note that get_max_uid already returns
493 one more that the maximum uid of all insn. */
494 if (get_max_uid () > max_uid_for_loop)
495 abort ();
496 /* Now reset it to the actual size we need. See above. */
497 max_uid_for_loop = get_max_uid ();
499 /* find_and_verify_loops has already called compute_luids, but it
500 might have rearranged code afterwards, so we need to recompute
501 the luids now. */
502 max_luid = compute_luids (f, NULL_RTX, 0);
504 /* Don't leave gaps in uid_luid for insns that have been
505 deleted. It is possible that the first or last insn
506 using some register has been deleted by cross-jumping.
507 Make sure that uid_luid for that former insn's uid
508 points to the general area where that insn used to be. */
509 for (i = 0; i < max_uid_for_loop; i++)
511 uid_luid[0] = uid_luid[i];
512 if (uid_luid[0] != 0)
513 break;
515 for (i = 0; i < max_uid_for_loop; i++)
516 if (uid_luid[i] == 0)
517 uid_luid[i] = uid_luid[i - 1];
519 /* Determine if the function has indirect jump. On some systems
520 this prevents low overhead loop instructions from being used. */
521 indirect_jump_in_function = indirect_jump_in_function_p (f);
523 /* Now scan the loops, last ones first, since this means inner ones are done
524 before outer ones. */
525 for (i = max_loop_num - 1; i >= 0; i--)
527 struct loop *loop = &loops->array[i];
529 if (! loop->invalid && loop->end)
530 scan_loop (loop, flags);
533 /* If there were lexical blocks inside the loop, they have been
534 replicated. We will now have more than one NOTE_INSN_BLOCK_BEG
535 and NOTE_INSN_BLOCK_END for each such block. We must duplicate
536 the BLOCKs as well. */
537 if (write_symbols != NO_DEBUG)
538 reorder_blocks ();
540 end_alias_analysis ();
542 /* Clean up. */
543 free (moved_once);
544 free (uid_luid);
545 free (uid_loop);
546 free (loops_info);
547 free (loops->array);
550 /* Returns the next insn, in execution order, after INSN. START and
551 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
552 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
553 insn-stream; it is used with loops that are entered near the
554 bottom. */
556 static rtx
557 next_insn_in_loop (loop, insn)
558 const struct loop *loop;
559 rtx insn;
561 insn = NEXT_INSN (insn);
563 if (insn == loop->end)
565 if (loop->top)
566 /* Go to the top of the loop, and continue there. */
567 insn = loop->top;
568 else
569 /* We're done. */
570 insn = NULL_RTX;
573 if (insn == loop->scan_start)
574 /* We're done. */
575 insn = NULL_RTX;
577 return insn;
580 /* Optimize one loop described by LOOP. */
582 /* ??? Could also move memory writes out of loops if the destination address
583 is invariant, the source is invariant, the memory write is not volatile,
584 and if we can prove that no read inside the loop can read this address
585 before the write occurs. If there is a read of this address after the
586 write, then we can also mark the memory read as invariant. */
588 static void
589 scan_loop (loop, flags)
590 struct loop *loop;
591 int flags;
593 register int i;
594 rtx loop_start = loop->start;
595 rtx loop_end = loop->end;
596 /* Additional information about the current loop being processed
597 that is used to compute the number of loop iterations for loop
598 unrolling and doloop optimization. */
599 struct loop_info *loop_info = LOOP_INFO (loop);
600 rtx p;
601 /* 1 if we are scanning insns that could be executed zero times. */
602 int maybe_never = 0;
603 /* 1 if we are scanning insns that might never be executed
604 due to a subroutine call which might exit before they are reached. */
605 int call_passed = 0;
606 /* Jump insn that enters the loop, or 0 if control drops in. */
607 rtx loop_entry_jump = 0;
608 /* Number of insns in the loop. */
609 int insn_count;
610 int in_libcall = 0;
611 int tem;
612 rtx temp, update_start, update_end;
613 /* The SET from an insn, if it is the only SET in the insn. */
614 rtx set, set1;
615 /* Chain describing insns movable in current loop. */
616 struct movable *movables = 0;
617 /* Last element in `movables' -- so we can add elements at the end. */
618 struct movable *last_movable = 0;
619 /* Ratio of extra register life span we can justify
620 for saving an instruction. More if loop doesn't call subroutines
621 since in that case saving an insn makes more difference
622 and more registers are available. */
623 int threshold;
624 /* Nonzero if we are scanning instructions in a sub-loop. */
625 int loop_depth = 0;
626 int nregs;
628 loop->top = 0;
630 /* Determine whether this loop starts with a jump down to a test at
631 the end. This will occur for a small number of loops with a test
632 that is too complex to duplicate in front of the loop.
634 We search for the first insn or label in the loop, skipping NOTEs.
635 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
636 (because we might have a loop executed only once that contains a
637 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
638 (in case we have a degenerate loop).
640 Note that if we mistakenly think that a loop is entered at the top
641 when, in fact, it is entered at the exit test, the only effect will be
642 slightly poorer optimization. Making the opposite error can generate
643 incorrect code. Since very few loops now start with a jump to the
644 exit test, the code here to detect that case is very conservative. */
646 for (p = NEXT_INSN (loop_start);
647 p != loop_end
648 && GET_CODE (p) != CODE_LABEL && GET_RTX_CLASS (GET_CODE (p)) != 'i'
649 && (GET_CODE (p) != NOTE
650 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
651 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
652 p = NEXT_INSN (p))
655 loop->scan_start = p;
657 /* Set up variables describing this loop. */
658 prescan_loop (loop);
659 threshold = (loop_info->has_call ? 1 : 2) * (1 + n_non_fixed_regs);
661 /* If loop has a jump before the first label,
662 the true entry is the target of that jump.
663 Start scan from there.
664 But record in LOOP->TOP the place where the end-test jumps
665 back to so we can scan that after the end of the loop. */
666 if (GET_CODE (p) == JUMP_INSN)
668 loop_entry_jump = p;
670 /* Loop entry must be unconditional jump (and not a RETURN) */
671 if (any_uncondjump_p (p)
672 && JUMP_LABEL (p) != 0
673 /* Check to see whether the jump actually
674 jumps out of the loop (meaning it's no loop).
675 This case can happen for things like
676 do {..} while (0). If this label was generated previously
677 by loop, we can't tell anything about it and have to reject
678 the loop. */
679 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, loop_end))
681 loop->top = next_label (loop->scan_start);
682 loop->scan_start = JUMP_LABEL (p);
686 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
687 as required by loop_reg_used_before_p. So skip such loops. (This
688 test may never be true, but it's best to play it safe.)
690 Also, skip loops where we do not start scanning at a label. This
691 test also rejects loops starting with a JUMP_INSN that failed the
692 test above. */
694 if (INSN_UID (loop->scan_start) >= max_uid_for_loop
695 || GET_CODE (loop->scan_start) != CODE_LABEL)
697 if (loop_dump_stream)
698 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
699 INSN_UID (loop_start), INSN_UID (loop_end));
700 return;
703 /* Count number of times each reg is set during this loop.
704 Set VARRAY_CHAR (may_not_optimize, I) if it is not safe to move out
705 the setting of register I. Set VARRAY_RTX (reg_single_usage, I). */
707 /* Allocate extra space for REGS that might be created by
708 load_mems. We allocate a little extra slop as well, in the hopes
709 that even after the moving of movables creates some new registers
710 we won't have to reallocate these arrays. However, we do grow
711 the arrays, if necessary, in load_mems_recount_loop_regs_set. */
712 nregs = max_reg_num () + loop_mems_idx + 16;
713 VARRAY_INT_INIT (set_in_loop, nregs, "set_in_loop");
714 VARRAY_INT_INIT (n_times_set, nregs, "n_times_set");
715 VARRAY_CHAR_INIT (may_not_optimize, nregs, "may_not_optimize");
716 VARRAY_RTX_INIT (reg_single_usage, nregs, "reg_single_usage");
718 count_loop_regs_set (loop->top ? loop->top : loop->start, loop->end,
719 may_not_optimize, reg_single_usage, &insn_count, nregs);
721 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
723 VARRAY_CHAR (may_not_optimize, i) = 1;
724 VARRAY_INT (set_in_loop, i) = 1;
727 #ifdef AVOID_CCMODE_COPIES
728 /* Don't try to move insns which set CC registers if we should not
729 create CCmode register copies. */
730 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
731 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
732 VARRAY_CHAR (may_not_optimize, i) = 1;
733 #endif
735 bcopy ((char *) &set_in_loop->data,
736 (char *) &n_times_set->data, nregs * sizeof (int));
738 if (loop_dump_stream)
740 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
741 INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
742 if (loop->cont)
743 fprintf (loop_dump_stream, "Continue at insn %d.\n",
744 INSN_UID (loop->cont));
747 /* Scan through the loop finding insns that are safe to move.
748 Set set_in_loop negative for the reg being set, so that
749 this reg will be considered invariant for subsequent insns.
750 We consider whether subsequent insns use the reg
751 in deciding whether it is worth actually moving.
753 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
754 and therefore it is possible that the insns we are scanning
755 would never be executed. At such times, we must make sure
756 that it is safe to execute the insn once instead of zero times.
757 When MAYBE_NEVER is 0, all insns will be executed at least once
758 so that is not a problem. */
760 for (p = next_insn_in_loop (loop, loop->scan_start);
761 p != NULL_RTX;
762 p = next_insn_in_loop (loop, p))
764 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
765 && find_reg_note (p, REG_LIBCALL, NULL_RTX))
766 in_libcall = 1;
767 else if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
768 && find_reg_note (p, REG_RETVAL, NULL_RTX))
769 in_libcall = 0;
771 if (GET_CODE (p) == INSN
772 && (set = single_set (p))
773 && GET_CODE (SET_DEST (set)) == REG
774 && ! VARRAY_CHAR (may_not_optimize, REGNO (SET_DEST (set))))
776 int tem1 = 0;
777 int tem2 = 0;
778 int move_insn = 0;
779 rtx src = SET_SRC (set);
780 rtx dependencies = 0;
782 /* Figure out what to use as a source of this insn. If a REG_EQUIV
783 note is given or if a REG_EQUAL note with a constant operand is
784 specified, use it as the source and mark that we should move
785 this insn by calling emit_move_insn rather that duplicating the
786 insn.
788 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
789 is present. */
790 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
791 if (temp)
792 src = XEXP (temp, 0), move_insn = 1;
793 else
795 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
796 if (temp && CONSTANT_P (XEXP (temp, 0)))
797 src = XEXP (temp, 0), move_insn = 1;
798 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
800 src = XEXP (temp, 0);
801 /* A libcall block can use regs that don't appear in
802 the equivalent expression. To move the libcall,
803 we must move those regs too. */
804 dependencies = libcall_other_reg (p, src);
808 /* Don't try to optimize a register that was made
809 by loop-optimization for an inner loop.
810 We don't know its life-span, so we can't compute the benefit. */
811 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
813 else if (/* The register is used in basic blocks other
814 than the one where it is set (meaning that
815 something after this point in the loop might
816 depend on its value before the set). */
817 ! reg_in_basic_block_p (p, SET_DEST (set))
818 /* And the set is not guaranteed to be executed one
819 the loop starts, or the value before the set is
820 needed before the set occurs...
822 ??? Note we have quadratic behaviour here, mitigated
823 by the fact that the previous test will often fail for
824 large loops. Rather than re-scanning the entire loop
825 each time for register usage, we should build tables
826 of the register usage and use them here instead. */
827 && (maybe_never
828 || loop_reg_used_before_p (loop, set, p)))
829 /* It is unsafe to move the set.
831 This code used to consider it OK to move a set of a variable
832 which was not created by the user and not used in an exit test.
833 That behavior is incorrect and was removed. */
835 else if ((tem = loop_invariant_p (loop, src))
836 && (dependencies == 0
837 || (tem2 = loop_invariant_p (loop, dependencies)) != 0)
838 && (VARRAY_INT (set_in_loop,
839 REGNO (SET_DEST (set))) == 1
840 || (tem1
841 = consec_sets_invariant_p
842 (loop, SET_DEST (set),
843 VARRAY_INT (set_in_loop, REGNO (SET_DEST (set))),
844 p)))
845 /* If the insn can cause a trap (such as divide by zero),
846 can't move it unless it's guaranteed to be executed
847 once loop is entered. Even a function call might
848 prevent the trap insn from being reached
849 (since it might exit!) */
850 && ! ((maybe_never || call_passed)
851 && may_trap_p (src)))
853 register struct movable *m;
854 register int regno = REGNO (SET_DEST (set));
856 /* A potential lossage is where we have a case where two insns
857 can be combined as long as they are both in the loop, but
858 we move one of them outside the loop. For large loops,
859 this can lose. The most common case of this is the address
860 of a function being called.
862 Therefore, if this register is marked as being used exactly
863 once if we are in a loop with calls (a "large loop"), see if
864 we can replace the usage of this register with the source
865 of this SET. If we can, delete this insn.
867 Don't do this if P has a REG_RETVAL note or if we have
868 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
870 if (loop_info->has_call
871 && VARRAY_RTX (reg_single_usage, regno) != 0
872 && VARRAY_RTX (reg_single_usage, regno) != const0_rtx
873 && REGNO_FIRST_UID (regno) == INSN_UID (p)
874 && (REGNO_LAST_UID (regno)
875 == INSN_UID (VARRAY_RTX (reg_single_usage, regno)))
876 && VARRAY_INT (set_in_loop, regno) == 1
877 && ! side_effects_p (SET_SRC (set))
878 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
879 && (! SMALL_REGISTER_CLASSES
880 || (! (GET_CODE (SET_SRC (set)) == REG
881 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
882 /* This test is not redundant; SET_SRC (set) might be
883 a call-clobbered register and the life of REGNO
884 might span a call. */
885 && ! modified_between_p (SET_SRC (set), p,
886 VARRAY_RTX
887 (reg_single_usage, regno))
888 && no_labels_between_p (p, VARRAY_RTX (reg_single_usage, regno))
889 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
890 VARRAY_RTX
891 (reg_single_usage, regno)))
893 /* Replace any usage in a REG_EQUAL note. Must copy the
894 new source, so that we don't get rtx sharing between the
895 SET_SOURCE and REG_NOTES of insn p. */
896 REG_NOTES (VARRAY_RTX (reg_single_usage, regno))
897 = replace_rtx (REG_NOTES (VARRAY_RTX
898 (reg_single_usage, regno)),
899 SET_DEST (set), copy_rtx (SET_SRC (set)));
901 PUT_CODE (p, NOTE);
902 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
903 NOTE_SOURCE_FILE (p) = 0;
904 VARRAY_INT (set_in_loop, regno) = 0;
905 continue;
908 m = (struct movable *) alloca (sizeof (struct movable));
909 m->next = 0;
910 m->insn = p;
911 m->set_src = src;
912 m->dependencies = dependencies;
913 m->set_dest = SET_DEST (set);
914 m->force = 0;
915 m->consec = VARRAY_INT (set_in_loop,
916 REGNO (SET_DEST (set))) - 1;
917 m->done = 0;
918 m->forces = 0;
919 m->partial = 0;
920 m->move_insn = move_insn;
921 m->move_insn_first = 0;
922 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
923 m->savemode = VOIDmode;
924 m->regno = regno;
925 /* Set M->cond if either loop_invariant_p
926 or consec_sets_invariant_p returned 2
927 (only conditionally invariant). */
928 m->cond = ((tem | tem1 | tem2) > 1);
929 m->global = (uid_luid[REGNO_LAST_UID (regno)]
930 > INSN_LUID (loop_end)
931 || uid_luid[REGNO_FIRST_UID (regno)] < INSN_LUID (loop_start));
932 m->match = 0;
933 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
934 - uid_luid[REGNO_FIRST_UID (regno)]);
935 m->savings = VARRAY_INT (n_times_set, regno);
936 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
937 m->savings += libcall_benefit (p);
938 VARRAY_INT (set_in_loop, regno) = move_insn ? -2 : -1;
939 /* Add M to the end of the chain MOVABLES. */
940 if (movables == 0)
941 movables = m;
942 else
943 last_movable->next = m;
944 last_movable = m;
946 if (m->consec > 0)
948 /* It is possible for the first instruction to have a
949 REG_EQUAL note but a non-invariant SET_SRC, so we must
950 remember the status of the first instruction in case
951 the last instruction doesn't have a REG_EQUAL note. */
952 m->move_insn_first = m->move_insn;
954 /* Skip this insn, not checking REG_LIBCALL notes. */
955 p = next_nonnote_insn (p);
956 /* Skip the consecutive insns, if there are any. */
957 p = skip_consec_insns (p, m->consec);
958 /* Back up to the last insn of the consecutive group. */
959 p = prev_nonnote_insn (p);
961 /* We must now reset m->move_insn, m->is_equiv, and possibly
962 m->set_src to correspond to the effects of all the
963 insns. */
964 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
965 if (temp)
966 m->set_src = XEXP (temp, 0), m->move_insn = 1;
967 else
969 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
970 if (temp && CONSTANT_P (XEXP (temp, 0)))
971 m->set_src = XEXP (temp, 0), m->move_insn = 1;
972 else
973 m->move_insn = 0;
976 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
979 /* If this register is always set within a STRICT_LOW_PART
980 or set to zero, then its high bytes are constant.
981 So clear them outside the loop and within the loop
982 just load the low bytes.
983 We must check that the machine has an instruction to do so.
984 Also, if the value loaded into the register
985 depends on the same register, this cannot be done. */
986 else if (SET_SRC (set) == const0_rtx
987 && GET_CODE (NEXT_INSN (p)) == INSN
988 && (set1 = single_set (NEXT_INSN (p)))
989 && GET_CODE (set1) == SET
990 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
991 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
992 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
993 == SET_DEST (set))
994 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
996 register int regno = REGNO (SET_DEST (set));
997 if (VARRAY_INT (set_in_loop, regno) == 2)
999 register struct movable *m;
1000 m = (struct movable *) alloca (sizeof (struct movable));
1001 m->next = 0;
1002 m->insn = p;
1003 m->set_dest = SET_DEST (set);
1004 m->dependencies = 0;
1005 m->force = 0;
1006 m->consec = 0;
1007 m->done = 0;
1008 m->forces = 0;
1009 m->move_insn = 0;
1010 m->move_insn_first = 0;
1011 m->partial = 1;
1012 /* If the insn may not be executed on some cycles,
1013 we can't clear the whole reg; clear just high part.
1014 Not even if the reg is used only within this loop.
1015 Consider this:
1016 while (1)
1017 while (s != t) {
1018 if (foo ()) x = *s;
1019 use (x);
1021 Clearing x before the inner loop could clobber a value
1022 being saved from the last time around the outer loop.
1023 However, if the reg is not used outside this loop
1024 and all uses of the register are in the same
1025 basic block as the store, there is no problem.
1027 If this insn was made by loop, we don't know its
1028 INSN_LUID and hence must make a conservative
1029 assumption. */
1030 m->global = (INSN_UID (p) >= max_uid_for_loop
1031 || (uid_luid[REGNO_LAST_UID (regno)]
1032 > INSN_LUID (loop_end))
1033 || (uid_luid[REGNO_FIRST_UID (regno)]
1034 < INSN_LUID (p))
1035 || (labels_in_range_p
1036 (p, uid_luid[REGNO_FIRST_UID (regno)])));
1037 if (maybe_never && m->global)
1038 m->savemode = GET_MODE (SET_SRC (set1));
1039 else
1040 m->savemode = VOIDmode;
1041 m->regno = regno;
1042 m->cond = 0;
1043 m->match = 0;
1044 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
1045 - uid_luid[REGNO_FIRST_UID (regno)]);
1046 m->savings = 1;
1047 VARRAY_INT (set_in_loop, regno) = -1;
1048 /* Add M to the end of the chain MOVABLES. */
1049 if (movables == 0)
1050 movables = m;
1051 else
1052 last_movable->next = m;
1053 last_movable = m;
1057 /* Past a call insn, we get to insns which might not be executed
1058 because the call might exit. This matters for insns that trap.
1059 Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
1060 so they don't count. */
1061 else if (GET_CODE (p) == CALL_INSN && ! in_libcall)
1062 call_passed = 1;
1063 /* Past a label or a jump, we get to insns for which we
1064 can't count on whether or how many times they will be
1065 executed during each iteration. Therefore, we can
1066 only move out sets of trivial variables
1067 (those not used after the loop). */
1068 /* Similar code appears twice in strength_reduce. */
1069 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1070 /* If we enter the loop in the middle, and scan around to the
1071 beginning, don't set maybe_never for that. This must be an
1072 unconditional jump, otherwise the code at the top of the
1073 loop might never be executed. Unconditional jumps are
1074 followed a by barrier then loop end. */
1075 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop->top
1076 && NEXT_INSN (NEXT_INSN (p)) == loop_end
1077 && any_uncondjump_p (p)))
1078 maybe_never = 1;
1079 else if (GET_CODE (p) == NOTE)
1081 /* At the virtual top of a converted loop, insns are again known to
1082 be executed: logically, the loop begins here even though the exit
1083 code has been duplicated. */
1084 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1085 maybe_never = call_passed = 0;
1086 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1087 loop_depth++;
1088 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1089 loop_depth--;
1093 /* If one movable subsumes another, ignore that other. */
1095 ignore_some_movables (movables);
1097 /* For each movable insn, see if the reg that it loads
1098 leads when it dies right into another conditionally movable insn.
1099 If so, record that the second insn "forces" the first one,
1100 since the second can be moved only if the first is. */
1102 force_movables (movables);
1104 /* See if there are multiple movable insns that load the same value.
1105 If there are, make all but the first point at the first one
1106 through the `match' field, and add the priorities of them
1107 all together as the priority of the first. */
1109 combine_movables (movables, nregs);
1111 /* Now consider each movable insn to decide whether it is worth moving.
1112 Store 0 in set_in_loop for each reg that is moved.
1114 Generally this increases code size, so do not move moveables when
1115 optimizing for code size. */
1117 if (! optimize_size)
1118 move_movables (loop, movables, threshold, insn_count, nregs);
1120 /* Now candidates that still are negative are those not moved.
1121 Change set_in_loop to indicate that those are not actually invariant. */
1122 for (i = 0; i < nregs; i++)
1123 if (VARRAY_INT (set_in_loop, i) < 0)
1124 VARRAY_INT (set_in_loop, i) = VARRAY_INT (n_times_set, i);
1126 /* Now that we've moved some things out of the loop, we might be able to
1127 hoist even more memory references. */
1128 load_mems_and_recount_loop_regs_set (loop, &insn_count);
1130 for (update_start = loop_start;
1131 PREV_INSN (update_start)
1132 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1133 update_start = PREV_INSN (update_start))
1135 update_end = NEXT_INSN (loop_end);
1137 reg_scan_update (update_start, update_end, loop_max_reg);
1138 loop_max_reg = max_reg_num ();
1140 if (flag_strength_reduce)
1142 the_movables = movables;
1143 strength_reduce (loop, insn_count, flags);
1145 reg_scan_update (update_start, update_end, loop_max_reg);
1146 loop_max_reg = max_reg_num ();
1149 VARRAY_FREE (reg_single_usage);
1150 VARRAY_FREE (set_in_loop);
1151 VARRAY_FREE (n_times_set);
1152 VARRAY_FREE (may_not_optimize);
1155 /* Add elements to *OUTPUT to record all the pseudo-regs
1156 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1158 void
1159 record_excess_regs (in_this, not_in_this, output)
1160 rtx in_this, not_in_this;
1161 rtx *output;
1163 enum rtx_code code;
1164 const char *fmt;
1165 int i;
1167 code = GET_CODE (in_this);
1169 switch (code)
1171 case PC:
1172 case CC0:
1173 case CONST_INT:
1174 case CONST_DOUBLE:
1175 case CONST:
1176 case SYMBOL_REF:
1177 case LABEL_REF:
1178 return;
1180 case REG:
1181 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1182 && ! reg_mentioned_p (in_this, not_in_this))
1183 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1184 return;
1186 default:
1187 break;
1190 fmt = GET_RTX_FORMAT (code);
1191 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1193 int j;
1195 switch (fmt[i])
1197 case 'E':
1198 for (j = 0; j < XVECLEN (in_this, i); j++)
1199 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1200 break;
1202 case 'e':
1203 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1204 break;
1209 /* Check what regs are referred to in the libcall block ending with INSN,
1210 aside from those mentioned in the equivalent value.
1211 If there are none, return 0.
1212 If there are one or more, return an EXPR_LIST containing all of them. */
1215 libcall_other_reg (insn, equiv)
1216 rtx insn, equiv;
1218 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1219 rtx p = XEXP (note, 0);
1220 rtx output = 0;
1222 /* First, find all the regs used in the libcall block
1223 that are not mentioned as inputs to the result. */
1225 while (p != insn)
1227 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1228 || GET_CODE (p) == CALL_INSN)
1229 record_excess_regs (PATTERN (p), equiv, &output);
1230 p = NEXT_INSN (p);
1233 return output;
1236 /* Return 1 if all uses of REG
1237 are between INSN and the end of the basic block. */
1239 static int
1240 reg_in_basic_block_p (insn, reg)
1241 rtx insn, reg;
1243 int regno = REGNO (reg);
1244 rtx p;
1246 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1247 return 0;
1249 /* Search this basic block for the already recorded last use of the reg. */
1250 for (p = insn; p; p = NEXT_INSN (p))
1252 switch (GET_CODE (p))
1254 case NOTE:
1255 break;
1257 case INSN:
1258 case CALL_INSN:
1259 /* Ordinary insn: if this is the last use, we win. */
1260 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1261 return 1;
1262 break;
1264 case JUMP_INSN:
1265 /* Jump insn: if this is the last use, we win. */
1266 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1267 return 1;
1268 /* Otherwise, it's the end of the basic block, so we lose. */
1269 return 0;
1271 case CODE_LABEL:
1272 case BARRIER:
1273 /* It's the end of the basic block, so we lose. */
1274 return 0;
1276 default:
1277 break;
1281 /* The "last use" that was recorded can't be found after the first
1282 use. This can happen when the last use was deleted while
1283 processing an inner loop, this inner loop was then completely
1284 unrolled, and the outer loop is always exited after the inner loop,
1285 so that everything after the first use becomes a single basic block. */
1286 return 1;
1289 /* Compute the benefit of eliminating the insns in the block whose
1290 last insn is LAST. This may be a group of insns used to compute a
1291 value directly or can contain a library call. */
1293 static int
1294 libcall_benefit (last)
1295 rtx last;
1297 rtx insn;
1298 int benefit = 0;
1300 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1301 insn != last; insn = NEXT_INSN (insn))
1303 if (GET_CODE (insn) == CALL_INSN)
1304 benefit += 10; /* Assume at least this many insns in a library
1305 routine. */
1306 else if (GET_CODE (insn) == INSN
1307 && GET_CODE (PATTERN (insn)) != USE
1308 && GET_CODE (PATTERN (insn)) != CLOBBER)
1309 benefit++;
1312 return benefit;
1315 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1317 static rtx
1318 skip_consec_insns (insn, count)
1319 rtx insn;
1320 int count;
1322 for (; count > 0; count--)
1324 rtx temp;
1326 /* If first insn of libcall sequence, skip to end. */
1327 /* Do this at start of loop, since INSN is guaranteed to
1328 be an insn here. */
1329 if (GET_CODE (insn) != NOTE
1330 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1331 insn = XEXP (temp, 0);
1333 do insn = NEXT_INSN (insn);
1334 while (GET_CODE (insn) == NOTE);
1337 return insn;
1340 /* Ignore any movable whose insn falls within a libcall
1341 which is part of another movable.
1342 We make use of the fact that the movable for the libcall value
1343 was made later and so appears later on the chain. */
1345 static void
1346 ignore_some_movables (movables)
1347 struct movable *movables;
1349 register struct movable *m, *m1;
1351 for (m = movables; m; m = m->next)
1353 /* Is this a movable for the value of a libcall? */
1354 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1355 if (note)
1357 rtx insn;
1358 /* Check for earlier movables inside that range,
1359 and mark them invalid. We cannot use LUIDs here because
1360 insns created by loop.c for prior loops don't have LUIDs.
1361 Rather than reject all such insns from movables, we just
1362 explicitly check each insn in the libcall (since invariant
1363 libcalls aren't that common). */
1364 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1365 for (m1 = movables; m1 != m; m1 = m1->next)
1366 if (m1->insn == insn)
1367 m1->done = 1;
1372 /* For each movable insn, see if the reg that it loads
1373 leads when it dies right into another conditionally movable insn.
1374 If so, record that the second insn "forces" the first one,
1375 since the second can be moved only if the first is. */
1377 static void
1378 force_movables (movables)
1379 struct movable *movables;
1381 register struct movable *m, *m1;
1382 for (m1 = movables; m1; m1 = m1->next)
1383 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1384 if (!m1->partial && !m1->done)
1386 int regno = m1->regno;
1387 for (m = m1->next; m; m = m->next)
1388 /* ??? Could this be a bug? What if CSE caused the
1389 register of M1 to be used after this insn?
1390 Since CSE does not update regno_last_uid,
1391 this insn M->insn might not be where it dies.
1392 But very likely this doesn't matter; what matters is
1393 that M's reg is computed from M1's reg. */
1394 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1395 && !m->done)
1396 break;
1397 if (m != 0 && m->set_src == m1->set_dest
1398 /* If m->consec, m->set_src isn't valid. */
1399 && m->consec == 0)
1400 m = 0;
1402 /* Increase the priority of the moving the first insn
1403 since it permits the second to be moved as well. */
1404 if (m != 0)
1406 m->forces = m1;
1407 m1->lifetime += m->lifetime;
1408 m1->savings += m->savings;
1413 /* Find invariant expressions that are equal and can be combined into
1414 one register. */
1416 static void
1417 combine_movables (movables, nregs)
1418 struct movable *movables;
1419 int nregs;
1421 register struct movable *m;
1422 char *matched_regs = (char *) xmalloc (nregs);
1423 enum machine_mode mode;
1425 /* Regs that are set more than once are not allowed to match
1426 or be matched. I'm no longer sure why not. */
1427 /* Perhaps testing m->consec_sets would be more appropriate here? */
1429 for (m = movables; m; m = m->next)
1430 if (m->match == 0 && VARRAY_INT (n_times_set, m->regno) == 1 && !m->partial)
1432 register struct movable *m1;
1433 int regno = m->regno;
1435 bzero (matched_regs, nregs);
1436 matched_regs[regno] = 1;
1438 /* We want later insns to match the first one. Don't make the first
1439 one match any later ones. So start this loop at m->next. */
1440 for (m1 = m->next; m1; m1 = m1->next)
1441 if (m != m1 && m1->match == 0 && VARRAY_INT (n_times_set, m1->regno) == 1
1442 /* A reg used outside the loop mustn't be eliminated. */
1443 && !m1->global
1444 /* A reg used for zero-extending mustn't be eliminated. */
1445 && !m1->partial
1446 && (matched_regs[m1->regno]
1449 /* Can combine regs with different modes loaded from the
1450 same constant only if the modes are the same or
1451 if both are integer modes with M wider or the same
1452 width as M1. The check for integer is redundant, but
1453 safe, since the only case of differing destination
1454 modes with equal sources is when both sources are
1455 VOIDmode, i.e., CONST_INT. */
1456 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1457 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1458 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1459 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1460 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1461 /* See if the source of M1 says it matches M. */
1462 && ((GET_CODE (m1->set_src) == REG
1463 && matched_regs[REGNO (m1->set_src)])
1464 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1465 movables))))
1466 && ((m->dependencies == m1->dependencies)
1467 || rtx_equal_p (m->dependencies, m1->dependencies)))
1469 m->lifetime += m1->lifetime;
1470 m->savings += m1->savings;
1471 m1->done = 1;
1472 m1->match = m;
1473 matched_regs[m1->regno] = 1;
1477 /* Now combine the regs used for zero-extension.
1478 This can be done for those not marked `global'
1479 provided their lives don't overlap. */
1481 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1482 mode = GET_MODE_WIDER_MODE (mode))
1484 register struct movable *m0 = 0;
1486 /* Combine all the registers for extension from mode MODE.
1487 Don't combine any that are used outside this loop. */
1488 for (m = movables; m; m = m->next)
1489 if (m->partial && ! m->global
1490 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1492 register struct movable *m1;
1493 int first = uid_luid[REGNO_FIRST_UID (m->regno)];
1494 int last = uid_luid[REGNO_LAST_UID (m->regno)];
1496 if (m0 == 0)
1498 /* First one: don't check for overlap, just record it. */
1499 m0 = m;
1500 continue;
1503 /* Make sure they extend to the same mode.
1504 (Almost always true.) */
1505 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1506 continue;
1508 /* We already have one: check for overlap with those
1509 already combined together. */
1510 for (m1 = movables; m1 != m; m1 = m1->next)
1511 if (m1 == m0 || (m1->partial && m1->match == m0))
1512 if (! (uid_luid[REGNO_FIRST_UID (m1->regno)] > last
1513 || uid_luid[REGNO_LAST_UID (m1->regno)] < first))
1514 goto overlap;
1516 /* No overlap: we can combine this with the others. */
1517 m0->lifetime += m->lifetime;
1518 m0->savings += m->savings;
1519 m->done = 1;
1520 m->match = m0;
1522 overlap: ;
1526 /* Clean up. */
1527 free (matched_regs);
1530 /* Return 1 if regs X and Y will become the same if moved. */
1532 static int
1533 regs_match_p (x, y, movables)
1534 rtx x, y;
1535 struct movable *movables;
1537 unsigned int xn = REGNO (x);
1538 unsigned int yn = REGNO (y);
1539 struct movable *mx, *my;
1541 for (mx = movables; mx; mx = mx->next)
1542 if (mx->regno == xn)
1543 break;
1545 for (my = movables; my; my = my->next)
1546 if (my->regno == yn)
1547 break;
1549 return (mx && my
1550 && ((mx->match == my->match && mx->match != 0)
1551 || mx->match == my
1552 || mx == my->match));
1555 /* Return 1 if X and Y are identical-looking rtx's.
1556 This is the Lisp function EQUAL for rtx arguments.
1558 If two registers are matching movables or a movable register and an
1559 equivalent constant, consider them equal. */
1561 static int
1562 rtx_equal_for_loop_p (x, y, movables)
1563 rtx x, y;
1564 struct movable *movables;
1566 register int i;
1567 register int j;
1568 register struct movable *m;
1569 register enum rtx_code code;
1570 register const char *fmt;
1572 if (x == y)
1573 return 1;
1574 if (x == 0 || y == 0)
1575 return 0;
1577 code = GET_CODE (x);
1579 /* If we have a register and a constant, they may sometimes be
1580 equal. */
1581 if (GET_CODE (x) == REG && VARRAY_INT (set_in_loop, REGNO (x)) == -2
1582 && CONSTANT_P (y))
1584 for (m = movables; m; m = m->next)
1585 if (m->move_insn && m->regno == REGNO (x)
1586 && rtx_equal_p (m->set_src, y))
1587 return 1;
1589 else if (GET_CODE (y) == REG && VARRAY_INT (set_in_loop, REGNO (y)) == -2
1590 && CONSTANT_P (x))
1592 for (m = movables; m; m = m->next)
1593 if (m->move_insn && m->regno == REGNO (y)
1594 && rtx_equal_p (m->set_src, x))
1595 return 1;
1598 /* Otherwise, rtx's of different codes cannot be equal. */
1599 if (code != GET_CODE (y))
1600 return 0;
1602 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1603 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1605 if (GET_MODE (x) != GET_MODE (y))
1606 return 0;
1608 /* These three types of rtx's can be compared nonrecursively. */
1609 if (code == REG)
1610 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1612 if (code == LABEL_REF)
1613 return XEXP (x, 0) == XEXP (y, 0);
1614 if (code == SYMBOL_REF)
1615 return XSTR (x, 0) == XSTR (y, 0);
1617 /* Compare the elements. If any pair of corresponding elements
1618 fail to match, return 0 for the whole things. */
1620 fmt = GET_RTX_FORMAT (code);
1621 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1623 switch (fmt[i])
1625 case 'w':
1626 if (XWINT (x, i) != XWINT (y, i))
1627 return 0;
1628 break;
1630 case 'i':
1631 if (XINT (x, i) != XINT (y, i))
1632 return 0;
1633 break;
1635 case 'E':
1636 /* Two vectors must have the same length. */
1637 if (XVECLEN (x, i) != XVECLEN (y, i))
1638 return 0;
1640 /* And the corresponding elements must match. */
1641 for (j = 0; j < XVECLEN (x, i); j++)
1642 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
1643 return 0;
1644 break;
1646 case 'e':
1647 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
1648 return 0;
1649 break;
1651 case 's':
1652 if (strcmp (XSTR (x, i), XSTR (y, i)))
1653 return 0;
1654 break;
1656 case 'u':
1657 /* These are just backpointers, so they don't matter. */
1658 break;
1660 case '0':
1661 break;
1663 /* It is believed that rtx's at this level will never
1664 contain anything but integers and other rtx's,
1665 except for within LABEL_REFs and SYMBOL_REFs. */
1666 default:
1667 abort ();
1670 return 1;
1673 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1674 insns in INSNS which use the reference. */
1676 static void
1677 add_label_notes (x, insns)
1678 rtx x;
1679 rtx insns;
1681 enum rtx_code code = GET_CODE (x);
1682 int i, j;
1683 const char *fmt;
1684 rtx insn;
1686 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1688 /* This code used to ignore labels that referred to dispatch tables to
1689 avoid flow generating (slighly) worse code.
1691 We no longer ignore such label references (see LABEL_REF handling in
1692 mark_jump_label for additional information). */
1693 for (insn = insns; insn; insn = NEXT_INSN (insn))
1694 if (reg_mentioned_p (XEXP (x, 0), insn))
1695 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, XEXP (x, 0),
1696 REG_NOTES (insn));
1699 fmt = GET_RTX_FORMAT (code);
1700 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1702 if (fmt[i] == 'e')
1703 add_label_notes (XEXP (x, i), insns);
1704 else if (fmt[i] == 'E')
1705 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1706 add_label_notes (XVECEXP (x, i, j), insns);
1710 /* Scan MOVABLES, and move the insns that deserve to be moved.
1711 If two matching movables are combined, replace one reg with the
1712 other throughout. */
1714 static void
1715 move_movables (loop, movables, threshold, insn_count, nregs)
1716 struct loop *loop;
1717 struct movable *movables;
1718 int threshold;
1719 int insn_count;
1720 int nregs;
1722 rtx new_start = 0;
1723 register struct movable *m;
1724 register rtx p;
1725 rtx loop_start = loop->start;
1726 rtx loop_end = loop->end;
1727 /* Map of pseudo-register replacements to handle combining
1728 when we move several insns that load the same value
1729 into different pseudo-registers. */
1730 rtx *reg_map = (rtx *) xcalloc (nregs, sizeof (rtx));
1731 char *already_moved = (char *) xcalloc (nregs, sizeof (char));
1733 num_movables = 0;
1735 for (m = movables; m; m = m->next)
1737 /* Describe this movable insn. */
1739 if (loop_dump_stream)
1741 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1742 INSN_UID (m->insn), m->regno, m->lifetime);
1743 if (m->consec > 0)
1744 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1745 if (m->cond)
1746 fprintf (loop_dump_stream, "cond ");
1747 if (m->force)
1748 fprintf (loop_dump_stream, "force ");
1749 if (m->global)
1750 fprintf (loop_dump_stream, "global ");
1751 if (m->done)
1752 fprintf (loop_dump_stream, "done ");
1753 if (m->move_insn)
1754 fprintf (loop_dump_stream, "move-insn ");
1755 if (m->match)
1756 fprintf (loop_dump_stream, "matches %d ",
1757 INSN_UID (m->match->insn));
1758 if (m->forces)
1759 fprintf (loop_dump_stream, "forces %d ",
1760 INSN_UID (m->forces->insn));
1763 /* Count movables. Value used in heuristics in strength_reduce. */
1764 num_movables++;
1766 /* Ignore the insn if it's already done (it matched something else).
1767 Otherwise, see if it is now safe to move. */
1769 if (!m->done
1770 && (! m->cond
1771 || (1 == loop_invariant_p (loop, m->set_src)
1772 && (m->dependencies == 0
1773 || 1 == loop_invariant_p (loop, m->dependencies))
1774 && (m->consec == 0
1775 || 1 == consec_sets_invariant_p (loop, m->set_dest,
1776 m->consec + 1,
1777 m->insn))))
1778 && (! m->forces || m->forces->done))
1780 register int regno;
1781 register rtx p;
1782 int savings = m->savings;
1784 /* We have an insn that is safe to move.
1785 Compute its desirability. */
1787 p = m->insn;
1788 regno = m->regno;
1790 if (loop_dump_stream)
1791 fprintf (loop_dump_stream, "savings %d ", savings);
1793 if (moved_once[regno] && loop_dump_stream)
1794 fprintf (loop_dump_stream, "halved since already moved ");
1796 /* An insn MUST be moved if we already moved something else
1797 which is safe only if this one is moved too: that is,
1798 if already_moved[REGNO] is nonzero. */
1800 /* An insn is desirable to move if the new lifetime of the
1801 register is no more than THRESHOLD times the old lifetime.
1802 If it's not desirable, it means the loop is so big
1803 that moving won't speed things up much,
1804 and it is liable to make register usage worse. */
1806 /* It is also desirable to move if it can be moved at no
1807 extra cost because something else was already moved. */
1809 if (already_moved[regno]
1810 || flag_move_all_movables
1811 || (threshold * savings * m->lifetime) >=
1812 (moved_once[regno] ? insn_count * 2 : insn_count)
1813 || (m->forces && m->forces->done
1814 && VARRAY_INT (n_times_set, m->forces->regno) == 1))
1816 int count;
1817 register struct movable *m1;
1818 rtx first = NULL_RTX;
1820 /* Now move the insns that set the reg. */
1822 if (m->partial && m->match)
1824 rtx newpat, i1;
1825 rtx r1, r2;
1826 /* Find the end of this chain of matching regs.
1827 Thus, we load each reg in the chain from that one reg.
1828 And that reg is loaded with 0 directly,
1829 since it has ->match == 0. */
1830 for (m1 = m; m1->match; m1 = m1->match);
1831 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1832 SET_DEST (PATTERN (m1->insn)));
1833 i1 = emit_insn_before (newpat, loop_start);
1835 /* Mark the moved, invariant reg as being allowed to
1836 share a hard reg with the other matching invariant. */
1837 REG_NOTES (i1) = REG_NOTES (m->insn);
1838 r1 = SET_DEST (PATTERN (m->insn));
1839 r2 = SET_DEST (PATTERN (m1->insn));
1840 regs_may_share
1841 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1842 gen_rtx_EXPR_LIST (VOIDmode, r2,
1843 regs_may_share));
1844 delete_insn (m->insn);
1846 if (new_start == 0)
1847 new_start = i1;
1849 if (loop_dump_stream)
1850 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1852 /* If we are to re-generate the item being moved with a
1853 new move insn, first delete what we have and then emit
1854 the move insn before the loop. */
1855 else if (m->move_insn)
1857 rtx i1, temp;
1859 for (count = m->consec; count >= 0; count--)
1861 /* If this is the first insn of a library call sequence,
1862 skip to the end. */
1863 if (GET_CODE (p) != NOTE
1864 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1865 p = XEXP (temp, 0);
1867 /* If this is the last insn of a libcall sequence, then
1868 delete every insn in the sequence except the last.
1869 The last insn is handled in the normal manner. */
1870 if (GET_CODE (p) != NOTE
1871 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1873 temp = XEXP (temp, 0);
1874 while (temp != p)
1875 temp = delete_insn (temp);
1878 temp = p;
1879 p = delete_insn (p);
1881 /* simplify_giv_expr expects that it can walk the insns
1882 at m->insn forwards and see this old sequence we are
1883 tossing here. delete_insn does preserve the next
1884 pointers, but when we skip over a NOTE we must fix
1885 it up. Otherwise that code walks into the non-deleted
1886 insn stream. */
1887 while (p && GET_CODE (p) == NOTE)
1888 p = NEXT_INSN (temp) = NEXT_INSN (p);
1891 start_sequence ();
1892 emit_move_insn (m->set_dest, m->set_src);
1893 temp = get_insns ();
1894 end_sequence ();
1896 add_label_notes (m->set_src, temp);
1898 i1 = emit_insns_before (temp, loop_start);
1899 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1900 REG_NOTES (i1)
1901 = gen_rtx_EXPR_LIST (m->is_equiv ? REG_EQUIV : REG_EQUAL,
1902 m->set_src, REG_NOTES (i1));
1904 if (loop_dump_stream)
1905 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1907 /* The more regs we move, the less we like moving them. */
1908 threshold -= 3;
1910 else
1912 for (count = m->consec; count >= 0; count--)
1914 rtx i1, temp;
1916 /* If first insn of libcall sequence, skip to end. */
1917 /* Do this at start of loop, since p is guaranteed to
1918 be an insn here. */
1919 if (GET_CODE (p) != NOTE
1920 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1921 p = XEXP (temp, 0);
1923 /* If last insn of libcall sequence, move all
1924 insns except the last before the loop. The last
1925 insn is handled in the normal manner. */
1926 if (GET_CODE (p) != NOTE
1927 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1929 rtx fn_address = 0;
1930 rtx fn_reg = 0;
1931 rtx fn_address_insn = 0;
1933 first = 0;
1934 for (temp = XEXP (temp, 0); temp != p;
1935 temp = NEXT_INSN (temp))
1937 rtx body;
1938 rtx n;
1939 rtx next;
1941 if (GET_CODE (temp) == NOTE)
1942 continue;
1944 body = PATTERN (temp);
1946 /* Find the next insn after TEMP,
1947 not counting USE or NOTE insns. */
1948 for (next = NEXT_INSN (temp); next != p;
1949 next = NEXT_INSN (next))
1950 if (! (GET_CODE (next) == INSN
1951 && GET_CODE (PATTERN (next)) == USE)
1952 && GET_CODE (next) != NOTE)
1953 break;
1955 /* If that is the call, this may be the insn
1956 that loads the function address.
1958 Extract the function address from the insn
1959 that loads it into a register.
1960 If this insn was cse'd, we get incorrect code.
1962 So emit a new move insn that copies the
1963 function address into the register that the
1964 call insn will use. flow.c will delete any
1965 redundant stores that we have created. */
1966 if (GET_CODE (next) == CALL_INSN
1967 && GET_CODE (body) == SET
1968 && GET_CODE (SET_DEST (body)) == REG
1969 && (n = find_reg_note (temp, REG_EQUAL,
1970 NULL_RTX)))
1972 fn_reg = SET_SRC (body);
1973 if (GET_CODE (fn_reg) != REG)
1974 fn_reg = SET_DEST (body);
1975 fn_address = XEXP (n, 0);
1976 fn_address_insn = temp;
1978 /* We have the call insn.
1979 If it uses the register we suspect it might,
1980 load it with the correct address directly. */
1981 if (GET_CODE (temp) == CALL_INSN
1982 && fn_address != 0
1983 && reg_referenced_p (fn_reg, body))
1984 emit_insn_after (gen_move_insn (fn_reg,
1985 fn_address),
1986 fn_address_insn);
1988 if (GET_CODE (temp) == CALL_INSN)
1990 i1 = emit_call_insn_before (body, loop_start);
1991 /* Because the USAGE information potentially
1992 contains objects other than hard registers
1993 we need to copy it. */
1994 if (CALL_INSN_FUNCTION_USAGE (temp))
1995 CALL_INSN_FUNCTION_USAGE (i1)
1996 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
1998 else
1999 i1 = emit_insn_before (body, loop_start);
2000 if (first == 0)
2001 first = i1;
2002 if (temp == fn_address_insn)
2003 fn_address_insn = i1;
2004 REG_NOTES (i1) = REG_NOTES (temp);
2005 delete_insn (temp);
2007 if (new_start == 0)
2008 new_start = first;
2010 if (m->savemode != VOIDmode)
2012 /* P sets REG to zero; but we should clear only
2013 the bits that are not covered by the mode
2014 m->savemode. */
2015 rtx reg = m->set_dest;
2016 rtx sequence;
2017 rtx tem;
2019 start_sequence ();
2020 tem = expand_binop
2021 (GET_MODE (reg), and_optab, reg,
2022 GEN_INT ((((HOST_WIDE_INT) 1
2023 << GET_MODE_BITSIZE (m->savemode)))
2024 - 1),
2025 reg, 1, OPTAB_LIB_WIDEN);
2026 if (tem == 0)
2027 abort ();
2028 if (tem != reg)
2029 emit_move_insn (reg, tem);
2030 sequence = gen_sequence ();
2031 end_sequence ();
2032 i1 = emit_insn_before (sequence, loop_start);
2034 else if (GET_CODE (p) == CALL_INSN)
2036 i1 = emit_call_insn_before (PATTERN (p), loop_start);
2037 /* Because the USAGE information potentially
2038 contains objects other than hard registers
2039 we need to copy it. */
2040 if (CALL_INSN_FUNCTION_USAGE (p))
2041 CALL_INSN_FUNCTION_USAGE (i1)
2042 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
2044 else if (count == m->consec && m->move_insn_first)
2046 /* The SET_SRC might not be invariant, so we must
2047 use the REG_EQUAL note. */
2048 start_sequence ();
2049 emit_move_insn (m->set_dest, m->set_src);
2050 temp = get_insns ();
2051 end_sequence ();
2053 add_label_notes (m->set_src, temp);
2055 i1 = emit_insns_before (temp, loop_start);
2056 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2057 REG_NOTES (i1)
2058 = gen_rtx_EXPR_LIST ((m->is_equiv ? REG_EQUIV
2059 : REG_EQUAL),
2060 m->set_src, REG_NOTES (i1));
2062 else
2063 i1 = emit_insn_before (PATTERN (p), loop_start);
2065 if (REG_NOTES (i1) == 0)
2067 REG_NOTES (i1) = REG_NOTES (p);
2069 /* If there is a REG_EQUAL note present whose value
2070 is not loop invariant, then delete it, since it
2071 may cause problems with later optimization passes.
2072 It is possible for cse to create such notes
2073 like this as a result of record_jump_cond. */
2075 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2076 && ! loop_invariant_p (loop, XEXP (temp, 0)))
2077 remove_note (i1, temp);
2080 if (new_start == 0)
2081 new_start = i1;
2083 if (loop_dump_stream)
2084 fprintf (loop_dump_stream, " moved to %d",
2085 INSN_UID (i1));
2087 /* If library call, now fix the REG_NOTES that contain
2088 insn pointers, namely REG_LIBCALL on FIRST
2089 and REG_RETVAL on I1. */
2090 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
2092 XEXP (temp, 0) = first;
2093 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2094 XEXP (temp, 0) = i1;
2097 temp = p;
2098 delete_insn (p);
2099 p = NEXT_INSN (p);
2101 /* simplify_giv_expr expects that it can walk the insns
2102 at m->insn forwards and see this old sequence we are
2103 tossing here. delete_insn does preserve the next
2104 pointers, but when we skip over a NOTE we must fix
2105 it up. Otherwise that code walks into the non-deleted
2106 insn stream. */
2107 while (p && GET_CODE (p) == NOTE)
2108 p = NEXT_INSN (temp) = NEXT_INSN (p);
2111 /* The more regs we move, the less we like moving them. */
2112 threshold -= 3;
2115 /* Any other movable that loads the same register
2116 MUST be moved. */
2117 already_moved[regno] = 1;
2119 /* This reg has been moved out of one loop. */
2120 moved_once[regno] = 1;
2122 /* The reg set here is now invariant. */
2123 if (! m->partial)
2124 VARRAY_INT (set_in_loop, regno) = 0;
2126 m->done = 1;
2128 /* Change the length-of-life info for the register
2129 to say it lives at least the full length of this loop.
2130 This will help guide optimizations in outer loops. */
2132 if (uid_luid[REGNO_FIRST_UID (regno)] > INSN_LUID (loop_start))
2133 /* This is the old insn before all the moved insns.
2134 We can't use the moved insn because it is out of range
2135 in uid_luid. Only the old insns have luids. */
2136 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2137 if (uid_luid[REGNO_LAST_UID (regno)] < INSN_LUID (loop_end))
2138 REGNO_LAST_UID (regno) = INSN_UID (loop_end);
2140 /* Combine with this moved insn any other matching movables. */
2142 if (! m->partial)
2143 for (m1 = movables; m1; m1 = m1->next)
2144 if (m1->match == m)
2146 rtx temp;
2148 /* Schedule the reg loaded by M1
2149 for replacement so that shares the reg of M.
2150 If the modes differ (only possible in restricted
2151 circumstances, make a SUBREG.
2153 Note this assumes that the target dependent files
2154 treat REG and SUBREG equally, including within
2155 GO_IF_LEGITIMATE_ADDRESS and in all the
2156 predicates since we never verify that replacing the
2157 original register with a SUBREG results in a
2158 recognizable insn. */
2159 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2160 reg_map[m1->regno] = m->set_dest;
2161 else
2162 reg_map[m1->regno]
2163 = gen_lowpart_common (GET_MODE (m1->set_dest),
2164 m->set_dest);
2166 /* Get rid of the matching insn
2167 and prevent further processing of it. */
2168 m1->done = 1;
2170 /* if library call, delete all insn except last, which
2171 is deleted below */
2172 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2173 NULL_RTX)))
2175 for (temp = XEXP (temp, 0); temp != m1->insn;
2176 temp = NEXT_INSN (temp))
2177 delete_insn (temp);
2179 delete_insn (m1->insn);
2181 /* Any other movable that loads the same register
2182 MUST be moved. */
2183 already_moved[m1->regno] = 1;
2185 /* The reg merged here is now invariant,
2186 if the reg it matches is invariant. */
2187 if (! m->partial)
2188 VARRAY_INT (set_in_loop, m1->regno) = 0;
2191 else if (loop_dump_stream)
2192 fprintf (loop_dump_stream, "not desirable");
2194 else if (loop_dump_stream && !m->match)
2195 fprintf (loop_dump_stream, "not safe");
2197 if (loop_dump_stream)
2198 fprintf (loop_dump_stream, "\n");
2201 if (new_start == 0)
2202 new_start = loop_start;
2204 /* Go through all the instructions in the loop, making
2205 all the register substitutions scheduled in REG_MAP. */
2206 for (p = new_start; p != loop_end; p = NEXT_INSN (p))
2207 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2208 || GET_CODE (p) == CALL_INSN)
2210 replace_regs (PATTERN (p), reg_map, nregs, 0);
2211 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2212 INSN_CODE (p) = -1;
2215 /* Clean up. */
2216 free (reg_map);
2217 free (already_moved);
2220 #if 0
2221 /* Scan X and replace the address of any MEM in it with ADDR.
2222 REG is the address that MEM should have before the replacement. */
2224 static void
2225 replace_call_address (x, reg, addr)
2226 rtx x, reg, addr;
2228 register enum rtx_code code;
2229 register int i;
2230 register const char *fmt;
2232 if (x == 0)
2233 return;
2234 code = GET_CODE (x);
2235 switch (code)
2237 case PC:
2238 case CC0:
2239 case CONST_INT:
2240 case CONST_DOUBLE:
2241 case CONST:
2242 case SYMBOL_REF:
2243 case LABEL_REF:
2244 case REG:
2245 return;
2247 case SET:
2248 /* Short cut for very common case. */
2249 replace_call_address (XEXP (x, 1), reg, addr);
2250 return;
2252 case CALL:
2253 /* Short cut for very common case. */
2254 replace_call_address (XEXP (x, 0), reg, addr);
2255 return;
2257 case MEM:
2258 /* If this MEM uses a reg other than the one we expected,
2259 something is wrong. */
2260 if (XEXP (x, 0) != reg)
2261 abort ();
2262 XEXP (x, 0) = addr;
2263 return;
2265 default:
2266 break;
2269 fmt = GET_RTX_FORMAT (code);
2270 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2272 if (fmt[i] == 'e')
2273 replace_call_address (XEXP (x, i), reg, addr);
2274 else if (fmt[i] == 'E')
2276 register int j;
2277 for (j = 0; j < XVECLEN (x, i); j++)
2278 replace_call_address (XVECEXP (x, i, j), reg, addr);
2282 #endif
2284 /* Return the number of memory refs to addresses that vary
2285 in the rtx X. */
2287 static int
2288 count_nonfixed_reads (loop, x)
2289 const struct loop *loop;
2290 rtx x;
2292 register enum rtx_code code;
2293 register int i;
2294 register const char *fmt;
2295 int value;
2297 if (x == 0)
2298 return 0;
2300 code = GET_CODE (x);
2301 switch (code)
2303 case PC:
2304 case CC0:
2305 case CONST_INT:
2306 case CONST_DOUBLE:
2307 case CONST:
2308 case SYMBOL_REF:
2309 case LABEL_REF:
2310 case REG:
2311 return 0;
2313 case MEM:
2314 return ((loop_invariant_p (loop, XEXP (x, 0)) != 1)
2315 + count_nonfixed_reads (loop, XEXP (x, 0)));
2317 default:
2318 break;
2321 value = 0;
2322 fmt = GET_RTX_FORMAT (code);
2323 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2325 if (fmt[i] == 'e')
2326 value += count_nonfixed_reads (loop, XEXP (x, i));
2327 if (fmt[i] == 'E')
2329 register int j;
2330 for (j = 0; j < XVECLEN (x, i); j++)
2331 value += count_nonfixed_reads (loop, XVECEXP (x, i, j));
2334 return value;
2338 #if 0
2339 /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2340 Replace it with an instruction to load just the low bytes
2341 if the machine supports such an instruction,
2342 and insert above LOOP_START an instruction to clear the register. */
2344 static void
2345 constant_high_bytes (p, loop_start)
2346 rtx p, loop_start;
2348 register rtx new;
2349 register int insn_code_number;
2351 /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2352 to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
2355 = gen_rtx_SET
2356 (VOIDmode,
2357 gen_rtx_STRICT_LOW_PART
2358 (VOIDmode,
2359 gen_rtx_SUBREG (GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
2360 SET_DEST (PATTERN (p)), 0)),
2361 XEXP (SET_SRC (PATTERN (p)), 0));
2363 insn_code_number = recog (new, p);
2365 if (insn_code_number)
2367 register int i;
2369 /* Clear destination register before the loop. */
2370 emit_insn_before (gen_rtx_SET (VOIDmode,
2371 SET_DEST (PATTERN (p)), const0_rtx),
2372 loop_start);
2374 /* Inside the loop, just load the low part. */
2375 PATTERN (p) = new;
2378 #endif
2380 /* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
2381 `has_call', `has_volatile', and `has_tablejump' within LOOP.
2382 Set the global variables `unknown_address_altered',
2383 `unknown_constant_address_altered', and `num_mem_sets'. Also, fill
2384 in the array `loop_mems' and the list `loop_store_mems'. */
2386 static void
2387 prescan_loop (loop)
2388 struct loop *loop;
2390 register int level = 1;
2391 rtx insn;
2392 struct loop_info *loop_info = LOOP_INFO (loop);
2393 rtx start = loop->start;
2394 rtx end = loop->end;
2395 /* The label after END. Jumping here is just like falling off the
2396 end of the loop. We use next_nonnote_insn instead of next_label
2397 as a hedge against the (pathological) case where some actual insn
2398 might end up between the two. */
2399 rtx exit_target = next_nonnote_insn (end);
2401 loop_info->has_indirect_jump = indirect_jump_in_function;
2402 loop_info->has_call = 0;
2403 loop_info->has_volatile = 0;
2404 loop_info->has_tablejump = 0;
2405 loop_info->has_multiple_exit_targets = 0;
2406 loop->cont = 0;
2407 loop->vtop = 0;
2408 loop->level = 1;
2410 unknown_address_altered = 0;
2411 unknown_constant_address_altered = 0;
2412 loop_store_mems = NULL_RTX;
2413 first_loop_store_insn = NULL_RTX;
2414 loop_mems_idx = 0;
2415 num_mem_sets = 0;
2417 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2418 insn = NEXT_INSN (insn))
2420 if (GET_CODE (insn) == NOTE)
2422 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2424 ++level;
2425 /* Count number of loops contained in this one. */
2426 loop->level++;
2428 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2430 --level;
2431 if (level == 0)
2433 end = insn;
2434 break;
2437 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2439 if (level == 1)
2440 loop->cont = insn;
2442 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP)
2444 /* If there is a NOTE_INSN_LOOP_VTOP, then this is a for
2445 or while style loop, with a loop exit test at the
2446 start. Thus, we can assume that the loop condition
2447 was true when the loop was entered. */
2448 if (level == 1)
2449 loop->vtop = insn;
2452 else if (GET_CODE (insn) == CALL_INSN)
2454 if (! CONST_CALL_P (insn))
2455 unknown_address_altered = 1;
2456 loop_info->has_call = 1;
2458 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
2460 rtx label1 = NULL_RTX;
2461 rtx label2 = NULL_RTX;
2463 if (volatile_refs_p (PATTERN (insn)))
2464 loop_info->has_volatile = 1;
2466 if (GET_CODE (insn) == JUMP_INSN
2467 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2468 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2469 loop_info->has_tablejump = 1;
2471 note_stores (PATTERN (insn), note_addr_stored, NULL);
2472 if (! first_loop_store_insn && loop_store_mems)
2473 first_loop_store_insn = insn;
2475 if (! loop_info->has_multiple_exit_targets
2476 && GET_CODE (insn) == JUMP_INSN
2477 && GET_CODE (PATTERN (insn)) == SET
2478 && SET_DEST (PATTERN (insn)) == pc_rtx)
2480 if (GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE)
2482 label1 = XEXP (SET_SRC (PATTERN (insn)), 1);
2483 label2 = XEXP (SET_SRC (PATTERN (insn)), 2);
2485 else
2487 label1 = SET_SRC (PATTERN (insn));
2490 do {
2491 if (label1 && label1 != pc_rtx)
2493 if (GET_CODE (label1) != LABEL_REF)
2495 /* Something tricky. */
2496 loop_info->has_multiple_exit_targets = 1;
2497 break;
2499 else if (XEXP (label1, 0) != exit_target
2500 && LABEL_OUTSIDE_LOOP_P (label1))
2502 /* A jump outside the current loop. */
2503 loop_info->has_multiple_exit_targets = 1;
2504 break;
2508 label1 = label2;
2509 label2 = NULL_RTX;
2510 } while (label1);
2513 else if (GET_CODE (insn) == RETURN)
2514 loop_info->has_multiple_exit_targets = 1;
2517 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2518 if (/* We can't tell what MEMs are aliased by what. */
2519 ! unknown_address_altered
2520 /* An exception thrown by a called function might land us
2521 anywhere. */
2522 && ! loop_info->has_call
2523 /* We don't want loads for MEMs moved to a location before the
2524 one at which their stack memory becomes allocated. (Note
2525 that this is not a problem for malloc, etc., since those
2526 require actual function calls. */
2527 && ! current_function_calls_alloca
2528 /* There are ways to leave the loop other than falling off the
2529 end. */
2530 && ! loop_info->has_multiple_exit_targets)
2531 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2532 insn = NEXT_INSN (insn))
2533 for_each_rtx (&insn, insert_loop_mem, 0);
2536 /* LOOP->CONT_DOMINATOR is now the last label between the loop start
2537 and the continue note that is a the destination of a (cond)jump after
2538 the continue note. If there is any (cond)jump between the loop start
2539 and what we have so far as LOOP->CONT_DOMINATOR that has a
2540 target between LOOP->DOMINATOR and the continue note, move
2541 LOOP->CONT_DOMINATOR forward to that label; if a jump's
2542 destination cannot be determined, clear LOOP->CONT_DOMINATOR. */
2544 static void
2545 verify_dominator (loop)
2546 struct loop *loop;
2548 rtx insn;
2550 if (! loop->cont_dominator)
2551 /* This can happen for an empty loop, e.g. in
2552 gcc.c-torture/compile/920410-2.c */
2553 return;
2554 if (loop->cont_dominator == const0_rtx)
2556 loop->cont_dominator = 0;
2557 return;
2559 for (insn = loop->start; insn != loop->cont_dominator;
2560 insn = NEXT_INSN (insn))
2562 if (GET_CODE (insn) == JUMP_INSN
2563 && GET_CODE (PATTERN (insn)) != RETURN)
2565 rtx label = JUMP_LABEL (insn);
2566 int label_luid;
2568 /* If it is not a jump we can easily understand or for
2569 which we do not have jump target information in the JUMP_LABEL
2570 field (consider ADDR_VEC and ADDR_DIFF_VEC insns), then clear
2571 LOOP->CONT_DOMINATOR. */
2572 if (! any_condjump_p (insn)
2573 || label == NULL_RTX)
2575 loop->cont_dominator = NULL_RTX;
2576 return;
2579 label_luid = INSN_LUID (label);
2580 if (label_luid < INSN_LUID (loop->cont)
2581 && (label_luid
2582 > INSN_LUID (loop->cont)))
2583 loop->cont_dominator = label;
2588 /* Scan the function looking for loops. Record the start and end of each loop.
2589 Also mark as invalid loops any loops that contain a setjmp or are branched
2590 to from outside the loop. */
2592 static void
2593 find_and_verify_loops (f, loops)
2594 rtx f;
2595 struct loops *loops;
2597 rtx insn;
2598 rtx label;
2599 int num_loops;
2600 struct loop *current_loop;
2601 struct loop *next_loop;
2602 struct loop *loop;
2604 num_loops = loops->num;
2606 compute_luids (f, NULL_RTX, 0);
2608 /* If there are jumps to undefined labels,
2609 treat them as jumps out of any/all loops.
2610 This also avoids writing past end of tables when there are no loops. */
2611 uid_loop[0] = NULL;
2613 /* Find boundaries of loops, mark which loops are contained within
2614 loops, and invalidate loops that have setjmp. */
2616 num_loops = 0;
2617 current_loop = NULL;
2618 for (insn = f; insn; insn = NEXT_INSN (insn))
2620 if (GET_CODE (insn) == NOTE)
2621 switch (NOTE_LINE_NUMBER (insn))
2623 case NOTE_INSN_LOOP_BEG:
2624 next_loop = loops->array + num_loops;
2625 next_loop->num = num_loops;
2626 num_loops++;
2627 next_loop->start = insn;
2628 next_loop->outer = current_loop;
2629 current_loop = next_loop;
2630 break;
2632 case NOTE_INSN_SETJMP:
2633 /* In this case, we must invalidate our current loop and any
2634 enclosing loop. */
2635 for (loop = current_loop; loop; loop = loop->outer)
2637 loop->invalid = 1;
2638 if (loop_dump_stream)
2639 fprintf (loop_dump_stream,
2640 "\nLoop at %d ignored due to setjmp.\n",
2641 INSN_UID (loop->start));
2643 break;
2645 case NOTE_INSN_LOOP_CONT:
2646 current_loop->cont = insn;
2647 break;
2648 case NOTE_INSN_LOOP_END:
2649 if (! current_loop)
2650 abort ();
2652 current_loop->end = insn;
2653 verify_dominator (current_loop);
2654 current_loop = current_loop->outer;
2655 break;
2657 default:
2658 break;
2660 /* If for any loop, this is a jump insn between the NOTE_INSN_LOOP_CONT
2661 and NOTE_INSN_LOOP_END notes, update loop->dominator. */
2662 else if (GET_CODE (insn) == JUMP_INSN
2663 && GET_CODE (PATTERN (insn)) != RETURN
2664 && current_loop)
2666 rtx label = JUMP_LABEL (insn);
2668 if (! any_condjump_p (insn))
2669 label = NULL_RTX;
2671 loop = current_loop;
2674 /* First see if we care about this loop. */
2675 if (loop->cont && loop->cont_dominator != const0_rtx)
2677 /* If the jump destination is not known, invalidate
2678 loop->const_dominator. */
2679 if (! label)
2680 loop->cont_dominator = const0_rtx;
2681 else
2682 /* Check if the destination is between loop start and
2683 cont. */
2684 if ((INSN_LUID (label)
2685 < INSN_LUID (loop->cont))
2686 && (INSN_LUID (label)
2687 > INSN_LUID (loop->start))
2688 /* And if there is no later destination already
2689 recorded. */
2690 && (! loop->cont_dominator
2691 || (INSN_LUID (label)
2692 > INSN_LUID (loop->cont_dominator))))
2693 loop->cont_dominator = label;
2695 loop = loop->outer;
2697 while (loop);
2700 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2701 enclosing loop, but this doesn't matter. */
2702 uid_loop[INSN_UID (insn)] = current_loop;
2705 /* Any loop containing a label used in an initializer must be invalidated,
2706 because it can be jumped into from anywhere. */
2708 for (label = forced_labels; label; label = XEXP (label, 1))
2710 for (loop = uid_loop[INSN_UID (XEXP (label, 0))];
2711 loop; loop = loop->outer)
2712 loop->invalid = 1;
2715 /* Any loop containing a label used for an exception handler must be
2716 invalidated, because it can be jumped into from anywhere. */
2718 for (label = exception_handler_labels; label; label = XEXP (label, 1))
2720 for (loop = uid_loop[INSN_UID (XEXP (label, 0))];
2721 loop; loop = loop->outer)
2722 loop->invalid = 1;
2725 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2726 loop that it is not contained within, that loop is marked invalid.
2727 If any INSN or CALL_INSN uses a label's address, then the loop containing
2728 that label is marked invalid, because it could be jumped into from
2729 anywhere.
2731 Also look for blocks of code ending in an unconditional branch that
2732 exits the loop. If such a block is surrounded by a conditional
2733 branch around the block, move the block elsewhere (see below) and
2734 invert the jump to point to the code block. This may eliminate a
2735 label in our loop and will simplify processing by both us and a
2736 possible second cse pass. */
2738 for (insn = f; insn; insn = NEXT_INSN (insn))
2739 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2741 struct loop *this_loop = uid_loop[INSN_UID (insn)];
2743 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2745 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2746 if (note)
2748 for (loop = uid_loop[INSN_UID (XEXP (note, 0))];
2749 loop; loop = loop->outer)
2750 loop->invalid = 1;
2754 if (GET_CODE (insn) != JUMP_INSN)
2755 continue;
2757 mark_loop_jump (PATTERN (insn), this_loop);
2759 /* See if this is an unconditional branch outside the loop. */
2760 if (this_loop
2761 && (GET_CODE (PATTERN (insn)) == RETURN
2762 || (any_uncondjump_p (insn)
2763 && onlyjump_p (insn)
2764 && (uid_loop[INSN_UID (JUMP_LABEL (insn))]
2765 != this_loop)))
2766 && get_max_uid () < max_uid_for_loop)
2768 rtx p;
2769 rtx our_next = next_real_insn (insn);
2770 rtx last_insn_to_move = NEXT_INSN (insn);
2771 struct loop *dest_loop;
2772 struct loop *outer_loop = NULL;
2774 /* Go backwards until we reach the start of the loop, a label,
2775 or a JUMP_INSN. */
2776 for (p = PREV_INSN (insn);
2777 GET_CODE (p) != CODE_LABEL
2778 && ! (GET_CODE (p) == NOTE
2779 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2780 && GET_CODE (p) != JUMP_INSN;
2781 p = PREV_INSN (p))
2784 /* Check for the case where we have a jump to an inner nested
2785 loop, and do not perform the optimization in that case. */
2787 if (JUMP_LABEL (insn))
2789 dest_loop = uid_loop[INSN_UID (JUMP_LABEL (insn))];
2790 if (dest_loop)
2792 for (outer_loop = dest_loop; outer_loop;
2793 outer_loop = outer_loop->outer)
2794 if (outer_loop == this_loop)
2795 break;
2799 /* Make sure that the target of P is within the current loop. */
2801 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2802 && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
2803 outer_loop = this_loop;
2805 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2806 we have a block of code to try to move.
2808 We look backward and then forward from the target of INSN
2809 to find a BARRIER at the same loop depth as the target.
2810 If we find such a BARRIER, we make a new label for the start
2811 of the block, invert the jump in P and point it to that label,
2812 and move the block of code to the spot we found. */
2814 if (! outer_loop
2815 && GET_CODE (p) == JUMP_INSN
2816 && JUMP_LABEL (p) != 0
2817 /* Just ignore jumps to labels that were never emitted.
2818 These always indicate compilation errors. */
2819 && INSN_UID (JUMP_LABEL (p)) != 0
2820 && any_condjump_p (p) && onlyjump_p (p)
2821 && next_real_insn (JUMP_LABEL (p)) == our_next
2822 /* If it's not safe to move the sequence, then we
2823 mustn't try. */
2824 && insns_safe_to_move_p (p, NEXT_INSN (insn),
2825 &last_insn_to_move))
2827 rtx target
2828 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2829 struct loop *target_loop = uid_loop[INSN_UID (target)];
2830 rtx loc, loc2;
2832 for (loc = target; loc; loc = PREV_INSN (loc))
2833 if (GET_CODE (loc) == BARRIER
2834 /* Don't move things inside a tablejump. */
2835 && ((loc2 = next_nonnote_insn (loc)) == 0
2836 || GET_CODE (loc2) != CODE_LABEL
2837 || (loc2 = next_nonnote_insn (loc2)) == 0
2838 || GET_CODE (loc2) != JUMP_INSN
2839 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2840 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2841 && uid_loop[INSN_UID (loc)] == target_loop)
2842 break;
2844 if (loc == 0)
2845 for (loc = target; loc; loc = NEXT_INSN (loc))
2846 if (GET_CODE (loc) == BARRIER
2847 /* Don't move things inside a tablejump. */
2848 && ((loc2 = next_nonnote_insn (loc)) == 0
2849 || GET_CODE (loc2) != CODE_LABEL
2850 || (loc2 = next_nonnote_insn (loc2)) == 0
2851 || GET_CODE (loc2) != JUMP_INSN
2852 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2853 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2854 && uid_loop[INSN_UID (loc)] == target_loop)
2855 break;
2857 if (loc)
2859 rtx cond_label = JUMP_LABEL (p);
2860 rtx new_label = get_label_after (p);
2862 /* Ensure our label doesn't go away. */
2863 LABEL_NUSES (cond_label)++;
2865 /* Verify that uid_loop is large enough and that
2866 we can invert P. */
2867 if (invert_jump (p, new_label, 1))
2869 rtx q, r;
2871 /* If no suitable BARRIER was found, create a suitable
2872 one before TARGET. Since TARGET is a fall through
2873 path, we'll need to insert an jump around our block
2874 and a add a BARRIER before TARGET.
2876 This creates an extra unconditional jump outside
2877 the loop. However, the benefits of removing rarely
2878 executed instructions from inside the loop usually
2879 outweighs the cost of the extra unconditional jump
2880 outside the loop. */
2881 if (loc == 0)
2883 rtx temp;
2885 temp = gen_jump (JUMP_LABEL (insn));
2886 temp = emit_jump_insn_before (temp, target);
2887 JUMP_LABEL (temp) = JUMP_LABEL (insn);
2888 LABEL_NUSES (JUMP_LABEL (insn))++;
2889 loc = emit_barrier_before (target);
2892 /* Include the BARRIER after INSN and copy the
2893 block after LOC. */
2894 new_label = squeeze_notes (new_label,
2895 last_insn_to_move);
2896 reorder_insns (new_label, last_insn_to_move, loc);
2898 /* All those insns are now in TARGET_LOOP. */
2899 for (q = new_label;
2900 q != NEXT_INSN (last_insn_to_move);
2901 q = NEXT_INSN (q))
2902 uid_loop[INSN_UID (q)] = target_loop;
2904 /* The label jumped to by INSN is no longer a loop
2905 exit. Unless INSN does not have a label (e.g.,
2906 it is a RETURN insn), search loop->exit_labels
2907 to find its label_ref, and remove it. Also turn
2908 off LABEL_OUTSIDE_LOOP_P bit. */
2909 if (JUMP_LABEL (insn))
2911 for (q = 0,
2912 r = this_loop->exit_labels;
2913 r; q = r, r = LABEL_NEXTREF (r))
2914 if (XEXP (r, 0) == JUMP_LABEL (insn))
2916 LABEL_OUTSIDE_LOOP_P (r) = 0;
2917 if (q)
2918 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2919 else
2920 this_loop->exit_labels = LABEL_NEXTREF (r);
2921 break;
2924 for (loop = this_loop; loop && loop != target_loop;
2925 loop = loop->outer)
2926 loop->exit_count--;
2928 /* If we didn't find it, then something is
2929 wrong. */
2930 if (! r)
2931 abort ();
2934 /* P is now a jump outside the loop, so it must be put
2935 in loop->exit_labels, and marked as such.
2936 The easiest way to do this is to just call
2937 mark_loop_jump again for P. */
2938 mark_loop_jump (PATTERN (p), this_loop);
2940 /* If INSN now jumps to the insn after it,
2941 delete INSN. */
2942 if (JUMP_LABEL (insn) != 0
2943 && (next_real_insn (JUMP_LABEL (insn))
2944 == next_real_insn (insn)))
2945 delete_insn (insn);
2948 /* Continue the loop after where the conditional
2949 branch used to jump, since the only branch insn
2950 in the block (if it still remains) is an inter-loop
2951 branch and hence needs no processing. */
2952 insn = NEXT_INSN (cond_label);
2954 if (--LABEL_NUSES (cond_label) == 0)
2955 delete_insn (cond_label);
2957 /* This loop will be continued with NEXT_INSN (insn). */
2958 insn = PREV_INSN (insn);
2965 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2966 loops it is contained in, mark the target loop invalid.
2968 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2970 static void
2971 mark_loop_jump (x, loop)
2972 rtx x;
2973 struct loop *loop;
2975 struct loop *dest_loop;
2976 struct loop *outer_loop;
2977 int i;
2979 switch (GET_CODE (x))
2981 case PC:
2982 case USE:
2983 case CLOBBER:
2984 case REG:
2985 case MEM:
2986 case CONST_INT:
2987 case CONST_DOUBLE:
2988 case RETURN:
2989 return;
2991 case CONST:
2992 /* There could be a label reference in here. */
2993 mark_loop_jump (XEXP (x, 0), loop);
2994 return;
2996 case PLUS:
2997 case MINUS:
2998 case MULT:
2999 mark_loop_jump (XEXP (x, 0), loop);
3000 mark_loop_jump (XEXP (x, 1), loop);
3001 return;
3003 case LO_SUM:
3004 /* This may refer to a LABEL_REF or SYMBOL_REF. */
3005 mark_loop_jump (XEXP (x, 1), loop);
3006 return;
3008 case SIGN_EXTEND:
3009 case ZERO_EXTEND:
3010 mark_loop_jump (XEXP (x, 0), loop);
3011 return;
3013 case LABEL_REF:
3014 dest_loop = uid_loop[INSN_UID (XEXP (x, 0))];
3016 /* Link together all labels that branch outside the loop. This
3017 is used by final_[bg]iv_value and the loop unrolling code. Also
3018 mark this LABEL_REF so we know that this branch should predict
3019 false. */
3021 /* A check to make sure the label is not in an inner nested loop,
3022 since this does not count as a loop exit. */
3023 if (dest_loop)
3025 for (outer_loop = dest_loop; outer_loop;
3026 outer_loop = outer_loop->outer)
3027 if (outer_loop == loop)
3028 break;
3030 else
3031 outer_loop = NULL;
3033 if (loop && ! outer_loop)
3035 LABEL_OUTSIDE_LOOP_P (x) = 1;
3036 LABEL_NEXTREF (x) = loop->exit_labels;
3037 loop->exit_labels = x;
3039 for (outer_loop = loop;
3040 outer_loop && outer_loop != dest_loop;
3041 outer_loop = outer_loop->outer)
3042 outer_loop->exit_count++;
3045 /* If this is inside a loop, but not in the current loop or one enclosed
3046 by it, it invalidates at least one loop. */
3048 if (! dest_loop)
3049 return;
3051 /* We must invalidate every nested loop containing the target of this
3052 label, except those that also contain the jump insn. */
3054 for (; dest_loop; dest_loop = dest_loop->outer)
3056 /* Stop when we reach a loop that also contains the jump insn. */
3057 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3058 if (dest_loop == outer_loop)
3059 return;
3061 /* If we get here, we know we need to invalidate a loop. */
3062 if (loop_dump_stream && ! dest_loop->invalid)
3063 fprintf (loop_dump_stream,
3064 "\nLoop at %d ignored due to multiple entry points.\n",
3065 INSN_UID (dest_loop->start));
3067 dest_loop->invalid = 1;
3069 return;
3071 case SET:
3072 /* If this is not setting pc, ignore. */
3073 if (SET_DEST (x) == pc_rtx)
3074 mark_loop_jump (SET_SRC (x), loop);
3075 return;
3077 case IF_THEN_ELSE:
3078 mark_loop_jump (XEXP (x, 1), loop);
3079 mark_loop_jump (XEXP (x, 2), loop);
3080 return;
3082 case PARALLEL:
3083 case ADDR_VEC:
3084 for (i = 0; i < XVECLEN (x, 0); i++)
3085 mark_loop_jump (XVECEXP (x, 0, i), loop);
3086 return;
3088 case ADDR_DIFF_VEC:
3089 for (i = 0; i < XVECLEN (x, 1); i++)
3090 mark_loop_jump (XVECEXP (x, 1, i), loop);
3091 return;
3093 default:
3094 /* Strictly speaking this is not a jump into the loop, only a possible
3095 jump out of the loop. However, we have no way to link the destination
3096 of this jump onto the list of exit labels. To be safe we mark this
3097 loop and any containing loops as invalid. */
3098 if (loop)
3100 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3102 if (loop_dump_stream && ! outer_loop->invalid)
3103 fprintf (loop_dump_stream,
3104 "\nLoop at %d ignored due to unknown exit jump.\n",
3105 INSN_UID (outer_loop->start));
3106 outer_loop->invalid = 1;
3109 return;
3113 /* Return nonzero if there is a label in the range from
3114 insn INSN to and including the insn whose luid is END
3115 INSN must have an assigned luid (i.e., it must not have
3116 been previously created by loop.c). */
3118 static int
3119 labels_in_range_p (insn, end)
3120 rtx insn;
3121 int end;
3123 while (insn && INSN_LUID (insn) <= end)
3125 if (GET_CODE (insn) == CODE_LABEL)
3126 return 1;
3127 insn = NEXT_INSN (insn);
3130 return 0;
3133 /* Record that a memory reference X is being set. */
3135 static void
3136 note_addr_stored (x, y, data)
3137 rtx x;
3138 rtx y ATTRIBUTE_UNUSED;
3139 void *data ATTRIBUTE_UNUSED;
3141 if (x == 0 || GET_CODE (x) != MEM)
3142 return;
3144 /* Count number of memory writes.
3145 This affects heuristics in strength_reduce. */
3146 num_mem_sets++;
3148 /* BLKmode MEM means all memory is clobbered. */
3149 if (GET_MODE (x) == BLKmode)
3151 if (RTX_UNCHANGING_P (x))
3152 unknown_constant_address_altered = 1;
3153 else
3154 unknown_address_altered = 1;
3156 return;
3159 loop_store_mems = gen_rtx_EXPR_LIST (VOIDmode, x, loop_store_mems);
3162 /* X is a value modified by an INSN that references a biv inside a loop
3163 exit test (ie, X is somehow related to the value of the biv). If X
3164 is a pseudo that is used more than once, then the biv is (effectively)
3165 used more than once. DATA is really an `int *', and is set if the
3166 biv is used more than once. */
3168 static void
3169 note_set_pseudo_multiple_uses (x, y, data)
3170 rtx x;
3171 rtx y ATTRIBUTE_UNUSED;
3172 void *data;
3174 if (x == 0)
3175 return;
3177 while (GET_CODE (x) == STRICT_LOW_PART
3178 || GET_CODE (x) == SIGN_EXTRACT
3179 || GET_CODE (x) == ZERO_EXTRACT
3180 || GET_CODE (x) == SUBREG)
3181 x = XEXP (x, 0);
3183 if (GET_CODE (x) != REG || REGNO (x) < FIRST_PSEUDO_REGISTER)
3184 return;
3186 /* If we do not have usage information, or if we know the register
3187 is used more than once, note that fact for check_dbra_loop. */
3188 if (REGNO (x) >= max_reg_before_loop
3189 || ! VARRAY_RTX (reg_single_usage, REGNO (x))
3190 || VARRAY_RTX (reg_single_usage, REGNO (x)) == const0_rtx)
3191 *((int *) data) = 1;
3194 /* Return nonzero if the rtx X is invariant over the current loop.
3196 The value is 2 if we refer to something only conditionally invariant.
3198 If `unknown_address_altered' is nonzero, no memory ref is invariant.
3199 Otherwise, a memory ref is invariant if it does not conflict with
3200 anything stored in `loop_store_mems'. */
3203 loop_invariant_p (loop, x)
3204 const struct loop *loop;
3205 register rtx x;
3207 register int i;
3208 register enum rtx_code code;
3209 register const char *fmt;
3210 int conditional = 0;
3211 rtx mem_list_entry;
3213 if (x == 0)
3214 return 1;
3215 code = GET_CODE (x);
3216 switch (code)
3218 case CONST_INT:
3219 case CONST_DOUBLE:
3220 case SYMBOL_REF:
3221 case CONST:
3222 return 1;
3224 case LABEL_REF:
3225 /* A LABEL_REF is normally invariant, however, if we are unrolling
3226 loops, and this label is inside the loop, then it isn't invariant.
3227 This is because each unrolled copy of the loop body will have
3228 a copy of this label. If this was invariant, then an insn loading
3229 the address of this label into a register might get moved outside
3230 the loop, and then each loop body would end up using the same label.
3232 We don't know the loop bounds here though, so just fail for all
3233 labels. */
3234 if (flag_unroll_loops)
3235 return 0;
3236 else
3237 return 1;
3239 case PC:
3240 case CC0:
3241 case UNSPEC_VOLATILE:
3242 return 0;
3244 case REG:
3245 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3246 since the reg might be set by initialization within the loop. */
3248 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3249 || x == arg_pointer_rtx)
3250 && ! current_function_has_nonlocal_goto)
3251 return 1;
3253 if (LOOP_INFO (loop)->has_call
3254 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3255 return 0;
3257 if (VARRAY_INT (set_in_loop, REGNO (x)) < 0)
3258 return 2;
3260 return VARRAY_INT (set_in_loop, REGNO (x)) == 0;
3262 case MEM:
3263 /* Volatile memory references must be rejected. Do this before
3264 checking for read-only items, so that volatile read-only items
3265 will be rejected also. */
3266 if (MEM_VOLATILE_P (x))
3267 return 0;
3269 /* If we had a subroutine call, any location in memory could
3270 have been clobbered. We used to test here for volatile and
3271 readonly, but true_dependence knows how to do that better
3272 than we do. */
3273 if (RTX_UNCHANGING_P (x)
3274 ? unknown_constant_address_altered : unknown_address_altered)
3275 return 0;
3277 /* See if there is any dependence between a store and this load. */
3278 mem_list_entry = loop_store_mems;
3279 while (mem_list_entry)
3281 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3282 x, rtx_varies_p))
3283 return 0;
3285 mem_list_entry = XEXP (mem_list_entry, 1);
3288 /* It's not invalidated by a store in memory
3289 but we must still verify the address is invariant. */
3290 break;
3292 case ASM_OPERANDS:
3293 /* Don't mess with insns declared volatile. */
3294 if (MEM_VOLATILE_P (x))
3295 return 0;
3296 break;
3298 default:
3299 break;
3302 fmt = GET_RTX_FORMAT (code);
3303 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3305 if (fmt[i] == 'e')
3307 int tem = loop_invariant_p (loop, XEXP (x, i));
3308 if (tem == 0)
3309 return 0;
3310 if (tem == 2)
3311 conditional = 1;
3313 else if (fmt[i] == 'E')
3315 register int j;
3316 for (j = 0; j < XVECLEN (x, i); j++)
3318 int tem = loop_invariant_p (loop, XVECEXP (x, i, j));
3319 if (tem == 0)
3320 return 0;
3321 if (tem == 2)
3322 conditional = 1;
3328 return 1 + conditional;
3332 /* Return nonzero if all the insns in the loop that set REG
3333 are INSN and the immediately following insns,
3334 and if each of those insns sets REG in an invariant way
3335 (not counting uses of REG in them).
3337 The value is 2 if some of these insns are only conditionally invariant.
3339 We assume that INSN itself is the first set of REG
3340 and that its source is invariant. */
3342 static int
3343 consec_sets_invariant_p (loop, reg, n_sets, insn)
3344 const struct loop *loop;
3345 int n_sets;
3346 rtx reg, insn;
3348 rtx p = insn;
3349 unsigned int regno = REGNO (reg);
3350 rtx temp;
3351 /* Number of sets we have to insist on finding after INSN. */
3352 int count = n_sets - 1;
3353 int old = VARRAY_INT (set_in_loop, regno);
3354 int value = 0;
3355 int this;
3357 /* If N_SETS hit the limit, we can't rely on its value. */
3358 if (n_sets == 127)
3359 return 0;
3361 VARRAY_INT (set_in_loop, regno) = 0;
3363 while (count > 0)
3365 register enum rtx_code code;
3366 rtx set;
3368 p = NEXT_INSN (p);
3369 code = GET_CODE (p);
3371 /* If library call, skip to end of it. */
3372 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3373 p = XEXP (temp, 0);
3375 this = 0;
3376 if (code == INSN
3377 && (set = single_set (p))
3378 && GET_CODE (SET_DEST (set)) == REG
3379 && REGNO (SET_DEST (set)) == regno)
3381 this = loop_invariant_p (loop, SET_SRC (set));
3382 if (this != 0)
3383 value |= this;
3384 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3386 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3387 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3388 notes are OK. */
3389 this = (CONSTANT_P (XEXP (temp, 0))
3390 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3391 && loop_invariant_p (loop, XEXP (temp, 0))));
3392 if (this != 0)
3393 value |= this;
3396 if (this != 0)
3397 count--;
3398 else if (code != NOTE)
3400 VARRAY_INT (set_in_loop, regno) = old;
3401 return 0;
3405 VARRAY_INT (set_in_loop, regno) = old;
3406 /* If loop_invariant_p ever returned 2, we return 2. */
3407 return 1 + (value & 2);
3410 #if 0
3411 /* I don't think this condition is sufficient to allow INSN
3412 to be moved, so we no longer test it. */
3414 /* Return 1 if all insns in the basic block of INSN and following INSN
3415 that set REG are invariant according to TABLE. */
3417 static int
3418 all_sets_invariant_p (reg, insn, table)
3419 rtx reg, insn;
3420 short *table;
3422 register rtx p = insn;
3423 register int regno = REGNO (reg);
3425 while (1)
3427 register enum rtx_code code;
3428 p = NEXT_INSN (p);
3429 code = GET_CODE (p);
3430 if (code == CODE_LABEL || code == JUMP_INSN)
3431 return 1;
3432 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3433 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3434 && REGNO (SET_DEST (PATTERN (p))) == regno)
3436 if (! loop_invariant_p (loop, SET_SRC (PATTERN (p)), table))
3437 return 0;
3441 #endif /* 0 */
3443 /* Look at all uses (not sets) of registers in X. For each, if it is
3444 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3445 a different insn, set USAGE[REGNO] to const0_rtx. */
3447 static void
3448 find_single_use_in_loop (insn, x, usage)
3449 rtx insn;
3450 rtx x;
3451 varray_type usage;
3453 enum rtx_code code = GET_CODE (x);
3454 const char *fmt = GET_RTX_FORMAT (code);
3455 int i, j;
3457 if (code == REG)
3458 VARRAY_RTX (usage, REGNO (x))
3459 = (VARRAY_RTX (usage, REGNO (x)) != 0
3460 && VARRAY_RTX (usage, REGNO (x)) != insn)
3461 ? const0_rtx : insn;
3463 else if (code == SET)
3465 /* Don't count SET_DEST if it is a REG; otherwise count things
3466 in SET_DEST because if a register is partially modified, it won't
3467 show up as a potential movable so we don't care how USAGE is set
3468 for it. */
3469 if (GET_CODE (SET_DEST (x)) != REG)
3470 find_single_use_in_loop (insn, SET_DEST (x), usage);
3471 find_single_use_in_loop (insn, SET_SRC (x), usage);
3473 else
3474 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3476 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3477 find_single_use_in_loop (insn, XEXP (x, i), usage);
3478 else if (fmt[i] == 'E')
3479 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3480 find_single_use_in_loop (insn, XVECEXP (x, i, j), usage);
3484 /* Count and record any set in X which is contained in INSN. Update
3485 MAY_NOT_MOVE and LAST_SET for any register set in X. */
3487 static void
3488 count_one_set (insn, x, may_not_move, last_set)
3489 rtx insn, x;
3490 varray_type may_not_move;
3491 rtx *last_set;
3493 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3494 /* Don't move a reg that has an explicit clobber.
3495 It's not worth the pain to try to do it correctly. */
3496 VARRAY_CHAR (may_not_move, REGNO (XEXP (x, 0))) = 1;
3498 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3500 rtx dest = SET_DEST (x);
3501 while (GET_CODE (dest) == SUBREG
3502 || GET_CODE (dest) == ZERO_EXTRACT
3503 || GET_CODE (dest) == SIGN_EXTRACT
3504 || GET_CODE (dest) == STRICT_LOW_PART)
3505 dest = XEXP (dest, 0);
3506 if (GET_CODE (dest) == REG)
3508 register int regno = REGNO (dest);
3509 /* If this is the first setting of this reg
3510 in current basic block, and it was set before,
3511 it must be set in two basic blocks, so it cannot
3512 be moved out of the loop. */
3513 if (VARRAY_INT (set_in_loop, regno) > 0
3514 && last_set[regno] == 0)
3515 VARRAY_CHAR (may_not_move, regno) = 1;
3516 /* If this is not first setting in current basic block,
3517 see if reg was used in between previous one and this.
3518 If so, neither one can be moved. */
3519 if (last_set[regno] != 0
3520 && reg_used_between_p (dest, last_set[regno], insn))
3521 VARRAY_CHAR (may_not_move, regno) = 1;
3522 if (VARRAY_INT (set_in_loop, regno) < 127)
3523 ++VARRAY_INT (set_in_loop, regno);
3524 last_set[regno] = insn;
3529 /* Increment SET_IN_LOOP at the index of each register
3530 that is modified by an insn between FROM and TO.
3531 If the value of an element of SET_IN_LOOP becomes 127 or more,
3532 stop incrementing it, to avoid overflow.
3534 Store in SINGLE_USAGE[I] the single insn in which register I is
3535 used, if it is only used once. Otherwise, it is set to 0 (for no
3536 uses) or const0_rtx for more than one use. This parameter may be zero,
3537 in which case this processing is not done.
3539 Store in *COUNT_PTR the number of actual instruction
3540 in the loop. We use this to decide what is worth moving out. */
3542 /* last_set[n] is nonzero iff reg n has been set in the current basic block.
3543 In that case, it is the insn that last set reg n. */
3545 static void
3546 count_loop_regs_set (from, to, may_not_move, single_usage, count_ptr, nregs)
3547 register rtx from, to;
3548 varray_type may_not_move;
3549 varray_type single_usage;
3550 int *count_ptr;
3551 int nregs;
3553 register rtx *last_set = (rtx *) xcalloc (nregs, sizeof (rtx));
3554 register rtx insn;
3555 register int count = 0;
3557 for (insn = from; insn != to; insn = NEXT_INSN (insn))
3559 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3561 ++count;
3563 /* Record registers that have exactly one use. */
3564 find_single_use_in_loop (insn, PATTERN (insn), single_usage);
3566 /* Include uses in REG_EQUAL notes. */
3567 if (REG_NOTES (insn))
3568 find_single_use_in_loop (insn, REG_NOTES (insn), single_usage);
3570 if (GET_CODE (PATTERN (insn)) == SET
3571 || GET_CODE (PATTERN (insn)) == CLOBBER)
3572 count_one_set (insn, PATTERN (insn), may_not_move, last_set);
3573 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3575 register int i;
3576 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3577 count_one_set (insn, XVECEXP (PATTERN (insn), 0, i),
3578 may_not_move, last_set);
3582 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
3583 bzero ((char *) last_set, nregs * sizeof (rtx));
3585 *count_ptr = count;
3587 /* Clean up. */
3588 free (last_set);
3591 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3592 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3593 contained in insn INSN is used by any insn that precedes INSN in
3594 cyclic order starting from the loop entry point.
3596 We don't want to use INSN_LUID here because if we restrict INSN to those
3597 that have a valid INSN_LUID, it means we cannot move an invariant out
3598 from an inner loop past two loops. */
3600 static int
3601 loop_reg_used_before_p (loop, set, insn)
3602 const struct loop *loop;
3603 rtx set, insn;
3605 rtx reg = SET_DEST (set);
3606 rtx p;
3608 /* Scan forward checking for register usage. If we hit INSN, we
3609 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3610 for (p = loop->scan_start; p != insn; p = NEXT_INSN (p))
3612 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
3613 && reg_overlap_mentioned_p (reg, PATTERN (p)))
3614 return 1;
3616 if (p == loop->end)
3617 p = loop->start;
3620 return 0;
3623 /* A "basic induction variable" or biv is a pseudo reg that is set
3624 (within this loop) only by incrementing or decrementing it. */
3625 /* A "general induction variable" or giv is a pseudo reg whose
3626 value is a linear function of a biv. */
3628 /* Bivs are recognized by `basic_induction_var';
3629 Givs by `general_induction_var'. */
3631 /* Indexed by register number, indicates whether or not register is an
3632 induction variable, and if so what type. */
3634 varray_type reg_iv_type;
3636 /* Indexed by register number, contains pointer to `struct induction'
3637 if register is an induction variable. This holds general info for
3638 all induction variables. */
3640 varray_type reg_iv_info;
3642 /* Indexed by register number, contains pointer to `struct iv_class'
3643 if register is a basic induction variable. This holds info describing
3644 the class (a related group) of induction variables that the biv belongs
3645 to. */
3647 struct iv_class **reg_biv_class;
3649 /* The head of a list which links together (via the next field)
3650 every iv class for the current loop. */
3652 struct iv_class *loop_iv_list;
3654 /* Givs made from biv increments are always splittable for loop unrolling.
3655 Since there is no regscan info for them, we have to keep track of them
3656 separately. */
3657 unsigned int first_increment_giv, last_increment_giv;
3659 /* Communication with routines called via `note_stores'. */
3661 static rtx note_insn;
3663 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3665 static rtx addr_placeholder;
3667 /* ??? Unfinished optimizations, and possible future optimizations,
3668 for the strength reduction code. */
3670 /* ??? The interaction of biv elimination, and recognition of 'constant'
3671 bivs, may cause problems. */
3673 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3674 performance problems.
3676 Perhaps don't eliminate things that can be combined with an addressing
3677 mode. Find all givs that have the same biv, mult_val, and add_val;
3678 then for each giv, check to see if its only use dies in a following
3679 memory address. If so, generate a new memory address and check to see
3680 if it is valid. If it is valid, then store the modified memory address,
3681 otherwise, mark the giv as not done so that it will get its own iv. */
3683 /* ??? Could try to optimize branches when it is known that a biv is always
3684 positive. */
3686 /* ??? When replace a biv in a compare insn, we should replace with closest
3687 giv so that an optimized branch can still be recognized by the combiner,
3688 e.g. the VAX acb insn. */
3690 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3691 was rerun in loop_optimize whenever a register was added or moved.
3692 Also, some of the optimizations could be a little less conservative. */
3694 /* Scan the loop body and call FNCALL for each insn. In the addition to the
3695 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
3696 callback.
3698 NOT_EVERY_ITERATION if current insn is not executed at least once for every
3699 loop iteration except for the last one.
3701 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
3702 loop iteration.
3704 void
3705 for_each_insn_in_loop (loop, fncall)
3706 struct loop *loop;
3707 loop_insn_callback fncall;
3709 /* This is 1 if current insn is not executed at least once for every loop
3710 iteration. */
3711 int not_every_iteration = 0;
3712 int maybe_multiple = 0;
3713 int past_loop_latch = 0;
3714 int loop_depth = 0;
3715 rtx p;
3717 /* If loop_scan_start points to the loop exit test, we have to be wary of
3718 subversive use of gotos inside expression statements. */
3719 if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
3720 maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
3722 /* Scan through loop to find all possible bivs. */
3724 for (p = next_insn_in_loop (loop, loop->scan_start);
3725 p != NULL_RTX;
3726 p = next_insn_in_loop (loop, p))
3728 p = fncall (loop, p, not_every_iteration, maybe_multiple);
3730 /* Past CODE_LABEL, we get to insns that may be executed multiple
3731 times. The only way we can be sure that they can't is if every
3732 jump insn between here and the end of the loop either
3733 returns, exits the loop, is a jump to a location that is still
3734 behind the label, or is a jump to the loop start. */
3736 if (GET_CODE (p) == CODE_LABEL)
3738 rtx insn = p;
3740 maybe_multiple = 0;
3742 while (1)
3744 insn = NEXT_INSN (insn);
3745 if (insn == loop->scan_start)
3746 break;
3747 if (insn == loop->end)
3749 if (loop->top != 0)
3750 insn = loop->top;
3751 else
3752 break;
3753 if (insn == loop->scan_start)
3754 break;
3757 if (GET_CODE (insn) == JUMP_INSN
3758 && GET_CODE (PATTERN (insn)) != RETURN
3759 && (!any_condjump_p (insn)
3760 || (JUMP_LABEL (insn) != 0
3761 && JUMP_LABEL (insn) != loop->scan_start
3762 && !loop_insn_first_p (p, JUMP_LABEL (insn)))))
3764 maybe_multiple = 1;
3765 break;
3770 /* Past a jump, we get to insns for which we can't count
3771 on whether they will be executed during each iteration. */
3772 /* This code appears twice in strength_reduce. There is also similar
3773 code in scan_loop. */
3774 if (GET_CODE (p) == JUMP_INSN
3775 /* If we enter the loop in the middle, and scan around to the
3776 beginning, don't set not_every_iteration for that.
3777 This can be any kind of jump, since we want to know if insns
3778 will be executed if the loop is executed. */
3779 && !(JUMP_LABEL (p) == loop->top
3780 && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
3781 && any_uncondjump_p (p))
3782 || (NEXT_INSN (p) == loop->end && any_condjump_p (p)))))
3784 rtx label = 0;
3786 /* If this is a jump outside the loop, then it also doesn't
3787 matter. Check to see if the target of this branch is on the
3788 loop->exits_labels list. */
3790 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
3791 if (XEXP (label, 0) == JUMP_LABEL (p))
3792 break;
3794 if (!label)
3795 not_every_iteration = 1;
3798 else if (GET_CODE (p) == NOTE)
3800 /* At the virtual top of a converted loop, insns are again known to
3801 be executed each iteration: logically, the loop begins here
3802 even though the exit code has been duplicated.
3804 Insns are also again known to be executed each iteration at
3805 the LOOP_CONT note. */
3806 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
3807 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
3808 && loop_depth == 0)
3809 not_every_iteration = 0;
3810 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3811 loop_depth++;
3812 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3813 loop_depth--;
3816 /* Note if we pass a loop latch. If we do, then we can not clear
3817 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
3818 a loop since a jump before the last CODE_LABEL may have started
3819 a new loop iteration.
3821 Note that LOOP_TOP is only set for rotated loops and we need
3822 this check for all loops, so compare against the CODE_LABEL
3823 which immediately follows LOOP_START. */
3824 if (GET_CODE (p) == JUMP_INSN
3825 && JUMP_LABEL (p) == NEXT_INSN (loop->start))
3826 past_loop_latch = 1;
3828 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3829 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3830 or not an insn is known to be executed each iteration of the
3831 loop, whether or not any iterations are known to occur.
3833 Therefore, if we have just passed a label and have no more labels
3834 between here and the test insn of the loop, and we have not passed
3835 a jump to the top of the loop, then we know these insns will be
3836 executed each iteration. */
3838 if (not_every_iteration
3839 && !past_loop_latch
3840 && GET_CODE (p) == CODE_LABEL
3841 && no_labels_between_p (p, loop->end)
3842 && loop_insn_first_p (p, loop->cont))
3843 not_every_iteration = 0;
3847 /* Perform strength reduction and induction variable elimination.
3849 Pseudo registers created during this function will be beyond the last
3850 valid index in several tables including n_times_set and regno_last_uid.
3851 This does not cause a problem here, because the added registers cannot be
3852 givs outside of their loop, and hence will never be reconsidered.
3853 But scan_loop must check regnos to make sure they are in bounds. */
3855 static void
3856 strength_reduce (loop, insn_count, flags)
3857 struct loop *loop;
3858 int insn_count;
3859 int flags;
3861 rtx p;
3862 /* Temporary list pointers for traversing loop_iv_list. */
3863 struct iv_class *bl, **backbl;
3864 struct loop_info *loop_info = LOOP_INFO (loop);
3865 /* Ratio of extra register life span we can justify
3866 for saving an instruction. More if loop doesn't call subroutines
3867 since in that case saving an insn makes more difference
3868 and more registers are available. */
3869 /* ??? could set this to last value of threshold in move_movables */
3870 int threshold = (loop_info->has_call ? 1 : 2) * (3 + n_non_fixed_regs);
3871 /* Map of pseudo-register replacements. */
3872 rtx *reg_map = NULL;
3873 int reg_map_size;
3874 int call_seen;
3875 rtx test;
3876 rtx end_insert_before;
3877 int n_extra_increment;
3878 int unrolled_insn_copies = 0;
3879 rtx loop_start = loop->start;
3880 rtx loop_end = loop->end;
3881 rtx loop_scan_start = loop->scan_start;
3883 VARRAY_INT_INIT (reg_iv_type, max_reg_before_loop, "reg_iv_type");
3884 VARRAY_GENERIC_PTR_INIT (reg_iv_info, max_reg_before_loop, "reg_iv_info");
3885 reg_biv_class = (struct iv_class **)
3886 xcalloc (max_reg_before_loop, sizeof (struct iv_class *));
3888 loop_iv_list = 0;
3889 addr_placeholder = gen_reg_rtx (Pmode);
3891 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3892 must be put before this insn, so that they will appear in the right
3893 order (i.e. loop order).
3895 If loop_end is the end of the current function, then emit a
3896 NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3897 dummy note insn. */
3898 if (NEXT_INSN (loop_end) != 0)
3899 end_insert_before = NEXT_INSN (loop_end);
3900 else
3901 end_insert_before = emit_note_after (NOTE_INSN_DELETED, loop_end);
3903 for_each_insn_in_loop (loop, check_insn_for_bivs);
3905 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3906 Make a sanity check against n_times_set. */
3907 for (backbl = &loop_iv_list, bl = *backbl; bl; bl = bl->next)
3909 if (REG_IV_TYPE (bl->regno) != BASIC_INDUCT
3910 /* Above happens if register modified by subreg, etc. */
3911 /* Make sure it is not recognized as a basic induction var: */
3912 || VARRAY_INT (n_times_set, bl->regno) != bl->biv_count
3913 /* If never incremented, it is invariant that we decided not to
3914 move. So leave it alone. */
3915 || ! bl->incremented)
3917 if (loop_dump_stream)
3918 fprintf (loop_dump_stream, "Reg %d: biv discarded, %s\n",
3919 bl->regno,
3920 (REG_IV_TYPE (bl->regno) != BASIC_INDUCT
3921 ? "not induction variable"
3922 : (! bl->incremented ? "never incremented"
3923 : "count error")));
3925 REG_IV_TYPE (bl->regno) = NOT_BASIC_INDUCT;
3926 *backbl = bl->next;
3928 else
3930 backbl = &bl->next;
3932 if (loop_dump_stream)
3933 fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
3937 /* Exit if there are no bivs. */
3938 if (! loop_iv_list)
3940 /* Can still unroll the loop anyways, but indicate that there is no
3941 strength reduction info available. */
3942 if (flags & LOOP_UNROLL)
3943 unroll_loop (loop, insn_count, end_insert_before, 0);
3945 goto egress;
3948 /* Find initial value for each biv by searching backwards from loop_start,
3949 halting at first label. Also record any test condition. */
3951 call_seen = 0;
3952 for (p = loop_start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3954 note_insn = p;
3956 if (GET_CODE (p) == CALL_INSN)
3957 call_seen = 1;
3959 if (INSN_P (p))
3960 note_stores (PATTERN (p), record_initial, NULL);
3962 /* Record any test of a biv that branches around the loop if no store
3963 between it and the start of loop. We only care about tests with
3964 constants and registers and only certain of those. */
3965 if (GET_CODE (p) == JUMP_INSN
3966 && JUMP_LABEL (p) != 0
3967 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end)
3968 && (test = get_condition_for_loop (loop, p)) != 0
3969 && GET_CODE (XEXP (test, 0)) == REG
3970 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3971 && (bl = reg_biv_class[REGNO (XEXP (test, 0))]) != 0
3972 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop_start)
3973 && bl->init_insn == 0)
3975 /* If an NE test, we have an initial value! */
3976 if (GET_CODE (test) == NE)
3978 bl->init_insn = p;
3979 bl->init_set = gen_rtx_SET (VOIDmode,
3980 XEXP (test, 0), XEXP (test, 1));
3982 else
3983 bl->initial_test = test;
3987 /* Look at the each biv and see if we can say anything better about its
3988 initial value from any initializing insns set up above. (This is done
3989 in two passes to avoid missing SETs in a PARALLEL.) */
3990 for (backbl = &loop_iv_list; (bl = *backbl); backbl = &bl->next)
3992 rtx src;
3993 rtx note;
3995 if (! bl->init_insn)
3996 continue;
3998 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
3999 is a constant, use the value of that. */
4000 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
4001 && CONSTANT_P (XEXP (note, 0)))
4002 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
4003 && CONSTANT_P (XEXP (note, 0))))
4004 src = XEXP (note, 0);
4005 else
4006 src = SET_SRC (bl->init_set);
4008 if (loop_dump_stream)
4009 fprintf (loop_dump_stream,
4010 "Biv %d initialized at insn %d: initial value ",
4011 bl->regno, INSN_UID (bl->init_insn));
4013 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
4014 || GET_MODE (src) == VOIDmode)
4015 && valid_initial_value_p (src, bl->init_insn, call_seen, loop_start))
4017 bl->initial_value = src;
4019 if (loop_dump_stream)
4021 if (GET_CODE (src) == CONST_INT)
4023 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (src));
4024 fputc ('\n', loop_dump_stream);
4026 else
4028 print_rtl (loop_dump_stream, src);
4029 fprintf (loop_dump_stream, "\n");
4033 else
4035 struct iv_class *bl2 = 0;
4036 rtx increment = NULL_RTX;
4038 /* Biv initial value is not a simple move. If it is the sum of
4039 another biv and a constant, check if both bivs are incremented
4040 in lockstep. Then we are actually looking at a giv.
4041 For simplicity, we only handle the case where there is but a
4042 single increment, and the register is not used elsewhere. */
4043 if (bl->biv_count == 1
4044 && bl->regno < max_reg_before_loop
4045 && uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
4046 && GET_CODE (src) == PLUS
4047 && GET_CODE (XEXP (src, 0)) == REG
4048 && CONSTANT_P (XEXP (src, 1))
4049 && ((increment = biv_total_increment (bl)) != NULL_RTX))
4051 unsigned int regno = REGNO (XEXP (src, 0));
4053 for (bl2 = loop_iv_list; bl2; bl2 = bl2->next)
4054 if (bl2->regno == regno)
4055 break;
4058 /* Now, can we transform this biv into a giv? */
4059 if (bl2
4060 && bl2->biv_count == 1
4061 && rtx_equal_p (increment, biv_total_increment (bl2))
4062 /* init_insn is only set to insns that are before loop_start
4063 without any intervening labels. */
4064 && ! reg_set_between_p (bl2->biv->src_reg,
4065 PREV_INSN (bl->init_insn), loop_start)
4066 /* The register from BL2 must be set before the register from
4067 BL is set, or we must be able to move the latter set after
4068 the former set. Currently there can't be any labels
4069 in-between when biv_total_increment returns nonzero both times
4070 but we test it here in case some day some real cfg analysis
4071 gets used to set always_computable. */
4072 && (loop_insn_first_p (bl2->biv->insn, bl->biv->insn)
4073 ? no_labels_between_p (bl2->biv->insn, bl->biv->insn)
4074 : (! reg_used_between_p (bl->biv->src_reg, bl->biv->insn,
4075 bl2->biv->insn)
4076 && no_jumps_between_p (bl->biv->insn, bl2->biv->insn)))
4077 && validate_change (bl->biv->insn,
4078 &SET_SRC (single_set (bl->biv->insn)),
4079 copy_rtx (src), 0))
4081 rtx dominator = loop->cont_dominator;
4082 rtx giv = bl->biv->src_reg;
4083 rtx giv_insn = bl->biv->insn;
4084 rtx after_giv = NEXT_INSN (giv_insn);
4086 if (loop_dump_stream)
4087 fprintf (loop_dump_stream, "is giv of biv %d\n", bl2->regno);
4088 /* Let this giv be discovered by the generic code. */
4089 REG_IV_TYPE (bl->regno) = UNKNOWN_INDUCT;
4090 reg_biv_class[bl->regno] = (struct iv_class *) NULL_PTR;
4091 /* We can get better optimization if we can move the giv setting
4092 before the first giv use. */
4093 if (dominator
4094 && ! loop_insn_first_p (dominator, loop_scan_start)
4095 && ! reg_set_between_p (bl2->biv->src_reg, loop_start,
4096 dominator)
4097 && ! reg_used_between_p (giv, loop_start, dominator)
4098 && ! reg_used_between_p (giv, giv_insn, loop_end))
4100 rtx p;
4101 rtx next;
4103 for (next = NEXT_INSN (dominator); ; next = NEXT_INSN (next))
4105 if (GET_CODE (next) == JUMP_INSN
4106 || (INSN_P (next)
4107 && insn_dependant_p (giv_insn, next)))
4108 break;
4109 #ifdef HAVE_cc0
4110 if (! INSN_P (next)
4111 || ! sets_cc0_p (PATTERN (next)))
4112 #endif
4113 dominator = next;
4115 if (loop_dump_stream)
4116 fprintf (loop_dump_stream, "move after insn %d\n",
4117 INSN_UID (dominator));
4118 /* Avoid problems with luids by actually moving the insn
4119 and adjusting all luids in the range. */
4120 reorder_insns (giv_insn, giv_insn, dominator);
4121 for (p = dominator; INSN_UID (p) >= max_uid_for_loop; )
4122 p = PREV_INSN (p);
4123 compute_luids (giv_insn, after_giv, INSN_LUID (p));
4124 /* If the only purpose of the init insn is to initialize
4125 this giv, delete it. */
4126 if (single_set (bl->init_insn)
4127 && ! reg_used_between_p (giv, bl->init_insn, loop_start))
4128 delete_insn (bl->init_insn);
4130 else if (! loop_insn_first_p (bl2->biv->insn, bl->biv->insn))
4132 rtx p = PREV_INSN (giv_insn);
4133 while (INSN_UID (p) >= max_uid_for_loop)
4134 p = PREV_INSN (p);
4135 reorder_insns (giv_insn, giv_insn, bl2->biv->insn);
4136 compute_luids (after_giv, NEXT_INSN (giv_insn),
4137 INSN_LUID (p));
4139 /* Remove this biv from the chain. */
4140 *backbl = bl->next;
4143 /* If we can't make it a giv,
4144 let biv keep initial value of "itself". */
4145 else if (loop_dump_stream)
4146 fprintf (loop_dump_stream, "is complex\n");
4150 /* If a biv is unconditionally incremented several times in a row, convert
4151 all but the last increment into a giv. */
4153 /* Get an upper bound for the number of registers
4154 we might have after all bivs have been processed. */
4155 first_increment_giv = max_reg_num ();
4156 for (n_extra_increment = 0, bl = loop_iv_list; bl; bl = bl->next)
4157 n_extra_increment += bl->biv_count - 1;
4159 /* If the loop contains volatile memory references do not allow any
4160 replacements to take place, since this could loose the volatile
4161 markers. */
4162 if (n_extra_increment && ! loop_info->has_volatile)
4164 unsigned int nregs = first_increment_giv + n_extra_increment;
4166 /* Reallocate reg_iv_type and reg_iv_info. */
4167 VARRAY_GROW (reg_iv_type, nregs);
4168 VARRAY_GROW (reg_iv_info, nregs);
4170 for (bl = loop_iv_list; bl; bl = bl->next)
4172 struct induction **vp, *v, *next;
4173 int biv_dead_after_loop = 0;
4175 /* The biv increments lists are in reverse order. Fix this
4176 first. */
4177 for (v = bl->biv, bl->biv = 0; v; v = next)
4179 next = v->next_iv;
4180 v->next_iv = bl->biv;
4181 bl->biv = v;
4184 /* We must guard against the case that an early exit between v->insn
4185 and next->insn leaves the biv live after the loop, since that
4186 would mean that we'd be missing an increment for the final
4187 value. The following test to set biv_dead_after_loop is like
4188 the first part of the test to set bl->eliminable.
4189 We don't check here if we can calculate the final value, since
4190 this can't succeed if we already know that there is a jump
4191 between v->insn and next->insn, yet next->always_executed is
4192 set and next->maybe_multiple is cleared. Such a combination
4193 implies that the jump destination is outside the loop.
4194 If we want to make this check more sophisticated, we should
4195 check each branch between v->insn and next->insn individually
4196 to see if the biv is dead at its destination. */
4198 if (uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
4199 && bl->init_insn
4200 && INSN_UID (bl->init_insn) < max_uid_for_loop
4201 && (uid_luid[REGNO_FIRST_UID (bl->regno)]
4202 >= INSN_LUID (bl->init_insn))
4203 #ifdef HAVE_decrement_and_branch_until_zero
4204 && ! bl->nonneg
4205 #endif
4206 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
4207 biv_dead_after_loop = 1;
4209 for (vp = &bl->biv, next = *vp; v = next, next = v->next_iv;)
4211 HOST_WIDE_INT offset;
4212 rtx set, add_val, old_reg, dest_reg, last_use_insn, note;
4213 int old_regno, new_regno;
4214 rtx next_loc_insn;
4216 if (! v->always_executed
4217 || v->maybe_multiple
4218 || GET_CODE (v->add_val) != CONST_INT
4219 || ! next->always_executed
4220 || next->maybe_multiple
4221 || ! CONSTANT_P (next->add_val)
4222 || v->mult_val != const1_rtx
4223 || next->mult_val != const1_rtx
4224 || ! (biv_dead_after_loop
4225 || no_jumps_between_p (v->insn, next->insn)))
4227 vp = &v->next_iv;
4228 continue;
4230 offset = INTVAL (v->add_val);
4231 set = single_set (v->insn);
4232 add_val = plus_constant (next->add_val, offset);
4233 old_reg = v->dest_reg;
4234 dest_reg = gen_reg_rtx (v->mode);
4236 /* Unlike reg_iv_type / reg_iv_info, the other three arrays
4237 have been allocated with some slop space, so we may not
4238 actually need to reallocate them. If we do, the following
4239 if statement will be executed just once in this loop. */
4240 if ((unsigned) max_reg_num () > n_times_set->num_elements)
4242 /* Grow all the remaining arrays. */
4243 VARRAY_GROW (set_in_loop, nregs);
4244 VARRAY_GROW (n_times_set, nregs);
4245 VARRAY_GROW (may_not_optimize, nregs);
4246 VARRAY_GROW (reg_single_usage, nregs);
4249 /* Some bivs are incremented with a multi-insn sequence.
4250 The first insn contains the add. */
4251 next_loc_insn = next->insn;
4252 while (! loc_mentioned_in_p (next->location,
4253 PATTERN (next_loc_insn)))
4254 next_loc_insn = PREV_INSN (next_loc_insn);
4256 if (next_loc_insn == v->insn)
4257 abort ();
4259 if (! validate_change (next_loc_insn, next->location, add_val, 0))
4261 vp = &v->next_iv;
4262 continue;
4265 /* Here we can try to eliminate the increment by combining
4266 it into the uses. */
4268 /* Set last_use_insn so that we can check against it. */
4270 for (last_use_insn = v->insn, p = NEXT_INSN (v->insn);
4271 p != next_loc_insn;
4272 p = next_insn_in_loop (loop, p))
4274 if (!INSN_P (p))
4275 continue;
4276 if (reg_mentioned_p (old_reg, PATTERN (p)))
4278 last_use_insn = p;
4282 /* If we can't get the LUIDs for the insns, we can't
4283 calculate the lifetime. This is likely from unrolling
4284 of an inner loop, so there is little point in making this
4285 a DEST_REG giv anyways. */
4286 if (INSN_UID (v->insn) >= max_uid_for_loop
4287 || INSN_UID (last_use_insn) >= max_uid_for_loop
4288 || ! validate_change (v->insn, &SET_DEST (set), dest_reg, 0))
4290 /* Change the increment at NEXT back to what it was. */
4291 if (! validate_change (next_loc_insn, next->location,
4292 next->add_val, 0))
4293 abort ();
4294 vp = &v->next_iv;
4295 continue;
4297 next->add_val = add_val;
4298 v->dest_reg = dest_reg;
4299 v->giv_type = DEST_REG;
4300 v->location = &SET_SRC (set);
4301 v->cant_derive = 0;
4302 v->combined_with = 0;
4303 v->maybe_dead = 0;
4304 v->derive_adjustment = 0;
4305 v->same = 0;
4306 v->ignore = 0;
4307 v->new_reg = 0;
4308 v->final_value = 0;
4309 v->same_insn = 0;
4310 v->auto_inc_opt = 0;
4311 v->unrolled = 0;
4312 v->shared = 0;
4313 v->derived_from = 0;
4314 v->always_computable = 1;
4315 v->always_executed = 1;
4316 v->replaceable = 1;
4317 v->no_const_addval = 0;
4319 old_regno = REGNO (old_reg);
4320 new_regno = REGNO (dest_reg);
4321 VARRAY_INT (set_in_loop, old_regno)--;
4322 VARRAY_INT (set_in_loop, new_regno) = 1;
4323 VARRAY_INT (n_times_set, old_regno)--;
4324 VARRAY_INT (n_times_set, new_regno) = 1;
4325 VARRAY_CHAR (may_not_optimize, new_regno) = 0;
4327 REG_IV_TYPE (new_regno) = GENERAL_INDUCT;
4328 REG_IV_INFO (new_regno) = v;
4330 /* If next_insn has a REG_EQUAL note that mentiones OLD_REG,
4331 it must be replaced. */
4332 note = find_reg_note (next->insn, REG_EQUAL, NULL_RTX);
4333 if (note && reg_mentioned_p (old_reg, XEXP (note, 0)))
4334 XEXP (note, 0) = copy_rtx (SET_SRC (single_set (next->insn)));
4336 /* Remove the increment from the list of biv increments,
4337 and record it as a giv. */
4338 *vp = next;
4339 bl->biv_count--;
4340 v->next_iv = bl->giv;
4341 bl->giv = v;
4342 bl->giv_count++;
4343 v->benefit = rtx_cost (SET_SRC (set), SET);
4344 bl->total_benefit += v->benefit;
4346 /* Now replace the biv with DEST_REG in all insns between
4347 the replaced increment and the next increment, and
4348 remember the last insn that needed a replacement. */
4349 for (last_use_insn = v->insn, p = NEXT_INSN (v->insn);
4350 p != next_loc_insn;
4351 p = next_insn_in_loop (loop, p))
4353 rtx note;
4355 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
4356 continue;
4357 if (reg_mentioned_p (old_reg, PATTERN (p)))
4359 last_use_insn = p;
4360 if (! validate_replace_rtx (old_reg, dest_reg, p))
4361 abort ();
4363 for (note = REG_NOTES (p); note; note = XEXP (note, 1))
4365 if (GET_CODE (note) == EXPR_LIST)
4366 XEXP (note, 0)
4367 = replace_rtx (XEXP (note, 0), old_reg, dest_reg);
4371 v->last_use = last_use_insn;
4372 v->lifetime = INSN_LUID (last_use_insn) - INSN_LUID (v->insn);
4373 /* If the lifetime is zero, it means that this register is really
4374 a dead store. So mark this as a giv that can be ignored.
4375 This will not prevent the biv from being eliminated. */
4376 if (v->lifetime == 0)
4377 v->ignore = 1;
4379 if (loop_dump_stream)
4380 fprintf (loop_dump_stream,
4381 "Increment %d of biv %d converted to giv %d.\n\n",
4382 INSN_UID (v->insn), old_regno, new_regno);
4386 last_increment_giv = max_reg_num () - 1;
4388 /* Search the loop for general induction variables. */
4390 for_each_insn_in_loop (loop, check_insn_for_givs);
4392 /* Try to calculate and save the number of loop iterations. This is
4393 set to zero if the actual number can not be calculated. This must
4394 be called after all giv's have been identified, since otherwise it may
4395 fail if the iteration variable is a giv. */
4397 loop_iterations (loop);
4399 /* Now for each giv for which we still don't know whether or not it is
4400 replaceable, check to see if it is replaceable because its final value
4401 can be calculated. This must be done after loop_iterations is called,
4402 so that final_giv_value will work correctly. */
4404 for (bl = loop_iv_list; bl; bl = bl->next)
4406 struct induction *v;
4408 for (v = bl->giv; v; v = v->next_iv)
4409 if (! v->replaceable && ! v->not_replaceable)
4410 check_final_value (loop, v);
4413 /* Try to prove that the loop counter variable (if any) is always
4414 nonnegative; if so, record that fact with a REG_NONNEG note
4415 so that "decrement and branch until zero" insn can be used. */
4416 check_dbra_loop (loop, insn_count);
4418 /* Create reg_map to hold substitutions for replaceable giv regs.
4419 Some givs might have been made from biv increments, so look at
4420 reg_iv_type for a suitable size. */
4421 reg_map_size = reg_iv_type->num_elements;
4422 reg_map = (rtx *) xcalloc (reg_map_size, sizeof (rtx));
4424 /* Examine each iv class for feasibility of strength reduction/induction
4425 variable elimination. */
4427 for (bl = loop_iv_list; bl; bl = bl->next)
4429 struct induction *v;
4430 int benefit;
4431 int all_reduced;
4432 rtx final_value = 0;
4433 unsigned int nregs;
4435 /* Test whether it will be possible to eliminate this biv
4436 provided all givs are reduced. This is possible if either
4437 the reg is not used outside the loop, or we can compute
4438 what its final value will be.
4440 For architectures with a decrement_and_branch_until_zero insn,
4441 don't do this if we put a REG_NONNEG note on the endtest for
4442 this biv. */
4444 /* Compare against bl->init_insn rather than loop_start.
4445 We aren't concerned with any uses of the biv between
4446 init_insn and loop_start since these won't be affected
4447 by the value of the biv elsewhere in the function, so
4448 long as init_insn doesn't use the biv itself.
4449 March 14, 1989 -- self@bayes.arc.nasa.gov */
4451 if ((uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
4452 && bl->init_insn
4453 && INSN_UID (bl->init_insn) < max_uid_for_loop
4454 && uid_luid[REGNO_FIRST_UID (bl->regno)] >= INSN_LUID (bl->init_insn)
4455 #ifdef HAVE_decrement_and_branch_until_zero
4456 && ! bl->nonneg
4457 #endif
4458 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
4459 || ((final_value = final_biv_value (loop, bl))
4460 #ifdef HAVE_decrement_and_branch_until_zero
4461 && ! bl->nonneg
4462 #endif
4464 bl->eliminable = maybe_eliminate_biv (loop, bl, 0, threshold,
4465 insn_count);
4466 else
4468 if (loop_dump_stream)
4470 fprintf (loop_dump_stream,
4471 "Cannot eliminate biv %d.\n",
4472 bl->regno);
4473 fprintf (loop_dump_stream,
4474 "First use: insn %d, last use: insn %d.\n",
4475 REGNO_FIRST_UID (bl->regno),
4476 REGNO_LAST_UID (bl->regno));
4480 /* Combine all giv's for this iv_class. */
4481 combine_givs (bl);
4483 /* This will be true at the end, if all givs which depend on this
4484 biv have been strength reduced.
4485 We can't (currently) eliminate the biv unless this is so. */
4486 all_reduced = 1;
4488 /* Check each giv in this class to see if we will benefit by reducing
4489 it. Skip giv's combined with others. */
4490 for (v = bl->giv; v; v = v->next_iv)
4492 struct induction *tv;
4494 if (v->ignore || v->same)
4495 continue;
4497 benefit = v->benefit;
4499 /* Reduce benefit if not replaceable, since we will insert
4500 a move-insn to replace the insn that calculates this giv.
4501 Don't do this unless the giv is a user variable, since it
4502 will often be marked non-replaceable because of the duplication
4503 of the exit code outside the loop. In such a case, the copies
4504 we insert are dead and will be deleted. So they don't have
4505 a cost. Similar situations exist. */
4506 /* ??? The new final_[bg]iv_value code does a much better job
4507 of finding replaceable giv's, and hence this code may no longer
4508 be necessary. */
4509 if (! v->replaceable && ! bl->eliminable
4510 && REG_USERVAR_P (v->dest_reg))
4511 benefit -= copy_cost;
4513 /* Decrease the benefit to count the add-insns that we will
4514 insert to increment the reduced reg for the giv. */
4515 benefit -= add_cost * bl->biv_count;
4517 /* Decide whether to strength-reduce this giv or to leave the code
4518 unchanged (recompute it from the biv each time it is used).
4519 This decision can be made independently for each giv. */
4521 #ifdef AUTO_INC_DEC
4522 /* Attempt to guess whether autoincrement will handle some of the
4523 new add insns; if so, increase BENEFIT (undo the subtraction of
4524 add_cost that was done above). */
4525 if (v->giv_type == DEST_ADDR
4526 && GET_CODE (v->mult_val) == CONST_INT)
4528 if (HAVE_POST_INCREMENT
4529 && INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4530 benefit += add_cost * bl->biv_count;
4531 else if (HAVE_PRE_INCREMENT
4532 && INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4533 benefit += add_cost * bl->biv_count;
4534 else if (HAVE_POST_DECREMENT
4535 && -INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4536 benefit += add_cost * bl->biv_count;
4537 else if (HAVE_PRE_DECREMENT
4538 && -INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4539 benefit += add_cost * bl->biv_count;
4541 #endif
4543 /* If an insn is not to be strength reduced, then set its ignore
4544 flag, and clear all_reduced. */
4546 /* A giv that depends on a reversed biv must be reduced if it is
4547 used after the loop exit, otherwise, it would have the wrong
4548 value after the loop exit. To make it simple, just reduce all
4549 of such giv's whether or not we know they are used after the loop
4550 exit. */
4552 if ( ! flag_reduce_all_givs && v->lifetime * threshold * benefit < insn_count
4553 && ! bl->reversed )
4555 if (loop_dump_stream)
4556 fprintf (loop_dump_stream,
4557 "giv of insn %d not worth while, %d vs %d.\n",
4558 INSN_UID (v->insn),
4559 v->lifetime * threshold * benefit, insn_count);
4560 v->ignore = 1;
4561 all_reduced = 0;
4563 else
4565 /* Check that we can increment the reduced giv without a
4566 multiply insn. If not, reject it. */
4568 for (tv = bl->biv; tv; tv = tv->next_iv)
4569 if (tv->mult_val == const1_rtx
4570 && ! product_cheap_p (tv->add_val, v->mult_val))
4572 if (loop_dump_stream)
4573 fprintf (loop_dump_stream,
4574 "giv of insn %d: would need a multiply.\n",
4575 INSN_UID (v->insn));
4576 v->ignore = 1;
4577 all_reduced = 0;
4578 break;
4583 /* Check for givs whose first use is their definition and whose
4584 last use is the definition of another giv. If so, it is likely
4585 dead and should not be used to derive another giv nor to
4586 eliminate a biv. */
4587 for (v = bl->giv; v; v = v->next_iv)
4589 if (v->ignore
4590 || (v->same && v->same->ignore))
4591 continue;
4593 if (v->last_use)
4595 struct induction *v1;
4597 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4598 if (v->last_use == v1->insn)
4599 v->maybe_dead = 1;
4601 else if (v->giv_type == DEST_REG
4602 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4604 struct induction *v1;
4606 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4607 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4608 v->maybe_dead = 1;
4612 /* Now that we know which givs will be reduced, try to rearrange the
4613 combinations to reduce register pressure.
4614 recombine_givs calls find_life_end, which needs reg_iv_type and
4615 reg_iv_info to be valid for all pseudos. We do the necessary
4616 reallocation here since it allows to check if there are still
4617 more bivs to process. */
4618 nregs = max_reg_num ();
4619 if (nregs > reg_iv_type->num_elements)
4621 /* If there are still more bivs to process, allocate some slack
4622 space so that we're not constantly reallocating these arrays. */
4623 if (bl->next)
4624 nregs += nregs / 4;
4625 /* Reallocate reg_iv_type and reg_iv_info. */
4626 VARRAY_GROW (reg_iv_type, nregs);
4627 VARRAY_GROW (reg_iv_info, nregs);
4629 recombine_givs (loop, bl, flags & LOOP_UNROLL);
4631 /* Reduce each giv that we decided to reduce. */
4633 for (v = bl->giv; v; v = v->next_iv)
4635 struct induction *tv;
4636 if (! v->ignore && v->same == 0)
4638 int auto_inc_opt = 0;
4640 /* If the code for derived givs immediately below has already
4641 allocated a new_reg, we must keep it. */
4642 if (! v->new_reg)
4643 v->new_reg = gen_reg_rtx (v->mode);
4645 if (v->derived_from)
4647 struct induction *d = v->derived_from;
4649 /* In case d->dest_reg is not replaceable, we have
4650 to replace it in v->insn now. */
4651 if (! d->new_reg)
4652 d->new_reg = gen_reg_rtx (d->mode);
4653 PATTERN (v->insn)
4654 = replace_rtx (PATTERN (v->insn), d->dest_reg, d->new_reg);
4655 PATTERN (v->insn)
4656 = replace_rtx (PATTERN (v->insn), v->dest_reg, v->new_reg);
4657 /* For each place where the biv is incremented, add an
4658 insn to set the new, reduced reg for the giv.
4659 We used to do this only for biv_count != 1, but
4660 this fails when there is a giv after a single biv
4661 increment, e.g. when the last giv was expressed as
4662 pre-decrement. */
4663 for (tv = bl->biv; tv; tv = tv->next_iv)
4665 /* We always emit reduced giv increments before the
4666 biv increment when bl->biv_count != 1. So by
4667 emitting the add insns for derived givs after the
4668 biv increment, they pick up the updated value of
4669 the reduced giv.
4670 If the reduced giv is processed with
4671 auto_inc_opt == 1, then it is incremented earlier
4672 than the biv, hence we'll still pick up the right
4673 value.
4674 If it's processed with auto_inc_opt == -1,
4675 that implies that the biv increment is before the
4676 first reduced giv's use. The derived giv's lifetime
4677 is after the reduced giv's lifetime, hence in this
4678 case, the biv increment doesn't matter. */
4679 emit_insn_after (copy_rtx (PATTERN (v->insn)), tv->insn);
4681 continue;
4684 #ifdef AUTO_INC_DEC
4685 /* If the target has auto-increment addressing modes, and
4686 this is an address giv, then try to put the increment
4687 immediately after its use, so that flow can create an
4688 auto-increment addressing mode. */
4689 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4690 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4691 /* We don't handle reversed biv's because bl->biv->insn
4692 does not have a valid INSN_LUID. */
4693 && ! bl->reversed
4694 && v->always_executed && ! v->maybe_multiple
4695 && INSN_UID (v->insn) < max_uid_for_loop)
4697 /* If other giv's have been combined with this one, then
4698 this will work only if all uses of the other giv's occur
4699 before this giv's insn. This is difficult to check.
4701 We simplify this by looking for the common case where
4702 there is one DEST_REG giv, and this giv's insn is the
4703 last use of the dest_reg of that DEST_REG giv. If the
4704 increment occurs after the address giv, then we can
4705 perform the optimization. (Otherwise, the increment
4706 would have to go before other_giv, and we would not be
4707 able to combine it with the address giv to get an
4708 auto-inc address.) */
4709 if (v->combined_with)
4711 struct induction *other_giv = 0;
4713 for (tv = bl->giv; tv; tv = tv->next_iv)
4714 if (tv->same == v)
4716 if (other_giv)
4717 break;
4718 else
4719 other_giv = tv;
4721 if (! tv && other_giv
4722 && REGNO (other_giv->dest_reg) < max_reg_before_loop
4723 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4724 == INSN_UID (v->insn))
4725 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4726 auto_inc_opt = 1;
4728 /* Check for case where increment is before the address
4729 giv. Do this test in "loop order". */
4730 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4731 && (INSN_LUID (v->insn) < INSN_LUID (loop_scan_start)
4732 || (INSN_LUID (bl->biv->insn)
4733 > INSN_LUID (loop_scan_start))))
4734 || (INSN_LUID (v->insn) < INSN_LUID (loop_scan_start)
4735 && (INSN_LUID (loop_scan_start)
4736 < INSN_LUID (bl->biv->insn))))
4737 auto_inc_opt = -1;
4738 else
4739 auto_inc_opt = 1;
4741 #ifdef HAVE_cc0
4743 rtx prev;
4745 /* We can't put an insn immediately after one setting
4746 cc0, or immediately before one using cc0. */
4747 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4748 || (auto_inc_opt == -1
4749 && (prev = prev_nonnote_insn (v->insn)) != 0
4750 && GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4751 && sets_cc0_p (PATTERN (prev))))
4752 auto_inc_opt = 0;
4754 #endif
4756 if (auto_inc_opt)
4757 v->auto_inc_opt = 1;
4759 #endif
4761 /* For each place where the biv is incremented, add an insn
4762 to increment the new, reduced reg for the giv. */
4763 for (tv = bl->biv; tv; tv = tv->next_iv)
4765 rtx insert_before;
4767 if (! auto_inc_opt)
4768 insert_before = tv->insn;
4769 else if (auto_inc_opt == 1)
4770 insert_before = NEXT_INSN (v->insn);
4771 else
4772 insert_before = v->insn;
4774 if (tv->mult_val == const1_rtx)
4775 emit_iv_add_mult (tv->add_val, v->mult_val,
4776 v->new_reg, v->new_reg, insert_before);
4777 else /* tv->mult_val == const0_rtx */
4778 /* A multiply is acceptable here
4779 since this is presumed to be seldom executed. */
4780 emit_iv_add_mult (tv->add_val, v->mult_val,
4781 v->add_val, v->new_reg, insert_before);
4784 /* Add code at loop start to initialize giv's reduced reg. */
4786 emit_iv_add_mult (bl->initial_value, v->mult_val,
4787 v->add_val, v->new_reg, loop_start);
4791 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4792 as not reduced.
4794 For each giv register that can be reduced now: if replaceable,
4795 substitute reduced reg wherever the old giv occurs;
4796 else add new move insn "giv_reg = reduced_reg". */
4798 for (v = bl->giv; v; v = v->next_iv)
4800 if (v->same && v->same->ignore)
4801 v->ignore = 1;
4803 if (v->ignore)
4804 continue;
4806 /* Update expression if this was combined, in case other giv was
4807 replaced. */
4808 if (v->same)
4809 v->new_reg = replace_rtx (v->new_reg,
4810 v->same->dest_reg, v->same->new_reg);
4812 if (v->giv_type == DEST_ADDR)
4813 /* Store reduced reg as the address in the memref where we found
4814 this giv. */
4815 validate_change (v->insn, v->location, v->new_reg, 0);
4816 else if (v->replaceable)
4818 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4820 #if 0
4821 /* I can no longer duplicate the original problem. Perhaps
4822 this is unnecessary now? */
4824 /* Replaceable; it isn't strictly necessary to delete the old
4825 insn and emit a new one, because v->dest_reg is now dead.
4827 However, especially when unrolling loops, the special
4828 handling for (set REG0 REG1) in the second cse pass may
4829 make v->dest_reg live again. To avoid this problem, emit
4830 an insn to set the original giv reg from the reduced giv.
4831 We can not delete the original insn, since it may be part
4832 of a LIBCALL, and the code in flow that eliminates dead
4833 libcalls will fail if it is deleted. */
4834 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4835 v->insn);
4836 #endif
4838 else
4840 /* Not replaceable; emit an insn to set the original giv reg from
4841 the reduced giv, same as above. */
4842 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4843 v->insn);
4846 /* When a loop is reversed, givs which depend on the reversed
4847 biv, and which are live outside the loop, must be set to their
4848 correct final value. This insn is only needed if the giv is
4849 not replaceable. The correct final value is the same as the
4850 value that the giv starts the reversed loop with. */
4851 if (bl->reversed && ! v->replaceable)
4852 emit_iv_add_mult (bl->initial_value, v->mult_val,
4853 v->add_val, v->dest_reg, end_insert_before);
4854 else if (v->final_value)
4856 rtx insert_before;
4858 /* If the loop has multiple exits, emit the insn before the
4859 loop to ensure that it will always be executed no matter
4860 how the loop exits. Otherwise, emit the insn after the loop,
4861 since this is slightly more efficient. */
4862 if (loop->exit_count)
4863 insert_before = loop_start;
4864 else
4865 insert_before = end_insert_before;
4866 emit_insn_before (gen_move_insn (v->dest_reg, v->final_value),
4867 insert_before);
4869 #if 0
4870 /* If the insn to set the final value of the giv was emitted
4871 before the loop, then we must delete the insn inside the loop
4872 that sets it. If this is a LIBCALL, then we must delete
4873 every insn in the libcall. Note, however, that
4874 final_giv_value will only succeed when there are multiple
4875 exits if the giv is dead at each exit, hence it does not
4876 matter that the original insn remains because it is dead
4877 anyways. */
4878 /* Delete the insn inside the loop that sets the giv since
4879 the giv is now set before (or after) the loop. */
4880 delete_insn (v->insn);
4881 #endif
4884 if (loop_dump_stream)
4886 fprintf (loop_dump_stream, "giv at %d reduced to ",
4887 INSN_UID (v->insn));
4888 print_rtl (loop_dump_stream, v->new_reg);
4889 fprintf (loop_dump_stream, "\n");
4893 /* All the givs based on the biv bl have been reduced if they
4894 merit it. */
4896 /* For each giv not marked as maybe dead that has been combined with a
4897 second giv, clear any "maybe dead" mark on that second giv.
4898 v->new_reg will either be or refer to the register of the giv it
4899 combined with.
4901 Doing this clearing avoids problems in biv elimination where a
4902 giv's new_reg is a complex value that can't be put in the insn but
4903 the giv combined with (with a reg as new_reg) is marked maybe_dead.
4904 Since the register will be used in either case, we'd prefer it be
4905 used from the simpler giv. */
4907 for (v = bl->giv; v; v = v->next_iv)
4908 if (! v->maybe_dead && v->same)
4909 v->same->maybe_dead = 0;
4911 /* Try to eliminate the biv, if it is a candidate.
4912 This won't work if ! all_reduced,
4913 since the givs we planned to use might not have been reduced.
4915 We have to be careful that we didn't initially think we could eliminate
4916 this biv because of a giv that we now think may be dead and shouldn't
4917 be used as a biv replacement.
4919 Also, there is the possibility that we may have a giv that looks
4920 like it can be used to eliminate a biv, but the resulting insn
4921 isn't valid. This can happen, for example, on the 88k, where a
4922 JUMP_INSN can compare a register only with zero. Attempts to
4923 replace it with a compare with a constant will fail.
4925 Note that in cases where this call fails, we may have replaced some
4926 of the occurrences of the biv with a giv, but no harm was done in
4927 doing so in the rare cases where it can occur. */
4929 if (all_reduced == 1 && bl->eliminable
4930 && maybe_eliminate_biv (loop, bl, 1, threshold, insn_count))
4932 /* ?? If we created a new test to bypass the loop entirely,
4933 or otherwise drop straight in, based on this test, then
4934 we might want to rewrite it also. This way some later
4935 pass has more hope of removing the initialization of this
4936 biv entirely. */
4938 /* If final_value != 0, then the biv may be used after loop end
4939 and we must emit an insn to set it just in case.
4941 Reversed bivs already have an insn after the loop setting their
4942 value, so we don't need another one. We can't calculate the
4943 proper final value for such a biv here anyways. */
4944 if (final_value != 0 && ! bl->reversed)
4946 rtx insert_before;
4948 /* If the loop has multiple exits, emit the insn before the
4949 loop to ensure that it will always be executed no matter
4950 how the loop exits. Otherwise, emit the insn after the
4951 loop, since this is slightly more efficient. */
4952 if (loop->exit_count)
4953 insert_before = loop_start;
4954 else
4955 insert_before = end_insert_before;
4957 emit_insn_before (gen_move_insn (bl->biv->dest_reg, final_value),
4958 end_insert_before);
4961 #if 0
4962 /* Delete all of the instructions inside the loop which set
4963 the biv, as they are all dead. If is safe to delete them,
4964 because an insn setting a biv will never be part of a libcall. */
4965 /* However, deleting them will invalidate the regno_last_uid info,
4966 so keeping them around is more convenient. Final_biv_value
4967 will only succeed when there are multiple exits if the biv
4968 is dead at each exit, hence it does not matter that the original
4969 insn remains, because it is dead anyways. */
4970 for (v = bl->biv; v; v = v->next_iv)
4971 delete_insn (v->insn);
4972 #endif
4974 if (loop_dump_stream)
4975 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
4976 bl->regno);
4980 /* Go through all the instructions in the loop, making all the
4981 register substitutions scheduled in REG_MAP. */
4983 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
4984 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4985 || GET_CODE (p) == CALL_INSN)
4987 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
4988 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
4989 INSN_CODE (p) = -1;
4992 if (loop_info->n_iterations > 0)
4994 /* When we completely unroll a loop we will likely not need the increment
4995 of the loop BIV and we will not need the conditional branch at the
4996 end of the loop. */
4997 unrolled_insn_copies = insn_count - 2;
4999 #ifdef HAVE_cc0
5000 /* When we completely unroll a loop on a HAVE_cc0 machine we will not
5001 need the comparison before the conditional branch at the end of the
5002 loop. */
5003 unrolled_insn_copies -= 1;
5004 #endif
5006 /* We'll need one copy for each loop iteration. */
5007 unrolled_insn_copies *= loop_info->n_iterations;
5009 /* A little slop to account for the ability to remove initialization
5010 code, better CSE, and other secondary benefits of completely
5011 unrolling some loops. */
5012 unrolled_insn_copies -= 1;
5014 /* Clamp the value. */
5015 if (unrolled_insn_copies < 0)
5016 unrolled_insn_copies = 0;
5019 /* Unroll loops from within strength reduction so that we can use the
5020 induction variable information that strength_reduce has already
5021 collected. Always unroll loops that would be as small or smaller
5022 unrolled than when rolled. */
5023 if ((flags & LOOP_UNROLL)
5024 || (loop_info->n_iterations > 0
5025 && unrolled_insn_copies <= insn_count))
5026 unroll_loop (loop, insn_count, end_insert_before, 1);
5028 #ifdef HAVE_decrement_and_branch_on_count
5029 /* Instrument the loop with BCT insn. */
5030 if (HAVE_decrement_and_branch_on_count && (flags & LOOP_BCT)
5031 && flag_branch_on_count_reg)
5032 insert_bct (loop);
5033 #endif /* HAVE_decrement_and_branch_on_count */
5035 if (loop_dump_stream)
5036 fprintf (loop_dump_stream, "\n");
5038 egress:
5039 VARRAY_FREE (reg_iv_type);
5040 VARRAY_FREE (reg_iv_info);
5041 free (reg_biv_class);
5042 if (reg_map)
5043 free (reg_map);
5046 /*Record all basic induction variables calculated in the insn. */
5047 static rtx
5048 check_insn_for_bivs (loop, p, not_every_iteration, maybe_multiple)
5049 struct loop *loop;
5050 rtx p;
5051 int not_every_iteration;
5052 int maybe_multiple;
5054 rtx set;
5055 rtx dest_reg;
5056 rtx inc_val;
5057 rtx mult_val;
5058 rtx *location;
5060 if (GET_CODE (p) == INSN
5061 && (set = single_set (p))
5062 && GET_CODE (SET_DEST (set)) == REG)
5064 dest_reg = SET_DEST (set);
5065 if (REGNO (dest_reg) < max_reg_before_loop
5066 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
5067 && REG_IV_TYPE (REGNO (dest_reg)) != NOT_BASIC_INDUCT)
5069 if (basic_induction_var (loop, SET_SRC (set),
5070 GET_MODE (SET_SRC (set)),
5071 dest_reg, p, &inc_val, &mult_val,
5072 &location))
5074 /* It is a possible basic induction variable.
5075 Create and initialize an induction structure for it. */
5077 struct induction *v
5078 = (struct induction *) oballoc (sizeof (struct induction));
5080 record_biv (v, p, dest_reg, inc_val, mult_val, location,
5081 not_every_iteration, maybe_multiple);
5082 REG_IV_TYPE (REGNO (dest_reg)) = BASIC_INDUCT;
5084 else if (REGNO (dest_reg) < max_reg_before_loop)
5085 REG_IV_TYPE (REGNO (dest_reg)) = NOT_BASIC_INDUCT;
5088 return p;
5091 /* Record all givs calculated in the insn.
5092 A register is a giv if: it is only set once, it is a function of a
5093 biv and a constant (or invariant), and it is not a biv. */
5094 static rtx
5095 check_insn_for_givs (loop, p, not_every_iteration, maybe_multiple)
5096 struct loop *loop;
5097 rtx p;
5098 int not_every_iteration;
5099 int maybe_multiple;
5101 rtx set;
5102 /* Look for a general induction variable in a register. */
5103 if (GET_CODE (p) == INSN
5104 && (set = single_set (p))
5105 && GET_CODE (SET_DEST (set)) == REG
5106 && ! VARRAY_CHAR (may_not_optimize, REGNO (SET_DEST (set))))
5108 rtx src_reg;
5109 rtx dest_reg;
5110 rtx add_val;
5111 rtx mult_val;
5112 int benefit;
5113 rtx regnote = 0;
5114 rtx last_consec_insn;
5116 dest_reg = SET_DEST (set);
5117 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
5118 return p;
5120 if (/* SET_SRC is a giv. */
5121 (general_induction_var (loop, SET_SRC (set), &src_reg, &add_val,
5122 &mult_val, 0, &benefit, VOIDmode)
5123 /* Equivalent expression is a giv. */
5124 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
5125 && general_induction_var (loop, XEXP (regnote, 0), &src_reg,
5126 &add_val, &mult_val, 0,
5127 &benefit, VOIDmode)))
5128 /* Don't try to handle any regs made by loop optimization.
5129 We have nothing on them in regno_first_uid, etc. */
5130 && REGNO (dest_reg) < max_reg_before_loop
5131 /* Don't recognize a BASIC_INDUCT_VAR here. */
5132 && dest_reg != src_reg
5133 /* This must be the only place where the register is set. */
5134 && (VARRAY_INT (n_times_set, REGNO (dest_reg)) == 1
5135 /* or all sets must be consecutive and make a giv. */
5136 || (benefit = consec_sets_giv (loop, benefit, p,
5137 src_reg, dest_reg,
5138 &add_val, &mult_val,
5139 &last_consec_insn))))
5141 struct induction *v
5142 = (struct induction *) oballoc (sizeof (struct induction));
5144 /* If this is a library call, increase benefit. */
5145 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5146 benefit += libcall_benefit (p);
5148 /* Skip the consecutive insns, if there are any. */
5149 if (VARRAY_INT (n_times_set, REGNO (dest_reg)) != 1)
5150 p = last_consec_insn;
5152 record_giv (loop, v, p, src_reg, dest_reg, mult_val, add_val,
5153 benefit, DEST_REG, not_every_iteration,
5154 maybe_multiple, NULL_PTR);
5159 #ifndef DONT_REDUCE_ADDR
5160 /* Look for givs which are memory addresses. */
5161 /* This resulted in worse code on a VAX 8600. I wonder if it
5162 still does. */
5163 if (GET_CODE (p) == INSN)
5164 find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
5165 maybe_multiple);
5166 #endif
5168 /* Update the status of whether giv can derive other givs. This can
5169 change when we pass a label or an insn that updates a biv. */
5170 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5171 || GET_CODE (p) == CODE_LABEL)
5172 update_giv_derive (loop, p);
5173 return p;
5176 /* Return 1 if X is a valid source for an initial value (or as value being
5177 compared against in an initial test).
5179 X must be either a register or constant and must not be clobbered between
5180 the current insn and the start of the loop.
5182 INSN is the insn containing X. */
5184 static int
5185 valid_initial_value_p (x, insn, call_seen, loop_start)
5186 rtx x;
5187 rtx insn;
5188 int call_seen;
5189 rtx loop_start;
5191 if (CONSTANT_P (x))
5192 return 1;
5194 /* Only consider pseudos we know about initialized in insns whose luids
5195 we know. */
5196 if (GET_CODE (x) != REG
5197 || REGNO (x) >= max_reg_before_loop)
5198 return 0;
5200 /* Don't use call-clobbered registers across a call which clobbers it. On
5201 some machines, don't use any hard registers at all. */
5202 if (REGNO (x) < FIRST_PSEUDO_REGISTER
5203 && (SMALL_REGISTER_CLASSES
5204 || (call_used_regs[REGNO (x)] && call_seen)))
5205 return 0;
5207 /* Don't use registers that have been clobbered before the start of the
5208 loop. */
5209 if (reg_set_between_p (x, insn, loop_start))
5210 return 0;
5212 return 1;
5215 /* Scan X for memory refs and check each memory address
5216 as a possible giv. INSN is the insn whose pattern X comes from.
5217 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5218 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
5219 more thanonce in each loop iteration. */
5221 static void
5222 find_mem_givs (loop, x, insn, not_every_iteration, maybe_multiple)
5223 const struct loop *loop;
5224 rtx x;
5225 rtx insn;
5226 int not_every_iteration, maybe_multiple;
5228 register int i, j;
5229 register enum rtx_code code;
5230 register const char *fmt;
5232 if (x == 0)
5233 return;
5235 code = GET_CODE (x);
5236 switch (code)
5238 case REG:
5239 case CONST_INT:
5240 case CONST:
5241 case CONST_DOUBLE:
5242 case SYMBOL_REF:
5243 case LABEL_REF:
5244 case PC:
5245 case CC0:
5246 case ADDR_VEC:
5247 case ADDR_DIFF_VEC:
5248 case USE:
5249 case CLOBBER:
5250 return;
5252 case MEM:
5254 rtx src_reg;
5255 rtx add_val;
5256 rtx mult_val;
5257 int benefit;
5259 /* This code used to disable creating GIVs with mult_val == 1 and
5260 add_val == 0. However, this leads to lost optimizations when
5261 it comes time to combine a set of related DEST_ADDR GIVs, since
5262 this one would not be seen. */
5264 if (general_induction_var (loop, XEXP (x, 0), &src_reg, &add_val,
5265 &mult_val, 1, &benefit, GET_MODE (x)))
5267 /* Found one; record it. */
5268 struct induction *v
5269 = (struct induction *) oballoc (sizeof (struct induction));
5271 record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
5272 add_val, benefit, DEST_ADDR, not_every_iteration,
5273 maybe_multiple, &XEXP (x, 0));
5275 v->mem_mode = GET_MODE (x);
5278 return;
5280 default:
5281 break;
5284 /* Recursively scan the subexpressions for other mem refs. */
5286 fmt = GET_RTX_FORMAT (code);
5287 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5288 if (fmt[i] == 'e')
5289 find_mem_givs (loop, XEXP (x, i), insn, not_every_iteration,
5290 maybe_multiple);
5291 else if (fmt[i] == 'E')
5292 for (j = 0; j < XVECLEN (x, i); j++)
5293 find_mem_givs (loop, XVECEXP (x, i, j), insn, not_every_iteration,
5294 maybe_multiple);
5297 /* Fill in the data about one biv update.
5298 V is the `struct induction' in which we record the biv. (It is
5299 allocated by the caller, with alloca.)
5300 INSN is the insn that sets it.
5301 DEST_REG is the biv's reg.
5303 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5304 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
5305 being set to INC_VAL.
5307 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5308 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5309 can be executed more than once per iteration. If MAYBE_MULTIPLE
5310 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5311 executed exactly once per iteration. */
5313 static void
5314 record_biv (v, insn, dest_reg, inc_val, mult_val, location,
5315 not_every_iteration, maybe_multiple)
5316 struct induction *v;
5317 rtx insn;
5318 rtx dest_reg;
5319 rtx inc_val;
5320 rtx mult_val;
5321 rtx *location;
5322 int not_every_iteration;
5323 int maybe_multiple;
5325 struct iv_class *bl;
5327 v->insn = insn;
5328 v->src_reg = dest_reg;
5329 v->dest_reg = dest_reg;
5330 v->mult_val = mult_val;
5331 v->add_val = inc_val;
5332 v->location = location;
5333 v->mode = GET_MODE (dest_reg);
5334 v->always_computable = ! not_every_iteration;
5335 v->always_executed = ! not_every_iteration;
5336 v->maybe_multiple = maybe_multiple;
5338 /* Add this to the reg's iv_class, creating a class
5339 if this is the first incrementation of the reg. */
5341 bl = reg_biv_class[REGNO (dest_reg)];
5342 if (bl == 0)
5344 /* Create and initialize new iv_class. */
5346 bl = (struct iv_class *) oballoc (sizeof (struct iv_class));
5348 bl->regno = REGNO (dest_reg);
5349 bl->biv = 0;
5350 bl->giv = 0;
5351 bl->biv_count = 0;
5352 bl->giv_count = 0;
5354 /* Set initial value to the reg itself. */
5355 bl->initial_value = dest_reg;
5356 /* We haven't seen the initializing insn yet */
5357 bl->init_insn = 0;
5358 bl->init_set = 0;
5359 bl->initial_test = 0;
5360 bl->incremented = 0;
5361 bl->eliminable = 0;
5362 bl->nonneg = 0;
5363 bl->reversed = 0;
5364 bl->total_benefit = 0;
5366 /* Add this class to loop_iv_list. */
5367 bl->next = loop_iv_list;
5368 loop_iv_list = bl;
5370 /* Put it in the array of biv register classes. */
5371 reg_biv_class[REGNO (dest_reg)] = bl;
5374 /* Update IV_CLASS entry for this biv. */
5375 v->next_iv = bl->biv;
5376 bl->biv = v;
5377 bl->biv_count++;
5378 if (mult_val == const1_rtx)
5379 bl->incremented = 1;
5381 if (loop_dump_stream)
5383 fprintf (loop_dump_stream,
5384 "Insn %d: possible biv, reg %d,",
5385 INSN_UID (insn), REGNO (dest_reg));
5386 if (GET_CODE (inc_val) == CONST_INT)
5388 fprintf (loop_dump_stream, " const =");
5389 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (inc_val));
5390 fputc ('\n', loop_dump_stream);
5392 else
5394 fprintf (loop_dump_stream, " const = ");
5395 print_rtl (loop_dump_stream, inc_val);
5396 fprintf (loop_dump_stream, "\n");
5401 /* Fill in the data about one giv.
5402 V is the `struct induction' in which we record the giv. (It is
5403 allocated by the caller, with alloca.)
5404 INSN is the insn that sets it.
5405 BENEFIT estimates the savings from deleting this insn.
5406 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5407 into a register or is used as a memory address.
5409 SRC_REG is the biv reg which the giv is computed from.
5410 DEST_REG is the giv's reg (if the giv is stored in a reg).
5411 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5412 LOCATION points to the place where this giv's value appears in INSN. */
5414 static void
5415 record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, benefit,
5416 type, not_every_iteration, maybe_multiple, location)
5417 const struct loop *loop;
5418 struct induction *v;
5419 rtx insn;
5420 rtx src_reg;
5421 rtx dest_reg;
5422 rtx mult_val, add_val;
5423 int benefit;
5424 enum g_types type;
5425 int not_every_iteration, maybe_multiple;
5426 rtx *location;
5428 struct induction *b;
5429 struct iv_class *bl;
5430 rtx set = single_set (insn);
5431 rtx temp;
5433 /* Attempt to prove constantness of the values. */
5434 temp = simplify_rtx (add_val);
5435 if (temp)
5436 add_val = temp;
5438 v->insn = insn;
5439 v->src_reg = src_reg;
5440 v->giv_type = type;
5441 v->dest_reg = dest_reg;
5442 v->mult_val = mult_val;
5443 v->add_val = add_val;
5444 v->benefit = benefit;
5445 v->location = location;
5446 v->cant_derive = 0;
5447 v->combined_with = 0;
5448 v->maybe_multiple = maybe_multiple;
5449 v->maybe_dead = 0;
5450 v->derive_adjustment = 0;
5451 v->same = 0;
5452 v->ignore = 0;
5453 v->new_reg = 0;
5454 v->final_value = 0;
5455 v->same_insn = 0;
5456 v->auto_inc_opt = 0;
5457 v->unrolled = 0;
5458 v->shared = 0;
5459 v->derived_from = 0;
5460 v->last_use = 0;
5462 /* The v->always_computable field is used in update_giv_derive, to
5463 determine whether a giv can be used to derive another giv. For a
5464 DEST_REG giv, INSN computes a new value for the giv, so its value
5465 isn't computable if INSN insn't executed every iteration.
5466 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5467 it does not compute a new value. Hence the value is always computable
5468 regardless of whether INSN is executed each iteration. */
5470 if (type == DEST_ADDR)
5471 v->always_computable = 1;
5472 else
5473 v->always_computable = ! not_every_iteration;
5475 v->always_executed = ! not_every_iteration;
5477 if (type == DEST_ADDR)
5479 v->mode = GET_MODE (*location);
5480 v->lifetime = 1;
5482 else /* type == DEST_REG */
5484 v->mode = GET_MODE (SET_DEST (set));
5486 v->lifetime = (uid_luid[REGNO_LAST_UID (REGNO (dest_reg))]
5487 - uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))]);
5489 /* If the lifetime is zero, it means that this register is
5490 really a dead store. So mark this as a giv that can be
5491 ignored. This will not prevent the biv from being eliminated. */
5492 if (v->lifetime == 0)
5493 v->ignore = 1;
5495 REG_IV_TYPE (REGNO (dest_reg)) = GENERAL_INDUCT;
5496 REG_IV_INFO (REGNO (dest_reg)) = v;
5499 /* Add the giv to the class of givs computed from one biv. */
5501 bl = reg_biv_class[REGNO (src_reg)];
5502 if (bl)
5504 v->next_iv = bl->giv;
5505 bl->giv = v;
5506 /* Don't count DEST_ADDR. This is supposed to count the number of
5507 insns that calculate givs. */
5508 if (type == DEST_REG)
5509 bl->giv_count++;
5510 bl->total_benefit += benefit;
5512 else
5513 /* Fatal error, biv missing for this giv? */
5514 abort ();
5516 if (type == DEST_ADDR)
5517 v->replaceable = 1;
5518 else
5520 /* The giv can be replaced outright by the reduced register only if all
5521 of the following conditions are true:
5522 - the insn that sets the giv is always executed on any iteration
5523 on which the giv is used at all
5524 (there are two ways to deduce this:
5525 either the insn is executed on every iteration,
5526 or all uses follow that insn in the same basic block),
5527 - the giv is not used outside the loop
5528 - no assignments to the biv occur during the giv's lifetime. */
5530 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
5531 /* Previous line always fails if INSN was moved by loop opt. */
5532 && uid_luid[REGNO_LAST_UID (REGNO (dest_reg))]
5533 < INSN_LUID (loop->end)
5534 && (! not_every_iteration
5535 || last_use_this_basic_block (dest_reg, insn)))
5537 /* Now check that there are no assignments to the biv within the
5538 giv's lifetime. This requires two separate checks. */
5540 /* Check each biv update, and fail if any are between the first
5541 and last use of the giv.
5543 If this loop contains an inner loop that was unrolled, then
5544 the insn modifying the biv may have been emitted by the loop
5545 unrolling code, and hence does not have a valid luid. Just
5546 mark the biv as not replaceable in this case. It is not very
5547 useful as a biv, because it is used in two different loops.
5548 It is very unlikely that we would be able to optimize the giv
5549 using this biv anyways. */
5551 v->replaceable = 1;
5552 for (b = bl->biv; b; b = b->next_iv)
5554 if (INSN_UID (b->insn) >= max_uid_for_loop
5555 || ((uid_luid[INSN_UID (b->insn)]
5556 >= uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))])
5557 && (uid_luid[INSN_UID (b->insn)]
5558 <= uid_luid[REGNO_LAST_UID (REGNO (dest_reg))])))
5560 v->replaceable = 0;
5561 v->not_replaceable = 1;
5562 break;
5566 /* If there are any backwards branches that go from after the
5567 biv update to before it, then this giv is not replaceable. */
5568 if (v->replaceable)
5569 for (b = bl->biv; b; b = b->next_iv)
5570 if (back_branch_in_range_p (loop, b->insn))
5572 v->replaceable = 0;
5573 v->not_replaceable = 1;
5574 break;
5577 else
5579 /* May still be replaceable, we don't have enough info here to
5580 decide. */
5581 v->replaceable = 0;
5582 v->not_replaceable = 0;
5586 /* Record whether the add_val contains a const_int, for later use by
5587 combine_givs. */
5589 rtx tem = add_val;
5591 v->no_const_addval = 1;
5592 if (tem == const0_rtx)
5594 else if (CONSTANT_P (add_val))
5595 v->no_const_addval = 0;
5596 if (GET_CODE (tem) == PLUS)
5598 while (1)
5600 if (GET_CODE (XEXP (tem, 0)) == PLUS)
5601 tem = XEXP (tem, 0);
5602 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
5603 tem = XEXP (tem, 1);
5604 else
5605 break;
5607 if (CONSTANT_P (XEXP (tem, 1)))
5608 v->no_const_addval = 0;
5612 if (loop_dump_stream)
5614 if (type == DEST_REG)
5615 fprintf (loop_dump_stream, "Insn %d: giv reg %d",
5616 INSN_UID (insn), REGNO (dest_reg));
5617 else
5618 fprintf (loop_dump_stream, "Insn %d: dest address",
5619 INSN_UID (insn));
5621 fprintf (loop_dump_stream, " src reg %d benefit %d",
5622 REGNO (src_reg), v->benefit);
5623 fprintf (loop_dump_stream, " lifetime %d",
5624 v->lifetime);
5626 if (v->replaceable)
5627 fprintf (loop_dump_stream, " replaceable");
5629 if (v->no_const_addval)
5630 fprintf (loop_dump_stream, " ncav");
5632 if (GET_CODE (mult_val) == CONST_INT)
5634 fprintf (loop_dump_stream, " mult ");
5635 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (mult_val));
5637 else
5639 fprintf (loop_dump_stream, " mult ");
5640 print_rtl (loop_dump_stream, mult_val);
5643 if (GET_CODE (add_val) == CONST_INT)
5645 fprintf (loop_dump_stream, " add ");
5646 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (add_val));
5648 else
5650 fprintf (loop_dump_stream, " add ");
5651 print_rtl (loop_dump_stream, add_val);
5655 if (loop_dump_stream)
5656 fprintf (loop_dump_stream, "\n");
5661 /* All this does is determine whether a giv can be made replaceable because
5662 its final value can be calculated. This code can not be part of record_giv
5663 above, because final_giv_value requires that the number of loop iterations
5664 be known, and that can not be accurately calculated until after all givs
5665 have been identified. */
5667 static void
5668 check_final_value (loop, v)
5669 const struct loop *loop;
5670 struct induction *v;
5672 struct iv_class *bl;
5673 rtx final_value = 0;
5675 bl = reg_biv_class[REGNO (v->src_reg)];
5677 /* DEST_ADDR givs will never reach here, because they are always marked
5678 replaceable above in record_giv. */
5680 /* The giv can be replaced outright by the reduced register only if all
5681 of the following conditions are true:
5682 - the insn that sets the giv is always executed on any iteration
5683 on which the giv is used at all
5684 (there are two ways to deduce this:
5685 either the insn is executed on every iteration,
5686 or all uses follow that insn in the same basic block),
5687 - its final value can be calculated (this condition is different
5688 than the one above in record_giv)
5689 - no assignments to the biv occur during the giv's lifetime. */
5691 #if 0
5692 /* This is only called now when replaceable is known to be false. */
5693 /* Clear replaceable, so that it won't confuse final_giv_value. */
5694 v->replaceable = 0;
5695 #endif
5697 if ((final_value = final_giv_value (loop, v))
5698 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
5700 int biv_increment_seen = 0;
5701 rtx p = v->insn;
5702 rtx last_giv_use;
5704 v->replaceable = 1;
5706 /* When trying to determine whether or not a biv increment occurs
5707 during the lifetime of the giv, we can ignore uses of the variable
5708 outside the loop because final_value is true. Hence we can not
5709 use regno_last_uid and regno_first_uid as above in record_giv. */
5711 /* Search the loop to determine whether any assignments to the
5712 biv occur during the giv's lifetime. Start with the insn
5713 that sets the giv, and search around the loop until we come
5714 back to that insn again.
5716 Also fail if there is a jump within the giv's lifetime that jumps
5717 to somewhere outside the lifetime but still within the loop. This
5718 catches spaghetti code where the execution order is not linear, and
5719 hence the above test fails. Here we assume that the giv lifetime
5720 does not extend from one iteration of the loop to the next, so as
5721 to make the test easier. Since the lifetime isn't known yet,
5722 this requires two loops. See also record_giv above. */
5724 last_giv_use = v->insn;
5726 while (1)
5728 p = NEXT_INSN (p);
5729 if (p == loop->end)
5730 p = NEXT_INSN (loop->start);
5731 if (p == v->insn)
5732 break;
5734 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5735 || GET_CODE (p) == CALL_INSN)
5737 if (biv_increment_seen)
5739 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
5741 v->replaceable = 0;
5742 v->not_replaceable = 1;
5743 break;
5746 else if (reg_set_p (v->src_reg, PATTERN (p)))
5747 biv_increment_seen = 1;
5748 else if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
5749 last_giv_use = p;
5753 /* Now that the lifetime of the giv is known, check for branches
5754 from within the lifetime to outside the lifetime if it is still
5755 replaceable. */
5757 if (v->replaceable)
5759 p = v->insn;
5760 while (1)
5762 p = NEXT_INSN (p);
5763 if (p == loop->end)
5764 p = NEXT_INSN (loop->start);
5765 if (p == last_giv_use)
5766 break;
5768 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
5769 && LABEL_NAME (JUMP_LABEL (p))
5770 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
5771 && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
5772 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
5773 && loop_insn_first_p (JUMP_LABEL (p), loop->end))))
5775 v->replaceable = 0;
5776 v->not_replaceable = 1;
5778 if (loop_dump_stream)
5779 fprintf (loop_dump_stream,
5780 "Found branch outside giv lifetime.\n");
5782 break;
5787 /* If it is replaceable, then save the final value. */
5788 if (v->replaceable)
5789 v->final_value = final_value;
5792 if (loop_dump_stream && v->replaceable)
5793 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
5794 INSN_UID (v->insn), REGNO (v->dest_reg));
5797 /* Update the status of whether a giv can derive other givs.
5799 We need to do something special if there is or may be an update to the biv
5800 between the time the giv is defined and the time it is used to derive
5801 another giv.
5803 In addition, a giv that is only conditionally set is not allowed to
5804 derive another giv once a label has been passed.
5806 The cases we look at are when a label or an update to a biv is passed. */
5808 static void
5809 update_giv_derive (loop, p)
5810 const struct loop *loop;
5811 rtx p;
5813 struct iv_class *bl;
5814 struct induction *biv, *giv;
5815 rtx tem;
5816 int dummy;
5818 /* Search all IV classes, then all bivs, and finally all givs.
5820 There are three cases we are concerned with. First we have the situation
5821 of a giv that is only updated conditionally. In that case, it may not
5822 derive any givs after a label is passed.
5824 The second case is when a biv update occurs, or may occur, after the
5825 definition of a giv. For certain biv updates (see below) that are
5826 known to occur between the giv definition and use, we can adjust the
5827 giv definition. For others, or when the biv update is conditional,
5828 we must prevent the giv from deriving any other givs. There are two
5829 sub-cases within this case.
5831 If this is a label, we are concerned with any biv update that is done
5832 conditionally, since it may be done after the giv is defined followed by
5833 a branch here (actually, we need to pass both a jump and a label, but
5834 this extra tracking doesn't seem worth it).
5836 If this is a jump, we are concerned about any biv update that may be
5837 executed multiple times. We are actually only concerned about
5838 backward jumps, but it is probably not worth performing the test
5839 on the jump again here.
5841 If this is a biv update, we must adjust the giv status to show that a
5842 subsequent biv update was performed. If this adjustment cannot be done,
5843 the giv cannot derive further givs. */
5845 for (bl = loop_iv_list; bl; bl = bl->next)
5846 for (biv = bl->biv; biv; biv = biv->next_iv)
5847 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
5848 || biv->insn == p)
5850 for (giv = bl->giv; giv; giv = giv->next_iv)
5852 /* If cant_derive is already true, there is no point in
5853 checking all of these conditions again. */
5854 if (giv->cant_derive)
5855 continue;
5857 /* If this giv is conditionally set and we have passed a label,
5858 it cannot derive anything. */
5859 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
5860 giv->cant_derive = 1;
5862 /* Skip givs that have mult_val == 0, since
5863 they are really invariants. Also skip those that are
5864 replaceable, since we know their lifetime doesn't contain
5865 any biv update. */
5866 else if (giv->mult_val == const0_rtx || giv->replaceable)
5867 continue;
5869 /* The only way we can allow this giv to derive another
5870 is if this is a biv increment and we can form the product
5871 of biv->add_val and giv->mult_val. In this case, we will
5872 be able to compute a compensation. */
5873 else if (biv->insn == p)
5875 tem = 0;
5877 if (biv->mult_val == const1_rtx)
5878 tem = simplify_giv_expr (loop,
5879 gen_rtx_MULT (giv->mode,
5880 biv->add_val,
5881 giv->mult_val),
5882 &dummy);
5884 if (tem && giv->derive_adjustment)
5885 tem = simplify_giv_expr
5886 (loop,
5887 gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
5888 &dummy);
5890 if (tem)
5891 giv->derive_adjustment = tem;
5892 else
5893 giv->cant_derive = 1;
5895 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5896 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
5897 giv->cant_derive = 1;
5902 /* Check whether an insn is an increment legitimate for a basic induction var.
5903 X is the source of insn P, or a part of it.
5904 MODE is the mode in which X should be interpreted.
5906 DEST_REG is the putative biv, also the destination of the insn.
5907 We accept patterns of these forms:
5908 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5909 REG = INVARIANT + REG
5911 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5912 store the additive term into *INC_VAL, and store the place where
5913 we found the additive term into *LOCATION.
5915 If X is an assignment of an invariant into DEST_REG, we set
5916 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5918 We also want to detect a BIV when it corresponds to a variable
5919 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5920 of the variable may be a PLUS that adds a SUBREG of that variable to
5921 an invariant and then sign- or zero-extends the result of the PLUS
5922 into the variable.
5924 Most GIVs in such cases will be in the promoted mode, since that is the
5925 probably the natural computation mode (and almost certainly the mode
5926 used for addresses) on the machine. So we view the pseudo-reg containing
5927 the variable as the BIV, as if it were simply incremented.
5929 Note that treating the entire pseudo as a BIV will result in making
5930 simple increments to any GIVs based on it. However, if the variable
5931 overflows in its declared mode but not its promoted mode, the result will
5932 be incorrect. This is acceptable if the variable is signed, since
5933 overflows in such cases are undefined, but not if it is unsigned, since
5934 those overflows are defined. So we only check for SIGN_EXTEND and
5935 not ZERO_EXTEND.
5937 If we cannot find a biv, we return 0. */
5939 static int
5940 basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
5941 const struct loop *loop;
5942 register rtx x;
5943 enum machine_mode mode;
5944 rtx dest_reg;
5945 rtx p;
5946 rtx *inc_val;
5947 rtx *mult_val;
5948 rtx **location;
5950 register enum rtx_code code;
5951 rtx *argp, arg;
5952 rtx insn, set = 0;
5954 code = GET_CODE (x);
5955 *location = NULL;
5956 switch (code)
5958 case PLUS:
5959 if (rtx_equal_p (XEXP (x, 0), dest_reg)
5960 || (GET_CODE (XEXP (x, 0)) == SUBREG
5961 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5962 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
5964 argp = &XEXP (x, 1);
5966 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
5967 || (GET_CODE (XEXP (x, 1)) == SUBREG
5968 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5969 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
5971 argp = &XEXP (x, 0);
5973 else
5974 return 0;
5976 arg = *argp;
5977 if (loop_invariant_p (loop, arg) != 1)
5978 return 0;
5980 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
5981 *mult_val = const1_rtx;
5982 *location = argp;
5983 return 1;
5985 case SUBREG:
5986 /* If this is a SUBREG for a promoted variable, check the inner
5987 value. */
5988 if (SUBREG_PROMOTED_VAR_P (x))
5989 return basic_induction_var (loop, SUBREG_REG (x),
5990 GET_MODE (SUBREG_REG (x)),
5991 dest_reg, p, inc_val, mult_val, location);
5992 return 0;
5994 case REG:
5995 /* If this register is assigned in a previous insn, look at its
5996 source, but don't go outside the loop or past a label. */
5998 /* If this sets a register to itself, we would repeat any previous
5999 biv increment if we applied this strategy blindly. */
6000 if (rtx_equal_p (dest_reg, x))
6001 return 0;
6003 insn = p;
6004 while (1)
6006 do {
6007 insn = PREV_INSN (insn);
6008 } while (insn && GET_CODE (insn) == NOTE
6009 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6011 if (!insn)
6012 break;
6013 set = single_set (insn);
6014 if (set == 0)
6015 break;
6017 if ((SET_DEST (set) == x
6018 || (GET_CODE (SET_DEST (set)) == SUBREG
6019 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
6020 <= UNITS_PER_WORD)
6021 && (GET_MODE_CLASS (GET_MODE (SET_DEST (set)))
6022 == MODE_INT)
6023 && SUBREG_REG (SET_DEST (set)) == x))
6024 && basic_induction_var (loop, SET_SRC (set),
6025 (GET_MODE (SET_SRC (set)) == VOIDmode
6026 ? GET_MODE (x)
6027 : GET_MODE (SET_SRC (set))),
6028 dest_reg, insn,
6029 inc_val, mult_val, location))
6030 return 1;
6032 /* ... fall through ... */
6034 /* Can accept constant setting of biv only when inside inner most loop.
6035 Otherwise, a biv of an inner loop may be incorrectly recognized
6036 as a biv of the outer loop,
6037 causing code to be moved INTO the inner loop. */
6038 case MEM:
6039 if (loop_invariant_p (loop, x) != 1)
6040 return 0;
6041 case CONST_INT:
6042 case SYMBOL_REF:
6043 case CONST:
6044 /* convert_modes aborts if we try to convert to or from CCmode, so just
6045 exclude that case. It is very unlikely that a condition code value
6046 would be a useful iterator anyways. */
6047 if (loop->level == 1
6048 && GET_MODE_CLASS (mode) != MODE_CC
6049 && GET_MODE_CLASS (GET_MODE (dest_reg)) != MODE_CC)
6051 /* Possible bug here? Perhaps we don't know the mode of X. */
6052 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
6053 *mult_val = const0_rtx;
6054 return 1;
6056 else
6057 return 0;
6059 case SIGN_EXTEND:
6060 return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6061 dest_reg, p, inc_val, mult_val, location);
6063 case ASHIFTRT:
6064 /* Similar, since this can be a sign extension. */
6065 for (insn = PREV_INSN (p);
6066 (insn && GET_CODE (insn) == NOTE
6067 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6068 insn = PREV_INSN (insn))
6071 if (insn)
6072 set = single_set (insn);
6074 if (! rtx_equal_p (dest_reg, XEXP (x, 0))
6075 && set && SET_DEST (set) == XEXP (x, 0)
6076 && GET_CODE (XEXP (x, 1)) == CONST_INT
6077 && INTVAL (XEXP (x, 1)) >= 0
6078 && GET_CODE (SET_SRC (set)) == ASHIFT
6079 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
6080 return basic_induction_var (loop, XEXP (SET_SRC (set), 0),
6081 GET_MODE (XEXP (x, 0)),
6082 dest_reg, insn, inc_val, mult_val,
6083 location);
6084 return 0;
6086 default:
6087 return 0;
6091 /* A general induction variable (giv) is any quantity that is a linear
6092 function of a basic induction variable,
6093 i.e. giv = biv * mult_val + add_val.
6094 The coefficients can be any loop invariant quantity.
6095 A giv need not be computed directly from the biv;
6096 it can be computed by way of other givs. */
6098 /* Determine whether X computes a giv.
6099 If it does, return a nonzero value
6100 which is the benefit from eliminating the computation of X;
6101 set *SRC_REG to the register of the biv that it is computed from;
6102 set *ADD_VAL and *MULT_VAL to the coefficients,
6103 such that the value of X is biv * mult + add; */
6105 static int
6106 general_induction_var (loop, x, src_reg, add_val, mult_val, is_addr,
6107 pbenefit, addr_mode)
6108 const struct loop *loop;
6109 rtx x;
6110 rtx *src_reg;
6111 rtx *add_val;
6112 rtx *mult_val;
6113 int is_addr;
6114 int *pbenefit;
6115 enum machine_mode addr_mode;
6117 rtx orig_x = x;
6118 char *storage;
6120 /* If this is an invariant, forget it, it isn't a giv. */
6121 if (loop_invariant_p (loop, x) == 1)
6122 return 0;
6124 /* See if the expression could be a giv and get its form.
6125 Mark our place on the obstack in case we don't find a giv. */
6126 storage = (char *) oballoc (0);
6127 *pbenefit = 0;
6128 x = simplify_giv_expr (loop, x, pbenefit);
6129 if (x == 0)
6131 obfree (storage);
6132 return 0;
6135 switch (GET_CODE (x))
6137 case USE:
6138 case CONST_INT:
6139 /* Since this is now an invariant and wasn't before, it must be a giv
6140 with MULT_VAL == 0. It doesn't matter which BIV we associate this
6141 with. */
6142 *src_reg = loop_iv_list->biv->dest_reg;
6143 *mult_val = const0_rtx;
6144 *add_val = x;
6145 break;
6147 case REG:
6148 /* This is equivalent to a BIV. */
6149 *src_reg = x;
6150 *mult_val = const1_rtx;
6151 *add_val = const0_rtx;
6152 break;
6154 case PLUS:
6155 /* Either (plus (biv) (invar)) or
6156 (plus (mult (biv) (invar_1)) (invar_2)). */
6157 if (GET_CODE (XEXP (x, 0)) == MULT)
6159 *src_reg = XEXP (XEXP (x, 0), 0);
6160 *mult_val = XEXP (XEXP (x, 0), 1);
6162 else
6164 *src_reg = XEXP (x, 0);
6165 *mult_val = const1_rtx;
6167 *add_val = XEXP (x, 1);
6168 break;
6170 case MULT:
6171 /* ADD_VAL is zero. */
6172 *src_reg = XEXP (x, 0);
6173 *mult_val = XEXP (x, 1);
6174 *add_val = const0_rtx;
6175 break;
6177 default:
6178 abort ();
6181 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6182 unless they are CONST_INT). */
6183 if (GET_CODE (*add_val) == USE)
6184 *add_val = XEXP (*add_val, 0);
6185 if (GET_CODE (*mult_val) == USE)
6186 *mult_val = XEXP (*mult_val, 0);
6188 if (is_addr)
6189 *pbenefit += address_cost (orig_x, addr_mode) - reg_address_cost;
6190 else
6191 *pbenefit += rtx_cost (orig_x, SET);
6193 /* Always return true if this is a giv so it will be detected as such,
6194 even if the benefit is zero or negative. This allows elimination
6195 of bivs that might otherwise not be eliminated. */
6196 return 1;
6199 /* Given an expression, X, try to form it as a linear function of a biv.
6200 We will canonicalize it to be of the form
6201 (plus (mult (BIV) (invar_1))
6202 (invar_2))
6203 with possible degeneracies.
6205 The invariant expressions must each be of a form that can be used as a
6206 machine operand. We surround then with a USE rtx (a hack, but localized
6207 and certainly unambiguous!) if not a CONST_INT for simplicity in this
6208 routine; it is the caller's responsibility to strip them.
6210 If no such canonicalization is possible (i.e., two biv's are used or an
6211 expression that is neither invariant nor a biv or giv), this routine
6212 returns 0.
6214 For a non-zero return, the result will have a code of CONST_INT, USE,
6215 REG (for a BIV), PLUS, or MULT. No other codes will occur.
6217 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
6219 static rtx sge_plus PARAMS ((enum machine_mode, rtx, rtx));
6220 static rtx sge_plus_constant PARAMS ((rtx, rtx));
6221 static int cmp_combine_givs_stats PARAMS ((const PTR, const PTR));
6222 static int cmp_recombine_givs_stats PARAMS ((const PTR, const PTR));
6224 static rtx
6225 simplify_giv_expr (loop, x, benefit)
6226 const struct loop *loop;
6227 rtx x;
6228 int *benefit;
6230 enum machine_mode mode = GET_MODE (x);
6231 rtx arg0, arg1;
6232 rtx tem;
6234 /* If this is not an integer mode, or if we cannot do arithmetic in this
6235 mode, this can't be a giv. */
6236 if (mode != VOIDmode
6237 && (GET_MODE_CLASS (mode) != MODE_INT
6238 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
6239 return NULL_RTX;
6241 switch (GET_CODE (x))
6243 case PLUS:
6244 arg0 = simplify_giv_expr (loop, XEXP (x, 0), benefit);
6245 arg1 = simplify_giv_expr (loop, XEXP (x, 1), benefit);
6246 if (arg0 == 0 || arg1 == 0)
6247 return NULL_RTX;
6249 /* Put constant last, CONST_INT last if both constant. */
6250 if ((GET_CODE (arg0) == USE
6251 || GET_CODE (arg0) == CONST_INT)
6252 && ! ((GET_CODE (arg0) == USE
6253 && GET_CODE (arg1) == USE)
6254 || GET_CODE (arg1) == CONST_INT))
6255 tem = arg0, arg0 = arg1, arg1 = tem;
6257 /* Handle addition of zero, then addition of an invariant. */
6258 if (arg1 == const0_rtx)
6259 return arg0;
6260 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
6261 switch (GET_CODE (arg0))
6263 case CONST_INT:
6264 case USE:
6265 /* Adding two invariants must result in an invariant, so enclose
6266 addition operation inside a USE and return it. */
6267 if (GET_CODE (arg0) == USE)
6268 arg0 = XEXP (arg0, 0);
6269 if (GET_CODE (arg1) == USE)
6270 arg1 = XEXP (arg1, 0);
6272 if (GET_CODE (arg0) == CONST_INT)
6273 tem = arg0, arg0 = arg1, arg1 = tem;
6274 if (GET_CODE (arg1) == CONST_INT)
6275 tem = sge_plus_constant (arg0, arg1);
6276 else
6277 tem = sge_plus (mode, arg0, arg1);
6279 if (GET_CODE (tem) != CONST_INT)
6280 tem = gen_rtx_USE (mode, tem);
6281 return tem;
6283 case REG:
6284 case MULT:
6285 /* biv + invar or mult + invar. Return sum. */
6286 return gen_rtx_PLUS (mode, arg0, arg1);
6288 case PLUS:
6289 /* (a + invar_1) + invar_2. Associate. */
6290 return
6291 simplify_giv_expr (loop,
6292 gen_rtx_PLUS (mode,
6293 XEXP (arg0, 0),
6294 gen_rtx_PLUS (mode,
6295 XEXP (arg0, 1),
6296 arg1)),
6297 benefit);
6299 default:
6300 abort ();
6303 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
6304 MULT to reduce cases. */
6305 if (GET_CODE (arg0) == REG)
6306 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
6307 if (GET_CODE (arg1) == REG)
6308 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
6310 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6311 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6312 Recurse to associate the second PLUS. */
6313 if (GET_CODE (arg1) == MULT)
6314 tem = arg0, arg0 = arg1, arg1 = tem;
6316 if (GET_CODE (arg1) == PLUS)
6317 return
6318 simplify_giv_expr (loop,
6319 gen_rtx_PLUS (mode,
6320 gen_rtx_PLUS (mode, arg0,
6321 XEXP (arg1, 0)),
6322 XEXP (arg1, 1)),
6323 benefit);
6325 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
6326 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
6327 return NULL_RTX;
6329 if (!rtx_equal_p (arg0, arg1))
6330 return NULL_RTX;
6332 return simplify_giv_expr (loop,
6333 gen_rtx_MULT (mode,
6334 XEXP (arg0, 0),
6335 gen_rtx_PLUS (mode,
6336 XEXP (arg0, 1),
6337 XEXP (arg1, 1))),
6338 benefit);
6340 case MINUS:
6341 /* Handle "a - b" as "a + b * (-1)". */
6342 return simplify_giv_expr (loop,
6343 gen_rtx_PLUS (mode,
6344 XEXP (x, 0),
6345 gen_rtx_MULT (mode,
6346 XEXP (x, 1),
6347 constm1_rtx)),
6348 benefit);
6350 case MULT:
6351 arg0 = simplify_giv_expr (loop, XEXP (x, 0), benefit);
6352 arg1 = simplify_giv_expr (loop, XEXP (x, 1), benefit);
6353 if (arg0 == 0 || arg1 == 0)
6354 return NULL_RTX;
6356 /* Put constant last, CONST_INT last if both constant. */
6357 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
6358 && GET_CODE (arg1) != CONST_INT)
6359 tem = arg0, arg0 = arg1, arg1 = tem;
6361 /* If second argument is not now constant, not giv. */
6362 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
6363 return NULL_RTX;
6365 /* Handle multiply by 0 or 1. */
6366 if (arg1 == const0_rtx)
6367 return const0_rtx;
6369 else if (arg1 == const1_rtx)
6370 return arg0;
6372 switch (GET_CODE (arg0))
6374 case REG:
6375 /* biv * invar. Done. */
6376 return gen_rtx_MULT (mode, arg0, arg1);
6378 case CONST_INT:
6379 /* Product of two constants. */
6380 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
6382 case USE:
6383 /* invar * invar is a giv, but attempt to simplify it somehow. */
6384 if (GET_CODE (arg1) != CONST_INT)
6385 return NULL_RTX;
6387 arg0 = XEXP (arg0, 0);
6388 if (GET_CODE (arg0) == MULT)
6390 /* (invar_0 * invar_1) * invar_2. Associate. */
6391 return simplify_giv_expr (loop,
6392 gen_rtx_MULT (mode,
6393 XEXP (arg0, 0),
6394 gen_rtx_MULT (mode,
6395 XEXP (arg0,
6397 arg1)),
6398 benefit);
6400 /* Porpagate the MULT expressions to the intermost nodes. */
6401 else if (GET_CODE (arg0) == PLUS)
6403 /* (invar_0 + invar_1) * invar_2. Distribute. */
6404 return simplify_giv_expr (loop,
6405 gen_rtx_PLUS (mode,
6406 gen_rtx_MULT (mode,
6407 XEXP (arg0,
6409 arg1),
6410 gen_rtx_MULT (mode,
6411 XEXP (arg0,
6413 arg1)),
6414 benefit);
6416 return gen_rtx_USE (mode, gen_rtx_MULT (mode, arg0, arg1));
6418 case MULT:
6419 /* (a * invar_1) * invar_2. Associate. */
6420 return simplify_giv_expr (loop,
6421 gen_rtx_MULT (mode,
6422 XEXP (arg0, 0),
6423 gen_rtx_MULT (mode,
6424 XEXP (arg0, 1),
6425 arg1)),
6426 benefit);
6428 case PLUS:
6429 /* (a + invar_1) * invar_2. Distribute. */
6430 return simplify_giv_expr (loop,
6431 gen_rtx_PLUS (mode,
6432 gen_rtx_MULT (mode,
6433 XEXP (arg0, 0),
6434 arg1),
6435 gen_rtx_MULT (mode,
6436 XEXP (arg0, 1),
6437 arg1)),
6438 benefit);
6440 default:
6441 abort ();
6444 case ASHIFT:
6445 /* Shift by constant is multiply by power of two. */
6446 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6447 return 0;
6449 return
6450 simplify_giv_expr (loop,
6451 gen_rtx_MULT (mode,
6452 XEXP (x, 0),
6453 GEN_INT ((HOST_WIDE_INT) 1
6454 << INTVAL (XEXP (x, 1)))),
6455 benefit);
6457 case NEG:
6458 /* "-a" is "a * (-1)" */
6459 return simplify_giv_expr (loop,
6460 gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
6461 benefit);
6463 case NOT:
6464 /* "~a" is "-a - 1". Silly, but easy. */
6465 return simplify_giv_expr (loop,
6466 gen_rtx_MINUS (mode,
6467 gen_rtx_NEG (mode, XEXP (x, 0)),
6468 const1_rtx),
6469 benefit);
6471 case USE:
6472 /* Already in proper form for invariant. */
6473 return x;
6475 case REG:
6476 /* If this is a new register, we can't deal with it. */
6477 if (REGNO (x) >= max_reg_before_loop)
6478 return 0;
6480 /* Check for biv or giv. */
6481 switch (REG_IV_TYPE (REGNO (x)))
6483 case BASIC_INDUCT:
6484 return x;
6485 case GENERAL_INDUCT:
6487 struct induction *v = REG_IV_INFO (REGNO (x));
6489 /* Form expression from giv and add benefit. Ensure this giv
6490 can derive another and subtract any needed adjustment if so. */
6491 *benefit += v->benefit;
6492 if (v->cant_derive)
6493 return 0;
6495 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
6496 v->src_reg, v->mult_val),
6497 v->add_val);
6499 if (v->derive_adjustment)
6500 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
6501 return simplify_giv_expr (loop, tem, benefit);
6504 default:
6505 /* If it isn't an induction variable, and it is invariant, we
6506 may be able to simplify things further by looking through
6507 the bits we just moved outside the loop. */
6508 if (loop_invariant_p (loop, x) == 1)
6510 struct movable *m;
6512 for (m = the_movables; m ; m = m->next)
6513 if (rtx_equal_p (x, m->set_dest))
6515 /* Ok, we found a match. Substitute and simplify. */
6517 /* If we match another movable, we must use that, as
6518 this one is going away. */
6519 if (m->match)
6520 return simplify_giv_expr (loop, m->match->set_dest,
6521 benefit);
6523 /* If consec is non-zero, this is a member of a group of
6524 instructions that were moved together. We handle this
6525 case only to the point of seeking to the last insn and
6526 looking for a REG_EQUAL. Fail if we don't find one. */
6527 if (m->consec != 0)
6529 int i = m->consec;
6530 tem = m->insn;
6531 do { tem = NEXT_INSN (tem); } while (--i > 0);
6533 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
6534 if (tem)
6535 tem = XEXP (tem, 0);
6537 else
6539 tem = single_set (m->insn);
6540 if (tem)
6541 tem = SET_SRC (tem);
6544 if (tem)
6546 /* What we are most interested in is pointer
6547 arithmetic on invariants -- only take
6548 patterns we may be able to do something with. */
6549 if (GET_CODE (tem) == PLUS
6550 || GET_CODE (tem) == MULT
6551 || GET_CODE (tem) == ASHIFT
6552 || GET_CODE (tem) == CONST_INT
6553 || GET_CODE (tem) == SYMBOL_REF)
6555 tem = simplify_giv_expr (loop, tem, benefit);
6556 if (tem)
6557 return tem;
6559 else if (GET_CODE (tem) == CONST
6560 && GET_CODE (XEXP (tem, 0)) == PLUS
6561 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
6562 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
6564 tem = simplify_giv_expr (loop, XEXP (tem, 0),
6565 benefit);
6566 if (tem)
6567 return tem;
6570 break;
6573 break;
6576 /* Fall through to general case. */
6577 default:
6578 /* If invariant, return as USE (unless CONST_INT).
6579 Otherwise, not giv. */
6580 if (GET_CODE (x) == USE)
6581 x = XEXP (x, 0);
6583 if (loop_invariant_p (loop, x) == 1)
6585 if (GET_CODE (x) == CONST_INT)
6586 return x;
6587 if (GET_CODE (x) == CONST
6588 && GET_CODE (XEXP (x, 0)) == PLUS
6589 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6590 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
6591 x = XEXP (x, 0);
6592 return gen_rtx_USE (mode, x);
6594 else
6595 return 0;
6599 /* This routine folds invariants such that there is only ever one
6600 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6602 static rtx
6603 sge_plus_constant (x, c)
6604 rtx x, c;
6606 if (GET_CODE (x) == CONST_INT)
6607 return GEN_INT (INTVAL (x) + INTVAL (c));
6608 else if (GET_CODE (x) != PLUS)
6609 return gen_rtx_PLUS (GET_MODE (x), x, c);
6610 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6612 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6613 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
6615 else if (GET_CODE (XEXP (x, 0)) == PLUS
6616 || GET_CODE (XEXP (x, 1)) != PLUS)
6618 return gen_rtx_PLUS (GET_MODE (x),
6619 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
6621 else
6623 return gen_rtx_PLUS (GET_MODE (x),
6624 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
6628 static rtx
6629 sge_plus (mode, x, y)
6630 enum machine_mode mode;
6631 rtx x, y;
6633 while (GET_CODE (y) == PLUS)
6635 rtx a = XEXP (y, 0);
6636 if (GET_CODE (a) == CONST_INT)
6637 x = sge_plus_constant (x, a);
6638 else
6639 x = gen_rtx_PLUS (mode, x, a);
6640 y = XEXP (y, 1);
6642 if (GET_CODE (y) == CONST_INT)
6643 x = sge_plus_constant (x, y);
6644 else
6645 x = gen_rtx_PLUS (mode, x, y);
6646 return x;
6649 /* Help detect a giv that is calculated by several consecutive insns;
6650 for example,
6651 giv = biv * M
6652 giv = giv + A
6653 The caller has already identified the first insn P as having a giv as dest;
6654 we check that all other insns that set the same register follow
6655 immediately after P, that they alter nothing else,
6656 and that the result of the last is still a giv.
6658 The value is 0 if the reg set in P is not really a giv.
6659 Otherwise, the value is the amount gained by eliminating
6660 all the consecutive insns that compute the value.
6662 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
6663 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
6665 The coefficients of the ultimate giv value are stored in
6666 *MULT_VAL and *ADD_VAL. */
6668 static int
6669 consec_sets_giv (loop, first_benefit, p, src_reg, dest_reg,
6670 add_val, mult_val, last_consec_insn)
6671 const struct loop *loop;
6672 int first_benefit;
6673 rtx p;
6674 rtx src_reg;
6675 rtx dest_reg;
6676 rtx *add_val;
6677 rtx *mult_val;
6678 rtx *last_consec_insn;
6680 int count;
6681 enum rtx_code code;
6682 int benefit;
6683 rtx temp;
6684 rtx set;
6686 /* Indicate that this is a giv so that we can update the value produced in
6687 each insn of the multi-insn sequence.
6689 This induction structure will be used only by the call to
6690 general_induction_var below, so we can allocate it on our stack.
6691 If this is a giv, our caller will replace the induct var entry with
6692 a new induction structure. */
6693 struct induction *v
6694 = (struct induction *) alloca (sizeof (struct induction));
6695 v->src_reg = src_reg;
6696 v->mult_val = *mult_val;
6697 v->add_val = *add_val;
6698 v->benefit = first_benefit;
6699 v->cant_derive = 0;
6700 v->derive_adjustment = 0;
6702 REG_IV_TYPE (REGNO (dest_reg)) = GENERAL_INDUCT;
6703 REG_IV_INFO (REGNO (dest_reg)) = v;
6705 count = VARRAY_INT (n_times_set, REGNO (dest_reg)) - 1;
6707 while (count > 0)
6709 p = NEXT_INSN (p);
6710 code = GET_CODE (p);
6712 /* If libcall, skip to end of call sequence. */
6713 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
6714 p = XEXP (temp, 0);
6716 if (code == INSN
6717 && (set = single_set (p))
6718 && GET_CODE (SET_DEST (set)) == REG
6719 && SET_DEST (set) == dest_reg
6720 && (general_induction_var (loop, SET_SRC (set), &src_reg,
6721 add_val, mult_val, 0, &benefit, VOIDmode)
6722 /* Giv created by equivalent expression. */
6723 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
6724 && general_induction_var (loop, XEXP (temp, 0), &src_reg,
6725 add_val, mult_val, 0, &benefit,
6726 VOIDmode)))
6727 && src_reg == v->src_reg)
6729 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
6730 benefit += libcall_benefit (p);
6732 count--;
6733 v->mult_val = *mult_val;
6734 v->add_val = *add_val;
6735 v->benefit = benefit;
6737 else if (code != NOTE)
6739 /* Allow insns that set something other than this giv to a
6740 constant. Such insns are needed on machines which cannot
6741 include long constants and should not disqualify a giv. */
6742 if (code == INSN
6743 && (set = single_set (p))
6744 && SET_DEST (set) != dest_reg
6745 && CONSTANT_P (SET_SRC (set)))
6746 continue;
6748 REG_IV_TYPE (REGNO (dest_reg)) = UNKNOWN_INDUCT;
6749 return 0;
6753 *last_consec_insn = p;
6754 return v->benefit;
6757 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6758 represented by G1. If no such expression can be found, or it is clear that
6759 it cannot possibly be a valid address, 0 is returned.
6761 To perform the computation, we note that
6762 G1 = x * v + a and
6763 G2 = y * v + b
6764 where `v' is the biv.
6766 So G2 = (y/b) * G1 + (b - a*y/x).
6768 Note that MULT = y/x.
6770 Update: A and B are now allowed to be additive expressions such that
6771 B contains all variables in A. That is, computing B-A will not require
6772 subtracting variables. */
6774 static rtx
6775 express_from_1 (a, b, mult)
6776 rtx a, b, mult;
6778 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
6780 if (mult == const0_rtx)
6781 return b;
6783 /* If MULT is not 1, we cannot handle A with non-constants, since we
6784 would then be required to subtract multiples of the registers in A.
6785 This is theoretically possible, and may even apply to some Fortran
6786 constructs, but it is a lot of work and we do not attempt it here. */
6788 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
6789 return NULL_RTX;
6791 /* In general these structures are sorted top to bottom (down the PLUS
6792 chain), but not left to right across the PLUS. If B is a higher
6793 order giv than A, we can strip one level and recurse. If A is higher
6794 order, we'll eventually bail out, but won't know that until the end.
6795 If they are the same, we'll strip one level around this loop. */
6797 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
6799 rtx ra, rb, oa, ob, tmp;
6801 ra = XEXP (a, 0), oa = XEXP (a, 1);
6802 if (GET_CODE (ra) == PLUS)
6803 tmp = ra, ra = oa, oa = tmp;
6805 rb = XEXP (b, 0), ob = XEXP (b, 1);
6806 if (GET_CODE (rb) == PLUS)
6807 tmp = rb, rb = ob, ob = tmp;
6809 if (rtx_equal_p (ra, rb))
6810 /* We matched: remove one reg completely. */
6811 a = oa, b = ob;
6812 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
6813 /* An alternate match. */
6814 a = oa, b = rb;
6815 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
6816 /* An alternate match. */
6817 a = ra, b = ob;
6818 else
6820 /* Indicates an extra register in B. Strip one level from B and
6821 recurse, hoping B was the higher order expression. */
6822 ob = express_from_1 (a, ob, mult);
6823 if (ob == NULL_RTX)
6824 return NULL_RTX;
6825 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
6829 /* Here we are at the last level of A, go through the cases hoping to
6830 get rid of everything but a constant. */
6832 if (GET_CODE (a) == PLUS)
6834 rtx ra, oa;
6836 ra = XEXP (a, 0), oa = XEXP (a, 1);
6837 if (rtx_equal_p (oa, b))
6838 oa = ra;
6839 else if (!rtx_equal_p (ra, b))
6840 return NULL_RTX;
6842 if (GET_CODE (oa) != CONST_INT)
6843 return NULL_RTX;
6845 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
6847 else if (GET_CODE (a) == CONST_INT)
6849 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
6851 else if (CONSTANT_P (a))
6853 return simplify_gen_binary (MINUS, GET_MODE (b), const0_rtx, a);
6855 else if (GET_CODE (b) == PLUS)
6857 if (rtx_equal_p (a, XEXP (b, 0)))
6858 return XEXP (b, 1);
6859 else if (rtx_equal_p (a, XEXP (b, 1)))
6860 return XEXP (b, 0);
6861 else
6862 return NULL_RTX;
6864 else if (rtx_equal_p (a, b))
6865 return const0_rtx;
6867 return NULL_RTX;
6871 express_from (g1, g2)
6872 struct induction *g1, *g2;
6874 rtx mult, add;
6876 /* The value that G1 will be multiplied by must be a constant integer. Also,
6877 the only chance we have of getting a valid address is if b*c/a (see above
6878 for notation) is also an integer. */
6879 if (GET_CODE (g1->mult_val) == CONST_INT
6880 && GET_CODE (g2->mult_val) == CONST_INT)
6882 if (g1->mult_val == const0_rtx
6883 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
6884 return NULL_RTX;
6885 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
6887 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
6888 mult = const1_rtx;
6889 else
6891 /* ??? Find out if the one is a multiple of the other? */
6892 return NULL_RTX;
6895 add = express_from_1 (g1->add_val, g2->add_val, mult);
6896 if (add == NULL_RTX)
6898 /* Failed. If we've got a multiplication factor between G1 and G2,
6899 scale G1's addend and try again. */
6900 if (INTVAL (mult) > 1)
6902 rtx g1_add_val = g1->add_val;
6903 if (GET_CODE (g1_add_val) == MULT
6904 && GET_CODE (XEXP (g1_add_val, 1)) == CONST_INT)
6906 HOST_WIDE_INT m;
6907 m = INTVAL (mult) * INTVAL (XEXP (g1_add_val, 1));
6908 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val),
6909 XEXP (g1_add_val, 0), GEN_INT (m));
6911 else
6913 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val), g1_add_val,
6914 mult);
6917 add = express_from_1 (g1_add_val, g2->add_val, const1_rtx);
6920 if (add == NULL_RTX)
6921 return NULL_RTX;
6923 /* Form simplified final result. */
6924 if (mult == const0_rtx)
6925 return add;
6926 else if (mult == const1_rtx)
6927 mult = g1->dest_reg;
6928 else
6929 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
6931 if (add == const0_rtx)
6932 return mult;
6933 else
6935 if (GET_CODE (add) == PLUS
6936 && CONSTANT_P (XEXP (add, 1)))
6938 rtx tem = XEXP (add, 1);
6939 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
6940 add = tem;
6943 return gen_rtx_PLUS (g2->mode, mult, add);
6948 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6949 represented by G1. This indicates that G2 should be combined with G1 and
6950 that G2 can use (either directly or via an address expression) a register
6951 used to represent G1. */
6953 static rtx
6954 combine_givs_p (g1, g2)
6955 struct induction *g1, *g2;
6957 rtx tem = express_from (g1, g2);
6959 /* If these givs are identical, they can be combined. We use the results
6960 of express_from because the addends are not in a canonical form, so
6961 rtx_equal_p is a weaker test. */
6962 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
6963 combination to be the other way round. */
6964 if (tem == g1->dest_reg
6965 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
6967 return g1->dest_reg;
6970 /* If G2 can be expressed as a function of G1 and that function is valid
6971 as an address and no more expensive than using a register for G2,
6972 the expression of G2 in terms of G1 can be used. */
6973 if (tem != NULL_RTX
6974 && g2->giv_type == DEST_ADDR
6975 && memory_address_p (g2->mem_mode, tem)
6976 /* ??? Looses, especially with -fforce-addr, where *g2->location
6977 will always be a register, and so anything more complicated
6978 gets discarded. */
6979 #if 0
6980 #ifdef ADDRESS_COST
6981 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location)
6982 #else
6983 && rtx_cost (tem, MEM) <= rtx_cost (*g2->location, MEM)
6984 #endif
6985 #endif
6988 return tem;
6991 return NULL_RTX;
6994 struct combine_givs_stats
6996 int giv_number;
6997 int total_benefit;
7000 static int
7001 cmp_combine_givs_stats (xp, yp)
7002 const PTR xp;
7003 const PTR yp;
7005 const struct combine_givs_stats * const x =
7006 (const struct combine_givs_stats *) xp;
7007 const struct combine_givs_stats * const y =
7008 (const struct combine_givs_stats *) yp;
7009 int d;
7010 d = y->total_benefit - x->total_benefit;
7011 /* Stabilize the sort. */
7012 if (!d)
7013 d = x->giv_number - y->giv_number;
7014 return d;
7017 /* Check all pairs of givs for iv_class BL and see if any can be combined with
7018 any other. If so, point SAME to the giv combined with and set NEW_REG to
7019 be an expression (in terms of the other giv's DEST_REG) equivalent to the
7020 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
7022 static void
7023 combine_givs (bl)
7024 struct iv_class *bl;
7026 /* Additional benefit to add for being combined multiple times. */
7027 const int extra_benefit = 3;
7029 struct induction *g1, *g2, **giv_array;
7030 int i, j, k, giv_count;
7031 struct combine_givs_stats *stats;
7032 rtx *can_combine;
7034 /* Count givs, because bl->giv_count is incorrect here. */
7035 giv_count = 0;
7036 for (g1 = bl->giv; g1; g1 = g1->next_iv)
7037 if (!g1->ignore)
7038 giv_count++;
7040 giv_array
7041 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
7042 i = 0;
7043 for (g1 = bl->giv; g1; g1 = g1->next_iv)
7044 if (!g1->ignore)
7045 giv_array[i++] = g1;
7047 stats = (struct combine_givs_stats *) xcalloc (giv_count, sizeof (*stats));
7048 can_combine = (rtx *) xcalloc (giv_count, giv_count * sizeof(rtx));
7050 for (i = 0; i < giv_count; i++)
7052 int this_benefit;
7053 rtx single_use;
7055 g1 = giv_array[i];
7056 stats[i].giv_number = i;
7058 /* If a DEST_REG GIV is used only once, do not allow it to combine
7059 with anything, for in doing so we will gain nothing that cannot
7060 be had by simply letting the GIV with which we would have combined
7061 to be reduced on its own. The losage shows up in particular with
7062 DEST_ADDR targets on hosts with reg+reg addressing, though it can
7063 be seen elsewhere as well. */
7064 if (g1->giv_type == DEST_REG
7065 && (single_use = VARRAY_RTX (reg_single_usage, REGNO (g1->dest_reg)))
7066 && single_use != const0_rtx)
7067 continue;
7069 this_benefit = g1->benefit;
7070 /* Add an additional weight for zero addends. */
7071 if (g1->no_const_addval)
7072 this_benefit += 1;
7074 for (j = 0; j < giv_count; j++)
7076 rtx this_combine;
7078 g2 = giv_array[j];
7079 if (g1 != g2
7080 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
7082 can_combine[i*giv_count + j] = this_combine;
7083 this_benefit += g2->benefit + extra_benefit;
7086 stats[i].total_benefit = this_benefit;
7089 /* Iterate, combining until we can't. */
7090 restart:
7091 qsort (stats, giv_count, sizeof(*stats), cmp_combine_givs_stats);
7093 if (loop_dump_stream)
7095 fprintf (loop_dump_stream, "Sorted combine statistics:\n");
7096 for (k = 0; k < giv_count; k++)
7098 g1 = giv_array[stats[k].giv_number];
7099 if (!g1->combined_with && !g1->same)
7100 fprintf (loop_dump_stream, " {%d, %d}",
7101 INSN_UID (giv_array[stats[k].giv_number]->insn),
7102 stats[k].total_benefit);
7104 putc ('\n', loop_dump_stream);
7107 for (k = 0; k < giv_count; k++)
7109 int g1_add_benefit = 0;
7111 i = stats[k].giv_number;
7112 g1 = giv_array[i];
7114 /* If it has already been combined, skip. */
7115 if (g1->combined_with || g1->same)
7116 continue;
7118 for (j = 0; j < giv_count; j++)
7120 g2 = giv_array[j];
7121 if (g1 != g2 && can_combine[i*giv_count + j]
7122 /* If it has already been combined, skip. */
7123 && ! g2->same && ! g2->combined_with)
7125 int l;
7127 g2->new_reg = can_combine[i*giv_count + j];
7128 g2->same = g1;
7129 g1->combined_with++;
7130 g1->lifetime += g2->lifetime;
7132 g1_add_benefit += g2->benefit;
7134 /* ??? The new final_[bg]iv_value code does a much better job
7135 of finding replaceable giv's, and hence this code may no
7136 longer be necessary. */
7137 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
7138 g1_add_benefit -= copy_cost;
7140 /* To help optimize the next set of combinations, remove
7141 this giv from the benefits of other potential mates. */
7142 for (l = 0; l < giv_count; ++l)
7144 int m = stats[l].giv_number;
7145 if (can_combine[m*giv_count + j])
7146 stats[l].total_benefit -= g2->benefit + extra_benefit;
7149 if (loop_dump_stream)
7150 fprintf (loop_dump_stream,
7151 "giv at %d combined with giv at %d\n",
7152 INSN_UID (g2->insn), INSN_UID (g1->insn));
7156 /* To help optimize the next set of combinations, remove
7157 this giv from the benefits of other potential mates. */
7158 if (g1->combined_with)
7160 for (j = 0; j < giv_count; ++j)
7162 int m = stats[j].giv_number;
7163 if (can_combine[m*giv_count + i])
7164 stats[j].total_benefit -= g1->benefit + extra_benefit;
7167 g1->benefit += g1_add_benefit;
7169 /* We've finished with this giv, and everything it touched.
7170 Restart the combination so that proper weights for the
7171 rest of the givs are properly taken into account. */
7172 /* ??? Ideally we would compact the arrays at this point, so
7173 as to not cover old ground. But sanely compacting
7174 can_combine is tricky. */
7175 goto restart;
7179 /* Clean up. */
7180 free (stats);
7181 free (can_combine);
7184 struct recombine_givs_stats
7186 int giv_number;
7187 int start_luid, end_luid;
7190 /* Used below as comparison function for qsort. We want a ascending luid
7191 when scanning the array starting at the end, thus the arguments are
7192 used in reverse. */
7193 static int
7194 cmp_recombine_givs_stats (xp, yp)
7195 const PTR xp;
7196 const PTR yp;
7198 const struct recombine_givs_stats * const x =
7199 (const struct recombine_givs_stats *) xp;
7200 const struct recombine_givs_stats * const y =
7201 (const struct recombine_givs_stats *) yp;
7202 int d;
7203 d = y->start_luid - x->start_luid;
7204 /* Stabilize the sort. */
7205 if (!d)
7206 d = y->giv_number - x->giv_number;
7207 return d;
7210 /* Scan X, which is a part of INSN, for the end of life of a giv. Also
7211 look for the start of life of a giv where the start has not been seen
7212 yet to unlock the search for the end of its life.
7213 Only consider givs that belong to BIV.
7214 Return the total number of lifetime ends that have been found. */
7215 static int
7216 find_life_end (x, stats, insn, biv)
7217 rtx x, insn, biv;
7218 struct recombine_givs_stats *stats;
7220 enum rtx_code code;
7221 const char *fmt;
7222 int i, j;
7223 int retval;
7225 code = GET_CODE (x);
7226 switch (code)
7228 case SET:
7230 rtx reg = SET_DEST (x);
7231 if (GET_CODE (reg) == REG)
7233 int regno = REGNO (reg);
7234 struct induction *v = REG_IV_INFO (regno);
7236 if (REG_IV_TYPE (regno) == GENERAL_INDUCT
7237 && ! v->ignore
7238 && v->src_reg == biv
7239 && stats[v->ix].end_luid <= 0)
7241 /* If we see a 0 here for end_luid, it means that we have
7242 scanned the entire loop without finding any use at all.
7243 We must not predicate this code on a start_luid match
7244 since that would make the test fail for givs that have
7245 been hoisted out of inner loops. */
7246 if (stats[v->ix].end_luid == 0)
7248 stats[v->ix].end_luid = stats[v->ix].start_luid;
7249 return 1 + find_life_end (SET_SRC (x), stats, insn, biv);
7251 else if (stats[v->ix].start_luid == INSN_LUID (insn))
7252 stats[v->ix].end_luid = 0;
7254 return find_life_end (SET_SRC (x), stats, insn, biv);
7256 break;
7258 case REG:
7260 int regno = REGNO (x);
7261 struct induction *v = REG_IV_INFO (regno);
7263 if (REG_IV_TYPE (regno) == GENERAL_INDUCT
7264 && ! v->ignore
7265 && v->src_reg == biv
7266 && stats[v->ix].end_luid == 0)
7268 while (INSN_UID (insn) >= max_uid_for_loop)
7269 insn = NEXT_INSN (insn);
7270 stats[v->ix].end_luid = INSN_LUID (insn);
7271 return 1;
7273 return 0;
7275 case LABEL_REF:
7276 case CONST_DOUBLE:
7277 case CONST_INT:
7278 case CONST:
7279 return 0;
7280 default:
7281 break;
7283 fmt = GET_RTX_FORMAT (code);
7284 retval = 0;
7285 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7287 if (fmt[i] == 'e')
7288 retval += find_life_end (XEXP (x, i), stats, insn, biv);
7290 else if (fmt[i] == 'E')
7291 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7292 retval += find_life_end (XVECEXP (x, i, j), stats, insn, biv);
7294 return retval;
7297 /* For each giv that has been combined with another, look if
7298 we can combine it with the most recently used one instead.
7299 This tends to shorten giv lifetimes, and helps the next step:
7300 try to derive givs from other givs. */
7301 static void
7302 recombine_givs (loop, bl, unroll_p)
7303 const struct loop *loop;
7304 struct iv_class *bl;
7305 int unroll_p;
7307 struct induction *v, **giv_array, *last_giv;
7308 struct recombine_givs_stats *stats;
7309 int giv_count;
7310 int i, rescan;
7311 int ends_need_computing;
7313 for (giv_count = 0, v = bl->giv; v; v = v->next_iv)
7315 if (! v->ignore)
7316 giv_count++;
7318 giv_array
7319 = (struct induction **) xmalloc (giv_count * sizeof (struct induction *));
7320 stats = (struct recombine_givs_stats *) xmalloc (giv_count * sizeof *stats);
7322 /* Initialize stats and set up the ix field for each giv in stats to name
7323 the corresponding index into stats. */
7324 for (i = 0, v = bl->giv; v; v = v->next_iv)
7326 rtx p;
7328 if (v->ignore)
7329 continue;
7330 giv_array[i] = v;
7331 stats[i].giv_number = i;
7332 /* If this giv has been hoisted out of an inner loop, use the luid of
7333 the previous insn. */
7334 for (p = v->insn; INSN_UID (p) >= max_uid_for_loop; )
7335 p = PREV_INSN (p);
7336 stats[i].start_luid = INSN_LUID (p);
7337 i++;
7340 qsort (stats, giv_count, sizeof(*stats), cmp_recombine_givs_stats);
7342 /* Set up the ix field for each giv in stats to name
7343 the corresponding index into stats, and
7344 do the actual most-recently-used recombination. */
7345 for (last_giv = 0, i = giv_count - 1; i >= 0; i--)
7347 v = giv_array[stats[i].giv_number];
7348 v->ix = i;
7349 if (v->same)
7351 struct induction *old_same = v->same;
7352 rtx new_combine;
7354 /* combine_givs_p actually says if we can make this transformation.
7355 The other tests are here only to avoid keeping a giv alive
7356 that could otherwise be eliminated. */
7357 if (last_giv
7358 && ((old_same->maybe_dead && ! old_same->combined_with)
7359 || ! last_giv->maybe_dead
7360 || last_giv->combined_with)
7361 && (new_combine = combine_givs_p (last_giv, v)))
7363 old_same->combined_with--;
7364 v->new_reg = new_combine;
7365 v->same = last_giv;
7366 last_giv->combined_with++;
7367 /* No need to update lifetimes / benefits here since we have
7368 already decided what to reduce. */
7370 if (loop_dump_stream)
7372 fprintf (loop_dump_stream,
7373 "giv at %d recombined with giv at %d as ",
7374 INSN_UID (v->insn), INSN_UID (last_giv->insn));
7375 print_rtl (loop_dump_stream, v->new_reg);
7376 putc ('\n', loop_dump_stream);
7378 continue;
7380 v = v->same;
7382 else if (v->giv_type != DEST_REG)
7383 continue;
7384 if (! last_giv
7385 || (last_giv->maybe_dead && ! last_giv->combined_with)
7386 || ! v->maybe_dead
7387 || v->combined_with)
7388 last_giv = v;
7391 ends_need_computing = 0;
7392 /* For each DEST_REG giv, compute lifetime starts, and try to compute
7393 lifetime ends from regscan info. */
7394 for (i = giv_count - 1; i >= 0; i--)
7396 v = giv_array[stats[i].giv_number];
7397 if (v->ignore)
7398 continue;
7399 if (v->giv_type == DEST_ADDR)
7401 /* Loop unrolling of an inner loop can even create new DEST_REG
7402 givs. */
7403 rtx p;
7404 for (p = v->insn; INSN_UID (p) >= max_uid_for_loop; )
7405 p = PREV_INSN (p);
7406 stats[i].start_luid = stats[i].end_luid = INSN_LUID (p);
7407 if (p != v->insn)
7408 stats[i].end_luid++;
7410 else /* v->giv_type == DEST_REG */
7412 if (v->last_use)
7414 stats[i].start_luid = INSN_LUID (v->insn);
7415 stats[i].end_luid = INSN_LUID (v->last_use);
7417 else if (INSN_UID (v->insn) >= max_uid_for_loop)
7419 rtx p;
7420 /* This insn has been created by loop optimization on an inner
7421 loop. We don't have a proper start_luid that will match
7422 when we see the first set. But we do know that there will
7423 be no use before the set, so we can set end_luid to 0 so that
7424 we'll start looking for the last use right away. */
7425 for (p = PREV_INSN (v->insn); INSN_UID (p) >= max_uid_for_loop; )
7426 p = PREV_INSN (p);
7427 stats[i].start_luid = INSN_LUID (p);
7428 stats[i].end_luid = 0;
7429 ends_need_computing++;
7431 else
7433 int regno = REGNO (v->dest_reg);
7434 int count = VARRAY_INT (n_times_set, regno) - 1;
7435 rtx p = v->insn;
7437 /* Find the first insn that sets the giv, so that we can verify
7438 if this giv's lifetime wraps around the loop. We also need
7439 the luid of the first setting insn in order to detect the
7440 last use properly. */
7441 while (count)
7443 p = prev_nonnote_insn (p);
7444 if (reg_set_p (v->dest_reg, p))
7445 count--;
7448 stats[i].start_luid = INSN_LUID (p);
7449 if (stats[i].start_luid > uid_luid[REGNO_FIRST_UID (regno)])
7451 stats[i].end_luid = -1;
7452 ends_need_computing++;
7454 else
7456 stats[i].end_luid = uid_luid[REGNO_LAST_UID (regno)];
7457 if (stats[i].end_luid > INSN_LUID (loop->end))
7459 stats[i].end_luid = -1;
7460 ends_need_computing++;
7467 /* If the regscan information was unconclusive for one or more DEST_REG
7468 givs, scan the all insn in the loop to find out lifetime ends. */
7469 if (ends_need_computing)
7471 rtx biv = bl->biv->src_reg;
7472 rtx p = loop->end;
7476 if (p == loop->start)
7477 p = loop->end;
7478 p = PREV_INSN (p);
7479 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
7480 continue;
7481 ends_need_computing -= find_life_end (PATTERN (p), stats, p, biv);
7483 while (ends_need_computing);
7486 /* Set start_luid back to the last insn that sets the giv. This allows
7487 more combinations. */
7488 for (i = giv_count - 1; i >= 0; i--)
7490 v = giv_array[stats[i].giv_number];
7491 if (v->ignore)
7492 continue;
7493 if (INSN_UID (v->insn) < max_uid_for_loop)
7494 stats[i].start_luid = INSN_LUID (v->insn);
7497 /* Now adjust lifetime ends by taking combined givs into account. */
7498 for (i = giv_count - 1; i >= 0; i--)
7500 unsigned luid;
7501 int j;
7503 v = giv_array[stats[i].giv_number];
7504 if (v->ignore)
7505 continue;
7506 if (v->same && ! v->same->ignore)
7508 j = v->same->ix;
7509 luid = stats[i].start_luid;
7510 /* Use unsigned arithmetic to model loop wrap-around. */
7511 if (luid - stats[j].start_luid
7512 > (unsigned) stats[j].end_luid - stats[j].start_luid)
7513 stats[j].end_luid = luid;
7517 qsort (stats, giv_count, sizeof(*stats), cmp_recombine_givs_stats);
7519 /* Try to derive DEST_REG givs from previous DEST_REG givs with the
7520 same mult_val and non-overlapping lifetime. This reduces register
7521 pressure.
7522 Once we find a DEST_REG giv that is suitable to derive others from,
7523 we set last_giv to this giv, and try to derive as many other DEST_REG
7524 givs from it without joining overlapping lifetimes. If we then
7525 encounter a DEST_REG giv that we can't derive, we set rescan to the
7526 index for this giv (unless rescan is already set).
7527 When we are finished with the current LAST_GIV (i.e. the inner loop
7528 terminates), we start again with rescan, which then becomes the new
7529 LAST_GIV. */
7530 for (i = giv_count - 1; i >= 0; i = rescan)
7532 int life_start = 0, life_end = 0;
7534 for (last_giv = 0, rescan = -1; i >= 0; i--)
7536 rtx sum;
7538 v = giv_array[stats[i].giv_number];
7539 if (v->giv_type != DEST_REG || v->derived_from || v->same)
7540 continue;
7541 if (! last_giv)
7543 /* Don't use a giv that's likely to be dead to derive
7544 others - that would be likely to keep that giv alive. */
7545 if (! v->maybe_dead || v->combined_with)
7547 last_giv = v;
7548 life_start = stats[i].start_luid;
7549 life_end = stats[i].end_luid;
7551 continue;
7553 /* Use unsigned arithmetic to model loop wrap around. */
7554 if (((unsigned) stats[i].start_luid - life_start
7555 >= (unsigned) life_end - life_start)
7556 && ((unsigned) stats[i].end_luid - life_start
7557 > (unsigned) life_end - life_start)
7558 /* Check that the giv insn we're about to use for deriving
7559 precedes all uses of that giv. Note that initializing the
7560 derived giv would defeat the purpose of reducing register
7561 pressure.
7562 ??? We could arrange to move the insn. */
7563 && ((unsigned) stats[i].end_luid - INSN_LUID (loop->start)
7564 > (unsigned) stats[i].start_luid - INSN_LUID (loop->start))
7565 && rtx_equal_p (last_giv->mult_val, v->mult_val)
7566 /* ??? Could handle libcalls, but would need more logic. */
7567 && ! find_reg_note (v->insn, REG_RETVAL, NULL_RTX)
7568 /* We would really like to know if for any giv that v
7569 is combined with, v->insn or any intervening biv increment
7570 dominates that combined giv. However, we
7571 don't have this detailed control flow information.
7572 N.B. since last_giv will be reduced, it is valid
7573 anywhere in the loop, so we don't need to check the
7574 validity of last_giv.
7575 We rely here on the fact that v->always_executed implies that
7576 there is no jump to someplace else in the loop before the
7577 giv insn, and hence any insn that is executed before the
7578 giv insn in the loop will have a lower luid. */
7579 && (v->always_executed || ! v->combined_with)
7580 && (sum = express_from (last_giv, v))
7581 /* Make sure we don't make the add more expensive. ADD_COST
7582 doesn't take different costs of registers and constants into
7583 account, so compare the cost of the actual SET_SRCs. */
7584 && (rtx_cost (sum, SET)
7585 <= rtx_cost (SET_SRC (single_set (v->insn)), SET))
7586 /* ??? unroll can't understand anything but reg + const_int
7587 sums. It would be cleaner to fix unroll. */
7588 && ((GET_CODE (sum) == PLUS
7589 && GET_CODE (XEXP (sum, 0)) == REG
7590 && GET_CODE (XEXP (sum, 1)) == CONST_INT)
7591 || ! unroll_p)
7592 && validate_change (v->insn, &PATTERN (v->insn),
7593 gen_rtx_SET (VOIDmode, v->dest_reg, sum), 0))
7595 v->derived_from = last_giv;
7596 life_end = stats[i].end_luid;
7598 if (loop_dump_stream)
7600 fprintf (loop_dump_stream,
7601 "giv at %d derived from %d as ",
7602 INSN_UID (v->insn), INSN_UID (last_giv->insn));
7603 print_rtl (loop_dump_stream, sum);
7604 putc ('\n', loop_dump_stream);
7607 else if (rescan < 0)
7608 rescan = i;
7612 /* Clean up. */
7613 free (giv_array);
7614 free (stats);
7617 /* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
7619 void
7620 emit_iv_add_mult (b, m, a, reg, insert_before)
7621 rtx b; /* initial value of basic induction variable */
7622 rtx m; /* multiplicative constant */
7623 rtx a; /* additive constant */
7624 rtx reg; /* destination register */
7625 rtx insert_before;
7627 rtx seq;
7628 rtx result;
7630 /* Prevent unexpected sharing of these rtx. */
7631 a = copy_rtx (a);
7632 b = copy_rtx (b);
7634 /* Increase the lifetime of any invariants moved further in code. */
7635 update_reg_last_use (a, insert_before);
7636 update_reg_last_use (b, insert_before);
7637 update_reg_last_use (m, insert_before);
7639 start_sequence ();
7640 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 0);
7641 if (reg != result)
7642 emit_move_insn (reg, result);
7643 seq = gen_sequence ();
7644 end_sequence ();
7646 emit_insn_before (seq, insert_before);
7648 /* It is entirely possible that the expansion created lots of new
7649 registers. Iterate over the sequence we just created and
7650 record them all. */
7652 if (GET_CODE (seq) == SEQUENCE)
7654 int i;
7655 for (i = 0; i < XVECLEN (seq, 0); ++i)
7657 rtx set = single_set (XVECEXP (seq, 0, i));
7658 if (set && GET_CODE (SET_DEST (set)) == REG)
7659 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
7662 else if (GET_CODE (seq) == SET
7663 && GET_CODE (SET_DEST (seq)) == REG)
7664 record_base_value (REGNO (SET_DEST (seq)), SET_SRC (seq), 0);
7667 /* Test whether A * B can be computed without
7668 an actual multiply insn. Value is 1 if so. */
7670 static int
7671 product_cheap_p (a, b)
7672 rtx a;
7673 rtx b;
7675 int i;
7676 rtx tmp;
7677 struct obstack *old_rtl_obstack = rtl_obstack;
7678 char *storage = (char *) obstack_alloc (&temp_obstack, 0);
7679 int win = 1;
7681 /* If only one is constant, make it B. */
7682 if (GET_CODE (a) == CONST_INT)
7683 tmp = a, a = b, b = tmp;
7685 /* If first constant, both constant, so don't need multiply. */
7686 if (GET_CODE (a) == CONST_INT)
7687 return 1;
7689 /* If second not constant, neither is constant, so would need multiply. */
7690 if (GET_CODE (b) != CONST_INT)
7691 return 0;
7693 /* One operand is constant, so might not need multiply insn. Generate the
7694 code for the multiply and see if a call or multiply, or long sequence
7695 of insns is generated. */
7697 rtl_obstack = &temp_obstack;
7698 start_sequence ();
7699 expand_mult (GET_MODE (a), a, b, NULL_RTX, 0);
7700 tmp = gen_sequence ();
7701 end_sequence ();
7703 if (GET_CODE (tmp) == SEQUENCE)
7705 if (XVEC (tmp, 0) == 0)
7706 win = 1;
7707 else if (XVECLEN (tmp, 0) > 3)
7708 win = 0;
7709 else
7710 for (i = 0; i < XVECLEN (tmp, 0); i++)
7712 rtx insn = XVECEXP (tmp, 0, i);
7714 if (GET_CODE (insn) != INSN
7715 || (GET_CODE (PATTERN (insn)) == SET
7716 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
7717 || (GET_CODE (PATTERN (insn)) == PARALLEL
7718 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
7719 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
7721 win = 0;
7722 break;
7726 else if (GET_CODE (tmp) == SET
7727 && GET_CODE (SET_SRC (tmp)) == MULT)
7728 win = 0;
7729 else if (GET_CODE (tmp) == PARALLEL
7730 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
7731 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
7732 win = 0;
7734 /* Free any storage we obtained in generating this multiply and restore rtl
7735 allocation to its normal obstack. */
7736 obstack_free (&temp_obstack, storage);
7737 rtl_obstack = old_rtl_obstack;
7739 return win;
7742 /* Check to see if loop can be terminated by a "decrement and branch until
7743 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
7744 Also try reversing an increment loop to a decrement loop
7745 to see if the optimization can be performed.
7746 Value is nonzero if optimization was performed. */
7748 /* This is useful even if the architecture doesn't have such an insn,
7749 because it might change a loops which increments from 0 to n to a loop
7750 which decrements from n to 0. A loop that decrements to zero is usually
7751 faster than one that increments from zero. */
7753 /* ??? This could be rewritten to use some of the loop unrolling procedures,
7754 such as approx_final_value, biv_total_increment, loop_iterations, and
7755 final_[bg]iv_value. */
7757 static int
7758 check_dbra_loop (loop, insn_count)
7759 struct loop *loop;
7760 int insn_count;
7762 struct iv_class *bl;
7763 rtx reg;
7764 rtx jump_label;
7765 rtx final_value;
7766 rtx start_value;
7767 rtx new_add_val;
7768 rtx comparison;
7769 rtx before_comparison;
7770 rtx p;
7771 rtx jump;
7772 rtx first_compare;
7773 int compare_and_branch;
7774 rtx loop_start = loop->start;
7775 rtx loop_end = loop->end;
7776 struct loop_info *loop_info = LOOP_INFO (loop);
7778 /* If last insn is a conditional branch, and the insn before tests a
7779 register value, try to optimize it. Otherwise, we can't do anything. */
7781 jump = PREV_INSN (loop_end);
7782 comparison = get_condition_for_loop (loop, jump);
7783 if (comparison == 0)
7784 return 0;
7785 if (!onlyjump_p (jump))
7786 return 0;
7788 /* Try to compute whether the compare/branch at the loop end is one or
7789 two instructions. */
7790 get_condition (jump, &first_compare);
7791 if (first_compare == jump)
7792 compare_and_branch = 1;
7793 else if (first_compare == prev_nonnote_insn (jump))
7794 compare_and_branch = 2;
7795 else
7796 return 0;
7798 /* Check all of the bivs to see if the compare uses one of them.
7799 Skip biv's set more than once because we can't guarantee that
7800 it will be zero on the last iteration. Also skip if the biv is
7801 used between its update and the test insn. */
7803 for (bl = loop_iv_list; bl; bl = bl->next)
7805 if (bl->biv_count == 1
7806 && ! bl->biv->maybe_multiple
7807 && bl->biv->dest_reg == XEXP (comparison, 0)
7808 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
7809 first_compare))
7810 break;
7813 if (! bl)
7814 return 0;
7816 /* Look for the case where the basic induction variable is always
7817 nonnegative, and equals zero on the last iteration.
7818 In this case, add a reg_note REG_NONNEG, which allows the
7819 m68k DBRA instruction to be used. */
7821 if (((GET_CODE (comparison) == GT
7822 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
7823 && INTVAL (XEXP (comparison, 1)) == -1)
7824 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
7825 && GET_CODE (bl->biv->add_val) == CONST_INT
7826 && INTVAL (bl->biv->add_val) < 0)
7828 /* Initial value must be greater than 0,
7829 init_val % -dec_value == 0 to ensure that it equals zero on
7830 the last iteration */
7832 if (GET_CODE (bl->initial_value) == CONST_INT
7833 && INTVAL (bl->initial_value) > 0
7834 && (INTVAL (bl->initial_value)
7835 % (-INTVAL (bl->biv->add_val))) == 0)
7837 /* register always nonnegative, add REG_NOTE to branch */
7838 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
7839 REG_NOTES (jump)
7840 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
7841 REG_NOTES (jump));
7842 bl->nonneg = 1;
7844 return 1;
7847 /* If the decrement is 1 and the value was tested as >= 0 before
7848 the loop, then we can safely optimize. */
7849 for (p = loop_start; p; p = PREV_INSN (p))
7851 if (GET_CODE (p) == CODE_LABEL)
7852 break;
7853 if (GET_CODE (p) != JUMP_INSN)
7854 continue;
7856 before_comparison = get_condition_for_loop (loop, p);
7857 if (before_comparison
7858 && XEXP (before_comparison, 0) == bl->biv->dest_reg
7859 && GET_CODE (before_comparison) == LT
7860 && XEXP (before_comparison, 1) == const0_rtx
7861 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
7862 && INTVAL (bl->biv->add_val) == -1)
7864 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
7865 REG_NOTES (jump)
7866 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
7867 REG_NOTES (jump));
7868 bl->nonneg = 1;
7870 return 1;
7874 else if (GET_CODE (bl->biv->add_val) == CONST_INT
7875 && INTVAL (bl->biv->add_val) > 0)
7877 /* Try to change inc to dec, so can apply above optimization. */
7878 /* Can do this if:
7879 all registers modified are induction variables or invariant,
7880 all memory references have non-overlapping addresses
7881 (obviously true if only one write)
7882 allow 2 insns for the compare/jump at the end of the loop. */
7883 /* Also, we must avoid any instructions which use both the reversed
7884 biv and another biv. Such instructions will fail if the loop is
7885 reversed. We meet this condition by requiring that either
7886 no_use_except_counting is true, or else that there is only
7887 one biv. */
7888 int num_nonfixed_reads = 0;
7889 /* 1 if the iteration var is used only to count iterations. */
7890 int no_use_except_counting = 0;
7891 /* 1 if the loop has no memory store, or it has a single memory store
7892 which is reversible. */
7893 int reversible_mem_store = 1;
7895 if (bl->giv_count == 0 && ! loop->exit_count)
7897 rtx bivreg = regno_reg_rtx[bl->regno];
7899 /* If there are no givs for this biv, and the only exit is the
7900 fall through at the end of the loop, then
7901 see if perhaps there are no uses except to count. */
7902 no_use_except_counting = 1;
7903 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7904 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7906 rtx set = single_set (p);
7908 if (set && GET_CODE (SET_DEST (set)) == REG
7909 && REGNO (SET_DEST (set)) == bl->regno)
7910 /* An insn that sets the biv is okay. */
7912 else if ((p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
7913 || p == prev_nonnote_insn (loop_end))
7914 && reg_mentioned_p (bivreg, PATTERN (p)))
7916 /* If either of these insns uses the biv and sets a pseudo
7917 that has more than one usage, then the biv has uses
7918 other than counting since it's used to derive a value
7919 that is used more than one time. */
7920 int note_set_pseudo_multiple_uses_retval = 0;
7921 note_stores (PATTERN (p), note_set_pseudo_multiple_uses,
7922 &note_set_pseudo_multiple_uses_retval);
7923 if (note_set_pseudo_multiple_uses_retval)
7925 no_use_except_counting = 0;
7926 break;
7929 else if (reg_mentioned_p (bivreg, PATTERN (p)))
7931 no_use_except_counting = 0;
7932 break;
7937 if (no_use_except_counting)
7938 ; /* no need to worry about MEMs. */
7939 else if (num_mem_sets <= 1)
7941 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7942 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7943 num_nonfixed_reads += count_nonfixed_reads (loop, PATTERN (p));
7945 /* If the loop has a single store, and the destination address is
7946 invariant, then we can't reverse the loop, because this address
7947 might then have the wrong value at loop exit.
7948 This would work if the source was invariant also, however, in that
7949 case, the insn should have been moved out of the loop. */
7951 if (num_mem_sets == 1)
7953 struct induction *v;
7955 reversible_mem_store
7956 = (! unknown_address_altered
7957 && ! unknown_constant_address_altered
7958 && ! loop_invariant_p (loop,
7959 XEXP (XEXP (loop_store_mems, 0),
7960 0)));
7962 /* If the store depends on a register that is set after the
7963 store, it depends on the initial value, and is thus not
7964 reversible. */
7965 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
7967 if (v->giv_type == DEST_REG
7968 && reg_mentioned_p (v->dest_reg,
7969 PATTERN (first_loop_store_insn))
7970 && loop_insn_first_p (first_loop_store_insn, v->insn))
7971 reversible_mem_store = 0;
7975 else
7976 return 0;
7978 /* This code only acts for innermost loops. Also it simplifies
7979 the memory address check by only reversing loops with
7980 zero or one memory access.
7981 Two memory accesses could involve parts of the same array,
7982 and that can't be reversed.
7983 If the biv is used only for counting, than we don't need to worry
7984 about all these things. */
7986 if ((num_nonfixed_reads <= 1
7987 && ! loop_info->has_call
7988 && ! loop_info->has_volatile
7989 && reversible_mem_store
7990 && (bl->giv_count + bl->biv_count + num_mem_sets
7991 + num_movables + compare_and_branch == insn_count)
7992 && (bl == loop_iv_list && bl->next == 0))
7993 || no_use_except_counting)
7995 rtx tem;
7997 /* Loop can be reversed. */
7998 if (loop_dump_stream)
7999 fprintf (loop_dump_stream, "Can reverse loop\n");
8001 /* Now check other conditions:
8003 The increment must be a constant, as must the initial value,
8004 and the comparison code must be LT.
8006 This test can probably be improved since +/- 1 in the constant
8007 can be obtained by changing LT to LE and vice versa; this is
8008 confusing. */
8010 if (comparison
8011 /* for constants, LE gets turned into LT */
8012 && (GET_CODE (comparison) == LT
8013 || (GET_CODE (comparison) == LE
8014 && no_use_except_counting)))
8016 HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
8017 rtx initial_value, comparison_value;
8018 int nonneg = 0;
8019 enum rtx_code cmp_code;
8020 int comparison_const_width;
8021 unsigned HOST_WIDE_INT comparison_sign_mask;
8023 add_val = INTVAL (bl->biv->add_val);
8024 comparison_value = XEXP (comparison, 1);
8025 if (GET_MODE (comparison_value) == VOIDmode)
8026 comparison_const_width
8027 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
8028 else
8029 comparison_const_width
8030 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
8031 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
8032 comparison_const_width = HOST_BITS_PER_WIDE_INT;
8033 comparison_sign_mask
8034 = (unsigned HOST_WIDE_INT)1 << (comparison_const_width - 1);
8036 /* If the comparison value is not a loop invariant, then we
8037 can not reverse this loop.
8039 ??? If the insns which initialize the comparison value as
8040 a whole compute an invariant result, then we could move
8041 them out of the loop and proceed with loop reversal. */
8042 if (! loop_invariant_p (loop, comparison_value))
8043 return 0;
8045 if (GET_CODE (comparison_value) == CONST_INT)
8046 comparison_val = INTVAL (comparison_value);
8047 initial_value = bl->initial_value;
8049 /* Normalize the initial value if it is an integer and
8050 has no other use except as a counter. This will allow
8051 a few more loops to be reversed. */
8052 if (no_use_except_counting
8053 && GET_CODE (comparison_value) == CONST_INT
8054 && GET_CODE (initial_value) == CONST_INT)
8056 comparison_val = comparison_val - INTVAL (bl->initial_value);
8057 /* The code below requires comparison_val to be a multiple
8058 of add_val in order to do the loop reversal, so
8059 round up comparison_val to a multiple of add_val.
8060 Since comparison_value is constant, we know that the
8061 current comparison code is LT. */
8062 comparison_val = comparison_val + add_val - 1;
8063 comparison_val
8064 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
8065 /* We postpone overflow checks for COMPARISON_VAL here;
8066 even if there is an overflow, we might still be able to
8067 reverse the loop, if converting the loop exit test to
8068 NE is possible. */
8069 initial_value = const0_rtx;
8072 /* First check if we can do a vanilla loop reversal. */
8073 if (initial_value == const0_rtx
8074 /* If we have a decrement_and_branch_on_count,
8075 prefer the NE test, since this will allow that
8076 instruction to be generated. Note that we must
8077 use a vanilla loop reversal if the biv is used to
8078 calculate a giv or has a non-counting use. */
8079 #if ! defined (HAVE_decrement_and_branch_until_zero) \
8080 && defined (HAVE_decrement_and_branch_on_count)
8081 && (! (add_val == 1 && loop->vtop
8082 && (bl->biv_count == 0
8083 || no_use_except_counting)))
8084 #endif
8085 && GET_CODE (comparison_value) == CONST_INT
8086 /* Now do postponed overflow checks on COMPARISON_VAL. */
8087 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
8088 & comparison_sign_mask))
8090 /* Register will always be nonnegative, with value
8091 0 on last iteration */
8092 add_adjust = add_val;
8093 nonneg = 1;
8094 cmp_code = GE;
8096 else if (add_val == 1 && loop->vtop
8097 && (bl->biv_count == 0
8098 || no_use_except_counting))
8100 add_adjust = 0;
8101 cmp_code = NE;
8103 else
8104 return 0;
8106 if (GET_CODE (comparison) == LE)
8107 add_adjust -= add_val;
8109 /* If the initial value is not zero, or if the comparison
8110 value is not an exact multiple of the increment, then we
8111 can not reverse this loop. */
8112 if (initial_value == const0_rtx
8113 && GET_CODE (comparison_value) == CONST_INT)
8115 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
8116 return 0;
8118 else
8120 if (! no_use_except_counting || add_val != 1)
8121 return 0;
8124 final_value = comparison_value;
8126 /* Reset these in case we normalized the initial value
8127 and comparison value above. */
8128 if (GET_CODE (comparison_value) == CONST_INT
8129 && GET_CODE (initial_value) == CONST_INT)
8131 comparison_value = GEN_INT (comparison_val);
8132 final_value
8133 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
8135 bl->initial_value = initial_value;
8137 /* Save some info needed to produce the new insns. */
8138 reg = bl->biv->dest_reg;
8139 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
8140 if (jump_label == pc_rtx)
8141 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
8142 new_add_val = GEN_INT (- INTVAL (bl->biv->add_val));
8144 /* Set start_value; if this is not a CONST_INT, we need
8145 to generate a SUB.
8146 Initialize biv to start_value before loop start.
8147 The old initializing insn will be deleted as a
8148 dead store by flow.c. */
8149 if (initial_value == const0_rtx
8150 && GET_CODE (comparison_value) == CONST_INT)
8152 start_value = GEN_INT (comparison_val - add_adjust);
8153 emit_insn_before (gen_move_insn (reg, start_value),
8154 loop_start);
8156 else if (GET_CODE (initial_value) == CONST_INT)
8158 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
8159 enum machine_mode mode = GET_MODE (reg);
8160 enum insn_code icode
8161 = add_optab->handlers[(int) mode].insn_code;
8163 if (! (*insn_data[icode].operand[0].predicate) (reg, mode)
8164 || ! ((*insn_data[icode].operand[1].predicate)
8165 (comparison_value, mode))
8166 || ! ((*insn_data[icode].operand[2].predicate)
8167 (offset, mode)))
8168 return 0;
8169 start_value
8170 = gen_rtx_PLUS (mode, comparison_value, offset);
8171 emit_insn_before ((GEN_FCN (icode)
8172 (reg, comparison_value, offset)),
8173 loop_start);
8174 if (GET_CODE (comparison) == LE)
8175 final_value = gen_rtx_PLUS (mode, comparison_value,
8176 GEN_INT (add_val));
8178 else if (! add_adjust)
8180 enum machine_mode mode = GET_MODE (reg);
8181 enum insn_code icode
8182 = sub_optab->handlers[(int) mode].insn_code;
8183 if (! (*insn_data[icode].operand[0].predicate) (reg, mode)
8184 || ! ((*insn_data[icode].operand[1].predicate)
8185 (comparison_value, mode))
8186 || ! ((*insn_data[icode].operand[2].predicate)
8187 (initial_value, mode)))
8188 return 0;
8189 start_value
8190 = gen_rtx_MINUS (mode, comparison_value, initial_value);
8191 emit_insn_before ((GEN_FCN (icode)
8192 (reg, comparison_value, initial_value)),
8193 loop_start);
8195 else
8196 /* We could handle the other cases too, but it'll be
8197 better to have a testcase first. */
8198 return 0;
8200 /* We may not have a single insn which can increment a reg, so
8201 create a sequence to hold all the insns from expand_inc. */
8202 start_sequence ();
8203 expand_inc (reg, new_add_val);
8204 tem = gen_sequence ();
8205 end_sequence ();
8207 p = emit_insn_before (tem, bl->biv->insn);
8208 delete_insn (bl->biv->insn);
8210 /* Update biv info to reflect its new status. */
8211 bl->biv->insn = p;
8212 bl->initial_value = start_value;
8213 bl->biv->add_val = new_add_val;
8215 /* Update loop info. */
8216 loop_info->initial_value = reg;
8217 loop_info->initial_equiv_value = reg;
8218 loop_info->final_value = const0_rtx;
8219 loop_info->final_equiv_value = const0_rtx;
8220 loop_info->comparison_value = const0_rtx;
8221 loop_info->comparison_code = cmp_code;
8222 loop_info->increment = new_add_val;
8224 /* Inc LABEL_NUSES so that delete_insn will
8225 not delete the label. */
8226 LABEL_NUSES (XEXP (jump_label, 0)) ++;
8228 /* Emit an insn after the end of the loop to set the biv's
8229 proper exit value if it is used anywhere outside the loop. */
8230 if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
8231 || ! bl->init_insn
8232 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
8233 emit_insn_after (gen_move_insn (reg, final_value),
8234 loop_end);
8236 /* Delete compare/branch at end of loop. */
8237 delete_insn (PREV_INSN (loop_end));
8238 if (compare_and_branch == 2)
8239 delete_insn (first_compare);
8241 /* Add new compare/branch insn at end of loop. */
8242 start_sequence ();
8243 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
8244 GET_MODE (reg), 0, 0,
8245 XEXP (jump_label, 0));
8246 tem = gen_sequence ();
8247 end_sequence ();
8248 emit_jump_insn_before (tem, loop_end);
8250 for (tem = PREV_INSN (loop_end);
8251 tem && GET_CODE (tem) != JUMP_INSN;
8252 tem = PREV_INSN (tem))
8255 if (tem)
8256 JUMP_LABEL (tem) = XEXP (jump_label, 0);
8258 if (nonneg)
8260 if (tem)
8262 /* Increment of LABEL_NUSES done above. */
8263 /* Register is now always nonnegative,
8264 so add REG_NONNEG note to the branch. */
8265 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, reg,
8266 REG_NOTES (tem));
8268 bl->nonneg = 1;
8271 /* No insn may reference both the reversed and another biv or it
8272 will fail (see comment near the top of the loop reversal
8273 code).
8274 Earlier on, we have verified that the biv has no use except
8275 counting, or it is the only biv in this function.
8276 However, the code that computes no_use_except_counting does
8277 not verify reg notes. It's possible to have an insn that
8278 references another biv, and has a REG_EQUAL note with an
8279 expression based on the reversed biv. To avoid this case,
8280 remove all REG_EQUAL notes based on the reversed biv
8281 here. */
8282 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8283 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
8285 rtx *pnote;
8286 rtx set = single_set (p);
8287 /* If this is a set of a GIV based on the reversed biv, any
8288 REG_EQUAL notes should still be correct. */
8289 if (! set
8290 || GET_CODE (SET_DEST (set)) != REG
8291 || (size_t) REGNO (SET_DEST (set)) >= reg_iv_type->num_elements
8292 || REG_IV_TYPE (REGNO (SET_DEST (set))) != GENERAL_INDUCT
8293 || REG_IV_INFO (REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
8294 for (pnote = &REG_NOTES (p); *pnote;)
8296 if (REG_NOTE_KIND (*pnote) == REG_EQUAL
8297 && reg_mentioned_p (regno_reg_rtx[bl->regno],
8298 XEXP (*pnote, 0)))
8299 *pnote = XEXP (*pnote, 1);
8300 else
8301 pnote = &XEXP (*pnote, 1);
8305 /* Mark that this biv has been reversed. Each giv which depends
8306 on this biv, and which is also live past the end of the loop
8307 will have to be fixed up. */
8309 bl->reversed = 1;
8311 if (loop_dump_stream)
8313 fprintf (loop_dump_stream, "Reversed loop");
8314 if (bl->nonneg)
8315 fprintf (loop_dump_stream, " and added reg_nonneg\n");
8316 else
8317 fprintf (loop_dump_stream, "\n");
8320 return 1;
8325 return 0;
8328 /* Verify whether the biv BL appears to be eliminable,
8329 based on the insns in the loop that refer to it.
8331 If ELIMINATE_P is non-zero, actually do the elimination.
8333 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8334 determine whether invariant insns should be placed inside or at the
8335 start of the loop. */
8337 static int
8338 maybe_eliminate_biv (loop, bl, eliminate_p, threshold, insn_count)
8339 const struct loop *loop;
8340 struct iv_class *bl;
8341 int eliminate_p;
8342 int threshold, insn_count;
8344 rtx reg = bl->biv->dest_reg;
8345 rtx loop_start = loop->start;
8346 rtx loop_end = loop->end;
8347 rtx p;
8349 /* Scan all insns in the loop, stopping if we find one that uses the
8350 biv in a way that we cannot eliminate. */
8352 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8354 enum rtx_code code = GET_CODE (p);
8355 rtx where = threshold >= insn_count ? loop_start : p;
8357 /* If this is a libcall that sets a giv, skip ahead to its end. */
8358 if (GET_RTX_CLASS (code) == 'i')
8360 rtx note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
8362 if (note)
8364 rtx last = XEXP (note, 0);
8365 rtx set = single_set (last);
8367 if (set && GET_CODE (SET_DEST (set)) == REG)
8369 unsigned int regno = REGNO (SET_DEST (set));
8371 if (regno < max_reg_before_loop
8372 && REG_IV_TYPE (regno) == GENERAL_INDUCT
8373 && REG_IV_INFO (regno)->src_reg == bl->biv->src_reg)
8374 p = last;
8378 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
8379 && reg_mentioned_p (reg, PATTERN (p))
8380 && ! maybe_eliminate_biv_1 (loop, PATTERN (p), p, bl,
8381 eliminate_p, where))
8383 if (loop_dump_stream)
8384 fprintf (loop_dump_stream,
8385 "Cannot eliminate biv %d: biv used in insn %d.\n",
8386 bl->regno, INSN_UID (p));
8387 break;
8391 if (p == loop_end)
8393 if (loop_dump_stream)
8394 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
8395 bl->regno, eliminate_p ? "was" : "can be");
8396 return 1;
8399 return 0;
8402 /* INSN and REFERENCE are instructions in the same insn chain.
8403 Return non-zero if INSN is first. */
8406 loop_insn_first_p (insn, reference)
8407 rtx insn, reference;
8409 rtx p, q;
8411 for (p = insn, q = reference; ;)
8413 /* Start with test for not first so that INSN == REFERENCE yields not
8414 first. */
8415 if (q == insn || ! p)
8416 return 0;
8417 if (p == reference || ! q)
8418 return 1;
8420 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
8421 previous insn, hence the <= comparison below does not work if
8422 P is a note. */
8423 if (INSN_UID (p) < max_uid_for_loop
8424 && INSN_UID (q) < max_uid_for_loop
8425 && GET_CODE (p) != NOTE)
8426 return INSN_LUID (p) <= INSN_LUID (q);
8428 if (INSN_UID (p) >= max_uid_for_loop
8429 || GET_CODE (p) == NOTE)
8430 p = NEXT_INSN (p);
8431 if (INSN_UID (q) >= max_uid_for_loop)
8432 q = NEXT_INSN (q);
8436 /* We are trying to eliminate BIV in INSN using GIV. Return non-zero if
8437 the offset that we have to take into account due to auto-increment /
8438 div derivation is zero. */
8439 static int
8440 biv_elimination_giv_has_0_offset (biv, giv, insn)
8441 struct induction *biv, *giv;
8442 rtx insn;
8444 /* If the giv V had the auto-inc address optimization applied
8445 to it, and INSN occurs between the giv insn and the biv
8446 insn, then we'd have to adjust the value used here.
8447 This is rare, so we don't bother to make this possible. */
8448 if (giv->auto_inc_opt
8449 && ((loop_insn_first_p (giv->insn, insn)
8450 && loop_insn_first_p (insn, biv->insn))
8451 || (loop_insn_first_p (biv->insn, insn)
8452 && loop_insn_first_p (insn, giv->insn))))
8453 return 0;
8455 /* If the giv V was derived from another giv, and INSN does
8456 not occur between the giv insn and the biv insn, then we'd
8457 have to adjust the value used here. This is rare, so we don't
8458 bother to make this possible. */
8459 if (giv->derived_from
8460 && ! (giv->always_executed
8461 && loop_insn_first_p (giv->insn, insn)
8462 && loop_insn_first_p (insn, biv->insn)))
8463 return 0;
8464 if (giv->same
8465 && giv->same->derived_from
8466 && ! (giv->same->always_executed
8467 && loop_insn_first_p (giv->same->insn, insn)
8468 && loop_insn_first_p (insn, biv->insn)))
8469 return 0;
8471 return 1;
8474 /* If BL appears in X (part of the pattern of INSN), see if we can
8475 eliminate its use. If so, return 1. If not, return 0.
8477 If BIV does not appear in X, return 1.
8479 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
8480 where extra insns should be added. Depending on how many items have been
8481 moved out of the loop, it will either be before INSN or at the start of
8482 the loop. */
8484 static int
8485 maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where)
8486 const struct loop *loop;
8487 rtx x, insn;
8488 struct iv_class *bl;
8489 int eliminate_p;
8490 rtx where;
8492 enum rtx_code code = GET_CODE (x);
8493 rtx reg = bl->biv->dest_reg;
8494 enum machine_mode mode = GET_MODE (reg);
8495 struct induction *v;
8496 rtx arg, tem;
8497 #ifdef HAVE_cc0
8498 rtx new;
8499 #endif
8500 int arg_operand;
8501 const char *fmt;
8502 int i, j;
8504 switch (code)
8506 case REG:
8507 /* If we haven't already been able to do something with this BIV,
8508 we can't eliminate it. */
8509 if (x == reg)
8510 return 0;
8511 return 1;
8513 case SET:
8514 /* If this sets the BIV, it is not a problem. */
8515 if (SET_DEST (x) == reg)
8516 return 1;
8518 /* If this is an insn that defines a giv, it is also ok because
8519 it will go away when the giv is reduced. */
8520 for (v = bl->giv; v; v = v->next_iv)
8521 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
8522 return 1;
8524 #ifdef HAVE_cc0
8525 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
8527 /* Can replace with any giv that was reduced and
8528 that has (MULT_VAL != 0) and (ADD_VAL == 0).
8529 Require a constant for MULT_VAL, so we know it's nonzero.
8530 ??? We disable this optimization to avoid potential
8531 overflows. */
8533 for (v = bl->giv; v; v = v->next_iv)
8534 if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
8535 && v->add_val == const0_rtx
8536 && ! v->ignore && ! v->maybe_dead && v->always_computable
8537 && v->mode == mode
8538 && 0)
8540 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8541 continue;
8543 if (! eliminate_p)
8544 return 1;
8546 /* If the giv has the opposite direction of change,
8547 then reverse the comparison. */
8548 if (INTVAL (v->mult_val) < 0)
8549 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
8550 const0_rtx, v->new_reg);
8551 else
8552 new = v->new_reg;
8554 /* We can probably test that giv's reduced reg. */
8555 if (validate_change (insn, &SET_SRC (x), new, 0))
8556 return 1;
8559 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8560 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
8561 Require a constant for MULT_VAL, so we know it's nonzero.
8562 ??? Do this only if ADD_VAL is a pointer to avoid a potential
8563 overflow problem. */
8565 for (v = bl->giv; v; v = v->next_iv)
8566 if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
8567 && ! v->ignore && ! v->maybe_dead && v->always_computable
8568 && v->mode == mode
8569 && (GET_CODE (v->add_val) == SYMBOL_REF
8570 || GET_CODE (v->add_val) == LABEL_REF
8571 || GET_CODE (v->add_val) == CONST
8572 || (GET_CODE (v->add_val) == REG
8573 && REGNO_POINTER_FLAG (REGNO (v->add_val)))))
8575 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8576 continue;
8578 if (! eliminate_p)
8579 return 1;
8581 /* If the giv has the opposite direction of change,
8582 then reverse the comparison. */
8583 if (INTVAL (v->mult_val) < 0)
8584 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
8585 v->new_reg);
8586 else
8587 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
8588 copy_rtx (v->add_val));
8590 /* Replace biv with the giv's reduced register. */
8591 update_reg_last_use (v->add_val, insn);
8592 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8593 return 1;
8595 /* Insn doesn't support that constant or invariant. Copy it
8596 into a register (it will be a loop invariant.) */
8597 tem = gen_reg_rtx (GET_MODE (v->new_reg));
8599 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
8600 where);
8602 /* Substitute the new register for its invariant value in
8603 the compare expression. */
8604 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
8605 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8606 return 1;
8609 #endif
8610 break;
8612 case COMPARE:
8613 case EQ: case NE:
8614 case GT: case GE: case GTU: case GEU:
8615 case LT: case LE: case LTU: case LEU:
8616 /* See if either argument is the biv. */
8617 if (XEXP (x, 0) == reg)
8618 arg = XEXP (x, 1), arg_operand = 1;
8619 else if (XEXP (x, 1) == reg)
8620 arg = XEXP (x, 0), arg_operand = 0;
8621 else
8622 break;
8624 if (CONSTANT_P (arg))
8626 /* First try to replace with any giv that has constant positive
8627 mult_val and constant add_val. We might be able to support
8628 negative mult_val, but it seems complex to do it in general. */
8630 for (v = bl->giv; v; v = v->next_iv)
8631 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
8632 && (GET_CODE (v->add_val) == SYMBOL_REF
8633 || GET_CODE (v->add_val) == LABEL_REF
8634 || GET_CODE (v->add_val) == CONST
8635 || (GET_CODE (v->add_val) == REG
8636 && REGNO_POINTER_FLAG (REGNO (v->add_val))))
8637 && ! v->ignore && ! v->maybe_dead && v->always_computable
8638 && v->mode == mode)
8640 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8641 continue;
8643 if (! eliminate_p)
8644 return 1;
8646 /* Replace biv with the giv's reduced reg. */
8647 validate_change (insn, &XEXP (x, 1-arg_operand), v->new_reg, 1);
8649 /* If all constants are actually constant integers and
8650 the derived constant can be directly placed in the COMPARE,
8651 do so. */
8652 if (GET_CODE (arg) == CONST_INT
8653 && GET_CODE (v->mult_val) == CONST_INT
8654 && GET_CODE (v->add_val) == CONST_INT)
8656 validate_change (insn, &XEXP (x, arg_operand),
8657 GEN_INT (INTVAL (arg)
8658 * INTVAL (v->mult_val)
8659 + INTVAL (v->add_val)), 1);
8661 else
8663 /* Otherwise, load it into a register. */
8664 tem = gen_reg_rtx (mode);
8665 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
8666 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8668 if (apply_change_group ())
8669 return 1;
8672 /* Look for giv with positive constant mult_val and nonconst add_val.
8673 Insert insns to calculate new compare value.
8674 ??? Turn this off due to possible overflow. */
8676 for (v = bl->giv; v; v = v->next_iv)
8677 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
8678 && ! v->ignore && ! v->maybe_dead && v->always_computable
8679 && v->mode == mode
8680 && 0)
8682 rtx tem;
8684 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8685 continue;
8687 if (! eliminate_p)
8688 return 1;
8690 tem = gen_reg_rtx (mode);
8692 /* Replace biv with giv's reduced register. */
8693 validate_change (insn, &XEXP (x, 1 - arg_operand),
8694 v->new_reg, 1);
8696 /* Compute value to compare against. */
8697 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
8698 /* Use it in this insn. */
8699 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8700 if (apply_change_group ())
8701 return 1;
8704 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
8706 if (loop_invariant_p (loop, arg) == 1)
8708 /* Look for giv with constant positive mult_val and nonconst
8709 add_val. Insert insns to compute new compare value.
8710 ??? Turn this off due to possible overflow. */
8712 for (v = bl->giv; v; v = v->next_iv)
8713 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
8714 && ! v->ignore && ! v->maybe_dead && v->always_computable
8715 && v->mode == mode
8716 && 0)
8718 rtx tem;
8720 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8721 continue;
8723 if (! eliminate_p)
8724 return 1;
8726 tem = gen_reg_rtx (mode);
8728 /* Replace biv with giv's reduced register. */
8729 validate_change (insn, &XEXP (x, 1 - arg_operand),
8730 v->new_reg, 1);
8732 /* Compute value to compare against. */
8733 emit_iv_add_mult (arg, v->mult_val, v->add_val,
8734 tem, where);
8735 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8736 if (apply_change_group ())
8737 return 1;
8741 /* This code has problems. Basically, you can't know when
8742 seeing if we will eliminate BL, whether a particular giv
8743 of ARG will be reduced. If it isn't going to be reduced,
8744 we can't eliminate BL. We can try forcing it to be reduced,
8745 but that can generate poor code.
8747 The problem is that the benefit of reducing TV, below should
8748 be increased if BL can actually be eliminated, but this means
8749 we might have to do a topological sort of the order in which
8750 we try to process biv. It doesn't seem worthwhile to do
8751 this sort of thing now. */
8753 #if 0
8754 /* Otherwise the reg compared with had better be a biv. */
8755 if (GET_CODE (arg) != REG
8756 || REG_IV_TYPE (REGNO (arg)) != BASIC_INDUCT)
8757 return 0;
8759 /* Look for a pair of givs, one for each biv,
8760 with identical coefficients. */
8761 for (v = bl->giv; v; v = v->next_iv)
8763 struct induction *tv;
8765 if (v->ignore || v->maybe_dead || v->mode != mode)
8766 continue;
8768 for (tv = reg_biv_class[REGNO (arg)]->giv; tv; tv = tv->next_iv)
8769 if (! tv->ignore && ! tv->maybe_dead
8770 && rtx_equal_p (tv->mult_val, v->mult_val)
8771 && rtx_equal_p (tv->add_val, v->add_val)
8772 && tv->mode == mode)
8774 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8775 continue;
8777 if (! eliminate_p)
8778 return 1;
8780 /* Replace biv with its giv's reduced reg. */
8781 XEXP (x, 1-arg_operand) = v->new_reg;
8782 /* Replace other operand with the other giv's
8783 reduced reg. */
8784 XEXP (x, arg_operand) = tv->new_reg;
8785 return 1;
8788 #endif
8791 /* If we get here, the biv can't be eliminated. */
8792 return 0;
8794 case MEM:
8795 /* If this address is a DEST_ADDR giv, it doesn't matter if the
8796 biv is used in it, since it will be replaced. */
8797 for (v = bl->giv; v; v = v->next_iv)
8798 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
8799 return 1;
8800 break;
8802 default:
8803 break;
8806 /* See if any subexpression fails elimination. */
8807 fmt = GET_RTX_FORMAT (code);
8808 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8810 switch (fmt[i])
8812 case 'e':
8813 if (! maybe_eliminate_biv_1 (loop, XEXP (x, i), insn, bl,
8814 eliminate_p, where))
8815 return 0;
8816 break;
8818 case 'E':
8819 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8820 if (! maybe_eliminate_biv_1 (loop, XVECEXP (x, i, j), insn, bl,
8821 eliminate_p, where))
8822 return 0;
8823 break;
8827 return 1;
8830 /* Return nonzero if the last use of REG
8831 is in an insn following INSN in the same basic block. */
8833 static int
8834 last_use_this_basic_block (reg, insn)
8835 rtx reg;
8836 rtx insn;
8838 rtx n;
8839 for (n = insn;
8840 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
8841 n = NEXT_INSN (n))
8843 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
8844 return 1;
8846 return 0;
8849 /* Called via `note_stores' to record the initial value of a biv. Here we
8850 just record the location of the set and process it later. */
8852 static void
8853 record_initial (dest, set, data)
8854 rtx dest;
8855 rtx set;
8856 void *data ATTRIBUTE_UNUSED;
8858 struct iv_class *bl;
8860 if (GET_CODE (dest) != REG
8861 || REGNO (dest) >= max_reg_before_loop
8862 || REG_IV_TYPE (REGNO (dest)) != BASIC_INDUCT)
8863 return;
8865 bl = reg_biv_class[REGNO (dest)];
8867 /* If this is the first set found, record it. */
8868 if (bl->init_insn == 0)
8870 bl->init_insn = note_insn;
8871 bl->init_set = set;
8875 /* If any of the registers in X are "old" and currently have a last use earlier
8876 than INSN, update them to have a last use of INSN. Their actual last use
8877 will be the previous insn but it will not have a valid uid_luid so we can't
8878 use it. */
8880 static void
8881 update_reg_last_use (x, insn)
8882 rtx x;
8883 rtx insn;
8885 /* Check for the case where INSN does not have a valid luid. In this case,
8886 there is no need to modify the regno_last_uid, as this can only happen
8887 when code is inserted after the loop_end to set a pseudo's final value,
8888 and hence this insn will never be the last use of x. */
8889 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
8890 && INSN_UID (insn) < max_uid_for_loop
8891 && uid_luid[REGNO_LAST_UID (REGNO (x))] < uid_luid[INSN_UID (insn)])
8892 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
8893 else
8895 register int i, j;
8896 register const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
8897 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8899 if (fmt[i] == 'e')
8900 update_reg_last_use (XEXP (x, i), insn);
8901 else if (fmt[i] == 'E')
8902 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8903 update_reg_last_use (XVECEXP (x, i, j), insn);
8908 /* Given an insn INSN and condition COND, return the condition in a
8909 canonical form to simplify testing by callers. Specifically:
8911 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
8912 (2) Both operands will be machine operands; (cc0) will have been replaced.
8913 (3) If an operand is a constant, it will be the second operand.
8914 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
8915 for GE, GEU, and LEU.
8917 If the condition cannot be understood, or is an inequality floating-point
8918 comparison which needs to be reversed, 0 will be returned.
8920 If REVERSE is non-zero, then reverse the condition prior to canonizing it.
8922 If EARLIEST is non-zero, it is a pointer to a place where the earliest
8923 insn used in locating the condition was found. If a replacement test
8924 of the condition is desired, it should be placed in front of that
8925 insn and we will be sure that the inputs are still valid.
8927 If WANT_REG is non-zero, we wish the condition to be relative to that
8928 register, if possible. Therefore, do not canonicalize the condition
8929 further. */
8932 canonicalize_condition (insn, cond, reverse, earliest, want_reg)
8933 rtx insn;
8934 rtx cond;
8935 int reverse;
8936 rtx *earliest;
8937 rtx want_reg;
8939 enum rtx_code code;
8940 rtx prev = insn;
8941 rtx set;
8942 rtx tem;
8943 rtx op0, op1;
8944 int reverse_code = 0;
8945 int did_reverse_condition = 0;
8946 enum machine_mode mode;
8948 code = GET_CODE (cond);
8949 mode = GET_MODE (cond);
8950 op0 = XEXP (cond, 0);
8951 op1 = XEXP (cond, 1);
8953 if (reverse)
8955 code = reverse_condition (code);
8956 did_reverse_condition ^= 1;
8959 if (earliest)
8960 *earliest = insn;
8962 /* If we are comparing a register with zero, see if the register is set
8963 in the previous insn to a COMPARE or a comparison operation. Perform
8964 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
8965 in cse.c */
8967 while (GET_RTX_CLASS (code) == '<'
8968 && op1 == CONST0_RTX (GET_MODE (op0))
8969 && op0 != want_reg)
8971 /* Set non-zero when we find something of interest. */
8972 rtx x = 0;
8974 #ifdef HAVE_cc0
8975 /* If comparison with cc0, import actual comparison from compare
8976 insn. */
8977 if (op0 == cc0_rtx)
8979 if ((prev = prev_nonnote_insn (prev)) == 0
8980 || GET_CODE (prev) != INSN
8981 || (set = single_set (prev)) == 0
8982 || SET_DEST (set) != cc0_rtx)
8983 return 0;
8985 op0 = SET_SRC (set);
8986 op1 = CONST0_RTX (GET_MODE (op0));
8987 if (earliest)
8988 *earliest = prev;
8990 #endif
8992 /* If this is a COMPARE, pick up the two things being compared. */
8993 if (GET_CODE (op0) == COMPARE)
8995 op1 = XEXP (op0, 1);
8996 op0 = XEXP (op0, 0);
8997 continue;
8999 else if (GET_CODE (op0) != REG)
9000 break;
9002 /* Go back to the previous insn. Stop if it is not an INSN. We also
9003 stop if it isn't a single set or if it has a REG_INC note because
9004 we don't want to bother dealing with it. */
9006 if ((prev = prev_nonnote_insn (prev)) == 0
9007 || GET_CODE (prev) != INSN
9008 || FIND_REG_INC_NOTE (prev, 0)
9009 || (set = single_set (prev)) == 0)
9010 break;
9012 /* If this is setting OP0, get what it sets it to if it looks
9013 relevant. */
9014 if (rtx_equal_p (SET_DEST (set), op0))
9016 enum machine_mode inner_mode = GET_MODE (SET_SRC (set));
9018 /* ??? We may not combine comparisons done in a CCmode with
9019 comparisons not done in a CCmode. This is to aid targets
9020 like Alpha that have an IEEE compliant EQ instruction, and
9021 a non-IEEE compliant BEQ instruction. The use of CCmode is
9022 actually artificial, simply to prevent the combination, but
9023 should not affect other platforms.
9025 However, we must allow VOIDmode comparisons to match either
9026 CCmode or non-CCmode comparison, because some ports have
9027 modeless comparisons inside branch patterns.
9029 ??? This mode check should perhaps look more like the mode check
9030 in simplify_comparison in combine. */
9032 if ((GET_CODE (SET_SRC (set)) == COMPARE
9033 || (((code == NE
9034 || (code == LT
9035 && GET_MODE_CLASS (inner_mode) == MODE_INT
9036 && (GET_MODE_BITSIZE (inner_mode)
9037 <= HOST_BITS_PER_WIDE_INT)
9038 && (STORE_FLAG_VALUE
9039 & ((HOST_WIDE_INT) 1
9040 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9041 #ifdef FLOAT_STORE_FLAG_VALUE
9042 || (code == LT
9043 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9044 && (REAL_VALUE_NEGATIVE
9045 (FLOAT_STORE_FLAG_VALUE (inner_mode))))
9046 #endif
9048 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
9049 && (((GET_MODE_CLASS (mode) == MODE_CC)
9050 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9051 || mode == VOIDmode || inner_mode == VOIDmode))
9052 x = SET_SRC (set);
9053 else if (((code == EQ
9054 || (code == GE
9055 && (GET_MODE_BITSIZE (inner_mode)
9056 <= HOST_BITS_PER_WIDE_INT)
9057 && GET_MODE_CLASS (inner_mode) == MODE_INT
9058 && (STORE_FLAG_VALUE
9059 & ((HOST_WIDE_INT) 1
9060 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9061 #ifdef FLOAT_STORE_FLAG_VALUE
9062 || (code == GE
9063 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9064 && (REAL_VALUE_NEGATIVE
9065 (FLOAT_STORE_FLAG_VALUE (inner_mode))))
9066 #endif
9068 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
9069 && (((GET_MODE_CLASS (mode) == MODE_CC)
9070 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9071 || mode == VOIDmode || inner_mode == VOIDmode))
9074 /* We might have reversed a LT to get a GE here. But this wasn't
9075 actually the comparison of data, so we don't flag that we
9076 have had to reverse the condition. */
9077 did_reverse_condition ^= 1;
9078 reverse_code = 1;
9079 x = SET_SRC (set);
9081 else
9082 break;
9085 else if (reg_set_p (op0, prev))
9086 /* If this sets OP0, but not directly, we have to give up. */
9087 break;
9089 if (x)
9091 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9092 code = GET_CODE (x);
9093 if (reverse_code)
9095 code = reverse_condition (code);
9096 if (code == UNKNOWN)
9097 return 0;
9098 did_reverse_condition ^= 1;
9099 reverse_code = 0;
9102 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
9103 if (earliest)
9104 *earliest = prev;
9108 /* If constant is first, put it last. */
9109 if (CONSTANT_P (op0))
9110 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
9112 /* If OP0 is the result of a comparison, we weren't able to find what
9113 was really being compared, so fail. */
9114 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
9115 return 0;
9117 /* Canonicalize any ordered comparison with integers involving equality
9118 if we can do computations in the relevant mode and we do not
9119 overflow. */
9121 if (GET_CODE (op1) == CONST_INT
9122 && GET_MODE (op0) != VOIDmode
9123 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
9125 HOST_WIDE_INT const_val = INTVAL (op1);
9126 unsigned HOST_WIDE_INT uconst_val = const_val;
9127 unsigned HOST_WIDE_INT max_val
9128 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
9130 switch (code)
9132 case LE:
9133 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
9134 code = LT, op1 = GEN_INT (const_val + 1);
9135 break;
9137 /* When cross-compiling, const_val might be sign-extended from
9138 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
9139 case GE:
9140 if ((HOST_WIDE_INT) (const_val & max_val)
9141 != (((HOST_WIDE_INT) 1
9142 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
9143 code = GT, op1 = GEN_INT (const_val - 1);
9144 break;
9146 case LEU:
9147 if (uconst_val < max_val)
9148 code = LTU, op1 = GEN_INT (uconst_val + 1);
9149 break;
9151 case GEU:
9152 if (uconst_val != 0)
9153 code = GTU, op1 = GEN_INT (uconst_val - 1);
9154 break;
9156 default:
9157 break;
9161 /* If this was floating-point and we reversed anything other than an
9162 EQ or NE or (UN)ORDERED, return zero. */
9163 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
9164 && did_reverse_condition
9165 && code != NE && code != EQ && code != UNORDERED && code != ORDERED
9166 && ! flag_fast_math
9167 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
9168 return 0;
9170 #ifdef HAVE_cc0
9171 /* Never return CC0; return zero instead. */
9172 if (op0 == cc0_rtx)
9173 return 0;
9174 #endif
9176 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
9179 /* Given a jump insn JUMP, return the condition that will cause it to branch
9180 to its JUMP_LABEL. If the condition cannot be understood, or is an
9181 inequality floating-point comparison which needs to be reversed, 0 will
9182 be returned.
9184 If EARLIEST is non-zero, it is a pointer to a place where the earliest
9185 insn used in locating the condition was found. If a replacement test
9186 of the condition is desired, it should be placed in front of that
9187 insn and we will be sure that the inputs are still valid. */
9190 get_condition (jump, earliest)
9191 rtx jump;
9192 rtx *earliest;
9194 rtx cond;
9195 int reverse;
9196 rtx set;
9198 /* If this is not a standard conditional jump, we can't parse it. */
9199 if (GET_CODE (jump) != JUMP_INSN
9200 || ! any_condjump_p (jump))
9201 return 0;
9202 set = pc_set (jump);
9204 cond = XEXP (SET_SRC (set), 0);
9206 /* If this branches to JUMP_LABEL when the condition is false, reverse
9207 the condition. */
9208 reverse
9209 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
9210 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump);
9212 return canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX);
9215 /* Similar to above routine, except that we also put an invariant last
9216 unless both operands are invariants. */
9219 get_condition_for_loop (loop, x)
9220 const struct loop *loop;
9221 rtx x;
9223 rtx comparison = get_condition (x, NULL_PTR);
9225 if (comparison == 0
9226 || ! loop_invariant_p (loop, XEXP (comparison, 0))
9227 || loop_invariant_p (loop, XEXP (comparison, 1)))
9228 return comparison;
9230 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
9231 XEXP (comparison, 1), XEXP (comparison, 0));
9234 #ifdef HAVE_decrement_and_branch_on_count
9235 /* Instrument loop for insertion of bct instruction. We distinguish between
9236 loops with compile-time bounds and those with run-time bounds.
9237 Information from loop_iterations() is used to compute compile-time bounds.
9238 Run-time bounds should use loop preconditioning, but currently ignored.
9241 static void
9242 insert_bct (loop)
9243 struct loop *loop;
9245 unsigned HOST_WIDE_INT n_iterations;
9246 rtx loop_start = loop->start;
9247 rtx loop_end = loop->end;
9248 struct loop_info *loop_info = LOOP_INFO (loop);
9249 int loop_num = loop->num;
9251 #if 0
9252 int increment_direction, compare_direction;
9253 /* If the loop condition is <= or >=, the number of iteration
9254 is 1 more than the range of the bounds of the loop. */
9255 int add_iteration = 0;
9256 enum machine_mode loop_var_mode = word_mode;
9257 #endif
9259 /* It's impossible to instrument a competely unrolled loop. */
9260 if (loop_info->unroll_number == loop_info->n_iterations)
9261 return;
9263 /* Make sure that the count register is not in use. */
9264 if (loop_info->used_count_register)
9266 if (loop_dump_stream)
9267 fprintf (loop_dump_stream,
9268 "insert_bct %d: BCT instrumentation failed: count register already in use\n",
9269 loop_num);
9270 return;
9273 /* Make sure that the function has no indirect jumps. */
9274 if (indirect_jump_in_function)
9276 if (loop_dump_stream)
9277 fprintf (loop_dump_stream,
9278 "insert_bct %d: BCT instrumentation failed: indirect jump in function\n",
9279 loop_num);
9280 return;
9283 /* Make sure that the last loop insn is a conditional jump. */
9284 if (GET_CODE (PREV_INSN (loop_end)) != JUMP_INSN
9285 || ! onlyjump_p (PREV_INSN (loop_end))
9286 || ! any_condjump_p (PREV_INSN (loop_end)))
9288 if (loop_dump_stream)
9289 fprintf (loop_dump_stream,
9290 "insert_bct %d: BCT instrumentation failed: invalid jump at loop end\n",
9291 loop_num);
9292 return;
9295 /* Make sure that the loop does not contain a function call
9296 (the count register might be altered by the called function). */
9297 if (loop_info->has_call)
9299 if (loop_dump_stream)
9300 fprintf (loop_dump_stream,
9301 "insert_bct %d: BCT instrumentation failed: function call in loop\n",
9302 loop_num);
9303 return;
9306 /* Make sure that the loop does not jump via a table.
9307 (the count register might be used to perform the branch on table). */
9308 if (loop_info->has_tablejump)
9310 if (loop_dump_stream)
9311 fprintf (loop_dump_stream,
9312 "insert_bct %d: BCT instrumentation failed: computed branch in the loop\n",
9313 loop_num);
9314 return;
9317 /* Account for loop unrolling in instrumented iteration count. */
9318 if (loop_info->unroll_number > 1)
9319 n_iterations = loop_info->n_iterations / loop_info->unroll_number;
9320 else
9321 n_iterations = loop_info->n_iterations;
9323 if (n_iterations != 0 && n_iterations < 3)
9325 /* Allow an enclosing outer loop to benefit if possible. */
9326 if (loop_dump_stream)
9327 fprintf (loop_dump_stream,
9328 "insert_bct %d: Too few iterations to benefit from BCT optimization\n",
9329 loop_num);
9330 return;
9333 /* Try to instrument the loop. */
9335 /* Handle the simpler case, where the bounds are known at compile time. */
9336 if (n_iterations > 0)
9338 struct loop *outer_loop;
9339 struct loop_info *outer_loop_info;
9341 /* Mark all enclosing loops that they cannot use count register. */
9342 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
9344 outer_loop_info = LOOP_INFO (outer_loop);
9345 outer_loop_info->used_count_register = 1;
9347 instrument_loop_bct (loop_start, loop_end, GEN_INT (n_iterations));
9348 return;
9351 /* Handle the more complex case, that the bounds are NOT known
9352 at compile time. In this case we generate run_time calculation
9353 of the number of iterations. */
9355 if (loop_info->iteration_var == 0)
9357 if (loop_dump_stream)
9358 fprintf (loop_dump_stream,
9359 "insert_bct %d: BCT Runtime Instrumentation failed: no loop iteration variable found\n",
9360 loop_num);
9361 return;
9364 if (GET_MODE_CLASS (GET_MODE (loop_info->iteration_var)) != MODE_INT
9365 || GET_MODE_SIZE (GET_MODE (loop_info->iteration_var)) != UNITS_PER_WORD)
9367 if (loop_dump_stream)
9368 fprintf (loop_dump_stream,
9369 "insert_bct %d: BCT Runtime Instrumentation failed: loop variable not integer\n",
9370 loop_num);
9371 return;
9374 /* With runtime bounds, if the compare is of the form '!=' we give up */
9375 if (loop_info->comparison_code == NE)
9377 if (loop_dump_stream)
9378 fprintf (loop_dump_stream,
9379 "insert_bct %d: BCT Runtime Instrumentation failed: runtime bounds with != comparison\n",
9380 loop_num);
9381 return;
9383 /* Use common loop preconditioning code instead. */
9384 #if 0
9385 else
9387 /* We rely on the existence of run-time guard to ensure that the
9388 loop executes at least once. */
9389 rtx sequence;
9390 rtx iterations_num_reg;
9392 unsigned HOST_WIDE_INT increment_value_abs
9393 = INTVAL (increment) * increment_direction;
9395 /* make sure that the increment is a power of two, otherwise (an
9396 expensive) divide is needed. */
9397 if (exact_log2 (increment_value_abs) == -1)
9399 if (loop_dump_stream)
9400 fprintf (loop_dump_stream,
9401 "insert_bct: not instrumenting BCT because the increment is not power of 2\n");
9402 return;
9405 /* compute the number of iterations */
9406 start_sequence ();
9408 rtx temp_reg;
9410 /* Again, the number of iterations is calculated by:
9412 ; compare-val - initial-val + (increment -1) + additional-iteration
9413 ; num_iterations = -----------------------------------------------------------------
9414 ; increment
9416 /* ??? Do we have to call copy_rtx here before passing rtx to
9417 expand_binop? */
9418 if (compare_direction > 0)
9420 /* <, <= :the loop variable is increasing */
9421 temp_reg = expand_binop (loop_var_mode, sub_optab,
9422 comparison_value, initial_value,
9423 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9425 else
9427 temp_reg = expand_binop (loop_var_mode, sub_optab,
9428 initial_value, comparison_value,
9429 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9432 if (increment_value_abs - 1 + add_iteration != 0)
9433 temp_reg = expand_binop (loop_var_mode, add_optab, temp_reg,
9434 GEN_INT (increment_value_abs - 1
9435 + add_iteration),
9436 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9438 if (increment_value_abs != 1)
9439 iterations_num_reg = expand_binop (loop_var_mode, asr_optab,
9440 temp_reg,
9441 GEN_INT (exact_log2 (increment_value_abs)),
9442 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9443 else
9444 iterations_num_reg = temp_reg;
9446 sequence = gen_sequence ();
9447 end_sequence ();
9448 emit_insn_before (sequence, loop_start);
9449 instrument_loop_bct (loop_start, loop_end, iterations_num_reg);
9452 return;
9453 #endif /* Complex case */
9456 /* Instrument loop by inserting a bct in it as follows:
9457 1. A new counter register is created.
9458 2. In the head of the loop the new variable is initialized to the value
9459 passed in the loop_num_iterations parameter.
9460 3. At the end of the loop, comparison of the register with 0 is generated.
9461 The created comparison follows the pattern defined for the
9462 decrement_and_branch_on_count insn, so this insn will be generated.
9463 4. The branch on the old variable are deleted. The compare must remain
9464 because it might be used elsewhere. If the loop-variable or condition
9465 register are used elsewhere, they will be eliminated by flow. */
9467 static void
9468 instrument_loop_bct (loop_start, loop_end, loop_num_iterations)
9469 rtx loop_start, loop_end;
9470 rtx loop_num_iterations;
9472 rtx counter_reg;
9473 rtx start_label;
9474 rtx sequence;
9476 if (HAVE_decrement_and_branch_on_count)
9478 if (loop_dump_stream)
9480 fputs ("instrument_bct: Inserting BCT (", loop_dump_stream);
9481 if (GET_CODE (loop_num_iterations) == CONST_INT)
9482 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC,
9483 INTVAL (loop_num_iterations));
9484 else
9485 fputs ("runtime", loop_dump_stream);
9486 fputs (" iterations)", loop_dump_stream);
9489 /* Discard original jump to continue loop. Original compare result
9490 may still be live, so it cannot be discarded explicitly. */
9491 delete_insn (PREV_INSN (loop_end));
9493 /* Insert the label which will delimit the start of the loop. */
9494 start_label = gen_label_rtx ();
9495 emit_label_after (start_label, loop_start);
9497 /* Insert initialization of the count register into the loop header. */
9498 start_sequence ();
9499 counter_reg = gen_reg_rtx (word_mode);
9500 emit_insn (gen_move_insn (counter_reg, loop_num_iterations));
9501 sequence = gen_sequence ();
9502 end_sequence ();
9503 emit_insn_before (sequence, loop_start);
9505 /* Insert new comparison on the count register instead of the
9506 old one, generating the needed BCT pattern (that will be
9507 later recognized by assembly generation phase). */
9508 sequence = emit_jump_insn_before (
9509 gen_decrement_and_branch_on_count (counter_reg, start_label),
9510 loop_end);
9512 if (GET_CODE (sequence) != JUMP_INSN)
9513 abort ();
9514 JUMP_LABEL (sequence) = start_label;
9515 LABEL_NUSES (start_label)++;
9518 #endif /* HAVE_decrement_and_branch_on_count */
9520 /* Scan the function and determine whether it has indirect (computed) jumps.
9522 This is taken mostly from flow.c; similar code exists elsewhere
9523 in the compiler. It may be useful to put this into rtlanal.c. */
9524 static int
9525 indirect_jump_in_function_p (start)
9526 rtx start;
9528 rtx insn;
9530 for (insn = start; insn; insn = NEXT_INSN (insn))
9531 if (computed_jump_p (insn))
9532 return 1;
9534 return 0;
9537 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
9538 documentation for LOOP_MEMS for the definition of `appropriate'.
9539 This function is called from prescan_loop via for_each_rtx. */
9541 static int
9542 insert_loop_mem (mem, data)
9543 rtx *mem;
9544 void *data ATTRIBUTE_UNUSED;
9546 int i;
9547 rtx m = *mem;
9549 if (m == NULL_RTX)
9550 return 0;
9552 switch (GET_CODE (m))
9554 case MEM:
9555 break;
9557 case CLOBBER:
9558 /* We're not interested in MEMs that are only clobbered. */
9559 return -1;
9561 case CONST_DOUBLE:
9562 /* We're not interested in the MEM associated with a
9563 CONST_DOUBLE, so there's no need to traverse into this. */
9564 return -1;
9566 case EXPR_LIST:
9567 /* We're not interested in any MEMs that only appear in notes. */
9568 return -1;
9570 default:
9571 /* This is not a MEM. */
9572 return 0;
9575 /* See if we've already seen this MEM. */
9576 for (i = 0; i < loop_mems_idx; ++i)
9577 if (rtx_equal_p (m, loop_mems[i].mem))
9579 if (GET_MODE (m) != GET_MODE (loop_mems[i].mem))
9580 /* The modes of the two memory accesses are different. If
9581 this happens, something tricky is going on, and we just
9582 don't optimize accesses to this MEM. */
9583 loop_mems[i].optimize = 0;
9585 return 0;
9588 /* Resize the array, if necessary. */
9589 if (loop_mems_idx == loop_mems_allocated)
9591 if (loop_mems_allocated != 0)
9592 loop_mems_allocated *= 2;
9593 else
9594 loop_mems_allocated = 32;
9596 loop_mems = (loop_mem_info*)
9597 xrealloc (loop_mems,
9598 loop_mems_allocated * sizeof (loop_mem_info));
9601 /* Actually insert the MEM. */
9602 loop_mems[loop_mems_idx].mem = m;
9603 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9604 because we can't put it in a register. We still store it in the
9605 table, though, so that if we see the same address later, but in a
9606 non-BLK mode, we'll not think we can optimize it at that point. */
9607 loop_mems[loop_mems_idx].optimize = (GET_MODE (m) != BLKmode);
9608 loop_mems[loop_mems_idx].reg = NULL_RTX;
9609 ++loop_mems_idx;
9611 return 0;
9614 /* Like load_mems, but also ensures that SET_IN_LOOP,
9615 MAY_NOT_OPTIMIZE, REG_SINGLE_USAGE, and INSN_COUNT have the correct
9616 values after load_mems. */
9618 static void
9619 load_mems_and_recount_loop_regs_set (loop, insn_count)
9620 const struct loop *loop;
9621 int *insn_count;
9623 int nregs = max_reg_num ();
9625 load_mems (loop);
9627 /* Recalculate set_in_loop and friends since load_mems may have
9628 created new registers. */
9629 if (max_reg_num () > nregs)
9631 int i;
9632 int old_nregs;
9634 old_nregs = nregs;
9635 nregs = max_reg_num ();
9637 if ((unsigned) nregs > set_in_loop->num_elements)
9639 /* Grow all the arrays. */
9640 VARRAY_GROW (set_in_loop, nregs);
9641 VARRAY_GROW (n_times_set, nregs);
9642 VARRAY_GROW (may_not_optimize, nregs);
9643 VARRAY_GROW (reg_single_usage, nregs);
9645 /* Clear the arrays */
9646 bzero ((char *) &set_in_loop->data, nregs * sizeof (int));
9647 bzero ((char *) &may_not_optimize->data, nregs * sizeof (char));
9648 bzero ((char *) &reg_single_usage->data, nregs * sizeof (rtx));
9650 count_loop_regs_set (loop->top ? loop->top : loop->start, loop->end,
9651 may_not_optimize, reg_single_usage,
9652 insn_count, nregs);
9654 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9656 VARRAY_CHAR (may_not_optimize, i) = 1;
9657 VARRAY_INT (set_in_loop, i) = 1;
9660 #ifdef AVOID_CCMODE_COPIES
9661 /* Don't try to move insns which set CC registers if we should not
9662 create CCmode register copies. */
9663 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
9664 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
9665 VARRAY_CHAR (may_not_optimize, i) = 1;
9666 #endif
9668 /* Set n_times_set for the new registers. */
9669 bcopy ((char *) (&set_in_loop->data.i[0] + old_nregs),
9670 (char *) (&n_times_set->data.i[0] + old_nregs),
9671 (nregs - old_nregs) * sizeof (int));
9675 /* Move MEMs into registers for the duration of the loop. */
9677 static void
9678 load_mems (loop)
9679 const struct loop *loop;
9681 int maybe_never = 0;
9682 int i;
9683 rtx p;
9684 rtx label = NULL_RTX;
9685 rtx end_label = NULL_RTX;
9686 /* Nonzero if the next instruction may never be executed. */
9687 int next_maybe_never = 0;
9688 int last_max_reg = max_reg_num ();
9690 if (loop_mems_idx == 0)
9691 return;
9693 /* Find start of the extended basic block that enters the loop. */
9694 for (p = loop->start;
9695 PREV_INSN (p) && GET_CODE (p) != CODE_LABEL;
9696 p = PREV_INSN (p))
9699 cselib_init ();
9701 /* Build table of mems that get set to constant values before the
9702 loop. */
9703 for (; p != loop->start; p = NEXT_INSN (p))
9704 cselib_process_insn (p);
9706 /* Check to see if it's possible that some instructions in the
9707 loop are never executed. */
9708 for (p = next_insn_in_loop (loop, loop->scan_start);
9709 p != NULL_RTX && ! maybe_never;
9710 p = next_insn_in_loop (loop, p))
9712 if (GET_CODE (p) == CODE_LABEL)
9713 maybe_never = 1;
9714 else if (GET_CODE (p) == JUMP_INSN
9715 /* If we enter the loop in the middle, and scan
9716 around to the beginning, don't set maybe_never
9717 for that. This must be an unconditional jump,
9718 otherwise the code at the top of the loop might
9719 never be executed. Unconditional jumps are
9720 followed a by barrier then loop end. */
9721 && ! (GET_CODE (p) == JUMP_INSN
9722 && JUMP_LABEL (p) == loop->top
9723 && NEXT_INSN (NEXT_INSN (p)) == loop->end
9724 && any_uncondjump_p (p)))
9726 if (!any_condjump_p (p))
9727 /* Something complicated. */
9728 maybe_never = 1;
9729 else
9730 /* If there are any more instructions in the loop, they
9731 might not be reached. */
9732 next_maybe_never = 1;
9734 else if (next_maybe_never)
9735 maybe_never = 1;
9738 /* Actually move the MEMs. */
9739 for (i = 0; i < loop_mems_idx; ++i)
9741 regset_head copies;
9742 int written = 0;
9743 rtx reg;
9744 rtx mem = loop_mems[i].mem;
9745 rtx mem_list_entry;
9747 if (MEM_VOLATILE_P (mem)
9748 || loop_invariant_p (loop, XEXP (mem, 0)) != 1)
9749 /* There's no telling whether or not MEM is modified. */
9750 loop_mems[i].optimize = 0;
9752 /* Go through the MEMs written to in the loop to see if this
9753 one is aliased by one of them. */
9754 mem_list_entry = loop_store_mems;
9755 while (mem_list_entry)
9757 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
9758 written = 1;
9759 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
9760 mem, rtx_varies_p))
9762 /* MEM is indeed aliased by this store. */
9763 loop_mems[i].optimize = 0;
9764 break;
9766 mem_list_entry = XEXP (mem_list_entry, 1);
9769 if (flag_float_store && written
9770 && GET_MODE_CLASS (GET_MODE (mem)) == MODE_FLOAT)
9771 loop_mems[i].optimize = 0;
9773 /* If this MEM is written to, we must be sure that there
9774 are no reads from another MEM that aliases this one. */
9775 if (loop_mems[i].optimize && written)
9777 int j;
9779 for (j = 0; j < loop_mems_idx; ++j)
9781 if (j == i)
9782 continue;
9783 else if (true_dependence (mem,
9784 VOIDmode,
9785 loop_mems[j].mem,
9786 rtx_varies_p))
9788 /* It's not safe to hoist loop_mems[i] out of
9789 the loop because writes to it might not be
9790 seen by reads from loop_mems[j]. */
9791 loop_mems[i].optimize = 0;
9792 break;
9797 if (maybe_never && may_trap_p (mem))
9798 /* We can't access the MEM outside the loop; it might
9799 cause a trap that wouldn't have happened otherwise. */
9800 loop_mems[i].optimize = 0;
9802 if (!loop_mems[i].optimize)
9803 /* We thought we were going to lift this MEM out of the
9804 loop, but later discovered that we could not. */
9805 continue;
9807 INIT_REG_SET (&copies);
9809 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
9810 order to keep scan_loop from moving stores to this MEM
9811 out of the loop just because this REG is neither a
9812 user-variable nor used in the loop test. */
9813 reg = gen_reg_rtx (GET_MODE (mem));
9814 REG_USERVAR_P (reg) = 1;
9815 loop_mems[i].reg = reg;
9817 /* Now, replace all references to the MEM with the
9818 corresponding pesudos. */
9819 maybe_never = 0;
9820 for (p = next_insn_in_loop (loop, loop->scan_start);
9821 p != NULL_RTX;
9822 p = next_insn_in_loop (loop, p))
9824 rtx_and_int ri;
9825 rtx set;
9827 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
9829 /* See if this copies the mem into a register that isn't
9830 modified afterwards. We'll try to do copy propagation
9831 a little further on. */
9832 set = single_set (p);
9833 if (set
9834 /* @@@ This test is _way_ too conservative. */
9835 && ! maybe_never
9836 && GET_CODE (SET_DEST (set)) == REG
9837 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
9838 && REGNO (SET_DEST (set)) < last_max_reg
9839 && VARRAY_INT (n_times_set, REGNO (SET_DEST (set))) == 1
9840 && rtx_equal_p (SET_SRC (set), loop_mems[i].mem))
9841 SET_REGNO_REG_SET (&copies, REGNO (SET_DEST (set)));
9842 ri.r = p;
9843 ri.i = i;
9844 for_each_rtx (&p, replace_loop_mem, &ri);
9847 if (GET_CODE (p) == CODE_LABEL
9848 || GET_CODE (p) == JUMP_INSN)
9849 maybe_never = 1;
9852 if (! apply_change_group ())
9853 /* We couldn't replace all occurrences of the MEM. */
9854 loop_mems[i].optimize = 0;
9855 else
9857 /* Load the memory immediately before LOOP->START, which is
9858 the NOTE_LOOP_BEG. */
9859 cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
9860 rtx set;
9861 rtx best = mem;
9862 int j;
9863 struct elt_loc_list *const_equiv = 0;
9865 if (e)
9867 struct elt_loc_list *equiv;
9868 struct elt_loc_list *best_equiv = 0;
9869 for (equiv = e->locs; equiv; equiv = equiv->next)
9871 if (CONSTANT_P (equiv->loc))
9872 const_equiv = equiv;
9873 else if (GET_CODE (equiv->loc) == REG
9874 /* Extending hard register lifetimes cuases crash
9875 on SRC targets. Doing so on non-SRC is
9876 probably also not good idea, since we most
9877 probably have pseudoregister equivalence as
9878 well. */
9879 && REGNO (equiv->loc) >= FIRST_PSEUDO_REGISTER)
9880 best_equiv = equiv;
9882 /* Use the constant equivalence if that is cheap enough. */
9883 if (! best_equiv)
9884 best_equiv = const_equiv;
9885 else if (const_equiv
9886 && (rtx_cost (const_equiv->loc, SET)
9887 <= rtx_cost (best_equiv->loc, SET)))
9889 best_equiv = const_equiv;
9890 const_equiv = 0;
9893 /* If best_equiv is nonzero, we know that MEM is set to a
9894 constant or register before the loop. We will use this
9895 knowledge to initialize the shadow register with that
9896 constant or reg rather than by loading from MEM. */
9897 if (best_equiv)
9898 best = copy_rtx (best_equiv->loc);
9900 set = gen_move_insn (reg, best);
9901 set = emit_insn_before (set, loop->start);
9902 if (const_equiv)
9903 REG_NOTES (set) = gen_rtx_EXPR_LIST (REG_EQUAL,
9904 copy_rtx (const_equiv->loc),
9905 REG_NOTES (set));
9907 if (written)
9909 if (label == NULL_RTX)
9911 /* We must compute the former
9912 right-after-the-end label before we insert
9913 the new one. */
9914 end_label = next_label (loop->end);
9915 label = gen_label_rtx ();
9916 emit_label_after (label, loop->end);
9919 /* Store the memory immediately after END, which is
9920 the NOTE_LOOP_END. */
9921 set = gen_move_insn (copy_rtx (mem), reg);
9922 emit_insn_after (set, label);
9925 if (loop_dump_stream)
9927 fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
9928 REGNO (reg), (written ? "r/w" : "r/o"));
9929 print_rtl (loop_dump_stream, mem);
9930 fputc ('\n', loop_dump_stream);
9933 /* Attempt a bit of copy propagation. This helps untangle the
9934 data flow, and enables {basic,general}_induction_var to find
9935 more bivs/givs. */
9936 EXECUTE_IF_SET_IN_REG_SET
9937 (&copies, FIRST_PSEUDO_REGISTER, j,
9939 try_copy_prop (loop, loop_mems[i].reg, j);
9941 CLEAR_REG_SET (&copies);
9945 if (label != NULL_RTX)
9947 /* Now, we need to replace all references to the previous exit
9948 label with the new one. */
9949 rtx_pair rr;
9950 rr.r1 = end_label;
9951 rr.r2 = label;
9953 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
9955 for_each_rtx (&p, replace_label, &rr);
9957 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
9958 field. This is not handled by for_each_rtx because it doesn't
9959 handle unprinted ('0') fields. We need to update JUMP_LABEL
9960 because the immediately following unroll pass will use it.
9961 replace_label would not work anyways, because that only handles
9962 LABEL_REFs. */
9963 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == end_label)
9964 JUMP_LABEL (p) = label;
9968 cselib_finish ();
9971 /* For communication between note_reg_stored and its caller. */
9972 struct note_reg_stored_arg
9974 int set_seen;
9975 rtx reg;
9978 /* Called via note_stores, record in SET_SEEN whether X, which is written,
9979 is equal to ARG. */
9980 static void
9981 note_reg_stored (x, setter, arg)
9982 rtx x, setter ATTRIBUTE_UNUSED;
9983 void *arg;
9985 struct note_reg_stored_arg *t = (struct note_reg_stored_arg *)arg;
9986 if (t->reg == x)
9987 t->set_seen = 1;
9990 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
9991 There must be exactly one insn that sets this pseudo; it will be
9992 deleted if all replacements succeed and we can prove that the register
9993 is not used after the loop. */
9995 static void
9996 try_copy_prop (loop, replacement, regno)
9997 const struct loop *loop;
9998 rtx replacement;
9999 unsigned int regno;
10001 /* This is the reg that we are copying from. */
10002 rtx reg_rtx = regno_reg_rtx[regno];
10003 rtx init_insn = 0;
10004 rtx insn;
10005 /* These help keep track of whether we replaced all uses of the reg. */
10006 int replaced_last = 0;
10007 int store_is_first = 0;
10009 for (insn = next_insn_in_loop (loop, loop->scan_start);
10010 insn != NULL_RTX;
10011 insn = next_insn_in_loop (loop, insn))
10013 rtx set;
10015 /* Only substitute within one extended basic block from the initializing
10016 insn. */
10017 if (GET_CODE (insn) == CODE_LABEL && init_insn)
10018 break;
10020 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
10021 continue;
10023 /* Is this the initializing insn? */
10024 set = single_set (insn);
10025 if (set
10026 && GET_CODE (SET_DEST (set)) == REG
10027 && REGNO (SET_DEST (set)) == regno)
10029 if (init_insn)
10030 abort ();
10032 init_insn = insn;
10033 if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
10034 store_is_first = 1;
10037 /* Only substitute after seeing the initializing insn. */
10038 if (init_insn && insn != init_insn)
10040 struct note_reg_stored_arg arg;
10041 rtx array[3];
10042 array[0] = reg_rtx;
10043 array[1] = replacement;
10044 array[2] = insn;
10046 for_each_rtx (&insn, replace_loop_reg, array);
10047 if (REGNO_LAST_UID (regno) == INSN_UID (insn))
10048 replaced_last = 1;
10050 /* Stop replacing when REPLACEMENT is modified. */
10051 arg.reg = replacement;
10052 arg.set_seen = 0;
10053 note_stores (PATTERN (insn), note_reg_stored, &arg);
10054 if (arg.set_seen)
10055 break;
10058 if (! init_insn)
10059 abort ();
10060 if (apply_change_group ())
10062 if (loop_dump_stream)
10063 fprintf (loop_dump_stream, " Replaced reg %d", regno);
10064 if (store_is_first && replaced_last)
10066 PUT_CODE (init_insn, NOTE);
10067 NOTE_LINE_NUMBER (init_insn) = NOTE_INSN_DELETED;
10068 if (loop_dump_stream)
10069 fprintf (loop_dump_stream, ", deleting init_insn (%d)",
10070 INSN_UID (init_insn));
10072 if (loop_dump_stream)
10073 fprintf (loop_dump_stream, ".\n");
10077 /* Replace MEM with its associated pseudo register. This function is
10078 called from load_mems via for_each_rtx. DATA is actually an
10079 rtx_and_int * describing the instruction currently being scanned
10080 and the MEM we are currently replacing. */
10082 static int
10083 replace_loop_mem (mem, data)
10084 rtx *mem;
10085 void *data;
10087 rtx_and_int *ri;
10088 rtx insn;
10089 int i;
10090 rtx m = *mem;
10092 if (m == NULL_RTX)
10093 return 0;
10095 switch (GET_CODE (m))
10097 case MEM:
10098 break;
10100 case CONST_DOUBLE:
10101 /* We're not interested in the MEM associated with a
10102 CONST_DOUBLE, so there's no need to traverse into one. */
10103 return -1;
10105 default:
10106 /* This is not a MEM. */
10107 return 0;
10110 ri = (rtx_and_int*) data;
10111 i = ri->i;
10113 if (!rtx_equal_p (loop_mems[i].mem, m))
10114 /* This is not the MEM we are currently replacing. */
10115 return 0;
10117 insn = ri->r;
10119 /* Actually replace the MEM. */
10120 validate_change (insn, mem, loop_mems[i].reg, 1);
10122 return 0;
10125 /* Replace one register with another. Called through for_each_rtx; PX points
10126 to the rtx being scanned. DATA is actually an array of three rtx's; the
10127 first one is the one to be replaced, and the second one the replacement.
10128 The third one is the current insn. */
10130 static int
10131 replace_loop_reg (px, data)
10132 rtx *px;
10133 void *data;
10135 rtx x = *px;
10136 rtx *array = (rtx *)data;
10138 if (x == NULL_RTX)
10139 return 0;
10141 if (x == array[0])
10142 validate_change (array[2], px, array[1], 1);
10144 return 0;
10147 /* Replace occurrences of the old exit label for the loop with the new
10148 one. DATA is an rtx_pair containing the old and new labels,
10149 respectively. */
10151 static int
10152 replace_label (x, data)
10153 rtx *x;
10154 void *data;
10156 rtx l = *x;
10157 rtx old_label = ((rtx_pair*) data)->r1;
10158 rtx new_label = ((rtx_pair*) data)->r2;
10160 if (l == NULL_RTX)
10161 return 0;
10163 if (GET_CODE (l) != LABEL_REF)
10164 return 0;
10166 if (XEXP (l, 0) != old_label)
10167 return 0;
10169 XEXP (l, 0) = new_label;
10170 ++LABEL_NUSES (new_label);
10171 --LABEL_NUSES (old_label);
10173 return 0;