OSDN Git Service

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