OSDN Git Service

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