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, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This is the pathetic reminder of old fame of the jump-optimization pass
23 of the compiler. Now it contains basically set of utility function to
26 Each CODE_LABEL has a count of the times it is used
27 stored in the LABEL_NUSES internal field, and each JUMP_INSN
28 has one label that it refers to stored in the
29 JUMP_LABEL internal field. With this we can detect labels that
30 become unused because of the deletion of all the jumps that
31 formerly used them. The JUMP_LABEL info is sometimes looked
34 The subroutines delete_insn, redirect_jump, and invert_jump are used
35 from other passes as well. */
39 #include "coretypes.h"
44 #include "hard-reg-set.h"
46 #include "insn-config.h"
47 #include "insn-attr.h"
53 #include "diagnostic.h"
59 /* Optimize jump y; x: ... y: jumpif... x?
60 Don't know if it is worth bothering with. */
61 /* Optimize two cases of conditional jump to conditional jump?
62 This can never delete any instruction or make anything dead,
63 or even change what is live at any point.
64 So perhaps let combiner do it. */
66 static rtx
next_nonnote_insn_in_loop (rtx
);
67 static void init_label_info (rtx
);
68 static void mark_all_labels (rtx
);
69 static int duplicate_loop_exit_test (rtx
);
70 static void delete_computation (rtx
);
71 static void redirect_exp_1 (rtx
*, rtx
, rtx
, rtx
);
72 static int redirect_exp (rtx
, rtx
, rtx
);
73 static void invert_exp_1 (rtx
);
74 static int invert_exp (rtx
);
75 static int returnjump_p_1 (rtx
*, void *);
76 static void delete_prior_computation (rtx
, rtx
);
78 /* Alternate entry into the jump optimizer. This entry point only rebuilds
79 the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
82 rebuild_jump_labels (rtx f
)
86 timevar_push (TV_REBUILD_JUMP
);
90 /* Keep track of labels used from static data; we don't track them
91 closely enough to delete them here, so make sure their reference
92 count doesn't drop to zero. */
94 for (insn
= forced_labels
; insn
; insn
= XEXP (insn
, 1))
95 if (GET_CODE (XEXP (insn
, 0)) == CODE_LABEL
)
96 LABEL_NUSES (XEXP (insn
, 0))++;
97 timevar_pop (TV_REBUILD_JUMP
);
100 /* Some old code expects exactly one BARRIER as the NEXT_INSN of a
101 non-fallthru insn. This is not generally true, as multiple barriers
102 may have crept in, or the BARRIER may be separated from the last
103 real insn by one or more NOTEs.
105 This simple pass moves barriers and removes duplicates so that the
109 cleanup_barriers (void)
111 rtx insn
, next
, prev
;
112 for (insn
= get_insns (); insn
; insn
= next
)
114 next
= NEXT_INSN (insn
);
115 if (GET_CODE (insn
) == BARRIER
)
117 prev
= prev_nonnote_insn (insn
);
118 if (GET_CODE (prev
) == BARRIER
)
119 delete_barrier (insn
);
120 else if (prev
!= PREV_INSN (insn
))
121 reorder_insns (insn
, insn
, prev
);
126 /* Return the next insn after INSN that is not a NOTE and is in the loop,
127 i.e. when there is no such INSN before NOTE_INSN_LOOP_END return NULL_RTX.
128 This routine does not look inside SEQUENCEs. */
131 next_nonnote_insn_in_loop (rtx insn
)
135 insn
= NEXT_INSN (insn
);
136 if (insn
== 0 || GET_CODE (insn
) != NOTE
)
138 if (GET_CODE (insn
) == NOTE
139 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
)
147 copy_loop_headers (rtx f
)
150 /* Now iterate optimizing jumps until nothing changes over one pass. */
151 for (insn
= f
; insn
; insn
= next
)
155 next
= NEXT_INSN (insn
);
157 /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
158 jump. Try to optimize by duplicating the loop exit test if so.
159 This is only safe immediately after regscan, because it uses
160 the values of regno_first_uid and regno_last_uid. */
161 if (GET_CODE (insn
) == NOTE
162 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
163 && (temp1
= next_nonnote_insn_in_loop (insn
)) != 0
164 && any_uncondjump_p (temp1
) && onlyjump_p (temp1
))
166 temp
= PREV_INSN (insn
);
167 if (duplicate_loop_exit_test (insn
))
169 next
= NEXT_INSN (temp
);
176 purge_line_number_notes (rtx f
)
180 /* Delete extraneous line number notes.
181 Note that two consecutive notes for different lines are not really
182 extraneous. There should be some indication where that line belonged,
183 even if it became empty. */
185 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
186 if (GET_CODE (insn
) == NOTE
)
188 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_FUNCTION_BEG
)
189 /* Any previous line note was for the prologue; gdb wants a new
190 note after the prologue even if it is for the same line. */
191 last_note
= NULL_RTX
;
192 else if (NOTE_LINE_NUMBER (insn
) >= 0)
194 /* Delete this note if it is identical to previous note. */
196 && NOTE_SOURCE_FILE (insn
) == NOTE_SOURCE_FILE (last_note
)
197 && NOTE_LINE_NUMBER (insn
) == NOTE_LINE_NUMBER (last_note
))
199 delete_related_insns (insn
);
208 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
209 notes whose labels don't occur in the insn any more. Returns the
210 largest INSN_UID found. */
212 init_label_info (rtx f
)
216 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
217 if (GET_CODE (insn
) == CODE_LABEL
)
218 LABEL_NUSES (insn
) = (LABEL_PRESERVE_P (insn
) != 0);
219 else if (GET_CODE (insn
) == JUMP_INSN
)
220 JUMP_LABEL (insn
) = 0;
221 else if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == CALL_INSN
)
225 for (note
= REG_NOTES (insn
); note
; note
= next
)
227 next
= XEXP (note
, 1);
228 if (REG_NOTE_KIND (note
) == REG_LABEL
229 && ! reg_mentioned_p (XEXP (note
, 0), PATTERN (insn
)))
230 remove_note (insn
, note
);
235 /* Mark the label each jump jumps to.
236 Combine consecutive labels, and count uses of labels. */
239 mark_all_labels (rtx f
)
243 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
246 if (GET_CODE (insn
) == CALL_INSN
247 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
249 mark_all_labels (XEXP (PATTERN (insn
), 0));
250 mark_all_labels (XEXP (PATTERN (insn
), 1));
251 mark_all_labels (XEXP (PATTERN (insn
), 2));
253 /* Canonicalize the tail recursion label attached to the
254 CALL_PLACEHOLDER insn. */
255 if (XEXP (PATTERN (insn
), 3))
257 rtx label_ref
= gen_rtx_LABEL_REF (VOIDmode
,
258 XEXP (PATTERN (insn
), 3));
259 mark_jump_label (label_ref
, insn
, 0);
260 XEXP (PATTERN (insn
), 3) = XEXP (label_ref
, 0);
266 mark_jump_label (PATTERN (insn
), insn
, 0);
267 if (! INSN_DELETED_P (insn
) && GET_CODE (insn
) == JUMP_INSN
)
269 /* When we know the LABEL_REF contained in a REG used in
270 an indirect jump, we'll have a REG_LABEL note so that
271 flow can tell where it's going. */
272 if (JUMP_LABEL (insn
) == 0)
274 rtx label_note
= find_reg_note (insn
, REG_LABEL
, NULL_RTX
);
277 /* But a LABEL_REF around the REG_LABEL note, so
278 that we can canonicalize it. */
279 rtx label_ref
= gen_rtx_LABEL_REF (VOIDmode
,
280 XEXP (label_note
, 0));
282 mark_jump_label (label_ref
, insn
, 0);
283 XEXP (label_note
, 0) = XEXP (label_ref
, 0);
284 JUMP_LABEL (insn
) = XEXP (label_note
, 0);
291 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
292 jump. Assume that this unconditional jump is to the exit test code. If
293 the code is sufficiently simple, make a copy of it before INSN,
294 followed by a jump to the exit of the loop. Then delete the unconditional
297 Return 1 if we made the change, else 0.
299 This is only safe immediately after a regscan pass because it uses the
300 values of regno_first_uid and regno_last_uid. */
303 duplicate_loop_exit_test (rtx loop_start
)
305 rtx insn
, set
, reg
, p
, link
;
306 rtx copy
= 0, first_copy
= 0;
309 = NEXT_INSN (JUMP_LABEL (next_nonnote_insn_in_loop (loop_start
)));
311 int max_reg
= max_reg_num ();
313 rtx loop_pre_header_label
;
316 /* If EXITCODE is not in the loop, then this optimization is not
317 safe; we will emit a VTOP note entirely outside the loop. */
318 for (insn
= loop_start
, loop_depth
= 0;
320 insn
= NEXT_INSN (insn
))
322 if (GET_CODE (insn
) != NOTE
)
325 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
327 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
328 && --loop_depth
== 0)
332 /* Scan the exit code. We do not perform this optimization if any insn:
336 has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
337 is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
339 We also do not do this if we find an insn with ASM_OPERANDS. While
340 this restriction should not be necessary, copying an insn with
341 ASM_OPERANDS can confuse asm_noperands in some cases.
343 Also, don't do this if the exit code is more than 20 insns. */
345 for (insn
= exitcode
;
347 && ! (GET_CODE (insn
) == NOTE
348 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
);
349 insn
= NEXT_INSN (insn
))
351 switch (GET_CODE (insn
))
359 && (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_BLOCK_BEG
360 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_BLOCK_END
))
361 /* If we were to duplicate this code, we would not move
362 the BLOCK notes, and so debugging the moved code would
363 be difficult. Thus, we only move the code with -O2 or
371 || find_reg_note (insn
, REG_RETVAL
, NULL_RTX
)
372 || find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
))
380 /* Unless INSN is zero, we can do the optimization. */
386 /* See if any insn sets a register only used in the loop exit code and
387 not a user variable. If so, replace it with a new register. */
388 for (insn
= exitcode
; insn
!= lastexit
; insn
= NEXT_INSN (insn
))
389 if (GET_CODE (insn
) == INSN
390 && (set
= single_set (insn
)) != 0
391 && ((reg
= SET_DEST (set
), GET_CODE (reg
) == REG
)
392 || (GET_CODE (reg
) == SUBREG
393 && (reg
= SUBREG_REG (reg
), GET_CODE (reg
) == REG
)))
394 && REGNO (reg
) >= FIRST_PSEUDO_REGISTER
395 && REGNO_FIRST_UID (REGNO (reg
)) == INSN_UID (insn
))
397 for (p
= NEXT_INSN (insn
); p
!= lastexit
; p
= NEXT_INSN (p
))
398 if (REGNO_LAST_UID (REGNO (reg
)) == INSN_UID (p
))
403 /* We can do the replacement. Allocate reg_map if this is the
404 first replacement we found. */
406 reg_map
= xcalloc (max_reg
, sizeof (rtx
));
408 REG_LOOP_TEST_P (reg
) = 1;
410 reg_map
[REGNO (reg
)] = gen_reg_rtx (GET_MODE (reg
));
413 loop_pre_header_label
= gen_label_rtx ();
415 /* Now copy each insn. */
416 for (insn
= exitcode
; insn
!= lastexit
; insn
= NEXT_INSN (insn
))
418 switch (GET_CODE (insn
))
421 copy
= emit_barrier_before (loop_start
);
424 /* Only copy line-number notes. */
425 if (NOTE_LINE_NUMBER (insn
) >= 0)
427 copy
= emit_note_before (NOTE_LINE_NUMBER (insn
), loop_start
);
428 NOTE_SOURCE_FILE (copy
) = NOTE_SOURCE_FILE (insn
);
433 copy
= emit_insn_before (copy_insn (PATTERN (insn
)), loop_start
);
435 replace_regs (PATTERN (copy
), reg_map
, max_reg
, 1);
437 mark_jump_label (PATTERN (copy
), copy
, 0);
438 INSN_LOCATOR (copy
) = INSN_LOCATOR (insn
);
440 /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
442 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
443 if (REG_NOTE_KIND (link
) != REG_LABEL
)
445 if (GET_CODE (link
) == EXPR_LIST
)
447 = copy_insn_1 (gen_rtx_EXPR_LIST (REG_NOTE_KIND (link
),
452 = copy_insn_1 (gen_rtx_INSN_LIST (REG_NOTE_KIND (link
),
457 if (reg_map
&& REG_NOTES (copy
))
458 replace_regs (REG_NOTES (copy
), reg_map
, max_reg
, 1);
462 copy
= emit_jump_insn_before (copy_insn (PATTERN (insn
)),
464 INSN_LOCATOR (copy
) = INSN_LOCATOR (insn
);
466 replace_regs (PATTERN (copy
), reg_map
, max_reg
, 1);
467 mark_jump_label (PATTERN (copy
), copy
, 0);
468 if (REG_NOTES (insn
))
470 REG_NOTES (copy
) = copy_insn_1 (REG_NOTES (insn
));
472 replace_regs (REG_NOTES (copy
), reg_map
, max_reg
, 1);
475 /* Predict conditional jump that do make loop looping as taken.
476 Other jumps are probably exit conditions, so predict
478 if (any_condjump_p (copy
))
480 rtx label
= JUMP_LABEL (copy
);
483 /* The jump_insn after loop_start should be followed
484 by barrier and loopback label. */
485 if (prev_nonnote_insn (label
)
486 && (prev_nonnote_insn (prev_nonnote_insn (label
))
487 == next_nonnote_insn (loop_start
)))
489 predict_insn_def (copy
, PRED_LOOP_HEADER
, TAKEN
);
490 /* To keep pre-header, we need to redirect all loop
491 entrances before the LOOP_BEG note. */
492 redirect_jump (copy
, loop_pre_header_label
, 0);
495 predict_insn_def (copy
, PRED_LOOP_HEADER
, NOT_TAKEN
);
504 /* Record the first insn we copied. We need it so that we can
505 scan the copied insns for new pseudo registers. */
510 /* Now clean up by emitting a jump to the end label and deleting the jump
511 at the start of the loop. */
512 if (! copy
|| GET_CODE (copy
) != BARRIER
)
514 copy
= emit_jump_insn_before (gen_jump (get_label_after (insn
)),
517 /* Record the first insn we copied. We need it so that we can
518 scan the copied insns for new pseudo registers. This may not
519 be strictly necessary since we should have copied at least one
520 insn above. But I am going to be safe. */
524 mark_jump_label (PATTERN (copy
), copy
, 0);
525 emit_barrier_before (loop_start
);
528 emit_label_before (loop_pre_header_label
, loop_start
);
530 /* Now scan from the first insn we copied to the last insn we copied
531 (copy) for new pseudo registers. Do this after the code to jump to
532 the end label since that might create a new pseudo too. */
533 reg_scan_update (first_copy
, copy
, max_reg
);
535 /* Mark the exit code as the virtual top of the converted loop. */
536 emit_note_before (NOTE_INSN_LOOP_VTOP
, exitcode
);
538 delete_related_insns (next_nonnote_insn (loop_start
));
547 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
548 notes between START and END out before START. START and END may be such
549 notes. Returns the values of the new starting and ending insns, which
550 may be different if the original ones were such notes.
551 Return true if there were only such notes and no real instructions. */
554 squeeze_notes (rtx
* startp
, rtx
* endp
)
562 rtx past_end
= NEXT_INSN (end
);
564 for (insn
= start
; insn
!= past_end
; insn
= next
)
566 next
= NEXT_INSN (insn
);
567 if (GET_CODE (insn
) == NOTE
568 && (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_BLOCK_END
569 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_BLOCK_BEG
570 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
571 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
572 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_CONT
573 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_VTOP
))
579 rtx prev
= PREV_INSN (insn
);
580 PREV_INSN (insn
) = PREV_INSN (start
);
581 NEXT_INSN (insn
) = start
;
582 NEXT_INSN (PREV_INSN (insn
)) = insn
;
583 PREV_INSN (NEXT_INSN (insn
)) = insn
;
584 NEXT_INSN (prev
) = next
;
585 PREV_INSN (next
) = prev
;
592 /* There were no real instructions. */
593 if (start
== past_end
)
603 /* Return the label before INSN, or put a new label there. */
606 get_label_before (rtx insn
)
610 /* Find an existing label at this point
611 or make a new one if there is none. */
612 label
= prev_nonnote_insn (insn
);
614 if (label
== 0 || GET_CODE (label
) != CODE_LABEL
)
616 rtx prev
= PREV_INSN (insn
);
618 label
= gen_label_rtx ();
619 emit_label_after (label
, prev
);
620 LABEL_NUSES (label
) = 0;
625 /* Return the label after INSN, or put a new label there. */
628 get_label_after (rtx insn
)
632 /* Find an existing label at this point
633 or make a new one if there is none. */
634 label
= next_nonnote_insn (insn
);
636 if (label
== 0 || GET_CODE (label
) != CODE_LABEL
)
638 label
= gen_label_rtx ();
639 emit_label_after (label
, insn
);
640 LABEL_NUSES (label
) = 0;
645 /* Given a comparison (CODE ARG0 ARG1), inside an insn, INSN, return a code
646 of reversed comparison if it is possible to do so. Otherwise return UNKNOWN.
647 UNKNOWN may be returned in case we are having CC_MODE compare and we don't
648 know whether it's source is floating point or integer comparison. Machine
649 description should define REVERSIBLE_CC_MODE and REVERSE_CONDITION macros
650 to help this function avoid overhead in these cases. */
652 reversed_comparison_code_parts (enum rtx_code code
, rtx arg0
, rtx arg1
, rtx insn
)
654 enum machine_mode mode
;
656 /* If this is not actually a comparison, we can't reverse it. */
657 if (GET_RTX_CLASS (code
) != '<')
660 mode
= GET_MODE (arg0
);
661 if (mode
== VOIDmode
)
662 mode
= GET_MODE (arg1
);
664 /* First see if machine description supply us way to reverse the comparison.
665 Give it priority over everything else to allow machine description to do
667 #ifdef REVERSIBLE_CC_MODE
668 if (GET_MODE_CLASS (mode
) == MODE_CC
669 && REVERSIBLE_CC_MODE (mode
))
671 #ifdef REVERSE_CONDITION
672 return REVERSE_CONDITION (code
, mode
);
674 return reverse_condition (code
);
678 /* Try a few special cases based on the comparison code. */
687 /* It is always safe to reverse EQ and NE, even for the floating
688 point. Similarly the unsigned comparisons are never used for
689 floating point so we can reverse them in the default way. */
690 return reverse_condition (code
);
695 /* In case we already see unordered comparison, we can be sure to
696 be dealing with floating point so we don't need any more tests. */
697 return reverse_condition_maybe_unordered (code
);
702 /* We don't have safe way to reverse these yet. */
708 if (GET_MODE_CLASS (mode
) == MODE_CC
|| CC0_P (arg0
))
711 /* Try to search for the comparison to determine the real mode.
712 This code is expensive, but with sane machine description it
713 will be never used, since REVERSIBLE_CC_MODE will return true
718 for (prev
= prev_nonnote_insn (insn
);
719 prev
!= 0 && GET_CODE (prev
) != CODE_LABEL
;
720 prev
= prev_nonnote_insn (prev
))
722 rtx set
= set_of (arg0
, prev
);
723 if (set
&& GET_CODE (set
) == SET
724 && rtx_equal_p (SET_DEST (set
), arg0
))
726 rtx src
= SET_SRC (set
);
728 if (GET_CODE (src
) == COMPARE
)
730 rtx comparison
= src
;
731 arg0
= XEXP (src
, 0);
732 mode
= GET_MODE (arg0
);
733 if (mode
== VOIDmode
)
734 mode
= GET_MODE (XEXP (comparison
, 1));
737 /* We can get past reg-reg moves. This may be useful for model
738 of i387 comparisons that first move flag registers around. */
745 /* If register is clobbered in some ununderstandable way,
752 /* Test for an integer condition, or a floating-point comparison
753 in which NaNs can be ignored. */
754 if (GET_CODE (arg0
) == CONST_INT
755 || (GET_MODE (arg0
) != VOIDmode
756 && GET_MODE_CLASS (mode
) != MODE_CC
757 && !HONOR_NANS (mode
)))
758 return reverse_condition (code
);
763 /* A wrapper around the previous function to take COMPARISON as rtx
764 expression. This simplifies many callers. */
766 reversed_comparison_code (rtx comparison
, rtx insn
)
768 if (GET_RTX_CLASS (GET_CODE (comparison
)) != '<')
770 return reversed_comparison_code_parts (GET_CODE (comparison
),
771 XEXP (comparison
, 0),
772 XEXP (comparison
, 1), insn
);
775 /* Given an rtx-code for a comparison, return the code for the negated
776 comparison. If no such code exists, return UNKNOWN.
778 WATCH OUT! reverse_condition is not safe to use on a jump that might
779 be acting on the results of an IEEE floating point comparison, because
780 of the special treatment of non-signaling nans in comparisons.
781 Use reversed_comparison_code instead. */
784 reverse_condition (enum rtx_code code
)
826 /* Similar, but we're allowed to generate unordered comparisons, which
827 makes it safe for IEEE floating-point. Of course, we have to recognize
828 that the target will support them too... */
831 reverse_condition_maybe_unordered (enum rtx_code code
)
869 /* Similar, but return the code when two operands of a comparison are swapped.
870 This IS safe for IEEE floating-point. */
873 swap_condition (enum rtx_code code
)
915 /* Given a comparison CODE, return the corresponding unsigned comparison.
916 If CODE is an equality comparison or already an unsigned comparison,
920 unsigned_condition (enum rtx_code code
)
946 /* Similarly, return the signed version of a comparison. */
949 signed_condition (enum rtx_code code
)
975 /* Return nonzero if CODE1 is more strict than CODE2, i.e., if the
976 truth of CODE1 implies the truth of CODE2. */
979 comparison_dominates_p (enum rtx_code code1
, enum rtx_code code2
)
981 /* UNKNOWN comparison codes can happen as a result of trying to revert
983 They can't match anything, so we have to reject them here. */
984 if (code1
== UNKNOWN
|| code2
== UNKNOWN
)
993 if (code2
== UNLE
|| code2
== UNGE
)
998 if (code2
== LE
|| code2
== LEU
|| code2
== GE
|| code2
== GEU
1004 if (code2
== UNLE
|| code2
== NE
)
1009 if (code2
== LE
|| code2
== NE
|| code2
== ORDERED
|| code2
== LTGT
)
1014 if (code2
== UNGE
|| code2
== NE
)
1019 if (code2
== GE
|| code2
== NE
|| code2
== ORDERED
|| code2
== LTGT
)
1025 if (code2
== ORDERED
)
1030 if (code2
== NE
|| code2
== ORDERED
)
1035 if (code2
== LEU
|| code2
== NE
)
1040 if (code2
== GEU
|| code2
== NE
)
1045 if (code2
== NE
|| code2
== UNEQ
|| code2
== UNLE
|| code2
== UNLT
1046 || code2
== UNGE
|| code2
== UNGT
)
1057 /* Return 1 if INSN is an unconditional jump and nothing else. */
1060 simplejump_p (rtx insn
)
1062 return (GET_CODE (insn
) == JUMP_INSN
1063 && GET_CODE (PATTERN (insn
)) == SET
1064 && GET_CODE (SET_DEST (PATTERN (insn
))) == PC
1065 && GET_CODE (SET_SRC (PATTERN (insn
))) == LABEL_REF
);
1068 /* Return nonzero if INSN is a (possibly) conditional jump
1071 Use this function is deprecated, since we need to support combined
1072 branch and compare insns. Use any_condjump_p instead whenever possible. */
1075 condjump_p (rtx insn
)
1077 rtx x
= PATTERN (insn
);
1079 if (GET_CODE (x
) != SET
1080 || GET_CODE (SET_DEST (x
)) != PC
)
1084 if (GET_CODE (x
) == LABEL_REF
)
1087 return (GET_CODE (x
) == IF_THEN_ELSE
1088 && ((GET_CODE (XEXP (x
, 2)) == PC
1089 && (GET_CODE (XEXP (x
, 1)) == LABEL_REF
1090 || GET_CODE (XEXP (x
, 1)) == RETURN
))
1091 || (GET_CODE (XEXP (x
, 1)) == PC
1092 && (GET_CODE (XEXP (x
, 2)) == LABEL_REF
1093 || GET_CODE (XEXP (x
, 2)) == RETURN
))));
1098 /* Return nonzero if INSN is a (possibly) conditional jump inside a
1101 Use this function is deprecated, since we need to support combined
1102 branch and compare insns. Use any_condjump_p instead whenever possible. */
1105 condjump_in_parallel_p (rtx insn
)
1107 rtx x
= PATTERN (insn
);
1109 if (GET_CODE (x
) != PARALLEL
)
1112 x
= XVECEXP (x
, 0, 0);
1114 if (GET_CODE (x
) != SET
)
1116 if (GET_CODE (SET_DEST (x
)) != PC
)
1118 if (GET_CODE (SET_SRC (x
)) == LABEL_REF
)
1120 if (GET_CODE (SET_SRC (x
)) != IF_THEN_ELSE
)
1122 if (XEXP (SET_SRC (x
), 2) == pc_rtx
1123 && (GET_CODE (XEXP (SET_SRC (x
), 1)) == LABEL_REF
1124 || GET_CODE (XEXP (SET_SRC (x
), 1)) == RETURN
))
1126 if (XEXP (SET_SRC (x
), 1) == pc_rtx
1127 && (GET_CODE (XEXP (SET_SRC (x
), 2)) == LABEL_REF
1128 || GET_CODE (XEXP (SET_SRC (x
), 2)) == RETURN
))
1133 /* Return set of PC, otherwise NULL. */
1139 if (GET_CODE (insn
) != JUMP_INSN
)
1141 pat
= PATTERN (insn
);
1143 /* The set is allowed to appear either as the insn pattern or
1144 the first set in a PARALLEL. */
1145 if (GET_CODE (pat
) == PARALLEL
)
1146 pat
= XVECEXP (pat
, 0, 0);
1147 if (GET_CODE (pat
) == SET
&& GET_CODE (SET_DEST (pat
)) == PC
)
1153 /* Return true when insn is an unconditional direct jump,
1154 possibly bundled inside a PARALLEL. */
1157 any_uncondjump_p (rtx insn
)
1159 rtx x
= pc_set (insn
);
1162 if (GET_CODE (SET_SRC (x
)) != LABEL_REF
)
1167 /* Return true when insn is a conditional jump. This function works for
1168 instructions containing PC sets in PARALLELs. The instruction may have
1169 various other effects so before removing the jump you must verify
1172 Note that unlike condjump_p it returns false for unconditional jumps. */
1175 any_condjump_p (rtx insn
)
1177 rtx x
= pc_set (insn
);
1182 if (GET_CODE (SET_SRC (x
)) != IF_THEN_ELSE
)
1185 a
= GET_CODE (XEXP (SET_SRC (x
), 1));
1186 b
= GET_CODE (XEXP (SET_SRC (x
), 2));
1188 return ((b
== PC
&& (a
== LABEL_REF
|| a
== RETURN
))
1189 || (a
== PC
&& (b
== LABEL_REF
|| b
== RETURN
)));
1192 /* Return the label of a conditional jump. */
1195 condjump_label (rtx insn
)
1197 rtx x
= pc_set (insn
);
1202 if (GET_CODE (x
) == LABEL_REF
)
1204 if (GET_CODE (x
) != IF_THEN_ELSE
)
1206 if (XEXP (x
, 2) == pc_rtx
&& GET_CODE (XEXP (x
, 1)) == LABEL_REF
)
1208 if (XEXP (x
, 1) == pc_rtx
&& GET_CODE (XEXP (x
, 2)) == LABEL_REF
)
1213 /* Return true if INSN is a (possibly conditional) return insn. */
1216 returnjump_p_1 (rtx
*loc
, void *data ATTRIBUTE_UNUSED
)
1220 return x
&& (GET_CODE (x
) == RETURN
1221 || (GET_CODE (x
) == SET
&& SET_IS_RETURN_P (x
)));
1225 returnjump_p (rtx insn
)
1227 if (GET_CODE (insn
) != JUMP_INSN
)
1229 return for_each_rtx (&PATTERN (insn
), returnjump_p_1
, NULL
);
1232 /* Return true if INSN is a jump that only transfers control and
1236 onlyjump_p (rtx insn
)
1240 if (GET_CODE (insn
) != JUMP_INSN
)
1243 set
= single_set (insn
);
1246 if (GET_CODE (SET_DEST (set
)) != PC
)
1248 if (side_effects_p (SET_SRC (set
)))
1256 /* Return nonzero if X is an RTX that only sets the condition codes
1257 and has no side effects. */
1260 only_sets_cc0_p (rtx x
)
1268 return sets_cc0_p (x
) == 1 && ! side_effects_p (x
);
1271 /* Return 1 if X is an RTX that does nothing but set the condition codes
1272 and CLOBBER or USE registers.
1273 Return -1 if X does explicitly set the condition codes,
1274 but also does other things. */
1285 if (GET_CODE (x
) == SET
&& SET_DEST (x
) == cc0_rtx
)
1287 if (GET_CODE (x
) == PARALLEL
)
1291 int other_things
= 0;
1292 for (i
= XVECLEN (x
, 0) - 1; i
>= 0; i
--)
1294 if (GET_CODE (XVECEXP (x
, 0, i
)) == SET
1295 && SET_DEST (XVECEXP (x
, 0, i
)) == cc0_rtx
)
1297 else if (GET_CODE (XVECEXP (x
, 0, i
)) == SET
)
1300 return ! sets_cc0
? 0 : other_things
? -1 : 1;
1306 /* Follow any unconditional jump at LABEL;
1307 return the ultimate label reached by any such chain of jumps.
1308 If LABEL is not followed by a jump, return LABEL.
1309 If the chain loops or we can't find end, return LABEL,
1310 since that tells caller to avoid changing the insn.
1312 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
1313 a USE or CLOBBER. */
1316 follow_jumps (rtx label
)
1325 && (insn
= next_active_insn (value
)) != 0
1326 && GET_CODE (insn
) == JUMP_INSN
1327 && ((JUMP_LABEL (insn
) != 0 && any_uncondjump_p (insn
)
1328 && onlyjump_p (insn
))
1329 || GET_CODE (PATTERN (insn
)) == RETURN
)
1330 && (next
= NEXT_INSN (insn
))
1331 && GET_CODE (next
) == BARRIER
);
1334 /* Don't chain through the insn that jumps into a loop
1335 from outside the loop,
1336 since that would create multiple loop entry jumps
1337 and prevent loop optimization. */
1339 if (!reload_completed
)
1340 for (tem
= value
; tem
!= insn
; tem
= NEXT_INSN (tem
))
1341 if (GET_CODE (tem
) == NOTE
1342 && (NOTE_LINE_NUMBER (tem
) == NOTE_INSN_LOOP_BEG
1343 /* ??? Optional. Disables some optimizations, but makes
1344 gcov output more accurate with -O. */
1345 || (flag_test_coverage
&& NOTE_LINE_NUMBER (tem
) > 0)))
1348 /* If we have found a cycle, make the insn jump to itself. */
1349 if (JUMP_LABEL (insn
) == label
)
1352 tem
= next_active_insn (JUMP_LABEL (insn
));
1353 if (tem
&& (GET_CODE (PATTERN (tem
)) == ADDR_VEC
1354 || GET_CODE (PATTERN (tem
)) == ADDR_DIFF_VEC
))
1357 value
= JUMP_LABEL (insn
);
1365 /* Find all CODE_LABELs referred to in X, and increment their use counts.
1366 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
1367 in INSN, then store one of them in JUMP_LABEL (INSN).
1368 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
1369 referenced in INSN, add a REG_LABEL note containing that label to INSN.
1370 Also, when there are consecutive labels, canonicalize on the last of them.
1372 Note that two labels separated by a loop-beginning note
1373 must be kept distinct if we have not yet done loop-optimization,
1374 because the gap between them is where loop-optimize
1375 will want to move invariant code to. CROSS_JUMP tells us
1376 that loop-optimization is done with. */
1379 mark_jump_label (rtx x
, rtx insn
, int in_mem
)
1381 RTX_CODE code
= GET_CODE (x
);
1404 /* If this is a constant-pool reference, see if it is a label. */
1405 if (CONSTANT_POOL_ADDRESS_P (x
))
1406 mark_jump_label (get_pool_constant (x
), insn
, in_mem
);
1411 rtx label
= XEXP (x
, 0);
1413 /* Ignore remaining references to unreachable labels that
1414 have been deleted. */
1415 if (GET_CODE (label
) == NOTE
1416 && NOTE_LINE_NUMBER (label
) == NOTE_INSN_DELETED_LABEL
)
1419 if (GET_CODE (label
) != CODE_LABEL
)
1422 /* Ignore references to labels of containing functions. */
1423 if (LABEL_REF_NONLOCAL_P (x
))
1426 XEXP (x
, 0) = label
;
1427 if (! insn
|| ! INSN_DELETED_P (insn
))
1428 ++LABEL_NUSES (label
);
1432 if (GET_CODE (insn
) == JUMP_INSN
)
1433 JUMP_LABEL (insn
) = label
;
1436 /* Add a REG_LABEL note for LABEL unless there already
1437 is one. All uses of a label, except for labels
1438 that are the targets of jumps, must have a
1440 if (! find_reg_note (insn
, REG_LABEL
, label
))
1441 REG_NOTES (insn
) = gen_rtx_INSN_LIST (REG_LABEL
, label
,
1448 /* Do walk the labels in a vector, but not the first operand of an
1449 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
1452 if (! INSN_DELETED_P (insn
))
1454 int eltnum
= code
== ADDR_DIFF_VEC
? 1 : 0;
1456 for (i
= 0; i
< XVECLEN (x
, eltnum
); i
++)
1457 mark_jump_label (XVECEXP (x
, eltnum
, i
), NULL_RTX
, in_mem
);
1465 fmt
= GET_RTX_FORMAT (code
);
1466 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1469 mark_jump_label (XEXP (x
, i
), insn
, in_mem
);
1470 else if (fmt
[i
] == 'E')
1473 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1474 mark_jump_label (XVECEXP (x
, i
, j
), insn
, in_mem
);
1479 /* If all INSN does is set the pc, delete it,
1480 and delete the insn that set the condition codes for it
1481 if that's what the previous thing was. */
1484 delete_jump (rtx insn
)
1486 rtx set
= single_set (insn
);
1488 if (set
&& GET_CODE (SET_DEST (set
)) == PC
)
1489 delete_computation (insn
);
1492 /* Verify INSN is a BARRIER and delete it. */
1495 delete_barrier (rtx insn
)
1497 if (GET_CODE (insn
) != BARRIER
)
1503 /* Recursively delete prior insns that compute the value (used only by INSN
1504 which the caller is deleting) stored in the register mentioned by NOTE
1505 which is a REG_DEAD note associated with INSN. */
1508 delete_prior_computation (rtx note
, rtx insn
)
1511 rtx reg
= XEXP (note
, 0);
1513 for (our_prev
= prev_nonnote_insn (insn
);
1514 our_prev
&& (GET_CODE (our_prev
) == INSN
1515 || GET_CODE (our_prev
) == CALL_INSN
);
1516 our_prev
= prev_nonnote_insn (our_prev
))
1518 rtx pat
= PATTERN (our_prev
);
1520 /* If we reach a CALL which is not calling a const function
1521 or the callee pops the arguments, then give up. */
1522 if (GET_CODE (our_prev
) == CALL_INSN
1523 && (! CONST_OR_PURE_CALL_P (our_prev
)
1524 || GET_CODE (pat
) != SET
|| GET_CODE (SET_SRC (pat
)) != CALL
))
1527 /* If we reach a SEQUENCE, it is too complex to try to
1528 do anything with it, so give up. We can be run during
1529 and after reorg, so SEQUENCE rtl can legitimately show
1531 if (GET_CODE (pat
) == SEQUENCE
)
1534 if (GET_CODE (pat
) == USE
1535 && GET_CODE (XEXP (pat
, 0)) == INSN
)
1536 /* reorg creates USEs that look like this. We leave them
1537 alone because reorg needs them for its own purposes. */
1540 if (reg_set_p (reg
, pat
))
1542 if (side_effects_p (pat
) && GET_CODE (our_prev
) != CALL_INSN
)
1545 if (GET_CODE (pat
) == PARALLEL
)
1547 /* If we find a SET of something else, we can't
1552 for (i
= 0; i
< XVECLEN (pat
, 0); i
++)
1554 rtx part
= XVECEXP (pat
, 0, i
);
1556 if (GET_CODE (part
) == SET
1557 && SET_DEST (part
) != reg
)
1561 if (i
== XVECLEN (pat
, 0))
1562 delete_computation (our_prev
);
1564 else if (GET_CODE (pat
) == SET
1565 && GET_CODE (SET_DEST (pat
)) == REG
)
1567 int dest_regno
= REGNO (SET_DEST (pat
));
1570 + (dest_regno
< FIRST_PSEUDO_REGISTER
1571 ? HARD_REGNO_NREGS (dest_regno
,
1572 GET_MODE (SET_DEST (pat
))) : 1));
1573 int regno
= REGNO (reg
);
1576 + (regno
< FIRST_PSEUDO_REGISTER
1577 ? HARD_REGNO_NREGS (regno
, GET_MODE (reg
)) : 1));
1579 if (dest_regno
>= regno
1580 && dest_endregno
<= endregno
)
1581 delete_computation (our_prev
);
1583 /* We may have a multi-word hard register and some, but not
1584 all, of the words of the register are needed in subsequent
1585 insns. Write REG_UNUSED notes for those parts that were not
1587 else if (dest_regno
<= regno
1588 && dest_endregno
>= endregno
)
1592 REG_NOTES (our_prev
)
1593 = gen_rtx_EXPR_LIST (REG_UNUSED
, reg
,
1594 REG_NOTES (our_prev
));
1596 for (i
= dest_regno
; i
< dest_endregno
; i
++)
1597 if (! find_regno_note (our_prev
, REG_UNUSED
, i
))
1600 if (i
== dest_endregno
)
1601 delete_computation (our_prev
);
1608 /* If PAT references the register that dies here, it is an
1609 additional use. Hence any prior SET isn't dead. However, this
1610 insn becomes the new place for the REG_DEAD note. */
1611 if (reg_overlap_mentioned_p (reg
, pat
))
1613 XEXP (note
, 1) = REG_NOTES (our_prev
);
1614 REG_NOTES (our_prev
) = note
;
1620 /* Delete INSN and recursively delete insns that compute values used only
1621 by INSN. This uses the REG_DEAD notes computed during flow analysis.
1622 If we are running before flow.c, we need do nothing since flow.c will
1623 delete dead code. We also can't know if the registers being used are
1624 dead or not at this point.
1626 Otherwise, look at all our REG_DEAD notes. If a previous insn does
1627 nothing other than set a register that dies in this insn, we can delete
1630 On machines with CC0, if CC0 is used in this insn, we may be able to
1631 delete the insn that set it. */
1634 delete_computation (rtx insn
)
1639 if (reg_referenced_p (cc0_rtx
, PATTERN (insn
)))
1641 rtx prev
= prev_nonnote_insn (insn
);
1642 /* We assume that at this stage
1643 CC's are always set explicitly
1644 and always immediately before the jump that
1645 will use them. So if the previous insn
1646 exists to set the CC's, delete it
1647 (unless it performs auto-increments, etc.). */
1648 if (prev
&& GET_CODE (prev
) == INSN
1649 && sets_cc0_p (PATTERN (prev
)))
1651 if (sets_cc0_p (PATTERN (prev
)) > 0
1652 && ! side_effects_p (PATTERN (prev
)))
1653 delete_computation (prev
);
1655 /* Otherwise, show that cc0 won't be used. */
1656 REG_NOTES (prev
) = gen_rtx_EXPR_LIST (REG_UNUSED
,
1657 cc0_rtx
, REG_NOTES (prev
));
1662 for (note
= REG_NOTES (insn
); note
; note
= next
)
1664 next
= XEXP (note
, 1);
1666 if (REG_NOTE_KIND (note
) != REG_DEAD
1667 /* Verify that the REG_NOTE is legitimate. */
1668 || GET_CODE (XEXP (note
, 0)) != REG
)
1671 delete_prior_computation (note
, insn
);
1674 delete_related_insns (insn
);
1677 /* Delete insn INSN from the chain of insns and update label ref counts
1678 and delete insns now unreachable.
1680 Returns the first insn after INSN that was not deleted.
1682 Usage of this instruction is deprecated. Use delete_insn instead and
1683 subsequent cfg_cleanup pass to delete unreachable code if needed. */
1686 delete_related_insns (rtx insn
)
1688 int was_code_label
= (GET_CODE (insn
) == CODE_LABEL
);
1690 rtx next
= NEXT_INSN (insn
), prev
= PREV_INSN (insn
);
1692 while (next
&& INSN_DELETED_P (next
))
1693 next
= NEXT_INSN (next
);
1695 /* This insn is already deleted => return first following nondeleted. */
1696 if (INSN_DELETED_P (insn
))
1701 /* If instruction is followed by a barrier,
1702 delete the barrier too. */
1704 if (next
!= 0 && GET_CODE (next
) == BARRIER
)
1707 /* If deleting a jump, decrement the count of the label,
1708 and delete the label if it is now unused. */
1710 if (GET_CODE (insn
) == JUMP_INSN
&& JUMP_LABEL (insn
))
1712 rtx lab
= JUMP_LABEL (insn
), lab_next
;
1714 if (LABEL_NUSES (lab
) == 0)
1716 /* This can delete NEXT or PREV,
1717 either directly if NEXT is JUMP_LABEL (INSN),
1718 or indirectly through more levels of jumps. */
1719 delete_related_insns (lab
);
1721 /* I feel a little doubtful about this loop,
1722 but I see no clean and sure alternative way
1723 to find the first insn after INSN that is not now deleted.
1724 I hope this works. */
1725 while (next
&& INSN_DELETED_P (next
))
1726 next
= NEXT_INSN (next
);
1729 else if (tablejump_p (insn
, NULL
, &lab_next
))
1731 /* If we're deleting the tablejump, delete the dispatch table.
1732 We may not be able to kill the label immediately preceding
1733 just yet, as it might be referenced in code leading up to
1735 delete_related_insns (lab_next
);
1739 /* Likewise if we're deleting a dispatch table. */
1741 if (GET_CODE (insn
) == JUMP_INSN
1742 && (GET_CODE (PATTERN (insn
)) == ADDR_VEC
1743 || GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
))
1745 rtx pat
= PATTERN (insn
);
1746 int i
, diff_vec_p
= GET_CODE (pat
) == ADDR_DIFF_VEC
;
1747 int len
= XVECLEN (pat
, diff_vec_p
);
1749 for (i
= 0; i
< len
; i
++)
1750 if (LABEL_NUSES (XEXP (XVECEXP (pat
, diff_vec_p
, i
), 0)) == 0)
1751 delete_related_insns (XEXP (XVECEXP (pat
, diff_vec_p
, i
), 0));
1752 while (next
&& INSN_DELETED_P (next
))
1753 next
= NEXT_INSN (next
);
1757 /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note. */
1758 if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == CALL_INSN
)
1759 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
1760 if (REG_NOTE_KIND (note
) == REG_LABEL
1761 /* This could also be a NOTE_INSN_DELETED_LABEL note. */
1762 && GET_CODE (XEXP (note
, 0)) == CODE_LABEL
)
1763 if (LABEL_NUSES (XEXP (note
, 0)) == 0)
1764 delete_related_insns (XEXP (note
, 0));
1766 while (prev
&& (INSN_DELETED_P (prev
) || GET_CODE (prev
) == NOTE
))
1767 prev
= PREV_INSN (prev
);
1769 /* If INSN was a label and a dispatch table follows it,
1770 delete the dispatch table. The tablejump must have gone already.
1771 It isn't useful to fall through into a table. */
1774 && NEXT_INSN (insn
) != 0
1775 && GET_CODE (NEXT_INSN (insn
)) == JUMP_INSN
1776 && (GET_CODE (PATTERN (NEXT_INSN (insn
))) == ADDR_VEC
1777 || GET_CODE (PATTERN (NEXT_INSN (insn
))) == ADDR_DIFF_VEC
))
1778 next
= delete_related_insns (NEXT_INSN (insn
));
1780 /* If INSN was a label, delete insns following it if now unreachable. */
1782 if (was_code_label
&& prev
&& GET_CODE (prev
) == BARRIER
)
1786 && (GET_RTX_CLASS (code
= GET_CODE (next
)) == 'i'
1787 || code
== NOTE
|| code
== BARRIER
1788 || (code
== CODE_LABEL
&& INSN_DELETED_P (next
))))
1791 && NOTE_LINE_NUMBER (next
) != NOTE_INSN_FUNCTION_END
)
1792 next
= NEXT_INSN (next
);
1793 /* Keep going past other deleted labels to delete what follows. */
1794 else if (code
== CODE_LABEL
&& INSN_DELETED_P (next
))
1795 next
= NEXT_INSN (next
);
1797 /* Note: if this deletes a jump, it can cause more
1798 deletion of unreachable code, after a different label.
1799 As long as the value from this recursive call is correct,
1800 this invocation functions correctly. */
1801 next
= delete_related_insns (next
);
1808 /* Delete a range of insns from FROM to TO, inclusive.
1809 This is for the sake of peephole optimization, so assume
1810 that whatever these insns do will still be done by a new
1811 peephole insn that will replace them. */
1814 delete_for_peephole (rtx from
, rtx to
)
1820 rtx next
= NEXT_INSN (insn
);
1821 rtx prev
= PREV_INSN (insn
);
1823 if (GET_CODE (insn
) != NOTE
)
1825 INSN_DELETED_P (insn
) = 1;
1827 /* Patch this insn out of the chain. */
1828 /* We don't do this all at once, because we
1829 must preserve all NOTEs. */
1831 NEXT_INSN (prev
) = next
;
1834 PREV_INSN (next
) = prev
;
1842 /* Note that if TO is an unconditional jump
1843 we *do not* delete the BARRIER that follows,
1844 since the peephole that replaces this sequence
1845 is also an unconditional jump in that case. */
1848 /* We have determined that AVOIDED_INSN is never reached, and are
1849 about to delete it. If the insn chain between AVOIDED_INSN and
1850 FINISH contains more than one line from the current function, and
1851 contains at least one operation, print a warning if the user asked
1852 for it. If FINISH is NULL, look between AVOIDED_INSN and a LABEL.
1854 CSE and inlining can duplicate insns, so it's possible to get
1855 spurious warnings from this. */
1858 never_reached_warning (rtx avoided_insn
, rtx finish
)
1861 rtx a_line_note
= NULL
;
1862 int two_avoided_lines
= 0, contains_insn
= 0, reached_end
= 0;
1864 if (!warn_notreached
)
1867 /* Back up to the first of any NOTEs preceding avoided_insn; flow passes
1868 us the head of a block, a NOTE_INSN_BASIC_BLOCK, which often follows
1870 insn
= avoided_insn
;
1873 rtx prev
= PREV_INSN (insn
);
1874 if (prev
== NULL_RTX
1875 || GET_CODE (prev
) != NOTE
)
1880 /* Scan forwards, looking at LINE_NUMBER notes, until we hit a LABEL
1881 in case FINISH is NULL, otherwise until we run out of insns. */
1883 for (; insn
!= NULL
; insn
= NEXT_INSN (insn
))
1885 if ((finish
== NULL
&& GET_CODE (insn
) == CODE_LABEL
)
1886 || GET_CODE (insn
) == BARRIER
)
1889 if (GET_CODE (insn
) == NOTE
/* A line number note? */
1890 && NOTE_LINE_NUMBER (insn
) >= 0)
1892 if (a_line_note
== NULL
)
1895 two_avoided_lines
|= (NOTE_LINE_NUMBER (a_line_note
)
1896 != NOTE_LINE_NUMBER (insn
));
1898 else if (INSN_P (insn
))
1908 if (two_avoided_lines
&& contains_insn
)
1911 locus
.file
= NOTE_SOURCE_FILE (a_line_note
);
1912 locus
.line
= NOTE_LINE_NUMBER (a_line_note
);
1913 warning ("%Hwill never be executed", &locus
);
1917 /* Throughout LOC, redirect OLABEL to NLABEL. Treat null OLABEL or
1918 NLABEL as a return. Accrue modifications into the change group. */
1921 redirect_exp_1 (rtx
*loc
, rtx olabel
, rtx nlabel
, rtx insn
)
1924 RTX_CODE code
= GET_CODE (x
);
1928 if (code
== LABEL_REF
)
1930 if (XEXP (x
, 0) == olabel
)
1934 n
= gen_rtx_LABEL_REF (VOIDmode
, nlabel
);
1936 n
= gen_rtx_RETURN (VOIDmode
);
1938 validate_change (insn
, loc
, n
, 1);
1942 else if (code
== RETURN
&& olabel
== 0)
1944 x
= gen_rtx_LABEL_REF (VOIDmode
, nlabel
);
1945 if (loc
== &PATTERN (insn
))
1946 x
= gen_rtx_SET (VOIDmode
, pc_rtx
, x
);
1947 validate_change (insn
, loc
, x
, 1);
1951 if (code
== SET
&& nlabel
== 0 && SET_DEST (x
) == pc_rtx
1952 && GET_CODE (SET_SRC (x
)) == LABEL_REF
1953 && XEXP (SET_SRC (x
), 0) == olabel
)
1955 validate_change (insn
, loc
, gen_rtx_RETURN (VOIDmode
), 1);
1959 fmt
= GET_RTX_FORMAT (code
);
1960 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1963 redirect_exp_1 (&XEXP (x
, i
), olabel
, nlabel
, insn
);
1964 else if (fmt
[i
] == 'E')
1967 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1968 redirect_exp_1 (&XVECEXP (x
, i
, j
), olabel
, nlabel
, insn
);
1973 /* Similar, but apply the change group and report success or failure. */
1976 redirect_exp (rtx olabel
, rtx nlabel
, rtx insn
)
1980 if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
1981 loc
= &XVECEXP (PATTERN (insn
), 0, 0);
1983 loc
= &PATTERN (insn
);
1985 redirect_exp_1 (loc
, olabel
, nlabel
, insn
);
1986 if (num_validated_changes () == 0)
1989 return apply_change_group ();
1992 /* Make JUMP go to NLABEL instead of where it jumps now. Accrue
1993 the modifications into the change group. Return false if we did
1994 not see how to do that. */
1997 redirect_jump_1 (rtx jump
, rtx nlabel
)
1999 int ochanges
= num_validated_changes ();
2002 if (GET_CODE (PATTERN (jump
)) == PARALLEL
)
2003 loc
= &XVECEXP (PATTERN (jump
), 0, 0);
2005 loc
= &PATTERN (jump
);
2007 redirect_exp_1 (loc
, JUMP_LABEL (jump
), nlabel
, jump
);
2008 return num_validated_changes () > ochanges
;
2011 /* Make JUMP go to NLABEL instead of where it jumps now. If the old
2012 jump target label is unused as a result, it and the code following
2015 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
2018 The return value will be 1 if the change was made, 0 if it wasn't
2019 (this can only occur for NLABEL == 0). */
2022 redirect_jump (rtx jump
, rtx nlabel
, int delete_unused
)
2024 rtx olabel
= JUMP_LABEL (jump
);
2027 if (nlabel
== olabel
)
2030 if (! redirect_exp (olabel
, nlabel
, jump
))
2033 JUMP_LABEL (jump
) = nlabel
;
2035 ++LABEL_NUSES (nlabel
);
2037 /* Update labels in any REG_EQUAL note. */
2038 if ((note
= find_reg_note (jump
, REG_EQUAL
, NULL_RTX
)) != NULL_RTX
)
2040 if (nlabel
&& olabel
)
2042 rtx dest
= XEXP (note
, 0);
2044 if (GET_CODE (dest
) == IF_THEN_ELSE
)
2046 if (GET_CODE (XEXP (dest
, 1)) == LABEL_REF
2047 && XEXP (XEXP (dest
, 1), 0) == olabel
)
2048 XEXP (XEXP (dest
, 1), 0) = nlabel
;
2049 if (GET_CODE (XEXP (dest
, 2)) == LABEL_REF
2050 && XEXP (XEXP (dest
, 2), 0) == olabel
)
2051 XEXP (XEXP (dest
, 2), 0) = nlabel
;
2054 remove_note (jump
, note
);
2057 remove_note (jump
, note
);
2060 /* If we're eliding the jump over exception cleanups at the end of a
2061 function, move the function end note so that -Wreturn-type works. */
2062 if (olabel
&& nlabel
2063 && NEXT_INSN (olabel
)
2064 && GET_CODE (NEXT_INSN (olabel
)) == NOTE
2065 && NOTE_LINE_NUMBER (NEXT_INSN (olabel
)) == NOTE_INSN_FUNCTION_END
)
2066 emit_note_after (NOTE_INSN_FUNCTION_END
, nlabel
);
2068 if (olabel
&& --LABEL_NUSES (olabel
) == 0 && delete_unused
2069 /* Undefined labels will remain outside the insn stream. */
2070 && INSN_UID (olabel
))
2071 delete_related_insns (olabel
);
2076 /* Invert the jump condition of rtx X contained in jump insn, INSN.
2077 Accrue the modifications into the change group. */
2080 invert_exp_1 (rtx insn
)
2083 rtx x
= pc_set (insn
);
2089 code
= GET_CODE (x
);
2091 if (code
== IF_THEN_ELSE
)
2093 rtx comp
= XEXP (x
, 0);
2095 enum rtx_code reversed_code
;
2097 /* We can do this in two ways: The preferable way, which can only
2098 be done if this is not an integer comparison, is to reverse
2099 the comparison code. Otherwise, swap the THEN-part and ELSE-part
2100 of the IF_THEN_ELSE. If we can't do either, fail. */
2102 reversed_code
= reversed_comparison_code (comp
, insn
);
2104 if (reversed_code
!= UNKNOWN
)
2106 validate_change (insn
, &XEXP (x
, 0),
2107 gen_rtx_fmt_ee (reversed_code
,
2108 GET_MODE (comp
), XEXP (comp
, 0),
2115 validate_change (insn
, &XEXP (x
, 1), XEXP (x
, 2), 1);
2116 validate_change (insn
, &XEXP (x
, 2), tem
, 1);
2122 /* Invert the jump condition of conditional jump insn, INSN.
2124 Return 1 if we can do so, 0 if we cannot find a way to do so that
2125 matches a pattern. */
2128 invert_exp (rtx insn
)
2130 invert_exp_1 (insn
);
2131 if (num_validated_changes () == 0)
2134 return apply_change_group ();
2137 /* Invert the condition of the jump JUMP, and make it jump to label
2138 NLABEL instead of where it jumps now. Accrue changes into the
2139 change group. Return false if we didn't see how to perform the
2140 inversion and redirection. */
2143 invert_jump_1 (rtx jump
, rtx nlabel
)
2147 ochanges
= num_validated_changes ();
2148 invert_exp_1 (jump
);
2149 if (num_validated_changes () == ochanges
)
2152 return redirect_jump_1 (jump
, nlabel
);
2155 /* Invert the condition of the jump JUMP, and make it jump to label
2156 NLABEL instead of where it jumps now. Return true if successful. */
2159 invert_jump (rtx jump
, rtx nlabel
, int delete_unused
)
2161 /* We have to either invert the condition and change the label or
2162 do neither. Either operation could fail. We first try to invert
2163 the jump. If that succeeds, we try changing the label. If that fails,
2164 we invert the jump back to what it was. */
2166 if (! invert_exp (jump
))
2169 if (redirect_jump (jump
, nlabel
, delete_unused
))
2171 /* Remove REG_EQUAL note if we have one. */
2172 rtx note
= find_reg_note (jump
, REG_EQUAL
, NULL_RTX
);
2174 remove_note (jump
, note
);
2176 invert_br_probabilities (jump
);
2181 if (! invert_exp (jump
))
2182 /* This should just be putting it back the way it was. */
2189 /* Like rtx_equal_p except that it considers two REGs as equal
2190 if they renumber to the same value and considers two commutative
2191 operations to be the same if the order of the operands has been
2194 ??? Addition is not commutative on the PA due to the weird implicit
2195 space register selection rules for memory addresses. Therefore, we
2196 don't consider a + b == b + a.
2198 We could/should make this test a little tighter. Possibly only
2199 disabling it on the PA via some backend macro or only disabling this
2200 case when the PLUS is inside a MEM. */
2203 rtx_renumbered_equal_p (rtx x
, rtx y
)
2206 RTX_CODE code
= GET_CODE (x
);
2212 if ((code
== REG
|| (code
== SUBREG
&& GET_CODE (SUBREG_REG (x
)) == REG
))
2213 && (GET_CODE (y
) == REG
|| (GET_CODE (y
) == SUBREG
2214 && GET_CODE (SUBREG_REG (y
)) == REG
)))
2216 int reg_x
= -1, reg_y
= -1;
2217 int byte_x
= 0, byte_y
= 0;
2219 if (GET_MODE (x
) != GET_MODE (y
))
2222 /* If we haven't done any renumbering, don't
2223 make any assumptions. */
2224 if (reg_renumber
== 0)
2225 return rtx_equal_p (x
, y
);
2229 reg_x
= REGNO (SUBREG_REG (x
));
2230 byte_x
= SUBREG_BYTE (x
);
2232 if (reg_renumber
[reg_x
] >= 0)
2234 reg_x
= subreg_regno_offset (reg_renumber
[reg_x
],
2235 GET_MODE (SUBREG_REG (x
)),
2244 if (reg_renumber
[reg_x
] >= 0)
2245 reg_x
= reg_renumber
[reg_x
];
2248 if (GET_CODE (y
) == SUBREG
)
2250 reg_y
= REGNO (SUBREG_REG (y
));
2251 byte_y
= SUBREG_BYTE (y
);
2253 if (reg_renumber
[reg_y
] >= 0)
2255 reg_y
= subreg_regno_offset (reg_renumber
[reg_y
],
2256 GET_MODE (SUBREG_REG (y
)),
2265 if (reg_renumber
[reg_y
] >= 0)
2266 reg_y
= reg_renumber
[reg_y
];
2269 return reg_x
>= 0 && reg_x
== reg_y
&& byte_x
== byte_y
;
2272 /* Now we have disposed of all the cases
2273 in which different rtx codes can match. */
2274 if (code
!= GET_CODE (y
))
2287 /* We can't assume nonlocal labels have their following insns yet. */
2288 if (LABEL_REF_NONLOCAL_P (x
) || LABEL_REF_NONLOCAL_P (y
))
2289 return XEXP (x
, 0) == XEXP (y
, 0);
2291 /* Two label-refs are equivalent if they point at labels
2292 in the same position in the instruction stream. */
2293 return (next_real_insn (XEXP (x
, 0))
2294 == next_real_insn (XEXP (y
, 0)));
2297 return XSTR (x
, 0) == XSTR (y
, 0);
2300 /* If we didn't match EQ equality above, they aren't the same. */
2307 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
2309 if (GET_MODE (x
) != GET_MODE (y
))
2312 /* For commutative operations, the RTX match if the operand match in any
2313 order. Also handle the simple binary and unary cases without a loop.
2315 ??? Don't consider PLUS a commutative operator; see comments above. */
2316 if ((code
== EQ
|| code
== NE
|| GET_RTX_CLASS (code
) == 'c')
2318 return ((rtx_renumbered_equal_p (XEXP (x
, 0), XEXP (y
, 0))
2319 && rtx_renumbered_equal_p (XEXP (x
, 1), XEXP (y
, 1)))
2320 || (rtx_renumbered_equal_p (XEXP (x
, 0), XEXP (y
, 1))
2321 && rtx_renumbered_equal_p (XEXP (x
, 1), XEXP (y
, 0))));
2322 else if (GET_RTX_CLASS (code
) == '<' || GET_RTX_CLASS (code
) == '2')
2323 return (rtx_renumbered_equal_p (XEXP (x
, 0), XEXP (y
, 0))
2324 && rtx_renumbered_equal_p (XEXP (x
, 1), XEXP (y
, 1)));
2325 else if (GET_RTX_CLASS (code
) == '1')
2326 return rtx_renumbered_equal_p (XEXP (x
, 0), XEXP (y
, 0));
2328 /* Compare the elements. If any pair of corresponding elements
2329 fail to match, return 0 for the whole things. */
2331 fmt
= GET_RTX_FORMAT (code
);
2332 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2338 if (XWINT (x
, i
) != XWINT (y
, i
))
2343 if (XINT (x
, i
) != XINT (y
, i
))
2348 if (XTREE (x
, i
) != XTREE (y
, i
))
2353 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
2358 if (! rtx_renumbered_equal_p (XEXP (x
, i
), XEXP (y
, i
)))
2363 if (XEXP (x
, i
) != XEXP (y
, i
))
2370 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
2372 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
2373 if (!rtx_renumbered_equal_p (XVECEXP (x
, i
, j
), XVECEXP (y
, i
, j
)))
2384 /* If X is a hard register or equivalent to one or a subregister of one,
2385 return the hard register number. If X is a pseudo register that was not
2386 assigned a hard register, return the pseudo register number. Otherwise,
2387 return -1. Any rtx is valid for X. */
2392 if (GET_CODE (x
) == REG
)
2394 if (REGNO (x
) >= FIRST_PSEUDO_REGISTER
&& reg_renumber
[REGNO (x
)] >= 0)
2395 return reg_renumber
[REGNO (x
)];
2398 if (GET_CODE (x
) == SUBREG
)
2400 int base
= true_regnum (SUBREG_REG (x
));
2401 if (base
>= 0 && base
< FIRST_PSEUDO_REGISTER
)
2402 return base
+ subreg_regno_offset (REGNO (SUBREG_REG (x
)),
2403 GET_MODE (SUBREG_REG (x
)),
2404 SUBREG_BYTE (x
), GET_MODE (x
));
2409 /* Return regno of the register REG and handle subregs too. */
2411 reg_or_subregno (rtx reg
)
2415 if (GET_CODE (reg
) == SUBREG
)
2416 return REGNO (SUBREG_REG (reg
));