OSDN Git Service

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