OSDN Git Service

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