* reorg.c (fill_simple_delay_slots): Avoid calling optimize_skip
[official-gcc.git] / gcc / reorg.c
blob90ea42ea471638a13d473f8c2940b2ff4b43348a
1 /* Perform instruction reorganizations for delay slot filling.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012
4 Free Software Foundation, Inc.
5 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu).
6 Hacked by Michael Tiemann (tiemann@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Instruction reorganization pass.
26 This pass runs after register allocation and final jump
27 optimization. It should be the last pass to run before peephole.
28 It serves primarily to fill delay slots of insns, typically branch
29 and call insns. Other insns typically involve more complicated
30 interactions of data dependencies and resource constraints, and
31 are better handled by scheduling before register allocation (by the
32 function `schedule_insns').
34 The Branch Penalty is the number of extra cycles that are needed to
35 execute a branch insn. On an ideal machine, branches take a single
36 cycle, and the Branch Penalty is 0. Several RISC machines approach
37 branch delays differently:
39 The MIPS has a single branch delay slot. Most insns
40 (except other branches) can be used to fill this slot. When the
41 slot is filled, two insns execute in two cycles, reducing the
42 branch penalty to zero.
44 The SPARC always has a branch delay slot, but its effects can be
45 annulled when the branch is not taken. This means that failing to
46 find other sources of insns, we can hoist an insn from the branch
47 target that would only be safe to execute knowing that the branch
48 is taken.
50 The HP-PA always has a branch delay slot. For unconditional branches
51 its effects can be annulled when the branch is taken. The effects
52 of the delay slot in a conditional branch can be nullified for forward
53 taken branches, or for untaken backward branches. This means
54 we can hoist insns from the fall-through path for forward branches or
55 steal insns from the target of backward branches.
57 The TMS320C3x and C4x have three branch delay slots. When the three
58 slots are filled, the branch penalty is zero. Most insns can fill the
59 delay slots except jump insns.
61 Three techniques for filling delay slots have been implemented so far:
63 (1) `fill_simple_delay_slots' is the simplest, most efficient way
64 to fill delay slots. This pass first looks for insns which come
65 from before the branch and which are safe to execute after the
66 branch. Then it searches after the insn requiring delay slots or,
67 in the case of a branch, for insns that are after the point at
68 which the branch merges into the fallthrough code, if such a point
69 exists. When such insns are found, the branch penalty decreases
70 and no code expansion takes place.
72 (2) `fill_eager_delay_slots' is more complicated: it is used for
73 scheduling conditional jumps, or for scheduling jumps which cannot
74 be filled using (1). A machine need not have annulled jumps to use
75 this strategy, but it helps (by keeping more options open).
76 `fill_eager_delay_slots' tries to guess the direction the branch
77 will go; if it guesses right 100% of the time, it can reduce the
78 branch penalty as much as `fill_simple_delay_slots' does. If it
79 guesses wrong 100% of the time, it might as well schedule nops. When
80 `fill_eager_delay_slots' takes insns from the fall-through path of
81 the jump, usually there is no code expansion; when it takes insns
82 from the branch target, there is code expansion if it is not the
83 only way to reach that target.
85 (3) `relax_delay_slots' uses a set of rules to simplify code that
86 has been reorganized by (1) and (2). It finds cases where
87 conditional test can be eliminated, jumps can be threaded, extra
88 insns can be eliminated, etc. It is the job of (1) and (2) to do a
89 good job of scheduling locally; `relax_delay_slots' takes care of
90 making the various individual schedules work well together. It is
91 especially tuned to handle the control flow interactions of branch
92 insns. It does nothing for insns with delay slots that do not
93 branch.
95 On machines that use CC0, we are very conservative. We will not make
96 a copy of an insn involving CC0 since we want to maintain a 1-1
97 correspondence between the insn that sets and uses CC0. The insns are
98 allowed to be separated by placing an insn that sets CC0 (but not an insn
99 that uses CC0; we could do this, but it doesn't seem worthwhile) in a
100 delay slot. In that case, we point each insn at the other with REG_CC_USER
101 and REG_CC_SETTER notes. Note that these restrictions affect very few
102 machines because most RISC machines with delay slots will not use CC0
103 (the RT is the only known exception at this point).
105 Not yet implemented:
107 The Acorn Risc Machine can conditionally execute most insns, so
108 it is profitable to move single insns into a position to execute
109 based on the condition code of the previous insn.
111 The HP-PA can conditionally nullify insns, providing a similar
112 effect to the ARM, differing mostly in which insn is "in charge". */
114 #include "config.h"
115 #include "system.h"
116 #include "coretypes.h"
117 #include "tm.h"
118 #include "diagnostic-core.h"
119 #include "rtl.h"
120 #include "tm_p.h"
121 #include "expr.h"
122 #include "function.h"
123 #include "insn-config.h"
124 #include "conditions.h"
125 #include "hard-reg-set.h"
126 #include "basic-block.h"
127 #include "regs.h"
128 #include "recog.h"
129 #include "flags.h"
130 #include "obstack.h"
131 #include "insn-attr.h"
132 #include "resource.h"
133 #include "except.h"
134 #include "params.h"
135 #include "target.h"
136 #include "tree-pass.h"
137 #include "emit-rtl.h"
139 #ifdef DELAY_SLOTS
141 #ifndef ANNUL_IFTRUE_SLOTS
142 #define eligible_for_annul_true(INSN, SLOTS, TRIAL, FLAGS) 0
143 #endif
144 #ifndef ANNUL_IFFALSE_SLOTS
145 #define eligible_for_annul_false(INSN, SLOTS, TRIAL, FLAGS) 0
146 #endif
148 /* Insns which have delay slots that have not yet been filled. */
150 static struct obstack unfilled_slots_obstack;
151 static rtx *unfilled_firstobj;
153 /* Define macros to refer to the first and last slot containing unfilled
154 insns. These are used because the list may move and its address
155 should be recomputed at each use. */
157 #define unfilled_slots_base \
158 ((rtx *) obstack_base (&unfilled_slots_obstack))
160 #define unfilled_slots_next \
161 ((rtx *) obstack_next_free (&unfilled_slots_obstack))
163 /* Points to the label before the end of the function, or before a
164 return insn. */
165 static rtx function_return_label;
166 /* Likewise for a simple_return. */
167 static rtx function_simple_return_label;
169 /* Mapping between INSN_UID's and position in the code since INSN_UID's do
170 not always monotonically increase. */
171 static int *uid_to_ruid;
173 /* Highest valid index in `uid_to_ruid'. */
174 static int max_uid;
176 static int stop_search_p (rtx, int);
177 static int resource_conflicts_p (struct resources *, struct resources *);
178 static int insn_references_resource_p (rtx, struct resources *, bool);
179 static int insn_sets_resource_p (rtx, struct resources *, bool);
180 static rtx find_end_label (rtx);
181 static rtx emit_delay_sequence (rtx, rtx, int);
182 static rtx add_to_delay_list (rtx, rtx);
183 static rtx delete_from_delay_slot (rtx);
184 static void delete_scheduled_jump (rtx);
185 static void note_delay_statistics (int, int);
186 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
187 static rtx optimize_skip (rtx);
188 #endif
189 static int get_jump_flags (rtx, rtx);
190 static int rare_destination (rtx);
191 static int mostly_true_jump (rtx, rtx);
192 static rtx get_branch_condition (rtx, rtx);
193 static int condition_dominates_p (rtx, rtx);
194 static int redirect_with_delay_slots_safe_p (rtx, rtx, rtx);
195 static int redirect_with_delay_list_safe_p (rtx, rtx, rtx);
196 static int check_annul_list_true_false (int, rtx);
197 static rtx steal_delay_list_from_target (rtx, rtx, rtx, rtx,
198 struct resources *,
199 struct resources *,
200 struct resources *,
201 int, int *, int *, rtx *);
202 static rtx steal_delay_list_from_fallthrough (rtx, rtx, rtx, rtx,
203 struct resources *,
204 struct resources *,
205 struct resources *,
206 int, int *, int *);
207 static void try_merge_delay_insns (rtx, rtx);
208 static rtx redundant_insn (rtx, rtx, rtx);
209 static int own_thread_p (rtx, rtx, int);
210 static void update_block (rtx, rtx);
211 static int reorg_redirect_jump (rtx, rtx);
212 static void update_reg_dead_notes (rtx, rtx);
213 static void fix_reg_dead_note (rtx, rtx);
214 static void update_reg_unused_notes (rtx, rtx);
215 static void fill_simple_delay_slots (int);
216 static rtx fill_slots_from_thread (rtx, rtx, rtx, rtx,
217 int, int, int, int,
218 int *, rtx);
219 static void fill_eager_delay_slots (void);
220 static void relax_delay_slots (rtx);
221 static void make_return_insns (rtx);
223 /* A wrapper around next_active_insn which takes care to return ret_rtx
224 unchanged. */
226 static rtx
227 first_active_target_insn (rtx insn)
229 if (ANY_RETURN_P (insn))
230 return insn;
231 return next_active_insn (insn);
234 /* Return true iff INSN is a simplejump, or any kind of return insn. */
236 static bool
237 simplejump_or_return_p (rtx insn)
239 return (JUMP_P (insn)
240 && (simplejump_p (insn) || ANY_RETURN_P (PATTERN (insn))));
243 /* Return TRUE if this insn should stop the search for insn to fill delay
244 slots. LABELS_P indicates that labels should terminate the search.
245 In all cases, jumps terminate the search. */
247 static int
248 stop_search_p (rtx insn, int labels_p)
250 if (insn == 0)
251 return 1;
253 /* If the insn can throw an exception that is caught within the function,
254 it may effectively perform a jump from the viewpoint of the function.
255 Therefore act like for a jump. */
256 if (can_throw_internal (insn))
257 return 1;
259 switch (GET_CODE (insn))
261 case NOTE:
262 case CALL_INSN:
263 return 0;
265 case CODE_LABEL:
266 return labels_p;
268 case JUMP_INSN:
269 case BARRIER:
270 return 1;
272 case INSN:
273 /* OK unless it contains a delay slot or is an `asm' insn of some type.
274 We don't know anything about these. */
275 return (GET_CODE (PATTERN (insn)) == SEQUENCE
276 || GET_CODE (PATTERN (insn)) == ASM_INPUT
277 || asm_noperands (PATTERN (insn)) >= 0);
279 default:
280 gcc_unreachable ();
284 /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
285 resource set contains a volatile memory reference. Otherwise, return FALSE. */
287 static int
288 resource_conflicts_p (struct resources *res1, struct resources *res2)
290 if ((res1->cc && res2->cc) || (res1->memory && res2->memory)
291 || (res1->unch_memory && res2->unch_memory)
292 || res1->volatil || res2->volatil)
293 return 1;
295 #ifdef HARD_REG_SET
296 return (res1->regs & res2->regs) != HARD_CONST (0);
297 #else
299 int i;
301 for (i = 0; i < HARD_REG_SET_LONGS; i++)
302 if ((res1->regs[i] & res2->regs[i]) != 0)
303 return 1;
304 return 0;
306 #endif
309 /* Return TRUE if any resource marked in RES, a `struct resources', is
310 referenced by INSN. If INCLUDE_DELAYED_EFFECTS is set, return if the called
311 routine is using those resources.
313 We compute this by computing all the resources referenced by INSN and
314 seeing if this conflicts with RES. It might be faster to directly check
315 ourselves, and this is the way it used to work, but it means duplicating
316 a large block of complex code. */
318 static int
319 insn_references_resource_p (rtx insn, struct resources *res,
320 bool include_delayed_effects)
322 struct resources insn_res;
324 CLEAR_RESOURCE (&insn_res);
325 mark_referenced_resources (insn, &insn_res, include_delayed_effects);
326 return resource_conflicts_p (&insn_res, res);
329 /* Return TRUE if INSN modifies resources that are marked in RES.
330 INCLUDE_DELAYED_EFFECTS is set if the actions of that routine should be
331 included. CC0 is only modified if it is explicitly set; see comments
332 in front of mark_set_resources for details. */
334 static int
335 insn_sets_resource_p (rtx insn, struct resources *res,
336 bool include_delayed_effects)
338 struct resources insn_sets;
340 CLEAR_RESOURCE (&insn_sets);
341 mark_set_resources (insn, &insn_sets, 0,
342 (include_delayed_effects
343 ? MARK_SRC_DEST_CALL
344 : MARK_SRC_DEST));
345 return resource_conflicts_p (&insn_sets, res);
348 /* Find a label at the end of the function or before a RETURN. If there
349 is none, try to make one. If that fails, returns 0.
351 The property of such a label is that it is placed just before the
352 epilogue or a bare RETURN insn, so that another bare RETURN can be
353 turned into a jump to the label unconditionally. In particular, the
354 label cannot be placed before a RETURN insn with a filled delay slot.
356 ??? There may be a problem with the current implementation. Suppose
357 we start with a bare RETURN insn and call find_end_label. It may set
358 function_return_label just before the RETURN. Suppose the machinery
359 is able to fill the delay slot of the RETURN insn afterwards. Then
360 function_return_label is no longer valid according to the property
361 described above and find_end_label will still return it unmodified.
362 Note that this is probably mitigated by the following observation:
363 once function_return_label is made, it is very likely the target of
364 a jump, so filling the delay slot of the RETURN will be much more
365 difficult.
366 KIND is either simple_return_rtx or ret_rtx, indicating which type of
367 return we're looking for. */
369 static rtx
370 find_end_label (rtx kind)
372 rtx insn;
373 rtx *plabel;
375 if (kind == ret_rtx)
376 plabel = &function_return_label;
377 else
379 gcc_assert (kind == simple_return_rtx);
380 plabel = &function_simple_return_label;
383 /* If we found one previously, return it. */
384 if (*plabel)
385 return *plabel;
387 /* Otherwise, see if there is a label at the end of the function. If there
388 is, it must be that RETURN insns aren't needed, so that is our return
389 label and we don't have to do anything else. */
391 insn = get_last_insn ();
392 while (NOTE_P (insn)
393 || (NONJUMP_INSN_P (insn)
394 && (GET_CODE (PATTERN (insn)) == USE
395 || GET_CODE (PATTERN (insn)) == CLOBBER)))
396 insn = PREV_INSN (insn);
398 /* When a target threads its epilogue we might already have a
399 suitable return insn. If so put a label before it for the
400 function_return_label. */
401 if (BARRIER_P (insn)
402 && JUMP_P (PREV_INSN (insn))
403 && PATTERN (PREV_INSN (insn)) == kind)
405 rtx temp = PREV_INSN (PREV_INSN (insn));
406 rtx label = gen_label_rtx ();
407 LABEL_NUSES (label) = 0;
409 /* Put the label before any USE insns that may precede the RETURN
410 insn. */
411 while (GET_CODE (temp) == USE)
412 temp = PREV_INSN (temp);
414 emit_label_after (label, temp);
415 *plabel = label;
418 else if (LABEL_P (insn))
419 *plabel = insn;
420 else
422 rtx label = gen_label_rtx ();
423 LABEL_NUSES (label) = 0;
424 /* If the basic block reorder pass moves the return insn to
425 some other place try to locate it again and put our
426 function_return_label there. */
427 while (insn && ! (JUMP_P (insn) && (PATTERN (insn) == kind)))
428 insn = PREV_INSN (insn);
429 if (insn)
431 insn = PREV_INSN (insn);
433 /* Put the label before any USE insns that may precede the
434 RETURN insn. */
435 while (GET_CODE (insn) == USE)
436 insn = PREV_INSN (insn);
438 emit_label_after (label, insn);
440 else
442 #ifdef HAVE_epilogue
443 if (HAVE_epilogue
444 #ifdef HAVE_return
445 && ! HAVE_return
446 #endif
448 /* The RETURN insn has its delay slot filled so we cannot
449 emit the label just before it. Since we already have
450 an epilogue and cannot emit a new RETURN, we cannot
451 emit the label at all. */
452 return NULL_RTX;
453 #endif /* HAVE_epilogue */
455 /* Otherwise, make a new label and emit a RETURN and BARRIER,
456 if needed. */
457 emit_label (label);
458 #ifdef HAVE_return
459 /* We don't bother trying to create a return insn if the
460 epilogue has filled delay-slots; we would have to try and
461 move the delay-slot fillers to the delay-slots for the new
462 return insn or in front of the new return insn. */
463 if (crtl->epilogue_delay_list == NULL
464 && HAVE_return)
466 /* The return we make may have delay slots too. */
467 rtx insn = gen_return ();
468 insn = emit_jump_insn (insn);
469 set_return_jump_label (insn);
470 emit_barrier ();
471 if (num_delay_slots (insn) > 0)
472 obstack_ptr_grow (&unfilled_slots_obstack, insn);
474 #endif
476 *plabel = label;
479 /* Show one additional use for this label so it won't go away until
480 we are done. */
481 ++LABEL_NUSES (*plabel);
483 return *plabel;
486 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
487 the pattern of INSN with the SEQUENCE.
489 Chain the insns so that NEXT_INSN of each insn in the sequence points to
490 the next and NEXT_INSN of the last insn in the sequence points to
491 the first insn after the sequence. Similarly for PREV_INSN. This makes
492 it easier to scan all insns.
494 Returns the SEQUENCE that replaces INSN. */
496 static rtx
497 emit_delay_sequence (rtx insn, rtx list, int length)
499 int i = 1;
500 rtx li;
501 int had_barrier = 0;
503 /* Allocate the rtvec to hold the insns and the SEQUENCE. */
504 rtvec seqv = rtvec_alloc (length + 1);
505 rtx seq = gen_rtx_SEQUENCE (VOIDmode, seqv);
506 rtx seq_insn = make_insn_raw (seq);
507 rtx first = get_insns ();
508 rtx last = get_last_insn ();
510 /* Make a copy of the insn having delay slots. */
511 rtx delay_insn = copy_rtx (insn);
513 /* If INSN is followed by a BARRIER, delete the BARRIER since it will only
514 confuse further processing. Update LAST in case it was the last insn.
515 We will put the BARRIER back in later. */
516 if (NEXT_INSN (insn) && BARRIER_P (NEXT_INSN (insn)))
518 delete_related_insns (NEXT_INSN (insn));
519 last = get_last_insn ();
520 had_barrier = 1;
523 /* Splice our SEQUENCE into the insn stream where INSN used to be. */
524 NEXT_INSN (seq_insn) = NEXT_INSN (insn);
525 PREV_INSN (seq_insn) = PREV_INSN (insn);
527 if (insn != last)
528 PREV_INSN (NEXT_INSN (seq_insn)) = seq_insn;
530 if (insn != first)
531 NEXT_INSN (PREV_INSN (seq_insn)) = seq_insn;
533 /* Note the calls to set_new_first_and_last_insn must occur after
534 SEQ_INSN has been completely spliced into the insn stream.
536 Otherwise CUR_INSN_UID will get set to an incorrect value because
537 set_new_first_and_last_insn will not find SEQ_INSN in the chain. */
538 if (insn == last)
539 set_new_first_and_last_insn (first, seq_insn);
541 if (insn == first)
542 set_new_first_and_last_insn (seq_insn, last);
544 /* Build our SEQUENCE and rebuild the insn chain. */
545 XVECEXP (seq, 0, 0) = delay_insn;
546 INSN_DELETED_P (delay_insn) = 0;
547 PREV_INSN (delay_insn) = PREV_INSN (seq_insn);
549 INSN_LOCATION (seq_insn) = INSN_LOCATION (delay_insn);
551 for (li = list; li; li = XEXP (li, 1), i++)
553 rtx tem = XEXP (li, 0);
554 rtx note, next;
556 /* Show that this copy of the insn isn't deleted. */
557 INSN_DELETED_P (tem) = 0;
559 XVECEXP (seq, 0, i) = tem;
560 PREV_INSN (tem) = XVECEXP (seq, 0, i - 1);
561 NEXT_INSN (XVECEXP (seq, 0, i - 1)) = tem;
563 /* SPARC assembler, for instance, emit warning when debug info is output
564 into the delay slot. */
565 if (INSN_LOCATION (tem) && !INSN_LOCATION (seq_insn))
566 INSN_LOCATION (seq_insn) = INSN_LOCATION (tem);
567 INSN_LOCATION (tem) = 0;
569 for (note = REG_NOTES (tem); note; note = next)
571 next = XEXP (note, 1);
572 switch (REG_NOTE_KIND (note))
574 case REG_DEAD:
575 /* Remove any REG_DEAD notes because we can't rely on them now
576 that the insn has been moved. */
577 remove_note (tem, note);
578 break;
580 case REG_LABEL_OPERAND:
581 case REG_LABEL_TARGET:
582 /* Keep the label reference count up to date. */
583 if (LABEL_P (XEXP (note, 0)))
584 LABEL_NUSES (XEXP (note, 0)) ++;
585 break;
587 default:
588 break;
593 NEXT_INSN (XVECEXP (seq, 0, length)) = NEXT_INSN (seq_insn);
595 /* If the previous insn is a SEQUENCE, update the NEXT_INSN pointer on the
596 last insn in that SEQUENCE to point to us. Similarly for the first
597 insn in the following insn if it is a SEQUENCE. */
599 if (PREV_INSN (seq_insn) && NONJUMP_INSN_P (PREV_INSN (seq_insn))
600 && GET_CODE (PATTERN (PREV_INSN (seq_insn))) == SEQUENCE)
601 NEXT_INSN (XVECEXP (PATTERN (PREV_INSN (seq_insn)), 0,
602 XVECLEN (PATTERN (PREV_INSN (seq_insn)), 0) - 1))
603 = seq_insn;
605 if (NEXT_INSN (seq_insn) && NONJUMP_INSN_P (NEXT_INSN (seq_insn))
606 && GET_CODE (PATTERN (NEXT_INSN (seq_insn))) == SEQUENCE)
607 PREV_INSN (XVECEXP (PATTERN (NEXT_INSN (seq_insn)), 0, 0)) = seq_insn;
609 /* If there used to be a BARRIER, put it back. */
610 if (had_barrier)
611 emit_barrier_after (seq_insn);
613 gcc_assert (i == length + 1);
615 return seq_insn;
618 /* Add INSN to DELAY_LIST and return the head of the new list. The list must
619 be in the order in which the insns are to be executed. */
621 static rtx
622 add_to_delay_list (rtx insn, rtx delay_list)
624 /* If we have an empty list, just make a new list element. If
625 INSN has its block number recorded, clear it since we may
626 be moving the insn to a new block. */
628 if (delay_list == 0)
630 clear_hashed_info_for_insn (insn);
631 return gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
634 /* Otherwise this must be an INSN_LIST. Add INSN to the end of the
635 list. */
636 XEXP (delay_list, 1) = add_to_delay_list (insn, XEXP (delay_list, 1));
638 return delay_list;
641 /* Delete INSN from the delay slot of the insn that it is in, which may
642 produce an insn with no delay slots. Return the new insn. */
644 static rtx
645 delete_from_delay_slot (rtx insn)
647 rtx trial, seq_insn, seq, prev;
648 rtx delay_list = 0;
649 int i;
650 int had_barrier = 0;
652 /* We first must find the insn containing the SEQUENCE with INSN in its
653 delay slot. Do this by finding an insn, TRIAL, where
654 PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
656 for (trial = insn;
657 PREV_INSN (NEXT_INSN (trial)) == trial;
658 trial = NEXT_INSN (trial))
661 seq_insn = PREV_INSN (NEXT_INSN (trial));
662 seq = PATTERN (seq_insn);
664 if (NEXT_INSN (seq_insn) && BARRIER_P (NEXT_INSN (seq_insn)))
665 had_barrier = 1;
667 /* Create a delay list consisting of all the insns other than the one
668 we are deleting (unless we were the only one). */
669 if (XVECLEN (seq, 0) > 2)
670 for (i = 1; i < XVECLEN (seq, 0); i++)
671 if (XVECEXP (seq, 0, i) != insn)
672 delay_list = add_to_delay_list (XVECEXP (seq, 0, i), delay_list);
674 /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
675 list, and rebuild the delay list if non-empty. */
676 prev = PREV_INSN (seq_insn);
677 trial = XVECEXP (seq, 0, 0);
678 delete_related_insns (seq_insn);
679 add_insn_after (trial, prev, NULL);
681 /* If there was a barrier after the old SEQUENCE, remit it. */
682 if (had_barrier)
683 emit_barrier_after (trial);
685 /* If there are any delay insns, remit them. Otherwise clear the
686 annul flag. */
687 if (delay_list)
688 trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2);
689 else if (JUMP_P (trial))
690 INSN_ANNULLED_BRANCH_P (trial) = 0;
692 INSN_FROM_TARGET_P (insn) = 0;
694 /* Show we need to fill this insn again. */
695 obstack_ptr_grow (&unfilled_slots_obstack, trial);
697 return trial;
700 /* Delete INSN, a JUMP_INSN. If it is a conditional jump, we must track down
701 the insn that sets CC0 for it and delete it too. */
703 static void
704 delete_scheduled_jump (rtx insn)
706 /* Delete the insn that sets cc0 for us. On machines without cc0, we could
707 delete the insn that sets the condition code, but it is hard to find it.
708 Since this case is rare anyway, don't bother trying; there would likely
709 be other insns that became dead anyway, which we wouldn't know to
710 delete. */
712 #ifdef HAVE_cc0
713 if (reg_mentioned_p (cc0_rtx, insn))
715 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
717 /* If a reg-note was found, it points to an insn to set CC0. This
718 insn is in the delay list of some other insn. So delete it from
719 the delay list it was in. */
720 if (note)
722 if (! FIND_REG_INC_NOTE (XEXP (note, 0), NULL_RTX)
723 && sets_cc0_p (PATTERN (XEXP (note, 0))) == 1)
724 delete_from_delay_slot (XEXP (note, 0));
726 else
728 /* The insn setting CC0 is our previous insn, but it may be in
729 a delay slot. It will be the last insn in the delay slot, if
730 it is. */
731 rtx trial = previous_insn (insn);
732 if (NOTE_P (trial))
733 trial = prev_nonnote_insn (trial);
734 if (sets_cc0_p (PATTERN (trial)) != 1
735 || FIND_REG_INC_NOTE (trial, NULL_RTX))
736 return;
737 if (PREV_INSN (NEXT_INSN (trial)) == trial)
738 delete_related_insns (trial);
739 else
740 delete_from_delay_slot (trial);
743 #endif
745 delete_related_insns (insn);
748 /* Counters for delay-slot filling. */
750 #define NUM_REORG_FUNCTIONS 2
751 #define MAX_DELAY_HISTOGRAM 3
752 #define MAX_REORG_PASSES 2
754 static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
756 static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
758 static int reorg_pass_number;
760 static void
761 note_delay_statistics (int slots_filled, int index)
763 num_insns_needing_delays[index][reorg_pass_number]++;
764 if (slots_filled > MAX_DELAY_HISTOGRAM)
765 slots_filled = MAX_DELAY_HISTOGRAM;
766 num_filled_delays[index][slots_filled][reorg_pass_number]++;
769 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
771 /* Optimize the following cases:
773 1. When a conditional branch skips over only one instruction,
774 use an annulling branch and put that insn in the delay slot.
775 Use either a branch that annuls when the condition if true or
776 invert the test with a branch that annuls when the condition is
777 false. This saves insns, since otherwise we must copy an insn
778 from the L1 target.
780 (orig) (skip) (otherwise)
781 Bcc.n L1 Bcc',a L1 Bcc,a L1'
782 insn insn insn2
783 L1: L1: L1:
784 insn2 insn2 insn2
785 insn3 insn3 L1':
786 insn3
788 2. When a conditional branch skips over only one instruction,
789 and after that, it unconditionally branches somewhere else,
790 perform the similar optimization. This saves executing the
791 second branch in the case where the inverted condition is true.
793 Bcc.n L1 Bcc',a L2
794 insn insn
795 L1: L1:
796 Bra L2 Bra L2
798 INSN is a JUMP_INSN.
800 This should be expanded to skip over N insns, where N is the number
801 of delay slots required. */
803 static rtx
804 optimize_skip (rtx insn)
806 rtx trial = next_nonnote_insn (insn);
807 rtx next_trial = next_active_insn (trial);
808 rtx delay_list = 0;
809 int flags;
811 flags = get_jump_flags (insn, JUMP_LABEL (insn));
813 if (trial == 0
814 || !NONJUMP_INSN_P (trial)
815 || GET_CODE (PATTERN (trial)) == SEQUENCE
816 || recog_memoized (trial) < 0
817 || (! eligible_for_annul_false (insn, 0, trial, flags)
818 && ! eligible_for_annul_true (insn, 0, trial, flags))
819 || can_throw_internal (trial))
820 return 0;
822 /* There are two cases where we are just executing one insn (we assume
823 here that a branch requires only one insn; this should be generalized
824 at some point): Where the branch goes around a single insn or where
825 we have one insn followed by a branch to the same label we branch to.
826 In both of these cases, inverting the jump and annulling the delay
827 slot give the same effect in fewer insns. */
828 if ((next_trial == next_active_insn (JUMP_LABEL (insn))
829 && ! (next_trial == 0 && crtl->epilogue_delay_list != 0))
830 || (next_trial != 0
831 && simplejump_or_return_p (next_trial)
832 && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)))
834 if (eligible_for_annul_false (insn, 0, trial, flags))
836 if (invert_jump (insn, JUMP_LABEL (insn), 1))
837 INSN_FROM_TARGET_P (trial) = 1;
838 else if (! eligible_for_annul_true (insn, 0, trial, flags))
839 return 0;
842 delay_list = add_to_delay_list (trial, NULL_RTX);
843 next_trial = next_active_insn (trial);
844 update_block (trial, trial);
845 delete_related_insns (trial);
847 /* Also, if we are targeting an unconditional
848 branch, thread our jump to the target of that branch. Don't
849 change this into a RETURN here, because it may not accept what
850 we have in the delay slot. We'll fix this up later. */
851 if (next_trial && simplejump_or_return_p (next_trial))
853 rtx target_label = JUMP_LABEL (next_trial);
854 if (ANY_RETURN_P (target_label))
855 target_label = find_end_label (target_label);
857 if (target_label)
859 /* Recompute the flags based on TARGET_LABEL since threading
860 the jump to TARGET_LABEL may change the direction of the
861 jump (which may change the circumstances in which the
862 delay slot is nullified). */
863 flags = get_jump_flags (insn, target_label);
864 if (eligible_for_annul_true (insn, 0, trial, flags))
865 reorg_redirect_jump (insn, target_label);
869 INSN_ANNULLED_BRANCH_P (insn) = 1;
872 return delay_list;
874 #endif
876 /* Encode and return branch direction and prediction information for
877 INSN assuming it will jump to LABEL.
879 Non conditional branches return no direction information and
880 are predicted as very likely taken. */
882 static int
883 get_jump_flags (rtx insn, rtx label)
885 int flags;
887 /* get_jump_flags can be passed any insn with delay slots, these may
888 be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
889 direction information, and only if they are conditional jumps.
891 If LABEL is a return, then there is no way to determine the branch
892 direction. */
893 if (JUMP_P (insn)
894 && (condjump_p (insn) || condjump_in_parallel_p (insn))
895 && !ANY_RETURN_P (label)
896 && INSN_UID (insn) <= max_uid
897 && INSN_UID (label) <= max_uid)
898 flags
899 = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
900 ? ATTR_FLAG_forward : ATTR_FLAG_backward;
901 /* No valid direction information. */
902 else
903 flags = 0;
905 return flags;
908 /* Return 1 if INSN is a destination that will be branched to rarely (the
909 return point of a function); return 2 if DEST will be branched to very
910 rarely (a call to a function that doesn't return). Otherwise,
911 return 0. */
913 static int
914 rare_destination (rtx insn)
916 int jump_count = 0;
917 rtx next;
919 for (; insn && !ANY_RETURN_P (insn); insn = next)
921 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
922 insn = XVECEXP (PATTERN (insn), 0, 0);
924 next = NEXT_INSN (insn);
926 switch (GET_CODE (insn))
928 case CODE_LABEL:
929 return 0;
930 case BARRIER:
931 /* A BARRIER can either be after a JUMP_INSN or a CALL_INSN. We
932 don't scan past JUMP_INSNs, so any barrier we find here must
933 have been after a CALL_INSN and hence mean the call doesn't
934 return. */
935 return 2;
936 case JUMP_INSN:
937 if (ANY_RETURN_P (PATTERN (insn)))
938 return 1;
939 else if (simplejump_p (insn)
940 && jump_count++ < 10)
941 next = JUMP_LABEL (insn);
942 else
943 return 0;
945 default:
946 break;
950 /* If we got here it means we hit the end of the function. So this
951 is an unlikely destination. */
953 return 1;
956 /* Return truth value of the statement that this branch
957 is mostly taken. If we think that the branch is extremely likely
958 to be taken, we return 2. If the branch is slightly more likely to be
959 taken, return 1. If the branch is slightly less likely to be taken,
960 return 0 and if the branch is highly unlikely to be taken, return -1.
962 CONDITION, if nonzero, is the condition that JUMP_INSN is testing. */
964 static int
965 mostly_true_jump (rtx jump_insn, rtx condition)
967 rtx target_label = JUMP_LABEL (jump_insn);
968 rtx note;
969 int rare_dest, rare_fallthrough;
971 /* If branch probabilities are available, then use that number since it
972 always gives a correct answer. */
973 note = find_reg_note (jump_insn, REG_BR_PROB, 0);
974 if (note)
976 int prob = INTVAL (XEXP (note, 0));
978 if (prob >= REG_BR_PROB_BASE * 9 / 10)
979 return 2;
980 else if (prob >= REG_BR_PROB_BASE / 2)
981 return 1;
982 else if (prob >= REG_BR_PROB_BASE / 10)
983 return 0;
984 else
985 return -1;
988 /* Look at the relative rarities of the fallthrough and destination. If
989 they differ, we can predict the branch that way. */
990 rare_dest = rare_destination (target_label);
991 rare_fallthrough = rare_destination (NEXT_INSN (jump_insn));
993 switch (rare_fallthrough - rare_dest)
995 case -2:
996 return -1;
997 case -1:
998 return 0;
999 case 0:
1000 break;
1001 case 1:
1002 return 1;
1003 case 2:
1004 return 2;
1007 /* If we couldn't figure out what this jump was, assume it won't be
1008 taken. This should be rare. */
1009 if (condition == 0)
1010 return 0;
1012 /* Predict backward branches usually take, forward branches usually not. If
1013 we don't know whether this is forward or backward, assume the branch
1014 will be taken, since most are. */
1015 return (ANY_RETURN_P (target_label) || INSN_UID (jump_insn) > max_uid
1016 || INSN_UID (target_label) > max_uid
1017 || (uid_to_ruid[INSN_UID (jump_insn)]
1018 > uid_to_ruid[INSN_UID (target_label)]));
1021 /* Return the condition under which INSN will branch to TARGET. If TARGET
1022 is zero, return the condition under which INSN will return. If INSN is
1023 an unconditional branch, return const_true_rtx. If INSN isn't a simple
1024 type of jump, or it doesn't go to TARGET, return 0. */
1026 static rtx
1027 get_branch_condition (rtx insn, rtx target)
1029 rtx pat = PATTERN (insn);
1030 rtx src;
1032 if (condjump_in_parallel_p (insn))
1033 pat = XVECEXP (pat, 0, 0);
1035 if (ANY_RETURN_P (pat))
1036 return pat == target ? const_true_rtx : 0;
1038 if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
1039 return 0;
1041 src = SET_SRC (pat);
1042 if (GET_CODE (src) == LABEL_REF && XEXP (src, 0) == target)
1043 return const_true_rtx;
1045 else if (GET_CODE (src) == IF_THEN_ELSE
1046 && XEXP (src, 2) == pc_rtx
1047 && GET_CODE (XEXP (src, 1)) == LABEL_REF
1048 && XEXP (XEXP (src, 1), 0) == target)
1049 return XEXP (src, 0);
1051 else if (GET_CODE (src) == IF_THEN_ELSE
1052 && XEXP (src, 1) == pc_rtx
1053 && GET_CODE (XEXP (src, 2)) == LABEL_REF
1054 && XEXP (XEXP (src, 2), 0) == target)
1056 enum rtx_code rev;
1057 rev = reversed_comparison_code (XEXP (src, 0), insn);
1058 if (rev != UNKNOWN)
1059 return gen_rtx_fmt_ee (rev, GET_MODE (XEXP (src, 0)),
1060 XEXP (XEXP (src, 0), 0),
1061 XEXP (XEXP (src, 0), 1));
1064 return 0;
1067 /* Return nonzero if CONDITION is more strict than the condition of
1068 INSN, i.e., if INSN will always branch if CONDITION is true. */
1070 static int
1071 condition_dominates_p (rtx condition, rtx insn)
1073 rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
1074 enum rtx_code code = GET_CODE (condition);
1075 enum rtx_code other_code;
1077 if (rtx_equal_p (condition, other_condition)
1078 || other_condition == const_true_rtx)
1079 return 1;
1081 else if (condition == const_true_rtx || other_condition == 0)
1082 return 0;
1084 other_code = GET_CODE (other_condition);
1085 if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
1086 || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
1087 || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
1088 return 0;
1090 return comparison_dominates_p (code, other_code);
1093 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1094 any insns already in the delay slot of JUMP. */
1096 static int
1097 redirect_with_delay_slots_safe_p (rtx jump, rtx newlabel, rtx seq)
1099 int flags, i;
1100 rtx pat = PATTERN (seq);
1102 /* Make sure all the delay slots of this jump would still
1103 be valid after threading the jump. If they are still
1104 valid, then return nonzero. */
1106 flags = get_jump_flags (jump, newlabel);
1107 for (i = 1; i < XVECLEN (pat, 0); i++)
1108 if (! (
1109 #ifdef ANNUL_IFFALSE_SLOTS
1110 (INSN_ANNULLED_BRANCH_P (jump)
1111 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1112 ? eligible_for_annul_false (jump, i - 1,
1113 XVECEXP (pat, 0, i), flags) :
1114 #endif
1115 #ifdef ANNUL_IFTRUE_SLOTS
1116 (INSN_ANNULLED_BRANCH_P (jump)
1117 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1118 ? eligible_for_annul_true (jump, i - 1,
1119 XVECEXP (pat, 0, i), flags) :
1120 #endif
1121 eligible_for_delay (jump, i - 1, XVECEXP (pat, 0, i), flags)))
1122 break;
1124 return (i == XVECLEN (pat, 0));
1127 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1128 any insns we wish to place in the delay slot of JUMP. */
1130 static int
1131 redirect_with_delay_list_safe_p (rtx jump, rtx newlabel, rtx delay_list)
1133 int flags, i;
1134 rtx li;
1136 /* Make sure all the insns in DELAY_LIST would still be
1137 valid after threading the jump. If they are still
1138 valid, then return nonzero. */
1140 flags = get_jump_flags (jump, newlabel);
1141 for (li = delay_list, i = 0; li; li = XEXP (li, 1), i++)
1142 if (! (
1143 #ifdef ANNUL_IFFALSE_SLOTS
1144 (INSN_ANNULLED_BRANCH_P (jump)
1145 && INSN_FROM_TARGET_P (XEXP (li, 0)))
1146 ? eligible_for_annul_false (jump, i, XEXP (li, 0), flags) :
1147 #endif
1148 #ifdef ANNUL_IFTRUE_SLOTS
1149 (INSN_ANNULLED_BRANCH_P (jump)
1150 && ! INSN_FROM_TARGET_P (XEXP (li, 0)))
1151 ? eligible_for_annul_true (jump, i, XEXP (li, 0), flags) :
1152 #endif
1153 eligible_for_delay (jump, i, XEXP (li, 0), flags)))
1154 break;
1156 return (li == NULL);
1159 /* DELAY_LIST is a list of insns that have already been placed into delay
1160 slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
1161 If not, return 0; otherwise return 1. */
1163 static int
1164 check_annul_list_true_false (int annul_true_p, rtx delay_list)
1166 rtx temp;
1168 if (delay_list)
1170 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1172 rtx trial = XEXP (temp, 0);
1174 if ((annul_true_p && INSN_FROM_TARGET_P (trial))
1175 || (!annul_true_p && !INSN_FROM_TARGET_P (trial)))
1176 return 0;
1180 return 1;
1183 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
1184 the condition tested by INSN is CONDITION and the resources shown in
1185 OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1186 from SEQ's delay list, in addition to whatever insns it may execute
1187 (in DELAY_LIST). SETS and NEEDED are denote resources already set and
1188 needed while searching for delay slot insns. Return the concatenated
1189 delay list if possible, otherwise, return 0.
1191 SLOTS_TO_FILL is the total number of slots required by INSN, and
1192 PSLOTS_FILLED points to the number filled so far (also the number of
1193 insns in DELAY_LIST). It is updated with the number that have been
1194 filled from the SEQUENCE, if any.
1196 PANNUL_P points to a nonzero value if we already know that we need
1197 to annul INSN. If this routine determines that annulling is needed,
1198 it may set that value nonzero.
1200 PNEW_THREAD points to a location that is to receive the place at which
1201 execution should continue. */
1203 static rtx
1204 steal_delay_list_from_target (rtx insn, rtx condition, rtx seq,
1205 rtx delay_list, struct resources *sets,
1206 struct resources *needed,
1207 struct resources *other_needed,
1208 int slots_to_fill, int *pslots_filled,
1209 int *pannul_p, rtx *pnew_thread)
1211 rtx temp;
1212 int slots_remaining = slots_to_fill - *pslots_filled;
1213 int total_slots_filled = *pslots_filled;
1214 rtx new_delay_list = 0;
1215 int must_annul = *pannul_p;
1216 int used_annul = 0;
1217 int i;
1218 struct resources cc_set;
1220 /* We can't do anything if there are more delay slots in SEQ than we
1221 can handle, or if we don't know that it will be a taken branch.
1222 We know that it will be a taken branch if it is either an unconditional
1223 branch or a conditional branch with a stricter branch condition.
1225 Also, exit if the branch has more than one set, since then it is computing
1226 other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1227 ??? It may be possible to move other sets into INSN in addition to
1228 moving the instructions in the delay slots.
1230 We can not steal the delay list if one of the instructions in the
1231 current delay_list modifies the condition codes and the jump in the
1232 sequence is a conditional jump. We can not do this because we can
1233 not change the direction of the jump because the condition codes
1234 will effect the direction of the jump in the sequence. */
1236 CLEAR_RESOURCE (&cc_set);
1237 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1239 rtx trial = XEXP (temp, 0);
1241 mark_set_resources (trial, &cc_set, 0, MARK_SRC_DEST_CALL);
1242 if (insn_references_resource_p (XVECEXP (seq , 0, 0), &cc_set, false))
1243 return delay_list;
1246 if (XVECLEN (seq, 0) - 1 > slots_remaining
1247 || ! condition_dominates_p (condition, XVECEXP (seq, 0, 0))
1248 || ! single_set (XVECEXP (seq, 0, 0)))
1249 return delay_list;
1251 #ifdef MD_CAN_REDIRECT_BRANCH
1252 /* On some targets, branches with delay slots can have a limited
1253 displacement. Give the back end a chance to tell us we can't do
1254 this. */
1255 if (! MD_CAN_REDIRECT_BRANCH (insn, XVECEXP (seq, 0, 0)))
1256 return delay_list;
1257 #endif
1259 for (i = 1; i < XVECLEN (seq, 0); i++)
1261 rtx trial = XVECEXP (seq, 0, i);
1262 int flags;
1264 if (insn_references_resource_p (trial, sets, false)
1265 || insn_sets_resource_p (trial, needed, false)
1266 || insn_sets_resource_p (trial, sets, false)
1267 #ifdef HAVE_cc0
1268 /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1269 delay list. */
1270 || find_reg_note (trial, REG_CC_USER, NULL_RTX)
1271 #endif
1272 /* If TRIAL is from the fallthrough code of an annulled branch insn
1273 in SEQ, we cannot use it. */
1274 || (INSN_ANNULLED_BRANCH_P (XVECEXP (seq, 0, 0))
1275 && ! INSN_FROM_TARGET_P (trial)))
1276 return delay_list;
1278 /* If this insn was already done (usually in a previous delay slot),
1279 pretend we put it in our delay slot. */
1280 if (redundant_insn (trial, insn, new_delay_list))
1281 continue;
1283 /* We will end up re-vectoring this branch, so compute flags
1284 based on jumping to the new label. */
1285 flags = get_jump_flags (insn, JUMP_LABEL (XVECEXP (seq, 0, 0)));
1287 if (! must_annul
1288 && ((condition == const_true_rtx
1289 || (! insn_sets_resource_p (trial, other_needed, false)
1290 && ! may_trap_or_fault_p (PATTERN (trial)))))
1291 ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1292 : (must_annul || (delay_list == NULL && new_delay_list == NULL))
1293 && (must_annul = 1,
1294 check_annul_list_true_false (0, delay_list)
1295 && check_annul_list_true_false (0, new_delay_list)
1296 && eligible_for_annul_false (insn, total_slots_filled,
1297 trial, flags)))
1299 if (must_annul)
1300 used_annul = 1;
1301 temp = copy_delay_slot_insn (trial);
1302 INSN_FROM_TARGET_P (temp) = 1;
1303 new_delay_list = add_to_delay_list (temp, new_delay_list);
1304 total_slots_filled++;
1306 if (--slots_remaining == 0)
1307 break;
1309 else
1310 return delay_list;
1313 /* Show the place to which we will be branching. */
1314 *pnew_thread = first_active_target_insn (JUMP_LABEL (XVECEXP (seq, 0, 0)));
1316 /* Add any new insns to the delay list and update the count of the
1317 number of slots filled. */
1318 *pslots_filled = total_slots_filled;
1319 if (used_annul)
1320 *pannul_p = 1;
1322 if (delay_list == 0)
1323 return new_delay_list;
1325 for (temp = new_delay_list; temp; temp = XEXP (temp, 1))
1326 delay_list = add_to_delay_list (XEXP (temp, 0), delay_list);
1328 return delay_list;
1331 /* Similar to steal_delay_list_from_target except that SEQ is on the
1332 fallthrough path of INSN. Here we only do something if the delay insn
1333 of SEQ is an unconditional branch. In that case we steal its delay slot
1334 for INSN since unconditional branches are much easier to fill. */
1336 static rtx
1337 steal_delay_list_from_fallthrough (rtx insn, rtx condition, rtx seq,
1338 rtx delay_list, struct resources *sets,
1339 struct resources *needed,
1340 struct resources *other_needed,
1341 int slots_to_fill, int *pslots_filled,
1342 int *pannul_p)
1344 int i;
1345 int flags;
1346 int must_annul = *pannul_p;
1347 int used_annul = 0;
1349 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1351 /* We can't do anything if SEQ's delay insn isn't an
1352 unconditional branch. */
1354 if (! simplejump_or_return_p (XVECEXP (seq, 0, 0)))
1355 return delay_list;
1357 for (i = 1; i < XVECLEN (seq, 0); i++)
1359 rtx trial = XVECEXP (seq, 0, i);
1361 /* If TRIAL sets CC0, stealing it will move it too far from the use
1362 of CC0. */
1363 if (insn_references_resource_p (trial, sets, false)
1364 || insn_sets_resource_p (trial, needed, false)
1365 || insn_sets_resource_p (trial, sets, false)
1366 #ifdef HAVE_cc0
1367 || sets_cc0_p (PATTERN (trial))
1368 #endif
1371 break;
1373 /* If this insn was already done, we don't need it. */
1374 if (redundant_insn (trial, insn, delay_list))
1376 delete_from_delay_slot (trial);
1377 continue;
1380 if (! must_annul
1381 && ((condition == const_true_rtx
1382 || (! insn_sets_resource_p (trial, other_needed, false)
1383 && ! may_trap_or_fault_p (PATTERN (trial)))))
1384 ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1385 : (must_annul || delay_list == NULL) && (must_annul = 1,
1386 check_annul_list_true_false (1, delay_list)
1387 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1389 if (must_annul)
1390 used_annul = 1;
1391 delete_from_delay_slot (trial);
1392 delay_list = add_to_delay_list (trial, delay_list);
1394 if (++(*pslots_filled) == slots_to_fill)
1395 break;
1397 else
1398 break;
1401 if (used_annul)
1402 *pannul_p = 1;
1403 return delay_list;
1406 /* Try merging insns starting at THREAD which match exactly the insns in
1407 INSN's delay list.
1409 If all insns were matched and the insn was previously annulling, the
1410 annul bit will be cleared.
1412 For each insn that is merged, if the branch is or will be non-annulling,
1413 we delete the merged insn. */
1415 static void
1416 try_merge_delay_insns (rtx insn, rtx thread)
1418 rtx trial, next_trial;
1419 rtx delay_insn = XVECEXP (PATTERN (insn), 0, 0);
1420 int annul_p = JUMP_P (delay_insn) && INSN_ANNULLED_BRANCH_P (delay_insn);
1421 int slot_number = 1;
1422 int num_slots = XVECLEN (PATTERN (insn), 0);
1423 rtx next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1424 struct resources set, needed;
1425 rtx merged_insns = 0;
1426 int i;
1427 int flags;
1429 flags = get_jump_flags (delay_insn, JUMP_LABEL (delay_insn));
1431 CLEAR_RESOURCE (&needed);
1432 CLEAR_RESOURCE (&set);
1434 /* If this is not an annulling branch, take into account anything needed in
1435 INSN's delay slot. This prevents two increments from being incorrectly
1436 folded into one. If we are annulling, this would be the correct
1437 thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1438 will essentially disable this optimization. This method is somewhat of
1439 a kludge, but I don't see a better way.) */
1440 if (! annul_p)
1441 for (i = 1 ; i < num_slots; i++)
1442 if (XVECEXP (PATTERN (insn), 0, i))
1443 mark_referenced_resources (XVECEXP (PATTERN (insn), 0, i), &needed,
1444 true);
1446 for (trial = thread; !stop_search_p (trial, 1); trial = next_trial)
1448 rtx pat = PATTERN (trial);
1449 rtx oldtrial = trial;
1451 next_trial = next_nonnote_insn (trial);
1453 /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1454 if (NONJUMP_INSN_P (trial)
1455 && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1456 continue;
1458 if (GET_CODE (next_to_match) == GET_CODE (trial)
1459 #ifdef HAVE_cc0
1460 /* We can't share an insn that sets cc0. */
1461 && ! sets_cc0_p (pat)
1462 #endif
1463 && ! insn_references_resource_p (trial, &set, true)
1464 && ! insn_sets_resource_p (trial, &set, true)
1465 && ! insn_sets_resource_p (trial, &needed, true)
1466 && (trial = try_split (pat, trial, 0)) != 0
1467 /* Update next_trial, in case try_split succeeded. */
1468 && (next_trial = next_nonnote_insn (trial))
1469 /* Likewise THREAD. */
1470 && (thread = oldtrial == thread ? trial : thread)
1471 && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1472 /* Have to test this condition if annul condition is different
1473 from (and less restrictive than) non-annulling one. */
1474 && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1477 if (! annul_p)
1479 update_block (trial, thread);
1480 if (trial == thread)
1481 thread = next_active_insn (thread);
1483 delete_related_insns (trial);
1484 INSN_FROM_TARGET_P (next_to_match) = 0;
1486 else
1487 merged_insns = gen_rtx_INSN_LIST (VOIDmode, trial, merged_insns);
1489 if (++slot_number == num_slots)
1490 break;
1492 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1495 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1496 mark_referenced_resources (trial, &needed, true);
1499 /* See if we stopped on a filled insn. If we did, try to see if its
1500 delay slots match. */
1501 if (slot_number != num_slots
1502 && trial && NONJUMP_INSN_P (trial)
1503 && GET_CODE (PATTERN (trial)) == SEQUENCE
1504 && !(JUMP_P (XVECEXP (PATTERN (trial), 0, 0))
1505 && INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial), 0, 0))))
1507 rtx pat = PATTERN (trial);
1508 rtx filled_insn = XVECEXP (pat, 0, 0);
1510 /* Account for resources set/needed by the filled insn. */
1511 mark_set_resources (filled_insn, &set, 0, MARK_SRC_DEST_CALL);
1512 mark_referenced_resources (filled_insn, &needed, true);
1514 for (i = 1; i < XVECLEN (pat, 0); i++)
1516 rtx dtrial = XVECEXP (pat, 0, i);
1518 if (! insn_references_resource_p (dtrial, &set, true)
1519 && ! insn_sets_resource_p (dtrial, &set, true)
1520 && ! insn_sets_resource_p (dtrial, &needed, true)
1521 #ifdef HAVE_cc0
1522 && ! sets_cc0_p (PATTERN (dtrial))
1523 #endif
1524 && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1525 && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1527 if (! annul_p)
1529 rtx new_rtx;
1531 update_block (dtrial, thread);
1532 new_rtx = delete_from_delay_slot (dtrial);
1533 if (INSN_DELETED_P (thread))
1534 thread = new_rtx;
1535 INSN_FROM_TARGET_P (next_to_match) = 0;
1537 else
1538 merged_insns = gen_rtx_INSN_LIST (SImode, dtrial,
1539 merged_insns);
1541 if (++slot_number == num_slots)
1542 break;
1544 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1546 else
1548 /* Keep track of the set/referenced resources for the delay
1549 slots of any trial insns we encounter. */
1550 mark_set_resources (dtrial, &set, 0, MARK_SRC_DEST_CALL);
1551 mark_referenced_resources (dtrial, &needed, true);
1556 /* If all insns in the delay slot have been matched and we were previously
1557 annulling the branch, we need not any more. In that case delete all the
1558 merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1559 the delay list so that we know that it isn't only being used at the
1560 target. */
1561 if (slot_number == num_slots && annul_p)
1563 for (; merged_insns; merged_insns = XEXP (merged_insns, 1))
1565 if (GET_MODE (merged_insns) == SImode)
1567 rtx new_rtx;
1569 update_block (XEXP (merged_insns, 0), thread);
1570 new_rtx = delete_from_delay_slot (XEXP (merged_insns, 0));
1571 if (INSN_DELETED_P (thread))
1572 thread = new_rtx;
1574 else
1576 update_block (XEXP (merged_insns, 0), thread);
1577 delete_related_insns (XEXP (merged_insns, 0));
1581 INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1583 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1584 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i)) = 0;
1588 /* See if INSN is redundant with an insn in front of TARGET. Often this
1589 is called when INSN is a candidate for a delay slot of TARGET.
1590 DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1591 of INSN. Often INSN will be redundant with an insn in a delay slot of
1592 some previous insn. This happens when we have a series of branches to the
1593 same label; in that case the first insn at the target might want to go
1594 into each of the delay slots.
1596 If we are not careful, this routine can take up a significant fraction
1597 of the total compilation time (4%), but only wins rarely. Hence we
1598 speed this routine up by making two passes. The first pass goes back
1599 until it hits a label and sees if it finds an insn with an identical
1600 pattern. Only in this (relatively rare) event does it check for
1601 data conflicts.
1603 We do not split insns we encounter. This could cause us not to find a
1604 redundant insn, but the cost of splitting seems greater than the possible
1605 gain in rare cases. */
1607 static rtx
1608 redundant_insn (rtx insn, rtx target, rtx delay_list)
1610 rtx target_main = target;
1611 rtx ipat = PATTERN (insn);
1612 rtx trial, pat;
1613 struct resources needed, set;
1614 int i;
1615 unsigned insns_to_search;
1617 /* If INSN has any REG_UNUSED notes, it can't match anything since we
1618 are allowed to not actually assign to such a register. */
1619 if (find_reg_note (insn, REG_UNUSED, NULL_RTX) != 0)
1620 return 0;
1622 /* Scan backwards looking for a match. */
1623 for (trial = PREV_INSN (target),
1624 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1625 trial && insns_to_search > 0;
1626 trial = PREV_INSN (trial))
1628 if (LABEL_P (trial))
1629 return 0;
1631 if (!NONDEBUG_INSN_P (trial))
1632 continue;
1633 --insns_to_search;
1635 pat = PATTERN (trial);
1636 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1637 continue;
1639 if (GET_CODE (pat) == SEQUENCE)
1641 /* Stop for a CALL and its delay slots because it is difficult to
1642 track its resource needs correctly. */
1643 if (CALL_P (XVECEXP (pat, 0, 0)))
1644 return 0;
1646 /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1647 slots because it is difficult to track its resource needs
1648 correctly. */
1650 #ifdef INSN_SETS_ARE_DELAYED
1651 if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1652 return 0;
1653 #endif
1655 #ifdef INSN_REFERENCES_ARE_DELAYED
1656 if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1657 return 0;
1658 #endif
1660 /* See if any of the insns in the delay slot match, updating
1661 resource requirements as we go. */
1662 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1663 if (GET_CODE (XVECEXP (pat, 0, i)) == GET_CODE (insn)
1664 && rtx_equal_p (PATTERN (XVECEXP (pat, 0, i)), ipat)
1665 && ! find_reg_note (XVECEXP (pat, 0, i), REG_UNUSED, NULL_RTX))
1666 break;
1668 /* If found a match, exit this loop early. */
1669 if (i > 0)
1670 break;
1673 else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat)
1674 && ! find_reg_note (trial, REG_UNUSED, NULL_RTX))
1675 break;
1678 /* If we didn't find an insn that matches, return 0. */
1679 if (trial == 0)
1680 return 0;
1682 /* See what resources this insn sets and needs. If they overlap, or
1683 if this insn references CC0, it can't be redundant. */
1685 CLEAR_RESOURCE (&needed);
1686 CLEAR_RESOURCE (&set);
1687 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1688 mark_referenced_resources (insn, &needed, true);
1690 /* If TARGET is a SEQUENCE, get the main insn. */
1691 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1692 target_main = XVECEXP (PATTERN (target), 0, 0);
1694 if (resource_conflicts_p (&needed, &set)
1695 #ifdef HAVE_cc0
1696 || reg_mentioned_p (cc0_rtx, ipat)
1697 #endif
1698 /* The insn requiring the delay may not set anything needed or set by
1699 INSN. */
1700 || insn_sets_resource_p (target_main, &needed, true)
1701 || insn_sets_resource_p (target_main, &set, true))
1702 return 0;
1704 /* Insns we pass may not set either NEEDED or SET, so merge them for
1705 simpler tests. */
1706 needed.memory |= set.memory;
1707 needed.unch_memory |= set.unch_memory;
1708 IOR_HARD_REG_SET (needed.regs, set.regs);
1710 /* This insn isn't redundant if it conflicts with an insn that either is
1711 or will be in a delay slot of TARGET. */
1713 while (delay_list)
1715 if (insn_sets_resource_p (XEXP (delay_list, 0), &needed, true))
1716 return 0;
1717 delay_list = XEXP (delay_list, 1);
1720 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1721 for (i = 1; i < XVECLEN (PATTERN (target), 0); i++)
1722 if (insn_sets_resource_p (XVECEXP (PATTERN (target), 0, i), &needed,
1723 true))
1724 return 0;
1726 /* Scan backwards until we reach a label or an insn that uses something
1727 INSN sets or sets something insn uses or sets. */
1729 for (trial = PREV_INSN (target),
1730 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1731 trial && !LABEL_P (trial) && insns_to_search > 0;
1732 trial = PREV_INSN (trial))
1734 if (!NONDEBUG_INSN_P (trial))
1735 continue;
1736 --insns_to_search;
1738 pat = PATTERN (trial);
1739 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1740 continue;
1742 if (GET_CODE (pat) == SEQUENCE)
1744 bool annul_p = false;
1745 rtx control = XVECEXP (pat, 0, 0);
1747 /* If this is a CALL_INSN and its delay slots, it is hard to track
1748 the resource needs properly, so give up. */
1749 if (CALL_P (control))
1750 return 0;
1752 /* If this is an INSN or JUMP_INSN with delayed effects, it
1753 is hard to track the resource needs properly, so give up. */
1755 #ifdef INSN_SETS_ARE_DELAYED
1756 if (INSN_SETS_ARE_DELAYED (control))
1757 return 0;
1758 #endif
1760 #ifdef INSN_REFERENCES_ARE_DELAYED
1761 if (INSN_REFERENCES_ARE_DELAYED (control))
1762 return 0;
1763 #endif
1765 if (JUMP_P (control))
1766 annul_p = INSN_ANNULLED_BRANCH_P (control);
1768 /* See if any of the insns in the delay slot match, updating
1769 resource requirements as we go. */
1770 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1772 rtx candidate = XVECEXP (pat, 0, i);
1774 /* If an insn will be annulled if the branch is false, it isn't
1775 considered as a possible duplicate insn. */
1776 if (rtx_equal_p (PATTERN (candidate), ipat)
1777 && ! (annul_p && INSN_FROM_TARGET_P (candidate)))
1779 /* Show that this insn will be used in the sequel. */
1780 INSN_FROM_TARGET_P (candidate) = 0;
1781 return candidate;
1784 /* Unless this is an annulled insn from the target of a branch,
1785 we must stop if it sets anything needed or set by INSN. */
1786 if ((!annul_p || !INSN_FROM_TARGET_P (candidate))
1787 && insn_sets_resource_p (candidate, &needed, true))
1788 return 0;
1791 /* If the insn requiring the delay slot conflicts with INSN, we
1792 must stop. */
1793 if (insn_sets_resource_p (control, &needed, true))
1794 return 0;
1796 else
1798 /* See if TRIAL is the same as INSN. */
1799 pat = PATTERN (trial);
1800 if (rtx_equal_p (pat, ipat))
1801 return trial;
1803 /* Can't go any further if TRIAL conflicts with INSN. */
1804 if (insn_sets_resource_p (trial, &needed, true))
1805 return 0;
1809 return 0;
1812 /* Return 1 if THREAD can only be executed in one way. If LABEL is nonzero,
1813 it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1814 is nonzero, we are allowed to fall into this thread; otherwise, we are
1815 not.
1817 If LABEL is used more than one or we pass a label other than LABEL before
1818 finding an active insn, we do not own this thread. */
1820 static int
1821 own_thread_p (rtx thread, rtx label, int allow_fallthrough)
1823 rtx active_insn;
1824 rtx insn;
1826 /* We don't own the function end. */
1827 if (thread == 0 || ANY_RETURN_P (thread))
1828 return 0;
1830 /* Get the first active insn, or THREAD, if it is an active insn. */
1831 active_insn = next_active_insn (PREV_INSN (thread));
1833 for (insn = thread; insn != active_insn; insn = NEXT_INSN (insn))
1834 if (LABEL_P (insn)
1835 && (insn != label || LABEL_NUSES (insn) != 1))
1836 return 0;
1838 if (allow_fallthrough)
1839 return 1;
1841 /* Ensure that we reach a BARRIER before any insn or label. */
1842 for (insn = prev_nonnote_insn (thread);
1843 insn == 0 || !BARRIER_P (insn);
1844 insn = prev_nonnote_insn (insn))
1845 if (insn == 0
1846 || LABEL_P (insn)
1847 || (NONJUMP_INSN_P (insn)
1848 && GET_CODE (PATTERN (insn)) != USE
1849 && GET_CODE (PATTERN (insn)) != CLOBBER))
1850 return 0;
1852 return 1;
1855 /* Called when INSN is being moved from a location near the target of a jump.
1856 We leave a marker of the form (use (INSN)) immediately in front
1857 of WHERE for mark_target_live_regs. These markers will be deleted when
1858 reorg finishes.
1860 We used to try to update the live status of registers if WHERE is at
1861 the start of a basic block, but that can't work since we may remove a
1862 BARRIER in relax_delay_slots. */
1864 static void
1865 update_block (rtx insn, rtx where)
1867 /* Ignore if this was in a delay slot and it came from the target of
1868 a branch. */
1869 if (INSN_FROM_TARGET_P (insn))
1870 return;
1872 emit_insn_before (gen_rtx_USE (VOIDmode, insn), where);
1874 /* INSN might be making a value live in a block where it didn't use to
1875 be. So recompute liveness information for this block. */
1877 incr_ticks_for_insn (insn);
1880 /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1881 the basic block containing the jump. */
1883 static int
1884 reorg_redirect_jump (rtx jump, rtx nlabel)
1886 incr_ticks_for_insn (jump);
1887 return redirect_jump (jump, nlabel, 1);
1890 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1891 We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1892 that reference values used in INSN. If we find one, then we move the
1893 REG_DEAD note to INSN.
1895 This is needed to handle the case where a later insn (after INSN) has a
1896 REG_DEAD note for a register used by INSN, and this later insn subsequently
1897 gets moved before a CODE_LABEL because it is a redundant insn. In this
1898 case, mark_target_live_regs may be confused into thinking the register
1899 is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1901 static void
1902 update_reg_dead_notes (rtx insn, rtx delayed_insn)
1904 rtx p, link, next;
1906 for (p = next_nonnote_insn (insn); p != delayed_insn;
1907 p = next_nonnote_insn (p))
1908 for (link = REG_NOTES (p); link; link = next)
1910 next = XEXP (link, 1);
1912 if (REG_NOTE_KIND (link) != REG_DEAD
1913 || !REG_P (XEXP (link, 0)))
1914 continue;
1916 if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
1918 /* Move the REG_DEAD note from P to INSN. */
1919 remove_note (p, link);
1920 XEXP (link, 1) = REG_NOTES (insn);
1921 REG_NOTES (insn) = link;
1926 /* Called when an insn redundant with start_insn is deleted. If there
1927 is a REG_DEAD note for the target of start_insn between start_insn
1928 and stop_insn, then the REG_DEAD note needs to be deleted since the
1929 value no longer dies there.
1931 If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1932 confused into thinking the register is dead. */
1934 static void
1935 fix_reg_dead_note (rtx start_insn, rtx stop_insn)
1937 rtx p, link, next;
1939 for (p = next_nonnote_insn (start_insn); p != stop_insn;
1940 p = next_nonnote_insn (p))
1941 for (link = REG_NOTES (p); link; link = next)
1943 next = XEXP (link, 1);
1945 if (REG_NOTE_KIND (link) != REG_DEAD
1946 || !REG_P (XEXP (link, 0)))
1947 continue;
1949 if (reg_set_p (XEXP (link, 0), PATTERN (start_insn)))
1951 remove_note (p, link);
1952 return;
1957 /* Delete any REG_UNUSED notes that exist on INSN but not on REDUNDANT_INSN.
1959 This handles the case of udivmodXi4 instructions which optimize their
1960 output depending on whether any REG_UNUSED notes are present.
1961 we must make sure that INSN calculates as many results as REDUNDANT_INSN
1962 does. */
1964 static void
1965 update_reg_unused_notes (rtx insn, rtx redundant_insn)
1967 rtx link, next;
1969 for (link = REG_NOTES (insn); link; link = next)
1971 next = XEXP (link, 1);
1973 if (REG_NOTE_KIND (link) != REG_UNUSED
1974 || !REG_P (XEXP (link, 0)))
1975 continue;
1977 if (! find_regno_note (redundant_insn, REG_UNUSED,
1978 REGNO (XEXP (link, 0))))
1979 remove_note (insn, link);
1983 /* Return the label before INSN, or put a new label there. */
1985 static rtx
1986 get_label_before (rtx insn)
1988 rtx label;
1990 /* Find an existing label at this point
1991 or make a new one if there is none. */
1992 label = prev_nonnote_insn (insn);
1994 if (label == 0 || !LABEL_P (label))
1996 rtx prev = PREV_INSN (insn);
1998 label = gen_label_rtx ();
1999 emit_label_after (label, prev);
2000 LABEL_NUSES (label) = 0;
2002 return label;
2005 /* Scan a function looking for insns that need a delay slot and find insns to
2006 put into the delay slot.
2008 NON_JUMPS_P is nonzero if we are to only try to fill non-jump insns (such
2009 as calls). We do these first since we don't want jump insns (that are
2010 easier to fill) to get the only insns that could be used for non-jump insns.
2011 When it is zero, only try to fill JUMP_INSNs.
2013 When slots are filled in this manner, the insns (including the
2014 delay_insn) are put together in a SEQUENCE rtx. In this fashion,
2015 it is possible to tell whether a delay slot has really been filled
2016 or not. `final' knows how to deal with this, by communicating
2017 through FINAL_SEQUENCE. */
2019 static void
2020 fill_simple_delay_slots (int non_jumps_p)
2022 rtx insn, pat, trial, next_trial;
2023 int i;
2024 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2025 struct resources needed, set;
2026 int slots_to_fill, slots_filled;
2027 rtx delay_list;
2029 for (i = 0; i < num_unfilled_slots; i++)
2031 int flags;
2032 /* Get the next insn to fill. If it has already had any slots assigned,
2033 we can't do anything with it. Maybe we'll improve this later. */
2035 insn = unfilled_slots_base[i];
2036 if (insn == 0
2037 || INSN_DELETED_P (insn)
2038 || (NONJUMP_INSN_P (insn)
2039 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2040 || (JUMP_P (insn) && non_jumps_p)
2041 || (!JUMP_P (insn) && ! non_jumps_p))
2042 continue;
2044 /* It may have been that this insn used to need delay slots, but
2045 now doesn't; ignore in that case. This can happen, for example,
2046 on the HP PA RISC, where the number of delay slots depends on
2047 what insns are nearby. */
2048 slots_to_fill = num_delay_slots (insn);
2050 /* Some machine description have defined instructions to have
2051 delay slots only in certain circumstances which may depend on
2052 nearby insns (which change due to reorg's actions).
2054 For example, the PA port normally has delay slots for unconditional
2055 jumps.
2057 However, the PA port claims such jumps do not have a delay slot
2058 if they are immediate successors of certain CALL_INSNs. This
2059 allows the port to favor filling the delay slot of the call with
2060 the unconditional jump. */
2061 if (slots_to_fill == 0)
2062 continue;
2064 /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
2065 says how many. After initialization, first try optimizing
2067 call _foo call _foo
2068 nop add %o7,.-L1,%o7
2069 b,a L1
2072 If this case applies, the delay slot of the call is filled with
2073 the unconditional jump. This is done first to avoid having the
2074 delay slot of the call filled in the backward scan. Also, since
2075 the unconditional jump is likely to also have a delay slot, that
2076 insn must exist when it is subsequently scanned.
2078 This is tried on each insn with delay slots as some machines
2079 have insns which perform calls, but are not represented as
2080 CALL_INSNs. */
2082 slots_filled = 0;
2083 delay_list = 0;
2085 if (JUMP_P (insn))
2086 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2087 else
2088 flags = get_jump_flags (insn, NULL_RTX);
2090 if ((trial = next_active_insn (insn))
2091 && JUMP_P (trial)
2092 && simplejump_p (trial)
2093 && eligible_for_delay (insn, slots_filled, trial, flags)
2094 && no_labels_between_p (insn, trial)
2095 && ! can_throw_internal (trial))
2097 rtx *tmp;
2098 slots_filled++;
2099 delay_list = add_to_delay_list (trial, delay_list);
2101 /* TRIAL may have had its delay slot filled, then unfilled. When
2102 the delay slot is unfilled, TRIAL is placed back on the unfilled
2103 slots obstack. Unfortunately, it is placed on the end of the
2104 obstack, not in its original location. Therefore, we must search
2105 from entry i + 1 to the end of the unfilled slots obstack to
2106 try and find TRIAL. */
2107 tmp = &unfilled_slots_base[i + 1];
2108 while (*tmp != trial && tmp != unfilled_slots_next)
2109 tmp++;
2111 /* Remove the unconditional jump from consideration for delay slot
2112 filling and unthread it. */
2113 if (*tmp == trial)
2114 *tmp = 0;
2116 rtx next = NEXT_INSN (trial);
2117 rtx prev = PREV_INSN (trial);
2118 if (prev)
2119 NEXT_INSN (prev) = next;
2120 if (next)
2121 PREV_INSN (next) = prev;
2125 /* Now, scan backwards from the insn to search for a potential
2126 delay-slot candidate. Stop searching when a label or jump is hit.
2128 For each candidate, if it is to go into the delay slot (moved
2129 forward in execution sequence), it must not need or set any resources
2130 that were set by later insns and must not set any resources that
2131 are needed for those insns.
2133 The delay slot insn itself sets resources unless it is a call
2134 (in which case the called routine, not the insn itself, is doing
2135 the setting). */
2137 if (slots_filled < slots_to_fill)
2139 CLEAR_RESOURCE (&needed);
2140 CLEAR_RESOURCE (&set);
2141 mark_set_resources (insn, &set, 0, MARK_SRC_DEST);
2142 mark_referenced_resources (insn, &needed, false);
2144 for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, 1);
2145 trial = next_trial)
2147 next_trial = prev_nonnote_insn (trial);
2149 /* This must be an INSN or CALL_INSN. */
2150 pat = PATTERN (trial);
2152 /* Stand-alone USE and CLOBBER are just for flow. */
2153 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2154 continue;
2156 /* Check for resource conflict first, to avoid unnecessary
2157 splitting. */
2158 if (! insn_references_resource_p (trial, &set, true)
2159 && ! insn_sets_resource_p (trial, &set, true)
2160 && ! insn_sets_resource_p (trial, &needed, true)
2161 #ifdef HAVE_cc0
2162 /* Can't separate set of cc0 from its use. */
2163 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2164 #endif
2165 && ! can_throw_internal (trial))
2167 trial = try_split (pat, trial, 1);
2168 next_trial = prev_nonnote_insn (trial);
2169 if (eligible_for_delay (insn, slots_filled, trial, flags))
2171 /* In this case, we are searching backward, so if we
2172 find insns to put on the delay list, we want
2173 to put them at the head, rather than the
2174 tail, of the list. */
2176 update_reg_dead_notes (trial, insn);
2177 delay_list = gen_rtx_INSN_LIST (VOIDmode,
2178 trial, delay_list);
2179 update_block (trial, trial);
2180 delete_related_insns (trial);
2181 if (slots_to_fill == ++slots_filled)
2182 break;
2183 continue;
2187 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2188 mark_referenced_resources (trial, &needed, true);
2192 /* If all needed slots haven't been filled, we come here. */
2194 /* Try to optimize case of jumping around a single insn. */
2195 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
2196 if (slots_filled != slots_to_fill
2197 && delay_list == 0
2198 && JUMP_P (insn)
2199 && (condjump_p (insn) || condjump_in_parallel_p (insn))
2200 && !ANY_RETURN_P (JUMP_LABEL (insn)))
2202 delay_list = optimize_skip (insn);
2203 if (delay_list)
2204 slots_filled += 1;
2206 #endif
2208 /* Try to get insns from beyond the insn needing the delay slot.
2209 These insns can neither set or reference resources set in insns being
2210 skipped, cannot set resources in the insn being skipped, and, if this
2211 is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2212 call might not return).
2214 There used to be code which continued past the target label if
2215 we saw all uses of the target label. This code did not work,
2216 because it failed to account for some instructions which were
2217 both annulled and marked as from the target. This can happen as a
2218 result of optimize_skip. Since this code was redundant with
2219 fill_eager_delay_slots anyways, it was just deleted. */
2221 if (slots_filled != slots_to_fill
2222 /* If this instruction could throw an exception which is
2223 caught in the same function, then it's not safe to fill
2224 the delay slot with an instruction from beyond this
2225 point. For example, consider:
2227 int i = 2;
2229 try {
2230 f();
2231 i = 3;
2232 } catch (...) {}
2234 return i;
2236 Even though `i' is a local variable, we must be sure not
2237 to put `i = 3' in the delay slot if `f' might throw an
2238 exception.
2240 Presumably, we should also check to see if we could get
2241 back to this function via `setjmp'. */
2242 && ! can_throw_internal (insn)
2243 && (!JUMP_P (insn)
2244 || ((condjump_p (insn) || condjump_in_parallel_p (insn))
2245 && ! simplejump_p (insn)
2246 && !ANY_RETURN_P (JUMP_LABEL (insn)))))
2248 /* Invariant: If insn is a JUMP_INSN, the insn's jump
2249 label. Otherwise, zero. */
2250 rtx target = 0;
2251 int maybe_never = 0;
2252 rtx pat, trial_delay;
2254 CLEAR_RESOURCE (&needed);
2255 CLEAR_RESOURCE (&set);
2257 if (CALL_P (insn))
2259 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2260 mark_referenced_resources (insn, &needed, true);
2261 maybe_never = 1;
2263 else
2265 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2266 mark_referenced_resources (insn, &needed, true);
2267 if (JUMP_P (insn))
2268 target = JUMP_LABEL (insn);
2271 if (target == 0 || ANY_RETURN_P (target))
2272 for (trial = next_nonnote_insn (insn); !stop_search_p (trial, 1);
2273 trial = next_trial)
2275 next_trial = next_nonnote_insn (trial);
2277 /* This must be an INSN or CALL_INSN. */
2278 pat = PATTERN (trial);
2280 /* Stand-alone USE and CLOBBER are just for flow. */
2281 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2282 continue;
2284 /* If this already has filled delay slots, get the insn needing
2285 the delay slots. */
2286 if (GET_CODE (pat) == SEQUENCE)
2287 trial_delay = XVECEXP (pat, 0, 0);
2288 else
2289 trial_delay = trial;
2291 /* Stop our search when seeing a jump. */
2292 if (JUMP_P (trial_delay))
2293 break;
2295 /* See if we have a resource problem before we try to
2296 split. */
2297 if (GET_CODE (pat) != SEQUENCE
2298 && ! insn_references_resource_p (trial, &set, true)
2299 && ! insn_sets_resource_p (trial, &set, true)
2300 && ! insn_sets_resource_p (trial, &needed, true)
2301 #ifdef HAVE_cc0
2302 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2303 #endif
2304 && ! (maybe_never && may_trap_or_fault_p (pat))
2305 && (trial = try_split (pat, trial, 0))
2306 && eligible_for_delay (insn, slots_filled, trial, flags)
2307 && ! can_throw_internal(trial))
2309 next_trial = next_nonnote_insn (trial);
2310 delay_list = add_to_delay_list (trial, delay_list);
2312 #ifdef HAVE_cc0
2313 if (reg_mentioned_p (cc0_rtx, pat))
2314 link_cc0_insns (trial);
2315 #endif
2317 delete_related_insns (trial);
2318 if (slots_to_fill == ++slots_filled)
2319 break;
2320 continue;
2323 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2324 mark_referenced_resources (trial, &needed, true);
2326 /* Ensure we don't put insns between the setting of cc and the
2327 comparison by moving a setting of cc into an earlier delay
2328 slot since these insns could clobber the condition code. */
2329 set.cc = 1;
2331 /* If this is a call or jump, we might not get here. */
2332 if (CALL_P (trial_delay)
2333 || JUMP_P (trial_delay))
2334 maybe_never = 1;
2337 /* If there are slots left to fill and our search was stopped by an
2338 unconditional branch, try the insn at the branch target. We can
2339 redirect the branch if it works.
2341 Don't do this if the insn at the branch target is a branch. */
2342 if (slots_to_fill != slots_filled
2343 && trial
2344 && jump_to_label_p (trial)
2345 && simplejump_p (trial)
2346 && (target == 0 || JUMP_LABEL (trial) == target)
2347 && (next_trial = next_active_insn (JUMP_LABEL (trial))) != 0
2348 && ! (NONJUMP_INSN_P (next_trial)
2349 && GET_CODE (PATTERN (next_trial)) == SEQUENCE)
2350 && !JUMP_P (next_trial)
2351 && ! insn_references_resource_p (next_trial, &set, true)
2352 && ! insn_sets_resource_p (next_trial, &set, true)
2353 && ! insn_sets_resource_p (next_trial, &needed, true)
2354 #ifdef HAVE_cc0
2355 && ! reg_mentioned_p (cc0_rtx, PATTERN (next_trial))
2356 #endif
2357 && ! (maybe_never && may_trap_or_fault_p (PATTERN (next_trial)))
2358 && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2359 && eligible_for_delay (insn, slots_filled, next_trial, flags)
2360 && ! can_throw_internal (trial))
2362 /* See comment in relax_delay_slots about necessity of using
2363 next_real_insn here. */
2364 rtx new_label = next_real_insn (next_trial);
2366 if (new_label != 0)
2367 new_label = get_label_before (new_label);
2368 else
2369 new_label = find_end_label (simple_return_rtx);
2371 if (new_label)
2373 delay_list
2374 = add_to_delay_list (copy_delay_slot_insn (next_trial),
2375 delay_list);
2376 slots_filled++;
2377 reorg_redirect_jump (trial, new_label);
2379 /* If we merged because we both jumped to the same place,
2380 redirect the original insn also. */
2381 if (target)
2382 reorg_redirect_jump (insn, new_label);
2387 /* If this is an unconditional jump, then try to get insns from the
2388 target of the jump. */
2389 if (JUMP_P (insn)
2390 && simplejump_p (insn)
2391 && slots_filled != slots_to_fill)
2392 delay_list
2393 = fill_slots_from_thread (insn, const_true_rtx,
2394 next_active_insn (JUMP_LABEL (insn)),
2395 NULL, 1, 1,
2396 own_thread_p (JUMP_LABEL (insn),
2397 JUMP_LABEL (insn), 0),
2398 slots_to_fill, &slots_filled,
2399 delay_list);
2401 if (delay_list)
2402 unfilled_slots_base[i]
2403 = emit_delay_sequence (insn, delay_list, slots_filled);
2405 if (slots_to_fill == slots_filled)
2406 unfilled_slots_base[i] = 0;
2408 note_delay_statistics (slots_filled, 0);
2411 #ifdef DELAY_SLOTS_FOR_EPILOGUE
2412 /* See if the epilogue needs any delay slots. Try to fill them if so.
2413 The only thing we can do is scan backwards from the end of the
2414 function. If we did this in a previous pass, it is incorrect to do it
2415 again. */
2416 if (crtl->epilogue_delay_list)
2417 return;
2419 slots_to_fill = DELAY_SLOTS_FOR_EPILOGUE;
2420 if (slots_to_fill == 0)
2421 return;
2423 slots_filled = 0;
2424 CLEAR_RESOURCE (&set);
2426 /* The frame pointer and stack pointer are needed at the beginning of
2427 the epilogue, so instructions setting them can not be put in the
2428 epilogue delay slot. However, everything else needed at function
2429 end is safe, so we don't want to use end_of_function_needs here. */
2430 CLEAR_RESOURCE (&needed);
2431 if (frame_pointer_needed)
2433 SET_HARD_REG_BIT (needed.regs, FRAME_POINTER_REGNUM);
2434 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2435 SET_HARD_REG_BIT (needed.regs, HARD_FRAME_POINTER_REGNUM);
2436 #endif
2437 if (! EXIT_IGNORE_STACK
2438 || crtl->sp_is_unchanging)
2439 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2441 else
2442 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2444 #ifdef EPILOGUE_USES
2445 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2447 if (EPILOGUE_USES (i))
2448 SET_HARD_REG_BIT (needed.regs, i);
2450 #endif
2452 for (trial = get_last_insn (); ! stop_search_p (trial, 1);
2453 trial = PREV_INSN (trial))
2455 if (NOTE_P (trial))
2456 continue;
2457 pat = PATTERN (trial);
2458 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2459 continue;
2461 if (! insn_references_resource_p (trial, &set, true)
2462 && ! insn_sets_resource_p (trial, &needed, true)
2463 && ! insn_sets_resource_p (trial, &set, true)
2464 #ifdef HAVE_cc0
2465 /* Don't want to mess with cc0 here. */
2466 && ! reg_mentioned_p (cc0_rtx, pat)
2467 #endif
2468 && ! can_throw_internal (trial))
2470 trial = try_split (pat, trial, 1);
2471 if (ELIGIBLE_FOR_EPILOGUE_DELAY (trial, slots_filled))
2473 /* Here as well we are searching backward, so put the
2474 insns we find on the head of the list. */
2476 crtl->epilogue_delay_list
2477 = gen_rtx_INSN_LIST (VOIDmode, trial,
2478 crtl->epilogue_delay_list);
2479 mark_end_of_function_resources (trial, true);
2480 update_block (trial, trial);
2481 delete_related_insns (trial);
2483 /* Clear deleted bit so final.c will output the insn. */
2484 INSN_DELETED_P (trial) = 0;
2486 if (slots_to_fill == ++slots_filled)
2487 break;
2488 continue;
2492 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2493 mark_referenced_resources (trial, &needed, true);
2496 note_delay_statistics (slots_filled, 0);
2497 #endif
2500 /* Follow any unconditional jump at LABEL, for the purpose of redirecting JUMP;
2501 return the ultimate label reached by any such chain of jumps.
2502 Return a suitable return rtx if the chain ultimately leads to a
2503 return instruction.
2504 If LABEL is not followed by a jump, return LABEL.
2505 If the chain loops or we can't find end, return LABEL,
2506 since that tells caller to avoid changing the insn.
2507 If the returned label is obtained by following a REG_CROSSING_JUMP
2508 jump, set *CROSSING to true, otherwise set it to false. */
2510 static rtx
2511 follow_jumps (rtx label, rtx jump, bool *crossing)
2513 rtx insn;
2514 rtx next;
2515 rtx value = label;
2516 int depth;
2518 *crossing = false;
2519 if (ANY_RETURN_P (label))
2520 return label;
2521 for (depth = 0;
2522 (depth < 10
2523 && (insn = next_active_insn (value)) != 0
2524 && JUMP_P (insn)
2525 && JUMP_LABEL (insn) != NULL_RTX
2526 && ((any_uncondjump_p (insn) && onlyjump_p (insn))
2527 || ANY_RETURN_P (PATTERN (insn)))
2528 && (next = NEXT_INSN (insn))
2529 && BARRIER_P (next));
2530 depth++)
2532 rtx this_label = JUMP_LABEL (insn);
2533 rtx tem;
2535 /* If we have found a cycle, make the insn jump to itself. */
2536 if (this_label == label)
2537 return label;
2538 if (ANY_RETURN_P (this_label))
2539 return this_label;
2540 tem = next_active_insn (this_label);
2541 if (tem
2542 && (GET_CODE (PATTERN (tem)) == ADDR_VEC
2543 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
2544 break;
2546 if (!targetm.can_follow_jump (jump, insn))
2547 break;
2548 if (!*crossing)
2549 *crossing
2550 = find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX) != NULL_RTX;
2551 value = this_label;
2553 if (depth == 10)
2554 return label;
2555 return value;
2558 /* Try to find insns to place in delay slots.
2560 INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2561 or is an unconditional branch if CONDITION is const_true_rtx.
2562 *PSLOTS_FILLED is updated with the number of slots that we have filled.
2564 THREAD is a flow-of-control, either the insns to be executed if the
2565 branch is true or if the branch is false, THREAD_IF_TRUE says which.
2567 OPPOSITE_THREAD is the thread in the opposite direction. It is used
2568 to see if any potential delay slot insns set things needed there.
2570 LIKELY is nonzero if it is extremely likely that the branch will be
2571 taken and THREAD_IF_TRUE is set. This is used for the branch at the
2572 end of a loop back up to the top.
2574 OWN_THREAD and OWN_OPPOSITE_THREAD are true if we are the only user of the
2575 thread. I.e., it is the fallthrough code of our jump or the target of the
2576 jump when we are the only jump going there.
2578 If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2579 case, we can only take insns from the head of the thread for our delay
2580 slot. We then adjust the jump to point after the insns we have taken. */
2582 static rtx
2583 fill_slots_from_thread (rtx insn, rtx condition, rtx thread,
2584 rtx opposite_thread, int likely, int thread_if_true,
2585 int own_thread, int slots_to_fill,
2586 int *pslots_filled, rtx delay_list)
2588 rtx new_thread;
2589 struct resources opposite_needed, set, needed;
2590 rtx trial;
2591 int lose = 0;
2592 int must_annul = 0;
2593 int flags;
2595 /* Validate our arguments. */
2596 gcc_assert(condition != const_true_rtx || thread_if_true);
2597 gcc_assert(own_thread || thread_if_true);
2599 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2601 /* If our thread is the end of subroutine, we can't get any delay
2602 insns from that. */
2603 if (thread == NULL_RTX || ANY_RETURN_P (thread))
2604 return delay_list;
2606 /* If this is an unconditional branch, nothing is needed at the
2607 opposite thread. Otherwise, compute what is needed there. */
2608 if (condition == const_true_rtx)
2609 CLEAR_RESOURCE (&opposite_needed);
2610 else
2611 mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2613 /* If the insn at THREAD can be split, do it here to avoid having to
2614 update THREAD and NEW_THREAD if it is done in the loop below. Also
2615 initialize NEW_THREAD. */
2617 new_thread = thread = try_split (PATTERN (thread), thread, 0);
2619 /* Scan insns at THREAD. We are looking for an insn that can be removed
2620 from THREAD (it neither sets nor references resources that were set
2621 ahead of it and it doesn't set anything needs by the insns ahead of
2622 it) and that either can be placed in an annulling insn or aren't
2623 needed at OPPOSITE_THREAD. */
2625 CLEAR_RESOURCE (&needed);
2626 CLEAR_RESOURCE (&set);
2628 /* If we do not own this thread, we must stop as soon as we find
2629 something that we can't put in a delay slot, since all we can do
2630 is branch into THREAD at a later point. Therefore, labels stop
2631 the search if this is not the `true' thread. */
2633 for (trial = thread;
2634 ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2635 trial = next_nonnote_insn (trial))
2637 rtx pat, old_trial;
2639 /* If we have passed a label, we no longer own this thread. */
2640 if (LABEL_P (trial))
2642 own_thread = 0;
2643 continue;
2646 pat = PATTERN (trial);
2647 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2648 continue;
2650 /* If TRIAL conflicts with the insns ahead of it, we lose. Also,
2651 don't separate or copy insns that set and use CC0. */
2652 if (! insn_references_resource_p (trial, &set, true)
2653 && ! insn_sets_resource_p (trial, &set, true)
2654 && ! insn_sets_resource_p (trial, &needed, true)
2655 #ifdef HAVE_cc0
2656 && ! (reg_mentioned_p (cc0_rtx, pat)
2657 && (! own_thread || ! sets_cc0_p (pat)))
2658 #endif
2659 && ! can_throw_internal (trial))
2661 rtx prior_insn;
2663 /* If TRIAL is redundant with some insn before INSN, we don't
2664 actually need to add it to the delay list; we can merely pretend
2665 we did. */
2666 if ((prior_insn = redundant_insn (trial, insn, delay_list)))
2668 fix_reg_dead_note (prior_insn, insn);
2669 if (own_thread)
2671 update_block (trial, thread);
2672 if (trial == thread)
2674 thread = next_active_insn (thread);
2675 if (new_thread == trial)
2676 new_thread = thread;
2679 delete_related_insns (trial);
2681 else
2683 update_reg_unused_notes (prior_insn, trial);
2684 new_thread = next_active_insn (trial);
2687 continue;
2690 /* There are two ways we can win: If TRIAL doesn't set anything
2691 needed at the opposite thread and can't trap, or if it can
2692 go into an annulled delay slot. */
2693 if (!must_annul
2694 && (condition == const_true_rtx
2695 || (! insn_sets_resource_p (trial, &opposite_needed, true)
2696 && ! may_trap_or_fault_p (pat)
2697 && ! RTX_FRAME_RELATED_P (trial))))
2699 old_trial = trial;
2700 trial = try_split (pat, trial, 0);
2701 if (new_thread == old_trial)
2702 new_thread = trial;
2703 if (thread == old_trial)
2704 thread = trial;
2705 pat = PATTERN (trial);
2706 if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2707 goto winner;
2709 else if (0
2710 #ifdef ANNUL_IFTRUE_SLOTS
2711 || ! thread_if_true
2712 #endif
2713 #ifdef ANNUL_IFFALSE_SLOTS
2714 || thread_if_true
2715 #endif
2718 old_trial = trial;
2719 trial = try_split (pat, trial, 0);
2720 if (new_thread == old_trial)
2721 new_thread = trial;
2722 if (thread == old_trial)
2723 thread = trial;
2724 pat = PATTERN (trial);
2725 if ((must_annul || delay_list == NULL) && (thread_if_true
2726 ? check_annul_list_true_false (0, delay_list)
2727 && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2728 : check_annul_list_true_false (1, delay_list)
2729 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2731 rtx temp;
2733 must_annul = 1;
2734 winner:
2736 #ifdef HAVE_cc0
2737 if (reg_mentioned_p (cc0_rtx, pat))
2738 link_cc0_insns (trial);
2739 #endif
2741 /* If we own this thread, delete the insn. If this is the
2742 destination of a branch, show that a basic block status
2743 may have been updated. In any case, mark the new
2744 starting point of this thread. */
2745 if (own_thread)
2747 rtx note;
2749 update_block (trial, thread);
2750 if (trial == thread)
2752 thread = next_active_insn (thread);
2753 if (new_thread == trial)
2754 new_thread = thread;
2757 /* We are moving this insn, not deleting it. We must
2758 temporarily increment the use count on any referenced
2759 label lest it be deleted by delete_related_insns. */
2760 for (note = REG_NOTES (trial);
2761 note != NULL_RTX;
2762 note = XEXP (note, 1))
2763 if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2764 || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2766 /* REG_LABEL_OPERAND could be
2767 NOTE_INSN_DELETED_LABEL too. */
2768 if (LABEL_P (XEXP (note, 0)))
2769 LABEL_NUSES (XEXP (note, 0))++;
2770 else
2771 gcc_assert (REG_NOTE_KIND (note)
2772 == REG_LABEL_OPERAND);
2774 if (jump_to_label_p (trial))
2775 LABEL_NUSES (JUMP_LABEL (trial))++;
2777 delete_related_insns (trial);
2779 for (note = REG_NOTES (trial);
2780 note != NULL_RTX;
2781 note = XEXP (note, 1))
2782 if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2783 || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2785 /* REG_LABEL_OPERAND could be
2786 NOTE_INSN_DELETED_LABEL too. */
2787 if (LABEL_P (XEXP (note, 0)))
2788 LABEL_NUSES (XEXP (note, 0))--;
2789 else
2790 gcc_assert (REG_NOTE_KIND (note)
2791 == REG_LABEL_OPERAND);
2793 if (jump_to_label_p (trial))
2794 LABEL_NUSES (JUMP_LABEL (trial))--;
2796 else
2797 new_thread = next_active_insn (trial);
2799 temp = own_thread ? trial : copy_delay_slot_insn (trial);
2800 if (thread_if_true)
2801 INSN_FROM_TARGET_P (temp) = 1;
2803 delay_list = add_to_delay_list (temp, delay_list);
2805 if (slots_to_fill == ++(*pslots_filled))
2807 /* Even though we have filled all the slots, we
2808 may be branching to a location that has a
2809 redundant insn. Skip any if so. */
2810 while (new_thread && ! own_thread
2811 && ! insn_sets_resource_p (new_thread, &set, true)
2812 && ! insn_sets_resource_p (new_thread, &needed,
2813 true)
2814 && ! insn_references_resource_p (new_thread,
2815 &set, true)
2816 && (prior_insn
2817 = redundant_insn (new_thread, insn,
2818 delay_list)))
2820 /* We know we do not own the thread, so no need
2821 to call update_block and delete_insn. */
2822 fix_reg_dead_note (prior_insn, insn);
2823 update_reg_unused_notes (prior_insn, new_thread);
2824 new_thread = next_active_insn (new_thread);
2826 break;
2829 continue;
2834 /* This insn can't go into a delay slot. */
2835 lose = 1;
2836 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2837 mark_referenced_resources (trial, &needed, true);
2839 /* Ensure we don't put insns between the setting of cc and the comparison
2840 by moving a setting of cc into an earlier delay slot since these insns
2841 could clobber the condition code. */
2842 set.cc = 1;
2844 /* If this insn is a register-register copy and the next insn has
2845 a use of our destination, change it to use our source. That way,
2846 it will become a candidate for our delay slot the next time
2847 through this loop. This case occurs commonly in loops that
2848 scan a list.
2850 We could check for more complex cases than those tested below,
2851 but it doesn't seem worth it. It might also be a good idea to try
2852 to swap the two insns. That might do better.
2854 We can't do this if the next insn modifies our destination, because
2855 that would make the replacement into the insn invalid. We also can't
2856 do this if it modifies our source, because it might be an earlyclobber
2857 operand. This latter test also prevents updating the contents of
2858 a PRE_INC. We also can't do this if there's overlap of source and
2859 destination. Overlap may happen for larger-than-register-size modes. */
2861 if (NONJUMP_INSN_P (trial) && GET_CODE (pat) == SET
2862 && REG_P (SET_SRC (pat))
2863 && REG_P (SET_DEST (pat))
2864 && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2866 rtx next = next_nonnote_insn (trial);
2868 if (next && NONJUMP_INSN_P (next)
2869 && GET_CODE (PATTERN (next)) != USE
2870 && ! reg_set_p (SET_DEST (pat), next)
2871 && ! reg_set_p (SET_SRC (pat), next)
2872 && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2873 && ! modified_in_p (SET_DEST (pat), next))
2874 validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2878 /* If we stopped on a branch insn that has delay slots, see if we can
2879 steal some of the insns in those slots. */
2880 if (trial && NONJUMP_INSN_P (trial)
2881 && GET_CODE (PATTERN (trial)) == SEQUENCE
2882 && JUMP_P (XVECEXP (PATTERN (trial), 0, 0)))
2884 /* If this is the `true' thread, we will want to follow the jump,
2885 so we can only do this if we have taken everything up to here. */
2886 if (thread_if_true && trial == new_thread)
2888 delay_list
2889 = steal_delay_list_from_target (insn, condition, PATTERN (trial),
2890 delay_list, &set, &needed,
2891 &opposite_needed, slots_to_fill,
2892 pslots_filled, &must_annul,
2893 &new_thread);
2894 /* If we owned the thread and are told that it branched
2895 elsewhere, make sure we own the thread at the new location. */
2896 if (own_thread && trial != new_thread)
2897 own_thread = own_thread_p (new_thread, new_thread, 0);
2899 else if (! thread_if_true)
2900 delay_list
2901 = steal_delay_list_from_fallthrough (insn, condition,
2902 PATTERN (trial),
2903 delay_list, &set, &needed,
2904 &opposite_needed, slots_to_fill,
2905 pslots_filled, &must_annul);
2908 /* If we haven't found anything for this delay slot and it is very
2909 likely that the branch will be taken, see if the insn at our target
2910 increments or decrements a register with an increment that does not
2911 depend on the destination register. If so, try to place the opposite
2912 arithmetic insn after the jump insn and put the arithmetic insn in the
2913 delay slot. If we can't do this, return. */
2914 if (delay_list == 0 && likely
2915 && new_thread && !ANY_RETURN_P (new_thread)
2916 && NONJUMP_INSN_P (new_thread)
2917 && !RTX_FRAME_RELATED_P (new_thread)
2918 && GET_CODE (PATTERN (new_thread)) != ASM_INPUT
2919 && asm_noperands (PATTERN (new_thread)) < 0)
2921 rtx pat = PATTERN (new_thread);
2922 rtx dest;
2923 rtx src;
2925 trial = new_thread;
2926 pat = PATTERN (trial);
2928 if (!NONJUMP_INSN_P (trial)
2929 || GET_CODE (pat) != SET
2930 || ! eligible_for_delay (insn, 0, trial, flags)
2931 || can_throw_internal (trial))
2932 return 0;
2934 dest = SET_DEST (pat), src = SET_SRC (pat);
2935 if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2936 && rtx_equal_p (XEXP (src, 0), dest)
2937 && (!FLOAT_MODE_P (GET_MODE (src))
2938 || flag_unsafe_math_optimizations)
2939 && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2940 && ! side_effects_p (pat))
2942 rtx other = XEXP (src, 1);
2943 rtx new_arith;
2944 rtx ninsn;
2946 /* If this is a constant adjustment, use the same code with
2947 the negated constant. Otherwise, reverse the sense of the
2948 arithmetic. */
2949 if (CONST_INT_P (other))
2950 new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2951 negate_rtx (GET_MODE (src), other));
2952 else
2953 new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2954 GET_MODE (src), dest, other);
2956 ninsn = emit_insn_after (gen_rtx_SET (VOIDmode, dest, new_arith),
2957 insn);
2959 if (recog_memoized (ninsn) < 0
2960 || (extract_insn (ninsn), ! constrain_operands (1)))
2962 delete_related_insns (ninsn);
2963 return 0;
2966 if (own_thread)
2968 update_block (trial, thread);
2969 if (trial == thread)
2971 thread = next_active_insn (thread);
2972 if (new_thread == trial)
2973 new_thread = thread;
2975 delete_related_insns (trial);
2977 else
2978 new_thread = next_active_insn (trial);
2980 ninsn = own_thread ? trial : copy_delay_slot_insn (trial);
2981 if (thread_if_true)
2982 INSN_FROM_TARGET_P (ninsn) = 1;
2984 delay_list = add_to_delay_list (ninsn, NULL_RTX);
2985 (*pslots_filled)++;
2989 if (delay_list && must_annul)
2990 INSN_ANNULLED_BRANCH_P (insn) = 1;
2992 /* If we are to branch into the middle of this thread, find an appropriate
2993 label or make a new one if none, and redirect INSN to it. If we hit the
2994 end of the function, use the end-of-function label. */
2995 if (new_thread != thread)
2997 rtx label;
2998 bool crossing = false;
3000 gcc_assert (thread_if_true);
3002 if (new_thread && simplejump_or_return_p (new_thread)
3003 && redirect_with_delay_list_safe_p (insn,
3004 JUMP_LABEL (new_thread),
3005 delay_list))
3006 new_thread = follow_jumps (JUMP_LABEL (new_thread), insn, &crossing);
3008 if (ANY_RETURN_P (new_thread))
3009 label = find_end_label (new_thread);
3010 else if (LABEL_P (new_thread))
3011 label = new_thread;
3012 else
3013 label = get_label_before (new_thread);
3015 if (label)
3017 reorg_redirect_jump (insn, label);
3018 if (crossing)
3019 set_unique_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX);
3023 return delay_list;
3026 /* Make another attempt to find insns to place in delay slots.
3028 We previously looked for insns located in front of the delay insn
3029 and, for non-jump delay insns, located behind the delay insn.
3031 Here only try to schedule jump insns and try to move insns from either
3032 the target or the following insns into the delay slot. If annulling is
3033 supported, we will be likely to do this. Otherwise, we can do this only
3034 if safe. */
3036 static void
3037 fill_eager_delay_slots (void)
3039 rtx insn;
3040 int i;
3041 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
3043 for (i = 0; i < num_unfilled_slots; i++)
3045 rtx condition;
3046 rtx target_label, insn_at_target, fallthrough_insn;
3047 rtx delay_list = 0;
3048 int own_target;
3049 int own_fallthrough;
3050 int prediction, slots_to_fill, slots_filled;
3052 insn = unfilled_slots_base[i];
3053 if (insn == 0
3054 || INSN_DELETED_P (insn)
3055 || !JUMP_P (insn)
3056 || ! (condjump_p (insn) || condjump_in_parallel_p (insn)))
3057 continue;
3059 slots_to_fill = num_delay_slots (insn);
3060 /* Some machine description have defined instructions to have
3061 delay slots only in certain circumstances which may depend on
3062 nearby insns (which change due to reorg's actions).
3064 For example, the PA port normally has delay slots for unconditional
3065 jumps.
3067 However, the PA port claims such jumps do not have a delay slot
3068 if they are immediate successors of certain CALL_INSNs. This
3069 allows the port to favor filling the delay slot of the call with
3070 the unconditional jump. */
3071 if (slots_to_fill == 0)
3072 continue;
3074 slots_filled = 0;
3075 target_label = JUMP_LABEL (insn);
3076 condition = get_branch_condition (insn, target_label);
3078 if (condition == 0)
3079 continue;
3081 /* Get the next active fallthrough and target insns and see if we own
3082 them. Then see whether the branch is likely true. We don't need
3083 to do a lot of this for unconditional branches. */
3085 insn_at_target = first_active_target_insn (target_label);
3086 own_target = own_thread_p (target_label, target_label, 0);
3088 if (condition == const_true_rtx)
3090 own_fallthrough = 0;
3091 fallthrough_insn = 0;
3092 prediction = 2;
3094 else
3096 fallthrough_insn = next_active_insn (insn);
3097 own_fallthrough = own_thread_p (NEXT_INSN (insn), NULL_RTX, 1);
3098 prediction = mostly_true_jump (insn, condition);
3101 /* If this insn is expected to branch, first try to get insns from our
3102 target, then our fallthrough insns. If it is not expected to branch,
3103 try the other order. */
3105 if (prediction > 0)
3107 delay_list
3108 = fill_slots_from_thread (insn, condition, insn_at_target,
3109 fallthrough_insn, prediction == 2, 1,
3110 own_target,
3111 slots_to_fill, &slots_filled, delay_list);
3113 if (delay_list == 0 && own_fallthrough)
3115 /* Even though we didn't find anything for delay slots,
3116 we might have found a redundant insn which we deleted
3117 from the thread that was filled. So we have to recompute
3118 the next insn at the target. */
3119 target_label = JUMP_LABEL (insn);
3120 insn_at_target = first_active_target_insn (target_label);
3122 delay_list
3123 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3124 insn_at_target, 0, 0,
3125 own_fallthrough,
3126 slots_to_fill, &slots_filled,
3127 delay_list);
3130 else
3132 if (own_fallthrough)
3133 delay_list
3134 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3135 insn_at_target, 0, 0,
3136 own_fallthrough,
3137 slots_to_fill, &slots_filled,
3138 delay_list);
3140 if (delay_list == 0)
3141 delay_list
3142 = fill_slots_from_thread (insn, condition, insn_at_target,
3143 next_active_insn (insn), 0, 1,
3144 own_target,
3145 slots_to_fill, &slots_filled,
3146 delay_list);
3149 if (delay_list)
3150 unfilled_slots_base[i]
3151 = emit_delay_sequence (insn, delay_list, slots_filled);
3153 if (slots_to_fill == slots_filled)
3154 unfilled_slots_base[i] = 0;
3156 note_delay_statistics (slots_filled, 1);
3160 static void delete_computation (rtx insn);
3162 /* Recursively delete prior insns that compute the value (used only by INSN
3163 which the caller is deleting) stored in the register mentioned by NOTE
3164 which is a REG_DEAD note associated with INSN. */
3166 static void
3167 delete_prior_computation (rtx note, rtx insn)
3169 rtx our_prev;
3170 rtx reg = XEXP (note, 0);
3172 for (our_prev = prev_nonnote_insn (insn);
3173 our_prev && (NONJUMP_INSN_P (our_prev)
3174 || CALL_P (our_prev));
3175 our_prev = prev_nonnote_insn (our_prev))
3177 rtx pat = PATTERN (our_prev);
3179 /* If we reach a CALL which is not calling a const function
3180 or the callee pops the arguments, then give up. */
3181 if (CALL_P (our_prev)
3182 && (! RTL_CONST_CALL_P (our_prev)
3183 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
3184 break;
3186 /* If we reach a SEQUENCE, it is too complex to try to
3187 do anything with it, so give up. We can be run during
3188 and after reorg, so SEQUENCE rtl can legitimately show
3189 up here. */
3190 if (GET_CODE (pat) == SEQUENCE)
3191 break;
3193 if (GET_CODE (pat) == USE
3194 && NONJUMP_INSN_P (XEXP (pat, 0)))
3195 /* reorg creates USEs that look like this. We leave them
3196 alone because reorg needs them for its own purposes. */
3197 break;
3199 if (reg_set_p (reg, pat))
3201 if (side_effects_p (pat) && !CALL_P (our_prev))
3202 break;
3204 if (GET_CODE (pat) == PARALLEL)
3206 /* If we find a SET of something else, we can't
3207 delete the insn. */
3209 int i;
3211 for (i = 0; i < XVECLEN (pat, 0); i++)
3213 rtx part = XVECEXP (pat, 0, i);
3215 if (GET_CODE (part) == SET
3216 && SET_DEST (part) != reg)
3217 break;
3220 if (i == XVECLEN (pat, 0))
3221 delete_computation (our_prev);
3223 else if (GET_CODE (pat) == SET
3224 && REG_P (SET_DEST (pat)))
3226 int dest_regno = REGNO (SET_DEST (pat));
3227 int dest_endregno = END_REGNO (SET_DEST (pat));
3228 int regno = REGNO (reg);
3229 int endregno = END_REGNO (reg);
3231 if (dest_regno >= regno
3232 && dest_endregno <= endregno)
3233 delete_computation (our_prev);
3235 /* We may have a multi-word hard register and some, but not
3236 all, of the words of the register are needed in subsequent
3237 insns. Write REG_UNUSED notes for those parts that were not
3238 needed. */
3239 else if (dest_regno <= regno
3240 && dest_endregno >= endregno)
3242 int i;
3244 add_reg_note (our_prev, REG_UNUSED, reg);
3246 for (i = dest_regno; i < dest_endregno; i++)
3247 if (! find_regno_note (our_prev, REG_UNUSED, i))
3248 break;
3250 if (i == dest_endregno)
3251 delete_computation (our_prev);
3255 break;
3258 /* If PAT references the register that dies here, it is an
3259 additional use. Hence any prior SET isn't dead. However, this
3260 insn becomes the new place for the REG_DEAD note. */
3261 if (reg_overlap_mentioned_p (reg, pat))
3263 XEXP (note, 1) = REG_NOTES (our_prev);
3264 REG_NOTES (our_prev) = note;
3265 break;
3270 /* Delete INSN and recursively delete insns that compute values used only
3271 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3273 Look at all our REG_DEAD notes. If a previous insn does nothing other
3274 than set a register that dies in this insn, we can delete that insn
3275 as well.
3277 On machines with CC0, if CC0 is used in this insn, we may be able to
3278 delete the insn that set it. */
3280 static void
3281 delete_computation (rtx insn)
3283 rtx note, next;
3285 #ifdef HAVE_cc0
3286 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
3288 rtx prev = prev_nonnote_insn (insn);
3289 /* We assume that at this stage
3290 CC's are always set explicitly
3291 and always immediately before the jump that
3292 will use them. So if the previous insn
3293 exists to set the CC's, delete it
3294 (unless it performs auto-increments, etc.). */
3295 if (prev && NONJUMP_INSN_P (prev)
3296 && sets_cc0_p (PATTERN (prev)))
3298 if (sets_cc0_p (PATTERN (prev)) > 0
3299 && ! side_effects_p (PATTERN (prev)))
3300 delete_computation (prev);
3301 else
3302 /* Otherwise, show that cc0 won't be used. */
3303 add_reg_note (prev, REG_UNUSED, cc0_rtx);
3306 #endif
3308 for (note = REG_NOTES (insn); note; note = next)
3310 next = XEXP (note, 1);
3312 if (REG_NOTE_KIND (note) != REG_DEAD
3313 /* Verify that the REG_NOTE is legitimate. */
3314 || !REG_P (XEXP (note, 0)))
3315 continue;
3317 delete_prior_computation (note, insn);
3320 delete_related_insns (insn);
3323 /* If all INSN does is set the pc, delete it,
3324 and delete the insn that set the condition codes for it
3325 if that's what the previous thing was. */
3327 static void
3328 delete_jump (rtx insn)
3330 rtx set = single_set (insn);
3332 if (set && GET_CODE (SET_DEST (set)) == PC)
3333 delete_computation (insn);
3336 static rtx
3337 label_before_next_insn (rtx x, rtx scan_limit)
3339 rtx insn = next_active_insn (x);
3340 while (insn)
3342 insn = PREV_INSN (insn);
3343 if (insn == scan_limit || insn == NULL_RTX)
3344 return NULL_RTX;
3345 if (LABEL_P (insn))
3346 break;
3348 return insn;
3352 /* Once we have tried two ways to fill a delay slot, make a pass over the
3353 code to try to improve the results and to do such things as more jump
3354 threading. */
3356 static void
3357 relax_delay_slots (rtx first)
3359 rtx insn, next, pat;
3360 rtx trial, delay_insn, target_label;
3362 /* Look at every JUMP_INSN and see if we can improve it. */
3363 for (insn = first; insn; insn = next)
3365 rtx other;
3366 bool crossing;
3368 next = next_active_insn (insn);
3370 /* If this is a jump insn, see if it now jumps to a jump, jumps to
3371 the next insn, or jumps to a label that is not the last of a
3372 group of consecutive labels. */
3373 if (JUMP_P (insn)
3374 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3375 && !ANY_RETURN_P (target_label = JUMP_LABEL (insn)))
3377 target_label
3378 = skip_consecutive_labels (follow_jumps (target_label, insn,
3379 &crossing));
3380 if (ANY_RETURN_P (target_label))
3381 target_label = find_end_label (target_label);
3383 if (target_label && next_active_insn (target_label) == next
3384 && ! condjump_in_parallel_p (insn))
3386 delete_jump (insn);
3387 continue;
3390 if (target_label && target_label != JUMP_LABEL (insn))
3392 reorg_redirect_jump (insn, target_label);
3393 if (crossing)
3394 set_unique_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX);
3397 /* See if this jump conditionally branches around an unconditional
3398 jump. If so, invert this jump and point it to the target of the
3399 second jump. */
3400 if (next && simplejump_or_return_p (next)
3401 && any_condjump_p (insn)
3402 && target_label
3403 && next_active_insn (target_label) == next_active_insn (next)
3404 && no_labels_between_p (insn, next))
3406 rtx label = JUMP_LABEL (next);
3408 /* Be careful how we do this to avoid deleting code or
3409 labels that are momentarily dead. See similar optimization
3410 in jump.c.
3412 We also need to ensure we properly handle the case when
3413 invert_jump fails. */
3415 ++LABEL_NUSES (target_label);
3416 if (!ANY_RETURN_P (label))
3417 ++LABEL_NUSES (label);
3419 if (invert_jump (insn, label, 1))
3421 delete_related_insns (next);
3422 next = insn;
3425 if (!ANY_RETURN_P (label))
3426 --LABEL_NUSES (label);
3428 if (--LABEL_NUSES (target_label) == 0)
3429 delete_related_insns (target_label);
3431 continue;
3435 /* If this is an unconditional jump and the previous insn is a
3436 conditional jump, try reversing the condition of the previous
3437 insn and swapping our targets. The next pass might be able to
3438 fill the slots.
3440 Don't do this if we expect the conditional branch to be true, because
3441 we would then be making the more common case longer. */
3443 if (simplejump_or_return_p (insn)
3444 && (other = prev_active_insn (insn)) != 0
3445 && any_condjump_p (other)
3446 && no_labels_between_p (other, insn)
3447 && 0 > mostly_true_jump (other,
3448 get_branch_condition (other,
3449 JUMP_LABEL (other))))
3451 rtx other_target = JUMP_LABEL (other);
3452 target_label = JUMP_LABEL (insn);
3454 if (invert_jump (other, target_label, 0))
3455 reorg_redirect_jump (insn, other_target);
3458 /* Now look only at cases where we have a filled delay slot. */
3459 if (!NONJUMP_INSN_P (insn) || GET_CODE (PATTERN (insn)) != SEQUENCE)
3460 continue;
3462 pat = PATTERN (insn);
3463 delay_insn = XVECEXP (pat, 0, 0);
3465 /* See if the first insn in the delay slot is redundant with some
3466 previous insn. Remove it from the delay slot if so; then set up
3467 to reprocess this insn. */
3468 if (redundant_insn (XVECEXP (pat, 0, 1), delay_insn, 0))
3470 delete_from_delay_slot (XVECEXP (pat, 0, 1));
3471 next = prev_active_insn (next);
3472 continue;
3475 /* See if we have a RETURN insn with a filled delay slot followed
3476 by a RETURN insn with an unfilled a delay slot. If so, we can delete
3477 the first RETURN (but not its delay insn). This gives the same
3478 effect in fewer instructions.
3480 Only do so if optimizing for size since this results in slower, but
3481 smaller code. */
3482 if (optimize_function_for_size_p (cfun)
3483 && ANY_RETURN_P (PATTERN (delay_insn))
3484 && next
3485 && JUMP_P (next)
3486 && PATTERN (next) == PATTERN (delay_insn))
3488 rtx after;
3489 int i;
3491 /* Delete the RETURN and just execute the delay list insns.
3493 We do this by deleting the INSN containing the SEQUENCE, then
3494 re-emitting the insns separately, and then deleting the RETURN.
3495 This allows the count of the jump target to be properly
3496 decremented.
3498 Note that we need to change the INSN_UID of the re-emitted insns
3499 since it is used to hash the insns for mark_target_live_regs and
3500 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3502 Clear the from target bit, since these insns are no longer
3503 in delay slots. */
3504 for (i = 0; i < XVECLEN (pat, 0); i++)
3505 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3507 trial = PREV_INSN (insn);
3508 delete_related_insns (insn);
3509 gcc_assert (GET_CODE (pat) == SEQUENCE);
3510 add_insn_after (delay_insn, trial, NULL);
3511 after = delay_insn;
3512 for (i = 1; i < XVECLEN (pat, 0); i++)
3513 after = emit_copy_of_insn_after (XVECEXP (pat, 0, i), after);
3514 delete_scheduled_jump (delay_insn);
3515 continue;
3518 /* Now look only at the cases where we have a filled JUMP_INSN. */
3519 if (!JUMP_P (delay_insn)
3520 || !(condjump_p (delay_insn) || condjump_in_parallel_p (delay_insn)))
3521 continue;
3523 target_label = JUMP_LABEL (delay_insn);
3524 if (target_label && ANY_RETURN_P (target_label))
3525 continue;
3527 /* If this jump goes to another unconditional jump, thread it, but
3528 don't convert a jump into a RETURN here. */
3529 trial = skip_consecutive_labels (follow_jumps (target_label, delay_insn,
3530 &crossing));
3531 if (ANY_RETURN_P (trial))
3532 trial = find_end_label (trial);
3534 if (trial && trial != target_label
3535 && redirect_with_delay_slots_safe_p (delay_insn, trial, insn))
3537 reorg_redirect_jump (delay_insn, trial);
3538 target_label = trial;
3539 if (crossing)
3540 set_unique_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX);
3543 /* If the first insn at TARGET_LABEL is redundant with a previous
3544 insn, redirect the jump to the following insn and process again.
3545 We use next_real_insn instead of next_active_insn so we
3546 don't skip USE-markers, or we'll end up with incorrect
3547 liveness info. */
3548 trial = next_real_insn (target_label);
3549 if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3550 && redundant_insn (trial, insn, 0)
3551 && ! can_throw_internal (trial))
3553 /* Figure out where to emit the special USE insn so we don't
3554 later incorrectly compute register live/death info. */
3555 rtx tmp = next_active_insn (trial);
3556 if (tmp == 0)
3557 tmp = find_end_label (simple_return_rtx);
3559 if (tmp)
3561 /* Insert the special USE insn and update dataflow info. */
3562 update_block (trial, tmp);
3564 /* Now emit a label before the special USE insn, and
3565 redirect our jump to the new label. */
3566 target_label = get_label_before (PREV_INSN (tmp));
3567 reorg_redirect_jump (delay_insn, target_label);
3568 next = insn;
3569 continue;
3573 /* Similarly, if it is an unconditional jump with one insn in its
3574 delay list and that insn is redundant, thread the jump. */
3575 if (trial && GET_CODE (PATTERN (trial)) == SEQUENCE
3576 && XVECLEN (PATTERN (trial), 0) == 2
3577 && JUMP_P (XVECEXP (PATTERN (trial), 0, 0))
3578 && simplejump_or_return_p (XVECEXP (PATTERN (trial), 0, 0))
3579 && redundant_insn (XVECEXP (PATTERN (trial), 0, 1), insn, 0))
3581 target_label = JUMP_LABEL (XVECEXP (PATTERN (trial), 0, 0));
3582 if (ANY_RETURN_P (target_label))
3583 target_label = find_end_label (target_label);
3585 if (target_label
3586 && redirect_with_delay_slots_safe_p (delay_insn, target_label,
3587 insn))
3589 reorg_redirect_jump (delay_insn, target_label);
3590 next = insn;
3591 continue;
3595 /* See if we have a simple (conditional) jump that is useless. */
3596 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3597 && ! condjump_in_parallel_p (delay_insn)
3598 && prev_active_insn (target_label) == insn
3599 && ! BARRIER_P (prev_nonnote_insn (target_label))
3600 #ifdef HAVE_cc0
3601 /* If the last insn in the delay slot sets CC0 for some insn,
3602 various code assumes that it is in a delay slot. We could
3603 put it back where it belonged and delete the register notes,
3604 but it doesn't seem worthwhile in this uncommon case. */
3605 && ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3606 REG_CC_USER, NULL_RTX)
3607 #endif
3610 rtx after;
3611 int i;
3613 /* All this insn does is execute its delay list and jump to the
3614 following insn. So delete the jump and just execute the delay
3615 list insns.
3617 We do this by deleting the INSN containing the SEQUENCE, then
3618 re-emitting the insns separately, and then deleting the jump.
3619 This allows the count of the jump target to be properly
3620 decremented.
3622 Note that we need to change the INSN_UID of the re-emitted insns
3623 since it is used to hash the insns for mark_target_live_regs and
3624 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3626 Clear the from target bit, since these insns are no longer
3627 in delay slots. */
3628 for (i = 0; i < XVECLEN (pat, 0); i++)
3629 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3631 trial = PREV_INSN (insn);
3632 delete_related_insns (insn);
3633 gcc_assert (GET_CODE (pat) == SEQUENCE);
3634 add_insn_after (delay_insn, trial, NULL);
3635 after = delay_insn;
3636 for (i = 1; i < XVECLEN (pat, 0); i++)
3637 after = emit_copy_of_insn_after (XVECEXP (pat, 0, i), after);
3638 delete_scheduled_jump (delay_insn);
3639 continue;
3642 /* See if this is an unconditional jump around a single insn which is
3643 identical to the one in its delay slot. In this case, we can just
3644 delete the branch and the insn in its delay slot. */
3645 if (next && NONJUMP_INSN_P (next)
3646 && label_before_next_insn (next, insn) == target_label
3647 && simplejump_p (insn)
3648 && XVECLEN (pat, 0) == 2
3649 && rtx_equal_p (PATTERN (next), PATTERN (XVECEXP (pat, 0, 1))))
3651 delete_related_insns (insn);
3652 continue;
3655 /* See if this jump (with its delay slots) conditionally branches
3656 around an unconditional jump (without delay slots). If so, invert
3657 this jump and point it to the target of the second jump. We cannot
3658 do this for annulled jumps, though. Again, don't convert a jump to
3659 a RETURN here. */
3660 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3661 && any_condjump_p (delay_insn)
3662 && next && simplejump_or_return_p (next)
3663 && next_active_insn (target_label) == next_active_insn (next)
3664 && no_labels_between_p (insn, next))
3666 rtx label = JUMP_LABEL (next);
3667 rtx old_label = JUMP_LABEL (delay_insn);
3669 if (ANY_RETURN_P (label))
3670 label = find_end_label (label);
3672 /* find_end_label can generate a new label. Check this first. */
3673 if (label
3674 && no_labels_between_p (insn, next)
3675 && redirect_with_delay_slots_safe_p (delay_insn, label, insn))
3677 /* Be careful how we do this to avoid deleting code or labels
3678 that are momentarily dead. See similar optimization in
3679 jump.c */
3680 if (old_label)
3681 ++LABEL_NUSES (old_label);
3683 if (invert_jump (delay_insn, label, 1))
3685 int i;
3687 /* Must update the INSN_FROM_TARGET_P bits now that
3688 the branch is reversed, so that mark_target_live_regs
3689 will handle the delay slot insn correctly. */
3690 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
3692 rtx slot = XVECEXP (PATTERN (insn), 0, i);
3693 INSN_FROM_TARGET_P (slot) = ! INSN_FROM_TARGET_P (slot);
3696 delete_related_insns (next);
3697 next = insn;
3700 if (old_label && --LABEL_NUSES (old_label) == 0)
3701 delete_related_insns (old_label);
3702 continue;
3706 /* If we own the thread opposite the way this insn branches, see if we
3707 can merge its delay slots with following insns. */
3708 if (INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3709 && own_thread_p (NEXT_INSN (insn), 0, 1))
3710 try_merge_delay_insns (insn, next);
3711 else if (! INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3712 && own_thread_p (target_label, target_label, 0))
3713 try_merge_delay_insns (insn, next_active_insn (target_label));
3715 /* If we get here, we haven't deleted INSN. But we may have deleted
3716 NEXT, so recompute it. */
3717 next = next_active_insn (insn);
3722 /* Look for filled jumps to the end of function label. We can try to convert
3723 them into RETURN insns if the insns in the delay slot are valid for the
3724 RETURN as well. */
3726 static void
3727 make_return_insns (rtx first)
3729 rtx insn, jump_insn, pat;
3730 rtx real_return_label = function_return_label;
3731 rtx real_simple_return_label = function_simple_return_label;
3732 int slots, i;
3734 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3735 /* If a previous pass filled delay slots in the epilogue, things get a
3736 bit more complicated, as those filler insns would generally (without
3737 data flow analysis) have to be executed after any existing branch
3738 delay slot filler insns. It is also unknown whether such a
3739 transformation would actually be profitable. Note that the existing
3740 code only cares for branches with (some) filled delay slots. */
3741 if (crtl->epilogue_delay_list != NULL)
3742 return;
3743 #endif
3745 /* See if there is a RETURN insn in the function other than the one we
3746 made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3747 into a RETURN to jump to it. */
3748 for (insn = first; insn; insn = NEXT_INSN (insn))
3749 if (JUMP_P (insn) && ANY_RETURN_P (PATTERN (insn)))
3751 rtx t = get_label_before (insn);
3752 if (PATTERN (insn) == ret_rtx)
3753 real_return_label = t;
3754 else
3755 real_simple_return_label = t;
3756 break;
3759 /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3760 was equal to END_OF_FUNCTION_LABEL. */
3761 if (real_return_label)
3762 LABEL_NUSES (real_return_label)++;
3763 if (real_simple_return_label)
3764 LABEL_NUSES (real_simple_return_label)++;
3766 /* Clear the list of insns to fill so we can use it. */
3767 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3769 for (insn = first; insn; insn = NEXT_INSN (insn))
3771 int flags;
3772 rtx kind, real_label;
3774 /* Only look at filled JUMP_INSNs that go to the end of function
3775 label. */
3776 if (!NONJUMP_INSN_P (insn)
3777 || GET_CODE (PATTERN (insn)) != SEQUENCE
3778 || !jump_to_label_p (XVECEXP (PATTERN (insn), 0, 0)))
3779 continue;
3781 if (JUMP_LABEL (XVECEXP (PATTERN (insn), 0, 0)) == function_return_label)
3783 kind = ret_rtx;
3784 real_label = real_return_label;
3786 else if (JUMP_LABEL (XVECEXP (PATTERN (insn), 0, 0))
3787 == function_simple_return_label)
3789 kind = simple_return_rtx;
3790 real_label = real_simple_return_label;
3792 else
3793 continue;
3795 pat = PATTERN (insn);
3796 jump_insn = XVECEXP (pat, 0, 0);
3798 /* If we can't make the jump into a RETURN, try to redirect it to the best
3799 RETURN and go on to the next insn. */
3800 if (!reorg_redirect_jump (jump_insn, kind))
3802 /* Make sure redirecting the jump will not invalidate the delay
3803 slot insns. */
3804 if (redirect_with_delay_slots_safe_p (jump_insn, real_label, insn))
3805 reorg_redirect_jump (jump_insn, real_label);
3806 continue;
3809 /* See if this RETURN can accept the insns current in its delay slot.
3810 It can if it has more or an equal number of slots and the contents
3811 of each is valid. */
3813 flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3814 slots = num_delay_slots (jump_insn);
3815 if (slots >= XVECLEN (pat, 0) - 1)
3817 for (i = 1; i < XVECLEN (pat, 0); i++)
3818 if (! (
3819 #ifdef ANNUL_IFFALSE_SLOTS
3820 (INSN_ANNULLED_BRANCH_P (jump_insn)
3821 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3822 ? eligible_for_annul_false (jump_insn, i - 1,
3823 XVECEXP (pat, 0, i), flags) :
3824 #endif
3825 #ifdef ANNUL_IFTRUE_SLOTS
3826 (INSN_ANNULLED_BRANCH_P (jump_insn)
3827 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3828 ? eligible_for_annul_true (jump_insn, i - 1,
3829 XVECEXP (pat, 0, i), flags) :
3830 #endif
3831 eligible_for_delay (jump_insn, i - 1,
3832 XVECEXP (pat, 0, i), flags)))
3833 break;
3835 else
3836 i = 0;
3838 if (i == XVECLEN (pat, 0))
3839 continue;
3841 /* We have to do something with this insn. If it is an unconditional
3842 RETURN, delete the SEQUENCE and output the individual insns,
3843 followed by the RETURN. Then set things up so we try to find
3844 insns for its delay slots, if it needs some. */
3845 if (ANY_RETURN_P (PATTERN (jump_insn)))
3847 rtx prev = PREV_INSN (insn);
3849 delete_related_insns (insn);
3850 for (i = 1; i < XVECLEN (pat, 0); i++)
3851 prev = emit_insn_after (PATTERN (XVECEXP (pat, 0, i)), prev);
3853 insn = emit_jump_insn_after (PATTERN (jump_insn), prev);
3854 emit_barrier_after (insn);
3856 if (slots)
3857 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3859 else
3860 /* It is probably more efficient to keep this with its current
3861 delay slot as a branch to a RETURN. */
3862 reorg_redirect_jump (jump_insn, real_label);
3865 /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3866 new delay slots we have created. */
3867 if (real_return_label != NULL_RTX && --LABEL_NUSES (real_return_label) == 0)
3868 delete_related_insns (real_return_label);
3869 if (real_simple_return_label != NULL_RTX
3870 && --LABEL_NUSES (real_simple_return_label) == 0)
3871 delete_related_insns (real_simple_return_label);
3873 fill_simple_delay_slots (1);
3874 fill_simple_delay_slots (0);
3877 /* Try to find insns to place in delay slots. */
3879 void
3880 dbr_schedule (rtx first)
3882 rtx insn, next, epilogue_insn = 0;
3883 int i;
3884 bool need_return_insns;
3886 /* If the current function has no insns other than the prologue and
3887 epilogue, then do not try to fill any delay slots. */
3888 if (n_basic_blocks == NUM_FIXED_BLOCKS)
3889 return;
3891 /* Find the highest INSN_UID and allocate and initialize our map from
3892 INSN_UID's to position in code. */
3893 for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3895 if (INSN_UID (insn) > max_uid)
3896 max_uid = INSN_UID (insn);
3897 if (NOTE_P (insn)
3898 && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
3899 epilogue_insn = insn;
3902 uid_to_ruid = XNEWVEC (int, max_uid + 1);
3903 for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3904 uid_to_ruid[INSN_UID (insn)] = i;
3906 /* Initialize the list of insns that need filling. */
3907 if (unfilled_firstobj == 0)
3909 gcc_obstack_init (&unfilled_slots_obstack);
3910 unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3913 for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3915 rtx target;
3917 if (JUMP_P (insn))
3918 INSN_ANNULLED_BRANCH_P (insn) = 0;
3919 INSN_FROM_TARGET_P (insn) = 0;
3921 /* Skip vector tables. We can't get attributes for them. */
3922 if (JUMP_TABLE_DATA_P (insn))
3923 continue;
3925 if (num_delay_slots (insn) > 0)
3926 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3928 /* Ensure all jumps go to the last of a set of consecutive labels. */
3929 if (JUMP_P (insn)
3930 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3931 && !ANY_RETURN_P (JUMP_LABEL (insn))
3932 && ((target = skip_consecutive_labels (JUMP_LABEL (insn)))
3933 != JUMP_LABEL (insn)))
3934 redirect_jump (insn, target, 1);
3937 init_resource_info (epilogue_insn);
3939 /* Show we haven't computed an end-of-function label yet. */
3940 function_return_label = function_simple_return_label = NULL_RTX;
3942 /* Initialize the statistics for this function. */
3943 memset (num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3944 memset (num_filled_delays, 0, sizeof num_filled_delays);
3946 /* Now do the delay slot filling. Try everything twice in case earlier
3947 changes make more slots fillable. */
3949 for (reorg_pass_number = 0;
3950 reorg_pass_number < MAX_REORG_PASSES;
3951 reorg_pass_number++)
3953 fill_simple_delay_slots (1);
3954 fill_simple_delay_slots (0);
3955 fill_eager_delay_slots ();
3956 relax_delay_slots (first);
3959 /* If we made an end of function label, indicate that it is now
3960 safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3961 If it is now unused, delete it. */
3962 if (function_return_label && --LABEL_NUSES (function_return_label) == 0)
3963 delete_related_insns (function_return_label);
3964 if (function_simple_return_label
3965 && --LABEL_NUSES (function_simple_return_label) == 0)
3966 delete_related_insns (function_simple_return_label);
3968 need_return_insns = false;
3969 #ifdef HAVE_return
3970 need_return_insns |= HAVE_return && function_return_label != 0;
3971 #endif
3972 #ifdef HAVE_simple_return
3973 need_return_insns |= HAVE_simple_return && function_simple_return_label != 0;
3974 #endif
3975 if (need_return_insns)
3976 make_return_insns (first);
3978 /* Delete any USE insns made by update_block; subsequent passes don't need
3979 them or know how to deal with them. */
3980 for (insn = first; insn; insn = next)
3982 next = NEXT_INSN (insn);
3984 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
3985 && INSN_P (XEXP (PATTERN (insn), 0)))
3986 next = delete_related_insns (insn);
3989 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3991 /* It is not clear why the line below is needed, but it does seem to be. */
3992 unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3994 if (dump_file)
3996 int i, j, need_comma;
3997 int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3998 int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
4000 for (reorg_pass_number = 0;
4001 reorg_pass_number < MAX_REORG_PASSES;
4002 reorg_pass_number++)
4004 fprintf (dump_file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
4005 for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
4007 need_comma = 0;
4008 fprintf (dump_file, ";; Reorg function #%d\n", i);
4010 fprintf (dump_file, ";; %d insns needing delay slots\n;; ",
4011 num_insns_needing_delays[i][reorg_pass_number]);
4013 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
4014 if (num_filled_delays[i][j][reorg_pass_number])
4016 if (need_comma)
4017 fprintf (dump_file, ", ");
4018 need_comma = 1;
4019 fprintf (dump_file, "%d got %d delays",
4020 num_filled_delays[i][j][reorg_pass_number], j);
4022 fprintf (dump_file, "\n");
4025 memset (total_delay_slots, 0, sizeof total_delay_slots);
4026 memset (total_annul_slots, 0, sizeof total_annul_slots);
4027 for (insn = first; insn; insn = NEXT_INSN (insn))
4029 if (! INSN_DELETED_P (insn)
4030 && NONJUMP_INSN_P (insn)
4031 && GET_CODE (PATTERN (insn)) != USE
4032 && GET_CODE (PATTERN (insn)) != CLOBBER)
4034 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
4036 rtx control;
4037 j = XVECLEN (PATTERN (insn), 0) - 1;
4038 if (j > MAX_DELAY_HISTOGRAM)
4039 j = MAX_DELAY_HISTOGRAM;
4040 control = XVECEXP (PATTERN (insn), 0, 0);
4041 if (JUMP_P (control) && INSN_ANNULLED_BRANCH_P (control))
4042 total_annul_slots[j]++;
4043 else
4044 total_delay_slots[j]++;
4046 else if (num_delay_slots (insn) > 0)
4047 total_delay_slots[0]++;
4050 fprintf (dump_file, ";; Reorg totals: ");
4051 need_comma = 0;
4052 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
4054 if (total_delay_slots[j])
4056 if (need_comma)
4057 fprintf (dump_file, ", ");
4058 need_comma = 1;
4059 fprintf (dump_file, "%d got %d delays", total_delay_slots[j], j);
4062 fprintf (dump_file, "\n");
4063 #if defined (ANNUL_IFTRUE_SLOTS) || defined (ANNUL_IFFALSE_SLOTS)
4064 fprintf (dump_file, ";; Reorg annuls: ");
4065 need_comma = 0;
4066 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
4068 if (total_annul_slots[j])
4070 if (need_comma)
4071 fprintf (dump_file, ", ");
4072 need_comma = 1;
4073 fprintf (dump_file, "%d got %d delays", total_annul_slots[j], j);
4076 fprintf (dump_file, "\n");
4077 #endif
4078 fprintf (dump_file, "\n");
4081 free_resource_info ();
4082 free (uid_to_ruid);
4083 #ifdef DELAY_SLOTS_FOR_EPILOGUE
4084 /* SPARC assembler, for instance, emit warning when debug info is output
4085 into the delay slot. */
4087 rtx link;
4089 for (link = crtl->epilogue_delay_list;
4090 link;
4091 link = XEXP (link, 1))
4092 INSN_LOCATION (XEXP (link, 0)) = 0;
4095 #endif
4096 crtl->dbr_scheduled_p = true;
4098 #endif /* DELAY_SLOTS */
4100 static bool
4101 gate_handle_delay_slots (void)
4103 #ifdef DELAY_SLOTS
4104 /* At -O0 dataflow info isn't updated after RA. */
4105 return optimize > 0 && flag_delayed_branch && !crtl->dbr_scheduled_p;
4106 #else
4107 return 0;
4108 #endif
4111 /* Run delay slot optimization. */
4112 static unsigned int
4113 rest_of_handle_delay_slots (void)
4115 #ifdef DELAY_SLOTS
4116 dbr_schedule (get_insns ());
4117 #endif
4118 return 0;
4121 struct rtl_opt_pass pass_delay_slots =
4124 RTL_PASS,
4125 "dbr", /* name */
4126 OPTGROUP_NONE, /* optinfo_flags */
4127 gate_handle_delay_slots, /* gate */
4128 rest_of_handle_delay_slots, /* execute */
4129 NULL, /* sub */
4130 NULL, /* next */
4131 0, /* static_pass_number */
4132 TV_DBR_SCHED, /* tv_id */
4133 0, /* properties_required */
4134 0, /* properties_provided */
4135 0, /* properties_destroyed */
4136 0, /* todo_flags_start */
4137 TODO_ggc_collect /* todo_flags_finish */
4141 /* Machine dependent reorg pass. */
4142 static bool
4143 gate_handle_machine_reorg (void)
4145 return targetm.machine_dependent_reorg != 0;
4149 static unsigned int
4150 rest_of_handle_machine_reorg (void)
4152 targetm.machine_dependent_reorg ();
4153 return 0;
4156 struct rtl_opt_pass pass_machine_reorg =
4159 RTL_PASS,
4160 "mach", /* name */
4161 OPTGROUP_NONE, /* optinfo_flags */
4162 gate_handle_machine_reorg, /* gate */
4163 rest_of_handle_machine_reorg, /* execute */
4164 NULL, /* sub */
4165 NULL, /* next */
4166 0, /* static_pass_number */
4167 TV_MACH_DEP, /* tv_id */
4168 0, /* properties_required */
4169 0, /* properties_provided */
4170 0, /* properties_destroyed */
4171 0, /* todo_flags_start */
4172 TODO_ggc_collect /* todo_flags_finish */