Do not do src->dest copy if register would not be allocated a normal register
[official-gcc.git] / gcc / loop.c
blobac06b1e41cff47afba3f365271813962be5a7f37
1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 88, 89, 91-97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This is the loop optimization pass of the compiler.
23 It finds invariant computations within loops and moves them
24 to the beginning of the loop. Then it identifies basic and
25 general induction variables. Strength reduction is applied to the general
26 induction variables, and induction variable elimination is applied to
27 the basic induction variables.
29 It also finds cases where
30 a register is set within the loop by zero-extending a narrower value
31 and changes these to zero the entire register once before the loop
32 and merely copy the low part within the loop.
34 Most of the complexity is in heuristics to decide when it is worth
35 while to do these things. */
37 #include "config.h"
38 #include "system.h"
39 #include "rtl.h"
40 #include "obstack.h"
41 #include "expr.h"
42 #include "insn-config.h"
43 #include "insn-flags.h"
44 #include "regs.h"
45 #include "hard-reg-set.h"
46 #include "recog.h"
47 #include "flags.h"
48 #include "real.h"
49 #include "loop.h"
50 #include "except.h"
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 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 move_insn_first:1;/* Same as above, if this is necessary for the
266 first insn of a consecutive sets group. */
267 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
268 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
269 that we should avoid changing when clearing
270 the rest of the reg. */
271 struct movable *match; /* First entry for same value */
272 struct movable *forces; /* An insn that must be moved if this is */
273 struct movable *next;
276 FILE *loop_dump_stream;
278 /* Forward declarations. */
280 static void find_and_verify_loops PROTO((rtx));
281 static void mark_loop_jump PROTO((rtx, int));
282 static void prescan_loop PROTO((rtx, rtx));
283 static int reg_in_basic_block_p PROTO((rtx, rtx));
284 static int consec_sets_invariant_p PROTO((rtx, int, rtx));
285 static rtx libcall_other_reg PROTO((rtx, rtx));
286 static int labels_in_range_p PROTO((rtx, int));
287 static void count_loop_regs_set PROTO((rtx, rtx, char *, rtx *, int *, int));
288 static void note_addr_stored PROTO((rtx, rtx));
289 static int loop_reg_used_before_p PROTO((rtx, rtx, rtx, rtx, rtx));
290 static void scan_loop PROTO((rtx, rtx, int, int));
291 #if 0
292 static void replace_call_address PROTO(());
293 #endif
294 static rtx skip_consec_insns PROTO((rtx, int));
295 static int libcall_benefit PROTO((rtx));
296 static void ignore_some_movables PROTO((struct movable *));
297 static void force_movables PROTO((struct movable *));
298 static void combine_movables PROTO((struct movable *, int));
299 static int regs_match_p PROTO((rtx, rtx, struct movable *));
300 static int rtx_equal_for_loop_p PROTO((rtx, rtx, struct movable *));
301 static void add_label_notes PROTO((rtx, rtx));
302 static void move_movables PROTO((struct movable *, int, int, rtx, rtx, int));
303 static int count_nonfixed_reads PROTO((rtx));
304 static void strength_reduce PROTO((rtx, rtx, rtx, int, rtx, rtx, int));
305 static void find_single_use_in_loop PROTO((rtx, rtx, rtx *));
306 static int valid_initial_value_p PROTO((rtx, rtx, int, rtx));
307 static void find_mem_givs PROTO((rtx, rtx, int, rtx, rtx));
308 static void record_biv PROTO((struct induction *, rtx, rtx, rtx, rtx, int, int));
309 static void check_final_value PROTO((struct induction *, rtx, rtx));
310 static void record_giv PROTO((struct induction *, rtx, rtx, rtx, rtx, rtx, int, enum g_types, int, rtx *, rtx, rtx));
311 static void update_giv_derive PROTO((rtx));
312 static int basic_induction_var PROTO((rtx, enum machine_mode, rtx, rtx, rtx *, rtx *));
313 static rtx simplify_giv_expr PROTO((rtx, int *));
314 static int general_induction_var PROTO((rtx, rtx *, rtx *, rtx *));
315 static int consec_sets_giv PROTO((int, rtx, rtx, rtx, rtx *, rtx *));
316 static int check_dbra_loop PROTO((rtx, int, rtx));
317 #ifdef ADDRESS_COST
318 static rtx express_from PROTO((struct induction *, struct induction *));
319 #endif
320 static int combine_givs_p PROTO((struct induction *, struct induction *));
321 #ifdef GIV_SORT_CRITERION
322 static int giv_sort PROTO((struct induction **, struct induction **));
323 #endif
324 static void combine_givs PROTO((struct iv_class *));
325 static int product_cheap_p PROTO((rtx, rtx));
326 static int maybe_eliminate_biv PROTO((struct iv_class *, rtx, rtx, int, int, int));
327 static int maybe_eliminate_biv_1 PROTO((rtx, rtx, struct iv_class *, int, rtx));
328 static int last_use_this_basic_block PROTO((rtx, rtx));
329 static void record_initial PROTO((rtx, rtx));
330 static void update_reg_last_use PROTO((rtx, rtx));
332 #ifdef HAIFA
333 /* This is extern from unroll.c */
334 extern void iteration_info PROTO((rtx, rtx *, rtx *, rtx, rtx));
336 /* Two main functions for implementing bct:
337 first - to be called before loop unrolling, and the second - after */
338 #ifdef HAVE_decrement_and_branch_on_count
339 static void analyze_loop_iterations PROTO((rtx, rtx));
340 static void insert_bct PROTO((rtx, rtx));
342 /* Auxiliary function that inserts the bct pattern into the loop */
343 static void instrument_loop_bct PROTO((rtx, rtx, rtx));
344 #endif /* HAVE_decrement_and_branch_on_count */
345 #endif /* HAIFA */
347 /* Indirect_jump_in_function is computed once per function. */
348 int indirect_jump_in_function = 0;
349 static int indirect_jump_in_function_p PROTO((rtx));
352 /* Relative gain of eliminating various kinds of operations. */
353 int add_cost;
354 #if 0
355 int shift_cost;
356 int mult_cost;
357 #endif
359 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
360 copy the value of the strength reduced giv to its original register. */
361 int copy_cost;
363 void
364 init_loop ()
366 char *free_point = (char *) oballoc (1);
367 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
369 add_cost = rtx_cost (gen_rtx_PLUS (word_mode, reg, reg), SET);
371 /* We multiply by 2 to reconcile the difference in scale between
372 these two ways of computing costs. Otherwise the cost of a copy
373 will be far less than the cost of an add. */
375 copy_cost = 2 * 2;
377 /* Free the objects we just allocated. */
378 obfree (free_point);
380 /* Initialize the obstack used for rtl in product_cheap_p. */
381 gcc_obstack_init (&temp_obstack);
384 /* Entry point of this file. Perform loop optimization
385 on the current function. F is the first insn of the function
386 and DUMPFILE is a stream for output of a trace of actions taken
387 (or 0 if none should be output). */
389 void
390 loop_optimize (f, dumpfile, unroll_p)
391 /* f is the first instruction of a chain of insns for one function */
392 rtx f;
393 FILE *dumpfile;
394 int unroll_p;
396 register rtx insn;
397 register int i;
398 rtx last_insn;
400 loop_dump_stream = dumpfile;
402 init_recog_no_volatile ();
404 max_reg_before_loop = max_reg_num ();
406 moved_once = (char *) alloca (max_reg_before_loop);
407 bzero (moved_once, max_reg_before_loop);
409 regs_may_share = 0;
411 /* Count the number of loops. */
413 max_loop_num = 0;
414 for (insn = f; insn; insn = NEXT_INSN (insn))
416 if (GET_CODE (insn) == NOTE
417 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
418 max_loop_num++;
421 /* Don't waste time if no loops. */
422 if (max_loop_num == 0)
423 return;
425 /* Get size to use for tables indexed by uids.
426 Leave some space for labels allocated by find_and_verify_loops. */
427 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
429 uid_luid = (int *) alloca (max_uid_for_loop * sizeof (int));
430 uid_loop_num = (int *) alloca (max_uid_for_loop * sizeof (int));
432 bzero ((char *) uid_luid, max_uid_for_loop * sizeof (int));
433 bzero ((char *) uid_loop_num, max_uid_for_loop * sizeof (int));
435 /* Allocate tables for recording each loop. We set each entry, so they need
436 not be zeroed. */
437 loop_number_loop_starts = (rtx *) alloca (max_loop_num * sizeof (rtx));
438 loop_number_loop_ends = (rtx *) alloca (max_loop_num * sizeof (rtx));
439 loop_outer_loop = (int *) alloca (max_loop_num * sizeof (int));
440 loop_invalid = (char *) alloca (max_loop_num * sizeof (char));
441 loop_number_exit_labels = (rtx *) alloca (max_loop_num * sizeof (rtx));
442 loop_number_exit_count = (int *) alloca (max_loop_num * sizeof (int));
444 /* This is initialized by the unrolling code, so we go ahead
445 and clear them just in case we are not performing loop
446 unrolling. */
447 loop_unroll_factor = (int *) alloca (max_loop_num *sizeof (int));
448 bzero ((char *) loop_unroll_factor, max_loop_num * sizeof (int));
450 #ifdef HAIFA
451 /* Allocate for BCT optimization */
452 loop_can_insert_bct = (int *) alloca (max_loop_num * sizeof (int));
453 bzero ((char *) loop_can_insert_bct, max_loop_num * sizeof (int));
455 loop_used_count_register = (int *) alloca (max_loop_num * sizeof (int));
456 bzero ((char *) loop_used_count_register, max_loop_num * sizeof (int));
458 loop_increment = (rtx *) alloca (max_loop_num * sizeof (rtx));
459 loop_comparison_value = (rtx *) alloca (max_loop_num * sizeof (rtx));
460 loop_start_value = (rtx *) alloca (max_loop_num * sizeof (rtx));
461 bzero ((char *) loop_increment, max_loop_num * sizeof (rtx));
462 bzero ((char *) loop_comparison_value, max_loop_num * sizeof (rtx));
463 bzero ((char *) loop_start_value, max_loop_num * sizeof (rtx));
465 loop_comparison_code
466 = (enum rtx_code *) alloca (max_loop_num * sizeof (enum rtx_code));
467 bzero ((char *) loop_comparison_code, max_loop_num * sizeof (enum rtx_code));
468 #endif /* HAIFA */
470 /* Find and process each loop.
471 First, find them, and record them in order of their beginnings. */
472 find_and_verify_loops (f);
474 /* Now find all register lifetimes. This must be done after
475 find_and_verify_loops, because it might reorder the insns in the
476 function. */
477 reg_scan (f, max_reg_num (), 1);
479 /* This must occur after reg_scan so that registers created by gcse
480 will have entries in the register tables.
482 We could have added a call to reg_scan after gcse_main in toplev.c,
483 but moving this call to init_alias_analysis is more efficient. */
484 init_alias_analysis ();
486 /* See if we went too far. */
487 if (get_max_uid () > max_uid_for_loop)
488 abort ();
489 /* Now reset it to the actual size we need. See above. */
490 max_uid_for_loop = get_max_uid () + 1;
492 /* Compute the mapping from uids to luids.
493 LUIDs are numbers assigned to insns, like uids,
494 except that luids increase monotonically through the code.
495 Don't assign luids to line-number NOTEs, so that the distance in luids
496 between two insns is not affected by -g. */
498 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
500 last_insn = insn;
501 if (GET_CODE (insn) != NOTE
502 || NOTE_LINE_NUMBER (insn) <= 0)
503 uid_luid[INSN_UID (insn)] = ++i;
504 else
505 /* Give a line number note the same luid as preceding insn. */
506 uid_luid[INSN_UID (insn)] = i;
509 max_luid = i + 1;
511 /* Don't leave gaps in uid_luid for insns that have been
512 deleted. It is possible that the first or last insn
513 using some register has been deleted by cross-jumping.
514 Make sure that uid_luid for that former insn's uid
515 points to the general area where that insn used to be. */
516 for (i = 0; i < max_uid_for_loop; i++)
518 uid_luid[0] = uid_luid[i];
519 if (uid_luid[0] != 0)
520 break;
522 for (i = 0; i < max_uid_for_loop; i++)
523 if (uid_luid[i] == 0)
524 uid_luid[i] = uid_luid[i - 1];
526 /* Create a mapping from loops to BLOCK tree nodes. */
527 if (unroll_p && write_symbols != NO_DEBUG)
528 find_loop_tree_blocks ();
530 /* Determine if the function has indirect jump. On some systems
531 this prevents low overhead loop instructions from being used. */
532 indirect_jump_in_function = indirect_jump_in_function_p (f);
534 /* Now scan the loops, last ones first, since this means inner ones are done
535 before outer ones. */
536 for (i = max_loop_num-1; i >= 0; i--)
537 if (! loop_invalid[i] && loop_number_loop_ends[i])
538 scan_loop (loop_number_loop_starts[i], loop_number_loop_ends[i],
539 max_reg_num (), unroll_p);
541 /* If debugging and unrolling loops, we must replicate the tree nodes
542 corresponding to the blocks inside the loop, so that the original one
543 to one mapping will remain. */
544 if (unroll_p && write_symbols != NO_DEBUG)
545 unroll_block_trees ();
548 /* Optimize one loop whose start is LOOP_START and end is END.
549 LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
550 NOTE_INSN_LOOP_END. */
552 /* ??? Could also move memory writes out of loops if the destination address
553 is invariant, the source is invariant, the memory write is not volatile,
554 and if we can prove that no read inside the loop can read this address
555 before the write occurs. If there is a read of this address after the
556 write, then we can also mark the memory read as invariant. */
558 static void
559 scan_loop (loop_start, end, nregs, unroll_p)
560 rtx loop_start, end;
561 int nregs;
562 int unroll_p;
564 register int i;
565 register rtx p;
566 /* 1 if we are scanning insns that could be executed zero times. */
567 int maybe_never = 0;
568 /* 1 if we are scanning insns that might never be executed
569 due to a subroutine call which might exit before they are reached. */
570 int call_passed = 0;
571 /* For a rotated loop that is entered near the bottom,
572 this is the label at the top. Otherwise it is zero. */
573 rtx loop_top = 0;
574 /* Jump insn that enters the loop, or 0 if control drops in. */
575 rtx loop_entry_jump = 0;
576 /* Place in the loop where control enters. */
577 rtx scan_start;
578 /* Number of insns in the loop. */
579 int insn_count;
580 int in_libcall = 0;
581 int tem;
582 rtx temp;
583 /* The SET from an insn, if it is the only SET in the insn. */
584 rtx set, set1;
585 /* Chain describing insns movable in current loop. */
586 struct movable *movables = 0;
587 /* Last element in `movables' -- so we can add elements at the end. */
588 struct movable *last_movable = 0;
589 /* Ratio of extra register life span we can justify
590 for saving an instruction. More if loop doesn't call subroutines
591 since in that case saving an insn makes more difference
592 and more registers are available. */
593 int threshold;
594 /* If we have calls, contains the insn in which a register was used
595 if it was used exactly once; contains const0_rtx if it was used more
596 than once. */
597 rtx *reg_single_usage = 0;
598 /* Nonzero if we are scanning instructions in a sub-loop. */
599 int loop_depth = 0;
601 n_times_set = (int *) alloca (nregs * sizeof (int));
602 n_times_used = (int *) alloca (nregs * sizeof (int));
603 may_not_optimize = (char *) alloca (nregs);
605 /* Determine whether this loop starts with a jump down to a test at
606 the end. This will occur for a small number of loops with a test
607 that is too complex to duplicate in front of the loop.
609 We search for the first insn or label in the loop, skipping NOTEs.
610 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
611 (because we might have a loop executed only once that contains a
612 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
613 (in case we have a degenerate loop).
615 Note that if we mistakenly think that a loop is entered at the top
616 when, in fact, it is entered at the exit test, the only effect will be
617 slightly poorer optimization. Making the opposite error can generate
618 incorrect code. Since very few loops now start with a jump to the
619 exit test, the code here to detect that case is very conservative. */
621 for (p = NEXT_INSN (loop_start);
622 p != end
623 && GET_CODE (p) != CODE_LABEL && GET_RTX_CLASS (GET_CODE (p)) != 'i'
624 && (GET_CODE (p) != NOTE
625 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
626 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
627 p = NEXT_INSN (p))
630 scan_start = p;
632 /* Set up variables describing this loop. */
633 prescan_loop (loop_start, end);
634 threshold = (loop_has_call ? 1 : 2) * (1 + n_non_fixed_regs);
636 /* If loop has a jump before the first label,
637 the true entry is the target of that jump.
638 Start scan from there.
639 But record in LOOP_TOP the place where the end-test jumps
640 back to so we can scan that after the end of the loop. */
641 if (GET_CODE (p) == JUMP_INSN)
643 loop_entry_jump = p;
645 /* Loop entry must be unconditional jump (and not a RETURN) */
646 if (simplejump_p (p)
647 && JUMP_LABEL (p) != 0
648 /* Check to see whether the jump actually
649 jumps out of the loop (meaning it's no loop).
650 This case can happen for things like
651 do {..} while (0). If this label was generated previously
652 by loop, we can't tell anything about it and have to reject
653 the loop. */
654 && INSN_UID (JUMP_LABEL (p)) < max_uid_for_loop
655 && INSN_LUID (JUMP_LABEL (p)) >= INSN_LUID (loop_start)
656 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (end))
658 loop_top = next_label (scan_start);
659 scan_start = JUMP_LABEL (p);
663 /* If SCAN_START was an insn created by loop, we don't know its luid
664 as required by loop_reg_used_before_p. So skip such loops. (This
665 test may never be true, but it's best to play it safe.)
667 Also, skip loops where we do not start scanning at a label. This
668 test also rejects loops starting with a JUMP_INSN that failed the
669 test above. */
671 if (INSN_UID (scan_start) >= max_uid_for_loop
672 || GET_CODE (scan_start) != CODE_LABEL)
674 if (loop_dump_stream)
675 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
676 INSN_UID (loop_start), INSN_UID (end));
677 return;
680 /* Count number of times each reg is set during this loop.
681 Set may_not_optimize[I] if it is not safe to move out
682 the setting of register I. If this loop has calls, set
683 reg_single_usage[I]. */
685 bzero ((char *) n_times_set, nregs * sizeof (int));
686 bzero (may_not_optimize, nregs);
688 if (loop_has_call)
690 reg_single_usage = (rtx *) alloca (nregs * sizeof (rtx));
691 bzero ((char *) reg_single_usage, nregs * sizeof (rtx));
694 count_loop_regs_set (loop_top ? loop_top : loop_start, end,
695 may_not_optimize, reg_single_usage, &insn_count, nregs);
697 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
698 may_not_optimize[i] = 1, n_times_set[i] = 1;
699 bcopy ((char *) n_times_set, (char *) n_times_used, nregs * sizeof (int));
701 if (loop_dump_stream)
703 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
704 INSN_UID (loop_start), INSN_UID (end), insn_count);
705 if (loop_continue)
706 fprintf (loop_dump_stream, "Continue at insn %d.\n",
707 INSN_UID (loop_continue));
710 /* Scan through the loop finding insns that are safe to move.
711 Set n_times_set negative for the reg being set, so that
712 this reg will be considered invariant for subsequent insns.
713 We consider whether subsequent insns use the reg
714 in deciding whether it is worth actually moving.
716 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
717 and therefore it is possible that the insns we are scanning
718 would never be executed. At such times, we must make sure
719 that it is safe to execute the insn once instead of zero times.
720 When MAYBE_NEVER is 0, all insns will be executed at least once
721 so that is not a problem. */
723 p = scan_start;
724 while (1)
726 p = NEXT_INSN (p);
727 /* At end of a straight-in loop, we are done.
728 At end of a loop entered at the bottom, scan the top. */
729 if (p == scan_start)
730 break;
731 if (p == end)
733 if (loop_top != 0)
734 p = loop_top;
735 else
736 break;
737 if (p == scan_start)
738 break;
741 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
742 && find_reg_note (p, REG_LIBCALL, NULL_RTX))
743 in_libcall = 1;
744 else if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
745 && find_reg_note (p, REG_RETVAL, NULL_RTX))
746 in_libcall = 0;
748 if (GET_CODE (p) == INSN
749 && (set = single_set (p))
750 && GET_CODE (SET_DEST (set)) == REG
751 && ! may_not_optimize[REGNO (SET_DEST (set))])
753 int tem1 = 0;
754 int tem2 = 0;
755 int move_insn = 0;
756 rtx src = SET_SRC (set);
757 rtx dependencies = 0;
759 /* Figure out what to use as a source of this insn. If a REG_EQUIV
760 note is given or if a REG_EQUAL note with a constant operand is
761 specified, use it as the source and mark that we should move
762 this insn by calling emit_move_insn rather that duplicating the
763 insn.
765 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
766 is present. */
767 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
768 if (temp)
769 src = XEXP (temp, 0), move_insn = 1;
770 else
772 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
773 if (temp && CONSTANT_P (XEXP (temp, 0)))
774 src = XEXP (temp, 0), move_insn = 1;
775 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
777 src = XEXP (temp, 0);
778 /* A libcall block can use regs that don't appear in
779 the equivalent expression. To move the libcall,
780 we must move those regs too. */
781 dependencies = libcall_other_reg (p, src);
785 /* Don't try to optimize a register that was made
786 by loop-optimization for an inner loop.
787 We don't know its life-span, so we can't compute the benefit. */
788 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
790 /* In order to move a register, we need to have one of three cases:
791 (1) it is used only in the same basic block as the set
792 (2) it is not a user variable and it is not used in the
793 exit test (this can cause the variable to be used
794 before it is set just like a user-variable).
795 (3) the set is guaranteed to be executed once the loop starts,
796 and the reg is not used until after that. */
797 else if (! ((! maybe_never
798 && ! loop_reg_used_before_p (set, p, loop_start,
799 scan_start, end))
800 || (! REG_USERVAR_P (SET_DEST (set))
801 && ! REG_LOOP_TEST_P (SET_DEST (set)))
802 || reg_in_basic_block_p (p, SET_DEST (set))))
804 else if ((tem = invariant_p (src))
805 && (dependencies == 0
806 || (tem2 = invariant_p (dependencies)) != 0)
807 && (n_times_set[REGNO (SET_DEST (set))] == 1
808 || (tem1
809 = consec_sets_invariant_p (SET_DEST (set),
810 n_times_set[REGNO (SET_DEST (set))],
811 p)))
812 /* If the insn can cause a trap (such as divide by zero),
813 can't move it unless it's guaranteed to be executed
814 once loop is entered. Even a function call might
815 prevent the trap insn from being reached
816 (since it might exit!) */
817 && ! ((maybe_never || call_passed)
818 && may_trap_p (src)))
820 register struct movable *m;
821 register int regno = REGNO (SET_DEST (set));
823 /* A potential lossage is where we have a case where two insns
824 can be combined as long as they are both in the loop, but
825 we move one of them outside the loop. For large loops,
826 this can lose. The most common case of this is the address
827 of a function being called.
829 Therefore, if this register is marked as being used exactly
830 once if we are in a loop with calls (a "large loop"), see if
831 we can replace the usage of this register with the source
832 of this SET. If we can, delete this insn.
834 Don't do this if P has a REG_RETVAL note or if we have
835 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
837 if (reg_single_usage && reg_single_usage[regno] != 0
838 && reg_single_usage[regno] != const0_rtx
839 && REGNO_FIRST_UID (regno) == INSN_UID (p)
840 && (REGNO_LAST_UID (regno)
841 == INSN_UID (reg_single_usage[regno]))
842 && n_times_set[REGNO (SET_DEST (set))] == 1
843 && ! side_effects_p (SET_SRC (set))
844 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
845 && (! SMALL_REGISTER_CLASSES
846 || (! (GET_CODE (SET_SRC (set)) == REG
847 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
848 /* This test is not redundant; SET_SRC (set) might be
849 a call-clobbered register and the life of REGNO
850 might span a call. */
851 && ! modified_between_p (SET_SRC (set), p,
852 reg_single_usage[regno])
853 && no_labels_between_p (p, reg_single_usage[regno])
854 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
855 reg_single_usage[regno]))
857 /* Replace any usage in a REG_EQUAL note. Must copy the
858 new source, so that we don't get rtx sharing between the
859 SET_SOURCE and REG_NOTES of insn p. */
860 REG_NOTES (reg_single_usage[regno])
861 = replace_rtx (REG_NOTES (reg_single_usage[regno]),
862 SET_DEST (set), copy_rtx (SET_SRC (set)));
864 PUT_CODE (p, NOTE);
865 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
866 NOTE_SOURCE_FILE (p) = 0;
867 n_times_set[regno] = 0;
868 continue;
871 m = (struct movable *) alloca (sizeof (struct movable));
872 m->next = 0;
873 m->insn = p;
874 m->set_src = src;
875 m->dependencies = dependencies;
876 m->set_dest = SET_DEST (set);
877 m->force = 0;
878 m->consec = n_times_set[REGNO (SET_DEST (set))] - 1;
879 m->done = 0;
880 m->forces = 0;
881 m->partial = 0;
882 m->move_insn = move_insn;
883 m->move_insn_first = 0;
884 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
885 m->savemode = VOIDmode;
886 m->regno = regno;
887 /* Set M->cond if either invariant_p or consec_sets_invariant_p
888 returned 2 (only conditionally invariant). */
889 m->cond = ((tem | tem1 | tem2) > 1);
890 m->global = (uid_luid[REGNO_LAST_UID (regno)] > INSN_LUID (end)
891 || uid_luid[REGNO_FIRST_UID (regno)] < INSN_LUID (loop_start));
892 m->match = 0;
893 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
894 - uid_luid[REGNO_FIRST_UID (regno)]);
895 m->savings = n_times_used[regno];
896 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
897 m->savings += libcall_benefit (p);
898 n_times_set[regno] = move_insn ? -2 : -1;
899 /* Add M to the end of the chain MOVABLES. */
900 if (movables == 0)
901 movables = m;
902 else
903 last_movable->next = m;
904 last_movable = m;
906 if (m->consec > 0)
908 /* It is possible for the first instruction to have a
909 REG_EQUAL note but a non-invariant SET_SRC, so we must
910 remember the status of the first instruction in case
911 the last instruction doesn't have a REG_EQUAL note. */
912 m->move_insn_first = m->move_insn;
914 /* Skip this insn, not checking REG_LIBCALL notes. */
915 p = next_nonnote_insn (p);
916 /* Skip the consecutive insns, if there are any. */
917 p = skip_consec_insns (p, m->consec);
918 /* Back up to the last insn of the consecutive group. */
919 p = prev_nonnote_insn (p);
921 /* We must now reset m->move_insn, m->is_equiv, and possibly
922 m->set_src to correspond to the effects of all the
923 insns. */
924 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
925 if (temp)
926 m->set_src = XEXP (temp, 0), m->move_insn = 1;
927 else
929 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
930 if (temp && CONSTANT_P (XEXP (temp, 0)))
931 m->set_src = XEXP (temp, 0), m->move_insn = 1;
932 else
933 m->move_insn = 0;
936 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
939 /* If this register is always set within a STRICT_LOW_PART
940 or set to zero, then its high bytes are constant.
941 So clear them outside the loop and within the loop
942 just load the low bytes.
943 We must check that the machine has an instruction to do so.
944 Also, if the value loaded into the register
945 depends on the same register, this cannot be done. */
946 else if (SET_SRC (set) == const0_rtx
947 && GET_CODE (NEXT_INSN (p)) == INSN
948 && (set1 = single_set (NEXT_INSN (p)))
949 && GET_CODE (set1) == SET
950 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
951 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
952 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
953 == SET_DEST (set))
954 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
956 register int regno = REGNO (SET_DEST (set));
957 if (n_times_set[regno] == 2)
959 register struct movable *m;
960 m = (struct movable *) alloca (sizeof (struct movable));
961 m->next = 0;
962 m->insn = p;
963 m->set_dest = SET_DEST (set);
964 m->dependencies = 0;
965 m->force = 0;
966 m->consec = 0;
967 m->done = 0;
968 m->forces = 0;
969 m->move_insn = 0;
970 m->move_insn_first = 0;
971 m->partial = 1;
972 /* If the insn may not be executed on some cycles,
973 we can't clear the whole reg; clear just high part.
974 Not even if the reg is used only within this loop.
975 Consider this:
976 while (1)
977 while (s != t) {
978 if (foo ()) x = *s;
979 use (x);
981 Clearing x before the inner loop could clobber a value
982 being saved from the last time around the outer loop.
983 However, if the reg is not used outside this loop
984 and all uses of the register are in the same
985 basic block as the store, there is no problem.
987 If this insn was made by loop, we don't know its
988 INSN_LUID and hence must make a conservative
989 assumption. */
990 m->global = (INSN_UID (p) >= max_uid_for_loop
991 || (uid_luid[REGNO_LAST_UID (regno)]
992 > INSN_LUID (end))
993 || (uid_luid[REGNO_FIRST_UID (regno)]
994 < INSN_LUID (p))
995 || (labels_in_range_p
996 (p, uid_luid[REGNO_FIRST_UID (regno)])));
997 if (maybe_never && m->global)
998 m->savemode = GET_MODE (SET_SRC (set1));
999 else
1000 m->savemode = VOIDmode;
1001 m->regno = regno;
1002 m->cond = 0;
1003 m->match = 0;
1004 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
1005 - uid_luid[REGNO_FIRST_UID (regno)]);
1006 m->savings = 1;
1007 n_times_set[regno] = -1;
1008 /* Add M to the end of the chain MOVABLES. */
1009 if (movables == 0)
1010 movables = m;
1011 else
1012 last_movable->next = m;
1013 last_movable = m;
1017 /* Past a call insn, we get to insns which might not be executed
1018 because the call might exit. This matters for insns that trap.
1019 Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
1020 so they don't count. */
1021 else if (GET_CODE (p) == CALL_INSN && ! in_libcall)
1022 call_passed = 1;
1023 /* Past a label or a jump, we get to insns for which we
1024 can't count on whether or how many times they will be
1025 executed during each iteration. Therefore, we can
1026 only move out sets of trivial variables
1027 (those not used after the loop). */
1028 /* Similar code appears twice in strength_reduce. */
1029 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1030 /* If we enter the loop in the middle, and scan around to the
1031 beginning, don't set maybe_never for that. This must be an
1032 unconditional jump, otherwise the code at the top of the
1033 loop might never be executed. Unconditional jumps are
1034 followed a by barrier then loop end. */
1035 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop_top
1036 && NEXT_INSN (NEXT_INSN (p)) == end
1037 && simplejump_p (p)))
1038 maybe_never = 1;
1039 else if (GET_CODE (p) == NOTE)
1041 /* At the virtual top of a converted loop, insns are again known to
1042 be executed: logically, the loop begins here even though the exit
1043 code has been duplicated. */
1044 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1045 maybe_never = call_passed = 0;
1046 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1047 loop_depth++;
1048 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1049 loop_depth--;
1053 /* If one movable subsumes another, ignore that other. */
1055 ignore_some_movables (movables);
1057 /* For each movable insn, see if the reg that it loads
1058 leads when it dies right into another conditionally movable insn.
1059 If so, record that the second insn "forces" the first one,
1060 since the second can be moved only if the first is. */
1062 force_movables (movables);
1064 /* See if there are multiple movable insns that load the same value.
1065 If there are, make all but the first point at the first one
1066 through the `match' field, and add the priorities of them
1067 all together as the priority of the first. */
1069 combine_movables (movables, nregs);
1071 /* Now consider each movable insn to decide whether it is worth moving.
1072 Store 0 in n_times_set for each reg that is moved.
1074 Generally this increases code size, so do not move moveables when
1075 optimizing for code size. */
1077 if (! optimize_size)
1078 move_movables (movables, threshold,
1079 insn_count, loop_start, end, nregs);
1081 /* Now candidates that still are negative are those not moved.
1082 Change n_times_set to indicate that those are not actually invariant. */
1083 for (i = 0; i < nregs; i++)
1084 if (n_times_set[i] < 0)
1085 n_times_set[i] = n_times_used[i];
1087 if (flag_strength_reduce)
1088 strength_reduce (scan_start, end, loop_top,
1089 insn_count, loop_start, end, unroll_p);
1092 /* Add elements to *OUTPUT to record all the pseudo-regs
1093 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1095 void
1096 record_excess_regs (in_this, not_in_this, output)
1097 rtx in_this, not_in_this;
1098 rtx *output;
1100 enum rtx_code code;
1101 char *fmt;
1102 int i;
1104 code = GET_CODE (in_this);
1106 switch (code)
1108 case PC:
1109 case CC0:
1110 case CONST_INT:
1111 case CONST_DOUBLE:
1112 case CONST:
1113 case SYMBOL_REF:
1114 case LABEL_REF:
1115 return;
1117 case REG:
1118 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1119 && ! reg_mentioned_p (in_this, not_in_this))
1120 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1121 return;
1123 default:
1124 break;
1127 fmt = GET_RTX_FORMAT (code);
1128 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1130 int j;
1132 switch (fmt[i])
1134 case 'E':
1135 for (j = 0; j < XVECLEN (in_this, i); j++)
1136 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1137 break;
1139 case 'e':
1140 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1141 break;
1146 /* Check what regs are referred to in the libcall block ending with INSN,
1147 aside from those mentioned in the equivalent value.
1148 If there are none, return 0.
1149 If there are one or more, return an EXPR_LIST containing all of them. */
1151 static rtx
1152 libcall_other_reg (insn, equiv)
1153 rtx insn, equiv;
1155 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1156 rtx p = XEXP (note, 0);
1157 rtx output = 0;
1159 /* First, find all the regs used in the libcall block
1160 that are not mentioned as inputs to the result. */
1162 while (p != insn)
1164 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1165 || GET_CODE (p) == CALL_INSN)
1166 record_excess_regs (PATTERN (p), equiv, &output);
1167 p = NEXT_INSN (p);
1170 return output;
1173 /* Return 1 if all uses of REG
1174 are between INSN and the end of the basic block. */
1176 static int
1177 reg_in_basic_block_p (insn, reg)
1178 rtx insn, reg;
1180 int regno = REGNO (reg);
1181 rtx p;
1183 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1184 return 0;
1186 /* Search this basic block for the already recorded last use of the reg. */
1187 for (p = insn; p; p = NEXT_INSN (p))
1189 switch (GET_CODE (p))
1191 case NOTE:
1192 break;
1194 case INSN:
1195 case CALL_INSN:
1196 /* Ordinary insn: if this is the last use, we win. */
1197 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1198 return 1;
1199 break;
1201 case JUMP_INSN:
1202 /* Jump insn: if this is the last use, we win. */
1203 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1204 return 1;
1205 /* Otherwise, it's the end of the basic block, so we lose. */
1206 return 0;
1208 case CODE_LABEL:
1209 case BARRIER:
1210 /* It's the end of the basic block, so we lose. */
1211 return 0;
1213 default:
1214 break;
1218 /* The "last use" doesn't follow the "first use"?? */
1219 abort ();
1222 /* Compute the benefit of eliminating the insns in the block whose
1223 last insn is LAST. This may be a group of insns used to compute a
1224 value directly or can contain a library call. */
1226 static int
1227 libcall_benefit (last)
1228 rtx last;
1230 rtx insn;
1231 int benefit = 0;
1233 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1234 insn != last; insn = NEXT_INSN (insn))
1236 if (GET_CODE (insn) == CALL_INSN)
1237 benefit += 10; /* Assume at least this many insns in a library
1238 routine. */
1239 else if (GET_CODE (insn) == INSN
1240 && GET_CODE (PATTERN (insn)) != USE
1241 && GET_CODE (PATTERN (insn)) != CLOBBER)
1242 benefit++;
1245 return benefit;
1248 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1250 static rtx
1251 skip_consec_insns (insn, count)
1252 rtx insn;
1253 int count;
1255 for (; count > 0; count--)
1257 rtx temp;
1259 /* If first insn of libcall sequence, skip to end. */
1260 /* Do this at start of loop, since INSN is guaranteed to
1261 be an insn here. */
1262 if (GET_CODE (insn) != NOTE
1263 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1264 insn = XEXP (temp, 0);
1266 do insn = NEXT_INSN (insn);
1267 while (GET_CODE (insn) == NOTE);
1270 return insn;
1273 /* Ignore any movable whose insn falls within a libcall
1274 which is part of another movable.
1275 We make use of the fact that the movable for the libcall value
1276 was made later and so appears later on the chain. */
1278 static void
1279 ignore_some_movables (movables)
1280 struct movable *movables;
1282 register struct movable *m, *m1;
1284 for (m = movables; m; m = m->next)
1286 /* Is this a movable for the value of a libcall? */
1287 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1288 if (note)
1290 rtx insn;
1291 /* Check for earlier movables inside that range,
1292 and mark them invalid. We cannot use LUIDs here because
1293 insns created by loop.c for prior loops don't have LUIDs.
1294 Rather than reject all such insns from movables, we just
1295 explicitly check each insn in the libcall (since invariant
1296 libcalls aren't that common). */
1297 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1298 for (m1 = movables; m1 != m; m1 = m1->next)
1299 if (m1->insn == insn)
1300 m1->done = 1;
1305 /* For each movable insn, see if the reg that it loads
1306 leads when it dies right into another conditionally movable insn.
1307 If so, record that the second insn "forces" the first one,
1308 since the second can be moved only if the first is. */
1310 static void
1311 force_movables (movables)
1312 struct movable *movables;
1314 register struct movable *m, *m1;
1315 for (m1 = movables; m1; m1 = m1->next)
1316 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1317 if (!m1->partial && !m1->done)
1319 int regno = m1->regno;
1320 for (m = m1->next; m; m = m->next)
1321 /* ??? Could this be a bug? What if CSE caused the
1322 register of M1 to be used after this insn?
1323 Since CSE does not update regno_last_uid,
1324 this insn M->insn might not be where it dies.
1325 But very likely this doesn't matter; what matters is
1326 that M's reg is computed from M1's reg. */
1327 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1328 && !m->done)
1329 break;
1330 if (m != 0 && m->set_src == m1->set_dest
1331 /* If m->consec, m->set_src isn't valid. */
1332 && m->consec == 0)
1333 m = 0;
1335 /* Increase the priority of the moving the first insn
1336 since it permits the second to be moved as well. */
1337 if (m != 0)
1339 m->forces = m1;
1340 m1->lifetime += m->lifetime;
1341 m1->savings += m->savings;
1346 /* Find invariant expressions that are equal and can be combined into
1347 one register. */
1349 static void
1350 combine_movables (movables, nregs)
1351 struct movable *movables;
1352 int nregs;
1354 register struct movable *m;
1355 char *matched_regs = (char *) alloca (nregs);
1356 enum machine_mode mode;
1358 /* Regs that are set more than once are not allowed to match
1359 or be matched. I'm no longer sure why not. */
1360 /* Perhaps testing m->consec_sets would be more appropriate here? */
1362 for (m = movables; m; m = m->next)
1363 if (m->match == 0 && n_times_used[m->regno] == 1 && !m->partial)
1365 register struct movable *m1;
1366 int regno = m->regno;
1368 bzero (matched_regs, nregs);
1369 matched_regs[regno] = 1;
1371 /* We want later insns to match the first one. Don't make the first
1372 one match any later ones. So start this loop at m->next. */
1373 for (m1 = m->next; m1; m1 = m1->next)
1374 if (m != m1 && m1->match == 0 && n_times_used[m1->regno] == 1
1375 /* A reg used outside the loop mustn't be eliminated. */
1376 && !m1->global
1377 /* A reg used for zero-extending mustn't be eliminated. */
1378 && !m1->partial
1379 && (matched_regs[m1->regno]
1382 /* Can combine regs with different modes loaded from the
1383 same constant only if the modes are the same or
1384 if both are integer modes with M wider or the same
1385 width as M1. The check for integer is redundant, but
1386 safe, since the only case of differing destination
1387 modes with equal sources is when both sources are
1388 VOIDmode, i.e., CONST_INT. */
1389 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1390 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1391 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1392 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1393 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1394 /* See if the source of M1 says it matches M. */
1395 && ((GET_CODE (m1->set_src) == REG
1396 && matched_regs[REGNO (m1->set_src)])
1397 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1398 movables))))
1399 && ((m->dependencies == m1->dependencies)
1400 || rtx_equal_p (m->dependencies, m1->dependencies)))
1402 m->lifetime += m1->lifetime;
1403 m->savings += m1->savings;
1404 m1->done = 1;
1405 m1->match = m;
1406 matched_regs[m1->regno] = 1;
1410 /* Now combine the regs used for zero-extension.
1411 This can be done for those not marked `global'
1412 provided their lives don't overlap. */
1414 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1415 mode = GET_MODE_WIDER_MODE (mode))
1417 register struct movable *m0 = 0;
1419 /* Combine all the registers for extension from mode MODE.
1420 Don't combine any that are used outside this loop. */
1421 for (m = movables; m; m = m->next)
1422 if (m->partial && ! m->global
1423 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1425 register struct movable *m1;
1426 int first = uid_luid[REGNO_FIRST_UID (m->regno)];
1427 int last = uid_luid[REGNO_LAST_UID (m->regno)];
1429 if (m0 == 0)
1431 /* First one: don't check for overlap, just record it. */
1432 m0 = m;
1433 continue;
1436 /* Make sure they extend to the same mode.
1437 (Almost always true.) */
1438 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1439 continue;
1441 /* We already have one: check for overlap with those
1442 already combined together. */
1443 for (m1 = movables; m1 != m; m1 = m1->next)
1444 if (m1 == m0 || (m1->partial && m1->match == m0))
1445 if (! (uid_luid[REGNO_FIRST_UID (m1->regno)] > last
1446 || uid_luid[REGNO_LAST_UID (m1->regno)] < first))
1447 goto overlap;
1449 /* No overlap: we can combine this with the others. */
1450 m0->lifetime += m->lifetime;
1451 m0->savings += m->savings;
1452 m->done = 1;
1453 m->match = m0;
1455 overlap: ;
1460 /* Return 1 if regs X and Y will become the same if moved. */
1462 static int
1463 regs_match_p (x, y, movables)
1464 rtx x, y;
1465 struct movable *movables;
1467 int xn = REGNO (x);
1468 int yn = REGNO (y);
1469 struct movable *mx, *my;
1471 for (mx = movables; mx; mx = mx->next)
1472 if (mx->regno == xn)
1473 break;
1475 for (my = movables; my; my = my->next)
1476 if (my->regno == yn)
1477 break;
1479 return (mx && my
1480 && ((mx->match == my->match && mx->match != 0)
1481 || mx->match == my
1482 || mx == my->match));
1485 /* Return 1 if X and Y are identical-looking rtx's.
1486 This is the Lisp function EQUAL for rtx arguments.
1488 If two registers are matching movables or a movable register and an
1489 equivalent constant, consider them equal. */
1491 static int
1492 rtx_equal_for_loop_p (x, y, movables)
1493 rtx x, y;
1494 struct movable *movables;
1496 register int i;
1497 register int j;
1498 register struct movable *m;
1499 register enum rtx_code code;
1500 register char *fmt;
1502 if (x == y)
1503 return 1;
1504 if (x == 0 || y == 0)
1505 return 0;
1507 code = GET_CODE (x);
1509 /* If we have a register and a constant, they may sometimes be
1510 equal. */
1511 if (GET_CODE (x) == REG && n_times_set[REGNO (x)] == -2
1512 && CONSTANT_P (y))
1514 for (m = movables; m; m = m->next)
1515 if (m->move_insn && m->regno == REGNO (x)
1516 && rtx_equal_p (m->set_src, y))
1517 return 1;
1519 else if (GET_CODE (y) == REG && n_times_set[REGNO (y)] == -2
1520 && CONSTANT_P (x))
1522 for (m = movables; m; m = m->next)
1523 if (m->move_insn && m->regno == REGNO (y)
1524 && rtx_equal_p (m->set_src, x))
1525 return 1;
1528 /* Otherwise, rtx's of different codes cannot be equal. */
1529 if (code != GET_CODE (y))
1530 return 0;
1532 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1533 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1535 if (GET_MODE (x) != GET_MODE (y))
1536 return 0;
1538 /* These three types of rtx's can be compared nonrecursively. */
1539 if (code == REG)
1540 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1542 if (code == LABEL_REF)
1543 return XEXP (x, 0) == XEXP (y, 0);
1544 if (code == SYMBOL_REF)
1545 return XSTR (x, 0) == XSTR (y, 0);
1547 /* Compare the elements. If any pair of corresponding elements
1548 fail to match, return 0 for the whole things. */
1550 fmt = GET_RTX_FORMAT (code);
1551 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1553 switch (fmt[i])
1555 case 'w':
1556 if (XWINT (x, i) != XWINT (y, i))
1557 return 0;
1558 break;
1560 case 'i':
1561 if (XINT (x, i) != XINT (y, i))
1562 return 0;
1563 break;
1565 case 'E':
1566 /* Two vectors must have the same length. */
1567 if (XVECLEN (x, i) != XVECLEN (y, i))
1568 return 0;
1570 /* And the corresponding elements must match. */
1571 for (j = 0; j < XVECLEN (x, i); j++)
1572 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
1573 return 0;
1574 break;
1576 case 'e':
1577 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
1578 return 0;
1579 break;
1581 case 's':
1582 if (strcmp (XSTR (x, i), XSTR (y, i)))
1583 return 0;
1584 break;
1586 case 'u':
1587 /* These are just backpointers, so they don't matter. */
1588 break;
1590 case '0':
1591 break;
1593 /* It is believed that rtx's at this level will never
1594 contain anything but integers and other rtx's,
1595 except for within LABEL_REFs and SYMBOL_REFs. */
1596 default:
1597 abort ();
1600 return 1;
1603 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1604 insns in INSNS which use thet reference. */
1606 static void
1607 add_label_notes (x, insns)
1608 rtx x;
1609 rtx insns;
1611 enum rtx_code code = GET_CODE (x);
1612 int i, j;
1613 char *fmt;
1614 rtx insn;
1616 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1618 rtx next = next_real_insn (XEXP (x, 0));
1620 /* Don't record labels that refer to dispatch tables.
1621 This is not necessary, since the tablejump references the same label.
1622 And if we did record them, flow.c would make worse code. */
1623 if (next == 0
1624 || ! (GET_CODE (next) == JUMP_INSN
1625 && (GET_CODE (PATTERN (next)) == ADDR_VEC
1626 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
1628 for (insn = insns; insn; insn = NEXT_INSN (insn))
1629 if (reg_mentioned_p (XEXP (x, 0), insn))
1630 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, XEXP (x, 0),
1631 REG_NOTES (insn));
1633 return;
1636 fmt = GET_RTX_FORMAT (code);
1637 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1639 if (fmt[i] == 'e')
1640 add_label_notes (XEXP (x, i), insns);
1641 else if (fmt[i] == 'E')
1642 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1643 add_label_notes (XVECEXP (x, i, j), insns);
1647 /* Scan MOVABLES, and move the insns that deserve to be moved.
1648 If two matching movables are combined, replace one reg with the
1649 other throughout. */
1651 static void
1652 move_movables (movables, threshold, insn_count, loop_start, end, nregs)
1653 struct movable *movables;
1654 int threshold;
1655 int insn_count;
1656 rtx loop_start;
1657 rtx end;
1658 int nregs;
1660 rtx new_start = 0;
1661 register struct movable *m;
1662 register rtx p;
1663 /* Map of pseudo-register replacements to handle combining
1664 when we move several insns that load the same value
1665 into different pseudo-registers. */
1666 rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
1667 char *already_moved = (char *) alloca (nregs);
1669 bzero (already_moved, nregs);
1670 bzero ((char *) reg_map, nregs * sizeof (rtx));
1672 num_movables = 0;
1674 for (m = movables; m; m = m->next)
1676 /* Describe this movable insn. */
1678 if (loop_dump_stream)
1680 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1681 INSN_UID (m->insn), m->regno, m->lifetime);
1682 if (m->consec > 0)
1683 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1684 if (m->cond)
1685 fprintf (loop_dump_stream, "cond ");
1686 if (m->force)
1687 fprintf (loop_dump_stream, "force ");
1688 if (m->global)
1689 fprintf (loop_dump_stream, "global ");
1690 if (m->done)
1691 fprintf (loop_dump_stream, "done ");
1692 if (m->move_insn)
1693 fprintf (loop_dump_stream, "move-insn ");
1694 if (m->match)
1695 fprintf (loop_dump_stream, "matches %d ",
1696 INSN_UID (m->match->insn));
1697 if (m->forces)
1698 fprintf (loop_dump_stream, "forces %d ",
1699 INSN_UID (m->forces->insn));
1702 /* Count movables. Value used in heuristics in strength_reduce. */
1703 num_movables++;
1705 /* Ignore the insn if it's already done (it matched something else).
1706 Otherwise, see if it is now safe to move. */
1708 if (!m->done
1709 && (! m->cond
1710 || (1 == invariant_p (m->set_src)
1711 && (m->dependencies == 0
1712 || 1 == invariant_p (m->dependencies))
1713 && (m->consec == 0
1714 || 1 == consec_sets_invariant_p (m->set_dest,
1715 m->consec + 1,
1716 m->insn))))
1717 && (! m->forces || m->forces->done))
1719 register int regno;
1720 register rtx p;
1721 int savings = m->savings;
1723 /* We have an insn that is safe to move.
1724 Compute its desirability. */
1726 p = m->insn;
1727 regno = m->regno;
1729 if (loop_dump_stream)
1730 fprintf (loop_dump_stream, "savings %d ", savings);
1732 if (moved_once[regno])
1734 insn_count *= 2;
1736 if (loop_dump_stream)
1737 fprintf (loop_dump_stream, "halved since already moved ");
1740 /* An insn MUST be moved if we already moved something else
1741 which is safe only if this one is moved too: that is,
1742 if already_moved[REGNO] is nonzero. */
1744 /* An insn is desirable to move if the new lifetime of the
1745 register is no more than THRESHOLD times the old lifetime.
1746 If it's not desirable, it means the loop is so big
1747 that moving won't speed things up much,
1748 and it is liable to make register usage worse. */
1750 /* It is also desirable to move if it can be moved at no
1751 extra cost because something else was already moved. */
1753 if (already_moved[regno]
1754 || flag_move_all_movables
1755 || (threshold * savings * m->lifetime) >= insn_count
1756 || (m->forces && m->forces->done
1757 && n_times_used[m->forces->regno] == 1))
1759 int count;
1760 register struct movable *m1;
1761 rtx first;
1763 /* Now move the insns that set the reg. */
1765 if (m->partial && m->match)
1767 rtx newpat, i1;
1768 rtx r1, r2;
1769 /* Find the end of this chain of matching regs.
1770 Thus, we load each reg in the chain from that one reg.
1771 And that reg is loaded with 0 directly,
1772 since it has ->match == 0. */
1773 for (m1 = m; m1->match; m1 = m1->match);
1774 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1775 SET_DEST (PATTERN (m1->insn)));
1776 i1 = emit_insn_before (newpat, loop_start);
1778 /* Mark the moved, invariant reg as being allowed to
1779 share a hard reg with the other matching invariant. */
1780 REG_NOTES (i1) = REG_NOTES (m->insn);
1781 r1 = SET_DEST (PATTERN (m->insn));
1782 r2 = SET_DEST (PATTERN (m1->insn));
1783 regs_may_share
1784 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1785 gen_rtx_EXPR_LIST (VOIDmode, r2,
1786 regs_may_share));
1787 delete_insn (m->insn);
1789 if (new_start == 0)
1790 new_start = i1;
1792 if (loop_dump_stream)
1793 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1795 /* If we are to re-generate the item being moved with a
1796 new move insn, first delete what we have and then emit
1797 the move insn before the loop. */
1798 else if (m->move_insn)
1800 rtx i1, temp;
1802 for (count = m->consec; count >= 0; count--)
1804 /* If this is the first insn of a library call sequence,
1805 skip to the end. */
1806 if (GET_CODE (p) != NOTE
1807 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1808 p = XEXP (temp, 0);
1810 /* If this is the last insn of a libcall sequence, then
1811 delete every insn in the sequence except the last.
1812 The last insn is handled in the normal manner. */
1813 if (GET_CODE (p) != NOTE
1814 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1816 temp = XEXP (temp, 0);
1817 while (temp != p)
1818 temp = delete_insn (temp);
1821 p = delete_insn (p);
1822 while (p && GET_CODE (p) == NOTE)
1823 p = NEXT_INSN (p);
1826 start_sequence ();
1827 emit_move_insn (m->set_dest, m->set_src);
1828 temp = get_insns ();
1829 end_sequence ();
1831 add_label_notes (m->set_src, temp);
1833 i1 = emit_insns_before (temp, loop_start);
1834 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1835 REG_NOTES (i1)
1836 = gen_rtx_EXPR_LIST (m->is_equiv ? REG_EQUIV : REG_EQUAL,
1837 m->set_src, REG_NOTES (i1));
1839 if (loop_dump_stream)
1840 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1842 /* The more regs we move, the less we like moving them. */
1843 threshold -= 3;
1845 else
1847 for (count = m->consec; count >= 0; count--)
1849 rtx i1, temp;
1851 /* If first insn of libcall sequence, skip to end. */
1852 /* Do this at start of loop, since p is guaranteed to
1853 be an insn here. */
1854 if (GET_CODE (p) != NOTE
1855 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1856 p = XEXP (temp, 0);
1858 /* If last insn of libcall sequence, move all
1859 insns except the last before the loop. The last
1860 insn is handled in the normal manner. */
1861 if (GET_CODE (p) != NOTE
1862 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1864 rtx fn_address = 0;
1865 rtx fn_reg = 0;
1866 rtx fn_address_insn = 0;
1868 first = 0;
1869 for (temp = XEXP (temp, 0); temp != p;
1870 temp = NEXT_INSN (temp))
1872 rtx body;
1873 rtx n;
1874 rtx next;
1876 if (GET_CODE (temp) == NOTE)
1877 continue;
1879 body = PATTERN (temp);
1881 /* Find the next insn after TEMP,
1882 not counting USE or NOTE insns. */
1883 for (next = NEXT_INSN (temp); next != p;
1884 next = NEXT_INSN (next))
1885 if (! (GET_CODE (next) == INSN
1886 && GET_CODE (PATTERN (next)) == USE)
1887 && GET_CODE (next) != NOTE)
1888 break;
1890 /* If that is the call, this may be the insn
1891 that loads the function address.
1893 Extract the function address from the insn
1894 that loads it into a register.
1895 If this insn was cse'd, we get incorrect code.
1897 So emit a new move insn that copies the
1898 function address into the register that the
1899 call insn will use. flow.c will delete any
1900 redundant stores that we have created. */
1901 if (GET_CODE (next) == CALL_INSN
1902 && GET_CODE (body) == SET
1903 && GET_CODE (SET_DEST (body)) == REG
1904 && (n = find_reg_note (temp, REG_EQUAL,
1905 NULL_RTX)))
1907 fn_reg = SET_SRC (body);
1908 if (GET_CODE (fn_reg) != REG)
1909 fn_reg = SET_DEST (body);
1910 fn_address = XEXP (n, 0);
1911 fn_address_insn = temp;
1913 /* We have the call insn.
1914 If it uses the register we suspect it might,
1915 load it with the correct address directly. */
1916 if (GET_CODE (temp) == CALL_INSN
1917 && fn_address != 0
1918 && reg_referenced_p (fn_reg, body))
1919 emit_insn_after (gen_move_insn (fn_reg,
1920 fn_address),
1921 fn_address_insn);
1923 if (GET_CODE (temp) == CALL_INSN)
1925 i1 = emit_call_insn_before (body, loop_start);
1926 /* Because the USAGE information potentially
1927 contains objects other than hard registers
1928 we need to copy it. */
1929 if (CALL_INSN_FUNCTION_USAGE (temp))
1930 CALL_INSN_FUNCTION_USAGE (i1)
1931 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
1933 else
1934 i1 = emit_insn_before (body, loop_start);
1935 if (first == 0)
1936 first = i1;
1937 if (temp == fn_address_insn)
1938 fn_address_insn = i1;
1939 REG_NOTES (i1) = REG_NOTES (temp);
1940 delete_insn (temp);
1943 if (m->savemode != VOIDmode)
1945 /* P sets REG to zero; but we should clear only
1946 the bits that are not covered by the mode
1947 m->savemode. */
1948 rtx reg = m->set_dest;
1949 rtx sequence;
1950 rtx tem;
1952 start_sequence ();
1953 tem = expand_binop
1954 (GET_MODE (reg), and_optab, reg,
1955 GEN_INT ((((HOST_WIDE_INT) 1
1956 << GET_MODE_BITSIZE (m->savemode)))
1957 - 1),
1958 reg, 1, OPTAB_LIB_WIDEN);
1959 if (tem == 0)
1960 abort ();
1961 if (tem != reg)
1962 emit_move_insn (reg, tem);
1963 sequence = gen_sequence ();
1964 end_sequence ();
1965 i1 = emit_insn_before (sequence, loop_start);
1967 else if (GET_CODE (p) == CALL_INSN)
1969 i1 = emit_call_insn_before (PATTERN (p), loop_start);
1970 /* Because the USAGE information potentially
1971 contains objects other than hard registers
1972 we need to copy it. */
1973 if (CALL_INSN_FUNCTION_USAGE (p))
1974 CALL_INSN_FUNCTION_USAGE (i1)
1975 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
1977 else if (count == m->consec && m->move_insn_first)
1979 /* The SET_SRC might not be invariant, so we must
1980 use the REG_EQUAL note. */
1981 start_sequence ();
1982 emit_move_insn (m->set_dest, m->set_src);
1983 temp = get_insns ();
1984 end_sequence ();
1986 add_label_notes (m->set_src, temp);
1988 i1 = emit_insns_before (temp, loop_start);
1989 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1990 REG_NOTES (i1)
1991 = gen_rtx_EXPR_LIST ((m->is_equiv ? REG_EQUIV
1992 : REG_EQUAL),
1993 m->set_src, REG_NOTES (i1));
1995 else
1996 i1 = emit_insn_before (PATTERN (p), loop_start);
1998 if (REG_NOTES (i1) == 0)
2000 REG_NOTES (i1) = REG_NOTES (p);
2002 /* If there is a REG_EQUAL note present whose value
2003 is not loop invariant, then delete it, since it
2004 may cause problems with later optimization passes.
2005 It is possible for cse to create such notes
2006 like this as a result of record_jump_cond. */
2008 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2009 && ! invariant_p (XEXP (temp, 0)))
2010 remove_note (i1, temp);
2013 if (new_start == 0)
2014 new_start = i1;
2016 if (loop_dump_stream)
2017 fprintf (loop_dump_stream, " moved to %d",
2018 INSN_UID (i1));
2020 /* If library call, now fix the REG_NOTES that contain
2021 insn pointers, namely REG_LIBCALL on FIRST
2022 and REG_RETVAL on I1. */
2023 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
2025 XEXP (temp, 0) = first;
2026 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2027 XEXP (temp, 0) = i1;
2030 delete_insn (p);
2031 do p = NEXT_INSN (p);
2032 while (p && GET_CODE (p) == NOTE);
2035 /* The more regs we move, the less we like moving them. */
2036 threshold -= 3;
2039 /* Any other movable that loads the same register
2040 MUST be moved. */
2041 already_moved[regno] = 1;
2043 /* This reg has been moved out of one loop. */
2044 moved_once[regno] = 1;
2046 /* The reg set here is now invariant. */
2047 if (! m->partial)
2048 n_times_set[regno] = 0;
2050 m->done = 1;
2052 /* Change the length-of-life info for the register
2053 to say it lives at least the full length of this loop.
2054 This will help guide optimizations in outer loops. */
2056 if (uid_luid[REGNO_FIRST_UID (regno)] > INSN_LUID (loop_start))
2057 /* This is the old insn before all the moved insns.
2058 We can't use the moved insn because it is out of range
2059 in uid_luid. Only the old insns have luids. */
2060 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2061 if (uid_luid[REGNO_LAST_UID (regno)] < INSN_LUID (end))
2062 REGNO_LAST_UID (regno) = INSN_UID (end);
2064 /* Combine with this moved insn any other matching movables. */
2066 if (! m->partial)
2067 for (m1 = movables; m1; m1 = m1->next)
2068 if (m1->match == m)
2070 rtx temp;
2072 /* Schedule the reg loaded by M1
2073 for replacement so that shares the reg of M.
2074 If the modes differ (only possible in restricted
2075 circumstances, make a SUBREG. */
2076 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2077 reg_map[m1->regno] = m->set_dest;
2078 else
2079 reg_map[m1->regno]
2080 = gen_lowpart_common (GET_MODE (m1->set_dest),
2081 m->set_dest);
2083 /* Get rid of the matching insn
2084 and prevent further processing of it. */
2085 m1->done = 1;
2087 /* if library call, delete all insn except last, which
2088 is deleted below */
2089 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2090 NULL_RTX)))
2092 for (temp = XEXP (temp, 0); temp != m1->insn;
2093 temp = NEXT_INSN (temp))
2094 delete_insn (temp);
2096 delete_insn (m1->insn);
2098 /* Any other movable that loads the same register
2099 MUST be moved. */
2100 already_moved[m1->regno] = 1;
2102 /* The reg merged here is now invariant,
2103 if the reg it matches is invariant. */
2104 if (! m->partial)
2105 n_times_set[m1->regno] = 0;
2108 else if (loop_dump_stream)
2109 fprintf (loop_dump_stream, "not desirable");
2111 else if (loop_dump_stream && !m->match)
2112 fprintf (loop_dump_stream, "not safe");
2114 if (loop_dump_stream)
2115 fprintf (loop_dump_stream, "\n");
2118 if (new_start == 0)
2119 new_start = loop_start;
2121 /* Go through all the instructions in the loop, making
2122 all the register substitutions scheduled in REG_MAP. */
2123 for (p = new_start; p != end; p = NEXT_INSN (p))
2124 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2125 || GET_CODE (p) == CALL_INSN)
2127 replace_regs (PATTERN (p), reg_map, nregs, 0);
2128 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2129 INSN_CODE (p) = -1;
2133 #if 0
2134 /* Scan X and replace the address of any MEM in it with ADDR.
2135 REG is the address that MEM should have before the replacement. */
2137 static void
2138 replace_call_address (x, reg, addr)
2139 rtx x, reg, addr;
2141 register enum rtx_code code;
2142 register int i;
2143 register char *fmt;
2145 if (x == 0)
2146 return;
2147 code = GET_CODE (x);
2148 switch (code)
2150 case PC:
2151 case CC0:
2152 case CONST_INT:
2153 case CONST_DOUBLE:
2154 case CONST:
2155 case SYMBOL_REF:
2156 case LABEL_REF:
2157 case REG:
2158 return;
2160 case SET:
2161 /* Short cut for very common case. */
2162 replace_call_address (XEXP (x, 1), reg, addr);
2163 return;
2165 case CALL:
2166 /* Short cut for very common case. */
2167 replace_call_address (XEXP (x, 0), reg, addr);
2168 return;
2170 case MEM:
2171 /* If this MEM uses a reg other than the one we expected,
2172 something is wrong. */
2173 if (XEXP (x, 0) != reg)
2174 abort ();
2175 XEXP (x, 0) = addr;
2176 return;
2178 default:
2179 break;
2182 fmt = GET_RTX_FORMAT (code);
2183 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2185 if (fmt[i] == 'e')
2186 replace_call_address (XEXP (x, i), reg, addr);
2187 if (fmt[i] == 'E')
2189 register int j;
2190 for (j = 0; j < XVECLEN (x, i); j++)
2191 replace_call_address (XVECEXP (x, i, j), reg, addr);
2195 #endif
2197 /* Return the number of memory refs to addresses that vary
2198 in the rtx X. */
2200 static int
2201 count_nonfixed_reads (x)
2202 rtx x;
2204 register enum rtx_code code;
2205 register int i;
2206 register char *fmt;
2207 int value;
2209 if (x == 0)
2210 return 0;
2212 code = GET_CODE (x);
2213 switch (code)
2215 case PC:
2216 case CC0:
2217 case CONST_INT:
2218 case CONST_DOUBLE:
2219 case CONST:
2220 case SYMBOL_REF:
2221 case LABEL_REF:
2222 case REG:
2223 return 0;
2225 case MEM:
2226 return ((invariant_p (XEXP (x, 0)) != 1)
2227 + count_nonfixed_reads (XEXP (x, 0)));
2229 default:
2230 break;
2233 value = 0;
2234 fmt = GET_RTX_FORMAT (code);
2235 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2237 if (fmt[i] == 'e')
2238 value += count_nonfixed_reads (XEXP (x, i));
2239 if (fmt[i] == 'E')
2241 register int j;
2242 for (j = 0; j < XVECLEN (x, i); j++)
2243 value += count_nonfixed_reads (XVECEXP (x, i, j));
2246 return value;
2250 #if 0
2251 /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2252 Replace it with an instruction to load just the low bytes
2253 if the machine supports such an instruction,
2254 and insert above LOOP_START an instruction to clear the register. */
2256 static void
2257 constant_high_bytes (p, loop_start)
2258 rtx p, loop_start;
2260 register rtx new;
2261 register int insn_code_number;
2263 /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2264 to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
2266 new = gen_rtx_SET (VOIDmode,
2267 gen_rtx_STRICT_LOW_PART (VOIDmode,
2268 gen_rtx_SUBREG (GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
2269 SET_DEST (PATTERN (p)),
2270 0)),
2271 XEXP (SET_SRC (PATTERN (p)), 0));
2272 insn_code_number = recog (new, p);
2274 if (insn_code_number)
2276 register int i;
2278 /* Clear destination register before the loop. */
2279 emit_insn_before (gen_rtx_SET (VOIDmode, SET_DEST (PATTERN (p)),
2280 const0_rtx),
2281 loop_start);
2283 /* Inside the loop, just load the low part. */
2284 PATTERN (p) = new;
2287 #endif
2289 /* Scan a loop setting the variables `unknown_address_altered',
2290 `num_mem_sets', `loop_continue', loops_enclosed', `loop_has_call',
2291 and `loop_has_volatile'.
2292 Also, fill in the array `loop_store_mems'. */
2294 static void
2295 prescan_loop (start, end)
2296 rtx start, end;
2298 register int level = 1;
2299 register rtx insn;
2301 unknown_address_altered = 0;
2302 loop_has_call = 0;
2303 loop_has_volatile = 0;
2304 loop_store_mems_idx = 0;
2306 num_mem_sets = 0;
2307 loops_enclosed = 1;
2308 loop_continue = 0;
2310 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2311 insn = NEXT_INSN (insn))
2313 if (GET_CODE (insn) == NOTE)
2315 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2317 ++level;
2318 /* Count number of loops contained in this one. */
2319 loops_enclosed++;
2321 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2323 --level;
2324 if (level == 0)
2326 end = insn;
2327 break;
2330 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2332 if (level == 1)
2333 loop_continue = insn;
2336 else if (GET_CODE (insn) == CALL_INSN)
2338 if (! CONST_CALL_P (insn))
2339 unknown_address_altered = 1;
2340 loop_has_call = 1;
2342 else
2344 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
2346 if (volatile_refs_p (PATTERN (insn)))
2347 loop_has_volatile = 1;
2349 note_stores (PATTERN (insn), note_addr_stored);
2355 /* Scan the function looking for loops. Record the start and end of each loop.
2356 Also mark as invalid loops any loops that contain a setjmp or are branched
2357 to from outside the loop. */
2359 static void
2360 find_and_verify_loops (f)
2361 rtx f;
2363 rtx insn, label;
2364 int current_loop = -1;
2365 int next_loop = -1;
2366 int loop;
2368 /* If there are jumps to undefined labels,
2369 treat them as jumps out of any/all loops.
2370 This also avoids writing past end of tables when there are no loops. */
2371 uid_loop_num[0] = -1;
2373 /* Find boundaries of loops, mark which loops are contained within
2374 loops, and invalidate loops that have setjmp. */
2376 for (insn = f; insn; insn = NEXT_INSN (insn))
2378 if (GET_CODE (insn) == NOTE)
2379 switch (NOTE_LINE_NUMBER (insn))
2381 case NOTE_INSN_LOOP_BEG:
2382 loop_number_loop_starts[++next_loop] = insn;
2383 loop_number_loop_ends[next_loop] = 0;
2384 loop_outer_loop[next_loop] = current_loop;
2385 loop_invalid[next_loop] = 0;
2386 loop_number_exit_labels[next_loop] = 0;
2387 loop_number_exit_count[next_loop] = 0;
2388 current_loop = next_loop;
2389 break;
2391 case NOTE_INSN_SETJMP:
2392 /* In this case, we must invalidate our current loop and any
2393 enclosing loop. */
2394 for (loop = current_loop; loop != -1; loop = loop_outer_loop[loop])
2396 loop_invalid[loop] = 1;
2397 if (loop_dump_stream)
2398 fprintf (loop_dump_stream,
2399 "\nLoop at %d ignored due to setjmp.\n",
2400 INSN_UID (loop_number_loop_starts[loop]));
2402 break;
2404 case NOTE_INSN_LOOP_END:
2405 if (current_loop == -1)
2406 abort ();
2408 loop_number_loop_ends[current_loop] = insn;
2409 current_loop = loop_outer_loop[current_loop];
2410 break;
2412 default:
2413 break;
2416 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2417 enclosing loop, but this doesn't matter. */
2418 uid_loop_num[INSN_UID (insn)] = current_loop;
2421 /* Any loop containing a label used in an initializer must be invalidated,
2422 because it can be jumped into from anywhere. */
2424 for (label = forced_labels; label; label = XEXP (label, 1))
2426 int loop_num;
2428 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2429 loop_num != -1;
2430 loop_num = loop_outer_loop[loop_num])
2431 loop_invalid[loop_num] = 1;
2434 /* Any loop containing a label used for an exception handler must be
2435 invalidated, because it can be jumped into from anywhere. */
2437 for (label = exception_handler_labels; label; label = XEXP (label, 1))
2439 int loop_num;
2441 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2442 loop_num != -1;
2443 loop_num = loop_outer_loop[loop_num])
2444 loop_invalid[loop_num] = 1;
2447 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2448 loop that it is not contained within, that loop is marked invalid.
2449 If any INSN or CALL_INSN uses a label's address, then the loop containing
2450 that label is marked invalid, because it could be jumped into from
2451 anywhere.
2453 Also look for blocks of code ending in an unconditional branch that
2454 exits the loop. If such a block is surrounded by a conditional
2455 branch around the block, move the block elsewhere (see below) and
2456 invert the jump to point to the code block. This may eliminate a
2457 label in our loop and will simplify processing by both us and a
2458 possible second cse pass. */
2460 for (insn = f; insn; insn = NEXT_INSN (insn))
2461 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2463 int this_loop_num = uid_loop_num[INSN_UID (insn)];
2465 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2467 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2468 if (note)
2470 int loop_num;
2472 for (loop_num = uid_loop_num[INSN_UID (XEXP (note, 0))];
2473 loop_num != -1;
2474 loop_num = loop_outer_loop[loop_num])
2475 loop_invalid[loop_num] = 1;
2479 if (GET_CODE (insn) != JUMP_INSN)
2480 continue;
2482 mark_loop_jump (PATTERN (insn), this_loop_num);
2484 /* See if this is an unconditional branch outside the loop. */
2485 if (this_loop_num != -1
2486 && (GET_CODE (PATTERN (insn)) == RETURN
2487 || (simplejump_p (insn)
2488 && (uid_loop_num[INSN_UID (JUMP_LABEL (insn))]
2489 != this_loop_num)))
2490 && get_max_uid () < max_uid_for_loop)
2492 rtx p;
2493 rtx our_next = next_real_insn (insn);
2494 int dest_loop;
2495 int outer_loop = -1;
2497 /* Go backwards until we reach the start of the loop, a label,
2498 or a JUMP_INSN. */
2499 for (p = PREV_INSN (insn);
2500 GET_CODE (p) != CODE_LABEL
2501 && ! (GET_CODE (p) == NOTE
2502 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2503 && GET_CODE (p) != JUMP_INSN;
2504 p = PREV_INSN (p))
2507 /* Check for the case where we have a jump to an inner nested
2508 loop, and do not perform the optimization in that case. */
2510 if (JUMP_LABEL (insn))
2512 dest_loop = uid_loop_num[INSN_UID (JUMP_LABEL (insn))];
2513 if (dest_loop != -1)
2515 for (outer_loop = dest_loop; outer_loop != -1;
2516 outer_loop = loop_outer_loop[outer_loop])
2517 if (outer_loop == this_loop_num)
2518 break;
2522 /* Make sure that the target of P is within the current loop. */
2524 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2525 && uid_loop_num[INSN_UID (JUMP_LABEL (p))] != this_loop_num)
2526 outer_loop = this_loop_num;
2528 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2529 we have a block of code to try to move.
2531 We look backward and then forward from the target of INSN
2532 to find a BARRIER at the same loop depth as the target.
2533 If we find such a BARRIER, we make a new label for the start
2534 of the block, invert the jump in P and point it to that label,
2535 and move the block of code to the spot we found. */
2537 if (outer_loop == -1
2538 && GET_CODE (p) == JUMP_INSN
2539 && JUMP_LABEL (p) != 0
2540 /* Just ignore jumps to labels that were never emitted.
2541 These always indicate compilation errors. */
2542 && INSN_UID (JUMP_LABEL (p)) != 0
2543 && condjump_p (p)
2544 && ! simplejump_p (p)
2545 && next_real_insn (JUMP_LABEL (p)) == our_next)
2547 rtx target
2548 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2549 int target_loop_num = uid_loop_num[INSN_UID (target)];
2550 rtx loc;
2552 for (loc = target; loc; loc = PREV_INSN (loc))
2553 if (GET_CODE (loc) == BARRIER
2554 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2555 break;
2557 if (loc == 0)
2558 for (loc = target; loc; loc = NEXT_INSN (loc))
2559 if (GET_CODE (loc) == BARRIER
2560 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2561 break;
2563 if (loc)
2565 rtx cond_label = JUMP_LABEL (p);
2566 rtx new_label = get_label_after (p);
2568 /* Ensure our label doesn't go away. */
2569 LABEL_NUSES (cond_label)++;
2571 /* Verify that uid_loop_num is large enough and that
2572 we can invert P. */
2573 if (invert_jump (p, new_label))
2575 rtx q, r;
2577 /* If no suitable BARRIER was found, create a suitable
2578 one before TARGET. Since TARGET is a fall through
2579 path, we'll need to insert an jump around our block
2580 and a add a BARRIER before TARGET.
2582 This creates an extra unconditional jump outside
2583 the loop. However, the benefits of removing rarely
2584 executed instructions from inside the loop usually
2585 outweighs the cost of the extra unconditional jump
2586 outside the loop. */
2587 if (loc == 0)
2589 rtx temp;
2591 temp = gen_jump (JUMP_LABEL (insn));
2592 temp = emit_jump_insn_before (temp, target);
2593 JUMP_LABEL (temp) = JUMP_LABEL (insn);
2594 LABEL_NUSES (JUMP_LABEL (insn))++;
2595 loc = emit_barrier_before (target);
2598 /* Include the BARRIER after INSN and copy the
2599 block after LOC. */
2600 new_label = squeeze_notes (new_label, NEXT_INSN (insn));
2601 reorder_insns (new_label, NEXT_INSN (insn), loc);
2603 /* All those insns are now in TARGET_LOOP_NUM. */
2604 for (q = new_label; q != NEXT_INSN (NEXT_INSN (insn));
2605 q = NEXT_INSN (q))
2606 uid_loop_num[INSN_UID (q)] = target_loop_num;
2608 /* The label jumped to by INSN is no longer a loop exit.
2609 Unless INSN does not have a label (e.g., it is a
2610 RETURN insn), search loop_number_exit_labels to find
2611 its label_ref, and remove it. Also turn off
2612 LABEL_OUTSIDE_LOOP_P bit. */
2613 if (JUMP_LABEL (insn))
2615 int loop_num;
2617 for (q = 0,
2618 r = loop_number_exit_labels[this_loop_num];
2619 r; q = r, r = LABEL_NEXTREF (r))
2620 if (XEXP (r, 0) == JUMP_LABEL (insn))
2622 LABEL_OUTSIDE_LOOP_P (r) = 0;
2623 if (q)
2624 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2625 else
2626 loop_number_exit_labels[this_loop_num]
2627 = LABEL_NEXTREF (r);
2628 break;
2631 for (loop_num = this_loop_num;
2632 loop_num != -1 && loop_num != target_loop_num;
2633 loop_num = loop_outer_loop[loop_num])
2634 loop_number_exit_count[loop_num]--;
2636 /* If we didn't find it, then something is wrong. */
2637 if (! r)
2638 abort ();
2641 /* P is now a jump outside the loop, so it must be put
2642 in loop_number_exit_labels, and marked as such.
2643 The easiest way to do this is to just call
2644 mark_loop_jump again for P. */
2645 mark_loop_jump (PATTERN (p), this_loop_num);
2647 /* If INSN now jumps to the insn after it,
2648 delete INSN. */
2649 if (JUMP_LABEL (insn) != 0
2650 && (next_real_insn (JUMP_LABEL (insn))
2651 == next_real_insn (insn)))
2652 delete_insn (insn);
2655 /* Continue the loop after where the conditional
2656 branch used to jump, since the only branch insn
2657 in the block (if it still remains) is an inter-loop
2658 branch and hence needs no processing. */
2659 insn = NEXT_INSN (cond_label);
2661 if (--LABEL_NUSES (cond_label) == 0)
2662 delete_insn (cond_label);
2664 /* This loop will be continued with NEXT_INSN (insn). */
2665 insn = PREV_INSN (insn);
2672 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2673 loops it is contained in, mark the target loop invalid.
2675 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2677 static void
2678 mark_loop_jump (x, loop_num)
2679 rtx x;
2680 int loop_num;
2682 int dest_loop;
2683 int outer_loop;
2684 int i;
2686 switch (GET_CODE (x))
2688 case PC:
2689 case USE:
2690 case CLOBBER:
2691 case REG:
2692 case MEM:
2693 case CONST_INT:
2694 case CONST_DOUBLE:
2695 case RETURN:
2696 return;
2698 case CONST:
2699 /* There could be a label reference in here. */
2700 mark_loop_jump (XEXP (x, 0), loop_num);
2701 return;
2703 case PLUS:
2704 case MINUS:
2705 case MULT:
2706 mark_loop_jump (XEXP (x, 0), loop_num);
2707 mark_loop_jump (XEXP (x, 1), loop_num);
2708 return;
2710 case SIGN_EXTEND:
2711 case ZERO_EXTEND:
2712 mark_loop_jump (XEXP (x, 0), loop_num);
2713 return;
2715 case LABEL_REF:
2716 dest_loop = uid_loop_num[INSN_UID (XEXP (x, 0))];
2718 /* Link together all labels that branch outside the loop. This
2719 is used by final_[bg]iv_value and the loop unrolling code. Also
2720 mark this LABEL_REF so we know that this branch should predict
2721 false. */
2723 /* A check to make sure the label is not in an inner nested loop,
2724 since this does not count as a loop exit. */
2725 if (dest_loop != -1)
2727 for (outer_loop = dest_loop; outer_loop != -1;
2728 outer_loop = loop_outer_loop[outer_loop])
2729 if (outer_loop == loop_num)
2730 break;
2732 else
2733 outer_loop = -1;
2735 if (loop_num != -1 && outer_loop == -1)
2737 LABEL_OUTSIDE_LOOP_P (x) = 1;
2738 LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
2739 loop_number_exit_labels[loop_num] = x;
2741 for (outer_loop = loop_num;
2742 outer_loop != -1 && outer_loop != dest_loop;
2743 outer_loop = loop_outer_loop[outer_loop])
2744 loop_number_exit_count[outer_loop]++;
2747 /* If this is inside a loop, but not in the current loop or one enclosed
2748 by it, it invalidates at least one loop. */
2750 if (dest_loop == -1)
2751 return;
2753 /* We must invalidate every nested loop containing the target of this
2754 label, except those that also contain the jump insn. */
2756 for (; dest_loop != -1; dest_loop = loop_outer_loop[dest_loop])
2758 /* Stop when we reach a loop that also contains the jump insn. */
2759 for (outer_loop = loop_num; outer_loop != -1;
2760 outer_loop = loop_outer_loop[outer_loop])
2761 if (dest_loop == outer_loop)
2762 return;
2764 /* If we get here, we know we need to invalidate a loop. */
2765 if (loop_dump_stream && ! loop_invalid[dest_loop])
2766 fprintf (loop_dump_stream,
2767 "\nLoop at %d ignored due to multiple entry points.\n",
2768 INSN_UID (loop_number_loop_starts[dest_loop]));
2770 loop_invalid[dest_loop] = 1;
2772 return;
2774 case SET:
2775 /* If this is not setting pc, ignore. */
2776 if (SET_DEST (x) == pc_rtx)
2777 mark_loop_jump (SET_SRC (x), loop_num);
2778 return;
2780 case IF_THEN_ELSE:
2781 mark_loop_jump (XEXP (x, 1), loop_num);
2782 mark_loop_jump (XEXP (x, 2), loop_num);
2783 return;
2785 case PARALLEL:
2786 case ADDR_VEC:
2787 for (i = 0; i < XVECLEN (x, 0); i++)
2788 mark_loop_jump (XVECEXP (x, 0, i), loop_num);
2789 return;
2791 case ADDR_DIFF_VEC:
2792 for (i = 0; i < XVECLEN (x, 1); i++)
2793 mark_loop_jump (XVECEXP (x, 1, i), loop_num);
2794 return;
2796 default:
2797 /* Treat anything else (such as a symbol_ref)
2798 as a branch out of this loop, but not into any loop. */
2800 if (loop_num != -1)
2802 #ifdef HAIFA
2803 LABEL_OUTSIDE_LOOP_P (x) = 1;
2804 LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
2805 #endif /* HAIFA */
2807 loop_number_exit_labels[loop_num] = x;
2809 for (outer_loop = loop_num; outer_loop != -1;
2810 outer_loop = loop_outer_loop[outer_loop])
2811 loop_number_exit_count[outer_loop]++;
2813 return;
2817 /* Return nonzero if there is a label in the range from
2818 insn INSN to and including the insn whose luid is END
2819 INSN must have an assigned luid (i.e., it must not have
2820 been previously created by loop.c). */
2822 static int
2823 labels_in_range_p (insn, end)
2824 rtx insn;
2825 int end;
2827 while (insn && INSN_LUID (insn) <= end)
2829 if (GET_CODE (insn) == CODE_LABEL)
2830 return 1;
2831 insn = NEXT_INSN (insn);
2834 return 0;
2837 /* Record that a memory reference X is being set. */
2839 static void
2840 note_addr_stored (x, y)
2841 rtx x;
2842 rtx y ATTRIBUTE_UNUSED;
2844 register int i;
2846 if (x == 0 || GET_CODE (x) != MEM)
2847 return;
2849 /* Count number of memory writes.
2850 This affects heuristics in strength_reduce. */
2851 num_mem_sets++;
2853 /* BLKmode MEM means all memory is clobbered. */
2854 if (GET_MODE (x) == BLKmode)
2855 unknown_address_altered = 1;
2857 if (unknown_address_altered)
2858 return;
2860 for (i = 0; i < loop_store_mems_idx; i++)
2861 if (rtx_equal_p (XEXP (loop_store_mems[i], 0), XEXP (x, 0))
2862 && MEM_IN_STRUCT_P (x) == MEM_IN_STRUCT_P (loop_store_mems[i]))
2864 /* We are storing at the same address as previously noted. Save the
2865 wider reference. */
2866 if (GET_MODE_SIZE (GET_MODE (x))
2867 > GET_MODE_SIZE (GET_MODE (loop_store_mems[i])))
2868 loop_store_mems[i] = x;
2869 break;
2872 if (i == NUM_STORES)
2873 unknown_address_altered = 1;
2875 else if (i == loop_store_mems_idx)
2876 loop_store_mems[loop_store_mems_idx++] = x;
2879 /* Return nonzero if the rtx X is invariant over the current loop.
2881 The value is 2 if we refer to something only conditionally invariant.
2883 If `unknown_address_altered' is nonzero, no memory ref is invariant.
2884 Otherwise, a memory ref is invariant if it does not conflict with
2885 anything stored in `loop_store_mems'. */
2888 invariant_p (x)
2889 register rtx x;
2891 register int i;
2892 register enum rtx_code code;
2893 register char *fmt;
2894 int conditional = 0;
2896 if (x == 0)
2897 return 1;
2898 code = GET_CODE (x);
2899 switch (code)
2901 case CONST_INT:
2902 case CONST_DOUBLE:
2903 case SYMBOL_REF:
2904 case CONST:
2905 return 1;
2907 case LABEL_REF:
2908 /* A LABEL_REF is normally invariant, however, if we are unrolling
2909 loops, and this label is inside the loop, then it isn't invariant.
2910 This is because each unrolled copy of the loop body will have
2911 a copy of this label. If this was invariant, then an insn loading
2912 the address of this label into a register might get moved outside
2913 the loop, and then each loop body would end up using the same label.
2915 We don't know the loop bounds here though, so just fail for all
2916 labels. */
2917 if (flag_unroll_loops)
2918 return 0;
2919 else
2920 return 1;
2922 case PC:
2923 case CC0:
2924 case UNSPEC_VOLATILE:
2925 return 0;
2927 case REG:
2928 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
2929 since the reg might be set by initialization within the loop. */
2931 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
2932 || x == arg_pointer_rtx)
2933 && ! current_function_has_nonlocal_goto)
2934 return 1;
2936 if (loop_has_call
2937 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
2938 return 0;
2940 if (n_times_set[REGNO (x)] < 0)
2941 return 2;
2943 return n_times_set[REGNO (x)] == 0;
2945 case MEM:
2946 /* Volatile memory references must be rejected. Do this before
2947 checking for read-only items, so that volatile read-only items
2948 will be rejected also. */
2949 if (MEM_VOLATILE_P (x))
2950 return 0;
2952 /* Read-only items (such as constants in a constant pool) are
2953 invariant if their address is. */
2954 if (RTX_UNCHANGING_P (x))
2955 break;
2957 /* If we filled the table (or had a subroutine call), any location
2958 in memory could have been clobbered. */
2959 if (unknown_address_altered)
2960 return 0;
2962 /* See if there is any dependence between a store and this load. */
2963 for (i = loop_store_mems_idx - 1; i >= 0; i--)
2964 if (true_dependence (loop_store_mems[i], VOIDmode, x, rtx_varies_p))
2965 return 0;
2967 /* It's not invalidated by a store in memory
2968 but we must still verify the address is invariant. */
2969 break;
2971 case ASM_OPERANDS:
2972 /* Don't mess with insns declared volatile. */
2973 if (MEM_VOLATILE_P (x))
2974 return 0;
2975 break;
2977 default:
2978 break;
2981 fmt = GET_RTX_FORMAT (code);
2982 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2984 if (fmt[i] == 'e')
2986 int tem = invariant_p (XEXP (x, i));
2987 if (tem == 0)
2988 return 0;
2989 if (tem == 2)
2990 conditional = 1;
2992 else if (fmt[i] == 'E')
2994 register int j;
2995 for (j = 0; j < XVECLEN (x, i); j++)
2997 int tem = invariant_p (XVECEXP (x, i, j));
2998 if (tem == 0)
2999 return 0;
3000 if (tem == 2)
3001 conditional = 1;
3007 return 1 + conditional;
3011 /* Return nonzero if all the insns in the loop that set REG
3012 are INSN and the immediately following insns,
3013 and if each of those insns sets REG in an invariant way
3014 (not counting uses of REG in them).
3016 The value is 2 if some of these insns are only conditionally invariant.
3018 We assume that INSN itself is the first set of REG
3019 and that its source is invariant. */
3021 static int
3022 consec_sets_invariant_p (reg, n_sets, insn)
3023 int n_sets;
3024 rtx reg, insn;
3026 register rtx p = insn;
3027 register int regno = REGNO (reg);
3028 rtx temp;
3029 /* Number of sets we have to insist on finding after INSN. */
3030 int count = n_sets - 1;
3031 int old = n_times_set[regno];
3032 int value = 0;
3033 int this;
3035 /* If N_SETS hit the limit, we can't rely on its value. */
3036 if (n_sets == 127)
3037 return 0;
3039 n_times_set[regno] = 0;
3041 while (count > 0)
3043 register enum rtx_code code;
3044 rtx set;
3046 p = NEXT_INSN (p);
3047 code = GET_CODE (p);
3049 /* If library call, skip to end of it. */
3050 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3051 p = XEXP (temp, 0);
3053 this = 0;
3054 if (code == INSN
3055 && (set = single_set (p))
3056 && GET_CODE (SET_DEST (set)) == REG
3057 && REGNO (SET_DEST (set)) == regno)
3059 this = invariant_p (SET_SRC (set));
3060 if (this != 0)
3061 value |= this;
3062 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3064 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3065 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3066 notes are OK. */
3067 this = (CONSTANT_P (XEXP (temp, 0))
3068 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3069 && invariant_p (XEXP (temp, 0))));
3070 if (this != 0)
3071 value |= this;
3074 if (this != 0)
3075 count--;
3076 else if (code != NOTE)
3078 n_times_set[regno] = old;
3079 return 0;
3083 n_times_set[regno] = old;
3084 /* If invariant_p ever returned 2, we return 2. */
3085 return 1 + (value & 2);
3088 #if 0
3089 /* I don't think this condition is sufficient to allow INSN
3090 to be moved, so we no longer test it. */
3092 /* Return 1 if all insns in the basic block of INSN and following INSN
3093 that set REG are invariant according to TABLE. */
3095 static int
3096 all_sets_invariant_p (reg, insn, table)
3097 rtx reg, insn;
3098 short *table;
3100 register rtx p = insn;
3101 register int regno = REGNO (reg);
3103 while (1)
3105 register enum rtx_code code;
3106 p = NEXT_INSN (p);
3107 code = GET_CODE (p);
3108 if (code == CODE_LABEL || code == JUMP_INSN)
3109 return 1;
3110 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3111 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3112 && REGNO (SET_DEST (PATTERN (p))) == regno)
3114 if (!invariant_p (SET_SRC (PATTERN (p)), table))
3115 return 0;
3119 #endif /* 0 */
3121 /* Look at all uses (not sets) of registers in X. For each, if it is
3122 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3123 a different insn, set USAGE[REGNO] to const0_rtx. */
3125 static void
3126 find_single_use_in_loop (insn, x, usage)
3127 rtx insn;
3128 rtx x;
3129 rtx *usage;
3131 enum rtx_code code = GET_CODE (x);
3132 char *fmt = GET_RTX_FORMAT (code);
3133 int i, j;
3135 if (code == REG)
3136 usage[REGNO (x)]
3137 = (usage[REGNO (x)] != 0 && usage[REGNO (x)] != insn)
3138 ? const0_rtx : insn;
3140 else if (code == SET)
3142 /* Don't count SET_DEST if it is a REG; otherwise count things
3143 in SET_DEST because if a register is partially modified, it won't
3144 show up as a potential movable so we don't care how USAGE is set
3145 for it. */
3146 if (GET_CODE (SET_DEST (x)) != REG)
3147 find_single_use_in_loop (insn, SET_DEST (x), usage);
3148 find_single_use_in_loop (insn, SET_SRC (x), usage);
3150 else
3151 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3153 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3154 find_single_use_in_loop (insn, XEXP (x, i), usage);
3155 else if (fmt[i] == 'E')
3156 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3157 find_single_use_in_loop (insn, XVECEXP (x, i, j), usage);
3161 /* Increment N_TIMES_SET at the index of each register
3162 that is modified by an insn between FROM and TO.
3163 If the value of an element of N_TIMES_SET becomes 127 or more,
3164 stop incrementing it, to avoid overflow.
3166 Store in SINGLE_USAGE[I] the single insn in which register I is
3167 used, if it is only used once. Otherwise, it is set to 0 (for no
3168 uses) or const0_rtx for more than one use. This parameter may be zero,
3169 in which case this processing is not done.
3171 Store in *COUNT_PTR the number of actual instruction
3172 in the loop. We use this to decide what is worth moving out. */
3174 /* last_set[n] is nonzero iff reg n has been set in the current basic block.
3175 In that case, it is the insn that last set reg n. */
3177 static void
3178 count_loop_regs_set (from, to, may_not_move, single_usage, count_ptr, nregs)
3179 register rtx from, to;
3180 char *may_not_move;
3181 rtx *single_usage;
3182 int *count_ptr;
3183 int nregs;
3185 register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
3186 register rtx insn;
3187 register int count = 0;
3188 register rtx dest;
3190 bzero ((char *) last_set, nregs * sizeof (rtx));
3191 for (insn = from; insn != to; insn = NEXT_INSN (insn))
3193 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3195 ++count;
3197 /* If requested, record registers that have exactly one use. */
3198 if (single_usage)
3200 find_single_use_in_loop (insn, PATTERN (insn), single_usage);
3202 /* Include uses in REG_EQUAL notes. */
3203 if (REG_NOTES (insn))
3204 find_single_use_in_loop (insn, REG_NOTES (insn), single_usage);
3207 if (GET_CODE (PATTERN (insn)) == CLOBBER
3208 && GET_CODE (XEXP (PATTERN (insn), 0)) == REG)
3209 /* Don't move a reg that has an explicit clobber.
3210 We might do so sometimes, but it's not worth the pain. */
3211 may_not_move[REGNO (XEXP (PATTERN (insn), 0))] = 1;
3213 if (GET_CODE (PATTERN (insn)) == SET
3214 || GET_CODE (PATTERN (insn)) == CLOBBER)
3216 dest = SET_DEST (PATTERN (insn));
3217 while (GET_CODE (dest) == SUBREG
3218 || GET_CODE (dest) == ZERO_EXTRACT
3219 || GET_CODE (dest) == SIGN_EXTRACT
3220 || GET_CODE (dest) == STRICT_LOW_PART)
3221 dest = XEXP (dest, 0);
3222 if (GET_CODE (dest) == REG)
3224 register int regno = REGNO (dest);
3225 /* If this is the first setting of this reg
3226 in current basic block, and it was set before,
3227 it must be set in two basic blocks, so it cannot
3228 be moved out of the loop. */
3229 if (n_times_set[regno] > 0 && last_set[regno] == 0)
3230 may_not_move[regno] = 1;
3231 /* If this is not first setting in current basic block,
3232 see if reg was used in between previous one and this.
3233 If so, neither one can be moved. */
3234 if (last_set[regno] != 0
3235 && reg_used_between_p (dest, last_set[regno], insn))
3236 may_not_move[regno] = 1;
3237 if (n_times_set[regno] < 127)
3238 ++n_times_set[regno];
3239 last_set[regno] = insn;
3242 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3244 register int i;
3245 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3247 register rtx x = XVECEXP (PATTERN (insn), 0, i);
3248 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3249 /* Don't move a reg that has an explicit clobber.
3250 It's not worth the pain to try to do it correctly. */
3251 may_not_move[REGNO (XEXP (x, 0))] = 1;
3253 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3255 dest = SET_DEST (x);
3256 while (GET_CODE (dest) == SUBREG
3257 || GET_CODE (dest) == ZERO_EXTRACT
3258 || GET_CODE (dest) == SIGN_EXTRACT
3259 || GET_CODE (dest) == STRICT_LOW_PART)
3260 dest = XEXP (dest, 0);
3261 if (GET_CODE (dest) == REG)
3263 register int regno = REGNO (dest);
3264 if (n_times_set[regno] > 0 && last_set[regno] == 0)
3265 may_not_move[regno] = 1;
3266 if (last_set[regno] != 0
3267 && reg_used_between_p (dest, last_set[regno], insn))
3268 may_not_move[regno] = 1;
3269 if (n_times_set[regno] < 127)
3270 ++n_times_set[regno];
3271 last_set[regno] = insn;
3278 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
3279 bzero ((char *) last_set, nregs * sizeof (rtx));
3281 *count_ptr = count;
3284 /* Given a loop that is bounded by LOOP_START and LOOP_END
3285 and that is entered at SCAN_START,
3286 return 1 if the register set in SET contained in insn INSN is used by
3287 any insn that precedes INSN in cyclic order starting
3288 from the loop entry point.
3290 We don't want to use INSN_LUID here because if we restrict INSN to those
3291 that have a valid INSN_LUID, it means we cannot move an invariant out
3292 from an inner loop past two loops. */
3294 static int
3295 loop_reg_used_before_p (set, insn, loop_start, scan_start, loop_end)
3296 rtx set, insn, loop_start, scan_start, loop_end;
3298 rtx reg = SET_DEST (set);
3299 rtx p;
3301 /* Scan forward checking for register usage. If we hit INSN, we
3302 are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
3303 for (p = scan_start; p != insn; p = NEXT_INSN (p))
3305 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
3306 && reg_overlap_mentioned_p (reg, PATTERN (p)))
3307 return 1;
3309 if (p == loop_end)
3310 p = loop_start;
3313 return 0;
3316 /* A "basic induction variable" or biv is a pseudo reg that is set
3317 (within this loop) only by incrementing or decrementing it. */
3318 /* A "general induction variable" or giv is a pseudo reg whose
3319 value is a linear function of a biv. */
3321 /* Bivs are recognized by `basic_induction_var';
3322 Givs by `general_induct_var'. */
3324 /* Indexed by register number, indicates whether or not register is an
3325 induction variable, and if so what type. */
3327 enum iv_mode *reg_iv_type;
3329 /* Indexed by register number, contains pointer to `struct induction'
3330 if register is an induction variable. This holds general info for
3331 all induction variables. */
3333 struct induction **reg_iv_info;
3335 /* Indexed by register number, contains pointer to `struct iv_class'
3336 if register is a basic induction variable. This holds info describing
3337 the class (a related group) of induction variables that the biv belongs
3338 to. */
3340 struct iv_class **reg_biv_class;
3342 /* The head of a list which links together (via the next field)
3343 every iv class for the current loop. */
3345 struct iv_class *loop_iv_list;
3347 /* Communication with routines called via `note_stores'. */
3349 static rtx note_insn;
3351 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3353 static rtx addr_placeholder;
3355 /* ??? Unfinished optimizations, and possible future optimizations,
3356 for the strength reduction code. */
3358 /* ??? There is one more optimization you might be interested in doing: to
3359 allocate pseudo registers for frequently-accessed memory locations.
3360 If the same memory location is referenced each time around, it might
3361 be possible to copy it into a register before and out after.
3362 This is especially useful when the memory location is a variable which
3363 is in a stack slot because somewhere its address is taken. If the
3364 loop doesn't contain a function call and the variable isn't volatile,
3365 it is safe to keep the value in a register for the duration of the
3366 loop. One tricky thing is that the copying of the value back from the
3367 register has to be done on all exits from the loop. You need to check that
3368 all the exits from the loop go to the same place. */
3370 /* ??? The interaction of biv elimination, and recognition of 'constant'
3371 bivs, may cause problems. */
3373 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3374 performance problems.
3376 Perhaps don't eliminate things that can be combined with an addressing
3377 mode. Find all givs that have the same biv, mult_val, and add_val;
3378 then for each giv, check to see if its only use dies in a following
3379 memory address. If so, generate a new memory address and check to see
3380 if it is valid. If it is valid, then store the modified memory address,
3381 otherwise, mark the giv as not done so that it will get its own iv. */
3383 /* ??? Could try to optimize branches when it is known that a biv is always
3384 positive. */
3386 /* ??? When replace a biv in a compare insn, we should replace with closest
3387 giv so that an optimized branch can still be recognized by the combiner,
3388 e.g. the VAX acb insn. */
3390 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3391 was rerun in loop_optimize whenever a register was added or moved.
3392 Also, some of the optimizations could be a little less conservative. */
3394 /* Perform strength reduction and induction variable elimination. */
3396 /* Pseudo registers created during this function will be beyond the last
3397 valid index in several tables including n_times_set and regno_last_uid.
3398 This does not cause a problem here, because the added registers cannot be
3399 givs outside of their loop, and hence will never be reconsidered.
3400 But scan_loop must check regnos to make sure they are in bounds. */
3402 static void
3403 strength_reduce (scan_start, end, loop_top, insn_count,
3404 loop_start, loop_end, unroll_p)
3405 rtx scan_start;
3406 rtx end;
3407 rtx loop_top;
3408 int insn_count;
3409 rtx loop_start;
3410 rtx loop_end;
3411 int unroll_p;
3413 rtx p;
3414 rtx set;
3415 rtx inc_val;
3416 rtx mult_val;
3417 rtx dest_reg;
3418 /* This is 1 if current insn is not executed at least once for every loop
3419 iteration. */
3420 int not_every_iteration = 0;
3421 /* This is 1 if current insn may be executed more than once for every
3422 loop iteration. */
3423 int maybe_multiple = 0;
3424 /* Temporary list pointers for traversing loop_iv_list. */
3425 struct iv_class *bl, **backbl;
3426 /* Ratio of extra register life span we can justify
3427 for saving an instruction. More if loop doesn't call subroutines
3428 since in that case saving an insn makes more difference
3429 and more registers are available. */
3430 /* ??? could set this to last value of threshold in move_movables */
3431 int threshold = (loop_has_call ? 1 : 2) * (3 + n_non_fixed_regs);
3432 /* Map of pseudo-register replacements. */
3433 rtx *reg_map;
3434 int call_seen;
3435 rtx test;
3436 rtx end_insert_before;
3437 int loop_depth = 0;
3439 reg_iv_type = (enum iv_mode *) alloca (max_reg_before_loop
3440 * sizeof (enum iv_mode *));
3441 bzero ((char *) reg_iv_type, max_reg_before_loop * sizeof (enum iv_mode *));
3442 reg_iv_info = (struct induction **)
3443 alloca (max_reg_before_loop * sizeof (struct induction *));
3444 bzero ((char *) reg_iv_info, (max_reg_before_loop
3445 * sizeof (struct induction *)));
3446 reg_biv_class = (struct iv_class **)
3447 alloca (max_reg_before_loop * sizeof (struct iv_class *));
3448 bzero ((char *) reg_biv_class, (max_reg_before_loop
3449 * sizeof (struct iv_class *)));
3451 loop_iv_list = 0;
3452 addr_placeholder = gen_reg_rtx (Pmode);
3454 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3455 must be put before this insn, so that they will appear in the right
3456 order (i.e. loop order).
3458 If loop_end is the end of the current function, then emit a
3459 NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3460 dummy note insn. */
3461 if (NEXT_INSN (loop_end) != 0)
3462 end_insert_before = NEXT_INSN (loop_end);
3463 else
3464 end_insert_before = emit_note_after (NOTE_INSN_DELETED, loop_end);
3466 /* Scan through loop to find all possible bivs. */
3468 p = scan_start;
3469 while (1)
3471 p = NEXT_INSN (p);
3472 /* At end of a straight-in loop, we are done.
3473 At end of a loop entered at the bottom, scan the top. */
3474 if (p == scan_start)
3475 break;
3476 if (p == end)
3478 if (loop_top != 0)
3479 p = loop_top;
3480 else
3481 break;
3482 if (p == scan_start)
3483 break;
3486 if (GET_CODE (p) == INSN
3487 && (set = single_set (p))
3488 && GET_CODE (SET_DEST (set)) == REG)
3490 dest_reg = SET_DEST (set);
3491 if (REGNO (dest_reg) < max_reg_before_loop
3492 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
3493 && reg_iv_type[REGNO (dest_reg)] != NOT_BASIC_INDUCT)
3495 if (basic_induction_var (SET_SRC (set), GET_MODE (SET_SRC (set)),
3496 dest_reg, p, &inc_val, &mult_val))
3498 /* It is a possible basic induction variable.
3499 Create and initialize an induction structure for it. */
3501 struct induction *v
3502 = (struct induction *) alloca (sizeof (struct induction));
3504 record_biv (v, p, dest_reg, inc_val, mult_val,
3505 not_every_iteration, maybe_multiple);
3506 reg_iv_type[REGNO (dest_reg)] = BASIC_INDUCT;
3508 else if (REGNO (dest_reg) < max_reg_before_loop)
3509 reg_iv_type[REGNO (dest_reg)] = NOT_BASIC_INDUCT;
3513 /* Past CODE_LABEL, we get to insns that may be executed multiple
3514 times. The only way we can be sure that they can't is if every
3515 jump insn between here and the end of the loop either
3516 returns, exits the loop, is a forward jump, or is a jump
3517 to the loop start. */
3519 if (GET_CODE (p) == CODE_LABEL)
3521 rtx insn = p;
3523 maybe_multiple = 0;
3525 while (1)
3527 insn = NEXT_INSN (insn);
3528 if (insn == scan_start)
3529 break;
3530 if (insn == end)
3532 if (loop_top != 0)
3533 insn = loop_top;
3534 else
3535 break;
3536 if (insn == scan_start)
3537 break;
3540 if (GET_CODE (insn) == JUMP_INSN
3541 && GET_CODE (PATTERN (insn)) != RETURN
3542 && (! condjump_p (insn)
3543 || (JUMP_LABEL (insn) != 0
3544 && JUMP_LABEL (insn) != scan_start
3545 && (INSN_UID (JUMP_LABEL (insn)) >= max_uid_for_loop
3546 || INSN_UID (insn) >= max_uid_for_loop
3547 || (INSN_LUID (JUMP_LABEL (insn))
3548 < INSN_LUID (insn))))))
3550 maybe_multiple = 1;
3551 break;
3556 /* Past a jump, we get to insns for which we can't count
3557 on whether they will be executed during each iteration. */
3558 /* This code appears twice in strength_reduce. There is also similar
3559 code in scan_loop. */
3560 if (GET_CODE (p) == JUMP_INSN
3561 /* If we enter the loop in the middle, and scan around to the
3562 beginning, don't set not_every_iteration for that.
3563 This can be any kind of jump, since we want to know if insns
3564 will be executed if the loop is executed. */
3565 && ! (JUMP_LABEL (p) == loop_top
3566 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3567 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3569 rtx label = 0;
3571 /* If this is a jump outside the loop, then it also doesn't
3572 matter. Check to see if the target of this branch is on the
3573 loop_number_exits_labels list. */
3575 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3576 label;
3577 label = LABEL_NEXTREF (label))
3578 if (XEXP (label, 0) == JUMP_LABEL (p))
3579 break;
3581 if (! label)
3582 not_every_iteration = 1;
3585 else if (GET_CODE (p) == NOTE)
3587 /* At the virtual top of a converted loop, insns are again known to
3588 be executed each iteration: logically, the loop begins here
3589 even though the exit code has been duplicated. */
3590 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3591 not_every_iteration = 0;
3592 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3593 loop_depth++;
3594 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3595 loop_depth--;
3598 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3599 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3600 or not an insn is known to be executed each iteration of the
3601 loop, whether or not any iterations are known to occur.
3603 Therefore, if we have just passed a label and have no more labels
3604 between here and the test insn of the loop, we know these insns
3605 will be executed each iteration. */
3607 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3608 && no_labels_between_p (p, loop_end))
3609 not_every_iteration = 0;
3612 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3613 Make a sanity check against n_times_set. */
3614 for (backbl = &loop_iv_list, bl = *backbl; bl; bl = bl->next)
3616 if (reg_iv_type[bl->regno] != BASIC_INDUCT
3617 /* Above happens if register modified by subreg, etc. */
3618 /* Make sure it is not recognized as a basic induction var: */
3619 || n_times_set[bl->regno] != bl->biv_count
3620 /* If never incremented, it is invariant that we decided not to
3621 move. So leave it alone. */
3622 || ! bl->incremented)
3624 if (loop_dump_stream)
3625 fprintf (loop_dump_stream, "Reg %d: biv discarded, %s\n",
3626 bl->regno,
3627 (reg_iv_type[bl->regno] != BASIC_INDUCT
3628 ? "not induction variable"
3629 : (! bl->incremented ? "never incremented"
3630 : "count error")));
3632 reg_iv_type[bl->regno] = NOT_BASIC_INDUCT;
3633 *backbl = bl->next;
3635 else
3637 backbl = &bl->next;
3639 if (loop_dump_stream)
3640 fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
3644 /* Exit if there are no bivs. */
3645 if (! loop_iv_list)
3647 /* Can still unroll the loop anyways, but indicate that there is no
3648 strength reduction info available. */
3649 if (unroll_p)
3650 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 0);
3652 return;
3655 /* Find initial value for each biv by searching backwards from loop_start,
3656 halting at first label. Also record any test condition. */
3658 call_seen = 0;
3659 for (p = loop_start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3661 note_insn = p;
3663 if (GET_CODE (p) == CALL_INSN)
3664 call_seen = 1;
3666 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3667 || GET_CODE (p) == CALL_INSN)
3668 note_stores (PATTERN (p), record_initial);
3670 /* Record any test of a biv that branches around the loop if no store
3671 between it and the start of loop. We only care about tests with
3672 constants and registers and only certain of those. */
3673 if (GET_CODE (p) == JUMP_INSN
3674 && JUMP_LABEL (p) != 0
3675 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end)
3676 && (test = get_condition_for_loop (p)) != 0
3677 && GET_CODE (XEXP (test, 0)) == REG
3678 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3679 && (bl = reg_biv_class[REGNO (XEXP (test, 0))]) != 0
3680 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop_start)
3681 && bl->init_insn == 0)
3683 /* If an NE test, we have an initial value! */
3684 if (GET_CODE (test) == NE)
3686 bl->init_insn = p;
3687 bl->init_set = gen_rtx_SET (VOIDmode,
3688 XEXP (test, 0), XEXP (test, 1));
3690 else
3691 bl->initial_test = test;
3695 /* Look at the each biv and see if we can say anything better about its
3696 initial value from any initializing insns set up above. (This is done
3697 in two passes to avoid missing SETs in a PARALLEL.) */
3698 for (bl = loop_iv_list; bl; bl = bl->next)
3700 rtx src;
3701 rtx note;
3703 if (! bl->init_insn)
3704 continue;
3706 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
3707 is a constant, use the value of that. */
3708 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
3709 && CONSTANT_P (XEXP (note, 0)))
3710 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
3711 && CONSTANT_P (XEXP (note, 0))))
3712 src = XEXP (note, 0);
3713 else
3714 src = SET_SRC (bl->init_set);
3716 if (loop_dump_stream)
3717 fprintf (loop_dump_stream,
3718 "Biv %d initialized at insn %d: initial value ",
3719 bl->regno, INSN_UID (bl->init_insn));
3721 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
3722 || GET_MODE (src) == VOIDmode)
3723 && valid_initial_value_p (src, bl->init_insn, call_seen, loop_start))
3725 bl->initial_value = src;
3727 if (loop_dump_stream)
3729 if (GET_CODE (src) == CONST_INT)
3731 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (src));
3732 fputc ('\n', loop_dump_stream);
3734 else
3736 print_rtl (loop_dump_stream, src);
3737 fprintf (loop_dump_stream, "\n");
3741 else
3743 /* Biv initial value is not simple move,
3744 so let it keep initial value of "itself". */
3746 if (loop_dump_stream)
3747 fprintf (loop_dump_stream, "is complex\n");
3751 /* Search the loop for general induction variables. */
3753 /* A register is a giv if: it is only set once, it is a function of a
3754 biv and a constant (or invariant), and it is not a biv. */
3756 not_every_iteration = 0;
3757 loop_depth = 0;
3758 p = scan_start;
3759 while (1)
3761 p = NEXT_INSN (p);
3762 /* At end of a straight-in loop, we are done.
3763 At end of a loop entered at the bottom, scan the top. */
3764 if (p == scan_start)
3765 break;
3766 if (p == end)
3768 if (loop_top != 0)
3769 p = loop_top;
3770 else
3771 break;
3772 if (p == scan_start)
3773 break;
3776 /* Look for a general induction variable in a register. */
3777 if (GET_CODE (p) == INSN
3778 && (set = single_set (p))
3779 && GET_CODE (SET_DEST (set)) == REG
3780 && ! may_not_optimize[REGNO (SET_DEST (set))])
3782 rtx src_reg;
3783 rtx add_val;
3784 rtx mult_val;
3785 int benefit;
3786 rtx regnote = 0;
3788 dest_reg = SET_DEST (set);
3789 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
3790 continue;
3792 if (/* SET_SRC is a giv. */
3793 ((benefit = general_induction_var (SET_SRC (set),
3794 &src_reg, &add_val,
3795 &mult_val))
3796 /* Equivalent expression is a giv. */
3797 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
3798 && (benefit = general_induction_var (XEXP (regnote, 0),
3799 &src_reg,
3800 &add_val, &mult_val))))
3801 /* Don't try to handle any regs made by loop optimization.
3802 We have nothing on them in regno_first_uid, etc. */
3803 && REGNO (dest_reg) < max_reg_before_loop
3804 /* Don't recognize a BASIC_INDUCT_VAR here. */
3805 && dest_reg != src_reg
3806 /* This must be the only place where the register is set. */
3807 && (n_times_set[REGNO (dest_reg)] == 1
3808 /* or all sets must be consecutive and make a giv. */
3809 || (benefit = consec_sets_giv (benefit, p,
3810 src_reg, dest_reg,
3811 &add_val, &mult_val))))
3813 int count;
3814 struct induction *v
3815 = (struct induction *) alloca (sizeof (struct induction));
3816 rtx temp;
3818 /* If this is a library call, increase benefit. */
3819 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
3820 benefit += libcall_benefit (p);
3822 /* Skip the consecutive insns, if there are any. */
3823 for (count = n_times_set[REGNO (dest_reg)] - 1;
3824 count > 0; count--)
3826 /* If first insn of libcall sequence, skip to end.
3827 Do this at start of loop, since INSN is guaranteed to
3828 be an insn here. */
3829 if (GET_CODE (p) != NOTE
3830 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3831 p = XEXP (temp, 0);
3833 do p = NEXT_INSN (p);
3834 while (GET_CODE (p) == NOTE);
3837 record_giv (v, p, src_reg, dest_reg, mult_val, add_val, benefit,
3838 DEST_REG, not_every_iteration, NULL_PTR, loop_start,
3839 loop_end);
3844 #ifndef DONT_REDUCE_ADDR
3845 /* Look for givs which are memory addresses. */
3846 /* This resulted in worse code on a VAX 8600. I wonder if it
3847 still does. */
3848 if (GET_CODE (p) == INSN)
3849 find_mem_givs (PATTERN (p), p, not_every_iteration, loop_start,
3850 loop_end);
3851 #endif
3853 /* Update the status of whether giv can derive other givs. This can
3854 change when we pass a label or an insn that updates a biv. */
3855 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3856 || GET_CODE (p) == CODE_LABEL)
3857 update_giv_derive (p);
3859 /* Past a jump, we get to insns for which we can't count
3860 on whether they will be executed during each iteration. */
3861 /* This code appears twice in strength_reduce. There is also similar
3862 code in scan_loop. */
3863 if (GET_CODE (p) == JUMP_INSN
3864 /* If we enter the loop in the middle, and scan around to the
3865 beginning, don't set not_every_iteration for that.
3866 This can be any kind of jump, since we want to know if insns
3867 will be executed if the loop is executed. */
3868 && ! (JUMP_LABEL (p) == loop_top
3869 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3870 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3872 rtx label = 0;
3874 /* If this is a jump outside the loop, then it also doesn't
3875 matter. Check to see if the target of this branch is on the
3876 loop_number_exits_labels list. */
3878 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3879 label;
3880 label = LABEL_NEXTREF (label))
3881 if (XEXP (label, 0) == JUMP_LABEL (p))
3882 break;
3884 if (! label)
3885 not_every_iteration = 1;
3888 else if (GET_CODE (p) == NOTE)
3890 /* At the virtual top of a converted loop, insns are again known to
3891 be executed each iteration: logically, the loop begins here
3892 even though the exit code has been duplicated. */
3893 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3894 not_every_iteration = 0;
3895 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3896 loop_depth++;
3897 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3898 loop_depth--;
3901 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3902 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3903 or not an insn is known to be executed each iteration of the
3904 loop, whether or not any iterations are known to occur.
3906 Therefore, if we have just passed a label and have no more labels
3907 between here and the test insn of the loop, we know these insns
3908 will be executed each iteration. */
3910 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3911 && no_labels_between_p (p, loop_end))
3912 not_every_iteration = 0;
3915 /* Try to calculate and save the number of loop iterations. This is
3916 set to zero if the actual number can not be calculated. This must
3917 be called after all giv's have been identified, since otherwise it may
3918 fail if the iteration variable is a giv. */
3920 loop_n_iterations = loop_iterations (loop_start, loop_end);
3922 /* Now for each giv for which we still don't know whether or not it is
3923 replaceable, check to see if it is replaceable because its final value
3924 can be calculated. This must be done after loop_iterations is called,
3925 so that final_giv_value will work correctly. */
3927 for (bl = loop_iv_list; bl; bl = bl->next)
3929 struct induction *v;
3931 for (v = bl->giv; v; v = v->next_iv)
3932 if (! v->replaceable && ! v->not_replaceable)
3933 check_final_value (v, loop_start, loop_end);
3936 /* Try to prove that the loop counter variable (if any) is always
3937 nonnegative; if so, record that fact with a REG_NONNEG note
3938 so that "decrement and branch until zero" insn can be used. */
3939 check_dbra_loop (loop_end, insn_count, loop_start);
3941 #ifdef HAIFA
3942 /* record loop-variables relevant for BCT optimization before unrolling
3943 the loop. Unrolling may update part of this information, and the
3944 correct data will be used for generating the BCT. */
3945 #ifdef HAVE_decrement_and_branch_on_count
3946 if (HAVE_decrement_and_branch_on_count)
3947 analyze_loop_iterations (loop_start, loop_end);
3948 #endif
3949 #endif /* HAIFA */
3951 /* Create reg_map to hold substitutions for replaceable giv regs. */
3952 reg_map = (rtx *) alloca (max_reg_before_loop * sizeof (rtx));
3953 bzero ((char *) reg_map, max_reg_before_loop * sizeof (rtx));
3955 /* Examine each iv class for feasibility of strength reduction/induction
3956 variable elimination. */
3958 for (bl = loop_iv_list; bl; bl = bl->next)
3960 struct induction *v;
3961 int benefit;
3962 int all_reduced;
3963 rtx final_value = 0;
3965 /* Test whether it will be possible to eliminate this biv
3966 provided all givs are reduced. This is possible if either
3967 the reg is not used outside the loop, or we can compute
3968 what its final value will be.
3970 For architectures with a decrement_and_branch_until_zero insn,
3971 don't do this if we put a REG_NONNEG note on the endtest for
3972 this biv. */
3974 /* Compare against bl->init_insn rather than loop_start.
3975 We aren't concerned with any uses of the biv between
3976 init_insn and loop_start since these won't be affected
3977 by the value of the biv elsewhere in the function, so
3978 long as init_insn doesn't use the biv itself.
3979 March 14, 1989 -- self@bayes.arc.nasa.gov */
3981 if ((uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
3982 && bl->init_insn
3983 && INSN_UID (bl->init_insn) < max_uid_for_loop
3984 && uid_luid[REGNO_FIRST_UID (bl->regno)] >= INSN_LUID (bl->init_insn)
3985 #ifdef HAVE_decrement_and_branch_until_zero
3986 && ! bl->nonneg
3987 #endif
3988 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
3989 || ((final_value = final_biv_value (bl, loop_start, loop_end))
3990 #ifdef HAVE_decrement_and_branch_until_zero
3991 && ! bl->nonneg
3992 #endif
3994 bl->eliminable = maybe_eliminate_biv (bl, loop_start, end, 0,
3995 threshold, insn_count);
3996 else
3998 if (loop_dump_stream)
4000 fprintf (loop_dump_stream,
4001 "Cannot eliminate biv %d.\n",
4002 bl->regno);
4003 fprintf (loop_dump_stream,
4004 "First use: insn %d, last use: insn %d.\n",
4005 REGNO_FIRST_UID (bl->regno),
4006 REGNO_LAST_UID (bl->regno));
4010 /* Combine all giv's for this iv_class. */
4011 combine_givs (bl);
4013 /* This will be true at the end, if all givs which depend on this
4014 biv have been strength reduced.
4015 We can't (currently) eliminate the biv unless this is so. */
4016 all_reduced = 1;
4018 /* Check each giv in this class to see if we will benefit by reducing
4019 it. Skip giv's combined with others. */
4020 for (v = bl->giv; v; v = v->next_iv)
4022 struct induction *tv;
4024 if (v->ignore || v->same)
4025 continue;
4027 benefit = v->benefit;
4029 /* Reduce benefit if not replaceable, since we will insert
4030 a move-insn to replace the insn that calculates this giv.
4031 Don't do this unless the giv is a user variable, since it
4032 will often be marked non-replaceable because of the duplication
4033 of the exit code outside the loop. In such a case, the copies
4034 we insert are dead and will be deleted. So they don't have
4035 a cost. Similar situations exist. */
4036 /* ??? The new final_[bg]iv_value code does a much better job
4037 of finding replaceable giv's, and hence this code may no longer
4038 be necessary. */
4039 if (! v->replaceable && ! bl->eliminable
4040 && REG_USERVAR_P (v->dest_reg))
4041 benefit -= copy_cost;
4043 /* Decrease the benefit to count the add-insns that we will
4044 insert to increment the reduced reg for the giv. */
4045 benefit -= add_cost * bl->biv_count;
4047 /* Decide whether to strength-reduce this giv or to leave the code
4048 unchanged (recompute it from the biv each time it is used).
4049 This decision can be made independently for each giv. */
4051 #ifdef AUTO_INC_DEC
4052 /* Attempt to guess whether autoincrement will handle some of the
4053 new add insns; if so, increase BENEFIT (undo the subtraction of
4054 add_cost that was done above). */
4055 if (v->giv_type == DEST_ADDR
4056 && GET_CODE (v->mult_val) == CONST_INT)
4058 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_PRE_INCREMENT)
4059 if (INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4060 benefit += add_cost * bl->biv_count;
4061 #endif
4062 #if defined (HAVE_POST_DECREMENT) || defined (HAVE_PRE_DECREMENT)
4063 if (-INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4064 benefit += add_cost * bl->biv_count;
4065 #endif
4067 #endif
4069 /* If an insn is not to be strength reduced, then set its ignore
4070 flag, and clear all_reduced. */
4072 /* A giv that depends on a reversed biv must be reduced if it is
4073 used after the loop exit, otherwise, it would have the wrong
4074 value after the loop exit. To make it simple, just reduce all
4075 of such giv's whether or not we know they are used after the loop
4076 exit. */
4078 if ( ! flag_reduce_all_givs && v->lifetime * threshold * benefit < insn_count
4079 && ! bl->reversed )
4081 if (loop_dump_stream)
4082 fprintf (loop_dump_stream,
4083 "giv of insn %d not worth while, %d vs %d.\n",
4084 INSN_UID (v->insn),
4085 v->lifetime * threshold * benefit, insn_count);
4086 v->ignore = 1;
4087 all_reduced = 0;
4089 else
4091 /* Check that we can increment the reduced giv without a
4092 multiply insn. If not, reject it. */
4094 for (tv = bl->biv; tv; tv = tv->next_iv)
4095 if (tv->mult_val == const1_rtx
4096 && ! product_cheap_p (tv->add_val, v->mult_val))
4098 if (loop_dump_stream)
4099 fprintf (loop_dump_stream,
4100 "giv of insn %d: would need a multiply.\n",
4101 INSN_UID (v->insn));
4102 v->ignore = 1;
4103 all_reduced = 0;
4104 break;
4109 /* Reduce each giv that we decided to reduce. */
4111 for (v = bl->giv; v; v = v->next_iv)
4113 struct induction *tv;
4114 if (! v->ignore && v->same == 0)
4116 int auto_inc_opt = 0;
4118 v->new_reg = gen_reg_rtx (v->mode);
4120 #ifdef AUTO_INC_DEC
4121 /* If the target has auto-increment addressing modes, and
4122 this is an address giv, then try to put the increment
4123 immediately after its use, so that flow can create an
4124 auto-increment addressing mode. */
4125 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4126 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4127 /* We don't handle reversed biv's because bl->biv->insn
4128 does not have a valid INSN_LUID. */
4129 && ! bl->reversed
4130 && v->always_executed && ! v->maybe_multiple
4131 && INSN_UID (v->insn) < max_uid_for_loop)
4133 /* If other giv's have been combined with this one, then
4134 this will work only if all uses of the other giv's occur
4135 before this giv's insn. This is difficult to check.
4137 We simplify this by looking for the common case where
4138 there is one DEST_REG giv, and this giv's insn is the
4139 last use of the dest_reg of that DEST_REG giv. If the
4140 increment occurs after the address giv, then we can
4141 perform the optimization. (Otherwise, the increment
4142 would have to go before other_giv, and we would not be
4143 able to combine it with the address giv to get an
4144 auto-inc address.) */
4145 if (v->combined_with)
4147 struct induction *other_giv = 0;
4149 for (tv = bl->giv; tv; tv = tv->next_iv)
4150 if (tv->same == v)
4152 if (other_giv)
4153 break;
4154 else
4155 other_giv = tv;
4157 if (! tv && other_giv
4158 && REGNO (other_giv->dest_reg) < max_reg_before_loop
4159 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4160 == INSN_UID (v->insn))
4161 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4162 auto_inc_opt = 1;
4164 /* Check for case where increment is before the address
4165 giv. Do this test in "loop order". */
4166 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4167 && (INSN_LUID (v->insn) < INSN_LUID (scan_start)
4168 || (INSN_LUID (bl->biv->insn)
4169 > INSN_LUID (scan_start))))
4170 || (INSN_LUID (v->insn) < INSN_LUID (scan_start)
4171 && (INSN_LUID (scan_start)
4172 < INSN_LUID (bl->biv->insn))))
4173 auto_inc_opt = -1;
4174 else
4175 auto_inc_opt = 1;
4177 #ifdef HAVE_cc0
4179 rtx prev;
4181 /* We can't put an insn immediately after one setting
4182 cc0, or immediately before one using cc0. */
4183 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4184 || (auto_inc_opt == -1
4185 && (prev = prev_nonnote_insn (v->insn)) != 0
4186 && GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4187 && sets_cc0_p (PATTERN (prev))))
4188 auto_inc_opt = 0;
4190 #endif
4192 if (auto_inc_opt)
4193 v->auto_inc_opt = 1;
4195 #endif
4197 /* For each place where the biv is incremented, add an insn
4198 to increment the new, reduced reg for the giv. */
4199 for (tv = bl->biv; tv; tv = tv->next_iv)
4201 rtx insert_before;
4203 if (! auto_inc_opt)
4204 insert_before = tv->insn;
4205 else if (auto_inc_opt == 1)
4206 insert_before = NEXT_INSN (v->insn);
4207 else
4208 insert_before = v->insn;
4210 if (tv->mult_val == const1_rtx)
4211 emit_iv_add_mult (tv->add_val, v->mult_val,
4212 v->new_reg, v->new_reg, insert_before);
4213 else /* tv->mult_val == const0_rtx */
4214 /* A multiply is acceptable here
4215 since this is presumed to be seldom executed. */
4216 emit_iv_add_mult (tv->add_val, v->mult_val,
4217 v->add_val, v->new_reg, insert_before);
4220 /* Add code at loop start to initialize giv's reduced reg. */
4222 emit_iv_add_mult (bl->initial_value, v->mult_val,
4223 v->add_val, v->new_reg, loop_start);
4227 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4228 as not reduced.
4230 For each giv register that can be reduced now: if replaceable,
4231 substitute reduced reg wherever the old giv occurs;
4232 else add new move insn "giv_reg = reduced_reg".
4234 Also check for givs whose first use is their definition and whose
4235 last use is the definition of another giv. If so, it is likely
4236 dead and should not be used to eliminate a biv. */
4237 for (v = bl->giv; v; v = v->next_iv)
4239 if (v->same && v->same->ignore)
4240 v->ignore = 1;
4242 if (v->ignore)
4243 continue;
4245 if (v->giv_type == DEST_REG
4246 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4248 struct induction *v1;
4250 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4251 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4252 v->maybe_dead = 1;
4255 /* Update expression if this was combined, in case other giv was
4256 replaced. */
4257 if (v->same)
4258 v->new_reg = replace_rtx (v->new_reg,
4259 v->same->dest_reg, v->same->new_reg);
4261 if (v->giv_type == DEST_ADDR)
4262 /* Store reduced reg as the address in the memref where we found
4263 this giv. */
4264 validate_change (v->insn, v->location, v->new_reg, 0);
4265 else if (v->replaceable)
4267 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4269 #if 0
4270 /* I can no longer duplicate the original problem. Perhaps
4271 this is unnecessary now? */
4273 /* Replaceable; it isn't strictly necessary to delete the old
4274 insn and emit a new one, because v->dest_reg is now dead.
4276 However, especially when unrolling loops, the special
4277 handling for (set REG0 REG1) in the second cse pass may
4278 make v->dest_reg live again. To avoid this problem, emit
4279 an insn to set the original giv reg from the reduced giv.
4280 We can not delete the original insn, since it may be part
4281 of a LIBCALL, and the code in flow that eliminates dead
4282 libcalls will fail if it is deleted. */
4283 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4284 v->insn);
4285 #endif
4287 else
4289 /* Not replaceable; emit an insn to set the original giv reg from
4290 the reduced giv, same as above. */
4291 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4292 v->insn);
4295 /* When a loop is reversed, givs which depend on the reversed
4296 biv, and which are live outside the loop, must be set to their
4297 correct final value. This insn is only needed if the giv is
4298 not replaceable. The correct final value is the same as the
4299 value that the giv starts the reversed loop with. */
4300 if (bl->reversed && ! v->replaceable)
4301 emit_iv_add_mult (bl->initial_value, v->mult_val,
4302 v->add_val, v->dest_reg, end_insert_before);
4303 else if (v->final_value)
4305 rtx insert_before;
4307 /* If the loop has multiple exits, emit the insn before the
4308 loop to ensure that it will always be executed no matter
4309 how the loop exits. Otherwise, emit the insn after the loop,
4310 since this is slightly more efficient. */
4311 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4312 insert_before = loop_start;
4313 else
4314 insert_before = end_insert_before;
4315 emit_insn_before (gen_move_insn (v->dest_reg, v->final_value),
4316 insert_before);
4318 #if 0
4319 /* If the insn to set the final value of the giv was emitted
4320 before the loop, then we must delete the insn inside the loop
4321 that sets it. If this is a LIBCALL, then we must delete
4322 every insn in the libcall. Note, however, that
4323 final_giv_value will only succeed when there are multiple
4324 exits if the giv is dead at each exit, hence it does not
4325 matter that the original insn remains because it is dead
4326 anyways. */
4327 /* Delete the insn inside the loop that sets the giv since
4328 the giv is now set before (or after) the loop. */
4329 delete_insn (v->insn);
4330 #endif
4333 if (loop_dump_stream)
4335 fprintf (loop_dump_stream, "giv at %d reduced to ",
4336 INSN_UID (v->insn));
4337 print_rtl (loop_dump_stream, v->new_reg);
4338 fprintf (loop_dump_stream, "\n");
4342 /* All the givs based on the biv bl have been reduced if they
4343 merit it. */
4345 /* For each giv not marked as maybe dead that has been combined with a
4346 second giv, clear any "maybe dead" mark on that second giv.
4347 v->new_reg will either be or refer to the register of the giv it
4348 combined with.
4350 Doing this clearing avoids problems in biv elimination where a
4351 giv's new_reg is a complex value that can't be put in the insn but
4352 the giv combined with (with a reg as new_reg) is marked maybe_dead.
4353 Since the register will be used in either case, we'd prefer it be
4354 used from the simpler giv. */
4356 for (v = bl->giv; v; v = v->next_iv)
4357 if (! v->maybe_dead && v->same)
4358 v->same->maybe_dead = 0;
4360 /* Try to eliminate the biv, if it is a candidate.
4361 This won't work if ! all_reduced,
4362 since the givs we planned to use might not have been reduced.
4364 We have to be careful that we didn't initially think we could eliminate
4365 this biv because of a giv that we now think may be dead and shouldn't
4366 be used as a biv replacement.
4368 Also, there is the possibility that we may have a giv that looks
4369 like it can be used to eliminate a biv, but the resulting insn
4370 isn't valid. This can happen, for example, on the 88k, where a
4371 JUMP_INSN can compare a register only with zero. Attempts to
4372 replace it with a compare with a constant will fail.
4374 Note that in cases where this call fails, we may have replaced some
4375 of the occurrences of the biv with a giv, but no harm was done in
4376 doing so in the rare cases where it can occur. */
4378 if (all_reduced == 1 && bl->eliminable
4379 && maybe_eliminate_biv (bl, loop_start, end, 1,
4380 threshold, insn_count))
4383 /* ?? If we created a new test to bypass the loop entirely,
4384 or otherwise drop straight in, based on this test, then
4385 we might want to rewrite it also. This way some later
4386 pass has more hope of removing the initialization of this
4387 biv entirely. */
4389 /* If final_value != 0, then the biv may be used after loop end
4390 and we must emit an insn to set it just in case.
4392 Reversed bivs already have an insn after the loop setting their
4393 value, so we don't need another one. We can't calculate the
4394 proper final value for such a biv here anyways. */
4395 if (final_value != 0 && ! bl->reversed)
4397 rtx insert_before;
4399 /* If the loop has multiple exits, emit the insn before the
4400 loop to ensure that it will always be executed no matter
4401 how the loop exits. Otherwise, emit the insn after the
4402 loop, since this is slightly more efficient. */
4403 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4404 insert_before = loop_start;
4405 else
4406 insert_before = end_insert_before;
4408 emit_insn_before (gen_move_insn (bl->biv->dest_reg, final_value),
4409 end_insert_before);
4412 #if 0
4413 /* Delete all of the instructions inside the loop which set
4414 the biv, as they are all dead. If is safe to delete them,
4415 because an insn setting a biv will never be part of a libcall. */
4416 /* However, deleting them will invalidate the regno_last_uid info,
4417 so keeping them around is more convenient. Final_biv_value
4418 will only succeed when there are multiple exits if the biv
4419 is dead at each exit, hence it does not matter that the original
4420 insn remains, because it is dead anyways. */
4421 for (v = bl->biv; v; v = v->next_iv)
4422 delete_insn (v->insn);
4423 #endif
4425 if (loop_dump_stream)
4426 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
4427 bl->regno);
4431 /* Go through all the instructions in the loop, making all the
4432 register substitutions scheduled in REG_MAP. */
4434 for (p = loop_start; p != end; p = NEXT_INSN (p))
4435 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4436 || GET_CODE (p) == CALL_INSN)
4438 replace_regs (PATTERN (p), reg_map, max_reg_before_loop, 0);
4439 replace_regs (REG_NOTES (p), reg_map, max_reg_before_loop, 0);
4440 INSN_CODE (p) = -1;
4443 /* Unroll loops from within strength reduction so that we can use the
4444 induction variable information that strength_reduce has already
4445 collected. */
4447 if (unroll_p)
4448 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 1);
4450 #ifdef HAIFA
4451 /* instrument the loop with bct insn */
4452 #ifdef HAVE_decrement_and_branch_on_count
4453 if (HAVE_decrement_and_branch_on_count)
4454 insert_bct (loop_start, loop_end);
4455 #endif
4456 #endif /* HAIFA */
4458 if (loop_dump_stream)
4459 fprintf (loop_dump_stream, "\n");
4462 /* Return 1 if X is a valid source for an initial value (or as value being
4463 compared against in an initial test).
4465 X must be either a register or constant and must not be clobbered between
4466 the current insn and the start of the loop.
4468 INSN is the insn containing X. */
4470 static int
4471 valid_initial_value_p (x, insn, call_seen, loop_start)
4472 rtx x;
4473 rtx insn;
4474 int call_seen;
4475 rtx loop_start;
4477 if (CONSTANT_P (x))
4478 return 1;
4480 /* Only consider pseudos we know about initialized in insns whose luids
4481 we know. */
4482 if (GET_CODE (x) != REG
4483 || REGNO (x) >= max_reg_before_loop)
4484 return 0;
4486 /* Don't use call-clobbered registers across a call which clobbers it. On
4487 some machines, don't use any hard registers at all. */
4488 if (REGNO (x) < FIRST_PSEUDO_REGISTER
4489 && (SMALL_REGISTER_CLASSES
4490 || (call_used_regs[REGNO (x)] && call_seen)))
4491 return 0;
4493 /* Don't use registers that have been clobbered before the start of the
4494 loop. */
4495 if (reg_set_between_p (x, insn, loop_start))
4496 return 0;
4498 return 1;
4501 /* Scan X for memory refs and check each memory address
4502 as a possible giv. INSN is the insn whose pattern X comes from.
4503 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
4504 every loop iteration. */
4506 static void
4507 find_mem_givs (x, insn, not_every_iteration, loop_start, loop_end)
4508 rtx x;
4509 rtx insn;
4510 int not_every_iteration;
4511 rtx loop_start, loop_end;
4513 register int i, j;
4514 register enum rtx_code code;
4515 register char *fmt;
4517 if (x == 0)
4518 return;
4520 code = GET_CODE (x);
4521 switch (code)
4523 case REG:
4524 case CONST_INT:
4525 case CONST:
4526 case CONST_DOUBLE:
4527 case SYMBOL_REF:
4528 case LABEL_REF:
4529 case PC:
4530 case CC0:
4531 case ADDR_VEC:
4532 case ADDR_DIFF_VEC:
4533 case USE:
4534 case CLOBBER:
4535 return;
4537 case MEM:
4539 rtx src_reg;
4540 rtx add_val;
4541 rtx mult_val;
4542 int benefit;
4544 benefit = general_induction_var (XEXP (x, 0),
4545 &src_reg, &add_val, &mult_val);
4547 /* Don't make a DEST_ADDR giv with mult_val == 1 && add_val == 0.
4548 Such a giv isn't useful. */
4549 if (benefit > 0 && (mult_val != const1_rtx || add_val != const0_rtx))
4551 /* Found one; record it. */
4552 struct induction *v
4553 = (struct induction *) oballoc (sizeof (struct induction));
4555 record_giv (v, insn, src_reg, addr_placeholder, mult_val,
4556 add_val, benefit, DEST_ADDR, not_every_iteration,
4557 &XEXP (x, 0), loop_start, loop_end);
4559 v->mem_mode = GET_MODE (x);
4562 return;
4564 default:
4565 break;
4568 /* Recursively scan the subexpressions for other mem refs. */
4570 fmt = GET_RTX_FORMAT (code);
4571 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4572 if (fmt[i] == 'e')
4573 find_mem_givs (XEXP (x, i), insn, not_every_iteration, loop_start,
4574 loop_end);
4575 else if (fmt[i] == 'E')
4576 for (j = 0; j < XVECLEN (x, i); j++)
4577 find_mem_givs (XVECEXP (x, i, j), insn, not_every_iteration,
4578 loop_start, loop_end);
4581 /* Fill in the data about one biv update.
4582 V is the `struct induction' in which we record the biv. (It is
4583 allocated by the caller, with alloca.)
4584 INSN is the insn that sets it.
4585 DEST_REG is the biv's reg.
4587 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
4588 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
4589 being set to INC_VAL.
4591 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
4592 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
4593 can be executed more than once per iteration. If MAYBE_MULTIPLE
4594 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
4595 executed exactly once per iteration. */
4597 static void
4598 record_biv (v, insn, dest_reg, inc_val, mult_val,
4599 not_every_iteration, maybe_multiple)
4600 struct induction *v;
4601 rtx insn;
4602 rtx dest_reg;
4603 rtx inc_val;
4604 rtx mult_val;
4605 int not_every_iteration;
4606 int maybe_multiple;
4608 struct iv_class *bl;
4610 v->insn = insn;
4611 v->src_reg = dest_reg;
4612 v->dest_reg = dest_reg;
4613 v->mult_val = mult_val;
4614 v->add_val = inc_val;
4615 v->mode = GET_MODE (dest_reg);
4616 v->always_computable = ! not_every_iteration;
4617 v->always_executed = ! not_every_iteration;
4618 v->maybe_multiple = maybe_multiple;
4620 /* Add this to the reg's iv_class, creating a class
4621 if this is the first incrementation of the reg. */
4623 bl = reg_biv_class[REGNO (dest_reg)];
4624 if (bl == 0)
4626 /* Create and initialize new iv_class. */
4628 bl = (struct iv_class *) oballoc (sizeof (struct iv_class));
4630 bl->regno = REGNO (dest_reg);
4631 bl->biv = 0;
4632 bl->giv = 0;
4633 bl->biv_count = 0;
4634 bl->giv_count = 0;
4636 /* Set initial value to the reg itself. */
4637 bl->initial_value = dest_reg;
4638 /* We haven't seen the initializing insn yet */
4639 bl->init_insn = 0;
4640 bl->init_set = 0;
4641 bl->initial_test = 0;
4642 bl->incremented = 0;
4643 bl->eliminable = 0;
4644 bl->nonneg = 0;
4645 bl->reversed = 0;
4646 bl->total_benefit = 0;
4648 /* Add this class to loop_iv_list. */
4649 bl->next = loop_iv_list;
4650 loop_iv_list = bl;
4652 /* Put it in the array of biv register classes. */
4653 reg_biv_class[REGNO (dest_reg)] = bl;
4656 /* Update IV_CLASS entry for this biv. */
4657 v->next_iv = bl->biv;
4658 bl->biv = v;
4659 bl->biv_count++;
4660 if (mult_val == const1_rtx)
4661 bl->incremented = 1;
4663 if (loop_dump_stream)
4665 fprintf (loop_dump_stream,
4666 "Insn %d: possible biv, reg %d,",
4667 INSN_UID (insn), REGNO (dest_reg));
4668 if (GET_CODE (inc_val) == CONST_INT)
4670 fprintf (loop_dump_stream, " const =");
4671 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (inc_val));
4672 fputc ('\n', loop_dump_stream);
4674 else
4676 fprintf (loop_dump_stream, " const = ");
4677 print_rtl (loop_dump_stream, inc_val);
4678 fprintf (loop_dump_stream, "\n");
4683 /* Fill in the data about one giv.
4684 V is the `struct induction' in which we record the giv. (It is
4685 allocated by the caller, with alloca.)
4686 INSN is the insn that sets it.
4687 BENEFIT estimates the savings from deleting this insn.
4688 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
4689 into a register or is used as a memory address.
4691 SRC_REG is the biv reg which the giv is computed from.
4692 DEST_REG is the giv's reg (if the giv is stored in a reg).
4693 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
4694 LOCATION points to the place where this giv's value appears in INSN. */
4696 static void
4697 record_giv (v, insn, src_reg, dest_reg, mult_val, add_val, benefit,
4698 type, not_every_iteration, location, loop_start, loop_end)
4699 struct induction *v;
4700 rtx insn;
4701 rtx src_reg;
4702 rtx dest_reg;
4703 rtx mult_val, add_val;
4704 int benefit;
4705 enum g_types type;
4706 int not_every_iteration;
4707 rtx *location;
4708 rtx loop_start, loop_end;
4710 struct induction *b;
4711 struct iv_class *bl;
4712 rtx set = single_set (insn);
4714 v->insn = insn;
4715 v->src_reg = src_reg;
4716 v->giv_type = type;
4717 v->dest_reg = dest_reg;
4718 v->mult_val = mult_val;
4719 v->add_val = add_val;
4720 v->benefit = benefit;
4721 v->location = location;
4722 v->cant_derive = 0;
4723 v->combined_with = 0;
4724 v->maybe_multiple = 0;
4725 v->maybe_dead = 0;
4726 v->derive_adjustment = 0;
4727 v->same = 0;
4728 v->ignore = 0;
4729 v->new_reg = 0;
4730 v->final_value = 0;
4731 v->same_insn = 0;
4732 v->auto_inc_opt = 0;
4733 v->unrolled = 0;
4734 v->shared = 0;
4736 /* The v->always_computable field is used in update_giv_derive, to
4737 determine whether a giv can be used to derive another giv. For a
4738 DEST_REG giv, INSN computes a new value for the giv, so its value
4739 isn't computable if INSN insn't executed every iteration.
4740 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
4741 it does not compute a new value. Hence the value is always computable
4742 regardless of whether INSN is executed each iteration. */
4744 if (type == DEST_ADDR)
4745 v->always_computable = 1;
4746 else
4747 v->always_computable = ! not_every_iteration;
4749 v->always_executed = ! not_every_iteration;
4751 if (type == DEST_ADDR)
4753 v->mode = GET_MODE (*location);
4754 v->lifetime = 1;
4755 v->times_used = 1;
4757 else /* type == DEST_REG */
4759 v->mode = GET_MODE (SET_DEST (set));
4761 v->lifetime = (uid_luid[REGNO_LAST_UID (REGNO (dest_reg))]
4762 - uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))]);
4764 v->times_used = n_times_used[REGNO (dest_reg)];
4766 /* If the lifetime is zero, it means that this register is
4767 really a dead store. So mark this as a giv that can be
4768 ignored. This will not prevent the biv from being eliminated. */
4769 if (v->lifetime == 0)
4770 v->ignore = 1;
4772 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
4773 reg_iv_info[REGNO (dest_reg)] = v;
4776 /* Add the giv to the class of givs computed from one biv. */
4778 bl = reg_biv_class[REGNO (src_reg)];
4779 if (bl)
4781 v->next_iv = bl->giv;
4782 bl->giv = v;
4783 /* Don't count DEST_ADDR. This is supposed to count the number of
4784 insns that calculate givs. */
4785 if (type == DEST_REG)
4786 bl->giv_count++;
4787 bl->total_benefit += benefit;
4789 else
4790 /* Fatal error, biv missing for this giv? */
4791 abort ();
4793 if (type == DEST_ADDR)
4794 v->replaceable = 1;
4795 else
4797 /* The giv can be replaced outright by the reduced register only if all
4798 of the following conditions are true:
4799 - the insn that sets the giv is always executed on any iteration
4800 on which the giv is used at all
4801 (there are two ways to deduce this:
4802 either the insn is executed on every iteration,
4803 or all uses follow that insn in the same basic block),
4804 - the giv is not used outside the loop
4805 - no assignments to the biv occur during the giv's lifetime. */
4807 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
4808 /* Previous line always fails if INSN was moved by loop opt. */
4809 && uid_luid[REGNO_LAST_UID (REGNO (dest_reg))] < INSN_LUID (loop_end)
4810 && (! not_every_iteration
4811 || last_use_this_basic_block (dest_reg, insn)))
4813 /* Now check that there are no assignments to the biv within the
4814 giv's lifetime. This requires two separate checks. */
4816 /* Check each biv update, and fail if any are between the first
4817 and last use of the giv.
4819 If this loop contains an inner loop that was unrolled, then
4820 the insn modifying the biv may have been emitted by the loop
4821 unrolling code, and hence does not have a valid luid. Just
4822 mark the biv as not replaceable in this case. It is not very
4823 useful as a biv, because it is used in two different loops.
4824 It is very unlikely that we would be able to optimize the giv
4825 using this biv anyways. */
4827 v->replaceable = 1;
4828 for (b = bl->biv; b; b = b->next_iv)
4830 if (INSN_UID (b->insn) >= max_uid_for_loop
4831 || ((uid_luid[INSN_UID (b->insn)]
4832 >= uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))])
4833 && (uid_luid[INSN_UID (b->insn)]
4834 <= uid_luid[REGNO_LAST_UID (REGNO (dest_reg))])))
4836 v->replaceable = 0;
4837 v->not_replaceable = 1;
4838 break;
4842 /* If there are any backwards branches that go from after the
4843 biv update to before it, then this giv is not replaceable. */
4844 if (v->replaceable)
4845 for (b = bl->biv; b; b = b->next_iv)
4846 if (back_branch_in_range_p (b->insn, loop_start, loop_end))
4848 v->replaceable = 0;
4849 v->not_replaceable = 1;
4850 break;
4853 else
4855 /* May still be replaceable, we don't have enough info here to
4856 decide. */
4857 v->replaceable = 0;
4858 v->not_replaceable = 0;
4862 if (loop_dump_stream)
4864 if (type == DEST_REG)
4865 fprintf (loop_dump_stream, "Insn %d: giv reg %d",
4866 INSN_UID (insn), REGNO (dest_reg));
4867 else
4868 fprintf (loop_dump_stream, "Insn %d: dest address",
4869 INSN_UID (insn));
4871 fprintf (loop_dump_stream, " src reg %d benefit %d",
4872 REGNO (src_reg), v->benefit);
4873 fprintf (loop_dump_stream, " used %d lifetime %d",
4874 v->times_used, v->lifetime);
4876 if (v->replaceable)
4877 fprintf (loop_dump_stream, " replaceable");
4879 if (GET_CODE (mult_val) == CONST_INT)
4881 fprintf (loop_dump_stream, " mult ");
4882 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (mult_val));
4884 else
4886 fprintf (loop_dump_stream, " mult ");
4887 print_rtl (loop_dump_stream, mult_val);
4890 if (GET_CODE (add_val) == CONST_INT)
4892 fprintf (loop_dump_stream, " add ");
4893 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (add_val));
4895 else
4897 fprintf (loop_dump_stream, " add ");
4898 print_rtl (loop_dump_stream, add_val);
4902 if (loop_dump_stream)
4903 fprintf (loop_dump_stream, "\n");
4908 /* All this does is determine whether a giv can be made replaceable because
4909 its final value can be calculated. This code can not be part of record_giv
4910 above, because final_giv_value requires that the number of loop iterations
4911 be known, and that can not be accurately calculated until after all givs
4912 have been identified. */
4914 static void
4915 check_final_value (v, loop_start, loop_end)
4916 struct induction *v;
4917 rtx loop_start, loop_end;
4919 struct iv_class *bl;
4920 rtx final_value = 0;
4922 bl = reg_biv_class[REGNO (v->src_reg)];
4924 /* DEST_ADDR givs will never reach here, because they are always marked
4925 replaceable above in record_giv. */
4927 /* The giv can be replaced outright by the reduced register only if all
4928 of the following conditions are true:
4929 - the insn that sets the giv is always executed on any iteration
4930 on which the giv is used at all
4931 (there are two ways to deduce this:
4932 either the insn is executed on every iteration,
4933 or all uses follow that insn in the same basic block),
4934 - its final value can be calculated (this condition is different
4935 than the one above in record_giv)
4936 - no assignments to the biv occur during the giv's lifetime. */
4938 #if 0
4939 /* This is only called now when replaceable is known to be false. */
4940 /* Clear replaceable, so that it won't confuse final_giv_value. */
4941 v->replaceable = 0;
4942 #endif
4944 if ((final_value = final_giv_value (v, loop_start, loop_end))
4945 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
4947 int biv_increment_seen = 0;
4948 rtx p = v->insn;
4949 rtx last_giv_use;
4951 v->replaceable = 1;
4953 /* When trying to determine whether or not a biv increment occurs
4954 during the lifetime of the giv, we can ignore uses of the variable
4955 outside the loop because final_value is true. Hence we can not
4956 use regno_last_uid and regno_first_uid as above in record_giv. */
4958 /* Search the loop to determine whether any assignments to the
4959 biv occur during the giv's lifetime. Start with the insn
4960 that sets the giv, and search around the loop until we come
4961 back to that insn again.
4963 Also fail if there is a jump within the giv's lifetime that jumps
4964 to somewhere outside the lifetime but still within the loop. This
4965 catches spaghetti code where the execution order is not linear, and
4966 hence the above test fails. Here we assume that the giv lifetime
4967 does not extend from one iteration of the loop to the next, so as
4968 to make the test easier. Since the lifetime isn't known yet,
4969 this requires two loops. See also record_giv above. */
4971 last_giv_use = v->insn;
4973 while (1)
4975 p = NEXT_INSN (p);
4976 if (p == loop_end)
4977 p = NEXT_INSN (loop_start);
4978 if (p == v->insn)
4979 break;
4981 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4982 || GET_CODE (p) == CALL_INSN)
4984 if (biv_increment_seen)
4986 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4988 v->replaceable = 0;
4989 v->not_replaceable = 1;
4990 break;
4993 else if (reg_set_p (v->src_reg, PATTERN (p)))
4994 biv_increment_seen = 1;
4995 else if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4996 last_giv_use = p;
5000 /* Now that the lifetime of the giv is known, check for branches
5001 from within the lifetime to outside the lifetime if it is still
5002 replaceable. */
5004 if (v->replaceable)
5006 p = v->insn;
5007 while (1)
5009 p = NEXT_INSN (p);
5010 if (p == loop_end)
5011 p = NEXT_INSN (loop_start);
5012 if (p == last_giv_use)
5013 break;
5015 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
5016 && LABEL_NAME (JUMP_LABEL (p))
5017 && ((INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop)
5018 || (INSN_UID (v->insn) >= max_uid_for_loop)
5019 || (INSN_UID (last_giv_use) >= max_uid_for_loop)
5020 || (INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (v->insn)
5021 && INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop_start))
5022 || (INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (last_giv_use)
5023 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop_end))))
5025 v->replaceable = 0;
5026 v->not_replaceable = 1;
5028 if (loop_dump_stream)
5029 fprintf (loop_dump_stream,
5030 "Found branch outside giv lifetime.\n");
5032 break;
5037 /* If it is replaceable, then save the final value. */
5038 if (v->replaceable)
5039 v->final_value = final_value;
5042 if (loop_dump_stream && v->replaceable)
5043 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
5044 INSN_UID (v->insn), REGNO (v->dest_reg));
5047 /* Update the status of whether a giv can derive other givs.
5049 We need to do something special if there is or may be an update to the biv
5050 between the time the giv is defined and the time it is used to derive
5051 another giv.
5053 In addition, a giv that is only conditionally set is not allowed to
5054 derive another giv once a label has been passed.
5056 The cases we look at are when a label or an update to a biv is passed. */
5058 static void
5059 update_giv_derive (p)
5060 rtx p;
5062 struct iv_class *bl;
5063 struct induction *biv, *giv;
5064 rtx tem;
5065 int dummy;
5067 /* Search all IV classes, then all bivs, and finally all givs.
5069 There are three cases we are concerned with. First we have the situation
5070 of a giv that is only updated conditionally. In that case, it may not
5071 derive any givs after a label is passed.
5073 The second case is when a biv update occurs, or may occur, after the
5074 definition of a giv. For certain biv updates (see below) that are
5075 known to occur between the giv definition and use, we can adjust the
5076 giv definition. For others, or when the biv update is conditional,
5077 we must prevent the giv from deriving any other givs. There are two
5078 sub-cases within this case.
5080 If this is a label, we are concerned with any biv update that is done
5081 conditionally, since it may be done after the giv is defined followed by
5082 a branch here (actually, we need to pass both a jump and a label, but
5083 this extra tracking doesn't seem worth it).
5085 If this is a jump, we are concerned about any biv update that may be
5086 executed multiple times. We are actually only concerned about
5087 backward jumps, but it is probably not worth performing the test
5088 on the jump again here.
5090 If this is a biv update, we must adjust the giv status to show that a
5091 subsequent biv update was performed. If this adjustment cannot be done,
5092 the giv cannot derive further givs. */
5094 for (bl = loop_iv_list; bl; bl = bl->next)
5095 for (biv = bl->biv; biv; biv = biv->next_iv)
5096 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
5097 || biv->insn == p)
5099 for (giv = bl->giv; giv; giv = giv->next_iv)
5101 /* If cant_derive is already true, there is no point in
5102 checking all of these conditions again. */
5103 if (giv->cant_derive)
5104 continue;
5106 /* If this giv is conditionally set and we have passed a label,
5107 it cannot derive anything. */
5108 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
5109 giv->cant_derive = 1;
5111 /* Skip givs that have mult_val == 0, since
5112 they are really invariants. Also skip those that are
5113 replaceable, since we know their lifetime doesn't contain
5114 any biv update. */
5115 else if (giv->mult_val == const0_rtx || giv->replaceable)
5116 continue;
5118 /* The only way we can allow this giv to derive another
5119 is if this is a biv increment and we can form the product
5120 of biv->add_val and giv->mult_val. In this case, we will
5121 be able to compute a compensation. */
5122 else if (biv->insn == p)
5124 tem = 0;
5126 if (biv->mult_val == const1_rtx)
5127 tem = simplify_giv_expr (gen_rtx_MULT (giv->mode,
5128 biv->add_val,
5129 giv->mult_val),
5130 &dummy);
5132 if (tem && giv->derive_adjustment)
5133 tem = simplify_giv_expr (gen_rtx_PLUS (giv->mode, tem,
5134 giv->derive_adjustment),
5135 &dummy);
5136 if (tem)
5137 giv->derive_adjustment = tem;
5138 else
5139 giv->cant_derive = 1;
5141 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5142 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
5143 giv->cant_derive = 1;
5148 /* Check whether an insn is an increment legitimate for a basic induction var.
5149 X is the source of insn P, or a part of it.
5150 MODE is the mode in which X should be interpreted.
5152 DEST_REG is the putative biv, also the destination of the insn.
5153 We accept patterns of these forms:
5154 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5155 REG = INVARIANT + REG
5157 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5158 and store the additive term into *INC_VAL.
5160 If X is an assignment of an invariant into DEST_REG, we set
5161 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5163 We also want to detect a BIV when it corresponds to a variable
5164 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5165 of the variable may be a PLUS that adds a SUBREG of that variable to
5166 an invariant and then sign- or zero-extends the result of the PLUS
5167 into the variable.
5169 Most GIVs in such cases will be in the promoted mode, since that is the
5170 probably the natural computation mode (and almost certainly the mode
5171 used for addresses) on the machine. So we view the pseudo-reg containing
5172 the variable as the BIV, as if it were simply incremented.
5174 Note that treating the entire pseudo as a BIV will result in making
5175 simple increments to any GIVs based on it. However, if the variable
5176 overflows in its declared mode but not its promoted mode, the result will
5177 be incorrect. This is acceptable if the variable is signed, since
5178 overflows in such cases are undefined, but not if it is unsigned, since
5179 those overflows are defined. So we only check for SIGN_EXTEND and
5180 not ZERO_EXTEND.
5182 If we cannot find a biv, we return 0. */
5184 static int
5185 basic_induction_var (x, mode, dest_reg, p, inc_val, mult_val)
5186 register rtx x;
5187 enum machine_mode mode;
5188 rtx p;
5189 rtx dest_reg;
5190 rtx *inc_val;
5191 rtx *mult_val;
5193 register enum rtx_code code;
5194 rtx arg;
5195 rtx insn, set = 0;
5197 code = GET_CODE (x);
5198 switch (code)
5200 case PLUS:
5201 if (XEXP (x, 0) == dest_reg
5202 || (GET_CODE (XEXP (x, 0)) == SUBREG
5203 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5204 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
5205 arg = XEXP (x, 1);
5206 else if (XEXP (x, 1) == dest_reg
5207 || (GET_CODE (XEXP (x, 1)) == SUBREG
5208 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5209 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
5210 arg = XEXP (x, 0);
5211 else
5212 return 0;
5214 if (invariant_p (arg) != 1)
5215 return 0;
5217 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
5218 *mult_val = const1_rtx;
5219 return 1;
5221 case SUBREG:
5222 /* If this is a SUBREG for a promoted variable, check the inner
5223 value. */
5224 if (SUBREG_PROMOTED_VAR_P (x))
5225 return basic_induction_var (SUBREG_REG (x), GET_MODE (SUBREG_REG (x)),
5226 dest_reg, p, inc_val, mult_val);
5227 return 0;
5229 case REG:
5230 /* If this register is assigned in the previous insn, look at its
5231 source, but don't go outside the loop or past a label. */
5233 for (insn = PREV_INSN (p);
5234 (insn && GET_CODE (insn) == NOTE
5235 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5236 insn = PREV_INSN (insn))
5239 if (insn)
5240 set = single_set (insn);
5242 if (set != 0
5243 && (SET_DEST (set) == x
5244 || (GET_CODE (SET_DEST (set)) == SUBREG
5245 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
5246 <= UNITS_PER_WORD)
5247 && SUBREG_REG (SET_DEST (set)) == x)))
5248 return basic_induction_var (SET_SRC (set),
5249 (GET_MODE (SET_SRC (set)) == VOIDmode
5250 ? GET_MODE (x)
5251 : GET_MODE (SET_SRC (set))),
5252 dest_reg, insn,
5253 inc_val, mult_val);
5254 /* ... fall through ... */
5256 /* Can accept constant setting of biv only when inside inner most loop.
5257 Otherwise, a biv of an inner loop may be incorrectly recognized
5258 as a biv of the outer loop,
5259 causing code to be moved INTO the inner loop. */
5260 case MEM:
5261 if (invariant_p (x) != 1)
5262 return 0;
5263 case CONST_INT:
5264 case SYMBOL_REF:
5265 case CONST:
5266 /* convert_modes aborts if we try to convert to or from CCmode, so just
5267 exclude that case. It is very unlikely that a condition code value
5268 would be a useful iterator anyways. */
5269 if (loops_enclosed == 1
5270 && GET_MODE_CLASS (mode) != MODE_CC
5271 && GET_MODE_CLASS (GET_MODE (dest_reg)) != MODE_CC)
5273 /* Possible bug here? Perhaps we don't know the mode of X. */
5274 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
5275 *mult_val = const0_rtx;
5276 return 1;
5278 else
5279 return 0;
5281 case SIGN_EXTEND:
5282 return basic_induction_var (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5283 dest_reg, p, inc_val, mult_val);
5284 case ASHIFTRT:
5285 /* Similar, since this can be a sign extension. */
5286 for (insn = PREV_INSN (p);
5287 (insn && GET_CODE (insn) == NOTE
5288 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5289 insn = PREV_INSN (insn))
5292 if (insn)
5293 set = single_set (insn);
5295 if (set && SET_DEST (set) == XEXP (x, 0)
5296 && GET_CODE (XEXP (x, 1)) == CONST_INT
5297 && INTVAL (XEXP (x, 1)) >= 0
5298 && GET_CODE (SET_SRC (set)) == ASHIFT
5299 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
5300 return basic_induction_var (XEXP (SET_SRC (set), 0),
5301 GET_MODE (XEXP (x, 0)),
5302 dest_reg, insn, inc_val, mult_val);
5303 return 0;
5305 default:
5306 return 0;
5310 /* A general induction variable (giv) is any quantity that is a linear
5311 function of a basic induction variable,
5312 i.e. giv = biv * mult_val + add_val.
5313 The coefficients can be any loop invariant quantity.
5314 A giv need not be computed directly from the biv;
5315 it can be computed by way of other givs. */
5317 /* Determine whether X computes a giv.
5318 If it does, return a nonzero value
5319 which is the benefit from eliminating the computation of X;
5320 set *SRC_REG to the register of the biv that it is computed from;
5321 set *ADD_VAL and *MULT_VAL to the coefficients,
5322 such that the value of X is biv * mult + add; */
5324 static int
5325 general_induction_var (x, src_reg, add_val, mult_val)
5326 rtx x;
5327 rtx *src_reg;
5328 rtx *add_val;
5329 rtx *mult_val;
5331 rtx orig_x = x;
5332 int benefit = 0;
5333 char *storage;
5335 /* If this is an invariant, forget it, it isn't a giv. */
5336 if (invariant_p (x) == 1)
5337 return 0;
5339 /* See if the expression could be a giv and get its form.
5340 Mark our place on the obstack in case we don't find a giv. */
5341 storage = (char *) oballoc (0);
5342 x = simplify_giv_expr (x, &benefit);
5343 if (x == 0)
5345 obfree (storage);
5346 return 0;
5349 switch (GET_CODE (x))
5351 case USE:
5352 case CONST_INT:
5353 /* Since this is now an invariant and wasn't before, it must be a giv
5354 with MULT_VAL == 0. It doesn't matter which BIV we associate this
5355 with. */
5356 *src_reg = loop_iv_list->biv->dest_reg;
5357 *mult_val = const0_rtx;
5358 *add_val = x;
5359 break;
5361 case REG:
5362 /* This is equivalent to a BIV. */
5363 *src_reg = x;
5364 *mult_val = const1_rtx;
5365 *add_val = const0_rtx;
5366 break;
5368 case PLUS:
5369 /* Either (plus (biv) (invar)) or
5370 (plus (mult (biv) (invar_1)) (invar_2)). */
5371 if (GET_CODE (XEXP (x, 0)) == MULT)
5373 *src_reg = XEXP (XEXP (x, 0), 0);
5374 *mult_val = XEXP (XEXP (x, 0), 1);
5376 else
5378 *src_reg = XEXP (x, 0);
5379 *mult_val = const1_rtx;
5381 *add_val = XEXP (x, 1);
5382 break;
5384 case MULT:
5385 /* ADD_VAL is zero. */
5386 *src_reg = XEXP (x, 0);
5387 *mult_val = XEXP (x, 1);
5388 *add_val = const0_rtx;
5389 break;
5391 default:
5392 abort ();
5395 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
5396 unless they are CONST_INT). */
5397 if (GET_CODE (*add_val) == USE)
5398 *add_val = XEXP (*add_val, 0);
5399 if (GET_CODE (*mult_val) == USE)
5400 *mult_val = XEXP (*mult_val, 0);
5402 benefit += rtx_cost (orig_x, SET);
5404 /* Always return some benefit if this is a giv so it will be detected
5405 as such. This allows elimination of bivs that might otherwise
5406 not be eliminated. */
5407 return benefit == 0 ? 1 : benefit;
5410 /* Given an expression, X, try to form it as a linear function of a biv.
5411 We will canonicalize it to be of the form
5412 (plus (mult (BIV) (invar_1))
5413 (invar_2))
5414 with possible degeneracies.
5416 The invariant expressions must each be of a form that can be used as a
5417 machine operand. We surround then with a USE rtx (a hack, but localized
5418 and certainly unambiguous!) if not a CONST_INT for simplicity in this
5419 routine; it is the caller's responsibility to strip them.
5421 If no such canonicalization is possible (i.e., two biv's are used or an
5422 expression that is neither invariant nor a biv or giv), this routine
5423 returns 0.
5425 For a non-zero return, the result will have a code of CONST_INT, USE,
5426 REG (for a BIV), PLUS, or MULT. No other codes will occur.
5428 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
5430 static rtx
5431 simplify_giv_expr (x, benefit)
5432 rtx x;
5433 int *benefit;
5435 enum machine_mode mode = GET_MODE (x);
5436 rtx arg0, arg1;
5437 rtx tem;
5439 /* If this is not an integer mode, or if we cannot do arithmetic in this
5440 mode, this can't be a giv. */
5441 if (mode != VOIDmode
5442 && (GET_MODE_CLASS (mode) != MODE_INT
5443 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
5444 return 0;
5446 switch (GET_CODE (x))
5448 case PLUS:
5449 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5450 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5451 if (arg0 == 0 || arg1 == 0)
5452 return 0;
5454 /* Put constant last, CONST_INT last if both constant. */
5455 if ((GET_CODE (arg0) == USE
5456 || GET_CODE (arg0) == CONST_INT)
5457 && GET_CODE (arg1) != CONST_INT)
5458 tem = arg0, arg0 = arg1, arg1 = tem;
5460 /* Handle addition of zero, then addition of an invariant. */
5461 if (arg1 == const0_rtx)
5462 return arg0;
5463 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
5464 switch (GET_CODE (arg0))
5466 case CONST_INT:
5467 case USE:
5468 /* Both invariant. Only valid if sum is machine operand.
5469 First strip off possible USE on the operands. */
5470 if (GET_CODE (arg0) == USE)
5471 arg0 = XEXP (arg0, 0);
5473 if (GET_CODE (arg1) == USE)
5474 arg1 = XEXP (arg1, 0);
5476 tem = 0;
5477 if (CONSTANT_P (arg0) && GET_CODE (arg1) == CONST_INT)
5479 tem = plus_constant (arg0, INTVAL (arg1));
5480 if (GET_CODE (tem) != CONST_INT)
5481 tem = gen_rtx_USE (mode, tem);
5483 else
5485 /* Adding two invariants must result in an invariant,
5486 so enclose addition operation inside a USE and
5487 return it. */
5488 tem = gen_rtx_USE (mode, gen_rtx_PLUS (mode, arg0, arg1));
5491 return tem;
5493 case REG:
5494 case MULT:
5495 /* biv + invar or mult + invar. Return sum. */
5496 return gen_rtx_PLUS (mode, arg0, arg1);
5498 case PLUS:
5499 /* (a + invar_1) + invar_2. Associate. */
5500 return simplify_giv_expr (gen_rtx_PLUS (mode,
5501 XEXP (arg0, 0),
5502 gen_rtx_PLUS (mode,
5503 XEXP (arg0, 1), arg1)),
5504 benefit);
5506 default:
5507 abort ();
5510 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
5511 MULT to reduce cases. */
5512 if (GET_CODE (arg0) == REG)
5513 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
5514 if (GET_CODE (arg1) == REG)
5515 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
5517 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
5518 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
5519 Recurse to associate the second PLUS. */
5520 if (GET_CODE (arg1) == MULT)
5521 tem = arg0, arg0 = arg1, arg1 = tem;
5523 if (GET_CODE (arg1) == PLUS)
5524 return simplify_giv_expr (gen_rtx_PLUS (mode,
5525 gen_rtx_PLUS (mode, arg0,
5526 XEXP (arg1, 0)),
5527 XEXP (arg1, 1)),
5528 benefit);
5530 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
5531 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
5532 abort ();
5534 if (XEXP (arg0, 0) != XEXP (arg1, 0))
5535 return 0;
5537 return simplify_giv_expr (gen_rtx_MULT (mode,
5538 XEXP (arg0, 0),
5539 gen_rtx_PLUS (mode,
5540 XEXP (arg0, 1),
5541 XEXP (arg1, 1))),
5542 benefit);
5544 case MINUS:
5545 /* Handle "a - b" as "a + b * (-1)". */
5546 return simplify_giv_expr (gen_rtx_PLUS (mode,
5547 XEXP (x, 0),
5548 gen_rtx_MULT (mode, XEXP (x, 1),
5549 constm1_rtx)),
5550 benefit);
5552 case MULT:
5553 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5554 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5555 if (arg0 == 0 || arg1 == 0)
5556 return 0;
5558 /* Put constant last, CONST_INT last if both constant. */
5559 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
5560 && GET_CODE (arg1) != CONST_INT)
5561 tem = arg0, arg0 = arg1, arg1 = tem;
5563 /* If second argument is not now constant, not giv. */
5564 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
5565 return 0;
5567 /* Handle multiply by 0 or 1. */
5568 if (arg1 == const0_rtx)
5569 return const0_rtx;
5571 else if (arg1 == const1_rtx)
5572 return arg0;
5574 switch (GET_CODE (arg0))
5576 case REG:
5577 /* biv * invar. Done. */
5578 return gen_rtx_MULT (mode, arg0, arg1);
5580 case CONST_INT:
5581 /* Product of two constants. */
5582 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
5584 case USE:
5585 /* invar * invar. Not giv. */
5586 return 0;
5588 case MULT:
5589 /* (a * invar_1) * invar_2. Associate. */
5590 return simplify_giv_expr (gen_rtx_MULT (mode, XEXP (arg0, 0),
5591 gen_rtx_MULT (mode,
5592 XEXP (arg0, 1),
5593 arg1)),
5594 benefit);
5596 case PLUS:
5597 /* (a + invar_1) * invar_2. Distribute. */
5598 return simplify_giv_expr (gen_rtx_PLUS (mode,
5599 gen_rtx_MULT (mode,
5600 XEXP (arg0, 0),
5601 arg1),
5602 gen_rtx_MULT (mode,
5603 XEXP (arg0, 1),
5604 arg1)),
5605 benefit);
5607 default:
5608 abort ();
5611 case ASHIFT:
5612 /* Shift by constant is multiply by power of two. */
5613 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5614 return 0;
5616 return simplify_giv_expr (gen_rtx_MULT (mode,
5617 XEXP (x, 0),
5618 GEN_INT ((HOST_WIDE_INT) 1
5619 << INTVAL (XEXP (x, 1)))),
5620 benefit);
5622 case NEG:
5623 /* "-a" is "a * (-1)" */
5624 return simplify_giv_expr (gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
5625 benefit);
5627 case NOT:
5628 /* "~a" is "-a - 1". Silly, but easy. */
5629 return simplify_giv_expr (gen_rtx_MINUS (mode,
5630 gen_rtx_NEG (mode, XEXP (x, 0)),
5631 const1_rtx),
5632 benefit);
5634 case USE:
5635 /* Already in proper form for invariant. */
5636 return x;
5638 case REG:
5639 /* If this is a new register, we can't deal with it. */
5640 if (REGNO (x) >= max_reg_before_loop)
5641 return 0;
5643 /* Check for biv or giv. */
5644 switch (reg_iv_type[REGNO (x)])
5646 case BASIC_INDUCT:
5647 return x;
5648 case GENERAL_INDUCT:
5650 struct induction *v = reg_iv_info[REGNO (x)];
5652 /* Form expression from giv and add benefit. Ensure this giv
5653 can derive another and subtract any needed adjustment if so. */
5654 *benefit += v->benefit;
5655 if (v->cant_derive)
5656 return 0;
5658 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode, v->src_reg,
5659 v->mult_val),
5660 v->add_val);
5661 if (v->derive_adjustment)
5662 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
5663 return simplify_giv_expr (tem, benefit);
5666 default:
5667 break;
5670 /* Fall through to general case. */
5671 default:
5672 /* If invariant, return as USE (unless CONST_INT).
5673 Otherwise, not giv. */
5674 if (GET_CODE (x) == USE)
5675 x = XEXP (x, 0);
5677 if (invariant_p (x) == 1)
5679 if (GET_CODE (x) == CONST_INT)
5680 return x;
5681 else
5682 return gen_rtx_USE (mode, x);
5684 else
5685 return 0;
5689 /* Help detect a giv that is calculated by several consecutive insns;
5690 for example,
5691 giv = biv * M
5692 giv = giv + A
5693 The caller has already identified the first insn P as having a giv as dest;
5694 we check that all other insns that set the same register follow
5695 immediately after P, that they alter nothing else,
5696 and that the result of the last is still a giv.
5698 The value is 0 if the reg set in P is not really a giv.
5699 Otherwise, the value is the amount gained by eliminating
5700 all the consecutive insns that compute the value.
5702 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
5703 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
5705 The coefficients of the ultimate giv value are stored in
5706 *MULT_VAL and *ADD_VAL. */
5708 static int
5709 consec_sets_giv (first_benefit, p, src_reg, dest_reg,
5710 add_val, mult_val)
5711 int first_benefit;
5712 rtx p;
5713 rtx src_reg;
5714 rtx dest_reg;
5715 rtx *add_val;
5716 rtx *mult_val;
5718 int count;
5719 enum rtx_code code;
5720 int benefit;
5721 rtx temp;
5722 rtx set;
5724 /* Indicate that this is a giv so that we can update the value produced in
5725 each insn of the multi-insn sequence.
5727 This induction structure will be used only by the call to
5728 general_induction_var below, so we can allocate it on our stack.
5729 If this is a giv, our caller will replace the induct var entry with
5730 a new induction structure. */
5731 struct induction *v
5732 = (struct induction *) alloca (sizeof (struct induction));
5733 v->src_reg = src_reg;
5734 v->mult_val = *mult_val;
5735 v->add_val = *add_val;
5736 v->benefit = first_benefit;
5737 v->cant_derive = 0;
5738 v->derive_adjustment = 0;
5740 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
5741 reg_iv_info[REGNO (dest_reg)] = v;
5743 count = n_times_set[REGNO (dest_reg)] - 1;
5745 while (count > 0)
5747 p = NEXT_INSN (p);
5748 code = GET_CODE (p);
5750 /* If libcall, skip to end of call sequence. */
5751 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
5752 p = XEXP (temp, 0);
5754 if (code == INSN
5755 && (set = single_set (p))
5756 && GET_CODE (SET_DEST (set)) == REG
5757 && SET_DEST (set) == dest_reg
5758 && ((benefit = general_induction_var (SET_SRC (set), &src_reg,
5759 add_val, mult_val))
5760 /* Giv created by equivalent expression. */
5761 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
5762 && (benefit = general_induction_var (XEXP (temp, 0), &src_reg,
5763 add_val, mult_val))))
5764 && src_reg == v->src_reg)
5766 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5767 benefit += libcall_benefit (p);
5769 count--;
5770 v->mult_val = *mult_val;
5771 v->add_val = *add_val;
5772 v->benefit = benefit;
5774 else if (code != NOTE)
5776 /* Allow insns that set something other than this giv to a
5777 constant. Such insns are needed on machines which cannot
5778 include long constants and should not disqualify a giv. */
5779 if (code == INSN
5780 && (set = single_set (p))
5781 && SET_DEST (set) != dest_reg
5782 && CONSTANT_P (SET_SRC (set)))
5783 continue;
5785 reg_iv_type[REGNO (dest_reg)] = UNKNOWN_INDUCT;
5786 return 0;
5790 return v->benefit;
5793 /* Return an rtx, if any, that expresses giv G2 as a function of the register
5794 represented by G1. If no such expression can be found, or it is clear that
5795 it cannot possibly be a valid address, 0 is returned.
5797 To perform the computation, we note that
5798 G1 = a * v + b and
5799 G2 = c * v + d
5800 where `v' is the biv.
5802 So G2 = (c/a) * G1 + (d - b*c/a) */
5804 #ifdef ADDRESS_COST
5805 static rtx
5806 express_from (g1, g2)
5807 struct induction *g1, *g2;
5809 rtx mult, add;
5811 /* The value that G1 will be multiplied by must be a constant integer. Also,
5812 the only chance we have of getting a valid address is if b*c/a (see above
5813 for notation) is also an integer. */
5814 if (GET_CODE (g1->mult_val) != CONST_INT
5815 || GET_CODE (g2->mult_val) != CONST_INT
5816 || GET_CODE (g1->add_val) != CONST_INT
5817 || g1->mult_val == const0_rtx
5818 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
5819 return 0;
5821 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
5822 add = plus_constant (g2->add_val, - INTVAL (g1->add_val) * INTVAL (mult));
5824 /* Form simplified final result. */
5825 if (mult == const0_rtx)
5826 return add;
5827 else if (mult == const1_rtx)
5828 mult = g1->dest_reg;
5829 else
5830 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
5832 if (add == const0_rtx)
5833 return mult;
5834 else
5835 return gen_rtx_PLUS (g2->mode, mult, add);
5837 #endif
5839 /* Return 1 if giv G2 can be combined with G1. This means that G2 can use
5840 (either directly or via an address expression) a register used to represent
5841 G1. Set g2->new_reg to a represtation of G1 (normally just
5842 g1->dest_reg). */
5844 static int
5845 combine_givs_p (g1, g2)
5846 struct induction *g1, *g2;
5848 #ifdef ADDRESS_COST
5849 rtx tem;
5850 #endif
5852 /* If these givs are identical, they can be combined. */
5853 if (rtx_equal_p (g1->mult_val, g2->mult_val)
5854 && rtx_equal_p (g1->add_val, g2->add_val))
5856 g2->new_reg = g1->dest_reg;
5857 return 1;
5860 #ifdef ADDRESS_COST
5861 /* If G2 can be expressed as a function of G1 and that function is valid
5862 as an address and no more expensive than using a register for G2,
5863 the expression of G2 in terms of G1 can be used. */
5864 if (g2->giv_type == DEST_ADDR
5865 && (tem = express_from (g1, g2)) != 0
5866 && memory_address_p (g2->mem_mode, tem)
5867 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location))
5869 g2->new_reg = tem;
5870 return 1;
5872 #endif
5874 return 0;
5877 #ifdef GIV_SORT_CRITERION
5878 /* Compare two givs and sort the most desirable one for combinations first.
5879 This is used only in one qsort call below. */
5881 static int
5882 giv_sort (x, y)
5883 struct induction **x, **y;
5885 GIV_SORT_CRITERION (*x, *y);
5887 return 0;
5889 #endif
5891 /* Check all pairs of givs for iv_class BL and see if any can be combined with
5892 any other. If so, point SAME to the giv combined with and set NEW_REG to
5893 be an expression (in terms of the other giv's DEST_REG) equivalent to the
5894 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
5896 static void
5897 combine_givs (bl)
5898 struct iv_class *bl;
5900 struct induction *g1, *g2, **giv_array;
5901 int i, j, giv_count, pass;
5903 /* Count givs, because bl->giv_count is incorrect here. */
5904 giv_count = 0;
5905 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5906 giv_count++;
5908 giv_array
5909 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
5910 i = 0;
5911 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5912 giv_array[i++] = g1;
5914 #ifdef GIV_SORT_CRITERION
5915 /* Sort the givs if GIV_SORT_CRITERION is defined.
5916 This is usually defined for processors which lack
5917 negative register offsets so more givs may be combined. */
5919 if (loop_dump_stream)
5920 fprintf (loop_dump_stream, "%d givs counted, sorting...\n", giv_count);
5922 qsort (giv_array, giv_count, sizeof (struct induction *), giv_sort);
5923 #endif
5925 for (i = 0; i < giv_count; i++)
5927 g1 = giv_array[i];
5928 for (pass = 0; pass <= 1; pass++)
5929 for (j = 0; j < giv_count; j++)
5931 g2 = giv_array[j];
5932 if (g1 != g2
5933 /* First try to combine with replaceable givs, then all givs. */
5934 && (g1->replaceable || pass == 1)
5935 /* If either has already been combined or is to be ignored, can't
5936 combine. */
5937 && ! g1->ignore && ! g2->ignore && ! g1->same && ! g2->same
5938 /* If something has been based on G2, G2 cannot itself be based
5939 on something else. */
5940 && ! g2->combined_with
5941 && combine_givs_p (g1, g2))
5943 /* g2->new_reg set by `combine_givs_p' */
5944 g2->same = g1;
5945 g1->combined_with = 1;
5947 /* If one of these givs is a DEST_REG that was only used
5948 once, by the other giv, this is actually a single use.
5949 The DEST_REG has the correct cost, while the other giv
5950 counts the REG use too often. */
5951 if (g2->giv_type == DEST_REG
5952 && n_times_used[REGNO (g2->dest_reg)] == 1
5953 && reg_mentioned_p (g2->dest_reg, PATTERN (g1->insn)))
5954 g1->benefit = g2->benefit;
5955 else if (g1->giv_type != DEST_REG
5956 || n_times_used[REGNO (g1->dest_reg)] != 1
5957 || ! reg_mentioned_p (g1->dest_reg,
5958 PATTERN (g2->insn)))
5960 g1->benefit += g2->benefit;
5961 g1->times_used += g2->times_used;
5963 /* ??? The new final_[bg]iv_value code does a much better job
5964 of finding replaceable giv's, and hence this code may no
5965 longer be necessary. */
5966 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
5967 g1->benefit -= copy_cost;
5968 g1->lifetime += g2->lifetime;
5970 if (loop_dump_stream)
5971 fprintf (loop_dump_stream, "giv at %d combined with giv at %d\n",
5972 INSN_UID (g2->insn), INSN_UID (g1->insn));
5978 /* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
5980 void
5981 emit_iv_add_mult (b, m, a, reg, insert_before)
5982 rtx b; /* initial value of basic induction variable */
5983 rtx m; /* multiplicative constant */
5984 rtx a; /* additive constant */
5985 rtx reg; /* destination register */
5986 rtx insert_before;
5988 rtx seq;
5989 rtx result;
5991 /* Prevent unexpected sharing of these rtx. */
5992 a = copy_rtx (a);
5993 b = copy_rtx (b);
5995 /* Increase the lifetime of any invariants moved further in code. */
5996 update_reg_last_use (a, insert_before);
5997 update_reg_last_use (b, insert_before);
5998 update_reg_last_use (m, insert_before);
6000 start_sequence ();
6001 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 0);
6002 if (reg != result)
6003 emit_move_insn (reg, result);
6004 seq = gen_sequence ();
6005 end_sequence ();
6007 emit_insn_before (seq, insert_before);
6009 record_base_value (REGNO (reg), b, 0);
6012 /* Test whether A * B can be computed without
6013 an actual multiply insn. Value is 1 if so. */
6015 static int
6016 product_cheap_p (a, b)
6017 rtx a;
6018 rtx b;
6020 int i;
6021 rtx tmp;
6022 struct obstack *old_rtl_obstack = rtl_obstack;
6023 char *storage = (char *) obstack_alloc (&temp_obstack, 0);
6024 int win = 1;
6026 /* If only one is constant, make it B. */
6027 if (GET_CODE (a) == CONST_INT)
6028 tmp = a, a = b, b = tmp;
6030 /* If first constant, both constant, so don't need multiply. */
6031 if (GET_CODE (a) == CONST_INT)
6032 return 1;
6034 /* If second not constant, neither is constant, so would need multiply. */
6035 if (GET_CODE (b) != CONST_INT)
6036 return 0;
6038 /* One operand is constant, so might not need multiply insn. Generate the
6039 code for the multiply and see if a call or multiply, or long sequence
6040 of insns is generated. */
6042 rtl_obstack = &temp_obstack;
6043 start_sequence ();
6044 expand_mult (GET_MODE (a), a, b, NULL_RTX, 0);
6045 tmp = gen_sequence ();
6046 end_sequence ();
6048 if (GET_CODE (tmp) == SEQUENCE)
6050 if (XVEC (tmp, 0) == 0)
6051 win = 1;
6052 else if (XVECLEN (tmp, 0) > 3)
6053 win = 0;
6054 else
6055 for (i = 0; i < XVECLEN (tmp, 0); i++)
6057 rtx insn = XVECEXP (tmp, 0, i);
6059 if (GET_CODE (insn) != INSN
6060 || (GET_CODE (PATTERN (insn)) == SET
6061 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
6062 || (GET_CODE (PATTERN (insn)) == PARALLEL
6063 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
6064 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
6066 win = 0;
6067 break;
6071 else if (GET_CODE (tmp) == SET
6072 && GET_CODE (SET_SRC (tmp)) == MULT)
6073 win = 0;
6074 else if (GET_CODE (tmp) == PARALLEL
6075 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
6076 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
6077 win = 0;
6079 /* Free any storage we obtained in generating this multiply and restore rtl
6080 allocation to its normal obstack. */
6081 obstack_free (&temp_obstack, storage);
6082 rtl_obstack = old_rtl_obstack;
6084 return win;
6087 /* Check to see if loop can be terminated by a "decrement and branch until
6088 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
6089 Also try reversing an increment loop to a decrement loop
6090 to see if the optimization can be performed.
6091 Value is nonzero if optimization was performed. */
6093 /* This is useful even if the architecture doesn't have such an insn,
6094 because it might change a loops which increments from 0 to n to a loop
6095 which decrements from n to 0. A loop that decrements to zero is usually
6096 faster than one that increments from zero. */
6098 /* ??? This could be rewritten to use some of the loop unrolling procedures,
6099 such as approx_final_value, biv_total_increment, loop_iterations, and
6100 final_[bg]iv_value. */
6102 static int
6103 check_dbra_loop (loop_end, insn_count, loop_start)
6104 rtx loop_end;
6105 int insn_count;
6106 rtx loop_start;
6108 struct iv_class *bl;
6109 rtx reg;
6110 rtx jump_label;
6111 rtx final_value;
6112 rtx start_value;
6113 rtx new_add_val;
6114 rtx comparison;
6115 rtx before_comparison;
6116 rtx p;
6117 rtx jump;
6118 rtx first_compare;
6119 int compare_and_branch;
6121 /* If last insn is a conditional branch, and the insn before tests a
6122 register value, try to optimize it. Otherwise, we can't do anything. */
6124 jump = PREV_INSN (loop_end);
6125 comparison = get_condition_for_loop (jump);
6126 if (comparison == 0)
6127 return 0;
6129 /* Try to compute whether the compare/branch at the loop end is one or
6130 two instructions. */
6131 get_condition (jump, &first_compare);
6132 if (first_compare == jump)
6133 compare_and_branch = 1;
6134 else if (first_compare == prev_nonnote_insn (jump))
6135 compare_and_branch = 2;
6136 else
6137 return 0;
6139 /* Check all of the bivs to see if the compare uses one of them.
6140 Skip biv's set more than once because we can't guarantee that
6141 it will be zero on the last iteration. Also skip if the biv is
6142 used between its update and the test insn. */
6144 for (bl = loop_iv_list; bl; bl = bl->next)
6146 if (bl->biv_count == 1
6147 && bl->biv->dest_reg == XEXP (comparison, 0)
6148 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
6149 first_compare))
6150 break;
6153 if (! bl)
6154 return 0;
6156 /* Look for the case where the basic induction variable is always
6157 nonnegative, and equals zero on the last iteration.
6158 In this case, add a reg_note REG_NONNEG, which allows the
6159 m68k DBRA instruction to be used. */
6161 if (((GET_CODE (comparison) == GT
6162 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6163 && INTVAL (XEXP (comparison, 1)) == -1)
6164 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
6165 && GET_CODE (bl->biv->add_val) == CONST_INT
6166 && INTVAL (bl->biv->add_val) < 0)
6168 /* Initial value must be greater than 0,
6169 init_val % -dec_value == 0 to ensure that it equals zero on
6170 the last iteration */
6172 if (GET_CODE (bl->initial_value) == CONST_INT
6173 && INTVAL (bl->initial_value) > 0
6174 && (INTVAL (bl->initial_value)
6175 % (-INTVAL (bl->biv->add_val))) == 0)
6177 /* register always nonnegative, add REG_NOTE to branch */
6178 REG_NOTES (PREV_INSN (loop_end))
6179 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
6180 REG_NOTES (PREV_INSN (loop_end)));
6181 bl->nonneg = 1;
6183 return 1;
6186 /* If the decrement is 1 and the value was tested as >= 0 before
6187 the loop, then we can safely optimize. */
6188 for (p = loop_start; p; p = PREV_INSN (p))
6190 if (GET_CODE (p) == CODE_LABEL)
6191 break;
6192 if (GET_CODE (p) != JUMP_INSN)
6193 continue;
6195 before_comparison = get_condition_for_loop (p);
6196 if (before_comparison
6197 && XEXP (before_comparison, 0) == bl->biv->dest_reg
6198 && GET_CODE (before_comparison) == LT
6199 && XEXP (before_comparison, 1) == const0_rtx
6200 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
6201 && INTVAL (bl->biv->add_val) == -1)
6203 REG_NOTES (PREV_INSN (loop_end))
6204 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
6205 REG_NOTES (PREV_INSN (loop_end)));
6206 bl->nonneg = 1;
6208 return 1;
6212 else if (num_mem_sets <= 1)
6214 /* Try to change inc to dec, so can apply above optimization. */
6215 /* Can do this if:
6216 all registers modified are induction variables or invariant,
6217 all memory references have non-overlapping addresses
6218 (obviously true if only one write)
6219 allow 2 insns for the compare/jump at the end of the loop. */
6220 /* Also, we must avoid any instructions which use both the reversed
6221 biv and another biv. Such instructions will fail if the loop is
6222 reversed. We meet this condition by requiring that either
6223 no_use_except_counting is true, or else that there is only
6224 one biv. */
6225 int num_nonfixed_reads = 0;
6226 /* 1 if the iteration var is used only to count iterations. */
6227 int no_use_except_counting = 0;
6228 /* 1 if the loop has no memory store, or it has a single memory store
6229 which is reversible. */
6230 int reversible_mem_store = 1;
6232 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6233 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6234 num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
6236 if (bl->giv_count == 0
6237 && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
6239 rtx bivreg = regno_reg_rtx[bl->regno];
6241 /* If there are no givs for this biv, and the only exit is the
6242 fall through at the end of the loop, then
6243 see if perhaps there are no uses except to count. */
6244 no_use_except_counting = 1;
6245 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6246 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6248 rtx set = single_set (p);
6250 if (set && GET_CODE (SET_DEST (set)) == REG
6251 && REGNO (SET_DEST (set)) == bl->regno)
6252 /* An insn that sets the biv is okay. */
6254 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
6255 || p == prev_nonnote_insn (loop_end))
6256 /* Don't bother about the end test. */
6258 else if (reg_mentioned_p (bivreg, PATTERN (p)))
6259 /* Any other use of the biv is no good. */
6261 no_use_except_counting = 0;
6262 break;
6267 /* If the loop has a single store, and the destination address is
6268 invariant, then we can't reverse the loop, because this address
6269 might then have the wrong value at loop exit.
6270 This would work if the source was invariant also, however, in that
6271 case, the insn should have been moved out of the loop. */
6273 if (num_mem_sets == 1)
6274 reversible_mem_store
6275 = (! unknown_address_altered
6276 && ! invariant_p (XEXP (loop_store_mems[0], 0)));
6278 /* This code only acts for innermost loops. Also it simplifies
6279 the memory address check by only reversing loops with
6280 zero or one memory access.
6281 Two memory accesses could involve parts of the same array,
6282 and that can't be reversed. */
6284 if (num_nonfixed_reads <= 1
6285 && !loop_has_call
6286 && !loop_has_volatile
6287 && reversible_mem_store
6288 && (no_use_except_counting
6289 || ((bl->giv_count + bl->biv_count + num_mem_sets
6290 + num_movables + compare_and_branch == insn_count)
6291 && (bl == loop_iv_list && bl->next == 0))))
6293 rtx tem;
6295 /* Loop can be reversed. */
6296 if (loop_dump_stream)
6297 fprintf (loop_dump_stream, "Can reverse loop\n");
6299 /* Now check other conditions:
6301 The increment must be a constant, as must the initial value,
6302 and the comparison code must be LT.
6304 This test can probably be improved since +/- 1 in the constant
6305 can be obtained by changing LT to LE and vice versa; this is
6306 confusing. */
6308 if (comparison
6309 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6310 /* LE gets turned into LT */
6311 && GET_CODE (comparison) == LT
6312 && GET_CODE (bl->initial_value) == CONST_INT)
6314 HOST_WIDE_INT add_val, comparison_val;
6315 rtx initial_value;
6317 add_val = INTVAL (bl->biv->add_val);
6318 comparison_val = INTVAL (XEXP (comparison, 1));
6319 final_value = XEXP (comparison, 1);
6320 initial_value = bl->initial_value;
6322 /* Normalize the initial value if it is an integer and
6323 has no other use except as a counter. This will allow
6324 a few more loops to be reversed. */
6325 if (no_use_except_counting
6326 && GET_CODE (initial_value) == CONST_INT)
6328 comparison_val = comparison_val - INTVAL (bl->initial_value);
6329 /* Check for overflow. If comparison_val ends up as a
6330 negative value, then we can't reverse the loop. */
6331 if (comparison_val >= 0)
6332 initial_value = const0_rtx;
6335 /* If the initial value is not zero, or if the comparison
6336 value is not an exact multiple of the increment, then we
6337 can not reverse this loop. */
6338 if (initial_value != const0_rtx
6339 || (comparison_val % add_val) != 0)
6340 return 0;
6342 /* Reset these in case we normalized the initial value
6343 and comparison value above. */
6344 bl->initial_value = initial_value;
6345 XEXP (comparison, 1) = GEN_INT (comparison_val);
6347 /* Register will always be nonnegative, with value
6348 0 on last iteration if loop reversed */
6350 /* Save some info needed to produce the new insns. */
6351 reg = bl->biv->dest_reg;
6352 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
6353 if (jump_label == pc_rtx)
6354 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
6355 new_add_val = GEN_INT (- INTVAL (bl->biv->add_val));
6357 start_value = GEN_INT (INTVAL (XEXP (comparison, 1))
6358 - INTVAL (bl->biv->add_val));
6360 /* Initialize biv to start_value before loop start.
6361 The old initializing insn will be deleted as a
6362 dead store by flow.c. */
6363 emit_insn_before (gen_move_insn (reg, start_value), loop_start);
6365 /* Add insn to decrement register, and delete insn
6366 that incremented the register. */
6367 p = emit_insn_before (gen_add2_insn (reg, new_add_val),
6368 bl->biv->insn);
6369 delete_insn (bl->biv->insn);
6371 /* Update biv info to reflect its new status. */
6372 bl->biv->insn = p;
6373 bl->initial_value = start_value;
6374 bl->biv->add_val = new_add_val;
6376 /* Inc LABEL_NUSES so that delete_insn will
6377 not delete the label. */
6378 LABEL_NUSES (XEXP (jump_label, 0)) ++;
6380 /* Emit an insn after the end of the loop to set the biv's
6381 proper exit value if it is used anywhere outside the loop. */
6382 if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
6383 || ! bl->init_insn
6384 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
6385 emit_insn_after (gen_move_insn (reg, final_value),
6386 loop_end);
6388 /* Delete compare/branch at end of loop. */
6389 delete_insn (PREV_INSN (loop_end));
6390 if (compare_and_branch == 2)
6391 delete_insn (first_compare);
6393 /* Add new compare/branch insn at end of loop. */
6394 start_sequence ();
6395 emit_cmp_insn (reg, const0_rtx, GE, NULL_RTX,
6396 GET_MODE (reg), 0, 0);
6397 emit_jump_insn (gen_bge (XEXP (jump_label, 0)));
6398 tem = gen_sequence ();
6399 end_sequence ();
6400 emit_jump_insn_before (tem, loop_end);
6402 for (tem = PREV_INSN (loop_end);
6403 tem && GET_CODE (tem) != JUMP_INSN; tem = PREV_INSN (tem))
6405 if (tem)
6407 JUMP_LABEL (tem) = XEXP (jump_label, 0);
6409 /* Increment of LABEL_NUSES done above. */
6410 /* Register is now always nonnegative,
6411 so add REG_NONNEG note to the branch. */
6412 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
6413 REG_NOTES (tem));
6416 bl->nonneg = 1;
6418 /* Mark that this biv has been reversed. Each giv which depends
6419 on this biv, and which is also live past the end of the loop
6420 will have to be fixed up. */
6422 bl->reversed = 1;
6424 if (loop_dump_stream)
6425 fprintf (loop_dump_stream,
6426 "Reversed loop and added reg_nonneg\n");
6428 return 1;
6433 return 0;
6436 /* Verify whether the biv BL appears to be eliminable,
6437 based on the insns in the loop that refer to it.
6438 LOOP_START is the first insn of the loop, and END is the end insn.
6440 If ELIMINATE_P is non-zero, actually do the elimination.
6442 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
6443 determine whether invariant insns should be placed inside or at the
6444 start of the loop. */
6446 static int
6447 maybe_eliminate_biv (bl, loop_start, end, eliminate_p, threshold, insn_count)
6448 struct iv_class *bl;
6449 rtx loop_start;
6450 rtx end;
6451 int eliminate_p;
6452 int threshold, insn_count;
6454 rtx reg = bl->biv->dest_reg;
6455 rtx p;
6457 /* Scan all insns in the loop, stopping if we find one that uses the
6458 biv in a way that we cannot eliminate. */
6460 for (p = loop_start; p != end; p = NEXT_INSN (p))
6462 enum rtx_code code = GET_CODE (p);
6463 rtx where = threshold >= insn_count ? loop_start : p;
6465 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
6466 && reg_mentioned_p (reg, PATTERN (p))
6467 && ! maybe_eliminate_biv_1 (PATTERN (p), p, bl, eliminate_p, where))
6469 if (loop_dump_stream)
6470 fprintf (loop_dump_stream,
6471 "Cannot eliminate biv %d: biv used in insn %d.\n",
6472 bl->regno, INSN_UID (p));
6473 break;
6477 if (p == end)
6479 if (loop_dump_stream)
6480 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
6481 bl->regno, eliminate_p ? "was" : "can be");
6482 return 1;
6485 return 0;
6488 /* If BL appears in X (part of the pattern of INSN), see if we can
6489 eliminate its use. If so, return 1. If not, return 0.
6491 If BIV does not appear in X, return 1.
6493 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
6494 where extra insns should be added. Depending on how many items have been
6495 moved out of the loop, it will either be before INSN or at the start of
6496 the loop. */
6498 static int
6499 maybe_eliminate_biv_1 (x, insn, bl, eliminate_p, where)
6500 rtx x, insn;
6501 struct iv_class *bl;
6502 int eliminate_p;
6503 rtx where;
6505 enum rtx_code code = GET_CODE (x);
6506 rtx reg = bl->biv->dest_reg;
6507 enum machine_mode mode = GET_MODE (reg);
6508 struct induction *v;
6509 rtx arg, tem;
6510 #ifdef HAVE_cc0
6511 rtx new;
6512 #endif
6513 int arg_operand;
6514 char *fmt;
6515 int i, j;
6517 switch (code)
6519 case REG:
6520 /* If we haven't already been able to do something with this BIV,
6521 we can't eliminate it. */
6522 if (x == reg)
6523 return 0;
6524 return 1;
6526 case SET:
6527 /* If this sets the BIV, it is not a problem. */
6528 if (SET_DEST (x) == reg)
6529 return 1;
6531 /* If this is an insn that defines a giv, it is also ok because
6532 it will go away when the giv is reduced. */
6533 for (v = bl->giv; v; v = v->next_iv)
6534 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
6535 return 1;
6537 #ifdef HAVE_cc0
6538 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
6540 /* Can replace with any giv that was reduced and
6541 that has (MULT_VAL != 0) and (ADD_VAL == 0).
6542 Require a constant for MULT_VAL, so we know it's nonzero.
6543 ??? We disable this optimization to avoid potential
6544 overflows. */
6546 for (v = bl->giv; v; v = v->next_iv)
6547 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6548 && v->add_val == const0_rtx
6549 && ! v->ignore && ! v->maybe_dead && v->always_computable
6550 && v->mode == mode
6551 && 0)
6553 /* If the giv V had the auto-inc address optimization applied
6554 to it, and INSN occurs between the giv insn and the biv
6555 insn, then we must adjust the value used here.
6556 This is rare, so we don't bother to do so. */
6557 if (v->auto_inc_opt
6558 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6559 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6560 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6561 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6562 continue;
6564 if (! eliminate_p)
6565 return 1;
6567 /* If the giv has the opposite direction of change,
6568 then reverse the comparison. */
6569 if (INTVAL (v->mult_val) < 0)
6570 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
6571 const0_rtx, v->new_reg);
6572 else
6573 new = v->new_reg;
6575 /* We can probably test that giv's reduced reg. */
6576 if (validate_change (insn, &SET_SRC (x), new, 0))
6577 return 1;
6580 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
6581 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
6582 Require a constant for MULT_VAL, so we know it's nonzero.
6583 ??? Do this only if ADD_VAL is a pointer to avoid a potential
6584 overflow problem. */
6586 for (v = bl->giv; v; v = v->next_iv)
6587 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6588 && ! v->ignore && ! v->maybe_dead && v->always_computable
6589 && v->mode == mode
6590 && (GET_CODE (v->add_val) == SYMBOL_REF
6591 || GET_CODE (v->add_val) == LABEL_REF
6592 || GET_CODE (v->add_val) == CONST
6593 || (GET_CODE (v->add_val) == REG
6594 && REGNO_POINTER_FLAG (REGNO (v->add_val)))))
6596 /* If the giv V had the auto-inc address optimization applied
6597 to it, and INSN occurs between the giv insn and the biv
6598 insn, then we must adjust the value used here.
6599 This is rare, so we don't bother to do so. */
6600 if (v->auto_inc_opt
6601 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6602 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6603 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6604 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6605 continue;
6607 if (! eliminate_p)
6608 return 1;
6610 /* If the giv has the opposite direction of change,
6611 then reverse the comparison. */
6612 if (INTVAL (v->mult_val) < 0)
6613 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
6614 v->new_reg);
6615 else
6616 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
6617 copy_rtx (v->add_val));
6619 /* Replace biv with the giv's reduced register. */
6620 update_reg_last_use (v->add_val, insn);
6621 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6622 return 1;
6624 /* Insn doesn't support that constant or invariant. Copy it
6625 into a register (it will be a loop invariant.) */
6626 tem = gen_reg_rtx (GET_MODE (v->new_reg));
6628 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
6629 where);
6631 /* Substitute the new register for its invariant value in
6632 the compare expression. */
6633 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
6634 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6635 return 1;
6638 #endif
6639 break;
6641 case COMPARE:
6642 case EQ: case NE:
6643 case GT: case GE: case GTU: case GEU:
6644 case LT: case LE: case LTU: case LEU:
6645 /* See if either argument is the biv. */
6646 if (XEXP (x, 0) == reg)
6647 arg = XEXP (x, 1), arg_operand = 1;
6648 else if (XEXP (x, 1) == reg)
6649 arg = XEXP (x, 0), arg_operand = 0;
6650 else
6651 break;
6653 if (CONSTANT_P (arg))
6655 /* First try to replace with any giv that has constant positive
6656 mult_val and constant add_val. We might be able to support
6657 negative mult_val, but it seems complex to do it in general. */
6659 for (v = bl->giv; v; v = v->next_iv)
6660 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6661 && (GET_CODE (v->add_val) == SYMBOL_REF
6662 || GET_CODE (v->add_val) == LABEL_REF
6663 || GET_CODE (v->add_val) == CONST
6664 || (GET_CODE (v->add_val) == REG
6665 && REGNO_POINTER_FLAG (REGNO (v->add_val))))
6666 && ! v->ignore && ! v->maybe_dead && v->always_computable
6667 && v->mode == mode)
6669 /* If the giv V had the auto-inc address optimization applied
6670 to it, and INSN occurs between the giv insn and the biv
6671 insn, then we must adjust the value used here.
6672 This is rare, so we don't bother to do so. */
6673 if (v->auto_inc_opt
6674 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6675 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6676 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6677 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6678 continue;
6680 if (! eliminate_p)
6681 return 1;
6683 /* Replace biv with the giv's reduced reg. */
6684 XEXP (x, 1-arg_operand) = v->new_reg;
6686 /* If all constants are actually constant integers and
6687 the derived constant can be directly placed in the COMPARE,
6688 do so. */
6689 if (GET_CODE (arg) == CONST_INT
6690 && GET_CODE (v->mult_val) == CONST_INT
6691 && GET_CODE (v->add_val) == CONST_INT
6692 && validate_change (insn, &XEXP (x, arg_operand),
6693 GEN_INT (INTVAL (arg)
6694 * INTVAL (v->mult_val)
6695 + INTVAL (v->add_val)), 0))
6696 return 1;
6698 /* Otherwise, load it into a register. */
6699 tem = gen_reg_rtx (mode);
6700 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6701 if (validate_change (insn, &XEXP (x, arg_operand), tem, 0))
6702 return 1;
6704 /* If that failed, put back the change we made above. */
6705 XEXP (x, 1-arg_operand) = reg;
6708 /* Look for giv with positive constant mult_val and nonconst add_val.
6709 Insert insns to calculate new compare value.
6710 ??? Turn this off due to possible overflow. */
6712 for (v = bl->giv; v; v = v->next_iv)
6713 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6714 && ! v->ignore && ! v->maybe_dead && v->always_computable
6715 && v->mode == mode
6716 && 0)
6718 rtx tem;
6720 /* If the giv V had the auto-inc address optimization applied
6721 to it, and INSN occurs between the giv insn and the biv
6722 insn, then we must adjust the value used here.
6723 This is rare, so we don't bother to do so. */
6724 if (v->auto_inc_opt
6725 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6726 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6727 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6728 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6729 continue;
6731 if (! eliminate_p)
6732 return 1;
6734 tem = gen_reg_rtx (mode);
6736 /* Replace biv with giv's reduced register. */
6737 validate_change (insn, &XEXP (x, 1 - arg_operand),
6738 v->new_reg, 1);
6740 /* Compute value to compare against. */
6741 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6742 /* Use it in this insn. */
6743 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6744 if (apply_change_group ())
6745 return 1;
6748 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
6750 if (invariant_p (arg) == 1)
6752 /* Look for giv with constant positive mult_val and nonconst
6753 add_val. Insert insns to compute new compare value.
6754 ??? Turn this off due to possible overflow. */
6756 for (v = bl->giv; v; v = v->next_iv)
6757 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6758 && ! v->ignore && ! v->maybe_dead && v->always_computable
6759 && v->mode == mode
6760 && 0)
6762 rtx tem;
6764 /* If the giv V had the auto-inc address optimization applied
6765 to it, and INSN occurs between the giv insn and the biv
6766 insn, then we must adjust the value used here.
6767 This is rare, so we don't bother to do so. */
6768 if (v->auto_inc_opt
6769 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6770 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6771 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6772 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6773 continue;
6775 if (! eliminate_p)
6776 return 1;
6778 tem = gen_reg_rtx (mode);
6780 /* Replace biv with giv's reduced register. */
6781 validate_change (insn, &XEXP (x, 1 - arg_operand),
6782 v->new_reg, 1);
6784 /* Compute value to compare against. */
6785 emit_iv_add_mult (arg, v->mult_val, v->add_val,
6786 tem, where);
6787 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6788 if (apply_change_group ())
6789 return 1;
6793 /* This code has problems. Basically, you can't know when
6794 seeing if we will eliminate BL, whether a particular giv
6795 of ARG will be reduced. If it isn't going to be reduced,
6796 we can't eliminate BL. We can try forcing it to be reduced,
6797 but that can generate poor code.
6799 The problem is that the benefit of reducing TV, below should
6800 be increased if BL can actually be eliminated, but this means
6801 we might have to do a topological sort of the order in which
6802 we try to process biv. It doesn't seem worthwhile to do
6803 this sort of thing now. */
6805 #if 0
6806 /* Otherwise the reg compared with had better be a biv. */
6807 if (GET_CODE (arg) != REG
6808 || reg_iv_type[REGNO (arg)] != BASIC_INDUCT)
6809 return 0;
6811 /* Look for a pair of givs, one for each biv,
6812 with identical coefficients. */
6813 for (v = bl->giv; v; v = v->next_iv)
6815 struct induction *tv;
6817 if (v->ignore || v->maybe_dead || v->mode != mode)
6818 continue;
6820 for (tv = reg_biv_class[REGNO (arg)]->giv; tv; tv = tv->next_iv)
6821 if (! tv->ignore && ! tv->maybe_dead
6822 && rtx_equal_p (tv->mult_val, v->mult_val)
6823 && rtx_equal_p (tv->add_val, v->add_val)
6824 && tv->mode == mode)
6826 /* If the giv V had the auto-inc address optimization applied
6827 to it, and INSN occurs between the giv insn and the biv
6828 insn, then we must adjust the value used here.
6829 This is rare, so we don't bother to do so. */
6830 if (v->auto_inc_opt
6831 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6832 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6833 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6834 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6835 continue;
6837 if (! eliminate_p)
6838 return 1;
6840 /* Replace biv with its giv's reduced reg. */
6841 XEXP (x, 1-arg_operand) = v->new_reg;
6842 /* Replace other operand with the other giv's
6843 reduced reg. */
6844 XEXP (x, arg_operand) = tv->new_reg;
6845 return 1;
6848 #endif
6851 /* If we get here, the biv can't be eliminated. */
6852 return 0;
6854 case MEM:
6855 /* If this address is a DEST_ADDR giv, it doesn't matter if the
6856 biv is used in it, since it will be replaced. */
6857 for (v = bl->giv; v; v = v->next_iv)
6858 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
6859 return 1;
6860 break;
6862 default:
6863 break;
6866 /* See if any subexpression fails elimination. */
6867 fmt = GET_RTX_FORMAT (code);
6868 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6870 switch (fmt[i])
6872 case 'e':
6873 if (! maybe_eliminate_biv_1 (XEXP (x, i), insn, bl,
6874 eliminate_p, where))
6875 return 0;
6876 break;
6878 case 'E':
6879 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6880 if (! maybe_eliminate_biv_1 (XVECEXP (x, i, j), insn, bl,
6881 eliminate_p, where))
6882 return 0;
6883 break;
6887 return 1;
6890 /* Return nonzero if the last use of REG
6891 is in an insn following INSN in the same basic block. */
6893 static int
6894 last_use_this_basic_block (reg, insn)
6895 rtx reg;
6896 rtx insn;
6898 rtx n;
6899 for (n = insn;
6900 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
6901 n = NEXT_INSN (n))
6903 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
6904 return 1;
6906 return 0;
6909 /* Called via `note_stores' to record the initial value of a biv. Here we
6910 just record the location of the set and process it later. */
6912 static void
6913 record_initial (dest, set)
6914 rtx dest;
6915 rtx set;
6917 struct iv_class *bl;
6919 if (GET_CODE (dest) != REG
6920 || REGNO (dest) >= max_reg_before_loop
6921 || reg_iv_type[REGNO (dest)] != BASIC_INDUCT)
6922 return;
6924 bl = reg_biv_class[REGNO (dest)];
6926 /* If this is the first set found, record it. */
6927 if (bl->init_insn == 0)
6929 bl->init_insn = note_insn;
6930 bl->init_set = set;
6934 /* If any of the registers in X are "old" and currently have a last use earlier
6935 than INSN, update them to have a last use of INSN. Their actual last use
6936 will be the previous insn but it will not have a valid uid_luid so we can't
6937 use it. */
6939 static void
6940 update_reg_last_use (x, insn)
6941 rtx x;
6942 rtx insn;
6944 /* Check for the case where INSN does not have a valid luid. In this case,
6945 there is no need to modify the regno_last_uid, as this can only happen
6946 when code is inserted after the loop_end to set a pseudo's final value,
6947 and hence this insn will never be the last use of x. */
6948 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
6949 && INSN_UID (insn) < max_uid_for_loop
6950 && uid_luid[REGNO_LAST_UID (REGNO (x))] < uid_luid[INSN_UID (insn)])
6951 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
6952 else
6954 register int i, j;
6955 register char *fmt = GET_RTX_FORMAT (GET_CODE (x));
6956 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6958 if (fmt[i] == 'e')
6959 update_reg_last_use (XEXP (x, i), insn);
6960 else if (fmt[i] == 'E')
6961 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6962 update_reg_last_use (XVECEXP (x, i, j), insn);
6967 /* Given a jump insn JUMP, return the condition that will cause it to branch
6968 to its JUMP_LABEL. If the condition cannot be understood, or is an
6969 inequality floating-point comparison which needs to be reversed, 0 will
6970 be returned.
6972 If EARLIEST is non-zero, it is a pointer to a place where the earliest
6973 insn used in locating the condition was found. If a replacement test
6974 of the condition is desired, it should be placed in front of that
6975 insn and we will be sure that the inputs are still valid.
6977 The condition will be returned in a canonical form to simplify testing by
6978 callers. Specifically:
6980 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
6981 (2) Both operands will be machine operands; (cc0) will have been replaced.
6982 (3) If an operand is a constant, it will be the second operand.
6983 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
6984 for GE, GEU, and LEU. */
6987 get_condition (jump, earliest)
6988 rtx jump;
6989 rtx *earliest;
6991 enum rtx_code code;
6992 rtx prev = jump;
6993 rtx set;
6994 rtx tem;
6995 rtx op0, op1;
6996 int reverse_code = 0;
6997 int did_reverse_condition = 0;
6998 enum machine_mode mode;
7000 /* If this is not a standard conditional jump, we can't parse it. */
7001 if (GET_CODE (jump) != JUMP_INSN
7002 || ! condjump_p (jump) || simplejump_p (jump))
7003 return 0;
7005 code = GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 0));
7006 mode = GET_MODE (XEXP (SET_SRC (PATTERN (jump)), 0));
7007 op0 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 0);
7008 op1 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 1);
7010 if (earliest)
7011 *earliest = jump;
7013 /* If this branches to JUMP_LABEL when the condition is false, reverse
7014 the condition. */
7015 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
7016 && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
7017 code = reverse_condition (code), did_reverse_condition ^= 1;
7019 /* If we are comparing a register with zero, see if the register is set
7020 in the previous insn to a COMPARE or a comparison operation. Perform
7021 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
7022 in cse.c */
7024 while (GET_RTX_CLASS (code) == '<' && op1 == CONST0_RTX (GET_MODE (op0)))
7026 /* Set non-zero when we find something of interest. */
7027 rtx x = 0;
7029 #ifdef HAVE_cc0
7030 /* If comparison with cc0, import actual comparison from compare
7031 insn. */
7032 if (op0 == cc0_rtx)
7034 if ((prev = prev_nonnote_insn (prev)) == 0
7035 || GET_CODE (prev) != INSN
7036 || (set = single_set (prev)) == 0
7037 || SET_DEST (set) != cc0_rtx)
7038 return 0;
7040 op0 = SET_SRC (set);
7041 op1 = CONST0_RTX (GET_MODE (op0));
7042 if (earliest)
7043 *earliest = prev;
7045 #endif
7047 /* If this is a COMPARE, pick up the two things being compared. */
7048 if (GET_CODE (op0) == COMPARE)
7050 op1 = XEXP (op0, 1);
7051 op0 = XEXP (op0, 0);
7052 continue;
7054 else if (GET_CODE (op0) != REG)
7055 break;
7057 /* Go back to the previous insn. Stop if it is not an INSN. We also
7058 stop if it isn't a single set or if it has a REG_INC note because
7059 we don't want to bother dealing with it. */
7061 if ((prev = prev_nonnote_insn (prev)) == 0
7062 || GET_CODE (prev) != INSN
7063 || FIND_REG_INC_NOTE (prev, 0)
7064 || (set = single_set (prev)) == 0)
7065 break;
7067 /* If this is setting OP0, get what it sets it to if it looks
7068 relevant. */
7069 if (rtx_equal_p (SET_DEST (set), op0))
7071 enum machine_mode inner_mode = GET_MODE (SET_SRC (set));
7073 /* ??? We may not combine comparisons done in a CCmode with
7074 comparisons not done in a CCmode. This is to aid targets
7075 like Alpha that have an IEEE compliant EQ instruction, and
7076 a non-IEEE compliant BEQ instruction. The use of CCmode is
7077 actually artificial, simply to prevent the combination, but
7078 should not affect other platforms. */
7080 if ((GET_CODE (SET_SRC (set)) == COMPARE
7081 || (((code == NE
7082 || (code == LT
7083 && GET_MODE_CLASS (inner_mode) == MODE_INT
7084 && (GET_MODE_BITSIZE (inner_mode)
7085 <= HOST_BITS_PER_WIDE_INT)
7086 && (STORE_FLAG_VALUE
7087 & ((HOST_WIDE_INT) 1
7088 << (GET_MODE_BITSIZE (inner_mode) - 1))))
7089 #ifdef FLOAT_STORE_FLAG_VALUE
7090 || (code == LT
7091 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
7092 && FLOAT_STORE_FLAG_VALUE < 0)
7093 #endif
7095 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
7096 && ((GET_MODE_CLASS (mode) == MODE_CC)
7097 != (GET_MODE_CLASS (inner_mode) == MODE_CC)))
7098 x = SET_SRC (set);
7099 else if (((code == EQ
7100 || (code == GE
7101 && (GET_MODE_BITSIZE (inner_mode)
7102 <= HOST_BITS_PER_WIDE_INT)
7103 && GET_MODE_CLASS (inner_mode) == MODE_INT
7104 && (STORE_FLAG_VALUE
7105 & ((HOST_WIDE_INT) 1
7106 << (GET_MODE_BITSIZE (inner_mode) - 1))))
7107 #ifdef FLOAT_STORE_FLAG_VALUE
7108 || (code == GE
7109 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
7110 && FLOAT_STORE_FLAG_VALUE < 0)
7111 #endif
7113 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
7114 && ((GET_MODE_CLASS (mode) == MODE_CC)
7115 != (GET_MODE_CLASS (inner_mode) == MODE_CC)))
7117 /* We might have reversed a LT to get a GE here. But this wasn't
7118 actually the comparison of data, so we don't flag that we
7119 have had to reverse the condition. */
7120 did_reverse_condition ^= 1;
7121 reverse_code = 1;
7122 x = SET_SRC (set);
7124 else
7125 break;
7128 else if (reg_set_p (op0, prev))
7129 /* If this sets OP0, but not directly, we have to give up. */
7130 break;
7132 if (x)
7134 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
7135 code = GET_CODE (x);
7136 if (reverse_code)
7138 code = reverse_condition (code);
7139 did_reverse_condition ^= 1;
7140 reverse_code = 0;
7143 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
7144 if (earliest)
7145 *earliest = prev;
7149 /* If constant is first, put it last. */
7150 if (CONSTANT_P (op0))
7151 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
7153 /* If OP0 is the result of a comparison, we weren't able to find what
7154 was really being compared, so fail. */
7155 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
7156 return 0;
7158 /* Canonicalize any ordered comparison with integers involving equality
7159 if we can do computations in the relevant mode and we do not
7160 overflow. */
7162 if (GET_CODE (op1) == CONST_INT
7163 && GET_MODE (op0) != VOIDmode
7164 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
7166 HOST_WIDE_INT const_val = INTVAL (op1);
7167 unsigned HOST_WIDE_INT uconst_val = const_val;
7168 unsigned HOST_WIDE_INT max_val
7169 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
7171 switch (code)
7173 case LE:
7174 if (const_val != max_val >> 1)
7175 code = LT, op1 = GEN_INT (const_val + 1);
7176 break;
7178 /* When cross-compiling, const_val might be sign-extended from
7179 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
7180 case GE:
7181 if ((const_val & max_val)
7182 != (((HOST_WIDE_INT) 1
7183 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
7184 code = GT, op1 = GEN_INT (const_val - 1);
7185 break;
7187 case LEU:
7188 if (uconst_val < max_val)
7189 code = LTU, op1 = GEN_INT (uconst_val + 1);
7190 break;
7192 case GEU:
7193 if (uconst_val != 0)
7194 code = GTU, op1 = GEN_INT (uconst_val - 1);
7195 break;
7197 default:
7198 break;
7202 /* If this was floating-point and we reversed anything other than an
7203 EQ or NE, return zero. */
7204 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
7205 && did_reverse_condition && code != NE && code != EQ
7206 && ! flag_fast_math
7207 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
7208 return 0;
7210 #ifdef HAVE_cc0
7211 /* Never return CC0; return zero instead. */
7212 if (op0 == cc0_rtx)
7213 return 0;
7214 #endif
7216 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
7219 /* Similar to above routine, except that we also put an invariant last
7220 unless both operands are invariants. */
7223 get_condition_for_loop (x)
7224 rtx x;
7226 rtx comparison = get_condition (x, NULL_PTR);
7228 if (comparison == 0
7229 || ! invariant_p (XEXP (comparison, 0))
7230 || invariant_p (XEXP (comparison, 1)))
7231 return comparison;
7233 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
7234 XEXP (comparison, 1), XEXP (comparison, 0));
7237 #ifdef HAIFA
7238 /* Analyze a loop in order to instrument it with the use of count register.
7239 loop_start and loop_end are the first and last insns of the loop.
7240 This function works in cooperation with insert_bct ().
7241 loop_can_insert_bct[loop_num] is set according to whether the optimization
7242 is applicable to the loop. When it is applicable, the following variables
7243 are also set:
7244 loop_start_value[loop_num]
7245 loop_comparison_value[loop_num]
7246 loop_increment[loop_num]
7247 loop_comparison_code[loop_num] */
7249 #ifdef HAVE_decrement_and_branch_on_count
7250 static
7251 void analyze_loop_iterations (loop_start, loop_end)
7252 rtx loop_start, loop_end;
7254 rtx comparison, comparison_value;
7255 rtx iteration_var, initial_value, increment;
7256 enum rtx_code comparison_code;
7258 rtx last_loop_insn;
7259 rtx insn;
7260 int i;
7262 /* loop_variable mode */
7263 enum machine_mode original_mode;
7265 /* find the number of the loop */
7266 int loop_num = uid_loop_num [INSN_UID (loop_start)];
7268 /* we change our mind only when we are sure that loop will be instrumented */
7269 loop_can_insert_bct[loop_num] = 0;
7271 /* is the optimization suppressed. */
7272 if ( !flag_branch_on_count_reg )
7273 return;
7275 /* make sure that count-reg is not in use */
7276 if (loop_used_count_register[loop_num]){
7277 if (loop_dump_stream)
7278 fprintf (loop_dump_stream,
7279 "analyze_loop_iterations %d: BCT instrumentation failed: count register already in use\n",
7280 loop_num);
7281 return;
7284 /* make sure that the function has no indirect jumps. */
7285 if (indirect_jump_in_function){
7286 if (loop_dump_stream)
7287 fprintf (loop_dump_stream,
7288 "analyze_loop_iterations %d: BCT instrumentation failed: indirect jump in function\n",
7289 loop_num);
7290 return;
7293 /* make sure that the last loop insn is a conditional jump */
7294 last_loop_insn = PREV_INSN (loop_end);
7295 if (GET_CODE (last_loop_insn) != JUMP_INSN || !condjump_p (last_loop_insn)) {
7296 if (loop_dump_stream)
7297 fprintf (loop_dump_stream,
7298 "analyze_loop_iterations %d: BCT instrumentation failed: invalid jump at loop end\n",
7299 loop_num);
7300 return;
7303 /* First find the iteration variable. If the last insn is a conditional
7304 branch, and the insn preceding it tests a register value, make that
7305 register the iteration variable. */
7307 /* We used to use prev_nonnote_insn here, but that fails because it might
7308 accidentally get the branch for a contained loop if the branch for this
7309 loop was deleted. We can only trust branches immediately before the
7310 loop_end. */
7312 comparison = get_condition_for_loop (last_loop_insn);
7313 /* ??? Get_condition may switch position of induction variable and
7314 invariant register when it canonicalizes the comparison. */
7316 if (comparison == 0) {
7317 if (loop_dump_stream)
7318 fprintf (loop_dump_stream,
7319 "analyze_loop_iterations %d: BCT instrumentation failed: comparison not found\n",
7320 loop_num);
7321 return;
7324 comparison_code = GET_CODE (comparison);
7325 iteration_var = XEXP (comparison, 0);
7326 comparison_value = XEXP (comparison, 1);
7328 original_mode = GET_MODE (iteration_var);
7329 if (GET_MODE_CLASS (original_mode) != MODE_INT
7330 || GET_MODE_SIZE (original_mode) != UNITS_PER_WORD) {
7331 if (loop_dump_stream)
7332 fprintf (loop_dump_stream,
7333 "analyze_loop_iterations %d: BCT Instrumentation failed: loop variable not integer\n",
7334 loop_num);
7335 return;
7338 /* get info about loop bounds and increment */
7339 iteration_info (iteration_var, &initial_value, &increment,
7340 loop_start, loop_end);
7342 /* make sure that all required loop data were found */
7343 if (!(initial_value && increment && comparison_value
7344 && invariant_p (comparison_value) && invariant_p (increment)
7345 && ! indirect_jump_in_function))
7347 if (loop_dump_stream) {
7348 fprintf (loop_dump_stream,
7349 "analyze_loop_iterations %d: BCT instrumentation failed because of wrong loop: ", loop_num);
7350 if (!(initial_value && increment && comparison_value)) {
7351 fprintf (loop_dump_stream, "\tbounds not available: ");
7352 if ( ! initial_value )
7353 fprintf (loop_dump_stream, "initial ");
7354 if ( ! increment )
7355 fprintf (loop_dump_stream, "increment ");
7356 if ( ! comparison_value )
7357 fprintf (loop_dump_stream, "comparison ");
7358 fprintf (loop_dump_stream, "\n");
7360 if (!invariant_p (comparison_value) || !invariant_p (increment))
7361 fprintf (loop_dump_stream, "\tloop bounds not invariant\n");
7363 return;
7366 /* make sure that the increment is constant */
7367 if (GET_CODE (increment) != CONST_INT) {
7368 if (loop_dump_stream)
7369 fprintf (loop_dump_stream,
7370 "analyze_loop_iterations %d: instrumentation failed: not arithmetic loop\n",
7371 loop_num);
7372 return;
7375 /* make sure that the loop contains neither function call, nor jump on table.
7376 (the count register might be altered by the called function, and might
7377 be used for a branch on table). */
7378 for (insn = loop_start; insn && insn != loop_end; insn = NEXT_INSN (insn)) {
7379 if (GET_CODE (insn) == CALL_INSN){
7380 if (loop_dump_stream)
7381 fprintf (loop_dump_stream,
7382 "analyze_loop_iterations %d: BCT instrumentation failed: function call in the loop\n",
7383 loop_num);
7384 return;
7387 if (GET_CODE (insn) == JUMP_INSN
7388 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
7389 || GET_CODE (PATTERN (insn)) == ADDR_VEC)){
7390 if (loop_dump_stream)
7391 fprintf (loop_dump_stream,
7392 "analyze_loop_iterations %d: BCT instrumentation failed: computed branch in the loop\n",
7393 loop_num);
7394 return;
7398 /* At this point, we are sure that the loop can be instrumented with BCT.
7399 Some of the loops, however, will not be instrumented - the final decision
7400 is taken by insert_bct () */
7401 if (loop_dump_stream)
7402 fprintf (loop_dump_stream,
7403 "analyze_loop_iterations: loop (luid =%d) can be BCT instrumented.\n",
7404 loop_num);
7406 /* mark all enclosing loops that they cannot use count register */
7407 /* ???: In fact, since insert_bct may decide not to instrument this loop,
7408 marking here may prevent instrumenting an enclosing loop that could
7409 actually be instrumented. But since this is rare, it is safer to mark
7410 here in case the order of calling (analyze/insert)_bct would be changed. */
7411 for (i=loop_num; i != -1; i = loop_outer_loop[i])
7412 loop_used_count_register[i] = 1;
7414 /* Set data structures which will be used by the instrumentation phase */
7415 loop_start_value[loop_num] = initial_value;
7416 loop_comparison_value[loop_num] = comparison_value;
7417 loop_increment[loop_num] = increment;
7418 loop_comparison_code[loop_num] = comparison_code;
7419 loop_can_insert_bct[loop_num] = 1;
7423 /* instrument loop for insertion of bct instruction. We distinguish between
7424 loops with compile-time bounds, to those with run-time bounds. The loop
7425 behaviour is analized according to the following characteristics/variables:
7426 ; Input variables:
7427 ; comparison-value: the value to which the iteration counter is compared.
7428 ; initial-value: iteration-counter initial value.
7429 ; increment: iteration-counter increment.
7430 ; Computed variables:
7431 ; increment-direction: the sign of the increment.
7432 ; compare-direction: '1' for GT, GTE, '-1' for LT, LTE, '0' for NE.
7433 ; range-direction: sign (comparison-value - initial-value)
7434 We give up on the following cases:
7435 ; loop variable overflow.
7436 ; run-time loop bounds with comparison code NE.
7439 static void
7440 insert_bct (loop_start, loop_end)
7441 rtx loop_start, loop_end;
7443 rtx initial_value, comparison_value, increment;
7444 enum rtx_code comparison_code;
7446 int increment_direction, compare_direction;
7447 int unsigned_p = 0;
7449 /* if the loop condition is <= or >=, the number of iteration
7450 is 1 more than the range of the bounds of the loop */
7451 int add_iteration = 0;
7453 /* the only machine mode we work with - is the integer of the size that the
7454 machine has */
7455 enum machine_mode loop_var_mode = SImode;
7457 int loop_num = uid_loop_num [INSN_UID (loop_start)];
7459 /* get loop-variables. No need to check that these are valid - already
7460 checked in analyze_loop_iterations (). */
7461 comparison_code = loop_comparison_code[loop_num];
7462 initial_value = loop_start_value[loop_num];
7463 comparison_value = loop_comparison_value[loop_num];
7464 increment = loop_increment[loop_num];
7466 /* check analyze_loop_iterations decision for this loop. */
7467 if (! loop_can_insert_bct[loop_num]){
7468 if (loop_dump_stream)
7469 fprintf (loop_dump_stream,
7470 "insert_bct: [%d] - was decided not to instrument by analyze_loop_iterations ()\n",
7471 loop_num);
7472 return;
7475 /* It's impossible to instrument a competely unrolled loop. */
7476 if (loop_unroll_factor [loop_num] == -1)
7477 return;
7479 /* make sure that the last loop insn is a conditional jump .
7480 This check is repeated from analyze_loop_iterations (),
7481 because unrolling might have changed that. */
7482 if (GET_CODE (PREV_INSN (loop_end)) != JUMP_INSN
7483 || !condjump_p (PREV_INSN (loop_end))) {
7484 if (loop_dump_stream)
7485 fprintf (loop_dump_stream,
7486 "insert_bct: not instrumenting BCT because of invalid branch\n");
7487 return;
7490 /* fix increment in case loop was unrolled. */
7491 if (loop_unroll_factor [loop_num] > 1)
7492 increment = GEN_INT ( INTVAL (increment) * loop_unroll_factor [loop_num] );
7494 /* determine properties and directions of the loop */
7495 increment_direction = (INTVAL (increment) > 0) ? 1:-1;
7496 switch ( comparison_code ) {
7497 case LEU:
7498 unsigned_p = 1;
7499 /* fallthrough */
7500 case LE:
7501 compare_direction = 1;
7502 add_iteration = 1;
7503 break;
7504 case GEU:
7505 unsigned_p = 1;
7506 /* fallthrough */
7507 case GE:
7508 compare_direction = -1;
7509 add_iteration = 1;
7510 break;
7511 case EQ:
7512 /* in this case we cannot know the number of iterations */
7513 if (loop_dump_stream)
7514 fprintf (loop_dump_stream,
7515 "insert_bct: %d: loop cannot be instrumented: == in condition\n",
7516 loop_num);
7517 return;
7518 case LTU:
7519 unsigned_p = 1;
7520 /* fallthrough */
7521 case LT:
7522 compare_direction = 1;
7523 break;
7524 case GTU:
7525 unsigned_p = 1;
7526 /* fallthrough */
7527 case GT:
7528 compare_direction = -1;
7529 break;
7530 case NE:
7531 compare_direction = 0;
7532 break;
7533 default:
7534 abort ();
7538 /* make sure that the loop does not end by an overflow */
7539 if (compare_direction != increment_direction) {
7540 if (loop_dump_stream)
7541 fprintf (loop_dump_stream,
7542 "insert_bct: %d: loop cannot be instrumented: terminated by overflow\n",
7543 loop_num);
7544 return;
7547 /* try to instrument the loop. */
7549 /* Handle the simpler case, where the bounds are known at compile time. */
7550 if (GET_CODE (initial_value) == CONST_INT && GET_CODE (comparison_value) == CONST_INT)
7552 int n_iterations;
7553 int increment_value_abs = INTVAL (increment) * increment_direction;
7555 /* check the relation between compare-val and initial-val */
7556 int difference = INTVAL (comparison_value) - INTVAL (initial_value);
7557 int range_direction = (difference > 0) ? 1 : -1;
7559 /* make sure the loop executes enough iterations to gain from BCT */
7560 if (difference > -3 && difference < 3) {
7561 if (loop_dump_stream)
7562 fprintf (loop_dump_stream,
7563 "insert_bct: loop %d not BCT instrumented: too small iteration count.\n",
7564 loop_num);
7565 return;
7568 /* make sure that the loop executes at least once */
7569 if ((range_direction == 1 && compare_direction == -1)
7570 || (range_direction == -1 && compare_direction == 1))
7572 if (loop_dump_stream)
7573 fprintf (loop_dump_stream,
7574 "insert_bct: loop %d: does not iterate even once. Not instrumenting.\n",
7575 loop_num);
7576 return;
7579 /* make sure that the loop does not end by an overflow (in compile time
7580 bounds we must have an additional check for overflow, because here
7581 we also support the compare code of 'NE'. */
7582 if (comparison_code == NE
7583 && increment_direction != range_direction) {
7584 if (loop_dump_stream)
7585 fprintf (loop_dump_stream,
7586 "insert_bct (compile time bounds): %d: loop not instrumented: terminated by overflow\n",
7587 loop_num);
7588 return;
7591 /* Determine the number of iterations by:
7593 ; compare-val - initial-val + (increment -1) + additional-iteration
7594 ; num_iterations = -----------------------------------------------------------------
7595 ; increment
7597 difference = (range_direction > 0) ? difference : -difference;
7598 #if 0
7599 fprintf (stderr, "difference is: %d\n", difference); /* @*/
7600 fprintf (stderr, "increment_value_abs is: %d\n", increment_value_abs); /* @*/
7601 fprintf (stderr, "add_iteration is: %d\n", add_iteration); /* @*/
7602 fprintf (stderr, "INTVAL (comparison_value) is: %d\n", INTVAL (comparison_value)); /* @*/
7603 fprintf (stderr, "INTVAL (initial_value) is: %d\n", INTVAL (initial_value)); /* @*/
7604 #endif
7606 if (increment_value_abs == 0) {
7607 fprintf (stderr, "insert_bct: error: increment == 0 !!!\n");
7608 abort ();
7610 n_iterations = (difference + increment_value_abs - 1 + add_iteration)
7611 / increment_value_abs;
7613 #if 0
7614 fprintf (stderr, "number of iterations is: %d\n", n_iterations); /* @*/
7615 #endif
7616 instrument_loop_bct (loop_start, loop_end, GEN_INT (n_iterations));
7618 /* Done with this loop. */
7619 return;
7622 /* Handle the more complex case, that the bounds are NOT known at compile time. */
7623 /* In this case we generate run_time calculation of the number of iterations */
7625 /* With runtime bounds, if the compare is of the form '!=' we give up */
7626 if (comparison_code == NE) {
7627 if (loop_dump_stream)
7628 fprintf (loop_dump_stream,
7629 "insert_bct: fail for loop %d: runtime bounds with != comparison\n",
7630 loop_num);
7631 return;
7634 else {
7635 /* We rely on the existence of run-time guard to ensure that the
7636 loop executes at least once. */
7637 rtx sequence;
7638 rtx iterations_num_reg;
7640 int increment_value_abs = INTVAL (increment) * increment_direction;
7642 /* make sure that the increment is a power of two, otherwise (an
7643 expensive) divide is needed. */
7644 if (exact_log2 (increment_value_abs) == -1)
7646 if (loop_dump_stream)
7647 fprintf (loop_dump_stream,
7648 "insert_bct: not instrumenting BCT because the increment is not power of 2\n");
7649 return;
7652 /* compute the number of iterations */
7653 start_sequence ();
7655 rtx temp_reg;
7657 /* Again, the number of iterations is calculated by:
7659 ; compare-val - initial-val + (increment -1) + additional-iteration
7660 ; num_iterations = -----------------------------------------------------------------
7661 ; increment
7663 /* ??? Do we have to call copy_rtx here before passing rtx to
7664 expand_binop? */
7665 if (compare_direction > 0) {
7666 /* <, <= :the loop variable is increasing */
7667 temp_reg = expand_binop (loop_var_mode, sub_optab, comparison_value,
7668 initial_value, NULL_RTX, 0, OPTAB_LIB_WIDEN);
7670 else {
7671 temp_reg = expand_binop (loop_var_mode, sub_optab, initial_value,
7672 comparison_value, NULL_RTX, 0, OPTAB_LIB_WIDEN);
7675 if (increment_value_abs - 1 + add_iteration != 0)
7676 temp_reg = expand_binop (loop_var_mode, add_optab, temp_reg,
7677 GEN_INT (increment_value_abs - 1 + add_iteration),
7678 NULL_RTX, 0, OPTAB_LIB_WIDEN);
7680 if (increment_value_abs != 1)
7682 /* ??? This will generate an expensive divide instruction for
7683 most targets. The original authors apparently expected this
7684 to be a shift, since they test for power-of-2 divisors above,
7685 but just naively generating a divide instruction will not give
7686 a shift. It happens to work for the PowerPC target because
7687 the rs6000.md file has a divide pattern that emits shifts.
7688 It will probably not work for any other target. */
7689 iterations_num_reg = expand_binop (loop_var_mode, sdiv_optab,
7690 temp_reg,
7691 GEN_INT (increment_value_abs),
7692 NULL_RTX, 0, OPTAB_LIB_WIDEN);
7694 else
7695 iterations_num_reg = temp_reg;
7697 sequence = gen_sequence ();
7698 end_sequence ();
7699 emit_insn_before (sequence, loop_start);
7700 instrument_loop_bct (loop_start, loop_end, iterations_num_reg);
7704 /* instrument loop by inserting a bct in it. This is done in the following way:
7705 1. A new register is created and assigned the hard register number of the count
7706 register.
7707 2. In the head of the loop the new variable is initialized by the value passed in the
7708 loop_num_iterations parameter.
7709 3. At the end of the loop, comparison of the register with 0 is generated.
7710 The created comparison follows the pattern defined for the
7711 decrement_and_branch_on_count insn, so this insn will be generated in assembly
7712 generation phase.
7713 4. The compare&branch on the old variable is deleted. So, if the loop-variable was
7714 not used elsewhere, it will be eliminated by data-flow analisys. */
7716 static void
7717 instrument_loop_bct (loop_start, loop_end, loop_num_iterations)
7718 rtx loop_start, loop_end;
7719 rtx loop_num_iterations;
7721 rtx temp_reg1, temp_reg2;
7722 rtx start_label;
7724 rtx sequence;
7725 enum machine_mode loop_var_mode = SImode;
7727 if (HAVE_decrement_and_branch_on_count)
7729 if (loop_dump_stream)
7730 fprintf (loop_dump_stream, "Loop: Inserting BCT\n");
7732 /* eliminate the check on the old variable */
7733 delete_insn (PREV_INSN (loop_end));
7734 delete_insn (PREV_INSN (loop_end));
7736 /* insert the label which will delimit the start of the loop */
7737 start_label = gen_label_rtx ();
7738 emit_label_after (start_label, loop_start);
7740 /* insert initialization of the count register into the loop header */
7741 start_sequence ();
7742 temp_reg1 = gen_reg_rtx (loop_var_mode);
7743 emit_insn (gen_move_insn (temp_reg1, loop_num_iterations));
7745 /* this will be count register */
7746 temp_reg2 = gen_rtx_REG (loop_var_mode, COUNT_REGISTER_REGNUM);
7747 /* we have to move the value to the count register from an GPR
7748 because rtx pointed to by loop_num_iterations could contain
7749 expression which cannot be moved into count register */
7750 emit_insn (gen_move_insn (temp_reg2, temp_reg1));
7752 sequence = gen_sequence ();
7753 end_sequence ();
7754 emit_insn_after (sequence, loop_start);
7756 /* insert new comparison on the count register instead of the
7757 old one, generating the needed BCT pattern (that will be
7758 later recognized by assembly generation phase). */
7759 emit_jump_insn_before (gen_decrement_and_branch_on_count (temp_reg2, start_label),
7760 loop_end);
7761 LABEL_NUSES (start_label)++;
7765 #endif /* HAVE_decrement_and_branch_on_count */
7767 #endif /* HAIFA */
7769 /* Scan the function and determine whether it has indirect (computed) jumps.
7771 This is taken mostly from flow.c; similar code exists elsewhere
7772 in the compiler. It may be useful to put this into rtlanal.c. */
7773 static int
7774 indirect_jump_in_function_p (start)
7775 rtx start;
7777 rtx insn;
7779 for (insn = start; insn; insn = NEXT_INSN (insn))
7780 if (computed_jump_p (insn))
7781 return 1;
7783 return 0;