Merge from vendor branch OPENSSL:
[dragonfly.git] / contrib / gcc-3.4 / gcc / reorg.c
1 /* Perform instruction reorganizations for delay slot filling.
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu).
5    Hacked by Michael Tiemann (tiemann@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA.  */
23
24 /* Instruction reorganization pass.
25
26    This pass runs after register allocation and final jump
27    optimization.  It should be the last pass to run before peephole.
28    It serves primarily to fill delay slots of insns, typically branch
29    and call insns.  Other insns typically involve more complicated
30    interactions of data dependencies and resource constraints, and
31    are better handled by scheduling before register allocation (by the
32    function `schedule_insns').
33
34    The Branch Penalty is the number of extra cycles that are needed to
35    execute a branch insn.  On an ideal machine, branches take a single
36    cycle, and the Branch Penalty is 0.  Several RISC machines approach
37    branch delays differently:
38
39    The MIPS 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               /* See comment in relax_delay_slots about necessity of using
2362                  next_real_insn here.  */
2363               rtx new_label = next_real_insn (next_trial);
2364
2365               if (new_label != 0)
2366                 new_label = get_label_before (new_label);
2367               else
2368                 new_label = find_end_label ();
2369
2370               delay_list
2371                 = add_to_delay_list (copy_rtx (next_trial), delay_list);
2372               slots_filled++;
2373               reorg_redirect_jump (trial, new_label);
2374
2375               /* If we merged because we both jumped to the same place,
2376                  redirect the original insn also.  */
2377               if (target)
2378                 reorg_redirect_jump (insn, new_label);
2379             }
2380         }
2381
2382       /* If this is an unconditional jump, then try to get insns from the
2383          target of the jump.  */
2384       if (GET_CODE (insn) == JUMP_INSN
2385           && simplejump_p (insn)
2386           && slots_filled != slots_to_fill)
2387         delay_list
2388           = fill_slots_from_thread (insn, const_true_rtx,
2389                                     next_active_insn (JUMP_LABEL (insn)),
2390                                     NULL, 1, 1,
2391                                     own_thread_p (JUMP_LABEL (insn),
2392                                                   JUMP_LABEL (insn), 0),
2393                                     slots_to_fill, &slots_filled,
2394                                     delay_list);
2395
2396       if (delay_list)
2397         unfilled_slots_base[i]
2398           = emit_delay_sequence (insn, delay_list, slots_filled);
2399
2400       if (slots_to_fill == slots_filled)
2401         unfilled_slots_base[i] = 0;
2402
2403       note_delay_statistics (slots_filled, 0);
2404     }
2405
2406 #ifdef DELAY_SLOTS_FOR_EPILOGUE
2407   /* See if the epilogue needs any delay slots.  Try to fill them if so.
2408      The only thing we can do is scan backwards from the end of the
2409      function.  If we did this in a previous pass, it is incorrect to do it
2410      again.  */
2411   if (current_function_epilogue_delay_list)
2412     return;
2413
2414   slots_to_fill = DELAY_SLOTS_FOR_EPILOGUE;
2415   if (slots_to_fill == 0)
2416     return;
2417
2418   slots_filled = 0;
2419   CLEAR_RESOURCE (&set);
2420
2421   /* The frame pointer and stack pointer are needed at the beginning of
2422      the epilogue, so instructions setting them can not be put in the
2423      epilogue delay slot.  However, everything else needed at function
2424      end is safe, so we don't want to use end_of_function_needs here.  */
2425   CLEAR_RESOURCE (&needed);
2426   if (frame_pointer_needed)
2427     {
2428       SET_HARD_REG_BIT (needed.regs, FRAME_POINTER_REGNUM);
2429 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2430       SET_HARD_REG_BIT (needed.regs, HARD_FRAME_POINTER_REGNUM);
2431 #endif
2432       if (! EXIT_IGNORE_STACK
2433           || current_function_sp_is_unchanging)
2434         SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2435     }
2436   else
2437     SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2438
2439 #ifdef EPILOGUE_USES
2440   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2441     {
2442       if (EPILOGUE_USES (i))
2443         SET_HARD_REG_BIT (needed.regs, i);
2444     }
2445 #endif
2446
2447   for (trial = get_last_insn (); ! stop_search_p (trial, 1);
2448        trial = PREV_INSN (trial))
2449     {
2450       if (GET_CODE (trial) == NOTE)
2451         continue;
2452       pat = PATTERN (trial);
2453       if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2454         continue;
2455
2456       if (! insn_references_resource_p (trial, &set, 1)
2457           && ! insn_sets_resource_p (trial, &needed, 1)
2458           && ! insn_sets_resource_p (trial, &set, 1)
2459 #ifdef HAVE_cc0
2460           /* Don't want to mess with cc0 here.  */
2461           && ! reg_mentioned_p (cc0_rtx, pat)
2462 #endif
2463           && ! can_throw_internal (trial))
2464         {
2465           trial = try_split (pat, trial, 1);
2466           if (ELIGIBLE_FOR_EPILOGUE_DELAY (trial, slots_filled))
2467             {
2468               /* Here as well we are searching backward, so put the
2469                  insns we find on the head of the list.  */
2470
2471               current_function_epilogue_delay_list
2472                 = gen_rtx_INSN_LIST (VOIDmode, trial,
2473                                      current_function_epilogue_delay_list);
2474               mark_end_of_function_resources (trial, 1);
2475               update_block (trial, trial);
2476               delete_related_insns (trial);
2477
2478               /* Clear deleted bit so final.c will output the insn.  */
2479               INSN_DELETED_P (trial) = 0;
2480
2481               if (slots_to_fill == ++slots_filled)
2482                 break;
2483               continue;
2484             }
2485         }
2486
2487       mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2488       mark_referenced_resources (trial, &needed, 1);
2489     }
2490
2491   note_delay_statistics (slots_filled, 0);
2492 #endif
2493 }
2494 \f
2495 /* Try to find insns to place in delay slots.
2496
2497    INSN is the jump needing SLOTS_TO_FILL delay slots.  It tests CONDITION
2498    or is an unconditional branch if CONDITION is const_true_rtx.
2499    *PSLOTS_FILLED is updated with the number of slots that we have filled.
2500
2501    THREAD is a flow-of-control, either the insns to be executed if the
2502    branch is true or if the branch is false, THREAD_IF_TRUE says which.
2503
2504    OPPOSITE_THREAD is the thread in the opposite direction.  It is used
2505    to see if any potential delay slot insns set things needed there.
2506
2507    LIKELY is nonzero if it is extremely likely that the branch will be
2508    taken and THREAD_IF_TRUE is set.  This is used for the branch at the
2509    end of a loop back up to the top.
2510
2511    OWN_THREAD and OWN_OPPOSITE_THREAD are true if we are the only user of the
2512    thread.  I.e., it is the fallthrough code of our jump or the target of the
2513    jump when we are the only jump going there.
2514
2515    If OWN_THREAD is false, it must be the "true" thread of a jump.  In that
2516    case, we can only take insns from the head of the thread for our delay
2517    slot.  We then adjust the jump to point after the insns we have taken.  */
2518
2519 static rtx
2520 fill_slots_from_thread (rtx insn, rtx condition, rtx thread,
2521                         rtx opposite_thread, int likely, int thread_if_true,
2522                         int own_thread, int slots_to_fill,
2523                         int *pslots_filled, rtx delay_list)
2524 {
2525   rtx new_thread;
2526   struct resources opposite_needed, set, needed;
2527   rtx trial;
2528   int lose = 0;
2529   int must_annul = 0;
2530   int flags;
2531
2532   /* Validate our arguments.  */
2533   if ((condition == const_true_rtx && ! thread_if_true)
2534       || (! own_thread && ! thread_if_true))
2535     abort ();
2536
2537   flags = get_jump_flags (insn, JUMP_LABEL (insn));
2538
2539   /* If our thread is the end of subroutine, we can't get any delay
2540      insns from that.  */
2541   if (thread == 0)
2542     return delay_list;
2543
2544   /* If this is an unconditional branch, nothing is needed at the
2545      opposite thread.  Otherwise, compute what is needed there.  */
2546   if (condition == const_true_rtx)
2547     CLEAR_RESOURCE (&opposite_needed);
2548   else
2549     mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2550
2551   /* If the insn at THREAD can be split, do it here to avoid having to
2552      update THREAD and NEW_THREAD if it is done in the loop below.  Also
2553      initialize NEW_THREAD.  */
2554
2555   new_thread = thread = try_split (PATTERN (thread), thread, 0);
2556
2557   /* Scan insns at THREAD.  We are looking for an insn that can be removed
2558      from THREAD (it neither sets nor references resources that were set
2559      ahead of it and it doesn't set anything needs by the insns ahead of
2560      it) and that either can be placed in an annulling insn or aren't
2561      needed at OPPOSITE_THREAD.  */
2562
2563   CLEAR_RESOURCE (&needed);
2564   CLEAR_RESOURCE (&set);
2565
2566   /* If we do not own this thread, we must stop as soon as we find
2567      something that we can't put in a delay slot, since all we can do
2568      is branch into THREAD at a later point.  Therefore, labels stop
2569      the search if this is not the `true' thread.  */
2570
2571   for (trial = thread;
2572        ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2573        trial = next_nonnote_insn (trial))
2574     {
2575       rtx pat, old_trial;
2576
2577       /* If we have passed a label, we no longer own this thread.  */
2578       if (GET_CODE (trial) == CODE_LABEL)
2579         {
2580           own_thread = 0;
2581           continue;
2582         }
2583
2584       pat = PATTERN (trial);
2585       if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2586         continue;
2587
2588       /* If TRIAL conflicts with the insns ahead of it, we lose.  Also,
2589          don't separate or copy insns that set and use CC0.  */
2590       if (! insn_references_resource_p (trial, &set, 1)
2591           && ! insn_sets_resource_p (trial, &set, 1)
2592           && ! insn_sets_resource_p (trial, &needed, 1)
2593 #ifdef HAVE_cc0
2594           && ! (reg_mentioned_p (cc0_rtx, pat)
2595                 && (! own_thread || ! sets_cc0_p (pat)))
2596 #endif
2597           && ! can_throw_internal (trial))
2598         {
2599           rtx prior_insn;
2600
2601           /* If TRIAL is redundant with some insn before INSN, we don't
2602              actually need to add it to the delay list; we can merely pretend
2603              we did.  */
2604           if ((prior_insn = redundant_insn (trial, insn, delay_list)))
2605             {
2606               fix_reg_dead_note (prior_insn, insn);
2607               if (own_thread)
2608                 {
2609                   update_block (trial, thread);
2610                   if (trial == thread)
2611                     {
2612                       thread = next_active_insn (thread);
2613                       if (new_thread == trial)
2614                         new_thread = thread;
2615                     }
2616
2617                   delete_related_insns (trial);
2618                 }
2619               else
2620                 {
2621                   update_reg_unused_notes (prior_insn, trial);
2622                   new_thread = next_active_insn (trial);
2623                 }
2624
2625               continue;
2626             }
2627
2628           /* There are two ways we can win:  If TRIAL doesn't set anything
2629              needed at the opposite thread and can't trap, or if it can
2630              go into an annulled delay slot.  */
2631           if (!must_annul
2632               && (condition == const_true_rtx
2633                   || (! insn_sets_resource_p (trial, &opposite_needed, 1)
2634                       && ! may_trap_p (pat))))
2635             {
2636               old_trial = trial;
2637               trial = try_split (pat, trial, 0);
2638               if (new_thread == old_trial)
2639                 new_thread = trial;
2640               if (thread == old_trial)
2641                 thread = trial;
2642               pat = PATTERN (trial);
2643               if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2644                 goto winner;
2645             }
2646           else if (0
2647 #ifdef ANNUL_IFTRUE_SLOTS
2648                    || ! thread_if_true
2649 #endif
2650 #ifdef ANNUL_IFFALSE_SLOTS
2651                    || thread_if_true
2652 #endif
2653                    )
2654             {
2655               old_trial = trial;
2656               trial = try_split (pat, trial, 0);
2657               if (new_thread == old_trial)
2658                 new_thread = trial;
2659               if (thread == old_trial)
2660                 thread = trial;
2661               pat = PATTERN (trial);
2662               if ((must_annul || delay_list == NULL) && (thread_if_true
2663                    ? check_annul_list_true_false (0, delay_list)
2664                      && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2665                    : check_annul_list_true_false (1, delay_list)
2666                      && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2667                 {
2668                   rtx temp;
2669
2670                   must_annul = 1;
2671                 winner:
2672
2673 #ifdef HAVE_cc0
2674                   if (reg_mentioned_p (cc0_rtx, pat))
2675                     link_cc0_insns (trial);
2676 #endif
2677
2678                   /* If we own this thread, delete the insn.  If this is the
2679                      destination of a branch, show that a basic block status
2680                      may have been updated.  In any case, mark the new
2681                      starting point of this thread.  */
2682                   if (own_thread)
2683                     {
2684                       rtx note;
2685
2686                       update_block (trial, thread);
2687                       if (trial == thread)
2688                         {
2689                           thread = next_active_insn (thread);
2690                           if (new_thread == trial)
2691                             new_thread = thread;
2692                         }
2693
2694                       /* We are moving this insn, not deleting it.  We must
2695                          temporarily increment the use count on any referenced
2696                          label lest it be deleted by delete_related_insns.  */
2697                       note = find_reg_note (trial, REG_LABEL, 0);
2698                       /* REG_LABEL could be NOTE_INSN_DELETED_LABEL too.  */
2699                       if (note && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2700                         LABEL_NUSES (XEXP (note, 0))++;
2701
2702                       delete_related_insns (trial);
2703
2704                       if (note && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2705                         LABEL_NUSES (XEXP (note, 0))--;
2706                     }
2707                   else
2708                     new_thread = next_active_insn (trial);
2709
2710                   temp = own_thread ? trial : copy_rtx (trial);
2711                   if (thread_if_true)
2712                     INSN_FROM_TARGET_P (temp) = 1;
2713
2714                   delay_list = add_to_delay_list (temp, delay_list);
2715
2716                   if (slots_to_fill == ++(*pslots_filled))
2717                     {
2718                       /* Even though we have filled all the slots, we
2719                          may be branching to a location that has a
2720                          redundant insn.  Skip any if so.  */
2721                       while (new_thread && ! own_thread
2722                              && ! insn_sets_resource_p (new_thread, &set, 1)
2723                              && ! insn_sets_resource_p (new_thread, &needed, 1)
2724                              && ! insn_references_resource_p (new_thread,
2725                                                               &set, 1)
2726                              && (prior_insn
2727                                  = redundant_insn (new_thread, insn,
2728                                                    delay_list)))
2729                         {
2730                           /* We know we do not own the thread, so no need
2731                              to call update_block and delete_insn.  */
2732                           fix_reg_dead_note (prior_insn, insn);
2733                           update_reg_unused_notes (prior_insn, new_thread);
2734                           new_thread = next_active_insn (new_thread);
2735                         }
2736                       break;
2737                     }
2738
2739                   continue;
2740                 }
2741             }
2742         }
2743
2744       /* This insn can't go into a delay slot.  */
2745       lose = 1;
2746       mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2747       mark_referenced_resources (trial, &needed, 1);
2748
2749       /* Ensure we don't put insns between the setting of cc and the comparison
2750          by moving a setting of cc into an earlier delay slot since these insns
2751          could clobber the condition code.  */
2752       set.cc = 1;
2753
2754       /* If this insn is a register-register copy and the next insn has
2755          a use of our destination, change it to use our source.  That way,
2756          it will become a candidate for our delay slot the next time
2757          through this loop.  This case occurs commonly in loops that
2758          scan a list.
2759
2760          We could check for more complex cases than those tested below,
2761          but it doesn't seem worth it.  It might also be a good idea to try
2762          to swap the two insns.  That might do better.
2763
2764          We can't do this if the next insn modifies our destination, because
2765          that would make the replacement into the insn invalid.  We also can't
2766          do this if it modifies our source, because it might be an earlyclobber
2767          operand.  This latter test also prevents updating the contents of
2768          a PRE_INC.  We also can't do this if there's overlap of source and
2769          destination.  Overlap may happen for larger-than-register-size modes.  */
2770
2771       if (GET_CODE (trial) == INSN && GET_CODE (pat) == SET
2772           && GET_CODE (SET_SRC (pat)) == REG
2773           && GET_CODE (SET_DEST (pat)) == REG
2774           && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2775         {
2776           rtx next = next_nonnote_insn (trial);
2777
2778           if (next && GET_CODE (next) == INSN
2779               && GET_CODE (PATTERN (next)) != USE
2780               && ! reg_set_p (SET_DEST (pat), next)
2781               && ! reg_set_p (SET_SRC (pat), next)
2782               && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2783               && ! modified_in_p (SET_DEST (pat), next))
2784             validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2785         }
2786     }
2787
2788   /* If we stopped on a branch insn that has delay slots, see if we can
2789      steal some of the insns in those slots.  */
2790   if (trial && GET_CODE (trial) == INSN
2791       && GET_CODE (PATTERN (trial)) == SEQUENCE
2792       && GET_CODE (XVECEXP (PATTERN (trial), 0, 0)) == JUMP_INSN)
2793     {
2794       /* If this is the `true' thread, we will want to follow the jump,
2795          so we can only do this if we have taken everything up to here.  */
2796       if (thread_if_true && trial == new_thread)
2797         {
2798           delay_list
2799             = steal_delay_list_from_target (insn, condition, PATTERN (trial),
2800                                             delay_list, &set, &needed,
2801                                             &opposite_needed, slots_to_fill,
2802                                             pslots_filled, &must_annul,
2803                                             &new_thread);
2804           /* If we owned the thread and are told that it branched
2805              elsewhere, make sure we own the thread at the new location.  */
2806           if (own_thread && trial != new_thread)
2807             own_thread = own_thread_p (new_thread, new_thread, 0);
2808         }
2809       else if (! thread_if_true)
2810         delay_list
2811           = steal_delay_list_from_fallthrough (insn, condition,
2812                                                PATTERN (trial),
2813                                                delay_list, &set, &needed,
2814                                                &opposite_needed, slots_to_fill,
2815                                                pslots_filled, &must_annul);
2816     }
2817
2818   /* If we haven't found anything for this delay slot and it is very
2819      likely that the branch will be taken, see if the insn at our target
2820      increments or decrements a register with an increment that does not
2821      depend on the destination register.  If so, try to place the opposite
2822      arithmetic insn after the jump insn and put the arithmetic insn in the
2823      delay slot.  If we can't do this, return.  */
2824   if (delay_list == 0 && likely && new_thread
2825       && GET_CODE (new_thread) == INSN
2826       && GET_CODE (PATTERN (new_thread)) != ASM_INPUT
2827       && asm_noperands (PATTERN (new_thread)) < 0)
2828     {
2829       rtx pat = PATTERN (new_thread);
2830       rtx dest;
2831       rtx src;
2832
2833       trial = new_thread;
2834       pat = PATTERN (trial);
2835
2836       if (GET_CODE (trial) != INSN
2837           || GET_CODE (pat) != SET
2838           || ! eligible_for_delay (insn, 0, trial, flags)
2839           || can_throw_internal (trial))
2840         return 0;
2841
2842       dest = SET_DEST (pat), src = SET_SRC (pat);
2843       if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2844           && rtx_equal_p (XEXP (src, 0), dest)
2845           && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2846           && ! side_effects_p (pat))
2847         {
2848           rtx other = XEXP (src, 1);
2849           rtx new_arith;
2850           rtx ninsn;
2851
2852           /* If this is a constant adjustment, use the same code with
2853              the negated constant.  Otherwise, reverse the sense of the
2854              arithmetic.  */
2855           if (GET_CODE (other) == CONST_INT)
2856             new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2857                                         negate_rtx (GET_MODE (src), other));
2858           else
2859             new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2860                                         GET_MODE (src), dest, other);
2861
2862           ninsn = emit_insn_after (gen_rtx_SET (VOIDmode, dest, new_arith),
2863                                    insn);
2864
2865           if (recog_memoized (ninsn) < 0
2866               || (extract_insn (ninsn), ! constrain_operands (1)))
2867             {
2868               delete_related_insns (ninsn);
2869               return 0;
2870             }
2871
2872           if (own_thread)
2873             {
2874               update_block (trial, thread);
2875               if (trial == thread)
2876                 {
2877                   thread = next_active_insn (thread);
2878                   if (new_thread == trial)
2879                     new_thread = thread;
2880                 }
2881               delete_related_insns (trial);
2882             }
2883           else
2884             new_thread = next_active_insn (trial);
2885
2886           ninsn = own_thread ? trial : copy_rtx (trial);
2887           if (thread_if_true)
2888             INSN_FROM_TARGET_P (ninsn) = 1;
2889
2890           delay_list = add_to_delay_list (ninsn, NULL_RTX);
2891           (*pslots_filled)++;
2892         }
2893     }
2894
2895   if (delay_list && must_annul)
2896     INSN_ANNULLED_BRANCH_P (insn) = 1;
2897
2898   /* If we are to branch into the middle of this thread, find an appropriate
2899      label or make a new one if none, and redirect INSN to it.  If we hit the
2900      end of the function, use the end-of-function label.  */
2901   if (new_thread != thread)
2902     {
2903       rtx label;
2904
2905       if (! thread_if_true)
2906         abort ();
2907
2908       if (new_thread && GET_CODE (new_thread) == JUMP_INSN
2909           && (simplejump_p (new_thread)
2910               || GET_CODE (PATTERN (new_thread)) == RETURN)
2911           && redirect_with_delay_list_safe_p (insn,
2912                                               JUMP_LABEL (new_thread),
2913                                               delay_list))
2914         new_thread = follow_jumps (JUMP_LABEL (new_thread));
2915
2916       if (new_thread == 0)
2917         label = find_end_label ();
2918       else if (GET_CODE (new_thread) == CODE_LABEL)
2919         label = new_thread;
2920       else
2921         label = get_label_before (new_thread);
2922
2923       reorg_redirect_jump (insn, label);
2924     }
2925
2926   return delay_list;
2927 }
2928 \f
2929 /* Make another attempt to find insns to place in delay slots.
2930
2931    We previously looked for insns located in front of the delay insn
2932    and, for non-jump delay insns, located behind the delay insn.
2933
2934    Here only try to schedule jump insns and try to move insns from either
2935    the target or the following insns into the delay slot.  If annulling is
2936    supported, we will be likely to do this.  Otherwise, we can do this only
2937    if safe.  */
2938
2939 static void
2940 fill_eager_delay_slots (void)
2941 {
2942   rtx insn;
2943   int i;
2944   int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2945
2946   for (i = 0; i < num_unfilled_slots; i++)
2947     {
2948       rtx condition;
2949       rtx target_label, insn_at_target, fallthrough_insn;
2950       rtx delay_list = 0;
2951       int own_target;
2952       int own_fallthrough;
2953       int prediction, slots_to_fill, slots_filled;
2954
2955       insn = unfilled_slots_base[i];
2956       if (insn == 0
2957           || INSN_DELETED_P (insn)
2958           || GET_CODE (insn) != JUMP_INSN
2959           || ! (condjump_p (insn) || condjump_in_parallel_p (insn)))
2960         continue;
2961
2962       slots_to_fill = num_delay_slots (insn);
2963       /* Some machine description have defined instructions to have
2964          delay slots only in certain circumstances which may depend on
2965          nearby insns (which change due to reorg's actions).
2966
2967          For example, the PA port normally has delay slots for unconditional
2968          jumps.
2969
2970          However, the PA port claims such jumps do not have a delay slot
2971          if they are immediate successors of certain CALL_INSNs.  This
2972          allows the port to favor filling the delay slot of the call with
2973          the unconditional jump.  */
2974       if (slots_to_fill == 0)
2975         continue;
2976
2977       slots_filled = 0;
2978       target_label = JUMP_LABEL (insn);
2979       condition = get_branch_condition (insn, target_label);
2980
2981       if (condition == 0)
2982         continue;
2983
2984       /* Get the next active fallthrough and target insns and see if we own
2985          them.  Then see whether the branch is likely true.  We don't need
2986          to do a lot of this for unconditional branches.  */
2987
2988       insn_at_target = next_active_insn (target_label);
2989       own_target = own_thread_p (target_label, target_label, 0);
2990
2991       if (condition == const_true_rtx)
2992         {
2993           own_fallthrough = 0;
2994           fallthrough_insn = 0;
2995           prediction = 2;
2996         }
2997       else
2998         {
2999           fallthrough_insn = next_active_insn (insn);
3000           own_fallthrough = own_thread_p (NEXT_INSN (insn), NULL_RTX, 1);
3001           prediction = mostly_true_jump (insn, condition);
3002         }
3003
3004       /* If this insn is expected to branch, first try to get insns from our
3005          target, then our fallthrough insns.  If it is not expected to branch,
3006          try the other order.  */
3007
3008       if (prediction > 0)
3009         {
3010           delay_list
3011             = fill_slots_from_thread (insn, condition, insn_at_target,
3012                                       fallthrough_insn, prediction == 2, 1,
3013                                       own_target,
3014                                       slots_to_fill, &slots_filled, delay_list);
3015
3016           if (delay_list == 0 && own_fallthrough)
3017             {
3018               /* Even though we didn't find anything for delay slots,
3019                  we might have found a redundant insn which we deleted
3020                  from the thread that was filled.  So we have to recompute
3021                  the next insn at the target.  */
3022               target_label = JUMP_LABEL (insn);
3023               insn_at_target = next_active_insn (target_label);
3024
3025               delay_list
3026                 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3027                                           insn_at_target, 0, 0,
3028                                           own_fallthrough,
3029                                           slots_to_fill, &slots_filled,
3030                                           delay_list);
3031             }
3032         }
3033       else
3034         {
3035           if (own_fallthrough)
3036             delay_list
3037               = fill_slots_from_thread (insn, condition, fallthrough_insn,
3038                                         insn_at_target, 0, 0,
3039                                         own_fallthrough,
3040                                         slots_to_fill, &slots_filled,
3041                                         delay_list);
3042
3043           if (delay_list == 0)
3044             delay_list
3045               = fill_slots_from_thread (insn, condition, insn_at_target,
3046                                         next_active_insn (insn), 0, 1,
3047                                         own_target,
3048                                         slots_to_fill, &slots_filled,
3049                                         delay_list);
3050         }
3051
3052       if (delay_list)
3053         unfilled_slots_base[i]
3054           = emit_delay_sequence (insn, delay_list, slots_filled);
3055
3056       if (slots_to_fill == slots_filled)
3057         unfilled_slots_base[i] = 0;
3058
3059       note_delay_statistics (slots_filled, 1);
3060     }
3061 }
3062 \f
3063 /* Once we have tried two ways to fill a delay slot, make a pass over the
3064    code to try to improve the results and to do such things as more jump
3065    threading.  */
3066
3067 static void
3068 relax_delay_slots (rtx first)
3069 {
3070   rtx insn, next, pat;
3071   rtx trial, delay_insn, target_label;
3072
3073   /* Look at every JUMP_INSN and see if we can improve it.  */
3074   for (insn = first; insn; insn = next)
3075     {
3076       rtx other;
3077
3078       next = next_active_insn (insn);
3079
3080       /* If this is a jump insn, see if it now jumps to a jump, jumps to
3081          the next insn, or jumps to a label that is not the last of a
3082          group of consecutive labels.  */
3083       if (GET_CODE (insn) == JUMP_INSN
3084           && (condjump_p (insn) || condjump_in_parallel_p (insn))
3085           && (target_label = JUMP_LABEL (insn)) != 0)
3086         {
3087           target_label = follow_jumps (target_label);
3088           /* See comment further down why we must use next_real_insn here,
3089              instead of next_active_insn.  */
3090           target_label = prev_label (next_real_insn (target_label));
3091
3092           if (target_label == 0)
3093             target_label = find_end_label ();
3094
3095           if (next_active_insn (target_label) == next
3096               && ! condjump_in_parallel_p (insn))
3097             {
3098               delete_jump (insn);
3099               continue;
3100             }
3101
3102           if (target_label != JUMP_LABEL (insn))
3103             reorg_redirect_jump (insn, target_label);
3104
3105           /* See if this jump branches around an unconditional jump.
3106              If so, invert this jump and point it to the target of the
3107              second jump.  */
3108           if (next && GET_CODE (next) == JUMP_INSN
3109               && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3110               && next_active_insn (target_label) == next_active_insn (next)
3111               && no_labels_between_p (insn, next))
3112             {
3113               rtx label = JUMP_LABEL (next);
3114
3115               /* Be careful how we do this to avoid deleting code or
3116                  labels that are momentarily dead.  See similar optimization
3117                  in jump.c.
3118
3119                  We also need to ensure we properly handle the case when
3120                  invert_jump fails.  */
3121
3122               ++LABEL_NUSES (target_label);
3123               if (label)
3124                 ++LABEL_NUSES (label);
3125
3126               if (invert_jump (insn, label, 1))
3127                 {
3128                   delete_related_insns (next);
3129                   next = insn;
3130                 }
3131
3132               if (label)
3133                 --LABEL_NUSES (label);
3134
3135               if (--LABEL_NUSES (target_label) == 0)
3136                 delete_related_insns (target_label);
3137
3138               continue;
3139             }
3140         }
3141
3142       /* If this is an unconditional jump and the previous insn is a
3143          conditional jump, try reversing the condition of the previous
3144          insn and swapping our targets.  The next pass might be able to
3145          fill the slots.
3146
3147          Don't do this if we expect the conditional branch to be true, because
3148          we would then be making the more common case longer.  */
3149
3150       if (GET_CODE (insn) == JUMP_INSN
3151           && (simplejump_p (insn) || GET_CODE (PATTERN (insn)) == RETURN)
3152           && (other = prev_active_insn (insn)) != 0
3153           && (condjump_p (other) || condjump_in_parallel_p (other))
3154           && no_labels_between_p (other, insn)
3155           && 0 > mostly_true_jump (other,
3156                                    get_branch_condition (other,
3157                                                          JUMP_LABEL (other))))
3158         {
3159           rtx other_target = JUMP_LABEL (other);
3160           target_label = JUMP_LABEL (insn);
3161
3162           if (invert_jump (other, target_label, 0))
3163             reorg_redirect_jump (insn, other_target);
3164         }
3165
3166       /* Now look only at cases where we have filled a delay slot.  */
3167       if (GET_CODE (insn) != INSN
3168           || GET_CODE (PATTERN (insn)) != SEQUENCE)
3169         continue;
3170
3171       pat = PATTERN (insn);
3172       delay_insn = XVECEXP (pat, 0, 0);
3173
3174       /* See if the first insn in the delay slot is redundant with some
3175          previous insn.  Remove it from the delay slot if so; then set up
3176          to reprocess this insn.  */
3177       if (redundant_insn (XVECEXP (pat, 0, 1), delay_insn, 0))
3178         {
3179           delete_from_delay_slot (XVECEXP (pat, 0, 1));
3180           next = prev_active_insn (next);
3181           continue;
3182         }
3183
3184       /* See if we have a RETURN insn with a filled delay slot followed
3185          by a RETURN insn with an unfilled a delay slot.  If so, we can delete
3186          the first RETURN (but not it's delay insn).  This gives the same
3187          effect in fewer instructions.
3188
3189          Only do so if optimizing for size since this results in slower, but
3190          smaller code.  */
3191       if (optimize_size
3192           && GET_CODE (PATTERN (delay_insn)) == RETURN
3193           && next
3194           && GET_CODE (next) == JUMP_INSN
3195           && GET_CODE (PATTERN (next)) == RETURN)
3196         {
3197           rtx after;
3198           int i;
3199
3200           /* Delete the RETURN and just execute the delay list insns.
3201
3202              We do this by deleting the INSN containing the SEQUENCE, then
3203              re-emitting the insns separately, and then deleting the RETURN.
3204              This allows the count of the jump target to be properly
3205              decremented.  */
3206
3207           /* Clear the from target bit, since these insns are no longer
3208              in delay slots.  */
3209           for (i = 0; i < XVECLEN (pat, 0); i++)
3210             INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3211
3212           trial = PREV_INSN (insn);
3213           delete_related_insns (insn);
3214           if (GET_CODE (pat) != SEQUENCE)
3215             abort ();
3216           after = trial;
3217           for (i = 0; i < XVECLEN (pat, 0); i++)
3218             {
3219               rtx this_insn = XVECEXP (pat, 0, i);
3220               add_insn_after (this_insn, after);
3221               after = this_insn;
3222             }
3223           delete_scheduled_jump (delay_insn);
3224           continue;
3225         }
3226
3227       /* Now look only at the cases where we have a filled JUMP_INSN.  */
3228       if (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) != JUMP_INSN
3229           || ! (condjump_p (XVECEXP (PATTERN (insn), 0, 0))
3230                 || condjump_in_parallel_p (XVECEXP (PATTERN (insn), 0, 0))))
3231         continue;
3232
3233       target_label = JUMP_LABEL (delay_insn);
3234
3235       if (target_label)
3236         {
3237           /* If this jump goes to another unconditional jump, thread it, but
3238              don't convert a jump into a RETURN here.  */
3239           trial = follow_jumps (target_label);
3240           /* We use next_real_insn instead of next_active_insn, so that
3241              the special USE insns emitted by reorg won't be ignored.
3242              If they are ignored, then they will get deleted if target_label
3243              is now unreachable, and that would cause mark_target_live_regs
3244              to fail.  */
3245           trial = prev_label (next_real_insn (trial));
3246           if (trial == 0 && target_label != 0)
3247             trial = find_end_label ();
3248
3249           if (trial != target_label
3250               && redirect_with_delay_slots_safe_p (delay_insn, trial, insn))
3251             {
3252               reorg_redirect_jump (delay_insn, trial);
3253               target_label = trial;
3254             }
3255
3256           /* If the first insn at TARGET_LABEL is redundant with a previous
3257              insn, redirect the jump to the following insn process again.  */
3258           trial = next_active_insn (target_label);
3259           if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3260               && redundant_insn (trial, insn, 0)
3261               && ! can_throw_internal (trial))
3262             {
3263               rtx tmp;
3264
3265               /* Figure out where to emit the special USE insn so we don't
3266                  later incorrectly compute register live/death info.  */
3267               tmp = next_active_insn (trial);
3268               if (tmp == 0)
3269                 tmp = find_end_label ();
3270
3271               /* Insert the special USE insn and update dataflow info.  */
3272               update_block (trial, tmp);
3273
3274               /* Now emit a label before the special USE insn, and
3275                  redirect our jump to the new label.  */
3276               target_label = get_label_before (PREV_INSN (tmp));
3277               reorg_redirect_jump (delay_insn, target_label);
3278               next = insn;
3279               continue;
3280             }
3281
3282           /* Similarly, if it is an unconditional jump with one insn in its
3283              delay list and that insn is redundant, thread the jump.  */
3284           if (trial && GET_CODE (PATTERN (trial)) == SEQUENCE
3285               && XVECLEN (PATTERN (trial), 0) == 2
3286               && GET_CODE (XVECEXP (PATTERN (trial), 0, 0)) == JUMP_INSN
3287               && (simplejump_p (XVECEXP (PATTERN (trial), 0, 0))
3288                   || GET_CODE (PATTERN (XVECEXP (PATTERN (trial), 0, 0))) == RETURN)
3289               && redundant_insn (XVECEXP (PATTERN (trial), 0, 1), insn, 0))
3290             {
3291               target_label = JUMP_LABEL (XVECEXP (PATTERN (trial), 0, 0));
3292               if (target_label == 0)
3293                 target_label = find_end_label ();
3294
3295               if (redirect_with_delay_slots_safe_p (delay_insn, target_label,
3296                                                     insn))
3297                 {
3298                   reorg_redirect_jump (delay_insn, target_label);
3299                   next = insn;
3300                   continue;
3301                 }
3302             }
3303         }
3304
3305       if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3306           && prev_active_insn (target_label) == insn
3307           && ! condjump_in_parallel_p (delay_insn)
3308 #ifdef HAVE_cc0
3309           /* If the last insn in the delay slot sets CC0 for some insn,
3310              various code assumes that it is in a delay slot.  We could
3311              put it back where it belonged and delete the register notes,
3312              but it doesn't seem worthwhile in this uncommon case.  */
3313           && ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3314                               REG_CC_USER, NULL_RTX)
3315 #endif
3316           )
3317         {
3318           rtx after;
3319           int i;
3320
3321           /* All this insn does is execute its delay list and jump to the
3322              following insn.  So delete the jump and just execute the delay
3323              list insns.
3324
3325              We do this by deleting the INSN containing the SEQUENCE, then
3326              re-emitting the insns separately, and then deleting the jump.
3327              This allows the count of the jump target to be properly
3328              decremented.  */
3329
3330           /* Clear the from target bit, since these insns are no longer
3331              in delay slots.  */
3332           for (i = 0; i < XVECLEN (pat, 0); i++)
3333             INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3334
3335           trial = PREV_INSN (insn);
3336           delete_related_insns (insn);
3337           if (GET_CODE (pat) != SEQUENCE)
3338             abort ();
3339           after = trial;
3340           for (i = 0; i < XVECLEN (pat, 0); i++)
3341             {
3342               rtx this_insn = XVECEXP (pat, 0, i);
3343               add_insn_after (this_insn, after);
3344               after = this_insn;
3345             }
3346           delete_scheduled_jump (delay_insn);
3347           continue;
3348         }
3349
3350       /* See if this is an unconditional jump around a single insn which is
3351          identical to the one in its delay slot.  In this case, we can just
3352          delete the branch and the insn in its delay slot.  */
3353       if (next && GET_CODE (next) == INSN
3354           && prev_label (next_active_insn (next)) == target_label
3355           && simplejump_p (insn)
3356           && XVECLEN (pat, 0) == 2
3357           && rtx_equal_p (PATTERN (next), PATTERN (XVECEXP (pat, 0, 1))))
3358         {
3359           delete_related_insns (insn);
3360           continue;
3361         }
3362
3363       /* See if this jump (with its delay slots) branches around another
3364          jump (without delay slots).  If so, invert this jump and point
3365          it to the target of the second jump.  We cannot do this for
3366          annulled jumps, though.  Again, don't convert a jump to a RETURN
3367          here.  */
3368       if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3369           && next && GET_CODE (next) == JUMP_INSN
3370           && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3371           && next_active_insn (target_label) == next_active_insn (next)
3372           && no_labels_between_p (insn, next))
3373         {
3374           rtx label = JUMP_LABEL (next);
3375           rtx old_label = JUMP_LABEL (delay_insn);
3376
3377           if (label == 0)
3378             label = find_end_label ();
3379
3380           /* find_end_label can generate a new label. Check this first.  */
3381           if (no_labels_between_p (insn, next)
3382               && redirect_with_delay_slots_safe_p (delay_insn, label, insn))
3383             {
3384               /* Be careful how we do this to avoid deleting code or labels
3385                  that are momentarily dead.  See similar optimization in
3386                  jump.c  */
3387               if (old_label)
3388                 ++LABEL_NUSES (old_label);
3389
3390               if (invert_jump (delay_insn, label, 1))
3391                 {
3392                   int i;
3393
3394                   /* Must update the INSN_FROM_TARGET_P bits now that
3395                      the branch is reversed, so that mark_target_live_regs
3396                      will handle the delay slot insn correctly.  */
3397                   for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
3398                     {
3399                       rtx slot = XVECEXP (PATTERN (insn), 0, i);
3400                       INSN_FROM_TARGET_P (slot) = ! INSN_FROM_TARGET_P (slot);
3401                     }
3402
3403                   delete_related_insns (next);
3404                   next = insn;
3405                 }
3406
3407               if (old_label && --LABEL_NUSES (old_label) == 0)
3408                 delete_related_insns (old_label);
3409               continue;
3410             }
3411         }
3412
3413       /* If we own the thread opposite the way this insn branches, see if we
3414          can merge its delay slots with following insns.  */
3415       if (INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3416           && own_thread_p (NEXT_INSN (insn), 0, 1))
3417         try_merge_delay_insns (insn, next);
3418       else if (! INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3419                && own_thread_p (target_label, target_label, 0))
3420         try_merge_delay_insns (insn, next_active_insn (target_label));
3421
3422       /* If we get here, we haven't deleted INSN.  But we may have deleted
3423          NEXT, so recompute it.  */
3424       next = next_active_insn (insn);
3425     }
3426 }
3427 \f
3428 #ifdef HAVE_return
3429
3430 /* Look for filled jumps to the end of function label.  We can try to convert
3431    them into RETURN insns if the insns in the delay slot are valid for the
3432    RETURN as well.  */
3433
3434 static void
3435 make_return_insns (rtx first)
3436 {
3437   rtx insn, jump_insn, pat;
3438   rtx real_return_label = end_of_function_label;
3439   int slots, i;
3440
3441 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3442   /* If a previous pass filled delay slots in the epilogue, things get a
3443      bit more complicated, as those filler insns would generally (without
3444      data flow analysis) have to be executed after any existing branch
3445      delay slot filler insns.  It is also unknown whether such a
3446      transformation would actually be profitable.  Note that the existing
3447      code only cares for branches with (some) filled delay slots.  */
3448   if (current_function_epilogue_delay_list != NULL)
3449     return;
3450 #endif
3451
3452   /* See if there is a RETURN insn in the function other than the one we
3453      made for END_OF_FUNCTION_LABEL.  If so, set up anything we can't change
3454      into a RETURN to jump to it.  */
3455   for (insn = first; insn; insn = NEXT_INSN (insn))
3456     if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) == RETURN)
3457       {
3458         real_return_label = get_label_before (insn);
3459         break;
3460       }
3461
3462   /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3463      was equal to END_OF_FUNCTION_LABEL.  */
3464   LABEL_NUSES (real_return_label)++;
3465
3466   /* Clear the list of insns to fill so we can use it.  */
3467   obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3468
3469   for (insn = first; insn; insn = NEXT_INSN (insn))
3470     {
3471       int flags;
3472
3473       /* Only look at filled JUMP_INSNs that go to the end of function
3474          label.  */
3475       if (GET_CODE (insn) != INSN
3476           || GET_CODE (PATTERN (insn)) != SEQUENCE
3477           || GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) != JUMP_INSN
3478           || JUMP_LABEL (XVECEXP (PATTERN (insn), 0, 0)) != end_of_function_label)
3479         continue;
3480
3481       pat = PATTERN (insn);
3482       jump_insn = XVECEXP (pat, 0, 0);
3483
3484       /* If we can't make the jump into a RETURN, try to redirect it to the best
3485          RETURN and go on to the next insn.  */
3486       if (! reorg_redirect_jump (jump_insn, NULL_RTX))
3487         {
3488           /* Make sure redirecting the jump will not invalidate the delay
3489              slot insns.  */
3490           if (redirect_with_delay_slots_safe_p (jump_insn,
3491                                                 real_return_label,
3492                                                 insn))
3493             reorg_redirect_jump (jump_insn, real_return_label);
3494           continue;
3495         }
3496
3497       /* See if this RETURN can accept the insns current in its delay slot.
3498          It can if it has more or an equal number of slots and the contents
3499          of each is valid.  */
3500
3501       flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3502       slots = num_delay_slots (jump_insn);
3503       if (slots >= XVECLEN (pat, 0) - 1)
3504         {
3505           for (i = 1; i < XVECLEN (pat, 0); i++)
3506             if (! (
3507 #ifdef ANNUL_IFFALSE_SLOTS
3508                    (INSN_ANNULLED_BRANCH_P (jump_insn)
3509                     && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3510                    ? eligible_for_annul_false (jump_insn, i - 1,
3511                                                XVECEXP (pat, 0, i), flags) :
3512 #endif
3513 #ifdef ANNUL_IFTRUE_SLOTS
3514                    (INSN_ANNULLED_BRANCH_P (jump_insn)
3515                     && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3516                    ? eligible_for_annul_true (jump_insn, i - 1,
3517                                               XVECEXP (pat, 0, i), flags) :
3518 #endif
3519                    eligible_for_delay (jump_insn, i - 1,
3520                                        XVECEXP (pat, 0, i), flags)))
3521               break;
3522         }
3523       else
3524         i = 0;
3525
3526       if (i == XVECLEN (pat, 0))
3527         continue;
3528
3529       /* We have to do something with this insn.  If it is an unconditional
3530          RETURN, delete the SEQUENCE and output the individual insns,
3531          followed by the RETURN.  Then set things up so we try to find
3532          insns for its delay slots, if it needs some.  */
3533       if (GET_CODE (PATTERN (jump_insn)) == RETURN)
3534         {
3535           rtx prev = PREV_INSN (insn);
3536
3537           delete_related_insns (insn);
3538           for (i = 1; i < XVECLEN (pat, 0); i++)
3539             prev = emit_insn_after (PATTERN (XVECEXP (pat, 0, i)), prev);
3540
3541           insn = emit_jump_insn_after (PATTERN (jump_insn), prev);
3542           emit_barrier_after (insn);
3543
3544           if (slots)
3545             obstack_ptr_grow (&unfilled_slots_obstack, insn);
3546         }
3547       else
3548         /* It is probably more efficient to keep this with its current
3549            delay slot as a branch to a RETURN.  */
3550         reorg_redirect_jump (jump_insn, real_return_label);
3551     }
3552
3553   /* Now delete REAL_RETURN_LABEL if we never used it.  Then try to fill any
3554      new delay slots we have created.  */
3555   if (--LABEL_NUSES (real_return_label) == 0)
3556     delete_related_insns (real_return_label);
3557
3558   fill_simple_delay_slots (1);
3559   fill_simple_delay_slots (0);
3560 }
3561 #endif
3562 \f
3563 /* Try to find insns to place in delay slots.  */
3564
3565 void
3566 dbr_schedule (rtx first, FILE *file)
3567 {
3568   rtx insn, next, epilogue_insn = 0;
3569   int i;
3570 #if 0
3571   int old_flag_no_peephole = flag_no_peephole;
3572
3573   /* Execute `final' once in prescan mode to delete any insns that won't be
3574      used.  Don't let final try to do any peephole optimization--it will
3575      ruin dataflow information for this pass.  */
3576
3577   flag_no_peephole = 1;
3578   final (first, 0, NO_DEBUG, 1, 1);
3579   flag_no_peephole = old_flag_no_peephole;
3580 #endif
3581
3582   /* If the current function has no insns other than the prologue and
3583      epilogue, then do not try to fill any delay slots.  */
3584   if (n_basic_blocks == 0)
3585     return;
3586
3587   /* Find the highest INSN_UID and allocate and initialize our map from
3588      INSN_UID's to position in code.  */
3589   for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3590     {
3591       if (INSN_UID (insn) > max_uid)
3592         max_uid = INSN_UID (insn);
3593       if (GET_CODE (insn) == NOTE
3594           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
3595         epilogue_insn = insn;
3596     }
3597
3598   uid_to_ruid = xmalloc ((max_uid + 1) * sizeof (int));
3599   for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3600     uid_to_ruid[INSN_UID (insn)] = i;
3601
3602   /* Initialize the list of insns that need filling.  */
3603   if (unfilled_firstobj == 0)
3604     {
3605       gcc_obstack_init (&unfilled_slots_obstack);
3606       unfilled_firstobj = obstack_alloc (&unfilled_slots_obstack, 0);
3607     }
3608
3609   for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3610     {
3611       rtx target;
3612
3613       INSN_ANNULLED_BRANCH_P (insn) = 0;
3614       INSN_FROM_TARGET_P (insn) = 0;
3615
3616       /* Skip vector tables.  We can't get attributes for them.  */
3617       if (GET_CODE (insn) == JUMP_INSN
3618           && (GET_CODE (PATTERN (insn)) == ADDR_VEC
3619               || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
3620         continue;
3621
3622       if (num_delay_slots (insn) > 0)
3623         obstack_ptr_grow (&unfilled_slots_obstack, insn);
3624
3625       /* Ensure all jumps go to the last of a set of consecutive labels.  */
3626       if (GET_CODE (insn) == JUMP_INSN
3627           && (condjump_p (insn) || condjump_in_parallel_p (insn))
3628           && JUMP_LABEL (insn) != 0
3629           && ((target = prev_label (next_active_insn (JUMP_LABEL (insn))))
3630               != JUMP_LABEL (insn)))
3631         redirect_jump (insn, target, 1);
3632     }
3633
3634   init_resource_info (epilogue_insn);
3635
3636   /* Show we haven't computed an end-of-function label yet.  */
3637   end_of_function_label = 0;
3638
3639   /* Initialize the statistics for this function.  */
3640   memset (num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3641   memset (num_filled_delays, 0, sizeof num_filled_delays);
3642
3643   /* Now do the delay slot filling.  Try everything twice in case earlier
3644      changes make more slots fillable.  */
3645
3646   for (reorg_pass_number = 0;
3647        reorg_pass_number < MAX_REORG_PASSES;
3648        reorg_pass_number++)
3649     {
3650       fill_simple_delay_slots (1);
3651       fill_simple_delay_slots (0);
3652       fill_eager_delay_slots ();
3653       relax_delay_slots (first);
3654     }
3655
3656   /* Delete any USE insns made by update_block; subsequent passes don't need
3657      them or know how to deal with them.  */
3658   for (insn = first; insn; insn = next)
3659     {
3660       next = NEXT_INSN (insn);
3661
3662       if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE
3663           && INSN_P (XEXP (PATTERN (insn), 0)))
3664         next = delete_related_insns (insn);
3665     }
3666
3667   /* If we made an end of function label, indicate that it is now
3668      safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3669      If it is now unused, delete it.  */
3670   if (end_of_function_label && --LABEL_NUSES (end_of_function_label) == 0)
3671     delete_related_insns (end_of_function_label);
3672
3673 #ifdef HAVE_return
3674   if (HAVE_return && end_of_function_label != 0)
3675     make_return_insns (first);
3676 #endif
3677
3678   obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3679
3680   /* It is not clear why the line below is needed, but it does seem to be.  */
3681   unfilled_firstobj = obstack_alloc (&unfilled_slots_obstack, 0);
3682
3683   if (file)
3684     {
3685       int i, j, need_comma;
3686       int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3687       int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
3688
3689       for (reorg_pass_number = 0;
3690            reorg_pass_number < MAX_REORG_PASSES;
3691            reorg_pass_number++)
3692         {
3693           fprintf (file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
3694           for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
3695             {
3696               need_comma = 0;
3697               fprintf (file, ";; Reorg function #%d\n", i);
3698
3699               fprintf (file, ";; %d insns needing delay slots\n;; ",
3700                        num_insns_needing_delays[i][reorg_pass_number]);
3701
3702               for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3703                 if (num_filled_delays[i][j][reorg_pass_number])
3704                   {
3705                     if (need_comma)
3706                       fprintf (file, ", ");
3707                     need_comma = 1;
3708                     fprintf (file, "%d got %d delays",
3709                              num_filled_delays[i][j][reorg_pass_number], j);
3710                   }
3711               fprintf (file, "\n");
3712             }
3713         }
3714       memset (total_delay_slots, 0, sizeof total_delay_slots);
3715       memset (total_annul_slots, 0, sizeof total_annul_slots);
3716       for (insn = first; insn; insn = NEXT_INSN (insn))
3717         {
3718           if (! INSN_DELETED_P (insn)
3719               && GET_CODE (insn) == INSN
3720               && GET_CODE (PATTERN (insn)) != USE
3721               && GET_CODE (PATTERN (insn)) != CLOBBER)
3722             {
3723               if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3724                 {
3725                   j = XVECLEN (PATTERN (insn), 0) - 1;
3726                   if (j > MAX_DELAY_HISTOGRAM)
3727                     j = MAX_DELAY_HISTOGRAM;
3728                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (insn), 0, 0)))
3729                     total_annul_slots[j]++;
3730                   else
3731                     total_delay_slots[j]++;
3732                 }
3733               else if (num_delay_slots (insn) > 0)
3734                 total_delay_slots[0]++;
3735             }
3736         }
3737       fprintf (file, ";; Reorg totals: ");
3738       need_comma = 0;
3739       for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3740         {
3741           if (total_delay_slots[j])
3742             {
3743               if (need_comma)
3744                 fprintf (file, ", ");
3745               need_comma = 1;
3746               fprintf (file, "%d got %d delays", total_delay_slots[j], j);
3747             }
3748         }
3749       fprintf (file, "\n");
3750 #if defined (ANNUL_IFTRUE_SLOTS) || defined (ANNUL_IFFALSE_SLOTS)
3751       fprintf (file, ";; Reorg annuls: ");
3752       need_comma = 0;
3753       for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3754         {
3755           if (total_annul_slots[j])
3756             {
3757               if (need_comma)
3758                 fprintf (file, ", ");
3759               need_comma = 1;
3760               fprintf (file, "%d got %d delays", total_annul_slots[j], j);
3761             }
3762         }
3763       fprintf (file, "\n");
3764 #endif
3765       fprintf (file, "\n");
3766     }
3767
3768   /* For all JUMP insns, fill in branch prediction notes, so that during
3769      assembler output a target can set branch prediction bits in the code.
3770      We have to do this now, as up until this point the destinations of
3771      JUMPS can be moved around and changed, but past right here that cannot
3772      happen.  */
3773   for (insn = first; insn; insn = NEXT_INSN (insn))
3774     {
3775       int pred_flags;
3776
3777       if (GET_CODE (insn) == INSN)
3778         {
3779           rtx pat = PATTERN (insn);
3780
3781           if (GET_CODE (pat) == SEQUENCE)
3782             insn = XVECEXP (pat, 0, 0);
3783         }
3784       if (GET_CODE (insn) != JUMP_INSN)
3785         continue;
3786
3787       pred_flags = get_jump_flags (insn, JUMP_LABEL (insn));
3788       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_BR_PRED,
3789                                             GEN_INT (pred_flags),
3790                                             REG_NOTES (insn));
3791     }
3792   free_resource_info ();
3793   free (uid_to_ruid);
3794 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3795   /* SPARC assembler, for instance, emit warning when debug info is output
3796      into the delay slot.  */
3797   {
3798     rtx link;
3799
3800     for (link = current_function_epilogue_delay_list;
3801          link;
3802          link = XEXP (link, 1))
3803       INSN_LOCATOR (XEXP (link, 0)) = 0;
3804   }
3805 #endif
3806 }
3807 #endif /* DELAY_SLOTS */