home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / jump.c < prev    next >
C/C++ Source or Header  |  1992-03-17  |  102KB  |  3,481 lines

  1. /* Optimize jump instructions, for GNU compiler.
  2.    Copyright (C) 1987-1990 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 "expr.h"
  59. #include "insn-config.h"
  60. #include "insn-flags.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. /* Maximum index in jump_chain.  */
  87.  
  88. static int max_jump_chain;
  89.  
  90. static int duplicate_loop_exit_test ();
  91. rtx delete_insn ();
  92. int redirect_jump ();
  93. static int redirect_exp ();
  94. void redirect_tablejump ();
  95. static int delete_labelref_insn ();
  96. int invert_jump ();
  97. static int invert_exp ();
  98. int condjump_p ();
  99. int simplejump_p ();
  100.  
  101. extern rtx gen_jump ();
  102.  
  103. extern void squeeze_notes ();
  104. static void mark_jump_label ();
  105. void delete_jump ();
  106. static void delete_from_jump_chain ();
  107. static int tension_vector_labels ();
  108. static void find_cross_jump ();
  109. static void do_cross_jump ();
  110. static int jump_back_p ();
  111.  
  112. /* Delete no-op jumps and optimize jumps to jumps
  113.    and jumps around jumps.
  114.    Delete unused labels and unreachable code.
  115.    If CROSS_JUMP is nonzero, detect matching code
  116.    before a jump and its destination and unify them.
  117.    If NOOP_MOVES is nonzero, also delete no-op move insns.
  118.  
  119.    If `optimize' is zero, don't change any code,
  120.    just determine whether control drops off the end of the function.
  121.    This case occurs when we have -W and not -O.
  122.    It works because `delete_insn' checks the value of `optimize'
  123.    and refrains from actually deleting when that is 0.  */
  124.  
  125. void
  126. jump_optimize (f, cross_jump, noop_moves)
  127.      rtx f;
  128. {
  129.   register rtx insn;
  130.   int changed;
  131.   int first = 1;
  132.   int max_uid = 0;
  133.   rtx last_insn;
  134.  
  135.   /* Initialize LABEL_NUSES and JUMP_LABEL fields.  */
  136.  
  137.   for (insn = f; insn; insn = NEXT_INSN (insn))
  138.     {
  139.       if (GET_CODE (insn) == CODE_LABEL)
  140.     LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
  141.       else if (GET_CODE (insn) == JUMP_INSN)
  142.     JUMP_LABEL (insn) = 0;
  143.       if (INSN_UID (insn) > max_uid)
  144.     max_uid = INSN_UID (insn);
  145.     }
  146.  
  147.   max_uid++;
  148.  
  149.   /* Delete insns following barriers, up to next label.  */
  150.  
  151.   for (insn = f; insn;)
  152.     {
  153.       if (GET_CODE (insn) == BARRIER)
  154.     {
  155.       insn = NEXT_INSN (insn);
  156.       while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
  157.         {
  158.           if (GET_CODE (insn) == NOTE
  159.           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
  160.         insn = NEXT_INSN (insn);
  161.           else
  162.         insn = delete_insn (insn);
  163.         }
  164.       /* INSN is now the code_label.  */
  165.     }
  166.       else
  167.     insn = NEXT_INSN (insn);
  168.     }
  169.  
  170.   /* Leave some extra room for labels and duplicate exit test insns
  171.      we make.  */
  172.   max_jump_chain = max_uid * 14 / 10;
  173.   jump_chain = (rtx *) alloca (max_jump_chain * sizeof (rtx));
  174.   bzero (jump_chain, max_jump_chain * sizeof (rtx));
  175.  
  176.   /* Mark the label each jump jumps to.
  177.      Combine consecutive labels, and count uses of labels.
  178.  
  179.      For each label, make a chain (using `jump_chain')
  180.      of all the *unconditional* jumps that jump to it;
  181.      also make a chain of all returns.  */
  182.  
  183.   for (insn = f; insn; insn = NEXT_INSN (insn))
  184.     if ((GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == INSN
  185.      || GET_CODE (insn) == CALL_INSN)
  186.     && ! INSN_DELETED_P (insn))
  187.       {
  188.     mark_jump_label (PATTERN (insn), insn, cross_jump);
  189.     if (GET_CODE (insn) == JUMP_INSN)
  190.       {
  191.         if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
  192.           {
  193.         jump_chain[INSN_UID (insn)]
  194.           = jump_chain[INSN_UID (JUMP_LABEL (insn))];
  195.         jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
  196.           }
  197.         if (GET_CODE (PATTERN (insn)) == RETURN)
  198.           {
  199.         jump_chain[INSN_UID (insn)] = jump_chain[0];
  200.         jump_chain[0] = insn;
  201.           }
  202.       }
  203.       }
  204.  
  205.   /* Delete all labels already not referenced.
  206.      Also find the last insn.  */
  207.  
  208.   last_insn = 0;
  209.   for (insn = f; insn; )
  210.     {
  211.       if (GET_CODE (insn) == CODE_LABEL && LABEL_NUSES (insn) == 0)
  212.     insn = delete_insn (insn);
  213.       else
  214.     {
  215.       last_insn = insn;
  216.       insn = NEXT_INSN (insn);
  217.     }
  218.     }
  219.  
  220.   if (!optimize)
  221.     {
  222.       /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
  223.      If so record that this function can drop off the end.  */
  224.  
  225.       insn = last_insn;
  226.       {
  227.     int n_labels = 1;
  228.     while (insn
  229.            /* One label can follow the end-note: the return label.  */
  230.            && ((GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
  231.            /* Ordinary insns can follow it if returning a structure.  */
  232.            || GET_CODE (insn) == INSN
  233.            /* If machine uses explicit RETURN insns, no epilogue,
  234.               then one of them follows the note.  */
  235.            || (GET_CODE (insn) == JUMP_INSN
  236.                && GET_CODE (PATTERN (insn)) == RETURN)
  237.            /* Other kinds of notes can follow also.  */
  238.            || (GET_CODE (insn) == NOTE
  239.                && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)))
  240.       insn = PREV_INSN (insn);
  241.       }
  242.  
  243.       if (insn && GET_CODE (insn) == NOTE
  244.       && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END
  245.       && ! INSN_DELETED_P (insn))
  246.     {
  247.       extern int current_function_returns_null;
  248.       current_function_returns_null = 1;
  249.     }
  250.       /* Zero the "deleted" flag of all the "deleted" insns.  */
  251.       for (insn = f; insn; insn = NEXT_INSN (insn))
  252.     INSN_DELETED_P (insn) = 0;
  253.       return;
  254.     }
  255.  
  256. #ifdef HAVE_return
  257.   if (HAVE_return)
  258.     {
  259.       /* If we fall through to the epilogue, see if we can insert a RETURN insn
  260.      in front of it.  If the machine allows it at this point (we might be
  261.      after reload for a leaf routine), it will improve optimization for it
  262.      to be there.  */
  263.       insn = get_last_insn ();
  264.       while (insn && GET_CODE (insn) == NOTE)
  265.     insn = PREV_INSN (insn);
  266.  
  267.       if (insn && GET_CODE (insn) != BARRIER)
  268.     {
  269.       emit_jump_insn (gen_return ());
  270.       emit_barrier ();
  271.     }
  272.     }
  273. #endif
  274.  
  275.   if (noop_moves)
  276.     for (insn = f; insn; )
  277.       {
  278.     register rtx next = NEXT_INSN (insn);
  279.  
  280.     if (GET_CODE (insn) == INSN)
  281.       {
  282.         register rtx body = PATTERN (insn);
  283.  
  284. /* Combine stack_adjusts with following push_insns.  */
  285. #ifdef PUSH_ROUNDING
  286.         if (GET_CODE (body) == SET
  287.         && SET_DEST (body) == stack_pointer_rtx
  288.         && GET_CODE (SET_SRC (body)) == PLUS
  289.         && XEXP (SET_SRC (body), 0) == stack_pointer_rtx
  290.         && GET_CODE (XEXP (SET_SRC (body), 1)) == CONST_INT
  291.         && INTVAL (XEXP (SET_SRC (body), 1)) > 0)
  292.           {
  293.         rtx p;
  294.         rtx stack_adjust_insn = insn;
  295.         int stack_adjust_amount = INTVAL (XEXP (SET_SRC (body), 1));
  296.         int total_pushed = 0;
  297.         int pushes = 0;
  298.  
  299.         /* Find all successive push insns.  */
  300.         p = insn;
  301.         /* Don't convert more than three pushes;
  302.            that starts adding too many displaced addresses
  303.            and the whole thing starts becoming a losing
  304.            proposition.  */
  305.         while (pushes < 3)
  306.           {
  307.             rtx pbody, dest;
  308.             p = next_nonnote_insn (p);
  309.             if (p == 0 || GET_CODE (p) != INSN)
  310.               break;
  311.             pbody = PATTERN (p);
  312.             if (GET_CODE (pbody) != SET)
  313.               break;
  314.             dest = SET_DEST (pbody);
  315.             /* Allow a no-op move between the adjust and the push.  */
  316.             if (GET_CODE (dest) == REG
  317.             && GET_CODE (SET_SRC (pbody)) == REG
  318.             && REGNO (dest) == REGNO (SET_SRC (pbody)))
  319.               continue;
  320.             if (! (GET_CODE (dest) == MEM
  321.                && GET_CODE (XEXP (dest, 0)) == POST_INC
  322.                && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
  323.               break;
  324.             pushes++;
  325.             if (total_pushed + GET_MODE_SIZE (SET_DEST (pbody))
  326.             > stack_adjust_amount)
  327.               break;
  328.             total_pushed += GET_MODE_SIZE (SET_DEST (pbody));
  329.           }
  330.  
  331.         /* Discard the amount pushed from the stack adjust;
  332.            maybe eliminate it entirely.  */
  333.         if (total_pushed >= stack_adjust_amount)
  334.           {
  335.             delete_insn (stack_adjust_insn);
  336.             total_pushed = stack_adjust_amount;
  337.           }
  338.         else
  339.           XEXP (SET_SRC (PATTERN (stack_adjust_insn)), 1)
  340.             = gen_rtx (CONST_INT, VOIDmode, 
  341.                    stack_adjust_amount - total_pushed);
  342.  
  343.         /* Change the appropriate push insns to ordinary stores.  */
  344.         p = insn;
  345.         while (total_pushed > 0)
  346.           {
  347.             rtx pbody, dest;
  348.             p = next_nonnote_insn (p);
  349.             if (GET_CODE (p) != INSN)
  350.               break;
  351.             pbody = PATTERN (p);
  352.             if (GET_CODE (pbody) == SET)
  353.               break;
  354.             dest = SET_DEST (pbody);
  355.             if (! (GET_CODE (dest) == MEM
  356.                && GET_CODE (XEXP (dest, 0)) == POST_INC
  357.                && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
  358.               break;
  359.             total_pushed -= GET_MODE_SIZE (SET_DEST (pbody));
  360.             /* If this push doesn't fully fit in the space
  361.                of the stack adjust that we deleted,
  362.                make another stack adjust here for what we
  363.                didn't use up.  There should be peepholes
  364.                to recognize the resulting sequence of insns.  */
  365.             if (total_pushed < 0)
  366.               {
  367.             emit_insn_before (gen_add2_insn (stack_pointer_rtx,
  368.                              gen_rtx (CONST_INT, VOIDmode, - total_pushed)),
  369.                       p);
  370.             break;
  371.               }
  372.             XEXP (dest, 0)
  373.               = plus_constant (stack_pointer_rtx, total_pushed);
  374.           }
  375.           }
  376. #endif
  377.  
  378.         /* Detect and delete no-op move instructions
  379.            resulting from not allocating a parameter in a register.  */
  380.  
  381.         if (GET_CODE (body) == SET
  382.         && (SET_DEST (body) == SET_SRC (body)
  383.             || (GET_CODE (SET_DEST (body)) == MEM
  384.             && GET_CODE (SET_SRC (body)) == MEM
  385.             && rtx_equal_p (SET_SRC (body), SET_DEST (body))))
  386.         && ! (GET_CODE (SET_DEST (body)) == MEM
  387.               && MEM_VOLATILE_P (SET_DEST (body)))
  388.         && ! (GET_CODE (SET_SRC (body)) == MEM
  389.               && MEM_VOLATILE_P (SET_SRC (body))))
  390.           delete_insn (insn);
  391.  
  392.         /* Detect and ignore no-op move instructions
  393.            resulting from smart or fortuitous register allocation.  */
  394.  
  395.         else if (GET_CODE (body) == SET)
  396.           {
  397.         int sreg = true_regnum (SET_SRC (body));
  398.         int dreg = true_regnum (SET_DEST (body));
  399.  
  400.         if (sreg == dreg && sreg >= 0)
  401.           delete_insn (insn);
  402.         else if (sreg >= 0 && dreg >= 0)
  403.           {
  404.             rtx trial;
  405.             rtx tem = find_equiv_reg (0, insn, 0,
  406.                           sreg, 0, dreg,
  407.                           GET_MODE (SET_SRC (body)));
  408.  
  409. #ifdef PRESERVE_DEATH_INFO_REGNO_P
  410.             /* Deleting insn could lose a death-note for SREG or DREG
  411.                so don't do it if final needs accurate death-notes.  */
  412.             if (! PRESERVE_DEATH_INFO_REGNO_P (sreg)
  413.             && ! PRESERVE_DEATH_INFO_REGNO_P (dreg))
  414. #endif
  415.               {
  416.             /* DREG may have been the target of a REG_DEAD note in
  417.                the insn which makes INSN redundant.  If so, reorg
  418.                would still think it is dead.  So search for such a
  419.                note and delete it if we find it.  */
  420.             for (trial = prev_nonnote_insn (insn);
  421.                  trial && GET_CODE (trial) != CODE_LABEL;
  422.                  trial = prev_nonnote_insn (trial))
  423.               if (find_regno_note (trial, REG_DEAD, dreg))
  424.                 {
  425.                   remove_death (dreg, trial);
  426.                   break;
  427.                 }
  428.  
  429.             if (tem != 0
  430.                 && GET_MODE (tem) == GET_MODE (SET_DEST (body)))
  431.               delete_insn (insn);
  432.               }
  433.           }
  434.         else if (dreg >= 0 && CONSTANT_P (SET_SRC (body))
  435.              && find_equiv_reg (SET_SRC (body), insn, 0, dreg, 0,
  436.                         0, GET_MODE (SET_DEST (body))))
  437.           {
  438.             /* This handles the case where we have two consecutive
  439.                assignments of the same constant to pseudos that didn't
  440.                get a hard reg.  Each SET from the constant will be
  441.                converted into a SET of the spill register and an
  442.                output reload will be made following it.  This produces
  443.                two loads of the same constant into the same spill
  444.                register.  */
  445.  
  446.             rtx in_insn = insn;
  447.  
  448.             /* Look back for a death note for the first reg.
  449.                If there is one, it is no longer accurate.  */
  450.             while (in_insn && GET_CODE (in_insn) != CODE_LABEL)
  451.               {
  452.             if ((GET_CODE (in_insn) == INSN
  453.                  || GET_CODE (in_insn) == JUMP_INSN)
  454.                 && find_regno_note (in_insn, REG_DEAD, dreg))
  455.               {
  456.                 remove_death (dreg, in_insn);
  457.                 break;
  458.               }
  459.             in_insn = PREV_INSN (in_insn);
  460.               }
  461.  
  462.             /* Delete the second load of the value.  */
  463.             delete_insn (insn);
  464.           }
  465.           }
  466.         else if (GET_CODE (body) == PARALLEL)
  467.           {
  468.         /* If each part is a set between two identical registers or
  469.            a USE or CLOBBER, delete the insn. */
  470.         int i, sreg, dreg;
  471.         rtx tem;
  472.  
  473.         for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
  474.           {
  475.             tem = XVECEXP (body, 0, i);
  476.             if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
  477.               continue;
  478.  
  479.             if (GET_CODE (tem) != SET
  480.                 || (sreg = true_regnum (SET_SRC (tem))) < 0
  481.                 || (dreg = true_regnum (SET_DEST (tem))) < 0
  482.                 || dreg != sreg)
  483.               break;
  484.           }
  485.           
  486.         if (i < 0)
  487.           delete_insn (insn);
  488.           }
  489. #if !BYTES_BIG_ENDIAN /* Not worth the hair to detect this
  490.              in the big-endian case.  */
  491.         /* Also delete insns to store bit fields if they are no-ops.  */
  492.         else if (GET_CODE (body) == SET
  493.              && GET_CODE (SET_DEST (body)) == ZERO_EXTRACT
  494.              && XEXP (SET_DEST (body), 2) == const0_rtx
  495.              && XEXP (SET_DEST (body), 0) == SET_SRC (body)
  496.              && ! (GET_CODE (SET_SRC (body)) == MEM
  497.                && MEM_VOLATILE_P (SET_SRC (body))))
  498.           delete_insn (insn);
  499. #endif /* not BYTES_BIG_ENDIAN */
  500.       }
  501.       insn = next;
  502.     }
  503.  
  504.   /* Now iterate optimizing jumps until nothing changes over one pass.  */
  505.   changed = 1;
  506.   while (changed)
  507.     {
  508.       register rtx next;
  509.       changed = 0;
  510.  
  511.       for (insn = f; insn; insn = next)
  512.     {
  513.       register rtx reallabelprev;
  514.       register rtx temp, temp1, temp2, temp3, temp4;
  515.       register rtx nlabel;
  516.       int this_is_simplejump, this_is_condjump;
  517. #if 0
  518.       /* If NOT the first iteration, if this is the last jump pass
  519.          (just before final), do the special peephole optimizations.
  520.          Avoiding the first iteration gives ordinary jump opts
  521.          a chance to work before peephole opts.  */
  522.  
  523.       if (noop_moves && !first && !flag_no_peephole)
  524.         if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
  525.           peephole (insn);
  526. #endif
  527.  
  528.       /* That could have deleted some insns after INSN, so check now
  529.          what the following insn is.  */
  530.  
  531.       next = NEXT_INSN (insn);
  532.  
  533.       /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
  534.          jump.  Try to optimize by duplicating the loop exit test if so. */
  535.       if (! noop_moves && GET_CODE (insn) == NOTE
  536.           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
  537.           && (temp1 = next_nonnote_insn (insn)) != 0
  538.           && simplejump_p (temp1))
  539.         {
  540.           temp = PREV_INSN (insn);
  541.           if (duplicate_loop_exit_test (insn))
  542.         {
  543.           changed = 1;
  544.           next = NEXT_INSN (temp);
  545.           continue;
  546.         }
  547.         }
  548.           
  549.       if (GET_CODE (insn) != JUMP_INSN)
  550.         continue;
  551.  
  552.       this_is_simplejump = simplejump_p (insn);
  553.       this_is_condjump = condjump_p (insn);
  554.  
  555.       /* Tension the labels in dispatch tables.  */
  556.  
  557.       if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
  558.         changed |= tension_vector_labels (PATTERN (insn), 0, noop_moves);
  559.       if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
  560.         changed |= tension_vector_labels (PATTERN (insn), 1, noop_moves);
  561.  
  562.       /* If a dispatch table always goes to the same place,
  563.          get rid of it and replace the insn that uses it.  */
  564.  
  565.       if (GET_CODE (PATTERN (insn)) == ADDR_VEC
  566.           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
  567.         {
  568.           int i;
  569.           rtx pat = PATTERN (insn);
  570.           int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
  571.           int len = XVECLEN (pat, diff_vec_p);
  572.           rtx dispatch = prev_real_insn (insn);
  573.  
  574.           for (i = 0; i < len; i++)
  575.         if (XEXP (XVECEXP (pat, diff_vec_p, i), 0)
  576.             != XEXP (XVECEXP (pat, diff_vec_p, 0), 0))
  577.           break;
  578.           if (i == len
  579.           && GET_CODE (dispatch) == JUMP_INSN
  580.           && JUMP_LABEL (dispatch) != 0
  581.           /* Don't mess with a casesi insn.  */
  582.           && !(GET_CODE (PATTERN (dispatch)) == SET
  583.                && (GET_CODE (SET_SRC (PATTERN (dispatch)))
  584.                == IF_THEN_ELSE))
  585.           && next_real_insn (JUMP_LABEL (dispatch)) == insn)
  586.         {
  587.           redirect_tablejump (dispatch,
  588.                       XEXP (XVECEXP (pat, diff_vec_p, 0), 0));
  589.           changed = 1;
  590.         }
  591.         }
  592.  
  593.       reallabelprev = prev_active_insn (JUMP_LABEL (insn));
  594.  
  595.       /* If a jump references the end of the function, try to turn
  596.          it into a RETURN insn, possibly a conditional one.  */
  597.       if (JUMP_LABEL (insn)
  598.           && next_active_insn (JUMP_LABEL (insn)) == 0)
  599.         changed |= redirect_jump (insn, 0);
  600.  
  601.       /* Detect jump to following insn.  */
  602.       if (reallabelprev == insn && condjump_p (insn))
  603.         {
  604.           delete_jump (insn);
  605.           changed = 1;
  606.           continue;
  607.         }
  608.  
  609.       /* If we have an unconditional jump preceeded by a USE, try to put
  610.          the USE before the target and jump there.  This simplifies many
  611.          of the optimizations below since we don't have to worry about
  612.          dealing with these USE insns.  We only do this if the label
  613.          being branch to already has the identical USE or if code
  614.          never falls through to that label.  */
  615.  
  616.       if (this_is_simplejump
  617.           && (temp = prev_nonnote_insn (insn)) != 0
  618.           && GET_CODE (temp) == INSN && GET_CODE (PATTERN (temp)) == USE
  619.           && (temp1 = prev_nonnote_insn (JUMP_LABEL (insn))) != 0
  620.           && (GET_CODE (temp1) == BARRIER
  621.           || (GET_CODE (temp1) == INSN
  622.               && rtx_equal_p (PATTERN (temp), PATTERN (temp1)))))
  623.         {
  624.           if (GET_CODE (temp1) == BARRIER)
  625.         {
  626.           reorder_insns (temp, temp, temp1);
  627.           temp1 = NEXT_INSN (temp1);
  628.         }
  629.           else
  630.         delete_insn (temp);
  631.  
  632.           redirect_jump (insn, get_label_before (temp1));
  633.           reallabelprev = prev_real_insn (temp1);
  634.           changed = 1;
  635.         }
  636.  
  637.       /* Simplify   if (...) x = a; else x = b; by converting it
  638.          to         x = b; if (...) x = a;
  639.          if B is sufficiently simple, the test doesn't involve X,
  640.          and nothing in the test modifies B or X.
  641.  
  642.          INSN is the branch over the `else' part. 
  643.  
  644.          We set:
  645.  
  646.          TEMP to the jump insn preceeding "x = a;"
  647.          TEMP1 to X
  648.          TEMP2 to the insn that sets "x = b;"  */
  649.  
  650.       if (this_is_simplejump
  651.           && (temp3 = prev_active_insn (insn)) != 0
  652.           && GET_CODE (temp3) == INSN
  653.           && GET_CODE (PATTERN (temp3)) == SET
  654.           && GET_CODE (temp1 = SET_DEST (PATTERN (temp3))) == REG
  655. #ifdef SMALL_REGISTER_CLASSES
  656.           && REGNO (temp1) >= FIRST_PSEUDO_REGISTER
  657. #endif
  658.           && (temp2 = next_active_insn (insn)) != 0
  659.           && GET_CODE (temp2) == INSN
  660.           && GET_CODE (PATTERN (temp2)) == SET
  661.           && rtx_equal_p (SET_DEST (PATTERN (temp2)), temp1)
  662.           && (GET_CODE (SET_SRC (PATTERN (temp2))) == REG
  663.           || GET_CODE (SET_SRC (PATTERN (temp2))) == CONST_INT)
  664.           && (temp = prev_active_insn (temp3)) != 0
  665.           && condjump_p (temp) && ! simplejump_p (temp)
  666.           /* TEMP must skip over the "x = a;" insn */
  667.           && prev_real_insn (JUMP_LABEL (temp)) == insn
  668.           && no_labels_between_p (insn, JUMP_LABEL (temp))
  669.           /* There must be no other entries to the "x = b;" insn.  */
  670.           && no_labels_between_p (JUMP_LABEL (temp), temp2)
  671.           /* INSN must either branch to the insn after TEMP2 or the insn
  672.          after TEMP2 must branch to the same place as INSN.  */
  673.           && (reallabelprev == temp2
  674.           || ((temp4 = next_active_insn (temp2)) != 0
  675.               && simplejump_p (temp4)
  676.               && JUMP_LABEL (temp4) == JUMP_LABEL (insn))))
  677.         {
  678.           /* The test expression, X, may be a complicated test with
  679.          multiple branches.  See if we can find all the uses of
  680.          the label that TEMP branches to without hitting a CALL_INSN
  681.          or a jump to somewhere else.  */
  682.           rtx target = JUMP_LABEL (temp);
  683.           int nuses = LABEL_NUSES (target);
  684.           rtx p;
  685.  
  686.           for (p = temp; nuses && p; p = prev_nonnote_insn (p))
  687.         {
  688.           if (GET_CODE (p) == JUMP_INSN)
  689.             {
  690.               if (condjump_p (p) && ! simplejump_p (p)
  691.               && JUMP_LABEL (p) == target)
  692.             nuses--;
  693.               else
  694.             break;
  695.             }
  696.           else if (GET_CODE (p) == CALL_INSN)
  697.             break;
  698.         }
  699.  
  700. #ifdef HAVE_cc0
  701.           /* We cannot insert anything between a set of cc and its use
  702.          so if P uses cc0, we must back up to the previous insn.  */
  703.           if (p && GET_RTX_CLASS (GET_CODE (p)) == 'i'
  704.           && sets_cc0_p (PATTERN (p)))
  705.         p = prev_nonnote_insn (p);
  706. #endif
  707.  
  708.           /* If we found all the uses and there was no data conflict, we
  709.          can move the assignment unless we can branch into the middle
  710.          from somewhere.  */
  711.           if (nuses == 0 && p
  712.           && no_labels_between_p (p, insn)
  713.           && ! reg_referenced_between_p (temp1, p, NEXT_INSN (temp3))
  714.           && ! reg_set_between_p (temp1, p, temp3)
  715.  
  716.           && (GET_CODE (SET_SRC (PATTERN (temp2))) == CONST_INT
  717.               || ! reg_set_between_p (SET_SRC (PATTERN (temp2)),
  718.                           p, temp2)))
  719.         {
  720.           reorder_insns (temp2, temp2, p);
  721.  
  722.           /* Set NEXT to an insn that we know won't go away.  */
  723.           next = next_active_insn (insn);
  724.  
  725.           /* Delete the jump around the set.  Note that we must do
  726.              this before we redirect the test jumps so that it won't
  727.              delete the code immediately following the assignment
  728.              we moved (which might be a jump).  */
  729.  
  730.           delete_insn (insn);
  731.  
  732.           /* We either have two consecutive labels or a jump to
  733.              a jump, so adjust all the JUMP_INSNs to branch to where
  734.              INSN branches to.  */
  735.           for (p = NEXT_INSN (p); p != next; p = NEXT_INSN (p))
  736.             if (GET_CODE (p) == JUMP_INSN)
  737.               redirect_jump (p, target);
  738.  
  739.           changed = 1;
  740.           continue;
  741.         }
  742.         }
  743.  
  744.       /* If we have x = a; if (...) x = b;
  745.          and either A or B is zero, try to use a store-flag insn to
  746.          avoid the jump.  (If the jump would be faster, the machine
  747.          should not have defined the scc insns!).  These case are often
  748.          made by the previous optimization.
  749.  
  750.          INSN here is the jump around the store.  We set:
  751.  
  752.          TEMP to the "x = b;" insn.
  753.          TEMP1 to X.
  754.          TEMP2 to B.
  755.          TEMP3 to A.
  756.          TEMP4 to the condition being tested.  */
  757.  
  758.       if (
  759.           /* We can't do this after reload has completed.  */
  760.           ! noop_moves
  761.           && this_is_condjump && ! this_is_simplejump
  762.           /* Set TEMP to the "x = b;" insn.  */
  763.           && (temp = next_nonnote_insn (insn)) != 0
  764.           && GET_CODE (temp) == INSN && GET_CODE (PATTERN (temp)) == SET
  765.           && GET_CODE (temp1 = SET_DEST (PATTERN (temp))) == REG
  766.           && GET_MODE_CLASS (GET_MODE (temp1)) == MODE_INT
  767.           && (GET_CODE (temp2 = SET_SRC (PATTERN (temp))) == REG
  768.           || GET_CODE (temp2) == CONST_INT)
  769.           && (temp3 = reg_set_last (temp1, insn)) != 0
  770.           && (GET_CODE (temp3) == REG || GET_CODE (temp3) == CONST_INT)
  771.           /* INSN must either branch to the insn after TEMP or the insn
  772.          after TEMP must branch to the same place as INSN.  */
  773.           && (reallabelprev == temp
  774.           || ((temp4 = next_active_insn (temp)) != 0
  775.               && simplejump_p (temp4)
  776.               && JUMP_LABEL (temp4) == JUMP_LABEL (insn)))
  777.           && (temp4 = get_condition (insn, 0)) != 0
  778.           /* If B is zero, OK; if A is zero, can only do this if we
  779.          can reverse the condition.  */
  780.           && (temp2 == const0_rtx
  781.           || (temp3 == const0_rtx
  782.               && (can_reverse_comparison_p (temp4, insn)))))
  783.         {
  784.           enum rtx_code code = GET_CODE (temp4);
  785.           rtx yes = temp3, var = temp1;
  786.           int normalizep;
  787.           rtx target, win;
  788.  
  789.           /* If necessary, reverse the condition.  */
  790.           if (temp3 == const0_rtx)
  791.         code = reverse_condition (code), yes = temp2;
  792.  
  793.           /* See if we can do this with a store-flag insn. */
  794.           start_sequence ();
  795.  
  796.           /* If YES is the constant 1, it is best to just compute
  797.          the result directly.  If YES is constant and STORE_FLAG_VALUE
  798.          includes all of its bits, it is best to compute the flag
  799.          value unnormalized and `and' it with YES.  Otherwise,
  800.          normalize to -1 and `and' with YES.  */
  801.           normalizep = (yes == const1_rtx ? 1
  802.                 : (GET_CODE (yes) == CONST_INT
  803.                    && (INTVAL (yes) & ~ STORE_FLAG_VALUE) == 0) ? 0
  804.                 : -1);
  805.  
  806.           win = emit_store_flag (var, code, XEXP (temp4, 0),
  807.                      XEXP (temp4, 1), VOIDmode,
  808.                      (code == LTU || code == LEU 
  809.                       || code == GEU || code == GTU),
  810.                      normalizep);
  811.           if (win)
  812.         {
  813.           rtx seq;
  814.  
  815.           if (win != var)
  816.             emit_move_insn (var, win);
  817.           if (normalizep != 1)
  818.             expand_and (yes, var, var);
  819.           seq = gen_sequence ();
  820.           end_sequence ();
  821.           emit_insn_after (seq, temp);
  822.           delete_insn (temp);
  823.           next = NEXT_INSN (insn);
  824. #ifdef HAVE_cc0
  825.           delete_insn (prev_nonnote_insn (insn));
  826. #endif
  827.           delete_insn (insn);
  828.           changed = 1;
  829.         }
  830.           else
  831.         end_sequence ();
  832.         }
  833.  
  834.       /* Simplify   if (...) x = 1; else {...}  if (x) ...
  835.          We recognize this case scanning backwards as well.
  836.  
  837.          TEMP is the assignment to x;
  838.          TEMP1 is the label at the head of the second if.  */
  839.       /* @@ This should call get_condition to find the values being
  840.          compared, instead of looking for a COMPARE insn when HAVE_cc0
  841.          is not defined.  This would allow it to work on the m88k.  */
  842.       /* @@ This optimization is only safe before cse is run if HAVE_cc0
  843.          is not defined and the condition is tested by a separate compare
  844.          insn.  This is because the code below assumes that the result
  845.          of the compare dies in the following branch.  */
  846.       else if (this_is_simplejump
  847.            /* Safe to skip USE and CLOBBER insns here
  848.               since they will not be deleted.  */
  849.            && (temp = prev_active_insn (insn))
  850.            && no_labels_between_p (temp, insn)
  851.            && GET_CODE (temp) == INSN
  852.            && GET_CODE (PATTERN (temp)) == SET
  853.            && GET_CODE (SET_DEST (PATTERN (temp))) == REG
  854.            && CONSTANT_P (SET_SRC (PATTERN (temp)))
  855.            && (temp1 = next_active_insn (JUMP_LABEL (insn)))
  856.            /* If we find that the next value tested is `x'
  857.               (TEMP1 is the insn where this happens), win.  */
  858.            && GET_CODE (temp1) == INSN
  859.            && GET_CODE (PATTERN (temp1)) == SET
  860. #ifdef HAVE_cc0
  861.            /* Does temp1 `tst' the value of x?  */
  862.            && SET_SRC (PATTERN (temp1)) == SET_DEST (PATTERN (temp))
  863.            && SET_DEST (PATTERN (temp1)) == cc0_rtx
  864.            && (temp1 = next_nonnote_insn (temp1))
  865. #else
  866.            /* Does temp1 compare the value of x against zero?  */
  867.            && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
  868.            && XEXP (SET_SRC (PATTERN (temp1)), 1) == const0_rtx
  869.            && (XEXP (SET_SRC (PATTERN (temp1)), 0)
  870.                == SET_DEST (PATTERN (temp)))
  871.            && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
  872.            && (temp1 = find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
  873. #endif
  874.            && condjump_p (temp1))
  875.         {
  876.           /* Get the if_then_else from the condjump.  */
  877.           rtx choice = SET_SRC (PATTERN (temp1));
  878.           if (GET_CODE (choice) == IF_THEN_ELSE
  879.           && (GET_CODE (XEXP (choice, 0)) == EQ
  880.               || GET_CODE (XEXP (choice, 0)) == NE))
  881.         {
  882.           rtx ultimate;
  883.           int want_nonzero = (GET_CODE (XEXP (choice, 0)) == NE);
  884.           /* Get the place that condjump will jump to
  885.              if it is reached from here.  */
  886.           if ((SET_SRC (PATTERN (temp)) != const0_rtx)
  887.               == want_nonzero)
  888.             ultimate = XEXP (choice, 1);
  889.           else
  890.             ultimate = XEXP (choice, 2);
  891.           /* Get it as a CODE_LABEL.  */
  892.           if (ultimate == pc_rtx)
  893.             ultimate = get_label_after (temp1);
  894.           else
  895.             /* Get the label out of the LABEL_REF.  */
  896.             ultimate = XEXP (ultimate, 0);
  897.           changed |= redirect_jump (insn, ultimate);
  898.         }
  899.         }
  900. #if 0
  901.       /* @@ This needs a bit of work before it will be right.
  902.  
  903.          Any type of comparison can be accepted for the first and
  904.          second compare.  When rewriting the first jump, we must
  905.          compute the what conditions can reach label3, and use the
  906.          appropriate code.  We can not simply reverse/swap the code
  907.          of the first jump.  In some cases, the second jump must be
  908.          rewritten also.
  909.  
  910.          For example, 
  911.          <  == converts to >  ==
  912.          <  != converts to ==  >
  913.          etc.
  914.  
  915.          If the code is written to only accept an '==' test for the second
  916.          compare, then all that needs to be done is to swap the condition
  917.          of the first branch.
  918.  
  919.          It is questionable whether we want this optimization anyways,
  920.          since if the user wrote code like this because he/she knew that
  921.          the jump to label1 is taken most of the time, then rewritting
  922.          this gives slower code.  */
  923.       /* @@ This should call get_condition to find the values being
  924.          compared, instead of looking for a COMPARE insn when HAVE_cc0
  925.          is not defined.  This would allow it to work on the m88k.  */
  926.       /* @@ This optimization is only safe before cse is run if HAVE_cc0
  927.          is not defined and the condition is tested by a separate compare
  928.          insn.  This is because the code below assumes that the result
  929.          of the compare dies in the following branch.  */
  930.  
  931.       /* Simplify  test a ~= b
  932.                condjump label1;
  933.                test a == b
  934.                condjump label2;
  935.                jump label3;
  936.                label1:
  937.  
  938.          rewriting as
  939.                test a ~~= b
  940.                condjump label3
  941.                test a == b
  942.                condjump label2
  943.                label1:
  944.  
  945.          where ~= is an inequality, e.g. >, and ~~= is the swapped
  946.          inequality, e.g. <.
  947.  
  948.          We recognize this case scanning backwards.
  949.  
  950.          TEMP is the conditional jump to `label2';
  951.          TEMP1 is the test for `a == b';
  952.          TEMP2 is the conditional jump to `label1';
  953.          TEMP3 is the test for `a ~= b'.  */
  954.       else if (this_is_simplejump
  955.            && (temp = prev_active_insn (insn))
  956.            && no_labels_between_p (temp, insn)
  957.            && condjump_p (temp)
  958.            && (temp1 = prev_active_insn (temp))
  959.            && no_labels_between_p (temp1, temp)
  960.            && GET_CODE (temp1) == INSN
  961.            && GET_CODE (PATTERN (temp1)) == SET
  962. #ifdef HAVE_cc0
  963.            && sets_cc0_p (PATTERN (temp1)) == 1
  964. #else
  965.            && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
  966.            && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
  967.            && (temp == find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
  968. #endif
  969.            && (temp2 = prev_active_insn (temp1))
  970.            && no_labels_between_p (temp2, temp1)
  971.            && condjump_p (temp2)
  972.            && JUMP_LABEL (temp2) == next_nonnote_insn (NEXT_INSN (insn))
  973.            && (temp3 = prev_active_insn (temp2))
  974.            && no_labels_between_p (temp3, temp2)
  975.            && GET_CODE (PATTERN (temp3)) == SET
  976.            && rtx_equal_p (SET_DEST (PATTERN (temp3)),
  977.                    SET_DEST (PATTERN (temp1)))
  978.            && rtx_equal_p (SET_SRC (PATTERN (temp1)),
  979.                    SET_SRC (PATTERN (temp3)))
  980.            && ! inequality_comparisons_p (PATTERN (temp))
  981.            && inequality_comparisons_p (PATTERN (temp2))))
  982.       {
  983.           rtx fallthrough_label = JUMP_LABEL (temp2);
  984.  
  985.           ++LABEL_NUSES (fallthrough_label);
  986.           if (swap_jump (temp2, JUMP_LABEL (insn)))
  987.         {
  988.           delete_insn (insn);
  989.           changed = 1;
  990.         }
  991.  
  992.           if (--LABEL_NUSES (fallthrough_label) == 0)
  993.         delete_insn (fallthrough_label);
  994.         }
  995. #endif
  996.       /* Simplify  if (...) {... x = 1;} if (x) ...
  997.  
  998.          We recognize this case backwards.
  999.  
  1000.          TEMP is the test of `x';
  1001.          TEMP1 is the assignment to `x' at the end of the
  1002.          previous statement.  */
  1003.       /* @@ This should call get_condition to find the values being
  1004.          compared, instead of looking for a COMPARE insn when HAVE_cc0
  1005.          is not defined.  This would allow it to work on the m88k.  */
  1006.       /* @@ This optimization is only safe before cse is run if HAVE_cc0
  1007.          is not defined and the condition is tested by a separate compare
  1008.          insn.  This is because the code below assumes that the result
  1009.          of the compare dies in the following branch.  */
  1010.  
  1011.       /* ??? This has to be turned off.  The problem is that the
  1012.          unconditional jump might indirectly end up branching to the
  1013.          label between TEMP1 and TEMP.  We can't detect this, in general,
  1014.          since it may become a jump to there after further optimizations.
  1015.          If that jump is done, it will be deleted, so we will retry
  1016.          this optimization in the next pass, thus an infinite loop.
  1017.  
  1018.          The present code prevents this by putting the jump after the
  1019.          label, but this is not logically correct.  */
  1020. #if 0
  1021.       else if (this_is_condjump
  1022.            /* Safe to skip USE and CLOBBER insns here
  1023.               since they will not be deleted.  */
  1024.            && (temp = prev_active_insn (insn))
  1025.            && no_labels_between_p (temp, insn)
  1026.            && GET_CODE (temp) == INSN
  1027.            && GET_CODE (PATTERN (temp)) == SET
  1028. #ifdef HAVE_cc0
  1029.            && sets_cc0_p (PATTERN (temp)) == 1
  1030.            && GET_CODE (SET_SRC (PATTERN (temp))) == REG
  1031. #else
  1032.            /* Temp must be a compare insn, we can not accept a register
  1033.               to register move here, since it may not be simply a
  1034.               tst insn.  */
  1035.            && GET_CODE (SET_SRC (PATTERN (temp))) == COMPARE
  1036.            && XEXP (SET_SRC (PATTERN (temp)), 1) == const0_rtx
  1037.            && GET_CODE (XEXP (SET_SRC (PATTERN (temp)), 0)) == REG
  1038.            && GET_CODE (SET_DEST (PATTERN (temp))) == REG
  1039.            && insn == find_next_ref (SET_DEST (PATTERN (temp)), temp)
  1040. #endif
  1041.            /* May skip USE or CLOBBER insns here
  1042.               for checking for opportunity, since we
  1043.               take care of them later.  */
  1044.            && (temp1 = prev_active_insn (temp))
  1045.            && GET_CODE (temp1) == INSN
  1046.            && GET_CODE (PATTERN (temp1)) == SET
  1047. #ifdef HAVE_cc0
  1048.            && SET_SRC (PATTERN (temp)) == SET_DEST (PATTERN (temp1))
  1049. #else
  1050.            && (XEXP (SET_SRC (PATTERN (temp)), 0)
  1051.                == SET_DEST (PATTERN (temp1)))
  1052. #endif
  1053.            && CONSTANT_P (SET_SRC (PATTERN (temp1)))
  1054.            /* If this isn't true, cse will do the job.  */
  1055.            && ! no_labels_between_p (temp1, temp))
  1056.         {
  1057.           /* Get the if_then_else from the condjump.  */
  1058.           rtx choice = SET_SRC (PATTERN (insn));
  1059.           if (GET_CODE (choice) == IF_THEN_ELSE
  1060.           && (GET_CODE (XEXP (choice, 0)) == EQ
  1061.               || GET_CODE (XEXP (choice, 0)) == NE))
  1062.         {
  1063.           int want_nonzero = (GET_CODE (XEXP (choice, 0)) == NE);
  1064.           rtx last_insn;
  1065.           rtx ultimate;
  1066.           rtx p;
  1067.  
  1068.           /* Get the place that condjump will jump to
  1069.              if it is reached from here.  */
  1070.           if ((SET_SRC (PATTERN (temp1)) != const0_rtx)
  1071.               == want_nonzero)
  1072.             ultimate = XEXP (choice, 1);
  1073.           else
  1074.             ultimate = XEXP (choice, 2);
  1075.           /* Get it as a CODE_LABEL.  */
  1076.           if (ultimate == pc_rtx)
  1077.             ultimate = get_label_after (insn);
  1078.           else
  1079.             /* Get the label out of the LABEL_REF.  */
  1080.             ultimate = XEXP (ultimate, 0);
  1081.  
  1082.           /* Insert the jump after any USE or CLOBBER
  1083.              that follows TEMP1.  */
  1084.           last_insn = prev_real_insn (temp);
  1085.  
  1086.           /* If we would be branching to the next insn, the jump
  1087.              would immediately be deleted and the re-inserted in
  1088.              a subsequent pass over the code.  So don't do anything
  1089.              in that case.  */
  1090.           if (next_active_insn (last_insn)
  1091.               != next_active_insn (ultimate))
  1092.             {
  1093.               emit_barrier_after (last_insn);
  1094.               p = emit_jump_insn_after (gen_jump (ultimate),
  1095.                         last_insn);
  1096.               JUMP_LABEL (p) = ultimate;
  1097.               ++LABEL_NUSES (ultimate);
  1098.               if (INSN_UID (ultimate) < max_jump_chain
  1099.               && INSN_CODE (p) < max_jump_chain)
  1100.             {
  1101.               jump_chain[INSN_UID (p)]
  1102.                 = jump_chain[INSN_UID (ultimate)];
  1103.               jump_chain[INSN_UID (ultimate)] = p;
  1104.             }
  1105.               changed = 1;
  1106.               continue;
  1107.             }
  1108.         }
  1109.         }
  1110. #endif
  1111.       /* Detect a conditional jump going to the same place
  1112.          as an immediately following unconditional jump.  */
  1113.       else if (this_is_condjump
  1114.            && (temp = next_active_insn (insn)) != 0
  1115.            && simplejump_p (temp)
  1116.            && (next_active_insn (JUMP_LABEL (insn))
  1117.                == next_active_insn (JUMP_LABEL (temp))))
  1118.         {
  1119.           delete_jump (insn);
  1120.           changed = 1;
  1121.           continue;
  1122.         }
  1123.       /* Detect jumping over an unconditional jump.  */
  1124.  
  1125.       else if (reallabelprev != 0
  1126.            && GET_CODE (reallabelprev) == JUMP_INSN
  1127.            && prev_active_insn (reallabelprev) == insn
  1128.            && no_labels_between_p (insn, reallabelprev)
  1129.            && simplejump_p (reallabelprev))
  1130.         {
  1131.           /* When we invert the unconditional jump, we will be
  1132.          decrementing the usage count of its old label.
  1133.          Make sure that we don't delete it now because that
  1134.          might cause the following code to be deleted.  */
  1135.           rtx prev_uses = prev_nonnote_insn (reallabelprev);
  1136.           rtx prev_label = JUMP_LABEL (insn);
  1137.  
  1138.           ++LABEL_NUSES (prev_label);
  1139.  
  1140.           if (invert_jump (insn, JUMP_LABEL (reallabelprev)))
  1141.         {
  1142.           /* It is very likely that if there are USE insns before
  1143.              this jump, they hold REG_DEAD notes.  These REG_DEAD
  1144.              notes are no longer valid due to this optimization,
  1145.              and will cause the life-analysis that following passes
  1146.              (notably delayed-branch scheduling) to think that
  1147.              these registers are dead when they are not.
  1148.  
  1149.              To prevent this trouble, we just remove the USE insns
  1150.              from the insn chain.  */
  1151.  
  1152.           while (prev_uses && GET_CODE (prev_uses) == INSN
  1153.              && GET_CODE (PATTERN (prev_uses)) == USE)
  1154.             {
  1155.               rtx useless = prev_uses;
  1156.               prev_uses = prev_nonnote_insn (prev_uses);
  1157.               delete_insn (useless);
  1158.             }
  1159.  
  1160.           delete_insn (reallabelprev);
  1161.           next = insn;
  1162.           changed = 1;
  1163.         }
  1164.  
  1165.           /* We can now safely delete the label if it is unreferenced
  1166.          since the delete_insn above has deleted the BARRIER.  */
  1167.           if (--LABEL_NUSES (prev_label) == 0)
  1168.         delete_insn (prev_label);
  1169.           continue;
  1170.         }
  1171.       else
  1172.         {
  1173.           /* Detect a jump to a jump.  */
  1174.  
  1175.           nlabel = follow_jumps (JUMP_LABEL (insn), noop_moves);
  1176.           if (nlabel != JUMP_LABEL (insn)
  1177.           && redirect_jump (insn, nlabel))
  1178.         {
  1179.           changed = 1;
  1180.           next = insn;
  1181.         }
  1182.  
  1183.           /* Look for   if (foo) bar; else break;  */
  1184.           /* The insns look like this:
  1185.          insn = condjump label1;
  1186.          ...range1 (some insns)...
  1187.          jump label2;
  1188.          label1:
  1189.          ...range2 (some insns)...
  1190.          jump somewhere unconditionally
  1191.          label2:  */
  1192.           {
  1193.         rtx label1 = next_label (insn);
  1194.         rtx range1end = label1 ? prev_active_insn (label1) : 0;
  1195.         /* Don't do this optimization on the first round, so that
  1196.            jump-around-a-jump gets simplified before we ask here
  1197.            whether a jump is unconditional.
  1198.  
  1199.            Also don't do it when we are called after reload since
  1200.            it will confuse reorg.  */
  1201.         if (! first
  1202.             && (noop_moves ? ! flag_delayed_branch : 1)
  1203.             /* Make sure INSN is something we can invert.  */
  1204.             && condjump_p (insn)
  1205.             && label1 != 0
  1206.             && JUMP_LABEL (insn) == label1
  1207.             && LABEL_NUSES (label1) == 1
  1208.             && GET_CODE (range1end) == JUMP_INSN
  1209.             && simplejump_p (range1end))
  1210.           {
  1211.             rtx label2 = next_label (label1);
  1212.             rtx range2end = label2 ? prev_active_insn (label2) : 0;
  1213.             if (range1end != range2end
  1214.             && JUMP_LABEL (range1end) == label2
  1215.             && GET_CODE (range2end) == JUMP_INSN
  1216.             && GET_CODE (NEXT_INSN (range2end)) == BARRIER
  1217.             /* Invert the jump condition, so we
  1218.                still execute the same insns in each case.  */
  1219.             && invert_jump (insn, label1))
  1220.               {
  1221.             rtx range1beg = next_active_insn (insn);
  1222.             rtx range2beg = next_active_insn (label1);
  1223.             rtx range1after, range2after;
  1224.             rtx range1before, range2before;
  1225.  
  1226.             /* Don't move NOTEs for blocks or loops; shift them
  1227.                outside the ranges, where they'll stay put.  */
  1228.             squeeze_notes (range1beg, range1end);
  1229.             squeeze_notes (range2beg, range2end);
  1230.  
  1231.             /* Get current surrounds of the 2 ranges.  */
  1232.             range1before = PREV_INSN (range1beg);
  1233.             range2before = PREV_INSN (range2beg);
  1234.             range1after = NEXT_INSN (range1end);
  1235.             range2after = NEXT_INSN (range2end);
  1236.  
  1237.             /* Splice range2 where range1 was.  */
  1238.             NEXT_INSN (range1before) = range2beg;
  1239.             PREV_INSN (range2beg) = range1before;
  1240.             NEXT_INSN (range2end) = range1after;
  1241.             PREV_INSN (range1after) = range2end;
  1242.             /* Splice range1 where range2 was.  */
  1243.             NEXT_INSN (range2before) = range1beg;
  1244.             PREV_INSN (range1beg) = range2before;
  1245.             NEXT_INSN (range1end) = range2after;
  1246.             PREV_INSN (range2after) = range1end;
  1247.             changed = 1;
  1248.             continue;
  1249.               }
  1250.           }
  1251.           }
  1252.  
  1253.           /* Now that the jump has been tensioned,
  1254.          try cross jumping: check for identical code
  1255.          before the jump and before its target label. */
  1256.  
  1257.           /* First, cross jumping of conditional jumps:  */
  1258.  
  1259.           if (cross_jump && condjump_p (insn))
  1260.         {
  1261.           rtx newjpos, newlpos;
  1262.           rtx x = prev_real_insn (JUMP_LABEL (insn));
  1263.  
  1264.           /* A conditional jump may be crossjumped
  1265.              only if the place it jumps to follows
  1266.              an opposing jump that comes back here.  */
  1267.  
  1268.           if (x != 0 && ! jump_back_p (x, insn))
  1269.             /* We have no opposing jump;
  1270.                cannot cross jump this insn.  */
  1271.             x = 0;
  1272.  
  1273.           newjpos = 0;
  1274.           /* TARGET is nonzero if it is ok to cross jump
  1275.              to code before TARGET.  If so, see if matches.  */
  1276.           if (x != 0)
  1277.             find_cross_jump (insn, x, 2,
  1278.                      &newjpos, &newlpos);
  1279.  
  1280.           if (newjpos != 0)
  1281.             {
  1282.               do_cross_jump (insn, newjpos, newlpos);
  1283.               /* Make the old conditional jump
  1284.              into an unconditional one.  */
  1285.               SET_SRC (PATTERN (insn))
  1286.             = gen_rtx (LABEL_REF, VOIDmode, JUMP_LABEL (insn));
  1287.               INSN_CODE (insn) = -1;
  1288.               emit_barrier_after (insn);
  1289.               /* Add to jump_chain unless this is a new label
  1290.              whose UID is too large. */
  1291.               if (INSN_UID (JUMP_LABEL (insn)) < max_jump_chain)
  1292.             {
  1293.               jump_chain[INSN_UID (insn)]
  1294.                 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
  1295.               jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
  1296.             }
  1297.               changed = 1;
  1298.               next = insn;
  1299.             }
  1300.         }
  1301.  
  1302.           /* Cross jumping of unconditional jumps:
  1303.          a few differences.  */
  1304.  
  1305.           if (cross_jump && simplejump_p (insn))
  1306.         {
  1307.           rtx newjpos, newlpos;
  1308.           rtx target;
  1309.  
  1310.           newjpos = 0;
  1311.  
  1312.           /* TARGET is nonzero if it is ok to cross jump
  1313.              to code before TARGET.  If so, see if matches.  */
  1314.           find_cross_jump (insn, JUMP_LABEL (insn), 1,
  1315.                    &newjpos, &newlpos);
  1316.  
  1317.           /* If cannot cross jump to code before the label,
  1318.              see if we can cross jump to another jump to
  1319.              the same label.  */
  1320.           /* Try each other jump to this label.  */
  1321.           if (INSN_UID (JUMP_LABEL (insn)) < max_uid)
  1322.             for (target = jump_chain[INSN_UID (JUMP_LABEL (insn))];
  1323.              target != 0 && newjpos == 0;
  1324.              target = jump_chain[INSN_UID (target)])
  1325.               if (target != insn
  1326.               && JUMP_LABEL (target) == JUMP_LABEL (insn)
  1327.               /* Ignore TARGET if it's deleted.  */
  1328.               && ! INSN_DELETED_P (target))
  1329.             find_cross_jump (insn, target, 2,
  1330.                      &newjpos, &newlpos);
  1331.  
  1332.           if (newjpos != 0)
  1333.             {
  1334.               do_cross_jump (insn, newjpos, newlpos);
  1335.               changed = 1;
  1336.               next = insn;
  1337.             }
  1338.         }
  1339.  
  1340.           /* This code was dead in the previous jump.c!  */
  1341.           if (cross_jump && GET_CODE (PATTERN (insn)) == RETURN)
  1342.         {
  1343.           /* Return insns all "jump to the same place"
  1344.              so we can cross-jump between any two of them.  */
  1345.  
  1346.           rtx newjpos, newlpos, target;
  1347.  
  1348.           newjpos = 0;
  1349.  
  1350.           /* If cannot cross jump to code before the label,
  1351.              see if we can cross jump to another jump to
  1352.              the same label.  */
  1353.           /* Try each other jump to this label.  */
  1354.           for (target = jump_chain[0];
  1355.                target != 0 && newjpos == 0;
  1356.                target = jump_chain[INSN_UID (target)])
  1357.             if (target != insn
  1358.             && ! INSN_DELETED_P (target)
  1359.             && GET_CODE (PATTERN (target)) == RETURN)
  1360.               find_cross_jump (insn, target, 2,
  1361.                        &newjpos, &newlpos);
  1362.  
  1363.           if (newjpos != 0)
  1364.             {
  1365.               do_cross_jump (insn, newjpos, newlpos);
  1366.               changed = 1;
  1367.               next = insn;
  1368.             }
  1369.         }
  1370.         }
  1371.     }
  1372.  
  1373.       first = 0;
  1374.     }
  1375.  
  1376.   /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
  1377.      If so, delete it, and record that this function can drop off the end.  */
  1378.  
  1379.   insn = last_insn;
  1380.   {
  1381.     int n_labels = 1;
  1382.     while (insn
  1383.        /* One label can follow the end-note: the return label.  */
  1384.        && ((GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
  1385.            /* Ordinary insns can follow it if returning a structure.  */
  1386.            || GET_CODE (insn) == INSN
  1387.            /* If machine uses explicit RETURN insns, no epilogue,
  1388.           then one of them follows the note.  */
  1389.            || (GET_CODE (insn) == JUMP_INSN
  1390.            && GET_CODE (PATTERN (insn)) == RETURN)
  1391.            /* Other kinds of notes can follow also.  */
  1392.            || (GET_CODE (insn) == NOTE
  1393.            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)))
  1394.       insn = PREV_INSN (insn);
  1395.   }
  1396.   if (insn && GET_CODE (insn) == NOTE
  1397.       && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END)
  1398.     {
  1399.       extern int current_function_returns_null;
  1400.       current_function_returns_null = 1;
  1401.       delete_insn (insn);
  1402.     }
  1403.  
  1404.   /* Show JUMP_CHAIN no longer valid.  */
  1405.   jump_chain = 0;
  1406. }
  1407.  
  1408. /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
  1409.    jump.  Assume that this unconditional jump is to the exit test code.  If
  1410.    the code is sufficiently simple, make a copy of it before INSN,
  1411.    followed by a jump to the exit of the loop.  Then delete the unconditional
  1412.    jump after INSN.
  1413.  
  1414.    Note that it is possible we can get confused here if the jump immediately
  1415.    after the loop start branches outside the loop but within an outer loop.
  1416.    If we are near the exit of that loop, we will copy its exit test.  This
  1417.    will not generate incorrect code, but could suppress some optimizations.
  1418.    However, such cases are degenerate loops anyway.
  1419.  
  1420.    Return 1 if we made the change, else 0.  */
  1421.  
  1422. static int
  1423. duplicate_loop_exit_test (loop_start)
  1424.      rtx loop_start;
  1425. {
  1426.   rtx insn, set, p;
  1427.   rtx copy, link;
  1428.   int num_insns = 0;
  1429.   rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
  1430.   rtx lastexit;
  1431.   int max_reg = max_reg_num ();
  1432.   rtx *reg_map = 0;
  1433.  
  1434.   /* Scan the exit code.  We do not perform this optimization if any insn:
  1435.  
  1436.          is a CALL_INSN
  1437.      is a CODE_LABEL
  1438.      has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
  1439.      is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
  1440.      is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
  1441.           are not valid
  1442.  
  1443.      Also, don't do this if the exit code is more than 20 insns.  */
  1444.  
  1445.   for (insn = exitcode;
  1446.        insn
  1447.        && ! (GET_CODE (insn) == NOTE
  1448.          && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
  1449.        insn = NEXT_INSN (insn))
  1450.     {
  1451.       switch (GET_CODE (insn))
  1452.     {
  1453.     case CODE_LABEL:
  1454.     case CALL_INSN:
  1455.       return 0;
  1456.     case NOTE:
  1457.       if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
  1458.           || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
  1459.           || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
  1460.         return 0;
  1461.       break;
  1462.     case JUMP_INSN:
  1463.     case INSN:
  1464.       if (++num_insns > 20
  1465.           || find_reg_note (insn, REG_RETVAL, 0)
  1466.           || find_reg_note (insn, REG_LIBCALL, 0))
  1467.         return 0;
  1468.       break;
  1469.     }
  1470.     }
  1471.  
  1472.   /* Unless INSN is zero, we can do the optimization.  */
  1473.   if (insn == 0)
  1474.     return 0;
  1475.  
  1476.   lastexit = insn;
  1477.  
  1478.   /* See if any insn sets a register only used in the loop exit code and
  1479.      not a user variable.  If so, replace it with a new register.  */
  1480.   for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
  1481.     if (GET_CODE (insn) == INSN
  1482.     && (set = single_set (insn)) != 0
  1483.     && GET_CODE (SET_DEST (set)) == REG
  1484.     && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
  1485.     && regno_first_uid[REGNO (SET_DEST (set))] == INSN_UID (insn))
  1486.       {
  1487.     for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
  1488.       if (regno_last_uid[REGNO (SET_DEST (set))] == INSN_UID (p))
  1489.         break;
  1490.  
  1491.     if (p != lastexit)
  1492.       {
  1493.         /* We can do the replacement.  Allocate reg_map if this is the
  1494.            first replacement we found.  */
  1495.         if (reg_map == 0)
  1496.           {
  1497.         reg_map = (rtx *) alloca (max_reg * sizeof (rtx));
  1498.         bzero (reg_map, max_reg * sizeof (rtx));
  1499.           }
  1500.  
  1501.         reg_map[REGNO (SET_DEST (set))]
  1502.           = gen_reg_rtx (GET_MODE (SET_DEST (set)));
  1503.       }
  1504.       }
  1505.  
  1506.   /* Now copy each insn.  */
  1507.   for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
  1508.     switch (GET_CODE (insn))
  1509.       {
  1510.       case BARRIER:
  1511.     copy = emit_barrier_before (loop_start);
  1512.     break;
  1513.       case NOTE:
  1514.     /* Only copy line-number notes.  */
  1515.     if (NOTE_LINE_NUMBER (insn) >= 0)
  1516.       {
  1517.         copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
  1518.         NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
  1519.       }
  1520.     break;
  1521.  
  1522.       case INSN:
  1523.     copy = emit_insn_before (copy_rtx (PATTERN (insn)), loop_start);
  1524.     if (reg_map)
  1525.       replace_regs (PATTERN (copy), reg_map, max_reg, 1);
  1526.  
  1527.     mark_jump_label (PATTERN (copy), copy, 0);
  1528.  
  1529.     /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
  1530.        make them.  */
  1531.     for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1532.       if (REG_NOTE_KIND (link) != REG_LABEL)
  1533.         REG_NOTES (copy)
  1534.           = copy_rtx (gen_rtx (EXPR_LIST, REG_NOTE_KIND (link),
  1535.                    XEXP (link, 0), REG_NOTES (copy)));
  1536.     break;
  1537.  
  1538.       case JUMP_INSN:
  1539.     copy = emit_jump_insn_before (copy_rtx (PATTERN (insn)), loop_start);
  1540.     if (reg_map)
  1541.       replace_regs (PATTERN (copy), reg_map, max_reg, 1);
  1542.     mark_jump_label (PATTERN (copy), copy, 0);
  1543.     if (REG_NOTES (insn))
  1544.       REG_NOTES (copy) = copy_rtx (REG_NOTES (insn));
  1545.     
  1546.     /* If this is a simple jump, add it to the jump chain.  */
  1547.  
  1548.     if (INSN_UID (copy) < max_jump_chain && JUMP_LABEL (copy)
  1549.         && simplejump_p (copy))
  1550.       {
  1551.         jump_chain[INSN_UID (copy)]
  1552.           = jump_chain[INSN_UID (JUMP_LABEL (copy))];
  1553.         jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
  1554.       }
  1555.     break;
  1556.  
  1557.       default:
  1558.     abort ();
  1559.       }
  1560.  
  1561.   /* Now clean up by emitting a jump to the end label and deleting the jump
  1562.      at the start of the loop.  */
  1563.   if (GET_CODE (copy) != BARRIER)
  1564.     {
  1565.       copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
  1566.                     loop_start);
  1567.       mark_jump_label (PATTERN (copy), copy, 0);
  1568.       if (INSN_UID (copy) < max_jump_chain
  1569.       && INSN_UID (JUMP_LABEL (copy)) < max_jump_chain)
  1570.     {
  1571.       jump_chain[INSN_UID (copy)]
  1572.         = jump_chain[INSN_UID (JUMP_LABEL (copy))];
  1573.       jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
  1574.     }
  1575.       emit_barrier_before (loop_start);
  1576.     }
  1577.  
  1578.   delete_insn (next_nonnote_insn (loop_start));
  1579.  
  1580.   /* Mark the exit code as the virtual top of the converted loop.  */
  1581.   emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
  1582.  
  1583.   return 1;
  1584. }
  1585.  
  1586. /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, and
  1587.    loop-end notes between START and END out before START.  Assume neither
  1588.    START nor END is such a note.  */
  1589.  
  1590. void
  1591. squeeze_notes (start, end)
  1592.      rtx start, end;
  1593. {
  1594.   rtx insn;
  1595.   rtx next;
  1596.  
  1597.   for (insn = start; insn != end; insn = next)
  1598.     {
  1599.       next = NEXT_INSN (insn);
  1600.       if (GET_CODE (insn) == NOTE
  1601.       && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
  1602.           || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
  1603.           || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
  1604.           || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
  1605.           || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
  1606.           || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
  1607.     {
  1608.       rtx prev = PREV_INSN (insn);
  1609.       PREV_INSN (insn) = PREV_INSN (start);
  1610.       NEXT_INSN (insn) = start;
  1611.       NEXT_INSN (PREV_INSN (insn)) = insn;
  1612.       PREV_INSN (NEXT_INSN (insn)) = insn;
  1613.       NEXT_INSN (prev) = next;
  1614.       PREV_INSN (next) = prev;
  1615.     }
  1616.     }
  1617. }
  1618.  
  1619. /* Compare the instructions before insn E1 with those before E2
  1620.    to find an opportunity for cross jumping.
  1621.    (This means detecting identical sequences of insns followed by
  1622.    jumps to the same place, or followed by a label and a jump
  1623.    to that label, and replacing one with a jump to the other.)
  1624.  
  1625.    Assume E1 is a jump that jumps to label E2
  1626.    (that is not always true but it might as well be).
  1627.    Find the longest possible equivalent sequences
  1628.    and store the first insns of those sequences into *F1 and *F2.
  1629.    Store zero there if no equivalent preceding instructions are found.
  1630.  
  1631.    We give up if we find a label in stream 1.
  1632.    Actually we could transfer that label into stream 2.  */
  1633.  
  1634. static void
  1635. find_cross_jump (e1, e2, minimum, f1, f2)
  1636.      rtx e1, e2;
  1637.      int minimum;
  1638.      rtx *f1, *f2;
  1639. {
  1640.   register rtx i1 = e1, i2 = e2;
  1641.   register rtx p1, p2;
  1642.  
  1643.   rtx last1 = 0, last2 = 0;
  1644.   rtx afterlast1 = 0, afterlast2 = 0;
  1645.   rtx prev1;
  1646.  
  1647.   *f1 = 0;
  1648.   *f2 = 0;
  1649.  
  1650.   while (1)
  1651.     {
  1652.       i1 = prev_nonnote_insn (i1);
  1653.  
  1654.       i2 = PREV_INSN (i2);
  1655.       while (i2 && (GET_CODE (i2) == NOTE || GET_CODE (i2) == CODE_LABEL))
  1656.     i2 = PREV_INSN (i2);
  1657.  
  1658.       if (i1 == 0)
  1659.     break;
  1660.  
  1661.       /* Don't allow the range of insns preceding E1 or E2
  1662.      to include the other (E2 or E1).  */
  1663.       if (i2 == e1 || i1 == e2)
  1664.     break;
  1665.  
  1666.       /* If we will get to this code by jumping, those jumps will be
  1667.      tensioned to go directly to the new label (before I2),
  1668.      so this cross-jumping won't cost extra.  So reduce the minimum.  */
  1669.       if (GET_CODE (i1) == CODE_LABEL)
  1670.     {
  1671.       --minimum;
  1672.       break;
  1673.     }
  1674.  
  1675.       if (i2 == 0 || GET_CODE (i1) != GET_CODE (i2))
  1676.     break;
  1677.  
  1678.       p1 = PATTERN (i1);
  1679.       p2 = PATTERN (i2);
  1680.     
  1681.       if (GET_CODE (p1) != GET_CODE (p2)
  1682.       || !rtx_renumbered_equal_p (p1, p2))
  1683.     {
  1684.       /* The following code helps take care of G++ cleanups.  */
  1685.       rtx equiv1 = find_reg_note (i1, REG_EQUAL, 0);
  1686.       rtx equiv2 = find_reg_note (i2, REG_EQUAL, 0);
  1687.  
  1688.       if (equiv1 != 0 && equiv2 != 0
  1689.           && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
  1690.         {
  1691.           rtx s1 = single_set (i1);
  1692.           rtx s2 = single_set (i2);
  1693.           if (s1 != 0 && s2 != 0
  1694.           && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
  1695.         {
  1696.           validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
  1697.           validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
  1698.           if (! rtx_renumbered_equal_p (p1, p2))
  1699.             cancel_changes (0);
  1700.           else if (apply_change_group ())
  1701.             goto win;
  1702.         }
  1703.         }
  1704.  
  1705.       /* Insns fail to match; cross jumping is limited to the following
  1706.          insns.  */
  1707.  
  1708. #ifdef HAVE_cc0
  1709.       /* Don't allow the insn after a compare to be shared by
  1710.          cross-jumping unless the compare is also shared.
  1711.          Here, if either of these non-matching insns is a compare,
  1712.          exclude the following insn from possible cross-jumping.  */
  1713.       if (sets_cc0_p (p1) || sets_cc0_p (p2))
  1714.         last1 = afterlast1, last2 = afterlast2, ++minimum;
  1715. #endif
  1716.  
  1717.       /* If cross-jumping here will feed a jump-around-jump
  1718.          optimization, this jump won't cost extra, so reduce
  1719.          the minimum.  */
  1720.       if (GET_CODE (i1) == JUMP_INSN
  1721.           && JUMP_LABEL (i1)
  1722.           && prev_real_insn (JUMP_LABEL (i1)) == e1)
  1723.         --minimum;
  1724.       break;
  1725.     }
  1726.  
  1727.     win:
  1728.       if (GET_CODE (p1) != USE && GET_CODE (p1) != CLOBBER)
  1729.     {
  1730.       /* Ok, this insn is potentially includable in a cross-jump here.  */
  1731.       afterlast1 = last1, afterlast2 = last2;
  1732.       last1 = i1, last2 = i2, --minimum;
  1733.     }
  1734.     }
  1735.  
  1736.   /* We have to be careful that we do not cross-jump into the middle of
  1737.      USE-CALL_INSN-CLOBBER sequence.  This sequence is used instead of
  1738.      putting the USE and CLOBBERs inside the CALL_INSN.  The delay slot
  1739.      scheduler needs to know what registers are used and modified by the
  1740.      CALL_INSN and needs the adjacent USE and CLOBBERs to do so.
  1741.  
  1742.      ??? At some point we should probably change this so that these are
  1743.      part of the CALL_INSN.  The way we are doing it now is a kludge that
  1744.      is now causing trouble.  */
  1745.  
  1746.   if (last1 != 0 && GET_CODE (last1) == CALL_INSN
  1747.       && (prev1 = prev_nonnote_insn (last1))
  1748.       && GET_CODE (prev1) == INSN
  1749.       && GET_CODE (PATTERN (prev1)) == USE)
  1750.     {
  1751.       /* Remove this CALL_INSN from the range we can cross-jump.  */
  1752.       last1 = next_nonnote_insn (last1);
  1753.       last2 = next_nonnote_insn (last2);
  1754.  
  1755.       minimum++;
  1756.     }
  1757.  
  1758.   /* Skip past CLOBBERS since they may be right after a CALL_INSN.  It
  1759.      isn't worth checking for the CALL_INSN.  */
  1760.   while (last1 != 0 && GET_CODE (PATTERN (last1)) == CLOBBER)
  1761.     last1 = next_nonnote_insn (last1), last2 = next_nonnote_insn (last2);
  1762.  
  1763.   if (minimum <= 0 && last1 != 0 && last1 != e1)
  1764.     *f1 = last1, *f2 = last2;
  1765. }
  1766.  
  1767. static void
  1768. do_cross_jump (insn, newjpos, newlpos)
  1769.      rtx insn, newjpos, newlpos;
  1770. {
  1771.   /* Find an existing label at this point
  1772.      or make a new one if there is none.  */
  1773.   register rtx label = get_label_before (newlpos);
  1774.  
  1775.   /* Make the same jump insn jump to the new point.  */
  1776.   if (GET_CODE (PATTERN (insn)) == RETURN)
  1777.     {
  1778.       /* Remove from jump chain of returns.  */
  1779.       delete_from_jump_chain (insn);
  1780.       /* Change the insn.  */
  1781.       PATTERN (insn) = gen_jump (label);
  1782.       INSN_CODE (insn) = -1;
  1783.       JUMP_LABEL (insn) = label;
  1784.       LABEL_NUSES (label)++;
  1785.       /* Add to new the jump chain.  */
  1786.       if (INSN_UID (label) < max_jump_chain
  1787.       && INSN_UID (insn) < max_jump_chain)
  1788.     {
  1789.       jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (label)];
  1790.       jump_chain[INSN_UID (label)] = insn;
  1791.     }
  1792.     }
  1793.   else
  1794.     redirect_jump (insn, label);
  1795.  
  1796.   /* Delete the matching insns before the jump.  */
  1797.   newjpos = PREV_INSN (newjpos);
  1798.   while (NEXT_INSN (newjpos) != insn)
  1799.     /* Don't delete line numbers.  */
  1800.     if (GET_CODE (NEXT_INSN (newjpos)) != NOTE)
  1801.       delete_insn (NEXT_INSN (newjpos));
  1802.     else
  1803.       newjpos = NEXT_INSN (newjpos);
  1804. }
  1805.  
  1806. /* Return the label before INSN, or put a new label there.  */
  1807.  
  1808. rtx
  1809. get_label_before (insn)
  1810.      rtx insn;
  1811. {
  1812.   rtx label;
  1813.  
  1814.   /* Find an existing label at this point
  1815.      or make a new one if there is none.  */
  1816.   label = PREV_INSN (insn);
  1817.   while (label && GET_CODE (label) == NOTE)
  1818.     label = PREV_INSN (label);
  1819.  
  1820.   if (label == 0 || GET_CODE (label) != CODE_LABEL)
  1821.     {
  1822.       label = gen_label_rtx ();
  1823.       emit_label_after (label, PREV_INSN (insn));
  1824.       LABEL_NUSES (label) = 0;
  1825.     }
  1826.   return label;
  1827. }
  1828.  
  1829. /* Return the label after INSN, or put a new label there.  */
  1830.  
  1831. rtx
  1832. get_label_after (insn)
  1833.      rtx insn;
  1834. {
  1835.   rtx label;
  1836.  
  1837.   /* Find an existing label at this point
  1838.      or make a new one if there is none.  */
  1839.   label = NEXT_INSN (insn);
  1840.   while (label && GET_CODE (label) == NOTE)
  1841.     label = NEXT_INSN (label);
  1842.  
  1843.   if (label == 0 || GET_CODE (label) != CODE_LABEL)
  1844.     {
  1845.       label = gen_label_rtx ();
  1846.       emit_label_after (label, insn);
  1847.       LABEL_NUSES (label) = 0;
  1848.     }
  1849.   return label;
  1850. }
  1851.  
  1852. /* Return 1 if INSN is a jump that jumps to right after TARGET
  1853.    only on the condition that TARGET itself would drop through.
  1854.    Assumes that TARGET is a conditional jump.  */
  1855.  
  1856. static int
  1857. jump_back_p (insn, target)
  1858.      rtx insn, target;
  1859. {
  1860.   rtx cinsn, ctarget;
  1861.   enum rtx_code codei, codet;
  1862.  
  1863.   if (simplejump_p (insn) || ! condjump_p (insn)
  1864.       || simplejump_p (target)
  1865.       || target != prev_real_insn (JUMP_LABEL (insn)))
  1866.     return 0;
  1867.  
  1868.   cinsn = XEXP (SET_SRC (PATTERN (insn)), 0);
  1869.   ctarget = XEXP (SET_SRC (PATTERN (target)), 0);
  1870.  
  1871.   codei = GET_CODE (cinsn);
  1872.   codet = GET_CODE (ctarget);
  1873.  
  1874.   if (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx)
  1875.     {
  1876.       if (! can_reverse_comparison_p (cinsn, insn))
  1877.     return 0;
  1878.       codei = reverse_condition (codei);
  1879.     }
  1880.  
  1881.   if (XEXP (SET_SRC (PATTERN (target)), 2) == pc_rtx)
  1882.     {
  1883.       if (! can_reverse_comparison_p (ctarget, target))
  1884.     return 0;
  1885.       codet = reverse_condition (codet);
  1886.     }
  1887.  
  1888.   return (codei == codet
  1889.       && rtx_renumbered_equal_p (XEXP (cinsn, 0), XEXP (ctarget, 0))
  1890.       && rtx_renumbered_equal_p (XEXP (cinsn, 1), XEXP (ctarget, 1)));
  1891. }
  1892.  
  1893. /* Given a comparison, COMPARISON, inside a conditional jump insn, INSN,
  1894.    return non-zero if it is safe to reverse this comparison.  It is if our
  1895.    floating-point is not IEEE, if this is an NE or EQ comparison, or if
  1896.    this is known to be an integer comparison.  */
  1897.  
  1898. int
  1899. can_reverse_comparison_p (comparison, insn)
  1900.      rtx comparison;
  1901.      rtx insn;
  1902. {
  1903.   rtx arg0;
  1904.  
  1905.   /* If this is not actually a comparison, we can't reverse it.  */
  1906.   if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
  1907.     return 0;
  1908.  
  1909.   if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  1910.       /* If this is an NE comparison, it is safe to reverse it to an EQ
  1911.      comparison and vice versa, even for floating point.  If no operands
  1912.      are NaNs, the reversal is valid.  If some operand is a NaN, EQ is
  1913.      always false and NE is always true, so the reversal is also valid.  */
  1914.       || GET_CODE (comparison) == NE
  1915.       || GET_CODE (comparison) == EQ)
  1916.     return 1;
  1917.  
  1918.   arg0 = XEXP (comparison, 0);
  1919.  
  1920.   /* Make sure ARG0 is one of the actual objects being compared.  If we
  1921.      can't do this, we can't be sure the comparison can be reversed. 
  1922.  
  1923.      Handle cc0 and a MODE_CC register.  */
  1924.   if ((GET_CODE (arg0) == REG && GET_MODE_CLASS (GET_MODE (arg0)) == MODE_CC)
  1925. #ifdef HAVE_cc0
  1926.       || arg0 == cc0_rtx
  1927. #endif
  1928.       )
  1929.     {
  1930.       rtx prev = prev_nonnote_insn (insn);
  1931.       rtx set = single_set (prev);
  1932.  
  1933.       if (set == 0 || SET_DEST (set) != arg0)
  1934.     return 0;
  1935.  
  1936.       arg0 = SET_SRC (set);
  1937.  
  1938.       if (GET_CODE (arg0) == COMPARE)
  1939.     arg0 = XEXP (arg0, 0);
  1940.     }
  1941.  
  1942.   /* We can reverse this if ARG0 is a CONST_INT or if its mode is
  1943.      not VOIDmode and neither a MODE_CC nor MODE_FLOAT type.  */
  1944.   return (GET_CODE (arg0) == CONST_INT
  1945.       || (GET_MODE (arg0) != VOIDmode
  1946.           && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_CC
  1947.           && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_FLOAT));
  1948. }
  1949.  
  1950. /* Given an rtx-code for a comparison, return the code
  1951.    for the negated comparison.
  1952.    WATCH OUT!  reverse_condition is not safe to use on a jump
  1953.    that might be acting on the results of an IEEE floating point comparison,
  1954.    because of the special treatment of non-signaling nans in comparisons.  
  1955.    Use can_reverse_comparison_p to be sure.  */
  1956.  
  1957. enum rtx_code
  1958. reverse_condition (code)
  1959.      enum rtx_code code;
  1960. {
  1961.   switch (code)
  1962.     {
  1963.     case EQ:
  1964.       return NE;
  1965.  
  1966.     case NE:
  1967.       return EQ;
  1968.  
  1969.     case GT:
  1970.       return LE;
  1971.  
  1972.     case GE:
  1973.       return LT;
  1974.  
  1975.     case LT:
  1976.       return GE;
  1977.  
  1978.     case LE:
  1979.       return GT;
  1980.  
  1981.     case GTU:
  1982.       return LEU;
  1983.  
  1984.     case GEU:
  1985.       return LTU;
  1986.  
  1987.     case LTU:
  1988.       return GEU;
  1989.  
  1990.     case LEU:
  1991.       return GTU;
  1992.  
  1993.     default:
  1994.       abort ();
  1995.       return UNKNOWN;
  1996.     }
  1997. }
  1998.  
  1999. /* Similar, but return the code when two operands of a comparison are swapped.
  2000.    This IS safe for IEEE floating-point.  */
  2001.  
  2002. enum rtx_code
  2003. swap_condition (code)
  2004.      enum rtx_code code;
  2005. {
  2006.   switch (code)
  2007.     {
  2008.     case EQ:
  2009.     case NE:
  2010.       return code;
  2011.  
  2012.     case GT:
  2013.       return LT;
  2014.  
  2015.     case GE:
  2016.       return LE;
  2017.  
  2018.     case LT:
  2019.       return GT;
  2020.  
  2021.     case LE:
  2022.       return GE;
  2023.  
  2024.     case GTU:
  2025.       return LTU;
  2026.  
  2027.     case GEU:
  2028.       return LEU;
  2029.  
  2030.     case LTU:
  2031.       return GTU;
  2032.  
  2033.     case LEU:
  2034.       return GEU;
  2035.  
  2036.     default:
  2037.       abort ();
  2038.       return UNKNOWN;
  2039.     }
  2040. }
  2041.  
  2042. /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
  2043.    truth of CODE1 implies the truth of CODE2.  */
  2044.  
  2045. int
  2046. comparison_dominates_p (code1, code2)
  2047.      enum rtx_code code1, code2;
  2048. {
  2049.   if (code1 == code2)
  2050.     return 1;
  2051.  
  2052.   switch (code1)
  2053.     {
  2054.     case EQ:
  2055.       if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU)
  2056.     return 1;
  2057.       break;
  2058.  
  2059.     case LT:
  2060.       if (code2 == LE)
  2061.     return 1;
  2062.       break;
  2063.  
  2064.     case GT:
  2065.       if (code2 == GE)
  2066.     return 1;
  2067.       break;
  2068.  
  2069.     case LTU:
  2070.       if (code2 == LEU)
  2071.     return 1;
  2072.       break;
  2073.  
  2074.     case GTU:
  2075.       if (code2 == GEU)
  2076.     return 1;
  2077.       break;
  2078.     }
  2079.  
  2080.   return 0;
  2081. }
  2082.  
  2083. /* Return 1 if INSN is an unconditional jump and nothing else.  */
  2084.  
  2085. int
  2086. simplejump_p (insn)
  2087.      rtx insn;
  2088. {
  2089.   return (GET_CODE (insn) == JUMP_INSN
  2090.       && GET_CODE (PATTERN (insn)) == SET
  2091.       && GET_CODE (SET_DEST (PATTERN (insn))) == PC
  2092.       && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
  2093. }
  2094.  
  2095. /* Return nonzero if INSN is a (possibly) conditional jump
  2096.    and nothing more.  */
  2097.  
  2098. int
  2099. condjump_p (insn)
  2100.      rtx insn;
  2101. {
  2102.   register rtx x = PATTERN (insn);
  2103.   if (GET_CODE (x) != SET)
  2104.     return 0;
  2105.   if (GET_CODE (SET_DEST (x)) != PC)
  2106.     return 0;
  2107.   if (GET_CODE (SET_SRC (x)) == LABEL_REF)
  2108.     return 1;
  2109.   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
  2110.     return 0;
  2111.   if (XEXP (SET_SRC (x), 2) == pc_rtx
  2112.       && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
  2113.       || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
  2114.     return 1;
  2115.   if (XEXP (SET_SRC (x), 1) == pc_rtx
  2116.       && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
  2117.       || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
  2118.     return 1;
  2119.   return 0;
  2120. }
  2121.  
  2122. /* Return 1 if X is an RTX that does nothing but set the condition codes
  2123.    and CLOBBER or USE registers.
  2124.    Return -1 if X does explicitly set the condition codes,
  2125.    but also does other things.  */
  2126.  
  2127. int
  2128. sets_cc0_p (x)
  2129.      rtx x;
  2130. {
  2131. #ifdef HAVE_cc0
  2132.   if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
  2133.     return 1;
  2134.   if (GET_CODE (x) == PARALLEL)
  2135.     {
  2136.       int i;
  2137.       int sets_cc0 = 0;
  2138.       int other_things = 0;
  2139.       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
  2140.     {
  2141.       if (GET_CODE (XVECEXP (x, 0, i)) == SET
  2142.           && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
  2143.         sets_cc0 = 1;
  2144.       else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
  2145.         other_things = 1;
  2146.     }
  2147.       return ! sets_cc0 ? 0 : other_things ? -1 : 1;
  2148.     }
  2149.   return 0;
  2150. #else
  2151.   abort ();
  2152. #endif
  2153. }
  2154.  
  2155. /* Follow any unconditional jump at LABEL;
  2156.    return the ultimate label reached by any such chain of jumps.
  2157.    If LABEL is not followed by a jump, return LABEL.
  2158.    If AFTER_RELOAD is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
  2159.    a USE or CLOBBER.  */
  2160.  
  2161. rtx
  2162. follow_jumps (label, after_reload)
  2163.      rtx label;
  2164.      int after_reload;
  2165. {
  2166.   register rtx insn;
  2167.   register rtx next;
  2168.   register rtx value = label;
  2169.   register int depth;
  2170.  
  2171.   for (depth = 0;
  2172.        (depth < 10
  2173.     && (insn = next_active_insn (value)) != 0
  2174.     && GET_CODE (insn) == JUMP_INSN
  2175.     && (JUMP_LABEL (insn) != 0 || GET_CODE (PATTERN (insn)) == RETURN)
  2176.     && (next = NEXT_INSN (insn))
  2177.     && GET_CODE (next) == BARRIER);
  2178.        depth++)
  2179.     {
  2180.       /* Don't chain through the insn that jumps into a loop
  2181.      from outside the loop,
  2182.      since that would create multiple loop entry jumps
  2183.      and prevent loop optimization.  */
  2184.       rtx tem;
  2185.       if (!after_reload)
  2186.     for (tem = value; tem != insn; tem = NEXT_INSN (tem))
  2187.       if (GET_CODE (tem) == NOTE
  2188.           && NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG)
  2189.         return value;
  2190.  
  2191.       /* If we have found a cycle, make the insn jump to itself.  */
  2192.       if (JUMP_LABEL (insn) == label)
  2193.     break;
  2194.       value = JUMP_LABEL (insn);
  2195.     }
  2196.   return value;
  2197. }
  2198.  
  2199. /* Assuming that field IDX of X is a vector of label_refs,
  2200.    replace each of them by the ultimate label reached by it.
  2201.    Return nonzero if a change is made.
  2202.    If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG.  */
  2203.  
  2204. static int
  2205. tension_vector_labels (x, idx, after_reload)
  2206.      register rtx x;
  2207.      register int idx;
  2208.      int after_reload;
  2209. {
  2210.   int changed = 0;
  2211.   register int i;
  2212.   for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
  2213.     {
  2214.       register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
  2215.       register rtx nlabel = follow_jumps (olabel, after_reload);
  2216.       if (nlabel && nlabel != olabel)
  2217.     {
  2218.       XEXP (XVECEXP (x, idx, i), 0) = nlabel;
  2219.       ++LABEL_NUSES (nlabel);
  2220.       if (--LABEL_NUSES (olabel) == 0)
  2221.         delete_insn (olabel);
  2222.       changed = 1;
  2223.     }
  2224.     }
  2225.   return changed;
  2226. }
  2227.  
  2228. /* Find all CODE_LABELs referred to in X, and increment their use counts.
  2229.    If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
  2230.    in INSN, then store one of them in JUMP_LABEL (INSN).
  2231.    If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
  2232.    referenced in INSN, add a REG_LABEL note containing that label to INSN.
  2233.    Also, when there are consecutive labels, canonicalize on the last of them.
  2234.  
  2235.    Note that two labels separated by a loop-beginning note
  2236.    must be kept distinct if we have not yet done loop-optimization,
  2237.    because the gap between them is where loop-optimize
  2238.    will want to move invariant code to.  CROSS_JUMP tells us
  2239.    that loop-optimization is done with.
  2240.  
  2241.    Once reload has completed (CROSS_JUMP non-zero), we need not consider
  2242.    two labels distinct if they are separated by only USE or CLOBBER insns.  */
  2243.  
  2244. static void
  2245. mark_jump_label (x, insn, cross_jump)
  2246.      register rtx x;
  2247.      rtx insn;
  2248.      int cross_jump;
  2249. {
  2250.   register RTX_CODE code = GET_CODE (x);
  2251.   register int i;
  2252.   register char *fmt;
  2253.  
  2254.   switch (code)
  2255.     {
  2256.     case PC:
  2257.     case CC0:
  2258.     case REG:
  2259.     case SUBREG:
  2260.     case CONST_INT:
  2261.     case SYMBOL_REF:
  2262.     case CONST_DOUBLE:
  2263.     case CLOBBER:
  2264.     case CALL:
  2265.       return;
  2266.  
  2267.     case LABEL_REF:
  2268.       {
  2269.     register rtx label = XEXP (x, 0);
  2270.     register rtx next;
  2271.     if (GET_CODE (label) != CODE_LABEL)
  2272.       abort ();
  2273.     /* If there are other labels following this one,
  2274.        replace it with the last of the consecutive labels.  */
  2275.     for (next = NEXT_INSN (label); next; next = NEXT_INSN (next))
  2276.       {
  2277.         if (GET_CODE (next) == CODE_LABEL)
  2278.           label = next;
  2279.         else if (cross_jump && GET_CODE (next) == INSN
  2280.              && (GET_CODE (PATTERN (next)) == USE
  2281.              || GET_CODE (PATTERN (next)) == CLOBBER))
  2282.           continue;
  2283.         else if (GET_CODE (next) != NOTE)
  2284.           break;
  2285.         else if (! cross_jump
  2286.              && (NOTE_LINE_NUMBER (next) == NOTE_INSN_LOOP_BEG
  2287.              || NOTE_LINE_NUMBER (next) == NOTE_INSN_FUNCTION_END))
  2288.           break;
  2289.       }
  2290.     XEXP (x, 0) = label;
  2291.     ++LABEL_NUSES (label);
  2292.     if (insn)
  2293.       {
  2294.         if (GET_CODE (insn) == JUMP_INSN)
  2295.           JUMP_LABEL (insn) = label;
  2296.         else if (! find_reg_note (insn, REG_LABEL, 0))
  2297.           {
  2298.         rtx next = next_real_insn (label);
  2299.         /* Don't record labels that refer to dispatch tables.
  2300.            This is not necessary, since the tablejump
  2301.            references the same label.
  2302.            And if we did record them, flow.c would make worse code.  */
  2303.         if (! (GET_CODE (next) == JUMP_INSN
  2304.                && (GET_CODE (PATTERN (next)) == ADDR_VEC
  2305.                || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
  2306.           REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, label,
  2307.                           REG_NOTES (insn));
  2308.           }
  2309.       }
  2310.     return;
  2311.       }
  2312.  
  2313.   /* Do walk the labels in a vector, but not the first operand of an
  2314.      ADDR_DIFF_VEC.  Don't set the JUMP_LABEL of a vector.  */
  2315.     case ADDR_VEC:
  2316.     case ADDR_DIFF_VEC:
  2317.       {
  2318.     int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
  2319.  
  2320.     for (i = 0; i < XVECLEN (x, eltnum); i++)
  2321.       mark_jump_label (XVECEXP (x, eltnum, i), 0, cross_jump);
  2322.     return;
  2323.       }
  2324.     }
  2325.  
  2326.   fmt = GET_RTX_FORMAT (code);
  2327.   for (i = GET_RTX_LENGTH (code); i >= 0; i--)
  2328.     {
  2329.       if (fmt[i] == 'e')
  2330.     mark_jump_label (XEXP (x, i), insn, cross_jump);
  2331.       else if (fmt[i] == 'E')
  2332.     {
  2333.       register int j;
  2334.       for (j = 0; j < XVECLEN (x, i); j++)
  2335.         mark_jump_label (XVECEXP (x, i, j), insn, cross_jump);
  2336.     }
  2337.     }
  2338. }
  2339.  
  2340. /* If all INSN does is set the pc, delete it,
  2341.    and delete the insn that set the condition codes for it
  2342.    if that's what the previous thing was.  */
  2343.  
  2344. void
  2345. delete_jump (insn)
  2346.      rtx insn;
  2347. {
  2348.   register rtx x = PATTERN (insn);
  2349.   register rtx prev;
  2350.  
  2351.   if (GET_CODE (x) == SET
  2352.       && GET_CODE (SET_DEST (x)) == PC)
  2353.     {
  2354.       prev = prev_nonnote_insn (insn);
  2355. #ifdef HAVE_cc0
  2356.       /* We assume that at this stage
  2357.      CC's are always set explicitly
  2358.      and always immediately before the jump that
  2359.      will use them.  So if the previous insn
  2360.      exists to set the CC's, delete it
  2361.      (unless it performs auto-increments, etc.).  */
  2362.       if (prev && GET_CODE (prev) == INSN
  2363.       && sets_cc0_p (PATTERN (prev)))
  2364.     {
  2365.       if (sets_cc0_p (PATTERN (prev)) > 0
  2366.           && !FIND_REG_INC_NOTE (prev, 0))
  2367.         delete_insn (prev);
  2368.       else
  2369.         /* Otherwise, show that cc0 won't be used.  */
  2370.         REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_UNUSED,
  2371.                     cc0_rtx, REG_NOTES (prev));
  2372.     }
  2373. #else
  2374.       {
  2375.     rtx note;
  2376.  
  2377.     /* If we are running before flow.c, we need do nothing since flow.c
  2378.        will delete the set of the condition code if it is dead.  We also
  2379.        can't know if the register being used as the condition code is
  2380.        dead or not at this point.
  2381.  
  2382.        Otherwise, look at all our REG_DEAD notes.  If a previous insn
  2383.        does nothing other than set a register that dies in this jump,
  2384.        we can delete the insn.  */
  2385.  
  2386.     for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
  2387.       {
  2388.         rtx our_prev;
  2389.  
  2390.         if (REG_NOTE_KIND (note) != REG_DEAD)
  2391.           continue;
  2392.  
  2393.         for (our_prev = prev_nonnote_insn (insn);
  2394.          our_prev && GET_CODE (our_prev) == INSN;
  2395.          our_prev = prev_nonnote_insn (our_prev))
  2396.           {
  2397.         /* If we reach a SEQUENCE, it is too complex to try to
  2398.            do anything with it, so give up.  */
  2399.         if (GET_CODE (PATTERN (our_prev)) == SEQUENCE)
  2400.           break;
  2401.  
  2402.         if (GET_CODE (PATTERN (our_prev)) == USE
  2403.             && GET_CODE (XEXP (PATTERN (our_prev), 0)) == INSN)
  2404.           /* reorg creates USEs that look like this.  We leave them
  2405.              alone because reorg needs them for its own purposes.  */
  2406.           break;
  2407.  
  2408.         if (reg_set_p (XEXP (note, 0), PATTERN (our_prev)))
  2409.           {
  2410.             if (FIND_REG_INC_NOTE (our_prev, 0))
  2411.               break;
  2412.  
  2413.             if (GET_CODE (PATTERN (our_prev)) == PARALLEL)
  2414.               {
  2415.             /* If we find a SET of something else, we can't
  2416.                delete the insn.  */
  2417.  
  2418.             int i;
  2419.  
  2420.             for (i = 0; i < XVECLEN (PATTERN (our_prev), 0); i++)
  2421.               {
  2422.                 rtx part = XVECEXP (PATTERN (our_prev), 0, i);
  2423.  
  2424.                 if (GET_CODE (part) == SET
  2425.                 && SET_DEST (part) != XEXP (note, 0))
  2426.                   break;
  2427.               }
  2428.  
  2429.             if (i == XVECLEN (PATTERN (our_prev), 0))
  2430.               delete_insn (our_prev);
  2431.               }
  2432.             else if (GET_CODE (PATTERN (our_prev)) == SET
  2433.                  && SET_DEST (PATTERN (our_prev)) == XEXP (note, 0))
  2434.               delete_insn (our_prev);
  2435.  
  2436.             break;
  2437.           }
  2438.  
  2439.         /* If OUR_PREV references the register that dies here,
  2440.            it is an additional use.  Hence any prior SET isn't
  2441.            dead.  */
  2442.         if (reg_overlap_mentioned_p (XEXP (note, 0),
  2443.                          PATTERN (our_prev)))
  2444.           break;
  2445.           }
  2446.       }
  2447.       }
  2448. #endif
  2449.       /* Now delete the jump insn itself.  */
  2450.       delete_insn (insn);
  2451.     }
  2452. }
  2453.  
  2454. /* Delete insn INSN from the chain of insns and update label ref counts.
  2455.    May delete some following insns as a consequence; may even delete
  2456.    a label elsewhere and insns that follow it.
  2457.  
  2458.    Returns the first insn after INSN that was not deleted.  */
  2459.  
  2460. rtx
  2461. delete_insn (insn)
  2462.      register rtx insn;
  2463. {
  2464.   register rtx next = NEXT_INSN (insn);
  2465.   register rtx prev = PREV_INSN (insn);
  2466.  
  2467.   while (next && INSN_DELETED_P (next))
  2468.     next = NEXT_INSN (next);
  2469.  
  2470.   /* This insn is already deleted => return first following nondeleted.  */
  2471.   if (INSN_DELETED_P (insn))
  2472.     return next;
  2473.  
  2474.   /* Mark this insn as deleted.  */
  2475.  
  2476.   INSN_DELETED_P (insn) = 1;
  2477.  
  2478.   /* If this is an unconditional jump, delete it from the jump chain.  */
  2479.   if (simplejump_p (insn))
  2480.     delete_from_jump_chain (insn);
  2481.  
  2482.   /* If instruction is followed by a barrier,
  2483.      delete the barrier too.  */
  2484.  
  2485.   if (next != 0 && GET_CODE (next) == BARRIER)
  2486.     {
  2487.       INSN_DELETED_P (next) = 1;
  2488.       next = NEXT_INSN (next);
  2489.     }
  2490.  
  2491.   /* Patch out INSN (and the barrier if any) */
  2492.  
  2493.   if (optimize)
  2494.     {
  2495.       if (prev)
  2496.     {
  2497.       NEXT_INSN (prev) = next;
  2498.       if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
  2499.         NEXT_INSN (XVECEXP (PATTERN (prev), 0,
  2500.                 XVECLEN (PATTERN (prev), 0) - 1)) = next;
  2501.     }
  2502.  
  2503.       if (next)
  2504.     {
  2505.       PREV_INSN (next) = prev;
  2506.       if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
  2507.         PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
  2508.     }
  2509.  
  2510.       if (prev && NEXT_INSN (prev) == 0)
  2511.     set_last_insn (prev);
  2512.     }
  2513.  
  2514.   /* If deleting a jump, decrement the count of the label,
  2515.      and delete the label if it is now unused.  */
  2516.  
  2517.   if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
  2518.     if (--LABEL_NUSES (JUMP_LABEL (insn)) == 0)
  2519.       {
  2520.     /* This can delete NEXT or PREV,
  2521.        either directly if NEXT is JUMP_LABEL (INSN),
  2522.        or indirectly through more levels of jumps.  */
  2523.     delete_insn (JUMP_LABEL (insn));
  2524.     /* I feel a little doubtful about this loop,
  2525.        but I see no clean and sure alternative way
  2526.        to find the first insn after INSN that is not now deleted.
  2527.        I hope this works.  */
  2528.     while (next && INSN_DELETED_P (next))
  2529.       next = NEXT_INSN (next);
  2530.     return next;
  2531.       }
  2532.  
  2533.   while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
  2534.     prev = PREV_INSN (prev);
  2535.  
  2536.   /* If INSN was a label and a dispatch table follows it,
  2537.      delete the dispatch table.  The tablejump must have gone already.
  2538.      It isn't useful to fall through into a table.  */
  2539.  
  2540.   if (GET_CODE (insn) == CODE_LABEL
  2541.       && NEXT_INSN (insn) != 0
  2542.       && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
  2543.       && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
  2544.       || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
  2545.     next = delete_insn (NEXT_INSN (insn));
  2546.  
  2547.   /* If INSN was a label, delete insns following it if now unreachable.  */
  2548.  
  2549.   if (GET_CODE (insn) == CODE_LABEL && prev
  2550.       && GET_CODE (prev) == BARRIER)
  2551.     {
  2552.       register RTX_CODE code;
  2553.       while (next != 0
  2554.          && ((code = GET_CODE (next)) == INSN
  2555.          || code == JUMP_INSN || code == CALL_INSN
  2556.          || code == NOTE))
  2557.     {
  2558.       if (code == NOTE
  2559.           && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
  2560.         next = NEXT_INSN (next);
  2561.       else
  2562.         /* Note: if this deletes a jump, it can cause more
  2563.            deletion of unreachable code, after a different label.
  2564.            As long as the value from this recursive call is correct,
  2565.            this invocation functions correctly.  */
  2566.         next = delete_insn (next);
  2567.     }
  2568.     }
  2569.  
  2570.   return next;
  2571. }
  2572.  
  2573. /* Advance from INSN till reaching something not deleted
  2574.    then return that.  May return INSN itself.  */
  2575.  
  2576. rtx
  2577. next_nondeleted_insn (insn)
  2578.      rtx insn;
  2579. {
  2580.   while (INSN_DELETED_P (insn))
  2581.     insn = NEXT_INSN (insn);
  2582.   return insn;
  2583. }
  2584.  
  2585. /* Delete a range of insns from FROM to TO, inclusive.
  2586.    This is for the sake of peephole optimization, so assume
  2587.    that whatever these insns do will still be done by a new
  2588.    peephole insn that will replace them.  */
  2589.  
  2590. void
  2591. delete_for_peephole (from, to)
  2592.      register rtx from, to;
  2593. {
  2594.   register rtx insn = from;
  2595.  
  2596.   while (1)
  2597.     {
  2598.       register rtx next = NEXT_INSN (insn);
  2599.       register rtx prev = PREV_INSN (insn);
  2600.  
  2601.       if (GET_CODE (insn) != NOTE)
  2602.     {
  2603.       INSN_DELETED_P (insn) = 1;
  2604.  
  2605.       /* Patch this insn out of the chain.  */
  2606.       /* We don't do this all at once, because we
  2607.          must preserve all NOTEs.  */
  2608.       if (prev)
  2609.         NEXT_INSN (prev) = next;
  2610.  
  2611.       if (next)
  2612.         PREV_INSN (next) = prev;
  2613.     }
  2614.  
  2615.       if (insn == to)
  2616.     break;
  2617.       insn = next;
  2618.     }
  2619.  
  2620.   /* Note that if TO is an unconditional jump
  2621.      we *do not* delete the BARRIER that follows,
  2622.      since the peephole that replaces this sequence
  2623.      is also an unconditional jump in that case.  */
  2624. }
  2625.  
  2626. /* Invert the condition of the jump JUMP, and make it jump
  2627.    to label NLABEL instead of where it jumps now.  */
  2628.  
  2629. int
  2630. invert_jump (jump, nlabel)
  2631.      rtx jump, nlabel;
  2632. {
  2633.   register rtx olabel = JUMP_LABEL (jump);
  2634.  
  2635.   /* We have to either invert the condition and change the label or
  2636.      do neither.  Either operation could fail.  We first try to invert
  2637.      the jump. If that succeeds, we try changing the label.  If that fails,
  2638.      we invert the jump back to what it was.  */
  2639.  
  2640.   if (! invert_exp (PATTERN (jump), jump))
  2641.     return 0;
  2642.  
  2643.   if (redirect_jump (jump, nlabel))
  2644.     return 1;
  2645.  
  2646.   if (! invert_exp (PATTERN (jump), jump))
  2647.     /* This should just be putting it back the way it was.  */
  2648.     abort ();
  2649.  
  2650.   return  0;
  2651. }
  2652.  
  2653. /* Invert the jump condition of rtx X contained in jump insn, INSN. 
  2654.  
  2655.    Return 1 if we can do so, 0 if we cannot find a way to do so that
  2656.    matches a pattern.  */
  2657.  
  2658. static int
  2659. invert_exp (x, insn)
  2660.      rtx x;
  2661.      rtx insn;
  2662. {
  2663.   register RTX_CODE code;
  2664.   register int i;
  2665.   register char *fmt;
  2666.  
  2667.   code = GET_CODE (x);
  2668.  
  2669.   if (code == IF_THEN_ELSE)
  2670.     {
  2671.       register rtx comp = XEXP (x, 0);
  2672.       register rtx tem;
  2673.  
  2674.       /* We can do this in two ways:  The preferable way, which can only
  2675.      be done if this is not an integer comparison, is to reverse
  2676.      the comparison code.  Otherwise, swap the THEN-part and ELSE_part
  2677.      of the IF_THEN_ELSE.  If we can't do either, fail.  */
  2678.  
  2679.       if (can_reverse_comparison_p (comp, insn)
  2680.       && validate_change (insn, &XEXP (x, 0),
  2681.                   gen_rtx (reverse_condition (GET_CODE (comp)),
  2682.                        GET_MODE (comp), XEXP (comp, 0),
  2683.                        XEXP (comp, 1)), 0))
  2684.     return 1;
  2685.                        
  2686.       tem = XEXP (x, 1);
  2687.       validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
  2688.       validate_change (insn, &XEXP (x, 2), tem, 1);
  2689.       return apply_change_group ();
  2690.     }
  2691.  
  2692.   fmt = GET_RTX_FORMAT (code);
  2693.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2694.     {
  2695.       if (fmt[i] == 'e')
  2696.     if (! invert_exp (XEXP (x, i), insn))
  2697.       return 0;
  2698.       if (fmt[i] == 'E')
  2699.     {
  2700.       register int j;
  2701.       for (j = 0; j < XVECLEN (x, i); j++)
  2702.         if (!invert_exp (XVECEXP (x, i, j), insn))
  2703.           return 0;
  2704.     }
  2705.     }
  2706.  
  2707.   return 1;
  2708. }
  2709.  
  2710. /* Make jump JUMP jump to label NLABEL instead of where it jumps now.
  2711.    If the old jump target label is unused as a result,
  2712.    it and the code following it may be deleted.
  2713.  
  2714.    If NLABEL is zero, we are to turn the jump into a (possibly conditional)
  2715.    RETURN insn.
  2716.  
  2717.    The return value will be 1 if the change was made, 0 if it wasn't (this
  2718.    can only occur for NLABEL == 0).  */
  2719.  
  2720. int
  2721. redirect_jump (jump, nlabel)
  2722.      rtx jump, nlabel;
  2723. {
  2724.   register rtx olabel = JUMP_LABEL (jump);
  2725.  
  2726.   if (nlabel == olabel)
  2727.     return 1;
  2728.  
  2729.   if (! redirect_exp (&PATTERN (jump), olabel, nlabel, jump))
  2730.     return 0;
  2731.  
  2732.   /* If this is an unconditional branch, delete it from the jump_chain of
  2733.      OLABEL and add it to the jump_chain of NLABEL (assuming both labels
  2734.      have UID's in range and JUMP_CHAIN is valid).  */
  2735.   if (jump_chain && (simplejump_p (jump)
  2736.              || GET_CODE (PATTERN (jump)) == RETURN))
  2737.     {
  2738.       int label_index = nlabel ? INSN_UID (nlabel) : 0;
  2739.  
  2740.       delete_from_jump_chain (jump);
  2741.       if (label_index < max_jump_chain)
  2742.     {
  2743.       jump_chain[INSN_UID (jump)] = jump_chain[label_index];
  2744.       jump_chain[label_index] = jump;
  2745.     }
  2746.     }
  2747.  
  2748.   JUMP_LABEL (jump) = nlabel;
  2749.   if (nlabel)
  2750.     ++LABEL_NUSES (nlabel);
  2751.  
  2752.   if (olabel && --LABEL_NUSES (olabel) == 0)
  2753.     delete_insn (olabel);
  2754.  
  2755.   return 1;
  2756. }
  2757.  
  2758. /* Delete the instruction JUMP from any jump chain it might be on.  */
  2759.  
  2760. static void
  2761. delete_from_jump_chain (jump)
  2762.      rtx jump;
  2763. {
  2764.   int index;
  2765.   rtx olabel = JUMP_LABEL (jump);
  2766.  
  2767.   /* Handle unconditional jumps.  */
  2768.   if (jump_chain && olabel != 0
  2769.       && INSN_UID (olabel) < max_jump_chain
  2770.       && simplejump_p (jump))
  2771.     index = INSN_UID (olabel);
  2772.   /* Handle return insns.  */
  2773.   else if (jump_chain && GET_CODE (PATTERN (jump)) == RETURN)
  2774.     index = 0;
  2775.   else return;
  2776.  
  2777.   if (jump_chain[index] == jump)
  2778.     jump_chain[index] = jump_chain[INSN_UID (jump)];
  2779.   else
  2780.     {
  2781.       rtx insn;
  2782.  
  2783.       for (insn = jump_chain[index];
  2784.        insn != 0;
  2785.        insn = jump_chain[INSN_UID (insn)])
  2786.     if (jump_chain[INSN_UID (insn)] == jump)
  2787.       {
  2788.         jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (jump)];
  2789.         break;
  2790.       }
  2791.     }
  2792. }
  2793.  
  2794. /* If NLABEL is nonzero, throughout the rtx at LOC,
  2795.    alter (LABEL_REF OLABEL) to (LABEL_REF NLABEL).  If OLABEL is
  2796.    zero, alter (RETURN) to (LABEL_REF NLABEL).
  2797.  
  2798.    If NLABEL is zero, alter (LABEL_REF OLABEL) to (RETURN) and check
  2799.    validity with validate_change.  Convert (set (pc) (label_ref olabel))
  2800.    to (return).
  2801.  
  2802.    Return 0 if we found a change we would like to make but it is invalid.
  2803.    Otherwise, return 1.  */
  2804.  
  2805. static int
  2806. redirect_exp (loc, olabel, nlabel, insn)
  2807.      rtx *loc;
  2808.      rtx olabel, nlabel;
  2809.      rtx insn;
  2810. {
  2811.   register rtx x = *loc;
  2812.   register RTX_CODE code = GET_CODE (x);
  2813.   register int i;
  2814.   register char *fmt;
  2815.  
  2816.   if (code == LABEL_REF)
  2817.     {
  2818.       if (XEXP (x, 0) == olabel)
  2819.     {
  2820.       if (nlabel)
  2821.         XEXP (x, 0) = nlabel;
  2822.       else
  2823.         return validate_change (insn, loc, gen_rtx (RETURN, VOIDmode), 0);
  2824.       return 1;
  2825.     }
  2826.     }
  2827.   else if (code == RETURN && olabel == 0)
  2828.     {
  2829.       x = gen_rtx (LABEL_REF, VOIDmode, nlabel);
  2830.       if (loc == &PATTERN (insn))
  2831.     x = gen_rtx (SET, VOIDmode, pc_rtx, x);
  2832.       return validate_change (insn, loc, x, 0);
  2833.     }
  2834.  
  2835.   if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
  2836.       && GET_CODE (SET_SRC (x)) == LABEL_REF
  2837.       && XEXP (SET_SRC (x), 0) == olabel)
  2838.     return validate_change (insn, loc, gen_rtx (RETURN, VOIDmode), 0);
  2839.  
  2840.   fmt = GET_RTX_FORMAT (code);
  2841.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2842.     {
  2843.       if (fmt[i] == 'e')
  2844.     if (! redirect_exp (&XEXP (x, i), olabel, nlabel, insn))
  2845.       return 0;
  2846.       if (fmt[i] == 'E')
  2847.     {
  2848.       register int j;
  2849.       for (j = 0; j < XVECLEN (x, i); j++)
  2850.         if (! redirect_exp (&XVECEXP (x, i, j), olabel, nlabel, insn))
  2851.           return 0;
  2852.     }
  2853.     }
  2854.  
  2855.   return 1;
  2856. }
  2857.  
  2858. /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
  2859.  
  2860.    If the old jump target label (before the dispatch table) becomes unused,
  2861.    it and the dispatch table may be deleted.  In that case, find the insn
  2862.    before the jump references that label and delete it and logical sucessors
  2863.    too.  */
  2864.  
  2865. void
  2866. redirect_tablejump (jump, nlabel)
  2867.      rtx jump, nlabel;
  2868. {
  2869.   register rtx olabel = JUMP_LABEL (jump);
  2870.  
  2871.   /* Add this jump to the jump_chain of NLABEL.  */
  2872.   if (jump_chain && INSN_UID (nlabel) < max_jump_chain
  2873.       && INSN_UID (jump) < max_jump_chain)
  2874.     {
  2875.       jump_chain[INSN_UID (jump)] = jump_chain[INSN_UID (nlabel)];
  2876.       jump_chain[INSN_UID (nlabel)] = jump;
  2877.     }
  2878.  
  2879.   PATTERN (jump) = gen_jump (nlabel);
  2880.   JUMP_LABEL (jump) = nlabel;
  2881.   ++LABEL_NUSES (nlabel);
  2882.   INSN_CODE (jump) = -1;
  2883.  
  2884.   if (--LABEL_NUSES (olabel) == 0)
  2885.     {
  2886.       delete_labelref_insn (jump, olabel, 0);
  2887.       delete_insn (olabel);
  2888.     }
  2889. }
  2890.  
  2891. /* Find the insn referencing LABEL that is a logical predecessor of INSN.
  2892.    If we found one, delete it and then delete this insn if DELETE_THIS is
  2893.    non-zero.  Return non-zero if INSN or a predecessor references LABEL.  */
  2894.  
  2895. static int
  2896. delete_labelref_insn (insn, label, delete_this)
  2897.      rtx insn, label;
  2898.      int delete_this;
  2899. {
  2900.   int deleted = 0;
  2901.   rtx link;
  2902.  
  2903.   if (GET_CODE (insn) != NOTE
  2904.       && reg_mentioned_p (label, PATTERN (insn)))
  2905.     {
  2906.       if (delete_this)
  2907.     {
  2908.       delete_insn (insn);
  2909.       deleted = 1;
  2910.     }
  2911.       else
  2912.     return 1;
  2913.     }
  2914.  
  2915.   for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
  2916.     if (delete_labelref_insn (XEXP (link, 0), label, 1))
  2917.       {
  2918.     if (delete_this)
  2919.       {
  2920.         delete_insn (insn);
  2921.         deleted = 1;
  2922.       }
  2923.     else
  2924.       return 1;
  2925.       }
  2926.  
  2927.   return deleted;
  2928. }
  2929.  
  2930. /* Like rtx_equal_p except that it considers two REGs as equal
  2931.    if they renumber to the same value.  */
  2932.  
  2933. int
  2934. rtx_renumbered_equal_p (x, y)
  2935.      rtx x, y;
  2936. {
  2937.   register int i;
  2938.   register RTX_CODE code = GET_CODE (x);
  2939.   register char *fmt;
  2940.       
  2941.   if (x == y)
  2942.     return 1;
  2943.   if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
  2944.       && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
  2945.                   && GET_CODE (SUBREG_REG (y)) == REG)))
  2946.     {
  2947.       register int j;
  2948.  
  2949.       if (GET_MODE (x) != GET_MODE (y))
  2950.     return 0;
  2951.  
  2952.       /* If we haven't done any renumbering, don't
  2953.      make any assumptions.  */
  2954.       if (reg_renumber == 0)
  2955.     return rtx_equal_p (x, y);
  2956.  
  2957.       if (code == SUBREG)
  2958.     {
  2959.       i = REGNO (SUBREG_REG (x));
  2960.       if (reg_renumber[i] >= 0)
  2961.         i = reg_renumber[i];
  2962.       i += SUBREG_WORD (x);
  2963.     }
  2964.       else
  2965.     {
  2966.       i = REGNO (x);
  2967.       if (reg_renumber[i] >= 0)
  2968.         i = reg_renumber[i];
  2969.     }
  2970.       if (GET_CODE (y) == SUBREG)
  2971.     {
  2972.       j = REGNO (SUBREG_REG (y));
  2973.       if (reg_renumber[j] >= 0)
  2974.         j = reg_renumber[j];
  2975.       j += SUBREG_WORD (y);
  2976.     }
  2977.       else
  2978.     {
  2979.       j = REGNO (y);
  2980.       if (reg_renumber[j] >= 0)
  2981.         j = reg_renumber[j];
  2982.     }
  2983.       return i == j;
  2984.     }
  2985.   /* Now we have disposed of all the cases 
  2986.      in which different rtx codes can match.  */
  2987.   if (code != GET_CODE (y))
  2988.     return 0;
  2989.   switch (code)
  2990.     {
  2991.     case PC:
  2992.     case CC0:
  2993.     case ADDR_VEC:
  2994.     case ADDR_DIFF_VEC:
  2995.       return 0;
  2996.  
  2997.     case CONST_INT:
  2998.       return XINT (x, 0) == XINT (y, 0);
  2999.  
  3000.     case LABEL_REF:
  3001.       /* Two label-refs are equivalent if they point at labels
  3002.      in the same position in the instruction stream.  */
  3003.       return (next_real_insn (XEXP (x, 0))
  3004.           == next_real_insn (XEXP (y, 0)));
  3005.  
  3006.     case SYMBOL_REF:
  3007.       return XSTR (x, 0) == XSTR (y, 0);
  3008.     }
  3009.  
  3010.   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
  3011.  
  3012.   if (GET_MODE (x) != GET_MODE (y))
  3013.     return 0;
  3014.  
  3015.   /* Compare the elements.  If any pair of corresponding elements
  3016.      fail to match, return 0 for the whole things.  */
  3017.  
  3018.   fmt = GET_RTX_FORMAT (code);
  3019.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  3020.     {
  3021.       register int j;
  3022.       switch (fmt[i])
  3023.     {
  3024.     case 'i':
  3025.       if (XINT (x, i) != XINT (y, i))
  3026.         return 0;
  3027.       break;
  3028.  
  3029.     case 's':
  3030.       if (strcmp (XSTR (x, i), XSTR (y, i)))
  3031.         return 0;
  3032.       break;
  3033.  
  3034.     case 'e':
  3035.       if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
  3036.         return 0;
  3037.       break;
  3038.  
  3039.     case 'u':
  3040.       if (XEXP (x, i) != XEXP (y, i))
  3041.         return 0;
  3042.       /* fall through.  */
  3043.     case '0':
  3044.       break;
  3045.  
  3046.     case 'E':
  3047.       if (XVECLEN (x, i) != XVECLEN (y, i))
  3048.         return 0;
  3049.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  3050.         if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
  3051.           return 0;
  3052.       break;
  3053.  
  3054.     default:
  3055.       abort ();
  3056.     }
  3057.     }
  3058.   return 1;
  3059. }
  3060.  
  3061. /* If X is a hard register or equivalent to one or a subregister of one,
  3062.    return the hard register number.  If X is a pseudo register that was not
  3063.    assigned a hard register, return the pseudo register number.  Otherwise,
  3064.    return -1.  Any rtx is valid for X.  */
  3065.  
  3066. int
  3067. true_regnum (x)
  3068.      rtx x;
  3069. {
  3070.   if (GET_CODE (x) == REG)
  3071.     {
  3072.       if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
  3073.     return reg_renumber[REGNO (x)];
  3074.       return REGNO (x);
  3075.     }
  3076.   if (GET_CODE (x) == SUBREG)
  3077.     {
  3078.       int base = true_regnum (SUBREG_REG (x));
  3079.       if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
  3080.     return SUBREG_WORD (x) + base;
  3081.     }
  3082.   return -1;
  3083. }
  3084.  
  3085. /* Optimize code of the form:
  3086.  
  3087.     for (x = a[i]; x; ...)
  3088.       ...
  3089.     for (x = a[i]; x; ...)
  3090.       ...
  3091.       foo:
  3092.  
  3093.    Loop optimize will change the above code into
  3094.  
  3095.     if (x = a[i])
  3096.       for (;;)
  3097.          { ...; if (! (x = ...)) break; }
  3098.     if (x = a[i])
  3099.       for (;;)
  3100.          { ...; if (! (x = ...)) break; }
  3101.       foo:
  3102.  
  3103.    In general, if the first test fails, the program can branch
  3104.    directly to `foo' and skip the second try which is doomed to fail.
  3105.    We run this after loop optimization and before flow analysis.  */
  3106.    
  3107. /* When comparing the insn patterns, we track the fact that different
  3108.    pseudo-register numbers may have been used in each computation.
  3109.    The following array stores an equivalence -- same_regs[I] == J means
  3110.    that pseudo register I was used in the first set of tests in a context
  3111.    where J was used in the second set.  We also count the number of such
  3112.    pending equivalences.  If nonzero, the expressions really aren't the
  3113.    same.  */
  3114.  
  3115. static short *same_regs;
  3116.  
  3117. static int num_same_regs;
  3118.  
  3119. /* Track any registers modified between the target of the first jump and
  3120.    the second jump.  They never compare equal.  */
  3121.  
  3122. static char *modified_regs;
  3123.  
  3124. /* Record if memory was modified.  */
  3125.  
  3126. static int modified_mem;
  3127.  
  3128. /* Called via note_stores on each insn between the target of the first 
  3129.    branch and the second branch.  It marks any changed registers.  */
  3130.  
  3131. static void
  3132. mark_modified_reg (dest, x)
  3133.      rtx dest;
  3134.      rtx x;
  3135. {
  3136.   int regno, i;
  3137.  
  3138.   if (GET_CODE (dest) == SUBREG)
  3139.     dest = SUBREG_REG (dest);
  3140.  
  3141.   if (GET_CODE (dest) == MEM)
  3142.     modified_mem = 1;
  3143.  
  3144.   if (GET_CODE (dest) != REG)
  3145.     return;
  3146.  
  3147.   regno = REGNO (dest);
  3148.   if (regno >= FIRST_PSEUDO_REGISTER)
  3149.     modified_regs[regno] = 1;
  3150.   else
  3151.     for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
  3152.       modified_regs[regno + i] = 1;
  3153. }
  3154.  
  3155. /* F is the first insn in the chain of insns.  */
  3156.    
  3157. void
  3158. thread_jumps (f, max_reg, verbose)
  3159.      rtx f;
  3160.      int max_reg;
  3161.      int verbose;
  3162. {
  3163.   /* Basic algorithm is to find a conditional branch,
  3164.      the label it may branch to, and the branch after
  3165.      that label.  If the two branches test the same condition,
  3166.      walk back from both branch paths until the insn patterns
  3167.      differ, or code labels are hit.  If we make it back to
  3168.      the target of the first branch, then we know that the first branch
  3169.      will either always succeed or always fail depending on the relative
  3170.      senses of the two branches.  So adjust the first branch accordingly
  3171.      in this case.  */
  3172.      
  3173.   rtx label, b1, b2, t1, t2;
  3174.   enum rtx_code code1, code2;
  3175.   rtx b1op0, b1op1, b2op0, b2op1;
  3176.   int changed = 1;
  3177.   int i;
  3178.   short *all_reset;
  3179.  
  3180.   /* Allocate register tables and quick-reset table.  */
  3181.   modified_regs = (char *) alloca (max_reg * sizeof (char));
  3182.   same_regs = (short *) alloca (max_reg * sizeof (short));
  3183.   all_reset = (short *) alloca (max_reg * sizeof (short));
  3184.   for (i = 0; i < max_reg; i++)
  3185.     all_reset[i] = -1;
  3186.     
  3187.   while (changed)
  3188.     {
  3189.       changed = 0;
  3190.  
  3191.       for (b1 = f; b1; b1 = NEXT_INSN (b1))
  3192.     {
  3193.       /* Get to a candidate branch insn.  */
  3194.       if (GET_CODE (b1) != JUMP_INSN
  3195.           || ! condjump_p (b1) || simplejump_p (b1)
  3196.           || JUMP_LABEL (b1) == 0)
  3197.         continue;
  3198.  
  3199.       bzero (modified_regs, max_reg * sizeof (char));
  3200.       modified_mem = 0;
  3201.  
  3202.       bcopy (all_reset, same_regs, max_reg * sizeof (short));
  3203.       num_same_regs = 0;
  3204.  
  3205.       label = JUMP_LABEL (b1);
  3206.  
  3207.       /* Look for a branch after the target.  Record any registers and
  3208.          memory modified between the target and the branch.  Stop when we
  3209.          get to a label since we can't know what was changed there.  */
  3210.       for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
  3211.         {
  3212.           if (GET_CODE (b2) == CODE_LABEL)
  3213.         break;
  3214.  
  3215.           else if (GET_CODE (b2) == JUMP_INSN)
  3216.         {
  3217.           /* If this is an unconditional jump and is the only use of
  3218.              its target label, we can follow it.  */
  3219.           if (simplejump_p (b2)
  3220.               && JUMP_LABEL (b2) != 0
  3221.               && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
  3222.             {
  3223.               b2 = JUMP_LABEL (b2);
  3224.               continue;
  3225.             }
  3226.           else
  3227.             break;
  3228.         }
  3229.  
  3230.           if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
  3231.         continue;
  3232.  
  3233.           if (GET_CODE (b2) == CALL_INSN)
  3234.         {
  3235.           modified_mem = 1;
  3236.           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  3237.             if (call_used_regs[i] && ! fixed_regs[i]
  3238.             && i != STACK_POINTER_REGNUM
  3239.             && i != FRAME_POINTER_REGNUM
  3240.             && i != ARG_POINTER_REGNUM)
  3241.               modified_regs[i] = 1;
  3242.         }
  3243.  
  3244.           note_stores (PATTERN (b2), mark_modified_reg);
  3245.         }
  3246.  
  3247.       /* Check the next candidate branch insn from the label
  3248.          of the first.  */
  3249.       if (b2 == 0
  3250.           || GET_CODE (b2) != JUMP_INSN
  3251.           || b2 == b1
  3252.           || ! condjump_p (b2)
  3253.           || simplejump_p (b2))
  3254.         continue;
  3255.  
  3256.       /* Get the comparison codes and operands, reversing the
  3257.          codes if appropriate.  If we don't have comparison codes,
  3258.          we can't do anything.  */
  3259.       b1op0 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 0);
  3260.       b1op1 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 1);
  3261.       code1 = GET_CODE (XEXP (SET_SRC (PATTERN (b1)), 0));
  3262.       if (XEXP (SET_SRC (PATTERN (b1)), 1) == pc_rtx)
  3263.         code1 = reverse_condition (code1);
  3264.  
  3265.       b2op0 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 0);
  3266.       b2op1 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 1);
  3267.       code2 = GET_CODE (XEXP (SET_SRC (PATTERN (b2)), 0));
  3268.       if (XEXP (SET_SRC (PATTERN (b2)), 1) == pc_rtx)
  3269.         code2 = reverse_condition (code2);
  3270.  
  3271.       /* If they test the same things and knowing that B1 branches
  3272.          tells us whether or not B2 branches, check if we
  3273.          can thread the branch.  */
  3274.       if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
  3275.           && rtx_equal_for_thread_p (b1op1, b2op1, b2)
  3276.           && (comparison_dominates_p (code1, code2)
  3277.           || comparison_dominates_p (code1, reverse_condition (code2))))
  3278.         {
  3279.           t1 = prev_nonnote_insn (b1);
  3280.           t2 = prev_nonnote_insn (b2);
  3281.           
  3282.           while (t1 != 0 && t2 != 0)
  3283.         {
  3284.           if (t1 == 0 || t2 == 0)
  3285.             break;
  3286.  
  3287.           if (t2 == label)
  3288.             {
  3289.               /* We have reached the target of the first branch.
  3290.                  If there are no pending register equivalents,
  3291.              we know that this branch will either always
  3292.              succeed (if the senses of the two branches are
  3293.              the same) or always fail (if not).  */
  3294.               rtx new_label;
  3295.  
  3296.               if (num_same_regs != 0)
  3297.             break;
  3298.  
  3299.               if (comparison_dominates_p (code1, code2))
  3300.                   new_label = JUMP_LABEL (b2);
  3301.               else
  3302.             new_label = get_label_after (b2);
  3303.  
  3304.               redirect_jump (b1, new_label);
  3305.               changed = 1;
  3306.               break;
  3307.             }
  3308.             
  3309.           /* If either of these is not a normal insn (it might be
  3310.              a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail.  (NOTEs
  3311.              have already been skipped above.)  Similarly, fail
  3312.              if the insns are different.  */
  3313.           if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
  3314.               || recog_memoized (t1) != recog_memoized (t2)
  3315.               || ! rtx_equal_for_thread_p (PATTERN (t1),
  3316.                            PATTERN (t2), t2))
  3317.             break;
  3318.             
  3319.           t1 = prev_nonnote_insn (t1);
  3320.           t2 = prev_nonnote_insn (t2);
  3321.         }
  3322.         }
  3323.     }
  3324.     }
  3325. }
  3326.  
  3327. /* This is like RTX_EQUAL_P except that it knows about our handling of
  3328.    possibly equivalent registers and knows to consider volatile and
  3329.    modified objects as not equal.
  3330.    
  3331.    YINSN is the insn containing Y.  */
  3332.  
  3333. int
  3334. rtx_equal_for_thread_p (x, y, yinsn)
  3335.      rtx x, y;
  3336.      rtx yinsn;
  3337. {
  3338.   register int i;
  3339.   register int j;
  3340.   register enum rtx_code code;
  3341.   register char *fmt;
  3342.  
  3343.   code = GET_CODE (x);
  3344.   /* Rtx's of different codes cannot be equal.  */
  3345.   if (code != GET_CODE (y))
  3346.     return 0;
  3347.  
  3348.   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
  3349.      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
  3350.  
  3351.   if (GET_MODE (x) != GET_MODE (y))
  3352.     return 0;
  3353.  
  3354.   /* Handle special-cases first.  */
  3355.   switch (code)
  3356.     {
  3357.     case REG:
  3358.       if (REGNO (x) == REGNO (y) && ! modified_regs[REGNO (x)])
  3359.         return 1;
  3360.  
  3361.       /* If neither is user variable or hard register, check for possible
  3362.      equivalence.  */
  3363.       if (REG_USERVAR_P (x) || REG_USERVAR_P (y)
  3364.       || REGNO (x) < FIRST_PSEUDO_REGISTER
  3365.       || REGNO (y) < FIRST_PSEUDO_REGISTER)
  3366.     return 0;
  3367.  
  3368.       if (same_regs[REGNO (x)] == -1)
  3369.     {
  3370.       same_regs[REGNO (x)] = REGNO (y);
  3371.       num_same_regs++;
  3372.  
  3373.       /* If this is the first time we are seeing a register on the `Y'
  3374.          side, see if it is the last use.  If not, we can't thread the 
  3375.          jump, so mark it as not equivalent.  */
  3376.       if (regno_last_uid[REGNO (y)] != INSN_UID (yinsn))
  3377.         return 0;
  3378.  
  3379.       return 1;
  3380.     }
  3381.       else
  3382.     return (same_regs[REGNO (x)] == REGNO (y));
  3383.  
  3384.       break;
  3385.  
  3386.     case MEM:
  3387.       /* If memory modified or either volatile, not eqivalent.
  3388.      Else, check address. */
  3389.       if (modified_mem || MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
  3390.     return 0;
  3391.  
  3392.       return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
  3393.  
  3394.     case ASM_INPUT:
  3395.       if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
  3396.     return 0;
  3397.  
  3398.       break;
  3399.  
  3400.     case SET:
  3401.       /* Cancel a pending `same_regs' if setting equivalenced registers.
  3402.      Then process source.  */
  3403.       if (GET_CODE (SET_DEST (x)) == REG
  3404.           && GET_CODE (SET_DEST (y)) == REG)
  3405.     {
  3406.           if (same_regs[REGNO (SET_DEST (x))] == REGNO (SET_DEST (y)))
  3407.         {
  3408.           same_regs[REGNO (SET_DEST (x))] = -1;
  3409.           num_same_regs--;
  3410.         }
  3411.       else if (REGNO (SET_DEST (x)) != REGNO (SET_DEST (y)))
  3412.         return 0;
  3413.     }
  3414.       else
  3415.     if (rtx_equal_for_thread_p (SET_DEST (x), SET_DEST (y), yinsn) == 0)
  3416.       return 0;
  3417.  
  3418.       return rtx_equal_for_thread_p (SET_SRC (x), SET_SRC (y), yinsn);
  3419.  
  3420.     case LABEL_REF:
  3421.       return XEXP (x, 0) == XEXP (y, 0);
  3422.  
  3423.     case SYMBOL_REF:
  3424.       return XSTR (x, 0) == XSTR (y, 0);
  3425.     }
  3426.  
  3427.   if (x == y)
  3428.     return 1;
  3429.  
  3430.   fmt = GET_RTX_FORMAT (code);
  3431.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  3432.     {
  3433.       switch (fmt[i])
  3434.     {
  3435.     case 'n':
  3436.     case 'i':
  3437.       if (XINT (x, i) != XINT (y, i))
  3438.         return 0;
  3439.       break;
  3440.  
  3441.     case 'V':
  3442.     case 'E':
  3443.       /* Two vectors must have the same length.  */
  3444.       if (XVECLEN (x, i) != XVECLEN (y, i))
  3445.         return 0;
  3446.  
  3447.       /* And the corresponding elements must match.  */
  3448.       for (j = 0; j < XVECLEN (x, i); j++)
  3449.         if (rtx_equal_for_thread_p (XVECEXP (x, i, j),
  3450.                             XVECEXP (y, i, j), yinsn) == 0)
  3451.           return 0;
  3452.       break;
  3453.  
  3454.     case 'e':
  3455.       if (rtx_equal_for_thread_p (XEXP (x, i), XEXP (y, i), yinsn) == 0)
  3456.         return 0;
  3457.       break;
  3458.  
  3459.     case 'S':
  3460.     case 's':
  3461.       if (strcmp (XSTR (x, i), XSTR (y, i)))
  3462.         return 0;
  3463.       break;
  3464.  
  3465.     case 'u':
  3466.       /* These are just backpointers, so they don't matter.  */
  3467.       break;
  3468.  
  3469.     case '0':
  3470.       break;
  3471.  
  3472.       /* It is believed that rtx's at this level will never
  3473.          contain anything but integers and other rtx's,
  3474.          except for within LABEL_REFs and SYMBOL_REFs.  */
  3475.     default:
  3476.       abort ();
  3477.     }
  3478.     }
  3479.   return 1;
  3480. }
  3481.