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