PR other/16240
[official-gcc.git] / gcc / jump.c
blobc69a4894b6316c60882d61b4343df248529c8e11
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 (GET_CODE (XEXP (insn, 0)) == CODE_LABEL)
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 (GET_CODE (insn) == BARRIER)
115 prev = prev_nonnote_insn (insn);
116 if (GET_CODE (prev) == BARRIER)
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 (GET_CODE (insn) == NOTE)
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 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
146 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
148 delete_related_insns (insn);
149 continue;
152 last_note = insn;
157 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
158 notes whose labels don't occur in the insn any more. Returns the
159 largest INSN_UID found. */
160 static void
161 init_label_info (rtx f)
163 rtx insn;
165 for (insn = f; insn; insn = NEXT_INSN (insn))
166 if (GET_CODE (insn) == CODE_LABEL)
167 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
168 else if (GET_CODE (insn) == JUMP_INSN)
169 JUMP_LABEL (insn) = 0;
170 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
172 rtx note, next;
174 for (note = REG_NOTES (insn); note; note = next)
176 next = XEXP (note, 1);
177 if (REG_NOTE_KIND (note) == REG_LABEL
178 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
179 remove_note (insn, note);
184 /* Mark the label each jump jumps to.
185 Combine consecutive labels, and count uses of labels. */
187 static void
188 mark_all_labels (rtx f)
190 rtx insn;
192 for (insn = f; insn; insn = NEXT_INSN (insn))
193 if (INSN_P (insn))
195 mark_jump_label (PATTERN (insn), insn, 0);
196 if (! INSN_DELETED_P (insn) && GET_CODE (insn) == JUMP_INSN)
198 /* When we know the LABEL_REF contained in a REG used in
199 an indirect jump, we'll have a REG_LABEL note so that
200 flow can tell where it's going. */
201 if (JUMP_LABEL (insn) == 0)
203 rtx label_note = find_reg_note (insn, REG_LABEL, NULL_RTX);
204 if (label_note)
206 /* But a LABEL_REF around the REG_LABEL note, so
207 that we can canonicalize it. */
208 rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
209 XEXP (label_note, 0));
211 mark_jump_label (label_ref, insn, 0);
212 XEXP (label_note, 0) = XEXP (label_ref, 0);
213 JUMP_LABEL (insn) = XEXP (label_note, 0);
220 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
221 notes between START and END out before START. START and END may be such
222 notes. Returns the values of the new starting and ending insns, which
223 may be different if the original ones were such notes.
224 Return true if there were only such notes and no real instructions. */
226 bool
227 squeeze_notes (rtx* startp, rtx* endp)
229 rtx start = *startp;
230 rtx end = *endp;
232 rtx insn;
233 rtx next;
234 rtx last = NULL;
235 rtx past_end = NEXT_INSN (end);
237 for (insn = start; insn != past_end; insn = next)
239 next = NEXT_INSN (insn);
240 if (GET_CODE (insn) == NOTE
241 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
242 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
243 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
244 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
245 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
246 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
248 if (insn == start)
249 start = next;
250 else
252 rtx prev = PREV_INSN (insn);
253 PREV_INSN (insn) = PREV_INSN (start);
254 NEXT_INSN (insn) = start;
255 NEXT_INSN (PREV_INSN (insn)) = insn;
256 PREV_INSN (NEXT_INSN (insn)) = insn;
257 NEXT_INSN (prev) = next;
258 PREV_INSN (next) = prev;
261 else
262 last = insn;
265 /* There were no real instructions. */
266 if (start == past_end)
267 return true;
269 end = last;
271 *startp = start;
272 *endp = end;
273 return false;
276 /* Return the label before INSN, or put a new label there. */
279 get_label_before (rtx insn)
281 rtx label;
283 /* Find an existing label at this point
284 or make a new one if there is none. */
285 label = prev_nonnote_insn (insn);
287 if (label == 0 || GET_CODE (label) != CODE_LABEL)
289 rtx prev = PREV_INSN (insn);
291 label = gen_label_rtx ();
292 emit_label_after (label, prev);
293 LABEL_NUSES (label) = 0;
295 return label;
298 /* Return the label after INSN, or put a new label there. */
301 get_label_after (rtx insn)
303 rtx label;
305 /* Find an existing label at this point
306 or make a new one if there is none. */
307 label = next_nonnote_insn (insn);
309 if (label == 0 || GET_CODE (label) != CODE_LABEL)
311 label = gen_label_rtx ();
312 emit_label_after (label, insn);
313 LABEL_NUSES (label) = 0;
315 return label;
318 /* Given a comparison (CODE ARG0 ARG1), inside an insn, INSN, return a code
319 of reversed comparison if it is possible to do so. Otherwise return UNKNOWN.
320 UNKNOWN may be returned in case we are having CC_MODE compare and we don't
321 know whether it's source is floating point or integer comparison. Machine
322 description should define REVERSIBLE_CC_MODE and REVERSE_CONDITION macros
323 to help this function avoid overhead in these cases. */
324 enum rtx_code
325 reversed_comparison_code_parts (enum rtx_code code, rtx arg0, rtx arg1, rtx insn)
327 enum machine_mode mode;
329 /* If this is not actually a comparison, we can't reverse it. */
330 if (GET_RTX_CLASS (code) != RTX_COMPARE
331 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE)
332 return UNKNOWN;
334 mode = GET_MODE (arg0);
335 if (mode == VOIDmode)
336 mode = GET_MODE (arg1);
338 /* First see if machine description supplies us way to reverse the
339 comparison. Give it priority over everything else to allow
340 machine description to do tricks. */
341 if (GET_MODE_CLASS (mode) == MODE_CC
342 && REVERSIBLE_CC_MODE (mode))
344 #ifdef REVERSE_CONDITION
345 return REVERSE_CONDITION (code, mode);
346 #endif
347 return reverse_condition (code);
350 /* Try a few special cases based on the comparison code. */
351 switch (code)
353 case GEU:
354 case GTU:
355 case LEU:
356 case LTU:
357 case NE:
358 case EQ:
359 /* It is always safe to reverse EQ and NE, even for the floating
360 point. Similarly the unsigned comparisons are never used for
361 floating point so we can reverse them in the default way. */
362 return reverse_condition (code);
363 case ORDERED:
364 case UNORDERED:
365 case LTGT:
366 case UNEQ:
367 /* In case we already see unordered comparison, we can be sure to
368 be dealing with floating point so we don't need any more tests. */
369 return reverse_condition_maybe_unordered (code);
370 case UNLT:
371 case UNLE:
372 case UNGT:
373 case UNGE:
374 /* We don't have safe way to reverse these yet. */
375 return UNKNOWN;
376 default:
377 break;
380 if (GET_MODE_CLASS (mode) == MODE_CC || CC0_P (arg0))
382 rtx prev;
383 /* Try to search for the comparison to determine the real mode.
384 This code is expensive, but with sane machine description it
385 will be never used, since REVERSIBLE_CC_MODE will return true
386 in all cases. */
387 if (! insn)
388 return UNKNOWN;
390 for (prev = prev_nonnote_insn (insn);
391 prev != 0 && GET_CODE (prev) != CODE_LABEL;
392 prev = prev_nonnote_insn (prev))
394 rtx set = set_of (arg0, prev);
395 if (set && GET_CODE (set) == SET
396 && rtx_equal_p (SET_DEST (set), arg0))
398 rtx src = SET_SRC (set);
400 if (GET_CODE (src) == COMPARE)
402 rtx comparison = src;
403 arg0 = XEXP (src, 0);
404 mode = GET_MODE (arg0);
405 if (mode == VOIDmode)
406 mode = GET_MODE (XEXP (comparison, 1));
407 break;
409 /* We can get past reg-reg moves. This may be useful for model
410 of i387 comparisons that first move flag registers around. */
411 if (REG_P (src))
413 arg0 = src;
414 continue;
417 /* If register is clobbered in some ununderstandable way,
418 give up. */
419 if (set)
420 return UNKNOWN;
424 /* Test for an integer condition, or a floating-point comparison
425 in which NaNs can be ignored. */
426 if (GET_CODE (arg0) == CONST_INT
427 || (GET_MODE (arg0) != VOIDmode
428 && GET_MODE_CLASS (mode) != MODE_CC
429 && !HONOR_NANS (mode)))
430 return reverse_condition (code);
432 return UNKNOWN;
435 /* A wrapper around the previous function to take COMPARISON as rtx
436 expression. This simplifies many callers. */
437 enum rtx_code
438 reversed_comparison_code (rtx comparison, rtx insn)
440 if (!COMPARISON_P (comparison))
441 return UNKNOWN;
442 return reversed_comparison_code_parts (GET_CODE (comparison),
443 XEXP (comparison, 0),
444 XEXP (comparison, 1), insn);
447 /* Given an rtx-code for a comparison, return the code for the negated
448 comparison. If no such code exists, return UNKNOWN.
450 WATCH OUT! reverse_condition is not safe to use on a jump that might
451 be acting on the results of an IEEE floating point comparison, because
452 of the special treatment of non-signaling nans in comparisons.
453 Use reversed_comparison_code instead. */
455 enum rtx_code
456 reverse_condition (enum rtx_code code)
458 switch (code)
460 case EQ:
461 return NE;
462 case NE:
463 return EQ;
464 case GT:
465 return LE;
466 case GE:
467 return LT;
468 case LT:
469 return GE;
470 case LE:
471 return GT;
472 case GTU:
473 return LEU;
474 case GEU:
475 return LTU;
476 case LTU:
477 return GEU;
478 case LEU:
479 return GTU;
480 case UNORDERED:
481 return ORDERED;
482 case ORDERED:
483 return UNORDERED;
485 case UNLT:
486 case UNLE:
487 case UNGT:
488 case UNGE:
489 case UNEQ:
490 case LTGT:
491 return UNKNOWN;
493 default:
494 abort ();
498 /* Similar, but we're allowed to generate unordered comparisons, which
499 makes it safe for IEEE floating-point. Of course, we have to recognize
500 that the target will support them too... */
502 enum rtx_code
503 reverse_condition_maybe_unordered (enum rtx_code code)
505 switch (code)
507 case EQ:
508 return NE;
509 case NE:
510 return EQ;
511 case GT:
512 return UNLE;
513 case GE:
514 return UNLT;
515 case LT:
516 return UNGE;
517 case LE:
518 return UNGT;
519 case LTGT:
520 return UNEQ;
521 case UNORDERED:
522 return ORDERED;
523 case ORDERED:
524 return UNORDERED;
525 case UNLT:
526 return GE;
527 case UNLE:
528 return GT;
529 case UNGT:
530 return LE;
531 case UNGE:
532 return LT;
533 case UNEQ:
534 return LTGT;
536 default:
537 abort ();
541 /* Similar, but return the code when two operands of a comparison are swapped.
542 This IS safe for IEEE floating-point. */
544 enum rtx_code
545 swap_condition (enum rtx_code code)
547 switch (code)
549 case EQ:
550 case NE:
551 case UNORDERED:
552 case ORDERED:
553 case UNEQ:
554 case LTGT:
555 return code;
557 case GT:
558 return LT;
559 case GE:
560 return LE;
561 case LT:
562 return GT;
563 case LE:
564 return GE;
565 case GTU:
566 return LTU;
567 case GEU:
568 return LEU;
569 case LTU:
570 return GTU;
571 case LEU:
572 return GEU;
573 case UNLT:
574 return UNGT;
575 case UNLE:
576 return UNGE;
577 case UNGT:
578 return UNLT;
579 case UNGE:
580 return UNLE;
582 default:
583 abort ();
587 /* Given a comparison CODE, return the corresponding unsigned comparison.
588 If CODE is an equality comparison or already an unsigned comparison,
589 CODE is returned. */
591 enum rtx_code
592 unsigned_condition (enum rtx_code code)
594 switch (code)
596 case EQ:
597 case NE:
598 case GTU:
599 case GEU:
600 case LTU:
601 case LEU:
602 return code;
604 case GT:
605 return GTU;
606 case GE:
607 return GEU;
608 case LT:
609 return LTU;
610 case LE:
611 return LEU;
613 default:
614 abort ();
618 /* Similarly, return the signed version of a comparison. */
620 enum rtx_code
621 signed_condition (enum rtx_code code)
623 switch (code)
625 case EQ:
626 case NE:
627 case GT:
628 case GE:
629 case LT:
630 case LE:
631 return code;
633 case GTU:
634 return GT;
635 case GEU:
636 return GE;
637 case LTU:
638 return LT;
639 case LEU:
640 return LE;
642 default:
643 abort ();
647 /* Return nonzero if CODE1 is more strict than CODE2, i.e., if the
648 truth of CODE1 implies the truth of CODE2. */
651 comparison_dominates_p (enum rtx_code code1, enum rtx_code code2)
653 /* UNKNOWN comparison codes can happen as a result of trying to revert
654 comparison codes.
655 They can't match anything, so we have to reject them here. */
656 if (code1 == UNKNOWN || code2 == UNKNOWN)
657 return 0;
659 if (code1 == code2)
660 return 1;
662 switch (code1)
664 case UNEQ:
665 if (code2 == UNLE || code2 == UNGE)
666 return 1;
667 break;
669 case EQ:
670 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU
671 || code2 == ORDERED)
672 return 1;
673 break;
675 case UNLT:
676 if (code2 == UNLE || code2 == NE)
677 return 1;
678 break;
680 case LT:
681 if (code2 == LE || code2 == NE || code2 == ORDERED || code2 == LTGT)
682 return 1;
683 break;
685 case UNGT:
686 if (code2 == UNGE || code2 == NE)
687 return 1;
688 break;
690 case GT:
691 if (code2 == GE || code2 == NE || code2 == ORDERED || code2 == LTGT)
692 return 1;
693 break;
695 case GE:
696 case LE:
697 if (code2 == ORDERED)
698 return 1;
699 break;
701 case LTGT:
702 if (code2 == NE || code2 == ORDERED)
703 return 1;
704 break;
706 case LTU:
707 if (code2 == LEU || code2 == NE)
708 return 1;
709 break;
711 case GTU:
712 if (code2 == GEU || code2 == NE)
713 return 1;
714 break;
716 case UNORDERED:
717 if (code2 == NE || code2 == UNEQ || code2 == UNLE || code2 == UNLT
718 || code2 == UNGE || code2 == UNGT)
719 return 1;
720 break;
722 default:
723 break;
726 return 0;
729 /* Return 1 if INSN is an unconditional jump and nothing else. */
732 simplejump_p (rtx insn)
734 return (GET_CODE (insn) == JUMP_INSN
735 && GET_CODE (PATTERN (insn)) == SET
736 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
737 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
740 /* Return nonzero if INSN is a (possibly) conditional jump
741 and nothing more.
743 Use of this function is deprecated, since we need to support combined
744 branch and compare insns. Use any_condjump_p instead whenever possible. */
747 condjump_p (rtx insn)
749 rtx x = PATTERN (insn);
751 if (GET_CODE (x) != SET
752 || GET_CODE (SET_DEST (x)) != PC)
753 return 0;
755 x = SET_SRC (x);
756 if (GET_CODE (x) == LABEL_REF)
757 return 1;
758 else
759 return (GET_CODE (x) == IF_THEN_ELSE
760 && ((GET_CODE (XEXP (x, 2)) == PC
761 && (GET_CODE (XEXP (x, 1)) == LABEL_REF
762 || GET_CODE (XEXP (x, 1)) == RETURN))
763 || (GET_CODE (XEXP (x, 1)) == PC
764 && (GET_CODE (XEXP (x, 2)) == LABEL_REF
765 || GET_CODE (XEXP (x, 2)) == RETURN))));
767 return 0;
770 /* Return nonzero if INSN is a (possibly) conditional jump inside a
771 PARALLEL.
773 Use this function is deprecated, since we need to support combined
774 branch and compare insns. Use any_condjump_p instead whenever possible. */
777 condjump_in_parallel_p (rtx insn)
779 rtx x = PATTERN (insn);
781 if (GET_CODE (x) != PARALLEL)
782 return 0;
783 else
784 x = XVECEXP (x, 0, 0);
786 if (GET_CODE (x) != SET)
787 return 0;
788 if (GET_CODE (SET_DEST (x)) != PC)
789 return 0;
790 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
791 return 1;
792 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
793 return 0;
794 if (XEXP (SET_SRC (x), 2) == pc_rtx
795 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
796 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
797 return 1;
798 if (XEXP (SET_SRC (x), 1) == pc_rtx
799 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
800 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
801 return 1;
802 return 0;
805 /* Return set of PC, otherwise NULL. */
808 pc_set (rtx insn)
810 rtx pat;
811 if (GET_CODE (insn) != JUMP_INSN)
812 return NULL_RTX;
813 pat = PATTERN (insn);
815 /* The set is allowed to appear either as the insn pattern or
816 the first set in a PARALLEL. */
817 if (GET_CODE (pat) == PARALLEL)
818 pat = XVECEXP (pat, 0, 0);
819 if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == PC)
820 return pat;
822 return NULL_RTX;
825 /* Return true when insn is an unconditional direct jump,
826 possibly bundled inside a PARALLEL. */
829 any_uncondjump_p (rtx insn)
831 rtx x = pc_set (insn);
832 if (!x)
833 return 0;
834 if (GET_CODE (SET_SRC (x)) != LABEL_REF)
835 return 0;
836 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
837 return 0;
838 return 1;
841 /* Return true when insn is a conditional jump. This function works for
842 instructions containing PC sets in PARALLELs. The instruction may have
843 various other effects so before removing the jump you must verify
844 onlyjump_p.
846 Note that unlike condjump_p it returns false for unconditional jumps. */
849 any_condjump_p (rtx insn)
851 rtx x = pc_set (insn);
852 enum rtx_code a, b;
854 if (!x)
855 return 0;
856 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
857 return 0;
859 a = GET_CODE (XEXP (SET_SRC (x), 1));
860 b = GET_CODE (XEXP (SET_SRC (x), 2));
862 return ((b == PC && (a == LABEL_REF || a == RETURN))
863 || (a == PC && (b == LABEL_REF || b == RETURN)));
866 /* Return the label of a conditional jump. */
869 condjump_label (rtx insn)
871 rtx x = pc_set (insn);
873 if (!x)
874 return NULL_RTX;
875 x = SET_SRC (x);
876 if (GET_CODE (x) == LABEL_REF)
877 return x;
878 if (GET_CODE (x) != IF_THEN_ELSE)
879 return NULL_RTX;
880 if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
881 return XEXP (x, 1);
882 if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
883 return XEXP (x, 2);
884 return NULL_RTX;
887 /* Return true if INSN is a (possibly conditional) return insn. */
889 static int
890 returnjump_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
892 rtx x = *loc;
894 return x && (GET_CODE (x) == RETURN
895 || (GET_CODE (x) == SET && SET_IS_RETURN_P (x)));
899 returnjump_p (rtx insn)
901 if (GET_CODE (insn) != JUMP_INSN)
902 return 0;
903 return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
906 /* Return true if INSN is a jump that only transfers control and
907 nothing more. */
910 onlyjump_p (rtx insn)
912 rtx set;
914 if (GET_CODE (insn) != JUMP_INSN)
915 return 0;
917 set = single_set (insn);
918 if (set == NULL)
919 return 0;
920 if (GET_CODE (SET_DEST (set)) != PC)
921 return 0;
922 if (side_effects_p (SET_SRC (set)))
923 return 0;
925 return 1;
928 #ifdef HAVE_cc0
930 /* Return nonzero if X is an RTX that only sets the condition codes
931 and has no side effects. */
934 only_sets_cc0_p (rtx x)
936 if (! x)
937 return 0;
939 if (INSN_P (x))
940 x = PATTERN (x);
942 return sets_cc0_p (x) == 1 && ! side_effects_p (x);
945 /* Return 1 if X is an RTX that does nothing but set the condition codes
946 and CLOBBER or USE registers.
947 Return -1 if X does explicitly set the condition codes,
948 but also does other things. */
951 sets_cc0_p (rtx x)
953 if (! x)
954 return 0;
956 if (INSN_P (x))
957 x = PATTERN (x);
959 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
960 return 1;
961 if (GET_CODE (x) == PARALLEL)
963 int i;
964 int sets_cc0 = 0;
965 int other_things = 0;
966 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
968 if (GET_CODE (XVECEXP (x, 0, i)) == SET
969 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
970 sets_cc0 = 1;
971 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
972 other_things = 1;
974 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
976 return 0;
978 #endif
980 /* Follow any unconditional jump at LABEL;
981 return the ultimate label reached by any such chain of jumps.
982 Return null if the chain ultimately leads to a return instruction.
983 If LABEL is not followed by a jump, return LABEL.
984 If the chain loops or we can't find end, return LABEL,
985 since that tells caller to avoid changing the insn.
987 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
988 a USE or CLOBBER. */
991 follow_jumps (rtx label)
993 rtx insn;
994 rtx next;
995 rtx value = label;
996 int depth;
998 for (depth = 0;
999 (depth < 10
1000 && (insn = next_active_insn (value)) != 0
1001 && GET_CODE (insn) == JUMP_INSN
1002 && ((JUMP_LABEL (insn) != 0 && any_uncondjump_p (insn)
1003 && onlyjump_p (insn))
1004 || GET_CODE (PATTERN (insn)) == RETURN)
1005 && (next = NEXT_INSN (insn))
1006 && GET_CODE (next) == BARRIER);
1007 depth++)
1009 /* Don't chain through the insn that jumps into a loop
1010 from outside the loop,
1011 since that would create multiple loop entry jumps
1012 and prevent loop optimization. */
1013 rtx tem;
1014 if (!reload_completed)
1015 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
1016 if (GET_CODE (tem) == NOTE
1017 && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
1018 /* ??? Optional. Disables some optimizations, but makes
1019 gcov output more accurate with -O. */
1020 || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
1021 return value;
1023 /* If we have found a cycle, make the insn jump to itself. */
1024 if (JUMP_LABEL (insn) == label)
1025 return label;
1027 tem = next_active_insn (JUMP_LABEL (insn));
1028 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
1029 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
1030 break;
1032 value = JUMP_LABEL (insn);
1034 if (depth == 10)
1035 return label;
1036 return value;
1040 /* Find all CODE_LABELs referred to in X, and increment their use counts.
1041 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
1042 in INSN, then store one of them in JUMP_LABEL (INSN).
1043 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
1044 referenced in INSN, add a REG_LABEL note containing that label to INSN.
1045 Also, when there are consecutive labels, canonicalize on the last of them.
1047 Note that two labels separated by a loop-beginning note
1048 must be kept distinct if we have not yet done loop-optimization,
1049 because the gap between them is where loop-optimize
1050 will want to move invariant code to. CROSS_JUMP tells us
1051 that loop-optimization is done with. */
1053 void
1054 mark_jump_label (rtx x, rtx insn, int in_mem)
1056 RTX_CODE code = GET_CODE (x);
1057 int i;
1058 const char *fmt;
1060 switch (code)
1062 case PC:
1063 case CC0:
1064 case REG:
1065 case CONST_INT:
1066 case CONST_DOUBLE:
1067 case CLOBBER:
1068 case CALL:
1069 return;
1071 case MEM:
1072 in_mem = 1;
1073 break;
1075 case SYMBOL_REF:
1076 if (!in_mem)
1077 return;
1079 /* If this is a constant-pool reference, see if it is a label. */
1080 if (CONSTANT_POOL_ADDRESS_P (x))
1081 mark_jump_label (get_pool_constant (x), insn, in_mem);
1082 break;
1084 case LABEL_REF:
1086 rtx label = XEXP (x, 0);
1088 /* Ignore remaining references to unreachable labels that
1089 have been deleted. */
1090 if (GET_CODE (label) == NOTE
1091 && NOTE_LINE_NUMBER (label) == NOTE_INSN_DELETED_LABEL)
1092 break;
1094 if (GET_CODE (label) != CODE_LABEL)
1095 abort ();
1097 /* Ignore references to labels of containing functions. */
1098 if (LABEL_REF_NONLOCAL_P (x))
1099 break;
1101 XEXP (x, 0) = label;
1102 if (! insn || ! INSN_DELETED_P (insn))
1103 ++LABEL_NUSES (label);
1105 if (insn)
1107 if (GET_CODE (insn) == JUMP_INSN)
1108 JUMP_LABEL (insn) = label;
1109 else
1111 /* Add a REG_LABEL note for LABEL unless there already
1112 is one. All uses of a label, except for labels
1113 that are the targets of jumps, must have a
1114 REG_LABEL note. */
1115 if (! find_reg_note (insn, REG_LABEL, label))
1116 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
1117 REG_NOTES (insn));
1120 return;
1123 /* Do walk the labels in a vector, but not the first operand of an
1124 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
1125 case ADDR_VEC:
1126 case ADDR_DIFF_VEC:
1127 if (! INSN_DELETED_P (insn))
1129 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
1131 for (i = 0; i < XVECLEN (x, eltnum); i++)
1132 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, in_mem);
1134 return;
1136 default:
1137 break;
1140 fmt = GET_RTX_FORMAT (code);
1141 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1143 if (fmt[i] == 'e')
1144 mark_jump_label (XEXP (x, i), insn, in_mem);
1145 else if (fmt[i] == 'E')
1147 int j;
1148 for (j = 0; j < XVECLEN (x, i); j++)
1149 mark_jump_label (XVECEXP (x, i, j), insn, in_mem);
1154 /* If all INSN does is set the pc, delete it,
1155 and delete the insn that set the condition codes for it
1156 if that's what the previous thing was. */
1158 void
1159 delete_jump (rtx insn)
1161 rtx set = single_set (insn);
1163 if (set && GET_CODE (SET_DEST (set)) == PC)
1164 delete_computation (insn);
1167 /* Verify INSN is a BARRIER and delete it. */
1169 void
1170 delete_barrier (rtx insn)
1172 if (GET_CODE (insn) != BARRIER)
1173 abort ();
1175 delete_insn (insn);
1178 /* Recursively delete prior insns that compute the value (used only by INSN
1179 which the caller is deleting) stored in the register mentioned by NOTE
1180 which is a REG_DEAD note associated with INSN. */
1182 static void
1183 delete_prior_computation (rtx note, rtx insn)
1185 rtx our_prev;
1186 rtx reg = XEXP (note, 0);
1188 for (our_prev = prev_nonnote_insn (insn);
1189 our_prev && (GET_CODE (our_prev) == INSN
1190 || GET_CODE (our_prev) == CALL_INSN);
1191 our_prev = prev_nonnote_insn (our_prev))
1193 rtx pat = PATTERN (our_prev);
1195 /* If we reach a CALL which is not calling a const function
1196 or the callee pops the arguments, then give up. */
1197 if (GET_CODE (our_prev) == CALL_INSN
1198 && (! CONST_OR_PURE_CALL_P (our_prev)
1199 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
1200 break;
1202 /* If we reach a SEQUENCE, it is too complex to try to
1203 do anything with it, so give up. We can be run during
1204 and after reorg, so SEQUENCE rtl can legitimately show
1205 up here. */
1206 if (GET_CODE (pat) == SEQUENCE)
1207 break;
1209 if (GET_CODE (pat) == USE
1210 && GET_CODE (XEXP (pat, 0)) == INSN)
1211 /* reorg creates USEs that look like this. We leave them
1212 alone because reorg needs them for its own purposes. */
1213 break;
1215 if (reg_set_p (reg, pat))
1217 if (side_effects_p (pat) && GET_CODE (our_prev) != CALL_INSN)
1218 break;
1220 if (GET_CODE (pat) == PARALLEL)
1222 /* If we find a SET of something else, we can't
1223 delete the insn. */
1225 int i;
1227 for (i = 0; i < XVECLEN (pat, 0); i++)
1229 rtx part = XVECEXP (pat, 0, i);
1231 if (GET_CODE (part) == SET
1232 && SET_DEST (part) != reg)
1233 break;
1236 if (i == XVECLEN (pat, 0))
1237 delete_computation (our_prev);
1239 else if (GET_CODE (pat) == SET
1240 && REG_P (SET_DEST (pat)))
1242 int dest_regno = REGNO (SET_DEST (pat));
1243 int dest_endregno
1244 = (dest_regno
1245 + (dest_regno < FIRST_PSEUDO_REGISTER
1246 ? hard_regno_nregs[dest_regno]
1247 [GET_MODE (SET_DEST (pat))] : 1));
1248 int regno = REGNO (reg);
1249 int endregno
1250 = (regno
1251 + (regno < FIRST_PSEUDO_REGISTER
1252 ? hard_regno_nregs[regno][GET_MODE (reg)] : 1));
1254 if (dest_regno >= regno
1255 && dest_endregno <= endregno)
1256 delete_computation (our_prev);
1258 /* We may have a multi-word hard register and some, but not
1259 all, of the words of the register are needed in subsequent
1260 insns. Write REG_UNUSED notes for those parts that were not
1261 needed. */
1262 else if (dest_regno <= regno
1263 && dest_endregno >= endregno)
1265 int i;
1267 REG_NOTES (our_prev)
1268 = gen_rtx_EXPR_LIST (REG_UNUSED, reg,
1269 REG_NOTES (our_prev));
1271 for (i = dest_regno; i < dest_endregno; i++)
1272 if (! find_regno_note (our_prev, REG_UNUSED, i))
1273 break;
1275 if (i == dest_endregno)
1276 delete_computation (our_prev);
1280 break;
1283 /* If PAT references the register that dies here, it is an
1284 additional use. Hence any prior SET isn't dead. However, this
1285 insn becomes the new place for the REG_DEAD note. */
1286 if (reg_overlap_mentioned_p (reg, pat))
1288 XEXP (note, 1) = REG_NOTES (our_prev);
1289 REG_NOTES (our_prev) = note;
1290 break;
1295 /* Delete INSN and recursively delete insns that compute values used only
1296 by INSN. This uses the REG_DEAD notes computed during flow analysis.
1297 If we are running before flow.c, we need do nothing since flow.c will
1298 delete dead code. We also can't know if the registers being used are
1299 dead or not at this point.
1301 Otherwise, look at all our REG_DEAD notes. If a previous insn does
1302 nothing other than set a register that dies in this insn, we can delete
1303 that insn as well.
1305 On machines with CC0, if CC0 is used in this insn, we may be able to
1306 delete the insn that set it. */
1308 static void
1309 delete_computation (rtx insn)
1311 rtx note, next;
1313 #ifdef HAVE_cc0
1314 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
1316 rtx prev = prev_nonnote_insn (insn);
1317 /* We assume that at this stage
1318 CC's are always set explicitly
1319 and always immediately before the jump that
1320 will use them. So if the previous insn
1321 exists to set the CC's, delete it
1322 (unless it performs auto-increments, etc.). */
1323 if (prev && GET_CODE (prev) == INSN
1324 && sets_cc0_p (PATTERN (prev)))
1326 if (sets_cc0_p (PATTERN (prev)) > 0
1327 && ! side_effects_p (PATTERN (prev)))
1328 delete_computation (prev);
1329 else
1330 /* Otherwise, show that cc0 won't be used. */
1331 REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
1332 cc0_rtx, REG_NOTES (prev));
1335 #endif
1337 for (note = REG_NOTES (insn); note; note = next)
1339 next = XEXP (note, 1);
1341 if (REG_NOTE_KIND (note) != REG_DEAD
1342 /* Verify that the REG_NOTE is legitimate. */
1343 || !REG_P (XEXP (note, 0)))
1344 continue;
1346 delete_prior_computation (note, insn);
1349 delete_related_insns (insn);
1352 /* Delete insn INSN from the chain of insns and update label ref counts
1353 and delete insns now unreachable.
1355 Returns the first insn after INSN that was not deleted.
1357 Usage of this instruction is deprecated. Use delete_insn instead and
1358 subsequent cfg_cleanup pass to delete unreachable code if needed. */
1361 delete_related_insns (rtx insn)
1363 int was_code_label = (GET_CODE (insn) == CODE_LABEL);
1364 rtx note;
1365 rtx next = NEXT_INSN (insn), prev = PREV_INSN (insn);
1367 while (next && INSN_DELETED_P (next))
1368 next = NEXT_INSN (next);
1370 /* This insn is already deleted => return first following nondeleted. */
1371 if (INSN_DELETED_P (insn))
1372 return next;
1374 delete_insn (insn);
1376 /* If instruction is followed by a barrier,
1377 delete the barrier too. */
1379 if (next != 0 && GET_CODE (next) == BARRIER)
1380 delete_insn (next);
1382 /* If deleting a jump, decrement the count of the label,
1383 and delete the label if it is now unused. */
1385 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
1387 rtx lab = JUMP_LABEL (insn), lab_next;
1389 if (LABEL_NUSES (lab) == 0)
1391 /* This can delete NEXT or PREV,
1392 either directly if NEXT is JUMP_LABEL (INSN),
1393 or indirectly through more levels of jumps. */
1394 delete_related_insns (lab);
1396 /* I feel a little doubtful about this loop,
1397 but I see no clean and sure alternative way
1398 to find the first insn after INSN that is not now deleted.
1399 I hope this works. */
1400 while (next && INSN_DELETED_P (next))
1401 next = NEXT_INSN (next);
1402 return next;
1404 else if (tablejump_p (insn, NULL, &lab_next))
1406 /* If we're deleting the tablejump, delete the dispatch table.
1407 We may not be able to kill the label immediately preceding
1408 just yet, as it might be referenced in code leading up to
1409 the tablejump. */
1410 delete_related_insns (lab_next);
1414 /* Likewise if we're deleting a dispatch table. */
1416 if (GET_CODE (insn) == JUMP_INSN
1417 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
1418 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
1420 rtx pat = PATTERN (insn);
1421 int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1422 int len = XVECLEN (pat, diff_vec_p);
1424 for (i = 0; i < len; i++)
1425 if (LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
1426 delete_related_insns (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
1427 while (next && INSN_DELETED_P (next))
1428 next = NEXT_INSN (next);
1429 return next;
1432 /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note. */
1433 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
1434 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1435 if (REG_NOTE_KIND (note) == REG_LABEL
1436 /* This could also be a NOTE_INSN_DELETED_LABEL note. */
1437 && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
1438 if (LABEL_NUSES (XEXP (note, 0)) == 0)
1439 delete_related_insns (XEXP (note, 0));
1441 while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
1442 prev = PREV_INSN (prev);
1444 /* If INSN was a label and a dispatch table follows it,
1445 delete the dispatch table. The tablejump must have gone already.
1446 It isn't useful to fall through into a table. */
1448 if (was_code_label
1449 && NEXT_INSN (insn) != 0
1450 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
1451 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
1452 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
1453 next = delete_related_insns (NEXT_INSN (insn));
1455 /* If INSN was a label, delete insns following it if now unreachable. */
1457 if (was_code_label && prev && GET_CODE (prev) == BARRIER)
1459 enum rtx_code code;
1460 while (next)
1462 code = GET_CODE (next);
1463 if (code == NOTE
1464 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
1465 next = NEXT_INSN (next);
1466 /* Keep going past other deleted labels to delete what follows. */
1467 else if (code == CODE_LABEL && INSN_DELETED_P (next))
1468 next = NEXT_INSN (next);
1469 else if (code == BARRIER || INSN_P (next))
1470 /* Note: if this deletes a jump, it can cause more
1471 deletion of unreachable code, after a different label.
1472 As long as the value from this recursive call is correct,
1473 this invocation functions correctly. */
1474 next = delete_related_insns (next);
1475 else
1476 break;
1480 return next;
1483 /* Delete a range of insns from FROM to TO, inclusive.
1484 This is for the sake of peephole optimization, so assume
1485 that whatever these insns do will still be done by a new
1486 peephole insn that will replace them. */
1488 void
1489 delete_for_peephole (rtx from, rtx to)
1491 rtx insn = from;
1493 while (1)
1495 rtx next = NEXT_INSN (insn);
1496 rtx prev = PREV_INSN (insn);
1498 if (GET_CODE (insn) != NOTE)
1500 INSN_DELETED_P (insn) = 1;
1502 /* Patch this insn out of the chain. */
1503 /* We don't do this all at once, because we
1504 must preserve all NOTEs. */
1505 if (prev)
1506 NEXT_INSN (prev) = next;
1508 if (next)
1509 PREV_INSN (next) = prev;
1512 if (insn == to)
1513 break;
1514 insn = next;
1517 /* Note that if TO is an unconditional jump
1518 we *do not* delete the BARRIER that follows,
1519 since the peephole that replaces this sequence
1520 is also an unconditional jump in that case. */
1523 /* Throughout LOC, redirect OLABEL to NLABEL. Treat null OLABEL or
1524 NLABEL as a return. Accrue modifications into the change group. */
1526 static void
1527 redirect_exp_1 (rtx *loc, rtx olabel, rtx nlabel, rtx insn)
1529 rtx x = *loc;
1530 RTX_CODE code = GET_CODE (x);
1531 int i;
1532 const char *fmt;
1534 if (code == LABEL_REF)
1536 if (XEXP (x, 0) == olabel)
1538 rtx n;
1539 if (nlabel)
1540 n = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1541 else
1542 n = gen_rtx_RETURN (VOIDmode);
1544 validate_change (insn, loc, n, 1);
1545 return;
1548 else if (code == RETURN && olabel == 0)
1550 x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1551 if (loc == &PATTERN (insn))
1552 x = gen_rtx_SET (VOIDmode, pc_rtx, x);
1553 validate_change (insn, loc, x, 1);
1554 return;
1557 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
1558 && GET_CODE (SET_SRC (x)) == LABEL_REF
1559 && XEXP (SET_SRC (x), 0) == olabel)
1561 validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 1);
1562 return;
1565 fmt = GET_RTX_FORMAT (code);
1566 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1568 if (fmt[i] == 'e')
1569 redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
1570 else if (fmt[i] == 'E')
1572 int j;
1573 for (j = 0; j < XVECLEN (x, i); j++)
1574 redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
1579 /* Similar, but apply the change group and report success or failure. */
1581 static int
1582 redirect_exp (rtx olabel, rtx nlabel, rtx insn)
1584 rtx *loc;
1586 if (GET_CODE (PATTERN (insn)) == PARALLEL)
1587 loc = &XVECEXP (PATTERN (insn), 0, 0);
1588 else
1589 loc = &PATTERN (insn);
1591 redirect_exp_1 (loc, olabel, nlabel, insn);
1592 if (num_validated_changes () == 0)
1593 return 0;
1595 return apply_change_group ();
1598 /* Make JUMP go to NLABEL instead of where it jumps now. Accrue
1599 the modifications into the change group. Return false if we did
1600 not see how to do that. */
1603 redirect_jump_1 (rtx jump, rtx nlabel)
1605 int ochanges = num_validated_changes ();
1606 rtx *loc;
1608 if (GET_CODE (PATTERN (jump)) == PARALLEL)
1609 loc = &XVECEXP (PATTERN (jump), 0, 0);
1610 else
1611 loc = &PATTERN (jump);
1613 redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
1614 return num_validated_changes () > ochanges;
1617 /* Make JUMP go to NLABEL instead of where it jumps now. If the old
1618 jump target label is unused as a result, it and the code following
1619 it may be deleted.
1621 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
1622 RETURN insn.
1624 The return value will be 1 if the change was made, 0 if it wasn't
1625 (this can only occur for NLABEL == 0). */
1628 redirect_jump (rtx jump, rtx nlabel, int delete_unused)
1630 rtx olabel = JUMP_LABEL (jump);
1631 rtx note;
1633 if (nlabel == olabel)
1634 return 1;
1636 if (! redirect_exp (olabel, nlabel, jump))
1637 return 0;
1639 JUMP_LABEL (jump) = nlabel;
1640 if (nlabel)
1641 ++LABEL_NUSES (nlabel);
1643 /* Update labels in any REG_EQUAL note. */
1644 if ((note = find_reg_note (jump, REG_EQUAL, NULL_RTX)) != NULL_RTX)
1646 if (nlabel && olabel)
1648 rtx dest = XEXP (note, 0);
1650 if (GET_CODE (dest) == IF_THEN_ELSE)
1652 if (GET_CODE (XEXP (dest, 1)) == LABEL_REF
1653 && XEXP (XEXP (dest, 1), 0) == olabel)
1654 XEXP (XEXP (dest, 1), 0) = nlabel;
1655 if (GET_CODE (XEXP (dest, 2)) == LABEL_REF
1656 && XEXP (XEXP (dest, 2), 0) == olabel)
1657 XEXP (XEXP (dest, 2), 0) = nlabel;
1659 else
1660 remove_note (jump, note);
1662 else
1663 remove_note (jump, note);
1666 /* If we're eliding the jump over exception cleanups at the end of a
1667 function, move the function end note so that -Wreturn-type works. */
1668 if (olabel && nlabel
1669 && NEXT_INSN (olabel)
1670 && GET_CODE (NEXT_INSN (olabel)) == NOTE
1671 && NOTE_LINE_NUMBER (NEXT_INSN (olabel)) == NOTE_INSN_FUNCTION_END)
1672 emit_note_after (NOTE_INSN_FUNCTION_END, nlabel);
1674 if (olabel && --LABEL_NUSES (olabel) == 0 && delete_unused
1675 /* Undefined labels will remain outside the insn stream. */
1676 && INSN_UID (olabel))
1677 delete_related_insns (olabel);
1679 return 1;
1682 /* Invert the jump condition of rtx X contained in jump insn, INSN.
1683 Accrue the modifications into the change group. */
1685 static void
1686 invert_exp_1 (rtx insn)
1688 RTX_CODE code;
1689 rtx x = pc_set (insn);
1691 if (!x)
1692 abort ();
1693 x = SET_SRC (x);
1695 code = GET_CODE (x);
1697 if (code == IF_THEN_ELSE)
1699 rtx comp = XEXP (x, 0);
1700 rtx tem;
1701 enum rtx_code reversed_code;
1703 /* We can do this in two ways: The preferable way, which can only
1704 be done if this is not an integer comparison, is to reverse
1705 the comparison code. Otherwise, swap the THEN-part and ELSE-part
1706 of the IF_THEN_ELSE. If we can't do either, fail. */
1708 reversed_code = reversed_comparison_code (comp, insn);
1710 if (reversed_code != UNKNOWN)
1712 validate_change (insn, &XEXP (x, 0),
1713 gen_rtx_fmt_ee (reversed_code,
1714 GET_MODE (comp), XEXP (comp, 0),
1715 XEXP (comp, 1)),
1717 return;
1720 tem = XEXP (x, 1);
1721 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
1722 validate_change (insn, &XEXP (x, 2), tem, 1);
1724 else
1725 abort ();
1728 /* Invert the jump condition of conditional jump insn, INSN.
1730 Return 1 if we can do so, 0 if we cannot find a way to do so that
1731 matches a pattern. */
1733 static int
1734 invert_exp (rtx insn)
1736 invert_exp_1 (insn);
1737 if (num_validated_changes () == 0)
1738 return 0;
1740 return apply_change_group ();
1743 /* Invert the condition of the jump JUMP, and make it jump to label
1744 NLABEL instead of where it jumps now. Accrue changes into the
1745 change group. Return false if we didn't see how to perform the
1746 inversion and redirection. */
1749 invert_jump_1 (rtx jump, rtx nlabel)
1751 int ochanges;
1753 ochanges = num_validated_changes ();
1754 invert_exp_1 (jump);
1755 if (num_validated_changes () == ochanges)
1756 return 0;
1758 return redirect_jump_1 (jump, nlabel);
1761 /* Invert the condition of the jump JUMP, and make it jump to label
1762 NLABEL instead of where it jumps now. Return true if successful. */
1765 invert_jump (rtx jump, rtx nlabel, int delete_unused)
1767 /* We have to either invert the condition and change the label or
1768 do neither. Either operation could fail. We first try to invert
1769 the jump. If that succeeds, we try changing the label. If that fails,
1770 we invert the jump back to what it was. */
1772 if (! invert_exp (jump))
1773 return 0;
1775 if (redirect_jump (jump, nlabel, delete_unused))
1777 /* Remove REG_EQUAL note if we have one. */
1778 rtx note = find_reg_note (jump, REG_EQUAL, NULL_RTX);
1779 if (note)
1780 remove_note (jump, note);
1782 invert_br_probabilities (jump);
1784 return 1;
1787 if (! invert_exp (jump))
1788 /* This should just be putting it back the way it was. */
1789 abort ();
1791 return 0;
1795 /* Like rtx_equal_p except that it considers two REGs as equal
1796 if they renumber to the same value and considers two commutative
1797 operations to be the same if the order of the operands has been
1798 reversed.
1800 ??? Addition is not commutative on the PA due to the weird implicit
1801 space register selection rules for memory addresses. Therefore, we
1802 don't consider a + b == b + a.
1804 We could/should make this test a little tighter. Possibly only
1805 disabling it on the PA via some backend macro or only disabling this
1806 case when the PLUS is inside a MEM. */
1809 rtx_renumbered_equal_p (rtx x, rtx y)
1811 int i;
1812 enum rtx_code code = GET_CODE (x);
1813 const char *fmt;
1815 if (x == y)
1816 return 1;
1818 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
1819 && (REG_P (y) || (GET_CODE (y) == SUBREG
1820 && REG_P (SUBREG_REG (y)))))
1822 int reg_x = -1, reg_y = -1;
1823 int byte_x = 0, byte_y = 0;
1825 if (GET_MODE (x) != GET_MODE (y))
1826 return 0;
1828 /* If we haven't done any renumbering, don't
1829 make any assumptions. */
1830 if (reg_renumber == 0)
1831 return rtx_equal_p (x, y);
1833 if (code == SUBREG)
1835 reg_x = REGNO (SUBREG_REG (x));
1836 byte_x = SUBREG_BYTE (x);
1838 if (reg_renumber[reg_x] >= 0)
1840 reg_x = subreg_regno_offset (reg_renumber[reg_x],
1841 GET_MODE (SUBREG_REG (x)),
1842 byte_x,
1843 GET_MODE (x));
1844 byte_x = 0;
1847 else
1849 reg_x = REGNO (x);
1850 if (reg_renumber[reg_x] >= 0)
1851 reg_x = reg_renumber[reg_x];
1854 if (GET_CODE (y) == SUBREG)
1856 reg_y = REGNO (SUBREG_REG (y));
1857 byte_y = SUBREG_BYTE (y);
1859 if (reg_renumber[reg_y] >= 0)
1861 reg_y = subreg_regno_offset (reg_renumber[reg_y],
1862 GET_MODE (SUBREG_REG (y)),
1863 byte_y,
1864 GET_MODE (y));
1865 byte_y = 0;
1868 else
1870 reg_y = REGNO (y);
1871 if (reg_renumber[reg_y] >= 0)
1872 reg_y = reg_renumber[reg_y];
1875 return reg_x >= 0 && reg_x == reg_y && byte_x == byte_y;
1878 /* Now we have disposed of all the cases
1879 in which different rtx codes can match. */
1880 if (code != GET_CODE (y))
1881 return 0;
1883 switch (code)
1885 case PC:
1886 case CC0:
1887 case ADDR_VEC:
1888 case ADDR_DIFF_VEC:
1889 case CONST_INT:
1890 return 0;
1892 case LABEL_REF:
1893 /* We can't assume nonlocal labels have their following insns yet. */
1894 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
1895 return XEXP (x, 0) == XEXP (y, 0);
1897 /* Two label-refs are equivalent if they point at labels
1898 in the same position in the instruction stream. */
1899 return (next_real_insn (XEXP (x, 0))
1900 == next_real_insn (XEXP (y, 0)));
1902 case SYMBOL_REF:
1903 return XSTR (x, 0) == XSTR (y, 0);
1905 case CODE_LABEL:
1906 /* If we didn't match EQ equality above, they aren't the same. */
1907 return 0;
1909 default:
1910 break;
1913 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
1915 if (GET_MODE (x) != GET_MODE (y))
1916 return 0;
1918 /* For commutative operations, the RTX match if the operand match in any
1919 order. Also handle the simple binary and unary cases without a loop.
1921 ??? Don't consider PLUS a commutative operator; see comments above. */
1922 if (COMMUTATIVE_P (x) && code != PLUS)
1923 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1924 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
1925 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
1926 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
1927 else if (NON_COMMUTATIVE_P (x))
1928 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1929 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
1930 else if (UNARY_P (x))
1931 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
1933 /* Compare the elements. If any pair of corresponding elements
1934 fail to match, return 0 for the whole things. */
1936 fmt = GET_RTX_FORMAT (code);
1937 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1939 int j;
1940 switch (fmt[i])
1942 case 'w':
1943 if (XWINT (x, i) != XWINT (y, i))
1944 return 0;
1945 break;
1947 case 'i':
1948 if (XINT (x, i) != XINT (y, i))
1949 return 0;
1950 break;
1952 case 't':
1953 if (XTREE (x, i) != XTREE (y, i))
1954 return 0;
1955 break;
1957 case 's':
1958 if (strcmp (XSTR (x, i), XSTR (y, i)))
1959 return 0;
1960 break;
1962 case 'e':
1963 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
1964 return 0;
1965 break;
1967 case 'u':
1968 if (XEXP (x, i) != XEXP (y, i))
1969 return 0;
1970 /* Fall through. */
1971 case '0':
1972 break;
1974 case 'E':
1975 if (XVECLEN (x, i) != XVECLEN (y, i))
1976 return 0;
1977 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1978 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1979 return 0;
1980 break;
1982 default:
1983 abort ();
1986 return 1;
1989 /* If X is a hard register or equivalent to one or a subregister of one,
1990 return the hard register number. If X is a pseudo register that was not
1991 assigned a hard register, return the pseudo register number. Otherwise,
1992 return -1. Any rtx is valid for X. */
1995 true_regnum (rtx x)
1997 if (REG_P (x))
1999 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
2000 return reg_renumber[REGNO (x)];
2001 return REGNO (x);
2003 if (GET_CODE (x) == SUBREG)
2005 int base = true_regnum (SUBREG_REG (x));
2006 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
2007 return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
2008 GET_MODE (SUBREG_REG (x)),
2009 SUBREG_BYTE (x), GET_MODE (x));
2011 return -1;
2014 /* Return regno of the register REG and handle subregs too. */
2015 unsigned int
2016 reg_or_subregno (rtx reg)
2018 if (REG_P (reg))
2019 return REGNO (reg);
2020 if (GET_CODE (reg) == SUBREG)
2021 return REGNO (SUBREG_REG (reg));
2022 abort ();