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