1 /* Move constant computations out of loops.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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)
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.
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This is the loop optimization pass of the compiler.
22 It finds invariant computations within loops and moves them
23 to the beginning of the loop. Then it identifies basic and
24 general induction variables. Strength reduction is applied to the general
25 induction variables, and induction variable elimination is applied to
26 the basic induction variables.
28 It also finds cases where
29 a register is set within the loop by zero-extending a narrower value
30 and changes these to zero the entire register once before the loop
31 and merely copy the low part within the loop.
33 Most of the complexity is in heuristics to decide when it is worth
34 while to do these things. */
41 #include "insn-config.h"
42 #include "insn-flags.h"
44 #include "hard-reg-set.h"
50 /* Vector mapping INSN_UIDs to luids.
51 The luids are like uids but increase monotonically always.
52 We use them to see whether a jump comes from outside a given loop. */
56 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
57 number the insn is contained in. */
61 /* 1 + largest uid of any insn. */
65 /* 1 + luid of last insn. */
69 /* Number of loops detected in current function. Used as index to the
72 static int max_loop_num
;
74 /* Indexed by loop number, contains the first and last insn of each loop. */
76 static rtx
*loop_number_loop_starts
, *loop_number_loop_ends
;
78 /* For each loop, gives the containing loop number, -1 if none. */
82 /* Indexed by loop number, contains a nonzero value if the "loop" isn't
83 really a loop (an insn outside the loop branches into it). */
85 static char *loop_invalid
;
87 /* Indexed by loop number, links together all LABEL_REFs which refer to
88 code labels outside the loop. Used by routines that need to know all
89 loop exits, such as final_biv_value and final_giv_value.
91 This does not include loop exits due to return instructions. This is
92 because all bivs and givs are pseudos, and hence must be dead after a
93 return, so the presense of a return does not affect any of the
94 optimizations that use this info. It is simpler to just not include return
95 instructions on this list. */
97 rtx
*loop_number_exit_labels
;
99 /* Holds the number of loop iterations. It is zero if the number could not be
100 calculated. Must be unsigned since the number of iterations can
101 be as high as 2^wordsize-1. For loops with a wider iterator, this number
102 will will be zero if the number of loop iterations is too large for an
103 unsigned integer to hold. */
105 unsigned HOST_WIDE_INT loop_n_iterations
;
107 /* Nonzero if there is a subroutine call in the current loop.
108 (unknown_address_altered is also nonzero in this case.) */
110 static int loop_has_call
;
112 /* Nonzero if there is a volatile memory reference in the current
115 static int loop_has_volatile
;
117 /* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
118 current loop. A continue statement will generate a branch to
119 NEXT_INSN (loop_continue). */
121 static rtx loop_continue
;
123 /* Indexed by register number, contains the number of times the reg
124 is set during the loop being scanned.
125 During code motion, a negative value indicates a reg that has been
126 made a candidate; in particular -2 means that it is an candidate that
127 we know is equal to a constant and -1 means that it is an candidate
128 not known equal to a constant.
129 After code motion, regs moved have 0 (which is accurate now)
130 while the failed candidates have the original number of times set.
132 Therefore, at all times, == 0 indicates an invariant register;
133 < 0 a conditionally invariant one. */
135 static short *n_times_set
;
137 /* Original value of n_times_set; same except that this value
138 is not set negative for a reg whose sets have been made candidates
139 and not set to 0 for a reg that is moved. */
141 static short *n_times_used
;
143 /* Index by register number, 1 indicates that the register
144 cannot be moved or strength reduced. */
146 static char *may_not_optimize
;
148 /* Nonzero means reg N has already been moved out of one loop.
149 This reduces the desire to move it out of another. */
151 static char *moved_once
;
153 /* Array of MEMs that are stored in this loop. If there are too many to fit
154 here, we just turn on unknown_address_altered. */
156 #define NUM_STORES 20
157 static rtx loop_store_mems
[NUM_STORES
];
159 /* Index of first available slot in above array. */
160 static int loop_store_mems_idx
;
162 /* Nonzero if we don't know what MEMs were changed in the current loop.
163 This happens if the loop contains a call (in which case `loop_has_call'
164 will also be set) or if we store into more than NUM_STORES MEMs. */
166 static int unknown_address_altered
;
168 /* Count of movable (i.e. invariant) instructions discovered in the loop. */
169 static int num_movables
;
171 /* Count of memory write instructions discovered in the loop. */
172 static int num_mem_sets
;
174 /* Number of loops contained within the current one, including itself. */
175 static int loops_enclosed
;
177 /* Bound on pseudo register number before loop optimization.
178 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
179 int max_reg_before_loop
;
181 /* This obstack is used in product_cheap_p to allocate its rtl. It
182 may call gen_reg_rtx which, in turn, may reallocate regno_reg_rtx.
183 If we used the same obstack that it did, we would be deallocating
186 static struct obstack temp_obstack
;
188 /* This is where the pointer to the obstack being used for RTL is stored. */
190 extern struct obstack
*rtl_obstack
;
192 #define obstack_chunk_alloc xmalloc
193 #define obstack_chunk_free free
195 extern char *oballoc ();
197 /* During the analysis of a loop, a chain of `struct movable's
198 is made to record all the movable insns found.
199 Then the entire chain can be scanned to decide which to move. */
203 rtx insn
; /* A movable insn */
204 rtx set_src
; /* The expression this reg is set from. */
205 rtx set_dest
; /* The destination of this SET. */
206 rtx dependencies
; /* When INSN is libcall, this is an EXPR_LIST
207 of any registers used within the LIBCALL. */
208 int consec
; /* Number of consecutive following insns
209 that must be moved with this one. */
210 int regno
; /* The register it sets */
211 short lifetime
; /* lifetime of that register;
212 may be adjusted when matching movables
213 that load the same value are found. */
214 short savings
; /* Number of insns we can move for this reg,
215 including other movables that force this
216 or match this one. */
217 unsigned int cond
: 1; /* 1 if only conditionally movable */
218 unsigned int force
: 1; /* 1 means MUST move this insn */
219 unsigned int global
: 1; /* 1 means reg is live outside this loop */
220 /* If PARTIAL is 1, GLOBAL means something different:
221 that the reg is live outside the range from where it is set
222 to the following label. */
223 unsigned int done
: 1; /* 1 inhibits further processing of this */
225 unsigned int partial
: 1; /* 1 means this reg is used for zero-extending.
226 In particular, moving it does not make it
228 unsigned int move_insn
: 1; /* 1 means that we call emit_move_insn to
229 load SRC, rather than copying INSN. */
230 unsigned int is_equiv
: 1; /* 1 means a REG_EQUIV is present on INSN. */
231 enum machine_mode savemode
; /* Nonzero means it is a mode for a low part
232 that we should avoid changing when clearing
233 the rest of the reg. */
234 struct movable
*match
; /* First entry for same value */
235 struct movable
*forces
; /* An insn that must be moved if this is */
236 struct movable
*next
;
239 FILE *loop_dump_stream
;
241 /* Forward declarations. */
243 static void find_and_verify_loops ();
244 static void mark_loop_jump ();
245 static void prescan_loop ();
246 static int reg_in_basic_block_p ();
247 static int consec_sets_invariant_p ();
248 static rtx
libcall_other_reg ();
249 static int labels_in_range_p ();
250 static void count_loop_regs_set ();
251 static void note_addr_stored ();
252 static int loop_reg_used_before_p ();
253 static void scan_loop ();
254 static void replace_call_address ();
255 static rtx
skip_consec_insns ();
256 static int libcall_benefit ();
257 static void ignore_some_movables ();
258 static void force_movables ();
259 static void combine_movables ();
260 static int rtx_equal_for_loop_p ();
261 static void move_movables ();
262 static void strength_reduce ();
263 static int valid_initial_value_p ();
264 static void find_mem_givs ();
265 static void record_biv ();
266 static void check_final_value ();
267 static void record_giv ();
268 static void update_giv_derive ();
269 static void delete_insn_forces ();
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 ();
285 /* Relative gain of eliminating various kinds of operations. */
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. */
299 char *free_point
= (char *) oballoc (1);
300 rtx reg
= gen_rtx (REG
, word_mode
, 0);
301 rtx pow2
= GEN_INT (32);
305 add_cost
= rtx_cost (gen_rtx (PLUS
, word_mode
, reg
, reg
), SET
);
307 /* We multiply by 2 to reconcile the difference in scale between
308 these two ways of computing costs. Otherwise the cost of a copy
309 will be far less than the cost of an add. */
313 /* Free the objects we just allocated. */
316 /* Initialize the obstack used for rtl in product_cheap_p. */
317 gcc_obstack_init (&temp_obstack
);
320 /* Entry point of this file. Perform loop optimization
321 on the current function. F is the first insn of the function
322 and DUMPFILE is a stream for output of a trace of actions taken
323 (or 0 if none should be output). */
326 loop_optimize (f
, dumpfile
)
327 /* f is the first instruction of a chain of insns for one function */
336 loop_dump_stream
= dumpfile
;
338 init_recog_no_volatile ();
339 init_alias_analysis ();
341 max_reg_before_loop
= max_reg_num ();
343 moved_once
= (char *) alloca (max_reg_before_loop
);
344 bzero (moved_once
, max_reg_before_loop
);
348 /* Count the number of loops. */
351 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
353 if (GET_CODE (insn
) == NOTE
354 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
358 /* Don't waste time if no loops. */
359 if (max_loop_num
== 0)
362 /* Get size to use for tables indexed by uids.
363 Leave some space for labels allocated by find_and_verify_loops. */
364 max_uid_for_loop
= get_max_uid () + 1 + max_loop_num
* 4;
366 uid_luid
= (int *) alloca (max_uid_for_loop
* sizeof (int));
367 uid_loop_num
= (int *) alloca (max_uid_for_loop
* sizeof (int));
369 bzero (uid_luid
, max_uid_for_loop
* sizeof (int));
370 bzero (uid_loop_num
, max_uid_for_loop
* sizeof (int));
372 /* Allocate tables for recording each loop. We set each entry, so they need
374 loop_number_loop_starts
= (rtx
*) alloca (max_loop_num
* sizeof (rtx
));
375 loop_number_loop_ends
= (rtx
*) alloca (max_loop_num
* sizeof (rtx
));
376 loop_outer_loop
= (int *) alloca (max_loop_num
* sizeof (int));
377 loop_invalid
= (char *) alloca (max_loop_num
* sizeof (char));
378 loop_number_exit_labels
= (rtx
*) alloca (max_loop_num
* sizeof (rtx
));
380 /* Find and process each loop.
381 First, find them, and record them in order of their beginnings. */
382 find_and_verify_loops (f
);
384 /* Now find all register lifetimes. This must be done after
385 find_and_verify_loops, because it might reorder the insns in the
387 reg_scan (f
, max_reg_num (), 1);
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. */
395 for (insn
= f
, i
= 0; insn
; insn
= NEXT_INSN (insn
))
398 if (GET_CODE (insn
) != NOTE
399 || NOTE_LINE_NUMBER (insn
) <= 0)
400 uid_luid
[INSN_UID (insn
)] = ++i
;
402 /* Give a line number note the same luid as preceding insn. */
403 uid_luid
[INSN_UID (insn
)] = i
;
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
++)
415 uid_luid
[0] = uid_luid
[i
];
416 if (uid_luid
[0] != 0)
419 for (i
= 0; i
< max_uid_for_loop
; i
++)
420 if (uid_luid
[i
] == 0)
421 uid_luid
[i
] = uid_luid
[i
- 1];
423 /* Create a mapping from loops to BLOCK tree nodes. */
424 if (flag_unroll_loops
&& write_symbols
!= NO_DEBUG
)
425 find_loop_tree_blocks ();
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
],
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 ();
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. */
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. */
452 scan_loop (loop_start
, end
, nregs
)
458 /* 1 if we are scanning insns that could be executed zero times. */
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. */
463 /* For a rotated loop that is entered near the bottom,
464 this is the label at the top. Otherwise it is zero. */
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. */
470 /* Number of insns in the loop. */
475 /* The SET from an insn, if it is the only SET in the insn. */
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. */
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
489 rtx
*reg_single_usage
= 0;
491 n_times_set
= (short *) alloca (nregs
* sizeof (short));
492 n_times_used
= (short *) alloca (nregs
* sizeof (short));
493 may_not_optimize
= (char *) alloca (nregs
);
495 /* Determine whether this loop starts with a jump down to a test at
496 the end. This will occur for a small number of loops with a test
497 that is too complex to duplicate in front of the loop.
499 We search for the first insn or label in the loop, skipping NOTEs.
500 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
501 (because we might have a loop executed only once that contains a
502 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
503 (in case we have a degenerate loop).
505 Note that if we mistakenly think that a loop is entered at the top
506 when, in fact, it is entered at the exit test, the only effect will be
507 slightly poorer optimization. Making the opposite error can generate
508 incorrect code. Since very few loops now start with a jump to the
509 exit test, the code here to detect that case is very conservative. */
511 for (p
= NEXT_INSN (loop_start
);
513 && GET_CODE (p
) != CODE_LABEL
&& GET_RTX_CLASS (GET_CODE (p
)) != 'i'
514 && (GET_CODE (p
) != NOTE
515 || (NOTE_LINE_NUMBER (p
) != NOTE_INSN_LOOP_BEG
516 && NOTE_LINE_NUMBER (p
) != NOTE_INSN_LOOP_END
));
522 /* Set up variables describing this loop. */
523 prescan_loop (loop_start
, end
);
524 threshold
= (loop_has_call
? 1 : 2) * (1 + n_non_fixed_regs
);
526 /* If loop has a jump before the first label,
527 the true entry is the target of that jump.
528 Start scan from there.
529 But record in LOOP_TOP the place where the end-test jumps
530 back to so we can scan that after the end of the loop. */
531 if (GET_CODE (p
) == JUMP_INSN
)
535 /* Loop entry must be unconditional jump (and not a RETURN) */
537 && JUMP_LABEL (p
) != 0
538 /* Check to see whether the jump actually
539 jumps out of the loop (meaning it's no loop).
540 This case can happen for things like
541 do {..} while (0). If this label was generated previously
542 by loop, we can't tell anything about it and have to reject
544 && INSN_UID (JUMP_LABEL (p
)) < max_uid_for_loop
545 && INSN_LUID (JUMP_LABEL (p
)) >= INSN_LUID (loop_start
)
546 && INSN_LUID (JUMP_LABEL (p
)) < INSN_LUID (end
))
548 loop_top
= next_label (scan_start
);
549 scan_start
= JUMP_LABEL (p
);
553 /* If SCAN_START was an insn created by loop, we don't know its luid
554 as required by loop_reg_used_before_p. So skip such loops. (This
555 test may never be true, but it's best to play it safe.)
557 Also, skip loops where we do not start scanning at a label. This
558 test also rejects loops starting with a JUMP_INSN that failed the
561 if (INSN_UID (scan_start
) >= max_uid_for_loop
562 || GET_CODE (scan_start
) != CODE_LABEL
)
564 if (loop_dump_stream
)
565 fprintf (loop_dump_stream
, "\nLoop from %d to %d is phony.\n\n",
566 INSN_UID (loop_start
), INSN_UID (end
));
570 /* Count number of times each reg is set during this loop.
571 Set may_not_optimize[I] if it is not safe to move out
572 the setting of register I. If this loop has calls, set
573 reg_single_usage[I]. */
575 bzero (n_times_set
, nregs
* sizeof (short));
576 bzero (may_not_optimize
, nregs
);
580 reg_single_usage
= (rtx
*) alloca (nregs
* sizeof (rtx
));
581 bzero (reg_single_usage
, nregs
* sizeof (rtx
));
584 count_loop_regs_set (loop_top
? loop_top
: loop_start
, end
,
585 may_not_optimize
, reg_single_usage
, &insn_count
, nregs
);
587 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
588 may_not_optimize
[i
] = 1, n_times_set
[i
] = 1;
589 bcopy (n_times_set
, n_times_used
, nregs
* sizeof (short));
591 if (loop_dump_stream
)
593 fprintf (loop_dump_stream
, "\nLoop from %d to %d: %d real insns.\n",
594 INSN_UID (loop_start
), INSN_UID (end
), insn_count
);
596 fprintf (loop_dump_stream
, "Continue at insn %d.\n",
597 INSN_UID (loop_continue
));
600 /* Scan through the loop finding insns that are safe to move.
601 Set n_times_set negative for the reg being set, so that
602 this reg will be considered invariant for subsequent insns.
603 We consider whether subsequent insns use the reg
604 in deciding whether it is worth actually moving.
606 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
607 and therefore it is possible that the insns we are scanning
608 would never be executed. At such times, we must make sure
609 that it is safe to execute the insn once instead of zero times.
610 When MAYBE_NEVER is 0, all insns will be executed at least once
611 so that is not a problem. */
617 /* At end of a straight-in loop, we are done.
618 At end of a loop entered at the bottom, scan the top. */
624 p
= NEXT_INSN (loop_top
);
631 if (GET_RTX_CLASS (GET_CODE (p
)) == 'i'
632 && find_reg_note (p
, REG_LIBCALL
, NULL_RTX
))
634 else if (GET_RTX_CLASS (GET_CODE (p
)) == 'i'
635 && find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
638 if (GET_CODE (p
) == INSN
639 && (set
= single_set (p
))
640 && GET_CODE (SET_DEST (set
)) == REG
641 && ! may_not_optimize
[REGNO (SET_DEST (set
))])
646 rtx src
= SET_SRC (set
);
647 rtx dependencies
= 0;
649 /* Figure out what to use as a source of this insn. If a REG_EQUIV
650 note is given or if a REG_EQUAL note with a constant operand is
651 specified, use it as the source and mark that we should move
652 this insn by calling emit_move_insn rather that duplicating the
655 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
657 temp
= find_reg_note (p
, REG_EQUIV
, NULL_RTX
);
659 src
= XEXP (temp
, 0), move_insn
= 1;
662 temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
);
663 if (temp
&& CONSTANT_P (XEXP (temp
, 0)))
664 src
= XEXP (temp
, 0), move_insn
= 1;
665 if (temp
&& find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
667 src
= XEXP (temp
, 0);
668 /* A libcall block can use regs that don't appear in
669 the equivalent expression. To move the libcall,
670 we must move those regs too. */
671 dependencies
= libcall_other_reg (p
, src
);
675 /* Don't try to optimize a register that was made
676 by loop-optimization for an inner loop.
677 We don't know its life-span, so we can't compute the benefit. */
678 if (REGNO (SET_DEST (set
)) >= max_reg_before_loop
)
680 /* In order to move a register, we need to have one of three cases:
681 (1) it is used only in the same basic block as the set
682 (2) it is not a user variable.
683 (3) the set is guaranteed to be executed once the loop starts,
684 and the reg is not used until after that. */
685 else if (! ((! maybe_never
686 && ! loop_reg_used_before_p (set
, p
, loop_start
,
688 || ! REG_USERVAR_P (SET_DEST (PATTERN (p
)))
689 || reg_in_basic_block_p (p
, SET_DEST (PATTERN (p
)))))
691 else if ((tem
= invariant_p (src
))
692 && (dependencies
== 0
693 || (tem2
= invariant_p (dependencies
)) != 0)
694 && (n_times_set
[REGNO (SET_DEST (set
))] == 1
696 = consec_sets_invariant_p (SET_DEST (set
),
697 n_times_set
[REGNO (SET_DEST (set
))],
699 /* If the insn can cause a trap (such as divide by zero),
700 can't move it unless it's guaranteed to be executed
701 once loop is entered. Even a function call might
702 prevent the trap insn from being reached
703 (since it might exit!) */
704 && ! ((maybe_never
|| call_passed
)
705 && may_trap_p (src
)))
707 register struct movable
*m
;
708 register int regno
= REGNO (SET_DEST (set
));
710 /* A potential lossage is where we have a case where two insns
711 can be combined as long as they are both in the loop, but
712 we move one of them outside the loop. For large loops,
713 this can lose. The most common case of this is the address
714 of a function being called.
716 Therefore, if this register is marked as being used exactly
717 once if we are in a loop with calls (a "large loop"), see if
718 we can replace the usage of this register with the source
719 of this SET. If we can, delete this insn.
721 Don't do this if P has a REG_RETVAL note or if we have
722 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
724 if (reg_single_usage
&& reg_single_usage
[regno
] != 0
725 && reg_single_usage
[regno
] != const0_rtx
726 && regno_first_uid
[regno
] == INSN_UID (p
)
727 && (regno_last_uid
[regno
]
728 == INSN_UID (reg_single_usage
[regno
]))
729 && n_times_set
[REGNO (SET_DEST (set
))] == 1
730 && ! side_effects_p (SET_SRC (set
))
731 && ! find_reg_note (p
, REG_RETVAL
, NULL_RTX
)
732 #ifdef SMALL_REGISTER_CLASSES
733 && ! (GET_CODE (SET_SRC (set
)) == REG
734 && REGNO (SET_SRC (set
)) < FIRST_PSEUDO_REGISTER
)
736 /* This test is not redundant; SET_SRC (set) might be
737 a call-clobbered register and the life of REGNO
738 might span a call. */
739 && ! modified_between_p (SET_SRC (set
), p
,
740 reg_single_usage
[regno
])
741 && validate_replace_rtx (SET_DEST (set
), SET_SRC (set
),
742 reg_single_usage
[regno
]))
744 /* Replace any usage in a REG_EQUAL note. */
745 REG_NOTES (reg_single_usage
[regno
])
746 = replace_rtx (REG_NOTES (reg_single_usage
[regno
]),
747 SET_DEST (set
), SET_SRC (set
));
750 NOTE_LINE_NUMBER (p
) = NOTE_INSN_DELETED
;
751 NOTE_SOURCE_FILE (p
) = 0;
752 n_times_set
[regno
] = 0;
756 m
= (struct movable
*) alloca (sizeof (struct movable
));
760 m
->dependencies
= dependencies
;
761 m
->set_dest
= SET_DEST (set
);
763 m
->consec
= n_times_set
[REGNO (SET_DEST (set
))] - 1;
767 m
->move_insn
= move_insn
;
768 m
->is_equiv
= (find_reg_note (p
, REG_EQUIV
, NULL_RTX
) != 0);
769 m
->savemode
= VOIDmode
;
771 /* Set M->cond if either invariant_p or consec_sets_invariant_p
772 returned 2 (only conditionally invariant). */
773 m
->cond
= ((tem
| tem1
| tem2
) > 1);
774 m
->global
= (uid_luid
[regno_last_uid
[regno
]] > INSN_LUID (end
)
775 || uid_luid
[regno_first_uid
[regno
]] < INSN_LUID (loop_start
));
777 m
->lifetime
= (uid_luid
[regno_last_uid
[regno
]]
778 - uid_luid
[regno_first_uid
[regno
]]);
779 m
->savings
= n_times_used
[regno
];
780 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
781 m
->savings
+= libcall_benefit (p
);
782 n_times_set
[regno
] = move_insn
? -2 : -1;
783 /* Add M to the end of the chain MOVABLES. */
787 last_movable
->next
= m
;
792 /* Skip this insn, not checking REG_LIBCALL notes. */
794 /* Skip the consecutive insns, if there are any. */
795 p
= skip_consec_insns (p
, m
->consec
);
796 /* Back up to the last insn of the consecutive group. */
797 p
= prev_nonnote_insn (p
);
799 /* We must now reset m->move_insn, m->is_equiv, and possibly
800 m->set_src to correspond to the effects of all the
802 temp
= find_reg_note (p
, REG_EQUIV
, NULL_RTX
);
804 m
->set_src
= XEXP (temp
, 0), m
->move_insn
= 1;
807 temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
);
808 if (temp
&& CONSTANT_P (XEXP (temp
, 0)))
809 m
->set_src
= XEXP (temp
, 0), m
->move_insn
= 1;
814 m
->is_equiv
= (find_reg_note (p
, REG_EQUIV
, NULL_RTX
) != 0);
817 /* If this register is always set within a STRICT_LOW_PART
818 or set to zero, then its high bytes are constant.
819 So clear them outside the loop and within the loop
820 just load the low bytes.
821 We must check that the machine has an instruction to do so.
822 Also, if the value loaded into the register
823 depends on the same register, this cannot be done. */
824 else if (SET_SRC (set
) == const0_rtx
825 && GET_CODE (NEXT_INSN (p
)) == INSN
826 && (set1
= single_set (NEXT_INSN (p
)))
827 && GET_CODE (set1
) == SET
828 && (GET_CODE (SET_DEST (set1
)) == STRICT_LOW_PART
)
829 && (GET_CODE (XEXP (SET_DEST (set1
), 0)) == SUBREG
)
830 && (SUBREG_REG (XEXP (SET_DEST (set1
), 0))
832 && !reg_mentioned_p (SET_DEST (set
), SET_SRC (set1
)))
834 register int regno
= REGNO (SET_DEST (set
));
835 if (n_times_set
[regno
] == 2)
837 register struct movable
*m
;
838 m
= (struct movable
*) alloca (sizeof (struct movable
));
841 m
->set_dest
= SET_DEST (set
);
849 /* If the insn may not be executed on some cycles,
850 we can't clear the whole reg; clear just high part.
851 Not even if the reg is used only within this loop.
858 Clearing x before the inner loop could clobber a value
859 being saved from the last time around the outer loop.
860 However, if the reg is not used outside this loop
861 and all uses of the register are in the same
862 basic block as the store, there is no problem.
864 If this insn was made by loop, we don't know its
865 INSN_LUID and hence must make a conservative
867 m
->global
= (INSN_UID (p
) >= max_uid_for_loop
868 || (uid_luid
[regno_last_uid
[regno
]]
870 || (uid_luid
[regno_first_uid
[regno
]]
872 || (labels_in_range_p
873 (p
, uid_luid
[regno_first_uid
[regno
]])));
874 if (maybe_never
&& m
->global
)
875 m
->savemode
= GET_MODE (SET_SRC (set1
));
877 m
->savemode
= VOIDmode
;
881 m
->lifetime
= (uid_luid
[regno_last_uid
[regno
]]
882 - uid_luid
[regno_first_uid
[regno
]]);
884 n_times_set
[regno
] = -1;
885 /* Add M to the end of the chain MOVABLES. */
889 last_movable
->next
= m
;
894 /* Past a call insn, we get to insns which might not be executed
895 because the call might exit. This matters for insns that trap.
896 Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
897 so they don't count. */
898 else if (GET_CODE (p
) == CALL_INSN
&& ! in_libcall
)
900 /* Past a label or a jump, we get to insns for which we
901 can't count on whether or how many times they will be
902 executed during each iteration. Therefore, we can
903 only move out sets of trivial variables
904 (those not used after the loop). */
905 /* This code appears in three places, once in scan_loop, and twice
906 in strength_reduce. */
907 else if ((GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
)
908 /* If we enter the loop in the middle, and scan around to the
909 beginning, don't set maybe_never for that. This must be an
910 unconditional jump, otherwise the code at the top of the
911 loop might never be executed. Unconditional jumps are
912 followed a by barrier then loop end. */
913 && ! (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
) == loop_top
914 && NEXT_INSN (NEXT_INSN (p
)) == end
915 && simplejump_p (p
)))
917 /* At the virtual top of a converted loop, insns are again known to
918 be executed: logically, the loop begins here even though the exit
919 code has been duplicated. */
920 else if (GET_CODE (p
) == NOTE
921 && NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_VTOP
)
922 maybe_never
= call_passed
= 0;
925 /* If one movable subsumes another, ignore that other. */
927 ignore_some_movables (movables
);
929 /* For each movable insn, see if the reg that it loads
930 leads when it dies right into another conditionally movable insn.
931 If so, record that the second insn "forces" the first one,
932 since the second can be moved only if the first is. */
934 force_movables (movables
);
936 /* See if there are multiple movable insns that load the same value.
937 If there are, make all but the first point at the first one
938 through the `match' field, and add the priorities of them
939 all together as the priority of the first. */
941 combine_movables (movables
, nregs
);
943 /* Now consider each movable insn to decide whether it is worth moving.
944 Store 0 in n_times_set for each reg that is moved. */
946 move_movables (movables
, threshold
,
947 insn_count
, loop_start
, end
, nregs
);
949 /* Now candidates that still are negative are those not moved.
950 Change n_times_set to indicate that those are not actually invariant. */
951 for (i
= 0; i
< nregs
; i
++)
952 if (n_times_set
[i
] < 0)
953 n_times_set
[i
] = n_times_used
[i
];
955 if (flag_strength_reduce
)
956 strength_reduce (scan_start
, end
, loop_top
,
957 insn_count
, loop_start
, end
);
960 /* Add elements to *OUTPUT to record all the pseudo-regs
961 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
964 record_excess_regs (in_this
, not_in_this
, output
)
965 rtx in_this
, not_in_this
;
972 code
= GET_CODE (in_this
);
986 if (REGNO (in_this
) >= FIRST_PSEUDO_REGISTER
987 && ! reg_mentioned_p (in_this
, not_in_this
))
988 *output
= gen_rtx (EXPR_LIST
, VOIDmode
, in_this
, *output
);
992 fmt
= GET_RTX_FORMAT (code
);
993 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1000 for (j
= 0; j
< XVECLEN (in_this
, i
); j
++)
1001 record_excess_regs (XVECEXP (in_this
, i
, j
), not_in_this
, output
);
1005 record_excess_regs (XEXP (in_this
, i
), not_in_this
, output
);
1011 /* Check what regs are referred to in the libcall block ending with INSN,
1012 aside from those mentioned in the equivalent value.
1013 If there are none, return 0.
1014 If there are one or more, return an EXPR_LIST containing all of them. */
1017 libcall_other_reg (insn
, equiv
)
1020 rtx note
= find_reg_note (insn
, REG_RETVAL
, NULL_RTX
);
1021 rtx p
= XEXP (note
, 0);
1024 /* First, find all the regs used in the libcall block
1025 that are not mentioned as inputs to the result. */
1029 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
1030 || GET_CODE (p
) == CALL_INSN
)
1031 record_excess_regs (PATTERN (p
), equiv
, &output
);
1038 /* Return 1 if all uses of REG
1039 are between INSN and the end of the basic block. */
1042 reg_in_basic_block_p (insn
, reg
)
1045 int regno
= REGNO (reg
);
1048 if (regno_first_uid
[regno
] != INSN_UID (insn
))
1051 /* Search this basic block for the already recorded last use of the reg. */
1052 for (p
= insn
; p
; p
= NEXT_INSN (p
))
1054 switch (GET_CODE (p
))
1061 /* Ordinary insn: if this is the last use, we win. */
1062 if (regno_last_uid
[regno
] == INSN_UID (p
))
1067 /* Jump insn: if this is the last use, we win. */
1068 if (regno_last_uid
[regno
] == INSN_UID (p
))
1070 /* Otherwise, it's the end of the basic block, so we lose. */
1075 /* It's the end of the basic block, so we lose. */
1080 /* The "last use" doesn't follow the "first use"?? */
1084 /* Compute the benefit of eliminating the insns in the block whose
1085 last insn is LAST. This may be a group of insns used to compute a
1086 value directly or can contain a library call. */
1089 libcall_benefit (last
)
1095 for (insn
= XEXP (find_reg_note (last
, REG_RETVAL
, NULL_RTX
), 0);
1096 insn
!= last
; insn
= NEXT_INSN (insn
))
1098 if (GET_CODE (insn
) == CALL_INSN
)
1099 benefit
+= 10; /* Assume at least this many insns in a library
1101 else if (GET_CODE (insn
) == INSN
1102 && GET_CODE (PATTERN (insn
)) != USE
1103 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
1110 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1113 skip_consec_insns (insn
, count
)
1117 for (; count
> 0; count
--)
1121 /* If first insn of libcall sequence, skip to end. */
1122 /* Do this at start of loop, since INSN is guaranteed to
1124 if (GET_CODE (insn
) != NOTE
1125 && (temp
= find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
)))
1126 insn
= XEXP (temp
, 0);
1128 do insn
= NEXT_INSN (insn
);
1129 while (GET_CODE (insn
) == NOTE
);
1135 /* Ignore any movable whose insn falls within a libcall
1136 which is part of another movable.
1137 We make use of the fact that the movable for the libcall value
1138 was made later and so appears later on the chain. */
1141 ignore_some_movables (movables
)
1142 struct movable
*movables
;
1144 register struct movable
*m
, *m1
;
1146 for (m
= movables
; m
; m
= m
->next
)
1148 /* Is this a movable for the value of a libcall? */
1149 rtx note
= find_reg_note (m
->insn
, REG_RETVAL
, NULL_RTX
);
1153 /* Check for earlier movables inside that range,
1154 and mark them invalid. We cannot use LUIDs here because
1155 insns created by loop.c for prior loops don't have LUIDs.
1156 Rather than reject all such insns from movables, we just
1157 explicitly check each insn in the libcall (since invariant
1158 libcalls aren't that common). */
1159 for (insn
= XEXP (note
, 0); insn
!= m
->insn
; insn
= NEXT_INSN (insn
))
1160 for (m1
= movables
; m1
!= m
; m1
= m1
->next
)
1161 if (m1
->insn
== insn
)
1167 /* For each movable insn, see if the reg that it loads
1168 leads when it dies right into another conditionally movable insn.
1169 If so, record that the second insn "forces" the first one,
1170 since the second can be moved only if the first is. */
1173 force_movables (movables
)
1174 struct movable
*movables
;
1176 register struct movable
*m
, *m1
;
1177 for (m1
= movables
; m1
; m1
= m1
->next
)
1178 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1179 if (!m1
->partial
&& !m1
->done
)
1181 int regno
= m1
->regno
;
1182 for (m
= m1
->next
; m
; m
= m
->next
)
1183 /* ??? Could this be a bug? What if CSE caused the
1184 register of M1 to be used after this insn?
1185 Since CSE does not update regno_last_uid,
1186 this insn M->insn might not be where it dies.
1187 But very likely this doesn't matter; what matters is
1188 that M's reg is computed from M1's reg. */
1189 if (INSN_UID (m
->insn
) == regno_last_uid
[regno
]
1192 if (m
!= 0 && m
->set_src
== m1
->set_dest
1193 /* If m->consec, m->set_src isn't valid. */
1197 /* Increase the priority of the moving the first insn
1198 since it permits the second to be moved as well. */
1202 m1
->lifetime
+= m
->lifetime
;
1203 m1
->savings
+= m1
->savings
;
1208 /* Find invariant expressions that are equal and can be combined into
1212 combine_movables (movables
, nregs
)
1213 struct movable
*movables
;
1216 register struct movable
*m
;
1217 char *matched_regs
= (char *) alloca (nregs
);
1218 enum machine_mode mode
;
1220 /* Regs that are set more than once are not allowed to match
1221 or be matched. I'm no longer sure why not. */
1222 /* Perhaps testing m->consec_sets would be more appropriate here? */
1224 for (m
= movables
; m
; m
= m
->next
)
1225 if (m
->match
== 0 && n_times_used
[m
->regno
] == 1 && !m
->partial
)
1227 register struct movable
*m1
;
1228 int regno
= m
->regno
;
1229 rtx reg_note
, reg_note1
;
1231 bzero (matched_regs
, nregs
);
1232 matched_regs
[regno
] = 1;
1234 for (m1
= movables
; m1
; m1
= m1
->next
)
1235 if (m
!= m1
&& m1
->match
== 0 && n_times_used
[m1
->regno
] == 1
1236 /* A reg used outside the loop mustn't be eliminated. */
1238 /* A reg used for zero-extending mustn't be eliminated. */
1240 && (matched_regs
[m1
->regno
]
1243 /* Can combine regs with different modes loaded from the
1244 same constant only if the modes are the same or
1245 if both are integer modes with M wider or the same
1246 width as M1. The check for integer is redundant, but
1247 safe, since the only case of differing destination
1248 modes with equal sources is when both sources are
1249 VOIDmode, i.e., CONST_INT. */
1250 (GET_MODE (m
->set_dest
) == GET_MODE (m1
->set_dest
)
1251 || (GET_MODE_CLASS (GET_MODE (m
->set_dest
)) == MODE_INT
1252 && GET_MODE_CLASS (GET_MODE (m1
->set_dest
)) == MODE_INT
1253 && (GET_MODE_BITSIZE (GET_MODE (m
->set_dest
))
1254 >= GET_MODE_BITSIZE (GET_MODE (m1
->set_dest
)))))
1255 /* See if the source of M1 says it matches M. */
1256 && ((GET_CODE (m1
->set_src
) == REG
1257 && matched_regs
[REGNO (m1
->set_src
)])
1258 || rtx_equal_for_loop_p (m
->set_src
, m1
->set_src
,
1260 && ((m
->dependencies
== m1
->dependencies
)
1261 || rtx_equal_p (m
->dependencies
, m1
->dependencies
)))
1263 m
->lifetime
+= m1
->lifetime
;
1264 m
->savings
+= m1
->savings
;
1267 matched_regs
[m1
->regno
] = 1;
1271 /* Now combine the regs used for zero-extension.
1272 This can be done for those not marked `global'
1273 provided their lives don't overlap. */
1275 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1276 mode
= GET_MODE_WIDER_MODE (mode
))
1278 register struct movable
*m0
= 0;
1280 /* Combine all the registers for extension from mode MODE.
1281 Don't combine any that are used outside this loop. */
1282 for (m
= movables
; m
; m
= m
->next
)
1283 if (m
->partial
&& ! m
->global
1284 && mode
== GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m
->insn
)))))
1286 register struct movable
*m1
;
1287 int first
= uid_luid
[regno_first_uid
[m
->regno
]];
1288 int last
= uid_luid
[regno_last_uid
[m
->regno
]];
1292 /* First one: don't check for overlap, just record it. */
1297 /* Make sure they extend to the same mode.
1298 (Almost always true.) */
1299 if (GET_MODE (m
->set_dest
) != GET_MODE (m0
->set_dest
))
1302 /* We already have one: check for overlap with those
1303 already combined together. */
1304 for (m1
= movables
; m1
!= m
; m1
= m1
->next
)
1305 if (m1
== m0
|| (m1
->partial
&& m1
->match
== m0
))
1306 if (! (uid_luid
[regno_first_uid
[m1
->regno
]] > last
1307 || uid_luid
[regno_last_uid
[m1
->regno
]] < first
))
1310 /* No overlap: we can combine this with the others. */
1311 m0
->lifetime
+= m
->lifetime
;
1312 m0
->savings
+= m
->savings
;
1321 /* Return 1 if regs X and Y will become the same if moved. */
1324 regs_match_p (x
, y
, movables
)
1326 struct movable
*movables
;
1330 struct movable
*mx
, *my
;
1332 for (mx
= movables
; mx
; mx
= mx
->next
)
1333 if (mx
->regno
== xn
)
1336 for (my
= movables
; my
; my
= my
->next
)
1337 if (my
->regno
== yn
)
1341 && ((mx
->match
== my
->match
&& mx
->match
!= 0)
1343 || mx
== my
->match
));
1346 /* Return 1 if X and Y are identical-looking rtx's.
1347 This is the Lisp function EQUAL for rtx arguments.
1349 If two registers are matching movables or a movable register and an
1350 equivalent constant, consider them equal. */
1353 rtx_equal_for_loop_p (x
, y
, movables
)
1355 struct movable
*movables
;
1359 register struct movable
*m
;
1360 register enum rtx_code code
;
1365 if (x
== 0 || y
== 0)
1368 code
= GET_CODE (x
);
1370 /* If we have a register and a constant, they may sometimes be
1372 if (GET_CODE (x
) == REG
&& n_times_set
[REGNO (x
)] == -2
1374 for (m
= movables
; m
; m
= m
->next
)
1375 if (m
->move_insn
&& m
->regno
== REGNO (x
)
1376 && rtx_equal_p (m
->set_src
, y
))
1379 else if (GET_CODE (y
) == REG
&& n_times_set
[REGNO (y
)] == -2
1381 for (m
= movables
; m
; m
= m
->next
)
1382 if (m
->move_insn
&& m
->regno
== REGNO (y
)
1383 && rtx_equal_p (m
->set_src
, x
))
1386 /* Otherwise, rtx's of different codes cannot be equal. */
1387 if (code
!= GET_CODE (y
))
1390 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1391 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1393 if (GET_MODE (x
) != GET_MODE (y
))
1396 /* These three types of rtx's can be compared nonrecursively. */
1398 return (REGNO (x
) == REGNO (y
) || regs_match_p (x
, y
, movables
));
1400 if (code
== LABEL_REF
)
1401 return XEXP (x
, 0) == XEXP (y
, 0);
1402 if (code
== SYMBOL_REF
)
1403 return XSTR (x
, 0) == XSTR (y
, 0);
1405 /* Compare the elements. If any pair of corresponding elements
1406 fail to match, return 0 for the whole things. */
1408 fmt
= GET_RTX_FORMAT (code
);
1409 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1414 if (XWINT (x
, i
) != XWINT (y
, i
))
1419 if (XINT (x
, i
) != XINT (y
, i
))
1424 /* Two vectors must have the same length. */
1425 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1428 /* And the corresponding elements must match. */
1429 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1430 if (rtx_equal_for_loop_p (XVECEXP (x
, i
, j
), XVECEXP (y
, i
, j
), movables
) == 0)
1435 if (rtx_equal_for_loop_p (XEXP (x
, i
), XEXP (y
, i
), movables
) == 0)
1440 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1445 /* These are just backpointers, so they don't matter. */
1451 /* It is believed that rtx's at this level will never
1452 contain anything but integers and other rtx's,
1453 except for within LABEL_REFs and SYMBOL_REFs. */
1461 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1462 insns in INSNS which use thet reference. */
1465 add_label_notes (x
, insns
)
1469 enum rtx_code code
= GET_CODE (x
);
1474 if (code
== LABEL_REF
&& !LABEL_REF_NONLOCAL_P (x
))
1476 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
1477 if (reg_mentioned_p (XEXP (x
, 0), insn
))
1478 REG_NOTES (insn
) = gen_rtx (EXPR_LIST
, REG_LABEL
, XEXP (x
, 0),
1483 fmt
= GET_RTX_FORMAT (code
);
1484 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1487 add_label_notes (XEXP (x
, i
), insns
);
1488 else if (fmt
[i
] == 'E')
1489 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
1490 add_label_notes (XVECEXP (x
, i
, j
), insns
);
1494 /* Scan MOVABLES, and move the insns that deserve to be moved.
1495 If two matching movables are combined, replace one reg with the
1496 other throughout. */
1499 move_movables (movables
, threshold
, insn_count
, loop_start
, end
, nregs
)
1500 struct movable
*movables
;
1508 register struct movable
*m
;
1510 /* Map of pseudo-register replacements to handle combining
1511 when we move several insns that load the same value
1512 into different pseudo-registers. */
1513 rtx
*reg_map
= (rtx
*) alloca (nregs
* sizeof (rtx
));
1514 char *already_moved
= (char *) alloca (nregs
);
1516 bzero (already_moved
, nregs
);
1517 bzero (reg_map
, nregs
* sizeof (rtx
));
1521 for (m
= movables
; m
; m
= m
->next
)
1523 /* Describe this movable insn. */
1525 if (loop_dump_stream
)
1527 fprintf (loop_dump_stream
, "Insn %d: regno %d (life %d), ",
1528 INSN_UID (m
->insn
), m
->regno
, m
->lifetime
);
1530 fprintf (loop_dump_stream
, "consec %d, ", m
->consec
);
1532 fprintf (loop_dump_stream
, "cond ");
1534 fprintf (loop_dump_stream
, "force ");
1536 fprintf (loop_dump_stream
, "global ");
1538 fprintf (loop_dump_stream
, "done ");
1540 fprintf (loop_dump_stream
, "move-insn ");
1542 fprintf (loop_dump_stream
, "matches %d ",
1543 INSN_UID (m
->match
->insn
));
1545 fprintf (loop_dump_stream
, "forces %d ",
1546 INSN_UID (m
->forces
->insn
));
1549 /* Count movables. Value used in heuristics in strength_reduce. */
1552 /* Ignore the insn if it's already done (it matched something else).
1553 Otherwise, see if it is now safe to move. */
1557 || (1 == invariant_p (m
->set_src
)
1558 && (m
->dependencies
== 0
1559 || 1 == invariant_p (m
->dependencies
))
1561 || 1 == consec_sets_invariant_p (m
->set_dest
,
1564 && (! m
->forces
|| m
->forces
->done
))
1568 int savings
= m
->savings
;
1570 /* We have an insn that is safe to move.
1571 Compute its desirability. */
1576 if (loop_dump_stream
)
1577 fprintf (loop_dump_stream
, "savings %d ", savings
);
1579 if (moved_once
[regno
])
1583 if (loop_dump_stream
)
1584 fprintf (loop_dump_stream
, "halved since already moved ");
1587 /* An insn MUST be moved if we already moved something else
1588 which is safe only if this one is moved too: that is,
1589 if already_moved[REGNO] is nonzero. */
1591 /* An insn is desirable to move if the new lifetime of the
1592 register is no more than THRESHOLD times the old lifetime.
1593 If it's not desirable, it means the loop is so big
1594 that moving won't speed things up much,
1595 and it is liable to make register usage worse. */
1597 /* It is also desirable to move if it can be moved at no
1598 extra cost because something else was already moved. */
1600 if (already_moved
[regno
]
1601 || (threshold
* savings
* m
->lifetime
) >= insn_count
1602 || (m
->forces
&& m
->forces
->done
1603 && n_times_used
[m
->forces
->regno
] == 1))
1606 register struct movable
*m1
;
1609 /* Now move the insns that set the reg. */
1611 if (m
->partial
&& m
->match
)
1615 /* Find the end of this chain of matching regs.
1616 Thus, we load each reg in the chain from that one reg.
1617 And that reg is loaded with 0 directly,
1618 since it has ->match == 0. */
1619 for (m1
= m
; m1
->match
; m1
= m1
->match
);
1620 newpat
= gen_move_insn (SET_DEST (PATTERN (m
->insn
)),
1621 SET_DEST (PATTERN (m1
->insn
)));
1622 i1
= emit_insn_before (newpat
, loop_start
);
1624 /* Mark the moved, invariant reg as being allowed to
1625 share a hard reg with the other matching invariant. */
1626 REG_NOTES (i1
) = REG_NOTES (m
->insn
);
1627 r1
= SET_DEST (PATTERN (m
->insn
));
1628 r2
= SET_DEST (PATTERN (m1
->insn
));
1629 regs_may_share
= gen_rtx (EXPR_LIST
, VOIDmode
, r1
,
1630 gen_rtx (EXPR_LIST
, VOIDmode
, r2
,
1632 delete_insn (m
->insn
);
1637 if (loop_dump_stream
)
1638 fprintf (loop_dump_stream
, " moved to %d", INSN_UID (i1
));
1640 /* If we are to re-generate the item being moved with a
1641 new move insn, first delete what we have and then emit
1642 the move insn before the loop. */
1643 else if (m
->move_insn
)
1647 for (count
= m
->consec
; count
>= 0; count
--)
1649 /* If this is the first insn of a library call sequence,
1651 if (GET_CODE (p
) != NOTE
1652 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
1655 /* If this is the last insn of a libcall sequence, then
1656 delete every insn in the sequence except the last.
1657 The last insn is handled in the normal manner. */
1658 if (GET_CODE (p
) != NOTE
1659 && (temp
= find_reg_note (p
, REG_RETVAL
, NULL_RTX
)))
1661 temp
= XEXP (temp
, 0);
1663 temp
= delete_insn (temp
);
1666 p
= delete_insn (p
);
1670 emit_move_insn (m
->set_dest
, m
->set_src
);
1671 temp
= get_insns ();
1674 add_label_notes (m
->set_src
, temp
);
1676 i1
= emit_insns_before (temp
, loop_start
);
1677 if (! find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
1679 = gen_rtx (EXPR_LIST
,
1680 m
->is_equiv
? REG_EQUIV
: REG_EQUAL
,
1681 m
->set_src
, REG_NOTES (i1
));
1683 if (loop_dump_stream
)
1684 fprintf (loop_dump_stream
, " moved to %d", INSN_UID (i1
));
1686 /* The more regs we move, the less we like moving them. */
1691 for (count
= m
->consec
; count
>= 0; count
--)
1695 /* If first insn of libcall sequence, skip to end. */
1696 /* Do this at start of loop, since p is guaranteed to
1698 if (GET_CODE (p
) != NOTE
1699 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
1702 /* If last insn of libcall sequence, move all
1703 insns except the last before the loop. The last
1704 insn is handled in the normal manner. */
1705 if (GET_CODE (p
) != NOTE
1706 && (temp
= find_reg_note (p
, REG_RETVAL
, NULL_RTX
)))
1710 rtx fn_address_insn
= 0;
1713 for (temp
= XEXP (temp
, 0); temp
!= p
;
1714 temp
= NEXT_INSN (temp
))
1720 if (GET_CODE (temp
) == NOTE
)
1723 body
= PATTERN (temp
);
1725 /* Find the next insn after TEMP,
1726 not counting USE or NOTE insns. */
1727 for (next
= NEXT_INSN (temp
); next
!= p
;
1728 next
= NEXT_INSN (next
))
1729 if (! (GET_CODE (next
) == INSN
1730 && GET_CODE (PATTERN (next
)) == USE
)
1731 && GET_CODE (next
) != NOTE
)
1734 /* If that is the call, this may be the insn
1735 that loads the function address.
1737 Extract the function address from the insn
1738 that loads it into a register.
1739 If this insn was cse'd, we get incorrect code.
1741 So emit a new move insn that copies the
1742 function address into the register that the
1743 call insn will use. flow.c will delete any
1744 redundant stores that we have created. */
1745 if (GET_CODE (next
) == CALL_INSN
1746 && GET_CODE (body
) == SET
1747 && GET_CODE (SET_DEST (body
)) == REG
1748 && (n
= find_reg_note (temp
, REG_EQUAL
,
1751 fn_reg
= SET_SRC (body
);
1752 if (GET_CODE (fn_reg
) != REG
)
1753 fn_reg
= SET_DEST (body
);
1754 fn_address
= XEXP (n
, 0);
1755 fn_address_insn
= temp
;
1757 /* We have the call insn.
1758 If it uses the register we suspect it might,
1759 load it with the correct address directly. */
1760 if (GET_CODE (temp
) == CALL_INSN
1762 && reg_referenced_p (fn_reg
, body
))
1763 emit_insn_after (gen_move_insn (fn_reg
,
1767 if (GET_CODE (temp
) == CALL_INSN
)
1768 i1
= emit_call_insn_before (body
, loop_start
);
1770 i1
= emit_insn_before (body
, loop_start
);
1773 if (temp
== fn_address_insn
)
1774 fn_address_insn
= i1
;
1775 REG_NOTES (i1
) = REG_NOTES (temp
);
1779 if (m
->savemode
!= VOIDmode
)
1781 /* P sets REG to zero; but we should clear only
1782 the bits that are not covered by the mode
1784 rtx reg
= m
->set_dest
;
1790 (GET_MODE (reg
), and_optab
, reg
,
1791 GEN_INT ((((HOST_WIDE_INT
) 1
1792 << GET_MODE_BITSIZE (m
->savemode
)))
1794 reg
, 1, OPTAB_LIB_WIDEN
);
1798 emit_move_insn (reg
, tem
);
1799 sequence
= gen_sequence ();
1801 i1
= emit_insn_before (sequence
, loop_start
);
1803 else if (GET_CODE (p
) == CALL_INSN
)
1804 i1
= emit_call_insn_before (PATTERN (p
), loop_start
);
1806 i1
= emit_insn_before (PATTERN (p
), loop_start
);
1808 REG_NOTES (i1
) = REG_NOTES (p
);
1813 if (loop_dump_stream
)
1814 fprintf (loop_dump_stream
, " moved to %d",
1818 /* This isn't needed because REG_NOTES is copied
1819 below and is wrong since P might be a PARALLEL. */
1820 if (REG_NOTES (i1
) == 0
1821 && ! m
->partial
/* But not if it's a zero-extend clr. */
1822 && ! m
->global
/* and not if used outside the loop
1823 (since it might get set outside). */
1824 && CONSTANT_P (SET_SRC (PATTERN (p
))))
1826 = gen_rtx (EXPR_LIST
, REG_EQUAL
,
1827 SET_SRC (PATTERN (p
)), REG_NOTES (i1
));
1830 /* If library call, now fix the REG_NOTES that contain
1831 insn pointers, namely REG_LIBCALL on FIRST
1832 and REG_RETVAL on I1. */
1833 if (temp
= find_reg_note (i1
, REG_RETVAL
, NULL_RTX
))
1835 XEXP (temp
, 0) = first
;
1836 temp
= find_reg_note (first
, REG_LIBCALL
, NULL_RTX
);
1837 XEXP (temp
, 0) = i1
;
1841 do p
= NEXT_INSN (p
);
1842 while (p
&& GET_CODE (p
) == NOTE
);
1845 /* The more regs we move, the less we like moving them. */
1849 /* Any other movable that loads the same register
1851 already_moved
[regno
] = 1;
1853 /* This reg has been moved out of one loop. */
1854 moved_once
[regno
] = 1;
1856 /* The reg set here is now invariant. */
1858 n_times_set
[regno
] = 0;
1862 /* Change the length-of-life info for the register
1863 to say it lives at least the full length of this loop.
1864 This will help guide optimizations in outer loops. */
1866 if (uid_luid
[regno_first_uid
[regno
]] > INSN_LUID (loop_start
))
1867 /* This is the old insn before all the moved insns.
1868 We can't use the moved insn because it is out of range
1869 in uid_luid. Only the old insns have luids. */
1870 regno_first_uid
[regno
] = INSN_UID (loop_start
);
1871 if (uid_luid
[regno_last_uid
[regno
]] < INSN_LUID (end
))
1872 regno_last_uid
[regno
] = INSN_UID (end
);
1874 /* Combine with this moved insn any other matching movables. */
1877 for (m1
= movables
; m1
; m1
= m1
->next
)
1882 /* Schedule the reg loaded by M1
1883 for replacement so that shares the reg of M.
1884 If the modes differ (only possible in restricted
1885 circumstances, make a SUBREG. */
1886 if (GET_MODE (m
->set_dest
) == GET_MODE (m1
->set_dest
))
1887 reg_map
[m1
->regno
] = m
->set_dest
;
1890 = gen_lowpart_common (GET_MODE (m1
->set_dest
),
1893 /* Get rid of the matching insn
1894 and prevent further processing of it. */
1897 /* if library call, delete all insn except last, which
1899 if (temp
= find_reg_note (m1
->insn
, REG_RETVAL
,
1902 for (temp
= XEXP (temp
, 0); temp
!= m1
->insn
;
1903 temp
= NEXT_INSN (temp
))
1906 delete_insn (m1
->insn
);
1908 /* Any other movable that loads the same register
1910 already_moved
[m1
->regno
] = 1;
1912 /* The reg merged here is now invariant,
1913 if the reg it matches is invariant. */
1915 n_times_set
[m1
->regno
] = 0;
1918 else if (loop_dump_stream
)
1919 fprintf (loop_dump_stream
, "not desirable");
1921 else if (loop_dump_stream
&& !m
->match
)
1922 fprintf (loop_dump_stream
, "not safe");
1924 if (loop_dump_stream
)
1925 fprintf (loop_dump_stream
, "\n");
1929 new_start
= loop_start
;
1931 /* Go through all the instructions in the loop, making
1932 all the register substitutions scheduled in REG_MAP. */
1933 for (p
= new_start
; p
!= end
; p
= NEXT_INSN (p
))
1934 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
1935 || GET_CODE (p
) == CALL_INSN
)
1937 replace_regs (PATTERN (p
), reg_map
, nregs
, 0);
1938 replace_regs (REG_NOTES (p
), reg_map
, nregs
, 0);
1943 /* Scan X and replace the address of any MEM in it with ADDR.
1944 REG is the address that MEM should have before the replacement. */
1947 replace_call_address (x
, reg
, addr
)
1950 register enum rtx_code code
;
1956 code
= GET_CODE (x
);
1970 /* Short cut for very common case. */
1971 replace_call_address (XEXP (x
, 1), reg
, addr
);
1975 /* Short cut for very common case. */
1976 replace_call_address (XEXP (x
, 0), reg
, addr
);
1980 /* If this MEM uses a reg other than the one we expected,
1981 something is wrong. */
1982 if (XEXP (x
, 0) != reg
)
1988 fmt
= GET_RTX_FORMAT (code
);
1989 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1992 replace_call_address (XEXP (x
, i
), reg
, addr
);
1996 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1997 replace_call_address (XVECEXP (x
, i
, j
), reg
, addr
);
2003 /* Return the number of memory refs to addresses that vary
2007 count_nonfixed_reads (x
)
2010 register enum rtx_code code
;
2018 code
= GET_CODE (x
);
2032 return ((invariant_p (XEXP (x
, 0)) != 1)
2033 + count_nonfixed_reads (XEXP (x
, 0)));
2037 fmt
= GET_RTX_FORMAT (code
);
2038 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2041 value
+= count_nonfixed_reads (XEXP (x
, i
));
2045 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2046 value
+= count_nonfixed_reads (XVECEXP (x
, i
, j
));
2054 /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2055 Replace it with an instruction to load just the low bytes
2056 if the machine supports such an instruction,
2057 and insert above LOOP_START an instruction to clear the register. */
2060 constant_high_bytes (p
, loop_start
)
2064 register int insn_code_number
;
2066 /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2067 to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
2069 new = gen_rtx (SET
, VOIDmode
,
2070 gen_rtx (STRICT_LOW_PART
, VOIDmode
,
2071 gen_rtx (SUBREG
, GET_MODE (XEXP (SET_SRC (PATTERN (p
)), 0)),
2072 SET_DEST (PATTERN (p
)),
2074 XEXP (SET_SRC (PATTERN (p
)), 0));
2075 insn_code_number
= recog (new, p
);
2077 if (insn_code_number
)
2081 /* Clear destination register before the loop. */
2082 emit_insn_before (gen_rtx (SET
, VOIDmode
,
2083 SET_DEST (PATTERN (p
)),
2087 /* Inside the loop, just load the low part. */
2093 /* Scan a loop setting the variables `unknown_address_altered',
2094 `num_mem_sets', `loop_continue', loops_enclosed', `loop_has_call',
2095 and `loop_has_volatile'.
2096 Also, fill in the array `loop_store_mems'. */
2099 prescan_loop (start
, end
)
2102 register int level
= 1;
2105 unknown_address_altered
= 0;
2107 loop_has_volatile
= 0;
2108 loop_store_mems_idx
= 0;
2114 for (insn
= NEXT_INSN (start
); insn
!= NEXT_INSN (end
);
2115 insn
= NEXT_INSN (insn
))
2117 if (GET_CODE (insn
) == NOTE
)
2119 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
2122 /* Count number of loops contained in this one. */
2125 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
)
2134 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_CONT
)
2137 loop_continue
= insn
;
2140 else if (GET_CODE (insn
) == CALL_INSN
)
2142 unknown_address_altered
= 1;
2147 if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == JUMP_INSN
)
2149 if (volatile_refs_p (PATTERN (insn
)))
2150 loop_has_volatile
= 1;
2152 note_stores (PATTERN (insn
), note_addr_stored
);
2158 /* Scan the function looking for loops. Record the start and end of each loop.
2159 Also mark as invalid loops any loops that contain a setjmp or are branched
2160 to from outside the loop. */
2163 find_and_verify_loops (f
)
2167 int current_loop
= -1;
2171 /* If there are jumps to undefined labels,
2172 treat them as jumps out of any/all loops.
2173 This also avoids writing past end of tables when there are no loops. */
2174 uid_loop_num
[0] = -1;
2176 /* Find boundaries of loops, mark which loops are contained within
2177 loops, and invalidate loops that have setjmp. */
2179 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
2181 if (GET_CODE (insn
) == NOTE
)
2182 switch (NOTE_LINE_NUMBER (insn
))
2184 case NOTE_INSN_LOOP_BEG
:
2185 loop_number_loop_starts
[++next_loop
] = insn
;
2186 loop_number_loop_ends
[next_loop
] = 0;
2187 loop_outer_loop
[next_loop
] = current_loop
;
2188 loop_invalid
[next_loop
] = 0;
2189 loop_number_exit_labels
[next_loop
] = 0;
2190 current_loop
= next_loop
;
2193 case NOTE_INSN_SETJMP
:
2194 /* In this case, we must invalidate our current loop and any
2196 for (loop
= current_loop
; loop
!= -1; loop
= loop_outer_loop
[loop
])
2198 loop_invalid
[loop
] = 1;
2199 if (loop_dump_stream
)
2200 fprintf (loop_dump_stream
,
2201 "\nLoop at %d ignored due to setjmp.\n",
2202 INSN_UID (loop_number_loop_starts
[loop
]));
2206 case NOTE_INSN_LOOP_END
:
2207 if (current_loop
== -1)
2210 loop_number_loop_ends
[current_loop
] = insn
;
2211 current_loop
= loop_outer_loop
[current_loop
];
2216 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2217 enclosing loop, but this doesn't matter. */
2218 uid_loop_num
[INSN_UID (insn
)] = current_loop
;
2221 /* Now scan all JUMP_INSN's in the function. If any branches into a loop
2222 that it is not contained within, that loop is marked invalid.
2224 Also look for blocks of code ending in an unconditional branch that
2225 exits the loop. If such a block is surrounded by a conditional
2226 branch around the block, move the block elsewhere (see below) and
2227 invert the jump to point to the code block. This may eliminate a
2228 label in our loop and will simplify processing by both us and a
2229 possible second cse pass. */
2231 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
2232 if (GET_CODE (insn
) == JUMP_INSN
)
2234 int this_loop_num
= uid_loop_num
[INSN_UID (insn
)];
2236 mark_loop_jump (PATTERN (insn
), this_loop_num
);
2238 /* See if this is an unconditional branch outside the loop. */
2239 if (this_loop_num
!= -1
2240 && (GET_CODE (PATTERN (insn
)) == RETURN
2241 || (simplejump_p (insn
)
2242 && (uid_loop_num
[INSN_UID (JUMP_LABEL (insn
))]
2243 != this_loop_num
))))
2246 rtx our_next
= next_real_insn (insn
);
2248 /* Go backwards until we reach the start of the loop, a label,
2250 for (p
= PREV_INSN (insn
);
2251 GET_CODE (p
) != CODE_LABEL
2252 && ! (GET_CODE (p
) == NOTE
2253 && NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
2254 && GET_CODE (p
) != JUMP_INSN
;
2258 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2259 we have a block of code to try to move.
2261 We look backward and then forward from the target of INSN
2262 to find a BARRIER at the same loop depth as the target.
2263 If we find such a BARRIER, we make a new label for the start
2264 of the block, invert the jump in P and point it to that label,
2265 and move the block of code to the spot we found. */
2267 if (GET_CODE (p
) == JUMP_INSN
2268 && JUMP_LABEL (p
) != 0
2269 /* Just ignore jumps to labels that were never emitted.
2270 These always indicate compilation errors. */
2271 && INSN_UID (JUMP_LABEL (p
)) != 0
2273 && ! simplejump_p (p
)
2274 && next_real_insn (JUMP_LABEL (p
)) == our_next
)
2277 = JUMP_LABEL (insn
) ? JUMP_LABEL (insn
) : get_last_insn ();
2278 int target_loop_num
= uid_loop_num
[INSN_UID (target
)];
2281 for (loc
= target
; loc
; loc
= PREV_INSN (loc
))
2282 if (GET_CODE (loc
) == BARRIER
2283 && uid_loop_num
[INSN_UID (loc
)] == target_loop_num
)
2287 for (loc
= target
; loc
; loc
= NEXT_INSN (loc
))
2288 if (GET_CODE (loc
) == BARRIER
2289 && uid_loop_num
[INSN_UID (loc
)] == target_loop_num
)
2294 rtx cond_label
= JUMP_LABEL (p
);
2295 rtx new_label
= get_label_after (p
);
2297 /* Ensure our label doesn't go away. */
2298 LABEL_NUSES (cond_label
)++;
2300 /* Verify that uid_loop_num is large enough and that
2302 if (INSN_UID (new_label
) < max_uid_for_loop
2303 && invert_jump (p
, new_label
))
2307 /* Include the BARRIER after INSN and copy the
2309 new_label
= squeeze_notes (new_label
, NEXT_INSN (insn
));
2310 reorder_insns (new_label
, NEXT_INSN (insn
), loc
);
2312 /* All those insns are now in TARGET_LOOP_NUM. */
2313 for (q
= new_label
; q
!= NEXT_INSN (NEXT_INSN (insn
));
2315 uid_loop_num
[INSN_UID (q
)] = target_loop_num
;
2317 /* The label jumped to by INSN is no longer a loop exit.
2318 Unless INSN does not have a label (e.g., it is a
2319 RETURN insn), search loop_number_exit_labels to find
2320 its label_ref, and remove it. Also turn off
2321 LABEL_OUTSIDE_LOOP_P bit. */
2322 if (JUMP_LABEL (insn
))
2325 r
= loop_number_exit_labels
[this_loop_num
];
2326 r
; q
= r
, r
= LABEL_NEXTREF (r
))
2327 if (XEXP (r
, 0) == JUMP_LABEL (insn
))
2329 LABEL_OUTSIDE_LOOP_P (r
) = 0;
2331 LABEL_NEXTREF (q
) = LABEL_NEXTREF (r
);
2333 loop_number_exit_labels
[this_loop_num
]
2334 = LABEL_NEXTREF (r
);
2338 /* If we didn't find it, then something is wrong. */
2343 /* P is now a jump outside the loop, so it must be put
2344 in loop_number_exit_labels, and marked as such.
2345 The easiest way to do this is to just call
2346 mark_loop_jump again for P. */
2347 mark_loop_jump (PATTERN (p
), this_loop_num
);
2349 /* If INSN now jumps to the insn after it,
2351 if (JUMP_LABEL (insn
) != 0
2352 && (next_real_insn (JUMP_LABEL (insn
))
2353 == next_real_insn (insn
)))
2357 /* Continue the loop after where the conditional
2358 branch used to jump, since the only branch insn
2359 in the block (if it still remains) is an inter-loop
2360 branch and hence needs no processing. */
2361 insn
= NEXT_INSN (cond_label
);
2363 if (--LABEL_NUSES (cond_label
) == 0)
2364 delete_insn (cond_label
);
2371 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2372 loops it is contained in, mark the target loop invalid.
2374 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2377 mark_loop_jump (x
, loop_num
)
2385 switch (GET_CODE (x
))
2398 /* There could be a label reference in here. */
2399 mark_loop_jump (XEXP (x
, 0), loop_num
);
2406 mark_loop_jump (XEXP (x
, 0), loop_num
);
2407 mark_loop_jump (XEXP (x
, 1), loop_num
);
2412 mark_loop_jump (XEXP (x
, 0), loop_num
);
2416 dest_loop
= uid_loop_num
[INSN_UID (XEXP (x
, 0))];
2418 /* Link together all labels that branch outside the loop. This
2419 is used by final_[bg]iv_value and the loop unrolling code. Also
2420 mark this LABEL_REF so we know that this branch should predict
2423 if (dest_loop
!= loop_num
&& loop_num
!= -1)
2425 LABEL_OUTSIDE_LOOP_P (x
) = 1;
2426 LABEL_NEXTREF (x
) = loop_number_exit_labels
[loop_num
];
2427 loop_number_exit_labels
[loop_num
] = x
;
2430 /* If this is inside a loop, but not in the current loop or one enclosed
2431 by it, it invalidates at least one loop. */
2433 if (dest_loop
== -1)
2436 /* We must invalidate every nested loop containing the target of this
2437 label, except those that also contain the jump insn. */
2439 for (; dest_loop
!= -1; dest_loop
= loop_outer_loop
[dest_loop
])
2441 /* Stop when we reach a loop that also contains the jump insn. */
2442 for (outer_loop
= loop_num
; outer_loop
!= -1;
2443 outer_loop
= loop_outer_loop
[outer_loop
])
2444 if (dest_loop
== outer_loop
)
2447 /* If we get here, we know we need to invalidate a loop. */
2448 if (loop_dump_stream
&& ! loop_invalid
[dest_loop
])
2449 fprintf (loop_dump_stream
,
2450 "\nLoop at %d ignored due to multiple entry points.\n",
2451 INSN_UID (loop_number_loop_starts
[dest_loop
]));
2453 loop_invalid
[dest_loop
] = 1;
2458 /* If this is not setting pc, ignore. */
2459 if (SET_DEST (x
) == pc_rtx
)
2460 mark_loop_jump (SET_SRC (x
), loop_num
);
2464 mark_loop_jump (XEXP (x
, 1), loop_num
);
2465 mark_loop_jump (XEXP (x
, 2), loop_num
);
2470 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
2471 mark_loop_jump (XVECEXP (x
, 0, i
), loop_num
);
2475 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
2476 mark_loop_jump (XVECEXP (x
, 1, i
), loop_num
);
2480 /* Nothing else should occur in a JUMP_INSN. */
2485 /* Return nonzero if there is a label in the range from
2486 insn INSN to and including the insn whose luid is END
2487 INSN must have an assigned luid (i.e., it must not have
2488 been previously created by loop.c). */
2491 labels_in_range_p (insn
, end
)
2495 while (insn
&& INSN_LUID (insn
) <= end
)
2497 if (GET_CODE (insn
) == CODE_LABEL
)
2499 insn
= NEXT_INSN (insn
);
2505 /* Record that a memory reference X is being set. */
2508 note_addr_stored (x
)
2513 if (x
== 0 || GET_CODE (x
) != MEM
)
2516 /* Count number of memory writes.
2517 This affects heuristics in strength_reduce. */
2520 if (unknown_address_altered
)
2523 for (i
= 0; i
< loop_store_mems_idx
; i
++)
2524 if (rtx_equal_p (XEXP (loop_store_mems
[i
], 0), XEXP (x
, 0))
2525 && MEM_IN_STRUCT_P (x
) == MEM_IN_STRUCT_P (loop_store_mems
[i
]))
2527 /* We are storing at the same address as previously noted. Save the
2528 wider reference, treating BLKmode as wider. */
2529 if (GET_MODE (x
) == BLKmode
2530 || (GET_MODE_SIZE (GET_MODE (x
))
2531 > GET_MODE_SIZE (GET_MODE (loop_store_mems
[i
]))))
2532 loop_store_mems
[i
] = x
;
2536 if (i
== NUM_STORES
)
2537 unknown_address_altered
= 1;
2539 else if (i
== loop_store_mems_idx
)
2540 loop_store_mems
[loop_store_mems_idx
++] = x
;
2543 /* Return nonzero if the rtx X is invariant over the current loop.
2545 The value is 2 if we refer to something only conditionally invariant.
2547 If `unknown_address_altered' is nonzero, no memory ref is invariant.
2548 Otherwise, a memory ref is invariant if it does not conflict with
2549 anything stored in `loop_store_mems'. */
2556 register enum rtx_code code
;
2558 int conditional
= 0;
2562 code
= GET_CODE (x
);
2572 /* A LABEL_REF is normally invariant, however, if we are unrolling
2573 loops, and this label is inside the loop, then it isn't invariant.
2574 This is because each unrolled copy of the loop body will have
2575 a copy of this label. If this was invariant, then an insn loading
2576 the address of this label into a register might get moved outside
2577 the loop, and then each loop body would end up using the same label.
2579 We don't know the loop bounds here though, so just fail for all
2581 if (flag_unroll_loops
)
2588 case UNSPEC_VOLATILE
:
2592 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
2593 since the reg might be set by initialization within the loop. */
2594 if (x
== frame_pointer_rtx
|| x
== arg_pointer_rtx
)
2597 && REGNO (x
) < FIRST_PSEUDO_REGISTER
&& call_used_regs
[REGNO (x
)])
2599 if (n_times_set
[REGNO (x
)] < 0)
2601 return n_times_set
[REGNO (x
)] == 0;
2604 /* Read-only items (such as constants in a constant pool) are
2605 invariant if their address is. */
2606 if (RTX_UNCHANGING_P (x
))
2609 /* If we filled the table (or had a subroutine call), any location
2610 in memory could have been clobbered. */
2611 if (unknown_address_altered
2612 /* Don't mess with volatile memory references. */
2613 || MEM_VOLATILE_P (x
))
2616 /* See if there is any dependence between a store and this load. */
2617 for (i
= loop_store_mems_idx
- 1; i
>= 0; i
--)
2618 if (true_dependence (loop_store_mems
[i
], x
))
2621 /* It's not invalidated by a store in memory
2622 but we must still verify the address is invariant. */
2626 /* Don't mess with insns declared volatile. */
2627 if (MEM_VOLATILE_P (x
))
2631 fmt
= GET_RTX_FORMAT (code
);
2632 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2636 int tem
= invariant_p (XEXP (x
, i
));
2642 else if (fmt
[i
] == 'E')
2645 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2647 int tem
= invariant_p (XVECEXP (x
, i
, j
));
2657 return 1 + conditional
;
2660 /* Return 1 if OTHER (a mem ref) overlaps the area of memory
2661 which is SIZE bytes starting at BASE. */
2664 addr_overlap_p (other
, base
, size
)
2669 HOST_WIDE_INT start
= 0, end
;
2671 if (GET_CODE (base
) == CONST
)
2672 base
= XEXP (base
, 0);
2673 if (GET_CODE (base
) == PLUS
2674 && GET_CODE (XEXP (base
, 1)) == CONST_INT
)
2676 start
= INTVAL (XEXP (base
, 1));
2677 base
= XEXP (base
, 0);
2681 return refers_to_mem_p (other
, base
, start
, end
);
2684 /* Return nonzero if all the insns in the loop that set REG
2685 are INSN and the immediately following insns,
2686 and if each of those insns sets REG in an invariant way
2687 (not counting uses of REG in them).
2689 The value is 2 if some of these insns are only conditionally invariant.
2691 We assume that INSN itself is the first set of REG
2692 and that its source is invariant. */
2695 consec_sets_invariant_p (reg
, n_sets
, insn
)
2699 register rtx p
= insn
;
2700 register int regno
= REGNO (reg
);
2702 /* Number of sets we have to insist on finding after INSN. */
2703 int count
= n_sets
- 1;
2704 int old
= n_times_set
[regno
];
2708 /* If N_SETS hit the limit, we can't rely on its value. */
2712 n_times_set
[regno
] = 0;
2716 register enum rtx_code code
;
2720 code
= GET_CODE (p
);
2722 /* If library call, skip to end of of it. */
2723 if (code
== INSN
&& (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
2728 && (set
= single_set (p
))
2729 && GET_CODE (SET_DEST (set
)) == REG
2730 && REGNO (SET_DEST (set
)) == regno
)
2732 this = invariant_p (SET_SRC (set
));
2735 else if (temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
2737 this = invariant_p (XEXP (temp
, 0));
2744 else if (code
!= NOTE
)
2746 n_times_set
[regno
] = old
;
2751 n_times_set
[regno
] = old
;
2752 /* If invariant_p ever returned 2, we return 2. */
2753 return 1 + (value
& 2);
2757 /* I don't think this condition is sufficient to allow INSN
2758 to be moved, so we no longer test it. */
2760 /* Return 1 if all insns in the basic block of INSN and following INSN
2761 that set REG are invariant according to TABLE. */
2764 all_sets_invariant_p (reg
, insn
, table
)
2768 register rtx p
= insn
;
2769 register int regno
= REGNO (reg
);
2773 register enum rtx_code code
;
2775 code
= GET_CODE (p
);
2776 if (code
== CODE_LABEL
|| code
== JUMP_INSN
)
2778 if (code
== INSN
&& GET_CODE (PATTERN (p
)) == SET
2779 && GET_CODE (SET_DEST (PATTERN (p
))) == REG
2780 && REGNO (SET_DEST (PATTERN (p
))) == regno
)
2782 if (!invariant_p (SET_SRC (PATTERN (p
)), table
))
2789 /* Look at all uses (not sets) of registers in X. For each, if it is
2790 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
2791 a different insn, set USAGE[REGNO] to const0_rtx. */
2794 find_single_use_in_loop (insn
, x
, usage
)
2799 enum rtx_code code
= GET_CODE (x
);
2800 char *fmt
= GET_RTX_FORMAT (code
);
2805 = (usage
[REGNO (x
)] != 0 && usage
[REGNO (x
)] != insn
)
2806 ? const0_rtx
: insn
;
2808 else if (code
== SET
)
2810 /* Don't count SET_DEST if it is a REG; otherwise count things
2811 in SET_DEST because if a register is partially modified, it won't
2812 show up as a potential movable so we don't care how USAGE is set
2814 if (GET_CODE (SET_DEST (x
)) != REG
)
2815 find_single_use_in_loop (insn
, SET_DEST (x
), usage
);
2816 find_single_use_in_loop (insn
, SET_SRC (x
), usage
);
2819 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2821 if (fmt
[i
] == 'e' && XEXP (x
, i
) != 0)
2822 find_single_use_in_loop (insn
, XEXP (x
, i
), usage
);
2823 else if (fmt
[i
] == 'E')
2824 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
2825 find_single_use_in_loop (insn
, XVECEXP (x
, i
, j
), usage
);
2829 /* Increment N_TIMES_SET at the index of each register
2830 that is modified by an insn between FROM and TO.
2831 If the value of an element of N_TIMES_SET becomes 127 or more,
2832 stop incrementing it, to avoid overflow.
2834 Store in SINGLE_USAGE[I] the single insn in which register I is
2835 used, if it is only used once. Otherwise, it is set to 0 (for no
2836 uses) or const0_rtx for more than one use. This parameter may be zero,
2837 in which case this processing is not done.
2839 Store in *COUNT_PTR the number of actual instruction
2840 in the loop. We use this to decide what is worth moving out. */
2842 /* last_set[n] is nonzero iff reg n has been set in the current basic block.
2843 In that case, it is the insn that last set reg n. */
2846 count_loop_regs_set (from
, to
, may_not_move
, single_usage
, count_ptr
, nregs
)
2847 register rtx from
, to
;
2853 register rtx
*last_set
= (rtx
*) alloca (nregs
* sizeof (rtx
));
2855 register int count
= 0;
2858 bzero (last_set
, nregs
* sizeof (rtx
));
2859 for (insn
= from
; insn
!= to
; insn
= NEXT_INSN (insn
))
2861 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i')
2865 /* If requested, record registers that have exactly one use. */
2868 find_single_use_in_loop (insn
, PATTERN (insn
), single_usage
);
2870 /* Include uses in REG_EQUAL notes. */
2871 if (REG_NOTES (insn
))
2872 find_single_use_in_loop (insn
, REG_NOTES (insn
), single_usage
);
2875 if (GET_CODE (PATTERN (insn
)) == CLOBBER
2876 && GET_CODE (XEXP (PATTERN (insn
), 0)) == REG
)
2877 /* Don't move a reg that has an explicit clobber.
2878 We might do so sometimes, but it's not worth the pain. */
2879 may_not_move
[REGNO (XEXP (PATTERN (insn
), 0))] = 1;
2881 if (GET_CODE (PATTERN (insn
)) == SET
2882 || GET_CODE (PATTERN (insn
)) == CLOBBER
)
2884 dest
= SET_DEST (PATTERN (insn
));
2885 while (GET_CODE (dest
) == SUBREG
2886 || GET_CODE (dest
) == ZERO_EXTRACT
2887 || GET_CODE (dest
) == SIGN_EXTRACT
2888 || GET_CODE (dest
) == STRICT_LOW_PART
)
2889 dest
= XEXP (dest
, 0);
2890 if (GET_CODE (dest
) == REG
)
2892 register int regno
= REGNO (dest
);
2893 /* If this is the first setting of this reg
2894 in current basic block, and it was set before,
2895 it must be set in two basic blocks, so it cannot
2896 be moved out of the loop. */
2897 if (n_times_set
[regno
] > 0 && last_set
[regno
] == 0)
2898 may_not_move
[regno
] = 1;
2899 /* If this is not first setting in current basic block,
2900 see if reg was used in between previous one and this.
2901 If so, neither one can be moved. */
2902 if (last_set
[regno
] != 0
2903 && reg_used_between_p (dest
, last_set
[regno
], insn
))
2904 may_not_move
[regno
] = 1;
2905 if (n_times_set
[regno
] < 127)
2906 ++n_times_set
[regno
];
2907 last_set
[regno
] = insn
;
2910 else if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
2913 for (i
= XVECLEN (PATTERN (insn
), 0) - 1; i
>= 0; i
--)
2915 register rtx x
= XVECEXP (PATTERN (insn
), 0, i
);
2916 if (GET_CODE (x
) == CLOBBER
&& GET_CODE (XEXP (x
, 0)) == REG
)
2917 /* Don't move a reg that has an explicit clobber.
2918 It's not worth the pain to try to do it correctly. */
2919 may_not_move
[REGNO (XEXP (x
, 0))] = 1;
2921 if (GET_CODE (x
) == SET
|| GET_CODE (x
) == CLOBBER
)
2923 dest
= SET_DEST (x
);
2924 while (GET_CODE (dest
) == SUBREG
2925 || GET_CODE (dest
) == ZERO_EXTRACT
2926 || GET_CODE (dest
) == SIGN_EXTRACT
2927 || GET_CODE (dest
) == STRICT_LOW_PART
)
2928 dest
= XEXP (dest
, 0);
2929 if (GET_CODE (dest
) == REG
)
2931 register int regno
= REGNO (dest
);
2932 if (n_times_set
[regno
] > 0 && last_set
[regno
] == 0)
2933 may_not_move
[regno
] = 1;
2934 if (last_set
[regno
] != 0
2935 && reg_used_between_p (dest
, last_set
[regno
], insn
))
2936 may_not_move
[regno
] = 1;
2937 if (n_times_set
[regno
] < 127)
2938 ++n_times_set
[regno
];
2939 last_set
[regno
] = insn
;
2945 if (GET_CODE (insn
) == CODE_LABEL
|| GET_CODE (insn
) == JUMP_INSN
)
2946 bzero (last_set
, nregs
* sizeof (rtx
));
2951 /* Given a loop that is bounded by LOOP_START and LOOP_END
2952 and that is entered at SCAN_START,
2953 return 1 if the register set in SET contained in insn INSN is used by
2954 any insn that precedes INSN in cyclic order starting
2955 from the loop entry point.
2957 We don't want to use INSN_LUID here because if we restrict INSN to those
2958 that have a valid INSN_LUID, it means we cannot move an invariant out
2959 from an inner loop past two loops. */
2962 loop_reg_used_before_p (set
, insn
, loop_start
, scan_start
, loop_end
)
2963 rtx set
, insn
, loop_start
, scan_start
, loop_end
;
2965 rtx reg
= SET_DEST (set
);
2968 /* Scan forward checking for register usage. If we hit INSN, we
2969 are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
2970 for (p
= scan_start
; p
!= insn
; p
= NEXT_INSN (p
))
2972 if (GET_RTX_CLASS (GET_CODE (p
)) == 'i'
2973 && reg_overlap_mentioned_p (reg
, PATTERN (p
)))
2983 /* A "basic induction variable" or biv is a pseudo reg that is set
2984 (within this loop) only by incrementing or decrementing it. */
2985 /* A "general induction variable" or giv is a pseudo reg whose
2986 value is a linear function of a biv. */
2988 /* Bivs are recognized by `basic_induction_var';
2989 Givs by `general_induct_var'. */
2991 /* Indexed by register number, indicates whether or not register is an
2992 induction variable, and if so what type. */
2994 enum iv_mode
*reg_iv_type
;
2996 /* Indexed by register number, contains pointer to `struct induction'
2997 if register is an induction variable. This holds general info for
2998 all induction variables. */
3000 struct induction
**reg_iv_info
;
3002 /* Indexed by register number, contains pointer to `struct iv_class'
3003 if register is a basic induction variable. This holds info describing
3004 the class (a related group) of induction variables that the biv belongs
3007 struct iv_class
**reg_biv_class
;
3009 /* The head of a list which links together (via the next field)
3010 every iv class for the current loop. */
3012 struct iv_class
*loop_iv_list
;
3014 /* Communication with routines called via `note_stores'. */
3016 static rtx note_insn
;
3018 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3020 static rtx addr_placeholder
;
3022 /* ??? Unfinished optimizations, and possible future optimizations,
3023 for the strength reduction code. */
3025 /* ??? There is one more optimization you might be interested in doing: to
3026 allocate pseudo registers for frequently-accessed memory locations.
3027 If the same memory location is referenced each time around, it might
3028 be possible to copy it into a register before and out after.
3029 This is especially useful when the memory location is a variable which
3030 is in a stack slot because somewhere its address is taken. If the
3031 loop doesn't contain a function call and the variable isn't volatile,
3032 it is safe to keep the value in a register for the duration of the
3033 loop. One tricky thing is that the copying of the value back from the
3034 register has to be done on all exits from the loop. You need to check that
3035 all the exits from the loop go to the same place. */
3037 /* ??? The interaction of biv elimination, and recognition of 'constant'
3038 bivs, may cause problems. */
3040 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3041 performance problems.
3043 Perhaps don't eliminate things that can be combined with an addressing
3044 mode. Find all givs that have the same biv, mult_val, and add_val;
3045 then for each giv, check to see if its only use dies in a following
3046 memory address. If so, generate a new memory address and check to see
3047 if it is valid. If it is valid, then store the modified memory address,
3048 otherwise, mark the giv as not done so that it will get its own iv. */
3050 /* ??? Could try to optimize branches when it is known that a biv is always
3053 /* ??? When replace a biv in a compare insn, we should replace with closest
3054 giv so that an optimized branch can still be recognized by the combiner,
3055 e.g. the VAX acb insn. */
3057 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3058 was rerun in loop_optimize whenever a register was added or moved.
3059 Also, some of the optimizations could be a little less conservative. */
3061 /* Perform strength reduction and induction variable elimination. */
3063 /* Pseudo registers created during this function will be beyond the last
3064 valid index in several tables including n_times_set and regno_last_uid.
3065 This does not cause a problem here, because the added registers cannot be
3066 givs outside of their loop, and hence will never be reconsidered.
3067 But scan_loop must check regnos to make sure they are in bounds. */
3070 strength_reduce (scan_start
, end
, loop_top
, insn_count
,
3071 loop_start
, loop_end
)
3084 /* This is 1 if current insn is not executed at least once for every loop
3086 int not_every_iteration
= 0;
3087 /* This is 1 if current insn may be executed more than once for every
3089 int maybe_multiple
= 0;
3090 /* Temporary list pointers for traversing loop_iv_list. */
3091 struct iv_class
*bl
, **backbl
;
3092 /* Ratio of extra register life span we can justify
3093 for saving an instruction. More if loop doesn't call subroutines
3094 since in that case saving an insn makes more difference
3095 and more registers are available. */
3096 /* ??? could set this to last value of threshold in move_movables */
3097 int threshold
= (loop_has_call
? 1 : 2) * (3 + n_non_fixed_regs
);
3098 /* Map of pseudo-register replacements. */
3102 rtx end_insert_before
;
3104 reg_iv_type
= (enum iv_mode
*) alloca (max_reg_before_loop
3105 * sizeof (enum iv_mode
*));
3106 bzero ((char *) reg_iv_type
, max_reg_before_loop
* sizeof (enum iv_mode
*));
3107 reg_iv_info
= (struct induction
**)
3108 alloca (max_reg_before_loop
* sizeof (struct induction
*));
3109 bzero ((char *) reg_iv_info
, (max_reg_before_loop
3110 * sizeof (struct induction
*)));
3111 reg_biv_class
= (struct iv_class
**)
3112 alloca (max_reg_before_loop
* sizeof (struct iv_class
*));
3113 bzero ((char *) reg_biv_class
, (max_reg_before_loop
3114 * sizeof (struct iv_class
*)));
3117 addr_placeholder
= gen_reg_rtx (Pmode
);
3119 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3120 must be put before this insn, so that they will appear in the right
3121 order (i.e. loop order). */
3123 end_insert_before
= NEXT_INSN (loop_end
);
3125 /* Scan through loop to find all possible bivs. */
3131 /* At end of a straight-in loop, we are done.
3132 At end of a loop entered at the bottom, scan the top. */
3133 if (p
== scan_start
)
3138 p
= NEXT_INSN (loop_top
);
3141 if (p
== scan_start
)
3145 if (GET_CODE (p
) == INSN
3146 && (set
= single_set (p
))
3147 && GET_CODE (SET_DEST (set
)) == REG
)
3149 dest_reg
= SET_DEST (set
);
3150 if (REGNO (dest_reg
) < max_reg_before_loop
3151 && REGNO (dest_reg
) >= FIRST_PSEUDO_REGISTER
3152 && reg_iv_type
[REGNO (dest_reg
)] != NOT_BASIC_INDUCT
)
3154 if (basic_induction_var (SET_SRC (set
), dest_reg
,
3155 &inc_val
, &mult_val
))
3157 /* It is a possible basic induction variable.
3158 Create and initialize an induction structure for it. */
3161 = (struct induction
*) alloca (sizeof (struct induction
));
3163 record_biv (v
, p
, dest_reg
, inc_val
, mult_val
,
3164 not_every_iteration
, maybe_multiple
);
3165 reg_iv_type
[REGNO (dest_reg
)] = BASIC_INDUCT
;
3167 else if (REGNO (dest_reg
) < max_reg_before_loop
)
3168 reg_iv_type
[REGNO (dest_reg
)] = NOT_BASIC_INDUCT
;
3172 /* Past CODE_LABEL, we get to insns that may be executed multiple
3173 times. The only way we can be sure that they can't is if every
3174 every jump insn between here and the end of the loop either
3175 returns, exits the loop, or is a forward jump. */
3177 if (GET_CODE (p
) == CODE_LABEL
)
3185 insn
= NEXT_INSN (insn
);
3186 if (insn
== scan_start
)
3191 insn
= NEXT_INSN (loop_top
);
3194 if (insn
== scan_start
)
3198 if (GET_CODE (insn
) == JUMP_INSN
3199 && GET_CODE (PATTERN (insn
)) != RETURN
3200 && (! condjump_p (insn
)
3201 || (JUMP_LABEL (insn
) != 0
3202 && (INSN_UID (JUMP_LABEL (insn
)) >= max_uid_for_loop
3203 || INSN_UID (insn
) >= max_uid_for_loop
3204 || (INSN_LUID (JUMP_LABEL (insn
))
3205 < INSN_LUID (insn
))))))
3213 /* Past a label or a jump, we get to insns for which we can't count
3214 on whether or how many times they will be executed during each
3216 /* This code appears in three places, once in scan_loop, and twice
3217 in strength_reduce. */
3218 if ((GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
)
3219 /* If we enter the loop in the middle, and scan around to the
3220 beginning, don't set not_every_iteration for that.
3221 This can be any kind of jump, since we want to know if insns
3222 will be executed if the loop is executed. */
3223 && ! (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
) == loop_top
3224 && ((NEXT_INSN (NEXT_INSN (p
)) == loop_end
&& simplejump_p (p
))
3225 || (NEXT_INSN (p
) == loop_end
&& condjump_p (p
)))))
3226 not_every_iteration
= 1;
3228 /* At the virtual top of a converted loop, insns are again known to
3229 be executed each iteration: logically, the loop begins here
3230 even though the exit code has been duplicated. */
3232 else if (GET_CODE (p
) == NOTE
3233 && NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_VTOP
)
3234 not_every_iteration
= 0;
3236 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3237 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3238 or not an insn is known to be executed each iteration of the
3239 loop, whether or not any iterations are known to occur.
3241 Therefore, if we have just passed a label and have no more labels
3242 between here and the test insn of the loop, we know these insns
3243 will be executed each iteration. This can also happen if we
3244 have just passed a jump, for example, when there are nested loops. */
3246 if (not_every_iteration
&& GET_CODE (p
) == CODE_LABEL
3247 && no_labels_between_p (p
, loop_end
))
3248 not_every_iteration
= 0;
3251 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3252 Make a sanity check against n_times_set. */
3253 for (backbl
= &loop_iv_list
, bl
= *backbl
; bl
; bl
= bl
->next
)
3255 if (reg_iv_type
[bl
->regno
] != BASIC_INDUCT
3256 /* Above happens if register modified by subreg, etc. */
3257 /* Make sure it is not recognized as a basic induction var: */
3258 || n_times_set
[bl
->regno
] != bl
->biv_count
3259 /* If never incremented, it is invariant that we decided not to
3260 move. So leave it alone. */
3261 || ! bl
->incremented
)
3263 if (loop_dump_stream
)
3264 fprintf (loop_dump_stream
, "Reg %d: biv discarded, %s\n",
3266 (reg_iv_type
[bl
->regno
] != BASIC_INDUCT
3267 ? "not induction variable"
3268 : (! bl
->incremented
? "never incremented"
3271 reg_iv_type
[bl
->regno
] = NOT_BASIC_INDUCT
;
3278 if (loop_dump_stream
)
3279 fprintf (loop_dump_stream
, "Reg %d: biv verified\n", bl
->regno
);
3283 /* Exit if there are no bivs. */
3286 /* Can still unroll the loop anyways, but indicate that there is no
3287 strength reduction info available. */
3288 if (flag_unroll_loops
)
3289 unroll_loop (loop_end
, insn_count
, loop_start
, end_insert_before
, 0);
3294 /* Find initial value for each biv by searching backwards from loop_start,
3295 halting at first label. Also record any test condition. */
3298 for (p
= loop_start
; p
&& GET_CODE (p
) != CODE_LABEL
; p
= PREV_INSN (p
))
3302 if (GET_CODE (p
) == CALL_INSN
)
3305 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
3306 || GET_CODE (p
) == CALL_INSN
)
3307 note_stores (PATTERN (p
), record_initial
);
3309 /* Record any test of a biv that branches around the loop if no store
3310 between it and the start of loop. We only care about tests with
3311 constants and registers and only certain of those. */
3312 if (GET_CODE (p
) == JUMP_INSN
3313 && JUMP_LABEL (p
) != 0
3314 && next_real_insn (JUMP_LABEL (p
)) == next_real_insn (loop_end
)
3315 && (test
= get_condition_for_loop (p
)) != 0
3316 && GET_CODE (XEXP (test
, 0)) == REG
3317 && REGNO (XEXP (test
, 0)) < max_reg_before_loop
3318 && (bl
= reg_biv_class
[REGNO (XEXP (test
, 0))]) != 0
3319 && valid_initial_value_p (XEXP (test
, 1), p
, call_seen
, loop_start
)
3320 && bl
->init_insn
== 0)
3322 /* If an NE test, we have an initial value! */
3323 if (GET_CODE (test
) == NE
)
3326 bl
->init_set
= gen_rtx (SET
, VOIDmode
,
3327 XEXP (test
, 0), XEXP (test
, 1));
3330 bl
->initial_test
= test
;
3334 /* Look at the each biv and see if we can say anything better about its
3335 initial value from any initializing insns set up above. (This is done
3336 in two passes to avoid missing SETs in a PARALLEL.) */
3337 for (bl
= loop_iv_list
; bl
; bl
= bl
->next
)
3341 if (! bl
->init_insn
)
3344 src
= SET_SRC (bl
->init_set
);
3346 if (loop_dump_stream
)
3347 fprintf (loop_dump_stream
,
3348 "Biv %d initialized at insn %d: initial value ",
3349 bl
->regno
, INSN_UID (bl
->init_insn
));
3351 if (valid_initial_value_p (src
, bl
->init_insn
, call_seen
, loop_start
))
3353 bl
->initial_value
= src
;
3355 if (loop_dump_stream
)
3357 if (GET_CODE (src
) == CONST_INT
)
3358 fprintf (loop_dump_stream
, "%d\n", INTVAL (src
));
3361 print_rtl (loop_dump_stream
, src
);
3362 fprintf (loop_dump_stream
, "\n");
3368 /* Biv initial value is not simple move,
3369 so let it keep initial value of "itself". */
3371 if (loop_dump_stream
)
3372 fprintf (loop_dump_stream
, "is complex\n");
3376 /* Search the loop for general induction variables. */
3378 /* A register is a giv if: it is only set once, it is a function of a
3379 biv and a constant (or invariant), and it is not a biv. */
3381 not_every_iteration
= 0;
3386 /* At end of a straight-in loop, we are done.
3387 At end of a loop entered at the bottom, scan the top. */
3388 if (p
== scan_start
)
3393 p
= NEXT_INSN (loop_top
);
3396 if (p
== scan_start
)
3400 /* Look for a general induction variable in a register. */
3401 if (GET_CODE (p
) == INSN
3402 && (set
= single_set (p
))
3403 && GET_CODE (SET_DEST (set
)) == REG
3404 && ! may_not_optimize
[REGNO (SET_DEST (set
))])
3412 dest_reg
= SET_DEST (set
);
3413 if (REGNO (dest_reg
) < FIRST_PSEUDO_REGISTER
)
3416 if (/* SET_SRC is a giv. */
3417 ((benefit
= general_induction_var (SET_SRC (set
),
3420 /* Equivalent expression is a giv. */
3421 || ((regnote
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
3422 && (benefit
= general_induction_var (XEXP (regnote
, 0),
3424 &add_val
, &mult_val
))))
3425 /* Don't try to handle any regs made by loop optimization.
3426 We have nothing on them in regno_first_uid, etc. */
3427 && REGNO (dest_reg
) < max_reg_before_loop
3428 /* Don't recognize a BASIC_INDUCT_VAR here. */
3429 && dest_reg
!= src_reg
3430 /* This must be the only place where the register is set. */
3431 && (n_times_set
[REGNO (dest_reg
)] == 1
3432 /* or all sets must be consecutive and make a giv. */
3433 || (benefit
= consec_sets_giv (benefit
, p
,
3435 &add_val
, &mult_val
))))
3439 = (struct induction
*) alloca (sizeof (struct induction
));
3442 /* If this is a library call, increase benefit. */
3443 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
3444 benefit
+= libcall_benefit (p
);
3446 /* Skip the consecutive insns, if there are any. */
3447 for (count
= n_times_set
[REGNO (dest_reg
)] - 1;
3450 /* If first insn of libcall sequence, skip to end.
3451 Do this at start of loop, since INSN is guaranteed to
3453 if (GET_CODE (p
) != NOTE
3454 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
3457 do p
= NEXT_INSN (p
);
3458 while (GET_CODE (p
) == NOTE
);
3461 record_giv (v
, p
, src_reg
, dest_reg
, mult_val
, add_val
, benefit
,
3462 DEST_REG
, not_every_iteration
, NULL_PTR
, loop_start
,
3468 #ifndef DONT_REDUCE_ADDR
3469 /* Look for givs which are memory addresses. */
3470 /* This resulted in worse code on a VAX 8600. I wonder if it
3472 if (GET_CODE (p
) == INSN
)
3473 find_mem_givs (PATTERN (p
), p
, not_every_iteration
, loop_start
,
3477 /* Update the status of whether giv can derive other givs. This can
3478 change when we pass a label or an insn that updates a biv. */
3479 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
3480 || GET_CODE (p
) == CODE_LABEL
)
3481 update_giv_derive (p
);
3483 /* Past a label or a jump, we get to insns for which we can't count
3484 on whether or how many times they will be executed during each
3486 /* This code appears in three places, once in scan_loop, and twice
3487 in strength_reduce. */
3488 if ((GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
)
3489 /* If we enter the loop in the middle, and scan around
3490 to the beginning, don't set not_every_iteration for that.
3491 This can be any kind of jump, since we want to know if insns
3492 will be executed if the loop is executed. */
3493 && ! (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
) == loop_top
3494 && ((NEXT_INSN (NEXT_INSN (p
)) == loop_end
&& simplejump_p (p
))
3495 || (NEXT_INSN (p
) == loop_end
&& condjump_p (p
)))))
3496 not_every_iteration
= 1;
3498 /* At the virtual top of a converted loop, insns are again known to
3499 be executed each iteration: logically, the loop begins here
3500 even though the exit code has been duplicated. */
3502 else if (GET_CODE (p
) == NOTE
3503 && NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_VTOP
)
3504 not_every_iteration
= 0;
3506 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3507 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3508 or not an insn is known to be executed each iteration of the
3509 loop, whether or not any iterations are known to occur.
3511 Therefore, if we have just passed a label and have no more labels
3512 between here and the test insn of the loop, we know these insns
3513 will be executed each iteration. */
3515 if (not_every_iteration
&& GET_CODE (p
) == CODE_LABEL
3516 && no_labels_between_p (p
, loop_end
))
3517 not_every_iteration
= 0;
3520 /* Try to calculate and save the number of loop iterations. This is
3521 set to zero if the actual number can not be calculated. This must
3522 be called after all giv's have been identified, since otherwise it may
3523 fail if the iteration variable is a giv. */
3525 loop_n_iterations
= loop_iterations (loop_start
, loop_end
);
3527 /* Now for each giv for which we still don't know whether or not it is
3528 replaceable, check to see if it is replaceable because its final value
3529 can be calculated. This must be done after loop_iterations is called,
3530 so that final_giv_value will work correctly. */
3532 for (bl
= loop_iv_list
; bl
; bl
= bl
->next
)
3534 struct induction
*v
;
3536 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
3537 if (! v
->replaceable
&& ! v
->not_replaceable
)
3538 check_final_value (v
, loop_start
, loop_end
);
3541 /* Try to prove that the loop counter variable (if any) is always
3542 nonnegative; if so, record that fact with a REG_NONNEG note
3543 so that "decrement and branch until zero" insn can be used. */
3544 check_dbra_loop (loop_end
, insn_count
, loop_start
);
3546 /* Create reg_map to hold substitutions for replaceable giv regs. */
3547 reg_map
= (rtx
*) alloca (max_reg_before_loop
* sizeof (rtx
));
3548 bzero ((char *) reg_map
, max_reg_before_loop
* sizeof (rtx
));
3550 /* Examine each iv class for feasibility of strength reduction/induction
3551 variable elimination. */
3553 for (bl
= loop_iv_list
; bl
; bl
= bl
->next
)
3555 struct induction
*v
;
3558 rtx final_value
= 0;
3560 /* Test whether it will be possible to eliminate this biv
3561 provided all givs are reduced. This is possible if either
3562 the reg is not used outside the loop, or we can compute
3563 what its final value will be.
3565 For architectures with a decrement_and_branch_until_zero insn,
3566 don't do this if we put a REG_NONNEG note on the endtest for
3569 /* Compare against bl->init_insn rather than loop_start.
3570 We aren't concerned with any uses of the biv between
3571 init_insn and loop_start since these won't be affected
3572 by the value of the biv elsewhere in the function, so
3573 long as init_insn doesn't use the biv itself.
3574 March 14, 1989 -- self@bayes.arc.nasa.gov */
3576 if ((uid_luid
[regno_last_uid
[bl
->regno
]] < INSN_LUID (loop_end
)
3578 && INSN_UID (bl
->init_insn
) < max_uid_for_loop
3579 && uid_luid
[regno_first_uid
[bl
->regno
]] >= INSN_LUID (bl
->init_insn
)
3580 #ifdef HAVE_decrement_and_branch_until_zero
3583 && ! reg_mentioned_p (bl
->biv
->dest_reg
, SET_SRC (bl
->init_set
)))
3584 || ((final_value
= final_biv_value (bl
, loop_start
, loop_end
))
3585 #ifdef HAVE_decrement_and_branch_until_zero
3589 bl
->eliminable
= maybe_eliminate_biv (bl
, loop_start
, end
, 0,
3590 threshold
, insn_count
);
3593 if (loop_dump_stream
)
3595 fprintf (loop_dump_stream
,
3596 "Cannot eliminate biv %d.\n",
3598 fprintf (loop_dump_stream
,
3599 "First use: insn %d, last use: insn %d.\n",
3600 regno_first_uid
[bl
->regno
],
3601 regno_last_uid
[bl
->regno
]);
3605 /* Combine all giv's for this iv_class. */
3608 /* This will be true at the end, if all givs which depend on this
3609 biv have been strength reduced.
3610 We can't (currently) eliminate the biv unless this is so. */
3613 /* Check each giv in this class to see if we will benefit by reducing
3614 it. Skip giv's combined with others. */
3615 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
3617 struct induction
*tv
;
3619 if (v
->ignore
|| v
->same
)
3622 benefit
= v
->benefit
;
3624 /* Reduce benefit if not replaceable, since we will insert
3625 a move-insn to replace the insn that calculates this giv.
3626 Don't do this unless the giv is a user variable, since it
3627 will often be marked non-replaceable because of the duplication
3628 of the exit code outside the loop. In such a case, the copies
3629 we insert are dead and will be deleted. So they don't have
3630 a cost. Similar situations exist. */
3631 /* ??? The new final_[bg]iv_value code does a much better job
3632 of finding replaceable giv's, and hence this code may no longer
3634 if (! v
->replaceable
&& ! bl
->eliminable
3635 && REG_USERVAR_P (v
->dest_reg
))
3636 benefit
-= copy_cost
;
3638 /* Decrease the benefit to count the add-insns that we will
3639 insert to increment the reduced reg for the giv. */
3640 benefit
-= add_cost
* bl
->biv_count
;
3642 /* Decide whether to strength-reduce this giv or to leave the code
3643 unchanged (recompute it from the biv each time it is used).
3644 This decision can be made independently for each giv. */
3646 /* ??? Perhaps attempt to guess whether autoincrement will handle
3647 some of the new add insns; if so, can increase BENEFIT
3648 (undo the subtraction of add_cost that was done above). */
3650 /* If an insn is not to be strength reduced, then set its ignore
3651 flag, and clear all_reduced. */
3653 if (v
->lifetime
* threshold
* benefit
< insn_count
)
3655 if (loop_dump_stream
)
3656 fprintf (loop_dump_stream
,
3657 "giv of insn %d not worth while, %d vs %d.\n",
3659 v
->lifetime
* threshold
* benefit
, insn_count
);
3665 /* Check that we can increment the reduced giv without a
3666 multiply insn. If not, reject it. */
3668 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
3669 if (tv
->mult_val
== const1_rtx
3670 && ! product_cheap_p (tv
->add_val
, v
->mult_val
))
3672 if (loop_dump_stream
)
3673 fprintf (loop_dump_stream
,
3674 "giv of insn %d: would need a multiply.\n",
3675 INSN_UID (v
->insn
));
3683 /* Reduce each giv that we decided to reduce. */
3685 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
3687 struct induction
*tv
;
3688 if (! v
->ignore
&& v
->same
== 0)
3690 v
->new_reg
= gen_reg_rtx (v
->mode
);
3692 /* For each place where the biv is incremented,
3693 add an insn to increment the new, reduced reg for the giv. */
3694 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
3696 if (tv
->mult_val
== const1_rtx
)
3697 emit_iv_add_mult (tv
->add_val
, v
->mult_val
,
3698 v
->new_reg
, v
->new_reg
, tv
->insn
);
3699 else /* tv->mult_val == const0_rtx */
3700 /* A multiply is acceptable here
3701 since this is presumed to be seldom executed. */
3702 emit_iv_add_mult (tv
->add_val
, v
->mult_val
,
3703 v
->add_val
, v
->new_reg
, tv
->insn
);
3706 /* Add code at loop start to initialize giv's reduced reg. */
3708 emit_iv_add_mult (bl
->initial_value
, v
->mult_val
,
3709 v
->add_val
, v
->new_reg
, loop_start
);
3713 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
3716 For each giv register that can be reduced now: if replaceable,
3717 substitute reduced reg wherever the old giv occurs;
3718 else add new move insn "giv_reg = reduced_reg".
3720 Also check for givs whose first use is their definition and whose
3721 last use is the definition of another giv. If so, it is likely
3722 dead and should not be used to eliminate a biv. */
3723 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
3725 if (v
->same
&& v
->same
->ignore
)
3731 if (v
->giv_type
== DEST_REG
3732 && regno_first_uid
[REGNO (v
->dest_reg
)] == INSN_UID (v
->insn
))
3734 struct induction
*v1
;
3736 for (v1
= bl
->giv
; v1
; v1
= v1
->next_iv
)
3737 if (regno_last_uid
[REGNO (v
->dest_reg
)] == INSN_UID (v1
->insn
))
3741 /* Update expression if this was combined, in case other giv was
3744 v
->new_reg
= replace_rtx (v
->new_reg
,
3745 v
->same
->dest_reg
, v
->same
->new_reg
);
3747 if (v
->giv_type
== DEST_ADDR
)
3748 /* Store reduced reg as the address in the memref where we found
3750 *v
->location
= v
->new_reg
;
3751 else if (v
->replaceable
)
3753 reg_map
[REGNO (v
->dest_reg
)] = v
->new_reg
;
3756 /* I can no longer duplicate the original problem. Perhaps
3757 this is unnecessary now? */
3759 /* Replaceable; it isn't strictly necessary to delete the old
3760 insn and emit a new one, because v->dest_reg is now dead.
3762 However, especially when unrolling loops, the special
3763 handling for (set REG0 REG1) in the second cse pass may
3764 make v->dest_reg live again. To avoid this problem, emit
3765 an insn to set the original giv reg from the reduced giv.
3766 We can not delete the original insn, since it may be part
3767 of a LIBCALL, and the code in flow that eliminates dead
3768 libcalls will fail if it is deleted. */
3769 emit_insn_after (gen_move_insn (v
->dest_reg
, v
->new_reg
),
3775 /* Not replaceable; emit an insn to set the original giv reg from
3776 the reduced giv, same as above. */
3777 emit_insn_after (gen_move_insn (v
->dest_reg
, v
->new_reg
),
3781 /* When a loop is reversed, givs which depend on the reversed
3782 biv, and which are live outside the loop, must be set to their
3783 correct final value. This insn is only needed if the giv is
3784 not replaceable. The correct final value is the same as the
3785 value that the giv starts the reversed loop with. */
3786 if (bl
->reversed
&& ! v
->replaceable
)
3787 emit_iv_add_mult (bl
->initial_value
, v
->mult_val
,
3788 v
->add_val
, v
->dest_reg
, end_insert_before
);
3789 else if (v
->final_value
)
3793 /* If the loop has multiple exits, emit the insn before the
3794 loop to ensure that it will always be executed no matter
3795 how the loop exits. Otherwise, emit the insn after the loop,
3796 since this is slightly more efficient. */
3797 if (loop_number_exit_labels
[uid_loop_num
[INSN_UID (loop_start
)]])
3798 insert_before
= loop_start
;
3800 insert_before
= end_insert_before
;
3801 emit_insn_before (gen_move_insn (v
->dest_reg
, v
->final_value
),
3805 /* If the insn to set the final value of the giv was emitted
3806 before the loop, then we must delete the insn inside the loop
3807 that sets it. If this is a LIBCALL, then we must delete
3808 every insn in the libcall. Note, however, that
3809 final_giv_value will only succeed when there are multiple
3810 exits if the giv is dead at each exit, hence it does not
3811 matter that the original insn remains because it is dead
3813 /* Delete the insn inside the loop that sets the giv since
3814 the giv is now set before (or after) the loop. */
3815 delete_insn (v
->insn
);
3819 if (loop_dump_stream
)
3821 fprintf (loop_dump_stream
, "giv at %d reduced to ",
3822 INSN_UID (v
->insn
));
3823 print_rtl (loop_dump_stream
, v
->new_reg
);
3824 fprintf (loop_dump_stream
, "\n");
3828 /* All the givs based on the biv bl have been reduced if they
3831 /* For each giv not marked as maybe dead that has been combined with a
3832 second giv, clear any "maybe dead" mark on that second giv.
3833 v->new_reg will either be or refer to the register of the giv it
3836 Doing this clearing avoids problems in biv elimination where a
3837 giv's new_reg is a complex value that can't be put in the insn but
3838 the giv combined with (with a reg as new_reg) is marked maybe_dead.
3839 Since the register will be used in either case, we'd prefer it be
3840 used from the simpler giv. */
3842 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
3843 if (! v
->maybe_dead
&& v
->same
)
3844 v
->same
->maybe_dead
= 0;
3846 /* Try to eliminate the biv, if it is a candidate.
3847 This won't work if ! all_reduced,
3848 since the givs we planned to use might not have been reduced.
3850 We have to be careful that we didn't initially think we could eliminate
3851 this biv because of a giv that we now think may be dead and shouldn't
3852 be used as a biv replacement.
3854 Also, there is the possibility that we may have a giv that looks
3855 like it can be used to eliminate a biv, but the resulting insn
3856 isn't valid. This can happen, for example, on the 88k, where a
3857 JUMP_INSN can compare a register only with zero. Attempts to
3858 replace it with a compare with a constant will fail.
3860 Note that in cases where this call fails, we may have replaced some
3861 of the occurrences of the biv with a giv, but no harm was done in
3862 doing so in the rare cases where it can occur. */
3864 if (all_reduced
== 1 && bl
->eliminable
3865 && maybe_eliminate_biv (bl
, loop_start
, end
, 1,
3866 threshold
, insn_count
))
3869 /* ?? If we created a new test to bypass the loop entirely,
3870 or otherwise drop straight in, based on this test, then
3871 we might want to rewrite it also. This way some later
3872 pass has more hope of removing the initialization of this
3875 /* If final_value != 0, then the biv may be used after loop end
3876 and we must emit an insn to set it just in case.
3878 Reversed bivs already have an insn after the loop setting their
3879 value, so we don't need another one. We can't calculate the
3880 proper final value for such a biv here anyways. */
3881 if (final_value
!= 0 && ! bl
->reversed
)
3885 /* If the loop has multiple exits, emit the insn before the
3886 loop to ensure that it will always be executed no matter
3887 how the loop exits. Otherwise, emit the insn after the
3888 loop, since this is slightly more efficient. */
3889 if (loop_number_exit_labels
[uid_loop_num
[INSN_UID (loop_start
)]])
3890 insert_before
= loop_start
;
3892 insert_before
= end_insert_before
;
3894 emit_insn_before (gen_move_insn (bl
->biv
->dest_reg
, final_value
),
3899 /* Delete all of the instructions inside the loop which set
3900 the biv, as they are all dead. If is safe to delete them,
3901 because an insn setting a biv will never be part of a libcall. */
3902 /* However, deleting them will invalidate the regno_last_uid info,
3903 so keeping them around is more convenient. Final_biv_value
3904 will only succeed when there are multiple exits if the biv
3905 is dead at each exit, hence it does not matter that the original
3906 insn remains, because it is dead anyways. */
3907 for (v
= bl
->biv
; v
; v
= v
->next_iv
)
3908 delete_insn (v
->insn
);
3911 if (loop_dump_stream
)
3912 fprintf (loop_dump_stream
, "Reg %d: biv eliminated\n",
3917 /* Go through all the instructions in the loop, making all the
3918 register substitutions scheduled in REG_MAP. */
3920 for (p
= loop_start
; p
!= end
; p
= NEXT_INSN (p
))
3921 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
3922 || GET_CODE (p
) == CALL_INSN
)
3924 replace_regs (PATTERN (p
), reg_map
, max_reg_before_loop
, 0);
3925 replace_regs (REG_NOTES (p
), reg_map
, max_reg_before_loop
, 0);
3928 /* Unroll loops from within strength reduction so that we can use the
3929 induction variable information that strength_reduce has already
3932 if (flag_unroll_loops
)
3933 unroll_loop (loop_end
, insn_count
, loop_start
, end_insert_before
, 1);
3935 if (loop_dump_stream
)
3936 fprintf (loop_dump_stream
, "\n");
3939 /* Return 1 if X is a valid source for an initial value (or as value being
3940 compared against in an initial test).
3942 X must be either a register or constant and must not be clobbered between
3943 the current insn and the start of the loop.
3945 INSN is the insn containing X. */
3948 valid_initial_value_p (x
, insn
, call_seen
, loop_start
)
3957 /* Only consider pseudos we know about initialized in insns whose luids
3959 if (GET_CODE (x
) != REG
3960 || REGNO (x
) >= max_reg_before_loop
)
3963 /* Don't use call-clobbered registers across a call which clobbers it. On
3964 some machines, don't use any hard registers at all. */
3965 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
3966 #ifndef SMALL_REGISTER_CLASSES
3967 && call_used_regs
[REGNO (x
)] && call_seen
3972 /* Don't use registers that have been clobbered before the start of the
3974 if (reg_set_between_p (x
, insn
, loop_start
))
3980 /* Scan X for memory refs and check each memory address
3981 as a possible giv. INSN is the insn whose pattern X comes from.
3982 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
3983 every loop iteration. */
3986 find_mem_givs (x
, insn
, not_every_iteration
, loop_start
, loop_end
)
3989 int not_every_iteration
;
3990 rtx loop_start
, loop_end
;
3993 register enum rtx_code code
;
3999 code
= GET_CODE (x
);
4023 benefit
= general_induction_var (XEXP (x
, 0),
4024 &src_reg
, &add_val
, &mult_val
);
4026 /* Don't make a DEST_ADDR giv with mult_val == 1 && add_val == 0.
4027 Such a giv isn't useful. */
4028 if (benefit
> 0 && (mult_val
!= const1_rtx
|| add_val
!= const0_rtx
))
4030 /* Found one; record it. */
4032 = (struct induction
*) oballoc (sizeof (struct induction
));
4034 record_giv (v
, insn
, src_reg
, addr_placeholder
, mult_val
,
4035 add_val
, benefit
, DEST_ADDR
, not_every_iteration
,
4036 &XEXP (x
, 0), loop_start
, loop_end
);
4038 v
->mem_mode
= GET_MODE (x
);
4044 /* Recursively scan the subexpressions for other mem refs. */
4046 fmt
= GET_RTX_FORMAT (code
);
4047 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
4049 find_mem_givs (XEXP (x
, i
), insn
, not_every_iteration
, loop_start
,
4051 else if (fmt
[i
] == 'E')
4052 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
4053 find_mem_givs (XVECEXP (x
, i
, j
), insn
, not_every_iteration
,
4054 loop_start
, loop_end
);
4057 /* Fill in the data about one biv update.
4058 V is the `struct induction' in which we record the biv. (It is
4059 allocated by the caller, with alloca.)
4060 INSN is the insn that sets it.
4061 DEST_REG is the biv's reg.
4063 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
4064 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
4065 being set to INC_VAL.
4067 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
4068 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
4069 can be executed more than once per iteration. If MAYBE_MULTIPLE
4070 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
4071 executed exactly once per iteration. */
4074 record_biv (v
, insn
, dest_reg
, inc_val
, mult_val
,
4075 not_every_iteration
, maybe_multiple
)
4076 struct induction
*v
;
4081 int not_every_iteration
;
4084 struct iv_class
*bl
;
4087 v
->src_reg
= dest_reg
;
4088 v
->dest_reg
= dest_reg
;
4089 v
->mult_val
= mult_val
;
4090 v
->add_val
= inc_val
;
4091 v
->mode
= GET_MODE (dest_reg
);
4092 v
->always_computable
= ! not_every_iteration
;
4093 v
->maybe_multiple
= maybe_multiple
;
4095 /* Add this to the reg's iv_class, creating a class
4096 if this is the first incrementation of the reg. */
4098 bl
= reg_biv_class
[REGNO (dest_reg
)];
4101 /* Create and initialize new iv_class. */
4103 bl
= (struct iv_class
*) oballoc (sizeof (struct iv_class
));
4105 bl
->regno
= REGNO (dest_reg
);
4111 /* Set initial value to the reg itself. */
4112 bl
->initial_value
= dest_reg
;
4113 /* We haven't seen the initializing insn yet */
4116 bl
->initial_test
= 0;
4117 bl
->incremented
= 0;
4122 /* Add this class to loop_iv_list. */
4123 bl
->next
= loop_iv_list
;
4126 /* Put it in the array of biv register classes. */
4127 reg_biv_class
[REGNO (dest_reg
)] = bl
;
4130 /* Update IV_CLASS entry for this biv. */
4131 v
->next_iv
= bl
->biv
;
4134 if (mult_val
== const1_rtx
)
4135 bl
->incremented
= 1;
4137 if (loop_dump_stream
)
4139 fprintf (loop_dump_stream
,
4140 "Insn %d: possible biv, reg %d,",
4141 INSN_UID (insn
), REGNO (dest_reg
));
4142 if (GET_CODE (inc_val
) == CONST_INT
)
4143 fprintf (loop_dump_stream
, " const = %d\n",
4147 fprintf (loop_dump_stream
, " const = ");
4148 print_rtl (loop_dump_stream
, inc_val
);
4149 fprintf (loop_dump_stream
, "\n");
4154 /* Fill in the data about one giv.
4155 V is the `struct induction' in which we record the giv. (It is
4156 allocated by the caller, with alloca.)
4157 INSN is the insn that sets it.
4158 BENEFIT estimates the savings from deleting this insn.
4159 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
4160 into a register or is used as a memory address.
4162 SRC_REG is the biv reg which the giv is computed from.
4163 DEST_REG is the giv's reg (if the giv is stored in a reg).
4164 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
4165 LOCATION points to the place where this giv's value appears in INSN. */
4168 record_giv (v
, insn
, src_reg
, dest_reg
, mult_val
, add_val
, benefit
,
4169 type
, not_every_iteration
, location
, loop_start
, loop_end
)
4170 struct induction
*v
;
4174 rtx mult_val
, add_val
;
4177 int not_every_iteration
;
4179 rtx loop_start
, loop_end
;
4181 struct induction
*b
;
4182 struct iv_class
*bl
;
4183 rtx set
= single_set (insn
);
4187 v
->src_reg
= src_reg
;
4189 v
->dest_reg
= dest_reg
;
4190 v
->mult_val
= mult_val
;
4191 v
->add_val
= add_val
;
4192 v
->benefit
= benefit
;
4193 v
->location
= location
;
4195 v
->combined_with
= 0;
4196 v
->maybe_multiple
= 0;
4198 v
->derive_adjustment
= 0;
4204 /* The v->always_computable field is used in update_giv_derive, to
4205 determine whether a giv can be used to derive another giv. For a
4206 DEST_REG giv, INSN computes a new value for the giv, so its value
4207 isn't computable if INSN insn't executed every iteration.
4208 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
4209 it does not compute a new value. Hence the value is always computable
4210 regardless of whether INSN is executed each iteration. */
4212 if (type
== DEST_ADDR
)
4213 v
->always_computable
= 1;
4215 v
->always_computable
= ! not_every_iteration
;
4217 if (type
== DEST_ADDR
)
4219 v
->mode
= GET_MODE (*location
);
4223 else /* type == DEST_REG */
4225 v
->mode
= GET_MODE (SET_DEST (set
));
4227 v
->lifetime
= (uid_luid
[regno_last_uid
[REGNO (dest_reg
)]]
4228 - uid_luid
[regno_first_uid
[REGNO (dest_reg
)]]);
4230 v
->times_used
= n_times_used
[REGNO (dest_reg
)];
4232 /* If the lifetime is zero, it means that this register is
4233 really a dead store. So mark this as a giv that can be
4234 ignored. This will not prevent the biv from being eliminated. */
4235 if (v
->lifetime
== 0)
4238 reg_iv_type
[REGNO (dest_reg
)] = GENERAL_INDUCT
;
4239 reg_iv_info
[REGNO (dest_reg
)] = v
;
4242 /* Add the giv to the class of givs computed from one biv. */
4244 bl
= reg_biv_class
[REGNO (src_reg
)];
4247 v
->next_iv
= bl
->giv
;
4249 /* Don't count DEST_ADDR. This is supposed to count the number of
4250 insns that calculate givs. */
4251 if (type
== DEST_REG
)
4253 bl
->total_benefit
+= benefit
;
4256 /* Fatal error, biv missing for this giv? */
4259 if (type
== DEST_ADDR
)
4263 /* The giv can be replaced outright by the reduced register only if all
4264 of the following conditions are true:
4265 - the insn that sets the giv is always executed on any iteration
4266 on which the giv is used at all
4267 (there are two ways to deduce this:
4268 either the insn is executed on every iteration,
4269 or all uses follow that insn in the same basic block),
4270 - the giv is not used outside the loop
4271 - no assignments to the biv occur during the giv's lifetime. */
4273 if (regno_first_uid
[REGNO (dest_reg
)] == INSN_UID (insn
)
4274 /* Previous line always fails if INSN was moved by loop opt. */
4275 && uid_luid
[regno_last_uid
[REGNO (dest_reg
)]] < INSN_LUID (loop_end
)
4276 && (! not_every_iteration
4277 || last_use_this_basic_block (dest_reg
, insn
)))
4279 /* Now check that there are no assignments to the biv within the
4280 giv's lifetime. This requires two separate checks. */
4282 /* Check each biv update, and fail if any are between the first
4283 and last use of the giv.
4285 If this loop contains an inner loop that was unrolled, then
4286 the insn modifying the biv may have been emitted by the loop
4287 unrolling code, and hence does not have a valid luid. Just
4288 mark the biv as not replaceable in this case. It is not very
4289 useful as a biv, because it is used in two different loops.
4290 It is very unlikely that we would be able to optimize the giv
4291 using this biv anyways. */
4294 for (b
= bl
->biv
; b
; b
= b
->next_iv
)
4296 if (INSN_UID (b
->insn
) >= max_uid_for_loop
4297 || ((uid_luid
[INSN_UID (b
->insn
)]
4298 >= uid_luid
[regno_first_uid
[REGNO (dest_reg
)]])
4299 && (uid_luid
[INSN_UID (b
->insn
)]
4300 <= uid_luid
[regno_last_uid
[REGNO (dest_reg
)]])))
4303 v
->not_replaceable
= 1;
4308 /* Check each insn between the first and last use of the giv,
4309 and fail if any of them are branches that jump to a named label
4310 outside this range, but still inside the loop. This catches
4311 cases of spaghetti code where the execution order of insns
4312 is not linear, and hence the above test fails. For example,
4313 in the following code, j is not replaceable:
4314 for (i = 0; i < 100; ) {
4315 L0: j = 4*i; goto L1;
4319 printf ("k = %d\n", k); }
4320 This test is conservative, but this test succeeds rarely enough
4321 that it isn't a problem. See also check_final_value below. */
4325 INSN_UID (p
) >= max_uid_for_loop
4326 || INSN_LUID (p
) < uid_luid
[regno_last_uid
[REGNO (dest_reg
)]];
4329 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
)
4330 && LABEL_NAME (JUMP_LABEL (p
))
4331 && ((INSN_LUID (JUMP_LABEL (p
)) > INSN_LUID (loop_start
)
4332 && (INSN_LUID (JUMP_LABEL (p
))
4333 < uid_luid
[regno_first_uid
[REGNO (dest_reg
)]]))
4334 || (INSN_LUID (JUMP_LABEL (p
)) < INSN_LUID (loop_end
)
4335 && (INSN_LUID (JUMP_LABEL (p
))
4336 > uid_luid
[regno_last_uid
[REGNO (dest_reg
)]]))))
4339 v
->not_replaceable
= 1;
4341 if (loop_dump_stream
)
4342 fprintf (loop_dump_stream
,
4343 "Found branch outside giv lifetime.\n");
4351 /* May still be replaceable, we don't have enough info here to
4354 v
->not_replaceable
= 0;
4358 if (loop_dump_stream
)
4360 if (type
== DEST_REG
)
4361 fprintf (loop_dump_stream
, "Insn %d: giv reg %d",
4362 INSN_UID (insn
), REGNO (dest_reg
));
4364 fprintf (loop_dump_stream
, "Insn %d: dest address",
4367 fprintf (loop_dump_stream
, " src reg %d benefit %d",
4368 REGNO (src_reg
), v
->benefit
);
4369 fprintf (loop_dump_stream
, " used %d lifetime %d",
4370 v
->times_used
, v
->lifetime
);
4373 fprintf (loop_dump_stream
, " replaceable");
4375 if (GET_CODE (mult_val
) == CONST_INT
)
4376 fprintf (loop_dump_stream
, " mult %d",
4380 fprintf (loop_dump_stream
, " mult ");
4381 print_rtl (loop_dump_stream
, mult_val
);
4384 if (GET_CODE (add_val
) == CONST_INT
)
4385 fprintf (loop_dump_stream
, " add %d",
4389 fprintf (loop_dump_stream
, " add ");
4390 print_rtl (loop_dump_stream
, add_val
);
4394 if (loop_dump_stream
)
4395 fprintf (loop_dump_stream
, "\n");
4400 /* All this does is determine whether a giv can be made replaceable because
4401 its final value can be calculated. This code can not be part of record_giv
4402 above, because final_giv_value requires that the number of loop iterations
4403 be known, and that can not be accurately calculated until after all givs
4404 have been identified. */
4407 check_final_value (v
, loop_start
, loop_end
)
4408 struct induction
*v
;
4409 rtx loop_start
, loop_end
;
4411 struct iv_class
*bl
;
4412 rtx final_value
= 0;
4415 bl
= reg_biv_class
[REGNO (v
->src_reg
)];
4417 /* DEST_ADDR givs will never reach here, because they are always marked
4418 replaceable above in record_giv. */
4420 /* The giv can be replaced outright by the reduced register only if all
4421 of the following conditions are true:
4422 - the insn that sets the giv is always executed on any iteration
4423 on which the giv is used at all
4424 (there are two ways to deduce this:
4425 either the insn is executed on every iteration,
4426 or all uses follow that insn in the same basic block),
4427 - its final value can be calculated (this condition is different
4428 than the one above in record_giv)
4429 - no assignments to the biv occur during the giv's lifetime. */
4432 /* This is only called now when replaceable is known to be false. */
4433 /* Clear replaceable, so that it won't confuse final_giv_value. */
4437 if ((final_value
= final_giv_value (v
, loop_start
, loop_end
))
4438 && (v
->always_computable
|| last_use_this_basic_block (v
->dest_reg
, v
->insn
)))
4440 int biv_increment_seen
= 0;
4446 /* When trying to determine whether or not a biv increment occurs
4447 during the lifetime of the giv, we can ignore uses of the variable
4448 outside the loop because final_value is true. Hence we can not
4449 use regno_last_uid and regno_first_uid as above in record_giv. */
4451 /* Search the loop to determine whether any assignments to the
4452 biv occur during the giv's lifetime. Start with the insn
4453 that sets the giv, and search around the loop until we come
4454 back to that insn again.
4456 Also fail if there is a jump within the giv's lifetime that jumps
4457 to somewhere outside the lifetime but still within the loop. This
4458 catches spaghetti code where the execution order is not linear, and
4459 hence the above test fails. Here we assume that the giv lifetime
4460 does not extend from one iteration of the loop to the next, so as
4461 to make the test easier. Since the lifetime isn't known yet,
4462 this requires two loops. See also record_giv above. */
4464 last_giv_use
= v
->insn
;
4470 p
= NEXT_INSN (loop_start
);
4474 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
4475 || GET_CODE (p
) == CALL_INSN
)
4477 if (biv_increment_seen
)
4479 if (reg_mentioned_p (v
->dest_reg
, PATTERN (p
)))
4482 v
->not_replaceable
= 1;
4486 else if (GET_CODE (PATTERN (p
)) == SET
4487 && SET_DEST (PATTERN (p
)) == v
->src_reg
)
4488 biv_increment_seen
= 1;
4489 else if (reg_mentioned_p (v
->dest_reg
, PATTERN (p
)))
4494 /* Now that the lifetime of the giv is known, check for branches
4495 from within the lifetime to outside the lifetime if it is still
4505 p
= NEXT_INSN (loop_start
);
4506 if (p
== last_giv_use
)
4509 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
)
4510 && LABEL_NAME (JUMP_LABEL (p
))
4511 && ((INSN_LUID (JUMP_LABEL (p
)) < INSN_LUID (v
->insn
)
4512 && INSN_LUID (JUMP_LABEL (p
)) > INSN_LUID (loop_start
))
4513 || (INSN_LUID (JUMP_LABEL (p
)) > INSN_LUID (last_giv_use
)
4514 && INSN_LUID (JUMP_LABEL (p
)) < INSN_LUID (loop_end
))))
4517 v
->not_replaceable
= 1;
4519 if (loop_dump_stream
)
4520 fprintf (loop_dump_stream
,
4521 "Found branch outside giv lifetime.\n");
4528 /* If it is replaceable, then save the final value. */
4530 v
->final_value
= final_value
;
4533 if (loop_dump_stream
&& v
->replaceable
)
4534 fprintf (loop_dump_stream
, "Insn %d: giv reg %d final_value replaceable\n",
4535 INSN_UID (v
->insn
), REGNO (v
->dest_reg
));
4538 /* Update the status of whether a giv can derive other givs.
4540 We need to do something special if there is or may be an update to the biv
4541 between the time the giv is defined and the time it is used to derive
4544 In addition, a giv that is only conditionally set is not allowed to
4545 derive another giv once a label has been passed.
4547 The cases we look at are when a label or an update to a biv is passed. */
4550 update_giv_derive (p
)
4553 struct iv_class
*bl
;
4554 struct induction
*biv
, *giv
;
4558 /* Search all IV classes, then all bivs, and finally all givs.
4560 There are three cases we are concerned with. First we have the situation
4561 of a giv that is only updated conditionally. In that case, it may not
4562 derive any givs after a label is passed.
4564 The second case is when a biv update occurs, or may occur, after the
4565 definition of a giv. For certain biv updates (see below) that are
4566 known to occur between the giv definition and use, we can adjust the
4567 giv definition. For others, or when the biv update is conditional,
4568 we must prevent the giv from deriving any other givs. There are two
4569 sub-cases within this case.
4571 If this is a label, we are concerned with any biv update that is done
4572 conditionally, since it may be done after the giv is defined followed by
4573 a branch here (actually, we need to pass both a jump and a label, but
4574 this extra tracking doesn't seem worth it).
4576 If this is a jump, we are concerned about any biv update that may be
4577 executed multiple times. We are actually only concerned about
4578 backward jumps, but it is probably not worth performing the test
4579 on the jump again here.
4581 If this is a biv update, we must adjust the giv status to show that a
4582 subsequent biv update was performed. If this adjustment cannot be done,
4583 the giv cannot derive further givs. */
4585 for (bl
= loop_iv_list
; bl
; bl
= bl
->next
)
4586 for (biv
= bl
->biv
; biv
; biv
= biv
->next_iv
)
4587 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
4590 for (giv
= bl
->giv
; giv
; giv
= giv
->next_iv
)
4592 /* If cant_derive is already true, there is no point in
4593 checking all of these conditions again. */
4594 if (giv
->cant_derive
)
4597 /* If this giv is conditionally set and we have passed a label,
4598 it cannot derive anything. */
4599 if (GET_CODE (p
) == CODE_LABEL
&& ! giv
->always_computable
)
4600 giv
->cant_derive
= 1;
4602 /* Skip givs that have mult_val == 0, since
4603 they are really invariants. Also skip those that are
4604 replaceable, since we know their lifetime doesn't contain
4606 else if (giv
->mult_val
== const0_rtx
|| giv
->replaceable
)
4609 /* The only way we can allow this giv to derive another
4610 is if this is a biv increment and we can form the product
4611 of biv->add_val and giv->mult_val. In this case, we will
4612 be able to compute a compensation. */
4613 else if (biv
->insn
== p
)
4617 if (biv
->mult_val
== const1_rtx
)
4618 tem
= simplify_giv_expr (gen_rtx (MULT
, giv
->mode
,
4623 if (tem
&& giv
->derive_adjustment
)
4624 tem
= simplify_giv_expr (gen_rtx (PLUS
, giv
->mode
, tem
,
4625 giv
->derive_adjustment
),
4628 giv
->derive_adjustment
= tem
;
4630 giv
->cant_derive
= 1;
4632 else if ((GET_CODE (p
) == CODE_LABEL
&& ! biv
->always_computable
)
4633 || (GET_CODE (p
) == JUMP_INSN
&& biv
->maybe_multiple
))
4634 giv
->cant_derive
= 1;
4639 /* Check whether an insn is an increment legitimate for a basic induction var.
4640 X is the source of the insn.
4641 DEST_REG is the putative biv, also the destination of the insn.
4642 We accept patterns of these forms:
4643 REG = REG + INVARIANT
4644 REG = INVARIANT + REG
4645 REG = REG - CONSTANT
4647 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
4648 and store the additive term into *INC_VAL.
4650 If X is an assignment of an invariant into DEST_REG, we set
4651 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
4653 Otherwise we return 0. */
4656 basic_induction_var (x
, dest_reg
, inc_val
, mult_val
)
4662 register enum rtx_code code
;
4665 code
= GET_CODE (x
);
4669 if (XEXP (x
, 0) == dest_reg
)
4671 else if (XEXP (x
, 1) == dest_reg
)
4676 if (invariant_p (arg
) != 1)
4680 *mult_val
= const1_rtx
;
4684 if (XEXP (x
, 0) == dest_reg
4685 && GET_CODE (XEXP (x
, 1)) == CONST_INT
)
4686 *inc_val
= GEN_INT (- INTVAL (XEXP (x
, 1)));
4690 *mult_val
= const1_rtx
;
4693 /* Can accept constant setting of biv only when inside inner most loop.
4694 Otherwise, a biv of an inner loop may be incorrectly recognized
4695 as a biv of the outer loop,
4696 causing code to be moved INTO the inner loop. */
4699 if (invariant_p (x
) != 1)
4704 if (loops_enclosed
== 1)
4707 *mult_val
= const0_rtx
;
4718 /* A general induction variable (giv) is any quantity that is a linear
4719 function of a basic induction variable,
4720 i.e. giv = biv * mult_val + add_val.
4721 The coefficients can be any loop invariant quantity.
4722 A giv need not be computed directly from the biv;
4723 it can be computed by way of other givs. */
4725 /* Determine whether X computes a giv.
4726 If it does, return a nonzero value
4727 which is the benefit from eliminating the computation of X;
4728 set *SRC_REG to the register of the biv that it is computed from;
4729 set *ADD_VAL and *MULT_VAL to the coefficients,
4730 such that the value of X is biv * mult + add; */
4733 general_induction_var (x
, src_reg
, add_val
, mult_val
)
4743 /* If this is an invariant, forget it, it isn't a giv. */
4744 if (invariant_p (x
) == 1)
4747 /* See if the expression could be a giv and get its form.
4748 Mark our place on the obstack in case we don't find a giv. */
4749 storage
= (char *) oballoc (0);
4750 x
= simplify_giv_expr (x
, &benefit
);
4757 switch (GET_CODE (x
))
4761 /* Since this is now an invariant and wasn't before, it must be a giv
4762 with MULT_VAL == 0. It doesn't matter which BIV we associate this
4764 *src_reg
= loop_iv_list
->biv
->dest_reg
;
4765 *mult_val
= const0_rtx
;
4770 /* This is equivalent to a BIV. */
4772 *mult_val
= const1_rtx
;
4773 *add_val
= const0_rtx
;
4777 /* Either (plus (biv) (invar)) or
4778 (plus (mult (biv) (invar_1)) (invar_2)). */
4779 if (GET_CODE (XEXP (x
, 0)) == MULT
)
4781 *src_reg
= XEXP (XEXP (x
, 0), 0);
4782 *mult_val
= XEXP (XEXP (x
, 0), 1);
4786 *src_reg
= XEXP (x
, 0);
4787 *mult_val
= const1_rtx
;
4789 *add_val
= XEXP (x
, 1);
4793 /* ADD_VAL is zero. */
4794 *src_reg
= XEXP (x
, 0);
4795 *mult_val
= XEXP (x
, 1);
4796 *add_val
= const0_rtx
;
4803 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
4804 unless they are CONST_INT). */
4805 if (GET_CODE (*add_val
) == USE
)
4806 *add_val
= XEXP (*add_val
, 0);
4807 if (GET_CODE (*mult_val
) == USE
)
4808 *mult_val
= XEXP (*mult_val
, 0);
4810 benefit
+= rtx_cost (orig_x
, SET
);
4812 /* Always return some benefit if this is a giv so it will be detected
4813 as such. This allows elimination of bivs that might otherwise
4814 not be eliminated. */
4815 return benefit
== 0 ? 1 : benefit
;
4818 /* Given an expression, X, try to form it as a linear function of a biv.
4819 We will canonicalize it to be of the form
4820 (plus (mult (BIV) (invar_1))
4822 with possible degeneracies.
4824 The invariant expressions must each be of a form that can be used as a
4825 machine operand. We surround then with a USE rtx (a hack, but localized
4826 and certainly unambiguous!) if not a CONST_INT for simplicity in this
4827 routine; it is the caller's responsibility to strip them.
4829 If no such canonicalization is possible (i.e., two biv's are used or an
4830 expression that is neither invariant nor a biv or giv), this routine
4833 For a non-zero return, the result will have a code of CONST_INT, USE,
4834 REG (for a BIV), PLUS, or MULT. No other codes will occur.
4836 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
4839 simplify_giv_expr (x
, benefit
)
4843 enum machine_mode mode
= GET_MODE (x
);
4847 /* If this is not an integer mode, or if we cannot do arithmetic in this
4848 mode, this can't be a giv. */
4849 if (mode
!= VOIDmode
4850 && (GET_MODE_CLASS (mode
) != MODE_INT
4851 || GET_MODE_BITSIZE (mode
) > HOST_BITS_PER_WIDE_INT
))
4854 switch (GET_CODE (x
))
4857 arg0
= simplify_giv_expr (XEXP (x
, 0), benefit
);
4858 arg1
= simplify_giv_expr (XEXP (x
, 1), benefit
);
4859 if (arg0
== 0 || arg1
== 0)
4862 /* Put constant last, CONST_INT last if both constant. */
4863 if ((GET_CODE (arg0
) == USE
4864 || GET_CODE (arg0
) == CONST_INT
)
4865 && GET_CODE (arg1
) != CONST_INT
)
4866 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
4868 /* Handle addition of zero, then addition of an invariant. */
4869 if (arg1
== const0_rtx
)
4871 else if (GET_CODE (arg1
) == CONST_INT
|| GET_CODE (arg1
) == USE
)
4872 switch (GET_CODE (arg0
))
4876 /* Both invariant. Only valid if sum is machine operand.
4877 First strip off possible USE on first operand. */
4878 if (GET_CODE (arg0
) == USE
)
4879 arg0
= XEXP (arg0
, 0);
4882 if (CONSTANT_P (arg0
) && GET_CODE (arg1
) == CONST_INT
)
4884 tem
= plus_constant (arg0
, INTVAL (arg1
));
4885 if (GET_CODE (tem
) != CONST_INT
)
4886 tem
= gen_rtx (USE
, mode
, tem
);
4893 /* biv + invar or mult + invar. Return sum. */
4894 return gen_rtx (PLUS
, mode
, arg0
, arg1
);
4897 /* (a + invar_1) + invar_2. Associate. */
4898 return simplify_giv_expr (gen_rtx (PLUS
, mode
,
4900 gen_rtx (PLUS
, mode
,
4901 XEXP (arg0
, 1), arg1
)),
4908 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
4909 MULT to reduce cases. */
4910 if (GET_CODE (arg0
) == REG
)
4911 arg0
= gen_rtx (MULT
, mode
, arg0
, const1_rtx
);
4912 if (GET_CODE (arg1
) == REG
)
4913 arg1
= gen_rtx (MULT
, mode
, arg1
, const1_rtx
);
4915 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
4916 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
4917 Recurse to associate the second PLUS. */
4918 if (GET_CODE (arg1
) == MULT
)
4919 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
4921 if (GET_CODE (arg1
) == PLUS
)
4922 return simplify_giv_expr (gen_rtx (PLUS
, mode
,
4923 gen_rtx (PLUS
, mode
,
4924 arg0
, XEXP (arg1
, 0)),
4928 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
4929 if (GET_CODE (arg0
) != MULT
|| GET_CODE (arg1
) != MULT
)
4932 if (XEXP (arg0
, 0) != XEXP (arg1
, 0))
4935 return simplify_giv_expr (gen_rtx (MULT
, mode
,
4937 gen_rtx (PLUS
, mode
,
4943 /* Handle "a - b" as "a + b * (-1)". */
4944 return simplify_giv_expr (gen_rtx (PLUS
, mode
,
4946 gen_rtx (MULT
, mode
,
4947 XEXP (x
, 1), constm1_rtx
)),
4951 arg0
= simplify_giv_expr (XEXP (x
, 0), benefit
);
4952 arg1
= simplify_giv_expr (XEXP (x
, 1), benefit
);
4953 if (arg0
== 0 || arg1
== 0)
4956 /* Put constant last, CONST_INT last if both constant. */
4957 if ((GET_CODE (arg0
) == USE
|| GET_CODE (arg0
) == CONST_INT
)
4958 && GET_CODE (arg1
) != CONST_INT
)
4959 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
4961 /* If second argument is not now constant, not giv. */
4962 if (GET_CODE (arg1
) != USE
&& GET_CODE (arg1
) != CONST_INT
)
4965 /* Handle multiply by 0 or 1. */
4966 if (arg1
== const0_rtx
)
4969 else if (arg1
== const1_rtx
)
4972 switch (GET_CODE (arg0
))
4975 /* biv * invar. Done. */
4976 return gen_rtx (MULT
, mode
, arg0
, arg1
);
4979 /* Product of two constants. */
4980 return GEN_INT (INTVAL (arg0
) * INTVAL (arg1
));
4983 /* invar * invar. Not giv. */
4987 /* (a * invar_1) * invar_2. Associate. */
4988 return simplify_giv_expr (gen_rtx (MULT
, mode
,
4990 gen_rtx (MULT
, mode
,
4991 XEXP (arg0
, 1), arg1
)),
4995 /* (a + invar_1) * invar_2. Distribute. */
4996 return simplify_giv_expr (gen_rtx (PLUS
, mode
,
4997 gen_rtx (MULT
, mode
,
4998 XEXP (arg0
, 0), arg1
),
4999 gen_rtx (MULT
, mode
,
5000 XEXP (arg0
, 1), arg1
)),
5009 /* Shift by constant is multiply by power of two. */
5010 if (GET_CODE (XEXP (x
, 1)) != CONST_INT
)
5013 return simplify_giv_expr (gen_rtx (MULT
, mode
,
5015 GEN_INT ((HOST_WIDE_INT
) 1
5016 << INTVAL (XEXP (x
, 1)))),
5020 /* "-a" is "a * (-1)" */
5021 return simplify_giv_expr (gen_rtx (MULT
, mode
, XEXP (x
, 0), constm1_rtx
),
5025 /* "~a" is "-a - 1". Silly, but easy. */
5026 return simplify_giv_expr (gen_rtx (MINUS
, mode
,
5027 gen_rtx (NEG
, mode
, XEXP (x
, 0)),
5032 /* Already in proper form for invariant. */
5036 /* If this is a new register, we can't deal with it. */
5037 if (REGNO (x
) >= max_reg_before_loop
)
5040 /* Check for biv or giv. */
5041 switch (reg_iv_type
[REGNO (x
)])
5045 case GENERAL_INDUCT
:
5047 struct induction
*v
= reg_iv_info
[REGNO (x
)];
5049 /* Form expression from giv and add benefit. Ensure this giv
5050 can derive another and subtract any needed adjustment if so. */
5051 *benefit
+= v
->benefit
;
5055 tem
= gen_rtx (PLUS
, mode
, gen_rtx (MULT
, mode
,
5056 v
->src_reg
, v
->mult_val
),
5058 if (v
->derive_adjustment
)
5059 tem
= gen_rtx (MINUS
, mode
, tem
, v
->derive_adjustment
);
5060 return simplify_giv_expr (tem
, benefit
);
5064 /* Fall through to general case. */
5066 /* If invariant, return as USE (unless CONST_INT).
5067 Otherwise, not giv. */
5068 if (GET_CODE (x
) == USE
)
5071 if (invariant_p (x
) == 1)
5073 if (GET_CODE (x
) == CONST_INT
)
5076 return gen_rtx (USE
, mode
, x
);
5083 /* Help detect a giv that is calculated by several consecutive insns;
5087 The caller has already identified the first insn P as having a giv as dest;
5088 we check that all other insns that set the same register follow
5089 immediately after P, that they alter nothing else,
5090 and that the result of the last is still a giv.
5092 The value is 0 if the reg set in P is not really a giv.
5093 Otherwise, the value is the amount gained by eliminating
5094 all the consecutive insns that compute the value.
5096 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
5097 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
5099 The coefficients of the ultimate giv value are stored in
5100 *MULT_VAL and *ADD_VAL. */
5103 consec_sets_giv (first_benefit
, p
, src_reg
, dest_reg
,
5118 /* Indicate that this is a giv so that we can update the value produced in
5119 each insn of the multi-insn sequence.
5121 This induction structure will be used only by the call to
5122 general_induction_var below, so we can allocate it on our stack.
5123 If this is a giv, our caller will replace the induct var entry with
5124 a new induction structure. */
5126 = (struct induction
*) alloca (sizeof (struct induction
));
5127 v
->src_reg
= src_reg
;
5128 v
->mult_val
= *mult_val
;
5129 v
->add_val
= *add_val
;
5130 v
->benefit
= first_benefit
;
5132 v
->derive_adjustment
= 0;
5134 reg_iv_type
[REGNO (dest_reg
)] = GENERAL_INDUCT
;
5135 reg_iv_info
[REGNO (dest_reg
)] = v
;
5137 count
= n_times_set
[REGNO (dest_reg
)] - 1;
5142 code
= GET_CODE (p
);
5144 /* If libcall, skip to end of call sequence. */
5145 if (code
== INSN
&& (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
5149 && (set
= single_set (p
))
5150 && GET_CODE (SET_DEST (set
)) == REG
5151 && SET_DEST (set
) == dest_reg
5152 && ((benefit
= general_induction_var (SET_SRC (set
), &src_reg
,
5154 /* Giv created by equivalent expression. */
5155 || ((temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
5156 && (benefit
= general_induction_var (XEXP (temp
, 0), &src_reg
,
5157 add_val
, mult_val
))))
5158 && src_reg
== v
->src_reg
)
5160 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
5161 benefit
+= libcall_benefit (p
);
5164 v
->mult_val
= *mult_val
;
5165 v
->add_val
= *add_val
;
5166 v
->benefit
= benefit
;
5168 else if (code
!= NOTE
)
5170 /* Allow insns that set something other than this giv to a
5171 constant. Such insns are needed on machines which cannot
5172 include long constants and should not disqualify a giv. */
5174 && (set
= single_set (p
))
5175 && SET_DEST (set
) != dest_reg
5176 && CONSTANT_P (SET_SRC (set
)))
5179 reg_iv_type
[REGNO (dest_reg
)] = UNKNOWN_INDUCT
;
5187 /* Return an rtx, if any, that expresses giv G2 as a function of the register
5188 represented by G1. If no such expression can be found, or it is clear that
5189 it cannot possibly be a valid address, 0 is returned.
5191 To perform the computation, we note that
5194 where `v' is the biv.
5196 So G2 = (c/a) * G1 + (d - b*c/a) */
5200 express_from (g1
, g2
)
5201 struct induction
*g1
, *g2
;
5205 /* The value that G1 will be multiplied by must be a constant integer. Also,
5206 the only chance we have of getting a valid address is if b*c/a (see above
5207 for notation) is also an integer. */
5208 if (GET_CODE (g1
->mult_val
) != CONST_INT
5209 || GET_CODE (g2
->mult_val
) != CONST_INT
5210 || GET_CODE (g1
->add_val
) != CONST_INT
5211 || g1
->mult_val
== const0_rtx
5212 || INTVAL (g2
->mult_val
) % INTVAL (g1
->mult_val
) != 0)
5215 mult
= GEN_INT (INTVAL (g2
->mult_val
) / INTVAL (g1
->mult_val
));
5216 add
= plus_constant (g2
->add_val
, - INTVAL (g1
->add_val
) * INTVAL (mult
));
5218 /* Form simplified final result. */
5219 if (mult
== const0_rtx
)
5221 else if (mult
== const1_rtx
)
5222 mult
= g1
->dest_reg
;
5224 mult
= gen_rtx (MULT
, g2
->mode
, g1
->dest_reg
, mult
);
5226 if (add
== const0_rtx
)
5229 return gen_rtx (PLUS
, g2
->mode
, mult
, add
);
5233 /* Return 1 if giv G2 can be combined with G1. This means that G2 can use
5234 (either directly or via an address expression) a register used to represent
5235 G1. Set g2->new_reg to a represtation of G1 (normally just
5239 combine_givs_p (g1
, g2
)
5240 struct induction
*g1
, *g2
;
5244 /* If these givs are identical, they can be combined. */
5245 if (rtx_equal_p (g1
->mult_val
, g2
->mult_val
)
5246 && rtx_equal_p (g1
->add_val
, g2
->add_val
))
5248 g2
->new_reg
= g1
->dest_reg
;
5253 /* If G2 can be expressed as a function of G1 and that function is valid
5254 as an address and no more expensive than using a register for G2,
5255 the expression of G2 in terms of G1 can be used. */
5256 if (g2
->giv_type
== DEST_ADDR
5257 && (tem
= express_from (g1
, g2
)) != 0
5258 && memory_address_p (g2
->mem_mode
, tem
)
5259 && ADDRESS_COST (tem
) <= ADDRESS_COST (*g2
->location
))
5269 /* Check all pairs of givs for iv_class BL and see if any can be combined with
5270 any other. If so, point SAME to the giv combined with and set NEW_REG to
5271 be an expression (in terms of the other giv's DEST_REG) equivalent to the
5272 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
5276 struct iv_class
*bl
;
5278 struct induction
*g1
, *g2
;
5281 for (g1
= bl
->giv
; g1
; g1
= g1
->next_iv
)
5282 for (pass
= 0; pass
<= 1; pass
++)
5283 for (g2
= bl
->giv
; g2
; g2
= g2
->next_iv
)
5285 /* First try to combine with replaceable givs, then all givs. */
5286 && (g1
->replaceable
|| pass
== 1)
5287 /* If either has already been combined or is to be ignored, can't
5289 && ! g1
->ignore
&& ! g2
->ignore
&& ! g1
->same
&& ! g2
->same
5290 /* If something has been based on G2, G2 cannot itself be based
5291 on something else. */
5292 && ! g2
->combined_with
5293 && combine_givs_p (g1
, g2
))
5295 /* g2->new_reg set by `combine_givs_p' */
5297 g1
->combined_with
= 1;
5298 g1
->benefit
+= g2
->benefit
;
5299 /* ??? The new final_[bg]iv_value code does a much better job
5300 of finding replaceable giv's, and hence this code may no
5301 longer be necessary. */
5302 if (! g2
->replaceable
&& REG_USERVAR_P (g2
->dest_reg
))
5303 g1
->benefit
-= copy_cost
;
5304 g1
->lifetime
+= g2
->lifetime
;
5305 g1
->times_used
+= g2
->times_used
;
5307 if (loop_dump_stream
)
5308 fprintf (loop_dump_stream
, "giv at %d combined with giv at %d\n",
5309 INSN_UID (g2
->insn
), INSN_UID (g1
->insn
));
5313 /* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
5316 emit_iv_add_mult (b
, m
, a
, reg
, insert_before
)
5317 rtx b
; /* initial value of basic induction variable */
5318 rtx m
; /* multiplicative constant */
5319 rtx a
; /* additive constant */
5320 rtx reg
; /* destination register */
5326 /* Prevent unexpected sharing of these rtx. */
5330 /* Increase the lifetime of any invariants moved further in code. */
5331 update_reg_last_use (a
, insert_before
);
5332 update_reg_last_use (b
, insert_before
);
5333 update_reg_last_use (m
, insert_before
);
5336 result
= expand_mult_add (b
, reg
, m
, a
, GET_MODE (reg
), 0);
5338 emit_move_insn (reg
, result
);
5339 seq
= gen_sequence ();
5342 emit_insn_before (seq
, insert_before
);
5345 /* Test whether A * B can be computed without
5346 an actual multiply insn. Value is 1 if so. */
5349 product_cheap_p (a
, b
)
5355 struct obstack
*old_rtl_obstack
= rtl_obstack
;
5356 char *storage
= (char *) obstack_alloc (&temp_obstack
, 0);
5359 /* If only one is constant, make it B. */
5360 if (GET_CODE (a
) == CONST_INT
)
5361 tmp
= a
, a
= b
, b
= tmp
;
5363 /* If first constant, both constant, so don't need multiply. */
5364 if (GET_CODE (a
) == CONST_INT
)
5367 /* If second not constant, neither is constant, so would need multiply. */
5368 if (GET_CODE (b
) != CONST_INT
)
5371 /* One operand is constant, so might not need multiply insn. Generate the
5372 code for the multiply and see if a call or multiply, or long sequence
5373 of insns is generated. */
5375 rtl_obstack
= &temp_obstack
;
5377 expand_mult (GET_MODE (a
), a
, b
, NULL_RTX
, 0);
5378 tmp
= gen_sequence ();
5381 if (GET_CODE (tmp
) == SEQUENCE
)
5383 if (XVEC (tmp
, 0) == 0)
5385 else if (XVECLEN (tmp
, 0) > 3)
5388 for (i
= 0; i
< XVECLEN (tmp
, 0); i
++)
5390 rtx insn
= XVECEXP (tmp
, 0, i
);
5392 if (GET_CODE (insn
) != INSN
5393 || (GET_CODE (PATTERN (insn
)) == SET
5394 && GET_CODE (SET_SRC (PATTERN (insn
))) == MULT
)
5395 || (GET_CODE (PATTERN (insn
)) == PARALLEL
5396 && GET_CODE (XVECEXP (PATTERN (insn
), 0, 0)) == SET
5397 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn
), 0, 0))) == MULT
))
5404 else if (GET_CODE (tmp
) == SET
5405 && GET_CODE (SET_SRC (tmp
)) == MULT
)
5407 else if (GET_CODE (tmp
) == PARALLEL
5408 && GET_CODE (XVECEXP (tmp
, 0, 0)) == SET
5409 && GET_CODE (SET_SRC (XVECEXP (tmp
, 0, 0))) == MULT
)
5412 /* Free any storage we obtained in generating this multiply and restore rtl
5413 allocation to its normal obstack. */
5414 obstack_free (&temp_obstack
, storage
);
5415 rtl_obstack
= old_rtl_obstack
;
5420 /* Check to see if loop can be terminated by a "decrement and branch until
5421 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
5422 Also try reversing an increment loop to a decrement loop
5423 to see if the optimization can be performed.
5424 Value is nonzero if optimization was performed. */
5426 /* This is useful even if the architecture doesn't have such an insn,
5427 because it might change a loops which increments from 0 to n to a loop
5428 which decrements from n to 0. A loop that decrements to zero is usually
5429 faster than one that increments from zero. */
5431 /* ??? This could be rewritten to use some of the loop unrolling procedures,
5432 such as approx_final_value, biv_total_increment, loop_iterations, and
5433 final_[bg]iv_value. */
5436 check_dbra_loop (loop_end
, insn_count
, loop_start
)
5441 struct iv_class
*bl
;
5446 enum rtx_code branch_code
;
5449 rtx before_comparison
;
5452 /* If last insn is a conditional branch, and the insn before tests a
5453 register value, try to optimize it. Otherwise, we can't do anything. */
5455 comparison
= get_condition_for_loop (PREV_INSN (loop_end
));
5456 if (comparison
== 0)
5459 /* Check all of the bivs to see if the compare uses one of them.
5460 Skip biv's set more than once because we can't guarantee that
5461 it will be zero on the last iteration. Also skip if the biv is
5462 used between its update and the test insn. */
5464 for (bl
= loop_iv_list
; bl
; bl
= bl
->next
)
5466 if (bl
->biv_count
== 1
5467 && bl
->biv
->dest_reg
== XEXP (comparison
, 0)
5468 && ! reg_used_between_p (regno_reg_rtx
[bl
->regno
], bl
->biv
->insn
,
5469 PREV_INSN (PREV_INSN (loop_end
))))
5476 /* Look for the case where the basic induction variable is always
5477 nonnegative, and equals zero on the last iteration.
5478 In this case, add a reg_note REG_NONNEG, which allows the
5479 m68k DBRA instruction to be used. */
5481 if (((GET_CODE (comparison
) == GT
5482 && GET_CODE (XEXP (comparison
, 1)) == CONST_INT
5483 && INTVAL (XEXP (comparison
, 1)) == -1)
5484 || (GET_CODE (comparison
) == NE
&& XEXP (comparison
, 1) == const0_rtx
))
5485 && GET_CODE (bl
->biv
->add_val
) == CONST_INT
5486 && INTVAL (bl
->biv
->add_val
) < 0)
5488 /* Initial value must be greater than 0,
5489 init_val % -dec_value == 0 to ensure that it equals zero on
5490 the last iteration */
5492 if (GET_CODE (bl
->initial_value
) == CONST_INT
5493 && INTVAL (bl
->initial_value
) > 0
5494 && (INTVAL (bl
->initial_value
) %
5495 (-INTVAL (bl
->biv
->add_val
))) == 0)
5497 /* register always nonnegative, add REG_NOTE to branch */
5498 REG_NOTES (PREV_INSN (loop_end
))
5499 = gen_rtx (EXPR_LIST
, REG_NONNEG
, NULL_RTX
,
5500 REG_NOTES (PREV_INSN (loop_end
)));
5506 /* If the decrement is 1 and the value was tested as >= 0 before
5507 the loop, then we can safely optimize. */
5508 for (p
= loop_start
; p
; p
= PREV_INSN (p
))
5510 if (GET_CODE (p
) == CODE_LABEL
)
5512 if (GET_CODE (p
) != JUMP_INSN
)
5515 before_comparison
= get_condition_for_loop (p
);
5516 if (before_comparison
5517 && XEXP (before_comparison
, 0) == bl
->biv
->dest_reg
5518 && GET_CODE (before_comparison
) == LT
5519 && XEXP (before_comparison
, 1) == const0_rtx
5520 && ! reg_set_between_p (bl
->biv
->dest_reg
, p
, loop_start
)
5521 && INTVAL (bl
->biv
->add_val
) == -1)
5523 REG_NOTES (PREV_INSN (loop_end
))
5524 = gen_rtx (EXPR_LIST
, REG_NONNEG
, NULL_RTX
,
5525 REG_NOTES (PREV_INSN (loop_end
)));
5532 else if (num_mem_sets
<= 1)
5534 /* Try to change inc to dec, so can apply above optimization. */
5536 all registers modified are induction variables or invariant,
5537 all memory references have non-overlapping addresses
5538 (obviously true if only one write)
5539 allow 2 insns for the compare/jump at the end of the loop. */
5540 int num_nonfixed_reads
= 0;
5541 /* 1 if the iteration var is used only to count iterations. */
5542 int no_use_except_counting
= 0;
5544 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
5545 if (GET_RTX_CLASS (GET_CODE (p
)) == 'i')
5546 num_nonfixed_reads
+= count_nonfixed_reads (PATTERN (p
));
5548 if (bl
->giv_count
== 0
5549 && ! loop_number_exit_labels
[uid_loop_num
[INSN_UID (loop_start
)]])
5551 rtx bivreg
= regno_reg_rtx
[bl
->regno
];
5553 /* If there are no givs for this biv, and the only exit is the
5554 fall through at the end of the the loop, then
5555 see if perhaps there are no uses except to count. */
5556 no_use_except_counting
= 1;
5557 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
5558 if (GET_RTX_CLASS (GET_CODE (p
)) == 'i')
5560 rtx set
= single_set (p
);
5562 if (set
&& GET_CODE (SET_DEST (set
)) == REG
5563 && REGNO (SET_DEST (set
)) == bl
->regno
)
5564 /* An insn that sets the biv is okay. */
5566 else if (p
== prev_nonnote_insn (prev_nonnote_insn (loop_end
))
5567 || p
== prev_nonnote_insn (loop_end
))
5568 /* Don't bother about the end test. */
5570 else if (reg_mentioned_p (bivreg
, PATTERN (p
)))
5571 /* Any other use of the biv is no good. */
5573 no_use_except_counting
= 0;
5579 /* This code only acts for innermost loops. Also it simplifies
5580 the memory address check by only reversing loops with
5581 zero or one memory access.
5582 Two memory accesses could involve parts of the same array,
5583 and that can't be reversed. */
5585 if (num_nonfixed_reads
<= 1
5587 && !loop_has_volatile
5588 && (no_use_except_counting
5589 || (bl
->giv_count
+ bl
->biv_count
+ num_mem_sets
5590 + num_movables
+ 2 == insn_count
)))
5592 rtx condition
= get_condition_for_loop (PREV_INSN (loop_end
));
5596 /* Loop can be reversed. */
5597 if (loop_dump_stream
)
5598 fprintf (loop_dump_stream
, "Can reverse loop\n");
5600 /* Now check other conditions:
5601 initial_value must be zero,
5602 final_value % add_val == 0, so that when reversed, the
5603 biv will be zero on the last iteration.
5605 This test can probably be improved since +/- 1 in the constant
5606 can be obtained by changing LT to LE and vice versa; this is
5609 if (comparison
&& bl
->initial_value
== const0_rtx
5610 && GET_CODE (XEXP (comparison
, 1)) == CONST_INT
5611 /* LE gets turned into LT */
5612 && GET_CODE (comparison
) == LT
5613 && (INTVAL (XEXP (comparison
, 1))
5614 % INTVAL (bl
->biv
->add_val
)) == 0)
5616 /* Register will always be nonnegative, with value
5617 0 on last iteration if loop reversed */
5619 /* Save some info needed to produce the new insns. */
5620 reg
= bl
->biv
->dest_reg
;
5621 jump_label
= XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end
))), 1);
5622 new_add_val
= GEN_INT (- INTVAL (bl
->biv
->add_val
));
5624 final_value
= XEXP (comparison
, 1);
5625 start_value
= GEN_INT (INTVAL (XEXP (comparison
, 1))
5626 - INTVAL (bl
->biv
->add_val
));
5628 /* Initialize biv to start_value before loop start.
5629 The old initializing insn will be deleted as a
5630 dead store by flow.c. */
5631 emit_insn_before (gen_move_insn (reg
, start_value
), loop_start
);
5633 /* Add insn to decrement register, and delete insn
5634 that incremented the register. */
5635 p
= emit_insn_before (gen_add2_insn (reg
, new_add_val
),
5637 delete_insn (bl
->biv
->insn
);
5639 /* Update biv info to reflect its new status. */
5641 bl
->initial_value
= start_value
;
5642 bl
->biv
->add_val
= new_add_val
;
5644 /* Inc LABEL_NUSES so that delete_insn will
5645 not delete the label. */
5646 LABEL_NUSES (XEXP (jump_label
, 0)) ++;
5648 /* Emit an insn after the end of the loop to set the biv's
5649 proper exit value if it is used anywhere outside the loop. */
5650 if ((regno_last_uid
[bl
->regno
]
5651 != INSN_UID (PREV_INSN (PREV_INSN (loop_end
))))
5653 || regno_first_uid
[bl
->regno
] != INSN_UID (bl
->init_insn
))
5654 emit_insn_after (gen_move_insn (reg
, final_value
),
5657 /* Delete compare/branch at end of loop. */
5658 delete_insn (PREV_INSN (loop_end
));
5659 delete_insn (PREV_INSN (loop_end
));
5661 /* Add new compare/branch insn at end of loop. */
5663 emit_cmp_insn (reg
, const0_rtx
, GE
, NULL_RTX
,
5664 GET_MODE (reg
), 0, 0);
5665 emit_jump_insn (gen_bge (XEXP (jump_label
, 0)));
5666 tem
= gen_sequence ();
5668 emit_jump_insn_before (tem
, loop_end
);
5670 for (tem
= PREV_INSN (loop_end
);
5671 tem
&& GET_CODE (tem
) != JUMP_INSN
; tem
= PREV_INSN (tem
))
5675 JUMP_LABEL (tem
) = XEXP (jump_label
, 0);
5677 /* Increment of LABEL_NUSES done above. */
5678 /* Register is now always nonnegative,
5679 so add REG_NONNEG note to the branch. */
5680 REG_NOTES (tem
) = gen_rtx (EXPR_LIST
, REG_NONNEG
, NULL_RTX
,
5686 /* Mark that this biv has been reversed. Each giv which depends
5687 on this biv, and which is also live past the end of the loop
5688 will have to be fixed up. */
5692 if (loop_dump_stream
)
5693 fprintf (loop_dump_stream
,
5694 "Reversed loop and added reg_nonneg\n");
5704 /* Verify whether the biv BL appears to be eliminable,
5705 based on the insns in the loop that refer to it.
5706 LOOP_START is the first insn of the loop, and END is the end insn.
5708 If ELIMINATE_P is non-zero, actually do the elimination.
5710 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
5711 determine whether invariant insns should be placed inside or at the
5712 start of the loop. */
5715 maybe_eliminate_biv (bl
, loop_start
, end
, eliminate_p
, threshold
, insn_count
)
5716 struct iv_class
*bl
;
5720 int threshold
, insn_count
;
5722 rtx reg
= bl
->biv
->dest_reg
;
5724 struct induction
*v
;
5726 /* Scan all insns in the loop, stopping if we find one that uses the
5727 biv in a way that we cannot eliminate. */
5729 for (p
= loop_start
; p
!= end
; p
= NEXT_INSN (p
))
5731 enum rtx_code code
= GET_CODE (p
);
5732 rtx where
= threshold
>= insn_count
? loop_start
: p
;
5734 if ((code
== INSN
|| code
== JUMP_INSN
|| code
== CALL_INSN
)
5735 && reg_mentioned_p (reg
, PATTERN (p
))
5736 && ! maybe_eliminate_biv_1 (PATTERN (p
), p
, bl
, eliminate_p
, where
))
5738 if (loop_dump_stream
)
5739 fprintf (loop_dump_stream
,
5740 "Cannot eliminate biv %d: biv used in insn %d.\n",
5741 bl
->regno
, INSN_UID (p
));
5748 if (loop_dump_stream
)
5749 fprintf (loop_dump_stream
, "biv %d %s eliminated.\n",
5750 bl
->regno
, eliminate_p
? "was" : "can be");
5757 /* If BL appears in X (part of the pattern of INSN), see if we can
5758 eliminate its use. If so, return 1. If not, return 0.
5760 If BIV does not appear in X, return 1.
5762 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
5763 where extra insns should be added. Depending on how many items have been
5764 moved out of the loop, it will either be before INSN or at the start of
5768 maybe_eliminate_biv_1 (x
, insn
, bl
, eliminate_p
, where
)
5770 struct iv_class
*bl
;
5774 enum rtx_code code
= GET_CODE (x
);
5775 rtx reg
= bl
->biv
->dest_reg
;
5776 enum machine_mode mode
= GET_MODE (reg
);
5777 struct induction
*v
;
5786 /* If we haven't already been able to do something with this BIV,
5787 we can't eliminate it. */
5793 /* If this sets the BIV, it is not a problem. */
5794 if (SET_DEST (x
) == reg
)
5797 /* If this is an insn that defines a giv, it is also ok because
5798 it will go away when the giv is reduced. */
5799 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5800 if (v
->giv_type
== DEST_REG
&& SET_DEST (x
) == v
->dest_reg
)
5804 if (SET_DEST (x
) == cc0_rtx
&& SET_SRC (x
) == reg
)
5806 /* Can replace with any giv that was reduced and
5807 that has (MULT_VAL != 0) and (ADD_VAL == 0).
5808 Require a constant for MULT_VAL, so we know it's nonzero. */
5810 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5811 if (CONSTANT_P (v
->mult_val
) && v
->mult_val
!= const0_rtx
5812 && v
->add_val
== const0_rtx
5813 && ! v
->ignore
&& ! v
->maybe_dead
5819 /* If the giv has the opposite direction of change,
5820 then reverse the comparison. */
5821 if (INTVAL (v
->mult_val
) < 0)
5822 new = gen_rtx (COMPARE
, GET_MODE (v
->new_reg
),
5823 const0_rtx
, v
->new_reg
);
5827 /* We can probably test that giv's reduced reg. */
5828 if (validate_change (insn
, &SET_SRC (x
), new, 0))
5832 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
5833 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
5834 Require a constant for MULT_VAL, so we know it's nonzero. */
5836 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5837 if (CONSTANT_P (v
->mult_val
) && v
->mult_val
!= const0_rtx
5838 && ! v
->ignore
&& ! v
->maybe_dead
5844 /* If the giv has the opposite direction of change,
5845 then reverse the comparison. */
5846 if (INTVAL (v
->mult_val
) < 0)
5847 new = gen_rtx (COMPARE
, VOIDmode
, copy_rtx (v
->add_val
),
5850 new = gen_rtx (COMPARE
, VOIDmode
, v
->new_reg
,
5851 copy_rtx (v
->add_val
));
5853 /* Replace biv with the giv's reduced register. */
5854 update_reg_last_use (v
->add_val
, insn
);
5855 if (validate_change (insn
, &SET_SRC (PATTERN (insn
)), new, 0))
5858 /* Insn doesn't support that constant or invariant. Copy it
5859 into a register (it will be a loop invariant.) */
5860 tem
= gen_reg_rtx (GET_MODE (v
->new_reg
));
5862 emit_insn_before (gen_move_insn (tem
, copy_rtx (v
->add_val
)),
5865 if (validate_change (insn
, &SET_SRC (PATTERN (insn
)),
5866 gen_rtx (COMPARE
, VOIDmode
,
5867 v
->new_reg
, tem
), 0))
5876 case GT
: case GE
: case GTU
: case GEU
:
5877 case LT
: case LE
: case LTU
: case LEU
:
5878 /* See if either argument is the biv. */
5879 if (XEXP (x
, 0) == reg
)
5880 arg
= XEXP (x
, 1), arg_operand
= 1;
5881 else if (XEXP (x
, 1) == reg
)
5882 arg
= XEXP (x
, 0), arg_operand
= 0;
5886 if (CONSTANT_P (arg
))
5888 /* First try to replace with any giv that has constant positive
5889 mult_val and constant add_val. We might be able to support
5890 negative mult_val, but it seems complex to do it in general. */
5892 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5893 if (CONSTANT_P (v
->mult_val
) && INTVAL (v
->mult_val
) > 0
5894 && CONSTANT_P (v
->add_val
)
5895 && ! v
->ignore
&& ! v
->maybe_dead
5901 /* Replace biv with the giv's reduced reg. */
5902 XEXP (x
, 1-arg_operand
) = v
->new_reg
;
5904 /* If all constants are actually constant integers and
5905 the derived constant can be directly placed in the COMPARE,
5907 if (GET_CODE (arg
) == CONST_INT
5908 && GET_CODE (v
->mult_val
) == CONST_INT
5909 && GET_CODE (v
->add_val
) == CONST_INT
5910 && validate_change (insn
, &XEXP (x
, arg_operand
),
5911 GEN_INT (INTVAL (arg
)
5912 * INTVAL (v
->mult_val
)
5913 + INTVAL (v
->add_val
)), 0))
5916 /* Otherwise, load it into a register. */
5917 tem
= gen_reg_rtx (mode
);
5918 emit_iv_add_mult (arg
, v
->mult_val
, v
->add_val
, tem
, where
);
5919 if (validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 0))
5922 /* If that failed, put back the change we made above. */
5923 XEXP (x
, 1-arg_operand
) = reg
;
5926 /* Look for giv with positive constant mult_val and nonconst add_val.
5927 Insert insns to calculate new compare value. */
5929 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5930 if (CONSTANT_P (v
->mult_val
) && INTVAL (v
->mult_val
) > 0
5931 && ! v
->ignore
&& ! v
->maybe_dead
5939 tem
= gen_reg_rtx (mode
);
5941 /* Replace biv with giv's reduced register. */
5942 validate_change (insn
, &XEXP (x
, 1 - arg_operand
),
5945 /* Compute value to compare against. */
5946 emit_iv_add_mult (arg
, v
->mult_val
, v
->add_val
, tem
, where
);
5947 /* Use it in this insn. */
5948 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
5949 if (apply_change_group ())
5953 else if (GET_CODE (arg
) == REG
|| GET_CODE (arg
) == MEM
)
5955 if (invariant_p (arg
) == 1)
5957 /* Look for giv with constant positive mult_val and nonconst
5958 add_val. Insert insns to compute new compare value. */
5960 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5961 if (CONSTANT_P (v
->mult_val
) && INTVAL (v
->mult_val
) > 0
5962 && ! v
->ignore
&& ! v
->maybe_dead
5970 tem
= gen_reg_rtx (mode
);
5972 /* Replace biv with giv's reduced register. */
5973 validate_change (insn
, &XEXP (x
, 1 - arg_operand
),
5976 /* Compute value to compare against. */
5977 emit_iv_add_mult (arg
, v
->mult_val
, v
->add_val
,
5979 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
5980 if (apply_change_group ())
5985 /* This code has problems. Basically, you can't know when
5986 seeing if we will eliminate BL, whether a particular giv
5987 of ARG will be reduced. If it isn't going to be reduced,
5988 we can't eliminate BL. We can try forcing it to be reduced,
5989 but that can generate poor code.
5991 The problem is that the benefit of reducing TV, below should
5992 be increased if BL can actually be eliminated, but this means
5993 we might have to do a topological sort of the order in which
5994 we try to process biv. It doesn't seem worthwhile to do
5995 this sort of thing now. */
5998 /* Otherwise the reg compared with had better be a biv. */
5999 if (GET_CODE (arg
) != REG
6000 || reg_iv_type
[REGNO (arg
)] != BASIC_INDUCT
)
6003 /* Look for a pair of givs, one for each biv,
6004 with identical coefficients. */
6005 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
6007 struct induction
*tv
;
6009 if (v
->ignore
|| v
->maybe_dead
|| v
->mode
!= mode
)
6012 for (tv
= reg_biv_class
[REGNO (arg
)]->giv
; tv
; tv
= tv
->next_iv
)
6013 if (! tv
->ignore
&& ! tv
->maybe_dead
6014 && rtx_equal_p (tv
->mult_val
, v
->mult_val
)
6015 && rtx_equal_p (tv
->add_val
, v
->add_val
)
6016 && tv
->mode
== mode
)
6021 /* Replace biv with its giv's reduced reg. */
6022 XEXP (x
, 1-arg_operand
) = v
->new_reg
;
6023 /* Replace other operand with the other giv's
6025 XEXP (x
, arg_operand
) = tv
->new_reg
;
6032 /* If we get here, the biv can't be eliminated. */
6036 /* If this address is a DEST_ADDR giv, it doesn't matter if the
6037 biv is used in it, since it will be replaced. */
6038 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
6039 if (v
->giv_type
== DEST_ADDR
&& v
->location
== &XEXP (x
, 0))
6044 /* See if any subexpression fails elimination. */
6045 fmt
= GET_RTX_FORMAT (code
);
6046 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
6051 if (! maybe_eliminate_biv_1 (XEXP (x
, i
), insn
, bl
,
6052 eliminate_p
, where
))
6057 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
6058 if (! maybe_eliminate_biv_1 (XVECEXP (x
, i
, j
), insn
, bl
,
6059 eliminate_p
, where
))
6068 /* Return nonzero if the last use of REG
6069 is in an insn following INSN in the same basic block. */
6072 last_use_this_basic_block (reg
, insn
)
6078 n
&& GET_CODE (n
) != CODE_LABEL
&& GET_CODE (n
) != JUMP_INSN
;
6081 if (regno_last_uid
[REGNO (reg
)] == INSN_UID (n
))
6087 /* Called via `note_stores' to record the initial value of a biv. Here we
6088 just record the location of the set and process it later. */
6091 record_initial (dest
, set
)
6095 struct iv_class
*bl
;
6097 if (GET_CODE (dest
) != REG
6098 || REGNO (dest
) >= max_reg_before_loop
6099 || reg_iv_type
[REGNO (dest
)] != BASIC_INDUCT
)
6102 bl
= reg_biv_class
[REGNO (dest
)];
6104 /* If this is the first set found, record it. */
6105 if (bl
->init_insn
== 0)
6107 bl
->init_insn
= note_insn
;
6112 /* If any of the registers in X are "old" and currently have a last use earlier
6113 than INSN, update them to have a last use of INSN. Their actual last use
6114 will be the previous insn but it will not have a valid uid_luid so we can't
6118 update_reg_last_use (x
, insn
)
6122 /* Check for the case where INSN does not have a valid luid. In this case,
6123 there is no need to modify the regno_last_uid, as this can only happen
6124 when code is inserted after the loop_end to set a pseudo's final value,
6125 and hence this insn will never be the last use of x. */
6126 if (GET_CODE (x
) == REG
&& REGNO (x
) < max_reg_before_loop
6127 && INSN_UID (insn
) < max_uid_for_loop
6128 && uid_luid
[regno_last_uid
[REGNO (x
)]] < uid_luid
[INSN_UID (insn
)])
6129 regno_last_uid
[REGNO (x
)] = INSN_UID (insn
);
6133 register char *fmt
= GET_RTX_FORMAT (GET_CODE (x
));
6134 for (i
= GET_RTX_LENGTH (GET_CODE (x
)) - 1; i
>= 0; i
--)
6137 update_reg_last_use (XEXP (x
, i
), insn
);
6138 else if (fmt
[i
] == 'E')
6139 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
6140 update_reg_last_use (XVECEXP (x
, i
, j
), insn
);
6145 /* Given a jump insn JUMP, return the condition that will cause it to branch
6146 to its JUMP_LABEL. If the condition cannot be understood, or is an
6147 inequality floating-point comparison which needs to be reversed, 0 will
6150 If EARLIEST is non-zero, it is a pointer to a place where the earliest
6151 insn used in locating the condition was found. If a replacement test
6152 of the condition is desired, it should be placed in front of that
6153 insn and we will be sure that the inputs are still valid.
6155 The condition will be returned in a canonical form to simplify testing by
6156 callers. Specifically:
6158 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
6159 (2) Both operands will be machine operands; (cc0) will have been replaced.
6160 (3) If an operand is a constant, it will be the second operand.
6161 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
6162 for GE, GEU, and LEU. */
6165 get_condition (jump
, earliest
)
6174 int reverse_code
= 0;
6175 int did_reverse_condition
= 0;
6177 /* If this is not a standard conditional jump, we can't parse it. */
6178 if (GET_CODE (jump
) != JUMP_INSN
6179 || ! condjump_p (jump
) || simplejump_p (jump
))
6182 code
= GET_CODE (XEXP (SET_SRC (PATTERN (jump
)), 0));
6183 op0
= XEXP (XEXP (SET_SRC (PATTERN (jump
)), 0), 0);
6184 op1
= XEXP (XEXP (SET_SRC (PATTERN (jump
)), 0), 1);
6189 /* If this branches to JUMP_LABEL when the condition is false, reverse
6191 if (XEXP (XEXP (SET_SRC (PATTERN (jump
)), 2), 0) == JUMP_LABEL (jump
))
6192 code
= reverse_condition (code
), did_reverse_condition
^= 1;
6194 /* If we are comparing a register with zero, see if the register is set
6195 in the previous insn to a COMPARE or a comparison operation. Perform
6196 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
6199 while (GET_RTX_CLASS (code
) == '<' && op1
== const0_rtx
)
6201 /* Set non-zero when we find something of interest. */
6205 /* If comparison with cc0, import actual comparison from compare
6209 if ((prev
= prev_nonnote_insn (prev
)) == 0
6210 || GET_CODE (prev
) != INSN
6211 || (set
= single_set (prev
)) == 0
6212 || SET_DEST (set
) != cc0_rtx
)
6215 op0
= SET_SRC (set
);
6216 op1
= CONST0_RTX (GET_MODE (op0
));
6222 /* If this is a COMPARE, pick up the two things being compared. */
6223 if (GET_CODE (op0
) == COMPARE
)
6225 op1
= XEXP (op0
, 1);
6226 op0
= XEXP (op0
, 0);
6229 else if (GET_CODE (op0
) != REG
)
6232 /* Go back to the previous insn. Stop if it is not an INSN. We also
6233 stop if it isn't a single set or if it has a REG_INC note because
6234 we don't want to bother dealing with it. */
6236 if ((prev
= prev_nonnote_insn (prev
)) == 0
6237 || GET_CODE (prev
) != INSN
6238 || FIND_REG_INC_NOTE (prev
, 0)
6239 || (set
= single_set (prev
)) == 0)
6242 /* If this is setting OP0, get what it sets it to if it looks
6244 if (SET_DEST (set
) == op0
)
6246 enum machine_mode inner_mode
= GET_MODE (SET_SRC (set
));
6248 if ((GET_CODE (SET_SRC (set
)) == COMPARE
6251 && GET_MODE_CLASS (inner_mode
) == MODE_INT
6252 && (GET_MODE_BITSIZE (inner_mode
)
6253 <= HOST_BITS_PER_WIDE_INT
)
6254 && (STORE_FLAG_VALUE
6255 & ((HOST_WIDE_INT
) 1
6256 << (GET_MODE_BITSIZE (inner_mode
) - 1))))
6257 #ifdef FLOAT_STORE_FLAG_VALUE
6259 && GET_MODE_CLASS (inner_mode
) == MODE_FLOAT
6260 && FLOAT_STORE_FLAG_VALUE
< 0)
6263 && GET_RTX_CLASS (GET_CODE (SET_SRC (set
))) == '<')))
6265 else if (((code
== EQ
6267 && (GET_MODE_BITSIZE (inner_mode
)
6268 <= HOST_BITS_PER_WIDE_INT
)
6269 && GET_MODE_CLASS (inner_mode
) == MODE_INT
6270 && (STORE_FLAG_VALUE
6271 & ((HOST_WIDE_INT
) 1
6272 << (GET_MODE_BITSIZE (inner_mode
) - 1))))
6273 #ifdef FLOAT_STORE_FLAG_VALUE
6275 && GET_MODE_CLASS (inner_mode
) == MODE_FLOAT
6276 && FLOAT_STORE_FLAG_VALUE
< 0)
6279 && GET_RTX_CLASS (GET_CODE (SET_SRC (set
))) == '<')
6281 /* We might have reversed a LT to get a GE here. But this wasn't
6282 actually the comparison of data, so we don't flag that we
6283 have had to reverse the condition. */
6284 did_reverse_condition
^= 1;
6290 else if (reg_set_p (op0
, prev
))
6291 /* If this sets OP0, but not directly, we have to give up. */
6296 if (GET_RTX_CLASS (GET_CODE (x
)) == '<')
6297 code
= GET_CODE (x
);
6300 code
= reverse_condition (code
);
6301 did_reverse_condition
^= 1;
6305 op0
= XEXP (x
, 0), op1
= XEXP (x
, 1);
6311 /* If constant is first, put it last. */
6312 if (CONSTANT_P (op0
))
6313 code
= swap_condition (code
), tem
= op0
, op0
= op1
, op1
= tem
;
6315 /* If OP0 is the result of a comparison, we weren't able to find what
6316 was really being compared, so fail. */
6317 if (GET_MODE_CLASS (GET_MODE (op0
)) == MODE_CC
)
6320 /* Canonicalize any ordered comparison with integers involving equality. */
6321 if (GET_CODE (op1
) == CONST_INT
)
6323 HOST_WIDE_INT const_val
= INTVAL (op1
);
6324 unsigned HOST_WIDE_INT uconst_val
= const_val
;
6330 op1
= GEN_INT (const_val
+ 1);
6335 op1
= GEN_INT (const_val
- 1);
6340 op1
= GEN_INT (uconst_val
+ 1);
6345 op1
= GEN_INT (uconst_val
- 1);
6350 /* If this was floating-point and we reversed anything other than an
6351 EQ or NE, return zero. */
6352 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
6353 && did_reverse_condition
&& code
!= NE
&& code
!= EQ
6354 && GET_MODE_CLASS (GET_MODE (op0
)) == MODE_FLOAT
)
6358 /* Never return CC0; return zero instead. */
6363 return gen_rtx (code
, VOIDmode
, op0
, op1
);
6366 /* Similar to above routine, except that we also put an invariant last
6367 unless both operands are invariants. */
6370 get_condition_for_loop (x
)
6373 rtx comparison
= get_condition (x
, NULL_PTR
);
6376 || ! invariant_p (XEXP (comparison
, 0))
6377 || invariant_p (XEXP (comparison
, 1)))
6380 return gen_rtx (swap_condition (GET_CODE (comparison
)), VOIDmode
,
6381 XEXP (comparison
, 1), XEXP (comparison
, 0));