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