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