crontab: Don't build snapshot for 3.4.x anymore.
[official-gcc.git] / gcc / jump.c
blob38d1146ff2260713ff8319ce6a87dc0b203ef459
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, 2005
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This is the pathetic reminder of old fame of the jump-optimization pass
24 of the compiler. Now it contains basically set of utility function to
25 operate with jumps.
27 Each CODE_LABEL has a count of the times it is used
28 stored in the LABEL_NUSES internal field, and each JUMP_INSN
29 has one label that it refers to stored in the
30 JUMP_LABEL internal field. With this we can detect labels that
31 become unused because of the deletion of all the jumps that
32 formerly used them. The JUMP_LABEL info is sometimes looked
33 at by later passes.
35 The subroutines redirect_jump and invert_jump are used
36 from other passes as well. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h"
42 #include "rtl.h"
43 #include "tm_p.h"
44 #include "flags.h"
45 #include "hard-reg-set.h"
46 #include "regs.h"
47 #include "insn-config.h"
48 #include "insn-attr.h"
49 #include "recog.h"
50 #include "function.h"
51 #include "expr.h"
52 #include "real.h"
53 #include "except.h"
54 #include "diagnostic.h"
55 #include "toplev.h"
56 #include "reload.h"
57 #include "predict.h"
58 #include "timevar.h"
59 #include "tree-pass.h"
60 #include "target.h"
62 /* Optimize jump y; x: ... y: jumpif... x?
63 Don't know if it is worth bothering with. */
64 /* Optimize two cases of conditional jump to conditional jump?
65 This can never delete any instruction or make anything dead,
66 or even change what is live at any point.
67 So perhaps let combiner do it. */
69 static void init_label_info (rtx);
70 static void mark_all_labels (rtx);
71 static void delete_computation (rtx);
72 static void redirect_exp_1 (rtx *, rtx, rtx, rtx);
73 static int invert_exp_1 (rtx, rtx);
74 static int returnjump_p_1 (rtx *, void *);
75 static void delete_prior_computation (rtx, rtx);
77 /* Alternate entry into the jump optimizer. This entry point only rebuilds
78 the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
79 instructions. */
80 void
81 rebuild_jump_labels (rtx f)
83 rtx insn;
85 timevar_push (TV_REBUILD_JUMP);
86 init_label_info (f);
87 mark_all_labels (f);
89 /* Keep track of labels used from static data; we don't track them
90 closely enough to delete them here, so make sure their reference
91 count doesn't drop to zero. */
93 for (insn = forced_labels; insn; insn = XEXP (insn, 1))
94 if (LABEL_P (XEXP (insn, 0)))
95 LABEL_NUSES (XEXP (insn, 0))++;
96 timevar_pop (TV_REBUILD_JUMP);
99 /* Some old code expects exactly one BARRIER as the NEXT_INSN of a
100 non-fallthru insn. This is not generally true, as multiple barriers
101 may have crept in, or the BARRIER may be separated from the last
102 real insn by one or more NOTEs.
104 This simple pass moves barriers and removes duplicates so that the
105 old code is happy.
107 unsigned int
108 cleanup_barriers (void)
110 rtx insn, next, prev;
111 for (insn = get_insns (); insn; insn = next)
113 next = NEXT_INSN (insn);
114 if (BARRIER_P (insn))
116 prev = prev_nonnote_insn (insn);
117 if (BARRIER_P (prev))
118 delete_insn (insn);
119 else if (prev != PREV_INSN (insn))
120 reorder_insns (insn, insn, prev);
123 return 0;
126 struct tree_opt_pass pass_cleanup_barriers =
128 "barriers", /* name */
129 NULL, /* gate */
130 cleanup_barriers, /* execute */
131 NULL, /* sub */
132 NULL, /* next */
133 0, /* static_pass_number */
134 0, /* tv_id */
135 0, /* properties_required */
136 0, /* properties_provided */
137 0, /* properties_destroyed */
138 0, /* todo_flags_start */
139 TODO_dump_func, /* todo_flags_finish */
140 0 /* letter */
143 unsigned int
144 purge_line_number_notes (void)
146 rtx last_note = 0;
147 rtx insn;
148 /* Delete extraneous line number notes.
149 Note that two consecutive notes for different lines are not really
150 extraneous. There should be some indication where that line belonged,
151 even if it became empty. */
153 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
154 if (NOTE_P (insn))
156 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
157 /* Any previous line note was for the prologue; gdb wants a new
158 note after the prologue even if it is for the same line. */
159 last_note = NULL_RTX;
160 else if (NOTE_LINE_NUMBER (insn) >= 0)
162 /* Delete this note if it is identical to previous note. */
163 if (last_note
164 #ifdef USE_MAPPED_LOCATION
165 && NOTE_SOURCE_LOCATION (insn) == NOTE_SOURCE_LOCATION (last_note)
166 #else
167 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
168 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note)
169 #endif
172 delete_related_insns (insn);
173 continue;
176 last_note = insn;
179 return 0;
182 struct tree_opt_pass pass_purge_lineno_notes =
184 "elnotes", /* name */
185 NULL, /* gate */
186 purge_line_number_notes, /* execute */
187 NULL, /* sub */
188 NULL, /* next */
189 0, /* static_pass_number */
190 0, /* tv_id */
191 0, /* properties_required */
192 0, /* properties_provided */
193 0, /* properties_destroyed */
194 0, /* todo_flags_start */
195 TODO_dump_func, /* todo_flags_finish */
196 0 /* letter */
200 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
201 notes whose labels don't occur in the insn any more. Returns the
202 largest INSN_UID found. */
203 static void
204 init_label_info (rtx f)
206 rtx insn;
208 for (insn = f; insn; insn = NEXT_INSN (insn))
209 if (LABEL_P (insn))
210 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
211 else if (JUMP_P (insn))
212 JUMP_LABEL (insn) = 0;
213 else if (NONJUMP_INSN_P (insn) || CALL_P (insn))
215 rtx note, next;
217 for (note = REG_NOTES (insn); note; note = next)
219 next = XEXP (note, 1);
220 if (REG_NOTE_KIND (note) == REG_LABEL
221 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
222 remove_note (insn, note);
227 /* Mark the label each jump jumps to.
228 Combine consecutive labels, and count uses of labels. */
230 static void
231 mark_all_labels (rtx f)
233 rtx insn;
235 for (insn = f; insn; insn = NEXT_INSN (insn))
236 if (INSN_P (insn))
238 mark_jump_label (PATTERN (insn), insn, 0);
239 if (! INSN_DELETED_P (insn) && JUMP_P (insn))
241 /* When we know the LABEL_REF contained in a REG used in
242 an indirect jump, we'll have a REG_LABEL note so that
243 flow can tell where it's going. */
244 if (JUMP_LABEL (insn) == 0)
246 rtx label_note = find_reg_note (insn, REG_LABEL, NULL_RTX);
247 if (label_note)
249 /* But a LABEL_REF around the REG_LABEL note, so
250 that we can canonicalize it. */
251 rtx label_ref = gen_rtx_LABEL_REF (Pmode,
252 XEXP (label_note, 0));
254 mark_jump_label (label_ref, insn, 0);
255 XEXP (label_note, 0) = XEXP (label_ref, 0);
256 JUMP_LABEL (insn) = XEXP (label_note, 0);
263 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
264 notes between START and END out before START. START and END may be such
265 notes. Returns the values of the new starting and ending insns, which
266 may be different if the original ones were such notes.
267 Return true if there were only such notes and no real instructions. */
269 bool
270 squeeze_notes (rtx* startp, rtx* endp)
272 rtx start = *startp;
273 rtx end = *endp;
275 rtx insn;
276 rtx next;
277 rtx last = NULL;
278 rtx past_end = NEXT_INSN (end);
280 for (insn = start; insn != past_end; insn = next)
282 next = NEXT_INSN (insn);
283 if (NOTE_P (insn)
284 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
285 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
286 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
287 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END))
289 /* BLOCK_BEG or BLOCK_END notes only exist in the `final' pass. */
290 gcc_assert (NOTE_LINE_NUMBER (insn) != NOTE_INSN_BLOCK_BEG
291 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BLOCK_END);
293 if (insn == start)
294 start = next;
295 else
297 rtx prev = PREV_INSN (insn);
298 PREV_INSN (insn) = PREV_INSN (start);
299 NEXT_INSN (insn) = start;
300 NEXT_INSN (PREV_INSN (insn)) = insn;
301 PREV_INSN (NEXT_INSN (insn)) = insn;
302 NEXT_INSN (prev) = next;
303 PREV_INSN (next) = prev;
306 else
307 last = insn;
310 /* There were no real instructions. */
311 if (start == past_end)
312 return true;
314 end = last;
316 *startp = start;
317 *endp = end;
318 return false;
321 /* Return the label before INSN, or put a new label there. */
324 get_label_before (rtx insn)
326 rtx label;
328 /* Find an existing label at this point
329 or make a new one if there is none. */
330 label = prev_nonnote_insn (insn);
332 if (label == 0 || !LABEL_P (label))
334 rtx prev = PREV_INSN (insn);
336 label = gen_label_rtx ();
337 emit_label_after (label, prev);
338 LABEL_NUSES (label) = 0;
340 return label;
343 /* Return the label after INSN, or put a new label there. */
346 get_label_after (rtx insn)
348 rtx label;
350 /* Find an existing label at this point
351 or make a new one if there is none. */
352 label = next_nonnote_insn (insn);
354 if (label == 0 || !LABEL_P (label))
356 label = gen_label_rtx ();
357 emit_label_after (label, insn);
358 LABEL_NUSES (label) = 0;
360 return label;
363 /* Given a comparison (CODE ARG0 ARG1), inside an insn, INSN, return a code
364 of reversed comparison if it is possible to do so. Otherwise return UNKNOWN.
365 UNKNOWN may be returned in case we are having CC_MODE compare and we don't
366 know whether it's source is floating point or integer comparison. Machine
367 description should define REVERSIBLE_CC_MODE and REVERSE_CONDITION macros
368 to help this function avoid overhead in these cases. */
369 enum rtx_code
370 reversed_comparison_code_parts (enum rtx_code code, rtx arg0, rtx arg1, rtx insn)
372 enum machine_mode mode;
374 /* If this is not actually a comparison, we can't reverse it. */
375 if (GET_RTX_CLASS (code) != RTX_COMPARE
376 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE)
377 return UNKNOWN;
379 mode = GET_MODE (arg0);
380 if (mode == VOIDmode)
381 mode = GET_MODE (arg1);
383 /* First see if machine description supplies us way to reverse the
384 comparison. Give it priority over everything else to allow
385 machine description to do tricks. */
386 if (GET_MODE_CLASS (mode) == MODE_CC
387 && REVERSIBLE_CC_MODE (mode))
389 #ifdef REVERSE_CONDITION
390 return REVERSE_CONDITION (code, mode);
391 #endif
392 return reverse_condition (code);
395 /* Try a few special cases based on the comparison code. */
396 switch (code)
398 case GEU:
399 case GTU:
400 case LEU:
401 case LTU:
402 case NE:
403 case EQ:
404 /* It is always safe to reverse EQ and NE, even for the floating
405 point. Similarly the unsigned comparisons are never used for
406 floating point so we can reverse them in the default way. */
407 return reverse_condition (code);
408 case ORDERED:
409 case UNORDERED:
410 case LTGT:
411 case UNEQ:
412 /* In case we already see unordered comparison, we can be sure to
413 be dealing with floating point so we don't need any more tests. */
414 return reverse_condition_maybe_unordered (code);
415 case UNLT:
416 case UNLE:
417 case UNGT:
418 case UNGE:
419 /* We don't have safe way to reverse these yet. */
420 return UNKNOWN;
421 default:
422 break;
425 if (GET_MODE_CLASS (mode) == MODE_CC || CC0_P (arg0))
427 rtx prev;
428 /* Try to search for the comparison to determine the real mode.
429 This code is expensive, but with sane machine description it
430 will be never used, since REVERSIBLE_CC_MODE will return true
431 in all cases. */
432 if (! insn)
433 return UNKNOWN;
435 for (prev = prev_nonnote_insn (insn);
436 prev != 0 && !LABEL_P (prev);
437 prev = prev_nonnote_insn (prev))
439 rtx set = set_of (arg0, prev);
440 if (set && GET_CODE (set) == SET
441 && rtx_equal_p (SET_DEST (set), arg0))
443 rtx src = SET_SRC (set);
445 if (GET_CODE (src) == COMPARE)
447 rtx comparison = src;
448 arg0 = XEXP (src, 0);
449 mode = GET_MODE (arg0);
450 if (mode == VOIDmode)
451 mode = GET_MODE (XEXP (comparison, 1));
452 break;
454 /* We can get past reg-reg moves. This may be useful for model
455 of i387 comparisons that first move flag registers around. */
456 if (REG_P (src))
458 arg0 = src;
459 continue;
462 /* If register is clobbered in some ununderstandable way,
463 give up. */
464 if (set)
465 return UNKNOWN;
469 /* Test for an integer condition, or a floating-point comparison
470 in which NaNs can be ignored. */
471 if (GET_CODE (arg0) == CONST_INT
472 || (GET_MODE (arg0) != VOIDmode
473 && GET_MODE_CLASS (mode) != MODE_CC
474 && !HONOR_NANS (mode)))
475 return reverse_condition (code);
477 return UNKNOWN;
480 /* A wrapper around the previous function to take COMPARISON as rtx
481 expression. This simplifies many callers. */
482 enum rtx_code
483 reversed_comparison_code (rtx comparison, rtx insn)
485 if (!COMPARISON_P (comparison))
486 return UNKNOWN;
487 return reversed_comparison_code_parts (GET_CODE (comparison),
488 XEXP (comparison, 0),
489 XEXP (comparison, 1), insn);
492 /* Return comparison with reversed code of EXP.
493 Return NULL_RTX in case we fail to do the reversal. */
495 reversed_comparison (rtx exp, enum machine_mode mode)
497 enum rtx_code reversed_code = reversed_comparison_code (exp, NULL_RTX);
498 if (reversed_code == UNKNOWN)
499 return NULL_RTX;
500 else
501 return simplify_gen_relational (reversed_code, mode, VOIDmode,
502 XEXP (exp, 0), XEXP (exp, 1));
506 /* Given an rtx-code for a comparison, return the code for the negated
507 comparison. If no such code exists, return UNKNOWN.
509 WATCH OUT! reverse_condition is not safe to use on a jump that might
510 be acting on the results of an IEEE floating point comparison, because
511 of the special treatment of non-signaling nans in comparisons.
512 Use reversed_comparison_code instead. */
514 enum rtx_code
515 reverse_condition (enum rtx_code code)
517 switch (code)
519 case EQ:
520 return NE;
521 case NE:
522 return EQ;
523 case GT:
524 return LE;
525 case GE:
526 return LT;
527 case LT:
528 return GE;
529 case LE:
530 return GT;
531 case GTU:
532 return LEU;
533 case GEU:
534 return LTU;
535 case LTU:
536 return GEU;
537 case LEU:
538 return GTU;
539 case UNORDERED:
540 return ORDERED;
541 case ORDERED:
542 return UNORDERED;
544 case UNLT:
545 case UNLE:
546 case UNGT:
547 case UNGE:
548 case UNEQ:
549 case LTGT:
550 return UNKNOWN;
552 default:
553 gcc_unreachable ();
557 /* Similar, but we're allowed to generate unordered comparisons, which
558 makes it safe for IEEE floating-point. Of course, we have to recognize
559 that the target will support them too... */
561 enum rtx_code
562 reverse_condition_maybe_unordered (enum rtx_code code)
564 switch (code)
566 case EQ:
567 return NE;
568 case NE:
569 return EQ;
570 case GT:
571 return UNLE;
572 case GE:
573 return UNLT;
574 case LT:
575 return UNGE;
576 case LE:
577 return UNGT;
578 case LTGT:
579 return UNEQ;
580 case UNORDERED:
581 return ORDERED;
582 case ORDERED:
583 return UNORDERED;
584 case UNLT:
585 return GE;
586 case UNLE:
587 return GT;
588 case UNGT:
589 return LE;
590 case UNGE:
591 return LT;
592 case UNEQ:
593 return LTGT;
595 default:
596 gcc_unreachable ();
600 /* Similar, but return the code when two operands of a comparison are swapped.
601 This IS safe for IEEE floating-point. */
603 enum rtx_code
604 swap_condition (enum rtx_code code)
606 switch (code)
608 case EQ:
609 case NE:
610 case UNORDERED:
611 case ORDERED:
612 case UNEQ:
613 case LTGT:
614 return code;
616 case GT:
617 return LT;
618 case GE:
619 return LE;
620 case LT:
621 return GT;
622 case LE:
623 return GE;
624 case GTU:
625 return LTU;
626 case GEU:
627 return LEU;
628 case LTU:
629 return GTU;
630 case LEU:
631 return GEU;
632 case UNLT:
633 return UNGT;
634 case UNLE:
635 return UNGE;
636 case UNGT:
637 return UNLT;
638 case UNGE:
639 return UNLE;
641 default:
642 gcc_unreachable ();
646 /* Given a comparison CODE, return the corresponding unsigned comparison.
647 If CODE is an equality comparison or already an unsigned comparison,
648 CODE is returned. */
650 enum rtx_code
651 unsigned_condition (enum rtx_code code)
653 switch (code)
655 case EQ:
656 case NE:
657 case GTU:
658 case GEU:
659 case LTU:
660 case LEU:
661 return code;
663 case GT:
664 return GTU;
665 case GE:
666 return GEU;
667 case LT:
668 return LTU;
669 case LE:
670 return LEU;
672 default:
673 gcc_unreachable ();
677 /* Similarly, return the signed version of a comparison. */
679 enum rtx_code
680 signed_condition (enum rtx_code code)
682 switch (code)
684 case EQ:
685 case NE:
686 case GT:
687 case GE:
688 case LT:
689 case LE:
690 return code;
692 case GTU:
693 return GT;
694 case GEU:
695 return GE;
696 case LTU:
697 return LT;
698 case LEU:
699 return LE;
701 default:
702 gcc_unreachable ();
706 /* Return nonzero if CODE1 is more strict than CODE2, i.e., if the
707 truth of CODE1 implies the truth of CODE2. */
710 comparison_dominates_p (enum rtx_code code1, enum rtx_code code2)
712 /* UNKNOWN comparison codes can happen as a result of trying to revert
713 comparison codes.
714 They can't match anything, so we have to reject them here. */
715 if (code1 == UNKNOWN || code2 == UNKNOWN)
716 return 0;
718 if (code1 == code2)
719 return 1;
721 switch (code1)
723 case UNEQ:
724 if (code2 == UNLE || code2 == UNGE)
725 return 1;
726 break;
728 case EQ:
729 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU
730 || code2 == ORDERED)
731 return 1;
732 break;
734 case UNLT:
735 if (code2 == UNLE || code2 == NE)
736 return 1;
737 break;
739 case LT:
740 if (code2 == LE || code2 == NE || code2 == ORDERED || code2 == LTGT)
741 return 1;
742 break;
744 case UNGT:
745 if (code2 == UNGE || code2 == NE)
746 return 1;
747 break;
749 case GT:
750 if (code2 == GE || code2 == NE || code2 == ORDERED || code2 == LTGT)
751 return 1;
752 break;
754 case GE:
755 case LE:
756 if (code2 == ORDERED)
757 return 1;
758 break;
760 case LTGT:
761 if (code2 == NE || code2 == ORDERED)
762 return 1;
763 break;
765 case LTU:
766 if (code2 == LEU || code2 == NE)
767 return 1;
768 break;
770 case GTU:
771 if (code2 == GEU || code2 == NE)
772 return 1;
773 break;
775 case UNORDERED:
776 if (code2 == NE || code2 == UNEQ || code2 == UNLE || code2 == UNLT
777 || code2 == UNGE || code2 == UNGT)
778 return 1;
779 break;
781 default:
782 break;
785 return 0;
788 /* Return 1 if INSN is an unconditional jump and nothing else. */
791 simplejump_p (rtx insn)
793 return (JUMP_P (insn)
794 && GET_CODE (PATTERN (insn)) == SET
795 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
796 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
799 /* Return nonzero if INSN is a (possibly) conditional jump
800 and nothing more.
802 Use of this function is deprecated, since we need to support combined
803 branch and compare insns. Use any_condjump_p instead whenever possible. */
806 condjump_p (rtx insn)
808 rtx x = PATTERN (insn);
810 if (GET_CODE (x) != SET
811 || GET_CODE (SET_DEST (x)) != PC)
812 return 0;
814 x = SET_SRC (x);
815 if (GET_CODE (x) == LABEL_REF)
816 return 1;
817 else
818 return (GET_CODE (x) == IF_THEN_ELSE
819 && ((GET_CODE (XEXP (x, 2)) == PC
820 && (GET_CODE (XEXP (x, 1)) == LABEL_REF
821 || GET_CODE (XEXP (x, 1)) == RETURN))
822 || (GET_CODE (XEXP (x, 1)) == PC
823 && (GET_CODE (XEXP (x, 2)) == LABEL_REF
824 || GET_CODE (XEXP (x, 2)) == RETURN))));
827 /* Return nonzero if INSN is a (possibly) conditional jump inside a
828 PARALLEL.
830 Use this function is deprecated, since we need to support combined
831 branch and compare insns. Use any_condjump_p instead whenever possible. */
834 condjump_in_parallel_p (rtx insn)
836 rtx x = PATTERN (insn);
838 if (GET_CODE (x) != PARALLEL)
839 return 0;
840 else
841 x = XVECEXP (x, 0, 0);
843 if (GET_CODE (x) != SET)
844 return 0;
845 if (GET_CODE (SET_DEST (x)) != PC)
846 return 0;
847 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
848 return 1;
849 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
850 return 0;
851 if (XEXP (SET_SRC (x), 2) == pc_rtx
852 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
853 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
854 return 1;
855 if (XEXP (SET_SRC (x), 1) == pc_rtx
856 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
857 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
858 return 1;
859 return 0;
862 /* Return set of PC, otherwise NULL. */
865 pc_set (rtx insn)
867 rtx pat;
868 if (!JUMP_P (insn))
869 return NULL_RTX;
870 pat = PATTERN (insn);
872 /* The set is allowed to appear either as the insn pattern or
873 the first set in a PARALLEL. */
874 if (GET_CODE (pat) == PARALLEL)
875 pat = XVECEXP (pat, 0, 0);
876 if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == PC)
877 return pat;
879 return NULL_RTX;
882 /* Return true when insn is an unconditional direct jump,
883 possibly bundled inside a PARALLEL. */
886 any_uncondjump_p (rtx insn)
888 rtx x = pc_set (insn);
889 if (!x)
890 return 0;
891 if (GET_CODE (SET_SRC (x)) != LABEL_REF)
892 return 0;
893 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
894 return 0;
895 return 1;
898 /* Return true when insn is a conditional jump. This function works for
899 instructions containing PC sets in PARALLELs. The instruction may have
900 various other effects so before removing the jump you must verify
901 onlyjump_p.
903 Note that unlike condjump_p it returns false for unconditional jumps. */
906 any_condjump_p (rtx insn)
908 rtx x = pc_set (insn);
909 enum rtx_code a, b;
911 if (!x)
912 return 0;
913 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
914 return 0;
916 a = GET_CODE (XEXP (SET_SRC (x), 1));
917 b = GET_CODE (XEXP (SET_SRC (x), 2));
919 return ((b == PC && (a == LABEL_REF || a == RETURN))
920 || (a == PC && (b == LABEL_REF || b == RETURN)));
923 /* Return the label of a conditional jump. */
926 condjump_label (rtx insn)
928 rtx x = pc_set (insn);
930 if (!x)
931 return NULL_RTX;
932 x = SET_SRC (x);
933 if (GET_CODE (x) == LABEL_REF)
934 return x;
935 if (GET_CODE (x) != IF_THEN_ELSE)
936 return NULL_RTX;
937 if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
938 return XEXP (x, 1);
939 if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
940 return XEXP (x, 2);
941 return NULL_RTX;
944 /* Return true if INSN is a (possibly conditional) return insn. */
946 static int
947 returnjump_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
949 rtx x = *loc;
951 return x && (GET_CODE (x) == RETURN
952 || (GET_CODE (x) == SET && SET_IS_RETURN_P (x)));
956 returnjump_p (rtx insn)
958 if (!JUMP_P (insn))
959 return 0;
960 return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
963 /* Return true if INSN is a jump that only transfers control and
964 nothing more. */
967 onlyjump_p (rtx insn)
969 rtx set;
971 if (!JUMP_P (insn))
972 return 0;
974 set = single_set (insn);
975 if (set == NULL)
976 return 0;
977 if (GET_CODE (SET_DEST (set)) != PC)
978 return 0;
979 if (side_effects_p (SET_SRC (set)))
980 return 0;
982 return 1;
985 #ifdef HAVE_cc0
987 /* Return nonzero if X is an RTX that only sets the condition codes
988 and has no side effects. */
991 only_sets_cc0_p (rtx x)
993 if (! x)
994 return 0;
996 if (INSN_P (x))
997 x = PATTERN (x);
999 return sets_cc0_p (x) == 1 && ! side_effects_p (x);
1002 /* Return 1 if X is an RTX that does nothing but set the condition codes
1003 and CLOBBER or USE registers.
1004 Return -1 if X does explicitly set the condition codes,
1005 but also does other things. */
1008 sets_cc0_p (rtx x)
1010 if (! x)
1011 return 0;
1013 if (INSN_P (x))
1014 x = PATTERN (x);
1016 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
1017 return 1;
1018 if (GET_CODE (x) == PARALLEL)
1020 int i;
1021 int sets_cc0 = 0;
1022 int other_things = 0;
1023 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1025 if (GET_CODE (XVECEXP (x, 0, i)) == SET
1026 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
1027 sets_cc0 = 1;
1028 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
1029 other_things = 1;
1031 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
1033 return 0;
1035 #endif
1037 /* Follow any unconditional jump at LABEL;
1038 return the ultimate label reached by any such chain of jumps.
1039 Return null if the chain ultimately leads to a return instruction.
1040 If LABEL is not followed by a jump, return LABEL.
1041 If the chain loops or we can't find end, return LABEL,
1042 since that tells caller to avoid changing the insn.
1044 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
1045 a USE or CLOBBER. */
1048 follow_jumps (rtx label)
1050 rtx insn;
1051 rtx next;
1052 rtx value = label;
1053 int depth;
1055 for (depth = 0;
1056 (depth < 10
1057 && (insn = next_active_insn (value)) != 0
1058 && JUMP_P (insn)
1059 && ((JUMP_LABEL (insn) != 0 && any_uncondjump_p (insn)
1060 && onlyjump_p (insn))
1061 || GET_CODE (PATTERN (insn)) == RETURN)
1062 && (next = NEXT_INSN (insn))
1063 && BARRIER_P (next));
1064 depth++)
1066 /* Don't chain through the insn that jumps into a loop
1067 from outside the loop,
1068 since that would create multiple loop entry jumps
1069 and prevent loop optimization. */
1070 rtx tem;
1071 if (!reload_completed)
1072 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
1073 if (NOTE_P (tem)
1074 && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
1075 /* ??? Optional. Disables some optimizations, but makes
1076 gcov output more accurate with -O. */
1077 || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
1078 return value;
1080 /* If we have found a cycle, make the insn jump to itself. */
1081 if (JUMP_LABEL (insn) == label)
1082 return label;
1084 tem = next_active_insn (JUMP_LABEL (insn));
1085 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
1086 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
1087 break;
1089 value = JUMP_LABEL (insn);
1091 if (depth == 10)
1092 return label;
1093 return value;
1097 /* Find all CODE_LABELs referred to in X, and increment their use counts.
1098 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
1099 in INSN, then store one of them in JUMP_LABEL (INSN).
1100 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
1101 referenced in INSN, add a REG_LABEL note containing that label to INSN.
1102 Also, when there are consecutive labels, canonicalize on the last of them.
1104 Note that two labels separated by a loop-beginning note
1105 must be kept distinct if we have not yet done loop-optimization,
1106 because the gap between them is where loop-optimize
1107 will want to move invariant code to. CROSS_JUMP tells us
1108 that loop-optimization is done with. */
1110 void
1111 mark_jump_label (rtx x, rtx insn, int in_mem)
1113 RTX_CODE code = GET_CODE (x);
1114 int i;
1115 const char *fmt;
1117 switch (code)
1119 case PC:
1120 case CC0:
1121 case REG:
1122 case CONST_INT:
1123 case CONST_DOUBLE:
1124 case CLOBBER:
1125 case CALL:
1126 return;
1128 case MEM:
1129 in_mem = 1;
1130 break;
1132 case SYMBOL_REF:
1133 if (!in_mem)
1134 return;
1136 /* If this is a constant-pool reference, see if it is a label. */
1137 if (CONSTANT_POOL_ADDRESS_P (x))
1138 mark_jump_label (get_pool_constant (x), insn, in_mem);
1139 break;
1141 case LABEL_REF:
1143 rtx label = XEXP (x, 0);
1145 /* Ignore remaining references to unreachable labels that
1146 have been deleted. */
1147 if (NOTE_P (label)
1148 && NOTE_LINE_NUMBER (label) == NOTE_INSN_DELETED_LABEL)
1149 break;
1151 gcc_assert (LABEL_P (label));
1153 /* Ignore references to labels of containing functions. */
1154 if (LABEL_REF_NONLOCAL_P (x))
1155 break;
1157 XEXP (x, 0) = label;
1158 if (! insn || ! INSN_DELETED_P (insn))
1159 ++LABEL_NUSES (label);
1161 if (insn)
1163 if (JUMP_P (insn))
1164 JUMP_LABEL (insn) = label;
1165 else
1167 /* Add a REG_LABEL note for LABEL unless there already
1168 is one. All uses of a label, except for labels
1169 that are the targets of jumps, must have a
1170 REG_LABEL note. */
1171 if (! find_reg_note (insn, REG_LABEL, label))
1172 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
1173 REG_NOTES (insn));
1176 return;
1179 /* Do walk the labels in a vector, but not the first operand of an
1180 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
1181 case ADDR_VEC:
1182 case ADDR_DIFF_VEC:
1183 if (! INSN_DELETED_P (insn))
1185 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
1187 for (i = 0; i < XVECLEN (x, eltnum); i++)
1188 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, in_mem);
1190 return;
1192 default:
1193 break;
1196 fmt = GET_RTX_FORMAT (code);
1197 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1199 if (fmt[i] == 'e')
1200 mark_jump_label (XEXP (x, i), insn, in_mem);
1201 else if (fmt[i] == 'E')
1203 int j;
1204 for (j = 0; j < XVECLEN (x, i); j++)
1205 mark_jump_label (XVECEXP (x, i, j), insn, in_mem);
1210 /* If all INSN does is set the pc, delete it,
1211 and delete the insn that set the condition codes for it
1212 if that's what the previous thing was. */
1214 void
1215 delete_jump (rtx insn)
1217 rtx set = single_set (insn);
1219 if (set && GET_CODE (SET_DEST (set)) == PC)
1220 delete_computation (insn);
1223 /* Recursively delete prior insns that compute the value (used only by INSN
1224 which the caller is deleting) stored in the register mentioned by NOTE
1225 which is a REG_DEAD note associated with INSN. */
1227 static void
1228 delete_prior_computation (rtx note, rtx insn)
1230 rtx our_prev;
1231 rtx reg = XEXP (note, 0);
1233 for (our_prev = prev_nonnote_insn (insn);
1234 our_prev && (NONJUMP_INSN_P (our_prev)
1235 || CALL_P (our_prev));
1236 our_prev = prev_nonnote_insn (our_prev))
1238 rtx pat = PATTERN (our_prev);
1240 /* If we reach a CALL which is not calling a const function
1241 or the callee pops the arguments, then give up. */
1242 if (CALL_P (our_prev)
1243 && (! CONST_OR_PURE_CALL_P (our_prev)
1244 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
1245 break;
1247 /* If we reach a SEQUENCE, it is too complex to try to
1248 do anything with it, so give up. We can be run during
1249 and after reorg, so SEQUENCE rtl can legitimately show
1250 up here. */
1251 if (GET_CODE (pat) == SEQUENCE)
1252 break;
1254 if (GET_CODE (pat) == USE
1255 && NONJUMP_INSN_P (XEXP (pat, 0)))
1256 /* reorg creates USEs that look like this. We leave them
1257 alone because reorg needs them for its own purposes. */
1258 break;
1260 if (reg_set_p (reg, pat))
1262 if (side_effects_p (pat) && !CALL_P (our_prev))
1263 break;
1265 if (GET_CODE (pat) == PARALLEL)
1267 /* If we find a SET of something else, we can't
1268 delete the insn. */
1270 int i;
1272 for (i = 0; i < XVECLEN (pat, 0); i++)
1274 rtx part = XVECEXP (pat, 0, i);
1276 if (GET_CODE (part) == SET
1277 && SET_DEST (part) != reg)
1278 break;
1281 if (i == XVECLEN (pat, 0))
1282 delete_computation (our_prev);
1284 else if (GET_CODE (pat) == SET
1285 && REG_P (SET_DEST (pat)))
1287 int dest_regno = REGNO (SET_DEST (pat));
1288 int dest_endregno
1289 = (dest_regno
1290 + (dest_regno < FIRST_PSEUDO_REGISTER
1291 ? hard_regno_nregs[dest_regno]
1292 [GET_MODE (SET_DEST (pat))] : 1));
1293 int regno = REGNO (reg);
1294 int endregno
1295 = (regno
1296 + (regno < FIRST_PSEUDO_REGISTER
1297 ? hard_regno_nregs[regno][GET_MODE (reg)] : 1));
1299 if (dest_regno >= regno
1300 && dest_endregno <= endregno)
1301 delete_computation (our_prev);
1303 /* We may have a multi-word hard register and some, but not
1304 all, of the words of the register are needed in subsequent
1305 insns. Write REG_UNUSED notes for those parts that were not
1306 needed. */
1307 else if (dest_regno <= regno
1308 && dest_endregno >= endregno)
1310 int i;
1312 REG_NOTES (our_prev)
1313 = gen_rtx_EXPR_LIST (REG_UNUSED, reg,
1314 REG_NOTES (our_prev));
1316 for (i = dest_regno; i < dest_endregno; i++)
1317 if (! find_regno_note (our_prev, REG_UNUSED, i))
1318 break;
1320 if (i == dest_endregno)
1321 delete_computation (our_prev);
1325 break;
1328 /* If PAT references the register that dies here, it is an
1329 additional use. Hence any prior SET isn't dead. However, this
1330 insn becomes the new place for the REG_DEAD note. */
1331 if (reg_overlap_mentioned_p (reg, pat))
1333 XEXP (note, 1) = REG_NOTES (our_prev);
1334 REG_NOTES (our_prev) = note;
1335 break;
1340 /* Delete INSN and recursively delete insns that compute values used only
1341 by INSN. This uses the REG_DEAD notes computed during flow analysis.
1342 If we are running before flow.c, we need do nothing since flow.c will
1343 delete dead code. We also can't know if the registers being used are
1344 dead or not at this point.
1346 Otherwise, look at all our REG_DEAD notes. If a previous insn does
1347 nothing other than set a register that dies in this insn, we can delete
1348 that insn as well.
1350 On machines with CC0, if CC0 is used in this insn, we may be able to
1351 delete the insn that set it. */
1353 static void
1354 delete_computation (rtx insn)
1356 rtx note, next;
1358 #ifdef HAVE_cc0
1359 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
1361 rtx prev = prev_nonnote_insn (insn);
1362 /* We assume that at this stage
1363 CC's are always set explicitly
1364 and always immediately before the jump that
1365 will use them. So if the previous insn
1366 exists to set the CC's, delete it
1367 (unless it performs auto-increments, etc.). */
1368 if (prev && NONJUMP_INSN_P (prev)
1369 && sets_cc0_p (PATTERN (prev)))
1371 if (sets_cc0_p (PATTERN (prev)) > 0
1372 && ! side_effects_p (PATTERN (prev)))
1373 delete_computation (prev);
1374 else
1375 /* Otherwise, show that cc0 won't be used. */
1376 REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
1377 cc0_rtx, REG_NOTES (prev));
1380 #endif
1382 for (note = REG_NOTES (insn); note; note = next)
1384 next = XEXP (note, 1);
1386 if (REG_NOTE_KIND (note) != REG_DEAD
1387 /* Verify that the REG_NOTE is legitimate. */
1388 || !REG_P (XEXP (note, 0)))
1389 continue;
1391 delete_prior_computation (note, insn);
1394 delete_related_insns (insn);
1397 /* Delete insn INSN from the chain of insns and update label ref counts
1398 and delete insns now unreachable.
1400 Returns the first insn after INSN that was not deleted.
1402 Usage of this instruction is deprecated. Use delete_insn instead and
1403 subsequent cfg_cleanup pass to delete unreachable code if needed. */
1406 delete_related_insns (rtx insn)
1408 int was_code_label = (LABEL_P (insn));
1409 rtx note;
1410 rtx next = NEXT_INSN (insn), prev = PREV_INSN (insn);
1412 while (next && INSN_DELETED_P (next))
1413 next = NEXT_INSN (next);
1415 /* This insn is already deleted => return first following nondeleted. */
1416 if (INSN_DELETED_P (insn))
1417 return next;
1419 delete_insn (insn);
1421 /* If instruction is followed by a barrier,
1422 delete the barrier too. */
1424 if (next != 0 && BARRIER_P (next))
1425 delete_insn (next);
1427 /* If deleting a jump, decrement the count of the label,
1428 and delete the label if it is now unused. */
1430 if (JUMP_P (insn) && JUMP_LABEL (insn))
1432 rtx lab = JUMP_LABEL (insn), lab_next;
1434 if (LABEL_NUSES (lab) == 0)
1436 /* This can delete NEXT or PREV,
1437 either directly if NEXT is JUMP_LABEL (INSN),
1438 or indirectly through more levels of jumps. */
1439 delete_related_insns (lab);
1441 /* I feel a little doubtful about this loop,
1442 but I see no clean and sure alternative way
1443 to find the first insn after INSN that is not now deleted.
1444 I hope this works. */
1445 while (next && INSN_DELETED_P (next))
1446 next = NEXT_INSN (next);
1447 return next;
1449 else if (tablejump_p (insn, NULL, &lab_next))
1451 /* If we're deleting the tablejump, delete the dispatch table.
1452 We may not be able to kill the label immediately preceding
1453 just yet, as it might be referenced in code leading up to
1454 the tablejump. */
1455 delete_related_insns (lab_next);
1459 /* Likewise if we're deleting a dispatch table. */
1461 if (JUMP_P (insn)
1462 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
1463 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
1465 rtx pat = PATTERN (insn);
1466 int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1467 int len = XVECLEN (pat, diff_vec_p);
1469 for (i = 0; i < len; i++)
1470 if (LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
1471 delete_related_insns (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
1472 while (next && INSN_DELETED_P (next))
1473 next = NEXT_INSN (next);
1474 return next;
1477 /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note. */
1478 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
1479 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1480 if (REG_NOTE_KIND (note) == REG_LABEL
1481 /* This could also be a NOTE_INSN_DELETED_LABEL note. */
1482 && LABEL_P (XEXP (note, 0)))
1483 if (LABEL_NUSES (XEXP (note, 0)) == 0)
1484 delete_related_insns (XEXP (note, 0));
1486 while (prev && (INSN_DELETED_P (prev) || NOTE_P (prev)))
1487 prev = PREV_INSN (prev);
1489 /* If INSN was a label and a dispatch table follows it,
1490 delete the dispatch table. The tablejump must have gone already.
1491 It isn't useful to fall through into a table. */
1493 if (was_code_label
1494 && NEXT_INSN (insn) != 0
1495 && JUMP_P (NEXT_INSN (insn))
1496 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
1497 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
1498 next = delete_related_insns (NEXT_INSN (insn));
1500 /* If INSN was a label, delete insns following it if now unreachable. */
1502 if (was_code_label && prev && BARRIER_P (prev))
1504 enum rtx_code code;
1505 while (next)
1507 code = GET_CODE (next);
1508 if (code == NOTE
1509 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
1510 next = NEXT_INSN (next);
1511 /* Keep going past other deleted labels to delete what follows. */
1512 else if (code == CODE_LABEL && INSN_DELETED_P (next))
1513 next = NEXT_INSN (next);
1514 else if (code == BARRIER || INSN_P (next))
1515 /* Note: if this deletes a jump, it can cause more
1516 deletion of unreachable code, after a different label.
1517 As long as the value from this recursive call is correct,
1518 this invocation functions correctly. */
1519 next = delete_related_insns (next);
1520 else
1521 break;
1525 return next;
1528 /* Delete a range of insns from FROM to TO, inclusive.
1529 This is for the sake of peephole optimization, so assume
1530 that whatever these insns do will still be done by a new
1531 peephole insn that will replace them. */
1533 void
1534 delete_for_peephole (rtx from, rtx to)
1536 rtx insn = from;
1538 while (1)
1540 rtx next = NEXT_INSN (insn);
1541 rtx prev = PREV_INSN (insn);
1543 if (!NOTE_P (insn))
1545 INSN_DELETED_P (insn) = 1;
1547 /* Patch this insn out of the chain. */
1548 /* We don't do this all at once, because we
1549 must preserve all NOTEs. */
1550 if (prev)
1551 NEXT_INSN (prev) = next;
1553 if (next)
1554 PREV_INSN (next) = prev;
1557 if (insn == to)
1558 break;
1559 insn = next;
1562 /* Note that if TO is an unconditional jump
1563 we *do not* delete the BARRIER that follows,
1564 since the peephole that replaces this sequence
1565 is also an unconditional jump in that case. */
1568 /* Throughout LOC, redirect OLABEL to NLABEL. Treat null OLABEL or
1569 NLABEL as a return. Accrue modifications into the change group. */
1571 static void
1572 redirect_exp_1 (rtx *loc, rtx olabel, rtx nlabel, rtx insn)
1574 rtx x = *loc;
1575 RTX_CODE code = GET_CODE (x);
1576 int i;
1577 const char *fmt;
1579 if (code == LABEL_REF)
1581 if (XEXP (x, 0) == olabel)
1583 rtx n;
1584 if (nlabel)
1585 n = gen_rtx_LABEL_REF (Pmode, nlabel);
1586 else
1587 n = gen_rtx_RETURN (VOIDmode);
1589 validate_change (insn, loc, n, 1);
1590 return;
1593 else if (code == RETURN && olabel == 0)
1595 if (nlabel)
1596 x = gen_rtx_LABEL_REF (Pmode, nlabel);
1597 else
1598 x = gen_rtx_RETURN (VOIDmode);
1599 if (loc == &PATTERN (insn))
1600 x = gen_rtx_SET (VOIDmode, pc_rtx, x);
1601 validate_change (insn, loc, x, 1);
1602 return;
1605 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
1606 && GET_CODE (SET_SRC (x)) == LABEL_REF
1607 && XEXP (SET_SRC (x), 0) == olabel)
1609 validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 1);
1610 return;
1613 fmt = GET_RTX_FORMAT (code);
1614 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1616 if (fmt[i] == 'e')
1617 redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
1618 else if (fmt[i] == 'E')
1620 int j;
1621 for (j = 0; j < XVECLEN (x, i); j++)
1622 redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
1627 /* Make JUMP go to NLABEL instead of where it jumps now. Accrue
1628 the modifications into the change group. Return false if we did
1629 not see how to do that. */
1632 redirect_jump_1 (rtx jump, rtx nlabel)
1634 int ochanges = num_validated_changes ();
1635 rtx *loc;
1637 if (GET_CODE (PATTERN (jump)) == PARALLEL)
1638 loc = &XVECEXP (PATTERN (jump), 0, 0);
1639 else
1640 loc = &PATTERN (jump);
1642 redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
1643 return num_validated_changes () > ochanges;
1646 /* Make JUMP go to NLABEL instead of where it jumps now. If the old
1647 jump target label is unused as a result, it and the code following
1648 it may be deleted.
1650 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
1651 RETURN insn.
1653 The return value will be 1 if the change was made, 0 if it wasn't
1654 (this can only occur for NLABEL == 0). */
1657 redirect_jump (rtx jump, rtx nlabel, int delete_unused)
1659 rtx olabel = JUMP_LABEL (jump);
1661 if (nlabel == olabel)
1662 return 1;
1664 if (! redirect_jump_1 (jump, nlabel) || ! apply_change_group ())
1665 return 0;
1667 redirect_jump_2 (jump, olabel, nlabel, delete_unused, 0);
1668 return 1;
1671 /* Fix up JUMP_LABEL and label ref counts after OLABEL has been replaced with
1672 NLABEL in JUMP. If DELETE_UNUSED is non-negative, copy a
1673 NOTE_INSN_FUNCTION_END found after OLABEL to the place after NLABEL.
1674 If DELETE_UNUSED is positive, delete related insn to OLABEL if its ref
1675 count has dropped to zero. */
1676 void
1677 redirect_jump_2 (rtx jump, rtx olabel, rtx nlabel, int delete_unused,
1678 int invert)
1680 rtx note;
1682 JUMP_LABEL (jump) = nlabel;
1683 if (nlabel)
1684 ++LABEL_NUSES (nlabel);
1686 /* Update labels in any REG_EQUAL note. */
1687 if ((note = find_reg_note (jump, REG_EQUAL, NULL_RTX)) != NULL_RTX)
1689 if (!nlabel || (invert && !invert_exp_1 (XEXP (note, 0), jump)))
1690 remove_note (jump, note);
1691 else
1693 redirect_exp_1 (&XEXP (note, 0), olabel, nlabel, jump);
1694 confirm_change_group ();
1698 /* If we're eliding the jump over exception cleanups at the end of a
1699 function, move the function end note so that -Wreturn-type works. */
1700 if (olabel && nlabel
1701 && NEXT_INSN (olabel)
1702 && NOTE_P (NEXT_INSN (olabel))
1703 && NOTE_LINE_NUMBER (NEXT_INSN (olabel)) == NOTE_INSN_FUNCTION_END
1704 && delete_unused >= 0)
1705 emit_note_after (NOTE_INSN_FUNCTION_END, nlabel);
1707 if (olabel && --LABEL_NUSES (olabel) == 0 && delete_unused > 0
1708 /* Undefined labels will remain outside the insn stream. */
1709 && INSN_UID (olabel))
1710 delete_related_insns (olabel);
1711 if (invert)
1712 invert_br_probabilities (jump);
1715 /* Invert the jump condition X contained in jump insn INSN. Accrue the
1716 modifications into the change group. Return nonzero for success. */
1717 static int
1718 invert_exp_1 (rtx x, rtx insn)
1720 RTX_CODE code = GET_CODE (x);
1722 if (code == IF_THEN_ELSE)
1724 rtx comp = XEXP (x, 0);
1725 rtx tem;
1726 enum rtx_code reversed_code;
1728 /* We can do this in two ways: The preferable way, which can only
1729 be done if this is not an integer comparison, is to reverse
1730 the comparison code. Otherwise, swap the THEN-part and ELSE-part
1731 of the IF_THEN_ELSE. If we can't do either, fail. */
1733 reversed_code = reversed_comparison_code (comp, insn);
1735 if (reversed_code != UNKNOWN)
1737 validate_change (insn, &XEXP (x, 0),
1738 gen_rtx_fmt_ee (reversed_code,
1739 GET_MODE (comp), XEXP (comp, 0),
1740 XEXP (comp, 1)),
1742 return 1;
1745 tem = XEXP (x, 1);
1746 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
1747 validate_change (insn, &XEXP (x, 2), tem, 1);
1748 return 1;
1750 else
1751 return 0;
1754 /* Invert the condition of the jump JUMP, and make it jump to label
1755 NLABEL instead of where it jumps now. Accrue changes into the
1756 change group. Return false if we didn't see how to perform the
1757 inversion and redirection. */
1760 invert_jump_1 (rtx jump, rtx nlabel)
1762 rtx x = pc_set (jump);
1763 int ochanges;
1764 int ok;
1766 ochanges = num_validated_changes ();
1767 gcc_assert (x);
1768 ok = invert_exp_1 (SET_SRC (x), jump);
1769 gcc_assert (ok);
1771 if (num_validated_changes () == ochanges)
1772 return 0;
1774 /* redirect_jump_1 will fail of nlabel == olabel, and the current use is
1775 in Pmode, so checking this is not merely an optimization. */
1776 return nlabel == JUMP_LABEL (jump) || redirect_jump_1 (jump, nlabel);
1779 /* Invert the condition of the jump JUMP, and make it jump to label
1780 NLABEL instead of where it jumps now. Return true if successful. */
1783 invert_jump (rtx jump, rtx nlabel, int delete_unused)
1785 rtx olabel = JUMP_LABEL (jump);
1787 if (invert_jump_1 (jump, nlabel) && apply_change_group ())
1789 redirect_jump_2 (jump, olabel, nlabel, delete_unused, 1);
1790 return 1;
1792 cancel_changes (0);
1793 return 0;
1797 /* Like rtx_equal_p except that it considers two REGs as equal
1798 if they renumber to the same value and considers two commutative
1799 operations to be the same if the order of the operands has been
1800 reversed. */
1803 rtx_renumbered_equal_p (rtx x, rtx y)
1805 int i;
1806 enum rtx_code code = GET_CODE (x);
1807 const char *fmt;
1809 if (x == y)
1810 return 1;
1812 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
1813 && (REG_P (y) || (GET_CODE (y) == SUBREG
1814 && REG_P (SUBREG_REG (y)))))
1816 int reg_x = -1, reg_y = -1;
1817 int byte_x = 0, byte_y = 0;
1819 if (GET_MODE (x) != GET_MODE (y))
1820 return 0;
1822 /* If we haven't done any renumbering, don't
1823 make any assumptions. */
1824 if (reg_renumber == 0)
1825 return rtx_equal_p (x, y);
1827 if (code == SUBREG)
1829 reg_x = REGNO (SUBREG_REG (x));
1830 byte_x = SUBREG_BYTE (x);
1832 if (reg_renumber[reg_x] >= 0)
1834 reg_x = subreg_regno_offset (reg_renumber[reg_x],
1835 GET_MODE (SUBREG_REG (x)),
1836 byte_x,
1837 GET_MODE (x));
1838 byte_x = 0;
1841 else
1843 reg_x = REGNO (x);
1844 if (reg_renumber[reg_x] >= 0)
1845 reg_x = reg_renumber[reg_x];
1848 if (GET_CODE (y) == SUBREG)
1850 reg_y = REGNO (SUBREG_REG (y));
1851 byte_y = SUBREG_BYTE (y);
1853 if (reg_renumber[reg_y] >= 0)
1855 reg_y = subreg_regno_offset (reg_renumber[reg_y],
1856 GET_MODE (SUBREG_REG (y)),
1857 byte_y,
1858 GET_MODE (y));
1859 byte_y = 0;
1862 else
1864 reg_y = REGNO (y);
1865 if (reg_renumber[reg_y] >= 0)
1866 reg_y = reg_renumber[reg_y];
1869 return reg_x >= 0 && reg_x == reg_y && byte_x == byte_y;
1872 /* Now we have disposed of all the cases
1873 in which different rtx codes can match. */
1874 if (code != GET_CODE (y))
1875 return 0;
1877 switch (code)
1879 case PC:
1880 case CC0:
1881 case ADDR_VEC:
1882 case ADDR_DIFF_VEC:
1883 case CONST_INT:
1884 case CONST_DOUBLE:
1885 return 0;
1887 case LABEL_REF:
1888 /* We can't assume nonlocal labels have their following insns yet. */
1889 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
1890 return XEXP (x, 0) == XEXP (y, 0);
1892 /* Two label-refs are equivalent if they point at labels
1893 in the same position in the instruction stream. */
1894 return (next_real_insn (XEXP (x, 0))
1895 == next_real_insn (XEXP (y, 0)));
1897 case SYMBOL_REF:
1898 return XSTR (x, 0) == XSTR (y, 0);
1900 case CODE_LABEL:
1901 /* If we didn't match EQ equality above, they aren't the same. */
1902 return 0;
1904 default:
1905 break;
1908 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
1910 if (GET_MODE (x) != GET_MODE (y))
1911 return 0;
1913 /* For commutative operations, the RTX match if the operand match in any
1914 order. Also handle the simple binary and unary cases without a loop. */
1915 if (targetm.commutative_p (x, UNKNOWN))
1916 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1917 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
1918 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
1919 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
1920 else if (NON_COMMUTATIVE_P (x))
1921 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1922 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
1923 else if (UNARY_P (x))
1924 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
1926 /* Compare the elements. If any pair of corresponding elements
1927 fail to match, return 0 for the whole things. */
1929 fmt = GET_RTX_FORMAT (code);
1930 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1932 int j;
1933 switch (fmt[i])
1935 case 'w':
1936 if (XWINT (x, i) != XWINT (y, i))
1937 return 0;
1938 break;
1940 case 'i':
1941 if (XINT (x, i) != XINT (y, i))
1942 return 0;
1943 break;
1945 case 't':
1946 if (XTREE (x, i) != XTREE (y, i))
1947 return 0;
1948 break;
1950 case 's':
1951 if (strcmp (XSTR (x, i), XSTR (y, i)))
1952 return 0;
1953 break;
1955 case 'e':
1956 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
1957 return 0;
1958 break;
1960 case 'u':
1961 if (XEXP (x, i) != XEXP (y, i))
1962 return 0;
1963 /* Fall through. */
1964 case '0':
1965 break;
1967 case 'E':
1968 if (XVECLEN (x, i) != XVECLEN (y, i))
1969 return 0;
1970 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1971 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1972 return 0;
1973 break;
1975 default:
1976 gcc_unreachable ();
1979 return 1;
1982 /* If X is a hard register or equivalent to one or a subregister of one,
1983 return the hard register number. If X is a pseudo register that was not
1984 assigned a hard register, return the pseudo register number. Otherwise,
1985 return -1. Any rtx is valid for X. */
1988 true_regnum (rtx x)
1990 if (REG_P (x))
1992 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
1993 return reg_renumber[REGNO (x)];
1994 return REGNO (x);
1996 if (GET_CODE (x) == SUBREG)
1998 int base = true_regnum (SUBREG_REG (x));
1999 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
2000 return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
2001 GET_MODE (SUBREG_REG (x)),
2002 SUBREG_BYTE (x), GET_MODE (x));
2004 return -1;
2007 /* Return regno of the register REG and handle subregs too. */
2008 unsigned int
2009 reg_or_subregno (rtx reg)
2011 if (GET_CODE (reg) == SUBREG)
2012 reg = SUBREG_REG (reg);
2013 gcc_assert (REG_P (reg));
2014 return REGNO (reg);