OSDN Git Service

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