* config/c4x/c4x.h (INITIALIZE_TRAMPOLINE): Replace 'tramp' with 'TRAMP' in
[official-gcc.git] / gcc / reorg.c
blob2ae0a8f2259f57a77c6be28daf82f21b0494f86f
1 /* Perform instruction reorganizations for delay slot filling.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 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 has a single branch delay slot. Most insns
40 (except other branches) can be used to fill this slot. When the
41 slot is filled, two insns execute in two cycles, reducing the
42 branch penalty to zero.
44 The SPARC always has a branch delay slot, but its effects can be
45 annulled when the branch is not taken. This means that failing to
46 find other sources of insns, we can hoist an insn from the branch
47 target that would only be safe to execute knowing that the branch
48 is taken.
50 The HP-PA always has a branch delay slot. For unconditional branches
51 its effects can be annulled when the branch is taken. The effects
52 of the delay slot in a conditional branch can be nullified for forward
53 taken branches, or for untaken backward branches. This means
54 we can hoist insns from the fall-through path for forward branches or
55 steal insns from the target of backward branches.
57 The TMS320C3x and C4x have three branch delay slots. When the three
58 slots are filled, the branch penalty is zero. Most insns can fill the
59 delay slots except jump insns.
61 Three techniques for filling delay slots have been implemented so far:
63 (1) `fill_simple_delay_slots' is the simplest, most efficient way
64 to fill delay slots. This pass first looks for insns which come
65 from before the branch and which are safe to execute after the
66 branch. Then it searches after the insn requiring delay slots or,
67 in the case of a branch, for insns that are after the point at
68 which the branch merges into the fallthrough code, if such a point
69 exists. When such insns are found, the branch penalty decreases
70 and no code expansion takes place.
72 (2) `fill_eager_delay_slots' is more complicated: it is used for
73 scheduling conditional jumps, or for scheduling jumps which cannot
74 be filled using (1). A machine need not have annulled jumps to use
75 this strategy, but it helps (by keeping more options open).
76 `fill_eager_delay_slots' tries to guess the direction the branch
77 will go; if it guesses right 100% of the time, it can reduce the
78 branch penalty as much as `fill_simple_delay_slots' does. If it
79 guesses wrong 100% of the time, it might as well schedule nops. When
80 `fill_eager_delay_slots' takes insns from the fall-through path of
81 the jump, usually there is no code expansion; when it takes insns
82 from the branch target, there is code expansion if it is not the
83 only way to reach that target.
85 (3) `relax_delay_slots' uses a set of rules to simplify code that
86 has been reorganized by (1) and (2). It finds cases where
87 conditional test can be eliminated, jumps can be threaded, extra
88 insns can be eliminated, etc. It is the job of (1) and (2) to do a
89 good job of scheduling locally; `relax_delay_slots' takes care of
90 making the various individual schedules work well together. It is
91 especially tuned to handle the control flow interactions of branch
92 insns. It does nothing for insns with delay slots that do not
93 branch.
95 On machines that use CC0, we are very conservative. We will not make
96 a copy of an insn involving CC0 since we want to maintain a 1-1
97 correspondence between the insn that sets and uses CC0. The insns are
98 allowed to be separated by placing an insn that sets CC0 (but not an insn
99 that uses CC0; we could do this, but it doesn't seem worthwhile) in a
100 delay slot. In that case, we point each insn at the other with REG_CC_USER
101 and REG_CC_SETTER notes. Note that these restrictions affect very few
102 machines because most RISC machines with delay slots will not use CC0
103 (the RT is the only known exception at this point).
105 Not yet implemented:
107 The Acorn Risc Machine can conditionally execute most insns, so
108 it is profitable to move single insns into a position to execute
109 based on the condition code of the previous insn.
111 The HP-PA can conditionally nullify insns, providing a similar
112 effect to the ARM, differing mostly in which insn is "in charge". */
114 #include "config.h"
115 #include "system.h"
116 #include "coretypes.h"
117 #include "tm.h"
118 #include "toplev.h"
119 #include "rtl.h"
120 #include "tm_p.h"
121 #include "expr.h"
122 #include "function.h"
123 #include "insn-config.h"
124 #include "conditions.h"
125 #include "hard-reg-set.h"
126 #include "basic-block.h"
127 #include "regs.h"
128 #include "recog.h"
129 #include "flags.h"
130 #include "output.h"
131 #include "obstack.h"
132 #include "insn-attr.h"
133 #include "resource.h"
134 #include "except.h"
135 #include "params.h"
137 #ifdef DELAY_SLOTS
139 #ifndef ANNUL_IFTRUE_SLOTS
140 #define eligible_for_annul_true(INSN, SLOTS, TRIAL, FLAGS) 0
141 #endif
142 #ifndef ANNUL_IFFALSE_SLOTS
143 #define eligible_for_annul_false(INSN, SLOTS, TRIAL, FLAGS) 0
144 #endif
146 /* Insns which have delay slots that have not yet been filled. */
148 static struct obstack unfilled_slots_obstack;
149 static rtx *unfilled_firstobj;
151 /* Define macros to refer to the first and last slot containing unfilled
152 insns. These are used because the list may move and its address
153 should be recomputed at each use. */
155 #define unfilled_slots_base \
156 ((rtx *) obstack_base (&unfilled_slots_obstack))
158 #define unfilled_slots_next \
159 ((rtx *) obstack_next_free (&unfilled_slots_obstack))
161 /* Points to the label before the end of the function. */
162 static rtx end_of_function_label;
164 /* Mapping between INSN_UID's and position in the code since INSN_UID's do
165 not always monotonically increase. */
166 static int *uid_to_ruid;
168 /* Highest valid index in `uid_to_ruid'. */
169 static int max_uid;
171 static int stop_search_p (rtx, int);
172 static int resource_conflicts_p (struct resources *, struct resources *);
173 static int insn_references_resource_p (rtx, struct resources *, int);
174 static int insn_sets_resource_p (rtx, struct resources *, int);
175 static rtx find_end_label (void);
176 static rtx emit_delay_sequence (rtx, rtx, int);
177 static rtx add_to_delay_list (rtx, rtx);
178 static rtx delete_from_delay_slot (rtx);
179 static void delete_scheduled_jump (rtx);
180 static void note_delay_statistics (int, int);
181 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
182 static rtx optimize_skip (rtx);
183 #endif
184 static int get_jump_flags (rtx, rtx);
185 static int rare_destination (rtx);
186 static int mostly_true_jump (rtx, rtx);
187 static rtx get_branch_condition (rtx, rtx);
188 static int condition_dominates_p (rtx, rtx);
189 static int redirect_with_delay_slots_safe_p (rtx, rtx, rtx);
190 static int redirect_with_delay_list_safe_p (rtx, rtx, rtx);
191 static int check_annul_list_true_false (int, rtx);
192 static rtx steal_delay_list_from_target (rtx, rtx, rtx, rtx,
193 struct resources *,
194 struct resources *,
195 struct resources *,
196 int, int *, int *, rtx *);
197 static rtx steal_delay_list_from_fallthrough (rtx, rtx, rtx, rtx,
198 struct resources *,
199 struct resources *,
200 struct resources *,
201 int, int *, int *);
202 static void try_merge_delay_insns (rtx, rtx);
203 static rtx redundant_insn (rtx, rtx, rtx);
204 static int own_thread_p (rtx, rtx, int);
205 static void update_block (rtx, rtx);
206 static int reorg_redirect_jump (rtx, rtx);
207 static void update_reg_dead_notes (rtx, rtx);
208 static void fix_reg_dead_note (rtx, rtx);
209 static void update_reg_unused_notes (rtx, rtx);
210 static void fill_simple_delay_slots (int);
211 static rtx fill_slots_from_thread (rtx, rtx, rtx, rtx, int, int, int, int,
212 int *, rtx);
213 static void fill_eager_delay_slots (void);
214 static void relax_delay_slots (rtx);
215 #ifdef HAVE_return
216 static void make_return_insns (rtx);
217 #endif
219 /* Return TRUE if this insn should stop the search for insn to fill delay
220 slots. LABELS_P indicates that labels should terminate the search.
221 In all cases, jumps terminate the search. */
223 static int
224 stop_search_p (rtx insn, int labels_p)
226 if (insn == 0)
227 return 1;
229 /* If the insn can throw an exception that is caught within the function,
230 it may effectively perform a jump from the viewpoint of the function.
231 Therefore act like for a jump. */
232 if (can_throw_internal (insn))
233 return 1;
235 switch (GET_CODE (insn))
237 case NOTE:
238 case CALL_INSN:
239 return 0;
241 case CODE_LABEL:
242 return labels_p;
244 case JUMP_INSN:
245 case BARRIER:
246 return 1;
248 case INSN:
249 /* OK unless it contains a delay slot or is an `asm' insn of some type.
250 We don't know anything about these. */
251 return (GET_CODE (PATTERN (insn)) == SEQUENCE
252 || GET_CODE (PATTERN (insn)) == ASM_INPUT
253 || asm_noperands (PATTERN (insn)) >= 0);
255 default:
256 abort ();
260 /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
261 resource set contains a volatile memory reference. Otherwise, return FALSE. */
263 static int
264 resource_conflicts_p (struct resources *res1, struct resources *res2)
266 if ((res1->cc && res2->cc) || (res1->memory && res2->memory)
267 || (res1->unch_memory && res2->unch_memory)
268 || res1->volatil || res2->volatil)
269 return 1;
271 #ifdef HARD_REG_SET
272 return (res1->regs & res2->regs) != HARD_CONST (0);
273 #else
275 int i;
277 for (i = 0; i < HARD_REG_SET_LONGS; i++)
278 if ((res1->regs[i] & res2->regs[i]) != 0)
279 return 1;
280 return 0;
282 #endif
285 /* Return TRUE if any resource marked in RES, a `struct resources', is
286 referenced by INSN. If INCLUDE_DELAYED_EFFECTS is set, return if the called
287 routine is using those resources.
289 We compute this by computing all the resources referenced by INSN and
290 seeing if this conflicts with RES. It might be faster to directly check
291 ourselves, and this is the way it used to work, but it means duplicating
292 a large block of complex code. */
294 static int
295 insn_references_resource_p (rtx insn, struct resources *res,
296 int include_delayed_effects)
298 struct resources insn_res;
300 CLEAR_RESOURCE (&insn_res);
301 mark_referenced_resources (insn, &insn_res, include_delayed_effects);
302 return resource_conflicts_p (&insn_res, res);
305 /* Return TRUE if INSN modifies resources that are marked in RES.
306 INCLUDE_DELAYED_EFFECTS is set if the actions of that routine should be
307 included. CC0 is only modified if it is explicitly set; see comments
308 in front of mark_set_resources for details. */
310 static int
311 insn_sets_resource_p (rtx insn, struct resources *res,
312 int include_delayed_effects)
314 struct resources insn_sets;
316 CLEAR_RESOURCE (&insn_sets);
317 mark_set_resources (insn, &insn_sets, 0, include_delayed_effects);
318 return resource_conflicts_p (&insn_sets, res);
321 /* Find a label at the end of the function or before a RETURN. If there is
322 none, make one. */
324 static rtx
325 find_end_label (void)
327 rtx insn;
329 /* If we found one previously, return it. */
330 if (end_of_function_label)
331 return end_of_function_label;
333 /* Otherwise, see if there is a label at the end of the function. If there
334 is, it must be that RETURN insns aren't needed, so that is our return
335 label and we don't have to do anything else. */
337 insn = get_last_insn ();
338 while (GET_CODE (insn) == NOTE
339 || (GET_CODE (insn) == INSN
340 && (GET_CODE (PATTERN (insn)) == USE
341 || GET_CODE (PATTERN (insn)) == CLOBBER)))
342 insn = PREV_INSN (insn);
344 /* When a target threads its epilogue we might already have a
345 suitable return insn. If so put a label before it for the
346 end_of_function_label. */
347 if (GET_CODE (insn) == BARRIER
348 && GET_CODE (PREV_INSN (insn)) == JUMP_INSN
349 && GET_CODE (PATTERN (PREV_INSN (insn))) == RETURN)
351 rtx temp = PREV_INSN (PREV_INSN (insn));
352 end_of_function_label = gen_label_rtx ();
353 LABEL_NUSES (end_of_function_label) = 0;
355 /* Put the label before an USE insns that may proceed the RETURN insn. */
356 while (GET_CODE (temp) == USE)
357 temp = PREV_INSN (temp);
359 emit_label_after (end_of_function_label, temp);
362 else if (GET_CODE (insn) == CODE_LABEL)
363 end_of_function_label = insn;
364 else
366 end_of_function_label = gen_label_rtx ();
367 LABEL_NUSES (end_of_function_label) = 0;
368 /* If the basic block reorder pass moves the return insn to
369 some other place try to locate it again and put our
370 end_of_function_label there. */
371 while (insn && ! (GET_CODE (insn) == JUMP_INSN
372 && (GET_CODE (PATTERN (insn)) == RETURN)))
373 insn = PREV_INSN (insn);
374 if (insn)
376 insn = PREV_INSN (insn);
378 /* Put the label before an USE insns that may proceed the
379 RETURN insn. */
380 while (GET_CODE (insn) == USE)
381 insn = PREV_INSN (insn);
383 emit_label_after (end_of_function_label, insn);
385 else
387 /* Otherwise, make a new label and emit a RETURN and BARRIER,
388 if needed. */
389 emit_label (end_of_function_label);
390 #ifdef HAVE_return
391 if (HAVE_return)
393 /* The return we make may have delay slots too. */
394 rtx insn = gen_return ();
395 insn = emit_jump_insn (insn);
396 emit_barrier ();
397 if (num_delay_slots (insn) > 0)
398 obstack_ptr_grow (&unfilled_slots_obstack, insn);
400 #endif
404 /* Show one additional use for this label so it won't go away until
405 we are done. */
406 ++LABEL_NUSES (end_of_function_label);
408 return end_of_function_label;
411 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
412 the pattern of INSN with the SEQUENCE.
414 Chain the insns so that NEXT_INSN of each insn in the sequence points to
415 the next and NEXT_INSN of the last insn in the sequence points to
416 the first insn after the sequence. Similarly for PREV_INSN. This makes
417 it easier to scan all insns.
419 Returns the SEQUENCE that replaces INSN. */
421 static rtx
422 emit_delay_sequence (rtx insn, rtx list, int length)
424 int i = 1;
425 rtx li;
426 int had_barrier = 0;
428 /* Allocate the rtvec to hold the insns and the SEQUENCE. */
429 rtvec seqv = rtvec_alloc (length + 1);
430 rtx seq = gen_rtx_SEQUENCE (VOIDmode, seqv);
431 rtx seq_insn = make_insn_raw (seq);
432 rtx first = get_insns ();
433 rtx last = get_last_insn ();
435 /* Make a copy of the insn having delay slots. */
436 rtx delay_insn = copy_rtx (insn);
438 /* If INSN is followed by a BARRIER, delete the BARRIER since it will only
439 confuse further processing. Update LAST in case it was the last insn.
440 We will put the BARRIER back in later. */
441 if (NEXT_INSN (insn) && GET_CODE (NEXT_INSN (insn)) == BARRIER)
443 delete_related_insns (NEXT_INSN (insn));
444 last = get_last_insn ();
445 had_barrier = 1;
448 /* Splice our SEQUENCE into the insn stream where INSN used to be. */
449 NEXT_INSN (seq_insn) = NEXT_INSN (insn);
450 PREV_INSN (seq_insn) = PREV_INSN (insn);
452 if (insn != last)
453 PREV_INSN (NEXT_INSN (seq_insn)) = seq_insn;
455 if (insn != first)
456 NEXT_INSN (PREV_INSN (seq_insn)) = seq_insn;
458 /* Note the calls to set_new_first_and_last_insn must occur after
459 SEQ_INSN has been completely spliced into the insn stream.
461 Otherwise CUR_INSN_UID will get set to an incorrect value because
462 set_new_first_and_last_insn will not find SEQ_INSN in the chain. */
463 if (insn == last)
464 set_new_first_and_last_insn (first, seq_insn);
466 if (insn == first)
467 set_new_first_and_last_insn (seq_insn, last);
469 /* Build our SEQUENCE and rebuild the insn chain. */
470 XVECEXP (seq, 0, 0) = delay_insn;
471 INSN_DELETED_P (delay_insn) = 0;
472 PREV_INSN (delay_insn) = PREV_INSN (seq_insn);
474 for (li = list; li; li = XEXP (li, 1), i++)
476 rtx tem = XEXP (li, 0);
477 rtx note, next;
479 /* Show that this copy of the insn isn't deleted. */
480 INSN_DELETED_P (tem) = 0;
482 XVECEXP (seq, 0, i) = tem;
483 PREV_INSN (tem) = XVECEXP (seq, 0, i - 1);
484 NEXT_INSN (XVECEXP (seq, 0, i - 1)) = tem;
486 /* SPARC assembler, for instance, emit warning when debug info is output
487 into the delay slot. */
488 if (INSN_LOCATOR (tem) && !INSN_LOCATOR (seq_insn))
489 INSN_LOCATOR (seq_insn) = INSN_LOCATOR (tem);
490 INSN_LOCATOR (tem) = 0;
492 for (note = REG_NOTES (tem); note; note = next)
494 next = XEXP (note, 1);
495 switch (REG_NOTE_KIND (note))
497 case REG_DEAD:
498 /* Remove any REG_DEAD notes because we can't rely on them now
499 that the insn has been moved. */
500 remove_note (tem, note);
501 break;
503 case REG_LABEL:
504 /* Keep the label reference count up to date. */
505 if (GET_CODE (XEXP (note, 0)) == CODE_LABEL)
506 LABEL_NUSES (XEXP (note, 0)) ++;
507 break;
509 default:
510 break;
515 NEXT_INSN (XVECEXP (seq, 0, length)) = NEXT_INSN (seq_insn);
517 /* If the previous insn is a SEQUENCE, update the NEXT_INSN pointer on the
518 last insn in that SEQUENCE to point to us. Similarly for the first
519 insn in the following insn if it is a SEQUENCE. */
521 if (PREV_INSN (seq_insn) && GET_CODE (PREV_INSN (seq_insn)) == INSN
522 && GET_CODE (PATTERN (PREV_INSN (seq_insn))) == SEQUENCE)
523 NEXT_INSN (XVECEXP (PATTERN (PREV_INSN (seq_insn)), 0,
524 XVECLEN (PATTERN (PREV_INSN (seq_insn)), 0) - 1))
525 = seq_insn;
527 if (NEXT_INSN (seq_insn) && GET_CODE (NEXT_INSN (seq_insn)) == INSN
528 && GET_CODE (PATTERN (NEXT_INSN (seq_insn))) == SEQUENCE)
529 PREV_INSN (XVECEXP (PATTERN (NEXT_INSN (seq_insn)), 0, 0)) = seq_insn;
531 /* If there used to be a BARRIER, put it back. */
532 if (had_barrier)
533 emit_barrier_after (seq_insn);
535 if (i != length + 1)
536 abort ();
538 return seq_insn;
541 /* Add INSN to DELAY_LIST and return the head of the new list. The list must
542 be in the order in which the insns are to be executed. */
544 static rtx
545 add_to_delay_list (rtx insn, rtx delay_list)
547 /* If we have an empty list, just make a new list element. If
548 INSN has its block number recorded, clear it since we may
549 be moving the insn to a new block. */
551 if (delay_list == 0)
553 clear_hashed_info_for_insn (insn);
554 return gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
557 /* Otherwise this must be an INSN_LIST. Add INSN to the end of the
558 list. */
559 XEXP (delay_list, 1) = add_to_delay_list (insn, XEXP (delay_list, 1));
561 return delay_list;
564 /* Delete INSN from the delay slot of the insn that it is in, which may
565 produce an insn with no delay slots. Return the new insn. */
567 static rtx
568 delete_from_delay_slot (rtx insn)
570 rtx trial, seq_insn, seq, prev;
571 rtx delay_list = 0;
572 int i;
574 /* We first must find the insn containing the SEQUENCE with INSN in its
575 delay slot. Do this by finding an insn, TRIAL, where
576 PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
578 for (trial = insn;
579 PREV_INSN (NEXT_INSN (trial)) == trial;
580 trial = NEXT_INSN (trial))
583 seq_insn = PREV_INSN (NEXT_INSN (trial));
584 seq = PATTERN (seq_insn);
586 /* Create a delay list consisting of all the insns other than the one
587 we are deleting (unless we were the only one). */
588 if (XVECLEN (seq, 0) > 2)
589 for (i = 1; i < XVECLEN (seq, 0); i++)
590 if (XVECEXP (seq, 0, i) != insn)
591 delay_list = add_to_delay_list (XVECEXP (seq, 0, i), delay_list);
593 /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
594 list, and rebuild the delay list if non-empty. */
595 prev = PREV_INSN (seq_insn);
596 trial = XVECEXP (seq, 0, 0);
597 delete_related_insns (seq_insn);
598 add_insn_after (trial, prev);
600 if (GET_CODE (trial) == JUMP_INSN
601 && (simplejump_p (trial) || GET_CODE (PATTERN (trial)) == RETURN))
602 emit_barrier_after (trial);
604 /* If there are any delay insns, remit them. Otherwise clear the
605 annul flag. */
606 if (delay_list)
607 trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2);
608 else if (GET_CODE (trial) == JUMP_INSN
609 || GET_CODE (trial) == CALL_INSN
610 || GET_CODE (trial) == INSN)
611 INSN_ANNULLED_BRANCH_P (trial) = 0;
613 INSN_FROM_TARGET_P (insn) = 0;
615 /* Show we need to fill this insn again. */
616 obstack_ptr_grow (&unfilled_slots_obstack, trial);
618 return trial;
621 /* Delete INSN, a JUMP_INSN. If it is a conditional jump, we must track down
622 the insn that sets CC0 for it and delete it too. */
624 static void
625 delete_scheduled_jump (rtx insn)
627 /* Delete the insn that sets cc0 for us. On machines without cc0, we could
628 delete the insn that sets the condition code, but it is hard to find it.
629 Since this case is rare anyway, don't bother trying; there would likely
630 be other insns that became dead anyway, which we wouldn't know to
631 delete. */
633 #ifdef HAVE_cc0
634 if (reg_mentioned_p (cc0_rtx, insn))
636 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
638 /* If a reg-note was found, it points to an insn to set CC0. This
639 insn is in the delay list of some other insn. So delete it from
640 the delay list it was in. */
641 if (note)
643 if (! FIND_REG_INC_NOTE (XEXP (note, 0), NULL_RTX)
644 && sets_cc0_p (PATTERN (XEXP (note, 0))) == 1)
645 delete_from_delay_slot (XEXP (note, 0));
647 else
649 /* The insn setting CC0 is our previous insn, but it may be in
650 a delay slot. It will be the last insn in the delay slot, if
651 it is. */
652 rtx trial = previous_insn (insn);
653 if (GET_CODE (trial) == NOTE)
654 trial = prev_nonnote_insn (trial);
655 if (sets_cc0_p (PATTERN (trial)) != 1
656 || FIND_REG_INC_NOTE (trial, NULL_RTX))
657 return;
658 if (PREV_INSN (NEXT_INSN (trial)) == trial)
659 delete_related_insns (trial);
660 else
661 delete_from_delay_slot (trial);
664 #endif
666 delete_related_insns (insn);
669 /* Counters for delay-slot filling. */
671 #define NUM_REORG_FUNCTIONS 2
672 #define MAX_DELAY_HISTOGRAM 3
673 #define MAX_REORG_PASSES 2
675 static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
677 static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
679 static int reorg_pass_number;
681 static void
682 note_delay_statistics (int slots_filled, int index)
684 num_insns_needing_delays[index][reorg_pass_number]++;
685 if (slots_filled > MAX_DELAY_HISTOGRAM)
686 slots_filled = MAX_DELAY_HISTOGRAM;
687 num_filled_delays[index][slots_filled][reorg_pass_number]++;
690 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
692 /* Optimize the following cases:
694 1. When a conditional branch skips over only one instruction,
695 use an annulling branch and put that insn in the delay slot.
696 Use either a branch that annuls when the condition if true or
697 invert the test with a branch that annuls when the condition is
698 false. This saves insns, since otherwise we must copy an insn
699 from the L1 target.
701 (orig) (skip) (otherwise)
702 Bcc.n L1 Bcc',a L1 Bcc,a L1'
703 insn insn insn2
704 L1: L1: L1:
705 insn2 insn2 insn2
706 insn3 insn3 L1':
707 insn3
709 2. When a conditional branch skips over only one instruction,
710 and after that, it unconditionally branches somewhere else,
711 perform the similar optimization. This saves executing the
712 second branch in the case where the inverted condition is true.
714 Bcc.n L1 Bcc',a L2
715 insn insn
716 L1: L1:
717 Bra L2 Bra L2
719 INSN is a JUMP_INSN.
721 This should be expanded to skip over N insns, where N is the number
722 of delay slots required. */
724 static rtx
725 optimize_skip (rtx insn)
727 rtx trial = next_nonnote_insn (insn);
728 rtx next_trial = next_active_insn (trial);
729 rtx delay_list = 0;
730 rtx target_label;
731 int flags;
733 flags = get_jump_flags (insn, JUMP_LABEL (insn));
735 if (trial == 0
736 || GET_CODE (trial) != INSN
737 || GET_CODE (PATTERN (trial)) == SEQUENCE
738 || recog_memoized (trial) < 0
739 || (! eligible_for_annul_false (insn, 0, trial, flags)
740 && ! eligible_for_annul_true (insn, 0, trial, flags))
741 || can_throw_internal (trial))
742 return 0;
744 /* There are two cases where we are just executing one insn (we assume
745 here that a branch requires only one insn; this should be generalized
746 at some point): Where the branch goes around a single insn or where
747 we have one insn followed by a branch to the same label we branch to.
748 In both of these cases, inverting the jump and annulling the delay
749 slot give the same effect in fewer insns. */
750 if ((next_trial == next_active_insn (JUMP_LABEL (insn))
751 && ! (next_trial == 0 && current_function_epilogue_delay_list != 0))
752 || (next_trial != 0
753 && GET_CODE (next_trial) == JUMP_INSN
754 && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)
755 && (simplejump_p (next_trial)
756 || GET_CODE (PATTERN (next_trial)) == RETURN)))
758 if (eligible_for_annul_false (insn, 0, trial, flags))
760 if (invert_jump (insn, JUMP_LABEL (insn), 1))
761 INSN_FROM_TARGET_P (trial) = 1;
762 else if (! eligible_for_annul_true (insn, 0, trial, flags))
763 return 0;
766 delay_list = add_to_delay_list (trial, NULL_RTX);
767 next_trial = next_active_insn (trial);
768 update_block (trial, trial);
769 delete_related_insns (trial);
771 /* Also, if we are targeting an unconditional
772 branch, thread our jump to the target of that branch. Don't
773 change this into a RETURN here, because it may not accept what
774 we have in the delay slot. We'll fix this up later. */
775 if (next_trial && GET_CODE (next_trial) == JUMP_INSN
776 && (simplejump_p (next_trial)
777 || GET_CODE (PATTERN (next_trial)) == RETURN))
779 target_label = JUMP_LABEL (next_trial);
780 if (target_label == 0)
781 target_label = find_end_label ();
783 /* Recompute the flags based on TARGET_LABEL since threading
784 the jump to TARGET_LABEL may change the direction of the
785 jump (which may change the circumstances in which the
786 delay slot is nullified). */
787 flags = get_jump_flags (insn, target_label);
788 if (eligible_for_annul_true (insn, 0, trial, flags))
789 reorg_redirect_jump (insn, target_label);
792 INSN_ANNULLED_BRANCH_P (insn) = 1;
795 return delay_list;
797 #endif
799 /* Encode and return branch direction and prediction information for
800 INSN assuming it will jump to LABEL.
802 Non conditional branches return no direction information and
803 are predicted as very likely taken. */
805 static int
806 get_jump_flags (rtx insn, rtx label)
808 int flags;
810 /* get_jump_flags can be passed any insn with delay slots, these may
811 be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
812 direction information, and only if they are conditional jumps.
814 If LABEL is zero, then there is no way to determine the branch
815 direction. */
816 if (GET_CODE (insn) == JUMP_INSN
817 && (condjump_p (insn) || condjump_in_parallel_p (insn))
818 && INSN_UID (insn) <= max_uid
819 && label != 0
820 && INSN_UID (label) <= max_uid)
821 flags
822 = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
823 ? ATTR_FLAG_forward : ATTR_FLAG_backward;
824 /* No valid direction information. */
825 else
826 flags = 0;
828 /* If insn is a conditional branch call mostly_true_jump to get
829 determine the branch prediction.
831 Non conditional branches are predicted as very likely taken. */
832 if (GET_CODE (insn) == JUMP_INSN
833 && (condjump_p (insn) || condjump_in_parallel_p (insn)))
835 int prediction;
837 prediction = mostly_true_jump (insn, get_branch_condition (insn, label));
838 switch (prediction)
840 case 2:
841 flags |= (ATTR_FLAG_very_likely | ATTR_FLAG_likely);
842 break;
843 case 1:
844 flags |= ATTR_FLAG_likely;
845 break;
846 case 0:
847 flags |= ATTR_FLAG_unlikely;
848 break;
849 case -1:
850 flags |= (ATTR_FLAG_very_unlikely | ATTR_FLAG_unlikely);
851 break;
853 default:
854 abort ();
857 else
858 flags |= (ATTR_FLAG_very_likely | ATTR_FLAG_likely);
860 return flags;
863 /* Return 1 if INSN is a destination that will be branched to rarely (the
864 return point of a function); return 2 if DEST will be branched to very
865 rarely (a call to a function that doesn't return). Otherwise,
866 return 0. */
868 static int
869 rare_destination (rtx insn)
871 int jump_count = 0;
872 rtx next;
874 for (; insn; insn = next)
876 if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
877 insn = XVECEXP (PATTERN (insn), 0, 0);
879 next = NEXT_INSN (insn);
881 switch (GET_CODE (insn))
883 case CODE_LABEL:
884 return 0;
885 case BARRIER:
886 /* A BARRIER can either be after a JUMP_INSN or a CALL_INSN. We
887 don't scan past JUMP_INSNs, so any barrier we find here must
888 have been after a CALL_INSN and hence mean the call doesn't
889 return. */
890 return 2;
891 case JUMP_INSN:
892 if (GET_CODE (PATTERN (insn)) == RETURN)
893 return 1;
894 else if (simplejump_p (insn)
895 && jump_count++ < 10)
896 next = JUMP_LABEL (insn);
897 else
898 return 0;
900 default:
901 break;
905 /* If we got here it means we hit the end of the function. So this
906 is an unlikely destination. */
908 return 1;
911 /* Return truth value of the statement that this branch
912 is mostly taken. If we think that the branch is extremely likely
913 to be taken, we return 2. If the branch is slightly more likely to be
914 taken, return 1. If the branch is slightly less likely to be taken,
915 return 0 and if the branch is highly unlikely to be taken, return -1.
917 CONDITION, if nonzero, is the condition that JUMP_INSN is testing. */
919 static int
920 mostly_true_jump (rtx jump_insn, rtx condition)
922 rtx target_label = JUMP_LABEL (jump_insn);
923 rtx insn, note;
924 int rare_dest = rare_destination (target_label);
925 int rare_fallthrough = rare_destination (NEXT_INSN (jump_insn));
927 /* If branch probabilities are available, then use that number since it
928 always gives a correct answer. */
929 note = find_reg_note (jump_insn, REG_BR_PROB, 0);
930 if (note)
932 int prob = INTVAL (XEXP (note, 0));
934 if (prob >= REG_BR_PROB_BASE * 9 / 10)
935 return 2;
936 else if (prob >= REG_BR_PROB_BASE / 2)
937 return 1;
938 else if (prob >= REG_BR_PROB_BASE / 10)
939 return 0;
940 else
941 return -1;
944 /* ??? Ought to use estimate_probability instead. */
946 /* If this is a branch outside a loop, it is highly unlikely. */
947 if (GET_CODE (PATTERN (jump_insn)) == SET
948 && GET_CODE (SET_SRC (PATTERN (jump_insn))) == IF_THEN_ELSE
949 && ((GET_CODE (XEXP (SET_SRC (PATTERN (jump_insn)), 1)) == LABEL_REF
950 && LABEL_OUTSIDE_LOOP_P (XEXP (SET_SRC (PATTERN (jump_insn)), 1)))
951 || (GET_CODE (XEXP (SET_SRC (PATTERN (jump_insn)), 2)) == LABEL_REF
952 && LABEL_OUTSIDE_LOOP_P (XEXP (SET_SRC (PATTERN (jump_insn)), 2)))))
953 return -1;
955 if (target_label)
957 /* If this is the test of a loop, it is very likely true. We scan
958 backwards from the target label. If we find a NOTE_INSN_LOOP_BEG
959 before the next real insn, we assume the branch is to the top of
960 the loop. */
961 for (insn = PREV_INSN (target_label);
962 insn && GET_CODE (insn) == NOTE;
963 insn = PREV_INSN (insn))
964 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
965 return 2;
967 /* If this is a jump to the test of a loop, it is likely true. We scan
968 forwards from the target label. If we find a NOTE_INSN_LOOP_VTOP
969 before the next real insn, we assume the branch is to the loop branch
970 test. */
971 for (insn = NEXT_INSN (target_label);
972 insn && GET_CODE (insn) == NOTE;
973 insn = PREV_INSN (insn))
974 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP)
975 return 1;
978 /* Look at the relative rarities of the fallthrough and destination. If
979 they differ, we can predict the branch that way. */
981 switch (rare_fallthrough - rare_dest)
983 case -2:
984 return -1;
985 case -1:
986 return 0;
987 case 0:
988 break;
989 case 1:
990 return 1;
991 case 2:
992 return 2;
995 /* If we couldn't figure out what this jump was, assume it won't be
996 taken. This should be rare. */
997 if (condition == 0)
998 return 0;
1000 /* EQ tests are usually false and NE tests are usually true. Also,
1001 most quantities are positive, so we can make the appropriate guesses
1002 about signed comparisons against zero. */
1003 switch (GET_CODE (condition))
1005 case CONST_INT:
1006 /* Unconditional branch. */
1007 return 1;
1008 case EQ:
1009 return 0;
1010 case NE:
1011 return 1;
1012 case LE:
1013 case LT:
1014 if (XEXP (condition, 1) == const0_rtx)
1015 return 0;
1016 break;
1017 case GE:
1018 case GT:
1019 if (XEXP (condition, 1) == const0_rtx)
1020 return 1;
1021 break;
1023 default:
1024 break;
1027 /* Predict backward branches usually take, forward branches usually not. If
1028 we don't know whether this is forward or backward, assume the branch
1029 will be taken, since most are. */
1030 return (target_label == 0 || INSN_UID (jump_insn) > max_uid
1031 || INSN_UID (target_label) > max_uid
1032 || (uid_to_ruid[INSN_UID (jump_insn)]
1033 > uid_to_ruid[INSN_UID (target_label)]));
1036 /* Return the condition under which INSN will branch to TARGET. If TARGET
1037 is zero, return the condition under which INSN will return. If INSN is
1038 an unconditional branch, return const_true_rtx. If INSN isn't a simple
1039 type of jump, or it doesn't go to TARGET, return 0. */
1041 static rtx
1042 get_branch_condition (rtx insn, rtx target)
1044 rtx pat = PATTERN (insn);
1045 rtx src;
1047 if (condjump_in_parallel_p (insn))
1048 pat = XVECEXP (pat, 0, 0);
1050 if (GET_CODE (pat) == RETURN)
1051 return target == 0 ? const_true_rtx : 0;
1053 else if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
1054 return 0;
1056 src = SET_SRC (pat);
1057 if (GET_CODE (src) == LABEL_REF && XEXP (src, 0) == target)
1058 return const_true_rtx;
1060 else if (GET_CODE (src) == IF_THEN_ELSE
1061 && ((target == 0 && GET_CODE (XEXP (src, 1)) == RETURN)
1062 || (GET_CODE (XEXP (src, 1)) == LABEL_REF
1063 && XEXP (XEXP (src, 1), 0) == target))
1064 && XEXP (src, 2) == pc_rtx)
1065 return XEXP (src, 0);
1067 else if (GET_CODE (src) == IF_THEN_ELSE
1068 && ((target == 0 && GET_CODE (XEXP (src, 2)) == RETURN)
1069 || (GET_CODE (XEXP (src, 2)) == LABEL_REF
1070 && XEXP (XEXP (src, 2), 0) == target))
1071 && XEXP (src, 1) == pc_rtx)
1073 enum rtx_code rev;
1074 rev = reversed_comparison_code (XEXP (src, 0), insn);
1075 if (rev != UNKNOWN)
1076 return gen_rtx_fmt_ee (rev, GET_MODE (XEXP (src, 0)),
1077 XEXP (XEXP (src, 0), 0),
1078 XEXP (XEXP (src, 0), 1));
1081 return 0;
1084 /* Return nonzero if CONDITION is more strict than the condition of
1085 INSN, i.e., if INSN will always branch if CONDITION is true. */
1087 static int
1088 condition_dominates_p (rtx condition, rtx insn)
1090 rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
1091 enum rtx_code code = GET_CODE (condition);
1092 enum rtx_code other_code;
1094 if (rtx_equal_p (condition, other_condition)
1095 || other_condition == const_true_rtx)
1096 return 1;
1098 else if (condition == const_true_rtx || other_condition == 0)
1099 return 0;
1101 other_code = GET_CODE (other_condition);
1102 if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
1103 || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
1104 || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
1105 return 0;
1107 return comparison_dominates_p (code, other_code);
1110 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1111 any insns already in the delay slot of JUMP. */
1113 static int
1114 redirect_with_delay_slots_safe_p (rtx jump, rtx newlabel, rtx seq)
1116 int flags, i;
1117 rtx pat = PATTERN (seq);
1119 /* Make sure all the delay slots of this jump would still
1120 be valid after threading the jump. If they are still
1121 valid, then return nonzero. */
1123 flags = get_jump_flags (jump, newlabel);
1124 for (i = 1; i < XVECLEN (pat, 0); i++)
1125 if (! (
1126 #ifdef ANNUL_IFFALSE_SLOTS
1127 (INSN_ANNULLED_BRANCH_P (jump)
1128 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1129 ? eligible_for_annul_false (jump, i - 1,
1130 XVECEXP (pat, 0, i), flags) :
1131 #endif
1132 #ifdef ANNUL_IFTRUE_SLOTS
1133 (INSN_ANNULLED_BRANCH_P (jump)
1134 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1135 ? eligible_for_annul_true (jump, i - 1,
1136 XVECEXP (pat, 0, i), flags) :
1137 #endif
1138 eligible_for_delay (jump, i - 1, XVECEXP (pat, 0, i), flags)))
1139 break;
1141 return (i == XVECLEN (pat, 0));
1144 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1145 any insns we wish to place in the delay slot of JUMP. */
1147 static int
1148 redirect_with_delay_list_safe_p (rtx jump, rtx newlabel, rtx delay_list)
1150 int flags, i;
1151 rtx li;
1153 /* Make sure all the insns in DELAY_LIST would still be
1154 valid after threading the jump. If they are still
1155 valid, then return nonzero. */
1157 flags = get_jump_flags (jump, newlabel);
1158 for (li = delay_list, i = 0; li; li = XEXP (li, 1), i++)
1159 if (! (
1160 #ifdef ANNUL_IFFALSE_SLOTS
1161 (INSN_ANNULLED_BRANCH_P (jump)
1162 && INSN_FROM_TARGET_P (XEXP (li, 0)))
1163 ? eligible_for_annul_false (jump, i, XEXP (li, 0), flags) :
1164 #endif
1165 #ifdef ANNUL_IFTRUE_SLOTS
1166 (INSN_ANNULLED_BRANCH_P (jump)
1167 && ! INSN_FROM_TARGET_P (XEXP (li, 0)))
1168 ? eligible_for_annul_true (jump, i, XEXP (li, 0), flags) :
1169 #endif
1170 eligible_for_delay (jump, i, XEXP (li, 0), flags)))
1171 break;
1173 return (li == NULL);
1176 /* DELAY_LIST is a list of insns that have already been placed into delay
1177 slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
1178 If not, return 0; otherwise return 1. */
1180 static int
1181 check_annul_list_true_false (int annul_true_p, rtx delay_list)
1183 rtx temp;
1185 if (delay_list)
1187 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1189 rtx trial = XEXP (temp, 0);
1191 if ((annul_true_p && INSN_FROM_TARGET_P (trial))
1192 || (!annul_true_p && !INSN_FROM_TARGET_P (trial)))
1193 return 0;
1197 return 1;
1200 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
1201 the condition tested by INSN is CONDITION and the resources shown in
1202 OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1203 from SEQ's delay list, in addition to whatever insns it may execute
1204 (in DELAY_LIST). SETS and NEEDED are denote resources already set and
1205 needed while searching for delay slot insns. Return the concatenated
1206 delay list if possible, otherwise, return 0.
1208 SLOTS_TO_FILL is the total number of slots required by INSN, and
1209 PSLOTS_FILLED points to the number filled so far (also the number of
1210 insns in DELAY_LIST). It is updated with the number that have been
1211 filled from the SEQUENCE, if any.
1213 PANNUL_P points to a nonzero value if we already know that we need
1214 to annul INSN. If this routine determines that annulling is needed,
1215 it may set that value nonzero.
1217 PNEW_THREAD points to a location that is to receive the place at which
1218 execution should continue. */
1220 static rtx
1221 steal_delay_list_from_target (rtx insn, rtx condition, rtx seq,
1222 rtx delay_list, struct resources *sets,
1223 struct resources *needed,
1224 struct resources *other_needed,
1225 int slots_to_fill, int *pslots_filled,
1226 int *pannul_p, rtx *pnew_thread)
1228 rtx temp;
1229 int slots_remaining = slots_to_fill - *pslots_filled;
1230 int total_slots_filled = *pslots_filled;
1231 rtx new_delay_list = 0;
1232 int must_annul = *pannul_p;
1233 int used_annul = 0;
1234 int i;
1235 struct resources cc_set;
1237 /* We can't do anything if there are more delay slots in SEQ than we
1238 can handle, or if we don't know that it will be a taken branch.
1239 We know that it will be a taken branch if it is either an unconditional
1240 branch or a conditional branch with a stricter branch condition.
1242 Also, exit if the branch has more than one set, since then it is computing
1243 other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1244 ??? It may be possible to move other sets into INSN in addition to
1245 moving the instructions in the delay slots.
1247 We can not steal the delay list if one of the instructions in the
1248 current delay_list modifies the condition codes and the jump in the
1249 sequence is a conditional jump. We can not do this because we can
1250 not change the direction of the jump because the condition codes
1251 will effect the direction of the jump in the sequence. */
1253 CLEAR_RESOURCE (&cc_set);
1254 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1256 rtx trial = XEXP (temp, 0);
1258 mark_set_resources (trial, &cc_set, 0, MARK_SRC_DEST_CALL);
1259 if (insn_references_resource_p (XVECEXP (seq , 0, 0), &cc_set, 0))
1260 return delay_list;
1263 if (XVECLEN (seq, 0) - 1 > slots_remaining
1264 || ! condition_dominates_p (condition, XVECEXP (seq, 0, 0))
1265 || ! single_set (XVECEXP (seq, 0, 0)))
1266 return delay_list;
1268 #ifdef MD_CAN_REDIRECT_BRANCH
1269 /* On some targets, branches with delay slots can have a limited
1270 displacement. Give the back end a chance to tell us we can't do
1271 this. */
1272 if (! MD_CAN_REDIRECT_BRANCH (insn, XVECEXP (seq, 0, 0)))
1273 return delay_list;
1274 #endif
1276 for (i = 1; i < XVECLEN (seq, 0); i++)
1278 rtx trial = XVECEXP (seq, 0, i);
1279 int flags;
1281 if (insn_references_resource_p (trial, sets, 0)
1282 || insn_sets_resource_p (trial, needed, 0)
1283 || insn_sets_resource_p (trial, sets, 0)
1284 #ifdef HAVE_cc0
1285 /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1286 delay list. */
1287 || find_reg_note (trial, REG_CC_USER, NULL_RTX)
1288 #endif
1289 /* If TRIAL is from the fallthrough code of an annulled branch insn
1290 in SEQ, we cannot use it. */
1291 || (INSN_ANNULLED_BRANCH_P (XVECEXP (seq, 0, 0))
1292 && ! INSN_FROM_TARGET_P (trial)))
1293 return delay_list;
1295 /* If this insn was already done (usually in a previous delay slot),
1296 pretend we put it in our delay slot. */
1297 if (redundant_insn (trial, insn, new_delay_list))
1298 continue;
1300 /* We will end up re-vectoring this branch, so compute flags
1301 based on jumping to the new label. */
1302 flags = get_jump_flags (insn, JUMP_LABEL (XVECEXP (seq, 0, 0)));
1304 if (! must_annul
1305 && ((condition == const_true_rtx
1306 || (! insn_sets_resource_p (trial, other_needed, 0)
1307 && ! may_trap_p (PATTERN (trial)))))
1308 ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1309 : (must_annul || (delay_list == NULL && new_delay_list == NULL))
1310 && (must_annul = 1,
1311 check_annul_list_true_false (0, delay_list)
1312 && check_annul_list_true_false (0, new_delay_list)
1313 && eligible_for_annul_false (insn, total_slots_filled,
1314 trial, flags)))
1316 if (must_annul)
1317 used_annul = 1;
1318 temp = copy_rtx (trial);
1319 INSN_FROM_TARGET_P (temp) = 1;
1320 new_delay_list = add_to_delay_list (temp, new_delay_list);
1321 total_slots_filled++;
1323 if (--slots_remaining == 0)
1324 break;
1326 else
1327 return delay_list;
1330 /* Show the place to which we will be branching. */
1331 *pnew_thread = next_active_insn (JUMP_LABEL (XVECEXP (seq, 0, 0)));
1333 /* Add any new insns to the delay list and update the count of the
1334 number of slots filled. */
1335 *pslots_filled = total_slots_filled;
1336 if (used_annul)
1337 *pannul_p = 1;
1339 if (delay_list == 0)
1340 return new_delay_list;
1342 for (temp = new_delay_list; temp; temp = XEXP (temp, 1))
1343 delay_list = add_to_delay_list (XEXP (temp, 0), delay_list);
1345 return delay_list;
1348 /* Similar to steal_delay_list_from_target except that SEQ is on the
1349 fallthrough path of INSN. Here we only do something if the delay insn
1350 of SEQ is an unconditional branch. In that case we steal its delay slot
1351 for INSN since unconditional branches are much easier to fill. */
1353 static rtx
1354 steal_delay_list_from_fallthrough (rtx insn, rtx condition, rtx seq,
1355 rtx delay_list, struct resources *sets,
1356 struct resources *needed,
1357 struct resources *other_needed,
1358 int slots_to_fill, int *pslots_filled,
1359 int *pannul_p)
1361 int i;
1362 int flags;
1363 int must_annul = *pannul_p;
1364 int used_annul = 0;
1366 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1368 /* We can't do anything if SEQ's delay insn isn't an
1369 unconditional branch. */
1371 if (! simplejump_p (XVECEXP (seq, 0, 0))
1372 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) != RETURN)
1373 return delay_list;
1375 for (i = 1; i < XVECLEN (seq, 0); i++)
1377 rtx trial = XVECEXP (seq, 0, i);
1379 /* If TRIAL sets CC0, stealing it will move it too far from the use
1380 of CC0. */
1381 if (insn_references_resource_p (trial, sets, 0)
1382 || insn_sets_resource_p (trial, needed, 0)
1383 || insn_sets_resource_p (trial, sets, 0)
1384 #ifdef HAVE_cc0
1385 || sets_cc0_p (PATTERN (trial))
1386 #endif
1389 break;
1391 /* If this insn was already done, we don't need it. */
1392 if (redundant_insn (trial, insn, delay_list))
1394 delete_from_delay_slot (trial);
1395 continue;
1398 if (! must_annul
1399 && ((condition == const_true_rtx
1400 || (! insn_sets_resource_p (trial, other_needed, 0)
1401 && ! may_trap_p (PATTERN (trial)))))
1402 ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1403 : (must_annul || delay_list == NULL) && (must_annul = 1,
1404 check_annul_list_true_false (1, delay_list)
1405 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1407 if (must_annul)
1408 used_annul = 1;
1409 delete_from_delay_slot (trial);
1410 delay_list = add_to_delay_list (trial, delay_list);
1412 if (++(*pslots_filled) == slots_to_fill)
1413 break;
1415 else
1416 break;
1419 if (used_annul)
1420 *pannul_p = 1;
1421 return delay_list;
1424 /* Try merging insns starting at THREAD which match exactly the insns in
1425 INSN's delay list.
1427 If all insns were matched and the insn was previously annulling, the
1428 annul bit will be cleared.
1430 For each insn that is merged, if the branch is or will be non-annulling,
1431 we delete the merged insn. */
1433 static void
1434 try_merge_delay_insns (rtx insn, rtx thread)
1436 rtx trial, next_trial;
1437 rtx delay_insn = XVECEXP (PATTERN (insn), 0, 0);
1438 int annul_p = INSN_ANNULLED_BRANCH_P (delay_insn);
1439 int slot_number = 1;
1440 int num_slots = XVECLEN (PATTERN (insn), 0);
1441 rtx next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1442 struct resources set, needed;
1443 rtx merged_insns = 0;
1444 int i;
1445 int flags;
1447 flags = get_jump_flags (delay_insn, JUMP_LABEL (delay_insn));
1449 CLEAR_RESOURCE (&needed);
1450 CLEAR_RESOURCE (&set);
1452 /* If this is not an annulling branch, take into account anything needed in
1453 INSN's delay slot. This prevents two increments from being incorrectly
1454 folded into one. If we are annulling, this would be the correct
1455 thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1456 will essentially disable this optimization. This method is somewhat of
1457 a kludge, but I don't see a better way.) */
1458 if (! annul_p)
1459 for (i = 1 ; i < num_slots; i++)
1460 if (XVECEXP (PATTERN (insn), 0, i))
1461 mark_referenced_resources (XVECEXP (PATTERN (insn), 0, i), &needed, 1);
1463 for (trial = thread; !stop_search_p (trial, 1); trial = next_trial)
1465 rtx pat = PATTERN (trial);
1466 rtx oldtrial = trial;
1468 next_trial = next_nonnote_insn (trial);
1470 /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1471 if (GET_CODE (trial) == INSN
1472 && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1473 continue;
1475 if (GET_CODE (next_to_match) == GET_CODE (trial)
1476 #ifdef HAVE_cc0
1477 /* We can't share an insn that sets cc0. */
1478 && ! sets_cc0_p (pat)
1479 #endif
1480 && ! insn_references_resource_p (trial, &set, 1)
1481 && ! insn_sets_resource_p (trial, &set, 1)
1482 && ! insn_sets_resource_p (trial, &needed, 1)
1483 && (trial = try_split (pat, trial, 0)) != 0
1484 /* Update next_trial, in case try_split succeeded. */
1485 && (next_trial = next_nonnote_insn (trial))
1486 /* Likewise THREAD. */
1487 && (thread = oldtrial == thread ? trial : thread)
1488 && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1489 /* Have to test this condition if annul condition is different
1490 from (and less restrictive than) non-annulling one. */
1491 && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1494 if (! annul_p)
1496 update_block (trial, thread);
1497 if (trial == thread)
1498 thread = next_active_insn (thread);
1500 delete_related_insns (trial);
1501 INSN_FROM_TARGET_P (next_to_match) = 0;
1503 else
1504 merged_insns = gen_rtx_INSN_LIST (VOIDmode, trial, merged_insns);
1506 if (++slot_number == num_slots)
1507 break;
1509 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1512 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1513 mark_referenced_resources (trial, &needed, 1);
1516 /* See if we stopped on a filled insn. If we did, try to see if its
1517 delay slots match. */
1518 if (slot_number != num_slots
1519 && trial && GET_CODE (trial) == INSN
1520 && GET_CODE (PATTERN (trial)) == SEQUENCE
1521 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial), 0, 0)))
1523 rtx pat = PATTERN (trial);
1524 rtx filled_insn = XVECEXP (pat, 0, 0);
1526 /* Account for resources set/needed by the filled insn. */
1527 mark_set_resources (filled_insn, &set, 0, MARK_SRC_DEST_CALL);
1528 mark_referenced_resources (filled_insn, &needed, 1);
1530 for (i = 1; i < XVECLEN (pat, 0); i++)
1532 rtx dtrial = XVECEXP (pat, 0, i);
1534 if (! insn_references_resource_p (dtrial, &set, 1)
1535 && ! insn_sets_resource_p (dtrial, &set, 1)
1536 && ! insn_sets_resource_p (dtrial, &needed, 1)
1537 #ifdef HAVE_cc0
1538 && ! sets_cc0_p (PATTERN (dtrial))
1539 #endif
1540 && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1541 && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1543 if (! annul_p)
1545 rtx new;
1547 update_block (dtrial, thread);
1548 new = delete_from_delay_slot (dtrial);
1549 if (INSN_DELETED_P (thread))
1550 thread = new;
1551 INSN_FROM_TARGET_P (next_to_match) = 0;
1553 else
1554 merged_insns = gen_rtx_INSN_LIST (SImode, dtrial,
1555 merged_insns);
1557 if (++slot_number == num_slots)
1558 break;
1560 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1562 else
1564 /* Keep track of the set/referenced resources for the delay
1565 slots of any trial insns we encounter. */
1566 mark_set_resources (dtrial, &set, 0, MARK_SRC_DEST_CALL);
1567 mark_referenced_resources (dtrial, &needed, 1);
1572 /* If all insns in the delay slot have been matched and we were previously
1573 annulling the branch, we need not any more. In that case delete all the
1574 merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1575 the delay list so that we know that it isn't only being used at the
1576 target. */
1577 if (slot_number == num_slots && annul_p)
1579 for (; merged_insns; merged_insns = XEXP (merged_insns, 1))
1581 if (GET_MODE (merged_insns) == SImode)
1583 rtx new;
1585 update_block (XEXP (merged_insns, 0), thread);
1586 new = delete_from_delay_slot (XEXP (merged_insns, 0));
1587 if (INSN_DELETED_P (thread))
1588 thread = new;
1590 else
1592 update_block (XEXP (merged_insns, 0), thread);
1593 delete_related_insns (XEXP (merged_insns, 0));
1597 INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1599 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1600 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i)) = 0;
1604 /* See if INSN is redundant with an insn in front of TARGET. Often this
1605 is called when INSN is a candidate for a delay slot of TARGET.
1606 DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1607 of INSN. Often INSN will be redundant with an insn in a delay slot of
1608 some previous insn. This happens when we have a series of branches to the
1609 same label; in that case the first insn at the target might want to go
1610 into each of the delay slots.
1612 If we are not careful, this routine can take up a significant fraction
1613 of the total compilation time (4%), but only wins rarely. Hence we
1614 speed this routine up by making two passes. The first pass goes back
1615 until it hits a label and sees if it finds an insn with an identical
1616 pattern. Only in this (relatively rare) event does it check for
1617 data conflicts.
1619 We do not split insns we encounter. This could cause us not to find a
1620 redundant insn, but the cost of splitting seems greater than the possible
1621 gain in rare cases. */
1623 static rtx
1624 redundant_insn (rtx insn, rtx target, rtx delay_list)
1626 rtx target_main = target;
1627 rtx ipat = PATTERN (insn);
1628 rtx trial, pat;
1629 struct resources needed, set;
1630 int i;
1631 unsigned insns_to_search;
1633 /* If INSN has any REG_UNUSED notes, it can't match anything since we
1634 are allowed to not actually assign to such a register. */
1635 if (find_reg_note (insn, REG_UNUSED, NULL_RTX) != 0)
1636 return 0;
1638 /* Scan backwards looking for a match. */
1639 for (trial = PREV_INSN (target),
1640 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1641 trial && insns_to_search > 0;
1642 trial = PREV_INSN (trial), --insns_to_search)
1644 if (GET_CODE (trial) == CODE_LABEL)
1645 return 0;
1647 if (! INSN_P (trial))
1648 continue;
1650 pat = PATTERN (trial);
1651 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1652 continue;
1654 if (GET_CODE (pat) == SEQUENCE)
1656 /* Stop for a CALL and its delay slots because it is difficult to
1657 track its resource needs correctly. */
1658 if (GET_CODE (XVECEXP (pat, 0, 0)) == CALL_INSN)
1659 return 0;
1661 /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1662 slots because it is difficult to track its resource needs
1663 correctly. */
1665 #ifdef INSN_SETS_ARE_DELAYED
1666 if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1667 return 0;
1668 #endif
1670 #ifdef INSN_REFERENCES_ARE_DELAYED
1671 if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1672 return 0;
1673 #endif
1675 /* See if any of the insns in the delay slot match, updating
1676 resource requirements as we go. */
1677 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1678 if (GET_CODE (XVECEXP (pat, 0, i)) == GET_CODE (insn)
1679 && rtx_equal_p (PATTERN (XVECEXP (pat, 0, i)), ipat)
1680 && ! find_reg_note (XVECEXP (pat, 0, i), REG_UNUSED, NULL_RTX))
1681 break;
1683 /* If found a match, exit this loop early. */
1684 if (i > 0)
1685 break;
1688 else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat)
1689 && ! find_reg_note (trial, REG_UNUSED, NULL_RTX))
1690 break;
1693 /* If we didn't find an insn that matches, return 0. */
1694 if (trial == 0)
1695 return 0;
1697 /* See what resources this insn sets and needs. If they overlap, or
1698 if this insn references CC0, it can't be redundant. */
1700 CLEAR_RESOURCE (&needed);
1701 CLEAR_RESOURCE (&set);
1702 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1703 mark_referenced_resources (insn, &needed, 1);
1705 /* If TARGET is a SEQUENCE, get the main insn. */
1706 if (GET_CODE (target) == INSN && GET_CODE (PATTERN (target)) == SEQUENCE)
1707 target_main = XVECEXP (PATTERN (target), 0, 0);
1709 if (resource_conflicts_p (&needed, &set)
1710 #ifdef HAVE_cc0
1711 || reg_mentioned_p (cc0_rtx, ipat)
1712 #endif
1713 /* The insn requiring the delay may not set anything needed or set by
1714 INSN. */
1715 || insn_sets_resource_p (target_main, &needed, 1)
1716 || insn_sets_resource_p (target_main, &set, 1))
1717 return 0;
1719 /* Insns we pass may not set either NEEDED or SET, so merge them for
1720 simpler tests. */
1721 needed.memory |= set.memory;
1722 needed.unch_memory |= set.unch_memory;
1723 IOR_HARD_REG_SET (needed.regs, set.regs);
1725 /* This insn isn't redundant if it conflicts with an insn that either is
1726 or will be in a delay slot of TARGET. */
1728 while (delay_list)
1730 if (insn_sets_resource_p (XEXP (delay_list, 0), &needed, 1))
1731 return 0;
1732 delay_list = XEXP (delay_list, 1);
1735 if (GET_CODE (target) == INSN && GET_CODE (PATTERN (target)) == SEQUENCE)
1736 for (i = 1; i < XVECLEN (PATTERN (target), 0); i++)
1737 if (insn_sets_resource_p (XVECEXP (PATTERN (target), 0, i), &needed, 1))
1738 return 0;
1740 /* Scan backwards until we reach a label or an insn that uses something
1741 INSN sets or sets something insn uses or sets. */
1743 for (trial = PREV_INSN (target),
1744 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1745 trial && GET_CODE (trial) != CODE_LABEL && insns_to_search > 0;
1746 trial = PREV_INSN (trial), --insns_to_search)
1748 if (GET_CODE (trial) != INSN && GET_CODE (trial) != CALL_INSN
1749 && GET_CODE (trial) != JUMP_INSN)
1750 continue;
1752 pat = PATTERN (trial);
1753 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1754 continue;
1756 if (GET_CODE (pat) == SEQUENCE)
1758 /* If this is a CALL_INSN and its delay slots, it is hard to track
1759 the resource needs properly, so give up. */
1760 if (GET_CODE (XVECEXP (pat, 0, 0)) == CALL_INSN)
1761 return 0;
1763 /* If this is an INSN or JUMP_INSN with delayed effects, it
1764 is hard to track the resource needs properly, so give up. */
1766 #ifdef INSN_SETS_ARE_DELAYED
1767 if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1768 return 0;
1769 #endif
1771 #ifdef INSN_REFERENCES_ARE_DELAYED
1772 if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1773 return 0;
1774 #endif
1776 /* See if any of the insns in the delay slot match, updating
1777 resource requirements as we go. */
1778 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1780 rtx candidate = XVECEXP (pat, 0, i);
1782 /* If an insn will be annulled if the branch is false, it isn't
1783 considered as a possible duplicate insn. */
1784 if (rtx_equal_p (PATTERN (candidate), ipat)
1785 && ! (INSN_ANNULLED_BRANCH_P (XVECEXP (pat, 0, 0))
1786 && INSN_FROM_TARGET_P (candidate)))
1788 /* Show that this insn will be used in the sequel. */
1789 INSN_FROM_TARGET_P (candidate) = 0;
1790 return candidate;
1793 /* Unless this is an annulled insn from the target of a branch,
1794 we must stop if it sets anything needed or set by INSN. */
1795 if ((! INSN_ANNULLED_BRANCH_P (XVECEXP (pat, 0, 0))
1796 || ! INSN_FROM_TARGET_P (candidate))
1797 && insn_sets_resource_p (candidate, &needed, 1))
1798 return 0;
1801 /* If the insn requiring the delay slot conflicts with INSN, we
1802 must stop. */
1803 if (insn_sets_resource_p (XVECEXP (pat, 0, 0), &needed, 1))
1804 return 0;
1806 else
1808 /* See if TRIAL is the same as INSN. */
1809 pat = PATTERN (trial);
1810 if (rtx_equal_p (pat, ipat))
1811 return trial;
1813 /* Can't go any further if TRIAL conflicts with INSN. */
1814 if (insn_sets_resource_p (trial, &needed, 1))
1815 return 0;
1819 return 0;
1822 /* Return 1 if THREAD can only be executed in one way. If LABEL is nonzero,
1823 it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1824 is nonzero, we are allowed to fall into this thread; otherwise, we are
1825 not.
1827 If LABEL is used more than one or we pass a label other than LABEL before
1828 finding an active insn, we do not own this thread. */
1830 static int
1831 own_thread_p (rtx thread, rtx label, int allow_fallthrough)
1833 rtx active_insn;
1834 rtx insn;
1836 /* We don't own the function end. */
1837 if (thread == 0)
1838 return 0;
1840 /* Get the first active insn, or THREAD, if it is an active insn. */
1841 active_insn = next_active_insn (PREV_INSN (thread));
1843 for (insn = thread; insn != active_insn; insn = NEXT_INSN (insn))
1844 if (GET_CODE (insn) == CODE_LABEL
1845 && (insn != label || LABEL_NUSES (insn) != 1))
1846 return 0;
1848 if (allow_fallthrough)
1849 return 1;
1851 /* Ensure that we reach a BARRIER before any insn or label. */
1852 for (insn = prev_nonnote_insn (thread);
1853 insn == 0 || GET_CODE (insn) != BARRIER;
1854 insn = prev_nonnote_insn (insn))
1855 if (insn == 0
1856 || GET_CODE (insn) == CODE_LABEL
1857 || (GET_CODE (insn) == INSN
1858 && GET_CODE (PATTERN (insn)) != USE
1859 && GET_CODE (PATTERN (insn)) != CLOBBER))
1860 return 0;
1862 return 1;
1865 /* Called when INSN is being moved from a location near the target of a jump.
1866 We leave a marker of the form (use (INSN)) immediately in front
1867 of WHERE for mark_target_live_regs. These markers will be deleted when
1868 reorg finishes.
1870 We used to try to update the live status of registers if WHERE is at
1871 the start of a basic block, but that can't work since we may remove a
1872 BARRIER in relax_delay_slots. */
1874 static void
1875 update_block (rtx insn, rtx where)
1877 /* Ignore if this was in a delay slot and it came from the target of
1878 a branch. */
1879 if (INSN_FROM_TARGET_P (insn))
1880 return;
1882 emit_insn_before (gen_rtx_USE (VOIDmode, insn), where);
1884 /* INSN might be making a value live in a block where it didn't use to
1885 be. So recompute liveness information for this block. */
1887 incr_ticks_for_insn (insn);
1890 /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1891 the basic block containing the jump. */
1893 static int
1894 reorg_redirect_jump (rtx jump, rtx nlabel)
1896 incr_ticks_for_insn (jump);
1897 return redirect_jump (jump, nlabel, 1);
1900 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1901 We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1902 that reference values used in INSN. If we find one, then we move the
1903 REG_DEAD note to INSN.
1905 This is needed to handle the case where an later insn (after INSN) has a
1906 REG_DEAD note for a register used by INSN, and this later insn subsequently
1907 gets moved before a CODE_LABEL because it is a redundant insn. In this
1908 case, mark_target_live_regs may be confused into thinking the register
1909 is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1911 static void
1912 update_reg_dead_notes (rtx insn, rtx delayed_insn)
1914 rtx p, link, next;
1916 for (p = next_nonnote_insn (insn); p != delayed_insn;
1917 p = next_nonnote_insn (p))
1918 for (link = REG_NOTES (p); link; link = next)
1920 next = XEXP (link, 1);
1922 if (REG_NOTE_KIND (link) != REG_DEAD
1923 || GET_CODE (XEXP (link, 0)) != REG)
1924 continue;
1926 if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
1928 /* Move the REG_DEAD note from P to INSN. */
1929 remove_note (p, link);
1930 XEXP (link, 1) = REG_NOTES (insn);
1931 REG_NOTES (insn) = link;
1936 /* Called when an insn redundant with start_insn is deleted. If there
1937 is a REG_DEAD note for the target of start_insn between start_insn
1938 and stop_insn, then the REG_DEAD note needs to be deleted since the
1939 value no longer dies there.
1941 If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1942 confused into thinking the register is dead. */
1944 static void
1945 fix_reg_dead_note (rtx start_insn, rtx stop_insn)
1947 rtx p, link, next;
1949 for (p = next_nonnote_insn (start_insn); p != stop_insn;
1950 p = next_nonnote_insn (p))
1951 for (link = REG_NOTES (p); link; link = next)
1953 next = XEXP (link, 1);
1955 if (REG_NOTE_KIND (link) != REG_DEAD
1956 || GET_CODE (XEXP (link, 0)) != REG)
1957 continue;
1959 if (reg_set_p (XEXP (link, 0), PATTERN (start_insn)))
1961 remove_note (p, link);
1962 return;
1967 /* Delete any REG_UNUSED notes that exist on INSN but not on REDUNDANT_INSN.
1969 This handles the case of udivmodXi4 instructions which optimize their
1970 output depending on whether any REG_UNUSED notes are present.
1971 we must make sure that INSN calculates as many results as REDUNDANT_INSN
1972 does. */
1974 static void
1975 update_reg_unused_notes (rtx insn, rtx redundant_insn)
1977 rtx link, next;
1979 for (link = REG_NOTES (insn); link; link = next)
1981 next = XEXP (link, 1);
1983 if (REG_NOTE_KIND (link) != REG_UNUSED
1984 || GET_CODE (XEXP (link, 0)) != REG)
1985 continue;
1987 if (! find_regno_note (redundant_insn, REG_UNUSED,
1988 REGNO (XEXP (link, 0))))
1989 remove_note (insn, link);
1993 /* Scan a function looking for insns that need a delay slot and find insns to
1994 put into the delay slot.
1996 NON_JUMPS_P is nonzero if we are to only try to fill non-jump insns (such
1997 as calls). We do these first since we don't want jump insns (that are
1998 easier to fill) to get the only insns that could be used for non-jump insns.
1999 When it is zero, only try to fill JUMP_INSNs.
2001 When slots are filled in this manner, the insns (including the
2002 delay_insn) are put together in a SEQUENCE rtx. In this fashion,
2003 it is possible to tell whether a delay slot has really been filled
2004 or not. `final' knows how to deal with this, by communicating
2005 through FINAL_SEQUENCE. */
2007 static void
2008 fill_simple_delay_slots (int non_jumps_p)
2010 rtx insn, pat, trial, next_trial;
2011 int i;
2012 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2013 struct resources needed, set;
2014 int slots_to_fill, slots_filled;
2015 rtx delay_list;
2017 for (i = 0; i < num_unfilled_slots; i++)
2019 int flags;
2020 /* Get the next insn to fill. If it has already had any slots assigned,
2021 we can't do anything with it. Maybe we'll improve this later. */
2023 insn = unfilled_slots_base[i];
2024 if (insn == 0
2025 || INSN_DELETED_P (insn)
2026 || (GET_CODE (insn) == INSN
2027 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2028 || (GET_CODE (insn) == JUMP_INSN && non_jumps_p)
2029 || (GET_CODE (insn) != JUMP_INSN && ! non_jumps_p))
2030 continue;
2032 /* It may have been that this insn used to need delay slots, but
2033 now doesn't; ignore in that case. This can happen, for example,
2034 on the HP PA RISC, where the number of delay slots depends on
2035 what insns are nearby. */
2036 slots_to_fill = num_delay_slots (insn);
2038 /* Some machine description have defined instructions to have
2039 delay slots only in certain circumstances which may depend on
2040 nearby insns (which change due to reorg's actions).
2042 For example, the PA port normally has delay slots for unconditional
2043 jumps.
2045 However, the PA port claims such jumps do not have a delay slot
2046 if they are immediate successors of certain CALL_INSNs. This
2047 allows the port to favor filling the delay slot of the call with
2048 the unconditional jump. */
2049 if (slots_to_fill == 0)
2050 continue;
2052 /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
2053 says how many. After initialization, first try optimizing
2055 call _foo call _foo
2056 nop add %o7,.-L1,%o7
2057 b,a L1
2060 If this case applies, the delay slot of the call is filled with
2061 the unconditional jump. This is done first to avoid having the
2062 delay slot of the call filled in the backward scan. Also, since
2063 the unconditional jump is likely to also have a delay slot, that
2064 insn must exist when it is subsequently scanned.
2066 This is tried on each insn with delay slots as some machines
2067 have insns which perform calls, but are not represented as
2068 CALL_INSNs. */
2070 slots_filled = 0;
2071 delay_list = 0;
2073 if (GET_CODE (insn) == JUMP_INSN)
2074 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2075 else
2076 flags = get_jump_flags (insn, NULL_RTX);
2078 if ((trial = next_active_insn (insn))
2079 && GET_CODE (trial) == JUMP_INSN
2080 && simplejump_p (trial)
2081 && eligible_for_delay (insn, slots_filled, trial, flags)
2082 && no_labels_between_p (insn, trial)
2083 && ! can_throw_internal (trial))
2085 rtx *tmp;
2086 slots_filled++;
2087 delay_list = add_to_delay_list (trial, delay_list);
2089 /* TRIAL may have had its delay slot filled, then unfilled. When
2090 the delay slot is unfilled, TRIAL is placed back on the unfilled
2091 slots obstack. Unfortunately, it is placed on the end of the
2092 obstack, not in its original location. Therefore, we must search
2093 from entry i + 1 to the end of the unfilled slots obstack to
2094 try and find TRIAL. */
2095 tmp = &unfilled_slots_base[i + 1];
2096 while (*tmp != trial && tmp != unfilled_slots_next)
2097 tmp++;
2099 /* Remove the unconditional jump from consideration for delay slot
2100 filling and unthread it. */
2101 if (*tmp == trial)
2102 *tmp = 0;
2104 rtx next = NEXT_INSN (trial);
2105 rtx prev = PREV_INSN (trial);
2106 if (prev)
2107 NEXT_INSN (prev) = next;
2108 if (next)
2109 PREV_INSN (next) = prev;
2113 /* Now, scan backwards from the insn to search for a potential
2114 delay-slot candidate. Stop searching when a label or jump is hit.
2116 For each candidate, if it is to go into the delay slot (moved
2117 forward in execution sequence), it must not need or set any resources
2118 that were set by later insns and must not set any resources that
2119 are needed for those insns.
2121 The delay slot insn itself sets resources unless it is a call
2122 (in which case the called routine, not the insn itself, is doing
2123 the setting). */
2125 if (slots_filled < slots_to_fill)
2127 CLEAR_RESOURCE (&needed);
2128 CLEAR_RESOURCE (&set);
2129 mark_set_resources (insn, &set, 0, MARK_SRC_DEST);
2130 mark_referenced_resources (insn, &needed, 0);
2132 for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, 1);
2133 trial = next_trial)
2135 next_trial = prev_nonnote_insn (trial);
2137 /* This must be an INSN or CALL_INSN. */
2138 pat = PATTERN (trial);
2140 /* USE and CLOBBER at this level was just for flow; ignore it. */
2141 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2142 continue;
2144 /* Check for resource conflict first, to avoid unnecessary
2145 splitting. */
2146 if (! insn_references_resource_p (trial, &set, 1)
2147 && ! insn_sets_resource_p (trial, &set, 1)
2148 && ! insn_sets_resource_p (trial, &needed, 1)
2149 #ifdef HAVE_cc0
2150 /* Can't separate set of cc0 from its use. */
2151 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2152 #endif
2153 && ! can_throw_internal (trial))
2155 trial = try_split (pat, trial, 1);
2156 next_trial = prev_nonnote_insn (trial);
2157 if (eligible_for_delay (insn, slots_filled, trial, flags))
2159 /* In this case, we are searching backward, so if we
2160 find insns to put on the delay list, we want
2161 to put them at the head, rather than the
2162 tail, of the list. */
2164 update_reg_dead_notes (trial, insn);
2165 delay_list = gen_rtx_INSN_LIST (VOIDmode,
2166 trial, delay_list);
2167 update_block (trial, trial);
2168 delete_related_insns (trial);
2169 if (slots_to_fill == ++slots_filled)
2170 break;
2171 continue;
2175 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2176 mark_referenced_resources (trial, &needed, 1);
2180 /* If all needed slots haven't been filled, we come here. */
2182 /* Try to optimize case of jumping around a single insn. */
2183 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
2184 if (slots_filled != slots_to_fill
2185 && delay_list == 0
2186 && GET_CODE (insn) == JUMP_INSN
2187 && (condjump_p (insn) || condjump_in_parallel_p (insn)))
2189 delay_list = optimize_skip (insn);
2190 if (delay_list)
2191 slots_filled += 1;
2193 #endif
2195 /* Try to get insns from beyond the insn needing the delay slot.
2196 These insns can neither set or reference resources set in insns being
2197 skipped, cannot set resources in the insn being skipped, and, if this
2198 is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2199 call might not return).
2201 There used to be code which continued past the target label if
2202 we saw all uses of the target label. This code did not work,
2203 because it failed to account for some instructions which were
2204 both annulled and marked as from the target. This can happen as a
2205 result of optimize_skip. Since this code was redundant with
2206 fill_eager_delay_slots anyways, it was just deleted. */
2208 if (slots_filled != slots_to_fill
2209 /* If this instruction could throw an exception which is
2210 caught in the same function, then it's not safe to fill
2211 the delay slot with an instruction from beyond this
2212 point. For example, consider:
2214 int i = 2;
2216 try {
2217 f();
2218 i = 3;
2219 } catch (...) {}
2221 return i;
2223 Even though `i' is a local variable, we must be sure not
2224 to put `i = 3' in the delay slot if `f' might throw an
2225 exception.
2227 Presumably, we should also check to see if we could get
2228 back to this function via `setjmp'. */
2229 && ! can_throw_internal (insn)
2230 && (GET_CODE (insn) != JUMP_INSN
2231 || ((condjump_p (insn) || condjump_in_parallel_p (insn))
2232 && ! simplejump_p (insn)
2233 && JUMP_LABEL (insn) != 0)))
2235 /* Invariant: If insn is a JUMP_INSN, the insn's jump
2236 label. Otherwise, zero. */
2237 rtx target = 0;
2238 int maybe_never = 0;
2239 rtx pat, trial_delay;
2241 CLEAR_RESOURCE (&needed);
2242 CLEAR_RESOURCE (&set);
2244 if (GET_CODE (insn) == CALL_INSN)
2246 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2247 mark_referenced_resources (insn, &needed, 1);
2248 maybe_never = 1;
2250 else
2252 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2253 mark_referenced_resources (insn, &needed, 1);
2254 if (GET_CODE (insn) == JUMP_INSN)
2255 target = JUMP_LABEL (insn);
2258 if (target == 0)
2259 for (trial = next_nonnote_insn (insn); trial; trial = next_trial)
2261 next_trial = next_nonnote_insn (trial);
2263 if (GET_CODE (trial) == CODE_LABEL
2264 || GET_CODE (trial) == BARRIER)
2265 break;
2267 /* We must have an INSN, JUMP_INSN, or CALL_INSN. */
2268 pat = PATTERN (trial);
2270 /* Stand-alone USE and CLOBBER are just for flow. */
2271 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2272 continue;
2274 /* If this already has filled delay slots, get the insn needing
2275 the delay slots. */
2276 if (GET_CODE (pat) == SEQUENCE)
2277 trial_delay = XVECEXP (pat, 0, 0);
2278 else
2279 trial_delay = trial;
2281 /* Stop our search when seeing an unconditional jump. */
2282 if (GET_CODE (trial_delay) == JUMP_INSN)
2283 break;
2285 /* See if we have a resource problem before we try to
2286 split. */
2287 if (GET_CODE (pat) != SEQUENCE
2288 && ! insn_references_resource_p (trial, &set, 1)
2289 && ! insn_sets_resource_p (trial, &set, 1)
2290 && ! insn_sets_resource_p (trial, &needed, 1)
2291 #ifdef HAVE_cc0
2292 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2293 #endif
2294 && ! (maybe_never && may_trap_p (pat))
2295 && (trial = try_split (pat, trial, 0))
2296 && eligible_for_delay (insn, slots_filled, trial, flags)
2297 && ! can_throw_internal(trial))
2299 next_trial = next_nonnote_insn (trial);
2300 delay_list = add_to_delay_list (trial, delay_list);
2302 #ifdef HAVE_cc0
2303 if (reg_mentioned_p (cc0_rtx, pat))
2304 link_cc0_insns (trial);
2305 #endif
2307 delete_related_insns (trial);
2308 if (slots_to_fill == ++slots_filled)
2309 break;
2310 continue;
2313 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2314 mark_referenced_resources (trial, &needed, 1);
2316 /* Ensure we don't put insns between the setting of cc and the
2317 comparison by moving a setting of cc into an earlier delay
2318 slot since these insns could clobber the condition code. */
2319 set.cc = 1;
2321 /* If this is a call or jump, we might not get here. */
2322 if (GET_CODE (trial_delay) == CALL_INSN
2323 || GET_CODE (trial_delay) == JUMP_INSN)
2324 maybe_never = 1;
2327 /* If there are slots left to fill and our search was stopped by an
2328 unconditional branch, try the insn at the branch target. We can
2329 redirect the branch if it works.
2331 Don't do this if the insn at the branch target is a branch. */
2332 if (slots_to_fill != slots_filled
2333 && trial
2334 && GET_CODE (trial) == JUMP_INSN
2335 && simplejump_p (trial)
2336 && (target == 0 || JUMP_LABEL (trial) == target)
2337 && (next_trial = next_active_insn (JUMP_LABEL (trial))) != 0
2338 && ! (GET_CODE (next_trial) == INSN
2339 && GET_CODE (PATTERN (next_trial)) == SEQUENCE)
2340 && GET_CODE (next_trial) != JUMP_INSN
2341 && ! insn_references_resource_p (next_trial, &set, 1)
2342 && ! insn_sets_resource_p (next_trial, &set, 1)
2343 && ! insn_sets_resource_p (next_trial, &needed, 1)
2344 #ifdef HAVE_cc0
2345 && ! reg_mentioned_p (cc0_rtx, PATTERN (next_trial))
2346 #endif
2347 && ! (maybe_never && may_trap_p (PATTERN (next_trial)))
2348 && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2349 && eligible_for_delay (insn, slots_filled, next_trial, flags)
2350 && ! can_throw_internal (trial))
2352 /* See comment in relax_delay_slots about necessity of using
2353 next_real_insn here. */
2354 rtx new_label = next_real_insn (next_trial);
2356 if (new_label != 0)
2357 new_label = get_label_before (new_label);
2358 else
2359 new_label = find_end_label ();
2361 delay_list
2362 = add_to_delay_list (copy_rtx (next_trial), delay_list);
2363 slots_filled++;
2364 reorg_redirect_jump (trial, new_label);
2366 /* If we merged because we both jumped to the same place,
2367 redirect the original insn also. */
2368 if (target)
2369 reorg_redirect_jump (insn, new_label);
2373 /* If this is an unconditional jump, then try to get insns from the
2374 target of the jump. */
2375 if (GET_CODE (insn) == JUMP_INSN
2376 && simplejump_p (insn)
2377 && slots_filled != slots_to_fill)
2378 delay_list
2379 = fill_slots_from_thread (insn, const_true_rtx,
2380 next_active_insn (JUMP_LABEL (insn)),
2381 NULL, 1, 1,
2382 own_thread_p (JUMP_LABEL (insn),
2383 JUMP_LABEL (insn), 0),
2384 slots_to_fill, &slots_filled,
2385 delay_list);
2387 if (delay_list)
2388 unfilled_slots_base[i]
2389 = emit_delay_sequence (insn, delay_list, slots_filled);
2391 if (slots_to_fill == slots_filled)
2392 unfilled_slots_base[i] = 0;
2394 note_delay_statistics (slots_filled, 0);
2397 #ifdef DELAY_SLOTS_FOR_EPILOGUE
2398 /* See if the epilogue needs any delay slots. Try to fill them if so.
2399 The only thing we can do is scan backwards from the end of the
2400 function. If we did this in a previous pass, it is incorrect to do it
2401 again. */
2402 if (current_function_epilogue_delay_list)
2403 return;
2405 slots_to_fill = DELAY_SLOTS_FOR_EPILOGUE;
2406 if (slots_to_fill == 0)
2407 return;
2409 slots_filled = 0;
2410 CLEAR_RESOURCE (&set);
2412 /* The frame pointer and stack pointer are needed at the beginning of
2413 the epilogue, so instructions setting them can not be put in the
2414 epilogue delay slot. However, everything else needed at function
2415 end is safe, so we don't want to use end_of_function_needs here. */
2416 CLEAR_RESOURCE (&needed);
2417 if (frame_pointer_needed)
2419 SET_HARD_REG_BIT (needed.regs, FRAME_POINTER_REGNUM);
2420 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2421 SET_HARD_REG_BIT (needed.regs, HARD_FRAME_POINTER_REGNUM);
2422 #endif
2423 if (! EXIT_IGNORE_STACK
2424 || current_function_sp_is_unchanging)
2425 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2427 else
2428 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2430 #ifdef EPILOGUE_USES
2431 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2433 if (EPILOGUE_USES (i))
2434 SET_HARD_REG_BIT (needed.regs, i);
2436 #endif
2438 for (trial = get_last_insn (); ! stop_search_p (trial, 1);
2439 trial = PREV_INSN (trial))
2441 if (GET_CODE (trial) == NOTE)
2442 continue;
2443 pat = PATTERN (trial);
2444 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2445 continue;
2447 if (! insn_references_resource_p (trial, &set, 1)
2448 && ! insn_sets_resource_p (trial, &needed, 1)
2449 && ! insn_sets_resource_p (trial, &set, 1)
2450 #ifdef HAVE_cc0
2451 /* Don't want to mess with cc0 here. */
2452 && ! reg_mentioned_p (cc0_rtx, pat)
2453 #endif
2454 && ! can_throw_internal (trial))
2456 trial = try_split (pat, trial, 1);
2457 if (ELIGIBLE_FOR_EPILOGUE_DELAY (trial, slots_filled))
2459 /* Here as well we are searching backward, so put the
2460 insns we find on the head of the list. */
2462 current_function_epilogue_delay_list
2463 = gen_rtx_INSN_LIST (VOIDmode, trial,
2464 current_function_epilogue_delay_list);
2465 mark_end_of_function_resources (trial, 1);
2466 update_block (trial, trial);
2467 delete_related_insns (trial);
2469 /* Clear deleted bit so final.c will output the insn. */
2470 INSN_DELETED_P (trial) = 0;
2472 if (slots_to_fill == ++slots_filled)
2473 break;
2474 continue;
2478 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2479 mark_referenced_resources (trial, &needed, 1);
2482 note_delay_statistics (slots_filled, 0);
2483 #endif
2486 /* Try to find insns to place in delay slots.
2488 INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2489 or is an unconditional branch if CONDITION is const_true_rtx.
2490 *PSLOTS_FILLED is updated with the number of slots that we have filled.
2492 THREAD is a flow-of-control, either the insns to be executed if the
2493 branch is true or if the branch is false, THREAD_IF_TRUE says which.
2495 OPPOSITE_THREAD is the thread in the opposite direction. It is used
2496 to see if any potential delay slot insns set things needed there.
2498 LIKELY is nonzero if it is extremely likely that the branch will be
2499 taken and THREAD_IF_TRUE is set. This is used for the branch at the
2500 end of a loop back up to the top.
2502 OWN_THREAD and OWN_OPPOSITE_THREAD are true if we are the only user of the
2503 thread. I.e., it is the fallthrough code of our jump or the target of the
2504 jump when we are the only jump going there.
2506 If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2507 case, we can only take insns from the head of the thread for our delay
2508 slot. We then adjust the jump to point after the insns we have taken. */
2510 static rtx
2511 fill_slots_from_thread (rtx insn, rtx condition, rtx thread,
2512 rtx opposite_thread, int likely, int thread_if_true,
2513 int own_thread, int slots_to_fill,
2514 int *pslots_filled, rtx delay_list)
2516 rtx new_thread;
2517 struct resources opposite_needed, set, needed;
2518 rtx trial;
2519 int lose = 0;
2520 int must_annul = 0;
2521 int flags;
2523 /* Validate our arguments. */
2524 if ((condition == const_true_rtx && ! thread_if_true)
2525 || (! own_thread && ! thread_if_true))
2526 abort ();
2528 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2530 /* If our thread is the end of subroutine, we can't get any delay
2531 insns from that. */
2532 if (thread == 0)
2533 return delay_list;
2535 /* If this is an unconditional branch, nothing is needed at the
2536 opposite thread. Otherwise, compute what is needed there. */
2537 if (condition == const_true_rtx)
2538 CLEAR_RESOURCE (&opposite_needed);
2539 else
2540 mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2542 /* If the insn at THREAD can be split, do it here to avoid having to
2543 update THREAD and NEW_THREAD if it is done in the loop below. Also
2544 initialize NEW_THREAD. */
2546 new_thread = thread = try_split (PATTERN (thread), thread, 0);
2548 /* Scan insns at THREAD. We are looking for an insn that can be removed
2549 from THREAD (it neither sets nor references resources that were set
2550 ahead of it and it doesn't set anything needs by the insns ahead of
2551 it) and that either can be placed in an annulling insn or aren't
2552 needed at OPPOSITE_THREAD. */
2554 CLEAR_RESOURCE (&needed);
2555 CLEAR_RESOURCE (&set);
2557 /* If we do not own this thread, we must stop as soon as we find
2558 something that we can't put in a delay slot, since all we can do
2559 is branch into THREAD at a later point. Therefore, labels stop
2560 the search if this is not the `true' thread. */
2562 for (trial = thread;
2563 ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2564 trial = next_nonnote_insn (trial))
2566 rtx pat, old_trial;
2568 /* If we have passed a label, we no longer own this thread. */
2569 if (GET_CODE (trial) == CODE_LABEL)
2571 own_thread = 0;
2572 continue;
2575 pat = PATTERN (trial);
2576 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2577 continue;
2579 /* If TRIAL conflicts with the insns ahead of it, we lose. Also,
2580 don't separate or copy insns that set and use CC0. */
2581 if (! insn_references_resource_p (trial, &set, 1)
2582 && ! insn_sets_resource_p (trial, &set, 1)
2583 && ! insn_sets_resource_p (trial, &needed, 1)
2584 #ifdef HAVE_cc0
2585 && ! (reg_mentioned_p (cc0_rtx, pat)
2586 && (! own_thread || ! sets_cc0_p (pat)))
2587 #endif
2588 && ! can_throw_internal (trial))
2590 rtx prior_insn;
2592 /* If TRIAL is redundant with some insn before INSN, we don't
2593 actually need to add it to the delay list; we can merely pretend
2594 we did. */
2595 if ((prior_insn = redundant_insn (trial, insn, delay_list)))
2597 fix_reg_dead_note (prior_insn, insn);
2598 if (own_thread)
2600 update_block (trial, thread);
2601 if (trial == thread)
2603 thread = next_active_insn (thread);
2604 if (new_thread == trial)
2605 new_thread = thread;
2608 delete_related_insns (trial);
2610 else
2612 update_reg_unused_notes (prior_insn, trial);
2613 new_thread = next_active_insn (trial);
2616 continue;
2619 /* There are two ways we can win: If TRIAL doesn't set anything
2620 needed at the opposite thread and can't trap, or if it can
2621 go into an annulled delay slot. */
2622 if (!must_annul
2623 && (condition == const_true_rtx
2624 || (! insn_sets_resource_p (trial, &opposite_needed, 1)
2625 && ! may_trap_p (pat))))
2627 old_trial = trial;
2628 trial = try_split (pat, trial, 0);
2629 if (new_thread == old_trial)
2630 new_thread = trial;
2631 if (thread == old_trial)
2632 thread = trial;
2633 pat = PATTERN (trial);
2634 if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2635 goto winner;
2637 else if (0
2638 #ifdef ANNUL_IFTRUE_SLOTS
2639 || ! thread_if_true
2640 #endif
2641 #ifdef ANNUL_IFFALSE_SLOTS
2642 || thread_if_true
2643 #endif
2646 old_trial = trial;
2647 trial = try_split (pat, trial, 0);
2648 if (new_thread == old_trial)
2649 new_thread = trial;
2650 if (thread == old_trial)
2651 thread = trial;
2652 pat = PATTERN (trial);
2653 if ((must_annul || delay_list == NULL) && (thread_if_true
2654 ? check_annul_list_true_false (0, delay_list)
2655 && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2656 : check_annul_list_true_false (1, delay_list)
2657 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2659 rtx temp;
2661 must_annul = 1;
2662 winner:
2664 #ifdef HAVE_cc0
2665 if (reg_mentioned_p (cc0_rtx, pat))
2666 link_cc0_insns (trial);
2667 #endif
2669 /* If we own this thread, delete the insn. If this is the
2670 destination of a branch, show that a basic block status
2671 may have been updated. In any case, mark the new
2672 starting point of this thread. */
2673 if (own_thread)
2675 rtx note;
2677 update_block (trial, thread);
2678 if (trial == thread)
2680 thread = next_active_insn (thread);
2681 if (new_thread == trial)
2682 new_thread = thread;
2685 /* We are moving this insn, not deleting it. We must
2686 temporarily increment the use count on any referenced
2687 label lest it be deleted by delete_related_insns. */
2688 note = find_reg_note (trial, REG_LABEL, 0);
2689 /* REG_LABEL could be NOTE_INSN_DELETED_LABEL too. */
2690 if (note && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2691 LABEL_NUSES (XEXP (note, 0))++;
2693 delete_related_insns (trial);
2695 if (note && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2696 LABEL_NUSES (XEXP (note, 0))--;
2698 else
2699 new_thread = next_active_insn (trial);
2701 temp = own_thread ? trial : copy_rtx (trial);
2702 if (thread_if_true)
2703 INSN_FROM_TARGET_P (temp) = 1;
2705 delay_list = add_to_delay_list (temp, delay_list);
2707 if (slots_to_fill == ++(*pslots_filled))
2709 /* Even though we have filled all the slots, we
2710 may be branching to a location that has a
2711 redundant insn. Skip any if so. */
2712 while (new_thread && ! own_thread
2713 && ! insn_sets_resource_p (new_thread, &set, 1)
2714 && ! insn_sets_resource_p (new_thread, &needed, 1)
2715 && ! insn_references_resource_p (new_thread,
2716 &set, 1)
2717 && (prior_insn
2718 = redundant_insn (new_thread, insn,
2719 delay_list)))
2721 /* We know we do not own the thread, so no need
2722 to call update_block and delete_insn. */
2723 fix_reg_dead_note (prior_insn, insn);
2724 update_reg_unused_notes (prior_insn, new_thread);
2725 new_thread = next_active_insn (new_thread);
2727 break;
2730 continue;
2735 /* This insn can't go into a delay slot. */
2736 lose = 1;
2737 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2738 mark_referenced_resources (trial, &needed, 1);
2740 /* Ensure we don't put insns between the setting of cc and the comparison
2741 by moving a setting of cc into an earlier delay slot since these insns
2742 could clobber the condition code. */
2743 set.cc = 1;
2745 /* If this insn is a register-register copy and the next insn has
2746 a use of our destination, change it to use our source. That way,
2747 it will become a candidate for our delay slot the next time
2748 through this loop. This case occurs commonly in loops that
2749 scan a list.
2751 We could check for more complex cases than those tested below,
2752 but it doesn't seem worth it. It might also be a good idea to try
2753 to swap the two insns. That might do better.
2755 We can't do this if the next insn modifies our destination, because
2756 that would make the replacement into the insn invalid. We also can't
2757 do this if it modifies our source, because it might be an earlyclobber
2758 operand. This latter test also prevents updating the contents of
2759 a PRE_INC. We also can't do this if there's overlap of source and
2760 destination. Overlap may happen for larger-than-register-size modes. */
2762 if (GET_CODE (trial) == INSN && GET_CODE (pat) == SET
2763 && GET_CODE (SET_SRC (pat)) == REG
2764 && GET_CODE (SET_DEST (pat)) == REG
2765 && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2767 rtx next = next_nonnote_insn (trial);
2769 if (next && GET_CODE (next) == INSN
2770 && GET_CODE (PATTERN (next)) != USE
2771 && ! reg_set_p (SET_DEST (pat), next)
2772 && ! reg_set_p (SET_SRC (pat), next)
2773 && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2774 && ! modified_in_p (SET_DEST (pat), next))
2775 validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2779 /* If we stopped on a branch insn that has delay slots, see if we can
2780 steal some of the insns in those slots. */
2781 if (trial && GET_CODE (trial) == INSN
2782 && GET_CODE (PATTERN (trial)) == SEQUENCE
2783 && GET_CODE (XVECEXP (PATTERN (trial), 0, 0)) == JUMP_INSN)
2785 /* If this is the `true' thread, we will want to follow the jump,
2786 so we can only do this if we have taken everything up to here. */
2787 if (thread_if_true && trial == new_thread)
2789 delay_list
2790 = steal_delay_list_from_target (insn, condition, PATTERN (trial),
2791 delay_list, &set, &needed,
2792 &opposite_needed, slots_to_fill,
2793 pslots_filled, &must_annul,
2794 &new_thread);
2795 /* If we owned the thread and are told that it branched
2796 elsewhere, make sure we own the thread at the new location. */
2797 if (own_thread && trial != new_thread)
2798 own_thread = own_thread_p (new_thread, new_thread, 0);
2800 else if (! thread_if_true)
2801 delay_list
2802 = steal_delay_list_from_fallthrough (insn, condition,
2803 PATTERN (trial),
2804 delay_list, &set, &needed,
2805 &opposite_needed, slots_to_fill,
2806 pslots_filled, &must_annul);
2809 /* If we haven't found anything for this delay slot and it is very
2810 likely that the branch will be taken, see if the insn at our target
2811 increments or decrements a register with an increment that does not
2812 depend on the destination register. If so, try to place the opposite
2813 arithmetic insn after the jump insn and put the arithmetic insn in the
2814 delay slot. If we can't do this, return. */
2815 if (delay_list == 0 && likely && new_thread
2816 && GET_CODE (new_thread) == INSN
2817 && GET_CODE (PATTERN (new_thread)) != ASM_INPUT
2818 && asm_noperands (PATTERN (new_thread)) < 0)
2820 rtx pat = PATTERN (new_thread);
2821 rtx dest;
2822 rtx src;
2824 trial = new_thread;
2825 pat = PATTERN (trial);
2827 if (GET_CODE (trial) != INSN
2828 || GET_CODE (pat) != SET
2829 || ! eligible_for_delay (insn, 0, trial, flags)
2830 || can_throw_internal (trial))
2831 return 0;
2833 dest = SET_DEST (pat), src = SET_SRC (pat);
2834 if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2835 && rtx_equal_p (XEXP (src, 0), dest)
2836 && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2837 && ! side_effects_p (pat))
2839 rtx other = XEXP (src, 1);
2840 rtx new_arith;
2841 rtx ninsn;
2843 /* If this is a constant adjustment, use the same code with
2844 the negated constant. Otherwise, reverse the sense of the
2845 arithmetic. */
2846 if (GET_CODE (other) == CONST_INT)
2847 new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2848 negate_rtx (GET_MODE (src), other));
2849 else
2850 new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2851 GET_MODE (src), dest, other);
2853 ninsn = emit_insn_after (gen_rtx_SET (VOIDmode, dest, new_arith),
2854 insn);
2856 if (recog_memoized (ninsn) < 0
2857 || (extract_insn (ninsn), ! constrain_operands (1)))
2859 delete_related_insns (ninsn);
2860 return 0;
2863 if (own_thread)
2865 update_block (trial, thread);
2866 if (trial == thread)
2868 thread = next_active_insn (thread);
2869 if (new_thread == trial)
2870 new_thread = thread;
2872 delete_related_insns (trial);
2874 else
2875 new_thread = next_active_insn (trial);
2877 ninsn = own_thread ? trial : copy_rtx (trial);
2878 if (thread_if_true)
2879 INSN_FROM_TARGET_P (ninsn) = 1;
2881 delay_list = add_to_delay_list (ninsn, NULL_RTX);
2882 (*pslots_filled)++;
2886 if (delay_list && must_annul)
2887 INSN_ANNULLED_BRANCH_P (insn) = 1;
2889 /* If we are to branch into the middle of this thread, find an appropriate
2890 label or make a new one if none, and redirect INSN to it. If we hit the
2891 end of the function, use the end-of-function label. */
2892 if (new_thread != thread)
2894 rtx label;
2896 if (! thread_if_true)
2897 abort ();
2899 if (new_thread && GET_CODE (new_thread) == JUMP_INSN
2900 && (simplejump_p (new_thread)
2901 || GET_CODE (PATTERN (new_thread)) == RETURN)
2902 && redirect_with_delay_list_safe_p (insn,
2903 JUMP_LABEL (new_thread),
2904 delay_list))
2905 new_thread = follow_jumps (JUMP_LABEL (new_thread));
2907 if (new_thread == 0)
2908 label = find_end_label ();
2909 else if (GET_CODE (new_thread) == CODE_LABEL)
2910 label = new_thread;
2911 else
2912 label = get_label_before (new_thread);
2914 reorg_redirect_jump (insn, label);
2917 return delay_list;
2920 /* Make another attempt to find insns to place in delay slots.
2922 We previously looked for insns located in front of the delay insn
2923 and, for non-jump delay insns, located behind the delay insn.
2925 Here only try to schedule jump insns and try to move insns from either
2926 the target or the following insns into the delay slot. If annulling is
2927 supported, we will be likely to do this. Otherwise, we can do this only
2928 if safe. */
2930 static void
2931 fill_eager_delay_slots (void)
2933 rtx insn;
2934 int i;
2935 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2937 for (i = 0; i < num_unfilled_slots; i++)
2939 rtx condition;
2940 rtx target_label, insn_at_target, fallthrough_insn;
2941 rtx delay_list = 0;
2942 int own_target;
2943 int own_fallthrough;
2944 int prediction, slots_to_fill, slots_filled;
2946 insn = unfilled_slots_base[i];
2947 if (insn == 0
2948 || INSN_DELETED_P (insn)
2949 || GET_CODE (insn) != JUMP_INSN
2950 || ! (condjump_p (insn) || condjump_in_parallel_p (insn)))
2951 continue;
2953 slots_to_fill = num_delay_slots (insn);
2954 /* Some machine description have defined instructions to have
2955 delay slots only in certain circumstances which may depend on
2956 nearby insns (which change due to reorg's actions).
2958 For example, the PA port normally has delay slots for unconditional
2959 jumps.
2961 However, the PA port claims such jumps do not have a delay slot
2962 if they are immediate successors of certain CALL_INSNs. This
2963 allows the port to favor filling the delay slot of the call with
2964 the unconditional jump. */
2965 if (slots_to_fill == 0)
2966 continue;
2968 slots_filled = 0;
2969 target_label = JUMP_LABEL (insn);
2970 condition = get_branch_condition (insn, target_label);
2972 if (condition == 0)
2973 continue;
2975 /* Get the next active fallthrough and target insns and see if we own
2976 them. Then see whether the branch is likely true. We don't need
2977 to do a lot of this for unconditional branches. */
2979 insn_at_target = next_active_insn (target_label);
2980 own_target = own_thread_p (target_label, target_label, 0);
2982 if (condition == const_true_rtx)
2984 own_fallthrough = 0;
2985 fallthrough_insn = 0;
2986 prediction = 2;
2988 else
2990 fallthrough_insn = next_active_insn (insn);
2991 own_fallthrough = own_thread_p (NEXT_INSN (insn), NULL_RTX, 1);
2992 prediction = mostly_true_jump (insn, condition);
2995 /* If this insn is expected to branch, first try to get insns from our
2996 target, then our fallthrough insns. If it is not expected to branch,
2997 try the other order. */
2999 if (prediction > 0)
3001 delay_list
3002 = fill_slots_from_thread (insn, condition, insn_at_target,
3003 fallthrough_insn, prediction == 2, 1,
3004 own_target,
3005 slots_to_fill, &slots_filled, delay_list);
3007 if (delay_list == 0 && own_fallthrough)
3009 /* Even though we didn't find anything for delay slots,
3010 we might have found a redundant insn which we deleted
3011 from the thread that was filled. So we have to recompute
3012 the next insn at the target. */
3013 target_label = JUMP_LABEL (insn);
3014 insn_at_target = next_active_insn (target_label);
3016 delay_list
3017 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3018 insn_at_target, 0, 0,
3019 own_fallthrough,
3020 slots_to_fill, &slots_filled,
3021 delay_list);
3024 else
3026 if (own_fallthrough)
3027 delay_list
3028 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3029 insn_at_target, 0, 0,
3030 own_fallthrough,
3031 slots_to_fill, &slots_filled,
3032 delay_list);
3034 if (delay_list == 0)
3035 delay_list
3036 = fill_slots_from_thread (insn, condition, insn_at_target,
3037 next_active_insn (insn), 0, 1,
3038 own_target,
3039 slots_to_fill, &slots_filled,
3040 delay_list);
3043 if (delay_list)
3044 unfilled_slots_base[i]
3045 = emit_delay_sequence (insn, delay_list, slots_filled);
3047 if (slots_to_fill == slots_filled)
3048 unfilled_slots_base[i] = 0;
3050 note_delay_statistics (slots_filled, 1);
3054 /* Once we have tried two ways to fill a delay slot, make a pass over the
3055 code to try to improve the results and to do such things as more jump
3056 threading. */
3058 static void
3059 relax_delay_slots (rtx first)
3061 rtx insn, next, pat;
3062 rtx trial, delay_insn, target_label;
3064 /* Look at every JUMP_INSN and see if we can improve it. */
3065 for (insn = first; insn; insn = next)
3067 rtx other;
3069 next = next_active_insn (insn);
3071 /* If this is a jump insn, see if it now jumps to a jump, jumps to
3072 the next insn, or jumps to a label that is not the last of a
3073 group of consecutive labels. */
3074 if (GET_CODE (insn) == JUMP_INSN
3075 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3076 && (target_label = JUMP_LABEL (insn)) != 0)
3078 target_label = follow_jumps (target_label);
3079 /* See comment further down why we must use next_real_insn here,
3080 instead of next_active_insn. */
3081 target_label = prev_label (next_real_insn (target_label));
3083 if (target_label == 0)
3084 target_label = find_end_label ();
3086 if (next_active_insn (target_label) == next
3087 && ! condjump_in_parallel_p (insn))
3089 delete_jump (insn);
3090 continue;
3093 if (target_label != JUMP_LABEL (insn))
3094 reorg_redirect_jump (insn, target_label);
3096 /* See if this jump branches around an unconditional jump.
3097 If so, invert this jump and point it to the target of the
3098 second jump. */
3099 if (next && GET_CODE (next) == JUMP_INSN
3100 && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3101 && next_active_insn (target_label) == next_active_insn (next)
3102 && no_labels_between_p (insn, next))
3104 rtx label = JUMP_LABEL (next);
3106 /* Be careful how we do this to avoid deleting code or
3107 labels that are momentarily dead. See similar optimization
3108 in jump.c.
3110 We also need to ensure we properly handle the case when
3111 invert_jump fails. */
3113 ++LABEL_NUSES (target_label);
3114 if (label)
3115 ++LABEL_NUSES (label);
3117 if (invert_jump (insn, label, 1))
3119 delete_related_insns (next);
3120 next = insn;
3123 if (label)
3124 --LABEL_NUSES (label);
3126 if (--LABEL_NUSES (target_label) == 0)
3127 delete_related_insns (target_label);
3129 continue;
3133 /* If this is an unconditional jump and the previous insn is a
3134 conditional jump, try reversing the condition of the previous
3135 insn and swapping our targets. The next pass might be able to
3136 fill the slots.
3138 Don't do this if we expect the conditional branch to be true, because
3139 we would then be making the more common case longer. */
3141 if (GET_CODE (insn) == JUMP_INSN
3142 && (simplejump_p (insn) || GET_CODE (PATTERN (insn)) == RETURN)
3143 && (other = prev_active_insn (insn)) != 0
3144 && (condjump_p (other) || condjump_in_parallel_p (other))
3145 && no_labels_between_p (other, insn)
3146 && 0 > mostly_true_jump (other,
3147 get_branch_condition (other,
3148 JUMP_LABEL (other))))
3150 rtx other_target = JUMP_LABEL (other);
3151 target_label = JUMP_LABEL (insn);
3153 if (invert_jump (other, target_label, 0))
3154 reorg_redirect_jump (insn, other_target);
3157 /* Now look only at cases where we have filled a delay slot. */
3158 if (GET_CODE (insn) != INSN
3159 || GET_CODE (PATTERN (insn)) != SEQUENCE)
3160 continue;
3162 pat = PATTERN (insn);
3163 delay_insn = XVECEXP (pat, 0, 0);
3165 /* See if the first insn in the delay slot is redundant with some
3166 previous insn. Remove it from the delay slot if so; then set up
3167 to reprocess this insn. */
3168 if (redundant_insn (XVECEXP (pat, 0, 1), delay_insn, 0))
3170 delete_from_delay_slot (XVECEXP (pat, 0, 1));
3171 next = prev_active_insn (next);
3172 continue;
3175 /* See if we have a RETURN insn with a filled delay slot followed
3176 by a RETURN insn with an unfilled a delay slot. If so, we can delete
3177 the first RETURN (but not its delay insn). This gives the same
3178 effect in fewer instructions.
3180 Only do so if optimizing for size since this results in slower, but
3181 smaller code. */
3182 if (optimize_size
3183 && GET_CODE (PATTERN (delay_insn)) == RETURN
3184 && next
3185 && GET_CODE (next) == JUMP_INSN
3186 && GET_CODE (PATTERN (next)) == RETURN)
3188 rtx after;
3189 int i;
3191 /* Delete the RETURN and just execute the delay list insns.
3193 We do this by deleting the INSN containing the SEQUENCE, then
3194 re-emitting the insns separately, and then deleting the RETURN.
3195 This allows the count of the jump target to be properly
3196 decremented. */
3198 /* Clear the from target bit, since these insns are no longer
3199 in delay slots. */
3200 for (i = 0; i < XVECLEN (pat, 0); i++)
3201 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3203 trial = PREV_INSN (insn);
3204 delete_related_insns (insn);
3205 if (GET_CODE (pat) != SEQUENCE)
3206 abort ();
3207 after = trial;
3208 for (i = 0; i < XVECLEN (pat, 0); i++)
3210 rtx this_insn = XVECEXP (pat, 0, i);
3211 add_insn_after (this_insn, after);
3212 after = this_insn;
3214 delete_scheduled_jump (delay_insn);
3215 continue;
3218 /* Now look only at the cases where we have a filled JUMP_INSN. */
3219 if (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) != JUMP_INSN
3220 || ! (condjump_p (XVECEXP (PATTERN (insn), 0, 0))
3221 || condjump_in_parallel_p (XVECEXP (PATTERN (insn), 0, 0))))
3222 continue;
3224 target_label = JUMP_LABEL (delay_insn);
3226 if (target_label)
3228 /* If this jump goes to another unconditional jump, thread it, but
3229 don't convert a jump into a RETURN here. */
3230 trial = follow_jumps (target_label);
3231 /* We use next_real_insn instead of next_active_insn, so that
3232 the special USE insns emitted by reorg won't be ignored.
3233 If they are ignored, then they will get deleted if target_label
3234 is now unreachable, and that would cause mark_target_live_regs
3235 to fail. */
3236 trial = prev_label (next_real_insn (trial));
3237 if (trial == 0 && target_label != 0)
3238 trial = find_end_label ();
3240 if (trial != target_label
3241 && redirect_with_delay_slots_safe_p (delay_insn, trial, insn))
3243 reorg_redirect_jump (delay_insn, trial);
3244 target_label = trial;
3247 /* If the first insn at TARGET_LABEL is redundant with a previous
3248 insn, redirect the jump to the following insn process again. */
3249 trial = next_active_insn (target_label);
3250 if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3251 && redundant_insn (trial, insn, 0)
3252 && ! can_throw_internal (trial))
3254 rtx tmp;
3256 /* Figure out where to emit the special USE insn so we don't
3257 later incorrectly compute register live/death info. */
3258 tmp = next_active_insn (trial);
3259 if (tmp == 0)
3260 tmp = find_end_label ();
3262 /* Insert the special USE insn and update dataflow info. */
3263 update_block (trial, tmp);
3265 /* Now emit a label before the special USE insn, and
3266 redirect our jump to the new label. */
3267 target_label = get_label_before (PREV_INSN (tmp));
3268 reorg_redirect_jump (delay_insn, target_label);
3269 next = insn;
3270 continue;
3273 /* Similarly, if it is an unconditional jump with one insn in its
3274 delay list and that insn is redundant, thread the jump. */
3275 if (trial && GET_CODE (PATTERN (trial)) == SEQUENCE
3276 && XVECLEN (PATTERN (trial), 0) == 2
3277 && GET_CODE (XVECEXP (PATTERN (trial), 0, 0)) == JUMP_INSN
3278 && (simplejump_p (XVECEXP (PATTERN (trial), 0, 0))
3279 || GET_CODE (PATTERN (XVECEXP (PATTERN (trial), 0, 0))) == RETURN)
3280 && redundant_insn (XVECEXP (PATTERN (trial), 0, 1), insn, 0))
3282 target_label = JUMP_LABEL (XVECEXP (PATTERN (trial), 0, 0));
3283 if (target_label == 0)
3284 target_label = find_end_label ();
3286 if (redirect_with_delay_slots_safe_p (delay_insn, target_label,
3287 insn))
3289 reorg_redirect_jump (delay_insn, target_label);
3290 next = insn;
3291 continue;
3296 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3297 && prev_active_insn (target_label) == insn
3298 && ! condjump_in_parallel_p (delay_insn)
3299 #ifdef HAVE_cc0
3300 /* If the last insn in the delay slot sets CC0 for some insn,
3301 various code assumes that it is in a delay slot. We could
3302 put it back where it belonged and delete the register notes,
3303 but it doesn't seem worthwhile in this uncommon case. */
3304 && ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3305 REG_CC_USER, NULL_RTX)
3306 #endif
3309 rtx after;
3310 int i;
3312 /* All this insn does is execute its delay list and jump to the
3313 following insn. So delete the jump and just execute the delay
3314 list insns.
3316 We do this by deleting the INSN containing the SEQUENCE, then
3317 re-emitting the insns separately, and then deleting the jump.
3318 This allows the count of the jump target to be properly
3319 decremented. */
3321 /* Clear the from target bit, since these insns are no longer
3322 in delay slots. */
3323 for (i = 0; i < XVECLEN (pat, 0); i++)
3324 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3326 trial = PREV_INSN (insn);
3327 delete_related_insns (insn);
3328 if (GET_CODE (pat) != SEQUENCE)
3329 abort ();
3330 after = trial;
3331 for (i = 0; i < XVECLEN (pat, 0); i++)
3333 rtx this_insn = XVECEXP (pat, 0, i);
3334 add_insn_after (this_insn, after);
3335 after = this_insn;
3337 delete_scheduled_jump (delay_insn);
3338 continue;
3341 /* See if this is an unconditional jump around a single insn which is
3342 identical to the one in its delay slot. In this case, we can just
3343 delete the branch and the insn in its delay slot. */
3344 if (next && GET_CODE (next) == INSN
3345 && prev_label (next_active_insn (next)) == target_label
3346 && simplejump_p (insn)
3347 && XVECLEN (pat, 0) == 2
3348 && rtx_equal_p (PATTERN (next), PATTERN (XVECEXP (pat, 0, 1))))
3350 delete_related_insns (insn);
3351 continue;
3354 /* See if this jump (with its delay slots) branches around another
3355 jump (without delay slots). If so, invert this jump and point
3356 it to the target of the second jump. We cannot do this for
3357 annulled jumps, though. Again, don't convert a jump to a RETURN
3358 here. */
3359 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3360 && next && GET_CODE (next) == JUMP_INSN
3361 && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3362 && next_active_insn (target_label) == next_active_insn (next)
3363 && no_labels_between_p (insn, next))
3365 rtx label = JUMP_LABEL (next);
3366 rtx old_label = JUMP_LABEL (delay_insn);
3368 if (label == 0)
3369 label = find_end_label ();
3371 /* find_end_label can generate a new label. Check this first. */
3372 if (no_labels_between_p (insn, next)
3373 && redirect_with_delay_slots_safe_p (delay_insn, label, insn))
3375 /* Be careful how we do this to avoid deleting code or labels
3376 that are momentarily dead. See similar optimization in
3377 jump.c */
3378 if (old_label)
3379 ++LABEL_NUSES (old_label);
3381 if (invert_jump (delay_insn, label, 1))
3383 int i;
3385 /* Must update the INSN_FROM_TARGET_P bits now that
3386 the branch is reversed, so that mark_target_live_regs
3387 will handle the delay slot insn correctly. */
3388 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
3390 rtx slot = XVECEXP (PATTERN (insn), 0, i);
3391 INSN_FROM_TARGET_P (slot) = ! INSN_FROM_TARGET_P (slot);
3394 delete_related_insns (next);
3395 next = insn;
3398 if (old_label && --LABEL_NUSES (old_label) == 0)
3399 delete_related_insns (old_label);
3400 continue;
3404 /* If we own the thread opposite the way this insn branches, see if we
3405 can merge its delay slots with following insns. */
3406 if (INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3407 && own_thread_p (NEXT_INSN (insn), 0, 1))
3408 try_merge_delay_insns (insn, next);
3409 else if (! INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3410 && own_thread_p (target_label, target_label, 0))
3411 try_merge_delay_insns (insn, next_active_insn (target_label));
3413 /* If we get here, we haven't deleted INSN. But we may have deleted
3414 NEXT, so recompute it. */
3415 next = next_active_insn (insn);
3419 #ifdef HAVE_return
3421 /* Look for filled jumps to the end of function label. We can try to convert
3422 them into RETURN insns if the insns in the delay slot are valid for the
3423 RETURN as well. */
3425 static void
3426 make_return_insns (rtx first)
3428 rtx insn, jump_insn, pat;
3429 rtx real_return_label = end_of_function_label;
3430 int slots, i;
3432 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3433 /* If a previous pass filled delay slots in the epilogue, things get a
3434 bit more complicated, as those filler insns would generally (without
3435 data flow analysis) have to be executed after any existing branch
3436 delay slot filler insns. It is also unknown whether such a
3437 transformation would actually be profitable. Note that the existing
3438 code only cares for branches with (some) filled delay slots. */
3439 if (current_function_epilogue_delay_list != NULL)
3440 return;
3441 #endif
3443 /* See if there is a RETURN insn in the function other than the one we
3444 made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3445 into a RETURN to jump to it. */
3446 for (insn = first; insn; insn = NEXT_INSN (insn))
3447 if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) == RETURN)
3449 real_return_label = get_label_before (insn);
3450 break;
3453 /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3454 was equal to END_OF_FUNCTION_LABEL. */
3455 LABEL_NUSES (real_return_label)++;
3457 /* Clear the list of insns to fill so we can use it. */
3458 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3460 for (insn = first; insn; insn = NEXT_INSN (insn))
3462 int flags;
3464 /* Only look at filled JUMP_INSNs that go to the end of function
3465 label. */
3466 if (GET_CODE (insn) != INSN
3467 || GET_CODE (PATTERN (insn)) != SEQUENCE
3468 || GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) != JUMP_INSN
3469 || JUMP_LABEL (XVECEXP (PATTERN (insn), 0, 0)) != end_of_function_label)
3470 continue;
3472 pat = PATTERN (insn);
3473 jump_insn = XVECEXP (pat, 0, 0);
3475 /* If we can't make the jump into a RETURN, try to redirect it to the best
3476 RETURN and go on to the next insn. */
3477 if (! reorg_redirect_jump (jump_insn, NULL_RTX))
3479 /* Make sure redirecting the jump will not invalidate the delay
3480 slot insns. */
3481 if (redirect_with_delay_slots_safe_p (jump_insn,
3482 real_return_label,
3483 insn))
3484 reorg_redirect_jump (jump_insn, real_return_label);
3485 continue;
3488 /* See if this RETURN can accept the insns current in its delay slot.
3489 It can if it has more or an equal number of slots and the contents
3490 of each is valid. */
3492 flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3493 slots = num_delay_slots (jump_insn);
3494 if (slots >= XVECLEN (pat, 0) - 1)
3496 for (i = 1; i < XVECLEN (pat, 0); i++)
3497 if (! (
3498 #ifdef ANNUL_IFFALSE_SLOTS
3499 (INSN_ANNULLED_BRANCH_P (jump_insn)
3500 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3501 ? eligible_for_annul_false (jump_insn, i - 1,
3502 XVECEXP (pat, 0, i), flags) :
3503 #endif
3504 #ifdef ANNUL_IFTRUE_SLOTS
3505 (INSN_ANNULLED_BRANCH_P (jump_insn)
3506 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3507 ? eligible_for_annul_true (jump_insn, i - 1,
3508 XVECEXP (pat, 0, i), flags) :
3509 #endif
3510 eligible_for_delay (jump_insn, i - 1,
3511 XVECEXP (pat, 0, i), flags)))
3512 break;
3514 else
3515 i = 0;
3517 if (i == XVECLEN (pat, 0))
3518 continue;
3520 /* We have to do something with this insn. If it is an unconditional
3521 RETURN, delete the SEQUENCE and output the individual insns,
3522 followed by the RETURN. Then set things up so we try to find
3523 insns for its delay slots, if it needs some. */
3524 if (GET_CODE (PATTERN (jump_insn)) == RETURN)
3526 rtx prev = PREV_INSN (insn);
3528 delete_related_insns (insn);
3529 for (i = 1; i < XVECLEN (pat, 0); i++)
3530 prev = emit_insn_after (PATTERN (XVECEXP (pat, 0, i)), prev);
3532 insn = emit_jump_insn_after (PATTERN (jump_insn), prev);
3533 emit_barrier_after (insn);
3535 if (slots)
3536 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3538 else
3539 /* It is probably more efficient to keep this with its current
3540 delay slot as a branch to a RETURN. */
3541 reorg_redirect_jump (jump_insn, real_return_label);
3544 /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3545 new delay slots we have created. */
3546 if (--LABEL_NUSES (real_return_label) == 0)
3547 delete_related_insns (real_return_label);
3549 fill_simple_delay_slots (1);
3550 fill_simple_delay_slots (0);
3552 #endif
3554 /* Try to find insns to place in delay slots. */
3556 void
3557 dbr_schedule (rtx first, FILE *file)
3559 rtx insn, next, epilogue_insn = 0;
3560 int i;
3561 #if 0
3562 int old_flag_no_peephole = flag_no_peephole;
3564 /* Execute `final' once in prescan mode to delete any insns that won't be
3565 used. Don't let final try to do any peephole optimization--it will
3566 ruin dataflow information for this pass. */
3568 flag_no_peephole = 1;
3569 final (first, 0, NO_DEBUG, 1, 1);
3570 flag_no_peephole = old_flag_no_peephole;
3571 #endif
3573 /* If the current function has no insns other than the prologue and
3574 epilogue, then do not try to fill any delay slots. */
3575 if (n_basic_blocks == 0)
3576 return;
3578 /* Find the highest INSN_UID and allocate and initialize our map from
3579 INSN_UID's to position in code. */
3580 for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3582 if (INSN_UID (insn) > max_uid)
3583 max_uid = INSN_UID (insn);
3584 if (GET_CODE (insn) == NOTE
3585 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
3586 epilogue_insn = insn;
3589 uid_to_ruid = xmalloc ((max_uid + 1) * sizeof (int));
3590 for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3591 uid_to_ruid[INSN_UID (insn)] = i;
3593 /* Initialize the list of insns that need filling. */
3594 if (unfilled_firstobj == 0)
3596 gcc_obstack_init (&unfilled_slots_obstack);
3597 unfilled_firstobj = obstack_alloc (&unfilled_slots_obstack, 0);
3600 for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3602 rtx target;
3604 INSN_ANNULLED_BRANCH_P (insn) = 0;
3605 INSN_FROM_TARGET_P (insn) = 0;
3607 /* Skip vector tables. We can't get attributes for them. */
3608 if (GET_CODE (insn) == JUMP_INSN
3609 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
3610 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
3611 continue;
3613 if (num_delay_slots (insn) > 0)
3614 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3616 /* Ensure all jumps go to the last of a set of consecutive labels. */
3617 if (GET_CODE (insn) == JUMP_INSN
3618 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3619 && JUMP_LABEL (insn) != 0
3620 && ((target = prev_label (next_active_insn (JUMP_LABEL (insn))))
3621 != JUMP_LABEL (insn)))
3622 redirect_jump (insn, target, 1);
3625 init_resource_info (epilogue_insn);
3627 /* Show we haven't computed an end-of-function label yet. */
3628 end_of_function_label = 0;
3630 /* Initialize the statistics for this function. */
3631 memset (num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3632 memset (num_filled_delays, 0, sizeof num_filled_delays);
3634 /* Now do the delay slot filling. Try everything twice in case earlier
3635 changes make more slots fillable. */
3637 for (reorg_pass_number = 0;
3638 reorg_pass_number < MAX_REORG_PASSES;
3639 reorg_pass_number++)
3641 fill_simple_delay_slots (1);
3642 fill_simple_delay_slots (0);
3643 fill_eager_delay_slots ();
3644 relax_delay_slots (first);
3647 /* Delete any USE insns made by update_block; subsequent passes don't need
3648 them or know how to deal with them. */
3649 for (insn = first; insn; insn = next)
3651 next = NEXT_INSN (insn);
3653 if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE
3654 && INSN_P (XEXP (PATTERN (insn), 0)))
3655 next = delete_related_insns (insn);
3658 /* If we made an end of function label, indicate that it is now
3659 safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3660 If it is now unused, delete it. */
3661 if (end_of_function_label && --LABEL_NUSES (end_of_function_label) == 0)
3662 delete_related_insns (end_of_function_label);
3664 #ifdef HAVE_return
3665 if (HAVE_return && end_of_function_label != 0)
3666 make_return_insns (first);
3667 #endif
3669 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3671 /* It is not clear why the line below is needed, but it does seem to be. */
3672 unfilled_firstobj = obstack_alloc (&unfilled_slots_obstack, 0);
3674 if (file)
3676 int i, j, need_comma;
3677 int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3678 int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
3680 for (reorg_pass_number = 0;
3681 reorg_pass_number < MAX_REORG_PASSES;
3682 reorg_pass_number++)
3684 fprintf (file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
3685 for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
3687 need_comma = 0;
3688 fprintf (file, ";; Reorg function #%d\n", i);
3690 fprintf (file, ";; %d insns needing delay slots\n;; ",
3691 num_insns_needing_delays[i][reorg_pass_number]);
3693 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3694 if (num_filled_delays[i][j][reorg_pass_number])
3696 if (need_comma)
3697 fprintf (file, ", ");
3698 need_comma = 1;
3699 fprintf (file, "%d got %d delays",
3700 num_filled_delays[i][j][reorg_pass_number], j);
3702 fprintf (file, "\n");
3705 memset (total_delay_slots, 0, sizeof total_delay_slots);
3706 memset (total_annul_slots, 0, sizeof total_annul_slots);
3707 for (insn = first; insn; insn = NEXT_INSN (insn))
3709 if (! INSN_DELETED_P (insn)
3710 && GET_CODE (insn) == INSN
3711 && GET_CODE (PATTERN (insn)) != USE
3712 && GET_CODE (PATTERN (insn)) != CLOBBER)
3714 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3716 j = XVECLEN (PATTERN (insn), 0) - 1;
3717 if (j > MAX_DELAY_HISTOGRAM)
3718 j = MAX_DELAY_HISTOGRAM;
3719 if (INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (insn), 0, 0)))
3720 total_annul_slots[j]++;
3721 else
3722 total_delay_slots[j]++;
3724 else if (num_delay_slots (insn) > 0)
3725 total_delay_slots[0]++;
3728 fprintf (file, ";; Reorg totals: ");
3729 need_comma = 0;
3730 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3732 if (total_delay_slots[j])
3734 if (need_comma)
3735 fprintf (file, ", ");
3736 need_comma = 1;
3737 fprintf (file, "%d got %d delays", total_delay_slots[j], j);
3740 fprintf (file, "\n");
3741 #if defined (ANNUL_IFTRUE_SLOTS) || defined (ANNUL_IFFALSE_SLOTS)
3742 fprintf (file, ";; Reorg annuls: ");
3743 need_comma = 0;
3744 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3746 if (total_annul_slots[j])
3748 if (need_comma)
3749 fprintf (file, ", ");
3750 need_comma = 1;
3751 fprintf (file, "%d got %d delays", total_annul_slots[j], j);
3754 fprintf (file, "\n");
3755 #endif
3756 fprintf (file, "\n");
3759 /* For all JUMP insns, fill in branch prediction notes, so that during
3760 assembler output a target can set branch prediction bits in the code.
3761 We have to do this now, as up until this point the destinations of
3762 JUMPS can be moved around and changed, but past right here that cannot
3763 happen. */
3764 for (insn = first; insn; insn = NEXT_INSN (insn))
3766 int pred_flags;
3768 if (GET_CODE (insn) == INSN)
3770 rtx pat = PATTERN (insn);
3772 if (GET_CODE (pat) == SEQUENCE)
3773 insn = XVECEXP (pat, 0, 0);
3775 if (GET_CODE (insn) != JUMP_INSN)
3776 continue;
3778 pred_flags = get_jump_flags (insn, JUMP_LABEL (insn));
3779 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_BR_PRED,
3780 GEN_INT (pred_flags),
3781 REG_NOTES (insn));
3783 free_resource_info ();
3784 free (uid_to_ruid);
3785 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3786 /* SPARC assembler, for instance, emit warning when debug info is output
3787 into the delay slot. */
3789 rtx link;
3791 for (link = current_function_epilogue_delay_list;
3792 link;
3793 link = XEXP (link, 1))
3794 INSN_LOCATOR (XEXP (link, 0)) = 0;
3796 #endif
3798 #endif /* DELAY_SLOTS */