OSDN Git Service

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