1 /* Optimize jump instructions, for GNU compiler.
2 Copyright (C) 1987, 88, 89, 91-94, 1995 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 jump-optimization pass of the compiler.
22 It is run two or three times: once before cse, sometimes once after cse,
23 and once after reload (before final).
25 jump_optimize deletes unreachable code and labels that are not used.
26 It also deletes jumps that jump to the following insn,
27 and simplifies jumps around unconditional jumps and jumps
28 to unconditional jumps.
30 Each CODE_LABEL has a count of the times it is used
31 stored in the LABEL_NUSES internal field, and each JUMP_INSN
32 has one label that it refers to stored in the
33 JUMP_LABEL internal field. With this we can detect labels that
34 become unused because of the deletion of all the jumps that
35 formerly used them. The JUMP_LABEL info is sometimes looked
38 Optionally, cross-jumping can be done. Currently it is done
39 only the last time (when after reload and before final).
40 In fact, the code for cross-jumping now assumes that register
41 allocation has been done, since it uses `rtx_renumbered_equal_p'.
43 Jump optimization is done after cse when cse's constant-propagation
44 causes jumps to become unconditional or to be deleted.
46 Unreachable loops are not detected here, because the labels
47 have references and the insns appear reachable from the labels.
48 find_basic_blocks in flow.c finds and deletes such loops.
50 The subroutines delete_insn, redirect_jump, and invert_jump are used
51 from other passes as well. */
56 #include "hard-reg-set.h"
58 #include "insn-config.h"
59 #include "insn-flags.h"
63 /* ??? Eventually must record somehow the labels used by jumps
64 from nested functions. */
65 /* Pre-record the next or previous real insn for each label?
66 No, this pass is very fast anyway. */
67 /* Condense consecutive labels?
68 This would make life analysis faster, maybe. */
69 /* Optimize jump y; x: ... y: jumpif... x?
70 Don't know if it is worth bothering with. */
71 /* Optimize two cases of conditional jump to conditional jump?
72 This can never delete any instruction or make anything dead,
73 or even change what is live at any point.
74 So perhaps let combiner do it. */
76 /* Vector indexed by uid.
77 For each CODE_LABEL, index by its uid to get first unconditional jump
78 that jumps to the label.
79 For each JUMP_INSN, index by its uid to get the next unconditional jump
80 that jumps to the same label.
81 Element 0 is the start of a chain of all return insns.
82 (It is safe to use element 0 because insn uid 0 is not used. */
84 static rtx *jump_chain;
86 /* List of labels referred to from initializers.
87 These can never be deleted. */
90 /* Maximum index in jump_chain. */
92 static int max_jump_chain;
94 /* Set nonzero by jump_optimize if control can fall through
95 to the end of the function. */
98 /* Indicates whether death notes are significant in cross jump analysis.
99 Normally they are not significant, because of A and B jump to C,
100 and R dies in A, it must die in B. But this might not be true after
101 stack register conversion, and we must compare death notes in that
104 static int cross_jump_death_matters = 0;
106 static int duplicate_loop_exit_test PROTO((rtx));
107 static void find_cross_jump PROTO((rtx, rtx, int, rtx *, rtx *));
108 static void do_cross_jump PROTO((rtx, rtx, rtx));
109 static int jump_back_p PROTO((rtx, rtx));
110 static int tension_vector_labels PROTO((rtx, int));
111 static void mark_jump_label PROTO((rtx, rtx, int));
112 static void delete_computation PROTO((rtx));
113 static void delete_from_jump_chain PROTO((rtx));
114 static int delete_labelref_insn PROTO((rtx, rtx, int));
115 static void redirect_tablejump PROTO((rtx, rtx));
117 /* Delete no-op jumps and optimize jumps to jumps
118 and jumps around jumps.
119 Delete unused labels and unreachable code.
121 If CROSS_JUMP is 1, detect matching code
122 before a jump and its destination and unify them.
123 If CROSS_JUMP is 2, do cross-jumping, but pay attention to death notes.
125 If NOOP_MOVES is nonzero, delete no-op move insns.
127 If AFTER_REGSCAN is nonzero, then this jump pass is being run immediately
128 after regscan, and it is safe to use regno_first_uid and regno_last_uid.
130 If `optimize' is zero, don't change any code,
131 just determine whether control drops off the end of the function.
132 This case occurs when we have -W and not -O.
133 It works because `delete_insn' checks the value of `optimize'
134 and refrains from actually deleting when that is 0. */
137 jump_optimize (f, cross_jump, noop_moves, after_regscan)
143 register rtx insn, next, note;
149 cross_jump_death_matters = (cross_jump == 2);
151 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
152 notes whose labels don't occur in the insn any more. */
154 for (insn = f; insn; insn = NEXT_INSN (insn))
156 if (GET_CODE (insn) == CODE_LABEL)
157 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
158 else if (GET_CODE (insn) == JUMP_INSN)
159 JUMP_LABEL (insn) = 0;
160 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
161 for (note = REG_NOTES (insn); note; note = next)
163 next = XEXP (note, 1);
164 if (REG_NOTE_KIND (note) == REG_LABEL
165 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
166 remove_note (insn, note);
169 if (INSN_UID (insn) > max_uid)
170 max_uid = INSN_UID (insn);
175 /* Delete insns following barriers, up to next label. */
177 for (insn = f; insn;)
179 if (GET_CODE (insn) == BARRIER)
181 insn = NEXT_INSN (insn);
182 while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
184 if (GET_CODE (insn) == NOTE
185 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
186 insn = NEXT_INSN (insn);
188 insn = delete_insn (insn);
190 /* INSN is now the code_label. */
193 insn = NEXT_INSN (insn);
196 /* Leave some extra room for labels and duplicate exit test insns
198 max_jump_chain = max_uid * 14 / 10;
199 jump_chain = (rtx *) alloca (max_jump_chain * sizeof (rtx));
200 bzero ((char *) jump_chain, max_jump_chain * sizeof (rtx));
202 /* Mark the label each jump jumps to.
203 Combine consecutive labels, and count uses of labels.
205 For each label, make a chain (using `jump_chain')
206 of all the *unconditional* jumps that jump to it;
207 also make a chain of all returns. */
209 for (insn = f; insn; insn = NEXT_INSN (insn))
210 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
211 && ! INSN_DELETED_P (insn))
213 mark_jump_label (PATTERN (insn), insn, cross_jump);
214 if (GET_CODE (insn) == JUMP_INSN)
216 if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
218 jump_chain[INSN_UID (insn)]
219 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
220 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
222 if (GET_CODE (PATTERN (insn)) == RETURN)
224 jump_chain[INSN_UID (insn)] = jump_chain[0];
225 jump_chain[0] = insn;
230 /* Keep track of labels used from static data;
231 they cannot ever be deleted. */
233 for (insn = forced_labels; insn; insn = XEXP (insn, 1))
234 LABEL_NUSES (XEXP (insn, 0))++;
236 /* Delete all labels already not referenced.
237 Also find the last insn. */
240 for (insn = f; insn; )
242 if (GET_CODE (insn) == CODE_LABEL && LABEL_NUSES (insn) == 0)
243 insn = delete_insn (insn);
247 insn = NEXT_INSN (insn);
253 /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
254 If so record that this function can drop off the end. */
260 /* One label can follow the end-note: the return label. */
261 && ((GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
262 /* Ordinary insns can follow it if returning a structure. */
263 || GET_CODE (insn) == INSN
264 /* If machine uses explicit RETURN insns, no epilogue,
265 then one of them follows the note. */
266 || (GET_CODE (insn) == JUMP_INSN
267 && GET_CODE (PATTERN (insn)) == RETURN)
268 /* A barrier can follow the return insn. */
269 || GET_CODE (insn) == BARRIER
270 /* Other kinds of notes can follow also. */
271 || (GET_CODE (insn) == NOTE
272 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)))
273 insn = PREV_INSN (insn);
276 /* Report if control can fall through at the end of the function. */
277 if (insn && GET_CODE (insn) == NOTE
278 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END
279 && ! INSN_DELETED_P (insn))
282 /* Zero the "deleted" flag of all the "deleted" insns. */
283 for (insn = f; insn; insn = NEXT_INSN (insn))
284 INSN_DELETED_P (insn) = 0;
291 /* If we fall through to the epilogue, see if we can insert a RETURN insn
292 in front of it. If the machine allows it at this point (we might be
293 after reload for a leaf routine), it will improve optimization for it
295 insn = get_last_insn ();
296 while (insn && GET_CODE (insn) == NOTE)
297 insn = PREV_INSN (insn);
299 if (insn && GET_CODE (insn) != BARRIER)
301 emit_jump_insn (gen_return ());
308 for (insn = f; insn; )
310 next = NEXT_INSN (insn);
312 if (GET_CODE (insn) == INSN)
314 register rtx body = PATTERN (insn);
316 /* Combine stack_adjusts with following push_insns. */
318 if (GET_CODE (body) == SET
319 && SET_DEST (body) == stack_pointer_rtx
320 && GET_CODE (SET_SRC (body)) == PLUS
321 && XEXP (SET_SRC (body), 0) == stack_pointer_rtx
322 && GET_CODE (XEXP (SET_SRC (body), 1)) == CONST_INT
323 && INTVAL (XEXP (SET_SRC (body), 1)) > 0)
326 rtx stack_adjust_insn = insn;
327 int stack_adjust_amount = INTVAL (XEXP (SET_SRC (body), 1));
328 int total_pushed = 0;
331 /* Find all successive push insns. */
333 /* Don't convert more than three pushes;
334 that starts adding too many displaced addresses
335 and the whole thing starts becoming a losing
340 p = next_nonnote_insn (p);
341 if (p == 0 || GET_CODE (p) != INSN)
344 if (GET_CODE (pbody) != SET)
346 dest = SET_DEST (pbody);
347 /* Allow a no-op move between the adjust and the push. */
348 if (GET_CODE (dest) == REG
349 && GET_CODE (SET_SRC (pbody)) == REG
350 && REGNO (dest) == REGNO (SET_SRC (pbody)))
352 if (! (GET_CODE (dest) == MEM
353 && GET_CODE (XEXP (dest, 0)) == POST_INC
354 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
357 if (total_pushed + GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)))
358 > stack_adjust_amount)
360 total_pushed += GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
363 /* Discard the amount pushed from the stack adjust;
364 maybe eliminate it entirely. */
365 if (total_pushed >= stack_adjust_amount)
367 delete_computation (stack_adjust_insn);
368 total_pushed = stack_adjust_amount;
371 XEXP (SET_SRC (PATTERN (stack_adjust_insn)), 1)
372 = GEN_INT (stack_adjust_amount - total_pushed);
374 /* Change the appropriate push insns to ordinary stores. */
376 while (total_pushed > 0)
379 p = next_nonnote_insn (p);
380 if (GET_CODE (p) != INSN)
383 if (GET_CODE (pbody) == SET)
385 dest = SET_DEST (pbody);
386 if (! (GET_CODE (dest) == MEM
387 && GET_CODE (XEXP (dest, 0)) == POST_INC
388 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
390 total_pushed -= GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
391 /* If this push doesn't fully fit in the space
392 of the stack adjust that we deleted,
393 make another stack adjust here for what we
394 didn't use up. There should be peepholes
395 to recognize the resulting sequence of insns. */
396 if (total_pushed < 0)
398 emit_insn_before (gen_add2_insn (stack_pointer_rtx,
399 GEN_INT (- total_pushed)),
404 = plus_constant (stack_pointer_rtx, total_pushed);
409 /* Detect and delete no-op move instructions
410 resulting from not allocating a parameter in a register. */
412 if (GET_CODE (body) == SET
413 && (SET_DEST (body) == SET_SRC (body)
414 || (GET_CODE (SET_DEST (body)) == MEM
415 && GET_CODE (SET_SRC (body)) == MEM
416 && rtx_equal_p (SET_SRC (body), SET_DEST (body))))
417 && ! (GET_CODE (SET_DEST (body)) == MEM
418 && MEM_VOLATILE_P (SET_DEST (body)))
419 && ! (GET_CODE (SET_SRC (body)) == MEM
420 && MEM_VOLATILE_P (SET_SRC (body))))
421 delete_computation (insn);
423 /* Detect and ignore no-op move instructions
424 resulting from smart or fortuitous register allocation. */
426 else if (GET_CODE (body) == SET)
428 int sreg = true_regnum (SET_SRC (body));
429 int dreg = true_regnum (SET_DEST (body));
431 if (sreg == dreg && sreg >= 0)
433 else if (sreg >= 0 && dreg >= 0)
436 rtx tem = find_equiv_reg (NULL_RTX, insn, 0,
437 sreg, NULL_PTR, dreg,
438 GET_MODE (SET_SRC (body)));
440 #ifdef PRESERVE_DEATH_INFO_REGNO_P
441 /* Deleting insn could lose a death-note for SREG or DREG
442 so don't do it if final needs accurate death-notes. */
443 if (! PRESERVE_DEATH_INFO_REGNO_P (sreg)
444 && ! PRESERVE_DEATH_INFO_REGNO_P (dreg))
447 /* DREG may have been the target of a REG_DEAD note in
448 the insn which makes INSN redundant. If so, reorg
449 would still think it is dead. So search for such a
450 note and delete it if we find it. */
451 for (trial = prev_nonnote_insn (insn);
452 trial && GET_CODE (trial) != CODE_LABEL;
453 trial = prev_nonnote_insn (trial))
454 if (find_regno_note (trial, REG_DEAD, dreg))
456 remove_death (dreg, trial);
461 && GET_MODE (tem) == GET_MODE (SET_DEST (body)))
465 else if (dreg >= 0 && CONSTANT_P (SET_SRC (body))
466 && find_equiv_reg (SET_SRC (body), insn, 0, dreg,
468 GET_MODE (SET_DEST (body))))
470 /* This handles the case where we have two consecutive
471 assignments of the same constant to pseudos that didn't
472 get a hard reg. Each SET from the constant will be
473 converted into a SET of the spill register and an
474 output reload will be made following it. This produces
475 two loads of the same constant into the same spill
480 /* Look back for a death note for the first reg.
481 If there is one, it is no longer accurate. */
482 while (in_insn && GET_CODE (in_insn) != CODE_LABEL)
484 if ((GET_CODE (in_insn) == INSN
485 || GET_CODE (in_insn) == JUMP_INSN)
486 && find_regno_note (in_insn, REG_DEAD, dreg))
488 remove_death (dreg, in_insn);
491 in_insn = PREV_INSN (in_insn);
494 /* Delete the second load of the value. */
498 else if (GET_CODE (body) == PARALLEL)
500 /* If each part is a set between two identical registers or
501 a USE or CLOBBER, delete the insn. */
505 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
507 tem = XVECEXP (body, 0, i);
508 if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
511 if (GET_CODE (tem) != SET
512 || (sreg = true_regnum (SET_SRC (tem))) < 0
513 || (dreg = true_regnum (SET_DEST (tem))) < 0
521 /* Also delete insns to store bit fields if they are no-ops. */
522 /* Not worth the hair to detect this in the big-endian case. */
523 else if (! BYTES_BIG_ENDIAN
524 && GET_CODE (body) == SET
525 && GET_CODE (SET_DEST (body)) == ZERO_EXTRACT
526 && XEXP (SET_DEST (body), 2) == const0_rtx
527 && XEXP (SET_DEST (body), 0) == SET_SRC (body)
528 && ! (GET_CODE (SET_SRC (body)) == MEM
529 && MEM_VOLATILE_P (SET_SRC (body))))
535 /* If we haven't yet gotten to reload and we have just run regscan,
536 delete any insn that sets a register that isn't used elsewhere.
537 This helps some of the optimizations below by having less insns
538 being jumped around. */
540 if (! reload_completed && after_regscan)
541 for (insn = f; insn; insn = next)
543 rtx set = single_set (insn);
545 next = NEXT_INSN (insn);
547 if (set && GET_CODE (SET_DEST (set)) == REG
548 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
549 && regno_first_uid[REGNO (SET_DEST (set))] == INSN_UID (insn)
550 /* We use regno_last_note_uid so as not to delete the setting
551 of a reg that's used in notes. A subsequent optimization
552 might arrange to use that reg for real. */
553 && regno_last_note_uid[REGNO (SET_DEST (set))] == INSN_UID (insn)
554 && ! side_effects_p (SET_SRC (set))
555 && ! find_reg_note (insn, REG_RETVAL, 0))
559 /* Now iterate optimizing jumps until nothing changes over one pass. */
565 for (insn = f; insn; insn = next)
568 rtx temp, temp1, temp2, temp3, temp4, temp5, temp6;
570 int this_is_simplejump, this_is_condjump, reversep;
571 int this_is_condjump_in_parallel;
573 /* If NOT the first iteration, if this is the last jump pass
574 (just before final), do the special peephole optimizations.
575 Avoiding the first iteration gives ordinary jump opts
576 a chance to work before peephole opts. */
578 if (reload_completed && !first && !flag_no_peephole)
579 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
583 /* That could have deleted some insns after INSN, so check now
584 what the following insn is. */
586 next = NEXT_INSN (insn);
588 /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
589 jump. Try to optimize by duplicating the loop exit test if so.
590 This is only safe immediately after regscan, because it uses
591 the values of regno_first_uid and regno_last_uid. */
592 if (after_regscan && GET_CODE (insn) == NOTE
593 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
594 && (temp1 = next_nonnote_insn (insn)) != 0
595 && simplejump_p (temp1))
597 temp = PREV_INSN (insn);
598 if (duplicate_loop_exit_test (insn))
601 next = NEXT_INSN (temp);
606 if (GET_CODE (insn) != JUMP_INSN)
609 this_is_simplejump = simplejump_p (insn);
610 this_is_condjump = condjump_p (insn);
611 this_is_condjump_in_parallel = condjump_in_parallel_p (insn);
613 /* Tension the labels in dispatch tables. */
615 if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
616 changed |= tension_vector_labels (PATTERN (insn), 0);
617 if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
618 changed |= tension_vector_labels (PATTERN (insn), 1);
620 /* If a dispatch table always goes to the same place,
621 get rid of it and replace the insn that uses it. */
623 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
624 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
627 rtx pat = PATTERN (insn);
628 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
629 int len = XVECLEN (pat, diff_vec_p);
630 rtx dispatch = prev_real_insn (insn);
632 for (i = 0; i < len; i++)
633 if (XEXP (XVECEXP (pat, diff_vec_p, i), 0)
634 != XEXP (XVECEXP (pat, diff_vec_p, 0), 0))
638 && GET_CODE (dispatch) == JUMP_INSN
639 && JUMP_LABEL (dispatch) != 0
640 /* Don't mess with a casesi insn. */
641 && !(GET_CODE (PATTERN (dispatch)) == SET
642 && (GET_CODE (SET_SRC (PATTERN (dispatch)))
644 && next_real_insn (JUMP_LABEL (dispatch)) == insn)
646 redirect_tablejump (dispatch,
647 XEXP (XVECEXP (pat, diff_vec_p, 0), 0));
652 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
654 /* If a jump references the end of the function, try to turn
655 it into a RETURN insn, possibly a conditional one. */
656 if (JUMP_LABEL (insn)
657 && (next_active_insn (JUMP_LABEL (insn)) == 0
658 || GET_CODE (PATTERN (next_active_insn (JUMP_LABEL (insn))))
660 changed |= redirect_jump (insn, NULL_RTX);
662 /* Detect jump to following insn. */
663 if (reallabelprev == insn && condjump_p (insn))
665 next = next_real_insn (JUMP_LABEL (insn));
671 /* If we have an unconditional jump preceded by a USE, try to put
672 the USE before the target and jump there. This simplifies many
673 of the optimizations below since we don't have to worry about
674 dealing with these USE insns. We only do this if the label
675 being branch to already has the identical USE or if code
676 never falls through to that label. */
678 if (this_is_simplejump
679 && (temp = prev_nonnote_insn (insn)) != 0
680 && GET_CODE (temp) == INSN && GET_CODE (PATTERN (temp)) == USE
681 && (temp1 = prev_nonnote_insn (JUMP_LABEL (insn))) != 0
682 && (GET_CODE (temp1) == BARRIER
683 || (GET_CODE (temp1) == INSN
684 && rtx_equal_p (PATTERN (temp), PATTERN (temp1)))))
686 if (GET_CODE (temp1) == BARRIER)
688 emit_insn_after (PATTERN (temp), temp1);
689 temp1 = NEXT_INSN (temp1);
693 redirect_jump (insn, get_label_before (temp1));
694 reallabelprev = prev_real_insn (temp1);
698 /* Simplify if (...) x = a; else x = b; by converting it
699 to x = b; if (...) x = a;
700 if B is sufficiently simple, the test doesn't involve X,
701 and nothing in the test modifies B or X.
703 If we have small register classes, we also can't do this if X
706 If the "x = b;" insn has any REG_NOTES, we don't do this because
707 of the possibility that we are running after CSE and there is a
708 REG_EQUAL note that is only valid if the branch has already been
709 taken. If we move the insn with the REG_EQUAL note, we may
710 fold the comparison to always be false in a later CSE pass.
711 (We could also delete the REG_NOTES when moving the insn, but it
712 seems simpler to not move it.) An exception is that we can move
713 the insn if the only note is a REG_EQUAL or REG_EQUIV whose
714 value is the same as "b".
716 INSN is the branch over the `else' part.
720 TEMP to the jump insn preceding "x = a;"
722 TEMP2 to the insn that sets "x = b;"
723 TEMP3 to the insn that sets "x = a;"
724 TEMP4 to the set of "x = b"; */
726 if (this_is_simplejump
727 && (temp3 = prev_active_insn (insn)) != 0
728 && GET_CODE (temp3) == INSN
729 && (temp4 = single_set (temp3)) != 0
730 && GET_CODE (temp1 = SET_DEST (temp4)) == REG
731 #ifdef SMALL_REGISTER_CLASSES
732 && REGNO (temp1) >= FIRST_PSEUDO_REGISTER
734 && (temp2 = next_active_insn (insn)) != 0
735 && GET_CODE (temp2) == INSN
736 && (temp4 = single_set (temp2)) != 0
737 && rtx_equal_p (SET_DEST (temp4), temp1)
738 && (GET_CODE (SET_SRC (temp4)) == REG
739 || GET_CODE (SET_SRC (temp4)) == SUBREG
740 || CONSTANT_P (SET_SRC (temp4)))
741 && (REG_NOTES (temp2) == 0
742 || ((REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUAL
743 || REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUIV)
744 && XEXP (REG_NOTES (temp2), 1) == 0
745 && rtx_equal_p (XEXP (REG_NOTES (temp2), 0),
747 && (temp = prev_active_insn (temp3)) != 0
748 && condjump_p (temp) && ! simplejump_p (temp)
749 /* TEMP must skip over the "x = a;" insn */
750 && prev_real_insn (JUMP_LABEL (temp)) == insn
751 && no_labels_between_p (insn, JUMP_LABEL (temp))
752 /* There must be no other entries to the "x = b;" insn. */
753 && no_labels_between_p (JUMP_LABEL (temp), temp2)
754 /* INSN must either branch to the insn after TEMP2 or the insn
755 after TEMP2 must branch to the same place as INSN. */
756 && (reallabelprev == temp2
757 || ((temp5 = next_active_insn (temp2)) != 0
758 && simplejump_p (temp5)
759 && JUMP_LABEL (temp5) == JUMP_LABEL (insn))))
761 /* The test expression, X, may be a complicated test with
762 multiple branches. See if we can find all the uses of
763 the label that TEMP branches to without hitting a CALL_INSN
764 or a jump to somewhere else. */
765 rtx target = JUMP_LABEL (temp);
766 int nuses = LABEL_NUSES (target);
769 /* Set P to the first jump insn that goes around "x = a;". */
770 for (p = temp; nuses && p; p = prev_nonnote_insn (p))
772 if (GET_CODE (p) == JUMP_INSN)
774 if (condjump_p (p) && ! simplejump_p (p)
775 && JUMP_LABEL (p) == target)
784 else if (GET_CODE (p) == CALL_INSN)
789 /* We cannot insert anything between a set of cc and its use
790 so if P uses cc0, we must back up to the previous insn. */
791 q = prev_nonnote_insn (p);
792 if (q && GET_RTX_CLASS (GET_CODE (q)) == 'i'
793 && sets_cc0_p (PATTERN (q)))
800 /* If we found all the uses and there was no data conflict, we
801 can move the assignment unless we can branch into the middle
804 && no_labels_between_p (p, insn)
805 && ! reg_referenced_between_p (temp1, p, NEXT_INSN (temp3))
806 && ! reg_set_between_p (temp1, p, temp3)
807 && (GET_CODE (SET_SRC (temp4)) == CONST_INT
808 || ! reg_set_between_p (SET_SRC (temp4), p, temp2)))
810 emit_insn_after_with_line_notes (PATTERN (temp2), p, temp2);
813 /* Set NEXT to an insn that we know won't go away. */
814 next = next_active_insn (insn);
816 /* Delete the jump around the set. Note that we must do
817 this before we redirect the test jumps so that it won't
818 delete the code immediately following the assignment
819 we moved (which might be a jump). */
823 /* We either have two consecutive labels or a jump to
824 a jump, so adjust all the JUMP_INSNs to branch to where
826 for (p = NEXT_INSN (p); p != next; p = NEXT_INSN (p))
827 if (GET_CODE (p) == JUMP_INSN)
828 redirect_jump (p, target);
836 /* If we have if (...) x = exp; and branches are expensive,
837 EXP is a single insn, does not have any side effects, cannot
838 trap, and is not too costly, convert this to
839 t = exp; if (...) x = t;
841 Don't do this when we have CC0 because it is unlikely to help
842 and we'd need to worry about where to place the new insn and
843 the potential for conflicts. We also can't do this when we have
844 notes on the insn for the same reason as above.
848 TEMP to the "x = exp;" insn.
849 TEMP1 to the single set in the "x = exp; insn.
852 if (! reload_completed
853 && this_is_condjump && ! this_is_simplejump
855 && (temp = next_nonnote_insn (insn)) != 0
856 && GET_CODE (temp) == INSN
857 && REG_NOTES (temp) == 0
858 && (reallabelprev == temp
859 || ((temp2 = next_active_insn (temp)) != 0
860 && simplejump_p (temp2)
861 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
862 && (temp1 = single_set (temp)) != 0
863 && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
864 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
865 #ifdef SMALL_REGISTER_CLASSES
866 && REGNO (temp2) >= FIRST_PSEUDO_REGISTER
868 && GET_CODE (SET_SRC (temp1)) != REG
869 && GET_CODE (SET_SRC (temp1)) != SUBREG
870 && GET_CODE (SET_SRC (temp1)) != CONST_INT
871 && ! side_effects_p (SET_SRC (temp1))
872 && ! may_trap_p (SET_SRC (temp1))
873 && rtx_cost (SET_SRC (temp1)) < 10)
875 rtx new = gen_reg_rtx (GET_MODE (temp2));
877 if (validate_change (temp, &SET_DEST (temp1), new, 0))
879 next = emit_insn_after (gen_move_insn (temp2, new), insn);
880 emit_insn_after_with_line_notes (PATTERN (temp),
881 PREV_INSN (insn), temp);
883 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
887 /* Similarly, if it takes two insns to compute EXP but they
888 have the same destination. Here TEMP3 will be the second
889 insn and TEMP4 the SET from that insn. */
891 if (! reload_completed
892 && this_is_condjump && ! this_is_simplejump
894 && (temp = next_nonnote_insn (insn)) != 0
895 && GET_CODE (temp) == INSN
896 && REG_NOTES (temp) == 0
897 && (temp3 = next_nonnote_insn (temp)) != 0
898 && GET_CODE (temp3) == INSN
899 && REG_NOTES (temp3) == 0
900 && (reallabelprev == temp3
901 || ((temp2 = next_active_insn (temp3)) != 0
902 && simplejump_p (temp2)
903 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
904 && (temp1 = single_set (temp)) != 0
905 && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
906 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
907 #ifdef SMALL_REGISTER_CLASSES
908 && REGNO (temp2) >= FIRST_PSEUDO_REGISTER
910 && ! side_effects_p (SET_SRC (temp1))
911 && ! may_trap_p (SET_SRC (temp1))
912 && rtx_cost (SET_SRC (temp1)) < 10
913 && (temp4 = single_set (temp3)) != 0
914 && rtx_equal_p (SET_DEST (temp4), temp2)
915 && ! side_effects_p (SET_SRC (temp4))
916 && ! may_trap_p (SET_SRC (temp4))
917 && rtx_cost (SET_SRC (temp4)) < 10)
919 rtx new = gen_reg_rtx (GET_MODE (temp2));
921 if (validate_change (temp, &SET_DEST (temp1), new, 0))
923 next = emit_insn_after (gen_move_insn (temp2, new), insn);
924 emit_insn_after_with_line_notes (PATTERN (temp),
925 PREV_INSN (insn), temp);
926 emit_insn_after_with_line_notes
927 (replace_rtx (PATTERN (temp3), temp2, new),
928 PREV_INSN (insn), temp3);
931 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
935 /* Finally, handle the case where two insns are used to
936 compute EXP but a temporary register is used. Here we must
937 ensure that the temporary register is not used anywhere else. */
939 if (! reload_completed
941 && this_is_condjump && ! this_is_simplejump
943 && (temp = next_nonnote_insn (insn)) != 0
944 && GET_CODE (temp) == INSN
945 && REG_NOTES (temp) == 0
946 && (temp3 = next_nonnote_insn (temp)) != 0
947 && GET_CODE (temp3) == INSN
948 && REG_NOTES (temp3) == 0
949 && (reallabelprev == temp3
950 || ((temp2 = next_active_insn (temp3)) != 0
951 && simplejump_p (temp2)
952 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
953 && (temp1 = single_set (temp)) != 0
954 && (temp5 = SET_DEST (temp1),
955 (GET_CODE (temp5) == REG
956 || (GET_CODE (temp5) == SUBREG
957 && (temp5 = SUBREG_REG (temp5),
958 GET_CODE (temp5) == REG))))
959 && REGNO (temp5) >= FIRST_PSEUDO_REGISTER
960 && regno_first_uid[REGNO (temp5)] == INSN_UID (temp)
961 && regno_last_uid[REGNO (temp5)] == INSN_UID (temp3)
962 && ! side_effects_p (SET_SRC (temp1))
963 && ! may_trap_p (SET_SRC (temp1))
964 && rtx_cost (SET_SRC (temp1)) < 10
965 && (temp4 = single_set (temp3)) != 0
966 && (temp2 = SET_DEST (temp4), GET_CODE (temp2) == REG)
967 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
968 #ifdef SMALL_REGISTER_CLASSES
969 && REGNO (temp2) >= FIRST_PSEUDO_REGISTER
971 && rtx_equal_p (SET_DEST (temp4), temp2)
972 && ! side_effects_p (SET_SRC (temp4))
973 && ! may_trap_p (SET_SRC (temp4))
974 && rtx_cost (SET_SRC (temp4)) < 10)
976 rtx new = gen_reg_rtx (GET_MODE (temp2));
978 if (validate_change (temp3, &SET_DEST (temp4), new, 0))
980 next = emit_insn_after (gen_move_insn (temp2, new), insn);
981 emit_insn_after_with_line_notes (PATTERN (temp),
982 PREV_INSN (insn), temp);
983 emit_insn_after_with_line_notes (PATTERN (temp3),
984 PREV_INSN (insn), temp3);
987 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
990 #endif /* HAVE_cc0 */
992 /* Try to use a conditional move (if the target has them), or a
993 store-flag insn. The general case is:
995 1) x = a; if (...) x = b; and
998 If the jump would be faster, the machine should not have defined
999 the movcc or scc insns!. These cases are often made by the
1000 previous optimization.
1002 The second case is treated as x = x; if (...) x = b;.
1004 INSN here is the jump around the store. We set:
1006 TEMP to the "x = b;" insn.
1009 TEMP3 to A (X in the second case).
1010 TEMP4 to the condition being tested.
1011 TEMP5 to the earliest insn used to find the condition. */
1013 if (/* We can't do this after reload has completed. */
1015 && this_is_condjump && ! this_is_simplejump
1016 /* Set TEMP to the "x = b;" insn. */
1017 && (temp = next_nonnote_insn (insn)) != 0
1018 && GET_CODE (temp) == INSN
1019 && GET_CODE (PATTERN (temp)) == SET
1020 && GET_CODE (temp1 = SET_DEST (PATTERN (temp))) == REG
1021 #ifdef SMALL_REGISTER_CLASSES
1022 && REGNO (temp1) >= FIRST_PSEUDO_REGISTER
1024 && (GET_CODE (temp2 = SET_SRC (PATTERN (temp))) == REG
1025 || GET_CODE (temp2) == SUBREG
1026 /* ??? How about floating point constants? */
1027 || GET_CODE (temp2) == CONST_INT)
1028 /* Allow either form, but prefer the former if both apply.
1029 There is no point in using the old value of TEMP1 if
1030 it is a register, since cse will alias them. It can
1031 lose if the old value were a hard register since CSE
1032 won't replace hard registers. */
1033 && (((temp3 = reg_set_last (temp1, insn)) != 0)
1034 /* Make the latter case look like x = x; if (...) x = b; */
1035 || (temp3 = temp1, 1))
1036 /* INSN must either branch to the insn after TEMP or the insn
1037 after TEMP must branch to the same place as INSN. */
1038 && (reallabelprev == temp
1039 || ((temp4 = next_active_insn (temp)) != 0
1040 && simplejump_p (temp4)
1041 && JUMP_LABEL (temp4) == JUMP_LABEL (insn)))
1042 && (temp4 = get_condition (insn, &temp5)) != 0
1043 /* We must be comparing objects whose modes imply the size.
1044 We could handle BLKmode if (1) emit_store_flag could
1045 and (2) we could find the size reliably. */
1046 && GET_MODE (XEXP (temp4, 0)) != BLKmode
1047 /* No point in doing any of this if branches are cheap or we
1048 don't have conditional moves. */
1049 && (BRANCH_COST >= 2
1050 #ifdef HAVE_conditional_move
1055 /* If the previous insn sets CC0 and something else, we can't
1056 do this since we are going to delete that insn. */
1058 && ! ((temp6 = prev_nonnote_insn (insn)) != 0
1059 && GET_CODE (temp6) == INSN
1060 && (sets_cc0_p (PATTERN (temp6)) == -1
1061 || (sets_cc0_p (PATTERN (temp6)) == 1
1062 && FIND_REG_INC_NOTE (temp6, NULL_RTX))))
1066 #ifdef HAVE_conditional_move
1067 /* First try a conditional move. */
1069 enum rtx_code code = GET_CODE (temp4);
1071 rtx cond0, cond1, aval, bval;
1074 /* Copy the compared variables into cond0 and cond1, so that
1075 any side effects performed in or after the old comparison,
1076 will not affect our compare which will come later. */
1077 /* ??? Is it possible to just use the comparison in the jump
1078 insn? After all, we're going to delete it. We'd have
1079 to modify emit_conditional_move to take a comparison rtx
1080 instead or write a new function. */
1081 cond0 = gen_reg_rtx (GET_MODE (XEXP (temp4, 0)));
1082 /* We want the target to be able to simplify comparisons with
1083 zero (and maybe other constants as well), so don't create
1084 pseudos for them. There's no need to either. */
1085 if (GET_CODE (XEXP (temp4, 1)) == CONST_INT
1086 || GET_CODE (XEXP (temp4, 1)) == CONST_DOUBLE)
1087 cond1 = XEXP (temp4, 1);
1089 cond1 = gen_reg_rtx (GET_MODE (XEXP (temp4, 1)));
1095 target = emit_conditional_move (var, code,
1096 cond0, cond1, VOIDmode,
1097 aval, bval, GET_MODE (var),
1098 (code == LTU || code == GEU
1099 || code == LEU || code == GTU));
1105 /* Save the conditional move sequence but don't emit it
1106 yet. On some machines, like the alpha, it is possible
1107 that temp5 == insn, so next generate the sequence that
1108 saves the compared values and then emit both
1109 sequences ensuring seq1 occurs before seq2. */
1110 seq2 = get_insns ();
1113 /* Now that we can't fail, generate the copy insns that
1114 preserve the compared values. */
1116 emit_move_insn (cond0, XEXP (temp4, 0));
1117 if (cond1 != XEXP (temp4, 1))
1118 emit_move_insn (cond1, XEXP (temp4, 1));
1119 seq1 = get_insns ();
1122 emit_insns_before (seq1, temp5);
1123 emit_insns_before (seq2, insn);
1125 /* ??? We can also delete the insn that sets X to A.
1126 Flow will do it too though. */
1128 next = NEXT_INSN (insn);
1138 /* That didn't work, try a store-flag insn.
1140 We further divide the cases into:
1142 1) x = a; if (...) x = b; and either A or B is zero,
1143 2) if (...) x = 0; and jumps are expensive,
1144 3) x = a; if (...) x = b; and A and B are constants where all
1145 the set bits in A are also set in B and jumps are expensive,
1146 4) x = a; if (...) x = b; and A and B non-zero, and jumps are
1148 5) if (...) x = b; if jumps are even more expensive. */
1150 if (GET_MODE_CLASS (GET_MODE (temp1)) == MODE_INT
1151 && ((GET_CODE (temp3) == CONST_INT)
1152 /* Make the latter case look like
1153 x = x; if (...) x = 0; */
1156 && temp2 == const0_rtx)
1157 || BRANCH_COST >= 3)))
1158 /* If B is zero, OK; if A is zero, can only do (1) if we
1159 can reverse the condition. See if (3) applies possibly
1160 by reversing the condition. Prefer reversing to (4) when
1161 branches are very expensive. */
1162 && ((reversep = 0, temp2 == const0_rtx)
1163 || (temp3 == const0_rtx
1164 && (reversep = can_reverse_comparison_p (temp4, insn)))
1165 || (BRANCH_COST >= 2
1166 && GET_CODE (temp2) == CONST_INT
1167 && GET_CODE (temp3) == CONST_INT
1168 && ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp2)
1169 || ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp3)
1170 && (reversep = can_reverse_comparison_p (temp4,
1172 || BRANCH_COST >= 3)
1175 enum rtx_code code = GET_CODE (temp4);
1176 rtx uval, cval, var = temp1;
1180 /* If necessary, reverse the condition. */
1182 code = reverse_condition (code), uval = temp2, cval = temp3;
1184 uval = temp3, cval = temp2;
1186 /* If CVAL is non-zero, normalize to -1. Otherwise, if UVAL
1187 is the constant 1, it is best to just compute the result
1188 directly. If UVAL is constant and STORE_FLAG_VALUE
1189 includes all of its bits, it is best to compute the flag
1190 value unnormalized and `and' it with UVAL. Otherwise,
1191 normalize to -1 and `and' with UVAL. */
1192 normalizep = (cval != const0_rtx ? -1
1193 : (uval == const1_rtx ? 1
1194 : (GET_CODE (uval) == CONST_INT
1195 && (INTVAL (uval) & ~STORE_FLAG_VALUE) == 0)
1198 /* We will be putting the store-flag insn immediately in
1199 front of the comparison that was originally being done,
1200 so we know all the variables in TEMP4 will be valid.
1201 However, this might be in front of the assignment of
1202 A to VAR. If it is, it would clobber the store-flag
1203 we will be emitting.
1205 Therefore, emit into a temporary which will be copied to
1206 VAR immediately after TEMP. */
1209 target = emit_store_flag (gen_reg_rtx (GET_MODE (var)), code,
1210 XEXP (temp4, 0), XEXP (temp4, 1),
1212 (code == LTU || code == LEU
1213 || code == GEU || code == GTU),
1223 /* Put the store-flag insns in front of the first insn
1224 used to compute the condition to ensure that we
1225 use the same values of them as the current
1226 comparison. However, the remainder of the insns we
1227 generate will be placed directly in front of the
1228 jump insn, in case any of the pseudos we use
1229 are modified earlier. */
1231 emit_insns_before (seq, temp5);
1235 /* Both CVAL and UVAL are non-zero. */
1236 if (cval != const0_rtx && uval != const0_rtx)
1240 tem1 = expand_and (uval, target, NULL_RTX);
1241 if (GET_CODE (cval) == CONST_INT
1242 && GET_CODE (uval) == CONST_INT
1243 && (INTVAL (cval) & INTVAL (uval)) == INTVAL (cval))
1247 tem2 = expand_unop (GET_MODE (var), one_cmpl_optab,
1248 target, NULL_RTX, 0);
1249 tem2 = expand_and (cval, tem2,
1250 (GET_CODE (tem2) == REG
1254 /* If we usually make new pseudos, do so here. This
1255 turns out to help machines that have conditional
1257 /* ??? Conditional moves have already been handled.
1258 This may be obsolete. */
1260 if (flag_expensive_optimizations)
1263 target = expand_binop (GET_MODE (var), ior_optab,
1267 else if (normalizep != 1)
1269 /* We know that either CVAL or UVAL is zero. If
1270 UVAL is zero, negate TARGET and `and' with CVAL.
1271 Otherwise, `and' with UVAL. */
1272 if (uval == const0_rtx)
1274 target = expand_unop (GET_MODE (var), one_cmpl_optab,
1275 target, NULL_RTX, 0);
1279 target = expand_and (uval, target,
1280 (GET_CODE (target) == REG
1281 && ! preserve_subexpressions_p ()
1282 ? target : NULL_RTX));
1285 emit_move_insn (var, target);
1289 /* If INSN uses CC0, we must not separate it from the
1290 insn that sets cc0. */
1291 if (reg_mentioned_p (cc0_rtx, PATTERN (before)))
1292 before = prev_nonnote_insn (before);
1294 emit_insns_before (seq, before);
1297 next = NEXT_INSN (insn);
1307 /* If branches are expensive, convert
1308 if (foo) bar++; to bar += (foo != 0);
1309 and similarly for "bar--;"
1311 INSN is the conditional branch around the arithmetic. We set:
1313 TEMP is the arithmetic insn.
1314 TEMP1 is the SET doing the arithmetic.
1315 TEMP2 is the operand being incremented or decremented.
1316 TEMP3 to the condition being tested.
1317 TEMP4 to the earliest insn used to find the condition. */
1319 if ((BRANCH_COST >= 2
1327 && ! reload_completed
1328 && this_is_condjump && ! this_is_simplejump
1329 && (temp = next_nonnote_insn (insn)) != 0
1330 && (temp1 = single_set (temp)) != 0
1331 && (temp2 = SET_DEST (temp1),
1332 GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT)
1333 && GET_CODE (SET_SRC (temp1)) == PLUS
1334 && (XEXP (SET_SRC (temp1), 1) == const1_rtx
1335 || XEXP (SET_SRC (temp1), 1) == constm1_rtx)
1336 && rtx_equal_p (temp2, XEXP (SET_SRC (temp1), 0))
1337 && ! side_effects_p (temp2)
1338 && ! may_trap_p (temp2)
1339 /* INSN must either branch to the insn after TEMP or the insn
1340 after TEMP must branch to the same place as INSN. */
1341 && (reallabelprev == temp
1342 || ((temp3 = next_active_insn (temp)) != 0
1343 && simplejump_p (temp3)
1344 && JUMP_LABEL (temp3) == JUMP_LABEL (insn)))
1345 && (temp3 = get_condition (insn, &temp4)) != 0
1346 /* We must be comparing objects whose modes imply the size.
1347 We could handle BLKmode if (1) emit_store_flag could
1348 and (2) we could find the size reliably. */
1349 && GET_MODE (XEXP (temp3, 0)) != BLKmode
1350 && can_reverse_comparison_p (temp3, insn))
1352 rtx temp6, target = 0, seq, init_insn = 0, init = temp2;
1353 enum rtx_code code = reverse_condition (GET_CODE (temp3));
1357 /* It must be the case that TEMP2 is not modified in the range
1358 [TEMP4, INSN). The one exception we make is if the insn
1359 before INSN sets TEMP2 to something which is also unchanged
1360 in that range. In that case, we can move the initialization
1361 into our sequence. */
1363 if ((temp5 = prev_active_insn (insn)) != 0
1364 && GET_CODE (temp5) == INSN
1365 && (temp6 = single_set (temp5)) != 0
1366 && rtx_equal_p (temp2, SET_DEST (temp6))
1367 && (CONSTANT_P (SET_SRC (temp6))
1368 || GET_CODE (SET_SRC (temp6)) == REG
1369 || GET_CODE (SET_SRC (temp6)) == SUBREG))
1371 emit_insn (PATTERN (temp5));
1373 init = SET_SRC (temp6);
1376 if (CONSTANT_P (init)
1377 || ! reg_set_between_p (init, PREV_INSN (temp4), insn))
1378 target = emit_store_flag (gen_reg_rtx (GET_MODE (temp2)), code,
1379 XEXP (temp3, 0), XEXP (temp3, 1),
1381 (code == LTU || code == LEU
1382 || code == GTU || code == GEU), 1);
1384 /* If we can do the store-flag, do the addition or
1388 target = expand_binop (GET_MODE (temp2),
1389 (XEXP (SET_SRC (temp1), 1) == const1_rtx
1390 ? add_optab : sub_optab),
1391 temp2, target, temp2, 0, OPTAB_WIDEN);
1395 /* Put the result back in temp2 in case it isn't already.
1396 Then replace the jump, possible a CC0-setting insn in
1397 front of the jump, and TEMP, with the sequence we have
1400 if (target != temp2)
1401 emit_move_insn (temp2, target);
1406 emit_insns_before (seq, temp4);
1410 delete_insn (init_insn);
1412 next = NEXT_INSN (insn);
1414 delete_insn (prev_nonnote_insn (insn));
1424 /* Simplify if (...) x = 1; else {...} if (x) ...
1425 We recognize this case scanning backwards as well.
1427 TEMP is the assignment to x;
1428 TEMP1 is the label at the head of the second if. */
1429 /* ?? This should call get_condition to find the values being
1430 compared, instead of looking for a COMPARE insn when HAVE_cc0
1431 is not defined. This would allow it to work on the m88k. */
1432 /* ?? This optimization is only safe before cse is run if HAVE_cc0
1433 is not defined and the condition is tested by a separate compare
1434 insn. This is because the code below assumes that the result
1435 of the compare dies in the following branch.
1437 Not only that, but there might be other insns between the
1438 compare and branch whose results are live. Those insns need
1441 A way to fix this is to move the insns at JUMP_LABEL (insn)
1442 to before INSN. If we are running before flow, they will
1443 be deleted if they aren't needed. But this doesn't work
1446 This is really a special-case of jump threading, anyway. The
1447 right thing to do is to replace this and jump threading with
1448 much simpler code in cse.
1450 This code has been turned off in the non-cc0 case in the
1454 else if (this_is_simplejump
1455 /* Safe to skip USE and CLOBBER insns here
1456 since they will not be deleted. */
1457 && (temp = prev_active_insn (insn))
1458 && no_labels_between_p (temp, insn)
1459 && GET_CODE (temp) == INSN
1460 && GET_CODE (PATTERN (temp)) == SET
1461 && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1462 && CONSTANT_P (SET_SRC (PATTERN (temp)))
1463 && (temp1 = next_active_insn (JUMP_LABEL (insn)))
1464 /* If we find that the next value tested is `x'
1465 (TEMP1 is the insn where this happens), win. */
1466 && GET_CODE (temp1) == INSN
1467 && GET_CODE (PATTERN (temp1)) == SET
1469 /* Does temp1 `tst' the value of x? */
1470 && SET_SRC (PATTERN (temp1)) == SET_DEST (PATTERN (temp))
1471 && SET_DEST (PATTERN (temp1)) == cc0_rtx
1472 && (temp1 = next_nonnote_insn (temp1))
1474 /* Does temp1 compare the value of x against zero? */
1475 && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1476 && XEXP (SET_SRC (PATTERN (temp1)), 1) == const0_rtx
1477 && (XEXP (SET_SRC (PATTERN (temp1)), 0)
1478 == SET_DEST (PATTERN (temp)))
1479 && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1480 && (temp1 = find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1482 && condjump_p (temp1))
1484 /* Get the if_then_else from the condjump. */
1485 rtx choice = SET_SRC (PATTERN (temp1));
1486 if (GET_CODE (choice) == IF_THEN_ELSE)
1488 enum rtx_code code = GET_CODE (XEXP (choice, 0));
1489 rtx val = SET_SRC (PATTERN (temp));
1491 = simplify_relational_operation (code, GET_MODE (SET_DEST (PATTERN (temp))),
1495 if (cond == const_true_rtx)
1496 ultimate = XEXP (choice, 1);
1497 else if (cond == const0_rtx)
1498 ultimate = XEXP (choice, 2);
1502 if (ultimate == pc_rtx)
1503 ultimate = get_label_after (temp1);
1504 else if (ultimate && GET_CODE (ultimate) != RETURN)
1505 ultimate = XEXP (ultimate, 0);
1508 changed |= redirect_jump (insn, ultimate);
1514 /* @@ This needs a bit of work before it will be right.
1516 Any type of comparison can be accepted for the first and
1517 second compare. When rewriting the first jump, we must
1518 compute the what conditions can reach label3, and use the
1519 appropriate code. We can not simply reverse/swap the code
1520 of the first jump. In some cases, the second jump must be
1524 < == converts to > ==
1525 < != converts to == >
1528 If the code is written to only accept an '==' test for the second
1529 compare, then all that needs to be done is to swap the condition
1530 of the first branch.
1532 It is questionable whether we want this optimization anyways,
1533 since if the user wrote code like this because he/she knew that
1534 the jump to label1 is taken most of the time, then rewriting
1535 this gives slower code. */
1536 /* @@ This should call get_condition to find the values being
1537 compared, instead of looking for a COMPARE insn when HAVE_cc0
1538 is not defined. This would allow it to work on the m88k. */
1539 /* @@ This optimization is only safe before cse is run if HAVE_cc0
1540 is not defined and the condition is tested by a separate compare
1541 insn. This is because the code below assumes that the result
1542 of the compare dies in the following branch. */
1544 /* Simplify test a ~= b
1558 where ~= is an inequality, e.g. >, and ~~= is the swapped
1561 We recognize this case scanning backwards.
1563 TEMP is the conditional jump to `label2';
1564 TEMP1 is the test for `a == b';
1565 TEMP2 is the conditional jump to `label1';
1566 TEMP3 is the test for `a ~= b'. */
1567 else if (this_is_simplejump
1568 && (temp = prev_active_insn (insn))
1569 && no_labels_between_p (temp, insn)
1570 && condjump_p (temp)
1571 && (temp1 = prev_active_insn (temp))
1572 && no_labels_between_p (temp1, temp)
1573 && GET_CODE (temp1) == INSN
1574 && GET_CODE (PATTERN (temp1)) == SET
1576 && sets_cc0_p (PATTERN (temp1)) == 1
1578 && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1579 && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1580 && (temp == find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1582 && (temp2 = prev_active_insn (temp1))
1583 && no_labels_between_p (temp2, temp1)
1584 && condjump_p (temp2)
1585 && JUMP_LABEL (temp2) == next_nonnote_insn (NEXT_INSN (insn))
1586 && (temp3 = prev_active_insn (temp2))
1587 && no_labels_between_p (temp3, temp2)
1588 && GET_CODE (PATTERN (temp3)) == SET
1589 && rtx_equal_p (SET_DEST (PATTERN (temp3)),
1590 SET_DEST (PATTERN (temp1)))
1591 && rtx_equal_p (SET_SRC (PATTERN (temp1)),
1592 SET_SRC (PATTERN (temp3)))
1593 && ! inequality_comparisons_p (PATTERN (temp))
1594 && inequality_comparisons_p (PATTERN (temp2)))
1596 rtx fallthrough_label = JUMP_LABEL (temp2);
1598 ++LABEL_NUSES (fallthrough_label);
1599 if (swap_jump (temp2, JUMP_LABEL (insn)))
1605 if (--LABEL_NUSES (fallthrough_label) == 0)
1606 delete_insn (fallthrough_label);
1609 /* Simplify if (...) {... x = 1;} if (x) ...
1611 We recognize this case backwards.
1613 TEMP is the test of `x';
1614 TEMP1 is the assignment to `x' at the end of the
1615 previous statement. */
1616 /* @@ This should call get_condition to find the values being
1617 compared, instead of looking for a COMPARE insn when HAVE_cc0
1618 is not defined. This would allow it to work on the m88k. */
1619 /* @@ This optimization is only safe before cse is run if HAVE_cc0
1620 is not defined and the condition is tested by a separate compare
1621 insn. This is because the code below assumes that the result
1622 of the compare dies in the following branch. */
1624 /* ??? This has to be turned off. The problem is that the
1625 unconditional jump might indirectly end up branching to the
1626 label between TEMP1 and TEMP. We can't detect this, in general,
1627 since it may become a jump to there after further optimizations.
1628 If that jump is done, it will be deleted, so we will retry
1629 this optimization in the next pass, thus an infinite loop.
1631 The present code prevents this by putting the jump after the
1632 label, but this is not logically correct. */
1634 else if (this_is_condjump
1635 /* Safe to skip USE and CLOBBER insns here
1636 since they will not be deleted. */
1637 && (temp = prev_active_insn (insn))
1638 && no_labels_between_p (temp, insn)
1639 && GET_CODE (temp) == INSN
1640 && GET_CODE (PATTERN (temp)) == SET
1642 && sets_cc0_p (PATTERN (temp)) == 1
1643 && GET_CODE (SET_SRC (PATTERN (temp))) == REG
1645 /* Temp must be a compare insn, we can not accept a register
1646 to register move here, since it may not be simply a
1648 && GET_CODE (SET_SRC (PATTERN (temp))) == COMPARE
1649 && XEXP (SET_SRC (PATTERN (temp)), 1) == const0_rtx
1650 && GET_CODE (XEXP (SET_SRC (PATTERN (temp)), 0)) == REG
1651 && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1652 && insn == find_next_ref (SET_DEST (PATTERN (temp)), temp)
1654 /* May skip USE or CLOBBER insns here
1655 for checking for opportunity, since we
1656 take care of them later. */
1657 && (temp1 = prev_active_insn (temp))
1658 && GET_CODE (temp1) == INSN
1659 && GET_CODE (PATTERN (temp1)) == SET
1661 && SET_SRC (PATTERN (temp)) == SET_DEST (PATTERN (temp1))
1663 && (XEXP (SET_SRC (PATTERN (temp)), 0)
1664 == SET_DEST (PATTERN (temp1)))
1666 && CONSTANT_P (SET_SRC (PATTERN (temp1)))
1667 /* If this isn't true, cse will do the job. */
1668 && ! no_labels_between_p (temp1, temp))
1670 /* Get the if_then_else from the condjump. */
1671 rtx choice = SET_SRC (PATTERN (insn));
1672 if (GET_CODE (choice) == IF_THEN_ELSE
1673 && (GET_CODE (XEXP (choice, 0)) == EQ
1674 || GET_CODE (XEXP (choice, 0)) == NE))
1676 int want_nonzero = (GET_CODE (XEXP (choice, 0)) == NE);
1681 /* Get the place that condjump will jump to
1682 if it is reached from here. */
1683 if ((SET_SRC (PATTERN (temp1)) != const0_rtx)
1685 ultimate = XEXP (choice, 1);
1687 ultimate = XEXP (choice, 2);
1688 /* Get it as a CODE_LABEL. */
1689 if (ultimate == pc_rtx)
1690 ultimate = get_label_after (insn);
1692 /* Get the label out of the LABEL_REF. */
1693 ultimate = XEXP (ultimate, 0);
1695 /* Insert the jump immediately before TEMP, specifically
1696 after the label that is between TEMP1 and TEMP. */
1697 last_insn = PREV_INSN (temp);
1699 /* If we would be branching to the next insn, the jump
1700 would immediately be deleted and the re-inserted in
1701 a subsequent pass over the code. So don't do anything
1703 if (next_active_insn (last_insn)
1704 != next_active_insn (ultimate))
1706 emit_barrier_after (last_insn);
1707 p = emit_jump_insn_after (gen_jump (ultimate),
1709 JUMP_LABEL (p) = ultimate;
1710 ++LABEL_NUSES (ultimate);
1711 if (INSN_UID (ultimate) < max_jump_chain
1712 && INSN_CODE (p) < max_jump_chain)
1714 jump_chain[INSN_UID (p)]
1715 = jump_chain[INSN_UID (ultimate)];
1716 jump_chain[INSN_UID (ultimate)] = p;
1724 /* Detect a conditional jump going to the same place
1725 as an immediately following unconditional jump. */
1726 else if (this_is_condjump
1727 && (temp = next_active_insn (insn)) != 0
1728 && simplejump_p (temp)
1729 && (next_active_insn (JUMP_LABEL (insn))
1730 == next_active_insn (JUMP_LABEL (temp))))
1736 /* Detect a conditional jump jumping over an unconditional jump. */
1738 else if ((this_is_condjump || this_is_condjump_in_parallel)
1739 && ! this_is_simplejump
1740 && reallabelprev != 0
1741 && GET_CODE (reallabelprev) == JUMP_INSN
1742 && prev_active_insn (reallabelprev) == insn
1743 && no_labels_between_p (insn, reallabelprev)
1744 && simplejump_p (reallabelprev))
1746 /* When we invert the unconditional jump, we will be
1747 decrementing the usage count of its old label.
1748 Make sure that we don't delete it now because that
1749 might cause the following code to be deleted. */
1750 rtx prev_uses = prev_nonnote_insn (reallabelprev);
1751 rtx prev_label = JUMP_LABEL (insn);
1754 ++LABEL_NUSES (prev_label);
1756 if (invert_jump (insn, JUMP_LABEL (reallabelprev)))
1758 /* It is very likely that if there are USE insns before
1759 this jump, they hold REG_DEAD notes. These REG_DEAD
1760 notes are no longer valid due to this optimization,
1761 and will cause the life-analysis that following passes
1762 (notably delayed-branch scheduling) to think that
1763 these registers are dead when they are not.
1765 To prevent this trouble, we just remove the USE insns
1766 from the insn chain. */
1768 while (prev_uses && GET_CODE (prev_uses) == INSN
1769 && GET_CODE (PATTERN (prev_uses)) == USE)
1771 rtx useless = prev_uses;
1772 prev_uses = prev_nonnote_insn (prev_uses);
1773 delete_insn (useless);
1776 delete_insn (reallabelprev);
1781 /* We can now safely delete the label if it is unreferenced
1782 since the delete_insn above has deleted the BARRIER. */
1783 if (prev_label && --LABEL_NUSES (prev_label) == 0)
1784 delete_insn (prev_label);
1789 /* Detect a jump to a jump. */
1791 nlabel = follow_jumps (JUMP_LABEL (insn));
1792 if (nlabel != JUMP_LABEL (insn)
1793 && redirect_jump (insn, nlabel))
1799 /* Look for if (foo) bar; else break; */
1800 /* The insns look like this:
1801 insn = condjump label1;
1802 ...range1 (some insns)...
1805 ...range2 (some insns)...
1806 jump somewhere unconditionally
1809 rtx label1 = next_label (insn);
1810 rtx range1end = label1 ? prev_active_insn (label1) : 0;
1811 /* Don't do this optimization on the first round, so that
1812 jump-around-a-jump gets simplified before we ask here
1813 whether a jump is unconditional.
1815 Also don't do it when we are called after reload since
1816 it will confuse reorg. */
1818 && (reload_completed ? ! flag_delayed_branch : 1)
1819 /* Make sure INSN is something we can invert. */
1820 && condjump_p (insn)
1822 && JUMP_LABEL (insn) == label1
1823 && LABEL_NUSES (label1) == 1
1824 && GET_CODE (range1end) == JUMP_INSN
1825 && simplejump_p (range1end))
1827 rtx label2 = next_label (label1);
1828 rtx range2end = label2 ? prev_active_insn (label2) : 0;
1829 if (range1end != range2end
1830 && JUMP_LABEL (range1end) == label2
1831 && GET_CODE (range2end) == JUMP_INSN
1832 && GET_CODE (NEXT_INSN (range2end)) == BARRIER
1833 /* Invert the jump condition, so we
1834 still execute the same insns in each case. */
1835 && invert_jump (insn, label1))
1837 rtx range1beg = next_active_insn (insn);
1838 rtx range2beg = next_active_insn (label1);
1839 rtx range1after, range2after;
1840 rtx range1before, range2before;
1843 /* Include in each range any notes before it, to be
1844 sure that we get the line number note if any, even
1845 if there are other notes here. */
1846 while (PREV_INSN (range1beg)
1847 && GET_CODE (PREV_INSN (range1beg)) == NOTE)
1848 range1beg = PREV_INSN (range1beg);
1850 while (PREV_INSN (range2beg)
1851 && GET_CODE (PREV_INSN (range2beg)) == NOTE)
1852 range2beg = PREV_INSN (range2beg);
1854 /* Don't move NOTEs for blocks or loops; shift them
1855 outside the ranges, where they'll stay put. */
1856 range1beg = squeeze_notes (range1beg, range1end);
1857 range2beg = squeeze_notes (range2beg, range2end);
1859 /* Get current surrounds of the 2 ranges. */
1860 range1before = PREV_INSN (range1beg);
1861 range2before = PREV_INSN (range2beg);
1862 range1after = NEXT_INSN (range1end);
1863 range2after = NEXT_INSN (range2end);
1865 /* Splice range2 where range1 was. */
1866 NEXT_INSN (range1before) = range2beg;
1867 PREV_INSN (range2beg) = range1before;
1868 NEXT_INSN (range2end) = range1after;
1869 PREV_INSN (range1after) = range2end;
1870 /* Splice range1 where range2 was. */
1871 NEXT_INSN (range2before) = range1beg;
1872 PREV_INSN (range1beg) = range2before;
1873 NEXT_INSN (range1end) = range2after;
1874 PREV_INSN (range2after) = range1end;
1876 /* Check for a loop end note between the end of
1877 range2, and the next code label. If there is one,
1878 then what we have really seen is
1879 if (foo) break; end_of_loop;
1880 and moved the break sequence outside the loop.
1881 We must move the LOOP_END note to where the
1882 loop really ends now, or we will confuse loop
1883 optimization. Stop if we find a LOOP_BEG note
1884 first, since we don't want to move the LOOP_END
1885 note in that case. */
1886 for (;range2after != label2; range2after = rangenext)
1888 rangenext = NEXT_INSN (range2after);
1889 if (GET_CODE (range2after) == NOTE)
1891 if (NOTE_LINE_NUMBER (range2after)
1892 == NOTE_INSN_LOOP_END)
1894 NEXT_INSN (PREV_INSN (range2after))
1896 PREV_INSN (rangenext)
1897 = PREV_INSN (range2after);
1898 PREV_INSN (range2after)
1899 = PREV_INSN (range1beg);
1900 NEXT_INSN (range2after) = range1beg;
1901 NEXT_INSN (PREV_INSN (range1beg))
1903 PREV_INSN (range1beg) = range2after;
1905 else if (NOTE_LINE_NUMBER (range2after)
1906 == NOTE_INSN_LOOP_BEG)
1916 /* Now that the jump has been tensioned,
1917 try cross jumping: check for identical code
1918 before the jump and before its target label. */
1920 /* First, cross jumping of conditional jumps: */
1922 if (cross_jump && condjump_p (insn))
1924 rtx newjpos, newlpos;
1925 rtx x = prev_real_insn (JUMP_LABEL (insn));
1927 /* A conditional jump may be crossjumped
1928 only if the place it jumps to follows
1929 an opposing jump that comes back here. */
1931 if (x != 0 && ! jump_back_p (x, insn))
1932 /* We have no opposing jump;
1933 cannot cross jump this insn. */
1937 /* TARGET is nonzero if it is ok to cross jump
1938 to code before TARGET. If so, see if matches. */
1940 find_cross_jump (insn, x, 2,
1941 &newjpos, &newlpos);
1945 do_cross_jump (insn, newjpos, newlpos);
1946 /* Make the old conditional jump
1947 into an unconditional one. */
1948 SET_SRC (PATTERN (insn))
1949 = gen_rtx (LABEL_REF, VOIDmode, JUMP_LABEL (insn));
1950 INSN_CODE (insn) = -1;
1951 emit_barrier_after (insn);
1952 /* Add to jump_chain unless this is a new label
1953 whose UID is too large. */
1954 if (INSN_UID (JUMP_LABEL (insn)) < max_jump_chain)
1956 jump_chain[INSN_UID (insn)]
1957 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
1958 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
1965 /* Cross jumping of unconditional jumps:
1966 a few differences. */
1968 if (cross_jump && simplejump_p (insn))
1970 rtx newjpos, newlpos;
1975 /* TARGET is nonzero if it is ok to cross jump
1976 to code before TARGET. If so, see if matches. */
1977 find_cross_jump (insn, JUMP_LABEL (insn), 1,
1978 &newjpos, &newlpos);
1980 /* If cannot cross jump to code before the label,
1981 see if we can cross jump to another jump to
1983 /* Try each other jump to this label. */
1984 if (INSN_UID (JUMP_LABEL (insn)) < max_uid)
1985 for (target = jump_chain[INSN_UID (JUMP_LABEL (insn))];
1986 target != 0 && newjpos == 0;
1987 target = jump_chain[INSN_UID (target)])
1989 && JUMP_LABEL (target) == JUMP_LABEL (insn)
1990 /* Ignore TARGET if it's deleted. */
1991 && ! INSN_DELETED_P (target))
1992 find_cross_jump (insn, target, 2,
1993 &newjpos, &newlpos);
1997 do_cross_jump (insn, newjpos, newlpos);
2003 /* This code was dead in the previous jump.c! */
2004 if (cross_jump && GET_CODE (PATTERN (insn)) == RETURN)
2006 /* Return insns all "jump to the same place"
2007 so we can cross-jump between any two of them. */
2009 rtx newjpos, newlpos, target;
2013 /* If cannot cross jump to code before the label,
2014 see if we can cross jump to another jump to
2016 /* Try each other jump to this label. */
2017 for (target = jump_chain[0];
2018 target != 0 && newjpos == 0;
2019 target = jump_chain[INSN_UID (target)])
2021 && ! INSN_DELETED_P (target)
2022 && GET_CODE (PATTERN (target)) == RETURN)
2023 find_cross_jump (insn, target, 2,
2024 &newjpos, &newlpos);
2028 do_cross_jump (insn, newjpos, newlpos);
2039 /* Delete extraneous line number notes.
2040 Note that two consecutive notes for different lines are not really
2041 extraneous. There should be some indication where that line belonged,
2042 even if it became empty. */
2047 for (insn = f; insn; insn = NEXT_INSN (insn))
2048 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0)
2050 /* Delete this note if it is identical to previous note. */
2052 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
2053 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
2066 /* If we fall through to the epilogue, see if we can insert a RETURN insn
2067 in front of it. If the machine allows it at this point (we might be
2068 after reload for a leaf routine), it will improve optimization for it
2069 to be there. We do this both here and at the start of this pass since
2070 the RETURN might have been deleted by some of our optimizations. */
2071 insn = get_last_insn ();
2072 while (insn && GET_CODE (insn) == NOTE)
2073 insn = PREV_INSN (insn);
2075 if (insn && GET_CODE (insn) != BARRIER)
2077 emit_jump_insn (gen_return ());
2083 /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
2084 If so, delete it, and record that this function can drop off the end. */
2090 /* One label can follow the end-note: the return label. */
2091 && ((GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
2092 /* Ordinary insns can follow it if returning a structure. */
2093 || GET_CODE (insn) == INSN
2094 /* If machine uses explicit RETURN insns, no epilogue,
2095 then one of them follows the note. */
2096 || (GET_CODE (insn) == JUMP_INSN
2097 && GET_CODE (PATTERN (insn)) == RETURN)
2098 /* A barrier can follow the return insn. */
2099 || GET_CODE (insn) == BARRIER
2100 /* Other kinds of notes can follow also. */
2101 || (GET_CODE (insn) == NOTE
2102 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)))
2103 insn = PREV_INSN (insn);
2106 /* Report if control can fall through at the end of the function. */
2107 if (insn && GET_CODE (insn) == NOTE
2108 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END)
2114 /* Show JUMP_CHAIN no longer valid. */
2118 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
2119 jump. Assume that this unconditional jump is to the exit test code. If
2120 the code is sufficiently simple, make a copy of it before INSN,
2121 followed by a jump to the exit of the loop. Then delete the unconditional
2124 Note that it is possible we can get confused here if the jump immediately
2125 after the loop start branches outside the loop but within an outer loop.
2126 If we are near the exit of that loop, we will copy its exit test. This
2127 will not generate incorrect code, but could suppress some optimizations.
2128 However, such cases are degenerate loops anyway.
2130 Return 1 if we made the change, else 0.
2132 This is only safe immediately after a regscan pass because it uses the
2133 values of regno_first_uid and regno_last_uid. */
2136 duplicate_loop_exit_test (loop_start)
2139 rtx insn, set, reg, p, link;
2142 rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
2144 int max_reg = max_reg_num ();
2147 /* Scan the exit code. We do not perform this optimization if any insn:
2151 has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
2152 is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
2153 is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
2156 Also, don't do this if the exit code is more than 20 insns. */
2158 for (insn = exitcode;
2160 && ! (GET_CODE (insn) == NOTE
2161 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
2162 insn = NEXT_INSN (insn))
2164 switch (GET_CODE (insn))
2170 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2171 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2172 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
2177 if (++num_insns > 20
2178 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
2179 || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2185 /* Unless INSN is zero, we can do the optimization. */
2191 /* See if any insn sets a register only used in the loop exit code and
2192 not a user variable. If so, replace it with a new register. */
2193 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2194 if (GET_CODE (insn) == INSN
2195 && (set = single_set (insn)) != 0
2196 && ((reg = SET_DEST (set), GET_CODE (reg) == REG)
2197 || (GET_CODE (reg) == SUBREG
2198 && (reg = SUBREG_REG (reg), GET_CODE (reg) == REG)))
2199 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
2200 && regno_first_uid[REGNO (reg)] == INSN_UID (insn))
2202 for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
2203 if (regno_last_uid[REGNO (reg)] == INSN_UID (p))
2208 /* We can do the replacement. Allocate reg_map if this is the
2209 first replacement we found. */
2212 reg_map = (rtx *) alloca (max_reg * sizeof (rtx));
2213 bzero ((char *) reg_map, max_reg * sizeof (rtx));
2216 REG_LOOP_TEST_P (reg) = 1;
2218 reg_map[REGNO (reg)] = gen_reg_rtx (GET_MODE (reg));
2222 /* Now copy each insn. */
2223 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2224 switch (GET_CODE (insn))
2227 copy = emit_barrier_before (loop_start);
2230 /* Only copy line-number notes. */
2231 if (NOTE_LINE_NUMBER (insn) >= 0)
2233 copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
2234 NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
2239 copy = emit_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2241 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2243 mark_jump_label (PATTERN (copy), copy, 0);
2245 /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
2247 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2248 if (REG_NOTE_KIND (link) != REG_LABEL)
2250 = copy_rtx (gen_rtx (EXPR_LIST, REG_NOTE_KIND (link),
2251 XEXP (link, 0), REG_NOTES (copy)));
2252 if (reg_map && REG_NOTES (copy))
2253 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2257 copy = emit_jump_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2259 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2260 mark_jump_label (PATTERN (copy), copy, 0);
2261 if (REG_NOTES (insn))
2263 REG_NOTES (copy) = copy_rtx (REG_NOTES (insn));
2265 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2268 /* If this is a simple jump, add it to the jump chain. */
2270 if (INSN_UID (copy) < max_jump_chain && JUMP_LABEL (copy)
2271 && simplejump_p (copy))
2273 jump_chain[INSN_UID (copy)]
2274 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2275 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2283 /* Now clean up by emitting a jump to the end label and deleting the jump
2284 at the start of the loop. */
2285 if (! copy || GET_CODE (copy) != BARRIER)
2287 copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
2289 mark_jump_label (PATTERN (copy), copy, 0);
2290 if (INSN_UID (copy) < max_jump_chain
2291 && INSN_UID (JUMP_LABEL (copy)) < max_jump_chain)
2293 jump_chain[INSN_UID (copy)]
2294 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2295 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2297 emit_barrier_before (loop_start);
2300 /* Mark the exit code as the virtual top of the converted loop. */
2301 emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
2303 delete_insn (next_nonnote_insn (loop_start));
2308 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, and
2309 loop-end notes between START and END out before START. Assume that
2310 END is not such a note. START may be such a note. Returns the value
2311 of the new starting insn, which may be different if the original start
2315 squeeze_notes (start, end)
2321 for (insn = start; insn != end; insn = next)
2323 next = NEXT_INSN (insn);
2324 if (GET_CODE (insn) == NOTE
2325 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
2326 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2327 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2328 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
2329 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
2330 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
2336 rtx prev = PREV_INSN (insn);
2337 PREV_INSN (insn) = PREV_INSN (start);
2338 NEXT_INSN (insn) = start;
2339 NEXT_INSN (PREV_INSN (insn)) = insn;
2340 PREV_INSN (NEXT_INSN (insn)) = insn;
2341 NEXT_INSN (prev) = next;
2342 PREV_INSN (next) = prev;
2350 /* Compare the instructions before insn E1 with those before E2
2351 to find an opportunity for cross jumping.
2352 (This means detecting identical sequences of insns followed by
2353 jumps to the same place, or followed by a label and a jump
2354 to that label, and replacing one with a jump to the other.)
2356 Assume E1 is a jump that jumps to label E2
2357 (that is not always true but it might as well be).
2358 Find the longest possible equivalent sequences
2359 and store the first insns of those sequences into *F1 and *F2.
2360 Store zero there if no equivalent preceding instructions are found.
2362 We give up if we find a label in stream 1.
2363 Actually we could transfer that label into stream 2. */
2366 find_cross_jump (e1, e2, minimum, f1, f2)
2371 register rtx i1 = e1, i2 = e2;
2372 register rtx p1, p2;
2375 rtx last1 = 0, last2 = 0;
2376 rtx afterlast1 = 0, afterlast2 = 0;
2384 i1 = prev_nonnote_insn (i1);
2386 i2 = PREV_INSN (i2);
2387 while (i2 && (GET_CODE (i2) == NOTE || GET_CODE (i2) == CODE_LABEL))
2388 i2 = PREV_INSN (i2);
2393 /* Don't allow the range of insns preceding E1 or E2
2394 to include the other (E2 or E1). */
2395 if (i2 == e1 || i1 == e2)
2398 /* If we will get to this code by jumping, those jumps will be
2399 tensioned to go directly to the new label (before I2),
2400 so this cross-jumping won't cost extra. So reduce the minimum. */
2401 if (GET_CODE (i1) == CODE_LABEL)
2407 if (i2 == 0 || GET_CODE (i1) != GET_CODE (i2))
2413 /* If this is a CALL_INSN, compare register usage information.
2414 If we don't check this on stack register machines, the two
2415 CALL_INSNs might be merged leaving reg-stack.c with mismatching
2416 numbers of stack registers in the same basic block.
2417 If we don't check this on machines with delay slots, a delay slot may
2418 be filled that clobbers a parameter expected by the subroutine.
2420 ??? We take the simple route for now and assume that if they're
2421 equal, they were constructed identically. */
2423 if (GET_CODE (i1) == CALL_INSN
2424 && ! rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
2425 CALL_INSN_FUNCTION_USAGE (i2)))
2429 /* If cross_jump_death_matters is not 0, the insn's mode
2430 indicates whether or not the insn contains any stack-like
2433 if (!lose && cross_jump_death_matters && GET_MODE (i1) == QImode)
2435 /* If register stack conversion has already been done, then
2436 death notes must also be compared before it is certain that
2437 the two instruction streams match. */
2440 HARD_REG_SET i1_regset, i2_regset;
2442 CLEAR_HARD_REG_SET (i1_regset);
2443 CLEAR_HARD_REG_SET (i2_regset);
2445 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
2446 if (REG_NOTE_KIND (note) == REG_DEAD
2447 && STACK_REG_P (XEXP (note, 0)))
2448 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
2450 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
2451 if (REG_NOTE_KIND (note) == REG_DEAD
2452 && STACK_REG_P (XEXP (note, 0)))
2453 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
2455 GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
2464 if (lose || GET_CODE (p1) != GET_CODE (p2)
2465 || ! rtx_renumbered_equal_p (p1, p2))
2467 /* The following code helps take care of G++ cleanups. */
2471 if (!lose && GET_CODE (p1) == GET_CODE (p2)
2472 && ((equiv1 = find_reg_note (i1, REG_EQUAL, NULL_RTX)) != 0
2473 || (equiv1 = find_reg_note (i1, REG_EQUIV, NULL_RTX)) != 0)
2474 && ((equiv2 = find_reg_note (i2, REG_EQUAL, NULL_RTX)) != 0
2475 || (equiv2 = find_reg_note (i2, REG_EQUIV, NULL_RTX)) != 0)
2476 /* If the equivalences are not to a constant, they may
2477 reference pseudos that no longer exist, so we can't
2479 && CONSTANT_P (XEXP (equiv1, 0))
2480 && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
2482 rtx s1 = single_set (i1);
2483 rtx s2 = single_set (i2);
2484 if (s1 != 0 && s2 != 0
2485 && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
2487 validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
2488 validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
2489 if (! rtx_renumbered_equal_p (p1, p2))
2491 else if (apply_change_group ())
2496 /* Insns fail to match; cross jumping is limited to the following
2500 /* Don't allow the insn after a compare to be shared by
2501 cross-jumping unless the compare is also shared.
2502 Here, if either of these non-matching insns is a compare,
2503 exclude the following insn from possible cross-jumping. */
2504 if (sets_cc0_p (p1) || sets_cc0_p (p2))
2505 last1 = afterlast1, last2 = afterlast2, ++minimum;
2508 /* If cross-jumping here will feed a jump-around-jump
2509 optimization, this jump won't cost extra, so reduce
2511 if (GET_CODE (i1) == JUMP_INSN
2513 && prev_real_insn (JUMP_LABEL (i1)) == e1)
2519 if (GET_CODE (p1) != USE && GET_CODE (p1) != CLOBBER)
2521 /* Ok, this insn is potentially includable in a cross-jump here. */
2522 afterlast1 = last1, afterlast2 = last2;
2523 last1 = i1, last2 = i2, --minimum;
2527 if (minimum <= 0 && last1 != 0 && last1 != e1)
2528 *f1 = last1, *f2 = last2;
2532 do_cross_jump (insn, newjpos, newlpos)
2533 rtx insn, newjpos, newlpos;
2535 /* Find an existing label at this point
2536 or make a new one if there is none. */
2537 register rtx label = get_label_before (newlpos);
2539 /* Make the same jump insn jump to the new point. */
2540 if (GET_CODE (PATTERN (insn)) == RETURN)
2542 /* Remove from jump chain of returns. */
2543 delete_from_jump_chain (insn);
2544 /* Change the insn. */
2545 PATTERN (insn) = gen_jump (label);
2546 INSN_CODE (insn) = -1;
2547 JUMP_LABEL (insn) = label;
2548 LABEL_NUSES (label)++;
2549 /* Add to new the jump chain. */
2550 if (INSN_UID (label) < max_jump_chain
2551 && INSN_UID (insn) < max_jump_chain)
2553 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (label)];
2554 jump_chain[INSN_UID (label)] = insn;
2558 redirect_jump (insn, label);
2560 /* Delete the matching insns before the jump. Also, remove any REG_EQUAL
2561 or REG_EQUIV note in the NEWLPOS stream that isn't also present in
2562 the NEWJPOS stream. */
2564 while (newjpos != insn)
2568 for (lnote = REG_NOTES (newlpos); lnote; lnote = XEXP (lnote, 1))
2569 if ((REG_NOTE_KIND (lnote) == REG_EQUAL
2570 || REG_NOTE_KIND (lnote) == REG_EQUIV)
2571 && ! find_reg_note (newjpos, REG_EQUAL, XEXP (lnote, 0))
2572 && ! find_reg_note (newjpos, REG_EQUIV, XEXP (lnote, 0)))
2573 remove_note (newlpos, lnote);
2575 delete_insn (newjpos);
2576 newjpos = next_real_insn (newjpos);
2577 newlpos = next_real_insn (newlpos);
2581 /* Return the label before INSN, or put a new label there. */
2584 get_label_before (insn)
2589 /* Find an existing label at this point
2590 or make a new one if there is none. */
2591 label = prev_nonnote_insn (insn);
2593 if (label == 0 || GET_CODE (label) != CODE_LABEL)
2595 rtx prev = PREV_INSN (insn);
2597 label = gen_label_rtx ();
2598 emit_label_after (label, prev);
2599 LABEL_NUSES (label) = 0;
2604 /* Return the label after INSN, or put a new label there. */
2607 get_label_after (insn)
2612 /* Find an existing label at this point
2613 or make a new one if there is none. */
2614 label = next_nonnote_insn (insn);
2616 if (label == 0 || GET_CODE (label) != CODE_LABEL)
2618 label = gen_label_rtx ();
2619 emit_label_after (label, insn);
2620 LABEL_NUSES (label) = 0;
2625 /* Return 1 if INSN is a jump that jumps to right after TARGET
2626 only on the condition that TARGET itself would drop through.
2627 Assumes that TARGET is a conditional jump. */
2630 jump_back_p (insn, target)
2634 enum rtx_code codei, codet;
2636 if (simplejump_p (insn) || ! condjump_p (insn)
2637 || simplejump_p (target)
2638 || target != prev_real_insn (JUMP_LABEL (insn)))
2641 cinsn = XEXP (SET_SRC (PATTERN (insn)), 0);
2642 ctarget = XEXP (SET_SRC (PATTERN (target)), 0);
2644 codei = GET_CODE (cinsn);
2645 codet = GET_CODE (ctarget);
2647 if (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx)
2649 if (! can_reverse_comparison_p (cinsn, insn))
2651 codei = reverse_condition (codei);
2654 if (XEXP (SET_SRC (PATTERN (target)), 2) == pc_rtx)
2656 if (! can_reverse_comparison_p (ctarget, target))
2658 codet = reverse_condition (codet);
2661 return (codei == codet
2662 && rtx_renumbered_equal_p (XEXP (cinsn, 0), XEXP (ctarget, 0))
2663 && rtx_renumbered_equal_p (XEXP (cinsn, 1), XEXP (ctarget, 1)));
2666 /* Given a comparison, COMPARISON, inside a conditional jump insn, INSN,
2667 return non-zero if it is safe to reverse this comparison. It is if our
2668 floating-point is not IEEE, if this is an NE or EQ comparison, or if
2669 this is known to be an integer comparison. */
2672 can_reverse_comparison_p (comparison, insn)
2678 /* If this is not actually a comparison, we can't reverse it. */
2679 if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
2682 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
2683 /* If this is an NE comparison, it is safe to reverse it to an EQ
2684 comparison and vice versa, even for floating point. If no operands
2685 are NaNs, the reversal is valid. If some operand is a NaN, EQ is
2686 always false and NE is always true, so the reversal is also valid. */
2688 || GET_CODE (comparison) == NE
2689 || GET_CODE (comparison) == EQ)
2692 arg0 = XEXP (comparison, 0);
2694 /* Make sure ARG0 is one of the actual objects being compared. If we
2695 can't do this, we can't be sure the comparison can be reversed.
2697 Handle cc0 and a MODE_CC register. */
2698 if ((GET_CODE (arg0) == REG && GET_MODE_CLASS (GET_MODE (arg0)) == MODE_CC)
2704 rtx prev = prev_nonnote_insn (insn);
2705 rtx set = single_set (prev);
2707 if (set == 0 || SET_DEST (set) != arg0)
2710 arg0 = SET_SRC (set);
2712 if (GET_CODE (arg0) == COMPARE)
2713 arg0 = XEXP (arg0, 0);
2716 /* We can reverse this if ARG0 is a CONST_INT or if its mode is
2717 not VOIDmode and neither a MODE_CC nor MODE_FLOAT type. */
2718 return (GET_CODE (arg0) == CONST_INT
2719 || (GET_MODE (arg0) != VOIDmode
2720 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_CC
2721 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_FLOAT));
2724 /* Given an rtx-code for a comparison, return the code
2725 for the negated comparison.
2726 WATCH OUT! reverse_condition is not safe to use on a jump
2727 that might be acting on the results of an IEEE floating point comparison,
2728 because of the special treatment of non-signaling nans in comparisons.
2729 Use can_reverse_comparison_p to be sure. */
2732 reverse_condition (code)
2773 /* Similar, but return the code when two operands of a comparison are swapped.
2774 This IS safe for IEEE floating-point. */
2777 swap_condition (code)
2816 /* Given a comparison CODE, return the corresponding unsigned comparison.
2817 If CODE is an equality comparison or already an unsigned comparison,
2818 CODE is returned. */
2821 unsigned_condition (code)
2851 /* Similarly, return the signed version of a comparison. */
2854 signed_condition (code)
2884 /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
2885 truth of CODE1 implies the truth of CODE2. */
2888 comparison_dominates_p (code1, code2)
2889 enum rtx_code code1, code2;
2897 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU)
2902 if (code2 == LE || code2 == NE)
2907 if (code2 == GE || code2 == NE)
2912 if (code2 == LEU || code2 == NE)
2917 if (code2 == GEU || code2 == NE)
2925 /* Return 1 if INSN is an unconditional jump and nothing else. */
2931 return (GET_CODE (insn) == JUMP_INSN
2932 && GET_CODE (PATTERN (insn)) == SET
2933 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
2934 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
2937 /* Return nonzero if INSN is a (possibly) conditional jump
2938 and nothing more. */
2944 register rtx x = PATTERN (insn);
2945 if (GET_CODE (x) != SET)
2947 if (GET_CODE (SET_DEST (x)) != PC)
2949 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
2951 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
2953 if (XEXP (SET_SRC (x), 2) == pc_rtx
2954 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
2955 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
2957 if (XEXP (SET_SRC (x), 1) == pc_rtx
2958 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
2959 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
2964 /* Return nonzero if INSN is a (possibly) conditional jump
2965 and nothing more. */
2968 condjump_in_parallel_p (insn)
2971 register rtx x = PATTERN (insn);
2973 if (GET_CODE (x) != PARALLEL)
2976 x = XVECEXP (x, 0, 0);
2978 if (GET_CODE (x) != SET)
2980 if (GET_CODE (SET_DEST (x)) != PC)
2982 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
2984 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
2986 if (XEXP (SET_SRC (x), 2) == pc_rtx
2987 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
2988 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
2990 if (XEXP (SET_SRC (x), 1) == pc_rtx
2991 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
2992 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
2997 /* Return 1 if X is an RTX that does nothing but set the condition codes
2998 and CLOBBER or USE registers.
2999 Return -1 if X does explicitly set the condition codes,
3000 but also does other things. */
3007 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
3009 if (GET_CODE (x) == PARALLEL)
3013 int other_things = 0;
3014 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3016 if (GET_CODE (XVECEXP (x, 0, i)) == SET
3017 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
3019 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
3022 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
3030 /* Follow any unconditional jump at LABEL;
3031 return the ultimate label reached by any such chain of jumps.
3032 If LABEL is not followed by a jump, return LABEL.
3033 If the chain loops or we can't find end, return LABEL,
3034 since that tells caller to avoid changing the insn.
3036 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
3037 a USE or CLOBBER. */
3040 follow_jumps (label)
3045 register rtx value = label;
3050 && (insn = next_active_insn (value)) != 0
3051 && GET_CODE (insn) == JUMP_INSN
3052 && (JUMP_LABEL (insn) != 0 || GET_CODE (PATTERN (insn)) == RETURN)
3053 && (next = NEXT_INSN (insn))
3054 && GET_CODE (next) == BARRIER);
3057 /* Don't chain through the insn that jumps into a loop
3058 from outside the loop,
3059 since that would create multiple loop entry jumps
3060 and prevent loop optimization. */
3062 if (!reload_completed)
3063 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
3064 if (GET_CODE (tem) == NOTE
3065 && NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG)
3068 /* If we have found a cycle, make the insn jump to itself. */
3069 if (JUMP_LABEL (insn) == label)
3072 tem = next_active_insn (JUMP_LABEL (insn));
3073 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
3074 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
3077 value = JUMP_LABEL (insn);
3084 /* Assuming that field IDX of X is a vector of label_refs,
3085 replace each of them by the ultimate label reached by it.
3086 Return nonzero if a change is made.
3087 If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG. */
3090 tension_vector_labels (x, idx)
3096 for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
3098 register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
3099 register rtx nlabel = follow_jumps (olabel);
3100 if (nlabel && nlabel != olabel)
3102 XEXP (XVECEXP (x, idx, i), 0) = nlabel;
3103 ++LABEL_NUSES (nlabel);
3104 if (--LABEL_NUSES (olabel) == 0)
3105 delete_insn (olabel);
3112 /* Find all CODE_LABELs referred to in X, and increment their use counts.
3113 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
3114 in INSN, then store one of them in JUMP_LABEL (INSN).
3115 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
3116 referenced in INSN, add a REG_LABEL note containing that label to INSN.
3117 Also, when there are consecutive labels, canonicalize on the last of them.
3119 Note that two labels separated by a loop-beginning note
3120 must be kept distinct if we have not yet done loop-optimization,
3121 because the gap between them is where loop-optimize
3122 will want to move invariant code to. CROSS_JUMP tells us
3123 that loop-optimization is done with.
3125 Once reload has completed (CROSS_JUMP non-zero), we need not consider
3126 two labels distinct if they are separated by only USE or CLOBBER insns. */
3129 mark_jump_label (x, insn, cross_jump)
3134 register RTX_CODE code = GET_CODE (x);
3152 /* If this is a constant-pool reference, see if it is a label. */
3153 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3154 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3155 mark_jump_label (get_pool_constant (XEXP (x, 0)), insn, cross_jump);
3160 rtx label = XEXP (x, 0);
3165 if (GET_CODE (label) != CODE_LABEL)
3168 /* Ignore references to labels of containing functions. */
3169 if (LABEL_REF_NONLOCAL_P (x))
3172 /* If there are other labels following this one,
3173 replace it with the last of the consecutive labels. */
3174 for (next = NEXT_INSN (label); next; next = NEXT_INSN (next))
3176 if (GET_CODE (next) == CODE_LABEL)
3178 else if (cross_jump && GET_CODE (next) == INSN
3179 && (GET_CODE (PATTERN (next)) == USE
3180 || GET_CODE (PATTERN (next)) == CLOBBER))
3182 else if (GET_CODE (next) != NOTE)
3184 else if (! cross_jump
3185 && (NOTE_LINE_NUMBER (next) == NOTE_INSN_LOOP_BEG
3186 || NOTE_LINE_NUMBER (next) == NOTE_INSN_FUNCTION_END))
3190 XEXP (x, 0) = label;
3191 ++LABEL_NUSES (label);
3195 if (GET_CODE (insn) == JUMP_INSN)
3196 JUMP_LABEL (insn) = label;
3198 /* If we've changed OLABEL and we had a REG_LABEL note
3199 for it, update it as well. */
3200 else if (label != olabel
3201 && (note = find_reg_note (insn, REG_LABEL, olabel)) != 0)
3202 XEXP (note, 0) = label;
3204 /* Otherwise, add a REG_LABEL note for LABEL unless there already
3206 else if (! find_reg_note (insn, REG_LABEL, label))
3208 rtx next = next_real_insn (label);
3209 /* Don't record labels that refer to dispatch tables.
3210 This is not necessary, since the tablejump
3211 references the same label.
3212 And if we did record them, flow.c would make worse code. */
3214 || ! (GET_CODE (next) == JUMP_INSN
3215 && (GET_CODE (PATTERN (next)) == ADDR_VEC
3216 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
3217 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, label,
3224 /* Do walk the labels in a vector, but not the first operand of an
3225 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
3229 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
3231 for (i = 0; i < XVECLEN (x, eltnum); i++)
3232 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, cross_jump);
3237 fmt = GET_RTX_FORMAT (code);
3238 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3241 mark_jump_label (XEXP (x, i), insn, cross_jump);
3242 else if (fmt[i] == 'E')
3245 for (j = 0; j < XVECLEN (x, i); j++)
3246 mark_jump_label (XVECEXP (x, i, j), insn, cross_jump);
3251 /* If all INSN does is set the pc, delete it,
3252 and delete the insn that set the condition codes for it
3253 if that's what the previous thing was. */
3259 register rtx set = single_set (insn);
3261 if (set && GET_CODE (SET_DEST (set)) == PC)
3262 delete_computation (insn);
3265 /* Delete INSN and recursively delete insns that compute values used only
3266 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3267 If we are running before flow.c, we need do nothing since flow.c will
3268 delete dead code. We also can't know if the registers being used are
3269 dead or not at this point.
3271 Otherwise, look at all our REG_DEAD notes. If a previous insn does
3272 nothing other than set a register that dies in this insn, we can delete
3275 On machines with CC0, if CC0 is used in this insn, we may be able to
3276 delete the insn that set it. */
3279 delete_computation (insn)
3285 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
3287 rtx prev = prev_nonnote_insn (insn);
3288 /* We assume that at this stage
3289 CC's are always set explicitly
3290 and always immediately before the jump that
3291 will use them. So if the previous insn
3292 exists to set the CC's, delete it
3293 (unless it performs auto-increments, etc.). */
3294 if (prev && GET_CODE (prev) == INSN
3295 && sets_cc0_p (PATTERN (prev)))
3297 if (sets_cc0_p (PATTERN (prev)) > 0
3298 && !FIND_REG_INC_NOTE (prev, NULL_RTX))
3299 delete_computation (prev);
3301 /* Otherwise, show that cc0 won't be used. */
3302 REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_UNUSED,
3303 cc0_rtx, REG_NOTES (prev));
3308 for (note = REG_NOTES (insn); note; note = next)
3312 next = XEXP (note, 1);
3314 if (REG_NOTE_KIND (note) != REG_DEAD
3315 /* Verify that the REG_NOTE is legitimate. */
3316 || GET_CODE (XEXP (note, 0)) != REG)
3319 for (our_prev = prev_nonnote_insn (insn);
3320 our_prev && GET_CODE (our_prev) == INSN;
3321 our_prev = prev_nonnote_insn (our_prev))
3323 /* If we reach a SEQUENCE, it is too complex to try to
3324 do anything with it, so give up. */
3325 if (GET_CODE (PATTERN (our_prev)) == SEQUENCE)
3328 if (GET_CODE (PATTERN (our_prev)) == USE
3329 && GET_CODE (XEXP (PATTERN (our_prev), 0)) == INSN)
3330 /* reorg creates USEs that look like this. We leave them
3331 alone because reorg needs them for its own purposes. */
3334 if (reg_set_p (XEXP (note, 0), PATTERN (our_prev)))
3336 if (FIND_REG_INC_NOTE (our_prev, NULL_RTX))
3339 if (GET_CODE (PATTERN (our_prev)) == PARALLEL)
3341 /* If we find a SET of something else, we can't
3346 for (i = 0; i < XVECLEN (PATTERN (our_prev), 0); i++)
3348 rtx part = XVECEXP (PATTERN (our_prev), 0, i);
3350 if (GET_CODE (part) == SET
3351 && SET_DEST (part) != XEXP (note, 0))
3355 if (i == XVECLEN (PATTERN (our_prev), 0))
3356 delete_computation (our_prev);
3358 else if (GET_CODE (PATTERN (our_prev)) == SET
3359 && SET_DEST (PATTERN (our_prev)) == XEXP (note, 0))
3360 delete_computation (our_prev);
3365 /* If OUR_PREV references the register that dies here, it is an
3366 additional use. Hence any prior SET isn't dead. However, this
3367 insn becomes the new place for the REG_DEAD note. */
3368 if (reg_overlap_mentioned_p (XEXP (note, 0),
3369 PATTERN (our_prev)))
3371 XEXP (note, 1) = REG_NOTES (our_prev);
3372 REG_NOTES (our_prev) = note;
3381 /* Delete insn INSN from the chain of insns and update label ref counts.
3382 May delete some following insns as a consequence; may even delete
3383 a label elsewhere and insns that follow it.
3385 Returns the first insn after INSN that was not deleted. */
3391 register rtx next = NEXT_INSN (insn);
3392 register rtx prev = PREV_INSN (insn);
3393 register int was_code_label = (GET_CODE (insn) == CODE_LABEL);
3394 register int dont_really_delete = 0;
3396 while (next && INSN_DELETED_P (next))
3397 next = NEXT_INSN (next);
3399 /* This insn is already deleted => return first following nondeleted. */
3400 if (INSN_DELETED_P (insn))
3403 /* Don't delete user-declared labels. Convert them to special NOTEs
3405 if (was_code_label && LABEL_NAME (insn) != 0
3406 && optimize && ! dont_really_delete)
3408 PUT_CODE (insn, NOTE);
3409 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
3410 NOTE_SOURCE_FILE (insn) = 0;
3411 dont_really_delete = 1;
3414 /* Mark this insn as deleted. */
3415 INSN_DELETED_P (insn) = 1;
3417 /* If this is an unconditional jump, delete it from the jump chain. */
3418 if (simplejump_p (insn))
3419 delete_from_jump_chain (insn);
3421 /* If instruction is followed by a barrier,
3422 delete the barrier too. */
3424 if (next != 0 && GET_CODE (next) == BARRIER)
3426 INSN_DELETED_P (next) = 1;
3427 next = NEXT_INSN (next);
3430 /* Patch out INSN (and the barrier if any) */
3432 if (optimize && ! dont_really_delete)
3436 NEXT_INSN (prev) = next;
3437 if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
3438 NEXT_INSN (XVECEXP (PATTERN (prev), 0,
3439 XVECLEN (PATTERN (prev), 0) - 1)) = next;
3444 PREV_INSN (next) = prev;
3445 if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
3446 PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
3449 if (prev && NEXT_INSN (prev) == 0)
3450 set_last_insn (prev);
3453 /* If deleting a jump, decrement the count of the label,
3454 and delete the label if it is now unused. */
3456 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
3457 if (--LABEL_NUSES (JUMP_LABEL (insn)) == 0)
3459 /* This can delete NEXT or PREV,
3460 either directly if NEXT is JUMP_LABEL (INSN),
3461 or indirectly through more levels of jumps. */
3462 delete_insn (JUMP_LABEL (insn));
3463 /* I feel a little doubtful about this loop,
3464 but I see no clean and sure alternative way
3465 to find the first insn after INSN that is not now deleted.
3466 I hope this works. */
3467 while (next && INSN_DELETED_P (next))
3468 next = NEXT_INSN (next);
3472 /* Likewise if we're deleting a dispatch table. */
3474 if (GET_CODE (insn) == JUMP_INSN
3475 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
3476 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
3478 rtx pat = PATTERN (insn);
3479 int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
3480 int len = XVECLEN (pat, diff_vec_p);
3482 for (i = 0; i < len; i++)
3483 if (--LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
3484 delete_insn (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
3485 while (next && INSN_DELETED_P (next))
3486 next = NEXT_INSN (next);
3490 while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
3491 prev = PREV_INSN (prev);
3493 /* If INSN was a label and a dispatch table follows it,
3494 delete the dispatch table. The tablejump must have gone already.
3495 It isn't useful to fall through into a table. */
3498 && NEXT_INSN (insn) != 0
3499 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
3500 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
3501 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
3502 next = delete_insn (NEXT_INSN (insn));
3504 /* If INSN was a label, delete insns following it if now unreachable. */
3506 if (was_code_label && prev && GET_CODE (prev) == BARRIER)
3508 register RTX_CODE code;
3510 && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
3511 || code == NOTE || code == BARRIER
3512 || (code == CODE_LABEL && INSN_DELETED_P (next))))
3515 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
3516 next = NEXT_INSN (next);
3517 /* Keep going past other deleted labels to delete what follows. */
3518 else if (code == CODE_LABEL && INSN_DELETED_P (next))
3519 next = NEXT_INSN (next);
3521 /* Note: if this deletes a jump, it can cause more
3522 deletion of unreachable code, after a different label.
3523 As long as the value from this recursive call is correct,
3524 this invocation functions correctly. */
3525 next = delete_insn (next);
3532 /* Advance from INSN till reaching something not deleted
3533 then return that. May return INSN itself. */
3536 next_nondeleted_insn (insn)
3539 while (INSN_DELETED_P (insn))
3540 insn = NEXT_INSN (insn);
3544 /* Delete a range of insns from FROM to TO, inclusive.
3545 This is for the sake of peephole optimization, so assume
3546 that whatever these insns do will still be done by a new
3547 peephole insn that will replace them. */
3550 delete_for_peephole (from, to)
3551 register rtx from, to;
3553 register rtx insn = from;
3557 register rtx next = NEXT_INSN (insn);
3558 register rtx prev = PREV_INSN (insn);
3560 if (GET_CODE (insn) != NOTE)
3562 INSN_DELETED_P (insn) = 1;
3564 /* Patch this insn out of the chain. */
3565 /* We don't do this all at once, because we
3566 must preserve all NOTEs. */
3568 NEXT_INSN (prev) = next;
3571 PREV_INSN (next) = prev;
3579 /* Note that if TO is an unconditional jump
3580 we *do not* delete the BARRIER that follows,
3581 since the peephole that replaces this sequence
3582 is also an unconditional jump in that case. */
3585 /* Invert the condition of the jump JUMP, and make it jump
3586 to label NLABEL instead of where it jumps now. */
3589 invert_jump (jump, nlabel)
3592 /* We have to either invert the condition and change the label or
3593 do neither. Either operation could fail. We first try to invert
3594 the jump. If that succeeds, we try changing the label. If that fails,
3595 we invert the jump back to what it was. */
3597 if (! invert_exp (PATTERN (jump), jump))
3600 if (redirect_jump (jump, nlabel))
3603 if (! invert_exp (PATTERN (jump), jump))
3604 /* This should just be putting it back the way it was. */
3610 /* Invert the jump condition of rtx X contained in jump insn, INSN.
3612 Return 1 if we can do so, 0 if we cannot find a way to do so that
3613 matches a pattern. */
3616 invert_exp (x, insn)
3620 register RTX_CODE code;
3624 code = GET_CODE (x);
3626 if (code == IF_THEN_ELSE)
3628 register rtx comp = XEXP (x, 0);
3631 /* We can do this in two ways: The preferable way, which can only
3632 be done if this is not an integer comparison, is to reverse
3633 the comparison code. Otherwise, swap the THEN-part and ELSE-part
3634 of the IF_THEN_ELSE. If we can't do either, fail. */
3636 if (can_reverse_comparison_p (comp, insn)
3637 && validate_change (insn, &XEXP (x, 0),
3638 gen_rtx (reverse_condition (GET_CODE (comp)),
3639 GET_MODE (comp), XEXP (comp, 0),
3640 XEXP (comp, 1)), 0))
3644 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
3645 validate_change (insn, &XEXP (x, 2), tem, 1);
3646 return apply_change_group ();
3649 fmt = GET_RTX_FORMAT (code);
3650 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3653 if (! invert_exp (XEXP (x, i), insn))
3658 for (j = 0; j < XVECLEN (x, i); j++)
3659 if (!invert_exp (XVECEXP (x, i, j), insn))
3667 /* Make jump JUMP jump to label NLABEL instead of where it jumps now.
3668 If the old jump target label is unused as a result,
3669 it and the code following it may be deleted.
3671 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
3674 The return value will be 1 if the change was made, 0 if it wasn't (this
3675 can only occur for NLABEL == 0). */
3678 redirect_jump (jump, nlabel)
3681 register rtx olabel = JUMP_LABEL (jump);
3683 if (nlabel == olabel)
3686 if (! redirect_exp (&PATTERN (jump), olabel, nlabel, jump))
3689 /* If this is an unconditional branch, delete it from the jump_chain of
3690 OLABEL and add it to the jump_chain of NLABEL (assuming both labels
3691 have UID's in range and JUMP_CHAIN is valid). */
3692 if (jump_chain && (simplejump_p (jump)
3693 || GET_CODE (PATTERN (jump)) == RETURN))
3695 int label_index = nlabel ? INSN_UID (nlabel) : 0;
3697 delete_from_jump_chain (jump);
3698 if (label_index < max_jump_chain
3699 && INSN_UID (jump) < max_jump_chain)
3701 jump_chain[INSN_UID (jump)] = jump_chain[label_index];
3702 jump_chain[label_index] = jump;
3706 JUMP_LABEL (jump) = nlabel;
3708 ++LABEL_NUSES (nlabel);
3710 if (olabel && --LABEL_NUSES (olabel) == 0)
3711 delete_insn (olabel);
3716 /* Delete the instruction JUMP from any jump chain it might be on. */
3719 delete_from_jump_chain (jump)
3723 rtx olabel = JUMP_LABEL (jump);
3725 /* Handle unconditional jumps. */
3726 if (jump_chain && olabel != 0
3727 && INSN_UID (olabel) < max_jump_chain
3728 && simplejump_p (jump))
3729 index = INSN_UID (olabel);
3730 /* Handle return insns. */
3731 else if (jump_chain && GET_CODE (PATTERN (jump)) == RETURN)
3735 if (jump_chain[index] == jump)
3736 jump_chain[index] = jump_chain[INSN_UID (jump)];
3741 for (insn = jump_chain[index];
3743 insn = jump_chain[INSN_UID (insn)])
3744 if (jump_chain[INSN_UID (insn)] == jump)
3746 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (jump)];
3752 /* If NLABEL is nonzero, throughout the rtx at LOC,
3753 alter (LABEL_REF OLABEL) to (LABEL_REF NLABEL). If OLABEL is
3754 zero, alter (RETURN) to (LABEL_REF NLABEL).
3756 If NLABEL is zero, alter (LABEL_REF OLABEL) to (RETURN) and check
3757 validity with validate_change. Convert (set (pc) (label_ref olabel))
3760 Return 0 if we found a change we would like to make but it is invalid.
3761 Otherwise, return 1. */
3764 redirect_exp (loc, olabel, nlabel, insn)
3769 register rtx x = *loc;
3770 register RTX_CODE code = GET_CODE (x);
3774 if (code == LABEL_REF)
3776 if (XEXP (x, 0) == olabel)
3779 XEXP (x, 0) = nlabel;
3781 return validate_change (insn, loc, gen_rtx (RETURN, VOIDmode), 0);
3785 else if (code == RETURN && olabel == 0)
3787 x = gen_rtx (LABEL_REF, VOIDmode, nlabel);
3788 if (loc == &PATTERN (insn))
3789 x = gen_rtx (SET, VOIDmode, pc_rtx, x);
3790 return validate_change (insn, loc, x, 0);
3793 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
3794 && GET_CODE (SET_SRC (x)) == LABEL_REF
3795 && XEXP (SET_SRC (x), 0) == olabel)
3796 return validate_change (insn, loc, gen_rtx (RETURN, VOIDmode), 0);
3798 fmt = GET_RTX_FORMAT (code);
3799 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3802 if (! redirect_exp (&XEXP (x, i), olabel, nlabel, insn))
3807 for (j = 0; j < XVECLEN (x, i); j++)
3808 if (! redirect_exp (&XVECEXP (x, i, j), olabel, nlabel, insn))
3816 /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
3818 If the old jump target label (before the dispatch table) becomes unused,
3819 it and the dispatch table may be deleted. In that case, find the insn
3820 before the jump references that label and delete it and logical successors
3824 redirect_tablejump (jump, nlabel)
3827 register rtx olabel = JUMP_LABEL (jump);
3829 /* Add this jump to the jump_chain of NLABEL. */
3830 if (jump_chain && INSN_UID (nlabel) < max_jump_chain
3831 && INSN_UID (jump) < max_jump_chain)
3833 jump_chain[INSN_UID (jump)] = jump_chain[INSN_UID (nlabel)];
3834 jump_chain[INSN_UID (nlabel)] = jump;
3837 PATTERN (jump) = gen_jump (nlabel);
3838 JUMP_LABEL (jump) = nlabel;
3839 ++LABEL_NUSES (nlabel);
3840 INSN_CODE (jump) = -1;
3842 if (--LABEL_NUSES (olabel) == 0)
3844 delete_labelref_insn (jump, olabel, 0);
3845 delete_insn (olabel);
3849 /* Find the insn referencing LABEL that is a logical predecessor of INSN.
3850 If we found one, delete it and then delete this insn if DELETE_THIS is
3851 non-zero. Return non-zero if INSN or a predecessor references LABEL. */
3854 delete_labelref_insn (insn, label, delete_this)
3861 if (GET_CODE (insn) != NOTE
3862 && reg_mentioned_p (label, PATTERN (insn)))
3873 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
3874 if (delete_labelref_insn (XEXP (link, 0), label, 1))
3888 /* Like rtx_equal_p except that it considers two REGs as equal
3889 if they renumber to the same value and considers two commutative
3890 operations to be the same if the order of the operands has been
3894 rtx_renumbered_equal_p (x, y)
3898 register RTX_CODE code = GET_CODE (x);
3904 if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
3905 && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
3906 && GET_CODE (SUBREG_REG (y)) == REG)))
3908 int reg_x = -1, reg_y = -1;
3909 int word_x = 0, word_y = 0;
3911 if (GET_MODE (x) != GET_MODE (y))
3914 /* If we haven't done any renumbering, don't
3915 make any assumptions. */
3916 if (reg_renumber == 0)
3917 return rtx_equal_p (x, y);
3921 reg_x = REGNO (SUBREG_REG (x));
3922 word_x = SUBREG_WORD (x);
3924 if (reg_renumber[reg_x] >= 0)
3926 reg_x = reg_renumber[reg_x] + word_x;
3934 if (reg_renumber[reg_x] >= 0)
3935 reg_x = reg_renumber[reg_x];
3938 if (GET_CODE (y) == SUBREG)
3940 reg_y = REGNO (SUBREG_REG (y));
3941 word_y = SUBREG_WORD (y);
3943 if (reg_renumber[reg_y] >= 0)
3945 reg_y = reg_renumber[reg_y];
3953 if (reg_renumber[reg_y] >= 0)
3954 reg_y = reg_renumber[reg_y];
3957 return reg_x >= 0 && reg_x == reg_y && word_x == word_y;
3960 /* Now we have disposed of all the cases
3961 in which different rtx codes can match. */
3962 if (code != GET_CODE (y))
3974 return INTVAL (x) == INTVAL (y);
3977 /* We can't assume nonlocal labels have their following insns yet. */
3978 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
3979 return XEXP (x, 0) == XEXP (y, 0);
3981 /* Two label-refs are equivalent if they point at labels
3982 in the same position in the instruction stream. */
3983 return (next_real_insn (XEXP (x, 0))
3984 == next_real_insn (XEXP (y, 0)));
3987 return XSTR (x, 0) == XSTR (y, 0);
3990 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
3992 if (GET_MODE (x) != GET_MODE (y))
3995 /* For commutative operations, the RTX match if the operand match in any
3996 order. Also handle the simple binary and unary cases without a loop. */
3997 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
3998 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
3999 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
4000 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
4001 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
4002 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4003 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
4004 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
4005 else if (GET_RTX_CLASS (code) == '1')
4006 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
4008 /* Compare the elements. If any pair of corresponding elements
4009 fail to match, return 0 for the whole things. */
4011 fmt = GET_RTX_FORMAT (code);
4012 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4018 if (XWINT (x, i) != XWINT (y, i))
4023 if (XINT (x, i) != XINT (y, i))
4028 if (strcmp (XSTR (x, i), XSTR (y, i)))
4033 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
4038 if (XEXP (x, i) != XEXP (y, i))
4045 if (XVECLEN (x, i) != XVECLEN (y, i))
4047 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4048 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
4059 /* If X is a hard register or equivalent to one or a subregister of one,
4060 return the hard register number. If X is a pseudo register that was not
4061 assigned a hard register, return the pseudo register number. Otherwise,
4062 return -1. Any rtx is valid for X. */
4068 if (GET_CODE (x) == REG)
4070 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
4071 return reg_renumber[REGNO (x)];
4074 if (GET_CODE (x) == SUBREG)
4076 int base = true_regnum (SUBREG_REG (x));
4077 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
4078 return SUBREG_WORD (x) + base;
4083 /* Optimize code of the form:
4085 for (x = a[i]; x; ...)
4087 for (x = a[i]; x; ...)
4091 Loop optimize will change the above code into
4095 { ...; if (! (x = ...)) break; }
4098 { ...; if (! (x = ...)) break; }
4101 In general, if the first test fails, the program can branch
4102 directly to `foo' and skip the second try which is doomed to fail.
4103 We run this after loop optimization and before flow analysis. */
4105 /* When comparing the insn patterns, we track the fact that different
4106 pseudo-register numbers may have been used in each computation.
4107 The following array stores an equivalence -- same_regs[I] == J means
4108 that pseudo register I was used in the first set of tests in a context
4109 where J was used in the second set. We also count the number of such
4110 pending equivalences. If nonzero, the expressions really aren't the
4113 static int *same_regs;
4115 static int num_same_regs;
4117 /* Track any registers modified between the target of the first jump and
4118 the second jump. They never compare equal. */
4120 static char *modified_regs;
4122 /* Record if memory was modified. */
4124 static int modified_mem;
4126 /* Called via note_stores on each insn between the target of the first
4127 branch and the second branch. It marks any changed registers. */
4130 mark_modified_reg (dest, x)
4136 if (GET_CODE (dest) == SUBREG)
4137 dest = SUBREG_REG (dest);
4139 if (GET_CODE (dest) == MEM)
4142 if (GET_CODE (dest) != REG)
4145 regno = REGNO (dest);
4146 if (regno >= FIRST_PSEUDO_REGISTER)
4147 modified_regs[regno] = 1;
4149 for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
4150 modified_regs[regno + i] = 1;
4153 /* F is the first insn in the chain of insns. */
4156 thread_jumps (f, max_reg, flag_before_loop)
4159 int flag_before_loop;
4161 /* Basic algorithm is to find a conditional branch,
4162 the label it may branch to, and the branch after
4163 that label. If the two branches test the same condition,
4164 walk back from both branch paths until the insn patterns
4165 differ, or code labels are hit. If we make it back to
4166 the target of the first branch, then we know that the first branch
4167 will either always succeed or always fail depending on the relative
4168 senses of the two branches. So adjust the first branch accordingly
4171 rtx label, b1, b2, t1, t2;
4172 enum rtx_code code1, code2;
4173 rtx b1op0, b1op1, b2op0, b2op1;
4178 /* Allocate register tables and quick-reset table. */
4179 modified_regs = (char *) alloca (max_reg * sizeof (char));
4180 same_regs = (int *) alloca (max_reg * sizeof (int));
4181 all_reset = (int *) alloca (max_reg * sizeof (int));
4182 for (i = 0; i < max_reg; i++)
4189 for (b1 = f; b1; b1 = NEXT_INSN (b1))
4191 /* Get to a candidate branch insn. */
4192 if (GET_CODE (b1) != JUMP_INSN
4193 || ! condjump_p (b1) || simplejump_p (b1)
4194 || JUMP_LABEL (b1) == 0)
4197 bzero (modified_regs, max_reg * sizeof (char));
4200 bcopy ((char *) all_reset, (char *) same_regs,
4201 max_reg * sizeof (int));
4204 label = JUMP_LABEL (b1);
4206 /* Look for a branch after the target. Record any registers and
4207 memory modified between the target and the branch. Stop when we
4208 get to a label since we can't know what was changed there. */
4209 for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
4211 if (GET_CODE (b2) == CODE_LABEL)
4214 else if (GET_CODE (b2) == JUMP_INSN)
4216 /* If this is an unconditional jump and is the only use of
4217 its target label, we can follow it. */
4218 if (simplejump_p (b2)
4219 && JUMP_LABEL (b2) != 0
4220 && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
4222 b2 = JUMP_LABEL (b2);
4229 if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
4232 if (GET_CODE (b2) == CALL_INSN)
4235 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4236 if (call_used_regs[i] && ! fixed_regs[i]
4237 && i != STACK_POINTER_REGNUM
4238 && i != FRAME_POINTER_REGNUM
4239 && i != HARD_FRAME_POINTER_REGNUM
4240 && i != ARG_POINTER_REGNUM)
4241 modified_regs[i] = 1;
4244 note_stores (PATTERN (b2), mark_modified_reg);
4247 /* Check the next candidate branch insn from the label
4250 || GET_CODE (b2) != JUMP_INSN
4252 || ! condjump_p (b2)
4253 || simplejump_p (b2))
4256 /* Get the comparison codes and operands, reversing the
4257 codes if appropriate. If we don't have comparison codes,
4258 we can't do anything. */
4259 b1op0 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 0);
4260 b1op1 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 1);
4261 code1 = GET_CODE (XEXP (SET_SRC (PATTERN (b1)), 0));
4262 if (XEXP (SET_SRC (PATTERN (b1)), 1) == pc_rtx)
4263 code1 = reverse_condition (code1);
4265 b2op0 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 0);
4266 b2op1 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 1);
4267 code2 = GET_CODE (XEXP (SET_SRC (PATTERN (b2)), 0));
4268 if (XEXP (SET_SRC (PATTERN (b2)), 1) == pc_rtx)
4269 code2 = reverse_condition (code2);
4271 /* If they test the same things and knowing that B1 branches
4272 tells us whether or not B2 branches, check if we
4273 can thread the branch. */
4274 if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
4275 && rtx_equal_for_thread_p (b1op1, b2op1, b2)
4276 && (comparison_dominates_p (code1, code2)
4277 || comparison_dominates_p (code1, reverse_condition (code2))))
4279 t1 = prev_nonnote_insn (b1);
4280 t2 = prev_nonnote_insn (b2);
4282 while (t1 != 0 && t2 != 0)
4286 /* We have reached the target of the first branch.
4287 If there are no pending register equivalents,
4288 we know that this branch will either always
4289 succeed (if the senses of the two branches are
4290 the same) or always fail (if not). */
4293 if (num_same_regs != 0)
4296 if (comparison_dominates_p (code1, code2))
4297 new_label = JUMP_LABEL (b2);
4299 new_label = get_label_after (b2);
4301 if (JUMP_LABEL (b1) != new_label)
4303 rtx prev = PREV_INSN (new_label);
4305 if (flag_before_loop
4306 && NOTE_LINE_NUMBER (prev) == NOTE_INSN_LOOP_BEG)
4308 /* Don't thread to the loop label. If a loop
4309 label is reused, loop optimization will
4310 be disabled for that loop. */
4311 new_label = gen_label_rtx ();
4312 emit_label_after (new_label, PREV_INSN (prev));
4314 changed |= redirect_jump (b1, new_label);
4319 /* If either of these is not a normal insn (it might be
4320 a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail. (NOTEs
4321 have already been skipped above.) Similarly, fail
4322 if the insns are different. */
4323 if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
4324 || recog_memoized (t1) != recog_memoized (t2)
4325 || ! rtx_equal_for_thread_p (PATTERN (t1),
4329 t1 = prev_nonnote_insn (t1);
4330 t2 = prev_nonnote_insn (t2);
4337 /* This is like RTX_EQUAL_P except that it knows about our handling of
4338 possibly equivalent registers and knows to consider volatile and
4339 modified objects as not equal.
4341 YINSN is the insn containing Y. */
4344 rtx_equal_for_thread_p (x, y, yinsn)
4350 register enum rtx_code code;
4353 code = GET_CODE (x);
4354 /* Rtx's of different codes cannot be equal. */
4355 if (code != GET_CODE (y))
4358 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
4359 (REG:SI x) and (REG:HI x) are NOT equivalent. */
4361 if (GET_MODE (x) != GET_MODE (y))
4364 /* For commutative operations, the RTX match if the operand match in any
4365 order. Also handle the simple binary and unary cases without a loop. */
4366 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
4367 return ((rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4368 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn))
4369 || (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 1), yinsn)
4370 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 0), yinsn)));
4371 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4372 return (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4373 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn));
4374 else if (GET_RTX_CLASS (code) == '1')
4375 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4377 /* Handle special-cases first. */
4381 if (REGNO (x) == REGNO (y) && ! modified_regs[REGNO (x)])
4384 /* If neither is user variable or hard register, check for possible
4386 if (REG_USERVAR_P (x) || REG_USERVAR_P (y)
4387 || REGNO (x) < FIRST_PSEUDO_REGISTER
4388 || REGNO (y) < FIRST_PSEUDO_REGISTER)
4391 if (same_regs[REGNO (x)] == -1)
4393 same_regs[REGNO (x)] = REGNO (y);
4396 /* If this is the first time we are seeing a register on the `Y'
4397 side, see if it is the last use. If not, we can't thread the
4398 jump, so mark it as not equivalent. */
4399 if (regno_last_uid[REGNO (y)] != INSN_UID (yinsn))
4405 return (same_regs[REGNO (x)] == REGNO (y));
4410 /* If memory modified or either volatile, not equivalent.
4411 Else, check address. */
4412 if (modified_mem || MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4415 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4418 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4424 /* Cancel a pending `same_regs' if setting equivalenced registers.
4425 Then process source. */
4426 if (GET_CODE (SET_DEST (x)) == REG
4427 && GET_CODE (SET_DEST (y)) == REG)
4429 if (same_regs[REGNO (SET_DEST (x))] == REGNO (SET_DEST (y)))
4431 same_regs[REGNO (SET_DEST (x))] = -1;
4434 else if (REGNO (SET_DEST (x)) != REGNO (SET_DEST (y)))
4438 if (rtx_equal_for_thread_p (SET_DEST (x), SET_DEST (y), yinsn) == 0)
4441 return rtx_equal_for_thread_p (SET_SRC (x), SET_SRC (y), yinsn);
4444 return XEXP (x, 0) == XEXP (y, 0);
4447 return XSTR (x, 0) == XSTR (y, 0);
4453 fmt = GET_RTX_FORMAT (code);
4454 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4459 if (XWINT (x, i) != XWINT (y, i))
4465 if (XINT (x, i) != XINT (y, i))
4471 /* Two vectors must have the same length. */
4472 if (XVECLEN (x, i) != XVECLEN (y, i))
4475 /* And the corresponding elements must match. */
4476 for (j = 0; j < XVECLEN (x, i); j++)
4477 if (rtx_equal_for_thread_p (XVECEXP (x, i, j),
4478 XVECEXP (y, i, j), yinsn) == 0)
4483 if (rtx_equal_for_thread_p (XEXP (x, i), XEXP (y, i), yinsn) == 0)
4489 if (strcmp (XSTR (x, i), XSTR (y, i)))
4494 /* These are just backpointers, so they don't matter. */
4500 /* It is believed that rtx's at this level will never
4501 contain anything but integers and other rtx's,
4502 except for within LABEL_REFs and SYMBOL_REFs. */