2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / reorg.c
blobbca5c60ca7be67ab34cb697eba3d26f4ab7d48c7
1 /* Perform instruction reorganizations for delay slot filling.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu).
4 Hacked by Michael Tiemann (tiemann@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Instruction reorganization pass.
24 This pass runs after register allocation and final jump
25 optimization. It should be the last pass to run before peephole.
26 It serves primarily to fill delay slots of insns, typically branch
27 and call insns. Other insns typically involve more complicated
28 interactions of data dependencies and resource constraints, and
29 are better handled by scheduling before register allocation (by the
30 function `schedule_insns').
32 The Branch Penalty is the number of extra cycles that are needed to
33 execute a branch insn. On an ideal machine, branches take a single
34 cycle, and the Branch Penalty is 0. Several RISC machines approach
35 branch delays differently:
37 The MIPS has a single branch delay slot. Most insns
38 (except other branches) can be used to fill this slot. When the
39 slot is filled, two insns execute in two cycles, reducing the
40 branch penalty to zero.
42 The SPARC always has a branch delay slot, but its effects can be
43 annulled when the branch is not taken. This means that failing to
44 find other sources of insns, we can hoist an insn from the branch
45 target that would only be safe to execute knowing that the branch
46 is taken.
48 The HP-PA always has a branch delay slot. For unconditional branches
49 its effects can be annulled when the branch is taken. The effects
50 of the delay slot in a conditional branch can be nullified for forward
51 taken branches, or for untaken backward branches. This means
52 we can hoist insns from the fall-through path for forward branches or
53 steal insns from the target of backward branches.
55 The TMS320C3x and C4x have three branch delay slots. When the three
56 slots are filled, the branch penalty is zero. Most insns can fill the
57 delay slots except jump insns.
59 Three techniques for filling delay slots have been implemented so far:
61 (1) `fill_simple_delay_slots' is the simplest, most efficient way
62 to fill delay slots. This pass first looks for insns which come
63 from before the branch and which are safe to execute after the
64 branch. Then it searches after the insn requiring delay slots or,
65 in the case of a branch, for insns that are after the point at
66 which the branch merges into the fallthrough code, if such a point
67 exists. When such insns are found, the branch penalty decreases
68 and no code expansion takes place.
70 (2) `fill_eager_delay_slots' is more complicated: it is used for
71 scheduling conditional jumps, or for scheduling jumps which cannot
72 be filled using (1). A machine need not have annulled jumps to use
73 this strategy, but it helps (by keeping more options open).
74 `fill_eager_delay_slots' tries to guess the direction the branch
75 will go; if it guesses right 100% of the time, it can reduce the
76 branch penalty as much as `fill_simple_delay_slots' does. If it
77 guesses wrong 100% of the time, it might as well schedule nops. When
78 `fill_eager_delay_slots' takes insns from the fall-through path of
79 the jump, usually there is no code expansion; when it takes insns
80 from the branch target, there is code expansion if it is not the
81 only way to reach that target.
83 (3) `relax_delay_slots' uses a set of rules to simplify code that
84 has been reorganized by (1) and (2). It finds cases where
85 conditional test can be eliminated, jumps can be threaded, extra
86 insns can be eliminated, etc. It is the job of (1) and (2) to do a
87 good job of scheduling locally; `relax_delay_slots' takes care of
88 making the various individual schedules work well together. It is
89 especially tuned to handle the control flow interactions of branch
90 insns. It does nothing for insns with delay slots that do not
91 branch.
93 On machines that use CC0, we are very conservative. We will not make
94 a copy of an insn involving CC0 since we want to maintain a 1-1
95 correspondence between the insn that sets and uses CC0. The insns are
96 allowed to be separated by placing an insn that sets CC0 (but not an insn
97 that uses CC0; we could do this, but it doesn't seem worthwhile) in a
98 delay slot. In that case, we point each insn at the other with REG_CC_USER
99 and REG_CC_SETTER notes. Note that these restrictions affect very few
100 machines because most RISC machines with delay slots will not use CC0
101 (the RT is the only known exception at this point). */
103 #include "config.h"
104 #include "system.h"
105 #include "coretypes.h"
106 #include "tm.h"
107 #include "diagnostic-core.h"
108 #include "rtl.h"
109 #include "tm_p.h"
110 #include "symtab.h"
111 #include "hard-reg-set.h"
112 #include "input.h"
113 #include "function.h"
114 #include "flags.h"
115 #include "alias.h"
116 #include "tree.h"
117 #include "insn-config.h"
118 #include "expmed.h"
119 #include "dojump.h"
120 #include "explow.h"
121 #include "calls.h"
122 #include "emit-rtl.h"
123 #include "varasm.h"
124 #include "stmt.h"
125 #include "expr.h"
126 #include "conditions.h"
127 #include "predict.h"
128 #include "dominance.h"
129 #include "cfg.h"
130 #include "basic-block.h"
131 #include "regs.h"
132 #include "recog.h"
133 #include "obstack.h"
134 #include "insn-attr.h"
135 #include "resource.h"
136 #include "except.h"
137 #include "params.h"
138 #include "target.h"
139 #include "tree-pass.h"
141 #ifdef DELAY_SLOTS
143 #ifndef ANNUL_IFTRUE_SLOTS
144 #define eligible_for_annul_true(INSN, SLOTS, TRIAL, FLAGS) 0
145 #endif
146 #ifndef ANNUL_IFFALSE_SLOTS
147 #define eligible_for_annul_false(INSN, SLOTS, TRIAL, FLAGS) 0
148 #endif
151 /* First, some functions that were used before GCC got a control flow graph.
152 These functions are now only used here in reorg.c, and have therefore
153 been moved here to avoid inadvertent misuse elsewhere in the compiler. */
155 /* Return the last label to mark the same position as LABEL. Return LABEL
156 itself if it is null or any return rtx. */
158 static rtx
159 skip_consecutive_labels (rtx label_or_return)
161 rtx_insn *insn;
163 if (label_or_return && ANY_RETURN_P (label_or_return))
164 return label_or_return;
166 rtx_insn *label = as_a <rtx_insn *> (label_or_return);
168 for (insn = label; insn != 0 && !INSN_P (insn); insn = NEXT_INSN (insn))
169 if (LABEL_P (insn))
170 label = insn;
172 return label;
175 /* INSN uses CC0 and is being moved into a delay slot. Set up REG_CC_SETTER
176 and REG_CC_USER notes so we can find it. */
178 static void
179 link_cc0_insns (rtx_insn *insn)
181 rtx user = next_nonnote_insn (insn);
183 if (NONJUMP_INSN_P (user) && GET_CODE (PATTERN (user)) == SEQUENCE)
184 user = XVECEXP (PATTERN (user), 0, 0);
186 add_reg_note (user, REG_CC_SETTER, insn);
187 add_reg_note (insn, REG_CC_USER, user);
190 /* Insns which have delay slots that have not yet been filled. */
192 static struct obstack unfilled_slots_obstack;
193 static rtx *unfilled_firstobj;
195 /* Define macros to refer to the first and last slot containing unfilled
196 insns. These are used because the list may move and its address
197 should be recomputed at each use. */
199 #define unfilled_slots_base \
200 ((rtx_insn **) obstack_base (&unfilled_slots_obstack))
202 #define unfilled_slots_next \
203 ((rtx_insn **) obstack_next_free (&unfilled_slots_obstack))
205 /* Points to the label before the end of the function, or before a
206 return insn. */
207 static rtx_code_label *function_return_label;
208 /* Likewise for a simple_return. */
209 static rtx_code_label *function_simple_return_label;
211 /* Mapping between INSN_UID's and position in the code since INSN_UID's do
212 not always monotonically increase. */
213 static int *uid_to_ruid;
215 /* Highest valid index in `uid_to_ruid'. */
216 static int max_uid;
218 static int stop_search_p (rtx_insn *, int);
219 static int resource_conflicts_p (struct resources *, struct resources *);
220 static int insn_references_resource_p (rtx, struct resources *, bool);
221 static int insn_sets_resource_p (rtx, struct resources *, bool);
222 static rtx_code_label *find_end_label (rtx);
223 static rtx_insn *emit_delay_sequence (rtx_insn *, rtx_insn_list *, int);
224 static rtx_insn_list *add_to_delay_list (rtx_insn *, rtx_insn_list *);
225 static rtx_insn *delete_from_delay_slot (rtx_insn *);
226 static void delete_scheduled_jump (rtx_insn *);
227 static void note_delay_statistics (int, int);
228 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
229 static rtx_insn_list *optimize_skip (rtx_jump_insn *);
230 #endif
231 static int get_jump_flags (const rtx_insn *, rtx);
232 static int mostly_true_jump (rtx);
233 static rtx get_branch_condition (const rtx_insn *, rtx);
234 static int condition_dominates_p (rtx, const rtx_insn *);
235 static int redirect_with_delay_slots_safe_p (rtx_insn *, rtx, rtx);
236 static int redirect_with_delay_list_safe_p (rtx_insn *, rtx, rtx_insn_list *);
237 static int check_annul_list_true_false (int, rtx);
238 static rtx_insn_list *steal_delay_list_from_target (rtx_insn *, rtx,
239 rtx_sequence *,
240 rtx_insn_list *,
241 struct resources *,
242 struct resources *,
243 struct resources *,
244 int, int *, int *,
245 rtx *);
246 static rtx_insn_list *steal_delay_list_from_fallthrough (rtx_insn *, rtx,
247 rtx_sequence *,
248 rtx_insn_list *,
249 struct resources *,
250 struct resources *,
251 struct resources *,
252 int, int *, int *);
253 static void try_merge_delay_insns (rtx_insn *, rtx_insn *);
254 static rtx redundant_insn (rtx, rtx_insn *, rtx);
255 static int own_thread_p (rtx, rtx, int);
256 static void update_block (rtx_insn *, rtx);
257 static int reorg_redirect_jump (rtx_jump_insn *, rtx);
258 static void update_reg_dead_notes (rtx_insn *, rtx_insn *);
259 static void fix_reg_dead_note (rtx, rtx);
260 static void update_reg_unused_notes (rtx, rtx);
261 static void fill_simple_delay_slots (int);
262 static rtx_insn_list *fill_slots_from_thread (rtx_jump_insn *, rtx, rtx, rtx,
263 int, int, int, int,
264 int *, rtx_insn_list *);
265 static void fill_eager_delay_slots (void);
266 static void relax_delay_slots (rtx_insn *);
267 static void make_return_insns (rtx_insn *);
269 /* A wrapper around next_active_insn which takes care to return ret_rtx
270 unchanged. */
272 static rtx
273 first_active_target_insn (rtx insn)
275 if (ANY_RETURN_P (insn))
276 return insn;
277 return next_active_insn (as_a <rtx_insn *> (insn));
280 /* Return true iff INSN is a simplejump, or any kind of return insn. */
282 static bool
283 simplejump_or_return_p (rtx insn)
285 return (JUMP_P (insn)
286 && (simplejump_p (as_a <rtx_insn *> (insn))
287 || ANY_RETURN_P (PATTERN (insn))));
290 /* Return TRUE if this insn should stop the search for insn to fill delay
291 slots. LABELS_P indicates that labels should terminate the search.
292 In all cases, jumps terminate the search. */
294 static int
295 stop_search_p (rtx_insn *insn, int labels_p)
297 if (insn == 0)
298 return 1;
300 /* If the insn can throw an exception that is caught within the function,
301 it may effectively perform a jump from the viewpoint of the function.
302 Therefore act like for a jump. */
303 if (can_throw_internal (insn))
304 return 1;
306 switch (GET_CODE (insn))
308 case NOTE:
309 case CALL_INSN:
310 return 0;
312 case CODE_LABEL:
313 return labels_p;
315 case JUMP_INSN:
316 case BARRIER:
317 return 1;
319 case INSN:
320 /* OK unless it contains a delay slot or is an `asm' insn of some type.
321 We don't know anything about these. */
322 return (GET_CODE (PATTERN (insn)) == SEQUENCE
323 || GET_CODE (PATTERN (insn)) == ASM_INPUT
324 || asm_noperands (PATTERN (insn)) >= 0);
326 default:
327 gcc_unreachable ();
331 /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
332 resource set contains a volatile memory reference. Otherwise, return FALSE. */
334 static int
335 resource_conflicts_p (struct resources *res1, struct resources *res2)
337 if ((res1->cc && res2->cc) || (res1->memory && res2->memory)
338 || res1->volatil || res2->volatil)
339 return 1;
341 return hard_reg_set_intersect_p (res1->regs, res2->regs);
344 /* Return TRUE if any resource marked in RES, a `struct resources', is
345 referenced by INSN. If INCLUDE_DELAYED_EFFECTS is set, return if the called
346 routine is using those resources.
348 We compute this by computing all the resources referenced by INSN and
349 seeing if this conflicts with RES. It might be faster to directly check
350 ourselves, and this is the way it used to work, but it means duplicating
351 a large block of complex code. */
353 static int
354 insn_references_resource_p (rtx insn, struct resources *res,
355 bool include_delayed_effects)
357 struct resources insn_res;
359 CLEAR_RESOURCE (&insn_res);
360 mark_referenced_resources (insn, &insn_res, include_delayed_effects);
361 return resource_conflicts_p (&insn_res, res);
364 /* Return TRUE if INSN modifies resources that are marked in RES.
365 INCLUDE_DELAYED_EFFECTS is set if the actions of that routine should be
366 included. CC0 is only modified if it is explicitly set; see comments
367 in front of mark_set_resources for details. */
369 static int
370 insn_sets_resource_p (rtx insn, struct resources *res,
371 bool include_delayed_effects)
373 struct resources insn_sets;
375 CLEAR_RESOURCE (&insn_sets);
376 mark_set_resources (insn, &insn_sets, 0,
377 (include_delayed_effects
378 ? MARK_SRC_DEST_CALL
379 : MARK_SRC_DEST));
380 return resource_conflicts_p (&insn_sets, res);
383 /* Find a label at the end of the function or before a RETURN. If there
384 is none, try to make one. If that fails, returns 0.
386 The property of such a label is that it is placed just before the
387 epilogue or a bare RETURN insn, so that another bare RETURN can be
388 turned into a jump to the label unconditionally. In particular, the
389 label cannot be placed before a RETURN insn with a filled delay slot.
391 ??? There may be a problem with the current implementation. Suppose
392 we start with a bare RETURN insn and call find_end_label. It may set
393 function_return_label just before the RETURN. Suppose the machinery
394 is able to fill the delay slot of the RETURN insn afterwards. Then
395 function_return_label is no longer valid according to the property
396 described above and find_end_label will still return it unmodified.
397 Note that this is probably mitigated by the following observation:
398 once function_return_label is made, it is very likely the target of
399 a jump, so filling the delay slot of the RETURN will be much more
400 difficult.
401 KIND is either simple_return_rtx or ret_rtx, indicating which type of
402 return we're looking for. */
404 static rtx_code_label *
405 find_end_label (rtx kind)
407 rtx_insn *insn;
408 rtx_code_label **plabel;
410 if (kind == ret_rtx)
411 plabel = &function_return_label;
412 else
414 gcc_assert (kind == simple_return_rtx);
415 plabel = &function_simple_return_label;
418 /* If we found one previously, return it. */
419 if (*plabel)
420 return *plabel;
422 /* Otherwise, see if there is a label at the end of the function. If there
423 is, it must be that RETURN insns aren't needed, so that is our return
424 label and we don't have to do anything else. */
426 insn = get_last_insn ();
427 while (NOTE_P (insn)
428 || (NONJUMP_INSN_P (insn)
429 && (GET_CODE (PATTERN (insn)) == USE
430 || GET_CODE (PATTERN (insn)) == CLOBBER)))
431 insn = PREV_INSN (insn);
433 /* When a target threads its epilogue we might already have a
434 suitable return insn. If so put a label before it for the
435 function_return_label. */
436 if (BARRIER_P (insn)
437 && JUMP_P (PREV_INSN (insn))
438 && PATTERN (PREV_INSN (insn)) == kind)
440 rtx_insn *temp = PREV_INSN (PREV_INSN (insn));
441 rtx_code_label *label = gen_label_rtx ();
442 LABEL_NUSES (label) = 0;
444 /* Put the label before any USE insns that may precede the RETURN
445 insn. */
446 while (GET_CODE (temp) == USE)
447 temp = PREV_INSN (temp);
449 emit_label_after (label, temp);
450 *plabel = label;
453 else if (LABEL_P (insn))
454 *plabel = as_a <rtx_code_label *> (insn);
455 else
457 rtx_code_label *label = gen_label_rtx ();
458 LABEL_NUSES (label) = 0;
459 /* If the basic block reorder pass moves the return insn to
460 some other place try to locate it again and put our
461 function_return_label there. */
462 while (insn && ! (JUMP_P (insn) && (PATTERN (insn) == kind)))
463 insn = PREV_INSN (insn);
464 if (insn)
466 insn = PREV_INSN (insn);
468 /* Put the label before any USE insns that may precede the
469 RETURN insn. */
470 while (GET_CODE (insn) == USE)
471 insn = PREV_INSN (insn);
473 emit_label_after (label, insn);
475 else
477 if (HAVE_epilogue && ! HAVE_return)
478 /* The RETURN insn has its delay slot filled so we cannot
479 emit the label just before it. Since we already have
480 an epilogue and cannot emit a new RETURN, we cannot
481 emit the label at all. */
482 return NULL;
484 /* Otherwise, make a new label and emit a RETURN and BARRIER,
485 if needed. */
486 emit_label (label);
487 if (HAVE_return)
489 /* The return we make may have delay slots too. */
490 rtx pat = gen_return ();
491 rtx_insn *insn = emit_jump_insn (pat);
492 set_return_jump_label (insn);
493 emit_barrier ();
494 if (num_delay_slots (insn) > 0)
495 obstack_ptr_grow (&unfilled_slots_obstack, insn);
498 *plabel = label;
501 /* Show one additional use for this label so it won't go away until
502 we are done. */
503 ++LABEL_NUSES (*plabel);
505 return *plabel;
508 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
509 the pattern of INSN with the SEQUENCE.
511 Returns the insn containing the SEQUENCE that replaces INSN. */
513 static rtx_insn *
514 emit_delay_sequence (rtx_insn *insn, rtx_insn_list *list, int length)
516 /* Allocate the rtvec to hold the insns and the SEQUENCE. */
517 rtvec seqv = rtvec_alloc (length + 1);
518 rtx seq = gen_rtx_SEQUENCE (VOIDmode, seqv);
519 rtx_insn *seq_insn = make_insn_raw (seq);
521 /* If DELAY_INSN has a location, use it for SEQ_INSN. If DELAY_INSN does
522 not have a location, but one of the delayed insns does, we pick up a
523 location from there later. */
524 INSN_LOCATION (seq_insn) = INSN_LOCATION (insn);
526 /* Unlink INSN from the insn chain, so that we can put it into
527 the SEQUENCE. Remember where we want to emit SEQUENCE in AFTER. */
528 rtx_insn *after = PREV_INSN (insn);
529 remove_insn (insn);
530 SET_NEXT_INSN (insn) = SET_PREV_INSN (insn) = NULL;
532 /* Build our SEQUENCE and rebuild the insn chain. */
533 int i = 1;
534 start_sequence ();
535 XVECEXP (seq, 0, 0) = emit_insn (insn);
536 for (rtx_insn_list *li = list; li; li = li->next (), i++)
538 rtx_insn *tem = li->insn ();
539 rtx note, next;
541 /* Show that this copy of the insn isn't deleted. */
542 tem->set_undeleted ();
544 /* Unlink insn from its original place, and re-emit it into
545 the sequence. */
546 SET_NEXT_INSN (tem) = SET_PREV_INSN (tem) = NULL;
547 XVECEXP (seq, 0, i) = emit_insn (tem);
549 /* SPARC assembler, for instance, emit warning when debug info is output
550 into the delay slot. */
551 if (INSN_LOCATION (tem) && !INSN_LOCATION (seq_insn))
552 INSN_LOCATION (seq_insn) = INSN_LOCATION (tem);
553 INSN_LOCATION (tem) = 0;
555 for (note = REG_NOTES (tem); note; note = next)
557 next = XEXP (note, 1);
558 switch (REG_NOTE_KIND (note))
560 case REG_DEAD:
561 /* Remove any REG_DEAD notes because we can't rely on them now
562 that the insn has been moved. */
563 remove_note (tem, note);
564 break;
566 case REG_LABEL_OPERAND:
567 case REG_LABEL_TARGET:
568 /* Keep the label reference count up to date. */
569 if (LABEL_P (XEXP (note, 0)))
570 LABEL_NUSES (XEXP (note, 0)) ++;
571 break;
573 default:
574 break;
578 end_sequence ();
579 gcc_assert (i == length + 1);
581 /* Splice our SEQUENCE into the insn stream where INSN used to be. */
582 add_insn_after (seq_insn, after, NULL);
584 return seq_insn;
587 /* Add INSN to DELAY_LIST and return the head of the new list. The list must
588 be in the order in which the insns are to be executed. */
590 static rtx_insn_list *
591 add_to_delay_list (rtx_insn *insn, rtx_insn_list *delay_list)
593 /* If we have an empty list, just make a new list element. If
594 INSN has its block number recorded, clear it since we may
595 be moving the insn to a new block. */
597 if (delay_list == 0)
599 clear_hashed_info_for_insn (insn);
600 return gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
603 /* Otherwise this must be an INSN_LIST. Add INSN to the end of the
604 list. */
605 XEXP (delay_list, 1) = add_to_delay_list (insn, delay_list->next ());
607 return delay_list;
610 /* Delete INSN from the delay slot of the insn that it is in, which may
611 produce an insn with no delay slots. Return the new insn. */
613 static rtx_insn *
614 delete_from_delay_slot (rtx_insn *insn)
616 rtx_insn *trial, *seq_insn, *prev;
617 rtx_sequence *seq;
618 rtx_insn_list *delay_list = 0;
619 int i;
620 int had_barrier = 0;
622 /* We first must find the insn containing the SEQUENCE with INSN in its
623 delay slot. Do this by finding an insn, TRIAL, where
624 PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
626 for (trial = insn;
627 PREV_INSN (NEXT_INSN (trial)) == trial;
628 trial = NEXT_INSN (trial))
631 seq_insn = PREV_INSN (NEXT_INSN (trial));
632 seq = as_a <rtx_sequence *> (PATTERN (seq_insn));
634 if (NEXT_INSN (seq_insn) && BARRIER_P (NEXT_INSN (seq_insn)))
635 had_barrier = 1;
637 /* Create a delay list consisting of all the insns other than the one
638 we are deleting (unless we were the only one). */
639 if (seq->len () > 2)
640 for (i = 1; i < seq->len (); i++)
641 if (seq->insn (i) != insn)
642 delay_list = add_to_delay_list (seq->insn (i), delay_list);
644 /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
645 list, and rebuild the delay list if non-empty. */
646 prev = PREV_INSN (seq_insn);
647 trial = seq->insn (0);
648 delete_related_insns (seq_insn);
649 add_insn_after (trial, prev, NULL);
651 /* If there was a barrier after the old SEQUENCE, remit it. */
652 if (had_barrier)
653 emit_barrier_after (trial);
655 /* If there are any delay insns, remit them. Otherwise clear the
656 annul flag. */
657 if (delay_list)
658 trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2);
659 else if (JUMP_P (trial))
660 INSN_ANNULLED_BRANCH_P (trial) = 0;
662 INSN_FROM_TARGET_P (insn) = 0;
664 /* Show we need to fill this insn again. */
665 obstack_ptr_grow (&unfilled_slots_obstack, trial);
667 return trial;
670 /* Delete INSN, a JUMP_INSN. If it is a conditional jump, we must track down
671 the insn that sets CC0 for it and delete it too. */
673 static void
674 delete_scheduled_jump (rtx_insn *insn)
676 /* Delete the insn that sets cc0 for us. On machines without cc0, we could
677 delete the insn that sets the condition code, but it is hard to find it.
678 Since this case is rare anyway, don't bother trying; there would likely
679 be other insns that became dead anyway, which we wouldn't know to
680 delete. */
682 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, insn))
684 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
686 /* If a reg-note was found, it points to an insn to set CC0. This
687 insn is in the delay list of some other insn. So delete it from
688 the delay list it was in. */
689 if (note)
691 if (! FIND_REG_INC_NOTE (XEXP (note, 0), NULL_RTX)
692 && sets_cc0_p (PATTERN (XEXP (note, 0))) == 1)
693 delete_from_delay_slot (as_a <rtx_insn *> (XEXP (note, 0)));
695 else
697 /* The insn setting CC0 is our previous insn, but it may be in
698 a delay slot. It will be the last insn in the delay slot, if
699 it is. */
700 rtx_insn *trial = previous_insn (insn);
701 if (NOTE_P (trial))
702 trial = prev_nonnote_insn (trial);
703 if (sets_cc0_p (PATTERN (trial)) != 1
704 || FIND_REG_INC_NOTE (trial, NULL_RTX))
705 return;
706 if (PREV_INSN (NEXT_INSN (trial)) == trial)
707 delete_related_insns (trial);
708 else
709 delete_from_delay_slot (trial);
713 delete_related_insns (insn);
716 /* Counters for delay-slot filling. */
718 #define NUM_REORG_FUNCTIONS 2
719 #define MAX_DELAY_HISTOGRAM 3
720 #define MAX_REORG_PASSES 2
722 static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
724 static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
726 static int reorg_pass_number;
728 static void
729 note_delay_statistics (int slots_filled, int index)
731 num_insns_needing_delays[index][reorg_pass_number]++;
732 if (slots_filled > MAX_DELAY_HISTOGRAM)
733 slots_filled = MAX_DELAY_HISTOGRAM;
734 num_filled_delays[index][slots_filled][reorg_pass_number]++;
737 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
739 /* Optimize the following cases:
741 1. When a conditional branch skips over only one instruction,
742 use an annulling branch and put that insn in the delay slot.
743 Use either a branch that annuls when the condition if true or
744 invert the test with a branch that annuls when the condition is
745 false. This saves insns, since otherwise we must copy an insn
746 from the L1 target.
748 (orig) (skip) (otherwise)
749 Bcc.n L1 Bcc',a L1 Bcc,a L1'
750 insn insn insn2
751 L1: L1: L1:
752 insn2 insn2 insn2
753 insn3 insn3 L1':
754 insn3
756 2. When a conditional branch skips over only one instruction,
757 and after that, it unconditionally branches somewhere else,
758 perform the similar optimization. This saves executing the
759 second branch in the case where the inverted condition is true.
761 Bcc.n L1 Bcc',a L2
762 insn insn
763 L1: L1:
764 Bra L2 Bra L2
766 INSN is a JUMP_INSN.
768 This should be expanded to skip over N insns, where N is the number
769 of delay slots required. */
771 static rtx_insn_list *
772 optimize_skip (rtx_jump_insn *insn)
774 rtx_insn *trial = next_nonnote_insn (insn);
775 rtx_insn *next_trial = next_active_insn (trial);
776 rtx_insn_list *delay_list = 0;
777 int flags;
779 flags = get_jump_flags (insn, JUMP_LABEL (insn));
781 if (trial == 0
782 || !NONJUMP_INSN_P (trial)
783 || GET_CODE (PATTERN (trial)) == SEQUENCE
784 || recog_memoized (trial) < 0
785 || (! eligible_for_annul_false (insn, 0, trial, flags)
786 && ! eligible_for_annul_true (insn, 0, trial, flags))
787 || can_throw_internal (trial))
788 return 0;
790 /* There are two cases where we are just executing one insn (we assume
791 here that a branch requires only one insn; this should be generalized
792 at some point): Where the branch goes around a single insn or where
793 we have one insn followed by a branch to the same label we branch to.
794 In both of these cases, inverting the jump and annulling the delay
795 slot give the same effect in fewer insns. */
796 if (next_trial == next_active_insn (JUMP_LABEL (insn))
797 || (next_trial != 0
798 && simplejump_or_return_p (next_trial)
799 && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)))
801 if (eligible_for_annul_false (insn, 0, trial, flags))
803 if (invert_jump (insn, JUMP_LABEL (insn), 1))
804 INSN_FROM_TARGET_P (trial) = 1;
805 else if (! eligible_for_annul_true (insn, 0, trial, flags))
806 return 0;
809 delay_list = add_to_delay_list (trial, NULL);
810 next_trial = next_active_insn (trial);
811 update_block (trial, trial);
812 delete_related_insns (trial);
814 /* Also, if we are targeting an unconditional
815 branch, thread our jump to the target of that branch. Don't
816 change this into a RETURN here, because it may not accept what
817 we have in the delay slot. We'll fix this up later. */
818 if (next_trial && simplejump_or_return_p (next_trial))
820 rtx target_label = JUMP_LABEL (next_trial);
821 if (ANY_RETURN_P (target_label))
822 target_label = find_end_label (target_label);
824 if (target_label)
826 /* Recompute the flags based on TARGET_LABEL since threading
827 the jump to TARGET_LABEL may change the direction of the
828 jump (which may change the circumstances in which the
829 delay slot is nullified). */
830 flags = get_jump_flags (insn, target_label);
831 if (eligible_for_annul_true (insn, 0, trial, flags))
832 reorg_redirect_jump (insn, target_label);
836 INSN_ANNULLED_BRANCH_P (insn) = 1;
839 return delay_list;
841 #endif
843 /* Encode and return branch direction and prediction information for
844 INSN assuming it will jump to LABEL.
846 Non conditional branches return no direction information and
847 are predicted as very likely taken. */
849 static int
850 get_jump_flags (const rtx_insn *insn, rtx label)
852 int flags;
854 /* get_jump_flags can be passed any insn with delay slots, these may
855 be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
856 direction information, and only if they are conditional jumps.
858 If LABEL is a return, then there is no way to determine the branch
859 direction. */
860 if (JUMP_P (insn)
861 && (condjump_p (insn) || condjump_in_parallel_p (insn))
862 && !ANY_RETURN_P (label)
863 && INSN_UID (insn) <= max_uid
864 && INSN_UID (label) <= max_uid)
865 flags
866 = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
867 ? ATTR_FLAG_forward : ATTR_FLAG_backward;
868 /* No valid direction information. */
869 else
870 flags = 0;
872 return flags;
875 /* Return truth value of the statement that this branch
876 is mostly taken. If we think that the branch is extremely likely
877 to be taken, we return 2. If the branch is slightly more likely to be
878 taken, return 1. If the branch is slightly less likely to be taken,
879 return 0 and if the branch is highly unlikely to be taken, return -1. */
881 static int
882 mostly_true_jump (rtx jump_insn)
884 /* If branch probabilities are available, then use that number since it
885 always gives a correct answer. */
886 rtx note = find_reg_note (jump_insn, REG_BR_PROB, 0);
887 if (note)
889 int prob = XINT (note, 0);
891 if (prob >= REG_BR_PROB_BASE * 9 / 10)
892 return 2;
893 else if (prob >= REG_BR_PROB_BASE / 2)
894 return 1;
895 else if (prob >= REG_BR_PROB_BASE / 10)
896 return 0;
897 else
898 return -1;
901 /* If there is no note, assume branches are not taken.
902 This should be rare. */
903 return 0;
906 /* Return the condition under which INSN will branch to TARGET. If TARGET
907 is zero, return the condition under which INSN will return. If INSN is
908 an unconditional branch, return const_true_rtx. If INSN isn't a simple
909 type of jump, or it doesn't go to TARGET, return 0. */
911 static rtx
912 get_branch_condition (const rtx_insn *insn, rtx target)
914 rtx pat = PATTERN (insn);
915 rtx src;
917 if (condjump_in_parallel_p (insn))
918 pat = XVECEXP (pat, 0, 0);
920 if (ANY_RETURN_P (pat) && pat == target)
921 return const_true_rtx;
923 if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
924 return 0;
926 src = SET_SRC (pat);
927 if (GET_CODE (src) == LABEL_REF && LABEL_REF_LABEL (src) == target)
928 return const_true_rtx;
930 else if (GET_CODE (src) == IF_THEN_ELSE
931 && XEXP (src, 2) == pc_rtx
932 && ((GET_CODE (XEXP (src, 1)) == LABEL_REF
933 && LABEL_REF_LABEL (XEXP (src, 1)) == target)
934 || (ANY_RETURN_P (XEXP (src, 1)) && XEXP (src, 1) == target)))
935 return XEXP (src, 0);
937 else if (GET_CODE (src) == IF_THEN_ELSE
938 && XEXP (src, 1) == pc_rtx
939 && ((GET_CODE (XEXP (src, 2)) == LABEL_REF
940 && LABEL_REF_LABEL (XEXP (src, 2)) == target)
941 || (ANY_RETURN_P (XEXP (src, 2)) && XEXP (src, 2) == target)))
943 enum rtx_code rev;
944 rev = reversed_comparison_code (XEXP (src, 0), insn);
945 if (rev != UNKNOWN)
946 return gen_rtx_fmt_ee (rev, GET_MODE (XEXP (src, 0)),
947 XEXP (XEXP (src, 0), 0),
948 XEXP (XEXP (src, 0), 1));
951 return 0;
954 /* Return nonzero if CONDITION is more strict than the condition of
955 INSN, i.e., if INSN will always branch if CONDITION is true. */
957 static int
958 condition_dominates_p (rtx condition, const rtx_insn *insn)
960 rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
961 enum rtx_code code = GET_CODE (condition);
962 enum rtx_code other_code;
964 if (rtx_equal_p (condition, other_condition)
965 || other_condition == const_true_rtx)
966 return 1;
968 else if (condition == const_true_rtx || other_condition == 0)
969 return 0;
971 other_code = GET_CODE (other_condition);
972 if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
973 || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
974 || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
975 return 0;
977 return comparison_dominates_p (code, other_code);
980 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
981 any insns already in the delay slot of JUMP. */
983 static int
984 redirect_with_delay_slots_safe_p (rtx_insn *jump, rtx newlabel, rtx seq)
986 int flags, i;
987 rtx_sequence *pat = as_a <rtx_sequence *> (PATTERN (seq));
989 /* Make sure all the delay slots of this jump would still
990 be valid after threading the jump. If they are still
991 valid, then return nonzero. */
993 flags = get_jump_flags (jump, newlabel);
994 for (i = 1; i < pat->len (); i++)
995 if (! (
996 #ifdef ANNUL_IFFALSE_SLOTS
997 (INSN_ANNULLED_BRANCH_P (jump)
998 && INSN_FROM_TARGET_P (pat->insn (i)))
999 ? eligible_for_annul_false (jump, i - 1, pat->insn (i), flags) :
1000 #endif
1001 #ifdef ANNUL_IFTRUE_SLOTS
1002 (INSN_ANNULLED_BRANCH_P (jump)
1003 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
1004 ? eligible_for_annul_true (jump, i - 1, pat->insn (i), flags) :
1005 #endif
1006 eligible_for_delay (jump, i - 1, pat->insn (i), flags)))
1007 break;
1009 return (i == pat->len ());
1012 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
1013 any insns we wish to place in the delay slot of JUMP. */
1015 static int
1016 redirect_with_delay_list_safe_p (rtx_insn *jump, rtx newlabel,
1017 rtx_insn_list *delay_list)
1019 int flags, i;
1020 rtx_insn_list *li;
1022 /* Make sure all the insns in DELAY_LIST would still be
1023 valid after threading the jump. If they are still
1024 valid, then return nonzero. */
1026 flags = get_jump_flags (jump, newlabel);
1027 for (li = delay_list, i = 0; li; li = li->next (), i++)
1028 if (! (
1029 #ifdef ANNUL_IFFALSE_SLOTS
1030 (INSN_ANNULLED_BRANCH_P (jump)
1031 && INSN_FROM_TARGET_P (li->insn ()))
1032 ? eligible_for_annul_false (jump, i, li->insn (), flags) :
1033 #endif
1034 #ifdef ANNUL_IFTRUE_SLOTS
1035 (INSN_ANNULLED_BRANCH_P (jump)
1036 && ! INSN_FROM_TARGET_P (XEXP (li, 0)))
1037 ? eligible_for_annul_true (jump, i, li->insn (), flags) :
1038 #endif
1039 eligible_for_delay (jump, i, li->insn (), flags)))
1040 break;
1042 return (li == NULL);
1045 /* DELAY_LIST is a list of insns that have already been placed into delay
1046 slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
1047 If not, return 0; otherwise return 1. */
1049 static int
1050 check_annul_list_true_false (int annul_true_p, rtx delay_list)
1052 rtx temp;
1054 if (delay_list)
1056 for (temp = delay_list; temp; temp = XEXP (temp, 1))
1058 rtx trial = XEXP (temp, 0);
1060 if ((annul_true_p && INSN_FROM_TARGET_P (trial))
1061 || (!annul_true_p && !INSN_FROM_TARGET_P (trial)))
1062 return 0;
1066 return 1;
1069 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
1070 the condition tested by INSN is CONDITION and the resources shown in
1071 OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1072 from SEQ's delay list, in addition to whatever insns it may execute
1073 (in DELAY_LIST). SETS and NEEDED are denote resources already set and
1074 needed while searching for delay slot insns. Return the concatenated
1075 delay list if possible, otherwise, return 0.
1077 SLOTS_TO_FILL is the total number of slots required by INSN, and
1078 PSLOTS_FILLED points to the number filled so far (also the number of
1079 insns in DELAY_LIST). It is updated with the number that have been
1080 filled from the SEQUENCE, if any.
1082 PANNUL_P points to a nonzero value if we already know that we need
1083 to annul INSN. If this routine determines that annulling is needed,
1084 it may set that value nonzero.
1086 PNEW_THREAD points to a location that is to receive the place at which
1087 execution should continue. */
1089 static rtx_insn_list *
1090 steal_delay_list_from_target (rtx_insn *insn, rtx condition, rtx_sequence *seq,
1091 rtx_insn_list *delay_list, struct resources *sets,
1092 struct resources *needed,
1093 struct resources *other_needed,
1094 int slots_to_fill, int *pslots_filled,
1095 int *pannul_p, rtx *pnew_thread)
1097 int slots_remaining = slots_to_fill - *pslots_filled;
1098 int total_slots_filled = *pslots_filled;
1099 rtx_insn_list *new_delay_list = 0;
1100 int must_annul = *pannul_p;
1101 int used_annul = 0;
1102 int i;
1103 struct resources cc_set;
1104 bool *redundant;
1106 /* We can't do anything if there are more delay slots in SEQ than we
1107 can handle, or if we don't know that it will be a taken branch.
1108 We know that it will be a taken branch if it is either an unconditional
1109 branch or a conditional branch with a stricter branch condition.
1111 Also, exit if the branch has more than one set, since then it is computing
1112 other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1113 ??? It may be possible to move other sets into INSN in addition to
1114 moving the instructions in the delay slots.
1116 We can not steal the delay list if one of the instructions in the
1117 current delay_list modifies the condition codes and the jump in the
1118 sequence is a conditional jump. We can not do this because we can
1119 not change the direction of the jump because the condition codes
1120 will effect the direction of the jump in the sequence. */
1122 CLEAR_RESOURCE (&cc_set);
1123 for (rtx_insn_list *temp = delay_list; temp; temp = temp->next ())
1125 rtx_insn *trial = temp->insn ();
1127 mark_set_resources (trial, &cc_set, 0, MARK_SRC_DEST_CALL);
1128 if (insn_references_resource_p (seq->insn (0), &cc_set, false))
1129 return delay_list;
1132 if (XVECLEN (seq, 0) - 1 > slots_remaining
1133 || ! condition_dominates_p (condition, seq->insn (0))
1134 || ! single_set (seq->insn (0)))
1135 return delay_list;
1137 /* On some targets, branches with delay slots can have a limited
1138 displacement. Give the back end a chance to tell us we can't do
1139 this. */
1140 if (! targetm.can_follow_jump (insn, seq->insn (0)))
1141 return delay_list;
1143 redundant = XALLOCAVEC (bool, XVECLEN (seq, 0));
1144 for (i = 1; i < seq->len (); i++)
1146 rtx_insn *trial = seq->insn (i);
1147 int flags;
1149 if (insn_references_resource_p (trial, sets, false)
1150 || insn_sets_resource_p (trial, needed, false)
1151 || insn_sets_resource_p (trial, sets, false)
1152 /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1153 delay list. */
1154 || (HAVE_cc0 && find_reg_note (trial, REG_CC_USER, NULL_RTX))
1155 /* If TRIAL is from the fallthrough code of an annulled branch insn
1156 in SEQ, we cannot use it. */
1157 || (INSN_ANNULLED_BRANCH_P (seq->insn (0))
1158 && ! INSN_FROM_TARGET_P (trial)))
1159 return delay_list;
1161 /* If this insn was already done (usually in a previous delay slot),
1162 pretend we put it in our delay slot. */
1163 redundant[i] = redundant_insn (trial, insn, new_delay_list);
1164 if (redundant[i])
1165 continue;
1167 /* We will end up re-vectoring this branch, so compute flags
1168 based on jumping to the new label. */
1169 flags = get_jump_flags (insn, JUMP_LABEL (seq->insn (0)));
1171 if (! must_annul
1172 && ((condition == const_true_rtx
1173 || (! insn_sets_resource_p (trial, other_needed, false)
1174 && ! may_trap_or_fault_p (PATTERN (trial)))))
1175 ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1176 : (must_annul || (delay_list == NULL && new_delay_list == NULL))
1177 && (must_annul = 1,
1178 check_annul_list_true_false (0, delay_list)
1179 && check_annul_list_true_false (0, new_delay_list)
1180 && eligible_for_annul_false (insn, total_slots_filled,
1181 trial, flags)))
1183 if (must_annul)
1184 used_annul = 1;
1185 rtx_insn *temp = copy_delay_slot_insn (trial);
1186 INSN_FROM_TARGET_P (temp) = 1;
1187 new_delay_list = add_to_delay_list (temp, new_delay_list);
1188 total_slots_filled++;
1190 if (--slots_remaining == 0)
1191 break;
1193 else
1194 return delay_list;
1197 /* Record the effect of the instructions that were redundant and which
1198 we therefore decided not to copy. */
1199 for (i = 1; i < seq->len (); i++)
1200 if (redundant[i])
1201 update_block (seq->insn (i), insn);
1203 /* Show the place to which we will be branching. */
1204 *pnew_thread = first_active_target_insn (JUMP_LABEL (seq->insn (0)));
1206 /* Add any new insns to the delay list and update the count of the
1207 number of slots filled. */
1208 *pslots_filled = total_slots_filled;
1209 if (used_annul)
1210 *pannul_p = 1;
1212 if (delay_list == 0)
1213 return new_delay_list;
1215 for (rtx_insn_list *temp = new_delay_list; temp; temp = temp->next ())
1216 delay_list = add_to_delay_list (temp->insn (), delay_list);
1218 return delay_list;
1221 /* Similar to steal_delay_list_from_target except that SEQ is on the
1222 fallthrough path of INSN. Here we only do something if the delay insn
1223 of SEQ is an unconditional branch. In that case we steal its delay slot
1224 for INSN since unconditional branches are much easier to fill. */
1226 static rtx_insn_list *
1227 steal_delay_list_from_fallthrough (rtx_insn *insn, rtx condition,
1228 rtx_sequence *seq,
1229 rtx_insn_list *delay_list,
1230 struct resources *sets,
1231 struct resources *needed,
1232 struct resources *other_needed,
1233 int slots_to_fill, int *pslots_filled,
1234 int *pannul_p)
1236 int i;
1237 int flags;
1238 int must_annul = *pannul_p;
1239 int used_annul = 0;
1241 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1243 /* We can't do anything if SEQ's delay insn isn't an
1244 unconditional branch. */
1246 if (! simplejump_or_return_p (seq->insn (0)))
1247 return delay_list;
1249 for (i = 1; i < seq->len (); i++)
1251 rtx_insn *trial = seq->insn (i);
1253 /* If TRIAL sets CC0, stealing it will move it too far from the use
1254 of CC0. */
1255 if (insn_references_resource_p (trial, sets, false)
1256 || insn_sets_resource_p (trial, needed, false)
1257 || insn_sets_resource_p (trial, sets, false)
1258 || (HAVE_cc0 && sets_cc0_p (PATTERN (trial))))
1260 break;
1262 /* If this insn was already done, we don't need it. */
1263 if (redundant_insn (trial, insn, delay_list))
1265 update_block (trial, insn);
1266 delete_from_delay_slot (trial);
1267 continue;
1270 if (! must_annul
1271 && ((condition == const_true_rtx
1272 || (! insn_sets_resource_p (trial, other_needed, false)
1273 && ! may_trap_or_fault_p (PATTERN (trial)))))
1274 ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1275 : (must_annul || delay_list == NULL) && (must_annul = 1,
1276 check_annul_list_true_false (1, delay_list)
1277 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1279 if (must_annul)
1280 used_annul = 1;
1281 delete_from_delay_slot (trial);
1282 delay_list = add_to_delay_list (trial, delay_list);
1284 if (++(*pslots_filled) == slots_to_fill)
1285 break;
1287 else
1288 break;
1291 if (used_annul)
1292 *pannul_p = 1;
1293 return delay_list;
1296 /* Try merging insns starting at THREAD which match exactly the insns in
1297 INSN's delay list.
1299 If all insns were matched and the insn was previously annulling, the
1300 annul bit will be cleared.
1302 For each insn that is merged, if the branch is or will be non-annulling,
1303 we delete the merged insn. */
1305 static void
1306 try_merge_delay_insns (rtx_insn *insn, rtx_insn *thread)
1308 rtx_insn *trial, *next_trial;
1309 rtx_insn *delay_insn = as_a <rtx_insn *> (XVECEXP (PATTERN (insn), 0, 0));
1310 int annul_p = JUMP_P (delay_insn) && INSN_ANNULLED_BRANCH_P (delay_insn);
1311 int slot_number = 1;
1312 int num_slots = XVECLEN (PATTERN (insn), 0);
1313 rtx next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1314 struct resources set, needed, modified;
1315 rtx_insn_list *merged_insns = 0;
1316 int i, j;
1317 int flags;
1319 flags = get_jump_flags (delay_insn, JUMP_LABEL (delay_insn));
1321 CLEAR_RESOURCE (&needed);
1322 CLEAR_RESOURCE (&set);
1324 /* If this is not an annulling branch, take into account anything needed in
1325 INSN's delay slot. This prevents two increments from being incorrectly
1326 folded into one. If we are annulling, this would be the correct
1327 thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1328 will essentially disable this optimization. This method is somewhat of
1329 a kludge, but I don't see a better way.) */
1330 if (! annul_p)
1331 for (i = 1 ; i < num_slots; i++)
1332 if (XVECEXP (PATTERN (insn), 0, i))
1333 mark_referenced_resources (XVECEXP (PATTERN (insn), 0, i), &needed,
1334 true);
1336 for (trial = thread; !stop_search_p (trial, 1); trial = next_trial)
1338 rtx pat = PATTERN (trial);
1339 rtx oldtrial = trial;
1341 next_trial = next_nonnote_insn (trial);
1343 /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1344 if (NONJUMP_INSN_P (trial)
1345 && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1346 continue;
1348 if (GET_CODE (next_to_match) == GET_CODE (trial)
1349 /* We can't share an insn that sets cc0. */
1350 && (!HAVE_cc0 || ! sets_cc0_p (pat))
1351 && ! insn_references_resource_p (trial, &set, true)
1352 && ! insn_sets_resource_p (trial, &set, true)
1353 && ! insn_sets_resource_p (trial, &needed, true)
1354 && (trial = try_split (pat, trial, 0)) != 0
1355 /* Update next_trial, in case try_split succeeded. */
1356 && (next_trial = next_nonnote_insn (trial))
1357 /* Likewise THREAD. */
1358 && (thread = oldtrial == thread ? trial : thread)
1359 && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1360 /* Have to test this condition if annul condition is different
1361 from (and less restrictive than) non-annulling one. */
1362 && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1365 if (! annul_p)
1367 update_block (trial, thread);
1368 if (trial == thread)
1369 thread = next_active_insn (thread);
1371 delete_related_insns (trial);
1372 INSN_FROM_TARGET_P (next_to_match) = 0;
1374 else
1375 merged_insns = gen_rtx_INSN_LIST (VOIDmode, trial, merged_insns);
1377 if (++slot_number == num_slots)
1378 break;
1380 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1383 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1384 mark_referenced_resources (trial, &needed, true);
1387 /* See if we stopped on a filled insn. If we did, try to see if its
1388 delay slots match. */
1389 if (slot_number != num_slots
1390 && trial && NONJUMP_INSN_P (trial)
1391 && GET_CODE (PATTERN (trial)) == SEQUENCE
1392 && !(JUMP_P (XVECEXP (PATTERN (trial), 0, 0))
1393 && INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial), 0, 0))))
1395 rtx_sequence *pat = as_a <rtx_sequence *> (PATTERN (trial));
1396 rtx filled_insn = XVECEXP (pat, 0, 0);
1398 /* Account for resources set/needed by the filled insn. */
1399 mark_set_resources (filled_insn, &set, 0, MARK_SRC_DEST_CALL);
1400 mark_referenced_resources (filled_insn, &needed, true);
1402 for (i = 1; i < pat->len (); i++)
1404 rtx_insn *dtrial = pat->insn (i);
1406 CLEAR_RESOURCE (&modified);
1407 /* Account for resources set by the the insn following NEXT_TO_MATCH
1408 inside INSN's delay list. */
1409 for (j = 1; slot_number + j < num_slots; j++)
1410 mark_set_resources (XVECEXP (PATTERN (insn), 0, slot_number + j),
1411 &modified, 0, MARK_SRC_DEST_CALL);
1412 /* Account for resources set by the the insn before DTRIAL and inside
1413 TRIAL's delay list. */
1414 for (j = 1; j < i; j++)
1415 mark_set_resources (XVECEXP (pat, 0, j),
1416 &modified, 0, MARK_SRC_DEST_CALL);
1417 if (! insn_references_resource_p (dtrial, &set, true)
1418 && ! insn_sets_resource_p (dtrial, &set, true)
1419 && ! insn_sets_resource_p (dtrial, &needed, true)
1420 && (!HAVE_cc0 || ! sets_cc0_p (PATTERN (dtrial)))
1421 && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1422 /* Check that DTRIAL and NEXT_TO_MATCH does not reference a
1423 resource modified between them (only dtrial is checked because
1424 next_to_match and dtrial shall to be equal in order to hit
1425 this line) */
1426 && ! insn_references_resource_p (dtrial, &modified, true)
1427 && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1429 if (! annul_p)
1431 rtx_insn *new_rtx;
1433 update_block (dtrial, thread);
1434 new_rtx = delete_from_delay_slot (dtrial);
1435 if (thread->deleted ())
1436 thread = new_rtx;
1437 INSN_FROM_TARGET_P (next_to_match) = 0;
1439 else
1440 merged_insns = gen_rtx_INSN_LIST (SImode, dtrial,
1441 merged_insns);
1443 if (++slot_number == num_slots)
1444 break;
1446 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1448 else
1450 /* Keep track of the set/referenced resources for the delay
1451 slots of any trial insns we encounter. */
1452 mark_set_resources (dtrial, &set, 0, MARK_SRC_DEST_CALL);
1453 mark_referenced_resources (dtrial, &needed, true);
1458 /* If all insns in the delay slot have been matched and we were previously
1459 annulling the branch, we need not any more. In that case delete all the
1460 merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1461 the delay list so that we know that it isn't only being used at the
1462 target. */
1463 if (slot_number == num_slots && annul_p)
1465 for (; merged_insns; merged_insns = merged_insns->next ())
1467 if (GET_MODE (merged_insns) == SImode)
1469 rtx_insn *new_rtx;
1471 update_block (merged_insns->insn (), thread);
1472 new_rtx = delete_from_delay_slot (merged_insns->insn ());
1473 if (thread->deleted ())
1474 thread = new_rtx;
1476 else
1478 update_block (merged_insns->insn (), thread);
1479 delete_related_insns (merged_insns->insn ());
1483 INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1485 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1486 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i)) = 0;
1490 /* See if INSN is redundant with an insn in front of TARGET. Often this
1491 is called when INSN is a candidate for a delay slot of TARGET.
1492 DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1493 of INSN. Often INSN will be redundant with an insn in a delay slot of
1494 some previous insn. This happens when we have a series of branches to the
1495 same label; in that case the first insn at the target might want to go
1496 into each of the delay slots.
1498 If we are not careful, this routine can take up a significant fraction
1499 of the total compilation time (4%), but only wins rarely. Hence we
1500 speed this routine up by making two passes. The first pass goes back
1501 until it hits a label and sees if it finds an insn with an identical
1502 pattern. Only in this (relatively rare) event does it check for
1503 data conflicts.
1505 We do not split insns we encounter. This could cause us not to find a
1506 redundant insn, but the cost of splitting seems greater than the possible
1507 gain in rare cases. */
1509 static rtx
1510 redundant_insn (rtx insn, rtx_insn *target, rtx delay_list)
1512 rtx target_main = target;
1513 rtx ipat = PATTERN (insn);
1514 rtx_insn *trial;
1515 rtx pat;
1516 struct resources needed, set;
1517 int i;
1518 unsigned insns_to_search;
1520 /* If INSN has any REG_UNUSED notes, it can't match anything since we
1521 are allowed to not actually assign to such a register. */
1522 if (find_reg_note (insn, REG_UNUSED, NULL_RTX) != 0)
1523 return 0;
1525 /* Scan backwards looking for a match. */
1526 for (trial = PREV_INSN (target),
1527 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1528 trial && insns_to_search > 0;
1529 trial = PREV_INSN (trial))
1531 /* (use (insn))s can come immediately after a barrier if the
1532 label that used to precede them has been deleted as dead.
1533 See delete_related_insns. */
1534 if (LABEL_P (trial) || BARRIER_P (trial))
1535 return 0;
1537 if (!INSN_P (trial))
1538 continue;
1539 --insns_to_search;
1541 pat = PATTERN (trial);
1542 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1543 continue;
1545 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (pat))
1547 /* Stop for a CALL and its delay slots because it is difficult to
1548 track its resource needs correctly. */
1549 if (CALL_P (seq->element (0)))
1550 return 0;
1552 /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1553 slots because it is difficult to track its resource needs
1554 correctly. */
1556 if (INSN_SETS_ARE_DELAYED (seq->insn (0)))
1557 return 0;
1559 if (INSN_REFERENCES_ARE_DELAYED (seq->insn (0)))
1560 return 0;
1562 /* See if any of the insns in the delay slot match, updating
1563 resource requirements as we go. */
1564 for (i = seq->len () - 1; i > 0; i--)
1565 if (GET_CODE (seq->element (i)) == GET_CODE (insn)
1566 && rtx_equal_p (PATTERN (seq->element (i)), ipat)
1567 && ! find_reg_note (seq->element (i), REG_UNUSED, NULL_RTX))
1568 break;
1570 /* If found a match, exit this loop early. */
1571 if (i > 0)
1572 break;
1575 else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat)
1576 && ! find_reg_note (trial, REG_UNUSED, NULL_RTX))
1577 break;
1580 /* If we didn't find an insn that matches, return 0. */
1581 if (trial == 0)
1582 return 0;
1584 /* See what resources this insn sets and needs. If they overlap, or
1585 if this insn references CC0, it can't be redundant. */
1587 CLEAR_RESOURCE (&needed);
1588 CLEAR_RESOURCE (&set);
1589 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1590 mark_referenced_resources (insn, &needed, true);
1592 /* If TARGET is a SEQUENCE, get the main insn. */
1593 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1594 target_main = XVECEXP (PATTERN (target), 0, 0);
1596 if (resource_conflicts_p (&needed, &set)
1597 || (HAVE_cc0 && reg_mentioned_p (cc0_rtx, ipat))
1598 /* The insn requiring the delay may not set anything needed or set by
1599 INSN. */
1600 || insn_sets_resource_p (target_main, &needed, true)
1601 || insn_sets_resource_p (target_main, &set, true))
1602 return 0;
1604 /* Insns we pass may not set either NEEDED or SET, so merge them for
1605 simpler tests. */
1606 needed.memory |= set.memory;
1607 IOR_HARD_REG_SET (needed.regs, set.regs);
1609 /* This insn isn't redundant if it conflicts with an insn that either is
1610 or will be in a delay slot of TARGET. */
1612 while (delay_list)
1614 if (insn_sets_resource_p (XEXP (delay_list, 0), &needed, true))
1615 return 0;
1616 delay_list = XEXP (delay_list, 1);
1619 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1620 for (i = 1; i < XVECLEN (PATTERN (target), 0); i++)
1621 if (insn_sets_resource_p (XVECEXP (PATTERN (target), 0, i), &needed,
1622 true))
1623 return 0;
1625 /* Scan backwards until we reach a label or an insn that uses something
1626 INSN sets or sets something insn uses or sets. */
1628 for (trial = PREV_INSN (target),
1629 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1630 trial && !LABEL_P (trial) && insns_to_search > 0;
1631 trial = PREV_INSN (trial))
1633 if (!INSN_P (trial))
1634 continue;
1635 --insns_to_search;
1637 pat = PATTERN (trial);
1638 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1639 continue;
1641 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (pat))
1643 bool annul_p = false;
1644 rtx_insn *control = seq->insn (0);
1646 /* If this is a CALL_INSN and its delay slots, it is hard to track
1647 the resource needs properly, so give up. */
1648 if (CALL_P (control))
1649 return 0;
1651 /* If this is an INSN or JUMP_INSN with delayed effects, it
1652 is hard to track the resource needs properly, so give up. */
1654 if (INSN_SETS_ARE_DELAYED (control))
1655 return 0;
1657 if (INSN_REFERENCES_ARE_DELAYED (control))
1658 return 0;
1660 if (JUMP_P (control))
1661 annul_p = INSN_ANNULLED_BRANCH_P (control);
1663 /* See if any of the insns in the delay slot match, updating
1664 resource requirements as we go. */
1665 for (i = seq->len () - 1; i > 0; i--)
1667 rtx candidate = seq->element (i);
1669 /* If an insn will be annulled if the branch is false, it isn't
1670 considered as a possible duplicate insn. */
1671 if (rtx_equal_p (PATTERN (candidate), ipat)
1672 && ! (annul_p && INSN_FROM_TARGET_P (candidate)))
1674 /* Show that this insn will be used in the sequel. */
1675 INSN_FROM_TARGET_P (candidate) = 0;
1676 return candidate;
1679 /* Unless this is an annulled insn from the target of a branch,
1680 we must stop if it sets anything needed or set by INSN. */
1681 if ((!annul_p || !INSN_FROM_TARGET_P (candidate))
1682 && insn_sets_resource_p (candidate, &needed, true))
1683 return 0;
1686 /* If the insn requiring the delay slot conflicts with INSN, we
1687 must stop. */
1688 if (insn_sets_resource_p (control, &needed, true))
1689 return 0;
1691 else
1693 /* See if TRIAL is the same as INSN. */
1694 pat = PATTERN (trial);
1695 if (rtx_equal_p (pat, ipat))
1696 return trial;
1698 /* Can't go any further if TRIAL conflicts with INSN. */
1699 if (insn_sets_resource_p (trial, &needed, true))
1700 return 0;
1704 return 0;
1707 /* Return 1 if THREAD can only be executed in one way. If LABEL is nonzero,
1708 it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1709 is nonzero, we are allowed to fall into this thread; otherwise, we are
1710 not.
1712 If LABEL is used more than one or we pass a label other than LABEL before
1713 finding an active insn, we do not own this thread. */
1715 static int
1716 own_thread_p (rtx thread, rtx label, int allow_fallthrough)
1718 rtx_insn *active_insn;
1719 rtx_insn *insn;
1721 /* We don't own the function end. */
1722 if (thread == 0 || ANY_RETURN_P (thread))
1723 return 0;
1725 /* We have a non-NULL insn. */
1726 rtx_insn *thread_insn = as_a <rtx_insn *> (thread);
1728 /* Get the first active insn, or THREAD_INSN, if it is an active insn. */
1729 active_insn = next_active_insn (PREV_INSN (thread_insn));
1731 for (insn = thread_insn; insn != active_insn; insn = NEXT_INSN (insn))
1732 if (LABEL_P (insn)
1733 && (insn != label || LABEL_NUSES (insn) != 1))
1734 return 0;
1736 if (allow_fallthrough)
1737 return 1;
1739 /* Ensure that we reach a BARRIER before any insn or label. */
1740 for (insn = prev_nonnote_insn (thread_insn);
1741 insn == 0 || !BARRIER_P (insn);
1742 insn = prev_nonnote_insn (insn))
1743 if (insn == 0
1744 || LABEL_P (insn)
1745 || (NONJUMP_INSN_P (insn)
1746 && GET_CODE (PATTERN (insn)) != USE
1747 && GET_CODE (PATTERN (insn)) != CLOBBER))
1748 return 0;
1750 return 1;
1753 /* Called when INSN is being moved from a location near the target of a jump.
1754 We leave a marker of the form (use (INSN)) immediately in front
1755 of WHERE for mark_target_live_regs. These markers will be deleted when
1756 reorg finishes.
1758 We used to try to update the live status of registers if WHERE is at
1759 the start of a basic block, but that can't work since we may remove a
1760 BARRIER in relax_delay_slots. */
1762 static void
1763 update_block (rtx_insn *insn, rtx where)
1765 /* Ignore if this was in a delay slot and it came from the target of
1766 a branch. */
1767 if (INSN_FROM_TARGET_P (insn))
1768 return;
1770 emit_insn_before (gen_rtx_USE (VOIDmode, insn), where);
1772 /* INSN might be making a value live in a block where it didn't use to
1773 be. So recompute liveness information for this block. */
1775 incr_ticks_for_insn (insn);
1778 /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1779 the basic block containing the jump. */
1781 static int
1782 reorg_redirect_jump (rtx_jump_insn *jump, rtx nlabel)
1784 incr_ticks_for_insn (jump);
1785 return redirect_jump (jump, nlabel, 1);
1788 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1789 We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1790 that reference values used in INSN. If we find one, then we move the
1791 REG_DEAD note to INSN.
1793 This is needed to handle the case where a later insn (after INSN) has a
1794 REG_DEAD note for a register used by INSN, and this later insn subsequently
1795 gets moved before a CODE_LABEL because it is a redundant insn. In this
1796 case, mark_target_live_regs may be confused into thinking the register
1797 is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1799 static void
1800 update_reg_dead_notes (rtx_insn *insn, rtx_insn *delayed_insn)
1802 rtx link, next;
1803 rtx_insn *p;
1805 for (p = next_nonnote_insn (insn); p != delayed_insn;
1806 p = next_nonnote_insn (p))
1807 for (link = REG_NOTES (p); link; link = next)
1809 next = XEXP (link, 1);
1811 if (REG_NOTE_KIND (link) != REG_DEAD
1812 || !REG_P (XEXP (link, 0)))
1813 continue;
1815 if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
1817 /* Move the REG_DEAD note from P to INSN. */
1818 remove_note (p, link);
1819 XEXP (link, 1) = REG_NOTES (insn);
1820 REG_NOTES (insn) = link;
1825 /* Called when an insn redundant with start_insn is deleted. If there
1826 is a REG_DEAD note for the target of start_insn between start_insn
1827 and stop_insn, then the REG_DEAD note needs to be deleted since the
1828 value no longer dies there.
1830 If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1831 confused into thinking the register is dead. */
1833 static void
1834 fix_reg_dead_note (rtx start_insn, rtx stop_insn)
1836 rtx link, next;
1837 rtx_insn *p;
1839 for (p = next_nonnote_insn (start_insn); p != stop_insn;
1840 p = next_nonnote_insn (p))
1841 for (link = REG_NOTES (p); link; link = next)
1843 next = XEXP (link, 1);
1845 if (REG_NOTE_KIND (link) != REG_DEAD
1846 || !REG_P (XEXP (link, 0)))
1847 continue;
1849 if (reg_set_p (XEXP (link, 0), PATTERN (start_insn)))
1851 remove_note (p, link);
1852 return;
1857 /* Delete any REG_UNUSED notes that exist on INSN but not on REDUNDANT_INSN.
1859 This handles the case of udivmodXi4 instructions which optimize their
1860 output depending on whether any REG_UNUSED notes are present.
1861 we must make sure that INSN calculates as many results as REDUNDANT_INSN
1862 does. */
1864 static void
1865 update_reg_unused_notes (rtx insn, rtx redundant_insn)
1867 rtx link, next;
1869 for (link = REG_NOTES (insn); link; link = next)
1871 next = XEXP (link, 1);
1873 if (REG_NOTE_KIND (link) != REG_UNUSED
1874 || !REG_P (XEXP (link, 0)))
1875 continue;
1877 if (! find_regno_note (redundant_insn, REG_UNUSED,
1878 REGNO (XEXP (link, 0))))
1879 remove_note (insn, link);
1883 static vec <rtx> sibling_labels;
1885 /* Return the label before INSN, or put a new label there. If SIBLING is
1886 non-zero, it is another label associated with the new label (if any),
1887 typically the former target of the jump that will be redirected to
1888 the new label. */
1890 static rtx_insn *
1891 get_label_before (rtx_insn *insn, rtx sibling)
1893 rtx_insn *label;
1895 /* Find an existing label at this point
1896 or make a new one if there is none. */
1897 label = prev_nonnote_insn (insn);
1899 if (label == 0 || !LABEL_P (label))
1901 rtx_insn *prev = PREV_INSN (insn);
1903 label = gen_label_rtx ();
1904 emit_label_after (label, prev);
1905 LABEL_NUSES (label) = 0;
1906 if (sibling)
1908 sibling_labels.safe_push (label);
1909 sibling_labels.safe_push (sibling);
1912 return label;
1915 /* Scan a function looking for insns that need a delay slot and find insns to
1916 put into the delay slot.
1918 NON_JUMPS_P is nonzero if we are to only try to fill non-jump insns (such
1919 as calls). We do these first since we don't want jump insns (that are
1920 easier to fill) to get the only insns that could be used for non-jump insns.
1921 When it is zero, only try to fill JUMP_INSNs.
1923 When slots are filled in this manner, the insns (including the
1924 delay_insn) are put together in a SEQUENCE rtx. In this fashion,
1925 it is possible to tell whether a delay slot has really been filled
1926 or not. `final' knows how to deal with this, by communicating
1927 through FINAL_SEQUENCE. */
1929 static void
1930 fill_simple_delay_slots (int non_jumps_p)
1932 rtx_insn *insn, *trial, *next_trial;
1933 rtx pat;
1934 int i;
1935 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
1936 struct resources needed, set;
1937 int slots_to_fill, slots_filled;
1938 rtx_insn_list *delay_list;
1940 for (i = 0; i < num_unfilled_slots; i++)
1942 int flags;
1943 /* Get the next insn to fill. If it has already had any slots assigned,
1944 we can't do anything with it. Maybe we'll improve this later. */
1946 insn = unfilled_slots_base[i];
1947 if (insn == 0
1948 || insn->deleted ()
1949 || (NONJUMP_INSN_P (insn)
1950 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1951 || (JUMP_P (insn) && non_jumps_p)
1952 || (!JUMP_P (insn) && ! non_jumps_p))
1953 continue;
1955 /* It may have been that this insn used to need delay slots, but
1956 now doesn't; ignore in that case. This can happen, for example,
1957 on the HP PA RISC, where the number of delay slots depends on
1958 what insns are nearby. */
1959 slots_to_fill = num_delay_slots (insn);
1961 /* Some machine description have defined instructions to have
1962 delay slots only in certain circumstances which may depend on
1963 nearby insns (which change due to reorg's actions).
1965 For example, the PA port normally has delay slots for unconditional
1966 jumps.
1968 However, the PA port claims such jumps do not have a delay slot
1969 if they are immediate successors of certain CALL_INSNs. This
1970 allows the port to favor filling the delay slot of the call with
1971 the unconditional jump. */
1972 if (slots_to_fill == 0)
1973 continue;
1975 /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
1976 says how many. After initialization, first try optimizing
1978 call _foo call _foo
1979 nop add %o7,.-L1,%o7
1980 b,a L1
1983 If this case applies, the delay slot of the call is filled with
1984 the unconditional jump. This is done first to avoid having the
1985 delay slot of the call filled in the backward scan. Also, since
1986 the unconditional jump is likely to also have a delay slot, that
1987 insn must exist when it is subsequently scanned.
1989 This is tried on each insn with delay slots as some machines
1990 have insns which perform calls, but are not represented as
1991 CALL_INSNs. */
1993 slots_filled = 0;
1994 delay_list = 0;
1996 if (JUMP_P (insn))
1997 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1998 else
1999 flags = get_jump_flags (insn, NULL_RTX);
2001 if ((trial = next_active_insn (insn))
2002 && JUMP_P (trial)
2003 && simplejump_p (trial)
2004 && eligible_for_delay (insn, slots_filled, trial, flags)
2005 && no_labels_between_p (insn, trial)
2006 && ! can_throw_internal (trial))
2008 rtx_insn **tmp;
2009 slots_filled++;
2010 delay_list = add_to_delay_list (trial, delay_list);
2012 /* TRIAL may have had its delay slot filled, then unfilled. When
2013 the delay slot is unfilled, TRIAL is placed back on the unfilled
2014 slots obstack. Unfortunately, it is placed on the end of the
2015 obstack, not in its original location. Therefore, we must search
2016 from entry i + 1 to the end of the unfilled slots obstack to
2017 try and find TRIAL. */
2018 tmp = &unfilled_slots_base[i + 1];
2019 while (*tmp != trial && tmp != unfilled_slots_next)
2020 tmp++;
2022 /* Remove the unconditional jump from consideration for delay slot
2023 filling and unthread it. */
2024 if (*tmp == trial)
2025 *tmp = 0;
2027 rtx_insn *next = NEXT_INSN (trial);
2028 rtx_insn *prev = PREV_INSN (trial);
2029 if (prev)
2030 SET_NEXT_INSN (prev) = next;
2031 if (next)
2032 SET_PREV_INSN (next) = prev;
2036 /* Now, scan backwards from the insn to search for a potential
2037 delay-slot candidate. Stop searching when a label or jump is hit.
2039 For each candidate, if it is to go into the delay slot (moved
2040 forward in execution sequence), it must not need or set any resources
2041 that were set by later insns and must not set any resources that
2042 are needed for those insns.
2044 The delay slot insn itself sets resources unless it is a call
2045 (in which case the called routine, not the insn itself, is doing
2046 the setting). */
2048 if (slots_filled < slots_to_fill)
2050 /* If the flags register is dead after the insn, then we want to be
2051 able to accept a candidate that clobbers it. For this purpose,
2052 we need to filter the flags register during life analysis, so
2053 that it doesn't create RAW and WAW dependencies, while still
2054 creating the necessary WAR dependencies. */
2055 bool filter_flags
2056 = (slots_to_fill == 1
2057 && targetm.flags_regnum != INVALID_REGNUM
2058 && find_regno_note (insn, REG_DEAD, targetm.flags_regnum));
2059 struct resources fset;
2060 CLEAR_RESOURCE (&needed);
2061 CLEAR_RESOURCE (&set);
2062 mark_set_resources (insn, &set, 0, MARK_SRC_DEST);
2063 if (filter_flags)
2065 CLEAR_RESOURCE (&fset);
2066 mark_set_resources (insn, &fset, 0, MARK_SRC_DEST);
2068 mark_referenced_resources (insn, &needed, false);
2070 for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, 1);
2071 trial = next_trial)
2073 next_trial = prev_nonnote_insn (trial);
2075 /* This must be an INSN or CALL_INSN. */
2076 pat = PATTERN (trial);
2078 /* Stand-alone USE and CLOBBER are just for flow. */
2079 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2080 continue;
2082 /* Check for resource conflict first, to avoid unnecessary
2083 splitting. */
2084 if (! insn_references_resource_p (trial, &set, true)
2085 && ! insn_sets_resource_p (trial,
2086 filter_flags ? &fset : &set,
2087 true)
2088 && ! insn_sets_resource_p (trial, &needed, true)
2089 /* Can't separate set of cc0 from its use. */
2090 && (!HAVE_cc0 || ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat)))
2091 && ! can_throw_internal (trial))
2093 trial = try_split (pat, trial, 1);
2094 next_trial = prev_nonnote_insn (trial);
2095 if (eligible_for_delay (insn, slots_filled, trial, flags))
2097 /* In this case, we are searching backward, so if we
2098 find insns to put on the delay list, we want
2099 to put them at the head, rather than the
2100 tail, of the list. */
2102 update_reg_dead_notes (trial, insn);
2103 delay_list = gen_rtx_INSN_LIST (VOIDmode,
2104 trial, delay_list);
2105 update_block (trial, trial);
2106 delete_related_insns (trial);
2107 if (slots_to_fill == ++slots_filled)
2108 break;
2109 continue;
2113 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2114 if (filter_flags)
2116 mark_set_resources (trial, &fset, 0, MARK_SRC_DEST_CALL);
2117 /* If the flags register is set, then it doesn't create RAW
2118 dependencies any longer and it also doesn't create WAW
2119 dependencies since it's dead after the original insn. */
2120 if (TEST_HARD_REG_BIT (fset.regs, targetm.flags_regnum))
2122 CLEAR_HARD_REG_BIT (needed.regs, targetm.flags_regnum);
2123 CLEAR_HARD_REG_BIT (fset.regs, targetm.flags_regnum);
2126 mark_referenced_resources (trial, &needed, true);
2130 /* If all needed slots haven't been filled, we come here. */
2132 /* Try to optimize case of jumping around a single insn. */
2133 #if defined(ANNUL_IFFALSE_SLOTS) || defined(ANNUL_IFTRUE_SLOTS)
2134 if (slots_filled != slots_to_fill
2135 && delay_list == 0
2136 && JUMP_P (insn)
2137 && (condjump_p (insn) || condjump_in_parallel_p (insn))
2138 && !ANY_RETURN_P (JUMP_LABEL (insn)))
2140 delay_list = optimize_skip (as_a <rtx_jump_insn *> (insn));
2141 if (delay_list)
2142 slots_filled += 1;
2144 #endif
2146 /* Try to get insns from beyond the insn needing the delay slot.
2147 These insns can neither set or reference resources set in insns being
2148 skipped, cannot set resources in the insn being skipped, and, if this
2149 is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2150 call might not return).
2152 There used to be code which continued past the target label if
2153 we saw all uses of the target label. This code did not work,
2154 because it failed to account for some instructions which were
2155 both annulled and marked as from the target. This can happen as a
2156 result of optimize_skip. Since this code was redundant with
2157 fill_eager_delay_slots anyways, it was just deleted. */
2159 if (slots_filled != slots_to_fill
2160 /* If this instruction could throw an exception which is
2161 caught in the same function, then it's not safe to fill
2162 the delay slot with an instruction from beyond this
2163 point. For example, consider:
2165 int i = 2;
2167 try {
2168 f();
2169 i = 3;
2170 } catch (...) {}
2172 return i;
2174 Even though `i' is a local variable, we must be sure not
2175 to put `i = 3' in the delay slot if `f' might throw an
2176 exception.
2178 Presumably, we should also check to see if we could get
2179 back to this function via `setjmp'. */
2180 && ! can_throw_internal (insn)
2181 && !JUMP_P (insn))
2183 int maybe_never = 0;
2184 rtx pat, trial_delay;
2186 CLEAR_RESOURCE (&needed);
2187 CLEAR_RESOURCE (&set);
2188 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2189 mark_referenced_resources (insn, &needed, true);
2191 if (CALL_P (insn))
2192 maybe_never = 1;
2194 for (trial = next_nonnote_insn (insn); !stop_search_p (trial, 1);
2195 trial = next_trial)
2197 next_trial = next_nonnote_insn (trial);
2199 /* This must be an INSN or CALL_INSN. */
2200 pat = PATTERN (trial);
2202 /* Stand-alone USE and CLOBBER are just for flow. */
2203 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2204 continue;
2206 /* If this already has filled delay slots, get the insn needing
2207 the delay slots. */
2208 if (GET_CODE (pat) == SEQUENCE)
2209 trial_delay = XVECEXP (pat, 0, 0);
2210 else
2211 trial_delay = trial;
2213 /* Stop our search when seeing a jump. */
2214 if (JUMP_P (trial_delay))
2215 break;
2217 /* See if we have a resource problem before we try to split. */
2218 if (GET_CODE (pat) != SEQUENCE
2219 && ! insn_references_resource_p (trial, &set, true)
2220 && ! insn_sets_resource_p (trial, &set, true)
2221 && ! insn_sets_resource_p (trial, &needed, true)
2222 && (!HAVE_cc0 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat)))
2223 && ! (maybe_never && may_trap_or_fault_p (pat))
2224 && (trial = try_split (pat, trial, 0))
2225 && eligible_for_delay (insn, slots_filled, trial, flags)
2226 && ! can_throw_internal (trial))
2228 next_trial = next_nonnote_insn (trial);
2229 delay_list = add_to_delay_list (trial, delay_list);
2230 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, pat))
2231 link_cc0_insns (trial);
2233 delete_related_insns (trial);
2234 if (slots_to_fill == ++slots_filled)
2235 break;
2236 continue;
2239 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2240 mark_referenced_resources (trial, &needed, true);
2242 /* Ensure we don't put insns between the setting of cc and the
2243 comparison by moving a setting of cc into an earlier delay
2244 slot since these insns could clobber the condition code. */
2245 set.cc = 1;
2247 /* If this is a call, we might not get here. */
2248 if (CALL_P (trial_delay))
2249 maybe_never = 1;
2252 /* If there are slots left to fill and our search was stopped by an
2253 unconditional branch, try the insn at the branch target. We can
2254 redirect the branch if it works.
2256 Don't do this if the insn at the branch target is a branch. */
2257 if (slots_to_fill != slots_filled
2258 && trial
2259 && jump_to_label_p (trial)
2260 && simplejump_p (trial)
2261 && (next_trial = next_active_insn (JUMP_LABEL (trial))) != 0
2262 && ! (NONJUMP_INSN_P (next_trial)
2263 && GET_CODE (PATTERN (next_trial)) == SEQUENCE)
2264 && !JUMP_P (next_trial)
2265 && ! insn_references_resource_p (next_trial, &set, true)
2266 && ! insn_sets_resource_p (next_trial, &set, true)
2267 && ! insn_sets_resource_p (next_trial, &needed, true)
2268 && (!HAVE_cc0 || ! reg_mentioned_p (cc0_rtx, PATTERN (next_trial)))
2269 && ! (maybe_never && may_trap_or_fault_p (PATTERN (next_trial)))
2270 && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2271 && eligible_for_delay (insn, slots_filled, next_trial, flags)
2272 && ! can_throw_internal (trial))
2274 /* See comment in relax_delay_slots about necessity of using
2275 next_real_insn here. */
2276 rtx_insn *new_label = next_real_insn (next_trial);
2278 if (new_label != 0)
2279 new_label = get_label_before (new_label, JUMP_LABEL (trial));
2280 else
2281 new_label = find_end_label (simple_return_rtx);
2283 if (new_label)
2285 delay_list
2286 = add_to_delay_list (copy_delay_slot_insn (next_trial),
2287 delay_list);
2288 slots_filled++;
2289 reorg_redirect_jump (as_a <rtx_jump_insn *> (trial),
2290 new_label);
2295 /* If this is an unconditional jump, then try to get insns from the
2296 target of the jump. */
2297 rtx_jump_insn *jump_insn;
2298 if ((jump_insn = dyn_cast <rtx_jump_insn *> (insn))
2299 && simplejump_p (jump_insn)
2300 && slots_filled != slots_to_fill)
2301 delay_list
2302 = fill_slots_from_thread (jump_insn, const_true_rtx,
2303 next_active_insn (JUMP_LABEL (insn)),
2304 NULL, 1, 1,
2305 own_thread_p (JUMP_LABEL (insn),
2306 JUMP_LABEL (insn), 0),
2307 slots_to_fill, &slots_filled,
2308 delay_list);
2310 if (delay_list)
2311 unfilled_slots_base[i]
2312 = emit_delay_sequence (insn, delay_list, slots_filled);
2314 if (slots_to_fill == slots_filled)
2315 unfilled_slots_base[i] = 0;
2317 note_delay_statistics (slots_filled, 0);
2321 /* Follow any unconditional jump at LABEL, for the purpose of redirecting JUMP;
2322 return the ultimate label reached by any such chain of jumps.
2323 Return a suitable return rtx if the chain ultimately leads to a
2324 return instruction.
2325 If LABEL is not followed by a jump, return LABEL.
2326 If the chain loops or we can't find end, return LABEL,
2327 since that tells caller to avoid changing the insn.
2328 If the returned label is obtained by following a crossing jump,
2329 set *CROSSING to true, otherwise set it to false. */
2331 static rtx
2332 follow_jumps (rtx label, rtx_insn *jump, bool *crossing)
2334 rtx_insn *insn;
2335 rtx_insn *next;
2336 int depth;
2338 *crossing = false;
2339 if (ANY_RETURN_P (label))
2340 return label;
2342 rtx_insn *value = as_a <rtx_insn *> (label);
2344 for (depth = 0;
2345 (depth < 10
2346 && (insn = next_active_insn (value)) != 0
2347 && JUMP_P (insn)
2348 && JUMP_LABEL (insn) != NULL_RTX
2349 && ((any_uncondjump_p (insn) && onlyjump_p (insn))
2350 || ANY_RETURN_P (PATTERN (insn)))
2351 && (next = NEXT_INSN (insn))
2352 && BARRIER_P (next));
2353 depth++)
2355 rtx this_label_or_return = JUMP_LABEL (insn);
2357 /* If we have found a cycle, make the insn jump to itself. */
2358 if (this_label_or_return == label)
2359 return label;
2361 /* Cannot follow returns and cannot look through tablejumps. */
2362 if (ANY_RETURN_P (this_label_or_return))
2363 return this_label_or_return;
2365 rtx_insn *this_label = as_a <rtx_insn *> (this_label_or_return);
2366 if (NEXT_INSN (this_label)
2367 && JUMP_TABLE_DATA_P (NEXT_INSN (this_label)))
2368 break;
2370 if (!targetm.can_follow_jump (jump, insn))
2371 break;
2372 if (!*crossing)
2373 *crossing = CROSSING_JUMP_P (jump);
2374 value = this_label;
2376 if (depth == 10)
2377 return label;
2378 return value;
2381 /* Try to find insns to place in delay slots.
2383 INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2384 or is an unconditional branch if CONDITION is const_true_rtx.
2385 *PSLOTS_FILLED is updated with the number of slots that we have filled.
2387 THREAD is a flow-of-control, either the insns to be executed if the
2388 branch is true or if the branch is false, THREAD_IF_TRUE says which.
2390 OPPOSITE_THREAD is the thread in the opposite direction. It is used
2391 to see if any potential delay slot insns set things needed there.
2393 LIKELY is nonzero if it is extremely likely that the branch will be
2394 taken and THREAD_IF_TRUE is set. This is used for the branch at the
2395 end of a loop back up to the top.
2397 OWN_THREAD and OWN_OPPOSITE_THREAD are true if we are the only user of the
2398 thread. I.e., it is the fallthrough code of our jump or the target of the
2399 jump when we are the only jump going there.
2401 If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2402 case, we can only take insns from the head of the thread for our delay
2403 slot. We then adjust the jump to point after the insns we have taken. */
2405 static rtx_insn_list *
2406 fill_slots_from_thread (rtx_jump_insn *insn, rtx condition,
2407 rtx thread_or_return, rtx opposite_thread, int likely,
2408 int thread_if_true, int own_thread, int slots_to_fill,
2409 int *pslots_filled, rtx_insn_list *delay_list)
2411 rtx new_thread;
2412 struct resources opposite_needed, set, needed;
2413 rtx_insn *trial;
2414 int lose = 0;
2415 int must_annul = 0;
2416 int flags;
2418 /* Validate our arguments. */
2419 gcc_assert (condition != const_true_rtx || thread_if_true);
2420 gcc_assert (own_thread || thread_if_true);
2422 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2424 /* If our thread is the end of subroutine, we can't get any delay
2425 insns from that. */
2426 if (thread_or_return == NULL_RTX || ANY_RETURN_P (thread_or_return))
2427 return delay_list;
2429 rtx_insn *thread = as_a <rtx_insn *> (thread_or_return);
2431 /* If this is an unconditional branch, nothing is needed at the
2432 opposite thread. Otherwise, compute what is needed there. */
2433 if (condition == const_true_rtx)
2434 CLEAR_RESOURCE (&opposite_needed);
2435 else
2436 mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2438 /* If the insn at THREAD can be split, do it here to avoid having to
2439 update THREAD and NEW_THREAD if it is done in the loop below. Also
2440 initialize NEW_THREAD. */
2442 new_thread = thread = try_split (PATTERN (thread), thread, 0);
2444 /* Scan insns at THREAD. We are looking for an insn that can be removed
2445 from THREAD (it neither sets nor references resources that were set
2446 ahead of it and it doesn't set anything needs by the insns ahead of
2447 it) and that either can be placed in an annulling insn or aren't
2448 needed at OPPOSITE_THREAD. */
2450 CLEAR_RESOURCE (&needed);
2451 CLEAR_RESOURCE (&set);
2453 /* If we do not own this thread, we must stop as soon as we find
2454 something that we can't put in a delay slot, since all we can do
2455 is branch into THREAD at a later point. Therefore, labels stop
2456 the search if this is not the `true' thread. */
2458 for (trial = thread;
2459 ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2460 trial = next_nonnote_insn (trial))
2462 rtx pat, old_trial;
2464 /* If we have passed a label, we no longer own this thread. */
2465 if (LABEL_P (trial))
2467 own_thread = 0;
2468 continue;
2471 pat = PATTERN (trial);
2472 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2473 continue;
2475 /* If TRIAL conflicts with the insns ahead of it, we lose. Also,
2476 don't separate or copy insns that set and use CC0. */
2477 if (! insn_references_resource_p (trial, &set, true)
2478 && ! insn_sets_resource_p (trial, &set, true)
2479 && ! insn_sets_resource_p (trial, &needed, true)
2480 && (!HAVE_cc0 || (! (reg_mentioned_p (cc0_rtx, pat)
2481 && (! own_thread || ! sets_cc0_p (pat)))))
2482 && ! can_throw_internal (trial))
2484 rtx prior_insn;
2486 /* If TRIAL is redundant with some insn before INSN, we don't
2487 actually need to add it to the delay list; we can merely pretend
2488 we did. */
2489 if ((prior_insn = redundant_insn (trial, insn, delay_list)))
2491 fix_reg_dead_note (prior_insn, insn);
2492 if (own_thread)
2494 update_block (trial, thread);
2495 if (trial == thread)
2497 thread = next_active_insn (thread);
2498 if (new_thread == trial)
2499 new_thread = thread;
2502 delete_related_insns (trial);
2504 else
2506 update_reg_unused_notes (prior_insn, trial);
2507 new_thread = next_active_insn (trial);
2510 continue;
2513 /* There are two ways we can win: If TRIAL doesn't set anything
2514 needed at the opposite thread and can't trap, or if it can
2515 go into an annulled delay slot. But we want neither to copy
2516 nor to speculate frame-related insns. */
2517 if (!must_annul
2518 && ((condition == const_true_rtx
2519 && (own_thread || !RTX_FRAME_RELATED_P (trial)))
2520 || (! insn_sets_resource_p (trial, &opposite_needed, true)
2521 && ! may_trap_or_fault_p (pat)
2522 && ! RTX_FRAME_RELATED_P (trial))))
2524 old_trial = trial;
2525 trial = try_split (pat, trial, 0);
2526 if (new_thread == old_trial)
2527 new_thread = trial;
2528 if (thread == old_trial)
2529 thread = trial;
2530 pat = PATTERN (trial);
2531 if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2532 goto winner;
2534 else if (0
2535 #ifdef ANNUL_IFTRUE_SLOTS
2536 || ! thread_if_true
2537 #endif
2538 #ifdef ANNUL_IFFALSE_SLOTS
2539 || thread_if_true
2540 #endif
2543 old_trial = trial;
2544 trial = try_split (pat, trial, 0);
2545 if (new_thread == old_trial)
2546 new_thread = trial;
2547 if (thread == old_trial)
2548 thread = trial;
2549 pat = PATTERN (trial);
2550 if ((must_annul || delay_list == NULL) && (thread_if_true
2551 ? check_annul_list_true_false (0, delay_list)
2552 && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2553 : check_annul_list_true_false (1, delay_list)
2554 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2556 rtx_insn *temp;
2558 must_annul = 1;
2559 winner:
2561 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, pat))
2562 link_cc0_insns (trial);
2564 /* If we own this thread, delete the insn. If this is the
2565 destination of a branch, show that a basic block status
2566 may have been updated. In any case, mark the new
2567 starting point of this thread. */
2568 if (own_thread)
2570 rtx note;
2572 update_block (trial, thread);
2573 if (trial == thread)
2575 thread = next_active_insn (thread);
2576 if (new_thread == trial)
2577 new_thread = thread;
2580 /* We are moving this insn, not deleting it. We must
2581 temporarily increment the use count on any referenced
2582 label lest it be deleted by delete_related_insns. */
2583 for (note = REG_NOTES (trial);
2584 note != NULL_RTX;
2585 note = XEXP (note, 1))
2586 if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2587 || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2589 /* REG_LABEL_OPERAND could be
2590 NOTE_INSN_DELETED_LABEL too. */
2591 if (LABEL_P (XEXP (note, 0)))
2592 LABEL_NUSES (XEXP (note, 0))++;
2593 else
2594 gcc_assert (REG_NOTE_KIND (note)
2595 == REG_LABEL_OPERAND);
2597 if (jump_to_label_p (trial))
2598 LABEL_NUSES (JUMP_LABEL (trial))++;
2600 delete_related_insns (trial);
2602 for (note = REG_NOTES (trial);
2603 note != NULL_RTX;
2604 note = XEXP (note, 1))
2605 if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2606 || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2608 /* REG_LABEL_OPERAND could be
2609 NOTE_INSN_DELETED_LABEL too. */
2610 if (LABEL_P (XEXP (note, 0)))
2611 LABEL_NUSES (XEXP (note, 0))--;
2612 else
2613 gcc_assert (REG_NOTE_KIND (note)
2614 == REG_LABEL_OPERAND);
2616 if (jump_to_label_p (trial))
2617 LABEL_NUSES (JUMP_LABEL (trial))--;
2619 else
2620 new_thread = next_active_insn (trial);
2622 temp = own_thread ? trial : copy_delay_slot_insn (trial);
2623 if (thread_if_true)
2624 INSN_FROM_TARGET_P (temp) = 1;
2626 delay_list = add_to_delay_list (temp, delay_list);
2628 if (slots_to_fill == ++(*pslots_filled))
2630 /* Even though we have filled all the slots, we
2631 may be branching to a location that has a
2632 redundant insn. Skip any if so. */
2633 while (new_thread && ! own_thread
2634 && ! insn_sets_resource_p (new_thread, &set, true)
2635 && ! insn_sets_resource_p (new_thread, &needed,
2636 true)
2637 && ! insn_references_resource_p (new_thread,
2638 &set, true)
2639 && (prior_insn
2640 = redundant_insn (new_thread, insn,
2641 delay_list)))
2643 /* We know we do not own the thread, so no need
2644 to call update_block and delete_insn. */
2645 fix_reg_dead_note (prior_insn, insn);
2646 update_reg_unused_notes (prior_insn, new_thread);
2647 new_thread = next_active_insn (new_thread);
2649 break;
2652 continue;
2657 /* This insn can't go into a delay slot. */
2658 lose = 1;
2659 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2660 mark_referenced_resources (trial, &needed, true);
2662 /* Ensure we don't put insns between the setting of cc and the comparison
2663 by moving a setting of cc into an earlier delay slot since these insns
2664 could clobber the condition code. */
2665 set.cc = 1;
2667 /* If this insn is a register-register copy and the next insn has
2668 a use of our destination, change it to use our source. That way,
2669 it will become a candidate for our delay slot the next time
2670 through this loop. This case occurs commonly in loops that
2671 scan a list.
2673 We could check for more complex cases than those tested below,
2674 but it doesn't seem worth it. It might also be a good idea to try
2675 to swap the two insns. That might do better.
2677 We can't do this if the next insn modifies our destination, because
2678 that would make the replacement into the insn invalid. We also can't
2679 do this if it modifies our source, because it might be an earlyclobber
2680 operand. This latter test also prevents updating the contents of
2681 a PRE_INC. We also can't do this if there's overlap of source and
2682 destination. Overlap may happen for larger-than-register-size modes. */
2684 if (NONJUMP_INSN_P (trial) && GET_CODE (pat) == SET
2685 && REG_P (SET_SRC (pat))
2686 && REG_P (SET_DEST (pat))
2687 && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2689 rtx_insn *next = next_nonnote_insn (trial);
2691 if (next && NONJUMP_INSN_P (next)
2692 && GET_CODE (PATTERN (next)) != USE
2693 && ! reg_set_p (SET_DEST (pat), next)
2694 && ! reg_set_p (SET_SRC (pat), next)
2695 && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2696 && ! modified_in_p (SET_DEST (pat), next))
2697 validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2701 /* If we stopped on a branch insn that has delay slots, see if we can
2702 steal some of the insns in those slots. */
2703 if (trial && NONJUMP_INSN_P (trial)
2704 && GET_CODE (PATTERN (trial)) == SEQUENCE
2705 && JUMP_P (XVECEXP (PATTERN (trial), 0, 0)))
2707 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (trial));
2708 /* If this is the `true' thread, we will want to follow the jump,
2709 so we can only do this if we have taken everything up to here. */
2710 if (thread_if_true && trial == new_thread)
2712 delay_list
2713 = steal_delay_list_from_target (insn, condition, sequence,
2714 delay_list, &set, &needed,
2715 &opposite_needed, slots_to_fill,
2716 pslots_filled, &must_annul,
2717 &new_thread);
2718 /* If we owned the thread and are told that it branched
2719 elsewhere, make sure we own the thread at the new location. */
2720 if (own_thread && trial != new_thread)
2721 own_thread = own_thread_p (new_thread, new_thread, 0);
2723 else if (! thread_if_true)
2724 delay_list
2725 = steal_delay_list_from_fallthrough (insn, condition,
2726 sequence,
2727 delay_list, &set, &needed,
2728 &opposite_needed, slots_to_fill,
2729 pslots_filled, &must_annul);
2732 /* If we haven't found anything for this delay slot and it is very
2733 likely that the branch will be taken, see if the insn at our target
2734 increments or decrements a register with an increment that does not
2735 depend on the destination register. If so, try to place the opposite
2736 arithmetic insn after the jump insn and put the arithmetic insn in the
2737 delay slot. If we can't do this, return. */
2738 if (delay_list == 0 && likely
2739 && new_thread && !ANY_RETURN_P (new_thread)
2740 && NONJUMP_INSN_P (new_thread)
2741 && !RTX_FRAME_RELATED_P (new_thread)
2742 && GET_CODE (PATTERN (new_thread)) != ASM_INPUT
2743 && asm_noperands (PATTERN (new_thread)) < 0)
2745 rtx pat = PATTERN (new_thread);
2746 rtx dest;
2747 rtx src;
2749 /* We know "new_thread" is an insn due to NONJUMP_INSN_P (new_thread)
2750 above. */
2751 trial = as_a <rtx_insn *> (new_thread);
2752 pat = PATTERN (trial);
2754 if (!NONJUMP_INSN_P (trial)
2755 || GET_CODE (pat) != SET
2756 || ! eligible_for_delay (insn, 0, trial, flags)
2757 || can_throw_internal (trial))
2758 return 0;
2760 dest = SET_DEST (pat), src = SET_SRC (pat);
2761 if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2762 && rtx_equal_p (XEXP (src, 0), dest)
2763 && (!FLOAT_MODE_P (GET_MODE (src))
2764 || flag_unsafe_math_optimizations)
2765 && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2766 && ! side_effects_p (pat))
2768 rtx other = XEXP (src, 1);
2769 rtx new_arith;
2770 rtx_insn *ninsn;
2772 /* If this is a constant adjustment, use the same code with
2773 the negated constant. Otherwise, reverse the sense of the
2774 arithmetic. */
2775 if (CONST_INT_P (other))
2776 new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2777 negate_rtx (GET_MODE (src), other));
2778 else
2779 new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2780 GET_MODE (src), dest, other);
2782 ninsn = emit_insn_after (gen_rtx_SET (dest, new_arith), insn);
2784 if (recog_memoized (ninsn) < 0
2785 || (extract_insn (ninsn),
2786 !constrain_operands (1, get_preferred_alternatives (ninsn))))
2788 delete_related_insns (ninsn);
2789 return 0;
2792 if (own_thread)
2794 update_block (trial, thread);
2795 if (trial == thread)
2797 thread = next_active_insn (thread);
2798 if (new_thread == trial)
2799 new_thread = thread;
2801 delete_related_insns (trial);
2803 else
2804 new_thread = next_active_insn (trial);
2806 ninsn = own_thread ? trial : copy_delay_slot_insn (trial);
2807 if (thread_if_true)
2808 INSN_FROM_TARGET_P (ninsn) = 1;
2810 delay_list = add_to_delay_list (ninsn, NULL);
2811 (*pslots_filled)++;
2815 if (delay_list && must_annul)
2816 INSN_ANNULLED_BRANCH_P (insn) = 1;
2818 /* If we are to branch into the middle of this thread, find an appropriate
2819 label or make a new one if none, and redirect INSN to it. If we hit the
2820 end of the function, use the end-of-function label. */
2821 if (new_thread != thread)
2823 rtx label;
2824 bool crossing = false;
2826 gcc_assert (thread_if_true);
2828 if (new_thread && simplejump_or_return_p (new_thread)
2829 && redirect_with_delay_list_safe_p (insn,
2830 JUMP_LABEL (new_thread),
2831 delay_list))
2832 new_thread = follow_jumps (JUMP_LABEL (new_thread), insn,
2833 &crossing);
2835 if (ANY_RETURN_P (new_thread))
2836 label = find_end_label (new_thread);
2837 else if (LABEL_P (new_thread))
2838 label = new_thread;
2839 else
2840 label = get_label_before (as_a <rtx_insn *> (new_thread),
2841 JUMP_LABEL (insn));
2843 if (label)
2845 reorg_redirect_jump (insn, label);
2846 if (crossing)
2847 CROSSING_JUMP_P (insn) = 1;
2851 return delay_list;
2854 /* Make another attempt to find insns to place in delay slots.
2856 We previously looked for insns located in front of the delay insn
2857 and, for non-jump delay insns, located behind the delay insn.
2859 Here only try to schedule jump insns and try to move insns from either
2860 the target or the following insns into the delay slot. If annulling is
2861 supported, we will be likely to do this. Otherwise, we can do this only
2862 if safe. */
2864 static void
2865 fill_eager_delay_slots (void)
2867 rtx_insn *insn;
2868 int i;
2869 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2871 for (i = 0; i < num_unfilled_slots; i++)
2873 rtx condition;
2874 rtx target_label, insn_at_target;
2875 rtx_insn *fallthrough_insn;
2876 rtx_insn_list *delay_list = 0;
2877 rtx_jump_insn *jump_insn;
2878 int own_target;
2879 int own_fallthrough;
2880 int prediction, slots_to_fill, slots_filled;
2882 insn = unfilled_slots_base[i];
2883 if (insn == 0
2884 || insn->deleted ()
2885 || ! (jump_insn = dyn_cast <rtx_jump_insn *> (insn))
2886 || ! (condjump_p (jump_insn) || condjump_in_parallel_p (jump_insn)))
2887 continue;
2889 slots_to_fill = num_delay_slots (jump_insn);
2890 /* Some machine description have defined instructions to have
2891 delay slots only in certain circumstances which may depend on
2892 nearby insns (which change due to reorg's actions).
2894 For example, the PA port normally has delay slots for unconditional
2895 jumps.
2897 However, the PA port claims such jumps do not have a delay slot
2898 if they are immediate successors of certain CALL_INSNs. This
2899 allows the port to favor filling the delay slot of the call with
2900 the unconditional jump. */
2901 if (slots_to_fill == 0)
2902 continue;
2904 slots_filled = 0;
2905 target_label = JUMP_LABEL (jump_insn);
2906 condition = get_branch_condition (jump_insn, target_label);
2908 if (condition == 0)
2909 continue;
2911 /* Get the next active fallthrough and target insns and see if we own
2912 them. Then see whether the branch is likely true. We don't need
2913 to do a lot of this for unconditional branches. */
2915 insn_at_target = first_active_target_insn (target_label);
2916 own_target = own_thread_p (target_label, target_label, 0);
2918 if (condition == const_true_rtx)
2920 own_fallthrough = 0;
2921 fallthrough_insn = 0;
2922 prediction = 2;
2924 else
2926 fallthrough_insn = next_active_insn (jump_insn);
2927 own_fallthrough = own_thread_p (NEXT_INSN (jump_insn), NULL_RTX, 1);
2928 prediction = mostly_true_jump (jump_insn);
2931 /* If this insn is expected to branch, first try to get insns from our
2932 target, then our fallthrough insns. If it is not expected to branch,
2933 try the other order. */
2935 if (prediction > 0)
2937 delay_list
2938 = fill_slots_from_thread (jump_insn, condition, insn_at_target,
2939 fallthrough_insn, prediction == 2, 1,
2940 own_target,
2941 slots_to_fill, &slots_filled, delay_list);
2943 if (delay_list == 0 && own_fallthrough)
2945 /* Even though we didn't find anything for delay slots,
2946 we might have found a redundant insn which we deleted
2947 from the thread that was filled. So we have to recompute
2948 the next insn at the target. */
2949 target_label = JUMP_LABEL (jump_insn);
2950 insn_at_target = first_active_target_insn (target_label);
2952 delay_list
2953 = fill_slots_from_thread (jump_insn, condition,
2954 fallthrough_insn,
2955 insn_at_target, 0, 0,
2956 own_fallthrough,
2957 slots_to_fill, &slots_filled,
2958 delay_list);
2961 else
2963 if (own_fallthrough)
2964 delay_list
2965 = fill_slots_from_thread (jump_insn, condition, fallthrough_insn,
2966 insn_at_target, 0, 0,
2967 own_fallthrough,
2968 slots_to_fill, &slots_filled,
2969 delay_list);
2971 if (delay_list == 0)
2972 delay_list
2973 = fill_slots_from_thread (jump_insn, condition, insn_at_target,
2974 next_active_insn (insn), 0, 1,
2975 own_target,
2976 slots_to_fill, &slots_filled,
2977 delay_list);
2980 if (delay_list)
2981 unfilled_slots_base[i]
2982 = emit_delay_sequence (jump_insn, delay_list, slots_filled);
2984 if (slots_to_fill == slots_filled)
2985 unfilled_slots_base[i] = 0;
2987 note_delay_statistics (slots_filled, 1);
2991 static void delete_computation (rtx insn);
2993 /* Recursively delete prior insns that compute the value (used only by INSN
2994 which the caller is deleting) stored in the register mentioned by NOTE
2995 which is a REG_DEAD note associated with INSN. */
2997 static void
2998 delete_prior_computation (rtx note, rtx insn)
3000 rtx our_prev;
3001 rtx reg = XEXP (note, 0);
3003 for (our_prev = prev_nonnote_insn (insn);
3004 our_prev && (NONJUMP_INSN_P (our_prev)
3005 || CALL_P (our_prev));
3006 our_prev = prev_nonnote_insn (our_prev))
3008 rtx pat = PATTERN (our_prev);
3010 /* If we reach a CALL which is not calling a const function
3011 or the callee pops the arguments, then give up. */
3012 if (CALL_P (our_prev)
3013 && (! RTL_CONST_CALL_P (our_prev)
3014 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
3015 break;
3017 /* If we reach a SEQUENCE, it is too complex to try to
3018 do anything with it, so give up. We can be run during
3019 and after reorg, so SEQUENCE rtl can legitimately show
3020 up here. */
3021 if (GET_CODE (pat) == SEQUENCE)
3022 break;
3024 if (GET_CODE (pat) == USE
3025 && NONJUMP_INSN_P (XEXP (pat, 0)))
3026 /* reorg creates USEs that look like this. We leave them
3027 alone because reorg needs them for its own purposes. */
3028 break;
3030 if (reg_set_p (reg, pat))
3032 if (side_effects_p (pat) && !CALL_P (our_prev))
3033 break;
3035 if (GET_CODE (pat) == PARALLEL)
3037 /* If we find a SET of something else, we can't
3038 delete the insn. */
3040 int i;
3042 for (i = 0; i < XVECLEN (pat, 0); i++)
3044 rtx part = XVECEXP (pat, 0, i);
3046 if (GET_CODE (part) == SET
3047 && SET_DEST (part) != reg)
3048 break;
3051 if (i == XVECLEN (pat, 0))
3052 delete_computation (our_prev);
3054 else if (GET_CODE (pat) == SET
3055 && REG_P (SET_DEST (pat)))
3057 int dest_regno = REGNO (SET_DEST (pat));
3058 int dest_endregno = END_REGNO (SET_DEST (pat));
3059 int regno = REGNO (reg);
3060 int endregno = END_REGNO (reg);
3062 if (dest_regno >= regno
3063 && dest_endregno <= endregno)
3064 delete_computation (our_prev);
3066 /* We may have a multi-word hard register and some, but not
3067 all, of the words of the register are needed in subsequent
3068 insns. Write REG_UNUSED notes for those parts that were not
3069 needed. */
3070 else if (dest_regno <= regno
3071 && dest_endregno >= endregno)
3073 int i;
3075 add_reg_note (our_prev, REG_UNUSED, reg);
3077 for (i = dest_regno; i < dest_endregno; i++)
3078 if (! find_regno_note (our_prev, REG_UNUSED, i))
3079 break;
3081 if (i == dest_endregno)
3082 delete_computation (our_prev);
3086 break;
3089 /* If PAT references the register that dies here, it is an
3090 additional use. Hence any prior SET isn't dead. However, this
3091 insn becomes the new place for the REG_DEAD note. */
3092 if (reg_overlap_mentioned_p (reg, pat))
3094 XEXP (note, 1) = REG_NOTES (our_prev);
3095 REG_NOTES (our_prev) = note;
3096 break;
3101 /* Delete INSN and recursively delete insns that compute values used only
3102 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3104 Look at all our REG_DEAD notes. If a previous insn does nothing other
3105 than set a register that dies in this insn, we can delete that insn
3106 as well.
3108 On machines with CC0, if CC0 is used in this insn, we may be able to
3109 delete the insn that set it. */
3111 static void
3112 delete_computation (rtx insn)
3114 rtx note, next;
3116 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
3118 rtx_insn *prev = prev_nonnote_insn (insn);
3119 /* We assume that at this stage
3120 CC's are always set explicitly
3121 and always immediately before the jump that
3122 will use them. So if the previous insn
3123 exists to set the CC's, delete it
3124 (unless it performs auto-increments, etc.). */
3125 if (prev && NONJUMP_INSN_P (prev)
3126 && sets_cc0_p (PATTERN (prev)))
3128 if (sets_cc0_p (PATTERN (prev)) > 0
3129 && ! side_effects_p (PATTERN (prev)))
3130 delete_computation (prev);
3131 else
3132 /* Otherwise, show that cc0 won't be used. */
3133 add_reg_note (prev, REG_UNUSED, cc0_rtx);
3137 for (note = REG_NOTES (insn); note; note = next)
3139 next = XEXP (note, 1);
3141 if (REG_NOTE_KIND (note) != REG_DEAD
3142 /* Verify that the REG_NOTE is legitimate. */
3143 || !REG_P (XEXP (note, 0)))
3144 continue;
3146 delete_prior_computation (note, insn);
3149 delete_related_insns (insn);
3152 /* If all INSN does is set the pc, delete it,
3153 and delete the insn that set the condition codes for it
3154 if that's what the previous thing was. */
3156 static void
3157 delete_jump (rtx_insn *insn)
3159 rtx set = single_set (insn);
3161 if (set && GET_CODE (SET_DEST (set)) == PC)
3162 delete_computation (insn);
3165 static rtx_insn *
3166 label_before_next_insn (rtx x, rtx scan_limit)
3168 rtx_insn *insn = next_active_insn (x);
3169 while (insn)
3171 insn = PREV_INSN (insn);
3172 if (insn == scan_limit || insn == NULL_RTX)
3173 return NULL;
3174 if (LABEL_P (insn))
3175 break;
3177 return insn;
3180 /* Return TRUE if there is a NOTE_INSN_SWITCH_TEXT_SECTIONS note in between
3181 BEG and END. */
3183 static bool
3184 switch_text_sections_between_p (const rtx_insn *beg, const rtx_insn *end)
3186 const rtx_insn *p;
3187 for (p = beg; p != end; p = NEXT_INSN (p))
3188 if (NOTE_P (p) && NOTE_KIND (p) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
3189 return true;
3190 return false;
3194 /* Once we have tried two ways to fill a delay slot, make a pass over the
3195 code to try to improve the results and to do such things as more jump
3196 threading. */
3198 static void
3199 relax_delay_slots (rtx_insn *first)
3201 rtx_insn *insn, *next;
3202 rtx_sequence *pat;
3203 rtx trial;
3204 rtx_insn *delay_insn;
3205 rtx target_label;
3207 /* Look at every JUMP_INSN and see if we can improve it. */
3208 for (insn = first; insn; insn = next)
3210 rtx_insn *other;
3211 bool crossing;
3213 next = next_active_insn (insn);
3215 /* If this is a jump insn, see if it now jumps to a jump, jumps to
3216 the next insn, or jumps to a label that is not the last of a
3217 group of consecutive labels. */
3218 if (is_a <rtx_jump_insn *> (insn)
3219 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3220 && !ANY_RETURN_P (target_label = JUMP_LABEL (insn)))
3222 rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (insn);
3223 target_label
3224 = skip_consecutive_labels (follow_jumps (target_label, jump_insn,
3225 &crossing));
3226 if (ANY_RETURN_P (target_label))
3227 target_label = find_end_label (target_label);
3229 if (target_label && next_active_insn (target_label) == next
3230 && ! condjump_in_parallel_p (jump_insn)
3231 && ! (next && switch_text_sections_between_p (jump_insn, next)))
3233 delete_jump (jump_insn);
3234 continue;
3237 if (target_label && target_label != JUMP_LABEL (jump_insn))
3239 reorg_redirect_jump (jump_insn, target_label);
3240 if (crossing)
3241 CROSSING_JUMP_P (jump_insn) = 1;
3244 /* See if this jump conditionally branches around an unconditional
3245 jump. If so, invert this jump and point it to the target of the
3246 second jump. Check if it's possible on the target. */
3247 if (next && simplejump_or_return_p (next)
3248 && any_condjump_p (jump_insn)
3249 && target_label
3250 && next_active_insn (target_label) == next_active_insn (next)
3251 && no_labels_between_p (jump_insn, next)
3252 && targetm.can_follow_jump (jump_insn, next))
3254 rtx label = JUMP_LABEL (next);
3256 /* Be careful how we do this to avoid deleting code or
3257 labels that are momentarily dead. See similar optimization
3258 in jump.c.
3260 We also need to ensure we properly handle the case when
3261 invert_jump fails. */
3263 ++LABEL_NUSES (target_label);
3264 if (!ANY_RETURN_P (label))
3265 ++LABEL_NUSES (label);
3267 if (invert_jump (jump_insn, label, 1))
3269 delete_related_insns (next);
3270 next = jump_insn;
3273 if (!ANY_RETURN_P (label))
3274 --LABEL_NUSES (label);
3276 if (--LABEL_NUSES (target_label) == 0)
3277 delete_related_insns (target_label);
3279 continue;
3283 /* If this is an unconditional jump and the previous insn is a
3284 conditional jump, try reversing the condition of the previous
3285 insn and swapping our targets. The next pass might be able to
3286 fill the slots.
3288 Don't do this if we expect the conditional branch to be true, because
3289 we would then be making the more common case longer. */
3291 if (simplejump_or_return_p (insn)
3292 && (other = prev_active_insn (insn)) != 0
3293 && any_condjump_p (other)
3294 && no_labels_between_p (other, insn)
3295 && 0 > mostly_true_jump (other))
3297 rtx other_target = JUMP_LABEL (other);
3298 target_label = JUMP_LABEL (insn);
3300 if (invert_jump (as_a <rtx_jump_insn *> (other), target_label, 0))
3301 reorg_redirect_jump (as_a <rtx_jump_insn *> (insn), other_target);
3304 /* Now look only at cases where we have a filled delay slot. */
3305 if (!NONJUMP_INSN_P (insn) || GET_CODE (PATTERN (insn)) != SEQUENCE)
3306 continue;
3308 pat = as_a <rtx_sequence *> (PATTERN (insn));
3309 delay_insn = pat->insn (0);
3311 /* See if the first insn in the delay slot is redundant with some
3312 previous insn. Remove it from the delay slot if so; then set up
3313 to reprocess this insn. */
3314 if (redundant_insn (pat->insn (1), delay_insn, 0))
3316 update_block (pat->insn (1), insn);
3317 delete_from_delay_slot (pat->insn (1));
3318 next = prev_active_insn (next);
3319 continue;
3322 /* See if we have a RETURN insn with a filled delay slot followed
3323 by a RETURN insn with an unfilled a delay slot. If so, we can delete
3324 the first RETURN (but not its delay insn). This gives the same
3325 effect in fewer instructions.
3327 Only do so if optimizing for size since this results in slower, but
3328 smaller code. */
3329 if (optimize_function_for_size_p (cfun)
3330 && ANY_RETURN_P (PATTERN (delay_insn))
3331 && next
3332 && JUMP_P (next)
3333 && PATTERN (next) == PATTERN (delay_insn))
3335 rtx_insn *after;
3336 int i;
3338 /* Delete the RETURN and just execute the delay list insns.
3340 We do this by deleting the INSN containing the SEQUENCE, then
3341 re-emitting the insns separately, and then deleting the RETURN.
3342 This allows the count of the jump target to be properly
3343 decremented.
3345 Note that we need to change the INSN_UID of the re-emitted insns
3346 since it is used to hash the insns for mark_target_live_regs and
3347 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3349 Clear the from target bit, since these insns are no longer
3350 in delay slots. */
3351 for (i = 0; i < XVECLEN (pat, 0); i++)
3352 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3354 trial = PREV_INSN (insn);
3355 delete_related_insns (insn);
3356 gcc_assert (GET_CODE (pat) == SEQUENCE);
3357 add_insn_after (delay_insn, trial, NULL);
3358 after = delay_insn;
3359 for (i = 1; i < pat->len (); i++)
3360 after = emit_copy_of_insn_after (pat->insn (i), after);
3361 delete_scheduled_jump (delay_insn);
3362 continue;
3365 /* Now look only at the cases where we have a filled JUMP_INSN. */
3366 rtx_jump_insn *delay_jump_insn =
3367 dyn_cast <rtx_jump_insn *> (delay_insn);
3368 if (! delay_jump_insn || !(condjump_p (delay_jump_insn)
3369 || condjump_in_parallel_p (delay_jump_insn)))
3370 continue;
3372 target_label = JUMP_LABEL (delay_jump_insn);
3373 if (target_label && ANY_RETURN_P (target_label))
3374 continue;
3376 /* If this jump goes to another unconditional jump, thread it, but
3377 don't convert a jump into a RETURN here. */
3378 trial = skip_consecutive_labels (follow_jumps (target_label,
3379 delay_jump_insn,
3380 &crossing));
3381 if (ANY_RETURN_P (trial))
3382 trial = find_end_label (trial);
3384 if (trial && trial != target_label
3385 && redirect_with_delay_slots_safe_p (delay_jump_insn, trial, insn))
3387 reorg_redirect_jump (delay_jump_insn, trial);
3388 target_label = trial;
3389 if (crossing)
3390 CROSSING_JUMP_P (insn) = 1;
3393 /* If the first insn at TARGET_LABEL is redundant with a previous
3394 insn, redirect the jump to the following insn and process again.
3395 We use next_real_insn instead of next_active_insn so we
3396 don't skip USE-markers, or we'll end up with incorrect
3397 liveness info. */
3398 trial = next_real_insn (target_label);
3399 if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3400 && redundant_insn (trial, insn, 0)
3401 && ! can_throw_internal (trial))
3403 /* Figure out where to emit the special USE insn so we don't
3404 later incorrectly compute register live/death info. */
3405 rtx_insn *tmp = next_active_insn (trial);
3406 if (tmp == 0)
3407 tmp = find_end_label (simple_return_rtx);
3409 if (tmp)
3411 /* Insert the special USE insn and update dataflow info.
3412 We know "trial" is an insn here as it is the output of
3413 next_real_insn () above. */
3414 update_block (as_a <rtx_insn *> (trial), tmp);
3416 /* Now emit a label before the special USE insn, and
3417 redirect our jump to the new label. */
3418 target_label = get_label_before (PREV_INSN (tmp), target_label);
3419 reorg_redirect_jump (delay_jump_insn, target_label);
3420 next = insn;
3421 continue;
3425 /* Similarly, if it is an unconditional jump with one insn in its
3426 delay list and that insn is redundant, thread the jump. */
3427 rtx_sequence *trial_seq =
3428 trial ? dyn_cast <rtx_sequence *> (PATTERN (trial)) : NULL;
3429 if (trial_seq
3430 && trial_seq->len () == 2
3431 && JUMP_P (trial_seq->insn (0))
3432 && simplejump_or_return_p (trial_seq->insn (0))
3433 && redundant_insn (trial_seq->insn (1), insn, 0))
3435 target_label = JUMP_LABEL (trial_seq->insn (0));
3436 if (ANY_RETURN_P (target_label))
3437 target_label = find_end_label (target_label);
3439 if (target_label
3440 && redirect_with_delay_slots_safe_p (delay_jump_insn,
3441 target_label, insn))
3443 update_block (trial_seq->insn (1), insn);
3444 reorg_redirect_jump (delay_jump_insn, target_label);
3445 next = insn;
3446 continue;
3450 /* See if we have a simple (conditional) jump that is useless. */
3451 if (! INSN_ANNULLED_BRANCH_P (delay_jump_insn)
3452 && ! condjump_in_parallel_p (delay_jump_insn)
3453 && prev_active_insn (target_label) == insn
3454 && ! BARRIER_P (prev_nonnote_insn (target_label))
3455 #if HAVE_cc0
3456 /* If the last insn in the delay slot sets CC0 for some insn,
3457 various code assumes that it is in a delay slot. We could
3458 put it back where it belonged and delete the register notes,
3459 but it doesn't seem worthwhile in this uncommon case. */
3460 && ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3461 REG_CC_USER, NULL_RTX)
3462 #endif
3465 rtx_insn *after;
3466 int i;
3468 /* All this insn does is execute its delay list and jump to the
3469 following insn. So delete the jump and just execute the delay
3470 list insns.
3472 We do this by deleting the INSN containing the SEQUENCE, then
3473 re-emitting the insns separately, and then deleting the jump.
3474 This allows the count of the jump target to be properly
3475 decremented.
3477 Note that we need to change the INSN_UID of the re-emitted insns
3478 since it is used to hash the insns for mark_target_live_regs and
3479 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3481 Clear the from target bit, since these insns are no longer
3482 in delay slots. */
3483 for (i = 0; i < XVECLEN (pat, 0); i++)
3484 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3486 trial = PREV_INSN (insn);
3487 delete_related_insns (insn);
3488 gcc_assert (GET_CODE (pat) == SEQUENCE);
3489 add_insn_after (delay_jump_insn, trial, NULL);
3490 after = delay_jump_insn;
3491 for (i = 1; i < pat->len (); i++)
3492 after = emit_copy_of_insn_after (pat->insn (i), after);
3493 delete_scheduled_jump (delay_jump_insn);
3494 continue;
3497 /* See if this is an unconditional jump around a single insn which is
3498 identical to the one in its delay slot. In this case, we can just
3499 delete the branch and the insn in its delay slot. */
3500 if (next && NONJUMP_INSN_P (next)
3501 && label_before_next_insn (next, insn) == target_label
3502 && simplejump_p (insn)
3503 && XVECLEN (pat, 0) == 2
3504 && rtx_equal_p (PATTERN (next), PATTERN (pat->insn (1))))
3506 delete_related_insns (insn);
3507 continue;
3510 /* See if this jump (with its delay slots) conditionally branches
3511 around an unconditional jump (without delay slots). If so, invert
3512 this jump and point it to the target of the second jump. We cannot
3513 do this for annulled jumps, though. Again, don't convert a jump to
3514 a RETURN here. */
3515 if (! INSN_ANNULLED_BRANCH_P (delay_jump_insn)
3516 && any_condjump_p (delay_jump_insn)
3517 && next && simplejump_or_return_p (next)
3518 && next_active_insn (target_label) == next_active_insn (next)
3519 && no_labels_between_p (insn, next))
3521 rtx label = JUMP_LABEL (next);
3522 rtx old_label = JUMP_LABEL (delay_jump_insn);
3524 if (ANY_RETURN_P (label))
3525 label = find_end_label (label);
3527 /* find_end_label can generate a new label. Check this first. */
3528 if (label
3529 && no_labels_between_p (insn, next)
3530 && redirect_with_delay_slots_safe_p (delay_jump_insn,
3531 label, insn))
3533 /* Be careful how we do this to avoid deleting code or labels
3534 that are momentarily dead. See similar optimization in
3535 jump.c */
3536 if (old_label)
3537 ++LABEL_NUSES (old_label);
3539 if (invert_jump (delay_jump_insn, label, 1))
3541 int i;
3543 /* Must update the INSN_FROM_TARGET_P bits now that
3544 the branch is reversed, so that mark_target_live_regs
3545 will handle the delay slot insn correctly. */
3546 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
3548 rtx slot = XVECEXP (PATTERN (insn), 0, i);
3549 INSN_FROM_TARGET_P (slot) = ! INSN_FROM_TARGET_P (slot);
3552 delete_related_insns (next);
3553 next = insn;
3556 if (old_label && --LABEL_NUSES (old_label) == 0)
3557 delete_related_insns (old_label);
3558 continue;
3562 /* If we own the thread opposite the way this insn branches, see if we
3563 can merge its delay slots with following insns. */
3564 if (INSN_FROM_TARGET_P (pat->insn (1))
3565 && own_thread_p (NEXT_INSN (insn), 0, 1))
3566 try_merge_delay_insns (insn, next);
3567 else if (! INSN_FROM_TARGET_P (pat->insn (1))
3568 && own_thread_p (target_label, target_label, 0))
3569 try_merge_delay_insns (insn, next_active_insn (target_label));
3571 /* If we get here, we haven't deleted INSN. But we may have deleted
3572 NEXT, so recompute it. */
3573 next = next_active_insn (insn);
3578 /* Look for filled jumps to the end of function label. We can try to convert
3579 them into RETURN insns if the insns in the delay slot are valid for the
3580 RETURN as well. */
3582 static void
3583 make_return_insns (rtx_insn *first)
3585 rtx_insn *insn;
3586 rtx_jump_insn *jump_insn;
3587 rtx real_return_label = function_return_label;
3588 rtx real_simple_return_label = function_simple_return_label;
3589 int slots, i;
3591 /* See if there is a RETURN insn in the function other than the one we
3592 made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3593 into a RETURN to jump to it. */
3594 for (insn = first; insn; insn = NEXT_INSN (insn))
3595 if (JUMP_P (insn) && ANY_RETURN_P (PATTERN (insn)))
3597 rtx t = get_label_before (insn, NULL_RTX);
3598 if (PATTERN (insn) == ret_rtx)
3599 real_return_label = t;
3600 else
3601 real_simple_return_label = t;
3602 break;
3605 /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3606 was equal to END_OF_FUNCTION_LABEL. */
3607 if (real_return_label)
3608 LABEL_NUSES (real_return_label)++;
3609 if (real_simple_return_label)
3610 LABEL_NUSES (real_simple_return_label)++;
3612 /* Clear the list of insns to fill so we can use it. */
3613 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3615 for (insn = first; insn; insn = NEXT_INSN (insn))
3617 int flags;
3618 rtx kind, real_label;
3620 /* Only look at filled JUMP_INSNs that go to the end of function
3621 label. */
3622 if (!NONJUMP_INSN_P (insn))
3623 continue;
3625 if (GET_CODE (PATTERN (insn)) != SEQUENCE)
3626 continue;
3628 rtx_sequence *pat = as_a <rtx_sequence *> (PATTERN (insn));
3630 if (!jump_to_label_p (pat->insn (0)))
3631 continue;
3633 if (JUMP_LABEL (pat->insn (0)) == function_return_label)
3635 kind = ret_rtx;
3636 real_label = real_return_label;
3638 else if (JUMP_LABEL (pat->insn (0)) == function_simple_return_label)
3640 kind = simple_return_rtx;
3641 real_label = real_simple_return_label;
3643 else
3644 continue;
3646 jump_insn = as_a <rtx_jump_insn *> (pat->insn (0));
3648 /* If we can't make the jump into a RETURN, try to redirect it to the best
3649 RETURN and go on to the next insn. */
3650 if (!reorg_redirect_jump (jump_insn, kind))
3652 /* Make sure redirecting the jump will not invalidate the delay
3653 slot insns. */
3654 if (redirect_with_delay_slots_safe_p (jump_insn, real_label, insn))
3655 reorg_redirect_jump (jump_insn, real_label);
3656 continue;
3659 /* See if this RETURN can accept the insns current in its delay slot.
3660 It can if it has more or an equal number of slots and the contents
3661 of each is valid. */
3663 flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3664 slots = num_delay_slots (jump_insn);
3665 if (slots >= XVECLEN (pat, 0) - 1)
3667 for (i = 1; i < XVECLEN (pat, 0); i++)
3668 if (! (
3669 #ifdef ANNUL_IFFALSE_SLOTS
3670 (INSN_ANNULLED_BRANCH_P (jump_insn)
3671 && INSN_FROM_TARGET_P (pat->insn (i)))
3672 ? eligible_for_annul_false (jump_insn, i - 1,
3673 pat->insn (i), flags) :
3674 #endif
3675 #ifdef ANNUL_IFTRUE_SLOTS
3676 (INSN_ANNULLED_BRANCH_P (jump_insn)
3677 && ! INSN_FROM_TARGET_P (pat->insn (i)))
3678 ? eligible_for_annul_true (jump_insn, i - 1,
3679 pat->insn (i), flags) :
3680 #endif
3681 eligible_for_delay (jump_insn, i - 1,
3682 pat->insn (i), flags)))
3683 break;
3685 else
3686 i = 0;
3688 if (i == XVECLEN (pat, 0))
3689 continue;
3691 /* We have to do something with this insn. If it is an unconditional
3692 RETURN, delete the SEQUENCE and output the individual insns,
3693 followed by the RETURN. Then set things up so we try to find
3694 insns for its delay slots, if it needs some. */
3695 if (ANY_RETURN_P (PATTERN (jump_insn)))
3697 rtx_insn *prev = PREV_INSN (insn);
3699 delete_related_insns (insn);
3700 for (i = 1; i < XVECLEN (pat, 0); i++)
3701 prev = emit_insn_after (PATTERN (XVECEXP (pat, 0, i)), prev);
3703 insn = emit_jump_insn_after (PATTERN (jump_insn), prev);
3704 emit_barrier_after (insn);
3706 if (slots)
3707 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3709 else
3710 /* It is probably more efficient to keep this with its current
3711 delay slot as a branch to a RETURN. */
3712 reorg_redirect_jump (jump_insn, real_label);
3715 /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3716 new delay slots we have created. */
3717 if (real_return_label != NULL_RTX && --LABEL_NUSES (real_return_label) == 0)
3718 delete_related_insns (real_return_label);
3719 if (real_simple_return_label != NULL_RTX
3720 && --LABEL_NUSES (real_simple_return_label) == 0)
3721 delete_related_insns (real_simple_return_label);
3723 fill_simple_delay_slots (1);
3724 fill_simple_delay_slots (0);
3727 /* Try to find insns to place in delay slots. */
3729 static void
3730 dbr_schedule (rtx_insn *first)
3732 rtx_insn *insn, *next, *epilogue_insn = 0;
3733 int i;
3734 bool need_return_insns;
3736 /* If the current function has no insns other than the prologue and
3737 epilogue, then do not try to fill any delay slots. */
3738 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
3739 return;
3741 /* Find the highest INSN_UID and allocate and initialize our map from
3742 INSN_UID's to position in code. */
3743 for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3745 if (INSN_UID (insn) > max_uid)
3746 max_uid = INSN_UID (insn);
3747 if (NOTE_P (insn)
3748 && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
3749 epilogue_insn = insn;
3752 uid_to_ruid = XNEWVEC (int, max_uid + 1);
3753 for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3754 uid_to_ruid[INSN_UID (insn)] = i;
3756 /* Initialize the list of insns that need filling. */
3757 if (unfilled_firstobj == 0)
3759 gcc_obstack_init (&unfilled_slots_obstack);
3760 unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3763 for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3765 rtx target;
3767 /* Skip vector tables. We can't get attributes for them. */
3768 if (JUMP_TABLE_DATA_P (insn))
3769 continue;
3771 if (JUMP_P (insn))
3772 INSN_ANNULLED_BRANCH_P (insn) = 0;
3773 INSN_FROM_TARGET_P (insn) = 0;
3775 if (num_delay_slots (insn) > 0)
3776 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3778 /* Ensure all jumps go to the last of a set of consecutive labels. */
3779 if (JUMP_P (insn)
3780 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3781 && !ANY_RETURN_P (JUMP_LABEL (insn))
3782 && ((target = skip_consecutive_labels (JUMP_LABEL (insn)))
3783 != JUMP_LABEL (insn)))
3784 redirect_jump (as_a <rtx_jump_insn *> (insn), target, 1);
3787 init_resource_info (epilogue_insn);
3789 /* Show we haven't computed an end-of-function label yet. */
3790 function_return_label = function_simple_return_label = NULL;
3792 /* Initialize the statistics for this function. */
3793 memset (num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3794 memset (num_filled_delays, 0, sizeof num_filled_delays);
3796 /* Now do the delay slot filling. Try everything twice in case earlier
3797 changes make more slots fillable. */
3799 for (reorg_pass_number = 0;
3800 reorg_pass_number < MAX_REORG_PASSES;
3801 reorg_pass_number++)
3803 fill_simple_delay_slots (1);
3804 fill_simple_delay_slots (0);
3805 fill_eager_delay_slots ();
3806 relax_delay_slots (first);
3809 /* If we made an end of function label, indicate that it is now
3810 safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3811 If it is now unused, delete it. */
3812 if (function_return_label && --LABEL_NUSES (function_return_label) == 0)
3813 delete_related_insns (function_return_label);
3814 if (function_simple_return_label
3815 && --LABEL_NUSES (function_simple_return_label) == 0)
3816 delete_related_insns (function_simple_return_label);
3818 need_return_insns = false;
3819 need_return_insns |= HAVE_return && function_return_label != 0;
3820 need_return_insns |= HAVE_simple_return && function_simple_return_label != 0;
3821 if (need_return_insns)
3822 make_return_insns (first);
3824 /* Delete any USE insns made by update_block; subsequent passes don't need
3825 them or know how to deal with them. */
3826 for (insn = first; insn; insn = next)
3828 next = NEXT_INSN (insn);
3830 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
3831 && INSN_P (XEXP (PATTERN (insn), 0)))
3832 next = delete_related_insns (insn);
3835 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3837 /* It is not clear why the line below is needed, but it does seem to be. */
3838 unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3840 if (dump_file)
3842 int i, j, need_comma;
3843 int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3844 int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
3846 for (reorg_pass_number = 0;
3847 reorg_pass_number < MAX_REORG_PASSES;
3848 reorg_pass_number++)
3850 fprintf (dump_file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
3851 for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
3853 need_comma = 0;
3854 fprintf (dump_file, ";; Reorg function #%d\n", i);
3856 fprintf (dump_file, ";; %d insns needing delay slots\n;; ",
3857 num_insns_needing_delays[i][reorg_pass_number]);
3859 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3860 if (num_filled_delays[i][j][reorg_pass_number])
3862 if (need_comma)
3863 fprintf (dump_file, ", ");
3864 need_comma = 1;
3865 fprintf (dump_file, "%d got %d delays",
3866 num_filled_delays[i][j][reorg_pass_number], j);
3868 fprintf (dump_file, "\n");
3871 memset (total_delay_slots, 0, sizeof total_delay_slots);
3872 memset (total_annul_slots, 0, sizeof total_annul_slots);
3873 for (insn = first; insn; insn = NEXT_INSN (insn))
3875 if (! insn->deleted ()
3876 && NONJUMP_INSN_P (insn)
3877 && GET_CODE (PATTERN (insn)) != USE
3878 && GET_CODE (PATTERN (insn)) != CLOBBER)
3880 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3882 rtx control;
3883 j = XVECLEN (PATTERN (insn), 0) - 1;
3884 if (j > MAX_DELAY_HISTOGRAM)
3885 j = MAX_DELAY_HISTOGRAM;
3886 control = XVECEXP (PATTERN (insn), 0, 0);
3887 if (JUMP_P (control) && INSN_ANNULLED_BRANCH_P (control))
3888 total_annul_slots[j]++;
3889 else
3890 total_delay_slots[j]++;
3892 else if (num_delay_slots (insn) > 0)
3893 total_delay_slots[0]++;
3896 fprintf (dump_file, ";; Reorg totals: ");
3897 need_comma = 0;
3898 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3900 if (total_delay_slots[j])
3902 if (need_comma)
3903 fprintf (dump_file, ", ");
3904 need_comma = 1;
3905 fprintf (dump_file, "%d got %d delays", total_delay_slots[j], j);
3908 fprintf (dump_file, "\n");
3909 #if defined (ANNUL_IFTRUE_SLOTS) || defined (ANNUL_IFFALSE_SLOTS)
3910 fprintf (dump_file, ";; Reorg annuls: ");
3911 need_comma = 0;
3912 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3914 if (total_annul_slots[j])
3916 if (need_comma)
3917 fprintf (dump_file, ", ");
3918 need_comma = 1;
3919 fprintf (dump_file, "%d got %d delays", total_annul_slots[j], j);
3922 fprintf (dump_file, "\n");
3923 #endif
3924 fprintf (dump_file, "\n");
3927 if (!sibling_labels.is_empty ())
3929 update_alignments (sibling_labels);
3930 sibling_labels.release ();
3933 free_resource_info ();
3934 free (uid_to_ruid);
3935 crtl->dbr_scheduled_p = true;
3937 #endif /* DELAY_SLOTS */
3939 /* Run delay slot optimization. */
3940 static unsigned int
3941 rest_of_handle_delay_slots (void)
3943 #ifdef DELAY_SLOTS
3944 dbr_schedule (get_insns ());
3945 #endif
3946 return 0;
3949 namespace {
3951 const pass_data pass_data_delay_slots =
3953 RTL_PASS, /* type */
3954 "dbr", /* name */
3955 OPTGROUP_NONE, /* optinfo_flags */
3956 TV_DBR_SCHED, /* tv_id */
3957 0, /* properties_required */
3958 0, /* properties_provided */
3959 0, /* properties_destroyed */
3960 0, /* todo_flags_start */
3961 0, /* todo_flags_finish */
3964 class pass_delay_slots : public rtl_opt_pass
3966 public:
3967 pass_delay_slots (gcc::context *ctxt)
3968 : rtl_opt_pass (pass_data_delay_slots, ctxt)
3971 /* opt_pass methods: */
3972 virtual bool gate (function *);
3973 virtual unsigned int execute (function *)
3975 return rest_of_handle_delay_slots ();
3978 }; // class pass_delay_slots
3980 bool
3981 pass_delay_slots::gate (function *)
3983 #ifdef DELAY_SLOTS
3984 /* At -O0 dataflow info isn't updated after RA. */
3985 return optimize > 0 && flag_delayed_branch && !crtl->dbr_scheduled_p;
3986 #else
3987 return 0;
3988 #endif
3991 } // anon namespace
3993 rtl_opt_pass *
3994 make_pass_delay_slots (gcc::context *ctxt)
3996 return new pass_delay_slots (ctxt);
3999 /* Machine dependent reorg pass. */
4001 namespace {
4003 const pass_data pass_data_machine_reorg =
4005 RTL_PASS, /* type */
4006 "mach", /* name */
4007 OPTGROUP_NONE, /* optinfo_flags */
4008 TV_MACH_DEP, /* tv_id */
4009 0, /* properties_required */
4010 0, /* properties_provided */
4011 0, /* properties_destroyed */
4012 0, /* todo_flags_start */
4013 0, /* todo_flags_finish */
4016 class pass_machine_reorg : public rtl_opt_pass
4018 public:
4019 pass_machine_reorg (gcc::context *ctxt)
4020 : rtl_opt_pass (pass_data_machine_reorg, ctxt)
4023 /* opt_pass methods: */
4024 virtual bool gate (function *)
4026 return targetm.machine_dependent_reorg != 0;
4029 virtual unsigned int execute (function *)
4031 targetm.machine_dependent_reorg ();
4032 return 0;
4035 }; // class pass_machine_reorg
4037 } // anon namespace
4039 rtl_opt_pass *
4040 make_pass_machine_reorg (gcc::context *ctxt)
4042 return new pass_machine_reorg (ctxt);