OSDN Git Service

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