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