OSDN Git Service

* recog.c (verify_changes, confirm_change_group): New functions,
[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 invert_exp_1 (rtx, rtx);
71 static int returnjump_p_1 (rtx *, void *);
72 static void delete_prior_computation (rtx, rtx);
73 \f
74 /* Alternate entry into the jump optimizer.  This entry point only rebuilds
75    the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
76    instructions.  */
77 void
78 rebuild_jump_labels (rtx f)
79 {
80   rtx insn;
81
82   timevar_push (TV_REBUILD_JUMP);
83   init_label_info (f);
84   mark_all_labels (f);
85
86   /* Keep track of labels used from static data; we don't track them
87      closely enough to delete them here, so make sure their reference
88      count doesn't drop to zero.  */
89
90   for (insn = forced_labels; insn; insn = XEXP (insn, 1))
91     if (LABEL_P (XEXP (insn, 0)))
92       LABEL_NUSES (XEXP (insn, 0))++;
93   timevar_pop (TV_REBUILD_JUMP);
94 }
95 \f
96 /* Some old code expects exactly one BARRIER as the NEXT_INSN of a
97    non-fallthru insn.  This is not generally true, as multiple barriers
98    may have crept in, or the BARRIER may be separated from the last
99    real insn by one or more NOTEs.
100
101    This simple pass moves barriers and removes duplicates so that the
102    old code is happy.
103  */
104 void
105 cleanup_barriers (void)
106 {
107   rtx insn, next, prev;
108   for (insn = get_insns (); insn; insn = next)
109     {
110       next = NEXT_INSN (insn);
111       if (BARRIER_P (insn))
112         {
113           prev = prev_nonnote_insn (insn);
114           if (BARRIER_P (prev))
115             delete_insn (insn);
116           else if (prev != PREV_INSN (insn))
117             reorder_insns (insn, insn, prev);
118         }
119     }
120 }
121
122 void
123 purge_line_number_notes (rtx f)
124 {
125   rtx last_note = 0;
126   rtx insn;
127   /* Delete extraneous line number notes.
128      Note that two consecutive notes for different lines are not really
129      extraneous.  There should be some indication where that line belonged,
130      even if it became empty.  */
131
132   for (insn = f; insn; insn = NEXT_INSN (insn))
133     if (NOTE_P (insn))
134       {
135         if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
136           /* Any previous line note was for the prologue; gdb wants a new
137              note after the prologue even if it is for the same line.  */
138           last_note = NULL_RTX;
139         else if (NOTE_LINE_NUMBER (insn) >= 0)
140           {
141             /* Delete this note if it is identical to previous note.  */
142             if (last_note
143 #ifdef USE_MAPPED_LOCATION
144                 && NOTE_SOURCE_LOCATION (insn) == NOTE_SOURCE_LOCATION (last_note)
145 #else
146                 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
147                 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note)
148 #endif
149 )
150               {
151                 delete_related_insns (insn);
152                 continue;
153               }
154
155             last_note = insn;
156           }
157       }
158 }
159 \f
160 /* Initialize LABEL_NUSES and JUMP_LABEL fields.  Delete any REG_LABEL
161    notes whose labels don't occur in the insn any more.  Returns the
162    largest INSN_UID found.  */
163 static void
164 init_label_info (rtx f)
165 {
166   rtx insn;
167
168   for (insn = f; insn; insn = NEXT_INSN (insn))
169     if (LABEL_P (insn))
170       LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
171     else if (JUMP_P (insn))
172       JUMP_LABEL (insn) = 0;
173     else if (NONJUMP_INSN_P (insn) || CALL_P (insn))
174       {
175         rtx note, next;
176
177         for (note = REG_NOTES (insn); note; note = next)
178           {
179             next = XEXP (note, 1);
180             if (REG_NOTE_KIND (note) == REG_LABEL
181                 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
182               remove_note (insn, note);
183           }
184       }
185 }
186
187 /* Mark the label each jump jumps to.
188    Combine consecutive labels, and count uses of labels.  */
189
190 static void
191 mark_all_labels (rtx f)
192 {
193   rtx insn;
194
195   for (insn = f; insn; insn = NEXT_INSN (insn))
196     if (INSN_P (insn))
197       {
198         mark_jump_label (PATTERN (insn), insn, 0);
199         if (! INSN_DELETED_P (insn) && JUMP_P (insn))
200           {
201             /* When we know the LABEL_REF contained in a REG used in
202                an indirect jump, we'll have a REG_LABEL note so that
203                flow can tell where it's going.  */
204             if (JUMP_LABEL (insn) == 0)
205               {
206                 rtx label_note = find_reg_note (insn, REG_LABEL, NULL_RTX);
207                 if (label_note)
208                   {
209                     /* But a LABEL_REF around the REG_LABEL note, so
210                        that we can canonicalize it.  */
211                     rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
212                                                        XEXP (label_note, 0));
213
214                     mark_jump_label (label_ref, insn, 0);
215                     XEXP (label_note, 0) = XEXP (label_ref, 0);
216                     JUMP_LABEL (insn) = XEXP (label_note, 0);
217                   }
218               }
219           }
220       }
221 }
222 \f
223 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
224    notes between START and END out before START.  START and END may be such
225    notes.  Returns the values of the new starting and ending insns, which
226    may be different if the original ones were such notes.
227    Return true if there were only such notes and no real instructions.  */
228
229 bool
230 squeeze_notes (rtx* startp, rtx* endp)
231 {
232   rtx start = *startp;
233   rtx end = *endp;
234
235   rtx insn;
236   rtx next;
237   rtx last = NULL;
238   rtx past_end = NEXT_INSN (end);
239
240   for (insn = start; insn != past_end; insn = next)
241     {
242       next = NEXT_INSN (insn);
243       if (NOTE_P (insn)
244           && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
245               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
246               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
247               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END))
248         {
249           /* BLOCK_BEG or BLOCK_END notes only exist in the `final' pass.  */
250           gcc_assert (NOTE_LINE_NUMBER (insn) != NOTE_INSN_BLOCK_BEG
251                       && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BLOCK_END);
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
773 /* Return nonzero if INSN is a (possibly) conditional jump inside a
774    PARALLEL.
775
776    Use this function is deprecated, since we need to support combined
777    branch and compare insns.  Use any_condjump_p instead whenever possible.  */
778
779 int
780 condjump_in_parallel_p (rtx insn)
781 {
782   rtx x = PATTERN (insn);
783
784   if (GET_CODE (x) != PARALLEL)
785     return 0;
786   else
787     x = XVECEXP (x, 0, 0);
788
789   if (GET_CODE (x) != SET)
790     return 0;
791   if (GET_CODE (SET_DEST (x)) != PC)
792     return 0;
793   if (GET_CODE (SET_SRC (x)) == LABEL_REF)
794     return 1;
795   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
796     return 0;
797   if (XEXP (SET_SRC (x), 2) == pc_rtx
798       && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
799           || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
800     return 1;
801   if (XEXP (SET_SRC (x), 1) == pc_rtx
802       && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
803           || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
804     return 1;
805   return 0;
806 }
807
808 /* Return set of PC, otherwise NULL.  */
809
810 rtx
811 pc_set (rtx insn)
812 {
813   rtx pat;
814   if (!JUMP_P (insn))
815     return NULL_RTX;
816   pat = PATTERN (insn);
817
818   /* The set is allowed to appear either as the insn pattern or
819      the first set in a PARALLEL.  */
820   if (GET_CODE (pat) == PARALLEL)
821     pat = XVECEXP (pat, 0, 0);
822   if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == PC)
823     return pat;
824
825   return NULL_RTX;
826 }
827
828 /* Return true when insn is an unconditional direct jump,
829    possibly bundled inside a PARALLEL.  */
830
831 int
832 any_uncondjump_p (rtx insn)
833 {
834   rtx x = pc_set (insn);
835   if (!x)
836     return 0;
837   if (GET_CODE (SET_SRC (x)) != LABEL_REF)
838     return 0;
839   if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
840     return 0;
841   return 1;
842 }
843
844 /* Return true when insn is a conditional jump.  This function works for
845    instructions containing PC sets in PARALLELs.  The instruction may have
846    various other effects so before removing the jump you must verify
847    onlyjump_p.
848
849    Note that unlike condjump_p it returns false for unconditional jumps.  */
850
851 int
852 any_condjump_p (rtx insn)
853 {
854   rtx x = pc_set (insn);
855   enum rtx_code a, b;
856
857   if (!x)
858     return 0;
859   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
860     return 0;
861
862   a = GET_CODE (XEXP (SET_SRC (x), 1));
863   b = GET_CODE (XEXP (SET_SRC (x), 2));
864
865   return ((b == PC && (a == LABEL_REF || a == RETURN))
866           || (a == PC && (b == LABEL_REF || b == RETURN)));
867 }
868
869 /* Return the label of a conditional jump.  */
870
871 rtx
872 condjump_label (rtx insn)
873 {
874   rtx x = pc_set (insn);
875
876   if (!x)
877     return NULL_RTX;
878   x = SET_SRC (x);
879   if (GET_CODE (x) == LABEL_REF)
880     return x;
881   if (GET_CODE (x) != IF_THEN_ELSE)
882     return NULL_RTX;
883   if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
884     return XEXP (x, 1);
885   if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
886     return XEXP (x, 2);
887   return NULL_RTX;
888 }
889
890 /* Return true if INSN is a (possibly conditional) return insn.  */
891
892 static int
893 returnjump_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
894 {
895   rtx x = *loc;
896
897   return x && (GET_CODE (x) == RETURN
898                || (GET_CODE (x) == SET && SET_IS_RETURN_P (x)));
899 }
900
901 int
902 returnjump_p (rtx insn)
903 {
904   if (!JUMP_P (insn))
905     return 0;
906   return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
907 }
908
909 /* Return true if INSN is a jump that only transfers control and
910    nothing more.  */
911
912 int
913 onlyjump_p (rtx insn)
914 {
915   rtx set;
916
917   if (!JUMP_P (insn))
918     return 0;
919
920   set = single_set (insn);
921   if (set == NULL)
922     return 0;
923   if (GET_CODE (SET_DEST (set)) != PC)
924     return 0;
925   if (side_effects_p (SET_SRC (set)))
926     return 0;
927
928   return 1;
929 }
930
931 #ifdef HAVE_cc0
932
933 /* Return nonzero if X is an RTX that only sets the condition codes
934    and has no side effects.  */
935
936 int
937 only_sets_cc0_p (rtx x)
938 {
939   if (! x)
940     return 0;
941
942   if (INSN_P (x))
943     x = PATTERN (x);
944
945   return sets_cc0_p (x) == 1 && ! side_effects_p (x);
946 }
947
948 /* Return 1 if X is an RTX that does nothing but set the condition codes
949    and CLOBBER or USE registers.
950    Return -1 if X does explicitly set the condition codes,
951    but also does other things.  */
952
953 int
954 sets_cc0_p (rtx x)
955 {
956   if (! x)
957     return 0;
958
959   if (INSN_P (x))
960     x = PATTERN (x);
961
962   if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
963     return 1;
964   if (GET_CODE (x) == PARALLEL)
965     {
966       int i;
967       int sets_cc0 = 0;
968       int other_things = 0;
969       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
970         {
971           if (GET_CODE (XVECEXP (x, 0, i)) == SET
972               && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
973             sets_cc0 = 1;
974           else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
975             other_things = 1;
976         }
977       return ! sets_cc0 ? 0 : other_things ? -1 : 1;
978     }
979   return 0;
980 }
981 #endif
982 \f
983 /* Follow any unconditional jump at LABEL;
984    return the ultimate label reached by any such chain of jumps.
985    Return null if the chain ultimately leads to a return instruction.
986    If LABEL is not followed by a jump, return LABEL.
987    If the chain loops or we can't find end, return LABEL,
988    since that tells caller to avoid changing the insn.
989
990    If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
991    a USE or CLOBBER.  */
992
993 rtx
994 follow_jumps (rtx label)
995 {
996   rtx insn;
997   rtx next;
998   rtx value = label;
999   int depth;
1000
1001   for (depth = 0;
1002        (depth < 10
1003         && (insn = next_active_insn (value)) != 0
1004         && JUMP_P (insn)
1005         && ((JUMP_LABEL (insn) != 0 && any_uncondjump_p (insn)
1006              && onlyjump_p (insn))
1007             || GET_CODE (PATTERN (insn)) == RETURN)
1008         && (next = NEXT_INSN (insn))
1009         && BARRIER_P (next));
1010        depth++)
1011     {
1012       /* Don't chain through the insn that jumps into a loop
1013          from outside the loop,
1014          since that would create multiple loop entry jumps
1015          and prevent loop optimization.  */
1016       rtx tem;
1017       if (!reload_completed)
1018         for (tem = value; tem != insn; tem = NEXT_INSN (tem))
1019           if (NOTE_P (tem)
1020               && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
1021                   /* ??? Optional.  Disables some optimizations, but makes
1022                      gcov output more accurate with -O.  */
1023                   || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
1024             return value;
1025
1026       /* If we have found a cycle, make the insn jump to itself.  */
1027       if (JUMP_LABEL (insn) == label)
1028         return label;
1029
1030       tem = next_active_insn (JUMP_LABEL (insn));
1031       if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
1032                   || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
1033         break;
1034
1035       value = JUMP_LABEL (insn);
1036     }
1037   if (depth == 10)
1038     return label;
1039   return value;
1040 }
1041
1042 \f
1043 /* Find all CODE_LABELs referred to in X, and increment their use counts.
1044    If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
1045    in INSN, then store one of them in JUMP_LABEL (INSN).
1046    If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
1047    referenced in INSN, add a REG_LABEL note containing that label to INSN.
1048    Also, when there are consecutive labels, canonicalize on the last of them.
1049
1050    Note that two labels separated by a loop-beginning note
1051    must be kept distinct if we have not yet done loop-optimization,
1052    because the gap between them is where loop-optimize
1053    will want to move invariant code to.  CROSS_JUMP tells us
1054    that loop-optimization is done with.  */
1055
1056 void
1057 mark_jump_label (rtx x, rtx insn, int in_mem)
1058 {
1059   RTX_CODE code = GET_CODE (x);
1060   int i;
1061   const char *fmt;
1062
1063   switch (code)
1064     {
1065     case PC:
1066     case CC0:
1067     case REG:
1068     case CONST_INT:
1069     case CONST_DOUBLE:
1070     case CLOBBER:
1071     case CALL:
1072       return;
1073
1074     case MEM:
1075       in_mem = 1;
1076       break;
1077
1078     case SYMBOL_REF:
1079       if (!in_mem)
1080         return;
1081
1082       /* If this is a constant-pool reference, see if it is a label.  */
1083       if (CONSTANT_POOL_ADDRESS_P (x))
1084         mark_jump_label (get_pool_constant (x), insn, in_mem);
1085       break;
1086
1087     case LABEL_REF:
1088       {
1089         rtx label = XEXP (x, 0);
1090
1091         /* Ignore remaining references to unreachable labels that
1092            have been deleted.  */
1093         if (NOTE_P (label)
1094             && NOTE_LINE_NUMBER (label) == NOTE_INSN_DELETED_LABEL)
1095           break;
1096
1097         if (!LABEL_P (label))
1098           abort ();
1099
1100         /* Ignore references to labels of containing functions.  */
1101         if (LABEL_REF_NONLOCAL_P (x))
1102           break;
1103
1104         XEXP (x, 0) = label;
1105         if (! insn || ! INSN_DELETED_P (insn))
1106           ++LABEL_NUSES (label);
1107
1108         if (insn)
1109           {
1110             if (JUMP_P (insn))
1111               JUMP_LABEL (insn) = label;
1112             else
1113               {
1114                 /* Add a REG_LABEL note for LABEL unless there already
1115                    is one.  All uses of a label, except for labels
1116                    that are the targets of jumps, must have a
1117                    REG_LABEL note.  */
1118                 if (! find_reg_note (insn, REG_LABEL, label))
1119                   REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
1120                                                         REG_NOTES (insn));
1121               }
1122           }
1123         return;
1124       }
1125
1126   /* Do walk the labels in a vector, but not the first operand of an
1127      ADDR_DIFF_VEC.  Don't set the JUMP_LABEL of a vector.  */
1128     case ADDR_VEC:
1129     case ADDR_DIFF_VEC:
1130       if (! INSN_DELETED_P (insn))
1131         {
1132           int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
1133
1134           for (i = 0; i < XVECLEN (x, eltnum); i++)
1135             mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, in_mem);
1136         }
1137       return;
1138
1139     default:
1140       break;
1141     }
1142
1143   fmt = GET_RTX_FORMAT (code);
1144   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1145     {
1146       if (fmt[i] == 'e')
1147         mark_jump_label (XEXP (x, i), insn, in_mem);
1148       else if (fmt[i] == 'E')
1149         {
1150           int j;
1151           for (j = 0; j < XVECLEN (x, i); j++)
1152             mark_jump_label (XVECEXP (x, i, j), insn, in_mem);
1153         }
1154     }
1155 }
1156
1157 /* If all INSN does is set the pc, delete it,
1158    and delete the insn that set the condition codes for it
1159    if that's what the previous thing was.  */
1160
1161 void
1162 delete_jump (rtx insn)
1163 {
1164   rtx set = single_set (insn);
1165
1166   if (set && GET_CODE (SET_DEST (set)) == PC)
1167     delete_computation (insn);
1168 }
1169
1170 /* Recursively delete prior insns that compute the value (used only by INSN
1171    which the caller is deleting) stored in the register mentioned by NOTE
1172    which is a REG_DEAD note associated with INSN.  */
1173
1174 static void
1175 delete_prior_computation (rtx note, rtx insn)
1176 {
1177   rtx our_prev;
1178   rtx reg = XEXP (note, 0);
1179
1180   for (our_prev = prev_nonnote_insn (insn);
1181        our_prev && (NONJUMP_INSN_P (our_prev)
1182                     || CALL_P (our_prev));
1183        our_prev = prev_nonnote_insn (our_prev))
1184     {
1185       rtx pat = PATTERN (our_prev);
1186
1187       /* If we reach a CALL which is not calling a const function
1188          or the callee pops the arguments, then give up.  */
1189       if (CALL_P (our_prev)
1190           && (! CONST_OR_PURE_CALL_P (our_prev)
1191               || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
1192         break;
1193
1194       /* If we reach a SEQUENCE, it is too complex to try to
1195          do anything with it, so give up.  We can be run during
1196          and after reorg, so SEQUENCE rtl can legitimately show
1197          up here.  */
1198       if (GET_CODE (pat) == SEQUENCE)
1199         break;
1200
1201       if (GET_CODE (pat) == USE
1202           && NONJUMP_INSN_P (XEXP (pat, 0)))
1203         /* reorg creates USEs that look like this.  We leave them
1204            alone because reorg needs them for its own purposes.  */
1205         break;
1206
1207       if (reg_set_p (reg, pat))
1208         {
1209           if (side_effects_p (pat) && !CALL_P (our_prev))
1210             break;
1211
1212           if (GET_CODE (pat) == PARALLEL)
1213             {
1214               /* If we find a SET of something else, we can't
1215                  delete the insn.  */
1216
1217               int i;
1218
1219               for (i = 0; i < XVECLEN (pat, 0); i++)
1220                 {
1221                   rtx part = XVECEXP (pat, 0, i);
1222
1223                   if (GET_CODE (part) == SET
1224                       && SET_DEST (part) != reg)
1225                     break;
1226                 }
1227
1228               if (i == XVECLEN (pat, 0))
1229                 delete_computation (our_prev);
1230             }
1231           else if (GET_CODE (pat) == SET
1232                    && REG_P (SET_DEST (pat)))
1233             {
1234               int dest_regno = REGNO (SET_DEST (pat));
1235               int dest_endregno
1236                 = (dest_regno
1237                    + (dest_regno < FIRST_PSEUDO_REGISTER
1238                       ? hard_regno_nregs[dest_regno]
1239                                         [GET_MODE (SET_DEST (pat))] : 1));
1240               int regno = REGNO (reg);
1241               int endregno
1242                 = (regno
1243                    + (regno < FIRST_PSEUDO_REGISTER
1244                       ? hard_regno_nregs[regno][GET_MODE (reg)] : 1));
1245
1246               if (dest_regno >= regno
1247                   && dest_endregno <= endregno)
1248                 delete_computation (our_prev);
1249
1250               /* We may have a multi-word hard register and some, but not
1251                  all, of the words of the register are needed in subsequent
1252                  insns.  Write REG_UNUSED notes for those parts that were not
1253                  needed.  */
1254               else if (dest_regno <= regno
1255                        && dest_endregno >= endregno)
1256                 {
1257                   int i;
1258
1259                   REG_NOTES (our_prev)
1260                     = gen_rtx_EXPR_LIST (REG_UNUSED, reg,
1261                                          REG_NOTES (our_prev));
1262
1263                   for (i = dest_regno; i < dest_endregno; i++)
1264                     if (! find_regno_note (our_prev, REG_UNUSED, i))
1265                       break;
1266
1267                   if (i == dest_endregno)
1268                     delete_computation (our_prev);
1269                 }
1270             }
1271
1272           break;
1273         }
1274
1275       /* If PAT references the register that dies here, it is an
1276          additional use.  Hence any prior SET isn't dead.  However, this
1277          insn becomes the new place for the REG_DEAD note.  */
1278       if (reg_overlap_mentioned_p (reg, pat))
1279         {
1280           XEXP (note, 1) = REG_NOTES (our_prev);
1281           REG_NOTES (our_prev) = note;
1282           break;
1283         }
1284     }
1285 }
1286
1287 /* Delete INSN and recursively delete insns that compute values used only
1288    by INSN.  This uses the REG_DEAD notes computed during flow analysis.
1289    If we are running before flow.c, we need do nothing since flow.c will
1290    delete dead code.  We also can't know if the registers being used are
1291    dead or not at this point.
1292
1293    Otherwise, look at all our REG_DEAD notes.  If a previous insn does
1294    nothing other than set a register that dies in this insn, we can delete
1295    that insn as well.
1296
1297    On machines with CC0, if CC0 is used in this insn, we may be able to
1298    delete the insn that set it.  */
1299
1300 static void
1301 delete_computation (rtx insn)
1302 {
1303   rtx note, next;
1304
1305 #ifdef HAVE_cc0
1306   if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
1307     {
1308       rtx prev = prev_nonnote_insn (insn);
1309       /* We assume that at this stage
1310          CC's are always set explicitly
1311          and always immediately before the jump that
1312          will use them.  So if the previous insn
1313          exists to set the CC's, delete it
1314          (unless it performs auto-increments, etc.).  */
1315       if (prev && NONJUMP_INSN_P (prev)
1316           && sets_cc0_p (PATTERN (prev)))
1317         {
1318           if (sets_cc0_p (PATTERN (prev)) > 0
1319               && ! side_effects_p (PATTERN (prev)))
1320             delete_computation (prev);
1321           else
1322             /* Otherwise, show that cc0 won't be used.  */
1323             REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
1324                                                   cc0_rtx, REG_NOTES (prev));
1325         }
1326     }
1327 #endif
1328
1329   for (note = REG_NOTES (insn); note; note = next)
1330     {
1331       next = XEXP (note, 1);
1332
1333       if (REG_NOTE_KIND (note) != REG_DEAD
1334           /* Verify that the REG_NOTE is legitimate.  */
1335           || !REG_P (XEXP (note, 0)))
1336         continue;
1337
1338       delete_prior_computation (note, insn);
1339     }
1340
1341   delete_related_insns (insn);
1342 }
1343 \f
1344 /* Delete insn INSN from the chain of insns and update label ref counts
1345    and delete insns now unreachable.
1346
1347    Returns the first insn after INSN that was not deleted.
1348
1349    Usage of this instruction is deprecated.  Use delete_insn instead and
1350    subsequent cfg_cleanup pass to delete unreachable code if needed.  */
1351
1352 rtx
1353 delete_related_insns (rtx insn)
1354 {
1355   int was_code_label = (LABEL_P (insn));
1356   rtx note;
1357   rtx next = NEXT_INSN (insn), prev = PREV_INSN (insn);
1358
1359   while (next && INSN_DELETED_P (next))
1360     next = NEXT_INSN (next);
1361
1362   /* This insn is already deleted => return first following nondeleted.  */
1363   if (INSN_DELETED_P (insn))
1364     return next;
1365
1366   delete_insn (insn);
1367
1368   /* If instruction is followed by a barrier,
1369      delete the barrier too.  */
1370
1371   if (next != 0 && BARRIER_P (next))
1372     delete_insn (next);
1373
1374   /* If deleting a jump, decrement the count of the label,
1375      and delete the label if it is now unused.  */
1376
1377   if (JUMP_P (insn) && JUMP_LABEL (insn))
1378     {
1379       rtx lab = JUMP_LABEL (insn), lab_next;
1380
1381       if (LABEL_NUSES (lab) == 0)
1382         {
1383           /* This can delete NEXT or PREV,
1384              either directly if NEXT is JUMP_LABEL (INSN),
1385              or indirectly through more levels of jumps.  */
1386           delete_related_insns (lab);
1387
1388           /* I feel a little doubtful about this loop,
1389              but I see no clean and sure alternative way
1390              to find the first insn after INSN that is not now deleted.
1391              I hope this works.  */
1392           while (next && INSN_DELETED_P (next))
1393             next = NEXT_INSN (next);
1394           return next;
1395         }
1396       else if (tablejump_p (insn, NULL, &lab_next))
1397         {
1398           /* If we're deleting the tablejump, delete the dispatch table.
1399              We may not be able to kill the label immediately preceding
1400              just yet, as it might be referenced in code leading up to
1401              the tablejump.  */
1402           delete_related_insns (lab_next);
1403         }
1404     }
1405
1406   /* Likewise if we're deleting a dispatch table.  */
1407
1408   if (JUMP_P (insn)
1409       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
1410           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
1411     {
1412       rtx pat = PATTERN (insn);
1413       int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1414       int len = XVECLEN (pat, diff_vec_p);
1415
1416       for (i = 0; i < len; i++)
1417         if (LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
1418           delete_related_insns (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
1419       while (next && INSN_DELETED_P (next))
1420         next = NEXT_INSN (next);
1421       return next;
1422     }
1423
1424   /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note.  */
1425   if (NONJUMP_INSN_P (insn) || CALL_P (insn))
1426     for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1427       if (REG_NOTE_KIND (note) == REG_LABEL
1428           /* This could also be a NOTE_INSN_DELETED_LABEL note.  */
1429           && LABEL_P (XEXP (note, 0)))
1430         if (LABEL_NUSES (XEXP (note, 0)) == 0)
1431           delete_related_insns (XEXP (note, 0));
1432
1433   while (prev && (INSN_DELETED_P (prev) || NOTE_P (prev)))
1434     prev = PREV_INSN (prev);
1435
1436   /* If INSN was a label and a dispatch table follows it,
1437      delete the dispatch table.  The tablejump must have gone already.
1438      It isn't useful to fall through into a table.  */
1439
1440   if (was_code_label
1441       && NEXT_INSN (insn) != 0
1442       && JUMP_P (NEXT_INSN (insn))
1443       && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
1444           || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
1445     next = delete_related_insns (NEXT_INSN (insn));
1446
1447   /* If INSN was a label, delete insns following it if now unreachable.  */
1448
1449   if (was_code_label && prev && BARRIER_P (prev))
1450     {
1451       enum rtx_code code;
1452       while (next)
1453         {
1454           code = GET_CODE (next);
1455           if (code == NOTE
1456               && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
1457             next = NEXT_INSN (next);
1458           /* Keep going past other deleted labels to delete what follows.  */
1459           else if (code == CODE_LABEL && INSN_DELETED_P (next))
1460             next = NEXT_INSN (next);
1461           else if (code == BARRIER || INSN_P (next))
1462             /* Note: if this deletes a jump, it can cause more
1463                deletion of unreachable code, after a different label.
1464                As long as the value from this recursive call is correct,
1465                this invocation functions correctly.  */
1466             next = delete_related_insns (next);
1467           else
1468             break;
1469         }
1470     }
1471
1472   return next;
1473 }
1474 \f
1475 /* Delete a range of insns from FROM to TO, inclusive.
1476    This is for the sake of peephole optimization, so assume
1477    that whatever these insns do will still be done by a new
1478    peephole insn that will replace them.  */
1479
1480 void
1481 delete_for_peephole (rtx from, rtx to)
1482 {
1483   rtx insn = from;
1484
1485   while (1)
1486     {
1487       rtx next = NEXT_INSN (insn);
1488       rtx prev = PREV_INSN (insn);
1489
1490       if (!NOTE_P (insn))
1491         {
1492           INSN_DELETED_P (insn) = 1;
1493
1494           /* Patch this insn out of the chain.  */
1495           /* We don't do this all at once, because we
1496              must preserve all NOTEs.  */
1497           if (prev)
1498             NEXT_INSN (prev) = next;
1499
1500           if (next)
1501             PREV_INSN (next) = prev;
1502         }
1503
1504       if (insn == to)
1505         break;
1506       insn = next;
1507     }
1508
1509   /* Note that if TO is an unconditional jump
1510      we *do not* delete the BARRIER that follows,
1511      since the peephole that replaces this sequence
1512      is also an unconditional jump in that case.  */
1513 }
1514 \f
1515 /* Throughout LOC, redirect OLABEL to NLABEL.  Treat null OLABEL or
1516    NLABEL as a return.  Accrue modifications into the change group.  */
1517
1518 static void
1519 redirect_exp_1 (rtx *loc, rtx olabel, rtx nlabel, rtx insn)
1520 {
1521   rtx x = *loc;
1522   RTX_CODE code = GET_CODE (x);
1523   int i;
1524   const char *fmt;
1525
1526   if (code == LABEL_REF)
1527     {
1528       if (XEXP (x, 0) == olabel)
1529         {
1530           rtx n;
1531           if (nlabel)
1532             n = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1533           else
1534             n = gen_rtx_RETURN (VOIDmode);
1535
1536           validate_change (insn, loc, n, 1);
1537           return;
1538         }
1539     }
1540   else if (code == RETURN && olabel == 0)
1541     {
1542       x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1543       if (loc == &PATTERN (insn))
1544         x = gen_rtx_SET (VOIDmode, pc_rtx, x);
1545       validate_change (insn, loc, x, 1);
1546       return;
1547     }
1548
1549   if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
1550       && GET_CODE (SET_SRC (x)) == LABEL_REF
1551       && XEXP (SET_SRC (x), 0) == olabel)
1552     {
1553       validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 1);
1554       return;
1555     }
1556
1557   fmt = GET_RTX_FORMAT (code);
1558   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1559     {
1560       if (fmt[i] == 'e')
1561         redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
1562       else if (fmt[i] == 'E')
1563         {
1564           int j;
1565           for (j = 0; j < XVECLEN (x, i); j++)
1566             redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
1567         }
1568     }
1569 }
1570
1571 /* Make JUMP go to NLABEL instead of where it jumps now.  Accrue
1572    the modifications into the change group.  Return false if we did
1573    not see how to do that.  */
1574
1575 int
1576 redirect_jump_1 (rtx jump, rtx nlabel)
1577 {
1578   int ochanges = num_validated_changes ();
1579   rtx *loc;
1580
1581   if (GET_CODE (PATTERN (jump)) == PARALLEL)
1582     loc = &XVECEXP (PATTERN (jump), 0, 0);
1583   else
1584     loc = &PATTERN (jump);
1585
1586   redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
1587   return num_validated_changes () > ochanges;
1588 }
1589
1590 /* Make JUMP go to NLABEL instead of where it jumps now.  If the old
1591    jump target label is unused as a result, it and the code following
1592    it may be deleted.
1593
1594    If NLABEL is zero, we are to turn the jump into a (possibly conditional)
1595    RETURN insn.
1596
1597    The return value will be 1 if the change was made, 0 if it wasn't
1598    (this can only occur for NLABEL == 0).  */
1599
1600 int
1601 redirect_jump (rtx jump, rtx nlabel, int delete_unused)
1602 {
1603   rtx olabel = JUMP_LABEL (jump);
1604
1605   if (nlabel == olabel)
1606     return 1;
1607
1608   if (! redirect_jump_1 (jump, nlabel) || ! apply_change_group ())
1609     return 0;
1610
1611   redirect_jump_2 (jump, olabel, nlabel, delete_unused, 0);
1612   return 1;
1613 }
1614
1615 /* Fix up JUMP_LABEL and label ref counts after OLABEL has been replaced with
1616    NLABEL in JUMP.  If DELETE_UNUSED is non-negative, copy a
1617    NOTE_INSN_FUNCTION_END found after OLABEL to the place after NLABEL.
1618    If DELETE_UNUSED is positive, delete related insn to OLABEL if its ref
1619    count has dropped to zero.  */
1620 void
1621 redirect_jump_2 (rtx jump, rtx olabel, rtx nlabel, int delete_unused,
1622                  int invert)
1623 {
1624   rtx note;
1625
1626   JUMP_LABEL (jump) = nlabel;
1627   if (nlabel)
1628     ++LABEL_NUSES (nlabel);
1629
1630   /* Update labels in any REG_EQUAL note.  */
1631   if ((note = find_reg_note (jump, REG_EQUAL, NULL_RTX)) != NULL_RTX)
1632     {
1633       if (!nlabel || (invert && !invert_exp_1 (XEXP (note, 0), jump)))
1634         remove_note (jump, note);
1635       else
1636         {
1637           redirect_exp_1 (&XEXP (note, 0), olabel, nlabel, jump);
1638           confirm_change_group ();
1639         }
1640     }
1641
1642   /* If we're eliding the jump over exception cleanups at the end of a
1643      function, move the function end note so that -Wreturn-type works.  */
1644   if (olabel && nlabel
1645       && NEXT_INSN (olabel)
1646       && NOTE_P (NEXT_INSN (olabel))
1647       && NOTE_LINE_NUMBER (NEXT_INSN (olabel)) == NOTE_INSN_FUNCTION_END
1648       && delete_unused >= 0)
1649     emit_note_after (NOTE_INSN_FUNCTION_END, nlabel);
1650
1651   if (olabel && --LABEL_NUSES (olabel) == 0 && delete_unused > 0
1652       /* Undefined labels will remain outside the insn stream.  */
1653       && INSN_UID (olabel))
1654     delete_related_insns (olabel);
1655   if (invert)
1656     invert_br_probabilities (jump);
1657 }
1658
1659 /* Invert the jump condition X contained in jump insn INSN.  Accrue the
1660    modifications into the change group.  Return nonzero for success.  */
1661 static int
1662 invert_exp_1 (rtx x, rtx insn)
1663 {
1664   RTX_CODE code = GET_CODE (x);
1665
1666   if (code == IF_THEN_ELSE)
1667     {
1668       rtx comp = XEXP (x, 0);
1669       rtx tem;
1670       enum rtx_code reversed_code;
1671
1672       /* We can do this in two ways:  The preferable way, which can only
1673          be done if this is not an integer comparison, is to reverse
1674          the comparison code.  Otherwise, swap the THEN-part and ELSE-part
1675          of the IF_THEN_ELSE.  If we can't do either, fail.  */
1676
1677       reversed_code = reversed_comparison_code (comp, insn);
1678
1679       if (reversed_code != UNKNOWN)
1680         {
1681           validate_change (insn, &XEXP (x, 0),
1682                            gen_rtx_fmt_ee (reversed_code,
1683                                            GET_MODE (comp), XEXP (comp, 0),
1684                                            XEXP (comp, 1)),
1685                            1);
1686           return 1;
1687         }
1688
1689       tem = XEXP (x, 1);
1690       validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
1691       validate_change (insn, &XEXP (x, 2), tem, 1);
1692       return 1;
1693     }
1694   else
1695     return 0;
1696 }
1697
1698 /* Invert the condition of the jump JUMP, and make it jump to label
1699    NLABEL instead of where it jumps now.  Accrue changes into the
1700    change group.  Return false if we didn't see how to perform the
1701    inversion and redirection.  */
1702
1703 int
1704 invert_jump_1 (rtx jump, rtx nlabel)
1705 {
1706   rtx x = pc_set (jump);
1707   int ochanges;
1708
1709   ochanges = num_validated_changes ();
1710   if (!x || !invert_exp_1 (SET_SRC (x), jump))
1711     abort ();
1712   if (num_validated_changes () == ochanges)
1713     return 0;
1714
1715   return redirect_jump_1 (jump, nlabel);
1716 }
1717
1718 /* Invert the condition of the jump JUMP, and make it jump to label
1719    NLABEL instead of where it jumps now.  Return true if successful.  */
1720
1721 int
1722 invert_jump (rtx jump, rtx nlabel, int delete_unused)
1723 {
1724   rtx olabel = JUMP_LABEL (jump);
1725
1726   if (invert_jump_1 (jump, nlabel) && apply_change_group ())
1727     {
1728       redirect_jump_2 (jump, olabel, nlabel, delete_unused, 1);
1729       return 1;
1730     }
1731   cancel_changes (0);
1732   return 0;
1733 }
1734
1735 \f
1736 /* Like rtx_equal_p except that it considers two REGs as equal
1737    if they renumber to the same value and considers two commutative
1738    operations to be the same if the order of the operands has been
1739    reversed.
1740
1741    ??? Addition is not commutative on the PA due to the weird implicit
1742    space register selection rules for memory addresses.  Therefore, we
1743    don't consider a + b == b + a.
1744
1745    We could/should make this test a little tighter.  Possibly only
1746    disabling it on the PA via some backend macro or only disabling this
1747    case when the PLUS is inside a MEM.  */
1748
1749 int
1750 rtx_renumbered_equal_p (rtx x, rtx y)
1751 {
1752   int i;
1753   enum rtx_code code = GET_CODE (x);
1754   const char *fmt;
1755
1756   if (x == y)
1757     return 1;
1758
1759   if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
1760       && (REG_P (y) || (GET_CODE (y) == SUBREG
1761                                   && REG_P (SUBREG_REG (y)))))
1762     {
1763       int reg_x = -1, reg_y = -1;
1764       int byte_x = 0, byte_y = 0;
1765
1766       if (GET_MODE (x) != GET_MODE (y))
1767         return 0;
1768
1769       /* If we haven't done any renumbering, don't
1770          make any assumptions.  */
1771       if (reg_renumber == 0)
1772         return rtx_equal_p (x, y);
1773
1774       if (code == SUBREG)
1775         {
1776           reg_x = REGNO (SUBREG_REG (x));
1777           byte_x = SUBREG_BYTE (x);
1778
1779           if (reg_renumber[reg_x] >= 0)
1780             {
1781               reg_x = subreg_regno_offset (reg_renumber[reg_x],
1782                                            GET_MODE (SUBREG_REG (x)),
1783                                            byte_x,
1784                                            GET_MODE (x));
1785               byte_x = 0;
1786             }
1787         }
1788       else
1789         {
1790           reg_x = REGNO (x);
1791           if (reg_renumber[reg_x] >= 0)
1792             reg_x = reg_renumber[reg_x];
1793         }
1794
1795       if (GET_CODE (y) == SUBREG)
1796         {
1797           reg_y = REGNO (SUBREG_REG (y));
1798           byte_y = SUBREG_BYTE (y);
1799
1800           if (reg_renumber[reg_y] >= 0)
1801             {
1802               reg_y = subreg_regno_offset (reg_renumber[reg_y],
1803                                            GET_MODE (SUBREG_REG (y)),
1804                                            byte_y,
1805                                            GET_MODE (y));
1806               byte_y = 0;
1807             }
1808         }
1809       else
1810         {
1811           reg_y = REGNO (y);
1812           if (reg_renumber[reg_y] >= 0)
1813             reg_y = reg_renumber[reg_y];
1814         }
1815
1816       return reg_x >= 0 && reg_x == reg_y && byte_x == byte_y;
1817     }
1818
1819   /* Now we have disposed of all the cases
1820      in which different rtx codes can match.  */
1821   if (code != GET_CODE (y))
1822     return 0;
1823
1824   switch (code)
1825     {
1826     case PC:
1827     case CC0:
1828     case ADDR_VEC:
1829     case ADDR_DIFF_VEC:
1830     case CONST_INT:
1831       return 0;
1832
1833     case LABEL_REF:
1834       /* We can't assume nonlocal labels have their following insns yet.  */
1835       if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
1836         return XEXP (x, 0) == XEXP (y, 0);
1837
1838       /* Two label-refs are equivalent if they point at labels
1839          in the same position in the instruction stream.  */
1840       return (next_real_insn (XEXP (x, 0))
1841               == next_real_insn (XEXP (y, 0)));
1842
1843     case SYMBOL_REF:
1844       return XSTR (x, 0) == XSTR (y, 0);
1845
1846     case CODE_LABEL:
1847       /* If we didn't match EQ equality above, they aren't the same.  */
1848       return 0;
1849
1850     default:
1851       break;
1852     }
1853
1854   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
1855
1856   if (GET_MODE (x) != GET_MODE (y))
1857     return 0;
1858
1859   /* For commutative operations, the RTX match if the operand match in any
1860      order.  Also handle the simple binary and unary cases without a loop.
1861
1862      ??? Don't consider PLUS a commutative operator; see comments above.  */
1863   if (COMMUTATIVE_P (x) && code != PLUS)
1864     return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1865              && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
1866             || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
1867                 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
1868   else if (NON_COMMUTATIVE_P (x))
1869     return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1870             && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
1871   else if (UNARY_P (x))
1872     return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
1873
1874   /* Compare the elements.  If any pair of corresponding elements
1875      fail to match, return 0 for the whole things.  */
1876
1877   fmt = GET_RTX_FORMAT (code);
1878   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1879     {
1880       int j;
1881       switch (fmt[i])
1882         {
1883         case 'w':
1884           if (XWINT (x, i) != XWINT (y, i))
1885             return 0;
1886           break;
1887
1888         case 'i':
1889           if (XINT (x, i) != XINT (y, i))
1890             return 0;
1891           break;
1892
1893         case 't':
1894           if (XTREE (x, i) != XTREE (y, i))
1895             return 0;
1896           break;
1897
1898         case 's':
1899           if (strcmp (XSTR (x, i), XSTR (y, i)))
1900             return 0;
1901           break;
1902
1903         case 'e':
1904           if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
1905             return 0;
1906           break;
1907
1908         case 'u':
1909           if (XEXP (x, i) != XEXP (y, i))
1910             return 0;
1911           /* Fall through.  */
1912         case '0':
1913           break;
1914
1915         case 'E':
1916           if (XVECLEN (x, i) != XVECLEN (y, i))
1917             return 0;
1918           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1919             if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1920               return 0;
1921           break;
1922
1923         default:
1924           abort ();
1925         }
1926     }
1927   return 1;
1928 }
1929 \f
1930 /* If X is a hard register or equivalent to one or a subregister of one,
1931    return the hard register number.  If X is a pseudo register that was not
1932    assigned a hard register, return the pseudo register number.  Otherwise,
1933    return -1.  Any rtx is valid for X.  */
1934
1935 int
1936 true_regnum (rtx x)
1937 {
1938   if (REG_P (x))
1939     {
1940       if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
1941         return reg_renumber[REGNO (x)];
1942       return REGNO (x);
1943     }
1944   if (GET_CODE (x) == SUBREG)
1945     {
1946       int base = true_regnum (SUBREG_REG (x));
1947       if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
1948         return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
1949                                            GET_MODE (SUBREG_REG (x)),
1950                                            SUBREG_BYTE (x), GET_MODE (x));
1951     }
1952   return -1;
1953 }
1954
1955 /* Return regno of the register REG and handle subregs too.  */
1956 unsigned int
1957 reg_or_subregno (rtx reg)
1958 {
1959   if (REG_P (reg))
1960     return REGNO (reg);
1961   if (GET_CODE (reg) == SUBREG)
1962     return REGNO (SUBREG_REG (reg));
1963   abort ();
1964 }