OSDN Git Service

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