1 /* Optimize jump instructions, for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* This is the jump-optimization pass of the compiler.
23 It is run two or three times: once before cse, sometimes once after cse,
24 and once after reload (before final).
26 jump_optimize deletes unreachable code and labels that are not used.
27 It also deletes jumps that jump to the following insn,
28 and simplifies jumps around unconditional jumps and jumps
29 to unconditional jumps.
31 Each CODE_LABEL has a count of the times it is used
32 stored in the LABEL_NUSES internal field, and each JUMP_INSN
33 has one label that it refers to stored in the
34 JUMP_LABEL internal field. With this we can detect labels that
35 become unused because of the deletion of all the jumps that
36 formerly used them. The JUMP_LABEL info is sometimes looked
39 Jump optimization is done after cse when cse's constant-propagation
40 causes jumps to become unconditional or to be deleted.
42 Unreachable loops are not detected here, because the labels
43 have references and the insns appear reachable from the labels.
44 find_basic_blocks in flow.c finds and deletes such loops.
46 The subroutines delete_insn, redirect_jump, and invert_jump are used
47 from other passes as well. */
54 #include "hard-reg-set.h"
56 #include "insn-config.h"
57 #include "insn-attr.h"
67 /* ??? Eventually must record somehow the labels used by jumps
68 from nested functions. */
69 /* Pre-record the next or previous real insn for each label?
70 No, this pass is very fast anyway. */
71 /* Condense consecutive labels?
72 This would make life analysis faster, maybe. */
73 /* Optimize jump y; x: ... y: jumpif... x?
74 Don't know if it is worth bothering with. */
75 /* Optimize two cases of conditional jump to conditional jump?
76 This can never delete any instruction or make anything dead,
77 or even change what is live at any point.
78 So perhaps let combiner do it. */
80 /* Vector indexed by uid.
81 For each CODE_LABEL, index by its uid to get first unconditional jump
82 that jumps to the label.
83 For each JUMP_INSN, index by its uid to get the next unconditional jump
84 that jumps to the same label.
85 Element 0 is the start of a chain of all return insns.
86 (It is safe to use element 0 because insn uid 0 is not used. */
88 static rtx *jump_chain;
90 /* Maximum index in jump_chain. */
92 static int max_jump_chain;
94 static int init_label_info PARAMS ((rtx));
95 static void delete_barrier_successors PARAMS ((rtx));
96 static void mark_all_labels PARAMS ((rtx));
97 static rtx delete_unreferenced_labels PARAMS ((rtx));
98 static void delete_noop_moves PARAMS ((rtx));
99 static int duplicate_loop_exit_test PARAMS ((rtx));
100 static int tension_vector_labels PARAMS ((rtx, int));
101 static void delete_computation PARAMS ((rtx));
102 static void redirect_exp_1 PARAMS ((rtx *, rtx, rtx, rtx));
103 static int redirect_exp PARAMS ((rtx, rtx, rtx));
104 static void invert_exp_1 PARAMS ((rtx));
105 static int invert_exp PARAMS ((rtx));
106 static void delete_from_jump_chain PARAMS ((rtx));
107 static int delete_labelref_insn PARAMS ((rtx, rtx, int));
108 static void mark_modified_reg PARAMS ((rtx, rtx, void *));
109 static void redirect_tablejump PARAMS ((rtx, rtx));
110 static void jump_optimize_1 PARAMS ((rtx, int, int, int, int));
111 static int returnjump_p_1 PARAMS ((rtx *, void *));
112 static void delete_prior_computation PARAMS ((rtx, rtx));
114 /* Main external entry point into the jump optimizer. See comments before
115 jump_optimize_1 for descriptions of the arguments. */
117 jump_optimize (f, noop_moves, after_regscan)
122 jump_optimize_1 (f, noop_moves, after_regscan, 0, 0);
125 /* Alternate entry into the jump optimizer. This entry point only rebuilds
126 the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
129 rebuild_jump_labels (f)
132 jump_optimize_1 (f, 0, 0, 1, 0);
135 /* Alternate entry into the jump optimizer. Do only trivial optimizations. */
138 jump_optimize_minimal (f)
141 jump_optimize_1 (f, 0, 0, 0, 1);
144 /* Delete no-op jumps and optimize jumps to jumps
145 and jumps around jumps.
146 Delete unused labels and unreachable code.
148 If NOOP_MOVES is nonzero, delete no-op move insns.
150 If AFTER_REGSCAN is nonzero, then this jump pass is being run immediately
151 after regscan, and it is safe to use regno_first_uid and regno_last_uid.
153 If MARK_LABELS_ONLY is nonzero, then we only rebuild the jump chain
154 and JUMP_LABEL field for jumping insns.
156 If `optimize' is zero, don't change any code,
157 just determine whether control drops off the end of the function.
158 This case occurs when we have -W and not -O.
159 It works because `delete_insn' checks the value of `optimize'
160 and refrains from actually deleting when that is 0.
162 If MINIMAL is nonzero, then we only perform trivial optimizations:
164 * Removal of unreachable code after BARRIERs.
165 * Removal of unreferenced CODE_LABELs.
166 * Removal of a jump to the next instruction.
167 * Removal of a conditional jump followed by an unconditional jump
168 to the same target as the conditional jump.
169 * Simplify a conditional jump around an unconditional jump.
170 * Simplify a jump to a jump.
171 * Delete extraneous line number notes.
175 jump_optimize_1 (f, noop_moves, after_regscan,
176 mark_labels_only, minimal)
180 int mark_labels_only;
183 register rtx insn, next;
190 max_uid = init_label_info (f) + 1;
192 /* Leave some extra room for labels and duplicate exit test insns
194 max_jump_chain = max_uid * 14 / 10;
195 jump_chain = (rtx *) xcalloc (max_jump_chain, sizeof (rtx));
199 /* Keep track of labels used from static data; we don't track them
200 closely enough to delete them here, so make sure their reference
201 count doesn't drop to zero. */
203 for (insn = forced_labels; insn; insn = XEXP (insn, 1))
204 if (GET_CODE (XEXP (insn, 0)) == CODE_LABEL)
205 LABEL_NUSES (XEXP (insn, 0))++;
207 /* Keep track of labels used for marking handlers for exception
208 regions; they cannot usually be deleted. */
210 for (insn = exception_handler_labels; insn; insn = XEXP (insn, 1))
211 if (GET_CODE (XEXP (insn, 0)) == CODE_LABEL)
212 LABEL_NUSES (XEXP (insn, 0))++;
214 /* Quit now if we just wanted to rebuild the JUMP_LABEL and REG_LABEL
215 notes and recompute LABEL_NUSES. */
216 if (mark_labels_only)
219 delete_barrier_successors (f);
221 last_insn = delete_unreferenced_labels (f);
224 delete_noop_moves (f);
226 /* Now iterate optimizing jumps until nothing changes over one pass. */
228 old_max_reg = max_reg_num ();
233 for (insn = f; insn; insn = next)
236 rtx temp, temp1, temp2 = NULL_RTX;
237 rtx temp4 ATTRIBUTE_UNUSED;
239 int this_is_any_uncondjump;
240 int this_is_any_condjump;
241 int this_is_onlyjump;
243 next = NEXT_INSN (insn);
245 /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
246 jump. Try to optimize by duplicating the loop exit test if so.
247 This is only safe immediately after regscan, because it uses
248 the values of regno_first_uid and regno_last_uid. */
249 if (after_regscan && GET_CODE (insn) == NOTE
250 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
251 && (temp1 = next_nonnote_insn (insn)) != 0
252 && any_uncondjump_p (temp1)
253 && onlyjump_p (temp1))
255 temp = PREV_INSN (insn);
256 if (duplicate_loop_exit_test (insn))
259 next = NEXT_INSN (temp);
264 if (GET_CODE (insn) != JUMP_INSN)
267 this_is_any_condjump = any_condjump_p (insn);
268 this_is_any_uncondjump = any_uncondjump_p (insn);
269 this_is_onlyjump = onlyjump_p (insn);
271 /* Tension the labels in dispatch tables. */
273 if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
274 changed |= tension_vector_labels (PATTERN (insn), 0);
275 if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
276 changed |= tension_vector_labels (PATTERN (insn), 1);
278 /* See if this jump goes to another jump and redirect if so. */
279 nlabel = follow_jumps (JUMP_LABEL (insn));
280 if (nlabel != JUMP_LABEL (insn))
281 changed |= redirect_jump (insn, nlabel, 1);
283 if (! optimize || minimal)
286 /* If a dispatch table always goes to the same place,
287 get rid of it and replace the insn that uses it. */
289 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
290 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
293 rtx pat = PATTERN (insn);
294 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
295 int len = XVECLEN (pat, diff_vec_p);
296 rtx dispatch = prev_real_insn (insn);
299 for (i = 0; i < len; i++)
300 if (XEXP (XVECEXP (pat, diff_vec_p, i), 0)
301 != XEXP (XVECEXP (pat, diff_vec_p, 0), 0))
306 && GET_CODE (dispatch) == JUMP_INSN
307 && JUMP_LABEL (dispatch) != 0
308 /* Don't mess with a casesi insn.
309 XXX according to the comment before computed_jump_p(),
310 all casesi insns should be a parallel of the jump
311 and a USE of a LABEL_REF. */
312 && ! ((set = single_set (dispatch)) != NULL
313 && (GET_CODE (SET_SRC (set)) == IF_THEN_ELSE))
314 && next_real_insn (JUMP_LABEL (dispatch)) == insn)
316 redirect_tablejump (dispatch,
317 XEXP (XVECEXP (pat, diff_vec_p, 0), 0));
322 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
324 /* Detect jump to following insn. */
325 if (reallabelprev == insn
326 && (this_is_any_condjump || this_is_any_uncondjump)
329 next = next_real_insn (JUMP_LABEL (insn));
332 /* Remove the "inactive" but "real" insns (i.e. uses and
333 clobbers) in between here and there. */
335 while ((temp = next_real_insn (temp)) != next)
342 /* Detect a conditional jump going to the same place
343 as an immediately following unconditional jump. */
344 else if (this_is_any_condjump && this_is_onlyjump
345 && (temp = next_active_insn (insn)) != 0
346 && simplejump_p (temp)
347 && (next_active_insn (JUMP_LABEL (insn))
348 == next_active_insn (JUMP_LABEL (temp))))
350 /* Don't mess up test coverage analysis. */
352 if (flag_test_coverage && !reload_completed)
353 for (temp2 = insn; temp2 != temp; temp2 = NEXT_INSN (temp2))
354 if (GET_CODE (temp2) == NOTE && NOTE_LINE_NUMBER (temp2) > 0)
359 /* Ensure that we jump to the later of the two labels.
370 If we leave the goto L1, we'll incorrectly leave
371 return-reg dead for TEST true. */
373 temp2 = next_active_insn (JUMP_LABEL (insn));
375 temp2 = get_last_insn ();
376 if (GET_CODE (temp2) != CODE_LABEL)
377 temp2 = prev_label (temp2);
378 if (temp2 != JUMP_LABEL (temp))
379 redirect_jump (temp, temp2, 1);
387 /* Detect a conditional jump jumping over an unconditional jump. */
389 else if (this_is_any_condjump
390 && reallabelprev != 0
391 && GET_CODE (reallabelprev) == JUMP_INSN
392 && prev_active_insn (reallabelprev) == insn
393 && no_labels_between_p (insn, reallabelprev)
394 && any_uncondjump_p (reallabelprev)
395 && onlyjump_p (reallabelprev))
397 /* When we invert the unconditional jump, we will be
398 decrementing the usage count of its old label.
399 Make sure that we don't delete it now because that
400 might cause the following code to be deleted. */
401 rtx prev_uses = prev_nonnote_insn (reallabelprev);
402 rtx prev_label = JUMP_LABEL (insn);
405 ++LABEL_NUSES (prev_label);
407 if (invert_jump (insn, JUMP_LABEL (reallabelprev), 1))
409 /* It is very likely that if there are USE insns before
410 this jump, they hold REG_DEAD notes. These REG_DEAD
411 notes are no longer valid due to this optimization,
412 and will cause the life-analysis that following passes
413 (notably delayed-branch scheduling) to think that
414 these registers are dead when they are not.
416 To prevent this trouble, we just remove the USE insns
417 from the insn chain. */
419 while (prev_uses && GET_CODE (prev_uses) == INSN
420 && GET_CODE (PATTERN (prev_uses)) == USE)
422 rtx useless = prev_uses;
423 prev_uses = prev_nonnote_insn (prev_uses);
424 delete_insn (useless);
427 delete_insn (reallabelprev);
431 /* We can now safely delete the label if it is unreferenced
432 since the delete_insn above has deleted the BARRIER. */
433 if (prev_label && --LABEL_NUSES (prev_label) == 0)
434 delete_insn (prev_label);
436 next = NEXT_INSN (insn);
439 /* If we have an unconditional jump preceded by a USE, try to put
440 the USE before the target and jump there. This simplifies many
441 of the optimizations below since we don't have to worry about
442 dealing with these USE insns. We only do this if the label
443 being branch to already has the identical USE or if code
444 never falls through to that label. */
446 else if (this_is_any_uncondjump
447 && (temp = prev_nonnote_insn (insn)) != 0
448 && GET_CODE (temp) == INSN
449 && GET_CODE (PATTERN (temp)) == USE
450 && (temp1 = prev_nonnote_insn (JUMP_LABEL (insn))) != 0
451 && (GET_CODE (temp1) == BARRIER
452 || (GET_CODE (temp1) == INSN
453 && rtx_equal_p (PATTERN (temp), PATTERN (temp1))))
454 /* Don't do this optimization if we have a loop containing
455 only the USE instruction, and the loop start label has
456 a usage count of 1. This is because we will redo this
457 optimization everytime through the outer loop, and jump
458 opt will never exit. */
459 && ! ((temp2 = prev_nonnote_insn (temp)) != 0
460 && temp2 == JUMP_LABEL (insn)
461 && LABEL_NUSES (temp2) == 1))
463 if (GET_CODE (temp1) == BARRIER)
465 emit_insn_after (PATTERN (temp), temp1);
466 temp1 = NEXT_INSN (temp1);
470 redirect_jump (insn, get_label_before (temp1), 1);
471 reallabelprev = prev_real_insn (temp1);
473 next = NEXT_INSN (insn);
480 /* Delete extraneous line number notes.
481 Note that two consecutive notes for different lines are not really
482 extraneous. There should be some indication where that line belonged,
483 even if it became empty. */
488 for (insn = f; insn; insn = NEXT_INSN (insn))
489 if (GET_CODE (insn) == NOTE)
491 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
492 /* Any previous line note was for the prologue; gdb wants a new
493 note after the prologue even if it is for the same line. */
494 last_note = NULL_RTX;
495 else if (NOTE_LINE_NUMBER (insn) >= 0)
497 /* Delete this note if it is identical to previous note. */
499 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
500 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
517 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
518 notes whose labels don't occur in the insn any more. Returns the
519 largest INSN_UID found. */
527 for (insn = f; insn; insn = NEXT_INSN (insn))
529 if (GET_CODE (insn) == CODE_LABEL)
530 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
531 else if (GET_CODE (insn) == JUMP_INSN)
532 JUMP_LABEL (insn) = 0;
533 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
537 for (note = REG_NOTES (insn); note; note = next)
539 next = XEXP (note, 1);
540 if (REG_NOTE_KIND (note) == REG_LABEL
541 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
542 remove_note (insn, note);
545 if (INSN_UID (insn) > largest_uid)
546 largest_uid = INSN_UID (insn);
552 /* Delete insns following barriers, up to next label.
554 Also delete no-op jumps created by gcse. */
557 delete_barrier_successors (f)
563 for (insn = f; insn;)
565 if (GET_CODE (insn) == BARRIER)
567 insn = NEXT_INSN (insn);
569 never_reached_warning (insn);
571 while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
573 if (GET_CODE (insn) == JUMP_INSN)
575 /* Detect when we're deleting a tablejump; get rid of
576 the jump table as well. */
577 rtx next1 = next_nonnote_insn (insn);
578 rtx next2 = next1 ? next_nonnote_insn (next1) : 0;
579 if (next2 && GET_CODE (next1) == CODE_LABEL
580 && GET_CODE (next2) == JUMP_INSN
581 && (GET_CODE (PATTERN (next2)) == ADDR_VEC
582 || GET_CODE (PATTERN (next2)) == ADDR_DIFF_VEC))
588 insn = delete_insn (insn);
590 else if (GET_CODE (insn) == NOTE
591 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
592 insn = NEXT_INSN (insn);
594 insn = delete_insn (insn);
596 /* INSN is now the code_label. */
599 /* Also remove (set (pc) (pc)) insns which can be created by
600 gcse. We eliminate such insns now to avoid having them
601 cause problems later. */
602 else if (GET_CODE (insn) == JUMP_INSN
603 && (set = pc_set (insn)) != NULL
604 && SET_SRC (set) == pc_rtx
605 && SET_DEST (set) == pc_rtx
606 && onlyjump_p (insn))
607 insn = delete_insn (insn);
610 insn = NEXT_INSN (insn);
614 /* Mark the label each jump jumps to.
615 Combine consecutive labels, and count uses of labels.
617 For each label, make a chain (using `jump_chain')
618 of all the *unconditional* jumps that jump to it;
619 also make a chain of all returns. */
627 for (insn = f; insn; insn = NEXT_INSN (insn))
630 if (GET_CODE (insn) == CALL_INSN
631 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
633 mark_all_labels (XEXP (PATTERN (insn), 0));
634 mark_all_labels (XEXP (PATTERN (insn), 1));
635 mark_all_labels (XEXP (PATTERN (insn), 2));
637 /* Canonicalize the tail recursion label attached to the
638 CALL_PLACEHOLDER insn. */
639 if (XEXP (PATTERN (insn), 3))
641 rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
642 XEXP (PATTERN (insn), 3));
643 mark_jump_label (label_ref, insn, 0);
644 XEXP (PATTERN (insn), 3) = XEXP (label_ref, 0);
650 mark_jump_label (PATTERN (insn), insn, 0);
651 if (! INSN_DELETED_P (insn) && GET_CODE (insn) == JUMP_INSN)
653 /* When we know the LABEL_REF contained in a REG used in
654 an indirect jump, we'll have a REG_LABEL note so that
655 flow can tell where it's going. */
656 if (JUMP_LABEL (insn) == 0)
658 rtx label_note = find_reg_note (insn, REG_LABEL, NULL_RTX);
661 /* But a LABEL_REF around the REG_LABEL note, so
662 that we can canonicalize it. */
663 rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
664 XEXP (label_note, 0));
666 mark_jump_label (label_ref, insn, 0);
667 XEXP (label_note, 0) = XEXP (label_ref, 0);
668 JUMP_LABEL (insn) = XEXP (label_note, 0);
671 if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
673 jump_chain[INSN_UID (insn)]
674 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
675 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
677 if (GET_CODE (PATTERN (insn)) == RETURN)
679 jump_chain[INSN_UID (insn)] = jump_chain[0];
680 jump_chain[0] = insn;
686 /* Delete all labels already not referenced.
687 Also find and return the last insn. */
690 delete_unreferenced_labels (f)
693 rtx final = NULL_RTX;
696 for (insn = f; insn;)
698 if (GET_CODE (insn) == CODE_LABEL
699 && LABEL_NUSES (insn) == 0
700 && LABEL_ALTERNATE_NAME (insn) == NULL)
701 insn = delete_insn (insn);
705 insn = NEXT_INSN (insn);
712 /* Delete various simple forms of moves which have no necessary
716 delete_noop_moves (f)
721 for (insn = f; insn;)
723 next = NEXT_INSN (insn);
725 if (GET_CODE (insn) == INSN)
727 register rtx body = PATTERN (insn);
729 /* Detect and delete no-op move instructions
730 resulting from not allocating a parameter in a register. */
732 if (GET_CODE (body) == SET && set_noop_p (body))
733 delete_computation (insn);
735 /* Detect and ignore no-op move instructions
736 resulting from smart or fortuitous register allocation. */
738 else if (GET_CODE (body) == SET)
740 int sreg = true_regnum (SET_SRC (body));
741 int dreg = true_regnum (SET_DEST (body));
743 if (sreg == dreg && sreg >= 0)
745 else if (sreg >= 0 && dreg >= 0)
748 rtx tem = find_equiv_reg (NULL_RTX, insn, 0,
750 GET_MODE (SET_SRC (body)));
753 && GET_MODE (tem) == GET_MODE (SET_DEST (body)))
755 /* DREG may have been the target of a REG_DEAD note in
756 the insn which makes INSN redundant. If so, reorg
757 would still think it is dead. So search for such a
758 note and delete it if we find it. */
759 if (! find_regno_note (insn, REG_UNUSED, dreg))
760 for (trial = prev_nonnote_insn (insn);
761 trial && GET_CODE (trial) != CODE_LABEL;
762 trial = prev_nonnote_insn (trial))
763 if (find_regno_note (trial, REG_DEAD, dreg))
765 remove_death (dreg, trial);
769 /* Deleting insn could lose a death-note for SREG. */
770 if ((trial = find_regno_note (insn, REG_DEAD, sreg)))
772 /* Change this into a USE so that we won't emit
773 code for it, but still can keep the note. */
775 = gen_rtx_USE (VOIDmode, XEXP (trial, 0));
776 INSN_CODE (insn) = -1;
777 /* Remove all reg notes but the REG_DEAD one. */
778 REG_NOTES (insn) = trial;
779 XEXP (trial, 1) = NULL_RTX;
785 else if (dreg >= 0 && CONSTANT_P (SET_SRC (body))
786 && find_equiv_reg (SET_SRC (body), insn, 0, dreg,
787 NULL, 0, GET_MODE (SET_DEST (body))))
789 /* This handles the case where we have two consecutive
790 assignments of the same constant to pseudos that didn't
791 get a hard reg. Each SET from the constant will be
792 converted into a SET of the spill register and an
793 output reload will be made following it. This produces
794 two loads of the same constant into the same spill
799 /* Look back for a death note for the first reg.
800 If there is one, it is no longer accurate. */
801 while (in_insn && GET_CODE (in_insn) != CODE_LABEL)
803 if ((GET_CODE (in_insn) == INSN
804 || GET_CODE (in_insn) == JUMP_INSN)
805 && find_regno_note (in_insn, REG_DEAD, dreg))
807 remove_death (dreg, in_insn);
810 in_insn = PREV_INSN (in_insn);
813 /* Delete the second load of the value. */
817 else if (GET_CODE (body) == PARALLEL)
819 /* If each part is a set between two identical registers or
820 a USE or CLOBBER, delete the insn. */
824 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
826 tem = XVECEXP (body, 0, i);
827 if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
830 if (GET_CODE (tem) != SET
831 || (sreg = true_regnum (SET_SRC (tem))) < 0
832 || (dreg = true_regnum (SET_DEST (tem))) < 0
845 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
846 jump. Assume that this unconditional jump is to the exit test code. If
847 the code is sufficiently simple, make a copy of it before INSN,
848 followed by a jump to the exit of the loop. Then delete the unconditional
851 Return 1 if we made the change, else 0.
853 This is only safe immediately after a regscan pass because it uses the
854 values of regno_first_uid and regno_last_uid. */
857 duplicate_loop_exit_test (loop_start)
860 rtx insn, set, reg, p, link;
861 rtx copy = 0, first_copy = 0;
863 rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
865 int max_reg = max_reg_num ();
868 /* Scan the exit code. We do not perform this optimization if any insn:
872 has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
873 is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
874 is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
877 We also do not do this if we find an insn with ASM_OPERANDS. While
878 this restriction should not be necessary, copying an insn with
879 ASM_OPERANDS can confuse asm_noperands in some cases.
881 Also, don't do this if the exit code is more than 20 insns. */
883 for (insn = exitcode;
885 && ! (GET_CODE (insn) == NOTE
886 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
887 insn = NEXT_INSN (insn))
889 switch (GET_CODE (insn))
895 /* We could be in front of the wrong NOTE_INSN_LOOP_END if there is
896 a jump immediately after the loop start that branches outside
897 the loop but within an outer loop, near the exit test.
898 If we copied this exit test and created a phony
899 NOTE_INSN_LOOP_VTOP, this could make instructions immediately
900 before the exit test look like these could be safely moved
901 out of the loop even if they actually may be never executed.
902 This can be avoided by checking here for NOTE_INSN_LOOP_CONT. */
904 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
905 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
909 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
910 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
911 /* If we were to duplicate this code, we would not move
912 the BLOCK notes, and so debugging the moved code would
913 be difficult. Thus, we only move the code with -O2 or
920 /* The code below would grossly mishandle REG_WAS_0 notes,
921 so get rid of them here. */
922 while ((p = find_reg_note (insn, REG_WAS_0, NULL_RTX)) != 0)
923 remove_note (insn, p);
925 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
926 || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
934 /* Unless INSN is zero, we can do the optimization. */
940 /* See if any insn sets a register only used in the loop exit code and
941 not a user variable. If so, replace it with a new register. */
942 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
943 if (GET_CODE (insn) == INSN
944 && (set = single_set (insn)) != 0
945 && ((reg = SET_DEST (set), GET_CODE (reg) == REG)
946 || (GET_CODE (reg) == SUBREG
947 && (reg = SUBREG_REG (reg), GET_CODE (reg) == REG)))
948 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
949 && REGNO_FIRST_UID (REGNO (reg)) == INSN_UID (insn))
951 for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
952 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (p))
957 /* We can do the replacement. Allocate reg_map if this is the
958 first replacement we found. */
960 reg_map = (rtx *) xcalloc (max_reg, sizeof (rtx));
962 REG_LOOP_TEST_P (reg) = 1;
964 reg_map[REGNO (reg)] = gen_reg_rtx (GET_MODE (reg));
968 /* Now copy each insn. */
969 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
971 switch (GET_CODE (insn))
974 copy = emit_barrier_before (loop_start);
977 /* Only copy line-number notes. */
978 if (NOTE_LINE_NUMBER (insn) >= 0)
980 copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
981 NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
986 copy = emit_insn_before (copy_insn (PATTERN (insn)), loop_start);
988 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
990 mark_jump_label (PATTERN (copy), copy, 0);
992 /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
994 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
995 if (REG_NOTE_KIND (link) != REG_LABEL)
997 if (GET_CODE (link) == EXPR_LIST)
999 = copy_insn_1 (gen_rtx_EXPR_LIST (REG_NOTE_KIND (link),
1004 = copy_insn_1 (gen_rtx_INSN_LIST (REG_NOTE_KIND (link),
1009 if (reg_map && REG_NOTES (copy))
1010 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
1014 copy = emit_jump_insn_before (copy_insn (PATTERN (insn)),
1017 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
1018 mark_jump_label (PATTERN (copy), copy, 0);
1019 if (REG_NOTES (insn))
1021 REG_NOTES (copy) = copy_insn_1 (REG_NOTES (insn));
1023 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
1026 /* Predict conditional jump that do make loop looping as taken.
1027 Other jumps are probably exit conditions, so predict
1029 if (any_condjump_p (copy))
1031 rtx label = JUMP_LABEL (copy);
1034 /* The jump_insn after loop_start should be followed
1035 by barrier and loopback label. */
1036 if (prev_nonnote_insn (label)
1037 && (PREV_INSN (prev_nonnote_insn (label))
1038 == NEXT_INSN (loop_start)))
1039 predict_insn_def (copy, PRED_LOOP_HEADER, TAKEN);
1041 predict_insn_def (copy, PRED_LOOP_HEADER, NOT_TAKEN);
1044 /* If this is a simple jump, add it to the jump chain. */
1046 if (INSN_UID (copy) < max_jump_chain && JUMP_LABEL (copy)
1047 && simplejump_p (copy))
1049 jump_chain[INSN_UID (copy)]
1050 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
1051 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
1059 /* Record the first insn we copied. We need it so that we can
1060 scan the copied insns for new pseudo registers. */
1065 /* Now clean up by emitting a jump to the end label and deleting the jump
1066 at the start of the loop. */
1067 if (! copy || GET_CODE (copy) != BARRIER)
1069 copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
1072 /* Record the first insn we copied. We need it so that we can
1073 scan the copied insns for new pseudo registers. This may not
1074 be strictly necessary since we should have copied at least one
1075 insn above. But I am going to be safe. */
1079 mark_jump_label (PATTERN (copy), copy, 0);
1080 if (INSN_UID (copy) < max_jump_chain
1081 && INSN_UID (JUMP_LABEL (copy)) < max_jump_chain)
1083 jump_chain[INSN_UID (copy)]
1084 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
1085 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
1087 emit_barrier_before (loop_start);
1090 /* Now scan from the first insn we copied to the last insn we copied
1091 (copy) for new pseudo registers. Do this after the code to jump to
1092 the end label since that might create a new pseudo too. */
1093 reg_scan_update (first_copy, copy, max_reg);
1095 /* Mark the exit code as the virtual top of the converted loop. */
1096 emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
1098 delete_insn (next_nonnote_insn (loop_start));
1107 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
1108 notes between START and END out before START. Assume that END is not
1109 such a note. START may be such a note. Returns the value of the new
1110 starting insn, which may be different if the original start was such a
1114 squeeze_notes (start, end)
1120 for (insn = start; insn != end; insn = next)
1122 next = NEXT_INSN (insn);
1123 if (GET_CODE (insn) == NOTE
1124 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
1125 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
1126 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
1127 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
1128 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
1129 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
1135 rtx prev = PREV_INSN (insn);
1136 PREV_INSN (insn) = PREV_INSN (start);
1137 NEXT_INSN (insn) = start;
1138 NEXT_INSN (PREV_INSN (insn)) = insn;
1139 PREV_INSN (NEXT_INSN (insn)) = insn;
1140 NEXT_INSN (prev) = next;
1141 PREV_INSN (next) = prev;
1149 /* Return the label before INSN, or put a new label there. */
1152 get_label_before (insn)
1157 /* Find an existing label at this point
1158 or make a new one if there is none. */
1159 label = prev_nonnote_insn (insn);
1161 if (label == 0 || GET_CODE (label) != CODE_LABEL)
1163 rtx prev = PREV_INSN (insn);
1165 label = gen_label_rtx ();
1166 emit_label_after (label, prev);
1167 LABEL_NUSES (label) = 0;
1172 /* Return the label after INSN, or put a new label there. */
1175 get_label_after (insn)
1180 /* Find an existing label at this point
1181 or make a new one if there is none. */
1182 label = next_nonnote_insn (insn);
1184 if (label == 0 || GET_CODE (label) != CODE_LABEL)
1186 label = gen_label_rtx ();
1187 emit_label_after (label, insn);
1188 LABEL_NUSES (label) = 0;
1193 /* Given a comparison (CODE ARG0 ARG1), inside an insn, INSN, return a code
1194 of reversed comparison if it is possible to do so. Otherwise return UNKNOWN.
1195 UNKNOWN may be returned in case we are having CC_MODE compare and we don't
1196 know whether it's source is floating point or integer comparison. Machine
1197 description should define REVERSIBLE_CC_MODE and REVERSE_CONDITION macros
1198 to help this function avoid overhead in these cases. */
1200 reversed_comparison_code_parts (code, arg0, arg1, insn)
1201 rtx insn, arg0, arg1;
1204 enum machine_mode mode;
1206 /* If this is not actually a comparison, we can't reverse it. */
1207 if (GET_RTX_CLASS (code) != '<')
1210 mode = GET_MODE (arg0);
1211 if (mode == VOIDmode)
1212 mode = GET_MODE (arg1);
1214 /* First see if machine description supply us way to reverse the comparison.
1215 Give it priority over everything else to allow machine description to do
1217 #ifdef REVERSIBLE_CC_MODE
1218 if (GET_MODE_CLASS (mode) == MODE_CC
1219 && REVERSIBLE_CC_MODE (mode))
1221 #ifdef REVERSE_CONDITION
1222 return REVERSE_CONDITION (code, mode);
1224 return reverse_condition (code);
1228 /* Try a few special cases based on the comparison code. */
1237 /* It is always safe to reverse EQ and NE, even for the floating
1238 point. Similary the unsigned comparisons are never used for
1239 floating point so we can reverse them in the default way. */
1240 return reverse_condition (code);
1245 /* In case we already see unordered comparison, we can be sure to
1246 be dealing with floating point so we don't need any more tests. */
1247 return reverse_condition_maybe_unordered (code);
1252 /* We don't have safe way to reverse these yet. */
1258 /* In case we give up IEEE compatibility, all comparisons are reversible. */
1259 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1260 || flag_unsafe_math_optimizations)
1261 return reverse_condition (code);
1263 if (GET_MODE_CLASS (mode) == MODE_CC
1270 /* Try to search for the comparison to determine the real mode.
1271 This code is expensive, but with sane machine description it
1272 will be never used, since REVERSIBLE_CC_MODE will return true
1277 for (prev = prev_nonnote_insn (insn);
1278 prev != 0 && GET_CODE (prev) != CODE_LABEL;
1279 prev = prev_nonnote_insn (prev))
1281 rtx set = set_of (arg0, prev);
1282 if (set && GET_CODE (set) == SET
1283 && rtx_equal_p (SET_DEST (set), arg0))
1285 rtx src = SET_SRC (set);
1287 if (GET_CODE (src) == COMPARE)
1289 rtx comparison = src;
1290 arg0 = XEXP (src, 0);
1291 mode = GET_MODE (arg0);
1292 if (mode == VOIDmode)
1293 mode = GET_MODE (XEXP (comparison, 1));
1296 /* We can get past reg-reg moves. This may be usefull for model
1297 of i387 comparisons that first move flag registers around. */
1304 /* If register is clobbered in some ununderstandable way,
1311 /* An integer condition. */
1312 if (GET_CODE (arg0) == CONST_INT
1313 || (GET_MODE (arg0) != VOIDmode
1314 && GET_MODE_CLASS (mode) != MODE_CC
1315 && ! FLOAT_MODE_P (mode)))
1316 return reverse_condition (code);
1321 /* An wrapper around the previous function to take COMPARISON as rtx
1322 expression. This simplifies many callers. */
1324 reversed_comparison_code (comparison, insn)
1325 rtx comparison, insn;
1327 if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
1329 return reversed_comparison_code_parts (GET_CODE (comparison),
1330 XEXP (comparison, 0),
1331 XEXP (comparison, 1), insn);
1334 /* Given an rtx-code for a comparison, return the code for the negated
1335 comparison. If no such code exists, return UNKNOWN.
1337 WATCH OUT! reverse_condition is not safe to use on a jump that might
1338 be acting on the results of an IEEE floating point comparison, because
1339 of the special treatment of non-signaling nans in comparisons.
1340 Use reversed_comparison_code instead. */
1343 reverse_condition (code)
1386 /* Similar, but we're allowed to generate unordered comparisons, which
1387 makes it safe for IEEE floating-point. Of course, we have to recognize
1388 that the target will support them too... */
1391 reverse_condition_maybe_unordered (code)
1394 /* Non-IEEE formats don't have unordered conditions. */
1395 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
1396 return reverse_condition (code);
1434 /* Similar, but return the code when two operands of a comparison are swapped.
1435 This IS safe for IEEE floating-point. */
1438 swap_condition (code)
1481 /* Given a comparison CODE, return the corresponding unsigned comparison.
1482 If CODE is an equality comparison or already an unsigned comparison,
1483 CODE is returned. */
1486 unsigned_condition (code)
1513 /* Similarly, return the signed version of a comparison. */
1516 signed_condition (code)
1543 /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
1544 truth of CODE1 implies the truth of CODE2. */
1547 comparison_dominates_p (code1, code2)
1548 enum rtx_code code1, code2;
1550 /* UNKNOWN comparison codes can happen as a result of trying to revert
1552 They can't match anything, so we have to reject them here. */
1553 if (code1 == UNKNOWN || code2 == UNKNOWN)
1562 if (code2 == UNLE || code2 == UNGE)
1567 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU
1568 || code2 == ORDERED)
1573 if (code2 == UNLE || code2 == NE)
1578 if (code2 == LE || code2 == NE || code2 == ORDERED || code2 == LTGT)
1583 if (code2 == UNGE || code2 == NE)
1588 if (code2 == GE || code2 == NE || code2 == ORDERED || code2 == LTGT)
1594 if (code2 == ORDERED)
1599 if (code2 == NE || code2 == ORDERED)
1604 if (code2 == LEU || code2 == NE)
1609 if (code2 == GEU || code2 == NE)
1614 if (code2 == NE || code2 == UNEQ || code2 == UNLE || code2 == UNLT
1615 || code2 == UNGE || code2 == UNGT)
1626 /* Return 1 if INSN is an unconditional jump and nothing else. */
1632 return (GET_CODE (insn) == JUMP_INSN
1633 && GET_CODE (PATTERN (insn)) == SET
1634 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
1635 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
1638 /* Return nonzero if INSN is a (possibly) conditional jump
1641 Use this function is deprecated, since we need to support combined
1642 branch and compare insns. Use any_condjump_p instead whenever possible. */
1648 register rtx x = PATTERN (insn);
1650 if (GET_CODE (x) != SET
1651 || GET_CODE (SET_DEST (x)) != PC)
1655 if (GET_CODE (x) == LABEL_REF)
1658 return (GET_CODE (x) == IF_THEN_ELSE
1659 && ((GET_CODE (XEXP (x, 2)) == PC
1660 && (GET_CODE (XEXP (x, 1)) == LABEL_REF
1661 || GET_CODE (XEXP (x, 1)) == RETURN))
1662 || (GET_CODE (XEXP (x, 1)) == PC
1663 && (GET_CODE (XEXP (x, 2)) == LABEL_REF
1664 || GET_CODE (XEXP (x, 2)) == RETURN))));
1669 /* Return nonzero if INSN is a (possibly) conditional jump inside a
1672 Use this function is deprecated, since we need to support combined
1673 branch and compare insns. Use any_condjump_p instead whenever possible. */
1676 condjump_in_parallel_p (insn)
1679 register rtx x = PATTERN (insn);
1681 if (GET_CODE (x) != PARALLEL)
1684 x = XVECEXP (x, 0, 0);
1686 if (GET_CODE (x) != SET)
1688 if (GET_CODE (SET_DEST (x)) != PC)
1690 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
1692 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
1694 if (XEXP (SET_SRC (x), 2) == pc_rtx
1695 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
1696 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
1698 if (XEXP (SET_SRC (x), 1) == pc_rtx
1699 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
1700 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
1705 /* Return set of PC, otherwise NULL. */
1712 if (GET_CODE (insn) != JUMP_INSN)
1714 pat = PATTERN (insn);
1716 /* The set is allowed to appear either as the insn pattern or
1717 the first set in a PARALLEL. */
1718 if (GET_CODE (pat) == PARALLEL)
1719 pat = XVECEXP (pat, 0, 0);
1720 if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == PC)
1726 /* Return true when insn is an unconditional direct jump,
1727 possibly bundled inside a PARALLEL. */
1730 any_uncondjump_p (insn)
1733 rtx x = pc_set (insn);
1736 if (GET_CODE (SET_SRC (x)) != LABEL_REF)
1741 /* Return true when insn is a conditional jump. This function works for
1742 instructions containing PC sets in PARALLELs. The instruction may have
1743 various other effects so before removing the jump you must verify
1746 Note that unlike condjump_p it returns false for unconditional jumps. */
1749 any_condjump_p (insn)
1752 rtx x = pc_set (insn);
1757 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
1760 a = GET_CODE (XEXP (SET_SRC (x), 1));
1761 b = GET_CODE (XEXP (SET_SRC (x), 2));
1763 return ((b == PC && (a == LABEL_REF || a == RETURN))
1764 || (a == PC && (b == LABEL_REF || b == RETURN)));
1767 /* Return the label of a conditional jump. */
1770 condjump_label (insn)
1773 rtx x = pc_set (insn);
1778 if (GET_CODE (x) == LABEL_REF)
1780 if (GET_CODE (x) != IF_THEN_ELSE)
1782 if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
1784 if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
1789 /* Return true if INSN is a (possibly conditional) return insn. */
1792 returnjump_p_1 (loc, data)
1794 void *data ATTRIBUTE_UNUSED;
1797 return x && GET_CODE (x) == RETURN;
1804 if (GET_CODE (insn) != JUMP_INSN)
1806 return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
1809 /* Return true if INSN is a jump that only transfers control and
1818 if (GET_CODE (insn) != JUMP_INSN)
1821 set = single_set (insn);
1824 if (GET_CODE (SET_DEST (set)) != PC)
1826 if (side_effects_p (SET_SRC (set)))
1834 /* Return 1 if X is an RTX that does nothing but set the condition codes
1835 and CLOBBER or USE registers.
1836 Return -1 if X does explicitly set the condition codes,
1837 but also does other things. */
1841 rtx x ATTRIBUTE_UNUSED;
1843 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
1845 if (GET_CODE (x) == PARALLEL)
1849 int other_things = 0;
1850 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1852 if (GET_CODE (XVECEXP (x, 0, i)) == SET
1853 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
1855 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
1858 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
1864 /* Follow any unconditional jump at LABEL;
1865 return the ultimate label reached by any such chain of jumps.
1866 If LABEL is not followed by a jump, return LABEL.
1867 If the chain loops or we can't find end, return LABEL,
1868 since that tells caller to avoid changing the insn.
1870 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
1871 a USE or CLOBBER. */
1874 follow_jumps (label)
1879 register rtx value = label;
1884 && (insn = next_active_insn (value)) != 0
1885 && GET_CODE (insn) == JUMP_INSN
1886 && ((JUMP_LABEL (insn) != 0 && any_uncondjump_p (insn)
1887 && onlyjump_p (insn))
1888 || GET_CODE (PATTERN (insn)) == RETURN)
1889 && (next = NEXT_INSN (insn))
1890 && GET_CODE (next) == BARRIER);
1893 /* Don't chain through the insn that jumps into a loop
1894 from outside the loop,
1895 since that would create multiple loop entry jumps
1896 and prevent loop optimization. */
1898 if (!reload_completed)
1899 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
1900 if (GET_CODE (tem) == NOTE
1901 && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
1902 /* ??? Optional. Disables some optimizations, but makes
1903 gcov output more accurate with -O. */
1904 || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
1907 /* If we have found a cycle, make the insn jump to itself. */
1908 if (JUMP_LABEL (insn) == label)
1911 tem = next_active_insn (JUMP_LABEL (insn));
1912 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
1913 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
1916 value = JUMP_LABEL (insn);
1923 /* Assuming that field IDX of X is a vector of label_refs,
1924 replace each of them by the ultimate label reached by it.
1925 Return nonzero if a change is made.
1926 If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG. */
1929 tension_vector_labels (x, idx)
1935 for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
1937 register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
1938 register rtx nlabel = follow_jumps (olabel);
1939 if (nlabel && nlabel != olabel)
1941 XEXP (XVECEXP (x, idx, i), 0) = nlabel;
1942 ++LABEL_NUSES (nlabel);
1943 if (--LABEL_NUSES (olabel) == 0)
1944 delete_insn (olabel);
1951 /* Find all CODE_LABELs referred to in X, and increment their use counts.
1952 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
1953 in INSN, then store one of them in JUMP_LABEL (INSN).
1954 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
1955 referenced in INSN, add a REG_LABEL note containing that label to INSN.
1956 Also, when there are consecutive labels, canonicalize on the last of them.
1958 Note that two labels separated by a loop-beginning note
1959 must be kept distinct if we have not yet done loop-optimization,
1960 because the gap between them is where loop-optimize
1961 will want to move invariant code to. CROSS_JUMP tells us
1962 that loop-optimization is done with. */
1965 mark_jump_label (x, insn, in_mem)
1970 register RTX_CODE code = GET_CODE (x);
1972 register const char *fmt;
1994 /* If this is a constant-pool reference, see if it is a label. */
1995 if (CONSTANT_POOL_ADDRESS_P (x))
1996 mark_jump_label (get_pool_constant (x), insn, in_mem);
2001 rtx label = XEXP (x, 0);
2003 /* Ignore remaining references to unreachable labels that
2004 have been deleted. */
2005 if (GET_CODE (label) == NOTE
2006 && NOTE_LINE_NUMBER (label) == NOTE_INSN_DELETED_LABEL)
2009 if (GET_CODE (label) != CODE_LABEL)
2012 /* Ignore references to labels of containing functions. */
2013 if (LABEL_REF_NONLOCAL_P (x))
2016 XEXP (x, 0) = label;
2017 if (! insn || ! INSN_DELETED_P (insn))
2018 ++LABEL_NUSES (label);
2022 if (GET_CODE (insn) == JUMP_INSN)
2023 JUMP_LABEL (insn) = label;
2026 /* Add a REG_LABEL note for LABEL unless there already
2027 is one. All uses of a label, except for labels
2028 that are the targets of jumps, must have a
2030 if (! find_reg_note (insn, REG_LABEL, label))
2031 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
2038 /* Do walk the labels in a vector, but not the first operand of an
2039 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
2042 if (! INSN_DELETED_P (insn))
2044 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
2046 for (i = 0; i < XVECLEN (x, eltnum); i++)
2047 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, in_mem);
2055 fmt = GET_RTX_FORMAT (code);
2056 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2059 mark_jump_label (XEXP (x, i), insn, in_mem);
2060 else if (fmt[i] == 'E')
2063 for (j = 0; j < XVECLEN (x, i); j++)
2064 mark_jump_label (XVECEXP (x, i, j), insn, in_mem);
2069 /* If all INSN does is set the pc, delete it,
2070 and delete the insn that set the condition codes for it
2071 if that's what the previous thing was. */
2077 register rtx set = single_set (insn);
2079 if (set && GET_CODE (SET_DEST (set)) == PC)
2080 delete_computation (insn);
2083 /* Verify INSN is a BARRIER and delete it. */
2086 delete_barrier (insn)
2089 if (GET_CODE (insn) != BARRIER)
2095 /* Recursively delete prior insns that compute the value (used only by INSN
2096 which the caller is deleting) stored in the register mentioned by NOTE
2097 which is a REG_DEAD note associated with INSN. */
2100 delete_prior_computation (note, insn)
2105 rtx reg = XEXP (note, 0);
2107 for (our_prev = prev_nonnote_insn (insn);
2108 our_prev && (GET_CODE (our_prev) == INSN
2109 || GET_CODE (our_prev) == CALL_INSN);
2110 our_prev = prev_nonnote_insn (our_prev))
2112 rtx pat = PATTERN (our_prev);
2114 /* If we reach a CALL which is not calling a const function
2115 or the callee pops the arguments, then give up. */
2116 if (GET_CODE (our_prev) == CALL_INSN
2117 && (! CONST_CALL_P (our_prev)
2118 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
2121 /* If we reach a SEQUENCE, it is too complex to try to
2122 do anything with it, so give up. */
2123 if (GET_CODE (pat) == SEQUENCE)
2126 if (GET_CODE (pat) == USE
2127 && GET_CODE (XEXP (pat, 0)) == INSN)
2128 /* reorg creates USEs that look like this. We leave them
2129 alone because reorg needs them for its own purposes. */
2132 if (reg_set_p (reg, pat))
2134 if (side_effects_p (pat) && GET_CODE (our_prev) != CALL_INSN)
2137 if (GET_CODE (pat) == PARALLEL)
2139 /* If we find a SET of something else, we can't
2144 for (i = 0; i < XVECLEN (pat, 0); i++)
2146 rtx part = XVECEXP (pat, 0, i);
2148 if (GET_CODE (part) == SET
2149 && SET_DEST (part) != reg)
2153 if (i == XVECLEN (pat, 0))
2154 delete_computation (our_prev);
2156 else if (GET_CODE (pat) == SET
2157 && GET_CODE (SET_DEST (pat)) == REG)
2159 int dest_regno = REGNO (SET_DEST (pat));
2162 + (dest_regno < FIRST_PSEUDO_REGISTER
2163 ? HARD_REGNO_NREGS (dest_regno,
2164 GET_MODE (SET_DEST (pat))) : 1));
2165 int regno = REGNO (reg);
2168 + (regno < FIRST_PSEUDO_REGISTER
2169 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1));
2171 if (dest_regno >= regno
2172 && dest_endregno <= endregno)
2173 delete_computation (our_prev);
2175 /* We may have a multi-word hard register and some, but not
2176 all, of the words of the register are needed in subsequent
2177 insns. Write REG_UNUSED notes for those parts that were not
2179 else if (dest_regno <= regno
2180 && dest_endregno >= endregno)
2184 REG_NOTES (our_prev)
2185 = gen_rtx_EXPR_LIST (REG_UNUSED, reg,
2186 REG_NOTES (our_prev));
2188 for (i = dest_regno; i < dest_endregno; i++)
2189 if (! find_regno_note (our_prev, REG_UNUSED, i))
2192 if (i == dest_endregno)
2193 delete_computation (our_prev);
2200 /* If PAT references the register that dies here, it is an
2201 additional use. Hence any prior SET isn't dead. However, this
2202 insn becomes the new place for the REG_DEAD note. */
2203 if (reg_overlap_mentioned_p (reg, pat))
2205 XEXP (note, 1) = REG_NOTES (our_prev);
2206 REG_NOTES (our_prev) = note;
2212 /* Delete INSN and recursively delete insns that compute values used only
2213 by INSN. This uses the REG_DEAD notes computed during flow analysis.
2214 If we are running before flow.c, we need do nothing since flow.c will
2215 delete dead code. We also can't know if the registers being used are
2216 dead or not at this point.
2218 Otherwise, look at all our REG_DEAD notes. If a previous insn does
2219 nothing other than set a register that dies in this insn, we can delete
2222 On machines with CC0, if CC0 is used in this insn, we may be able to
2223 delete the insn that set it. */
2226 delete_computation (insn)
2232 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
2234 rtx prev = prev_nonnote_insn (insn);
2235 /* We assume that at this stage
2236 CC's are always set explicitly
2237 and always immediately before the jump that
2238 will use them. So if the previous insn
2239 exists to set the CC's, delete it
2240 (unless it performs auto-increments, etc.). */
2241 if (prev && GET_CODE (prev) == INSN
2242 && sets_cc0_p (PATTERN (prev)))
2244 if (sets_cc0_p (PATTERN (prev)) > 0
2245 && ! side_effects_p (PATTERN (prev)))
2246 delete_computation (prev);
2248 /* Otherwise, show that cc0 won't be used. */
2249 REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
2250 cc0_rtx, REG_NOTES (prev));
2255 for (note = REG_NOTES (insn); note; note = next)
2257 next = XEXP (note, 1);
2259 if (REG_NOTE_KIND (note) != REG_DEAD
2260 /* Verify that the REG_NOTE is legitimate. */
2261 || GET_CODE (XEXP (note, 0)) != REG)
2264 delete_prior_computation (note, insn);
2270 /* Delete insn INSN from the chain of insns and update label ref counts.
2271 May delete some following insns as a consequence; may even delete
2272 a label elsewhere and insns that follow it.
2274 Returns the first insn after INSN that was not deleted. */
2280 register rtx next = NEXT_INSN (insn);
2281 register rtx prev = PREV_INSN (insn);
2282 register int was_code_label = (GET_CODE (insn) == CODE_LABEL);
2283 register int dont_really_delete = 0;
2286 while (next && INSN_DELETED_P (next))
2287 next = NEXT_INSN (next);
2289 /* This insn is already deleted => return first following nondeleted. */
2290 if (INSN_DELETED_P (insn))
2294 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
2296 /* Don't delete user-declared labels. When optimizing, convert them
2297 to special NOTEs instead. When not optimizing, leave them alone. */
2298 if (was_code_label && LABEL_NAME (insn) != 0)
2302 const char *name = LABEL_NAME (insn);
2303 PUT_CODE (insn, NOTE);
2304 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
2305 NOTE_SOURCE_FILE (insn) = name;
2308 dont_really_delete = 1;
2311 /* Mark this insn as deleted. */
2312 INSN_DELETED_P (insn) = 1;
2314 /* If this is an unconditional jump, delete it from the jump chain. */
2315 if (simplejump_p (insn))
2316 delete_from_jump_chain (insn);
2318 /* If instruction is followed by a barrier,
2319 delete the barrier too. */
2321 if (next != 0 && GET_CODE (next) == BARRIER)
2323 INSN_DELETED_P (next) = 1;
2324 next = NEXT_INSN (next);
2327 /* Patch out INSN (and the barrier if any) */
2329 if (! dont_really_delete)
2333 NEXT_INSN (prev) = next;
2334 if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
2335 NEXT_INSN (XVECEXP (PATTERN (prev), 0,
2336 XVECLEN (PATTERN (prev), 0) - 1)) = next;
2341 PREV_INSN (next) = prev;
2342 if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
2343 PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
2346 if (prev && NEXT_INSN (prev) == 0)
2347 set_last_insn (prev);
2350 /* If deleting a jump, decrement the count of the label,
2351 and delete the label if it is now unused. */
2353 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
2355 rtx lab = JUMP_LABEL (insn), lab_next;
2357 if (--LABEL_NUSES (lab) == 0)
2359 /* This can delete NEXT or PREV,
2360 either directly if NEXT is JUMP_LABEL (INSN),
2361 or indirectly through more levels of jumps. */
2364 /* I feel a little doubtful about this loop,
2365 but I see no clean and sure alternative way
2366 to find the first insn after INSN that is not now deleted.
2367 I hope this works. */
2368 while (next && INSN_DELETED_P (next))
2369 next = NEXT_INSN (next);
2372 else if ((lab_next = next_nonnote_insn (lab)) != NULL
2373 && GET_CODE (lab_next) == JUMP_INSN
2374 && (GET_CODE (PATTERN (lab_next)) == ADDR_VEC
2375 || GET_CODE (PATTERN (lab_next)) == ADDR_DIFF_VEC))
2377 /* If we're deleting the tablejump, delete the dispatch table.
2378 We may not be able to kill the label immediately preceeding
2379 just yet, as it might be referenced in code leading up to
2381 delete_insn (lab_next);
2385 /* Likewise if we're deleting a dispatch table. */
2387 if (GET_CODE (insn) == JUMP_INSN
2388 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
2389 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
2391 rtx pat = PATTERN (insn);
2392 int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
2393 int len = XVECLEN (pat, diff_vec_p);
2395 for (i = 0; i < len; i++)
2396 if (--LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
2397 delete_insn (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
2398 while (next && INSN_DELETED_P (next))
2399 next = NEXT_INSN (next);
2403 /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note. */
2404 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2405 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2406 if (REG_NOTE_KIND (note) == REG_LABEL
2407 /* This could also be a NOTE_INSN_DELETED_LABEL note. */
2408 && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2409 if (--LABEL_NUSES (XEXP (note, 0)) == 0)
2410 delete_insn (XEXP (note, 0));
2412 while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
2413 prev = PREV_INSN (prev);
2415 /* If INSN was a label and a dispatch table follows it,
2416 delete the dispatch table. The tablejump must have gone already.
2417 It isn't useful to fall through into a table. */
2420 && NEXT_INSN (insn) != 0
2421 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
2422 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
2423 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
2424 next = delete_insn (NEXT_INSN (insn));
2426 /* If INSN was a label, delete insns following it if now unreachable. */
2428 if (was_code_label && prev && GET_CODE (prev) == BARRIER)
2430 register RTX_CODE code;
2432 && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
2433 || code == NOTE || code == BARRIER
2434 || (code == CODE_LABEL && INSN_DELETED_P (next))))
2437 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
2438 next = NEXT_INSN (next);
2439 /* Keep going past other deleted labels to delete what follows. */
2440 else if (code == CODE_LABEL && INSN_DELETED_P (next))
2441 next = NEXT_INSN (next);
2443 /* Note: if this deletes a jump, it can cause more
2444 deletion of unreachable code, after a different label.
2445 As long as the value from this recursive call is correct,
2446 this invocation functions correctly. */
2447 next = delete_insn (next);
2454 /* Advance from INSN till reaching something not deleted
2455 then return that. May return INSN itself. */
2458 next_nondeleted_insn (insn)
2461 while (INSN_DELETED_P (insn))
2462 insn = NEXT_INSN (insn);
2466 /* Delete a range of insns from FROM to TO, inclusive.
2467 This is for the sake of peephole optimization, so assume
2468 that whatever these insns do will still be done by a new
2469 peephole insn that will replace them. */
2472 delete_for_peephole (from, to)
2473 register rtx from, to;
2475 register rtx insn = from;
2479 register rtx next = NEXT_INSN (insn);
2480 register rtx prev = PREV_INSN (insn);
2482 if (GET_CODE (insn) != NOTE)
2484 INSN_DELETED_P (insn) = 1;
2486 /* Patch this insn out of the chain. */
2487 /* We don't do this all at once, because we
2488 must preserve all NOTEs. */
2490 NEXT_INSN (prev) = next;
2493 PREV_INSN (next) = prev;
2501 /* Note that if TO is an unconditional jump
2502 we *do not* delete the BARRIER that follows,
2503 since the peephole that replaces this sequence
2504 is also an unconditional jump in that case. */
2507 /* We have determined that INSN is never reached, and are about to
2508 delete it. Print a warning if the user asked for one.
2510 To try to make this warning more useful, this should only be called
2511 once per basic block not reached, and it only warns when the basic
2512 block contains more than one line from the current function, and
2513 contains at least one operation. CSE and inlining can duplicate insns,
2514 so it's possible to get spurious warnings from this. */
2517 never_reached_warning (avoided_insn)
2521 rtx a_line_note = NULL;
2522 int two_avoided_lines = 0;
2523 int contains_insn = 0;
2525 if (! warn_notreached)
2528 /* Scan forwards, looking at LINE_NUMBER notes, until
2529 we hit a LABEL or we run out of insns. */
2531 for (insn = avoided_insn; insn != NULL; insn = NEXT_INSN (insn))
2533 if (GET_CODE (insn) == CODE_LABEL)
2535 else if (GET_CODE (insn) == NOTE /* A line number note? */
2536 && NOTE_LINE_NUMBER (insn) >= 0)
2538 if (a_line_note == NULL)
2541 two_avoided_lines |= (NOTE_LINE_NUMBER (a_line_note)
2542 != NOTE_LINE_NUMBER (insn));
2544 else if (INSN_P (insn))
2547 if (two_avoided_lines && contains_insn)
2548 warning_with_file_and_line (NOTE_SOURCE_FILE (a_line_note),
2549 NOTE_LINE_NUMBER (a_line_note),
2550 "will never be executed");
2553 /* Throughout LOC, redirect OLABEL to NLABEL. Treat null OLABEL or
2554 NLABEL as a return. Accrue modifications into the change group. */
2557 redirect_exp_1 (loc, olabel, nlabel, insn)
2562 register rtx x = *loc;
2563 register RTX_CODE code = GET_CODE (x);
2565 register const char *fmt;
2567 if (code == LABEL_REF)
2569 if (XEXP (x, 0) == olabel)
2573 n = gen_rtx_LABEL_REF (VOIDmode, nlabel);
2575 n = gen_rtx_RETURN (VOIDmode);
2577 validate_change (insn, loc, n, 1);
2581 else if (code == RETURN && olabel == 0)
2583 x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
2584 if (loc == &PATTERN (insn))
2585 x = gen_rtx_SET (VOIDmode, pc_rtx, x);
2586 validate_change (insn, loc, x, 1);
2590 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
2591 && GET_CODE (SET_SRC (x)) == LABEL_REF
2592 && XEXP (SET_SRC (x), 0) == olabel)
2594 validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 1);
2598 fmt = GET_RTX_FORMAT (code);
2599 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2602 redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
2603 else if (fmt[i] == 'E')
2606 for (j = 0; j < XVECLEN (x, i); j++)
2607 redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
2612 /* Similar, but apply the change group and report success or failure. */
2615 redirect_exp (olabel, nlabel, insn)
2621 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2622 loc = &XVECEXP (PATTERN (insn), 0, 0);
2624 loc = &PATTERN (insn);
2626 redirect_exp_1 (loc, olabel, nlabel, insn);
2627 if (num_validated_changes () == 0)
2630 return apply_change_group ();
2633 /* Make JUMP go to NLABEL instead of where it jumps now. Accrue
2634 the modifications into the change group. Return false if we did
2635 not see how to do that. */
2638 redirect_jump_1 (jump, nlabel)
2641 int ochanges = num_validated_changes ();
2644 if (GET_CODE (PATTERN (jump)) == PARALLEL)
2645 loc = &XVECEXP (PATTERN (jump), 0, 0);
2647 loc = &PATTERN (jump);
2649 redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
2650 return num_validated_changes () > ochanges;
2653 /* Make JUMP go to NLABEL instead of where it jumps now. If the old
2654 jump target label is unused as a result, it and the code following
2657 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
2660 The return value will be 1 if the change was made, 0 if it wasn't
2661 (this can only occur for NLABEL == 0). */
2664 redirect_jump (jump, nlabel, delete_unused)
2668 register rtx olabel = JUMP_LABEL (jump);
2670 if (nlabel == olabel)
2673 if (! redirect_exp (olabel, nlabel, jump))
2676 /* If this is an unconditional branch, delete it from the jump_chain of
2677 OLABEL and add it to the jump_chain of NLABEL (assuming both labels
2678 have UID's in range and JUMP_CHAIN is valid). */
2679 if (jump_chain && (simplejump_p (jump)
2680 || GET_CODE (PATTERN (jump)) == RETURN))
2682 int label_index = nlabel ? INSN_UID (nlabel) : 0;
2684 delete_from_jump_chain (jump);
2685 if (label_index < max_jump_chain
2686 && INSN_UID (jump) < max_jump_chain)
2688 jump_chain[INSN_UID (jump)] = jump_chain[label_index];
2689 jump_chain[label_index] = jump;
2693 JUMP_LABEL (jump) = nlabel;
2695 ++LABEL_NUSES (nlabel);
2697 /* If we're eliding the jump over exception cleanups at the end of a
2698 function, move the function end note so that -Wreturn-type works. */
2699 if (olabel && nlabel
2700 && NEXT_INSN (olabel)
2701 && GET_CODE (NEXT_INSN (olabel)) == NOTE
2702 && NOTE_LINE_NUMBER (NEXT_INSN (olabel)) == NOTE_INSN_FUNCTION_END)
2703 emit_note_after (NOTE_INSN_FUNCTION_END, nlabel);
2705 if (olabel && --LABEL_NUSES (olabel) == 0 && delete_unused)
2706 delete_insn (olabel);
2711 /* Invert the jump condition of rtx X contained in jump insn, INSN.
2712 Accrue the modifications into the change group. */
2718 register RTX_CODE code;
2719 rtx x = pc_set (insn);
2725 code = GET_CODE (x);
2727 if (code == IF_THEN_ELSE)
2729 register rtx comp = XEXP (x, 0);
2731 enum rtx_code reversed_code;
2733 /* We can do this in two ways: The preferable way, which can only
2734 be done if this is not an integer comparison, is to reverse
2735 the comparison code. Otherwise, swap the THEN-part and ELSE-part
2736 of the IF_THEN_ELSE. If we can't do either, fail. */
2738 reversed_code = reversed_comparison_code (comp, insn);
2740 if (reversed_code != UNKNOWN)
2742 validate_change (insn, &XEXP (x, 0),
2743 gen_rtx_fmt_ee (reversed_code,
2744 GET_MODE (comp), XEXP (comp, 0),
2751 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
2752 validate_change (insn, &XEXP (x, 2), tem, 1);
2758 /* Invert the jump condition of conditional jump insn, INSN.
2760 Return 1 if we can do so, 0 if we cannot find a way to do so that
2761 matches a pattern. */
2767 invert_exp_1 (insn);
2768 if (num_validated_changes () == 0)
2771 return apply_change_group ();
2774 /* Invert the condition of the jump JUMP, and make it jump to label
2775 NLABEL instead of where it jumps now. Accrue changes into the
2776 change group. Return false if we didn't see how to perform the
2777 inversion and redirection. */
2780 invert_jump_1 (jump, nlabel)
2785 ochanges = num_validated_changes ();
2786 invert_exp_1 (jump);
2787 if (num_validated_changes () == ochanges)
2790 return redirect_jump_1 (jump, nlabel);
2793 /* Invert the condition of the jump JUMP, and make it jump to label
2794 NLABEL instead of where it jumps now. Return true if successful. */
2797 invert_jump (jump, nlabel, delete_unused)
2801 /* We have to either invert the condition and change the label or
2802 do neither. Either operation could fail. We first try to invert
2803 the jump. If that succeeds, we try changing the label. If that fails,
2804 we invert the jump back to what it was. */
2806 if (! invert_exp (jump))
2809 if (redirect_jump (jump, nlabel, delete_unused))
2811 invert_br_probabilities (jump);
2816 if (! invert_exp (jump))
2817 /* This should just be putting it back the way it was. */
2823 /* Delete the instruction JUMP from any jump chain it might be on. */
2826 delete_from_jump_chain (jump)
2830 rtx olabel = JUMP_LABEL (jump);
2832 /* Handle unconditional jumps. */
2833 if (jump_chain && olabel != 0
2834 && INSN_UID (olabel) < max_jump_chain
2835 && simplejump_p (jump))
2836 index = INSN_UID (olabel);
2837 /* Handle return insns. */
2838 else if (jump_chain && GET_CODE (PATTERN (jump)) == RETURN)
2843 if (jump_chain[index] == jump)
2844 jump_chain[index] = jump_chain[INSN_UID (jump)];
2849 for (insn = jump_chain[index];
2851 insn = jump_chain[INSN_UID (insn)])
2852 if (jump_chain[INSN_UID (insn)] == jump)
2854 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (jump)];
2860 /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
2862 If the old jump target label (before the dispatch table) becomes unused,
2863 it and the dispatch table may be deleted. In that case, find the insn
2864 before the jump references that label and delete it and logical successors
2868 redirect_tablejump (jump, nlabel)
2871 register rtx olabel = JUMP_LABEL (jump);
2872 rtx *notep, note, next;
2874 /* Add this jump to the jump_chain of NLABEL. */
2875 if (jump_chain && INSN_UID (nlabel) < max_jump_chain
2876 && INSN_UID (jump) < max_jump_chain)
2878 jump_chain[INSN_UID (jump)] = jump_chain[INSN_UID (nlabel)];
2879 jump_chain[INSN_UID (nlabel)] = jump;
2882 for (notep = ®_NOTES (jump), note = *notep; note; note = next)
2884 next = XEXP (note, 1);
2886 if (REG_NOTE_KIND (note) != REG_DEAD
2887 /* Verify that the REG_NOTE is legitimate. */
2888 || GET_CODE (XEXP (note, 0)) != REG
2889 || ! reg_mentioned_p (XEXP (note, 0), PATTERN (jump)))
2890 notep = &XEXP (note, 1);
2893 delete_prior_computation (note, jump);
2898 PATTERN (jump) = gen_jump (nlabel);
2899 JUMP_LABEL (jump) = nlabel;
2900 ++LABEL_NUSES (nlabel);
2901 INSN_CODE (jump) = -1;
2903 if (--LABEL_NUSES (olabel) == 0)
2905 delete_labelref_insn (jump, olabel, 0);
2906 delete_insn (olabel);
2910 /* Find the insn referencing LABEL that is a logical predecessor of INSN.
2911 If we found one, delete it and then delete this insn if DELETE_THIS is
2912 non-zero. Return non-zero if INSN or a predecessor references LABEL. */
2915 delete_labelref_insn (insn, label, delete_this)
2922 if (GET_CODE (insn) != NOTE
2923 && reg_mentioned_p (label, PATTERN (insn)))
2934 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
2935 if (delete_labelref_insn (XEXP (link, 0), label, 1))
2949 /* Like rtx_equal_p except that it considers two REGs as equal
2950 if they renumber to the same value and considers two commutative
2951 operations to be the same if the order of the operands has been
2954 ??? Addition is not commutative on the PA due to the weird implicit
2955 space register selection rules for memory addresses. Therefore, we
2956 don't consider a + b == b + a.
2958 We could/should make this test a little tighter. Possibly only
2959 disabling it on the PA via some backend macro or only disabling this
2960 case when the PLUS is inside a MEM. */
2963 rtx_renumbered_equal_p (x, y)
2967 register RTX_CODE code = GET_CODE (x);
2968 register const char *fmt;
2973 if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
2974 && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
2975 && GET_CODE (SUBREG_REG (y)) == REG)))
2977 int reg_x = -1, reg_y = -1;
2978 int byte_x = 0, byte_y = 0;
2980 if (GET_MODE (x) != GET_MODE (y))
2983 /* If we haven't done any renumbering, don't
2984 make any assumptions. */
2985 if (reg_renumber == 0)
2986 return rtx_equal_p (x, y);
2990 reg_x = REGNO (SUBREG_REG (x));
2991 byte_x = SUBREG_BYTE (x);
2993 if (reg_renumber[reg_x] >= 0)
2995 reg_x = subreg_regno_offset (reg_renumber[reg_x],
2996 GET_MODE (SUBREG_REG (x)),
3005 if (reg_renumber[reg_x] >= 0)
3006 reg_x = reg_renumber[reg_x];
3009 if (GET_CODE (y) == SUBREG)
3011 reg_y = REGNO (SUBREG_REG (y));
3012 byte_y = SUBREG_BYTE (y);
3014 if (reg_renumber[reg_y] >= 0)
3016 reg_y = subreg_regno_offset (reg_renumber[reg_y],
3017 GET_MODE (SUBREG_REG (y)),
3026 if (reg_renumber[reg_y] >= 0)
3027 reg_y = reg_renumber[reg_y];
3030 return reg_x >= 0 && reg_x == reg_y && byte_x == byte_y;
3033 /* Now we have disposed of all the cases
3034 in which different rtx codes can match. */
3035 if (code != GET_CODE (y))
3047 return INTVAL (x) == INTVAL (y);
3050 /* We can't assume nonlocal labels have their following insns yet. */
3051 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
3052 return XEXP (x, 0) == XEXP (y, 0);
3054 /* Two label-refs are equivalent if they point at labels
3055 in the same position in the instruction stream. */
3056 return (next_real_insn (XEXP (x, 0))
3057 == next_real_insn (XEXP (y, 0)));
3060 return XSTR (x, 0) == XSTR (y, 0);
3063 /* If we didn't match EQ equality above, they aren't the same. */
3070 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
3072 if (GET_MODE (x) != GET_MODE (y))
3075 /* For commutative operations, the RTX match if the operand match in any
3076 order. Also handle the simple binary and unary cases without a loop.
3078 ??? Don't consider PLUS a commutative operator; see comments above. */
3079 if ((code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
3081 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
3082 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
3083 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
3084 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
3085 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
3086 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
3087 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
3088 else if (GET_RTX_CLASS (code) == '1')
3089 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
3091 /* Compare the elements. If any pair of corresponding elements
3092 fail to match, return 0 for the whole things. */
3094 fmt = GET_RTX_FORMAT (code);
3095 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3101 if (XWINT (x, i) != XWINT (y, i))
3106 if (XINT (x, i) != XINT (y, i))
3111 if (XTREE (x, i) != XTREE (y, i))
3116 if (strcmp (XSTR (x, i), XSTR (y, i)))
3121 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
3126 if (XEXP (x, i) != XEXP (y, i))
3133 if (XVECLEN (x, i) != XVECLEN (y, i))
3135 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3136 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
3147 /* If X is a hard register or equivalent to one or a subregister of one,
3148 return the hard register number. If X is a pseudo register that was not
3149 assigned a hard register, return the pseudo register number. Otherwise,
3150 return -1. Any rtx is valid for X. */
3156 if (GET_CODE (x) == REG)
3158 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
3159 return reg_renumber[REGNO (x)];
3162 if (GET_CODE (x) == SUBREG)
3164 int base = true_regnum (SUBREG_REG (x));
3165 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
3166 return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
3167 GET_MODE (SUBREG_REG (x)),
3168 SUBREG_BYTE (x), GET_MODE (x));
3173 /* Optimize code of the form:
3175 for (x = a[i]; x; ...)
3177 for (x = a[i]; x; ...)
3181 Loop optimize will change the above code into
3185 { ...; if (! (x = ...)) break; }
3188 { ...; if (! (x = ...)) break; }
3191 In general, if the first test fails, the program can branch
3192 directly to `foo' and skip the second try which is doomed to fail.
3193 We run this after loop optimization and before flow analysis. */
3195 /* When comparing the insn patterns, we track the fact that different
3196 pseudo-register numbers may have been used in each computation.
3197 The following array stores an equivalence -- same_regs[I] == J means
3198 that pseudo register I was used in the first set of tests in a context
3199 where J was used in the second set. We also count the number of such
3200 pending equivalences. If nonzero, the expressions really aren't the
3203 static int *same_regs;
3205 static int num_same_regs;
3207 /* Track any registers modified between the target of the first jump and
3208 the second jump. They never compare equal. */
3210 static char *modified_regs;
3212 /* Record if memory was modified. */
3214 static int modified_mem;
3216 /* Called via note_stores on each insn between the target of the first
3217 branch and the second branch. It marks any changed registers. */
3220 mark_modified_reg (dest, x, data)
3223 void *data ATTRIBUTE_UNUSED;
3228 if (GET_CODE (dest) == SUBREG)
3229 dest = SUBREG_REG (dest);
3231 if (GET_CODE (dest) == MEM)
3234 if (GET_CODE (dest) != REG)
3237 regno = REGNO (dest);
3238 if (regno >= FIRST_PSEUDO_REGISTER)
3239 modified_regs[regno] = 1;
3240 /* Don't consider a hard condition code register as modified,
3241 if it is only being set. thread_jumps will check if it is set
3242 to the same value. */
3243 else if (GET_MODE_CLASS (GET_MODE (dest)) != MODE_CC
3244 || GET_CODE (x) != SET
3245 || ! rtx_equal_p (dest, SET_DEST (x))
3246 || HARD_REGNO_NREGS (regno, GET_MODE (dest)) != 1)
3247 for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
3248 modified_regs[regno + i] = 1;
3251 /* F is the first insn in the chain of insns. */
3254 thread_jumps (f, max_reg, flag_before_loop)
3257 int flag_before_loop;
3259 /* Basic algorithm is to find a conditional branch,
3260 the label it may branch to, and the branch after
3261 that label. If the two branches test the same condition,
3262 walk back from both branch paths until the insn patterns
3263 differ, or code labels are hit. If we make it back to
3264 the target of the first branch, then we know that the first branch
3265 will either always succeed or always fail depending on the relative
3266 senses of the two branches. So adjust the first branch accordingly
3269 rtx label, b1, b2, t1, t2;
3270 enum rtx_code code1, code2;
3271 rtx b1op0, b1op1, b2op0, b2op1;
3275 enum rtx_code reversed_code1, reversed_code2;
3277 /* Allocate register tables and quick-reset table. */
3278 modified_regs = (char *) xmalloc (max_reg * sizeof (char));
3279 same_regs = (int *) xmalloc (max_reg * sizeof (int));
3280 all_reset = (int *) xmalloc (max_reg * sizeof (int));
3281 for (i = 0; i < max_reg; i++)
3288 for (b1 = f; b1; b1 = NEXT_INSN (b1))
3293 /* Get to a candidate branch insn. */
3294 if (GET_CODE (b1) != JUMP_INSN
3295 || ! any_condjump_p (b1) || JUMP_LABEL (b1) == 0)
3298 memset (modified_regs, 0, max_reg * sizeof (char));
3301 memcpy (same_regs, all_reset, max_reg * sizeof (int));
3304 label = JUMP_LABEL (b1);
3306 /* Look for a branch after the target. Record any registers and
3307 memory modified between the target and the branch. Stop when we
3308 get to a label since we can't know what was changed there. */
3309 for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
3311 if (GET_CODE (b2) == CODE_LABEL)
3314 else if (GET_CODE (b2) == JUMP_INSN)
3316 /* If this is an unconditional jump and is the only use of
3317 its target label, we can follow it. */
3318 if (any_uncondjump_p (b2)
3320 && JUMP_LABEL (b2) != 0
3321 && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
3323 b2 = JUMP_LABEL (b2);
3330 if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
3333 if (GET_CODE (b2) == CALL_INSN)
3336 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3337 if (call_used_regs[i] && ! fixed_regs[i]
3338 && i != STACK_POINTER_REGNUM
3339 && i != FRAME_POINTER_REGNUM
3340 && i != HARD_FRAME_POINTER_REGNUM
3341 && i != ARG_POINTER_REGNUM)
3342 modified_regs[i] = 1;
3345 note_stores (PATTERN (b2), mark_modified_reg, NULL);
3348 /* Check the next candidate branch insn from the label
3351 || GET_CODE (b2) != JUMP_INSN
3353 || !any_condjump_p (b2)
3354 || !onlyjump_p (b2))
3359 /* Get the comparison codes and operands, reversing the
3360 codes if appropriate. If we don't have comparison codes,
3361 we can't do anything. */
3362 b1op0 = XEXP (XEXP (SET_SRC (set), 0), 0);
3363 b1op1 = XEXP (XEXP (SET_SRC (set), 0), 1);
3364 code1 = GET_CODE (XEXP (SET_SRC (set), 0));
3365 reversed_code1 = code1;
3366 if (XEXP (SET_SRC (set), 1) == pc_rtx)
3367 code1 = reversed_comparison_code (XEXP (SET_SRC (set), 0), b1);
3369 reversed_code1 = reversed_comparison_code (XEXP (SET_SRC (set), 0), b1);
3371 b2op0 = XEXP (XEXP (SET_SRC (set2), 0), 0);
3372 b2op1 = XEXP (XEXP (SET_SRC (set2), 0), 1);
3373 code2 = GET_CODE (XEXP (SET_SRC (set2), 0));
3374 reversed_code2 = code2;
3375 if (XEXP (SET_SRC (set2), 1) == pc_rtx)
3376 code2 = reversed_comparison_code (XEXP (SET_SRC (set2), 0), b2);
3378 reversed_code2 = reversed_comparison_code (XEXP (SET_SRC (set2), 0), b2);
3380 /* If they test the same things and knowing that B1 branches
3381 tells us whether or not B2 branches, check if we
3382 can thread the branch. */
3383 if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
3384 && rtx_equal_for_thread_p (b1op1, b2op1, b2)
3385 && (comparison_dominates_p (code1, code2)
3386 || comparison_dominates_p (code1, reversed_code2)))
3389 t1 = prev_nonnote_insn (b1);
3390 t2 = prev_nonnote_insn (b2);
3392 while (t1 != 0 && t2 != 0)
3396 /* We have reached the target of the first branch.
3397 If there are no pending register equivalents,
3398 we know that this branch will either always
3399 succeed (if the senses of the two branches are
3400 the same) or always fail (if not). */
3403 if (num_same_regs != 0)
3406 if (comparison_dominates_p (code1, code2))
3407 new_label = JUMP_LABEL (b2);
3409 new_label = get_label_after (b2);
3411 if (JUMP_LABEL (b1) != new_label)
3413 rtx prev = PREV_INSN (new_label);
3415 if (flag_before_loop
3416 && GET_CODE (prev) == NOTE
3417 && NOTE_LINE_NUMBER (prev) == NOTE_INSN_LOOP_BEG)
3419 /* Don't thread to the loop label. If a loop
3420 label is reused, loop optimization will
3421 be disabled for that loop. */
3422 new_label = gen_label_rtx ();
3423 emit_label_after (new_label, PREV_INSN (prev));
3425 changed |= redirect_jump (b1, new_label, 1);
3430 /* If either of these is not a normal insn (it might be
3431 a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail. (NOTEs
3432 have already been skipped above.) Similarly, fail
3433 if the insns are different. */
3434 if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
3435 || recog_memoized (t1) != recog_memoized (t2)
3436 || ! rtx_equal_for_thread_p (PATTERN (t1),
3440 t1 = prev_nonnote_insn (t1);
3441 t2 = prev_nonnote_insn (t2);
3448 free (modified_regs);
3453 /* This is like RTX_EQUAL_P except that it knows about our handling of
3454 possibly equivalent registers and knows to consider volatile and
3455 modified objects as not equal.
3457 YINSN is the insn containing Y. */
3460 rtx_equal_for_thread_p (x, y, yinsn)
3466 register enum rtx_code code;
3467 register const char *fmt;
3469 code = GET_CODE (x);
3470 /* Rtx's of different codes cannot be equal. */
3471 if (code != GET_CODE (y))
3474 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
3475 (REG:SI x) and (REG:HI x) are NOT equivalent. */
3477 if (GET_MODE (x) != GET_MODE (y))
3480 /* For floating-point, consider everything unequal. This is a bit
3481 pessimistic, but this pass would only rarely do anything for FP
3483 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
3484 && FLOAT_MODE_P (GET_MODE (x)) && ! flag_unsafe_math_optimizations)
3487 /* For commutative operations, the RTX match if the operand match in any
3488 order. Also handle the simple binary and unary cases without a loop. */
3489 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
3490 return ((rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
3491 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn))
3492 || (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 1), yinsn)
3493 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 0), yinsn)));
3494 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
3495 return (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
3496 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn));
3497 else if (GET_RTX_CLASS (code) == '1')
3498 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
3500 /* Handle special-cases first. */
3504 if (REGNO (x) == REGNO (y) && ! modified_regs[REGNO (x)])
3507 /* If neither is user variable or hard register, check for possible
3509 if (REG_USERVAR_P (x) || REG_USERVAR_P (y)
3510 || REGNO (x) < FIRST_PSEUDO_REGISTER
3511 || REGNO (y) < FIRST_PSEUDO_REGISTER)
3514 if (same_regs[REGNO (x)] == -1)
3516 same_regs[REGNO (x)] = REGNO (y);
3519 /* If this is the first time we are seeing a register on the `Y'
3520 side, see if it is the last use. If not, we can't thread the
3521 jump, so mark it as not equivalent. */
3522 if (REGNO_LAST_UID (REGNO (y)) != INSN_UID (yinsn))
3528 return (same_regs[REGNO (x)] == (int) REGNO (y));
3533 /* If memory modified or either volatile, not equivalent.
3534 Else, check address. */
3535 if (modified_mem || MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
3538 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
3541 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
3547 /* Cancel a pending `same_regs' if setting equivalenced registers.
3548 Then process source. */
3549 if (GET_CODE (SET_DEST (x)) == REG
3550 && GET_CODE (SET_DEST (y)) == REG)
3552 if (same_regs[REGNO (SET_DEST (x))] == (int) REGNO (SET_DEST (y)))
3554 same_regs[REGNO (SET_DEST (x))] = -1;
3557 else if (REGNO (SET_DEST (x)) != REGNO (SET_DEST (y)))
3562 if (rtx_equal_for_thread_p (SET_DEST (x), SET_DEST (y), yinsn) == 0)
3566 return rtx_equal_for_thread_p (SET_SRC (x), SET_SRC (y), yinsn);
3569 return XEXP (x, 0) == XEXP (y, 0);
3572 return XSTR (x, 0) == XSTR (y, 0);
3581 fmt = GET_RTX_FORMAT (code);
3582 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3587 if (XWINT (x, i) != XWINT (y, i))
3593 if (XINT (x, i) != XINT (y, i))
3599 /* Two vectors must have the same length. */
3600 if (XVECLEN (x, i) != XVECLEN (y, i))
3603 /* And the corresponding elements must match. */
3604 for (j = 0; j < XVECLEN (x, i); j++)
3605 if (rtx_equal_for_thread_p (XVECEXP (x, i, j),
3606 XVECEXP (y, i, j), yinsn) == 0)
3611 if (rtx_equal_for_thread_p (XEXP (x, i), XEXP (y, i), yinsn) == 0)
3617 if (strcmp (XSTR (x, i), XSTR (y, i)))
3622 /* These are just backpointers, so they don't matter. */
3629 /* It is believed that rtx's at this level will never
3630 contain anything but integers and other rtx's,
3631 except for within LABEL_REFs and SYMBOL_REFs. */