Pass 9th fp argument correctly on System V/eabi; Add @plt for -fPIC/-mrelocatable
[official-gcc.git] / gcc / loop.c
blobe43d58518d167aae9648ea46cb1b0e49d540915a
1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 88, 89, 91-6, 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This is the loop optimization pass of the compiler.
23 It finds invariant computations within loops and moves them
24 to the beginning of the loop. Then it identifies basic and
25 general induction variables. Strength reduction is applied to the general
26 induction variables, and induction variable elimination is applied to
27 the basic induction variables.
29 It also finds cases where
30 a register is set within the loop by zero-extending a narrower value
31 and changes these to zero the entire register once before the loop
32 and merely copy the low part within the loop.
34 Most of the complexity is in heuristics to decide when it is worth
35 while to do these things. */
37 #include "config.h"
38 #include <stdio.h>
39 #include "rtl.h"
40 #include "obstack.h"
41 #include "expr.h"
42 #include "insn-config.h"
43 #include "insn-flags.h"
44 #include "regs.h"
45 #include "hard-reg-set.h"
46 #include "recog.h"
47 #include "flags.h"
48 #include "real.h"
49 #include "loop.h"
50 #include "except.h"
52 /* Vector mapping INSN_UIDs to luids.
53 The luids are like uids but increase monotonically always.
54 We use them to see whether a jump comes from outside a given loop. */
56 int *uid_luid;
58 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
59 number the insn is contained in. */
61 int *uid_loop_num;
63 /* 1 + largest uid of any insn. */
65 int max_uid_for_loop;
67 /* 1 + luid of last insn. */
69 static int max_luid;
71 /* Number of loops detected in current function. Used as index to the
72 next few tables. */
74 static int max_loop_num;
76 /* Indexed by loop number, contains the first and last insn of each loop. */
78 static rtx *loop_number_loop_starts, *loop_number_loop_ends;
80 /* For each loop, gives the containing loop number, -1 if none. */
82 int *loop_outer_loop;
84 #ifdef HAIFA
85 /* The main output of analyze_loop_iterations is placed here */
87 int *loop_can_insert_bct;
89 /* For each loop, determines whether some of its inner loops has used
90 count register */
92 int *loop_used_count_register;
94 /* loop parameters for arithmetic loops. These loops have a loop variable
95 which is initialized to loop_start_value, incremented in each iteration
96 by "loop_increment". At the end of the iteration the loop variable is
97 compared to the loop_comparison_value (using loop_comparison_code). */
99 rtx *loop_increment;
100 rtx *loop_comparison_value;
101 rtx *loop_start_value;
102 enum rtx_code *loop_comparison_code;
103 #endif /* HAIFA */
105 /* For each loop, keep track of its unrolling factor.
106 Potential values:
107 0: unrolled
108 1: not unrolled.
109 -1: completely unrolled
110 >0: holds the unroll exact factor. */
111 int *loop_unroll_factor;
113 /* Indexed by loop number, contains a nonzero value if the "loop" isn't
114 really a loop (an insn outside the loop branches into it). */
116 static char *loop_invalid;
118 /* Indexed by loop number, links together all LABEL_REFs which refer to
119 code labels outside the loop. Used by routines that need to know all
120 loop exits, such as final_biv_value and final_giv_value.
122 This does not include loop exits due to return instructions. This is
123 because all bivs and givs are pseudos, and hence must be dead after a
124 return, so the presense of a return does not affect any of the
125 optimizations that use this info. It is simpler to just not include return
126 instructions on this list. */
128 rtx *loop_number_exit_labels;
130 /* Indexed by loop number, counts the number of LABEL_REFs on
131 loop_number_exit_labels for this loop and all loops nested inside it. */
133 int *loop_number_exit_count;
135 /* Holds the number of loop iterations. It is zero if the number could not be
136 calculated. Must be unsigned since the number of iterations can
137 be as high as 2^wordsize-1. For loops with a wider iterator, this number
138 will will be zero if the number of loop iterations is too large for an
139 unsigned integer to hold. */
141 unsigned HOST_WIDE_INT loop_n_iterations;
143 /* Nonzero if there is a subroutine call in the current loop. */
145 static int loop_has_call;
147 /* Nonzero if there is a volatile memory reference in the current
148 loop. */
150 static int loop_has_volatile;
152 /* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
153 current loop. A continue statement will generate a branch to
154 NEXT_INSN (loop_continue). */
156 static rtx loop_continue;
158 /* Indexed by register number, contains the number of times the reg
159 is set during the loop being scanned.
160 During code motion, a negative value indicates a reg that has been
161 made a candidate; in particular -2 means that it is an candidate that
162 we know is equal to a constant and -1 means that it is an candidate
163 not known equal to a constant.
164 After code motion, regs moved have 0 (which is accurate now)
165 while the failed candidates have the original number of times set.
167 Therefore, at all times, == 0 indicates an invariant register;
168 < 0 a conditionally invariant one. */
170 static int *n_times_set;
172 /* Original value of n_times_set; same except that this value
173 is not set negative for a reg whose sets have been made candidates
174 and not set to 0 for a reg that is moved. */
176 static int *n_times_used;
178 /* Index by register number, 1 indicates that the register
179 cannot be moved or strength reduced. */
181 static char *may_not_optimize;
183 /* Nonzero means reg N has already been moved out of one loop.
184 This reduces the desire to move it out of another. */
186 static char *moved_once;
188 /* Array of MEMs that are stored in this loop. If there are too many to fit
189 here, we just turn on unknown_address_altered. */
191 #define NUM_STORES 30
192 static rtx loop_store_mems[NUM_STORES];
194 /* Index of first available slot in above array. */
195 static int loop_store_mems_idx;
197 /* Nonzero if we don't know what MEMs were changed in the current loop.
198 This happens if the loop contains a call (in which case `loop_has_call'
199 will also be set) or if we store into more than NUM_STORES MEMs. */
201 static int unknown_address_altered;
203 /* Count of movable (i.e. invariant) instructions discovered in the loop. */
204 static int num_movables;
206 /* Count of memory write instructions discovered in the loop. */
207 static int num_mem_sets;
209 /* Number of loops contained within the current one, including itself. */
210 static int loops_enclosed;
212 /* Bound on pseudo register number before loop optimization.
213 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
214 int max_reg_before_loop;
216 /* This obstack is used in product_cheap_p to allocate its rtl. It
217 may call gen_reg_rtx which, in turn, may reallocate regno_reg_rtx.
218 If we used the same obstack that it did, we would be deallocating
219 that array. */
221 static struct obstack temp_obstack;
223 /* This is where the pointer to the obstack being used for RTL is stored. */
225 extern struct obstack *rtl_obstack;
227 #define obstack_chunk_alloc xmalloc
228 #define obstack_chunk_free free
230 extern char *oballoc ();
232 /* During the analysis of a loop, a chain of `struct movable's
233 is made to record all the movable insns found.
234 Then the entire chain can be scanned to decide which to move. */
236 struct movable
238 rtx insn; /* A movable insn */
239 rtx set_src; /* The expression this reg is set from. */
240 rtx set_dest; /* The destination of this SET. */
241 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
242 of any registers used within the LIBCALL. */
243 int consec; /* Number of consecutive following insns
244 that must be moved with this one. */
245 int regno; /* The register it sets */
246 short lifetime; /* lifetime of that register;
247 may be adjusted when matching movables
248 that load the same value are found. */
249 short savings; /* Number of insns we can move for this reg,
250 including other movables that force this
251 or match this one. */
252 unsigned int cond : 1; /* 1 if only conditionally movable */
253 unsigned int force : 1; /* 1 means MUST move this insn */
254 unsigned int global : 1; /* 1 means reg is live outside this loop */
255 /* If PARTIAL is 1, GLOBAL means something different:
256 that the reg is live outside the range from where it is set
257 to the following label. */
258 unsigned int done : 1; /* 1 inhibits further processing of this */
260 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
261 In particular, moving it does not make it
262 invariant. */
263 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
264 load SRC, rather than copying INSN. */
265 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
266 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
267 that we should avoid changing when clearing
268 the rest of the reg. */
269 struct movable *match; /* First entry for same value */
270 struct movable *forces; /* An insn that must be moved if this is */
271 struct movable *next;
274 FILE *loop_dump_stream;
276 /* Forward declarations. */
278 static void find_and_verify_loops ();
279 static void mark_loop_jump ();
280 static void prescan_loop ();
281 static int reg_in_basic_block_p ();
282 static int consec_sets_invariant_p ();
283 static rtx libcall_other_reg ();
284 static int labels_in_range_p ();
285 static void count_loop_regs_set ();
286 static void note_addr_stored ();
287 static int loop_reg_used_before_p ();
288 static void scan_loop ();
289 #if 0
290 static void replace_call_address ();
291 #endif
292 static rtx skip_consec_insns ();
293 static int libcall_benefit ();
294 static void ignore_some_movables ();
295 static void force_movables ();
296 static void combine_movables ();
297 static int rtx_equal_for_loop_p ();
298 static void move_movables ();
299 static void strength_reduce ();
300 static int valid_initial_value_p ();
301 static void find_mem_givs ();
302 static void record_biv ();
303 static void check_final_value ();
304 static void record_giv ();
305 static void update_giv_derive ();
306 static int basic_induction_var ();
307 static rtx simplify_giv_expr ();
308 static int general_induction_var ();
309 static int consec_sets_giv ();
310 static int check_dbra_loop ();
311 static rtx express_from ();
312 static int combine_givs_p ();
313 static void combine_givs ();
314 static int product_cheap_p ();
315 static int maybe_eliminate_biv ();
316 static int maybe_eliminate_biv_1 ();
317 static int last_use_this_basic_block ();
318 static void record_initial ();
319 static void update_reg_last_use ();
321 #ifdef HAIFA
322 /* This is extern from unroll.c */
323 void iteration_info ();
325 /* Two main functions for implementing bct:
326 first - to be called before loop unrolling, and the second - after */
327 static void analyze_loop_iterations ();
328 static void insert_bct ();
330 /* Auxiliary function that inserts the bct pattern into the loop */
331 static void instrument_loop_bct ();
332 #endif /* HAIFA */
334 /* Indirect_jump_in_function is computed once per function. */
335 int indirect_jump_in_function = 0;
336 static int indirect_jump_in_function_p ();
339 /* Relative gain of eliminating various kinds of operations. */
340 int add_cost;
341 #if 0
342 int shift_cost;
343 int mult_cost;
344 #endif
346 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
347 copy the value of the strength reduced giv to its original register. */
348 int copy_cost;
350 void
351 init_loop ()
353 char *free_point = (char *) oballoc (1);
354 rtx reg = gen_rtx (REG, word_mode, LAST_VIRTUAL_REGISTER + 1);
356 add_cost = rtx_cost (gen_rtx (PLUS, word_mode, reg, reg), SET);
358 /* We multiply by 2 to reconcile the difference in scale between
359 these two ways of computing costs. Otherwise the cost of a copy
360 will be far less than the cost of an add. */
362 copy_cost = 2 * 2;
364 /* Free the objects we just allocated. */
365 obfree (free_point);
367 /* Initialize the obstack used for rtl in product_cheap_p. */
368 gcc_obstack_init (&temp_obstack);
371 /* Entry point of this file. Perform loop optimization
372 on the current function. F is the first insn of the function
373 and DUMPFILE is a stream for output of a trace of actions taken
374 (or 0 if none should be output). */
376 void
377 loop_optimize (f, dumpfile)
378 /* f is the first instruction of a chain of insns for one function */
379 rtx f;
380 FILE *dumpfile;
382 register rtx insn;
383 register int i;
384 rtx last_insn;
386 loop_dump_stream = dumpfile;
388 init_recog_no_volatile ();
389 init_alias_analysis ();
391 max_reg_before_loop = max_reg_num ();
393 moved_once = (char *) alloca (max_reg_before_loop);
394 bzero (moved_once, max_reg_before_loop);
396 regs_may_share = 0;
398 /* Count the number of loops. */
400 max_loop_num = 0;
401 for (insn = f; insn; insn = NEXT_INSN (insn))
403 if (GET_CODE (insn) == NOTE
404 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
405 max_loop_num++;
408 /* Don't waste time if no loops. */
409 if (max_loop_num == 0)
410 return;
412 /* Get size to use for tables indexed by uids.
413 Leave some space for labels allocated by find_and_verify_loops. */
414 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
416 uid_luid = (int *) alloca (max_uid_for_loop * sizeof (int));
417 uid_loop_num = (int *) alloca (max_uid_for_loop * sizeof (int));
419 bzero ((char *) uid_luid, max_uid_for_loop * sizeof (int));
420 bzero ((char *) uid_loop_num, max_uid_for_loop * sizeof (int));
422 /* Allocate tables for recording each loop. We set each entry, so they need
423 not be zeroed. */
424 loop_number_loop_starts = (rtx *) alloca (max_loop_num * sizeof (rtx));
425 loop_number_loop_ends = (rtx *) alloca (max_loop_num * sizeof (rtx));
426 loop_outer_loop = (int *) alloca (max_loop_num * sizeof (int));
427 loop_invalid = (char *) alloca (max_loop_num * sizeof (char));
428 loop_number_exit_labels = (rtx *) alloca (max_loop_num * sizeof (rtx));
429 loop_number_exit_count = (int *) alloca (max_loop_num * sizeof (int));
431 /* This is initialized by the unrolling code, so we go ahead
432 and clear them just in case we are not performing loop
433 unrolling. */
434 loop_unroll_factor = (int *) alloca (max_loop_num *sizeof (int));
435 bzero ((char *) loop_unroll_factor, max_loop_num * sizeof (int));
437 #ifdef HAIFA
438 /* Allocate for BCT optimization */
439 loop_can_insert_bct = (int *) alloca (max_loop_num * sizeof (int));
440 bzero ((char *) loop_can_insert_bct, max_loop_num * sizeof (int));
442 loop_used_count_register = (int *) alloca (max_loop_num * sizeof (int));
443 bzero ((char *) loop_used_count_register, max_loop_num * sizeof (int));
445 loop_increment = (rtx *) alloca (max_loop_num * sizeof (rtx));
446 loop_comparison_value = (rtx *) alloca (max_loop_num * sizeof (rtx));
447 loop_start_value = (rtx *) alloca (max_loop_num * sizeof (rtx));
448 bzero ((char *) loop_increment, max_loop_num * sizeof (rtx));
449 bzero ((char *) loop_comparison_value, max_loop_num * sizeof (rtx));
450 bzero ((char *) loop_start_value, max_loop_num * sizeof (rtx));
452 loop_comparison_code
453 = (enum rtx_code *) alloca (max_loop_num * sizeof (enum rtx_code));
454 bzero ((char *) loop_comparison_code, max_loop_num * sizeof (enum rtx_code));
455 #endif /* HAIFA */
457 /* Find and process each loop.
458 First, find them, and record them in order of their beginnings. */
459 find_and_verify_loops (f);
461 /* Now find all register lifetimes. This must be done after
462 find_and_verify_loops, because it might reorder the insns in the
463 function. */
464 reg_scan (f, max_reg_num (), 1);
466 /* See if we went too far. */
467 if (get_max_uid () > max_uid_for_loop)
468 abort ();
470 /* Compute the mapping from uids to luids.
471 LUIDs are numbers assigned to insns, like uids,
472 except that luids increase monotonically through the code.
473 Don't assign luids to line-number NOTEs, so that the distance in luids
474 between two insns is not affected by -g. */
476 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
478 last_insn = insn;
479 if (GET_CODE (insn) != NOTE
480 || NOTE_LINE_NUMBER (insn) <= 0)
481 uid_luid[INSN_UID (insn)] = ++i;
482 else
483 /* Give a line number note the same luid as preceding insn. */
484 uid_luid[INSN_UID (insn)] = i;
487 max_luid = i + 1;
489 /* Don't leave gaps in uid_luid for insns that have been
490 deleted. It is possible that the first or last insn
491 using some register has been deleted by cross-jumping.
492 Make sure that uid_luid for that former insn's uid
493 points to the general area where that insn used to be. */
494 for (i = 0; i < max_uid_for_loop; i++)
496 uid_luid[0] = uid_luid[i];
497 if (uid_luid[0] != 0)
498 break;
500 for (i = 0; i < max_uid_for_loop; i++)
501 if (uid_luid[i] == 0)
502 uid_luid[i] = uid_luid[i - 1];
504 /* Create a mapping from loops to BLOCK tree nodes. */
505 if (flag_unroll_loops && write_symbols != NO_DEBUG)
506 find_loop_tree_blocks ();
508 /* Determine if the function has indirect jump. On some systems
509 this prevents low overhead loop instructions from being used. */
510 indirect_jump_in_function = indirect_jump_in_function_p (f);
512 /* Now scan the loops, last ones first, since this means inner ones are done
513 before outer ones. */
514 for (i = max_loop_num-1; i >= 0; i--)
515 if (! loop_invalid[i] && loop_number_loop_ends[i])
516 scan_loop (loop_number_loop_starts[i], loop_number_loop_ends[i],
517 max_reg_num ());
519 /* If debugging and unrolling loops, we must replicate the tree nodes
520 corresponding to the blocks inside the loop, so that the original one
521 to one mapping will remain. */
522 if (flag_unroll_loops && write_symbols != NO_DEBUG)
523 unroll_block_trees ();
526 /* Optimize one loop whose start is LOOP_START and end is END.
527 LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
528 NOTE_INSN_LOOP_END. */
530 /* ??? Could also move memory writes out of loops if the destination address
531 is invariant, the source is invariant, the memory write is not volatile,
532 and if we can prove that no read inside the loop can read this address
533 before the write occurs. If there is a read of this address after the
534 write, then we can also mark the memory read as invariant. */
536 static void
537 scan_loop (loop_start, end, nregs)
538 rtx loop_start, end;
539 int nregs;
541 register int i;
542 register rtx p;
543 /* 1 if we are scanning insns that could be executed zero times. */
544 int maybe_never = 0;
545 /* 1 if we are scanning insns that might never be executed
546 due to a subroutine call which might exit before they are reached. */
547 int call_passed = 0;
548 /* For a rotated loop that is entered near the bottom,
549 this is the label at the top. Otherwise it is zero. */
550 rtx loop_top = 0;
551 /* Jump insn that enters the loop, or 0 if control drops in. */
552 rtx loop_entry_jump = 0;
553 /* Place in the loop where control enters. */
554 rtx scan_start;
555 /* Number of insns in the loop. */
556 int insn_count;
557 int in_libcall = 0;
558 int tem;
559 rtx temp;
560 /* The SET from an insn, if it is the only SET in the insn. */
561 rtx set, set1;
562 /* Chain describing insns movable in current loop. */
563 struct movable *movables = 0;
564 /* Last element in `movables' -- so we can add elements at the end. */
565 struct movable *last_movable = 0;
566 /* Ratio of extra register life span we can justify
567 for saving an instruction. More if loop doesn't call subroutines
568 since in that case saving an insn makes more difference
569 and more registers are available. */
570 int threshold;
571 /* If we have calls, contains the insn in which a register was used
572 if it was used exactly once; contains const0_rtx if it was used more
573 than once. */
574 rtx *reg_single_usage = 0;
575 /* Nonzero if we are scanning instructions in a sub-loop. */
576 int loop_depth = 0;
578 n_times_set = (int *) alloca (nregs * sizeof (int));
579 n_times_used = (int *) alloca (nregs * sizeof (int));
580 may_not_optimize = (char *) alloca (nregs);
582 /* Determine whether this loop starts with a jump down to a test at
583 the end. This will occur for a small number of loops with a test
584 that is too complex to duplicate in front of the loop.
586 We search for the first insn or label in the loop, skipping NOTEs.
587 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
588 (because we might have a loop executed only once that contains a
589 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
590 (in case we have a degenerate loop).
592 Note that if we mistakenly think that a loop is entered at the top
593 when, in fact, it is entered at the exit test, the only effect will be
594 slightly poorer optimization. Making the opposite error can generate
595 incorrect code. Since very few loops now start with a jump to the
596 exit test, the code here to detect that case is very conservative. */
598 for (p = NEXT_INSN (loop_start);
599 p != end
600 && GET_CODE (p) != CODE_LABEL && GET_RTX_CLASS (GET_CODE (p)) != 'i'
601 && (GET_CODE (p) != NOTE
602 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
603 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
604 p = NEXT_INSN (p))
607 scan_start = p;
609 /* Set up variables describing this loop. */
610 prescan_loop (loop_start, end);
611 threshold = (loop_has_call ? 1 : 2) * (1 + n_non_fixed_regs);
613 /* If loop has a jump before the first label,
614 the true entry is the target of that jump.
615 Start scan from there.
616 But record in LOOP_TOP the place where the end-test jumps
617 back to so we can scan that after the end of the loop. */
618 if (GET_CODE (p) == JUMP_INSN)
620 loop_entry_jump = p;
622 /* Loop entry must be unconditional jump (and not a RETURN) */
623 if (simplejump_p (p)
624 && JUMP_LABEL (p) != 0
625 /* Check to see whether the jump actually
626 jumps out of the loop (meaning it's no loop).
627 This case can happen for things like
628 do {..} while (0). If this label was generated previously
629 by loop, we can't tell anything about it and have to reject
630 the loop. */
631 && INSN_UID (JUMP_LABEL (p)) < max_uid_for_loop
632 && INSN_LUID (JUMP_LABEL (p)) >= INSN_LUID (loop_start)
633 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (end))
635 loop_top = next_label (scan_start);
636 scan_start = JUMP_LABEL (p);
640 /* If SCAN_START was an insn created by loop, we don't know its luid
641 as required by loop_reg_used_before_p. So skip such loops. (This
642 test may never be true, but it's best to play it safe.)
644 Also, skip loops where we do not start scanning at a label. This
645 test also rejects loops starting with a JUMP_INSN that failed the
646 test above. */
648 if (INSN_UID (scan_start) >= max_uid_for_loop
649 || GET_CODE (scan_start) != CODE_LABEL)
651 if (loop_dump_stream)
652 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
653 INSN_UID (loop_start), INSN_UID (end));
654 return;
657 /* Count number of times each reg is set during this loop.
658 Set may_not_optimize[I] if it is not safe to move out
659 the setting of register I. If this loop has calls, set
660 reg_single_usage[I]. */
662 bzero ((char *) n_times_set, nregs * sizeof (int));
663 bzero (may_not_optimize, nregs);
665 if (loop_has_call)
667 reg_single_usage = (rtx *) alloca (nregs * sizeof (rtx));
668 bzero ((char *) reg_single_usage, nregs * sizeof (rtx));
671 count_loop_regs_set (loop_top ? loop_top : loop_start, end,
672 may_not_optimize, reg_single_usage, &insn_count, nregs);
674 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
675 may_not_optimize[i] = 1, n_times_set[i] = 1;
676 bcopy ((char *) n_times_set, (char *) n_times_used, nregs * sizeof (int));
678 if (loop_dump_stream)
680 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
681 INSN_UID (loop_start), INSN_UID (end), insn_count);
682 if (loop_continue)
683 fprintf (loop_dump_stream, "Continue at insn %d.\n",
684 INSN_UID (loop_continue));
687 /* Scan through the loop finding insns that are safe to move.
688 Set n_times_set negative for the reg being set, so that
689 this reg will be considered invariant for subsequent insns.
690 We consider whether subsequent insns use the reg
691 in deciding whether it is worth actually moving.
693 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
694 and therefore it is possible that the insns we are scanning
695 would never be executed. At such times, we must make sure
696 that it is safe to execute the insn once instead of zero times.
697 When MAYBE_NEVER is 0, all insns will be executed at least once
698 so that is not a problem. */
700 p = scan_start;
701 while (1)
703 p = NEXT_INSN (p);
704 /* At end of a straight-in loop, we are done.
705 At end of a loop entered at the bottom, scan the top. */
706 if (p == scan_start)
707 break;
708 if (p == end)
710 if (loop_top != 0)
711 p = loop_top;
712 else
713 break;
714 if (p == scan_start)
715 break;
718 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
719 && find_reg_note (p, REG_LIBCALL, NULL_RTX))
720 in_libcall = 1;
721 else if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
722 && find_reg_note (p, REG_RETVAL, NULL_RTX))
723 in_libcall = 0;
725 if (GET_CODE (p) == INSN
726 && (set = single_set (p))
727 && GET_CODE (SET_DEST (set)) == REG
728 && ! may_not_optimize[REGNO (SET_DEST (set))])
730 int tem1 = 0;
731 int tem2 = 0;
732 int move_insn = 0;
733 rtx src = SET_SRC (set);
734 rtx dependencies = 0;
736 /* Figure out what to use as a source of this insn. If a REG_EQUIV
737 note is given or if a REG_EQUAL note with a constant operand is
738 specified, use it as the source and mark that we should move
739 this insn by calling emit_move_insn rather that duplicating the
740 insn.
742 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
743 is present. */
744 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
745 if (temp)
746 src = XEXP (temp, 0), move_insn = 1;
747 else
749 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
750 if (temp && CONSTANT_P (XEXP (temp, 0)))
751 src = XEXP (temp, 0), move_insn = 1;
752 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
754 src = XEXP (temp, 0);
755 /* A libcall block can use regs that don't appear in
756 the equivalent expression. To move the libcall,
757 we must move those regs too. */
758 dependencies = libcall_other_reg (p, src);
762 /* Don't try to optimize a register that was made
763 by loop-optimization for an inner loop.
764 We don't know its life-span, so we can't compute the benefit. */
765 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
767 /* In order to move a register, we need to have one of three cases:
768 (1) it is used only in the same basic block as the set
769 (2) it is not a user variable and it is not used in the
770 exit test (this can cause the variable to be used
771 before it is set just like a user-variable).
772 (3) the set is guaranteed to be executed once the loop starts,
773 and the reg is not used until after that. */
774 else if (! ((! maybe_never
775 && ! loop_reg_used_before_p (set, p, loop_start,
776 scan_start, end))
777 || (! REG_USERVAR_P (SET_DEST (set))
778 && ! REG_LOOP_TEST_P (SET_DEST (set)))
779 || reg_in_basic_block_p (p, SET_DEST (set))))
781 else if ((tem = invariant_p (src))
782 && (dependencies == 0
783 || (tem2 = invariant_p (dependencies)) != 0)
784 && (n_times_set[REGNO (SET_DEST (set))] == 1
785 || (tem1
786 = consec_sets_invariant_p (SET_DEST (set),
787 n_times_set[REGNO (SET_DEST (set))],
788 p)))
789 /* If the insn can cause a trap (such as divide by zero),
790 can't move it unless it's guaranteed to be executed
791 once loop is entered. Even a function call might
792 prevent the trap insn from being reached
793 (since it might exit!) */
794 && ! ((maybe_never || call_passed)
795 && may_trap_p (src)))
797 register struct movable *m;
798 register int regno = REGNO (SET_DEST (set));
800 /* A potential lossage is where we have a case where two insns
801 can be combined as long as they are both in the loop, but
802 we move one of them outside the loop. For large loops,
803 this can lose. The most common case of this is the address
804 of a function being called.
806 Therefore, if this register is marked as being used exactly
807 once if we are in a loop with calls (a "large loop"), see if
808 we can replace the usage of this register with the source
809 of this SET. If we can, delete this insn.
811 Don't do this if P has a REG_RETVAL note or if we have
812 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
814 if (reg_single_usage && reg_single_usage[regno] != 0
815 && reg_single_usage[regno] != const0_rtx
816 && REGNO_FIRST_UID (regno) == INSN_UID (p)
817 && (REGNO_LAST_UID (regno)
818 == INSN_UID (reg_single_usage[regno]))
819 && n_times_set[REGNO (SET_DEST (set))] == 1
820 && ! side_effects_p (SET_SRC (set))
821 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
822 && (! SMALL_REGISTER_CLASSES
823 || (! (GET_CODE (SET_SRC (set)) == REG
824 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
825 /* This test is not redundant; SET_SRC (set) might be
826 a call-clobbered register and the life of REGNO
827 might span a call. */
828 && ! modified_between_p (SET_SRC (set), p,
829 reg_single_usage[regno])
830 && no_labels_between_p (p, reg_single_usage[regno])
831 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
832 reg_single_usage[regno]))
834 /* Replace any usage in a REG_EQUAL note. Must copy the
835 new source, so that we don't get rtx sharing between the
836 SET_SOURCE and REG_NOTES of insn p. */
837 REG_NOTES (reg_single_usage[regno])
838 = replace_rtx (REG_NOTES (reg_single_usage[regno]),
839 SET_DEST (set), copy_rtx (SET_SRC (set)));
841 PUT_CODE (p, NOTE);
842 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
843 NOTE_SOURCE_FILE (p) = 0;
844 n_times_set[regno] = 0;
845 continue;
848 m = (struct movable *) alloca (sizeof (struct movable));
849 m->next = 0;
850 m->insn = p;
851 m->set_src = src;
852 m->dependencies = dependencies;
853 m->set_dest = SET_DEST (set);
854 m->force = 0;
855 m->consec = n_times_set[REGNO (SET_DEST (set))] - 1;
856 m->done = 0;
857 m->forces = 0;
858 m->partial = 0;
859 m->move_insn = move_insn;
860 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
861 m->savemode = VOIDmode;
862 m->regno = regno;
863 /* Set M->cond if either invariant_p or consec_sets_invariant_p
864 returned 2 (only conditionally invariant). */
865 m->cond = ((tem | tem1 | tem2) > 1);
866 m->global = (uid_luid[REGNO_LAST_UID (regno)] > INSN_LUID (end)
867 || uid_luid[REGNO_FIRST_UID (regno)] < INSN_LUID (loop_start));
868 m->match = 0;
869 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
870 - uid_luid[REGNO_FIRST_UID (regno)]);
871 m->savings = n_times_used[regno];
872 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
873 m->savings += libcall_benefit (p);
874 n_times_set[regno] = move_insn ? -2 : -1;
875 /* Add M to the end of the chain MOVABLES. */
876 if (movables == 0)
877 movables = m;
878 else
879 last_movable->next = m;
880 last_movable = m;
882 if (m->consec > 0)
884 /* Skip this insn, not checking REG_LIBCALL notes. */
885 p = next_nonnote_insn (p);
886 /* Skip the consecutive insns, if there are any. */
887 p = skip_consec_insns (p, m->consec);
888 /* Back up to the last insn of the consecutive group. */
889 p = prev_nonnote_insn (p);
891 /* We must now reset m->move_insn, m->is_equiv, and possibly
892 m->set_src to correspond to the effects of all the
893 insns. */
894 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
895 if (temp)
896 m->set_src = XEXP (temp, 0), m->move_insn = 1;
897 else
899 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
900 if (temp && CONSTANT_P (XEXP (temp, 0)))
901 m->set_src = XEXP (temp, 0), m->move_insn = 1;
902 else
903 m->move_insn = 0;
906 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
909 /* If this register is always set within a STRICT_LOW_PART
910 or set to zero, then its high bytes are constant.
911 So clear them outside the loop and within the loop
912 just load the low bytes.
913 We must check that the machine has an instruction to do so.
914 Also, if the value loaded into the register
915 depends on the same register, this cannot be done. */
916 else if (SET_SRC (set) == const0_rtx
917 && GET_CODE (NEXT_INSN (p)) == INSN
918 && (set1 = single_set (NEXT_INSN (p)))
919 && GET_CODE (set1) == SET
920 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
921 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
922 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
923 == SET_DEST (set))
924 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
926 register int regno = REGNO (SET_DEST (set));
927 if (n_times_set[regno] == 2)
929 register struct movable *m;
930 m = (struct movable *) alloca (sizeof (struct movable));
931 m->next = 0;
932 m->insn = p;
933 m->set_dest = SET_DEST (set);
934 m->dependencies = 0;
935 m->force = 0;
936 m->consec = 0;
937 m->done = 0;
938 m->forces = 0;
939 m->move_insn = 0;
940 m->partial = 1;
941 /* If the insn may not be executed on some cycles,
942 we can't clear the whole reg; clear just high part.
943 Not even if the reg is used only within this loop.
944 Consider this:
945 while (1)
946 while (s != t) {
947 if (foo ()) x = *s;
948 use (x);
950 Clearing x before the inner loop could clobber a value
951 being saved from the last time around the outer loop.
952 However, if the reg is not used outside this loop
953 and all uses of the register are in the same
954 basic block as the store, there is no problem.
956 If this insn was made by loop, we don't know its
957 INSN_LUID and hence must make a conservative
958 assumption. */
959 m->global = (INSN_UID (p) >= max_uid_for_loop
960 || (uid_luid[REGNO_LAST_UID (regno)]
961 > INSN_LUID (end))
962 || (uid_luid[REGNO_FIRST_UID (regno)]
963 < INSN_LUID (p))
964 || (labels_in_range_p
965 (p, uid_luid[REGNO_FIRST_UID (regno)])));
966 if (maybe_never && m->global)
967 m->savemode = GET_MODE (SET_SRC (set1));
968 else
969 m->savemode = VOIDmode;
970 m->regno = regno;
971 m->cond = 0;
972 m->match = 0;
973 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
974 - uid_luid[REGNO_FIRST_UID (regno)]);
975 m->savings = 1;
976 n_times_set[regno] = -1;
977 /* Add M to the end of the chain MOVABLES. */
978 if (movables == 0)
979 movables = m;
980 else
981 last_movable->next = m;
982 last_movable = m;
986 /* Past a call insn, we get to insns which might not be executed
987 because the call might exit. This matters for insns that trap.
988 Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
989 so they don't count. */
990 else if (GET_CODE (p) == CALL_INSN && ! in_libcall)
991 call_passed = 1;
992 /* Past a label or a jump, we get to insns for which we
993 can't count on whether or how many times they will be
994 executed during each iteration. Therefore, we can
995 only move out sets of trivial variables
996 (those not used after the loop). */
997 /* Similar code appears twice in strength_reduce. */
998 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
999 /* If we enter the loop in the middle, and scan around to the
1000 beginning, don't set maybe_never for that. This must be an
1001 unconditional jump, otherwise the code at the top of the
1002 loop might never be executed. Unconditional jumps are
1003 followed a by barrier then loop end. */
1004 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop_top
1005 && NEXT_INSN (NEXT_INSN (p)) == end
1006 && simplejump_p (p)))
1007 maybe_never = 1;
1008 else if (GET_CODE (p) == NOTE)
1010 /* At the virtual top of a converted loop, insns are again known to
1011 be executed: logically, the loop begins here even though the exit
1012 code has been duplicated. */
1013 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1014 maybe_never = call_passed = 0;
1015 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1016 loop_depth++;
1017 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1018 loop_depth--;
1022 /* If one movable subsumes another, ignore that other. */
1024 ignore_some_movables (movables);
1026 /* For each movable insn, see if the reg that it loads
1027 leads when it dies right into another conditionally movable insn.
1028 If so, record that the second insn "forces" the first one,
1029 since the second can be moved only if the first is. */
1031 force_movables (movables);
1033 /* See if there are multiple movable insns that load the same value.
1034 If there are, make all but the first point at the first one
1035 through the `match' field, and add the priorities of them
1036 all together as the priority of the first. */
1038 combine_movables (movables, nregs);
1040 /* Now consider each movable insn to decide whether it is worth moving.
1041 Store 0 in n_times_set for each reg that is moved. */
1043 move_movables (movables, threshold,
1044 insn_count, loop_start, end, nregs);
1046 /* Now candidates that still are negative are those not moved.
1047 Change n_times_set to indicate that those are not actually invariant. */
1048 for (i = 0; i < nregs; i++)
1049 if (n_times_set[i] < 0)
1050 n_times_set[i] = n_times_used[i];
1052 if (flag_strength_reduce)
1053 strength_reduce (scan_start, end, loop_top,
1054 insn_count, loop_start, end);
1057 /* Add elements to *OUTPUT to record all the pseudo-regs
1058 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1060 void
1061 record_excess_regs (in_this, not_in_this, output)
1062 rtx in_this, not_in_this;
1063 rtx *output;
1065 enum rtx_code code;
1066 char *fmt;
1067 int i;
1069 code = GET_CODE (in_this);
1071 switch (code)
1073 case PC:
1074 case CC0:
1075 case CONST_INT:
1076 case CONST_DOUBLE:
1077 case CONST:
1078 case SYMBOL_REF:
1079 case LABEL_REF:
1080 return;
1082 case REG:
1083 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1084 && ! reg_mentioned_p (in_this, not_in_this))
1085 *output = gen_rtx (EXPR_LIST, VOIDmode, in_this, *output);
1086 return;
1088 default:
1089 break;
1092 fmt = GET_RTX_FORMAT (code);
1093 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1095 int j;
1097 switch (fmt[i])
1099 case 'E':
1100 for (j = 0; j < XVECLEN (in_this, i); j++)
1101 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1102 break;
1104 case 'e':
1105 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1106 break;
1111 /* Check what regs are referred to in the libcall block ending with INSN,
1112 aside from those mentioned in the equivalent value.
1113 If there are none, return 0.
1114 If there are one or more, return an EXPR_LIST containing all of them. */
1116 static rtx
1117 libcall_other_reg (insn, equiv)
1118 rtx insn, equiv;
1120 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1121 rtx p = XEXP (note, 0);
1122 rtx output = 0;
1124 /* First, find all the regs used in the libcall block
1125 that are not mentioned as inputs to the result. */
1127 while (p != insn)
1129 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1130 || GET_CODE (p) == CALL_INSN)
1131 record_excess_regs (PATTERN (p), equiv, &output);
1132 p = NEXT_INSN (p);
1135 return output;
1138 /* Return 1 if all uses of REG
1139 are between INSN and the end of the basic block. */
1141 static int
1142 reg_in_basic_block_p (insn, reg)
1143 rtx insn, reg;
1145 int regno = REGNO (reg);
1146 rtx p;
1148 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1149 return 0;
1151 /* Search this basic block for the already recorded last use of the reg. */
1152 for (p = insn; p; p = NEXT_INSN (p))
1154 switch (GET_CODE (p))
1156 case NOTE:
1157 break;
1159 case INSN:
1160 case CALL_INSN:
1161 /* Ordinary insn: if this is the last use, we win. */
1162 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1163 return 1;
1164 break;
1166 case JUMP_INSN:
1167 /* Jump insn: if this is the last use, we win. */
1168 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1169 return 1;
1170 /* Otherwise, it's the end of the basic block, so we lose. */
1171 return 0;
1173 case CODE_LABEL:
1174 case BARRIER:
1175 /* It's the end of the basic block, so we lose. */
1176 return 0;
1178 default:
1179 break;
1183 /* The "last use" doesn't follow the "first use"?? */
1184 abort ();
1187 /* Compute the benefit of eliminating the insns in the block whose
1188 last insn is LAST. This may be a group of insns used to compute a
1189 value directly or can contain a library call. */
1191 static int
1192 libcall_benefit (last)
1193 rtx last;
1195 rtx insn;
1196 int benefit = 0;
1198 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1199 insn != last; insn = NEXT_INSN (insn))
1201 if (GET_CODE (insn) == CALL_INSN)
1202 benefit += 10; /* Assume at least this many insns in a library
1203 routine. */
1204 else if (GET_CODE (insn) == INSN
1205 && GET_CODE (PATTERN (insn)) != USE
1206 && GET_CODE (PATTERN (insn)) != CLOBBER)
1207 benefit++;
1210 return benefit;
1213 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1215 static rtx
1216 skip_consec_insns (insn, count)
1217 rtx insn;
1218 int count;
1220 for (; count > 0; count--)
1222 rtx temp;
1224 /* If first insn of libcall sequence, skip to end. */
1225 /* Do this at start of loop, since INSN is guaranteed to
1226 be an insn here. */
1227 if (GET_CODE (insn) != NOTE
1228 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1229 insn = XEXP (temp, 0);
1231 do insn = NEXT_INSN (insn);
1232 while (GET_CODE (insn) == NOTE);
1235 return insn;
1238 /* Ignore any movable whose insn falls within a libcall
1239 which is part of another movable.
1240 We make use of the fact that the movable for the libcall value
1241 was made later and so appears later on the chain. */
1243 static void
1244 ignore_some_movables (movables)
1245 struct movable *movables;
1247 register struct movable *m, *m1;
1249 for (m = movables; m; m = m->next)
1251 /* Is this a movable for the value of a libcall? */
1252 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1253 if (note)
1255 rtx insn;
1256 /* Check for earlier movables inside that range,
1257 and mark them invalid. We cannot use LUIDs here because
1258 insns created by loop.c for prior loops don't have LUIDs.
1259 Rather than reject all such insns from movables, we just
1260 explicitly check each insn in the libcall (since invariant
1261 libcalls aren't that common). */
1262 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1263 for (m1 = movables; m1 != m; m1 = m1->next)
1264 if (m1->insn == insn)
1265 m1->done = 1;
1270 /* For each movable insn, see if the reg that it loads
1271 leads when it dies right into another conditionally movable insn.
1272 If so, record that the second insn "forces" the first one,
1273 since the second can be moved only if the first is. */
1275 static void
1276 force_movables (movables)
1277 struct movable *movables;
1279 register struct movable *m, *m1;
1280 for (m1 = movables; m1; m1 = m1->next)
1281 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1282 if (!m1->partial && !m1->done)
1284 int regno = m1->regno;
1285 for (m = m1->next; m; m = m->next)
1286 /* ??? Could this be a bug? What if CSE caused the
1287 register of M1 to be used after this insn?
1288 Since CSE does not update regno_last_uid,
1289 this insn M->insn might not be where it dies.
1290 But very likely this doesn't matter; what matters is
1291 that M's reg is computed from M1's reg. */
1292 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1293 && !m->done)
1294 break;
1295 if (m != 0 && m->set_src == m1->set_dest
1296 /* If m->consec, m->set_src isn't valid. */
1297 && m->consec == 0)
1298 m = 0;
1300 /* Increase the priority of the moving the first insn
1301 since it permits the second to be moved as well. */
1302 if (m != 0)
1304 m->forces = m1;
1305 m1->lifetime += m->lifetime;
1306 m1->savings += m1->savings;
1311 /* Find invariant expressions that are equal and can be combined into
1312 one register. */
1314 static void
1315 combine_movables (movables, nregs)
1316 struct movable *movables;
1317 int nregs;
1319 register struct movable *m;
1320 char *matched_regs = (char *) alloca (nregs);
1321 enum machine_mode mode;
1323 /* Regs that are set more than once are not allowed to match
1324 or be matched. I'm no longer sure why not. */
1325 /* Perhaps testing m->consec_sets would be more appropriate here? */
1327 for (m = movables; m; m = m->next)
1328 if (m->match == 0 && n_times_used[m->regno] == 1 && !m->partial)
1330 register struct movable *m1;
1331 int regno = m->regno;
1333 bzero (matched_regs, nregs);
1334 matched_regs[regno] = 1;
1336 /* We want later insns to match the first one. Don't make the first
1337 one match any later ones. So start this loop at m->next. */
1338 for (m1 = m->next; m1; m1 = m1->next)
1339 if (m != m1 && m1->match == 0 && n_times_used[m1->regno] == 1
1340 /* A reg used outside the loop mustn't be eliminated. */
1341 && !m1->global
1342 /* A reg used for zero-extending mustn't be eliminated. */
1343 && !m1->partial
1344 && (matched_regs[m1->regno]
1347 /* Can combine regs with different modes loaded from the
1348 same constant only if the modes are the same or
1349 if both are integer modes with M wider or the same
1350 width as M1. The check for integer is redundant, but
1351 safe, since the only case of differing destination
1352 modes with equal sources is when both sources are
1353 VOIDmode, i.e., CONST_INT. */
1354 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1355 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1356 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1357 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1358 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1359 /* See if the source of M1 says it matches M. */
1360 && ((GET_CODE (m1->set_src) == REG
1361 && matched_regs[REGNO (m1->set_src)])
1362 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1363 movables))))
1364 && ((m->dependencies == m1->dependencies)
1365 || rtx_equal_p (m->dependencies, m1->dependencies)))
1367 m->lifetime += m1->lifetime;
1368 m->savings += m1->savings;
1369 m1->done = 1;
1370 m1->match = m;
1371 matched_regs[m1->regno] = 1;
1375 /* Now combine the regs used for zero-extension.
1376 This can be done for those not marked `global'
1377 provided their lives don't overlap. */
1379 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1380 mode = GET_MODE_WIDER_MODE (mode))
1382 register struct movable *m0 = 0;
1384 /* Combine all the registers for extension from mode MODE.
1385 Don't combine any that are used outside this loop. */
1386 for (m = movables; m; m = m->next)
1387 if (m->partial && ! m->global
1388 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1390 register struct movable *m1;
1391 int first = uid_luid[REGNO_FIRST_UID (m->regno)];
1392 int last = uid_luid[REGNO_LAST_UID (m->regno)];
1394 if (m0 == 0)
1396 /* First one: don't check for overlap, just record it. */
1397 m0 = m;
1398 continue;
1401 /* Make sure they extend to the same mode.
1402 (Almost always true.) */
1403 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1404 continue;
1406 /* We already have one: check for overlap with those
1407 already combined together. */
1408 for (m1 = movables; m1 != m; m1 = m1->next)
1409 if (m1 == m0 || (m1->partial && m1->match == m0))
1410 if (! (uid_luid[REGNO_FIRST_UID (m1->regno)] > last
1411 || uid_luid[REGNO_LAST_UID (m1->regno)] < first))
1412 goto overlap;
1414 /* No overlap: we can combine this with the others. */
1415 m0->lifetime += m->lifetime;
1416 m0->savings += m->savings;
1417 m->done = 1;
1418 m->match = m0;
1420 overlap: ;
1425 /* Return 1 if regs X and Y will become the same if moved. */
1427 static int
1428 regs_match_p (x, y, movables)
1429 rtx x, y;
1430 struct movable *movables;
1432 int xn = REGNO (x);
1433 int yn = REGNO (y);
1434 struct movable *mx, *my;
1436 for (mx = movables; mx; mx = mx->next)
1437 if (mx->regno == xn)
1438 break;
1440 for (my = movables; my; my = my->next)
1441 if (my->regno == yn)
1442 break;
1444 return (mx && my
1445 && ((mx->match == my->match && mx->match != 0)
1446 || mx->match == my
1447 || mx == my->match));
1450 /* Return 1 if X and Y are identical-looking rtx's.
1451 This is the Lisp function EQUAL for rtx arguments.
1453 If two registers are matching movables or a movable register and an
1454 equivalent constant, consider them equal. */
1456 static int
1457 rtx_equal_for_loop_p (x, y, movables)
1458 rtx x, y;
1459 struct movable *movables;
1461 register int i;
1462 register int j;
1463 register struct movable *m;
1464 register enum rtx_code code;
1465 register char *fmt;
1467 if (x == y)
1468 return 1;
1469 if (x == 0 || y == 0)
1470 return 0;
1472 code = GET_CODE (x);
1474 /* If we have a register and a constant, they may sometimes be
1475 equal. */
1476 if (GET_CODE (x) == REG && n_times_set[REGNO (x)] == -2
1477 && CONSTANT_P (y))
1478 for (m = movables; m; m = m->next)
1479 if (m->move_insn && m->regno == REGNO (x)
1480 && rtx_equal_p (m->set_src, y))
1481 return 1;
1483 else if (GET_CODE (y) == REG && n_times_set[REGNO (y)] == -2
1484 && CONSTANT_P (x))
1485 for (m = movables; m; m = m->next)
1486 if (m->move_insn && m->regno == REGNO (y)
1487 && rtx_equal_p (m->set_src, x))
1488 return 1;
1490 /* Otherwise, rtx's of different codes cannot be equal. */
1491 if (code != GET_CODE (y))
1492 return 0;
1494 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1495 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1497 if (GET_MODE (x) != GET_MODE (y))
1498 return 0;
1500 /* These three types of rtx's can be compared nonrecursively. */
1501 if (code == REG)
1502 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1504 if (code == LABEL_REF)
1505 return XEXP (x, 0) == XEXP (y, 0);
1506 if (code == SYMBOL_REF)
1507 return XSTR (x, 0) == XSTR (y, 0);
1509 /* Compare the elements. If any pair of corresponding elements
1510 fail to match, return 0 for the whole things. */
1512 fmt = GET_RTX_FORMAT (code);
1513 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1515 switch (fmt[i])
1517 case 'w':
1518 if (XWINT (x, i) != XWINT (y, i))
1519 return 0;
1520 break;
1522 case 'i':
1523 if (XINT (x, i) != XINT (y, i))
1524 return 0;
1525 break;
1527 case 'E':
1528 /* Two vectors must have the same length. */
1529 if (XVECLEN (x, i) != XVECLEN (y, i))
1530 return 0;
1532 /* And the corresponding elements must match. */
1533 for (j = 0; j < XVECLEN (x, i); j++)
1534 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
1535 return 0;
1536 break;
1538 case 'e':
1539 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
1540 return 0;
1541 break;
1543 case 's':
1544 if (strcmp (XSTR (x, i), XSTR (y, i)))
1545 return 0;
1546 break;
1548 case 'u':
1549 /* These are just backpointers, so they don't matter. */
1550 break;
1552 case '0':
1553 break;
1555 /* It is believed that rtx's at this level will never
1556 contain anything but integers and other rtx's,
1557 except for within LABEL_REFs and SYMBOL_REFs. */
1558 default:
1559 abort ();
1562 return 1;
1565 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1566 insns in INSNS which use thet reference. */
1568 static void
1569 add_label_notes (x, insns)
1570 rtx x;
1571 rtx insns;
1573 enum rtx_code code = GET_CODE (x);
1574 int i, j;
1575 char *fmt;
1576 rtx insn;
1578 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1580 rtx next = next_real_insn (XEXP (x, 0));
1582 /* Don't record labels that refer to dispatch tables.
1583 This is not necessary, since the tablejump references the same label.
1584 And if we did record them, flow.c would make worse code. */
1585 if (next == 0
1586 || ! (GET_CODE (next) == JUMP_INSN
1587 && (GET_CODE (PATTERN (next)) == ADDR_VEC
1588 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
1590 for (insn = insns; insn; insn = NEXT_INSN (insn))
1591 if (reg_mentioned_p (XEXP (x, 0), insn))
1592 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, XEXP (x, 0),
1593 REG_NOTES (insn));
1595 return;
1598 fmt = GET_RTX_FORMAT (code);
1599 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1601 if (fmt[i] == 'e')
1602 add_label_notes (XEXP (x, i), insns);
1603 else if (fmt[i] == 'E')
1604 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1605 add_label_notes (XVECEXP (x, i, j), insns);
1609 /* Scan MOVABLES, and move the insns that deserve to be moved.
1610 If two matching movables are combined, replace one reg with the
1611 other throughout. */
1613 static void
1614 move_movables (movables, threshold, insn_count, loop_start, end, nregs)
1615 struct movable *movables;
1616 int threshold;
1617 int insn_count;
1618 rtx loop_start;
1619 rtx end;
1620 int nregs;
1622 rtx new_start = 0;
1623 register struct movable *m;
1624 register rtx p;
1625 /* Map of pseudo-register replacements to handle combining
1626 when we move several insns that load the same value
1627 into different pseudo-registers. */
1628 rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
1629 char *already_moved = (char *) alloca (nregs);
1631 bzero (already_moved, nregs);
1632 bzero ((char *) reg_map, nregs * sizeof (rtx));
1634 num_movables = 0;
1636 for (m = movables; m; m = m->next)
1638 /* Describe this movable insn. */
1640 if (loop_dump_stream)
1642 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1643 INSN_UID (m->insn), m->regno, m->lifetime);
1644 if (m->consec > 0)
1645 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1646 if (m->cond)
1647 fprintf (loop_dump_stream, "cond ");
1648 if (m->force)
1649 fprintf (loop_dump_stream, "force ");
1650 if (m->global)
1651 fprintf (loop_dump_stream, "global ");
1652 if (m->done)
1653 fprintf (loop_dump_stream, "done ");
1654 if (m->move_insn)
1655 fprintf (loop_dump_stream, "move-insn ");
1656 if (m->match)
1657 fprintf (loop_dump_stream, "matches %d ",
1658 INSN_UID (m->match->insn));
1659 if (m->forces)
1660 fprintf (loop_dump_stream, "forces %d ",
1661 INSN_UID (m->forces->insn));
1664 /* Count movables. Value used in heuristics in strength_reduce. */
1665 num_movables++;
1667 /* Ignore the insn if it's already done (it matched something else).
1668 Otherwise, see if it is now safe to move. */
1670 if (!m->done
1671 && (! m->cond
1672 || (1 == invariant_p (m->set_src)
1673 && (m->dependencies == 0
1674 || 1 == invariant_p (m->dependencies))
1675 && (m->consec == 0
1676 || 1 == consec_sets_invariant_p (m->set_dest,
1677 m->consec + 1,
1678 m->insn))))
1679 && (! m->forces || m->forces->done))
1681 register int regno;
1682 register rtx p;
1683 int savings = m->savings;
1685 /* We have an insn that is safe to move.
1686 Compute its desirability. */
1688 p = m->insn;
1689 regno = m->regno;
1691 if (loop_dump_stream)
1692 fprintf (loop_dump_stream, "savings %d ", savings);
1694 if (moved_once[regno])
1696 insn_count *= 2;
1698 if (loop_dump_stream)
1699 fprintf (loop_dump_stream, "halved since already moved ");
1702 /* An insn MUST be moved if we already moved something else
1703 which is safe only if this one is moved too: that is,
1704 if already_moved[REGNO] is nonzero. */
1706 /* An insn is desirable to move if the new lifetime of the
1707 register is no more than THRESHOLD times the old lifetime.
1708 If it's not desirable, it means the loop is so big
1709 that moving won't speed things up much,
1710 and it is liable to make register usage worse. */
1712 /* It is also desirable to move if it can be moved at no
1713 extra cost because something else was already moved. */
1715 if (already_moved[regno]
1716 || flag_move_all_movables
1717 || (threshold * savings * m->lifetime) >= insn_count
1718 || (m->forces && m->forces->done
1719 && n_times_used[m->forces->regno] == 1))
1721 int count;
1722 register struct movable *m1;
1723 rtx first;
1725 /* Now move the insns that set the reg. */
1727 if (m->partial && m->match)
1729 rtx newpat, i1;
1730 rtx r1, r2;
1731 /* Find the end of this chain of matching regs.
1732 Thus, we load each reg in the chain from that one reg.
1733 And that reg is loaded with 0 directly,
1734 since it has ->match == 0. */
1735 for (m1 = m; m1->match; m1 = m1->match);
1736 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1737 SET_DEST (PATTERN (m1->insn)));
1738 i1 = emit_insn_before (newpat, loop_start);
1740 /* Mark the moved, invariant reg as being allowed to
1741 share a hard reg with the other matching invariant. */
1742 REG_NOTES (i1) = REG_NOTES (m->insn);
1743 r1 = SET_DEST (PATTERN (m->insn));
1744 r2 = SET_DEST (PATTERN (m1->insn));
1745 regs_may_share = gen_rtx (EXPR_LIST, VOIDmode, r1,
1746 gen_rtx (EXPR_LIST, VOIDmode, r2,
1747 regs_may_share));
1748 delete_insn (m->insn);
1750 if (new_start == 0)
1751 new_start = i1;
1753 if (loop_dump_stream)
1754 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1756 /* If we are to re-generate the item being moved with a
1757 new move insn, first delete what we have and then emit
1758 the move insn before the loop. */
1759 else if (m->move_insn)
1761 rtx i1, temp;
1763 for (count = m->consec; count >= 0; count--)
1765 /* If this is the first insn of a library call sequence,
1766 skip to the end. */
1767 if (GET_CODE (p) != NOTE
1768 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1769 p = XEXP (temp, 0);
1771 /* If this is the last insn of a libcall sequence, then
1772 delete every insn in the sequence except the last.
1773 The last insn is handled in the normal manner. */
1774 if (GET_CODE (p) != NOTE
1775 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1777 temp = XEXP (temp, 0);
1778 while (temp != p)
1779 temp = delete_insn (temp);
1782 p = delete_insn (p);
1783 while (p && GET_CODE (p) == NOTE)
1784 p = NEXT_INSN (p);
1787 start_sequence ();
1788 emit_move_insn (m->set_dest, m->set_src);
1789 temp = get_insns ();
1790 end_sequence ();
1792 add_label_notes (m->set_src, temp);
1794 i1 = emit_insns_before (temp, loop_start);
1795 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1796 REG_NOTES (i1)
1797 = gen_rtx (EXPR_LIST,
1798 m->is_equiv ? REG_EQUIV : REG_EQUAL,
1799 m->set_src, REG_NOTES (i1));
1801 if (loop_dump_stream)
1802 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1804 /* The more regs we move, the less we like moving them. */
1805 threshold -= 3;
1807 else
1809 for (count = m->consec; count >= 0; count--)
1811 rtx i1, temp;
1813 /* If first insn of libcall sequence, skip to end. */
1814 /* Do this at start of loop, since p is guaranteed to
1815 be an insn here. */
1816 if (GET_CODE (p) != NOTE
1817 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1818 p = XEXP (temp, 0);
1820 /* If last insn of libcall sequence, move all
1821 insns except the last before the loop. The last
1822 insn is handled in the normal manner. */
1823 if (GET_CODE (p) != NOTE
1824 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1826 rtx fn_address = 0;
1827 rtx fn_reg = 0;
1828 rtx fn_address_insn = 0;
1830 first = 0;
1831 for (temp = XEXP (temp, 0); temp != p;
1832 temp = NEXT_INSN (temp))
1834 rtx body;
1835 rtx n;
1836 rtx next;
1838 if (GET_CODE (temp) == NOTE)
1839 continue;
1841 body = PATTERN (temp);
1843 /* Find the next insn after TEMP,
1844 not counting USE or NOTE insns. */
1845 for (next = NEXT_INSN (temp); next != p;
1846 next = NEXT_INSN (next))
1847 if (! (GET_CODE (next) == INSN
1848 && GET_CODE (PATTERN (next)) == USE)
1849 && GET_CODE (next) != NOTE)
1850 break;
1852 /* If that is the call, this may be the insn
1853 that loads the function address.
1855 Extract the function address from the insn
1856 that loads it into a register.
1857 If this insn was cse'd, we get incorrect code.
1859 So emit a new move insn that copies the
1860 function address into the register that the
1861 call insn will use. flow.c will delete any
1862 redundant stores that we have created. */
1863 if (GET_CODE (next) == CALL_INSN
1864 && GET_CODE (body) == SET
1865 && GET_CODE (SET_DEST (body)) == REG
1866 && (n = find_reg_note (temp, REG_EQUAL,
1867 NULL_RTX)))
1869 fn_reg = SET_SRC (body);
1870 if (GET_CODE (fn_reg) != REG)
1871 fn_reg = SET_DEST (body);
1872 fn_address = XEXP (n, 0);
1873 fn_address_insn = temp;
1875 /* We have the call insn.
1876 If it uses the register we suspect it might,
1877 load it with the correct address directly. */
1878 if (GET_CODE (temp) == CALL_INSN
1879 && fn_address != 0
1880 && reg_referenced_p (fn_reg, body))
1881 emit_insn_after (gen_move_insn (fn_reg,
1882 fn_address),
1883 fn_address_insn);
1885 if (GET_CODE (temp) == CALL_INSN)
1887 i1 = emit_call_insn_before (body, loop_start);
1888 /* Because the USAGE information potentially
1889 contains objects other than hard registers
1890 we need to copy it. */
1891 if (CALL_INSN_FUNCTION_USAGE (temp))
1892 CALL_INSN_FUNCTION_USAGE (i1)
1893 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
1895 else
1896 i1 = emit_insn_before (body, loop_start);
1897 if (first == 0)
1898 first = i1;
1899 if (temp == fn_address_insn)
1900 fn_address_insn = i1;
1901 REG_NOTES (i1) = REG_NOTES (temp);
1902 delete_insn (temp);
1905 if (m->savemode != VOIDmode)
1907 /* P sets REG to zero; but we should clear only
1908 the bits that are not covered by the mode
1909 m->savemode. */
1910 rtx reg = m->set_dest;
1911 rtx sequence;
1912 rtx tem;
1914 start_sequence ();
1915 tem = expand_binop
1916 (GET_MODE (reg), and_optab, reg,
1917 GEN_INT ((((HOST_WIDE_INT) 1
1918 << GET_MODE_BITSIZE (m->savemode)))
1919 - 1),
1920 reg, 1, OPTAB_LIB_WIDEN);
1921 if (tem == 0)
1922 abort ();
1923 if (tem != reg)
1924 emit_move_insn (reg, tem);
1925 sequence = gen_sequence ();
1926 end_sequence ();
1927 i1 = emit_insn_before (sequence, loop_start);
1929 else if (GET_CODE (p) == CALL_INSN)
1931 i1 = emit_call_insn_before (PATTERN (p), loop_start);
1932 /* Because the USAGE information potentially
1933 contains objects other than hard registers
1934 we need to copy it. */
1935 if (CALL_INSN_FUNCTION_USAGE (p))
1936 CALL_INSN_FUNCTION_USAGE (i1)
1937 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
1939 else
1940 i1 = emit_insn_before (PATTERN (p), loop_start);
1942 REG_NOTES (i1) = REG_NOTES (p);
1944 /* If there is a REG_EQUAL note present whose value is
1945 not loop invariant, then delete it, since it may
1946 cause problems with later optimization passes.
1947 It is possible for cse to create such notes
1948 like this as a result of record_jump_cond. */
1950 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
1951 && ! invariant_p (XEXP (temp, 0)))
1952 remove_note (i1, temp);
1954 if (new_start == 0)
1955 new_start = i1;
1957 if (loop_dump_stream)
1958 fprintf (loop_dump_stream, " moved to %d",
1959 INSN_UID (i1));
1961 #if 0
1962 /* This isn't needed because REG_NOTES is copied
1963 below and is wrong since P might be a PARALLEL. */
1964 if (REG_NOTES (i1) == 0
1965 && ! m->partial /* But not if it's a zero-extend clr. */
1966 && ! m->global /* and not if used outside the loop
1967 (since it might get set outside). */
1968 && CONSTANT_P (SET_SRC (PATTERN (p))))
1969 REG_NOTES (i1)
1970 = gen_rtx (EXPR_LIST, REG_EQUAL,
1971 SET_SRC (PATTERN (p)), REG_NOTES (i1));
1972 #endif
1974 /* If library call, now fix the REG_NOTES that contain
1975 insn pointers, namely REG_LIBCALL on FIRST
1976 and REG_RETVAL on I1. */
1977 if (temp = find_reg_note (i1, REG_RETVAL, NULL_RTX))
1979 XEXP (temp, 0) = first;
1980 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
1981 XEXP (temp, 0) = i1;
1984 delete_insn (p);
1985 do p = NEXT_INSN (p);
1986 while (p && GET_CODE (p) == NOTE);
1989 /* The more regs we move, the less we like moving them. */
1990 threshold -= 3;
1993 /* Any other movable that loads the same register
1994 MUST be moved. */
1995 already_moved[regno] = 1;
1997 /* This reg has been moved out of one loop. */
1998 moved_once[regno] = 1;
2000 /* The reg set here is now invariant. */
2001 if (! m->partial)
2002 n_times_set[regno] = 0;
2004 m->done = 1;
2006 /* Change the length-of-life info for the register
2007 to say it lives at least the full length of this loop.
2008 This will help guide optimizations in outer loops. */
2010 if (uid_luid[REGNO_FIRST_UID (regno)] > INSN_LUID (loop_start))
2011 /* This is the old insn before all the moved insns.
2012 We can't use the moved insn because it is out of range
2013 in uid_luid. Only the old insns have luids. */
2014 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2015 if (uid_luid[REGNO_LAST_UID (regno)] < INSN_LUID (end))
2016 REGNO_LAST_UID (regno) = INSN_UID (end);
2018 /* Combine with this moved insn any other matching movables. */
2020 if (! m->partial)
2021 for (m1 = movables; m1; m1 = m1->next)
2022 if (m1->match == m)
2024 rtx temp;
2026 /* Schedule the reg loaded by M1
2027 for replacement so that shares the reg of M.
2028 If the modes differ (only possible in restricted
2029 circumstances, make a SUBREG. */
2030 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2031 reg_map[m1->regno] = m->set_dest;
2032 else
2033 reg_map[m1->regno]
2034 = gen_lowpart_common (GET_MODE (m1->set_dest),
2035 m->set_dest);
2037 /* Get rid of the matching insn
2038 and prevent further processing of it. */
2039 m1->done = 1;
2041 /* if library call, delete all insn except last, which
2042 is deleted below */
2043 if (temp = find_reg_note (m1->insn, REG_RETVAL,
2044 NULL_RTX))
2046 for (temp = XEXP (temp, 0); temp != m1->insn;
2047 temp = NEXT_INSN (temp))
2048 delete_insn (temp);
2050 delete_insn (m1->insn);
2052 /* Any other movable that loads the same register
2053 MUST be moved. */
2054 already_moved[m1->regno] = 1;
2056 /* The reg merged here is now invariant,
2057 if the reg it matches is invariant. */
2058 if (! m->partial)
2059 n_times_set[m1->regno] = 0;
2062 else if (loop_dump_stream)
2063 fprintf (loop_dump_stream, "not desirable");
2065 else if (loop_dump_stream && !m->match)
2066 fprintf (loop_dump_stream, "not safe");
2068 if (loop_dump_stream)
2069 fprintf (loop_dump_stream, "\n");
2072 if (new_start == 0)
2073 new_start = loop_start;
2075 /* Go through all the instructions in the loop, making
2076 all the register substitutions scheduled in REG_MAP. */
2077 for (p = new_start; p != end; p = NEXT_INSN (p))
2078 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2079 || GET_CODE (p) == CALL_INSN)
2081 replace_regs (PATTERN (p), reg_map, nregs, 0);
2082 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2083 INSN_CODE (p) = -1;
2087 #if 0
2088 /* Scan X and replace the address of any MEM in it with ADDR.
2089 REG is the address that MEM should have before the replacement. */
2091 static void
2092 replace_call_address (x, reg, addr)
2093 rtx x, reg, addr;
2095 register enum rtx_code code;
2096 register int i;
2097 register char *fmt;
2099 if (x == 0)
2100 return;
2101 code = GET_CODE (x);
2102 switch (code)
2104 case PC:
2105 case CC0:
2106 case CONST_INT:
2107 case CONST_DOUBLE:
2108 case CONST:
2109 case SYMBOL_REF:
2110 case LABEL_REF:
2111 case REG:
2112 return;
2114 case SET:
2115 /* Short cut for very common case. */
2116 replace_call_address (XEXP (x, 1), reg, addr);
2117 return;
2119 case CALL:
2120 /* Short cut for very common case. */
2121 replace_call_address (XEXP (x, 0), reg, addr);
2122 return;
2124 case MEM:
2125 /* If this MEM uses a reg other than the one we expected,
2126 something is wrong. */
2127 if (XEXP (x, 0) != reg)
2128 abort ();
2129 XEXP (x, 0) = addr;
2130 return;
2132 default:
2133 break;
2136 fmt = GET_RTX_FORMAT (code);
2137 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2139 if (fmt[i] == 'e')
2140 replace_call_address (XEXP (x, i), reg, addr);
2141 if (fmt[i] == 'E')
2143 register int j;
2144 for (j = 0; j < XVECLEN (x, i); j++)
2145 replace_call_address (XVECEXP (x, i, j), reg, addr);
2149 #endif
2151 /* Return the number of memory refs to addresses that vary
2152 in the rtx X. */
2154 static int
2155 count_nonfixed_reads (x)
2156 rtx x;
2158 register enum rtx_code code;
2159 register int i;
2160 register char *fmt;
2161 int value;
2163 if (x == 0)
2164 return 0;
2166 code = GET_CODE (x);
2167 switch (code)
2169 case PC:
2170 case CC0:
2171 case CONST_INT:
2172 case CONST_DOUBLE:
2173 case CONST:
2174 case SYMBOL_REF:
2175 case LABEL_REF:
2176 case REG:
2177 return 0;
2179 case MEM:
2180 return ((invariant_p (XEXP (x, 0)) != 1)
2181 + count_nonfixed_reads (XEXP (x, 0)));
2183 default:
2184 break;
2187 value = 0;
2188 fmt = GET_RTX_FORMAT (code);
2189 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2191 if (fmt[i] == 'e')
2192 value += count_nonfixed_reads (XEXP (x, i));
2193 if (fmt[i] == 'E')
2195 register int j;
2196 for (j = 0; j < XVECLEN (x, i); j++)
2197 value += count_nonfixed_reads (XVECEXP (x, i, j));
2200 return value;
2204 #if 0
2205 /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2206 Replace it with an instruction to load just the low bytes
2207 if the machine supports such an instruction,
2208 and insert above LOOP_START an instruction to clear the register. */
2210 static void
2211 constant_high_bytes (p, loop_start)
2212 rtx p, loop_start;
2214 register rtx new;
2215 register int insn_code_number;
2217 /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2218 to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
2220 new = gen_rtx (SET, VOIDmode,
2221 gen_rtx (STRICT_LOW_PART, VOIDmode,
2222 gen_rtx (SUBREG, GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
2223 SET_DEST (PATTERN (p)),
2224 0)),
2225 XEXP (SET_SRC (PATTERN (p)), 0));
2226 insn_code_number = recog (new, p);
2228 if (insn_code_number)
2230 register int i;
2232 /* Clear destination register before the loop. */
2233 emit_insn_before (gen_rtx (SET, VOIDmode,
2234 SET_DEST (PATTERN (p)),
2235 const0_rtx),
2236 loop_start);
2238 /* Inside the loop, just load the low part. */
2239 PATTERN (p) = new;
2242 #endif
2244 /* Scan a loop setting the variables `unknown_address_altered',
2245 `num_mem_sets', `loop_continue', loops_enclosed', `loop_has_call',
2246 and `loop_has_volatile'.
2247 Also, fill in the array `loop_store_mems'. */
2249 static void
2250 prescan_loop (start, end)
2251 rtx start, end;
2253 register int level = 1;
2254 register rtx insn;
2256 unknown_address_altered = 0;
2257 loop_has_call = 0;
2258 loop_has_volatile = 0;
2259 loop_store_mems_idx = 0;
2261 num_mem_sets = 0;
2262 loops_enclosed = 1;
2263 loop_continue = 0;
2265 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2266 insn = NEXT_INSN (insn))
2268 if (GET_CODE (insn) == NOTE)
2270 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2272 ++level;
2273 /* Count number of loops contained in this one. */
2274 loops_enclosed++;
2276 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2278 --level;
2279 if (level == 0)
2281 end = insn;
2282 break;
2285 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2287 if (level == 1)
2288 loop_continue = insn;
2291 else if (GET_CODE (insn) == CALL_INSN)
2293 if (! CONST_CALL_P (insn))
2294 unknown_address_altered = 1;
2295 loop_has_call = 1;
2297 else
2299 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
2301 if (volatile_refs_p (PATTERN (insn)))
2302 loop_has_volatile = 1;
2304 note_stores (PATTERN (insn), note_addr_stored);
2310 /* Scan the function looking for loops. Record the start and end of each loop.
2311 Also mark as invalid loops any loops that contain a setjmp or are branched
2312 to from outside the loop. */
2314 static void
2315 find_and_verify_loops (f)
2316 rtx f;
2318 rtx insn, label;
2319 int current_loop = -1;
2320 int next_loop = -1;
2321 int loop;
2323 /* If there are jumps to undefined labels,
2324 treat them as jumps out of any/all loops.
2325 This also avoids writing past end of tables when there are no loops. */
2326 uid_loop_num[0] = -1;
2328 /* Find boundaries of loops, mark which loops are contained within
2329 loops, and invalidate loops that have setjmp. */
2331 for (insn = f; insn; insn = NEXT_INSN (insn))
2333 if (GET_CODE (insn) == NOTE)
2334 switch (NOTE_LINE_NUMBER (insn))
2336 case NOTE_INSN_LOOP_BEG:
2337 loop_number_loop_starts[++next_loop] = insn;
2338 loop_number_loop_ends[next_loop] = 0;
2339 loop_outer_loop[next_loop] = current_loop;
2340 loop_invalid[next_loop] = 0;
2341 loop_number_exit_labels[next_loop] = 0;
2342 loop_number_exit_count[next_loop] = 0;
2343 current_loop = next_loop;
2344 break;
2346 case NOTE_INSN_SETJMP:
2347 /* In this case, we must invalidate our current loop and any
2348 enclosing loop. */
2349 for (loop = current_loop; loop != -1; loop = loop_outer_loop[loop])
2351 loop_invalid[loop] = 1;
2352 if (loop_dump_stream)
2353 fprintf (loop_dump_stream,
2354 "\nLoop at %d ignored due to setjmp.\n",
2355 INSN_UID (loop_number_loop_starts[loop]));
2357 break;
2359 case NOTE_INSN_LOOP_END:
2360 if (current_loop == -1)
2361 abort ();
2363 loop_number_loop_ends[current_loop] = insn;
2364 current_loop = loop_outer_loop[current_loop];
2365 break;
2367 default:
2368 break;
2371 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2372 enclosing loop, but this doesn't matter. */
2373 uid_loop_num[INSN_UID (insn)] = current_loop;
2376 /* Any loop containing a label used in an initializer must be invalidated,
2377 because it can be jumped into from anywhere. */
2379 for (label = forced_labels; label; label = XEXP (label, 1))
2381 int loop_num;
2383 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2384 loop_num != -1;
2385 loop_num = loop_outer_loop[loop_num])
2386 loop_invalid[loop_num] = 1;
2389 /* Any loop containing a label used for an exception handler must be
2390 invalidated, because it can be jumped into from anywhere. */
2392 for (label = exception_handler_labels; label; label = XEXP (label, 1))
2394 int loop_num;
2396 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2397 loop_num != -1;
2398 loop_num = loop_outer_loop[loop_num])
2399 loop_invalid[loop_num] = 1;
2402 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2403 loop that it is not contained within, that loop is marked invalid.
2404 If any INSN or CALL_INSN uses a label's address, then the loop containing
2405 that label is marked invalid, because it could be jumped into from
2406 anywhere.
2408 Also look for blocks of code ending in an unconditional branch that
2409 exits the loop. If such a block is surrounded by a conditional
2410 branch around the block, move the block elsewhere (see below) and
2411 invert the jump to point to the code block. This may eliminate a
2412 label in our loop and will simplify processing by both us and a
2413 possible second cse pass. */
2415 for (insn = f; insn; insn = NEXT_INSN (insn))
2416 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2418 int this_loop_num = uid_loop_num[INSN_UID (insn)];
2420 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2422 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2423 if (note)
2425 int loop_num;
2427 for (loop_num = uid_loop_num[INSN_UID (XEXP (note, 0))];
2428 loop_num != -1;
2429 loop_num = loop_outer_loop[loop_num])
2430 loop_invalid[loop_num] = 1;
2434 if (GET_CODE (insn) != JUMP_INSN)
2435 continue;
2437 mark_loop_jump (PATTERN (insn), this_loop_num);
2439 /* See if this is an unconditional branch outside the loop. */
2440 if (this_loop_num != -1
2441 && (GET_CODE (PATTERN (insn)) == RETURN
2442 || (simplejump_p (insn)
2443 && (uid_loop_num[INSN_UID (JUMP_LABEL (insn))]
2444 != this_loop_num)))
2445 && get_max_uid () < max_uid_for_loop)
2447 rtx p;
2448 rtx our_next = next_real_insn (insn);
2449 int dest_loop;
2450 int outer_loop = -1;
2452 /* Go backwards until we reach the start of the loop, a label,
2453 or a JUMP_INSN. */
2454 for (p = PREV_INSN (insn);
2455 GET_CODE (p) != CODE_LABEL
2456 && ! (GET_CODE (p) == NOTE
2457 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2458 && GET_CODE (p) != JUMP_INSN;
2459 p = PREV_INSN (p))
2462 /* Check for the case where we have a jump to an inner nested
2463 loop, and do not perform the optimization in that case. */
2465 if (JUMP_LABEL (insn))
2467 dest_loop = uid_loop_num[INSN_UID (JUMP_LABEL (insn))];
2468 if (dest_loop != -1)
2470 for (outer_loop = dest_loop; outer_loop != -1;
2471 outer_loop = loop_outer_loop[outer_loop])
2472 if (outer_loop == this_loop_num)
2473 break;
2477 /* Make sure that the target of P is within the current loop. */
2479 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2480 && uid_loop_num[INSN_UID (JUMP_LABEL (p))] != this_loop_num)
2481 outer_loop = this_loop_num;
2483 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2484 we have a block of code to try to move.
2486 We look backward and then forward from the target of INSN
2487 to find a BARRIER at the same loop depth as the target.
2488 If we find such a BARRIER, we make a new label for the start
2489 of the block, invert the jump in P and point it to that label,
2490 and move the block of code to the spot we found. */
2492 if (outer_loop == -1
2493 && GET_CODE (p) == JUMP_INSN
2494 && JUMP_LABEL (p) != 0
2495 /* Just ignore jumps to labels that were never emitted.
2496 These always indicate compilation errors. */
2497 && INSN_UID (JUMP_LABEL (p)) != 0
2498 && condjump_p (p)
2499 && ! simplejump_p (p)
2500 && next_real_insn (JUMP_LABEL (p)) == our_next)
2502 rtx target
2503 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2504 int target_loop_num = uid_loop_num[INSN_UID (target)];
2505 rtx loc;
2507 for (loc = target; loc; loc = PREV_INSN (loc))
2508 if (GET_CODE (loc) == BARRIER
2509 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2510 break;
2512 if (loc == 0)
2513 for (loc = target; loc; loc = NEXT_INSN (loc))
2514 if (GET_CODE (loc) == BARRIER
2515 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2516 break;
2518 if (loc)
2520 rtx cond_label = JUMP_LABEL (p);
2521 rtx new_label = get_label_after (p);
2523 /* Ensure our label doesn't go away. */
2524 LABEL_NUSES (cond_label)++;
2526 /* Verify that uid_loop_num is large enough and that
2527 we can invert P. */
2528 if (invert_jump (p, new_label))
2530 rtx q, r;
2532 /* Include the BARRIER after INSN and copy the
2533 block after LOC. */
2534 new_label = squeeze_notes (new_label, NEXT_INSN (insn));
2535 reorder_insns (new_label, NEXT_INSN (insn), loc);
2537 /* All those insns are now in TARGET_LOOP_NUM. */
2538 for (q = new_label; q != NEXT_INSN (NEXT_INSN (insn));
2539 q = NEXT_INSN (q))
2540 uid_loop_num[INSN_UID (q)] = target_loop_num;
2542 /* The label jumped to by INSN is no longer a loop exit.
2543 Unless INSN does not have a label (e.g., it is a
2544 RETURN insn), search loop_number_exit_labels to find
2545 its label_ref, and remove it. Also turn off
2546 LABEL_OUTSIDE_LOOP_P bit. */
2547 if (JUMP_LABEL (insn))
2549 int loop_num;
2551 for (q = 0,
2552 r = loop_number_exit_labels[this_loop_num];
2553 r; q = r, r = LABEL_NEXTREF (r))
2554 if (XEXP (r, 0) == JUMP_LABEL (insn))
2556 LABEL_OUTSIDE_LOOP_P (r) = 0;
2557 if (q)
2558 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2559 else
2560 loop_number_exit_labels[this_loop_num]
2561 = LABEL_NEXTREF (r);
2562 break;
2565 for (loop_num = this_loop_num;
2566 loop_num != -1 && loop_num != target_loop_num;
2567 loop_num = loop_outer_loop[loop_num])
2568 loop_number_exit_count[loop_num]--;
2570 /* If we didn't find it, then something is wrong. */
2571 if (! r)
2572 abort ();
2575 /* P is now a jump outside the loop, so it must be put
2576 in loop_number_exit_labels, and marked as such.
2577 The easiest way to do this is to just call
2578 mark_loop_jump again for P. */
2579 mark_loop_jump (PATTERN (p), this_loop_num);
2581 /* If INSN now jumps to the insn after it,
2582 delete INSN. */
2583 if (JUMP_LABEL (insn) != 0
2584 && (next_real_insn (JUMP_LABEL (insn))
2585 == next_real_insn (insn)))
2586 delete_insn (insn);
2589 /* Continue the loop after where the conditional
2590 branch used to jump, since the only branch insn
2591 in the block (if it still remains) is an inter-loop
2592 branch and hence needs no processing. */
2593 insn = NEXT_INSN (cond_label);
2595 if (--LABEL_NUSES (cond_label) == 0)
2596 delete_insn (cond_label);
2598 /* This loop will be continued with NEXT_INSN (insn). */
2599 insn = PREV_INSN (insn);
2606 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2607 loops it is contained in, mark the target loop invalid.
2609 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2611 static void
2612 mark_loop_jump (x, loop_num)
2613 rtx x;
2614 int loop_num;
2616 int dest_loop;
2617 int outer_loop;
2618 int i;
2620 switch (GET_CODE (x))
2622 case PC:
2623 case USE:
2624 case CLOBBER:
2625 case REG:
2626 case MEM:
2627 case CONST_INT:
2628 case CONST_DOUBLE:
2629 case RETURN:
2630 return;
2632 case CONST:
2633 /* There could be a label reference in here. */
2634 mark_loop_jump (XEXP (x, 0), loop_num);
2635 return;
2637 case PLUS:
2638 case MINUS:
2639 case MULT:
2640 mark_loop_jump (XEXP (x, 0), loop_num);
2641 mark_loop_jump (XEXP (x, 1), loop_num);
2642 return;
2644 case SIGN_EXTEND:
2645 case ZERO_EXTEND:
2646 mark_loop_jump (XEXP (x, 0), loop_num);
2647 return;
2649 case LABEL_REF:
2650 dest_loop = uid_loop_num[INSN_UID (XEXP (x, 0))];
2652 /* Link together all labels that branch outside the loop. This
2653 is used by final_[bg]iv_value and the loop unrolling code. Also
2654 mark this LABEL_REF so we know that this branch should predict
2655 false. */
2657 /* A check to make sure the label is not in an inner nested loop,
2658 since this does not count as a loop exit. */
2659 if (dest_loop != -1)
2661 for (outer_loop = dest_loop; outer_loop != -1;
2662 outer_loop = loop_outer_loop[outer_loop])
2663 if (outer_loop == loop_num)
2664 break;
2666 else
2667 outer_loop = -1;
2669 if (loop_num != -1 && outer_loop == -1)
2671 LABEL_OUTSIDE_LOOP_P (x) = 1;
2672 LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
2673 loop_number_exit_labels[loop_num] = x;
2675 for (outer_loop = loop_num;
2676 outer_loop != -1 && outer_loop != dest_loop;
2677 outer_loop = loop_outer_loop[outer_loop])
2678 loop_number_exit_count[outer_loop]++;
2681 /* If this is inside a loop, but not in the current loop or one enclosed
2682 by it, it invalidates at least one loop. */
2684 if (dest_loop == -1)
2685 return;
2687 /* We must invalidate every nested loop containing the target of this
2688 label, except those that also contain the jump insn. */
2690 for (; dest_loop != -1; dest_loop = loop_outer_loop[dest_loop])
2692 /* Stop when we reach a loop that also contains the jump insn. */
2693 for (outer_loop = loop_num; outer_loop != -1;
2694 outer_loop = loop_outer_loop[outer_loop])
2695 if (dest_loop == outer_loop)
2696 return;
2698 /* If we get here, we know we need to invalidate a loop. */
2699 if (loop_dump_stream && ! loop_invalid[dest_loop])
2700 fprintf (loop_dump_stream,
2701 "\nLoop at %d ignored due to multiple entry points.\n",
2702 INSN_UID (loop_number_loop_starts[dest_loop]));
2704 loop_invalid[dest_loop] = 1;
2706 return;
2708 case SET:
2709 /* If this is not setting pc, ignore. */
2710 if (SET_DEST (x) == pc_rtx)
2711 mark_loop_jump (SET_SRC (x), loop_num);
2712 return;
2714 case IF_THEN_ELSE:
2715 mark_loop_jump (XEXP (x, 1), loop_num);
2716 mark_loop_jump (XEXP (x, 2), loop_num);
2717 return;
2719 case PARALLEL:
2720 case ADDR_VEC:
2721 for (i = 0; i < XVECLEN (x, 0); i++)
2722 mark_loop_jump (XVECEXP (x, 0, i), loop_num);
2723 return;
2725 case ADDR_DIFF_VEC:
2726 for (i = 0; i < XVECLEN (x, 1); i++)
2727 mark_loop_jump (XVECEXP (x, 1, i), loop_num);
2728 return;
2730 default:
2731 /* Treat anything else (such as a symbol_ref)
2732 as a branch out of this loop, but not into any loop. */
2734 if (loop_num != -1)
2736 #ifdef HAIFA
2737 LABEL_OUTSIDE_LOOP_P (x) = 1;
2738 LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
2739 #endif /* HAIFA */
2741 loop_number_exit_labels[loop_num] = x;
2743 for (outer_loop = loop_num; outer_loop != -1;
2744 outer_loop = loop_outer_loop[outer_loop])
2745 loop_number_exit_count[outer_loop]++;
2747 return;
2751 /* Return nonzero if there is a label in the range from
2752 insn INSN to and including the insn whose luid is END
2753 INSN must have an assigned luid (i.e., it must not have
2754 been previously created by loop.c). */
2756 static int
2757 labels_in_range_p (insn, end)
2758 rtx insn;
2759 int end;
2761 while (insn && INSN_LUID (insn) <= end)
2763 if (GET_CODE (insn) == CODE_LABEL)
2764 return 1;
2765 insn = NEXT_INSN (insn);
2768 return 0;
2771 /* Record that a memory reference X is being set. */
2773 static void
2774 note_addr_stored (x)
2775 rtx x;
2777 register int i;
2779 if (x == 0 || GET_CODE (x) != MEM)
2780 return;
2782 /* Count number of memory writes.
2783 This affects heuristics in strength_reduce. */
2784 num_mem_sets++;
2786 /* BLKmode MEM means all memory is clobbered. */
2787 if (GET_MODE (x) == BLKmode)
2788 unknown_address_altered = 1;
2790 if (unknown_address_altered)
2791 return;
2793 for (i = 0; i < loop_store_mems_idx; i++)
2794 if (rtx_equal_p (XEXP (loop_store_mems[i], 0), XEXP (x, 0))
2795 && MEM_IN_STRUCT_P (x) == MEM_IN_STRUCT_P (loop_store_mems[i]))
2797 /* We are storing at the same address as previously noted. Save the
2798 wider reference. */
2799 if (GET_MODE_SIZE (GET_MODE (x))
2800 > GET_MODE_SIZE (GET_MODE (loop_store_mems[i])))
2801 loop_store_mems[i] = x;
2802 break;
2805 if (i == NUM_STORES)
2806 unknown_address_altered = 1;
2808 else if (i == loop_store_mems_idx)
2809 loop_store_mems[loop_store_mems_idx++] = x;
2812 /* Return nonzero if the rtx X is invariant over the current loop.
2814 The value is 2 if we refer to something only conditionally invariant.
2816 If `unknown_address_altered' is nonzero, no memory ref is invariant.
2817 Otherwise, a memory ref is invariant if it does not conflict with
2818 anything stored in `loop_store_mems'. */
2821 invariant_p (x)
2822 register rtx x;
2824 register int i;
2825 register enum rtx_code code;
2826 register char *fmt;
2827 int conditional = 0;
2829 if (x == 0)
2830 return 1;
2831 code = GET_CODE (x);
2832 switch (code)
2834 case CONST_INT:
2835 case CONST_DOUBLE:
2836 case SYMBOL_REF:
2837 case CONST:
2838 return 1;
2840 case LABEL_REF:
2841 /* A LABEL_REF is normally invariant, however, if we are unrolling
2842 loops, and this label is inside the loop, then it isn't invariant.
2843 This is because each unrolled copy of the loop body will have
2844 a copy of this label. If this was invariant, then an insn loading
2845 the address of this label into a register might get moved outside
2846 the loop, and then each loop body would end up using the same label.
2848 We don't know the loop bounds here though, so just fail for all
2849 labels. */
2850 /* ??? This is also necessary if flag_rerun_loop_opt is true, because in
2851 this case we may be doing loop unrolling the second time we run loop,
2852 and hence the first loop run also needs this check. There is no way
2853 to check here whether the second run will actually do loop unrolling
2854 though, as that info is in a local var in rest_of_compilation. */
2855 if (flag_unroll_loops || flag_rerun_loop_opt)
2856 return 0;
2857 else
2858 return 1;
2860 case PC:
2861 case CC0:
2862 case UNSPEC_VOLATILE:
2863 return 0;
2865 case REG:
2866 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
2867 since the reg might be set by initialization within the loop. */
2869 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
2870 || x == arg_pointer_rtx)
2871 && ! current_function_has_nonlocal_goto)
2872 return 1;
2874 if (loop_has_call
2875 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
2876 return 0;
2878 if (n_times_set[REGNO (x)] < 0)
2879 return 2;
2881 return n_times_set[REGNO (x)] == 0;
2883 case MEM:
2884 /* Volatile memory references must be rejected. Do this before
2885 checking for read-only items, so that volatile read-only items
2886 will be rejected also. */
2887 if (MEM_VOLATILE_P (x))
2888 return 0;
2890 /* Read-only items (such as constants in a constant pool) are
2891 invariant if their address is. */
2892 if (RTX_UNCHANGING_P (x))
2893 break;
2895 /* If we filled the table (or had a subroutine call), any location
2896 in memory could have been clobbered. */
2897 if (unknown_address_altered)
2898 return 0;
2900 /* See if there is any dependence between a store and this load. */
2901 for (i = loop_store_mems_idx - 1; i >= 0; i--)
2902 if (true_dependence (loop_store_mems[i], VOIDmode, x, rtx_varies_p))
2903 return 0;
2905 /* It's not invalidated by a store in memory
2906 but we must still verify the address is invariant. */
2907 break;
2909 case ASM_OPERANDS:
2910 /* Don't mess with insns declared volatile. */
2911 if (MEM_VOLATILE_P (x))
2912 return 0;
2913 break;
2915 default:
2916 break;
2919 fmt = GET_RTX_FORMAT (code);
2920 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2922 if (fmt[i] == 'e')
2924 int tem = invariant_p (XEXP (x, i));
2925 if (tem == 0)
2926 return 0;
2927 if (tem == 2)
2928 conditional = 1;
2930 else if (fmt[i] == 'E')
2932 register int j;
2933 for (j = 0; j < XVECLEN (x, i); j++)
2935 int tem = invariant_p (XVECEXP (x, i, j));
2936 if (tem == 0)
2937 return 0;
2938 if (tem == 2)
2939 conditional = 1;
2945 return 1 + conditional;
2949 /* Return nonzero if all the insns in the loop that set REG
2950 are INSN and the immediately following insns,
2951 and if each of those insns sets REG in an invariant way
2952 (not counting uses of REG in them).
2954 The value is 2 if some of these insns are only conditionally invariant.
2956 We assume that INSN itself is the first set of REG
2957 and that its source is invariant. */
2959 static int
2960 consec_sets_invariant_p (reg, n_sets, insn)
2961 int n_sets;
2962 rtx reg, insn;
2964 register rtx p = insn;
2965 register int regno = REGNO (reg);
2966 rtx temp;
2967 /* Number of sets we have to insist on finding after INSN. */
2968 int count = n_sets - 1;
2969 int old = n_times_set[regno];
2970 int value = 0;
2971 int this;
2973 /* If N_SETS hit the limit, we can't rely on its value. */
2974 if (n_sets == 127)
2975 return 0;
2977 n_times_set[regno] = 0;
2979 while (count > 0)
2981 register enum rtx_code code;
2982 rtx set;
2984 p = NEXT_INSN (p);
2985 code = GET_CODE (p);
2987 /* If library call, skip to end of of it. */
2988 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
2989 p = XEXP (temp, 0);
2991 this = 0;
2992 if (code == INSN
2993 && (set = single_set (p))
2994 && GET_CODE (SET_DEST (set)) == REG
2995 && REGNO (SET_DEST (set)) == regno)
2997 this = invariant_p (SET_SRC (set));
2998 if (this != 0)
2999 value |= this;
3000 else if (temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
3002 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3003 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3004 notes are OK. */
3005 this = (CONSTANT_P (XEXP (temp, 0))
3006 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3007 && invariant_p (XEXP (temp, 0))));
3008 if (this != 0)
3009 value |= this;
3012 if (this != 0)
3013 count--;
3014 else if (code != NOTE)
3016 n_times_set[regno] = old;
3017 return 0;
3021 n_times_set[regno] = old;
3022 /* If invariant_p ever returned 2, we return 2. */
3023 return 1 + (value & 2);
3026 #if 0
3027 /* I don't think this condition is sufficient to allow INSN
3028 to be moved, so we no longer test it. */
3030 /* Return 1 if all insns in the basic block of INSN and following INSN
3031 that set REG are invariant according to TABLE. */
3033 static int
3034 all_sets_invariant_p (reg, insn, table)
3035 rtx reg, insn;
3036 short *table;
3038 register rtx p = insn;
3039 register int regno = REGNO (reg);
3041 while (1)
3043 register enum rtx_code code;
3044 p = NEXT_INSN (p);
3045 code = GET_CODE (p);
3046 if (code == CODE_LABEL || code == JUMP_INSN)
3047 return 1;
3048 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3049 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3050 && REGNO (SET_DEST (PATTERN (p))) == regno)
3052 if (!invariant_p (SET_SRC (PATTERN (p)), table))
3053 return 0;
3057 #endif /* 0 */
3059 /* Look at all uses (not sets) of registers in X. For each, if it is
3060 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3061 a different insn, set USAGE[REGNO] to const0_rtx. */
3063 static void
3064 find_single_use_in_loop (insn, x, usage)
3065 rtx insn;
3066 rtx x;
3067 rtx *usage;
3069 enum rtx_code code = GET_CODE (x);
3070 char *fmt = GET_RTX_FORMAT (code);
3071 int i, j;
3073 if (code == REG)
3074 usage[REGNO (x)]
3075 = (usage[REGNO (x)] != 0 && usage[REGNO (x)] != insn)
3076 ? const0_rtx : insn;
3078 else if (code == SET)
3080 /* Don't count SET_DEST if it is a REG; otherwise count things
3081 in SET_DEST because if a register is partially modified, it won't
3082 show up as a potential movable so we don't care how USAGE is set
3083 for it. */
3084 if (GET_CODE (SET_DEST (x)) != REG)
3085 find_single_use_in_loop (insn, SET_DEST (x), usage);
3086 find_single_use_in_loop (insn, SET_SRC (x), usage);
3088 else
3089 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3091 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3092 find_single_use_in_loop (insn, XEXP (x, i), usage);
3093 else if (fmt[i] == 'E')
3094 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3095 find_single_use_in_loop (insn, XVECEXP (x, i, j), usage);
3099 /* Increment N_TIMES_SET at the index of each register
3100 that is modified by an insn between FROM and TO.
3101 If the value of an element of N_TIMES_SET becomes 127 or more,
3102 stop incrementing it, to avoid overflow.
3104 Store in SINGLE_USAGE[I] the single insn in which register I is
3105 used, if it is only used once. Otherwise, it is set to 0 (for no
3106 uses) or const0_rtx for more than one use. This parameter may be zero,
3107 in which case this processing is not done.
3109 Store in *COUNT_PTR the number of actual instruction
3110 in the loop. We use this to decide what is worth moving out. */
3112 /* last_set[n] is nonzero iff reg n has been set in the current basic block.
3113 In that case, it is the insn that last set reg n. */
3115 static void
3116 count_loop_regs_set (from, to, may_not_move, single_usage, count_ptr, nregs)
3117 register rtx from, to;
3118 char *may_not_move;
3119 rtx *single_usage;
3120 int *count_ptr;
3121 int nregs;
3123 register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
3124 register rtx insn;
3125 register int count = 0;
3126 register rtx dest;
3128 bzero ((char *) last_set, nregs * sizeof (rtx));
3129 for (insn = from; insn != to; insn = NEXT_INSN (insn))
3131 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3133 ++count;
3135 /* If requested, record registers that have exactly one use. */
3136 if (single_usage)
3138 find_single_use_in_loop (insn, PATTERN (insn), single_usage);
3140 /* Include uses in REG_EQUAL notes. */
3141 if (REG_NOTES (insn))
3142 find_single_use_in_loop (insn, REG_NOTES (insn), single_usage);
3145 if (GET_CODE (PATTERN (insn)) == CLOBBER
3146 && GET_CODE (XEXP (PATTERN (insn), 0)) == REG)
3147 /* Don't move a reg that has an explicit clobber.
3148 We might do so sometimes, but it's not worth the pain. */
3149 may_not_move[REGNO (XEXP (PATTERN (insn), 0))] = 1;
3151 if (GET_CODE (PATTERN (insn)) == SET
3152 || GET_CODE (PATTERN (insn)) == CLOBBER)
3154 dest = SET_DEST (PATTERN (insn));
3155 while (GET_CODE (dest) == SUBREG
3156 || GET_CODE (dest) == ZERO_EXTRACT
3157 || GET_CODE (dest) == SIGN_EXTRACT
3158 || GET_CODE (dest) == STRICT_LOW_PART)
3159 dest = XEXP (dest, 0);
3160 if (GET_CODE (dest) == REG)
3162 register int regno = REGNO (dest);
3163 /* If this is the first setting of this reg
3164 in current basic block, and it was set before,
3165 it must be set in two basic blocks, so it cannot
3166 be moved out of the loop. */
3167 if (n_times_set[regno] > 0 && last_set[regno] == 0)
3168 may_not_move[regno] = 1;
3169 /* If this is not first setting in current basic block,
3170 see if reg was used in between previous one and this.
3171 If so, neither one can be moved. */
3172 if (last_set[regno] != 0
3173 && reg_used_between_p (dest, last_set[regno], insn))
3174 may_not_move[regno] = 1;
3175 if (n_times_set[regno] < 127)
3176 ++n_times_set[regno];
3177 last_set[regno] = insn;
3180 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3182 register int i;
3183 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3185 register rtx x = XVECEXP (PATTERN (insn), 0, i);
3186 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3187 /* Don't move a reg that has an explicit clobber.
3188 It's not worth the pain to try to do it correctly. */
3189 may_not_move[REGNO (XEXP (x, 0))] = 1;
3191 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3193 dest = SET_DEST (x);
3194 while (GET_CODE (dest) == SUBREG
3195 || GET_CODE (dest) == ZERO_EXTRACT
3196 || GET_CODE (dest) == SIGN_EXTRACT
3197 || GET_CODE (dest) == STRICT_LOW_PART)
3198 dest = XEXP (dest, 0);
3199 if (GET_CODE (dest) == REG)
3201 register int regno = REGNO (dest);
3202 if (n_times_set[regno] > 0 && last_set[regno] == 0)
3203 may_not_move[regno] = 1;
3204 if (last_set[regno] != 0
3205 && reg_used_between_p (dest, last_set[regno], insn))
3206 may_not_move[regno] = 1;
3207 if (n_times_set[regno] < 127)
3208 ++n_times_set[regno];
3209 last_set[regno] = insn;
3216 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
3217 bzero ((char *) last_set, nregs * sizeof (rtx));
3219 *count_ptr = count;
3222 /* Given a loop that is bounded by LOOP_START and LOOP_END
3223 and that is entered at SCAN_START,
3224 return 1 if the register set in SET contained in insn INSN is used by
3225 any insn that precedes INSN in cyclic order starting
3226 from the loop entry point.
3228 We don't want to use INSN_LUID here because if we restrict INSN to those
3229 that have a valid INSN_LUID, it means we cannot move an invariant out
3230 from an inner loop past two loops. */
3232 static int
3233 loop_reg_used_before_p (set, insn, loop_start, scan_start, loop_end)
3234 rtx set, insn, loop_start, scan_start, loop_end;
3236 rtx reg = SET_DEST (set);
3237 rtx p;
3239 /* Scan forward checking for register usage. If we hit INSN, we
3240 are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
3241 for (p = scan_start; p != insn; p = NEXT_INSN (p))
3243 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
3244 && reg_overlap_mentioned_p (reg, PATTERN (p)))
3245 return 1;
3247 if (p == loop_end)
3248 p = loop_start;
3251 return 0;
3254 /* A "basic induction variable" or biv is a pseudo reg that is set
3255 (within this loop) only by incrementing or decrementing it. */
3256 /* A "general induction variable" or giv is a pseudo reg whose
3257 value is a linear function of a biv. */
3259 /* Bivs are recognized by `basic_induction_var';
3260 Givs by `general_induct_var'. */
3262 /* Indexed by register number, indicates whether or not register is an
3263 induction variable, and if so what type. */
3265 enum iv_mode *reg_iv_type;
3267 /* Indexed by register number, contains pointer to `struct induction'
3268 if register is an induction variable. This holds general info for
3269 all induction variables. */
3271 struct induction **reg_iv_info;
3273 /* Indexed by register number, contains pointer to `struct iv_class'
3274 if register is a basic induction variable. This holds info describing
3275 the class (a related group) of induction variables that the biv belongs
3276 to. */
3278 struct iv_class **reg_biv_class;
3280 /* The head of a list which links together (via the next field)
3281 every iv class for the current loop. */
3283 struct iv_class *loop_iv_list;
3285 /* Communication with routines called via `note_stores'. */
3287 static rtx note_insn;
3289 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3291 static rtx addr_placeholder;
3293 /* ??? Unfinished optimizations, and possible future optimizations,
3294 for the strength reduction code. */
3296 /* ??? There is one more optimization you might be interested in doing: to
3297 allocate pseudo registers for frequently-accessed memory locations.
3298 If the same memory location is referenced each time around, it might
3299 be possible to copy it into a register before and out after.
3300 This is especially useful when the memory location is a variable which
3301 is in a stack slot because somewhere its address is taken. If the
3302 loop doesn't contain a function call and the variable isn't volatile,
3303 it is safe to keep the value in a register for the duration of the
3304 loop. One tricky thing is that the copying of the value back from the
3305 register has to be done on all exits from the loop. You need to check that
3306 all the exits from the loop go to the same place. */
3308 /* ??? The interaction of biv elimination, and recognition of 'constant'
3309 bivs, may cause problems. */
3311 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3312 performance problems.
3314 Perhaps don't eliminate things that can be combined with an addressing
3315 mode. Find all givs that have the same biv, mult_val, and add_val;
3316 then for each giv, check to see if its only use dies in a following
3317 memory address. If so, generate a new memory address and check to see
3318 if it is valid. If it is valid, then store the modified memory address,
3319 otherwise, mark the giv as not done so that it will get its own iv. */
3321 /* ??? Could try to optimize branches when it is known that a biv is always
3322 positive. */
3324 /* ??? When replace a biv in a compare insn, we should replace with closest
3325 giv so that an optimized branch can still be recognized by the combiner,
3326 e.g. the VAX acb insn. */
3328 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3329 was rerun in loop_optimize whenever a register was added or moved.
3330 Also, some of the optimizations could be a little less conservative. */
3332 /* Perform strength reduction and induction variable elimination. */
3334 /* Pseudo registers created during this function will be beyond the last
3335 valid index in several tables including n_times_set and regno_last_uid.
3336 This does not cause a problem here, because the added registers cannot be
3337 givs outside of their loop, and hence will never be reconsidered.
3338 But scan_loop must check regnos to make sure they are in bounds. */
3340 static void
3341 strength_reduce (scan_start, end, loop_top, insn_count,
3342 loop_start, loop_end)
3343 rtx scan_start;
3344 rtx end;
3345 rtx loop_top;
3346 int insn_count;
3347 rtx loop_start;
3348 rtx loop_end;
3350 rtx p;
3351 rtx set;
3352 rtx inc_val;
3353 rtx mult_val;
3354 rtx dest_reg;
3355 /* This is 1 if current insn is not executed at least once for every loop
3356 iteration. */
3357 int not_every_iteration = 0;
3358 /* This is 1 if current insn may be executed more than once for every
3359 loop iteration. */
3360 int maybe_multiple = 0;
3361 /* Temporary list pointers for traversing loop_iv_list. */
3362 struct iv_class *bl, **backbl;
3363 /* Ratio of extra register life span we can justify
3364 for saving an instruction. More if loop doesn't call subroutines
3365 since in that case saving an insn makes more difference
3366 and more registers are available. */
3367 /* ??? could set this to last value of threshold in move_movables */
3368 int threshold = (loop_has_call ? 1 : 2) * (3 + n_non_fixed_regs);
3369 /* Map of pseudo-register replacements. */
3370 rtx *reg_map;
3371 int call_seen;
3372 rtx test;
3373 rtx end_insert_before;
3374 int loop_depth = 0;
3376 reg_iv_type = (enum iv_mode *) alloca (max_reg_before_loop
3377 * sizeof (enum iv_mode *));
3378 bzero ((char *) reg_iv_type, max_reg_before_loop * sizeof (enum iv_mode *));
3379 reg_iv_info = (struct induction **)
3380 alloca (max_reg_before_loop * sizeof (struct induction *));
3381 bzero ((char *) reg_iv_info, (max_reg_before_loop
3382 * sizeof (struct induction *)));
3383 reg_biv_class = (struct iv_class **)
3384 alloca (max_reg_before_loop * sizeof (struct iv_class *));
3385 bzero ((char *) reg_biv_class, (max_reg_before_loop
3386 * sizeof (struct iv_class *)));
3388 loop_iv_list = 0;
3389 addr_placeholder = gen_reg_rtx (Pmode);
3391 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3392 must be put before this insn, so that they will appear in the right
3393 order (i.e. loop order).
3395 If loop_end is the end of the current function, then emit a
3396 NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3397 dummy note insn. */
3398 if (NEXT_INSN (loop_end) != 0)
3399 end_insert_before = NEXT_INSN (loop_end);
3400 else
3401 end_insert_before = emit_note_after (NOTE_INSN_DELETED, loop_end);
3403 /* Scan through loop to find all possible bivs. */
3405 p = scan_start;
3406 while (1)
3408 p = NEXT_INSN (p);
3409 /* At end of a straight-in loop, we are done.
3410 At end of a loop entered at the bottom, scan the top. */
3411 if (p == scan_start)
3412 break;
3413 if (p == end)
3415 if (loop_top != 0)
3416 p = loop_top;
3417 else
3418 break;
3419 if (p == scan_start)
3420 break;
3423 if (GET_CODE (p) == INSN
3424 && (set = single_set (p))
3425 && GET_CODE (SET_DEST (set)) == REG)
3427 dest_reg = SET_DEST (set);
3428 if (REGNO (dest_reg) < max_reg_before_loop
3429 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
3430 && reg_iv_type[REGNO (dest_reg)] != NOT_BASIC_INDUCT)
3432 if (basic_induction_var (SET_SRC (set), GET_MODE (SET_SRC (set)),
3433 dest_reg, p, &inc_val, &mult_val))
3435 /* It is a possible basic induction variable.
3436 Create and initialize an induction structure for it. */
3438 struct induction *v
3439 = (struct induction *) alloca (sizeof (struct induction));
3441 record_biv (v, p, dest_reg, inc_val, mult_val,
3442 not_every_iteration, maybe_multiple);
3443 reg_iv_type[REGNO (dest_reg)] = BASIC_INDUCT;
3445 else if (REGNO (dest_reg) < max_reg_before_loop)
3446 reg_iv_type[REGNO (dest_reg)] = NOT_BASIC_INDUCT;
3450 /* Past CODE_LABEL, we get to insns that may be executed multiple
3451 times. The only way we can be sure that they can't is if every
3452 every jump insn between here and the end of the loop either
3453 returns, exits the loop, is a forward jump, or is a jump
3454 to the loop start. */
3456 if (GET_CODE (p) == CODE_LABEL)
3458 rtx insn = p;
3460 maybe_multiple = 0;
3462 while (1)
3464 insn = NEXT_INSN (insn);
3465 if (insn == scan_start)
3466 break;
3467 if (insn == end)
3469 if (loop_top != 0)
3470 insn = loop_top;
3471 else
3472 break;
3473 if (insn == scan_start)
3474 break;
3477 if (GET_CODE (insn) == JUMP_INSN
3478 && GET_CODE (PATTERN (insn)) != RETURN
3479 && (! condjump_p (insn)
3480 || (JUMP_LABEL (insn) != 0
3481 && JUMP_LABEL (insn) != scan_start
3482 && (INSN_UID (JUMP_LABEL (insn)) >= max_uid_for_loop
3483 || INSN_UID (insn) >= max_uid_for_loop
3484 || (INSN_LUID (JUMP_LABEL (insn))
3485 < INSN_LUID (insn))))))
3487 maybe_multiple = 1;
3488 break;
3493 /* Past a jump, we get to insns for which we can't count
3494 on whether they will be executed during each iteration. */
3495 /* This code appears twice in strength_reduce. There is also similar
3496 code in scan_loop. */
3497 if (GET_CODE (p) == JUMP_INSN
3498 /* If we enter the loop in the middle, and scan around to the
3499 beginning, don't set not_every_iteration for that.
3500 This can be any kind of jump, since we want to know if insns
3501 will be executed if the loop is executed. */
3502 && ! (JUMP_LABEL (p) == loop_top
3503 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3504 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3506 rtx label = 0;
3508 /* If this is a jump outside the loop, then it also doesn't
3509 matter. Check to see if the target of this branch is on the
3510 loop_number_exits_labels list. */
3512 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3513 label;
3514 label = LABEL_NEXTREF (label))
3515 if (XEXP (label, 0) == JUMP_LABEL (p))
3516 break;
3518 if (! label)
3519 not_every_iteration = 1;
3522 else if (GET_CODE (p) == NOTE)
3524 /* At the virtual top of a converted loop, insns are again known to
3525 be executed each iteration: logically, the loop begins here
3526 even though the exit code has been duplicated. */
3527 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3528 not_every_iteration = 0;
3529 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3530 loop_depth++;
3531 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3532 loop_depth--;
3535 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3536 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3537 or not an insn is known to be executed each iteration of the
3538 loop, whether or not any iterations are known to occur.
3540 Therefore, if we have just passed a label and have no more labels
3541 between here and the test insn of the loop, we know these insns
3542 will be executed each iteration. */
3544 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3545 && no_labels_between_p (p, loop_end))
3546 not_every_iteration = 0;
3549 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3550 Make a sanity check against n_times_set. */
3551 for (backbl = &loop_iv_list, bl = *backbl; bl; bl = bl->next)
3553 if (reg_iv_type[bl->regno] != BASIC_INDUCT
3554 /* Above happens if register modified by subreg, etc. */
3555 /* Make sure it is not recognized as a basic induction var: */
3556 || n_times_set[bl->regno] != bl->biv_count
3557 /* If never incremented, it is invariant that we decided not to
3558 move. So leave it alone. */
3559 || ! bl->incremented)
3561 if (loop_dump_stream)
3562 fprintf (loop_dump_stream, "Reg %d: biv discarded, %s\n",
3563 bl->regno,
3564 (reg_iv_type[bl->regno] != BASIC_INDUCT
3565 ? "not induction variable"
3566 : (! bl->incremented ? "never incremented"
3567 : "count error")));
3569 reg_iv_type[bl->regno] = NOT_BASIC_INDUCT;
3570 *backbl = bl->next;
3572 else
3574 backbl = &bl->next;
3576 if (loop_dump_stream)
3577 fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
3581 /* Exit if there are no bivs. */
3582 if (! loop_iv_list)
3584 /* Can still unroll the loop anyways, but indicate that there is no
3585 strength reduction info available. */
3586 if (flag_unroll_loops)
3587 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 0);
3589 return;
3592 /* Find initial value for each biv by searching backwards from loop_start,
3593 halting at first label. Also record any test condition. */
3595 call_seen = 0;
3596 for (p = loop_start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3598 note_insn = p;
3600 if (GET_CODE (p) == CALL_INSN)
3601 call_seen = 1;
3603 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3604 || GET_CODE (p) == CALL_INSN)
3605 note_stores (PATTERN (p), record_initial);
3607 /* Record any test of a biv that branches around the loop if no store
3608 between it and the start of loop. We only care about tests with
3609 constants and registers and only certain of those. */
3610 if (GET_CODE (p) == JUMP_INSN
3611 && JUMP_LABEL (p) != 0
3612 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end)
3613 && (test = get_condition_for_loop (p)) != 0
3614 && GET_CODE (XEXP (test, 0)) == REG
3615 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3616 && (bl = reg_biv_class[REGNO (XEXP (test, 0))]) != 0
3617 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop_start)
3618 && bl->init_insn == 0)
3620 /* If an NE test, we have an initial value! */
3621 if (GET_CODE (test) == NE)
3623 bl->init_insn = p;
3624 bl->init_set = gen_rtx (SET, VOIDmode,
3625 XEXP (test, 0), XEXP (test, 1));
3627 else
3628 bl->initial_test = test;
3632 /* Look at the each biv and see if we can say anything better about its
3633 initial value from any initializing insns set up above. (This is done
3634 in two passes to avoid missing SETs in a PARALLEL.) */
3635 for (bl = loop_iv_list; bl; bl = bl->next)
3637 rtx src;
3639 if (! bl->init_insn)
3640 continue;
3642 src = SET_SRC (bl->init_set);
3644 if (loop_dump_stream)
3645 fprintf (loop_dump_stream,
3646 "Biv %d initialized at insn %d: initial value ",
3647 bl->regno, INSN_UID (bl->init_insn));
3649 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
3650 || GET_MODE (src) == VOIDmode)
3651 && valid_initial_value_p (src, bl->init_insn, call_seen, loop_start))
3653 bl->initial_value = src;
3655 if (loop_dump_stream)
3657 if (GET_CODE (src) == CONST_INT)
3658 fprintf (loop_dump_stream, "%d\n", INTVAL (src));
3659 else
3661 print_rtl (loop_dump_stream, src);
3662 fprintf (loop_dump_stream, "\n");
3666 else
3668 /* Biv initial value is not simple move,
3669 so let it keep initial value of "itself". */
3671 if (loop_dump_stream)
3672 fprintf (loop_dump_stream, "is complex\n");
3676 /* Search the loop for general induction variables. */
3678 /* A register is a giv if: it is only set once, it is a function of a
3679 biv and a constant (or invariant), and it is not a biv. */
3681 not_every_iteration = 0;
3682 loop_depth = 0;
3683 p = scan_start;
3684 while (1)
3686 p = NEXT_INSN (p);
3687 /* At end of a straight-in loop, we are done.
3688 At end of a loop entered at the bottom, scan the top. */
3689 if (p == scan_start)
3690 break;
3691 if (p == end)
3693 if (loop_top != 0)
3694 p = loop_top;
3695 else
3696 break;
3697 if (p == scan_start)
3698 break;
3701 /* Look for a general induction variable in a register. */
3702 if (GET_CODE (p) == INSN
3703 && (set = single_set (p))
3704 && GET_CODE (SET_DEST (set)) == REG
3705 && ! may_not_optimize[REGNO (SET_DEST (set))])
3707 rtx src_reg;
3708 rtx add_val;
3709 rtx mult_val;
3710 int benefit;
3711 rtx regnote = 0;
3713 dest_reg = SET_DEST (set);
3714 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
3715 continue;
3717 if (/* SET_SRC is a giv. */
3718 ((benefit = general_induction_var (SET_SRC (set),
3719 &src_reg, &add_val,
3720 &mult_val))
3721 /* Equivalent expression is a giv. */
3722 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
3723 && (benefit = general_induction_var (XEXP (regnote, 0),
3724 &src_reg,
3725 &add_val, &mult_val))))
3726 /* Don't try to handle any regs made by loop optimization.
3727 We have nothing on them in regno_first_uid, etc. */
3728 && REGNO (dest_reg) < max_reg_before_loop
3729 /* Don't recognize a BASIC_INDUCT_VAR here. */
3730 && dest_reg != src_reg
3731 /* This must be the only place where the register is set. */
3732 && (n_times_set[REGNO (dest_reg)] == 1
3733 /* or all sets must be consecutive and make a giv. */
3734 || (benefit = consec_sets_giv (benefit, p,
3735 src_reg, dest_reg,
3736 &add_val, &mult_val))))
3738 int count;
3739 struct induction *v
3740 = (struct induction *) alloca (sizeof (struct induction));
3741 rtx temp;
3743 /* If this is a library call, increase benefit. */
3744 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
3745 benefit += libcall_benefit (p);
3747 /* Skip the consecutive insns, if there are any. */
3748 for (count = n_times_set[REGNO (dest_reg)] - 1;
3749 count > 0; count--)
3751 /* If first insn of libcall sequence, skip to end.
3752 Do this at start of loop, since INSN is guaranteed to
3753 be an insn here. */
3754 if (GET_CODE (p) != NOTE
3755 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3756 p = XEXP (temp, 0);
3758 do p = NEXT_INSN (p);
3759 while (GET_CODE (p) == NOTE);
3762 record_giv (v, p, src_reg, dest_reg, mult_val, add_val, benefit,
3763 DEST_REG, not_every_iteration, NULL_PTR, loop_start,
3764 loop_end);
3769 #ifndef DONT_REDUCE_ADDR
3770 /* Look for givs which are memory addresses. */
3771 /* This resulted in worse code on a VAX 8600. I wonder if it
3772 still does. */
3773 if (GET_CODE (p) == INSN)
3774 find_mem_givs (PATTERN (p), p, not_every_iteration, loop_start,
3775 loop_end);
3776 #endif
3778 /* Update the status of whether giv can derive other givs. This can
3779 change when we pass a label or an insn that updates a biv. */
3780 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3781 || GET_CODE (p) == CODE_LABEL)
3782 update_giv_derive (p);
3784 /* Past a jump, we get to insns for which we can't count
3785 on whether they will be executed during each iteration. */
3786 /* This code appears twice in strength_reduce. There is also similar
3787 code in scan_loop. */
3788 if (GET_CODE (p) == JUMP_INSN
3789 /* If we enter the loop in the middle, and scan around to the
3790 beginning, don't set not_every_iteration for that.
3791 This can be any kind of jump, since we want to know if insns
3792 will be executed if the loop is executed. */
3793 && ! (JUMP_LABEL (p) == loop_top
3794 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3795 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3797 rtx label = 0;
3799 /* If this is a jump outside the loop, then it also doesn't
3800 matter. Check to see if the target of this branch is on the
3801 loop_number_exits_labels list. */
3803 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3804 label;
3805 label = LABEL_NEXTREF (label))
3806 if (XEXP (label, 0) == JUMP_LABEL (p))
3807 break;
3809 if (! label)
3810 not_every_iteration = 1;
3813 else if (GET_CODE (p) == NOTE)
3815 /* At the virtual top of a converted loop, insns are again known to
3816 be executed each iteration: logically, the loop begins here
3817 even though the exit code has been duplicated. */
3818 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3819 not_every_iteration = 0;
3820 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3821 loop_depth++;
3822 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3823 loop_depth--;
3826 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3827 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3828 or not an insn is known to be executed each iteration of the
3829 loop, whether or not any iterations are known to occur.
3831 Therefore, if we have just passed a label and have no more labels
3832 between here and the test insn of the loop, we know these insns
3833 will be executed each iteration. */
3835 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3836 && no_labels_between_p (p, loop_end))
3837 not_every_iteration = 0;
3840 /* Try to calculate and save the number of loop iterations. This is
3841 set to zero if the actual number can not be calculated. This must
3842 be called after all giv's have been identified, since otherwise it may
3843 fail if the iteration variable is a giv. */
3845 loop_n_iterations = loop_iterations (loop_start, loop_end);
3847 /* Now for each giv for which we still don't know whether or not it is
3848 replaceable, check to see if it is replaceable because its final value
3849 can be calculated. This must be done after loop_iterations is called,
3850 so that final_giv_value will work correctly. */
3852 for (bl = loop_iv_list; bl; bl = bl->next)
3854 struct induction *v;
3856 for (v = bl->giv; v; v = v->next_iv)
3857 if (! v->replaceable && ! v->not_replaceable)
3858 check_final_value (v, loop_start, loop_end);
3861 /* Try to prove that the loop counter variable (if any) is always
3862 nonnegative; if so, record that fact with a REG_NONNEG note
3863 so that "decrement and branch until zero" insn can be used. */
3864 check_dbra_loop (loop_end, insn_count, loop_start);
3866 #ifdef HAIFA
3867 /* record loop-variables relevant for BCT optimization before unrolling
3868 the loop. Unrolling may update part of this information, and the
3869 correct data will be used for generating the BCT. */
3870 #ifdef HAVE_decrement_and_branch_on_count
3871 if (HAVE_decrement_and_branch_on_count)
3872 analyze_loop_iterations (loop_start, loop_end);
3873 #endif
3874 #endif /* HAIFA */
3876 /* Create reg_map to hold substitutions for replaceable giv regs. */
3877 reg_map = (rtx *) alloca (max_reg_before_loop * sizeof (rtx));
3878 bzero ((char *) reg_map, max_reg_before_loop * sizeof (rtx));
3880 /* Examine each iv class for feasibility of strength reduction/induction
3881 variable elimination. */
3883 for (bl = loop_iv_list; bl; bl = bl->next)
3885 struct induction *v;
3886 int benefit;
3887 int all_reduced;
3888 rtx final_value = 0;
3890 /* Test whether it will be possible to eliminate this biv
3891 provided all givs are reduced. This is possible if either
3892 the reg is not used outside the loop, or we can compute
3893 what its final value will be.
3895 For architectures with a decrement_and_branch_until_zero insn,
3896 don't do this if we put a REG_NONNEG note on the endtest for
3897 this biv. */
3899 /* Compare against bl->init_insn rather than loop_start.
3900 We aren't concerned with any uses of the biv between
3901 init_insn and loop_start since these won't be affected
3902 by the value of the biv elsewhere in the function, so
3903 long as init_insn doesn't use the biv itself.
3904 March 14, 1989 -- self@bayes.arc.nasa.gov */
3906 if ((uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
3907 && bl->init_insn
3908 && INSN_UID (bl->init_insn) < max_uid_for_loop
3909 && uid_luid[REGNO_FIRST_UID (bl->regno)] >= INSN_LUID (bl->init_insn)
3910 #ifdef HAVE_decrement_and_branch_until_zero
3911 && ! bl->nonneg
3912 #endif
3913 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
3914 || ((final_value = final_biv_value (bl, loop_start, loop_end))
3915 #ifdef HAVE_decrement_and_branch_until_zero
3916 && ! bl->nonneg
3917 #endif
3919 bl->eliminable = maybe_eliminate_biv (bl, loop_start, end, 0,
3920 threshold, insn_count);
3921 else
3923 if (loop_dump_stream)
3925 fprintf (loop_dump_stream,
3926 "Cannot eliminate biv %d.\n",
3927 bl->regno);
3928 fprintf (loop_dump_stream,
3929 "First use: insn %d, last use: insn %d.\n",
3930 REGNO_FIRST_UID (bl->regno),
3931 REGNO_LAST_UID (bl->regno));
3935 /* Combine all giv's for this iv_class. */
3936 combine_givs (bl);
3938 /* This will be true at the end, if all givs which depend on this
3939 biv have been strength reduced.
3940 We can't (currently) eliminate the biv unless this is so. */
3941 all_reduced = 1;
3943 /* Check each giv in this class to see if we will benefit by reducing
3944 it. Skip giv's combined with others. */
3945 for (v = bl->giv; v; v = v->next_iv)
3947 struct induction *tv;
3949 if (v->ignore || v->same)
3950 continue;
3952 benefit = v->benefit;
3954 /* Reduce benefit if not replaceable, since we will insert
3955 a move-insn to replace the insn that calculates this giv.
3956 Don't do this unless the giv is a user variable, since it
3957 will often be marked non-replaceable because of the duplication
3958 of the exit code outside the loop. In such a case, the copies
3959 we insert are dead and will be deleted. So they don't have
3960 a cost. Similar situations exist. */
3961 /* ??? The new final_[bg]iv_value code does a much better job
3962 of finding replaceable giv's, and hence this code may no longer
3963 be necessary. */
3964 if (! v->replaceable && ! bl->eliminable
3965 && REG_USERVAR_P (v->dest_reg))
3966 benefit -= copy_cost;
3968 /* Decrease the benefit to count the add-insns that we will
3969 insert to increment the reduced reg for the giv. */
3970 benefit -= add_cost * bl->biv_count;
3972 /* Decide whether to strength-reduce this giv or to leave the code
3973 unchanged (recompute it from the biv each time it is used).
3974 This decision can be made independently for each giv. */
3976 #ifdef AUTO_INC_DEC
3977 /* Attempt to guess whether autoincrement will handle some of the
3978 new add insns; if so, increase BENEFIT (undo the subtraction of
3979 add_cost that was done above). */
3980 if (v->giv_type == DEST_ADDR
3981 && GET_CODE (v->mult_val) == CONST_INT)
3983 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_PRE_INCREMENT)
3984 if (INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
3985 benefit += add_cost * bl->biv_count;
3986 #endif
3987 #if defined (HAVE_POST_DECREMENT) || defined (HAVE_PRE_DECREMENT)
3988 if (-INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
3989 benefit += add_cost * bl->biv_count;
3990 #endif
3992 #endif
3994 /* If an insn is not to be strength reduced, then set its ignore
3995 flag, and clear all_reduced. */
3997 /* A giv that depends on a reversed biv must be reduced if it is
3998 used after the loop exit, otherwise, it would have the wrong
3999 value after the loop exit. To make it simple, just reduce all
4000 of such giv's whether or not we know they are used after the loop
4001 exit. */
4003 if ( ! flag_reduce_all_givs && v->lifetime * threshold * benefit < insn_count
4004 && ! bl->reversed )
4006 if (loop_dump_stream)
4007 fprintf (loop_dump_stream,
4008 "giv of insn %d not worth while, %d vs %d.\n",
4009 INSN_UID (v->insn),
4010 v->lifetime * threshold * benefit, insn_count);
4011 v->ignore = 1;
4012 all_reduced = 0;
4014 else
4016 /* Check that we can increment the reduced giv without a
4017 multiply insn. If not, reject it. */
4019 for (tv = bl->biv; tv; tv = tv->next_iv)
4020 if (tv->mult_val == const1_rtx
4021 && ! product_cheap_p (tv->add_val, v->mult_val))
4023 if (loop_dump_stream)
4024 fprintf (loop_dump_stream,
4025 "giv of insn %d: would need a multiply.\n",
4026 INSN_UID (v->insn));
4027 v->ignore = 1;
4028 all_reduced = 0;
4029 break;
4034 /* Reduce each giv that we decided to reduce. */
4036 for (v = bl->giv; v; v = v->next_iv)
4038 struct induction *tv;
4039 if (! v->ignore && v->same == 0)
4041 int auto_inc_opt = 0;
4043 v->new_reg = gen_reg_rtx (v->mode);
4045 #ifdef AUTO_INC_DEC
4046 /* If the target has auto-increment addressing modes, and
4047 this is an address giv, then try to put the increment
4048 immediately after its use, so that flow can create an
4049 auto-increment addressing mode. */
4050 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4051 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4052 /* We don't handle reversed biv's because bl->biv->insn
4053 does not have a valid INSN_LUID. */
4054 && ! bl->reversed
4055 && v->always_executed && ! v->maybe_multiple)
4057 /* If other giv's have been combined with this one, then
4058 this will work only if all uses of the other giv's occur
4059 before this giv's insn. This is difficult to check.
4061 We simplify this by looking for the common case where
4062 there is one DEST_REG giv, and this giv's insn is the
4063 last use of the dest_reg of that DEST_REG giv. If the
4064 the increment occurs after the address giv, then we can
4065 perform the optimization. (Otherwise, the increment
4066 would have to go before other_giv, and we would not be
4067 able to combine it with the address giv to get an
4068 auto-inc address.) */
4069 if (v->combined_with)
4071 struct induction *other_giv = 0;
4073 for (tv = bl->giv; tv; tv = tv->next_iv)
4074 if (tv->same == v)
4076 if (other_giv)
4077 break;
4078 else
4079 other_giv = tv;
4081 if (! tv && other_giv
4082 && REGNO (other_giv->dest_reg) < max_reg_before_loop
4083 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4084 == INSN_UID (v->insn))
4085 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4086 auto_inc_opt = 1;
4088 /* Check for case where increment is before the the address
4089 giv. */
4090 else if (INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn))
4091 auto_inc_opt = -1;
4092 else
4093 auto_inc_opt = 1;
4095 #ifdef HAVE_cc0
4097 rtx prev;
4099 /* We can't put an insn immediately after one setting
4100 cc0, or immediately before one using cc0. */
4101 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4102 || (auto_inc_opt == -1
4103 && (prev = prev_nonnote_insn (v->insn)) != 0
4104 && GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4105 && sets_cc0_p (PATTERN (prev))))
4106 auto_inc_opt = 0;
4108 #endif
4110 if (auto_inc_opt)
4111 v->auto_inc_opt = 1;
4113 #endif
4115 /* For each place where the biv is incremented, add an insn
4116 to increment the new, reduced reg for the giv. */
4117 for (tv = bl->biv; tv; tv = tv->next_iv)
4119 rtx insert_before;
4121 if (! auto_inc_opt)
4122 insert_before = tv->insn;
4123 else if (auto_inc_opt == 1)
4124 insert_before = NEXT_INSN (v->insn);
4125 else
4126 insert_before = v->insn;
4128 if (tv->mult_val == const1_rtx)
4129 emit_iv_add_mult (tv->add_val, v->mult_val,
4130 v->new_reg, v->new_reg, insert_before);
4131 else /* tv->mult_val == const0_rtx */
4132 /* A multiply is acceptable here
4133 since this is presumed to be seldom executed. */
4134 emit_iv_add_mult (tv->add_val, v->mult_val,
4135 v->add_val, v->new_reg, insert_before);
4138 /* Add code at loop start to initialize giv's reduced reg. */
4140 emit_iv_add_mult (bl->initial_value, v->mult_val,
4141 v->add_val, v->new_reg, loop_start);
4145 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4146 as not reduced.
4148 For each giv register that can be reduced now: if replaceable,
4149 substitute reduced reg wherever the old giv occurs;
4150 else add new move insn "giv_reg = reduced_reg".
4152 Also check for givs whose first use is their definition and whose
4153 last use is the definition of another giv. If so, it is likely
4154 dead and should not be used to eliminate a biv. */
4155 for (v = bl->giv; v; v = v->next_iv)
4157 if (v->same && v->same->ignore)
4158 v->ignore = 1;
4160 if (v->ignore)
4161 continue;
4163 if (v->giv_type == DEST_REG
4164 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4166 struct induction *v1;
4168 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4169 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4170 v->maybe_dead = 1;
4173 /* Update expression if this was combined, in case other giv was
4174 replaced. */
4175 if (v->same)
4176 v->new_reg = replace_rtx (v->new_reg,
4177 v->same->dest_reg, v->same->new_reg);
4179 if (v->giv_type == DEST_ADDR)
4180 /* Store reduced reg as the address in the memref where we found
4181 this giv. */
4182 validate_change (v->insn, v->location, v->new_reg, 0);
4183 else if (v->replaceable)
4185 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4187 #if 0
4188 /* I can no longer duplicate the original problem. Perhaps
4189 this is unnecessary now? */
4191 /* Replaceable; it isn't strictly necessary to delete the old
4192 insn and emit a new one, because v->dest_reg is now dead.
4194 However, especially when unrolling loops, the special
4195 handling for (set REG0 REG1) in the second cse pass may
4196 make v->dest_reg live again. To avoid this problem, emit
4197 an insn to set the original giv reg from the reduced giv.
4198 We can not delete the original insn, since it may be part
4199 of a LIBCALL, and the code in flow that eliminates dead
4200 libcalls will fail if it is deleted. */
4201 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4202 v->insn);
4203 #endif
4205 else
4207 /* Not replaceable; emit an insn to set the original giv reg from
4208 the reduced giv, same as above. */
4209 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4210 v->insn);
4213 /* When a loop is reversed, givs which depend on the reversed
4214 biv, and which are live outside the loop, must be set to their
4215 correct final value. This insn is only needed if the giv is
4216 not replaceable. The correct final value is the same as the
4217 value that the giv starts the reversed loop with. */
4218 if (bl->reversed && ! v->replaceable)
4219 emit_iv_add_mult (bl->initial_value, v->mult_val,
4220 v->add_val, v->dest_reg, end_insert_before);
4221 else if (v->final_value)
4223 rtx insert_before;
4225 /* If the loop has multiple exits, emit the insn before the
4226 loop to ensure that it will always be executed no matter
4227 how the loop exits. Otherwise, emit the insn after the loop,
4228 since this is slightly more efficient. */
4229 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4230 insert_before = loop_start;
4231 else
4232 insert_before = end_insert_before;
4233 emit_insn_before (gen_move_insn (v->dest_reg, v->final_value),
4234 insert_before);
4236 #if 0
4237 /* If the insn to set the final value of the giv was emitted
4238 before the loop, then we must delete the insn inside the loop
4239 that sets it. If this is a LIBCALL, then we must delete
4240 every insn in the libcall. Note, however, that
4241 final_giv_value will only succeed when there are multiple
4242 exits if the giv is dead at each exit, hence it does not
4243 matter that the original insn remains because it is dead
4244 anyways. */
4245 /* Delete the insn inside the loop that sets the giv since
4246 the giv is now set before (or after) the loop. */
4247 delete_insn (v->insn);
4248 #endif
4251 if (loop_dump_stream)
4253 fprintf (loop_dump_stream, "giv at %d reduced to ",
4254 INSN_UID (v->insn));
4255 print_rtl (loop_dump_stream, v->new_reg);
4256 fprintf (loop_dump_stream, "\n");
4260 /* All the givs based on the biv bl have been reduced if they
4261 merit it. */
4263 /* For each giv not marked as maybe dead that has been combined with a
4264 second giv, clear any "maybe dead" mark on that second giv.
4265 v->new_reg will either be or refer to the register of the giv it
4266 combined with.
4268 Doing this clearing avoids problems in biv elimination where a
4269 giv's new_reg is a complex value that can't be put in the insn but
4270 the giv combined with (with a reg as new_reg) is marked maybe_dead.
4271 Since the register will be used in either case, we'd prefer it be
4272 used from the simpler giv. */
4274 for (v = bl->giv; v; v = v->next_iv)
4275 if (! v->maybe_dead && v->same)
4276 v->same->maybe_dead = 0;
4278 /* Try to eliminate the biv, if it is a candidate.
4279 This won't work if ! all_reduced,
4280 since the givs we planned to use might not have been reduced.
4282 We have to be careful that we didn't initially think we could eliminate
4283 this biv because of a giv that we now think may be dead and shouldn't
4284 be used as a biv replacement.
4286 Also, there is the possibility that we may have a giv that looks
4287 like it can be used to eliminate a biv, but the resulting insn
4288 isn't valid. This can happen, for example, on the 88k, where a
4289 JUMP_INSN can compare a register only with zero. Attempts to
4290 replace it with a compare with a constant will fail.
4292 Note that in cases where this call fails, we may have replaced some
4293 of the occurrences of the biv with a giv, but no harm was done in
4294 doing so in the rare cases where it can occur. */
4296 if (all_reduced == 1 && bl->eliminable
4297 && maybe_eliminate_biv (bl, loop_start, end, 1,
4298 threshold, insn_count))
4301 /* ?? If we created a new test to bypass the loop entirely,
4302 or otherwise drop straight in, based on this test, then
4303 we might want to rewrite it also. This way some later
4304 pass has more hope of removing the initialization of this
4305 biv entirely. */
4307 /* If final_value != 0, then the biv may be used after loop end
4308 and we must emit an insn to set it just in case.
4310 Reversed bivs already have an insn after the loop setting their
4311 value, so we don't need another one. We can't calculate the
4312 proper final value for such a biv here anyways. */
4313 if (final_value != 0 && ! bl->reversed)
4315 rtx insert_before;
4317 /* If the loop has multiple exits, emit the insn before the
4318 loop to ensure that it will always be executed no matter
4319 how the loop exits. Otherwise, emit the insn after the
4320 loop, since this is slightly more efficient. */
4321 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4322 insert_before = loop_start;
4323 else
4324 insert_before = end_insert_before;
4326 emit_insn_before (gen_move_insn (bl->biv->dest_reg, final_value),
4327 end_insert_before);
4330 #if 0
4331 /* Delete all of the instructions inside the loop which set
4332 the biv, as they are all dead. If is safe to delete them,
4333 because an insn setting a biv will never be part of a libcall. */
4334 /* However, deleting them will invalidate the regno_last_uid info,
4335 so keeping them around is more convenient. Final_biv_value
4336 will only succeed when there are multiple exits if the biv
4337 is dead at each exit, hence it does not matter that the original
4338 insn remains, because it is dead anyways. */
4339 for (v = bl->biv; v; v = v->next_iv)
4340 delete_insn (v->insn);
4341 #endif
4343 if (loop_dump_stream)
4344 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
4345 bl->regno);
4349 /* Go through all the instructions in the loop, making all the
4350 register substitutions scheduled in REG_MAP. */
4352 for (p = loop_start; p != end; p = NEXT_INSN (p))
4353 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4354 || GET_CODE (p) == CALL_INSN)
4356 replace_regs (PATTERN (p), reg_map, max_reg_before_loop, 0);
4357 replace_regs (REG_NOTES (p), reg_map, max_reg_before_loop, 0);
4358 INSN_CODE (p) = -1;
4361 /* Unroll loops from within strength reduction so that we can use the
4362 induction variable information that strength_reduce has already
4363 collected. */
4365 if (flag_unroll_loops)
4366 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 1);
4368 #ifdef HAIFA
4369 /* instrument the loop with bct insn */
4370 #ifdef HAVE_decrement_and_branch_on_count
4371 if (HAVE_decrement_and_branch_on_count)
4372 insert_bct (loop_start, loop_end);
4373 #endif
4374 #endif /* HAIFA */
4376 if (loop_dump_stream)
4377 fprintf (loop_dump_stream, "\n");
4380 /* Return 1 if X is a valid source for an initial value (or as value being
4381 compared against in an initial test).
4383 X must be either a register or constant and must not be clobbered between
4384 the current insn and the start of the loop.
4386 INSN is the insn containing X. */
4388 static int
4389 valid_initial_value_p (x, insn, call_seen, loop_start)
4390 rtx x;
4391 rtx insn;
4392 int call_seen;
4393 rtx loop_start;
4395 if (CONSTANT_P (x))
4396 return 1;
4398 /* Only consider pseudos we know about initialized in insns whose luids
4399 we know. */
4400 if (GET_CODE (x) != REG
4401 || REGNO (x) >= max_reg_before_loop)
4402 return 0;
4404 /* Don't use call-clobbered registers across a call which clobbers it. On
4405 some machines, don't use any hard registers at all. */
4406 if (REGNO (x) < FIRST_PSEUDO_REGISTER
4407 && (SMALL_REGISTER_CLASSES
4408 || (call_used_regs[REGNO (x)] && call_seen)))
4409 return 0;
4411 /* Don't use registers that have been clobbered before the start of the
4412 loop. */
4413 if (reg_set_between_p (x, insn, loop_start))
4414 return 0;
4416 return 1;
4419 /* Scan X for memory refs and check each memory address
4420 as a possible giv. INSN is the insn whose pattern X comes from.
4421 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
4422 every loop iteration. */
4424 static void
4425 find_mem_givs (x, insn, not_every_iteration, loop_start, loop_end)
4426 rtx x;
4427 rtx insn;
4428 int not_every_iteration;
4429 rtx loop_start, loop_end;
4431 register int i, j;
4432 register enum rtx_code code;
4433 register char *fmt;
4435 if (x == 0)
4436 return;
4438 code = GET_CODE (x);
4439 switch (code)
4441 case REG:
4442 case CONST_INT:
4443 case CONST:
4444 case CONST_DOUBLE:
4445 case SYMBOL_REF:
4446 case LABEL_REF:
4447 case PC:
4448 case CC0:
4449 case ADDR_VEC:
4450 case ADDR_DIFF_VEC:
4451 case USE:
4452 case CLOBBER:
4453 return;
4455 case MEM:
4457 rtx src_reg;
4458 rtx add_val;
4459 rtx mult_val;
4460 int benefit;
4462 benefit = general_induction_var (XEXP (x, 0),
4463 &src_reg, &add_val, &mult_val);
4465 /* Don't make a DEST_ADDR giv with mult_val == 1 && add_val == 0.
4466 Such a giv isn't useful. */
4467 if (benefit > 0 && (mult_val != const1_rtx || add_val != const0_rtx))
4469 /* Found one; record it. */
4470 struct induction *v
4471 = (struct induction *) oballoc (sizeof (struct induction));
4473 record_giv (v, insn, src_reg, addr_placeholder, mult_val,
4474 add_val, benefit, DEST_ADDR, not_every_iteration,
4475 &XEXP (x, 0), loop_start, loop_end);
4477 v->mem_mode = GET_MODE (x);
4480 return;
4482 default:
4483 break;
4486 /* Recursively scan the subexpressions for other mem refs. */
4488 fmt = GET_RTX_FORMAT (code);
4489 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4490 if (fmt[i] == 'e')
4491 find_mem_givs (XEXP (x, i), insn, not_every_iteration, loop_start,
4492 loop_end);
4493 else if (fmt[i] == 'E')
4494 for (j = 0; j < XVECLEN (x, i); j++)
4495 find_mem_givs (XVECEXP (x, i, j), insn, not_every_iteration,
4496 loop_start, loop_end);
4499 /* Fill in the data about one biv update.
4500 V is the `struct induction' in which we record the biv. (It is
4501 allocated by the caller, with alloca.)
4502 INSN is the insn that sets it.
4503 DEST_REG is the biv's reg.
4505 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
4506 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
4507 being set to INC_VAL.
4509 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
4510 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
4511 can be executed more than once per iteration. If MAYBE_MULTIPLE
4512 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
4513 executed exactly once per iteration. */
4515 static void
4516 record_biv (v, insn, dest_reg, inc_val, mult_val,
4517 not_every_iteration, maybe_multiple)
4518 struct induction *v;
4519 rtx insn;
4520 rtx dest_reg;
4521 rtx inc_val;
4522 rtx mult_val;
4523 int not_every_iteration;
4524 int maybe_multiple;
4526 struct iv_class *bl;
4528 v->insn = insn;
4529 v->src_reg = dest_reg;
4530 v->dest_reg = dest_reg;
4531 v->mult_val = mult_val;
4532 v->add_val = inc_val;
4533 v->mode = GET_MODE (dest_reg);
4534 v->always_computable = ! not_every_iteration;
4535 v->always_executed = ! not_every_iteration;
4536 v->maybe_multiple = maybe_multiple;
4538 /* Add this to the reg's iv_class, creating a class
4539 if this is the first incrementation of the reg. */
4541 bl = reg_biv_class[REGNO (dest_reg)];
4542 if (bl == 0)
4544 /* Create and initialize new iv_class. */
4546 bl = (struct iv_class *) oballoc (sizeof (struct iv_class));
4548 bl->regno = REGNO (dest_reg);
4549 bl->biv = 0;
4550 bl->giv = 0;
4551 bl->biv_count = 0;
4552 bl->giv_count = 0;
4554 /* Set initial value to the reg itself. */
4555 bl->initial_value = dest_reg;
4556 /* We haven't seen the initializing insn yet */
4557 bl->init_insn = 0;
4558 bl->init_set = 0;
4559 bl->initial_test = 0;
4560 bl->incremented = 0;
4561 bl->eliminable = 0;
4562 bl->nonneg = 0;
4563 bl->reversed = 0;
4564 bl->total_benefit = 0;
4566 /* Add this class to loop_iv_list. */
4567 bl->next = loop_iv_list;
4568 loop_iv_list = bl;
4570 /* Put it in the array of biv register classes. */
4571 reg_biv_class[REGNO (dest_reg)] = bl;
4574 /* Update IV_CLASS entry for this biv. */
4575 v->next_iv = bl->biv;
4576 bl->biv = v;
4577 bl->biv_count++;
4578 if (mult_val == const1_rtx)
4579 bl->incremented = 1;
4581 if (loop_dump_stream)
4583 fprintf (loop_dump_stream,
4584 "Insn %d: possible biv, reg %d,",
4585 INSN_UID (insn), REGNO (dest_reg));
4586 if (GET_CODE (inc_val) == CONST_INT)
4587 fprintf (loop_dump_stream, " const = %d\n",
4588 INTVAL (inc_val));
4589 else
4591 fprintf (loop_dump_stream, " const = ");
4592 print_rtl (loop_dump_stream, inc_val);
4593 fprintf (loop_dump_stream, "\n");
4598 /* Fill in the data about one giv.
4599 V is the `struct induction' in which we record the giv. (It is
4600 allocated by the caller, with alloca.)
4601 INSN is the insn that sets it.
4602 BENEFIT estimates the savings from deleting this insn.
4603 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
4604 into a register or is used as a memory address.
4606 SRC_REG is the biv reg which the giv is computed from.
4607 DEST_REG is the giv's reg (if the giv is stored in a reg).
4608 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
4609 LOCATION points to the place where this giv's value appears in INSN. */
4611 static void
4612 record_giv (v, insn, src_reg, dest_reg, mult_val, add_val, benefit,
4613 type, not_every_iteration, location, loop_start, loop_end)
4614 struct induction *v;
4615 rtx insn;
4616 rtx src_reg;
4617 rtx dest_reg;
4618 rtx mult_val, add_val;
4619 int benefit;
4620 enum g_types type;
4621 int not_every_iteration;
4622 rtx *location;
4623 rtx loop_start, loop_end;
4625 struct induction *b;
4626 struct iv_class *bl;
4627 rtx set = single_set (insn);
4628 rtx p;
4630 v->insn = insn;
4631 v->src_reg = src_reg;
4632 v->giv_type = type;
4633 v->dest_reg = dest_reg;
4634 v->mult_val = mult_val;
4635 v->add_val = add_val;
4636 v->benefit = benefit;
4637 v->location = location;
4638 v->cant_derive = 0;
4639 v->combined_with = 0;
4640 v->maybe_multiple = 0;
4641 v->maybe_dead = 0;
4642 v->derive_adjustment = 0;
4643 v->same = 0;
4644 v->ignore = 0;
4645 v->new_reg = 0;
4646 v->final_value = 0;
4647 v->same_insn = 0;
4648 v->auto_inc_opt = 0;
4649 v->unrolled = 0;
4650 v->shared = 0;
4652 /* The v->always_computable field is used in update_giv_derive, to
4653 determine whether a giv can be used to derive another giv. For a
4654 DEST_REG giv, INSN computes a new value for the giv, so its value
4655 isn't computable if INSN insn't executed every iteration.
4656 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
4657 it does not compute a new value. Hence the value is always computable
4658 regardless of whether INSN is executed each iteration. */
4660 if (type == DEST_ADDR)
4661 v->always_computable = 1;
4662 else
4663 v->always_computable = ! not_every_iteration;
4665 v->always_executed = ! not_every_iteration;
4667 if (type == DEST_ADDR)
4669 v->mode = GET_MODE (*location);
4670 v->lifetime = 1;
4671 v->times_used = 1;
4673 else /* type == DEST_REG */
4675 v->mode = GET_MODE (SET_DEST (set));
4677 v->lifetime = (uid_luid[REGNO_LAST_UID (REGNO (dest_reg))]
4678 - uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))]);
4680 v->times_used = n_times_used[REGNO (dest_reg)];
4682 /* If the lifetime is zero, it means that this register is
4683 really a dead store. So mark this as a giv that can be
4684 ignored. This will not prevent the biv from being eliminated. */
4685 if (v->lifetime == 0)
4686 v->ignore = 1;
4688 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
4689 reg_iv_info[REGNO (dest_reg)] = v;
4692 /* Add the giv to the class of givs computed from one biv. */
4694 bl = reg_biv_class[REGNO (src_reg)];
4695 if (bl)
4697 v->next_iv = bl->giv;
4698 bl->giv = v;
4699 /* Don't count DEST_ADDR. This is supposed to count the number of
4700 insns that calculate givs. */
4701 if (type == DEST_REG)
4702 bl->giv_count++;
4703 bl->total_benefit += benefit;
4705 else
4706 /* Fatal error, biv missing for this giv? */
4707 abort ();
4709 if (type == DEST_ADDR)
4710 v->replaceable = 1;
4711 else
4713 /* The giv can be replaced outright by the reduced register only if all
4714 of the following conditions are true:
4715 - the insn that sets the giv is always executed on any iteration
4716 on which the giv is used at all
4717 (there are two ways to deduce this:
4718 either the insn is executed on every iteration,
4719 or all uses follow that insn in the same basic block),
4720 - the giv is not used outside the loop
4721 - no assignments to the biv occur during the giv's lifetime. */
4723 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
4724 /* Previous line always fails if INSN was moved by loop opt. */
4725 && uid_luid[REGNO_LAST_UID (REGNO (dest_reg))] < INSN_LUID (loop_end)
4726 && (! not_every_iteration
4727 || last_use_this_basic_block (dest_reg, insn)))
4729 /* Now check that there are no assignments to the biv within the
4730 giv's lifetime. This requires two separate checks. */
4732 /* Check each biv update, and fail if any are between the first
4733 and last use of the giv.
4735 If this loop contains an inner loop that was unrolled, then
4736 the insn modifying the biv may have been emitted by the loop
4737 unrolling code, and hence does not have a valid luid. Just
4738 mark the biv as not replaceable in this case. It is not very
4739 useful as a biv, because it is used in two different loops.
4740 It is very unlikely that we would be able to optimize the giv
4741 using this biv anyways. */
4743 v->replaceable = 1;
4744 for (b = bl->biv; b; b = b->next_iv)
4746 if (INSN_UID (b->insn) >= max_uid_for_loop
4747 || ((uid_luid[INSN_UID (b->insn)]
4748 >= uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))])
4749 && (uid_luid[INSN_UID (b->insn)]
4750 <= uid_luid[REGNO_LAST_UID (REGNO (dest_reg))])))
4752 v->replaceable = 0;
4753 v->not_replaceable = 1;
4754 break;
4758 /* If there are any backwards branches that go from after the
4759 biv update to before it, then this giv is not replaceable. */
4760 if (v->replaceable)
4761 for (b = bl->biv; b; b = b->next_iv)
4762 if (back_branch_in_range_p (b->insn, loop_start, loop_end))
4764 v->replaceable = 0;
4765 v->not_replaceable = 1;
4766 break;
4769 else
4771 /* May still be replaceable, we don't have enough info here to
4772 decide. */
4773 v->replaceable = 0;
4774 v->not_replaceable = 0;
4778 if (loop_dump_stream)
4780 if (type == DEST_REG)
4781 fprintf (loop_dump_stream, "Insn %d: giv reg %d",
4782 INSN_UID (insn), REGNO (dest_reg));
4783 else
4784 fprintf (loop_dump_stream, "Insn %d: dest address",
4785 INSN_UID (insn));
4787 fprintf (loop_dump_stream, " src reg %d benefit %d",
4788 REGNO (src_reg), v->benefit);
4789 fprintf (loop_dump_stream, " used %d lifetime %d",
4790 v->times_used, v->lifetime);
4792 if (v->replaceable)
4793 fprintf (loop_dump_stream, " replaceable");
4795 if (GET_CODE (mult_val) == CONST_INT)
4796 fprintf (loop_dump_stream, " mult %d",
4797 INTVAL (mult_val));
4798 else
4800 fprintf (loop_dump_stream, " mult ");
4801 print_rtl (loop_dump_stream, mult_val);
4804 if (GET_CODE (add_val) == CONST_INT)
4805 fprintf (loop_dump_stream, " add %d",
4806 INTVAL (add_val));
4807 else
4809 fprintf (loop_dump_stream, " add ");
4810 print_rtl (loop_dump_stream, add_val);
4814 if (loop_dump_stream)
4815 fprintf (loop_dump_stream, "\n");
4820 /* All this does is determine whether a giv can be made replaceable because
4821 its final value can be calculated. This code can not be part of record_giv
4822 above, because final_giv_value requires that the number of loop iterations
4823 be known, and that can not be accurately calculated until after all givs
4824 have been identified. */
4826 static void
4827 check_final_value (v, loop_start, loop_end)
4828 struct induction *v;
4829 rtx loop_start, loop_end;
4831 struct iv_class *bl;
4832 rtx final_value = 0;
4834 bl = reg_biv_class[REGNO (v->src_reg)];
4836 /* DEST_ADDR givs will never reach here, because they are always marked
4837 replaceable above in record_giv. */
4839 /* The giv can be replaced outright by the reduced register only if all
4840 of the following conditions are true:
4841 - the insn that sets the giv is always executed on any iteration
4842 on which the giv is used at all
4843 (there are two ways to deduce this:
4844 either the insn is executed on every iteration,
4845 or all uses follow that insn in the same basic block),
4846 - its final value can be calculated (this condition is different
4847 than the one above in record_giv)
4848 - no assignments to the biv occur during the giv's lifetime. */
4850 #if 0
4851 /* This is only called now when replaceable is known to be false. */
4852 /* Clear replaceable, so that it won't confuse final_giv_value. */
4853 v->replaceable = 0;
4854 #endif
4856 if ((final_value = final_giv_value (v, loop_start, loop_end))
4857 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
4859 int biv_increment_seen = 0;
4860 rtx p = v->insn;
4861 rtx last_giv_use;
4863 v->replaceable = 1;
4865 /* When trying to determine whether or not a biv increment occurs
4866 during the lifetime of the giv, we can ignore uses of the variable
4867 outside the loop because final_value is true. Hence we can not
4868 use regno_last_uid and regno_first_uid as above in record_giv. */
4870 /* Search the loop to determine whether any assignments to the
4871 biv occur during the giv's lifetime. Start with the insn
4872 that sets the giv, and search around the loop until we come
4873 back to that insn again.
4875 Also fail if there is a jump within the giv's lifetime that jumps
4876 to somewhere outside the lifetime but still within the loop. This
4877 catches spaghetti code where the execution order is not linear, and
4878 hence the above test fails. Here we assume that the giv lifetime
4879 does not extend from one iteration of the loop to the next, so as
4880 to make the test easier. Since the lifetime isn't known yet,
4881 this requires two loops. See also record_giv above. */
4883 last_giv_use = v->insn;
4885 while (1)
4887 p = NEXT_INSN (p);
4888 if (p == loop_end)
4889 p = NEXT_INSN (loop_start);
4890 if (p == v->insn)
4891 break;
4893 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4894 || GET_CODE (p) == CALL_INSN)
4896 if (biv_increment_seen)
4898 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4900 v->replaceable = 0;
4901 v->not_replaceable = 1;
4902 break;
4905 else if (reg_set_p (v->src_reg, PATTERN (p)))
4906 biv_increment_seen = 1;
4907 else if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4908 last_giv_use = p;
4912 /* Now that the lifetime of the giv is known, check for branches
4913 from within the lifetime to outside the lifetime if it is still
4914 replaceable. */
4916 if (v->replaceable)
4918 p = v->insn;
4919 while (1)
4921 p = NEXT_INSN (p);
4922 if (p == loop_end)
4923 p = NEXT_INSN (loop_start);
4924 if (p == last_giv_use)
4925 break;
4927 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
4928 && LABEL_NAME (JUMP_LABEL (p))
4929 && ((INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop)
4930 || (INSN_UID (v->insn) >= max_uid_for_loop)
4931 || (INSN_UID (last_giv_use) >= max_uid_for_loop)
4932 || (INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (v->insn)
4933 && INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop_start))
4934 || (INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (last_giv_use)
4935 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop_end))))
4937 v->replaceable = 0;
4938 v->not_replaceable = 1;
4940 if (loop_dump_stream)
4941 fprintf (loop_dump_stream,
4942 "Found branch outside giv lifetime.\n");
4944 break;
4949 /* If it is replaceable, then save the final value. */
4950 if (v->replaceable)
4951 v->final_value = final_value;
4954 if (loop_dump_stream && v->replaceable)
4955 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
4956 INSN_UID (v->insn), REGNO (v->dest_reg));
4959 /* Update the status of whether a giv can derive other givs.
4961 We need to do something special if there is or may be an update to the biv
4962 between the time the giv is defined and the time it is used to derive
4963 another giv.
4965 In addition, a giv that is only conditionally set is not allowed to
4966 derive another giv once a label has been passed.
4968 The cases we look at are when a label or an update to a biv is passed. */
4970 static void
4971 update_giv_derive (p)
4972 rtx p;
4974 struct iv_class *bl;
4975 struct induction *biv, *giv;
4976 rtx tem;
4977 int dummy;
4979 /* Search all IV classes, then all bivs, and finally all givs.
4981 There are three cases we are concerned with. First we have the situation
4982 of a giv that is only updated conditionally. In that case, it may not
4983 derive any givs after a label is passed.
4985 The second case is when a biv update occurs, or may occur, after the
4986 definition of a giv. For certain biv updates (see below) that are
4987 known to occur between the giv definition and use, we can adjust the
4988 giv definition. For others, or when the biv update is conditional,
4989 we must prevent the giv from deriving any other givs. There are two
4990 sub-cases within this case.
4992 If this is a label, we are concerned with any biv update that is done
4993 conditionally, since it may be done after the giv is defined followed by
4994 a branch here (actually, we need to pass both a jump and a label, but
4995 this extra tracking doesn't seem worth it).
4997 If this is a jump, we are concerned about any biv update that may be
4998 executed multiple times. We are actually only concerned about
4999 backward jumps, but it is probably not worth performing the test
5000 on the jump again here.
5002 If this is a biv update, we must adjust the giv status to show that a
5003 subsequent biv update was performed. If this adjustment cannot be done,
5004 the giv cannot derive further givs. */
5006 for (bl = loop_iv_list; bl; bl = bl->next)
5007 for (biv = bl->biv; biv; biv = biv->next_iv)
5008 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
5009 || biv->insn == p)
5011 for (giv = bl->giv; giv; giv = giv->next_iv)
5013 /* If cant_derive is already true, there is no point in
5014 checking all of these conditions again. */
5015 if (giv->cant_derive)
5016 continue;
5018 /* If this giv is conditionally set and we have passed a label,
5019 it cannot derive anything. */
5020 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
5021 giv->cant_derive = 1;
5023 /* Skip givs that have mult_val == 0, since
5024 they are really invariants. Also skip those that are
5025 replaceable, since we know their lifetime doesn't contain
5026 any biv update. */
5027 else if (giv->mult_val == const0_rtx || giv->replaceable)
5028 continue;
5030 /* The only way we can allow this giv to derive another
5031 is if this is a biv increment and we can form the product
5032 of biv->add_val and giv->mult_val. In this case, we will
5033 be able to compute a compensation. */
5034 else if (biv->insn == p)
5036 tem = 0;
5038 if (biv->mult_val == const1_rtx)
5039 tem = simplify_giv_expr (gen_rtx (MULT, giv->mode,
5040 biv->add_val,
5041 giv->mult_val),
5042 &dummy);
5044 if (tem && giv->derive_adjustment)
5045 tem = simplify_giv_expr (gen_rtx (PLUS, giv->mode, tem,
5046 giv->derive_adjustment),
5047 &dummy);
5048 if (tem)
5049 giv->derive_adjustment = tem;
5050 else
5051 giv->cant_derive = 1;
5053 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5054 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
5055 giv->cant_derive = 1;
5060 /* Check whether an insn is an increment legitimate for a basic induction var.
5061 X is the source of insn P, or a part of it.
5062 MODE is the mode in which X should be interpreted.
5064 DEST_REG is the putative biv, also the destination of the insn.
5065 We accept patterns of these forms:
5066 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5067 REG = INVARIANT + REG
5069 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5070 and store the additive term into *INC_VAL.
5072 If X is an assignment of an invariant into DEST_REG, we set
5073 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5075 We also want to detect a BIV when it corresponds to a variable
5076 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5077 of the variable may be a PLUS that adds a SUBREG of that variable to
5078 an invariant and then sign- or zero-extends the result of the PLUS
5079 into the variable.
5081 Most GIVs in such cases will be in the promoted mode, since that is the
5082 probably the natural computation mode (and almost certainly the mode
5083 used for addresses) on the machine. So we view the pseudo-reg containing
5084 the variable as the BIV, as if it were simply incremented.
5086 Note that treating the entire pseudo as a BIV will result in making
5087 simple increments to any GIVs based on it. However, if the variable
5088 overflows in its declared mode but not its promoted mode, the result will
5089 be incorrect. This is acceptable if the variable is signed, since
5090 overflows in such cases are undefined, but not if it is unsigned, since
5091 those overflows are defined. So we only check for SIGN_EXTEND and
5092 not ZERO_EXTEND.
5094 If we cannot find a biv, we return 0. */
5096 static int
5097 basic_induction_var (x, mode, dest_reg, p, inc_val, mult_val)
5098 register rtx x;
5099 enum machine_mode mode;
5100 rtx p;
5101 rtx dest_reg;
5102 rtx *inc_val;
5103 rtx *mult_val;
5105 register enum rtx_code code;
5106 rtx arg;
5107 rtx insn, set = 0;
5109 code = GET_CODE (x);
5110 switch (code)
5112 case PLUS:
5113 if (XEXP (x, 0) == dest_reg
5114 || (GET_CODE (XEXP (x, 0)) == SUBREG
5115 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5116 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
5117 arg = XEXP (x, 1);
5118 else if (XEXP (x, 1) == dest_reg
5119 || (GET_CODE (XEXP (x, 1)) == SUBREG
5120 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5121 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
5122 arg = XEXP (x, 0);
5123 else
5124 return 0;
5126 if (invariant_p (arg) != 1)
5127 return 0;
5129 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
5130 *mult_val = const1_rtx;
5131 return 1;
5133 case SUBREG:
5134 /* If this is a SUBREG for a promoted variable, check the inner
5135 value. */
5136 if (SUBREG_PROMOTED_VAR_P (x))
5137 return basic_induction_var (SUBREG_REG (x), GET_MODE (SUBREG_REG (x)),
5138 dest_reg, p, inc_val, mult_val);
5139 return 0;
5141 case REG:
5142 /* If this register is assigned in the previous insn, look at its
5143 source, but don't go outside the loop or past a label. */
5145 for (insn = PREV_INSN (p);
5146 (insn && GET_CODE (insn) == NOTE
5147 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5148 insn = PREV_INSN (insn))
5151 if (insn)
5152 set = single_set (insn);
5154 if (set != 0
5155 && (SET_DEST (set) == x
5156 || (GET_CODE (SET_DEST (set)) == SUBREG
5157 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
5158 <= UNITS_PER_WORD)
5159 && SUBREG_REG (SET_DEST (set)) == x)))
5160 return basic_induction_var (SET_SRC (set),
5161 (GET_MODE (SET_SRC (set)) == VOIDmode
5162 ? GET_MODE (x)
5163 : GET_MODE (SET_SRC (set))),
5164 dest_reg, insn,
5165 inc_val, mult_val);
5166 /* ... fall through ... */
5168 /* Can accept constant setting of biv only when inside inner most loop.
5169 Otherwise, a biv of an inner loop may be incorrectly recognized
5170 as a biv of the outer loop,
5171 causing code to be moved INTO the inner loop. */
5172 case MEM:
5173 if (invariant_p (x) != 1)
5174 return 0;
5175 case CONST_INT:
5176 case SYMBOL_REF:
5177 case CONST:
5178 if (loops_enclosed == 1)
5180 /* Possible bug here? Perhaps we don't know the mode of X. */
5181 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
5182 *mult_val = const0_rtx;
5183 return 1;
5185 else
5186 return 0;
5188 case SIGN_EXTEND:
5189 return basic_induction_var (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5190 dest_reg, p, inc_val, mult_val);
5191 case ASHIFTRT:
5192 /* Similar, since this can be a sign extension. */
5193 for (insn = PREV_INSN (p);
5194 (insn && GET_CODE (insn) == NOTE
5195 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5196 insn = PREV_INSN (insn))
5199 if (insn)
5200 set = single_set (insn);
5202 if (set && SET_DEST (set) == XEXP (x, 0)
5203 && GET_CODE (XEXP (x, 1)) == CONST_INT
5204 && INTVAL (XEXP (x, 1)) >= 0
5205 && GET_CODE (SET_SRC (set)) == ASHIFT
5206 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
5207 return basic_induction_var (XEXP (SET_SRC (set), 0),
5208 GET_MODE (XEXP (x, 0)),
5209 dest_reg, insn, inc_val, mult_val);
5210 return 0;
5212 default:
5213 return 0;
5217 /* A general induction variable (giv) is any quantity that is a linear
5218 function of a basic induction variable,
5219 i.e. giv = biv * mult_val + add_val.
5220 The coefficients can be any loop invariant quantity.
5221 A giv need not be computed directly from the biv;
5222 it can be computed by way of other givs. */
5224 /* Determine whether X computes a giv.
5225 If it does, return a nonzero value
5226 which is the benefit from eliminating the computation of X;
5227 set *SRC_REG to the register of the biv that it is computed from;
5228 set *ADD_VAL and *MULT_VAL to the coefficients,
5229 such that the value of X is biv * mult + add; */
5231 static int
5232 general_induction_var (x, src_reg, add_val, mult_val)
5233 rtx x;
5234 rtx *src_reg;
5235 rtx *add_val;
5236 rtx *mult_val;
5238 rtx orig_x = x;
5239 int benefit = 0;
5240 char *storage;
5242 /* If this is an invariant, forget it, it isn't a giv. */
5243 if (invariant_p (x) == 1)
5244 return 0;
5246 /* See if the expression could be a giv and get its form.
5247 Mark our place on the obstack in case we don't find a giv. */
5248 storage = (char *) oballoc (0);
5249 x = simplify_giv_expr (x, &benefit);
5250 if (x == 0)
5252 obfree (storage);
5253 return 0;
5256 switch (GET_CODE (x))
5258 case USE:
5259 case CONST_INT:
5260 /* Since this is now an invariant and wasn't before, it must be a giv
5261 with MULT_VAL == 0. It doesn't matter which BIV we associate this
5262 with. */
5263 *src_reg = loop_iv_list->biv->dest_reg;
5264 *mult_val = const0_rtx;
5265 *add_val = x;
5266 break;
5268 case REG:
5269 /* This is equivalent to a BIV. */
5270 *src_reg = x;
5271 *mult_val = const1_rtx;
5272 *add_val = const0_rtx;
5273 break;
5275 case PLUS:
5276 /* Either (plus (biv) (invar)) or
5277 (plus (mult (biv) (invar_1)) (invar_2)). */
5278 if (GET_CODE (XEXP (x, 0)) == MULT)
5280 *src_reg = XEXP (XEXP (x, 0), 0);
5281 *mult_val = XEXP (XEXP (x, 0), 1);
5283 else
5285 *src_reg = XEXP (x, 0);
5286 *mult_val = const1_rtx;
5288 *add_val = XEXP (x, 1);
5289 break;
5291 case MULT:
5292 /* ADD_VAL is zero. */
5293 *src_reg = XEXP (x, 0);
5294 *mult_val = XEXP (x, 1);
5295 *add_val = const0_rtx;
5296 break;
5298 default:
5299 abort ();
5302 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
5303 unless they are CONST_INT). */
5304 if (GET_CODE (*add_val) == USE)
5305 *add_val = XEXP (*add_val, 0);
5306 if (GET_CODE (*mult_val) == USE)
5307 *mult_val = XEXP (*mult_val, 0);
5309 benefit += rtx_cost (orig_x, SET);
5311 /* Always return some benefit if this is a giv so it will be detected
5312 as such. This allows elimination of bivs that might otherwise
5313 not be eliminated. */
5314 return benefit == 0 ? 1 : benefit;
5317 /* Given an expression, X, try to form it as a linear function of a biv.
5318 We will canonicalize it to be of the form
5319 (plus (mult (BIV) (invar_1))
5320 (invar_2))
5321 with possible degeneracies.
5323 The invariant expressions must each be of a form that can be used as a
5324 machine operand. We surround then with a USE rtx (a hack, but localized
5325 and certainly unambiguous!) if not a CONST_INT for simplicity in this
5326 routine; it is the caller's responsibility to strip them.
5328 If no such canonicalization is possible (i.e., two biv's are used or an
5329 expression that is neither invariant nor a biv or giv), this routine
5330 returns 0.
5332 For a non-zero return, the result will have a code of CONST_INT, USE,
5333 REG (for a BIV), PLUS, or MULT. No other codes will occur.
5335 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
5337 static rtx
5338 simplify_giv_expr (x, benefit)
5339 rtx x;
5340 int *benefit;
5342 enum machine_mode mode = GET_MODE (x);
5343 rtx arg0, arg1;
5344 rtx tem;
5346 /* If this is not an integer mode, or if we cannot do arithmetic in this
5347 mode, this can't be a giv. */
5348 if (mode != VOIDmode
5349 && (GET_MODE_CLASS (mode) != MODE_INT
5350 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
5351 return 0;
5353 switch (GET_CODE (x))
5355 case PLUS:
5356 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5357 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5358 if (arg0 == 0 || arg1 == 0)
5359 return 0;
5361 /* Put constant last, CONST_INT last if both constant. */
5362 if ((GET_CODE (arg0) == USE
5363 || GET_CODE (arg0) == CONST_INT)
5364 && GET_CODE (arg1) != CONST_INT)
5365 tem = arg0, arg0 = arg1, arg1 = tem;
5367 /* Handle addition of zero, then addition of an invariant. */
5368 if (arg1 == const0_rtx)
5369 return arg0;
5370 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
5371 switch (GET_CODE (arg0))
5373 case CONST_INT:
5374 case USE:
5375 /* Both invariant. Only valid if sum is machine operand.
5376 First strip off possible USE on the operands. */
5377 if (GET_CODE (arg0) == USE)
5378 arg0 = XEXP (arg0, 0);
5380 if (GET_CODE (arg1) == USE)
5381 arg1 = XEXP (arg1, 0);
5383 tem = 0;
5384 if (CONSTANT_P (arg0) && GET_CODE (arg1) == CONST_INT)
5386 tem = plus_constant (arg0, INTVAL (arg1));
5387 if (GET_CODE (tem) != CONST_INT)
5388 tem = gen_rtx (USE, mode, tem);
5390 else
5392 /* Adding two invariants must result in an invariant,
5393 so enclose addition operation inside a USE and
5394 return it. */
5395 tem = gen_rtx (USE, mode, gen_rtx (PLUS, mode, arg0, arg1));
5398 return tem;
5400 case REG:
5401 case MULT:
5402 /* biv + invar or mult + invar. Return sum. */
5403 return gen_rtx (PLUS, mode, arg0, arg1);
5405 case PLUS:
5406 /* (a + invar_1) + invar_2. Associate. */
5407 return simplify_giv_expr (gen_rtx (PLUS, mode,
5408 XEXP (arg0, 0),
5409 gen_rtx (PLUS, mode,
5410 XEXP (arg0, 1), arg1)),
5411 benefit);
5413 default:
5414 abort ();
5417 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
5418 MULT to reduce cases. */
5419 if (GET_CODE (arg0) == REG)
5420 arg0 = gen_rtx (MULT, mode, arg0, const1_rtx);
5421 if (GET_CODE (arg1) == REG)
5422 arg1 = gen_rtx (MULT, mode, arg1, const1_rtx);
5424 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
5425 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
5426 Recurse to associate the second PLUS. */
5427 if (GET_CODE (arg1) == MULT)
5428 tem = arg0, arg0 = arg1, arg1 = tem;
5430 if (GET_CODE (arg1) == PLUS)
5431 return simplify_giv_expr (gen_rtx (PLUS, mode,
5432 gen_rtx (PLUS, mode,
5433 arg0, XEXP (arg1, 0)),
5434 XEXP (arg1, 1)),
5435 benefit);
5437 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
5438 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
5439 abort ();
5441 if (XEXP (arg0, 0) != XEXP (arg1, 0))
5442 return 0;
5444 return simplify_giv_expr (gen_rtx (MULT, mode,
5445 XEXP (arg0, 0),
5446 gen_rtx (PLUS, mode,
5447 XEXP (arg0, 1),
5448 XEXP (arg1, 1))),
5449 benefit);
5451 case MINUS:
5452 /* Handle "a - b" as "a + b * (-1)". */
5453 return simplify_giv_expr (gen_rtx (PLUS, mode,
5454 XEXP (x, 0),
5455 gen_rtx (MULT, mode,
5456 XEXP (x, 1), constm1_rtx)),
5457 benefit);
5459 case MULT:
5460 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5461 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5462 if (arg0 == 0 || arg1 == 0)
5463 return 0;
5465 /* Put constant last, CONST_INT last if both constant. */
5466 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
5467 && GET_CODE (arg1) != CONST_INT)
5468 tem = arg0, arg0 = arg1, arg1 = tem;
5470 /* If second argument is not now constant, not giv. */
5471 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
5472 return 0;
5474 /* Handle multiply by 0 or 1. */
5475 if (arg1 == const0_rtx)
5476 return const0_rtx;
5478 else if (arg1 == const1_rtx)
5479 return arg0;
5481 switch (GET_CODE (arg0))
5483 case REG:
5484 /* biv * invar. Done. */
5485 return gen_rtx (MULT, mode, arg0, arg1);
5487 case CONST_INT:
5488 /* Product of two constants. */
5489 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
5491 case USE:
5492 /* invar * invar. Not giv. */
5493 return 0;
5495 case MULT:
5496 /* (a * invar_1) * invar_2. Associate. */
5497 return simplify_giv_expr (gen_rtx (MULT, mode,
5498 XEXP (arg0, 0),
5499 gen_rtx (MULT, mode,
5500 XEXP (arg0, 1), arg1)),
5501 benefit);
5503 case PLUS:
5504 /* (a + invar_1) * invar_2. Distribute. */
5505 return simplify_giv_expr (gen_rtx (PLUS, mode,
5506 gen_rtx (MULT, mode,
5507 XEXP (arg0, 0), arg1),
5508 gen_rtx (MULT, mode,
5509 XEXP (arg0, 1), arg1)),
5510 benefit);
5512 default:
5513 abort ();
5516 case ASHIFT:
5517 /* Shift by constant is multiply by power of two. */
5518 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5519 return 0;
5521 return simplify_giv_expr (gen_rtx (MULT, mode,
5522 XEXP (x, 0),
5523 GEN_INT ((HOST_WIDE_INT) 1
5524 << INTVAL (XEXP (x, 1)))),
5525 benefit);
5527 case NEG:
5528 /* "-a" is "a * (-1)" */
5529 return simplify_giv_expr (gen_rtx (MULT, mode, XEXP (x, 0), constm1_rtx),
5530 benefit);
5532 case NOT:
5533 /* "~a" is "-a - 1". Silly, but easy. */
5534 return simplify_giv_expr (gen_rtx (MINUS, mode,
5535 gen_rtx (NEG, mode, XEXP (x, 0)),
5536 const1_rtx),
5537 benefit);
5539 case USE:
5540 /* Already in proper form for invariant. */
5541 return x;
5543 case REG:
5544 /* If this is a new register, we can't deal with it. */
5545 if (REGNO (x) >= max_reg_before_loop)
5546 return 0;
5548 /* Check for biv or giv. */
5549 switch (reg_iv_type[REGNO (x)])
5551 case BASIC_INDUCT:
5552 return x;
5553 case GENERAL_INDUCT:
5555 struct induction *v = reg_iv_info[REGNO (x)];
5557 /* Form expression from giv and add benefit. Ensure this giv
5558 can derive another and subtract any needed adjustment if so. */
5559 *benefit += v->benefit;
5560 if (v->cant_derive)
5561 return 0;
5563 tem = gen_rtx (PLUS, mode, gen_rtx (MULT, mode,
5564 v->src_reg, v->mult_val),
5565 v->add_val);
5566 if (v->derive_adjustment)
5567 tem = gen_rtx (MINUS, mode, tem, v->derive_adjustment);
5568 return simplify_giv_expr (tem, benefit);
5571 default:
5572 break;
5575 /* Fall through to general case. */
5576 default:
5577 /* If invariant, return as USE (unless CONST_INT).
5578 Otherwise, not giv. */
5579 if (GET_CODE (x) == USE)
5580 x = XEXP (x, 0);
5582 if (invariant_p (x) == 1)
5584 if (GET_CODE (x) == CONST_INT)
5585 return x;
5586 else
5587 return gen_rtx (USE, mode, x);
5589 else
5590 return 0;
5594 /* Help detect a giv that is calculated by several consecutive insns;
5595 for example,
5596 giv = biv * M
5597 giv = giv + A
5598 The caller has already identified the first insn P as having a giv as dest;
5599 we check that all other insns that set the same register follow
5600 immediately after P, that they alter nothing else,
5601 and that the result of the last is still a giv.
5603 The value is 0 if the reg set in P is not really a giv.
5604 Otherwise, the value is the amount gained by eliminating
5605 all the consecutive insns that compute the value.
5607 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
5608 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
5610 The coefficients of the ultimate giv value are stored in
5611 *MULT_VAL and *ADD_VAL. */
5613 static int
5614 consec_sets_giv (first_benefit, p, src_reg, dest_reg,
5615 add_val, mult_val)
5616 int first_benefit;
5617 rtx p;
5618 rtx src_reg;
5619 rtx dest_reg;
5620 rtx *add_val;
5621 rtx *mult_val;
5623 int count;
5624 enum rtx_code code;
5625 int benefit;
5626 rtx temp;
5627 rtx set;
5629 /* Indicate that this is a giv so that we can update the value produced in
5630 each insn of the multi-insn sequence.
5632 This induction structure will be used only by the call to
5633 general_induction_var below, so we can allocate it on our stack.
5634 If this is a giv, our caller will replace the induct var entry with
5635 a new induction structure. */
5636 struct induction *v
5637 = (struct induction *) alloca (sizeof (struct induction));
5638 v->src_reg = src_reg;
5639 v->mult_val = *mult_val;
5640 v->add_val = *add_val;
5641 v->benefit = first_benefit;
5642 v->cant_derive = 0;
5643 v->derive_adjustment = 0;
5645 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
5646 reg_iv_info[REGNO (dest_reg)] = v;
5648 count = n_times_set[REGNO (dest_reg)] - 1;
5650 while (count > 0)
5652 p = NEXT_INSN (p);
5653 code = GET_CODE (p);
5655 /* If libcall, skip to end of call sequence. */
5656 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
5657 p = XEXP (temp, 0);
5659 if (code == INSN
5660 && (set = single_set (p))
5661 && GET_CODE (SET_DEST (set)) == REG
5662 && SET_DEST (set) == dest_reg
5663 && ((benefit = general_induction_var (SET_SRC (set), &src_reg,
5664 add_val, mult_val))
5665 /* Giv created by equivalent expression. */
5666 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
5667 && (benefit = general_induction_var (XEXP (temp, 0), &src_reg,
5668 add_val, mult_val))))
5669 && src_reg == v->src_reg)
5671 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5672 benefit += libcall_benefit (p);
5674 count--;
5675 v->mult_val = *mult_val;
5676 v->add_val = *add_val;
5677 v->benefit = benefit;
5679 else if (code != NOTE)
5681 /* Allow insns that set something other than this giv to a
5682 constant. Such insns are needed on machines which cannot
5683 include long constants and should not disqualify a giv. */
5684 if (code == INSN
5685 && (set = single_set (p))
5686 && SET_DEST (set) != dest_reg
5687 && CONSTANT_P (SET_SRC (set)))
5688 continue;
5690 reg_iv_type[REGNO (dest_reg)] = UNKNOWN_INDUCT;
5691 return 0;
5695 return v->benefit;
5698 /* Return an rtx, if any, that expresses giv G2 as a function of the register
5699 represented by G1. If no such expression can be found, or it is clear that
5700 it cannot possibly be a valid address, 0 is returned.
5702 To perform the computation, we note that
5703 G1 = a * v + b and
5704 G2 = c * v + d
5705 where `v' is the biv.
5707 So G2 = (c/a) * G1 + (d - b*c/a) */
5709 #ifdef ADDRESS_COST
5710 static rtx
5711 express_from (g1, g2)
5712 struct induction *g1, *g2;
5714 rtx mult, add;
5716 /* The value that G1 will be multiplied by must be a constant integer. Also,
5717 the only chance we have of getting a valid address is if b*c/a (see above
5718 for notation) is also an integer. */
5719 if (GET_CODE (g1->mult_val) != CONST_INT
5720 || GET_CODE (g2->mult_val) != CONST_INT
5721 || GET_CODE (g1->add_val) != CONST_INT
5722 || g1->mult_val == const0_rtx
5723 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
5724 return 0;
5726 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
5727 add = plus_constant (g2->add_val, - INTVAL (g1->add_val) * INTVAL (mult));
5729 /* Form simplified final result. */
5730 if (mult == const0_rtx)
5731 return add;
5732 else if (mult == const1_rtx)
5733 mult = g1->dest_reg;
5734 else
5735 mult = gen_rtx (MULT, g2->mode, g1->dest_reg, mult);
5737 if (add == const0_rtx)
5738 return mult;
5739 else
5740 return gen_rtx (PLUS, g2->mode, mult, add);
5742 #endif
5744 /* Return 1 if giv G2 can be combined with G1. This means that G2 can use
5745 (either directly or via an address expression) a register used to represent
5746 G1. Set g2->new_reg to a represtation of G1 (normally just
5747 g1->dest_reg). */
5749 static int
5750 combine_givs_p (g1, g2)
5751 struct induction *g1, *g2;
5753 rtx tem;
5755 /* If these givs are identical, they can be combined. */
5756 if (rtx_equal_p (g1->mult_val, g2->mult_val)
5757 && rtx_equal_p (g1->add_val, g2->add_val))
5759 g2->new_reg = g1->dest_reg;
5760 return 1;
5763 #ifdef ADDRESS_COST
5764 /* If G2 can be expressed as a function of G1 and that function is valid
5765 as an address and no more expensive than using a register for G2,
5766 the expression of G2 in terms of G1 can be used. */
5767 if (g2->giv_type == DEST_ADDR
5768 && (tem = express_from (g1, g2)) != 0
5769 && memory_address_p (g2->mem_mode, tem)
5770 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location))
5772 g2->new_reg = tem;
5773 return 1;
5775 #endif
5777 return 0;
5780 #ifdef GIV_SORT_CRITERION
5781 /* Compare two givs and sort the most desirable one for combinations first.
5782 This is used only in one qsort call below. */
5784 static int
5785 giv_sort (x, y)
5786 struct induction **x, **y;
5788 GIV_SORT_CRITERION (*x, *y);
5790 return 0;
5792 #endif
5794 /* Check all pairs of givs for iv_class BL and see if any can be combined with
5795 any other. If so, point SAME to the giv combined with and set NEW_REG to
5796 be an expression (in terms of the other giv's DEST_REG) equivalent to the
5797 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
5799 static void
5800 combine_givs (bl)
5801 struct iv_class *bl;
5803 struct induction *g1, *g2, **giv_array, *temp_iv;
5804 int i, j, giv_count, pass;
5806 /* Count givs, because bl->giv_count is incorrect here. */
5807 giv_count = 0;
5808 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5809 giv_count++;
5811 giv_array
5812 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
5813 i = 0;
5814 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5815 giv_array[i++] = g1;
5817 #ifdef GIV_SORT_CRITERION
5818 /* Sort the givs if GIV_SORT_CRITERION is defined.
5819 This is usually defined for processors which lack
5820 negative register offsets so more givs may be combined. */
5822 if (loop_dump_stream)
5823 fprintf (loop_dump_stream, "%d givs counted, sorting...\n", giv_count);
5825 qsort (giv_array, giv_count, sizeof (struct induction *), giv_sort);
5826 #endif
5828 for (i = 0; i < giv_count; i++)
5830 g1 = giv_array[i];
5831 for (pass = 0; pass <= 1; pass++)
5832 for (j = 0; j < giv_count; j++)
5834 g2 = giv_array[j];
5835 if (g1 != g2
5836 /* First try to combine with replaceable givs, then all givs. */
5837 && (g1->replaceable || pass == 1)
5838 /* If either has already been combined or is to be ignored, can't
5839 combine. */
5840 && ! g1->ignore && ! g2->ignore && ! g1->same && ! g2->same
5841 /* If something has been based on G2, G2 cannot itself be based
5842 on something else. */
5843 && ! g2->combined_with
5844 && combine_givs_p (g1, g2))
5846 /* g2->new_reg set by `combine_givs_p' */
5847 g2->same = g1;
5848 g1->combined_with = 1;
5850 /* If one of these givs is a DEST_REG that was only used
5851 once, by the other giv, this is actually a single use.
5852 The DEST_REG has the correct cost, while the other giv
5853 counts the REG use too often. */
5854 if (g2->giv_type == DEST_REG
5855 && n_times_used[REGNO (g2->dest_reg)] == 1
5856 && reg_mentioned_p (g2->dest_reg, PATTERN (g1->insn)))
5857 g1->benefit = g2->benefit;
5858 else if (g1->giv_type != DEST_REG
5859 || n_times_used[REGNO (g1->dest_reg)] != 1
5860 || ! reg_mentioned_p (g1->dest_reg,
5861 PATTERN (g2->insn)))
5863 g1->benefit += g2->benefit;
5864 g1->times_used += g2->times_used;
5866 /* ??? The new final_[bg]iv_value code does a much better job
5867 of finding replaceable giv's, and hence this code may no
5868 longer be necessary. */
5869 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
5870 g1->benefit -= copy_cost;
5871 g1->lifetime += g2->lifetime;
5873 if (loop_dump_stream)
5874 fprintf (loop_dump_stream, "giv at %d combined with giv at %d\n",
5875 INSN_UID (g2->insn), INSN_UID (g1->insn));
5881 /* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
5883 void
5884 emit_iv_add_mult (b, m, a, reg, insert_before)
5885 rtx b; /* initial value of basic induction variable */
5886 rtx m; /* multiplicative constant */
5887 rtx a; /* additive constant */
5888 rtx reg; /* destination register */
5889 rtx insert_before;
5891 rtx seq;
5892 rtx result;
5894 /* Prevent unexpected sharing of these rtx. */
5895 a = copy_rtx (a);
5896 b = copy_rtx (b);
5898 /* Increase the lifetime of any invariants moved further in code. */
5899 update_reg_last_use (a, insert_before);
5900 update_reg_last_use (b, insert_before);
5901 update_reg_last_use (m, insert_before);
5903 start_sequence ();
5904 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 0);
5905 if (reg != result)
5906 emit_move_insn (reg, result);
5907 seq = gen_sequence ();
5908 end_sequence ();
5910 emit_insn_before (seq, insert_before);
5912 record_base_value (REGNO (reg), b);
5915 /* Test whether A * B can be computed without
5916 an actual multiply insn. Value is 1 if so. */
5918 static int
5919 product_cheap_p (a, b)
5920 rtx a;
5921 rtx b;
5923 int i;
5924 rtx tmp;
5925 struct obstack *old_rtl_obstack = rtl_obstack;
5926 char *storage = (char *) obstack_alloc (&temp_obstack, 0);
5927 int win = 1;
5929 /* If only one is constant, make it B. */
5930 if (GET_CODE (a) == CONST_INT)
5931 tmp = a, a = b, b = tmp;
5933 /* If first constant, both constant, so don't need multiply. */
5934 if (GET_CODE (a) == CONST_INT)
5935 return 1;
5937 /* If second not constant, neither is constant, so would need multiply. */
5938 if (GET_CODE (b) != CONST_INT)
5939 return 0;
5941 /* One operand is constant, so might not need multiply insn. Generate the
5942 code for the multiply and see if a call or multiply, or long sequence
5943 of insns is generated. */
5945 rtl_obstack = &temp_obstack;
5946 start_sequence ();
5947 expand_mult (GET_MODE (a), a, b, NULL_RTX, 0);
5948 tmp = gen_sequence ();
5949 end_sequence ();
5951 if (GET_CODE (tmp) == SEQUENCE)
5953 if (XVEC (tmp, 0) == 0)
5954 win = 1;
5955 else if (XVECLEN (tmp, 0) > 3)
5956 win = 0;
5957 else
5958 for (i = 0; i < XVECLEN (tmp, 0); i++)
5960 rtx insn = XVECEXP (tmp, 0, i);
5962 if (GET_CODE (insn) != INSN
5963 || (GET_CODE (PATTERN (insn)) == SET
5964 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
5965 || (GET_CODE (PATTERN (insn)) == PARALLEL
5966 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
5967 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
5969 win = 0;
5970 break;
5974 else if (GET_CODE (tmp) == SET
5975 && GET_CODE (SET_SRC (tmp)) == MULT)
5976 win = 0;
5977 else if (GET_CODE (tmp) == PARALLEL
5978 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
5979 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
5980 win = 0;
5982 /* Free any storage we obtained in generating this multiply and restore rtl
5983 allocation to its normal obstack. */
5984 obstack_free (&temp_obstack, storage);
5985 rtl_obstack = old_rtl_obstack;
5987 return win;
5990 /* Check to see if loop can be terminated by a "decrement and branch until
5991 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
5992 Also try reversing an increment loop to a decrement loop
5993 to see if the optimization can be performed.
5994 Value is nonzero if optimization was performed. */
5996 /* This is useful even if the architecture doesn't have such an insn,
5997 because it might change a loops which increments from 0 to n to a loop
5998 which decrements from n to 0. A loop that decrements to zero is usually
5999 faster than one that increments from zero. */
6001 /* ??? This could be rewritten to use some of the loop unrolling procedures,
6002 such as approx_final_value, biv_total_increment, loop_iterations, and
6003 final_[bg]iv_value. */
6005 static int
6006 check_dbra_loop (loop_end, insn_count, loop_start)
6007 rtx loop_end;
6008 int insn_count;
6009 rtx loop_start;
6011 struct iv_class *bl;
6012 rtx reg;
6013 rtx jump_label;
6014 rtx final_value;
6015 rtx start_value;
6016 rtx new_add_val;
6017 rtx comparison;
6018 rtx before_comparison;
6019 rtx p;
6021 /* If last insn is a conditional branch, and the insn before tests a
6022 register value, try to optimize it. Otherwise, we can't do anything. */
6024 comparison = get_condition_for_loop (PREV_INSN (loop_end));
6025 if (comparison == 0)
6026 return 0;
6028 /* Check all of the bivs to see if the compare uses one of them.
6029 Skip biv's set more than once because we can't guarantee that
6030 it will be zero on the last iteration. Also skip if the biv is
6031 used between its update and the test insn. */
6033 for (bl = loop_iv_list; bl; bl = bl->next)
6035 if (bl->biv_count == 1
6036 && bl->biv->dest_reg == XEXP (comparison, 0)
6037 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
6038 PREV_INSN (PREV_INSN (loop_end))))
6039 break;
6042 if (! bl)
6043 return 0;
6045 /* Look for the case where the basic induction variable is always
6046 nonnegative, and equals zero on the last iteration.
6047 In this case, add a reg_note REG_NONNEG, which allows the
6048 m68k DBRA instruction to be used. */
6050 if (((GET_CODE (comparison) == GT
6051 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6052 && INTVAL (XEXP (comparison, 1)) == -1)
6053 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
6054 && GET_CODE (bl->biv->add_val) == CONST_INT
6055 && INTVAL (bl->biv->add_val) < 0)
6057 /* Initial value must be greater than 0,
6058 init_val % -dec_value == 0 to ensure that it equals zero on
6059 the last iteration */
6061 if (GET_CODE (bl->initial_value) == CONST_INT
6062 && INTVAL (bl->initial_value) > 0
6063 && (INTVAL (bl->initial_value)
6064 % (-INTVAL (bl->biv->add_val))) == 0)
6066 /* register always nonnegative, add REG_NOTE to branch */
6067 REG_NOTES (PREV_INSN (loop_end))
6068 = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
6069 REG_NOTES (PREV_INSN (loop_end)));
6070 bl->nonneg = 1;
6072 return 1;
6075 /* If the decrement is 1 and the value was tested as >= 0 before
6076 the loop, then we can safely optimize. */
6077 for (p = loop_start; p; p = PREV_INSN (p))
6079 if (GET_CODE (p) == CODE_LABEL)
6080 break;
6081 if (GET_CODE (p) != JUMP_INSN)
6082 continue;
6084 before_comparison = get_condition_for_loop (p);
6085 if (before_comparison
6086 && XEXP (before_comparison, 0) == bl->biv->dest_reg
6087 && GET_CODE (before_comparison) == LT
6088 && XEXP (before_comparison, 1) == const0_rtx
6089 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
6090 && INTVAL (bl->biv->add_val) == -1)
6092 REG_NOTES (PREV_INSN (loop_end))
6093 = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
6094 REG_NOTES (PREV_INSN (loop_end)));
6095 bl->nonneg = 1;
6097 return 1;
6101 else if (num_mem_sets <= 1)
6103 /* Try to change inc to dec, so can apply above optimization. */
6104 /* Can do this if:
6105 all registers modified are induction variables or invariant,
6106 all memory references have non-overlapping addresses
6107 (obviously true if only one write)
6108 allow 2 insns for the compare/jump at the end of the loop. */
6109 /* Also, we must avoid any instructions which use both the reversed
6110 biv and another biv. Such instructions will fail if the loop is
6111 reversed. We meet this condition by requiring that either
6112 no_use_except_counting is true, or else that there is only
6113 one biv. */
6114 int num_nonfixed_reads = 0;
6115 /* 1 if the iteration var is used only to count iterations. */
6116 int no_use_except_counting = 0;
6117 /* 1 if the loop has no memory store, or it has a single memory store
6118 which is reversible. */
6119 int reversible_mem_store = 1;
6121 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6122 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6123 num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
6125 if (bl->giv_count == 0
6126 && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
6128 rtx bivreg = regno_reg_rtx[bl->regno];
6130 /* If there are no givs for this biv, and the only exit is the
6131 fall through at the end of the the loop, then
6132 see if perhaps there are no uses except to count. */
6133 no_use_except_counting = 1;
6134 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6135 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6137 rtx set = single_set (p);
6139 if (set && GET_CODE (SET_DEST (set)) == REG
6140 && REGNO (SET_DEST (set)) == bl->regno)
6141 /* An insn that sets the biv is okay. */
6143 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
6144 || p == prev_nonnote_insn (loop_end))
6145 /* Don't bother about the end test. */
6147 else if (reg_mentioned_p (bivreg, PATTERN (p)))
6148 /* Any other use of the biv is no good. */
6150 no_use_except_counting = 0;
6151 break;
6156 /* If the loop has a single store, and the destination address is
6157 invariant, then we can't reverse the loop, because this address
6158 might then have the wrong value at loop exit.
6159 This would work if the source was invariant also, however, in that
6160 case, the insn should have been moved out of the loop. */
6162 if (num_mem_sets == 1)
6163 reversible_mem_store
6164 = (! unknown_address_altered
6165 && ! invariant_p (XEXP (loop_store_mems[0], 0)));
6167 /* This code only acts for innermost loops. Also it simplifies
6168 the memory address check by only reversing loops with
6169 zero or one memory access.
6170 Two memory accesses could involve parts of the same array,
6171 and that can't be reversed. */
6173 if (num_nonfixed_reads <= 1
6174 && !loop_has_call
6175 && !loop_has_volatile
6176 && reversible_mem_store
6177 && (no_use_except_counting
6178 || ((bl->giv_count + bl->biv_count + num_mem_sets
6179 + num_movables + 2 == insn_count)
6180 && (bl == loop_iv_list && bl->next == 0))))
6182 rtx tem;
6184 /* Loop can be reversed. */
6185 if (loop_dump_stream)
6186 fprintf (loop_dump_stream, "Can reverse loop\n");
6188 /* Now check other conditions:
6190 The increment must be a constant and the comparison code
6191 must be LT.
6193 This test can probably be improved since +/- 1 in the constant
6194 can be obtained by changing LT to LE and vice versa; this is
6195 confusing. */
6197 if (comparison
6198 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6199 /* LE gets turned into LT */
6200 && GET_CODE (comparison) == LT)
6202 HOST_WIDE_INT add_val, comparison_val;
6203 rtx initial_value;
6205 add_val = INTVAL (bl->biv->add_val);
6206 comparison_val = INTVAL (XEXP (comparison, 1));
6207 initial_value = bl->initial_value;
6209 /* Normalize the initial value if it has no other use
6210 except as a counter. This will allow a few more loops
6211 to be reversed. */
6212 if (no_use_except_counting)
6214 comparison_val = comparison_val - INTVAL (bl->initial_value);
6215 initial_value = const0_rtx;
6218 /* If the initial value is not zero, or if the comparison
6219 value is not an exact multiple of the increment, then we
6220 can not reverse this loop. */
6221 if (initial_value != const0_rtx
6222 || (comparison_val % add_val) != 0)
6223 return 0;
6225 /* Reset these in case we normalized the initial value
6226 and comparison value above. */
6227 bl->initial_value = initial_value;
6228 XEXP (comparison, 1) = GEN_INT (comparison_val);
6230 /* Register will always be nonnegative, with value
6231 0 on last iteration if loop reversed */
6233 /* Save some info needed to produce the new insns. */
6234 reg = bl->biv->dest_reg;
6235 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
6236 if (jump_label == pc_rtx)
6237 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
6238 new_add_val = GEN_INT (- INTVAL (bl->biv->add_val));
6240 final_value = XEXP (comparison, 1);
6241 start_value = GEN_INT (INTVAL (XEXP (comparison, 1))
6242 - INTVAL (bl->biv->add_val));
6244 /* Initialize biv to start_value before loop start.
6245 The old initializing insn will be deleted as a
6246 dead store by flow.c. */
6247 emit_insn_before (gen_move_insn (reg, start_value), loop_start);
6249 /* Add insn to decrement register, and delete insn
6250 that incremented the register. */
6251 p = emit_insn_before (gen_add2_insn (reg, new_add_val),
6252 bl->biv->insn);
6253 delete_insn (bl->biv->insn);
6255 /* Update biv info to reflect its new status. */
6256 bl->biv->insn = p;
6257 bl->initial_value = start_value;
6258 bl->biv->add_val = new_add_val;
6260 /* Inc LABEL_NUSES so that delete_insn will
6261 not delete the label. */
6262 LABEL_NUSES (XEXP (jump_label, 0)) ++;
6264 /* Emit an insn after the end of the loop to set the biv's
6265 proper exit value if it is used anywhere outside the loop. */
6266 if ((REGNO_LAST_UID (bl->regno)
6267 != INSN_UID (PREV_INSN (PREV_INSN (loop_end))))
6268 || ! bl->init_insn
6269 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
6270 emit_insn_after (gen_move_insn (reg, final_value),
6271 loop_end);
6273 /* Delete compare/branch at end of loop. */
6274 delete_insn (PREV_INSN (loop_end));
6275 delete_insn (PREV_INSN (loop_end));
6277 /* Add new compare/branch insn at end of loop. */
6278 start_sequence ();
6279 emit_cmp_insn (reg, const0_rtx, GE, NULL_RTX,
6280 GET_MODE (reg), 0, 0);
6281 emit_jump_insn (gen_bge (XEXP (jump_label, 0)));
6282 tem = gen_sequence ();
6283 end_sequence ();
6284 emit_jump_insn_before (tem, loop_end);
6286 for (tem = PREV_INSN (loop_end);
6287 tem && GET_CODE (tem) != JUMP_INSN; tem = PREV_INSN (tem))
6289 if (tem)
6291 JUMP_LABEL (tem) = XEXP (jump_label, 0);
6293 /* Increment of LABEL_NUSES done above. */
6294 /* Register is now always nonnegative,
6295 so add REG_NONNEG note to the branch. */
6296 REG_NOTES (tem) = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
6297 REG_NOTES (tem));
6300 bl->nonneg = 1;
6302 /* Mark that this biv has been reversed. Each giv which depends
6303 on this biv, and which is also live past the end of the loop
6304 will have to be fixed up. */
6306 bl->reversed = 1;
6308 if (loop_dump_stream)
6309 fprintf (loop_dump_stream,
6310 "Reversed loop and added reg_nonneg\n");
6312 return 1;
6317 return 0;
6320 /* Verify whether the biv BL appears to be eliminable,
6321 based on the insns in the loop that refer to it.
6322 LOOP_START is the first insn of the loop, and END is the end insn.
6324 If ELIMINATE_P is non-zero, actually do the elimination.
6326 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
6327 determine whether invariant insns should be placed inside or at the
6328 start of the loop. */
6330 static int
6331 maybe_eliminate_biv (bl, loop_start, end, eliminate_p, threshold, insn_count)
6332 struct iv_class *bl;
6333 rtx loop_start;
6334 rtx end;
6335 int eliminate_p;
6336 int threshold, insn_count;
6338 rtx reg = bl->biv->dest_reg;
6339 rtx p;
6341 /* Scan all insns in the loop, stopping if we find one that uses the
6342 biv in a way that we cannot eliminate. */
6344 for (p = loop_start; p != end; p = NEXT_INSN (p))
6346 enum rtx_code code = GET_CODE (p);
6347 rtx where = threshold >= insn_count ? loop_start : p;
6349 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
6350 && reg_mentioned_p (reg, PATTERN (p))
6351 && ! maybe_eliminate_biv_1 (PATTERN (p), p, bl, eliminate_p, where))
6353 if (loop_dump_stream)
6354 fprintf (loop_dump_stream,
6355 "Cannot eliminate biv %d: biv used in insn %d.\n",
6356 bl->regno, INSN_UID (p));
6357 break;
6361 if (p == end)
6363 if (loop_dump_stream)
6364 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
6365 bl->regno, eliminate_p ? "was" : "can be");
6366 return 1;
6369 return 0;
6372 /* If BL appears in X (part of the pattern of INSN), see if we can
6373 eliminate its use. If so, return 1. If not, return 0.
6375 If BIV does not appear in X, return 1.
6377 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
6378 where extra insns should be added. Depending on how many items have been
6379 moved out of the loop, it will either be before INSN or at the start of
6380 the loop. */
6382 static int
6383 maybe_eliminate_biv_1 (x, insn, bl, eliminate_p, where)
6384 rtx x, insn;
6385 struct iv_class *bl;
6386 int eliminate_p;
6387 rtx where;
6389 enum rtx_code code = GET_CODE (x);
6390 rtx reg = bl->biv->dest_reg;
6391 enum machine_mode mode = GET_MODE (reg);
6392 struct induction *v;
6393 rtx arg, new, tem;
6394 int arg_operand;
6395 char *fmt;
6396 int i, j;
6398 switch (code)
6400 case REG:
6401 /* If we haven't already been able to do something with this BIV,
6402 we can't eliminate it. */
6403 if (x == reg)
6404 return 0;
6405 return 1;
6407 case SET:
6408 /* If this sets the BIV, it is not a problem. */
6409 if (SET_DEST (x) == reg)
6410 return 1;
6412 /* If this is an insn that defines a giv, it is also ok because
6413 it will go away when the giv is reduced. */
6414 for (v = bl->giv; v; v = v->next_iv)
6415 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
6416 return 1;
6418 #ifdef HAVE_cc0
6419 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
6421 /* Can replace with any giv that was reduced and
6422 that has (MULT_VAL != 0) and (ADD_VAL == 0).
6423 Require a constant for MULT_VAL, so we know it's nonzero.
6424 ??? We disable this optimization to avoid potential
6425 overflows. */
6427 for (v = bl->giv; v; v = v->next_iv)
6428 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6429 && v->add_val == const0_rtx
6430 && ! v->ignore && ! v->maybe_dead && v->always_computable
6431 && v->mode == mode
6432 && 0)
6434 /* If the giv V had the auto-inc address optimization applied
6435 to it, and INSN occurs between the giv insn and the biv
6436 insn, then we must adjust the value used here.
6437 This is rare, so we don't bother to do so. */
6438 if (v->auto_inc_opt
6439 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6440 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6441 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6442 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6443 continue;
6445 if (! eliminate_p)
6446 return 1;
6448 /* If the giv has the opposite direction of change,
6449 then reverse the comparison. */
6450 if (INTVAL (v->mult_val) < 0)
6451 new = gen_rtx (COMPARE, GET_MODE (v->new_reg),
6452 const0_rtx, v->new_reg);
6453 else
6454 new = v->new_reg;
6456 /* We can probably test that giv's reduced reg. */
6457 if (validate_change (insn, &SET_SRC (x), new, 0))
6458 return 1;
6461 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
6462 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
6463 Require a constant for MULT_VAL, so we know it's nonzero.
6464 ??? Do this only if ADD_VAL is a pointer to avoid a potential
6465 overflow problem. */
6467 for (v = bl->giv; v; v = v->next_iv)
6468 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6469 && ! v->ignore && ! v->maybe_dead && v->always_computable
6470 && v->mode == mode
6471 && (GET_CODE (v->add_val) == SYMBOL_REF
6472 || GET_CODE (v->add_val) == LABEL_REF
6473 || GET_CODE (v->add_val) == CONST
6474 || (GET_CODE (v->add_val) == REG
6475 && REGNO_POINTER_FLAG (REGNO (v->add_val)))))
6477 /* If the giv V had the auto-inc address optimization applied
6478 to it, and INSN occurs between the giv insn and the biv
6479 insn, then we must adjust the value used here.
6480 This is rare, so we don't bother to do so. */
6481 if (v->auto_inc_opt
6482 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6483 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6484 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6485 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6486 continue;
6488 if (! eliminate_p)
6489 return 1;
6491 /* If the giv has the opposite direction of change,
6492 then reverse the comparison. */
6493 if (INTVAL (v->mult_val) < 0)
6494 new = gen_rtx (COMPARE, VOIDmode, copy_rtx (v->add_val),
6495 v->new_reg);
6496 else
6497 new = gen_rtx (COMPARE, VOIDmode, v->new_reg,
6498 copy_rtx (v->add_val));
6500 /* Replace biv with the giv's reduced register. */
6501 update_reg_last_use (v->add_val, insn);
6502 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6503 return 1;
6505 /* Insn doesn't support that constant or invariant. Copy it
6506 into a register (it will be a loop invariant.) */
6507 tem = gen_reg_rtx (GET_MODE (v->new_reg));
6509 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
6510 where);
6512 /* Substitute the new register for its invariant value in
6513 the compare expression. */
6514 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
6515 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6516 return 1;
6519 #endif
6520 break;
6522 case COMPARE:
6523 case EQ: case NE:
6524 case GT: case GE: case GTU: case GEU:
6525 case LT: case LE: case LTU: case LEU:
6526 /* See if either argument is the biv. */
6527 if (XEXP (x, 0) == reg)
6528 arg = XEXP (x, 1), arg_operand = 1;
6529 else if (XEXP (x, 1) == reg)
6530 arg = XEXP (x, 0), arg_operand = 0;
6531 else
6532 break;
6534 if (CONSTANT_P (arg))
6536 /* First try to replace with any giv that has constant positive
6537 mult_val and constant add_val. We might be able to support
6538 negative mult_val, but it seems complex to do it in general. */
6540 for (v = bl->giv; v; v = v->next_iv)
6541 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6542 && (GET_CODE (v->add_val) == SYMBOL_REF
6543 || GET_CODE (v->add_val) == LABEL_REF
6544 || GET_CODE (v->add_val) == CONST
6545 || (GET_CODE (v->add_val) == REG
6546 && REGNO_POINTER_FLAG (REGNO (v->add_val))))
6547 && ! v->ignore && ! v->maybe_dead && v->always_computable
6548 && v->mode == mode)
6550 /* If the giv V had the auto-inc address optimization applied
6551 to it, and INSN occurs between the giv insn and the biv
6552 insn, then we must adjust the value used here.
6553 This is rare, so we don't bother to do so. */
6554 if (v->auto_inc_opt
6555 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6556 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6557 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6558 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6559 continue;
6561 if (! eliminate_p)
6562 return 1;
6564 /* Replace biv with the giv's reduced reg. */
6565 XEXP (x, 1-arg_operand) = v->new_reg;
6567 /* If all constants are actually constant integers and
6568 the derived constant can be directly placed in the COMPARE,
6569 do so. */
6570 if (GET_CODE (arg) == CONST_INT
6571 && GET_CODE (v->mult_val) == CONST_INT
6572 && GET_CODE (v->add_val) == CONST_INT
6573 && validate_change (insn, &XEXP (x, arg_operand),
6574 GEN_INT (INTVAL (arg)
6575 * INTVAL (v->mult_val)
6576 + INTVAL (v->add_val)), 0))
6577 return 1;
6579 /* Otherwise, load it into a register. */
6580 tem = gen_reg_rtx (mode);
6581 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6582 if (validate_change (insn, &XEXP (x, arg_operand), tem, 0))
6583 return 1;
6585 /* If that failed, put back the change we made above. */
6586 XEXP (x, 1-arg_operand) = reg;
6589 /* Look for giv with positive constant mult_val and nonconst add_val.
6590 Insert insns to calculate new compare value.
6591 ??? Turn this off due to possible overflow. */
6593 for (v = bl->giv; v; v = v->next_iv)
6594 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6595 && ! v->ignore && ! v->maybe_dead && v->always_computable
6596 && v->mode == mode
6597 && 0)
6599 rtx tem;
6601 /* If the giv V had the auto-inc address optimization applied
6602 to it, and INSN occurs between the giv insn and the biv
6603 insn, then we must adjust the value used here.
6604 This is rare, so we don't bother to do so. */
6605 if (v->auto_inc_opt
6606 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6607 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6608 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6609 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6610 continue;
6612 if (! eliminate_p)
6613 return 1;
6615 tem = gen_reg_rtx (mode);
6617 /* Replace biv with giv's reduced register. */
6618 validate_change (insn, &XEXP (x, 1 - arg_operand),
6619 v->new_reg, 1);
6621 /* Compute value to compare against. */
6622 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6623 /* Use it in this insn. */
6624 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6625 if (apply_change_group ())
6626 return 1;
6629 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
6631 if (invariant_p (arg) == 1)
6633 /* Look for giv with constant positive mult_val and nonconst
6634 add_val. Insert insns to compute new compare value.
6635 ??? Turn this off due to possible overflow. */
6637 for (v = bl->giv; v; v = v->next_iv)
6638 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6639 && ! v->ignore && ! v->maybe_dead && v->always_computable
6640 && v->mode == mode
6641 && 0)
6643 rtx tem;
6645 /* If the giv V had the auto-inc address optimization applied
6646 to it, and INSN occurs between the giv insn and the biv
6647 insn, then we must adjust the value used here.
6648 This is rare, so we don't bother to do so. */
6649 if (v->auto_inc_opt
6650 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6651 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6652 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6653 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6654 continue;
6656 if (! eliminate_p)
6657 return 1;
6659 tem = gen_reg_rtx (mode);
6661 /* Replace biv with giv's reduced register. */
6662 validate_change (insn, &XEXP (x, 1 - arg_operand),
6663 v->new_reg, 1);
6665 /* Compute value to compare against. */
6666 emit_iv_add_mult (arg, v->mult_val, v->add_val,
6667 tem, where);
6668 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6669 if (apply_change_group ())
6670 return 1;
6674 /* This code has problems. Basically, you can't know when
6675 seeing if we will eliminate BL, whether a particular giv
6676 of ARG will be reduced. If it isn't going to be reduced,
6677 we can't eliminate BL. We can try forcing it to be reduced,
6678 but that can generate poor code.
6680 The problem is that the benefit of reducing TV, below should
6681 be increased if BL can actually be eliminated, but this means
6682 we might have to do a topological sort of the order in which
6683 we try to process biv. It doesn't seem worthwhile to do
6684 this sort of thing now. */
6686 #if 0
6687 /* Otherwise the reg compared with had better be a biv. */
6688 if (GET_CODE (arg) != REG
6689 || reg_iv_type[REGNO (arg)] != BASIC_INDUCT)
6690 return 0;
6692 /* Look for a pair of givs, one for each biv,
6693 with identical coefficients. */
6694 for (v = bl->giv; v; v = v->next_iv)
6696 struct induction *tv;
6698 if (v->ignore || v->maybe_dead || v->mode != mode)
6699 continue;
6701 for (tv = reg_biv_class[REGNO (arg)]->giv; tv; tv = tv->next_iv)
6702 if (! tv->ignore && ! tv->maybe_dead
6703 && rtx_equal_p (tv->mult_val, v->mult_val)
6704 && rtx_equal_p (tv->add_val, v->add_val)
6705 && tv->mode == mode)
6707 /* If the giv V had the auto-inc address optimization applied
6708 to it, and INSN occurs between the giv insn and the biv
6709 insn, then we must adjust the value used here.
6710 This is rare, so we don't bother to do so. */
6711 if (v->auto_inc_opt
6712 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6713 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6714 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6715 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6716 continue;
6718 if (! eliminate_p)
6719 return 1;
6721 /* Replace biv with its giv's reduced reg. */
6722 XEXP (x, 1-arg_operand) = v->new_reg;
6723 /* Replace other operand with the other giv's
6724 reduced reg. */
6725 XEXP (x, arg_operand) = tv->new_reg;
6726 return 1;
6729 #endif
6732 /* If we get here, the biv can't be eliminated. */
6733 return 0;
6735 case MEM:
6736 /* If this address is a DEST_ADDR giv, it doesn't matter if the
6737 biv is used in it, since it will be replaced. */
6738 for (v = bl->giv; v; v = v->next_iv)
6739 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
6740 return 1;
6741 break;
6743 default:
6744 break;
6747 /* See if any subexpression fails elimination. */
6748 fmt = GET_RTX_FORMAT (code);
6749 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6751 switch (fmt[i])
6753 case 'e':
6754 if (! maybe_eliminate_biv_1 (XEXP (x, i), insn, bl,
6755 eliminate_p, where))
6756 return 0;
6757 break;
6759 case 'E':
6760 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6761 if (! maybe_eliminate_biv_1 (XVECEXP (x, i, j), insn, bl,
6762 eliminate_p, where))
6763 return 0;
6764 break;
6768 return 1;
6771 /* Return nonzero if the last use of REG
6772 is in an insn following INSN in the same basic block. */
6774 static int
6775 last_use_this_basic_block (reg, insn)
6776 rtx reg;
6777 rtx insn;
6779 rtx n;
6780 for (n = insn;
6781 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
6782 n = NEXT_INSN (n))
6784 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
6785 return 1;
6787 return 0;
6790 /* Called via `note_stores' to record the initial value of a biv. Here we
6791 just record the location of the set and process it later. */
6793 static void
6794 record_initial (dest, set)
6795 rtx dest;
6796 rtx set;
6798 struct iv_class *bl;
6800 if (GET_CODE (dest) != REG
6801 || REGNO (dest) >= max_reg_before_loop
6802 || reg_iv_type[REGNO (dest)] != BASIC_INDUCT)
6803 return;
6805 bl = reg_biv_class[REGNO (dest)];
6807 /* If this is the first set found, record it. */
6808 if (bl->init_insn == 0)
6810 bl->init_insn = note_insn;
6811 bl->init_set = set;
6815 /* If any of the registers in X are "old" and currently have a last use earlier
6816 than INSN, update them to have a last use of INSN. Their actual last use
6817 will be the previous insn but it will not have a valid uid_luid so we can't
6818 use it. */
6820 static void
6821 update_reg_last_use (x, insn)
6822 rtx x;
6823 rtx insn;
6825 /* Check for the case where INSN does not have a valid luid. In this case,
6826 there is no need to modify the regno_last_uid, as this can only happen
6827 when code is inserted after the loop_end to set a pseudo's final value,
6828 and hence this insn will never be the last use of x. */
6829 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
6830 && INSN_UID (insn) < max_uid_for_loop
6831 && uid_luid[REGNO_LAST_UID (REGNO (x))] < uid_luid[INSN_UID (insn)])
6832 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
6833 else
6835 register int i, j;
6836 register char *fmt = GET_RTX_FORMAT (GET_CODE (x));
6837 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6839 if (fmt[i] == 'e')
6840 update_reg_last_use (XEXP (x, i), insn);
6841 else if (fmt[i] == 'E')
6842 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6843 update_reg_last_use (XVECEXP (x, i, j), insn);
6848 /* Given a jump insn JUMP, return the condition that will cause it to branch
6849 to its JUMP_LABEL. If the condition cannot be understood, or is an
6850 inequality floating-point comparison which needs to be reversed, 0 will
6851 be returned.
6853 If EARLIEST is non-zero, it is a pointer to a place where the earliest
6854 insn used in locating the condition was found. If a replacement test
6855 of the condition is desired, it should be placed in front of that
6856 insn and we will be sure that the inputs are still valid.
6858 The condition will be returned in a canonical form to simplify testing by
6859 callers. Specifically:
6861 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
6862 (2) Both operands will be machine operands; (cc0) will have been replaced.
6863 (3) If an operand is a constant, it will be the second operand.
6864 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
6865 for GE, GEU, and LEU. */
6868 get_condition (jump, earliest)
6869 rtx jump;
6870 rtx *earliest;
6872 enum rtx_code code;
6873 rtx prev = jump;
6874 rtx set;
6875 rtx tem;
6876 rtx op0, op1;
6877 int reverse_code = 0;
6878 int did_reverse_condition = 0;
6880 /* If this is not a standard conditional jump, we can't parse it. */
6881 if (GET_CODE (jump) != JUMP_INSN
6882 || ! condjump_p (jump) || simplejump_p (jump))
6883 return 0;
6885 code = GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 0));
6886 op0 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 0);
6887 op1 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 1);
6889 if (earliest)
6890 *earliest = jump;
6892 /* If this branches to JUMP_LABEL when the condition is false, reverse
6893 the condition. */
6894 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
6895 && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
6896 code = reverse_condition (code), did_reverse_condition ^= 1;
6898 /* If we are comparing a register with zero, see if the register is set
6899 in the previous insn to a COMPARE or a comparison operation. Perform
6900 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
6901 in cse.c */
6903 while (GET_RTX_CLASS (code) == '<' && op1 == CONST0_RTX (GET_MODE (op0)))
6905 /* Set non-zero when we find something of interest. */
6906 rtx x = 0;
6908 #ifdef HAVE_cc0
6909 /* If comparison with cc0, import actual comparison from compare
6910 insn. */
6911 if (op0 == cc0_rtx)
6913 if ((prev = prev_nonnote_insn (prev)) == 0
6914 || GET_CODE (prev) != INSN
6915 || (set = single_set (prev)) == 0
6916 || SET_DEST (set) != cc0_rtx)
6917 return 0;
6919 op0 = SET_SRC (set);
6920 op1 = CONST0_RTX (GET_MODE (op0));
6921 if (earliest)
6922 *earliest = prev;
6924 #endif
6926 /* If this is a COMPARE, pick up the two things being compared. */
6927 if (GET_CODE (op0) == COMPARE)
6929 op1 = XEXP (op0, 1);
6930 op0 = XEXP (op0, 0);
6931 continue;
6933 else if (GET_CODE (op0) != REG)
6934 break;
6936 /* Go back to the previous insn. Stop if it is not an INSN. We also
6937 stop if it isn't a single set or if it has a REG_INC note because
6938 we don't want to bother dealing with it. */
6940 if ((prev = prev_nonnote_insn (prev)) == 0
6941 || GET_CODE (prev) != INSN
6942 || FIND_REG_INC_NOTE (prev, 0)
6943 || (set = single_set (prev)) == 0)
6944 break;
6946 /* If this is setting OP0, get what it sets it to if it looks
6947 relevant. */
6948 if (rtx_equal_p (SET_DEST (set), op0))
6950 enum machine_mode inner_mode = GET_MODE (SET_SRC (set));
6952 if ((GET_CODE (SET_SRC (set)) == COMPARE
6953 || (((code == NE
6954 || (code == LT
6955 && GET_MODE_CLASS (inner_mode) == MODE_INT
6956 && (GET_MODE_BITSIZE (inner_mode)
6957 <= HOST_BITS_PER_WIDE_INT)
6958 && (STORE_FLAG_VALUE
6959 & ((HOST_WIDE_INT) 1
6960 << (GET_MODE_BITSIZE (inner_mode) - 1))))
6961 #ifdef FLOAT_STORE_FLAG_VALUE
6962 || (code == LT
6963 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
6964 && FLOAT_STORE_FLAG_VALUE < 0)
6965 #endif
6967 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')))
6968 x = SET_SRC (set);
6969 else if (((code == EQ
6970 || (code == GE
6971 && (GET_MODE_BITSIZE (inner_mode)
6972 <= HOST_BITS_PER_WIDE_INT)
6973 && GET_MODE_CLASS (inner_mode) == MODE_INT
6974 && (STORE_FLAG_VALUE
6975 & ((HOST_WIDE_INT) 1
6976 << (GET_MODE_BITSIZE (inner_mode) - 1))))
6977 #ifdef FLOAT_STORE_FLAG_VALUE
6978 || (code == GE
6979 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
6980 && FLOAT_STORE_FLAG_VALUE < 0)
6981 #endif
6983 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')
6985 /* We might have reversed a LT to get a GE here. But this wasn't
6986 actually the comparison of data, so we don't flag that we
6987 have had to reverse the condition. */
6988 did_reverse_condition ^= 1;
6989 reverse_code = 1;
6990 x = SET_SRC (set);
6992 else
6993 break;
6996 else if (reg_set_p (op0, prev))
6997 /* If this sets OP0, but not directly, we have to give up. */
6998 break;
7000 if (x)
7002 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
7003 code = GET_CODE (x);
7004 if (reverse_code)
7006 code = reverse_condition (code);
7007 did_reverse_condition ^= 1;
7008 reverse_code = 0;
7011 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
7012 if (earliest)
7013 *earliest = prev;
7017 /* If constant is first, put it last. */
7018 if (CONSTANT_P (op0))
7019 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
7021 /* If OP0 is the result of a comparison, we weren't able to find what
7022 was really being compared, so fail. */
7023 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
7024 return 0;
7026 /* Canonicalize any ordered comparison with integers involving equality
7027 if we can do computations in the relevant mode and we do not
7028 overflow. */
7030 if (GET_CODE (op1) == CONST_INT
7031 && GET_MODE (op0) != VOIDmode
7032 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
7034 HOST_WIDE_INT const_val = INTVAL (op1);
7035 unsigned HOST_WIDE_INT uconst_val = const_val;
7036 unsigned HOST_WIDE_INT max_val
7037 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
7039 switch (code)
7041 case LE:
7042 if (const_val != max_val >> 1)
7043 code = LT, op1 = GEN_INT (const_val + 1);
7044 break;
7046 case GE:
7047 if (const_val
7048 != (((HOST_WIDE_INT) 1
7049 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
7050 code = GT, op1 = GEN_INT (const_val - 1);
7051 break;
7053 case LEU:
7054 if (uconst_val != max_val)
7055 code = LTU, op1 = GEN_INT (uconst_val + 1);
7056 break;
7058 case GEU:
7059 if (uconst_val != 0)
7060 code = GTU, op1 = GEN_INT (uconst_val - 1);
7061 break;
7063 default:
7064 break;
7068 /* If this was floating-point and we reversed anything other than an
7069 EQ or NE, return zero. */
7070 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
7071 && did_reverse_condition && code != NE && code != EQ
7072 && ! flag_fast_math
7073 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
7074 return 0;
7076 #ifdef HAVE_cc0
7077 /* Never return CC0; return zero instead. */
7078 if (op0 == cc0_rtx)
7079 return 0;
7080 #endif
7082 return gen_rtx (code, VOIDmode, op0, op1);
7085 /* Similar to above routine, except that we also put an invariant last
7086 unless both operands are invariants. */
7089 get_condition_for_loop (x)
7090 rtx x;
7092 rtx comparison = get_condition (x, NULL_PTR);
7094 if (comparison == 0
7095 || ! invariant_p (XEXP (comparison, 0))
7096 || invariant_p (XEXP (comparison, 1)))
7097 return comparison;
7099 return gen_rtx (swap_condition (GET_CODE (comparison)), VOIDmode,
7100 XEXP (comparison, 1), XEXP (comparison, 0));
7103 #ifdef HAIFA
7104 /* Analyze a loop in order to instrument it with the use of count register.
7105 loop_start and loop_end are the first and last insns of the loop.
7106 This function works in cooperation with insert_bct ().
7107 loop_can_insert_bct[loop_num] is set according to whether the optimization
7108 is applicable to the loop. When it is applicable, the following variables
7109 are also set:
7110 loop_start_value[loop_num]
7111 loop_comparison_value[loop_num]
7112 loop_increment[loop_num]
7113 loop_comparison_code[loop_num] */
7115 static
7116 void analyze_loop_iterations (loop_start, loop_end)
7117 rtx loop_start, loop_end;
7119 rtx comparison, comparison_value;
7120 rtx iteration_var, initial_value, increment;
7121 enum rtx_code comparison_code;
7123 rtx last_loop_insn;
7124 rtx insn;
7125 int i;
7127 /* loop_variable mode */
7128 enum machine_mode original_mode;
7130 /* find the number of the loop */
7131 int loop_num = uid_loop_num [INSN_UID (loop_start)];
7133 /* we change our mind only when we are sure that loop will be instrumented */
7134 loop_can_insert_bct[loop_num] = 0;
7136 /* is the optimization suppressed. */
7137 if ( !flag_branch_on_count_reg )
7138 return;
7140 /* make sure that count-reg is not in use */
7141 if (loop_used_count_register[loop_num]){
7142 if (loop_dump_stream)
7143 fprintf (loop_dump_stream,
7144 "analyze_loop_iterations %d: BCT instrumentation failed: count register already in use\n",
7145 loop_num);
7146 return;
7149 /* make sure that the function has no indirect jumps. */
7150 if (indirect_jump_in_function){
7151 if (loop_dump_stream)
7152 fprintf (loop_dump_stream,
7153 "analyze_loop_iterations %d: BCT instrumentation failed: indirect jump in function\n",
7154 loop_num);
7155 return;
7158 /* make sure that the last loop insn is a conditional jump */
7159 last_loop_insn = PREV_INSN (loop_end);
7160 if (GET_CODE (last_loop_insn) != JUMP_INSN || !condjump_p (last_loop_insn)) {
7161 if (loop_dump_stream)
7162 fprintf (loop_dump_stream,
7163 "analyze_loop_iterations %d: BCT instrumentation failed: invalid jump at loop end\n",
7164 loop_num);
7165 return;
7168 /* First find the iteration variable. If the last insn is a conditional
7169 branch, and the insn preceding it tests a register value, make that
7170 register the iteration variable. */
7172 /* We used to use prev_nonnote_insn here, but that fails because it might
7173 accidentally get the branch for a contained loop if the branch for this
7174 loop was deleted. We can only trust branches immediately before the
7175 loop_end. */
7177 comparison = get_condition_for_loop (last_loop_insn);
7178 /* ??? Get_condition may switch position of induction variable and
7179 invariant register when it canonicalizes the comparison. */
7181 if (comparison == 0) {
7182 if (loop_dump_stream)
7183 fprintf (loop_dump_stream,
7184 "analyze_loop_iterations %d: BCT instrumentation failed: comparison not found\n",
7185 loop_num);
7186 return;
7189 comparison_code = GET_CODE (comparison);
7190 iteration_var = XEXP (comparison, 0);
7191 comparison_value = XEXP (comparison, 1);
7193 original_mode = GET_MODE (iteration_var);
7194 if (GET_MODE_CLASS (original_mode) != MODE_INT
7195 || GET_MODE_SIZE (original_mode) != UNITS_PER_WORD) {
7196 if (loop_dump_stream)
7197 fprintf (loop_dump_stream,
7198 "analyze_loop_iterations %d: BCT Instrumentation failed: loop variable not integer\n",
7199 loop_num);
7200 return;
7203 /* get info about loop bounds and increment */
7204 iteration_info (iteration_var, &initial_value, &increment,
7205 loop_start, loop_end);
7207 /* make sure that all required loop data were found */
7208 if (!(initial_value && increment && comparison_value
7209 && invariant_p (comparison_value) && invariant_p (increment)
7210 && ! indirect_jump_in_function))
7212 if (loop_dump_stream) {
7213 fprintf (loop_dump_stream,
7214 "analyze_loop_iterations %d: BCT instrumentation failed because of wrong loop: ", loop_num);
7215 if (!(initial_value && increment && comparison_value)) {
7216 fprintf (loop_dump_stream, "\tbounds not available: ");
7217 if ( ! initial_value )
7218 fprintf (loop_dump_stream, "initial ");
7219 if ( ! increment )
7220 fprintf (loop_dump_stream, "increment ");
7221 if ( ! comparison_value )
7222 fprintf (loop_dump_stream, "comparison ");
7223 fprintf (loop_dump_stream, "\n");
7225 if (!invariant_p (comparison_value) || !invariant_p (increment))
7226 fprintf (loop_dump_stream, "\tloop bounds not invariant\n");
7228 return;
7231 /* make sure that the increment is constant */
7232 if (GET_CODE (increment) != CONST_INT) {
7233 if (loop_dump_stream)
7234 fprintf (loop_dump_stream,
7235 "analyze_loop_iterations %d: instrumentation failed: not arithmetic loop\n",
7236 loop_num);
7237 return;
7240 /* make sure that the loop contains neither function call, nor jump on table.
7241 (the count register might be altered by the called function, and might
7242 be used for a branch on table). */
7243 for (insn = loop_start; insn && insn != loop_end; insn = NEXT_INSN (insn)) {
7244 if (GET_CODE (insn) == CALL_INSN){
7245 if (loop_dump_stream)
7246 fprintf (loop_dump_stream,
7247 "analyze_loop_iterations %d: BCT instrumentation failed: function call in the loop\n",
7248 loop_num);
7249 return;
7252 if (GET_CODE (insn) == JUMP_INSN
7253 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
7254 || GET_CODE (PATTERN (insn)) == ADDR_VEC)){
7255 if (loop_dump_stream)
7256 fprintf (loop_dump_stream,
7257 "analyze_loop_iterations %d: BCT instrumentation failed: computed branch in the loop\n",
7258 loop_num);
7259 return;
7263 /* At this point, we are sure that the loop can be instrumented with BCT.
7264 Some of the loops, however, will not be instrumented - the final decision
7265 is taken by insert_bct () */
7266 if (loop_dump_stream)
7267 fprintf (loop_dump_stream,
7268 "analyze_loop_iterations: loop (luid =%d) can be BCT instrumented.\n",
7269 loop_num);
7271 /* mark all enclosing loops that they cannot use count register */
7272 /* ???: In fact, since insert_bct may decide not to instrument this loop,
7273 marking here may prevent instrumenting an enclosing loop that could
7274 actually be instrumented. But since this is rare, it is safer to mark
7275 here in case the order of calling (analyze/insert)_bct would be changed. */
7276 for (i=loop_num; i != -1; i = loop_outer_loop[i])
7277 loop_used_count_register[i] = 1;
7279 /* Set data structures which will be used by the instrumentation phase */
7280 loop_start_value[loop_num] = initial_value;
7281 loop_comparison_value[loop_num] = comparison_value;
7282 loop_increment[loop_num] = increment;
7283 loop_comparison_code[loop_num] = comparison_code;
7284 loop_can_insert_bct[loop_num] = 1;
7288 /* instrument loop for insertion of bct instruction. We distinguish between
7289 loops with compile-time bounds, to those with run-time bounds. The loop
7290 behaviour is analized according to the following characteristics/variables:
7291 ; Input variables:
7292 ; comparison-value: the value to which the iteration counter is compared.
7293 ; initial-value: iteration-counter initial value.
7294 ; increment: iteration-counter increment.
7295 ; Computed variables:
7296 ; increment-direction: the sign of the increment.
7297 ; compare-direction: '1' for GT, GTE, '-1' for LT, LTE, '0' for NE.
7298 ; range-direction: sign (comparison-value - initial-value)
7299 We give up on the following cases:
7300 ; loop variable overflow.
7301 ; run-time loop bounds with comparison code NE.
7304 static void
7305 insert_bct (loop_start, loop_end)
7306 rtx loop_start, loop_end;
7308 rtx initial_value, comparison_value, increment;
7309 enum rtx_code comparison_code;
7311 int increment_direction, compare_direction;
7312 int unsigned_p = 0;
7314 /* if the loop condition is <= or >=, the number of iteration
7315 is 1 more than the range of the bounds of the loop */
7316 int add_iteration = 0;
7318 /* the only machine mode we work with - is the integer of the size that the
7319 machine has */
7320 enum machine_mode loop_var_mode = SImode;
7322 int loop_num = uid_loop_num [INSN_UID (loop_start)];
7324 /* get loop-variables. No need to check that these are valid - already
7325 checked in analyze_loop_iterations (). */
7326 comparison_code = loop_comparison_code[loop_num];
7327 initial_value = loop_start_value[loop_num];
7328 comparison_value = loop_comparison_value[loop_num];
7329 increment = loop_increment[loop_num];
7331 /* check analyze_loop_iterations decision for this loop. */
7332 if (! loop_can_insert_bct[loop_num]){
7333 if (loop_dump_stream)
7334 fprintf (loop_dump_stream,
7335 "insert_bct: [%d] - was decided not to instrument by analyze_loop_iterations ()\n",
7336 loop_num);
7337 return;
7340 /* It's impossible to instrument a competely unrolled loop. */
7341 if (loop_unroll_factor [loop_num] == -1)
7342 return;
7344 /* make sure that the last loop insn is a conditional jump .
7345 This check is repeated from analyze_loop_iterations (),
7346 because unrolling might have changed that. */
7347 if (GET_CODE (PREV_INSN (loop_end)) != JUMP_INSN
7348 || !condjump_p (PREV_INSN (loop_end))) {
7349 if (loop_dump_stream)
7350 fprintf (loop_dump_stream,
7351 "insert_bct: not instrumenting BCT because of invalid branch\n");
7352 return;
7355 /* fix increment in case loop was unrolled. */
7356 if (loop_unroll_factor [loop_num] > 1)
7357 increment = GEN_INT ( INTVAL (increment) * loop_unroll_factor [loop_num] );
7359 /* determine properties and directions of the loop */
7360 increment_direction = (INTVAL (increment) > 0) ? 1:-1;
7361 switch ( comparison_code ) {
7362 case LEU:
7363 unsigned_p = 1;
7364 /* fallthrough */
7365 case LE:
7366 compare_direction = 1;
7367 add_iteration = 1;
7368 break;
7369 case GEU:
7370 unsigned_p = 1;
7371 /* fallthrough */
7372 case GE:
7373 compare_direction = -1;
7374 add_iteration = 1;
7375 break;
7376 case EQ:
7377 /* in this case we cannot know the number of iterations */
7378 if (loop_dump_stream)
7379 fprintf (loop_dump_stream,
7380 "insert_bct: %d: loop cannot be instrumented: == in condition\n",
7381 loop_num);
7382 return;
7383 case LTU:
7384 unsigned_p = 1;
7385 /* fallthrough */
7386 case LT:
7387 compare_direction = 1;
7388 break;
7389 case GTU:
7390 unsigned_p = 1;
7391 /* fallthrough */
7392 case GT:
7393 compare_direction = -1;
7394 break;
7395 case NE:
7396 compare_direction = 0;
7397 break;
7398 default:
7399 abort ();
7403 /* make sure that the loop does not end by an overflow */
7404 if (compare_direction != increment_direction) {
7405 if (loop_dump_stream)
7406 fprintf (loop_dump_stream,
7407 "insert_bct: %d: loop cannot be instrumented: terminated by overflow\n",
7408 loop_num);
7409 return;
7412 /* try to instrument the loop. */
7414 /* Handle the simpler case, where the bounds are known at compile time. */
7415 if (GET_CODE (initial_value) == CONST_INT && GET_CODE (comparison_value) == CONST_INT)
7417 int n_iterations;
7418 int increment_value_abs = INTVAL (increment) * increment_direction;
7420 /* check the relation between compare-val and initial-val */
7421 int difference = INTVAL (comparison_value) - INTVAL (initial_value);
7422 int range_direction = (difference > 0) ? 1 : -1;
7424 /* make sure the loop executes enough iterations to gain from BCT */
7425 if (difference > -3 && difference < 3) {
7426 if (loop_dump_stream)
7427 fprintf (loop_dump_stream,
7428 "insert_bct: loop %d not BCT instrumented: too small iteration count.\n",
7429 loop_num);
7430 return;
7433 /* make sure that the loop executes at least once */
7434 if ((range_direction == 1 && compare_direction == -1)
7435 || (range_direction == -1 && compare_direction == 1))
7437 if (loop_dump_stream)
7438 fprintf (loop_dump_stream,
7439 "insert_bct: loop %d: does not iterate even once. Not instrumenting.\n",
7440 loop_num);
7441 return;
7444 /* make sure that the loop does not end by an overflow (in compile time
7445 bounds we must have an additional check for overflow, because here
7446 we also support the compare code of 'NE'. */
7447 if (comparison_code == NE
7448 && increment_direction != range_direction) {
7449 if (loop_dump_stream)
7450 fprintf (loop_dump_stream,
7451 "insert_bct (compile time bounds): %d: loop not instrumented: terminated by overflow\n",
7452 loop_num);
7453 return;
7456 /* Determine the number of iterations by:
7458 ; compare-val - initial-val + (increment -1) + additional-iteration
7459 ; num_iterations = -----------------------------------------------------------------
7460 ; increment
7462 difference = (range_direction > 0) ? difference : -difference;
7463 #if 0
7464 fprintf (stderr, "difference is: %d\n", difference); /* @*/
7465 fprintf (stderr, "increment_value_abs is: %d\n", increment_value_abs); /* @*/
7466 fprintf (stderr, "add_iteration is: %d\n", add_iteration); /* @*/
7467 fprintf (stderr, "INTVAL (comparison_value) is: %d\n", INTVAL (comparison_value)); /* @*/
7468 fprintf (stderr, "INTVAL (initial_value) is: %d\n", INTVAL (initial_value)); /* @*/
7469 #endif
7471 if (increment_value_abs == 0) {
7472 fprintf (stderr, "insert_bct: error: increment == 0 !!!\n");
7473 abort ();
7475 n_iterations = (difference + increment_value_abs - 1 + add_iteration)
7476 / increment_value_abs;
7478 #if 0
7479 fprintf (stderr, "number of iterations is: %d\n", n_iterations); /* @*/
7480 #endif
7481 instrument_loop_bct (loop_start, loop_end, GEN_INT (n_iterations));
7483 /* Done with this loop. */
7484 return;
7487 /* Handle the more complex case, that the bounds are NOT known at compile time. */
7488 /* In this case we generate run_time calculation of the number of iterations */
7490 /* With runtime bounds, if the compare is of the form '!=' we give up */
7491 if (comparison_code == NE) {
7492 if (loop_dump_stream)
7493 fprintf (loop_dump_stream,
7494 "insert_bct: fail for loop %d: runtime bounds with != comparison\n",
7495 loop_num);
7496 return;
7499 else {
7500 /* We rely on the existence of run-time guard to ensure that the
7501 loop executes at least once. */
7502 rtx sequence;
7503 rtx iterations_num_reg;
7505 int increment_value_abs = INTVAL (increment) * increment_direction;
7507 /* make sure that the increment is a power of two, otherwise (an
7508 expensive) divide is needed. */
7509 if (exact_log2 (increment_value_abs) == -1)
7511 if (loop_dump_stream)
7512 fprintf (loop_dump_stream,
7513 "insert_bct: not instrumenting BCT because the increment is not power of 2\n");
7514 return;
7517 /* compute the number of iterations */
7518 start_sequence ();
7520 /* CYGNUS LOCAL: HAIFA bug fix */
7521 rtx temp_reg;
7523 /* Again, the number of iterations is calculated by:
7525 ; compare-val - initial-val + (increment -1) + additional-iteration
7526 ; num_iterations = -----------------------------------------------------------------
7527 ; increment
7529 /* ??? Do we have to call copy_rtx here before passing rtx to
7530 expand_binop? */
7531 if (compare_direction > 0) {
7532 /* <, <= :the loop variable is increasing */
7533 temp_reg = expand_binop (loop_var_mode, sub_optab, comparison_value,
7534 initial_value, NULL_RTX, 0, OPTAB_LIB_WIDEN);
7536 else {
7537 temp_reg = expand_binop (loop_var_mode, sub_optab, initial_value,
7538 comparison_value, NULL_RTX, 0, OPTAB_LIB_WIDEN);
7541 if (increment_value_abs - 1 + add_iteration != 0)
7542 temp_reg = expand_binop (loop_var_mode, add_optab, temp_reg,
7543 GEN_INT (increment_value_abs - 1 + add_iteration),
7544 NULL_RTX, 0, OPTAB_LIB_WIDEN);
7546 if (increment_value_abs != 1)
7548 /* ??? This will generate an expensive divide instruction for
7549 most targets. The original authors apparently expected this
7550 to be a shift, since they test for power-of-2 divisors above,
7551 but just naively generating a divide instruction will not give
7552 a shift. It happens to work for the PowerPC target because
7553 the rs6000.md file has a divide pattern that emits shifts.
7554 It will probably not work for any other target. */
7555 iterations_num_reg = expand_binop (loop_var_mode, sdiv_optab,
7556 temp_reg,
7557 GEN_INT (increment_value_abs),
7558 NULL_RTX, 0, OPTAB_LIB_WIDEN);
7560 else
7561 iterations_num_reg = temp_reg;
7562 /* END CYGNUS LOCAL: HAIFA bug fix */
7564 sequence = gen_sequence ();
7565 end_sequence ();
7566 emit_insn_before (sequence, loop_start);
7567 instrument_loop_bct (loop_start, loop_end, iterations_num_reg);
7571 /* instrument loop by inserting a bct in it. This is done in the following way:
7572 1. A new register is created and assigned the hard register number of the count
7573 register.
7574 2. In the head of the loop the new variable is initialized by the value passed in the
7575 loop_num_iterations parameter.
7576 3. At the end of the loop, comparison of the register with 0 is generated.
7577 The created comparison follows the pattern defined for the
7578 decrement_and_branch_on_count insn, so this insn will be generated in assembly
7579 generation phase.
7580 4. The compare&branch on the old variable is deleted. So, if the loop-variable was
7581 not used elsewhere, it will be eliminated by data-flow analisys. */
7583 static void
7584 instrument_loop_bct (loop_start, loop_end, loop_num_iterations)
7585 rtx loop_start, loop_end;
7586 rtx loop_num_iterations;
7588 rtx temp_reg1, temp_reg2;
7589 rtx start_label;
7591 rtx sequence;
7592 enum machine_mode loop_var_mode = SImode;
7594 #ifdef HAVE_decrement_and_branch_on_count
7595 if (HAVE_decrement_and_branch_on_count)
7597 if (loop_dump_stream)
7598 fprintf (loop_dump_stream, "Loop: Inserting BCT\n");
7600 /* eliminate the check on the old variable */
7601 delete_insn (PREV_INSN (loop_end));
7602 delete_insn (PREV_INSN (loop_end));
7604 /* insert the label which will delimit the start of the loop */
7605 start_label = gen_label_rtx ();
7606 emit_label_after (start_label, loop_start);
7608 /* insert initialization of the count register into the loop header */
7609 start_sequence ();
7610 temp_reg1 = gen_reg_rtx (loop_var_mode);
7611 emit_insn (gen_move_insn (temp_reg1, loop_num_iterations));
7613 /* this will be count register */
7614 temp_reg2 = gen_rtx (REG, loop_var_mode, COUNT_REGISTER_REGNUM);
7615 /* we have to move the value to the count register from an GPR
7616 because rtx pointed to by loop_num_iterations could contain
7617 expression which cannot be moved into count register */
7618 emit_insn (gen_move_insn (temp_reg2, temp_reg1));
7620 sequence = gen_sequence ();
7621 end_sequence ();
7622 emit_insn_after (sequence, loop_start);
7624 /* insert new comparison on the count register instead of the
7625 old one, generating the needed BCT pattern (that will be
7626 later recognized by assembly generation phase). */
7627 emit_jump_insn_before (gen_decrement_and_branch_on_count (temp_reg2, start_label),
7628 loop_end);
7629 LABEL_NUSES (start_label)++;
7632 #endif /* HAVE_decrement_and_branch_on_count */
7634 #endif /* HAIFA */
7636 /* Scan the function and determine whether it has indirect (computed) jumps.
7638 This is taken mostly from flow.c; similar code exists elsewhere
7639 in the compiler. It may be useful to put this into rtlanal.c. */
7640 static int
7641 indirect_jump_in_function_p (start)
7642 rtx start;
7644 rtx insn;
7645 int is_indirect_jump = 0;
7647 for (insn = start; insn; insn = NEXT_INSN (insn))
7648 if (computed_jump_p (insn))
7649 return 1;
7651 return 0;