OSDN Git Service

(get_jump_flags): Check for LABEL == 0 before using LABEL.
[pf3gnuchains/gcc-fork.git] / gcc / reorg.c
1 /* Perform instruction reorganizations for delay slot filling.
2    Copyright (C) 1992 Free Software Foundation, Inc.
3    Contributed by Richard Kenner (kenner@nyu.edu).
4    Hacked by Michael Tiemann (tiemann@cygnus.com).
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22 /* Instruction reorganization pass.
23
24    This pass runs after register allocation and final jump
25    optimization.  It should be the last pass to run before peephole.
26    It serves primarily to fill delay slots of insns, typically branch
27    and call insns.  Other insns typically involve more complicated
28    interactions of data dependencies and resource constraints, and
29    are better handled by scheduling before register allocation (by the
30    function `schedule_insns').
31
32    The Branch Penalty is the number of extra cycles that are needed to
33    execute a branch insn.  On an ideal machine, branches take a single
34    cycle, and the Branch Penalty is 0.  Several RISC machines approach
35    branch delays differently:
36
37    The MIPS and AMD 29000 have a single branch delay slot.  Most insns
38    (except other branches) can be used to fill this slot.  When the
39    slot is filled, two insns execute in two cycles, reducing the
40    branch penalty to zero.
41
42    The Motorola 88000 conditionally exposes its branch delay slot,
43    so code is shorter when it is turned off, but will run faster
44    when useful insns are scheduled there.
45
46    The IBM ROMP has two forms of branch and call insns, both with and
47    without a delay slot.  Much like the 88k, insns not using the delay
48    slot can be shorted (2 bytes vs. 4 bytes), but will run slowed.
49
50    The SPARC always has a branch delay slot, but its effects can be
51    annulled when the branch is not taken.  This means that failing to
52    find other sources of insns, we can hoist an insn from the branch
53    target that would only be safe to execute knowing that the branch
54    is taken.
55
56    The HP-PA always has a branch delay slot.  For unconditional branches
57    its effects can be annulled when the branch is taken.  The effects 
58    of the delay slot in a conditional branch can be nullified for forward
59    taken branches, or for untaken backward branches.  This means
60    we can hoist insns from the fall-through path for forward branches or
61    steal insns from the target of backward branches.
62
63    Three techniques for filling delay slots have been implemented so far:
64
65    (1) `fill_simple_delay_slots' is the simplest, most efficient way
66    to fill delay slots.  This pass first looks for insns which come
67    from before the branch and which are safe to execute after the
68    branch.  Then it searches after the insn requiring delay slots or,
69    in the case of a branch, for insns that are after the point at
70    which the branch merges into the fallthrough code, if such a point
71    exists.  When such insns are found, the branch penalty decreases
72    and no code expansion takes place.
73
74    (2) `fill_eager_delay_slots' is more complicated: it is used for
75    scheduling conditional jumps, or for scheduling jumps which cannot
76    be filled using (1).  A machine need not have annulled jumps to use
77    this strategy, but it helps (by keeping more options open).
78    `fill_eager_delay_slots' tries to guess the direction the branch
79    will go; if it guesses right 100% of the time, it can reduce the
80    branch penalty as much as `fill_simple_delay_slots' does.  If it
81    guesses wrong 100% of the time, it might as well schedule nops (or
82    on the m88k, unexpose the branch slot).  When
83    `fill_eager_delay_slots' takes insns from the fall-through path of
84    the jump, usually there is no code expansion; when it takes insns
85    from the branch target, there is code expansion if it is not the
86    only way to reach that target.
87
88    (3) `relax_delay_slots' uses a set of rules to simplify code that
89    has been reorganized by (1) and (2).  It finds cases where
90    conditional test can be eliminated, jumps can be threaded, extra
91    insns can be eliminated, etc.  It is the job of (1) and (2) to do a
92    good job of scheduling locally; `relax_delay_slots' takes care of
93    making the various individual schedules work well together.  It is
94    especially tuned to handle the control flow interactions of branch
95    insns.  It does nothing for insns with delay slots that do not
96    branch.
97
98    On machines that use CC0, we are very conservative.  We will not make
99    a copy of an insn involving CC0 since we want to maintain a 1-1
100    correspondence between the insn that sets and uses CC0.  The insns are
101    allowed to be separated by placing an insn that sets CC0 (but not an insn
102    that uses CC0; we could do this, but it doesn't seem worthwhile) in a
103    delay slot.  In that case, we point each insn at the other with REG_CC_USER
104    and REG_CC_SETTER notes.  Note that these restrictions affect very few
105    machines because most RISC machines with delay slots will not use CC0
106    (the RT is the only known exception at this point).
107
108    Not yet implemented:
109
110    The Acorn Risc Machine can conditionally execute most insns, so
111    it is profitable to move single insns into a position to execute
112    based on the condition code of the previous insn.
113
114    The HP-PA can conditionally nullify insns, providing a similar
115    effect to the ARM, differing mostly in which insn is "in charge".   */
116
117 #include <stdio.h>
118 #include "config.h"
119 #include "rtl.h"
120 #include "insn-config.h"
121 #include "conditions.h"
122 #include "hard-reg-set.h"
123 #include "basic-block.h"
124 #include "regs.h"
125 #include "insn-flags.h"
126 #include "recog.h"
127 #include "flags.h"
128 #include "output.h"
129 #include "obstack.h"
130 #include "insn-attr.h"
131
132 #ifdef DELAY_SLOTS
133
134 #define obstack_chunk_alloc xmalloc
135 #define obstack_chunk_free free
136
137 #ifndef ANNUL_IFTRUE_SLOTS
138 #define eligible_for_annul_true(INSN, SLOTS, TRIAL, FLAGS) 0
139 #endif
140 #ifndef ANNUL_IFFALSE_SLOTS
141 #define eligible_for_annul_false(INSN, SLOTS, TRIAL, FLAGS) 0
142 #endif
143
144 /* Insns which have delay slots that have not yet been filled.  */
145
146 static struct obstack unfilled_slots_obstack;
147 static rtx *unfilled_firstobj;
148
149 /* Define macros to refer to the first and last slot containing unfilled
150    insns.  These are used because the list may move and its address
151    should be recomputed at each use.  */
152
153 #define unfilled_slots_base     \
154   ((rtx *) obstack_base (&unfilled_slots_obstack))
155
156 #define unfilled_slots_next     \
157   ((rtx *) obstack_next_free (&unfilled_slots_obstack))
158
159 /* This structure is used to indicate which hardware resources are set or
160    needed by insns so far.  */
161
162 struct resources
163 {
164   char memory;                  /* Insn sets or needs a memory location.  */
165   char volatil;                 /* Insn sets or needs a volatile memory loc. */
166   char cc;                      /* Insn sets or needs the condition codes.  */
167   HARD_REG_SET regs;            /* Which registers are set or needed.  */
168 };
169
170 /* Macro to clear all resources.  */
171 #define CLEAR_RESOURCE(RES)     \
172  do { (RES)->memory = (RES)->volatil = (RES)->cc = 0;   \
173       CLEAR_HARD_REG_SET ((RES)->regs); } while (0)
174
175 /* Indicates what resources are required at the beginning of the epilogue.  */
176 static struct resources start_of_epilogue_needs;
177
178 /* Indicates what resources are required at function end.  */
179 static struct resources end_of_function_needs;
180
181 /* Points to the label before the end of the function.  */
182 static rtx end_of_function_label;
183
184 /* This structure is used to record liveness information at the targets or
185    fallthrough insns of branches.  We will most likely need the information
186    at targets again, so save them in a hash table rather than recomputing them
187    each time.  */
188
189 struct target_info
190 {
191   int uid;                      /* INSN_UID of target.  */
192   struct target_info *next;     /* Next info for same hash bucket.  */
193   HARD_REG_SET live_regs;       /* Registers live at target.  */
194   int block;                    /* Basic block number containing target.  */
195   int bb_tick;                  /* Generation count of basic block info.  */
196 };
197
198 #define TARGET_HASH_PRIME 257
199
200 /* Define the hash table itself.  */
201 static struct target_info **target_hash_table;
202
203 /* For each basic block, we maintain a generation number of its basic
204    block info, which is updated each time we move an insn from the
205    target of a jump.  This is the generation number indexed by block
206    number.  */
207
208 static int *bb_ticks;
209
210 /* Mapping between INSN_UID's and position in the code since INSN_UID's do
211    not always monotonically increase.  */
212 static int *uid_to_ruid;
213
214 /* Highest valid index in `uid_to_ruid'.  */
215 static int max_uid;
216
217 static void mark_referenced_resources PROTO((rtx, struct resources *, int));
218 static void mark_set_resources  PROTO((rtx, struct resources *, int, int));
219 static int stop_search_p        PROTO((rtx, int));
220 static int resource_conflicts_p PROTO((struct resources *,
221                                        struct resources *));
222 static int insn_references_resource_p PROTO((rtx, struct resources *, int));
223 static int insn_sets_resources_p PROTO((rtx, struct resources *, int));
224 static rtx find_end_label       PROTO((void));
225 static rtx emit_delay_sequence  PROTO((rtx, rtx, int, int));
226 static rtx add_to_delay_list    PROTO((rtx, rtx));
227 static void delete_from_delay_slot PROTO((rtx));
228 static void delete_scheduled_jump PROTO((rtx));
229 static void note_delay_statistics PROTO((int, int));
230 static rtx optimize_skip        PROTO((rtx));
231 static int get_jump_flags PROTO((rtx, rtx));
232 static int rare_destination PROTO((rtx));
233 static int mostly_true_jump     PROTO((rtx, rtx));
234 static rtx get_branch_condition PROTO((rtx, rtx));
235 static int condition_dominates_p PROTO((rtx, rtx));
236 static rtx steal_delay_list_from_target PROTO((rtx, rtx, rtx, rtx,
237                                                struct resources *,
238                                                struct resources *,
239                                                struct resources *,
240                                                int, int *, int *, rtx *));
241 static rtx steal_delay_list_from_fallthrough PROTO((rtx, rtx, rtx, rtx,
242                                                     struct resources *,
243                                                     struct resources *,
244                                                     struct resources *,
245                                                     int, int *, int *));
246 static void try_merge_delay_insns PROTO((rtx, rtx));
247 static int redundant_insn_p     PROTO((rtx, rtx, rtx));
248 static int own_thread_p         PROTO((rtx, rtx, int));
249 static int find_basic_block     PROTO((rtx));
250 static void update_block        PROTO((rtx, rtx));
251 static void update_reg_dead_notes PROTO((rtx, rtx));
252 static void update_live_status  PROTO((rtx, rtx));
253 static rtx next_insn_no_annul   PROTO((rtx));
254 static void mark_target_live_regs PROTO((rtx, struct resources *));
255 static void fill_simple_delay_slots PROTO((rtx, int));
256 static rtx fill_slots_from_thread PROTO((rtx, rtx, rtx, rtx, int, int,
257                                          int, int, int, int *));
258 static void fill_eager_delay_slots PROTO((rtx));
259 static void relax_delay_slots   PROTO((rtx));
260 static void make_return_insns   PROTO((rtx));
261 \f
262 /* Given X, some rtl, and RES, a pointer to a `struct resource', mark
263    which resources are references by the insn.  If INCLUDE_CALLED_ROUTINE
264    is TRUE, resources used by the called routine will be included for
265    CALL_INSNs.  */
266
267 static void
268 mark_referenced_resources (x, res, include_delayed_effects)
269      register rtx x;
270      register struct resources *res;
271      register int include_delayed_effects;
272 {
273   register enum rtx_code code = GET_CODE (x);
274   register int i, j;
275   register char *format_ptr;
276
277   /* Handle leaf items for which we set resource flags.  Also, special-case
278      CALL, SET and CLOBBER operators.  */
279   switch (code)
280     {
281     case CONST:
282     case CONST_INT:
283     case CONST_DOUBLE:
284     case PC:
285     case SYMBOL_REF:
286     case LABEL_REF:
287       return;
288
289     case SUBREG:
290       if (GET_CODE (SUBREG_REG (x)) != REG)
291         mark_referenced_resources (SUBREG_REG (x), res, 0);
292       else
293         {
294           int regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
295           int last_regno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
296           for (i = regno; i < last_regno; i++)
297             SET_HARD_REG_BIT (res->regs, i);
298         }
299       return;
300
301     case REG:
302       for (i = 0; i < HARD_REGNO_NREGS (REGNO (x), GET_MODE (x)); i++)
303         SET_HARD_REG_BIT (res->regs, REGNO (x) + i);
304       return;
305
306     case MEM:
307       /* If this memory shouldn't change, it really isn't referencing
308          memory.  */
309       if (! RTX_UNCHANGING_P (x))
310         res->memory = 1;
311       res->volatil = MEM_VOLATILE_P (x);
312
313       /* Mark registers used to access memory.  */
314       mark_referenced_resources (XEXP (x, 0), res, 0);
315       return;
316
317     case CC0:
318       res->cc = 1;
319       return;
320
321     case UNSPEC_VOLATILE:
322     case ASM_INPUT:
323       /* Traditional asm's are always volatile.  */
324       res->volatil = 1;
325       return;
326
327     case ASM_OPERANDS:
328       res->volatil = MEM_VOLATILE_P (x);
329
330       /* For all ASM_OPERANDS, we must traverse the vector of input operands.
331          We can not just fall through here since then we would be confused
332          by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
333          traditional asms unlike their normal usage.  */
334       
335       for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
336         mark_referenced_resources (ASM_OPERANDS_INPUT (x, i), res, 0);
337       return;
338
339     case CALL:
340       /* The first operand will be a (MEM (xxx)) but doesn't really reference
341          memory.  The second operand may be referenced, though.  */
342       mark_referenced_resources (XEXP (XEXP (x, 0), 0), res, 0);
343       mark_referenced_resources (XEXP (x, 1), res, 0);
344       return;
345
346     case SET:
347       /* Usually, the first operand of SET is set, not referenced.  But
348          registers used to access memory are referenced.  SET_DEST is
349          also referenced if it is a ZERO_EXTRACT or SIGN_EXTRACT.  */
350
351       mark_referenced_resources (SET_SRC (x), res, 0);
352
353       x = SET_DEST (x);
354       if (GET_CODE (x) == SIGN_EXTRACT || GET_CODE (x) == ZERO_EXTRACT)
355         mark_referenced_resources (x, res, 0);
356       else if (GET_CODE (x) == SUBREG)
357         x = SUBREG_REG (x);
358       if (GET_CODE (x) == MEM)
359         mark_referenced_resources (XEXP (x, 0), res, 0);
360       return;
361
362     case CLOBBER:
363       return;
364
365     case CALL_INSN:
366       if (include_delayed_effects)
367         {
368           /* A CALL references memory, the frame pointer if it exists, the
369              stack pointer, any global registers and any registers given in
370              USE insns immediately in front of the CALL.
371
372              However, we may have moved some of the parameter loading insns
373              into the delay slot of this CALL.  If so, the USE's for them
374              don't count and should be skipped.  */
375           rtx insn = PREV_INSN (x);
376           rtx sequence = 0;
377           int seq_size = 0;
378           int i;
379
380           /* If we are part of a delay slot sequence, point at the SEQUENCE. */
381           if (NEXT_INSN (insn) != x)
382             {
383               sequence = PATTERN (NEXT_INSN (insn));
384               seq_size = XVECLEN (sequence, 0);
385               if (GET_CODE (sequence) != SEQUENCE)
386                 abort ();
387             }
388
389           res->memory = 1;
390           SET_HARD_REG_BIT (res->regs, STACK_POINTER_REGNUM);
391           if (frame_pointer_needed)
392             SET_HARD_REG_BIT (res->regs, FRAME_POINTER_REGNUM);
393
394           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
395             if (global_regs[i])
396               SET_HARD_REG_BIT (res->regs, i);
397
398           /* Skip any labels between the CALL_INSN and possible USE insns.  */
399           while (GET_CODE (insn) == CODE_LABEL)
400             insn = PREV_INSN (insn);
401
402           for ( ; (insn && GET_CODE (insn) == INSN
403                    && GET_CODE (PATTERN (insn)) == USE);
404                insn = PREV_INSN (insn))
405             {
406               for (i = 1; i < seq_size; i++)
407                 {
408                   rtx slot_pat = PATTERN (XVECEXP (sequence, 0, i));
409                   if (GET_CODE (slot_pat) == SET
410                       && rtx_equal_p (SET_DEST (slot_pat),
411                                       XEXP (PATTERN (insn), 0)))
412                     break;
413                 }
414               if (i >= seq_size)
415                 mark_referenced_resources (XEXP (PATTERN (insn), 0), res, 0);
416             }
417         }
418
419       /* ... fall through to other INSN processing ... */
420
421     case INSN:
422     case JUMP_INSN:
423
424 #ifdef INSN_REFERENCES_ARE_DELAYED
425       if (! include_delayed_effects
426           && INSN_REFERENCES_ARE_DELAYED (x))
427         return;
428 #endif
429
430       /* No special processing, just speed up.  */
431       mark_referenced_resources (PATTERN (x), res, include_delayed_effects);
432       return;
433     }
434
435   /* Process each sub-expression and flag what it needs.  */
436   format_ptr = GET_RTX_FORMAT (code);
437   for (i = 0; i < GET_RTX_LENGTH (code); i++)
438     switch (*format_ptr++)
439       {
440       case 'e':
441         mark_referenced_resources (XEXP (x, i), res, include_delayed_effects);
442         break;
443
444       case 'E':
445         for (j = 0; j < XVECLEN (x, i); j++)
446           mark_referenced_resources (XVECEXP (x, i, j), res,
447                                      include_delayed_effects);
448         break;
449       }
450 }
451 \f
452 /* Given X, a part of an insn, and a pointer to a `struct resource', RES,
453    indicate which resources are modified by the insn. If INCLUDE_CALLED_ROUTINE
454    is nonzero, also mark resources potentially set by the called routine.
455
456    If IN_DEST is nonzero, it means we are inside a SET.  Otherwise,
457    objects are being referenced instead of set.
458
459    We never mark the insn as modifying the condition code unless it explicitly
460    SETs CC0 even though this is not totally correct.  The reason for this is
461    that we require a SET of CC0 to immediately precede the reference to CC0.
462    So if some other insn sets CC0 as a side-effect, we know it cannot affect
463    our computation and thus may be placed in a delay slot.   */
464
465 static void
466 mark_set_resources (x, res, in_dest, include_delayed_effects)
467      register rtx x;
468      register struct resources *res;
469      int in_dest;
470      int include_delayed_effects;
471 {
472   register enum rtx_code code;
473   register int i, j;
474   register char *format_ptr;
475
476  restart:
477
478   code = GET_CODE (x);
479
480   switch (code)
481     {
482     case NOTE:
483     case BARRIER:
484     case CODE_LABEL:
485     case USE:
486     case CONST_INT:
487     case CONST_DOUBLE:
488     case LABEL_REF:
489     case SYMBOL_REF:
490     case CONST:
491     case PC:
492       /* These don't set any resources.  */
493       return;
494
495     case CC0:
496       if (in_dest)
497         res->cc = 1;
498       return;
499
500     case CALL_INSN:
501       /* Called routine modifies the condition code, memory, any registers
502          that aren't saved across calls, global registers and anything
503          explicitly CLOBBERed immediately after the CALL_INSN.  */
504
505       if (include_delayed_effects)
506         {
507           rtx next = NEXT_INSN (x);
508
509           res->cc = res->memory = 1;
510           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
511             if (call_used_regs[i] || global_regs[i])
512               SET_HARD_REG_BIT (res->regs, i);
513
514           /* Skip any possible labels between the CALL_INSN and CLOBBERs.  */
515           while (GET_CODE (next) == CODE_LABEL)
516             next = NEXT_INSN (next);
517
518           for (; (next && GET_CODE (next) == INSN
519                   && GET_CODE (PATTERN (next)) == CLOBBER);
520                next = NEXT_INSN (next))
521             mark_set_resources (XEXP (PATTERN (next), 0), res, 1, 0);
522         }
523
524       /* ... and also what it's RTL says it modifies, if anything.  */
525
526     case JUMP_INSN:
527     case INSN:
528
529         /* An insn consisting of just a CLOBBER (or USE) is just for flow
530            and doesn't actually do anything, so we ignore it.  */
531
532 #ifdef INSN_SETS_ARE_DELAYED
533       if (! include_delayed_effects
534           && INSN_SETS_ARE_DELAYED (x))
535         return;
536 #endif
537
538       x = PATTERN (x);
539       if (GET_CODE (x) != USE && GET_CODE (x) != CLOBBER)
540         goto restart;
541       return;
542
543     case SET:
544       /* If the source of a SET is a CALL, this is actually done by
545          the called routine.  So only include it if we are to include the
546          effects of the calling routine.  */
547
548       mark_set_resources (SET_DEST (x), res,
549                           (include_delayed_effects
550                            || GET_CODE (SET_SRC (x)) != CALL),
551                           0);
552
553       mark_set_resources (SET_SRC (x), res, 0, 0);
554       return;
555
556     case CLOBBER:
557       mark_set_resources (XEXP (x, 0), res, 1, 0);
558       return;
559       
560     case SEQUENCE:
561       for (i = 0; i < XVECLEN (x, 0); i++)
562         if (! (INSN_ANNULLED_BRANCH_P (XVECEXP (x, 0, 0))
563                && INSN_FROM_TARGET_P (XVECEXP (x, 0, i))))
564           mark_set_resources (XVECEXP (x, 0, i), res, 0,
565                               include_delayed_effects);
566       return;
567
568     case POST_INC:
569     case PRE_INC:
570     case POST_DEC:
571     case PRE_DEC:
572       mark_set_resources (XEXP (x, 0), res, 1, 0);
573       return;
574
575     case ZERO_EXTRACT:
576       mark_set_resources (XEXP (x, 0), res, in_dest, 0);
577       mark_set_resources (XEXP (x, 1), res, 0, 0);
578       mark_set_resources (XEXP (x, 2), res, 0, 0);
579       return;
580
581     case MEM:
582       if (in_dest)
583         {
584           res->memory = 1;
585           res->volatil = MEM_VOLATILE_P (x);
586         }
587
588       mark_set_resources (XEXP (x, 0), res, 0, 0);
589       return;
590
591     case REG:
592       if (in_dest)
593         for (i = 0; i < HARD_REGNO_NREGS (REGNO (x), GET_MODE (x)); i++)
594           SET_HARD_REG_BIT (res->regs, REGNO (x) + i);
595       return;
596     }
597
598   /* Process each sub-expression and flag what it needs.  */
599   format_ptr = GET_RTX_FORMAT (code);
600   for (i = 0; i < GET_RTX_LENGTH (code); i++)
601     switch (*format_ptr++)
602       {
603       case 'e':
604         mark_set_resources (XEXP (x, i), res, in_dest, include_delayed_effects);
605         break;
606
607       case 'E':
608         for (j = 0; j < XVECLEN (x, i); j++)
609           mark_set_resources (XVECEXP (x, i, j), res, in_dest,
610                               include_delayed_effects);
611         break;
612       }
613 }
614 \f
615 /* Return TRUE if this insn should stop the search for insn to fill delay
616    slots.  LABELS_P indicates that labels should terminate the search.
617    In all cases, jumps terminate the search.  */
618
619 static int
620 stop_search_p (insn, labels_p)
621      rtx insn;
622      int labels_p;
623 {
624   if (insn == 0)
625     return 1;
626
627   switch (GET_CODE (insn))
628     {
629     case NOTE:
630     case CALL_INSN:
631       return 0;
632
633     case CODE_LABEL:
634       return labels_p;
635
636     case JUMP_INSN:
637     case BARRIER:
638       return 1;
639
640     case INSN:
641       /* OK unless it contains a delay slot or is an `asm' insn of some type.
642          We don't know anything about these.  */
643       return (GET_CODE (PATTERN (insn)) == SEQUENCE
644               || GET_CODE (PATTERN (insn)) == ASM_INPUT
645               || asm_noperands (PATTERN (insn)) >= 0);
646
647     default:
648       abort ();
649     }
650 }
651 \f
652 /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
653    resource set contains a volatile memory reference.  Otherwise, return FALSE.  */
654
655 static int
656 resource_conflicts_p (res1, res2)
657      struct resources *res1, *res2;
658 {
659   if ((res1->cc && res2->cc) || (res1->memory && res2->memory)
660       || res1->volatil || res2->volatil)
661     return 1;
662
663 #ifdef HARD_REG_SET
664   return (res1->regs & res2->regs) != HARD_CONST (0);
665 #else
666   {
667     int i;
668
669     for (i = 0; i < HARD_REG_SET_LONGS; i++)
670       if ((res1->regs[i] & res2->regs[i]) != 0)
671         return 1;
672     return 0;
673   }
674 #endif
675 }
676
677 /* Return TRUE if any resource marked in RES, a `struct resources', is
678    referenced by INSN.  If INCLUDE_CALLED_ROUTINE is set, return if the called
679    routine is using those resources.
680
681    We compute this by computing all the resources referenced by INSN and
682    seeing if this conflicts with RES.  It might be faster to directly check
683    ourselves, and this is the way it used to work, but it means duplicating
684    a large block of complex code.  */
685
686 static int
687 insn_references_resource_p (insn, res, include_delayed_effects)
688      register rtx insn;
689      register struct resources *res;
690      int include_delayed_effects;
691 {
692   struct resources insn_res;
693
694   CLEAR_RESOURCE (&insn_res);
695   mark_referenced_resources (insn, &insn_res, include_delayed_effects);
696   return resource_conflicts_p (&insn_res, res);
697 }
698
699 /* Return TRUE if INSN modifies resources that are marked in RES.
700    INCLUDE_CALLED_ROUTINE is set if the actions of that routine should be
701    included.   CC0 is only modified if it is explicitly set; see comments
702    in front of mark_set_resources for details.  */
703
704 static int
705 insn_sets_resource_p (insn, res, include_delayed_effects)
706      register rtx insn;
707      register struct resources *res;
708      int include_delayed_effects;
709 {
710   struct resources insn_sets;
711
712   CLEAR_RESOURCE (&insn_sets);
713   mark_set_resources (insn, &insn_sets, 0, include_delayed_effects);
714   return resource_conflicts_p (&insn_sets, res);
715 }
716 \f
717 /* Find a label at the end of the function or before a RETURN.  If there is
718    none, make one.  */
719
720 static rtx
721 find_end_label ()
722 {
723   rtx insn;
724
725   /* If we found one previously, return it.  */
726   if (end_of_function_label)
727     return end_of_function_label;
728
729   /* Otherwise, see if there is a label at the end of the function.  If there
730      is, it must be that RETURN insns aren't needed, so that is our return
731      label and we don't have to do anything else.  */
732
733   insn = get_last_insn ();
734   while (GET_CODE (insn) == NOTE
735          || (GET_CODE (insn) == INSN
736              && (GET_CODE (PATTERN (insn)) == USE
737                  || GET_CODE (PATTERN (insn)) == CLOBBER)))
738     insn = PREV_INSN (insn);
739
740   if (GET_CODE (insn) == CODE_LABEL)
741    end_of_function_label = insn;
742   else
743     {
744       /* Otherwise, make a new label and emit a RETURN and BARRIER,
745          if needed.  */
746       end_of_function_label = gen_label_rtx ();
747       LABEL_NUSES (end_of_function_label) = 0;
748       emit_label (end_of_function_label);
749 #ifdef HAVE_return
750       if (HAVE_return)
751         {
752           /* The return we make may have delay slots too.  */
753           rtx insn = gen_return();
754           emit_jump_insn (insn);
755           emit_barrier ();
756           if (num_delay_slots (insn) > 0)
757             obstack_ptr_grow (&unfilled_slots_obstack, insn);
758         }
759 #endif
760     }
761
762   /* Show one additional use for this label so it won't go away until
763      we are done.  */
764   ++LABEL_NUSES (end_of_function_label);
765
766   return end_of_function_label;
767 }
768 \f
769 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
770    the pattern of INSN with the SEQUENCE.
771
772    Chain the insns so that NEXT_INSN of each insn in the sequence points to
773    the next and NEXT_INSN of the last insn in the sequence points to
774    the first insn after the sequence.  Similarly for PREV_INSN.  This makes
775    it easier to scan all insns.
776
777    Returns the SEQUENCE that replaces INSN.  */
778
779 static rtx
780 emit_delay_sequence (insn, list, length, avail)
781      rtx insn;
782      rtx list;
783      int length;
784      int avail;
785 {
786   register int i = 1;
787   register rtx li;
788   int had_barrier = 0;
789
790   /* Allocate the the rtvec to hold the insns and the SEQUENCE. */
791   rtvec seqv = rtvec_alloc (length + 1);
792   rtx seq = gen_rtx (SEQUENCE, VOIDmode, seqv);
793   rtx seq_insn = make_insn_raw (seq);
794   rtx first = get_insns ();
795   rtx last = get_last_insn ();
796
797   /* Make a copy of the insn having delay slots. */
798   rtx delay_insn = copy_rtx (insn);
799
800   /* If INSN is followed by a BARRIER, delete the BARRIER since it will only
801      confuse further processing.  Update LAST in case it was the last insn.  
802      We will put the BARRIER back in later.  */
803   if (NEXT_INSN (insn) && GET_CODE (NEXT_INSN (insn)) == BARRIER)
804     {
805       delete_insn (NEXT_INSN (insn));
806       last = get_last_insn ();
807       had_barrier = 1;
808     }
809
810   /* Splice our SEQUENCE into the insn stream where INSN used to be.  */
811   NEXT_INSN (seq_insn) = NEXT_INSN (insn);
812   PREV_INSN (seq_insn) = PREV_INSN (insn);
813
814   if (insn == last)
815     set_new_first_and_last_insn (first, seq_insn);
816   else
817     PREV_INSN (NEXT_INSN (seq_insn)) = seq_insn;
818
819   if (insn == first)
820     set_new_first_and_last_insn (seq_insn, last);
821   else
822     NEXT_INSN (PREV_INSN (seq_insn)) = seq_insn;
823
824   /* Build our SEQUENCE and rebuild the insn chain.  */
825   XVECEXP (seq, 0, 0) = delay_insn;
826   INSN_DELETED_P (delay_insn) = 0;
827   PREV_INSN (delay_insn) = PREV_INSN (seq_insn);
828
829   for (li = list; li; li = XEXP (li, 1), i++)
830     {
831       rtx tem = XEXP (li, 0);
832       rtx note;
833
834       /* Show that this copy of the insn isn't deleted.  */
835       INSN_DELETED_P (tem) = 0;
836
837       XVECEXP (seq, 0, i) = tem;
838       PREV_INSN (tem) = XVECEXP (seq, 0, i - 1);
839       NEXT_INSN (XVECEXP (seq, 0, i - 1)) = tem;
840
841       /* Remove any REG_DEAD notes because we can't rely on them now
842          that the insn has been moved.  */
843       for (note = REG_NOTES (tem); note; note = XEXP (note, 1))
844         if (REG_NOTE_KIND (note) == REG_DEAD)
845           XEXP (note, 0) = const0_rtx;
846     }
847
848   NEXT_INSN (XVECEXP (seq, 0, length)) = NEXT_INSN (seq_insn);
849
850   /* If the previous insn is a SEQUENCE, update the NEXT_INSN pointer on the
851      last insn in that SEQUENCE to point to us.  Similarly for the first
852      insn in the following insn if it is a SEQUENCE.  */
853
854   if (PREV_INSN (seq_insn) && GET_CODE (PREV_INSN (seq_insn)) == INSN
855       && GET_CODE (PATTERN (PREV_INSN (seq_insn))) == SEQUENCE)
856     NEXT_INSN (XVECEXP (PATTERN (PREV_INSN (seq_insn)), 0,
857                         XVECLEN (PATTERN (PREV_INSN (seq_insn)), 0) - 1))
858       = seq_insn;
859
860   if (NEXT_INSN (seq_insn) && GET_CODE (NEXT_INSN (seq_insn)) == INSN
861       && GET_CODE (PATTERN (NEXT_INSN (seq_insn))) == SEQUENCE)
862     PREV_INSN (XVECEXP (PATTERN (NEXT_INSN (seq_insn)), 0, 0)) = seq_insn;
863     
864   /* If there used to be a BARRIER, put it back.  */
865   if (had_barrier)
866     emit_barrier_after (seq_insn);
867
868   if (i != length + 1)
869     abort ();
870
871   return seq_insn;
872 }
873
874 /* Add INSN to DELAY_LIST and return the head of the new list.  The list must
875    be in the order in which the insns are to be executed.  */
876
877 static rtx
878 add_to_delay_list (insn, delay_list)
879      rtx insn;
880      rtx delay_list;
881 {
882   /* If we have an empty list, just make a new list element.  */
883   if (delay_list == 0)
884     return gen_rtx (INSN_LIST, VOIDmode, insn, NULL_RTX);
885
886   /* Otherwise this must be an INSN_LIST.  Add INSN to the end of the
887      list.  */
888   XEXP (delay_list, 1) = add_to_delay_list (insn, XEXP (delay_list, 1));
889
890   return delay_list;
891 }   
892 \f
893 /* Delete INSN from the the delay slot of the insn that it is in.  This may
894    produce an insn without anything in its delay slots.  */
895
896 static void
897 delete_from_delay_slot (insn)
898      rtx insn;
899 {
900   rtx trial, seq_insn, seq, prev;
901   rtx delay_list = 0;
902   int i;
903
904   /* We first must find the insn containing the SEQUENCE with INSN in its
905      delay slot.  Do this by finding an insn, TRIAL, where
906      PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL.  */
907
908   for (trial = insn;
909        PREV_INSN (NEXT_INSN (trial)) == trial;
910        trial = NEXT_INSN (trial))
911     ;
912
913   seq_insn = PREV_INSN (NEXT_INSN (trial));
914   seq = PATTERN (seq_insn);
915
916   /* Create a delay list consisting of all the insns other than the one
917      we are deleting (unless we were the only one).  */
918   if (XVECLEN (seq, 0) > 2)
919     for (i = 1; i < XVECLEN (seq, 0); i++)
920       if (XVECEXP (seq, 0, i) != insn)
921         delay_list = add_to_delay_list (XVECEXP (seq, 0, i), delay_list);
922
923   /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
924      list, and rebuild the delay list if non-empty.  */
925   prev = PREV_INSN (seq_insn);
926   trial = XVECEXP (seq, 0, 0);
927   delete_insn (seq_insn);
928   add_insn_after (trial, prev);
929
930   if (GET_CODE (trial) == JUMP_INSN
931       && (simplejump_p (trial) || GET_CODE (PATTERN (trial)) == RETURN))
932     emit_barrier_after (trial);
933
934   /* If there are any delay insns, remit them.  Otherwise clear the
935      annul flag.  */
936   if (delay_list)
937     trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2, 0);
938   else
939     INSN_ANNULLED_BRANCH_P (trial) = 0;
940
941   INSN_FROM_TARGET_P (insn) = 0;
942
943   /* Show we need to fill this insn again.  */
944   obstack_ptr_grow (&unfilled_slots_obstack, trial);
945 }
946 \f
947 /* Delete INSN, a JUMP_INSN.  If it is a conditional jump, we must track down
948    the insn that sets CC0 for it and delete it too.  */
949
950 static void
951 delete_scheduled_jump (insn)
952      rtx insn;
953 {
954   /* Delete the insn that sets cc0 for us.  On machines without cc0, we could
955      delete the insn that sets the condition code, but it is hard to find it.
956      Since this case is rare anyway, don't bother trying; there would likely
957      be other insns that became dead anyway, which we wouldn't know to
958      delete.  */
959
960 #ifdef HAVE_cc0
961   if (reg_mentioned_p (cc0_rtx, insn))
962     {
963       rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
964
965       /* If a reg-note was found, it points to an insn to set CC0.  This
966          insn is in the delay list of some other insn.  So delete it from
967          the delay list it was in.  */
968       if (note)
969         {
970           if (! FIND_REG_INC_NOTE (XEXP (note, 0), NULL_RTX)
971               && sets_cc0_p (PATTERN (XEXP (note, 0))) == 1)
972             delete_from_delay_slot (XEXP (note, 0));
973         }
974       else
975         {
976           /* The insn setting CC0 is our previous insn, but it may be in
977              a delay slot.  It will be the last insn in the delay slot, if
978              it is.  */
979           rtx trial = previous_insn (insn);
980           if (GET_CODE (trial) == NOTE)
981             trial = prev_nonnote_insn (trial);
982           if (sets_cc0_p (PATTERN (trial)) != 1
983               || FIND_REG_INC_NOTE (trial, 0))
984             return;
985           if (PREV_INSN (NEXT_INSN (trial)) == trial)
986             delete_insn (trial);
987           else
988             delete_from_delay_slot (trial);
989         }
990     }
991 #endif
992
993   delete_insn (insn);
994 }
995 \f
996 /* Counters for delay-slot filling.  */
997
998 #define NUM_REORG_FUNCTIONS 2
999 #define MAX_DELAY_HISTOGRAM 3
1000 #define MAX_REORG_PASSES 2
1001
1002 static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
1003
1004 static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
1005
1006 static int reorg_pass_number;
1007
1008 static void
1009 note_delay_statistics (slots_filled, index)
1010      int slots_filled, index;
1011 {
1012   num_insns_needing_delays[index][reorg_pass_number]++;
1013   if (slots_filled > MAX_DELAY_HISTOGRAM)
1014     slots_filled = MAX_DELAY_HISTOGRAM;
1015   num_filled_delays[index][slots_filled][reorg_pass_number]++;
1016 }
1017 \f
1018 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
1019
1020 /* Optimize the following cases:
1021
1022    1.  When a conditional branch skips over only one instruction,
1023        use an annulling branch and put that insn in the delay slot.
1024        Use either a branch that annuls when the condition if true or
1025        invert the test with a branch that annuls when the condition is
1026        false.  This saves insns, since otherwise we must copy an insn
1027        from the L1 target.
1028
1029         (orig)           (skip)         (otherwise)
1030         Bcc.n L1        Bcc',a L1       Bcc,a L1'
1031         insn            insn            insn2
1032       L1:             L1:             L1:
1033         insn2           insn2           insn2
1034         insn3           insn3         L1':
1035                                         insn3
1036
1037    2.  When a conditional branch skips over only one instruction,
1038        and after that, it unconditionally branches somewhere else,
1039        perform the similar optimization. This saves executing the
1040        second branch in the case where the inverted condition is true.
1041
1042         Bcc.n L1        Bcc',a L2
1043         insn            insn
1044       L1:             L1:
1045         Bra L2          Bra L2
1046
1047    INSN is a JUMP_INSN.
1048
1049    This should be expanded to skip over N insns, where N is the number
1050    of delay slots required.  */
1051
1052 static rtx
1053 optimize_skip (insn)
1054      register rtx insn;
1055 {
1056   register rtx trial = next_nonnote_insn (insn);
1057   rtx next_trial = next_active_insn (trial);
1058   rtx delay_list = 0;
1059   rtx target_label;
1060   int flags;
1061
1062   flags = get_jump_flags (insn, JUMP_LABEL (insn));
1063
1064   if (trial == 0
1065       || GET_CODE (trial) != INSN
1066       || GET_CODE (PATTERN (trial)) == SEQUENCE
1067       || recog_memoized (trial) < 0
1068       || (! eligible_for_annul_false (insn, 0, trial, flags)
1069           && ! eligible_for_annul_true (insn, 0, trial, flags)))
1070     return 0;
1071
1072   /* There are two cases where we are just executing one insn (we assume
1073      here that a branch requires only one insn; this should be generalized
1074      at some point):  Where the branch goes around a single insn or where
1075      we have one insn followed by a branch to the same label we branch to.
1076      In both of these cases, inverting the jump and annulling the delay
1077      slot give the same effect in fewer insns.  */
1078   if ((next_trial == next_active_insn (JUMP_LABEL (insn)))
1079       || (next_trial != 0
1080           && GET_CODE (next_trial) == JUMP_INSN
1081           && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)
1082           && (simplejump_p (next_trial)
1083               || GET_CODE (PATTERN (next_trial)) == RETURN)))
1084     {
1085       if (eligible_for_annul_false (insn, 0, trial, flags))
1086         {
1087           if (invert_jump (insn, JUMP_LABEL (insn)))
1088             INSN_FROM_TARGET_P (trial) = 1;
1089           else if (! eligible_for_annul_true (insn, 0, trial, flags))
1090             return 0;
1091         }
1092
1093       delay_list = add_to_delay_list (trial, NULL_RTX);
1094       next_trial = next_active_insn (trial);
1095       update_block (trial, trial);
1096       delete_insn (trial);
1097
1098       /* Also, if we are targeting an unconditional
1099          branch, thread our jump to the target of that branch.  Don't
1100          change this into a RETURN here, because it may not accept what
1101          we have in the delay slot.  We'll fix this up later.  */
1102       if (next_trial && GET_CODE (next_trial) == JUMP_INSN
1103           && (simplejump_p (next_trial)
1104               || GET_CODE (PATTERN (next_trial)) == RETURN))
1105         {
1106           target_label = JUMP_LABEL (next_trial);
1107           if (target_label == 0)
1108             target_label = find_end_label ();
1109           redirect_jump (insn, target_label);
1110         }
1111
1112       INSN_ANNULLED_BRANCH_P (insn) = 1;
1113     }
1114
1115   return delay_list;
1116 }
1117 #endif
1118 \f
1119
1120 /*  Encode and return branch direction and prediction information for
1121     INSN assuming it will jump to LABEL.
1122
1123     Non conditional branches return no direction information and
1124     are predicted as very likely taken.  */
1125 static int
1126 get_jump_flags (insn, label)
1127      rtx insn, label;
1128 {
1129   int flags;
1130
1131   /* get_jump_flags can be passed any insn with delay slots, these may
1132      be INSNs, CALL_INSNs, or JUMP_INSNs.  Only JUMP_INSNs have branch
1133      direction information, and only if they are conditional jumps.
1134
1135      If LABEL is zero, then there is no way to determine the branch
1136      direction.  */
1137   if (GET_CODE (insn) == JUMP_INSN
1138       && condjump_p (insn)
1139       && INSN_UID (insn) <= max_uid
1140       && label != 0
1141       && INSN_UID (label) <= max_uid)
1142     flags 
1143       = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
1144          ? ATTR_FLAG_forward : ATTR_FLAG_backward;
1145   /* No valid direction information.  */
1146   else
1147     flags = 0;
1148   
1149   /* If insn is a conditional branch call mostly_true_jump to get
1150      determine the branch prediction.  
1151
1152      Non conditional branches are predicted as very likely taken.  */
1153   if (GET_CODE (insn) == JUMP_INSN
1154       && condjump_p (insn))
1155     {
1156       int prediction;
1157
1158       prediction = mostly_true_jump (insn, get_branch_condition (insn, label));
1159       switch (prediction)
1160         {
1161           case 2:
1162             flags |= (ATTR_FLAG_very_likely | ATTR_FLAG_likely);
1163             break;
1164           case 1:
1165             flags |= ATTR_FLAG_likely;
1166             break;
1167           case 0:
1168             flags |= ATTR_FLAG_unlikely;
1169             break;
1170           case -1:
1171             flags |= (ATTR_FLAG_very_unlikely | ATTR_FLAG_unlikely);
1172             break;
1173
1174           default:
1175             abort();
1176         }
1177     }
1178   else
1179     flags |= (ATTR_FLAG_very_likely | ATTR_FLAG_likely);
1180
1181   return flags;
1182 }
1183
1184 /* Return 1 if DEST is a destination that will be branched to rarely (the
1185    return point of a function); return 2 if DEST will be branched to very
1186    rarely (a call to a function that doesn't return).  Otherwise,
1187    return 0.  */
1188
1189 static int
1190 rare_destination (insn)
1191      rtx insn;
1192 {
1193   int jump_count = 0;
1194
1195   for (; insn; insn = NEXT_INSN (insn))
1196     {
1197       if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
1198         insn = XVECEXP (PATTERN (insn), 0, 0);
1199
1200       switch (GET_CODE (insn))
1201         {
1202         case CODE_LABEL:
1203           return 0;
1204         case BARRIER:
1205           /* A BARRIER can either be after a JUMP_INSN or a CALL_INSN.  We 
1206              don't scan past JUMP_INSNs, so any barrier we find here must
1207              have been after a CALL_INSN and hence mean the call doesn't
1208              return.  */
1209           return 2;
1210         case JUMP_INSN:
1211           if (GET_CODE (PATTERN (insn)) == RETURN)
1212             return 1;
1213           else if (simplejump_p (insn)
1214                    && jump_count++ < 10)
1215             insn = JUMP_LABEL (insn);
1216           else
1217             return 0;
1218         }
1219     }
1220
1221   /* If we got here it means we hit the end of the function.  So this
1222      is an unlikely destination.  */
1223
1224   return 1;
1225 }
1226
1227 /* Return truth value of the statement that this branch
1228    is mostly taken.  If we think that the branch is extremely likely
1229    to be taken, we return 2.  If the branch is slightly more likely to be
1230    taken, return 1.  If the branch is slightly less likely to be taken,
1231    return 0 and if the branch is highly unlikely to be taken, return -1.
1232
1233    CONDITION, if non-zero, is the condition that JUMP_INSN is testing.  */
1234
1235 static int
1236 mostly_true_jump (jump_insn, condition)
1237      rtx jump_insn, condition;
1238 {
1239   rtx target_label = JUMP_LABEL (jump_insn);
1240   rtx insn;
1241   int rare_dest = rare_destination (target_label);
1242   int rare_fallthrough = rare_destination (NEXT_INSN (jump_insn));
1243
1244   /* If this is a branch outside a loop, it is highly unlikely.  */
1245   if (GET_CODE (PATTERN (jump_insn)) == SET
1246       && GET_CODE (SET_SRC (PATTERN (jump_insn))) == IF_THEN_ELSE
1247       && ((GET_CODE (XEXP (SET_SRC (PATTERN (jump_insn)), 1)) == LABEL_REF
1248            && LABEL_OUTSIDE_LOOP_P (XEXP (SET_SRC (PATTERN (jump_insn)), 1)))
1249           || (GET_CODE (XEXP (SET_SRC (PATTERN (jump_insn)), 2)) == LABEL_REF
1250               && LABEL_OUTSIDE_LOOP_P (XEXP (SET_SRC (PATTERN (jump_insn)), 2)))))
1251     return -1;
1252
1253   if (target_label)
1254     {
1255       /* If this is the test of a loop, it is very likely true.  We scan
1256          backwards from the target label.  If we find a NOTE_INSN_LOOP_BEG
1257          before the next real insn, we assume the branch is to the top of 
1258          the loop.  */
1259       for (insn = PREV_INSN (target_label);
1260            insn && GET_CODE (insn) == NOTE;
1261            insn = PREV_INSN (insn))
1262         if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1263           return 2;
1264
1265       /* If this is a jump to the test of a loop, it is likely true.  We scan
1266          forwards from the target label.  If we find a NOTE_INSN_LOOP_VTOP
1267          before the next real insn, we assume the branch is to the loop branch
1268          test.  */
1269       for (insn = NEXT_INSN (target_label);
1270            insn && GET_CODE (insn) == NOTE;
1271            insn = PREV_INSN (insn))
1272         if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP)
1273           return 1;
1274     }
1275
1276   /* Look at the relative rarities of the fallthough and destination.  If
1277      they differ, we can predict the branch that way. */
1278
1279   switch (rare_fallthrough - rare_dest)
1280     {
1281     case -2:
1282       return -1;
1283     case -1:
1284       return 0;
1285     case 0:
1286       break;
1287     case 1:
1288       return 1;
1289     case 2:
1290       return 2;
1291     }
1292
1293   /* If we couldn't figure out what this jump was, assume it won't be 
1294      taken.  This should be rare.  */
1295   if (condition == 0)
1296     return 0;
1297
1298   /* EQ tests are usually false and NE tests are usually true.  Also,
1299      most quantities are positive, so we can make the appropriate guesses
1300      about signed comparisons against zero.  */
1301   switch (GET_CODE (condition))
1302     {
1303     case CONST_INT:
1304       /* Unconditional branch.  */
1305       return 1;
1306     case EQ:
1307       return 0;
1308     case NE:
1309       return 1;
1310     case LE:
1311     case LT:
1312       if (XEXP (condition, 1) == const0_rtx)
1313         return 0;
1314       break;
1315     case GE:
1316     case GT:
1317       if (XEXP (condition, 1) == const0_rtx)
1318         return 1;
1319       break;
1320     }
1321
1322   /* Predict backward branches usually take, forward branches usually not.  If
1323      we don't know whether this is forward or backward, assume the branch
1324      will be taken, since most are.  */
1325   return (INSN_UID (jump_insn) > max_uid || INSN_UID (target_label) > max_uid
1326           || (uid_to_ruid[INSN_UID (jump_insn)]
1327               > uid_to_ruid[INSN_UID (target_label)]));;
1328 }
1329
1330 /* Return the condition under which INSN will branch to TARGET.  If TARGET
1331    is zero, return the condition under which INSN will return.  If INSN is
1332    an unconditional branch, return const_true_rtx.  If INSN isn't a simple
1333    type of jump, or it doesn't go to TARGET, return 0.  */
1334
1335 static rtx
1336 get_branch_condition (insn, target)
1337      rtx insn;
1338      rtx target;
1339 {
1340   rtx pat = PATTERN (insn);
1341   rtx src;
1342   
1343   if (GET_CODE (pat) == RETURN)
1344     return target == 0 ? const_true_rtx : 0;
1345
1346   else if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
1347     return 0;
1348
1349   src = SET_SRC (pat);
1350   if (GET_CODE (src) == LABEL_REF && XEXP (src, 0) == target)
1351     return const_true_rtx;
1352
1353   else if (GET_CODE (src) == IF_THEN_ELSE
1354            && ((target == 0 && GET_CODE (XEXP (src, 1)) == RETURN)
1355                || (GET_CODE (XEXP (src, 1)) == LABEL_REF
1356                    && XEXP (XEXP (src, 1), 0) == target))
1357            && XEXP (src, 2) == pc_rtx)
1358     return XEXP (src, 0);
1359
1360   else if (GET_CODE (src) == IF_THEN_ELSE
1361            && ((target == 0 && GET_CODE (XEXP (src, 2)) == RETURN)
1362                || (GET_CODE (XEXP (src, 2)) == LABEL_REF
1363                    && XEXP (XEXP (src, 2), 0) == target))
1364            && XEXP (src, 1) == pc_rtx)
1365     return gen_rtx (reverse_condition (GET_CODE (XEXP (src, 0))),
1366                     GET_MODE (XEXP (src, 0)),
1367                     XEXP (XEXP (src, 0), 0), XEXP (XEXP (src, 0), 1));
1368
1369   return 0;
1370 }
1371
1372 /* Return non-zero if CONDITION is more strict than the condition of
1373    INSN, i.e., if INSN will always branch if CONDITION is true.  */
1374
1375 static int
1376 condition_dominates_p (condition, insn)
1377      rtx condition;
1378      rtx insn;
1379 {
1380   rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
1381   enum rtx_code code = GET_CODE (condition);
1382   enum rtx_code other_code;
1383
1384   if (rtx_equal_p (condition, other_condition)
1385       || other_condition == const_true_rtx)
1386     return 1;
1387
1388   else if (condition == const_true_rtx || other_condition == 0)
1389     return 0;
1390
1391   other_code = GET_CODE (other_condition);
1392   if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
1393       || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
1394       || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
1395     return 0;
1396
1397   return comparison_dominates_p (code, other_code);
1398 }
1399 \f
1400 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE.  Given that
1401    the condition tested by INSN is CONDITION and the resources shown in
1402    OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1403    from SEQ's delay list, in addition to whatever insns it may execute
1404    (in DELAY_LIST).   SETS and NEEDED are denote resources already set and
1405    needed while searching for delay slot insns.  Return the concatenated
1406    delay list if possible, otherwise, return 0.
1407
1408    SLOTS_TO_FILL is the total number of slots required by INSN, and
1409    PSLOTS_FILLED points to the number filled so far (also the number of
1410    insns in DELAY_LIST).  It is updated with the number that have been
1411    filled from the SEQUENCE, if any.
1412
1413    PANNUL_P points to a non-zero value if we already know that we need
1414    to annul INSN.  If this routine determines that annulling is needed,
1415    it may set that value non-zero.
1416
1417    PNEW_THREAD points to a location that is to receive the place at which
1418    execution should continue.  */
1419
1420 static rtx
1421 steal_delay_list_from_target (insn, condition, seq, delay_list,
1422                               sets, needed, other_needed,
1423                               slots_to_fill, pslots_filled, pannul_p,
1424                               pnew_thread)
1425      rtx insn, condition;
1426      rtx seq;
1427      rtx delay_list;
1428      struct resources *sets, *needed, *other_needed;
1429      int slots_to_fill;
1430      int *pslots_filled;
1431      int *pannul_p;
1432      rtx *pnew_thread;
1433 {
1434   rtx temp;
1435   int slots_remaining = slots_to_fill - *pslots_filled;
1436   int total_slots_filled = *pslots_filled;
1437   rtx new_delay_list = 0;
1438   int must_annul = *pannul_p;
1439   int i;
1440
1441   /* We can't do anything if there are more delay slots in SEQ than we
1442      can handle, or if we don't know that it will be a taken branch.
1443
1444      We know that it will be a taken branch if it is either an unconditional
1445      branch or a conditional branch with a stricter branch condition.  */
1446
1447   if (XVECLEN (seq, 0) - 1 > slots_remaining
1448       || ! condition_dominates_p (condition, XVECEXP (seq, 0, 0)))
1449     return delay_list;
1450
1451   for (i = 1; i < XVECLEN (seq, 0); i++)
1452     {
1453       rtx trial = XVECEXP (seq, 0, i);
1454       int flags;
1455
1456       if (insn_references_resource_p (trial, sets, 0)
1457           || insn_sets_resource_p (trial, needed, 0)
1458           || insn_sets_resource_p (trial, sets, 0)
1459 #ifdef HAVE_cc0
1460           /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1461              delay list.  */
1462           || find_reg_note (trial, REG_CC_USER, NULL_RTX)
1463 #endif
1464           /* If TRIAL is from the fallthrough code of an annulled branch insn
1465              in SEQ, we cannot use it.  */
1466           || (INSN_ANNULLED_BRANCH_P (XVECEXP (seq, 0, 0))
1467               && ! INSN_FROM_TARGET_P (trial)))
1468         return delay_list;
1469
1470       /* If this insn was already done (usually in a previous delay slot),
1471          pretend we put it in our delay slot.  */
1472       if (redundant_insn_p (trial, insn, new_delay_list))
1473         continue;
1474
1475       /* We will end up re-vectoring this branch, so compute flags
1476          based on jumping to the new label.  */
1477       flags = get_jump_flags (insn, JUMP_LABEL (XVECEXP (seq, 0, 0)));
1478
1479       if (! must_annul
1480           && ((condition == const_true_rtx
1481                || (! insn_sets_resource_p (trial, other_needed, 0)
1482                    && ! may_trap_p (PATTERN (trial)))))
1483           ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1484           : (must_annul = 1,
1485              eligible_for_annul_false (insn, total_slots_filled, trial, flags)))
1486         {
1487           temp = copy_rtx (trial);
1488           INSN_FROM_TARGET_P (temp) = 1;
1489           new_delay_list = add_to_delay_list (temp, new_delay_list);
1490           total_slots_filled++;
1491
1492           if (--slots_remaining == 0)
1493             break;
1494         }
1495       else
1496         return delay_list;
1497     }
1498
1499   /* Show the place to which we will be branching.  */
1500   *pnew_thread = next_active_insn (JUMP_LABEL (XVECEXP (seq, 0, 0)));
1501
1502   /* Add any new insns to the delay list and update the count of the
1503      number of slots filled.  */
1504   *pslots_filled = total_slots_filled;
1505   *pannul_p = must_annul;
1506
1507   if (delay_list == 0)
1508     return new_delay_list;
1509
1510   for (temp = new_delay_list; temp; temp = XEXP (temp, 1))
1511     delay_list = add_to_delay_list (XEXP (temp, 0), delay_list);
1512
1513   return delay_list;
1514 }
1515 \f
1516 /* Similar to steal_delay_list_from_target except that SEQ is on the 
1517    fallthrough path of INSN.  Here we only do something if the delay insn
1518    of SEQ is an unconditional branch.  In that case we steal its delay slot
1519    for INSN since unconditional branches are much easier to fill.  */
1520
1521 static rtx
1522 steal_delay_list_from_fallthrough (insn, condition, seq, 
1523                                    delay_list, sets, needed, other_needed,
1524                                    slots_to_fill, pslots_filled, pannul_p)
1525      rtx insn, condition;
1526      rtx seq;
1527      rtx delay_list;
1528      struct resources *sets, *needed, *other_needed;
1529      int slots_to_fill;
1530      int *pslots_filled;
1531      int *pannul_p;
1532 {
1533   int i;
1534   int flags;
1535
1536   flags = get_jump_flags (insn, JUMP_LABEL (insn));
1537
1538   /* We can't do anything if SEQ's delay insn isn't an
1539      unconditional branch.  */
1540
1541   if (! simplejump_p (XVECEXP (seq, 0, 0))
1542       && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) != RETURN)
1543     return delay_list;
1544
1545   for (i = 1; i < XVECLEN (seq, 0); i++)
1546     {
1547       rtx trial = XVECEXP (seq, 0, i);
1548
1549       /* If TRIAL sets CC0, stealing it will move it too far from the use
1550          of CC0.  */
1551       if (insn_references_resource_p (trial, sets, 0)
1552           || insn_sets_resource_p (trial, needed, 0)
1553           || insn_sets_resource_p (trial, sets, 0)
1554 #ifdef HAVE_cc0
1555           || sets_cc0_p (PATTERN (trial))
1556 #endif
1557           )
1558
1559         break;
1560
1561       /* If this insn was already done, we don't need it.  */
1562       if (redundant_insn_p (trial, insn, delay_list))
1563         {
1564           delete_from_delay_slot (trial);
1565           continue;
1566         }
1567
1568       if (! *pannul_p
1569           && ((condition == const_true_rtx
1570                || (! insn_sets_resource_p (trial, other_needed, 0)
1571                    && ! may_trap_p (PATTERN (trial)))))
1572           ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1573           : (*pannul_p = 1,
1574              eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1575         {
1576           delete_from_delay_slot (trial);
1577           delay_list = add_to_delay_list (trial, delay_list);
1578
1579           if (++(*pslots_filled) == slots_to_fill)
1580             break;
1581         }
1582       else
1583         break;
1584     }
1585
1586   return delay_list;
1587 }
1588 \f
1589 /* Try merging insns starting at THREAD which match exactly the insns in
1590    INSN's delay list.
1591
1592    If all insns were matched and the insn was previously annulling, the
1593    annul bit will be cleared.
1594
1595    For each insn that is merged, if the branch is or will be non-annulling,
1596    we delete the merged insn.  */
1597
1598 static void
1599 try_merge_delay_insns (insn, thread)
1600      rtx insn, thread;
1601 {
1602   rtx trial, next_trial;
1603   rtx delay_insn = XVECEXP (PATTERN (insn), 0, 0);
1604   int annul_p = INSN_ANNULLED_BRANCH_P (delay_insn);
1605   int slot_number = 1;
1606   int num_slots = XVECLEN (PATTERN (insn), 0);
1607   rtx next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1608   struct resources set, needed;
1609   rtx merged_insns = 0;
1610   int i;
1611   int flags;
1612
1613   flags = get_jump_flags (insn, JUMP_LABEL (insn));
1614
1615   CLEAR_RESOURCE (&needed);
1616   CLEAR_RESOURCE (&set);
1617
1618   /* If this is not an annulling branch, take into account anything needed in
1619      NEXT_TO_MATCH.  This prevents two increments from being incorrectly
1620      folded into one.  If we are annulling, this would be the correct
1621      thing to do.  (The alternative, looking at things set in NEXT_TO_MATCH
1622      will essentially disable this optimization.  This method is somewhat of
1623      a kludge, but I don't see a better way.)  */
1624   if (! annul_p)
1625     mark_referenced_resources (next_to_match, &needed, 1);
1626
1627   for (trial = thread; !stop_search_p (trial, 1); trial = next_trial)
1628     {
1629       rtx pat = PATTERN (trial);
1630
1631       next_trial = next_nonnote_insn (trial);
1632
1633       /* TRIAL must be a CALL_INSN or INSN.  Skip USE and CLOBBER.  */
1634       if (GET_CODE (trial) == INSN
1635           && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1636         continue;
1637
1638       if (GET_CODE (next_to_match) == GET_CODE (trial)
1639 #ifdef HAVE_cc0
1640           /* We can't share an insn that sets cc0.  */
1641           && ! sets_cc0_p (pat)
1642 #endif
1643           && ! insn_references_resource_p (trial, &set, 1)
1644           && ! insn_sets_resource_p (trial, &set, 1)
1645           && ! insn_sets_resource_p (trial, &needed, 1)
1646           && (trial = try_split (pat, trial, 0)) != 0
1647           && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1648           /* Have to test this condition if annul condition is different
1649              from (and less restrictive than) non-annulling one.  */
1650           && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1651         {
1652           next_trial = next_nonnote_insn (trial);
1653
1654           if (! annul_p)
1655             {
1656               update_block (trial, thread);
1657               delete_insn (trial);
1658               INSN_FROM_TARGET_P (next_to_match) = 0;
1659             }
1660           else
1661             merged_insns = gen_rtx (INSN_LIST, VOIDmode, trial, merged_insns);
1662
1663           if (++slot_number == num_slots)
1664             break;
1665
1666           next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1667           if (! annul_p)
1668             mark_referenced_resources (next_to_match, &needed, 1);
1669         }
1670
1671       mark_set_resources (trial, &set, 0, 1);
1672       mark_referenced_resources (trial, &needed, 1);
1673     }
1674
1675   /* See if we stopped on a filled insn.  If we did, try to see if its
1676      delay slots match.  */
1677   if (slot_number != num_slots
1678       && trial && GET_CODE (trial) == INSN
1679       && GET_CODE (PATTERN (trial)) == SEQUENCE
1680       && ! INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial), 0, 0)))
1681     {
1682       rtx pat = PATTERN (trial);
1683
1684       for (i = 1; i < XVECLEN (pat, 0); i++)
1685         {
1686           rtx dtrial = XVECEXP (pat, 0, i);
1687
1688           if (! insn_references_resource_p (dtrial, &set, 1)
1689               && ! insn_sets_resource_p (dtrial, &set, 1)
1690               && ! insn_sets_resource_p (dtrial, &needed, 1)
1691 #ifdef HAVE_cc0
1692               && ! sets_cc0_p (PATTERN (dtrial))
1693 #endif
1694               && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1695               && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1696             {
1697               if (! annul_p)
1698                 {
1699                   update_block (dtrial, thread);
1700                   delete_from_delay_slot (dtrial);
1701                   INSN_FROM_TARGET_P (next_to_match) = 0;
1702                 }
1703               else
1704                 merged_insns = gen_rtx (INSN_LIST, SImode, dtrial,
1705                                         merged_insns);
1706
1707               if (++slot_number == num_slots)
1708                 break;
1709
1710               next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1711             }
1712         }
1713     }
1714
1715   /* If all insns in the delay slot have been matched and we were previously
1716      annulling the branch, we need not any more.  In that case delete all the
1717      merged insns.  Also clear the INSN_FROM_TARGET_P bit of each insn the
1718      the delay list so that we know that it isn't only being used at the
1719      target.  */
1720   if (next_to_match == 0 && annul_p)
1721     {
1722       for (; merged_insns; merged_insns = XEXP (merged_insns, 1))
1723         {
1724           if (GET_MODE (merged_insns) == SImode)
1725             {
1726               update_block (XEXP (merged_insns, 0), thread);
1727               delete_from_delay_slot (XEXP (merged_insns, 0));
1728             }
1729           else
1730             {
1731               update_block (XEXP (merged_insns, 0), thread);
1732               delete_insn (XEXP (merged_insns, 0));
1733             }
1734         }
1735
1736       INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1737
1738       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1739         INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i)) = 0;
1740     }
1741 }
1742 \f
1743 /* See if INSN is redundant with an insn in front of TARGET.  Often this
1744    is called when INSN is a candidate for a delay slot of TARGET.
1745    DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1746    of INSN.  Often INSN will be redundant with an insn in a delay slot of
1747    some previous insn.  This happens when we have a series of branches to the
1748    same label; in that case the first insn at the target might want to go
1749    into each of the delay slots.
1750
1751    If we are not careful, this routine can take up a significant fraction
1752    of the total compilation time (4%), but only wins rarely.  Hence we
1753    speed this routine up by making two passes.  The first pass goes back
1754    until it hits a label and sees if it find an insn with an identical
1755    pattern.  Only in this (relatively rare) event does it check for
1756    data conflicts.
1757
1758    We do not split insns we encounter.  This could cause us not to find a
1759    redundant insn, but the cost of splitting seems greater than the possible
1760    gain in rare cases.  */
1761
1762 static int
1763 redundant_insn_p (insn, target, delay_list)
1764      rtx insn;
1765      rtx target;
1766      rtx delay_list;
1767 {
1768   rtx target_main = target;
1769   rtx ipat = PATTERN (insn);
1770   rtx trial, pat;
1771   struct resources needed, set;
1772   int i;
1773
1774   /* Scan backwards looking for a match.  */
1775   for (trial = PREV_INSN (target); trial; trial = PREV_INSN (trial))
1776     {
1777       if (GET_CODE (trial) == CODE_LABEL)
1778         return 0;
1779
1780       if (GET_RTX_CLASS (GET_CODE (trial)) != 'i')
1781         continue;
1782
1783       pat = PATTERN (trial);
1784       if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1785         continue;
1786
1787       if (GET_CODE (pat) == SEQUENCE)
1788         {
1789           /* Stop for a CALL and its delay slots because it is difficult to
1790              track its resource needs correctly.  */
1791           if (GET_CODE (XVECEXP (pat, 0, 0)) == CALL_INSN)
1792             return 0;
1793
1794           /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1795              slots because it is difficult to track its resource needs 
1796              correctly.  */
1797
1798 #ifdef INSN_SETS_ARE_DELAYED
1799           if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1800             return 0; 
1801 #endif
1802
1803 #ifdef INSN_REFERENCES_ARE_DELAYED
1804           if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1805             return 0; 
1806 #endif
1807
1808           /* See if any of the insns in the delay slot match, updating
1809              resource requirements as we go.  */
1810           for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1811             if (GET_CODE (XVECEXP (pat, 0, i)) == GET_CODE (insn)
1812                 && rtx_equal_p (PATTERN (XVECEXP (pat, 0, i)), ipat))
1813               break;
1814
1815           /* If found a match, exit this loop early.  */
1816           if (i > 0)
1817             break;
1818         }
1819
1820       else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat))
1821         break;
1822     }
1823
1824   /* If we didn't find an insn that matches, return 0.  */
1825   if (trial == 0)
1826     return 0;
1827
1828   /* See what resources this insn sets and needs.  If they overlap, or
1829      if this insn references CC0, it can't be redundant.  */
1830
1831   CLEAR_RESOURCE (&needed);
1832   CLEAR_RESOURCE (&set);
1833   mark_set_resources (insn, &set, 0, 1);
1834   mark_referenced_resources (insn, &needed, 1);
1835
1836   /* If TARGET is a SEQUENCE, get the main insn.  */
1837   if (GET_CODE (target) == INSN && GET_CODE (PATTERN (target)) == SEQUENCE)
1838     target_main = XVECEXP (PATTERN (target), 0, 0);
1839
1840   if (resource_conflicts_p (&needed, &set)
1841 #ifdef HAVE_cc0
1842       || reg_mentioned_p (cc0_rtx, ipat)
1843 #endif
1844       /* The insn requiring the delay may not set anything needed or set by
1845          INSN.  */
1846       || insn_sets_resource_p (target_main, &needed, 1)
1847       || insn_sets_resource_p (target_main, &set, 1))
1848     return 0;
1849
1850   /* Insns we pass may not set either NEEDED or SET, so merge them for
1851      simpler tests.  */
1852   needed.memory |= set.memory;
1853   IOR_HARD_REG_SET (needed.regs, set.regs);
1854
1855   /* This insn isn't redundant if it conflicts with an insn that either is
1856      or will be in a delay slot of TARGET.  */
1857
1858   while (delay_list)
1859     {
1860       if (insn_sets_resource_p (XEXP (delay_list, 0), &needed, 1))
1861         return 0;
1862       delay_list = XEXP (delay_list, 1);
1863     }
1864
1865   if (GET_CODE (target) == INSN && GET_CODE (PATTERN (target)) == SEQUENCE)
1866     for (i = 1; i < XVECLEN (PATTERN (target), 0); i++)
1867       if (insn_sets_resource_p (XVECEXP (PATTERN (target), 0, i), &needed, 1))
1868         return 0;
1869
1870   /* Scan backwards until we reach a label or an insn that uses something
1871      INSN sets or sets something insn uses or sets.  */
1872
1873   for (trial = PREV_INSN (target);
1874        trial && GET_CODE (trial) != CODE_LABEL;
1875        trial = PREV_INSN (trial))
1876     {
1877       if (GET_CODE (trial) != INSN && GET_CODE (trial) != CALL_INSN
1878           && GET_CODE (trial) != JUMP_INSN)
1879         continue;
1880
1881       pat = PATTERN (trial);
1882       if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1883         continue;
1884
1885       if (GET_CODE (pat) == SEQUENCE)
1886         {
1887           /* If this is a CALL_INSN and its delay slots, it is hard to track
1888              the resource needs properly, so give up.  */
1889           if (GET_CODE (XVECEXP (pat, 0, 0)) == CALL_INSN)
1890             return 0;
1891
1892           /* If this this is an INSN or JUMP_INSN with delayed effects, it
1893              is hard to track the resource needs properly, so give up.  */
1894
1895 #ifdef INSN_SETS_ARE_DELAYED
1896           if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1897             return 0; 
1898 #endif
1899
1900 #ifdef INSN_REFERENCES_ARE_DELAYED
1901           if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1902             return 0; 
1903 #endif
1904
1905           /* See if any of the insns in the delay slot match, updating
1906              resource requirements as we go.  */
1907           for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1908             {
1909               rtx candidate = XVECEXP (pat, 0, i);
1910
1911               /* If an insn will be annulled if the branch is false, it isn't
1912                  considered as a possible duplicate insn.  */
1913               if (rtx_equal_p (PATTERN (candidate), ipat)
1914                   && ! (INSN_ANNULLED_BRANCH_P (XVECEXP (pat, 0, 0))
1915                         && INSN_FROM_TARGET_P (candidate)))
1916                 {
1917                   /* Show that this insn will be used in the sequel.  */
1918                   INSN_FROM_TARGET_P (candidate) = 0;
1919                   return 1;
1920                 }
1921
1922               /* Unless this is an annulled insn from the target of a branch,
1923                  we must stop if it sets anything needed or set by INSN.  */
1924               if ((! INSN_ANNULLED_BRANCH_P (XVECEXP (pat, 0, 0))
1925                    || ! INSN_FROM_TARGET_P (candidate))
1926                   && insn_sets_resource_p (candidate, &needed, 1))
1927                 return 0;
1928             }
1929
1930
1931           /* If the insn requiring the delay slot conflicts with INSN, we 
1932              must stop.  */
1933           if (insn_sets_resource_p (XVECEXP (pat, 0, 0), &needed, 1))
1934             return 0;
1935         }
1936       else
1937         {
1938           /* See if TRIAL is the same as INSN.  */
1939           pat = PATTERN (trial);
1940           if (rtx_equal_p (pat, ipat))
1941             return 1;
1942
1943           /* Can't go any further if TRIAL conflicts with INSN.  */
1944           if (insn_sets_resource_p (trial, &needed, 1))
1945             return 0;
1946         }
1947     }
1948
1949   return 0;
1950 }
1951 \f
1952 /* Return 1 if THREAD can only be executed in one way.  If LABEL is non-zero,
1953    it is the target of the branch insn being scanned.  If ALLOW_FALLTHROUGH
1954    is non-zero, we are allowed to fall into this thread; otherwise, we are
1955    not.
1956
1957    If LABEL is used more than one or we pass a label other than LABEL before
1958    finding an active insn, we do not own this thread.  */
1959
1960 static int
1961 own_thread_p (thread, label, allow_fallthrough)
1962      rtx thread;
1963      rtx label;
1964      int allow_fallthrough;
1965 {
1966   rtx active_insn;
1967   rtx insn;
1968
1969   /* We don't own the function end.  */
1970   if (thread == 0)
1971     return 0;
1972
1973   /* Get the first active insn, or THREAD, if it is an active insn.  */
1974   active_insn = next_active_insn (PREV_INSN (thread));
1975
1976   for (insn = thread; insn != active_insn; insn = NEXT_INSN (insn))
1977     if (GET_CODE (insn) == CODE_LABEL
1978         && (insn != label || LABEL_NUSES (insn) != 1))
1979       return 0;
1980
1981   if (allow_fallthrough)
1982     return 1;
1983
1984   /* Ensure that we reach a BARRIER before any insn or label.  */
1985   for (insn = prev_nonnote_insn (thread);
1986        insn == 0 || GET_CODE (insn) != BARRIER;
1987        insn = prev_nonnote_insn (insn))
1988     if (insn == 0
1989         || GET_CODE (insn) == CODE_LABEL
1990         || (GET_CODE (insn) == INSN
1991             && GET_CODE (PATTERN (insn)) != USE
1992             && GET_CODE (PATTERN (insn)) != CLOBBER))
1993       return 0;
1994
1995   return 1;
1996 }
1997 \f
1998 /* Find the number of the basic block that starts closest to INSN.  Return -1
1999    if we couldn't find such a basic block.  */
2000
2001 static int
2002 find_basic_block (insn)
2003      rtx insn;
2004 {
2005   int i;
2006
2007   /* Scan backwards to the previous BARRIER.  Then see if we can find a
2008      label that starts a basic block.  Return the basic block number.  */
2009
2010   for (insn = prev_nonnote_insn (insn);
2011        insn && GET_CODE (insn) != BARRIER;
2012        insn = prev_nonnote_insn (insn))
2013     ;
2014
2015   /* The start of the function is basic block zero.  */
2016   if (insn == 0)
2017     return 0;
2018
2019   /* See if any of the upcoming CODE_LABELs start a basic block.  If we reach
2020      anything other than a CODE_LABEL or note, we can't find this code.  */
2021   for (insn = next_nonnote_insn (insn);
2022        insn && GET_CODE (insn) == CODE_LABEL;
2023        insn = next_nonnote_insn (insn))
2024     {
2025       for (i = 0; i < n_basic_blocks; i++)
2026         if (insn == basic_block_head[i])
2027           return i;
2028     }
2029
2030   return -1;
2031 }
2032 \f
2033 /* Called when INSN is being moved from a location near the target of a jump.
2034    We leave a marker of the form (use (INSN)) immediately in front
2035    of WHERE for mark_target_live_regs.  These markers will be deleted when
2036    reorg finishes.
2037
2038    We used to try to update the live status of registers if WHERE is at
2039    the start of a basic block, but that can't work since we may remove a
2040    BARRIER in relax_delay_slots.  */
2041
2042 static void
2043 update_block (insn, where)
2044      rtx insn;
2045      rtx where;
2046 {
2047   int b;
2048
2049   /* Ignore if this was in a delay slot and it came from the target of 
2050      a branch.  */
2051   if (INSN_FROM_TARGET_P (insn))
2052     return;
2053
2054   emit_insn_before (gen_rtx (USE, VOIDmode, insn), where);
2055
2056   /* INSN might be making a value live in a block where it didn't use to
2057      be.  So recompute liveness information for this block.  */
2058
2059   b = find_basic_block (insn);
2060   if (b != -1)
2061     bb_ticks[b]++;
2062 }
2063
2064 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
2065    We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
2066    that reference values used in INSN.  If we find one, then we move the
2067    REG_DEAD note to INSN.
2068
2069    This is needed to handle the case where an later insn (after INSN) has a
2070    REG_DEAD note for a register used by INSN, and this later insn subsequently
2071    gets moved before a CODE_LABEL because it is a redundant insn.  In this
2072    case, mark_target_live_regs may be confused into thinking the register
2073    is dead because it sees a REG_DEAD note immediately before a CODE_LABEL.  */
2074
2075 static void
2076 update_reg_dead_notes (insn, delayed_insn)
2077      rtx insn, delayed_insn;
2078 {
2079   rtx p, link, next;
2080
2081   for (p = next_nonnote_insn (insn); p != delayed_insn;
2082        p = next_nonnote_insn (p))
2083     for (link = REG_NOTES (p); link; link = next)
2084       {
2085         next = XEXP (link, 1);
2086
2087         if (REG_NOTE_KIND (link) != REG_DEAD
2088             || GET_CODE (XEXP (link, 0)) != REG)
2089           continue;
2090
2091         if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
2092           {
2093             /* Move the REG_DEAD note from P to INSN.  */
2094             remove_note (p, link);
2095             XEXP (link, 1) = REG_NOTES (insn);
2096             REG_NOTES (insn) = link;
2097           }
2098       }
2099 }
2100 \f
2101 /* Marks registers possibly live at the current place being scanned by
2102    mark_target_live_regs.  Used only by next two function.    */
2103
2104 static HARD_REG_SET current_live_regs;
2105
2106 /* Marks registers for which we have seen a REG_DEAD note but no assignment.
2107    Also only used by the next two functions.  */
2108
2109 static HARD_REG_SET pending_dead_regs;
2110
2111 /* Utility function called from mark_target_live_regs via note_stores.
2112    It deadens any CLOBBERed registers and livens any SET registers.  */
2113
2114 static void
2115 update_live_status (dest, x)
2116      rtx dest;
2117      rtx x;
2118 {
2119   int first_regno, last_regno;
2120   int i;
2121
2122   if (GET_CODE (dest) != REG
2123       && (GET_CODE (dest) != SUBREG || GET_CODE (SUBREG_REG (dest)) != REG))
2124     return;
2125
2126   if (GET_CODE (dest) == SUBREG)
2127     first_regno = REGNO (SUBREG_REG (dest)) + SUBREG_WORD (dest);
2128   else
2129     first_regno = REGNO (dest);
2130
2131   last_regno = first_regno + HARD_REGNO_NREGS (first_regno, GET_MODE (dest));
2132
2133   if (GET_CODE (x) == CLOBBER)
2134     for (i = first_regno; i < last_regno; i++)
2135       CLEAR_HARD_REG_BIT (current_live_regs, i);
2136   else
2137     for (i = first_regno; i < last_regno; i++)
2138       {
2139         SET_HARD_REG_BIT (current_live_regs, i);
2140         CLEAR_HARD_REG_BIT (pending_dead_regs, i);
2141       }
2142 }
2143
2144 /* Similar to next_insn, but ignores insns in the delay slots of
2145    an annulled branch.  */
2146
2147 static rtx
2148 next_insn_no_annul (insn)
2149      rtx insn;
2150 {
2151   if (insn)
2152     {
2153       /* If INSN is an annulled branch, skip any insns from the target
2154          of the branch.  */
2155       if (INSN_ANNULLED_BRANCH_P (insn)
2156           && NEXT_INSN (PREV_INSN (insn)) != insn)
2157         while (INSN_FROM_TARGET_P (NEXT_INSN (insn)))
2158           insn = NEXT_INSN (insn);
2159
2160       insn = NEXT_INSN (insn);
2161       if (insn && GET_CODE (insn) == INSN
2162           && GET_CODE (PATTERN (insn)) == SEQUENCE)
2163         insn = XVECEXP (PATTERN (insn), 0, 0);
2164     }
2165
2166   return insn;
2167 }
2168 \f
2169 /* Set the resources that are live at TARGET.
2170
2171    If TARGET is zero, we refer to the end of the current function and can
2172    return our precomputed value.
2173
2174    Otherwise, we try to find out what is live by consulting the basic block
2175    information.  This is tricky, because we must consider the actions of
2176    reload and jump optimization, which occur after the basic block information
2177    has been computed.
2178
2179    Accordingly, we proceed as follows::
2180
2181    We find the previous BARRIER and look at all immediately following labels
2182    (with no intervening active insns) to see if any of them start a basic
2183    block.  If we hit the start of the function first, we use block 0.
2184
2185    Once we have found a basic block and a corresponding first insns, we can
2186    accurately compute the live status from basic_block_live_regs and
2187    reg_renumber.  (By starting at a label following a BARRIER, we are immune
2188    to actions taken by reload and jump.)  Then we scan all insns between
2189    that point and our target.  For each CLOBBER (or for call-clobbered regs
2190    when we pass a CALL_INSN), mark the appropriate registers are dead.  For
2191    a SET, mark them as live.
2192
2193    We have to be careful when using REG_DEAD notes because they are not
2194    updated by such things as find_equiv_reg.  So keep track of registers
2195    marked as dead that haven't been assigned to, and mark them dead at the
2196    next CODE_LABEL since reload and jump won't propagate values across labels.
2197
2198    If we cannot find the start of a basic block (should be a very rare
2199    case, if it can happen at all), mark everything as potentially live.
2200
2201    Next, scan forward from TARGET looking for things set or clobbered
2202    before they are used.  These are not live.
2203
2204    Because we can be called many times on the same target, save our results
2205    in a hash table indexed by INSN_UID.  */
2206
2207 static void
2208 mark_target_live_regs (target, res)
2209      rtx target;
2210      struct resources *res;
2211 {
2212   int b = -1;
2213   int i;
2214   struct target_info *tinfo;
2215   rtx insn, next;
2216   rtx jump_insn = 0;
2217   rtx jump_target;
2218   HARD_REG_SET scratch;
2219   struct resources set, needed;
2220   int jump_count = 0;
2221
2222   /* Handle end of function.  */
2223   if (target == 0)
2224     {
2225       *res = end_of_function_needs;
2226       return;
2227     }
2228
2229   /* We have to assume memory is needed, but the CC isn't.  */
2230   res->memory = 1;
2231   res->volatil = 0;
2232   res->cc = 0;
2233
2234   /* See if we have computed this value already.  */
2235   for (tinfo = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
2236        tinfo; tinfo = tinfo->next)
2237     if (tinfo->uid == INSN_UID (target))
2238       break;
2239
2240   /* Start by getting the basic block number.  If we have saved information,
2241      we can get it from there unless the insn at the start of the basic block
2242      has been deleted.  */
2243   if (tinfo && tinfo->block != -1
2244       && ! INSN_DELETED_P (basic_block_head[tinfo->block]))
2245     b = tinfo->block;
2246
2247   if (b == -1)
2248     b = find_basic_block (target);
2249
2250   if (tinfo)
2251     {
2252       /* If the information is up-to-date, use it.  Otherwise, we will
2253          update it below.  */
2254       if (b == tinfo->block && b != -1 && tinfo->bb_tick == bb_ticks[b])
2255         {
2256           COPY_HARD_REG_SET (res->regs, tinfo->live_regs);
2257           return;
2258         }
2259     }
2260   else
2261     {
2262       /* Allocate a place to put our results and chain it into the 
2263          hash table.  */
2264       tinfo = (struct target_info *) oballoc (sizeof (struct target_info));
2265       tinfo->uid = INSN_UID (target);
2266       tinfo->block = b;
2267       tinfo->next = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
2268       target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME] = tinfo;
2269     }
2270
2271   CLEAR_HARD_REG_SET (pending_dead_regs);
2272
2273   /* If we found a basic block, get the live registers from it and update
2274      them with anything set or killed between its start and the insn before
2275      TARGET.  Otherwise, we must assume everything is live.  */
2276   if (b != -1)
2277     {
2278       regset regs_live = basic_block_live_at_start[b];
2279       int offset, j;
2280       REGSET_ELT_TYPE bit;
2281       int regno;
2282       rtx start_insn, stop_insn;
2283
2284       /* Compute hard regs live at start of block -- this is the real hard regs
2285          marked live, plus live pseudo regs that have been renumbered to
2286          hard regs.  */
2287
2288 #ifdef HARD_REG_SET
2289       current_live_regs = *regs_live;
2290 #else
2291       COPY_HARD_REG_SET (current_live_regs, regs_live);
2292 #endif
2293
2294       for (offset = 0, i = 0; offset < regset_size; offset++)
2295         {
2296           if (regs_live[offset] == 0)
2297             i += REGSET_ELT_BITS;
2298           else
2299             for (bit = 1; bit && i < max_regno; bit <<= 1, i++)
2300               if ((regs_live[offset] & bit)
2301                   && (regno = reg_renumber[i]) >= 0)
2302                 for (j = regno;
2303                      j < regno + HARD_REGNO_NREGS (regno,
2304                                                    PSEUDO_REGNO_MODE (i));
2305                      j++)
2306                   SET_HARD_REG_BIT (current_live_regs, j);
2307         }
2308
2309       /* Get starting and ending insn, handling the case where each might
2310          be a SEQUENCE.  */
2311       start_insn = (b == 0 ? get_insns () : basic_block_head[b]);
2312       stop_insn = target;
2313
2314       if (GET_CODE (start_insn) == INSN
2315           && GET_CODE (PATTERN (start_insn)) == SEQUENCE)
2316         start_insn = XVECEXP (PATTERN (start_insn), 0, 0);
2317
2318       if (GET_CODE (stop_insn) == INSN
2319           && GET_CODE (PATTERN (stop_insn)) == SEQUENCE)
2320         stop_insn = next_insn (PREV_INSN (stop_insn));
2321
2322       for (insn = start_insn; insn != stop_insn;
2323            insn = next_insn_no_annul (insn))
2324         {
2325           rtx link;
2326           rtx real_insn = insn;
2327
2328           /* If this insn is from the target of a branch, it isn't going to
2329              be used in the sequel.  If it is used in both cases, this
2330              test will not be true.  */
2331           if (INSN_FROM_TARGET_P (insn))
2332             continue;
2333
2334           /* If this insn is a USE made by update_block, we care about the
2335              underlying insn.  */
2336           if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE
2337               && GET_RTX_CLASS (GET_CODE (XEXP (PATTERN (insn), 0))) == 'i')
2338               real_insn = XEXP (PATTERN (insn), 0);
2339
2340           if (GET_CODE (real_insn) == CALL_INSN)
2341             {
2342               /* CALL clobbers all call-used regs that aren't fixed except
2343                  sp, ap, and fp.  Do this before setting the result of the
2344                  call live.  */
2345               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2346                 if (call_used_regs[i]
2347                     && i != STACK_POINTER_REGNUM && i != FRAME_POINTER_REGNUM
2348                     && i != ARG_POINTER_REGNUM
2349 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2350                     && ! (i == ARG_POINTER_REGNUM && fixed_regs[i])
2351 #endif
2352 #ifdef PIC_OFFSET_TABLE_REGNUM
2353                     && ! (i == PIC_OFFSET_TABLE_REGNUM && flag_pic)
2354 #endif
2355                     )
2356                   CLEAR_HARD_REG_BIT (current_live_regs, i);
2357
2358               /* A CALL_INSN sets any global register live, since it may
2359                  have been modified by the call.  */
2360               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2361                 if (global_regs[i])
2362                   SET_HARD_REG_BIT (current_live_regs, i);
2363             }
2364
2365           /* Mark anything killed in an insn to be deadened at the next
2366              label.  Ignore USE insns; the only REG_DEAD notes will be for
2367              parameters.  But they might be early.  A CALL_INSN will usually
2368              clobber registers used for parameters.  It isn't worth bothering
2369              with the unlikely case when it won't.  */
2370           if ((GET_CODE (real_insn) == INSN
2371                && GET_CODE (PATTERN (real_insn)) != USE)
2372               || GET_CODE (real_insn) == JUMP_INSN
2373               || GET_CODE (real_insn) == CALL_INSN)
2374             {
2375               for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
2376                 if (REG_NOTE_KIND (link) == REG_DEAD
2377                     && GET_CODE (XEXP (link, 0)) == REG
2378                     && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
2379                   {
2380                     int first_regno = REGNO (XEXP (link, 0));
2381                     int last_regno
2382                       = (first_regno
2383                          + HARD_REGNO_NREGS (first_regno,
2384                                              GET_MODE (XEXP (link, 0))));
2385                          
2386                     for (i = first_regno; i < last_regno; i++)
2387                       SET_HARD_REG_BIT (pending_dead_regs, i);
2388                   }
2389
2390               note_stores (PATTERN (real_insn), update_live_status);
2391
2392               /* If any registers were unused after this insn, kill them.
2393                  These notes will always be accurate.  */
2394               for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
2395                 if (REG_NOTE_KIND (link) == REG_UNUSED
2396                     && GET_CODE (XEXP (link, 0)) == REG
2397                     && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
2398                   {
2399                     int first_regno = REGNO (XEXP (link, 0));
2400                     int last_regno
2401                       = (first_regno
2402                          + HARD_REGNO_NREGS (first_regno,
2403                                              GET_MODE (XEXP (link, 0))));
2404                          
2405                     for (i = first_regno; i < last_regno; i++)
2406                       CLEAR_HARD_REG_BIT (current_live_regs, i);
2407                   }
2408             }
2409
2410           else if (GET_CODE (real_insn) == CODE_LABEL)
2411             {
2412               /* A label clobbers the pending dead registers since neither
2413                  reload nor jump will propagate a value across a label.  */
2414               AND_COMPL_HARD_REG_SET (current_live_regs, pending_dead_regs);
2415               CLEAR_HARD_REG_SET (pending_dead_regs);
2416             }
2417
2418           /* The beginning of the epilogue corresponds to the end of the
2419              RTL chain when there are no epilogue insns.  Certain resources
2420              are implicitly required at that point.  */
2421           else if (GET_CODE (real_insn) == NOTE
2422                    && NOTE_LINE_NUMBER (real_insn) == NOTE_INSN_EPILOGUE_BEG)
2423             IOR_HARD_REG_SET (current_live_regs, start_of_epilogue_needs.regs);
2424         }
2425
2426       COPY_HARD_REG_SET (res->regs, current_live_regs);
2427       tinfo->block = b;
2428       tinfo->bb_tick = bb_ticks[b];
2429     }
2430   else
2431     /* We didn't find the start of a basic block.  Assume everything
2432        in use.  This should happen only extremely rarely.  */
2433     SET_HARD_REG_SET (res->regs);
2434
2435   /* Now step forward from TARGET looking for registers that are set before
2436      they are used.  These are dead.  If we pass a label, any pending dead
2437      registers that weren't yet used can be made dead.  Stop when we pass a
2438      conditional JUMP_INSN; follow the first few unconditional branches.  */
2439
2440   CLEAR_RESOURCE (&set);
2441   CLEAR_RESOURCE (&needed);
2442
2443   for (insn = target; insn; insn = next)
2444     {
2445       rtx this_jump_insn = insn;
2446
2447       next = NEXT_INSN (insn);
2448       switch (GET_CODE (insn))
2449         {
2450         case CODE_LABEL:
2451           AND_COMPL_HARD_REG_SET (pending_dead_regs, needed.regs);
2452           AND_COMPL_HARD_REG_SET (res->regs, pending_dead_regs);
2453           CLEAR_HARD_REG_SET (pending_dead_regs);
2454           continue;
2455
2456         case BARRIER:
2457         case NOTE:
2458           continue;
2459
2460         case INSN:
2461           if (GET_CODE (PATTERN (insn)) == USE)
2462             {
2463               /* If INSN is a USE made by update_block, we care about the
2464                  underlying insn.  Any registers set by the underlying insn
2465                  are live since the insn is being done somewhere else.  */
2466               if (GET_RTX_CLASS (GET_CODE (XEXP (PATTERN (insn), 0))) == 'i')
2467                 mark_set_resources (XEXP (PATTERN (insn), 0), res, 0, 1);
2468
2469               /* All other USE insns are to be ignored.  */
2470               continue;
2471             }
2472           else if (GET_CODE (PATTERN (insn)) == CLOBBER)
2473             continue;
2474           else if (GET_CODE (PATTERN (insn)) == SEQUENCE)
2475             {
2476               /* An unconditional jump can be used to fill the delay slot
2477                  of a call, so search for a JUMP_INSN in any position.  */
2478               for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2479                 {
2480                   this_jump_insn = XVECEXP (PATTERN (insn), 0, i);
2481                   if (GET_CODE (this_jump_insn) == JUMP_INSN)
2482                     break;
2483                 }
2484             }
2485         }
2486
2487       if (GET_CODE (this_jump_insn) == JUMP_INSN)
2488         {
2489           if (jump_count++ < 10
2490               && (simplejump_p (this_jump_insn)
2491                   || GET_CODE (PATTERN (this_jump_insn)) == RETURN))
2492             {
2493               next = next_active_insn (JUMP_LABEL (this_jump_insn));
2494               if (jump_insn == 0)
2495                 {
2496                   jump_insn = insn;
2497                   jump_target = JUMP_LABEL (this_jump_insn);
2498                 }
2499             }
2500           else
2501             break;
2502         }
2503
2504       mark_referenced_resources (insn, &needed, 1);
2505       mark_set_resources (insn, &set, 0, 1);
2506
2507       COPY_HARD_REG_SET (scratch, set.regs);
2508       AND_COMPL_HARD_REG_SET (scratch, needed.regs);
2509       AND_COMPL_HARD_REG_SET (res->regs, scratch);
2510     }
2511
2512   /* If we hit an unconditional branch, we have another way of finding out
2513      what is live: we can see what is live at the branch target and include
2514      anything used but not set before the branch.  The only things that are
2515      live are those that are live using the above test and the test below.
2516
2517      Don't try this if we expired our jump count above, since that would
2518      mean there may be an infinite loop in the function being compiled.  */
2519
2520   if (jump_insn && jump_count < 10)
2521     {
2522       struct resources new_resources;
2523       rtx stop_insn = next_active_insn (jump_insn);
2524
2525       mark_target_live_regs (next_active_insn (jump_target), &new_resources);
2526       CLEAR_RESOURCE (&set);
2527       CLEAR_RESOURCE (&needed);
2528
2529       /* Include JUMP_INSN in the needed registers.  */
2530       for (insn = target; insn != stop_insn; insn = next_active_insn (insn))
2531         {
2532           mark_referenced_resources (insn, &needed, 1);
2533
2534           COPY_HARD_REG_SET (scratch, needed.regs);
2535           AND_COMPL_HARD_REG_SET (scratch, set.regs);
2536           IOR_HARD_REG_SET (new_resources.regs, scratch);
2537
2538           mark_set_resources (insn, &set, 0, 1);
2539         }
2540
2541       AND_HARD_REG_SET (res->regs, new_resources.regs);
2542     }
2543
2544   COPY_HARD_REG_SET (tinfo->live_regs, res->regs);
2545 }
2546 \f
2547 /* Scan a function looking for insns that need a delay slot and find insns to
2548    put into the delay slot.
2549
2550    NON_JUMPS_P is non-zero if we are to only try to fill non-jump insns (such
2551    as calls).  We do these first since we don't want jump insns (that are
2552    easier to fill) to get the only insns that could be used for non-jump insns.
2553    When it is zero, only try to fill JUMP_INSNs.
2554
2555    When slots are filled in this manner, the insns (including the
2556    delay_insn) are put together in a SEQUENCE rtx.  In this fashion,
2557    it is possible to tell whether a delay slot has really been filled
2558    or not.  `final' knows how to deal with this, by communicating
2559    through FINAL_SEQUENCE.  */
2560
2561 static void
2562 fill_simple_delay_slots (first, non_jumps_p)
2563      rtx first;
2564      int non_jumps_p;
2565 {
2566   register rtx insn, pat, trial, next_trial;
2567   register int i, j;
2568   int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2569   struct resources needed, set;
2570   register int slots_to_fill, slots_filled;
2571   rtx delay_list;
2572
2573   for (i = 0; i < num_unfilled_slots; i++)
2574     {
2575       int flags;
2576       /* Get the next insn to fill.  If it has already had any slots assigned,
2577          we can't do anything with it.  Maybe we'll improve this later.  */
2578
2579       insn = unfilled_slots_base[i];
2580       if (insn == 0
2581           || INSN_DELETED_P (insn)
2582           || (GET_CODE (insn) == INSN
2583               && GET_CODE (PATTERN (insn)) == SEQUENCE)
2584           || (GET_CODE (insn) == JUMP_INSN && non_jumps_p)
2585           || (GET_CODE (insn) != JUMP_INSN && ! non_jumps_p))
2586         continue;
2587      
2588       flags = get_jump_flags (insn, JUMP_LABEL (insn));
2589       slots_to_fill = num_delay_slots (insn);
2590       if (slots_to_fill == 0)
2591         abort ();
2592
2593       /* This insn needs, or can use, some delay slots.  SLOTS_TO_FILL
2594          says how many.  After initialization, first try optimizing
2595
2596          call _foo              call _foo
2597          nop                    add %o7,.-L1,%o7
2598          b,a L1
2599          nop
2600
2601          If this case applies, the delay slot of the call is filled with
2602          the unconditional jump.  This is done first to avoid having the
2603          delay slot of the call filled in the backward scan.  Also, since
2604          the unconditional jump is likely to also have a delay slot, that
2605          insn must exist when it is subsequently scanned.  */
2606
2607       slots_filled = 0;
2608       delay_list = 0;
2609
2610       if (GET_CODE (insn) == CALL_INSN
2611           && (trial = next_active_insn (insn))
2612           && GET_CODE (trial) == JUMP_INSN
2613           && simplejump_p (trial)
2614           && eligible_for_delay (insn, slots_filled, trial, flags)
2615           && no_labels_between_p (insn, trial))
2616         {
2617           slots_filled++;
2618           delay_list = add_to_delay_list (trial, delay_list);
2619           /* Remove the unconditional jump from consideration for delay slot
2620              filling and unthread it.  */
2621           if (unfilled_slots_base[i + 1] == trial)
2622             unfilled_slots_base[i + 1] = 0;
2623           {
2624             rtx next = NEXT_INSN (trial);
2625             rtx prev = PREV_INSN (trial);
2626             if (prev)
2627               NEXT_INSN (prev) = next;
2628             if (next)
2629               PREV_INSN (next) = prev;
2630           }
2631         }
2632
2633       /* Now, scan backwards from the insn to search for a potential
2634          delay-slot candidate.  Stop searching when a label or jump is hit.
2635
2636          For each candidate, if it is to go into the delay slot (moved
2637          forward in execution sequence), it must not need or set any resources
2638          that were set by later insns and must not set any resources that
2639          are needed for those insns.
2640          
2641          The delay slot insn itself sets resources unless it is a call
2642          (in which case the called routine, not the insn itself, is doing
2643          the setting).  */
2644
2645       if (slots_filled < slots_to_fill)
2646         {
2647           CLEAR_RESOURCE (&needed);
2648           CLEAR_RESOURCE (&set);
2649           mark_set_resources (insn, &set, 0, 0);
2650           mark_referenced_resources (insn, &needed, 0);
2651
2652           for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, 1);
2653                trial = next_trial)
2654             {
2655               next_trial = prev_nonnote_insn (trial);
2656
2657               /* This must be an INSN or CALL_INSN.  */
2658               pat = PATTERN (trial);
2659
2660               /* USE and CLOBBER at this level was just for flow; ignore it.  */
2661               if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2662                 continue;
2663
2664               /* Check for resource conflict first, to avoid unnecessary 
2665                  splitting.  */
2666               if (! insn_references_resource_p (trial, &set, 1)
2667                   && ! insn_sets_resource_p (trial, &set, 1)
2668                   && ! insn_sets_resource_p (trial, &needed, 1)
2669 #ifdef HAVE_cc0
2670                   /* Can't separate set of cc0 from its use.  */
2671                   && ! (reg_mentioned_p (cc0_rtx, pat)
2672                         && ! sets_cc0_p (cc0_rtx, pat))
2673 #endif
2674                   )
2675                 {
2676                   trial = try_split (pat, trial, 1);
2677                   next_trial = prev_nonnote_insn (trial);
2678                   if (eligible_for_delay (insn, slots_filled, trial, flags))
2679                     {
2680                       /* In this case, we are searching backward, so if we
2681                          find insns to put on the delay list, we want
2682                          to put them at the head, rather than the
2683                          tail, of the list.  */
2684
2685                       update_reg_dead_notes (trial, insn);
2686                       delay_list = gen_rtx (INSN_LIST, VOIDmode,
2687                                             trial, delay_list);
2688                       update_block (trial, trial);
2689                       delete_insn (trial);
2690                       if (slots_to_fill == ++slots_filled)
2691                         break;
2692                       continue;
2693                     }
2694                 }
2695
2696               mark_set_resources (trial, &set, 0, 1);
2697               mark_referenced_resources (trial, &needed, 1);
2698             }
2699         }
2700
2701       /* If all needed slots haven't been filled, we come here.  */
2702
2703       /* Try to optimize case of jumping around a single insn.  */
2704 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
2705       if (slots_filled != slots_to_fill
2706           && delay_list == 0
2707           && GET_CODE (insn) == JUMP_INSN && condjump_p (insn))
2708         {
2709           delay_list = optimize_skip (insn);
2710           if (delay_list)
2711             slots_filled += 1;
2712         }
2713 #endif
2714
2715       /* Try to get insns from beyond the insn needing the delay slot.
2716          These insns can neither set or reference resources set in insns being
2717          skipped, cannot set resources in the insn being skipped, and, if this
2718          is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2719          call might not return).
2720
2721          If this is a conditional jump, see if it merges back to us early
2722          enough for us to pick up insns from the merge point.  Don't do
2723          this if there is another branch to our label unless we pass all of
2724          them.
2725
2726          Another similar merge is if we jump to the same place that a
2727          later unconditional jump branches to.  In that case, we don't
2728          care about the number of uses of our label.  */
2729
2730       if (slots_filled != slots_to_fill
2731           && (GET_CODE (insn) != JUMP_INSN
2732               || (condjump_p (insn) && ! simplejump_p (insn)
2733                    && JUMP_LABEL (insn) != 0)))
2734         {
2735           rtx target = 0;
2736           int maybe_never = 0;
2737           int passed_label = 0;
2738           int target_uses;
2739           struct resources needed_at_jump;
2740
2741           CLEAR_RESOURCE (&needed);
2742           CLEAR_RESOURCE (&set);
2743
2744           if (GET_CODE (insn) == CALL_INSN)
2745             {
2746               mark_set_resources (insn, &set, 0, 1);
2747               mark_referenced_resources (insn, &needed, 1);
2748               maybe_never = 1;
2749             }
2750           else 
2751             {
2752               mark_set_resources (insn, &set, 0, 1);
2753               mark_referenced_resources (insn, &needed, 1);
2754               if (GET_CODE (insn) == JUMP_INSN)
2755                 {
2756                   /* Get our target and show how many more uses we want to
2757                      see before we hit the label.  */
2758                   target = JUMP_LABEL (insn);
2759                   target_uses = LABEL_NUSES (target) - 1;
2760                 }
2761                 
2762             }
2763
2764           for (trial = next_nonnote_insn (insn); trial; trial = next_trial)
2765             {
2766               rtx pat, trial_delay;
2767
2768               next_trial = next_nonnote_insn (trial);
2769
2770               if (GET_CODE (trial) == CODE_LABEL)
2771                 {
2772                   passed_label = 1;
2773
2774                   /* If this is our target, see if we have seen all its uses.
2775                      If so, indicate we have passed our target and ignore it.
2776                      All other labels cause us to stop our search.  */
2777                   if (trial == target && target_uses == 0)
2778                     {
2779                       target = 0;
2780                       continue;
2781                     }
2782                   else
2783                     break;
2784                 }
2785               else if (GET_CODE (trial) == BARRIER)
2786                 break;
2787
2788               /* We must have an INSN, JUMP_INSN, or CALL_INSN.  */
2789               pat = PATTERN (trial);
2790
2791               /* Stand-alone USE and CLOBBER are just for flow.  */
2792               if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2793                 continue;
2794
2795               /* If this already has filled delay slots, get the insn needing
2796                  the delay slots.  */
2797               if (GET_CODE (pat) == SEQUENCE)
2798                 trial_delay = XVECEXP (pat, 0, 0);
2799               else
2800                 trial_delay = trial;
2801
2802               /* If this is a jump insn to our target, indicate that we have
2803                  seen another jump to it.  If we aren't handling a conditional
2804                  jump, stop our search. Otherwise, compute the needs at its
2805                  target and add them to NEEDED.  */
2806               if (GET_CODE (trial_delay) == JUMP_INSN)
2807                 {
2808                   if (target == 0)
2809                     break;
2810                   else if (JUMP_LABEL (trial_delay) == target)
2811                     target_uses--;
2812                   else
2813                     {
2814                       mark_target_live_regs
2815                         (next_active_insn (JUMP_LABEL (trial_delay)),
2816                          &needed_at_jump);
2817                       needed.memory |= needed_at_jump.memory;
2818                       IOR_HARD_REG_SET (needed.regs, needed_at_jump.regs);
2819                     }
2820                 }
2821
2822               /* See if we have a resource problem before we try to
2823                  split.   */
2824               if (target == 0
2825                   && GET_CODE (pat) != SEQUENCE
2826                   && ! insn_references_resource_p (trial, &set, 1)
2827                   && ! insn_sets_resource_p (trial, &set, 1)
2828                   && ! insn_sets_resource_p (trial, &needed, 1)
2829 #ifdef HAVE_cc0
2830                   && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2831 #endif
2832                   && ! (maybe_never && may_trap_p (pat))
2833                   && (trial = try_split (pat, trial, 0))
2834                   && eligible_for_delay (insn, slots_filled, trial, flags))
2835                 {
2836                   next_trial = next_nonnote_insn (trial);
2837                   delay_list = add_to_delay_list (trial, delay_list);
2838
2839 #ifdef HAVE_cc0
2840                   if (reg_mentioned_p (cc0_rtx, pat))
2841                     link_cc0_insns (trial);
2842 #endif
2843
2844                   if (passed_label)
2845                     update_block (trial, trial);
2846                   delete_insn (trial);
2847                   if (slots_to_fill == ++slots_filled)
2848                     break;
2849                   continue;
2850                 }
2851
2852               mark_set_resources (trial, &set, 0, 1);
2853               mark_referenced_resources (trial, &needed, 1);
2854
2855               /* Ensure we don't put insns between the setting of cc and the
2856                  comparison by moving a setting of cc into an earlier delay
2857                  slot since these insns could clobber the condition code.  */
2858               set.cc = 1;
2859
2860               /* If this is a call or jump, we might not get here.  */
2861               if (GET_CODE (trial) == CALL_INSN
2862                   || GET_CODE (trial) == JUMP_INSN)
2863                 maybe_never = 1;
2864             }
2865
2866           /* If there are slots left to fill and our search was stopped by an
2867              unconditional branch, try the insn at the branch target.  We can
2868              redirect the branch if it works.  */
2869           if (slots_to_fill != slots_filled
2870               && trial
2871               && GET_CODE (trial) == JUMP_INSN
2872               && simplejump_p (trial)
2873               && (target == 0 || JUMP_LABEL (trial) == target)
2874               && (next_trial = next_active_insn (JUMP_LABEL (trial))) != 0
2875               && ! (GET_CODE (next_trial) == INSN
2876                     && GET_CODE (PATTERN (next_trial)) == SEQUENCE)
2877               && ! insn_references_resource_p (next_trial, &set, 1)
2878               && ! insn_sets_resource_p (next_trial, &set, 1)
2879               && ! insn_sets_resource_p (next_trial, &needed, 1)
2880 #ifdef HAVE_cc0
2881               && ! reg_mentioned_p (cc0_rtx, PATTERN (next_trial))
2882 #endif
2883               && ! (maybe_never && may_trap_p (PATTERN (next_trial)))
2884               && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2885               && eligible_for_delay (insn, slots_filled, next_trial, flags))
2886             {
2887               rtx new_label = next_active_insn (next_trial);
2888
2889               if (new_label != 0)
2890                 new_label = get_label_before (new_label);
2891
2892               delay_list 
2893                 = add_to_delay_list (copy_rtx (next_trial), delay_list);
2894               slots_filled++;
2895               redirect_jump (trial, new_label);
2896
2897               /* If we merged because we both jumped to the same place,
2898                  redirect the original insn also.  */
2899               if (target)
2900                 redirect_jump (insn, new_label);
2901             }
2902         }
2903
2904       if (delay_list)
2905         unfilled_slots_base[i]
2906           = emit_delay_sequence (insn, delay_list,
2907                                  slots_filled, slots_to_fill);
2908
2909       if (slots_to_fill == slots_filled)
2910         unfilled_slots_base[i] = 0;
2911
2912       note_delay_statistics (slots_filled, 0);
2913     }
2914
2915 #ifdef DELAY_SLOTS_FOR_EPILOGUE
2916   /* See if the epilogue needs any delay slots.  Try to fill them if so.
2917      The only thing we can do is scan backwards from the end of the 
2918      function.  If we did this in a previous pass, it is incorrect to do it
2919      again.  */
2920   if (current_function_epilogue_delay_list)
2921     return;
2922
2923   slots_to_fill = DELAY_SLOTS_FOR_EPILOGUE;
2924   if (slots_to_fill == 0)
2925     return;
2926
2927   slots_filled = 0;
2928   CLEAR_RESOURCE (&needed);
2929   CLEAR_RESOURCE (&set);
2930
2931   for (trial = get_last_insn (); ! stop_search_p (trial, 1);
2932        trial = PREV_INSN (trial))
2933     {
2934       if (GET_CODE (trial) == NOTE)
2935         continue;
2936       pat = PATTERN (trial);
2937       if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2938         continue;
2939
2940       if (! insn_references_resource_p (trial, &set, 1)
2941           && ! insn_sets_resource_p (trial, &needed, 1)
2942 #ifdef HAVE_cc0
2943           /* Don't want to mess with cc0 here.  */
2944           && ! reg_mentioned_p (cc0_rtx, pat)
2945 #endif
2946           )
2947         {
2948           trial = try_split (pat, trial, 1);
2949           if (ELIGIBLE_FOR_EPILOGUE_DELAY (trial, slots_filled))
2950             {
2951               /* Here as well we are searching backward, so put the
2952                  insns we find on the head of the list.  */
2953
2954               current_function_epilogue_delay_list
2955                 = gen_rtx (INSN_LIST, VOIDmode, trial,
2956                            current_function_epilogue_delay_list);
2957               mark_referenced_resources (trial, &end_of_function_needs, 1);
2958               update_block (trial, trial);
2959               delete_insn (trial);
2960
2961               /* Clear deleted bit so final.c will output the insn.  */
2962               INSN_DELETED_P (trial) = 0;
2963
2964               if (slots_to_fill == ++slots_filled)
2965                 break;
2966               continue;
2967             }
2968         }
2969
2970       mark_set_resources (trial, &set, 0, 1);
2971       mark_referenced_resources (trial, &needed, 1);
2972     }
2973
2974   note_delay_statistics (slots_filled, 0);
2975 #endif
2976 }
2977 \f
2978 /* Try to find insns to place in delay slots.
2979
2980    INSN is the jump needing SLOTS_TO_FILL delay slots.  It tests CONDITION
2981    or is an unconditional branch if CONDITION is const_true_rtx.
2982    *PSLOTS_FILLED is updated with the number of slots that we have filled.
2983
2984    THREAD is a flow-of-control, either the insns to be executed if the
2985    branch is true or if the branch is false, THREAD_IF_TRUE says which.
2986
2987    OPPOSITE_THREAD is the thread in the opposite direction.  It is used
2988    to see if any potential delay slot insns set things needed there.
2989
2990    LIKELY is non-zero if it is extremely likely that the branch will be
2991    taken and THREAD_IF_TRUE is set.  This is used for the branch at the
2992    end of a loop back up to the top.
2993
2994    OWN_THREAD and OWN_OPPOSITE_THREAD are true if we are the only user of the
2995    thread.  I.e., it is the fallthrough code of our jump or the target of the
2996    jump when we are the only jump going there.
2997
2998    If OWN_THREAD is false, it must be the "true" thread of a jump.  In that
2999    case, we can only take insns from the head of the thread for our delay
3000    slot.  We then adjust the jump to point after the insns we have taken.  */
3001
3002 static rtx
3003 fill_slots_from_thread (insn, condition, thread, opposite_thread, likely,
3004                         thread_if_true, own_thread, own_opposite_thread,
3005                         slots_to_fill, pslots_filled)
3006      rtx insn;
3007      rtx condition;
3008      rtx thread, opposite_thread;
3009      int likely;
3010      int thread_if_true;
3011      int own_thread, own_opposite_thread;
3012      int slots_to_fill, *pslots_filled;
3013 {
3014   rtx new_thread;
3015   rtx delay_list = 0;
3016   struct resources opposite_needed, set, needed;
3017   rtx trial;
3018   int lose = 0;
3019   int must_annul = 0;
3020   int flags;
3021
3022   /* Validate our arguments.  */
3023   if ((condition == const_true_rtx && ! thread_if_true)
3024       || (! own_thread && ! thread_if_true))
3025     abort ();
3026
3027   flags = get_jump_flags (insn, JUMP_LABEL (insn));
3028
3029   /* If our thread is the end of subroutine, we can't get any delay
3030      insns from that.  */
3031   if (thread == 0)
3032     return 0;
3033
3034   /* If this is an unconditional branch, nothing is needed at the
3035      opposite thread.  Otherwise, compute what is needed there.  */
3036   if (condition == const_true_rtx)
3037     CLEAR_RESOURCE (&opposite_needed);
3038   else
3039     mark_target_live_regs (opposite_thread, &opposite_needed);
3040
3041   /* If the insn at THREAD can be split, do it here to avoid having to
3042      update THREAD and NEW_THREAD if it is done in the loop below.  Also
3043      initialize NEW_THREAD.  */
3044
3045   new_thread = thread = try_split (PATTERN (thread), thread, 0);
3046
3047   /* Scan insns at THREAD.  We are looking for an insn that can be removed
3048      from THREAD (it neither sets nor references resources that were set
3049      ahead of it and it doesn't set anything needs by the insns ahead of
3050      it) and that either can be placed in an annulling insn or aren't
3051      needed at OPPOSITE_THREAD.  */
3052
3053   CLEAR_RESOURCE (&needed);
3054   CLEAR_RESOURCE (&set);
3055
3056   /* If we do not own this thread, we must stop as soon as we find
3057      something that we can't put in a delay slot, since all we can do
3058      is branch into THREAD at a later point.  Therefore, labels stop
3059      the search if this is not the `true' thread.  */
3060
3061   for (trial = thread;
3062        ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
3063        trial = next_nonnote_insn (trial))
3064     {
3065       rtx pat;
3066
3067       /* If we have passed a label, we no longer own this thread.  */
3068       if (GET_CODE (trial) == CODE_LABEL)
3069         {
3070           own_thread = 0;
3071           continue;
3072         }
3073
3074       pat = PATTERN (trial);
3075       if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
3076         continue;
3077
3078       /* If TRIAL conflicts with the insns ahead of it, we lose.  Also,
3079          don't separate or copy insns that set and use CC0.  */
3080       if (! insn_references_resource_p (trial, &set, 1)
3081           && ! insn_sets_resource_p (trial, &set, 1)
3082           && ! insn_sets_resource_p (trial, &needed, 1)
3083 #ifdef HAVE_cc0
3084           && ! (reg_mentioned_p (cc0_rtx, pat)
3085                 && (! own_thread || ! sets_cc0_p (pat)))
3086 #endif
3087           )
3088         {
3089           /* If TRIAL is redundant with some insn before INSN, we don't
3090              actually need to add it to the delay list; we can merely pretend
3091              we did.  */
3092           if (redundant_insn_p (trial, insn, delay_list))
3093             {
3094               if (own_thread)
3095                 {
3096                   update_block (trial, thread);
3097                   delete_insn (trial);
3098                 }
3099               else
3100                 new_thread = next_active_insn (trial);
3101
3102               continue;
3103             }
3104
3105           /* There are two ways we can win:  If TRIAL doesn't set anything
3106              needed at the opposite thread and can't trap, or if it can
3107              go into an annulled delay slot.  */
3108           if (condition == const_true_rtx
3109               || (! insn_sets_resource_p (trial, &opposite_needed, 1)
3110                   && ! may_trap_p (pat)))
3111             {
3112               trial = try_split (pat, trial, 0);
3113               pat = PATTERN (trial);
3114               if (eligible_for_delay (insn, *pslots_filled, trial, flags))
3115                 goto winner;
3116             }
3117           else if (0
3118 #ifdef ANNUL_IFTRUE_SLOTS
3119                    || ! thread_if_true
3120 #endif
3121 #ifdef ANNUL_IFFALSE_SLOTS
3122                    || thread_if_true
3123 #endif
3124                    )
3125             {
3126               trial = try_split (pat, trial, 0);
3127               pat = PATTERN (trial);
3128               if ((thread_if_true
3129                    ? eligible_for_annul_false (insn, *pslots_filled, trial, flags)
3130                    : eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
3131                 {
3132                   rtx temp;
3133
3134                   must_annul = 1;
3135                 winner:
3136
3137 #ifdef HAVE_cc0
3138                   if (reg_mentioned_p (cc0_rtx, pat))
3139                     link_cc0_insns (trial);
3140 #endif
3141
3142                   /* If we own this thread, delete the insn.  If this is the
3143                      destination of a branch, show that a basic block status
3144                      may have been updated.  In any case, mark the new
3145                      starting point of this thread.  */
3146                   if (own_thread)
3147                     {
3148                       update_block (trial, thread);
3149                       delete_insn (trial);
3150                     }
3151                   else
3152                     new_thread = next_active_insn (trial);
3153
3154                   temp = own_thread ? trial : copy_rtx (trial);
3155                   if (thread_if_true)
3156                     INSN_FROM_TARGET_P (temp) = 1;
3157
3158                   delay_list = add_to_delay_list (temp, delay_list);
3159
3160                   if (slots_to_fill == ++(*pslots_filled))
3161                     {
3162                       /* Even though we have filled all the slots, we
3163                          may be branching to a location that has a
3164                          redundant insn.  Skip any if so.  */
3165                       while (new_thread && ! own_thread
3166                              && ! insn_sets_resource_p (new_thread, &set, 1)
3167                              && ! insn_sets_resource_p (new_thread, &needed, 1)
3168                              && ! insn_references_resource_p (new_thread,
3169                                                               &set, 1)
3170                              && redundant_insn_p (new_thread, insn,
3171                                                   delay_list))
3172                         new_thread = next_active_insn (new_thread);
3173                       break;
3174                     }
3175
3176                   continue;
3177                 }
3178             }
3179         }
3180
3181       /* This insn can't go into a delay slot.  */
3182       lose = 1;
3183       mark_set_resources (trial, &set, 0, 1);
3184       mark_referenced_resources (trial, &needed, 1);
3185
3186       /* Ensure we don't put insns between the setting of cc and the comparison
3187          by moving a setting of cc into an earlier delay slot since these insns
3188          could clobber the condition code.  */
3189       set.cc = 1;
3190
3191       /* If this insn is a register-register copy and the next insn has
3192          a use of our destination, change it to use our source.  That way,
3193          it will become a candidate for our delay slot the next time
3194          through this loop.  This case occurs commonly in loops that
3195          scan a list.
3196
3197          We could check for more complex cases than those tested below,
3198          but it doesn't seem worth it.  It might also be a good idea to try
3199          to swap the two insns.  That might do better.
3200
3201          We can't do this if the next insn modifies our source, because that
3202          would make the replacement into the insn invalid.  This also
3203          prevents updating the contents of a PRE_INC.  */
3204
3205       if (GET_CODE (trial) == INSN && GET_CODE (pat) == SET
3206           && GET_CODE (SET_SRC (pat)) == REG
3207           && GET_CODE (SET_DEST (pat)) == REG)
3208         {
3209           rtx next = next_nonnote_insn (trial);
3210
3211           if (next && GET_CODE (next) == INSN
3212               && GET_CODE (PATTERN (next)) != USE
3213               && ! reg_set_p (SET_DEST (pat), next)
3214               && reg_referenced_p (SET_DEST (pat), PATTERN (next)))
3215             validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
3216         }
3217     }
3218
3219   /* If we stopped on a branch insn that has delay slots, see if we can
3220      steal some of the insns in those slots.  */
3221   if (trial && GET_CODE (trial) == INSN
3222       && GET_CODE (PATTERN (trial)) == SEQUENCE
3223       && GET_CODE (XVECEXP (PATTERN (trial), 0, 0)) == JUMP_INSN)
3224     {
3225       /* If this is the `true' thread, we will want to follow the jump,
3226          so we can only do this if we have taken everything up to here.  */
3227       if (thread_if_true && trial == new_thread)
3228         delay_list
3229           = steal_delay_list_from_target (insn, condition, PATTERN (trial),
3230                                           delay_list, &set, &needed,
3231                                           &opposite_needed, slots_to_fill,
3232                                           pslots_filled, &must_annul,
3233                                           &new_thread);
3234       else if (! thread_if_true)
3235         delay_list
3236           = steal_delay_list_from_fallthrough (insn, condition,
3237                                                PATTERN (trial),
3238                                                delay_list, &set, &needed,
3239                                                &opposite_needed, slots_to_fill,
3240                                                pslots_filled, &must_annul);
3241     }
3242
3243   /* If we haven't found anything for this delay slot and it is very
3244      likely that the branch will be taken, see if the insn at our target
3245      increments or decrements a register with an increment that does not
3246      depend on the destination register.  If so, try to place the opposite
3247      arithmetic insn after the jump insn and put the arithmetic insn in the
3248      delay slot.  If we can't do this, return.  */
3249   if (delay_list == 0 && likely && new_thread && GET_CODE (new_thread) == INSN)
3250     {
3251       rtx pat = PATTERN (new_thread);
3252       rtx dest;
3253       rtx src;
3254
3255       trial = new_thread;
3256       pat = PATTERN (trial);
3257
3258       if (GET_CODE (trial) != INSN || GET_CODE (pat) != SET
3259           || ! eligible_for_delay (insn, 0, trial, flags))
3260         return 0;
3261
3262       dest = SET_DEST (pat), src = SET_SRC (pat);
3263       if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
3264           && rtx_equal_p (XEXP (src, 0), dest)
3265           && ! reg_overlap_mentioned_p (dest, XEXP (src, 1)))
3266         {
3267           rtx other = XEXP (src, 1);
3268           rtx new_arith;
3269           rtx ninsn;
3270
3271           /* If this is a constant adjustment, use the same code with
3272              the negated constant.  Otherwise, reverse the sense of the
3273              arithmetic.  */
3274           if (GET_CODE (other) == CONST_INT)
3275             new_arith = gen_rtx (GET_CODE (src), GET_MODE (src), dest,
3276                                  negate_rtx (GET_MODE (src), other));
3277           else
3278             new_arith = gen_rtx (GET_CODE (src) == PLUS ? MINUS : PLUS,
3279                                  GET_MODE (src), dest, other);
3280
3281           ninsn = emit_insn_after (gen_rtx (SET, VOIDmode, dest, new_arith),
3282                                    insn);
3283
3284           if (recog_memoized (ninsn) < 0
3285               || (insn_extract (ninsn),
3286                   ! constrain_operands (INSN_CODE (ninsn), 1)))
3287             {
3288               delete_insn (ninsn);
3289               return 0;
3290             }
3291
3292           if (own_thread)
3293             {
3294               update_block (trial, thread);
3295               delete_insn (trial);
3296             }
3297           else
3298             new_thread = next_active_insn (trial);
3299
3300           ninsn = own_thread ? trial : copy_rtx (trial);
3301           if (thread_if_true)
3302             INSN_FROM_TARGET_P (ninsn) = 1;
3303
3304           delay_list = add_to_delay_list (ninsn, NULL_RTX);
3305           (*pslots_filled)++;
3306         }
3307     }
3308
3309   if (delay_list && must_annul)
3310     INSN_ANNULLED_BRANCH_P (insn) = 1;
3311
3312   /* If we are to branch into the middle of this thread, find an appropriate
3313      label or make a new one if none, and redirect INSN to it.  If we hit the
3314      end of the function, use the end-of-function label.  */
3315   if (new_thread != thread)
3316     {
3317       rtx label;
3318
3319       if (! thread_if_true)
3320         abort ();
3321
3322       if (new_thread && GET_CODE (new_thread) == JUMP_INSN
3323           && (simplejump_p (new_thread)
3324               || GET_CODE (PATTERN (new_thread)) == RETURN))
3325         new_thread = follow_jumps (JUMP_LABEL (new_thread));
3326
3327       if (new_thread == 0)
3328         label = find_end_label ();
3329       else if (GET_CODE (new_thread) == CODE_LABEL)
3330         label = new_thread;
3331       else
3332         label = get_label_before (new_thread);
3333
3334       redirect_jump (insn, label);
3335     }
3336
3337   return delay_list;
3338 }
3339 \f
3340 /* Make another attempt to find insns to place in delay slots.
3341
3342    We previously looked for insns located in front of the delay insn
3343    and, for non-jump delay insns, located behind the delay insn.
3344
3345    Here only try to schedule jump insns and try to move insns from either
3346    the target or the following insns into the delay slot.  If annulling is
3347    supported, we will be likely to do this.  Otherwise, we can do this only
3348    if safe.  */
3349
3350 static void
3351 fill_eager_delay_slots (first)
3352      rtx first;
3353 {
3354   register rtx insn;
3355   register int i;
3356   int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
3357
3358   for (i = 0; i < num_unfilled_slots; i++)
3359     {
3360       rtx condition;
3361       rtx target_label, insn_at_target, fallthrough_insn;
3362       rtx delay_list = 0;
3363       int own_target;
3364       int own_fallthrough;
3365       int prediction, slots_to_fill, slots_filled;
3366
3367       insn = unfilled_slots_base[i];
3368       if (insn == 0
3369           || INSN_DELETED_P (insn)
3370           || GET_CODE (insn) != JUMP_INSN
3371           || ! condjump_p (insn))
3372         continue;
3373
3374       slots_to_fill = num_delay_slots (insn);
3375       if (slots_to_fill == 0)
3376         abort ();
3377
3378       slots_filled = 0;
3379       target_label = JUMP_LABEL (insn);
3380       condition = get_branch_condition (insn, target_label);
3381
3382       if (condition == 0)
3383         continue;
3384
3385       /* Get the next active fallthough and target insns and see if we own
3386          them.  Then see whether the branch is likely true.  We don't need
3387          to do a lot of this for unconditional branches.  */
3388
3389       insn_at_target = next_active_insn (target_label);
3390       own_target = own_thread_p (target_label, target_label, 0);
3391
3392       if (condition == const_true_rtx)
3393         {
3394           own_fallthrough = 0;
3395           fallthrough_insn = 0;
3396           prediction = 2;
3397         }
3398       else
3399         {
3400           fallthrough_insn = next_active_insn (insn);
3401           own_fallthrough = own_thread_p (NEXT_INSN (insn), NULL_RTX, 1);
3402           prediction = mostly_true_jump (insn, condition);
3403         }
3404
3405       /* If this insn is expected to branch, first try to get insns from our
3406          target, then our fallthrough insns.  If it is not, expected to branch,
3407          try the other order.  */
3408
3409       if (prediction > 0)
3410         {
3411           delay_list
3412             = fill_slots_from_thread (insn, condition, insn_at_target,
3413                                       fallthrough_insn, prediction == 2, 1,
3414                                       own_target, own_fallthrough,
3415                                       slots_to_fill, &slots_filled);
3416
3417           if (delay_list == 0 && own_fallthrough)
3418             {
3419               /* Even though we didn't find anything for delay slots,
3420                  we might have found a redundant insn which we deleted
3421                  from the thread that was filled.  So we have to recompute
3422                  the next insn at the target.  */
3423               target_label = JUMP_LABEL (insn);
3424               insn_at_target = next_active_insn (target_label);
3425
3426               delay_list
3427                 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3428                                           insn_at_target, 0, 0,
3429                                           own_fallthrough, own_target,
3430                                           slots_to_fill, &slots_filled);
3431             }
3432         }
3433       else
3434         {
3435           if (own_fallthrough)
3436             delay_list
3437               = fill_slots_from_thread (insn, condition, fallthrough_insn,
3438                                         insn_at_target, 0, 0,
3439                                         own_fallthrough, own_target,
3440                                         slots_to_fill, &slots_filled);
3441
3442           if (delay_list == 0)
3443             delay_list
3444               = fill_slots_from_thread (insn, condition, insn_at_target,
3445                                         next_active_insn (insn), 0, 1,
3446                                         own_target, own_fallthrough,
3447                                         slots_to_fill, &slots_filled);
3448         }
3449
3450       if (delay_list)
3451         unfilled_slots_base[i]
3452           = emit_delay_sequence (insn, delay_list,
3453                                  slots_filled, slots_to_fill);
3454
3455       if (slots_to_fill == slots_filled)
3456         unfilled_slots_base[i] = 0;
3457
3458       note_delay_statistics (slots_filled, 1);
3459     }
3460 }
3461 \f
3462 /* Once we have tried two ways to fill a delay slot, make a pass over the
3463    code to try to improve the results and to do such things as more jump
3464    threading.  */
3465
3466 static void
3467 relax_delay_slots (first)
3468      rtx first;
3469 {
3470   register rtx insn, next, pat;
3471   register rtx trial, delay_insn, target_label;
3472
3473   /* Look at every JUMP_INSN and see if we can improve it.  */
3474   for (insn = first; insn; insn = next)
3475     {
3476       rtx other;
3477
3478       next = next_active_insn (insn);
3479
3480       /* If this is a jump insn, see if it now jumps to a jump, jumps to
3481          the next insn, or jumps to a label that is not the last of a
3482          group of consecutive labels.  */
3483       if (GET_CODE (insn) == JUMP_INSN
3484           && condjump_p (insn)
3485           && (target_label = JUMP_LABEL (insn)) != 0)
3486         {
3487           target_label = follow_jumps (target_label);
3488           target_label = prev_label (next_active_insn (target_label));
3489
3490           if (target_label == 0)
3491             target_label = find_end_label ();
3492
3493           if (next_active_insn (target_label) == next)
3494             {
3495               delete_jump (insn);
3496               continue;
3497             }
3498
3499           if (target_label != JUMP_LABEL (insn))
3500             redirect_jump (insn, target_label);
3501
3502           /* See if this jump branches around a unconditional jump.
3503              If so, invert this jump and point it to the target of the
3504              second jump.  */
3505           if (next && GET_CODE (next) == JUMP_INSN
3506               && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3507               && next_active_insn (target_label) == next_active_insn (next)
3508               && no_labels_between_p (insn, next))
3509             {
3510               rtx label = JUMP_LABEL (next);
3511
3512               /* Be careful how we do this to avoid deleting code or
3513                  labels that are momentarily dead.  See similar optimization
3514                  in jump.c.
3515
3516                  We also need to ensure we properly handle the case when
3517                  invert_jump fails.  */
3518
3519               ++LABEL_NUSES (target_label);
3520               if (label)
3521                 ++LABEL_NUSES (label);
3522
3523               if (invert_jump (insn, label))
3524                 {
3525                   delete_insn (next);
3526                   next = insn;
3527                 }
3528
3529               if (label)
3530                 --LABEL_NUSES (label);
3531
3532               if (--LABEL_NUSES (target_label) == 0)
3533                 delete_insn (target_label);
3534
3535               continue;
3536             }
3537         }
3538           
3539       /* If this is an unconditional jump and the previous insn is a
3540          conditional jump, try reversing the condition of the previous
3541          insn and swapping our targets.  The next pass might be able to
3542          fill the slots.
3543
3544          Don't do this if we expect the conditional branch to be true, because
3545          we would then be making the more common case longer.  */
3546
3547       if (GET_CODE (insn) == JUMP_INSN
3548           && (simplejump_p (insn) || GET_CODE (PATTERN (insn)) == RETURN)
3549           && (other = prev_active_insn (insn)) != 0
3550           && condjump_p (other)
3551           && no_labels_between_p (other, insn)
3552           && 0 < mostly_true_jump (other,
3553                                    get_branch_condition (other,
3554                                                          JUMP_LABEL (other))))
3555         {
3556           rtx other_target = JUMP_LABEL (other);
3557
3558           /* Increment the count of OTHER_TARGET, so it doesn't get deleted
3559              as we move the label.  */
3560           if (other_target)
3561             ++LABEL_NUSES (other_target);
3562
3563           if (invert_jump (other, target_label))
3564             redirect_jump (insn, other_target);
3565
3566           if (other_target)
3567             --LABEL_NUSES (other_target);
3568         }
3569
3570       /* Now look only at cases where we have filled a delay slot.  */
3571       if (GET_CODE (insn) != INSN
3572           || GET_CODE (PATTERN (insn)) != SEQUENCE)
3573         continue;
3574
3575       pat = PATTERN (insn);
3576       delay_insn = XVECEXP (pat, 0, 0);
3577
3578       /* See if the first insn in the delay slot is redundant with some
3579          previous insn.  Remove it from the delay slot if so; then set up
3580          to reprocess this insn.  */
3581       if (redundant_insn_p (XVECEXP (pat, 0, 1), delay_insn, 0))
3582         {
3583           delete_from_delay_slot (XVECEXP (pat, 0, 1));
3584           next = prev_active_insn (next);
3585           continue;
3586         }
3587
3588       /* Now look only at the cases where we have a filled JUMP_INSN.  */
3589       if (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) != JUMP_INSN
3590           || ! condjump_p (XVECEXP (PATTERN (insn), 0, 0)))
3591         continue;
3592
3593       target_label = JUMP_LABEL (delay_insn);
3594
3595       if (target_label)
3596         {
3597           /* If this jump goes to another unconditional jump, thread it, but
3598              don't convert a jump into a RETURN here.  */
3599           trial = follow_jumps (target_label);
3600           trial = prev_label (next_active_insn (trial));
3601           if (trial == 0 && target_label != 0)
3602             trial = find_end_label ();
3603
3604           if (trial != target_label)
3605             {
3606               redirect_jump (delay_insn, trial);
3607               target_label = trial;
3608             }
3609
3610           /* If the first insn at TARGET_LABEL is redundant with a previous
3611              insn, redirect the jump to the following insn process again.  */
3612           trial = next_active_insn (target_label);
3613           if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3614               && redundant_insn_p (trial, insn, 0))
3615             {
3616               trial = next_active_insn (trial);
3617               if (trial == 0)
3618                 target_label = find_end_label ();
3619               else
3620                 target_label = get_label_before (trial);
3621               redirect_jump (delay_insn, target_label);
3622               next = insn;
3623               continue;
3624             }
3625
3626           /* Similarly, if it is an unconditional jump with one insn in its
3627              delay list and that insn is redundant, thread the jump.  */
3628           if (trial && GET_CODE (PATTERN (trial)) == SEQUENCE
3629               && XVECLEN (PATTERN (trial), 0) == 2
3630               && GET_CODE (XVECEXP (PATTERN (trial), 0, 0)) == JUMP_INSN
3631               && (simplejump_p (XVECEXP (PATTERN (trial), 0, 0))
3632                   || GET_CODE (PATTERN (XVECEXP (PATTERN (trial), 0, 0))) == RETURN)
3633               && redundant_insn_p (XVECEXP (PATTERN (trial), 0, 1), insn, 0))
3634             {
3635               target_label = JUMP_LABEL (XVECEXP (PATTERN (trial), 0, 0));
3636               if (target_label == 0)
3637                 target_label = find_end_label ();
3638               redirect_jump (delay_insn, target_label);
3639               next = insn;
3640               continue;
3641             }
3642         }
3643
3644       if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3645           && prev_active_insn (target_label) == insn
3646 #ifdef HAVE_cc0
3647           /* If the last insn in the delay slot sets CC0 for some insn,
3648              various code assumes that it is in a delay slot.  We could
3649              put it back where it belonged and delete the register notes,
3650              but it doesn't seem worthwhile in this uncommon case.  */
3651           && ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3652                               REG_CC_USER, NULL_RTX)
3653 #endif
3654           )
3655         {
3656           int i;
3657
3658           /* All this insn does is execute its delay list and jump to the
3659              following insn.  So delete the jump and just execute the delay
3660              list insns.
3661
3662              We do this by deleting the INSN containing the SEQUENCE, then
3663              re-emitting the insns separately, and then deleting the jump.
3664              This allows the count of the jump target to be properly
3665              decremented.  */
3666
3667           /* Clear the from target bit, since these insns are no longer
3668              in delay slots.  */
3669           for (i = 0; i < XVECLEN (pat, 0); i++)
3670             INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3671
3672           trial = PREV_INSN (insn);
3673           delete_insn (insn);
3674           emit_insn_after (pat, trial);
3675           delete_scheduled_jump (delay_insn);
3676           continue;
3677         }
3678
3679       /* See if this is an unconditional jump around a single insn which is
3680          identical to the one in its delay slot.  In this case, we can just
3681          delete the branch and the insn in its delay slot.  */
3682       if (next && GET_CODE (next) == INSN
3683           && prev_label (next_active_insn (next)) == target_label
3684           && simplejump_p (insn)
3685           && XVECLEN (pat, 0) == 2
3686           && rtx_equal_p (PATTERN (next), PATTERN (XVECEXP (pat, 0, 1))))
3687         {
3688           delete_insn (insn);
3689           continue;
3690         }
3691
3692       /* See if this jump (with its delay slots) branches around another
3693          jump (without delay slots).  If so, invert this jump and point
3694          it to the target of the second jump.  We cannot do this for
3695          annulled jumps, though.  Again, don't convert a jump to a RETURN
3696          here.  */
3697       if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3698           && next && GET_CODE (next) == JUMP_INSN
3699           && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3700           && next_active_insn (target_label) == next_active_insn (next)
3701           && no_labels_between_p (insn, next))
3702         {
3703           rtx label = JUMP_LABEL (next);
3704           rtx old_label = JUMP_LABEL (delay_insn);
3705
3706           if (label == 0)
3707             label = find_end_label ();
3708
3709           /* Be careful how we do this to avoid deleting code or labels
3710              that are momentarily dead.  See similar optimization in jump.c  */
3711           if (old_label)
3712             ++LABEL_NUSES (old_label);
3713
3714           if (invert_jump (delay_insn, label))
3715             {
3716               delete_insn (next);
3717               next = insn;
3718             }
3719
3720           if (old_label && --LABEL_NUSES (old_label) == 0)
3721             delete_insn (old_label);
3722           continue;
3723         }
3724
3725       /* If we own the thread opposite the way this insn branches, see if we
3726          can merge its delay slots with following insns.  */
3727       if (INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3728           && own_thread_p (NEXT_INSN (insn), 0, 1))
3729         try_merge_delay_insns (insn, next);
3730       else if (! INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3731                && own_thread_p (target_label, target_label, 0))
3732         try_merge_delay_insns (insn, next_active_insn (target_label));
3733
3734       /* If we get here, we haven't deleted INSN.  But we may have deleted
3735          NEXT, so recompute it.  */
3736       next = next_active_insn (insn);
3737     }
3738 }
3739 \f
3740 #ifdef HAVE_return
3741
3742 /* Look for filled jumps to the end of function label.  We can try to convert
3743    them into RETURN insns if the insns in the delay slot are valid for the
3744    RETURN as well.  */
3745
3746 static void
3747 make_return_insns (first)
3748      rtx first;
3749 {
3750   rtx insn, jump_insn, pat;
3751   rtx real_return_label = end_of_function_label;
3752   int slots, i;
3753
3754   /* See if there is a RETURN insn in the function other than the one we
3755      made for END_OF_FUNCTION_LABEL.  If so, set up anything we can't change
3756      into a RETURN to jump to it.  */
3757   for (insn = first; insn; insn = NEXT_INSN (insn))
3758     if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) == RETURN)
3759       {
3760         real_return_label = get_label_before (insn);
3761         break;
3762       }
3763   
3764   /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3765      was equal to END_OF_FUNCTION_LABEL.  */
3766   LABEL_NUSES (real_return_label)++;
3767
3768   /* Clear the list of insns to fill so we can use it.  */
3769   obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3770
3771   for (insn = first; insn; insn = NEXT_INSN (insn))
3772     {
3773       int flags;
3774
3775       /* Only look at filled JUMP_INSNs that go to the end of function
3776          label.  */
3777       if (GET_CODE (insn) != INSN
3778           || GET_CODE (PATTERN (insn)) != SEQUENCE
3779           || GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) != JUMP_INSN
3780           || JUMP_LABEL (XVECEXP (PATTERN (insn), 0, 0)) != end_of_function_label)
3781         continue;
3782
3783       pat = PATTERN (insn);
3784       jump_insn = XVECEXP (pat, 0, 0);
3785
3786       /* If we can't make the jump into a RETURN, redirect it to the best
3787          RETURN and go on to the next insn.  */
3788       if (! redirect_jump (jump_insn, NULL_RTX))
3789         {
3790           redirect_jump (jump_insn, real_return_label);
3791           continue;
3792         }
3793
3794       /* See if this RETURN can accept the insns current in its delay slot.
3795          It can if it has more or an equal number of slots and the contents
3796          of each is valid.  */
3797
3798       flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3799       slots = num_delay_slots (jump_insn);
3800       if (slots >= XVECLEN (pat, 0) - 1)
3801         {
3802           for (i = 1; i < XVECLEN (pat, 0); i++)
3803             if (! (
3804 #ifdef ANNUL_IFFALSE_SLOTS
3805                    (INSN_ANNULLED_BRANCH_P (jump_insn)
3806                     && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3807                    ? eligible_for_annul_false (jump_insn, i - 1,
3808                                                XVECEXP (pat, 0, i), flags) :
3809 #endif
3810 #ifdef ANNUL_IFTRUE_SLOTS
3811                    (INSN_ANNULLED_BRANCH_P (jump_insn)
3812                     && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3813                    ? eligible_for_annul_true (jump_insn, i - 1,
3814                                               XVECEXP (pat, 0, i), flags) :
3815 #endif
3816                    eligible_for_delay (jump_insn, i -1, XVECEXP (pat, 0, i), flags)))
3817               break;
3818         }
3819       else
3820         i = 0;
3821
3822       if (i == XVECLEN (pat, 0))
3823         continue;
3824
3825       /* We have to do something with this insn.  If it is an unconditional
3826          RETURN, delete the SEQUENCE and output the individual insns,
3827          followed by the RETURN.  Then set things up so we try to find
3828          insns for its delay slots, if it needs some.  */
3829       if (GET_CODE (PATTERN (jump_insn)) == RETURN)
3830         {
3831           rtx prev = PREV_INSN (insn);
3832
3833           delete_insn (insn);
3834           for (i = 1; i < XVECLEN (pat, 0); i++)
3835             prev = emit_insn_after (PATTERN (XVECEXP (pat, 0, i)), prev);
3836
3837           insn = emit_jump_insn_after (PATTERN (jump_insn), prev);
3838           emit_barrier_after (insn);
3839
3840           if (slots)
3841             obstack_ptr_grow (&unfilled_slots_obstack, insn);
3842         }
3843       else
3844         /* It is probably more efficient to keep this with its current
3845            delay slot as a branch to a RETURN.  */
3846         redirect_jump (jump_insn, real_return_label);
3847     }
3848
3849   /* Now delete REAL_RETURN_LABEL if we never used it.  Then try to fill any
3850      new delay slots we have created.  */
3851   if (--LABEL_NUSES (real_return_label) == 0)
3852     delete_insn (real_return_label);
3853
3854   fill_simple_delay_slots (first, 1);
3855   fill_simple_delay_slots (first, 0);
3856 }
3857 #endif
3858 \f
3859 /* Try to find insns to place in delay slots.  */
3860
3861 void
3862 dbr_schedule (first, file)
3863      rtx first;
3864      FILE *file;
3865 {
3866   rtx insn, next, epilogue_insn = 0;
3867   int i;
3868 #if 0
3869   int old_flag_no_peephole = flag_no_peephole;
3870
3871   /* Execute `final' once in prescan mode to delete any insns that won't be
3872      used.  Don't let final try to do any peephole optimization--it will
3873      ruin dataflow information for this pass.  */
3874
3875   flag_no_peephole = 1;
3876   final (first, 0, NO_DEBUG, 1, 1);
3877   flag_no_peephole = old_flag_no_peephole;
3878 #endif
3879
3880   /* Find the highest INSN_UID and allocate and initialize our map from
3881      INSN_UID's to position in code.  */
3882   for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3883     {
3884       if (INSN_UID (insn) > max_uid)
3885         max_uid = INSN_UID (insn);
3886       if (GET_CODE (insn) == NOTE
3887           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
3888         epilogue_insn = insn;
3889     }
3890
3891   uid_to_ruid = (int *) alloca ((max_uid + 1) * sizeof (int *));
3892   for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3893     uid_to_ruid[INSN_UID (insn)] = i;
3894   
3895   /* Initialize the list of insns that need filling.  */
3896   if (unfilled_firstobj == 0)
3897     {
3898       gcc_obstack_init (&unfilled_slots_obstack);
3899       unfilled_firstobj = (rtx *) obstack_alloc (&unfilled_slots_obstack, 0);
3900     }
3901
3902   for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3903     {
3904       rtx target;
3905
3906       INSN_ANNULLED_BRANCH_P (insn) = 0;
3907       INSN_FROM_TARGET_P (insn) = 0;
3908
3909       /* Skip vector tables.  We can't get attributes for them.  */
3910       if (GET_CODE (insn) == JUMP_INSN
3911           && (GET_CODE (PATTERN (insn)) == ADDR_VEC
3912               || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
3913         continue;
3914     
3915       if (num_delay_slots (insn) > 0)
3916         obstack_ptr_grow (&unfilled_slots_obstack, insn);
3917
3918       /* Ensure all jumps go to the last of a set of consecutive labels.  */
3919       if (GET_CODE (insn) == JUMP_INSN && condjump_p (insn)
3920           && JUMP_LABEL (insn) != 0
3921           && ((target = prev_label (next_active_insn (JUMP_LABEL (insn))))
3922               != JUMP_LABEL (insn)))
3923         redirect_jump (insn, target);
3924     }
3925
3926   /* Indicate what resources are required to be valid at the end of the current
3927      function.  The condition code never is and memory always is.  If the
3928      frame pointer is needed, it is and so is the stack pointer unless
3929      EXIT_IGNORE_STACK is non-zero.  If the frame pointer is not needed, the
3930      stack pointer is.  Registers used to return the function value are
3931      needed.  Registers holding global variables are needed.  */
3932
3933   end_of_function_needs.cc = 0;
3934   end_of_function_needs.memory = 1;
3935   CLEAR_HARD_REG_SET (end_of_function_needs.regs);
3936
3937   if (frame_pointer_needed)
3938     {
3939       SET_HARD_REG_BIT (end_of_function_needs.regs, FRAME_POINTER_REGNUM);
3940 #ifdef EXIT_IGNORE_STACK
3941       if (! EXIT_IGNORE_STACK)
3942 #endif
3943         SET_HARD_REG_BIT (end_of_function_needs.regs, STACK_POINTER_REGNUM);
3944     }
3945   else
3946     SET_HARD_REG_BIT (end_of_function_needs.regs, STACK_POINTER_REGNUM);
3947
3948   if (current_function_return_rtx != 0
3949       && GET_CODE (current_function_return_rtx) == REG)
3950     mark_referenced_resources (current_function_return_rtx,
3951                                &end_of_function_needs, 1);
3952
3953   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3954     if (global_regs[i])
3955       SET_HARD_REG_BIT (end_of_function_needs.regs, i);
3956
3957   /* The registers required to be live at the end of the function are
3958      represented in the flow information as being dead just prior to
3959      reaching the end of the function.  For example, the return of a value
3960      might be represented by a USE of the return register immediately
3961      followed by an unconditional jump to the return label where the
3962      return label is the end of the RTL chain.  The end of the RTL chain
3963      is then taken to mean that the return register is live.
3964
3965      This sequence is no longer maintained when epilogue instructions are
3966      added to the RTL chain.  To reconstruct the original meaning, the
3967      start of the epilogue (NOTE_INSN_EPILOGUE_BEG) is regarded as the
3968      point where these registers become live (start_of_epilogue_needs).
3969      If epilogue instructions are present, the registers set by those
3970      instructions won't have been processed by flow.  Thus, those
3971      registers are additionally required at the end of the RTL chain
3972      (end_of_function_needs).  */
3973
3974   start_of_epilogue_needs = end_of_function_needs;
3975
3976   while (epilogue_insn = next_nonnote_insn (epilogue_insn))
3977     mark_set_resources (epilogue_insn, &end_of_function_needs, 0, 1);
3978
3979   /* Show we haven't computed an end-of-function label yet.  */
3980   end_of_function_label = 0;
3981
3982   /* Allocate and initialize the tables used by mark_target_live_regs.  */
3983   target_hash_table
3984     = (struct target_info **) alloca ((TARGET_HASH_PRIME
3985                                        * sizeof (struct target_info *)));
3986   bzero (target_hash_table, TARGET_HASH_PRIME * sizeof (struct target_info *));
3987
3988   bb_ticks = (int *) alloca (n_basic_blocks * sizeof (int));
3989   bzero (bb_ticks, n_basic_blocks * sizeof (int));
3990
3991   /* Initialize the statistics for this function.  */
3992   bzero (num_insns_needing_delays, sizeof num_insns_needing_delays);
3993   bzero (num_filled_delays, sizeof num_filled_delays);
3994
3995   /* Now do the delay slot filling.  Try everything twice in case earlier
3996      changes make more slots fillable.  */
3997
3998   for (reorg_pass_number = 0;
3999        reorg_pass_number < MAX_REORG_PASSES;
4000        reorg_pass_number++)
4001     {
4002       fill_simple_delay_slots (first, 1);
4003       fill_simple_delay_slots (first, 0);
4004       fill_eager_delay_slots (first);
4005       relax_delay_slots (first);
4006     }
4007
4008   /* Delete any USE insns made by update_block; subsequent passes don't need
4009      them or know how to deal with them.  */
4010   for (insn = first; insn; insn = next)
4011     {
4012       next = NEXT_INSN (insn);
4013
4014       if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE
4015           && GET_RTX_CLASS (GET_CODE (XEXP (PATTERN (insn), 0))) == 'i')
4016         next = delete_insn (insn);
4017     }
4018
4019   /* If we made an end of function label, indicate that it is now
4020      safe to delete it by undoing our prior adjustment to LABEL_NUSES.
4021      If it is now unused, delete it.  */
4022   if (end_of_function_label && --LABEL_NUSES (end_of_function_label) == 0)
4023     delete_insn (end_of_function_label);
4024
4025 #ifdef HAVE_return
4026   if (HAVE_return && end_of_function_label != 0)
4027     make_return_insns (first);
4028 #endif
4029
4030   obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
4031
4032   /* It is not clear why the line below is needed, but it does seem to be.  */
4033   unfilled_firstobj = (rtx *) obstack_alloc (&unfilled_slots_obstack, 0);
4034
4035   /* Reposition the prologue and epilogue notes in case we moved the
4036      prologue/epilogue insns.  */
4037   reposition_prologue_and_epilogue_notes (first);
4038
4039   if (file)
4040     {
4041       register int i, j, need_comma;
4042
4043       for (reorg_pass_number = 0;
4044            reorg_pass_number < MAX_REORG_PASSES;
4045            reorg_pass_number++)
4046         {
4047           fprintf (file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
4048           for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
4049             {
4050               need_comma = 0;
4051               fprintf (file, ";; Reorg function #%d\n", i);
4052
4053               fprintf (file, ";; %d insns needing delay slots\n;; ",
4054                        num_insns_needing_delays[i][reorg_pass_number]);
4055
4056               for (j = 0; j < MAX_DELAY_HISTOGRAM; j++)
4057                 if (num_filled_delays[i][j][reorg_pass_number])
4058                   {
4059                     if (need_comma)
4060                       fprintf (file, ", ");
4061                     need_comma = 1;
4062                     fprintf (file, "%d got %d delays",
4063                              num_filled_delays[i][j][reorg_pass_number], j);
4064                   }
4065               fprintf (file, "\n");
4066             }
4067         }
4068     }
4069 }
4070 #endif /* DELAY_SLOTS */