OSDN Git Service

6ead32207739d6e8c6ccfc11e0e6cc4889deb90f
[pf3gnuchains/gcc-fork.git] / gcc / jump.c
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.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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.
25
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.
33
34    The subroutines redirect_jump and invert_jump are used
35    from other passes as well.  */
36
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"
58
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.  */
65
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);
75 \f
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)
81 {
82   rtx insn;
83
84   timevar_push (TV_REBUILD_JUMP);
85   init_label_info (f);
86   mark_all_labels (f);
87
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.  */
91
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);
96 }
97 \f
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.
102
103    This simple pass moves barriers and removes duplicates so that the
104    old code is happy.
105  */
106 void
107 cleanup_barriers (void)
108 {
109   rtx insn, next, prev;
110   for (insn = get_insns (); insn; insn = next)
111     {
112       next = NEXT_INSN (insn);
113       if (GET_CODE (insn) == BARRIER)
114         {
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);
120         }
121     }
122 }
123
124 void
125 purge_line_number_notes (rtx f)
126 {
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.  */
133
134   for (insn = f; insn; insn = NEXT_INSN (insn))
135     if (GET_CODE (insn) == NOTE)
136       {
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)
142           {
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))
147               {
148                 delete_related_insns (insn);
149                 continue;
150               }
151
152             last_note = insn;
153           }
154       }
155 }
156 \f
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)
162 {
163   rtx insn;
164
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)
171       {
172         rtx note, next;
173
174         for (note = REG_NOTES (insn); note; note = next)
175           {
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);
180           }
181       }
182 }
183
184 /* Mark the label each jump jumps to.
185    Combine consecutive labels, and count uses of labels.  */
186
187 static void
188 mark_all_labels (rtx f)
189 {
190   rtx insn;
191
192   for (insn = f; insn; insn = NEXT_INSN (insn))
193     if (INSN_P (insn))
194       {
195         if (GET_CODE (insn) == CALL_INSN
196             && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
197           {
198             mark_all_labels (XEXP (PATTERN (insn), 0));
199             mark_all_labels (XEXP (PATTERN (insn), 1));
200             mark_all_labels (XEXP (PATTERN (insn), 2));
201
202             /* Canonicalize the tail recursion label attached to the
203                CALL_PLACEHOLDER insn.  */
204             if (XEXP (PATTERN (insn), 3))
205               {
206                 rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
207                                                    XEXP (PATTERN (insn), 3));
208                 mark_jump_label (label_ref, insn, 0);
209                 XEXP (PATTERN (insn), 3) = XEXP (label_ref, 0);
210               }
211
212             continue;
213           }
214
215         mark_jump_label (PATTERN (insn), insn, 0);
216         if (! INSN_DELETED_P (insn) && GET_CODE (insn) == JUMP_INSN)
217           {
218             /* When we know the LABEL_REF contained in a REG used in
219                an indirect jump, we'll have a REG_LABEL note so that
220                flow can tell where it's going.  */
221             if (JUMP_LABEL (insn) == 0)
222               {
223                 rtx label_note = find_reg_note (insn, REG_LABEL, NULL_RTX);
224                 if (label_note)
225                   {
226                     /* But a LABEL_REF around the REG_LABEL note, so
227                        that we can canonicalize it.  */
228                     rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
229                                                        XEXP (label_note, 0));
230
231                     mark_jump_label (label_ref, insn, 0);
232                     XEXP (label_note, 0) = XEXP (label_ref, 0);
233                     JUMP_LABEL (insn) = XEXP (label_note, 0);
234                   }
235               }
236           }
237       }
238 }
239 \f
240 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
241    notes between START and END out before START.  START and END may be such
242    notes.  Returns the values of the new starting and ending insns, which
243    may be different if the original ones were such notes.
244    Return true if there were only such notes and no real instructions.  */
245
246 bool
247 squeeze_notes (rtx* startp, rtx* endp)
248 {
249   rtx start = *startp;
250   rtx end = *endp;
251
252   rtx insn;
253   rtx next;
254   rtx last = NULL;
255   rtx past_end = NEXT_INSN (end);
256
257   for (insn = start; insn != past_end; insn = next)
258     {
259       next = NEXT_INSN (insn);
260       if (GET_CODE (insn) == NOTE
261           && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
262               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
263               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
264               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
265               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
266               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
267         {
268           if (insn == start)
269             start = next;
270           else
271             {
272               rtx prev = PREV_INSN (insn);
273               PREV_INSN (insn) = PREV_INSN (start);
274               NEXT_INSN (insn) = start;
275               NEXT_INSN (PREV_INSN (insn)) = insn;
276               PREV_INSN (NEXT_INSN (insn)) = insn;
277               NEXT_INSN (prev) = next;
278               PREV_INSN (next) = prev;
279             }
280         }
281       else
282         last = insn;
283     }
284
285   /* There were no real instructions.  */
286   if (start == past_end)
287     return true;
288
289   end = last;
290
291   *startp = start;
292   *endp = end;
293   return false;
294 }
295 \f
296 /* Return the label before INSN, or put a new label there.  */
297
298 rtx
299 get_label_before (rtx insn)
300 {
301   rtx label;
302
303   /* Find an existing label at this point
304      or make a new one if there is none.  */
305   label = prev_nonnote_insn (insn);
306
307   if (label == 0 || GET_CODE (label) != CODE_LABEL)
308     {
309       rtx prev = PREV_INSN (insn);
310
311       label = gen_label_rtx ();
312       emit_label_after (label, prev);
313       LABEL_NUSES (label) = 0;
314     }
315   return label;
316 }
317
318 /* Return the label after INSN, or put a new label there.  */
319
320 rtx
321 get_label_after (rtx insn)
322 {
323   rtx label;
324
325   /* Find an existing label at this point
326      or make a new one if there is none.  */
327   label = next_nonnote_insn (insn);
328
329   if (label == 0 || GET_CODE (label) != CODE_LABEL)
330     {
331       label = gen_label_rtx ();
332       emit_label_after (label, insn);
333       LABEL_NUSES (label) = 0;
334     }
335   return label;
336 }
337 \f
338 /* Given a comparison (CODE ARG0 ARG1), inside an insn, INSN, return a code
339    of reversed comparison if it is possible to do so.  Otherwise return UNKNOWN.
340    UNKNOWN may be returned in case we are having CC_MODE compare and we don't
341    know whether it's source is floating point or integer comparison.  Machine
342    description should define REVERSIBLE_CC_MODE and REVERSE_CONDITION macros
343    to help this function avoid overhead in these cases.  */
344 enum rtx_code
345 reversed_comparison_code_parts (enum rtx_code code, rtx arg0, rtx arg1, rtx insn)
346 {
347   enum machine_mode mode;
348
349   /* If this is not actually a comparison, we can't reverse it.  */
350   if (GET_RTX_CLASS (code) != RTX_COMPARE
351       && GET_RTX_CLASS (code) != RTX_COMM_COMPARE)
352     return UNKNOWN;
353
354   mode = GET_MODE (arg0);
355   if (mode == VOIDmode)
356     mode = GET_MODE (arg1);
357
358   /* First see if machine description supplies us way to reverse the
359      comparison.  Give it priority over everything else to allow
360      machine description to do tricks.  */
361   if (GET_MODE_CLASS (mode) == MODE_CC
362       && REVERSIBLE_CC_MODE (mode))
363     {
364 #ifdef REVERSE_CONDITION
365       return REVERSE_CONDITION (code, mode);
366 #endif
367       return reverse_condition (code);
368     }
369
370   /* Try a few special cases based on the comparison code.  */
371   switch (code)
372     {
373     case GEU:
374     case GTU:
375     case LEU:
376     case LTU:
377     case NE:
378     case EQ:
379       /* It is always safe to reverse EQ and NE, even for the floating
380          point.  Similarly the unsigned comparisons are never used for
381          floating point so we can reverse them in the default way.  */
382       return reverse_condition (code);
383     case ORDERED:
384     case UNORDERED:
385     case LTGT:
386     case UNEQ:
387       /* In case we already see unordered comparison, we can be sure to
388          be dealing with floating point so we don't need any more tests.  */
389       return reverse_condition_maybe_unordered (code);
390     case UNLT:
391     case UNLE:
392     case UNGT:
393     case UNGE:
394       /* We don't have safe way to reverse these yet.  */
395       return UNKNOWN;
396     default:
397       break;
398     }
399
400   if (GET_MODE_CLASS (mode) == MODE_CC || CC0_P (arg0))
401     {
402       rtx prev;
403       /* Try to search for the comparison to determine the real mode.
404          This code is expensive, but with sane machine description it
405          will be never used, since REVERSIBLE_CC_MODE will return true
406          in all cases.  */
407       if (! insn)
408         return UNKNOWN;
409
410       for (prev = prev_nonnote_insn (insn);
411            prev != 0 && GET_CODE (prev) != CODE_LABEL;
412            prev = prev_nonnote_insn (prev))
413         {
414           rtx set = set_of (arg0, prev);
415           if (set && GET_CODE (set) == SET
416               && rtx_equal_p (SET_DEST (set), arg0))
417             {
418               rtx src = SET_SRC (set);
419
420               if (GET_CODE (src) == COMPARE)
421                 {
422                   rtx comparison = src;
423                   arg0 = XEXP (src, 0);
424                   mode = GET_MODE (arg0);
425                   if (mode == VOIDmode)
426                     mode = GET_MODE (XEXP (comparison, 1));
427                   break;
428                 }
429               /* We can get past reg-reg moves.  This may be useful for model
430                  of i387 comparisons that first move flag registers around.  */
431               if (REG_P (src))
432                 {
433                   arg0 = src;
434                   continue;
435                 }
436             }
437           /* If register is clobbered in some ununderstandable way,
438              give up.  */
439           if (set)
440             return UNKNOWN;
441         }
442     }
443
444   /* Test for an integer condition, or a floating-point comparison
445      in which NaNs can be ignored.  */
446   if (GET_CODE (arg0) == CONST_INT
447       || (GET_MODE (arg0) != VOIDmode
448           && GET_MODE_CLASS (mode) != MODE_CC
449           && !HONOR_NANS (mode)))
450     return reverse_condition (code);
451
452   return UNKNOWN;
453 }
454
455 /* A wrapper around the previous function to take COMPARISON as rtx
456    expression.  This simplifies many callers.  */
457 enum rtx_code
458 reversed_comparison_code (rtx comparison, rtx insn)
459 {
460   if (!COMPARISON_P (comparison))
461     return UNKNOWN;
462   return reversed_comparison_code_parts (GET_CODE (comparison),
463                                          XEXP (comparison, 0),
464                                          XEXP (comparison, 1), insn);
465 }
466 \f
467 /* Given an rtx-code for a comparison, return the code for the negated
468    comparison.  If no such code exists, return UNKNOWN.
469
470    WATCH OUT!  reverse_condition is not safe to use on a jump that might
471    be acting on the results of an IEEE floating point comparison, because
472    of the special treatment of non-signaling nans in comparisons.
473    Use reversed_comparison_code instead.  */
474
475 enum rtx_code
476 reverse_condition (enum rtx_code code)
477 {
478   switch (code)
479     {
480     case EQ:
481       return NE;
482     case NE:
483       return EQ;
484     case GT:
485       return LE;
486     case GE:
487       return LT;
488     case LT:
489       return GE;
490     case LE:
491       return GT;
492     case GTU:
493       return LEU;
494     case GEU:
495       return LTU;
496     case LTU:
497       return GEU;
498     case LEU:
499       return GTU;
500     case UNORDERED:
501       return ORDERED;
502     case ORDERED:
503       return UNORDERED;
504
505     case UNLT:
506     case UNLE:
507     case UNGT:
508     case UNGE:
509     case UNEQ:
510     case LTGT:
511       return UNKNOWN;
512
513     default:
514       abort ();
515     }
516 }
517
518 /* Similar, but we're allowed to generate unordered comparisons, which
519    makes it safe for IEEE floating-point.  Of course, we have to recognize
520    that the target will support them too...  */
521
522 enum rtx_code
523 reverse_condition_maybe_unordered (enum rtx_code code)
524 {
525   switch (code)
526     {
527     case EQ:
528       return NE;
529     case NE:
530       return EQ;
531     case GT:
532       return UNLE;
533     case GE:
534       return UNLT;
535     case LT:
536       return UNGE;
537     case LE:
538       return UNGT;
539     case LTGT:
540       return UNEQ;
541     case UNORDERED:
542       return ORDERED;
543     case ORDERED:
544       return UNORDERED;
545     case UNLT:
546       return GE;
547     case UNLE:
548       return GT;
549     case UNGT:
550       return LE;
551     case UNGE:
552       return LT;
553     case UNEQ:
554       return LTGT;
555
556     default:
557       abort ();
558     }
559 }
560
561 /* Similar, but return the code when two operands of a comparison are swapped.
562    This IS safe for IEEE floating-point.  */
563
564 enum rtx_code
565 swap_condition (enum rtx_code code)
566 {
567   switch (code)
568     {
569     case EQ:
570     case NE:
571     case UNORDERED:
572     case ORDERED:
573     case UNEQ:
574     case LTGT:
575       return code;
576
577     case GT:
578       return LT;
579     case GE:
580       return LE;
581     case LT:
582       return GT;
583     case LE:
584       return GE;
585     case GTU:
586       return LTU;
587     case GEU:
588       return LEU;
589     case LTU:
590       return GTU;
591     case LEU:
592       return GEU;
593     case UNLT:
594       return UNGT;
595     case UNLE:
596       return UNGE;
597     case UNGT:
598       return UNLT;
599     case UNGE:
600       return UNLE;
601
602     default:
603       abort ();
604     }
605 }
606
607 /* Given a comparison CODE, return the corresponding unsigned comparison.
608    If CODE is an equality comparison or already an unsigned comparison,
609    CODE is returned.  */
610
611 enum rtx_code
612 unsigned_condition (enum rtx_code code)
613 {
614   switch (code)
615     {
616     case EQ:
617     case NE:
618     case GTU:
619     case GEU:
620     case LTU:
621     case LEU:
622       return code;
623
624     case GT:
625       return GTU;
626     case GE:
627       return GEU;
628     case LT:
629       return LTU;
630     case LE:
631       return LEU;
632
633     default:
634       abort ();
635     }
636 }
637
638 /* Similarly, return the signed version of a comparison.  */
639
640 enum rtx_code
641 signed_condition (enum rtx_code code)
642 {
643   switch (code)
644     {
645     case EQ:
646     case NE:
647     case GT:
648     case GE:
649     case LT:
650     case LE:
651       return code;
652
653     case GTU:
654       return GT;
655     case GEU:
656       return GE;
657     case LTU:
658       return LT;
659     case LEU:
660       return LE;
661
662     default:
663       abort ();
664     }
665 }
666 \f
667 /* Return nonzero if CODE1 is more strict than CODE2, i.e., if the
668    truth of CODE1 implies the truth of CODE2.  */
669
670 int
671 comparison_dominates_p (enum rtx_code code1, enum rtx_code code2)
672 {
673   /* UNKNOWN comparison codes can happen as a result of trying to revert
674      comparison codes.
675      They can't match anything, so we have to reject them here.  */
676   if (code1 == UNKNOWN || code2 == UNKNOWN)
677     return 0;
678
679   if (code1 == code2)
680     return 1;
681
682   switch (code1)
683     {
684     case UNEQ:
685       if (code2 == UNLE || code2 == UNGE)
686         return 1;
687       break;
688
689     case EQ:
690       if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU
691           || code2 == ORDERED)
692         return 1;
693       break;
694
695     case UNLT:
696       if (code2 == UNLE || code2 == NE)
697         return 1;
698       break;
699
700     case LT:
701       if (code2 == LE || code2 == NE || code2 == ORDERED || code2 == LTGT)
702         return 1;
703       break;
704
705     case UNGT:
706       if (code2 == UNGE || code2 == NE)
707         return 1;
708       break;
709
710     case GT:
711       if (code2 == GE || code2 == NE || code2 == ORDERED || code2 == LTGT)
712         return 1;
713       break;
714
715     case GE:
716     case LE:
717       if (code2 == ORDERED)
718         return 1;
719       break;
720
721     case LTGT:
722       if (code2 == NE || code2 == ORDERED)
723         return 1;
724       break;
725
726     case LTU:
727       if (code2 == LEU || code2 == NE)
728         return 1;
729       break;
730
731     case GTU:
732       if (code2 == GEU || code2 == NE)
733         return 1;
734       break;
735
736     case UNORDERED:
737       if (code2 == NE || code2 == UNEQ || code2 == UNLE || code2 == UNLT
738           || code2 == UNGE || code2 == UNGT)
739         return 1;
740       break;
741
742     default:
743       break;
744     }
745
746   return 0;
747 }
748 \f
749 /* Return 1 if INSN is an unconditional jump and nothing else.  */
750
751 int
752 simplejump_p (rtx insn)
753 {
754   return (GET_CODE (insn) == JUMP_INSN
755           && GET_CODE (PATTERN (insn)) == SET
756           && GET_CODE (SET_DEST (PATTERN (insn))) == PC
757           && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
758 }
759
760 /* Return nonzero if INSN is a (possibly) conditional jump
761    and nothing more.
762
763    Use of this function is deprecated, since we need to support combined
764    branch and compare insns.  Use any_condjump_p instead whenever possible.  */
765
766 int
767 condjump_p (rtx insn)
768 {
769   rtx x = PATTERN (insn);
770
771   if (GET_CODE (x) != SET
772       || GET_CODE (SET_DEST (x)) != PC)
773     return 0;
774
775   x = SET_SRC (x);
776   if (GET_CODE (x) == LABEL_REF)
777     return 1;
778   else
779     return (GET_CODE (x) == IF_THEN_ELSE
780             && ((GET_CODE (XEXP (x, 2)) == PC
781                  && (GET_CODE (XEXP (x, 1)) == LABEL_REF
782                      || GET_CODE (XEXP (x, 1)) == RETURN))
783                 || (GET_CODE (XEXP (x, 1)) == PC
784                     && (GET_CODE (XEXP (x, 2)) == LABEL_REF
785                         || GET_CODE (XEXP (x, 2)) == RETURN))));
786
787   return 0;
788 }
789
790 /* Return nonzero if INSN is a (possibly) conditional jump inside a
791    PARALLEL.
792
793    Use this function is deprecated, since we need to support combined
794    branch and compare insns.  Use any_condjump_p instead whenever possible.  */
795
796 int
797 condjump_in_parallel_p (rtx insn)
798 {
799   rtx x = PATTERN (insn);
800
801   if (GET_CODE (x) != PARALLEL)
802     return 0;
803   else
804     x = XVECEXP (x, 0, 0);
805
806   if (GET_CODE (x) != SET)
807     return 0;
808   if (GET_CODE (SET_DEST (x)) != PC)
809     return 0;
810   if (GET_CODE (SET_SRC (x)) == LABEL_REF)
811     return 1;
812   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
813     return 0;
814   if (XEXP (SET_SRC (x), 2) == pc_rtx
815       && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
816           || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
817     return 1;
818   if (XEXP (SET_SRC (x), 1) == pc_rtx
819       && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
820           || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
821     return 1;
822   return 0;
823 }
824
825 /* Return set of PC, otherwise NULL.  */
826
827 rtx
828 pc_set (rtx insn)
829 {
830   rtx pat;
831   if (GET_CODE (insn) != JUMP_INSN)
832     return NULL_RTX;
833   pat = PATTERN (insn);
834
835   /* The set is allowed to appear either as the insn pattern or
836      the first set in a PARALLEL.  */
837   if (GET_CODE (pat) == PARALLEL)
838     pat = XVECEXP (pat, 0, 0);
839   if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == PC)
840     return pat;
841
842   return NULL_RTX;
843 }
844
845 /* Return true when insn is an unconditional direct jump,
846    possibly bundled inside a PARALLEL.  */
847
848 int
849 any_uncondjump_p (rtx insn)
850 {
851   rtx x = pc_set (insn);
852   if (!x)
853     return 0;
854   if (GET_CODE (SET_SRC (x)) != LABEL_REF)
855     return 0;
856   if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
857     return 0;
858   return 1;
859 }
860
861 /* Return true when insn is a conditional jump.  This function works for
862    instructions containing PC sets in PARALLELs.  The instruction may have
863    various other effects so before removing the jump you must verify
864    onlyjump_p.
865
866    Note that unlike condjump_p it returns false for unconditional jumps.  */
867
868 int
869 any_condjump_p (rtx insn)
870 {
871   rtx x = pc_set (insn);
872   enum rtx_code a, b;
873
874   if (!x)
875     return 0;
876   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
877     return 0;
878
879   a = GET_CODE (XEXP (SET_SRC (x), 1));
880   b = GET_CODE (XEXP (SET_SRC (x), 2));
881
882   return ((b == PC && (a == LABEL_REF || a == RETURN))
883           || (a == PC && (b == LABEL_REF || b == RETURN)));
884 }
885
886 /* Return the label of a conditional jump.  */
887
888 rtx
889 condjump_label (rtx insn)
890 {
891   rtx x = pc_set (insn);
892
893   if (!x)
894     return NULL_RTX;
895   x = SET_SRC (x);
896   if (GET_CODE (x) == LABEL_REF)
897     return x;
898   if (GET_CODE (x) != IF_THEN_ELSE)
899     return NULL_RTX;
900   if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
901     return XEXP (x, 1);
902   if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
903     return XEXP (x, 2);
904   return NULL_RTX;
905 }
906
907 /* Return true if INSN is a (possibly conditional) return insn.  */
908
909 static int
910 returnjump_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
911 {
912   rtx x = *loc;
913
914   return x && (GET_CODE (x) == RETURN
915                || (GET_CODE (x) == SET && SET_IS_RETURN_P (x)));
916 }
917
918 int
919 returnjump_p (rtx insn)
920 {
921   if (GET_CODE (insn) != JUMP_INSN)
922     return 0;
923   return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
924 }
925
926 /* Return true if INSN is a jump that only transfers control and
927    nothing more.  */
928
929 int
930 onlyjump_p (rtx insn)
931 {
932   rtx set;
933
934   if (GET_CODE (insn) != JUMP_INSN)
935     return 0;
936
937   set = single_set (insn);
938   if (set == NULL)
939     return 0;
940   if (GET_CODE (SET_DEST (set)) != PC)
941     return 0;
942   if (side_effects_p (SET_SRC (set)))
943     return 0;
944
945   return 1;
946 }
947
948 #ifdef HAVE_cc0
949
950 /* Return nonzero if X is an RTX that only sets the condition codes
951    and has no side effects.  */
952
953 int
954 only_sets_cc0_p (rtx x)
955 {
956   if (! x)
957     return 0;
958
959   if (INSN_P (x))
960     x = PATTERN (x);
961
962   return sets_cc0_p (x) == 1 && ! side_effects_p (x);
963 }
964
965 /* Return 1 if X is an RTX that does nothing but set the condition codes
966    and CLOBBER or USE registers.
967    Return -1 if X does explicitly set the condition codes,
968    but also does other things.  */
969
970 int
971 sets_cc0_p (rtx x)
972 {
973   if (! x)
974     return 0;
975
976   if (INSN_P (x))
977     x = PATTERN (x);
978
979   if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
980     return 1;
981   if (GET_CODE (x) == PARALLEL)
982     {
983       int i;
984       int sets_cc0 = 0;
985       int other_things = 0;
986       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
987         {
988           if (GET_CODE (XVECEXP (x, 0, i)) == SET
989               && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
990             sets_cc0 = 1;
991           else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
992             other_things = 1;
993         }
994       return ! sets_cc0 ? 0 : other_things ? -1 : 1;
995     }
996   return 0;
997 }
998 #endif
999 \f
1000 /* Follow any unconditional jump at LABEL;
1001    return the ultimate label reached by any such chain of jumps.
1002    Return null if the chain ultimately leads to a return instruction.
1003    If LABEL is not followed by a jump, return LABEL.
1004    If the chain loops or we can't find end, return LABEL,
1005    since that tells caller to avoid changing the insn.
1006
1007    If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
1008    a USE or CLOBBER.  */
1009
1010 rtx
1011 follow_jumps (rtx label)
1012 {
1013   rtx insn;
1014   rtx next;
1015   rtx value = label;
1016   int depth;
1017
1018   for (depth = 0;
1019        (depth < 10
1020         && (insn = next_active_insn (value)) != 0
1021         && GET_CODE (insn) == JUMP_INSN
1022         && ((JUMP_LABEL (insn) != 0 && any_uncondjump_p (insn)
1023              && onlyjump_p (insn))
1024             || GET_CODE (PATTERN (insn)) == RETURN)
1025         && (next = NEXT_INSN (insn))
1026         && GET_CODE (next) == BARRIER);
1027        depth++)
1028     {
1029       /* Don't chain through the insn that jumps into a loop
1030          from outside the loop,
1031          since that would create multiple loop entry jumps
1032          and prevent loop optimization.  */
1033       rtx tem;
1034       if (!reload_completed)
1035         for (tem = value; tem != insn; tem = NEXT_INSN (tem))
1036           if (GET_CODE (tem) == NOTE
1037               && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
1038                   /* ??? Optional.  Disables some optimizations, but makes
1039                      gcov output more accurate with -O.  */
1040                   || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
1041             return value;
1042
1043       /* If we have found a cycle, make the insn jump to itself.  */
1044       if (JUMP_LABEL (insn) == label)
1045         return label;
1046
1047       tem = next_active_insn (JUMP_LABEL (insn));
1048       if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
1049                   || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
1050         break;
1051
1052       value = JUMP_LABEL (insn);
1053     }
1054   if (depth == 10)
1055     return label;
1056   return value;
1057 }
1058
1059 \f
1060 /* Find all CODE_LABELs referred to in X, and increment their use counts.
1061    If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
1062    in INSN, then store one of them in JUMP_LABEL (INSN).
1063    If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
1064    referenced in INSN, add a REG_LABEL note containing that label to INSN.
1065    Also, when there are consecutive labels, canonicalize on the last of them.
1066
1067    Note that two labels separated by a loop-beginning note
1068    must be kept distinct if we have not yet done loop-optimization,
1069    because the gap between them is where loop-optimize
1070    will want to move invariant code to.  CROSS_JUMP tells us
1071    that loop-optimization is done with.  */
1072
1073 void
1074 mark_jump_label (rtx x, rtx insn, int in_mem)
1075 {
1076   RTX_CODE code = GET_CODE (x);
1077   int i;
1078   const char *fmt;
1079
1080   switch (code)
1081     {
1082     case PC:
1083     case CC0:
1084     case REG:
1085     case CONST_INT:
1086     case CONST_DOUBLE:
1087     case CLOBBER:
1088     case CALL:
1089       return;
1090
1091     case MEM:
1092       in_mem = 1;
1093       break;
1094
1095     case SYMBOL_REF:
1096       if (!in_mem)
1097         return;
1098
1099       /* If this is a constant-pool reference, see if it is a label.  */
1100       if (CONSTANT_POOL_ADDRESS_P (x))
1101         mark_jump_label (get_pool_constant (x), insn, in_mem);
1102       break;
1103
1104     case LABEL_REF:
1105       {
1106         rtx label = XEXP (x, 0);
1107
1108         /* Ignore remaining references to unreachable labels that
1109            have been deleted.  */
1110         if (GET_CODE (label) == NOTE
1111             && NOTE_LINE_NUMBER (label) == NOTE_INSN_DELETED_LABEL)
1112           break;
1113
1114         if (GET_CODE (label) != CODE_LABEL)
1115           abort ();
1116
1117         /* Ignore references to labels of containing functions.  */
1118         if (LABEL_REF_NONLOCAL_P (x))
1119           break;
1120
1121         XEXP (x, 0) = label;
1122         if (! insn || ! INSN_DELETED_P (insn))
1123           ++LABEL_NUSES (label);
1124
1125         if (insn)
1126           {
1127             if (GET_CODE (insn) == JUMP_INSN)
1128               JUMP_LABEL (insn) = label;
1129             else
1130               {
1131                 /* Add a REG_LABEL note for LABEL unless there already
1132                    is one.  All uses of a label, except for labels
1133                    that are the targets of jumps, must have a
1134                    REG_LABEL note.  */
1135                 if (! find_reg_note (insn, REG_LABEL, label))
1136                   REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
1137                                                         REG_NOTES (insn));
1138               }
1139           }
1140         return;
1141       }
1142
1143   /* Do walk the labels in a vector, but not the first operand of an
1144      ADDR_DIFF_VEC.  Don't set the JUMP_LABEL of a vector.  */
1145     case ADDR_VEC:
1146     case ADDR_DIFF_VEC:
1147       if (! INSN_DELETED_P (insn))
1148         {
1149           int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
1150
1151           for (i = 0; i < XVECLEN (x, eltnum); i++)
1152             mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, in_mem);
1153         }
1154       return;
1155
1156     default:
1157       break;
1158     }
1159
1160   fmt = GET_RTX_FORMAT (code);
1161   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1162     {
1163       if (fmt[i] == 'e')
1164         mark_jump_label (XEXP (x, i), insn, in_mem);
1165       else if (fmt[i] == 'E')
1166         {
1167           int j;
1168           for (j = 0; j < XVECLEN (x, i); j++)
1169             mark_jump_label (XVECEXP (x, i, j), insn, in_mem);
1170         }
1171     }
1172 }
1173
1174 /* If all INSN does is set the pc, delete it,
1175    and delete the insn that set the condition codes for it
1176    if that's what the previous thing was.  */
1177
1178 void
1179 delete_jump (rtx insn)
1180 {
1181   rtx set = single_set (insn);
1182
1183   if (set && GET_CODE (SET_DEST (set)) == PC)
1184     delete_computation (insn);
1185 }
1186
1187 /* Verify INSN is a BARRIER and delete it.  */
1188
1189 void
1190 delete_barrier (rtx insn)
1191 {
1192   if (GET_CODE (insn) != BARRIER)
1193     abort ();
1194
1195   delete_insn (insn);
1196 }
1197
1198 /* Recursively delete prior insns that compute the value (used only by INSN
1199    which the caller is deleting) stored in the register mentioned by NOTE
1200    which is a REG_DEAD note associated with INSN.  */
1201
1202 static void
1203 delete_prior_computation (rtx note, rtx insn)
1204 {
1205   rtx our_prev;
1206   rtx reg = XEXP (note, 0);
1207
1208   for (our_prev = prev_nonnote_insn (insn);
1209        our_prev && (GET_CODE (our_prev) == INSN
1210                     || GET_CODE (our_prev) == CALL_INSN);
1211        our_prev = prev_nonnote_insn (our_prev))
1212     {
1213       rtx pat = PATTERN (our_prev);
1214
1215       /* If we reach a CALL which is not calling a const function
1216          or the callee pops the arguments, then give up.  */
1217       if (GET_CODE (our_prev) == CALL_INSN
1218           && (! CONST_OR_PURE_CALL_P (our_prev)
1219               || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
1220         break;
1221
1222       /* If we reach a SEQUENCE, it is too complex to try to
1223          do anything with it, so give up.  We can be run during
1224          and after reorg, so SEQUENCE rtl can legitimately show
1225          up here.  */
1226       if (GET_CODE (pat) == SEQUENCE)
1227         break;
1228
1229       if (GET_CODE (pat) == USE
1230           && GET_CODE (XEXP (pat, 0)) == INSN)
1231         /* reorg creates USEs that look like this.  We leave them
1232            alone because reorg needs them for its own purposes.  */
1233         break;
1234
1235       if (reg_set_p (reg, pat))
1236         {
1237           if (side_effects_p (pat) && GET_CODE (our_prev) != CALL_INSN)
1238             break;
1239
1240           if (GET_CODE (pat) == PARALLEL)
1241             {
1242               /* If we find a SET of something else, we can't
1243                  delete the insn.  */
1244
1245               int i;
1246
1247               for (i = 0; i < XVECLEN (pat, 0); i++)
1248                 {
1249                   rtx part = XVECEXP (pat, 0, i);
1250
1251                   if (GET_CODE (part) == SET
1252                       && SET_DEST (part) != reg)
1253                     break;
1254                 }
1255
1256               if (i == XVECLEN (pat, 0))
1257                 delete_computation (our_prev);
1258             }
1259           else if (GET_CODE (pat) == SET
1260                    && GET_CODE (SET_DEST (pat)) == REG)
1261             {
1262               int dest_regno = REGNO (SET_DEST (pat));
1263               int dest_endregno
1264                 = (dest_regno
1265                    + (dest_regno < FIRST_PSEUDO_REGISTER
1266                       ? hard_regno_nregs[dest_regno]
1267                                         [GET_MODE (SET_DEST (pat))] : 1));
1268               int regno = REGNO (reg);
1269               int endregno
1270                 = (regno
1271                    + (regno < FIRST_PSEUDO_REGISTER
1272                       ? hard_regno_nregs[regno][GET_MODE (reg)] : 1));
1273
1274               if (dest_regno >= regno
1275                   && dest_endregno <= endregno)
1276                 delete_computation (our_prev);
1277
1278               /* We may have a multi-word hard register and some, but not
1279                  all, of the words of the register are needed in subsequent
1280                  insns.  Write REG_UNUSED notes for those parts that were not
1281                  needed.  */
1282               else if (dest_regno <= regno
1283                        && dest_endregno >= endregno)
1284                 {
1285                   int i;
1286
1287                   REG_NOTES (our_prev)
1288                     = gen_rtx_EXPR_LIST (REG_UNUSED, reg,
1289                                          REG_NOTES (our_prev));
1290
1291                   for (i = dest_regno; i < dest_endregno; i++)
1292                     if (! find_regno_note (our_prev, REG_UNUSED, i))
1293                       break;
1294
1295                   if (i == dest_endregno)
1296                     delete_computation (our_prev);
1297                 }
1298             }
1299
1300           break;
1301         }
1302
1303       /* If PAT references the register that dies here, it is an
1304          additional use.  Hence any prior SET isn't dead.  However, this
1305          insn becomes the new place for the REG_DEAD note.  */
1306       if (reg_overlap_mentioned_p (reg, pat))
1307         {
1308           XEXP (note, 1) = REG_NOTES (our_prev);
1309           REG_NOTES (our_prev) = note;
1310           break;
1311         }
1312     }
1313 }
1314
1315 /* Delete INSN and recursively delete insns that compute values used only
1316    by INSN.  This uses the REG_DEAD notes computed during flow analysis.
1317    If we are running before flow.c, we need do nothing since flow.c will
1318    delete dead code.  We also can't know if the registers being used are
1319    dead or not at this point.
1320
1321    Otherwise, look at all our REG_DEAD notes.  If a previous insn does
1322    nothing other than set a register that dies in this insn, we can delete
1323    that insn as well.
1324
1325    On machines with CC0, if CC0 is used in this insn, we may be able to
1326    delete the insn that set it.  */
1327
1328 static void
1329 delete_computation (rtx insn)
1330 {
1331   rtx note, next;
1332
1333 #ifdef HAVE_cc0
1334   if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
1335     {
1336       rtx prev = prev_nonnote_insn (insn);
1337       /* We assume that at this stage
1338          CC's are always set explicitly
1339          and always immediately before the jump that
1340          will use them.  So if the previous insn
1341          exists to set the CC's, delete it
1342          (unless it performs auto-increments, etc.).  */
1343       if (prev && GET_CODE (prev) == INSN
1344           && sets_cc0_p (PATTERN (prev)))
1345         {
1346           if (sets_cc0_p (PATTERN (prev)) > 0
1347               && ! side_effects_p (PATTERN (prev)))
1348             delete_computation (prev);
1349           else
1350             /* Otherwise, show that cc0 won't be used.  */
1351             REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
1352                                                   cc0_rtx, REG_NOTES (prev));
1353         }
1354     }
1355 #endif
1356
1357   for (note = REG_NOTES (insn); note; note = next)
1358     {
1359       next = XEXP (note, 1);
1360
1361       if (REG_NOTE_KIND (note) != REG_DEAD
1362           /* Verify that the REG_NOTE is legitimate.  */
1363           || GET_CODE (XEXP (note, 0)) != REG)
1364         continue;
1365
1366       delete_prior_computation (note, insn);
1367     }
1368
1369   delete_related_insns (insn);
1370 }
1371 \f
1372 /* Delete insn INSN from the chain of insns and update label ref counts
1373    and delete insns now unreachable.
1374
1375    Returns the first insn after INSN that was not deleted.
1376
1377    Usage of this instruction is deprecated.  Use delete_insn instead and
1378    subsequent cfg_cleanup pass to delete unreachable code if needed.  */
1379
1380 rtx
1381 delete_related_insns (rtx insn)
1382 {
1383   int was_code_label = (GET_CODE (insn) == CODE_LABEL);
1384   rtx note;
1385   rtx next = NEXT_INSN (insn), prev = PREV_INSN (insn);
1386
1387   while (next && INSN_DELETED_P (next))
1388     next = NEXT_INSN (next);
1389
1390   /* This insn is already deleted => return first following nondeleted.  */
1391   if (INSN_DELETED_P (insn))
1392     return next;
1393
1394   delete_insn (insn);
1395
1396   /* If instruction is followed by a barrier,
1397      delete the barrier too.  */
1398
1399   if (next != 0 && GET_CODE (next) == BARRIER)
1400     delete_insn (next);
1401
1402   /* If deleting a jump, decrement the count of the label,
1403      and delete the label if it is now unused.  */
1404
1405   if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
1406     {
1407       rtx lab = JUMP_LABEL (insn), lab_next;
1408
1409       if (LABEL_NUSES (lab) == 0)
1410         {
1411           /* This can delete NEXT or PREV,
1412              either directly if NEXT is JUMP_LABEL (INSN),
1413              or indirectly through more levels of jumps.  */
1414           delete_related_insns (lab);
1415
1416           /* I feel a little doubtful about this loop,
1417              but I see no clean and sure alternative way
1418              to find the first insn after INSN that is not now deleted.
1419              I hope this works.  */
1420           while (next && INSN_DELETED_P (next))
1421             next = NEXT_INSN (next);
1422           return next;
1423         }
1424       else if (tablejump_p (insn, NULL, &lab_next))
1425         {
1426           /* If we're deleting the tablejump, delete the dispatch table.
1427              We may not be able to kill the label immediately preceding
1428              just yet, as it might be referenced in code leading up to
1429              the tablejump.  */
1430           delete_related_insns (lab_next);
1431         }
1432     }
1433
1434   /* Likewise if we're deleting a dispatch table.  */
1435
1436   if (GET_CODE (insn) == JUMP_INSN
1437       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
1438           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
1439     {
1440       rtx pat = PATTERN (insn);
1441       int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1442       int len = XVECLEN (pat, diff_vec_p);
1443
1444       for (i = 0; i < len; i++)
1445         if (LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
1446           delete_related_insns (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
1447       while (next && INSN_DELETED_P (next))
1448         next = NEXT_INSN (next);
1449       return next;
1450     }
1451
1452   /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note.  */
1453   if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
1454     for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1455       if (REG_NOTE_KIND (note) == REG_LABEL
1456           /* This could also be a NOTE_INSN_DELETED_LABEL note.  */
1457           && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
1458         if (LABEL_NUSES (XEXP (note, 0)) == 0)
1459           delete_related_insns (XEXP (note, 0));
1460
1461   while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
1462     prev = PREV_INSN (prev);
1463
1464   /* If INSN was a label and a dispatch table follows it,
1465      delete the dispatch table.  The tablejump must have gone already.
1466      It isn't useful to fall through into a table.  */
1467
1468   if (was_code_label
1469       && NEXT_INSN (insn) != 0
1470       && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
1471       && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
1472           || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
1473     next = delete_related_insns (NEXT_INSN (insn));
1474
1475   /* If INSN was a label, delete insns following it if now unreachable.  */
1476
1477   if (was_code_label && prev && GET_CODE (prev) == BARRIER)
1478     {
1479       enum rtx_code code;
1480       while (next)
1481         {
1482           code = GET_CODE (next);
1483           if (code == NOTE
1484               && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
1485             next = NEXT_INSN (next);
1486           /* Keep going past other deleted labels to delete what follows.  */
1487           else if (code == CODE_LABEL && INSN_DELETED_P (next))
1488             next = NEXT_INSN (next);
1489           else if (code == BARRIER || INSN_P (next))
1490             /* Note: if this deletes a jump, it can cause more
1491                deletion of unreachable code, after a different label.
1492                As long as the value from this recursive call is correct,
1493                this invocation functions correctly.  */
1494             next = delete_related_insns (next);
1495           else
1496             break;
1497         }
1498     }
1499
1500   return next;
1501 }
1502 \f
1503 /* Delete a range of insns from FROM to TO, inclusive.
1504    This is for the sake of peephole optimization, so assume
1505    that whatever these insns do will still be done by a new
1506    peephole insn that will replace them.  */
1507
1508 void
1509 delete_for_peephole (rtx from, rtx to)
1510 {
1511   rtx insn = from;
1512
1513   while (1)
1514     {
1515       rtx next = NEXT_INSN (insn);
1516       rtx prev = PREV_INSN (insn);
1517
1518       if (GET_CODE (insn) != NOTE)
1519         {
1520           INSN_DELETED_P (insn) = 1;
1521
1522           /* Patch this insn out of the chain.  */
1523           /* We don't do this all at once, because we
1524              must preserve all NOTEs.  */
1525           if (prev)
1526             NEXT_INSN (prev) = next;
1527
1528           if (next)
1529             PREV_INSN (next) = prev;
1530         }
1531
1532       if (insn == to)
1533         break;
1534       insn = next;
1535     }
1536
1537   /* Note that if TO is an unconditional jump
1538      we *do not* delete the BARRIER that follows,
1539      since the peephole that replaces this sequence
1540      is also an unconditional jump in that case.  */
1541 }
1542 \f
1543 /* Throughout LOC, redirect OLABEL to NLABEL.  Treat null OLABEL or
1544    NLABEL as a return.  Accrue modifications into the change group.  */
1545
1546 static void
1547 redirect_exp_1 (rtx *loc, rtx olabel, rtx nlabel, rtx insn)
1548 {
1549   rtx x = *loc;
1550   RTX_CODE code = GET_CODE (x);
1551   int i;
1552   const char *fmt;
1553
1554   if (code == LABEL_REF)
1555     {
1556       if (XEXP (x, 0) == olabel)
1557         {
1558           rtx n;
1559           if (nlabel)
1560             n = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1561           else
1562             n = gen_rtx_RETURN (VOIDmode);
1563
1564           validate_change (insn, loc, n, 1);
1565           return;
1566         }
1567     }
1568   else if (code == RETURN && olabel == 0)
1569     {
1570       x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1571       if (loc == &PATTERN (insn))
1572         x = gen_rtx_SET (VOIDmode, pc_rtx, x);
1573       validate_change (insn, loc, x, 1);
1574       return;
1575     }
1576
1577   if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
1578       && GET_CODE (SET_SRC (x)) == LABEL_REF
1579       && XEXP (SET_SRC (x), 0) == olabel)
1580     {
1581       validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 1);
1582       return;
1583     }
1584
1585   fmt = GET_RTX_FORMAT (code);
1586   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1587     {
1588       if (fmt[i] == 'e')
1589         redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
1590       else if (fmt[i] == 'E')
1591         {
1592           int j;
1593           for (j = 0; j < XVECLEN (x, i); j++)
1594             redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
1595         }
1596     }
1597 }
1598
1599 /* Similar, but apply the change group and report success or failure.  */
1600
1601 static int
1602 redirect_exp (rtx olabel, rtx nlabel, rtx insn)
1603 {
1604   rtx *loc;
1605
1606   if (GET_CODE (PATTERN (insn)) == PARALLEL)
1607     loc = &XVECEXP (PATTERN (insn), 0, 0);
1608   else
1609     loc = &PATTERN (insn);
1610
1611   redirect_exp_1 (loc, olabel, nlabel, insn);
1612   if (num_validated_changes () == 0)
1613     return 0;
1614
1615   return apply_change_group ();
1616 }
1617
1618 /* Make JUMP go to NLABEL instead of where it jumps now.  Accrue
1619    the modifications into the change group.  Return false if we did
1620    not see how to do that.  */
1621
1622 int
1623 redirect_jump_1 (rtx jump, rtx nlabel)
1624 {
1625   int ochanges = num_validated_changes ();
1626   rtx *loc;
1627
1628   if (GET_CODE (PATTERN (jump)) == PARALLEL)
1629     loc = &XVECEXP (PATTERN (jump), 0, 0);
1630   else
1631     loc = &PATTERN (jump);
1632
1633   redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
1634   return num_validated_changes () > ochanges;
1635 }
1636
1637 /* Make JUMP go to NLABEL instead of where it jumps now.  If the old
1638    jump target label is unused as a result, it and the code following
1639    it may be deleted.
1640
1641    If NLABEL is zero, we are to turn the jump into a (possibly conditional)
1642    RETURN insn.
1643
1644    The return value will be 1 if the change was made, 0 if it wasn't
1645    (this can only occur for NLABEL == 0).  */
1646
1647 int
1648 redirect_jump (rtx jump, rtx nlabel, int delete_unused)
1649 {
1650   rtx olabel = JUMP_LABEL (jump);
1651   rtx note;
1652
1653   if (nlabel == olabel)
1654     return 1;
1655
1656   if (! redirect_exp (olabel, nlabel, jump))
1657     return 0;
1658
1659   JUMP_LABEL (jump) = nlabel;
1660   if (nlabel)
1661     ++LABEL_NUSES (nlabel);
1662
1663   /* Update labels in any REG_EQUAL note.  */
1664   if ((note = find_reg_note (jump, REG_EQUAL, NULL_RTX)) != NULL_RTX)
1665     {
1666       if (nlabel && olabel)
1667         {
1668           rtx dest = XEXP (note, 0);
1669
1670           if (GET_CODE (dest) == IF_THEN_ELSE)
1671             {
1672               if (GET_CODE (XEXP (dest, 1)) == LABEL_REF
1673                   && XEXP (XEXP (dest, 1), 0) == olabel)
1674                 XEXP (XEXP (dest, 1), 0) = nlabel;
1675               if (GET_CODE (XEXP (dest, 2)) == LABEL_REF
1676                   && XEXP (XEXP (dest, 2), 0) == olabel)
1677                 XEXP (XEXP (dest, 2), 0) = nlabel;
1678             }
1679           else
1680             remove_note (jump, note);
1681         }
1682       else
1683         remove_note (jump, note);
1684     }
1685
1686   /* If we're eliding the jump over exception cleanups at the end of a
1687      function, move the function end note so that -Wreturn-type works.  */
1688   if (olabel && nlabel
1689       && NEXT_INSN (olabel)
1690       && GET_CODE (NEXT_INSN (olabel)) == NOTE
1691       && NOTE_LINE_NUMBER (NEXT_INSN (olabel)) == NOTE_INSN_FUNCTION_END)
1692     emit_note_after (NOTE_INSN_FUNCTION_END, nlabel);
1693
1694   if (olabel && --LABEL_NUSES (olabel) == 0 && delete_unused
1695       /* Undefined labels will remain outside the insn stream.  */
1696       && INSN_UID (olabel))
1697     delete_related_insns (olabel);
1698
1699   return 1;
1700 }
1701
1702 /* Invert the jump condition of rtx X contained in jump insn, INSN.
1703    Accrue the modifications into the change group.  */
1704
1705 static void
1706 invert_exp_1 (rtx insn)
1707 {
1708   RTX_CODE code;
1709   rtx x = pc_set (insn);
1710
1711   if (!x)
1712     abort ();
1713   x = SET_SRC (x);
1714
1715   code = GET_CODE (x);
1716
1717   if (code == IF_THEN_ELSE)
1718     {
1719       rtx comp = XEXP (x, 0);
1720       rtx tem;
1721       enum rtx_code reversed_code;
1722
1723       /* We can do this in two ways:  The preferable way, which can only
1724          be done if this is not an integer comparison, is to reverse
1725          the comparison code.  Otherwise, swap the THEN-part and ELSE-part
1726          of the IF_THEN_ELSE.  If we can't do either, fail.  */
1727
1728       reversed_code = reversed_comparison_code (comp, insn);
1729
1730       if (reversed_code != UNKNOWN)
1731         {
1732           validate_change (insn, &XEXP (x, 0),
1733                            gen_rtx_fmt_ee (reversed_code,
1734                                            GET_MODE (comp), XEXP (comp, 0),
1735                                            XEXP (comp, 1)),
1736                            1);
1737           return;
1738         }
1739
1740       tem = XEXP (x, 1);
1741       validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
1742       validate_change (insn, &XEXP (x, 2), tem, 1);
1743     }
1744   else
1745     abort ();
1746 }
1747
1748 /* Invert the jump condition of conditional jump insn, INSN.
1749
1750    Return 1 if we can do so, 0 if we cannot find a way to do so that
1751    matches a pattern.  */
1752
1753 static int
1754 invert_exp (rtx insn)
1755 {
1756   invert_exp_1 (insn);
1757   if (num_validated_changes () == 0)
1758     return 0;
1759
1760   return apply_change_group ();
1761 }
1762
1763 /* Invert the condition of the jump JUMP, and make it jump to label
1764    NLABEL instead of where it jumps now.  Accrue changes into the
1765    change group.  Return false if we didn't see how to perform the
1766    inversion and redirection.  */
1767
1768 int
1769 invert_jump_1 (rtx jump, rtx nlabel)
1770 {
1771   int ochanges;
1772
1773   ochanges = num_validated_changes ();
1774   invert_exp_1 (jump);
1775   if (num_validated_changes () == ochanges)
1776     return 0;
1777
1778   return redirect_jump_1 (jump, nlabel);
1779 }
1780
1781 /* Invert the condition of the jump JUMP, and make it jump to label
1782    NLABEL instead of where it jumps now.  Return true if successful.  */
1783
1784 int
1785 invert_jump (rtx jump, rtx nlabel, int delete_unused)
1786 {
1787   /* We have to either invert the condition and change the label or
1788      do neither.  Either operation could fail.  We first try to invert
1789      the jump. If that succeeds, we try changing the label.  If that fails,
1790      we invert the jump back to what it was.  */
1791
1792   if (! invert_exp (jump))
1793     return 0;
1794
1795   if (redirect_jump (jump, nlabel, delete_unused))
1796     {
1797       /* Remove REG_EQUAL note if we have one.  */
1798       rtx note = find_reg_note (jump, REG_EQUAL, NULL_RTX);
1799       if (note)
1800         remove_note (jump, note);
1801
1802       invert_br_probabilities (jump);
1803
1804       return 1;
1805     }
1806
1807   if (! invert_exp (jump))
1808     /* This should just be putting it back the way it was.  */
1809     abort ();
1810
1811   return 0;
1812 }
1813
1814 \f
1815 /* Like rtx_equal_p except that it considers two REGs as equal
1816    if they renumber to the same value and considers two commutative
1817    operations to be the same if the order of the operands has been
1818    reversed.
1819
1820    ??? Addition is not commutative on the PA due to the weird implicit
1821    space register selection rules for memory addresses.  Therefore, we
1822    don't consider a + b == b + a.
1823
1824    We could/should make this test a little tighter.  Possibly only
1825    disabling it on the PA via some backend macro or only disabling this
1826    case when the PLUS is inside a MEM.  */
1827
1828 int
1829 rtx_renumbered_equal_p (rtx x, rtx y)
1830 {
1831   int i;
1832   enum rtx_code code = GET_CODE (x);
1833   const char *fmt;
1834
1835   if (x == y)
1836     return 1;
1837
1838   if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
1839       && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
1840                                   && GET_CODE (SUBREG_REG (y)) == REG)))
1841     {
1842       int reg_x = -1, reg_y = -1;
1843       int byte_x = 0, byte_y = 0;
1844
1845       if (GET_MODE (x) != GET_MODE (y))
1846         return 0;
1847
1848       /* If we haven't done any renumbering, don't
1849          make any assumptions.  */
1850       if (reg_renumber == 0)
1851         return rtx_equal_p (x, y);
1852
1853       if (code == SUBREG)
1854         {
1855           reg_x = REGNO (SUBREG_REG (x));
1856           byte_x = SUBREG_BYTE (x);
1857
1858           if (reg_renumber[reg_x] >= 0)
1859             {
1860               reg_x = subreg_regno_offset (reg_renumber[reg_x],
1861                                            GET_MODE (SUBREG_REG (x)),
1862                                            byte_x,
1863                                            GET_MODE (x));
1864               byte_x = 0;
1865             }
1866         }
1867       else
1868         {
1869           reg_x = REGNO (x);
1870           if (reg_renumber[reg_x] >= 0)
1871             reg_x = reg_renumber[reg_x];
1872         }
1873
1874       if (GET_CODE (y) == SUBREG)
1875         {
1876           reg_y = REGNO (SUBREG_REG (y));
1877           byte_y = SUBREG_BYTE (y);
1878
1879           if (reg_renumber[reg_y] >= 0)
1880             {
1881               reg_y = subreg_regno_offset (reg_renumber[reg_y],
1882                                            GET_MODE (SUBREG_REG (y)),
1883                                            byte_y,
1884                                            GET_MODE (y));
1885               byte_y = 0;
1886             }
1887         }
1888       else
1889         {
1890           reg_y = REGNO (y);
1891           if (reg_renumber[reg_y] >= 0)
1892             reg_y = reg_renumber[reg_y];
1893         }
1894
1895       return reg_x >= 0 && reg_x == reg_y && byte_x == byte_y;
1896     }
1897
1898   /* Now we have disposed of all the cases
1899      in which different rtx codes can match.  */
1900   if (code != GET_CODE (y))
1901     return 0;
1902
1903   switch (code)
1904     {
1905     case PC:
1906     case CC0:
1907     case ADDR_VEC:
1908     case ADDR_DIFF_VEC:
1909     case CONST_INT:
1910       return 0;
1911
1912     case LABEL_REF:
1913       /* We can't assume nonlocal labels have their following insns yet.  */
1914       if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
1915         return XEXP (x, 0) == XEXP (y, 0);
1916
1917       /* Two label-refs are equivalent if they point at labels
1918          in the same position in the instruction stream.  */
1919       return (next_real_insn (XEXP (x, 0))
1920               == next_real_insn (XEXP (y, 0)));
1921
1922     case SYMBOL_REF:
1923       return XSTR (x, 0) == XSTR (y, 0);
1924
1925     case CODE_LABEL:
1926       /* If we didn't match EQ equality above, they aren't the same.  */
1927       return 0;
1928
1929     default:
1930       break;
1931     }
1932
1933   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
1934
1935   if (GET_MODE (x) != GET_MODE (y))
1936     return 0;
1937
1938   /* For commutative operations, the RTX match if the operand match in any
1939      order.  Also handle the simple binary and unary cases without a loop.
1940
1941      ??? Don't consider PLUS a commutative operator; see comments above.  */
1942   if (COMMUTATIVE_P (x) && code != PLUS)
1943     return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1944              && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
1945             || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
1946                 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
1947   else if (NON_COMMUTATIVE_P (x))
1948     return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1949             && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
1950   else if (UNARY_P (x))
1951     return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
1952
1953   /* Compare the elements.  If any pair of corresponding elements
1954      fail to match, return 0 for the whole things.  */
1955
1956   fmt = GET_RTX_FORMAT (code);
1957   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1958     {
1959       int j;
1960       switch (fmt[i])
1961         {
1962         case 'w':
1963           if (XWINT (x, i) != XWINT (y, i))
1964             return 0;
1965           break;
1966
1967         case 'i':
1968           if (XINT (x, i) != XINT (y, i))
1969             return 0;
1970           break;
1971
1972         case 't':
1973           if (XTREE (x, i) != XTREE (y, i))
1974             return 0;
1975           break;
1976
1977         case 's':
1978           if (strcmp (XSTR (x, i), XSTR (y, i)))
1979             return 0;
1980           break;
1981
1982         case 'e':
1983           if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
1984             return 0;
1985           break;
1986
1987         case 'u':
1988           if (XEXP (x, i) != XEXP (y, i))
1989             return 0;
1990           /* Fall through.  */
1991         case '0':
1992           break;
1993
1994         case 'E':
1995           if (XVECLEN (x, i) != XVECLEN (y, i))
1996             return 0;
1997           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1998             if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1999               return 0;
2000           break;
2001
2002         default:
2003           abort ();
2004         }
2005     }
2006   return 1;
2007 }
2008 \f
2009 /* If X is a hard register or equivalent to one or a subregister of one,
2010    return the hard register number.  If X is a pseudo register that was not
2011    assigned a hard register, return the pseudo register number.  Otherwise,
2012    return -1.  Any rtx is valid for X.  */
2013
2014 int
2015 true_regnum (rtx x)
2016 {
2017   if (GET_CODE (x) == REG)
2018     {
2019       if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
2020         return reg_renumber[REGNO (x)];
2021       return REGNO (x);
2022     }
2023   if (GET_CODE (x) == SUBREG)
2024     {
2025       int base = true_regnum (SUBREG_REG (x));
2026       if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
2027         return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
2028                                            GET_MODE (SUBREG_REG (x)),
2029                                            SUBREG_BYTE (x), GET_MODE (x));
2030     }
2031   return -1;
2032 }
2033
2034 /* Return regno of the register REG and handle subregs too.  */
2035 unsigned int
2036 reg_or_subregno (rtx reg)
2037 {
2038   if (REG_P (reg))
2039     return REGNO (reg);
2040   if (GET_CODE (reg) == SUBREG)
2041     return REGNO (SUBREG_REG (reg));
2042   abort ();
2043 }