PR c++/11357
[official-gcc.git] / gcc / reorg.c
blob3d7159daa7c9d7be62366c33f26765dff46acc0b
1 /* Perform instruction reorganizations for delay slot filling.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu).
5 Hacked by Michael Tiemann (tiemann@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
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 and AMD 29000 have 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 Motorola 88000 conditionally exposes its branch delay slot,
45 so code is shorter when it is turned off, but will run faster
46 when useful insns are scheduled there.
48 The IBM ROMP has two forms of branch and call insns, both with and
49 without a delay slot. Much like the 88k, insns not using the delay
50 slot can be shorted (2 bytes vs. 4 bytes), but will run slowed.
52 The SPARC always has a branch delay slot, but its effects can be
53 annulled when the branch is not taken. This means that failing to
54 find other sources of insns, we can hoist an insn from the branch
55 target that would only be safe to execute knowing that the branch
56 is taken.
58 The HP-PA always has a branch delay slot. For unconditional branches
59 its effects can be annulled when the branch is taken. The effects
60 of the delay slot in a conditional branch can be nullified for forward
61 taken branches, or for untaken backward branches. This means
62 we can hoist insns from the fall-through path for forward branches or
63 steal insns from the target of backward branches.
65 The TMS320C3x and C4x have three branch delay slots. When the three
66 slots are filled, the branch penalty is zero. Most insns can fill the
67 delay slots except jump insns.
69 Three techniques for filling delay slots have been implemented so far:
71 (1) `fill_simple_delay_slots' is the simplest, most efficient way
72 to fill delay slots. This pass first looks for insns which come
73 from before the branch and which are safe to execute after the
74 branch. Then it searches after the insn requiring delay slots or,
75 in the case of a branch, for insns that are after the point at
76 which the branch merges into the fallthrough code, if such a point
77 exists. When such insns are found, the branch penalty decreases
78 and no code expansion takes place.
80 (2) `fill_eager_delay_slots' is more complicated: it is used for
81 scheduling conditional jumps, or for scheduling jumps which cannot
82 be filled using (1). A machine need not have annulled jumps to use
83 this strategy, but it helps (by keeping more options open).
84 `fill_eager_delay_slots' tries to guess the direction the branch
85 will go; if it guesses right 100% of the time, it can reduce the
86 branch penalty as much as `fill_simple_delay_slots' does. If it
87 guesses wrong 100% of the time, it might as well schedule nops (or
88 on the m88k, unexpose the branch slot). When
89 `fill_eager_delay_slots' takes insns from the fall-through path of
90 the jump, usually there is no code expansion; when it takes insns
91 from the branch target, there is code expansion if it is not the
92 only way to reach that target.
94 (3) `relax_delay_slots' uses a set of rules to simplify code that
95 has been reorganized by (1) and (2). It finds cases where
96 conditional test can be eliminated, jumps can be threaded, extra
97 insns can be eliminated, etc. It is the job of (1) and (2) to do a
98 good job of scheduling locally; `relax_delay_slots' takes care of
99 making the various individual schedules work well together. It is
100 especially tuned to handle the control flow interactions of branch
101 insns. It does nothing for insns with delay slots that do not
102 branch.
104 On machines that use CC0, we are very conservative. We will not make
105 a copy of an insn involving CC0 since we want to maintain a 1-1
106 correspondence between the insn that sets and uses CC0. The insns are
107 allowed to be separated by placing an insn that sets CC0 (but not an insn
108 that uses CC0; we could do this, but it doesn't seem worthwhile) in a
109 delay slot. In that case, we point each insn at the other with REG_CC_USER
110 and REG_CC_SETTER notes. Note that these restrictions affect very few
111 machines because most RISC machines with delay slots will not use CC0
112 (the RT is the only known exception at this point).
114 Not yet implemented:
116 The Acorn Risc Machine can conditionally execute most insns, so
117 it is profitable to move single insns into a position to execute
118 based on the condition code of the previous insn.
120 The HP-PA can conditionally nullify insns, providing a similar
121 effect to the ARM, differing mostly in which insn is "in charge". */
123 #include "config.h"
124 #include "system.h"
125 #include "coretypes.h"
126 #include "tm.h"
127 #include "toplev.h"
128 #include "rtl.h"
129 #include "tm_p.h"
130 #include "expr.h"
131 #include "function.h"
132 #include "insn-config.h"
133 #include "conditions.h"
134 #include "hard-reg-set.h"
135 #include "basic-block.h"
136 #include "regs.h"
137 #include "recog.h"
138 #include "flags.h"
139 #include "output.h"
140 #include "obstack.h"
141 #include "insn-attr.h"
142 #include "resource.h"
143 #include "except.h"
144 #include "params.h"
146 #ifdef DELAY_SLOTS
148 #ifndef ANNUL_IFTRUE_SLOTS
149 #define eligible_for_annul_true(INSN, SLOTS, TRIAL, FLAGS) 0
150 #endif
151 #ifndef ANNUL_IFFALSE_SLOTS
152 #define eligible_for_annul_false(INSN, SLOTS, TRIAL, FLAGS) 0
153 #endif
155 /* Insns which have delay slots that have not yet been filled. */
157 static struct obstack unfilled_slots_obstack;
158 static rtx *unfilled_firstobj;
160 /* Define macros to refer to the first and last slot containing unfilled
161 insns. These are used because the list may move and its address
162 should be recomputed at each use. */
164 #define unfilled_slots_base \
165 ((rtx *) obstack_base (&unfilled_slots_obstack))
167 #define unfilled_slots_next \
168 ((rtx *) obstack_next_free (&unfilled_slots_obstack))
170 /* Points to the label before the end of the function. */
171 static rtx end_of_function_label;
173 /* Mapping between INSN_UID's and position in the code since INSN_UID's do
174 not always monotonically increase. */
175 static int *uid_to_ruid;
177 /* Highest valid index in `uid_to_ruid'. */
178 static int max_uid;
180 static int stop_search_p (rtx, int);
181 static int resource_conflicts_p (struct resources *, struct resources *);
182 static int insn_references_resource_p (rtx, struct resources *, int);
183 static int insn_sets_resource_p (rtx, struct resources *, int);
184 static rtx find_end_label (void);
185 static rtx emit_delay_sequence (rtx, rtx, int);
186 static rtx add_to_delay_list (rtx, rtx);
187 static rtx delete_from_delay_slot (rtx);
188 static void delete_scheduled_jump (rtx);
189 static void note_delay_statistics (int, int);
190 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
191 static rtx optimize_skip (rtx);
192 #endif
193 static int get_jump_flags (rtx, rtx);
194 static int rare_destination (rtx);
195 static int mostly_true_jump (rtx, rtx);
196 static rtx get_branch_condition (rtx, rtx);
197 static int condition_dominates_p (rtx, rtx);
198 static int redirect_with_delay_slots_safe_p (rtx, rtx, rtx);
199 static int redirect_with_delay_list_safe_p (rtx, rtx, rtx);
200 static int check_annul_list_true_false (int, rtx);
201 static rtx steal_delay_list_from_target (rtx, rtx, rtx, rtx,
202 struct resources *,
203 struct resources *,
204 struct resources *,
205 int, int *, int *, rtx *);
206 static rtx steal_delay_list_from_fallthrough (rtx, rtx, rtx, rtx,
207 struct resources *,
208 struct resources *,
209 struct resources *,
210 int, int *, int *);
211 static void try_merge_delay_insns (rtx, rtx);
212 static rtx redundant_insn (rtx, rtx, rtx);
213 static int own_thread_p (rtx, rtx, int);
214 static void update_block (rtx, rtx);
215 static int reorg_redirect_jump (rtx, rtx);
216 static void update_reg_dead_notes (rtx, rtx);
217 static void fix_reg_dead_note (rtx, rtx);
218 static void update_reg_unused_notes (rtx, rtx);
219 static void fill_simple_delay_slots (int);
220 static rtx fill_slots_from_thread (rtx, rtx, rtx, rtx, int, int, int, int,
221 int *, rtx);
222 static void fill_eager_delay_slots (void);
223 static void relax_delay_slots (rtx);
224 #ifdef HAVE_return
225 static void make_return_insns (rtx);
226 #endif
228 /* Return TRUE if this insn should stop the search for insn to fill delay
229 slots. LABELS_P indicates that labels should terminate the search.
230 In all cases, jumps terminate the search. */
232 static int
233 stop_search_p (rtx insn, int labels_p)
235 if (insn == 0)
236 return 1;
238 switch (GET_CODE (insn))
240 case NOTE:
241 case CALL_INSN:
242 return 0;
244 case CODE_LABEL:
245 return labels_p;
247 case JUMP_INSN:
248 case BARRIER:
249 return 1;
251 case INSN:
252 /* OK unless it contains a delay slot or is an `asm' insn of some type.
253 We don't know anything about these. */
254 return (GET_CODE (PATTERN (insn)) == SEQUENCE
255 || GET_CODE (PATTERN (insn)) == ASM_INPUT
256 || asm_noperands (PATTERN (insn)) >= 0);
258 default:
259 abort ();
263 /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
264 resource set contains a volatile memory reference. Otherwise, return FALSE. */
266 static int
267 resource_conflicts_p (struct resources *res1, struct resources *res2)
269 if ((res1->cc && res2->cc) || (res1->memory && res2->memory)
270 || (res1->unch_memory && res2->unch_memory)
271 || res1->volatil || res2->volatil)
272 return 1;
274 #ifdef HARD_REG_SET
275 return (res1->regs & res2->regs) != HARD_CONST (0);
276 #else
278 int i;
280 for (i = 0; i < HARD_REG_SET_LONGS; i++)
281 if ((res1->regs[i] & res2->regs[i]) != 0)
282 return 1;
283 return 0;
285 #endif
288 /* Return TRUE if any resource marked in RES, a `struct resources', is
289 referenced by INSN. If INCLUDE_DELAYED_EFFECTS is set, return if the called
290 routine is using those resources.
292 We compute this by computing all the resources referenced by INSN and
293 seeing if this conflicts with RES. It might be faster to directly check
294 ourselves, and this is the way it used to work, but it means duplicating
295 a large block of complex code. */
297 static int
298 insn_references_resource_p (rtx insn, struct resources *res,
299 int include_delayed_effects)
301 struct resources insn_res;
303 CLEAR_RESOURCE (&insn_res);
304 mark_referenced_resources (insn, &insn_res, include_delayed_effects);
305 return resource_conflicts_p (&insn_res, res);
308 /* Return TRUE if INSN modifies resources that are marked in RES.
309 INCLUDE_DELAYED_EFFECTS is set if the actions of that routine should be
310 included. CC0 is only modified if it is explicitly set; see comments
311 in front of mark_set_resources for details. */
313 static int
314 insn_sets_resource_p (rtx insn, struct resources *res,
315 int include_delayed_effects)
317 struct resources insn_sets;
319 CLEAR_RESOURCE (&insn_sets);
320 mark_set_resources (insn, &insn_sets, 0, include_delayed_effects);
321 return resource_conflicts_p (&insn_sets, res);
324 /* Find a label at the end of the function or before a RETURN. If there is
325 none, make one. */
327 static rtx
328 find_end_label (void)
330 rtx insn;
332 /* If we found one previously, return it. */
333 if (end_of_function_label)
334 return end_of_function_label;
336 /* Otherwise, see if there is a label at the end of the function. If there
337 is, it must be that RETURN insns aren't needed, so that is our return
338 label and we don't have to do anything else. */
340 insn = get_last_insn ();
341 while (GET_CODE (insn) == NOTE
342 || (GET_CODE (insn) == INSN
343 && (GET_CODE (PATTERN (insn)) == USE
344 || GET_CODE (PATTERN (insn)) == CLOBBER)))
345 insn = PREV_INSN (insn);
347 /* When a target threads its epilogue we might already have a
348 suitable return insn. If so put a label before it for the
349 end_of_function_label. */
350 if (GET_CODE (insn) == BARRIER
351 && GET_CODE (PREV_INSN (insn)) == JUMP_INSN
352 && GET_CODE (PATTERN (PREV_INSN (insn))) == RETURN)
354 rtx temp = PREV_INSN (PREV_INSN (insn));
355 end_of_function_label = gen_label_rtx ();
356 LABEL_NUSES (end_of_function_label) = 0;
358 /* Put the label before an USE insns that may proceed the RETURN insn. */
359 while (GET_CODE (temp) == USE)
360 temp = PREV_INSN (temp);
362 emit_label_after (end_of_function_label, temp);
365 else if (GET_CODE (insn) == CODE_LABEL)
366 end_of_function_label = insn;
367 else
369 end_of_function_label = gen_label_rtx ();
370 LABEL_NUSES (end_of_function_label) = 0;
371 /* If the basic block reorder pass moves the return insn to
372 some other place try to locate it again and put our
373 end_of_function_label there. */
374 while (insn && ! (GET_CODE (insn) == JUMP_INSN
375 && (GET_CODE (PATTERN (insn)) == RETURN)))
376 insn = PREV_INSN (insn);
377 if (insn)
379 insn = PREV_INSN (insn);
381 /* Put the label before an USE insns that may proceed the
382 RETURN insn. */
383 while (GET_CODE (insn) == USE)
384 insn = PREV_INSN (insn);
386 emit_label_after (end_of_function_label, insn);
388 else
390 /* Otherwise, make a new label and emit a RETURN and BARRIER,
391 if needed. */
392 emit_label (end_of_function_label);
393 #ifdef HAVE_return
394 if (HAVE_return)
396 /* The return we make may have delay slots too. */
397 rtx insn = gen_return ();
398 insn = emit_jump_insn (insn);
399 emit_barrier ();
400 if (num_delay_slots (insn) > 0)
401 obstack_ptr_grow (&unfilled_slots_obstack, insn);
403 #endif
407 /* Show one additional use for this label so it won't go away until
408 we are done. */
409 ++LABEL_NUSES (end_of_function_label);
411 return end_of_function_label;
414 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
415 the pattern of INSN with the SEQUENCE.
417 Chain the insns so that NEXT_INSN of each insn in the sequence points to
418 the next and NEXT_INSN of the last insn in the sequence points to
419 the first insn after the sequence. Similarly for PREV_INSN. This makes
420 it easier to scan all insns.
422 Returns the SEQUENCE that replaces INSN. */
424 static rtx
425 emit_delay_sequence (rtx insn, rtx list, int length)
427 int i = 1;
428 rtx li;
429 int had_barrier = 0;
431 /* Allocate the rtvec to hold the insns and the SEQUENCE. */
432 rtvec seqv = rtvec_alloc (length + 1);
433 rtx seq = gen_rtx_SEQUENCE (VOIDmode, seqv);
434 rtx seq_insn = make_insn_raw (seq);
435 rtx first = get_insns ();
436 rtx last = get_last_insn ();
438 /* Make a copy of the insn having delay slots. */
439 rtx delay_insn = copy_rtx (insn);
441 /* If INSN is followed by a BARRIER, delete the BARRIER since it will only
442 confuse further processing. Update LAST in case it was the last insn.
443 We will put the BARRIER back in later. */
444 if (NEXT_INSN (insn) && GET_CODE (NEXT_INSN (insn)) == BARRIER)
446 delete_related_insns (NEXT_INSN (insn));
447 last = get_last_insn ();
448 had_barrier = 1;
451 /* Splice our SEQUENCE into the insn stream where INSN used to be. */
452 NEXT_INSN (seq_insn) = NEXT_INSN (insn);
453 PREV_INSN (seq_insn) = PREV_INSN (insn);
455 if (insn != last)
456 PREV_INSN (NEXT_INSN (seq_insn)) = seq_insn;
458 if (insn != first)
459 NEXT_INSN (PREV_INSN (seq_insn)) = seq_insn;
461 /* Note the calls to set_new_first_and_last_insn must occur after
462 SEQ_INSN has been completely spliced into the insn stream.
464 Otherwise CUR_INSN_UID will get set to an incorrect value because
465 set_new_first_and_last_insn will not find SEQ_INSN in the chain. */
466 if (insn == last)
467 set_new_first_and_last_insn (first, seq_insn);
469 if (insn == first)
470 set_new_first_and_last_insn (seq_insn, last);
472 /* Build our SEQUENCE and rebuild the insn chain. */
473 XVECEXP (seq, 0, 0) = delay_insn;
474 INSN_DELETED_P (delay_insn) = 0;
475 PREV_INSN (delay_insn) = PREV_INSN (seq_insn);
477 for (li = list; li; li = XEXP (li, 1), i++)
479 rtx tem = XEXP (li, 0);
480 rtx note, next;
482 /* Show that this copy of the insn isn't deleted. */
483 INSN_DELETED_P (tem) = 0;
485 XVECEXP (seq, 0, i) = tem;
486 PREV_INSN (tem) = XVECEXP (seq, 0, i - 1);
487 NEXT_INSN (XVECEXP (seq, 0, i - 1)) = tem;
489 /* SPARC assembler, for instance, emit warning when debug info is output
490 into the delay slot. */
491 if (INSN_LOCATOR (tem) && !INSN_LOCATOR (seq_insn))
492 INSN_LOCATOR (seq_insn) = INSN_LOCATOR (tem);
493 INSN_LOCATOR (tem) = 0;
495 for (note = REG_NOTES (tem); note; note = next)
497 next = XEXP (note, 1);
498 switch (REG_NOTE_KIND (note))
500 case REG_DEAD:
501 /* Remove any REG_DEAD notes because we can't rely on them now
502 that the insn has been moved. */
503 remove_note (tem, note);
504 break;
506 case REG_LABEL:
507 /* Keep the label reference count up to date. */
508 if (GET_CODE (XEXP (note, 0)) == CODE_LABEL)
509 LABEL_NUSES (XEXP (note, 0)) ++;
510 break;
512 default:
513 break;
518 NEXT_INSN (XVECEXP (seq, 0, length)) = NEXT_INSN (seq_insn);
520 /* If the previous insn is a SEQUENCE, update the NEXT_INSN pointer on the
521 last insn in that SEQUENCE to point to us. Similarly for the first
522 insn in the following insn if it is a SEQUENCE. */
524 if (PREV_INSN (seq_insn) && GET_CODE (PREV_INSN (seq_insn)) == INSN
525 && GET_CODE (PATTERN (PREV_INSN (seq_insn))) == SEQUENCE)
526 NEXT_INSN (XVECEXP (PATTERN (PREV_INSN (seq_insn)), 0,
527 XVECLEN (PATTERN (PREV_INSN (seq_insn)), 0) - 1))
528 = seq_insn;
530 if (NEXT_INSN (seq_insn) && GET_CODE (NEXT_INSN (seq_insn)) == INSN
531 && GET_CODE (PATTERN (NEXT_INSN (seq_insn))) == SEQUENCE)
532 PREV_INSN (XVECEXP (PATTERN (NEXT_INSN (seq_insn)), 0, 0)) = seq_insn;
534 /* If there used to be a BARRIER, put it back. */
535 if (had_barrier)
536 emit_barrier_after (seq_insn);
538 if (i != length + 1)
539 abort ();
541 return seq_insn;
544 /* Add INSN to DELAY_LIST and return the head of the new list. The list must
545 be in the order in which the insns are to be executed. */
547 static rtx
548 add_to_delay_list (rtx insn, rtx delay_list)
550 /* If we have an empty list, just make a new list element. If
551 INSN has its block number recorded, clear it since we may
552 be moving the insn to a new block. */
554 if (delay_list == 0)
556 clear_hashed_info_for_insn (insn);
557 return gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
560 /* Otherwise this must be an INSN_LIST. Add INSN to the end of the
561 list. */
562 XEXP (delay_list, 1) = add_to_delay_list (insn, XEXP (delay_list, 1));
564 return delay_list;
567 /* Delete INSN from the delay slot of the insn that it is in, which may
568 produce an insn with no delay slots. Return the new insn. */
570 static rtx
571 delete_from_delay_slot (rtx insn)
573 rtx trial, seq_insn, seq, prev;
574 rtx delay_list = 0;
575 int i;
577 /* We first must find the insn containing the SEQUENCE with INSN in its
578 delay slot. Do this by finding an insn, TRIAL, where
579 PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
581 for (trial = insn;
582 PREV_INSN (NEXT_INSN (trial)) == trial;
583 trial = NEXT_INSN (trial))
586 seq_insn = PREV_INSN (NEXT_INSN (trial));
587 seq = PATTERN (seq_insn);
589 /* Create a delay list consisting of all the insns other than the one
590 we are deleting (unless we were the only one). */
591 if (XVECLEN (seq, 0) > 2)
592 for (i = 1; i < XVECLEN (seq, 0); i++)
593 if (XVECEXP (seq, 0, i) != insn)
594 delay_list = add_to_delay_list (XVECEXP (seq, 0, i), delay_list);
596 /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
597 list, and rebuild the delay list if non-empty. */
598 prev = PREV_INSN (seq_insn);
599 trial = XVECEXP (seq, 0, 0);
600 delete_related_insns (seq_insn);
601 add_insn_after (trial, prev);
603 if (GET_CODE (trial) == JUMP_INSN
604 && (simplejump_p (trial) || GET_CODE (PATTERN (trial)) == RETURN))
605 emit_barrier_after (trial);
607 /* If there are any delay insns, remit them. Otherwise clear the
608 annul flag. */
609 if (delay_list)
610 trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2);
611 else if (GET_CODE (trial) == JUMP_INSN
612 || GET_CODE (trial) == CALL_INSN
613 || GET_CODE (trial) == INSN)
614 INSN_ANNULLED_BRANCH_P (trial) = 0;
616 INSN_FROM_TARGET_P (insn) = 0;
618 /* Show we need to fill this insn again. */
619 obstack_ptr_grow (&unfilled_slots_obstack, trial);
621 return trial;
624 /* Delete INSN, a JUMP_INSN. If it is a conditional jump, we must track down
625 the insn that sets CC0 for it and delete it too. */
627 static void
628 delete_scheduled_jump (rtx insn)
630 /* Delete the insn that sets cc0 for us. On machines without cc0, we could
631 delete the insn that sets the condition code, but it is hard to find it.
632 Since this case is rare anyway, don't bother trying; there would likely
633 be other insns that became dead anyway, which we wouldn't know to
634 delete. */
636 #ifdef HAVE_cc0
637 if (reg_mentioned_p (cc0_rtx, insn))
639 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
641 /* If a reg-note was found, it points to an insn to set CC0. This
642 insn is in the delay list of some other insn. So delete it from
643 the delay list it was in. */
644 if (note)
646 if (! FIND_REG_INC_NOTE (XEXP (note, 0), NULL_RTX)
647 && sets_cc0_p (PATTERN (XEXP (note, 0))) == 1)
648 delete_from_delay_slot (XEXP (note, 0));
650 else
652 /* The insn setting CC0 is our previous insn, but it may be in
653 a delay slot. It will be the last insn in the delay slot, if
654 it is. */
655 rtx trial = previous_insn (insn);
656 if (GET_CODE (trial) == NOTE)
657 trial = prev_nonnote_insn (trial);
658 if (sets_cc0_p (PATTERN (trial)) != 1
659 || FIND_REG_INC_NOTE (trial, NULL_RTX))
660 return;
661 if (PREV_INSN (NEXT_INSN (trial)) == trial)
662 delete_related_insns (trial);
663 else
664 delete_from_delay_slot (trial);
667 #endif
669 delete_related_insns (insn);
672 /* Counters for delay-slot filling. */
674 #define NUM_REORG_FUNCTIONS 2
675 #define MAX_DELAY_HISTOGRAM 3
676 #define MAX_REORG_PASSES 2
678 static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
680 static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
682 static int reorg_pass_number;
684 static void
685 note_delay_statistics (int slots_filled, int index)
687 num_insns_needing_delays[index][reorg_pass_number]++;
688 if (slots_filled > MAX_DELAY_HISTOGRAM)
689 slots_filled = MAX_DELAY_HISTOGRAM;
690 num_filled_delays[index][slots_filled][reorg_pass_number]++;
693 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
695 /* Optimize the following cases:
697 1. When a conditional branch skips over only one instruction,
698 use an annulling branch and put that insn in the delay slot.
699 Use either a branch that annuls when the condition if true or
700 invert the test with a branch that annuls when the condition is
701 false. This saves insns, since otherwise we must copy an insn
702 from the L1 target.
704 (orig) (skip) (otherwise)
705 Bcc.n L1 Bcc',a L1 Bcc,a L1'
706 insn insn insn2
707 L1: L1: L1:
708 insn2 insn2 insn2
709 insn3 insn3 L1':
710 insn3
712 2. When a conditional branch skips over only one instruction,
713 and after that, it unconditionally branches somewhere else,
714 perform the similar optimization. This saves executing the
715 second branch in the case where the inverted condition is true.
717 Bcc.n L1 Bcc',a L2
718 insn insn
719 L1: L1:
720 Bra L2 Bra L2
722 INSN is a JUMP_INSN.
724 This should be expanded to skip over N insns, where N is the number
725 of delay slots required. */
727 static rtx
728 optimize_skip (rtx insn)
730 rtx trial = next_nonnote_insn (insn);
731 rtx next_trial = next_active_insn (trial);
732 rtx delay_list = 0;
733 rtx target_label;
734 int flags;
736 flags = get_jump_flags (insn, JUMP_LABEL (insn));
738 if (trial == 0
739 || GET_CODE (trial) != INSN
740 || GET_CODE (PATTERN (trial)) == SEQUENCE
741 || recog_memoized (trial) < 0
742 || (! eligible_for_annul_false (insn, 0, trial, flags)
743 && ! eligible_for_annul_true (insn, 0, trial, flags))
744 || can_throw_internal (trial))
745 return 0;
747 /* There are two cases where we are just executing one insn (we assume
748 here that a branch requires only one insn; this should be generalized
749 at some point): Where the branch goes around a single insn or where
750 we have one insn followed by a branch to the same label we branch to.
751 In both of these cases, inverting the jump and annulling the delay
752 slot give the same effect in fewer insns. */
753 if ((next_trial == next_active_insn (JUMP_LABEL (insn))
754 && ! (next_trial == 0 && current_function_epilogue_delay_list != 0))
755 || (next_trial != 0
756 && GET_CODE (next_trial) == JUMP_INSN
757 && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)
758 && (simplejump_p (next_trial)
759 || GET_CODE (PATTERN (next_trial)) == RETURN)))
761 if (eligible_for_annul_false (insn, 0, trial, flags))
763 if (invert_jump (insn, JUMP_LABEL (insn), 1))
764 INSN_FROM_TARGET_P (trial) = 1;
765 else if (! eligible_for_annul_true (insn, 0, trial, flags))
766 return 0;
769 delay_list = add_to_delay_list (trial, NULL_RTX);
770 next_trial = next_active_insn (trial);
771 update_block (trial, trial);
772 delete_related_insns (trial);
774 /* Also, if we are targeting an unconditional
775 branch, thread our jump to the target of that branch. Don't
776 change this into a RETURN here, because it may not accept what
777 we have in the delay slot. We'll fix this up later. */
778 if (next_trial && GET_CODE (next_trial) == JUMP_INSN
779 && (simplejump_p (next_trial)
780 || GET_CODE (PATTERN (next_trial)) == RETURN))
782 target_label = JUMP_LABEL (next_trial);
783 if (target_label == 0)
784 target_label = find_end_label ();
786 /* Recompute the flags based on TARGET_LABEL since threading
787 the jump to TARGET_LABEL may change the direction of the
788 jump (which may change the circumstances in which the
789 delay slot is nullified). */
790 flags = get_jump_flags (insn, target_label);
791 if (eligible_for_annul_true (insn, 0, trial, flags))
792 reorg_redirect_jump (insn, target_label);
795 INSN_ANNULLED_BRANCH_P (insn) = 1;
798 return delay_list;
800 #endif
802 /* Encode and return branch direction and prediction information for
803 INSN assuming it will jump to LABEL.
805 Non conditional branches return no direction information and
806 are predicted as very likely taken. */
808 static int
809 get_jump_flags (rtx insn, rtx label)
811 int flags;
813 /* get_jump_flags can be passed any insn with delay slots, these may
814 be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
815 direction information, and only if they are conditional jumps.
817 If LABEL is zero, then there is no way to determine the branch
818 direction. */
819 if (GET_CODE (insn) == JUMP_INSN
820 && (condjump_p (insn) || condjump_in_parallel_p (insn))
821 && INSN_UID (insn) <= max_uid
822 && label != 0
823 && INSN_UID (label) <= max_uid)
824 flags
825 = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
826 ? ATTR_FLAG_forward : ATTR_FLAG_backward;
827 /* No valid direction information. */
828 else
829 flags = 0;
831 /* If insn is a conditional branch call mostly_true_jump to get
832 determine the branch prediction.
834 Non conditional branches are predicted as very likely taken. */
835 if (GET_CODE (insn) == JUMP_INSN
836 && (condjump_p (insn) || condjump_in_parallel_p (insn)))
838 int prediction;
840 prediction = mostly_true_jump (insn, get_branch_condition (insn, label));
841 switch (prediction)
843 case 2:
844 flags |= (ATTR_FLAG_very_likely | ATTR_FLAG_likely);
845 break;
846 case 1:
847 flags |= ATTR_FLAG_likely;
848 break;
849 case 0:
850 flags |= ATTR_FLAG_unlikely;
851 break;
852 case -1:
853 flags |= (ATTR_FLAG_very_unlikely | ATTR_FLAG_unlikely);
854 break;
856 default:
857 abort ();
860 else
861 flags |= (ATTR_FLAG_very_likely | ATTR_FLAG_likely);
863 return flags;
866 /* Return 1 if INSN is a destination that will be branched to rarely (the
867 return point of a function); return 2 if DEST will be branched to very
868 rarely (a call to a function that doesn't return). Otherwise,
869 return 0. */
871 static int
872 rare_destination (rtx insn)
874 int jump_count = 0;
875 rtx next;
877 for (; insn; insn = next)
879 if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
880 insn = XVECEXP (PATTERN (insn), 0, 0);
882 next = NEXT_INSN (insn);
884 switch (GET_CODE (insn))
886 case CODE_LABEL:
887 return 0;
888 case BARRIER:
889 /* A BARRIER can either be after a JUMP_INSN or a CALL_INSN. We
890 don't scan past JUMP_INSNs, so any barrier we find here must
891 have been after a CALL_INSN and hence mean the call doesn't
892 return. */
893 return 2;
894 case JUMP_INSN:
895 if (GET_CODE (PATTERN (insn)) == RETURN)
896 return 1;
897 else if (simplejump_p (insn)
898 && jump_count++ < 10)
899 next = JUMP_LABEL (insn);
900 else
901 return 0;
903 default:
904 break;
908 /* If we got here it means we hit the end of the function. So this
909 is an unlikely destination. */
911 return 1;
914 /* Return truth value of the statement that this branch
915 is mostly taken. If we think that the branch is extremely likely
916 to be taken, we return 2. If the branch is slightly more likely to be
917 taken, return 1. If the branch is slightly less likely to be taken,
918 return 0 and if the branch is highly unlikely to be taken, return -1.
920 CONDITION, if nonzero, is the condition that JUMP_INSN is testing. */
922 static int
923 mostly_true_jump (rtx jump_insn, rtx condition)
925 rtx target_label = JUMP_LABEL (jump_insn);
926 rtx insn, note;
927 int rare_dest = rare_destination (target_label);
928 int rare_fallthrough = rare_destination (NEXT_INSN (jump_insn));
930 /* If branch probabilities are available, then use that number since it
931 always gives a correct answer. */
932 note = find_reg_note (jump_insn, REG_BR_PROB, 0);
933 if (note)
935 int prob = INTVAL (XEXP (note, 0));
937 if (prob >= REG_BR_PROB_BASE * 9 / 10)
938 return 2;
939 else if (prob >= REG_BR_PROB_BASE / 2)
940 return 1;
941 else if (prob >= REG_BR_PROB_BASE / 10)
942 return 0;
943 else
944 return -1;
947 /* ??? Ought to use estimate_probability instead. */
949 /* If this is a branch outside a loop, it is highly unlikely. */
950 if (GET_CODE (PATTERN (jump_insn)) == SET
951 && GET_CODE (SET_SRC (PATTERN (jump_insn))) == IF_THEN_ELSE
952 && ((GET_CODE (XEXP (SET_SRC (PATTERN (jump_insn)), 1)) == LABEL_REF
953 && LABEL_OUTSIDE_LOOP_P (XEXP (SET_SRC (PATTERN (jump_insn)), 1)))
954 || (GET_CODE (XEXP (SET_SRC (PATTERN (jump_insn)), 2)) == LABEL_REF
955 && LABEL_OUTSIDE_LOOP_P (XEXP (SET_SRC (PATTERN (jump_insn)), 2)))))
956 return -1;
958 if (target_label)
960 /* If this is the test of a loop, it is very likely true. We scan
961 backwards from the target label. If we find a NOTE_INSN_LOOP_BEG
962 before the next real insn, we assume the branch is to the top of
963 the loop. */
964 for (insn = PREV_INSN (target_label);
965 insn && GET_CODE (insn) == NOTE;
966 insn = PREV_INSN (insn))
967 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
968 return 2;
970 /* If this is a jump to the test of a loop, it is likely true. We scan
971 forwards from the target label. If we find a NOTE_INSN_LOOP_VTOP
972 before the next real insn, we assume the branch is to the loop branch
973 test. */
974 for (insn = NEXT_INSN (target_label);
975 insn && GET_CODE (insn) == NOTE;
976 insn = PREV_INSN (insn))
977 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP)
978 return 1;
981 /* Look at the relative rarities of the fallthrough and destination. If
982 they differ, we can predict the branch that way. */
984 switch (rare_fallthrough - rare_dest)
986 case -2:
987 return -1;
988 case -1:
989 return 0;
990 case 0:
991 break;
992 case 1:
993 return 1;
994 case 2:
995 return 2;
998 /* If we couldn't figure out what this jump was, assume it won't be
999 taken. This should be rare. */
1000 if (condition == 0)
1001 return 0;
1003 /* EQ tests are usually false and NE tests are usually true. Also,
1004 most quantities are positive, so we can make the appropriate guesses
1005 about signed comparisons against zero. */
1006 switch (GET_CODE (condition))
1008 case CONST_INT:
1009 /* Unconditional branch. */
1010 return 1;
1011 case EQ:
1012 return 0;
1013 case NE:
1014 return 1;
1015 case LE:
1016 case LT:
1017 if (XEXP (condition, 1) == const0_rtx)
1018 return 0;
1019 break;
1020 case GE:
1021 case GT:
1022 if (XEXP (condition, 1) == const0_rtx)
1023 return 1;
1024 break;
1026 default:
1027 break;
1030 /* Predict backward branches usually take, forward branches usually not. If
1031 we don't know whether this is forward or backward, assume the branch
1032 will be taken, since most are. */
1033 return (target_label == 0 || INSN_UID (jump_insn) > max_uid
1034 || INSN_UID (target_label) > max_uid
1035 || (uid_to_ruid[INSN_UID (jump_insn)]
1036 > uid_to_ruid[INSN_UID (target_label)]));
1039 /* Return the condition under which INSN will branch to TARGET. If TARGET
1040 is zero, return the condition under which INSN will return. If INSN is
1041 an unconditional branch, return const_true_rtx. If INSN isn't a simple
1042 type of jump, or it doesn't go to TARGET, return 0. */
1044 static rtx
1045 get_branch_condition (rtx insn, rtx target)
1047 rtx pat = PATTERN (insn);
1048 rtx src;
1050 if (condjump_in_parallel_p (insn))
1051 pat = XVECEXP (pat, 0, 0);
1053 if (GET_CODE (pat) == RETURN)
1054 return target == 0 ? const_true_rtx : 0;
1056 else if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
1057 return 0;
1059 src = SET_SRC (pat);
1060 if (GET_CODE (src) == LABEL_REF && XEXP (src, 0) == target)
1061 return const_true_rtx;
1063 else if (GET_CODE (src) == IF_THEN_ELSE
1064 && ((target == 0 && GET_CODE (XEXP (src, 1)) == RETURN)
1065 || (GET_CODE (XEXP (src, 1)) == LABEL_REF
1066 && XEXP (XEXP (src, 1), 0) == target))
1067 && XEXP (src, 2) == pc_rtx)
1068 return XEXP (src, 0);
1070 else if (GET_CODE (src) == IF_THEN_ELSE
1071 && ((target == 0 && GET_CODE (XEXP (src, 2)) == RETURN)
1072 || (GET_CODE (XEXP (src, 2)) == LABEL_REF
1073 && XEXP (XEXP (src, 2), 0) == target))
1074 && XEXP (src, 1) == pc_rtx)
1076 enum rtx_code rev;
1077 rev = reversed_comparison_code (XEXP (src, 0), insn);
1078 if (rev != UNKNOWN)
1079 return gen_rtx_fmt_ee (rev, GET_MODE (XEXP (src, 0)),
1080 XEXP (XEXP (src, 0), 0),
1081 XEXP (XEXP (src, 0), 1));
1084 return 0;
1087 /* Return nonzero if CONDITION is more strict than the condition of
1088 INSN, i.e., if INSN will always branch if CONDITION is true. */
1090 static int
1091 condition_dominates_p (rtx condition, rtx insn)
1093 rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
1094 enum rtx_code code = GET_CODE (condition);
1095 enum rtx_code other_code;
1097 if (rtx_equal_p (condition, other_condition)
1098 || other_condition == const_true_rtx)
1099 return 1;
1101 else if (condition == const_true_rtx || other_condition == 0)
1102 return 0;
1104 other_code = GET_CODE (other_condition);
1105 if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
1106 || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
1107 || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
1108 return 0;
1110 return comparison_dominates_p (code, other_code);
1113 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1114 any insns already in the delay slot of JUMP. */
1116 static int
1117 redirect_with_delay_slots_safe_p (rtx jump, rtx newlabel, rtx seq)
1119 int flags, i;
1120 rtx pat = PATTERN (seq);
1122 /* Make sure all the delay slots of this jump would still
1123 be valid after threading the jump. If they are still
1124 valid, then return nonzero. */
1126 flags = get_jump_flags (jump, newlabel);
1127 for (i = 1; i < XVECLEN (pat, 0); i++)
1128 if (! (
1129 #ifdef ANNUL_IFFALSE_SLOTS
1130 (INSN_ANNULLED_BRANCH_P (jump)
1131 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1132 ? eligible_for_annul_false (jump, i - 1,
1133 XVECEXP (pat, 0, i), flags) :
1134 #endif
1135 #ifdef ANNUL_IFTRUE_SLOTS
1136 (INSN_ANNULLED_BRANCH_P (jump)
1137 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1138 ? eligible_for_annul_true (jump, i - 1,
1139 XVECEXP (pat, 0, i), flags) :
1140 #endif
1141 eligible_for_delay (jump, i - 1, XVECEXP (pat, 0, i), flags)))
1142 break;
1144 return (i == XVECLEN (pat, 0));
1147 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1148 any insns we wish to place in the delay slot of JUMP. */
1150 static int
1151 redirect_with_delay_list_safe_p (rtx jump, rtx newlabel, rtx delay_list)
1153 int flags, i;
1154 rtx li;
1156 /* Make sure all the insns in DELAY_LIST would still be
1157 valid after threading the jump. If they are still
1158 valid, then return nonzero. */
1160 flags = get_jump_flags (jump, newlabel);
1161 for (li = delay_list, i = 0; li; li = XEXP (li, 1), i++)
1162 if (! (
1163 #ifdef ANNUL_IFFALSE_SLOTS
1164 (INSN_ANNULLED_BRANCH_P (jump)
1165 && INSN_FROM_TARGET_P (XEXP (li, 0)))
1166 ? eligible_for_annul_false (jump, i, XEXP (li, 0), flags) :
1167 #endif
1168 #ifdef ANNUL_IFTRUE_SLOTS
1169 (INSN_ANNULLED_BRANCH_P (jump)
1170 && ! INSN_FROM_TARGET_P (XEXP (li, 0)))
1171 ? eligible_for_annul_true (jump, i, XEXP (li, 0), flags) :
1172 #endif
1173 eligible_for_delay (jump, i, XEXP (li, 0), flags)))
1174 break;
1176 return (li == NULL);
1179 /* DELAY_LIST is a list of insns that have already been placed into delay
1180 slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
1181 If not, return 0; otherwise return 1. */
1183 static int
1184 check_annul_list_true_false (int annul_true_p, rtx delay_list)
1186 rtx temp;
1188 if (delay_list)
1190 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1192 rtx trial = XEXP (temp, 0);
1194 if ((annul_true_p && INSN_FROM_TARGET_P (trial))
1195 || (!annul_true_p && !INSN_FROM_TARGET_P (trial)))
1196 return 0;
1200 return 1;
1203 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
1204 the condition tested by INSN is CONDITION and the resources shown in
1205 OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1206 from SEQ's delay list, in addition to whatever insns it may execute
1207 (in DELAY_LIST). SETS and NEEDED are denote resources already set and
1208 needed while searching for delay slot insns. Return the concatenated
1209 delay list if possible, otherwise, return 0.
1211 SLOTS_TO_FILL is the total number of slots required by INSN, and
1212 PSLOTS_FILLED points to the number filled so far (also the number of
1213 insns in DELAY_LIST). It is updated with the number that have been
1214 filled from the SEQUENCE, if any.
1216 PANNUL_P points to a nonzero value if we already know that we need
1217 to annul INSN. If this routine determines that annulling is needed,
1218 it may set that value nonzero.
1220 PNEW_THREAD points to a location that is to receive the place at which
1221 execution should continue. */
1223 static rtx
1224 steal_delay_list_from_target (rtx insn, rtx condition, rtx seq,
1225 rtx delay_list, struct resources *sets,
1226 struct resources *needed,
1227 struct resources *other_needed,
1228 int slots_to_fill, int *pslots_filled,
1229 int *pannul_p, rtx *pnew_thread)
1231 rtx temp;
1232 int slots_remaining = slots_to_fill - *pslots_filled;
1233 int total_slots_filled = *pslots_filled;
1234 rtx new_delay_list = 0;
1235 int must_annul = *pannul_p;
1236 int used_annul = 0;
1237 int i;
1238 struct resources cc_set;
1240 /* We can't do anything if there are more delay slots in SEQ than we
1241 can handle, or if we don't know that it will be a taken branch.
1242 We know that it will be a taken branch if it is either an unconditional
1243 branch or a conditional branch with a stricter branch condition.
1245 Also, exit if the branch has more than one set, since then it is computing
1246 other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1247 ??? It may be possible to move other sets into INSN in addition to
1248 moving the instructions in the delay slots.
1250 We can not steal the delay list if one of the instructions in the
1251 current delay_list modifies the condition codes and the jump in the
1252 sequence is a conditional jump. We can not do this because we can
1253 not change the direction of the jump because the condition codes
1254 will effect the direction of the jump in the sequence. */
1256 CLEAR_RESOURCE (&cc_set);
1257 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1259 rtx trial = XEXP (temp, 0);
1261 mark_set_resources (trial, &cc_set, 0, MARK_SRC_DEST_CALL);
1262 if (insn_references_resource_p (XVECEXP (seq , 0, 0), &cc_set, 0))
1263 return delay_list;
1266 if (XVECLEN (seq, 0) - 1 > slots_remaining
1267 || ! condition_dominates_p (condition, XVECEXP (seq, 0, 0))
1268 || ! single_set (XVECEXP (seq, 0, 0)))
1269 return delay_list;
1271 #ifdef MD_CAN_REDIRECT_BRANCH
1272 /* On some targets, branches with delay slots can have a limited
1273 displacement. Give the back end a chance to tell us we can't do
1274 this. */
1275 if (! MD_CAN_REDIRECT_BRANCH (insn, XVECEXP (seq, 0, 0)))
1276 return delay_list;
1277 #endif
1279 for (i = 1; i < XVECLEN (seq, 0); i++)
1281 rtx trial = XVECEXP (seq, 0, i);
1282 int flags;
1284 if (insn_references_resource_p (trial, sets, 0)
1285 || insn_sets_resource_p (trial, needed, 0)
1286 || insn_sets_resource_p (trial, sets, 0)
1287 #ifdef HAVE_cc0
1288 /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1289 delay list. */
1290 || find_reg_note (trial, REG_CC_USER, NULL_RTX)
1291 #endif
1292 /* If TRIAL is from the fallthrough code of an annulled branch insn
1293 in SEQ, we cannot use it. */
1294 || (INSN_ANNULLED_BRANCH_P (XVECEXP (seq, 0, 0))
1295 && ! INSN_FROM_TARGET_P (trial)))
1296 return delay_list;
1298 /* If this insn was already done (usually in a previous delay slot),
1299 pretend we put it in our delay slot. */
1300 if (redundant_insn (trial, insn, new_delay_list))
1301 continue;
1303 /* We will end up re-vectoring this branch, so compute flags
1304 based on jumping to the new label. */
1305 flags = get_jump_flags (insn, JUMP_LABEL (XVECEXP (seq, 0, 0)));
1307 if (! must_annul
1308 && ((condition == const_true_rtx
1309 || (! insn_sets_resource_p (trial, other_needed, 0)
1310 && ! may_trap_p (PATTERN (trial)))))
1311 ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1312 : (must_annul || (delay_list == NULL && new_delay_list == NULL))
1313 && (must_annul = 1,
1314 check_annul_list_true_false (0, delay_list)
1315 && check_annul_list_true_false (0, new_delay_list)
1316 && eligible_for_annul_false (insn, total_slots_filled,
1317 trial, flags)))
1319 if (must_annul)
1320 used_annul = 1;
1321 temp = copy_rtx (trial);
1322 INSN_FROM_TARGET_P (temp) = 1;
1323 new_delay_list = add_to_delay_list (temp, new_delay_list);
1324 total_slots_filled++;
1326 if (--slots_remaining == 0)
1327 break;
1329 else
1330 return delay_list;
1333 /* Show the place to which we will be branching. */
1334 *pnew_thread = next_active_insn (JUMP_LABEL (XVECEXP (seq, 0, 0)));
1336 /* Add any new insns to the delay list and update the count of the
1337 number of slots filled. */
1338 *pslots_filled = total_slots_filled;
1339 if (used_annul)
1340 *pannul_p = 1;
1342 if (delay_list == 0)
1343 return new_delay_list;
1345 for (temp = new_delay_list; temp; temp = XEXP (temp, 1))
1346 delay_list = add_to_delay_list (XEXP (temp, 0), delay_list);
1348 return delay_list;
1351 /* Similar to steal_delay_list_from_target except that SEQ is on the
1352 fallthrough path of INSN. Here we only do something if the delay insn
1353 of SEQ is an unconditional branch. In that case we steal its delay slot
1354 for INSN since unconditional branches are much easier to fill. */
1356 static rtx
1357 steal_delay_list_from_fallthrough (rtx insn, rtx condition, rtx seq,
1358 rtx delay_list, struct resources *sets,
1359 struct resources *needed,
1360 struct resources *other_needed,
1361 int slots_to_fill, int *pslots_filled,
1362 int *pannul_p)
1364 int i;
1365 int flags;
1366 int must_annul = *pannul_p;
1367 int used_annul = 0;
1369 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1371 /* We can't do anything if SEQ's delay insn isn't an
1372 unconditional branch. */
1374 if (! simplejump_p (XVECEXP (seq, 0, 0))
1375 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) != RETURN)
1376 return delay_list;
1378 for (i = 1; i < XVECLEN (seq, 0); i++)
1380 rtx trial = XVECEXP (seq, 0, i);
1382 /* If TRIAL sets CC0, stealing it will move it too far from the use
1383 of CC0. */
1384 if (insn_references_resource_p (trial, sets, 0)
1385 || insn_sets_resource_p (trial, needed, 0)
1386 || insn_sets_resource_p (trial, sets, 0)
1387 #ifdef HAVE_cc0
1388 || sets_cc0_p (PATTERN (trial))
1389 #endif
1392 break;
1394 /* If this insn was already done, we don't need it. */
1395 if (redundant_insn (trial, insn, delay_list))
1397 delete_from_delay_slot (trial);
1398 continue;
1401 if (! must_annul
1402 && ((condition == const_true_rtx
1403 || (! insn_sets_resource_p (trial, other_needed, 0)
1404 && ! may_trap_p (PATTERN (trial)))))
1405 ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1406 : (must_annul || delay_list == NULL) && (must_annul = 1,
1407 check_annul_list_true_false (1, delay_list)
1408 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1410 if (must_annul)
1411 used_annul = 1;
1412 delete_from_delay_slot (trial);
1413 delay_list = add_to_delay_list (trial, delay_list);
1415 if (++(*pslots_filled) == slots_to_fill)
1416 break;
1418 else
1419 break;
1422 if (used_annul)
1423 *pannul_p = 1;
1424 return delay_list;
1427 /* Try merging insns starting at THREAD which match exactly the insns in
1428 INSN's delay list.
1430 If all insns were matched and the insn was previously annulling, the
1431 annul bit will be cleared.
1433 For each insn that is merged, if the branch is or will be non-annulling,
1434 we delete the merged insn. */
1436 static void
1437 try_merge_delay_insns (rtx insn, rtx thread)
1439 rtx trial, next_trial;
1440 rtx delay_insn = XVECEXP (PATTERN (insn), 0, 0);
1441 int annul_p = INSN_ANNULLED_BRANCH_P (delay_insn);
1442 int slot_number = 1;
1443 int num_slots = XVECLEN (PATTERN (insn), 0);
1444 rtx next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1445 struct resources set, needed;
1446 rtx merged_insns = 0;
1447 int i;
1448 int flags;
1450 flags = get_jump_flags (delay_insn, JUMP_LABEL (delay_insn));
1452 CLEAR_RESOURCE (&needed);
1453 CLEAR_RESOURCE (&set);
1455 /* If this is not an annulling branch, take into account anything needed in
1456 INSN's delay slot. This prevents two increments from being incorrectly
1457 folded into one. If we are annulling, this would be the correct
1458 thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1459 will essentially disable this optimization. This method is somewhat of
1460 a kludge, but I don't see a better way.) */
1461 if (! annul_p)
1462 for (i = 1 ; i < num_slots; i++)
1463 if (XVECEXP (PATTERN (insn), 0, i))
1464 mark_referenced_resources (XVECEXP (PATTERN (insn), 0, i), &needed, 1);
1466 for (trial = thread; !stop_search_p (trial, 1); trial = next_trial)
1468 rtx pat = PATTERN (trial);
1469 rtx oldtrial = trial;
1471 next_trial = next_nonnote_insn (trial);
1473 /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1474 if (GET_CODE (trial) == INSN
1475 && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1476 continue;
1478 if (GET_CODE (next_to_match) == GET_CODE (trial)
1479 #ifdef HAVE_cc0
1480 /* We can't share an insn that sets cc0. */
1481 && ! sets_cc0_p (pat)
1482 #endif
1483 && ! insn_references_resource_p (trial, &set, 1)
1484 && ! insn_sets_resource_p (trial, &set, 1)
1485 && ! insn_sets_resource_p (trial, &needed, 1)
1486 && (trial = try_split (pat, trial, 0)) != 0
1487 /* Update next_trial, in case try_split succeeded. */
1488 && (next_trial = next_nonnote_insn (trial))
1489 /* Likewise THREAD. */
1490 && (thread = oldtrial == thread ? trial : thread)
1491 && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1492 /* Have to test this condition if annul condition is different
1493 from (and less restrictive than) non-annulling one. */
1494 && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1497 if (! annul_p)
1499 update_block (trial, thread);
1500 if (trial == thread)
1501 thread = next_active_insn (thread);
1503 delete_related_insns (trial);
1504 INSN_FROM_TARGET_P (next_to_match) = 0;
1506 else
1507 merged_insns = gen_rtx_INSN_LIST (VOIDmode, trial, merged_insns);
1509 if (++slot_number == num_slots)
1510 break;
1512 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1515 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1516 mark_referenced_resources (trial, &needed, 1);
1519 /* See if we stopped on a filled insn. If we did, try to see if its
1520 delay slots match. */
1521 if (slot_number != num_slots
1522 && trial && GET_CODE (trial) == INSN
1523 && GET_CODE (PATTERN (trial)) == SEQUENCE
1524 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial), 0, 0)))
1526 rtx pat = PATTERN (trial);
1527 rtx filled_insn = XVECEXP (pat, 0, 0);
1529 /* Account for resources set/needed by the filled insn. */
1530 mark_set_resources (filled_insn, &set, 0, MARK_SRC_DEST_CALL);
1531 mark_referenced_resources (filled_insn, &needed, 1);
1533 for (i = 1; i < XVECLEN (pat, 0); i++)
1535 rtx dtrial = XVECEXP (pat, 0, i);
1537 if (! insn_references_resource_p (dtrial, &set, 1)
1538 && ! insn_sets_resource_p (dtrial, &set, 1)
1539 && ! insn_sets_resource_p (dtrial, &needed, 1)
1540 #ifdef HAVE_cc0
1541 && ! sets_cc0_p (PATTERN (dtrial))
1542 #endif
1543 && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1544 && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1546 if (! annul_p)
1548 rtx new;
1550 update_block (dtrial, thread);
1551 new = delete_from_delay_slot (dtrial);
1552 if (INSN_DELETED_P (thread))
1553 thread = new;
1554 INSN_FROM_TARGET_P (next_to_match) = 0;
1556 else
1557 merged_insns = gen_rtx_INSN_LIST (SImode, dtrial,
1558 merged_insns);
1560 if (++slot_number == num_slots)
1561 break;
1563 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1565 else
1567 /* Keep track of the set/referenced resources for the delay
1568 slots of any trial insns we encounter. */
1569 mark_set_resources (dtrial, &set, 0, MARK_SRC_DEST_CALL);
1570 mark_referenced_resources (dtrial, &needed, 1);
1575 /* If all insns in the delay slot have been matched and we were previously
1576 annulling the branch, we need not any more. In that case delete all the
1577 merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1578 the delay list so that we know that it isn't only being used at the
1579 target. */
1580 if (slot_number == num_slots && annul_p)
1582 for (; merged_insns; merged_insns = XEXP (merged_insns, 1))
1584 if (GET_MODE (merged_insns) == SImode)
1586 rtx new;
1588 update_block (XEXP (merged_insns, 0), thread);
1589 new = delete_from_delay_slot (XEXP (merged_insns, 0));
1590 if (INSN_DELETED_P (thread))
1591 thread = new;
1593 else
1595 update_block (XEXP (merged_insns, 0), thread);
1596 delete_related_insns (XEXP (merged_insns, 0));
1600 INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1602 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1603 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i)) = 0;
1607 /* See if INSN is redundant with an insn in front of TARGET. Often this
1608 is called when INSN is a candidate for a delay slot of TARGET.
1609 DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1610 of INSN. Often INSN will be redundant with an insn in a delay slot of
1611 some previous insn. This happens when we have a series of branches to the
1612 same label; in that case the first insn at the target might want to go
1613 into each of the delay slots.
1615 If we are not careful, this routine can take up a significant fraction
1616 of the total compilation time (4%), but only wins rarely. Hence we
1617 speed this routine up by making two passes. The first pass goes back
1618 until it hits a label and sees if it find an insn with an identical
1619 pattern. Only in this (relatively rare) event does it check for
1620 data conflicts.
1622 We do not split insns we encounter. This could cause us not to find a
1623 redundant insn, but the cost of splitting seems greater than the possible
1624 gain in rare cases. */
1626 static rtx
1627 redundant_insn (rtx insn, rtx target, rtx delay_list)
1629 rtx target_main = target;
1630 rtx ipat = PATTERN (insn);
1631 rtx trial, pat;
1632 struct resources needed, set;
1633 int i;
1634 unsigned insns_to_search;
1636 /* If INSN has any REG_UNUSED notes, it can't match anything since we
1637 are allowed to not actually assign to such a register. */
1638 if (find_reg_note (insn, REG_UNUSED, NULL_RTX) != 0)
1639 return 0;
1641 /* Scan backwards looking for a match. */
1642 for (trial = PREV_INSN (target),
1643 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1644 trial && insns_to_search > 0;
1645 trial = PREV_INSN (trial), --insns_to_search)
1647 if (GET_CODE (trial) == CODE_LABEL)
1648 return 0;
1650 if (! INSN_P (trial))
1651 continue;
1653 pat = PATTERN (trial);
1654 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1655 continue;
1657 if (GET_CODE (pat) == SEQUENCE)
1659 /* Stop for a CALL and its delay slots because it is difficult to
1660 track its resource needs correctly. */
1661 if (GET_CODE (XVECEXP (pat, 0, 0)) == CALL_INSN)
1662 return 0;
1664 /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1665 slots because it is difficult to track its resource needs
1666 correctly. */
1668 #ifdef INSN_SETS_ARE_DELAYED
1669 if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1670 return 0;
1671 #endif
1673 #ifdef INSN_REFERENCES_ARE_DELAYED
1674 if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1675 return 0;
1676 #endif
1678 /* See if any of the insns in the delay slot match, updating
1679 resource requirements as we go. */
1680 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1681 if (GET_CODE (XVECEXP (pat, 0, i)) == GET_CODE (insn)
1682 && rtx_equal_p (PATTERN (XVECEXP (pat, 0, i)), ipat)
1683 && ! find_reg_note (XVECEXP (pat, 0, i), REG_UNUSED, NULL_RTX))
1684 break;
1686 /* If found a match, exit this loop early. */
1687 if (i > 0)
1688 break;
1691 else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat)
1692 && ! find_reg_note (trial, REG_UNUSED, NULL_RTX))
1693 break;
1696 /* If we didn't find an insn that matches, return 0. */
1697 if (trial == 0)
1698 return 0;
1700 /* See what resources this insn sets and needs. If they overlap, or
1701 if this insn references CC0, it can't be redundant. */
1703 CLEAR_RESOURCE (&needed);
1704 CLEAR_RESOURCE (&set);
1705 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1706 mark_referenced_resources (insn, &needed, 1);
1708 /* If TARGET is a SEQUENCE, get the main insn. */
1709 if (GET_CODE (target) == INSN && GET_CODE (PATTERN (target)) == SEQUENCE)
1710 target_main = XVECEXP (PATTERN (target), 0, 0);
1712 if (resource_conflicts_p (&needed, &set)
1713 #ifdef HAVE_cc0
1714 || reg_mentioned_p (cc0_rtx, ipat)
1715 #endif
1716 /* The insn requiring the delay may not set anything needed or set by
1717 INSN. */
1718 || insn_sets_resource_p (target_main, &needed, 1)
1719 || insn_sets_resource_p (target_main, &set, 1))
1720 return 0;
1722 /* Insns we pass may not set either NEEDED or SET, so merge them for
1723 simpler tests. */
1724 needed.memory |= set.memory;
1725 needed.unch_memory |= set.unch_memory;
1726 IOR_HARD_REG_SET (needed.regs, set.regs);
1728 /* This insn isn't redundant if it conflicts with an insn that either is
1729 or will be in a delay slot of TARGET. */
1731 while (delay_list)
1733 if (insn_sets_resource_p (XEXP (delay_list, 0), &needed, 1))
1734 return 0;
1735 delay_list = XEXP (delay_list, 1);
1738 if (GET_CODE (target) == INSN && GET_CODE (PATTERN (target)) == SEQUENCE)
1739 for (i = 1; i < XVECLEN (PATTERN (target), 0); i++)
1740 if (insn_sets_resource_p (XVECEXP (PATTERN (target), 0, i), &needed, 1))
1741 return 0;
1743 /* Scan backwards until we reach a label or an insn that uses something
1744 INSN sets or sets something insn uses or sets. */
1746 for (trial = PREV_INSN (target),
1747 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1748 trial && GET_CODE (trial) != CODE_LABEL && insns_to_search > 0;
1749 trial = PREV_INSN (trial), --insns_to_search)
1751 if (GET_CODE (trial) != INSN && GET_CODE (trial) != CALL_INSN
1752 && GET_CODE (trial) != JUMP_INSN)
1753 continue;
1755 pat = PATTERN (trial);
1756 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1757 continue;
1759 if (GET_CODE (pat) == SEQUENCE)
1761 /* If this is a CALL_INSN and its delay slots, it is hard to track
1762 the resource needs properly, so give up. */
1763 if (GET_CODE (XVECEXP (pat, 0, 0)) == CALL_INSN)
1764 return 0;
1766 /* If this is an INSN or JUMP_INSN with delayed effects, it
1767 is hard to track the resource needs properly, so give up. */
1769 #ifdef INSN_SETS_ARE_DELAYED
1770 if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1771 return 0;
1772 #endif
1774 #ifdef INSN_REFERENCES_ARE_DELAYED
1775 if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1776 return 0;
1777 #endif
1779 /* See if any of the insns in the delay slot match, updating
1780 resource requirements as we go. */
1781 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1783 rtx candidate = XVECEXP (pat, 0, i);
1785 /* If an insn will be annulled if the branch is false, it isn't
1786 considered as a possible duplicate insn. */
1787 if (rtx_equal_p (PATTERN (candidate), ipat)
1788 && ! (INSN_ANNULLED_BRANCH_P (XVECEXP (pat, 0, 0))
1789 && INSN_FROM_TARGET_P (candidate)))
1791 /* Show that this insn will be used in the sequel. */
1792 INSN_FROM_TARGET_P (candidate) = 0;
1793 return candidate;
1796 /* Unless this is an annulled insn from the target of a branch,
1797 we must stop if it sets anything needed or set by INSN. */
1798 if ((! INSN_ANNULLED_BRANCH_P (XVECEXP (pat, 0, 0))
1799 || ! INSN_FROM_TARGET_P (candidate))
1800 && insn_sets_resource_p (candidate, &needed, 1))
1801 return 0;
1804 /* If the insn requiring the delay slot conflicts with INSN, we
1805 must stop. */
1806 if (insn_sets_resource_p (XVECEXP (pat, 0, 0), &needed, 1))
1807 return 0;
1809 else
1811 /* See if TRIAL is the same as INSN. */
1812 pat = PATTERN (trial);
1813 if (rtx_equal_p (pat, ipat))
1814 return trial;
1816 /* Can't go any further if TRIAL conflicts with INSN. */
1817 if (insn_sets_resource_p (trial, &needed, 1))
1818 return 0;
1822 return 0;
1825 /* Return 1 if THREAD can only be executed in one way. If LABEL is nonzero,
1826 it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1827 is nonzero, we are allowed to fall into this thread; otherwise, we are
1828 not.
1830 If LABEL is used more than one or we pass a label other than LABEL before
1831 finding an active insn, we do not own this thread. */
1833 static int
1834 own_thread_p (rtx thread, rtx label, int allow_fallthrough)
1836 rtx active_insn;
1837 rtx insn;
1839 /* We don't own the function end. */
1840 if (thread == 0)
1841 return 0;
1843 /* Get the first active insn, or THREAD, if it is an active insn. */
1844 active_insn = next_active_insn (PREV_INSN (thread));
1846 for (insn = thread; insn != active_insn; insn = NEXT_INSN (insn))
1847 if (GET_CODE (insn) == CODE_LABEL
1848 && (insn != label || LABEL_NUSES (insn) != 1))
1849 return 0;
1851 if (allow_fallthrough)
1852 return 1;
1854 /* Ensure that we reach a BARRIER before any insn or label. */
1855 for (insn = prev_nonnote_insn (thread);
1856 insn == 0 || GET_CODE (insn) != BARRIER;
1857 insn = prev_nonnote_insn (insn))
1858 if (insn == 0
1859 || GET_CODE (insn) == CODE_LABEL
1860 || (GET_CODE (insn) == INSN
1861 && GET_CODE (PATTERN (insn)) != USE
1862 && GET_CODE (PATTERN (insn)) != CLOBBER))
1863 return 0;
1865 return 1;
1868 /* Called when INSN is being moved from a location near the target of a jump.
1869 We leave a marker of the form (use (INSN)) immediately in front
1870 of WHERE for mark_target_live_regs. These markers will be deleted when
1871 reorg finishes.
1873 We used to try to update the live status of registers if WHERE is at
1874 the start of a basic block, but that can't work since we may remove a
1875 BARRIER in relax_delay_slots. */
1877 static void
1878 update_block (rtx insn, rtx where)
1880 /* Ignore if this was in a delay slot and it came from the target of
1881 a branch. */
1882 if (INSN_FROM_TARGET_P (insn))
1883 return;
1885 emit_insn_before (gen_rtx_USE (VOIDmode, insn), where);
1887 /* INSN might be making a value live in a block where it didn't use to
1888 be. So recompute liveness information for this block. */
1890 incr_ticks_for_insn (insn);
1893 /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1894 the basic block containing the jump. */
1896 static int
1897 reorg_redirect_jump (rtx jump, rtx nlabel)
1899 incr_ticks_for_insn (jump);
1900 return redirect_jump (jump, nlabel, 1);
1903 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1904 We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1905 that reference values used in INSN. If we find one, then we move the
1906 REG_DEAD note to INSN.
1908 This is needed to handle the case where an later insn (after INSN) has a
1909 REG_DEAD note for a register used by INSN, and this later insn subsequently
1910 gets moved before a CODE_LABEL because it is a redundant insn. In this
1911 case, mark_target_live_regs may be confused into thinking the register
1912 is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1914 static void
1915 update_reg_dead_notes (rtx insn, rtx delayed_insn)
1917 rtx p, link, next;
1919 for (p = next_nonnote_insn (insn); p != delayed_insn;
1920 p = next_nonnote_insn (p))
1921 for (link = REG_NOTES (p); link; link = next)
1923 next = XEXP (link, 1);
1925 if (REG_NOTE_KIND (link) != REG_DEAD
1926 || GET_CODE (XEXP (link, 0)) != REG)
1927 continue;
1929 if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
1931 /* Move the REG_DEAD note from P to INSN. */
1932 remove_note (p, link);
1933 XEXP (link, 1) = REG_NOTES (insn);
1934 REG_NOTES (insn) = link;
1939 /* Called when an insn redundant with start_insn is deleted. If there
1940 is a REG_DEAD note for the target of start_insn between start_insn
1941 and stop_insn, then the REG_DEAD note needs to be deleted since the
1942 value no longer dies there.
1944 If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1945 confused into thinking the register is dead. */
1947 static void
1948 fix_reg_dead_note (rtx start_insn, rtx stop_insn)
1950 rtx p, link, next;
1952 for (p = next_nonnote_insn (start_insn); p != stop_insn;
1953 p = next_nonnote_insn (p))
1954 for (link = REG_NOTES (p); link; link = next)
1956 next = XEXP (link, 1);
1958 if (REG_NOTE_KIND (link) != REG_DEAD
1959 || GET_CODE (XEXP (link, 0)) != REG)
1960 continue;
1962 if (reg_set_p (XEXP (link, 0), PATTERN (start_insn)))
1964 remove_note (p, link);
1965 return;
1970 /* Delete any REG_UNUSED notes that exist on INSN but not on REDUNDANT_INSN.
1972 This handles the case of udivmodXi4 instructions which optimize their
1973 output depending on whether any REG_UNUSED notes are present.
1974 we must make sure that INSN calculates as many results as REDUNDANT_INSN
1975 does. */
1977 static void
1978 update_reg_unused_notes (rtx insn, rtx redundant_insn)
1980 rtx link, next;
1982 for (link = REG_NOTES (insn); link; link = next)
1984 next = XEXP (link, 1);
1986 if (REG_NOTE_KIND (link) != REG_UNUSED
1987 || GET_CODE (XEXP (link, 0)) != REG)
1988 continue;
1990 if (! find_regno_note (redundant_insn, REG_UNUSED,
1991 REGNO (XEXP (link, 0))))
1992 remove_note (insn, link);
1996 /* Scan a function looking for insns that need a delay slot and find insns to
1997 put into the delay slot.
1999 NON_JUMPS_P is nonzero if we are to only try to fill non-jump insns (such
2000 as calls). We do these first since we don't want jump insns (that are
2001 easier to fill) to get the only insns that could be used for non-jump insns.
2002 When it is zero, only try to fill JUMP_INSNs.
2004 When slots are filled in this manner, the insns (including the
2005 delay_insn) are put together in a SEQUENCE rtx. In this fashion,
2006 it is possible to tell whether a delay slot has really been filled
2007 or not. `final' knows how to deal with this, by communicating
2008 through FINAL_SEQUENCE. */
2010 static void
2011 fill_simple_delay_slots (int non_jumps_p)
2013 rtx insn, pat, trial, next_trial;
2014 int i;
2015 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2016 struct resources needed, set;
2017 int slots_to_fill, slots_filled;
2018 rtx delay_list;
2020 for (i = 0; i < num_unfilled_slots; i++)
2022 int flags;
2023 /* Get the next insn to fill. If it has already had any slots assigned,
2024 we can't do anything with it. Maybe we'll improve this later. */
2026 insn = unfilled_slots_base[i];
2027 if (insn == 0
2028 || INSN_DELETED_P (insn)
2029 || (GET_CODE (insn) == INSN
2030 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2031 || (GET_CODE (insn) == JUMP_INSN && non_jumps_p)
2032 || (GET_CODE (insn) != JUMP_INSN && ! non_jumps_p))
2033 continue;
2035 /* It may have been that this insn used to need delay slots, but
2036 now doesn't; ignore in that case. This can happen, for example,
2037 on the HP PA RISC, where the number of delay slots depends on
2038 what insns are nearby. */
2039 slots_to_fill = num_delay_slots (insn);
2041 /* Some machine description have defined instructions to have
2042 delay slots only in certain circumstances which may depend on
2043 nearby insns (which change due to reorg's actions).
2045 For example, the PA port normally has delay slots for unconditional
2046 jumps.
2048 However, the PA port claims such jumps do not have a delay slot
2049 if they are immediate successors of certain CALL_INSNs. This
2050 allows the port to favor filling the delay slot of the call with
2051 the unconditional jump. */
2052 if (slots_to_fill == 0)
2053 continue;
2055 /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
2056 says how many. After initialization, first try optimizing
2058 call _foo call _foo
2059 nop add %o7,.-L1,%o7
2060 b,a L1
2063 If this case applies, the delay slot of the call is filled with
2064 the unconditional jump. This is done first to avoid having the
2065 delay slot of the call filled in the backward scan. Also, since
2066 the unconditional jump is likely to also have a delay slot, that
2067 insn must exist when it is subsequently scanned.
2069 This is tried on each insn with delay slots as some machines
2070 have insns which perform calls, but are not represented as
2071 CALL_INSNs. */
2073 slots_filled = 0;
2074 delay_list = 0;
2076 if (GET_CODE (insn) == JUMP_INSN)
2077 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2078 else
2079 flags = get_jump_flags (insn, NULL_RTX);
2081 if ((trial = next_active_insn (insn))
2082 && GET_CODE (trial) == JUMP_INSN
2083 && simplejump_p (trial)
2084 && eligible_for_delay (insn, slots_filled, trial, flags)
2085 && no_labels_between_p (insn, trial)
2086 && ! can_throw_internal (trial))
2088 rtx *tmp;
2089 slots_filled++;
2090 delay_list = add_to_delay_list (trial, delay_list);
2092 /* TRIAL may have had its delay slot filled, then unfilled. When
2093 the delay slot is unfilled, TRIAL is placed back on the unfilled
2094 slots obstack. Unfortunately, it is placed on the end of the
2095 obstack, not in its original location. Therefore, we must search
2096 from entry i + 1 to the end of the unfilled slots obstack to
2097 try and find TRIAL. */
2098 tmp = &unfilled_slots_base[i + 1];
2099 while (*tmp != trial && tmp != unfilled_slots_next)
2100 tmp++;
2102 /* Remove the unconditional jump from consideration for delay slot
2103 filling and unthread it. */
2104 if (*tmp == trial)
2105 *tmp = 0;
2107 rtx next = NEXT_INSN (trial);
2108 rtx prev = PREV_INSN (trial);
2109 if (prev)
2110 NEXT_INSN (prev) = next;
2111 if (next)
2112 PREV_INSN (next) = prev;
2116 /* Now, scan backwards from the insn to search for a potential
2117 delay-slot candidate. Stop searching when a label or jump is hit.
2119 For each candidate, if it is to go into the delay slot (moved
2120 forward in execution sequence), it must not need or set any resources
2121 that were set by later insns and must not set any resources that
2122 are needed for those insns.
2124 The delay slot insn itself sets resources unless it is a call
2125 (in which case the called routine, not the insn itself, is doing
2126 the setting). */
2128 if (slots_filled < slots_to_fill)
2130 CLEAR_RESOURCE (&needed);
2131 CLEAR_RESOURCE (&set);
2132 mark_set_resources (insn, &set, 0, MARK_SRC_DEST);
2133 mark_referenced_resources (insn, &needed, 0);
2135 for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, 1);
2136 trial = next_trial)
2138 next_trial = prev_nonnote_insn (trial);
2140 /* This must be an INSN or CALL_INSN. */
2141 pat = PATTERN (trial);
2143 /* USE and CLOBBER at this level was just for flow; ignore it. */
2144 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2145 continue;
2147 /* Check for resource conflict first, to avoid unnecessary
2148 splitting. */
2149 if (! insn_references_resource_p (trial, &set, 1)
2150 && ! insn_sets_resource_p (trial, &set, 1)
2151 && ! insn_sets_resource_p (trial, &needed, 1)
2152 #ifdef HAVE_cc0
2153 /* Can't separate set of cc0 from its use. */
2154 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2155 #endif
2156 && ! can_throw_internal (trial))
2158 trial = try_split (pat, trial, 1);
2159 next_trial = prev_nonnote_insn (trial);
2160 if (eligible_for_delay (insn, slots_filled, trial, flags))
2162 /* In this case, we are searching backward, so if we
2163 find insns to put on the delay list, we want
2164 to put them at the head, rather than the
2165 tail, of the list. */
2167 update_reg_dead_notes (trial, insn);
2168 delay_list = gen_rtx_INSN_LIST (VOIDmode,
2169 trial, delay_list);
2170 update_block (trial, trial);
2171 delete_related_insns (trial);
2172 if (slots_to_fill == ++slots_filled)
2173 break;
2174 continue;
2178 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2179 mark_referenced_resources (trial, &needed, 1);
2183 /* If all needed slots haven't been filled, we come here. */
2185 /* Try to optimize case of jumping around a single insn. */
2186 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
2187 if (slots_filled != slots_to_fill
2188 && delay_list == 0
2189 && GET_CODE (insn) == JUMP_INSN
2190 && (condjump_p (insn) || condjump_in_parallel_p (insn)))
2192 delay_list = optimize_skip (insn);
2193 if (delay_list)
2194 slots_filled += 1;
2196 #endif
2198 /* Try to get insns from beyond the insn needing the delay slot.
2199 These insns can neither set or reference resources set in insns being
2200 skipped, cannot set resources in the insn being skipped, and, if this
2201 is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2202 call might not return).
2204 There used to be code which continued past the target label if
2205 we saw all uses of the target label. This code did not work,
2206 because it failed to account for some instructions which were
2207 both annulled and marked as from the target. This can happen as a
2208 result of optimize_skip. Since this code was redundant with
2209 fill_eager_delay_slots anyways, it was just deleted. */
2211 if (slots_filled != slots_to_fill
2212 /* If this instruction could throw an exception which is
2213 caught in the same function, then it's not safe to fill
2214 the delay slot with an instruction from beyond this
2215 point. For example, consider:
2217 int i = 2;
2219 try {
2220 f();
2221 i = 3;
2222 } catch (...) {}
2224 return i;
2226 Even though `i' is a local variable, we must be sure not
2227 to put `i = 3' in the delay slot if `f' might throw an
2228 exception.
2230 Presumably, we should also check to see if we could get
2231 back to this function via `setjmp'. */
2232 && ! can_throw_internal (insn)
2233 && (GET_CODE (insn) != JUMP_INSN
2234 || ((condjump_p (insn) || condjump_in_parallel_p (insn))
2235 && ! simplejump_p (insn)
2236 && JUMP_LABEL (insn) != 0)))
2238 /* Invariant: If insn is a JUMP_INSN, the insn's jump
2239 label. Otherwise, zero. */
2240 rtx target = 0;
2241 int maybe_never = 0;
2242 rtx pat, trial_delay;
2244 CLEAR_RESOURCE (&needed);
2245 CLEAR_RESOURCE (&set);
2247 if (GET_CODE (insn) == CALL_INSN)
2249 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2250 mark_referenced_resources (insn, &needed, 1);
2251 maybe_never = 1;
2253 else
2255 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2256 mark_referenced_resources (insn, &needed, 1);
2257 if (GET_CODE (insn) == JUMP_INSN)
2258 target = JUMP_LABEL (insn);
2261 if (target == 0)
2262 for (trial = next_nonnote_insn (insn); trial; trial = next_trial)
2264 next_trial = next_nonnote_insn (trial);
2266 if (GET_CODE (trial) == CODE_LABEL
2267 || GET_CODE (trial) == BARRIER)
2268 break;
2270 /* We must have an INSN, JUMP_INSN, or CALL_INSN. */
2271 pat = PATTERN (trial);
2273 /* Stand-alone USE and CLOBBER are just for flow. */
2274 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2275 continue;
2277 /* If this already has filled delay slots, get the insn needing
2278 the delay slots. */
2279 if (GET_CODE (pat) == SEQUENCE)
2280 trial_delay = XVECEXP (pat, 0, 0);
2281 else
2282 trial_delay = trial;
2284 /* Stop our search when seeing an unconditional jump. */
2285 if (GET_CODE (trial_delay) == JUMP_INSN)
2286 break;
2288 /* See if we have a resource problem before we try to
2289 split. */
2290 if (GET_CODE (pat) != SEQUENCE
2291 && ! insn_references_resource_p (trial, &set, 1)
2292 && ! insn_sets_resource_p (trial, &set, 1)
2293 && ! insn_sets_resource_p (trial, &needed, 1)
2294 #ifdef HAVE_cc0
2295 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2296 #endif
2297 && ! (maybe_never && may_trap_p (pat))
2298 && (trial = try_split (pat, trial, 0))
2299 && eligible_for_delay (insn, slots_filled, trial, flags)
2300 && ! can_throw_internal(trial))
2302 next_trial = next_nonnote_insn (trial);
2303 delay_list = add_to_delay_list (trial, delay_list);
2305 #ifdef HAVE_cc0
2306 if (reg_mentioned_p (cc0_rtx, pat))
2307 link_cc0_insns (trial);
2308 #endif
2310 delete_related_insns (trial);
2311 if (slots_to_fill == ++slots_filled)
2312 break;
2313 continue;
2316 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2317 mark_referenced_resources (trial, &needed, 1);
2319 /* Ensure we don't put insns between the setting of cc and the
2320 comparison by moving a setting of cc into an earlier delay
2321 slot since these insns could clobber the condition code. */
2322 set.cc = 1;
2324 /* If this is a call or jump, we might not get here. */
2325 if (GET_CODE (trial_delay) == CALL_INSN
2326 || GET_CODE (trial_delay) == JUMP_INSN)
2327 maybe_never = 1;
2330 /* If there are slots left to fill and our search was stopped by an
2331 unconditional branch, try the insn at the branch target. We can
2332 redirect the branch if it works.
2334 Don't do this if the insn at the branch target is a branch. */
2335 if (slots_to_fill != slots_filled
2336 && trial
2337 && GET_CODE (trial) == JUMP_INSN
2338 && simplejump_p (trial)
2339 && (target == 0 || JUMP_LABEL (trial) == target)
2340 && (next_trial = next_active_insn (JUMP_LABEL (trial))) != 0
2341 && ! (GET_CODE (next_trial) == INSN
2342 && GET_CODE (PATTERN (next_trial)) == SEQUENCE)
2343 && GET_CODE (next_trial) != JUMP_INSN
2344 && ! insn_references_resource_p (next_trial, &set, 1)
2345 && ! insn_sets_resource_p (next_trial, &set, 1)
2346 && ! insn_sets_resource_p (next_trial, &needed, 1)
2347 #ifdef HAVE_cc0
2348 && ! reg_mentioned_p (cc0_rtx, PATTERN (next_trial))
2349 #endif
2350 && ! (maybe_never && may_trap_p (PATTERN (next_trial)))
2351 && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2352 && eligible_for_delay (insn, slots_filled, next_trial, flags)
2353 && ! can_throw_internal (trial))
2355 rtx new_label = next_active_insn (next_trial);
2357 if (new_label != 0)
2358 new_label = get_label_before (new_label);
2359 else
2360 new_label = find_end_label ();
2362 delay_list
2363 = add_to_delay_list (copy_rtx (next_trial), delay_list);
2364 slots_filled++;
2365 reorg_redirect_jump (trial, new_label);
2367 /* If we merged because we both jumped to the same place,
2368 redirect the original insn also. */
2369 if (target)
2370 reorg_redirect_jump (insn, new_label);
2374 /* If this is an unconditional jump, then try to get insns from the
2375 target of the jump. */
2376 if (GET_CODE (insn) == JUMP_INSN
2377 && simplejump_p (insn)
2378 && slots_filled != slots_to_fill)
2379 delay_list
2380 = fill_slots_from_thread (insn, const_true_rtx,
2381 next_active_insn (JUMP_LABEL (insn)),
2382 NULL, 1, 1,
2383 own_thread_p (JUMP_LABEL (insn),
2384 JUMP_LABEL (insn), 0),
2385 slots_to_fill, &slots_filled,
2386 delay_list);
2388 if (delay_list)
2389 unfilled_slots_base[i]
2390 = emit_delay_sequence (insn, delay_list, slots_filled);
2392 if (slots_to_fill == slots_filled)
2393 unfilled_slots_base[i] = 0;
2395 note_delay_statistics (slots_filled, 0);
2398 #ifdef DELAY_SLOTS_FOR_EPILOGUE
2399 /* See if the epilogue needs any delay slots. Try to fill them if so.
2400 The only thing we can do is scan backwards from the end of the
2401 function. If we did this in a previous pass, it is incorrect to do it
2402 again. */
2403 if (current_function_epilogue_delay_list)
2404 return;
2406 slots_to_fill = DELAY_SLOTS_FOR_EPILOGUE;
2407 if (slots_to_fill == 0)
2408 return;
2410 slots_filled = 0;
2411 CLEAR_RESOURCE (&set);
2413 /* The frame pointer and stack pointer are needed at the beginning of
2414 the epilogue, so instructions setting them can not be put in the
2415 epilogue delay slot. However, everything else needed at function
2416 end is safe, so we don't want to use end_of_function_needs here. */
2417 CLEAR_RESOURCE (&needed);
2418 if (frame_pointer_needed)
2420 SET_HARD_REG_BIT (needed.regs, FRAME_POINTER_REGNUM);
2421 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2422 SET_HARD_REG_BIT (needed.regs, HARD_FRAME_POINTER_REGNUM);
2423 #endif
2424 #ifdef EXIT_IGNORE_STACK
2425 if (! EXIT_IGNORE_STACK
2426 || current_function_sp_is_unchanging)
2427 #endif
2428 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2430 else
2431 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2433 #ifdef EPILOGUE_USES
2434 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2436 if (EPILOGUE_USES (i))
2437 SET_HARD_REG_BIT (needed.regs, i);
2439 #endif
2441 for (trial = get_last_insn (); ! stop_search_p (trial, 1);
2442 trial = PREV_INSN (trial))
2444 if (GET_CODE (trial) == NOTE)
2445 continue;
2446 pat = PATTERN (trial);
2447 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2448 continue;
2450 if (! insn_references_resource_p (trial, &set, 1)
2451 && ! insn_sets_resource_p (trial, &needed, 1)
2452 && ! insn_sets_resource_p (trial, &set, 1)
2453 #ifdef HAVE_cc0
2454 /* Don't want to mess with cc0 here. */
2455 && ! reg_mentioned_p (cc0_rtx, pat)
2456 #endif
2457 && ! can_throw_internal (trial))
2459 trial = try_split (pat, trial, 1);
2460 if (ELIGIBLE_FOR_EPILOGUE_DELAY (trial, slots_filled))
2462 /* Here as well we are searching backward, so put the
2463 insns we find on the head of the list. */
2465 current_function_epilogue_delay_list
2466 = gen_rtx_INSN_LIST (VOIDmode, trial,
2467 current_function_epilogue_delay_list);
2468 mark_end_of_function_resources (trial, 1);
2469 update_block (trial, trial);
2470 delete_related_insns (trial);
2472 /* Clear deleted bit so final.c will output the insn. */
2473 INSN_DELETED_P (trial) = 0;
2475 if (slots_to_fill == ++slots_filled)
2476 break;
2477 continue;
2481 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2482 mark_referenced_resources (trial, &needed, 1);
2485 note_delay_statistics (slots_filled, 0);
2486 #endif
2489 /* Try to find insns to place in delay slots.
2491 INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2492 or is an unconditional branch if CONDITION is const_true_rtx.
2493 *PSLOTS_FILLED is updated with the number of slots that we have filled.
2495 THREAD is a flow-of-control, either the insns to be executed if the
2496 branch is true or if the branch is false, THREAD_IF_TRUE says which.
2498 OPPOSITE_THREAD is the thread in the opposite direction. It is used
2499 to see if any potential delay slot insns set things needed there.
2501 LIKELY is nonzero if it is extremely likely that the branch will be
2502 taken and THREAD_IF_TRUE is set. This is used for the branch at the
2503 end of a loop back up to the top.
2505 OWN_THREAD and OWN_OPPOSITE_THREAD are true if we are the only user of the
2506 thread. I.e., it is the fallthrough code of our jump or the target of the
2507 jump when we are the only jump going there.
2509 If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2510 case, we can only take insns from the head of the thread for our delay
2511 slot. We then adjust the jump to point after the insns we have taken. */
2513 static rtx
2514 fill_slots_from_thread (rtx insn, rtx condition, rtx thread,
2515 rtx opposite_thread, int likely, int thread_if_true,
2516 int own_thread, int slots_to_fill,
2517 int *pslots_filled, rtx delay_list)
2519 rtx new_thread;
2520 struct resources opposite_needed, set, needed;
2521 rtx trial;
2522 int lose = 0;
2523 int must_annul = 0;
2524 int flags;
2526 /* Validate our arguments. */
2527 if ((condition == const_true_rtx && ! thread_if_true)
2528 || (! own_thread && ! thread_if_true))
2529 abort ();
2531 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2533 /* If our thread is the end of subroutine, we can't get any delay
2534 insns from that. */
2535 if (thread == 0)
2536 return delay_list;
2538 /* If this is an unconditional branch, nothing is needed at the
2539 opposite thread. Otherwise, compute what is needed there. */
2540 if (condition == const_true_rtx)
2541 CLEAR_RESOURCE (&opposite_needed);
2542 else
2543 mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2545 /* If the insn at THREAD can be split, do it here to avoid having to
2546 update THREAD and NEW_THREAD if it is done in the loop below. Also
2547 initialize NEW_THREAD. */
2549 new_thread = thread = try_split (PATTERN (thread), thread, 0);
2551 /* Scan insns at THREAD. We are looking for an insn that can be removed
2552 from THREAD (it neither sets nor references resources that were set
2553 ahead of it and it doesn't set anything needs by the insns ahead of
2554 it) and that either can be placed in an annulling insn or aren't
2555 needed at OPPOSITE_THREAD. */
2557 CLEAR_RESOURCE (&needed);
2558 CLEAR_RESOURCE (&set);
2560 /* If we do not own this thread, we must stop as soon as we find
2561 something that we can't put in a delay slot, since all we can do
2562 is branch into THREAD at a later point. Therefore, labels stop
2563 the search if this is not the `true' thread. */
2565 for (trial = thread;
2566 ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2567 trial = next_nonnote_insn (trial))
2569 rtx pat, old_trial;
2571 /* If we have passed a label, we no longer own this thread. */
2572 if (GET_CODE (trial) == CODE_LABEL)
2574 own_thread = 0;
2575 continue;
2578 pat = PATTERN (trial);
2579 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2580 continue;
2582 /* If TRIAL conflicts with the insns ahead of it, we lose. Also,
2583 don't separate or copy insns that set and use CC0. */
2584 if (! insn_references_resource_p (trial, &set, 1)
2585 && ! insn_sets_resource_p (trial, &set, 1)
2586 && ! insn_sets_resource_p (trial, &needed, 1)
2587 #ifdef HAVE_cc0
2588 && ! (reg_mentioned_p (cc0_rtx, pat)
2589 && (! own_thread || ! sets_cc0_p (pat)))
2590 #endif
2591 && ! can_throw_internal (trial))
2593 rtx prior_insn;
2595 /* If TRIAL is redundant with some insn before INSN, we don't
2596 actually need to add it to the delay list; we can merely pretend
2597 we did. */
2598 if ((prior_insn = redundant_insn (trial, insn, delay_list)))
2600 fix_reg_dead_note (prior_insn, insn);
2601 if (own_thread)
2603 update_block (trial, thread);
2604 if (trial == thread)
2606 thread = next_active_insn (thread);
2607 if (new_thread == trial)
2608 new_thread = thread;
2611 delete_related_insns (trial);
2613 else
2615 update_reg_unused_notes (prior_insn, trial);
2616 new_thread = next_active_insn (trial);
2619 continue;
2622 /* There are two ways we can win: If TRIAL doesn't set anything
2623 needed at the opposite thread and can't trap, or if it can
2624 go into an annulled delay slot. */
2625 if (!must_annul
2626 && (condition == const_true_rtx
2627 || (! insn_sets_resource_p (trial, &opposite_needed, 1)
2628 && ! may_trap_p (pat))))
2630 old_trial = trial;
2631 trial = try_split (pat, trial, 0);
2632 if (new_thread == old_trial)
2633 new_thread = trial;
2634 if (thread == old_trial)
2635 thread = trial;
2636 pat = PATTERN (trial);
2637 if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2638 goto winner;
2640 else if (0
2641 #ifdef ANNUL_IFTRUE_SLOTS
2642 || ! thread_if_true
2643 #endif
2644 #ifdef ANNUL_IFFALSE_SLOTS
2645 || thread_if_true
2646 #endif
2649 old_trial = trial;
2650 trial = try_split (pat, trial, 0);
2651 if (new_thread == old_trial)
2652 new_thread = trial;
2653 if (thread == old_trial)
2654 thread = trial;
2655 pat = PATTERN (trial);
2656 if ((must_annul || delay_list == NULL) && (thread_if_true
2657 ? check_annul_list_true_false (0, delay_list)
2658 && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2659 : check_annul_list_true_false (1, delay_list)
2660 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2662 rtx temp;
2664 must_annul = 1;
2665 winner:
2667 #ifdef HAVE_cc0
2668 if (reg_mentioned_p (cc0_rtx, pat))
2669 link_cc0_insns (trial);
2670 #endif
2672 /* If we own this thread, delete the insn. If this is the
2673 destination of a branch, show that a basic block status
2674 may have been updated. In any case, mark the new
2675 starting point of this thread. */
2676 if (own_thread)
2678 rtx note;
2680 update_block (trial, thread);
2681 if (trial == thread)
2683 thread = next_active_insn (thread);
2684 if (new_thread == trial)
2685 new_thread = thread;
2688 /* We are moving this insn, not deleting it. We must
2689 temporarily increment the use count on any referenced
2690 label lest it be deleted by delete_related_insns. */
2691 note = find_reg_note (trial, REG_LABEL, 0);
2692 /* REG_LABEL could be NOTE_INSN_DELETED_LABEL too. */
2693 if (note && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2694 LABEL_NUSES (XEXP (note, 0))++;
2696 delete_related_insns (trial);
2698 if (note && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2699 LABEL_NUSES (XEXP (note, 0))--;
2701 else
2702 new_thread = next_active_insn (trial);
2704 temp = own_thread ? trial : copy_rtx (trial);
2705 if (thread_if_true)
2706 INSN_FROM_TARGET_P (temp) = 1;
2708 delay_list = add_to_delay_list (temp, delay_list);
2710 if (slots_to_fill == ++(*pslots_filled))
2712 /* Even though we have filled all the slots, we
2713 may be branching to a location that has a
2714 redundant insn. Skip any if so. */
2715 while (new_thread && ! own_thread
2716 && ! insn_sets_resource_p (new_thread, &set, 1)
2717 && ! insn_sets_resource_p (new_thread, &needed, 1)
2718 && ! insn_references_resource_p (new_thread,
2719 &set, 1)
2720 && (prior_insn
2721 = redundant_insn (new_thread, insn,
2722 delay_list)))
2724 /* We know we do not own the thread, so no need
2725 to call update_block and delete_insn. */
2726 fix_reg_dead_note (prior_insn, insn);
2727 update_reg_unused_notes (prior_insn, new_thread);
2728 new_thread = next_active_insn (new_thread);
2730 break;
2733 continue;
2738 /* This insn can't go into a delay slot. */
2739 lose = 1;
2740 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2741 mark_referenced_resources (trial, &needed, 1);
2743 /* Ensure we don't put insns between the setting of cc and the comparison
2744 by moving a setting of cc into an earlier delay slot since these insns
2745 could clobber the condition code. */
2746 set.cc = 1;
2748 /* If this insn is a register-register copy and the next insn has
2749 a use of our destination, change it to use our source. That way,
2750 it will become a candidate for our delay slot the next time
2751 through this loop. This case occurs commonly in loops that
2752 scan a list.
2754 We could check for more complex cases than those tested below,
2755 but it doesn't seem worth it. It might also be a good idea to try
2756 to swap the two insns. That might do better.
2758 We can't do this if the next insn modifies our destination, because
2759 that would make the replacement into the insn invalid. We also can't
2760 do this if it modifies our source, because it might be an earlyclobber
2761 operand. This latter test also prevents updating the contents of
2762 a PRE_INC. We also can't do this if there's overlap of source and
2763 destination. Overlap may happen for larger-than-register-size modes. */
2765 if (GET_CODE (trial) == INSN && GET_CODE (pat) == SET
2766 && GET_CODE (SET_SRC (pat)) == REG
2767 && GET_CODE (SET_DEST (pat)) == REG
2768 && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2770 rtx next = next_nonnote_insn (trial);
2772 if (next && GET_CODE (next) == INSN
2773 && GET_CODE (PATTERN (next)) != USE
2774 && ! reg_set_p (SET_DEST (pat), next)
2775 && ! reg_set_p (SET_SRC (pat), next)
2776 && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2777 && ! modified_in_p (SET_DEST (pat), next))
2778 validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2782 /* If we stopped on a branch insn that has delay slots, see if we can
2783 steal some of the insns in those slots. */
2784 if (trial && GET_CODE (trial) == INSN
2785 && GET_CODE (PATTERN (trial)) == SEQUENCE
2786 && GET_CODE (XVECEXP (PATTERN (trial), 0, 0)) == JUMP_INSN)
2788 /* If this is the `true' thread, we will want to follow the jump,
2789 so we can only do this if we have taken everything up to here. */
2790 if (thread_if_true && trial == new_thread)
2792 delay_list
2793 = steal_delay_list_from_target (insn, condition, PATTERN (trial),
2794 delay_list, &set, &needed,
2795 &opposite_needed, slots_to_fill,
2796 pslots_filled, &must_annul,
2797 &new_thread);
2798 /* If we owned the thread and are told that it branched
2799 elsewhere, make sure we own the thread at the new location. */
2800 if (own_thread && trial != new_thread)
2801 own_thread = own_thread_p (new_thread, new_thread, 0);
2803 else if (! thread_if_true)
2804 delay_list
2805 = steal_delay_list_from_fallthrough (insn, condition,
2806 PATTERN (trial),
2807 delay_list, &set, &needed,
2808 &opposite_needed, slots_to_fill,
2809 pslots_filled, &must_annul);
2812 /* If we haven't found anything for this delay slot and it is very
2813 likely that the branch will be taken, see if the insn at our target
2814 increments or decrements a register with an increment that does not
2815 depend on the destination register. If so, try to place the opposite
2816 arithmetic insn after the jump insn and put the arithmetic insn in the
2817 delay slot. If we can't do this, return. */
2818 if (delay_list == 0 && likely && new_thread
2819 && GET_CODE (new_thread) == INSN
2820 && GET_CODE (PATTERN (new_thread)) != ASM_INPUT
2821 && asm_noperands (PATTERN (new_thread)) < 0)
2823 rtx pat = PATTERN (new_thread);
2824 rtx dest;
2825 rtx src;
2827 trial = new_thread;
2828 pat = PATTERN (trial);
2830 if (GET_CODE (trial) != INSN
2831 || GET_CODE (pat) != SET
2832 || ! eligible_for_delay (insn, 0, trial, flags)
2833 || can_throw_internal (trial))
2834 return 0;
2836 dest = SET_DEST (pat), src = SET_SRC (pat);
2837 if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2838 && rtx_equal_p (XEXP (src, 0), dest)
2839 && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2840 && ! side_effects_p (pat))
2842 rtx other = XEXP (src, 1);
2843 rtx new_arith;
2844 rtx ninsn;
2846 /* If this is a constant adjustment, use the same code with
2847 the negated constant. Otherwise, reverse the sense of the
2848 arithmetic. */
2849 if (GET_CODE (other) == CONST_INT)
2850 new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2851 negate_rtx (GET_MODE (src), other));
2852 else
2853 new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2854 GET_MODE (src), dest, other);
2856 ninsn = emit_insn_after (gen_rtx_SET (VOIDmode, dest, new_arith),
2857 insn);
2859 if (recog_memoized (ninsn) < 0
2860 || (extract_insn (ninsn), ! constrain_operands (1)))
2862 delete_related_insns (ninsn);
2863 return 0;
2866 if (own_thread)
2868 update_block (trial, thread);
2869 if (trial == thread)
2871 thread = next_active_insn (thread);
2872 if (new_thread == trial)
2873 new_thread = thread;
2875 delete_related_insns (trial);
2877 else
2878 new_thread = next_active_insn (trial);
2880 ninsn = own_thread ? trial : copy_rtx (trial);
2881 if (thread_if_true)
2882 INSN_FROM_TARGET_P (ninsn) = 1;
2884 delay_list = add_to_delay_list (ninsn, NULL_RTX);
2885 (*pslots_filled)++;
2889 if (delay_list && must_annul)
2890 INSN_ANNULLED_BRANCH_P (insn) = 1;
2892 /* If we are to branch into the middle of this thread, find an appropriate
2893 label or make a new one if none, and redirect INSN to it. If we hit the
2894 end of the function, use the end-of-function label. */
2895 if (new_thread != thread)
2897 rtx label;
2899 if (! thread_if_true)
2900 abort ();
2902 if (new_thread && GET_CODE (new_thread) == JUMP_INSN
2903 && (simplejump_p (new_thread)
2904 || GET_CODE (PATTERN (new_thread)) == RETURN)
2905 && redirect_with_delay_list_safe_p (insn,
2906 JUMP_LABEL (new_thread),
2907 delay_list))
2908 new_thread = follow_jumps (JUMP_LABEL (new_thread));
2910 if (new_thread == 0)
2911 label = find_end_label ();
2912 else if (GET_CODE (new_thread) == CODE_LABEL)
2913 label = new_thread;
2914 else
2915 label = get_label_before (new_thread);
2917 reorg_redirect_jump (insn, label);
2920 return delay_list;
2923 /* Make another attempt to find insns to place in delay slots.
2925 We previously looked for insns located in front of the delay insn
2926 and, for non-jump delay insns, located behind the delay insn.
2928 Here only try to schedule jump insns and try to move insns from either
2929 the target or the following insns into the delay slot. If annulling is
2930 supported, we will be likely to do this. Otherwise, we can do this only
2931 if safe. */
2933 static void
2934 fill_eager_delay_slots (void)
2936 rtx insn;
2937 int i;
2938 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2940 for (i = 0; i < num_unfilled_slots; i++)
2942 rtx condition;
2943 rtx target_label, insn_at_target, fallthrough_insn;
2944 rtx delay_list = 0;
2945 int own_target;
2946 int own_fallthrough;
2947 int prediction, slots_to_fill, slots_filled;
2949 insn = unfilled_slots_base[i];
2950 if (insn == 0
2951 || INSN_DELETED_P (insn)
2952 || GET_CODE (insn) != JUMP_INSN
2953 || ! (condjump_p (insn) || condjump_in_parallel_p (insn)))
2954 continue;
2956 slots_to_fill = num_delay_slots (insn);
2957 /* Some machine description have defined instructions to have
2958 delay slots only in certain circumstances which may depend on
2959 nearby insns (which change due to reorg's actions).
2961 For example, the PA port normally has delay slots for unconditional
2962 jumps.
2964 However, the PA port claims such jumps do not have a delay slot
2965 if they are immediate successors of certain CALL_INSNs. This
2966 allows the port to favor filling the delay slot of the call with
2967 the unconditional jump. */
2968 if (slots_to_fill == 0)
2969 continue;
2971 slots_filled = 0;
2972 target_label = JUMP_LABEL (insn);
2973 condition = get_branch_condition (insn, target_label);
2975 if (condition == 0)
2976 continue;
2978 /* Get the next active fallthrough and target insns and see if we own
2979 them. Then see whether the branch is likely true. We don't need
2980 to do a lot of this for unconditional branches. */
2982 insn_at_target = next_active_insn (target_label);
2983 own_target = own_thread_p (target_label, target_label, 0);
2985 if (condition == const_true_rtx)
2987 own_fallthrough = 0;
2988 fallthrough_insn = 0;
2989 prediction = 2;
2991 else
2993 fallthrough_insn = next_active_insn (insn);
2994 own_fallthrough = own_thread_p (NEXT_INSN (insn), NULL_RTX, 1);
2995 prediction = mostly_true_jump (insn, condition);
2998 /* If this insn is expected to branch, first try to get insns from our
2999 target, then our fallthrough insns. If it is not expected to branch,
3000 try the other order. */
3002 if (prediction > 0)
3004 delay_list
3005 = fill_slots_from_thread (insn, condition, insn_at_target,
3006 fallthrough_insn, prediction == 2, 1,
3007 own_target,
3008 slots_to_fill, &slots_filled, delay_list);
3010 if (delay_list == 0 && own_fallthrough)
3012 /* Even though we didn't find anything for delay slots,
3013 we might have found a redundant insn which we deleted
3014 from the thread that was filled. So we have to recompute
3015 the next insn at the target. */
3016 target_label = JUMP_LABEL (insn);
3017 insn_at_target = next_active_insn (target_label);
3019 delay_list
3020 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3021 insn_at_target, 0, 0,
3022 own_fallthrough,
3023 slots_to_fill, &slots_filled,
3024 delay_list);
3027 else
3029 if (own_fallthrough)
3030 delay_list
3031 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3032 insn_at_target, 0, 0,
3033 own_fallthrough,
3034 slots_to_fill, &slots_filled,
3035 delay_list);
3037 if (delay_list == 0)
3038 delay_list
3039 = fill_slots_from_thread (insn, condition, insn_at_target,
3040 next_active_insn (insn), 0, 1,
3041 own_target,
3042 slots_to_fill, &slots_filled,
3043 delay_list);
3046 if (delay_list)
3047 unfilled_slots_base[i]
3048 = emit_delay_sequence (insn, delay_list, slots_filled);
3050 if (slots_to_fill == slots_filled)
3051 unfilled_slots_base[i] = 0;
3053 note_delay_statistics (slots_filled, 1);
3057 /* Once we have tried two ways to fill a delay slot, make a pass over the
3058 code to try to improve the results and to do such things as more jump
3059 threading. */
3061 static void
3062 relax_delay_slots (rtx first)
3064 rtx insn, next, pat;
3065 rtx trial, delay_insn, target_label;
3067 /* Look at every JUMP_INSN and see if we can improve it. */
3068 for (insn = first; insn; insn = next)
3070 rtx other;
3072 next = next_active_insn (insn);
3074 /* If this is a jump insn, see if it now jumps to a jump, jumps to
3075 the next insn, or jumps to a label that is not the last of a
3076 group of consecutive labels. */
3077 if (GET_CODE (insn) == JUMP_INSN
3078 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3079 && (target_label = JUMP_LABEL (insn)) != 0)
3081 target_label = follow_jumps (target_label);
3082 target_label = prev_label (next_active_insn (target_label));
3084 if (target_label == 0)
3085 target_label = find_end_label ();
3087 if (next_active_insn (target_label) == next
3088 && ! condjump_in_parallel_p (insn))
3090 delete_jump (insn);
3091 continue;
3094 if (target_label != JUMP_LABEL (insn))
3095 reorg_redirect_jump (insn, target_label);
3097 /* See if this jump branches around an unconditional jump.
3098 If so, invert this jump and point it to the target of the
3099 second jump. */
3100 if (next && GET_CODE (next) == JUMP_INSN
3101 && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3102 && next_active_insn (target_label) == next_active_insn (next)
3103 && no_labels_between_p (insn, next))
3105 rtx label = JUMP_LABEL (next);
3107 /* Be careful how we do this to avoid deleting code or
3108 labels that are momentarily dead. See similar optimization
3109 in jump.c.
3111 We also need to ensure we properly handle the case when
3112 invert_jump fails. */
3114 ++LABEL_NUSES (target_label);
3115 if (label)
3116 ++LABEL_NUSES (label);
3118 if (invert_jump (insn, label, 1))
3120 delete_related_insns (next);
3121 next = insn;
3124 if (label)
3125 --LABEL_NUSES (label);
3127 if (--LABEL_NUSES (target_label) == 0)
3128 delete_related_insns (target_label);
3130 continue;
3134 /* If this is an unconditional jump and the previous insn is a
3135 conditional jump, try reversing the condition of the previous
3136 insn and swapping our targets. The next pass might be able to
3137 fill the slots.
3139 Don't do this if we expect the conditional branch to be true, because
3140 we would then be making the more common case longer. */
3142 if (GET_CODE (insn) == JUMP_INSN
3143 && (simplejump_p (insn) || GET_CODE (PATTERN (insn)) == RETURN)
3144 && (other = prev_active_insn (insn)) != 0
3145 && (condjump_p (other) || condjump_in_parallel_p (other))
3146 && no_labels_between_p (other, insn)
3147 && 0 > mostly_true_jump (other,
3148 get_branch_condition (other,
3149 JUMP_LABEL (other))))
3151 rtx other_target = JUMP_LABEL (other);
3152 target_label = JUMP_LABEL (insn);
3154 if (invert_jump (other, target_label, 0))
3155 reorg_redirect_jump (insn, other_target);
3158 /* Now look only at cases where we have filled a delay slot. */
3159 if (GET_CODE (insn) != INSN
3160 || GET_CODE (PATTERN (insn)) != SEQUENCE)
3161 continue;
3163 pat = PATTERN (insn);
3164 delay_insn = XVECEXP (pat, 0, 0);
3166 /* See if the first insn in the delay slot is redundant with some
3167 previous insn. Remove it from the delay slot if so; then set up
3168 to reprocess this insn. */
3169 if (redundant_insn (XVECEXP (pat, 0, 1), delay_insn, 0))
3171 delete_from_delay_slot (XVECEXP (pat, 0, 1));
3172 next = prev_active_insn (next);
3173 continue;
3176 /* See if we have a RETURN insn with a filled delay slot followed
3177 by a RETURN insn with an unfilled a delay slot. If so, we can delete
3178 the first RETURN (but not it's delay insn). This gives the same
3179 effect in fewer instructions.
3181 Only do so if optimizing for size since this results in slower, but
3182 smaller code. */
3183 if (optimize_size
3184 && GET_CODE (PATTERN (delay_insn)) == RETURN
3185 && next
3186 && GET_CODE (next) == JUMP_INSN
3187 && GET_CODE (PATTERN (next)) == RETURN)
3189 rtx after;
3190 int i;
3192 /* Delete the RETURN and just execute the delay list insns.
3194 We do this by deleting the INSN containing the SEQUENCE, then
3195 re-emitting the insns separately, and then deleting the RETURN.
3196 This allows the count of the jump target to be properly
3197 decremented. */
3199 /* Clear the from target bit, since these insns are no longer
3200 in delay slots. */
3201 for (i = 0; i < XVECLEN (pat, 0); i++)
3202 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3204 trial = PREV_INSN (insn);
3205 delete_related_insns (insn);
3206 if (GET_CODE (pat) != SEQUENCE)
3207 abort ();
3208 after = trial;
3209 for (i = 0; i < XVECLEN (pat, 0); i++)
3211 rtx this_insn = XVECEXP (pat, 0, i);
3212 add_insn_after (this_insn, after);
3213 after = this_insn;
3215 delete_scheduled_jump (delay_insn);
3216 continue;
3219 /* Now look only at the cases where we have a filled JUMP_INSN. */
3220 if (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) != JUMP_INSN
3221 || ! (condjump_p (XVECEXP (PATTERN (insn), 0, 0))
3222 || condjump_in_parallel_p (XVECEXP (PATTERN (insn), 0, 0))))
3223 continue;
3225 target_label = JUMP_LABEL (delay_insn);
3227 if (target_label)
3229 /* If this jump goes to another unconditional jump, thread it, but
3230 don't convert a jump into a RETURN here. */
3231 trial = follow_jumps (target_label);
3232 /* We use next_real_insn instead of next_active_insn, so that
3233 the special USE insns emitted by reorg won't be ignored.
3234 If they are ignored, then they will get deleted if target_label
3235 is now unreachable, and that would cause mark_target_live_regs
3236 to fail. */
3237 trial = prev_label (next_real_insn (trial));
3238 if (trial == 0 && target_label != 0)
3239 trial = find_end_label ();
3241 if (trial != target_label
3242 && redirect_with_delay_slots_safe_p (delay_insn, trial, insn))
3244 reorg_redirect_jump (delay_insn, trial);
3245 target_label = trial;
3248 /* If the first insn at TARGET_LABEL is redundant with a previous
3249 insn, redirect the jump to the following insn process again. */
3250 trial = next_active_insn (target_label);
3251 if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3252 && redundant_insn (trial, insn, 0)
3253 && ! can_throw_internal (trial))
3255 rtx tmp;
3257 /* Figure out where to emit the special USE insn so we don't
3258 later incorrectly compute register live/death info. */
3259 tmp = next_active_insn (trial);
3260 if (tmp == 0)
3261 tmp = find_end_label ();
3263 /* Insert the special USE insn and update dataflow info. */
3264 update_block (trial, tmp);
3266 /* Now emit a label before the special USE insn, and
3267 redirect our jump to the new label. */
3268 target_label = get_label_before (PREV_INSN (tmp));
3269 reorg_redirect_jump (delay_insn, target_label);
3270 next = insn;
3271 continue;
3274 /* Similarly, if it is an unconditional jump with one insn in its
3275 delay list and that insn is redundant, thread the jump. */
3276 if (trial && GET_CODE (PATTERN (trial)) == SEQUENCE
3277 && XVECLEN (PATTERN (trial), 0) == 2
3278 && GET_CODE (XVECEXP (PATTERN (trial), 0, 0)) == JUMP_INSN
3279 && (simplejump_p (XVECEXP (PATTERN (trial), 0, 0))
3280 || GET_CODE (PATTERN (XVECEXP (PATTERN (trial), 0, 0))) == RETURN)
3281 && redundant_insn (XVECEXP (PATTERN (trial), 0, 1), insn, 0))
3283 target_label = JUMP_LABEL (XVECEXP (PATTERN (trial), 0, 0));
3284 if (target_label == 0)
3285 target_label = find_end_label ();
3287 if (redirect_with_delay_slots_safe_p (delay_insn, target_label,
3288 insn))
3290 reorg_redirect_jump (delay_insn, target_label);
3291 next = insn;
3292 continue;
3297 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3298 && prev_active_insn (target_label) == insn
3299 && ! condjump_in_parallel_p (delay_insn)
3300 #ifdef HAVE_cc0
3301 /* If the last insn in the delay slot sets CC0 for some insn,
3302 various code assumes that it is in a delay slot. We could
3303 put it back where it belonged and delete the register notes,
3304 but it doesn't seem worthwhile in this uncommon case. */
3305 && ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3306 REG_CC_USER, NULL_RTX)
3307 #endif
3310 rtx after;
3311 int i;
3313 /* All this insn does is execute its delay list and jump to the
3314 following insn. So delete the jump and just execute the delay
3315 list insns.
3317 We do this by deleting the INSN containing the SEQUENCE, then
3318 re-emitting the insns separately, and then deleting the jump.
3319 This allows the count of the jump target to be properly
3320 decremented. */
3322 /* Clear the from target bit, since these insns are no longer
3323 in delay slots. */
3324 for (i = 0; i < XVECLEN (pat, 0); i++)
3325 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3327 trial = PREV_INSN (insn);
3328 delete_related_insns (insn);
3329 if (GET_CODE (pat) != SEQUENCE)
3330 abort ();
3331 after = trial;
3332 for (i = 0; i < XVECLEN (pat, 0); i++)
3334 rtx this_insn = XVECEXP (pat, 0, i);
3335 add_insn_after (this_insn, after);
3336 after = this_insn;
3338 delete_scheduled_jump (delay_insn);
3339 continue;
3342 /* See if this is an unconditional jump around a single insn which is
3343 identical to the one in its delay slot. In this case, we can just
3344 delete the branch and the insn in its delay slot. */
3345 if (next && GET_CODE (next) == INSN
3346 && prev_label (next_active_insn (next)) == target_label
3347 && simplejump_p (insn)
3348 && XVECLEN (pat, 0) == 2
3349 && rtx_equal_p (PATTERN (next), PATTERN (XVECEXP (pat, 0, 1))))
3351 delete_related_insns (insn);
3352 continue;
3355 /* See if this jump (with its delay slots) branches around another
3356 jump (without delay slots). If so, invert this jump and point
3357 it to the target of the second jump. We cannot do this for
3358 annulled jumps, though. Again, don't convert a jump to a RETURN
3359 here. */
3360 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3361 && next && GET_CODE (next) == JUMP_INSN
3362 && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3363 && next_active_insn (target_label) == next_active_insn (next)
3364 && no_labels_between_p (insn, next))
3366 rtx label = JUMP_LABEL (next);
3367 rtx old_label = JUMP_LABEL (delay_insn);
3369 if (label == 0)
3370 label = find_end_label ();
3372 /* find_end_label can generate a new label. Check this first. */
3373 if (no_labels_between_p (insn, next)
3374 && redirect_with_delay_slots_safe_p (delay_insn, label, insn))
3376 /* Be careful how we do this to avoid deleting code or labels
3377 that are momentarily dead. See similar optimization in
3378 jump.c */
3379 if (old_label)
3380 ++LABEL_NUSES (old_label);
3382 if (invert_jump (delay_insn, label, 1))
3384 int i;
3386 /* Must update the INSN_FROM_TARGET_P bits now that
3387 the branch is reversed, so that mark_target_live_regs
3388 will handle the delay slot insn correctly. */
3389 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
3391 rtx slot = XVECEXP (PATTERN (insn), 0, i);
3392 INSN_FROM_TARGET_P (slot) = ! INSN_FROM_TARGET_P (slot);
3395 delete_related_insns (next);
3396 next = insn;
3399 if (old_label && --LABEL_NUSES (old_label) == 0)
3400 delete_related_insns (old_label);
3401 continue;
3405 /* If we own the thread opposite the way this insn branches, see if we
3406 can merge its delay slots with following insns. */
3407 if (INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3408 && own_thread_p (NEXT_INSN (insn), 0, 1))
3409 try_merge_delay_insns (insn, next);
3410 else if (! INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3411 && own_thread_p (target_label, target_label, 0))
3412 try_merge_delay_insns (insn, next_active_insn (target_label));
3414 /* If we get here, we haven't deleted INSN. But we may have deleted
3415 NEXT, so recompute it. */
3416 next = next_active_insn (insn);
3420 #ifdef HAVE_return
3422 /* Look for filled jumps to the end of function label. We can try to convert
3423 them into RETURN insns if the insns in the delay slot are valid for the
3424 RETURN as well. */
3426 static void
3427 make_return_insns (rtx first)
3429 rtx insn, jump_insn, pat;
3430 rtx real_return_label = end_of_function_label;
3431 int slots, i;
3433 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3434 /* If a previous pass filled delay slots in the epilogue, things get a
3435 bit more complicated, as those filler insns would generally (without
3436 data flow analysis) have to be executed after any existing branch
3437 delay slot filler insns. It is also unknown whether such a
3438 transformation would actually be profitable. Note that the existing
3439 code only cares for branches with (some) filled delay slots. */
3440 if (current_function_epilogue_delay_list != NULL)
3441 return;
3442 #endif
3444 /* See if there is a RETURN insn in the function other than the one we
3445 made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3446 into a RETURN to jump to it. */
3447 for (insn = first; insn; insn = NEXT_INSN (insn))
3448 if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) == RETURN)
3450 real_return_label = get_label_before (insn);
3451 break;
3454 /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3455 was equal to END_OF_FUNCTION_LABEL. */
3456 LABEL_NUSES (real_return_label)++;
3458 /* Clear the list of insns to fill so we can use it. */
3459 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3461 for (insn = first; insn; insn = NEXT_INSN (insn))
3463 int flags;
3465 /* Only look at filled JUMP_INSNs that go to the end of function
3466 label. */
3467 if (GET_CODE (insn) != INSN
3468 || GET_CODE (PATTERN (insn)) != SEQUENCE
3469 || GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) != JUMP_INSN
3470 || JUMP_LABEL (XVECEXP (PATTERN (insn), 0, 0)) != end_of_function_label)
3471 continue;
3473 pat = PATTERN (insn);
3474 jump_insn = XVECEXP (pat, 0, 0);
3476 /* If we can't make the jump into a RETURN, try to redirect it to the best
3477 RETURN and go on to the next insn. */
3478 if (! reorg_redirect_jump (jump_insn, NULL_RTX))
3480 /* Make sure redirecting the jump will not invalidate the delay
3481 slot insns. */
3482 if (redirect_with_delay_slots_safe_p (jump_insn,
3483 real_return_label,
3484 insn))
3485 reorg_redirect_jump (jump_insn, real_return_label);
3486 continue;
3489 /* See if this RETURN can accept the insns current in its delay slot.
3490 It can if it has more or an equal number of slots and the contents
3491 of each is valid. */
3493 flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3494 slots = num_delay_slots (jump_insn);
3495 if (slots >= XVECLEN (pat, 0) - 1)
3497 for (i = 1; i < XVECLEN (pat, 0); i++)
3498 if (! (
3499 #ifdef ANNUL_IFFALSE_SLOTS
3500 (INSN_ANNULLED_BRANCH_P (jump_insn)
3501 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3502 ? eligible_for_annul_false (jump_insn, i - 1,
3503 XVECEXP (pat, 0, i), flags) :
3504 #endif
3505 #ifdef ANNUL_IFTRUE_SLOTS
3506 (INSN_ANNULLED_BRANCH_P (jump_insn)
3507 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3508 ? eligible_for_annul_true (jump_insn, i - 1,
3509 XVECEXP (pat, 0, i), flags) :
3510 #endif
3511 eligible_for_delay (jump_insn, i - 1,
3512 XVECEXP (pat, 0, i), flags)))
3513 break;
3515 else
3516 i = 0;
3518 if (i == XVECLEN (pat, 0))
3519 continue;
3521 /* We have to do something with this insn. If it is an unconditional
3522 RETURN, delete the SEQUENCE and output the individual insns,
3523 followed by the RETURN. Then set things up so we try to find
3524 insns for its delay slots, if it needs some. */
3525 if (GET_CODE (PATTERN (jump_insn)) == RETURN)
3527 rtx prev = PREV_INSN (insn);
3529 delete_related_insns (insn);
3530 for (i = 1; i < XVECLEN (pat, 0); i++)
3531 prev = emit_insn_after (PATTERN (XVECEXP (pat, 0, i)), prev);
3533 insn = emit_jump_insn_after (PATTERN (jump_insn), prev);
3534 emit_barrier_after (insn);
3536 if (slots)
3537 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3539 else
3540 /* It is probably more efficient to keep this with its current
3541 delay slot as a branch to a RETURN. */
3542 reorg_redirect_jump (jump_insn, real_return_label);
3545 /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3546 new delay slots we have created. */
3547 if (--LABEL_NUSES (real_return_label) == 0)
3548 delete_related_insns (real_return_label);
3550 fill_simple_delay_slots (1);
3551 fill_simple_delay_slots (0);
3553 #endif
3555 /* Try to find insns to place in delay slots. */
3557 void
3558 dbr_schedule (rtx first, FILE *file)
3560 rtx insn, next, epilogue_insn = 0;
3561 int i;
3562 #if 0
3563 int old_flag_no_peephole = flag_no_peephole;
3565 /* Execute `final' once in prescan mode to delete any insns that won't be
3566 used. Don't let final try to do any peephole optimization--it will
3567 ruin dataflow information for this pass. */
3569 flag_no_peephole = 1;
3570 final (first, 0, NO_DEBUG, 1, 1);
3571 flag_no_peephole = old_flag_no_peephole;
3572 #endif
3574 /* If the current function has no insns other than the prologue and
3575 epilogue, then do not try to fill any delay slots. */
3576 if (n_basic_blocks == 0)
3577 return;
3579 /* Find the highest INSN_UID and allocate and initialize our map from
3580 INSN_UID's to position in code. */
3581 for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3583 if (INSN_UID (insn) > max_uid)
3584 max_uid = INSN_UID (insn);
3585 if (GET_CODE (insn) == NOTE
3586 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
3587 epilogue_insn = insn;
3590 uid_to_ruid = xmalloc ((max_uid + 1) * sizeof (int));
3591 for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3592 uid_to_ruid[INSN_UID (insn)] = i;
3594 /* Initialize the list of insns that need filling. */
3595 if (unfilled_firstobj == 0)
3597 gcc_obstack_init (&unfilled_slots_obstack);
3598 unfilled_firstobj = obstack_alloc (&unfilled_slots_obstack, 0);
3601 for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3603 rtx target;
3605 INSN_ANNULLED_BRANCH_P (insn) = 0;
3606 INSN_FROM_TARGET_P (insn) = 0;
3608 /* Skip vector tables. We can't get attributes for them. */
3609 if (GET_CODE (insn) == JUMP_INSN
3610 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
3611 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
3612 continue;
3614 if (num_delay_slots (insn) > 0)
3615 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3617 /* Ensure all jumps go to the last of a set of consecutive labels. */
3618 if (GET_CODE (insn) == JUMP_INSN
3619 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3620 && JUMP_LABEL (insn) != 0
3621 && ((target = prev_label (next_active_insn (JUMP_LABEL (insn))))
3622 != JUMP_LABEL (insn)))
3623 redirect_jump (insn, target, 1);
3626 init_resource_info (epilogue_insn);
3628 /* Show we haven't computed an end-of-function label yet. */
3629 end_of_function_label = 0;
3631 /* Initialize the statistics for this function. */
3632 memset (num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3633 memset (num_filled_delays, 0, sizeof num_filled_delays);
3635 /* Now do the delay slot filling. Try everything twice in case earlier
3636 changes make more slots fillable. */
3638 for (reorg_pass_number = 0;
3639 reorg_pass_number < MAX_REORG_PASSES;
3640 reorg_pass_number++)
3642 fill_simple_delay_slots (1);
3643 fill_simple_delay_slots (0);
3644 fill_eager_delay_slots ();
3645 relax_delay_slots (first);
3648 /* Delete any USE insns made by update_block; subsequent passes don't need
3649 them or know how to deal with them. */
3650 for (insn = first; insn; insn = next)
3652 next = NEXT_INSN (insn);
3654 if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE
3655 && INSN_P (XEXP (PATTERN (insn), 0)))
3656 next = delete_related_insns (insn);
3659 /* If we made an end of function label, indicate that it is now
3660 safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3661 If it is now unused, delete it. */
3662 if (end_of_function_label && --LABEL_NUSES (end_of_function_label) == 0)
3663 delete_related_insns (end_of_function_label);
3665 #ifdef HAVE_return
3666 if (HAVE_return && end_of_function_label != 0)
3667 make_return_insns (first);
3668 #endif
3670 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3672 /* It is not clear why the line below is needed, but it does seem to be. */
3673 unfilled_firstobj = obstack_alloc (&unfilled_slots_obstack, 0);
3675 if (file)
3677 int i, j, need_comma;
3678 int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3679 int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
3681 for (reorg_pass_number = 0;
3682 reorg_pass_number < MAX_REORG_PASSES;
3683 reorg_pass_number++)
3685 fprintf (file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
3686 for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
3688 need_comma = 0;
3689 fprintf (file, ";; Reorg function #%d\n", i);
3691 fprintf (file, ";; %d insns needing delay slots\n;; ",
3692 num_insns_needing_delays[i][reorg_pass_number]);
3694 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3695 if (num_filled_delays[i][j][reorg_pass_number])
3697 if (need_comma)
3698 fprintf (file, ", ");
3699 need_comma = 1;
3700 fprintf (file, "%d got %d delays",
3701 num_filled_delays[i][j][reorg_pass_number], j);
3703 fprintf (file, "\n");
3706 memset (total_delay_slots, 0, sizeof total_delay_slots);
3707 memset (total_annul_slots, 0, sizeof total_annul_slots);
3708 for (insn = first; insn; insn = NEXT_INSN (insn))
3710 if (! INSN_DELETED_P (insn)
3711 && GET_CODE (insn) == INSN
3712 && GET_CODE (PATTERN (insn)) != USE
3713 && GET_CODE (PATTERN (insn)) != CLOBBER)
3715 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3717 j = XVECLEN (PATTERN (insn), 0) - 1;
3718 if (j > MAX_DELAY_HISTOGRAM)
3719 j = MAX_DELAY_HISTOGRAM;
3720 if (INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (insn), 0, 0)))
3721 total_annul_slots[j]++;
3722 else
3723 total_delay_slots[j]++;
3725 else if (num_delay_slots (insn) > 0)
3726 total_delay_slots[0]++;
3729 fprintf (file, ";; Reorg totals: ");
3730 need_comma = 0;
3731 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3733 if (total_delay_slots[j])
3735 if (need_comma)
3736 fprintf (file, ", ");
3737 need_comma = 1;
3738 fprintf (file, "%d got %d delays", total_delay_slots[j], j);
3741 fprintf (file, "\n");
3742 #if defined (ANNUL_IFTRUE_SLOTS) || defined (ANNUL_IFFALSE_SLOTS)
3743 fprintf (file, ";; Reorg annuls: ");
3744 need_comma = 0;
3745 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3747 if (total_annul_slots[j])
3749 if (need_comma)
3750 fprintf (file, ", ");
3751 need_comma = 1;
3752 fprintf (file, "%d got %d delays", total_annul_slots[j], j);
3755 fprintf (file, "\n");
3756 #endif
3757 fprintf (file, "\n");
3760 /* For all JUMP insns, fill in branch prediction notes, so that during
3761 assembler output a target can set branch prediction bits in the code.
3762 We have to do this now, as up until this point the destinations of
3763 JUMPS can be moved around and changed, but past right here that cannot
3764 happen. */
3765 for (insn = first; insn; insn = NEXT_INSN (insn))
3767 int pred_flags;
3769 if (GET_CODE (insn) == INSN)
3771 rtx pat = PATTERN (insn);
3773 if (GET_CODE (pat) == SEQUENCE)
3774 insn = XVECEXP (pat, 0, 0);
3776 if (GET_CODE (insn) != JUMP_INSN)
3777 continue;
3779 pred_flags = get_jump_flags (insn, JUMP_LABEL (insn));
3780 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_BR_PRED,
3781 GEN_INT (pred_flags),
3782 REG_NOTES (insn));
3784 free_resource_info ();
3785 free (uid_to_ruid);
3786 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3787 /* SPARC assembler, for instance, emit warning when debug info is output
3788 into the delay slot. */
3790 rtx link;
3792 for (link = current_function_epilogue_delay_list;
3793 link;
3794 link = XEXP (link, 1))
3795 INSN_LOCATOR (XEXP (link, 0)) = 0;
3797 #endif
3799 #endif /* DELAY_SLOTS */