* MAINTAINERS: Update my email address.
[official-gcc.git] / gcc / jump.c
blobb84841c1754daa767795b03e029ac8ee91076455
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, 2004 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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
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
24 operate with jumps.
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
32 at by later passes.
34 The subroutines redirect_jump and invert_jump are used
35 from other passes as well. */
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "rtl.h"
42 #include "tm_p.h"
43 #include "flags.h"
44 #include "hard-reg-set.h"
45 #include "regs.h"
46 #include "insn-config.h"
47 #include "insn-attr.h"
48 #include "recog.h"
49 #include "function.h"
50 #include "expr.h"
51 #include "real.h"
52 #include "except.h"
53 #include "diagnostic.h"
54 #include "toplev.h"
55 #include "reload.h"
56 #include "predict.h"
57 #include "timevar.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 void init_label_info (rtx);
67 static void mark_all_labels (rtx);
68 static void delete_computation (rtx);
69 static void redirect_exp_1 (rtx *, rtx, rtx, rtx);
70 static int redirect_exp (rtx, rtx, rtx);
71 static void invert_exp_1 (rtx);
72 static int invert_exp (rtx);
73 static int returnjump_p_1 (rtx *, void *);
74 static void delete_prior_computation (rtx, rtx);
76 /* Alternate entry into the jump optimizer. This entry point only rebuilds
77 the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
78 instructions. */
79 void
80 rebuild_jump_labels (rtx f)
82 rtx insn;
84 timevar_push (TV_REBUILD_JUMP);
85 init_label_info (f);
86 mark_all_labels (f);
88 /* Keep track of labels used from static data; we don't track them
89 closely enough to delete them here, so make sure their reference
90 count doesn't drop to zero. */
92 for (insn = forced_labels; insn; insn = XEXP (insn, 1))
93 if (LABEL_P (XEXP (insn, 0)))
94 LABEL_NUSES (XEXP (insn, 0))++;
95 timevar_pop (TV_REBUILD_JUMP);
98 /* Some old code expects exactly one BARRIER as the NEXT_INSN of a
99 non-fallthru insn. This is not generally true, as multiple barriers
100 may have crept in, or the BARRIER may be separated from the last
101 real insn by one or more NOTEs.
103 This simple pass moves barriers and removes duplicates so that the
104 old code is happy.
106 void
107 cleanup_barriers (void)
109 rtx insn, next, prev;
110 for (insn = get_insns (); insn; insn = next)
112 next = NEXT_INSN (insn);
113 if (BARRIER_P (insn))
115 prev = prev_nonnote_insn (insn);
116 if (BARRIER_P (prev))
117 delete_barrier (insn);
118 else if (prev != PREV_INSN (insn))
119 reorder_insns (insn, insn, prev);
124 void
125 purge_line_number_notes (rtx f)
127 rtx last_note = 0;
128 rtx insn;
129 /* Delete extraneous line number notes.
130 Note that two consecutive notes for different lines are not really
131 extraneous. There should be some indication where that line belonged,
132 even if it became empty. */
134 for (insn = f; insn; insn = NEXT_INSN (insn))
135 if (NOTE_P (insn))
137 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
138 /* Any previous line note was for the prologue; gdb wants a new
139 note after the prologue even if it is for the same line. */
140 last_note = NULL_RTX;
141 else if (NOTE_LINE_NUMBER (insn) >= 0)
143 /* Delete this note if it is identical to previous note. */
144 if (last_note
145 #ifdef USE_MAPPED_LOCATION
146 && NOTE_SOURCE_LOCATION (insn) == NOTE_SOURCE_LOCATION (last_note)
147 #else
148 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
149 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note)
150 #endif
153 delete_related_insns (insn);
154 continue;
157 last_note = insn;
162 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
163 notes whose labels don't occur in the insn any more. Returns the
164 largest INSN_UID found. */
165 static void
166 init_label_info (rtx f)
168 rtx insn;
170 for (insn = f; insn; insn = NEXT_INSN (insn))
171 if (LABEL_P (insn))
172 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
173 else if (JUMP_P (insn))
174 JUMP_LABEL (insn) = 0;
175 else if (NONJUMP_INSN_P (insn) || CALL_P (insn))
177 rtx note, next;
179 for (note = REG_NOTES (insn); note; note = next)
181 next = XEXP (note, 1);
182 if (REG_NOTE_KIND (note) == REG_LABEL
183 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
184 remove_note (insn, note);
189 /* Mark the label each jump jumps to.
190 Combine consecutive labels, and count uses of labels. */
192 static void
193 mark_all_labels (rtx f)
195 rtx insn;
197 for (insn = f; insn; insn = NEXT_INSN (insn))
198 if (INSN_P (insn))
200 mark_jump_label (PATTERN (insn), insn, 0);
201 if (! INSN_DELETED_P (insn) && JUMP_P (insn))
203 /* When we know the LABEL_REF contained in a REG used in
204 an indirect jump, we'll have a REG_LABEL note so that
205 flow can tell where it's going. */
206 if (JUMP_LABEL (insn) == 0)
208 rtx label_note = find_reg_note (insn, REG_LABEL, NULL_RTX);
209 if (label_note)
211 /* But a LABEL_REF around the REG_LABEL note, so
212 that we can canonicalize it. */
213 rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
214 XEXP (label_note, 0));
216 mark_jump_label (label_ref, insn, 0);
217 XEXP (label_note, 0) = XEXP (label_ref, 0);
218 JUMP_LABEL (insn) = XEXP (label_note, 0);
225 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
226 notes between START and END out before START. START and END may be such
227 notes. Returns the values of the new starting and ending insns, which
228 may be different if the original ones were such notes.
229 Return true if there were only such notes and no real instructions. */
231 bool
232 squeeze_notes (rtx* startp, rtx* endp)
234 rtx start = *startp;
235 rtx end = *endp;
237 rtx insn;
238 rtx next;
239 rtx last = NULL;
240 rtx past_end = NEXT_INSN (end);
242 for (insn = start; insn != past_end; insn = next)
244 next = NEXT_INSN (insn);
245 if (NOTE_P (insn)
246 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
247 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
248 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
249 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END))
251 if (insn == start)
252 start = next;
253 else
255 rtx prev = PREV_INSN (insn);
256 PREV_INSN (insn) = PREV_INSN (start);
257 NEXT_INSN (insn) = start;
258 NEXT_INSN (PREV_INSN (insn)) = insn;
259 PREV_INSN (NEXT_INSN (insn)) = insn;
260 NEXT_INSN (prev) = next;
261 PREV_INSN (next) = prev;
264 else
265 last = insn;
268 /* There were no real instructions. */
269 if (start == past_end)
270 return true;
272 end = last;
274 *startp = start;
275 *endp = end;
276 return false;
279 /* Return the label before INSN, or put a new label there. */
282 get_label_before (rtx insn)
284 rtx label;
286 /* Find an existing label at this point
287 or make a new one if there is none. */
288 label = prev_nonnote_insn (insn);
290 if (label == 0 || !LABEL_P (label))
292 rtx prev = PREV_INSN (insn);
294 label = gen_label_rtx ();
295 emit_label_after (label, prev);
296 LABEL_NUSES (label) = 0;
298 return label;
301 /* Return the label after INSN, or put a new label there. */
304 get_label_after (rtx insn)
306 rtx label;
308 /* Find an existing label at this point
309 or make a new one if there is none. */
310 label = next_nonnote_insn (insn);
312 if (label == 0 || !LABEL_P (label))
314 label = gen_label_rtx ();
315 emit_label_after (label, insn);
316 LABEL_NUSES (label) = 0;
318 return label;
321 /* Given a comparison (CODE ARG0 ARG1), inside an insn, INSN, return a code
322 of reversed comparison if it is possible to do so. Otherwise return UNKNOWN.
323 UNKNOWN may be returned in case we are having CC_MODE compare and we don't
324 know whether it's source is floating point or integer comparison. Machine
325 description should define REVERSIBLE_CC_MODE and REVERSE_CONDITION macros
326 to help this function avoid overhead in these cases. */
327 enum rtx_code
328 reversed_comparison_code_parts (enum rtx_code code, rtx arg0, rtx arg1, rtx insn)
330 enum machine_mode mode;
332 /* If this is not actually a comparison, we can't reverse it. */
333 if (GET_RTX_CLASS (code) != RTX_COMPARE
334 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE)
335 return UNKNOWN;
337 mode = GET_MODE (arg0);
338 if (mode == VOIDmode)
339 mode = GET_MODE (arg1);
341 /* First see if machine description supplies us way to reverse the
342 comparison. Give it priority over everything else to allow
343 machine description to do tricks. */
344 if (GET_MODE_CLASS (mode) == MODE_CC
345 && REVERSIBLE_CC_MODE (mode))
347 #ifdef REVERSE_CONDITION
348 return REVERSE_CONDITION (code, mode);
349 #endif
350 return reverse_condition (code);
353 /* Try a few special cases based on the comparison code. */
354 switch (code)
356 case GEU:
357 case GTU:
358 case LEU:
359 case LTU:
360 case NE:
361 case EQ:
362 /* It is always safe to reverse EQ and NE, even for the floating
363 point. Similarly the unsigned comparisons are never used for
364 floating point so we can reverse them in the default way. */
365 return reverse_condition (code);
366 case ORDERED:
367 case UNORDERED:
368 case LTGT:
369 case UNEQ:
370 /* In case we already see unordered comparison, we can be sure to
371 be dealing with floating point so we don't need any more tests. */
372 return reverse_condition_maybe_unordered (code);
373 case UNLT:
374 case UNLE:
375 case UNGT:
376 case UNGE:
377 /* We don't have safe way to reverse these yet. */
378 return UNKNOWN;
379 default:
380 break;
383 if (GET_MODE_CLASS (mode) == MODE_CC || CC0_P (arg0))
385 rtx prev;
386 /* Try to search for the comparison to determine the real mode.
387 This code is expensive, but with sane machine description it
388 will be never used, since REVERSIBLE_CC_MODE will return true
389 in all cases. */
390 if (! insn)
391 return UNKNOWN;
393 for (prev = prev_nonnote_insn (insn);
394 prev != 0 && !LABEL_P (prev);
395 prev = prev_nonnote_insn (prev))
397 rtx set = set_of (arg0, prev);
398 if (set && GET_CODE (set) == SET
399 && rtx_equal_p (SET_DEST (set), arg0))
401 rtx src = SET_SRC (set);
403 if (GET_CODE (src) == COMPARE)
405 rtx comparison = src;
406 arg0 = XEXP (src, 0);
407 mode = GET_MODE (arg0);
408 if (mode == VOIDmode)
409 mode = GET_MODE (XEXP (comparison, 1));
410 break;
412 /* We can get past reg-reg moves. This may be useful for model
413 of i387 comparisons that first move flag registers around. */
414 if (REG_P (src))
416 arg0 = src;
417 continue;
420 /* If register is clobbered in some ununderstandable way,
421 give up. */
422 if (set)
423 return UNKNOWN;
427 /* Test for an integer condition, or a floating-point comparison
428 in which NaNs can be ignored. */
429 if (GET_CODE (arg0) == CONST_INT
430 || (GET_MODE (arg0) != VOIDmode
431 && GET_MODE_CLASS (mode) != MODE_CC
432 && !HONOR_NANS (mode)))
433 return reverse_condition (code);
435 return UNKNOWN;
438 /* A wrapper around the previous function to take COMPARISON as rtx
439 expression. This simplifies many callers. */
440 enum rtx_code
441 reversed_comparison_code (rtx comparison, rtx insn)
443 if (!COMPARISON_P (comparison))
444 return UNKNOWN;
445 return reversed_comparison_code_parts (GET_CODE (comparison),
446 XEXP (comparison, 0),
447 XEXP (comparison, 1), insn);
450 /* Given an rtx-code for a comparison, return the code for the negated
451 comparison. If no such code exists, return UNKNOWN.
453 WATCH OUT! reverse_condition is not safe to use on a jump that might
454 be acting on the results of an IEEE floating point comparison, because
455 of the special treatment of non-signaling nans in comparisons.
456 Use reversed_comparison_code instead. */
458 enum rtx_code
459 reverse_condition (enum rtx_code code)
461 switch (code)
463 case EQ:
464 return NE;
465 case NE:
466 return EQ;
467 case GT:
468 return LE;
469 case GE:
470 return LT;
471 case LT:
472 return GE;
473 case LE:
474 return GT;
475 case GTU:
476 return LEU;
477 case GEU:
478 return LTU;
479 case LTU:
480 return GEU;
481 case LEU:
482 return GTU;
483 case UNORDERED:
484 return ORDERED;
485 case ORDERED:
486 return UNORDERED;
488 case UNLT:
489 case UNLE:
490 case UNGT:
491 case UNGE:
492 case UNEQ:
493 case LTGT:
494 return UNKNOWN;
496 default:
497 abort ();
501 /* Similar, but we're allowed to generate unordered comparisons, which
502 makes it safe for IEEE floating-point. Of course, we have to recognize
503 that the target will support them too... */
505 enum rtx_code
506 reverse_condition_maybe_unordered (enum rtx_code code)
508 switch (code)
510 case EQ:
511 return NE;
512 case NE:
513 return EQ;
514 case GT:
515 return UNLE;
516 case GE:
517 return UNLT;
518 case LT:
519 return UNGE;
520 case LE:
521 return UNGT;
522 case LTGT:
523 return UNEQ;
524 case UNORDERED:
525 return ORDERED;
526 case ORDERED:
527 return UNORDERED;
528 case UNLT:
529 return GE;
530 case UNLE:
531 return GT;
532 case UNGT:
533 return LE;
534 case UNGE:
535 return LT;
536 case UNEQ:
537 return LTGT;
539 default:
540 abort ();
544 /* Similar, but return the code when two operands of a comparison are swapped.
545 This IS safe for IEEE floating-point. */
547 enum rtx_code
548 swap_condition (enum rtx_code code)
550 switch (code)
552 case EQ:
553 case NE:
554 case UNORDERED:
555 case ORDERED:
556 case UNEQ:
557 case LTGT:
558 return code;
560 case GT:
561 return LT;
562 case GE:
563 return LE;
564 case LT:
565 return GT;
566 case LE:
567 return GE;
568 case GTU:
569 return LTU;
570 case GEU:
571 return LEU;
572 case LTU:
573 return GTU;
574 case LEU:
575 return GEU;
576 case UNLT:
577 return UNGT;
578 case UNLE:
579 return UNGE;
580 case UNGT:
581 return UNLT;
582 case UNGE:
583 return UNLE;
585 default:
586 abort ();
590 /* Given a comparison CODE, return the corresponding unsigned comparison.
591 If CODE is an equality comparison or already an unsigned comparison,
592 CODE is returned. */
594 enum rtx_code
595 unsigned_condition (enum rtx_code code)
597 switch (code)
599 case EQ:
600 case NE:
601 case GTU:
602 case GEU:
603 case LTU:
604 case LEU:
605 return code;
607 case GT:
608 return GTU;
609 case GE:
610 return GEU;
611 case LT:
612 return LTU;
613 case LE:
614 return LEU;
616 default:
617 abort ();
621 /* Similarly, return the signed version of a comparison. */
623 enum rtx_code
624 signed_condition (enum rtx_code code)
626 switch (code)
628 case EQ:
629 case NE:
630 case GT:
631 case GE:
632 case LT:
633 case LE:
634 return code;
636 case GTU:
637 return GT;
638 case GEU:
639 return GE;
640 case LTU:
641 return LT;
642 case LEU:
643 return LE;
645 default:
646 abort ();
650 /* Return nonzero if CODE1 is more strict than CODE2, i.e., if the
651 truth of CODE1 implies the truth of CODE2. */
654 comparison_dominates_p (enum rtx_code code1, enum rtx_code code2)
656 /* UNKNOWN comparison codes can happen as a result of trying to revert
657 comparison codes.
658 They can't match anything, so we have to reject them here. */
659 if (code1 == UNKNOWN || code2 == UNKNOWN)
660 return 0;
662 if (code1 == code2)
663 return 1;
665 switch (code1)
667 case UNEQ:
668 if (code2 == UNLE || code2 == UNGE)
669 return 1;
670 break;
672 case EQ:
673 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU
674 || code2 == ORDERED)
675 return 1;
676 break;
678 case UNLT:
679 if (code2 == UNLE || code2 == NE)
680 return 1;
681 break;
683 case LT:
684 if (code2 == LE || code2 == NE || code2 == ORDERED || code2 == LTGT)
685 return 1;
686 break;
688 case UNGT:
689 if (code2 == UNGE || code2 == NE)
690 return 1;
691 break;
693 case GT:
694 if (code2 == GE || code2 == NE || code2 == ORDERED || code2 == LTGT)
695 return 1;
696 break;
698 case GE:
699 case LE:
700 if (code2 == ORDERED)
701 return 1;
702 break;
704 case LTGT:
705 if (code2 == NE || code2 == ORDERED)
706 return 1;
707 break;
709 case LTU:
710 if (code2 == LEU || code2 == NE)
711 return 1;
712 break;
714 case GTU:
715 if (code2 == GEU || code2 == NE)
716 return 1;
717 break;
719 case UNORDERED:
720 if (code2 == NE || code2 == UNEQ || code2 == UNLE || code2 == UNLT
721 || code2 == UNGE || code2 == UNGT)
722 return 1;
723 break;
725 default:
726 break;
729 return 0;
732 /* Return 1 if INSN is an unconditional jump and nothing else. */
735 simplejump_p (rtx insn)
737 return (JUMP_P (insn)
738 && GET_CODE (PATTERN (insn)) == SET
739 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
740 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
743 /* Return nonzero if INSN is a (possibly) conditional jump
744 and nothing more.
746 Use of this function is deprecated, since we need to support combined
747 branch and compare insns. Use any_condjump_p instead whenever possible. */
750 condjump_p (rtx insn)
752 rtx x = PATTERN (insn);
754 if (GET_CODE (x) != SET
755 || GET_CODE (SET_DEST (x)) != PC)
756 return 0;
758 x = SET_SRC (x);
759 if (GET_CODE (x) == LABEL_REF)
760 return 1;
761 else
762 return (GET_CODE (x) == IF_THEN_ELSE
763 && ((GET_CODE (XEXP (x, 2)) == PC
764 && (GET_CODE (XEXP (x, 1)) == LABEL_REF
765 || GET_CODE (XEXP (x, 1)) == RETURN))
766 || (GET_CODE (XEXP (x, 1)) == PC
767 && (GET_CODE (XEXP (x, 2)) == LABEL_REF
768 || GET_CODE (XEXP (x, 2)) == RETURN))));
770 return 0;
773 /* Return nonzero if INSN is a (possibly) conditional jump inside a
774 PARALLEL.
776 Use this function is deprecated, since we need to support combined
777 branch and compare insns. Use any_condjump_p instead whenever possible. */
780 condjump_in_parallel_p (rtx insn)
782 rtx x = PATTERN (insn);
784 if (GET_CODE (x) != PARALLEL)
785 return 0;
786 else
787 x = XVECEXP (x, 0, 0);
789 if (GET_CODE (x) != SET)
790 return 0;
791 if (GET_CODE (SET_DEST (x)) != PC)
792 return 0;
793 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
794 return 1;
795 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
796 return 0;
797 if (XEXP (SET_SRC (x), 2) == pc_rtx
798 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
799 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
800 return 1;
801 if (XEXP (SET_SRC (x), 1) == pc_rtx
802 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
803 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
804 return 1;
805 return 0;
808 /* Return set of PC, otherwise NULL. */
811 pc_set (rtx insn)
813 rtx pat;
814 if (!JUMP_P (insn))
815 return NULL_RTX;
816 pat = PATTERN (insn);
818 /* The set is allowed to appear either as the insn pattern or
819 the first set in a PARALLEL. */
820 if (GET_CODE (pat) == PARALLEL)
821 pat = XVECEXP (pat, 0, 0);
822 if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == PC)
823 return pat;
825 return NULL_RTX;
828 /* Return true when insn is an unconditional direct jump,
829 possibly bundled inside a PARALLEL. */
832 any_uncondjump_p (rtx insn)
834 rtx x = pc_set (insn);
835 if (!x)
836 return 0;
837 if (GET_CODE (SET_SRC (x)) != LABEL_REF)
838 return 0;
839 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
840 return 0;
841 return 1;
844 /* Return true when insn is a conditional jump. This function works for
845 instructions containing PC sets in PARALLELs. The instruction may have
846 various other effects so before removing the jump you must verify
847 onlyjump_p.
849 Note that unlike condjump_p it returns false for unconditional jumps. */
852 any_condjump_p (rtx insn)
854 rtx x = pc_set (insn);
855 enum rtx_code a, b;
857 if (!x)
858 return 0;
859 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
860 return 0;
862 a = GET_CODE (XEXP (SET_SRC (x), 1));
863 b = GET_CODE (XEXP (SET_SRC (x), 2));
865 return ((b == PC && (a == LABEL_REF || a == RETURN))
866 || (a == PC && (b == LABEL_REF || b == RETURN)));
869 /* Return the label of a conditional jump. */
872 condjump_label (rtx insn)
874 rtx x = pc_set (insn);
876 if (!x)
877 return NULL_RTX;
878 x = SET_SRC (x);
879 if (GET_CODE (x) == LABEL_REF)
880 return x;
881 if (GET_CODE (x) != IF_THEN_ELSE)
882 return NULL_RTX;
883 if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
884 return XEXP (x, 1);
885 if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
886 return XEXP (x, 2);
887 return NULL_RTX;
890 /* Return true if INSN is a (possibly conditional) return insn. */
892 static int
893 returnjump_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
895 rtx x = *loc;
897 return x && (GET_CODE (x) == RETURN
898 || (GET_CODE (x) == SET && SET_IS_RETURN_P (x)));
902 returnjump_p (rtx insn)
904 if (!JUMP_P (insn))
905 return 0;
906 return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
909 /* Return true if INSN is a jump that only transfers control and
910 nothing more. */
913 onlyjump_p (rtx insn)
915 rtx set;
917 if (!JUMP_P (insn))
918 return 0;
920 set = single_set (insn);
921 if (set == NULL)
922 return 0;
923 if (GET_CODE (SET_DEST (set)) != PC)
924 return 0;
925 if (side_effects_p (SET_SRC (set)))
926 return 0;
928 return 1;
931 #ifdef HAVE_cc0
933 /* Return nonzero if X is an RTX that only sets the condition codes
934 and has no side effects. */
937 only_sets_cc0_p (rtx x)
939 if (! x)
940 return 0;
942 if (INSN_P (x))
943 x = PATTERN (x);
945 return sets_cc0_p (x) == 1 && ! side_effects_p (x);
948 /* Return 1 if X is an RTX that does nothing but set the condition codes
949 and CLOBBER or USE registers.
950 Return -1 if X does explicitly set the condition codes,
951 but also does other things. */
954 sets_cc0_p (rtx x)
956 if (! x)
957 return 0;
959 if (INSN_P (x))
960 x = PATTERN (x);
962 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
963 return 1;
964 if (GET_CODE (x) == PARALLEL)
966 int i;
967 int sets_cc0 = 0;
968 int other_things = 0;
969 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
971 if (GET_CODE (XVECEXP (x, 0, i)) == SET
972 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
973 sets_cc0 = 1;
974 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
975 other_things = 1;
977 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
979 return 0;
981 #endif
983 /* Follow any unconditional jump at LABEL;
984 return the ultimate label reached by any such chain of jumps.
985 Return null if the chain ultimately leads to a return instruction.
986 If LABEL is not followed by a jump, return LABEL.
987 If the chain loops or we can't find end, return LABEL,
988 since that tells caller to avoid changing the insn.
990 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
991 a USE or CLOBBER. */
994 follow_jumps (rtx label)
996 rtx insn;
997 rtx next;
998 rtx value = label;
999 int depth;
1001 for (depth = 0;
1002 (depth < 10
1003 && (insn = next_active_insn (value)) != 0
1004 && JUMP_P (insn)
1005 && ((JUMP_LABEL (insn) != 0 && any_uncondjump_p (insn)
1006 && onlyjump_p (insn))
1007 || GET_CODE (PATTERN (insn)) == RETURN)
1008 && (next = NEXT_INSN (insn))
1009 && BARRIER_P (next));
1010 depth++)
1012 /* Don't chain through the insn that jumps into a loop
1013 from outside the loop,
1014 since that would create multiple loop entry jumps
1015 and prevent loop optimization. */
1016 rtx tem;
1017 if (!reload_completed)
1018 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
1019 if (NOTE_P (tem)
1020 && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
1021 /* ??? Optional. Disables some optimizations, but makes
1022 gcov output more accurate with -O. */
1023 || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
1024 return value;
1026 /* If we have found a cycle, make the insn jump to itself. */
1027 if (JUMP_LABEL (insn) == label)
1028 return label;
1030 tem = next_active_insn (JUMP_LABEL (insn));
1031 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
1032 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
1033 break;
1035 value = JUMP_LABEL (insn);
1037 if (depth == 10)
1038 return label;
1039 return value;
1043 /* Find all CODE_LABELs referred to in X, and increment their use counts.
1044 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
1045 in INSN, then store one of them in JUMP_LABEL (INSN).
1046 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
1047 referenced in INSN, add a REG_LABEL note containing that label to INSN.
1048 Also, when there are consecutive labels, canonicalize on the last of them.
1050 Note that two labels separated by a loop-beginning note
1051 must be kept distinct if we have not yet done loop-optimization,
1052 because the gap between them is where loop-optimize
1053 will want to move invariant code to. CROSS_JUMP tells us
1054 that loop-optimization is done with. */
1056 void
1057 mark_jump_label (rtx x, rtx insn, int in_mem)
1059 RTX_CODE code = GET_CODE (x);
1060 int i;
1061 const char *fmt;
1063 switch (code)
1065 case PC:
1066 case CC0:
1067 case REG:
1068 case CONST_INT:
1069 case CONST_DOUBLE:
1070 case CLOBBER:
1071 case CALL:
1072 return;
1074 case MEM:
1075 in_mem = 1;
1076 break;
1078 case SYMBOL_REF:
1079 if (!in_mem)
1080 return;
1082 /* If this is a constant-pool reference, see if it is a label. */
1083 if (CONSTANT_POOL_ADDRESS_P (x))
1084 mark_jump_label (get_pool_constant (x), insn, in_mem);
1085 break;
1087 case LABEL_REF:
1089 rtx label = XEXP (x, 0);
1091 /* Ignore remaining references to unreachable labels that
1092 have been deleted. */
1093 if (NOTE_P (label)
1094 && NOTE_LINE_NUMBER (label) == NOTE_INSN_DELETED_LABEL)
1095 break;
1097 if (!LABEL_P (label))
1098 abort ();
1100 /* Ignore references to labels of containing functions. */
1101 if (LABEL_REF_NONLOCAL_P (x))
1102 break;
1104 XEXP (x, 0) = label;
1105 if (! insn || ! INSN_DELETED_P (insn))
1106 ++LABEL_NUSES (label);
1108 if (insn)
1110 if (JUMP_P (insn))
1111 JUMP_LABEL (insn) = label;
1112 else
1114 /* Add a REG_LABEL note for LABEL unless there already
1115 is one. All uses of a label, except for labels
1116 that are the targets of jumps, must have a
1117 REG_LABEL note. */
1118 if (! find_reg_note (insn, REG_LABEL, label))
1119 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
1120 REG_NOTES (insn));
1123 return;
1126 /* Do walk the labels in a vector, but not the first operand of an
1127 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
1128 case ADDR_VEC:
1129 case ADDR_DIFF_VEC:
1130 if (! INSN_DELETED_P (insn))
1132 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
1134 for (i = 0; i < XVECLEN (x, eltnum); i++)
1135 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, in_mem);
1137 return;
1139 default:
1140 break;
1143 fmt = GET_RTX_FORMAT (code);
1144 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1146 if (fmt[i] == 'e')
1147 mark_jump_label (XEXP (x, i), insn, in_mem);
1148 else if (fmt[i] == 'E')
1150 int j;
1151 for (j = 0; j < XVECLEN (x, i); j++)
1152 mark_jump_label (XVECEXP (x, i, j), insn, in_mem);
1157 /* If all INSN does is set the pc, delete it,
1158 and delete the insn that set the condition codes for it
1159 if that's what the previous thing was. */
1161 void
1162 delete_jump (rtx insn)
1164 rtx set = single_set (insn);
1166 if (set && GET_CODE (SET_DEST (set)) == PC)
1167 delete_computation (insn);
1170 /* Verify INSN is a BARRIER and delete it. */
1172 void
1173 delete_barrier (rtx insn)
1175 if (!BARRIER_P (insn))
1176 abort ();
1178 delete_insn (insn);
1181 /* Recursively delete prior insns that compute the value (used only by INSN
1182 which the caller is deleting) stored in the register mentioned by NOTE
1183 which is a REG_DEAD note associated with INSN. */
1185 static void
1186 delete_prior_computation (rtx note, rtx insn)
1188 rtx our_prev;
1189 rtx reg = XEXP (note, 0);
1191 for (our_prev = prev_nonnote_insn (insn);
1192 our_prev && (NONJUMP_INSN_P (our_prev)
1193 || CALL_P (our_prev));
1194 our_prev = prev_nonnote_insn (our_prev))
1196 rtx pat = PATTERN (our_prev);
1198 /* If we reach a CALL which is not calling a const function
1199 or the callee pops the arguments, then give up. */
1200 if (CALL_P (our_prev)
1201 && (! CONST_OR_PURE_CALL_P (our_prev)
1202 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
1203 break;
1205 /* If we reach a SEQUENCE, it is too complex to try to
1206 do anything with it, so give up. We can be run during
1207 and after reorg, so SEQUENCE rtl can legitimately show
1208 up here. */
1209 if (GET_CODE (pat) == SEQUENCE)
1210 break;
1212 if (GET_CODE (pat) == USE
1213 && NONJUMP_INSN_P (XEXP (pat, 0)))
1214 /* reorg creates USEs that look like this. We leave them
1215 alone because reorg needs them for its own purposes. */
1216 break;
1218 if (reg_set_p (reg, pat))
1220 if (side_effects_p (pat) && !CALL_P (our_prev))
1221 break;
1223 if (GET_CODE (pat) == PARALLEL)
1225 /* If we find a SET of something else, we can't
1226 delete the insn. */
1228 int i;
1230 for (i = 0; i < XVECLEN (pat, 0); i++)
1232 rtx part = XVECEXP (pat, 0, i);
1234 if (GET_CODE (part) == SET
1235 && SET_DEST (part) != reg)
1236 break;
1239 if (i == XVECLEN (pat, 0))
1240 delete_computation (our_prev);
1242 else if (GET_CODE (pat) == SET
1243 && REG_P (SET_DEST (pat)))
1245 int dest_regno = REGNO (SET_DEST (pat));
1246 int dest_endregno
1247 = (dest_regno
1248 + (dest_regno < FIRST_PSEUDO_REGISTER
1249 ? hard_regno_nregs[dest_regno]
1250 [GET_MODE (SET_DEST (pat))] : 1));
1251 int regno = REGNO (reg);
1252 int endregno
1253 = (regno
1254 + (regno < FIRST_PSEUDO_REGISTER
1255 ? hard_regno_nregs[regno][GET_MODE (reg)] : 1));
1257 if (dest_regno >= regno
1258 && dest_endregno <= endregno)
1259 delete_computation (our_prev);
1261 /* We may have a multi-word hard register and some, but not
1262 all, of the words of the register are needed in subsequent
1263 insns. Write REG_UNUSED notes for those parts that were not
1264 needed. */
1265 else if (dest_regno <= regno
1266 && dest_endregno >= endregno)
1268 int i;
1270 REG_NOTES (our_prev)
1271 = gen_rtx_EXPR_LIST (REG_UNUSED, reg,
1272 REG_NOTES (our_prev));
1274 for (i = dest_regno; i < dest_endregno; i++)
1275 if (! find_regno_note (our_prev, REG_UNUSED, i))
1276 break;
1278 if (i == dest_endregno)
1279 delete_computation (our_prev);
1283 break;
1286 /* If PAT references the register that dies here, it is an
1287 additional use. Hence any prior SET isn't dead. However, this
1288 insn becomes the new place for the REG_DEAD note. */
1289 if (reg_overlap_mentioned_p (reg, pat))
1291 XEXP (note, 1) = REG_NOTES (our_prev);
1292 REG_NOTES (our_prev) = note;
1293 break;
1298 /* Delete INSN and recursively delete insns that compute values used only
1299 by INSN. This uses the REG_DEAD notes computed during flow analysis.
1300 If we are running before flow.c, we need do nothing since flow.c will
1301 delete dead code. We also can't know if the registers being used are
1302 dead or not at this point.
1304 Otherwise, look at all our REG_DEAD notes. If a previous insn does
1305 nothing other than set a register that dies in this insn, we can delete
1306 that insn as well.
1308 On machines with CC0, if CC0 is used in this insn, we may be able to
1309 delete the insn that set it. */
1311 static void
1312 delete_computation (rtx insn)
1314 rtx note, next;
1316 #ifdef HAVE_cc0
1317 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
1319 rtx prev = prev_nonnote_insn (insn);
1320 /* We assume that at this stage
1321 CC's are always set explicitly
1322 and always immediately before the jump that
1323 will use them. So if the previous insn
1324 exists to set the CC's, delete it
1325 (unless it performs auto-increments, etc.). */
1326 if (prev && NONJUMP_INSN_P (prev)
1327 && sets_cc0_p (PATTERN (prev)))
1329 if (sets_cc0_p (PATTERN (prev)) > 0
1330 && ! side_effects_p (PATTERN (prev)))
1331 delete_computation (prev);
1332 else
1333 /* Otherwise, show that cc0 won't be used. */
1334 REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
1335 cc0_rtx, REG_NOTES (prev));
1338 #endif
1340 for (note = REG_NOTES (insn); note; note = next)
1342 next = XEXP (note, 1);
1344 if (REG_NOTE_KIND (note) != REG_DEAD
1345 /* Verify that the REG_NOTE is legitimate. */
1346 || !REG_P (XEXP (note, 0)))
1347 continue;
1349 delete_prior_computation (note, insn);
1352 delete_related_insns (insn);
1355 /* Delete insn INSN from the chain of insns and update label ref counts
1356 and delete insns now unreachable.
1358 Returns the first insn after INSN that was not deleted.
1360 Usage of this instruction is deprecated. Use delete_insn instead and
1361 subsequent cfg_cleanup pass to delete unreachable code if needed. */
1364 delete_related_insns (rtx insn)
1366 int was_code_label = (LABEL_P (insn));
1367 rtx note;
1368 rtx next = NEXT_INSN (insn), prev = PREV_INSN (insn);
1370 while (next && INSN_DELETED_P (next))
1371 next = NEXT_INSN (next);
1373 /* This insn is already deleted => return first following nondeleted. */
1374 if (INSN_DELETED_P (insn))
1375 return next;
1377 delete_insn (insn);
1379 /* If instruction is followed by a barrier,
1380 delete the barrier too. */
1382 if (next != 0 && BARRIER_P (next))
1383 delete_insn (next);
1385 /* If deleting a jump, decrement the count of the label,
1386 and delete the label if it is now unused. */
1388 if (JUMP_P (insn) && JUMP_LABEL (insn))
1390 rtx lab = JUMP_LABEL (insn), lab_next;
1392 if (LABEL_NUSES (lab) == 0)
1394 /* This can delete NEXT or PREV,
1395 either directly if NEXT is JUMP_LABEL (INSN),
1396 or indirectly through more levels of jumps. */
1397 delete_related_insns (lab);
1399 /* I feel a little doubtful about this loop,
1400 but I see no clean and sure alternative way
1401 to find the first insn after INSN that is not now deleted.
1402 I hope this works. */
1403 while (next && INSN_DELETED_P (next))
1404 next = NEXT_INSN (next);
1405 return next;
1407 else if (tablejump_p (insn, NULL, &lab_next))
1409 /* If we're deleting the tablejump, delete the dispatch table.
1410 We may not be able to kill the label immediately preceding
1411 just yet, as it might be referenced in code leading up to
1412 the tablejump. */
1413 delete_related_insns (lab_next);
1417 /* Likewise if we're deleting a dispatch table. */
1419 if (JUMP_P (insn)
1420 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
1421 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
1423 rtx pat = PATTERN (insn);
1424 int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1425 int len = XVECLEN (pat, diff_vec_p);
1427 for (i = 0; i < len; i++)
1428 if (LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
1429 delete_related_insns (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
1430 while (next && INSN_DELETED_P (next))
1431 next = NEXT_INSN (next);
1432 return next;
1435 /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note. */
1436 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
1437 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1438 if (REG_NOTE_KIND (note) == REG_LABEL
1439 /* This could also be a NOTE_INSN_DELETED_LABEL note. */
1440 && LABEL_P (XEXP (note, 0)))
1441 if (LABEL_NUSES (XEXP (note, 0)) == 0)
1442 delete_related_insns (XEXP (note, 0));
1444 while (prev && (INSN_DELETED_P (prev) || NOTE_P (prev)))
1445 prev = PREV_INSN (prev);
1447 /* If INSN was a label and a dispatch table follows it,
1448 delete the dispatch table. The tablejump must have gone already.
1449 It isn't useful to fall through into a table. */
1451 if (was_code_label
1452 && NEXT_INSN (insn) != 0
1453 && JUMP_P (NEXT_INSN (insn))
1454 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
1455 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
1456 next = delete_related_insns (NEXT_INSN (insn));
1458 /* If INSN was a label, delete insns following it if now unreachable. */
1460 if (was_code_label && prev && BARRIER_P (prev))
1462 enum rtx_code code;
1463 while (next)
1465 code = GET_CODE (next);
1466 if (code == NOTE
1467 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
1468 next = NEXT_INSN (next);
1469 /* Keep going past other deleted labels to delete what follows. */
1470 else if (code == CODE_LABEL && INSN_DELETED_P (next))
1471 next = NEXT_INSN (next);
1472 else if (code == BARRIER || INSN_P (next))
1473 /* Note: if this deletes a jump, it can cause more
1474 deletion of unreachable code, after a different label.
1475 As long as the value from this recursive call is correct,
1476 this invocation functions correctly. */
1477 next = delete_related_insns (next);
1478 else
1479 break;
1483 return next;
1486 /* Delete a range of insns from FROM to TO, inclusive.
1487 This is for the sake of peephole optimization, so assume
1488 that whatever these insns do will still be done by a new
1489 peephole insn that will replace them. */
1491 void
1492 delete_for_peephole (rtx from, rtx to)
1494 rtx insn = from;
1496 while (1)
1498 rtx next = NEXT_INSN (insn);
1499 rtx prev = PREV_INSN (insn);
1501 if (!NOTE_P (insn))
1503 INSN_DELETED_P (insn) = 1;
1505 /* Patch this insn out of the chain. */
1506 /* We don't do this all at once, because we
1507 must preserve all NOTEs. */
1508 if (prev)
1509 NEXT_INSN (prev) = next;
1511 if (next)
1512 PREV_INSN (next) = prev;
1515 if (insn == to)
1516 break;
1517 insn = next;
1520 /* Note that if TO is an unconditional jump
1521 we *do not* delete the BARRIER that follows,
1522 since the peephole that replaces this sequence
1523 is also an unconditional jump in that case. */
1526 /* Throughout LOC, redirect OLABEL to NLABEL. Treat null OLABEL or
1527 NLABEL as a return. Accrue modifications into the change group. */
1529 static void
1530 redirect_exp_1 (rtx *loc, rtx olabel, rtx nlabel, rtx insn)
1532 rtx x = *loc;
1533 RTX_CODE code = GET_CODE (x);
1534 int i;
1535 const char *fmt;
1537 if (code == LABEL_REF)
1539 if (XEXP (x, 0) == olabel)
1541 rtx n;
1542 if (nlabel)
1543 n = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1544 else
1545 n = gen_rtx_RETURN (VOIDmode);
1547 validate_change (insn, loc, n, 1);
1548 return;
1551 else if (code == RETURN && olabel == 0)
1553 x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1554 if (loc == &PATTERN (insn))
1555 x = gen_rtx_SET (VOIDmode, pc_rtx, x);
1556 validate_change (insn, loc, x, 1);
1557 return;
1560 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
1561 && GET_CODE (SET_SRC (x)) == LABEL_REF
1562 && XEXP (SET_SRC (x), 0) == olabel)
1564 validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 1);
1565 return;
1568 fmt = GET_RTX_FORMAT (code);
1569 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1571 if (fmt[i] == 'e')
1572 redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
1573 else if (fmt[i] == 'E')
1575 int j;
1576 for (j = 0; j < XVECLEN (x, i); j++)
1577 redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
1582 /* Similar, but apply the change group and report success or failure. */
1584 static int
1585 redirect_exp (rtx olabel, rtx nlabel, rtx insn)
1587 rtx *loc;
1589 if (GET_CODE (PATTERN (insn)) == PARALLEL)
1590 loc = &XVECEXP (PATTERN (insn), 0, 0);
1591 else
1592 loc = &PATTERN (insn);
1594 redirect_exp_1 (loc, olabel, nlabel, insn);
1595 if (num_validated_changes () == 0)
1596 return 0;
1598 return apply_change_group ();
1601 /* Make JUMP go to NLABEL instead of where it jumps now. Accrue
1602 the modifications into the change group. Return false if we did
1603 not see how to do that. */
1606 redirect_jump_1 (rtx jump, rtx nlabel)
1608 int ochanges = num_validated_changes ();
1609 rtx *loc;
1611 if (GET_CODE (PATTERN (jump)) == PARALLEL)
1612 loc = &XVECEXP (PATTERN (jump), 0, 0);
1613 else
1614 loc = &PATTERN (jump);
1616 redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
1617 return num_validated_changes () > ochanges;
1620 /* Make JUMP go to NLABEL instead of where it jumps now. If the old
1621 jump target label is unused as a result, it and the code following
1622 it may be deleted.
1624 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
1625 RETURN insn.
1627 The return value will be 1 if the change was made, 0 if it wasn't
1628 (this can only occur for NLABEL == 0). */
1631 redirect_jump (rtx jump, rtx nlabel, int delete_unused)
1633 rtx olabel = JUMP_LABEL (jump);
1634 rtx note;
1636 if (nlabel == olabel)
1637 return 1;
1639 if (! redirect_exp (olabel, nlabel, jump))
1640 return 0;
1642 JUMP_LABEL (jump) = nlabel;
1643 if (nlabel)
1644 ++LABEL_NUSES (nlabel);
1646 /* Update labels in any REG_EQUAL note. */
1647 if ((note = find_reg_note (jump, REG_EQUAL, NULL_RTX)) != NULL_RTX)
1649 if (nlabel && olabel)
1651 rtx dest = XEXP (note, 0);
1653 if (GET_CODE (dest) == IF_THEN_ELSE)
1655 if (GET_CODE (XEXP (dest, 1)) == LABEL_REF
1656 && XEXP (XEXP (dest, 1), 0) == olabel)
1657 XEXP (XEXP (dest, 1), 0) = nlabel;
1658 if (GET_CODE (XEXP (dest, 2)) == LABEL_REF
1659 && XEXP (XEXP (dest, 2), 0) == olabel)
1660 XEXP (XEXP (dest, 2), 0) = nlabel;
1662 else
1663 remove_note (jump, note);
1665 else
1666 remove_note (jump, note);
1669 /* If we're eliding the jump over exception cleanups at the end of a
1670 function, move the function end note so that -Wreturn-type works. */
1671 if (olabel && nlabel
1672 && NEXT_INSN (olabel)
1673 && NOTE_P (NEXT_INSN (olabel))
1674 && NOTE_LINE_NUMBER (NEXT_INSN (olabel)) == NOTE_INSN_FUNCTION_END)
1675 emit_note_after (NOTE_INSN_FUNCTION_END, nlabel);
1677 if (olabel && --LABEL_NUSES (olabel) == 0 && delete_unused
1678 /* Undefined labels will remain outside the insn stream. */
1679 && INSN_UID (olabel))
1680 delete_related_insns (olabel);
1682 return 1;
1685 /* Invert the jump condition of rtx X contained in jump insn, INSN.
1686 Accrue the modifications into the change group. */
1688 static void
1689 invert_exp_1 (rtx insn)
1691 RTX_CODE code;
1692 rtx x = pc_set (insn);
1694 if (!x)
1695 abort ();
1696 x = SET_SRC (x);
1698 code = GET_CODE (x);
1700 if (code == IF_THEN_ELSE)
1702 rtx comp = XEXP (x, 0);
1703 rtx tem;
1704 enum rtx_code reversed_code;
1706 /* We can do this in two ways: The preferable way, which can only
1707 be done if this is not an integer comparison, is to reverse
1708 the comparison code. Otherwise, swap the THEN-part and ELSE-part
1709 of the IF_THEN_ELSE. If we can't do either, fail. */
1711 reversed_code = reversed_comparison_code (comp, insn);
1713 if (reversed_code != UNKNOWN)
1715 validate_change (insn, &XEXP (x, 0),
1716 gen_rtx_fmt_ee (reversed_code,
1717 GET_MODE (comp), XEXP (comp, 0),
1718 XEXP (comp, 1)),
1720 return;
1723 tem = XEXP (x, 1);
1724 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
1725 validate_change (insn, &XEXP (x, 2), tem, 1);
1727 else
1728 abort ();
1731 /* Invert the jump condition of conditional jump insn, INSN.
1733 Return 1 if we can do so, 0 if we cannot find a way to do so that
1734 matches a pattern. */
1736 static int
1737 invert_exp (rtx insn)
1739 invert_exp_1 (insn);
1740 if (num_validated_changes () == 0)
1741 return 0;
1743 return apply_change_group ();
1746 /* Invert the condition of the jump JUMP, and make it jump to label
1747 NLABEL instead of where it jumps now. Accrue changes into the
1748 change group. Return false if we didn't see how to perform the
1749 inversion and redirection. */
1752 invert_jump_1 (rtx jump, rtx nlabel)
1754 int ochanges;
1756 ochanges = num_validated_changes ();
1757 invert_exp_1 (jump);
1758 if (num_validated_changes () == ochanges)
1759 return 0;
1761 return redirect_jump_1 (jump, nlabel);
1764 /* Invert the condition of the jump JUMP, and make it jump to label
1765 NLABEL instead of where it jumps now. Return true if successful. */
1768 invert_jump (rtx jump, rtx nlabel, int delete_unused)
1770 /* We have to either invert the condition and change the label or
1771 do neither. Either operation could fail. We first try to invert
1772 the jump. If that succeeds, we try changing the label. If that fails,
1773 we invert the jump back to what it was. */
1775 if (! invert_exp (jump))
1776 return 0;
1778 if (redirect_jump (jump, nlabel, delete_unused))
1780 /* Remove REG_EQUAL note if we have one. */
1781 rtx note = find_reg_note (jump, REG_EQUAL, NULL_RTX);
1782 if (note)
1783 remove_note (jump, note);
1785 invert_br_probabilities (jump);
1787 return 1;
1790 if (! invert_exp (jump))
1791 /* This should just be putting it back the way it was. */
1792 abort ();
1794 return 0;
1798 /* Like rtx_equal_p except that it considers two REGs as equal
1799 if they renumber to the same value and considers two commutative
1800 operations to be the same if the order of the operands has been
1801 reversed.
1803 ??? Addition is not commutative on the PA due to the weird implicit
1804 space register selection rules for memory addresses. Therefore, we
1805 don't consider a + b == b + a.
1807 We could/should make this test a little tighter. Possibly only
1808 disabling it on the PA via some backend macro or only disabling this
1809 case when the PLUS is inside a MEM. */
1812 rtx_renumbered_equal_p (rtx x, rtx y)
1814 int i;
1815 enum rtx_code code = GET_CODE (x);
1816 const char *fmt;
1818 if (x == y)
1819 return 1;
1821 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
1822 && (REG_P (y) || (GET_CODE (y) == SUBREG
1823 && REG_P (SUBREG_REG (y)))))
1825 int reg_x = -1, reg_y = -1;
1826 int byte_x = 0, byte_y = 0;
1828 if (GET_MODE (x) != GET_MODE (y))
1829 return 0;
1831 /* If we haven't done any renumbering, don't
1832 make any assumptions. */
1833 if (reg_renumber == 0)
1834 return rtx_equal_p (x, y);
1836 if (code == SUBREG)
1838 reg_x = REGNO (SUBREG_REG (x));
1839 byte_x = SUBREG_BYTE (x);
1841 if (reg_renumber[reg_x] >= 0)
1843 reg_x = subreg_regno_offset (reg_renumber[reg_x],
1844 GET_MODE (SUBREG_REG (x)),
1845 byte_x,
1846 GET_MODE (x));
1847 byte_x = 0;
1850 else
1852 reg_x = REGNO (x);
1853 if (reg_renumber[reg_x] >= 0)
1854 reg_x = reg_renumber[reg_x];
1857 if (GET_CODE (y) == SUBREG)
1859 reg_y = REGNO (SUBREG_REG (y));
1860 byte_y = SUBREG_BYTE (y);
1862 if (reg_renumber[reg_y] >= 0)
1864 reg_y = subreg_regno_offset (reg_renumber[reg_y],
1865 GET_MODE (SUBREG_REG (y)),
1866 byte_y,
1867 GET_MODE (y));
1868 byte_y = 0;
1871 else
1873 reg_y = REGNO (y);
1874 if (reg_renumber[reg_y] >= 0)
1875 reg_y = reg_renumber[reg_y];
1878 return reg_x >= 0 && reg_x == reg_y && byte_x == byte_y;
1881 /* Now we have disposed of all the cases
1882 in which different rtx codes can match. */
1883 if (code != GET_CODE (y))
1884 return 0;
1886 switch (code)
1888 case PC:
1889 case CC0:
1890 case ADDR_VEC:
1891 case ADDR_DIFF_VEC:
1892 case CONST_INT:
1893 return 0;
1895 case LABEL_REF:
1896 /* We can't assume nonlocal labels have their following insns yet. */
1897 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
1898 return XEXP (x, 0) == XEXP (y, 0);
1900 /* Two label-refs are equivalent if they point at labels
1901 in the same position in the instruction stream. */
1902 return (next_real_insn (XEXP (x, 0))
1903 == next_real_insn (XEXP (y, 0)));
1905 case SYMBOL_REF:
1906 return XSTR (x, 0) == XSTR (y, 0);
1908 case CODE_LABEL:
1909 /* If we didn't match EQ equality above, they aren't the same. */
1910 return 0;
1912 default:
1913 break;
1916 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
1918 if (GET_MODE (x) != GET_MODE (y))
1919 return 0;
1921 /* For commutative operations, the RTX match if the operand match in any
1922 order. Also handle the simple binary and unary cases without a loop.
1924 ??? Don't consider PLUS a commutative operator; see comments above. */
1925 if (COMMUTATIVE_P (x) && code != PLUS)
1926 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1927 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
1928 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
1929 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
1930 else if (NON_COMMUTATIVE_P (x))
1931 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1932 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
1933 else if (UNARY_P (x))
1934 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
1936 /* Compare the elements. If any pair of corresponding elements
1937 fail to match, return 0 for the whole things. */
1939 fmt = GET_RTX_FORMAT (code);
1940 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1942 int j;
1943 switch (fmt[i])
1945 case 'w':
1946 if (XWINT (x, i) != XWINT (y, i))
1947 return 0;
1948 break;
1950 case 'i':
1951 if (XINT (x, i) != XINT (y, i))
1952 return 0;
1953 break;
1955 case 't':
1956 if (XTREE (x, i) != XTREE (y, i))
1957 return 0;
1958 break;
1960 case 's':
1961 if (strcmp (XSTR (x, i), XSTR (y, i)))
1962 return 0;
1963 break;
1965 case 'e':
1966 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
1967 return 0;
1968 break;
1970 case 'u':
1971 if (XEXP (x, i) != XEXP (y, i))
1972 return 0;
1973 /* Fall through. */
1974 case '0':
1975 break;
1977 case 'E':
1978 if (XVECLEN (x, i) != XVECLEN (y, i))
1979 return 0;
1980 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1981 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1982 return 0;
1983 break;
1985 default:
1986 abort ();
1989 return 1;
1992 /* If X is a hard register or equivalent to one or a subregister of one,
1993 return the hard register number. If X is a pseudo register that was not
1994 assigned a hard register, return the pseudo register number. Otherwise,
1995 return -1. Any rtx is valid for X. */
1998 true_regnum (rtx x)
2000 if (REG_P (x))
2002 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
2003 return reg_renumber[REGNO (x)];
2004 return REGNO (x);
2006 if (GET_CODE (x) == SUBREG)
2008 int base = true_regnum (SUBREG_REG (x));
2009 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
2010 return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
2011 GET_MODE (SUBREG_REG (x)),
2012 SUBREG_BYTE (x), GET_MODE (x));
2014 return -1;
2017 /* Return regno of the register REG and handle subregs too. */
2018 unsigned int
2019 reg_or_subregno (rtx reg)
2021 if (REG_P (reg))
2022 return REGNO (reg);
2023 if (GET_CODE (reg) == SUBREG)
2024 return REGNO (SUBREG_REG (reg));
2025 abort ();