1 /* Perform instruction reorganizations for delay slot filling.
2 Copyright (C) 1992-2018 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
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
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
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
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). */
105 #include "coretypes.h"
111 #include "memmodel.h"
114 #include "insn-config.h"
115 #include "emit-rtl.h"
117 #include "insn-attr.h"
118 #include "resource.h"
120 #include "tree-pass.h"
123 /* First, some functions that were used before GCC got a control flow graph.
124 These functions are now only used here in reorg.c, and have therefore
125 been moved here to avoid inadvertent misuse elsewhere in the compiler. */
127 /* Return the last label to mark the same position as LABEL. Return LABEL
128 itself if it is null or any return rtx. */
131 skip_consecutive_labels (rtx label_or_return
)
135 if (label_or_return
&& ANY_RETURN_P (label_or_return
))
136 return label_or_return
;
138 rtx_insn
*label
= as_a
<rtx_insn
*> (label_or_return
);
140 for (insn
= label
; insn
!= 0 && !INSN_P (insn
); insn
= NEXT_INSN (insn
))
147 /* INSN uses CC0 and is being moved into a delay slot. Set up REG_CC_SETTER
148 and REG_CC_USER notes so we can find it. */
151 link_cc0_insns (rtx_insn
*insn
)
153 rtx user
= next_nonnote_insn (insn
);
155 if (NONJUMP_INSN_P (user
) && GET_CODE (PATTERN (user
)) == SEQUENCE
)
156 user
= XVECEXP (PATTERN (user
), 0, 0);
158 add_reg_note (user
, REG_CC_SETTER
, insn
);
159 add_reg_note (insn
, REG_CC_USER
, user
);
162 /* Insns which have delay slots that have not yet been filled. */
164 static struct obstack unfilled_slots_obstack
;
165 static rtx
*unfilled_firstobj
;
167 /* Define macros to refer to the first and last slot containing unfilled
168 insns. These are used because the list may move and its address
169 should be recomputed at each use. */
171 #define unfilled_slots_base \
172 ((rtx_insn **) obstack_base (&unfilled_slots_obstack))
174 #define unfilled_slots_next \
175 ((rtx_insn **) obstack_next_free (&unfilled_slots_obstack))
177 /* Points to the label before the end of the function, or before a
179 static rtx_code_label
*function_return_label
;
180 /* Likewise for a simple_return. */
181 static rtx_code_label
*function_simple_return_label
;
183 /* Mapping between INSN_UID's and position in the code since INSN_UID's do
184 not always monotonically increase. */
185 static int *uid_to_ruid
;
187 /* Highest valid index in `uid_to_ruid'. */
190 static int stop_search_p (rtx_insn
*, int);
191 static int resource_conflicts_p (struct resources
*, struct resources
*);
192 static int insn_references_resource_p (rtx
, struct resources
*, bool);
193 static int insn_sets_resource_p (rtx
, struct resources
*, bool);
194 static rtx_code_label
*find_end_label (rtx
);
195 static rtx_insn
*emit_delay_sequence (rtx_insn
*, const vec
<rtx_insn
*> &,
197 static void add_to_delay_list (rtx_insn
*, vec
<rtx_insn
*> *);
198 static rtx_insn
*delete_from_delay_slot (rtx_insn
*);
199 static void delete_scheduled_jump (rtx_insn
*);
200 static void note_delay_statistics (int, int);
201 static int get_jump_flags (const rtx_insn
*, rtx
);
202 static int mostly_true_jump (rtx
);
203 static rtx
get_branch_condition (const rtx_insn
*, rtx
);
204 static int condition_dominates_p (rtx
, const rtx_insn
*);
205 static int redirect_with_delay_slots_safe_p (rtx_insn
*, rtx
, rtx
);
206 static int redirect_with_delay_list_safe_p (rtx_insn
*, rtx
,
207 const vec
<rtx_insn
*> &);
208 static int check_annul_list_true_false (int, const vec
<rtx_insn
*> &);
209 static void steal_delay_list_from_target (rtx_insn
*, rtx
, rtx_sequence
*,
216 static void steal_delay_list_from_fallthrough (rtx_insn
*, rtx
, rtx_sequence
*,
222 static void try_merge_delay_insns (rtx_insn
*, rtx_insn
*);
223 static rtx_insn
*redundant_insn (rtx
, rtx_insn
*, const vec
<rtx_insn
*> &);
224 static int own_thread_p (rtx
, rtx
, int);
225 static void update_block (rtx_insn
*, rtx_insn
*);
226 static int reorg_redirect_jump (rtx_jump_insn
*, rtx
);
227 static void update_reg_dead_notes (rtx_insn
*, rtx_insn
*);
228 static void fix_reg_dead_note (rtx_insn
*, rtx
);
229 static void update_reg_unused_notes (rtx_insn
*, rtx
);
230 static void fill_simple_delay_slots (int);
231 static void fill_slots_from_thread (rtx_jump_insn
*, rtx
, rtx
, rtx
,
233 int *, vec
<rtx_insn
*> *);
234 static void fill_eager_delay_slots (void);
235 static void relax_delay_slots (rtx_insn
*);
236 static void make_return_insns (rtx_insn
*);
238 /* A wrapper around next_active_insn which takes care to return ret_rtx
242 first_active_target_insn (rtx insn
)
244 if (ANY_RETURN_P (insn
))
246 return next_active_insn (as_a
<rtx_insn
*> (insn
));
249 /* Return true iff INSN is a simplejump, or any kind of return insn. */
252 simplejump_or_return_p (rtx insn
)
254 return (JUMP_P (insn
)
255 && (simplejump_p (as_a
<rtx_insn
*> (insn
))
256 || ANY_RETURN_P (PATTERN (insn
))));
259 /* Return TRUE if this insn should stop the search for insn to fill delay
260 slots. LABELS_P indicates that labels should terminate the search.
261 In all cases, jumps terminate the search. */
264 stop_search_p (rtx_insn
*insn
, int labels_p
)
269 /* If the insn can throw an exception that is caught within the function,
270 it may effectively perform a jump from the viewpoint of the function.
271 Therefore act like for a jump. */
272 if (can_throw_internal (insn
))
275 switch (GET_CODE (insn
))
290 /* OK unless it contains a delay slot or is an `asm' insn of some type.
291 We don't know anything about these. */
292 return (GET_CODE (PATTERN (insn
)) == SEQUENCE
293 || GET_CODE (PATTERN (insn
)) == ASM_INPUT
294 || asm_noperands (PATTERN (insn
)) >= 0);
301 /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
302 resource set contains a volatile memory reference. Otherwise, return FALSE. */
305 resource_conflicts_p (struct resources
*res1
, struct resources
*res2
)
307 if ((res1
->cc
&& res2
->cc
) || (res1
->memory
&& res2
->memory
)
308 || res1
->volatil
|| res2
->volatil
)
311 return hard_reg_set_intersect_p (res1
->regs
, res2
->regs
);
314 /* Return TRUE if any resource marked in RES, a `struct resources', is
315 referenced by INSN. If INCLUDE_DELAYED_EFFECTS is set, return if the called
316 routine is using those resources.
318 We compute this by computing all the resources referenced by INSN and
319 seeing if this conflicts with RES. It might be faster to directly check
320 ourselves, and this is the way it used to work, but it means duplicating
321 a large block of complex code. */
324 insn_references_resource_p (rtx insn
, struct resources
*res
,
325 bool include_delayed_effects
)
327 struct resources insn_res
;
329 CLEAR_RESOURCE (&insn_res
);
330 mark_referenced_resources (insn
, &insn_res
, include_delayed_effects
);
331 return resource_conflicts_p (&insn_res
, res
);
334 /* Return TRUE if INSN modifies resources that are marked in RES.
335 INCLUDE_DELAYED_EFFECTS is set if the actions of that routine should be
336 included. CC0 is only modified if it is explicitly set; see comments
337 in front of mark_set_resources for details. */
340 insn_sets_resource_p (rtx insn
, struct resources
*res
,
341 bool include_delayed_effects
)
343 struct resources insn_sets
;
345 CLEAR_RESOURCE (&insn_sets
);
346 mark_set_resources (insn
, &insn_sets
, 0,
347 (include_delayed_effects
350 return resource_conflicts_p (&insn_sets
, res
);
353 /* Find a label at the end of the function or before a RETURN. If there
354 is none, try to make one. If that fails, returns 0.
356 The property of such a label is that it is placed just before the
357 epilogue or a bare RETURN insn, so that another bare RETURN can be
358 turned into a jump to the label unconditionally. In particular, the
359 label cannot be placed before a RETURN insn with a filled delay slot.
361 ??? There may be a problem with the current implementation. Suppose
362 we start with a bare RETURN insn and call find_end_label. It may set
363 function_return_label just before the RETURN. Suppose the machinery
364 is able to fill the delay slot of the RETURN insn afterwards. Then
365 function_return_label is no longer valid according to the property
366 described above and find_end_label will still return it unmodified.
367 Note that this is probably mitigated by the following observation:
368 once function_return_label is made, it is very likely the target of
369 a jump, so filling the delay slot of the RETURN will be much more
371 KIND is either simple_return_rtx or ret_rtx, indicating which type of
372 return we're looking for. */
374 static rtx_code_label
*
375 find_end_label (rtx kind
)
378 rtx_code_label
**plabel
;
381 plabel
= &function_return_label
;
384 gcc_assert (kind
== simple_return_rtx
);
385 plabel
= &function_simple_return_label
;
388 /* If we found one previously, return it. */
392 /* Otherwise, see if there is a label at the end of the function. If there
393 is, it must be that RETURN insns aren't needed, so that is our return
394 label and we don't have to do anything else. */
396 insn
= get_last_insn ();
398 || (NONJUMP_INSN_P (insn
)
399 && (GET_CODE (PATTERN (insn
)) == USE
400 || GET_CODE (PATTERN (insn
)) == CLOBBER
401 || GET_CODE (PATTERN (insn
)) == CLOBBER_HIGH
)))
402 insn
= PREV_INSN (insn
);
404 /* When a target threads its epilogue we might already have a
405 suitable return insn. If so put a label before it for the
406 function_return_label. */
408 && JUMP_P (PREV_INSN (insn
))
409 && PATTERN (PREV_INSN (insn
)) == kind
)
411 rtx_insn
*temp
= PREV_INSN (PREV_INSN (insn
));
412 rtx_code_label
*label
= gen_label_rtx ();
413 LABEL_NUSES (label
) = 0;
415 /* Put the label before any USE insns that may precede the RETURN
417 while (GET_CODE (temp
) == USE
)
418 temp
= PREV_INSN (temp
);
420 emit_label_after (label
, temp
);
424 else if (LABEL_P (insn
))
425 *plabel
= as_a
<rtx_code_label
*> (insn
);
428 rtx_code_label
*label
= gen_label_rtx ();
429 LABEL_NUSES (label
) = 0;
430 /* If the basic block reorder pass moves the return insn to
431 some other place try to locate it again and put our
432 function_return_label there. */
433 while (insn
&& ! (JUMP_P (insn
) && (PATTERN (insn
) == kind
)))
434 insn
= PREV_INSN (insn
);
437 insn
= PREV_INSN (insn
);
439 /* Put the label before any USE insns that may precede the
441 while (GET_CODE (insn
) == USE
)
442 insn
= PREV_INSN (insn
);
444 emit_label_after (label
, insn
);
448 if (targetm
.have_epilogue () && ! targetm
.have_return ())
449 /* The RETURN insn has its delay slot filled so we cannot
450 emit the label just before it. Since we already have
451 an epilogue and cannot emit a new RETURN, we cannot
452 emit the label at all. */
455 /* Otherwise, make a new label and emit a RETURN and BARRIER,
458 if (targetm
.have_return ())
460 /* The return we make may have delay slots too. */
461 rtx_insn
*pat
= targetm
.gen_return ();
462 rtx_insn
*insn
= emit_jump_insn (pat
);
463 set_return_jump_label (insn
);
465 if (num_delay_slots (insn
) > 0)
466 obstack_ptr_grow (&unfilled_slots_obstack
, insn
);
472 /* Show one additional use for this label so it won't go away until
474 ++LABEL_NUSES (*plabel
);
479 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
480 the pattern of INSN with the SEQUENCE.
482 Returns the insn containing the SEQUENCE that replaces INSN. */
485 emit_delay_sequence (rtx_insn
*insn
, const vec
<rtx_insn
*> &list
, int length
)
487 /* Allocate the rtvec to hold the insns and the SEQUENCE. */
488 rtvec seqv
= rtvec_alloc (length
+ 1);
489 rtx seq
= gen_rtx_SEQUENCE (VOIDmode
, seqv
);
490 rtx_insn
*seq_insn
= make_insn_raw (seq
);
492 /* If DELAY_INSN has a location, use it for SEQ_INSN. If DELAY_INSN does
493 not have a location, but one of the delayed insns does, we pick up a
494 location from there later. */
495 INSN_LOCATION (seq_insn
) = INSN_LOCATION (insn
);
497 /* Unlink INSN from the insn chain, so that we can put it into
498 the SEQUENCE. Remember where we want to emit SEQUENCE in AFTER. */
499 rtx_insn
*after
= PREV_INSN (insn
);
501 SET_NEXT_INSN (insn
) = SET_PREV_INSN (insn
) = NULL
;
503 /* Build our SEQUENCE and rebuild the insn chain. */
505 XVECEXP (seq
, 0, 0) = emit_insn (insn
);
507 unsigned int delay_insns
= list
.length ();
508 gcc_assert (delay_insns
== (unsigned int) length
);
509 for (unsigned int i
= 0; i
< delay_insns
; i
++)
511 rtx_insn
*tem
= list
[i
];
514 /* Show that this copy of the insn isn't deleted. */
515 tem
->set_undeleted ();
517 /* Unlink insn from its original place, and re-emit it into
519 SET_NEXT_INSN (tem
) = SET_PREV_INSN (tem
) = NULL
;
520 XVECEXP (seq
, 0, i
+ 1) = emit_insn (tem
);
522 /* SPARC assembler, for instance, emit warning when debug info is output
523 into the delay slot. */
524 if (INSN_LOCATION (tem
) && !INSN_LOCATION (seq_insn
))
525 INSN_LOCATION (seq_insn
) = INSN_LOCATION (tem
);
526 INSN_LOCATION (tem
) = 0;
528 for (note
= REG_NOTES (tem
); note
; note
= next
)
530 next
= XEXP (note
, 1);
531 switch (REG_NOTE_KIND (note
))
534 /* Remove any REG_DEAD notes because we can't rely on them now
535 that the insn has been moved. */
536 remove_note (tem
, note
);
539 case REG_LABEL_OPERAND
:
540 case REG_LABEL_TARGET
:
541 /* Keep the label reference count up to date. */
542 if (LABEL_P (XEXP (note
, 0)))
543 LABEL_NUSES (XEXP (note
, 0)) ++;
553 /* Splice our SEQUENCE into the insn stream where INSN used to be. */
554 add_insn_after (seq_insn
, after
, NULL
);
559 /* Add INSN to DELAY_LIST and return the head of the new list. The list must
560 be in the order in which the insns are to be executed. */
563 add_to_delay_list (rtx_insn
*insn
, vec
<rtx_insn
*> *delay_list
)
565 /* If INSN has its block number recorded, clear it since we may
566 be moving the insn to a new block. */
567 clear_hashed_info_for_insn (insn
);
568 delay_list
->safe_push (insn
);
571 /* Delete INSN from the delay slot of the insn that it is in, which may
572 produce an insn with no delay slots. Return the new insn. */
575 delete_from_delay_slot (rtx_insn
*insn
)
577 rtx_insn
*trial
, *seq_insn
, *prev
;
582 /* We first must find the insn containing the SEQUENCE with INSN in its
583 delay slot. Do this by finding an insn, TRIAL, where
584 PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
587 PREV_INSN (NEXT_INSN (trial
)) == trial
;
588 trial
= NEXT_INSN (trial
))
591 seq_insn
= PREV_INSN (NEXT_INSN (trial
));
592 seq
= as_a
<rtx_sequence
*> (PATTERN (seq_insn
));
594 if (NEXT_INSN (seq_insn
) && BARRIER_P (NEXT_INSN (seq_insn
)))
597 /* Create a delay list consisting of all the insns other than the one
598 we are deleting (unless we were the only one). */
599 auto_vec
<rtx_insn
*, 5> delay_list
;
601 for (i
= 1; i
< seq
->len (); i
++)
602 if (seq
->insn (i
) != insn
)
603 add_to_delay_list (seq
->insn (i
), &delay_list
);
605 /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
606 list, and rebuild the delay list if non-empty. */
607 prev
= PREV_INSN (seq_insn
);
608 trial
= seq
->insn (0);
609 delete_related_insns (seq_insn
);
610 add_insn_after (trial
, prev
, NULL
);
612 /* If there was a barrier after the old SEQUENCE, remit it. */
614 emit_barrier_after (trial
);
616 /* If there are any delay insns, remit them. Otherwise clear the
618 if (!delay_list
.is_empty ())
619 trial
= emit_delay_sequence (trial
, delay_list
, XVECLEN (seq
, 0) - 2);
620 else if (JUMP_P (trial
))
621 INSN_ANNULLED_BRANCH_P (trial
) = 0;
623 INSN_FROM_TARGET_P (insn
) = 0;
625 /* Show we need to fill this insn again. */
626 obstack_ptr_grow (&unfilled_slots_obstack
, trial
);
631 /* Delete INSN, a JUMP_INSN. If it is a conditional jump, we must track down
632 the insn that sets CC0 for it and delete it too. */
635 delete_scheduled_jump (rtx_insn
*insn
)
637 /* Delete the insn that sets cc0 for us. On machines without cc0, we could
638 delete the insn that sets the condition code, but it is hard to find it.
639 Since this case is rare anyway, don't bother trying; there would likely
640 be other insns that became dead anyway, which we wouldn't know to
643 if (HAVE_cc0
&& reg_mentioned_p (cc0_rtx
, insn
))
645 rtx note
= find_reg_note (insn
, REG_CC_SETTER
, NULL_RTX
);
647 /* If a reg-note was found, it points to an insn to set CC0. This
648 insn is in the delay list of some other insn. So delete it from
649 the delay list it was in. */
652 if (! FIND_REG_INC_NOTE (XEXP (note
, 0), NULL_RTX
)
653 && sets_cc0_p (PATTERN (XEXP (note
, 0))) == 1)
654 delete_from_delay_slot (as_a
<rtx_insn
*> (XEXP (note
, 0)));
658 /* The insn setting CC0 is our previous insn, but it may be in
659 a delay slot. It will be the last insn in the delay slot, if
661 rtx_insn
*trial
= previous_insn (insn
);
663 trial
= prev_nonnote_insn (trial
);
664 if (sets_cc0_p (PATTERN (trial
)) != 1
665 || FIND_REG_INC_NOTE (trial
, NULL_RTX
))
667 if (PREV_INSN (NEXT_INSN (trial
)) == trial
)
668 delete_related_insns (trial
);
670 delete_from_delay_slot (trial
);
674 delete_related_insns (insn
);
677 /* Counters for delay-slot filling. */
679 #define NUM_REORG_FUNCTIONS 2
680 #define MAX_DELAY_HISTOGRAM 3
681 #define MAX_REORG_PASSES 2
683 static int num_insns_needing_delays
[NUM_REORG_FUNCTIONS
][MAX_REORG_PASSES
];
685 static int num_filled_delays
[NUM_REORG_FUNCTIONS
][MAX_DELAY_HISTOGRAM
+1][MAX_REORG_PASSES
];
687 static int reorg_pass_number
;
690 note_delay_statistics (int slots_filled
, int index
)
692 num_insns_needing_delays
[index
][reorg_pass_number
]++;
693 if (slots_filled
> MAX_DELAY_HISTOGRAM
)
694 slots_filled
= MAX_DELAY_HISTOGRAM
;
695 num_filled_delays
[index
][slots_filled
][reorg_pass_number
]++;
698 /* Optimize the following cases:
700 1. When a conditional branch skips over only one instruction,
701 use an annulling branch and put that insn in the delay slot.
702 Use either a branch that annuls when the condition if true or
703 invert the test with a branch that annuls when the condition is
704 false. This saves insns, since otherwise we must copy an insn
707 (orig) (skip) (otherwise)
708 Bcc.n L1 Bcc',a L1 Bcc,a L1'
715 2. When a conditional branch skips over only one instruction,
716 and after that, it unconditionally branches somewhere else,
717 perform the similar optimization. This saves executing the
718 second branch in the case where the inverted condition is true.
727 This should be expanded to skip over N insns, where N is the number
728 of delay slots required. */
731 optimize_skip (rtx_jump_insn
*insn
, vec
<rtx_insn
*> *delay_list
)
733 rtx_insn
*trial
= next_nonnote_insn (insn
);
734 rtx_insn
*next_trial
= next_active_insn (trial
);
737 flags
= get_jump_flags (insn
, JUMP_LABEL (insn
));
740 || !NONJUMP_INSN_P (trial
)
741 || GET_CODE (PATTERN (trial
)) == SEQUENCE
742 || recog_memoized (trial
) < 0
743 || (! eligible_for_annul_false (insn
, 0, trial
, flags
)
744 && ! eligible_for_annul_true (insn
, 0, trial
, flags
))
745 || RTX_FRAME_RELATED_P (trial
)
746 || can_throw_internal (trial
))
749 /* There are two cases where we are just executing one insn (we assume
750 here that a branch requires only one insn; this should be generalized
751 at some point): Where the branch goes around a single insn or where
752 we have one insn followed by a branch to the same label we branch to.
753 In both of these cases, inverting the jump and annulling the delay
754 slot give the same effect in fewer insns. */
755 if (next_trial
== next_active_insn (JUMP_LABEL_AS_INSN (insn
))
757 && simplejump_or_return_p (next_trial
)
758 && JUMP_LABEL (insn
) == JUMP_LABEL (next_trial
)))
760 if (eligible_for_annul_false (insn
, 0, trial
, flags
))
762 if (invert_jump (insn
, JUMP_LABEL (insn
), 1))
763 INSN_FROM_TARGET_P (trial
) = 1;
764 else if (! eligible_for_annul_true (insn
, 0, trial
, flags
))
768 add_to_delay_list (trial
, delay_list
);
769 next_trial
= next_active_insn (trial
);
770 update_block (trial
, trial
);
771 delete_related_insns (trial
);
773 /* Also, if we are targeting an unconditional
774 branch, thread our jump to the target of that branch. Don't
775 change this into a RETURN here, because it may not accept what
776 we have in the delay slot. We'll fix this up later. */
777 if (next_trial
&& simplejump_or_return_p (next_trial
))
779 rtx target_label
= JUMP_LABEL (next_trial
);
780 if (ANY_RETURN_P (target_label
))
781 target_label
= find_end_label (target_label
);
785 /* Recompute the flags based on TARGET_LABEL since threading
786 the jump to TARGET_LABEL may change the direction of the
787 jump (which may change the circumstances in which the
788 delay slot is nullified). */
789 flags
= get_jump_flags (insn
, target_label
);
790 if (eligible_for_annul_true (insn
, 0, trial
, flags
))
791 reorg_redirect_jump (insn
, target_label
);
795 INSN_ANNULLED_BRANCH_P (insn
) = 1;
799 /* Encode and return branch direction and prediction information for
800 INSN assuming it will jump to LABEL.
802 Non conditional branches return no direction information and
803 are predicted as very likely taken. */
806 get_jump_flags (const rtx_insn
*insn
, rtx label
)
810 /* get_jump_flags can be passed any insn with delay slots, these may
811 be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
812 direction information, and only if they are conditional jumps.
814 If LABEL is a return, then there is no way to determine the branch
817 && (condjump_p (insn
) || condjump_in_parallel_p (insn
))
818 && !ANY_RETURN_P (label
)
819 && INSN_UID (insn
) <= max_uid
820 && INSN_UID (label
) <= max_uid
)
822 = (uid_to_ruid
[INSN_UID (label
)] > uid_to_ruid
[INSN_UID (insn
)])
823 ? ATTR_FLAG_forward
: ATTR_FLAG_backward
;
824 /* No valid direction information. */
831 /* Return truth value of the statement that this branch
832 is mostly taken. If we think that the branch is extremely likely
833 to be taken, we return 2. If the branch is slightly more likely to be
834 taken, return 1. If the branch is slightly less likely to be taken,
835 return 0 and if the branch is highly unlikely to be taken, return -1. */
838 mostly_true_jump (rtx jump_insn
)
840 /* If branch probabilities are available, then use that number since it
841 always gives a correct answer. */
842 rtx note
= find_reg_note (jump_insn
, REG_BR_PROB
, 0);
845 int prob
= profile_probability::from_reg_br_prob_note (XINT (note
, 0))
846 .to_reg_br_prob_base ();
848 if (prob
>= REG_BR_PROB_BASE
* 9 / 10)
850 else if (prob
>= REG_BR_PROB_BASE
/ 2)
852 else if (prob
>= REG_BR_PROB_BASE
/ 10)
858 /* If there is no note, assume branches are not taken.
859 This should be rare. */
863 /* Return the condition under which INSN will branch to TARGET. If TARGET
864 is zero, return the condition under which INSN will return. If INSN is
865 an unconditional branch, return const_true_rtx. If INSN isn't a simple
866 type of jump, or it doesn't go to TARGET, return 0. */
869 get_branch_condition (const rtx_insn
*insn
, rtx target
)
871 rtx pat
= PATTERN (insn
);
874 if (condjump_in_parallel_p (insn
))
875 pat
= XVECEXP (pat
, 0, 0);
877 if (ANY_RETURN_P (pat
) && pat
== target
)
878 return const_true_rtx
;
880 if (GET_CODE (pat
) != SET
|| SET_DEST (pat
) != pc_rtx
)
884 if (GET_CODE (src
) == LABEL_REF
&& label_ref_label (src
) == target
)
885 return const_true_rtx
;
887 else if (GET_CODE (src
) == IF_THEN_ELSE
888 && XEXP (src
, 2) == pc_rtx
889 && ((GET_CODE (XEXP (src
, 1)) == LABEL_REF
890 && label_ref_label (XEXP (src
, 1)) == target
)
891 || (ANY_RETURN_P (XEXP (src
, 1)) && XEXP (src
, 1) == target
)))
892 return XEXP (src
, 0);
894 else if (GET_CODE (src
) == IF_THEN_ELSE
895 && XEXP (src
, 1) == pc_rtx
896 && ((GET_CODE (XEXP (src
, 2)) == LABEL_REF
897 && label_ref_label (XEXP (src
, 2)) == target
)
898 || (ANY_RETURN_P (XEXP (src
, 2)) && XEXP (src
, 2) == target
)))
901 rev
= reversed_comparison_code (XEXP (src
, 0), insn
);
903 return gen_rtx_fmt_ee (rev
, GET_MODE (XEXP (src
, 0)),
904 XEXP (XEXP (src
, 0), 0),
905 XEXP (XEXP (src
, 0), 1));
911 /* Return nonzero if CONDITION is more strict than the condition of
912 INSN, i.e., if INSN will always branch if CONDITION is true. */
915 condition_dominates_p (rtx condition
, const rtx_insn
*insn
)
917 rtx other_condition
= get_branch_condition (insn
, JUMP_LABEL (insn
));
918 enum rtx_code code
= GET_CODE (condition
);
919 enum rtx_code other_code
;
921 if (rtx_equal_p (condition
, other_condition
)
922 || other_condition
== const_true_rtx
)
925 else if (condition
== const_true_rtx
|| other_condition
== 0)
928 other_code
= GET_CODE (other_condition
);
929 if (GET_RTX_LENGTH (code
) != 2 || GET_RTX_LENGTH (other_code
) != 2
930 || ! rtx_equal_p (XEXP (condition
, 0), XEXP (other_condition
, 0))
931 || ! rtx_equal_p (XEXP (condition
, 1), XEXP (other_condition
, 1)))
934 return comparison_dominates_p (code
, other_code
);
937 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
938 any insns already in the delay slot of JUMP. */
941 redirect_with_delay_slots_safe_p (rtx_insn
*jump
, rtx newlabel
, rtx seq
)
944 rtx_sequence
*pat
= as_a
<rtx_sequence
*> (PATTERN (seq
));
946 /* Make sure all the delay slots of this jump would still
947 be valid after threading the jump. If they are still
948 valid, then return nonzero. */
950 flags
= get_jump_flags (jump
, newlabel
);
951 for (i
= 1; i
< pat
->len (); i
++)
953 #if ANNUL_IFFALSE_SLOTS
954 (INSN_ANNULLED_BRANCH_P (jump
)
955 && INSN_FROM_TARGET_P (pat
->insn (i
)))
956 ? eligible_for_annul_false (jump
, i
- 1, pat
->insn (i
), flags
) :
958 #if ANNUL_IFTRUE_SLOTS
959 (INSN_ANNULLED_BRANCH_P (jump
)
960 && ! INSN_FROM_TARGET_P (XVECEXP (pat
, 0, i
)))
961 ? eligible_for_annul_true (jump
, i
- 1, pat
->insn (i
), flags
) :
963 eligible_for_delay (jump
, i
- 1, pat
->insn (i
), flags
)))
966 return (i
== pat
->len ());
969 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
970 any insns we wish to place in the delay slot of JUMP. */
973 redirect_with_delay_list_safe_p (rtx_insn
*jump
, rtx newlabel
,
974 const vec
<rtx_insn
*> &delay_list
)
976 /* Make sure all the insns in DELAY_LIST would still be
977 valid after threading the jump. If they are still
978 valid, then return nonzero. */
980 int flags
= get_jump_flags (jump
, newlabel
);
981 unsigned int delay_insns
= delay_list
.length ();
983 for (; i
< delay_insns
; i
++)
985 #if ANNUL_IFFALSE_SLOTS
986 (INSN_ANNULLED_BRANCH_P (jump
)
987 && INSN_FROM_TARGET_P (delay_list
[i
]))
988 ? eligible_for_annul_false (jump
, i
, delay_list
[i
], flags
) :
990 #if ANNUL_IFTRUE_SLOTS
991 (INSN_ANNULLED_BRANCH_P (jump
)
992 && ! INSN_FROM_TARGET_P (delay_list
[i
]))
993 ? eligible_for_annul_true (jump
, i
, delay_list
[i
], flags
) :
995 eligible_for_delay (jump
, i
, delay_list
[i
], flags
)))
998 return i
== delay_insns
;
1001 /* DELAY_LIST is a list of insns that have already been placed into delay
1002 slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
1003 If not, return 0; otherwise return 1. */
1006 check_annul_list_true_false (int annul_true_p
,
1007 const vec
<rtx_insn
*> &delay_list
)
1011 FOR_EACH_VEC_ELT (delay_list
, i
, trial
)
1012 if ((annul_true_p
&& INSN_FROM_TARGET_P (trial
))
1013 || (!annul_true_p
&& !INSN_FROM_TARGET_P (trial
)))
1019 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
1020 the condition tested by INSN is CONDITION and the resources shown in
1021 OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1022 from SEQ's delay list, in addition to whatever insns it may execute
1023 (in DELAY_LIST). SETS and NEEDED are denote resources already set and
1024 needed while searching for delay slot insns. Return the concatenated
1025 delay list if possible, otherwise, return 0.
1027 SLOTS_TO_FILL is the total number of slots required by INSN, and
1028 PSLOTS_FILLED points to the number filled so far (also the number of
1029 insns in DELAY_LIST). It is updated with the number that have been
1030 filled from the SEQUENCE, if any.
1032 PANNUL_P points to a nonzero value if we already know that we need
1033 to annul INSN. If this routine determines that annulling is needed,
1034 it may set that value nonzero.
1036 PNEW_THREAD points to a location that is to receive the place at which
1037 execution should continue. */
1040 steal_delay_list_from_target (rtx_insn
*insn
, rtx condition
, rtx_sequence
*seq
,
1041 vec
<rtx_insn
*> *delay_list
,
1042 struct resources
*sets
,
1043 struct resources
*needed
,
1044 struct resources
*other_needed
,
1045 int slots_to_fill
, int *pslots_filled
,
1046 int *pannul_p
, rtx
*pnew_thread
)
1048 int slots_remaining
= slots_to_fill
- *pslots_filled
;
1049 int total_slots_filled
= *pslots_filled
;
1050 auto_vec
<rtx_insn
*, 5> new_delay_list
;
1051 int must_annul
= *pannul_p
;
1054 struct resources cc_set
;
1055 rtx_insn
**redundant
;
1057 /* We can't do anything if there are more delay slots in SEQ than we
1058 can handle, or if we don't know that it will be a taken branch.
1059 We know that it will be a taken branch if it is either an unconditional
1060 branch or a conditional branch with a stricter branch condition.
1062 Also, exit if the branch has more than one set, since then it is computing
1063 other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1064 ??? It may be possible to move other sets into INSN in addition to
1065 moving the instructions in the delay slots.
1067 We can not steal the delay list if one of the instructions in the
1068 current delay_list modifies the condition codes and the jump in the
1069 sequence is a conditional jump. We can not do this because we can
1070 not change the direction of the jump because the condition codes
1071 will effect the direction of the jump in the sequence. */
1073 CLEAR_RESOURCE (&cc_set
);
1076 FOR_EACH_VEC_ELT (*delay_list
, i
, trial
)
1078 mark_set_resources (trial
, &cc_set
, 0, MARK_SRC_DEST_CALL
);
1079 if (insn_references_resource_p (seq
->insn (0), &cc_set
, false))
1083 if (XVECLEN (seq
, 0) - 1 > slots_remaining
1084 || ! condition_dominates_p (condition
, seq
->insn (0))
1085 || ! single_set (seq
->insn (0)))
1088 /* On some targets, branches with delay slots can have a limited
1089 displacement. Give the back end a chance to tell us we can't do
1091 if (! targetm
.can_follow_jump (insn
, seq
->insn (0)))
1094 redundant
= XALLOCAVEC (rtx_insn
*, XVECLEN (seq
, 0));
1095 for (i
= 1; i
< seq
->len (); i
++)
1097 rtx_insn
*trial
= seq
->insn (i
);
1100 if (insn_references_resource_p (trial
, sets
, false)
1101 || insn_sets_resource_p (trial
, needed
, false)
1102 || insn_sets_resource_p (trial
, sets
, false)
1103 /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1105 || (HAVE_cc0
&& find_reg_note (trial
, REG_CC_USER
, NULL_RTX
))
1106 /* If TRIAL is from the fallthrough code of an annulled branch insn
1107 in SEQ, we cannot use it. */
1108 || (INSN_ANNULLED_BRANCH_P (seq
->insn (0))
1109 && ! INSN_FROM_TARGET_P (trial
)))
1112 /* If this insn was already done (usually in a previous delay slot),
1113 pretend we put it in our delay slot. */
1114 redundant
[i
] = redundant_insn (trial
, insn
, new_delay_list
);
1118 /* We will end up re-vectoring this branch, so compute flags
1119 based on jumping to the new label. */
1120 flags
= get_jump_flags (insn
, JUMP_LABEL (seq
->insn (0)));
1123 && ((condition
== const_true_rtx
1124 || (! insn_sets_resource_p (trial
, other_needed
, false)
1125 && ! may_trap_or_fault_p (PATTERN (trial
)))))
1126 ? eligible_for_delay (insn
, total_slots_filled
, trial
, flags
)
1127 : (must_annul
|| (delay_list
->is_empty () && new_delay_list
.is_empty ()))
1129 check_annul_list_true_false (0, *delay_list
)
1130 && check_annul_list_true_false (0, new_delay_list
)
1131 && eligible_for_annul_false (insn
, total_slots_filled
,
1136 /* Frame related instructions cannot go into annulled delay
1137 slots, it messes up the dwarf info. */
1138 if (RTX_FRAME_RELATED_P (trial
))
1142 rtx_insn
*temp
= copy_delay_slot_insn (trial
);
1143 INSN_FROM_TARGET_P (temp
) = 1;
1144 add_to_delay_list (temp
, &new_delay_list
);
1145 total_slots_filled
++;
1147 if (--slots_remaining
== 0)
1154 /* Record the effect of the instructions that were redundant and which
1155 we therefore decided not to copy. */
1156 for (i
= 1; i
< seq
->len (); i
++)
1159 fix_reg_dead_note (redundant
[i
], insn
);
1160 update_block (seq
->insn (i
), insn
);
1163 /* Show the place to which we will be branching. */
1164 *pnew_thread
= first_active_target_insn (JUMP_LABEL (seq
->insn (0)));
1166 /* Add any new insns to the delay list and update the count of the
1167 number of slots filled. */
1168 *pslots_filled
= total_slots_filled
;
1173 FOR_EACH_VEC_ELT (new_delay_list
, i
, temp
)
1174 add_to_delay_list (temp
, delay_list
);
1177 /* Similar to steal_delay_list_from_target except that SEQ is on the
1178 fallthrough path of INSN. Here we only do something if the delay insn
1179 of SEQ is an unconditional branch. In that case we steal its delay slot
1180 for INSN since unconditional branches are much easier to fill. */
1183 steal_delay_list_from_fallthrough (rtx_insn
*insn
, rtx condition
,
1185 vec
<rtx_insn
*> *delay_list
,
1186 struct resources
*sets
,
1187 struct resources
*needed
,
1188 struct resources
*other_needed
,
1189 int slots_to_fill
, int *pslots_filled
,
1194 int must_annul
= *pannul_p
;
1197 flags
= get_jump_flags (insn
, JUMP_LABEL (insn
));
1199 /* We can't do anything if SEQ's delay insn isn't an
1200 unconditional branch. */
1202 if (! simplejump_or_return_p (seq
->insn (0)))
1205 for (i
= 1; i
< seq
->len (); i
++)
1207 rtx_insn
*trial
= seq
->insn (i
);
1208 rtx_insn
*prior_insn
;
1210 /* If TRIAL sets CC0, stealing it will move it too far from the use
1212 if (insn_references_resource_p (trial
, sets
, false)
1213 || insn_sets_resource_p (trial
, needed
, false)
1214 || insn_sets_resource_p (trial
, sets
, false)
1215 || (HAVE_cc0
&& sets_cc0_p (PATTERN (trial
))))
1219 /* If this insn was already done, we don't need it. */
1220 if ((prior_insn
= redundant_insn (trial
, insn
, *delay_list
)))
1222 fix_reg_dead_note (prior_insn
, insn
);
1223 update_block (trial
, insn
);
1224 delete_from_delay_slot (trial
);
1229 && ((condition
== const_true_rtx
1230 || (! insn_sets_resource_p (trial
, other_needed
, false)
1231 && ! may_trap_or_fault_p (PATTERN (trial
)))))
1232 ? eligible_for_delay (insn
, *pslots_filled
, trial
, flags
)
1233 : (must_annul
|| delay_list
->is_empty ()) && (must_annul
= 1,
1234 check_annul_list_true_false (1, *delay_list
)
1235 && eligible_for_annul_true (insn
, *pslots_filled
, trial
, flags
)))
1239 delete_from_delay_slot (trial
);
1240 add_to_delay_list (trial
, delay_list
);
1242 if (++(*pslots_filled
) == slots_to_fill
)
1253 /* Try merging insns starting at THREAD which match exactly the insns in
1256 If all insns were matched and the insn was previously annulling, the
1257 annul bit will be cleared.
1259 For each insn that is merged, if the branch is or will be non-annulling,
1260 we delete the merged insn. */
1263 try_merge_delay_insns (rtx_insn
*insn
, rtx_insn
*thread
)
1265 rtx_insn
*trial
, *next_trial
;
1266 rtx_insn
*delay_insn
= as_a
<rtx_insn
*> (XVECEXP (PATTERN (insn
), 0, 0));
1267 int annul_p
= JUMP_P (delay_insn
) && INSN_ANNULLED_BRANCH_P (delay_insn
);
1268 int slot_number
= 1;
1269 int num_slots
= XVECLEN (PATTERN (insn
), 0);
1270 rtx next_to_match
= XVECEXP (PATTERN (insn
), 0, slot_number
);
1271 struct resources set
, needed
, modified
;
1272 auto_vec
<std::pair
<rtx_insn
*, bool>, 10> merged_insns
;
1275 flags
= get_jump_flags (delay_insn
, JUMP_LABEL (delay_insn
));
1277 CLEAR_RESOURCE (&needed
);
1278 CLEAR_RESOURCE (&set
);
1280 /* If this is not an annulling branch, take into account anything needed in
1281 INSN's delay slot. This prevents two increments from being incorrectly
1282 folded into one. If we are annulling, this would be the correct
1283 thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1284 will essentially disable this optimization. This method is somewhat of
1285 a kludge, but I don't see a better way.) */
1287 for (int i
= 1; i
< num_slots
; i
++)
1288 if (XVECEXP (PATTERN (insn
), 0, i
))
1289 mark_referenced_resources (XVECEXP (PATTERN (insn
), 0, i
), &needed
,
1292 for (trial
= thread
; !stop_search_p (trial
, 1); trial
= next_trial
)
1294 rtx pat
= PATTERN (trial
);
1295 rtx oldtrial
= trial
;
1297 next_trial
= next_nonnote_insn (trial
);
1299 /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1300 if (NONJUMP_INSN_P (trial
)
1301 && (GET_CODE (pat
) == USE
|| GET_CODE (pat
) == CLOBBER
1302 || GET_CODE (pat
) == CLOBBER_HIGH
))
1305 if (GET_CODE (next_to_match
) == GET_CODE (trial
)
1306 /* We can't share an insn that sets cc0. */
1307 && (!HAVE_cc0
|| ! sets_cc0_p (pat
))
1308 && ! insn_references_resource_p (trial
, &set
, true)
1309 && ! insn_sets_resource_p (trial
, &set
, true)
1310 && ! insn_sets_resource_p (trial
, &needed
, true)
1311 && (trial
= try_split (pat
, trial
, 0)) != 0
1312 /* Update next_trial, in case try_split succeeded. */
1313 && (next_trial
= next_nonnote_insn (trial
))
1314 /* Likewise THREAD. */
1315 && (thread
= oldtrial
== thread
? trial
: thread
)
1316 && rtx_equal_p (PATTERN (next_to_match
), PATTERN (trial
))
1317 /* Have to test this condition if annul condition is different
1318 from (and less restrictive than) non-annulling one. */
1319 && eligible_for_delay (delay_insn
, slot_number
- 1, trial
, flags
))
1324 update_block (trial
, thread
);
1325 if (trial
== thread
)
1326 thread
= next_active_insn (thread
);
1328 delete_related_insns (trial
);
1329 INSN_FROM_TARGET_P (next_to_match
) = 0;
1332 merged_insns
.safe_push (std::pair
<rtx_insn
*, bool> (trial
, false));
1334 if (++slot_number
== num_slots
)
1337 next_to_match
= XVECEXP (PATTERN (insn
), 0, slot_number
);
1340 mark_set_resources (trial
, &set
, 0, MARK_SRC_DEST_CALL
);
1341 mark_referenced_resources (trial
, &needed
, true);
1344 /* See if we stopped on a filled insn. If we did, try to see if its
1345 delay slots match. */
1346 if (slot_number
!= num_slots
1347 && trial
&& NONJUMP_INSN_P (trial
)
1348 && GET_CODE (PATTERN (trial
)) == SEQUENCE
1349 && !(JUMP_P (XVECEXP (PATTERN (trial
), 0, 0))
1350 && INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial
), 0, 0))))
1352 rtx_sequence
*pat
= as_a
<rtx_sequence
*> (PATTERN (trial
));
1353 rtx filled_insn
= XVECEXP (pat
, 0, 0);
1355 /* Account for resources set/needed by the filled insn. */
1356 mark_set_resources (filled_insn
, &set
, 0, MARK_SRC_DEST_CALL
);
1357 mark_referenced_resources (filled_insn
, &needed
, true);
1359 for (int i
= 1; i
< pat
->len (); i
++)
1361 rtx_insn
*dtrial
= pat
->insn (i
);
1363 CLEAR_RESOURCE (&modified
);
1364 /* Account for resources set by the insn following NEXT_TO_MATCH
1365 inside INSN's delay list. */
1366 for (int j
= 1; slot_number
+ j
< num_slots
; j
++)
1367 mark_set_resources (XVECEXP (PATTERN (insn
), 0, slot_number
+ j
),
1368 &modified
, 0, MARK_SRC_DEST_CALL
);
1369 /* Account for resources set by the insn before DTRIAL and inside
1370 TRIAL's delay list. */
1371 for (int j
= 1; j
< i
; j
++)
1372 mark_set_resources (XVECEXP (pat
, 0, j
),
1373 &modified
, 0, MARK_SRC_DEST_CALL
);
1374 if (! insn_references_resource_p (dtrial
, &set
, true)
1375 && ! insn_sets_resource_p (dtrial
, &set
, true)
1376 && ! insn_sets_resource_p (dtrial
, &needed
, true)
1377 && (!HAVE_cc0
|| ! sets_cc0_p (PATTERN (dtrial
)))
1378 && rtx_equal_p (PATTERN (next_to_match
), PATTERN (dtrial
))
1379 /* Check that DTRIAL and NEXT_TO_MATCH does not reference a
1380 resource modified between them (only dtrial is checked because
1381 next_to_match and dtrial shall to be equal in order to hit
1383 && ! insn_references_resource_p (dtrial
, &modified
, true)
1384 && eligible_for_delay (delay_insn
, slot_number
- 1, dtrial
, flags
))
1390 update_block (dtrial
, thread
);
1391 new_rtx
= delete_from_delay_slot (dtrial
);
1392 if (thread
->deleted ())
1394 INSN_FROM_TARGET_P (next_to_match
) = 0;
1397 merged_insns
.safe_push (std::pair
<rtx_insn
*, bool> (dtrial
,
1400 if (++slot_number
== num_slots
)
1403 next_to_match
= XVECEXP (PATTERN (insn
), 0, slot_number
);
1407 /* Keep track of the set/referenced resources for the delay
1408 slots of any trial insns we encounter. */
1409 mark_set_resources (dtrial
, &set
, 0, MARK_SRC_DEST_CALL
);
1410 mark_referenced_resources (dtrial
, &needed
, true);
1415 /* If all insns in the delay slot have been matched and we were previously
1416 annulling the branch, we need not any more. In that case delete all the
1417 merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1418 the delay list so that we know that it isn't only being used at the
1420 if (slot_number
== num_slots
&& annul_p
)
1422 unsigned int len
= merged_insns
.length ();
1423 for (unsigned int i
= len
- 1; i
< len
; i
--)
1424 if (merged_insns
[i
].second
)
1426 update_block (merged_insns
[i
].first
, thread
);
1427 rtx_insn
*new_rtx
= delete_from_delay_slot (merged_insns
[i
].first
);
1428 if (thread
->deleted ())
1433 update_block (merged_insns
[i
].first
, thread
);
1434 delete_related_insns (merged_insns
[i
].first
);
1437 INSN_ANNULLED_BRANCH_P (delay_insn
) = 0;
1439 for (int i
= 0; i
< XVECLEN (PATTERN (insn
), 0); i
++)
1440 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn
), 0, i
)) = 0;
1444 /* See if INSN is redundant with an insn in front of TARGET. Often this
1445 is called when INSN is a candidate for a delay slot of TARGET.
1446 DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1447 of INSN. Often INSN will be redundant with an insn in a delay slot of
1448 some previous insn. This happens when we have a series of branches to the
1449 same label; in that case the first insn at the target might want to go
1450 into each of the delay slots.
1452 If we are not careful, this routine can take up a significant fraction
1453 of the total compilation time (4%), but only wins rarely. Hence we
1454 speed this routine up by making two passes. The first pass goes back
1455 until it hits a label and sees if it finds an insn with an identical
1456 pattern. Only in this (relatively rare) event does it check for
1459 We do not split insns we encounter. This could cause us not to find a
1460 redundant insn, but the cost of splitting seems greater than the possible
1461 gain in rare cases. */
1464 redundant_insn (rtx insn
, rtx_insn
*target
, const vec
<rtx_insn
*> &delay_list
)
1466 rtx target_main
= target
;
1467 rtx ipat
= PATTERN (insn
);
1470 struct resources needed
, set
;
1472 unsigned insns_to_search
;
1474 /* If INSN has any REG_UNUSED notes, it can't match anything since we
1475 are allowed to not actually assign to such a register. */
1476 if (find_reg_note (insn
, REG_UNUSED
, NULL_RTX
) != 0)
1479 /* Scan backwards looking for a match. */
1480 for (trial
= PREV_INSN (target
),
1481 insns_to_search
= MAX_DELAY_SLOT_INSN_SEARCH
;
1482 trial
&& insns_to_search
> 0;
1483 trial
= PREV_INSN (trial
))
1485 /* (use (insn))s can come immediately after a barrier if the
1486 label that used to precede them has been deleted as dead.
1487 See delete_related_insns. */
1488 if (LABEL_P (trial
) || BARRIER_P (trial
))
1491 if (!INSN_P (trial
))
1495 pat
= PATTERN (trial
);
1496 if (GET_CODE (pat
) == USE
|| GET_CODE (pat
) == CLOBBER
1497 || GET_CODE (pat
) == CLOBBER_HIGH
)
1500 if (GET_CODE (trial
) == DEBUG_INSN
)
1503 if (rtx_sequence
*seq
= dyn_cast
<rtx_sequence
*> (pat
))
1505 /* Stop for a CALL and its delay slots because it is difficult to
1506 track its resource needs correctly. */
1507 if (CALL_P (seq
->element (0)))
1510 /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1511 slots because it is difficult to track its resource needs
1514 if (INSN_SETS_ARE_DELAYED (seq
->insn (0)))
1517 if (INSN_REFERENCES_ARE_DELAYED (seq
->insn (0)))
1520 /* See if any of the insns in the delay slot match, updating
1521 resource requirements as we go. */
1522 for (i
= seq
->len () - 1; i
> 0; i
--)
1523 if (GET_CODE (seq
->element (i
)) == GET_CODE (insn
)
1524 && rtx_equal_p (PATTERN (seq
->element (i
)), ipat
)
1525 && ! find_reg_note (seq
->element (i
), REG_UNUSED
, NULL_RTX
))
1528 /* If found a match, exit this loop early. */
1533 else if (GET_CODE (trial
) == GET_CODE (insn
) && rtx_equal_p (pat
, ipat
)
1534 && ! find_reg_note (trial
, REG_UNUSED
, NULL_RTX
))
1538 /* If we didn't find an insn that matches, return 0. */
1542 /* See what resources this insn sets and needs. If they overlap, or
1543 if this insn references CC0, it can't be redundant. */
1545 CLEAR_RESOURCE (&needed
);
1546 CLEAR_RESOURCE (&set
);
1547 mark_set_resources (insn
, &set
, 0, MARK_SRC_DEST_CALL
);
1548 mark_referenced_resources (insn
, &needed
, true);
1550 /* If TARGET is a SEQUENCE, get the main insn. */
1551 if (NONJUMP_INSN_P (target
) && GET_CODE (PATTERN (target
)) == SEQUENCE
)
1552 target_main
= XVECEXP (PATTERN (target
), 0, 0);
1554 if (resource_conflicts_p (&needed
, &set
)
1555 || (HAVE_cc0
&& reg_mentioned_p (cc0_rtx
, ipat
))
1556 /* The insn requiring the delay may not set anything needed or set by
1558 || insn_sets_resource_p (target_main
, &needed
, true)
1559 || insn_sets_resource_p (target_main
, &set
, true))
1562 /* Insns we pass may not set either NEEDED or SET, so merge them for
1564 needed
.memory
|= set
.memory
;
1565 IOR_HARD_REG_SET (needed
.regs
, set
.regs
);
1567 /* This insn isn't redundant if it conflicts with an insn that either is
1568 or will be in a delay slot of TARGET. */
1572 FOR_EACH_VEC_ELT (delay_list
, j
, temp
)
1573 if (insn_sets_resource_p (temp
, &needed
, true))
1576 if (NONJUMP_INSN_P (target
) && GET_CODE (PATTERN (target
)) == SEQUENCE
)
1577 for (i
= 1; i
< XVECLEN (PATTERN (target
), 0); i
++)
1578 if (insn_sets_resource_p (XVECEXP (PATTERN (target
), 0, i
), &needed
,
1582 /* Scan backwards until we reach a label or an insn that uses something
1583 INSN sets or sets something insn uses or sets. */
1585 for (trial
= PREV_INSN (target
),
1586 insns_to_search
= MAX_DELAY_SLOT_INSN_SEARCH
;
1587 trial
&& !LABEL_P (trial
) && insns_to_search
> 0;
1588 trial
= PREV_INSN (trial
))
1590 if (!INSN_P (trial
))
1594 pat
= PATTERN (trial
);
1595 if (GET_CODE (pat
) == USE
|| GET_CODE (pat
) == CLOBBER
1596 || GET_CODE (pat
) == CLOBBER_HIGH
)
1599 if (GET_CODE (trial
) == DEBUG_INSN
)
1602 if (rtx_sequence
*seq
= dyn_cast
<rtx_sequence
*> (pat
))
1604 bool annul_p
= false;
1605 rtx_insn
*control
= seq
->insn (0);
1607 /* If this is a CALL_INSN and its delay slots, it is hard to track
1608 the resource needs properly, so give up. */
1609 if (CALL_P (control
))
1612 /* If this is an INSN or JUMP_INSN with delayed effects, it
1613 is hard to track the resource needs properly, so give up. */
1615 if (INSN_SETS_ARE_DELAYED (control
))
1618 if (INSN_REFERENCES_ARE_DELAYED (control
))
1621 if (JUMP_P (control
))
1622 annul_p
= INSN_ANNULLED_BRANCH_P (control
);
1624 /* See if any of the insns in the delay slot match, updating
1625 resource requirements as we go. */
1626 for (i
= seq
->len () - 1; i
> 0; i
--)
1628 rtx_insn
*candidate
= seq
->insn (i
);
1630 /* If an insn will be annulled if the branch is false, it isn't
1631 considered as a possible duplicate insn. */
1632 if (rtx_equal_p (PATTERN (candidate
), ipat
)
1633 && ! (annul_p
&& INSN_FROM_TARGET_P (candidate
)))
1635 /* Show that this insn will be used in the sequel. */
1636 INSN_FROM_TARGET_P (candidate
) = 0;
1640 /* Unless this is an annulled insn from the target of a branch,
1641 we must stop if it sets anything needed or set by INSN. */
1642 if ((!annul_p
|| !INSN_FROM_TARGET_P (candidate
))
1643 && insn_sets_resource_p (candidate
, &needed
, true))
1647 /* If the insn requiring the delay slot conflicts with INSN, we
1649 if (insn_sets_resource_p (control
, &needed
, true))
1654 /* See if TRIAL is the same as INSN. */
1655 pat
= PATTERN (trial
);
1656 if (rtx_equal_p (pat
, ipat
))
1659 /* Can't go any further if TRIAL conflicts with INSN. */
1660 if (insn_sets_resource_p (trial
, &needed
, true))
1668 /* Return 1 if THREAD can only be executed in one way. If LABEL is nonzero,
1669 it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1670 is nonzero, we are allowed to fall into this thread; otherwise, we are
1673 If LABEL is used more than one or we pass a label other than LABEL before
1674 finding an active insn, we do not own this thread. */
1677 own_thread_p (rtx thread
, rtx label
, int allow_fallthrough
)
1679 rtx_insn
*active_insn
;
1682 /* We don't own the function end. */
1683 if (thread
== 0 || ANY_RETURN_P (thread
))
1686 /* We have a non-NULL insn. */
1687 rtx_insn
*thread_insn
= as_a
<rtx_insn
*> (thread
);
1689 /* Get the first active insn, or THREAD_INSN, if it is an active insn. */
1690 active_insn
= next_active_insn (PREV_INSN (thread_insn
));
1692 for (insn
= thread_insn
; insn
!= active_insn
; insn
= NEXT_INSN (insn
))
1694 && (insn
!= label
|| LABEL_NUSES (insn
) != 1))
1697 if (allow_fallthrough
)
1700 /* Ensure that we reach a BARRIER before any insn or label. */
1701 for (insn
= prev_nonnote_insn (thread_insn
);
1702 insn
== 0 || !BARRIER_P (insn
);
1703 insn
= prev_nonnote_insn (insn
))
1706 || (NONJUMP_INSN_P (insn
)
1707 && GET_CODE (PATTERN (insn
)) != USE
1708 && GET_CODE (PATTERN (insn
)) != CLOBBER
1709 && GET_CODE (PATTERN (insn
)) != CLOBBER_HIGH
))
1715 /* Called when INSN is being moved from a location near the target of a jump.
1716 We leave a marker of the form (use (INSN)) immediately in front of WHERE
1717 for mark_target_live_regs. These markers will be deleted at the end.
1719 We used to try to update the live status of registers if WHERE is at
1720 the start of a basic block, but that can't work since we may remove a
1721 BARRIER in relax_delay_slots. */
1724 update_block (rtx_insn
*insn
, rtx_insn
*where
)
1726 emit_insn_before (gen_rtx_USE (VOIDmode
, insn
), where
);
1728 /* INSN might be making a value live in a block where it didn't use to
1729 be. So recompute liveness information for this block. */
1730 incr_ticks_for_insn (insn
);
1733 /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1734 the basic block containing the jump. */
1737 reorg_redirect_jump (rtx_jump_insn
*jump
, rtx nlabel
)
1739 incr_ticks_for_insn (jump
);
1740 return redirect_jump (jump
, nlabel
, 1);
1743 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1744 We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1745 that reference values used in INSN. If we find one, then we move the
1746 REG_DEAD note to INSN.
1748 This is needed to handle the case where a later insn (after INSN) has a
1749 REG_DEAD note for a register used by INSN, and this later insn subsequently
1750 gets moved before a CODE_LABEL because it is a redundant insn. In this
1751 case, mark_target_live_regs may be confused into thinking the register
1752 is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1755 update_reg_dead_notes (rtx_insn
*insn
, rtx_insn
*delayed_insn
)
1760 for (p
= next_nonnote_insn (insn
); p
!= delayed_insn
;
1761 p
= next_nonnote_insn (p
))
1762 for (link
= REG_NOTES (p
); link
; link
= next
)
1764 next
= XEXP (link
, 1);
1766 if (REG_NOTE_KIND (link
) != REG_DEAD
1767 || !REG_P (XEXP (link
, 0)))
1770 if (reg_referenced_p (XEXP (link
, 0), PATTERN (insn
)))
1772 /* Move the REG_DEAD note from P to INSN. */
1773 remove_note (p
, link
);
1774 XEXP (link
, 1) = REG_NOTES (insn
);
1775 REG_NOTES (insn
) = link
;
1780 /* Called when an insn redundant with start_insn is deleted. If there
1781 is a REG_DEAD note for the target of start_insn between start_insn
1782 and stop_insn, then the REG_DEAD note needs to be deleted since the
1783 value no longer dies there.
1785 If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1786 confused into thinking the register is dead. */
1789 fix_reg_dead_note (rtx_insn
*start_insn
, rtx stop_insn
)
1794 for (p
= next_nonnote_insn (start_insn
); p
!= stop_insn
;
1795 p
= next_nonnote_insn (p
))
1796 for (link
= REG_NOTES (p
); link
; link
= next
)
1798 next
= XEXP (link
, 1);
1800 if (REG_NOTE_KIND (link
) != REG_DEAD
1801 || !REG_P (XEXP (link
, 0)))
1804 if (reg_set_p (XEXP (link
, 0), PATTERN (start_insn
)))
1806 remove_note (p
, link
);
1812 /* Delete any REG_UNUSED notes that exist on INSN but not on OTHER_INSN.
1814 This handles the case of udivmodXi4 instructions which optimize their
1815 output depending on whether any REG_UNUSED notes are present. We must
1816 make sure that INSN calculates as many results as OTHER_INSN does. */
1819 update_reg_unused_notes (rtx_insn
*insn
, rtx other_insn
)
1823 for (link
= REG_NOTES (insn
); link
; link
= next
)
1825 next
= XEXP (link
, 1);
1827 if (REG_NOTE_KIND (link
) != REG_UNUSED
1828 || !REG_P (XEXP (link
, 0)))
1831 if (!find_regno_note (other_insn
, REG_UNUSED
, REGNO (XEXP (link
, 0))))
1832 remove_note (insn
, link
);
1836 static vec
<rtx
> sibling_labels
;
1838 /* Return the label before INSN, or put a new label there. If SIBLING is
1839 non-zero, it is another label associated with the new label (if any),
1840 typically the former target of the jump that will be redirected to
1844 get_label_before (rtx_insn
*insn
, rtx sibling
)
1848 /* Find an existing label at this point
1849 or make a new one if there is none. */
1850 label
= prev_nonnote_insn (insn
);
1852 if (label
== 0 || !LABEL_P (label
))
1854 rtx_insn
*prev
= PREV_INSN (insn
);
1856 label
= gen_label_rtx ();
1857 emit_label_after (label
, prev
);
1858 LABEL_NUSES (label
) = 0;
1861 sibling_labels
.safe_push (label
);
1862 sibling_labels
.safe_push (sibling
);
1868 /* Scan a function looking for insns that need a delay slot and find insns to
1869 put into the delay slot.
1871 NON_JUMPS_P is nonzero if we are to only try to fill non-jump insns (such
1872 as calls). We do these first since we don't want jump insns (that are
1873 easier to fill) to get the only insns that could be used for non-jump insns.
1874 When it is zero, only try to fill JUMP_INSNs.
1876 When slots are filled in this manner, the insns (including the
1877 delay_insn) are put together in a SEQUENCE rtx. In this fashion,
1878 it is possible to tell whether a delay slot has really been filled
1879 or not. `final' knows how to deal with this, by communicating
1880 through FINAL_SEQUENCE. */
1883 fill_simple_delay_slots (int non_jumps_p
)
1885 rtx_insn
*insn
, *trial
, *next_trial
;
1888 int num_unfilled_slots
= unfilled_slots_next
- unfilled_slots_base
;
1889 struct resources needed
, set
;
1890 int slots_to_fill
, slots_filled
;
1891 auto_vec
<rtx_insn
*, 5> delay_list
;
1893 for (i
= 0; i
< num_unfilled_slots
; i
++)
1896 /* Get the next insn to fill. If it has already had any slots assigned,
1897 we can't do anything with it. Maybe we'll improve this later. */
1899 insn
= unfilled_slots_base
[i
];
1902 || (NONJUMP_INSN_P (insn
)
1903 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
1904 || (JUMP_P (insn
) && non_jumps_p
)
1905 || (!JUMP_P (insn
) && ! non_jumps_p
))
1908 /* It may have been that this insn used to need delay slots, but
1909 now doesn't; ignore in that case. This can happen, for example,
1910 on the HP PA RISC, where the number of delay slots depends on
1911 what insns are nearby. */
1912 slots_to_fill
= num_delay_slots (insn
);
1914 /* Some machine description have defined instructions to have
1915 delay slots only in certain circumstances which may depend on
1916 nearby insns (which change due to reorg's actions).
1918 For example, the PA port normally has delay slots for unconditional
1921 However, the PA port claims such jumps do not have a delay slot
1922 if they are immediate successors of certain CALL_INSNs. This
1923 allows the port to favor filling the delay slot of the call with
1924 the unconditional jump. */
1925 if (slots_to_fill
== 0)
1928 /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
1929 says how many. After initialization, first try optimizing
1932 nop add %o7,.-L1,%o7
1936 If this case applies, the delay slot of the call is filled with
1937 the unconditional jump. This is done first to avoid having the
1938 delay slot of the call filled in the backward scan. Also, since
1939 the unconditional jump is likely to also have a delay slot, that
1940 insn must exist when it is subsequently scanned.
1942 This is tried on each insn with delay slots as some machines
1943 have insns which perform calls, but are not represented as
1947 delay_list
.truncate (0);
1950 flags
= get_jump_flags (insn
, JUMP_LABEL (insn
));
1952 flags
= get_jump_flags (insn
, NULL_RTX
);
1954 if ((trial
= next_active_insn (insn
))
1956 && simplejump_p (trial
)
1957 && eligible_for_delay (insn
, slots_filled
, trial
, flags
)
1958 && no_labels_between_p (insn
, trial
)
1959 && ! can_throw_internal (trial
))
1963 add_to_delay_list (trial
, &delay_list
);
1965 /* TRIAL may have had its delay slot filled, then unfilled. When
1966 the delay slot is unfilled, TRIAL is placed back on the unfilled
1967 slots obstack. Unfortunately, it is placed on the end of the
1968 obstack, not in its original location. Therefore, we must search
1969 from entry i + 1 to the end of the unfilled slots obstack to
1970 try and find TRIAL. */
1971 tmp
= &unfilled_slots_base
[i
+ 1];
1972 while (*tmp
!= trial
&& tmp
!= unfilled_slots_next
)
1975 /* Remove the unconditional jump from consideration for delay slot
1976 filling and unthread it. */
1980 rtx_insn
*next
= NEXT_INSN (trial
);
1981 rtx_insn
*prev
= PREV_INSN (trial
);
1983 SET_NEXT_INSN (prev
) = next
;
1985 SET_PREV_INSN (next
) = prev
;
1989 /* Now, scan backwards from the insn to search for a potential
1990 delay-slot candidate. Stop searching when a label or jump is hit.
1992 For each candidate, if it is to go into the delay slot (moved
1993 forward in execution sequence), it must not need or set any resources
1994 that were set by later insns and must not set any resources that
1995 are needed for those insns.
1997 The delay slot insn itself sets resources unless it is a call
1998 (in which case the called routine, not the insn itself, is doing
2001 if (slots_filled
< slots_to_fill
)
2003 /* If the flags register is dead after the insn, then we want to be
2004 able to accept a candidate that clobbers it. For this purpose,
2005 we need to filter the flags register during life analysis, so
2006 that it doesn't create RAW and WAW dependencies, while still
2007 creating the necessary WAR dependencies. */
2009 = (slots_to_fill
== 1
2010 && targetm
.flags_regnum
!= INVALID_REGNUM
2011 && find_regno_note (insn
, REG_DEAD
, targetm
.flags_regnum
));
2012 struct resources fset
;
2013 CLEAR_RESOURCE (&needed
);
2014 CLEAR_RESOURCE (&set
);
2015 mark_set_resources (insn
, &set
, 0, MARK_SRC_DEST
);
2018 CLEAR_RESOURCE (&fset
);
2019 mark_set_resources (insn
, &fset
, 0, MARK_SRC_DEST
);
2021 mark_referenced_resources (insn
, &needed
, false);
2023 for (trial
= prev_nonnote_insn (insn
); ! stop_search_p (trial
, 1);
2026 next_trial
= prev_nonnote_insn (trial
);
2028 /* This must be an INSN or CALL_INSN. */
2029 pat
= PATTERN (trial
);
2031 /* Stand-alone USE and CLOBBER are just for flow. */
2032 if (GET_CODE (pat
) == USE
|| GET_CODE (pat
) == CLOBBER
2033 || GET_CODE (pat
) == CLOBBER_HIGH
)
2036 /* And DEBUG_INSNs never go into delay slots. */
2037 if (GET_CODE (trial
) == DEBUG_INSN
)
2040 /* Check for resource conflict first, to avoid unnecessary
2042 if (! insn_references_resource_p (trial
, &set
, true)
2043 && ! insn_sets_resource_p (trial
,
2044 filter_flags
? &fset
: &set
,
2046 && ! insn_sets_resource_p (trial
, &needed
, true)
2047 /* Can't separate set of cc0 from its use. */
2048 && (!HAVE_cc0
|| ! (reg_mentioned_p (cc0_rtx
, pat
) && ! sets_cc0_p (pat
)))
2049 && ! can_throw_internal (trial
))
2051 trial
= try_split (pat
, trial
, 1);
2052 next_trial
= prev_nonnote_insn (trial
);
2053 if (eligible_for_delay (insn
, slots_filled
, trial
, flags
))
2055 /* In this case, we are searching backward, so if we
2056 find insns to put on the delay list, we want
2057 to put them at the head, rather than the
2058 tail, of the list. */
2060 update_reg_dead_notes (trial
, insn
);
2061 delay_list
.safe_insert (0, trial
);
2062 update_block (trial
, trial
);
2063 delete_related_insns (trial
);
2064 if (slots_to_fill
== ++slots_filled
)
2070 mark_set_resources (trial
, &set
, 0, MARK_SRC_DEST_CALL
);
2073 mark_set_resources (trial
, &fset
, 0, MARK_SRC_DEST_CALL
);
2074 /* If the flags register is set, then it doesn't create RAW
2075 dependencies any longer and it also doesn't create WAW
2076 dependencies since it's dead after the original insn. */
2077 if (TEST_HARD_REG_BIT (fset
.regs
, targetm
.flags_regnum
))
2079 CLEAR_HARD_REG_BIT (needed
.regs
, targetm
.flags_regnum
);
2080 CLEAR_HARD_REG_BIT (fset
.regs
, targetm
.flags_regnum
);
2083 mark_referenced_resources (trial
, &needed
, true);
2087 /* If all needed slots haven't been filled, we come here. */
2089 /* Try to optimize case of jumping around a single insn. */
2090 if ((ANNUL_IFTRUE_SLOTS
|| ANNUL_IFFALSE_SLOTS
)
2091 && slots_filled
!= slots_to_fill
2092 && delay_list
.is_empty ()
2094 && (condjump_p (insn
) || condjump_in_parallel_p (insn
))
2095 && !ANY_RETURN_P (JUMP_LABEL (insn
)))
2097 optimize_skip (as_a
<rtx_jump_insn
*> (insn
), &delay_list
);
2098 if (!delay_list
.is_empty ())
2102 /* Try to get insns from beyond the insn needing the delay slot.
2103 These insns can neither set or reference resources set in insns being
2104 skipped, cannot set resources in the insn being skipped, and, if this
2105 is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2106 call might not return).
2108 There used to be code which continued past the target label if
2109 we saw all uses of the target label. This code did not work,
2110 because it failed to account for some instructions which were
2111 both annulled and marked as from the target. This can happen as a
2112 result of optimize_skip. Since this code was redundant with
2113 fill_eager_delay_slots anyways, it was just deleted. */
2115 if (slots_filled
!= slots_to_fill
2116 /* If this instruction could throw an exception which is
2117 caught in the same function, then it's not safe to fill
2118 the delay slot with an instruction from beyond this
2119 point. For example, consider:
2130 Even though `i' is a local variable, we must be sure not
2131 to put `i = 3' in the delay slot if `f' might throw an
2134 Presumably, we should also check to see if we could get
2135 back to this function via `setjmp'. */
2136 && ! can_throw_internal (insn
)
2139 int maybe_never
= 0;
2140 rtx pat
, trial_delay
;
2142 CLEAR_RESOURCE (&needed
);
2143 CLEAR_RESOURCE (&set
);
2144 mark_set_resources (insn
, &set
, 0, MARK_SRC_DEST_CALL
);
2145 mark_referenced_resources (insn
, &needed
, true);
2150 for (trial
= next_nonnote_insn (insn
); !stop_search_p (trial
, 1);
2153 next_trial
= next_nonnote_insn (trial
);
2155 /* This must be an INSN or CALL_INSN. */
2156 pat
= PATTERN (trial
);
2158 /* Stand-alone USE and CLOBBER are just for flow. */
2159 if (GET_CODE (pat
) == USE
|| GET_CODE (pat
) == CLOBBER
2160 || GET_CODE (pat
) == CLOBBER_HIGH
)
2163 /* And DEBUG_INSNs do not go in delay slots. */
2164 if (GET_CODE (trial
) == DEBUG_INSN
)
2167 /* If this already has filled delay slots, get the insn needing
2169 if (GET_CODE (pat
) == SEQUENCE
)
2170 trial_delay
= XVECEXP (pat
, 0, 0);
2172 trial_delay
= trial
;
2174 /* Stop our search when seeing a jump. */
2175 if (JUMP_P (trial_delay
))
2178 /* See if we have a resource problem before we try to split. */
2179 if (GET_CODE (pat
) != SEQUENCE
2180 && ! insn_references_resource_p (trial
, &set
, true)
2181 && ! insn_sets_resource_p (trial
, &set
, true)
2182 && ! insn_sets_resource_p (trial
, &needed
, true)
2183 && (!HAVE_cc0
&& ! (reg_mentioned_p (cc0_rtx
, pat
) && ! sets_cc0_p (pat
)))
2184 && ! (maybe_never
&& may_trap_or_fault_p (pat
))
2185 && (trial
= try_split (pat
, trial
, 0))
2186 && eligible_for_delay (insn
, slots_filled
, trial
, flags
)
2187 && ! can_throw_internal (trial
))
2189 next_trial
= next_nonnote_insn (trial
);
2190 add_to_delay_list (trial
, &delay_list
);
2191 if (HAVE_cc0
&& reg_mentioned_p (cc0_rtx
, pat
))
2192 link_cc0_insns (trial
);
2194 delete_related_insns (trial
);
2195 if (slots_to_fill
== ++slots_filled
)
2200 mark_set_resources (trial
, &set
, 0, MARK_SRC_DEST_CALL
);
2201 mark_referenced_resources (trial
, &needed
, true);
2203 /* Ensure we don't put insns between the setting of cc and the
2204 comparison by moving a setting of cc into an earlier delay
2205 slot since these insns could clobber the condition code. */
2208 /* If this is a call, we might not get here. */
2209 if (CALL_P (trial_delay
))
2213 /* If there are slots left to fill and our search was stopped by an
2214 unconditional branch, try the insn at the branch target. We can
2215 redirect the branch if it works.
2217 Don't do this if the insn at the branch target is a branch. */
2218 if (slots_to_fill
!= slots_filled
2220 && jump_to_label_p (trial
)
2221 && simplejump_p (trial
)
2222 && (next_trial
= next_active_insn (JUMP_LABEL_AS_INSN (trial
))) != 0
2223 && ! (NONJUMP_INSN_P (next_trial
)
2224 && GET_CODE (PATTERN (next_trial
)) == SEQUENCE
)
2225 && !JUMP_P (next_trial
)
2226 && ! insn_references_resource_p (next_trial
, &set
, true)
2227 && ! insn_sets_resource_p (next_trial
, &set
, true)
2228 && ! insn_sets_resource_p (next_trial
, &needed
, true)
2229 && (!HAVE_cc0
|| ! reg_mentioned_p (cc0_rtx
, PATTERN (next_trial
)))
2230 && ! (maybe_never
&& may_trap_or_fault_p (PATTERN (next_trial
)))
2231 && (next_trial
= try_split (PATTERN (next_trial
), next_trial
, 0))
2232 && eligible_for_delay (insn
, slots_filled
, next_trial
, flags
)
2233 && ! can_throw_internal (trial
))
2235 /* See comment in relax_delay_slots about necessity of using
2236 next_real_nondebug_insn here. */
2237 rtx_insn
*new_label
= next_real_nondebug_insn (next_trial
);
2240 new_label
= get_label_before (new_label
, JUMP_LABEL (trial
));
2242 new_label
= find_end_label (simple_return_rtx
);
2246 add_to_delay_list (copy_delay_slot_insn (next_trial
),
2249 reorg_redirect_jump (as_a
<rtx_jump_insn
*> (trial
),
2255 /* If this is an unconditional jump, then try to get insns from the
2256 target of the jump. */
2257 rtx_jump_insn
*jump_insn
;
2258 if ((jump_insn
= dyn_cast
<rtx_jump_insn
*> (insn
))
2259 && simplejump_p (jump_insn
)
2260 && slots_filled
!= slots_to_fill
)
2261 fill_slots_from_thread (jump_insn
, const_true_rtx
,
2262 next_active_insn (JUMP_LABEL_AS_INSN (insn
)),
2263 NULL
, 1, 1, own_thread_p (JUMP_LABEL (insn
),
2264 JUMP_LABEL (insn
), 0),
2265 slots_to_fill
, &slots_filled
, &delay_list
);
2267 if (!delay_list
.is_empty ())
2268 unfilled_slots_base
[i
]
2269 = emit_delay_sequence (insn
, delay_list
, slots_filled
);
2271 if (slots_to_fill
== slots_filled
)
2272 unfilled_slots_base
[i
] = 0;
2274 note_delay_statistics (slots_filled
, 0);
2278 /* Follow any unconditional jump at LABEL, for the purpose of redirecting JUMP;
2279 return the ultimate label reached by any such chain of jumps.
2280 Return a suitable return rtx if the chain ultimately leads to a
2282 If LABEL is not followed by a jump, return LABEL.
2283 If the chain loops or we can't find end, return LABEL,
2284 since that tells caller to avoid changing the insn.
2285 If the returned label is obtained by following a crossing jump,
2286 set *CROSSING to true, otherwise set it to false. */
2289 follow_jumps (rtx label
, rtx_insn
*jump
, bool *crossing
)
2296 if (ANY_RETURN_P (label
))
2299 rtx_insn
*value
= as_a
<rtx_insn
*> (label
);
2303 && (insn
= next_active_insn (value
)) != 0
2305 && JUMP_LABEL (insn
) != NULL_RTX
2306 && ((any_uncondjump_p (insn
) && onlyjump_p (insn
))
2307 || ANY_RETURN_P (PATTERN (insn
)))
2308 && (next
= NEXT_INSN (insn
))
2309 && BARRIER_P (next
));
2312 rtx this_label_or_return
= JUMP_LABEL (insn
);
2314 /* If we have found a cycle, make the insn jump to itself. */
2315 if (this_label_or_return
== label
)
2318 /* Cannot follow returns and cannot look through tablejumps. */
2319 if (ANY_RETURN_P (this_label_or_return
))
2320 return this_label_or_return
;
2322 rtx_insn
*this_label
= as_a
<rtx_insn
*> (this_label_or_return
);
2323 if (NEXT_INSN (this_label
)
2324 && JUMP_TABLE_DATA_P (NEXT_INSN (this_label
)))
2327 if (!targetm
.can_follow_jump (jump
, insn
))
2330 *crossing
= CROSSING_JUMP_P (jump
);
2338 /* Try to find insns to place in delay slots.
2340 INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2341 or is an unconditional branch if CONDITION is const_true_rtx.
2342 *PSLOTS_FILLED is updated with the number of slots that we have filled.
2344 THREAD is a flow-of-control, either the insns to be executed if the
2345 branch is true or if the branch is false, THREAD_IF_TRUE says which.
2347 OPPOSITE_THREAD is the thread in the opposite direction. It is used
2348 to see if any potential delay slot insns set things needed there.
2350 LIKELY is nonzero if it is extremely likely that the branch will be
2351 taken and THREAD_IF_TRUE is set. This is used for the branch at the
2352 end of a loop back up to the top.
2354 OWN_THREAD is true if we are the only user of the thread, i.e. it is
2355 the target of the jump when we are the only jump going there.
2357 If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2358 case, we can only take insns from the head of the thread for our delay
2359 slot. We then adjust the jump to point after the insns we have taken. */
2362 fill_slots_from_thread (rtx_jump_insn
*insn
, rtx condition
,
2363 rtx thread_or_return
, rtx opposite_thread
, int likely
,
2364 int thread_if_true
, int own_thread
, int slots_to_fill
,
2365 int *pslots_filled
, vec
<rtx_insn
*> *delay_list
)
2368 struct resources opposite_needed
, set
, needed
;
2374 /* Validate our arguments. */
2375 gcc_assert (condition
!= const_true_rtx
|| thread_if_true
);
2376 gcc_assert (own_thread
|| thread_if_true
);
2378 flags
= get_jump_flags (insn
, JUMP_LABEL (insn
));
2380 /* If our thread is the end of subroutine, we can't get any delay
2382 if (thread_or_return
== NULL_RTX
|| ANY_RETURN_P (thread_or_return
))
2385 rtx_insn
*thread
= as_a
<rtx_insn
*> (thread_or_return
);
2387 /* If this is an unconditional branch, nothing is needed at the
2388 opposite thread. Otherwise, compute what is needed there. */
2389 if (condition
== const_true_rtx
)
2390 CLEAR_RESOURCE (&opposite_needed
);
2392 mark_target_live_regs (get_insns (), opposite_thread
, &opposite_needed
);
2394 /* If the insn at THREAD can be split, do it here to avoid having to
2395 update THREAD and NEW_THREAD if it is done in the loop below. Also
2396 initialize NEW_THREAD. */
2398 new_thread
= thread
= try_split (PATTERN (thread
), thread
, 0);
2400 /* Scan insns at THREAD. We are looking for an insn that can be removed
2401 from THREAD (it neither sets nor references resources that were set
2402 ahead of it and it doesn't set anything needs by the insns ahead of
2403 it) and that either can be placed in an annulling insn or aren't
2404 needed at OPPOSITE_THREAD. */
2406 CLEAR_RESOURCE (&needed
);
2407 CLEAR_RESOURCE (&set
);
2409 /* If we do not own this thread, we must stop as soon as we find
2410 something that we can't put in a delay slot, since all we can do
2411 is branch into THREAD at a later point. Therefore, labels stop
2412 the search if this is not the `true' thread. */
2414 for (trial
= thread
;
2415 ! stop_search_p (trial
, ! thread_if_true
) && (! lose
|| own_thread
);
2416 trial
= next_nonnote_insn (trial
))
2420 /* If we have passed a label, we no longer own this thread. */
2421 if (LABEL_P (trial
))
2427 pat
= PATTERN (trial
);
2428 if (GET_CODE (pat
) == USE
|| GET_CODE (pat
) == CLOBBER
2429 || GET_CODE (pat
) == CLOBBER_HIGH
)
2432 if (GET_CODE (trial
) == DEBUG_INSN
)
2435 /* If TRIAL conflicts with the insns ahead of it, we lose. Also,
2436 don't separate or copy insns that set and use CC0. */
2437 if (! insn_references_resource_p (trial
, &set
, true)
2438 && ! insn_sets_resource_p (trial
, &set
, true)
2439 && ! insn_sets_resource_p (trial
, &needed
, true)
2440 && (!HAVE_cc0
|| (! (reg_mentioned_p (cc0_rtx
, pat
)
2441 && (! own_thread
|| ! sets_cc0_p (pat
)))))
2442 && ! can_throw_internal (trial
))
2444 rtx_insn
*prior_insn
;
2446 /* If TRIAL is redundant with some insn before INSN, we don't
2447 actually need to add it to the delay list; we can merely pretend
2449 if ((prior_insn
= redundant_insn (trial
, insn
, *delay_list
)))
2451 fix_reg_dead_note (prior_insn
, insn
);
2454 update_block (trial
, thread
);
2455 if (trial
== thread
)
2457 thread
= next_active_insn (thread
);
2458 if (new_thread
== trial
)
2459 new_thread
= thread
;
2462 delete_related_insns (trial
);
2466 update_reg_unused_notes (prior_insn
, trial
);
2467 new_thread
= next_active_insn (trial
);
2473 /* There are two ways we can win: If TRIAL doesn't set anything
2474 needed at the opposite thread and can't trap, or if it can
2475 go into an annulled delay slot. But we want neither to copy
2476 nor to speculate frame-related insns. */
2478 && ((condition
== const_true_rtx
2479 && (own_thread
|| !RTX_FRAME_RELATED_P (trial
)))
2480 || (! insn_sets_resource_p (trial
, &opposite_needed
, true)
2481 && ! may_trap_or_fault_p (pat
)
2482 && ! RTX_FRAME_RELATED_P (trial
))))
2485 trial
= try_split (pat
, trial
, 0);
2486 if (new_thread
== old_trial
)
2488 if (thread
== old_trial
)
2490 pat
= PATTERN (trial
);
2491 if (eligible_for_delay (insn
, *pslots_filled
, trial
, flags
))
2494 else if (!RTX_FRAME_RELATED_P (trial
)
2495 && ((ANNUL_IFTRUE_SLOTS
&& ! thread_if_true
)
2496 || (ANNUL_IFFALSE_SLOTS
&& thread_if_true
)))
2499 trial
= try_split (pat
, trial
, 0);
2500 if (new_thread
== old_trial
)
2502 if (thread
== old_trial
)
2504 pat
= PATTERN (trial
);
2505 if ((must_annul
|| delay_list
->is_empty ()) && (thread_if_true
2506 ? check_annul_list_true_false (0, *delay_list
)
2507 && eligible_for_annul_false (insn
, *pslots_filled
, trial
, flags
)
2508 : check_annul_list_true_false (1, *delay_list
)
2509 && eligible_for_annul_true (insn
, *pslots_filled
, trial
, flags
)))
2516 if (HAVE_cc0
&& reg_mentioned_p (cc0_rtx
, pat
))
2517 link_cc0_insns (trial
);
2519 /* If we own this thread, delete the insn. If this is the
2520 destination of a branch, show that a basic block status
2521 may have been updated. In any case, mark the new
2522 starting point of this thread. */
2527 update_block (trial
, thread
);
2528 if (trial
== thread
)
2530 thread
= next_active_insn (thread
);
2531 if (new_thread
== trial
)
2532 new_thread
= thread
;
2535 /* We are moving this insn, not deleting it. We must
2536 temporarily increment the use count on any referenced
2537 label lest it be deleted by delete_related_insns. */
2538 for (note
= REG_NOTES (trial
);
2540 note
= XEXP (note
, 1))
2541 if (REG_NOTE_KIND (note
) == REG_LABEL_OPERAND
2542 || REG_NOTE_KIND (note
) == REG_LABEL_TARGET
)
2544 /* REG_LABEL_OPERAND could be
2545 NOTE_INSN_DELETED_LABEL too. */
2546 if (LABEL_P (XEXP (note
, 0)))
2547 LABEL_NUSES (XEXP (note
, 0))++;
2549 gcc_assert (REG_NOTE_KIND (note
)
2550 == REG_LABEL_OPERAND
);
2552 if (jump_to_label_p (trial
))
2553 LABEL_NUSES (JUMP_LABEL (trial
))++;
2555 delete_related_insns (trial
);
2557 for (note
= REG_NOTES (trial
);
2559 note
= XEXP (note
, 1))
2560 if (REG_NOTE_KIND (note
) == REG_LABEL_OPERAND
2561 || REG_NOTE_KIND (note
) == REG_LABEL_TARGET
)
2563 /* REG_LABEL_OPERAND could be
2564 NOTE_INSN_DELETED_LABEL too. */
2565 if (LABEL_P (XEXP (note
, 0)))
2566 LABEL_NUSES (XEXP (note
, 0))--;
2568 gcc_assert (REG_NOTE_KIND (note
)
2569 == REG_LABEL_OPERAND
);
2571 if (jump_to_label_p (trial
))
2572 LABEL_NUSES (JUMP_LABEL (trial
))--;
2575 new_thread
= next_active_insn (trial
);
2577 temp
= own_thread
? trial
: copy_delay_slot_insn (trial
);
2579 INSN_FROM_TARGET_P (temp
) = 1;
2581 add_to_delay_list (temp
, delay_list
);
2583 if (slots_to_fill
== ++(*pslots_filled
))
2585 /* Even though we have filled all the slots, we
2586 may be branching to a location that has a
2587 redundant insn. Skip any if so. */
2588 while (new_thread
&& ! own_thread
2589 && ! insn_sets_resource_p (new_thread
, &set
, true)
2590 && ! insn_sets_resource_p (new_thread
, &needed
,
2592 && ! insn_references_resource_p (new_thread
,
2595 = redundant_insn (new_thread
, insn
,
2598 /* We know we do not own the thread, so no need
2599 to call update_block and delete_insn. */
2600 fix_reg_dead_note (prior_insn
, insn
);
2601 update_reg_unused_notes (prior_insn
, new_thread
);
2603 = next_active_insn (as_a
<rtx_insn
*> (new_thread
));
2613 /* This insn can't go into a delay slot. */
2615 mark_set_resources (trial
, &set
, 0, MARK_SRC_DEST_CALL
);
2616 mark_referenced_resources (trial
, &needed
, true);
2618 /* Ensure we don't put insns between the setting of cc and the comparison
2619 by moving a setting of cc into an earlier delay slot since these insns
2620 could clobber the condition code. */
2623 /* If this insn is a register-register copy and the next insn has
2624 a use of our destination, change it to use our source. That way,
2625 it will become a candidate for our delay slot the next time
2626 through this loop. This case occurs commonly in loops that
2629 We could check for more complex cases than those tested below,
2630 but it doesn't seem worth it. It might also be a good idea to try
2631 to swap the two insns. That might do better.
2633 We can't do this if the next insn modifies our destination, because
2634 that would make the replacement into the insn invalid. We also can't
2635 do this if it modifies our source, because it might be an earlyclobber
2636 operand. This latter test also prevents updating the contents of
2637 a PRE_INC. We also can't do this if there's overlap of source and
2638 destination. Overlap may happen for larger-than-register-size modes. */
2640 if (NONJUMP_INSN_P (trial
) && GET_CODE (pat
) == SET
2641 && REG_P (SET_SRC (pat
))
2642 && REG_P (SET_DEST (pat
))
2643 && !reg_overlap_mentioned_p (SET_DEST (pat
), SET_SRC (pat
)))
2645 rtx_insn
*next
= next_nonnote_insn (trial
);
2647 if (next
&& NONJUMP_INSN_P (next
)
2648 && GET_CODE (PATTERN (next
)) != USE
2649 && ! reg_set_p (SET_DEST (pat
), next
)
2650 && ! reg_set_p (SET_SRC (pat
), next
)
2651 && reg_referenced_p (SET_DEST (pat
), PATTERN (next
))
2652 && ! modified_in_p (SET_DEST (pat
), next
))
2653 validate_replace_rtx (SET_DEST (pat
), SET_SRC (pat
), next
);
2657 /* If we stopped on a branch insn that has delay slots, see if we can
2658 steal some of the insns in those slots. */
2659 if (trial
&& NONJUMP_INSN_P (trial
)
2660 && GET_CODE (PATTERN (trial
)) == SEQUENCE
2661 && JUMP_P (XVECEXP (PATTERN (trial
), 0, 0)))
2663 rtx_sequence
*sequence
= as_a
<rtx_sequence
*> (PATTERN (trial
));
2664 /* If this is the `true' thread, we will want to follow the jump,
2665 so we can only do this if we have taken everything up to here. */
2666 if (thread_if_true
&& trial
== new_thread
)
2668 steal_delay_list_from_target (insn
, condition
, sequence
,
2669 delay_list
, &set
, &needed
,
2670 &opposite_needed
, slots_to_fill
,
2671 pslots_filled
, &must_annul
,
2673 /* If we owned the thread and are told that it branched
2674 elsewhere, make sure we own the thread at the new location. */
2675 if (own_thread
&& trial
!= new_thread
)
2676 own_thread
= own_thread_p (new_thread
, new_thread
, 0);
2678 else if (! thread_if_true
)
2679 steal_delay_list_from_fallthrough (insn
, condition
, sequence
,
2680 delay_list
, &set
, &needed
,
2681 &opposite_needed
, slots_to_fill
,
2682 pslots_filled
, &must_annul
);
2685 /* If we haven't found anything for this delay slot and it is very
2686 likely that the branch will be taken, see if the insn at our target
2687 increments or decrements a register with an increment that does not
2688 depend on the destination register. If so, try to place the opposite
2689 arithmetic insn after the jump insn and put the arithmetic insn in the
2690 delay slot. If we can't do this, return. */
2691 if (delay_list
->is_empty () && likely
2692 && new_thread
&& !ANY_RETURN_P (new_thread
)
2693 && NONJUMP_INSN_P (new_thread
)
2694 && !RTX_FRAME_RELATED_P (new_thread
)
2695 && GET_CODE (PATTERN (new_thread
)) != ASM_INPUT
2696 && asm_noperands (PATTERN (new_thread
)) < 0)
2698 rtx pat
= PATTERN (new_thread
);
2702 /* We know "new_thread" is an insn due to NONJUMP_INSN_P (new_thread)
2704 trial
= as_a
<rtx_insn
*> (new_thread
);
2705 pat
= PATTERN (trial
);
2707 if (!NONJUMP_INSN_P (trial
)
2708 || GET_CODE (pat
) != SET
2709 || ! eligible_for_delay (insn
, 0, trial
, flags
)
2710 || can_throw_internal (trial
))
2713 dest
= SET_DEST (pat
), src
= SET_SRC (pat
);
2714 if ((GET_CODE (src
) == PLUS
|| GET_CODE (src
) == MINUS
)
2715 && rtx_equal_p (XEXP (src
, 0), dest
)
2716 && (!FLOAT_MODE_P (GET_MODE (src
))
2717 || flag_unsafe_math_optimizations
)
2718 && ! reg_overlap_mentioned_p (dest
, XEXP (src
, 1))
2719 && ! side_effects_p (pat
))
2721 rtx other
= XEXP (src
, 1);
2725 /* If this is a constant adjustment, use the same code with
2726 the negated constant. Otherwise, reverse the sense of the
2728 if (CONST_INT_P (other
))
2729 new_arith
= gen_rtx_fmt_ee (GET_CODE (src
), GET_MODE (src
), dest
,
2730 negate_rtx (GET_MODE (src
), other
));
2732 new_arith
= gen_rtx_fmt_ee (GET_CODE (src
) == PLUS
? MINUS
: PLUS
,
2733 GET_MODE (src
), dest
, other
);
2735 ninsn
= emit_insn_after (gen_rtx_SET (dest
, new_arith
), insn
);
2737 if (recog_memoized (ninsn
) < 0
2738 || (extract_insn (ninsn
),
2739 !constrain_operands (1, get_preferred_alternatives (ninsn
))))
2741 delete_related_insns (ninsn
);
2747 update_block (trial
, thread
);
2748 if (trial
== thread
)
2750 thread
= next_active_insn (thread
);
2751 if (new_thread
== trial
)
2752 new_thread
= thread
;
2754 delete_related_insns (trial
);
2757 new_thread
= next_active_insn (trial
);
2759 ninsn
= own_thread
? trial
: copy_delay_slot_insn (trial
);
2761 INSN_FROM_TARGET_P (ninsn
) = 1;
2763 add_to_delay_list (ninsn
, delay_list
);
2768 if (!delay_list
->is_empty () && must_annul
)
2769 INSN_ANNULLED_BRANCH_P (insn
) = 1;
2771 /* If we are to branch into the middle of this thread, find an appropriate
2772 label or make a new one if none, and redirect INSN to it. If we hit the
2773 end of the function, use the end-of-function label. */
2774 if (new_thread
!= thread
)
2777 bool crossing
= false;
2779 gcc_assert (thread_if_true
);
2781 if (new_thread
&& simplejump_or_return_p (new_thread
)
2782 && redirect_with_delay_list_safe_p (insn
,
2783 JUMP_LABEL (new_thread
),
2785 new_thread
= follow_jumps (JUMP_LABEL (new_thread
), insn
,
2788 if (ANY_RETURN_P (new_thread
))
2789 label
= find_end_label (new_thread
);
2790 else if (LABEL_P (new_thread
))
2793 label
= get_label_before (as_a
<rtx_insn
*> (new_thread
),
2798 reorg_redirect_jump (insn
, label
);
2800 CROSSING_JUMP_P (insn
) = 1;
2805 /* Make another attempt to find insns to place in delay slots.
2807 We previously looked for insns located in front of the delay insn
2808 and, for non-jump delay insns, located behind the delay insn.
2810 Here only try to schedule jump insns and try to move insns from either
2811 the target or the following insns into the delay slot. If annulling is
2812 supported, we will be likely to do this. Otherwise, we can do this only
2816 fill_eager_delay_slots (void)
2820 int num_unfilled_slots
= unfilled_slots_next
- unfilled_slots_base
;
2822 for (i
= 0; i
< num_unfilled_slots
; i
++)
2825 rtx target_label
, insn_at_target
;
2826 rtx_insn
*fallthrough_insn
;
2827 auto_vec
<rtx_insn
*, 5> delay_list
;
2828 rtx_jump_insn
*jump_insn
;
2830 int own_fallthrough
;
2831 int prediction
, slots_to_fill
, slots_filled
;
2833 insn
= unfilled_slots_base
[i
];
2836 || ! (jump_insn
= dyn_cast
<rtx_jump_insn
*> (insn
))
2837 || ! (condjump_p (jump_insn
) || condjump_in_parallel_p (jump_insn
)))
2840 slots_to_fill
= num_delay_slots (jump_insn
);
2841 /* Some machine description have defined instructions to have
2842 delay slots only in certain circumstances which may depend on
2843 nearby insns (which change due to reorg's actions).
2845 For example, the PA port normally has delay slots for unconditional
2848 However, the PA port claims such jumps do not have a delay slot
2849 if they are immediate successors of certain CALL_INSNs. This
2850 allows the port to favor filling the delay slot of the call with
2851 the unconditional jump. */
2852 if (slots_to_fill
== 0)
2856 target_label
= JUMP_LABEL (jump_insn
);
2857 condition
= get_branch_condition (jump_insn
, target_label
);
2862 /* Get the next active fallthrough and target insns and see if we own
2863 them. Then see whether the branch is likely true. We don't need
2864 to do a lot of this for unconditional branches. */
2866 insn_at_target
= first_active_target_insn (target_label
);
2867 own_target
= own_thread_p (target_label
, target_label
, 0);
2869 if (condition
== const_true_rtx
)
2871 own_fallthrough
= 0;
2872 fallthrough_insn
= 0;
2877 fallthrough_insn
= next_active_insn (jump_insn
);
2878 own_fallthrough
= own_thread_p (NEXT_INSN (jump_insn
), NULL_RTX
, 1);
2879 prediction
= mostly_true_jump (jump_insn
);
2882 /* If this insn is expected to branch, first try to get insns from our
2883 target, then our fallthrough insns. If it is not expected to branch,
2884 try the other order. */
2888 fill_slots_from_thread (jump_insn
, condition
, insn_at_target
,
2889 fallthrough_insn
, prediction
== 2, 1,
2891 slots_to_fill
, &slots_filled
, &delay_list
);
2893 if (delay_list
.is_empty () && own_fallthrough
)
2895 /* Even though we didn't find anything for delay slots,
2896 we might have found a redundant insn which we deleted
2897 from the thread that was filled. So we have to recompute
2898 the next insn at the target. */
2899 target_label
= JUMP_LABEL (jump_insn
);
2900 insn_at_target
= first_active_target_insn (target_label
);
2902 fill_slots_from_thread (jump_insn
, condition
, fallthrough_insn
,
2903 insn_at_target
, 0, 0, own_fallthrough
,
2904 slots_to_fill
, &slots_filled
,
2910 if (own_fallthrough
)
2911 fill_slots_from_thread (jump_insn
, condition
, fallthrough_insn
,
2912 insn_at_target
, 0, 0, own_fallthrough
,
2913 slots_to_fill
, &slots_filled
, &delay_list
);
2915 if (delay_list
.is_empty ())
2916 fill_slots_from_thread (jump_insn
, condition
, insn_at_target
,
2917 next_active_insn (insn
), 0, 1, own_target
,
2918 slots_to_fill
, &slots_filled
, &delay_list
);
2921 if (!delay_list
.is_empty ())
2922 unfilled_slots_base
[i
]
2923 = emit_delay_sequence (jump_insn
, delay_list
, slots_filled
);
2925 if (slots_to_fill
== slots_filled
)
2926 unfilled_slots_base
[i
] = 0;
2928 note_delay_statistics (slots_filled
, 1);
2932 static void delete_computation (rtx_insn
*insn
);
2934 /* Recursively delete prior insns that compute the value (used only by INSN
2935 which the caller is deleting) stored in the register mentioned by NOTE
2936 which is a REG_DEAD note associated with INSN. */
2939 delete_prior_computation (rtx note
, rtx_insn
*insn
)
2942 rtx reg
= XEXP (note
, 0);
2944 for (our_prev
= prev_nonnote_insn (insn
);
2945 our_prev
&& (NONJUMP_INSN_P (our_prev
)
2946 || CALL_P (our_prev
));
2947 our_prev
= prev_nonnote_insn (our_prev
))
2949 rtx pat
= PATTERN (our_prev
);
2951 /* If we reach a CALL which is not calling a const function
2952 or the callee pops the arguments, then give up. */
2953 if (CALL_P (our_prev
)
2954 && (! RTL_CONST_CALL_P (our_prev
)
2955 || GET_CODE (pat
) != SET
|| GET_CODE (SET_SRC (pat
)) != CALL
))
2958 /* If we reach a SEQUENCE, it is too complex to try to
2959 do anything with it, so give up. We can be run during
2960 and after reorg, so SEQUENCE rtl can legitimately show
2962 if (GET_CODE (pat
) == SEQUENCE
)
2965 if (GET_CODE (pat
) == USE
2966 && NONJUMP_INSN_P (XEXP (pat
, 0)))
2967 /* reorg creates USEs that look like this. We leave them
2968 alone because reorg needs them for its own purposes. */
2971 if (reg_set_p (reg
, pat
))
2973 if (side_effects_p (pat
) && !CALL_P (our_prev
))
2976 if (GET_CODE (pat
) == PARALLEL
)
2978 /* If we find a SET of something else, we can't
2983 for (i
= 0; i
< XVECLEN (pat
, 0); i
++)
2985 rtx part
= XVECEXP (pat
, 0, i
);
2987 if (GET_CODE (part
) == SET
2988 && SET_DEST (part
) != reg
)
2992 if (i
== XVECLEN (pat
, 0))
2993 delete_computation (our_prev
);
2995 else if (GET_CODE (pat
) == SET
2996 && REG_P (SET_DEST (pat
)))
2998 int dest_regno
= REGNO (SET_DEST (pat
));
2999 int dest_endregno
= END_REGNO (SET_DEST (pat
));
3000 int regno
= REGNO (reg
);
3001 int endregno
= END_REGNO (reg
);
3003 if (dest_regno
>= regno
3004 && dest_endregno
<= endregno
)
3005 delete_computation (our_prev
);
3007 /* We may have a multi-word hard register and some, but not
3008 all, of the words of the register are needed in subsequent
3009 insns. Write REG_UNUSED notes for those parts that were not
3011 else if (dest_regno
<= regno
3012 && dest_endregno
>= endregno
)
3016 add_reg_note (our_prev
, REG_UNUSED
, reg
);
3018 for (i
= dest_regno
; i
< dest_endregno
; i
++)
3019 if (! find_regno_note (our_prev
, REG_UNUSED
, i
))
3022 if (i
== dest_endregno
)
3023 delete_computation (our_prev
);
3030 /* If PAT references the register that dies here, it is an
3031 additional use. Hence any prior SET isn't dead. However, this
3032 insn becomes the new place for the REG_DEAD note. */
3033 if (reg_overlap_mentioned_p (reg
, pat
))
3035 XEXP (note
, 1) = REG_NOTES (our_prev
);
3036 REG_NOTES (our_prev
) = note
;
3042 /* Delete INSN and recursively delete insns that compute values used only
3043 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3045 Look at all our REG_DEAD notes. If a previous insn does nothing other
3046 than set a register that dies in this insn, we can delete that insn
3049 On machines with CC0, if CC0 is used in this insn, we may be able to
3050 delete the insn that set it. */
3053 delete_computation (rtx_insn
*insn
)
3057 if (HAVE_cc0
&& reg_referenced_p (cc0_rtx
, PATTERN (insn
)))
3059 rtx_insn
*prev
= prev_nonnote_insn (insn
);
3060 /* We assume that at this stage
3061 CC's are always set explicitly
3062 and always immediately before the jump that
3063 will use them. So if the previous insn
3064 exists to set the CC's, delete it
3065 (unless it performs auto-increments, etc.). */
3066 if (prev
&& NONJUMP_INSN_P (prev
)
3067 && sets_cc0_p (PATTERN (prev
)))
3069 if (sets_cc0_p (PATTERN (prev
)) > 0
3070 && ! side_effects_p (PATTERN (prev
)))
3071 delete_computation (prev
);
3073 /* Otherwise, show that cc0 won't be used. */
3074 add_reg_note (prev
, REG_UNUSED
, cc0_rtx
);
3078 for (note
= REG_NOTES (insn
); note
; note
= next
)
3080 next
= XEXP (note
, 1);
3082 if (REG_NOTE_KIND (note
) != REG_DEAD
3083 /* Verify that the REG_NOTE is legitimate. */
3084 || !REG_P (XEXP (note
, 0)))
3087 delete_prior_computation (note
, insn
);
3090 delete_related_insns (insn
);
3093 /* If all INSN does is set the pc, delete it,
3094 and delete the insn that set the condition codes for it
3095 if that's what the previous thing was. */
3098 delete_jump (rtx_insn
*insn
)
3100 rtx set
= single_set (insn
);
3102 if (set
&& GET_CODE (SET_DEST (set
)) == PC
)
3103 delete_computation (insn
);
3107 label_before_next_insn (rtx_insn
*x
, rtx scan_limit
)
3109 rtx_insn
*insn
= next_active_insn (x
);
3112 insn
= PREV_INSN (insn
);
3113 if (insn
== scan_limit
|| insn
== NULL_RTX
)
3121 /* Return TRUE if there is a NOTE_INSN_SWITCH_TEXT_SECTIONS note in between
3125 switch_text_sections_between_p (const rtx_insn
*beg
, const rtx_insn
*end
)
3128 for (p
= beg
; p
!= end
; p
= NEXT_INSN (p
))
3129 if (NOTE_P (p
) && NOTE_KIND (p
) == NOTE_INSN_SWITCH_TEXT_SECTIONS
)
3135 /* Once we have tried two ways to fill a delay slot, make a pass over the
3136 code to try to improve the results and to do such things as more jump
3140 relax_delay_slots (rtx_insn
*first
)
3142 rtx_insn
*insn
, *next
;
3144 rtx_insn
*delay_insn
;
3147 /* Look at every JUMP_INSN and see if we can improve it. */
3148 for (insn
= first
; insn
; insn
= next
)
3150 rtx_insn
*other
, *prior_insn
;
3153 next
= next_active_insn (insn
);
3155 /* If this is a jump insn, see if it now jumps to a jump, jumps to
3156 the next insn, or jumps to a label that is not the last of a
3157 group of consecutive labels. */
3158 if (is_a
<rtx_jump_insn
*> (insn
)
3159 && (condjump_p (insn
) || condjump_in_parallel_p (insn
))
3160 && !ANY_RETURN_P (target_label
= JUMP_LABEL (insn
)))
3162 rtx_jump_insn
*jump_insn
= as_a
<rtx_jump_insn
*> (insn
);
3164 = skip_consecutive_labels (follow_jumps (target_label
, jump_insn
,
3166 if (ANY_RETURN_P (target_label
))
3167 target_label
= find_end_label (target_label
);
3170 && next_active_insn (as_a
<rtx_insn
*> (target_label
)) == next
3171 && ! condjump_in_parallel_p (jump_insn
)
3172 && ! (next
&& switch_text_sections_between_p (jump_insn
, next
)))
3174 delete_jump (jump_insn
);
3178 if (target_label
&& target_label
!= JUMP_LABEL (jump_insn
))
3180 reorg_redirect_jump (jump_insn
, target_label
);
3182 CROSSING_JUMP_P (jump_insn
) = 1;
3185 /* See if this jump conditionally branches around an unconditional
3186 jump. If so, invert this jump and point it to the target of the
3187 second jump. Check if it's possible on the target. */
3188 if (next
&& simplejump_or_return_p (next
)
3189 && any_condjump_p (jump_insn
)
3191 && (next_active_insn (as_a
<rtx_insn
*> (target_label
))
3192 == next_active_insn (next
))
3193 && no_labels_between_p (jump_insn
, next
)
3194 && targetm
.can_follow_jump (jump_insn
, next
))
3196 rtx label
= JUMP_LABEL (next
);
3198 /* Be careful how we do this to avoid deleting code or
3199 labels that are momentarily dead. See similar optimization
3202 We also need to ensure we properly handle the case when
3203 invert_jump fails. */
3205 ++LABEL_NUSES (target_label
);
3206 if (!ANY_RETURN_P (label
))
3207 ++LABEL_NUSES (label
);
3209 if (invert_jump (jump_insn
, label
, 1))
3211 delete_related_insns (next
);
3215 if (!ANY_RETURN_P (label
))
3216 --LABEL_NUSES (label
);
3218 if (--LABEL_NUSES (target_label
) == 0)
3219 delete_related_insns (target_label
);
3225 /* If this is an unconditional jump and the previous insn is a
3226 conditional jump, try reversing the condition of the previous
3227 insn and swapping our targets. The next pass might be able to
3230 Don't do this if we expect the conditional branch to be true, because
3231 we would then be making the more common case longer. */
3233 if (simplejump_or_return_p (insn
)
3234 && (other
= prev_active_insn (insn
)) != 0
3235 && any_condjump_p (other
)
3236 && no_labels_between_p (other
, insn
)
3237 && mostly_true_jump (other
) < 0)
3239 rtx other_target
= JUMP_LABEL (other
);
3240 target_label
= JUMP_LABEL (insn
);
3242 if (invert_jump (as_a
<rtx_jump_insn
*> (other
), target_label
, 0))
3243 reorg_redirect_jump (as_a
<rtx_jump_insn
*> (insn
), other_target
);
3246 /* Now look only at cases where we have a filled delay slot. */
3247 if (!NONJUMP_INSN_P (insn
) || GET_CODE (PATTERN (insn
)) != SEQUENCE
)
3250 pat
= as_a
<rtx_sequence
*> (PATTERN (insn
));
3251 delay_insn
= pat
->insn (0);
3253 /* See if the first insn in the delay slot is redundant with some
3254 previous insn. Remove it from the delay slot if so; then set up
3255 to reprocess this insn. */
3256 if ((prior_insn
= redundant_insn (pat
->insn (1), delay_insn
, vNULL
)))
3258 fix_reg_dead_note (prior_insn
, insn
);
3259 update_block (pat
->insn (1), insn
);
3260 delete_from_delay_slot (pat
->insn (1));
3261 next
= prev_active_insn (next
);
3265 /* See if we have a RETURN insn with a filled delay slot followed
3266 by a RETURN insn with an unfilled a delay slot. If so, we can delete
3267 the first RETURN (but not its delay insn). This gives the same
3268 effect in fewer instructions.
3270 Only do so if optimizing for size since this results in slower, but
3272 if (optimize_function_for_size_p (cfun
)
3273 && ANY_RETURN_P (PATTERN (delay_insn
))
3276 && PATTERN (next
) == PATTERN (delay_insn
))
3281 /* Delete the RETURN and just execute the delay list insns.
3283 We do this by deleting the INSN containing the SEQUENCE, then
3284 re-emitting the insns separately, and then deleting the RETURN.
3285 This allows the count of the jump target to be properly
3288 Note that we need to change the INSN_UID of the re-emitted insns
3289 since it is used to hash the insns for mark_target_live_regs and
3290 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3292 Clear the from target bit, since these insns are no longer
3294 for (i
= 0; i
< XVECLEN (pat
, 0); i
++)
3295 INSN_FROM_TARGET_P (XVECEXP (pat
, 0, i
)) = 0;
3297 rtx_insn
*prev
= PREV_INSN (insn
);
3298 delete_related_insns (insn
);
3299 gcc_assert (GET_CODE (pat
) == SEQUENCE
);
3300 add_insn_after (delay_insn
, prev
, NULL
);
3302 for (i
= 1; i
< pat
->len (); i
++)
3303 after
= emit_copy_of_insn_after (pat
->insn (i
), after
);
3304 delete_scheduled_jump (delay_insn
);
3308 /* Now look only at the cases where we have a filled JUMP_INSN. */
3309 rtx_jump_insn
*delay_jump_insn
=
3310 dyn_cast
<rtx_jump_insn
*> (delay_insn
);
3311 if (! delay_jump_insn
|| !(condjump_p (delay_jump_insn
)
3312 || condjump_in_parallel_p (delay_jump_insn
)))
3315 target_label
= JUMP_LABEL (delay_jump_insn
);
3316 if (target_label
&& ANY_RETURN_P (target_label
))
3319 /* If this jump goes to another unconditional jump, thread it, but
3320 don't convert a jump into a RETURN here. */
3321 rtx trial
= skip_consecutive_labels (follow_jumps (target_label
,
3324 if (ANY_RETURN_P (trial
))
3325 trial
= find_end_label (trial
);
3327 if (trial
&& trial
!= target_label
3328 && redirect_with_delay_slots_safe_p (delay_jump_insn
, trial
, insn
))
3330 reorg_redirect_jump (delay_jump_insn
, trial
);
3331 target_label
= trial
;
3333 CROSSING_JUMP_P (delay_jump_insn
) = 1;
3336 /* If the first insn at TARGET_LABEL is redundant with a previous
3337 insn, redirect the jump to the following insn and process again.
3338 We use next_real_nondebug_insn instead of next_active_insn so we
3339 don't skip USE-markers, or we'll end up with incorrect
3341 trial
= next_real_nondebug_insn (target_label
);
3342 if (trial
&& GET_CODE (PATTERN (trial
)) != SEQUENCE
3343 && redundant_insn (trial
, insn
, vNULL
)
3344 && ! can_throw_internal (trial
))
3346 /* Figure out where to emit the special USE insn so we don't
3347 later incorrectly compute register live/death info. */
3348 rtx_insn
*tmp
= next_active_insn (as_a
<rtx_insn
*> (trial
));
3350 tmp
= find_end_label (simple_return_rtx
);
3354 /* Insert the special USE insn and update dataflow info.
3355 We know "trial" is an insn here as it is the output of
3356 next_real_nondebug_insn () above. */
3357 update_block (as_a
<rtx_insn
*> (trial
), tmp
);
3359 /* Now emit a label before the special USE insn, and
3360 redirect our jump to the new label. */
3361 target_label
= get_label_before (PREV_INSN (tmp
), target_label
);
3362 reorg_redirect_jump (delay_jump_insn
, target_label
);
3368 /* Similarly, if it is an unconditional jump with one insn in its
3369 delay list and that insn is redundant, thread the jump. */
3370 rtx_sequence
*trial_seq
=
3371 trial
? dyn_cast
<rtx_sequence
*> (PATTERN (trial
)) : NULL
;
3373 && trial_seq
->len () == 2
3374 && JUMP_P (trial_seq
->insn (0))
3375 && simplejump_or_return_p (trial_seq
->insn (0))
3376 && redundant_insn (trial_seq
->insn (1), insn
, vNULL
))
3378 rtx temp_label
= JUMP_LABEL (trial_seq
->insn (0));
3379 if (ANY_RETURN_P (temp_label
))
3380 temp_label
= find_end_label (temp_label
);
3383 && redirect_with_delay_slots_safe_p (delay_jump_insn
,
3386 update_block (trial_seq
->insn (1), insn
);
3387 reorg_redirect_jump (delay_jump_insn
, temp_label
);
3393 /* See if we have a simple (conditional) jump that is useless. */
3394 if (!CROSSING_JUMP_P (delay_jump_insn
)
3395 && !INSN_ANNULLED_BRANCH_P (delay_jump_insn
)
3396 && !condjump_in_parallel_p (delay_jump_insn
)
3397 && prev_active_insn (as_a
<rtx_insn
*> (target_label
)) == insn
3398 && !BARRIER_P (prev_nonnote_insn (as_a
<rtx_insn
*> (target_label
)))
3399 /* If the last insn in the delay slot sets CC0 for some insn,
3400 various code assumes that it is in a delay slot. We could
3401 put it back where it belonged and delete the register notes,
3402 but it doesn't seem worthwhile in this uncommon case. */
3404 || ! find_reg_note (XVECEXP (pat
, 0, XVECLEN (pat
, 0) - 1),
3405 REG_CC_USER
, NULL_RTX
)))
3410 /* All this insn does is execute its delay list and jump to the
3411 following insn. So delete the jump and just execute the delay
3414 We do this by deleting the INSN containing the SEQUENCE, then
3415 re-emitting the insns separately, and then deleting the jump.
3416 This allows the count of the jump target to be properly
3419 Note that we need to change the INSN_UID of the re-emitted insns
3420 since it is used to hash the insns for mark_target_live_regs and
3421 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3423 Clear the from target bit, since these insns are no longer
3425 for (i
= 0; i
< XVECLEN (pat
, 0); i
++)
3426 INSN_FROM_TARGET_P (XVECEXP (pat
, 0, i
)) = 0;
3428 rtx_insn
*prev
= PREV_INSN (insn
);
3429 delete_related_insns (insn
);
3430 gcc_assert (GET_CODE (pat
) == SEQUENCE
);
3431 add_insn_after (delay_jump_insn
, prev
, NULL
);
3432 after
= delay_jump_insn
;
3433 for (i
= 1; i
< pat
->len (); i
++)
3434 after
= emit_copy_of_insn_after (pat
->insn (i
), after
);
3435 delete_scheduled_jump (delay_jump_insn
);
3439 /* See if this is an unconditional jump around a single insn which is
3440 identical to the one in its delay slot. In this case, we can just
3441 delete the branch and the insn in its delay slot. */
3442 if (next
&& NONJUMP_INSN_P (next
)
3443 && label_before_next_insn (next
, insn
) == target_label
3444 && simplejump_p (insn
)
3445 && XVECLEN (pat
, 0) == 2
3446 && rtx_equal_p (PATTERN (next
), PATTERN (pat
->insn (1))))
3448 delete_related_insns (insn
);
3452 /* See if this jump (with its delay slots) conditionally branches
3453 around an unconditional jump (without delay slots). If so, invert
3454 this jump and point it to the target of the second jump. We cannot
3455 do this for annulled jumps, though. Again, don't convert a jump to
3457 if (! INSN_ANNULLED_BRANCH_P (delay_jump_insn
)
3458 && any_condjump_p (delay_jump_insn
)
3459 && next
&& simplejump_or_return_p (next
)
3460 && (next_active_insn (as_a
<rtx_insn
*> (target_label
))
3461 == next_active_insn (next
))
3462 && no_labels_between_p (insn
, next
))
3464 rtx label
= JUMP_LABEL (next
);
3465 rtx old_label
= JUMP_LABEL (delay_jump_insn
);
3467 if (ANY_RETURN_P (label
))
3468 label
= find_end_label (label
);
3470 /* find_end_label can generate a new label. Check this first. */
3472 && no_labels_between_p (insn
, next
)
3473 && redirect_with_delay_slots_safe_p (delay_jump_insn
,
3476 /* Be careful how we do this to avoid deleting code or labels
3477 that are momentarily dead. See similar optimization in
3480 ++LABEL_NUSES (old_label
);
3482 if (invert_jump (delay_jump_insn
, label
, 1))
3486 /* Must update the INSN_FROM_TARGET_P bits now that
3487 the branch is reversed, so that mark_target_live_regs
3488 will handle the delay slot insn correctly. */
3489 for (i
= 1; i
< XVECLEN (PATTERN (insn
), 0); i
++)
3491 rtx slot
= XVECEXP (PATTERN (insn
), 0, i
);
3492 INSN_FROM_TARGET_P (slot
) = ! INSN_FROM_TARGET_P (slot
);
3495 delete_related_insns (next
);
3499 if (old_label
&& --LABEL_NUSES (old_label
) == 0)
3500 delete_related_insns (old_label
);
3505 /* If we own the thread opposite the way this insn branches, see if we
3506 can merge its delay slots with following insns. */
3507 if (INSN_FROM_TARGET_P (pat
->insn (1))
3508 && own_thread_p (NEXT_INSN (insn
), 0, 1))
3509 try_merge_delay_insns (insn
, next
);
3510 else if (! INSN_FROM_TARGET_P (pat
->insn (1))
3511 && own_thread_p (target_label
, target_label
, 0))
3512 try_merge_delay_insns (insn
,
3513 next_active_insn (as_a
<rtx_insn
*> (target_label
)));
3515 /* If we get here, we haven't deleted INSN. But we may have deleted
3516 NEXT, so recompute it. */
3517 next
= next_active_insn (insn
);
3522 /* Look for filled jumps to the end of function label. We can try to convert
3523 them into RETURN insns if the insns in the delay slot are valid for the
3527 make_return_insns (rtx_insn
*first
)
3530 rtx_jump_insn
*jump_insn
;
3531 rtx real_return_label
= function_return_label
;
3532 rtx real_simple_return_label
= function_simple_return_label
;
3535 /* See if there is a RETURN insn in the function other than the one we
3536 made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3537 into a RETURN to jump to it. */
3538 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
3539 if (JUMP_P (insn
) && ANY_RETURN_P (PATTERN (insn
)))
3541 rtx t
= get_label_before (insn
, NULL_RTX
);
3542 if (PATTERN (insn
) == ret_rtx
)
3543 real_return_label
= t
;
3545 real_simple_return_label
= t
;
3549 /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3550 was equal to END_OF_FUNCTION_LABEL. */
3551 if (real_return_label
)
3552 LABEL_NUSES (real_return_label
)++;
3553 if (real_simple_return_label
)
3554 LABEL_NUSES (real_simple_return_label
)++;
3556 /* Clear the list of insns to fill so we can use it. */
3557 obstack_free (&unfilled_slots_obstack
, unfilled_firstobj
);
3559 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
3562 rtx kind
, real_label
;
3564 /* Only look at filled JUMP_INSNs that go to the end of function
3566 if (!NONJUMP_INSN_P (insn
))
3569 if (GET_CODE (PATTERN (insn
)) != SEQUENCE
)
3572 rtx_sequence
*pat
= as_a
<rtx_sequence
*> (PATTERN (insn
));
3574 if (!jump_to_label_p (pat
->insn (0)))
3577 if (JUMP_LABEL (pat
->insn (0)) == function_return_label
)
3580 real_label
= real_return_label
;
3582 else if (JUMP_LABEL (pat
->insn (0)) == function_simple_return_label
)
3584 kind
= simple_return_rtx
;
3585 real_label
= real_simple_return_label
;
3590 jump_insn
= as_a
<rtx_jump_insn
*> (pat
->insn (0));
3592 /* If we can't make the jump into a RETURN, try to redirect it to the best
3593 RETURN and go on to the next insn. */
3594 if (!reorg_redirect_jump (jump_insn
, kind
))
3596 /* Make sure redirecting the jump will not invalidate the delay
3598 if (redirect_with_delay_slots_safe_p (jump_insn
, real_label
, insn
))
3599 reorg_redirect_jump (jump_insn
, real_label
);
3603 /* See if this RETURN can accept the insns current in its delay slot.
3604 It can if it has more or an equal number of slots and the contents
3605 of each is valid. */
3607 flags
= get_jump_flags (jump_insn
, JUMP_LABEL (jump_insn
));
3608 slots
= num_delay_slots (jump_insn
);
3609 if (slots
>= XVECLEN (pat
, 0) - 1)
3611 for (i
= 1; i
< XVECLEN (pat
, 0); i
++)
3613 #if ANNUL_IFFALSE_SLOTS
3614 (INSN_ANNULLED_BRANCH_P (jump_insn
)
3615 && INSN_FROM_TARGET_P (pat
->insn (i
)))
3616 ? eligible_for_annul_false (jump_insn
, i
- 1,
3617 pat
->insn (i
), flags
) :
3619 #if ANNUL_IFTRUE_SLOTS
3620 (INSN_ANNULLED_BRANCH_P (jump_insn
)
3621 && ! INSN_FROM_TARGET_P (pat
->insn (i
)))
3622 ? eligible_for_annul_true (jump_insn
, i
- 1,
3623 pat
->insn (i
), flags
) :
3625 eligible_for_delay (jump_insn
, i
- 1,
3626 pat
->insn (i
), flags
)))
3632 if (i
== XVECLEN (pat
, 0))
3635 /* We have to do something with this insn. If it is an unconditional
3636 RETURN, delete the SEQUENCE and output the individual insns,
3637 followed by the RETURN. Then set things up so we try to find
3638 insns for its delay slots, if it needs some. */
3639 if (ANY_RETURN_P (PATTERN (jump_insn
)))
3641 rtx_insn
*after
= PREV_INSN (insn
);
3643 delete_related_insns (insn
);
3645 for (i
= 1; i
< pat
->len (); i
++)
3646 after
= emit_copy_of_insn_after (pat
->insn (i
), after
);
3647 add_insn_after (insn
, after
, NULL
);
3648 emit_barrier_after (insn
);
3651 obstack_ptr_grow (&unfilled_slots_obstack
, insn
);
3654 /* It is probably more efficient to keep this with its current
3655 delay slot as a branch to a RETURN. */
3656 reorg_redirect_jump (jump_insn
, real_label
);
3659 /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3660 new delay slots we have created. */
3661 if (real_return_label
!= NULL_RTX
&& --LABEL_NUSES (real_return_label
) == 0)
3662 delete_related_insns (real_return_label
);
3663 if (real_simple_return_label
!= NULL_RTX
3664 && --LABEL_NUSES (real_simple_return_label
) == 0)
3665 delete_related_insns (real_simple_return_label
);
3667 fill_simple_delay_slots (1);
3668 fill_simple_delay_slots (0);
3671 /* Try to find insns to place in delay slots. */
3674 dbr_schedule (rtx_insn
*first
)
3676 rtx_insn
*insn
, *next
, *epilogue_insn
= 0;
3678 bool need_return_insns
;
3680 /* If the current function has no insns other than the prologue and
3681 epilogue, then do not try to fill any delay slots. */
3682 if (n_basic_blocks_for_fn (cfun
) == NUM_FIXED_BLOCKS
)
3685 /* Find the highest INSN_UID and allocate and initialize our map from
3686 INSN_UID's to position in code. */
3687 for (max_uid
= 0, insn
= first
; insn
; insn
= NEXT_INSN (insn
))
3689 if (INSN_UID (insn
) > max_uid
)
3690 max_uid
= INSN_UID (insn
);
3692 && NOTE_KIND (insn
) == NOTE_INSN_EPILOGUE_BEG
)
3693 epilogue_insn
= insn
;
3696 uid_to_ruid
= XNEWVEC (int, max_uid
+ 1);
3697 for (i
= 0, insn
= first
; insn
; i
++, insn
= NEXT_INSN (insn
))
3698 uid_to_ruid
[INSN_UID (insn
)] = i
;
3700 /* Initialize the list of insns that need filling. */
3701 if (unfilled_firstobj
== 0)
3703 gcc_obstack_init (&unfilled_slots_obstack
);
3704 unfilled_firstobj
= XOBNEWVAR (&unfilled_slots_obstack
, rtx
, 0);
3707 for (insn
= next_active_insn (first
); insn
; insn
= next_active_insn (insn
))
3711 /* Skip vector tables. We can't get attributes for them. */
3712 if (JUMP_TABLE_DATA_P (insn
))
3716 INSN_ANNULLED_BRANCH_P (insn
) = 0;
3717 INSN_FROM_TARGET_P (insn
) = 0;
3719 if (num_delay_slots (insn
) > 0)
3720 obstack_ptr_grow (&unfilled_slots_obstack
, insn
);
3722 /* Ensure all jumps go to the last of a set of consecutive labels. */
3724 && (condjump_p (insn
) || condjump_in_parallel_p (insn
))
3725 && !ANY_RETURN_P (JUMP_LABEL (insn
))
3726 && ((target
= skip_consecutive_labels (JUMP_LABEL (insn
)))
3727 != JUMP_LABEL (insn
)))
3728 redirect_jump (as_a
<rtx_jump_insn
*> (insn
), target
, 1);
3731 init_resource_info (epilogue_insn
);
3733 /* Show we haven't computed an end-of-function label yet. */
3734 function_return_label
= function_simple_return_label
= NULL
;
3736 /* Initialize the statistics for this function. */
3737 memset (num_insns_needing_delays
, 0, sizeof num_insns_needing_delays
);
3738 memset (num_filled_delays
, 0, sizeof num_filled_delays
);
3740 /* Now do the delay slot filling. Try everything twice in case earlier
3741 changes make more slots fillable. */
3743 for (reorg_pass_number
= 0;
3744 reorg_pass_number
< MAX_REORG_PASSES
;
3745 reorg_pass_number
++)
3747 fill_simple_delay_slots (1);
3748 fill_simple_delay_slots (0);
3749 if (!targetm
.no_speculation_in_delay_slots_p ())
3750 fill_eager_delay_slots ();
3751 relax_delay_slots (first
);
3754 /* If we made an end of function label, indicate that it is now
3755 safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3756 If it is now unused, delete it. */
3757 if (function_return_label
&& --LABEL_NUSES (function_return_label
) == 0)
3758 delete_related_insns (function_return_label
);
3759 if (function_simple_return_label
3760 && --LABEL_NUSES (function_simple_return_label
) == 0)
3761 delete_related_insns (function_simple_return_label
);
3763 need_return_insns
= false;
3764 need_return_insns
|= targetm
.have_return () && function_return_label
!= 0;
3765 need_return_insns
|= (targetm
.have_simple_return ()
3766 && function_simple_return_label
!= 0);
3767 if (need_return_insns
)
3768 make_return_insns (first
);
3770 /* Delete any USE insns made by update_block; subsequent passes don't need
3771 them or know how to deal with them. */
3772 for (insn
= first
; insn
; insn
= next
)
3774 next
= NEXT_INSN (insn
);
3776 if (NONJUMP_INSN_P (insn
) && GET_CODE (PATTERN (insn
)) == USE
3777 && INSN_P (XEXP (PATTERN (insn
), 0)))
3778 next
= delete_related_insns (insn
);
3781 obstack_free (&unfilled_slots_obstack
, unfilled_firstobj
);
3783 /* It is not clear why the line below is needed, but it does seem to be. */
3784 unfilled_firstobj
= XOBNEWVAR (&unfilled_slots_obstack
, rtx
, 0);
3788 int i
, j
, need_comma
;
3789 int total_delay_slots
[MAX_DELAY_HISTOGRAM
+ 1];
3790 int total_annul_slots
[MAX_DELAY_HISTOGRAM
+ 1];
3792 for (reorg_pass_number
= 0;
3793 reorg_pass_number
< MAX_REORG_PASSES
;
3794 reorg_pass_number
++)
3796 fprintf (dump_file
, ";; Reorg pass #%d:\n", reorg_pass_number
+ 1);
3797 for (i
= 0; i
< NUM_REORG_FUNCTIONS
; i
++)
3800 fprintf (dump_file
, ";; Reorg function #%d\n", i
);
3802 fprintf (dump_file
, ";; %d insns needing delay slots\n;; ",
3803 num_insns_needing_delays
[i
][reorg_pass_number
]);
3805 for (j
= 0; j
< MAX_DELAY_HISTOGRAM
+ 1; j
++)
3806 if (num_filled_delays
[i
][j
][reorg_pass_number
])
3809 fprintf (dump_file
, ", ");
3811 fprintf (dump_file
, "%d got %d delays",
3812 num_filled_delays
[i
][j
][reorg_pass_number
], j
);
3814 fprintf (dump_file
, "\n");
3817 memset (total_delay_slots
, 0, sizeof total_delay_slots
);
3818 memset (total_annul_slots
, 0, sizeof total_annul_slots
);
3819 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
3821 if (! insn
->deleted ()
3822 && NONJUMP_INSN_P (insn
)
3823 && GET_CODE (PATTERN (insn
)) != USE
3824 && GET_CODE (PATTERN (insn
)) != CLOBBER
3825 && GET_CODE (PATTERN (insn
)) != CLOBBER_HIGH
)
3827 if (GET_CODE (PATTERN (insn
)) == SEQUENCE
)
3830 j
= XVECLEN (PATTERN (insn
), 0) - 1;
3831 if (j
> MAX_DELAY_HISTOGRAM
)
3832 j
= MAX_DELAY_HISTOGRAM
;
3833 control
= XVECEXP (PATTERN (insn
), 0, 0);
3834 if (JUMP_P (control
) && INSN_ANNULLED_BRANCH_P (control
))
3835 total_annul_slots
[j
]++;
3837 total_delay_slots
[j
]++;
3839 else if (num_delay_slots (insn
) > 0)
3840 total_delay_slots
[0]++;
3843 fprintf (dump_file
, ";; Reorg totals: ");
3845 for (j
= 0; j
< MAX_DELAY_HISTOGRAM
+ 1; j
++)
3847 if (total_delay_slots
[j
])
3850 fprintf (dump_file
, ", ");
3852 fprintf (dump_file
, "%d got %d delays", total_delay_slots
[j
], j
);
3855 fprintf (dump_file
, "\n");
3857 if (ANNUL_IFTRUE_SLOTS
|| ANNUL_IFFALSE_SLOTS
)
3859 fprintf (dump_file
, ";; Reorg annuls: ");
3861 for (j
= 0; j
< MAX_DELAY_HISTOGRAM
+ 1; j
++)
3863 if (total_annul_slots
[j
])
3866 fprintf (dump_file
, ", ");
3868 fprintf (dump_file
, "%d got %d delays", total_annul_slots
[j
], j
);
3871 fprintf (dump_file
, "\n");
3874 fprintf (dump_file
, "\n");
3877 if (!sibling_labels
.is_empty ())
3879 update_alignments (sibling_labels
);
3880 sibling_labels
.release ();
3883 free_resource_info ();
3885 crtl
->dbr_scheduled_p
= true;
3888 /* Run delay slot optimization. */
3890 rest_of_handle_delay_slots (void)
3893 dbr_schedule (get_insns ());
3900 const pass_data pass_data_delay_slots
=
3902 RTL_PASS
, /* type */
3904 OPTGROUP_NONE
, /* optinfo_flags */
3905 TV_DBR_SCHED
, /* tv_id */
3906 0, /* properties_required */
3907 0, /* properties_provided */
3908 0, /* properties_destroyed */
3909 0, /* todo_flags_start */
3910 0, /* todo_flags_finish */
3913 class pass_delay_slots
: public rtl_opt_pass
3916 pass_delay_slots (gcc::context
*ctxt
)
3917 : rtl_opt_pass (pass_data_delay_slots
, ctxt
)
3920 /* opt_pass methods: */
3921 virtual bool gate (function
*);
3922 virtual unsigned int execute (function
*)
3924 return rest_of_handle_delay_slots ();
3927 }; // class pass_delay_slots
3930 pass_delay_slots::gate (function
*)
3932 /* At -O0 dataflow info isn't updated after RA. */
3934 return optimize
> 0 && flag_delayed_branch
&& !crtl
->dbr_scheduled_p
;
3942 make_pass_delay_slots (gcc::context
*ctxt
)
3944 return new pass_delay_slots (ctxt
);
3947 /* Machine dependent reorg pass. */
3951 const pass_data pass_data_machine_reorg
=
3953 RTL_PASS
, /* type */
3955 OPTGROUP_NONE
, /* optinfo_flags */
3956 TV_MACH_DEP
, /* 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_machine_reorg
: public rtl_opt_pass
3967 pass_machine_reorg (gcc::context
*ctxt
)
3968 : rtl_opt_pass (pass_data_machine_reorg
, ctxt
)
3971 /* opt_pass methods: */
3972 virtual bool gate (function
*)
3974 return targetm
.machine_dependent_reorg
!= 0;
3977 virtual unsigned int execute (function
*)
3979 targetm
.machine_dependent_reorg ();
3983 }; // class pass_machine_reorg
3988 make_pass_machine_reorg (gcc::context
*ctxt
)
3990 return new pass_machine_reorg (ctxt
);