OSDN Git Service

4483c720fea32bbc13f0fd6d16d4f5fdeda0e7c3
[pf3gnuchains/gcc-fork.git] / gcc / jump.c
1 /* Optimize jump instructions, for GNU compiler.
2    Copyright (C) 1987, 88, 89, 91-94, 1995 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* This is the jump-optimization pass of the compiler.
22    It is run two or three times: once before cse, sometimes once after cse,
23    and once after reload (before final).
24
25    jump_optimize deletes unreachable code and labels that are not used.
26    It also deletes jumps that jump to the following insn,
27    and simplifies jumps around unconditional jumps and jumps
28    to unconditional jumps.
29
30    Each CODE_LABEL has a count of the times it is used
31    stored in the LABEL_NUSES internal field, and each JUMP_INSN
32    has one label that it refers to stored in the
33    JUMP_LABEL internal field.  With this we can detect labels that
34    become unused because of the deletion of all the jumps that
35    formerly used them.  The JUMP_LABEL info is sometimes looked
36    at by later passes.
37
38    Optionally, cross-jumping can be done.  Currently it is done
39    only the last time (when after reload and before final).
40    In fact, the code for cross-jumping now assumes that register
41    allocation has been done, since it uses `rtx_renumbered_equal_p'.
42
43    Jump optimization is done after cse when cse's constant-propagation
44    causes jumps to become unconditional or to be deleted.
45
46    Unreachable loops are not detected here, because the labels
47    have references and the insns appear reachable from the labels.
48    find_basic_blocks in flow.c finds and deletes such loops.
49
50    The subroutines delete_insn, redirect_jump, and invert_jump are used
51    from other passes as well.  */
52
53 #include "config.h"
54 #include "rtl.h"
55 #include "flags.h"
56 #include "hard-reg-set.h"
57 #include "regs.h"
58 #include "insn-config.h"
59 #include "insn-flags.h"
60 #include "expr.h"
61 #include "real.h"
62
63 /* ??? Eventually must record somehow the labels used by jumps
64    from nested functions.  */
65 /* Pre-record the next or previous real insn for each label?
66    No, this pass is very fast anyway.  */
67 /* Condense consecutive labels?
68    This would make life analysis faster, maybe.  */
69 /* Optimize jump y; x: ... y: jumpif... x?
70    Don't know if it is worth bothering with.  */
71 /* Optimize two cases of conditional jump to conditional jump?
72    This can never delete any instruction or make anything dead,
73    or even change what is live at any point.
74    So perhaps let combiner do it.  */
75
76 /* Vector indexed by uid.
77    For each CODE_LABEL, index by its uid to get first unconditional jump
78    that jumps to the label.
79    For each JUMP_INSN, index by its uid to get the next unconditional jump
80    that jumps to the same label.
81    Element 0 is the start of a chain of all return insns.
82    (It is safe to use element 0 because insn uid 0 is not used.  */
83
84 static rtx *jump_chain;
85
86 /* List of labels referred to from initializers.
87    These can never be deleted.  */
88 rtx forced_labels;
89
90 /* Maximum index in jump_chain.  */
91
92 static int max_jump_chain;
93
94 /* Set nonzero by jump_optimize if control can fall through
95    to the end of the function.  */
96 int can_reach_end;
97
98 /* Indicates whether death notes are significant in cross jump analysis.
99    Normally they are not significant, because of A and B jump to C,
100    and R dies in A, it must die in B.  But this might not be true after
101    stack register conversion, and we must compare death notes in that
102    case. */
103
104 static int cross_jump_death_matters = 0;
105
106 static int duplicate_loop_exit_test     PROTO((rtx));
107 static void find_cross_jump             PROTO((rtx, rtx, int, rtx *, rtx *));
108 static void do_cross_jump               PROTO((rtx, rtx, rtx));
109 static int jump_back_p                  PROTO((rtx, rtx));
110 static int tension_vector_labels        PROTO((rtx, int));
111 static void mark_jump_label             PROTO((rtx, rtx, int));
112 static void delete_computation          PROTO((rtx));
113 static void delete_from_jump_chain      PROTO((rtx));
114 static int delete_labelref_insn         PROTO((rtx, rtx, int));
115 static void redirect_tablejump          PROTO((rtx, rtx));
116 \f
117 /* Delete no-op jumps and optimize jumps to jumps
118    and jumps around jumps.
119    Delete unused labels and unreachable code.
120
121    If CROSS_JUMP is 1, detect matching code
122    before a jump and its destination and unify them.
123    If CROSS_JUMP is 2, do cross-jumping, but pay attention to death notes.
124
125    If NOOP_MOVES is nonzero, delete no-op move insns.
126
127    If AFTER_REGSCAN is nonzero, then this jump pass is being run immediately
128    after regscan, and it is safe to use regno_first_uid and regno_last_uid.
129
130    If `optimize' is zero, don't change any code,
131    just determine whether control drops off the end of the function.
132    This case occurs when we have -W and not -O.
133    It works because `delete_insn' checks the value of `optimize'
134    and refrains from actually deleting when that is 0.  */
135
136 void
137 jump_optimize (f, cross_jump, noop_moves, after_regscan)
138      rtx f;
139      int cross_jump;
140      int noop_moves;
141      int after_regscan;
142 {
143   register rtx insn, next, note;
144   int changed;
145   int first = 1;
146   int max_uid = 0;
147   rtx last_insn;
148
149   cross_jump_death_matters = (cross_jump == 2);
150
151   /* Initialize LABEL_NUSES and JUMP_LABEL fields.  Delete any REG_LABEL
152      notes whose labels don't occur in the insn any more.  */
153
154   for (insn = f; insn; insn = NEXT_INSN (insn))
155     {
156       if (GET_CODE (insn) == CODE_LABEL)
157         LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
158       else if (GET_CODE (insn) == JUMP_INSN)
159         JUMP_LABEL (insn) = 0;
160       else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
161         for (note = REG_NOTES (insn); note; note = next)
162           {
163             next = XEXP (note, 1);
164             if (REG_NOTE_KIND (note) == REG_LABEL
165                 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
166               remove_note (insn, note);
167           }
168
169       if (INSN_UID (insn) > max_uid)
170         max_uid = INSN_UID (insn);
171     }
172
173   max_uid++;
174
175   /* Delete insns following barriers, up to next label.  */
176
177   for (insn = f; insn;)
178     {
179       if (GET_CODE (insn) == BARRIER)
180         {
181           insn = NEXT_INSN (insn);
182           while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
183             {
184               if (GET_CODE (insn) == NOTE
185                   && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
186                 insn = NEXT_INSN (insn);
187               else
188                 insn = delete_insn (insn);
189             }
190           /* INSN is now the code_label.  */
191         }
192       else
193         insn = NEXT_INSN (insn);
194     }
195
196   /* Leave some extra room for labels and duplicate exit test insns
197      we make.  */
198   max_jump_chain = max_uid * 14 / 10;
199   jump_chain = (rtx *) alloca (max_jump_chain * sizeof (rtx));
200   bzero ((char *) jump_chain, max_jump_chain * sizeof (rtx));
201
202   /* Mark the label each jump jumps to.
203      Combine consecutive labels, and count uses of labels.
204
205      For each label, make a chain (using `jump_chain')
206      of all the *unconditional* jumps that jump to it;
207      also make a chain of all returns.  */
208
209   for (insn = f; insn; insn = NEXT_INSN (insn))
210     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
211         && ! INSN_DELETED_P (insn))
212       {
213         mark_jump_label (PATTERN (insn), insn, cross_jump);
214         if (GET_CODE (insn) == JUMP_INSN)
215           {
216             if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
217               {
218                 jump_chain[INSN_UID (insn)]
219                   = jump_chain[INSN_UID (JUMP_LABEL (insn))];
220                 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
221               }
222             if (GET_CODE (PATTERN (insn)) == RETURN)
223               {
224                 jump_chain[INSN_UID (insn)] = jump_chain[0];
225                 jump_chain[0] = insn;
226               }
227           }
228       }
229
230   /* Keep track of labels used from static data;
231      they cannot ever be deleted.  */
232
233   for (insn = forced_labels; insn; insn = XEXP (insn, 1))
234     LABEL_NUSES (XEXP (insn, 0))++;
235
236   /* Delete all labels already not referenced.
237      Also find the last insn.  */
238
239   last_insn = 0;
240   for (insn = f; insn; )
241     {
242       if (GET_CODE (insn) == CODE_LABEL && LABEL_NUSES (insn) == 0)
243         insn = delete_insn (insn);
244       else
245         {
246           last_insn = insn;
247           insn = NEXT_INSN (insn);
248         }
249     }
250
251   if (!optimize)
252     {
253       /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
254          If so record that this function can drop off the end.  */
255
256       insn = last_insn;
257       {
258         int n_labels = 1;
259         while (insn
260                /* One label can follow the end-note: the return label.  */
261                && ((GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
262                    /* Ordinary insns can follow it if returning a structure.  */
263                    || GET_CODE (insn) == INSN
264                    /* If machine uses explicit RETURN insns, no epilogue,
265                       then one of them follows the note.  */
266                    || (GET_CODE (insn) == JUMP_INSN
267                        && GET_CODE (PATTERN (insn)) == RETURN)
268                    /* A barrier can follow the return insn.  */
269                    || GET_CODE (insn) == BARRIER
270                    /* Other kinds of notes can follow also.  */
271                    || (GET_CODE (insn) == NOTE
272                        && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)))
273           insn = PREV_INSN (insn);
274       }
275
276       /* Report if control can fall through at the end of the function.  */
277       if (insn && GET_CODE (insn) == NOTE
278           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END
279           && ! INSN_DELETED_P (insn))
280         can_reach_end = 1;
281
282       /* Zero the "deleted" flag of all the "deleted" insns.  */
283       for (insn = f; insn; insn = NEXT_INSN (insn))
284         INSN_DELETED_P (insn) = 0;
285       return;
286     }
287
288 #ifdef HAVE_return
289   if (HAVE_return)
290     {
291       /* If we fall through to the epilogue, see if we can insert a RETURN insn
292          in front of it.  If the machine allows it at this point (we might be
293          after reload for a leaf routine), it will improve optimization for it
294          to be there.  */
295       insn = get_last_insn ();
296       while (insn && GET_CODE (insn) == NOTE)
297         insn = PREV_INSN (insn);
298
299       if (insn && GET_CODE (insn) != BARRIER)
300         {
301           emit_jump_insn (gen_return ());
302           emit_barrier ();
303         }
304     }
305 #endif
306
307   if (noop_moves)
308     for (insn = f; insn; )
309       {
310         next = NEXT_INSN (insn);
311
312         if (GET_CODE (insn) == INSN)
313           {
314             register rtx body = PATTERN (insn);
315
316 /* Combine stack_adjusts with following push_insns.  */
317 #ifdef PUSH_ROUNDING
318             if (GET_CODE (body) == SET
319                 && SET_DEST (body) == stack_pointer_rtx
320                 && GET_CODE (SET_SRC (body)) == PLUS
321                 && XEXP (SET_SRC (body), 0) == stack_pointer_rtx
322                 && GET_CODE (XEXP (SET_SRC (body), 1)) == CONST_INT
323                 && INTVAL (XEXP (SET_SRC (body), 1)) > 0)
324               {
325                 rtx p;
326                 rtx stack_adjust_insn = insn;
327                 int stack_adjust_amount = INTVAL (XEXP (SET_SRC (body), 1));
328                 int total_pushed = 0;
329                 int pushes = 0;
330
331                 /* Find all successive push insns.  */
332                 p = insn;
333                 /* Don't convert more than three pushes;
334                    that starts adding too many displaced addresses
335                    and the whole thing starts becoming a losing
336                    proposition.  */
337                 while (pushes < 3)
338                   {
339                     rtx pbody, dest;
340                     p = next_nonnote_insn (p);
341                     if (p == 0 || GET_CODE (p) != INSN)
342                       break;
343                     pbody = PATTERN (p);
344                     if (GET_CODE (pbody) != SET)
345                       break;
346                     dest = SET_DEST (pbody);
347                     /* Allow a no-op move between the adjust and the push.  */
348                     if (GET_CODE (dest) == REG
349                         && GET_CODE (SET_SRC (pbody)) == REG
350                         && REGNO (dest) == REGNO (SET_SRC (pbody)))
351                       continue;
352                     if (! (GET_CODE (dest) == MEM
353                            && GET_CODE (XEXP (dest, 0)) == POST_INC
354                            && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
355                       break;
356                     pushes++;
357                     if (total_pushed + GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)))
358                         > stack_adjust_amount)
359                       break;
360                     total_pushed += GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
361                   }
362
363                 /* Discard the amount pushed from the stack adjust;
364                    maybe eliminate it entirely.  */
365                 if (total_pushed >= stack_adjust_amount)
366                   {
367                     delete_computation (stack_adjust_insn);
368                     total_pushed = stack_adjust_amount;
369                   }
370                 else
371                   XEXP (SET_SRC (PATTERN (stack_adjust_insn)), 1)
372                     = GEN_INT (stack_adjust_amount - total_pushed);
373
374                 /* Change the appropriate push insns to ordinary stores.  */
375                 p = insn;
376                 while (total_pushed > 0)
377                   {
378                     rtx pbody, dest;
379                     p = next_nonnote_insn (p);
380                     if (GET_CODE (p) != INSN)
381                       break;
382                     pbody = PATTERN (p);
383                     if (GET_CODE (pbody) == SET)
384                       break;
385                     dest = SET_DEST (pbody);
386                     if (! (GET_CODE (dest) == MEM
387                            && GET_CODE (XEXP (dest, 0)) == POST_INC
388                            && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
389                       break;
390                     total_pushed -= GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
391                     /* If this push doesn't fully fit in the space
392                        of the stack adjust that we deleted,
393                        make another stack adjust here for what we
394                        didn't use up.  There should be peepholes
395                        to recognize the resulting sequence of insns.  */
396                     if (total_pushed < 0)
397                       {
398                         emit_insn_before (gen_add2_insn (stack_pointer_rtx,
399                                                          GEN_INT (- total_pushed)),
400                                           p);
401                         break;
402                       }
403                     XEXP (dest, 0)
404                       = plus_constant (stack_pointer_rtx, total_pushed);
405                   }
406               }
407 #endif
408
409             /* Detect and delete no-op move instructions
410                resulting from not allocating a parameter in a register.  */
411
412             if (GET_CODE (body) == SET
413                 && (SET_DEST (body) == SET_SRC (body)
414                     || (GET_CODE (SET_DEST (body)) == MEM
415                         && GET_CODE (SET_SRC (body)) == MEM
416                         && rtx_equal_p (SET_SRC (body), SET_DEST (body))))
417                 && ! (GET_CODE (SET_DEST (body)) == MEM
418                       && MEM_VOLATILE_P (SET_DEST (body)))
419                 && ! (GET_CODE (SET_SRC (body)) == MEM
420                       && MEM_VOLATILE_P (SET_SRC (body))))
421               delete_computation (insn);
422
423             /* Detect and ignore no-op move instructions
424                resulting from smart or fortuitous register allocation.  */
425
426             else if (GET_CODE (body) == SET)
427               {
428                 int sreg = true_regnum (SET_SRC (body));
429                 int dreg = true_regnum (SET_DEST (body));
430
431                 if (sreg == dreg && sreg >= 0)
432                   delete_insn (insn);
433                 else if (sreg >= 0 && dreg >= 0)
434                   {
435                     rtx trial;
436                     rtx tem = find_equiv_reg (NULL_RTX, insn, 0,
437                                               sreg, NULL_PTR, dreg,
438                                               GET_MODE (SET_SRC (body)));
439
440 #ifdef PRESERVE_DEATH_INFO_REGNO_P
441                     /* Deleting insn could lose a death-note for SREG or DREG
442                        so don't do it if final needs accurate death-notes.  */
443                     if (! PRESERVE_DEATH_INFO_REGNO_P (sreg)
444                         && ! PRESERVE_DEATH_INFO_REGNO_P (dreg))
445 #endif
446                       {
447                         /* DREG may have been the target of a REG_DEAD note in
448                            the insn which makes INSN redundant.  If so, reorg
449                            would still think it is dead.  So search for such a
450                            note and delete it if we find it.  */
451                         for (trial = prev_nonnote_insn (insn);
452                              trial && GET_CODE (trial) != CODE_LABEL;
453                              trial = prev_nonnote_insn (trial))
454                           if (find_regno_note (trial, REG_DEAD, dreg))
455                             {
456                               remove_death (dreg, trial);
457                               break;
458                             }
459
460                         if (tem != 0
461                             && GET_MODE (tem) == GET_MODE (SET_DEST (body)))
462                           delete_insn (insn);
463                       }
464                   }
465                 else if (dreg >= 0 && CONSTANT_P (SET_SRC (body))
466                          && find_equiv_reg (SET_SRC (body), insn, 0, dreg,
467                                             NULL_PTR, 0,
468                                             GET_MODE (SET_DEST (body))))
469                   {
470                     /* This handles the case where we have two consecutive
471                        assignments of the same constant to pseudos that didn't
472                        get a hard reg.  Each SET from the constant will be
473                        converted into a SET of the spill register and an
474                        output reload will be made following it.  This produces
475                        two loads of the same constant into the same spill
476                        register.  */
477
478                     rtx in_insn = insn;
479
480                     /* Look back for a death note for the first reg.
481                        If there is one, it is no longer accurate.  */
482                     while (in_insn && GET_CODE (in_insn) != CODE_LABEL)
483                       {
484                         if ((GET_CODE (in_insn) == INSN
485                              || GET_CODE (in_insn) == JUMP_INSN)
486                             && find_regno_note (in_insn, REG_DEAD, dreg))
487                           {
488                             remove_death (dreg, in_insn);
489                             break;
490                           }
491                         in_insn = PREV_INSN (in_insn);
492                       }
493
494                     /* Delete the second load of the value.  */
495                     delete_insn (insn);
496                   }
497               }
498             else if (GET_CODE (body) == PARALLEL)
499               {
500                 /* If each part is a set between two identical registers or
501                    a USE or CLOBBER, delete the insn. */
502                 int i, sreg, dreg;
503                 rtx tem;
504
505                 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
506                   {
507                     tem = XVECEXP (body, 0, i);
508                     if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
509                       continue;
510
511                     if (GET_CODE (tem) != SET
512                         || (sreg = true_regnum (SET_SRC (tem))) < 0
513                         || (dreg = true_regnum (SET_DEST (tem))) < 0
514                         || dreg != sreg)
515                       break;
516                   }
517                   
518                 if (i < 0)
519                   delete_insn (insn);
520               }
521             /* Also delete insns to store bit fields if they are no-ops.  */
522             /* Not worth the hair to detect this in the big-endian case.  */
523             else if (! BYTES_BIG_ENDIAN
524                      && GET_CODE (body) == SET
525                      && GET_CODE (SET_DEST (body)) == ZERO_EXTRACT
526                      && XEXP (SET_DEST (body), 2) == const0_rtx
527                      && XEXP (SET_DEST (body), 0) == SET_SRC (body)
528                      && ! (GET_CODE (SET_SRC (body)) == MEM
529                            && MEM_VOLATILE_P (SET_SRC (body))))
530               delete_insn (insn);
531           }
532       insn = next;
533     }
534
535   /* If we haven't yet gotten to reload and we have just run regscan,
536      delete any insn that sets a register that isn't used elsewhere.
537      This helps some of the optimizations below by having less insns
538      being jumped around.  */
539
540   if (! reload_completed && after_regscan)
541     for (insn = f; insn; insn = next)
542       {
543         rtx set = single_set (insn);
544
545         next = NEXT_INSN (insn);
546
547         if (set && GET_CODE (SET_DEST (set)) == REG
548             && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
549             && regno_first_uid[REGNO (SET_DEST (set))] == INSN_UID (insn)
550             /* We use regno_last_note_uid so as not to delete the setting
551                of a reg that's used in notes.  A subsequent optimization
552                might arrange to use that reg for real.  */             
553             && regno_last_note_uid[REGNO (SET_DEST (set))] == INSN_UID (insn)
554             && ! side_effects_p (SET_SRC (set))
555             && ! find_reg_note (insn, REG_RETVAL, 0))
556           delete_insn (insn);
557       }
558
559   /* Now iterate optimizing jumps until nothing changes over one pass.  */
560   changed = 1;
561   while (changed)
562     {
563       changed = 0;
564
565       for (insn = f; insn; insn = next)
566         {
567           rtx reallabelprev;
568           rtx temp, temp1, temp2, temp3, temp4, temp5, temp6;
569           rtx nlabel;
570           int this_is_simplejump, this_is_condjump, reversep;
571           int this_is_condjump_in_parallel;
572 #if 0
573           /* If NOT the first iteration, if this is the last jump pass
574              (just before final), do the special peephole optimizations.
575              Avoiding the first iteration gives ordinary jump opts
576              a chance to work before peephole opts.  */
577
578           if (reload_completed && !first && !flag_no_peephole)
579             if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
580               peephole (insn);
581 #endif
582
583           /* That could have deleted some insns after INSN, so check now
584              what the following insn is.  */
585
586           next = NEXT_INSN (insn);
587
588           /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
589              jump.  Try to optimize by duplicating the loop exit test if so.
590              This is only safe immediately after regscan, because it uses
591              the values of regno_first_uid and regno_last_uid.  */
592           if (after_regscan && GET_CODE (insn) == NOTE
593               && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
594               && (temp1 = next_nonnote_insn (insn)) != 0
595               && simplejump_p (temp1))
596             {
597               temp = PREV_INSN (insn);
598               if (duplicate_loop_exit_test (insn))
599                 {
600                   changed = 1;
601                   next = NEXT_INSN (temp);
602                   continue;
603                 }
604             }
605
606           if (GET_CODE (insn) != JUMP_INSN)
607             continue;
608
609           this_is_simplejump = simplejump_p (insn);
610           this_is_condjump = condjump_p (insn);
611           this_is_condjump_in_parallel = condjump_in_parallel_p (insn);
612
613           /* Tension the labels in dispatch tables.  */
614
615           if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
616             changed |= tension_vector_labels (PATTERN (insn), 0);
617           if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
618             changed |= tension_vector_labels (PATTERN (insn), 1);
619
620           /* If a dispatch table always goes to the same place,
621              get rid of it and replace the insn that uses it.  */
622
623           if (GET_CODE (PATTERN (insn)) == ADDR_VEC
624               || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
625             {
626               int i;
627               rtx pat = PATTERN (insn);
628               int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
629               int len = XVECLEN (pat, diff_vec_p);
630               rtx dispatch = prev_real_insn (insn);
631
632               for (i = 0; i < len; i++)
633                 if (XEXP (XVECEXP (pat, diff_vec_p, i), 0)
634                     != XEXP (XVECEXP (pat, diff_vec_p, 0), 0))
635                   break;
636               if (i == len
637                   && dispatch != 0
638                   && GET_CODE (dispatch) == JUMP_INSN
639                   && JUMP_LABEL (dispatch) != 0
640                   /* Don't mess with a casesi insn.  */
641                   && !(GET_CODE (PATTERN (dispatch)) == SET
642                        && (GET_CODE (SET_SRC (PATTERN (dispatch)))
643                            == IF_THEN_ELSE))
644                   && next_real_insn (JUMP_LABEL (dispatch)) == insn)
645                 {
646                   redirect_tablejump (dispatch,
647                                       XEXP (XVECEXP (pat, diff_vec_p, 0), 0));
648                   changed = 1;
649                 }
650             }
651
652           reallabelprev = prev_active_insn (JUMP_LABEL (insn));
653
654           /* If a jump references the end of the function, try to turn
655              it into a RETURN insn, possibly a conditional one.  */
656           if (JUMP_LABEL (insn)
657               && (next_active_insn (JUMP_LABEL (insn)) == 0
658                   || GET_CODE (PATTERN (next_active_insn (JUMP_LABEL (insn))))
659                       == RETURN))
660             changed |= redirect_jump (insn, NULL_RTX);
661
662           /* Detect jump to following insn.  */
663           if (reallabelprev == insn && condjump_p (insn))
664             {
665               next = next_real_insn (JUMP_LABEL (insn));
666               delete_jump (insn);
667               changed = 1;
668               continue;
669             }
670
671           /* If we have an unconditional jump preceded by a USE, try to put
672              the USE before the target and jump there.  This simplifies many
673              of the optimizations below since we don't have to worry about
674              dealing with these USE insns.  We only do this if the label
675              being branch to already has the identical USE or if code
676              never falls through to that label.  */
677
678           if (this_is_simplejump
679               && (temp = prev_nonnote_insn (insn)) != 0
680               && GET_CODE (temp) == INSN && GET_CODE (PATTERN (temp)) == USE
681               && (temp1 = prev_nonnote_insn (JUMP_LABEL (insn))) != 0
682               && (GET_CODE (temp1) == BARRIER
683                   || (GET_CODE (temp1) == INSN
684                       && rtx_equal_p (PATTERN (temp), PATTERN (temp1)))))
685             {
686               if (GET_CODE (temp1) == BARRIER)
687                 {
688                   emit_insn_after (PATTERN (temp), temp1);
689                   temp1 = NEXT_INSN (temp1);
690                 }
691
692               delete_insn (temp);
693               redirect_jump (insn, get_label_before (temp1));
694               reallabelprev = prev_real_insn (temp1);
695               changed = 1;
696             }
697
698           /* Simplify   if (...) x = a; else x = b; by converting it
699              to         x = b; if (...) x = a;
700              if B is sufficiently simple, the test doesn't involve X,
701              and nothing in the test modifies B or X.
702
703              If we have small register classes, we also can't do this if X
704              is a hard register.
705
706              If the "x = b;" insn has any REG_NOTES, we don't do this because
707              of the possibility that we are running after CSE and there is a
708              REG_EQUAL note that is only valid if the branch has already been
709              taken.  If we move the insn with the REG_EQUAL note, we may
710              fold the comparison to always be false in a later CSE pass.
711              (We could also delete the REG_NOTES when moving the insn, but it
712              seems simpler to not move it.)  An exception is that we can move
713              the insn if the only note is a REG_EQUAL or REG_EQUIV whose
714              value is the same as "b".
715
716              INSN is the branch over the `else' part. 
717
718              We set:
719
720              TEMP to the jump insn preceding "x = a;"
721              TEMP1 to X
722              TEMP2 to the insn that sets "x = b;"
723              TEMP3 to the insn that sets "x = a;"
724              TEMP4 to the set of "x = b";  */
725
726           if (this_is_simplejump
727               && (temp3 = prev_active_insn (insn)) != 0
728               && GET_CODE (temp3) == INSN
729               && (temp4 = single_set (temp3)) != 0
730               && GET_CODE (temp1 = SET_DEST (temp4)) == REG
731 #ifdef SMALL_REGISTER_CLASSES
732               && REGNO (temp1) >= FIRST_PSEUDO_REGISTER
733 #endif
734               && (temp2 = next_active_insn (insn)) != 0
735               && GET_CODE (temp2) == INSN
736               && (temp4 = single_set (temp2)) != 0
737               && rtx_equal_p (SET_DEST (temp4), temp1)
738               && (GET_CODE (SET_SRC (temp4)) == REG
739                   || GET_CODE (SET_SRC (temp4)) == SUBREG
740                   || CONSTANT_P (SET_SRC (temp4)))
741               && (REG_NOTES (temp2) == 0
742                   || ((REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUAL
743                        || REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUIV)
744                       && XEXP (REG_NOTES (temp2), 1) == 0
745                       && rtx_equal_p (XEXP (REG_NOTES (temp2), 0),
746                                       SET_SRC (temp4))))
747               && (temp = prev_active_insn (temp3)) != 0
748               && condjump_p (temp) && ! simplejump_p (temp)
749               /* TEMP must skip over the "x = a;" insn */
750               && prev_real_insn (JUMP_LABEL (temp)) == insn
751               && no_labels_between_p (insn, JUMP_LABEL (temp))
752               /* There must be no other entries to the "x = b;" insn.  */
753               && no_labels_between_p (JUMP_LABEL (temp), temp2)
754               /* INSN must either branch to the insn after TEMP2 or the insn
755                  after TEMP2 must branch to the same place as INSN.  */
756               && (reallabelprev == temp2
757                   || ((temp5 = next_active_insn (temp2)) != 0
758                       && simplejump_p (temp5)
759                       && JUMP_LABEL (temp5) == JUMP_LABEL (insn))))
760             {
761               /* The test expression, X, may be a complicated test with
762                  multiple branches.  See if we can find all the uses of
763                  the label that TEMP branches to without hitting a CALL_INSN
764                  or a jump to somewhere else.  */
765               rtx target = JUMP_LABEL (temp);
766               int nuses = LABEL_NUSES (target);
767               rtx p, q;
768
769               /* Set P to the first jump insn that goes around "x = a;".  */
770               for (p = temp; nuses && p; p = prev_nonnote_insn (p))
771                 {
772                   if (GET_CODE (p) == JUMP_INSN)
773                     {
774                       if (condjump_p (p) && ! simplejump_p (p)
775                           && JUMP_LABEL (p) == target)
776                         {
777                           nuses--;
778                           if (nuses == 0)
779                             break;
780                         }
781                       else
782                         break;
783                     }
784                   else if (GET_CODE (p) == CALL_INSN)
785                     break;
786                 }
787
788 #ifdef HAVE_cc0
789               /* We cannot insert anything between a set of cc and its use
790                  so if P uses cc0, we must back up to the previous insn.  */
791               q = prev_nonnote_insn (p);
792               if (q && GET_RTX_CLASS (GET_CODE (q)) == 'i'
793                   && sets_cc0_p (PATTERN (q)))
794                 p = q;
795 #endif
796
797               if (p)
798                 p = PREV_INSN (p);
799
800               /* If we found all the uses and there was no data conflict, we
801                  can move the assignment unless we can branch into the middle
802                  from somewhere.  */
803               if (nuses == 0 && p
804                   && no_labels_between_p (p, insn)
805                   && ! reg_referenced_between_p (temp1, p, NEXT_INSN (temp3))
806                   && ! reg_set_between_p (temp1, p, temp3)
807                   && (GET_CODE (SET_SRC (temp4)) == CONST_INT
808                       || ! reg_set_between_p (SET_SRC (temp4), p, temp2)))
809                 {
810                   emit_insn_after_with_line_notes (PATTERN (temp2), p, temp2);
811                   delete_insn (temp2);
812
813                   /* Set NEXT to an insn that we know won't go away.  */
814                   next = next_active_insn (insn);
815
816                   /* Delete the jump around the set.  Note that we must do
817                      this before we redirect the test jumps so that it won't
818                      delete the code immediately following the assignment
819                      we moved (which might be a jump).  */
820
821                   delete_insn (insn);
822
823                   /* We either have two consecutive labels or a jump to
824                      a jump, so adjust all the JUMP_INSNs to branch to where
825                      INSN branches to.  */
826                   for (p = NEXT_INSN (p); p != next; p = NEXT_INSN (p))
827                     if (GET_CODE (p) == JUMP_INSN)
828                       redirect_jump (p, target);
829
830                   changed = 1;
831                   continue;
832                 }
833             }
834
835 #ifndef HAVE_cc0
836           /* If we have if (...) x = exp;  and branches are expensive,
837              EXP is a single insn, does not have any side effects, cannot
838              trap, and is not too costly, convert this to
839              t = exp; if (...) x = t;
840
841              Don't do this when we have CC0 because it is unlikely to help
842              and we'd need to worry about where to place the new insn and
843              the potential for conflicts.  We also can't do this when we have
844              notes on the insn for the same reason as above.
845
846              We set:
847
848              TEMP to the "x = exp;" insn.
849              TEMP1 to the single set in the "x = exp; insn.
850              TEMP2 to "x".  */
851
852           if (! reload_completed
853               && this_is_condjump && ! this_is_simplejump
854               && BRANCH_COST >= 3
855               && (temp = next_nonnote_insn (insn)) != 0
856               && GET_CODE (temp) == INSN
857               && REG_NOTES (temp) == 0
858               && (reallabelprev == temp
859                   || ((temp2 = next_active_insn (temp)) != 0
860                       && simplejump_p (temp2)
861                       && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
862               && (temp1 = single_set (temp)) != 0
863               && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
864               && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
865 #ifdef SMALL_REGISTER_CLASSES
866               && REGNO (temp2) >= FIRST_PSEUDO_REGISTER
867 #endif
868               && GET_CODE (SET_SRC (temp1)) != REG
869               && GET_CODE (SET_SRC (temp1)) != SUBREG
870               && GET_CODE (SET_SRC (temp1)) != CONST_INT
871               && ! side_effects_p (SET_SRC (temp1))
872               && ! may_trap_p (SET_SRC (temp1))
873               && rtx_cost (SET_SRC (temp1)) < 10)
874             {
875               rtx new = gen_reg_rtx (GET_MODE (temp2));
876
877               if (validate_change (temp, &SET_DEST (temp1), new, 0))
878                 {
879                   next = emit_insn_after (gen_move_insn (temp2, new), insn);
880                   emit_insn_after_with_line_notes (PATTERN (temp), 
881                                                    PREV_INSN (insn), temp);
882                   delete_insn (temp);
883                   reallabelprev = prev_active_insn (JUMP_LABEL (insn));
884                 }
885             }
886
887           /* Similarly, if it takes two insns to compute EXP but they
888              have the same destination.  Here TEMP3 will be the second
889              insn and TEMP4 the SET from that insn.  */
890
891           if (! reload_completed
892               && this_is_condjump && ! this_is_simplejump
893               && BRANCH_COST >= 4
894               && (temp = next_nonnote_insn (insn)) != 0
895               && GET_CODE (temp) == INSN
896               && REG_NOTES (temp) == 0
897               && (temp3 = next_nonnote_insn (temp)) != 0
898               && GET_CODE (temp3) == INSN
899               && REG_NOTES (temp3) == 0
900               && (reallabelprev == temp3
901                   || ((temp2 = next_active_insn (temp3)) != 0
902                       && simplejump_p (temp2)
903                       && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
904               && (temp1 = single_set (temp)) != 0
905               && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
906               && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
907 #ifdef SMALL_REGISTER_CLASSES
908               && REGNO (temp2) >= FIRST_PSEUDO_REGISTER
909 #endif
910               && ! side_effects_p (SET_SRC (temp1))
911               && ! may_trap_p (SET_SRC (temp1))
912               && rtx_cost (SET_SRC (temp1)) < 10
913               && (temp4 = single_set (temp3)) != 0
914               && rtx_equal_p (SET_DEST (temp4), temp2)
915               && ! side_effects_p (SET_SRC (temp4))
916               && ! may_trap_p (SET_SRC (temp4))
917               && rtx_cost (SET_SRC (temp4)) < 10)
918             {
919               rtx new = gen_reg_rtx (GET_MODE (temp2));
920
921               if (validate_change (temp, &SET_DEST (temp1), new, 0))
922                 {
923                   next = emit_insn_after (gen_move_insn (temp2, new), insn);
924                   emit_insn_after_with_line_notes (PATTERN (temp),
925                                                    PREV_INSN (insn), temp);
926                   emit_insn_after_with_line_notes
927                     (replace_rtx (PATTERN (temp3), temp2, new),
928                      PREV_INSN (insn), temp3);
929                   delete_insn (temp);
930                   delete_insn (temp3);
931                   reallabelprev = prev_active_insn (JUMP_LABEL (insn));
932                 }
933             }
934
935           /* Finally, handle the case where two insns are used to 
936              compute EXP but a temporary register is used.  Here we must
937              ensure that the temporary register is not used anywhere else. */
938
939           if (! reload_completed
940               && after_regscan
941               && this_is_condjump && ! this_is_simplejump
942               && BRANCH_COST >= 4
943               && (temp = next_nonnote_insn (insn)) != 0
944               && GET_CODE (temp) == INSN
945               && REG_NOTES (temp) == 0
946               && (temp3 = next_nonnote_insn (temp)) != 0
947               && GET_CODE (temp3) == INSN
948               && REG_NOTES (temp3) == 0
949               && (reallabelprev == temp3
950                   || ((temp2 = next_active_insn (temp3)) != 0
951                       && simplejump_p (temp2)
952                       && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
953               && (temp1 = single_set (temp)) != 0
954               && (temp5 = SET_DEST (temp1),
955                   (GET_CODE (temp5) == REG
956                    || (GET_CODE (temp5) == SUBREG
957                        && (temp5 = SUBREG_REG (temp5),
958                            GET_CODE (temp5) == REG))))
959               && REGNO (temp5) >= FIRST_PSEUDO_REGISTER
960               && regno_first_uid[REGNO (temp5)] == INSN_UID (temp)
961               && regno_last_uid[REGNO (temp5)] == INSN_UID (temp3)
962               && ! side_effects_p (SET_SRC (temp1))
963               && ! may_trap_p (SET_SRC (temp1))
964               && rtx_cost (SET_SRC (temp1)) < 10
965               && (temp4 = single_set (temp3)) != 0
966               && (temp2 = SET_DEST (temp4), GET_CODE (temp2) == REG)
967               && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
968 #ifdef SMALL_REGISTER_CLASSES
969               && REGNO (temp2) >= FIRST_PSEUDO_REGISTER
970 #endif
971               && rtx_equal_p (SET_DEST (temp4), temp2)
972               && ! side_effects_p (SET_SRC (temp4))
973               && ! may_trap_p (SET_SRC (temp4))
974               && rtx_cost (SET_SRC (temp4)) < 10)
975             {
976               rtx new = gen_reg_rtx (GET_MODE (temp2));
977
978               if (validate_change (temp3, &SET_DEST (temp4), new, 0))
979                 {
980                   next = emit_insn_after (gen_move_insn (temp2, new), insn);
981                   emit_insn_after_with_line_notes (PATTERN (temp),
982                                                    PREV_INSN (insn), temp);
983                   emit_insn_after_with_line_notes (PATTERN (temp3),
984                                                    PREV_INSN (insn), temp3);
985                   delete_insn (temp);
986                   delete_insn (temp3);
987                   reallabelprev = prev_active_insn (JUMP_LABEL (insn));
988                 }
989             }
990 #endif /* HAVE_cc0 */
991
992           /* Try to use a conditional move (if the target has them), or a
993              store-flag insn.  The general case is:
994
995              1) x = a; if (...) x = b; and
996              2) if (...) x = b;
997
998              If the jump would be faster, the machine should not have defined
999              the movcc or scc insns!.  These cases are often made by the
1000              previous optimization.
1001
1002              The second case is treated as  x = x; if (...) x = b;.
1003
1004              INSN here is the jump around the store.  We set:
1005
1006              TEMP to the "x = b;" insn.
1007              TEMP1 to X.
1008              TEMP2 to B.
1009              TEMP3 to A (X in the second case).
1010              TEMP4 to the condition being tested.
1011              TEMP5 to the earliest insn used to find the condition.  */
1012
1013           if (/* We can't do this after reload has completed.  */
1014               ! reload_completed
1015               && this_is_condjump && ! this_is_simplejump
1016               /* Set TEMP to the "x = b;" insn.  */
1017               && (temp = next_nonnote_insn (insn)) != 0
1018               && GET_CODE (temp) == INSN
1019               && GET_CODE (PATTERN (temp)) == SET
1020               && GET_CODE (temp1 = SET_DEST (PATTERN (temp))) == REG
1021 #ifdef SMALL_REGISTER_CLASSES
1022               && REGNO (temp1) >= FIRST_PSEUDO_REGISTER
1023 #endif
1024               && (GET_CODE (temp2 = SET_SRC (PATTERN (temp))) == REG
1025                   || GET_CODE (temp2) == SUBREG
1026                   /* ??? How about floating point constants?  */
1027                   || GET_CODE (temp2) == CONST_INT)
1028               /* Allow either form, but prefer the former if both apply. 
1029                  There is no point in using the old value of TEMP1 if
1030                  it is a register, since cse will alias them.  It can
1031                  lose if the old value were a hard register since CSE
1032                  won't replace hard registers.  */
1033               && (((temp3 = reg_set_last (temp1, insn)) != 0)
1034                   /* Make the latter case look like  x = x; if (...) x = b;  */
1035                   || (temp3 = temp1, 1))
1036               /* INSN must either branch to the insn after TEMP or the insn
1037                  after TEMP must branch to the same place as INSN.  */
1038               && (reallabelprev == temp
1039                   || ((temp4 = next_active_insn (temp)) != 0
1040                       && simplejump_p (temp4)
1041                       && JUMP_LABEL (temp4) == JUMP_LABEL (insn)))
1042               && (temp4 = get_condition (insn, &temp5)) != 0
1043               /* We must be comparing objects whose modes imply the size.
1044                  We could handle BLKmode if (1) emit_store_flag could
1045                  and (2) we could find the size reliably.  */
1046               && GET_MODE (XEXP (temp4, 0)) != BLKmode
1047               /* No point in doing any of this if branches are cheap or we
1048                  don't have conditional moves.  */
1049               && (BRANCH_COST >= 2
1050 #ifdef HAVE_conditional_move
1051                   || 1
1052 #endif
1053                   )
1054 #ifdef HAVE_cc0
1055               /* If the previous insn sets CC0 and something else, we can't
1056                  do this since we are going to delete that insn.  */
1057
1058               && ! ((temp6 = prev_nonnote_insn (insn)) != 0
1059                     && GET_CODE (temp6) == INSN
1060                     && (sets_cc0_p (PATTERN (temp6)) == -1
1061                         || (sets_cc0_p (PATTERN (temp6)) == 1
1062                             && FIND_REG_INC_NOTE (temp6, NULL_RTX))))
1063 #endif
1064               )
1065             {
1066 #ifdef HAVE_conditional_move
1067               /* First try a conditional move.  */
1068               {
1069                 enum rtx_code code = GET_CODE (temp4);
1070                 rtx var = temp1;
1071                 rtx cond0, cond1, aval, bval;
1072                 rtx target;
1073
1074                 /* Copy the compared variables into cond0 and cond1, so that
1075                    any side effects performed in or after the old comparison,
1076                    will not affect our compare which will come later.  */
1077                 /* ??? Is it possible to just use the comparison in the jump
1078                    insn?  After all, we're going to delete it.  We'd have
1079                    to modify emit_conditional_move to take a comparison rtx
1080                    instead or write a new function.  */
1081                 cond0 = gen_reg_rtx (GET_MODE (XEXP (temp4, 0)));
1082                 /* We want the target to be able to simplify comparisons with
1083                    zero (and maybe other constants as well), so don't create
1084                    pseudos for them.  There's no need to either.  */
1085                 if (GET_CODE (XEXP (temp4, 1)) == CONST_INT
1086                     || GET_CODE (XEXP (temp4, 1)) == CONST_DOUBLE)
1087                   cond1 = XEXP (temp4, 1);
1088                 else
1089                   cond1 = gen_reg_rtx (GET_MODE (XEXP (temp4, 1)));
1090
1091                 aval = temp3;
1092                 bval = temp2;
1093
1094                 start_sequence ();
1095                 target = emit_conditional_move (var, code,
1096                                                 cond0, cond1, VOIDmode,
1097                                                 aval, bval, GET_MODE (var),
1098                                                 (code == LTU || code == GEU
1099                                                  || code == LEU || code == GTU));
1100
1101                 if (target)
1102                   {
1103                     rtx seq1,seq2;
1104
1105                     /* Save the conditional move sequence but don't emit it
1106                        yet.  On some machines, like the alpha, it is possible
1107                        that temp5 == insn, so next generate the sequence that
1108                        saves the compared values and then emit both
1109                        sequences ensuring seq1 occurs before seq2.  */
1110                     seq2 = get_insns ();
1111                     end_sequence ();
1112
1113                     /* Now that we can't fail, generate the copy insns that
1114                        preserve the compared values.  */
1115                     start_sequence ();
1116                     emit_move_insn (cond0, XEXP (temp4, 0));
1117                     if (cond1 != XEXP (temp4, 1))
1118                       emit_move_insn (cond1, XEXP (temp4, 1));
1119                     seq1 = get_insns ();
1120                     end_sequence ();
1121
1122                     emit_insns_before (seq1, temp5);
1123                     emit_insns_before (seq2, insn);
1124
1125                     /* ??? We can also delete the insn that sets X to A.
1126                        Flow will do it too though.  */
1127                     delete_insn (temp);
1128                     next = NEXT_INSN (insn);
1129                     delete_jump (insn);
1130                     changed = 1;
1131                     continue;
1132                   }
1133                 else
1134                   end_sequence ();
1135               }
1136 #endif
1137
1138               /* That didn't work, try a store-flag insn.
1139
1140                  We further divide the cases into:
1141
1142                  1) x = a; if (...) x = b; and either A or B is zero,
1143                  2) if (...) x = 0; and jumps are expensive,
1144                  3) x = a; if (...) x = b; and A and B are constants where all
1145                  the set bits in A are also set in B and jumps are expensive,
1146                  4) x = a; if (...) x = b; and A and B non-zero, and jumps are
1147                  more expensive, and
1148                  5) if (...) x = b; if jumps are even more expensive.  */
1149
1150               if (GET_MODE_CLASS (GET_MODE (temp1)) == MODE_INT
1151                   && ((GET_CODE (temp3) == CONST_INT)
1152                       /* Make the latter case look like
1153                          x = x; if (...) x = 0;  */
1154                       || (temp3 = temp1,
1155                           ((BRANCH_COST >= 2
1156                             && temp2 == const0_rtx)
1157                            || BRANCH_COST >= 3)))
1158                   /* If B is zero, OK; if A is zero, can only do (1) if we
1159                      can reverse the condition.  See if (3) applies possibly
1160                      by reversing the condition.  Prefer reversing to (4) when
1161                      branches are very expensive.  */
1162                   && ((reversep = 0, temp2 == const0_rtx)
1163                       || (temp3 == const0_rtx
1164                           && (reversep = can_reverse_comparison_p (temp4, insn)))
1165                       || (BRANCH_COST >= 2
1166                           && GET_CODE (temp2) == CONST_INT
1167                           && GET_CODE (temp3) == CONST_INT
1168                           && ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp2)
1169                               || ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp3)
1170                                   && (reversep = can_reverse_comparison_p (temp4,
1171                                                                            insn)))))
1172                       || BRANCH_COST >= 3)
1173                   )
1174                 {
1175                   enum rtx_code code = GET_CODE (temp4);
1176                   rtx uval, cval, var = temp1;
1177                   int normalizep;
1178                   rtx target;
1179
1180                   /* If necessary, reverse the condition.  */
1181                   if (reversep)
1182                     code = reverse_condition (code), uval = temp2, cval = temp3;
1183                   else
1184                     uval = temp3, cval = temp2;
1185
1186                   /* If CVAL is non-zero, normalize to -1.  Otherwise, if UVAL
1187                      is the constant 1, it is best to just compute the result
1188                      directly.  If UVAL is constant and STORE_FLAG_VALUE
1189                      includes all of its bits, it is best to compute the flag
1190                      value unnormalized and `and' it with UVAL.  Otherwise,
1191                      normalize to -1 and `and' with UVAL.  */
1192                   normalizep = (cval != const0_rtx ? -1
1193                                 : (uval == const1_rtx ? 1
1194                                    : (GET_CODE (uval) == CONST_INT
1195                                       && (INTVAL (uval) & ~STORE_FLAG_VALUE) == 0)
1196                                    ? 0 : -1));
1197
1198                   /* We will be putting the store-flag insn immediately in
1199                      front of the comparison that was originally being done,
1200                      so we know all the variables in TEMP4 will be valid.
1201                      However, this might be in front of the assignment of
1202                      A to VAR.  If it is, it would clobber the store-flag
1203                      we will be emitting.
1204
1205                      Therefore, emit into a temporary which will be copied to
1206                      VAR immediately after TEMP.  */
1207
1208                   start_sequence ();
1209                   target = emit_store_flag (gen_reg_rtx (GET_MODE (var)), code,
1210                                             XEXP (temp4, 0), XEXP (temp4, 1),
1211                                             VOIDmode,
1212                                             (code == LTU || code == LEU 
1213                                              || code == GEU || code == GTU),
1214                                             normalizep);
1215                   if (target)
1216                     {
1217                       rtx seq;
1218                       rtx before = insn;
1219
1220                       seq = get_insns ();
1221                       end_sequence ();
1222
1223                       /* Put the store-flag insns in front of the first insn
1224                          used to compute the condition to ensure that we
1225                          use the same values of them as the current 
1226                          comparison.  However, the remainder of the insns we
1227                          generate will be placed directly in front of the
1228                          jump insn, in case any of the pseudos we use
1229                          are modified earlier.  */
1230
1231                       emit_insns_before (seq, temp5);
1232
1233                       start_sequence ();
1234
1235                       /* Both CVAL and UVAL are non-zero.  */
1236                       if (cval != const0_rtx && uval != const0_rtx)
1237                         {
1238                           rtx tem1, tem2;
1239
1240                           tem1 = expand_and (uval, target, NULL_RTX);
1241                           if (GET_CODE (cval) == CONST_INT
1242                               && GET_CODE (uval) == CONST_INT
1243                               && (INTVAL (cval) & INTVAL (uval)) == INTVAL (cval))
1244                             tem2 = cval;
1245                           else
1246                             {
1247                               tem2 = expand_unop (GET_MODE (var), one_cmpl_optab,
1248                                                   target, NULL_RTX, 0);
1249                               tem2 = expand_and (cval, tem2,
1250                                                  (GET_CODE (tem2) == REG
1251                                                   ? tem2 : 0));
1252                             }
1253
1254                           /* If we usually make new pseudos, do so here.  This
1255                              turns out to help machines that have conditional
1256                              move insns.  */
1257                           /* ??? Conditional moves have already been handled.
1258                              This may be obsolete.  */
1259
1260                           if (flag_expensive_optimizations)
1261                             target = 0;
1262
1263                           target = expand_binop (GET_MODE (var), ior_optab,
1264                                                  tem1, tem2, target,
1265                                                  1, OPTAB_WIDEN);
1266                         }
1267                       else if (normalizep != 1)
1268                         {
1269                           /* We know that either CVAL or UVAL is zero.  If
1270                              UVAL is zero, negate TARGET and `and' with CVAL.
1271                              Otherwise, `and' with UVAL.  */
1272                           if (uval == const0_rtx)
1273                             {
1274                               target = expand_unop (GET_MODE (var), one_cmpl_optab,
1275                                                     target, NULL_RTX, 0);
1276                               uval = cval;
1277                             }
1278
1279                           target = expand_and (uval, target,
1280                                                (GET_CODE (target) == REG
1281                                                 && ! preserve_subexpressions_p ()
1282                                                 ? target : NULL_RTX));
1283                         }
1284                   
1285                       emit_move_insn (var, target);
1286                       seq = get_insns ();
1287                       end_sequence ();
1288 #ifdef HAVE_cc0
1289                       /* If INSN uses CC0, we must not separate it from the
1290                          insn that sets cc0.  */
1291                       if (reg_mentioned_p (cc0_rtx, PATTERN (before)))
1292                         before = prev_nonnote_insn (before);
1293 #endif
1294                       emit_insns_before (seq, before);
1295
1296                       delete_insn (temp);
1297                       next = NEXT_INSN (insn);
1298                       delete_jump (insn);
1299                       changed = 1;
1300                       continue;
1301                     }
1302                   else
1303                     end_sequence ();
1304                 }
1305             }
1306
1307           /* If branches are expensive, convert
1308                 if (foo) bar++;    to    bar += (foo != 0);
1309              and similarly for "bar--;" 
1310
1311              INSN is the conditional branch around the arithmetic.  We set:
1312
1313              TEMP is the arithmetic insn.
1314              TEMP1 is the SET doing the arithmetic.
1315              TEMP2 is the operand being incremented or decremented.
1316              TEMP3 to the condition being tested.
1317              TEMP4 to the earliest insn used to find the condition.  */
1318
1319           if ((BRANCH_COST >= 2
1320 #ifdef HAVE_incscc
1321                || HAVE_incscc
1322 #endif
1323 #ifdef HAVE_decscc
1324                || HAVE_decscc
1325 #endif
1326               )
1327               && ! reload_completed
1328               && this_is_condjump && ! this_is_simplejump
1329               && (temp = next_nonnote_insn (insn)) != 0
1330               && (temp1 = single_set (temp)) != 0
1331               && (temp2 = SET_DEST (temp1),
1332                   GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT)
1333               && GET_CODE (SET_SRC (temp1)) == PLUS
1334               && (XEXP (SET_SRC (temp1), 1) == const1_rtx
1335                   || XEXP (SET_SRC (temp1), 1) == constm1_rtx)
1336               && rtx_equal_p (temp2, XEXP (SET_SRC (temp1), 0))
1337               && ! side_effects_p (temp2)
1338               && ! may_trap_p (temp2)
1339               /* INSN must either branch to the insn after TEMP or the insn
1340                  after TEMP must branch to the same place as INSN.  */
1341               && (reallabelprev == temp
1342                   || ((temp3 = next_active_insn (temp)) != 0
1343                       && simplejump_p (temp3)
1344                       && JUMP_LABEL (temp3) == JUMP_LABEL (insn)))
1345               && (temp3 = get_condition (insn, &temp4)) != 0
1346               /* We must be comparing objects whose modes imply the size.
1347                  We could handle BLKmode if (1) emit_store_flag could
1348                  and (2) we could find the size reliably.  */
1349               && GET_MODE (XEXP (temp3, 0)) != BLKmode
1350               && can_reverse_comparison_p (temp3, insn))
1351             {
1352               rtx temp6, target = 0, seq, init_insn = 0, init = temp2;
1353               enum rtx_code code = reverse_condition (GET_CODE (temp3));
1354
1355               start_sequence ();
1356
1357               /* It must be the case that TEMP2 is not modified in the range
1358                  [TEMP4, INSN).  The one exception we make is if the insn
1359                  before INSN sets TEMP2 to something which is also unchanged
1360                  in that range.  In that case, we can move the initialization
1361                  into our sequence.  */
1362
1363               if ((temp5 = prev_active_insn (insn)) != 0
1364                   && GET_CODE (temp5) == INSN
1365                   && (temp6 = single_set (temp5)) != 0
1366                   && rtx_equal_p (temp2, SET_DEST (temp6))
1367                   && (CONSTANT_P (SET_SRC (temp6))
1368                       || GET_CODE (SET_SRC (temp6)) == REG
1369                       || GET_CODE (SET_SRC (temp6)) == SUBREG))
1370                 {
1371                   emit_insn (PATTERN (temp5));
1372                   init_insn = temp5;
1373                   init = SET_SRC (temp6);
1374                 }
1375
1376               if (CONSTANT_P (init)
1377                   || ! reg_set_between_p (init, PREV_INSN (temp4), insn))
1378                 target = emit_store_flag (gen_reg_rtx (GET_MODE (temp2)), code,
1379                                           XEXP (temp3, 0), XEXP (temp3, 1),
1380                                           VOIDmode,
1381                                           (code == LTU || code == LEU
1382                                            || code == GTU || code == GEU), 1);
1383
1384               /* If we can do the store-flag, do the addition or
1385                  subtraction.  */
1386
1387               if (target)
1388                 target = expand_binop (GET_MODE (temp2),
1389                                        (XEXP (SET_SRC (temp1), 1) == const1_rtx
1390                                         ? add_optab : sub_optab),
1391                                        temp2, target, temp2, 0, OPTAB_WIDEN);
1392
1393               if (target != 0)
1394                 {
1395                   /* Put the result back in temp2 in case it isn't already.
1396                      Then replace the jump, possible a CC0-setting insn in
1397                      front of the jump, and TEMP, with the sequence we have
1398                      made.  */
1399
1400                   if (target != temp2)
1401                     emit_move_insn (temp2, target);
1402
1403                   seq = get_insns ();
1404                   end_sequence ();
1405
1406                   emit_insns_before (seq, temp4);
1407                   delete_insn (temp);
1408
1409                   if (init_insn)
1410                     delete_insn (init_insn);
1411
1412                   next = NEXT_INSN (insn);
1413 #ifdef HAVE_cc0
1414                   delete_insn (prev_nonnote_insn (insn));
1415 #endif
1416                   delete_insn (insn);
1417                   changed = 1;
1418                   continue;
1419                 }
1420               else
1421                 end_sequence ();
1422             }
1423
1424           /* Simplify   if (...) x = 1; else {...}  if (x) ...
1425              We recognize this case scanning backwards as well.
1426
1427              TEMP is the assignment to x;
1428              TEMP1 is the label at the head of the second if.  */
1429           /* ?? This should call get_condition to find the values being
1430              compared, instead of looking for a COMPARE insn when HAVE_cc0
1431              is not defined.  This would allow it to work on the m88k.  */
1432           /* ?? This optimization is only safe before cse is run if HAVE_cc0
1433              is not defined and the condition is tested by a separate compare
1434              insn.  This is because the code below assumes that the result
1435              of the compare dies in the following branch.
1436
1437              Not only that, but there might be other insns between the
1438              compare and branch whose results are live.  Those insns need
1439              to be executed.
1440
1441              A way to fix this is to move the insns at JUMP_LABEL (insn)
1442              to before INSN.  If we are running before flow, they will
1443              be deleted if they aren't needed.   But this doesn't work
1444              well after flow.
1445
1446              This is really a special-case of jump threading, anyway.  The
1447              right thing to do is to replace this and jump threading with
1448              much simpler code in cse.
1449
1450              This code has been turned off in the non-cc0 case in the
1451              meantime.  */
1452
1453 #ifdef HAVE_cc0
1454           else if (this_is_simplejump
1455                    /* Safe to skip USE and CLOBBER insns here
1456                       since they will not be deleted.  */
1457                    && (temp = prev_active_insn (insn))
1458                    && no_labels_between_p (temp, insn)
1459                    && GET_CODE (temp) == INSN
1460                    && GET_CODE (PATTERN (temp)) == SET
1461                    && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1462                    && CONSTANT_P (SET_SRC (PATTERN (temp)))
1463                    && (temp1 = next_active_insn (JUMP_LABEL (insn)))
1464                    /* If we find that the next value tested is `x'
1465                       (TEMP1 is the insn where this happens), win.  */
1466                    && GET_CODE (temp1) == INSN
1467                    && GET_CODE (PATTERN (temp1)) == SET
1468 #ifdef HAVE_cc0
1469                    /* Does temp1 `tst' the value of x?  */
1470                    && SET_SRC (PATTERN (temp1)) == SET_DEST (PATTERN (temp))
1471                    && SET_DEST (PATTERN (temp1)) == cc0_rtx
1472                    && (temp1 = next_nonnote_insn (temp1))
1473 #else
1474                    /* Does temp1 compare the value of x against zero?  */
1475                    && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1476                    && XEXP (SET_SRC (PATTERN (temp1)), 1) == const0_rtx
1477                    && (XEXP (SET_SRC (PATTERN (temp1)), 0)
1478                        == SET_DEST (PATTERN (temp)))
1479                    && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1480                    && (temp1 = find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1481 #endif
1482                    && condjump_p (temp1))
1483             {
1484               /* Get the if_then_else from the condjump.  */
1485               rtx choice = SET_SRC (PATTERN (temp1));
1486               if (GET_CODE (choice) == IF_THEN_ELSE)
1487                 {
1488                   enum rtx_code code = GET_CODE (XEXP (choice, 0));
1489                   rtx val = SET_SRC (PATTERN (temp));
1490                   rtx cond
1491                     = simplify_relational_operation (code, GET_MODE (SET_DEST (PATTERN (temp))),
1492                                                      val, const0_rtx);
1493                   rtx ultimate;
1494
1495                   if (cond == const_true_rtx)
1496                     ultimate = XEXP (choice, 1);
1497                   else if (cond == const0_rtx)
1498                     ultimate = XEXP (choice, 2);
1499                   else
1500                     ultimate = 0;
1501
1502                   if (ultimate == pc_rtx)
1503                     ultimate = get_label_after (temp1);
1504                   else if (ultimate && GET_CODE (ultimate) != RETURN)
1505                     ultimate = XEXP (ultimate, 0);
1506
1507                   if (ultimate)
1508                     changed |= redirect_jump (insn, ultimate);
1509                 }
1510             }
1511 #endif
1512
1513 #if 0
1514           /* @@ This needs a bit of work before it will be right.
1515
1516              Any type of comparison can be accepted for the first and
1517              second compare.  When rewriting the first jump, we must
1518              compute the what conditions can reach label3, and use the
1519              appropriate code.  We can not simply reverse/swap the code
1520              of the first jump.  In some cases, the second jump must be
1521              rewritten also.
1522
1523              For example, 
1524              <  == converts to >  ==
1525              <  != converts to ==  >
1526              etc.
1527
1528              If the code is written to only accept an '==' test for the second
1529              compare, then all that needs to be done is to swap the condition
1530              of the first branch.
1531
1532              It is questionable whether we want this optimization anyways,
1533              since if the user wrote code like this because he/she knew that
1534              the jump to label1 is taken most of the time, then rewriting
1535              this gives slower code.  */
1536           /* @@ This should call get_condition to find the values being
1537              compared, instead of looking for a COMPARE insn when HAVE_cc0
1538              is not defined.  This would allow it to work on the m88k.  */
1539           /* @@ This optimization is only safe before cse is run if HAVE_cc0
1540              is not defined and the condition is tested by a separate compare
1541              insn.  This is because the code below assumes that the result
1542              of the compare dies in the following branch.  */
1543
1544           /* Simplify  test a ~= b
1545                        condjump label1;
1546                        test a == b
1547                        condjump label2;
1548                        jump label3;
1549                        label1:
1550
1551              rewriting as
1552                        test a ~~= b
1553                        condjump label3
1554                        test a == b
1555                        condjump label2
1556                        label1:
1557
1558              where ~= is an inequality, e.g. >, and ~~= is the swapped
1559              inequality, e.g. <.
1560
1561              We recognize this case scanning backwards.
1562
1563              TEMP is the conditional jump to `label2';
1564              TEMP1 is the test for `a == b';
1565              TEMP2 is the conditional jump to `label1';
1566              TEMP3 is the test for `a ~= b'.  */
1567           else if (this_is_simplejump
1568                    && (temp = prev_active_insn (insn))
1569                    && no_labels_between_p (temp, insn)
1570                    && condjump_p (temp)
1571                    && (temp1 = prev_active_insn (temp))
1572                    && no_labels_between_p (temp1, temp)
1573                    && GET_CODE (temp1) == INSN
1574                    && GET_CODE (PATTERN (temp1)) == SET
1575 #ifdef HAVE_cc0
1576                    && sets_cc0_p (PATTERN (temp1)) == 1
1577 #else
1578                    && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1579                    && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1580                    && (temp == find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1581 #endif
1582                    && (temp2 = prev_active_insn (temp1))
1583                    && no_labels_between_p (temp2, temp1)
1584                    && condjump_p (temp2)
1585                    && JUMP_LABEL (temp2) == next_nonnote_insn (NEXT_INSN (insn))
1586                    && (temp3 = prev_active_insn (temp2))
1587                    && no_labels_between_p (temp3, temp2)
1588                    && GET_CODE (PATTERN (temp3)) == SET
1589                    && rtx_equal_p (SET_DEST (PATTERN (temp3)),
1590                                    SET_DEST (PATTERN (temp1)))
1591                    && rtx_equal_p (SET_SRC (PATTERN (temp1)),
1592                                    SET_SRC (PATTERN (temp3)))
1593                    && ! inequality_comparisons_p (PATTERN (temp))
1594                    && inequality_comparisons_p (PATTERN (temp2)))
1595             {
1596               rtx fallthrough_label = JUMP_LABEL (temp2);
1597
1598               ++LABEL_NUSES (fallthrough_label);
1599               if (swap_jump (temp2, JUMP_LABEL (insn)))
1600                 {
1601                   delete_insn (insn);
1602                   changed = 1;
1603                 }
1604
1605               if (--LABEL_NUSES (fallthrough_label) == 0)
1606                 delete_insn (fallthrough_label);
1607             }
1608 #endif
1609           /* Simplify  if (...) {... x = 1;} if (x) ...
1610
1611              We recognize this case backwards.
1612
1613              TEMP is the test of `x';
1614              TEMP1 is the assignment to `x' at the end of the
1615              previous statement.  */
1616           /* @@ This should call get_condition to find the values being
1617              compared, instead of looking for a COMPARE insn when HAVE_cc0
1618              is not defined.  This would allow it to work on the m88k.  */
1619           /* @@ This optimization is only safe before cse is run if HAVE_cc0
1620              is not defined and the condition is tested by a separate compare
1621              insn.  This is because the code below assumes that the result
1622              of the compare dies in the following branch.  */
1623
1624           /* ??? This has to be turned off.  The problem is that the
1625              unconditional jump might indirectly end up branching to the
1626              label between TEMP1 and TEMP.  We can't detect this, in general,
1627              since it may become a jump to there after further optimizations.
1628              If that jump is done, it will be deleted, so we will retry
1629              this optimization in the next pass, thus an infinite loop.
1630
1631              The present code prevents this by putting the jump after the
1632              label, but this is not logically correct.  */
1633 #if 0
1634           else if (this_is_condjump
1635                    /* Safe to skip USE and CLOBBER insns here
1636                       since they will not be deleted.  */
1637                    && (temp = prev_active_insn (insn))
1638                    && no_labels_between_p (temp, insn)
1639                    && GET_CODE (temp) == INSN
1640                    && GET_CODE (PATTERN (temp)) == SET
1641 #ifdef HAVE_cc0
1642                    && sets_cc0_p (PATTERN (temp)) == 1
1643                    && GET_CODE (SET_SRC (PATTERN (temp))) == REG
1644 #else
1645                    /* Temp must be a compare insn, we can not accept a register
1646                       to register move here, since it may not be simply a
1647                       tst insn.  */
1648                    && GET_CODE (SET_SRC (PATTERN (temp))) == COMPARE
1649                    && XEXP (SET_SRC (PATTERN (temp)), 1) == const0_rtx
1650                    && GET_CODE (XEXP (SET_SRC (PATTERN (temp)), 0)) == REG
1651                    && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1652                    && insn == find_next_ref (SET_DEST (PATTERN (temp)), temp)
1653 #endif
1654                    /* May skip USE or CLOBBER insns here
1655                       for checking for opportunity, since we
1656                       take care of them later.  */
1657                    && (temp1 = prev_active_insn (temp))
1658                    && GET_CODE (temp1) == INSN
1659                    && GET_CODE (PATTERN (temp1)) == SET
1660 #ifdef HAVE_cc0
1661                    && SET_SRC (PATTERN (temp)) == SET_DEST (PATTERN (temp1))
1662 #else
1663                    && (XEXP (SET_SRC (PATTERN (temp)), 0)
1664                        == SET_DEST (PATTERN (temp1)))
1665 #endif
1666                    && CONSTANT_P (SET_SRC (PATTERN (temp1)))
1667                    /* If this isn't true, cse will do the job.  */
1668                    && ! no_labels_between_p (temp1, temp))
1669             {
1670               /* Get the if_then_else from the condjump.  */
1671               rtx choice = SET_SRC (PATTERN (insn));
1672               if (GET_CODE (choice) == IF_THEN_ELSE
1673                   && (GET_CODE (XEXP (choice, 0)) == EQ
1674                       || GET_CODE (XEXP (choice, 0)) == NE))
1675                 {
1676                   int want_nonzero = (GET_CODE (XEXP (choice, 0)) == NE);
1677                   rtx last_insn;
1678                   rtx ultimate;
1679                   rtx p;
1680
1681                   /* Get the place that condjump will jump to
1682                      if it is reached from here.  */
1683                   if ((SET_SRC (PATTERN (temp1)) != const0_rtx)
1684                       == want_nonzero)
1685                     ultimate = XEXP (choice, 1);
1686                   else
1687                     ultimate = XEXP (choice, 2);
1688                   /* Get it as a CODE_LABEL.  */
1689                   if (ultimate == pc_rtx)
1690                     ultimate = get_label_after (insn);
1691                   else
1692                     /* Get the label out of the LABEL_REF.  */
1693                     ultimate = XEXP (ultimate, 0);
1694
1695                   /* Insert the jump immediately before TEMP, specifically
1696                      after the label that is between TEMP1 and TEMP.  */
1697                   last_insn = PREV_INSN (temp);
1698
1699                   /* If we would be branching to the next insn, the jump
1700                      would immediately be deleted and the re-inserted in
1701                      a subsequent pass over the code.  So don't do anything
1702                      in that case.  */
1703                   if (next_active_insn (last_insn)
1704                       != next_active_insn (ultimate))
1705                     {
1706                       emit_barrier_after (last_insn);
1707                       p = emit_jump_insn_after (gen_jump (ultimate),
1708                                                 last_insn);
1709                       JUMP_LABEL (p) = ultimate;
1710                       ++LABEL_NUSES (ultimate);
1711                       if (INSN_UID (ultimate) < max_jump_chain
1712                           && INSN_CODE (p) < max_jump_chain)
1713                         {
1714                           jump_chain[INSN_UID (p)]
1715                             = jump_chain[INSN_UID (ultimate)];
1716                           jump_chain[INSN_UID (ultimate)] = p;
1717                         }
1718                       changed = 1;
1719                       continue;
1720                     }
1721                 }
1722             }
1723 #endif
1724           /* Detect a conditional jump going to the same place
1725              as an immediately following unconditional jump.  */
1726           else if (this_is_condjump
1727                    && (temp = next_active_insn (insn)) != 0
1728                    && simplejump_p (temp)
1729                    && (next_active_insn (JUMP_LABEL (insn))
1730                        == next_active_insn (JUMP_LABEL (temp))))
1731             {
1732               delete_jump (insn);
1733               changed = 1;
1734               continue;
1735             }
1736           /* Detect a conditional jump jumping over an unconditional jump.  */
1737
1738           else if ((this_is_condjump || this_is_condjump_in_parallel)
1739                    && ! this_is_simplejump
1740                    && reallabelprev != 0
1741                    && GET_CODE (reallabelprev) == JUMP_INSN
1742                    && prev_active_insn (reallabelprev) == insn
1743                    && no_labels_between_p (insn, reallabelprev)
1744                    && simplejump_p (reallabelprev))
1745             {
1746               /* When we invert the unconditional jump, we will be
1747                  decrementing the usage count of its old label.
1748                  Make sure that we don't delete it now because that
1749                  might cause the following code to be deleted.  */
1750               rtx prev_uses = prev_nonnote_insn (reallabelprev);
1751               rtx prev_label = JUMP_LABEL (insn);
1752
1753               if (prev_label)
1754                 ++LABEL_NUSES (prev_label);
1755
1756               if (invert_jump (insn, JUMP_LABEL (reallabelprev)))
1757                 {
1758                   /* It is very likely that if there are USE insns before
1759                      this jump, they hold REG_DEAD notes.  These REG_DEAD
1760                      notes are no longer valid due to this optimization,
1761                      and will cause the life-analysis that following passes
1762                      (notably delayed-branch scheduling) to think that
1763                      these registers are dead when they are not.
1764
1765                      To prevent this trouble, we just remove the USE insns
1766                      from the insn chain.  */
1767
1768                   while (prev_uses && GET_CODE (prev_uses) == INSN
1769                          && GET_CODE (PATTERN (prev_uses)) == USE)
1770                     {
1771                       rtx useless = prev_uses;
1772                       prev_uses = prev_nonnote_insn (prev_uses);
1773                       delete_insn (useless);
1774                     }
1775
1776                   delete_insn (reallabelprev);
1777                   next = insn;
1778                   changed = 1;
1779                 }
1780
1781               /* We can now safely delete the label if it is unreferenced
1782                  since the delete_insn above has deleted the BARRIER.  */
1783               if (prev_label && --LABEL_NUSES (prev_label) == 0)
1784                 delete_insn (prev_label);
1785               continue;
1786             }
1787           else
1788             {
1789               /* Detect a jump to a jump.  */
1790
1791               nlabel = follow_jumps (JUMP_LABEL (insn));
1792               if (nlabel != JUMP_LABEL (insn)
1793                   && redirect_jump (insn, nlabel))
1794                 {
1795                   changed = 1;
1796                   next = insn;
1797                 }
1798
1799               /* Look for   if (foo) bar; else break;  */
1800               /* The insns look like this:
1801                  insn = condjump label1;
1802                  ...range1 (some insns)...
1803                  jump label2;
1804                  label1:
1805                  ...range2 (some insns)...
1806                  jump somewhere unconditionally
1807                  label2:  */
1808               {
1809                 rtx label1 = next_label (insn);
1810                 rtx range1end = label1 ? prev_active_insn (label1) : 0;
1811                 /* Don't do this optimization on the first round, so that
1812                    jump-around-a-jump gets simplified before we ask here
1813                    whether a jump is unconditional.
1814
1815                    Also don't do it when we are called after reload since
1816                    it will confuse reorg.  */
1817                 if (! first
1818                     && (reload_completed ? ! flag_delayed_branch : 1)
1819                     /* Make sure INSN is something we can invert.  */
1820                     && condjump_p (insn)
1821                     && label1 != 0
1822                     && JUMP_LABEL (insn) == label1
1823                     && LABEL_NUSES (label1) == 1
1824                     && GET_CODE (range1end) == JUMP_INSN
1825                     && simplejump_p (range1end))
1826                   {
1827                     rtx label2 = next_label (label1);
1828                     rtx range2end = label2 ? prev_active_insn (label2) : 0;
1829                     if (range1end != range2end
1830                         && JUMP_LABEL (range1end) == label2
1831                         && GET_CODE (range2end) == JUMP_INSN
1832                         && GET_CODE (NEXT_INSN (range2end)) == BARRIER
1833                         /* Invert the jump condition, so we
1834                            still execute the same insns in each case.  */
1835                         && invert_jump (insn, label1))
1836                       {
1837                         rtx range1beg = next_active_insn (insn);
1838                         rtx range2beg = next_active_insn (label1);
1839                         rtx range1after, range2after;
1840                         rtx range1before, range2before;
1841                         rtx rangenext;
1842
1843                         /* Include in each range any notes before it, to be
1844                            sure that we get the line number note if any, even
1845                            if there are other notes here.  */
1846                         while (PREV_INSN (range1beg)
1847                                && GET_CODE (PREV_INSN (range1beg)) == NOTE)
1848                           range1beg = PREV_INSN (range1beg);
1849
1850                         while (PREV_INSN (range2beg)
1851                                && GET_CODE (PREV_INSN (range2beg)) == NOTE)
1852                           range2beg = PREV_INSN (range2beg);
1853
1854                         /* Don't move NOTEs for blocks or loops; shift them
1855                            outside the ranges, where they'll stay put.  */
1856                         range1beg = squeeze_notes (range1beg, range1end);
1857                         range2beg = squeeze_notes (range2beg, range2end);
1858
1859                         /* Get current surrounds of the 2 ranges.  */
1860                         range1before = PREV_INSN (range1beg);
1861                         range2before = PREV_INSN (range2beg);
1862                         range1after = NEXT_INSN (range1end);
1863                         range2after = NEXT_INSN (range2end);
1864
1865                         /* Splice range2 where range1 was.  */
1866                         NEXT_INSN (range1before) = range2beg;
1867                         PREV_INSN (range2beg) = range1before;
1868                         NEXT_INSN (range2end) = range1after;
1869                         PREV_INSN (range1after) = range2end;
1870                         /* Splice range1 where range2 was.  */
1871                         NEXT_INSN (range2before) = range1beg;
1872                         PREV_INSN (range1beg) = range2before;
1873                         NEXT_INSN (range1end) = range2after;
1874                         PREV_INSN (range2after) = range1end;
1875
1876                         /* Check for a loop end note between the end of
1877                            range2, and the next code label.  If there is one,
1878                            then what we have really seen is
1879                            if (foo) break; end_of_loop;
1880                            and moved the break sequence outside the loop.
1881                            We must move the LOOP_END note to where the
1882                            loop really ends now, or we will confuse loop
1883                            optimization.  Stop if we find a LOOP_BEG note
1884                            first, since we don't want to move the LOOP_END
1885                            note in that case.  */
1886                         for (;range2after != label2; range2after = rangenext)
1887                           {
1888                             rangenext = NEXT_INSN (range2after);
1889                             if (GET_CODE (range2after) == NOTE)
1890                               {
1891                                 if (NOTE_LINE_NUMBER (range2after)
1892                                     == NOTE_INSN_LOOP_END)
1893                                   {
1894                                     NEXT_INSN (PREV_INSN (range2after))
1895                                       = rangenext;
1896                                     PREV_INSN (rangenext)
1897                                       = PREV_INSN (range2after);
1898                                     PREV_INSN (range2after) 
1899                                       = PREV_INSN (range1beg);
1900                                     NEXT_INSN (range2after) = range1beg;
1901                                     NEXT_INSN (PREV_INSN (range1beg))
1902                                       = range2after;
1903                                     PREV_INSN (range1beg) = range2after;
1904                                   }
1905                                 else if (NOTE_LINE_NUMBER (range2after)
1906                                          == NOTE_INSN_LOOP_BEG)
1907                                   break;
1908                               }
1909                           }
1910                         changed = 1;
1911                         continue;
1912                       }
1913                   }
1914               }
1915
1916               /* Now that the jump has been tensioned,
1917                  try cross jumping: check for identical code
1918                  before the jump and before its target label. */
1919
1920               /* First, cross jumping of conditional jumps:  */
1921
1922               if (cross_jump && condjump_p (insn))
1923                 {
1924                   rtx newjpos, newlpos;
1925                   rtx x = prev_real_insn (JUMP_LABEL (insn));
1926
1927                   /* A conditional jump may be crossjumped
1928                      only if the place it jumps to follows
1929                      an opposing jump that comes back here.  */
1930
1931                   if (x != 0 && ! jump_back_p (x, insn))
1932                     /* We have no opposing jump;
1933                        cannot cross jump this insn.  */
1934                     x = 0;
1935
1936                   newjpos = 0;
1937                   /* TARGET is nonzero if it is ok to cross jump
1938                      to code before TARGET.  If so, see if matches.  */
1939                   if (x != 0)
1940                     find_cross_jump (insn, x, 2,
1941                                      &newjpos, &newlpos);
1942
1943                   if (newjpos != 0)
1944                     {
1945                       do_cross_jump (insn, newjpos, newlpos);
1946                       /* Make the old conditional jump
1947                          into an unconditional one.  */
1948                       SET_SRC (PATTERN (insn))
1949                         = gen_rtx (LABEL_REF, VOIDmode, JUMP_LABEL (insn));
1950                       INSN_CODE (insn) = -1;
1951                       emit_barrier_after (insn);
1952                       /* Add to jump_chain unless this is a new label
1953                          whose UID is too large. */
1954                       if (INSN_UID (JUMP_LABEL (insn)) < max_jump_chain)
1955                         {
1956                           jump_chain[INSN_UID (insn)]
1957                             = jump_chain[INSN_UID (JUMP_LABEL (insn))];
1958                           jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
1959                         }
1960                       changed = 1;
1961                       next = insn;
1962                     }
1963                 }
1964
1965               /* Cross jumping of unconditional jumps:
1966                  a few differences.  */
1967
1968               if (cross_jump && simplejump_p (insn))
1969                 {
1970                   rtx newjpos, newlpos;
1971                   rtx target;
1972
1973                   newjpos = 0;
1974
1975                   /* TARGET is nonzero if it is ok to cross jump
1976                      to code before TARGET.  If so, see if matches.  */
1977                   find_cross_jump (insn, JUMP_LABEL (insn), 1,
1978                                    &newjpos, &newlpos);
1979
1980                   /* If cannot cross jump to code before the label,
1981                      see if we can cross jump to another jump to
1982                      the same label.  */
1983                   /* Try each other jump to this label.  */
1984                   if (INSN_UID (JUMP_LABEL (insn)) < max_uid)
1985                     for (target = jump_chain[INSN_UID (JUMP_LABEL (insn))];
1986                          target != 0 && newjpos == 0;
1987                          target = jump_chain[INSN_UID (target)])
1988                       if (target != insn
1989                           && JUMP_LABEL (target) == JUMP_LABEL (insn)
1990                           /* Ignore TARGET if it's deleted.  */
1991                           && ! INSN_DELETED_P (target))
1992                         find_cross_jump (insn, target, 2,
1993                                          &newjpos, &newlpos);
1994
1995                   if (newjpos != 0)
1996                     {
1997                       do_cross_jump (insn, newjpos, newlpos);
1998                       changed = 1;
1999                       next = insn;
2000                     }
2001                 }
2002
2003               /* This code was dead in the previous jump.c!  */
2004               if (cross_jump && GET_CODE (PATTERN (insn)) == RETURN)
2005                 {
2006                   /* Return insns all "jump to the same place"
2007                      so we can cross-jump between any two of them.  */
2008
2009                   rtx newjpos, newlpos, target;
2010
2011                   newjpos = 0;
2012
2013                   /* If cannot cross jump to code before the label,
2014                      see if we can cross jump to another jump to
2015                      the same label.  */
2016                   /* Try each other jump to this label.  */
2017                   for (target = jump_chain[0];
2018                        target != 0 && newjpos == 0;
2019                        target = jump_chain[INSN_UID (target)])
2020                     if (target != insn
2021                         && ! INSN_DELETED_P (target)
2022                         && GET_CODE (PATTERN (target)) == RETURN)
2023                       find_cross_jump (insn, target, 2,
2024                                        &newjpos, &newlpos);
2025
2026                   if (newjpos != 0)
2027                     {
2028                       do_cross_jump (insn, newjpos, newlpos);
2029                       changed = 1;
2030                       next = insn;
2031                     }
2032                 }
2033             }
2034         }
2035
2036       first = 0;
2037     }
2038
2039   /* Delete extraneous line number notes.
2040      Note that two consecutive notes for different lines are not really
2041      extraneous.  There should be some indication where that line belonged,
2042      even if it became empty.  */
2043
2044   {
2045     rtx last_note = 0;
2046
2047     for (insn = f; insn; insn = NEXT_INSN (insn))
2048       if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0)
2049         {
2050           /* Delete this note if it is identical to previous note.  */
2051           if (last_note
2052               && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
2053               && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
2054             {
2055               delete_insn (insn);
2056               continue;
2057             }
2058
2059           last_note = insn;
2060         }
2061   }
2062
2063 #ifdef HAVE_return
2064   if (HAVE_return)
2065     {
2066       /* If we fall through to the epilogue, see if we can insert a RETURN insn
2067          in front of it.  If the machine allows it at this point (we might be
2068          after reload for a leaf routine), it will improve optimization for it
2069          to be there.  We do this both here and at the start of this pass since
2070          the RETURN might have been deleted by some of our optimizations.  */
2071       insn = get_last_insn ();
2072       while (insn && GET_CODE (insn) == NOTE)
2073         insn = PREV_INSN (insn);
2074
2075       if (insn && GET_CODE (insn) != BARRIER)
2076         {
2077           emit_jump_insn (gen_return ());
2078           emit_barrier ();
2079         }
2080     }
2081 #endif
2082
2083   /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
2084      If so, delete it, and record that this function can drop off the end.  */
2085
2086   insn = last_insn;
2087   {
2088     int n_labels = 1;
2089     while (insn
2090            /* One label can follow the end-note: the return label.  */
2091            && ((GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
2092                /* Ordinary insns can follow it if returning a structure.  */
2093                || GET_CODE (insn) == INSN
2094                /* If machine uses explicit RETURN insns, no epilogue,
2095                   then one of them follows the note.  */
2096                || (GET_CODE (insn) == JUMP_INSN
2097                    && GET_CODE (PATTERN (insn)) == RETURN)
2098                /* A barrier can follow the return insn.  */
2099                || GET_CODE (insn) == BARRIER
2100                /* Other kinds of notes can follow also.  */
2101                || (GET_CODE (insn) == NOTE
2102                    && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)))
2103       insn = PREV_INSN (insn);
2104   }
2105
2106   /* Report if control can fall through at the end of the function.  */
2107   if (insn && GET_CODE (insn) == NOTE
2108       && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END)
2109     {
2110       can_reach_end = 1;
2111       delete_insn (insn);
2112     }
2113
2114   /* Show JUMP_CHAIN no longer valid.  */
2115   jump_chain = 0;
2116 }
2117 \f
2118 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
2119    jump.  Assume that this unconditional jump is to the exit test code.  If
2120    the code is sufficiently simple, make a copy of it before INSN,
2121    followed by a jump to the exit of the loop.  Then delete the unconditional
2122    jump after INSN.
2123
2124    Note that it is possible we can get confused here if the jump immediately
2125    after the loop start branches outside the loop but within an outer loop.
2126    If we are near the exit of that loop, we will copy its exit test.  This
2127    will not generate incorrect code, but could suppress some optimizations.
2128    However, such cases are degenerate loops anyway.
2129
2130    Return 1 if we made the change, else 0.
2131
2132    This is only safe immediately after a regscan pass because it uses the
2133    values of regno_first_uid and regno_last_uid.  */
2134
2135 static int
2136 duplicate_loop_exit_test (loop_start)
2137      rtx loop_start;
2138 {
2139   rtx insn, set, reg, p, link;
2140   rtx copy = 0;
2141   int num_insns = 0;
2142   rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
2143   rtx lastexit;
2144   int max_reg = max_reg_num ();
2145   rtx *reg_map = 0;
2146
2147   /* Scan the exit code.  We do not perform this optimization if any insn:
2148
2149          is a CALL_INSN
2150          is a CODE_LABEL
2151          has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
2152          is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
2153          is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
2154               are not valid
2155
2156      Also, don't do this if the exit code is more than 20 insns.  */
2157
2158   for (insn = exitcode;
2159        insn
2160        && ! (GET_CODE (insn) == NOTE
2161              && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
2162        insn = NEXT_INSN (insn))
2163     {
2164       switch (GET_CODE (insn))
2165         {
2166         case CODE_LABEL:
2167         case CALL_INSN:
2168           return 0;
2169         case NOTE:
2170           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2171               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2172               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
2173             return 0;
2174           break;
2175         case JUMP_INSN:
2176         case INSN:
2177           if (++num_insns > 20
2178               || find_reg_note (insn, REG_RETVAL, NULL_RTX)
2179               || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2180             return 0;
2181           break;
2182         }
2183     }
2184
2185   /* Unless INSN is zero, we can do the optimization.  */
2186   if (insn == 0)
2187     return 0;
2188
2189   lastexit = insn;
2190
2191   /* See if any insn sets a register only used in the loop exit code and
2192      not a user variable.  If so, replace it with a new register.  */
2193   for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2194     if (GET_CODE (insn) == INSN
2195         && (set = single_set (insn)) != 0
2196         && ((reg = SET_DEST (set), GET_CODE (reg) == REG)
2197             || (GET_CODE (reg) == SUBREG
2198                 && (reg = SUBREG_REG (reg), GET_CODE (reg) == REG)))
2199         && REGNO (reg) >= FIRST_PSEUDO_REGISTER
2200         && regno_first_uid[REGNO (reg)] == INSN_UID (insn))
2201       {
2202         for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
2203           if (regno_last_uid[REGNO (reg)] == INSN_UID (p))
2204             break;
2205
2206         if (p != lastexit)
2207           {
2208             /* We can do the replacement.  Allocate reg_map if this is the
2209                first replacement we found.  */
2210             if (reg_map == 0)
2211               {
2212                 reg_map = (rtx *) alloca (max_reg * sizeof (rtx));
2213                 bzero ((char *) reg_map, max_reg * sizeof (rtx));
2214               }
2215
2216             REG_LOOP_TEST_P (reg) = 1;
2217
2218             reg_map[REGNO (reg)] = gen_reg_rtx (GET_MODE (reg));
2219           }
2220       }
2221
2222   /* Now copy each insn.  */
2223   for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2224     switch (GET_CODE (insn))
2225       {
2226       case BARRIER:
2227         copy = emit_barrier_before (loop_start);
2228         break;
2229       case NOTE:
2230         /* Only copy line-number notes.  */
2231         if (NOTE_LINE_NUMBER (insn) >= 0)
2232           {
2233             copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
2234             NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
2235           }
2236         break;
2237
2238       case INSN:
2239         copy = emit_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2240         if (reg_map)
2241           replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2242
2243         mark_jump_label (PATTERN (copy), copy, 0);
2244
2245         /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
2246            make them.  */
2247         for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2248           if (REG_NOTE_KIND (link) != REG_LABEL)
2249             REG_NOTES (copy)
2250               = copy_rtx (gen_rtx (EXPR_LIST, REG_NOTE_KIND (link),
2251                                    XEXP (link, 0), REG_NOTES (copy)));
2252         if (reg_map && REG_NOTES (copy))
2253           replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2254         break;
2255
2256       case JUMP_INSN:
2257         copy = emit_jump_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2258         if (reg_map)
2259           replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2260         mark_jump_label (PATTERN (copy), copy, 0);
2261         if (REG_NOTES (insn))
2262           {
2263             REG_NOTES (copy) = copy_rtx (REG_NOTES (insn));
2264             if (reg_map)
2265               replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2266           }
2267         
2268         /* If this is a simple jump, add it to the jump chain.  */
2269
2270         if (INSN_UID (copy) < max_jump_chain && JUMP_LABEL (copy)
2271             && simplejump_p (copy))
2272           {
2273             jump_chain[INSN_UID (copy)]
2274               = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2275             jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2276           }
2277         break;
2278
2279       default:
2280         abort ();
2281       }
2282
2283   /* Now clean up by emitting a jump to the end label and deleting the jump
2284      at the start of the loop.  */
2285   if (! copy || GET_CODE (copy) != BARRIER)
2286     {
2287       copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
2288                                     loop_start);
2289       mark_jump_label (PATTERN (copy), copy, 0);
2290       if (INSN_UID (copy) < max_jump_chain
2291           && INSN_UID (JUMP_LABEL (copy)) < max_jump_chain)
2292         {
2293           jump_chain[INSN_UID (copy)]
2294             = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2295           jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2296         }
2297       emit_barrier_before (loop_start);
2298     }
2299
2300   /* Mark the exit code as the virtual top of the converted loop.  */
2301   emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
2302
2303   delete_insn (next_nonnote_insn (loop_start));
2304
2305   return 1;
2306 }
2307 \f
2308 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, and
2309    loop-end notes between START and END out before START.  Assume that
2310    END is not such a note.  START may be such a note.  Returns the value
2311    of the new starting insn, which may be different if the original start
2312    was such a note.  */
2313
2314 rtx
2315 squeeze_notes (start, end)
2316      rtx start, end;
2317 {
2318   rtx insn;
2319   rtx next;
2320
2321   for (insn = start; insn != end; insn = next)
2322     {
2323       next = NEXT_INSN (insn);
2324       if (GET_CODE (insn) == NOTE
2325           && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
2326               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2327               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2328               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
2329               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
2330               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
2331         {
2332           if (insn == start)
2333             start = next;
2334           else
2335             {
2336               rtx prev = PREV_INSN (insn);
2337               PREV_INSN (insn) = PREV_INSN (start);
2338               NEXT_INSN (insn) = start;
2339               NEXT_INSN (PREV_INSN (insn)) = insn;
2340               PREV_INSN (NEXT_INSN (insn)) = insn;
2341               NEXT_INSN (prev) = next;
2342               PREV_INSN (next) = prev;
2343             }
2344         }
2345     }
2346
2347   return start;
2348 }
2349 \f
2350 /* Compare the instructions before insn E1 with those before E2
2351    to find an opportunity for cross jumping.
2352    (This means detecting identical sequences of insns followed by
2353    jumps to the same place, or followed by a label and a jump
2354    to that label, and replacing one with a jump to the other.)
2355
2356    Assume E1 is a jump that jumps to label E2
2357    (that is not always true but it might as well be).
2358    Find the longest possible equivalent sequences
2359    and store the first insns of those sequences into *F1 and *F2.
2360    Store zero there if no equivalent preceding instructions are found.
2361
2362    We give up if we find a label in stream 1.
2363    Actually we could transfer that label into stream 2.  */
2364
2365 static void
2366 find_cross_jump (e1, e2, minimum, f1, f2)
2367      rtx e1, e2;
2368      int minimum;
2369      rtx *f1, *f2;
2370 {
2371   register rtx i1 = e1, i2 = e2;
2372   register rtx p1, p2;
2373   int lose = 0;
2374
2375   rtx last1 = 0, last2 = 0;
2376   rtx afterlast1 = 0, afterlast2 = 0;
2377   rtx prev1;
2378
2379   *f1 = 0;
2380   *f2 = 0;
2381
2382   while (1)
2383     {
2384       i1 = prev_nonnote_insn (i1);
2385
2386       i2 = PREV_INSN (i2);
2387       while (i2 && (GET_CODE (i2) == NOTE || GET_CODE (i2) == CODE_LABEL))
2388         i2 = PREV_INSN (i2);
2389
2390       if (i1 == 0)
2391         break;
2392
2393       /* Don't allow the range of insns preceding E1 or E2
2394          to include the other (E2 or E1).  */
2395       if (i2 == e1 || i1 == e2)
2396         break;
2397
2398       /* If we will get to this code by jumping, those jumps will be
2399          tensioned to go directly to the new label (before I2),
2400          so this cross-jumping won't cost extra.  So reduce the minimum.  */
2401       if (GET_CODE (i1) == CODE_LABEL)
2402         {
2403           --minimum;
2404           break;
2405         }
2406
2407       if (i2 == 0 || GET_CODE (i1) != GET_CODE (i2))
2408         break;
2409
2410       p1 = PATTERN (i1);
2411       p2 = PATTERN (i2);
2412         
2413       /* If this is a CALL_INSN, compare register usage information.
2414          If we don't check this on stack register machines, the two
2415          CALL_INSNs might be merged leaving reg-stack.c with mismatching
2416          numbers of stack registers in the same basic block.
2417          If we don't check this on machines with delay slots, a delay slot may
2418          be filled that clobbers a parameter expected by the subroutine.
2419
2420          ??? We take the simple route for now and assume that if they're
2421          equal, they were constructed identically.  */
2422
2423       if (GET_CODE (i1) == CALL_INSN
2424           && ! rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
2425                             CALL_INSN_FUNCTION_USAGE (i2)))
2426         lose = 1;
2427
2428 #ifdef STACK_REGS
2429       /* If cross_jump_death_matters is not 0, the insn's mode
2430          indicates whether or not the insn contains any stack-like
2431          regs. */
2432
2433       if (!lose && cross_jump_death_matters && GET_MODE (i1) == QImode)
2434         {
2435           /* If register stack conversion has already been done, then
2436              death notes must also be compared before it is certain that
2437              the two instruction streams match. */
2438
2439           rtx note;
2440           HARD_REG_SET i1_regset, i2_regset;
2441
2442           CLEAR_HARD_REG_SET (i1_regset);
2443           CLEAR_HARD_REG_SET (i2_regset);
2444
2445           for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
2446             if (REG_NOTE_KIND (note) == REG_DEAD
2447                 && STACK_REG_P (XEXP (note, 0)))
2448               SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
2449
2450           for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
2451             if (REG_NOTE_KIND (note) == REG_DEAD
2452                 && STACK_REG_P (XEXP (note, 0)))
2453               SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
2454
2455           GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
2456
2457           lose = 1;
2458
2459         done:
2460           ;
2461         }
2462 #endif
2463
2464       if (lose  || GET_CODE (p1) != GET_CODE (p2)
2465           || ! rtx_renumbered_equal_p (p1, p2))
2466         {
2467           /* The following code helps take care of G++ cleanups.  */
2468           rtx equiv1;
2469           rtx equiv2;
2470
2471           if (!lose && GET_CODE (p1) == GET_CODE (p2)
2472               && ((equiv1 = find_reg_note (i1, REG_EQUAL, NULL_RTX)) != 0
2473                   || (equiv1 = find_reg_note (i1, REG_EQUIV, NULL_RTX)) != 0)
2474               && ((equiv2 = find_reg_note (i2, REG_EQUAL, NULL_RTX)) != 0
2475                   || (equiv2 = find_reg_note (i2, REG_EQUIV, NULL_RTX)) != 0)
2476               /* If the equivalences are not to a constant, they may
2477                  reference pseudos that no longer exist, so we can't
2478                  use them.  */
2479               && CONSTANT_P (XEXP (equiv1, 0))
2480               && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
2481             {
2482               rtx s1 = single_set (i1);
2483               rtx s2 = single_set (i2);
2484               if (s1 != 0 && s2 != 0
2485                   && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
2486                 {
2487                   validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
2488                   validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
2489                   if (! rtx_renumbered_equal_p (p1, p2))
2490                     cancel_changes (0);
2491                   else if (apply_change_group ())
2492                     goto win;
2493                 }
2494             }
2495
2496           /* Insns fail to match; cross jumping is limited to the following
2497              insns.  */
2498
2499 #ifdef HAVE_cc0
2500           /* Don't allow the insn after a compare to be shared by
2501              cross-jumping unless the compare is also shared.
2502              Here, if either of these non-matching insns is a compare,
2503              exclude the following insn from possible cross-jumping.  */
2504           if (sets_cc0_p (p1) || sets_cc0_p (p2))
2505             last1 = afterlast1, last2 = afterlast2, ++minimum;
2506 #endif
2507
2508           /* If cross-jumping here will feed a jump-around-jump
2509              optimization, this jump won't cost extra, so reduce
2510              the minimum.  */
2511           if (GET_CODE (i1) == JUMP_INSN
2512               && JUMP_LABEL (i1)
2513               && prev_real_insn (JUMP_LABEL (i1)) == e1)
2514             --minimum;
2515           break;
2516         }
2517
2518     win:
2519       if (GET_CODE (p1) != USE && GET_CODE (p1) != CLOBBER)
2520         {
2521           /* Ok, this insn is potentially includable in a cross-jump here.  */
2522           afterlast1 = last1, afterlast2 = last2;
2523           last1 = i1, last2 = i2, --minimum;
2524         }
2525     }
2526
2527   if (minimum <= 0 && last1 != 0 && last1 != e1)
2528     *f1 = last1, *f2 = last2;
2529 }
2530
2531 static void
2532 do_cross_jump (insn, newjpos, newlpos)
2533      rtx insn, newjpos, newlpos;
2534 {
2535   /* Find an existing label at this point
2536      or make a new one if there is none.  */
2537   register rtx label = get_label_before (newlpos);
2538
2539   /* Make the same jump insn jump to the new point.  */
2540   if (GET_CODE (PATTERN (insn)) == RETURN)
2541     {
2542       /* Remove from jump chain of returns.  */
2543       delete_from_jump_chain (insn);
2544       /* Change the insn.  */
2545       PATTERN (insn) = gen_jump (label);
2546       INSN_CODE (insn) = -1;
2547       JUMP_LABEL (insn) = label;
2548       LABEL_NUSES (label)++;
2549       /* Add to new the jump chain.  */
2550       if (INSN_UID (label) < max_jump_chain
2551           && INSN_UID (insn) < max_jump_chain)
2552         {
2553           jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (label)];
2554           jump_chain[INSN_UID (label)] = insn;
2555         }
2556     }
2557   else
2558     redirect_jump (insn, label);
2559
2560   /* Delete the matching insns before the jump.  Also, remove any REG_EQUAL
2561      or REG_EQUIV note in the NEWLPOS stream that isn't also present in
2562      the NEWJPOS stream.  */
2563
2564   while (newjpos != insn)
2565     {
2566       rtx lnote;
2567
2568       for (lnote = REG_NOTES (newlpos); lnote; lnote = XEXP (lnote, 1))
2569         if ((REG_NOTE_KIND (lnote) == REG_EQUAL
2570              || REG_NOTE_KIND (lnote) == REG_EQUIV)
2571             && ! find_reg_note (newjpos, REG_EQUAL, XEXP (lnote, 0))
2572             && ! find_reg_note (newjpos, REG_EQUIV, XEXP (lnote, 0)))
2573           remove_note (newlpos, lnote);
2574
2575       delete_insn (newjpos);
2576       newjpos = next_real_insn (newjpos);
2577       newlpos = next_real_insn (newlpos);
2578     }
2579 }
2580 \f
2581 /* Return the label before INSN, or put a new label there.  */
2582
2583 rtx
2584 get_label_before (insn)
2585      rtx insn;
2586 {
2587   rtx label;
2588
2589   /* Find an existing label at this point
2590      or make a new one if there is none.  */
2591   label = prev_nonnote_insn (insn);
2592
2593   if (label == 0 || GET_CODE (label) != CODE_LABEL)
2594     {
2595       rtx prev = PREV_INSN (insn);
2596
2597       label = gen_label_rtx ();
2598       emit_label_after (label, prev);
2599       LABEL_NUSES (label) = 0;
2600     }
2601   return label;
2602 }
2603
2604 /* Return the label after INSN, or put a new label there.  */
2605
2606 rtx
2607 get_label_after (insn)
2608      rtx insn;
2609 {
2610   rtx label;
2611
2612   /* Find an existing label at this point
2613      or make a new one if there is none.  */
2614   label = next_nonnote_insn (insn);
2615
2616   if (label == 0 || GET_CODE (label) != CODE_LABEL)
2617     {
2618       label = gen_label_rtx ();
2619       emit_label_after (label, insn);
2620       LABEL_NUSES (label) = 0;
2621     }
2622   return label;
2623 }
2624 \f
2625 /* Return 1 if INSN is a jump that jumps to right after TARGET
2626    only on the condition that TARGET itself would drop through.
2627    Assumes that TARGET is a conditional jump.  */
2628
2629 static int
2630 jump_back_p (insn, target)
2631      rtx insn, target;
2632 {
2633   rtx cinsn, ctarget;
2634   enum rtx_code codei, codet;
2635
2636   if (simplejump_p (insn) || ! condjump_p (insn)
2637       || simplejump_p (target)
2638       || target != prev_real_insn (JUMP_LABEL (insn)))
2639     return 0;
2640
2641   cinsn = XEXP (SET_SRC (PATTERN (insn)), 0);
2642   ctarget = XEXP (SET_SRC (PATTERN (target)), 0);
2643
2644   codei = GET_CODE (cinsn);
2645   codet = GET_CODE (ctarget);
2646
2647   if (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx)
2648     {
2649       if (! can_reverse_comparison_p (cinsn, insn))
2650         return 0;
2651       codei = reverse_condition (codei);
2652     }
2653
2654   if (XEXP (SET_SRC (PATTERN (target)), 2) == pc_rtx)
2655     {
2656       if (! can_reverse_comparison_p (ctarget, target))
2657         return 0;
2658       codet = reverse_condition (codet);
2659     }
2660
2661   return (codei == codet
2662           && rtx_renumbered_equal_p (XEXP (cinsn, 0), XEXP (ctarget, 0))
2663           && rtx_renumbered_equal_p (XEXP (cinsn, 1), XEXP (ctarget, 1)));
2664 }
2665 \f
2666 /* Given a comparison, COMPARISON, inside a conditional jump insn, INSN,
2667    return non-zero if it is safe to reverse this comparison.  It is if our
2668    floating-point is not IEEE, if this is an NE or EQ comparison, or if
2669    this is known to be an integer comparison.  */
2670
2671 int
2672 can_reverse_comparison_p (comparison, insn)
2673      rtx comparison;
2674      rtx insn;
2675 {
2676   rtx arg0;
2677
2678   /* If this is not actually a comparison, we can't reverse it.  */
2679   if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
2680     return 0;
2681
2682   if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
2683       /* If this is an NE comparison, it is safe to reverse it to an EQ
2684          comparison and vice versa, even for floating point.  If no operands
2685          are NaNs, the reversal is valid.  If some operand is a NaN, EQ is
2686          always false and NE is always true, so the reversal is also valid.  */
2687       || flag_fast_math
2688       || GET_CODE (comparison) == NE
2689       || GET_CODE (comparison) == EQ)
2690     return 1;
2691
2692   arg0 = XEXP (comparison, 0);
2693
2694   /* Make sure ARG0 is one of the actual objects being compared.  If we
2695      can't do this, we can't be sure the comparison can be reversed. 
2696
2697      Handle cc0 and a MODE_CC register.  */
2698   if ((GET_CODE (arg0) == REG && GET_MODE_CLASS (GET_MODE (arg0)) == MODE_CC)
2699 #ifdef HAVE_cc0
2700       || arg0 == cc0_rtx
2701 #endif
2702       )
2703     {
2704       rtx prev = prev_nonnote_insn (insn);
2705       rtx set = single_set (prev);
2706
2707       if (set == 0 || SET_DEST (set) != arg0)
2708         return 0;
2709
2710       arg0 = SET_SRC (set);
2711
2712       if (GET_CODE (arg0) == COMPARE)
2713         arg0 = XEXP (arg0, 0);
2714     }
2715
2716   /* We can reverse this if ARG0 is a CONST_INT or if its mode is
2717      not VOIDmode and neither a MODE_CC nor MODE_FLOAT type.  */
2718   return (GET_CODE (arg0) == CONST_INT
2719           || (GET_MODE (arg0) != VOIDmode
2720               && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_CC
2721               && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_FLOAT));
2722 }
2723
2724 /* Given an rtx-code for a comparison, return the code
2725    for the negated comparison.
2726    WATCH OUT!  reverse_condition is not safe to use on a jump
2727    that might be acting on the results of an IEEE floating point comparison,
2728    because of the special treatment of non-signaling nans in comparisons.  
2729    Use can_reverse_comparison_p to be sure.  */
2730
2731 enum rtx_code
2732 reverse_condition (code)
2733      enum rtx_code code;
2734 {
2735   switch (code)
2736     {
2737     case EQ:
2738       return NE;
2739
2740     case NE:
2741       return EQ;
2742
2743     case GT:
2744       return LE;
2745
2746     case GE:
2747       return LT;
2748
2749     case LT:
2750       return GE;
2751
2752     case LE:
2753       return GT;
2754
2755     case GTU:
2756       return LEU;
2757
2758     case GEU:
2759       return LTU;
2760
2761     case LTU:
2762       return GEU;
2763
2764     case LEU:
2765       return GTU;
2766
2767     default:
2768       abort ();
2769       return UNKNOWN;
2770     }
2771 }
2772
2773 /* Similar, but return the code when two operands of a comparison are swapped.
2774    This IS safe for IEEE floating-point.  */
2775
2776 enum rtx_code
2777 swap_condition (code)
2778      enum rtx_code code;
2779 {
2780   switch (code)
2781     {
2782     case EQ:
2783     case NE:
2784       return code;
2785
2786     case GT:
2787       return LT;
2788
2789     case GE:
2790       return LE;
2791
2792     case LT:
2793       return GT;
2794
2795     case LE:
2796       return GE;
2797
2798     case GTU:
2799       return LTU;
2800
2801     case GEU:
2802       return LEU;
2803
2804     case LTU:
2805       return GTU;
2806
2807     case LEU:
2808       return GEU;
2809
2810     default:
2811       abort ();
2812       return UNKNOWN;
2813     }
2814 }
2815
2816 /* Given a comparison CODE, return the corresponding unsigned comparison.
2817    If CODE is an equality comparison or already an unsigned comparison,
2818    CODE is returned.  */
2819
2820 enum rtx_code
2821 unsigned_condition (code)
2822      enum rtx_code code;
2823 {
2824   switch (code)
2825     {
2826     case EQ:
2827     case NE:
2828     case GTU:
2829     case GEU:
2830     case LTU:
2831     case LEU:
2832       return code;
2833
2834     case GT:
2835       return GTU;
2836
2837     case GE:
2838       return GEU;
2839
2840     case LT:
2841       return LTU;
2842
2843     case LE:
2844       return LEU;
2845
2846     default:
2847       abort ();
2848     }
2849 }
2850
2851 /* Similarly, return the signed version of a comparison.  */
2852
2853 enum rtx_code
2854 signed_condition (code)
2855      enum rtx_code code;
2856 {
2857   switch (code)
2858     {
2859     case EQ:
2860     case NE:
2861     case GT:
2862     case GE:
2863     case LT:
2864     case LE:
2865       return code;
2866
2867     case GTU:
2868       return GT;
2869
2870     case GEU:
2871       return GE;
2872
2873     case LTU:
2874       return LT;
2875
2876     case LEU:
2877       return LE;
2878
2879     default:
2880       abort ();
2881     }
2882 }
2883 \f
2884 /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
2885    truth of CODE1 implies the truth of CODE2.  */
2886
2887 int
2888 comparison_dominates_p (code1, code2)
2889      enum rtx_code code1, code2;
2890 {
2891   if (code1 == code2)
2892     return 1;
2893
2894   switch (code1)
2895     {
2896     case EQ:
2897       if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU)
2898         return 1;
2899       break;
2900
2901     case LT:
2902       if (code2 == LE || code2 == NE)
2903         return 1;
2904       break;
2905
2906     case GT:
2907       if (code2 == GE || code2 == NE)
2908         return 1;
2909       break;
2910
2911     case LTU:
2912       if (code2 == LEU || code2 == NE)
2913         return 1;
2914       break;
2915
2916     case GTU:
2917       if (code2 == GEU || code2 == NE)
2918         return 1;
2919       break;
2920     }
2921
2922   return 0;
2923 }
2924 \f
2925 /* Return 1 if INSN is an unconditional jump and nothing else.  */
2926
2927 int
2928 simplejump_p (insn)
2929      rtx insn;
2930 {
2931   return (GET_CODE (insn) == JUMP_INSN
2932           && GET_CODE (PATTERN (insn)) == SET
2933           && GET_CODE (SET_DEST (PATTERN (insn))) == PC
2934           && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
2935 }
2936
2937 /* Return nonzero if INSN is a (possibly) conditional jump
2938    and nothing more.  */
2939
2940 int
2941 condjump_p (insn)
2942      rtx insn;
2943 {
2944   register rtx x = PATTERN (insn);
2945   if (GET_CODE (x) != SET)
2946     return 0;
2947   if (GET_CODE (SET_DEST (x)) != PC)
2948     return 0;
2949   if (GET_CODE (SET_SRC (x)) == LABEL_REF)
2950     return 1;
2951   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
2952     return 0;
2953   if (XEXP (SET_SRC (x), 2) == pc_rtx
2954       && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
2955           || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
2956     return 1;
2957   if (XEXP (SET_SRC (x), 1) == pc_rtx
2958       && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
2959           || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
2960     return 1;
2961   return 0;
2962 }
2963
2964 /* Return nonzero if INSN is a (possibly) conditional jump
2965    and nothing more.  */
2966
2967 int
2968 condjump_in_parallel_p (insn)
2969      rtx insn;
2970 {
2971   register rtx x = PATTERN (insn);
2972
2973   if (GET_CODE (x) != PARALLEL)
2974     return 0;
2975   else
2976     x = XVECEXP (x, 0, 0);
2977
2978   if (GET_CODE (x) != SET)
2979     return 0;
2980   if (GET_CODE (SET_DEST (x)) != PC)
2981     return 0;
2982   if (GET_CODE (SET_SRC (x)) == LABEL_REF)
2983     return 1;
2984   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
2985     return 0;
2986   if (XEXP (SET_SRC (x), 2) == pc_rtx
2987       && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
2988           || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
2989     return 1;
2990   if (XEXP (SET_SRC (x), 1) == pc_rtx
2991       && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
2992           || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
2993     return 1;
2994   return 0;
2995 }
2996
2997 /* Return 1 if X is an RTX that does nothing but set the condition codes
2998    and CLOBBER or USE registers.
2999    Return -1 if X does explicitly set the condition codes,
3000    but also does other things.  */
3001
3002 int
3003 sets_cc0_p (x)
3004      rtx x;
3005 {
3006 #ifdef HAVE_cc0
3007   if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
3008     return 1;
3009   if (GET_CODE (x) == PARALLEL)
3010     {
3011       int i;
3012       int sets_cc0 = 0;
3013       int other_things = 0;
3014       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3015         {
3016           if (GET_CODE (XVECEXP (x, 0, i)) == SET
3017               && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
3018             sets_cc0 = 1;
3019           else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
3020             other_things = 1;
3021         }
3022       return ! sets_cc0 ? 0 : other_things ? -1 : 1;
3023     }
3024   return 0;
3025 #else
3026   abort ();
3027 #endif
3028 }
3029 \f
3030 /* Follow any unconditional jump at LABEL;
3031    return the ultimate label reached by any such chain of jumps.
3032    If LABEL is not followed by a jump, return LABEL.
3033    If the chain loops or we can't find end, return LABEL,
3034    since that tells caller to avoid changing the insn.
3035
3036    If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
3037    a USE or CLOBBER.  */
3038
3039 rtx
3040 follow_jumps (label)
3041      rtx label;
3042 {
3043   register rtx insn;
3044   register rtx next;
3045   register rtx value = label;
3046   register int depth;
3047
3048   for (depth = 0;
3049        (depth < 10
3050         && (insn = next_active_insn (value)) != 0
3051         && GET_CODE (insn) == JUMP_INSN
3052         && (JUMP_LABEL (insn) != 0 || GET_CODE (PATTERN (insn)) == RETURN)
3053         && (next = NEXT_INSN (insn))
3054         && GET_CODE (next) == BARRIER);
3055        depth++)
3056     {
3057       /* Don't chain through the insn that jumps into a loop
3058          from outside the loop,
3059          since that would create multiple loop entry jumps
3060          and prevent loop optimization.  */
3061       rtx tem;
3062       if (!reload_completed)
3063         for (tem = value; tem != insn; tem = NEXT_INSN (tem))
3064           if (GET_CODE (tem) == NOTE
3065               && NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG)
3066             return value;
3067
3068       /* If we have found a cycle, make the insn jump to itself.  */
3069       if (JUMP_LABEL (insn) == label)
3070         return label;
3071
3072       tem = next_active_insn (JUMP_LABEL (insn));
3073       if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
3074                   || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
3075         break;
3076
3077       value = JUMP_LABEL (insn);
3078     }
3079   if (depth == 10)
3080     return label;
3081   return value;
3082 }
3083
3084 /* Assuming that field IDX of X is a vector of label_refs,
3085    replace each of them by the ultimate label reached by it.
3086    Return nonzero if a change is made.
3087    If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG.  */
3088
3089 static int
3090 tension_vector_labels (x, idx)
3091      register rtx x;
3092      register int idx;
3093 {
3094   int changed = 0;
3095   register int i;
3096   for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
3097     {
3098       register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
3099       register rtx nlabel = follow_jumps (olabel);
3100       if (nlabel && nlabel != olabel)
3101         {
3102           XEXP (XVECEXP (x, idx, i), 0) = nlabel;
3103           ++LABEL_NUSES (nlabel);
3104           if (--LABEL_NUSES (olabel) == 0)
3105             delete_insn (olabel);
3106           changed = 1;
3107         }
3108     }
3109   return changed;
3110 }
3111 \f
3112 /* Find all CODE_LABELs referred to in X, and increment their use counts.
3113    If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
3114    in INSN, then store one of them in JUMP_LABEL (INSN).
3115    If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
3116    referenced in INSN, add a REG_LABEL note containing that label to INSN.
3117    Also, when there are consecutive labels, canonicalize on the last of them.
3118
3119    Note that two labels separated by a loop-beginning note
3120    must be kept distinct if we have not yet done loop-optimization,
3121    because the gap between them is where loop-optimize
3122    will want to move invariant code to.  CROSS_JUMP tells us
3123    that loop-optimization is done with.
3124
3125    Once reload has completed (CROSS_JUMP non-zero), we need not consider
3126    two labels distinct if they are separated by only USE or CLOBBER insns.  */
3127
3128 static void
3129 mark_jump_label (x, insn, cross_jump)
3130      register rtx x;
3131      rtx insn;
3132      int cross_jump;
3133 {
3134   register RTX_CODE code = GET_CODE (x);
3135   register int i;
3136   register char *fmt;
3137
3138   switch (code)
3139     {
3140     case PC:
3141     case CC0:
3142     case REG:
3143     case SUBREG:
3144     case CONST_INT:
3145     case SYMBOL_REF:
3146     case CONST_DOUBLE:
3147     case CLOBBER:
3148     case CALL:
3149       return;
3150
3151     case MEM:
3152       /* If this is a constant-pool reference, see if it is a label.  */
3153       if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3154           && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3155         mark_jump_label (get_pool_constant (XEXP (x, 0)), insn, cross_jump);
3156       break;
3157
3158     case LABEL_REF:
3159       {
3160         rtx label = XEXP (x, 0);
3161         rtx olabel = label;
3162         rtx note;
3163         rtx next;
3164
3165         if (GET_CODE (label) != CODE_LABEL)
3166           abort ();
3167
3168         /* Ignore references to labels of containing functions.  */
3169         if (LABEL_REF_NONLOCAL_P (x))
3170           break;
3171
3172         /* If there are other labels following this one,
3173            replace it with the last of the consecutive labels.  */
3174         for (next = NEXT_INSN (label); next; next = NEXT_INSN (next))
3175           {
3176             if (GET_CODE (next) == CODE_LABEL)
3177               label = next;
3178             else if (cross_jump && GET_CODE (next) == INSN
3179                      && (GET_CODE (PATTERN (next)) == USE
3180                          || GET_CODE (PATTERN (next)) == CLOBBER))
3181               continue;
3182             else if (GET_CODE (next) != NOTE)
3183               break;
3184             else if (! cross_jump
3185                      && (NOTE_LINE_NUMBER (next) == NOTE_INSN_LOOP_BEG
3186                          || NOTE_LINE_NUMBER (next) == NOTE_INSN_FUNCTION_END))
3187               break;
3188           }
3189
3190         XEXP (x, 0) = label;
3191         ++LABEL_NUSES (label);
3192
3193         if (insn)
3194           {
3195             if (GET_CODE (insn) == JUMP_INSN)
3196               JUMP_LABEL (insn) = label;
3197
3198             /* If we've changed OLABEL and we had a REG_LABEL note
3199                for it, update it as well.  */
3200             else if (label != olabel
3201                      && (note = find_reg_note (insn, REG_LABEL, olabel)) != 0)
3202               XEXP (note, 0) = label;
3203
3204             /* Otherwise, add a REG_LABEL note for LABEL unless there already
3205                is one.  */
3206             else if (! find_reg_note (insn, REG_LABEL, label))
3207               {
3208                 rtx next = next_real_insn (label);
3209                 /* Don't record labels that refer to dispatch tables.
3210                    This is not necessary, since the tablejump
3211                    references the same label.
3212                    And if we did record them, flow.c would make worse code.  */
3213                 if (next == 0
3214                     || ! (GET_CODE (next) == JUMP_INSN
3215                           && (GET_CODE (PATTERN (next)) == ADDR_VEC
3216                               || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
3217                   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, label,
3218                                               REG_NOTES (insn));
3219               }
3220           }
3221         return;
3222       }
3223
3224   /* Do walk the labels in a vector, but not the first operand of an
3225      ADDR_DIFF_VEC.  Don't set the JUMP_LABEL of a vector.  */
3226     case ADDR_VEC:
3227     case ADDR_DIFF_VEC:
3228       {
3229         int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
3230
3231         for (i = 0; i < XVECLEN (x, eltnum); i++)
3232           mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, cross_jump);
3233         return;
3234       }
3235     }
3236
3237   fmt = GET_RTX_FORMAT (code);
3238   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3239     {
3240       if (fmt[i] == 'e')
3241         mark_jump_label (XEXP (x, i), insn, cross_jump);
3242       else if (fmt[i] == 'E')
3243         {
3244           register int j;
3245           for (j = 0; j < XVECLEN (x, i); j++)
3246             mark_jump_label (XVECEXP (x, i, j), insn, cross_jump);
3247         }
3248     }
3249 }
3250
3251 /* If all INSN does is set the pc, delete it,
3252    and delete the insn that set the condition codes for it
3253    if that's what the previous thing was.  */
3254
3255 void
3256 delete_jump (insn)
3257      rtx insn;
3258 {
3259   register rtx set = single_set (insn);
3260
3261   if (set && GET_CODE (SET_DEST (set)) == PC)
3262     delete_computation (insn);
3263 }
3264
3265 /* Delete INSN and recursively delete insns that compute values used only
3266    by INSN.  This uses the REG_DEAD notes computed during flow analysis.
3267    If we are running before flow.c, we need do nothing since flow.c will
3268    delete dead code.  We also can't know if the registers being used are
3269    dead or not at this point.
3270
3271    Otherwise, look at all our REG_DEAD notes.  If a previous insn does
3272    nothing other than set a register that dies in this insn, we can delete
3273    that insn as well.
3274
3275    On machines with CC0, if CC0 is used in this insn, we may be able to
3276    delete the insn that set it.  */
3277
3278 static void
3279 delete_computation (insn)
3280      rtx insn;
3281 {
3282   rtx note, next;
3283
3284 #ifdef HAVE_cc0
3285   if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
3286     {
3287       rtx prev = prev_nonnote_insn (insn);
3288       /* We assume that at this stage
3289          CC's are always set explicitly
3290          and always immediately before the jump that
3291          will use them.  So if the previous insn
3292          exists to set the CC's, delete it
3293          (unless it performs auto-increments, etc.).  */
3294       if (prev && GET_CODE (prev) == INSN
3295           && sets_cc0_p (PATTERN (prev)))
3296         {
3297           if (sets_cc0_p (PATTERN (prev)) > 0
3298               && !FIND_REG_INC_NOTE (prev, NULL_RTX))
3299             delete_computation (prev);
3300           else
3301             /* Otherwise, show that cc0 won't be used.  */
3302             REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_UNUSED,
3303                                         cc0_rtx, REG_NOTES (prev));
3304         }
3305     }
3306 #endif
3307
3308   for (note = REG_NOTES (insn); note; note = next)
3309     {
3310       rtx our_prev;
3311
3312       next = XEXP (note, 1);
3313
3314       if (REG_NOTE_KIND (note) != REG_DEAD
3315           /* Verify that the REG_NOTE is legitimate.  */
3316           || GET_CODE (XEXP (note, 0)) != REG)
3317         continue;
3318
3319       for (our_prev = prev_nonnote_insn (insn);
3320            our_prev && GET_CODE (our_prev) == INSN;
3321            our_prev = prev_nonnote_insn (our_prev))
3322         {
3323           /* If we reach a SEQUENCE, it is too complex to try to
3324              do anything with it, so give up.  */
3325           if (GET_CODE (PATTERN (our_prev)) == SEQUENCE)
3326             break;
3327
3328           if (GET_CODE (PATTERN (our_prev)) == USE
3329               && GET_CODE (XEXP (PATTERN (our_prev), 0)) == INSN)
3330             /* reorg creates USEs that look like this.  We leave them
3331                alone because reorg needs them for its own purposes.  */
3332             break;
3333
3334           if (reg_set_p (XEXP (note, 0), PATTERN (our_prev)))
3335             {
3336               if (FIND_REG_INC_NOTE (our_prev, NULL_RTX))
3337                 break;
3338
3339               if (GET_CODE (PATTERN (our_prev)) == PARALLEL)
3340                 {
3341                   /* If we find a SET of something else, we can't
3342                      delete the insn.  */
3343
3344                   int i;
3345
3346                   for (i = 0; i < XVECLEN (PATTERN (our_prev), 0); i++)
3347                     {
3348                       rtx part = XVECEXP (PATTERN (our_prev), 0, i);
3349
3350                       if (GET_CODE (part) == SET
3351                           && SET_DEST (part) != XEXP (note, 0))
3352                         break;
3353                     }
3354
3355                   if (i == XVECLEN (PATTERN (our_prev), 0))
3356                     delete_computation (our_prev);
3357                 }
3358               else if (GET_CODE (PATTERN (our_prev)) == SET
3359                        && SET_DEST (PATTERN (our_prev)) == XEXP (note, 0))
3360                 delete_computation (our_prev);
3361
3362               break;
3363             }
3364
3365           /* If OUR_PREV references the register that dies here, it is an
3366              additional use.  Hence any prior SET isn't dead.  However, this
3367              insn becomes the new place for the REG_DEAD note.  */
3368           if (reg_overlap_mentioned_p (XEXP (note, 0),
3369                                        PATTERN (our_prev)))
3370             {
3371               XEXP (note, 1) = REG_NOTES (our_prev);
3372               REG_NOTES (our_prev) = note;
3373               break;
3374             }
3375         }
3376     }
3377
3378   delete_insn (insn);
3379 }
3380 \f
3381 /* Delete insn INSN from the chain of insns and update label ref counts.
3382    May delete some following insns as a consequence; may even delete
3383    a label elsewhere and insns that follow it.
3384
3385    Returns the first insn after INSN that was not deleted.  */
3386
3387 rtx
3388 delete_insn (insn)
3389      register rtx insn;
3390 {
3391   register rtx next = NEXT_INSN (insn);
3392   register rtx prev = PREV_INSN (insn);
3393   register int was_code_label = (GET_CODE (insn) == CODE_LABEL);
3394   register int dont_really_delete = 0;
3395
3396   while (next && INSN_DELETED_P (next))
3397     next = NEXT_INSN (next);
3398
3399   /* This insn is already deleted => return first following nondeleted.  */
3400   if (INSN_DELETED_P (insn))
3401     return next;
3402
3403   /* Don't delete user-declared labels.  Convert them to special NOTEs
3404      instead.  */
3405   if (was_code_label && LABEL_NAME (insn) != 0
3406       && optimize && ! dont_really_delete)
3407     {
3408       PUT_CODE (insn, NOTE);
3409       NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
3410       NOTE_SOURCE_FILE (insn) = 0;
3411       dont_really_delete = 1;
3412     }
3413   else
3414     /* Mark this insn as deleted.  */
3415     INSN_DELETED_P (insn) = 1;
3416
3417   /* If this is an unconditional jump, delete it from the jump chain.  */
3418   if (simplejump_p (insn))
3419     delete_from_jump_chain (insn);
3420
3421   /* If instruction is followed by a barrier,
3422      delete the barrier too.  */
3423
3424   if (next != 0 && GET_CODE (next) == BARRIER)
3425     {
3426       INSN_DELETED_P (next) = 1;
3427       next = NEXT_INSN (next);
3428     }
3429
3430   /* Patch out INSN (and the barrier if any) */
3431
3432   if (optimize && ! dont_really_delete)
3433     {
3434       if (prev)
3435         {
3436           NEXT_INSN (prev) = next;
3437           if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
3438             NEXT_INSN (XVECEXP (PATTERN (prev), 0,
3439                                 XVECLEN (PATTERN (prev), 0) - 1)) = next;
3440         }
3441
3442       if (next)
3443         {
3444           PREV_INSN (next) = prev;
3445           if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
3446             PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
3447         }
3448
3449       if (prev && NEXT_INSN (prev) == 0)
3450         set_last_insn (prev);
3451     }
3452
3453   /* If deleting a jump, decrement the count of the label,
3454      and delete the label if it is now unused.  */
3455
3456   if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
3457     if (--LABEL_NUSES (JUMP_LABEL (insn)) == 0)
3458       {
3459         /* This can delete NEXT or PREV,
3460            either directly if NEXT is JUMP_LABEL (INSN),
3461            or indirectly through more levels of jumps.  */
3462         delete_insn (JUMP_LABEL (insn));
3463         /* I feel a little doubtful about this loop,
3464            but I see no clean and sure alternative way
3465            to find the first insn after INSN that is not now deleted.
3466            I hope this works.  */
3467         while (next && INSN_DELETED_P (next))
3468           next = NEXT_INSN (next);
3469         return next;
3470       }
3471
3472   /* Likewise if we're deleting a dispatch table.  */
3473
3474   if (GET_CODE (insn) == JUMP_INSN
3475       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
3476           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
3477     {
3478       rtx pat = PATTERN (insn);
3479       int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
3480       int len = XVECLEN (pat, diff_vec_p);
3481
3482       for (i = 0; i < len; i++)
3483         if (--LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
3484           delete_insn (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
3485       while (next && INSN_DELETED_P (next))
3486         next = NEXT_INSN (next);
3487       return next;
3488     }
3489
3490   while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
3491     prev = PREV_INSN (prev);
3492
3493   /* If INSN was a label and a dispatch table follows it,
3494      delete the dispatch table.  The tablejump must have gone already.
3495      It isn't useful to fall through into a table.  */
3496
3497   if (was_code_label
3498       && NEXT_INSN (insn) != 0
3499       && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
3500       && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
3501           || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
3502     next = delete_insn (NEXT_INSN (insn));
3503
3504   /* If INSN was a label, delete insns following it if now unreachable.  */
3505
3506   if (was_code_label && prev && GET_CODE (prev) == BARRIER)
3507     {
3508       register RTX_CODE code;
3509       while (next != 0
3510              && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
3511                  || code == NOTE || code == BARRIER
3512                  || (code == CODE_LABEL && INSN_DELETED_P (next))))
3513         {
3514           if (code == NOTE
3515               && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
3516             next = NEXT_INSN (next);
3517           /* Keep going past other deleted labels to delete what follows.  */
3518           else if (code == CODE_LABEL && INSN_DELETED_P (next))
3519             next = NEXT_INSN (next);
3520           else
3521             /* Note: if this deletes a jump, it can cause more
3522                deletion of unreachable code, after a different label.
3523                As long as the value from this recursive call is correct,
3524                this invocation functions correctly.  */
3525             next = delete_insn (next);
3526         }
3527     }
3528
3529   return next;
3530 }
3531
3532 /* Advance from INSN till reaching something not deleted
3533    then return that.  May return INSN itself.  */
3534
3535 rtx
3536 next_nondeleted_insn (insn)
3537      rtx insn;
3538 {
3539   while (INSN_DELETED_P (insn))
3540     insn = NEXT_INSN (insn);
3541   return insn;
3542 }
3543 \f
3544 /* Delete a range of insns from FROM to TO, inclusive.
3545    This is for the sake of peephole optimization, so assume
3546    that whatever these insns do will still be done by a new
3547    peephole insn that will replace them.  */
3548
3549 void
3550 delete_for_peephole (from, to)
3551      register rtx from, to;
3552 {
3553   register rtx insn = from;
3554
3555   while (1)
3556     {
3557       register rtx next = NEXT_INSN (insn);
3558       register rtx prev = PREV_INSN (insn);
3559
3560       if (GET_CODE (insn) != NOTE)
3561         {
3562           INSN_DELETED_P (insn) = 1;
3563
3564           /* Patch this insn out of the chain.  */
3565           /* We don't do this all at once, because we
3566              must preserve all NOTEs.  */
3567           if (prev)
3568             NEXT_INSN (prev) = next;
3569
3570           if (next)
3571             PREV_INSN (next) = prev;
3572         }
3573
3574       if (insn == to)
3575         break;
3576       insn = next;
3577     }
3578
3579   /* Note that if TO is an unconditional jump
3580      we *do not* delete the BARRIER that follows,
3581      since the peephole that replaces this sequence
3582      is also an unconditional jump in that case.  */
3583 }
3584 \f
3585 /* Invert the condition of the jump JUMP, and make it jump
3586    to label NLABEL instead of where it jumps now.  */
3587
3588 int
3589 invert_jump (jump, nlabel)
3590      rtx jump, nlabel;
3591 {
3592   /* We have to either invert the condition and change the label or
3593      do neither.  Either operation could fail.  We first try to invert
3594      the jump. If that succeeds, we try changing the label.  If that fails,
3595      we invert the jump back to what it was.  */
3596
3597   if (! invert_exp (PATTERN (jump), jump))
3598     return 0;
3599
3600   if (redirect_jump (jump, nlabel))
3601     return 1;
3602
3603   if (! invert_exp (PATTERN (jump), jump))
3604     /* This should just be putting it back the way it was.  */
3605     abort ();
3606
3607   return  0;
3608 }
3609
3610 /* Invert the jump condition of rtx X contained in jump insn, INSN. 
3611
3612    Return 1 if we can do so, 0 if we cannot find a way to do so that
3613    matches a pattern.  */
3614
3615 int
3616 invert_exp (x, insn)
3617      rtx x;
3618      rtx insn;
3619 {
3620   register RTX_CODE code;
3621   register int i;
3622   register char *fmt;
3623
3624   code = GET_CODE (x);
3625
3626   if (code == IF_THEN_ELSE)
3627     {
3628       register rtx comp = XEXP (x, 0);
3629       register rtx tem;
3630
3631       /* We can do this in two ways:  The preferable way, which can only
3632          be done if this is not an integer comparison, is to reverse
3633          the comparison code.  Otherwise, swap the THEN-part and ELSE-part
3634          of the IF_THEN_ELSE.  If we can't do either, fail.  */
3635
3636       if (can_reverse_comparison_p (comp, insn)
3637           && validate_change (insn, &XEXP (x, 0),
3638                               gen_rtx (reverse_condition (GET_CODE (comp)),
3639                                        GET_MODE (comp), XEXP (comp, 0),
3640                                        XEXP (comp, 1)), 0))
3641         return 1;
3642                                        
3643       tem = XEXP (x, 1);
3644       validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
3645       validate_change (insn, &XEXP (x, 2), tem, 1);
3646       return apply_change_group ();
3647     }
3648
3649   fmt = GET_RTX_FORMAT (code);
3650   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3651     {
3652       if (fmt[i] == 'e')
3653         if (! invert_exp (XEXP (x, i), insn))
3654           return 0;
3655       if (fmt[i] == 'E')
3656         {
3657           register int j;
3658           for (j = 0; j < XVECLEN (x, i); j++)
3659             if (!invert_exp (XVECEXP (x, i, j), insn))
3660               return 0;
3661         }
3662     }
3663
3664   return 1;
3665 }
3666 \f
3667 /* Make jump JUMP jump to label NLABEL instead of where it jumps now.
3668    If the old jump target label is unused as a result,
3669    it and the code following it may be deleted.
3670
3671    If NLABEL is zero, we are to turn the jump into a (possibly conditional)
3672    RETURN insn.
3673
3674    The return value will be 1 if the change was made, 0 if it wasn't (this
3675    can only occur for NLABEL == 0).  */
3676
3677 int
3678 redirect_jump (jump, nlabel)
3679      rtx jump, nlabel;
3680 {
3681   register rtx olabel = JUMP_LABEL (jump);
3682
3683   if (nlabel == olabel)
3684     return 1;
3685
3686   if (! redirect_exp (&PATTERN (jump), olabel, nlabel, jump))
3687     return 0;
3688
3689   /* If this is an unconditional branch, delete it from the jump_chain of
3690      OLABEL and add it to the jump_chain of NLABEL (assuming both labels
3691      have UID's in range and JUMP_CHAIN is valid).  */
3692   if (jump_chain && (simplejump_p (jump)
3693                      || GET_CODE (PATTERN (jump)) == RETURN))
3694     {
3695       int label_index = nlabel ? INSN_UID (nlabel) : 0;
3696
3697       delete_from_jump_chain (jump);
3698       if (label_index < max_jump_chain
3699           && INSN_UID (jump) < max_jump_chain)
3700         {
3701           jump_chain[INSN_UID (jump)] = jump_chain[label_index];
3702           jump_chain[label_index] = jump;
3703         }
3704     }
3705
3706   JUMP_LABEL (jump) = nlabel;
3707   if (nlabel)
3708     ++LABEL_NUSES (nlabel);
3709
3710   if (olabel && --LABEL_NUSES (olabel) == 0)
3711     delete_insn (olabel);
3712
3713   return 1;
3714 }
3715
3716 /* Delete the instruction JUMP from any jump chain it might be on.  */
3717
3718 static void
3719 delete_from_jump_chain (jump)
3720      rtx jump;
3721 {
3722   int index;
3723   rtx olabel = JUMP_LABEL (jump);
3724
3725   /* Handle unconditional jumps.  */
3726   if (jump_chain && olabel != 0
3727       && INSN_UID (olabel) < max_jump_chain
3728       && simplejump_p (jump))
3729     index = INSN_UID (olabel);
3730   /* Handle return insns.  */
3731   else if (jump_chain && GET_CODE (PATTERN (jump)) == RETURN)
3732     index = 0;
3733   else return;
3734
3735   if (jump_chain[index] == jump)
3736     jump_chain[index] = jump_chain[INSN_UID (jump)];
3737   else
3738     {
3739       rtx insn;
3740
3741       for (insn = jump_chain[index];
3742            insn != 0;
3743            insn = jump_chain[INSN_UID (insn)])
3744         if (jump_chain[INSN_UID (insn)] == jump)
3745           {
3746             jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (jump)];
3747             break;
3748           }
3749     }
3750 }
3751
3752 /* If NLABEL is nonzero, throughout the rtx at LOC,
3753    alter (LABEL_REF OLABEL) to (LABEL_REF NLABEL).  If OLABEL is
3754    zero, alter (RETURN) to (LABEL_REF NLABEL).
3755
3756    If NLABEL is zero, alter (LABEL_REF OLABEL) to (RETURN) and check
3757    validity with validate_change.  Convert (set (pc) (label_ref olabel))
3758    to (return).
3759
3760    Return 0 if we found a change we would like to make but it is invalid.
3761    Otherwise, return 1.  */
3762
3763 int
3764 redirect_exp (loc, olabel, nlabel, insn)
3765      rtx *loc;
3766      rtx olabel, nlabel;
3767      rtx insn;
3768 {
3769   register rtx x = *loc;
3770   register RTX_CODE code = GET_CODE (x);
3771   register int i;
3772   register char *fmt;
3773
3774   if (code == LABEL_REF)
3775     {
3776       if (XEXP (x, 0) == olabel)
3777         {
3778           if (nlabel)
3779             XEXP (x, 0) = nlabel;
3780           else
3781             return validate_change (insn, loc, gen_rtx (RETURN, VOIDmode), 0);
3782           return 1;
3783         }
3784     }
3785   else if (code == RETURN && olabel == 0)
3786     {
3787       x = gen_rtx (LABEL_REF, VOIDmode, nlabel);
3788       if (loc == &PATTERN (insn))
3789         x = gen_rtx (SET, VOIDmode, pc_rtx, x);
3790       return validate_change (insn, loc, x, 0);
3791     }
3792
3793   if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
3794       && GET_CODE (SET_SRC (x)) == LABEL_REF
3795       && XEXP (SET_SRC (x), 0) == olabel)
3796     return validate_change (insn, loc, gen_rtx (RETURN, VOIDmode), 0);
3797
3798   fmt = GET_RTX_FORMAT (code);
3799   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3800     {
3801       if (fmt[i] == 'e')
3802         if (! redirect_exp (&XEXP (x, i), olabel, nlabel, insn))
3803           return 0;
3804       if (fmt[i] == 'E')
3805         {
3806           register int j;
3807           for (j = 0; j < XVECLEN (x, i); j++)
3808             if (! redirect_exp (&XVECEXP (x, i, j), olabel, nlabel, insn))
3809               return 0;
3810         }
3811     }
3812
3813   return 1;
3814 }
3815 \f
3816 /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
3817
3818    If the old jump target label (before the dispatch table) becomes unused,
3819    it and the dispatch table may be deleted.  In that case, find the insn
3820    before the jump references that label and delete it and logical successors
3821    too.  */
3822
3823 static void
3824 redirect_tablejump (jump, nlabel)
3825      rtx jump, nlabel;
3826 {
3827   register rtx olabel = JUMP_LABEL (jump);
3828
3829   /* Add this jump to the jump_chain of NLABEL.  */
3830   if (jump_chain && INSN_UID (nlabel) < max_jump_chain
3831       && INSN_UID (jump) < max_jump_chain)
3832     {
3833       jump_chain[INSN_UID (jump)] = jump_chain[INSN_UID (nlabel)];
3834       jump_chain[INSN_UID (nlabel)] = jump;
3835     }
3836
3837   PATTERN (jump) = gen_jump (nlabel);
3838   JUMP_LABEL (jump) = nlabel;
3839   ++LABEL_NUSES (nlabel);
3840   INSN_CODE (jump) = -1;
3841
3842   if (--LABEL_NUSES (olabel) == 0)
3843     {
3844       delete_labelref_insn (jump, olabel, 0);
3845       delete_insn (olabel);
3846     }
3847 }
3848
3849 /* Find the insn referencing LABEL that is a logical predecessor of INSN.
3850    If we found one, delete it and then delete this insn if DELETE_THIS is
3851    non-zero.  Return non-zero if INSN or a predecessor references LABEL.  */
3852
3853 static int
3854 delete_labelref_insn (insn, label, delete_this)
3855      rtx insn, label;
3856      int delete_this;
3857 {
3858   int deleted = 0;
3859   rtx link;
3860
3861   if (GET_CODE (insn) != NOTE
3862       && reg_mentioned_p (label, PATTERN (insn)))
3863     {
3864       if (delete_this)
3865         {
3866           delete_insn (insn);
3867           deleted = 1;
3868         }
3869       else
3870         return 1;
3871     }
3872
3873   for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
3874     if (delete_labelref_insn (XEXP (link, 0), label, 1))
3875       {
3876         if (delete_this)
3877           {
3878             delete_insn (insn);
3879             deleted = 1;
3880           }
3881         else
3882           return 1;
3883       }
3884
3885   return deleted;
3886 }
3887 \f
3888 /* Like rtx_equal_p except that it considers two REGs as equal
3889    if they renumber to the same value and considers two commutative
3890    operations to be the same if the order of the operands has been
3891    reversed.  */
3892
3893 int
3894 rtx_renumbered_equal_p (x, y)
3895      rtx x, y;
3896 {
3897   register int i;
3898   register RTX_CODE code = GET_CODE (x);
3899   register char *fmt;
3900       
3901   if (x == y)
3902     return 1;
3903
3904   if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
3905       && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
3906                                   && GET_CODE (SUBREG_REG (y)) == REG)))
3907     {
3908       int reg_x = -1, reg_y = -1;
3909       int word_x = 0, word_y = 0;
3910
3911       if (GET_MODE (x) != GET_MODE (y))
3912         return 0;
3913
3914       /* If we haven't done any renumbering, don't
3915          make any assumptions.  */
3916       if (reg_renumber == 0)
3917         return rtx_equal_p (x, y);
3918
3919       if (code == SUBREG)
3920         {
3921           reg_x = REGNO (SUBREG_REG (x));
3922           word_x = SUBREG_WORD (x);
3923
3924           if (reg_renumber[reg_x] >= 0)
3925             {
3926               reg_x = reg_renumber[reg_x] + word_x;
3927               word_x = 0;
3928             }
3929         }
3930
3931       else
3932         {
3933           reg_x = REGNO (x);
3934           if (reg_renumber[reg_x] >= 0)
3935             reg_x = reg_renumber[reg_x];
3936         }
3937
3938       if (GET_CODE (y) == SUBREG)
3939         {
3940           reg_y = REGNO (SUBREG_REG (y));
3941           word_y = SUBREG_WORD (y);
3942
3943           if (reg_renumber[reg_y] >= 0)
3944             {
3945               reg_y = reg_renumber[reg_y];
3946               word_y = 0;
3947             }
3948         }
3949
3950       else
3951         {
3952           reg_y = REGNO (y);
3953           if (reg_renumber[reg_y] >= 0)
3954             reg_y = reg_renumber[reg_y];
3955         }
3956
3957       return reg_x >= 0 && reg_x == reg_y && word_x == word_y;
3958     }
3959
3960   /* Now we have disposed of all the cases 
3961      in which different rtx codes can match.  */
3962   if (code != GET_CODE (y))
3963     return 0;
3964
3965   switch (code)
3966     {
3967     case PC:
3968     case CC0:
3969     case ADDR_VEC:
3970     case ADDR_DIFF_VEC:
3971       return 0;
3972
3973     case CONST_INT:
3974       return INTVAL (x) == INTVAL (y);
3975
3976     case LABEL_REF:
3977       /* We can't assume nonlocal labels have their following insns yet.  */
3978       if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
3979         return XEXP (x, 0) == XEXP (y, 0);
3980
3981       /* Two label-refs are equivalent if they point at labels
3982          in the same position in the instruction stream.  */
3983       return (next_real_insn (XEXP (x, 0))
3984               == next_real_insn (XEXP (y, 0)));
3985
3986     case SYMBOL_REF:
3987       return XSTR (x, 0) == XSTR (y, 0);
3988     }
3989
3990   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
3991
3992   if (GET_MODE (x) != GET_MODE (y))
3993     return 0;
3994
3995   /* For commutative operations, the RTX match if the operand match in any
3996      order.  Also handle the simple binary and unary cases without a loop.  */
3997   if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
3998     return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
3999              && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
4000             || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
4001                 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
4002   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4003     return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
4004             && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
4005   else if (GET_RTX_CLASS (code) == '1')
4006     return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
4007
4008   /* Compare the elements.  If any pair of corresponding elements
4009      fail to match, return 0 for the whole things.  */
4010
4011   fmt = GET_RTX_FORMAT (code);
4012   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4013     {
4014       register int j;
4015       switch (fmt[i])
4016         {
4017         case 'w':
4018           if (XWINT (x, i) != XWINT (y, i))
4019             return 0;
4020           break;
4021
4022         case 'i':
4023           if (XINT (x, i) != XINT (y, i))
4024             return 0;
4025           break;
4026
4027         case 's':
4028           if (strcmp (XSTR (x, i), XSTR (y, i)))
4029             return 0;
4030           break;
4031
4032         case 'e':
4033           if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
4034             return 0;
4035           break;
4036
4037         case 'u':
4038           if (XEXP (x, i) != XEXP (y, i))
4039             return 0;
4040           /* fall through.  */
4041         case '0':
4042           break;
4043
4044         case 'E':
4045           if (XVECLEN (x, i) != XVECLEN (y, i))
4046             return 0;
4047           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4048             if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
4049               return 0;
4050           break;
4051
4052         default:
4053           abort ();
4054         }
4055     }
4056   return 1;
4057 }
4058 \f
4059 /* If X is a hard register or equivalent to one or a subregister of one,
4060    return the hard register number.  If X is a pseudo register that was not
4061    assigned a hard register, return the pseudo register number.  Otherwise,
4062    return -1.  Any rtx is valid for X.  */
4063
4064 int
4065 true_regnum (x)
4066      rtx x;
4067 {
4068   if (GET_CODE (x) == REG)
4069     {
4070       if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
4071         return reg_renumber[REGNO (x)];
4072       return REGNO (x);
4073     }
4074   if (GET_CODE (x) == SUBREG)
4075     {
4076       int base = true_regnum (SUBREG_REG (x));
4077       if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
4078         return SUBREG_WORD (x) + base;
4079     }
4080   return -1;
4081 }
4082 \f
4083 /* Optimize code of the form:
4084
4085         for (x = a[i]; x; ...)
4086           ...
4087         for (x = a[i]; x; ...)
4088           ...
4089       foo:
4090
4091    Loop optimize will change the above code into
4092
4093         if (x = a[i])
4094           for (;;)
4095              { ...; if (! (x = ...)) break; }
4096         if (x = a[i])
4097           for (;;)
4098              { ...; if (! (x = ...)) break; }
4099       foo:
4100
4101    In general, if the first test fails, the program can branch
4102    directly to `foo' and skip the second try which is doomed to fail.
4103    We run this after loop optimization and before flow analysis.  */
4104    
4105 /* When comparing the insn patterns, we track the fact that different
4106    pseudo-register numbers may have been used in each computation.
4107    The following array stores an equivalence -- same_regs[I] == J means
4108    that pseudo register I was used in the first set of tests in a context
4109    where J was used in the second set.  We also count the number of such
4110    pending equivalences.  If nonzero, the expressions really aren't the
4111    same.  */
4112
4113 static int *same_regs;
4114
4115 static int num_same_regs;
4116
4117 /* Track any registers modified between the target of the first jump and
4118    the second jump.  They never compare equal.  */
4119
4120 static char *modified_regs;
4121
4122 /* Record if memory was modified.  */
4123
4124 static int modified_mem;
4125
4126 /* Called via note_stores on each insn between the target of the first 
4127    branch and the second branch.  It marks any changed registers.  */
4128
4129 static void
4130 mark_modified_reg (dest, x)
4131      rtx dest;
4132      rtx x;
4133 {
4134   int regno, i;
4135
4136   if (GET_CODE (dest) == SUBREG)
4137     dest = SUBREG_REG (dest);
4138
4139   if (GET_CODE (dest) == MEM)
4140     modified_mem = 1;
4141
4142   if (GET_CODE (dest) != REG)
4143     return;
4144
4145   regno = REGNO (dest);
4146   if (regno >= FIRST_PSEUDO_REGISTER)
4147     modified_regs[regno] = 1;
4148   else
4149     for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
4150       modified_regs[regno + i] = 1;
4151 }
4152
4153 /* F is the first insn in the chain of insns.  */
4154    
4155 void
4156 thread_jumps (f, max_reg, flag_before_loop)
4157      rtx f;
4158      int max_reg;
4159      int flag_before_loop;
4160 {
4161   /* Basic algorithm is to find a conditional branch,
4162      the label it may branch to, and the branch after
4163      that label.  If the two branches test the same condition,
4164      walk back from both branch paths until the insn patterns
4165      differ, or code labels are hit.  If we make it back to
4166      the target of the first branch, then we know that the first branch
4167      will either always succeed or always fail depending on the relative
4168      senses of the two branches.  So adjust the first branch accordingly
4169      in this case.  */
4170      
4171   rtx label, b1, b2, t1, t2;
4172   enum rtx_code code1, code2;
4173   rtx b1op0, b1op1, b2op0, b2op1;
4174   int changed = 1;
4175   int i;
4176   int *all_reset;
4177
4178   /* Allocate register tables and quick-reset table.  */
4179   modified_regs = (char *) alloca (max_reg * sizeof (char));
4180   same_regs = (int *) alloca (max_reg * sizeof (int));
4181   all_reset = (int *) alloca (max_reg * sizeof (int));
4182   for (i = 0; i < max_reg; i++)
4183     all_reset[i] = -1;
4184     
4185   while (changed)
4186     {
4187       changed = 0;
4188
4189       for (b1 = f; b1; b1 = NEXT_INSN (b1))
4190         {
4191           /* Get to a candidate branch insn.  */
4192           if (GET_CODE (b1) != JUMP_INSN
4193               || ! condjump_p (b1) || simplejump_p (b1)
4194               || JUMP_LABEL (b1) == 0)
4195             continue;
4196
4197           bzero (modified_regs, max_reg * sizeof (char));
4198           modified_mem = 0;
4199
4200           bcopy ((char *) all_reset, (char *) same_regs,
4201                  max_reg * sizeof (int));
4202           num_same_regs = 0;
4203
4204           label = JUMP_LABEL (b1);
4205
4206           /* Look for a branch after the target.  Record any registers and
4207              memory modified between the target and the branch.  Stop when we
4208              get to a label since we can't know what was changed there.  */
4209           for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
4210             {
4211               if (GET_CODE (b2) == CODE_LABEL)
4212                 break;
4213
4214               else if (GET_CODE (b2) == JUMP_INSN)
4215                 {
4216                   /* If this is an unconditional jump and is the only use of
4217                      its target label, we can follow it.  */
4218                   if (simplejump_p (b2)
4219                       && JUMP_LABEL (b2) != 0
4220                       && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
4221                     {
4222                       b2 = JUMP_LABEL (b2);
4223                       continue;
4224                     }
4225                   else
4226                     break;
4227                 }
4228
4229               if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
4230                 continue;
4231
4232               if (GET_CODE (b2) == CALL_INSN)
4233                 {
4234                   modified_mem = 1;
4235                   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4236                     if (call_used_regs[i] && ! fixed_regs[i]
4237                         && i != STACK_POINTER_REGNUM
4238                         && i != FRAME_POINTER_REGNUM
4239                         && i != HARD_FRAME_POINTER_REGNUM
4240                         && i != ARG_POINTER_REGNUM)
4241                       modified_regs[i] = 1;
4242                 }
4243
4244               note_stores (PATTERN (b2), mark_modified_reg);
4245             }
4246
4247           /* Check the next candidate branch insn from the label
4248              of the first.  */
4249           if (b2 == 0
4250               || GET_CODE (b2) != JUMP_INSN
4251               || b2 == b1
4252               || ! condjump_p (b2)
4253               || simplejump_p (b2))
4254             continue;
4255
4256           /* Get the comparison codes and operands, reversing the
4257              codes if appropriate.  If we don't have comparison codes,
4258              we can't do anything.  */
4259           b1op0 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 0);
4260           b1op1 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 1);
4261           code1 = GET_CODE (XEXP (SET_SRC (PATTERN (b1)), 0));
4262           if (XEXP (SET_SRC (PATTERN (b1)), 1) == pc_rtx)
4263             code1 = reverse_condition (code1);
4264
4265           b2op0 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 0);
4266           b2op1 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 1);
4267           code2 = GET_CODE (XEXP (SET_SRC (PATTERN (b2)), 0));
4268           if (XEXP (SET_SRC (PATTERN (b2)), 1) == pc_rtx)
4269             code2 = reverse_condition (code2);
4270
4271           /* If they test the same things and knowing that B1 branches
4272              tells us whether or not B2 branches, check if we
4273              can thread the branch.  */
4274           if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
4275               && rtx_equal_for_thread_p (b1op1, b2op1, b2)
4276               && (comparison_dominates_p (code1, code2)
4277                   || comparison_dominates_p (code1, reverse_condition (code2))))
4278             {
4279               t1 = prev_nonnote_insn (b1);
4280               t2 = prev_nonnote_insn (b2);
4281               
4282               while (t1 != 0 && t2 != 0)
4283                 {
4284                   if (t2 == label)
4285                     {
4286                       /* We have reached the target of the first branch.
4287                          If there are no pending register equivalents,
4288                          we know that this branch will either always
4289                          succeed (if the senses of the two branches are
4290                          the same) or always fail (if not).  */
4291                       rtx new_label;
4292
4293                       if (num_same_regs != 0)
4294                         break;
4295
4296                       if (comparison_dominates_p (code1, code2))
4297                         new_label = JUMP_LABEL (b2);
4298                       else
4299                         new_label = get_label_after (b2);
4300
4301                       if (JUMP_LABEL (b1) != new_label)
4302                         {
4303                           rtx prev = PREV_INSN (new_label);
4304
4305                           if (flag_before_loop
4306                               && NOTE_LINE_NUMBER (prev) == NOTE_INSN_LOOP_BEG)
4307                             {
4308                               /* Don't thread to the loop label.  If a loop
4309                                  label is reused, loop optimization will
4310                                  be disabled for that loop.  */
4311                               new_label = gen_label_rtx ();
4312                               emit_label_after (new_label, PREV_INSN (prev));
4313                             }
4314                           changed |= redirect_jump (b1, new_label);
4315                         }
4316                       break;
4317                     }
4318                     
4319                   /* If either of these is not a normal insn (it might be
4320                      a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail.  (NOTEs
4321                      have already been skipped above.)  Similarly, fail
4322                      if the insns are different.  */
4323                   if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
4324                       || recog_memoized (t1) != recog_memoized (t2)
4325                       || ! rtx_equal_for_thread_p (PATTERN (t1),
4326                                                    PATTERN (t2), t2))
4327                     break;
4328                     
4329                   t1 = prev_nonnote_insn (t1);
4330                   t2 = prev_nonnote_insn (t2);
4331                 }
4332             }
4333         }
4334     }
4335 }
4336 \f
4337 /* This is like RTX_EQUAL_P except that it knows about our handling of
4338    possibly equivalent registers and knows to consider volatile and
4339    modified objects as not equal.
4340    
4341    YINSN is the insn containing Y.  */
4342
4343 int
4344 rtx_equal_for_thread_p (x, y, yinsn)
4345      rtx x, y;
4346      rtx yinsn;
4347 {
4348   register int i;
4349   register int j;
4350   register enum rtx_code code;
4351   register char *fmt;
4352
4353   code = GET_CODE (x);
4354   /* Rtx's of different codes cannot be equal.  */
4355   if (code != GET_CODE (y))
4356     return 0;
4357
4358   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
4359      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
4360
4361   if (GET_MODE (x) != GET_MODE (y))
4362     return 0;
4363
4364   /* For commutative operations, the RTX match if the operand match in any
4365      order.  Also handle the simple binary and unary cases without a loop.  */
4366   if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
4367     return ((rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4368              && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn))
4369             || (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 1), yinsn)
4370                 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 0), yinsn)));
4371   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4372     return (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4373             && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn));
4374   else if (GET_RTX_CLASS (code) == '1')
4375     return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4376
4377   /* Handle special-cases first.  */
4378   switch (code)
4379     {
4380     case REG:
4381       if (REGNO (x) == REGNO (y) && ! modified_regs[REGNO (x)])
4382         return 1;
4383
4384       /* If neither is user variable or hard register, check for possible
4385          equivalence.  */
4386       if (REG_USERVAR_P (x) || REG_USERVAR_P (y)
4387           || REGNO (x) < FIRST_PSEUDO_REGISTER
4388           || REGNO (y) < FIRST_PSEUDO_REGISTER)
4389         return 0;
4390
4391       if (same_regs[REGNO (x)] == -1)
4392         {
4393           same_regs[REGNO (x)] = REGNO (y);
4394           num_same_regs++;
4395
4396           /* If this is the first time we are seeing a register on the `Y'
4397              side, see if it is the last use.  If not, we can't thread the 
4398              jump, so mark it as not equivalent.  */
4399           if (regno_last_uid[REGNO (y)] != INSN_UID (yinsn))
4400             return 0;
4401
4402           return 1;
4403         }
4404       else
4405         return (same_regs[REGNO (x)] == REGNO (y));
4406
4407       break;
4408
4409     case MEM:
4410       /* If memory modified or either volatile, not equivalent.
4411          Else, check address. */
4412       if (modified_mem || MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4413         return 0;
4414
4415       return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4416
4417     case ASM_INPUT:
4418       if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4419         return 0;
4420
4421       break;
4422
4423     case SET:
4424       /* Cancel a pending `same_regs' if setting equivalenced registers.
4425          Then process source.  */
4426       if (GET_CODE (SET_DEST (x)) == REG
4427           && GET_CODE (SET_DEST (y)) == REG)
4428         {
4429           if (same_regs[REGNO (SET_DEST (x))] == REGNO (SET_DEST (y)))
4430             {
4431               same_regs[REGNO (SET_DEST (x))] = -1;
4432               num_same_regs--;
4433             }
4434           else if (REGNO (SET_DEST (x)) != REGNO (SET_DEST (y)))
4435             return 0;
4436         }
4437       else
4438         if (rtx_equal_for_thread_p (SET_DEST (x), SET_DEST (y), yinsn) == 0)
4439           return 0;
4440
4441       return rtx_equal_for_thread_p (SET_SRC (x), SET_SRC (y), yinsn);
4442
4443     case LABEL_REF:
4444       return XEXP (x, 0) == XEXP (y, 0);
4445
4446     case SYMBOL_REF:
4447       return XSTR (x, 0) == XSTR (y, 0);
4448     }
4449
4450   if (x == y)
4451     return 1;
4452
4453   fmt = GET_RTX_FORMAT (code);
4454   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4455     {
4456       switch (fmt[i])
4457         {
4458         case 'w':
4459           if (XWINT (x, i) != XWINT (y, i))
4460             return 0;
4461           break;
4462
4463         case 'n':
4464         case 'i':
4465           if (XINT (x, i) != XINT (y, i))
4466             return 0;
4467           break;
4468
4469         case 'V':
4470         case 'E':
4471           /* Two vectors must have the same length.  */
4472           if (XVECLEN (x, i) != XVECLEN (y, i))
4473             return 0;
4474
4475           /* And the corresponding elements must match.  */
4476           for (j = 0; j < XVECLEN (x, i); j++)
4477             if (rtx_equal_for_thread_p (XVECEXP (x, i, j),
4478                                         XVECEXP (y, i, j), yinsn) == 0)
4479               return 0;
4480           break;
4481
4482         case 'e':
4483           if (rtx_equal_for_thread_p (XEXP (x, i), XEXP (y, i), yinsn) == 0)
4484             return 0;
4485           break;
4486
4487         case 'S':
4488         case 's':
4489           if (strcmp (XSTR (x, i), XSTR (y, i)))
4490             return 0;
4491           break;
4492
4493         case 'u':
4494           /* These are just backpointers, so they don't matter.  */
4495           break;
4496
4497         case '0':
4498           break;
4499
4500           /* It is believed that rtx's at this level will never
4501              contain anything but integers and other rtx's,
4502              except for within LABEL_REFs and SYMBOL_REFs.  */
4503         default:
4504           abort ();
4505         }
4506     }
4507   return 1;
4508 }