OSDN Git Service

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