OSDN Git Service

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