* configure: Regenerated.
[official-gcc.git] / gcc / reorg.c
blobd71802676637904b019910933e74304dcde976d2
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
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"
138 #ifdef DELAY_SLOTS
140 #ifndef ANNUL_IFTRUE_SLOTS
141 #define eligible_for_annul_true(INSN, SLOTS, TRIAL, FLAGS) 0
142 #endif
143 #ifndef ANNUL_IFFALSE_SLOTS
144 #define eligible_for_annul_false(INSN, SLOTS, TRIAL, FLAGS) 0
145 #endif
147 /* Insns which have delay slots that have not yet been filled. */
149 static struct obstack unfilled_slots_obstack;
150 static rtx *unfilled_firstobj;
152 /* Define macros to refer to the first and last slot containing unfilled
153 insns. These are used because the list may move and its address
154 should be recomputed at each use. */
156 #define unfilled_slots_base \
157 ((rtx *) obstack_base (&unfilled_slots_obstack))
159 #define unfilled_slots_next \
160 ((rtx *) obstack_next_free (&unfilled_slots_obstack))
162 /* Points to the label before the end of the function, or before a
163 return insn. */
164 static rtx function_return_label;
165 /* Likewise for a simple_return. */
166 static rtx function_simple_return_label;
168 /* Mapping between INSN_UID's and position in the code since INSN_UID's do
169 not always monotonically increase. */
170 static int *uid_to_ruid;
172 /* Highest valid index in `uid_to_ruid'. */
173 static int max_uid;
175 static int stop_search_p (rtx, int);
176 static int resource_conflicts_p (struct resources *, struct resources *);
177 static int insn_references_resource_p (rtx, struct resources *, bool);
178 static int insn_sets_resource_p (rtx, struct resources *, bool);
179 static rtx find_end_label (rtx);
180 static rtx emit_delay_sequence (rtx, rtx, int);
181 static rtx add_to_delay_list (rtx, rtx);
182 static rtx delete_from_delay_slot (rtx);
183 static void delete_scheduled_jump (rtx);
184 static void note_delay_statistics (int, int);
185 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
186 static rtx optimize_skip (rtx);
187 #endif
188 static int get_jump_flags (rtx, rtx);
189 static int rare_destination (rtx);
190 static int mostly_true_jump (rtx, rtx);
191 static rtx get_branch_condition (rtx, rtx);
192 static int condition_dominates_p (rtx, rtx);
193 static int redirect_with_delay_slots_safe_p (rtx, rtx, rtx);
194 static int redirect_with_delay_list_safe_p (rtx, rtx, rtx);
195 static int check_annul_list_true_false (int, rtx);
196 static rtx steal_delay_list_from_target (rtx, rtx, rtx, rtx,
197 struct resources *,
198 struct resources *,
199 struct resources *,
200 int, int *, int *, rtx *);
201 static rtx steal_delay_list_from_fallthrough (rtx, rtx, rtx, rtx,
202 struct resources *,
203 struct resources *,
204 struct resources *,
205 int, int *, int *);
206 static void try_merge_delay_insns (rtx, rtx);
207 static rtx redundant_insn (rtx, rtx, rtx);
208 static int own_thread_p (rtx, rtx, int);
209 static void update_block (rtx, rtx);
210 static int reorg_redirect_jump (rtx, rtx);
211 static void update_reg_dead_notes (rtx, rtx);
212 static void fix_reg_dead_note (rtx, rtx);
213 static void update_reg_unused_notes (rtx, rtx);
214 static void fill_simple_delay_slots (int);
215 static rtx fill_slots_from_thread (rtx, rtx, rtx, rtx,
216 int, int, int, int,
217 int *, rtx);
218 static void fill_eager_delay_slots (void);
219 static void relax_delay_slots (rtx);
220 static void make_return_insns (rtx);
222 /* A wrapper around next_active_insn which takes care to return ret_rtx
223 unchanged. */
225 static rtx
226 first_active_target_insn (rtx insn)
228 if (ANY_RETURN_P (insn))
229 return insn;
230 return next_active_insn (insn);
233 /* Return true iff INSN is a simplejump, or any kind of return insn. */
235 static bool
236 simplejump_or_return_p (rtx insn)
238 return (JUMP_P (insn)
239 && (simplejump_p (insn) || ANY_RETURN_P (PATTERN (insn))));
242 /* Return TRUE if this insn should stop the search for insn to fill delay
243 slots. LABELS_P indicates that labels should terminate the search.
244 In all cases, jumps terminate the search. */
246 static int
247 stop_search_p (rtx insn, int labels_p)
249 if (insn == 0)
250 return 1;
252 /* If the insn can throw an exception that is caught within the function,
253 it may effectively perform a jump from the viewpoint of the function.
254 Therefore act like for a jump. */
255 if (can_throw_internal (insn))
256 return 1;
258 switch (GET_CODE (insn))
260 case NOTE:
261 case CALL_INSN:
262 return 0;
264 case CODE_LABEL:
265 return labels_p;
267 case JUMP_INSN:
268 case BARRIER:
269 return 1;
271 case INSN:
272 /* OK unless it contains a delay slot or is an `asm' insn of some type.
273 We don't know anything about these. */
274 return (GET_CODE (PATTERN (insn)) == SEQUENCE
275 || GET_CODE (PATTERN (insn)) == ASM_INPUT
276 || asm_noperands (PATTERN (insn)) >= 0);
278 default:
279 gcc_unreachable ();
283 /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
284 resource set contains a volatile memory reference. Otherwise, return FALSE. */
286 static int
287 resource_conflicts_p (struct resources *res1, struct resources *res2)
289 if ((res1->cc && res2->cc) || (res1->memory && res2->memory)
290 || (res1->unch_memory && res2->unch_memory)
291 || res1->volatil || res2->volatil)
292 return 1;
294 #ifdef HARD_REG_SET
295 return (res1->regs & res2->regs) != HARD_CONST (0);
296 #else
298 int i;
300 for (i = 0; i < HARD_REG_SET_LONGS; i++)
301 if ((res1->regs[i] & res2->regs[i]) != 0)
302 return 1;
303 return 0;
305 #endif
308 /* Return TRUE if any resource marked in RES, a `struct resources', is
309 referenced by INSN. If INCLUDE_DELAYED_EFFECTS is set, return if the called
310 routine is using those resources.
312 We compute this by computing all the resources referenced by INSN and
313 seeing if this conflicts with RES. It might be faster to directly check
314 ourselves, and this is the way it used to work, but it means duplicating
315 a large block of complex code. */
317 static int
318 insn_references_resource_p (rtx insn, struct resources *res,
319 bool include_delayed_effects)
321 struct resources insn_res;
323 CLEAR_RESOURCE (&insn_res);
324 mark_referenced_resources (insn, &insn_res, include_delayed_effects);
325 return resource_conflicts_p (&insn_res, res);
328 /* Return TRUE if INSN modifies resources that are marked in RES.
329 INCLUDE_DELAYED_EFFECTS is set if the actions of that routine should be
330 included. CC0 is only modified if it is explicitly set; see comments
331 in front of mark_set_resources for details. */
333 static int
334 insn_sets_resource_p (rtx insn, struct resources *res,
335 bool include_delayed_effects)
337 struct resources insn_sets;
339 CLEAR_RESOURCE (&insn_sets);
340 mark_set_resources (insn, &insn_sets, 0,
341 (include_delayed_effects
342 ? MARK_SRC_DEST_CALL
343 : MARK_SRC_DEST));
344 return resource_conflicts_p (&insn_sets, res);
347 /* Find a label at the end of the function or before a RETURN. If there
348 is none, try to make one. If that fails, returns 0.
350 The property of such a label is that it is placed just before the
351 epilogue or a bare RETURN insn, so that another bare RETURN can be
352 turned into a jump to the label unconditionally. In particular, the
353 label cannot be placed before a RETURN insn with a filled delay slot.
355 ??? There may be a problem with the current implementation. Suppose
356 we start with a bare RETURN insn and call find_end_label. It may set
357 function_return_label just before the RETURN. Suppose the machinery
358 is able to fill the delay slot of the RETURN insn afterwards. Then
359 function_return_label is no longer valid according to the property
360 described above and find_end_label will still return it unmodified.
361 Note that this is probably mitigated by the following observation:
362 once function_return_label is made, it is very likely the target of
363 a jump, so filling the delay slot of the RETURN will be much more
364 difficult.
365 KIND is either simple_return_rtx or ret_rtx, indicating which type of
366 return we're looking for. */
368 static rtx
369 find_end_label (rtx kind)
371 rtx insn;
372 rtx *plabel;
374 if (kind == ret_rtx)
375 plabel = &function_return_label;
376 else
378 gcc_assert (kind == simple_return_rtx);
379 plabel = &function_simple_return_label;
382 /* If we found one previously, return it. */
383 if (*plabel)
384 return *plabel;
386 /* Otherwise, see if there is a label at the end of the function. If there
387 is, it must be that RETURN insns aren't needed, so that is our return
388 label and we don't have to do anything else. */
390 insn = get_last_insn ();
391 while (NOTE_P (insn)
392 || (NONJUMP_INSN_P (insn)
393 && (GET_CODE (PATTERN (insn)) == USE
394 || GET_CODE (PATTERN (insn)) == CLOBBER)))
395 insn = PREV_INSN (insn);
397 /* When a target threads its epilogue we might already have a
398 suitable return insn. If so put a label before it for the
399 function_return_label. */
400 if (BARRIER_P (insn)
401 && JUMP_P (PREV_INSN (insn))
402 && PATTERN (PREV_INSN (insn)) == kind)
404 rtx temp = PREV_INSN (PREV_INSN (insn));
405 rtx label = gen_label_rtx ();
406 LABEL_NUSES (label) = 0;
408 /* Put the label before any USE insns that may precede the RETURN
409 insn. */
410 while (GET_CODE (temp) == USE)
411 temp = PREV_INSN (temp);
413 emit_label_after (label, temp);
414 *plabel = label;
417 else if (LABEL_P (insn))
418 *plabel = insn;
419 else
421 rtx label = gen_label_rtx ();
422 LABEL_NUSES (label) = 0;
423 /* If the basic block reorder pass moves the return insn to
424 some other place try to locate it again and put our
425 function_return_label there. */
426 while (insn && ! (JUMP_P (insn) && (PATTERN (insn) == kind)))
427 insn = PREV_INSN (insn);
428 if (insn)
430 insn = PREV_INSN (insn);
432 /* Put the label before any USE insns that may precede the
433 RETURN insn. */
434 while (GET_CODE (insn) == USE)
435 insn = PREV_INSN (insn);
437 emit_label_after (label, insn);
439 else
441 #ifdef HAVE_epilogue
442 if (HAVE_epilogue
443 #ifdef HAVE_return
444 && ! HAVE_return
445 #endif
447 /* The RETURN insn has its delay slot filled so we cannot
448 emit the label just before it. Since we already have
449 an epilogue and cannot emit a new RETURN, we cannot
450 emit the label at all. */
451 return NULL_RTX;
452 #endif /* HAVE_epilogue */
454 /* Otherwise, make a new label and emit a RETURN and BARRIER,
455 if needed. */
456 emit_label (label);
457 #ifdef HAVE_return
458 /* We don't bother trying to create a return insn if the
459 epilogue has filled delay-slots; we would have to try and
460 move the delay-slot fillers to the delay-slots for the new
461 return insn or in front of the new return insn. */
462 if (crtl->epilogue_delay_list == NULL
463 && HAVE_return)
465 /* The return we make may have delay slots too. */
466 rtx insn = gen_return ();
467 insn = emit_jump_insn (insn);
468 set_return_jump_label (insn);
469 emit_barrier ();
470 if (num_delay_slots (insn) > 0)
471 obstack_ptr_grow (&unfilled_slots_obstack, insn);
473 #endif
475 *plabel = label;
478 /* Show one additional use for this label so it won't go away until
479 we are done. */
480 ++LABEL_NUSES (*plabel);
482 return *plabel;
485 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
486 the pattern of INSN with the SEQUENCE.
488 Chain the insns so that NEXT_INSN of each insn in the sequence points to
489 the next and NEXT_INSN of the last insn in the sequence points to
490 the first insn after the sequence. Similarly for PREV_INSN. This makes
491 it easier to scan all insns.
493 Returns the SEQUENCE that replaces INSN. */
495 static rtx
496 emit_delay_sequence (rtx insn, rtx list, int length)
498 int i = 1;
499 rtx li;
500 int had_barrier = 0;
502 /* Allocate the rtvec to hold the insns and the SEQUENCE. */
503 rtvec seqv = rtvec_alloc (length + 1);
504 rtx seq = gen_rtx_SEQUENCE (VOIDmode, seqv);
505 rtx seq_insn = make_insn_raw (seq);
506 rtx first = get_insns ();
507 rtx last = get_last_insn ();
509 /* Make a copy of the insn having delay slots. */
510 rtx delay_insn = copy_rtx (insn);
512 /* If INSN is followed by a BARRIER, delete the BARRIER since it will only
513 confuse further processing. Update LAST in case it was the last insn.
514 We will put the BARRIER back in later. */
515 if (NEXT_INSN (insn) && BARRIER_P (NEXT_INSN (insn)))
517 delete_related_insns (NEXT_INSN (insn));
518 last = get_last_insn ();
519 had_barrier = 1;
522 /* Splice our SEQUENCE into the insn stream where INSN used to be. */
523 NEXT_INSN (seq_insn) = NEXT_INSN (insn);
524 PREV_INSN (seq_insn) = PREV_INSN (insn);
526 if (insn != last)
527 PREV_INSN (NEXT_INSN (seq_insn)) = seq_insn;
529 if (insn != first)
530 NEXT_INSN (PREV_INSN (seq_insn)) = seq_insn;
532 /* Note the calls to set_new_first_and_last_insn must occur after
533 SEQ_INSN has been completely spliced into the insn stream.
535 Otherwise CUR_INSN_UID will get set to an incorrect value because
536 set_new_first_and_last_insn will not find SEQ_INSN in the chain. */
537 if (insn == last)
538 set_new_first_and_last_insn (first, seq_insn);
540 if (insn == first)
541 set_new_first_and_last_insn (seq_insn, last);
543 /* Build our SEQUENCE and rebuild the insn chain. */
544 XVECEXP (seq, 0, 0) = delay_insn;
545 INSN_DELETED_P (delay_insn) = 0;
546 PREV_INSN (delay_insn) = PREV_INSN (seq_insn);
548 INSN_LOCATOR (seq_insn) = INSN_LOCATOR (delay_insn);
550 for (li = list; li; li = XEXP (li, 1), i++)
552 rtx tem = XEXP (li, 0);
553 rtx note, next;
555 /* Show that this copy of the insn isn't deleted. */
556 INSN_DELETED_P (tem) = 0;
558 XVECEXP (seq, 0, i) = tem;
559 PREV_INSN (tem) = XVECEXP (seq, 0, i - 1);
560 NEXT_INSN (XVECEXP (seq, 0, i - 1)) = tem;
562 /* SPARC assembler, for instance, emit warning when debug info is output
563 into the delay slot. */
564 if (INSN_LOCATOR (tem) && !INSN_LOCATOR (seq_insn))
565 INSN_LOCATOR (seq_insn) = INSN_LOCATOR (tem);
566 INSN_LOCATOR (tem) = 0;
568 for (note = REG_NOTES (tem); note; note = next)
570 next = XEXP (note, 1);
571 switch (REG_NOTE_KIND (note))
573 case REG_DEAD:
574 /* Remove any REG_DEAD notes because we can't rely on them now
575 that the insn has been moved. */
576 remove_note (tem, note);
577 break;
579 case REG_LABEL_OPERAND:
580 case REG_LABEL_TARGET:
581 /* Keep the label reference count up to date. */
582 if (LABEL_P (XEXP (note, 0)))
583 LABEL_NUSES (XEXP (note, 0)) ++;
584 break;
586 default:
587 break;
592 NEXT_INSN (XVECEXP (seq, 0, length)) = NEXT_INSN (seq_insn);
594 /* If the previous insn is a SEQUENCE, update the NEXT_INSN pointer on the
595 last insn in that SEQUENCE to point to us. Similarly for the first
596 insn in the following insn if it is a SEQUENCE. */
598 if (PREV_INSN (seq_insn) && NONJUMP_INSN_P (PREV_INSN (seq_insn))
599 && GET_CODE (PATTERN (PREV_INSN (seq_insn))) == SEQUENCE)
600 NEXT_INSN (XVECEXP (PATTERN (PREV_INSN (seq_insn)), 0,
601 XVECLEN (PATTERN (PREV_INSN (seq_insn)), 0) - 1))
602 = seq_insn;
604 if (NEXT_INSN (seq_insn) && NONJUMP_INSN_P (NEXT_INSN (seq_insn))
605 && GET_CODE (PATTERN (NEXT_INSN (seq_insn))) == SEQUENCE)
606 PREV_INSN (XVECEXP (PATTERN (NEXT_INSN (seq_insn)), 0, 0)) = seq_insn;
608 /* If there used to be a BARRIER, put it back. */
609 if (had_barrier)
610 emit_barrier_after (seq_insn);
612 gcc_assert (i == length + 1);
614 return seq_insn;
617 /* Add INSN to DELAY_LIST and return the head of the new list. The list must
618 be in the order in which the insns are to be executed. */
620 static rtx
621 add_to_delay_list (rtx insn, rtx delay_list)
623 /* If we have an empty list, just make a new list element. If
624 INSN has its block number recorded, clear it since we may
625 be moving the insn to a new block. */
627 if (delay_list == 0)
629 clear_hashed_info_for_insn (insn);
630 return gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
633 /* Otherwise this must be an INSN_LIST. Add INSN to the end of the
634 list. */
635 XEXP (delay_list, 1) = add_to_delay_list (insn, XEXP (delay_list, 1));
637 return delay_list;
640 /* Delete INSN from the delay slot of the insn that it is in, which may
641 produce an insn with no delay slots. Return the new insn. */
643 static rtx
644 delete_from_delay_slot (rtx insn)
646 rtx trial, seq_insn, seq, prev;
647 rtx delay_list = 0;
648 int i;
649 int had_barrier = 0;
651 /* We first must find the insn containing the SEQUENCE with INSN in its
652 delay slot. Do this by finding an insn, TRIAL, where
653 PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
655 for (trial = insn;
656 PREV_INSN (NEXT_INSN (trial)) == trial;
657 trial = NEXT_INSN (trial))
660 seq_insn = PREV_INSN (NEXT_INSN (trial));
661 seq = PATTERN (seq_insn);
663 if (NEXT_INSN (seq_insn) && BARRIER_P (NEXT_INSN (seq_insn)))
664 had_barrier = 1;
666 /* Create a delay list consisting of all the insns other than the one
667 we are deleting (unless we were the only one). */
668 if (XVECLEN (seq, 0) > 2)
669 for (i = 1; i < XVECLEN (seq, 0); i++)
670 if (XVECEXP (seq, 0, i) != insn)
671 delay_list = add_to_delay_list (XVECEXP (seq, 0, i), delay_list);
673 /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
674 list, and rebuild the delay list if non-empty. */
675 prev = PREV_INSN (seq_insn);
676 trial = XVECEXP (seq, 0, 0);
677 delete_related_insns (seq_insn);
678 add_insn_after (trial, prev, NULL);
680 /* If there was a barrier after the old SEQUENCE, remit it. */
681 if (had_barrier)
682 emit_barrier_after (trial);
684 /* If there are any delay insns, remit them. Otherwise clear the
685 annul flag. */
686 if (delay_list)
687 trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2);
688 else if (JUMP_P (trial))
689 INSN_ANNULLED_BRANCH_P (trial) = 0;
691 INSN_FROM_TARGET_P (insn) = 0;
693 /* Show we need to fill this insn again. */
694 obstack_ptr_grow (&unfilled_slots_obstack, trial);
696 return trial;
699 /* Delete INSN, a JUMP_INSN. If it is a conditional jump, we must track down
700 the insn that sets CC0 for it and delete it too. */
702 static void
703 delete_scheduled_jump (rtx insn)
705 /* Delete the insn that sets cc0 for us. On machines without cc0, we could
706 delete the insn that sets the condition code, but it is hard to find it.
707 Since this case is rare anyway, don't bother trying; there would likely
708 be other insns that became dead anyway, which we wouldn't know to
709 delete. */
711 #ifdef HAVE_cc0
712 if (reg_mentioned_p (cc0_rtx, insn))
714 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
716 /* If a reg-note was found, it points to an insn to set CC0. This
717 insn is in the delay list of some other insn. So delete it from
718 the delay list it was in. */
719 if (note)
721 if (! FIND_REG_INC_NOTE (XEXP (note, 0), NULL_RTX)
722 && sets_cc0_p (PATTERN (XEXP (note, 0))) == 1)
723 delete_from_delay_slot (XEXP (note, 0));
725 else
727 /* The insn setting CC0 is our previous insn, but it may be in
728 a delay slot. It will be the last insn in the delay slot, if
729 it is. */
730 rtx trial = previous_insn (insn);
731 if (NOTE_P (trial))
732 trial = prev_nonnote_insn (trial);
733 if (sets_cc0_p (PATTERN (trial)) != 1
734 || FIND_REG_INC_NOTE (trial, NULL_RTX))
735 return;
736 if (PREV_INSN (NEXT_INSN (trial)) == trial)
737 delete_related_insns (trial);
738 else
739 delete_from_delay_slot (trial);
742 #endif
744 delete_related_insns (insn);
747 /* Counters for delay-slot filling. */
749 #define NUM_REORG_FUNCTIONS 2
750 #define MAX_DELAY_HISTOGRAM 3
751 #define MAX_REORG_PASSES 2
753 static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
755 static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
757 static int reorg_pass_number;
759 static void
760 note_delay_statistics (int slots_filled, int index)
762 num_insns_needing_delays[index][reorg_pass_number]++;
763 if (slots_filled > MAX_DELAY_HISTOGRAM)
764 slots_filled = MAX_DELAY_HISTOGRAM;
765 num_filled_delays[index][slots_filled][reorg_pass_number]++;
768 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
770 /* Optimize the following cases:
772 1. When a conditional branch skips over only one instruction,
773 use an annulling branch and put that insn in the delay slot.
774 Use either a branch that annuls when the condition if true or
775 invert the test with a branch that annuls when the condition is
776 false. This saves insns, since otherwise we must copy an insn
777 from the L1 target.
779 (orig) (skip) (otherwise)
780 Bcc.n L1 Bcc',a L1 Bcc,a L1'
781 insn insn insn2
782 L1: L1: L1:
783 insn2 insn2 insn2
784 insn3 insn3 L1':
785 insn3
787 2. When a conditional branch skips over only one instruction,
788 and after that, it unconditionally branches somewhere else,
789 perform the similar optimization. This saves executing the
790 second branch in the case where the inverted condition is true.
792 Bcc.n L1 Bcc',a L2
793 insn insn
794 L1: L1:
795 Bra L2 Bra L2
797 INSN is a JUMP_INSN.
799 This should be expanded to skip over N insns, where N is the number
800 of delay slots required. */
802 static rtx
803 optimize_skip (rtx insn)
805 rtx trial = next_nonnote_insn (insn);
806 rtx next_trial = next_active_insn (trial);
807 rtx delay_list = 0;
808 int flags;
810 flags = get_jump_flags (insn, JUMP_LABEL (insn));
812 if (trial == 0
813 || !NONJUMP_INSN_P (trial)
814 || GET_CODE (PATTERN (trial)) == SEQUENCE
815 || recog_memoized (trial) < 0
816 || (! eligible_for_annul_false (insn, 0, trial, flags)
817 && ! eligible_for_annul_true (insn, 0, trial, flags))
818 || can_throw_internal (trial))
819 return 0;
821 /* There are two cases where we are just executing one insn (we assume
822 here that a branch requires only one insn; this should be generalized
823 at some point): Where the branch goes around a single insn or where
824 we have one insn followed by a branch to the same label we branch to.
825 In both of these cases, inverting the jump and annulling the delay
826 slot give the same effect in fewer insns. */
827 if ((next_trial == next_active_insn (JUMP_LABEL (insn))
828 && ! (next_trial == 0 && crtl->epilogue_delay_list != 0))
829 || (next_trial != 0
830 && simplejump_or_return_p (next_trial)
831 && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)))
833 if (eligible_for_annul_false (insn, 0, trial, flags))
835 if (invert_jump (insn, JUMP_LABEL (insn), 1))
836 INSN_FROM_TARGET_P (trial) = 1;
837 else if (! eligible_for_annul_true (insn, 0, trial, flags))
838 return 0;
841 delay_list = add_to_delay_list (trial, NULL_RTX);
842 next_trial = next_active_insn (trial);
843 update_block (trial, trial);
844 delete_related_insns (trial);
846 /* Also, if we are targeting an unconditional
847 branch, thread our jump to the target of that branch. Don't
848 change this into a RETURN here, because it may not accept what
849 we have in the delay slot. We'll fix this up later. */
850 if (next_trial && simplejump_or_return_p (next_trial))
852 rtx target_label = JUMP_LABEL (next_trial);
853 if (ANY_RETURN_P (target_label))
854 target_label = find_end_label (target_label);
856 if (target_label)
858 /* Recompute the flags based on TARGET_LABEL since threading
859 the jump to TARGET_LABEL may change the direction of the
860 jump (which may change the circumstances in which the
861 delay slot is nullified). */
862 flags = get_jump_flags (insn, target_label);
863 if (eligible_for_annul_true (insn, 0, trial, flags))
864 reorg_redirect_jump (insn, target_label);
868 INSN_ANNULLED_BRANCH_P (insn) = 1;
871 return delay_list;
873 #endif
875 /* Encode and return branch direction and prediction information for
876 INSN assuming it will jump to LABEL.
878 Non conditional branches return no direction information and
879 are predicted as very likely taken. */
881 static int
882 get_jump_flags (rtx insn, rtx label)
884 int flags;
886 /* get_jump_flags can be passed any insn with delay slots, these may
887 be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
888 direction information, and only if they are conditional jumps.
890 If LABEL is a return, then there is no way to determine the branch
891 direction. */
892 if (JUMP_P (insn)
893 && (condjump_p (insn) || condjump_in_parallel_p (insn))
894 && !ANY_RETURN_P (label)
895 && INSN_UID (insn) <= max_uid
896 && INSN_UID (label) <= max_uid)
897 flags
898 = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
899 ? ATTR_FLAG_forward : ATTR_FLAG_backward;
900 /* No valid direction information. */
901 else
902 flags = 0;
904 return flags;
907 /* Return 1 if INSN is a destination that will be branched to rarely (the
908 return point of a function); return 2 if DEST will be branched to very
909 rarely (a call to a function that doesn't return). Otherwise,
910 return 0. */
912 static int
913 rare_destination (rtx insn)
915 int jump_count = 0;
916 rtx next;
918 for (; insn && !ANY_RETURN_P (insn); insn = next)
920 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
921 insn = XVECEXP (PATTERN (insn), 0, 0);
923 next = NEXT_INSN (insn);
925 switch (GET_CODE (insn))
927 case CODE_LABEL:
928 return 0;
929 case BARRIER:
930 /* A BARRIER can either be after a JUMP_INSN or a CALL_INSN. We
931 don't scan past JUMP_INSNs, so any barrier we find here must
932 have been after a CALL_INSN and hence mean the call doesn't
933 return. */
934 return 2;
935 case JUMP_INSN:
936 if (ANY_RETURN_P (PATTERN (insn)))
937 return 1;
938 else if (simplejump_p (insn)
939 && jump_count++ < 10)
940 next = JUMP_LABEL (insn);
941 else
942 return 0;
944 default:
945 break;
949 /* If we got here it means we hit the end of the function. So this
950 is an unlikely destination. */
952 return 1;
955 /* Return truth value of the statement that this branch
956 is mostly taken. If we think that the branch is extremely likely
957 to be taken, we return 2. If the branch is slightly more likely to be
958 taken, return 1. If the branch is slightly less likely to be taken,
959 return 0 and if the branch is highly unlikely to be taken, return -1.
961 CONDITION, if nonzero, is the condition that JUMP_INSN is testing. */
963 static int
964 mostly_true_jump (rtx jump_insn, rtx condition)
966 rtx target_label = JUMP_LABEL (jump_insn);
967 rtx note;
968 int rare_dest, rare_fallthrough;
970 /* If branch probabilities are available, then use that number since it
971 always gives a correct answer. */
972 note = find_reg_note (jump_insn, REG_BR_PROB, 0);
973 if (note)
975 int prob = INTVAL (XEXP (note, 0));
977 if (prob >= REG_BR_PROB_BASE * 9 / 10)
978 return 2;
979 else if (prob >= REG_BR_PROB_BASE / 2)
980 return 1;
981 else if (prob >= REG_BR_PROB_BASE / 10)
982 return 0;
983 else
984 return -1;
987 /* Look at the relative rarities of the fallthrough and destination. If
988 they differ, we can predict the branch that way. */
989 rare_dest = rare_destination (target_label);
990 rare_fallthrough = rare_destination (NEXT_INSN (jump_insn));
992 switch (rare_fallthrough - rare_dest)
994 case -2:
995 return -1;
996 case -1:
997 return 0;
998 case 0:
999 break;
1000 case 1:
1001 return 1;
1002 case 2:
1003 return 2;
1006 /* If we couldn't figure out what this jump was, assume it won't be
1007 taken. This should be rare. */
1008 if (condition == 0)
1009 return 0;
1011 /* Predict backward branches usually take, forward branches usually not. If
1012 we don't know whether this is forward or backward, assume the branch
1013 will be taken, since most are. */
1014 return (ANY_RETURN_P (target_label) || INSN_UID (jump_insn) > max_uid
1015 || INSN_UID (target_label) > max_uid
1016 || (uid_to_ruid[INSN_UID (jump_insn)]
1017 > uid_to_ruid[INSN_UID (target_label)]));
1020 /* Return the condition under which INSN will branch to TARGET. If TARGET
1021 is zero, return the condition under which INSN will return. If INSN is
1022 an unconditional branch, return const_true_rtx. If INSN isn't a simple
1023 type of jump, or it doesn't go to TARGET, return 0. */
1025 static rtx
1026 get_branch_condition (rtx insn, rtx target)
1028 rtx pat = PATTERN (insn);
1029 rtx src;
1031 if (condjump_in_parallel_p (insn))
1032 pat = XVECEXP (pat, 0, 0);
1034 if (ANY_RETURN_P (pat))
1035 return pat == target ? const_true_rtx : 0;
1037 if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
1038 return 0;
1040 src = SET_SRC (pat);
1041 if (GET_CODE (src) == LABEL_REF && XEXP (src, 0) == target)
1042 return const_true_rtx;
1044 else if (GET_CODE (src) == IF_THEN_ELSE
1045 && XEXP (src, 2) == pc_rtx
1046 && GET_CODE (XEXP (src, 1)) == LABEL_REF
1047 && XEXP (XEXP (src, 1), 0) == target)
1048 return XEXP (src, 0);
1050 else if (GET_CODE (src) == IF_THEN_ELSE
1051 && XEXP (src, 1) == pc_rtx
1052 && GET_CODE (XEXP (src, 2)) == LABEL_REF
1053 && XEXP (XEXP (src, 2), 0) == target)
1055 enum rtx_code rev;
1056 rev = reversed_comparison_code (XEXP (src, 0), insn);
1057 if (rev != UNKNOWN)
1058 return gen_rtx_fmt_ee (rev, GET_MODE (XEXP (src, 0)),
1059 XEXP (XEXP (src, 0), 0),
1060 XEXP (XEXP (src, 0), 1));
1063 return 0;
1066 /* Return nonzero if CONDITION is more strict than the condition of
1067 INSN, i.e., if INSN will always branch if CONDITION is true. */
1069 static int
1070 condition_dominates_p (rtx condition, rtx insn)
1072 rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
1073 enum rtx_code code = GET_CODE (condition);
1074 enum rtx_code other_code;
1076 if (rtx_equal_p (condition, other_condition)
1077 || other_condition == const_true_rtx)
1078 return 1;
1080 else if (condition == const_true_rtx || other_condition == 0)
1081 return 0;
1083 other_code = GET_CODE (other_condition);
1084 if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
1085 || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
1086 || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
1087 return 0;
1089 return comparison_dominates_p (code, other_code);
1092 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1093 any insns already in the delay slot of JUMP. */
1095 static int
1096 redirect_with_delay_slots_safe_p (rtx jump, rtx newlabel, rtx seq)
1098 int flags, i;
1099 rtx pat = PATTERN (seq);
1101 /* Make sure all the delay slots of this jump would still
1102 be valid after threading the jump. If they are still
1103 valid, then return nonzero. */
1105 flags = get_jump_flags (jump, newlabel);
1106 for (i = 1; i < XVECLEN (pat, 0); i++)
1107 if (! (
1108 #ifdef ANNUL_IFFALSE_SLOTS
1109 (INSN_ANNULLED_BRANCH_P (jump)
1110 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1111 ? eligible_for_annul_false (jump, i - 1,
1112 XVECEXP (pat, 0, i), flags) :
1113 #endif
1114 #ifdef ANNUL_IFTRUE_SLOTS
1115 (INSN_ANNULLED_BRANCH_P (jump)
1116 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1117 ? eligible_for_annul_true (jump, i - 1,
1118 XVECEXP (pat, 0, i), flags) :
1119 #endif
1120 eligible_for_delay (jump, i - 1, XVECEXP (pat, 0, i), flags)))
1121 break;
1123 return (i == XVECLEN (pat, 0));
1126 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1127 any insns we wish to place in the delay slot of JUMP. */
1129 static int
1130 redirect_with_delay_list_safe_p (rtx jump, rtx newlabel, rtx delay_list)
1132 int flags, i;
1133 rtx li;
1135 /* Make sure all the insns in DELAY_LIST would still be
1136 valid after threading the jump. If they are still
1137 valid, then return nonzero. */
1139 flags = get_jump_flags (jump, newlabel);
1140 for (li = delay_list, i = 0; li; li = XEXP (li, 1), i++)
1141 if (! (
1142 #ifdef ANNUL_IFFALSE_SLOTS
1143 (INSN_ANNULLED_BRANCH_P (jump)
1144 && INSN_FROM_TARGET_P (XEXP (li, 0)))
1145 ? eligible_for_annul_false (jump, i, XEXP (li, 0), flags) :
1146 #endif
1147 #ifdef ANNUL_IFTRUE_SLOTS
1148 (INSN_ANNULLED_BRANCH_P (jump)
1149 && ! INSN_FROM_TARGET_P (XEXP (li, 0)))
1150 ? eligible_for_annul_true (jump, i, XEXP (li, 0), flags) :
1151 #endif
1152 eligible_for_delay (jump, i, XEXP (li, 0), flags)))
1153 break;
1155 return (li == NULL);
1158 /* DELAY_LIST is a list of insns that have already been placed into delay
1159 slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
1160 If not, return 0; otherwise return 1. */
1162 static int
1163 check_annul_list_true_false (int annul_true_p, rtx delay_list)
1165 rtx temp;
1167 if (delay_list)
1169 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1171 rtx trial = XEXP (temp, 0);
1173 if ((annul_true_p && INSN_FROM_TARGET_P (trial))
1174 || (!annul_true_p && !INSN_FROM_TARGET_P (trial)))
1175 return 0;
1179 return 1;
1182 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
1183 the condition tested by INSN is CONDITION and the resources shown in
1184 OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1185 from SEQ's delay list, in addition to whatever insns it may execute
1186 (in DELAY_LIST). SETS and NEEDED are denote resources already set and
1187 needed while searching for delay slot insns. Return the concatenated
1188 delay list if possible, otherwise, return 0.
1190 SLOTS_TO_FILL is the total number of slots required by INSN, and
1191 PSLOTS_FILLED points to the number filled so far (also the number of
1192 insns in DELAY_LIST). It is updated with the number that have been
1193 filled from the SEQUENCE, if any.
1195 PANNUL_P points to a nonzero value if we already know that we need
1196 to annul INSN. If this routine determines that annulling is needed,
1197 it may set that value nonzero.
1199 PNEW_THREAD points to a location that is to receive the place at which
1200 execution should continue. */
1202 static rtx
1203 steal_delay_list_from_target (rtx insn, rtx condition, rtx seq,
1204 rtx delay_list, struct resources *sets,
1205 struct resources *needed,
1206 struct resources *other_needed,
1207 int slots_to_fill, int *pslots_filled,
1208 int *pannul_p, rtx *pnew_thread)
1210 rtx temp;
1211 int slots_remaining = slots_to_fill - *pslots_filled;
1212 int total_slots_filled = *pslots_filled;
1213 rtx new_delay_list = 0;
1214 int must_annul = *pannul_p;
1215 int used_annul = 0;
1216 int i;
1217 struct resources cc_set;
1219 /* We can't do anything if there are more delay slots in SEQ than we
1220 can handle, or if we don't know that it will be a taken branch.
1221 We know that it will be a taken branch if it is either an unconditional
1222 branch or a conditional branch with a stricter branch condition.
1224 Also, exit if the branch has more than one set, since then it is computing
1225 other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1226 ??? It may be possible to move other sets into INSN in addition to
1227 moving the instructions in the delay slots.
1229 We can not steal the delay list if one of the instructions in the
1230 current delay_list modifies the condition codes and the jump in the
1231 sequence is a conditional jump. We can not do this because we can
1232 not change the direction of the jump because the condition codes
1233 will effect the direction of the jump in the sequence. */
1235 CLEAR_RESOURCE (&cc_set);
1236 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1238 rtx trial = XEXP (temp, 0);
1240 mark_set_resources (trial, &cc_set, 0, MARK_SRC_DEST_CALL);
1241 if (insn_references_resource_p (XVECEXP (seq , 0, 0), &cc_set, false))
1242 return delay_list;
1245 if (XVECLEN (seq, 0) - 1 > slots_remaining
1246 || ! condition_dominates_p (condition, XVECEXP (seq, 0, 0))
1247 || ! single_set (XVECEXP (seq, 0, 0)))
1248 return delay_list;
1250 #ifdef MD_CAN_REDIRECT_BRANCH
1251 /* On some targets, branches with delay slots can have a limited
1252 displacement. Give the back end a chance to tell us we can't do
1253 this. */
1254 if (! MD_CAN_REDIRECT_BRANCH (insn, XVECEXP (seq, 0, 0)))
1255 return delay_list;
1256 #endif
1258 for (i = 1; i < XVECLEN (seq, 0); i++)
1260 rtx trial = XVECEXP (seq, 0, i);
1261 int flags;
1263 if (insn_references_resource_p (trial, sets, false)
1264 || insn_sets_resource_p (trial, needed, false)
1265 || insn_sets_resource_p (trial, sets, false)
1266 #ifdef HAVE_cc0
1267 /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1268 delay list. */
1269 || find_reg_note (trial, REG_CC_USER, NULL_RTX)
1270 #endif
1271 /* If TRIAL is from the fallthrough code of an annulled branch insn
1272 in SEQ, we cannot use it. */
1273 || (INSN_ANNULLED_BRANCH_P (XVECEXP (seq, 0, 0))
1274 && ! INSN_FROM_TARGET_P (trial)))
1275 return delay_list;
1277 /* If this insn was already done (usually in a previous delay slot),
1278 pretend we put it in our delay slot. */
1279 if (redundant_insn (trial, insn, new_delay_list))
1280 continue;
1282 /* We will end up re-vectoring this branch, so compute flags
1283 based on jumping to the new label. */
1284 flags = get_jump_flags (insn, JUMP_LABEL (XVECEXP (seq, 0, 0)));
1286 if (! must_annul
1287 && ((condition == const_true_rtx
1288 || (! insn_sets_resource_p (trial, other_needed, false)
1289 && ! may_trap_or_fault_p (PATTERN (trial)))))
1290 ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1291 : (must_annul || (delay_list == NULL && new_delay_list == NULL))
1292 && (must_annul = 1,
1293 check_annul_list_true_false (0, delay_list)
1294 && check_annul_list_true_false (0, new_delay_list)
1295 && eligible_for_annul_false (insn, total_slots_filled,
1296 trial, flags)))
1298 if (must_annul)
1299 used_annul = 1;
1300 temp = copy_rtx (trial);
1301 INSN_FROM_TARGET_P (temp) = 1;
1302 new_delay_list = add_to_delay_list (temp, new_delay_list);
1303 total_slots_filled++;
1305 if (--slots_remaining == 0)
1306 break;
1308 else
1309 return delay_list;
1312 /* Show the place to which we will be branching. */
1313 *pnew_thread = first_active_target_insn (JUMP_LABEL (XVECEXP (seq, 0, 0)));
1315 /* Add any new insns to the delay list and update the count of the
1316 number of slots filled. */
1317 *pslots_filled = total_slots_filled;
1318 if (used_annul)
1319 *pannul_p = 1;
1321 if (delay_list == 0)
1322 return new_delay_list;
1324 for (temp = new_delay_list; temp; temp = XEXP (temp, 1))
1325 delay_list = add_to_delay_list (XEXP (temp, 0), delay_list);
1327 return delay_list;
1330 /* Similar to steal_delay_list_from_target except that SEQ is on the
1331 fallthrough path of INSN. Here we only do something if the delay insn
1332 of SEQ is an unconditional branch. In that case we steal its delay slot
1333 for INSN since unconditional branches are much easier to fill. */
1335 static rtx
1336 steal_delay_list_from_fallthrough (rtx insn, rtx condition, rtx seq,
1337 rtx delay_list, struct resources *sets,
1338 struct resources *needed,
1339 struct resources *other_needed,
1340 int slots_to_fill, int *pslots_filled,
1341 int *pannul_p)
1343 int i;
1344 int flags;
1345 int must_annul = *pannul_p;
1346 int used_annul = 0;
1348 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1350 /* We can't do anything if SEQ's delay insn isn't an
1351 unconditional branch. */
1353 if (! simplejump_or_return_p (XVECEXP (seq, 0, 0)))
1354 return delay_list;
1356 for (i = 1; i < XVECLEN (seq, 0); i++)
1358 rtx trial = XVECEXP (seq, 0, i);
1360 /* If TRIAL sets CC0, stealing it will move it too far from the use
1361 of CC0. */
1362 if (insn_references_resource_p (trial, sets, false)
1363 || insn_sets_resource_p (trial, needed, false)
1364 || insn_sets_resource_p (trial, sets, false)
1365 #ifdef HAVE_cc0
1366 || sets_cc0_p (PATTERN (trial))
1367 #endif
1370 break;
1372 /* If this insn was already done, we don't need it. */
1373 if (redundant_insn (trial, insn, delay_list))
1375 delete_from_delay_slot (trial);
1376 continue;
1379 if (! must_annul
1380 && ((condition == const_true_rtx
1381 || (! insn_sets_resource_p (trial, other_needed, false)
1382 && ! may_trap_or_fault_p (PATTERN (trial)))))
1383 ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1384 : (must_annul || delay_list == NULL) && (must_annul = 1,
1385 check_annul_list_true_false (1, delay_list)
1386 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1388 if (must_annul)
1389 used_annul = 1;
1390 delete_from_delay_slot (trial);
1391 delay_list = add_to_delay_list (trial, delay_list);
1393 if (++(*pslots_filled) == slots_to_fill)
1394 break;
1396 else
1397 break;
1400 if (used_annul)
1401 *pannul_p = 1;
1402 return delay_list;
1405 /* Try merging insns starting at THREAD which match exactly the insns in
1406 INSN's delay list.
1408 If all insns were matched and the insn was previously annulling, the
1409 annul bit will be cleared.
1411 For each insn that is merged, if the branch is or will be non-annulling,
1412 we delete the merged insn. */
1414 static void
1415 try_merge_delay_insns (rtx insn, rtx thread)
1417 rtx trial, next_trial;
1418 rtx delay_insn = XVECEXP (PATTERN (insn), 0, 0);
1419 int annul_p = JUMP_P (delay_insn) && INSN_ANNULLED_BRANCH_P (delay_insn);
1420 int slot_number = 1;
1421 int num_slots = XVECLEN (PATTERN (insn), 0);
1422 rtx next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1423 struct resources set, needed;
1424 rtx merged_insns = 0;
1425 int i;
1426 int flags;
1428 flags = get_jump_flags (delay_insn, JUMP_LABEL (delay_insn));
1430 CLEAR_RESOURCE (&needed);
1431 CLEAR_RESOURCE (&set);
1433 /* If this is not an annulling branch, take into account anything needed in
1434 INSN's delay slot. This prevents two increments from being incorrectly
1435 folded into one. If we are annulling, this would be the correct
1436 thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1437 will essentially disable this optimization. This method is somewhat of
1438 a kludge, but I don't see a better way.) */
1439 if (! annul_p)
1440 for (i = 1 ; i < num_slots; i++)
1441 if (XVECEXP (PATTERN (insn), 0, i))
1442 mark_referenced_resources (XVECEXP (PATTERN (insn), 0, i), &needed,
1443 true);
1445 for (trial = thread; !stop_search_p (trial, 1); trial = next_trial)
1447 rtx pat = PATTERN (trial);
1448 rtx oldtrial = trial;
1450 next_trial = next_nonnote_insn (trial);
1452 /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1453 if (NONJUMP_INSN_P (trial)
1454 && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1455 continue;
1457 if (GET_CODE (next_to_match) == GET_CODE (trial)
1458 #ifdef HAVE_cc0
1459 /* We can't share an insn that sets cc0. */
1460 && ! sets_cc0_p (pat)
1461 #endif
1462 && ! insn_references_resource_p (trial, &set, true)
1463 && ! insn_sets_resource_p (trial, &set, true)
1464 && ! insn_sets_resource_p (trial, &needed, true)
1465 && (trial = try_split (pat, trial, 0)) != 0
1466 /* Update next_trial, in case try_split succeeded. */
1467 && (next_trial = next_nonnote_insn (trial))
1468 /* Likewise THREAD. */
1469 && (thread = oldtrial == thread ? trial : thread)
1470 && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1471 /* Have to test this condition if annul condition is different
1472 from (and less restrictive than) non-annulling one. */
1473 && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1476 if (! annul_p)
1478 update_block (trial, thread);
1479 if (trial == thread)
1480 thread = next_active_insn (thread);
1482 delete_related_insns (trial);
1483 INSN_FROM_TARGET_P (next_to_match) = 0;
1485 else
1486 merged_insns = gen_rtx_INSN_LIST (VOIDmode, trial, merged_insns);
1488 if (++slot_number == num_slots)
1489 break;
1491 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1494 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1495 mark_referenced_resources (trial, &needed, true);
1498 /* See if we stopped on a filled insn. If we did, try to see if its
1499 delay slots match. */
1500 if (slot_number != num_slots
1501 && trial && NONJUMP_INSN_P (trial)
1502 && GET_CODE (PATTERN (trial)) == SEQUENCE
1503 && !(JUMP_P (XVECEXP (PATTERN (trial), 0, 0))
1504 && INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial), 0, 0))))
1506 rtx pat = PATTERN (trial);
1507 rtx filled_insn = XVECEXP (pat, 0, 0);
1509 /* Account for resources set/needed by the filled insn. */
1510 mark_set_resources (filled_insn, &set, 0, MARK_SRC_DEST_CALL);
1511 mark_referenced_resources (filled_insn, &needed, true);
1513 for (i = 1; i < XVECLEN (pat, 0); i++)
1515 rtx dtrial = XVECEXP (pat, 0, i);
1517 if (! insn_references_resource_p (dtrial, &set, true)
1518 && ! insn_sets_resource_p (dtrial, &set, true)
1519 && ! insn_sets_resource_p (dtrial, &needed, true)
1520 #ifdef HAVE_cc0
1521 && ! sets_cc0_p (PATTERN (dtrial))
1522 #endif
1523 && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1524 && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1526 if (! annul_p)
1528 rtx new_rtx;
1530 update_block (dtrial, thread);
1531 new_rtx = delete_from_delay_slot (dtrial);
1532 if (INSN_DELETED_P (thread))
1533 thread = new_rtx;
1534 INSN_FROM_TARGET_P (next_to_match) = 0;
1536 else
1537 merged_insns = gen_rtx_INSN_LIST (SImode, dtrial,
1538 merged_insns);
1540 if (++slot_number == num_slots)
1541 break;
1543 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1545 else
1547 /* Keep track of the set/referenced resources for the delay
1548 slots of any trial insns we encounter. */
1549 mark_set_resources (dtrial, &set, 0, MARK_SRC_DEST_CALL);
1550 mark_referenced_resources (dtrial, &needed, true);
1555 /* If all insns in the delay slot have been matched and we were previously
1556 annulling the branch, we need not any more. In that case delete all the
1557 merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1558 the delay list so that we know that it isn't only being used at the
1559 target. */
1560 if (slot_number == num_slots && annul_p)
1562 for (; merged_insns; merged_insns = XEXP (merged_insns, 1))
1564 if (GET_MODE (merged_insns) == SImode)
1566 rtx new_rtx;
1568 update_block (XEXP (merged_insns, 0), thread);
1569 new_rtx = delete_from_delay_slot (XEXP (merged_insns, 0));
1570 if (INSN_DELETED_P (thread))
1571 thread = new_rtx;
1573 else
1575 update_block (XEXP (merged_insns, 0), thread);
1576 delete_related_insns (XEXP (merged_insns, 0));
1580 INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1582 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1583 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i)) = 0;
1587 /* See if INSN is redundant with an insn in front of TARGET. Often this
1588 is called when INSN is a candidate for a delay slot of TARGET.
1589 DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1590 of INSN. Often INSN will be redundant with an insn in a delay slot of
1591 some previous insn. This happens when we have a series of branches to the
1592 same label; in that case the first insn at the target might want to go
1593 into each of the delay slots.
1595 If we are not careful, this routine can take up a significant fraction
1596 of the total compilation time (4%), but only wins rarely. Hence we
1597 speed this routine up by making two passes. The first pass goes back
1598 until it hits a label and sees if it finds an insn with an identical
1599 pattern. Only in this (relatively rare) event does it check for
1600 data conflicts.
1602 We do not split insns we encounter. This could cause us not to find a
1603 redundant insn, but the cost of splitting seems greater than the possible
1604 gain in rare cases. */
1606 static rtx
1607 redundant_insn (rtx insn, rtx target, rtx delay_list)
1609 rtx target_main = target;
1610 rtx ipat = PATTERN (insn);
1611 rtx trial, pat;
1612 struct resources needed, set;
1613 int i;
1614 unsigned insns_to_search;
1616 /* If INSN has any REG_UNUSED notes, it can't match anything since we
1617 are allowed to not actually assign to such a register. */
1618 if (find_reg_note (insn, REG_UNUSED, NULL_RTX) != 0)
1619 return 0;
1621 /* Scan backwards looking for a match. */
1622 for (trial = PREV_INSN (target),
1623 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1624 trial && insns_to_search > 0;
1625 trial = PREV_INSN (trial))
1627 if (LABEL_P (trial))
1628 return 0;
1630 if (!NONDEBUG_INSN_P (trial))
1631 continue;
1632 --insns_to_search;
1634 pat = PATTERN (trial);
1635 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1636 continue;
1638 if (GET_CODE (pat) == SEQUENCE)
1640 /* Stop for a CALL and its delay slots because it is difficult to
1641 track its resource needs correctly. */
1642 if (CALL_P (XVECEXP (pat, 0, 0)))
1643 return 0;
1645 /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1646 slots because it is difficult to track its resource needs
1647 correctly. */
1649 #ifdef INSN_SETS_ARE_DELAYED
1650 if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1651 return 0;
1652 #endif
1654 #ifdef INSN_REFERENCES_ARE_DELAYED
1655 if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1656 return 0;
1657 #endif
1659 /* See if any of the insns in the delay slot match, updating
1660 resource requirements as we go. */
1661 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1662 if (GET_CODE (XVECEXP (pat, 0, i)) == GET_CODE (insn)
1663 && rtx_equal_p (PATTERN (XVECEXP (pat, 0, i)), ipat)
1664 && ! find_reg_note (XVECEXP (pat, 0, i), REG_UNUSED, NULL_RTX))
1665 break;
1667 /* If found a match, exit this loop early. */
1668 if (i > 0)
1669 break;
1672 else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat)
1673 && ! find_reg_note (trial, REG_UNUSED, NULL_RTX))
1674 break;
1677 /* If we didn't find an insn that matches, return 0. */
1678 if (trial == 0)
1679 return 0;
1681 /* See what resources this insn sets and needs. If they overlap, or
1682 if this insn references CC0, it can't be redundant. */
1684 CLEAR_RESOURCE (&needed);
1685 CLEAR_RESOURCE (&set);
1686 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1687 mark_referenced_resources (insn, &needed, true);
1689 /* If TARGET is a SEQUENCE, get the main insn. */
1690 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1691 target_main = XVECEXP (PATTERN (target), 0, 0);
1693 if (resource_conflicts_p (&needed, &set)
1694 #ifdef HAVE_cc0
1695 || reg_mentioned_p (cc0_rtx, ipat)
1696 #endif
1697 /* The insn requiring the delay may not set anything needed or set by
1698 INSN. */
1699 || insn_sets_resource_p (target_main, &needed, true)
1700 || insn_sets_resource_p (target_main, &set, true))
1701 return 0;
1703 /* Insns we pass may not set either NEEDED or SET, so merge them for
1704 simpler tests. */
1705 needed.memory |= set.memory;
1706 needed.unch_memory |= set.unch_memory;
1707 IOR_HARD_REG_SET (needed.regs, set.regs);
1709 /* This insn isn't redundant if it conflicts with an insn that either is
1710 or will be in a delay slot of TARGET. */
1712 while (delay_list)
1714 if (insn_sets_resource_p (XEXP (delay_list, 0), &needed, true))
1715 return 0;
1716 delay_list = XEXP (delay_list, 1);
1719 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1720 for (i = 1; i < XVECLEN (PATTERN (target), 0); i++)
1721 if (insn_sets_resource_p (XVECEXP (PATTERN (target), 0, i), &needed,
1722 true))
1723 return 0;
1725 /* Scan backwards until we reach a label or an insn that uses something
1726 INSN sets or sets something insn uses or sets. */
1728 for (trial = PREV_INSN (target),
1729 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1730 trial && !LABEL_P (trial) && insns_to_search > 0;
1731 trial = PREV_INSN (trial))
1733 if (!NONDEBUG_INSN_P (trial))
1734 continue;
1735 --insns_to_search;
1737 pat = PATTERN (trial);
1738 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1739 continue;
1741 if (GET_CODE (pat) == SEQUENCE)
1743 bool annul_p = false;
1744 rtx control = XVECEXP (pat, 0, 0);
1746 /* If this is a CALL_INSN and its delay slots, it is hard to track
1747 the resource needs properly, so give up. */
1748 if (CALL_P (control))
1749 return 0;
1751 /* If this is an INSN or JUMP_INSN with delayed effects, it
1752 is hard to track the resource needs properly, so give up. */
1754 #ifdef INSN_SETS_ARE_DELAYED
1755 if (INSN_SETS_ARE_DELAYED (control))
1756 return 0;
1757 #endif
1759 #ifdef INSN_REFERENCES_ARE_DELAYED
1760 if (INSN_REFERENCES_ARE_DELAYED (control))
1761 return 0;
1762 #endif
1764 if (JUMP_P (control))
1765 annul_p = INSN_ANNULLED_BRANCH_P (control);
1767 /* See if any of the insns in the delay slot match, updating
1768 resource requirements as we go. */
1769 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1771 rtx candidate = XVECEXP (pat, 0, i);
1773 /* If an insn will be annulled if the branch is false, it isn't
1774 considered as a possible duplicate insn. */
1775 if (rtx_equal_p (PATTERN (candidate), ipat)
1776 && ! (annul_p && INSN_FROM_TARGET_P (candidate)))
1778 /* Show that this insn will be used in the sequel. */
1779 INSN_FROM_TARGET_P (candidate) = 0;
1780 return candidate;
1783 /* Unless this is an annulled insn from the target of a branch,
1784 we must stop if it sets anything needed or set by INSN. */
1785 if ((!annul_p || !INSN_FROM_TARGET_P (candidate))
1786 && insn_sets_resource_p (candidate, &needed, true))
1787 return 0;
1790 /* If the insn requiring the delay slot conflicts with INSN, we
1791 must stop. */
1792 if (insn_sets_resource_p (control, &needed, true))
1793 return 0;
1795 else
1797 /* See if TRIAL is the same as INSN. */
1798 pat = PATTERN (trial);
1799 if (rtx_equal_p (pat, ipat))
1800 return trial;
1802 /* Can't go any further if TRIAL conflicts with INSN. */
1803 if (insn_sets_resource_p (trial, &needed, true))
1804 return 0;
1808 return 0;
1811 /* Return 1 if THREAD can only be executed in one way. If LABEL is nonzero,
1812 it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1813 is nonzero, we are allowed to fall into this thread; otherwise, we are
1814 not.
1816 If LABEL is used more than one or we pass a label other than LABEL before
1817 finding an active insn, we do not own this thread. */
1819 static int
1820 own_thread_p (rtx thread, rtx label, int allow_fallthrough)
1822 rtx active_insn;
1823 rtx insn;
1825 /* We don't own the function end. */
1826 if (thread == 0 || ANY_RETURN_P (thread))
1827 return 0;
1829 /* Get the first active insn, or THREAD, if it is an active insn. */
1830 active_insn = next_active_insn (PREV_INSN (thread));
1832 for (insn = thread; insn != active_insn; insn = NEXT_INSN (insn))
1833 if (LABEL_P (insn)
1834 && (insn != label || LABEL_NUSES (insn) != 1))
1835 return 0;
1837 if (allow_fallthrough)
1838 return 1;
1840 /* Ensure that we reach a BARRIER before any insn or label. */
1841 for (insn = prev_nonnote_insn (thread);
1842 insn == 0 || !BARRIER_P (insn);
1843 insn = prev_nonnote_insn (insn))
1844 if (insn == 0
1845 || LABEL_P (insn)
1846 || (NONJUMP_INSN_P (insn)
1847 && GET_CODE (PATTERN (insn)) != USE
1848 && GET_CODE (PATTERN (insn)) != CLOBBER))
1849 return 0;
1851 return 1;
1854 /* Called when INSN is being moved from a location near the target of a jump.
1855 We leave a marker of the form (use (INSN)) immediately in front
1856 of WHERE for mark_target_live_regs. These markers will be deleted when
1857 reorg finishes.
1859 We used to try to update the live status of registers if WHERE is at
1860 the start of a basic block, but that can't work since we may remove a
1861 BARRIER in relax_delay_slots. */
1863 static void
1864 update_block (rtx insn, rtx where)
1866 /* Ignore if this was in a delay slot and it came from the target of
1867 a branch. */
1868 if (INSN_FROM_TARGET_P (insn))
1869 return;
1871 emit_insn_before (gen_rtx_USE (VOIDmode, insn), where);
1873 /* INSN might be making a value live in a block where it didn't use to
1874 be. So recompute liveness information for this block. */
1876 incr_ticks_for_insn (insn);
1879 /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1880 the basic block containing the jump. */
1882 static int
1883 reorg_redirect_jump (rtx jump, rtx nlabel)
1885 incr_ticks_for_insn (jump);
1886 return redirect_jump (jump, nlabel, 1);
1889 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1890 We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1891 that reference values used in INSN. If we find one, then we move the
1892 REG_DEAD note to INSN.
1894 This is needed to handle the case where a later insn (after INSN) has a
1895 REG_DEAD note for a register used by INSN, and this later insn subsequently
1896 gets moved before a CODE_LABEL because it is a redundant insn. In this
1897 case, mark_target_live_regs may be confused into thinking the register
1898 is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1900 static void
1901 update_reg_dead_notes (rtx insn, rtx delayed_insn)
1903 rtx p, link, next;
1905 for (p = next_nonnote_insn (insn); p != delayed_insn;
1906 p = next_nonnote_insn (p))
1907 for (link = REG_NOTES (p); link; link = next)
1909 next = XEXP (link, 1);
1911 if (REG_NOTE_KIND (link) != REG_DEAD
1912 || !REG_P (XEXP (link, 0)))
1913 continue;
1915 if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
1917 /* Move the REG_DEAD note from P to INSN. */
1918 remove_note (p, link);
1919 XEXP (link, 1) = REG_NOTES (insn);
1920 REG_NOTES (insn) = link;
1925 /* Called when an insn redundant with start_insn is deleted. If there
1926 is a REG_DEAD note for the target of start_insn between start_insn
1927 and stop_insn, then the REG_DEAD note needs to be deleted since the
1928 value no longer dies there.
1930 If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1931 confused into thinking the register is dead. */
1933 static void
1934 fix_reg_dead_note (rtx start_insn, rtx stop_insn)
1936 rtx p, link, next;
1938 for (p = next_nonnote_insn (start_insn); p != stop_insn;
1939 p = next_nonnote_insn (p))
1940 for (link = REG_NOTES (p); link; link = next)
1942 next = XEXP (link, 1);
1944 if (REG_NOTE_KIND (link) != REG_DEAD
1945 || !REG_P (XEXP (link, 0)))
1946 continue;
1948 if (reg_set_p (XEXP (link, 0), PATTERN (start_insn)))
1950 remove_note (p, link);
1951 return;
1956 /* Delete any REG_UNUSED notes that exist on INSN but not on REDUNDANT_INSN.
1958 This handles the case of udivmodXi4 instructions which optimize their
1959 output depending on whether any REG_UNUSED notes are present.
1960 we must make sure that INSN calculates as many results as REDUNDANT_INSN
1961 does. */
1963 static void
1964 update_reg_unused_notes (rtx insn, rtx redundant_insn)
1966 rtx link, next;
1968 for (link = REG_NOTES (insn); link; link = next)
1970 next = XEXP (link, 1);
1972 if (REG_NOTE_KIND (link) != REG_UNUSED
1973 || !REG_P (XEXP (link, 0)))
1974 continue;
1976 if (! find_regno_note (redundant_insn, REG_UNUSED,
1977 REGNO (XEXP (link, 0))))
1978 remove_note (insn, link);
1982 /* Return the label before INSN, or put a new label there. */
1984 static rtx
1985 get_label_before (rtx insn)
1987 rtx label;
1989 /* Find an existing label at this point
1990 or make a new one if there is none. */
1991 label = prev_nonnote_insn (insn);
1993 if (label == 0 || !LABEL_P (label))
1995 rtx prev = PREV_INSN (insn);
1997 label = gen_label_rtx ();
1998 emit_label_after (label, prev);
1999 LABEL_NUSES (label) = 0;
2001 return label;
2004 /* Scan a function looking for insns that need a delay slot and find insns to
2005 put into the delay slot.
2007 NON_JUMPS_P is nonzero if we are to only try to fill non-jump insns (such
2008 as calls). We do these first since we don't want jump insns (that are
2009 easier to fill) to get the only insns that could be used for non-jump insns.
2010 When it is zero, only try to fill JUMP_INSNs.
2012 When slots are filled in this manner, the insns (including the
2013 delay_insn) are put together in a SEQUENCE rtx. In this fashion,
2014 it is possible to tell whether a delay slot has really been filled
2015 or not. `final' knows how to deal with this, by communicating
2016 through FINAL_SEQUENCE. */
2018 static void
2019 fill_simple_delay_slots (int non_jumps_p)
2021 rtx insn, pat, trial, next_trial;
2022 int i;
2023 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2024 struct resources needed, set;
2025 int slots_to_fill, slots_filled;
2026 rtx delay_list;
2028 for (i = 0; i < num_unfilled_slots; i++)
2030 int flags;
2031 /* Get the next insn to fill. If it has already had any slots assigned,
2032 we can't do anything with it. Maybe we'll improve this later. */
2034 insn = unfilled_slots_base[i];
2035 if (insn == 0
2036 || INSN_DELETED_P (insn)
2037 || (NONJUMP_INSN_P (insn)
2038 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2039 || (JUMP_P (insn) && non_jumps_p)
2040 || (!JUMP_P (insn) && ! non_jumps_p))
2041 continue;
2043 /* It may have been that this insn used to need delay slots, but
2044 now doesn't; ignore in that case. This can happen, for example,
2045 on the HP PA RISC, where the number of delay slots depends on
2046 what insns are nearby. */
2047 slots_to_fill = num_delay_slots (insn);
2049 /* Some machine description have defined instructions to have
2050 delay slots only in certain circumstances which may depend on
2051 nearby insns (which change due to reorg's actions).
2053 For example, the PA port normally has delay slots for unconditional
2054 jumps.
2056 However, the PA port claims such jumps do not have a delay slot
2057 if they are immediate successors of certain CALL_INSNs. This
2058 allows the port to favor filling the delay slot of the call with
2059 the unconditional jump. */
2060 if (slots_to_fill == 0)
2061 continue;
2063 /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
2064 says how many. After initialization, first try optimizing
2066 call _foo call _foo
2067 nop add %o7,.-L1,%o7
2068 b,a L1
2071 If this case applies, the delay slot of the call is filled with
2072 the unconditional jump. This is done first to avoid having the
2073 delay slot of the call filled in the backward scan. Also, since
2074 the unconditional jump is likely to also have a delay slot, that
2075 insn must exist when it is subsequently scanned.
2077 This is tried on each insn with delay slots as some machines
2078 have insns which perform calls, but are not represented as
2079 CALL_INSNs. */
2081 slots_filled = 0;
2082 delay_list = 0;
2084 if (JUMP_P (insn))
2085 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2086 else
2087 flags = get_jump_flags (insn, NULL_RTX);
2089 if ((trial = next_active_insn (insn))
2090 && JUMP_P (trial)
2091 && simplejump_p (trial)
2092 && eligible_for_delay (insn, slots_filled, trial, flags)
2093 && no_labels_between_p (insn, trial)
2094 && ! can_throw_internal (trial))
2096 rtx *tmp;
2097 slots_filled++;
2098 delay_list = add_to_delay_list (trial, delay_list);
2100 /* TRIAL may have had its delay slot filled, then unfilled. When
2101 the delay slot is unfilled, TRIAL is placed back on the unfilled
2102 slots obstack. Unfortunately, it is placed on the end of the
2103 obstack, not in its original location. Therefore, we must search
2104 from entry i + 1 to the end of the unfilled slots obstack to
2105 try and find TRIAL. */
2106 tmp = &unfilled_slots_base[i + 1];
2107 while (*tmp != trial && tmp != unfilled_slots_next)
2108 tmp++;
2110 /* Remove the unconditional jump from consideration for delay slot
2111 filling and unthread it. */
2112 if (*tmp == trial)
2113 *tmp = 0;
2115 rtx next = NEXT_INSN (trial);
2116 rtx prev = PREV_INSN (trial);
2117 if (prev)
2118 NEXT_INSN (prev) = next;
2119 if (next)
2120 PREV_INSN (next) = prev;
2124 /* Now, scan backwards from the insn to search for a potential
2125 delay-slot candidate. Stop searching when a label or jump is hit.
2127 For each candidate, if it is to go into the delay slot (moved
2128 forward in execution sequence), it must not need or set any resources
2129 that were set by later insns and must not set any resources that
2130 are needed for those insns.
2132 The delay slot insn itself sets resources unless it is a call
2133 (in which case the called routine, not the insn itself, is doing
2134 the setting). */
2136 if (slots_filled < slots_to_fill)
2138 CLEAR_RESOURCE (&needed);
2139 CLEAR_RESOURCE (&set);
2140 mark_set_resources (insn, &set, 0, MARK_SRC_DEST);
2141 mark_referenced_resources (insn, &needed, false);
2143 for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, 1);
2144 trial = next_trial)
2146 next_trial = prev_nonnote_insn (trial);
2148 /* This must be an INSN or CALL_INSN. */
2149 pat = PATTERN (trial);
2151 /* Stand-alone USE and CLOBBER are just for flow. */
2152 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2153 continue;
2155 /* Check for resource conflict first, to avoid unnecessary
2156 splitting. */
2157 if (! insn_references_resource_p (trial, &set, true)
2158 && ! insn_sets_resource_p (trial, &set, true)
2159 && ! insn_sets_resource_p (trial, &needed, true)
2160 #ifdef HAVE_cc0
2161 /* Can't separate set of cc0 from its use. */
2162 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2163 #endif
2164 && ! can_throw_internal (trial))
2166 trial = try_split (pat, trial, 1);
2167 next_trial = prev_nonnote_insn (trial);
2168 if (eligible_for_delay (insn, slots_filled, trial, flags))
2170 /* In this case, we are searching backward, so if we
2171 find insns to put on the delay list, we want
2172 to put them at the head, rather than the
2173 tail, of the list. */
2175 update_reg_dead_notes (trial, insn);
2176 delay_list = gen_rtx_INSN_LIST (VOIDmode,
2177 trial, delay_list);
2178 update_block (trial, trial);
2179 delete_related_insns (trial);
2180 if (slots_to_fill == ++slots_filled)
2181 break;
2182 continue;
2186 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2187 mark_referenced_resources (trial, &needed, true);
2191 /* If all needed slots haven't been filled, we come here. */
2193 /* Try to optimize case of jumping around a single insn. */
2194 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
2195 if (slots_filled != slots_to_fill
2196 && delay_list == 0
2197 && JUMP_P (insn)
2198 && (condjump_p (insn) || condjump_in_parallel_p (insn)))
2200 delay_list = optimize_skip (insn);
2201 if (delay_list)
2202 slots_filled += 1;
2204 #endif
2206 /* Try to get insns from beyond the insn needing the delay slot.
2207 These insns can neither set or reference resources set in insns being
2208 skipped, cannot set resources in the insn being skipped, and, if this
2209 is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2210 call might not return).
2212 There used to be code which continued past the target label if
2213 we saw all uses of the target label. This code did not work,
2214 because it failed to account for some instructions which were
2215 both annulled and marked as from the target. This can happen as a
2216 result of optimize_skip. Since this code was redundant with
2217 fill_eager_delay_slots anyways, it was just deleted. */
2219 if (slots_filled != slots_to_fill
2220 /* If this instruction could throw an exception which is
2221 caught in the same function, then it's not safe to fill
2222 the delay slot with an instruction from beyond this
2223 point. For example, consider:
2225 int i = 2;
2227 try {
2228 f();
2229 i = 3;
2230 } catch (...) {}
2232 return i;
2234 Even though `i' is a local variable, we must be sure not
2235 to put `i = 3' in the delay slot if `f' might throw an
2236 exception.
2238 Presumably, we should also check to see if we could get
2239 back to this function via `setjmp'. */
2240 && ! can_throw_internal (insn)
2241 && (!JUMP_P (insn)
2242 || ((condjump_p (insn) || condjump_in_parallel_p (insn))
2243 && ! simplejump_p (insn)
2244 && !ANY_RETURN_P (JUMP_LABEL (insn)))))
2246 /* Invariant: If insn is a JUMP_INSN, the insn's jump
2247 label. Otherwise, zero. */
2248 rtx target = 0;
2249 int maybe_never = 0;
2250 rtx pat, trial_delay;
2252 CLEAR_RESOURCE (&needed);
2253 CLEAR_RESOURCE (&set);
2255 if (CALL_P (insn))
2257 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2258 mark_referenced_resources (insn, &needed, true);
2259 maybe_never = 1;
2261 else
2263 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2264 mark_referenced_resources (insn, &needed, true);
2265 if (JUMP_P (insn))
2266 target = JUMP_LABEL (insn);
2269 if (target == 0 || ANY_RETURN_P (target))
2270 for (trial = next_nonnote_insn (insn); !stop_search_p (trial, 1);
2271 trial = next_trial)
2273 next_trial = next_nonnote_insn (trial);
2275 /* This must be an INSN or CALL_INSN. */
2276 pat = PATTERN (trial);
2278 /* Stand-alone USE and CLOBBER are just for flow. */
2279 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2280 continue;
2282 /* If this already has filled delay slots, get the insn needing
2283 the delay slots. */
2284 if (GET_CODE (pat) == SEQUENCE)
2285 trial_delay = XVECEXP (pat, 0, 0);
2286 else
2287 trial_delay = trial;
2289 /* Stop our search when seeing a jump. */
2290 if (JUMP_P (trial_delay))
2291 break;
2293 /* See if we have a resource problem before we try to
2294 split. */
2295 if (GET_CODE (pat) != SEQUENCE
2296 && ! insn_references_resource_p (trial, &set, true)
2297 && ! insn_sets_resource_p (trial, &set, true)
2298 && ! insn_sets_resource_p (trial, &needed, true)
2299 #ifdef HAVE_cc0
2300 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2301 #endif
2302 && ! (maybe_never && may_trap_or_fault_p (pat))
2303 && (trial = try_split (pat, trial, 0))
2304 && eligible_for_delay (insn, slots_filled, trial, flags)
2305 && ! can_throw_internal(trial))
2307 next_trial = next_nonnote_insn (trial);
2308 delay_list = add_to_delay_list (trial, delay_list);
2310 #ifdef HAVE_cc0
2311 if (reg_mentioned_p (cc0_rtx, pat))
2312 link_cc0_insns (trial);
2313 #endif
2315 delete_related_insns (trial);
2316 if (slots_to_fill == ++slots_filled)
2317 break;
2318 continue;
2321 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2322 mark_referenced_resources (trial, &needed, true);
2324 /* Ensure we don't put insns between the setting of cc and the
2325 comparison by moving a setting of cc into an earlier delay
2326 slot since these insns could clobber the condition code. */
2327 set.cc = 1;
2329 /* If this is a call or jump, we might not get here. */
2330 if (CALL_P (trial_delay)
2331 || JUMP_P (trial_delay))
2332 maybe_never = 1;
2335 /* If there are slots left to fill and our search was stopped by an
2336 unconditional branch, try the insn at the branch target. We can
2337 redirect the branch if it works.
2339 Don't do this if the insn at the branch target is a branch. */
2340 if (slots_to_fill != slots_filled
2341 && trial
2342 && jump_to_label_p (trial)
2343 && simplejump_p (trial)
2344 && (target == 0 || JUMP_LABEL (trial) == target)
2345 && (next_trial = next_active_insn (JUMP_LABEL (trial))) != 0
2346 && ! (NONJUMP_INSN_P (next_trial)
2347 && GET_CODE (PATTERN (next_trial)) == SEQUENCE)
2348 && !JUMP_P (next_trial)
2349 && ! insn_references_resource_p (next_trial, &set, true)
2350 && ! insn_sets_resource_p (next_trial, &set, true)
2351 && ! insn_sets_resource_p (next_trial, &needed, true)
2352 #ifdef HAVE_cc0
2353 && ! reg_mentioned_p (cc0_rtx, PATTERN (next_trial))
2354 #endif
2355 && ! (maybe_never && may_trap_or_fault_p (PATTERN (next_trial)))
2356 && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2357 && eligible_for_delay (insn, slots_filled, next_trial, flags)
2358 && ! can_throw_internal (trial))
2360 /* See comment in relax_delay_slots about necessity of using
2361 next_real_insn here. */
2362 rtx new_label = next_real_insn (next_trial);
2364 if (new_label != 0)
2365 new_label = get_label_before (new_label);
2366 else
2367 new_label = find_end_label (simple_return_rtx);
2369 if (new_label)
2371 delay_list
2372 = add_to_delay_list (copy_rtx (next_trial), delay_list);
2373 slots_filled++;
2374 reorg_redirect_jump (trial, new_label);
2376 /* If we merged because we both jumped to the same place,
2377 redirect the original insn also. */
2378 if (target)
2379 reorg_redirect_jump (insn, new_label);
2384 /* If this is an unconditional jump, then try to get insns from the
2385 target of the jump. */
2386 if (JUMP_P (insn)
2387 && simplejump_p (insn)
2388 && slots_filled != slots_to_fill)
2389 delay_list
2390 = fill_slots_from_thread (insn, const_true_rtx,
2391 next_active_insn (JUMP_LABEL (insn)),
2392 NULL, 1, 1,
2393 own_thread_p (JUMP_LABEL (insn),
2394 JUMP_LABEL (insn), 0),
2395 slots_to_fill, &slots_filled,
2396 delay_list);
2398 if (delay_list)
2399 unfilled_slots_base[i]
2400 = emit_delay_sequence (insn, delay_list, slots_filled);
2402 if (slots_to_fill == slots_filled)
2403 unfilled_slots_base[i] = 0;
2405 note_delay_statistics (slots_filled, 0);
2408 #ifdef DELAY_SLOTS_FOR_EPILOGUE
2409 /* See if the epilogue needs any delay slots. Try to fill them if so.
2410 The only thing we can do is scan backwards from the end of the
2411 function. If we did this in a previous pass, it is incorrect to do it
2412 again. */
2413 if (crtl->epilogue_delay_list)
2414 return;
2416 slots_to_fill = DELAY_SLOTS_FOR_EPILOGUE;
2417 if (slots_to_fill == 0)
2418 return;
2420 slots_filled = 0;
2421 CLEAR_RESOURCE (&set);
2423 /* The frame pointer and stack pointer are needed at the beginning of
2424 the epilogue, so instructions setting them can not be put in the
2425 epilogue delay slot. However, everything else needed at function
2426 end is safe, so we don't want to use end_of_function_needs here. */
2427 CLEAR_RESOURCE (&needed);
2428 if (frame_pointer_needed)
2430 SET_HARD_REG_BIT (needed.regs, FRAME_POINTER_REGNUM);
2431 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2432 SET_HARD_REG_BIT (needed.regs, HARD_FRAME_POINTER_REGNUM);
2433 #endif
2434 if (! EXIT_IGNORE_STACK
2435 || crtl->sp_is_unchanging)
2436 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2438 else
2439 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2441 #ifdef EPILOGUE_USES
2442 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2444 if (EPILOGUE_USES (i))
2445 SET_HARD_REG_BIT (needed.regs, i);
2447 #endif
2449 for (trial = get_last_insn (); ! stop_search_p (trial, 1);
2450 trial = PREV_INSN (trial))
2452 if (NOTE_P (trial))
2453 continue;
2454 pat = PATTERN (trial);
2455 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2456 continue;
2458 if (! insn_references_resource_p (trial, &set, true)
2459 && ! insn_sets_resource_p (trial, &needed, true)
2460 && ! insn_sets_resource_p (trial, &set, true)
2461 #ifdef HAVE_cc0
2462 /* Don't want to mess with cc0 here. */
2463 && ! reg_mentioned_p (cc0_rtx, pat)
2464 #endif
2465 && ! can_throw_internal (trial))
2467 trial = try_split (pat, trial, 1);
2468 if (ELIGIBLE_FOR_EPILOGUE_DELAY (trial, slots_filled))
2470 /* Here as well we are searching backward, so put the
2471 insns we find on the head of the list. */
2473 crtl->epilogue_delay_list
2474 = gen_rtx_INSN_LIST (VOIDmode, trial,
2475 crtl->epilogue_delay_list);
2476 mark_end_of_function_resources (trial, true);
2477 update_block (trial, trial);
2478 delete_related_insns (trial);
2480 /* Clear deleted bit so final.c will output the insn. */
2481 INSN_DELETED_P (trial) = 0;
2483 if (slots_to_fill == ++slots_filled)
2484 break;
2485 continue;
2489 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2490 mark_referenced_resources (trial, &needed, true);
2493 note_delay_statistics (slots_filled, 0);
2494 #endif
2497 /* Follow any unconditional jump at LABEL;
2498 return the ultimate label reached by any such chain of jumps.
2499 Return a suitable return rtx if the chain ultimately leads to a
2500 return instruction.
2501 If LABEL is not followed by a jump, return LABEL.
2502 If the chain loops or we can't find end, return LABEL,
2503 since that tells caller to avoid changing the insn. */
2505 static rtx
2506 follow_jumps (rtx label)
2508 rtx insn;
2509 rtx next;
2510 rtx value = label;
2511 int depth;
2513 if (ANY_RETURN_P (label))
2514 return label;
2515 for (depth = 0;
2516 (depth < 10
2517 && (insn = next_active_insn (value)) != 0
2518 && JUMP_P (insn)
2519 && JUMP_LABEL (insn) != NULL_RTX
2520 && ((any_uncondjump_p (insn) && onlyjump_p (insn))
2521 || ANY_RETURN_P (PATTERN (insn)))
2522 && (next = NEXT_INSN (insn))
2523 && BARRIER_P (next));
2524 depth++)
2526 rtx this_label = JUMP_LABEL (insn);
2527 rtx tem;
2529 /* If we have found a cycle, make the insn jump to itself. */
2530 if (this_label == label)
2531 return label;
2532 if (ANY_RETURN_P (this_label))
2533 return this_label;
2534 tem = next_active_insn (this_label);
2535 if (tem
2536 && (GET_CODE (PATTERN (tem)) == ADDR_VEC
2537 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
2538 break;
2540 value = this_label;
2542 if (depth == 10)
2543 return label;
2544 return value;
2547 /* Try to find insns to place in delay slots.
2549 INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2550 or is an unconditional branch if CONDITION is const_true_rtx.
2551 *PSLOTS_FILLED is updated with the number of slots that we have filled.
2553 THREAD is a flow-of-control, either the insns to be executed if the
2554 branch is true or if the branch is false, THREAD_IF_TRUE says which.
2556 OPPOSITE_THREAD is the thread in the opposite direction. It is used
2557 to see if any potential delay slot insns set things needed there.
2559 LIKELY is nonzero if it is extremely likely that the branch will be
2560 taken and THREAD_IF_TRUE is set. This is used for the branch at the
2561 end of a loop back up to the top.
2563 OWN_THREAD and OWN_OPPOSITE_THREAD are true if we are the only user of the
2564 thread. I.e., it is the fallthrough code of our jump or the target of the
2565 jump when we are the only jump going there.
2567 If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2568 case, we can only take insns from the head of the thread for our delay
2569 slot. We then adjust the jump to point after the insns we have taken. */
2571 static rtx
2572 fill_slots_from_thread (rtx insn, rtx condition, rtx thread,
2573 rtx opposite_thread, int likely, int thread_if_true,
2574 int own_thread, int slots_to_fill,
2575 int *pslots_filled, rtx delay_list)
2577 rtx new_thread;
2578 struct resources opposite_needed, set, needed;
2579 rtx trial;
2580 int lose = 0;
2581 int must_annul = 0;
2582 int flags;
2584 /* Validate our arguments. */
2585 gcc_assert(condition != const_true_rtx || thread_if_true);
2586 gcc_assert(own_thread || thread_if_true);
2588 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2590 /* If our thread is the end of subroutine, we can't get any delay
2591 insns from that. */
2592 if (thread == NULL_RTX || ANY_RETURN_P (thread))
2593 return delay_list;
2595 /* If this is an unconditional branch, nothing is needed at the
2596 opposite thread. Otherwise, compute what is needed there. */
2597 if (condition == const_true_rtx)
2598 CLEAR_RESOURCE (&opposite_needed);
2599 else
2600 mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2602 /* If the insn at THREAD can be split, do it here to avoid having to
2603 update THREAD and NEW_THREAD if it is done in the loop below. Also
2604 initialize NEW_THREAD. */
2606 new_thread = thread = try_split (PATTERN (thread), thread, 0);
2608 /* Scan insns at THREAD. We are looking for an insn that can be removed
2609 from THREAD (it neither sets nor references resources that were set
2610 ahead of it and it doesn't set anything needs by the insns ahead of
2611 it) and that either can be placed in an annulling insn or aren't
2612 needed at OPPOSITE_THREAD. */
2614 CLEAR_RESOURCE (&needed);
2615 CLEAR_RESOURCE (&set);
2617 /* If we do not own this thread, we must stop as soon as we find
2618 something that we can't put in a delay slot, since all we can do
2619 is branch into THREAD at a later point. Therefore, labels stop
2620 the search if this is not the `true' thread. */
2622 for (trial = thread;
2623 ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2624 trial = next_nonnote_insn (trial))
2626 rtx pat, old_trial;
2628 /* If we have passed a label, we no longer own this thread. */
2629 if (LABEL_P (trial))
2631 own_thread = 0;
2632 continue;
2635 pat = PATTERN (trial);
2636 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2637 continue;
2639 /* If TRIAL conflicts with the insns ahead of it, we lose. Also,
2640 don't separate or copy insns that set and use CC0. */
2641 if (! insn_references_resource_p (trial, &set, true)
2642 && ! insn_sets_resource_p (trial, &set, true)
2643 && ! insn_sets_resource_p (trial, &needed, true)
2644 #ifdef HAVE_cc0
2645 && ! (reg_mentioned_p (cc0_rtx, pat)
2646 && (! own_thread || ! sets_cc0_p (pat)))
2647 #endif
2648 && ! can_throw_internal (trial))
2650 rtx prior_insn;
2652 /* If TRIAL is redundant with some insn before INSN, we don't
2653 actually need to add it to the delay list; we can merely pretend
2654 we did. */
2655 if ((prior_insn = redundant_insn (trial, insn, delay_list)))
2657 fix_reg_dead_note (prior_insn, insn);
2658 if (own_thread)
2660 update_block (trial, thread);
2661 if (trial == thread)
2663 thread = next_active_insn (thread);
2664 if (new_thread == trial)
2665 new_thread = thread;
2668 delete_related_insns (trial);
2670 else
2672 update_reg_unused_notes (prior_insn, trial);
2673 new_thread = next_active_insn (trial);
2676 continue;
2679 /* There are two ways we can win: If TRIAL doesn't set anything
2680 needed at the opposite thread and can't trap, or if it can
2681 go into an annulled delay slot. */
2682 if (!must_annul
2683 && (condition == const_true_rtx
2684 || (! insn_sets_resource_p (trial, &opposite_needed, true)
2685 && ! may_trap_or_fault_p (pat)
2686 && ! RTX_FRAME_RELATED_P (trial))))
2688 old_trial = trial;
2689 trial = try_split (pat, trial, 0);
2690 if (new_thread == old_trial)
2691 new_thread = trial;
2692 if (thread == old_trial)
2693 thread = trial;
2694 pat = PATTERN (trial);
2695 if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2696 goto winner;
2698 else if (0
2699 #ifdef ANNUL_IFTRUE_SLOTS
2700 || ! thread_if_true
2701 #endif
2702 #ifdef ANNUL_IFFALSE_SLOTS
2703 || thread_if_true
2704 #endif
2707 old_trial = trial;
2708 trial = try_split (pat, trial, 0);
2709 if (new_thread == old_trial)
2710 new_thread = trial;
2711 if (thread == old_trial)
2712 thread = trial;
2713 pat = PATTERN (trial);
2714 if ((must_annul || delay_list == NULL) && (thread_if_true
2715 ? check_annul_list_true_false (0, delay_list)
2716 && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2717 : check_annul_list_true_false (1, delay_list)
2718 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2720 rtx temp;
2722 must_annul = 1;
2723 winner:
2725 #ifdef HAVE_cc0
2726 if (reg_mentioned_p (cc0_rtx, pat))
2727 link_cc0_insns (trial);
2728 #endif
2730 /* If we own this thread, delete the insn. If this is the
2731 destination of a branch, show that a basic block status
2732 may have been updated. In any case, mark the new
2733 starting point of this thread. */
2734 if (own_thread)
2736 rtx note;
2738 update_block (trial, thread);
2739 if (trial == thread)
2741 thread = next_active_insn (thread);
2742 if (new_thread == trial)
2743 new_thread = thread;
2746 /* We are moving this insn, not deleting it. We must
2747 temporarily increment the use count on any referenced
2748 label lest it be deleted by delete_related_insns. */
2749 for (note = REG_NOTES (trial);
2750 note != NULL_RTX;
2751 note = XEXP (note, 1))
2752 if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2753 || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2755 /* REG_LABEL_OPERAND could be
2756 NOTE_INSN_DELETED_LABEL too. */
2757 if (LABEL_P (XEXP (note, 0)))
2758 LABEL_NUSES (XEXP (note, 0))++;
2759 else
2760 gcc_assert (REG_NOTE_KIND (note)
2761 == REG_LABEL_OPERAND);
2763 if (jump_to_label_p (trial))
2764 LABEL_NUSES (JUMP_LABEL (trial))++;
2766 delete_related_insns (trial);
2768 for (note = REG_NOTES (trial);
2769 note != NULL_RTX;
2770 note = XEXP (note, 1))
2771 if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2772 || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2774 /* REG_LABEL_OPERAND could be
2775 NOTE_INSN_DELETED_LABEL too. */
2776 if (LABEL_P (XEXP (note, 0)))
2777 LABEL_NUSES (XEXP (note, 0))--;
2778 else
2779 gcc_assert (REG_NOTE_KIND (note)
2780 == REG_LABEL_OPERAND);
2782 if (jump_to_label_p (trial))
2783 LABEL_NUSES (JUMP_LABEL (trial))--;
2785 else
2786 new_thread = next_active_insn (trial);
2788 temp = own_thread ? trial : copy_rtx (trial);
2789 if (thread_if_true)
2790 INSN_FROM_TARGET_P (temp) = 1;
2792 delay_list = add_to_delay_list (temp, delay_list);
2794 if (slots_to_fill == ++(*pslots_filled))
2796 /* Even though we have filled all the slots, we
2797 may be branching to a location that has a
2798 redundant insn. Skip any if so. */
2799 while (new_thread && ! own_thread
2800 && ! insn_sets_resource_p (new_thread, &set, true)
2801 && ! insn_sets_resource_p (new_thread, &needed,
2802 true)
2803 && ! insn_references_resource_p (new_thread,
2804 &set, true)
2805 && (prior_insn
2806 = redundant_insn (new_thread, insn,
2807 delay_list)))
2809 /* We know we do not own the thread, so no need
2810 to call update_block and delete_insn. */
2811 fix_reg_dead_note (prior_insn, insn);
2812 update_reg_unused_notes (prior_insn, new_thread);
2813 new_thread = next_active_insn (new_thread);
2815 break;
2818 continue;
2823 /* This insn can't go into a delay slot. */
2824 lose = 1;
2825 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2826 mark_referenced_resources (trial, &needed, true);
2828 /* Ensure we don't put insns between the setting of cc and the comparison
2829 by moving a setting of cc into an earlier delay slot since these insns
2830 could clobber the condition code. */
2831 set.cc = 1;
2833 /* If this insn is a register-register copy and the next insn has
2834 a use of our destination, change it to use our source. That way,
2835 it will become a candidate for our delay slot the next time
2836 through this loop. This case occurs commonly in loops that
2837 scan a list.
2839 We could check for more complex cases than those tested below,
2840 but it doesn't seem worth it. It might also be a good idea to try
2841 to swap the two insns. That might do better.
2843 We can't do this if the next insn modifies our destination, because
2844 that would make the replacement into the insn invalid. We also can't
2845 do this if it modifies our source, because it might be an earlyclobber
2846 operand. This latter test also prevents updating the contents of
2847 a PRE_INC. We also can't do this if there's overlap of source and
2848 destination. Overlap may happen for larger-than-register-size modes. */
2850 if (NONJUMP_INSN_P (trial) && GET_CODE (pat) == SET
2851 && REG_P (SET_SRC (pat))
2852 && REG_P (SET_DEST (pat))
2853 && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2855 rtx next = next_nonnote_insn (trial);
2857 if (next && NONJUMP_INSN_P (next)
2858 && GET_CODE (PATTERN (next)) != USE
2859 && ! reg_set_p (SET_DEST (pat), next)
2860 && ! reg_set_p (SET_SRC (pat), next)
2861 && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2862 && ! modified_in_p (SET_DEST (pat), next))
2863 validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2867 /* If we stopped on a branch insn that has delay slots, see if we can
2868 steal some of the insns in those slots. */
2869 if (trial && NONJUMP_INSN_P (trial)
2870 && GET_CODE (PATTERN (trial)) == SEQUENCE
2871 && JUMP_P (XVECEXP (PATTERN (trial), 0, 0)))
2873 /* If this is the `true' thread, we will want to follow the jump,
2874 so we can only do this if we have taken everything up to here. */
2875 if (thread_if_true && trial == new_thread)
2877 delay_list
2878 = steal_delay_list_from_target (insn, condition, PATTERN (trial),
2879 delay_list, &set, &needed,
2880 &opposite_needed, slots_to_fill,
2881 pslots_filled, &must_annul,
2882 &new_thread);
2883 /* If we owned the thread and are told that it branched
2884 elsewhere, make sure we own the thread at the new location. */
2885 if (own_thread && trial != new_thread)
2886 own_thread = own_thread_p (new_thread, new_thread, 0);
2888 else if (! thread_if_true)
2889 delay_list
2890 = steal_delay_list_from_fallthrough (insn, condition,
2891 PATTERN (trial),
2892 delay_list, &set, &needed,
2893 &opposite_needed, slots_to_fill,
2894 pslots_filled, &must_annul);
2897 /* If we haven't found anything for this delay slot and it is very
2898 likely that the branch will be taken, see if the insn at our target
2899 increments or decrements a register with an increment that does not
2900 depend on the destination register. If so, try to place the opposite
2901 arithmetic insn after the jump insn and put the arithmetic insn in the
2902 delay slot. If we can't do this, return. */
2903 if (delay_list == 0 && likely
2904 && new_thread && !ANY_RETURN_P (new_thread)
2905 && NONJUMP_INSN_P (new_thread)
2906 && !RTX_FRAME_RELATED_P (new_thread)
2907 && GET_CODE (PATTERN (new_thread)) != ASM_INPUT
2908 && asm_noperands (PATTERN (new_thread)) < 0)
2910 rtx pat = PATTERN (new_thread);
2911 rtx dest;
2912 rtx src;
2914 trial = new_thread;
2915 pat = PATTERN (trial);
2917 if (!NONJUMP_INSN_P (trial)
2918 || GET_CODE (pat) != SET
2919 || ! eligible_for_delay (insn, 0, trial, flags)
2920 || can_throw_internal (trial))
2921 return 0;
2923 dest = SET_DEST (pat), src = SET_SRC (pat);
2924 if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2925 && rtx_equal_p (XEXP (src, 0), dest)
2926 && (!FLOAT_MODE_P (GET_MODE (src))
2927 || flag_unsafe_math_optimizations)
2928 && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2929 && ! side_effects_p (pat))
2931 rtx other = XEXP (src, 1);
2932 rtx new_arith;
2933 rtx ninsn;
2935 /* If this is a constant adjustment, use the same code with
2936 the negated constant. Otherwise, reverse the sense of the
2937 arithmetic. */
2938 if (CONST_INT_P (other))
2939 new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2940 negate_rtx (GET_MODE (src), other));
2941 else
2942 new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2943 GET_MODE (src), dest, other);
2945 ninsn = emit_insn_after (gen_rtx_SET (VOIDmode, dest, new_arith),
2946 insn);
2948 if (recog_memoized (ninsn) < 0
2949 || (extract_insn (ninsn), ! constrain_operands (1)))
2951 delete_related_insns (ninsn);
2952 return 0;
2955 if (own_thread)
2957 update_block (trial, thread);
2958 if (trial == thread)
2960 thread = next_active_insn (thread);
2961 if (new_thread == trial)
2962 new_thread = thread;
2964 delete_related_insns (trial);
2966 else
2967 new_thread = next_active_insn (trial);
2969 ninsn = own_thread ? trial : copy_rtx (trial);
2970 if (thread_if_true)
2971 INSN_FROM_TARGET_P (ninsn) = 1;
2973 delay_list = add_to_delay_list (ninsn, NULL_RTX);
2974 (*pslots_filled)++;
2978 if (delay_list && must_annul)
2979 INSN_ANNULLED_BRANCH_P (insn) = 1;
2981 /* If we are to branch into the middle of this thread, find an appropriate
2982 label or make a new one if none, and redirect INSN to it. If we hit the
2983 end of the function, use the end-of-function label. */
2984 if (new_thread != thread)
2986 rtx label;
2988 gcc_assert (thread_if_true);
2990 if (new_thread && simplejump_or_return_p (new_thread)
2991 && redirect_with_delay_list_safe_p (insn,
2992 JUMP_LABEL (new_thread),
2993 delay_list))
2994 new_thread = follow_jumps (JUMP_LABEL (new_thread));
2996 if (ANY_RETURN_P (new_thread))
2997 label = find_end_label (new_thread);
2998 else if (LABEL_P (new_thread))
2999 label = new_thread;
3000 else
3001 label = get_label_before (new_thread);
3003 if (label)
3004 reorg_redirect_jump (insn, label);
3007 return delay_list;
3010 /* Make another attempt to find insns to place in delay slots.
3012 We previously looked for insns located in front of the delay insn
3013 and, for non-jump delay insns, located behind the delay insn.
3015 Here only try to schedule jump insns and try to move insns from either
3016 the target or the following insns into the delay slot. If annulling is
3017 supported, we will be likely to do this. Otherwise, we can do this only
3018 if safe. */
3020 static void
3021 fill_eager_delay_slots (void)
3023 rtx insn;
3024 int i;
3025 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
3027 for (i = 0; i < num_unfilled_slots; i++)
3029 rtx condition;
3030 rtx target_label, insn_at_target, fallthrough_insn;
3031 rtx delay_list = 0;
3032 int own_target;
3033 int own_fallthrough;
3034 int prediction, slots_to_fill, slots_filled;
3036 insn = unfilled_slots_base[i];
3037 if (insn == 0
3038 || INSN_DELETED_P (insn)
3039 || !JUMP_P (insn)
3040 || ! (condjump_p (insn) || condjump_in_parallel_p (insn)))
3041 continue;
3043 slots_to_fill = num_delay_slots (insn);
3044 /* Some machine description have defined instructions to have
3045 delay slots only in certain circumstances which may depend on
3046 nearby insns (which change due to reorg's actions).
3048 For example, the PA port normally has delay slots for unconditional
3049 jumps.
3051 However, the PA port claims such jumps do not have a delay slot
3052 if they are immediate successors of certain CALL_INSNs. This
3053 allows the port to favor filling the delay slot of the call with
3054 the unconditional jump. */
3055 if (slots_to_fill == 0)
3056 continue;
3058 slots_filled = 0;
3059 target_label = JUMP_LABEL (insn);
3060 condition = get_branch_condition (insn, target_label);
3062 if (condition == 0)
3063 continue;
3065 /* Get the next active fallthrough and target insns and see if we own
3066 them. Then see whether the branch is likely true. We don't need
3067 to do a lot of this for unconditional branches. */
3069 insn_at_target = first_active_target_insn (target_label);
3070 own_target = own_thread_p (target_label, target_label, 0);
3072 if (condition == const_true_rtx)
3074 own_fallthrough = 0;
3075 fallthrough_insn = 0;
3076 prediction = 2;
3078 else
3080 fallthrough_insn = next_active_insn (insn);
3081 own_fallthrough = own_thread_p (NEXT_INSN (insn), NULL_RTX, 1);
3082 prediction = mostly_true_jump (insn, condition);
3085 /* If this insn is expected to branch, first try to get insns from our
3086 target, then our fallthrough insns. If it is not expected to branch,
3087 try the other order. */
3089 if (prediction > 0)
3091 delay_list
3092 = fill_slots_from_thread (insn, condition, insn_at_target,
3093 fallthrough_insn, prediction == 2, 1,
3094 own_target,
3095 slots_to_fill, &slots_filled, delay_list);
3097 if (delay_list == 0 && own_fallthrough)
3099 /* Even though we didn't find anything for delay slots,
3100 we might have found a redundant insn which we deleted
3101 from the thread that was filled. So we have to recompute
3102 the next insn at the target. */
3103 target_label = JUMP_LABEL (insn);
3104 insn_at_target = first_active_target_insn (target_label);
3106 delay_list
3107 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3108 insn_at_target, 0, 0,
3109 own_fallthrough,
3110 slots_to_fill, &slots_filled,
3111 delay_list);
3114 else
3116 if (own_fallthrough)
3117 delay_list
3118 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3119 insn_at_target, 0, 0,
3120 own_fallthrough,
3121 slots_to_fill, &slots_filled,
3122 delay_list);
3124 if (delay_list == 0)
3125 delay_list
3126 = fill_slots_from_thread (insn, condition, insn_at_target,
3127 next_active_insn (insn), 0, 1,
3128 own_target,
3129 slots_to_fill, &slots_filled,
3130 delay_list);
3133 if (delay_list)
3134 unfilled_slots_base[i]
3135 = emit_delay_sequence (insn, delay_list, slots_filled);
3137 if (slots_to_fill == slots_filled)
3138 unfilled_slots_base[i] = 0;
3140 note_delay_statistics (slots_filled, 1);
3144 static void delete_computation (rtx insn);
3146 /* Recursively delete prior insns that compute the value (used only by INSN
3147 which the caller is deleting) stored in the register mentioned by NOTE
3148 which is a REG_DEAD note associated with INSN. */
3150 static void
3151 delete_prior_computation (rtx note, rtx insn)
3153 rtx our_prev;
3154 rtx reg = XEXP (note, 0);
3156 for (our_prev = prev_nonnote_insn (insn);
3157 our_prev && (NONJUMP_INSN_P (our_prev)
3158 || CALL_P (our_prev));
3159 our_prev = prev_nonnote_insn (our_prev))
3161 rtx pat = PATTERN (our_prev);
3163 /* If we reach a CALL which is not calling a const function
3164 or the callee pops the arguments, then give up. */
3165 if (CALL_P (our_prev)
3166 && (! RTL_CONST_CALL_P (our_prev)
3167 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
3168 break;
3170 /* If we reach a SEQUENCE, it is too complex to try to
3171 do anything with it, so give up. We can be run during
3172 and after reorg, so SEQUENCE rtl can legitimately show
3173 up here. */
3174 if (GET_CODE (pat) == SEQUENCE)
3175 break;
3177 if (GET_CODE (pat) == USE
3178 && NONJUMP_INSN_P (XEXP (pat, 0)))
3179 /* reorg creates USEs that look like this. We leave them
3180 alone because reorg needs them for its own purposes. */
3181 break;
3183 if (reg_set_p (reg, pat))
3185 if (side_effects_p (pat) && !CALL_P (our_prev))
3186 break;
3188 if (GET_CODE (pat) == PARALLEL)
3190 /* If we find a SET of something else, we can't
3191 delete the insn. */
3193 int i;
3195 for (i = 0; i < XVECLEN (pat, 0); i++)
3197 rtx part = XVECEXP (pat, 0, i);
3199 if (GET_CODE (part) == SET
3200 && SET_DEST (part) != reg)
3201 break;
3204 if (i == XVECLEN (pat, 0))
3205 delete_computation (our_prev);
3207 else if (GET_CODE (pat) == SET
3208 && REG_P (SET_DEST (pat)))
3210 int dest_regno = REGNO (SET_DEST (pat));
3211 int dest_endregno = END_REGNO (SET_DEST (pat));
3212 int regno = REGNO (reg);
3213 int endregno = END_REGNO (reg);
3215 if (dest_regno >= regno
3216 && dest_endregno <= endregno)
3217 delete_computation (our_prev);
3219 /* We may have a multi-word hard register and some, but not
3220 all, of the words of the register are needed in subsequent
3221 insns. Write REG_UNUSED notes for those parts that were not
3222 needed. */
3223 else if (dest_regno <= regno
3224 && dest_endregno >= endregno)
3226 int i;
3228 add_reg_note (our_prev, REG_UNUSED, reg);
3230 for (i = dest_regno; i < dest_endregno; i++)
3231 if (! find_regno_note (our_prev, REG_UNUSED, i))
3232 break;
3234 if (i == dest_endregno)
3235 delete_computation (our_prev);
3239 break;
3242 /* If PAT references the register that dies here, it is an
3243 additional use. Hence any prior SET isn't dead. However, this
3244 insn becomes the new place for the REG_DEAD note. */
3245 if (reg_overlap_mentioned_p (reg, pat))
3247 XEXP (note, 1) = REG_NOTES (our_prev);
3248 REG_NOTES (our_prev) = note;
3249 break;
3254 /* Delete INSN and recursively delete insns that compute values used only
3255 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3257 Look at all our REG_DEAD notes. If a previous insn does nothing other
3258 than set a register that dies in this insn, we can delete that insn
3259 as well.
3261 On machines with CC0, if CC0 is used in this insn, we may be able to
3262 delete the insn that set it. */
3264 static void
3265 delete_computation (rtx insn)
3267 rtx note, next;
3269 #ifdef HAVE_cc0
3270 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
3272 rtx prev = prev_nonnote_insn (insn);
3273 /* We assume that at this stage
3274 CC's are always set explicitly
3275 and always immediately before the jump that
3276 will use them. So if the previous insn
3277 exists to set the CC's, delete it
3278 (unless it performs auto-increments, etc.). */
3279 if (prev && NONJUMP_INSN_P (prev)
3280 && sets_cc0_p (PATTERN (prev)))
3282 if (sets_cc0_p (PATTERN (prev)) > 0
3283 && ! side_effects_p (PATTERN (prev)))
3284 delete_computation (prev);
3285 else
3286 /* Otherwise, show that cc0 won't be used. */
3287 add_reg_note (prev, REG_UNUSED, cc0_rtx);
3290 #endif
3292 for (note = REG_NOTES (insn); note; note = next)
3294 next = XEXP (note, 1);
3296 if (REG_NOTE_KIND (note) != REG_DEAD
3297 /* Verify that the REG_NOTE is legitimate. */
3298 || !REG_P (XEXP (note, 0)))
3299 continue;
3301 delete_prior_computation (note, insn);
3304 delete_related_insns (insn);
3307 /* If all INSN does is set the pc, delete it,
3308 and delete the insn that set the condition codes for it
3309 if that's what the previous thing was. */
3311 static void
3312 delete_jump (rtx insn)
3314 rtx set = single_set (insn);
3316 if (set && GET_CODE (SET_DEST (set)) == PC)
3317 delete_computation (insn);
3320 static rtx
3321 label_before_next_insn (rtx x, rtx scan_limit)
3323 rtx insn = next_active_insn (x);
3324 while (insn)
3326 insn = PREV_INSN (insn);
3327 if (insn == scan_limit || insn == NULL_RTX)
3328 return NULL_RTX;
3329 if (LABEL_P (insn))
3330 break;
3332 return insn;
3336 /* Once we have tried two ways to fill a delay slot, make a pass over the
3337 code to try to improve the results and to do such things as more jump
3338 threading. */
3340 static void
3341 relax_delay_slots (rtx first)
3343 rtx insn, next, pat;
3344 rtx trial, delay_insn, target_label;
3346 /* Look at every JUMP_INSN and see if we can improve it. */
3347 for (insn = first; insn; insn = next)
3349 rtx other;
3351 next = next_active_insn (insn);
3353 /* If this is a jump insn, see if it now jumps to a jump, jumps to
3354 the next insn, or jumps to a label that is not the last of a
3355 group of consecutive labels. */
3356 if (JUMP_P (insn)
3357 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3358 && !ANY_RETURN_P (target_label = JUMP_LABEL (insn)))
3360 target_label = skip_consecutive_labels (follow_jumps (target_label));
3361 if (ANY_RETURN_P (target_label))
3362 target_label = find_end_label (target_label);
3364 if (target_label && next_active_insn (target_label) == next
3365 && ! condjump_in_parallel_p (insn))
3367 delete_jump (insn);
3368 continue;
3371 if (target_label && target_label != JUMP_LABEL (insn))
3372 reorg_redirect_jump (insn, target_label);
3374 /* See if this jump conditionally branches around an unconditional
3375 jump. If so, invert this jump and point it to the target of the
3376 second jump. */
3377 if (next && simplejump_or_return_p (next)
3378 && any_condjump_p (insn)
3379 && target_label
3380 && next_active_insn (target_label) == next_active_insn (next)
3381 && no_labels_between_p (insn, next))
3383 rtx label = JUMP_LABEL (next);
3385 /* Be careful how we do this to avoid deleting code or
3386 labels that are momentarily dead. See similar optimization
3387 in jump.c.
3389 We also need to ensure we properly handle the case when
3390 invert_jump fails. */
3392 ++LABEL_NUSES (target_label);
3393 if (!ANY_RETURN_P (label))
3394 ++LABEL_NUSES (label);
3396 if (invert_jump (insn, label, 1))
3398 delete_related_insns (next);
3399 next = insn;
3402 if (!ANY_RETURN_P (label))
3403 --LABEL_NUSES (label);
3405 if (--LABEL_NUSES (target_label) == 0)
3406 delete_related_insns (target_label);
3408 continue;
3412 /* If this is an unconditional jump and the previous insn is a
3413 conditional jump, try reversing the condition of the previous
3414 insn and swapping our targets. The next pass might be able to
3415 fill the slots.
3417 Don't do this if we expect the conditional branch to be true, because
3418 we would then be making the more common case longer. */
3420 if (simplejump_or_return_p (insn)
3421 && (other = prev_active_insn (insn)) != 0
3422 && any_condjump_p (other)
3423 && no_labels_between_p (other, insn)
3424 && 0 > mostly_true_jump (other,
3425 get_branch_condition (other,
3426 JUMP_LABEL (other))))
3428 rtx other_target = JUMP_LABEL (other);
3429 target_label = JUMP_LABEL (insn);
3431 if (invert_jump (other, target_label, 0))
3432 reorg_redirect_jump (insn, other_target);
3435 /* Now look only at cases where we have filled a delay slot. */
3436 if (!NONJUMP_INSN_P (insn)
3437 || GET_CODE (PATTERN (insn)) != SEQUENCE)
3438 continue;
3440 pat = PATTERN (insn);
3441 delay_insn = XVECEXP (pat, 0, 0);
3443 /* See if the first insn in the delay slot is redundant with some
3444 previous insn. Remove it from the delay slot if so; then set up
3445 to reprocess this insn. */
3446 if (redundant_insn (XVECEXP (pat, 0, 1), delay_insn, 0))
3448 delete_from_delay_slot (XVECEXP (pat, 0, 1));
3449 next = prev_active_insn (next);
3450 continue;
3453 /* See if we have a RETURN insn with a filled delay slot followed
3454 by a RETURN insn with an unfilled a delay slot. If so, we can delete
3455 the first RETURN (but not its delay insn). This gives the same
3456 effect in fewer instructions.
3458 Only do so if optimizing for size since this results in slower, but
3459 smaller code. */
3460 if (optimize_function_for_size_p (cfun)
3461 && ANY_RETURN_P (PATTERN (delay_insn))
3462 && next
3463 && JUMP_P (next)
3464 && PATTERN (next) == PATTERN (delay_insn))
3466 rtx after;
3467 int i;
3469 /* Delete the RETURN and just execute the delay list insns.
3471 We do this by deleting the INSN containing the SEQUENCE, then
3472 re-emitting the insns separately, and then deleting the RETURN.
3473 This allows the count of the jump target to be properly
3474 decremented.
3476 Note that we need to change the INSN_UID of the re-emitted insns
3477 since it is used to hash the insns for mark_target_live_regs and
3478 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3480 Clear the from target bit, since these insns are no longer
3481 in delay slots. */
3482 for (i = 0; i < XVECLEN (pat, 0); i++)
3483 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3485 trial = PREV_INSN (insn);
3486 delete_related_insns (insn);
3487 gcc_assert (GET_CODE (pat) == SEQUENCE);
3488 add_insn_after (delay_insn, trial, NULL);
3489 after = delay_insn;
3490 for (i = 1; i < XVECLEN (pat, 0); i++)
3491 after = emit_copy_of_insn_after (XVECEXP (pat, 0, i), after);
3492 delete_scheduled_jump (delay_insn);
3493 continue;
3496 /* Now look only at the cases where we have a filled JUMP_INSN. */
3497 if (!JUMP_P (XVECEXP (PATTERN (insn), 0, 0))
3498 || ! (condjump_p (XVECEXP (PATTERN (insn), 0, 0))
3499 || condjump_in_parallel_p (XVECEXP (PATTERN (insn), 0, 0))))
3500 continue;
3502 target_label = JUMP_LABEL (delay_insn);
3503 if (target_label && ANY_RETURN_P (target_label))
3504 continue;
3506 /* If this jump goes to another unconditional jump, thread it, but
3507 don't convert a jump into a RETURN here. */
3508 trial = skip_consecutive_labels (follow_jumps (target_label));
3509 if (ANY_RETURN_P (trial))
3510 trial = find_end_label (trial);
3512 if (trial && trial != target_label
3513 && redirect_with_delay_slots_safe_p (delay_insn, trial, insn))
3515 reorg_redirect_jump (delay_insn, trial);
3516 target_label = trial;
3519 /* If the first insn at TARGET_LABEL is redundant with a previous
3520 insn, redirect the jump to the following insn and process again.
3521 We use next_real_insn instead of next_active_insn so we
3522 don't skip USE-markers, or we'll end up with incorrect
3523 liveness info. */
3524 trial = next_real_insn (target_label);
3525 if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3526 && redundant_insn (trial, insn, 0)
3527 && ! can_throw_internal (trial))
3529 /* Figure out where to emit the special USE insn so we don't
3530 later incorrectly compute register live/death info. */
3531 rtx tmp = next_active_insn (trial);
3532 if (tmp == 0)
3533 tmp = find_end_label (simple_return_rtx);
3535 if (tmp)
3537 /* Insert the special USE insn and update dataflow info. */
3538 update_block (trial, tmp);
3540 /* Now emit a label before the special USE insn, and
3541 redirect our jump to the new label. */
3542 target_label = get_label_before (PREV_INSN (tmp));
3543 reorg_redirect_jump (delay_insn, target_label);
3544 next = insn;
3545 continue;
3549 /* Similarly, if it is an unconditional jump with one insn in its
3550 delay list and that insn is redundant, thread the jump. */
3551 if (trial && GET_CODE (PATTERN (trial)) == SEQUENCE
3552 && XVECLEN (PATTERN (trial), 0) == 2
3553 && JUMP_P (XVECEXP (PATTERN (trial), 0, 0))
3554 && simplejump_or_return_p (XVECEXP (PATTERN (trial), 0, 0))
3555 && redundant_insn (XVECEXP (PATTERN (trial), 0, 1), insn, 0))
3557 target_label = JUMP_LABEL (XVECEXP (PATTERN (trial), 0, 0));
3558 if (ANY_RETURN_P (target_label))
3559 target_label = find_end_label (target_label);
3561 if (target_label
3562 && redirect_with_delay_slots_safe_p (delay_insn, target_label,
3563 insn))
3565 reorg_redirect_jump (delay_insn, target_label);
3566 next = insn;
3567 continue;
3571 /* See if we have a simple (conditional) jump that is useless. */
3572 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3573 && ! condjump_in_parallel_p (delay_insn)
3574 && prev_active_insn (target_label) == insn
3575 && ! BARRIER_P (prev_nonnote_insn (target_label))
3576 #ifdef HAVE_cc0
3577 /* If the last insn in the delay slot sets CC0 for some insn,
3578 various code assumes that it is in a delay slot. We could
3579 put it back where it belonged and delete the register notes,
3580 but it doesn't seem worthwhile in this uncommon case. */
3581 && ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3582 REG_CC_USER, NULL_RTX)
3583 #endif
3586 rtx after;
3587 int i;
3589 /* All this insn does is execute its delay list and jump to the
3590 following insn. So delete the jump and just execute the delay
3591 list insns.
3593 We do this by deleting the INSN containing the SEQUENCE, then
3594 re-emitting the insns separately, and then deleting the jump.
3595 This allows the count of the jump target to be properly
3596 decremented.
3598 Note that we need to change the INSN_UID of the re-emitted insns
3599 since it is used to hash the insns for mark_target_live_regs and
3600 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3602 Clear the from target bit, since these insns are no longer
3603 in delay slots. */
3604 for (i = 0; i < XVECLEN (pat, 0); i++)
3605 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3607 trial = PREV_INSN (insn);
3608 delete_related_insns (insn);
3609 gcc_assert (GET_CODE (pat) == SEQUENCE);
3610 add_insn_after (delay_insn, trial, NULL);
3611 after = delay_insn;
3612 for (i = 1; i < XVECLEN (pat, 0); i++)
3613 after = emit_copy_of_insn_after (XVECEXP (pat, 0, i), after);
3614 delete_scheduled_jump (delay_insn);
3615 continue;
3618 /* See if this is an unconditional jump around a single insn which is
3619 identical to the one in its delay slot. In this case, we can just
3620 delete the branch and the insn in its delay slot. */
3621 if (next && NONJUMP_INSN_P (next)
3622 && label_before_next_insn (next, insn) == target_label
3623 && simplejump_p (insn)
3624 && XVECLEN (pat, 0) == 2
3625 && rtx_equal_p (PATTERN (next), PATTERN (XVECEXP (pat, 0, 1))))
3627 delete_related_insns (insn);
3628 continue;
3631 /* See if this jump (with its delay slots) conditionally branches
3632 around an unconditional jump (without delay slots). If so, invert
3633 this jump and point it to the target of the second jump. We cannot
3634 do this for annulled jumps, though. Again, don't convert a jump to
3635 a RETURN here. */
3636 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3637 && any_condjump_p (delay_insn)
3638 && next && simplejump_or_return_p (next)
3639 && next_active_insn (target_label) == next_active_insn (next)
3640 && no_labels_between_p (insn, next))
3642 rtx label = JUMP_LABEL (next);
3643 rtx old_label = JUMP_LABEL (delay_insn);
3645 if (ANY_RETURN_P (label))
3646 label = find_end_label (label);
3648 /* find_end_label can generate a new label. Check this first. */
3649 if (label
3650 && no_labels_between_p (insn, next)
3651 && redirect_with_delay_slots_safe_p (delay_insn, label, insn))
3653 /* Be careful how we do this to avoid deleting code or labels
3654 that are momentarily dead. See similar optimization in
3655 jump.c */
3656 if (old_label)
3657 ++LABEL_NUSES (old_label);
3659 if (invert_jump (delay_insn, label, 1))
3661 int i;
3663 /* Must update the INSN_FROM_TARGET_P bits now that
3664 the branch is reversed, so that mark_target_live_regs
3665 will handle the delay slot insn correctly. */
3666 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
3668 rtx slot = XVECEXP (PATTERN (insn), 0, i);
3669 INSN_FROM_TARGET_P (slot) = ! INSN_FROM_TARGET_P (slot);
3672 delete_related_insns (next);
3673 next = insn;
3676 if (old_label && --LABEL_NUSES (old_label) == 0)
3677 delete_related_insns (old_label);
3678 continue;
3682 /* If we own the thread opposite the way this insn branches, see if we
3683 can merge its delay slots with following insns. */
3684 if (INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3685 && own_thread_p (NEXT_INSN (insn), 0, 1))
3686 try_merge_delay_insns (insn, next);
3687 else if (! INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3688 && own_thread_p (target_label, target_label, 0))
3689 try_merge_delay_insns (insn, next_active_insn (target_label));
3691 /* If we get here, we haven't deleted INSN. But we may have deleted
3692 NEXT, so recompute it. */
3693 next = next_active_insn (insn);
3698 /* Look for filled jumps to the end of function label. We can try to convert
3699 them into RETURN insns if the insns in the delay slot are valid for the
3700 RETURN as well. */
3702 static void
3703 make_return_insns (rtx first)
3705 rtx insn, jump_insn, pat;
3706 rtx real_return_label = function_return_label;
3707 rtx real_simple_return_label = function_simple_return_label;
3708 int slots, i;
3710 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3711 /* If a previous pass filled delay slots in the epilogue, things get a
3712 bit more complicated, as those filler insns would generally (without
3713 data flow analysis) have to be executed after any existing branch
3714 delay slot filler insns. It is also unknown whether such a
3715 transformation would actually be profitable. Note that the existing
3716 code only cares for branches with (some) filled delay slots. */
3717 if (crtl->epilogue_delay_list != NULL)
3718 return;
3719 #endif
3721 /* See if there is a RETURN insn in the function other than the one we
3722 made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3723 into a RETURN to jump to it. */
3724 for (insn = first; insn; insn = NEXT_INSN (insn))
3725 if (JUMP_P (insn) && ANY_RETURN_P (PATTERN (insn)))
3727 rtx t = get_label_before (insn);
3728 if (PATTERN (insn) == ret_rtx)
3729 real_return_label = t;
3730 else
3731 real_simple_return_label = t;
3732 break;
3735 /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3736 was equal to END_OF_FUNCTION_LABEL. */
3737 if (real_return_label)
3738 LABEL_NUSES (real_return_label)++;
3739 if (real_simple_return_label)
3740 LABEL_NUSES (real_simple_return_label)++;
3742 /* Clear the list of insns to fill so we can use it. */
3743 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3745 for (insn = first; insn; insn = NEXT_INSN (insn))
3747 int flags;
3748 rtx kind, real_label;
3750 /* Only look at filled JUMP_INSNs that go to the end of function
3751 label. */
3752 if (!NONJUMP_INSN_P (insn)
3753 || GET_CODE (PATTERN (insn)) != SEQUENCE
3754 || !jump_to_label_p (XVECEXP (PATTERN (insn), 0, 0)))
3755 continue;
3757 if (JUMP_LABEL (XVECEXP (PATTERN (insn), 0, 0)) == function_return_label)
3759 kind = ret_rtx;
3760 real_label = real_return_label;
3762 else if (JUMP_LABEL (XVECEXP (PATTERN (insn), 0, 0))
3763 == function_simple_return_label)
3765 kind = simple_return_rtx;
3766 real_label = real_simple_return_label;
3768 else
3769 continue;
3771 pat = PATTERN (insn);
3772 jump_insn = XVECEXP (pat, 0, 0);
3774 /* If we can't make the jump into a RETURN, try to redirect it to the best
3775 RETURN and go on to the next insn. */
3776 if (!reorg_redirect_jump (jump_insn, kind))
3778 /* Make sure redirecting the jump will not invalidate the delay
3779 slot insns. */
3780 if (redirect_with_delay_slots_safe_p (jump_insn, real_label, insn))
3781 reorg_redirect_jump (jump_insn, real_label);
3782 continue;
3785 /* See if this RETURN can accept the insns current in its delay slot.
3786 It can if it has more or an equal number of slots and the contents
3787 of each is valid. */
3789 flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3790 slots = num_delay_slots (jump_insn);
3791 if (slots >= XVECLEN (pat, 0) - 1)
3793 for (i = 1; i < XVECLEN (pat, 0); i++)
3794 if (! (
3795 #ifdef ANNUL_IFFALSE_SLOTS
3796 (INSN_ANNULLED_BRANCH_P (jump_insn)
3797 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3798 ? eligible_for_annul_false (jump_insn, i - 1,
3799 XVECEXP (pat, 0, i), flags) :
3800 #endif
3801 #ifdef ANNUL_IFTRUE_SLOTS
3802 (INSN_ANNULLED_BRANCH_P (jump_insn)
3803 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3804 ? eligible_for_annul_true (jump_insn, i - 1,
3805 XVECEXP (pat, 0, i), flags) :
3806 #endif
3807 eligible_for_delay (jump_insn, i - 1,
3808 XVECEXP (pat, 0, i), flags)))
3809 break;
3811 else
3812 i = 0;
3814 if (i == XVECLEN (pat, 0))
3815 continue;
3817 /* We have to do something with this insn. If it is an unconditional
3818 RETURN, delete the SEQUENCE and output the individual insns,
3819 followed by the RETURN. Then set things up so we try to find
3820 insns for its delay slots, if it needs some. */
3821 if (ANY_RETURN_P (PATTERN (jump_insn)))
3823 rtx prev = PREV_INSN (insn);
3825 delete_related_insns (insn);
3826 for (i = 1; i < XVECLEN (pat, 0); i++)
3827 prev = emit_insn_after (PATTERN (XVECEXP (pat, 0, i)), prev);
3829 insn = emit_jump_insn_after (PATTERN (jump_insn), prev);
3830 emit_barrier_after (insn);
3832 if (slots)
3833 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3835 else
3836 /* It is probably more efficient to keep this with its current
3837 delay slot as a branch to a RETURN. */
3838 reorg_redirect_jump (jump_insn, real_label);
3841 /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3842 new delay slots we have created. */
3843 if (real_return_label != NULL_RTX && --LABEL_NUSES (real_return_label) == 0)
3844 delete_related_insns (real_return_label);
3845 if (real_simple_return_label != NULL_RTX
3846 && --LABEL_NUSES (real_simple_return_label) == 0)
3847 delete_related_insns (real_simple_return_label);
3849 fill_simple_delay_slots (1);
3850 fill_simple_delay_slots (0);
3853 /* Try to find insns to place in delay slots. */
3855 void
3856 dbr_schedule (rtx first)
3858 rtx insn, next, epilogue_insn = 0;
3859 int i;
3860 bool need_return_insns;
3862 /* If the current function has no insns other than the prologue and
3863 epilogue, then do not try to fill any delay slots. */
3864 if (n_basic_blocks == NUM_FIXED_BLOCKS)
3865 return;
3867 /* Find the highest INSN_UID and allocate and initialize our map from
3868 INSN_UID's to position in code. */
3869 for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3871 if (INSN_UID (insn) > max_uid)
3872 max_uid = INSN_UID (insn);
3873 if (NOTE_P (insn)
3874 && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
3875 epilogue_insn = insn;
3878 uid_to_ruid = XNEWVEC (int, max_uid + 1);
3879 for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3880 uid_to_ruid[INSN_UID (insn)] = i;
3882 /* Initialize the list of insns that need filling. */
3883 if (unfilled_firstobj == 0)
3885 gcc_obstack_init (&unfilled_slots_obstack);
3886 unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3889 for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3891 rtx target;
3893 if (JUMP_P (insn))
3894 INSN_ANNULLED_BRANCH_P (insn) = 0;
3895 INSN_FROM_TARGET_P (insn) = 0;
3897 /* Skip vector tables. We can't get attributes for them. */
3898 if (JUMP_TABLE_DATA_P (insn))
3899 continue;
3901 if (num_delay_slots (insn) > 0)
3902 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3904 /* Ensure all jumps go to the last of a set of consecutive labels. */
3905 if (JUMP_P (insn)
3906 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3907 && !ANY_RETURN_P (JUMP_LABEL (insn))
3908 && ((target = skip_consecutive_labels (JUMP_LABEL (insn)))
3909 != JUMP_LABEL (insn)))
3910 redirect_jump (insn, target, 1);
3913 init_resource_info (epilogue_insn);
3915 /* Show we haven't computed an end-of-function label yet. */
3916 function_return_label = function_simple_return_label = NULL_RTX;
3918 /* Initialize the statistics for this function. */
3919 memset (num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3920 memset (num_filled_delays, 0, sizeof num_filled_delays);
3922 /* Now do the delay slot filling. Try everything twice in case earlier
3923 changes make more slots fillable. */
3925 for (reorg_pass_number = 0;
3926 reorg_pass_number < MAX_REORG_PASSES;
3927 reorg_pass_number++)
3929 fill_simple_delay_slots (1);
3930 fill_simple_delay_slots (0);
3931 fill_eager_delay_slots ();
3932 relax_delay_slots (first);
3935 /* If we made an end of function label, indicate that it is now
3936 safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3937 If it is now unused, delete it. */
3938 if (function_return_label && --LABEL_NUSES (function_return_label) == 0)
3939 delete_related_insns (function_return_label);
3940 if (function_simple_return_label
3941 && --LABEL_NUSES (function_simple_return_label) == 0)
3942 delete_related_insns (function_simple_return_label);
3944 need_return_insns = false;
3945 #ifdef HAVE_return
3946 need_return_insns |= HAVE_return && function_return_label != 0;
3947 #endif
3948 #ifdef HAVE_simple_return
3949 need_return_insns |= HAVE_simple_return && function_simple_return_label != 0;
3950 #endif
3951 if (need_return_insns)
3952 make_return_insns (first);
3954 /* Delete any USE insns made by update_block; subsequent passes don't need
3955 them or know how to deal with them. */
3956 for (insn = first; insn; insn = next)
3958 next = NEXT_INSN (insn);
3960 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
3961 && INSN_P (XEXP (PATTERN (insn), 0)))
3962 next = delete_related_insns (insn);
3965 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3967 /* It is not clear why the line below is needed, but it does seem to be. */
3968 unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3970 if (dump_file)
3972 int i, j, need_comma;
3973 int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3974 int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
3976 for (reorg_pass_number = 0;
3977 reorg_pass_number < MAX_REORG_PASSES;
3978 reorg_pass_number++)
3980 fprintf (dump_file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
3981 for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
3983 need_comma = 0;
3984 fprintf (dump_file, ";; Reorg function #%d\n", i);
3986 fprintf (dump_file, ";; %d insns needing delay slots\n;; ",
3987 num_insns_needing_delays[i][reorg_pass_number]);
3989 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3990 if (num_filled_delays[i][j][reorg_pass_number])
3992 if (need_comma)
3993 fprintf (dump_file, ", ");
3994 need_comma = 1;
3995 fprintf (dump_file, "%d got %d delays",
3996 num_filled_delays[i][j][reorg_pass_number], j);
3998 fprintf (dump_file, "\n");
4001 memset (total_delay_slots, 0, sizeof total_delay_slots);
4002 memset (total_annul_slots, 0, sizeof total_annul_slots);
4003 for (insn = first; insn; insn = NEXT_INSN (insn))
4005 if (! INSN_DELETED_P (insn)
4006 && NONJUMP_INSN_P (insn)
4007 && GET_CODE (PATTERN (insn)) != USE
4008 && GET_CODE (PATTERN (insn)) != CLOBBER)
4010 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
4012 rtx control;
4013 j = XVECLEN (PATTERN (insn), 0) - 1;
4014 if (j > MAX_DELAY_HISTOGRAM)
4015 j = MAX_DELAY_HISTOGRAM;
4016 control = XVECEXP (PATTERN (insn), 0, 0);
4017 if (JUMP_P (control) && INSN_ANNULLED_BRANCH_P (control))
4018 total_annul_slots[j]++;
4019 else
4020 total_delay_slots[j]++;
4022 else if (num_delay_slots (insn) > 0)
4023 total_delay_slots[0]++;
4026 fprintf (dump_file, ";; Reorg totals: ");
4027 need_comma = 0;
4028 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
4030 if (total_delay_slots[j])
4032 if (need_comma)
4033 fprintf (dump_file, ", ");
4034 need_comma = 1;
4035 fprintf (dump_file, "%d got %d delays", total_delay_slots[j], j);
4038 fprintf (dump_file, "\n");
4039 #if defined (ANNUL_IFTRUE_SLOTS) || defined (ANNUL_IFFALSE_SLOTS)
4040 fprintf (dump_file, ";; Reorg annuls: ");
4041 need_comma = 0;
4042 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
4044 if (total_annul_slots[j])
4046 if (need_comma)
4047 fprintf (dump_file, ", ");
4048 need_comma = 1;
4049 fprintf (dump_file, "%d got %d delays", total_annul_slots[j], j);
4052 fprintf (dump_file, "\n");
4053 #endif
4054 fprintf (dump_file, "\n");
4057 free_resource_info ();
4058 free (uid_to_ruid);
4059 #ifdef DELAY_SLOTS_FOR_EPILOGUE
4060 /* SPARC assembler, for instance, emit warning when debug info is output
4061 into the delay slot. */
4063 rtx link;
4065 for (link = crtl->epilogue_delay_list;
4066 link;
4067 link = XEXP (link, 1))
4068 INSN_LOCATOR (XEXP (link, 0)) = 0;
4071 #endif
4072 crtl->dbr_scheduled_p = true;
4074 #endif /* DELAY_SLOTS */
4076 static bool
4077 gate_handle_delay_slots (void)
4079 #ifdef DELAY_SLOTS
4080 /* At -O0 dataflow info isn't updated after RA. */
4081 return optimize > 0 && flag_delayed_branch && !crtl->dbr_scheduled_p;
4082 #else
4083 return 0;
4084 #endif
4087 /* Run delay slot optimization. */
4088 static unsigned int
4089 rest_of_handle_delay_slots (void)
4091 #ifdef DELAY_SLOTS
4092 dbr_schedule (get_insns ());
4093 #endif
4094 return 0;
4097 struct rtl_opt_pass pass_delay_slots =
4100 RTL_PASS,
4101 "dbr", /* name */
4102 gate_handle_delay_slots, /* gate */
4103 rest_of_handle_delay_slots, /* execute */
4104 NULL, /* sub */
4105 NULL, /* next */
4106 0, /* static_pass_number */
4107 TV_DBR_SCHED, /* tv_id */
4108 0, /* properties_required */
4109 0, /* properties_provided */
4110 0, /* properties_destroyed */
4111 0, /* todo_flags_start */
4112 TODO_ggc_collect /* todo_flags_finish */
4116 /* Machine dependent reorg pass. */
4117 static bool
4118 gate_handle_machine_reorg (void)
4120 return targetm.machine_dependent_reorg != 0;
4124 static unsigned int
4125 rest_of_handle_machine_reorg (void)
4127 targetm.machine_dependent_reorg ();
4128 return 0;
4131 struct rtl_opt_pass pass_machine_reorg =
4134 RTL_PASS,
4135 "mach", /* name */
4136 gate_handle_machine_reorg, /* gate */
4137 rest_of_handle_machine_reorg, /* execute */
4138 NULL, /* sub */
4139 NULL, /* next */
4140 0, /* static_pass_number */
4141 TV_MACH_DEP, /* tv_id */
4142 0, /* properties_required */
4143 0, /* properties_provided */
4144 0, /* properties_destroyed */
4145 0, /* todo_flags_start */
4146 TODO_ggc_collect /* todo_flags_finish */