OSDN Git Service

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