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