alias.c (alias_invariant): New variable.
[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, 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, y)
2829 rtx x;
2830 rtx y ATTRIBUTE_UNUSED;
2831 {
2832 register int i;
2833
2834 if (x == 0 || GET_CODE (x) != MEM)
2835 return;
2836
2837 /* Count number of memory writes.
2838 This affects heuristics in strength_reduce. */
2839 num_mem_sets++;
2840
2841 /* BLKmode MEM means all memory is clobbered. */
2842 if (GET_MODE (x) == BLKmode)
2843 unknown_address_altered = 1;
2844
2845 if (unknown_address_altered)
2846 return;
2847
2848 for (i = 0; i < loop_store_mems_idx; i++)
2849 if (rtx_equal_p (XEXP (loop_store_mems[i], 0), XEXP (x, 0))
2850 && MEM_IN_STRUCT_P (x) == MEM_IN_STRUCT_P (loop_store_mems[i]))
2851 {
2852 /* We are storing at the same address as previously noted. Save the
2853 wider reference. */
2854 if (GET_MODE_SIZE (GET_MODE (x))
2855 > GET_MODE_SIZE (GET_MODE (loop_store_mems[i])))
2856 loop_store_mems[i] = x;
2857 break;
2858 }
2859
2860 if (i == NUM_STORES)
2861 unknown_address_altered = 1;
2862
2863 else if (i == loop_store_mems_idx)
2864 loop_store_mems[loop_store_mems_idx++] = x;
2865 }
2866 \f
2867 /* Return nonzero if the rtx X is invariant over the current loop.
2868
2869 The value is 2 if we refer to something only conditionally invariant.
2870
2871 If `unknown_address_altered' is nonzero, no memory ref is invariant.
2872 Otherwise, a memory ref is invariant if it does not conflict with
2873 anything stored in `loop_store_mems'. */
2874
2875 int
2876 invariant_p (x)
2877 register rtx x;
2878 {
2879 register int i;
2880 register enum rtx_code code;
2881 register char *fmt;
2882 int conditional = 0;
2883
2884 if (x == 0)
2885 return 1;
2886 code = GET_CODE (x);
2887 switch (code)
2888 {
2889 case CONST_INT:
2890 case CONST_DOUBLE:
2891 case SYMBOL_REF:
2892 case CONST:
2893 return 1;
2894
2895 case LABEL_REF:
2896 /* A LABEL_REF is normally invariant, however, if we are unrolling
2897 loops, and this label is inside the loop, then it isn't invariant.
2898 This is because each unrolled copy of the loop body will have
2899 a copy of this label. If this was invariant, then an insn loading
2900 the address of this label into a register might get moved outside
2901 the loop, and then each loop body would end up using the same label.
2902
2903 We don't know the loop bounds here though, so just fail for all
2904 labels. */
2905 if (flag_unroll_loops)
2906 return 0;
2907 else
2908 return 1;
2909
2910 case PC:
2911 case CC0:
2912 case UNSPEC_VOLATILE:
2913 return 0;
2914
2915 case REG:
2916 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
2917 since the reg might be set by initialization within the loop. */
2918
2919 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
2920 || x == arg_pointer_rtx)
2921 && ! current_function_has_nonlocal_goto)
2922 return 1;
2923
2924 if (loop_has_call
2925 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
2926 return 0;
2927
2928 if (n_times_set[REGNO (x)] < 0)
2929 return 2;
2930
2931 return n_times_set[REGNO (x)] == 0;
2932
2933 case MEM:
2934 /* Volatile memory references must be rejected. Do this before
2935 checking for read-only items, so that volatile read-only items
2936 will be rejected also. */
2937 if (MEM_VOLATILE_P (x))
2938 return 0;
2939
2940 /* Read-only items (such as constants in a constant pool) are
2941 invariant if their address is. */
2942 if (RTX_UNCHANGING_P (x))
2943 break;
2944
2945 /* If we filled the table (or had a subroutine call), any location
2946 in memory could have been clobbered. */
2947 if (unknown_address_altered)
2948 return 0;
2949
2950 /* See if there is any dependence between a store and this load. */
2951 for (i = loop_store_mems_idx - 1; i >= 0; i--)
2952 if (true_dependence (loop_store_mems[i], VOIDmode, x, rtx_varies_p))
2953 return 0;
2954
2955 /* It's not invalidated by a store in memory
2956 but we must still verify the address is invariant. */
2957 break;
2958
2959 case ASM_OPERANDS:
2960 /* Don't mess with insns declared volatile. */
2961 if (MEM_VOLATILE_P (x))
2962 return 0;
2963 break;
2964
2965 default:
2966 break;
2967 }
2968
2969 fmt = GET_RTX_FORMAT (code);
2970 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2971 {
2972 if (fmt[i] == 'e')
2973 {
2974 int tem = invariant_p (XEXP (x, i));
2975 if (tem == 0)
2976 return 0;
2977 if (tem == 2)
2978 conditional = 1;
2979 }
2980 else if (fmt[i] == 'E')
2981 {
2982 register int j;
2983 for (j = 0; j < XVECLEN (x, i); j++)
2984 {
2985 int tem = invariant_p (XVECEXP (x, i, j));
2986 if (tem == 0)
2987 return 0;
2988 if (tem == 2)
2989 conditional = 1;
2990 }
2991
2992 }
2993 }
2994
2995 return 1 + conditional;
2996 }
2997
2998 \f
2999 /* Return nonzero if all the insns in the loop that set REG
3000 are INSN and the immediately following insns,
3001 and if each of those insns sets REG in an invariant way
3002 (not counting uses of REG in them).
3003
3004 The value is 2 if some of these insns are only conditionally invariant.
3005
3006 We assume that INSN itself is the first set of REG
3007 and that its source is invariant. */
3008
3009 static int
3010 consec_sets_invariant_p (reg, n_sets, insn)
3011 int n_sets;
3012 rtx reg, insn;
3013 {
3014 register rtx p = insn;
3015 register int regno = REGNO (reg);
3016 rtx temp;
3017 /* Number of sets we have to insist on finding after INSN. */
3018 int count = n_sets - 1;
3019 int old = n_times_set[regno];
3020 int value = 0;
3021 int this;
3022
3023 /* If N_SETS hit the limit, we can't rely on its value. */
3024 if (n_sets == 127)
3025 return 0;
3026
3027 n_times_set[regno] = 0;
3028
3029 while (count > 0)
3030 {
3031 register enum rtx_code code;
3032 rtx set;
3033
3034 p = NEXT_INSN (p);
3035 code = GET_CODE (p);
3036
3037 /* If library call, skip to end of of it. */
3038 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3039 p = XEXP (temp, 0);
3040
3041 this = 0;
3042 if (code == INSN
3043 && (set = single_set (p))
3044 && GET_CODE (SET_DEST (set)) == REG
3045 && REGNO (SET_DEST (set)) == regno)
3046 {
3047 this = invariant_p (SET_SRC (set));
3048 if (this != 0)
3049 value |= this;
3050 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3051 {
3052 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3053 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3054 notes are OK. */
3055 this = (CONSTANT_P (XEXP (temp, 0))
3056 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3057 && invariant_p (XEXP (temp, 0))));
3058 if (this != 0)
3059 value |= this;
3060 }
3061 }
3062 if (this != 0)
3063 count--;
3064 else if (code != NOTE)
3065 {
3066 n_times_set[regno] = old;
3067 return 0;
3068 }
3069 }
3070
3071 n_times_set[regno] = old;
3072 /* If invariant_p ever returned 2, we return 2. */
3073 return 1 + (value & 2);
3074 }
3075
3076 #if 0
3077 /* I don't think this condition is sufficient to allow INSN
3078 to be moved, so we no longer test it. */
3079
3080 /* Return 1 if all insns in the basic block of INSN and following INSN
3081 that set REG are invariant according to TABLE. */
3082
3083 static int
3084 all_sets_invariant_p (reg, insn, table)
3085 rtx reg, insn;
3086 short *table;
3087 {
3088 register rtx p = insn;
3089 register int regno = REGNO (reg);
3090
3091 while (1)
3092 {
3093 register enum rtx_code code;
3094 p = NEXT_INSN (p);
3095 code = GET_CODE (p);
3096 if (code == CODE_LABEL || code == JUMP_INSN)
3097 return 1;
3098 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3099 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3100 && REGNO (SET_DEST (PATTERN (p))) == regno)
3101 {
3102 if (!invariant_p (SET_SRC (PATTERN (p)), table))
3103 return 0;
3104 }
3105 }
3106 }
3107 #endif /* 0 */
3108 \f
3109 /* Look at all uses (not sets) of registers in X. For each, if it is
3110 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3111 a different insn, set USAGE[REGNO] to const0_rtx. */
3112
3113 static void
3114 find_single_use_in_loop (insn, x, usage)
3115 rtx insn;
3116 rtx x;
3117 rtx *usage;
3118 {
3119 enum rtx_code code = GET_CODE (x);
3120 char *fmt = GET_RTX_FORMAT (code);
3121 int i, j;
3122
3123 if (code == REG)
3124 usage[REGNO (x)]
3125 = (usage[REGNO (x)] != 0 && usage[REGNO (x)] != insn)
3126 ? const0_rtx : insn;
3127
3128 else if (code == SET)
3129 {
3130 /* Don't count SET_DEST if it is a REG; otherwise count things
3131 in SET_DEST because if a register is partially modified, it won't
3132 show up as a potential movable so we don't care how USAGE is set
3133 for it. */
3134 if (GET_CODE (SET_DEST (x)) != REG)
3135 find_single_use_in_loop (insn, SET_DEST (x), usage);
3136 find_single_use_in_loop (insn, SET_SRC (x), usage);
3137 }
3138 else
3139 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3140 {
3141 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3142 find_single_use_in_loop (insn, XEXP (x, i), usage);
3143 else if (fmt[i] == 'E')
3144 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3145 find_single_use_in_loop (insn, XVECEXP (x, i, j), usage);
3146 }
3147 }
3148 \f
3149 /* Increment N_TIMES_SET at the index of each register
3150 that is modified by an insn between FROM and TO.
3151 If the value of an element of N_TIMES_SET becomes 127 or more,
3152 stop incrementing it, to avoid overflow.
3153
3154 Store in SINGLE_USAGE[I] the single insn in which register I is
3155 used, if it is only used once. Otherwise, it is set to 0 (for no
3156 uses) or const0_rtx for more than one use. This parameter may be zero,
3157 in which case this processing is not done.
3158
3159 Store in *COUNT_PTR the number of actual instruction
3160 in the loop. We use this to decide what is worth moving out. */
3161
3162 /* last_set[n] is nonzero iff reg n has been set in the current basic block.
3163 In that case, it is the insn that last set reg n. */
3164
3165 static void
3166 count_loop_regs_set (from, to, may_not_move, single_usage, count_ptr, nregs)
3167 register rtx from, to;
3168 char *may_not_move;
3169 rtx *single_usage;
3170 int *count_ptr;
3171 int nregs;
3172 {
3173 register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
3174 register rtx insn;
3175 register int count = 0;
3176 register rtx dest;
3177
3178 bzero ((char *) last_set, nregs * sizeof (rtx));
3179 for (insn = from; insn != to; insn = NEXT_INSN (insn))
3180 {
3181 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3182 {
3183 ++count;
3184
3185 /* If requested, record registers that have exactly one use. */
3186 if (single_usage)
3187 {
3188 find_single_use_in_loop (insn, PATTERN (insn), single_usage);
3189
3190 /* Include uses in REG_EQUAL notes. */
3191 if (REG_NOTES (insn))
3192 find_single_use_in_loop (insn, REG_NOTES (insn), single_usage);
3193 }
3194
3195 if (GET_CODE (PATTERN (insn)) == CLOBBER
3196 && GET_CODE (XEXP (PATTERN (insn), 0)) == REG)
3197 /* Don't move a reg that has an explicit clobber.
3198 We might do so sometimes, but it's not worth the pain. */
3199 may_not_move[REGNO (XEXP (PATTERN (insn), 0))] = 1;
3200
3201 if (GET_CODE (PATTERN (insn)) == SET
3202 || GET_CODE (PATTERN (insn)) == CLOBBER)
3203 {
3204 dest = SET_DEST (PATTERN (insn));
3205 while (GET_CODE (dest) == SUBREG
3206 || GET_CODE (dest) == ZERO_EXTRACT
3207 || GET_CODE (dest) == SIGN_EXTRACT
3208 || GET_CODE (dest) == STRICT_LOW_PART)
3209 dest = XEXP (dest, 0);
3210 if (GET_CODE (dest) == REG)
3211 {
3212 register int regno = REGNO (dest);
3213 /* If this is the first setting of this reg
3214 in current basic block, and it was set before,
3215 it must be set in two basic blocks, so it cannot
3216 be moved out of the loop. */
3217 if (n_times_set[regno] > 0 && last_set[regno] == 0)
3218 may_not_move[regno] = 1;
3219 /* If this is not first setting in current basic block,
3220 see if reg was used in between previous one and this.
3221 If so, neither one can be moved. */
3222 if (last_set[regno] != 0
3223 && reg_used_between_p (dest, last_set[regno], insn))
3224 may_not_move[regno] = 1;
3225 if (n_times_set[regno] < 127)
3226 ++n_times_set[regno];
3227 last_set[regno] = insn;
3228 }
3229 }
3230 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3231 {
3232 register int i;
3233 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3234 {
3235 register rtx x = XVECEXP (PATTERN (insn), 0, i);
3236 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3237 /* Don't move a reg that has an explicit clobber.
3238 It's not worth the pain to try to do it correctly. */
3239 may_not_move[REGNO (XEXP (x, 0))] = 1;
3240
3241 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3242 {
3243 dest = SET_DEST (x);
3244 while (GET_CODE (dest) == SUBREG
3245 || GET_CODE (dest) == ZERO_EXTRACT
3246 || GET_CODE (dest) == SIGN_EXTRACT
3247 || GET_CODE (dest) == STRICT_LOW_PART)
3248 dest = XEXP (dest, 0);
3249 if (GET_CODE (dest) == REG)
3250 {
3251 register int regno = REGNO (dest);
3252 if (n_times_set[regno] > 0 && last_set[regno] == 0)
3253 may_not_move[regno] = 1;
3254 if (last_set[regno] != 0
3255 && reg_used_between_p (dest, last_set[regno], insn))
3256 may_not_move[regno] = 1;
3257 if (n_times_set[regno] < 127)
3258 ++n_times_set[regno];
3259 last_set[regno] = insn;
3260 }
3261 }
3262 }
3263 }
3264 }
3265
3266 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
3267 bzero ((char *) last_set, nregs * sizeof (rtx));
3268 }
3269 *count_ptr = count;
3270 }
3271 \f
3272 /* Given a loop that is bounded by LOOP_START and LOOP_END
3273 and that is entered at SCAN_START,
3274 return 1 if the register set in SET contained in insn INSN is used by
3275 any insn that precedes INSN in cyclic order starting
3276 from the loop entry point.
3277
3278 We don't want to use INSN_LUID here because if we restrict INSN to those
3279 that have a valid INSN_LUID, it means we cannot move an invariant out
3280 from an inner loop past two loops. */
3281
3282 static int
3283 loop_reg_used_before_p (set, insn, loop_start, scan_start, loop_end)
3284 rtx set, insn, loop_start, scan_start, loop_end;
3285 {
3286 rtx reg = SET_DEST (set);
3287 rtx p;
3288
3289 /* Scan forward checking for register usage. If we hit INSN, we
3290 are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
3291 for (p = scan_start; p != insn; p = NEXT_INSN (p))
3292 {
3293 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
3294 && reg_overlap_mentioned_p (reg, PATTERN (p)))
3295 return 1;
3296
3297 if (p == loop_end)
3298 p = loop_start;
3299 }
3300
3301 return 0;
3302 }
3303 \f
3304 /* A "basic induction variable" or biv is a pseudo reg that is set
3305 (within this loop) only by incrementing or decrementing it. */
3306 /* A "general induction variable" or giv is a pseudo reg whose
3307 value is a linear function of a biv. */
3308
3309 /* Bivs are recognized by `basic_induction_var';
3310 Givs by `general_induct_var'. */
3311
3312 /* Indexed by register number, indicates whether or not register is an
3313 induction variable, and if so what type. */
3314
3315 enum iv_mode *reg_iv_type;
3316
3317 /* Indexed by register number, contains pointer to `struct induction'
3318 if register is an induction variable. This holds general info for
3319 all induction variables. */
3320
3321 struct induction **reg_iv_info;
3322
3323 /* Indexed by register number, contains pointer to `struct iv_class'
3324 if register is a basic induction variable. This holds info describing
3325 the class (a related group) of induction variables that the biv belongs
3326 to. */
3327
3328 struct iv_class **reg_biv_class;
3329
3330 /* The head of a list which links together (via the next field)
3331 every iv class for the current loop. */
3332
3333 struct iv_class *loop_iv_list;
3334
3335 /* Communication with routines called via `note_stores'. */
3336
3337 static rtx note_insn;
3338
3339 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3340
3341 static rtx addr_placeholder;
3342
3343 /* ??? Unfinished optimizations, and possible future optimizations,
3344 for the strength reduction code. */
3345
3346 /* ??? There is one more optimization you might be interested in doing: to
3347 allocate pseudo registers for frequently-accessed memory locations.
3348 If the same memory location is referenced each time around, it might
3349 be possible to copy it into a register before and out after.
3350 This is especially useful when the memory location is a variable which
3351 is in a stack slot because somewhere its address is taken. If the
3352 loop doesn't contain a function call and the variable isn't volatile,
3353 it is safe to keep the value in a register for the duration of the
3354 loop. One tricky thing is that the copying of the value back from the
3355 register has to be done on all exits from the loop. You need to check that
3356 all the exits from the loop go to the same place. */
3357
3358 /* ??? The interaction of biv elimination, and recognition of 'constant'
3359 bivs, may cause problems. */
3360
3361 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3362 performance problems.
3363
3364 Perhaps don't eliminate things that can be combined with an addressing
3365 mode. Find all givs that have the same biv, mult_val, and add_val;
3366 then for each giv, check to see if its only use dies in a following
3367 memory address. If so, generate a new memory address and check to see
3368 if it is valid. If it is valid, then store the modified memory address,
3369 otherwise, mark the giv as not done so that it will get its own iv. */
3370
3371 /* ??? Could try to optimize branches when it is known that a biv is always
3372 positive. */
3373
3374 /* ??? When replace a biv in a compare insn, we should replace with closest
3375 giv so that an optimized branch can still be recognized by the combiner,
3376 e.g. the VAX acb insn. */
3377
3378 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3379 was rerun in loop_optimize whenever a register was added or moved.
3380 Also, some of the optimizations could be a little less conservative. */
3381 \f
3382 /* Perform strength reduction and induction variable elimination. */
3383
3384 /* Pseudo registers created during this function will be beyond the last
3385 valid index in several tables including n_times_set and regno_last_uid.
3386 This does not cause a problem here, because the added registers cannot be
3387 givs outside of their loop, and hence will never be reconsidered.
3388 But scan_loop must check regnos to make sure they are in bounds. */
3389
3390 static void
3391 strength_reduce (scan_start, end, loop_top, insn_count,
3392 loop_start, loop_end, unroll_p)
3393 rtx scan_start;
3394 rtx end;
3395 rtx loop_top;
3396 int insn_count;
3397 rtx loop_start;
3398 rtx loop_end;
3399 int unroll_p;
3400 {
3401 rtx p;
3402 rtx set;
3403 rtx inc_val;
3404 rtx mult_val;
3405 rtx dest_reg;
3406 /* This is 1 if current insn is not executed at least once for every loop
3407 iteration. */
3408 int not_every_iteration = 0;
3409 /* This is 1 if current insn may be executed more than once for every
3410 loop iteration. */
3411 int maybe_multiple = 0;
3412 /* Temporary list pointers for traversing loop_iv_list. */
3413 struct iv_class *bl, **backbl;
3414 /* Ratio of extra register life span we can justify
3415 for saving an instruction. More if loop doesn't call subroutines
3416 since in that case saving an insn makes more difference
3417 and more registers are available. */
3418 /* ??? could set this to last value of threshold in move_movables */
3419 int threshold = (loop_has_call ? 1 : 2) * (3 + n_non_fixed_regs);
3420 /* Map of pseudo-register replacements. */
3421 rtx *reg_map;
3422 int call_seen;
3423 rtx test;
3424 rtx end_insert_before;
3425 int loop_depth = 0;
3426
3427 reg_iv_type = (enum iv_mode *) alloca (max_reg_before_loop
3428 * sizeof (enum iv_mode *));
3429 bzero ((char *) reg_iv_type, max_reg_before_loop * sizeof (enum iv_mode *));
3430 reg_iv_info = (struct induction **)
3431 alloca (max_reg_before_loop * sizeof (struct induction *));
3432 bzero ((char *) reg_iv_info, (max_reg_before_loop
3433 * sizeof (struct induction *)));
3434 reg_biv_class = (struct iv_class **)
3435 alloca (max_reg_before_loop * sizeof (struct iv_class *));
3436 bzero ((char *) reg_biv_class, (max_reg_before_loop
3437 * sizeof (struct iv_class *)));
3438
3439 loop_iv_list = 0;
3440 addr_placeholder = gen_reg_rtx (Pmode);
3441
3442 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3443 must be put before this insn, so that they will appear in the right
3444 order (i.e. loop order).
3445
3446 If loop_end is the end of the current function, then emit a
3447 NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3448 dummy note insn. */
3449 if (NEXT_INSN (loop_end) != 0)
3450 end_insert_before = NEXT_INSN (loop_end);
3451 else
3452 end_insert_before = emit_note_after (NOTE_INSN_DELETED, loop_end);
3453
3454 /* Scan through loop to find all possible bivs. */
3455
3456 p = scan_start;
3457 while (1)
3458 {
3459 p = NEXT_INSN (p);
3460 /* At end of a straight-in loop, we are done.
3461 At end of a loop entered at the bottom, scan the top. */
3462 if (p == scan_start)
3463 break;
3464 if (p == end)
3465 {
3466 if (loop_top != 0)
3467 p = loop_top;
3468 else
3469 break;
3470 if (p == scan_start)
3471 break;
3472 }
3473
3474 if (GET_CODE (p) == INSN
3475 && (set = single_set (p))
3476 && GET_CODE (SET_DEST (set)) == REG)
3477 {
3478 dest_reg = SET_DEST (set);
3479 if (REGNO (dest_reg) < max_reg_before_loop
3480 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
3481 && reg_iv_type[REGNO (dest_reg)] != NOT_BASIC_INDUCT)
3482 {
3483 if (basic_induction_var (SET_SRC (set), GET_MODE (SET_SRC (set)),
3484 dest_reg, p, &inc_val, &mult_val))
3485 {
3486 /* It is a possible basic induction variable.
3487 Create and initialize an induction structure for it. */
3488
3489 struct induction *v
3490 = (struct induction *) alloca (sizeof (struct induction));
3491
3492 record_biv (v, p, dest_reg, inc_val, mult_val,
3493 not_every_iteration, maybe_multiple);
3494 reg_iv_type[REGNO (dest_reg)] = BASIC_INDUCT;
3495 }
3496 else if (REGNO (dest_reg) < max_reg_before_loop)
3497 reg_iv_type[REGNO (dest_reg)] = NOT_BASIC_INDUCT;
3498 }
3499 }
3500
3501 /* Past CODE_LABEL, we get to insns that may be executed multiple
3502 times. The only way we can be sure that they can't is if every
3503 every jump insn between here and the end of the loop either
3504 returns, exits the loop, is a forward jump, or is a jump
3505 to the loop start. */
3506
3507 if (GET_CODE (p) == CODE_LABEL)
3508 {
3509 rtx insn = p;
3510
3511 maybe_multiple = 0;
3512
3513 while (1)
3514 {
3515 insn = NEXT_INSN (insn);
3516 if (insn == scan_start)
3517 break;
3518 if (insn == end)
3519 {
3520 if (loop_top != 0)
3521 insn = loop_top;
3522 else
3523 break;
3524 if (insn == scan_start)
3525 break;
3526 }
3527
3528 if (GET_CODE (insn) == JUMP_INSN
3529 && GET_CODE (PATTERN (insn)) != RETURN
3530 && (! condjump_p (insn)
3531 || (JUMP_LABEL (insn) != 0
3532 && JUMP_LABEL (insn) != scan_start
3533 && (INSN_UID (JUMP_LABEL (insn)) >= max_uid_for_loop
3534 || INSN_UID (insn) >= max_uid_for_loop
3535 || (INSN_LUID (JUMP_LABEL (insn))
3536 < INSN_LUID (insn))))))
3537 {
3538 maybe_multiple = 1;
3539 break;
3540 }
3541 }
3542 }
3543
3544 /* Past a jump, we get to insns for which we can't count
3545 on whether they will be executed during each iteration. */
3546 /* This code appears twice in strength_reduce. There is also similar
3547 code in scan_loop. */
3548 if (GET_CODE (p) == JUMP_INSN
3549 /* If we enter the loop in the middle, and scan around to the
3550 beginning, don't set not_every_iteration for that.
3551 This can be any kind of jump, since we want to know if insns
3552 will be executed if the loop is executed. */
3553 && ! (JUMP_LABEL (p) == loop_top
3554 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3555 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3556 {
3557 rtx label = 0;
3558
3559 /* If this is a jump outside the loop, then it also doesn't
3560 matter. Check to see if the target of this branch is on the
3561 loop_number_exits_labels list. */
3562
3563 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3564 label;
3565 label = LABEL_NEXTREF (label))
3566 if (XEXP (label, 0) == JUMP_LABEL (p))
3567 break;
3568
3569 if (! label)
3570 not_every_iteration = 1;
3571 }
3572
3573 else if (GET_CODE (p) == NOTE)
3574 {
3575 /* At the virtual top of a converted loop, insns are again known to
3576 be executed each iteration: logically, the loop begins here
3577 even though the exit code has been duplicated. */
3578 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3579 not_every_iteration = 0;
3580 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3581 loop_depth++;
3582 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3583 loop_depth--;
3584 }
3585
3586 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3587 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3588 or not an insn is known to be executed each iteration of the
3589 loop, whether or not any iterations are known to occur.
3590
3591 Therefore, if we have just passed a label and have no more labels
3592 between here and the test insn of the loop, we know these insns
3593 will be executed each iteration. */
3594
3595 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3596 && no_labels_between_p (p, loop_end))
3597 not_every_iteration = 0;
3598 }
3599
3600 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3601 Make a sanity check against n_times_set. */
3602 for (backbl = &loop_iv_list, bl = *backbl; bl; bl = bl->next)
3603 {
3604 if (reg_iv_type[bl->regno] != BASIC_INDUCT
3605 /* Above happens if register modified by subreg, etc. */
3606 /* Make sure it is not recognized as a basic induction var: */
3607 || n_times_set[bl->regno] != bl->biv_count
3608 /* If never incremented, it is invariant that we decided not to
3609 move. So leave it alone. */
3610 || ! bl->incremented)
3611 {
3612 if (loop_dump_stream)
3613 fprintf (loop_dump_stream, "Reg %d: biv discarded, %s\n",
3614 bl->regno,
3615 (reg_iv_type[bl->regno] != BASIC_INDUCT
3616 ? "not induction variable"
3617 : (! bl->incremented ? "never incremented"
3618 : "count error")));
3619
3620 reg_iv_type[bl->regno] = NOT_BASIC_INDUCT;
3621 *backbl = bl->next;
3622 }
3623 else
3624 {
3625 backbl = &bl->next;
3626
3627 if (loop_dump_stream)
3628 fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
3629 }
3630 }
3631
3632 /* Exit if there are no bivs. */
3633 if (! loop_iv_list)
3634 {
3635 /* Can still unroll the loop anyways, but indicate that there is no
3636 strength reduction info available. */
3637 if (unroll_p)
3638 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 0);
3639
3640 return;
3641 }
3642
3643 /* Find initial value for each biv by searching backwards from loop_start,
3644 halting at first label. Also record any test condition. */
3645
3646 call_seen = 0;
3647 for (p = loop_start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3648 {
3649 note_insn = p;
3650
3651 if (GET_CODE (p) == CALL_INSN)
3652 call_seen = 1;
3653
3654 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3655 || GET_CODE (p) == CALL_INSN)
3656 note_stores (PATTERN (p), record_initial);
3657
3658 /* Record any test of a biv that branches around the loop if no store
3659 between it and the start of loop. We only care about tests with
3660 constants and registers and only certain of those. */
3661 if (GET_CODE (p) == JUMP_INSN
3662 && JUMP_LABEL (p) != 0
3663 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end)
3664 && (test = get_condition_for_loop (p)) != 0
3665 && GET_CODE (XEXP (test, 0)) == REG
3666 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3667 && (bl = reg_biv_class[REGNO (XEXP (test, 0))]) != 0
3668 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop_start)
3669 && bl->init_insn == 0)
3670 {
3671 /* If an NE test, we have an initial value! */
3672 if (GET_CODE (test) == NE)
3673 {
3674 bl->init_insn = p;
3675 bl->init_set = gen_rtx_SET (VOIDmode,
3676 XEXP (test, 0), XEXP (test, 1));
3677 }
3678 else
3679 bl->initial_test = test;
3680 }
3681 }
3682
3683 /* Look at the each biv and see if we can say anything better about its
3684 initial value from any initializing insns set up above. (This is done
3685 in two passes to avoid missing SETs in a PARALLEL.) */
3686 for (bl = loop_iv_list; bl; bl = bl->next)
3687 {
3688 rtx src;
3689 rtx note;
3690
3691 if (! bl->init_insn)
3692 continue;
3693
3694 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
3695 is a constant, use the value of that. */
3696 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
3697 && CONSTANT_P (XEXP (note, 0)))
3698 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
3699 && CONSTANT_P (XEXP (note, 0))))
3700 src = XEXP (note, 0);
3701 else
3702 src = SET_SRC (bl->init_set);
3703
3704 if (loop_dump_stream)
3705 fprintf (loop_dump_stream,
3706 "Biv %d initialized at insn %d: initial value ",
3707 bl->regno, INSN_UID (bl->init_insn));
3708
3709 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
3710 || GET_MODE (src) == VOIDmode)
3711 && valid_initial_value_p (src, bl->init_insn, call_seen, loop_start))
3712 {
3713 bl->initial_value = src;
3714
3715 if (loop_dump_stream)
3716 {
3717 if (GET_CODE (src) == CONST_INT)
3718 {
3719 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (src));
3720 fputc ('\n', loop_dump_stream);
3721 }
3722 else
3723 {
3724 print_rtl (loop_dump_stream, src);
3725 fprintf (loop_dump_stream, "\n");
3726 }
3727 }
3728 }
3729 else
3730 {
3731 /* Biv initial value is not simple move,
3732 so let it keep initial value of "itself". */
3733
3734 if (loop_dump_stream)
3735 fprintf (loop_dump_stream, "is complex\n");
3736 }
3737 }
3738
3739 /* Search the loop for general induction variables. */
3740
3741 /* A register is a giv if: it is only set once, it is a function of a
3742 biv and a constant (or invariant), and it is not a biv. */
3743
3744 not_every_iteration = 0;
3745 loop_depth = 0;
3746 p = scan_start;
3747 while (1)
3748 {
3749 p = NEXT_INSN (p);
3750 /* At end of a straight-in loop, we are done.
3751 At end of a loop entered at the bottom, scan the top. */
3752 if (p == scan_start)
3753 break;
3754 if (p == end)
3755 {
3756 if (loop_top != 0)
3757 p = loop_top;
3758 else
3759 break;
3760 if (p == scan_start)
3761 break;
3762 }
3763
3764 /* Look for a general induction variable in a register. */
3765 if (GET_CODE (p) == INSN
3766 && (set = single_set (p))
3767 && GET_CODE (SET_DEST (set)) == REG
3768 && ! may_not_optimize[REGNO (SET_DEST (set))])
3769 {
3770 rtx src_reg;
3771 rtx add_val;
3772 rtx mult_val;
3773 int benefit;
3774 rtx regnote = 0;
3775
3776 dest_reg = SET_DEST (set);
3777 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
3778 continue;
3779
3780 if (/* SET_SRC is a giv. */
3781 ((benefit = general_induction_var (SET_SRC (set),
3782 &src_reg, &add_val,
3783 &mult_val))
3784 /* Equivalent expression is a giv. */
3785 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
3786 && (benefit = general_induction_var (XEXP (regnote, 0),
3787 &src_reg,
3788 &add_val, &mult_val))))
3789 /* Don't try to handle any regs made by loop optimization.
3790 We have nothing on them in regno_first_uid, etc. */
3791 && REGNO (dest_reg) < max_reg_before_loop
3792 /* Don't recognize a BASIC_INDUCT_VAR here. */
3793 && dest_reg != src_reg
3794 /* This must be the only place where the register is set. */
3795 && (n_times_set[REGNO (dest_reg)] == 1
3796 /* or all sets must be consecutive and make a giv. */
3797 || (benefit = consec_sets_giv (benefit, p,
3798 src_reg, dest_reg,
3799 &add_val, &mult_val))))
3800 {
3801 int count;
3802 struct induction *v
3803 = (struct induction *) alloca (sizeof (struct induction));
3804 rtx temp;
3805
3806 /* If this is a library call, increase benefit. */
3807 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
3808 benefit += libcall_benefit (p);
3809
3810 /* Skip the consecutive insns, if there are any. */
3811 for (count = n_times_set[REGNO (dest_reg)] - 1;
3812 count > 0; count--)
3813 {
3814 /* If first insn of libcall sequence, skip to end.
3815 Do this at start of loop, since INSN is guaranteed to
3816 be an insn here. */
3817 if (GET_CODE (p) != NOTE
3818 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3819 p = XEXP (temp, 0);
3820
3821 do p = NEXT_INSN (p);
3822 while (GET_CODE (p) == NOTE);
3823 }
3824
3825 record_giv (v, p, src_reg, dest_reg, mult_val, add_val, benefit,
3826 DEST_REG, not_every_iteration, NULL_PTR, loop_start,
3827 loop_end);
3828
3829 }
3830 }
3831
3832 #ifndef DONT_REDUCE_ADDR
3833 /* Look for givs which are memory addresses. */
3834 /* This resulted in worse code on a VAX 8600. I wonder if it
3835 still does. */
3836 if (GET_CODE (p) == INSN)
3837 find_mem_givs (PATTERN (p), p, not_every_iteration, loop_start,
3838 loop_end);
3839 #endif
3840
3841 /* Update the status of whether giv can derive other givs. This can
3842 change when we pass a label or an insn that updates a biv. */
3843 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3844 || GET_CODE (p) == CODE_LABEL)
3845 update_giv_derive (p);
3846
3847 /* Past a jump, we get to insns for which we can't count
3848 on whether they will be executed during each iteration. */
3849 /* This code appears twice in strength_reduce. There is also similar
3850 code in scan_loop. */
3851 if (GET_CODE (p) == JUMP_INSN
3852 /* If we enter the loop in the middle, and scan around to the
3853 beginning, don't set not_every_iteration for that.
3854 This can be any kind of jump, since we want to know if insns
3855 will be executed if the loop is executed. */
3856 && ! (JUMP_LABEL (p) == loop_top
3857 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3858 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3859 {
3860 rtx label = 0;
3861
3862 /* If this is a jump outside the loop, then it also doesn't
3863 matter. Check to see if the target of this branch is on the
3864 loop_number_exits_labels list. */
3865
3866 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3867 label;
3868 label = LABEL_NEXTREF (label))
3869 if (XEXP (label, 0) == JUMP_LABEL (p))
3870 break;
3871
3872 if (! label)
3873 not_every_iteration = 1;
3874 }
3875
3876 else if (GET_CODE (p) == NOTE)
3877 {
3878 /* At the virtual top of a converted loop, insns are again known to
3879 be executed each iteration: logically, the loop begins here
3880 even though the exit code has been duplicated. */
3881 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3882 not_every_iteration = 0;
3883 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3884 loop_depth++;
3885 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3886 loop_depth--;
3887 }
3888
3889 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3890 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3891 or not an insn is known to be executed each iteration of the
3892 loop, whether or not any iterations are known to occur.
3893
3894 Therefore, if we have just passed a label and have no more labels
3895 between here and the test insn of the loop, we know these insns
3896 will be executed each iteration. */
3897
3898 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3899 && no_labels_between_p (p, loop_end))
3900 not_every_iteration = 0;
3901 }
3902
3903 /* Try to calculate and save the number of loop iterations. This is
3904 set to zero if the actual number can not be calculated. This must
3905 be called after all giv's have been identified, since otherwise it may
3906 fail if the iteration variable is a giv. */
3907
3908 loop_n_iterations = loop_iterations (loop_start, loop_end);
3909
3910 /* Now for each giv for which we still don't know whether or not it is
3911 replaceable, check to see if it is replaceable because its final value
3912 can be calculated. This must be done after loop_iterations is called,
3913 so that final_giv_value will work correctly. */
3914
3915 for (bl = loop_iv_list; bl; bl = bl->next)
3916 {
3917 struct induction *v;
3918
3919 for (v = bl->giv; v; v = v->next_iv)
3920 if (! v->replaceable && ! v->not_replaceable)
3921 check_final_value (v, loop_start, loop_end);
3922 }
3923
3924 /* Try to prove that the loop counter variable (if any) is always
3925 nonnegative; if so, record that fact with a REG_NONNEG note
3926 so that "decrement and branch until zero" insn can be used. */
3927 check_dbra_loop (loop_end, insn_count, loop_start);
3928
3929 #ifdef HAIFA
3930 /* record loop-variables relevant for BCT optimization before unrolling
3931 the loop. Unrolling may update part of this information, and the
3932 correct data will be used for generating the BCT. */
3933 #ifdef HAVE_decrement_and_branch_on_count
3934 if (HAVE_decrement_and_branch_on_count)
3935 analyze_loop_iterations (loop_start, loop_end);
3936 #endif
3937 #endif /* HAIFA */
3938
3939 /* Create reg_map to hold substitutions for replaceable giv regs. */
3940 reg_map = (rtx *) alloca (max_reg_before_loop * sizeof (rtx));
3941 bzero ((char *) reg_map, max_reg_before_loop * sizeof (rtx));
3942
3943 /* Examine each iv class for feasibility of strength reduction/induction
3944 variable elimination. */
3945
3946 for (bl = loop_iv_list; bl; bl = bl->next)
3947 {
3948 struct induction *v;
3949 int benefit;
3950 int all_reduced;
3951 rtx final_value = 0;
3952
3953 /* Test whether it will be possible to eliminate this biv
3954 provided all givs are reduced. This is possible if either
3955 the reg is not used outside the loop, or we can compute
3956 what its final value will be.
3957
3958 For architectures with a decrement_and_branch_until_zero insn,
3959 don't do this if we put a REG_NONNEG note on the endtest for
3960 this biv. */
3961
3962 /* Compare against bl->init_insn rather than loop_start.
3963 We aren't concerned with any uses of the biv between
3964 init_insn and loop_start since these won't be affected
3965 by the value of the biv elsewhere in the function, so
3966 long as init_insn doesn't use the biv itself.
3967 March 14, 1989 -- self@bayes.arc.nasa.gov */
3968
3969 if ((uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
3970 && bl->init_insn
3971 && INSN_UID (bl->init_insn) < max_uid_for_loop
3972 && uid_luid[REGNO_FIRST_UID (bl->regno)] >= INSN_LUID (bl->init_insn)
3973 #ifdef HAVE_decrement_and_branch_until_zero
3974 && ! bl->nonneg
3975 #endif
3976 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
3977 || ((final_value = final_biv_value (bl, loop_start, loop_end))
3978 #ifdef HAVE_decrement_and_branch_until_zero
3979 && ! bl->nonneg
3980 #endif
3981 ))
3982 bl->eliminable = maybe_eliminate_biv (bl, loop_start, end, 0,
3983 threshold, insn_count);
3984 else
3985 {
3986 if (loop_dump_stream)
3987 {
3988 fprintf (loop_dump_stream,
3989 "Cannot eliminate biv %d.\n",
3990 bl->regno);
3991 fprintf (loop_dump_stream,
3992 "First use: insn %d, last use: insn %d.\n",
3993 REGNO_FIRST_UID (bl->regno),
3994 REGNO_LAST_UID (bl->regno));
3995 }
3996 }
3997
3998 /* Combine all giv's for this iv_class. */
3999 combine_givs (bl);
4000
4001 /* This will be true at the end, if all givs which depend on this
4002 biv have been strength reduced.
4003 We can't (currently) eliminate the biv unless this is so. */
4004 all_reduced = 1;
4005
4006 /* Check each giv in this class to see if we will benefit by reducing
4007 it. Skip giv's combined with others. */
4008 for (v = bl->giv; v; v = v->next_iv)
4009 {
4010 struct induction *tv;
4011
4012 if (v->ignore || v->same)
4013 continue;
4014
4015 benefit = v->benefit;
4016
4017 /* Reduce benefit if not replaceable, since we will insert
4018 a move-insn to replace the insn that calculates this giv.
4019 Don't do this unless the giv is a user variable, since it
4020 will often be marked non-replaceable because of the duplication
4021 of the exit code outside the loop. In such a case, the copies
4022 we insert are dead and will be deleted. So they don't have
4023 a cost. Similar situations exist. */
4024 /* ??? The new final_[bg]iv_value code does a much better job
4025 of finding replaceable giv's, and hence this code may no longer
4026 be necessary. */
4027 if (! v->replaceable && ! bl->eliminable
4028 && REG_USERVAR_P (v->dest_reg))
4029 benefit -= copy_cost;
4030
4031 /* Decrease the benefit to count the add-insns that we will
4032 insert to increment the reduced reg for the giv. */
4033 benefit -= add_cost * bl->biv_count;
4034
4035 /* Decide whether to strength-reduce this giv or to leave the code
4036 unchanged (recompute it from the biv each time it is used).
4037 This decision can be made independently for each giv. */
4038
4039 #ifdef AUTO_INC_DEC
4040 /* Attempt to guess whether autoincrement will handle some of the
4041 new add insns; if so, increase BENEFIT (undo the subtraction of
4042 add_cost that was done above). */
4043 if (v->giv_type == DEST_ADDR
4044 && GET_CODE (v->mult_val) == CONST_INT)
4045 {
4046 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_PRE_INCREMENT)
4047 if (INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4048 benefit += add_cost * bl->biv_count;
4049 #endif
4050 #if defined (HAVE_POST_DECREMENT) || defined (HAVE_PRE_DECREMENT)
4051 if (-INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4052 benefit += add_cost * bl->biv_count;
4053 #endif
4054 }
4055 #endif
4056
4057 /* If an insn is not to be strength reduced, then set its ignore
4058 flag, and clear all_reduced. */
4059
4060 /* A giv that depends on a reversed biv must be reduced if it is
4061 used after the loop exit, otherwise, it would have the wrong
4062 value after the loop exit. To make it simple, just reduce all
4063 of such giv's whether or not we know they are used after the loop
4064 exit. */
4065
4066 if ( ! flag_reduce_all_givs && v->lifetime * threshold * benefit < insn_count
4067 && ! bl->reversed )
4068 {
4069 if (loop_dump_stream)
4070 fprintf (loop_dump_stream,
4071 "giv of insn %d not worth while, %d vs %d.\n",
4072 INSN_UID (v->insn),
4073 v->lifetime * threshold * benefit, insn_count);
4074 v->ignore = 1;
4075 all_reduced = 0;
4076 }
4077 else
4078 {
4079 /* Check that we can increment the reduced giv without a
4080 multiply insn. If not, reject it. */
4081
4082 for (tv = bl->biv; tv; tv = tv->next_iv)
4083 if (tv->mult_val == const1_rtx
4084 && ! product_cheap_p (tv->add_val, v->mult_val))
4085 {
4086 if (loop_dump_stream)
4087 fprintf (loop_dump_stream,
4088 "giv of insn %d: would need a multiply.\n",
4089 INSN_UID (v->insn));
4090 v->ignore = 1;
4091 all_reduced = 0;
4092 break;
4093 }
4094 }
4095 }
4096
4097 /* Reduce each giv that we decided to reduce. */
4098
4099 for (v = bl->giv; v; v = v->next_iv)
4100 {
4101 struct induction *tv;
4102 if (! v->ignore && v->same == 0)
4103 {
4104 int auto_inc_opt = 0;
4105
4106 v->new_reg = gen_reg_rtx (v->mode);
4107
4108 #ifdef AUTO_INC_DEC
4109 /* If the target has auto-increment addressing modes, and
4110 this is an address giv, then try to put the increment
4111 immediately after its use, so that flow can create an
4112 auto-increment addressing mode. */
4113 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4114 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4115 /* We don't handle reversed biv's because bl->biv->insn
4116 does not have a valid INSN_LUID. */
4117 && ! bl->reversed
4118 && v->always_executed && ! v->maybe_multiple)
4119 {
4120 /* If other giv's have been combined with this one, then
4121 this will work only if all uses of the other giv's occur
4122 before this giv's insn. This is difficult to check.
4123
4124 We simplify this by looking for the common case where
4125 there is one DEST_REG giv, and this giv's insn is the
4126 last use of the dest_reg of that DEST_REG giv. If the
4127 the increment occurs after the address giv, then we can
4128 perform the optimization. (Otherwise, the increment
4129 would have to go before other_giv, and we would not be
4130 able to combine it with the address giv to get an
4131 auto-inc address.) */
4132 if (v->combined_with)
4133 {
4134 struct induction *other_giv = 0;
4135
4136 for (tv = bl->giv; tv; tv = tv->next_iv)
4137 if (tv->same == v)
4138 {
4139 if (other_giv)
4140 break;
4141 else
4142 other_giv = tv;
4143 }
4144 if (! tv && other_giv
4145 && REGNO (other_giv->dest_reg) < max_reg_before_loop
4146 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4147 == INSN_UID (v->insn))
4148 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4149 auto_inc_opt = 1;
4150 }
4151 /* Check for case where increment is before the the address
4152 giv. Do this test in "loop order". */
4153 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4154 && (INSN_LUID (v->insn) < INSN_LUID (scan_start)
4155 || (INSN_LUID (bl->biv->insn)
4156 > INSN_LUID (scan_start))))
4157 || (INSN_LUID (v->insn) < INSN_LUID (scan_start)
4158 && (INSN_LUID (scan_start)
4159 < INSN_LUID (bl->biv->insn))))
4160 auto_inc_opt = -1;
4161 else
4162 auto_inc_opt = 1;
4163
4164 #ifdef HAVE_cc0
4165 {
4166 rtx prev;
4167
4168 /* We can't put an insn immediately after one setting
4169 cc0, or immediately before one using cc0. */
4170 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4171 || (auto_inc_opt == -1
4172 && (prev = prev_nonnote_insn (v->insn)) != 0
4173 && GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4174 && sets_cc0_p (PATTERN (prev))))
4175 auto_inc_opt = 0;
4176 }
4177 #endif
4178
4179 if (auto_inc_opt)
4180 v->auto_inc_opt = 1;
4181 }
4182 #endif
4183
4184 /* For each place where the biv is incremented, add an insn
4185 to increment the new, reduced reg for the giv. */
4186 for (tv = bl->biv; tv; tv = tv->next_iv)
4187 {
4188 rtx insert_before;
4189
4190 if (! auto_inc_opt)
4191 insert_before = tv->insn;
4192 else if (auto_inc_opt == 1)
4193 insert_before = NEXT_INSN (v->insn);
4194 else
4195 insert_before = v->insn;
4196
4197 if (tv->mult_val == const1_rtx)
4198 emit_iv_add_mult (tv->add_val, v->mult_val,
4199 v->new_reg, v->new_reg, insert_before);
4200 else /* tv->mult_val == const0_rtx */
4201 /* A multiply is acceptable here
4202 since this is presumed to be seldom executed. */
4203 emit_iv_add_mult (tv->add_val, v->mult_val,
4204 v->add_val, v->new_reg, insert_before);
4205 }
4206
4207 /* Add code at loop start to initialize giv's reduced reg. */
4208
4209 emit_iv_add_mult (bl->initial_value, v->mult_val,
4210 v->add_val, v->new_reg, loop_start);
4211 }
4212 }
4213
4214 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4215 as not reduced.
4216
4217 For each giv register that can be reduced now: if replaceable,
4218 substitute reduced reg wherever the old giv occurs;
4219 else add new move insn "giv_reg = reduced_reg".
4220
4221 Also check for givs whose first use is their definition and whose
4222 last use is the definition of another giv. If so, it is likely
4223 dead and should not be used to eliminate a biv. */
4224 for (v = bl->giv; v; v = v->next_iv)
4225 {
4226 if (v->same && v->same->ignore)
4227 v->ignore = 1;
4228
4229 if (v->ignore)
4230 continue;
4231
4232 if (v->giv_type == DEST_REG
4233 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4234 {
4235 struct induction *v1;
4236
4237 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4238 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4239 v->maybe_dead = 1;
4240 }
4241
4242 /* Update expression if this was combined, in case other giv was
4243 replaced. */
4244 if (v->same)
4245 v->new_reg = replace_rtx (v->new_reg,
4246 v->same->dest_reg, v->same->new_reg);
4247
4248 if (v->giv_type == DEST_ADDR)
4249 /* Store reduced reg as the address in the memref where we found
4250 this giv. */
4251 validate_change (v->insn, v->location, v->new_reg, 0);
4252 else if (v->replaceable)
4253 {
4254 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4255
4256 #if 0
4257 /* I can no longer duplicate the original problem. Perhaps
4258 this is unnecessary now? */
4259
4260 /* Replaceable; it isn't strictly necessary to delete the old
4261 insn and emit a new one, because v->dest_reg is now dead.
4262
4263 However, especially when unrolling loops, the special
4264 handling for (set REG0 REG1) in the second cse pass may
4265 make v->dest_reg live again. To avoid this problem, emit
4266 an insn to set the original giv reg from the reduced giv.
4267 We can not delete the original insn, since it may be part
4268 of a LIBCALL, and the code in flow that eliminates dead
4269 libcalls will fail if it is deleted. */
4270 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4271 v->insn);
4272 #endif
4273 }
4274 else
4275 {
4276 /* Not replaceable; emit an insn to set the original giv reg from
4277 the reduced giv, same as above. */
4278 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4279 v->insn);
4280 }
4281
4282 /* When a loop is reversed, givs which depend on the reversed
4283 biv, and which are live outside the loop, must be set to their
4284 correct final value. This insn is only needed if the giv is
4285 not replaceable. The correct final value is the same as the
4286 value that the giv starts the reversed loop with. */
4287 if (bl->reversed && ! v->replaceable)
4288 emit_iv_add_mult (bl->initial_value, v->mult_val,
4289 v->add_val, v->dest_reg, end_insert_before);
4290 else if (v->final_value)
4291 {
4292 rtx insert_before;
4293
4294 /* If the loop has multiple exits, emit the insn before the
4295 loop to ensure that it will always be executed no matter
4296 how the loop exits. Otherwise, emit the insn after the loop,
4297 since this is slightly more efficient. */
4298 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4299 insert_before = loop_start;
4300 else
4301 insert_before = end_insert_before;
4302 emit_insn_before (gen_move_insn (v->dest_reg, v->final_value),
4303 insert_before);
4304
4305 #if 0
4306 /* If the insn to set the final value of the giv was emitted
4307 before the loop, then we must delete the insn inside the loop
4308 that sets it. If this is a LIBCALL, then we must delete
4309 every insn in the libcall. Note, however, that
4310 final_giv_value will only succeed when there are multiple
4311 exits if the giv is dead at each exit, hence it does not
4312 matter that the original insn remains because it is dead
4313 anyways. */
4314 /* Delete the insn inside the loop that sets the giv since
4315 the giv is now set before (or after) the loop. */
4316 delete_insn (v->insn);
4317 #endif
4318 }
4319
4320 if (loop_dump_stream)
4321 {
4322 fprintf (loop_dump_stream, "giv at %d reduced to ",
4323 INSN_UID (v->insn));
4324 print_rtl (loop_dump_stream, v->new_reg);
4325 fprintf (loop_dump_stream, "\n");
4326 }
4327 }
4328
4329 /* All the givs based on the biv bl have been reduced if they
4330 merit it. */
4331
4332 /* For each giv not marked as maybe dead that has been combined with a
4333 second giv, clear any "maybe dead" mark on that second giv.
4334 v->new_reg will either be or refer to the register of the giv it
4335 combined with.
4336
4337 Doing this clearing avoids problems in biv elimination where a
4338 giv's new_reg is a complex value that can't be put in the insn but
4339 the giv combined with (with a reg as new_reg) is marked maybe_dead.
4340 Since the register will be used in either case, we'd prefer it be
4341 used from the simpler giv. */
4342
4343 for (v = bl->giv; v; v = v->next_iv)
4344 if (! v->maybe_dead && v->same)
4345 v->same->maybe_dead = 0;
4346
4347 /* Try to eliminate the biv, if it is a candidate.
4348 This won't work if ! all_reduced,
4349 since the givs we planned to use might not have been reduced.
4350
4351 We have to be careful that we didn't initially think we could eliminate
4352 this biv because of a giv that we now think may be dead and shouldn't
4353 be used as a biv replacement.
4354
4355 Also, there is the possibility that we may have a giv that looks
4356 like it can be used to eliminate a biv, but the resulting insn
4357 isn't valid. This can happen, for example, on the 88k, where a
4358 JUMP_INSN can compare a register only with zero. Attempts to
4359 replace it with a compare with a constant will fail.
4360
4361 Note that in cases where this call fails, we may have replaced some
4362 of the occurrences of the biv with a giv, but no harm was done in
4363 doing so in the rare cases where it can occur. */
4364
4365 if (all_reduced == 1 && bl->eliminable
4366 && maybe_eliminate_biv (bl, loop_start, end, 1,
4367 threshold, insn_count))
4368
4369 {
4370 /* ?? If we created a new test to bypass the loop entirely,
4371 or otherwise drop straight in, based on this test, then
4372 we might want to rewrite it also. This way some later
4373 pass has more hope of removing the initialization of this
4374 biv entirely. */
4375
4376 /* If final_value != 0, then the biv may be used after loop end
4377 and we must emit an insn to set it just in case.
4378
4379 Reversed bivs already have an insn after the loop setting their
4380 value, so we don't need another one. We can't calculate the
4381 proper final value for such a biv here anyways. */
4382 if (final_value != 0 && ! bl->reversed)
4383 {
4384 rtx insert_before;
4385
4386 /* If the loop has multiple exits, emit the insn before the
4387 loop to ensure that it will always be executed no matter
4388 how the loop exits. Otherwise, emit the insn after the
4389 loop, since this is slightly more efficient. */
4390 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4391 insert_before = loop_start;
4392 else
4393 insert_before = end_insert_before;
4394
4395 emit_insn_before (gen_move_insn (bl->biv->dest_reg, final_value),
4396 end_insert_before);
4397 }
4398
4399 #if 0
4400 /* Delete all of the instructions inside the loop which set
4401 the biv, as they are all dead. If is safe to delete them,
4402 because an insn setting a biv will never be part of a libcall. */
4403 /* However, deleting them will invalidate the regno_last_uid info,
4404 so keeping them around is more convenient. Final_biv_value
4405 will only succeed when there are multiple exits if the biv
4406 is dead at each exit, hence it does not matter that the original
4407 insn remains, because it is dead anyways. */
4408 for (v = bl->biv; v; v = v->next_iv)
4409 delete_insn (v->insn);
4410 #endif
4411
4412 if (loop_dump_stream)
4413 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
4414 bl->regno);
4415 }
4416 }
4417
4418 /* Go through all the instructions in the loop, making all the
4419 register substitutions scheduled in REG_MAP. */
4420
4421 for (p = loop_start; p != end; p = NEXT_INSN (p))
4422 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4423 || GET_CODE (p) == CALL_INSN)
4424 {
4425 replace_regs (PATTERN (p), reg_map, max_reg_before_loop, 0);
4426 replace_regs (REG_NOTES (p), reg_map, max_reg_before_loop, 0);
4427 INSN_CODE (p) = -1;
4428 }
4429
4430 /* Unroll loops from within strength reduction so that we can use the
4431 induction variable information that strength_reduce has already
4432 collected. */
4433
4434 if (unroll_p)
4435 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 1);
4436
4437 #ifdef HAIFA
4438 /* instrument the loop with bct insn */
4439 #ifdef HAVE_decrement_and_branch_on_count
4440 if (HAVE_decrement_and_branch_on_count)
4441 insert_bct (loop_start, loop_end);
4442 #endif
4443 #endif /* HAIFA */
4444
4445 if (loop_dump_stream)
4446 fprintf (loop_dump_stream, "\n");
4447 }
4448 \f
4449 /* Return 1 if X is a valid source for an initial value (or as value being
4450 compared against in an initial test).
4451
4452 X must be either a register or constant and must not be clobbered between
4453 the current insn and the start of the loop.
4454
4455 INSN is the insn containing X. */
4456
4457 static int
4458 valid_initial_value_p (x, insn, call_seen, loop_start)
4459 rtx x;
4460 rtx insn;
4461 int call_seen;
4462 rtx loop_start;
4463 {
4464 if (CONSTANT_P (x))
4465 return 1;
4466
4467 /* Only consider pseudos we know about initialized in insns whose luids
4468 we know. */
4469 if (GET_CODE (x) != REG
4470 || REGNO (x) >= max_reg_before_loop)
4471 return 0;
4472
4473 /* Don't use call-clobbered registers across a call which clobbers it. On
4474 some machines, don't use any hard registers at all. */
4475 if (REGNO (x) < FIRST_PSEUDO_REGISTER
4476 && (SMALL_REGISTER_CLASSES
4477 || (call_used_regs[REGNO (x)] && call_seen)))
4478 return 0;
4479
4480 /* Don't use registers that have been clobbered before the start of the
4481 loop. */
4482 if (reg_set_between_p (x, insn, loop_start))
4483 return 0;
4484
4485 return 1;
4486 }
4487 \f
4488 /* Scan X for memory refs and check each memory address
4489 as a possible giv. INSN is the insn whose pattern X comes from.
4490 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
4491 every loop iteration. */
4492
4493 static void
4494 find_mem_givs (x, insn, not_every_iteration, loop_start, loop_end)
4495 rtx x;
4496 rtx insn;
4497 int not_every_iteration;
4498 rtx loop_start, loop_end;
4499 {
4500 register int i, j;
4501 register enum rtx_code code;
4502 register char *fmt;
4503
4504 if (x == 0)
4505 return;
4506
4507 code = GET_CODE (x);
4508 switch (code)
4509 {
4510 case REG:
4511 case CONST_INT:
4512 case CONST:
4513 case CONST_DOUBLE:
4514 case SYMBOL_REF:
4515 case LABEL_REF:
4516 case PC:
4517 case CC0:
4518 case ADDR_VEC:
4519 case ADDR_DIFF_VEC:
4520 case USE:
4521 case CLOBBER:
4522 return;
4523
4524 case MEM:
4525 {
4526 rtx src_reg;
4527 rtx add_val;
4528 rtx mult_val;
4529 int benefit;
4530
4531 benefit = general_induction_var (XEXP (x, 0),
4532 &src_reg, &add_val, &mult_val);
4533
4534 /* Don't make a DEST_ADDR giv with mult_val == 1 && add_val == 0.
4535 Such a giv isn't useful. */
4536 if (benefit > 0 && (mult_val != const1_rtx || add_val != const0_rtx))
4537 {
4538 /* Found one; record it. */
4539 struct induction *v
4540 = (struct induction *) oballoc (sizeof (struct induction));
4541
4542 record_giv (v, insn, src_reg, addr_placeholder, mult_val,
4543 add_val, benefit, DEST_ADDR, not_every_iteration,
4544 &XEXP (x, 0), loop_start, loop_end);
4545
4546 v->mem_mode = GET_MODE (x);
4547 }
4548 }
4549 return;
4550
4551 default:
4552 break;
4553 }
4554
4555 /* Recursively scan the subexpressions for other mem refs. */
4556
4557 fmt = GET_RTX_FORMAT (code);
4558 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4559 if (fmt[i] == 'e')
4560 find_mem_givs (XEXP (x, i), insn, not_every_iteration, loop_start,
4561 loop_end);
4562 else if (fmt[i] == 'E')
4563 for (j = 0; j < XVECLEN (x, i); j++)
4564 find_mem_givs (XVECEXP (x, i, j), insn, not_every_iteration,
4565 loop_start, loop_end);
4566 }
4567 \f
4568 /* Fill in the data about one biv update.
4569 V is the `struct induction' in which we record the biv. (It is
4570 allocated by the caller, with alloca.)
4571 INSN is the insn that sets it.
4572 DEST_REG is the biv's reg.
4573
4574 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
4575 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
4576 being set to INC_VAL.
4577
4578 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
4579 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
4580 can be executed more than once per iteration. If MAYBE_MULTIPLE
4581 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
4582 executed exactly once per iteration. */
4583
4584 static void
4585 record_biv (v, insn, dest_reg, inc_val, mult_val,
4586 not_every_iteration, maybe_multiple)
4587 struct induction *v;
4588 rtx insn;
4589 rtx dest_reg;
4590 rtx inc_val;
4591 rtx mult_val;
4592 int not_every_iteration;
4593 int maybe_multiple;
4594 {
4595 struct iv_class *bl;
4596
4597 v->insn = insn;
4598 v->src_reg = dest_reg;
4599 v->dest_reg = dest_reg;
4600 v->mult_val = mult_val;
4601 v->add_val = inc_val;
4602 v->mode = GET_MODE (dest_reg);
4603 v->always_computable = ! not_every_iteration;
4604 v->always_executed = ! not_every_iteration;
4605 v->maybe_multiple = maybe_multiple;
4606
4607 /* Add this to the reg's iv_class, creating a class
4608 if this is the first incrementation of the reg. */
4609
4610 bl = reg_biv_class[REGNO (dest_reg)];
4611 if (bl == 0)
4612 {
4613 /* Create and initialize new iv_class. */
4614
4615 bl = (struct iv_class *) oballoc (sizeof (struct iv_class));
4616
4617 bl->regno = REGNO (dest_reg);
4618 bl->biv = 0;
4619 bl->giv = 0;
4620 bl->biv_count = 0;
4621 bl->giv_count = 0;
4622
4623 /* Set initial value to the reg itself. */
4624 bl->initial_value = dest_reg;
4625 /* We haven't seen the initializing insn yet */
4626 bl->init_insn = 0;
4627 bl->init_set = 0;
4628 bl->initial_test = 0;
4629 bl->incremented = 0;
4630 bl->eliminable = 0;
4631 bl->nonneg = 0;
4632 bl->reversed = 0;
4633 bl->total_benefit = 0;
4634
4635 /* Add this class to loop_iv_list. */
4636 bl->next = loop_iv_list;
4637 loop_iv_list = bl;
4638
4639 /* Put it in the array of biv register classes. */
4640 reg_biv_class[REGNO (dest_reg)] = bl;
4641 }
4642
4643 /* Update IV_CLASS entry for this biv. */
4644 v->next_iv = bl->biv;
4645 bl->biv = v;
4646 bl->biv_count++;
4647 if (mult_val == const1_rtx)
4648 bl->incremented = 1;
4649
4650 if (loop_dump_stream)
4651 {
4652 fprintf (loop_dump_stream,
4653 "Insn %d: possible biv, reg %d,",
4654 INSN_UID (insn), REGNO (dest_reg));
4655 if (GET_CODE (inc_val) == CONST_INT)
4656 {
4657 fprintf (loop_dump_stream, " const =");
4658 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (inc_val));
4659 fputc ('\n', loop_dump_stream);
4660 }
4661 else
4662 {
4663 fprintf (loop_dump_stream, " const = ");
4664 print_rtl (loop_dump_stream, inc_val);
4665 fprintf (loop_dump_stream, "\n");
4666 }
4667 }
4668 }
4669 \f
4670 /* Fill in the data about one giv.
4671 V is the `struct induction' in which we record the giv. (It is
4672 allocated by the caller, with alloca.)
4673 INSN is the insn that sets it.
4674 BENEFIT estimates the savings from deleting this insn.
4675 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
4676 into a register or is used as a memory address.
4677
4678 SRC_REG is the biv reg which the giv is computed from.
4679 DEST_REG is the giv's reg (if the giv is stored in a reg).
4680 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
4681 LOCATION points to the place where this giv's value appears in INSN. */
4682
4683 static void
4684 record_giv (v, insn, src_reg, dest_reg, mult_val, add_val, benefit,
4685 type, not_every_iteration, location, loop_start, loop_end)
4686 struct induction *v;
4687 rtx insn;
4688 rtx src_reg;
4689 rtx dest_reg;
4690 rtx mult_val, add_val;
4691 int benefit;
4692 enum g_types type;
4693 int not_every_iteration;
4694 rtx *location;
4695 rtx loop_start, loop_end;
4696 {
4697 struct induction *b;
4698 struct iv_class *bl;
4699 rtx set = single_set (insn);
4700
4701 v->insn = insn;
4702 v->src_reg = src_reg;
4703 v->giv_type = type;
4704 v->dest_reg = dest_reg;
4705 v->mult_val = mult_val;
4706 v->add_val = add_val;
4707 v->benefit = benefit;
4708 v->location = location;
4709 v->cant_derive = 0;
4710 v->combined_with = 0;
4711 v->maybe_multiple = 0;
4712 v->maybe_dead = 0;
4713 v->derive_adjustment = 0;
4714 v->same = 0;
4715 v->ignore = 0;
4716 v->new_reg = 0;
4717 v->final_value = 0;
4718 v->same_insn = 0;
4719 v->auto_inc_opt = 0;
4720 v->unrolled = 0;
4721 v->shared = 0;
4722
4723 /* The v->always_computable field is used in update_giv_derive, to
4724 determine whether a giv can be used to derive another giv. For a
4725 DEST_REG giv, INSN computes a new value for the giv, so its value
4726 isn't computable if INSN insn't executed every iteration.
4727 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
4728 it does not compute a new value. Hence the value is always computable
4729 regardless of whether INSN is executed each iteration. */
4730
4731 if (type == DEST_ADDR)
4732 v->always_computable = 1;
4733 else
4734 v->always_computable = ! not_every_iteration;
4735
4736 v->always_executed = ! not_every_iteration;
4737
4738 if (type == DEST_ADDR)
4739 {
4740 v->mode = GET_MODE (*location);
4741 v->lifetime = 1;
4742 v->times_used = 1;
4743 }
4744 else /* type == DEST_REG */
4745 {
4746 v->mode = GET_MODE (SET_DEST (set));
4747
4748 v->lifetime = (uid_luid[REGNO_LAST_UID (REGNO (dest_reg))]
4749 - uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))]);
4750
4751 v->times_used = n_times_used[REGNO (dest_reg)];
4752
4753 /* If the lifetime is zero, it means that this register is
4754 really a dead store. So mark this as a giv that can be
4755 ignored. This will not prevent the biv from being eliminated. */
4756 if (v->lifetime == 0)
4757 v->ignore = 1;
4758
4759 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
4760 reg_iv_info[REGNO (dest_reg)] = v;
4761 }
4762
4763 /* Add the giv to the class of givs computed from one biv. */
4764
4765 bl = reg_biv_class[REGNO (src_reg)];
4766 if (bl)
4767 {
4768 v->next_iv = bl->giv;
4769 bl->giv = v;
4770 /* Don't count DEST_ADDR. This is supposed to count the number of
4771 insns that calculate givs. */
4772 if (type == DEST_REG)
4773 bl->giv_count++;
4774 bl->total_benefit += benefit;
4775 }
4776 else
4777 /* Fatal error, biv missing for this giv? */
4778 abort ();
4779
4780 if (type == DEST_ADDR)
4781 v->replaceable = 1;
4782 else
4783 {
4784 /* The giv can be replaced outright by the reduced register only if all
4785 of the following conditions are true:
4786 - the insn that sets the giv is always executed on any iteration
4787 on which the giv is used at all
4788 (there are two ways to deduce this:
4789 either the insn is executed on every iteration,
4790 or all uses follow that insn in the same basic block),
4791 - the giv is not used outside the loop
4792 - no assignments to the biv occur during the giv's lifetime. */
4793
4794 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
4795 /* Previous line always fails if INSN was moved by loop opt. */
4796 && uid_luid[REGNO_LAST_UID (REGNO (dest_reg))] < INSN_LUID (loop_end)
4797 && (! not_every_iteration
4798 || last_use_this_basic_block (dest_reg, insn)))
4799 {
4800 /* Now check that there are no assignments to the biv within the
4801 giv's lifetime. This requires two separate checks. */
4802
4803 /* Check each biv update, and fail if any are between the first
4804 and last use of the giv.
4805
4806 If this loop contains an inner loop that was unrolled, then
4807 the insn modifying the biv may have been emitted by the loop
4808 unrolling code, and hence does not have a valid luid. Just
4809 mark the biv as not replaceable in this case. It is not very
4810 useful as a biv, because it is used in two different loops.
4811 It is very unlikely that we would be able to optimize the giv
4812 using this biv anyways. */
4813
4814 v->replaceable = 1;
4815 for (b = bl->biv; b; b = b->next_iv)
4816 {
4817 if (INSN_UID (b->insn) >= max_uid_for_loop
4818 || ((uid_luid[INSN_UID (b->insn)]
4819 >= uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))])
4820 && (uid_luid[INSN_UID (b->insn)]
4821 <= uid_luid[REGNO_LAST_UID (REGNO (dest_reg))])))
4822 {
4823 v->replaceable = 0;
4824 v->not_replaceable = 1;
4825 break;
4826 }
4827 }
4828
4829 /* If there are any backwards branches that go from after the
4830 biv update to before it, then this giv is not replaceable. */
4831 if (v->replaceable)
4832 for (b = bl->biv; b; b = b->next_iv)
4833 if (back_branch_in_range_p (b->insn, loop_start, loop_end))
4834 {
4835 v->replaceable = 0;
4836 v->not_replaceable = 1;
4837 break;
4838 }
4839 }
4840 else
4841 {
4842 /* May still be replaceable, we don't have enough info here to
4843 decide. */
4844 v->replaceable = 0;
4845 v->not_replaceable = 0;
4846 }
4847 }
4848
4849 if (loop_dump_stream)
4850 {
4851 if (type == DEST_REG)
4852 fprintf (loop_dump_stream, "Insn %d: giv reg %d",
4853 INSN_UID (insn), REGNO (dest_reg));
4854 else
4855 fprintf (loop_dump_stream, "Insn %d: dest address",
4856 INSN_UID (insn));
4857
4858 fprintf (loop_dump_stream, " src reg %d benefit %d",
4859 REGNO (src_reg), v->benefit);
4860 fprintf (loop_dump_stream, " used %d lifetime %d",
4861 v->times_used, v->lifetime);
4862
4863 if (v->replaceable)
4864 fprintf (loop_dump_stream, " replaceable");
4865
4866 if (GET_CODE (mult_val) == CONST_INT)
4867 {
4868 fprintf (loop_dump_stream, " mult ");
4869 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (mult_val));
4870 }
4871 else
4872 {
4873 fprintf (loop_dump_stream, " mult ");
4874 print_rtl (loop_dump_stream, mult_val);
4875 }
4876
4877 if (GET_CODE (add_val) == CONST_INT)
4878 {
4879 fprintf (loop_dump_stream, " add ");
4880 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (add_val));
4881 }
4882 else
4883 {
4884 fprintf (loop_dump_stream, " add ");
4885 print_rtl (loop_dump_stream, add_val);
4886 }
4887 }
4888
4889 if (loop_dump_stream)
4890 fprintf (loop_dump_stream, "\n");
4891
4892 }
4893
4894
4895 /* All this does is determine whether a giv can be made replaceable because
4896 its final value can be calculated. This code can not be part of record_giv
4897 above, because final_giv_value requires that the number of loop iterations
4898 be known, and that can not be accurately calculated until after all givs
4899 have been identified. */
4900
4901 static void
4902 check_final_value (v, loop_start, loop_end)
4903 struct induction *v;
4904 rtx loop_start, loop_end;
4905 {
4906 struct iv_class *bl;
4907 rtx final_value = 0;
4908
4909 bl = reg_biv_class[REGNO (v->src_reg)];
4910
4911 /* DEST_ADDR givs will never reach here, because they are always marked
4912 replaceable above in record_giv. */
4913
4914 /* The giv can be replaced outright by the reduced register only if all
4915 of the following conditions are true:
4916 - the insn that sets the giv is always executed on any iteration
4917 on which the giv is used at all
4918 (there are two ways to deduce this:
4919 either the insn is executed on every iteration,
4920 or all uses follow that insn in the same basic block),
4921 - its final value can be calculated (this condition is different
4922 than the one above in record_giv)
4923 - no assignments to the biv occur during the giv's lifetime. */
4924
4925 #if 0
4926 /* This is only called now when replaceable is known to be false. */
4927 /* Clear replaceable, so that it won't confuse final_giv_value. */
4928 v->replaceable = 0;
4929 #endif
4930
4931 if ((final_value = final_giv_value (v, loop_start, loop_end))
4932 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
4933 {
4934 int biv_increment_seen = 0;
4935 rtx p = v->insn;
4936 rtx last_giv_use;
4937
4938 v->replaceable = 1;
4939
4940 /* When trying to determine whether or not a biv increment occurs
4941 during the lifetime of the giv, we can ignore uses of the variable
4942 outside the loop because final_value is true. Hence we can not
4943 use regno_last_uid and regno_first_uid as above in record_giv. */
4944
4945 /* Search the loop to determine whether any assignments to the
4946 biv occur during the giv's lifetime. Start with the insn
4947 that sets the giv, and search around the loop until we come
4948 back to that insn again.
4949
4950 Also fail if there is a jump within the giv's lifetime that jumps
4951 to somewhere outside the lifetime but still within the loop. This
4952 catches spaghetti code where the execution order is not linear, and
4953 hence the above test fails. Here we assume that the giv lifetime
4954 does not extend from one iteration of the loop to the next, so as
4955 to make the test easier. Since the lifetime isn't known yet,
4956 this requires two loops. See also record_giv above. */
4957
4958 last_giv_use = v->insn;
4959
4960 while (1)
4961 {
4962 p = NEXT_INSN (p);
4963 if (p == loop_end)
4964 p = NEXT_INSN (loop_start);
4965 if (p == v->insn)
4966 break;
4967
4968 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4969 || GET_CODE (p) == CALL_INSN)
4970 {
4971 if (biv_increment_seen)
4972 {
4973 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4974 {
4975 v->replaceable = 0;
4976 v->not_replaceable = 1;
4977 break;
4978 }
4979 }
4980 else if (reg_set_p (v->src_reg, PATTERN (p)))
4981 biv_increment_seen = 1;
4982 else if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4983 last_giv_use = p;
4984 }
4985 }
4986
4987 /* Now that the lifetime of the giv is known, check for branches
4988 from within the lifetime to outside the lifetime if it is still
4989 replaceable. */
4990
4991 if (v->replaceable)
4992 {
4993 p = v->insn;
4994 while (1)
4995 {
4996 p = NEXT_INSN (p);
4997 if (p == loop_end)
4998 p = NEXT_INSN (loop_start);
4999 if (p == last_giv_use)
5000 break;
5001
5002 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
5003 && LABEL_NAME (JUMP_LABEL (p))
5004 && ((INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop)
5005 || (INSN_UID (v->insn) >= max_uid_for_loop)
5006 || (INSN_UID (last_giv_use) >= max_uid_for_loop)
5007 || (INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (v->insn)
5008 && INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop_start))
5009 || (INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (last_giv_use)
5010 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop_end))))
5011 {
5012 v->replaceable = 0;
5013 v->not_replaceable = 1;
5014
5015 if (loop_dump_stream)
5016 fprintf (loop_dump_stream,
5017 "Found branch outside giv lifetime.\n");
5018
5019 break;
5020 }
5021 }
5022 }
5023
5024 /* If it is replaceable, then save the final value. */
5025 if (v->replaceable)
5026 v->final_value = final_value;
5027 }
5028
5029 if (loop_dump_stream && v->replaceable)
5030 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
5031 INSN_UID (v->insn), REGNO (v->dest_reg));
5032 }
5033 \f
5034 /* Update the status of whether a giv can derive other givs.
5035
5036 We need to do something special if there is or may be an update to the biv
5037 between the time the giv is defined and the time it is used to derive
5038 another giv.
5039
5040 In addition, a giv that is only conditionally set is not allowed to
5041 derive another giv once a label has been passed.
5042
5043 The cases we look at are when a label or an update to a biv is passed. */
5044
5045 static void
5046 update_giv_derive (p)
5047 rtx p;
5048 {
5049 struct iv_class *bl;
5050 struct induction *biv, *giv;
5051 rtx tem;
5052 int dummy;
5053
5054 /* Search all IV classes, then all bivs, and finally all givs.
5055
5056 There are three cases we are concerned with. First we have the situation
5057 of a giv that is only updated conditionally. In that case, it may not
5058 derive any givs after a label is passed.
5059
5060 The second case is when a biv update occurs, or may occur, after the
5061 definition of a giv. For certain biv updates (see below) that are
5062 known to occur between the giv definition and use, we can adjust the
5063 giv definition. For others, or when the biv update is conditional,
5064 we must prevent the giv from deriving any other givs. There are two
5065 sub-cases within this case.
5066
5067 If this is a label, we are concerned with any biv update that is done
5068 conditionally, since it may be done after the giv is defined followed by
5069 a branch here (actually, we need to pass both a jump and a label, but
5070 this extra tracking doesn't seem worth it).
5071
5072 If this is a jump, we are concerned about any biv update that may be
5073 executed multiple times. We are actually only concerned about
5074 backward jumps, but it is probably not worth performing the test
5075 on the jump again here.
5076
5077 If this is a biv update, we must adjust the giv status to show that a
5078 subsequent biv update was performed. If this adjustment cannot be done,
5079 the giv cannot derive further givs. */
5080
5081 for (bl = loop_iv_list; bl; bl = bl->next)
5082 for (biv = bl->biv; biv; biv = biv->next_iv)
5083 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
5084 || biv->insn == p)
5085 {
5086 for (giv = bl->giv; giv; giv = giv->next_iv)
5087 {
5088 /* If cant_derive is already true, there is no point in
5089 checking all of these conditions again. */
5090 if (giv->cant_derive)
5091 continue;
5092
5093 /* If this giv is conditionally set and we have passed a label,
5094 it cannot derive anything. */
5095 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
5096 giv->cant_derive = 1;
5097
5098 /* Skip givs that have mult_val == 0, since
5099 they are really invariants. Also skip those that are
5100 replaceable, since we know their lifetime doesn't contain
5101 any biv update. */
5102 else if (giv->mult_val == const0_rtx || giv->replaceable)
5103 continue;
5104
5105 /* The only way we can allow this giv to derive another
5106 is if this is a biv increment and we can form the product
5107 of biv->add_val and giv->mult_val. In this case, we will
5108 be able to compute a compensation. */
5109 else if (biv->insn == p)
5110 {
5111 tem = 0;
5112
5113 if (biv->mult_val == const1_rtx)
5114 tem = simplify_giv_expr (gen_rtx_MULT (giv->mode,
5115 biv->add_val,
5116 giv->mult_val),
5117 &dummy);
5118
5119 if (tem && giv->derive_adjustment)
5120 tem = simplify_giv_expr (gen_rtx_PLUS (giv->mode, tem,
5121 giv->derive_adjustment),
5122 &dummy);
5123 if (tem)
5124 giv->derive_adjustment = tem;
5125 else
5126 giv->cant_derive = 1;
5127 }
5128 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5129 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
5130 giv->cant_derive = 1;
5131 }
5132 }
5133 }
5134 \f
5135 /* Check whether an insn is an increment legitimate for a basic induction var.
5136 X is the source of insn P, or a part of it.
5137 MODE is the mode in which X should be interpreted.
5138
5139 DEST_REG is the putative biv, also the destination of the insn.
5140 We accept patterns of these forms:
5141 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5142 REG = INVARIANT + REG
5143
5144 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5145 and store the additive term into *INC_VAL.
5146
5147 If X is an assignment of an invariant into DEST_REG, we set
5148 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5149
5150 We also want to detect a BIV when it corresponds to a variable
5151 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5152 of the variable may be a PLUS that adds a SUBREG of that variable to
5153 an invariant and then sign- or zero-extends the result of the PLUS
5154 into the variable.
5155
5156 Most GIVs in such cases will be in the promoted mode, since that is the
5157 probably the natural computation mode (and almost certainly the mode
5158 used for addresses) on the machine. So we view the pseudo-reg containing
5159 the variable as the BIV, as if it were simply incremented.
5160
5161 Note that treating the entire pseudo as a BIV will result in making
5162 simple increments to any GIVs based on it. However, if the variable
5163 overflows in its declared mode but not its promoted mode, the result will
5164 be incorrect. This is acceptable if the variable is signed, since
5165 overflows in such cases are undefined, but not if it is unsigned, since
5166 those overflows are defined. So we only check for SIGN_EXTEND and
5167 not ZERO_EXTEND.
5168
5169 If we cannot find a biv, we return 0. */
5170
5171 static int
5172 basic_induction_var (x, mode, dest_reg, p, inc_val, mult_val)
5173 register rtx x;
5174 enum machine_mode mode;
5175 rtx p;
5176 rtx dest_reg;
5177 rtx *inc_val;
5178 rtx *mult_val;
5179 {
5180 register enum rtx_code code;
5181 rtx arg;
5182 rtx insn, set = 0;
5183
5184 code = GET_CODE (x);
5185 switch (code)
5186 {
5187 case PLUS:
5188 if (XEXP (x, 0) == dest_reg
5189 || (GET_CODE (XEXP (x, 0)) == SUBREG
5190 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5191 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
5192 arg = XEXP (x, 1);
5193 else if (XEXP (x, 1) == dest_reg
5194 || (GET_CODE (XEXP (x, 1)) == SUBREG
5195 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5196 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
5197 arg = XEXP (x, 0);
5198 else
5199 return 0;
5200
5201 if (invariant_p (arg) != 1)
5202 return 0;
5203
5204 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
5205 *mult_val = const1_rtx;
5206 return 1;
5207
5208 case SUBREG:
5209 /* If this is a SUBREG for a promoted variable, check the inner
5210 value. */
5211 if (SUBREG_PROMOTED_VAR_P (x))
5212 return basic_induction_var (SUBREG_REG (x), GET_MODE (SUBREG_REG (x)),
5213 dest_reg, p, inc_val, mult_val);
5214 return 0;
5215
5216 case REG:
5217 /* If this register is assigned in the previous insn, look at its
5218 source, but don't go outside the loop or past a label. */
5219
5220 for (insn = PREV_INSN (p);
5221 (insn && GET_CODE (insn) == NOTE
5222 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5223 insn = PREV_INSN (insn))
5224 ;
5225
5226 if (insn)
5227 set = single_set (insn);
5228
5229 if (set != 0
5230 && (SET_DEST (set) == x
5231 || (GET_CODE (SET_DEST (set)) == SUBREG
5232 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
5233 <= UNITS_PER_WORD)
5234 && SUBREG_REG (SET_DEST (set)) == x)))
5235 return basic_induction_var (SET_SRC (set),
5236 (GET_MODE (SET_SRC (set)) == VOIDmode
5237 ? GET_MODE (x)
5238 : GET_MODE (SET_SRC (set))),
5239 dest_reg, insn,
5240 inc_val, mult_val);
5241 /* ... fall through ... */
5242
5243 /* Can accept constant setting of biv only when inside inner most loop.
5244 Otherwise, a biv of an inner loop may be incorrectly recognized
5245 as a biv of the outer loop,
5246 causing code to be moved INTO the inner loop. */
5247 case MEM:
5248 if (invariant_p (x) != 1)
5249 return 0;
5250 case CONST_INT:
5251 case SYMBOL_REF:
5252 case CONST:
5253 if (loops_enclosed == 1)
5254 {
5255 /* Possible bug here? Perhaps we don't know the mode of X. */
5256 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
5257 *mult_val = const0_rtx;
5258 return 1;
5259 }
5260 else
5261 return 0;
5262
5263 case SIGN_EXTEND:
5264 return basic_induction_var (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5265 dest_reg, p, inc_val, mult_val);
5266 case ASHIFTRT:
5267 /* Similar, since this can be a sign extension. */
5268 for (insn = PREV_INSN (p);
5269 (insn && GET_CODE (insn) == NOTE
5270 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5271 insn = PREV_INSN (insn))
5272 ;
5273
5274 if (insn)
5275 set = single_set (insn);
5276
5277 if (set && SET_DEST (set) == XEXP (x, 0)
5278 && GET_CODE (XEXP (x, 1)) == CONST_INT
5279 && INTVAL (XEXP (x, 1)) >= 0
5280 && GET_CODE (SET_SRC (set)) == ASHIFT
5281 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
5282 return basic_induction_var (XEXP (SET_SRC (set), 0),
5283 GET_MODE (XEXP (x, 0)),
5284 dest_reg, insn, inc_val, mult_val);
5285 return 0;
5286
5287 default:
5288 return 0;
5289 }
5290 }
5291 \f
5292 /* A general induction variable (giv) is any quantity that is a linear
5293 function of a basic induction variable,
5294 i.e. giv = biv * mult_val + add_val.
5295 The coefficients can be any loop invariant quantity.
5296 A giv need not be computed directly from the biv;
5297 it can be computed by way of other givs. */
5298
5299 /* Determine whether X computes a giv.
5300 If it does, return a nonzero value
5301 which is the benefit from eliminating the computation of X;
5302 set *SRC_REG to the register of the biv that it is computed from;
5303 set *ADD_VAL and *MULT_VAL to the coefficients,
5304 such that the value of X is biv * mult + add; */
5305
5306 static int
5307 general_induction_var (x, src_reg, add_val, mult_val)
5308 rtx x;
5309 rtx *src_reg;
5310 rtx *add_val;
5311 rtx *mult_val;
5312 {
5313 rtx orig_x = x;
5314 int benefit = 0;
5315 char *storage;
5316
5317 /* If this is an invariant, forget it, it isn't a giv. */
5318 if (invariant_p (x) == 1)
5319 return 0;
5320
5321 /* See if the expression could be a giv and get its form.
5322 Mark our place on the obstack in case we don't find a giv. */
5323 storage = (char *) oballoc (0);
5324 x = simplify_giv_expr (x, &benefit);
5325 if (x == 0)
5326 {
5327 obfree (storage);
5328 return 0;
5329 }
5330
5331 switch (GET_CODE (x))
5332 {
5333 case USE:
5334 case CONST_INT:
5335 /* Since this is now an invariant and wasn't before, it must be a giv
5336 with MULT_VAL == 0. It doesn't matter which BIV we associate this
5337 with. */
5338 *src_reg = loop_iv_list->biv->dest_reg;
5339 *mult_val = const0_rtx;
5340 *add_val = x;
5341 break;
5342
5343 case REG:
5344 /* This is equivalent to a BIV. */
5345 *src_reg = x;
5346 *mult_val = const1_rtx;
5347 *add_val = const0_rtx;
5348 break;
5349
5350 case PLUS:
5351 /* Either (plus (biv) (invar)) or
5352 (plus (mult (biv) (invar_1)) (invar_2)). */
5353 if (GET_CODE (XEXP (x, 0)) == MULT)
5354 {
5355 *src_reg = XEXP (XEXP (x, 0), 0);
5356 *mult_val = XEXP (XEXP (x, 0), 1);
5357 }
5358 else
5359 {
5360 *src_reg = XEXP (x, 0);
5361 *mult_val = const1_rtx;
5362 }
5363 *add_val = XEXP (x, 1);
5364 break;
5365
5366 case MULT:
5367 /* ADD_VAL is zero. */
5368 *src_reg = XEXP (x, 0);
5369 *mult_val = XEXP (x, 1);
5370 *add_val = const0_rtx;
5371 break;
5372
5373 default:
5374 abort ();
5375 }
5376
5377 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
5378 unless they are CONST_INT). */
5379 if (GET_CODE (*add_val) == USE)
5380 *add_val = XEXP (*add_val, 0);
5381 if (GET_CODE (*mult_val) == USE)
5382 *mult_val = XEXP (*mult_val, 0);
5383
5384 benefit += rtx_cost (orig_x, SET);
5385
5386 /* Always return some benefit if this is a giv so it will be detected
5387 as such. This allows elimination of bivs that might otherwise
5388 not be eliminated. */
5389 return benefit == 0 ? 1 : benefit;
5390 }
5391 \f
5392 /* Given an expression, X, try to form it as a linear function of a biv.
5393 We will canonicalize it to be of the form
5394 (plus (mult (BIV) (invar_1))
5395 (invar_2))
5396 with possible degeneracies.
5397
5398 The invariant expressions must each be of a form that can be used as a
5399 machine operand. We surround then with a USE rtx (a hack, but localized
5400 and certainly unambiguous!) if not a CONST_INT for simplicity in this
5401 routine; it is the caller's responsibility to strip them.
5402
5403 If no such canonicalization is possible (i.e., two biv's are used or an
5404 expression that is neither invariant nor a biv or giv), this routine
5405 returns 0.
5406
5407 For a non-zero return, the result will have a code of CONST_INT, USE,
5408 REG (for a BIV), PLUS, or MULT. No other codes will occur.
5409
5410 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
5411
5412 static rtx
5413 simplify_giv_expr (x, benefit)
5414 rtx x;
5415 int *benefit;
5416 {
5417 enum machine_mode mode = GET_MODE (x);
5418 rtx arg0, arg1;
5419 rtx tem;
5420
5421 /* If this is not an integer mode, or if we cannot do arithmetic in this
5422 mode, this can't be a giv. */
5423 if (mode != VOIDmode
5424 && (GET_MODE_CLASS (mode) != MODE_INT
5425 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
5426 return 0;
5427
5428 switch (GET_CODE (x))
5429 {
5430 case PLUS:
5431 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5432 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5433 if (arg0 == 0 || arg1 == 0)
5434 return 0;
5435
5436 /* Put constant last, CONST_INT last if both constant. */
5437 if ((GET_CODE (arg0) == USE
5438 || GET_CODE (arg0) == CONST_INT)
5439 && GET_CODE (arg1) != CONST_INT)
5440 tem = arg0, arg0 = arg1, arg1 = tem;
5441
5442 /* Handle addition of zero, then addition of an invariant. */
5443 if (arg1 == const0_rtx)
5444 return arg0;
5445 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
5446 switch (GET_CODE (arg0))
5447 {
5448 case CONST_INT:
5449 case USE:
5450 /* Both invariant. Only valid if sum is machine operand.
5451 First strip off possible USE on the operands. */
5452 if (GET_CODE (arg0) == USE)
5453 arg0 = XEXP (arg0, 0);
5454
5455 if (GET_CODE (arg1) == USE)
5456 arg1 = XEXP (arg1, 0);
5457
5458 tem = 0;
5459 if (CONSTANT_P (arg0) && GET_CODE (arg1) == CONST_INT)
5460 {
5461 tem = plus_constant (arg0, INTVAL (arg1));
5462 if (GET_CODE (tem) != CONST_INT)
5463 tem = gen_rtx_USE (mode, tem);
5464 }
5465 else
5466 {
5467 /* Adding two invariants must result in an invariant,
5468 so enclose addition operation inside a USE and
5469 return it. */
5470 tem = gen_rtx_USE (mode, gen_rtx_PLUS (mode, arg0, arg1));
5471 }
5472
5473 return tem;
5474
5475 case REG:
5476 case MULT:
5477 /* biv + invar or mult + invar. Return sum. */
5478 return gen_rtx_PLUS (mode, arg0, arg1);
5479
5480 case PLUS:
5481 /* (a + invar_1) + invar_2. Associate. */
5482 return simplify_giv_expr (gen_rtx_PLUS (mode,
5483 XEXP (arg0, 0),
5484 gen_rtx_PLUS (mode,
5485 XEXP (arg0, 1), arg1)),
5486 benefit);
5487
5488 default:
5489 abort ();
5490 }
5491
5492 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
5493 MULT to reduce cases. */
5494 if (GET_CODE (arg0) == REG)
5495 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
5496 if (GET_CODE (arg1) == REG)
5497 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
5498
5499 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
5500 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
5501 Recurse to associate the second PLUS. */
5502 if (GET_CODE (arg1) == MULT)
5503 tem = arg0, arg0 = arg1, arg1 = tem;
5504
5505 if (GET_CODE (arg1) == PLUS)
5506 return simplify_giv_expr (gen_rtx_PLUS (mode,
5507 gen_rtx_PLUS (mode, arg0,
5508 XEXP (arg1, 0)),
5509 XEXP (arg1, 1)),
5510 benefit);
5511
5512 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
5513 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
5514 abort ();
5515
5516 if (XEXP (arg0, 0) != XEXP (arg1, 0))
5517 return 0;
5518
5519 return simplify_giv_expr (gen_rtx_MULT (mode,
5520 XEXP (arg0, 0),
5521 gen_rtx_PLUS (mode,
5522 XEXP (arg0, 1),
5523 XEXP (arg1, 1))),
5524 benefit);
5525
5526 case MINUS:
5527 /* Handle "a - b" as "a + b * (-1)". */
5528 return simplify_giv_expr (gen_rtx_PLUS (mode,
5529 XEXP (x, 0),
5530 gen_rtx_MULT (mode, XEXP (x, 1),
5531 constm1_rtx)),
5532 benefit);
5533
5534 case MULT:
5535 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5536 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5537 if (arg0 == 0 || arg1 == 0)
5538 return 0;
5539
5540 /* Put constant last, CONST_INT last if both constant. */
5541 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
5542 && GET_CODE (arg1) != CONST_INT)
5543 tem = arg0, arg0 = arg1, arg1 = tem;
5544
5545 /* If second argument is not now constant, not giv. */
5546 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
5547 return 0;
5548
5549 /* Handle multiply by 0 or 1. */
5550 if (arg1 == const0_rtx)
5551 return const0_rtx;
5552
5553 else if (arg1 == const1_rtx)
5554 return arg0;
5555
5556 switch (GET_CODE (arg0))
5557 {
5558 case REG:
5559 /* biv * invar. Done. */
5560 return gen_rtx_MULT (mode, arg0, arg1);
5561
5562 case CONST_INT:
5563 /* Product of two constants. */
5564 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
5565
5566 case USE:
5567 /* invar * invar. Not giv. */
5568 return 0;
5569
5570 case MULT:
5571 /* (a * invar_1) * invar_2. Associate. */
5572 return simplify_giv_expr (gen_rtx_MULT (mode, XEXP (arg0, 0),
5573 gen_rtx_MULT (mode,
5574 XEXP (arg0, 1),
5575 arg1)),
5576 benefit);
5577
5578 case PLUS:
5579 /* (a + invar_1) * invar_2. Distribute. */
5580 return simplify_giv_expr (gen_rtx_PLUS (mode,
5581 gen_rtx_MULT (mode,
5582 XEXP (arg0, 0),
5583 arg1),
5584 gen_rtx_MULT (mode,
5585 XEXP (arg0, 1),
5586 arg1)),
5587 benefit);
5588
5589 default:
5590 abort ();
5591 }
5592
5593 case ASHIFT:
5594 /* Shift by constant is multiply by power of two. */
5595 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5596 return 0;
5597
5598 return simplify_giv_expr (gen_rtx_MULT (mode,
5599 XEXP (x, 0),
5600 GEN_INT ((HOST_WIDE_INT) 1
5601 << INTVAL (XEXP (x, 1)))),
5602 benefit);
5603
5604 case NEG:
5605 /* "-a" is "a * (-1)" */
5606 return simplify_giv_expr (gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
5607 benefit);
5608
5609 case NOT:
5610 /* "~a" is "-a - 1". Silly, but easy. */
5611 return simplify_giv_expr (gen_rtx_MINUS (mode,
5612 gen_rtx_NEG (mode, XEXP (x, 0)),
5613 const1_rtx),
5614 benefit);
5615
5616 case USE:
5617 /* Already in proper form for invariant. */
5618 return x;
5619
5620 case REG:
5621 /* If this is a new register, we can't deal with it. */
5622 if (REGNO (x) >= max_reg_before_loop)
5623 return 0;
5624
5625 /* Check for biv or giv. */
5626 switch (reg_iv_type[REGNO (x)])
5627 {
5628 case BASIC_INDUCT:
5629 return x;
5630 case GENERAL_INDUCT:
5631 {
5632 struct induction *v = reg_iv_info[REGNO (x)];
5633
5634 /* Form expression from giv and add benefit. Ensure this giv
5635 can derive another and subtract any needed adjustment if so. */
5636 *benefit += v->benefit;
5637 if (v->cant_derive)
5638 return 0;
5639
5640 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode, v->src_reg,
5641 v->mult_val),
5642 v->add_val);
5643 if (v->derive_adjustment)
5644 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
5645 return simplify_giv_expr (tem, benefit);
5646 }
5647
5648 default:
5649 break;
5650 }
5651
5652 /* Fall through to general case. */
5653 default:
5654 /* If invariant, return as USE (unless CONST_INT).
5655 Otherwise, not giv. */
5656 if (GET_CODE (x) == USE)
5657 x = XEXP (x, 0);
5658
5659 if (invariant_p (x) == 1)
5660 {
5661 if (GET_CODE (x) == CONST_INT)
5662 return x;
5663 else
5664 return gen_rtx_USE (mode, x);
5665 }
5666 else
5667 return 0;
5668 }
5669 }
5670 \f
5671 /* Help detect a giv that is calculated by several consecutive insns;
5672 for example,
5673 giv = biv * M
5674 giv = giv + A
5675 The caller has already identified the first insn P as having a giv as dest;
5676 we check that all other insns that set the same register follow
5677 immediately after P, that they alter nothing else,
5678 and that the result of the last is still a giv.
5679
5680 The value is 0 if the reg set in P is not really a giv.
5681 Otherwise, the value is the amount gained by eliminating
5682 all the consecutive insns that compute the value.
5683
5684 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
5685 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
5686
5687 The coefficients of the ultimate giv value are stored in
5688 *MULT_VAL and *ADD_VAL. */
5689
5690 static int
5691 consec_sets_giv (first_benefit, p, src_reg, dest_reg,
5692 add_val, mult_val)
5693 int first_benefit;
5694 rtx p;
5695 rtx src_reg;
5696 rtx dest_reg;
5697 rtx *add_val;
5698 rtx *mult_val;
5699 {
5700 int count;
5701 enum rtx_code code;
5702 int benefit;
5703 rtx temp;
5704 rtx set;
5705
5706 /* Indicate that this is a giv so that we can update the value produced in
5707 each insn of the multi-insn sequence.
5708
5709 This induction structure will be used only by the call to
5710 general_induction_var below, so we can allocate it on our stack.
5711 If this is a giv, our caller will replace the induct var entry with
5712 a new induction structure. */
5713 struct induction *v
5714 = (struct induction *) alloca (sizeof (struct induction));
5715 v->src_reg = src_reg;
5716 v->mult_val = *mult_val;
5717 v->add_val = *add_val;
5718 v->benefit = first_benefit;
5719 v->cant_derive = 0;
5720 v->derive_adjustment = 0;
5721
5722 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
5723 reg_iv_info[REGNO (dest_reg)] = v;
5724
5725 count = n_times_set[REGNO (dest_reg)] - 1;
5726
5727 while (count > 0)
5728 {
5729 p = NEXT_INSN (p);
5730 code = GET_CODE (p);
5731
5732 /* If libcall, skip to end of call sequence. */
5733 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
5734 p = XEXP (temp, 0);
5735
5736 if (code == INSN
5737 && (set = single_set (p))
5738 && GET_CODE (SET_DEST (set)) == REG
5739 && SET_DEST (set) == dest_reg
5740 && ((benefit = general_induction_var (SET_SRC (set), &src_reg,
5741 add_val, mult_val))
5742 /* Giv created by equivalent expression. */
5743 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
5744 && (benefit = general_induction_var (XEXP (temp, 0), &src_reg,
5745 add_val, mult_val))))
5746 && src_reg == v->src_reg)
5747 {
5748 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5749 benefit += libcall_benefit (p);
5750
5751 count--;
5752 v->mult_val = *mult_val;
5753 v->add_val = *add_val;
5754 v->benefit = benefit;
5755 }
5756 else if (code != NOTE)
5757 {
5758 /* Allow insns that set something other than this giv to a
5759 constant. Such insns are needed on machines which cannot
5760 include long constants and should not disqualify a giv. */
5761 if (code == INSN
5762 && (set = single_set (p))
5763 && SET_DEST (set) != dest_reg
5764 && CONSTANT_P (SET_SRC (set)))
5765 continue;
5766
5767 reg_iv_type[REGNO (dest_reg)] = UNKNOWN_INDUCT;
5768 return 0;
5769 }
5770 }
5771
5772 return v->benefit;
5773 }
5774 \f
5775 /* Return an rtx, if any, that expresses giv G2 as a function of the register
5776 represented by G1. If no such expression can be found, or it is clear that
5777 it cannot possibly be a valid address, 0 is returned.
5778
5779 To perform the computation, we note that
5780 G1 = a * v + b and
5781 G2 = c * v + d
5782 where `v' is the biv.
5783
5784 So G2 = (c/a) * G1 + (d - b*c/a) */
5785
5786 #ifdef ADDRESS_COST
5787 static rtx
5788 express_from (g1, g2)
5789 struct induction *g1, *g2;
5790 {
5791 rtx mult, add;
5792
5793 /* The value that G1 will be multiplied by must be a constant integer. Also,
5794 the only chance we have of getting a valid address is if b*c/a (see above
5795 for notation) is also an integer. */
5796 if (GET_CODE (g1->mult_val) != CONST_INT
5797 || GET_CODE (g2->mult_val) != CONST_INT
5798 || GET_CODE (g1->add_val) != CONST_INT
5799 || g1->mult_val == const0_rtx
5800 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
5801 return 0;
5802
5803 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
5804 add = plus_constant (g2->add_val, - INTVAL (g1->add_val) * INTVAL (mult));
5805
5806 /* Form simplified final result. */
5807 if (mult == const0_rtx)
5808 return add;
5809 else if (mult == const1_rtx)
5810 mult = g1->dest_reg;
5811 else
5812 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
5813
5814 if (add == const0_rtx)
5815 return mult;
5816 else
5817 return gen_rtx_PLUS (g2->mode, mult, add);
5818 }
5819 #endif
5820 \f
5821 /* Return 1 if giv G2 can be combined with G1. This means that G2 can use
5822 (either directly or via an address expression) a register used to represent
5823 G1. Set g2->new_reg to a represtation of G1 (normally just
5824 g1->dest_reg). */
5825
5826 static int
5827 combine_givs_p (g1, g2)
5828 struct induction *g1, *g2;
5829 {
5830 rtx tem;
5831
5832 /* If these givs are identical, they can be combined. */
5833 if (rtx_equal_p (g1->mult_val, g2->mult_val)
5834 && rtx_equal_p (g1->add_val, g2->add_val))
5835 {
5836 g2->new_reg = g1->dest_reg;
5837 return 1;
5838 }
5839
5840 #ifdef ADDRESS_COST
5841 /* If G2 can be expressed as a function of G1 and that function is valid
5842 as an address and no more expensive than using a register for G2,
5843 the expression of G2 in terms of G1 can be used. */
5844 if (g2->giv_type == DEST_ADDR
5845 && (tem = express_from (g1, g2)) != 0
5846 && memory_address_p (g2->mem_mode, tem)
5847 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location))
5848 {
5849 g2->new_reg = tem;
5850 return 1;
5851 }
5852 #endif
5853
5854 return 0;
5855 }
5856 \f
5857 #ifdef GIV_SORT_CRITERION
5858 /* Compare two givs and sort the most desirable one for combinations first.
5859 This is used only in one qsort call below. */
5860
5861 static int
5862 giv_sort (x, y)
5863 struct induction **x, **y;
5864 {
5865 GIV_SORT_CRITERION (*x, *y);
5866
5867 return 0;
5868 }
5869 #endif
5870
5871 /* Check all pairs of givs for iv_class BL and see if any can be combined with
5872 any other. If so, point SAME to the giv combined with and set NEW_REG to
5873 be an expression (in terms of the other giv's DEST_REG) equivalent to the
5874 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
5875
5876 static void
5877 combine_givs (bl)
5878 struct iv_class *bl;
5879 {
5880 struct induction *g1, *g2, **giv_array;
5881 int i, j, giv_count, pass;
5882
5883 /* Count givs, because bl->giv_count is incorrect here. */
5884 giv_count = 0;
5885 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5886 giv_count++;
5887
5888 giv_array
5889 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
5890 i = 0;
5891 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5892 giv_array[i++] = g1;
5893
5894 #ifdef GIV_SORT_CRITERION
5895 /* Sort the givs if GIV_SORT_CRITERION is defined.
5896 This is usually defined for processors which lack
5897 negative register offsets so more givs may be combined. */
5898
5899 if (loop_dump_stream)
5900 fprintf (loop_dump_stream, "%d givs counted, sorting...\n", giv_count);
5901
5902 qsort (giv_array, giv_count, sizeof (struct induction *), giv_sort);
5903 #endif
5904
5905 for (i = 0; i < giv_count; i++)
5906 {
5907 g1 = giv_array[i];
5908 for (pass = 0; pass <= 1; pass++)
5909 for (j = 0; j < giv_count; j++)
5910 {
5911 g2 = giv_array[j];
5912 if (g1 != g2
5913 /* First try to combine with replaceable givs, then all givs. */
5914 && (g1->replaceable || pass == 1)
5915 /* If either has already been combined or is to be ignored, can't
5916 combine. */
5917 && ! g1->ignore && ! g2->ignore && ! g1->same && ! g2->same
5918 /* If something has been based on G2, G2 cannot itself be based
5919 on something else. */
5920 && ! g2->combined_with
5921 && combine_givs_p (g1, g2))
5922 {
5923 /* g2->new_reg set by `combine_givs_p' */
5924 g2->same = g1;
5925 g1->combined_with = 1;
5926
5927 /* If one of these givs is a DEST_REG that was only used
5928 once, by the other giv, this is actually a single use.
5929 The DEST_REG has the correct cost, while the other giv
5930 counts the REG use too often. */
5931 if (g2->giv_type == DEST_REG
5932 && n_times_used[REGNO (g2->dest_reg)] == 1
5933 && reg_mentioned_p (g2->dest_reg, PATTERN (g1->insn)))
5934 g1->benefit = g2->benefit;
5935 else if (g1->giv_type != DEST_REG
5936 || n_times_used[REGNO (g1->dest_reg)] != 1
5937 || ! reg_mentioned_p (g1->dest_reg,
5938 PATTERN (g2->insn)))
5939 {
5940 g1->benefit += g2->benefit;
5941 g1->times_used += g2->times_used;
5942 }
5943 /* ??? The new final_[bg]iv_value code does a much better job
5944 of finding replaceable giv's, and hence this code may no
5945 longer be necessary. */
5946 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
5947 g1->benefit -= copy_cost;
5948 g1->lifetime += g2->lifetime;
5949
5950 if (loop_dump_stream)
5951 fprintf (loop_dump_stream, "giv at %d combined with giv at %d\n",
5952 INSN_UID (g2->insn), INSN_UID (g1->insn));
5953 }
5954 }
5955 }
5956 }
5957 \f
5958 /* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
5959
5960 void
5961 emit_iv_add_mult (b, m, a, reg, insert_before)
5962 rtx b; /* initial value of basic induction variable */
5963 rtx m; /* multiplicative constant */
5964 rtx a; /* additive constant */
5965 rtx reg; /* destination register */
5966 rtx insert_before;
5967 {
5968 rtx seq;
5969 rtx result;
5970
5971 /* Prevent unexpected sharing of these rtx. */
5972 a = copy_rtx (a);
5973 b = copy_rtx (b);
5974
5975 /* Increase the lifetime of any invariants moved further in code. */
5976 update_reg_last_use (a, insert_before);
5977 update_reg_last_use (b, insert_before);
5978 update_reg_last_use (m, insert_before);
5979
5980 start_sequence ();
5981 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 0);
5982 if (reg != result)
5983 emit_move_insn (reg, result);
5984 seq = gen_sequence ();
5985 end_sequence ();
5986
5987 emit_insn_before (seq, insert_before);
5988
5989 record_base_value (REGNO (reg), b, 0);
5990 }
5991 \f
5992 /* Test whether A * B can be computed without
5993 an actual multiply insn. Value is 1 if so. */
5994
5995 static int
5996 product_cheap_p (a, b)
5997 rtx a;
5998 rtx b;
5999 {
6000 int i;
6001 rtx tmp;
6002 struct obstack *old_rtl_obstack = rtl_obstack;
6003 char *storage = (char *) obstack_alloc (&temp_obstack, 0);
6004 int win = 1;
6005
6006 /* If only one is constant, make it B. */
6007 if (GET_CODE (a) == CONST_INT)
6008 tmp = a, a = b, b = tmp;
6009
6010 /* If first constant, both constant, so don't need multiply. */
6011 if (GET_CODE (a) == CONST_INT)
6012 return 1;
6013
6014 /* If second not constant, neither is constant, so would need multiply. */
6015 if (GET_CODE (b) != CONST_INT)
6016 return 0;
6017
6018 /* One operand is constant, so might not need multiply insn. Generate the
6019 code for the multiply and see if a call or multiply, or long sequence
6020 of insns is generated. */
6021
6022 rtl_obstack = &temp_obstack;
6023 start_sequence ();
6024 expand_mult (GET_MODE (a), a, b, NULL_RTX, 0);
6025 tmp = gen_sequence ();
6026 end_sequence ();
6027
6028 if (GET_CODE (tmp) == SEQUENCE)
6029 {
6030 if (XVEC (tmp, 0) == 0)
6031 win = 1;
6032 else if (XVECLEN (tmp, 0) > 3)
6033 win = 0;
6034 else
6035 for (i = 0; i < XVECLEN (tmp, 0); i++)
6036 {
6037 rtx insn = XVECEXP (tmp, 0, i);
6038
6039 if (GET_CODE (insn) != INSN
6040 || (GET_CODE (PATTERN (insn)) == SET
6041 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
6042 || (GET_CODE (PATTERN (insn)) == PARALLEL
6043 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
6044 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
6045 {
6046 win = 0;
6047 break;
6048 }
6049 }
6050 }
6051 else if (GET_CODE (tmp) == SET
6052 && GET_CODE (SET_SRC (tmp)) == MULT)
6053 win = 0;
6054 else if (GET_CODE (tmp) == PARALLEL
6055 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
6056 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
6057 win = 0;
6058
6059 /* Free any storage we obtained in generating this multiply and restore rtl
6060 allocation to its normal obstack. */
6061 obstack_free (&temp_obstack, storage);
6062 rtl_obstack = old_rtl_obstack;
6063
6064 return win;
6065 }
6066 \f
6067 /* Check to see if loop can be terminated by a "decrement and branch until
6068 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
6069 Also try reversing an increment loop to a decrement loop
6070 to see if the optimization can be performed.
6071 Value is nonzero if optimization was performed. */
6072
6073 /* This is useful even if the architecture doesn't have such an insn,
6074 because it might change a loops which increments from 0 to n to a loop
6075 which decrements from n to 0. A loop that decrements to zero is usually
6076 faster than one that increments from zero. */
6077
6078 /* ??? This could be rewritten to use some of the loop unrolling procedures,
6079 such as approx_final_value, biv_total_increment, loop_iterations, and
6080 final_[bg]iv_value. */
6081
6082 static int
6083 check_dbra_loop (loop_end, insn_count, loop_start)
6084 rtx loop_end;
6085 int insn_count;
6086 rtx loop_start;
6087 {
6088 struct iv_class *bl;
6089 rtx reg;
6090 rtx jump_label;
6091 rtx final_value;
6092 rtx start_value;
6093 rtx new_add_val;
6094 rtx comparison;
6095 rtx before_comparison;
6096 rtx p;
6097
6098 /* If last insn is a conditional branch, and the insn before tests a
6099 register value, try to optimize it. Otherwise, we can't do anything. */
6100
6101 comparison = get_condition_for_loop (PREV_INSN (loop_end));
6102 if (comparison == 0)
6103 return 0;
6104
6105 /* Check all of the bivs to see if the compare uses one of them.
6106 Skip biv's set more than once because we can't guarantee that
6107 it will be zero on the last iteration. Also skip if the biv is
6108 used between its update and the test insn. */
6109
6110 for (bl = loop_iv_list; bl; bl = bl->next)
6111 {
6112 if (bl->biv_count == 1
6113 && bl->biv->dest_reg == XEXP (comparison, 0)
6114 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
6115 PREV_INSN (PREV_INSN (loop_end))))
6116 break;
6117 }
6118
6119 if (! bl)
6120 return 0;
6121
6122 /* Look for the case where the basic induction variable is always
6123 nonnegative, and equals zero on the last iteration.
6124 In this case, add a reg_note REG_NONNEG, which allows the
6125 m68k DBRA instruction to be used. */
6126
6127 if (((GET_CODE (comparison) == GT
6128 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6129 && INTVAL (XEXP (comparison, 1)) == -1)
6130 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
6131 && GET_CODE (bl->biv->add_val) == CONST_INT
6132 && INTVAL (bl->biv->add_val) < 0)
6133 {
6134 /* Initial value must be greater than 0,
6135 init_val % -dec_value == 0 to ensure that it equals zero on
6136 the last iteration */
6137
6138 if (GET_CODE (bl->initial_value) == CONST_INT
6139 && INTVAL (bl->initial_value) > 0
6140 && (INTVAL (bl->initial_value)
6141 % (-INTVAL (bl->biv->add_val))) == 0)
6142 {
6143 /* register always nonnegative, add REG_NOTE to branch */
6144 REG_NOTES (PREV_INSN (loop_end))
6145 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
6146 REG_NOTES (PREV_INSN (loop_end)));
6147 bl->nonneg = 1;
6148
6149 return 1;
6150 }
6151
6152 /* If the decrement is 1 and the value was tested as >= 0 before
6153 the loop, then we can safely optimize. */
6154 for (p = loop_start; p; p = PREV_INSN (p))
6155 {
6156 if (GET_CODE (p) == CODE_LABEL)
6157 break;
6158 if (GET_CODE (p) != JUMP_INSN)
6159 continue;
6160
6161 before_comparison = get_condition_for_loop (p);
6162 if (before_comparison
6163 && XEXP (before_comparison, 0) == bl->biv->dest_reg
6164 && GET_CODE (before_comparison) == LT
6165 && XEXP (before_comparison, 1) == const0_rtx
6166 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
6167 && INTVAL (bl->biv->add_val) == -1)
6168 {
6169 REG_NOTES (PREV_INSN (loop_end))
6170 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
6171 REG_NOTES (PREV_INSN (loop_end)));
6172 bl->nonneg = 1;
6173
6174 return 1;
6175 }
6176 }
6177 }
6178 else if (num_mem_sets <= 1)
6179 {
6180 /* Try to change inc to dec, so can apply above optimization. */
6181 /* Can do this if:
6182 all registers modified are induction variables or invariant,
6183 all memory references have non-overlapping addresses
6184 (obviously true if only one write)
6185 allow 2 insns for the compare/jump at the end of the loop. */
6186 /* Also, we must avoid any instructions which use both the reversed
6187 biv and another biv. Such instructions will fail if the loop is
6188 reversed. We meet this condition by requiring that either
6189 no_use_except_counting is true, or else that there is only
6190 one biv. */
6191 int num_nonfixed_reads = 0;
6192 /* 1 if the iteration var is used only to count iterations. */
6193 int no_use_except_counting = 0;
6194 /* 1 if the loop has no memory store, or it has a single memory store
6195 which is reversible. */
6196 int reversible_mem_store = 1;
6197
6198 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6199 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6200 num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
6201
6202 if (bl->giv_count == 0
6203 && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
6204 {
6205 rtx bivreg = regno_reg_rtx[bl->regno];
6206
6207 /* If there are no givs for this biv, and the only exit is the
6208 fall through at the end of the the loop, then
6209 see if perhaps there are no uses except to count. */
6210 no_use_except_counting = 1;
6211 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6212 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6213 {
6214 rtx set = single_set (p);
6215
6216 if (set && GET_CODE (SET_DEST (set)) == REG
6217 && REGNO (SET_DEST (set)) == bl->regno)
6218 /* An insn that sets the biv is okay. */
6219 ;
6220 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
6221 || p == prev_nonnote_insn (loop_end))
6222 /* Don't bother about the end test. */
6223 ;
6224 else if (reg_mentioned_p (bivreg, PATTERN (p)))
6225 /* Any other use of the biv is no good. */
6226 {
6227 no_use_except_counting = 0;
6228 break;
6229 }
6230 }
6231 }
6232
6233 /* If the loop has a single store, and the destination address is
6234 invariant, then we can't reverse the loop, because this address
6235 might then have the wrong value at loop exit.
6236 This would work if the source was invariant also, however, in that
6237 case, the insn should have been moved out of the loop. */
6238
6239 if (num_mem_sets == 1)
6240 reversible_mem_store
6241 = (! unknown_address_altered
6242 && ! invariant_p (XEXP (loop_store_mems[0], 0)));
6243
6244 /* This code only acts for innermost loops. Also it simplifies
6245 the memory address check by only reversing loops with
6246 zero or one memory access.
6247 Two memory accesses could involve parts of the same array,
6248 and that can't be reversed. */
6249
6250 if (num_nonfixed_reads <= 1
6251 && !loop_has_call
6252 && !loop_has_volatile
6253 && reversible_mem_store
6254 && (no_use_except_counting
6255 || ((bl->giv_count + bl->biv_count + num_mem_sets
6256 + num_movables + 2 == insn_count)
6257 && (bl == loop_iv_list && bl->next == 0))))
6258 {
6259 rtx tem;
6260
6261 /* Loop can be reversed. */
6262 if (loop_dump_stream)
6263 fprintf (loop_dump_stream, "Can reverse loop\n");
6264
6265 /* Now check other conditions:
6266
6267 The increment must be a constant, as must the initial value,
6268 and the comparison code must be LT.
6269
6270 This test can probably be improved since +/- 1 in the constant
6271 can be obtained by changing LT to LE and vice versa; this is
6272 confusing. */
6273
6274 if (comparison
6275 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6276 /* LE gets turned into LT */
6277 && GET_CODE (comparison) == LT
6278 && GET_CODE (bl->initial_value) == CONST_INT)
6279 {
6280 HOST_WIDE_INT add_val, comparison_val;
6281 rtx initial_value;
6282
6283 add_val = INTVAL (bl->biv->add_val);
6284 comparison_val = INTVAL (XEXP (comparison, 1));
6285 initial_value = bl->initial_value;
6286
6287 /* Normalize the initial value if it is an integer and
6288 has no other use except as a counter. This will allow
6289 a few more loops to be reversed. */
6290 if (no_use_except_counting
6291 && GET_CODE (initial_value) == CONST_INT)
6292 {
6293 comparison_val = comparison_val - INTVAL (bl->initial_value);
6294 /* Check for overflow. If comparison_val ends up as a
6295 negative value, then we can't reverse the loop. */
6296 if (comparison_val >= 0)
6297 initial_value = const0_rtx;
6298 }
6299
6300 /* If the initial value is not zero, or if the comparison
6301 value is not an exact multiple of the increment, then we
6302 can not reverse this loop. */
6303 if (initial_value != const0_rtx
6304 || (comparison_val % add_val) != 0)
6305 return 0;
6306
6307 /* Reset these in case we normalized the initial value
6308 and comparison value above. */
6309 bl->initial_value = initial_value;
6310 XEXP (comparison, 1) = GEN_INT (comparison_val);
6311
6312 /* Register will always be nonnegative, with value
6313 0 on last iteration if loop reversed */
6314
6315 /* Save some info needed to produce the new insns. */
6316 reg = bl->biv->dest_reg;
6317 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
6318 if (jump_label == pc_rtx)
6319 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
6320 new_add_val = GEN_INT (- INTVAL (bl->biv->add_val));
6321
6322 final_value = XEXP (comparison, 1);
6323 start_value = GEN_INT (INTVAL (XEXP (comparison, 1))
6324 - INTVAL (bl->biv->add_val));
6325
6326 /* Initialize biv to start_value before loop start.
6327 The old initializing insn will be deleted as a
6328 dead store by flow.c. */
6329 emit_insn_before (gen_move_insn (reg, start_value), loop_start);
6330
6331 /* Add insn to decrement register, and delete insn
6332 that incremented the register. */
6333 p = emit_insn_before (gen_add2_insn (reg, new_add_val),
6334 bl->biv->insn);
6335 delete_insn (bl->biv->insn);
6336
6337 /* Update biv info to reflect its new status. */
6338 bl->biv->insn = p;
6339 bl->initial_value = start_value;
6340 bl->biv->add_val = new_add_val;
6341
6342 /* Inc LABEL_NUSES so that delete_insn will
6343 not delete the label. */
6344 LABEL_NUSES (XEXP (jump_label, 0)) ++;
6345
6346 /* Emit an insn after the end of the loop to set the biv's
6347 proper exit value if it is used anywhere outside the loop. */
6348 if ((REGNO_LAST_UID (bl->regno)
6349 != INSN_UID (PREV_INSN (PREV_INSN (loop_end))))
6350 || ! bl->init_insn
6351 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
6352 emit_insn_after (gen_move_insn (reg, final_value),
6353 loop_end);
6354
6355 /* Delete compare/branch at end of loop. */
6356 delete_insn (PREV_INSN (loop_end));
6357 delete_insn (PREV_INSN (loop_end));
6358
6359 /* Add new compare/branch insn at end of loop. */
6360 start_sequence ();
6361 emit_cmp_insn (reg, const0_rtx, GE, NULL_RTX,
6362 GET_MODE (reg), 0, 0);
6363 emit_jump_insn (gen_bge (XEXP (jump_label, 0)));
6364 tem = gen_sequence ();
6365 end_sequence ();
6366 emit_jump_insn_before (tem, loop_end);
6367
6368 for (tem = PREV_INSN (loop_end);
6369 tem && GET_CODE (tem) != JUMP_INSN; tem = PREV_INSN (tem))
6370 ;
6371 if (tem)
6372 {
6373 JUMP_LABEL (tem) = XEXP (jump_label, 0);
6374
6375 /* Increment of LABEL_NUSES done above. */
6376 /* Register is now always nonnegative,
6377 so add REG_NONNEG note to the branch. */
6378 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
6379 REG_NOTES (tem));
6380 }
6381
6382 bl->nonneg = 1;
6383
6384 /* Mark that this biv has been reversed. Each giv which depends
6385 on this biv, and which is also live past the end of the loop
6386 will have to be fixed up. */
6387
6388 bl->reversed = 1;
6389
6390 if (loop_dump_stream)
6391 fprintf (loop_dump_stream,
6392 "Reversed loop and added reg_nonneg\n");
6393
6394 return 1;
6395 }
6396 }
6397 }
6398
6399 return 0;
6400 }
6401 \f
6402 /* Verify whether the biv BL appears to be eliminable,
6403 based on the insns in the loop that refer to it.
6404 LOOP_START is the first insn of the loop, and END is the end insn.
6405
6406 If ELIMINATE_P is non-zero, actually do the elimination.
6407
6408 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
6409 determine whether invariant insns should be placed inside or at the
6410 start of the loop. */
6411
6412 static int
6413 maybe_eliminate_biv (bl, loop_start, end, eliminate_p, threshold, insn_count)
6414 struct iv_class *bl;
6415 rtx loop_start;
6416 rtx end;
6417 int eliminate_p;
6418 int threshold, insn_count;
6419 {
6420 rtx reg = bl->biv->dest_reg;
6421 rtx p;
6422
6423 /* Scan all insns in the loop, stopping if we find one that uses the
6424 biv in a way that we cannot eliminate. */
6425
6426 for (p = loop_start; p != end; p = NEXT_INSN (p))
6427 {
6428 enum rtx_code code = GET_CODE (p);
6429 rtx where = threshold >= insn_count ? loop_start : p;
6430
6431 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
6432 && reg_mentioned_p (reg, PATTERN (p))
6433 && ! maybe_eliminate_biv_1 (PATTERN (p), p, bl, eliminate_p, where))
6434 {
6435 if (loop_dump_stream)
6436 fprintf (loop_dump_stream,
6437 "Cannot eliminate biv %d: biv used in insn %d.\n",
6438 bl->regno, INSN_UID (p));
6439 break;
6440 }
6441 }
6442
6443 if (p == end)
6444 {
6445 if (loop_dump_stream)
6446 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
6447 bl->regno, eliminate_p ? "was" : "can be");
6448 return 1;
6449 }
6450
6451 return 0;
6452 }
6453 \f
6454 /* If BL appears in X (part of the pattern of INSN), see if we can
6455 eliminate its use. If so, return 1. If not, return 0.
6456
6457 If BIV does not appear in X, return 1.
6458
6459 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
6460 where extra insns should be added. Depending on how many items have been
6461 moved out of the loop, it will either be before INSN or at the start of
6462 the loop. */
6463
6464 static int
6465 maybe_eliminate_biv_1 (x, insn, bl, eliminate_p, where)
6466 rtx x, insn;
6467 struct iv_class *bl;
6468 int eliminate_p;
6469 rtx where;
6470 {
6471 enum rtx_code code = GET_CODE (x);
6472 rtx reg = bl->biv->dest_reg;
6473 enum machine_mode mode = GET_MODE (reg);
6474 struct induction *v;
6475 rtx arg, tem;
6476 #ifdef HAVE_cc0
6477 rtx new;
6478 #endif
6479 int arg_operand;
6480 char *fmt;
6481 int i, j;
6482
6483 switch (code)
6484 {
6485 case REG:
6486 /* If we haven't already been able to do something with this BIV,
6487 we can't eliminate it. */
6488 if (x == reg)
6489 return 0;
6490 return 1;
6491
6492 case SET:
6493 /* If this sets the BIV, it is not a problem. */
6494 if (SET_DEST (x) == reg)
6495 return 1;
6496
6497 /* If this is an insn that defines a giv, it is also ok because
6498 it will go away when the giv is reduced. */
6499 for (v = bl->giv; v; v = v->next_iv)
6500 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
6501 return 1;
6502
6503 #ifdef HAVE_cc0
6504 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
6505 {
6506 /* Can replace with any giv that was reduced and
6507 that has (MULT_VAL != 0) and (ADD_VAL == 0).
6508 Require a constant for MULT_VAL, so we know it's nonzero.
6509 ??? We disable this optimization to avoid potential
6510 overflows. */
6511
6512 for (v = bl->giv; v; v = v->next_iv)
6513 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6514 && v->add_val == const0_rtx
6515 && ! v->ignore && ! v->maybe_dead && v->always_computable
6516 && v->mode == mode
6517 && 0)
6518 {
6519 /* If the giv V had the auto-inc address optimization applied
6520 to it, and INSN occurs between the giv insn and the biv
6521 insn, then we must adjust the value used here.
6522 This is rare, so we don't bother to do so. */
6523 if (v->auto_inc_opt
6524 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6525 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6526 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6527 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6528 continue;
6529
6530 if (! eliminate_p)
6531 return 1;
6532
6533 /* If the giv has the opposite direction of change,
6534 then reverse the comparison. */
6535 if (INTVAL (v->mult_val) < 0)
6536 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
6537 const0_rtx, v->new_reg);
6538 else
6539 new = v->new_reg;
6540
6541 /* We can probably test that giv's reduced reg. */
6542 if (validate_change (insn, &SET_SRC (x), new, 0))
6543 return 1;
6544 }
6545
6546 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
6547 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
6548 Require a constant for MULT_VAL, so we know it's nonzero.
6549 ??? Do this only if ADD_VAL is a pointer to avoid a potential
6550 overflow problem. */
6551
6552 for (v = bl->giv; v; v = v->next_iv)
6553 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6554 && ! v->ignore && ! v->maybe_dead && v->always_computable
6555 && v->mode == mode
6556 && (GET_CODE (v->add_val) == SYMBOL_REF
6557 || GET_CODE (v->add_val) == LABEL_REF
6558 || GET_CODE (v->add_val) == CONST
6559 || (GET_CODE (v->add_val) == REG
6560 && REGNO_POINTER_FLAG (REGNO (v->add_val)))))
6561 {
6562 /* If the giv V had the auto-inc address optimization applied
6563 to it, and INSN occurs between the giv insn and the biv
6564 insn, then we must adjust the value used here.
6565 This is rare, so we don't bother to do so. */
6566 if (v->auto_inc_opt
6567 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6568 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6569 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6570 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6571 continue;
6572
6573 if (! eliminate_p)
6574 return 1;
6575
6576 /* If the giv has the opposite direction of change,
6577 then reverse the comparison. */
6578 if (INTVAL (v->mult_val) < 0)
6579 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
6580 v->new_reg);
6581 else
6582 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
6583 copy_rtx (v->add_val));
6584
6585 /* Replace biv with the giv's reduced register. */
6586 update_reg_last_use (v->add_val, insn);
6587 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6588 return 1;
6589
6590 /* Insn doesn't support that constant or invariant. Copy it
6591 into a register (it will be a loop invariant.) */
6592 tem = gen_reg_rtx (GET_MODE (v->new_reg));
6593
6594 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
6595 where);
6596
6597 /* Substitute the new register for its invariant value in
6598 the compare expression. */
6599 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
6600 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6601 return 1;
6602 }
6603 }
6604 #endif
6605 break;
6606
6607 case COMPARE:
6608 case EQ: case NE:
6609 case GT: case GE: case GTU: case GEU:
6610 case LT: case LE: case LTU: case LEU:
6611 /* See if either argument is the biv. */
6612 if (XEXP (x, 0) == reg)
6613 arg = XEXP (x, 1), arg_operand = 1;
6614 else if (XEXP (x, 1) == reg)
6615 arg = XEXP (x, 0), arg_operand = 0;
6616 else
6617 break;
6618
6619 if (CONSTANT_P (arg))
6620 {
6621 /* First try to replace with any giv that has constant positive
6622 mult_val and constant add_val. We might be able to support
6623 negative mult_val, but it seems complex to do it in general. */
6624
6625 for (v = bl->giv; v; v = v->next_iv)
6626 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6627 && (GET_CODE (v->add_val) == SYMBOL_REF
6628 || GET_CODE (v->add_val) == LABEL_REF
6629 || GET_CODE (v->add_val) == CONST
6630 || (GET_CODE (v->add_val) == REG
6631 && REGNO_POINTER_FLAG (REGNO (v->add_val))))
6632 && ! v->ignore && ! v->maybe_dead && v->always_computable
6633 && v->mode == mode)
6634 {
6635 /* If the giv V had the auto-inc address optimization applied
6636 to it, and INSN occurs between the giv insn and the biv
6637 insn, then we must adjust the value used here.
6638 This is rare, so we don't bother to do so. */
6639 if (v->auto_inc_opt
6640 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6641 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6642 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6643 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6644 continue;
6645
6646 if (! eliminate_p)
6647 return 1;
6648
6649 /* Replace biv with the giv's reduced reg. */
6650 XEXP (x, 1-arg_operand) = v->new_reg;
6651
6652 /* If all constants are actually constant integers and
6653 the derived constant can be directly placed in the COMPARE,
6654 do so. */
6655 if (GET_CODE (arg) == CONST_INT
6656 && GET_CODE (v->mult_val) == CONST_INT
6657 && GET_CODE (v->add_val) == CONST_INT
6658 && validate_change (insn, &XEXP (x, arg_operand),
6659 GEN_INT (INTVAL (arg)
6660 * INTVAL (v->mult_val)
6661 + INTVAL (v->add_val)), 0))
6662 return 1;
6663
6664 /* Otherwise, load it into a register. */
6665 tem = gen_reg_rtx (mode);
6666 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6667 if (validate_change (insn, &XEXP (x, arg_operand), tem, 0))
6668 return 1;
6669
6670 /* If that failed, put back the change we made above. */
6671 XEXP (x, 1-arg_operand) = reg;
6672 }
6673
6674 /* Look for giv with positive constant mult_val and nonconst add_val.
6675 Insert insns to calculate new compare value.
6676 ??? Turn this off due to possible overflow. */
6677
6678 for (v = bl->giv; v; v = v->next_iv)
6679 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6680 && ! v->ignore && ! v->maybe_dead && v->always_computable
6681 && v->mode == mode
6682 && 0)
6683 {
6684 rtx tem;
6685
6686 /* If the giv V had the auto-inc address optimization applied
6687 to it, and INSN occurs between the giv insn and the biv
6688 insn, then we must adjust the value used here.
6689 This is rare, so we don't bother to do so. */
6690 if (v->auto_inc_opt
6691 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6692 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6693 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6694 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6695 continue;
6696
6697 if (! eliminate_p)
6698 return 1;
6699
6700 tem = gen_reg_rtx (mode);
6701
6702 /* Replace biv with giv's reduced register. */
6703 validate_change (insn, &XEXP (x, 1 - arg_operand),
6704 v->new_reg, 1);
6705
6706 /* Compute value to compare against. */
6707 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6708 /* Use it in this insn. */
6709 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6710 if (apply_change_group ())
6711 return 1;
6712 }
6713 }
6714 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
6715 {
6716 if (invariant_p (arg) == 1)
6717 {
6718 /* Look for giv with constant positive mult_val and nonconst
6719 add_val. Insert insns to compute new compare value.
6720 ??? Turn this off due to possible overflow. */
6721
6722 for (v = bl->giv; v; v = v->next_iv)
6723 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6724 && ! v->ignore && ! v->maybe_dead && v->always_computable
6725 && v->mode == mode
6726 && 0)
6727 {
6728 rtx tem;
6729
6730 /* If the giv V had the auto-inc address optimization applied
6731 to it, and INSN occurs between the giv insn and the biv
6732 insn, then we must adjust the value used here.
6733 This is rare, so we don't bother to do so. */
6734 if (v->auto_inc_opt
6735 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6736 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6737 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6738 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6739 continue;
6740
6741 if (! eliminate_p)
6742 return 1;
6743
6744 tem = gen_reg_rtx (mode);
6745
6746 /* Replace biv with giv's reduced register. */
6747 validate_change (insn, &XEXP (x, 1 - arg_operand),
6748 v->new_reg, 1);
6749
6750 /* Compute value to compare against. */
6751 emit_iv_add_mult (arg, v->mult_val, v->add_val,
6752 tem, where);
6753 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6754 if (apply_change_group ())
6755 return 1;
6756 }
6757 }
6758
6759 /* This code has problems. Basically, you can't know when
6760 seeing if we will eliminate BL, whether a particular giv
6761 of ARG will be reduced. If it isn't going to be reduced,
6762 we can't eliminate BL. We can try forcing it to be reduced,
6763 but that can generate poor code.
6764
6765 The problem is that the benefit of reducing TV, below should
6766 be increased if BL can actually be eliminated, but this means
6767 we might have to do a topological sort of the order in which
6768 we try to process biv. It doesn't seem worthwhile to do
6769 this sort of thing now. */
6770
6771 #if 0
6772 /* Otherwise the reg compared with had better be a biv. */
6773 if (GET_CODE (arg) != REG
6774 || reg_iv_type[REGNO (arg)] != BASIC_INDUCT)
6775 return 0;
6776
6777 /* Look for a pair of givs, one for each biv,
6778 with identical coefficients. */
6779 for (v = bl->giv; v; v = v->next_iv)
6780 {
6781 struct induction *tv;
6782
6783 if (v->ignore || v->maybe_dead || v->mode != mode)
6784 continue;
6785
6786 for (tv = reg_biv_class[REGNO (arg)]->giv; tv; tv = tv->next_iv)
6787 if (! tv->ignore && ! tv->maybe_dead
6788 && rtx_equal_p (tv->mult_val, v->mult_val)
6789 && rtx_equal_p (tv->add_val, v->add_val)
6790 && tv->mode == mode)
6791 {
6792 /* If the giv V had the auto-inc address optimization applied
6793 to it, and INSN occurs between the giv insn and the biv
6794 insn, then we must adjust the value used here.
6795 This is rare, so we don't bother to do so. */
6796 if (v->auto_inc_opt
6797 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6798 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6799 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6800 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6801 continue;
6802
6803 if (! eliminate_p)
6804 return 1;
6805
6806 /* Replace biv with its giv's reduced reg. */
6807 XEXP (x, 1-arg_operand) = v->new_reg;
6808 /* Replace other operand with the other giv's
6809 reduced reg. */
6810 XEXP (x, arg_operand) = tv->new_reg;
6811 return 1;
6812 }
6813 }
6814 #endif
6815 }
6816
6817 /* If we get here, the biv can't be eliminated. */
6818 return 0;
6819
6820 case MEM:
6821 /* If this address is a DEST_ADDR giv, it doesn't matter if the
6822 biv is used in it, since it will be replaced. */
6823 for (v = bl->giv; v; v = v->next_iv)
6824 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
6825 return 1;
6826 break;
6827
6828 default:
6829 break;
6830 }
6831
6832 /* See if any subexpression fails elimination. */
6833 fmt = GET_RTX_FORMAT (code);
6834 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6835 {
6836 switch (fmt[i])
6837 {
6838 case 'e':
6839 if (! maybe_eliminate_biv_1 (XEXP (x, i), insn, bl,
6840 eliminate_p, where))
6841 return 0;
6842 break;
6843
6844 case 'E':
6845 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6846 if (! maybe_eliminate_biv_1 (XVECEXP (x, i, j), insn, bl,
6847 eliminate_p, where))
6848 return 0;
6849 break;
6850 }
6851 }
6852
6853 return 1;
6854 }
6855 \f
6856 /* Return nonzero if the last use of REG
6857 is in an insn following INSN in the same basic block. */
6858
6859 static int
6860 last_use_this_basic_block (reg, insn)
6861 rtx reg;
6862 rtx insn;
6863 {
6864 rtx n;
6865 for (n = insn;
6866 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
6867 n = NEXT_INSN (n))
6868 {
6869 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
6870 return 1;
6871 }
6872 return 0;
6873 }
6874 \f
6875 /* Called via `note_stores' to record the initial value of a biv. Here we
6876 just record the location of the set and process it later. */
6877
6878 static void
6879 record_initial (dest, set)
6880 rtx dest;
6881 rtx set;
6882 {
6883 struct iv_class *bl;
6884
6885 if (GET_CODE (dest) != REG
6886 || REGNO (dest) >= max_reg_before_loop
6887 || reg_iv_type[REGNO (dest)] != BASIC_INDUCT)
6888 return;
6889
6890 bl = reg_biv_class[REGNO (dest)];
6891
6892 /* If this is the first set found, record it. */
6893 if (bl->init_insn == 0)
6894 {
6895 bl->init_insn = note_insn;
6896 bl->init_set = set;
6897 }
6898 }
6899 \f
6900 /* If any of the registers in X are "old" and currently have a last use earlier
6901 than INSN, update them to have a last use of INSN. Their actual last use
6902 will be the previous insn but it will not have a valid uid_luid so we can't
6903 use it. */
6904
6905 static void
6906 update_reg_last_use (x, insn)
6907 rtx x;
6908 rtx insn;
6909 {
6910 /* Check for the case where INSN does not have a valid luid. In this case,
6911 there is no need to modify the regno_last_uid, as this can only happen
6912 when code is inserted after the loop_end to set a pseudo's final value,
6913 and hence this insn will never be the last use of x. */
6914 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
6915 && INSN_UID (insn) < max_uid_for_loop
6916 && uid_luid[REGNO_LAST_UID (REGNO (x))] < uid_luid[INSN_UID (insn)])
6917 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
6918 else
6919 {
6920 register int i, j;
6921 register char *fmt = GET_RTX_FORMAT (GET_CODE (x));
6922 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6923 {
6924 if (fmt[i] == 'e')
6925 update_reg_last_use (XEXP (x, i), insn);
6926 else if (fmt[i] == 'E')
6927 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6928 update_reg_last_use (XVECEXP (x, i, j), insn);
6929 }
6930 }
6931 }
6932 \f
6933 /* Given a jump insn JUMP, return the condition that will cause it to branch
6934 to its JUMP_LABEL. If the condition cannot be understood, or is an
6935 inequality floating-point comparison which needs to be reversed, 0 will
6936 be returned.
6937
6938 If EARLIEST is non-zero, it is a pointer to a place where the earliest
6939 insn used in locating the condition was found. If a replacement test
6940 of the condition is desired, it should be placed in front of that
6941 insn and we will be sure that the inputs are still valid.
6942
6943 The condition will be returned in a canonical form to simplify testing by
6944 callers. Specifically:
6945
6946 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
6947 (2) Both operands will be machine operands; (cc0) will have been replaced.
6948 (3) If an operand is a constant, it will be the second operand.
6949 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
6950 for GE, GEU, and LEU. */
6951
6952 rtx
6953 get_condition (jump, earliest)
6954 rtx jump;
6955 rtx *earliest;
6956 {
6957 enum rtx_code code;
6958 rtx prev = jump;
6959 rtx set;
6960 rtx tem;
6961 rtx op0, op1;
6962 int reverse_code = 0;
6963 int did_reverse_condition = 0;
6964
6965 /* If this is not a standard conditional jump, we can't parse it. */
6966 if (GET_CODE (jump) != JUMP_INSN
6967 || ! condjump_p (jump) || simplejump_p (jump))
6968 return 0;
6969
6970 code = GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 0));
6971 op0 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 0);
6972 op1 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 1);
6973
6974 if (earliest)
6975 *earliest = jump;
6976
6977 /* If this branches to JUMP_LABEL when the condition is false, reverse
6978 the condition. */
6979 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
6980 && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
6981 code = reverse_condition (code), did_reverse_condition ^= 1;
6982
6983 /* If we are comparing a register with zero, see if the register is set
6984 in the previous insn to a COMPARE or a comparison operation. Perform
6985 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
6986 in cse.c */
6987
6988 while (GET_RTX_CLASS (code) == '<' && op1 == CONST0_RTX (GET_MODE (op0)))
6989 {
6990 /* Set non-zero when we find something of interest. */
6991 rtx x = 0;
6992
6993 #ifdef HAVE_cc0
6994 /* If comparison with cc0, import actual comparison from compare
6995 insn. */
6996 if (op0 == cc0_rtx)
6997 {
6998 if ((prev = prev_nonnote_insn (prev)) == 0
6999 || GET_CODE (prev) != INSN
7000 || (set = single_set (prev)) == 0
7001 || SET_DEST (set) != cc0_rtx)
7002 return 0;
7003
7004 op0 = SET_SRC (set);
7005 op1 = CONST0_RTX (GET_MODE (op0));
7006 if (earliest)
7007 *earliest = prev;
7008 }
7009 #endif
7010
7011 /* If this is a COMPARE, pick up the two things being compared. */
7012 if (GET_CODE (op0) == COMPARE)
7013 {
7014 op1 = XEXP (op0, 1);
7015 op0 = XEXP (op0, 0);
7016 continue;
7017 }
7018 else if (GET_CODE (op0) != REG)
7019 break;
7020
7021 /* Go back to the previous insn. Stop if it is not an INSN. We also
7022 stop if it isn't a single set or if it has a REG_INC note because
7023 we don't want to bother dealing with it. */
7024
7025 if ((prev = prev_nonnote_insn (prev)) == 0
7026 || GET_CODE (prev) != INSN
7027 || FIND_REG_INC_NOTE (prev, 0)
7028 || (set = single_set (prev)) == 0)
7029 break;
7030
7031 /* If this is setting OP0, get what it sets it to if it looks
7032 relevant. */
7033 if (rtx_equal_p (SET_DEST (set), op0))
7034 {
7035 enum machine_mode inner_mode = GET_MODE (SET_SRC (set));
7036
7037 if ((GET_CODE (SET_SRC (set)) == COMPARE
7038 || (((code == NE
7039 || (code == LT
7040 && GET_MODE_CLASS (inner_mode) == MODE_INT
7041 && (GET_MODE_BITSIZE (inner_mode)
7042 <= HOST_BITS_PER_WIDE_INT)
7043 && (STORE_FLAG_VALUE
7044 & ((HOST_WIDE_INT) 1
7045 << (GET_MODE_BITSIZE (inner_mode) - 1))))
7046 #ifdef FLOAT_STORE_FLAG_VALUE
7047 || (code == LT
7048 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
7049 && FLOAT_STORE_FLAG_VALUE < 0)
7050 #endif
7051 ))
7052 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')))
7053 x = SET_SRC (set);
7054 else if (((code == EQ
7055 || (code == GE
7056 && (GET_MODE_BITSIZE (inner_mode)
7057 <= HOST_BITS_PER_WIDE_INT)
7058 && GET_MODE_CLASS (inner_mode) == MODE_INT
7059 && (STORE_FLAG_VALUE
7060 & ((HOST_WIDE_INT) 1
7061 << (GET_MODE_BITSIZE (inner_mode) - 1))))
7062 #ifdef FLOAT_STORE_FLAG_VALUE
7063 || (code == GE
7064 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
7065 && FLOAT_STORE_FLAG_VALUE < 0)
7066 #endif
7067 ))
7068 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')
7069 {
7070 /* We might have reversed a LT to get a GE here. But this wasn't
7071 actually the comparison of data, so we don't flag that we
7072 have had to reverse the condition. */
7073 did_reverse_condition ^= 1;
7074 reverse_code = 1;
7075 x = SET_SRC (set);
7076 }
7077 else
7078 break;
7079 }
7080
7081 else if (reg_set_p (op0, prev))
7082 /* If this sets OP0, but not directly, we have to give up. */
7083 break;
7084
7085 if (x)
7086 {
7087 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
7088 code = GET_CODE (x);
7089 if (reverse_code)
7090 {
7091 code = reverse_condition (code);
7092 did_reverse_condition ^= 1;
7093 reverse_code = 0;
7094 }
7095
7096 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
7097 if (earliest)
7098 *earliest = prev;
7099 }
7100 }
7101
7102 /* If constant is first, put it last. */
7103 if (CONSTANT_P (op0))
7104 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
7105
7106 /* If OP0 is the result of a comparison, we weren't able to find what
7107 was really being compared, so fail. */
7108 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
7109 return 0;
7110
7111 /* Canonicalize any ordered comparison with integers involving equality
7112 if we can do computations in the relevant mode and we do not
7113 overflow. */
7114
7115 if (GET_CODE (op1) == CONST_INT
7116 && GET_MODE (op0) != VOIDmode
7117 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
7118 {
7119 HOST_WIDE_INT const_val = INTVAL (op1);
7120 unsigned HOST_WIDE_INT uconst_val = const_val;
7121 unsigned HOST_WIDE_INT max_val
7122 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
7123
7124 switch (code)
7125 {
7126 case LE:
7127 if (const_val != max_val >> 1)
7128 code = LT, op1 = GEN_INT (const_val + 1);
7129 break;
7130
7131 /* When cross-compiling, const_val might be sign-extended from
7132 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
7133 case GE:
7134 if ((const_val & max_val)
7135 != (((HOST_WIDE_INT) 1
7136 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
7137 code = GT, op1 = GEN_INT (const_val - 1);
7138 break;
7139
7140 case LEU:
7141 if (uconst_val < max_val)
7142 code = LTU, op1 = GEN_INT (uconst_val + 1);
7143 break;
7144
7145 case GEU:
7146 if (uconst_val != 0)
7147 code = GTU, op1 = GEN_INT (uconst_val - 1);
7148 break;
7149
7150 default:
7151 break;
7152 }
7153 }
7154
7155 /* If this was floating-point and we reversed anything other than an
7156 EQ or NE, return zero. */
7157 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
7158 && did_reverse_condition && code != NE && code != EQ
7159 && ! flag_fast_math
7160 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
7161 return 0;
7162
7163 #ifdef HAVE_cc0
7164 /* Never return CC0; return zero instead. */
7165 if (op0 == cc0_rtx)
7166 return 0;
7167 #endif
7168
7169 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
7170 }
7171
7172 /* Similar to above routine, except that we also put an invariant last
7173 unless both operands are invariants. */
7174
7175 rtx
7176 get_condition_for_loop (x)
7177 rtx x;
7178 {
7179 rtx comparison = get_condition (x, NULL_PTR);
7180
7181 if (comparison == 0
7182 || ! invariant_p (XEXP (comparison, 0))
7183 || invariant_p (XEXP (comparison, 1)))
7184 return comparison;
7185
7186 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
7187 XEXP (comparison, 1), XEXP (comparison, 0));
7188 }
7189
7190 #ifdef HAIFA
7191 /* Analyze a loop in order to instrument it with the use of count register.
7192 loop_start and loop_end are the first and last insns of the loop.
7193 This function works in cooperation with insert_bct ().
7194 loop_can_insert_bct[loop_num] is set according to whether the optimization
7195 is applicable to the loop. When it is applicable, the following variables
7196 are also set:
7197 loop_start_value[loop_num]
7198 loop_comparison_value[loop_num]
7199 loop_increment[loop_num]
7200 loop_comparison_code[loop_num] */
7201
7202 #ifdef HAVE_decrement_and_branch_on_count
7203 static
7204 void analyze_loop_iterations (loop_start, loop_end)
7205 rtx loop_start, loop_end;
7206 {
7207 rtx comparison, comparison_value;
7208 rtx iteration_var, initial_value, increment;
7209 enum rtx_code comparison_code;
7210
7211 rtx last_loop_insn;
7212 rtx insn;
7213 int i;
7214
7215 /* loop_variable mode */
7216 enum machine_mode original_mode;
7217
7218 /* find the number of the loop */
7219 int loop_num = uid_loop_num [INSN_UID (loop_start)];
7220
7221 /* we change our mind only when we are sure that loop will be instrumented */
7222 loop_can_insert_bct[loop_num] = 0;
7223
7224 /* is the optimization suppressed. */
7225 if ( !flag_branch_on_count_reg )
7226 return;
7227
7228 /* make sure that count-reg is not in use */
7229 if (loop_used_count_register[loop_num]){
7230 if (loop_dump_stream)
7231 fprintf (loop_dump_stream,
7232 "analyze_loop_iterations %d: BCT instrumentation failed: count register already in use\n",
7233 loop_num);
7234 return;
7235 }
7236
7237 /* make sure that the function has no indirect jumps. */
7238 if (indirect_jump_in_function){
7239 if (loop_dump_stream)
7240 fprintf (loop_dump_stream,
7241 "analyze_loop_iterations %d: BCT instrumentation failed: indirect jump in function\n",
7242 loop_num);
7243 return;
7244 }
7245
7246 /* make sure that the last loop insn is a conditional jump */
7247 last_loop_insn = PREV_INSN (loop_end);
7248 if (GET_CODE (last_loop_insn) != JUMP_INSN || !condjump_p (last_loop_insn)) {
7249 if (loop_dump_stream)
7250 fprintf (loop_dump_stream,
7251 "analyze_loop_iterations %d: BCT instrumentation failed: invalid jump at loop end\n",
7252 loop_num);
7253 return;
7254 }
7255
7256 /* First find the iteration variable. If the last insn is a conditional
7257 branch, and the insn preceding it tests a register value, make that
7258 register the iteration variable. */
7259
7260 /* We used to use prev_nonnote_insn here, but that fails because it might
7261 accidentally get the branch for a contained loop if the branch for this
7262 loop was deleted. We can only trust branches immediately before the
7263 loop_end. */
7264
7265 comparison = get_condition_for_loop (last_loop_insn);
7266 /* ??? Get_condition may switch position of induction variable and
7267 invariant register when it canonicalizes the comparison. */
7268
7269 if (comparison == 0) {
7270 if (loop_dump_stream)
7271 fprintf (loop_dump_stream,
7272 "analyze_loop_iterations %d: BCT instrumentation failed: comparison not found\n",
7273 loop_num);
7274 return;
7275 }
7276
7277 comparison_code = GET_CODE (comparison);
7278 iteration_var = XEXP (comparison, 0);
7279 comparison_value = XEXP (comparison, 1);
7280
7281 original_mode = GET_MODE (iteration_var);
7282 if (GET_MODE_CLASS (original_mode) != MODE_INT
7283 || GET_MODE_SIZE (original_mode) != UNITS_PER_WORD) {
7284 if (loop_dump_stream)
7285 fprintf (loop_dump_stream,
7286 "analyze_loop_iterations %d: BCT Instrumentation failed: loop variable not integer\n",
7287 loop_num);
7288 return;
7289 }
7290
7291 /* get info about loop bounds and increment */
7292 iteration_info (iteration_var, &initial_value, &increment,
7293 loop_start, loop_end);
7294
7295 /* make sure that all required loop data were found */
7296 if (!(initial_value && increment && comparison_value
7297 && invariant_p (comparison_value) && invariant_p (increment)
7298 && ! indirect_jump_in_function))
7299 {
7300 if (loop_dump_stream) {
7301 fprintf (loop_dump_stream,
7302 "analyze_loop_iterations %d: BCT instrumentation failed because of wrong loop: ", loop_num);
7303 if (!(initial_value && increment && comparison_value)) {
7304 fprintf (loop_dump_stream, "\tbounds not available: ");
7305 if ( ! initial_value )
7306 fprintf (loop_dump_stream, "initial ");
7307 if ( ! increment )
7308 fprintf (loop_dump_stream, "increment ");
7309 if ( ! comparison_value )
7310 fprintf (loop_dump_stream, "comparison ");
7311 fprintf (loop_dump_stream, "\n");
7312 }
7313 if (!invariant_p (comparison_value) || !invariant_p (increment))
7314 fprintf (loop_dump_stream, "\tloop bounds not invariant\n");
7315 }
7316 return;
7317 }
7318
7319 /* make sure that the increment is constant */
7320 if (GET_CODE (increment) != CONST_INT) {
7321 if (loop_dump_stream)
7322 fprintf (loop_dump_stream,
7323 "analyze_loop_iterations %d: instrumentation failed: not arithmetic loop\n",
7324 loop_num);
7325 return;
7326 }
7327
7328 /* make sure that the loop contains neither function call, nor jump on table.
7329 (the count register might be altered by the called function, and might
7330 be used for a branch on table). */
7331 for (insn = loop_start; insn && insn != loop_end; insn = NEXT_INSN (insn)) {
7332 if (GET_CODE (insn) == CALL_INSN){
7333 if (loop_dump_stream)
7334 fprintf (loop_dump_stream,
7335 "analyze_loop_iterations %d: BCT instrumentation failed: function call in the loop\n",
7336 loop_num);
7337 return;
7338 }
7339
7340 if (GET_CODE (insn) == JUMP_INSN
7341 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
7342 || GET_CODE (PATTERN (insn)) == ADDR_VEC)){
7343 if (loop_dump_stream)
7344 fprintf (loop_dump_stream,
7345 "analyze_loop_iterations %d: BCT instrumentation failed: computed branch in the loop\n",
7346 loop_num);
7347 return;
7348 }
7349 }
7350
7351 /* At this point, we are sure that the loop can be instrumented with BCT.
7352 Some of the loops, however, will not be instrumented - the final decision
7353 is taken by insert_bct () */
7354 if (loop_dump_stream)
7355 fprintf (loop_dump_stream,
7356 "analyze_loop_iterations: loop (luid =%d) can be BCT instrumented.\n",
7357 loop_num);
7358
7359 /* mark all enclosing loops that they cannot use count register */
7360 /* ???: In fact, since insert_bct may decide not to instrument this loop,
7361 marking here may prevent instrumenting an enclosing loop that could
7362 actually be instrumented. But since this is rare, it is safer to mark
7363 here in case the order of calling (analyze/insert)_bct would be changed. */
7364 for (i=loop_num; i != -1; i = loop_outer_loop[i])
7365 loop_used_count_register[i] = 1;
7366
7367 /* Set data structures which will be used by the instrumentation phase */
7368 loop_start_value[loop_num] = initial_value;
7369 loop_comparison_value[loop_num] = comparison_value;
7370 loop_increment[loop_num] = increment;
7371 loop_comparison_code[loop_num] = comparison_code;
7372 loop_can_insert_bct[loop_num] = 1;
7373 }
7374
7375
7376 /* instrument loop for insertion of bct instruction. We distinguish between
7377 loops with compile-time bounds, to those with run-time bounds. The loop
7378 behaviour is analized according to the following characteristics/variables:
7379 ; Input variables:
7380 ; comparison-value: the value to which the iteration counter is compared.
7381 ; initial-value: iteration-counter initial value.
7382 ; increment: iteration-counter increment.
7383 ; Computed variables:
7384 ; increment-direction: the sign of the increment.
7385 ; compare-direction: '1' for GT, GTE, '-1' for LT, LTE, '0' for NE.
7386 ; range-direction: sign (comparison-value - initial-value)
7387 We give up on the following cases:
7388 ; loop variable overflow.
7389 ; run-time loop bounds with comparison code NE.
7390 */
7391
7392 static void
7393 insert_bct (loop_start, loop_end)
7394 rtx loop_start, loop_end;
7395 {
7396 rtx initial_value, comparison_value, increment;
7397 enum rtx_code comparison_code;
7398
7399 int increment_direction, compare_direction;
7400 int unsigned_p = 0;
7401
7402 /* if the loop condition is <= or >=, the number of iteration
7403 is 1 more than the range of the bounds of the loop */
7404 int add_iteration = 0;
7405
7406 /* the only machine mode we work with - is the integer of the size that the
7407 machine has */
7408 enum machine_mode loop_var_mode = SImode;
7409
7410 int loop_num = uid_loop_num [INSN_UID (loop_start)];
7411
7412 /* get loop-variables. No need to check that these are valid - already
7413 checked in analyze_loop_iterations (). */
7414 comparison_code = loop_comparison_code[loop_num];
7415 initial_value = loop_start_value[loop_num];
7416 comparison_value = loop_comparison_value[loop_num];
7417 increment = loop_increment[loop_num];
7418
7419 /* check analyze_loop_iterations decision for this loop. */
7420 if (! loop_can_insert_bct[loop_num]){
7421 if (loop_dump_stream)
7422 fprintf (loop_dump_stream,
7423 "insert_bct: [%d] - was decided not to instrument by analyze_loop_iterations ()\n",
7424 loop_num);
7425 return;
7426 }
7427
7428 /* It's impossible to instrument a competely unrolled loop. */
7429 if (loop_unroll_factor [loop_num] == -1)
7430 return;
7431
7432 /* make sure that the last loop insn is a conditional jump .
7433 This check is repeated from analyze_loop_iterations (),
7434 because unrolling might have changed that. */
7435 if (GET_CODE (PREV_INSN (loop_end)) != JUMP_INSN
7436 || !condjump_p (PREV_INSN (loop_end))) {
7437 if (loop_dump_stream)
7438 fprintf (loop_dump_stream,
7439 "insert_bct: not instrumenting BCT because of invalid branch\n");
7440 return;
7441 }
7442
7443 /* fix increment in case loop was unrolled. */
7444 if (loop_unroll_factor [loop_num] > 1)
7445 increment = GEN_INT ( INTVAL (increment) * loop_unroll_factor [loop_num] );
7446
7447 /* determine properties and directions of the loop */
7448 increment_direction = (INTVAL (increment) > 0) ? 1:-1;
7449 switch ( comparison_code ) {
7450 case LEU:
7451 unsigned_p = 1;
7452 /* fallthrough */
7453 case LE:
7454 compare_direction = 1;
7455 add_iteration = 1;
7456 break;
7457 case GEU:
7458 unsigned_p = 1;
7459 /* fallthrough */
7460 case GE:
7461 compare_direction = -1;
7462 add_iteration = 1;
7463 break;
7464 case EQ:
7465 /* in this case we cannot know the number of iterations */
7466 if (loop_dump_stream)
7467 fprintf (loop_dump_stream,
7468 "insert_bct: %d: loop cannot be instrumented: == in condition\n",
7469 loop_num);
7470 return;
7471 case LTU:
7472 unsigned_p = 1;
7473 /* fallthrough */
7474 case LT:
7475 compare_direction = 1;
7476 break;
7477 case GTU:
7478 unsigned_p = 1;
7479 /* fallthrough */
7480 case GT:
7481 compare_direction = -1;
7482 break;
7483 case NE:
7484 compare_direction = 0;
7485 break;
7486 default:
7487 abort ();
7488 }
7489
7490
7491 /* make sure that the loop does not end by an overflow */
7492 if (compare_direction != increment_direction) {
7493 if (loop_dump_stream)
7494 fprintf (loop_dump_stream,
7495 "insert_bct: %d: loop cannot be instrumented: terminated by overflow\n",
7496 loop_num);
7497 return;
7498 }
7499
7500 /* try to instrument the loop. */
7501
7502 /* Handle the simpler case, where the bounds are known at compile time. */
7503 if (GET_CODE (initial_value) == CONST_INT && GET_CODE (comparison_value) == CONST_INT)
7504 {
7505 int n_iterations;
7506 int increment_value_abs = INTVAL (increment) * increment_direction;
7507
7508 /* check the relation between compare-val and initial-val */
7509 int difference = INTVAL (comparison_value) - INTVAL (initial_value);
7510 int range_direction = (difference > 0) ? 1 : -1;
7511
7512 /* make sure the loop executes enough iterations to gain from BCT */
7513 if (difference > -3 && difference < 3) {
7514 if (loop_dump_stream)
7515 fprintf (loop_dump_stream,
7516 "insert_bct: loop %d not BCT instrumented: too small iteration count.\n",
7517 loop_num);
7518 return;
7519 }
7520
7521 /* make sure that the loop executes at least once */
7522 if ((range_direction == 1 && compare_direction == -1)
7523 || (range_direction == -1 && compare_direction == 1))
7524 {
7525 if (loop_dump_stream)
7526 fprintf (loop_dump_stream,
7527 "insert_bct: loop %d: does not iterate even once. Not instrumenting.\n",
7528 loop_num);
7529 return;
7530 }
7531
7532 /* make sure that the loop does not end by an overflow (in compile time
7533 bounds we must have an additional check for overflow, because here
7534 we also support the compare code of 'NE'. */
7535 if (comparison_code == NE
7536 && increment_direction != range_direction) {
7537 if (loop_dump_stream)
7538 fprintf (loop_dump_stream,
7539 "insert_bct (compile time bounds): %d: loop not instrumented: terminated by overflow\n",
7540 loop_num);
7541 return;
7542 }
7543
7544 /* Determine the number of iterations by:
7545 ;
7546 ; compare-val - initial-val + (increment -1) + additional-iteration
7547 ; num_iterations = -----------------------------------------------------------------
7548 ; increment
7549 */
7550 difference = (range_direction > 0) ? difference : -difference;
7551 #if 0
7552 fprintf (stderr, "difference is: %d\n", difference); /* @*/
7553 fprintf (stderr, "increment_value_abs is: %d\n", increment_value_abs); /* @*/
7554 fprintf (stderr, "add_iteration is: %d\n", add_iteration); /* @*/
7555 fprintf (stderr, "INTVAL (comparison_value) is: %d\n", INTVAL (comparison_value)); /* @*/
7556 fprintf (stderr, "INTVAL (initial_value) is: %d\n", INTVAL (initial_value)); /* @*/
7557 #endif
7558
7559 if (increment_value_abs == 0) {
7560 fprintf (stderr, "insert_bct: error: increment == 0 !!!\n");
7561 abort ();
7562 }
7563 n_iterations = (difference + increment_value_abs - 1 + add_iteration)
7564 / increment_value_abs;
7565
7566 #if 0
7567 fprintf (stderr, "number of iterations is: %d\n", n_iterations); /* @*/
7568 #endif
7569 instrument_loop_bct (loop_start, loop_end, GEN_INT (n_iterations));
7570
7571 /* Done with this loop. */
7572 return;
7573 }
7574
7575 /* Handle the more complex case, that the bounds are NOT known at compile time. */
7576 /* In this case we generate run_time calculation of the number of iterations */
7577
7578 /* With runtime bounds, if the compare is of the form '!=' we give up */
7579 if (comparison_code == NE) {
7580 if (loop_dump_stream)
7581 fprintf (loop_dump_stream,
7582 "insert_bct: fail for loop %d: runtime bounds with != comparison\n",
7583 loop_num);
7584 return;
7585 }
7586
7587 else {
7588 /* We rely on the existence of run-time guard to ensure that the
7589 loop executes at least once. */
7590 rtx sequence;
7591 rtx iterations_num_reg;
7592
7593 int increment_value_abs = INTVAL (increment) * increment_direction;
7594
7595 /* make sure that the increment is a power of two, otherwise (an
7596 expensive) divide is needed. */
7597 if (exact_log2 (increment_value_abs) == -1)
7598 {
7599 if (loop_dump_stream)
7600 fprintf (loop_dump_stream,
7601 "insert_bct: not instrumenting BCT because the increment is not power of 2\n");
7602 return;
7603 }
7604
7605 /* compute the number of iterations */
7606 start_sequence ();
7607 {
7608 rtx temp_reg;
7609
7610 /* Again, the number of iterations is calculated by:
7611 ;
7612 ; compare-val - initial-val + (increment -1) + additional-iteration
7613 ; num_iterations = -----------------------------------------------------------------
7614 ; increment
7615 */
7616 /* ??? Do we have to call copy_rtx here before passing rtx to
7617 expand_binop? */
7618 if (compare_direction > 0) {
7619 /* <, <= :the loop variable is increasing */
7620 temp_reg = expand_binop (loop_var_mode, sub_optab, comparison_value,
7621 initial_value, NULL_RTX, 0, OPTAB_LIB_WIDEN);
7622 }
7623 else {
7624 temp_reg = expand_binop (loop_var_mode, sub_optab, initial_value,
7625 comparison_value, NULL_RTX, 0, OPTAB_LIB_WIDEN);
7626 }
7627
7628 if (increment_value_abs - 1 + add_iteration != 0)
7629 temp_reg = expand_binop (loop_var_mode, add_optab, temp_reg,
7630 GEN_INT (increment_value_abs - 1 + add_iteration),
7631 NULL_RTX, 0, OPTAB_LIB_WIDEN);
7632
7633 if (increment_value_abs != 1)
7634 {
7635 /* ??? This will generate an expensive divide instruction for
7636 most targets. The original authors apparently expected this
7637 to be a shift, since they test for power-of-2 divisors above,
7638 but just naively generating a divide instruction will not give
7639 a shift. It happens to work for the PowerPC target because
7640 the rs6000.md file has a divide pattern that emits shifts.
7641 It will probably not work for any other target. */
7642 iterations_num_reg = expand_binop (loop_var_mode, sdiv_optab,
7643 temp_reg,
7644 GEN_INT (increment_value_abs),
7645 NULL_RTX, 0, OPTAB_LIB_WIDEN);
7646 }
7647 else
7648 iterations_num_reg = temp_reg;
7649 }
7650 sequence = gen_sequence ();
7651 end_sequence ();
7652 emit_insn_before (sequence, loop_start);
7653 instrument_loop_bct (loop_start, loop_end, iterations_num_reg);
7654 }
7655 }
7656
7657 /* instrument loop by inserting a bct in it. This is done in the following way:
7658 1. A new register is created and assigned the hard register number of the count
7659 register.
7660 2. In the head of the loop the new variable is initialized by the value passed in the
7661 loop_num_iterations parameter.
7662 3. At the end of the loop, comparison of the register with 0 is generated.
7663 The created comparison follows the pattern defined for the
7664 decrement_and_branch_on_count insn, so this insn will be generated in assembly
7665 generation phase.
7666 4. The compare&branch on the old variable is deleted. So, if the loop-variable was
7667 not used elsewhere, it will be eliminated by data-flow analisys. */
7668
7669 static void
7670 instrument_loop_bct (loop_start, loop_end, loop_num_iterations)
7671 rtx loop_start, loop_end;
7672 rtx loop_num_iterations;
7673 {
7674 rtx temp_reg1, temp_reg2;
7675 rtx start_label;
7676
7677 rtx sequence;
7678 enum machine_mode loop_var_mode = SImode;
7679
7680 if (HAVE_decrement_and_branch_on_count)
7681 {
7682 if (loop_dump_stream)
7683 fprintf (loop_dump_stream, "Loop: Inserting BCT\n");
7684
7685 /* eliminate the check on the old variable */
7686 delete_insn (PREV_INSN (loop_end));
7687 delete_insn (PREV_INSN (loop_end));
7688
7689 /* insert the label which will delimit the start of the loop */
7690 start_label = gen_label_rtx ();
7691 emit_label_after (start_label, loop_start);
7692
7693 /* insert initialization of the count register into the loop header */
7694 start_sequence ();
7695 temp_reg1 = gen_reg_rtx (loop_var_mode);
7696 emit_insn (gen_move_insn (temp_reg1, loop_num_iterations));
7697
7698 /* this will be count register */
7699 temp_reg2 = gen_rtx_REG (loop_var_mode, COUNT_REGISTER_REGNUM);
7700 /* we have to move the value to the count register from an GPR
7701 because rtx pointed to by loop_num_iterations could contain
7702 expression which cannot be moved into count register */
7703 emit_insn (gen_move_insn (temp_reg2, temp_reg1));
7704
7705 sequence = gen_sequence ();
7706 end_sequence ();
7707 emit_insn_after (sequence, loop_start);
7708
7709 /* insert new comparison on the count register instead of the
7710 old one, generating the needed BCT pattern (that will be
7711 later recognized by assembly generation phase). */
7712 emit_jump_insn_before (gen_decrement_and_branch_on_count (temp_reg2, start_label),
7713 loop_end);
7714 LABEL_NUSES (start_label)++;
7715 }
7716
7717 }
7718 #endif /* HAVE_decrement_and_branch_on_count */
7719
7720 #endif /* HAIFA */
7721
7722 /* Scan the function and determine whether it has indirect (computed) jumps.
7723
7724 This is taken mostly from flow.c; similar code exists elsewhere
7725 in the compiler. It may be useful to put this into rtlanal.c. */
7726 static int
7727 indirect_jump_in_function_p (start)
7728 rtx start;
7729 {
7730 rtx insn;
7731
7732 for (insn = start; insn; insn = NEXT_INSN (insn))
7733 if (computed_jump_p (insn))
7734 return 1;
7735
7736 return 0;
7737 }