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