2004-08-26 Matthias Klose <doko@debian.org>
[official-gcc.git] / gcc / reorg.c
blobd464a3239583468d513f0a56d9b280b447b70f3f
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
322 is none, try to make one. If that fails, returns 0.
324 The property of such a label is that it is placed just before the
325 epilogue or a bare RETURN insn, so that another bare RETURN can be
326 turned into a jump to the label unconditionally. In particular, the
327 label cannot be placed before a RETURN insn with a filled delay slot.
329 ??? There may be a problem with the current implementation. Suppose
330 we start with a bare RETURN insn and call find_end_label. It may set
331 end_of_function_label just before the RETURN. Suppose the machinery
332 is able to fill the delay slot of the RETURN insn afterwards. Then
333 end_of_function_label is no longer valid according to the property
334 described above and find_end_label will still return it unmodified.
335 Note that this is probably mitigated by the following observation:
336 once end_of_function_label is made, it is very likely the target of
337 a jump, so filling the delay slot of the RETURN will be much more
338 difficult. */
340 static rtx
341 find_end_label (void)
343 rtx insn;
345 /* If we found one previously, return it. */
346 if (end_of_function_label)
347 return end_of_function_label;
349 /* Otherwise, see if there is a label at the end of the function. If there
350 is, it must be that RETURN insns aren't needed, so that is our return
351 label and we don't have to do anything else. */
353 insn = get_last_insn ();
354 while (NOTE_P (insn)
355 || (NONJUMP_INSN_P (insn)
356 && (GET_CODE (PATTERN (insn)) == USE
357 || GET_CODE (PATTERN (insn)) == CLOBBER)))
358 insn = PREV_INSN (insn);
360 /* When a target threads its epilogue we might already have a
361 suitable return insn. If so put a label before it for the
362 end_of_function_label. */
363 if (BARRIER_P (insn)
364 && JUMP_P (PREV_INSN (insn))
365 && GET_CODE (PATTERN (PREV_INSN (insn))) == RETURN)
367 rtx temp = PREV_INSN (PREV_INSN (insn));
368 end_of_function_label = gen_label_rtx ();
369 LABEL_NUSES (end_of_function_label) = 0;
371 /* Put the label before an USE insns that may precede the RETURN insn. */
372 while (GET_CODE (temp) == USE)
373 temp = PREV_INSN (temp);
375 emit_label_after (end_of_function_label, temp);
378 else if (LABEL_P (insn))
379 end_of_function_label = insn;
380 else
382 end_of_function_label = gen_label_rtx ();
383 LABEL_NUSES (end_of_function_label) = 0;
384 /* If the basic block reorder pass moves the return insn to
385 some other place try to locate it again and put our
386 end_of_function_label there. */
387 while (insn && ! (GET_CODE (insn) == JUMP_INSN
388 && (GET_CODE (PATTERN (insn)) == RETURN)))
389 insn = PREV_INSN (insn);
390 if (insn)
392 insn = PREV_INSN (insn);
394 /* Put the label before an USE insns that may proceed the
395 RETURN insn. */
396 while (GET_CODE (insn) == USE)
397 insn = PREV_INSN (insn);
399 emit_label_after (end_of_function_label, insn);
401 else
403 #ifdef HAVE_epilogue
404 if (HAVE_epilogue
405 #ifdef HAVE_return
406 && ! HAVE_return
407 #endif
410 /* The RETURN insn has its delay slot filled so we cannot
411 emit the label just before it. Since we already have
412 an epilogue and cannot emit a new RETURN, we cannot
413 emit the label at all. */
414 end_of_function_label = NULL_RTX;
415 return end_of_function_label;
417 #endif /* HAVE_epilogue */
419 /* Otherwise, make a new label and emit a RETURN and BARRIER,
420 if needed. */
421 emit_label (end_of_function_label);
422 #ifdef HAVE_return
423 if (HAVE_return)
425 /* The return we make may have delay slots too. */
426 rtx insn = gen_return ();
427 insn = emit_jump_insn (insn);
428 emit_barrier ();
429 if (num_delay_slots (insn) > 0)
430 obstack_ptr_grow (&unfilled_slots_obstack, insn);
432 #endif
436 /* Show one additional use for this label so it won't go away until
437 we are done. */
438 ++LABEL_NUSES (end_of_function_label);
440 return end_of_function_label;
443 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
444 the pattern of INSN with the SEQUENCE.
446 Chain the insns so that NEXT_INSN of each insn in the sequence points to
447 the next and NEXT_INSN of the last insn in the sequence points to
448 the first insn after the sequence. Similarly for PREV_INSN. This makes
449 it easier to scan all insns.
451 Returns the SEQUENCE that replaces INSN. */
453 static rtx
454 emit_delay_sequence (rtx insn, rtx list, int length)
456 int i = 1;
457 rtx li;
458 int had_barrier = 0;
460 /* Allocate the rtvec to hold the insns and the SEQUENCE. */
461 rtvec seqv = rtvec_alloc (length + 1);
462 rtx seq = gen_rtx_SEQUENCE (VOIDmode, seqv);
463 rtx seq_insn = make_insn_raw (seq);
464 rtx first = get_insns ();
465 rtx last = get_last_insn ();
467 /* Make a copy of the insn having delay slots. */
468 rtx delay_insn = copy_rtx (insn);
470 /* If INSN is followed by a BARRIER, delete the BARRIER since it will only
471 confuse further processing. Update LAST in case it was the last insn.
472 We will put the BARRIER back in later. */
473 if (NEXT_INSN (insn) && BARRIER_P (NEXT_INSN (insn)))
475 delete_related_insns (NEXT_INSN (insn));
476 last = get_last_insn ();
477 had_barrier = 1;
480 /* Splice our SEQUENCE into the insn stream where INSN used to be. */
481 NEXT_INSN (seq_insn) = NEXT_INSN (insn);
482 PREV_INSN (seq_insn) = PREV_INSN (insn);
484 if (insn != last)
485 PREV_INSN (NEXT_INSN (seq_insn)) = seq_insn;
487 if (insn != first)
488 NEXT_INSN (PREV_INSN (seq_insn)) = seq_insn;
490 /* Note the calls to set_new_first_and_last_insn must occur after
491 SEQ_INSN has been completely spliced into the insn stream.
493 Otherwise CUR_INSN_UID will get set to an incorrect value because
494 set_new_first_and_last_insn will not find SEQ_INSN in the chain. */
495 if (insn == last)
496 set_new_first_and_last_insn (first, seq_insn);
498 if (insn == first)
499 set_new_first_and_last_insn (seq_insn, last);
501 /* Build our SEQUENCE and rebuild the insn chain. */
502 XVECEXP (seq, 0, 0) = delay_insn;
503 INSN_DELETED_P (delay_insn) = 0;
504 PREV_INSN (delay_insn) = PREV_INSN (seq_insn);
506 for (li = list; li; li = XEXP (li, 1), i++)
508 rtx tem = XEXP (li, 0);
509 rtx note, next;
511 /* Show that this copy of the insn isn't deleted. */
512 INSN_DELETED_P (tem) = 0;
514 XVECEXP (seq, 0, i) = tem;
515 PREV_INSN (tem) = XVECEXP (seq, 0, i - 1);
516 NEXT_INSN (XVECEXP (seq, 0, i - 1)) = tem;
518 /* SPARC assembler, for instance, emit warning when debug info is output
519 into the delay slot. */
520 if (INSN_LOCATOR (tem) && !INSN_LOCATOR (seq_insn))
521 INSN_LOCATOR (seq_insn) = INSN_LOCATOR (tem);
522 INSN_LOCATOR (tem) = 0;
524 for (note = REG_NOTES (tem); note; note = next)
526 next = XEXP (note, 1);
527 switch (REG_NOTE_KIND (note))
529 case REG_DEAD:
530 /* Remove any REG_DEAD notes because we can't rely on them now
531 that the insn has been moved. */
532 remove_note (tem, note);
533 break;
535 case REG_LABEL:
536 /* Keep the label reference count up to date. */
537 if (LABEL_P (XEXP (note, 0)))
538 LABEL_NUSES (XEXP (note, 0)) ++;
539 break;
541 default:
542 break;
547 NEXT_INSN (XVECEXP (seq, 0, length)) = NEXT_INSN (seq_insn);
549 /* If the previous insn is a SEQUENCE, update the NEXT_INSN pointer on the
550 last insn in that SEQUENCE to point to us. Similarly for the first
551 insn in the following insn if it is a SEQUENCE. */
553 if (PREV_INSN (seq_insn) && NONJUMP_INSN_P (PREV_INSN (seq_insn))
554 && GET_CODE (PATTERN (PREV_INSN (seq_insn))) == SEQUENCE)
555 NEXT_INSN (XVECEXP (PATTERN (PREV_INSN (seq_insn)), 0,
556 XVECLEN (PATTERN (PREV_INSN (seq_insn)), 0) - 1))
557 = seq_insn;
559 if (NEXT_INSN (seq_insn) && NONJUMP_INSN_P (NEXT_INSN (seq_insn))
560 && GET_CODE (PATTERN (NEXT_INSN (seq_insn))) == SEQUENCE)
561 PREV_INSN (XVECEXP (PATTERN (NEXT_INSN (seq_insn)), 0, 0)) = seq_insn;
563 /* If there used to be a BARRIER, put it back. */
564 if (had_barrier)
565 emit_barrier_after (seq_insn);
567 if (i != length + 1)
568 abort ();
570 return seq_insn;
573 /* Add INSN to DELAY_LIST and return the head of the new list. The list must
574 be in the order in which the insns are to be executed. */
576 static rtx
577 add_to_delay_list (rtx insn, rtx delay_list)
579 /* If we have an empty list, just make a new list element. If
580 INSN has its block number recorded, clear it since we may
581 be moving the insn to a new block. */
583 if (delay_list == 0)
585 clear_hashed_info_for_insn (insn);
586 return gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
589 /* Otherwise this must be an INSN_LIST. Add INSN to the end of the
590 list. */
591 XEXP (delay_list, 1) = add_to_delay_list (insn, XEXP (delay_list, 1));
593 return delay_list;
596 /* Delete INSN from the delay slot of the insn that it is in, which may
597 produce an insn with no delay slots. Return the new insn. */
599 static rtx
600 delete_from_delay_slot (rtx insn)
602 rtx trial, seq_insn, seq, prev;
603 rtx delay_list = 0;
604 int i;
605 int had_barrier = 0;
607 /* We first must find the insn containing the SEQUENCE with INSN in its
608 delay slot. Do this by finding an insn, TRIAL, where
609 PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
611 for (trial = insn;
612 PREV_INSN (NEXT_INSN (trial)) == trial;
613 trial = NEXT_INSN (trial))
616 seq_insn = PREV_INSN (NEXT_INSN (trial));
617 seq = PATTERN (seq_insn);
619 if (NEXT_INSN (seq_insn) && BARRIER_P (NEXT_INSN (seq_insn)))
620 had_barrier = 1;
622 /* Create a delay list consisting of all the insns other than the one
623 we are deleting (unless we were the only one). */
624 if (XVECLEN (seq, 0) > 2)
625 for (i = 1; i < XVECLEN (seq, 0); i++)
626 if (XVECEXP (seq, 0, i) != insn)
627 delay_list = add_to_delay_list (XVECEXP (seq, 0, i), delay_list);
629 /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
630 list, and rebuild the delay list if non-empty. */
631 prev = PREV_INSN (seq_insn);
632 trial = XVECEXP (seq, 0, 0);
633 delete_related_insns (seq_insn);
634 add_insn_after (trial, prev);
636 /* If there was a barrier after the old SEQUENCE, remit it. */
637 if (had_barrier)
638 emit_barrier_after (trial);
640 /* If there are any delay insns, remit them. Otherwise clear the
641 annul flag. */
642 if (delay_list)
643 trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2);
644 else if (INSN_P (trial))
645 INSN_ANNULLED_BRANCH_P (trial) = 0;
647 INSN_FROM_TARGET_P (insn) = 0;
649 /* Show we need to fill this insn again. */
650 obstack_ptr_grow (&unfilled_slots_obstack, trial);
652 return trial;
655 /* Delete INSN, a JUMP_INSN. If it is a conditional jump, we must track down
656 the insn that sets CC0 for it and delete it too. */
658 static void
659 delete_scheduled_jump (rtx insn)
661 /* Delete the insn that sets cc0 for us. On machines without cc0, we could
662 delete the insn that sets the condition code, but it is hard to find it.
663 Since this case is rare anyway, don't bother trying; there would likely
664 be other insns that became dead anyway, which we wouldn't know to
665 delete. */
667 #ifdef HAVE_cc0
668 if (reg_mentioned_p (cc0_rtx, insn))
670 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
672 /* If a reg-note was found, it points to an insn to set CC0. This
673 insn is in the delay list of some other insn. So delete it from
674 the delay list it was in. */
675 if (note)
677 if (! FIND_REG_INC_NOTE (XEXP (note, 0), NULL_RTX)
678 && sets_cc0_p (PATTERN (XEXP (note, 0))) == 1)
679 delete_from_delay_slot (XEXP (note, 0));
681 else
683 /* The insn setting CC0 is our previous insn, but it may be in
684 a delay slot. It will be the last insn in the delay slot, if
685 it is. */
686 rtx trial = previous_insn (insn);
687 if (NOTE_P (trial))
688 trial = prev_nonnote_insn (trial);
689 if (sets_cc0_p (PATTERN (trial)) != 1
690 || FIND_REG_INC_NOTE (trial, NULL_RTX))
691 return;
692 if (PREV_INSN (NEXT_INSN (trial)) == trial)
693 delete_related_insns (trial);
694 else
695 delete_from_delay_slot (trial);
698 #endif
700 delete_related_insns (insn);
703 /* Counters for delay-slot filling. */
705 #define NUM_REORG_FUNCTIONS 2
706 #define MAX_DELAY_HISTOGRAM 3
707 #define MAX_REORG_PASSES 2
709 static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
711 static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
713 static int reorg_pass_number;
715 static void
716 note_delay_statistics (int slots_filled, int index)
718 num_insns_needing_delays[index][reorg_pass_number]++;
719 if (slots_filled > MAX_DELAY_HISTOGRAM)
720 slots_filled = MAX_DELAY_HISTOGRAM;
721 num_filled_delays[index][slots_filled][reorg_pass_number]++;
724 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
726 /* Optimize the following cases:
728 1. When a conditional branch skips over only one instruction,
729 use an annulling branch and put that insn in the delay slot.
730 Use either a branch that annuls when the condition if true or
731 invert the test with a branch that annuls when the condition is
732 false. This saves insns, since otherwise we must copy an insn
733 from the L1 target.
735 (orig) (skip) (otherwise)
736 Bcc.n L1 Bcc',a L1 Bcc,a L1'
737 insn insn insn2
738 L1: L1: L1:
739 insn2 insn2 insn2
740 insn3 insn3 L1':
741 insn3
743 2. When a conditional branch skips over only one instruction,
744 and after that, it unconditionally branches somewhere else,
745 perform the similar optimization. This saves executing the
746 second branch in the case where the inverted condition is true.
748 Bcc.n L1 Bcc',a L2
749 insn insn
750 L1: L1:
751 Bra L2 Bra L2
753 INSN is a JUMP_INSN.
755 This should be expanded to skip over N insns, where N is the number
756 of delay slots required. */
758 static rtx
759 optimize_skip (rtx insn)
761 rtx trial = next_nonnote_insn (insn);
762 rtx next_trial = next_active_insn (trial);
763 rtx delay_list = 0;
764 int flags;
766 flags = get_jump_flags (insn, JUMP_LABEL (insn));
768 if (trial == 0
769 || !NONJUMP_INSN_P (trial)
770 || GET_CODE (PATTERN (trial)) == SEQUENCE
771 || recog_memoized (trial) < 0
772 || (! eligible_for_annul_false (insn, 0, trial, flags)
773 && ! eligible_for_annul_true (insn, 0, trial, flags))
774 || can_throw_internal (trial))
775 return 0;
777 /* There are two cases where we are just executing one insn (we assume
778 here that a branch requires only one insn; this should be generalized
779 at some point): Where the branch goes around a single insn or where
780 we have one insn followed by a branch to the same label we branch to.
781 In both of these cases, inverting the jump and annulling the delay
782 slot give the same effect in fewer insns. */
783 if ((next_trial == next_active_insn (JUMP_LABEL (insn))
784 && ! (next_trial == 0 && current_function_epilogue_delay_list != 0))
785 || (next_trial != 0
786 && JUMP_P (next_trial)
787 && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)
788 && (simplejump_p (next_trial)
789 || GET_CODE (PATTERN (next_trial)) == RETURN)))
791 if (eligible_for_annul_false (insn, 0, trial, flags))
793 if (invert_jump (insn, JUMP_LABEL (insn), 1))
794 INSN_FROM_TARGET_P (trial) = 1;
795 else if (! eligible_for_annul_true (insn, 0, trial, flags))
796 return 0;
799 delay_list = add_to_delay_list (trial, NULL_RTX);
800 next_trial = next_active_insn (trial);
801 update_block (trial, trial);
802 delete_related_insns (trial);
804 /* Also, if we are targeting an unconditional
805 branch, thread our jump to the target of that branch. Don't
806 change this into a RETURN here, because it may not accept what
807 we have in the delay slot. We'll fix this up later. */
808 if (next_trial && JUMP_P (next_trial)
809 && (simplejump_p (next_trial)
810 || GET_CODE (PATTERN (next_trial)) == RETURN))
812 rtx target_label = JUMP_LABEL (next_trial);
813 if (target_label == 0)
814 target_label = find_end_label ();
816 if (target_label)
818 /* Recompute the flags based on TARGET_LABEL since threading
819 the jump to TARGET_LABEL may change the direction of the
820 jump (which may change the circumstances in which the
821 delay slot is nullified). */
822 flags = get_jump_flags (insn, target_label);
823 if (eligible_for_annul_true (insn, 0, trial, flags))
824 reorg_redirect_jump (insn, target_label);
828 INSN_ANNULLED_BRANCH_P (insn) = 1;
831 return delay_list;
833 #endif
835 /* Encode and return branch direction and prediction information for
836 INSN assuming it will jump to LABEL.
838 Non conditional branches return no direction information and
839 are predicted as very likely taken. */
841 static int
842 get_jump_flags (rtx insn, rtx label)
844 int flags;
846 /* get_jump_flags can be passed any insn with delay slots, these may
847 be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
848 direction information, and only if they are conditional jumps.
850 If LABEL is zero, then there is no way to determine the branch
851 direction. */
852 if (JUMP_P (insn)
853 && (condjump_p (insn) || condjump_in_parallel_p (insn))
854 && INSN_UID (insn) <= max_uid
855 && label != 0
856 && INSN_UID (label) <= max_uid)
857 flags
858 = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
859 ? ATTR_FLAG_forward : ATTR_FLAG_backward;
860 /* No valid direction information. */
861 else
862 flags = 0;
864 /* If insn is a conditional branch call mostly_true_jump to get
865 determine the branch prediction.
867 Non conditional branches are predicted as very likely taken. */
868 if (JUMP_P (insn)
869 && (condjump_p (insn) || condjump_in_parallel_p (insn)))
871 int prediction;
873 prediction = mostly_true_jump (insn, get_branch_condition (insn, label));
874 switch (prediction)
876 case 2:
877 flags |= (ATTR_FLAG_very_likely | ATTR_FLAG_likely);
878 break;
879 case 1:
880 flags |= ATTR_FLAG_likely;
881 break;
882 case 0:
883 flags |= ATTR_FLAG_unlikely;
884 break;
885 case -1:
886 flags |= (ATTR_FLAG_very_unlikely | ATTR_FLAG_unlikely);
887 break;
889 default:
890 abort ();
893 else
894 flags |= (ATTR_FLAG_very_likely | ATTR_FLAG_likely);
896 return flags;
899 /* Return 1 if INSN is a destination that will be branched to rarely (the
900 return point of a function); return 2 if DEST will be branched to very
901 rarely (a call to a function that doesn't return). Otherwise,
902 return 0. */
904 static int
905 rare_destination (rtx insn)
907 int jump_count = 0;
908 rtx next;
910 for (; insn; insn = next)
912 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
913 insn = XVECEXP (PATTERN (insn), 0, 0);
915 next = NEXT_INSN (insn);
917 switch (GET_CODE (insn))
919 case CODE_LABEL:
920 return 0;
921 case BARRIER:
922 /* A BARRIER can either be after a JUMP_INSN or a CALL_INSN. We
923 don't scan past JUMP_INSNs, so any barrier we find here must
924 have been after a CALL_INSN and hence mean the call doesn't
925 return. */
926 return 2;
927 case JUMP_INSN:
928 if (GET_CODE (PATTERN (insn)) == RETURN)
929 return 1;
930 else if (simplejump_p (insn)
931 && jump_count++ < 10)
932 next = JUMP_LABEL (insn);
933 else
934 return 0;
936 default:
937 break;
941 /* If we got here it means we hit the end of the function. So this
942 is an unlikely destination. */
944 return 1;
947 /* Return truth value of the statement that this branch
948 is mostly taken. If we think that the branch is extremely likely
949 to be taken, we return 2. If the branch is slightly more likely to be
950 taken, return 1. If the branch is slightly less likely to be taken,
951 return 0 and if the branch is highly unlikely to be taken, return -1.
953 CONDITION, if nonzero, is the condition that JUMP_INSN is testing. */
955 static int
956 mostly_true_jump (rtx jump_insn, rtx condition)
958 rtx target_label = JUMP_LABEL (jump_insn);
959 rtx insn, note;
960 int rare_dest = rare_destination (target_label);
961 int rare_fallthrough = rare_destination (NEXT_INSN (jump_insn));
963 /* If branch probabilities are available, then use that number since it
964 always gives a correct answer. */
965 note = find_reg_note (jump_insn, REG_BR_PROB, 0);
966 if (note)
968 int prob = INTVAL (XEXP (note, 0));
970 if (prob >= REG_BR_PROB_BASE * 9 / 10)
971 return 2;
972 else if (prob >= REG_BR_PROB_BASE / 2)
973 return 1;
974 else if (prob >= REG_BR_PROB_BASE / 10)
975 return 0;
976 else
977 return -1;
980 /* ??? Ought to use estimate_probability instead. */
982 /* If this is a branch outside a loop, it is highly unlikely. */
983 if (GET_CODE (PATTERN (jump_insn)) == SET
984 && GET_CODE (SET_SRC (PATTERN (jump_insn))) == IF_THEN_ELSE
985 && ((GET_CODE (XEXP (SET_SRC (PATTERN (jump_insn)), 1)) == LABEL_REF
986 && LABEL_OUTSIDE_LOOP_P (XEXP (SET_SRC (PATTERN (jump_insn)), 1)))
987 || (GET_CODE (XEXP (SET_SRC (PATTERN (jump_insn)), 2)) == LABEL_REF
988 && LABEL_OUTSIDE_LOOP_P (XEXP (SET_SRC (PATTERN (jump_insn)), 2)))))
989 return -1;
991 if (target_label)
993 /* If this is the test of a loop, it is very likely true. We scan
994 backwards from the target label. If we find a NOTE_INSN_LOOP_BEG
995 before the next real insn, we assume the branch is to the top of
996 the loop. */
997 for (insn = PREV_INSN (target_label);
998 insn && NOTE_P (insn);
999 insn = PREV_INSN (insn))
1000 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1001 return 2;
1004 /* Look at the relative rarities of the fallthrough and destination. If
1005 they differ, we can predict the branch that way. */
1007 switch (rare_fallthrough - rare_dest)
1009 case -2:
1010 return -1;
1011 case -1:
1012 return 0;
1013 case 0:
1014 break;
1015 case 1:
1016 return 1;
1017 case 2:
1018 return 2;
1021 /* If we couldn't figure out what this jump was, assume it won't be
1022 taken. This should be rare. */
1023 if (condition == 0)
1024 return 0;
1026 /* EQ tests are usually false and NE tests are usually true. Also,
1027 most quantities are positive, so we can make the appropriate guesses
1028 about signed comparisons against zero. */
1029 switch (GET_CODE (condition))
1031 case CONST_INT:
1032 /* Unconditional branch. */
1033 return 1;
1034 case EQ:
1035 return 0;
1036 case NE:
1037 return 1;
1038 case LE:
1039 case LT:
1040 if (XEXP (condition, 1) == const0_rtx)
1041 return 0;
1042 break;
1043 case GE:
1044 case GT:
1045 if (XEXP (condition, 1) == const0_rtx)
1046 return 1;
1047 break;
1049 default:
1050 break;
1053 /* Predict backward branches usually take, forward branches usually not. If
1054 we don't know whether this is forward or backward, assume the branch
1055 will be taken, since most are. */
1056 return (target_label == 0 || INSN_UID (jump_insn) > max_uid
1057 || INSN_UID (target_label) > max_uid
1058 || (uid_to_ruid[INSN_UID (jump_insn)]
1059 > uid_to_ruid[INSN_UID (target_label)]));
1062 /* Return the condition under which INSN will branch to TARGET. If TARGET
1063 is zero, return the condition under which INSN will return. If INSN is
1064 an unconditional branch, return const_true_rtx. If INSN isn't a simple
1065 type of jump, or it doesn't go to TARGET, return 0. */
1067 static rtx
1068 get_branch_condition (rtx insn, rtx target)
1070 rtx pat = PATTERN (insn);
1071 rtx src;
1073 if (condjump_in_parallel_p (insn))
1074 pat = XVECEXP (pat, 0, 0);
1076 if (GET_CODE (pat) == RETURN)
1077 return target == 0 ? const_true_rtx : 0;
1079 else if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
1080 return 0;
1082 src = SET_SRC (pat);
1083 if (GET_CODE (src) == LABEL_REF && XEXP (src, 0) == target)
1084 return const_true_rtx;
1086 else if (GET_CODE (src) == IF_THEN_ELSE
1087 && ((target == 0 && GET_CODE (XEXP (src, 1)) == RETURN)
1088 || (GET_CODE (XEXP (src, 1)) == LABEL_REF
1089 && XEXP (XEXP (src, 1), 0) == target))
1090 && XEXP (src, 2) == pc_rtx)
1091 return XEXP (src, 0);
1093 else if (GET_CODE (src) == IF_THEN_ELSE
1094 && ((target == 0 && GET_CODE (XEXP (src, 2)) == RETURN)
1095 || (GET_CODE (XEXP (src, 2)) == LABEL_REF
1096 && XEXP (XEXP (src, 2), 0) == target))
1097 && XEXP (src, 1) == pc_rtx)
1099 enum rtx_code rev;
1100 rev = reversed_comparison_code (XEXP (src, 0), insn);
1101 if (rev != UNKNOWN)
1102 return gen_rtx_fmt_ee (rev, GET_MODE (XEXP (src, 0)),
1103 XEXP (XEXP (src, 0), 0),
1104 XEXP (XEXP (src, 0), 1));
1107 return 0;
1110 /* Return nonzero if CONDITION is more strict than the condition of
1111 INSN, i.e., if INSN will always branch if CONDITION is true. */
1113 static int
1114 condition_dominates_p (rtx condition, rtx insn)
1116 rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
1117 enum rtx_code code = GET_CODE (condition);
1118 enum rtx_code other_code;
1120 if (rtx_equal_p (condition, other_condition)
1121 || other_condition == const_true_rtx)
1122 return 1;
1124 else if (condition == const_true_rtx || other_condition == 0)
1125 return 0;
1127 other_code = GET_CODE (other_condition);
1128 if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
1129 || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
1130 || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
1131 return 0;
1133 return comparison_dominates_p (code, other_code);
1136 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1137 any insns already in the delay slot of JUMP. */
1139 static int
1140 redirect_with_delay_slots_safe_p (rtx jump, rtx newlabel, rtx seq)
1142 int flags, i;
1143 rtx pat = PATTERN (seq);
1145 /* Make sure all the delay slots of this jump would still
1146 be valid after threading the jump. If they are still
1147 valid, then return nonzero. */
1149 flags = get_jump_flags (jump, newlabel);
1150 for (i = 1; i < XVECLEN (pat, 0); i++)
1151 if (! (
1152 #ifdef ANNUL_IFFALSE_SLOTS
1153 (INSN_ANNULLED_BRANCH_P (jump)
1154 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1155 ? eligible_for_annul_false (jump, i - 1,
1156 XVECEXP (pat, 0, i), flags) :
1157 #endif
1158 #ifdef ANNUL_IFTRUE_SLOTS
1159 (INSN_ANNULLED_BRANCH_P (jump)
1160 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1161 ? eligible_for_annul_true (jump, i - 1,
1162 XVECEXP (pat, 0, i), flags) :
1163 #endif
1164 eligible_for_delay (jump, i - 1, XVECEXP (pat, 0, i), flags)))
1165 break;
1167 return (i == XVECLEN (pat, 0));
1170 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1171 any insns we wish to place in the delay slot of JUMP. */
1173 static int
1174 redirect_with_delay_list_safe_p (rtx jump, rtx newlabel, rtx delay_list)
1176 int flags, i;
1177 rtx li;
1179 /* Make sure all the insns in DELAY_LIST would still be
1180 valid after threading the jump. If they are still
1181 valid, then return nonzero. */
1183 flags = get_jump_flags (jump, newlabel);
1184 for (li = delay_list, i = 0; li; li = XEXP (li, 1), i++)
1185 if (! (
1186 #ifdef ANNUL_IFFALSE_SLOTS
1187 (INSN_ANNULLED_BRANCH_P (jump)
1188 && INSN_FROM_TARGET_P (XEXP (li, 0)))
1189 ? eligible_for_annul_false (jump, i, XEXP (li, 0), flags) :
1190 #endif
1191 #ifdef ANNUL_IFTRUE_SLOTS
1192 (INSN_ANNULLED_BRANCH_P (jump)
1193 && ! INSN_FROM_TARGET_P (XEXP (li, 0)))
1194 ? eligible_for_annul_true (jump, i, XEXP (li, 0), flags) :
1195 #endif
1196 eligible_for_delay (jump, i, XEXP (li, 0), flags)))
1197 break;
1199 return (li == NULL);
1202 /* DELAY_LIST is a list of insns that have already been placed into delay
1203 slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
1204 If not, return 0; otherwise return 1. */
1206 static int
1207 check_annul_list_true_false (int annul_true_p, rtx delay_list)
1209 rtx temp;
1211 if (delay_list)
1213 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1215 rtx trial = XEXP (temp, 0);
1217 if ((annul_true_p && INSN_FROM_TARGET_P (trial))
1218 || (!annul_true_p && !INSN_FROM_TARGET_P (trial)))
1219 return 0;
1223 return 1;
1226 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
1227 the condition tested by INSN is CONDITION and the resources shown in
1228 OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1229 from SEQ's delay list, in addition to whatever insns it may execute
1230 (in DELAY_LIST). SETS and NEEDED are denote resources already set and
1231 needed while searching for delay slot insns. Return the concatenated
1232 delay list if possible, otherwise, return 0.
1234 SLOTS_TO_FILL is the total number of slots required by INSN, and
1235 PSLOTS_FILLED points to the number filled so far (also the number of
1236 insns in DELAY_LIST). It is updated with the number that have been
1237 filled from the SEQUENCE, if any.
1239 PANNUL_P points to a nonzero value if we already know that we need
1240 to annul INSN. If this routine determines that annulling is needed,
1241 it may set that value nonzero.
1243 PNEW_THREAD points to a location that is to receive the place at which
1244 execution should continue. */
1246 static rtx
1247 steal_delay_list_from_target (rtx insn, rtx condition, rtx seq,
1248 rtx delay_list, struct resources *sets,
1249 struct resources *needed,
1250 struct resources *other_needed,
1251 int slots_to_fill, int *pslots_filled,
1252 int *pannul_p, rtx *pnew_thread)
1254 rtx temp;
1255 int slots_remaining = slots_to_fill - *pslots_filled;
1256 int total_slots_filled = *pslots_filled;
1257 rtx new_delay_list = 0;
1258 int must_annul = *pannul_p;
1259 int used_annul = 0;
1260 int i;
1261 struct resources cc_set;
1263 /* We can't do anything if there are more delay slots in SEQ than we
1264 can handle, or if we don't know that it will be a taken branch.
1265 We know that it will be a taken branch if it is either an unconditional
1266 branch or a conditional branch with a stricter branch condition.
1268 Also, exit if the branch has more than one set, since then it is computing
1269 other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1270 ??? It may be possible to move other sets into INSN in addition to
1271 moving the instructions in the delay slots.
1273 We can not steal the delay list if one of the instructions in the
1274 current delay_list modifies the condition codes and the jump in the
1275 sequence is a conditional jump. We can not do this because we can
1276 not change the direction of the jump because the condition codes
1277 will effect the direction of the jump in the sequence. */
1279 CLEAR_RESOURCE (&cc_set);
1280 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1282 rtx trial = XEXP (temp, 0);
1284 mark_set_resources (trial, &cc_set, 0, MARK_SRC_DEST_CALL);
1285 if (insn_references_resource_p (XVECEXP (seq , 0, 0), &cc_set, 0))
1286 return delay_list;
1289 if (XVECLEN (seq, 0) - 1 > slots_remaining
1290 || ! condition_dominates_p (condition, XVECEXP (seq, 0, 0))
1291 || ! single_set (XVECEXP (seq, 0, 0)))
1292 return delay_list;
1294 #ifdef MD_CAN_REDIRECT_BRANCH
1295 /* On some targets, branches with delay slots can have a limited
1296 displacement. Give the back end a chance to tell us we can't do
1297 this. */
1298 if (! MD_CAN_REDIRECT_BRANCH (insn, XVECEXP (seq, 0, 0)))
1299 return delay_list;
1300 #endif
1302 for (i = 1; i < XVECLEN (seq, 0); i++)
1304 rtx trial = XVECEXP (seq, 0, i);
1305 int flags;
1307 if (insn_references_resource_p (trial, sets, 0)
1308 || insn_sets_resource_p (trial, needed, 0)
1309 || insn_sets_resource_p (trial, sets, 0)
1310 #ifdef HAVE_cc0
1311 /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1312 delay list. */
1313 || find_reg_note (trial, REG_CC_USER, NULL_RTX)
1314 #endif
1315 /* If TRIAL is from the fallthrough code of an annulled branch insn
1316 in SEQ, we cannot use it. */
1317 || (INSN_ANNULLED_BRANCH_P (XVECEXP (seq, 0, 0))
1318 && ! INSN_FROM_TARGET_P (trial)))
1319 return delay_list;
1321 /* If this insn was already done (usually in a previous delay slot),
1322 pretend we put it in our delay slot. */
1323 if (redundant_insn (trial, insn, new_delay_list))
1324 continue;
1326 /* We will end up re-vectoring this branch, so compute flags
1327 based on jumping to the new label. */
1328 flags = get_jump_flags (insn, JUMP_LABEL (XVECEXP (seq, 0, 0)));
1330 if (! must_annul
1331 && ((condition == const_true_rtx
1332 || (! insn_sets_resource_p (trial, other_needed, 0)
1333 && ! may_trap_p (PATTERN (trial)))))
1334 ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1335 : (must_annul || (delay_list == NULL && new_delay_list == NULL))
1336 && (must_annul = 1,
1337 check_annul_list_true_false (0, delay_list)
1338 && check_annul_list_true_false (0, new_delay_list)
1339 && eligible_for_annul_false (insn, total_slots_filled,
1340 trial, flags)))
1342 if (must_annul)
1343 used_annul = 1;
1344 temp = copy_rtx (trial);
1345 INSN_FROM_TARGET_P (temp) = 1;
1346 new_delay_list = add_to_delay_list (temp, new_delay_list);
1347 total_slots_filled++;
1349 if (--slots_remaining == 0)
1350 break;
1352 else
1353 return delay_list;
1356 /* Show the place to which we will be branching. */
1357 *pnew_thread = next_active_insn (JUMP_LABEL (XVECEXP (seq, 0, 0)));
1359 /* Add any new insns to the delay list and update the count of the
1360 number of slots filled. */
1361 *pslots_filled = total_slots_filled;
1362 if (used_annul)
1363 *pannul_p = 1;
1365 if (delay_list == 0)
1366 return new_delay_list;
1368 for (temp = new_delay_list; temp; temp = XEXP (temp, 1))
1369 delay_list = add_to_delay_list (XEXP (temp, 0), delay_list);
1371 return delay_list;
1374 /* Similar to steal_delay_list_from_target except that SEQ is on the
1375 fallthrough path of INSN. Here we only do something if the delay insn
1376 of SEQ is an unconditional branch. In that case we steal its delay slot
1377 for INSN since unconditional branches are much easier to fill. */
1379 static rtx
1380 steal_delay_list_from_fallthrough (rtx insn, rtx condition, rtx seq,
1381 rtx delay_list, struct resources *sets,
1382 struct resources *needed,
1383 struct resources *other_needed,
1384 int slots_to_fill, int *pslots_filled,
1385 int *pannul_p)
1387 int i;
1388 int flags;
1389 int must_annul = *pannul_p;
1390 int used_annul = 0;
1392 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1394 /* We can't do anything if SEQ's delay insn isn't an
1395 unconditional branch. */
1397 if (! simplejump_p (XVECEXP (seq, 0, 0))
1398 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) != RETURN)
1399 return delay_list;
1401 for (i = 1; i < XVECLEN (seq, 0); i++)
1403 rtx trial = XVECEXP (seq, 0, i);
1405 /* If TRIAL sets CC0, stealing it will move it too far from the use
1406 of CC0. */
1407 if (insn_references_resource_p (trial, sets, 0)
1408 || insn_sets_resource_p (trial, needed, 0)
1409 || insn_sets_resource_p (trial, sets, 0)
1410 #ifdef HAVE_cc0
1411 || sets_cc0_p (PATTERN (trial))
1412 #endif
1415 break;
1417 /* If this insn was already done, we don't need it. */
1418 if (redundant_insn (trial, insn, delay_list))
1420 delete_from_delay_slot (trial);
1421 continue;
1424 if (! must_annul
1425 && ((condition == const_true_rtx
1426 || (! insn_sets_resource_p (trial, other_needed, 0)
1427 && ! may_trap_p (PATTERN (trial)))))
1428 ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1429 : (must_annul || delay_list == NULL) && (must_annul = 1,
1430 check_annul_list_true_false (1, delay_list)
1431 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1433 if (must_annul)
1434 used_annul = 1;
1435 delete_from_delay_slot (trial);
1436 delay_list = add_to_delay_list (trial, delay_list);
1438 if (++(*pslots_filled) == slots_to_fill)
1439 break;
1441 else
1442 break;
1445 if (used_annul)
1446 *pannul_p = 1;
1447 return delay_list;
1450 /* Try merging insns starting at THREAD which match exactly the insns in
1451 INSN's delay list.
1453 If all insns were matched and the insn was previously annulling, the
1454 annul bit will be cleared.
1456 For each insn that is merged, if the branch is or will be non-annulling,
1457 we delete the merged insn. */
1459 static void
1460 try_merge_delay_insns (rtx insn, rtx thread)
1462 rtx trial, next_trial;
1463 rtx delay_insn = XVECEXP (PATTERN (insn), 0, 0);
1464 int annul_p = INSN_ANNULLED_BRANCH_P (delay_insn);
1465 int slot_number = 1;
1466 int num_slots = XVECLEN (PATTERN (insn), 0);
1467 rtx next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1468 struct resources set, needed;
1469 rtx merged_insns = 0;
1470 int i;
1471 int flags;
1473 flags = get_jump_flags (delay_insn, JUMP_LABEL (delay_insn));
1475 CLEAR_RESOURCE (&needed);
1476 CLEAR_RESOURCE (&set);
1478 /* If this is not an annulling branch, take into account anything needed in
1479 INSN's delay slot. This prevents two increments from being incorrectly
1480 folded into one. If we are annulling, this would be the correct
1481 thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1482 will essentially disable this optimization. This method is somewhat of
1483 a kludge, but I don't see a better way.) */
1484 if (! annul_p)
1485 for (i = 1 ; i < num_slots; i++)
1486 if (XVECEXP (PATTERN (insn), 0, i))
1487 mark_referenced_resources (XVECEXP (PATTERN (insn), 0, i), &needed, 1);
1489 for (trial = thread; !stop_search_p (trial, 1); trial = next_trial)
1491 rtx pat = PATTERN (trial);
1492 rtx oldtrial = trial;
1494 next_trial = next_nonnote_insn (trial);
1496 /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1497 if (NONJUMP_INSN_P (trial)
1498 && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1499 continue;
1501 if (GET_CODE (next_to_match) == GET_CODE (trial)
1502 #ifdef HAVE_cc0
1503 /* We can't share an insn that sets cc0. */
1504 && ! sets_cc0_p (pat)
1505 #endif
1506 && ! insn_references_resource_p (trial, &set, 1)
1507 && ! insn_sets_resource_p (trial, &set, 1)
1508 && ! insn_sets_resource_p (trial, &needed, 1)
1509 && (trial = try_split (pat, trial, 0)) != 0
1510 /* Update next_trial, in case try_split succeeded. */
1511 && (next_trial = next_nonnote_insn (trial))
1512 /* Likewise THREAD. */
1513 && (thread = oldtrial == thread ? trial : thread)
1514 && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1515 /* Have to test this condition if annul condition is different
1516 from (and less restrictive than) non-annulling one. */
1517 && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1520 if (! annul_p)
1522 update_block (trial, thread);
1523 if (trial == thread)
1524 thread = next_active_insn (thread);
1526 delete_related_insns (trial);
1527 INSN_FROM_TARGET_P (next_to_match) = 0;
1529 else
1530 merged_insns = gen_rtx_INSN_LIST (VOIDmode, trial, merged_insns);
1532 if (++slot_number == num_slots)
1533 break;
1535 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1538 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1539 mark_referenced_resources (trial, &needed, 1);
1542 /* See if we stopped on a filled insn. If we did, try to see if its
1543 delay slots match. */
1544 if (slot_number != num_slots
1545 && trial && NONJUMP_INSN_P (trial)
1546 && GET_CODE (PATTERN (trial)) == SEQUENCE
1547 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial), 0, 0)))
1549 rtx pat = PATTERN (trial);
1550 rtx filled_insn = XVECEXP (pat, 0, 0);
1552 /* Account for resources set/needed by the filled insn. */
1553 mark_set_resources (filled_insn, &set, 0, MARK_SRC_DEST_CALL);
1554 mark_referenced_resources (filled_insn, &needed, 1);
1556 for (i = 1; i < XVECLEN (pat, 0); i++)
1558 rtx dtrial = XVECEXP (pat, 0, i);
1560 if (! insn_references_resource_p (dtrial, &set, 1)
1561 && ! insn_sets_resource_p (dtrial, &set, 1)
1562 && ! insn_sets_resource_p (dtrial, &needed, 1)
1563 #ifdef HAVE_cc0
1564 && ! sets_cc0_p (PATTERN (dtrial))
1565 #endif
1566 && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1567 && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1569 if (! annul_p)
1571 rtx new;
1573 update_block (dtrial, thread);
1574 new = delete_from_delay_slot (dtrial);
1575 if (INSN_DELETED_P (thread))
1576 thread = new;
1577 INSN_FROM_TARGET_P (next_to_match) = 0;
1579 else
1580 merged_insns = gen_rtx_INSN_LIST (SImode, dtrial,
1581 merged_insns);
1583 if (++slot_number == num_slots)
1584 break;
1586 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1588 else
1590 /* Keep track of the set/referenced resources for the delay
1591 slots of any trial insns we encounter. */
1592 mark_set_resources (dtrial, &set, 0, MARK_SRC_DEST_CALL);
1593 mark_referenced_resources (dtrial, &needed, 1);
1598 /* If all insns in the delay slot have been matched and we were previously
1599 annulling the branch, we need not any more. In that case delete all the
1600 merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1601 the delay list so that we know that it isn't only being used at the
1602 target. */
1603 if (slot_number == num_slots && annul_p)
1605 for (; merged_insns; merged_insns = XEXP (merged_insns, 1))
1607 if (GET_MODE (merged_insns) == SImode)
1609 rtx new;
1611 update_block (XEXP (merged_insns, 0), thread);
1612 new = delete_from_delay_slot (XEXP (merged_insns, 0));
1613 if (INSN_DELETED_P (thread))
1614 thread = new;
1616 else
1618 update_block (XEXP (merged_insns, 0), thread);
1619 delete_related_insns (XEXP (merged_insns, 0));
1623 INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1625 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1626 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i)) = 0;
1630 /* See if INSN is redundant with an insn in front of TARGET. Often this
1631 is called when INSN is a candidate for a delay slot of TARGET.
1632 DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1633 of INSN. Often INSN will be redundant with an insn in a delay slot of
1634 some previous insn. This happens when we have a series of branches to the
1635 same label; in that case the first insn at the target might want to go
1636 into each of the delay slots.
1638 If we are not careful, this routine can take up a significant fraction
1639 of the total compilation time (4%), but only wins rarely. Hence we
1640 speed this routine up by making two passes. The first pass goes back
1641 until it hits a label and sees if it finds an insn with an identical
1642 pattern. Only in this (relatively rare) event does it check for
1643 data conflicts.
1645 We do not split insns we encounter. This could cause us not to find a
1646 redundant insn, but the cost of splitting seems greater than the possible
1647 gain in rare cases. */
1649 static rtx
1650 redundant_insn (rtx insn, rtx target, rtx delay_list)
1652 rtx target_main = target;
1653 rtx ipat = PATTERN (insn);
1654 rtx trial, pat;
1655 struct resources needed, set;
1656 int i;
1657 unsigned insns_to_search;
1659 /* If INSN has any REG_UNUSED notes, it can't match anything since we
1660 are allowed to not actually assign to such a register. */
1661 if (find_reg_note (insn, REG_UNUSED, NULL_RTX) != 0)
1662 return 0;
1664 /* Scan backwards looking for a match. */
1665 for (trial = PREV_INSN (target),
1666 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1667 trial && insns_to_search > 0;
1668 trial = PREV_INSN (trial), --insns_to_search)
1670 if (LABEL_P (trial))
1671 return 0;
1673 if (! INSN_P (trial))
1674 continue;
1676 pat = PATTERN (trial);
1677 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1678 continue;
1680 if (GET_CODE (pat) == SEQUENCE)
1682 /* Stop for a CALL and its delay slots because it is difficult to
1683 track its resource needs correctly. */
1684 if (CALL_P (XVECEXP (pat, 0, 0)))
1685 return 0;
1687 /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1688 slots because it is difficult to track its resource needs
1689 correctly. */
1691 #ifdef INSN_SETS_ARE_DELAYED
1692 if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1693 return 0;
1694 #endif
1696 #ifdef INSN_REFERENCES_ARE_DELAYED
1697 if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1698 return 0;
1699 #endif
1701 /* See if any of the insns in the delay slot match, updating
1702 resource requirements as we go. */
1703 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1704 if (GET_CODE (XVECEXP (pat, 0, i)) == GET_CODE (insn)
1705 && rtx_equal_p (PATTERN (XVECEXP (pat, 0, i)), ipat)
1706 && ! find_reg_note (XVECEXP (pat, 0, i), REG_UNUSED, NULL_RTX))
1707 break;
1709 /* If found a match, exit this loop early. */
1710 if (i > 0)
1711 break;
1714 else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat)
1715 && ! find_reg_note (trial, REG_UNUSED, NULL_RTX))
1716 break;
1719 /* If we didn't find an insn that matches, return 0. */
1720 if (trial == 0)
1721 return 0;
1723 /* See what resources this insn sets and needs. If they overlap, or
1724 if this insn references CC0, it can't be redundant. */
1726 CLEAR_RESOURCE (&needed);
1727 CLEAR_RESOURCE (&set);
1728 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1729 mark_referenced_resources (insn, &needed, 1);
1731 /* If TARGET is a SEQUENCE, get the main insn. */
1732 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1733 target_main = XVECEXP (PATTERN (target), 0, 0);
1735 if (resource_conflicts_p (&needed, &set)
1736 #ifdef HAVE_cc0
1737 || reg_mentioned_p (cc0_rtx, ipat)
1738 #endif
1739 /* The insn requiring the delay may not set anything needed or set by
1740 INSN. */
1741 || insn_sets_resource_p (target_main, &needed, 1)
1742 || insn_sets_resource_p (target_main, &set, 1))
1743 return 0;
1745 /* Insns we pass may not set either NEEDED or SET, so merge them for
1746 simpler tests. */
1747 needed.memory |= set.memory;
1748 needed.unch_memory |= set.unch_memory;
1749 IOR_HARD_REG_SET (needed.regs, set.regs);
1751 /* This insn isn't redundant if it conflicts with an insn that either is
1752 or will be in a delay slot of TARGET. */
1754 while (delay_list)
1756 if (insn_sets_resource_p (XEXP (delay_list, 0), &needed, 1))
1757 return 0;
1758 delay_list = XEXP (delay_list, 1);
1761 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1762 for (i = 1; i < XVECLEN (PATTERN (target), 0); i++)
1763 if (insn_sets_resource_p (XVECEXP (PATTERN (target), 0, i), &needed, 1))
1764 return 0;
1766 /* Scan backwards until we reach a label or an insn that uses something
1767 INSN sets or sets something insn uses or sets. */
1769 for (trial = PREV_INSN (target),
1770 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1771 trial && !LABEL_P (trial) && insns_to_search > 0;
1772 trial = PREV_INSN (trial), --insns_to_search)
1774 if (!INSN_P (trial))
1775 continue;
1777 pat = PATTERN (trial);
1778 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1779 continue;
1781 if (GET_CODE (pat) == SEQUENCE)
1783 /* If this is a CALL_INSN and its delay slots, it is hard to track
1784 the resource needs properly, so give up. */
1785 if (CALL_P (XVECEXP (pat, 0, 0)))
1786 return 0;
1788 /* If this is an INSN or JUMP_INSN with delayed effects, it
1789 is hard to track the resource needs properly, so give up. */
1791 #ifdef INSN_SETS_ARE_DELAYED
1792 if (INSN_SETS_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1793 return 0;
1794 #endif
1796 #ifdef INSN_REFERENCES_ARE_DELAYED
1797 if (INSN_REFERENCES_ARE_DELAYED (XVECEXP (pat, 0, 0)))
1798 return 0;
1799 #endif
1801 /* See if any of the insns in the delay slot match, updating
1802 resource requirements as we go. */
1803 for (i = XVECLEN (pat, 0) - 1; i > 0; i--)
1805 rtx candidate = XVECEXP (pat, 0, i);
1807 /* If an insn will be annulled if the branch is false, it isn't
1808 considered as a possible duplicate insn. */
1809 if (rtx_equal_p (PATTERN (candidate), ipat)
1810 && ! (INSN_ANNULLED_BRANCH_P (XVECEXP (pat, 0, 0))
1811 && INSN_FROM_TARGET_P (candidate)))
1813 /* Show that this insn will be used in the sequel. */
1814 INSN_FROM_TARGET_P (candidate) = 0;
1815 return candidate;
1818 /* Unless this is an annulled insn from the target of a branch,
1819 we must stop if it sets anything needed or set by INSN. */
1820 if ((! INSN_ANNULLED_BRANCH_P (XVECEXP (pat, 0, 0))
1821 || ! INSN_FROM_TARGET_P (candidate))
1822 && insn_sets_resource_p (candidate, &needed, 1))
1823 return 0;
1826 /* If the insn requiring the delay slot conflicts with INSN, we
1827 must stop. */
1828 if (insn_sets_resource_p (XVECEXP (pat, 0, 0), &needed, 1))
1829 return 0;
1831 else
1833 /* See if TRIAL is the same as INSN. */
1834 pat = PATTERN (trial);
1835 if (rtx_equal_p (pat, ipat))
1836 return trial;
1838 /* Can't go any further if TRIAL conflicts with INSN. */
1839 if (insn_sets_resource_p (trial, &needed, 1))
1840 return 0;
1844 return 0;
1847 /* Return 1 if THREAD can only be executed in one way. If LABEL is nonzero,
1848 it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1849 is nonzero, we are allowed to fall into this thread; otherwise, we are
1850 not.
1852 If LABEL is used more than one or we pass a label other than LABEL before
1853 finding an active insn, we do not own this thread. */
1855 static int
1856 own_thread_p (rtx thread, rtx label, int allow_fallthrough)
1858 rtx active_insn;
1859 rtx insn;
1861 /* We don't own the function end. */
1862 if (thread == 0)
1863 return 0;
1865 /* Get the first active insn, or THREAD, if it is an active insn. */
1866 active_insn = next_active_insn (PREV_INSN (thread));
1868 for (insn = thread; insn != active_insn; insn = NEXT_INSN (insn))
1869 if (LABEL_P (insn)
1870 && (insn != label || LABEL_NUSES (insn) != 1))
1871 return 0;
1873 if (allow_fallthrough)
1874 return 1;
1876 /* Ensure that we reach a BARRIER before any insn or label. */
1877 for (insn = prev_nonnote_insn (thread);
1878 insn == 0 || !BARRIER_P (insn);
1879 insn = prev_nonnote_insn (insn))
1880 if (insn == 0
1881 || LABEL_P (insn)
1882 || (NONJUMP_INSN_P (insn)
1883 && GET_CODE (PATTERN (insn)) != USE
1884 && GET_CODE (PATTERN (insn)) != CLOBBER))
1885 return 0;
1887 return 1;
1890 /* Called when INSN is being moved from a location near the target of a jump.
1891 We leave a marker of the form (use (INSN)) immediately in front
1892 of WHERE for mark_target_live_regs. These markers will be deleted when
1893 reorg finishes.
1895 We used to try to update the live status of registers if WHERE is at
1896 the start of a basic block, but that can't work since we may remove a
1897 BARRIER in relax_delay_slots. */
1899 static void
1900 update_block (rtx insn, rtx where)
1902 /* Ignore if this was in a delay slot and it came from the target of
1903 a branch. */
1904 if (INSN_FROM_TARGET_P (insn))
1905 return;
1907 emit_insn_before (gen_rtx_USE (VOIDmode, insn), where);
1909 /* INSN might be making a value live in a block where it didn't use to
1910 be. So recompute liveness information for this block. */
1912 incr_ticks_for_insn (insn);
1915 /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1916 the basic block containing the jump. */
1918 static int
1919 reorg_redirect_jump (rtx jump, rtx nlabel)
1921 incr_ticks_for_insn (jump);
1922 return redirect_jump (jump, nlabel, 1);
1925 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1926 We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1927 that reference values used in INSN. If we find one, then we move the
1928 REG_DEAD note to INSN.
1930 This is needed to handle the case where an later insn (after INSN) has a
1931 REG_DEAD note for a register used by INSN, and this later insn subsequently
1932 gets moved before a CODE_LABEL because it is a redundant insn. In this
1933 case, mark_target_live_regs may be confused into thinking the register
1934 is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1936 static void
1937 update_reg_dead_notes (rtx insn, rtx delayed_insn)
1939 rtx p, link, next;
1941 for (p = next_nonnote_insn (insn); p != delayed_insn;
1942 p = next_nonnote_insn (p))
1943 for (link = REG_NOTES (p); link; link = next)
1945 next = XEXP (link, 1);
1947 if (REG_NOTE_KIND (link) != REG_DEAD
1948 || !REG_P (XEXP (link, 0)))
1949 continue;
1951 if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
1953 /* Move the REG_DEAD note from P to INSN. */
1954 remove_note (p, link);
1955 XEXP (link, 1) = REG_NOTES (insn);
1956 REG_NOTES (insn) = link;
1961 /* Called when an insn redundant with start_insn is deleted. If there
1962 is a REG_DEAD note for the target of start_insn between start_insn
1963 and stop_insn, then the REG_DEAD note needs to be deleted since the
1964 value no longer dies there.
1966 If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1967 confused into thinking the register is dead. */
1969 static void
1970 fix_reg_dead_note (rtx start_insn, rtx stop_insn)
1972 rtx p, link, next;
1974 for (p = next_nonnote_insn (start_insn); p != stop_insn;
1975 p = next_nonnote_insn (p))
1976 for (link = REG_NOTES (p); link; link = next)
1978 next = XEXP (link, 1);
1980 if (REG_NOTE_KIND (link) != REG_DEAD
1981 || !REG_P (XEXP (link, 0)))
1982 continue;
1984 if (reg_set_p (XEXP (link, 0), PATTERN (start_insn)))
1986 remove_note (p, link);
1987 return;
1992 /* Delete any REG_UNUSED notes that exist on INSN but not on REDUNDANT_INSN.
1994 This handles the case of udivmodXi4 instructions which optimize their
1995 output depending on whether any REG_UNUSED notes are present.
1996 we must make sure that INSN calculates as many results as REDUNDANT_INSN
1997 does. */
1999 static void
2000 update_reg_unused_notes (rtx insn, rtx redundant_insn)
2002 rtx link, next;
2004 for (link = REG_NOTES (insn); link; link = next)
2006 next = XEXP (link, 1);
2008 if (REG_NOTE_KIND (link) != REG_UNUSED
2009 || !REG_P (XEXP (link, 0)))
2010 continue;
2012 if (! find_regno_note (redundant_insn, REG_UNUSED,
2013 REGNO (XEXP (link, 0))))
2014 remove_note (insn, link);
2018 /* Scan a function looking for insns that need a delay slot and find insns to
2019 put into the delay slot.
2021 NON_JUMPS_P is nonzero if we are to only try to fill non-jump insns (such
2022 as calls). We do these first since we don't want jump insns (that are
2023 easier to fill) to get the only insns that could be used for non-jump insns.
2024 When it is zero, only try to fill JUMP_INSNs.
2026 When slots are filled in this manner, the insns (including the
2027 delay_insn) are put together in a SEQUENCE rtx. In this fashion,
2028 it is possible to tell whether a delay slot has really been filled
2029 or not. `final' knows how to deal with this, by communicating
2030 through FINAL_SEQUENCE. */
2032 static void
2033 fill_simple_delay_slots (int non_jumps_p)
2035 rtx insn, pat, trial, next_trial;
2036 int i;
2037 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2038 struct resources needed, set;
2039 int slots_to_fill, slots_filled;
2040 rtx delay_list;
2042 for (i = 0; i < num_unfilled_slots; i++)
2044 int flags;
2045 /* Get the next insn to fill. If it has already had any slots assigned,
2046 we can't do anything with it. Maybe we'll improve this later. */
2048 insn = unfilled_slots_base[i];
2049 if (insn == 0
2050 || INSN_DELETED_P (insn)
2051 || (NONJUMP_INSN_P (insn)
2052 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2053 || (JUMP_P (insn) && non_jumps_p)
2054 || (!JUMP_P (insn) && ! non_jumps_p))
2055 continue;
2057 /* It may have been that this insn used to need delay slots, but
2058 now doesn't; ignore in that case. This can happen, for example,
2059 on the HP PA RISC, where the number of delay slots depends on
2060 what insns are nearby. */
2061 slots_to_fill = num_delay_slots (insn);
2063 /* Some machine description have defined instructions to have
2064 delay slots only in certain circumstances which may depend on
2065 nearby insns (which change due to reorg's actions).
2067 For example, the PA port normally has delay slots for unconditional
2068 jumps.
2070 However, the PA port claims such jumps do not have a delay slot
2071 if they are immediate successors of certain CALL_INSNs. This
2072 allows the port to favor filling the delay slot of the call with
2073 the unconditional jump. */
2074 if (slots_to_fill == 0)
2075 continue;
2077 /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
2078 says how many. After initialization, first try optimizing
2080 call _foo call _foo
2081 nop add %o7,.-L1,%o7
2082 b,a L1
2085 If this case applies, the delay slot of the call is filled with
2086 the unconditional jump. This is done first to avoid having the
2087 delay slot of the call filled in the backward scan. Also, since
2088 the unconditional jump is likely to also have a delay slot, that
2089 insn must exist when it is subsequently scanned.
2091 This is tried on each insn with delay slots as some machines
2092 have insns which perform calls, but are not represented as
2093 CALL_INSNs. */
2095 slots_filled = 0;
2096 delay_list = 0;
2098 if (JUMP_P (insn))
2099 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2100 else
2101 flags = get_jump_flags (insn, NULL_RTX);
2103 if ((trial = next_active_insn (insn))
2104 && JUMP_P (trial)
2105 && simplejump_p (trial)
2106 && eligible_for_delay (insn, slots_filled, trial, flags)
2107 && no_labels_between_p (insn, trial)
2108 && ! can_throw_internal (trial))
2110 rtx *tmp;
2111 slots_filled++;
2112 delay_list = add_to_delay_list (trial, delay_list);
2114 /* TRIAL may have had its delay slot filled, then unfilled. When
2115 the delay slot is unfilled, TRIAL is placed back on the unfilled
2116 slots obstack. Unfortunately, it is placed on the end of the
2117 obstack, not in its original location. Therefore, we must search
2118 from entry i + 1 to the end of the unfilled slots obstack to
2119 try and find TRIAL. */
2120 tmp = &unfilled_slots_base[i + 1];
2121 while (*tmp != trial && tmp != unfilled_slots_next)
2122 tmp++;
2124 /* Remove the unconditional jump from consideration for delay slot
2125 filling and unthread it. */
2126 if (*tmp == trial)
2127 *tmp = 0;
2129 rtx next = NEXT_INSN (trial);
2130 rtx prev = PREV_INSN (trial);
2131 if (prev)
2132 NEXT_INSN (prev) = next;
2133 if (next)
2134 PREV_INSN (next) = prev;
2138 /* Now, scan backwards from the insn to search for a potential
2139 delay-slot candidate. Stop searching when a label or jump is hit.
2141 For each candidate, if it is to go into the delay slot (moved
2142 forward in execution sequence), it must not need or set any resources
2143 that were set by later insns and must not set any resources that
2144 are needed for those insns.
2146 The delay slot insn itself sets resources unless it is a call
2147 (in which case the called routine, not the insn itself, is doing
2148 the setting). */
2150 if (slots_filled < slots_to_fill)
2152 CLEAR_RESOURCE (&needed);
2153 CLEAR_RESOURCE (&set);
2154 mark_set_resources (insn, &set, 0, MARK_SRC_DEST);
2155 mark_referenced_resources (insn, &needed, 0);
2157 for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, 1);
2158 trial = next_trial)
2160 next_trial = prev_nonnote_insn (trial);
2162 /* This must be an INSN or CALL_INSN. */
2163 pat = PATTERN (trial);
2165 /* USE and CLOBBER at this level was just for flow; ignore it. */
2166 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2167 continue;
2169 /* Check for resource conflict first, to avoid unnecessary
2170 splitting. */
2171 if (! insn_references_resource_p (trial, &set, 1)
2172 && ! insn_sets_resource_p (trial, &set, 1)
2173 && ! insn_sets_resource_p (trial, &needed, 1)
2174 #ifdef HAVE_cc0
2175 /* Can't separate set of cc0 from its use. */
2176 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2177 #endif
2178 && ! can_throw_internal (trial))
2180 trial = try_split (pat, trial, 1);
2181 next_trial = prev_nonnote_insn (trial);
2182 if (eligible_for_delay (insn, slots_filled, trial, flags))
2184 /* In this case, we are searching backward, so if we
2185 find insns to put on the delay list, we want
2186 to put them at the head, rather than the
2187 tail, of the list. */
2189 update_reg_dead_notes (trial, insn);
2190 delay_list = gen_rtx_INSN_LIST (VOIDmode,
2191 trial, delay_list);
2192 update_block (trial, trial);
2193 delete_related_insns (trial);
2194 if (slots_to_fill == ++slots_filled)
2195 break;
2196 continue;
2200 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2201 mark_referenced_resources (trial, &needed, 1);
2205 /* If all needed slots haven't been filled, we come here. */
2207 /* Try to optimize case of jumping around a single insn. */
2208 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
2209 if (slots_filled != slots_to_fill
2210 && delay_list == 0
2211 && JUMP_P (insn)
2212 && (condjump_p (insn) || condjump_in_parallel_p (insn)))
2214 delay_list = optimize_skip (insn);
2215 if (delay_list)
2216 slots_filled += 1;
2218 #endif
2220 /* Try to get insns from beyond the insn needing the delay slot.
2221 These insns can neither set or reference resources set in insns being
2222 skipped, cannot set resources in the insn being skipped, and, if this
2223 is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2224 call might not return).
2226 There used to be code which continued past the target label if
2227 we saw all uses of the target label. This code did not work,
2228 because it failed to account for some instructions which were
2229 both annulled and marked as from the target. This can happen as a
2230 result of optimize_skip. Since this code was redundant with
2231 fill_eager_delay_slots anyways, it was just deleted. */
2233 if (slots_filled != slots_to_fill
2234 /* If this instruction could throw an exception which is
2235 caught in the same function, then it's not safe to fill
2236 the delay slot with an instruction from beyond this
2237 point. For example, consider:
2239 int i = 2;
2241 try {
2242 f();
2243 i = 3;
2244 } catch (...) {}
2246 return i;
2248 Even though `i' is a local variable, we must be sure not
2249 to put `i = 3' in the delay slot if `f' might throw an
2250 exception.
2252 Presumably, we should also check to see if we could get
2253 back to this function via `setjmp'. */
2254 && ! can_throw_internal (insn)
2255 && (!JUMP_P (insn)
2256 || ((condjump_p (insn) || condjump_in_parallel_p (insn))
2257 && ! simplejump_p (insn)
2258 && JUMP_LABEL (insn) != 0)))
2260 /* Invariant: If insn is a JUMP_INSN, the insn's jump
2261 label. Otherwise, zero. */
2262 rtx target = 0;
2263 int maybe_never = 0;
2264 rtx pat, trial_delay;
2266 CLEAR_RESOURCE (&needed);
2267 CLEAR_RESOURCE (&set);
2269 if (CALL_P (insn))
2271 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2272 mark_referenced_resources (insn, &needed, 1);
2273 maybe_never = 1;
2275 else
2277 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2278 mark_referenced_resources (insn, &needed, 1);
2279 if (JUMP_P (insn))
2280 target = JUMP_LABEL (insn);
2283 if (target == 0)
2284 for (trial = next_nonnote_insn (insn); trial; trial = next_trial)
2286 next_trial = next_nonnote_insn (trial);
2288 if (LABEL_P (trial)
2289 || BARRIER_P (trial))
2290 break;
2292 /* We must have an INSN, JUMP_INSN, or CALL_INSN. */
2293 pat = PATTERN (trial);
2295 /* Stand-alone USE and CLOBBER are just for flow. */
2296 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2297 continue;
2299 /* If this already has filled delay slots, get the insn needing
2300 the delay slots. */
2301 if (GET_CODE (pat) == SEQUENCE)
2302 trial_delay = XVECEXP (pat, 0, 0);
2303 else
2304 trial_delay = trial;
2306 /* Stop our search when seeing an unconditional jump. */
2307 if (JUMP_P (trial_delay))
2308 break;
2310 /* See if we have a resource problem before we try to
2311 split. */
2312 if (GET_CODE (pat) != SEQUENCE
2313 && ! insn_references_resource_p (trial, &set, 1)
2314 && ! insn_sets_resource_p (trial, &set, 1)
2315 && ! insn_sets_resource_p (trial, &needed, 1)
2316 #ifdef HAVE_cc0
2317 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat))
2318 #endif
2319 && ! (maybe_never && may_trap_p (pat))
2320 && (trial = try_split (pat, trial, 0))
2321 && eligible_for_delay (insn, slots_filled, trial, flags)
2322 && ! can_throw_internal(trial))
2324 next_trial = next_nonnote_insn (trial);
2325 delay_list = add_to_delay_list (trial, delay_list);
2327 #ifdef HAVE_cc0
2328 if (reg_mentioned_p (cc0_rtx, pat))
2329 link_cc0_insns (trial);
2330 #endif
2332 delete_related_insns (trial);
2333 if (slots_to_fill == ++slots_filled)
2334 break;
2335 continue;
2338 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2339 mark_referenced_resources (trial, &needed, 1);
2341 /* Ensure we don't put insns between the setting of cc and the
2342 comparison by moving a setting of cc into an earlier delay
2343 slot since these insns could clobber the condition code. */
2344 set.cc = 1;
2346 /* If this is a call or jump, we might not get here. */
2347 if (CALL_P (trial_delay)
2348 || JUMP_P (trial_delay))
2349 maybe_never = 1;
2352 /* If there are slots left to fill and our search was stopped by an
2353 unconditional branch, try the insn at the branch target. We can
2354 redirect the branch if it works.
2356 Don't do this if the insn at the branch target is a branch. */
2357 if (slots_to_fill != slots_filled
2358 && trial
2359 && JUMP_P (trial)
2360 && simplejump_p (trial)
2361 && (target == 0 || JUMP_LABEL (trial) == target)
2362 && (next_trial = next_active_insn (JUMP_LABEL (trial))) != 0
2363 && ! (NONJUMP_INSN_P (next_trial)
2364 && GET_CODE (PATTERN (next_trial)) == SEQUENCE)
2365 && !JUMP_P (next_trial)
2366 && ! insn_references_resource_p (next_trial, &set, 1)
2367 && ! insn_sets_resource_p (next_trial, &set, 1)
2368 && ! insn_sets_resource_p (next_trial, &needed, 1)
2369 #ifdef HAVE_cc0
2370 && ! reg_mentioned_p (cc0_rtx, PATTERN (next_trial))
2371 #endif
2372 && ! (maybe_never && may_trap_p (PATTERN (next_trial)))
2373 && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2374 && eligible_for_delay (insn, slots_filled, next_trial, flags)
2375 && ! can_throw_internal (trial))
2377 /* See comment in relax_delay_slots about necessity of using
2378 next_real_insn here. */
2379 rtx new_label = next_real_insn (next_trial);
2381 if (new_label != 0)
2382 new_label = get_label_before (new_label);
2383 else
2384 new_label = find_end_label ();
2386 if (new_label)
2388 delay_list
2389 = add_to_delay_list (copy_rtx (next_trial), delay_list);
2390 slots_filled++;
2391 reorg_redirect_jump (trial, new_label);
2393 /* If we merged because we both jumped to the same place,
2394 redirect the original insn also. */
2395 if (target)
2396 reorg_redirect_jump (insn, new_label);
2401 /* If this is an unconditional jump, then try to get insns from the
2402 target of the jump. */
2403 if (JUMP_P (insn)
2404 && simplejump_p (insn)
2405 && slots_filled != slots_to_fill)
2406 delay_list
2407 = fill_slots_from_thread (insn, const_true_rtx,
2408 next_active_insn (JUMP_LABEL (insn)),
2409 NULL, 1, 1,
2410 own_thread_p (JUMP_LABEL (insn),
2411 JUMP_LABEL (insn), 0),
2412 slots_to_fill, &slots_filled,
2413 delay_list);
2415 if (delay_list)
2416 unfilled_slots_base[i]
2417 = emit_delay_sequence (insn, delay_list, slots_filled);
2419 if (slots_to_fill == slots_filled)
2420 unfilled_slots_base[i] = 0;
2422 note_delay_statistics (slots_filled, 0);
2425 #ifdef DELAY_SLOTS_FOR_EPILOGUE
2426 /* See if the epilogue needs any delay slots. Try to fill them if so.
2427 The only thing we can do is scan backwards from the end of the
2428 function. If we did this in a previous pass, it is incorrect to do it
2429 again. */
2430 if (current_function_epilogue_delay_list)
2431 return;
2433 slots_to_fill = DELAY_SLOTS_FOR_EPILOGUE;
2434 if (slots_to_fill == 0)
2435 return;
2437 slots_filled = 0;
2438 CLEAR_RESOURCE (&set);
2440 /* The frame pointer and stack pointer are needed at the beginning of
2441 the epilogue, so instructions setting them can not be put in the
2442 epilogue delay slot. However, everything else needed at function
2443 end is safe, so we don't want to use end_of_function_needs here. */
2444 CLEAR_RESOURCE (&needed);
2445 if (frame_pointer_needed)
2447 SET_HARD_REG_BIT (needed.regs, FRAME_POINTER_REGNUM);
2448 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2449 SET_HARD_REG_BIT (needed.regs, HARD_FRAME_POINTER_REGNUM);
2450 #endif
2451 if (! EXIT_IGNORE_STACK
2452 || current_function_sp_is_unchanging)
2453 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2455 else
2456 SET_HARD_REG_BIT (needed.regs, STACK_POINTER_REGNUM);
2458 #ifdef EPILOGUE_USES
2459 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2461 if (EPILOGUE_USES (i))
2462 SET_HARD_REG_BIT (needed.regs, i);
2464 #endif
2466 for (trial = get_last_insn (); ! stop_search_p (trial, 1);
2467 trial = PREV_INSN (trial))
2469 if (NOTE_P (trial))
2470 continue;
2471 pat = PATTERN (trial);
2472 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2473 continue;
2475 if (! insn_references_resource_p (trial, &set, 1)
2476 && ! insn_sets_resource_p (trial, &needed, 1)
2477 && ! insn_sets_resource_p (trial, &set, 1)
2478 #ifdef HAVE_cc0
2479 /* Don't want to mess with cc0 here. */
2480 && ! reg_mentioned_p (cc0_rtx, pat)
2481 #endif
2482 && ! can_throw_internal (trial))
2484 trial = try_split (pat, trial, 1);
2485 if (ELIGIBLE_FOR_EPILOGUE_DELAY (trial, slots_filled))
2487 /* Here as well we are searching backward, so put the
2488 insns we find on the head of the list. */
2490 current_function_epilogue_delay_list
2491 = gen_rtx_INSN_LIST (VOIDmode, trial,
2492 current_function_epilogue_delay_list);
2493 mark_end_of_function_resources (trial, 1);
2494 update_block (trial, trial);
2495 delete_related_insns (trial);
2497 /* Clear deleted bit so final.c will output the insn. */
2498 INSN_DELETED_P (trial) = 0;
2500 if (slots_to_fill == ++slots_filled)
2501 break;
2502 continue;
2506 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2507 mark_referenced_resources (trial, &needed, 1);
2510 note_delay_statistics (slots_filled, 0);
2511 #endif
2514 /* Try to find insns to place in delay slots.
2516 INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2517 or is an unconditional branch if CONDITION is const_true_rtx.
2518 *PSLOTS_FILLED is updated with the number of slots that we have filled.
2520 THREAD is a flow-of-control, either the insns to be executed if the
2521 branch is true or if the branch is false, THREAD_IF_TRUE says which.
2523 OPPOSITE_THREAD is the thread in the opposite direction. It is used
2524 to see if any potential delay slot insns set things needed there.
2526 LIKELY is nonzero if it is extremely likely that the branch will be
2527 taken and THREAD_IF_TRUE is set. This is used for the branch at the
2528 end of a loop back up to the top.
2530 OWN_THREAD and OWN_OPPOSITE_THREAD are true if we are the only user of the
2531 thread. I.e., it is the fallthrough code of our jump or the target of the
2532 jump when we are the only jump going there.
2534 If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2535 case, we can only take insns from the head of the thread for our delay
2536 slot. We then adjust the jump to point after the insns we have taken. */
2538 static rtx
2539 fill_slots_from_thread (rtx insn, rtx condition, rtx thread,
2540 rtx opposite_thread, int likely, int thread_if_true,
2541 int own_thread, int slots_to_fill,
2542 int *pslots_filled, rtx delay_list)
2544 rtx new_thread;
2545 struct resources opposite_needed, set, needed;
2546 rtx trial;
2547 int lose = 0;
2548 int must_annul = 0;
2549 int flags;
2551 /* Validate our arguments. */
2552 if ((condition == const_true_rtx && ! thread_if_true)
2553 || (! own_thread && ! thread_if_true))
2554 abort ();
2556 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2558 /* If our thread is the end of subroutine, we can't get any delay
2559 insns from that. */
2560 if (thread == 0)
2561 return delay_list;
2563 /* If this is an unconditional branch, nothing is needed at the
2564 opposite thread. Otherwise, compute what is needed there. */
2565 if (condition == const_true_rtx)
2566 CLEAR_RESOURCE (&opposite_needed);
2567 else
2568 mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2570 /* If the insn at THREAD can be split, do it here to avoid having to
2571 update THREAD and NEW_THREAD if it is done in the loop below. Also
2572 initialize NEW_THREAD. */
2574 new_thread = thread = try_split (PATTERN (thread), thread, 0);
2576 /* Scan insns at THREAD. We are looking for an insn that can be removed
2577 from THREAD (it neither sets nor references resources that were set
2578 ahead of it and it doesn't set anything needs by the insns ahead of
2579 it) and that either can be placed in an annulling insn or aren't
2580 needed at OPPOSITE_THREAD. */
2582 CLEAR_RESOURCE (&needed);
2583 CLEAR_RESOURCE (&set);
2585 /* If we do not own this thread, we must stop as soon as we find
2586 something that we can't put in a delay slot, since all we can do
2587 is branch into THREAD at a later point. Therefore, labels stop
2588 the search if this is not the `true' thread. */
2590 for (trial = thread;
2591 ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2592 trial = next_nonnote_insn (trial))
2594 rtx pat, old_trial;
2596 /* If we have passed a label, we no longer own this thread. */
2597 if (LABEL_P (trial))
2599 own_thread = 0;
2600 continue;
2603 pat = PATTERN (trial);
2604 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2605 continue;
2607 /* If TRIAL conflicts with the insns ahead of it, we lose. Also,
2608 don't separate or copy insns that set and use CC0. */
2609 if (! insn_references_resource_p (trial, &set, 1)
2610 && ! insn_sets_resource_p (trial, &set, 1)
2611 && ! insn_sets_resource_p (trial, &needed, 1)
2612 #ifdef HAVE_cc0
2613 && ! (reg_mentioned_p (cc0_rtx, pat)
2614 && (! own_thread || ! sets_cc0_p (pat)))
2615 #endif
2616 && ! can_throw_internal (trial))
2618 rtx prior_insn;
2620 /* If TRIAL is redundant with some insn before INSN, we don't
2621 actually need to add it to the delay list; we can merely pretend
2622 we did. */
2623 if ((prior_insn = redundant_insn (trial, insn, delay_list)))
2625 fix_reg_dead_note (prior_insn, insn);
2626 if (own_thread)
2628 update_block (trial, thread);
2629 if (trial == thread)
2631 thread = next_active_insn (thread);
2632 if (new_thread == trial)
2633 new_thread = thread;
2636 delete_related_insns (trial);
2638 else
2640 update_reg_unused_notes (prior_insn, trial);
2641 new_thread = next_active_insn (trial);
2644 continue;
2647 /* There are two ways we can win: If TRIAL doesn't set anything
2648 needed at the opposite thread and can't trap, or if it can
2649 go into an annulled delay slot. */
2650 if (!must_annul
2651 && (condition == const_true_rtx
2652 || (! insn_sets_resource_p (trial, &opposite_needed, 1)
2653 && ! may_trap_p (pat))))
2655 old_trial = trial;
2656 trial = try_split (pat, trial, 0);
2657 if (new_thread == old_trial)
2658 new_thread = trial;
2659 if (thread == old_trial)
2660 thread = trial;
2661 pat = PATTERN (trial);
2662 if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2663 goto winner;
2665 else if (0
2666 #ifdef ANNUL_IFTRUE_SLOTS
2667 || ! thread_if_true
2668 #endif
2669 #ifdef ANNUL_IFFALSE_SLOTS
2670 || thread_if_true
2671 #endif
2674 old_trial = trial;
2675 trial = try_split (pat, trial, 0);
2676 if (new_thread == old_trial)
2677 new_thread = trial;
2678 if (thread == old_trial)
2679 thread = trial;
2680 pat = PATTERN (trial);
2681 if ((must_annul || delay_list == NULL) && (thread_if_true
2682 ? check_annul_list_true_false (0, delay_list)
2683 && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2684 : check_annul_list_true_false (1, delay_list)
2685 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2687 rtx temp;
2689 must_annul = 1;
2690 winner:
2692 #ifdef HAVE_cc0
2693 if (reg_mentioned_p (cc0_rtx, pat))
2694 link_cc0_insns (trial);
2695 #endif
2697 /* If we own this thread, delete the insn. If this is the
2698 destination of a branch, show that a basic block status
2699 may have been updated. In any case, mark the new
2700 starting point of this thread. */
2701 if (own_thread)
2703 rtx note;
2705 update_block (trial, thread);
2706 if (trial == thread)
2708 thread = next_active_insn (thread);
2709 if (new_thread == trial)
2710 new_thread = thread;
2713 /* We are moving this insn, not deleting it. We must
2714 temporarily increment the use count on any referenced
2715 label lest it be deleted by delete_related_insns. */
2716 note = find_reg_note (trial, REG_LABEL, 0);
2717 /* REG_LABEL could be NOTE_INSN_DELETED_LABEL too. */
2718 if (note && LABEL_P (XEXP (note, 0)))
2719 LABEL_NUSES (XEXP (note, 0))++;
2721 delete_related_insns (trial);
2723 if (note && LABEL_P (XEXP (note, 0)))
2724 LABEL_NUSES (XEXP (note, 0))--;
2726 else
2727 new_thread = next_active_insn (trial);
2729 temp = own_thread ? trial : copy_rtx (trial);
2730 if (thread_if_true)
2731 INSN_FROM_TARGET_P (temp) = 1;
2733 delay_list = add_to_delay_list (temp, delay_list);
2735 if (slots_to_fill == ++(*pslots_filled))
2737 /* Even though we have filled all the slots, we
2738 may be branching to a location that has a
2739 redundant insn. Skip any if so. */
2740 while (new_thread && ! own_thread
2741 && ! insn_sets_resource_p (new_thread, &set, 1)
2742 && ! insn_sets_resource_p (new_thread, &needed, 1)
2743 && ! insn_references_resource_p (new_thread,
2744 &set, 1)
2745 && (prior_insn
2746 = redundant_insn (new_thread, insn,
2747 delay_list)))
2749 /* We know we do not own the thread, so no need
2750 to call update_block and delete_insn. */
2751 fix_reg_dead_note (prior_insn, insn);
2752 update_reg_unused_notes (prior_insn, new_thread);
2753 new_thread = next_active_insn (new_thread);
2755 break;
2758 continue;
2763 /* This insn can't go into a delay slot. */
2764 lose = 1;
2765 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2766 mark_referenced_resources (trial, &needed, 1);
2768 /* Ensure we don't put insns between the setting of cc and the comparison
2769 by moving a setting of cc into an earlier delay slot since these insns
2770 could clobber the condition code. */
2771 set.cc = 1;
2773 /* If this insn is a register-register copy and the next insn has
2774 a use of our destination, change it to use our source. That way,
2775 it will become a candidate for our delay slot the next time
2776 through this loop. This case occurs commonly in loops that
2777 scan a list.
2779 We could check for more complex cases than those tested below,
2780 but it doesn't seem worth it. It might also be a good idea to try
2781 to swap the two insns. That might do better.
2783 We can't do this if the next insn modifies our destination, because
2784 that would make the replacement into the insn invalid. We also can't
2785 do this if it modifies our source, because it might be an earlyclobber
2786 operand. This latter test also prevents updating the contents of
2787 a PRE_INC. We also can't do this if there's overlap of source and
2788 destination. Overlap may happen for larger-than-register-size modes. */
2790 if (NONJUMP_INSN_P (trial) && GET_CODE (pat) == SET
2791 && REG_P (SET_SRC (pat))
2792 && REG_P (SET_DEST (pat))
2793 && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2795 rtx next = next_nonnote_insn (trial);
2797 if (next && NONJUMP_INSN_P (next)
2798 && GET_CODE (PATTERN (next)) != USE
2799 && ! reg_set_p (SET_DEST (pat), next)
2800 && ! reg_set_p (SET_SRC (pat), next)
2801 && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2802 && ! modified_in_p (SET_DEST (pat), next))
2803 validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2807 /* If we stopped on a branch insn that has delay slots, see if we can
2808 steal some of the insns in those slots. */
2809 if (trial && NONJUMP_INSN_P (trial)
2810 && GET_CODE (PATTERN (trial)) == SEQUENCE
2811 && JUMP_P (XVECEXP (PATTERN (trial), 0, 0)))
2813 /* If this is the `true' thread, we will want to follow the jump,
2814 so we can only do this if we have taken everything up to here. */
2815 if (thread_if_true && trial == new_thread)
2817 delay_list
2818 = steal_delay_list_from_target (insn, condition, PATTERN (trial),
2819 delay_list, &set, &needed,
2820 &opposite_needed, slots_to_fill,
2821 pslots_filled, &must_annul,
2822 &new_thread);
2823 /* If we owned the thread and are told that it branched
2824 elsewhere, make sure we own the thread at the new location. */
2825 if (own_thread && trial != new_thread)
2826 own_thread = own_thread_p (new_thread, new_thread, 0);
2828 else if (! thread_if_true)
2829 delay_list
2830 = steal_delay_list_from_fallthrough (insn, condition,
2831 PATTERN (trial),
2832 delay_list, &set, &needed,
2833 &opposite_needed, slots_to_fill,
2834 pslots_filled, &must_annul);
2837 /* If we haven't found anything for this delay slot and it is very
2838 likely that the branch will be taken, see if the insn at our target
2839 increments or decrements a register with an increment that does not
2840 depend on the destination register. If so, try to place the opposite
2841 arithmetic insn after the jump insn and put the arithmetic insn in the
2842 delay slot. If we can't do this, return. */
2843 if (delay_list == 0 && likely && new_thread
2844 && NONJUMP_INSN_P (new_thread)
2845 && GET_CODE (PATTERN (new_thread)) != ASM_INPUT
2846 && asm_noperands (PATTERN (new_thread)) < 0)
2848 rtx pat = PATTERN (new_thread);
2849 rtx dest;
2850 rtx src;
2852 trial = new_thread;
2853 pat = PATTERN (trial);
2855 if (!NONJUMP_INSN_P (trial)
2856 || GET_CODE (pat) != SET
2857 || ! eligible_for_delay (insn, 0, trial, flags)
2858 || can_throw_internal (trial))
2859 return 0;
2861 dest = SET_DEST (pat), src = SET_SRC (pat);
2862 if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2863 && rtx_equal_p (XEXP (src, 0), dest)
2864 && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2865 && ! side_effects_p (pat))
2867 rtx other = XEXP (src, 1);
2868 rtx new_arith;
2869 rtx ninsn;
2871 /* If this is a constant adjustment, use the same code with
2872 the negated constant. Otherwise, reverse the sense of the
2873 arithmetic. */
2874 if (GET_CODE (other) == CONST_INT)
2875 new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2876 negate_rtx (GET_MODE (src), other));
2877 else
2878 new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2879 GET_MODE (src), dest, other);
2881 ninsn = emit_insn_after (gen_rtx_SET (VOIDmode, dest, new_arith),
2882 insn);
2884 if (recog_memoized (ninsn) < 0
2885 || (extract_insn (ninsn), ! constrain_operands (1)))
2887 delete_related_insns (ninsn);
2888 return 0;
2891 if (own_thread)
2893 update_block (trial, thread);
2894 if (trial == thread)
2896 thread = next_active_insn (thread);
2897 if (new_thread == trial)
2898 new_thread = thread;
2900 delete_related_insns (trial);
2902 else
2903 new_thread = next_active_insn (trial);
2905 ninsn = own_thread ? trial : copy_rtx (trial);
2906 if (thread_if_true)
2907 INSN_FROM_TARGET_P (ninsn) = 1;
2909 delay_list = add_to_delay_list (ninsn, NULL_RTX);
2910 (*pslots_filled)++;
2914 if (delay_list && must_annul)
2915 INSN_ANNULLED_BRANCH_P (insn) = 1;
2917 /* If we are to branch into the middle of this thread, find an appropriate
2918 label or make a new one if none, and redirect INSN to it. If we hit the
2919 end of the function, use the end-of-function label. */
2920 if (new_thread != thread)
2922 rtx label;
2924 if (! thread_if_true)
2925 abort ();
2927 if (new_thread && JUMP_P (new_thread)
2928 && (simplejump_p (new_thread)
2929 || GET_CODE (PATTERN (new_thread)) == RETURN)
2930 && redirect_with_delay_list_safe_p (insn,
2931 JUMP_LABEL (new_thread),
2932 delay_list))
2933 new_thread = follow_jumps (JUMP_LABEL (new_thread));
2935 if (new_thread == 0)
2936 label = find_end_label ();
2937 else if (LABEL_P (new_thread))
2938 label = new_thread;
2939 else
2940 label = get_label_before (new_thread);
2942 if (label)
2943 reorg_redirect_jump (insn, label);
2946 return delay_list;
2949 /* Make another attempt to find insns to place in delay slots.
2951 We previously looked for insns located in front of the delay insn
2952 and, for non-jump delay insns, located behind the delay insn.
2954 Here only try to schedule jump insns and try to move insns from either
2955 the target or the following insns into the delay slot. If annulling is
2956 supported, we will be likely to do this. Otherwise, we can do this only
2957 if safe. */
2959 static void
2960 fill_eager_delay_slots (void)
2962 rtx insn;
2963 int i;
2964 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2966 for (i = 0; i < num_unfilled_slots; i++)
2968 rtx condition;
2969 rtx target_label, insn_at_target, fallthrough_insn;
2970 rtx delay_list = 0;
2971 int own_target;
2972 int own_fallthrough;
2973 int prediction, slots_to_fill, slots_filled;
2975 insn = unfilled_slots_base[i];
2976 if (insn == 0
2977 || INSN_DELETED_P (insn)
2978 || !JUMP_P (insn)
2979 || ! (condjump_p (insn) || condjump_in_parallel_p (insn)))
2980 continue;
2982 slots_to_fill = num_delay_slots (insn);
2983 /* Some machine description have defined instructions to have
2984 delay slots only in certain circumstances which may depend on
2985 nearby insns (which change due to reorg's actions).
2987 For example, the PA port normally has delay slots for unconditional
2988 jumps.
2990 However, the PA port claims such jumps do not have a delay slot
2991 if they are immediate successors of certain CALL_INSNs. This
2992 allows the port to favor filling the delay slot of the call with
2993 the unconditional jump. */
2994 if (slots_to_fill == 0)
2995 continue;
2997 slots_filled = 0;
2998 target_label = JUMP_LABEL (insn);
2999 condition = get_branch_condition (insn, target_label);
3001 if (condition == 0)
3002 continue;
3004 /* Get the next active fallthrough and target insns and see if we own
3005 them. Then see whether the branch is likely true. We don't need
3006 to do a lot of this for unconditional branches. */
3008 insn_at_target = next_active_insn (target_label);
3009 own_target = own_thread_p (target_label, target_label, 0);
3011 if (condition == const_true_rtx)
3013 own_fallthrough = 0;
3014 fallthrough_insn = 0;
3015 prediction = 2;
3017 else
3019 fallthrough_insn = next_active_insn (insn);
3020 own_fallthrough = own_thread_p (NEXT_INSN (insn), NULL_RTX, 1);
3021 prediction = mostly_true_jump (insn, condition);
3024 /* If this insn is expected to branch, first try to get insns from our
3025 target, then our fallthrough insns. If it is not expected to branch,
3026 try the other order. */
3028 if (prediction > 0)
3030 delay_list
3031 = fill_slots_from_thread (insn, condition, insn_at_target,
3032 fallthrough_insn, prediction == 2, 1,
3033 own_target,
3034 slots_to_fill, &slots_filled, delay_list);
3036 if (delay_list == 0 && own_fallthrough)
3038 /* Even though we didn't find anything for delay slots,
3039 we might have found a redundant insn which we deleted
3040 from the thread that was filled. So we have to recompute
3041 the next insn at the target. */
3042 target_label = JUMP_LABEL (insn);
3043 insn_at_target = next_active_insn (target_label);
3045 delay_list
3046 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3047 insn_at_target, 0, 0,
3048 own_fallthrough,
3049 slots_to_fill, &slots_filled,
3050 delay_list);
3053 else
3055 if (own_fallthrough)
3056 delay_list
3057 = fill_slots_from_thread (insn, condition, fallthrough_insn,
3058 insn_at_target, 0, 0,
3059 own_fallthrough,
3060 slots_to_fill, &slots_filled,
3061 delay_list);
3063 if (delay_list == 0)
3064 delay_list
3065 = fill_slots_from_thread (insn, condition, insn_at_target,
3066 next_active_insn (insn), 0, 1,
3067 own_target,
3068 slots_to_fill, &slots_filled,
3069 delay_list);
3072 if (delay_list)
3073 unfilled_slots_base[i]
3074 = emit_delay_sequence (insn, delay_list, slots_filled);
3076 if (slots_to_fill == slots_filled)
3077 unfilled_slots_base[i] = 0;
3079 note_delay_statistics (slots_filled, 1);
3083 /* Once we have tried two ways to fill a delay slot, make a pass over the
3084 code to try to improve the results and to do such things as more jump
3085 threading. */
3087 static void
3088 relax_delay_slots (rtx first)
3090 rtx insn, next, pat;
3091 rtx trial, delay_insn, target_label;
3093 /* Look at every JUMP_INSN and see if we can improve it. */
3094 for (insn = first; insn; insn = next)
3096 rtx other;
3098 next = next_active_insn (insn);
3100 /* If this is a jump insn, see if it now jumps to a jump, jumps to
3101 the next insn, or jumps to a label that is not the last of a
3102 group of consecutive labels. */
3103 if (JUMP_P (insn)
3104 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3105 && (target_label = JUMP_LABEL (insn)) != 0)
3107 target_label = skip_consecutive_labels (follow_jumps (target_label));
3108 if (target_label == 0)
3109 target_label = find_end_label ();
3111 if (target_label && next_active_insn (target_label) == next
3112 && ! condjump_in_parallel_p (insn))
3114 delete_jump (insn);
3115 continue;
3118 if (target_label && target_label != JUMP_LABEL (insn))
3119 reorg_redirect_jump (insn, target_label);
3121 /* See if this jump branches around an unconditional jump.
3122 If so, invert this jump and point it to the target of the
3123 second jump. */
3124 if (next && JUMP_P (next)
3125 && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3126 && target_label
3127 && next_active_insn (target_label) == next_active_insn (next)
3128 && no_labels_between_p (insn, next))
3130 rtx label = JUMP_LABEL (next);
3132 /* Be careful how we do this to avoid deleting code or
3133 labels that are momentarily dead. See similar optimization
3134 in jump.c.
3136 We also need to ensure we properly handle the case when
3137 invert_jump fails. */
3139 ++LABEL_NUSES (target_label);
3140 if (label)
3141 ++LABEL_NUSES (label);
3143 if (invert_jump (insn, label, 1))
3145 delete_related_insns (next);
3146 next = insn;
3149 if (label)
3150 --LABEL_NUSES (label);
3152 if (--LABEL_NUSES (target_label) == 0)
3153 delete_related_insns (target_label);
3155 continue;
3159 /* If this is an unconditional jump and the previous insn is a
3160 conditional jump, try reversing the condition of the previous
3161 insn and swapping our targets. The next pass might be able to
3162 fill the slots.
3164 Don't do this if we expect the conditional branch to be true, because
3165 we would then be making the more common case longer. */
3167 if (JUMP_P (insn)
3168 && (simplejump_p (insn) || GET_CODE (PATTERN (insn)) == RETURN)
3169 && (other = prev_active_insn (insn)) != 0
3170 && (condjump_p (other) || condjump_in_parallel_p (other))
3171 && no_labels_between_p (other, insn)
3172 && 0 > mostly_true_jump (other,
3173 get_branch_condition (other,
3174 JUMP_LABEL (other))))
3176 rtx other_target = JUMP_LABEL (other);
3177 target_label = JUMP_LABEL (insn);
3179 if (invert_jump (other, target_label, 0))
3180 reorg_redirect_jump (insn, other_target);
3183 /* Now look only at cases where we have filled a delay slot. */
3184 if (!NONJUMP_INSN_P (insn)
3185 || GET_CODE (PATTERN (insn)) != SEQUENCE)
3186 continue;
3188 pat = PATTERN (insn);
3189 delay_insn = XVECEXP (pat, 0, 0);
3191 /* See if the first insn in the delay slot is redundant with some
3192 previous insn. Remove it from the delay slot if so; then set up
3193 to reprocess this insn. */
3194 if (redundant_insn (XVECEXP (pat, 0, 1), delay_insn, 0))
3196 delete_from_delay_slot (XVECEXP (pat, 0, 1));
3197 next = prev_active_insn (next);
3198 continue;
3201 /* See if we have a RETURN insn with a filled delay slot followed
3202 by a RETURN insn with an unfilled a delay slot. If so, we can delete
3203 the first RETURN (but not its delay insn). This gives the same
3204 effect in fewer instructions.
3206 Only do so if optimizing for size since this results in slower, but
3207 smaller code. */
3208 if (optimize_size
3209 && GET_CODE (PATTERN (delay_insn)) == RETURN
3210 && next
3211 && JUMP_P (next)
3212 && GET_CODE (PATTERN (next)) == RETURN)
3214 rtx after;
3215 int i;
3217 /* Delete the RETURN and just execute the delay list insns.
3219 We do this by deleting the INSN containing the SEQUENCE, then
3220 re-emitting the insns separately, and then deleting the RETURN.
3221 This allows the count of the jump target to be properly
3222 decremented. */
3224 /* Clear the from target bit, since these insns are no longer
3225 in delay slots. */
3226 for (i = 0; i < XVECLEN (pat, 0); i++)
3227 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3229 trial = PREV_INSN (insn);
3230 delete_related_insns (insn);
3231 if (GET_CODE (pat) != SEQUENCE)
3232 abort ();
3233 after = trial;
3234 for (i = 0; i < XVECLEN (pat, 0); i++)
3236 rtx this_insn = XVECEXP (pat, 0, i);
3237 add_insn_after (this_insn, after);
3238 after = this_insn;
3240 delete_scheduled_jump (delay_insn);
3241 continue;
3244 /* Now look only at the cases where we have a filled JUMP_INSN. */
3245 if (!JUMP_P (XVECEXP (PATTERN (insn), 0, 0))
3246 || ! (condjump_p (XVECEXP (PATTERN (insn), 0, 0))
3247 || condjump_in_parallel_p (XVECEXP (PATTERN (insn), 0, 0))))
3248 continue;
3250 target_label = JUMP_LABEL (delay_insn);
3252 if (target_label)
3254 /* If this jump goes to another unconditional jump, thread it, but
3255 don't convert a jump into a RETURN here. */
3256 trial = skip_consecutive_labels (follow_jumps (target_label));
3257 if (trial == 0)
3258 trial = find_end_label ();
3260 if (trial && trial != target_label
3261 && redirect_with_delay_slots_safe_p (delay_insn, trial, insn))
3263 reorg_redirect_jump (delay_insn, trial);
3264 target_label = trial;
3267 /* If the first insn at TARGET_LABEL is redundant with a previous
3268 insn, redirect the jump to the following insn process again. */
3269 trial = next_active_insn (target_label);
3270 if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3271 && redundant_insn (trial, insn, 0)
3272 && ! can_throw_internal (trial))
3274 /* Figure out where to emit the special USE insn so we don't
3275 later incorrectly compute register live/death info. */
3276 rtx tmp = next_active_insn (trial);
3277 if (tmp == 0)
3278 tmp = find_end_label ();
3280 if (tmp)
3282 /* Insert the special USE insn and update dataflow info. */
3283 update_block (trial, tmp);
3285 /* Now emit a label before the special USE insn, and
3286 redirect our jump to the new label. */
3287 target_label = get_label_before (PREV_INSN (tmp));
3288 reorg_redirect_jump (delay_insn, target_label);
3289 next = insn;
3290 continue;
3294 /* Similarly, if it is an unconditional jump with one insn in its
3295 delay list and that insn is redundant, thread the jump. */
3296 if (trial && GET_CODE (PATTERN (trial)) == SEQUENCE
3297 && XVECLEN (PATTERN (trial), 0) == 2
3298 && JUMP_P (XVECEXP (PATTERN (trial), 0, 0))
3299 && (simplejump_p (XVECEXP (PATTERN (trial), 0, 0))
3300 || GET_CODE (PATTERN (XVECEXP (PATTERN (trial), 0, 0))) == RETURN)
3301 && redundant_insn (XVECEXP (PATTERN (trial), 0, 1), insn, 0))
3303 target_label = JUMP_LABEL (XVECEXP (PATTERN (trial), 0, 0));
3304 if (target_label == 0)
3305 target_label = find_end_label ();
3307 if (target_label
3308 && redirect_with_delay_slots_safe_p (delay_insn, target_label,
3309 insn))
3311 reorg_redirect_jump (delay_insn, target_label);
3312 next = insn;
3313 continue;
3318 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3319 && prev_active_insn (target_label) == insn
3320 && ! condjump_in_parallel_p (delay_insn)
3321 #ifdef HAVE_cc0
3322 /* If the last insn in the delay slot sets CC0 for some insn,
3323 various code assumes that it is in a delay slot. We could
3324 put it back where it belonged and delete the register notes,
3325 but it doesn't seem worthwhile in this uncommon case. */
3326 && ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3327 REG_CC_USER, NULL_RTX)
3328 #endif
3331 rtx after;
3332 int i;
3334 /* All this insn does is execute its delay list and jump to the
3335 following insn. So delete the jump and just execute the delay
3336 list insns.
3338 We do this by deleting the INSN containing the SEQUENCE, then
3339 re-emitting the insns separately, and then deleting the jump.
3340 This allows the count of the jump target to be properly
3341 decremented. */
3343 /* Clear the from target bit, since these insns are no longer
3344 in delay slots. */
3345 for (i = 0; i < XVECLEN (pat, 0); i++)
3346 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3348 trial = PREV_INSN (insn);
3349 delete_related_insns (insn);
3350 if (GET_CODE (pat) != SEQUENCE)
3351 abort ();
3352 after = trial;
3353 for (i = 0; i < XVECLEN (pat, 0); i++)
3355 rtx this_insn = XVECEXP (pat, 0, i);
3356 add_insn_after (this_insn, after);
3357 after = this_insn;
3359 delete_scheduled_jump (delay_insn);
3360 continue;
3363 /* See if this is an unconditional jump around a single insn which is
3364 identical to the one in its delay slot. In this case, we can just
3365 delete the branch and the insn in its delay slot. */
3366 if (next && NONJUMP_INSN_P (next)
3367 && prev_label (next_active_insn (next)) == target_label
3368 && simplejump_p (insn)
3369 && XVECLEN (pat, 0) == 2
3370 && rtx_equal_p (PATTERN (next), PATTERN (XVECEXP (pat, 0, 1))))
3372 delete_related_insns (insn);
3373 continue;
3376 /* See if this jump (with its delay slots) branches around another
3377 jump (without delay slots). If so, invert this jump and point
3378 it to the target of the second jump. We cannot do this for
3379 annulled jumps, though. Again, don't convert a jump to a RETURN
3380 here. */
3381 if (! INSN_ANNULLED_BRANCH_P (delay_insn)
3382 && next && JUMP_P (next)
3383 && (simplejump_p (next) || GET_CODE (PATTERN (next)) == RETURN)
3384 && next_active_insn (target_label) == next_active_insn (next)
3385 && no_labels_between_p (insn, next))
3387 rtx label = JUMP_LABEL (next);
3388 rtx old_label = JUMP_LABEL (delay_insn);
3390 if (label == 0)
3391 label = find_end_label ();
3393 /* find_end_label can generate a new label. Check this first. */
3394 if (label
3395 && no_labels_between_p (insn, next)
3396 && redirect_with_delay_slots_safe_p (delay_insn, label, insn))
3398 /* Be careful how we do this to avoid deleting code or labels
3399 that are momentarily dead. See similar optimization in
3400 jump.c */
3401 if (old_label)
3402 ++LABEL_NUSES (old_label);
3404 if (invert_jump (delay_insn, label, 1))
3406 int i;
3408 /* Must update the INSN_FROM_TARGET_P bits now that
3409 the branch is reversed, so that mark_target_live_regs
3410 will handle the delay slot insn correctly. */
3411 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
3413 rtx slot = XVECEXP (PATTERN (insn), 0, i);
3414 INSN_FROM_TARGET_P (slot) = ! INSN_FROM_TARGET_P (slot);
3417 delete_related_insns (next);
3418 next = insn;
3421 if (old_label && --LABEL_NUSES (old_label) == 0)
3422 delete_related_insns (old_label);
3423 continue;
3427 /* If we own the thread opposite the way this insn branches, see if we
3428 can merge its delay slots with following insns. */
3429 if (INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3430 && own_thread_p (NEXT_INSN (insn), 0, 1))
3431 try_merge_delay_insns (insn, next);
3432 else if (! INSN_FROM_TARGET_P (XVECEXP (pat, 0, 1))
3433 && own_thread_p (target_label, target_label, 0))
3434 try_merge_delay_insns (insn, next_active_insn (target_label));
3436 /* If we get here, we haven't deleted INSN. But we may have deleted
3437 NEXT, so recompute it. */
3438 next = next_active_insn (insn);
3442 #ifdef HAVE_return
3444 /* Look for filled jumps to the end of function label. We can try to convert
3445 them into RETURN insns if the insns in the delay slot are valid for the
3446 RETURN as well. */
3448 static void
3449 make_return_insns (rtx first)
3451 rtx insn, jump_insn, pat;
3452 rtx real_return_label = end_of_function_label;
3453 int slots, i;
3455 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3456 /* If a previous pass filled delay slots in the epilogue, things get a
3457 bit more complicated, as those filler insns would generally (without
3458 data flow analysis) have to be executed after any existing branch
3459 delay slot filler insns. It is also unknown whether such a
3460 transformation would actually be profitable. Note that the existing
3461 code only cares for branches with (some) filled delay slots. */
3462 if (current_function_epilogue_delay_list != NULL)
3463 return;
3464 #endif
3466 /* See if there is a RETURN insn in the function other than the one we
3467 made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3468 into a RETURN to jump to it. */
3469 for (insn = first; insn; insn = NEXT_INSN (insn))
3470 if (JUMP_P (insn) && GET_CODE (PATTERN (insn)) == RETURN)
3472 real_return_label = get_label_before (insn);
3473 break;
3476 /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3477 was equal to END_OF_FUNCTION_LABEL. */
3478 LABEL_NUSES (real_return_label)++;
3480 /* Clear the list of insns to fill so we can use it. */
3481 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3483 for (insn = first; insn; insn = NEXT_INSN (insn))
3485 int flags;
3487 /* Only look at filled JUMP_INSNs that go to the end of function
3488 label. */
3489 if (!NONJUMP_INSN_P (insn)
3490 || GET_CODE (PATTERN (insn)) != SEQUENCE
3491 || !JUMP_P (XVECEXP (PATTERN (insn), 0, 0))
3492 || JUMP_LABEL (XVECEXP (PATTERN (insn), 0, 0)) != end_of_function_label)
3493 continue;
3495 pat = PATTERN (insn);
3496 jump_insn = XVECEXP (pat, 0, 0);
3498 /* If we can't make the jump into a RETURN, try to redirect it to the best
3499 RETURN and go on to the next insn. */
3500 if (! reorg_redirect_jump (jump_insn, NULL_RTX))
3502 /* Make sure redirecting the jump will not invalidate the delay
3503 slot insns. */
3504 if (redirect_with_delay_slots_safe_p (jump_insn,
3505 real_return_label,
3506 insn))
3507 reorg_redirect_jump (jump_insn, real_return_label);
3508 continue;
3511 /* See if this RETURN can accept the insns current in its delay slot.
3512 It can if it has more or an equal number of slots and the contents
3513 of each is valid. */
3515 flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3516 slots = num_delay_slots (jump_insn);
3517 if (slots >= XVECLEN (pat, 0) - 1)
3519 for (i = 1; i < XVECLEN (pat, 0); i++)
3520 if (! (
3521 #ifdef ANNUL_IFFALSE_SLOTS
3522 (INSN_ANNULLED_BRANCH_P (jump_insn)
3523 && INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3524 ? eligible_for_annul_false (jump_insn, i - 1,
3525 XVECEXP (pat, 0, i), flags) :
3526 #endif
3527 #ifdef ANNUL_IFTRUE_SLOTS
3528 (INSN_ANNULLED_BRANCH_P (jump_insn)
3529 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
3530 ? eligible_for_annul_true (jump_insn, i - 1,
3531 XVECEXP (pat, 0, i), flags) :
3532 #endif
3533 eligible_for_delay (jump_insn, i - 1,
3534 XVECEXP (pat, 0, i), flags)))
3535 break;
3537 else
3538 i = 0;
3540 if (i == XVECLEN (pat, 0))
3541 continue;
3543 /* We have to do something with this insn. If it is an unconditional
3544 RETURN, delete the SEQUENCE and output the individual insns,
3545 followed by the RETURN. Then set things up so we try to find
3546 insns for its delay slots, if it needs some. */
3547 if (GET_CODE (PATTERN (jump_insn)) == RETURN)
3549 rtx prev = PREV_INSN (insn);
3551 delete_related_insns (insn);
3552 for (i = 1; i < XVECLEN (pat, 0); i++)
3553 prev = emit_insn_after (PATTERN (XVECEXP (pat, 0, i)), prev);
3555 insn = emit_jump_insn_after (PATTERN (jump_insn), prev);
3556 emit_barrier_after (insn);
3558 if (slots)
3559 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3561 else
3562 /* It is probably more efficient to keep this with its current
3563 delay slot as a branch to a RETURN. */
3564 reorg_redirect_jump (jump_insn, real_return_label);
3567 /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3568 new delay slots we have created. */
3569 if (--LABEL_NUSES (real_return_label) == 0)
3570 delete_related_insns (real_return_label);
3572 fill_simple_delay_slots (1);
3573 fill_simple_delay_slots (0);
3575 #endif
3577 /* Try to find insns to place in delay slots. */
3579 void
3580 dbr_schedule (rtx first, FILE *file)
3582 rtx insn, next, epilogue_insn = 0;
3583 int i;
3584 #if 0
3585 int old_flag_no_peephole = flag_no_peephole;
3587 /* Execute `final' once in prescan mode to delete any insns that won't be
3588 used. Don't let final try to do any peephole optimization--it will
3589 ruin dataflow information for this pass. */
3591 flag_no_peephole = 1;
3592 final (first, 0, NO_DEBUG, 1, 1);
3593 flag_no_peephole = old_flag_no_peephole;
3594 #endif
3596 /* If the current function has no insns other than the prologue and
3597 epilogue, then do not try to fill any delay slots. */
3598 if (n_basic_blocks == 0)
3599 return;
3601 /* Find the highest INSN_UID and allocate and initialize our map from
3602 INSN_UID's to position in code. */
3603 for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3605 if (INSN_UID (insn) > max_uid)
3606 max_uid = INSN_UID (insn);
3607 if (NOTE_P (insn)
3608 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
3609 epilogue_insn = insn;
3612 uid_to_ruid = xmalloc ((max_uid + 1) * sizeof (int));
3613 for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3614 uid_to_ruid[INSN_UID (insn)] = i;
3616 /* Initialize the list of insns that need filling. */
3617 if (unfilled_firstobj == 0)
3619 gcc_obstack_init (&unfilled_slots_obstack);
3620 unfilled_firstobj = obstack_alloc (&unfilled_slots_obstack, 0);
3623 for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3625 rtx target;
3627 INSN_ANNULLED_BRANCH_P (insn) = 0;
3628 INSN_FROM_TARGET_P (insn) = 0;
3630 /* Skip vector tables. We can't get attributes for them. */
3631 if (JUMP_P (insn)
3632 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
3633 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
3634 continue;
3636 if (num_delay_slots (insn) > 0)
3637 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3639 /* Ensure all jumps go to the last of a set of consecutive labels. */
3640 if (JUMP_P (insn)
3641 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3642 && JUMP_LABEL (insn) != 0
3643 && ((target = skip_consecutive_labels (JUMP_LABEL (insn)))
3644 != JUMP_LABEL (insn)))
3645 redirect_jump (insn, target, 1);
3648 init_resource_info (epilogue_insn);
3650 /* Show we haven't computed an end-of-function label yet. */
3651 end_of_function_label = 0;
3653 /* Initialize the statistics for this function. */
3654 memset (num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3655 memset (num_filled_delays, 0, sizeof num_filled_delays);
3657 /* Now do the delay slot filling. Try everything twice in case earlier
3658 changes make more slots fillable. */
3660 for (reorg_pass_number = 0;
3661 reorg_pass_number < MAX_REORG_PASSES;
3662 reorg_pass_number++)
3664 fill_simple_delay_slots (1);
3665 fill_simple_delay_slots (0);
3666 fill_eager_delay_slots ();
3667 relax_delay_slots (first);
3670 /* Delete any USE insns made by update_block; subsequent passes don't need
3671 them or know how to deal with them. */
3672 for (insn = first; insn; insn = next)
3674 next = NEXT_INSN (insn);
3676 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
3677 && INSN_P (XEXP (PATTERN (insn), 0)))
3678 next = delete_related_insns (insn);
3681 /* If we made an end of function label, indicate that it is now
3682 safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3683 If it is now unused, delete it. */
3684 if (end_of_function_label && --LABEL_NUSES (end_of_function_label) == 0)
3685 delete_related_insns (end_of_function_label);
3687 #ifdef HAVE_return
3688 if (HAVE_return && end_of_function_label != 0)
3689 make_return_insns (first);
3690 #endif
3692 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3694 /* It is not clear why the line below is needed, but it does seem to be. */
3695 unfilled_firstobj = obstack_alloc (&unfilled_slots_obstack, 0);
3697 if (file)
3699 int i, j, need_comma;
3700 int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3701 int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
3703 for (reorg_pass_number = 0;
3704 reorg_pass_number < MAX_REORG_PASSES;
3705 reorg_pass_number++)
3707 fprintf (file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
3708 for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
3710 need_comma = 0;
3711 fprintf (file, ";; Reorg function #%d\n", i);
3713 fprintf (file, ";; %d insns needing delay slots\n;; ",
3714 num_insns_needing_delays[i][reorg_pass_number]);
3716 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3717 if (num_filled_delays[i][j][reorg_pass_number])
3719 if (need_comma)
3720 fprintf (file, ", ");
3721 need_comma = 1;
3722 fprintf (file, "%d got %d delays",
3723 num_filled_delays[i][j][reorg_pass_number], j);
3725 fprintf (file, "\n");
3728 memset (total_delay_slots, 0, sizeof total_delay_slots);
3729 memset (total_annul_slots, 0, sizeof total_annul_slots);
3730 for (insn = first; insn; insn = NEXT_INSN (insn))
3732 if (! INSN_DELETED_P (insn)
3733 && NONJUMP_INSN_P (insn)
3734 && GET_CODE (PATTERN (insn)) != USE
3735 && GET_CODE (PATTERN (insn)) != CLOBBER)
3737 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3739 j = XVECLEN (PATTERN (insn), 0) - 1;
3740 if (j > MAX_DELAY_HISTOGRAM)
3741 j = MAX_DELAY_HISTOGRAM;
3742 if (INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (insn), 0, 0)))
3743 total_annul_slots[j]++;
3744 else
3745 total_delay_slots[j]++;
3747 else if (num_delay_slots (insn) > 0)
3748 total_delay_slots[0]++;
3751 fprintf (file, ";; Reorg totals: ");
3752 need_comma = 0;
3753 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3755 if (total_delay_slots[j])
3757 if (need_comma)
3758 fprintf (file, ", ");
3759 need_comma = 1;
3760 fprintf (file, "%d got %d delays", total_delay_slots[j], j);
3763 fprintf (file, "\n");
3764 #if defined (ANNUL_IFTRUE_SLOTS) || defined (ANNUL_IFFALSE_SLOTS)
3765 fprintf (file, ";; Reorg annuls: ");
3766 need_comma = 0;
3767 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3769 if (total_annul_slots[j])
3771 if (need_comma)
3772 fprintf (file, ", ");
3773 need_comma = 1;
3774 fprintf (file, "%d got %d delays", total_annul_slots[j], j);
3777 fprintf (file, "\n");
3778 #endif
3779 fprintf (file, "\n");
3782 /* For all JUMP insns, fill in branch prediction notes, so that during
3783 assembler output a target can set branch prediction bits in the code.
3784 We have to do this now, as up until this point the destinations of
3785 JUMPS can be moved around and changed, but past right here that cannot
3786 happen. */
3787 for (insn = first; insn; insn = NEXT_INSN (insn))
3789 int pred_flags;
3791 if (NONJUMP_INSN_P (insn))
3793 rtx pat = PATTERN (insn);
3795 if (GET_CODE (pat) == SEQUENCE)
3796 insn = XVECEXP (pat, 0, 0);
3798 if (!JUMP_P (insn))
3799 continue;
3801 pred_flags = get_jump_flags (insn, JUMP_LABEL (insn));
3802 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_BR_PRED,
3803 GEN_INT (pred_flags),
3804 REG_NOTES (insn));
3806 free_resource_info ();
3807 free (uid_to_ruid);
3808 #ifdef DELAY_SLOTS_FOR_EPILOGUE
3809 /* SPARC assembler, for instance, emit warning when debug info is output
3810 into the delay slot. */
3812 rtx link;
3814 for (link = current_function_epilogue_delay_list;
3815 link;
3816 link = XEXP (link, 1))
3817 INSN_LOCATOR (XEXP (link, 0)) = 0;
3819 #endif
3821 #endif /* DELAY_SLOTS */