OSDN Git Service

e18abef51a781f290c0d318317df4b3835584023
[pf3gnuchains/gcc-fork.git] / gcc / jump.c
1 /* Optimize jump instructions, for GNU compiler.
2    Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3    1998, 1999, 2000, 2001, 2002, 2003 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 "toplev.h"
54 #include "reload.h"
55 #include "predict.h"
56
57 /* Optimize jump y; x: ... y: jumpif... x?
58    Don't know if it is worth bothering with.  */
59 /* Optimize two cases of conditional jump to conditional jump?
60    This can never delete any instruction or make anything dead,
61    or even change what is live at any point.
62    So perhaps let combiner do it.  */
63
64 static rtx next_nonnote_insn_in_loop    PARAMS ((rtx));
65 static void init_label_info             PARAMS ((rtx));
66 static void mark_all_labels             PARAMS ((rtx));
67 static int duplicate_loop_exit_test     PARAMS ((rtx));
68 static void delete_computation          PARAMS ((rtx));
69 static void redirect_exp_1              PARAMS ((rtx *, rtx, rtx, rtx));
70 static int redirect_exp                 PARAMS ((rtx, rtx, rtx));
71 static void invert_exp_1                PARAMS ((rtx));
72 static int invert_exp                   PARAMS ((rtx));
73 static int returnjump_p_1               PARAMS ((rtx *, void *));
74 static void delete_prior_computation    PARAMS ((rtx, rtx));
75 \f
76 /* Alternate entry into the jump optimizer.  This entry point only rebuilds
77    the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
78    instructions.  */
79 void
80 rebuild_jump_labels (f)
81      rtx f;
82 {
83   rtx insn;
84
85   init_label_info (f);
86   mark_all_labels (f);
87
88   /* Keep track of labels used from static data; we don't track them
89      closely enough to delete them here, so make sure their reference
90      count doesn't drop to zero.  */
91
92   for (insn = forced_labels; insn; insn = XEXP (insn, 1))
93     if (GET_CODE (XEXP (insn, 0)) == CODE_LABEL)
94       LABEL_NUSES (XEXP (insn, 0))++;
95 }
96 \f
97 /* Some old code expects exactly one BARRIER as the NEXT_INSN of a
98    non-fallthru insn.  This is not generally true, as multiple barriers
99    may have crept in, or the BARRIER may be separated from the last
100    real insn by one or more NOTEs.
101
102    This simple pass moves barriers and removes duplicates so that the
103    old code is happy.
104  */
105 void
106 cleanup_barriers ()
107 {
108   rtx insn, next, prev;
109   for (insn = get_insns (); insn; insn = next)
110     {
111       next = NEXT_INSN (insn);
112       if (GET_CODE (insn) == BARRIER)
113         {
114           prev = prev_nonnote_insn (insn);
115           if (GET_CODE (prev) == BARRIER)
116             delete_barrier (insn);
117           else if (prev != PREV_INSN (insn))
118             reorder_insns (insn, insn, prev);
119         }
120     }
121 }
122 \f
123 /* Return the next insn after INSN that is not a NOTE and is in the loop,
124    i.e. when there is no such INSN before NOTE_INSN_LOOP_END return NULL_RTX.
125    This routine does not look inside SEQUENCEs.  */
126
127 static rtx
128 next_nonnote_insn_in_loop (insn)
129      rtx insn;
130 {
131   while (insn)
132     {
133       insn = NEXT_INSN (insn);
134       if (insn == 0 || GET_CODE (insn) != NOTE)
135         break;
136       if (GET_CODE (insn) == NOTE
137           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
138         return NULL_RTX;
139     }
140
141   return insn;
142 }
143
144 void
145 copy_loop_headers (f)
146      rtx f;
147 {
148   rtx insn, next;
149   /* Now iterate optimizing jumps until nothing changes over one pass.  */
150   for (insn = f; insn; insn = next)
151     {
152       rtx temp, temp1;
153
154       next = NEXT_INSN (insn);
155
156       /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
157          jump.  Try to optimize by duplicating the loop exit test if so.
158          This is only safe immediately after regscan, because it uses
159          the values of regno_first_uid and regno_last_uid.  */
160       if (GET_CODE (insn) == NOTE
161           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
162           && (temp1 = next_nonnote_insn_in_loop (insn)) != 0
163           && any_uncondjump_p (temp1) && onlyjump_p (temp1))
164         {
165           temp = PREV_INSN (insn);
166           if (duplicate_loop_exit_test (insn))
167             {
168               next = NEXT_INSN (temp);
169             }
170         }
171     }
172 }
173
174 void
175 purge_line_number_notes (f)
176      rtx f;
177 {
178   rtx last_note = 0;
179   rtx insn;
180   /* Delete extraneous line number notes.
181      Note that two consecutive notes for different lines are not really
182      extraneous.  There should be some indication where that line belonged,
183      even if it became empty.  */
184
185   for (insn = f; insn; insn = NEXT_INSN (insn))
186     if (GET_CODE (insn) == NOTE)
187       {
188         if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
189           /* Any previous line note was for the prologue; gdb wants a new
190              note after the prologue even if it is for the same line.  */
191           last_note = NULL_RTX;
192         else if (NOTE_LINE_NUMBER (insn) >= 0)
193           {
194             /* Delete this note if it is identical to previous note.  */
195             if (last_note
196                 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
197                 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
198               {
199                 delete_related_insns (insn);
200                 continue;
201               }
202
203             last_note = insn;
204           }
205       }
206 }
207 \f
208 /* Initialize LABEL_NUSES and JUMP_LABEL fields.  Delete any REG_LABEL
209    notes whose labels don't occur in the insn any more.  Returns the
210    largest INSN_UID found.  */
211 static void
212 init_label_info (f)
213      rtx f;
214 {
215   rtx insn;
216
217   for (insn = f; insn; insn = NEXT_INSN (insn))
218     if (GET_CODE (insn) == CODE_LABEL)
219       LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
220     else if (GET_CODE (insn) == JUMP_INSN)
221       JUMP_LABEL (insn) = 0;
222     else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
223       {
224         rtx note, next;
225
226         for (note = REG_NOTES (insn); note; note = next)
227           {
228             next = XEXP (note, 1);
229             if (REG_NOTE_KIND (note) == REG_LABEL
230                 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
231               remove_note (insn, note);
232           }
233       }
234 }
235
236 /* Mark the label each jump jumps to.
237    Combine consecutive labels, and count uses of labels.  */
238
239 static void
240 mark_all_labels (f)
241      rtx f;
242 {
243   rtx insn;
244
245   for (insn = f; insn; insn = NEXT_INSN (insn))
246     if (INSN_P (insn))
247       {
248         if (GET_CODE (insn) == CALL_INSN
249             && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
250           {
251             mark_all_labels (XEXP (PATTERN (insn), 0));
252             mark_all_labels (XEXP (PATTERN (insn), 1));
253             mark_all_labels (XEXP (PATTERN (insn), 2));
254
255             /* Canonicalize the tail recursion label attached to the
256                CALL_PLACEHOLDER insn.  */
257             if (XEXP (PATTERN (insn), 3))
258               {
259                 rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
260                                                    XEXP (PATTERN (insn), 3));
261                 mark_jump_label (label_ref, insn, 0);
262                 XEXP (PATTERN (insn), 3) = XEXP (label_ref, 0);
263               }
264
265             continue;
266           }
267
268         mark_jump_label (PATTERN (insn), insn, 0);
269         if (! INSN_DELETED_P (insn) && GET_CODE (insn) == JUMP_INSN)
270           {
271             /* When we know the LABEL_REF contained in a REG used in
272                an indirect jump, we'll have a REG_LABEL note so that
273                flow can tell where it's going.  */
274             if (JUMP_LABEL (insn) == 0)
275               {
276                 rtx label_note = find_reg_note (insn, REG_LABEL, NULL_RTX);
277                 if (label_note)
278                   {
279                     /* But a LABEL_REF around the REG_LABEL note, so
280                        that we can canonicalize it.  */
281                     rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
282                                                        XEXP (label_note, 0));
283
284                     mark_jump_label (label_ref, insn, 0);
285                     XEXP (label_note, 0) = XEXP (label_ref, 0);
286                     JUMP_LABEL (insn) = XEXP (label_note, 0);
287                   }
288               }
289           }
290       }
291 }
292
293 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
294    jump.  Assume that this unconditional jump is to the exit test code.  If
295    the code is sufficiently simple, make a copy of it before INSN,
296    followed by a jump to the exit of the loop.  Then delete the unconditional
297    jump after INSN.
298
299    Return 1 if we made the change, else 0.
300
301    This is only safe immediately after a regscan pass because it uses the
302    values of regno_first_uid and regno_last_uid.  */
303
304 static int
305 duplicate_loop_exit_test (loop_start)
306      rtx loop_start;
307 {
308   rtx insn, set, reg, p, link;
309   rtx copy = 0, first_copy = 0;
310   int num_insns = 0;
311   rtx exitcode
312     = NEXT_INSN (JUMP_LABEL (next_nonnote_insn_in_loop (loop_start)));
313   rtx lastexit;
314   int max_reg = max_reg_num ();
315   rtx *reg_map = 0;
316   rtx loop_pre_header_label;
317
318   /* Scan the exit code.  We do not perform this optimization if any insn:
319
320          is a CALL_INSN
321          is a CODE_LABEL
322          has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
323          is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
324
325      We also do not do this if we find an insn with ASM_OPERANDS.  While
326      this restriction should not be necessary, copying an insn with
327      ASM_OPERANDS can confuse asm_noperands in some cases.
328
329      Also, don't do this if the exit code is more than 20 insns.  */
330
331   for (insn = exitcode;
332        insn
333        && ! (GET_CODE (insn) == NOTE
334              && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
335        insn = NEXT_INSN (insn))
336     {
337       switch (GET_CODE (insn))
338         {
339         case CODE_LABEL:
340         case CALL_INSN:
341           return 0;
342         case NOTE:
343
344           if (optimize < 2
345               && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
346                   || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
347             /* If we were to duplicate this code, we would not move
348                the BLOCK notes, and so debugging the moved code would
349                be difficult.  Thus, we only move the code with -O2 or
350                higher.  */
351             return 0;
352
353           break;
354         case JUMP_INSN:
355         case INSN:
356           /* The code below would grossly mishandle REG_WAS_0 notes,
357              so get rid of them here.  */
358           while ((p = find_reg_note (insn, REG_WAS_0, NULL_RTX)) != 0)
359             remove_note (insn, p);
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_SCOPE (copy) = INSN_SCOPE (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_SCOPE (copy) = INSN_SCOPE (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.  Similary 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 /* An 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
1278   if (! x)
1279     return 0;
1280
1281   if (INSN_P (x))
1282     x = PATTERN (x);
1283
1284   return sets_cc0_p (x) == 1 && ! side_effects_p (x);
1285 }
1286
1287 /* Return 1 if X is an RTX that does nothing but set the condition codes
1288    and CLOBBER or USE registers.
1289    Return -1 if X does explicitly set the condition codes,
1290    but also does other things.  */
1291
1292 int
1293 sets_cc0_p (x)
1294      rtx x;
1295 {
1296
1297   if (! x)
1298     return 0;
1299
1300   if (INSN_P (x))
1301     x = PATTERN (x);
1302
1303   if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
1304     return 1;
1305   if (GET_CODE (x) == PARALLEL)
1306     {
1307       int i;
1308       int sets_cc0 = 0;
1309       int other_things = 0;
1310       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1311         {
1312           if (GET_CODE (XVECEXP (x, 0, i)) == SET
1313               && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
1314             sets_cc0 = 1;
1315           else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
1316             other_things = 1;
1317         }
1318       return ! sets_cc0 ? 0 : other_things ? -1 : 1;
1319     }
1320   return 0;
1321 }
1322 #endif
1323 \f
1324 /* Follow any unconditional jump at LABEL;
1325    return the ultimate label reached by any such chain of jumps.
1326    If LABEL is not followed by a jump, return LABEL.
1327    If the chain loops or we can't find end, return LABEL,
1328    since that tells caller to avoid changing the insn.
1329
1330    If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
1331    a USE or CLOBBER.  */
1332
1333 rtx
1334 follow_jumps (label)
1335      rtx label;
1336 {
1337   rtx insn;
1338   rtx next;
1339   rtx value = label;
1340   int depth;
1341
1342   for (depth = 0;
1343        (depth < 10
1344         && (insn = next_active_insn (value)) != 0
1345         && GET_CODE (insn) == JUMP_INSN
1346         && ((JUMP_LABEL (insn) != 0 && any_uncondjump_p (insn)
1347              && onlyjump_p (insn))
1348             || GET_CODE (PATTERN (insn)) == RETURN)
1349         && (next = NEXT_INSN (insn))
1350         && GET_CODE (next) == BARRIER);
1351        depth++)
1352     {
1353       /* Don't chain through the insn that jumps into a loop
1354          from outside the loop,
1355          since that would create multiple loop entry jumps
1356          and prevent loop optimization.  */
1357       rtx tem;
1358       if (!reload_completed)
1359         for (tem = value; tem != insn; tem = NEXT_INSN (tem))
1360           if (GET_CODE (tem) == NOTE
1361               && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
1362                   /* ??? Optional.  Disables some optimizations, but makes
1363                      gcov output more accurate with -O.  */
1364                   || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
1365             return value;
1366
1367       /* If we have found a cycle, make the insn jump to itself.  */
1368       if (JUMP_LABEL (insn) == label)
1369         return label;
1370
1371       tem = next_active_insn (JUMP_LABEL (insn));
1372       if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
1373                   || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
1374         break;
1375
1376       value = JUMP_LABEL (insn);
1377     }
1378   if (depth == 10)
1379     return label;
1380   return value;
1381 }
1382
1383 \f
1384 /* Find all CODE_LABELs referred to in X, and increment their use counts.
1385    If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
1386    in INSN, then store one of them in JUMP_LABEL (INSN).
1387    If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
1388    referenced in INSN, add a REG_LABEL note containing that label to INSN.
1389    Also, when there are consecutive labels, canonicalize on the last of them.
1390
1391    Note that two labels separated by a loop-beginning note
1392    must be kept distinct if we have not yet done loop-optimization,
1393    because the gap between them is where loop-optimize
1394    will want to move invariant code to.  CROSS_JUMP tells us
1395    that loop-optimization is done with.  */
1396
1397 void
1398 mark_jump_label (x, insn, in_mem)
1399      rtx x;
1400      rtx insn;
1401      int in_mem;
1402 {
1403   RTX_CODE code = GET_CODE (x);
1404   int i;
1405   const char *fmt;
1406
1407   switch (code)
1408     {
1409     case PC:
1410     case CC0:
1411     case REG:
1412     case CONST_INT:
1413     case CONST_DOUBLE:
1414     case CLOBBER:
1415     case CALL:
1416       return;
1417
1418     case MEM:
1419       in_mem = 1;
1420       break;
1421
1422     case SYMBOL_REF:
1423       if (!in_mem)
1424         return;
1425
1426       /* If this is a constant-pool reference, see if it is a label.  */
1427       if (CONSTANT_POOL_ADDRESS_P (x))
1428         mark_jump_label (get_pool_constant (x), insn, in_mem);
1429       break;
1430
1431     case LABEL_REF:
1432       {
1433         rtx label = XEXP (x, 0);
1434
1435         /* Ignore remaining references to unreachable labels that
1436            have been deleted.  */
1437         if (GET_CODE (label) == NOTE
1438             && NOTE_LINE_NUMBER (label) == NOTE_INSN_DELETED_LABEL)
1439           break;
1440
1441         if (GET_CODE (label) != CODE_LABEL)
1442           abort ();
1443
1444         /* Ignore references to labels of containing functions.  */
1445         if (LABEL_REF_NONLOCAL_P (x))
1446           break;
1447
1448         XEXP (x, 0) = label;
1449         if (! insn || ! INSN_DELETED_P (insn))
1450           ++LABEL_NUSES (label);
1451
1452         if (insn)
1453           {
1454             if (GET_CODE (insn) == JUMP_INSN)
1455               JUMP_LABEL (insn) = label;
1456             else
1457               {
1458                 /* Add a REG_LABEL note for LABEL unless there already
1459                    is one.  All uses of a label, except for labels
1460                    that are the targets of jumps, must have a
1461                    REG_LABEL note.  */
1462                 if (! find_reg_note (insn, REG_LABEL, label))
1463                   REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
1464                                                         REG_NOTES (insn));
1465               }
1466           }
1467         return;
1468       }
1469
1470   /* Do walk the labels in a vector, but not the first operand of an
1471      ADDR_DIFF_VEC.  Don't set the JUMP_LABEL of a vector.  */
1472     case ADDR_VEC:
1473     case ADDR_DIFF_VEC:
1474       if (! INSN_DELETED_P (insn))
1475         {
1476           int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
1477
1478           for (i = 0; i < XVECLEN (x, eltnum); i++)
1479             mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, in_mem);
1480         }
1481       return;
1482
1483     default:
1484       break;
1485     }
1486
1487   fmt = GET_RTX_FORMAT (code);
1488   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1489     {
1490       if (fmt[i] == 'e')
1491         mark_jump_label (XEXP (x, i), insn, in_mem);
1492       else if (fmt[i] == 'E')
1493         {
1494           int j;
1495           for (j = 0; j < XVECLEN (x, i); j++)
1496             mark_jump_label (XVECEXP (x, i, j), insn, in_mem);
1497         }
1498     }
1499 }
1500
1501 /* If all INSN does is set the pc, delete it,
1502    and delete the insn that set the condition codes for it
1503    if that's what the previous thing was.  */
1504
1505 void
1506 delete_jump (insn)
1507      rtx insn;
1508 {
1509   rtx set = single_set (insn);
1510
1511   if (set && GET_CODE (SET_DEST (set)) == PC)
1512     delete_computation (insn);
1513 }
1514
1515 /* Verify INSN is a BARRIER and delete it.  */
1516
1517 void
1518 delete_barrier (insn)
1519      rtx insn;
1520 {
1521   if (GET_CODE (insn) != BARRIER)
1522     abort ();
1523
1524   delete_insn (insn);
1525 }
1526
1527 /* Recursively delete prior insns that compute the value (used only by INSN
1528    which the caller is deleting) stored in the register mentioned by NOTE
1529    which is a REG_DEAD note associated with INSN.  */
1530
1531 static void
1532 delete_prior_computation (note, insn)
1533      rtx note;
1534      rtx insn;
1535 {
1536   rtx our_prev;
1537   rtx reg = XEXP (note, 0);
1538
1539   for (our_prev = prev_nonnote_insn (insn);
1540        our_prev && (GET_CODE (our_prev) == INSN
1541                     || GET_CODE (our_prev) == CALL_INSN);
1542        our_prev = prev_nonnote_insn (our_prev))
1543     {
1544       rtx pat = PATTERN (our_prev);
1545
1546       /* If we reach a CALL which is not calling a const function
1547          or the callee pops the arguments, then give up.  */
1548       if (GET_CODE (our_prev) == CALL_INSN
1549           && (! CONST_OR_PURE_CALL_P (our_prev)
1550               || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
1551         break;
1552
1553       /* If we reach a SEQUENCE, it is too complex to try to
1554          do anything with it, so give up.  We can be run during
1555          and after reorg, so SEQUENCE rtl can legitimately show
1556          up here.  */
1557       if (GET_CODE (pat) == SEQUENCE)
1558         break;
1559
1560       if (GET_CODE (pat) == USE
1561           && GET_CODE (XEXP (pat, 0)) == INSN)
1562         /* reorg creates USEs that look like this.  We leave them
1563            alone because reorg needs them for its own purposes.  */
1564         break;
1565
1566       if (reg_set_p (reg, pat))
1567         {
1568           if (side_effects_p (pat) && GET_CODE (our_prev) != CALL_INSN)
1569             break;
1570
1571           if (GET_CODE (pat) == PARALLEL)
1572             {
1573               /* If we find a SET of something else, we can't
1574                  delete the insn.  */
1575
1576               int i;
1577
1578               for (i = 0; i < XVECLEN (pat, 0); i++)
1579                 {
1580                   rtx part = XVECEXP (pat, 0, i);
1581
1582                   if (GET_CODE (part) == SET
1583                       && SET_DEST (part) != reg)
1584                     break;
1585                 }
1586
1587               if (i == XVECLEN (pat, 0))
1588                 delete_computation (our_prev);
1589             }
1590           else if (GET_CODE (pat) == SET
1591                    && GET_CODE (SET_DEST (pat)) == REG)
1592             {
1593               int dest_regno = REGNO (SET_DEST (pat));
1594               int dest_endregno
1595                 = (dest_regno
1596                    + (dest_regno < FIRST_PSEUDO_REGISTER
1597                       ? HARD_REGNO_NREGS (dest_regno,
1598                                           GET_MODE (SET_DEST (pat))) : 1));
1599               int regno = REGNO (reg);
1600               int endregno
1601                 = (regno
1602                    + (regno < FIRST_PSEUDO_REGISTER
1603                       ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1));
1604
1605               if (dest_regno >= regno
1606                   && dest_endregno <= endregno)
1607                 delete_computation (our_prev);
1608
1609               /* We may have a multi-word hard register and some, but not
1610                  all, of the words of the register are needed in subsequent
1611                  insns.  Write REG_UNUSED notes for those parts that were not
1612                  needed.  */
1613               else if (dest_regno <= regno
1614                        && dest_endregno >= endregno)
1615                 {
1616                   int i;
1617
1618                   REG_NOTES (our_prev)
1619                     = gen_rtx_EXPR_LIST (REG_UNUSED, reg,
1620                                          REG_NOTES (our_prev));
1621
1622                   for (i = dest_regno; i < dest_endregno; i++)
1623                     if (! find_regno_note (our_prev, REG_UNUSED, i))
1624                       break;
1625
1626                   if (i == dest_endregno)
1627                     delete_computation (our_prev);
1628                 }
1629             }
1630
1631           break;
1632         }
1633
1634       /* If PAT references the register that dies here, it is an
1635          additional use.  Hence any prior SET isn't dead.  However, this
1636          insn becomes the new place for the REG_DEAD note.  */
1637       if (reg_overlap_mentioned_p (reg, pat))
1638         {
1639           XEXP (note, 1) = REG_NOTES (our_prev);
1640           REG_NOTES (our_prev) = note;
1641           break;
1642         }
1643     }
1644 }
1645
1646 /* Delete INSN and recursively delete insns that compute values used only
1647    by INSN.  This uses the REG_DEAD notes computed during flow analysis.
1648    If we are running before flow.c, we need do nothing since flow.c will
1649    delete dead code.  We also can't know if the registers being used are
1650    dead or not at this point.
1651
1652    Otherwise, look at all our REG_DEAD notes.  If a previous insn does
1653    nothing other than set a register that dies in this insn, we can delete
1654    that insn as well.
1655
1656    On machines with CC0, if CC0 is used in this insn, we may be able to
1657    delete the insn that set it.  */
1658
1659 static void
1660 delete_computation (insn)
1661      rtx insn;
1662 {
1663   rtx note, next;
1664
1665 #ifdef HAVE_cc0
1666   if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
1667     {
1668       rtx prev = prev_nonnote_insn (insn);
1669       /* We assume that at this stage
1670          CC's are always set explicitly
1671          and always immediately before the jump that
1672          will use them.  So if the previous insn
1673          exists to set the CC's, delete it
1674          (unless it performs auto-increments, etc.).  */
1675       if (prev && GET_CODE (prev) == INSN
1676           && sets_cc0_p (PATTERN (prev)))
1677         {
1678           if (sets_cc0_p (PATTERN (prev)) > 0
1679               && ! side_effects_p (PATTERN (prev)))
1680             delete_computation (prev);
1681           else
1682             /* Otherwise, show that cc0 won't be used.  */
1683             REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
1684                                                   cc0_rtx, REG_NOTES (prev));
1685         }
1686     }
1687 #endif
1688
1689   for (note = REG_NOTES (insn); note; note = next)
1690     {
1691       next = XEXP (note, 1);
1692
1693       if (REG_NOTE_KIND (note) != REG_DEAD
1694           /* Verify that the REG_NOTE is legitimate.  */
1695           || GET_CODE (XEXP (note, 0)) != REG)
1696         continue;
1697
1698       delete_prior_computation (note, insn);
1699     }
1700
1701   delete_related_insns (insn);
1702 }
1703 \f
1704 /* Delete insn INSN from the chain of insns and update label ref counts
1705    and delete insns now unreachable.
1706
1707    Returns the first insn after INSN that was not deleted.
1708
1709    Usage of this instruction is deprecated.  Use delete_insn instead and
1710    subsequent cfg_cleanup pass to delete unreachable code if needed.  */
1711
1712 rtx
1713 delete_related_insns (insn)
1714      rtx insn;
1715 {
1716   int was_code_label = (GET_CODE (insn) == CODE_LABEL);
1717   rtx note;
1718   rtx next = NEXT_INSN (insn), prev = PREV_INSN (insn);
1719
1720   while (next && INSN_DELETED_P (next))
1721     next = NEXT_INSN (next);
1722
1723   /* This insn is already deleted => return first following nondeleted.  */
1724   if (INSN_DELETED_P (insn))
1725     return next;
1726
1727   delete_insn (insn);
1728
1729   /* If instruction is followed by a barrier,
1730      delete the barrier too.  */
1731
1732   if (next != 0 && GET_CODE (next) == BARRIER)
1733     delete_insn (next);
1734
1735   /* If deleting a jump, decrement the count of the label,
1736      and delete the label if it is now unused.  */
1737
1738   if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
1739     {
1740       rtx lab = JUMP_LABEL (insn), lab_next;
1741
1742       if (LABEL_NUSES (lab) == 0)
1743         {
1744           /* This can delete NEXT or PREV,
1745              either directly if NEXT is JUMP_LABEL (INSN),
1746              or indirectly through more levels of jumps.  */
1747           delete_related_insns (lab);
1748
1749           /* I feel a little doubtful about this loop,
1750              but I see no clean and sure alternative way
1751              to find the first insn after INSN that is not now deleted.
1752              I hope this works.  */
1753           while (next && INSN_DELETED_P (next))
1754             next = NEXT_INSN (next);
1755           return next;
1756         }
1757       else if ((lab_next = next_nonnote_insn (lab)) != NULL
1758                && GET_CODE (lab_next) == JUMP_INSN
1759                && (GET_CODE (PATTERN (lab_next)) == ADDR_VEC
1760                    || GET_CODE (PATTERN (lab_next)) == ADDR_DIFF_VEC))
1761         {
1762           /* If we're deleting the tablejump, delete the dispatch table.
1763              We may not be able to kill the label immediately preceding
1764              just yet, as it might be referenced in code leading up to
1765              the tablejump.  */
1766           delete_related_insns (lab_next);
1767         }
1768     }
1769
1770   /* Likewise if we're deleting a dispatch table.  */
1771
1772   if (GET_CODE (insn) == JUMP_INSN
1773       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
1774           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
1775     {
1776       rtx pat = PATTERN (insn);
1777       int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1778       int len = XVECLEN (pat, diff_vec_p);
1779
1780       for (i = 0; i < len; i++)
1781         if (LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
1782           delete_related_insns (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
1783       while (next && INSN_DELETED_P (next))
1784         next = NEXT_INSN (next);
1785       return next;
1786     }
1787
1788   /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note.  */
1789   if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
1790     for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1791       if (REG_NOTE_KIND (note) == REG_LABEL
1792           /* This could also be a NOTE_INSN_DELETED_LABEL note.  */
1793           && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
1794         if (LABEL_NUSES (XEXP (note, 0)) == 0)
1795           delete_related_insns (XEXP (note, 0));
1796
1797   while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
1798     prev = PREV_INSN (prev);
1799
1800   /* If INSN was a label and a dispatch table follows it,
1801      delete the dispatch table.  The tablejump must have gone already.
1802      It isn't useful to fall through into a table.  */
1803
1804   if (was_code_label
1805       && NEXT_INSN (insn) != 0
1806       && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
1807       && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
1808           || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
1809     next = delete_related_insns (NEXT_INSN (insn));
1810
1811   /* If INSN was a label, delete insns following it if now unreachable.  */
1812
1813   if (was_code_label && prev && GET_CODE (prev) == BARRIER)
1814     {
1815       RTX_CODE code;
1816       while (next != 0
1817              && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
1818                  || code == NOTE || code == BARRIER
1819                  || (code == CODE_LABEL && INSN_DELETED_P (next))))
1820         {
1821           if (code == NOTE
1822               && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
1823             next = NEXT_INSN (next);
1824           /* Keep going past other deleted labels to delete what follows.  */
1825           else if (code == CODE_LABEL && INSN_DELETED_P (next))
1826             next = NEXT_INSN (next);
1827           else
1828             /* Note: if this deletes a jump, it can cause more
1829                deletion of unreachable code, after a different label.
1830                As long as the value from this recursive call is correct,
1831                this invocation functions correctly.  */
1832             next = delete_related_insns (next);
1833         }
1834     }
1835
1836   return next;
1837 }
1838
1839 /* Advance from INSN till reaching something not deleted
1840    then return that.  May return INSN itself.  */
1841
1842 rtx
1843 next_nondeleted_insn (insn)
1844      rtx insn;
1845 {
1846   while (INSN_DELETED_P (insn))
1847     insn = NEXT_INSN (insn);
1848   return insn;
1849 }
1850 \f
1851 /* Delete a range of insns from FROM to TO, inclusive.
1852    This is for the sake of peephole optimization, so assume
1853    that whatever these insns do will still be done by a new
1854    peephole insn that will replace them.  */
1855
1856 void
1857 delete_for_peephole (from, to)
1858      rtx from, to;
1859 {
1860   rtx insn = from;
1861
1862   while (1)
1863     {
1864       rtx next = NEXT_INSN (insn);
1865       rtx prev = PREV_INSN (insn);
1866
1867       if (GET_CODE (insn) != NOTE)
1868         {
1869           INSN_DELETED_P (insn) = 1;
1870
1871           /* Patch this insn out of the chain.  */
1872           /* We don't do this all at once, because we
1873              must preserve all NOTEs.  */
1874           if (prev)
1875             NEXT_INSN (prev) = next;
1876
1877           if (next)
1878             PREV_INSN (next) = prev;
1879         }
1880
1881       if (insn == to)
1882         break;
1883       insn = next;
1884     }
1885
1886   /* Note that if TO is an unconditional jump
1887      we *do not* delete the BARRIER that follows,
1888      since the peephole that replaces this sequence
1889      is also an unconditional jump in that case.  */
1890 }
1891 \f
1892 /* We have determined that AVOIDED_INSN is never reached, and are
1893    about to delete it.  If the insn chain between AVOIDED_INSN and
1894    FINISH contains more than one line from the current function, and
1895    contains at least one operation, print a warning if the user asked
1896    for it.  If FINISH is NULL, look between AVOIDED_INSN and a LABEL.
1897
1898    CSE and inlining can duplicate insns, so it's possible to get
1899    spurious warnings from this.  */
1900
1901 void
1902 never_reached_warning (avoided_insn, finish)
1903      rtx avoided_insn, finish;
1904 {
1905   rtx insn;
1906   rtx a_line_note = NULL;
1907   int two_avoided_lines = 0, contains_insn = 0, reached_end = 0;
1908
1909   if (!warn_notreached)
1910     return;
1911
1912   /* Scan forwards, looking at LINE_NUMBER notes, until we hit a LABEL
1913      in case FINISH is NULL, otherwise until we run out of insns.  */
1914
1915   for (insn = avoided_insn; insn != NULL; insn = NEXT_INSN (insn))
1916     {
1917       if ((finish == NULL && GET_CODE (insn) == CODE_LABEL)
1918           || GET_CODE (insn) == BARRIER)
1919         break;
1920
1921       if (GET_CODE (insn) == NOTE               /* A line number note?  */
1922           && NOTE_LINE_NUMBER (insn) >= 0)
1923         {
1924           if (a_line_note == NULL)
1925             a_line_note = insn;
1926           else
1927             two_avoided_lines |= (NOTE_LINE_NUMBER (a_line_note)
1928                                   != NOTE_LINE_NUMBER (insn));
1929         }
1930       else if (INSN_P (insn))
1931         {
1932           if (reached_end || a_line_note == NULL)
1933             break;
1934           contains_insn = 1;
1935         }
1936
1937       if (insn == finish)
1938         reached_end = 1;
1939     }
1940   if (two_avoided_lines && contains_insn)
1941     warning_with_file_and_line (NOTE_SOURCE_FILE (a_line_note),
1942                                 NOTE_LINE_NUMBER (a_line_note),
1943                                 "will never be executed");
1944 }
1945 \f
1946 /* Throughout LOC, redirect OLABEL to NLABEL.  Treat null OLABEL or
1947    NLABEL as a return.  Accrue modifications into the change group.  */
1948
1949 static void
1950 redirect_exp_1 (loc, olabel, nlabel, insn)
1951      rtx *loc;
1952      rtx olabel, nlabel;
1953      rtx insn;
1954 {
1955   rtx x = *loc;
1956   RTX_CODE code = GET_CODE (x);
1957   int i;
1958   const char *fmt;
1959
1960   if (code == LABEL_REF)
1961     {
1962       if (XEXP (x, 0) == olabel)
1963         {
1964           rtx n;
1965           if (nlabel)
1966             n = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1967           else
1968             n = gen_rtx_RETURN (VOIDmode);
1969
1970           validate_change (insn, loc, n, 1);
1971           return;
1972         }
1973     }
1974   else if (code == RETURN && olabel == 0)
1975     {
1976       x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1977       if (loc == &PATTERN (insn))
1978         x = gen_rtx_SET (VOIDmode, pc_rtx, x);
1979       validate_change (insn, loc, x, 1);
1980       return;
1981     }
1982
1983   if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
1984       && GET_CODE (SET_SRC (x)) == LABEL_REF
1985       && XEXP (SET_SRC (x), 0) == olabel)
1986     {
1987       validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 1);
1988       return;
1989     }
1990
1991   fmt = GET_RTX_FORMAT (code);
1992   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1993     {
1994       if (fmt[i] == 'e')
1995         redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
1996       else if (fmt[i] == 'E')
1997         {
1998           int j;
1999           for (j = 0; j < XVECLEN (x, i); j++)
2000             redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
2001         }
2002     }
2003 }
2004
2005 /* Similar, but apply the change group and report success or failure.  */
2006
2007 static int
2008 redirect_exp (olabel, nlabel, insn)
2009      rtx olabel, nlabel;
2010      rtx insn;
2011 {
2012   rtx *loc;
2013
2014   if (GET_CODE (PATTERN (insn)) == PARALLEL)
2015     loc = &XVECEXP (PATTERN (insn), 0, 0);
2016   else
2017     loc = &PATTERN (insn);
2018
2019   redirect_exp_1 (loc, olabel, nlabel, insn);
2020   if (num_validated_changes () == 0)
2021     return 0;
2022
2023   return apply_change_group ();
2024 }
2025
2026 /* Make JUMP go to NLABEL instead of where it jumps now.  Accrue
2027    the modifications into the change group.  Return false if we did
2028    not see how to do that.  */
2029
2030 int
2031 redirect_jump_1 (jump, nlabel)
2032      rtx jump, nlabel;
2033 {
2034   int ochanges = num_validated_changes ();
2035   rtx *loc;
2036
2037   if (GET_CODE (PATTERN (jump)) == PARALLEL)
2038     loc = &XVECEXP (PATTERN (jump), 0, 0);
2039   else
2040     loc = &PATTERN (jump);
2041
2042   redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
2043   return num_validated_changes () > ochanges;
2044 }
2045
2046 /* Make JUMP go to NLABEL instead of where it jumps now.  If the old
2047    jump target label is unused as a result, it and the code following
2048    it may be deleted.
2049
2050    If NLABEL is zero, we are to turn the jump into a (possibly conditional)
2051    RETURN insn.
2052
2053    The return value will be 1 if the change was made, 0 if it wasn't
2054    (this can only occur for NLABEL == 0).  */
2055
2056 int
2057 redirect_jump (jump, nlabel, delete_unused)
2058      rtx jump, nlabel;
2059      int delete_unused;
2060 {
2061   rtx olabel = JUMP_LABEL (jump);
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   /* If we're eliding the jump over exception cleanups at the end of a
2074      function, move the function end note so that -Wreturn-type works.  */
2075   if (olabel && nlabel
2076       && NEXT_INSN (olabel)
2077       && GET_CODE (NEXT_INSN (olabel)) == NOTE
2078       && NOTE_LINE_NUMBER (NEXT_INSN (olabel)) == NOTE_INSN_FUNCTION_END)
2079     emit_note_after (NOTE_INSN_FUNCTION_END, nlabel);
2080
2081   if (olabel && --LABEL_NUSES (olabel) == 0 && delete_unused
2082       /* Undefined labels will remain outside the insn stream.  */
2083       && INSN_UID (olabel))
2084     delete_related_insns (olabel);
2085
2086   return 1;
2087 }
2088
2089 /* Invert the jump condition of rtx X contained in jump insn, INSN.
2090    Accrue the modifications into the change group.  */
2091
2092 static void
2093 invert_exp_1 (insn)
2094      rtx insn;
2095 {
2096   RTX_CODE code;
2097   rtx x = pc_set (insn);
2098
2099   if (!x)
2100     abort ();
2101   x = SET_SRC (x);
2102
2103   code = GET_CODE (x);
2104
2105   if (code == IF_THEN_ELSE)
2106     {
2107       rtx comp = XEXP (x, 0);
2108       rtx tem;
2109       enum rtx_code reversed_code;
2110
2111       /* We can do this in two ways:  The preferable way, which can only
2112          be done if this is not an integer comparison, is to reverse
2113          the comparison code.  Otherwise, swap the THEN-part and ELSE-part
2114          of the IF_THEN_ELSE.  If we can't do either, fail.  */
2115
2116       reversed_code = reversed_comparison_code (comp, insn);
2117
2118       if (reversed_code != UNKNOWN)
2119         {
2120           validate_change (insn, &XEXP (x, 0),
2121                            gen_rtx_fmt_ee (reversed_code,
2122                                            GET_MODE (comp), XEXP (comp, 0),
2123                                            XEXP (comp, 1)),
2124                            1);
2125           return;
2126         }
2127
2128       tem = XEXP (x, 1);
2129       validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
2130       validate_change (insn, &XEXP (x, 2), tem, 1);
2131     }
2132   else
2133     abort ();
2134 }
2135
2136 /* Invert the jump condition of conditional jump insn, INSN.
2137
2138    Return 1 if we can do so, 0 if we cannot find a way to do so that
2139    matches a pattern.  */
2140
2141 static int
2142 invert_exp (insn)
2143      rtx insn;
2144 {
2145   invert_exp_1 (insn);
2146   if (num_validated_changes () == 0)
2147     return 0;
2148
2149   return apply_change_group ();
2150 }
2151
2152 /* Invert the condition of the jump JUMP, and make it jump to label
2153    NLABEL instead of where it jumps now.  Accrue changes into the
2154    change group.  Return false if we didn't see how to perform the
2155    inversion and redirection.  */
2156
2157 int
2158 invert_jump_1 (jump, nlabel)
2159      rtx jump, nlabel;
2160 {
2161   int ochanges;
2162
2163   ochanges = num_validated_changes ();
2164   invert_exp_1 (jump);
2165   if (num_validated_changes () == ochanges)
2166     return 0;
2167
2168   return redirect_jump_1 (jump, nlabel);
2169 }
2170
2171 /* Invert the condition of the jump JUMP, and make it jump to label
2172    NLABEL instead of where it jumps now.  Return true if successful.  */
2173
2174 int
2175 invert_jump (jump, nlabel, delete_unused)
2176      rtx jump, nlabel;
2177      int delete_unused;
2178 {
2179   /* We have to either invert the condition and change the label or
2180      do neither.  Either operation could fail.  We first try to invert
2181      the jump. If that succeeds, we try changing the label.  If that fails,
2182      we invert the jump back to what it was.  */
2183
2184   if (! invert_exp (jump))
2185     return 0;
2186
2187   if (redirect_jump (jump, nlabel, delete_unused))
2188     {
2189       invert_br_probabilities (jump);
2190
2191       return 1;
2192     }
2193
2194   if (! invert_exp (jump))
2195     /* This should just be putting it back the way it was.  */
2196     abort ();
2197
2198   return 0;
2199 }
2200
2201 \f
2202 /* Like rtx_equal_p except that it considers two REGs as equal
2203    if they renumber to the same value and considers two commutative
2204    operations to be the same if the order of the operands has been
2205    reversed.
2206
2207    ??? Addition is not commutative on the PA due to the weird implicit
2208    space register selection rules for memory addresses.  Therefore, we
2209    don't consider a + b == b + a.
2210
2211    We could/should make this test a little tighter.  Possibly only
2212    disabling it on the PA via some backend macro or only disabling this
2213    case when the PLUS is inside a MEM.  */
2214
2215 int
2216 rtx_renumbered_equal_p (x, y)
2217      rtx x, y;
2218 {
2219   int i;
2220   RTX_CODE code = GET_CODE (x);
2221   const char *fmt;
2222
2223   if (x == y)
2224     return 1;
2225
2226   if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
2227       && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
2228                                   && GET_CODE (SUBREG_REG (y)) == REG)))
2229     {
2230       int reg_x = -1, reg_y = -1;
2231       int byte_x = 0, byte_y = 0;
2232
2233       if (GET_MODE (x) != GET_MODE (y))
2234         return 0;
2235
2236       /* If we haven't done any renumbering, don't
2237          make any assumptions.  */
2238       if (reg_renumber == 0)
2239         return rtx_equal_p (x, y);
2240
2241       if (code == SUBREG)
2242         {
2243           reg_x = REGNO (SUBREG_REG (x));
2244           byte_x = SUBREG_BYTE (x);
2245
2246           if (reg_renumber[reg_x] >= 0)
2247             {
2248               reg_x = subreg_regno_offset (reg_renumber[reg_x],
2249                                            GET_MODE (SUBREG_REG (x)),
2250                                            byte_x,
2251                                            GET_MODE (x));
2252               byte_x = 0;
2253             }
2254         }
2255       else
2256         {
2257           reg_x = REGNO (x);
2258           if (reg_renumber[reg_x] >= 0)
2259             reg_x = reg_renumber[reg_x];
2260         }
2261
2262       if (GET_CODE (y) == SUBREG)
2263         {
2264           reg_y = REGNO (SUBREG_REG (y));
2265           byte_y = SUBREG_BYTE (y);
2266
2267           if (reg_renumber[reg_y] >= 0)
2268             {
2269               reg_y = subreg_regno_offset (reg_renumber[reg_y],
2270                                            GET_MODE (SUBREG_REG (y)),
2271                                            byte_y,
2272                                            GET_MODE (y));
2273               byte_y = 0;
2274             }
2275         }
2276       else
2277         {
2278           reg_y = REGNO (y);
2279           if (reg_renumber[reg_y] >= 0)
2280             reg_y = reg_renumber[reg_y];
2281         }
2282
2283       return reg_x >= 0 && reg_x == reg_y && byte_x == byte_y;
2284     }
2285
2286   /* Now we have disposed of all the cases
2287      in which different rtx codes can match.  */
2288   if (code != GET_CODE (y))
2289     return 0;
2290
2291   switch (code)
2292     {
2293     case PC:
2294     case CC0:
2295     case ADDR_VEC:
2296     case ADDR_DIFF_VEC:
2297       return 0;
2298
2299     case CONST_INT:
2300       return INTVAL (x) == INTVAL (y);
2301
2302     case LABEL_REF:
2303       /* We can't assume nonlocal labels have their following insns yet.  */
2304       if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
2305         return XEXP (x, 0) == XEXP (y, 0);
2306
2307       /* Two label-refs are equivalent if they point at labels
2308          in the same position in the instruction stream.  */
2309       return (next_real_insn (XEXP (x, 0))
2310               == next_real_insn (XEXP (y, 0)));
2311
2312     case SYMBOL_REF:
2313       return XSTR (x, 0) == XSTR (y, 0);
2314
2315     case CODE_LABEL:
2316       /* If we didn't match EQ equality above, they aren't the same.  */
2317       return 0;
2318
2319     default:
2320       break;
2321     }
2322
2323   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
2324
2325   if (GET_MODE (x) != GET_MODE (y))
2326     return 0;
2327
2328   /* For commutative operations, the RTX match if the operand match in any
2329      order.  Also handle the simple binary and unary cases without a loop.
2330
2331      ??? Don't consider PLUS a commutative operator; see comments above.  */
2332   if ((code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
2333       && code != PLUS)
2334     return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
2335              && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
2336             || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
2337                 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
2338   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
2339     return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
2340             && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
2341   else if (GET_RTX_CLASS (code) == '1')
2342     return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
2343
2344   /* Compare the elements.  If any pair of corresponding elements
2345      fail to match, return 0 for the whole things.  */
2346
2347   fmt = GET_RTX_FORMAT (code);
2348   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2349     {
2350       int j;
2351       switch (fmt[i])
2352         {
2353         case 'w':
2354           if (XWINT (x, i) != XWINT (y, i))
2355             return 0;
2356           break;
2357
2358         case 'i':
2359           if (XINT (x, i) != XINT (y, i))
2360             return 0;
2361           break;
2362
2363         case 't':
2364           if (XTREE (x, i) != XTREE (y, i))
2365             return 0;
2366           break;
2367
2368         case 's':
2369           if (strcmp (XSTR (x, i), XSTR (y, i)))
2370             return 0;
2371           break;
2372
2373         case 'e':
2374           if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
2375             return 0;
2376           break;
2377
2378         case 'u':
2379           if (XEXP (x, i) != XEXP (y, i))
2380             return 0;
2381           /* fall through.  */
2382         case '0':
2383           break;
2384
2385         case 'E':
2386           if (XVECLEN (x, i) != XVECLEN (y, i))
2387             return 0;
2388           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2389             if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
2390               return 0;
2391           break;
2392
2393         default:
2394           abort ();
2395         }
2396     }
2397   return 1;
2398 }
2399 \f
2400 /* If X is a hard register or equivalent to one or a subregister of one,
2401    return the hard register number.  If X is a pseudo register that was not
2402    assigned a hard register, return the pseudo register number.  Otherwise,
2403    return -1.  Any rtx is valid for X.  */
2404
2405 int
2406 true_regnum (x)
2407      rtx x;
2408 {
2409   if (GET_CODE (x) == REG)
2410     {
2411       if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
2412         return reg_renumber[REGNO (x)];
2413       return REGNO (x);
2414     }
2415   if (GET_CODE (x) == SUBREG)
2416     {
2417       int base = true_regnum (SUBREG_REG (x));
2418       if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
2419         return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
2420                                            GET_MODE (SUBREG_REG (x)),
2421                                            SUBREG_BYTE (x), GET_MODE (x));
2422     }
2423   return -1;
2424 }
2425
2426 /* Return regno of the register REG and handle subregs too.  */
2427 unsigned int
2428 reg_or_subregno (reg)
2429      rtx reg;
2430 {
2431   if (REG_P (reg))
2432     return REGNO (reg);
2433   if (GET_CODE (reg) == SUBREG)
2434     return REGNO (SUBREG_REG (reg));
2435   abort ();
2436 }