OSDN Git Service

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