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