Missed one in last change.
[official-gcc.git] / gcc / reorg.c
blob37a8214032679696ab7ee7972733f5f6cebdec29
1 /* Perform instruction reorganizations for delay slot filling.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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 PARAMS ((rtx, int));
181 static int resource_conflicts_p PARAMS ((struct resources *,
182 struct resources *));
183 static int insn_references_resource_p PARAMS ((rtx, struct resources *, int));
184 static int insn_sets_resource_p PARAMS ((rtx, struct resources *, int));
185 static rtx find_end_label PARAMS ((void));
186 static rtx emit_delay_sequence PARAMS ((rtx, rtx, int));
187 static rtx add_to_delay_list PARAMS ((rtx, rtx));
188 static rtx delete_from_delay_slot PARAMS ((rtx));
189 static void delete_scheduled_jump PARAMS ((rtx));
190 static void note_delay_statistics PARAMS ((int, int));
191 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
192 static rtx optimize_skip PARAMS ((rtx));
193 #endif
194 static int get_jump_flags PARAMS ((rtx, rtx));
195 static int rare_destination PARAMS ((rtx));
196 static int mostly_true_jump PARAMS ((rtx, rtx));
197 static rtx get_branch_condition PARAMS ((rtx, rtx));
198 static int condition_dominates_p PARAMS ((rtx, rtx));
199 static int redirect_with_delay_slots_safe_p PARAMS ((rtx, rtx, rtx));
200 static int redirect_with_delay_list_safe_p PARAMS ((rtx, rtx, rtx));
201 static int check_annul_list_true_false PARAMS ((int, rtx));
202 static rtx steal_delay_list_from_target PARAMS ((rtx, rtx, rtx, rtx,
203 struct resources *,
204 struct resources *,
205 struct resources *,
206 int, int *, int *, rtx *));
207 static rtx steal_delay_list_from_fallthrough PARAMS ((rtx, rtx, rtx, rtx,
208 struct resources *,
209 struct resources *,
210 struct resources *,
211 int, int *, int *));
212 static void try_merge_delay_insns PARAMS ((rtx, rtx));
213 static rtx redundant_insn PARAMS ((rtx, rtx, rtx));
214 static int own_thread_p PARAMS ((rtx, rtx, int));
215 static void update_block PARAMS ((rtx, rtx));
216 static int reorg_redirect_jump PARAMS ((rtx, rtx));
217 static void update_reg_dead_notes PARAMS ((rtx, rtx));
218 static void fix_reg_dead_note PARAMS ((rtx, rtx));
219 static void update_reg_unused_notes PARAMS ((rtx, rtx));
220 static void fill_simple_delay_slots PARAMS ((int));
221 static rtx fill_slots_from_thread PARAMS ((rtx, rtx, rtx, rtx, int, int,
222 int, int, int *, rtx));
223 static void fill_eager_delay_slots PARAMS ((void));
224 static void relax_delay_slots PARAMS ((rtx));
225 #ifdef HAVE_return
226 static void make_return_insns PARAMS ((rtx));
227 #endif
229 /* Return TRUE if this insn should stop the search for insn to fill delay
230 slots. LABELS_P indicates that labels should terminate the search.
231 In all cases, jumps terminate the search. */
233 static int
234 stop_search_p (insn, labels_p)
235 rtx insn;
236 int labels_p;
238 if (insn == 0)
239 return 1;
241 switch (GET_CODE (insn))
243 case NOTE:
244 case CALL_INSN:
245 return 0;
247 case CODE_LABEL:
248 return labels_p;
250 case JUMP_INSN:
251 case BARRIER:
252 return 1;
254 case INSN:
255 /* OK unless it contains a delay slot or is an `asm' insn of some type.
256 We don't know anything about these. */
257 return (GET_CODE (PATTERN (insn)) == SEQUENCE
258 || GET_CODE (PATTERN (insn)) == ASM_INPUT
259 || asm_noperands (PATTERN (insn)) >= 0);
261 default:
262 abort ();
266 /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
267 resource set contains a volatile memory reference. Otherwise, return FALSE. */
269 static int
270 resource_conflicts_p (res1, res2)
271 struct resources *res1, *res2;
273 if ((res1->cc && res2->cc) || (res1->memory && res2->memory)
274 || (res1->unch_memory && res2->unch_memory)
275 || res1->volatil || res2->volatil)
276 return 1;
278 #ifdef HARD_REG_SET
279 return (res1->regs & res2->regs) != HARD_CONST (0);
280 #else
282 int i;
284 for (i = 0; i < HARD_REG_SET_LONGS; i++)
285 if ((res1->regs[i] & res2->regs[i]) != 0)
286 return 1;
287 return 0;
289 #endif
292 /* Return TRUE if any resource marked in RES, a `struct resources', is
293 referenced by INSN. If INCLUDE_DELAYED_EFFECTS is set, return if the called
294 routine is using those resources.
296 We compute this by computing all the resources referenced by INSN and
297 seeing if this conflicts with RES. It might be faster to directly check
298 ourselves, and this is the way it used to work, but it means duplicating
299 a large block of complex code. */
301 static int
302 insn_references_resource_p (insn, res, include_delayed_effects)
303 rtx insn;
304 struct resources *res;
305 int include_delayed_effects;
307 struct resources insn_res;
309 CLEAR_RESOURCE (&insn_res);
310 mark_referenced_resources (insn, &insn_res, include_delayed_effects);
311 return resource_conflicts_p (&insn_res, res);
314 /* Return TRUE if INSN modifies resources that are marked in RES.
315 INCLUDE_DELAYED_EFFECTS is set if the actions of that routine should be
316 included. CC0 is only modified if it is explicitly set; see comments
317 in front of mark_set_resources for details. */
319 static int
320 insn_sets_resource_p (insn, res, include_delayed_effects)
321 rtx insn;
322 struct resources *res;
323 int include_delayed_effects;
325 struct resources insn_sets;
327 CLEAR_RESOURCE (&insn_sets);
328 mark_set_resources (insn, &insn_sets, 0, include_delayed_effects);
329 return resource_conflicts_p (&insn_sets, res);
332 /* Find a label at the end of the function or before a RETURN. If there is
333 none, make one. */
335 static rtx
336 find_end_label ()
338 rtx insn;
340 /* If we found one previously, return it. */
341 if (end_of_function_label)
342 return end_of_function_label;
344 /* Otherwise, see if there is a label at the end of the function. If there
345 is, it must be that RETURN insns aren't needed, so that is our return
346 label and we don't have to do anything else. */
348 insn = get_last_insn ();
349 while (GET_CODE (insn) == NOTE
350 || (GET_CODE (insn) == INSN
351 && (GET_CODE (PATTERN (insn)) == USE
352 || GET_CODE (PATTERN (insn)) == CLOBBER)))
353 insn = PREV_INSN (insn);
355 /* When a target threads its epilogue we might already have a
356 suitable return insn. If so put a label before it for the
357 end_of_function_label. */
358 if (GET_CODE (insn) == BARRIER
359 && GET_CODE (PREV_INSN (insn)) == JUMP_INSN
360 && GET_CODE (PATTERN (PREV_INSN (insn))) == RETURN)
362 rtx temp = PREV_INSN (PREV_INSN (insn));
363 end_of_function_label = gen_label_rtx ();
364 LABEL_NUSES (end_of_function_label) = 0;
366 /* Put the label before an USE insns that may proceed the RETURN insn. */
367 while (GET_CODE (temp) == USE)
368 temp = PREV_INSN (temp);
370 emit_label_after (end_of_function_label, temp);
373 else if (GET_CODE (insn) == CODE_LABEL)
374 end_of_function_label = insn;
375 else
377 end_of_function_label = gen_label_rtx ();
378 LABEL_NUSES (end_of_function_label) = 0;
379 /* If the basic block reorder pass moves the return insn to
380 some other place try to locate it again and put our
381 end_of_function_label there. */
382 while (insn && ! (GET_CODE (insn) == JUMP_INSN
383 && (GET_CODE (PATTERN (insn)) == RETURN)))
384 insn = PREV_INSN (insn);
385 if (insn)
387 insn = PREV_INSN (insn);
389 /* Put the label before an USE insns that may proceed the
390 RETURN insn. */
391 while (GET_CODE (insn) == USE)
392 insn = PREV_INSN (insn);
394 emit_label_after (end_of_function_label, insn);
396 else
398 /* Otherwise, make a new label and emit a RETURN and BARRIER,
399 if needed. */
400 emit_label (end_of_function_label);
401 #ifdef HAVE_return
402 if (HAVE_return)
404 /* The return we make may have delay slots too. */
405 rtx insn = gen_return ();
406 insn = emit_jump_insn (insn);
407 emit_barrier ();
408 if (num_delay_slots (insn) > 0)
409 obstack_ptr_grow (&unfilled_slots_obstack, insn);
411 #endif
415 /* Show one additional use for this label so it won't go away until
416 we are done. */
417 ++LABEL_NUSES (end_of_function_label);
419 return end_of_function_label;
422 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
423 the pattern of INSN with the SEQUENCE.
425 Chain the insns so that NEXT_INSN of each insn in the sequence points to
426 the next and NEXT_INSN of the last insn in the sequence points to
427 the first insn after the sequence. Similarly for PREV_INSN. This makes
428 it easier to scan all insns.
430 Returns the SEQUENCE that replaces INSN. */
432 static rtx
433 emit_delay_sequence (insn, list, length)
434 rtx insn;
435 rtx list;
436 int length;
438 int i = 1;
439 rtx li;
440 int had_barrier = 0;
442 /* Allocate the rtvec to hold the insns and the SEQUENCE. */
443 rtvec seqv = rtvec_alloc (length + 1);
444 rtx seq = gen_rtx_SEQUENCE (VOIDmode, seqv);
445 rtx seq_insn = make_insn_raw (seq);
446 rtx first = get_insns ();
447 rtx last = get_last_insn ();
449 /* Make a copy of the insn having delay slots. */
450 rtx delay_insn = copy_rtx (insn);
452 /* If INSN is followed by a BARRIER, delete the BARRIER since it will only
453 confuse further processing. Update LAST in case it was the last insn.
454 We will put the BARRIER back in later. */
455 if (NEXT_INSN (insn) && GET_CODE (NEXT_INSN (insn)) == BARRIER)
457 delete_related_insns (NEXT_INSN (insn));
458 last = get_last_insn ();
459 had_barrier = 1;
462 /* Splice our SEQUENCE into the insn stream where INSN used to be. */
463 NEXT_INSN (seq_insn) = NEXT_INSN (insn);
464 PREV_INSN (seq_insn) = PREV_INSN (insn);
466 if (insn != last)
467 PREV_INSN (NEXT_INSN (seq_insn)) = seq_insn;
469 if (insn != first)
470 NEXT_INSN (PREV_INSN (seq_insn)) = seq_insn;
472 /* Note the calls to set_new_first_and_last_insn must occur after
473 SEQ_INSN has been completely spliced into the insn stream.
475 Otherwise CUR_INSN_UID will get set to an incorrect value because
476 set_new_first_and_last_insn will not find SEQ_INSN in the chain. */
477 if (insn == last)
478 set_new_first_and_last_insn (first, seq_insn);
480 if (insn == first)
481 set_new_first_and_last_insn (seq_insn, last);
483 /* Build our SEQUENCE and rebuild the insn chain. */
484 XVECEXP (seq, 0, 0) = delay_insn;
485 INSN_DELETED_P (delay_insn) = 0;
486 PREV_INSN (delay_insn) = PREV_INSN (seq_insn);
488 for (li = list; li; li = XEXP (li, 1), i++)
490 rtx tem = XEXP (li, 0);
491 rtx note, next;
493 /* Show that this copy of the insn isn't deleted. */
494 INSN_DELETED_P (tem) = 0;
496 XVECEXP (seq, 0, i) = tem;
497 PREV_INSN (tem) = XVECEXP (seq, 0, i - 1);
498 NEXT_INSN (XVECEXP (seq, 0, i - 1)) = tem;
500 /* SPARC assembler, for instance, emit warning when debug info is output
501 into the delay slot. */
502 if (INSN_LOCATOR (tem) && !INSN_LOCATOR (seq_insn))
503 INSN_LOCATOR (seq_insn) = INSN_LOCATOR (tem);
504 INSN_LOCATOR (tem) = 0;
506 for (note = REG_NOTES (tem); note; note = next)
508 next = XEXP (note, 1);
509 switch (REG_NOTE_KIND (note))
511 case REG_DEAD:
512 /* Remove any REG_DEAD notes because we can't rely on them now
513 that the insn has been moved. */
514 remove_note (tem, note);
515 break;
517 case REG_LABEL:
518 /* Keep the label reference count up to date. */
519 if (GET_CODE (XEXP (note, 0)) == CODE_LABEL)
520 LABEL_NUSES (XEXP (note, 0)) ++;
521 break;
523 default:
524 break;
529 NEXT_INSN (XVECEXP (seq, 0, length)) = NEXT_INSN (seq_insn);
531 /* If the previous insn is a SEQUENCE, update the NEXT_INSN pointer on the
532 last insn in that SEQUENCE to point to us. Similarly for the first
533 insn in the following insn if it is a SEQUENCE. */
535 if (PREV_INSN (seq_insn) && GET_CODE (PREV_INSN (seq_insn)) == INSN
536 && GET_CODE (PATTERN (PREV_INSN (seq_insn))) == SEQUENCE)
537 NEXT_INSN (XVECEXP (PATTERN (PREV_INSN (seq_insn)), 0,
538 XVECLEN (PATTERN (PREV_INSN (seq_insn)), 0) - 1))
539 = seq_insn;
541 if (NEXT_INSN (seq_insn) && GET_CODE (NEXT_INSN (seq_insn)) == INSN
542 && GET_CODE (PATTERN (NEXT_INSN (seq_insn))) == SEQUENCE)
543 PREV_INSN (XVECEXP (PATTERN (NEXT_INSN (seq_insn)), 0, 0)) = seq_insn;
545 /* If there used to be a BARRIER, put it back. */
546 if (had_barrier)
547 emit_barrier_after (seq_insn);
549 if (i != length + 1)
550 abort ();
552 return seq_insn;
555 /* Add INSN to DELAY_LIST and return the head of the new list. The list must
556 be in the order in which the insns are to be executed. */
558 static rtx
559 add_to_delay_list (insn, delay_list)
560 rtx insn;
561 rtx delay_list;
563 /* If we have an empty list, just make a new list element. If
564 INSN has its block number recorded, clear it since we may
565 be moving the insn to a new block. */
567 if (delay_list == 0)
569 clear_hashed_info_for_insn (insn);
570 return gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
573 /* Otherwise this must be an INSN_LIST. Add INSN to the end of the
574 list. */
575 XEXP (delay_list, 1) = add_to_delay_list (insn, XEXP (delay_list, 1));
577 return delay_list;
580 /* Delete INSN from the delay slot of the insn that it is in, which may
581 produce an insn with no delay slots. Return the new insn. */
583 static rtx
584 delete_from_delay_slot (insn)
585 rtx insn;
587 rtx trial, seq_insn, seq, prev;
588 rtx delay_list = 0;
589 int i;
591 /* We first must find the insn containing the SEQUENCE with INSN in its
592 delay slot. Do this by finding an insn, TRIAL, where
593 PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
595 for (trial = insn;
596 PREV_INSN (NEXT_INSN (trial)) == trial;
597 trial = NEXT_INSN (trial))
600 seq_insn = PREV_INSN (NEXT_INSN (trial));
601 seq = PATTERN (seq_insn);
603 /* Create a delay list consisting of all the insns other than the one
604 we are deleting (unless we were the only one). */
605 if (XVECLEN (seq, 0) > 2)
606 for (i = 1; i < XVECLEN (seq, 0); i++)
607 if (XVECEXP (seq, 0, i) != insn)
608 delay_list = add_to_delay_list (XVECEXP (seq, 0, i), delay_list);
610 /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
611 list, and rebuild the delay list if non-empty. */
612 prev = PREV_INSN (seq_insn);
613 trial = XVECEXP (seq, 0, 0);
614 delete_related_insns (seq_insn);
615 add_insn_after (trial, prev);
617 if (GET_CODE (trial) == JUMP_INSN
618 && (simplejump_p (trial) || GET_CODE (PATTERN (trial)) == RETURN))
619 emit_barrier_after (trial);
621 /* If there are any delay insns, remit them. Otherwise clear the
622 annul flag. */
623 if (delay_list)
624 trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2);
625 else if (GET_CODE (trial) == JUMP_INSN
626 || GET_CODE (trial) == CALL_INSN
627 || GET_CODE (trial) == INSN)
628 INSN_ANNULLED_BRANCH_P (trial) = 0;
630 INSN_FROM_TARGET_P (insn) = 0;
632 /* Show we need to fill this insn again. */
633 obstack_ptr_grow (&unfilled_slots_obstack, trial);
635 return trial;
638 /* Delete INSN, a JUMP_INSN. If it is a conditional jump, we must track down
639 the insn that sets CC0 for it and delete it too. */
641 static void
642 delete_scheduled_jump (insn)
643 rtx insn;
645 /* Delete the insn that sets cc0 for us. On machines without cc0, we could
646 delete the insn that sets the condition code, but it is hard to find it.
647 Since this case is rare anyway, don't bother trying; there would likely
648 be other insns that became dead anyway, which we wouldn't know to
649 delete. */
651 #ifdef HAVE_cc0
652 if (reg_mentioned_p (cc0_rtx, insn))
654 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
656 /* If a reg-note was found, it points to an insn to set CC0. This
657 insn is in the delay list of some other insn. So delete it from
658 the delay list it was in. */
659 if (note)
661 if (! FIND_REG_INC_NOTE (XEXP (note, 0), NULL_RTX)
662 && sets_cc0_p (PATTERN (XEXP (note, 0))) == 1)
663 delete_from_delay_slot (XEXP (note, 0));
665 else
667 /* The insn setting CC0 is our previous insn, but it may be in
668 a delay slot. It will be the last insn in the delay slot, if
669 it is. */
670 rtx trial = previous_insn (insn);
671 if (GET_CODE (trial) == NOTE)
672 trial = prev_nonnote_insn (trial);
673 if (sets_cc0_p (PATTERN (trial)) != 1
674 || FIND_REG_INC_NOTE (trial, NULL_RTX))
675 return;
676 if (PREV_INSN (NEXT_INSN (trial)) == trial)
677 delete_related_insns (trial);
678 else
679 delete_from_delay_slot (trial);
682 #endif
684 delete_related_insns (insn);
687 /* Counters for delay-slot filling. */
689 #define NUM_REORG_FUNCTIONS 2
690 #define MAX_DELAY_HISTOGRAM 3
691 #define MAX_REORG_PASSES 2
693 static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
695 static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
697 static int reorg_pass_number;
699 static void
700 note_delay_statistics (slots_filled, index)
701 int slots_filled, index;
703 num_insns_needing_delays[index][reorg_pass_number]++;
704 if (slots_filled > MAX_DELAY_HISTOGRAM)
705 slots_filled = MAX_DELAY_HISTOGRAM;
706 num_filled_delays[index][slots_filled][reorg_pass_number]++;
709 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
711 /* Optimize the following cases:
713 1. When a conditional branch skips over only one instruction,
714 use an annulling branch and put that insn in the delay slot.
715 Use either a branch that annuls when the condition if true or
716 invert the test with a branch that annuls when the condition is
717 false. This saves insns, since otherwise we must copy an insn
718 from the L1 target.
720 (orig) (skip) (otherwise)
721 Bcc.n L1 Bcc',a L1 Bcc,a L1'
722 insn insn insn2
723 L1: L1: L1:
724 insn2 insn2 insn2
725 insn3 insn3 L1':
726 insn3
728 2. When a conditional branch skips over only one instruction,
729 and after that, it unconditionally branches somewhere else,
730 perform the similar optimization. This saves executing the
731 second branch in the case where the inverted condition is true.
733 Bcc.n L1 Bcc',a L2
734 insn insn
735 L1: L1:
736 Bra L2 Bra L2
738 INSN is a JUMP_INSN.
740 This should be expanded to skip over N insns, where N is the number
741 of delay slots required. */
743 static rtx
744 optimize_skip (insn)
745 rtx insn;
747 rtx trial = next_nonnote_insn (insn);
748 rtx next_trial = next_active_insn (trial);
749 rtx delay_list = 0;
750 rtx target_label;
751 int flags;
753 flags = get_jump_flags (insn, JUMP_LABEL (insn));
755 if (trial == 0
756 || GET_CODE (trial) != INSN
757 || GET_CODE (PATTERN (trial)) == SEQUENCE
758 || recog_memoized (trial) < 0
759 || (! eligible_for_annul_false (insn, 0, trial, flags)
760 && ! eligible_for_annul_true (insn, 0, trial, flags))
761 || can_throw_internal (trial))
762 return 0;
764 /* There are two cases where we are just executing one insn (we assume
765 here that a branch requires only one insn; this should be generalized
766 at some point): Where the branch goes around a single insn or where
767 we have one insn followed by a branch to the same label we branch to.
768 In both of these cases, inverting the jump and annulling the delay
769 slot give the same effect in fewer insns. */
770 if ((next_trial == next_active_insn (JUMP_LABEL (insn))
771 && ! (next_trial == 0 && current_function_epilogue_delay_list != 0))
772 || (next_trial != 0
773 && GET_CODE (next_trial) == JUMP_INSN
774 && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)
775 && (simplejump_p (next_trial)
776 || GET_CODE (PATTERN (next_trial)) == RETURN)))
778 if (eligible_for_annul_false (insn, 0, trial, flags))
780 if (invert_jump (insn, JUMP_LABEL (insn), 1))
781 INSN_FROM_TARGET_P (trial) = 1;
782 else if (! eligible_for_annul_true (insn, 0, trial, flags))
783 return 0;
786 delay_list = add_to_delay_list (trial, NULL_RTX);
787 next_trial = next_active_insn (trial);
788 update_block (trial, trial);
789 delete_related_insns (trial);
791 /* Also, if we are targeting an unconditional
792 branch, thread our jump to the target of that branch. Don't
793 change this into a RETURN here, because it may not accept what
794 we have in the delay slot. We'll fix this up later. */
795 if (next_trial && GET_CODE (next_trial) == JUMP_INSN
796 && (simplejump_p (next_trial)
797 || GET_CODE (PATTERN (next_trial)) == RETURN))
799 target_label = JUMP_LABEL (next_trial);
800 if (target_label == 0)
801 target_label = find_end_label ();
803 /* Recompute the flags based on TARGET_LABEL since threading
804 the jump to TARGET_LABEL may change the direction of the
805 jump (which may change the circumstances in which the
806 delay slot is nullified). */
807 flags = get_jump_flags (insn, target_label);
808 if (eligible_for_annul_true (insn, 0, trial, flags))
809 reorg_redirect_jump (insn, target_label);
812 INSN_ANNULLED_BRANCH_P (insn) = 1;
815 return delay_list;
817 #endif
819 /* Encode and return branch direction and prediction information for
820 INSN assuming it will jump to LABEL.
822 Non conditional branches return no direction information and
823 are predicted as very likely taken. */
825 static int
826 get_jump_flags (insn, label)
827 rtx insn, label;
829 int flags;
831 /* get_jump_flags can be passed any insn with delay slots, these may
832 be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
833 direction information, and only if they are conditional jumps.
835 If LABEL is zero, then there is no way to determine the branch
836 direction. */
837 if (GET_CODE (insn) == JUMP_INSN
838 && (condjump_p (insn) || condjump_in_parallel_p (insn))
839 && INSN_UID (insn) <= max_uid
840 && label != 0
841 && INSN_UID (label) <= max_uid)
842 flags
843 = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
844 ? ATTR_FLAG_forward : ATTR_FLAG_backward;
845 /* No valid direction information. */
846 else
847 flags = 0;
849 /* If insn is a conditional branch call mostly_true_jump to get
850 determine the branch prediction.
852 Non conditional branches are predicted as very likely taken. */
853 if (GET_CODE (insn) == JUMP_INSN
854 && (condjump_p (insn) || condjump_in_parallel_p (insn)))
856 int prediction;
858 prediction = mostly_true_jump (insn, get_branch_condition (insn, label));
859 switch (prediction)
861 case 2:
862 flags |= (ATTR_FLAG_very_likely | ATTR_FLAG_likely);
863 break;
864 case 1:
865 flags |= ATTR_FLAG_likely;
866 break;
867 case 0:
868 flags |= ATTR_FLAG_unlikely;
869 break;
870 case -1:
871 flags |= (ATTR_FLAG_very_unlikely | ATTR_FLAG_unlikely);
872 break;
874 default:
875 abort ();
878 else
879 flags |= (ATTR_FLAG_very_likely | ATTR_FLAG_likely);
881 return flags;
884 /* Return 1 if INSN is a destination that will be branched to rarely (the
885 return point of a function); return 2 if DEST will be branched to very
886 rarely (a call to a function that doesn't return). Otherwise,
887 return 0. */
889 static int
890 rare_destination (insn)
891 rtx insn;
893 int jump_count = 0;
894 rtx next;
896 for (; insn; insn = next)
898 if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
899 insn = XVECEXP (PATTERN (insn), 0, 0);
901 next = NEXT_INSN (insn);
903 switch (GET_CODE (insn))
905 case CODE_LABEL:
906 return 0;
907 case BARRIER:
908 /* A BARRIER can either be after a JUMP_INSN or a CALL_INSN. We
909 don't scan past JUMP_INSNs, so any barrier we find here must
910 have been after a CALL_INSN and hence mean the call doesn't
911 return. */
912 return 2;
913 case JUMP_INSN:
914 if (GET_CODE (PATTERN (insn)) == RETURN)
915 return 1;
916 else if (simplejump_p (insn)
917 && jump_count++ < 10)
918 next = JUMP_LABEL (insn);
919 else
920 return 0;
922 default:
923 break;
927 /* If we got here it means we hit the end of the function. So this
928 is an unlikely destination. */
930 return 1;
933 /* Return truth value of the statement that this branch
934 is mostly taken. If we think that the branch is extremely likely
935 to be taken, we return 2. If the branch is slightly more likely to be
936 taken, return 1. If the branch is slightly less likely to be taken,
937 return 0 and if the branch is highly unlikely to be taken, return -1.
939 CONDITION, if nonzero, is the condition that JUMP_INSN is testing. */
941 static int
942 mostly_true_jump (jump_insn, condition)
943 rtx jump_insn, condition;
945 rtx target_label = JUMP_LABEL (jump_insn);
946 rtx insn, note;
947 int rare_dest = rare_destination (target_label);
948 int rare_fallthrough = rare_destination (NEXT_INSN (jump_insn));
950 /* If branch probabilities are available, then use that number since it
951 always gives a correct answer. */
952 note = find_reg_note (jump_insn, REG_BR_PROB, 0);
953 if (note)
955 int prob = INTVAL (XEXP (note, 0));
957 if (prob >= REG_BR_PROB_BASE * 9 / 10)
958 return 2;
959 else if (prob >= REG_BR_PROB_BASE / 2)
960 return 1;
961 else if (prob >= REG_BR_PROB_BASE / 10)
962 return 0;
963 else
964 return -1;
967 /* ??? Ought to use estimate_probability instead. */
969 /* If this is a branch outside a loop, it is highly unlikely. */
970 if (GET_CODE (PATTERN (jump_insn)) == SET
971 && GET_CODE (SET_SRC (PATTERN (jump_insn))) == IF_THEN_ELSE
972 && ((GET_CODE (XEXP (SET_SRC (PATTERN (jump_insn)), 1)) == LABEL_REF
973 && LABEL_OUTSIDE_LOOP_P (XEXP (SET_SRC (PATTERN (jump_insn)), 1)))
974 || (GET_CODE (XEXP (SET_SRC (PATTERN (jump_insn)), 2)) == LABEL_REF
975 && LABEL_OUTSIDE_LOOP_P (XEXP (SET_SRC (PATTERN (jump_insn)), 2)))))
976 return -1;
978 if (target_label)
980 /* If this is the test of a loop, it is very likely true. We scan
981 backwards from the target label. If we find a NOTE_INSN_LOOP_BEG
982 before the next real insn, we assume the branch is to the top of
983 the loop. */
984 for (insn = PREV_INSN (target_label);
985 insn && GET_CODE (insn) == NOTE;
986 insn = PREV_INSN (insn))
987 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
988 return 2;
990 /* If this is a jump to the test of a loop, it is likely true. We scan
991 forwards from the target label. If we find a NOTE_INSN_LOOP_VTOP
992 before the next real insn, we assume the branch is to the loop branch
993 test. */
994 for (insn = NEXT_INSN (target_label);
995 insn && GET_CODE (insn) == NOTE;
996 insn = PREV_INSN (insn))
997 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP)
998 return 1;
1001 /* Look at the relative rarities of the fallthrough and destination. If
1002 they differ, we can predict the branch that way. */
1004 switch (rare_fallthrough - rare_dest)
1006 case -2:
1007 return -1;
1008 case -1:
1009 return 0;
1010 case 0:
1011 break;
1012 case 1:
1013 return 1;
1014 case 2:
1015 return 2;
1018 /* If we couldn't figure out what this jump was, assume it won't be
1019 taken. This should be rare. */
1020 if (condition == 0)
1021 return 0;
1023 /* EQ tests are usually false and NE tests are usually true. Also,
1024 most quantities are positive, so we can make the appropriate guesses
1025 about signed comparisons against zero. */
1026 switch (GET_CODE (condition))
1028 case CONST_INT:
1029 /* Unconditional branch. */
1030 return 1;
1031 case EQ:
1032 return 0;
1033 case NE:
1034 return 1;
1035 case LE:
1036 case LT:
1037 if (XEXP (condition, 1) == const0_rtx)
1038 return 0;
1039 break;
1040 case GE:
1041 case GT:
1042 if (XEXP (condition, 1) == const0_rtx)
1043 return 1;
1044 break;
1046 default:
1047 break;
1050 /* Predict backward branches usually take, forward branches usually not. If
1051 we don't know whether this is forward or backward, assume the branch
1052 will be taken, since most are. */
1053 return (target_label == 0 || INSN_UID (jump_insn) > max_uid
1054 || INSN_UID (target_label) > max_uid
1055 || (uid_to_ruid[INSN_UID (jump_insn)]
1056 > uid_to_ruid[INSN_UID (target_label)]));
1059 /* Return the condition under which INSN will branch to TARGET. If TARGET
1060 is zero, return the condition under which INSN will return. If INSN is
1061 an unconditional branch, return const_true_rtx. If INSN isn't a simple
1062 type of jump, or it doesn't go to TARGET, return 0. */
1064 static rtx
1065 get_branch_condition (insn, target)
1066 rtx insn;
1067 rtx target;
1069 rtx pat = PATTERN (insn);
1070 rtx src;
1072 if (condjump_in_parallel_p (insn))
1073 pat = XVECEXP (pat, 0, 0);
1075 if (GET_CODE (pat) == RETURN)
1076 return target == 0 ? const_true_rtx : 0;
1078 else if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
1079 return 0;
1081 src = SET_SRC (pat);
1082 if (GET_CODE (src) == LABEL_REF && XEXP (src, 0) == target)
1083 return const_true_rtx;
1085 else if (GET_CODE (src) == IF_THEN_ELSE
1086 && ((target == 0 && GET_CODE (XEXP (src, 1)) == RETURN)
1087 || (GET_CODE (XEXP (src, 1)) == LABEL_REF
1088 && XEXP (XEXP (src, 1), 0) == target))
1089 && XEXP (src, 2) == pc_rtx)
1090 return XEXP (src, 0);
1092 else if (GET_CODE (src) == IF_THEN_ELSE
1093 && ((target == 0 && GET_CODE (XEXP (src, 2)) == RETURN)
1094 || (GET_CODE (XEXP (src, 2)) == LABEL_REF
1095 && XEXP (XEXP (src, 2), 0) == target))
1096 && XEXP (src, 1) == pc_rtx)
1098 enum rtx_code rev;
1099 rev = reversed_comparison_code (XEXP (src, 0), insn);
1100 if (rev != UNKNOWN)
1101 return gen_rtx_fmt_ee (rev, GET_MODE (XEXP (src, 0)),
1102 XEXP (XEXP (src, 0), 0),
1103 XEXP (XEXP (src, 0), 1));
1106 return 0;
1109 /* Return nonzero if CONDITION is more strict than the condition of
1110 INSN, i.e., if INSN will always branch if CONDITION is true. */
1112 static int
1113 condition_dominates_p (condition, insn)
1114 rtx condition;
1115 rtx insn;
1117 rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
1118 enum rtx_code code = GET_CODE (condition);
1119 enum rtx_code other_code;
1121 if (rtx_equal_p (condition, other_condition)
1122 || other_condition == const_true_rtx)
1123 return 1;
1125 else if (condition == const_true_rtx || other_condition == 0)
1126 return 0;
1128 other_code = GET_CODE (other_condition);
1129 if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
1130 || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
1131 || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
1132 return 0;
1134 return comparison_dominates_p (code, other_code);
1137 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1138 any insns already in the delay slot of JUMP. */
1140 static int
1141 redirect_with_delay_slots_safe_p (jump, newlabel, seq)
1142 rtx jump, newlabel, seq;
1144 int flags, i;
1145 rtx pat = PATTERN (seq);
1147 /* Make sure all the delay slots of this jump would still
1148 be valid after threading the jump. If they are still
1149 valid, then return nonzero. */
1151 flags = get_jump_flags (jump, newlabel);
1152 for (i = 1; i < XVECLEN (pat, 0); i++)
1153 if (! (
1154 #ifdef ANNUL_IFFALSE_SLOTS
1155 (INSN_ANNULLED_BRANCH_P (jump)
1156 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1157 ? eligible_for_annul_false (jump, i - 1,
1158 XVECEXP (pat, 0, i), flags) :
1159 #endif
1160 #ifdef ANNUL_IFTRUE_SLOTS
1161 (INSN_ANNULLED_BRANCH_P (jump)
1162 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1163 ? eligible_for_annul_true (jump, i - 1,
1164 XVECEXP (pat, 0, i), flags) :
1165 #endif
1166 eligible_for_delay (jump, i - 1, XVECEXP (pat, 0, i), flags)))
1167 break;
1169 return (i == XVECLEN (pat, 0));
1172 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1173 any insns we wish to place in the delay slot of JUMP. */
1175 static int
1176 redirect_with_delay_list_safe_p (jump, newlabel, delay_list)
1177 rtx jump, newlabel, delay_list;
1179 int flags, i;
1180 rtx li;
1182 /* Make sure all the insns in DELAY_LIST would still be
1183 valid after threading the jump. If they are still
1184 valid, then return nonzero. */
1186 flags = get_jump_flags (jump, newlabel);
1187 for (li = delay_list, i = 0; li; li = XEXP (li, 1), i++)
1188 if (! (
1189 #ifdef ANNUL_IFFALSE_SLOTS
1190 (INSN_ANNULLED_BRANCH_P (jump)
1191 && INSN_FROM_TARGET_P (XEXP (li, 0)))
1192 ? eligible_for_annul_false (jump, i, XEXP (li, 0), flags) :
1193 #endif
1194 #ifdef ANNUL_IFTRUE_SLOTS
1195 (INSN_ANNULLED_BRANCH_P (jump)
1196 && ! INSN_FROM_TARGET_P (XEXP (li, 0)))
1197 ? eligible_for_annul_true (jump, i, XEXP (li, 0), flags) :
1198 #endif
1199 eligible_for_delay (jump, i, XEXP (li, 0), flags)))
1200 break;
1202 return (li == NULL);
1205 /* DELAY_LIST is a list of insns that have already been placed into delay
1206 slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
1207 If not, return 0; otherwise return 1. */
1209 static int
1210 check_annul_list_true_false (annul_true_p, delay_list)
1211 int annul_true_p;
1212 rtx delay_list;
1214 rtx temp;
1216 if (delay_list)
1218 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1220 rtx trial = XEXP (temp, 0);
1222 if ((annul_true_p && INSN_FROM_TARGET_P (trial))
1223 || (!annul_true_p && !INSN_FROM_TARGET_P (trial)))
1224 return 0;
1228 return 1;
1231 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
1232 the condition tested by INSN is CONDITION and the resources shown in
1233 OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1234 from SEQ's delay list, in addition to whatever insns it may execute
1235 (in DELAY_LIST). SETS and NEEDED are denote resources already set and
1236 needed while searching for delay slot insns. Return the concatenated
1237 delay list if possible, otherwise, return 0.
1239 SLOTS_TO_FILL is the total number of slots required by INSN, and
1240 PSLOTS_FILLED points to the number filled so far (also the number of
1241 insns in DELAY_LIST). It is updated with the number that have been
1242 filled from the SEQUENCE, if any.
1244 PANNUL_P points to a nonzero value if we already know that we need
1245 to annul INSN. If this routine determines that annulling is needed,
1246 it may set that value nonzero.
1248 PNEW_THREAD points to a location that is to receive the place at which
1249 execution should continue. */
1251 static rtx
1252 steal_delay_list_from_target (insn, condition, seq, delay_list,
1253 sets, needed, other_needed,
1254 slots_to_fill, pslots_filled, pannul_p,
1255 pnew_thread)
1256 rtx insn, condition;
1257 rtx seq;
1258 rtx delay_list;
1259 struct resources *sets, *needed, *other_needed;
1260 int slots_to_fill;
1261 int *pslots_filled;
1262 int *pannul_p;
1263 rtx *pnew_thread;
1265 rtx temp;
1266 int slots_remaining = slots_to_fill - *pslots_filled;
1267 int total_slots_filled = *pslots_filled;
1268 rtx new_delay_list = 0;
1269 int must_annul = *pannul_p;
1270 int used_annul = 0;
1271 int i;
1272 struct resources cc_set;
1274 /* We can't do anything if there are more delay slots in SEQ than we
1275 can handle, or if we don't know that it will be a taken branch.
1276 We know that it will be a taken branch if it is either an unconditional
1277 branch or a conditional branch with a stricter branch condition.
1279 Also, exit if the branch has more than one set, since then it is computing
1280 other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1281 ??? It may be possible to move other sets into INSN in addition to
1282 moving the instructions in the delay slots.
1284 We can not steal the delay list if one of the instructions in the
1285 current delay_list modifies the condition codes and the jump in the
1286 sequence is a conditional jump. We can not do this because we can
1287 not change the direction of the jump because the condition codes
1288 will effect the direction of the jump in the sequence. */
1290 CLEAR_RESOURCE (&cc_set);
1291 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1293 rtx trial = XEXP (temp, 0);
1295 mark_set_resources (trial, &cc_set, 0, MARK_SRC_DEST_CALL);
1296 if (insn_references_resource_p (XVECEXP (seq , 0, 0), &cc_set, 0))
1297 return delay_list;
1300 if (XVECLEN (seq, 0) - 1 > slots_remaining
1301 || ! condition_dominates_p (condition, XVECEXP (seq, 0, 0))
1302 || ! single_set (XVECEXP (seq, 0, 0)))
1303 return delay_list;
1305 #ifdef MD_CAN_REDIRECT_BRANCH
1306 /* On some targets, branches with delay slots can have a limited
1307 displacement. Give the back end a chance to tell us we can't do
1308 this. */
1309 if (! MD_CAN_REDIRECT_BRANCH (insn, XVECEXP (seq, 0, 0)))
1310 return delay_list;
1311 #endif
1313 for (i = 1; i < XVECLEN (seq, 0); i++)
1315 rtx trial = XVECEXP (seq, 0, i);
1316 int flags;
1318 if (insn_references_resource_p (trial, sets, 0)
1319 || insn_sets_resource_p (trial, needed, 0)
1320 || insn_sets_resource_p (trial, sets, 0)
1321 #ifdef HAVE_cc0
1322 /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1323 delay list. */
1324 || find_reg_note (trial, REG_CC_USER, NULL_RTX)
1325 #endif
1326 /* If TRIAL is from the fallthrough code of an annulled branch insn
1327 in SEQ, we cannot use it. */
1328 || (INSN_ANNULLED_BRANCH_P (XVECEXP (seq, 0, 0))
1329 && ! INSN_FROM_TARGET_P (trial)))
1330 return delay_list;
1332 /* If this insn was already done (usually in a previous delay slot),
1333 pretend we put it in our delay slot. */
1334 if (redundant_insn (trial, insn, new_delay_list))
1335 continue;
1337 /* We will end up re-vectoring this branch, so compute flags
1338 based on jumping to the new label. */
1339 flags = get_jump_flags (insn, JUMP_LABEL (XVECEXP (seq, 0, 0)));
1341 if (! must_annul
1342 && ((condition == const_true_rtx
1343 || (! insn_sets_resource_p (trial, other_needed, 0)
1344 && ! may_trap_p (PATTERN (trial)))))
1345 ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1346 : (must_annul || (delay_list == NULL && new_delay_list == NULL))
1347 && (must_annul = 1,
1348 check_annul_list_true_false (0, delay_list)
1349 && check_annul_list_true_false (0, new_delay_list)
1350 && eligible_for_annul_false (insn, total_slots_filled,
1351 trial, flags)))
1353 if (must_annul)
1354 used_annul = 1;
1355 temp = copy_rtx (trial);
1356 INSN_FROM_TARGET_P (temp) = 1;
1357 new_delay_list = add_to_delay_list (temp, new_delay_list);
1358 total_slots_filled++;
1360 if (--slots_remaining == 0)
1361 break;
1363 else
1364 return delay_list;
1367 /* Show the place to which we will be branching. */
1368 *pnew_thread = next_active_insn (JUMP_LABEL (XVECEXP (seq, 0, 0)));
1370 /* Add any new insns to the delay list and update the count of the
1371 number of slots filled. */
1372 *pslots_filled = total_slots_filled;
1373 if (used_annul)
1374 *pannul_p = 1;
1376 if (delay_list == 0)
1377 return new_delay_list;
1379 for (temp = new_delay_list; temp; temp = XEXP (temp, 1))
1380 delay_list = add_to_delay_list (XEXP (temp, 0), delay_list);
1382 return delay_list;
1385 /* Similar to steal_delay_list_from_target except that SEQ is on the
1386 fallthrough path of INSN. Here we only do something if the delay insn
1387 of SEQ is an unconditional branch. In that case we steal its delay slot
1388 for INSN since unconditional branches are much easier to fill. */
1390 static rtx
1391 steal_delay_list_from_fallthrough (insn, condition, seq,
1392 delay_list, sets, needed, other_needed,
1393 slots_to_fill, pslots_filled, pannul_p)
1394 rtx insn, condition;
1395 rtx seq;
1396 rtx delay_list;
1397 struct resources *sets, *needed, *other_needed;
1398 int slots_to_fill;
1399 int *pslots_filled;
1400 int *pannul_p;
1402 int i;
1403 int flags;
1404 int must_annul = *pannul_p;
1405 int used_annul = 0;
1407 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1409 /* We can't do anything if SEQ's delay insn isn't an
1410 unconditional branch. */
1412 if (! simplejump_p (XVECEXP (seq, 0, 0))
1413 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) != RETURN)
1414 return delay_list;
1416 for (i = 1; i < XVECLEN (seq, 0); i++)
1418 rtx trial = XVECEXP (seq, 0, i);
1420 /* If TRIAL sets CC0, stealing it will move it too far from the use
1421 of CC0. */
1422 if (insn_references_resource_p (trial, sets, 0)
1423 || insn_sets_resource_p (trial, needed, 0)
1424 || insn_sets_resource_p (trial, sets, 0)
1425 #ifdef HAVE_cc0
1426 || sets_cc0_p (PATTERN (trial))
1427 #endif
1430 break;
1432 /* If this insn was already done, we don't need it. */
1433 if (redundant_insn (trial, insn, delay_list))
1435 delete_from_delay_slot (trial);
1436 continue;
1439 if (! must_annul
1440 && ((condition == const_true_rtx
1441 || (! insn_sets_resource_p (trial, other_needed, 0)
1442 && ! may_trap_p (PATTERN (trial)))))
1443 ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1444 : (must_annul || delay_list == NULL) && (must_annul = 1,
1445 check_annul_list_true_false (1, delay_list)
1446 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1448 if (must_annul)
1449 used_annul = 1;
1450 delete_from_delay_slot (trial);
1451 delay_list = add_to_delay_list (trial, delay_list);
1453 if (++(*pslots_filled) == slots_to_fill)
1454 break;
1456 else
1457 break;
1460 if (used_annul)
1461 *pannul_p = 1;
1462 return delay_list;
1465 /* Try merging insns starting at THREAD which match exactly the insns in
1466 INSN's delay list.
1468 If all insns were matched and the insn was previously annulling, the
1469 annul bit will be cleared.
1471 For each insn that is merged, if the branch is or will be non-annulling,
1472 we delete the merged insn. */
1474 static void
1475 try_merge_delay_insns (insn, thread)
1476 rtx insn, thread;
1478 rtx trial, next_trial;
1479 rtx delay_insn = XVECEXP (PATTERN (insn), 0, 0);
1480 int annul_p = INSN_ANNULLED_BRANCH_P (delay_insn);
1481 int slot_number = 1;
1482 int num_slots = XVECLEN (PATTERN (insn), 0);
1483 rtx next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1484 struct resources set, needed;
1485 rtx merged_insns = 0;
1486 int i;
1487 int flags;
1489 flags = get_jump_flags (delay_insn, JUMP_LABEL (delay_insn));
1491 CLEAR_RESOURCE (&needed);
1492 CLEAR_RESOURCE (&set);
1494 /* If this is not an annulling branch, take into account anything needed in
1495 INSN's delay slot. This prevents two increments from being incorrectly
1496 folded into one. If we are annulling, this would be the correct
1497 thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1498 will essentially disable this optimization. This method is somewhat of
1499 a kludge, but I don't see a better way.) */
1500 if (! annul_p)
1501 for (i = 1 ; i < num_slots; i++)
1502 if (XVECEXP (PATTERN (insn), 0, i))
1503 mark_referenced_resources (XVECEXP (PATTERN (insn), 0, i), &needed, 1);
1505 for (trial = thread; !stop_search_p (trial, 1); trial = next_trial)
1507 rtx pat = PATTERN (trial);
1508 rtx oldtrial = trial;
1510 next_trial = next_nonnote_insn (trial);
1512 /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1513 if (GET_CODE (trial) == INSN
1514 && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1515 continue;
1517 if (GET_CODE (next_to_match) == GET_CODE (trial)
1518 #ifdef HAVE_cc0
1519 /* We can't share an insn that sets cc0. */
1520 && ! sets_cc0_p (pat)
1521 #endif
1522 && ! insn_references_resource_p (trial, &set, 1)
1523 && ! insn_sets_resource_p (trial, &set, 1)
1524 && ! insn_sets_resource_p (trial, &needed, 1)
1525 && (trial = try_split (pat, trial, 0)) != 0
1526 /* Update next_trial, in case try_split succeeded. */
1527 && (next_trial = next_nonnote_insn (trial))
1528 /* Likewise THREAD. */
1529 && (thread = oldtrial == thread ? trial : thread)
1530 && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1531 /* Have to test this condition if annul condition is different
1532 from (and less restrictive than) non-annulling one. */
1533 && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1536 if (! annul_p)
1538 update_block (trial, thread);
1539 if (trial == thread)
1540 thread = next_active_insn (thread);
1542 delete_related_insns (trial);
1543 INSN_FROM_TARGET_P (next_to_match) = 0;
1545 else
1546 merged_insns = gen_rtx_INSN_LIST (VOIDmode, trial, merged_insns);
1548 if (++slot_number == num_slots)
1549 break;
1551 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1554 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1555 mark_referenced_resources (trial, &needed, 1);
1558 /* See if we stopped on a filled insn. If we did, try to see if its
1559 delay slots match. */
1560 if (slot_number != num_slots
1561 && trial && GET_CODE (trial) == INSN
1562 && GET_CODE (PATTERN (trial)) == SEQUENCE
1563 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial), 0, 0)))
1565 rtx pat = PATTERN (trial);
1566 rtx filled_insn = XVECEXP (pat, 0, 0);
1568 /* Account for resources set/needed by the filled insn. */
1569 mark_set_resources (filled_insn, &set, 0, MARK_SRC_DEST_CALL);
1570 mark_referenced_resources (filled_insn, &needed, 1);
1572 for (i = 1; i < XVECLEN (pat, 0); i++)
1574 rtx dtrial = XVECEXP (pat, 0, i);
1576 if (! insn_references_resource_p (dtrial, &set, 1)
1577 && ! insn_sets_resource_p (dtrial, &set, 1)
1578 && ! insn_sets_resource_p (dtrial, &needed, 1)
1579 #ifdef HAVE_cc0
1580 && ! sets_cc0_p (PATTERN (dtrial))
1581 #endif
1582 && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1583 && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1585 if (! annul_p)
1587 rtx new;
1589 update_block (dtrial, thread);
1590 new = delete_from_delay_slot (dtrial);
1591 if (INSN_DELETED_P (thread))
1592 thread = new;
1593 INSN_FROM_TARGET_P (next_to_match) = 0;
1595 else
1596 merged_insns = gen_rtx_INSN_LIST (SImode, dtrial,
1597 merged_insns);
1599 if (++slot_number == num_slots)
1600 break;
1602 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1604 else
1606 /* Keep track of the set/referenced resources for the delay
1607 slots of any trial insns we encounter. */
1608 mark_set_resources (dtrial, &set, 0, MARK_SRC_DEST_CALL);
1609 mark_referenced_resources (dtrial, &needed, 1);
1614 /* If all insns in the delay slot have been matched and we were previously
1615 annulling the branch, we need not any more. In that case delete all the
1616 merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1617 the delay list so that we know that it isn't only being used at the
1618 target. */
1619 if (slot_number == num_slots && annul_p)
1621 for (; merged_insns; merged_insns = XEXP (merged_insns, 1))
1623 if (GET_MODE (merged_insns) == SImode)
1625 rtx new;
1627 update_block (XEXP (merged_insns, 0), thread);
1628 new = delete_from_delay_slot (XEXP (merged_insns, 0));
1629 if (INSN_DELETED_P (thread))
1630 thread = new;
1632 else
1634 update_block (XEXP (merged_insns, 0), thread);
1635 delete_related_insns (XEXP (merged_insns, 0));
1639 INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1641 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1642 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i)) = 0;
1646 /* See if INSN is redundant with an insn in front of TARGET. Often this
1647 is called when INSN is a candidate for a delay slot of TARGET.
1648 DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1649 of INSN. Often INSN will be redundant with an insn in a delay slot of
1650 some previous insn. This happens when we have a series of branches to the
1651 same label; in that case the first insn at the target might want to go
1652 into each of the delay slots.
1654 If we are not careful, this routine can take up a significant fraction
1655 of the total compilation time (4%), but only wins rarely. Hence we
1656 speed this routine up by making two passes. The first pass goes back
1657 until it hits a label and sees if it find an insn with an identical
1658 pattern. Only in this (relatively rare) event does it check for
1659 data conflicts.
1661 We do not split insns we encounter. This could cause us not to find a
1662 redundant insn, but the cost of splitting seems greater than the possible
1663 gain in rare cases. */
1665 static rtx
1666 redundant_insn (insn, target, delay_list)
1667 rtx insn;
1668 rtx target;
1669 rtx delay_list;
1671 rtx target_main = target;
1672 rtx ipat = PATTERN (insn);
1673 rtx trial, pat;
1674 struct resources needed, set;
1675 int i;
1676 unsigned insns_to_search;
1678 /* If INSN has any REG_UNUSED notes, it can't match anything since we
1679 are allowed to not actually assign to such a register. */
1680 if (find_reg_note (insn, REG_UNUSED, NULL_RTX) != 0)
1681 return 0;
1683 /* Scan backwards looking for a match. */
1684 for (trial = PREV_INSN (target),
1685 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1686 trial && insns_to_search > 0;
1687 trial = PREV_INSN (trial), --insns_to_search)
1689 if (GET_CODE (trial) == CODE_LABEL)
1690 return 0;
1692 if (! INSN_P (trial))
1693 continue;
1695 pat = PATTERN (trial);
1696 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1697 continue;
1699 if (GET_CODE (pat) == SEQUENCE)
1701 /* Stop for a CALL and its delay slots because it is difficult to
1702 track its resource needs correctly. */
1703 if (GET_CODE (XVECEXP (pat, 0, 0)) == CALL_INSN)
1704 return 0;
1706 /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1707 slots because it is difficult to track its resource needs
1708 correctly. */
1710 #ifdef INSN_SETS_ARE_DELAYED
1711 if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1712 return 0;
1713 #endif
1715 #ifdef INSN_REFERENCES_ARE_DELAYED
1716 if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1717 return 0;
1718 #endif
1720 /* See if any of the insns in the delay slot match, updating
1721 resource requirements as we go. */
1722 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1723 if (GET_CODE (XVECEXP (pat, 0, i)) == GET_CODE (insn)
1724 && rtx_equal_p (PATTERN (XVECEXP (pat, 0, i)), ipat)
1725 && ! find_reg_note (XVECEXP (pat, 0, i), REG_UNUSED, NULL_RTX))
1726 break;
1728 /* If found a match, exit this loop early. */
1729 if (i > 0)
1730 break;
1733 else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat)
1734 && ! find_reg_note (trial, REG_UNUSED, NULL_RTX))
1735 break;
1738 /* If we didn't find an insn that matches, return 0. */
1739 if (trial == 0)
1740 return 0;
1742 /* See what resources this insn sets and needs. If they overlap, or
1743 if this insn references CC0, it can't be redundant. */
1745 CLEAR_RESOURCE (&needed);
1746 CLEAR_RESOURCE (&set);
1747 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1748 mark_referenced_resources (insn, &needed, 1);
1750 /* If TARGET is a SEQUENCE, get the main insn. */
1751 if (GET_CODE (target) == INSN && GET_CODE (PATTERN (target)) == SEQUENCE)
1752 target_main = XVECEXP (PATTERN (target), 0, 0);
1754 if (resource_conflicts_p (&needed, &set)
1755 #ifdef HAVE_cc0
1756 || reg_mentioned_p (cc0_rtx, ipat)
1757 #endif
1758 /* The insn requiring the delay may not set anything needed or set by
1759 INSN. */
1760 || insn_sets_resource_p (target_main, &needed, 1)
1761 || insn_sets_resource_p (target_main, &set, 1))
1762 return 0;
1764 /* Insns we pass may not set either NEEDED or SET, so merge them for
1765 simpler tests. */
1766 needed.memory |= set.memory;
1767 needed.unch_memory |= set.unch_memory;
1768 IOR_HARD_REG_SET (needed.regs, set.regs);
1770 /* This insn isn't redundant if it conflicts with an insn that either is
1771 or will be in a delay slot of TARGET. */
1773 while (delay_list)
1775 if (insn_sets_resource_p (XEXP (delay_list, 0), &needed, 1))
1776 return 0;
1777 delay_list = XEXP (delay_list, 1);
1780 if (GET_CODE (target) == INSN && GET_CODE (PATTERN (target)) == SEQUENCE)
1781 for (i = 1; i < XVECLEN (PATTERN (target), 0); i++)
1782 if (insn_sets_resource_p (XVECEXP (PATTERN (target), 0, i), &needed, 1))
1783 return 0;
1785 /* Scan backwards until we reach a label or an insn that uses something
1786 INSN sets or sets something insn uses or sets. */
1788 for (trial = PREV_INSN (target),
1789 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1790 trial && GET_CODE (trial) != CODE_LABEL && insns_to_search > 0;
1791 trial = PREV_INSN (trial), --insns_to_search)
1793 if (GET_CODE (trial) != INSN && GET_CODE (trial) != CALL_INSN
1794 && GET_CODE (trial) != JUMP_INSN)
1795 continue;
1797 pat = PATTERN (trial);
1798 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1799 continue;
1801 if (GET_CODE (pat) == SEQUENCE)
1803 /* If this is a CALL_INSN and its delay slots, it is hard to track
1804 the resource needs properly, so give up. */
1805 if (GET_CODE (XVECEXP (pat, 0, 0)) == CALL_INSN)
1806 return 0;
1808 /* If this is an INSN or JUMP_INSN with delayed effects, it
1809 is hard to track the resource needs properly, so give up. */
1811 #ifdef INSN_SETS_ARE_DELAYED
1812 if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1813 return 0;
1814 #endif
1816 #ifdef INSN_REFERENCES_ARE_DELAYED
1817 if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1818 return 0;
1819 #endif
1821 /* See if any of the insns in the delay slot match, updating
1822 resource requirements as we go. */
1823 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1825 rtx candidate = XVECEXP (pat, 0, i);
1827 /* If an insn will be annulled if the branch is false, it isn't
1828 considered as a possible duplicate insn. */
1829 if (rtx_equal_p (PATTERN (candidate), ipat)
1830 && ! (INSN_ANNULLED_BRANCH_P (XVECEXP (pat, 0, 0))
1831 && INSN_FROM_TARGET_P (candidate)))
1833 /* Show that this insn will be used in the sequel. */
1834 INSN_FROM_TARGET_P (candidate) = 0;
1835 return candidate;
1838 /* Unless this is an annulled insn from the target of a branch,
1839 we must stop if it sets anything needed or set by INSN. */
1840 if ((! INSN_ANNULLED_BRANCH_P (XVECEXP (pat, 0, 0))
1841 || ! INSN_FROM_TARGET_P (candidate))
1842 && insn_sets_resource_p (candidate, &needed, 1))
1843 return 0;
1846 /* If the insn requiring the delay slot conflicts with INSN, we
1847 must stop. */
1848 if (insn_sets_resource_p (XVECEXP (pat, 0, 0), &needed, 1))
1849 return 0;
1851 else
1853 /* See if TRIAL is the same as INSN. */
1854 pat = PATTERN (trial);
1855 if (rtx_equal_p (pat, ipat))
1856 return trial;
1858 /* Can't go any further if TRIAL conflicts with INSN. */
1859 if (insn_sets_resource_p (trial, &needed, 1))
1860 return 0;
1864 return 0;
1867 /* Return 1 if THREAD can only be executed in one way. If LABEL is nonzero,
1868 it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1869 is nonzero, we are allowed to fall into this thread; otherwise, we are
1870 not.
1872 If LABEL is used more than one or we pass a label other than LABEL before
1873 finding an active insn, we do not own this thread. */
1875 static int
1876 own_thread_p (thread, label, allow_fallthrough)
1877 rtx thread;
1878 rtx label;
1879 int allow_fallthrough;
1881 rtx active_insn;
1882 rtx insn;
1884 /* We don't own the function end. */
1885 if (thread == 0)
1886 return 0;
1888 /* Get the first active insn, or THREAD, if it is an active insn. */
1889 active_insn = next_active_insn (PREV_INSN (thread));
1891 for (insn = thread; insn != active_insn; insn = NEXT_INSN (insn))
1892 if (GET_CODE (insn) == CODE_LABEL
1893 && (insn != label || LABEL_NUSES (insn) != 1))
1894 return 0;
1896 if (allow_fallthrough)
1897 return 1;
1899 /* Ensure that we reach a BARRIER before any insn or label. */
1900 for (insn = prev_nonnote_insn (thread);
1901 insn == 0 || GET_CODE (insn) != BARRIER;
1902 insn = prev_nonnote_insn (insn))
1903 if (insn == 0
1904 || GET_CODE (insn) == CODE_LABEL
1905 || (GET_CODE (insn) == INSN
1906 && GET_CODE (PATTERN (insn)) != USE
1907 && GET_CODE (PATTERN (insn)) != CLOBBER))
1908 return 0;
1910 return 1;
1913 /* Called when INSN is being moved from a location near the target of a jump.
1914 We leave a marker of the form (use (INSN)) immediately in front
1915 of WHERE for mark_target_live_regs. These markers will be deleted when
1916 reorg finishes.
1918 We used to try to update the live status of registers if WHERE is at
1919 the start of a basic block, but that can't work since we may remove a
1920 BARRIER in relax_delay_slots. */
1922 static void
1923 update_block (insn, where)
1924 rtx insn;
1925 rtx where;
1927 /* Ignore if this was in a delay slot and it came from the target of
1928 a branch. */
1929 if (INSN_FROM_TARGET_P (insn))
1930 return;
1932 emit_insn_before (gen_rtx_USE (VOIDmode, insn), where);
1934 /* INSN might be making a value live in a block where it didn't use to
1935 be. So recompute liveness information for this block. */
1937 incr_ticks_for_insn (insn);
1940 /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1941 the basic block containing the jump. */
1943 static int
1944 reorg_redirect_jump (jump, nlabel)
1945 rtx jump;
1946 rtx nlabel;
1948 incr_ticks_for_insn (jump);
1949 return redirect_jump (jump, nlabel, 1);
1952 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1953 We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1954 that reference values used in INSN. If we find one, then we move the
1955 REG_DEAD note to INSN.
1957 This is needed to handle the case where an later insn (after INSN) has a
1958 REG_DEAD note for a register used by INSN, and this later insn subsequently
1959 gets moved before a CODE_LABEL because it is a redundant insn. In this
1960 case, mark_target_live_regs may be confused into thinking the register
1961 is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1963 static void
1964 update_reg_dead_notes (insn, delayed_insn)
1965 rtx insn, delayed_insn;
1967 rtx p, link, next;
1969 for (p = next_nonnote_insn (insn); p != delayed_insn;
1970 p = next_nonnote_insn (p))
1971 for (link = REG_NOTES (p); link; link = next)
1973 next = XEXP (link, 1);
1975 if (REG_NOTE_KIND (link) != REG_DEAD
1976 || GET_CODE (XEXP (link, 0)) != REG)
1977 continue;
1979 if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
1981 /* Move the REG_DEAD note from P to INSN. */
1982 remove_note (p, link);
1983 XEXP (link, 1) = REG_NOTES (insn);
1984 REG_NOTES (insn) = link;
1989 /* Called when an insn redundant with start_insn is deleted. If there
1990 is a REG_DEAD note for the target of start_insn between start_insn
1991 and stop_insn, then the REG_DEAD note needs to be deleted since the
1992 value no longer dies there.
1994 If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1995 confused into thinking the register is dead. */
1997 static void
1998 fix_reg_dead_note (start_insn, stop_insn)
1999 rtx start_insn, stop_insn;
2001 rtx p, link, next;
2003 for (p = next_nonnote_insn (start_insn); p != stop_insn;
2004 p = next_nonnote_insn (p))
2005 for (link = REG_NOTES (p); link; link = next)
2007 next = XEXP (link, 1);
2009 if (REG_NOTE_KIND (link) != REG_DEAD
2010 || GET_CODE (XEXP (link, 0)) != REG)
2011 continue;
2013 if (reg_set_p (XEXP (link, 0), PATTERN (start_insn)))
2015 remove_note (p, link);
2016 return;
2021 /* Delete any REG_UNUSED notes that exist on INSN but not on REDUNDANT_INSN.
2023 This handles the case of udivmodXi4 instructions which optimize their
2024 output depending on whether any REG_UNUSED notes are present.
2025 we must make sure that INSN calculates as many results as REDUNDANT_INSN
2026 does. */
2028 static void
2029 update_reg_unused_notes (insn, redundant_insn)
2030 rtx insn, redundant_insn;
2032 rtx link, next;
2034 for (link = REG_NOTES (insn); link; link = next)
2036 next = XEXP (link, 1);
2038 if (REG_NOTE_KIND (link) != REG_UNUSED
2039 || GET_CODE (XEXP (link, 0)) != REG)
2040 continue;
2042 if (! find_regno_note (redundant_insn, REG_UNUSED,
2043 REGNO (XEXP (link, 0))))
2044 remove_note (insn, link);
2048 /* Scan a function looking for insns that need a delay slot and find insns to
2049 put into the delay slot.
2051 NON_JUMPS_P is nonzero if we are to only try to fill non-jump insns (such
2052 as calls). We do these first since we don't want jump insns (that are
2053 easier to fill) to get the only insns that could be used for non-jump insns.
2054 When it is zero, only try to fill JUMP_INSNs.
2056 When slots are filled in this manner, the insns (including the
2057 delay_insn) are put together in a SEQUENCE rtx. In this fashion,
2058 it is possible to tell whether a delay slot has really been filled
2059 or not. `final' knows how to deal with this, by communicating
2060 through FINAL_SEQUENCE. */
2062 static void
2063 fill_simple_delay_slots (non_jumps_p)
2064 int non_jumps_p;
2066 rtx insn, pat, trial, next_trial;
2067 int i;
2068 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2069 struct resources needed, set;
2070 int slots_to_fill, slots_filled;
2071 rtx delay_list;
2073 for (i = 0; i < num_unfilled_slots; i++)
2075 int flags;
2076 /* Get the next insn to fill. If it has already had any slots assigned,
2077 we can't do anything with it. Maybe we'll improve this later. */
2079 insn = unfilled_slots_base[i];
2080 if (insn == 0
2081 || INSN_DELETED_P (insn)
2082 || (GET_CODE (insn) == INSN
2083 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2084 || (GET_CODE (insn) == JUMP_INSN && non_jumps_p)
2085 || (GET_CODE (insn) != JUMP_INSN && ! non_jumps_p))
2086 continue;
2088 /* It may have been that this insn used to need delay slots, but
2089 now doesn't; ignore in that case. This can happen, for example,
2090 on the HP PA RISC, where the number of delay slots depends on
2091 what insns are nearby. */
2092 slots_to_fill = num_delay_slots (insn);
2094 /* Some machine description have defined instructions to have
2095 delay slots only in certain circumstances which may depend on
2096 nearby insns (which change due to reorg's actions).
2098 For example, the PA port normally has delay slots for unconditional
2099 jumps.
2101 However, the PA port claims such jumps do not have a delay slot
2102 if they are immediate successors of certain CALL_INSNs. This
2103 allows the port to favor filling the delay slot of the call with
2104 the unconditional jump. */
2105 if (slots_to_fill == 0)
2106 continue;
2108 /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
2109 says how many. After initialization, first try optimizing
2111 call _foo call _foo
2112 nop add %o7,.-L1,%o7
2113 b,a L1
2116 If this case applies, the delay slot of the call is filled with
2117 the unconditional jump. This is done first to avoid having the
2118 delay slot of the call filled in the backward scan. Also, since
2119 the unconditional jump is likely to also have a delay slot, that
2120 insn must exist when it is subsequently scanned.
2122 This is tried on each insn with delay slots as some machines
2123 have insns which perform calls, but are not represented as
2124 CALL_INSNs. */
2126 slots_filled = 0;
2127 delay_list = 0;
2129 if (GET_CODE (insn) == JUMP_INSN)
2130 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2131 else
2132 flags = get_jump_flags (insn, NULL_RTX);
2134 if ((trial = next_active_insn (insn))
2135 && GET_CODE (trial) == JUMP_INSN
2136 && simplejump_p (trial)
2137 && eligible_for_delay (insn, slots_filled, trial, flags)
2138 && no_labels_between_p (insn, trial)
2139 && ! can_throw_internal (trial))
2141 rtx *tmp;
2142 slots_filled++;
2143 delay_list = add_to_delay_list (trial, delay_list);
2145 /* TRIAL may have had its delay slot filled, then unfilled. When
2146 the delay slot is unfilled, TRIAL is placed back on the unfilled
2147 slots obstack. Unfortunately, it is placed on the end of the
2148 obstack, not in its original location. Therefore, we must search
2149 from entry i + 1 to the end of the unfilled slots obstack to
2150 try and find TRIAL. */
2151 tmp = &unfilled_slots_base[i + 1];
2152 while (*tmp != trial && tmp != unfilled_slots_next)
2153 tmp++;
2155 /* Remove the unconditional jump from consideration for delay slot
2156 filling and unthread it. */
2157 if (*tmp == trial)
2158 *tmp = 0;
2160 rtx next = NEXT_INSN (trial);
2161 rtx prev = PREV_INSN (trial);
2162 if (prev)
2163 NEXT_INSN (prev) = next;
2164 if (next)
2165 PREV_INSN (next) = prev;
2169 /* Now, scan backwards from the insn to search for a potential
2170 delay-slot candidate. Stop searching when a label or jump is hit.
2172 For each candidate, if it is to go into the delay slot (moved
2173 forward in execution sequence), it must not need or set any resources
2174 that were set by later insns and must not set any resources that
2175 are needed for those insns.
2177 The delay slot insn itself sets resources unless it is a call
2178 (in which case the called routine, not the insn itself, is doing
2179 the setting). */
2181 if (slots_filled < slots_to_fill)
2183 CLEAR_RESOURCE (&needed);
2184 CLEAR_RESOURCE (&set);
2185 mark_set_resources (insn, &set, 0, MARK_SRC_DEST);
2186 mark_referenced_resources (insn, &needed, 0);
2188 for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, 1);
2189 trial = next_trial)
2191 next_trial = prev_nonnote_insn (trial);
2193 /* This must be an INSN or CALL_INSN. */
2194 pat = PATTERN (trial);
2196 /* USE and CLOBBER at this level was just for flow; ignore it. */
2197 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2198 continue;
2200 /* Check for resource conflict first, to avoid unnecessary
2201 splitting. */
2202 if (! insn_references_resource_p (trial, &set, 1)
2203 && ! insn_sets_resource_p (trial, &set, 1)
2204 && ! insn_sets_resource_p (trial, &needed, 1)
2205 #ifdef HAVE_cc0
2206 /* Can't separate set of cc0 from its use. */
2207 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2208 #endif
2209 && ! can_throw_internal (trial))
2211 trial = try_split (pat, trial, 1);
2212 next_trial = prev_nonnote_insn (trial);
2213 if (eligible_for_delay (insn, slots_filled, trial, flags))
2215 /* In this case, we are searching backward, so if we
2216 find insns to put on the delay list, we want
2217 to put them at the head, rather than the
2218 tail, of the list. */
2220 update_reg_dead_notes (trial, insn);
2221 delay_list = gen_rtx_INSN_LIST (VOIDmode,
2222 trial, delay_list);
2223 update_block (trial, trial);
2224 delete_related_insns (trial);
2225 if (slots_to_fill == ++slots_filled)
2226 break;
2227 continue;
2231 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2232 mark_referenced_resources (trial, &needed, 1);
2236 /* If all needed slots haven't been filled, we come here. */
2238 /* Try to optimize case of jumping around a single insn. */
2239 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
2240 if (slots_filled != slots_to_fill
2241 && delay_list == 0
2242 && GET_CODE (insn) == JUMP_INSN
2243 && (condjump_p (insn) || condjump_in_parallel_p (insn)))
2245 delay_list = optimize_skip (insn);
2246 if (delay_list)
2247 slots_filled += 1;
2249 #endif
2251 /* Try to get insns from beyond the insn needing the delay slot.
2252 These insns can neither set or reference resources set in insns being
2253 skipped, cannot set resources in the insn being skipped, and, if this
2254 is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2255 call might not return).
2257 There used to be code which continued past the target label if
2258 we saw all uses of the target label. This code did not work,
2259 because it failed to account for some instructions which were
2260 both annulled and marked as from the target. This can happen as a
2261 result of optimize_skip. Since this code was redundant with
2262 fill_eager_delay_slots anyways, it was just deleted. */
2264 if (slots_filled != slots_to_fill
2265 /* If this instruction could throw an exception which is
2266 caught in the same function, then it's not safe to fill
2267 the delay slot with an instruction from beyond this
2268 point. For example, consider:
2270 int i = 2;
2272 try {
2273 f();
2274 i = 3;
2275 } catch (...) {}
2277 return i;
2279 Even though `i' is a local variable, we must be sure not
2280 to put `i = 3' in the delay slot if `f' might throw an
2281 exception.
2283 Presumably, we should also check to see if we could get
2284 back to this function via `setjmp'. */
2285 && ! can_throw_internal (insn)
2286 && (GET_CODE (insn) != JUMP_INSN
2287 || ((condjump_p (insn) || condjump_in_parallel_p (insn))
2288 && ! simplejump_p (insn)
2289 && JUMP_LABEL (insn) != 0)))
2291 /* Invariant: If insn is a JUMP_INSN, the insn's jump
2292 label. Otherwise, zero. */
2293 rtx target = 0;
2294 int maybe_never = 0;
2295 rtx pat, trial_delay;
2297 CLEAR_RESOURCE (&needed);
2298 CLEAR_RESOURCE (&set);
2300 if (GET_CODE (insn) == CALL_INSN)
2302 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2303 mark_referenced_resources (insn, &needed, 1);
2304 maybe_never = 1;
2306 else
2308 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2309 mark_referenced_resources (insn, &needed, 1);
2310 if (GET_CODE (insn) == JUMP_INSN)
2311 target = JUMP_LABEL (insn);
2314 if (target == 0)
2315 for (trial = next_nonnote_insn (insn); trial; trial = next_trial)
2317 next_trial = next_nonnote_insn (trial);
2319 if (GET_CODE (trial) == CODE_LABEL
2320 || GET_CODE (trial) == BARRIER)
2321 break;
2323 /* We must have an INSN, JUMP_INSN, or CALL_INSN. */
2324 pat = PATTERN (trial);
2326 /* Stand-alone USE and CLOBBER are just for flow. */
2327 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2328 continue;
2330 /* If this already has filled delay slots, get the insn needing
2331 the delay slots. */
2332 if (GET_CODE (pat) == SEQUENCE)
2333 trial_delay = XVECEXP (pat, 0, 0);
2334 else
2335 trial_delay = trial;
2337 /* Stop our search when seeing an unconditional jump. */
2338 if (GET_CODE (trial_delay) == JUMP_INSN)
2339 break;
2341 /* See if we have a resource problem before we try to
2342 split. */
2343 if (GET_CODE (pat) != SEQUENCE
2344 && ! insn_references_resource_p (trial, &set, 1)
2345 && ! insn_sets_resource_p (trial, &set, 1)
2346 && ! insn_sets_resource_p (trial, &needed, 1)
2347 #ifdef HAVE_cc0
2348 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2349 #endif
2350 && ! (maybe_never && may_trap_p (pat))
2351 && (trial = try_split (pat, trial, 0))
2352 && eligible_for_delay (insn, slots_filled, trial, flags)
2353 && ! can_throw_internal(trial))
2355 next_trial = next_nonnote_insn (trial);
2356 delay_list = add_to_delay_list (trial, delay_list);
2358 #ifdef HAVE_cc0
2359 if (reg_mentioned_p (cc0_rtx, pat))
2360 link_cc0_insns (trial);
2361 #endif
2363 delete_related_insns (trial);
2364 if (slots_to_fill == ++slots_filled)
2365 break;
2366 continue;
2369 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2370 mark_referenced_resources (trial, &needed, 1);
2372 /* Ensure we don't put insns between the setting of cc and the
2373 comparison by moving a setting of cc into an earlier delay
2374 slot since these insns could clobber the condition code. */
2375 set.cc = 1;
2377 /* If this is a call or jump, we might not get here. */
2378 if (GET_CODE (trial_delay) == CALL_INSN
2379 || GET_CODE (trial_delay) == JUMP_INSN)
2380 maybe_never = 1;
2383 /* If there are slots left to fill and our search was stopped by an
2384 unconditional branch, try the insn at the branch target. We can
2385 redirect the branch if it works.
2387 Don't do this if the insn at the branch target is a branch. */
2388 if (slots_to_fill != slots_filled
2389 && trial
2390 && GET_CODE (trial) == JUMP_INSN
2391 && simplejump_p (trial)
2392 && (target == 0 || JUMP_LABEL (trial) == target)
2393 && (next_trial = next_active_insn (JUMP_LABEL (trial))) != 0
2394 && ! (GET_CODE (next_trial) == INSN
2395 && GET_CODE (PATTERN (next_trial)) == SEQUENCE)
2396 && GET_CODE (next_trial) != JUMP_INSN
2397 && ! insn_references_resource_p (next_trial, &set, 1)
2398 && ! insn_sets_resource_p (next_trial, &set, 1)
2399 && ! insn_sets_resource_p (next_trial, &needed, 1)
2400 #ifdef HAVE_cc0
2401 && ! reg_mentioned_p (cc0_rtx, PATTERN (next_trial))
2402 #endif
2403 && ! (maybe_never && may_trap_p (PATTERN (next_trial)))
2404 && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2405 && eligible_for_delay (insn, slots_filled, next_trial, flags)
2406 && ! can_throw_internal (trial))
2408 rtx new_label = next_active_insn (next_trial);
2410 if (new_label != 0)
2411 new_label = get_label_before (new_label);
2412 else
2413 new_label = find_end_label ();
2415 delay_list
2416 = add_to_delay_list (copy_rtx (next_trial), delay_list);
2417 slots_filled++;
2418 reorg_redirect_jump (trial, new_label);
2420 /* If we merged because we both jumped to the same place,
2421 redirect the original insn also. */
2422 if (target)
2423 reorg_redirect_jump (insn, new_label);
2427 /* If this is an unconditional jump, then try to get insns from the
2428 target of the jump. */
2429 if (GET_CODE (insn) == JUMP_INSN
2430 && simplejump_p (insn)
2431 && slots_filled != slots_to_fill)
2432 delay_list
2433 = fill_slots_from_thread (insn, const_true_rtx,
2434 next_active_insn (JUMP_LABEL (insn)),
2435 NULL, 1, 1,
2436 own_thread_p (JUMP_LABEL (insn),
2437 JUMP_LABEL (insn), 0),
2438 slots_to_fill, &slots_filled,
2439 delay_list);
2441 if (delay_list)
2442 unfilled_slots_base[i]
2443 = emit_delay_sequence (insn, delay_list, slots_filled);
2445 if (slots_to_fill == slots_filled)
2446 unfilled_slots_base[i] = 0;
2448 note_delay_statistics (slots_filled, 0);
2451 #ifdef DELAY_SLOTS_FOR_EPILOGUE
2452 /* See if the epilogue needs any delay slots. Try to fill them if so.
2453 The only thing we can do is scan backwards from the end of the
2454 function. If we did this in a previous pass, it is incorrect to do it
2455 again. */
2456 if (current_function_epilogue_delay_list)
2457 return;
2459 slots_to_fill = DELAY_SLOTS_FOR_EPILOGUE;
2460 if (slots_to_fill == 0)
2461 return;
2463 slots_filled = 0;
2464 CLEAR_RESOURCE (&set);
2466 /* The frame pointer and stack pointer are needed at the beginning of
2467 the epilogue, so instructions setting them can not be put in the
2468 epilogue delay slot. However, everything else needed at function
2469 end is safe, so we don't want to use end_of_function_needs here. */
2470 CLEAR_RESOURCE (&needed);
2471 if (frame_pointer_needed)
2473 SET_HARD_REG_BIT (needed.regs, FRAME_POINTER_REGNUM);
2474 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2475 SET_HARD_REG_BIT (needed.regs, HARD_FRAME_POINTER_REGNUM);
2476 #endif
2477 #ifdef EXIT_IGNORE_STACK
2478 if (! EXIT_IGNORE_STACK
2479 || current_function_sp_is_unchanging)
2480 #endif
2481 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2483 else
2484 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2486 #ifdef EPILOGUE_USES
2487 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2489 if (EPILOGUE_USES (i))
2490 SET_HARD_REG_BIT (needed.regs, i);
2492 #endif
2494 for (trial = get_last_insn (); ! stop_search_p (trial, 1);
2495 trial = PREV_INSN (trial))
2497 if (GET_CODE (trial) == NOTE)
2498 continue;
2499 pat = PATTERN (trial);
2500 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2501 continue;
2503 if (! insn_references_resource_p (trial, &set, 1)
2504 && ! insn_sets_resource_p (trial, &needed, 1)
2505 && ! insn_sets_resource_p (trial, &set, 1)
2506 #ifdef HAVE_cc0
2507 /* Don't want to mess with cc0 here. */
2508 && ! reg_mentioned_p (cc0_rtx, pat)
2509 #endif
2510 && ! can_throw_internal (trial))
2512 trial = try_split (pat, trial, 1);
2513 if (ELIGIBLE_FOR_EPILOGUE_DELAY (trial, slots_filled))
2515 /* Here as well we are searching backward, so put the
2516 insns we find on the head of the list. */
2518 current_function_epilogue_delay_list
2519 = gen_rtx_INSN_LIST (VOIDmode, trial,
2520 current_function_epilogue_delay_list);
2521 mark_end_of_function_resources (trial, 1);
2522 update_block (trial, trial);
2523 delete_related_insns (trial);
2525 /* Clear deleted bit so final.c will output the insn. */
2526 INSN_DELETED_P (trial) = 0;
2528 if (slots_to_fill == ++slots_filled)
2529 break;
2530 continue;
2534 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2535 mark_referenced_resources (trial, &needed, 1);
2538 note_delay_statistics (slots_filled, 0);
2539 #endif
2542 /* Try to find insns to place in delay slots.
2544 INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2545 or is an unconditional branch if CONDITION is const_true_rtx.
2546 *PSLOTS_FILLED is updated with the number of slots that we have filled.
2548 THREAD is a flow-of-control, either the insns to be executed if the
2549 branch is true or if the branch is false, THREAD_IF_TRUE says which.
2551 OPPOSITE_THREAD is the thread in the opposite direction. It is used
2552 to see if any potential delay slot insns set things needed there.
2554 LIKELY is nonzero if it is extremely likely that the branch will be
2555 taken and THREAD_IF_TRUE is set. This is used for the branch at the
2556 end of a loop back up to the top.
2558 OWN_THREAD and OWN_OPPOSITE_THREAD are true if we are the only user of the
2559 thread. I.e., it is the fallthrough code of our jump or the target of the
2560 jump when we are the only jump going there.
2562 If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2563 case, we can only take insns from the head of the thread for our delay
2564 slot. We then adjust the jump to point after the insns we have taken. */
2566 static rtx
2567 fill_slots_from_thread (insn, condition, thread, opposite_thread, likely,
2568 thread_if_true, own_thread,
2569 slots_to_fill, pslots_filled, delay_list)
2570 rtx insn;
2571 rtx condition;
2572 rtx thread, opposite_thread;
2573 int likely;
2574 int thread_if_true;
2575 int own_thread;
2576 int slots_to_fill, *pslots_filled;
2577 rtx delay_list;
2579 rtx new_thread;
2580 struct resources opposite_needed, set, needed;
2581 rtx trial;
2582 int lose = 0;
2583 int must_annul = 0;
2584 int flags;
2586 /* Validate our arguments. */
2587 if ((condition == const_true_rtx && ! thread_if_true)
2588 || (! own_thread && ! thread_if_true))
2589 abort ();
2591 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2593 /* If our thread is the end of subroutine, we can't get any delay
2594 insns from that. */
2595 if (thread == 0)
2596 return delay_list;
2598 /* If this is an unconditional branch, nothing is needed at the
2599 opposite thread. Otherwise, compute what is needed there. */
2600 if (condition == const_true_rtx)
2601 CLEAR_RESOURCE (&opposite_needed);
2602 else
2603 mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2605 /* If the insn at THREAD can be split, do it here to avoid having to
2606 update THREAD and NEW_THREAD if it is done in the loop below. Also
2607 initialize NEW_THREAD. */
2609 new_thread = thread = try_split (PATTERN (thread), thread, 0);
2611 /* Scan insns at THREAD. We are looking for an insn that can be removed
2612 from THREAD (it neither sets nor references resources that were set
2613 ahead of it and it doesn't set anything needs by the insns ahead of
2614 it) and that either can be placed in an annulling insn or aren't
2615 needed at OPPOSITE_THREAD. */
2617 CLEAR_RESOURCE (&needed);
2618 CLEAR_RESOURCE (&set);
2620 /* If we do not own this thread, we must stop as soon as we find
2621 something that we can't put in a delay slot, since all we can do
2622 is branch into THREAD at a later point. Therefore, labels stop
2623 the search if this is not the `true' thread. */
2625 for (trial = thread;
2626 ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2627 trial = next_nonnote_insn (trial))
2629 rtx pat, old_trial;
2631 /* If we have passed a label, we no longer own this thread. */
2632 if (GET_CODE (trial) == CODE_LABEL)
2634 own_thread = 0;
2635 continue;
2638 pat = PATTERN (trial);
2639 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2640 continue;
2642 /* If TRIAL conflicts with the insns ahead of it, we lose. Also,
2643 don't separate or copy insns that set and use CC0. */
2644 if (! insn_references_resource_p (trial, &set, 1)
2645 && ! insn_sets_resource_p (trial, &set, 1)
2646 && ! insn_sets_resource_p (trial, &needed, 1)
2647 #ifdef HAVE_cc0
2648 && ! (reg_mentioned_p (cc0_rtx, pat)
2649 && (! own_thread || ! sets_cc0_p (pat)))
2650 #endif
2651 && ! can_throw_internal (trial))
2653 rtx prior_insn;
2655 /* If TRIAL is redundant with some insn before INSN, we don't
2656 actually need to add it to the delay list; we can merely pretend
2657 we did. */
2658 if ((prior_insn = redundant_insn (trial, insn, delay_list)))
2660 fix_reg_dead_note (prior_insn, insn);
2661 if (own_thread)
2663 update_block (trial, thread);
2664 if (trial == thread)
2666 thread = next_active_insn (thread);
2667 if (new_thread == trial)
2668 new_thread = thread;
2671 delete_related_insns (trial);
2673 else
2675 update_reg_unused_notes (prior_insn, trial);
2676 new_thread = next_active_insn (trial);
2679 continue;
2682 /* There are two ways we can win: If TRIAL doesn't set anything
2683 needed at the opposite thread and can't trap, or if it can
2684 go into an annulled delay slot. */
2685 if (!must_annul
2686 && (condition == const_true_rtx
2687 || (! insn_sets_resource_p (trial, &opposite_needed, 1)
2688 && ! may_trap_p (pat))))
2690 old_trial = trial;
2691 trial = try_split (pat, trial, 0);
2692 if (new_thread == old_trial)
2693 new_thread = trial;
2694 if (thread == old_trial)
2695 thread = trial;
2696 pat = PATTERN (trial);
2697 if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2698 goto winner;
2700 else if (0
2701 #ifdef ANNUL_IFTRUE_SLOTS
2702 || ! thread_if_true
2703 #endif
2704 #ifdef ANNUL_IFFALSE_SLOTS
2705 || thread_if_true
2706 #endif
2709 old_trial = trial;
2710 trial = try_split (pat, trial, 0);
2711 if (new_thread == old_trial)
2712 new_thread = trial;
2713 if (thread == old_trial)
2714 thread = trial;
2715 pat = PATTERN (trial);
2716 if ((must_annul || delay_list == NULL) && (thread_if_true
2717 ? check_annul_list_true_false (0, delay_list)
2718 && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2719 : check_annul_list_true_false (1, delay_list)
2720 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2722 rtx temp;
2724 must_annul = 1;
2725 winner:
2727 #ifdef HAVE_cc0
2728 if (reg_mentioned_p (cc0_rtx, pat))
2729 link_cc0_insns (trial);
2730 #endif
2732 /* If we own this thread, delete the insn. If this is the
2733 destination of a branch, show that a basic block status
2734 may have been updated. In any case, mark the new
2735 starting point of this thread. */
2736 if (own_thread)
2738 rtx note;
2740 update_block (trial, thread);
2741 if (trial == thread)
2743 thread = next_active_insn (thread);
2744 if (new_thread == trial)
2745 new_thread = thread;
2748 /* We are moving this insn, not deleting it. We must
2749 temporarily increment the use count on any referenced
2750 label lest it be deleted by delete_related_insns. */
2751 note = find_reg_note (trial, REG_LABEL, 0);
2752 /* REG_LABEL could be NOTE_INSN_DELETED_LABEL too. */
2753 if (note && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2754 LABEL_NUSES (XEXP (note, 0))++;
2756 delete_related_insns (trial);
2758 if (note && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2759 LABEL_NUSES (XEXP (note, 0))--;
2761 else
2762 new_thread = next_active_insn (trial);
2764 temp = own_thread ? trial : copy_rtx (trial);
2765 if (thread_if_true)
2766 INSN_FROM_TARGET_P (temp) = 1;
2768 delay_list = add_to_delay_list (temp, delay_list);
2770 if (slots_to_fill == ++(*pslots_filled))
2772 /* Even though we have filled all the slots, we
2773 may be branching to a location that has a
2774 redundant insn. Skip any if so. */
2775 while (new_thread && ! own_thread
2776 && ! insn_sets_resource_p (new_thread, &set, 1)
2777 && ! insn_sets_resource_p (new_thread, &needed, 1)
2778 && ! insn_references_resource_p (new_thread,
2779 &set, 1)
2780 && (prior_insn
2781 = redundant_insn (new_thread, insn,
2782 delay_list)))
2784 /* We know we do not own the thread, so no need
2785 to call update_block and delete_insn. */
2786 fix_reg_dead_note (prior_insn, insn);
2787 update_reg_unused_notes (prior_insn, new_thread);
2788 new_thread = next_active_insn (new_thread);
2790 break;
2793 continue;
2798 /* This insn can't go into a delay slot. */
2799 lose = 1;
2800 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2801 mark_referenced_resources (trial, &needed, 1);
2803 /* Ensure we don't put insns between the setting of cc and the comparison
2804 by moving a setting of cc into an earlier delay slot since these insns
2805 could clobber the condition code. */
2806 set.cc = 1;
2808 /* If this insn is a register-register copy and the next insn has
2809 a use of our destination, change it to use our source. That way,
2810 it will become a candidate for our delay slot the next time
2811 through this loop. This case occurs commonly in loops that
2812 scan a list.
2814 We could check for more complex cases than those tested below,
2815 but it doesn't seem worth it. It might also be a good idea to try
2816 to swap the two insns. That might do better.
2818 We can't do this if the next insn modifies our destination, because
2819 that would make the replacement into the insn invalid. We also can't
2820 do this if it modifies our source, because it might be an earlyclobber
2821 operand. This latter test also prevents updating the contents of
2822 a PRE_INC. We also can't do this if there's overlap of source and
2823 destination. Overlap may happen for larger-than-register-size modes. */
2825 if (GET_CODE (trial) == INSN && GET_CODE (pat) == SET
2826 && GET_CODE (SET_SRC (pat)) == REG
2827 && GET_CODE (SET_DEST (pat)) == REG
2828 && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2830 rtx next = next_nonnote_insn (trial);
2832 if (next && GET_CODE (next) == INSN
2833 && GET_CODE (PATTERN (next)) != USE
2834 && ! reg_set_p (SET_DEST (pat), next)
2835 && ! reg_set_p (SET_SRC (pat), next)
2836 && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2837 && ! modified_in_p (SET_DEST (pat), next))
2838 validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2842 /* If we stopped on a branch insn that has delay slots, see if we can
2843 steal some of the insns in those slots. */
2844 if (trial && GET_CODE (trial) == INSN
2845 && GET_CODE (PATTERN (trial)) == SEQUENCE
2846 && GET_CODE (XVECEXP (PATTERN (trial), 0, 0)) == JUMP_INSN)
2848 /* If this is the `true' thread, we will want to follow the jump,
2849 so we can only do this if we have taken everything up to here. */
2850 if (thread_if_true && trial == new_thread)
2852 delay_list
2853 = steal_delay_list_from_target (insn, condition, PATTERN (trial),
2854 delay_list, &set, &needed,
2855 &opposite_needed, slots_to_fill,
2856 pslots_filled, &must_annul,
2857 &new_thread);
2858 /* If we owned the thread and are told that it branched
2859 elsewhere, make sure we own the thread at the new location. */
2860 if (own_thread && trial != new_thread)
2861 own_thread = own_thread_p (new_thread, new_thread, 0);
2863 else if (! thread_if_true)
2864 delay_list
2865 = steal_delay_list_from_fallthrough (insn, condition,
2866 PATTERN (trial),
2867 delay_list, &set, &needed,
2868 &opposite_needed, slots_to_fill,
2869 pslots_filled, &must_annul);
2872 /* If we haven't found anything for this delay slot and it is very
2873 likely that the branch will be taken, see if the insn at our target
2874 increments or decrements a register with an increment that does not
2875 depend on the destination register. If so, try to place the opposite
2876 arithmetic insn after the jump insn and put the arithmetic insn in the
2877 delay slot. If we can't do this, return. */
2878 if (delay_list == 0 && likely && new_thread
2879 && GET_CODE (new_thread) == INSN
2880 && GET_CODE (PATTERN (new_thread)) != ASM_INPUT
2881 && asm_noperands (PATTERN (new_thread)) < 0)
2883 rtx pat = PATTERN (new_thread);
2884 rtx dest;
2885 rtx src;
2887 trial = new_thread;
2888 pat = PATTERN (trial);
2890 if (GET_CODE (trial) != INSN
2891 || GET_CODE (pat) != SET
2892 || ! eligible_for_delay (insn, 0, trial, flags)
2893 || can_throw_internal (trial))
2894 return 0;
2896 dest = SET_DEST (pat), src = SET_SRC (pat);
2897 if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2898 && rtx_equal_p (XEXP (src, 0), dest)
2899 && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2900 && ! side_effects_p (pat))
2902 rtx other = XEXP (src, 1);
2903 rtx new_arith;
2904 rtx ninsn;
2906 /* If this is a constant adjustment, use the same code with
2907 the negated constant. Otherwise, reverse the sense of the
2908 arithmetic. */
2909 if (GET_CODE (other) == CONST_INT)
2910 new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2911 negate_rtx (GET_MODE (src), other));
2912 else
2913 new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2914 GET_MODE (src), dest, other);
2916 ninsn = emit_insn_after (gen_rtx_SET (VOIDmode, dest, new_arith),
2917 insn);
2919 if (recog_memoized (ninsn) < 0
2920 || (extract_insn (ninsn), ! constrain_operands (1)))
2922 delete_related_insns (ninsn);
2923 return 0;
2926 if (own_thread)
2928 update_block (trial, thread);
2929 if (trial == thread)
2931 thread = next_active_insn (thread);
2932 if (new_thread == trial)
2933 new_thread = thread;
2935 delete_related_insns (trial);
2937 else
2938 new_thread = next_active_insn (trial);
2940 ninsn = own_thread ? trial : copy_rtx (trial);
2941 if (thread_if_true)
2942 INSN_FROM_TARGET_P (ninsn) = 1;
2944 delay_list = add_to_delay_list (ninsn, NULL_RTX);
2945 (*pslots_filled)++;
2949 if (delay_list && must_annul)
2950 INSN_ANNULLED_BRANCH_P (insn) = 1;
2952 /* If we are to branch into the middle of this thread, find an appropriate
2953 label or make a new one if none, and redirect INSN to it. If we hit the
2954 end of the function, use the end-of-function label. */
2955 if (new_thread != thread)
2957 rtx label;
2959 if (! thread_if_true)
2960 abort ();
2962 if (new_thread && GET_CODE (new_thread) == JUMP_INSN
2963 && (simplejump_p (new_thread)
2964 || GET_CODE (PATTERN (new_thread)) == RETURN)
2965 && redirect_with_delay_list_safe_p (insn,
2966 JUMP_LABEL (new_thread),
2967 delay_list))
2968 new_thread = follow_jumps (JUMP_LABEL (new_thread));
2970 if (new_thread == 0)
2971 label = find_end_label ();
2972 else if (GET_CODE (new_thread) == CODE_LABEL)
2973 label = new_thread;
2974 else
2975 label = get_label_before (new_thread);
2977 reorg_redirect_jump (insn, label);
2980 return delay_list;
2983 /* Make another attempt to find insns to place in delay slots.
2985 We previously looked for insns located in front of the delay insn
2986 and, for non-jump delay insns, located behind the delay insn.
2988 Here only try to schedule jump insns and try to move insns from either
2989 the target or the following insns into the delay slot. If annulling is
2990 supported, we will be likely to do this. Otherwise, we can do this only
2991 if safe. */
2993 static void
2994 fill_eager_delay_slots ()
2996 rtx insn;
2997 int i;
2998 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
3000 for (i = 0; i < num_unfilled_slots; i++)
3002 rtx condition;
3003 rtx target_label, insn_at_target, fallthrough_insn;
3004 rtx delay_list = 0;
3005 int own_target;
3006 int own_fallthrough;
3007 int prediction, slots_to_fill, slots_filled;
3009 insn = unfilled_slots_base[i];
3010 if (insn == 0
3011 || INSN_DELETED_P (insn)
3012 || GET_CODE (insn) != JUMP_INSN
3013 || ! (condjump_p (insn) || condjump_in_parallel_p (insn)))
3014 continue;
3016 slots_to_fill = num_delay_slots (insn);
3017 /* Some machine description have defined instructions to have
3018 delay slots only in certain circumstances which may depend on
3019 nearby insns (which change due to reorg's actions).
3021 For example, the PA port normally has delay slots for unconditional
3022 jumps.
3024 However, the PA port claims such jumps do not have a delay slot
3025 if they are immediate successors of certain CALL_INSNs. This
3026 allows the port to favor filling the delay slot of the call with
3027 the unconditional jump. */
3028 if (slots_to_fill == 0)
3029 continue;
3031 slots_filled = 0;
3032 target_label = JUMP_LABEL (insn);
3033 condition = get_branch_condition (insn, target_label);
3035 if (condition == 0)
3036 continue;
3038 /* Get the next active fallthrough and target insns and see if we own
3039 them. Then see whether the branch is likely true. We don't need
3040 to do a lot of this for unconditional branches. */
3042 insn_at_target = next_active_insn (target_label);
3043 own_target = own_thread_p (target_label, target_label, 0);
3045 if (condition == const_true_rtx)
3047 own_fallthrough = 0;
3048 fallthrough_insn = 0;
3049 prediction = 2;
3051 else
3053 fallthrough_insn = next_active_insn (insn);
3054 own_fallthrough = own_thread_p (NEXT_INSN (insn), NULL_RTX, 1);
3055 prediction = mostly_true_jump (insn, condition);
3058 /* If this insn is expected to branch, first try to get insns from our
3059 target, then our fallthrough insns. If it is not expected to branch,
3060 try the other order. */
3062 if (prediction > 0)
3064 delay_list
3065 = fill_slots_from_thread (insn, condition, insn_at_target,
3066 fallthrough_insn, prediction == 2, 1,
3067 own_target,
3068 slots_to_fill, &slots_filled, delay_list);
3070 if (delay_list == 0 && own_fallthrough)
3072 /* Even though we didn't find anything for delay slots,
3073 we might have found a redundant insn which we deleted
3074 from the thread that was filled. So we have to recompute
3075 the next insn at the target. */
3076 target_label = JUMP_LABEL (insn);
3077 insn_at_target = next_active_insn (target_label);
3079 delay_list
3080 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3081 insn_at_target, 0, 0,
3082 own_fallthrough,
3083 slots_to_fill, &slots_filled,
3084 delay_list);
3087 else
3089 if (own_fallthrough)
3090 delay_list
3091 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3092 insn_at_target, 0, 0,
3093 own_fallthrough,
3094 slots_to_fill, &slots_filled,
3095 delay_list);
3097 if (delay_list == 0)
3098 delay_list
3099 = fill_slots_from_thread (insn, condition, insn_at_target,
3100 next_active_insn (insn), 0, 1,
3101 own_target,
3102 slots_to_fill, &slots_filled,
3103 delay_list);
3106 if (delay_list)
3107 unfilled_slots_base[i]
3108 = emit_delay_sequence (insn, delay_list, slots_filled);
3110 if (slots_to_fill == slots_filled)
3111 unfilled_slots_base[i] = 0;
3113 note_delay_statistics (slots_filled, 1);
3117 /* Once we have tried two ways to fill a delay slot, make a pass over the
3118 code to try to improve the results and to do such things as more jump
3119 threading. */
3121 static void
3122 relax_delay_slots (first)
3123 rtx first;
3125 rtx insn, next, pat;
3126 rtx trial, delay_insn, target_label;
3128 /* Look at every JUMP_INSN and see if we can improve it. */
3129 for (insn = first; insn; insn = next)
3131 rtx other;
3133 next = next_active_insn (insn);
3135 /* If this is a jump insn, see if it now jumps to a jump, jumps to
3136 the next insn, or jumps to a label that is not the last of a
3137 group of consecutive labels. */
3138 if (GET_CODE (insn) == JUMP_INSN
3139 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3140 && (target_label = JUMP_LABEL (insn)) != 0)
3142 target_label = follow_jumps (target_label);
3143 target_label = prev_label (next_active_insn (target_label));
3145 if (target_label == 0)
3146 target_label = find_end_label ();
3148 if (next_active_insn (target_label) == next
3149 && ! condjump_in_parallel_p (insn))
3151 delete_jump (insn);
3152 continue;
3155 if (target_label != JUMP_LABEL (insn))
3156 reorg_redirect_jump (insn, target_label);
3158 /* See if this jump branches around an unconditional jump.
3159 If so, invert this jump and point it to the target of the
3160 second jump. */
3161 if (next && GET_CODE (next) == JUMP_INSN
3162 && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3163 && next_active_insn (target_label) == next_active_insn (next)
3164 && no_labels_between_p (insn, next))
3166 rtx label = JUMP_LABEL (next);
3168 /* Be careful how we do this to avoid deleting code or
3169 labels that are momentarily dead. See similar optimization
3170 in jump.c.
3172 We also need to ensure we properly handle the case when
3173 invert_jump fails. */
3175 ++LABEL_NUSES (target_label);
3176 if (label)
3177 ++LABEL_NUSES (label);
3179 if (invert_jump (insn, label, 1))
3181 delete_related_insns (next);
3182 next = insn;
3185 if (label)
3186 --LABEL_NUSES (label);
3188 if (--LABEL_NUSES (target_label) == 0)
3189 delete_related_insns (target_label);
3191 continue;
3195 /* If this is an unconditional jump and the previous insn is a
3196 conditional jump, try reversing the condition of the previous
3197 insn and swapping our targets. The next pass might be able to
3198 fill the slots.
3200 Don't do this if we expect the conditional branch to be true, because
3201 we would then be making the more common case longer. */
3203 if (GET_CODE (insn) == JUMP_INSN
3204 && (simplejump_p (insn) || GET_CODE (PATTERN (insn)) == RETURN)
3205 && (other = prev_active_insn (insn)) != 0
3206 && (condjump_p (other) || condjump_in_parallel_p (other))
3207 && no_labels_between_p (other, insn)
3208 && 0 > mostly_true_jump (other,
3209 get_branch_condition (other,
3210 JUMP_LABEL (other))))
3212 rtx other_target = JUMP_LABEL (other);
3213 target_label = JUMP_LABEL (insn);
3215 if (invert_jump (other, target_label, 0))
3216 reorg_redirect_jump (insn, other_target);
3219 /* Now look only at cases where we have filled a delay slot. */
3220 if (GET_CODE (insn) != INSN
3221 || GET_CODE (PATTERN (insn)) != SEQUENCE)
3222 continue;
3224 pat = PATTERN (insn);
3225 delay_insn = XVECEXP (pat, 0, 0);
3227 /* See if the first insn in the delay slot is redundant with some
3228 previous insn. Remove it from the delay slot if so; then set up
3229 to reprocess this insn. */
3230 if (redundant_insn (XVECEXP (pat, 0, 1), delay_insn, 0))
3232 delete_from_delay_slot (XVECEXP (pat, 0, 1));
3233 next = prev_active_insn (next);
3234 continue;
3237 /* See if we have a RETURN insn with a filled delay slot followed
3238 by a RETURN insn with an unfilled a delay slot. If so, we can delete
3239 the first RETURN (but not it's delay insn). This gives the same
3240 effect in fewer instructions.
3242 Only do so if optimizing for size since this results in slower, but
3243 smaller code. */
3244 if (optimize_size
3245 && GET_CODE (PATTERN (delay_insn)) == RETURN
3246 && next
3247 && GET_CODE (next) == JUMP_INSN
3248 && GET_CODE (PATTERN (next)) == RETURN)
3250 rtx after;
3251 int i;
3253 /* Delete the RETURN and just execute the delay list insns.
3255 We do this by deleting the INSN containing the SEQUENCE, then
3256 re-emitting the insns separately, and then deleting the RETURN.
3257 This allows the count of the jump target to be properly
3258 decremented. */
3260 /* Clear the from target bit, since these insns are no longer
3261 in delay slots. */
3262 for (i = 0; i < XVECLEN (pat, 0); i++)
3263 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3265 trial = PREV_INSN (insn);
3266 delete_related_insns (insn);
3267 if (GET_CODE (pat) != SEQUENCE)
3268 abort ();
3269 after = trial;
3270 for (i = 0; i < XVECLEN (pat, 0); i++)
3272 rtx this_insn = XVECEXP (pat, 0, i);
3273 add_insn_after (this_insn, after);
3274 after = this_insn;
3276 delete_scheduled_jump (delay_insn);
3277 continue;
3280 /* Now look only at the cases where we have a filled JUMP_INSN. */
3281 if (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) != JUMP_INSN
3282 || ! (condjump_p (XVECEXP (PATTERN (insn), 0, 0))
3283 || condjump_in_parallel_p (XVECEXP (PATTERN (insn), 0, 0))))
3284 continue;
3286 target_label = JUMP_LABEL (delay_insn);
3288 if (target_label)
3290 /* If this jump goes to another unconditional jump, thread it, but
3291 don't convert a jump into a RETURN here. */
3292 trial = follow_jumps (target_label);
3293 /* We use next_real_insn instead of next_active_insn, so that
3294 the special USE insns emitted by reorg won't be ignored.
3295 If they are ignored, then they will get deleted if target_label
3296 is now unreachable, and that would cause mark_target_live_regs
3297 to fail. */
3298 trial = prev_label (next_real_insn (trial));
3299 if (trial == 0 && target_label != 0)
3300 trial = find_end_label ();
3302 if (trial != target_label
3303 && redirect_with_delay_slots_safe_p (delay_insn, trial, insn))
3305 reorg_redirect_jump (delay_insn, trial);
3306 target_label = trial;
3309 /* If the first insn at TARGET_LABEL is redundant with a previous
3310 insn, redirect the jump to the following insn process again. */
3311 trial = next_active_insn (target_label);
3312 if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3313 && redundant_insn (trial, insn, 0)
3314 && ! can_throw_internal (trial))
3316 rtx tmp;
3318 /* Figure out where to emit the special USE insn so we don't
3319 later incorrectly compute register live/death info. */
3320 tmp = next_active_insn (trial);
3321 if (tmp == 0)
3322 tmp = find_end_label ();
3324 /* Insert the special USE insn and update dataflow info. */
3325 update_block (trial, tmp);
3327 /* Now emit a label before the special USE insn, and
3328 redirect our jump to the new label. */
3329 target_label = get_label_before (PREV_INSN (tmp));
3330 reorg_redirect_jump (delay_insn, target_label);
3331 next = insn;
3332 continue;
3335 /* Similarly, if it is an unconditional jump with one insn in its
3336 delay list and that insn is redundant, thread the jump. */
3337 if (trial && GET_CODE (PATTERN (trial)) == SEQUENCE
3338 && XVECLEN (PATTERN (trial), 0) == 2
3339 && GET_CODE (XVECEXP (PATTERN (trial), 0, 0)) == JUMP_INSN
3340 && (simplejump_p (XVECEXP (PATTERN (trial), 0, 0))
3341 || GET_CODE (PATTERN (XVECEXP (PATTERN (trial), 0, 0))) == RETURN)
3342 && redundant_insn (XVECEXP (PATTERN (trial), 0, 1), insn, 0))
3344 target_label = JUMP_LABEL (XVECEXP (PATTERN (trial), 0, 0));
3345 if (target_label == 0)
3346 target_label = find_end_label ();
3348 if (redirect_with_delay_slots_safe_p (delay_insn, target_label,
3349 insn))
3351 reorg_redirect_jump (delay_insn, target_label);
3352 next = insn;
3353 continue;
3358 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3359 && prev_active_insn (target_label) == insn
3360 && ! condjump_in_parallel_p (delay_insn)
3361 #ifdef HAVE_cc0
3362 /* If the last insn in the delay slot sets CC0 for some insn,
3363 various code assumes that it is in a delay slot. We could
3364 put it back where it belonged and delete the register notes,
3365 but it doesn't seem worthwhile in this uncommon case. */
3366 && ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3367 REG_CC_USER, NULL_RTX)
3368 #endif
3371 rtx after;
3372 int i;
3374 /* All this insn does is execute its delay list and jump to the
3375 following insn. So delete the jump and just execute the delay
3376 list insns.
3378 We do this by deleting the INSN containing the SEQUENCE, then
3379 re-emitting the insns separately, and then deleting the jump.
3380 This allows the count of the jump target to be properly
3381 decremented. */
3383 /* Clear the from target bit, since these insns are no longer
3384 in delay slots. */
3385 for (i = 0; i < XVECLEN (pat, 0); i++)
3386 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3388 trial = PREV_INSN (insn);
3389 delete_related_insns (insn);
3390 if (GET_CODE (pat) != SEQUENCE)
3391 abort ();
3392 after = trial;
3393 for (i = 0; i < XVECLEN (pat, 0); i++)
3395 rtx this_insn = XVECEXP (pat, 0, i);
3396 add_insn_after (this_insn, after);
3397 after = this_insn;
3399 delete_scheduled_jump (delay_insn);
3400 continue;
3403 /* See if this is an unconditional jump around a single insn which is
3404 identical to the one in its delay slot. In this case, we can just
3405 delete the branch and the insn in its delay slot. */
3406 if (next && GET_CODE (next) == INSN
3407 && prev_label (next_active_insn (next)) == target_label
3408 && simplejump_p (insn)
3409 && XVECLEN (pat, 0) == 2
3410 && rtx_equal_p (PATTERN (next), PATTERN (XVECEXP (pat, 0, 1))))
3412 delete_related_insns (insn);
3413 continue;
3416 /* See if this jump (with its delay slots) branches around another
3417 jump (without delay slots). If so, invert this jump and point
3418 it to the target of the second jump. We cannot do this for
3419 annulled jumps, though. Again, don't convert a jump to a RETURN
3420 here. */
3421 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3422 && next && GET_CODE (next) == JUMP_INSN
3423 && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3424 && next_active_insn (target_label) == next_active_insn (next)
3425 && no_labels_between_p (insn, next))
3427 rtx label = JUMP_LABEL (next);
3428 rtx old_label = JUMP_LABEL (delay_insn);
3430 if (label == 0)
3431 label = find_end_label ();
3433 /* find_end_label can generate a new label. Check this first. */
3434 if (no_labels_between_p (insn, next)
3435 && redirect_with_delay_slots_safe_p (delay_insn, label, insn))
3437 /* Be careful how we do this to avoid deleting code or labels
3438 that are momentarily dead. See similar optimization in
3439 jump.c */
3440 if (old_label)
3441 ++LABEL_NUSES (old_label);
3443 if (invert_jump (delay_insn, label, 1))
3445 int i;
3447 /* Must update the INSN_FROM_TARGET_P bits now that
3448 the branch is reversed, so that mark_target_live_regs
3449 will handle the delay slot insn correctly. */
3450 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
3452 rtx slot = XVECEXP (PATTERN (insn), 0, i);
3453 INSN_FROM_TARGET_P (slot) = ! INSN_FROM_TARGET_P (slot);
3456 delete_related_insns (next);
3457 next = insn;
3460 if (old_label && --LABEL_NUSES (old_label) == 0)
3461 delete_related_insns (old_label);
3462 continue;
3466 /* If we own the thread opposite the way this insn branches, see if we
3467 can merge its delay slots with following insns. */
3468 if (INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3469 && own_thread_p (NEXT_INSN (insn), 0, 1))
3470 try_merge_delay_insns (insn, next);
3471 else if (! INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3472 && own_thread_p (target_label, target_label, 0))
3473 try_merge_delay_insns (insn, next_active_insn (target_label));
3475 /* If we get here, we haven't deleted INSN. But we may have deleted
3476 NEXT, so recompute it. */
3477 next = next_active_insn (insn);
3481 #ifdef HAVE_return
3483 /* Look for filled jumps to the end of function label. We can try to convert
3484 them into RETURN insns if the insns in the delay slot are valid for the
3485 RETURN as well. */
3487 static void
3488 make_return_insns (first)
3489 rtx first;
3491 rtx insn, jump_insn, pat;
3492 rtx real_return_label = end_of_function_label;
3493 int slots, i;
3495 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3496 /* If a previous pass filled delay slots in the epilogue, things get a
3497 bit more complicated, as those filler insns would generally (without
3498 data flow analysis) have to be executed after any existing branch
3499 delay slot filler insns. It is also unknown whether such a
3500 transformation would actually be profitable. Note that the existing
3501 code only cares for branches with (some) filled delay slots. */
3502 if (current_function_epilogue_delay_list != NULL)
3503 return;
3504 #endif
3506 /* See if there is a RETURN insn in the function other than the one we
3507 made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3508 into a RETURN to jump to it. */
3509 for (insn = first; insn; insn = NEXT_INSN (insn))
3510 if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) == RETURN)
3512 real_return_label = get_label_before (insn);
3513 break;
3516 /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3517 was equal to END_OF_FUNCTION_LABEL. */
3518 LABEL_NUSES (real_return_label)++;
3520 /* Clear the list of insns to fill so we can use it. */
3521 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3523 for (insn = first; insn; insn = NEXT_INSN (insn))
3525 int flags;
3527 /* Only look at filled JUMP_INSNs that go to the end of function
3528 label. */
3529 if (GET_CODE (insn) != INSN
3530 || GET_CODE (PATTERN (insn)) != SEQUENCE
3531 || GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) != JUMP_INSN
3532 || JUMP_LABEL (XVECEXP (PATTERN (insn), 0, 0)) != end_of_function_label)
3533 continue;
3535 pat = PATTERN (insn);
3536 jump_insn = XVECEXP (pat, 0, 0);
3538 /* If we can't make the jump into a RETURN, try to redirect it to the best
3539 RETURN and go on to the next insn. */
3540 if (! reorg_redirect_jump (jump_insn, NULL_RTX))
3542 /* Make sure redirecting the jump will not invalidate the delay
3543 slot insns. */
3544 if (redirect_with_delay_slots_safe_p (jump_insn,
3545 real_return_label,
3546 insn))
3547 reorg_redirect_jump (jump_insn, real_return_label);
3548 continue;
3551 /* See if this RETURN can accept the insns current in its delay slot.
3552 It can if it has more or an equal number of slots and the contents
3553 of each is valid. */
3555 flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3556 slots = num_delay_slots (jump_insn);
3557 if (slots >= XVECLEN (pat, 0) - 1)
3559 for (i = 1; i < XVECLEN (pat, 0); i++)
3560 if (! (
3561 #ifdef ANNUL_IFFALSE_SLOTS
3562 (INSN_ANNULLED_BRANCH_P (jump_insn)
3563 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3564 ? eligible_for_annul_false (jump_insn, i - 1,
3565 XVECEXP (pat, 0, i), flags) :
3566 #endif
3567 #ifdef ANNUL_IFTRUE_SLOTS
3568 (INSN_ANNULLED_BRANCH_P (jump_insn)
3569 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3570 ? eligible_for_annul_true (jump_insn, i - 1,
3571 XVECEXP (pat, 0, i), flags) :
3572 #endif
3573 eligible_for_delay (jump_insn, i - 1,
3574 XVECEXP (pat, 0, i), flags)))
3575 break;
3577 else
3578 i = 0;
3580 if (i == XVECLEN (pat, 0))
3581 continue;
3583 /* We have to do something with this insn. If it is an unconditional
3584 RETURN, delete the SEQUENCE and output the individual insns,
3585 followed by the RETURN. Then set things up so we try to find
3586 insns for its delay slots, if it needs some. */
3587 if (GET_CODE (PATTERN (jump_insn)) == RETURN)
3589 rtx prev = PREV_INSN (insn);
3591 delete_related_insns (insn);
3592 for (i = 1; i < XVECLEN (pat, 0); i++)
3593 prev = emit_insn_after (PATTERN (XVECEXP (pat, 0, i)), prev);
3595 insn = emit_jump_insn_after (PATTERN (jump_insn), prev);
3596 emit_barrier_after (insn);
3598 if (slots)
3599 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3601 else
3602 /* It is probably more efficient to keep this with its current
3603 delay slot as a branch to a RETURN. */
3604 reorg_redirect_jump (jump_insn, real_return_label);
3607 /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3608 new delay slots we have created. */
3609 if (--LABEL_NUSES (real_return_label) == 0)
3610 delete_related_insns (real_return_label);
3612 fill_simple_delay_slots (1);
3613 fill_simple_delay_slots (0);
3615 #endif
3617 /* Try to find insns to place in delay slots. */
3619 void
3620 dbr_schedule (first, file)
3621 rtx first;
3622 FILE *file;
3624 rtx insn, next, epilogue_insn = 0;
3625 int i;
3626 #if 0
3627 int old_flag_no_peephole = flag_no_peephole;
3629 /* Execute `final' once in prescan mode to delete any insns that won't be
3630 used. Don't let final try to do any peephole optimization--it will
3631 ruin dataflow information for this pass. */
3633 flag_no_peephole = 1;
3634 final (first, 0, NO_DEBUG, 1, 1);
3635 flag_no_peephole = old_flag_no_peephole;
3636 #endif
3638 /* If the current function has no insns other than the prologue and
3639 epilogue, then do not try to fill any delay slots. */
3640 if (n_basic_blocks == 0)
3641 return;
3643 /* Find the highest INSN_UID and allocate and initialize our map from
3644 INSN_UID's to position in code. */
3645 for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3647 if (INSN_UID (insn) > max_uid)
3648 max_uid = INSN_UID (insn);
3649 if (GET_CODE (insn) == NOTE
3650 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
3651 epilogue_insn = insn;
3654 uid_to_ruid = (int *) xmalloc ((max_uid + 1) * sizeof (int));
3655 for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3656 uid_to_ruid[INSN_UID (insn)] = i;
3658 /* Initialize the list of insns that need filling. */
3659 if (unfilled_firstobj == 0)
3661 gcc_obstack_init (&unfilled_slots_obstack);
3662 unfilled_firstobj = (rtx *) obstack_alloc (&unfilled_slots_obstack, 0);
3665 for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3667 rtx target;
3669 INSN_ANNULLED_BRANCH_P (insn) = 0;
3670 INSN_FROM_TARGET_P (insn) = 0;
3672 /* Skip vector tables. We can't get attributes for them. */
3673 if (GET_CODE (insn) == JUMP_INSN
3674 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
3675 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
3676 continue;
3678 if (num_delay_slots (insn) > 0)
3679 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3681 /* Ensure all jumps go to the last of a set of consecutive labels. */
3682 if (GET_CODE (insn) == JUMP_INSN
3683 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3684 && JUMP_LABEL (insn) != 0
3685 && ((target = prev_label (next_active_insn (JUMP_LABEL (insn))))
3686 != JUMP_LABEL (insn)))
3687 redirect_jump (insn, target, 1);
3690 init_resource_info (epilogue_insn);
3692 /* Show we haven't computed an end-of-function label yet. */
3693 end_of_function_label = 0;
3695 /* Initialize the statistics for this function. */
3696 memset ((char *) num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3697 memset ((char *) num_filled_delays, 0, sizeof num_filled_delays);
3699 /* Now do the delay slot filling. Try everything twice in case earlier
3700 changes make more slots fillable. */
3702 for (reorg_pass_number = 0;
3703 reorg_pass_number < MAX_REORG_PASSES;
3704 reorg_pass_number++)
3706 fill_simple_delay_slots (1);
3707 fill_simple_delay_slots (0);
3708 fill_eager_delay_slots ();
3709 relax_delay_slots (first);
3712 /* Delete any USE insns made by update_block; subsequent passes don't need
3713 them or know how to deal with them. */
3714 for (insn = first; insn; insn = next)
3716 next = NEXT_INSN (insn);
3718 if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE
3719 && INSN_P (XEXP (PATTERN (insn), 0)))
3720 next = delete_related_insns (insn);
3723 /* If we made an end of function label, indicate that it is now
3724 safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3725 If it is now unused, delete it. */
3726 if (end_of_function_label && --LABEL_NUSES (end_of_function_label) == 0)
3727 delete_related_insns (end_of_function_label);
3729 #ifdef HAVE_return
3730 if (HAVE_return && end_of_function_label != 0)
3731 make_return_insns (first);
3732 #endif
3734 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3736 /* It is not clear why the line below is needed, but it does seem to be. */
3737 unfilled_firstobj = (rtx *) obstack_alloc (&unfilled_slots_obstack, 0);
3739 if (file)
3741 int i, j, need_comma;
3742 int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3743 int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
3745 for (reorg_pass_number = 0;
3746 reorg_pass_number < MAX_REORG_PASSES;
3747 reorg_pass_number++)
3749 fprintf (file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
3750 for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
3752 need_comma = 0;
3753 fprintf (file, ";; Reorg function #%d\n", i);
3755 fprintf (file, ";; %d insns needing delay slots\n;; ",
3756 num_insns_needing_delays[i][reorg_pass_number]);
3758 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3759 if (num_filled_delays[i][j][reorg_pass_number])
3761 if (need_comma)
3762 fprintf (file, ", ");
3763 need_comma = 1;
3764 fprintf (file, "%d got %d delays",
3765 num_filled_delays[i][j][reorg_pass_number], j);
3767 fprintf (file, "\n");
3770 memset ((char *) total_delay_slots, 0, sizeof total_delay_slots);
3771 memset ((char *) total_annul_slots, 0, sizeof total_annul_slots);
3772 for (insn = first; insn; insn = NEXT_INSN (insn))
3774 if (! INSN_DELETED_P (insn)
3775 && GET_CODE (insn) == INSN
3776 && GET_CODE (PATTERN (insn)) != USE
3777 && GET_CODE (PATTERN (insn)) != CLOBBER)
3779 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3781 j = XVECLEN (PATTERN (insn), 0) - 1;
3782 if (j > MAX_DELAY_HISTOGRAM)
3783 j = MAX_DELAY_HISTOGRAM;
3784 if (INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (insn), 0, 0)))
3785 total_annul_slots[j]++;
3786 else
3787 total_delay_slots[j]++;
3789 else if (num_delay_slots (insn) > 0)
3790 total_delay_slots[0]++;
3793 fprintf (file, ";; Reorg totals: ");
3794 need_comma = 0;
3795 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3797 if (total_delay_slots[j])
3799 if (need_comma)
3800 fprintf (file, ", ");
3801 need_comma = 1;
3802 fprintf (file, "%d got %d delays", total_delay_slots[j], j);
3805 fprintf (file, "\n");
3806 #if defined (ANNUL_IFTRUE_SLOTS) || defined (ANNUL_IFFALSE_SLOTS)
3807 fprintf (file, ";; Reorg annuls: ");
3808 need_comma = 0;
3809 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3811 if (total_annul_slots[j])
3813 if (need_comma)
3814 fprintf (file, ", ");
3815 need_comma = 1;
3816 fprintf (file, "%d got %d delays", total_annul_slots[j], j);
3819 fprintf (file, "\n");
3820 #endif
3821 fprintf (file, "\n");
3824 /* For all JUMP insns, fill in branch prediction notes, so that during
3825 assembler output a target can set branch prediction bits in the code.
3826 We have to do this now, as up until this point the destinations of
3827 JUMPS can be moved around and changed, but past right here that cannot
3828 happen. */
3829 for (insn = first; insn; insn = NEXT_INSN (insn))
3831 int pred_flags;
3833 if (GET_CODE (insn) == INSN)
3835 rtx pat = PATTERN (insn);
3837 if (GET_CODE (pat) == SEQUENCE)
3838 insn = XVECEXP (pat, 0, 0);
3840 if (GET_CODE (insn) != JUMP_INSN)
3841 continue;
3843 pred_flags = get_jump_flags (insn, JUMP_LABEL (insn));
3844 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_BR_PRED,
3845 GEN_INT (pred_flags),
3846 REG_NOTES (insn));
3848 free_resource_info ();
3849 free (uid_to_ruid);
3850 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3851 /* SPARC assembler, for instance, emit warning when debug info is output
3852 into the delay slot. */
3854 rtx link;
3856 for (link = current_function_epilogue_delay_list;
3857 link;
3858 link = XEXP (link, 1))
3859 INSN_LOCATOR (XEXP (link, 0)) = 0;
3861 #endif
3863 #endif /* DELAY_SLOTS */