PR rtl-optimization/83496
[official-gcc.git] / gcc / reorg.c
blobecdc3752af3c5cb5cf5fae4b2bc507802740e090
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
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Instruction reorganization pass.
24 This pass runs after register allocation and final jump
25 optimization. It should be the last pass to run before peephole.
26 It serves primarily to fill delay slots of insns, typically branch
27 and call insns. Other insns typically involve more complicated
28 interactions of data dependencies and resource constraints, and
29 are better handled by scheduling before register allocation (by the
30 function `schedule_insns').
32 The Branch Penalty is the number of extra cycles that are needed to
33 execute a branch insn. On an ideal machine, branches take a single
34 cycle, and the Branch Penalty is 0. Several RISC machines approach
35 branch delays differently:
37 The MIPS has a single branch delay slot. Most insns
38 (except other branches) can be used to fill this slot. When the
39 slot is filled, two insns execute in two cycles, reducing the
40 branch penalty to zero.
42 The SPARC always has a branch delay slot, but its effects can be
43 annulled when the branch is not taken. This means that failing to
44 find other sources of insns, we can hoist an insn from the branch
45 target that would only be safe to execute knowing that the branch
46 is taken.
48 The HP-PA always has a branch delay slot. For unconditional branches
49 its effects can be annulled when the branch is taken. The effects
50 of the delay slot in a conditional branch can be nullified for forward
51 taken branches, or for untaken backward branches. This means
52 we can hoist insns from the fall-through path for forward branches or
53 steal insns from the target of backward branches.
55 The TMS320C3x and C4x have three branch delay slots. When the three
56 slots are filled, the branch penalty is zero. Most insns can fill the
57 delay slots except jump insns.
59 Three techniques for filling delay slots have been implemented so far:
61 (1) `fill_simple_delay_slots' is the simplest, most efficient way
62 to fill delay slots. This pass first looks for insns which come
63 from before the branch and which are safe to execute after the
64 branch. Then it searches after the insn requiring delay slots or,
65 in the case of a branch, for insns that are after the point at
66 which the branch merges into the fallthrough code, if such a point
67 exists. When such insns are found, the branch penalty decreases
68 and no code expansion takes place.
70 (2) `fill_eager_delay_slots' is more complicated: it is used for
71 scheduling conditional jumps, or for scheduling jumps which cannot
72 be filled using (1). A machine need not have annulled jumps to use
73 this strategy, but it helps (by keeping more options open).
74 `fill_eager_delay_slots' tries to guess the direction the branch
75 will go; if it guesses right 100% of the time, it can reduce the
76 branch penalty as much as `fill_simple_delay_slots' does. If it
77 guesses wrong 100% of the time, it might as well schedule nops. When
78 `fill_eager_delay_slots' takes insns from the fall-through path of
79 the jump, usually there is no code expansion; when it takes insns
80 from the branch target, there is code expansion if it is not the
81 only way to reach that target.
83 (3) `relax_delay_slots' uses a set of rules to simplify code that
84 has been reorganized by (1) and (2). It finds cases where
85 conditional test can be eliminated, jumps can be threaded, extra
86 insns can be eliminated, etc. It is the job of (1) and (2) to do a
87 good job of scheduling locally; `relax_delay_slots' takes care of
88 making the various individual schedules work well together. It is
89 especially tuned to handle the control flow interactions of branch
90 insns. It does nothing for insns with delay slots that do not
91 branch.
93 On machines that use CC0, we are very conservative. We will not make
94 a copy of an insn involving CC0 since we want to maintain a 1-1
95 correspondence between the insn that sets and uses CC0. The insns are
96 allowed to be separated by placing an insn that sets CC0 (but not an insn
97 that uses CC0; we could do this, but it doesn't seem worthwhile) in a
98 delay slot. In that case, we point each insn at the other with REG_CC_USER
99 and REG_CC_SETTER notes. Note that these restrictions affect very few
100 machines because most RISC machines with delay slots will not use CC0
101 (the RT is the only known exception at this point). */
103 #include "config.h"
104 #include "system.h"
105 #include "coretypes.h"
106 #include "backend.h"
107 #include "target.h"
108 #include "rtl.h"
109 #include "tree.h"
110 #include "predict.h"
111 #include "memmodel.h"
112 #include "tm_p.h"
113 #include "expmed.h"
114 #include "insn-config.h"
115 #include "emit-rtl.h"
116 #include "recog.h"
117 #include "insn-attr.h"
118 #include "resource.h"
119 #include "params.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. */
130 static rtx
131 skip_consecutive_labels (rtx label_or_return)
133 rtx_insn *insn;
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))
141 if (LABEL_P (insn))
142 label = insn;
144 return label;
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. */
150 static void
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
178 return insn. */
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'. */
188 static int max_uid;
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 *> &,
196 int);
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 *,
210 vec<rtx_insn *> *,
211 struct resources *,
212 struct resources *,
213 struct resources *,
214 int, int *, int *,
215 rtx *);
216 static void steal_delay_list_from_fallthrough (rtx_insn *, rtx, rtx_sequence *,
217 vec<rtx_insn *> *,
218 struct resources *,
219 struct resources *,
220 struct resources *,
221 int, int *, int *);
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,
232 int, int, int, int,
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
239 unchanged. */
241 static rtx
242 first_active_target_insn (rtx insn)
244 if (ANY_RETURN_P (insn))
245 return 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. */
251 static bool
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. */
263 static int
264 stop_search_p (rtx_insn *insn, int labels_p)
266 if (insn == 0)
267 return 1;
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))
273 return 1;
275 switch (GET_CODE (insn))
277 case NOTE:
278 case CALL_INSN:
279 return 0;
281 case CODE_LABEL:
282 return labels_p;
284 case JUMP_INSN:
285 case BARRIER:
286 return 1;
288 case INSN:
289 /* OK unless it contains a delay slot or is an `asm' insn of some type.
290 We don't know anything about these. */
291 return (GET_CODE (PATTERN (insn)) == SEQUENCE
292 || GET_CODE (PATTERN (insn)) == ASM_INPUT
293 || asm_noperands (PATTERN (insn)) >= 0);
295 default:
296 gcc_unreachable ();
300 /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
301 resource set contains a volatile memory reference. Otherwise, return FALSE. */
303 static int
304 resource_conflicts_p (struct resources *res1, struct resources *res2)
306 if ((res1->cc && res2->cc) || (res1->memory && res2->memory)
307 || res1->volatil || res2->volatil)
308 return 1;
310 return hard_reg_set_intersect_p (res1->regs, res2->regs);
313 /* Return TRUE if any resource marked in RES, a `struct resources', is
314 referenced by INSN. If INCLUDE_DELAYED_EFFECTS is set, return if the called
315 routine is using those resources.
317 We compute this by computing all the resources referenced by INSN and
318 seeing if this conflicts with RES. It might be faster to directly check
319 ourselves, and this is the way it used to work, but it means duplicating
320 a large block of complex code. */
322 static int
323 insn_references_resource_p (rtx insn, struct resources *res,
324 bool include_delayed_effects)
326 struct resources insn_res;
328 CLEAR_RESOURCE (&insn_res);
329 mark_referenced_resources (insn, &insn_res, include_delayed_effects);
330 return resource_conflicts_p (&insn_res, res);
333 /* Return TRUE if INSN modifies resources that are marked in RES.
334 INCLUDE_DELAYED_EFFECTS is set if the actions of that routine should be
335 included. CC0 is only modified if it is explicitly set; see comments
336 in front of mark_set_resources for details. */
338 static int
339 insn_sets_resource_p (rtx insn, struct resources *res,
340 bool include_delayed_effects)
342 struct resources insn_sets;
344 CLEAR_RESOURCE (&insn_sets);
345 mark_set_resources (insn, &insn_sets, 0,
346 (include_delayed_effects
347 ? MARK_SRC_DEST_CALL
348 : MARK_SRC_DEST));
349 return resource_conflicts_p (&insn_sets, res);
352 /* Find a label at the end of the function or before a RETURN. If there
353 is none, try to make one. If that fails, returns 0.
355 The property of such a label is that it is placed just before the
356 epilogue or a bare RETURN insn, so that another bare RETURN can be
357 turned into a jump to the label unconditionally. In particular, the
358 label cannot be placed before a RETURN insn with a filled delay slot.
360 ??? There may be a problem with the current implementation. Suppose
361 we start with a bare RETURN insn and call find_end_label. It may set
362 function_return_label just before the RETURN. Suppose the machinery
363 is able to fill the delay slot of the RETURN insn afterwards. Then
364 function_return_label is no longer valid according to the property
365 described above and find_end_label will still return it unmodified.
366 Note that this is probably mitigated by the following observation:
367 once function_return_label is made, it is very likely the target of
368 a jump, so filling the delay slot of the RETURN will be much more
369 difficult.
370 KIND is either simple_return_rtx or ret_rtx, indicating which type of
371 return we're looking for. */
373 static rtx_code_label *
374 find_end_label (rtx kind)
376 rtx_insn *insn;
377 rtx_code_label **plabel;
379 if (kind == ret_rtx)
380 plabel = &function_return_label;
381 else
383 gcc_assert (kind == simple_return_rtx);
384 plabel = &function_simple_return_label;
387 /* If we found one previously, return it. */
388 if (*plabel)
389 return *plabel;
391 /* Otherwise, see if there is a label at the end of the function. If there
392 is, it must be that RETURN insns aren't needed, so that is our return
393 label and we don't have to do anything else. */
395 insn = get_last_insn ();
396 while (NOTE_P (insn)
397 || (NONJUMP_INSN_P (insn)
398 && (GET_CODE (PATTERN (insn)) == USE
399 || GET_CODE (PATTERN (insn)) == CLOBBER)))
400 insn = PREV_INSN (insn);
402 /* When a target threads its epilogue we might already have a
403 suitable return insn. If so put a label before it for the
404 function_return_label. */
405 if (BARRIER_P (insn)
406 && JUMP_P (PREV_INSN (insn))
407 && PATTERN (PREV_INSN (insn)) == kind)
409 rtx_insn *temp = PREV_INSN (PREV_INSN (insn));
410 rtx_code_label *label = gen_label_rtx ();
411 LABEL_NUSES (label) = 0;
413 /* Put the label before any USE insns that may precede the RETURN
414 insn. */
415 while (GET_CODE (temp) == USE)
416 temp = PREV_INSN (temp);
418 emit_label_after (label, temp);
419 *plabel = label;
422 else if (LABEL_P (insn))
423 *plabel = as_a <rtx_code_label *> (insn);
424 else
426 rtx_code_label *label = gen_label_rtx ();
427 LABEL_NUSES (label) = 0;
428 /* If the basic block reorder pass moves the return insn to
429 some other place try to locate it again and put our
430 function_return_label there. */
431 while (insn && ! (JUMP_P (insn) && (PATTERN (insn) == kind)))
432 insn = PREV_INSN (insn);
433 if (insn)
435 insn = PREV_INSN (insn);
437 /* Put the label before any USE insns that may precede the
438 RETURN insn. */
439 while (GET_CODE (insn) == USE)
440 insn = PREV_INSN (insn);
442 emit_label_after (label, insn);
444 else
446 if (targetm.have_epilogue () && ! targetm.have_return ())
447 /* The RETURN insn has its delay slot filled so we cannot
448 emit the label just before it. Since we already have
449 an epilogue and cannot emit a new RETURN, we cannot
450 emit the label at all. */
451 return NULL;
453 /* Otherwise, make a new label and emit a RETURN and BARRIER,
454 if needed. */
455 emit_label (label);
456 if (targetm.have_return ())
458 /* The return we make may have delay slots too. */
459 rtx_insn *pat = targetm.gen_return ();
460 rtx_insn *insn = emit_jump_insn (pat);
461 set_return_jump_label (insn);
462 emit_barrier ();
463 if (num_delay_slots (insn) > 0)
464 obstack_ptr_grow (&unfilled_slots_obstack, insn);
467 *plabel = label;
470 /* Show one additional use for this label so it won't go away until
471 we are done. */
472 ++LABEL_NUSES (*plabel);
474 return *plabel;
477 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
478 the pattern of INSN with the SEQUENCE.
480 Returns the insn containing the SEQUENCE that replaces INSN. */
482 static rtx_insn *
483 emit_delay_sequence (rtx_insn *insn, const vec<rtx_insn *> &list, int length)
485 /* Allocate the rtvec to hold the insns and the SEQUENCE. */
486 rtvec seqv = rtvec_alloc (length + 1);
487 rtx seq = gen_rtx_SEQUENCE (VOIDmode, seqv);
488 rtx_insn *seq_insn = make_insn_raw (seq);
490 /* If DELAY_INSN has a location, use it for SEQ_INSN. If DELAY_INSN does
491 not have a location, but one of the delayed insns does, we pick up a
492 location from there later. */
493 INSN_LOCATION (seq_insn) = INSN_LOCATION (insn);
495 /* Unlink INSN from the insn chain, so that we can put it into
496 the SEQUENCE. Remember where we want to emit SEQUENCE in AFTER. */
497 rtx_insn *after = PREV_INSN (insn);
498 remove_insn (insn);
499 SET_NEXT_INSN (insn) = SET_PREV_INSN (insn) = NULL;
501 /* Build our SEQUENCE and rebuild the insn chain. */
502 start_sequence ();
503 XVECEXP (seq, 0, 0) = emit_insn (insn);
505 unsigned int delay_insns = list.length ();
506 gcc_assert (delay_insns == (unsigned int) length);
507 for (unsigned int i = 0; i < delay_insns; i++)
509 rtx_insn *tem = list[i];
510 rtx note, next;
512 /* Show that this copy of the insn isn't deleted. */
513 tem->set_undeleted ();
515 /* Unlink insn from its original place, and re-emit it into
516 the sequence. */
517 SET_NEXT_INSN (tem) = SET_PREV_INSN (tem) = NULL;
518 XVECEXP (seq, 0, i + 1) = emit_insn (tem);
520 /* SPARC assembler, for instance, emit warning when debug info is output
521 into the delay slot. */
522 if (INSN_LOCATION (tem) && !INSN_LOCATION (seq_insn))
523 INSN_LOCATION (seq_insn) = INSN_LOCATION (tem);
524 INSN_LOCATION (tem) = 0;
526 for (note = REG_NOTES (tem); note; note = next)
528 next = XEXP (note, 1);
529 switch (REG_NOTE_KIND (note))
531 case REG_DEAD:
532 /* Remove any REG_DEAD notes because we can't rely on them now
533 that the insn has been moved. */
534 remove_note (tem, note);
535 break;
537 case REG_LABEL_OPERAND:
538 case REG_LABEL_TARGET:
539 /* Keep the label reference count up to date. */
540 if (LABEL_P (XEXP (note, 0)))
541 LABEL_NUSES (XEXP (note, 0)) ++;
542 break;
544 default:
545 break;
549 end_sequence ();
551 /* Splice our SEQUENCE into the insn stream where INSN used to be. */
552 add_insn_after (seq_insn, after, NULL);
554 return seq_insn;
557 /* Add INSN to DELAY_LIST and return the head of the new list. The list must
558 be in the order in which the insns are to be executed. */
560 static void
561 add_to_delay_list (rtx_insn *insn, vec<rtx_insn *> *delay_list)
563 /* If INSN has its block number recorded, clear it since we may
564 be moving the insn to a new block. */
565 clear_hashed_info_for_insn (insn);
566 delay_list->safe_push (insn);
569 /* Delete INSN from the delay slot of the insn that it is in, which may
570 produce an insn with no delay slots. Return the new insn. */
572 static rtx_insn *
573 delete_from_delay_slot (rtx_insn *insn)
575 rtx_insn *trial, *seq_insn, *prev;
576 rtx_sequence *seq;
577 int i;
578 int had_barrier = 0;
580 /* We first must find the insn containing the SEQUENCE with INSN in its
581 delay slot. Do this by finding an insn, TRIAL, where
582 PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
584 for (trial = insn;
585 PREV_INSN (NEXT_INSN (trial)) == trial;
586 trial = NEXT_INSN (trial))
589 seq_insn = PREV_INSN (NEXT_INSN (trial));
590 seq = as_a <rtx_sequence *> (PATTERN (seq_insn));
592 if (NEXT_INSN (seq_insn) && BARRIER_P (NEXT_INSN (seq_insn)))
593 had_barrier = 1;
595 /* Create a delay list consisting of all the insns other than the one
596 we are deleting (unless we were the only one). */
597 auto_vec<rtx_insn *, 5> delay_list;
598 if (seq->len () > 2)
599 for (i = 1; i < seq->len (); i++)
600 if (seq->insn (i) != insn)
601 add_to_delay_list (seq->insn (i), &delay_list);
603 /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
604 list, and rebuild the delay list if non-empty. */
605 prev = PREV_INSN (seq_insn);
606 trial = seq->insn (0);
607 delete_related_insns (seq_insn);
608 add_insn_after (trial, prev, NULL);
610 /* If there was a barrier after the old SEQUENCE, remit it. */
611 if (had_barrier)
612 emit_barrier_after (trial);
614 /* If there are any delay insns, remit them. Otherwise clear the
615 annul flag. */
616 if (!delay_list.is_empty ())
617 trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2);
618 else if (JUMP_P (trial))
619 INSN_ANNULLED_BRANCH_P (trial) = 0;
621 INSN_FROM_TARGET_P (insn) = 0;
623 /* Show we need to fill this insn again. */
624 obstack_ptr_grow (&unfilled_slots_obstack, trial);
626 return trial;
629 /* Delete INSN, a JUMP_INSN. If it is a conditional jump, we must track down
630 the insn that sets CC0 for it and delete it too. */
632 static void
633 delete_scheduled_jump (rtx_insn *insn)
635 /* Delete the insn that sets cc0 for us. On machines without cc0, we could
636 delete the insn that sets the condition code, but it is hard to find it.
637 Since this case is rare anyway, don't bother trying; there would likely
638 be other insns that became dead anyway, which we wouldn't know to
639 delete. */
641 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, insn))
643 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
645 /* If a reg-note was found, it points to an insn to set CC0. This
646 insn is in the delay list of some other insn. So delete it from
647 the delay list it was in. */
648 if (note)
650 if (! FIND_REG_INC_NOTE (XEXP (note, 0), NULL_RTX)
651 && sets_cc0_p (PATTERN (XEXP (note, 0))) == 1)
652 delete_from_delay_slot (as_a <rtx_insn *> (XEXP (note, 0)));
654 else
656 /* The insn setting CC0 is our previous insn, but it may be in
657 a delay slot. It will be the last insn in the delay slot, if
658 it is. */
659 rtx_insn *trial = previous_insn (insn);
660 if (NOTE_P (trial))
661 trial = prev_nonnote_insn (trial);
662 if (sets_cc0_p (PATTERN (trial)) != 1
663 || FIND_REG_INC_NOTE (trial, NULL_RTX))
664 return;
665 if (PREV_INSN (NEXT_INSN (trial)) == trial)
666 delete_related_insns (trial);
667 else
668 delete_from_delay_slot (trial);
672 delete_related_insns (insn);
675 /* Counters for delay-slot filling. */
677 #define NUM_REORG_FUNCTIONS 2
678 #define MAX_DELAY_HISTOGRAM 3
679 #define MAX_REORG_PASSES 2
681 static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
683 static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
685 static int reorg_pass_number;
687 static void
688 note_delay_statistics (int slots_filled, int index)
690 num_insns_needing_delays[index][reorg_pass_number]++;
691 if (slots_filled > MAX_DELAY_HISTOGRAM)
692 slots_filled = MAX_DELAY_HISTOGRAM;
693 num_filled_delays[index][slots_filled][reorg_pass_number]++;
696 /* Optimize the following cases:
698 1. When a conditional branch skips over only one instruction,
699 use an annulling branch and put that insn in the delay slot.
700 Use either a branch that annuls when the condition if true or
701 invert the test with a branch that annuls when the condition is
702 false. This saves insns, since otherwise we must copy an insn
703 from the L1 target.
705 (orig) (skip) (otherwise)
706 Bcc.n L1 Bcc',a L1 Bcc,a L1'
707 insn insn insn2
708 L1: L1: L1:
709 insn2 insn2 insn2
710 insn3 insn3 L1':
711 insn3
713 2. When a conditional branch skips over only one instruction,
714 and after that, it unconditionally branches somewhere else,
715 perform the similar optimization. This saves executing the
716 second branch in the case where the inverted condition is true.
718 Bcc.n L1 Bcc',a L2
719 insn insn
720 L1: L1:
721 Bra L2 Bra L2
723 INSN is a JUMP_INSN.
725 This should be expanded to skip over N insns, where N is the number
726 of delay slots required. */
728 static void
729 optimize_skip (rtx_jump_insn *insn, vec<rtx_insn *> *delay_list)
731 rtx_insn *trial = next_nonnote_insn (insn);
732 rtx_insn *next_trial = next_active_insn (trial);
733 int flags;
735 flags = get_jump_flags (insn, JUMP_LABEL (insn));
737 if (trial == 0
738 || !NONJUMP_INSN_P (trial)
739 || GET_CODE (PATTERN (trial)) == SEQUENCE
740 || recog_memoized (trial) < 0
741 || (! eligible_for_annul_false (insn, 0, trial, flags)
742 && ! eligible_for_annul_true (insn, 0, trial, flags))
743 || RTX_FRAME_RELATED_P (trial)
744 || can_throw_internal (trial))
745 return;
747 /* There are two cases where we are just executing one insn (we assume
748 here that a branch requires only one insn; this should be generalized
749 at some point): Where the branch goes around a single insn or where
750 we have one insn followed by a branch to the same label we branch to.
751 In both of these cases, inverting the jump and annulling the delay
752 slot give the same effect in fewer insns. */
753 if (next_trial == next_active_insn (JUMP_LABEL_AS_INSN (insn))
754 || (next_trial != 0
755 && simplejump_or_return_p (next_trial)
756 && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)))
758 if (eligible_for_annul_false (insn, 0, trial, flags))
760 if (invert_jump (insn, JUMP_LABEL (insn), 1))
761 INSN_FROM_TARGET_P (trial) = 1;
762 else if (! eligible_for_annul_true (insn, 0, trial, flags))
763 return;
766 add_to_delay_list (trial, delay_list);
767 next_trial = next_active_insn (trial);
768 update_block (trial, trial);
769 delete_related_insns (trial);
771 /* Also, if we are targeting an unconditional
772 branch, thread our jump to the target of that branch. Don't
773 change this into a RETURN here, because it may not accept what
774 we have in the delay slot. We'll fix this up later. */
775 if (next_trial && simplejump_or_return_p (next_trial))
777 rtx target_label = JUMP_LABEL (next_trial);
778 if (ANY_RETURN_P (target_label))
779 target_label = find_end_label (target_label);
781 if (target_label)
783 /* Recompute the flags based on TARGET_LABEL since threading
784 the jump to TARGET_LABEL may change the direction of the
785 jump (which may change the circumstances in which the
786 delay slot is nullified). */
787 flags = get_jump_flags (insn, target_label);
788 if (eligible_for_annul_true (insn, 0, trial, flags))
789 reorg_redirect_jump (insn, target_label);
793 INSN_ANNULLED_BRANCH_P (insn) = 1;
797 /* Encode and return branch direction and prediction information for
798 INSN assuming it will jump to LABEL.
800 Non conditional branches return no direction information and
801 are predicted as very likely taken. */
803 static int
804 get_jump_flags (const rtx_insn *insn, rtx label)
806 int flags;
808 /* get_jump_flags can be passed any insn with delay slots, these may
809 be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
810 direction information, and only if they are conditional jumps.
812 If LABEL is a return, then there is no way to determine the branch
813 direction. */
814 if (JUMP_P (insn)
815 && (condjump_p (insn) || condjump_in_parallel_p (insn))
816 && !ANY_RETURN_P (label)
817 && INSN_UID (insn) <= max_uid
818 && INSN_UID (label) <= max_uid)
819 flags
820 = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
821 ? ATTR_FLAG_forward : ATTR_FLAG_backward;
822 /* No valid direction information. */
823 else
824 flags = 0;
826 return flags;
829 /* Return truth value of the statement that this branch
830 is mostly taken. If we think that the branch is extremely likely
831 to be taken, we return 2. If the branch is slightly more likely to be
832 taken, return 1. If the branch is slightly less likely to be taken,
833 return 0 and if the branch is highly unlikely to be taken, return -1. */
835 static int
836 mostly_true_jump (rtx jump_insn)
838 /* If branch probabilities are available, then use that number since it
839 always gives a correct answer. */
840 rtx note = find_reg_note (jump_insn, REG_BR_PROB, 0);
841 if (note)
843 int prob = profile_probability::from_reg_br_prob_note (XINT (note, 0))
844 .to_reg_br_prob_base ();
846 if (prob >= REG_BR_PROB_BASE * 9 / 10)
847 return 2;
848 else if (prob >= REG_BR_PROB_BASE / 2)
849 return 1;
850 else if (prob >= REG_BR_PROB_BASE / 10)
851 return 0;
852 else
853 return -1;
856 /* If there is no note, assume branches are not taken.
857 This should be rare. */
858 return 0;
861 /* Return the condition under which INSN will branch to TARGET. If TARGET
862 is zero, return the condition under which INSN will return. If INSN is
863 an unconditional branch, return const_true_rtx. If INSN isn't a simple
864 type of jump, or it doesn't go to TARGET, return 0. */
866 static rtx
867 get_branch_condition (const rtx_insn *insn, rtx target)
869 rtx pat = PATTERN (insn);
870 rtx src;
872 if (condjump_in_parallel_p (insn))
873 pat = XVECEXP (pat, 0, 0);
875 if (ANY_RETURN_P (pat) && pat == target)
876 return const_true_rtx;
878 if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
879 return 0;
881 src = SET_SRC (pat);
882 if (GET_CODE (src) == LABEL_REF && label_ref_label (src) == target)
883 return const_true_rtx;
885 else if (GET_CODE (src) == IF_THEN_ELSE
886 && XEXP (src, 2) == pc_rtx
887 && ((GET_CODE (XEXP (src, 1)) == LABEL_REF
888 && label_ref_label (XEXP (src, 1)) == target)
889 || (ANY_RETURN_P (XEXP (src, 1)) && XEXP (src, 1) == target)))
890 return XEXP (src, 0);
892 else if (GET_CODE (src) == IF_THEN_ELSE
893 && XEXP (src, 1) == pc_rtx
894 && ((GET_CODE (XEXP (src, 2)) == LABEL_REF
895 && label_ref_label (XEXP (src, 2)) == target)
896 || (ANY_RETURN_P (XEXP (src, 2)) && XEXP (src, 2) == target)))
898 enum rtx_code rev;
899 rev = reversed_comparison_code (XEXP (src, 0), insn);
900 if (rev != UNKNOWN)
901 return gen_rtx_fmt_ee (rev, GET_MODE (XEXP (src, 0)),
902 XEXP (XEXP (src, 0), 0),
903 XEXP (XEXP (src, 0), 1));
906 return 0;
909 /* Return nonzero if CONDITION is more strict than the condition of
910 INSN, i.e., if INSN will always branch if CONDITION is true. */
912 static int
913 condition_dominates_p (rtx condition, const rtx_insn *insn)
915 rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
916 enum rtx_code code = GET_CODE (condition);
917 enum rtx_code other_code;
919 if (rtx_equal_p (condition, other_condition)
920 || other_condition == const_true_rtx)
921 return 1;
923 else if (condition == const_true_rtx || other_condition == 0)
924 return 0;
926 other_code = GET_CODE (other_condition);
927 if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
928 || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
929 || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
930 return 0;
932 return comparison_dominates_p (code, other_code);
935 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
936 any insns already in the delay slot of JUMP. */
938 static int
939 redirect_with_delay_slots_safe_p (rtx_insn *jump, rtx newlabel, rtx seq)
941 int flags, i;
942 rtx_sequence *pat = as_a <rtx_sequence *> (PATTERN (seq));
944 /* Make sure all the delay slots of this jump would still
945 be valid after threading the jump. If they are still
946 valid, then return nonzero. */
948 flags = get_jump_flags (jump, newlabel);
949 for (i = 1; i < pat->len (); i++)
950 if (! (
951 #if ANNUL_IFFALSE_SLOTS
952 (INSN_ANNULLED_BRANCH_P (jump)
953 && INSN_FROM_TARGET_P (pat->insn (i)))
954 ? eligible_for_annul_false (jump, i - 1, pat->insn (i), flags) :
955 #endif
956 #if ANNUL_IFTRUE_SLOTS
957 (INSN_ANNULLED_BRANCH_P (jump)
958 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
959 ? eligible_for_annul_true (jump, i - 1, pat->insn (i), flags) :
960 #endif
961 eligible_for_delay (jump, i - 1, pat->insn (i), flags)))
962 break;
964 return (i == pat->len ());
967 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
968 any insns we wish to place in the delay slot of JUMP. */
970 static int
971 redirect_with_delay_list_safe_p (rtx_insn *jump, rtx newlabel,
972 const vec<rtx_insn *> &delay_list)
974 /* Make sure all the insns in DELAY_LIST would still be
975 valid after threading the jump. If they are still
976 valid, then return nonzero. */
978 int flags = get_jump_flags (jump, newlabel);
979 unsigned int delay_insns = delay_list.length ();
980 unsigned int i = 0;
981 for (; i < delay_insns; i++)
982 if (! (
983 #if ANNUL_IFFALSE_SLOTS
984 (INSN_ANNULLED_BRANCH_P (jump)
985 && INSN_FROM_TARGET_P (delay_list[i]))
986 ? eligible_for_annul_false (jump, i, delay_list[i], flags) :
987 #endif
988 #if ANNUL_IFTRUE_SLOTS
989 (INSN_ANNULLED_BRANCH_P (jump)
990 && ! INSN_FROM_TARGET_P (delay_list[i]))
991 ? eligible_for_annul_true (jump, i, delay_list[i], flags) :
992 #endif
993 eligible_for_delay (jump, i, delay_list[i], flags)))
994 break;
996 return i == delay_insns;
999 /* DELAY_LIST is a list of insns that have already been placed into delay
1000 slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
1001 If not, return 0; otherwise return 1. */
1003 static int
1004 check_annul_list_true_false (int annul_true_p,
1005 const vec<rtx_insn *> &delay_list)
1007 rtx_insn *trial;
1008 unsigned int i;
1009 FOR_EACH_VEC_ELT (delay_list, i, trial)
1010 if ((annul_true_p && INSN_FROM_TARGET_P (trial))
1011 || (!annul_true_p && !INSN_FROM_TARGET_P (trial)))
1012 return 0;
1014 return 1;
1017 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
1018 the condition tested by INSN is CONDITION and the resources shown in
1019 OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1020 from SEQ's delay list, in addition to whatever insns it may execute
1021 (in DELAY_LIST). SETS and NEEDED are denote resources already set and
1022 needed while searching for delay slot insns. Return the concatenated
1023 delay list if possible, otherwise, return 0.
1025 SLOTS_TO_FILL is the total number of slots required by INSN, and
1026 PSLOTS_FILLED points to the number filled so far (also the number of
1027 insns in DELAY_LIST). It is updated with the number that have been
1028 filled from the SEQUENCE, if any.
1030 PANNUL_P points to a nonzero value if we already know that we need
1031 to annul INSN. If this routine determines that annulling is needed,
1032 it may set that value nonzero.
1034 PNEW_THREAD points to a location that is to receive the place at which
1035 execution should continue. */
1037 static void
1038 steal_delay_list_from_target (rtx_insn *insn, rtx condition, rtx_sequence *seq,
1039 vec<rtx_insn *> *delay_list,
1040 struct resources *sets,
1041 struct resources *needed,
1042 struct resources *other_needed,
1043 int slots_to_fill, int *pslots_filled,
1044 int *pannul_p, rtx *pnew_thread)
1046 int slots_remaining = slots_to_fill - *pslots_filled;
1047 int total_slots_filled = *pslots_filled;
1048 auto_vec<rtx_insn *, 5> new_delay_list;
1049 int must_annul = *pannul_p;
1050 int used_annul = 0;
1051 int i;
1052 struct resources cc_set;
1053 rtx_insn **redundant;
1055 /* We can't do anything if there are more delay slots in SEQ than we
1056 can handle, or if we don't know that it will be a taken branch.
1057 We know that it will be a taken branch if it is either an unconditional
1058 branch or a conditional branch with a stricter branch condition.
1060 Also, exit if the branch has more than one set, since then it is computing
1061 other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1062 ??? It may be possible to move other sets into INSN in addition to
1063 moving the instructions in the delay slots.
1065 We can not steal the delay list if one of the instructions in the
1066 current delay_list modifies the condition codes and the jump in the
1067 sequence is a conditional jump. We can not do this because we can
1068 not change the direction of the jump because the condition codes
1069 will effect the direction of the jump in the sequence. */
1071 CLEAR_RESOURCE (&cc_set);
1073 rtx_insn *trial;
1074 FOR_EACH_VEC_ELT (*delay_list, i, trial)
1076 mark_set_resources (trial, &cc_set, 0, MARK_SRC_DEST_CALL);
1077 if (insn_references_resource_p (seq->insn (0), &cc_set, false))
1078 return;
1081 if (XVECLEN (seq, 0) - 1 > slots_remaining
1082 || ! condition_dominates_p (condition, seq->insn (0))
1083 || ! single_set (seq->insn (0)))
1084 return;
1086 /* On some targets, branches with delay slots can have a limited
1087 displacement. Give the back end a chance to tell us we can't do
1088 this. */
1089 if (! targetm.can_follow_jump (insn, seq->insn (0)))
1090 return;
1092 redundant = XALLOCAVEC (rtx_insn *, XVECLEN (seq, 0));
1093 for (i = 1; i < seq->len (); i++)
1095 rtx_insn *trial = seq->insn (i);
1096 int flags;
1098 if (insn_references_resource_p (trial, sets, false)
1099 || insn_sets_resource_p (trial, needed, false)
1100 || insn_sets_resource_p (trial, sets, false)
1101 /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1102 delay list. */
1103 || (HAVE_cc0 && find_reg_note (trial, REG_CC_USER, NULL_RTX))
1104 /* If TRIAL is from the fallthrough code of an annulled branch insn
1105 in SEQ, we cannot use it. */
1106 || (INSN_ANNULLED_BRANCH_P (seq->insn (0))
1107 && ! INSN_FROM_TARGET_P (trial)))
1108 return;
1110 /* If this insn was already done (usually in a previous delay slot),
1111 pretend we put it in our delay slot. */
1112 redundant[i] = redundant_insn (trial, insn, new_delay_list);
1113 if (redundant[i])
1114 continue;
1116 /* We will end up re-vectoring this branch, so compute flags
1117 based on jumping to the new label. */
1118 flags = get_jump_flags (insn, JUMP_LABEL (seq->insn (0)));
1120 if (! must_annul
1121 && ((condition == const_true_rtx
1122 || (! insn_sets_resource_p (trial, other_needed, false)
1123 && ! may_trap_or_fault_p (PATTERN (trial)))))
1124 ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1125 : (must_annul || (delay_list->is_empty () && new_delay_list.is_empty ()))
1126 && (must_annul = 1,
1127 check_annul_list_true_false (0, *delay_list)
1128 && check_annul_list_true_false (0, new_delay_list)
1129 && eligible_for_annul_false (insn, total_slots_filled,
1130 trial, flags)))
1132 if (must_annul)
1134 /* Frame related instructions cannot go into annulled delay
1135 slots, it messes up the dwarf info. */
1136 if (RTX_FRAME_RELATED_P (trial))
1137 return;
1138 used_annul = 1;
1140 rtx_insn *temp = copy_delay_slot_insn (trial);
1141 INSN_FROM_TARGET_P (temp) = 1;
1142 add_to_delay_list (temp, &new_delay_list);
1143 total_slots_filled++;
1145 if (--slots_remaining == 0)
1146 break;
1148 else
1149 return;
1152 /* Record the effect of the instructions that were redundant and which
1153 we therefore decided not to copy. */
1154 for (i = 1; i < seq->len (); i++)
1155 if (redundant[i])
1157 fix_reg_dead_note (redundant[i], insn);
1158 update_block (seq->insn (i), insn);
1161 /* Show the place to which we will be branching. */
1162 *pnew_thread = first_active_target_insn (JUMP_LABEL (seq->insn (0)));
1164 /* Add any new insns to the delay list and update the count of the
1165 number of slots filled. */
1166 *pslots_filled = total_slots_filled;
1167 if (used_annul)
1168 *pannul_p = 1;
1170 rtx_insn *temp;
1171 FOR_EACH_VEC_ELT (new_delay_list, i, temp)
1172 add_to_delay_list (temp, delay_list);
1175 /* Similar to steal_delay_list_from_target except that SEQ is on the
1176 fallthrough path of INSN. Here we only do something if the delay insn
1177 of SEQ is an unconditional branch. In that case we steal its delay slot
1178 for INSN since unconditional branches are much easier to fill. */
1180 static void
1181 steal_delay_list_from_fallthrough (rtx_insn *insn, rtx condition,
1182 rtx_sequence *seq,
1183 vec<rtx_insn *> *delay_list,
1184 struct resources *sets,
1185 struct resources *needed,
1186 struct resources *other_needed,
1187 int slots_to_fill, int *pslots_filled,
1188 int *pannul_p)
1190 int i;
1191 int flags;
1192 int must_annul = *pannul_p;
1193 int used_annul = 0;
1195 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1197 /* We can't do anything if SEQ's delay insn isn't an
1198 unconditional branch. */
1200 if (! simplejump_or_return_p (seq->insn (0)))
1201 return;
1203 for (i = 1; i < seq->len (); i++)
1205 rtx_insn *trial = seq->insn (i);
1206 rtx_insn *prior_insn;
1208 /* If TRIAL sets CC0, stealing it will move it too far from the use
1209 of CC0. */
1210 if (insn_references_resource_p (trial, sets, false)
1211 || insn_sets_resource_p (trial, needed, false)
1212 || insn_sets_resource_p (trial, sets, false)
1213 || (HAVE_cc0 && sets_cc0_p (PATTERN (trial))))
1215 break;
1217 /* If this insn was already done, we don't need it. */
1218 if ((prior_insn = redundant_insn (trial, insn, *delay_list)))
1220 fix_reg_dead_note (prior_insn, insn);
1221 update_block (trial, insn);
1222 delete_from_delay_slot (trial);
1223 continue;
1226 if (! must_annul
1227 && ((condition == const_true_rtx
1228 || (! insn_sets_resource_p (trial, other_needed, false)
1229 && ! may_trap_or_fault_p (PATTERN (trial)))))
1230 ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1231 : (must_annul || delay_list->is_empty ()) && (must_annul = 1,
1232 check_annul_list_true_false (1, *delay_list)
1233 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1235 if (must_annul)
1236 used_annul = 1;
1237 delete_from_delay_slot (trial);
1238 add_to_delay_list (trial, delay_list);
1240 if (++(*pslots_filled) == slots_to_fill)
1241 break;
1243 else
1244 break;
1247 if (used_annul)
1248 *pannul_p = 1;
1251 /* Try merging insns starting at THREAD which match exactly the insns in
1252 INSN's delay list.
1254 If all insns were matched and the insn was previously annulling, the
1255 annul bit will be cleared.
1257 For each insn that is merged, if the branch is or will be non-annulling,
1258 we delete the merged insn. */
1260 static void
1261 try_merge_delay_insns (rtx_insn *insn, rtx_insn *thread)
1263 rtx_insn *trial, *next_trial;
1264 rtx_insn *delay_insn = as_a <rtx_insn *> (XVECEXP (PATTERN (insn), 0, 0));
1265 int annul_p = JUMP_P (delay_insn) && INSN_ANNULLED_BRANCH_P (delay_insn);
1266 int slot_number = 1;
1267 int num_slots = XVECLEN (PATTERN (insn), 0);
1268 rtx next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1269 struct resources set, needed, modified;
1270 auto_vec<std::pair<rtx_insn *, bool>, 10> merged_insns;
1271 int flags;
1273 flags = get_jump_flags (delay_insn, JUMP_LABEL (delay_insn));
1275 CLEAR_RESOURCE (&needed);
1276 CLEAR_RESOURCE (&set);
1278 /* If this is not an annulling branch, take into account anything needed in
1279 INSN's delay slot. This prevents two increments from being incorrectly
1280 folded into one. If we are annulling, this would be the correct
1281 thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1282 will essentially disable this optimization. This method is somewhat of
1283 a kludge, but I don't see a better way.) */
1284 if (! annul_p)
1285 for (int i = 1; i < num_slots; i++)
1286 if (XVECEXP (PATTERN (insn), 0, i))
1287 mark_referenced_resources (XVECEXP (PATTERN (insn), 0, i), &needed,
1288 true);
1290 for (trial = thread; !stop_search_p (trial, 1); trial = next_trial)
1292 rtx pat = PATTERN (trial);
1293 rtx oldtrial = trial;
1295 next_trial = next_nonnote_insn (trial);
1297 /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1298 if (NONJUMP_INSN_P (trial)
1299 && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1300 continue;
1302 if (GET_CODE (next_to_match) == GET_CODE (trial)
1303 /* We can't share an insn that sets cc0. */
1304 && (!HAVE_cc0 || ! sets_cc0_p (pat))
1305 && ! insn_references_resource_p (trial, &set, true)
1306 && ! insn_sets_resource_p (trial, &set, true)
1307 && ! insn_sets_resource_p (trial, &needed, true)
1308 && (trial = try_split (pat, trial, 0)) != 0
1309 /* Update next_trial, in case try_split succeeded. */
1310 && (next_trial = next_nonnote_insn (trial))
1311 /* Likewise THREAD. */
1312 && (thread = oldtrial == thread ? trial : thread)
1313 && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1314 /* Have to test this condition if annul condition is different
1315 from (and less restrictive than) non-annulling one. */
1316 && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1319 if (! annul_p)
1321 update_block (trial, thread);
1322 if (trial == thread)
1323 thread = next_active_insn (thread);
1325 delete_related_insns (trial);
1326 INSN_FROM_TARGET_P (next_to_match) = 0;
1328 else
1329 merged_insns.safe_push (std::pair<rtx_insn *, bool> (trial, false));
1331 if (++slot_number == num_slots)
1332 break;
1334 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1337 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1338 mark_referenced_resources (trial, &needed, true);
1341 /* See if we stopped on a filled insn. If we did, try to see if its
1342 delay slots match. */
1343 if (slot_number != num_slots
1344 && trial && NONJUMP_INSN_P (trial)
1345 && GET_CODE (PATTERN (trial)) == SEQUENCE
1346 && !(JUMP_P (XVECEXP (PATTERN (trial), 0, 0))
1347 && INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial), 0, 0))))
1349 rtx_sequence *pat = as_a <rtx_sequence *> (PATTERN (trial));
1350 rtx filled_insn = XVECEXP (pat, 0, 0);
1352 /* Account for resources set/needed by the filled insn. */
1353 mark_set_resources (filled_insn, &set, 0, MARK_SRC_DEST_CALL);
1354 mark_referenced_resources (filled_insn, &needed, true);
1356 for (int i = 1; i < pat->len (); i++)
1358 rtx_insn *dtrial = pat->insn (i);
1360 CLEAR_RESOURCE (&modified);
1361 /* Account for resources set by the insn following NEXT_TO_MATCH
1362 inside INSN's delay list. */
1363 for (int j = 1; slot_number + j < num_slots; j++)
1364 mark_set_resources (XVECEXP (PATTERN (insn), 0, slot_number + j),
1365 &modified, 0, MARK_SRC_DEST_CALL);
1366 /* Account for resources set by the insn before DTRIAL and inside
1367 TRIAL's delay list. */
1368 for (int j = 1; j < i; j++)
1369 mark_set_resources (XVECEXP (pat, 0, j),
1370 &modified, 0, MARK_SRC_DEST_CALL);
1371 if (! insn_references_resource_p (dtrial, &set, true)
1372 && ! insn_sets_resource_p (dtrial, &set, true)
1373 && ! insn_sets_resource_p (dtrial, &needed, true)
1374 && (!HAVE_cc0 || ! sets_cc0_p (PATTERN (dtrial)))
1375 && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1376 /* Check that DTRIAL and NEXT_TO_MATCH does not reference a
1377 resource modified between them (only dtrial is checked because
1378 next_to_match and dtrial shall to be equal in order to hit
1379 this line) */
1380 && ! insn_references_resource_p (dtrial, &modified, true)
1381 && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1383 if (! annul_p)
1385 rtx_insn *new_rtx;
1387 update_block (dtrial, thread);
1388 new_rtx = delete_from_delay_slot (dtrial);
1389 if (thread->deleted ())
1390 thread = new_rtx;
1391 INSN_FROM_TARGET_P (next_to_match) = 0;
1393 else
1394 merged_insns.safe_push (std::pair<rtx_insn *, bool> (dtrial,
1395 true));
1397 if (++slot_number == num_slots)
1398 break;
1400 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1402 else
1404 /* Keep track of the set/referenced resources for the delay
1405 slots of any trial insns we encounter. */
1406 mark_set_resources (dtrial, &set, 0, MARK_SRC_DEST_CALL);
1407 mark_referenced_resources (dtrial, &needed, true);
1412 /* If all insns in the delay slot have been matched and we were previously
1413 annulling the branch, we need not any more. In that case delete all the
1414 merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1415 the delay list so that we know that it isn't only being used at the
1416 target. */
1417 if (slot_number == num_slots && annul_p)
1419 unsigned int len = merged_insns.length ();
1420 for (unsigned int i = len - 1; i < len; i--)
1421 if (merged_insns[i].second)
1423 update_block (merged_insns[i].first, thread);
1424 rtx_insn *new_rtx = delete_from_delay_slot (merged_insns[i].first);
1425 if (thread->deleted ())
1426 thread = new_rtx;
1428 else
1430 update_block (merged_insns[i].first, thread);
1431 delete_related_insns (merged_insns[i].first);
1434 INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1436 for (int i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1437 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i)) = 0;
1441 /* See if INSN is redundant with an insn in front of TARGET. Often this
1442 is called when INSN is a candidate for a delay slot of TARGET.
1443 DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1444 of INSN. Often INSN will be redundant with an insn in a delay slot of
1445 some previous insn. This happens when we have a series of branches to the
1446 same label; in that case the first insn at the target might want to go
1447 into each of the delay slots.
1449 If we are not careful, this routine can take up a significant fraction
1450 of the total compilation time (4%), but only wins rarely. Hence we
1451 speed this routine up by making two passes. The first pass goes back
1452 until it hits a label and sees if it finds an insn with an identical
1453 pattern. Only in this (relatively rare) event does it check for
1454 data conflicts.
1456 We do not split insns we encounter. This could cause us not to find a
1457 redundant insn, but the cost of splitting seems greater than the possible
1458 gain in rare cases. */
1460 static rtx_insn *
1461 redundant_insn (rtx insn, rtx_insn *target, const vec<rtx_insn *> &delay_list)
1463 rtx target_main = target;
1464 rtx ipat = PATTERN (insn);
1465 rtx_insn *trial;
1466 rtx pat;
1467 struct resources needed, set;
1468 int i;
1469 unsigned insns_to_search;
1471 /* If INSN has any REG_UNUSED notes, it can't match anything since we
1472 are allowed to not actually assign to such a register. */
1473 if (find_reg_note (insn, REG_UNUSED, NULL_RTX) != 0)
1474 return 0;
1476 /* Scan backwards looking for a match. */
1477 for (trial = PREV_INSN (target),
1478 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1479 trial && insns_to_search > 0;
1480 trial = PREV_INSN (trial))
1482 /* (use (insn))s can come immediately after a barrier if the
1483 label that used to precede them has been deleted as dead.
1484 See delete_related_insns. */
1485 if (LABEL_P (trial) || BARRIER_P (trial))
1486 return 0;
1488 if (!INSN_P (trial))
1489 continue;
1490 --insns_to_search;
1492 pat = PATTERN (trial);
1493 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1494 continue;
1496 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (pat))
1498 /* Stop for a CALL and its delay slots because it is difficult to
1499 track its resource needs correctly. */
1500 if (CALL_P (seq->element (0)))
1501 return 0;
1503 /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1504 slots because it is difficult to track its resource needs
1505 correctly. */
1507 if (INSN_SETS_ARE_DELAYED (seq->insn (0)))
1508 return 0;
1510 if (INSN_REFERENCES_ARE_DELAYED (seq->insn (0)))
1511 return 0;
1513 /* See if any of the insns in the delay slot match, updating
1514 resource requirements as we go. */
1515 for (i = seq->len () - 1; i > 0; i--)
1516 if (GET_CODE (seq->element (i)) == GET_CODE (insn)
1517 && rtx_equal_p (PATTERN (seq->element (i)), ipat)
1518 && ! find_reg_note (seq->element (i), REG_UNUSED, NULL_RTX))
1519 break;
1521 /* If found a match, exit this loop early. */
1522 if (i > 0)
1523 break;
1526 else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat)
1527 && ! find_reg_note (trial, REG_UNUSED, NULL_RTX))
1528 break;
1531 /* If we didn't find an insn that matches, return 0. */
1532 if (trial == 0)
1533 return 0;
1535 /* See what resources this insn sets and needs. If they overlap, or
1536 if this insn references CC0, it can't be redundant. */
1538 CLEAR_RESOURCE (&needed);
1539 CLEAR_RESOURCE (&set);
1540 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1541 mark_referenced_resources (insn, &needed, true);
1543 /* If TARGET is a SEQUENCE, get the main insn. */
1544 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1545 target_main = XVECEXP (PATTERN (target), 0, 0);
1547 if (resource_conflicts_p (&needed, &set)
1548 || (HAVE_cc0 && reg_mentioned_p (cc0_rtx, ipat))
1549 /* The insn requiring the delay may not set anything needed or set by
1550 INSN. */
1551 || insn_sets_resource_p (target_main, &needed, true)
1552 || insn_sets_resource_p (target_main, &set, true))
1553 return 0;
1555 /* Insns we pass may not set either NEEDED or SET, so merge them for
1556 simpler tests. */
1557 needed.memory |= set.memory;
1558 IOR_HARD_REG_SET (needed.regs, set.regs);
1560 /* This insn isn't redundant if it conflicts with an insn that either is
1561 or will be in a delay slot of TARGET. */
1563 unsigned int j;
1564 rtx_insn *temp;
1565 FOR_EACH_VEC_ELT (delay_list, j, temp)
1566 if (insn_sets_resource_p (temp, &needed, true))
1567 return 0;
1569 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1570 for (i = 1; i < XVECLEN (PATTERN (target), 0); i++)
1571 if (insn_sets_resource_p (XVECEXP (PATTERN (target), 0, i), &needed,
1572 true))
1573 return 0;
1575 /* Scan backwards until we reach a label or an insn that uses something
1576 INSN sets or sets something insn uses or sets. */
1578 for (trial = PREV_INSN (target),
1579 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1580 trial && !LABEL_P (trial) && insns_to_search > 0;
1581 trial = PREV_INSN (trial))
1583 if (!INSN_P (trial))
1584 continue;
1585 --insns_to_search;
1587 pat = PATTERN (trial);
1588 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1589 continue;
1591 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (pat))
1593 bool annul_p = false;
1594 rtx_insn *control = seq->insn (0);
1596 /* If this is a CALL_INSN and its delay slots, it is hard to track
1597 the resource needs properly, so give up. */
1598 if (CALL_P (control))
1599 return 0;
1601 /* If this is an INSN or JUMP_INSN with delayed effects, it
1602 is hard to track the resource needs properly, so give up. */
1604 if (INSN_SETS_ARE_DELAYED (control))
1605 return 0;
1607 if (INSN_REFERENCES_ARE_DELAYED (control))
1608 return 0;
1610 if (JUMP_P (control))
1611 annul_p = INSN_ANNULLED_BRANCH_P (control);
1613 /* See if any of the insns in the delay slot match, updating
1614 resource requirements as we go. */
1615 for (i = seq->len () - 1; i > 0; i--)
1617 rtx_insn *candidate = seq->insn (i);
1619 /* If an insn will be annulled if the branch is false, it isn't
1620 considered as a possible duplicate insn. */
1621 if (rtx_equal_p (PATTERN (candidate), ipat)
1622 && ! (annul_p && INSN_FROM_TARGET_P (candidate)))
1624 /* Show that this insn will be used in the sequel. */
1625 INSN_FROM_TARGET_P (candidate) = 0;
1626 return candidate;
1629 /* Unless this is an annulled insn from the target of a branch,
1630 we must stop if it sets anything needed or set by INSN. */
1631 if ((!annul_p || !INSN_FROM_TARGET_P (candidate))
1632 && insn_sets_resource_p (candidate, &needed, true))
1633 return 0;
1636 /* If the insn requiring the delay slot conflicts with INSN, we
1637 must stop. */
1638 if (insn_sets_resource_p (control, &needed, true))
1639 return 0;
1641 else
1643 /* See if TRIAL is the same as INSN. */
1644 pat = PATTERN (trial);
1645 if (rtx_equal_p (pat, ipat))
1646 return trial;
1648 /* Can't go any further if TRIAL conflicts with INSN. */
1649 if (insn_sets_resource_p (trial, &needed, true))
1650 return 0;
1654 return 0;
1657 /* Return 1 if THREAD can only be executed in one way. If LABEL is nonzero,
1658 it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1659 is nonzero, we are allowed to fall into this thread; otherwise, we are
1660 not.
1662 If LABEL is used more than one or we pass a label other than LABEL before
1663 finding an active insn, we do not own this thread. */
1665 static int
1666 own_thread_p (rtx thread, rtx label, int allow_fallthrough)
1668 rtx_insn *active_insn;
1669 rtx_insn *insn;
1671 /* We don't own the function end. */
1672 if (thread == 0 || ANY_RETURN_P (thread))
1673 return 0;
1675 /* We have a non-NULL insn. */
1676 rtx_insn *thread_insn = as_a <rtx_insn *> (thread);
1678 /* Get the first active insn, or THREAD_INSN, if it is an active insn. */
1679 active_insn = next_active_insn (PREV_INSN (thread_insn));
1681 for (insn = thread_insn; insn != active_insn; insn = NEXT_INSN (insn))
1682 if (LABEL_P (insn)
1683 && (insn != label || LABEL_NUSES (insn) != 1))
1684 return 0;
1686 if (allow_fallthrough)
1687 return 1;
1689 /* Ensure that we reach a BARRIER before any insn or label. */
1690 for (insn = prev_nonnote_insn (thread_insn);
1691 insn == 0 || !BARRIER_P (insn);
1692 insn = prev_nonnote_insn (insn))
1693 if (insn == 0
1694 || LABEL_P (insn)
1695 || (NONJUMP_INSN_P (insn)
1696 && GET_CODE (PATTERN (insn)) != USE
1697 && GET_CODE (PATTERN (insn)) != CLOBBER))
1698 return 0;
1700 return 1;
1703 /* Called when INSN is being moved from a location near the target of a jump.
1704 We leave a marker of the form (use (INSN)) immediately in front of WHERE
1705 for mark_target_live_regs. These markers will be deleted at the end.
1707 We used to try to update the live status of registers if WHERE is at
1708 the start of a basic block, but that can't work since we may remove a
1709 BARRIER in relax_delay_slots. */
1711 static void
1712 update_block (rtx_insn *insn, rtx_insn *where)
1714 emit_insn_before (gen_rtx_USE (VOIDmode, insn), where);
1716 /* INSN might be making a value live in a block where it didn't use to
1717 be. So recompute liveness information for this block. */
1718 incr_ticks_for_insn (insn);
1721 /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1722 the basic block containing the jump. */
1724 static int
1725 reorg_redirect_jump (rtx_jump_insn *jump, rtx nlabel)
1727 incr_ticks_for_insn (jump);
1728 return redirect_jump (jump, nlabel, 1);
1731 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1732 We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1733 that reference values used in INSN. If we find one, then we move the
1734 REG_DEAD note to INSN.
1736 This is needed to handle the case where a later insn (after INSN) has a
1737 REG_DEAD note for a register used by INSN, and this later insn subsequently
1738 gets moved before a CODE_LABEL because it is a redundant insn. In this
1739 case, mark_target_live_regs may be confused into thinking the register
1740 is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1742 static void
1743 update_reg_dead_notes (rtx_insn *insn, rtx_insn *delayed_insn)
1745 rtx link, next;
1746 rtx_insn *p;
1748 for (p = next_nonnote_insn (insn); p != delayed_insn;
1749 p = next_nonnote_insn (p))
1750 for (link = REG_NOTES (p); link; link = next)
1752 next = XEXP (link, 1);
1754 if (REG_NOTE_KIND (link) != REG_DEAD
1755 || !REG_P (XEXP (link, 0)))
1756 continue;
1758 if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
1760 /* Move the REG_DEAD note from P to INSN. */
1761 remove_note (p, link);
1762 XEXP (link, 1) = REG_NOTES (insn);
1763 REG_NOTES (insn) = link;
1768 /* Called when an insn redundant with start_insn is deleted. If there
1769 is a REG_DEAD note for the target of start_insn between start_insn
1770 and stop_insn, then the REG_DEAD note needs to be deleted since the
1771 value no longer dies there.
1773 If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1774 confused into thinking the register is dead. */
1776 static void
1777 fix_reg_dead_note (rtx_insn *start_insn, rtx stop_insn)
1779 rtx link, next;
1780 rtx_insn *p;
1782 for (p = next_nonnote_insn (start_insn); p != stop_insn;
1783 p = next_nonnote_insn (p))
1784 for (link = REG_NOTES (p); link; link = next)
1786 next = XEXP (link, 1);
1788 if (REG_NOTE_KIND (link) != REG_DEAD
1789 || !REG_P (XEXP (link, 0)))
1790 continue;
1792 if (reg_set_p (XEXP (link, 0), PATTERN (start_insn)))
1794 remove_note (p, link);
1795 return;
1800 /* Delete any REG_UNUSED notes that exist on INSN but not on OTHER_INSN.
1802 This handles the case of udivmodXi4 instructions which optimize their
1803 output depending on whether any REG_UNUSED notes are present. We must
1804 make sure that INSN calculates as many results as OTHER_INSN does. */
1806 static void
1807 update_reg_unused_notes (rtx_insn *insn, rtx other_insn)
1809 rtx link, next;
1811 for (link = REG_NOTES (insn); link; link = next)
1813 next = XEXP (link, 1);
1815 if (REG_NOTE_KIND (link) != REG_UNUSED
1816 || !REG_P (XEXP (link, 0)))
1817 continue;
1819 if (!find_regno_note (other_insn, REG_UNUSED, REGNO (XEXP (link, 0))))
1820 remove_note (insn, link);
1824 static vec <rtx> sibling_labels;
1826 /* Return the label before INSN, or put a new label there. If SIBLING is
1827 non-zero, it is another label associated with the new label (if any),
1828 typically the former target of the jump that will be redirected to
1829 the new label. */
1831 static rtx_insn *
1832 get_label_before (rtx_insn *insn, rtx sibling)
1834 rtx_insn *label;
1836 /* Find an existing label at this point
1837 or make a new one if there is none. */
1838 label = prev_nonnote_insn (insn);
1840 if (label == 0 || !LABEL_P (label))
1842 rtx_insn *prev = PREV_INSN (insn);
1844 label = gen_label_rtx ();
1845 emit_label_after (label, prev);
1846 LABEL_NUSES (label) = 0;
1847 if (sibling)
1849 sibling_labels.safe_push (label);
1850 sibling_labels.safe_push (sibling);
1853 return label;
1856 /* Scan a function looking for insns that need a delay slot and find insns to
1857 put into the delay slot.
1859 NON_JUMPS_P is nonzero if we are to only try to fill non-jump insns (such
1860 as calls). We do these first since we don't want jump insns (that are
1861 easier to fill) to get the only insns that could be used for non-jump insns.
1862 When it is zero, only try to fill JUMP_INSNs.
1864 When slots are filled in this manner, the insns (including the
1865 delay_insn) are put together in a SEQUENCE rtx. In this fashion,
1866 it is possible to tell whether a delay slot has really been filled
1867 or not. `final' knows how to deal with this, by communicating
1868 through FINAL_SEQUENCE. */
1870 static void
1871 fill_simple_delay_slots (int non_jumps_p)
1873 rtx_insn *insn, *trial, *next_trial;
1874 rtx pat;
1875 int i;
1876 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
1877 struct resources needed, set;
1878 int slots_to_fill, slots_filled;
1879 auto_vec<rtx_insn *, 5> delay_list;
1881 for (i = 0; i < num_unfilled_slots; i++)
1883 int flags;
1884 /* Get the next insn to fill. If it has already had any slots assigned,
1885 we can't do anything with it. Maybe we'll improve this later. */
1887 insn = unfilled_slots_base[i];
1888 if (insn == 0
1889 || insn->deleted ()
1890 || (NONJUMP_INSN_P (insn)
1891 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1892 || (JUMP_P (insn) && non_jumps_p)
1893 || (!JUMP_P (insn) && ! non_jumps_p))
1894 continue;
1896 /* It may have been that this insn used to need delay slots, but
1897 now doesn't; ignore in that case. This can happen, for example,
1898 on the HP PA RISC, where the number of delay slots depends on
1899 what insns are nearby. */
1900 slots_to_fill = num_delay_slots (insn);
1902 /* Some machine description have defined instructions to have
1903 delay slots only in certain circumstances which may depend on
1904 nearby insns (which change due to reorg's actions).
1906 For example, the PA port normally has delay slots for unconditional
1907 jumps.
1909 However, the PA port claims such jumps do not have a delay slot
1910 if they are immediate successors of certain CALL_INSNs. This
1911 allows the port to favor filling the delay slot of the call with
1912 the unconditional jump. */
1913 if (slots_to_fill == 0)
1914 continue;
1916 /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
1917 says how many. After initialization, first try optimizing
1919 call _foo call _foo
1920 nop add %o7,.-L1,%o7
1921 b,a L1
1924 If this case applies, the delay slot of the call is filled with
1925 the unconditional jump. This is done first to avoid having the
1926 delay slot of the call filled in the backward scan. Also, since
1927 the unconditional jump is likely to also have a delay slot, that
1928 insn must exist when it is subsequently scanned.
1930 This is tried on each insn with delay slots as some machines
1931 have insns which perform calls, but are not represented as
1932 CALL_INSNs. */
1934 slots_filled = 0;
1935 delay_list.truncate (0);
1937 if (JUMP_P (insn))
1938 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1939 else
1940 flags = get_jump_flags (insn, NULL_RTX);
1942 if ((trial = next_active_insn (insn))
1943 && JUMP_P (trial)
1944 && simplejump_p (trial)
1945 && eligible_for_delay (insn, slots_filled, trial, flags)
1946 && no_labels_between_p (insn, trial)
1947 && ! can_throw_internal (trial))
1949 rtx_insn **tmp;
1950 slots_filled++;
1951 add_to_delay_list (trial, &delay_list);
1953 /* TRIAL may have had its delay slot filled, then unfilled. When
1954 the delay slot is unfilled, TRIAL is placed back on the unfilled
1955 slots obstack. Unfortunately, it is placed on the end of the
1956 obstack, not in its original location. Therefore, we must search
1957 from entry i + 1 to the end of the unfilled slots obstack to
1958 try and find TRIAL. */
1959 tmp = &unfilled_slots_base[i + 1];
1960 while (*tmp != trial && tmp != unfilled_slots_next)
1961 tmp++;
1963 /* Remove the unconditional jump from consideration for delay slot
1964 filling and unthread it. */
1965 if (*tmp == trial)
1966 *tmp = 0;
1968 rtx_insn *next = NEXT_INSN (trial);
1969 rtx_insn *prev = PREV_INSN (trial);
1970 if (prev)
1971 SET_NEXT_INSN (prev) = next;
1972 if (next)
1973 SET_PREV_INSN (next) = prev;
1977 /* Now, scan backwards from the insn to search for a potential
1978 delay-slot candidate. Stop searching when a label or jump is hit.
1980 For each candidate, if it is to go into the delay slot (moved
1981 forward in execution sequence), it must not need or set any resources
1982 that were set by later insns and must not set any resources that
1983 are needed for those insns.
1985 The delay slot insn itself sets resources unless it is a call
1986 (in which case the called routine, not the insn itself, is doing
1987 the setting). */
1989 if (slots_filled < slots_to_fill)
1991 /* If the flags register is dead after the insn, then we want to be
1992 able to accept a candidate that clobbers it. For this purpose,
1993 we need to filter the flags register during life analysis, so
1994 that it doesn't create RAW and WAW dependencies, while still
1995 creating the necessary WAR dependencies. */
1996 bool filter_flags
1997 = (slots_to_fill == 1
1998 && targetm.flags_regnum != INVALID_REGNUM
1999 && find_regno_note (insn, REG_DEAD, targetm.flags_regnum));
2000 struct resources fset;
2001 CLEAR_RESOURCE (&needed);
2002 CLEAR_RESOURCE (&set);
2003 mark_set_resources (insn, &set, 0, MARK_SRC_DEST);
2004 if (filter_flags)
2006 CLEAR_RESOURCE (&fset);
2007 mark_set_resources (insn, &fset, 0, MARK_SRC_DEST);
2009 mark_referenced_resources (insn, &needed, false);
2011 for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, 1);
2012 trial = next_trial)
2014 next_trial = prev_nonnote_insn (trial);
2016 /* This must be an INSN or CALL_INSN. */
2017 pat = PATTERN (trial);
2019 /* Stand-alone USE and CLOBBER are just for flow. */
2020 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2021 continue;
2023 /* Check for resource conflict first, to avoid unnecessary
2024 splitting. */
2025 if (! insn_references_resource_p (trial, &set, true)
2026 && ! insn_sets_resource_p (trial,
2027 filter_flags ? &fset : &set,
2028 true)
2029 && ! insn_sets_resource_p (trial, &needed, true)
2030 /* Can't separate set of cc0 from its use. */
2031 && (!HAVE_cc0 || ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat)))
2032 && ! can_throw_internal (trial))
2034 trial = try_split (pat, trial, 1);
2035 next_trial = prev_nonnote_insn (trial);
2036 if (eligible_for_delay (insn, slots_filled, trial, flags))
2038 /* In this case, we are searching backward, so if we
2039 find insns to put on the delay list, we want
2040 to put them at the head, rather than the
2041 tail, of the list. */
2043 update_reg_dead_notes (trial, insn);
2044 delay_list.safe_insert (0, trial);
2045 update_block (trial, trial);
2046 delete_related_insns (trial);
2047 if (slots_to_fill == ++slots_filled)
2048 break;
2049 continue;
2053 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2054 if (filter_flags)
2056 mark_set_resources (trial, &fset, 0, MARK_SRC_DEST_CALL);
2057 /* If the flags register is set, then it doesn't create RAW
2058 dependencies any longer and it also doesn't create WAW
2059 dependencies since it's dead after the original insn. */
2060 if (TEST_HARD_REG_BIT (fset.regs, targetm.flags_regnum))
2062 CLEAR_HARD_REG_BIT (needed.regs, targetm.flags_regnum);
2063 CLEAR_HARD_REG_BIT (fset.regs, targetm.flags_regnum);
2066 mark_referenced_resources (trial, &needed, true);
2070 /* If all needed slots haven't been filled, we come here. */
2072 /* Try to optimize case of jumping around a single insn. */
2073 if ((ANNUL_IFTRUE_SLOTS || ANNUL_IFFALSE_SLOTS)
2074 && slots_filled != slots_to_fill
2075 && delay_list.is_empty ()
2076 && JUMP_P (insn)
2077 && (condjump_p (insn) || condjump_in_parallel_p (insn))
2078 && !ANY_RETURN_P (JUMP_LABEL (insn)))
2080 optimize_skip (as_a <rtx_jump_insn *> (insn), &delay_list);
2081 if (!delay_list.is_empty ())
2082 slots_filled += 1;
2085 /* Try to get insns from beyond the insn needing the delay slot.
2086 These insns can neither set or reference resources set in insns being
2087 skipped, cannot set resources in the insn being skipped, and, if this
2088 is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2089 call might not return).
2091 There used to be code which continued past the target label if
2092 we saw all uses of the target label. This code did not work,
2093 because it failed to account for some instructions which were
2094 both annulled and marked as from the target. This can happen as a
2095 result of optimize_skip. Since this code was redundant with
2096 fill_eager_delay_slots anyways, it was just deleted. */
2098 if (slots_filled != slots_to_fill
2099 /* If this instruction could throw an exception which is
2100 caught in the same function, then it's not safe to fill
2101 the delay slot with an instruction from beyond this
2102 point. For example, consider:
2104 int i = 2;
2106 try {
2107 f();
2108 i = 3;
2109 } catch (...) {}
2111 return i;
2113 Even though `i' is a local variable, we must be sure not
2114 to put `i = 3' in the delay slot if `f' might throw an
2115 exception.
2117 Presumably, we should also check to see if we could get
2118 back to this function via `setjmp'. */
2119 && ! can_throw_internal (insn)
2120 && !JUMP_P (insn))
2122 int maybe_never = 0;
2123 rtx pat, trial_delay;
2125 CLEAR_RESOURCE (&needed);
2126 CLEAR_RESOURCE (&set);
2127 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2128 mark_referenced_resources (insn, &needed, true);
2130 if (CALL_P (insn))
2131 maybe_never = 1;
2133 for (trial = next_nonnote_insn (insn); !stop_search_p (trial, 1);
2134 trial = next_trial)
2136 next_trial = next_nonnote_insn (trial);
2138 /* This must be an INSN or CALL_INSN. */
2139 pat = PATTERN (trial);
2141 /* Stand-alone USE and CLOBBER are just for flow. */
2142 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2143 continue;
2145 /* If this already has filled delay slots, get the insn needing
2146 the delay slots. */
2147 if (GET_CODE (pat) == SEQUENCE)
2148 trial_delay = XVECEXP (pat, 0, 0);
2149 else
2150 trial_delay = trial;
2152 /* Stop our search when seeing a jump. */
2153 if (JUMP_P (trial_delay))
2154 break;
2156 /* See if we have a resource problem before we try to split. */
2157 if (GET_CODE (pat) != SEQUENCE
2158 && ! insn_references_resource_p (trial, &set, true)
2159 && ! insn_sets_resource_p (trial, &set, true)
2160 && ! insn_sets_resource_p (trial, &needed, true)
2161 && (!HAVE_cc0 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat)))
2162 && ! (maybe_never && may_trap_or_fault_p (pat))
2163 && (trial = try_split (pat, trial, 0))
2164 && eligible_for_delay (insn, slots_filled, trial, flags)
2165 && ! can_throw_internal (trial))
2167 next_trial = next_nonnote_insn (trial);
2168 add_to_delay_list (trial, &delay_list);
2169 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, pat))
2170 link_cc0_insns (trial);
2172 delete_related_insns (trial);
2173 if (slots_to_fill == ++slots_filled)
2174 break;
2175 continue;
2178 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2179 mark_referenced_resources (trial, &needed, true);
2181 /* Ensure we don't put insns between the setting of cc and the
2182 comparison by moving a setting of cc into an earlier delay
2183 slot since these insns could clobber the condition code. */
2184 set.cc = 1;
2186 /* If this is a call, we might not get here. */
2187 if (CALL_P (trial_delay))
2188 maybe_never = 1;
2191 /* If there are slots left to fill and our search was stopped by an
2192 unconditional branch, try the insn at the branch target. We can
2193 redirect the branch if it works.
2195 Don't do this if the insn at the branch target is a branch. */
2196 if (slots_to_fill != slots_filled
2197 && trial
2198 && jump_to_label_p (trial)
2199 && simplejump_p (trial)
2200 && (next_trial = next_active_insn (JUMP_LABEL_AS_INSN (trial))) != 0
2201 && ! (NONJUMP_INSN_P (next_trial)
2202 && GET_CODE (PATTERN (next_trial)) == SEQUENCE)
2203 && !JUMP_P (next_trial)
2204 && ! insn_references_resource_p (next_trial, &set, true)
2205 && ! insn_sets_resource_p (next_trial, &set, true)
2206 && ! insn_sets_resource_p (next_trial, &needed, true)
2207 && (!HAVE_cc0 || ! reg_mentioned_p (cc0_rtx, PATTERN (next_trial)))
2208 && ! (maybe_never && may_trap_or_fault_p (PATTERN (next_trial)))
2209 && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2210 && eligible_for_delay (insn, slots_filled, next_trial, flags)
2211 && ! can_throw_internal (trial))
2213 /* See comment in relax_delay_slots about necessity of using
2214 next_real_insn here. */
2215 rtx_insn *new_label = next_real_insn (next_trial);
2217 if (new_label != 0)
2218 new_label = get_label_before (new_label, JUMP_LABEL (trial));
2219 else
2220 new_label = find_end_label (simple_return_rtx);
2222 if (new_label)
2224 add_to_delay_list (copy_delay_slot_insn (next_trial),
2225 &delay_list);
2226 slots_filled++;
2227 reorg_redirect_jump (as_a <rtx_jump_insn *> (trial),
2228 new_label);
2233 /* If this is an unconditional jump, then try to get insns from the
2234 target of the jump. */
2235 rtx_jump_insn *jump_insn;
2236 if ((jump_insn = dyn_cast <rtx_jump_insn *> (insn))
2237 && simplejump_p (jump_insn)
2238 && slots_filled != slots_to_fill)
2239 fill_slots_from_thread (jump_insn, const_true_rtx,
2240 next_active_insn (JUMP_LABEL_AS_INSN (insn)),
2241 NULL, 1, 1, own_thread_p (JUMP_LABEL (insn),
2242 JUMP_LABEL (insn), 0),
2243 slots_to_fill, &slots_filled, &delay_list);
2245 if (!delay_list.is_empty ())
2246 unfilled_slots_base[i]
2247 = emit_delay_sequence (insn, delay_list, slots_filled);
2249 if (slots_to_fill == slots_filled)
2250 unfilled_slots_base[i] = 0;
2252 note_delay_statistics (slots_filled, 0);
2256 /* Follow any unconditional jump at LABEL, for the purpose of redirecting JUMP;
2257 return the ultimate label reached by any such chain of jumps.
2258 Return a suitable return rtx if the chain ultimately leads to a
2259 return instruction.
2260 If LABEL is not followed by a jump, return LABEL.
2261 If the chain loops or we can't find end, return LABEL,
2262 since that tells caller to avoid changing the insn.
2263 If the returned label is obtained by following a crossing jump,
2264 set *CROSSING to true, otherwise set it to false. */
2266 static rtx
2267 follow_jumps (rtx label, rtx_insn *jump, bool *crossing)
2269 rtx_insn *insn;
2270 rtx_insn *next;
2271 int depth;
2273 *crossing = false;
2274 if (ANY_RETURN_P (label))
2275 return label;
2277 rtx_insn *value = as_a <rtx_insn *> (label);
2279 for (depth = 0;
2280 (depth < 10
2281 && (insn = next_active_insn (value)) != 0
2282 && JUMP_P (insn)
2283 && JUMP_LABEL (insn) != NULL_RTX
2284 && ((any_uncondjump_p (insn) && onlyjump_p (insn))
2285 || ANY_RETURN_P (PATTERN (insn)))
2286 && (next = NEXT_INSN (insn))
2287 && BARRIER_P (next));
2288 depth++)
2290 rtx this_label_or_return = JUMP_LABEL (insn);
2292 /* If we have found a cycle, make the insn jump to itself. */
2293 if (this_label_or_return == label)
2294 return label;
2296 /* Cannot follow returns and cannot look through tablejumps. */
2297 if (ANY_RETURN_P (this_label_or_return))
2298 return this_label_or_return;
2300 rtx_insn *this_label = as_a <rtx_insn *> (this_label_or_return);
2301 if (NEXT_INSN (this_label)
2302 && JUMP_TABLE_DATA_P (NEXT_INSN (this_label)))
2303 break;
2305 if (!targetm.can_follow_jump (jump, insn))
2306 break;
2307 if (!*crossing)
2308 *crossing = CROSSING_JUMP_P (jump);
2309 value = this_label;
2311 if (depth == 10)
2312 return label;
2313 return value;
2316 /* Try to find insns to place in delay slots.
2318 INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2319 or is an unconditional branch if CONDITION is const_true_rtx.
2320 *PSLOTS_FILLED is updated with the number of slots that we have filled.
2322 THREAD is a flow-of-control, either the insns to be executed if the
2323 branch is true or if the branch is false, THREAD_IF_TRUE says which.
2325 OPPOSITE_THREAD is the thread in the opposite direction. It is used
2326 to see if any potential delay slot insns set things needed there.
2328 LIKELY is nonzero if it is extremely likely that the branch will be
2329 taken and THREAD_IF_TRUE is set. This is used for the branch at the
2330 end of a loop back up to the top.
2332 OWN_THREAD is true if we are the only user of the thread, i.e. it is
2333 the target of the jump when we are the only jump going there.
2335 If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2336 case, we can only take insns from the head of the thread for our delay
2337 slot. We then adjust the jump to point after the insns we have taken. */
2339 static void
2340 fill_slots_from_thread (rtx_jump_insn *insn, rtx condition,
2341 rtx thread_or_return, rtx opposite_thread, int likely,
2342 int thread_if_true, int own_thread, int slots_to_fill,
2343 int *pslots_filled, vec<rtx_insn *> *delay_list)
2345 rtx new_thread;
2346 struct resources opposite_needed, set, needed;
2347 rtx_insn *trial;
2348 int lose = 0;
2349 int must_annul = 0;
2350 int flags;
2352 /* Validate our arguments. */
2353 gcc_assert (condition != const_true_rtx || thread_if_true);
2354 gcc_assert (own_thread || thread_if_true);
2356 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2358 /* If our thread is the end of subroutine, we can't get any delay
2359 insns from that. */
2360 if (thread_or_return == NULL_RTX || ANY_RETURN_P (thread_or_return))
2361 return;
2363 rtx_insn *thread = as_a <rtx_insn *> (thread_or_return);
2365 /* If this is an unconditional branch, nothing is needed at the
2366 opposite thread. Otherwise, compute what is needed there. */
2367 if (condition == const_true_rtx)
2368 CLEAR_RESOURCE (&opposite_needed);
2369 else
2370 mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2372 /* If the insn at THREAD can be split, do it here to avoid having to
2373 update THREAD and NEW_THREAD if it is done in the loop below. Also
2374 initialize NEW_THREAD. */
2376 new_thread = thread = try_split (PATTERN (thread), thread, 0);
2378 /* Scan insns at THREAD. We are looking for an insn that can be removed
2379 from THREAD (it neither sets nor references resources that were set
2380 ahead of it and it doesn't set anything needs by the insns ahead of
2381 it) and that either can be placed in an annulling insn or aren't
2382 needed at OPPOSITE_THREAD. */
2384 CLEAR_RESOURCE (&needed);
2385 CLEAR_RESOURCE (&set);
2387 /* If we do not own this thread, we must stop as soon as we find
2388 something that we can't put in a delay slot, since all we can do
2389 is branch into THREAD at a later point. Therefore, labels stop
2390 the search if this is not the `true' thread. */
2392 for (trial = thread;
2393 ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2394 trial = next_nonnote_insn (trial))
2396 rtx pat, old_trial;
2398 /* If we have passed a label, we no longer own this thread. */
2399 if (LABEL_P (trial))
2401 own_thread = 0;
2402 continue;
2405 pat = PATTERN (trial);
2406 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2407 continue;
2409 /* If TRIAL conflicts with the insns ahead of it, we lose. Also,
2410 don't separate or copy insns that set and use CC0. */
2411 if (! insn_references_resource_p (trial, &set, true)
2412 && ! insn_sets_resource_p (trial, &set, true)
2413 && ! insn_sets_resource_p (trial, &needed, true)
2414 && (!HAVE_cc0 || (! (reg_mentioned_p (cc0_rtx, pat)
2415 && (! own_thread || ! sets_cc0_p (pat)))))
2416 && ! can_throw_internal (trial))
2418 rtx_insn *prior_insn;
2420 /* If TRIAL is redundant with some insn before INSN, we don't
2421 actually need to add it to the delay list; we can merely pretend
2422 we did. */
2423 if ((prior_insn = redundant_insn (trial, insn, *delay_list)))
2425 fix_reg_dead_note (prior_insn, insn);
2426 if (own_thread)
2428 update_block (trial, thread);
2429 if (trial == thread)
2431 thread = next_active_insn (thread);
2432 if (new_thread == trial)
2433 new_thread = thread;
2436 delete_related_insns (trial);
2438 else
2440 update_reg_unused_notes (prior_insn, trial);
2441 new_thread = next_active_insn (trial);
2444 continue;
2447 /* There are two ways we can win: If TRIAL doesn't set anything
2448 needed at the opposite thread and can't trap, or if it can
2449 go into an annulled delay slot. But we want neither to copy
2450 nor to speculate frame-related insns. */
2451 if (!must_annul
2452 && ((condition == const_true_rtx
2453 && (own_thread || !RTX_FRAME_RELATED_P (trial)))
2454 || (! insn_sets_resource_p (trial, &opposite_needed, true)
2455 && ! may_trap_or_fault_p (pat)
2456 && ! RTX_FRAME_RELATED_P (trial))))
2458 old_trial = trial;
2459 trial = try_split (pat, trial, 0);
2460 if (new_thread == old_trial)
2461 new_thread = trial;
2462 if (thread == old_trial)
2463 thread = trial;
2464 pat = PATTERN (trial);
2465 if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2466 goto winner;
2468 else if (!RTX_FRAME_RELATED_P (trial)
2469 && ((ANNUL_IFTRUE_SLOTS && ! thread_if_true)
2470 || (ANNUL_IFFALSE_SLOTS && thread_if_true)))
2472 old_trial = trial;
2473 trial = try_split (pat, trial, 0);
2474 if (new_thread == old_trial)
2475 new_thread = trial;
2476 if (thread == old_trial)
2477 thread = trial;
2478 pat = PATTERN (trial);
2479 if ((must_annul || delay_list->is_empty ()) && (thread_if_true
2480 ? check_annul_list_true_false (0, *delay_list)
2481 && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2482 : check_annul_list_true_false (1, *delay_list)
2483 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2485 rtx_insn *temp;
2487 must_annul = 1;
2488 winner:
2490 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, pat))
2491 link_cc0_insns (trial);
2493 /* If we own this thread, delete the insn. If this is the
2494 destination of a branch, show that a basic block status
2495 may have been updated. In any case, mark the new
2496 starting point of this thread. */
2497 if (own_thread)
2499 rtx note;
2501 update_block (trial, thread);
2502 if (trial == thread)
2504 thread = next_active_insn (thread);
2505 if (new_thread == trial)
2506 new_thread = thread;
2509 /* We are moving this insn, not deleting it. We must
2510 temporarily increment the use count on any referenced
2511 label lest it be deleted by delete_related_insns. */
2512 for (note = REG_NOTES (trial);
2513 note != NULL_RTX;
2514 note = XEXP (note, 1))
2515 if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2516 || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2518 /* REG_LABEL_OPERAND could be
2519 NOTE_INSN_DELETED_LABEL too. */
2520 if (LABEL_P (XEXP (note, 0)))
2521 LABEL_NUSES (XEXP (note, 0))++;
2522 else
2523 gcc_assert (REG_NOTE_KIND (note)
2524 == REG_LABEL_OPERAND);
2526 if (jump_to_label_p (trial))
2527 LABEL_NUSES (JUMP_LABEL (trial))++;
2529 delete_related_insns (trial);
2531 for (note = REG_NOTES (trial);
2532 note != NULL_RTX;
2533 note = XEXP (note, 1))
2534 if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2535 || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2537 /* REG_LABEL_OPERAND could be
2538 NOTE_INSN_DELETED_LABEL too. */
2539 if (LABEL_P (XEXP (note, 0)))
2540 LABEL_NUSES (XEXP (note, 0))--;
2541 else
2542 gcc_assert (REG_NOTE_KIND (note)
2543 == REG_LABEL_OPERAND);
2545 if (jump_to_label_p (trial))
2546 LABEL_NUSES (JUMP_LABEL (trial))--;
2548 else
2549 new_thread = next_active_insn (trial);
2551 temp = own_thread ? trial : copy_delay_slot_insn (trial);
2552 if (thread_if_true)
2553 INSN_FROM_TARGET_P (temp) = 1;
2555 add_to_delay_list (temp, delay_list);
2557 if (slots_to_fill == ++(*pslots_filled))
2559 /* Even though we have filled all the slots, we
2560 may be branching to a location that has a
2561 redundant insn. Skip any if so. */
2562 while (new_thread && ! own_thread
2563 && ! insn_sets_resource_p (new_thread, &set, true)
2564 && ! insn_sets_resource_p (new_thread, &needed,
2565 true)
2566 && ! insn_references_resource_p (new_thread,
2567 &set, true)
2568 && (prior_insn
2569 = redundant_insn (new_thread, insn,
2570 *delay_list)))
2572 /* We know we do not own the thread, so no need
2573 to call update_block and delete_insn. */
2574 fix_reg_dead_note (prior_insn, insn);
2575 update_reg_unused_notes (prior_insn, new_thread);
2576 new_thread
2577 = next_active_insn (as_a<rtx_insn *> (new_thread));
2579 break;
2582 continue;
2587 /* This insn can't go into a delay slot. */
2588 lose = 1;
2589 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2590 mark_referenced_resources (trial, &needed, true);
2592 /* Ensure we don't put insns between the setting of cc and the comparison
2593 by moving a setting of cc into an earlier delay slot since these insns
2594 could clobber the condition code. */
2595 set.cc = 1;
2597 /* If this insn is a register-register copy and the next insn has
2598 a use of our destination, change it to use our source. That way,
2599 it will become a candidate for our delay slot the next time
2600 through this loop. This case occurs commonly in loops that
2601 scan a list.
2603 We could check for more complex cases than those tested below,
2604 but it doesn't seem worth it. It might also be a good idea to try
2605 to swap the two insns. That might do better.
2607 We can't do this if the next insn modifies our destination, because
2608 that would make the replacement into the insn invalid. We also can't
2609 do this if it modifies our source, because it might be an earlyclobber
2610 operand. This latter test also prevents updating the contents of
2611 a PRE_INC. We also can't do this if there's overlap of source and
2612 destination. Overlap may happen for larger-than-register-size modes. */
2614 if (NONJUMP_INSN_P (trial) && GET_CODE (pat) == SET
2615 && REG_P (SET_SRC (pat))
2616 && REG_P (SET_DEST (pat))
2617 && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2619 rtx_insn *next = next_nonnote_insn (trial);
2621 if (next && NONJUMP_INSN_P (next)
2622 && GET_CODE (PATTERN (next)) != USE
2623 && ! reg_set_p (SET_DEST (pat), next)
2624 && ! reg_set_p (SET_SRC (pat), next)
2625 && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2626 && ! modified_in_p (SET_DEST (pat), next))
2627 validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2631 /* If we stopped on a branch insn that has delay slots, see if we can
2632 steal some of the insns in those slots. */
2633 if (trial && NONJUMP_INSN_P (trial)
2634 && GET_CODE (PATTERN (trial)) == SEQUENCE
2635 && JUMP_P (XVECEXP (PATTERN (trial), 0, 0)))
2637 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (trial));
2638 /* If this is the `true' thread, we will want to follow the jump,
2639 so we can only do this if we have taken everything up to here. */
2640 if (thread_if_true && trial == new_thread)
2642 steal_delay_list_from_target (insn, condition, sequence,
2643 delay_list, &set, &needed,
2644 &opposite_needed, slots_to_fill,
2645 pslots_filled, &must_annul,
2646 &new_thread);
2647 /* If we owned the thread and are told that it branched
2648 elsewhere, make sure we own the thread at the new location. */
2649 if (own_thread && trial != new_thread)
2650 own_thread = own_thread_p (new_thread, new_thread, 0);
2652 else if (! thread_if_true)
2653 steal_delay_list_from_fallthrough (insn, condition, sequence,
2654 delay_list, &set, &needed,
2655 &opposite_needed, slots_to_fill,
2656 pslots_filled, &must_annul);
2659 /* If we haven't found anything for this delay slot and it is very
2660 likely that the branch will be taken, see if the insn at our target
2661 increments or decrements a register with an increment that does not
2662 depend on the destination register. If so, try to place the opposite
2663 arithmetic insn after the jump insn and put the arithmetic insn in the
2664 delay slot. If we can't do this, return. */
2665 if (delay_list->is_empty () && likely
2666 && new_thread && !ANY_RETURN_P (new_thread)
2667 && NONJUMP_INSN_P (new_thread)
2668 && !RTX_FRAME_RELATED_P (new_thread)
2669 && GET_CODE (PATTERN (new_thread)) != ASM_INPUT
2670 && asm_noperands (PATTERN (new_thread)) < 0)
2672 rtx pat = PATTERN (new_thread);
2673 rtx dest;
2674 rtx src;
2676 /* We know "new_thread" is an insn due to NONJUMP_INSN_P (new_thread)
2677 above. */
2678 trial = as_a <rtx_insn *> (new_thread);
2679 pat = PATTERN (trial);
2681 if (!NONJUMP_INSN_P (trial)
2682 || GET_CODE (pat) != SET
2683 || ! eligible_for_delay (insn, 0, trial, flags)
2684 || can_throw_internal (trial))
2685 return;
2687 dest = SET_DEST (pat), src = SET_SRC (pat);
2688 if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2689 && rtx_equal_p (XEXP (src, 0), dest)
2690 && (!FLOAT_MODE_P (GET_MODE (src))
2691 || flag_unsafe_math_optimizations)
2692 && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2693 && ! side_effects_p (pat))
2695 rtx other = XEXP (src, 1);
2696 rtx new_arith;
2697 rtx_insn *ninsn;
2699 /* If this is a constant adjustment, use the same code with
2700 the negated constant. Otherwise, reverse the sense of the
2701 arithmetic. */
2702 if (CONST_INT_P (other))
2703 new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2704 negate_rtx (GET_MODE (src), other));
2705 else
2706 new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2707 GET_MODE (src), dest, other);
2709 ninsn = emit_insn_after (gen_rtx_SET (dest, new_arith), insn);
2711 if (recog_memoized (ninsn) < 0
2712 || (extract_insn (ninsn),
2713 !constrain_operands (1, get_preferred_alternatives (ninsn))))
2715 delete_related_insns (ninsn);
2716 return;
2719 if (own_thread)
2721 update_block (trial, thread);
2722 if (trial == thread)
2724 thread = next_active_insn (thread);
2725 if (new_thread == trial)
2726 new_thread = thread;
2728 delete_related_insns (trial);
2730 else
2731 new_thread = next_active_insn (trial);
2733 ninsn = own_thread ? trial : copy_delay_slot_insn (trial);
2734 if (thread_if_true)
2735 INSN_FROM_TARGET_P (ninsn) = 1;
2737 add_to_delay_list (ninsn, delay_list);
2738 (*pslots_filled)++;
2742 if (!delay_list->is_empty () && must_annul)
2743 INSN_ANNULLED_BRANCH_P (insn) = 1;
2745 /* If we are to branch into the middle of this thread, find an appropriate
2746 label or make a new one if none, and redirect INSN to it. If we hit the
2747 end of the function, use the end-of-function label. */
2748 if (new_thread != thread)
2750 rtx label;
2751 bool crossing = false;
2753 gcc_assert (thread_if_true);
2755 if (new_thread && simplejump_or_return_p (new_thread)
2756 && redirect_with_delay_list_safe_p (insn,
2757 JUMP_LABEL (new_thread),
2758 *delay_list))
2759 new_thread = follow_jumps (JUMP_LABEL (new_thread), insn,
2760 &crossing);
2762 if (ANY_RETURN_P (new_thread))
2763 label = find_end_label (new_thread);
2764 else if (LABEL_P (new_thread))
2765 label = new_thread;
2766 else
2767 label = get_label_before (as_a <rtx_insn *> (new_thread),
2768 JUMP_LABEL (insn));
2770 if (label)
2772 reorg_redirect_jump (insn, label);
2773 if (crossing)
2774 CROSSING_JUMP_P (insn) = 1;
2779 /* Make another attempt to find insns to place in delay slots.
2781 We previously looked for insns located in front of the delay insn
2782 and, for non-jump delay insns, located behind the delay insn.
2784 Here only try to schedule jump insns and try to move insns from either
2785 the target or the following insns into the delay slot. If annulling is
2786 supported, we will be likely to do this. Otherwise, we can do this only
2787 if safe. */
2789 static void
2790 fill_eager_delay_slots (void)
2792 rtx_insn *insn;
2793 int i;
2794 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2796 for (i = 0; i < num_unfilled_slots; i++)
2798 rtx condition;
2799 rtx target_label, insn_at_target;
2800 rtx_insn *fallthrough_insn;
2801 auto_vec<rtx_insn *, 5> delay_list;
2802 rtx_jump_insn *jump_insn;
2803 int own_target;
2804 int own_fallthrough;
2805 int prediction, slots_to_fill, slots_filled;
2807 insn = unfilled_slots_base[i];
2808 if (insn == 0
2809 || insn->deleted ()
2810 || ! (jump_insn = dyn_cast <rtx_jump_insn *> (insn))
2811 || ! (condjump_p (jump_insn) || condjump_in_parallel_p (jump_insn)))
2812 continue;
2814 slots_to_fill = num_delay_slots (jump_insn);
2815 /* Some machine description have defined instructions to have
2816 delay slots only in certain circumstances which may depend on
2817 nearby insns (which change due to reorg's actions).
2819 For example, the PA port normally has delay slots for unconditional
2820 jumps.
2822 However, the PA port claims such jumps do not have a delay slot
2823 if they are immediate successors of certain CALL_INSNs. This
2824 allows the port to favor filling the delay slot of the call with
2825 the unconditional jump. */
2826 if (slots_to_fill == 0)
2827 continue;
2829 slots_filled = 0;
2830 target_label = JUMP_LABEL (jump_insn);
2831 condition = get_branch_condition (jump_insn, target_label);
2833 if (condition == 0)
2834 continue;
2836 /* Get the next active fallthrough and target insns and see if we own
2837 them. Then see whether the branch is likely true. We don't need
2838 to do a lot of this for unconditional branches. */
2840 insn_at_target = first_active_target_insn (target_label);
2841 own_target = own_thread_p (target_label, target_label, 0);
2843 if (condition == const_true_rtx)
2845 own_fallthrough = 0;
2846 fallthrough_insn = 0;
2847 prediction = 2;
2849 else
2851 fallthrough_insn = next_active_insn (jump_insn);
2852 own_fallthrough = own_thread_p (NEXT_INSN (jump_insn), NULL_RTX, 1);
2853 prediction = mostly_true_jump (jump_insn);
2856 /* If this insn is expected to branch, first try to get insns from our
2857 target, then our fallthrough insns. If it is not expected to branch,
2858 try the other order. */
2860 if (prediction > 0)
2862 fill_slots_from_thread (jump_insn, condition, insn_at_target,
2863 fallthrough_insn, prediction == 2, 1,
2864 own_target,
2865 slots_to_fill, &slots_filled, &delay_list);
2867 if (delay_list.is_empty () && own_fallthrough)
2869 /* Even though we didn't find anything for delay slots,
2870 we might have found a redundant insn which we deleted
2871 from the thread that was filled. So we have to recompute
2872 the next insn at the target. */
2873 target_label = JUMP_LABEL (jump_insn);
2874 insn_at_target = first_active_target_insn (target_label);
2876 fill_slots_from_thread (jump_insn, condition, fallthrough_insn,
2877 insn_at_target, 0, 0, own_fallthrough,
2878 slots_to_fill, &slots_filled,
2879 &delay_list);
2882 else
2884 if (own_fallthrough)
2885 fill_slots_from_thread (jump_insn, condition, fallthrough_insn,
2886 insn_at_target, 0, 0, own_fallthrough,
2887 slots_to_fill, &slots_filled, &delay_list);
2889 if (delay_list.is_empty ())
2890 fill_slots_from_thread (jump_insn, condition, insn_at_target,
2891 next_active_insn (insn), 0, 1, own_target,
2892 slots_to_fill, &slots_filled, &delay_list);
2895 if (!delay_list.is_empty ())
2896 unfilled_slots_base[i]
2897 = emit_delay_sequence (jump_insn, delay_list, slots_filled);
2899 if (slots_to_fill == slots_filled)
2900 unfilled_slots_base[i] = 0;
2902 note_delay_statistics (slots_filled, 1);
2906 static void delete_computation (rtx_insn *insn);
2908 /* Recursively delete prior insns that compute the value (used only by INSN
2909 which the caller is deleting) stored in the register mentioned by NOTE
2910 which is a REG_DEAD note associated with INSN. */
2912 static void
2913 delete_prior_computation (rtx note, rtx_insn *insn)
2915 rtx_insn *our_prev;
2916 rtx reg = XEXP (note, 0);
2918 for (our_prev = prev_nonnote_insn (insn);
2919 our_prev && (NONJUMP_INSN_P (our_prev)
2920 || CALL_P (our_prev));
2921 our_prev = prev_nonnote_insn (our_prev))
2923 rtx pat = PATTERN (our_prev);
2925 /* If we reach a CALL which is not calling a const function
2926 or the callee pops the arguments, then give up. */
2927 if (CALL_P (our_prev)
2928 && (! RTL_CONST_CALL_P (our_prev)
2929 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
2930 break;
2932 /* If we reach a SEQUENCE, it is too complex to try to
2933 do anything with it, so give up. We can be run during
2934 and after reorg, so SEQUENCE rtl can legitimately show
2935 up here. */
2936 if (GET_CODE (pat) == SEQUENCE)
2937 break;
2939 if (GET_CODE (pat) == USE
2940 && NONJUMP_INSN_P (XEXP (pat, 0)))
2941 /* reorg creates USEs that look like this. We leave them
2942 alone because reorg needs them for its own purposes. */
2943 break;
2945 if (reg_set_p (reg, pat))
2947 if (side_effects_p (pat) && !CALL_P (our_prev))
2948 break;
2950 if (GET_CODE (pat) == PARALLEL)
2952 /* If we find a SET of something else, we can't
2953 delete the insn. */
2955 int i;
2957 for (i = 0; i < XVECLEN (pat, 0); i++)
2959 rtx part = XVECEXP (pat, 0, i);
2961 if (GET_CODE (part) == SET
2962 && SET_DEST (part) != reg)
2963 break;
2966 if (i == XVECLEN (pat, 0))
2967 delete_computation (our_prev);
2969 else if (GET_CODE (pat) == SET
2970 && REG_P (SET_DEST (pat)))
2972 int dest_regno = REGNO (SET_DEST (pat));
2973 int dest_endregno = END_REGNO (SET_DEST (pat));
2974 int regno = REGNO (reg);
2975 int endregno = END_REGNO (reg);
2977 if (dest_regno >= regno
2978 && dest_endregno <= endregno)
2979 delete_computation (our_prev);
2981 /* We may have a multi-word hard register and some, but not
2982 all, of the words of the register are needed in subsequent
2983 insns. Write REG_UNUSED notes for those parts that were not
2984 needed. */
2985 else if (dest_regno <= regno
2986 && dest_endregno >= endregno)
2988 int i;
2990 add_reg_note (our_prev, REG_UNUSED, reg);
2992 for (i = dest_regno; i < dest_endregno; i++)
2993 if (! find_regno_note (our_prev, REG_UNUSED, i))
2994 break;
2996 if (i == dest_endregno)
2997 delete_computation (our_prev);
3001 break;
3004 /* If PAT references the register that dies here, it is an
3005 additional use. Hence any prior SET isn't dead. However, this
3006 insn becomes the new place for the REG_DEAD note. */
3007 if (reg_overlap_mentioned_p (reg, pat))
3009 XEXP (note, 1) = REG_NOTES (our_prev);
3010 REG_NOTES (our_prev) = note;
3011 break;
3016 /* Delete INSN and recursively delete insns that compute values used only
3017 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3019 Look at all our REG_DEAD notes. If a previous insn does nothing other
3020 than set a register that dies in this insn, we can delete that insn
3021 as well.
3023 On machines with CC0, if CC0 is used in this insn, we may be able to
3024 delete the insn that set it. */
3026 static void
3027 delete_computation (rtx_insn *insn)
3029 rtx note, next;
3031 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
3033 rtx_insn *prev = prev_nonnote_insn (insn);
3034 /* We assume that at this stage
3035 CC's are always set explicitly
3036 and always immediately before the jump that
3037 will use them. So if the previous insn
3038 exists to set the CC's, delete it
3039 (unless it performs auto-increments, etc.). */
3040 if (prev && NONJUMP_INSN_P (prev)
3041 && sets_cc0_p (PATTERN (prev)))
3043 if (sets_cc0_p (PATTERN (prev)) > 0
3044 && ! side_effects_p (PATTERN (prev)))
3045 delete_computation (prev);
3046 else
3047 /* Otherwise, show that cc0 won't be used. */
3048 add_reg_note (prev, REG_UNUSED, cc0_rtx);
3052 for (note = REG_NOTES (insn); note; note = next)
3054 next = XEXP (note, 1);
3056 if (REG_NOTE_KIND (note) != REG_DEAD
3057 /* Verify that the REG_NOTE is legitimate. */
3058 || !REG_P (XEXP (note, 0)))
3059 continue;
3061 delete_prior_computation (note, insn);
3064 delete_related_insns (insn);
3067 /* If all INSN does is set the pc, delete it,
3068 and delete the insn that set the condition codes for it
3069 if that's what the previous thing was. */
3071 static void
3072 delete_jump (rtx_insn *insn)
3074 rtx set = single_set (insn);
3076 if (set && GET_CODE (SET_DEST (set)) == PC)
3077 delete_computation (insn);
3080 static rtx_insn *
3081 label_before_next_insn (rtx_insn *x, rtx scan_limit)
3083 rtx_insn *insn = next_active_insn (x);
3084 while (insn)
3086 insn = PREV_INSN (insn);
3087 if (insn == scan_limit || insn == NULL_RTX)
3088 return NULL;
3089 if (LABEL_P (insn))
3090 break;
3092 return insn;
3095 /* Return TRUE if there is a NOTE_INSN_SWITCH_TEXT_SECTIONS note in between
3096 BEG and END. */
3098 static bool
3099 switch_text_sections_between_p (const rtx_insn *beg, const rtx_insn *end)
3101 const rtx_insn *p;
3102 for (p = beg; p != end; p = NEXT_INSN (p))
3103 if (NOTE_P (p) && NOTE_KIND (p) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
3104 return true;
3105 return false;
3109 /* Once we have tried two ways to fill a delay slot, make a pass over the
3110 code to try to improve the results and to do such things as more jump
3111 threading. */
3113 static void
3114 relax_delay_slots (rtx_insn *first)
3116 rtx_insn *insn, *next;
3117 rtx_sequence *pat;
3118 rtx_insn *delay_insn;
3119 rtx target_label;
3121 /* Look at every JUMP_INSN and see if we can improve it. */
3122 for (insn = first; insn; insn = next)
3124 rtx_insn *other, *prior_insn;
3125 bool crossing;
3127 next = next_active_insn (insn);
3129 /* If this is a jump insn, see if it now jumps to a jump, jumps to
3130 the next insn, or jumps to a label that is not the last of a
3131 group of consecutive labels. */
3132 if (is_a <rtx_jump_insn *> (insn)
3133 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3134 && !ANY_RETURN_P (target_label = JUMP_LABEL (insn)))
3136 rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (insn);
3137 target_label
3138 = skip_consecutive_labels (follow_jumps (target_label, jump_insn,
3139 &crossing));
3140 if (ANY_RETURN_P (target_label))
3141 target_label = find_end_label (target_label);
3143 if (target_label
3144 && next_active_insn (as_a<rtx_insn *> (target_label)) == next
3145 && ! condjump_in_parallel_p (jump_insn)
3146 && ! (next && switch_text_sections_between_p (jump_insn, next)))
3148 delete_jump (jump_insn);
3149 continue;
3152 if (target_label && target_label != JUMP_LABEL (jump_insn))
3154 reorg_redirect_jump (jump_insn, target_label);
3155 if (crossing)
3156 CROSSING_JUMP_P (jump_insn) = 1;
3159 /* See if this jump conditionally branches around an unconditional
3160 jump. If so, invert this jump and point it to the target of the
3161 second jump. Check if it's possible on the target. */
3162 if (next && simplejump_or_return_p (next)
3163 && any_condjump_p (jump_insn)
3164 && target_label
3165 && (next_active_insn (as_a<rtx_insn *> (target_label))
3166 == next_active_insn (next))
3167 && no_labels_between_p (jump_insn, next)
3168 && targetm.can_follow_jump (jump_insn, next))
3170 rtx label = JUMP_LABEL (next);
3172 /* Be careful how we do this to avoid deleting code or
3173 labels that are momentarily dead. See similar optimization
3174 in jump.c.
3176 We also need to ensure we properly handle the case when
3177 invert_jump fails. */
3179 ++LABEL_NUSES (target_label);
3180 if (!ANY_RETURN_P (label))
3181 ++LABEL_NUSES (label);
3183 if (invert_jump (jump_insn, label, 1))
3185 delete_related_insns (next);
3186 next = jump_insn;
3189 if (!ANY_RETURN_P (label))
3190 --LABEL_NUSES (label);
3192 if (--LABEL_NUSES (target_label) == 0)
3193 delete_related_insns (target_label);
3195 continue;
3199 /* If this is an unconditional jump and the previous insn is a
3200 conditional jump, try reversing the condition of the previous
3201 insn and swapping our targets. The next pass might be able to
3202 fill the slots.
3204 Don't do this if we expect the conditional branch to be true, because
3205 we would then be making the more common case longer. */
3207 if (simplejump_or_return_p (insn)
3208 && (other = prev_active_insn (insn)) != 0
3209 && any_condjump_p (other)
3210 && no_labels_between_p (other, insn)
3211 && mostly_true_jump (other) < 0)
3213 rtx other_target = JUMP_LABEL (other);
3214 target_label = JUMP_LABEL (insn);
3216 if (invert_jump (as_a <rtx_jump_insn *> (other), target_label, 0))
3217 reorg_redirect_jump (as_a <rtx_jump_insn *> (insn), other_target);
3220 /* Now look only at cases where we have a filled delay slot. */
3221 if (!NONJUMP_INSN_P (insn) || GET_CODE (PATTERN (insn)) != SEQUENCE)
3222 continue;
3224 pat = as_a <rtx_sequence *> (PATTERN (insn));
3225 delay_insn = pat->insn (0);
3227 /* See if the first insn in the delay slot is redundant with some
3228 previous insn. Remove it from the delay slot if so; then set up
3229 to reprocess this insn. */
3230 if ((prior_insn = redundant_insn (pat->insn (1), delay_insn, vNULL)))
3232 fix_reg_dead_note (prior_insn, insn);
3233 update_block (pat->insn (1), insn);
3234 delete_from_delay_slot (pat->insn (1));
3235 next = prev_active_insn (next);
3236 continue;
3239 /* See if we have a RETURN insn with a filled delay slot followed
3240 by a RETURN insn with an unfilled a delay slot. If so, we can delete
3241 the first RETURN (but not its delay insn). This gives the same
3242 effect in fewer instructions.
3244 Only do so if optimizing for size since this results in slower, but
3245 smaller code. */
3246 if (optimize_function_for_size_p (cfun)
3247 && ANY_RETURN_P (PATTERN (delay_insn))
3248 && next
3249 && JUMP_P (next)
3250 && PATTERN (next) == PATTERN (delay_insn))
3252 rtx_insn *after;
3253 int i;
3255 /* Delete the RETURN and just execute the delay list insns.
3257 We do this by deleting the INSN containing the SEQUENCE, then
3258 re-emitting the insns separately, and then deleting the RETURN.
3259 This allows the count of the jump target to be properly
3260 decremented.
3262 Note that we need to change the INSN_UID of the re-emitted insns
3263 since it is used to hash the insns for mark_target_live_regs and
3264 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3266 Clear the from target bit, since these insns are no longer
3267 in delay slots. */
3268 for (i = 0; i < XVECLEN (pat, 0); i++)
3269 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3271 rtx_insn *prev = PREV_INSN (insn);
3272 delete_related_insns (insn);
3273 gcc_assert (GET_CODE (pat) == SEQUENCE);
3274 add_insn_after (delay_insn, prev, NULL);
3275 after = delay_insn;
3276 for (i = 1; i < pat->len (); i++)
3277 after = emit_copy_of_insn_after (pat->insn (i), after);
3278 delete_scheduled_jump (delay_insn);
3279 continue;
3282 /* Now look only at the cases where we have a filled JUMP_INSN. */
3283 rtx_jump_insn *delay_jump_insn =
3284 dyn_cast <rtx_jump_insn *> (delay_insn);
3285 if (! delay_jump_insn || !(condjump_p (delay_jump_insn)
3286 || condjump_in_parallel_p (delay_jump_insn)))
3287 continue;
3289 target_label = JUMP_LABEL (delay_jump_insn);
3290 if (target_label && ANY_RETURN_P (target_label))
3291 continue;
3293 /* If this jump goes to another unconditional jump, thread it, but
3294 don't convert a jump into a RETURN here. */
3295 rtx trial = skip_consecutive_labels (follow_jumps (target_label,
3296 delay_jump_insn,
3297 &crossing));
3298 if (ANY_RETURN_P (trial))
3299 trial = find_end_label (trial);
3301 if (trial && trial != target_label
3302 && redirect_with_delay_slots_safe_p (delay_jump_insn, trial, insn))
3304 reorg_redirect_jump (delay_jump_insn, trial);
3305 target_label = trial;
3306 if (crossing)
3307 CROSSING_JUMP_P (delay_jump_insn) = 1;
3310 /* If the first insn at TARGET_LABEL is redundant with a previous
3311 insn, redirect the jump to the following insn and process again.
3312 We use next_real_insn instead of next_active_insn so we
3313 don't skip USE-markers, or we'll end up with incorrect
3314 liveness info. */
3315 trial = next_real_insn (target_label);
3316 if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3317 && redundant_insn (trial, insn, vNULL)
3318 && ! can_throw_internal (trial))
3320 /* Figure out where to emit the special USE insn so we don't
3321 later incorrectly compute register live/death info. */
3322 rtx_insn *tmp = next_active_insn (as_a<rtx_insn *> (trial));
3323 if (tmp == 0)
3324 tmp = find_end_label (simple_return_rtx);
3326 if (tmp)
3328 /* Insert the special USE insn and update dataflow info.
3329 We know "trial" is an insn here as it is the output of
3330 next_real_insn () above. */
3331 update_block (as_a <rtx_insn *> (trial), tmp);
3333 /* Now emit a label before the special USE insn, and
3334 redirect our jump to the new label. */
3335 target_label = get_label_before (PREV_INSN (tmp), target_label);
3336 reorg_redirect_jump (delay_jump_insn, target_label);
3337 next = insn;
3338 continue;
3342 /* Similarly, if it is an unconditional jump with one insn in its
3343 delay list and that insn is redundant, thread the jump. */
3344 rtx_sequence *trial_seq =
3345 trial ? dyn_cast <rtx_sequence *> (PATTERN (trial)) : NULL;
3346 if (trial_seq
3347 && trial_seq->len () == 2
3348 && JUMP_P (trial_seq->insn (0))
3349 && simplejump_or_return_p (trial_seq->insn (0))
3350 && redundant_insn (trial_seq->insn (1), insn, vNULL))
3352 rtx temp_label = JUMP_LABEL (trial_seq->insn (0));
3353 if (ANY_RETURN_P (temp_label))
3354 temp_label = find_end_label (temp_label);
3356 if (temp_label
3357 && redirect_with_delay_slots_safe_p (delay_jump_insn,
3358 temp_label, insn))
3360 update_block (trial_seq->insn (1), insn);
3361 reorg_redirect_jump (delay_jump_insn, temp_label);
3362 next = insn;
3363 continue;
3367 /* See if we have a simple (conditional) jump that is useless. */
3368 if (!CROSSING_JUMP_P (delay_jump_insn)
3369 && !INSN_ANNULLED_BRANCH_P (delay_jump_insn)
3370 && !condjump_in_parallel_p (delay_jump_insn)
3371 && prev_active_insn (as_a<rtx_insn *> (target_label)) == insn
3372 && !BARRIER_P (prev_nonnote_insn (as_a<rtx_insn *> (target_label)))
3373 /* If the last insn in the delay slot sets CC0 for some insn,
3374 various code assumes that it is in a delay slot. We could
3375 put it back where it belonged and delete the register notes,
3376 but it doesn't seem worthwhile in this uncommon case. */
3377 && (!HAVE_cc0
3378 || ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3379 REG_CC_USER, NULL_RTX)))
3381 rtx_insn *after;
3382 int i;
3384 /* All this insn does is execute its delay list and jump to the
3385 following insn. So delete the jump and just execute the delay
3386 list insns.
3388 We do this by deleting the INSN containing the SEQUENCE, then
3389 re-emitting the insns separately, and then deleting the jump.
3390 This allows the count of the jump target to be properly
3391 decremented.
3393 Note that we need to change the INSN_UID of the re-emitted insns
3394 since it is used to hash the insns for mark_target_live_regs and
3395 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3397 Clear the from target bit, since these insns are no longer
3398 in delay slots. */
3399 for (i = 0; i < XVECLEN (pat, 0); i++)
3400 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3402 rtx_insn *prev = PREV_INSN (insn);
3403 delete_related_insns (insn);
3404 gcc_assert (GET_CODE (pat) == SEQUENCE);
3405 add_insn_after (delay_jump_insn, prev, NULL);
3406 after = delay_jump_insn;
3407 for (i = 1; i < pat->len (); i++)
3408 after = emit_copy_of_insn_after (pat->insn (i), after);
3409 delete_scheduled_jump (delay_jump_insn);
3410 continue;
3413 /* See if this is an unconditional jump around a single insn which is
3414 identical to the one in its delay slot. In this case, we can just
3415 delete the branch and the insn in its delay slot. */
3416 if (next && NONJUMP_INSN_P (next)
3417 && label_before_next_insn (next, insn) == target_label
3418 && simplejump_p (insn)
3419 && XVECLEN (pat, 0) == 2
3420 && rtx_equal_p (PATTERN (next), PATTERN (pat->insn (1))))
3422 delete_related_insns (insn);
3423 continue;
3426 /* See if this jump (with its delay slots) conditionally branches
3427 around an unconditional jump (without delay slots). If so, invert
3428 this jump and point it to the target of the second jump. We cannot
3429 do this for annulled jumps, though. Again, don't convert a jump to
3430 a RETURN here. */
3431 if (! INSN_ANNULLED_BRANCH_P (delay_jump_insn)
3432 && any_condjump_p (delay_jump_insn)
3433 && next && simplejump_or_return_p (next)
3434 && (next_active_insn (as_a<rtx_insn *> (target_label))
3435 == next_active_insn (next))
3436 && no_labels_between_p (insn, next))
3438 rtx label = JUMP_LABEL (next);
3439 rtx old_label = JUMP_LABEL (delay_jump_insn);
3441 if (ANY_RETURN_P (label))
3442 label = find_end_label (label);
3444 /* find_end_label can generate a new label. Check this first. */
3445 if (label
3446 && no_labels_between_p (insn, next)
3447 && redirect_with_delay_slots_safe_p (delay_jump_insn,
3448 label, insn))
3450 /* Be careful how we do this to avoid deleting code or labels
3451 that are momentarily dead. See similar optimization in
3452 jump.c */
3453 if (old_label)
3454 ++LABEL_NUSES (old_label);
3456 if (invert_jump (delay_jump_insn, label, 1))
3458 int i;
3460 /* Must update the INSN_FROM_TARGET_P bits now that
3461 the branch is reversed, so that mark_target_live_regs
3462 will handle the delay slot insn correctly. */
3463 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
3465 rtx slot = XVECEXP (PATTERN (insn), 0, i);
3466 INSN_FROM_TARGET_P (slot) = ! INSN_FROM_TARGET_P (slot);
3469 delete_related_insns (next);
3470 next = insn;
3473 if (old_label && --LABEL_NUSES (old_label) == 0)
3474 delete_related_insns (old_label);
3475 continue;
3479 /* If we own the thread opposite the way this insn branches, see if we
3480 can merge its delay slots with following insns. */
3481 if (INSN_FROM_TARGET_P (pat->insn (1))
3482 && own_thread_p (NEXT_INSN (insn), 0, 1))
3483 try_merge_delay_insns (insn, next);
3484 else if (! INSN_FROM_TARGET_P (pat->insn (1))
3485 && own_thread_p (target_label, target_label, 0))
3486 try_merge_delay_insns (insn,
3487 next_active_insn (as_a<rtx_insn *> (target_label)));
3489 /* If we get here, we haven't deleted INSN. But we may have deleted
3490 NEXT, so recompute it. */
3491 next = next_active_insn (insn);
3496 /* Look for filled jumps to the end of function label. We can try to convert
3497 them into RETURN insns if the insns in the delay slot are valid for the
3498 RETURN as well. */
3500 static void
3501 make_return_insns (rtx_insn *first)
3503 rtx_insn *insn;
3504 rtx_jump_insn *jump_insn;
3505 rtx real_return_label = function_return_label;
3506 rtx real_simple_return_label = function_simple_return_label;
3507 int slots, i;
3509 /* See if there is a RETURN insn in the function other than the one we
3510 made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3511 into a RETURN to jump to it. */
3512 for (insn = first; insn; insn = NEXT_INSN (insn))
3513 if (JUMP_P (insn) && ANY_RETURN_P (PATTERN (insn)))
3515 rtx t = get_label_before (insn, NULL_RTX);
3516 if (PATTERN (insn) == ret_rtx)
3517 real_return_label = t;
3518 else
3519 real_simple_return_label = t;
3520 break;
3523 /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3524 was equal to END_OF_FUNCTION_LABEL. */
3525 if (real_return_label)
3526 LABEL_NUSES (real_return_label)++;
3527 if (real_simple_return_label)
3528 LABEL_NUSES (real_simple_return_label)++;
3530 /* Clear the list of insns to fill so we can use it. */
3531 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3533 for (insn = first; insn; insn = NEXT_INSN (insn))
3535 int flags;
3536 rtx kind, real_label;
3538 /* Only look at filled JUMP_INSNs that go to the end of function
3539 label. */
3540 if (!NONJUMP_INSN_P (insn))
3541 continue;
3543 if (GET_CODE (PATTERN (insn)) != SEQUENCE)
3544 continue;
3546 rtx_sequence *pat = as_a <rtx_sequence *> (PATTERN (insn));
3548 if (!jump_to_label_p (pat->insn (0)))
3549 continue;
3551 if (JUMP_LABEL (pat->insn (0)) == function_return_label)
3553 kind = ret_rtx;
3554 real_label = real_return_label;
3556 else if (JUMP_LABEL (pat->insn (0)) == function_simple_return_label)
3558 kind = simple_return_rtx;
3559 real_label = real_simple_return_label;
3561 else
3562 continue;
3564 jump_insn = as_a <rtx_jump_insn *> (pat->insn (0));
3566 /* If we can't make the jump into a RETURN, try to redirect it to the best
3567 RETURN and go on to the next insn. */
3568 if (!reorg_redirect_jump (jump_insn, kind))
3570 /* Make sure redirecting the jump will not invalidate the delay
3571 slot insns. */
3572 if (redirect_with_delay_slots_safe_p (jump_insn, real_label, insn))
3573 reorg_redirect_jump (jump_insn, real_label);
3574 continue;
3577 /* See if this RETURN can accept the insns current in its delay slot.
3578 It can if it has more or an equal number of slots and the contents
3579 of each is valid. */
3581 flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3582 slots = num_delay_slots (jump_insn);
3583 if (slots >= XVECLEN (pat, 0) - 1)
3585 for (i = 1; i < XVECLEN (pat, 0); i++)
3586 if (! (
3587 #if ANNUL_IFFALSE_SLOTS
3588 (INSN_ANNULLED_BRANCH_P (jump_insn)
3589 && INSN_FROM_TARGET_P (pat->insn (i)))
3590 ? eligible_for_annul_false (jump_insn, i - 1,
3591 pat->insn (i), flags) :
3592 #endif
3593 #if ANNUL_IFTRUE_SLOTS
3594 (INSN_ANNULLED_BRANCH_P (jump_insn)
3595 && ! INSN_FROM_TARGET_P (pat->insn (i)))
3596 ? eligible_for_annul_true (jump_insn, i - 1,
3597 pat->insn (i), flags) :
3598 #endif
3599 eligible_for_delay (jump_insn, i - 1,
3600 pat->insn (i), flags)))
3601 break;
3603 else
3604 i = 0;
3606 if (i == XVECLEN (pat, 0))
3607 continue;
3609 /* We have to do something with this insn. If it is an unconditional
3610 RETURN, delete the SEQUENCE and output the individual insns,
3611 followed by the RETURN. Then set things up so we try to find
3612 insns for its delay slots, if it needs some. */
3613 if (ANY_RETURN_P (PATTERN (jump_insn)))
3615 rtx_insn *prev = PREV_INSN (insn);
3617 delete_related_insns (insn);
3618 for (i = 1; i < XVECLEN (pat, 0); i++)
3620 rtx_insn *in_seq_insn = as_a<rtx_insn *> (XVECEXP (pat, 0, i));
3621 prev = emit_insn_after_setloc (PATTERN (in_seq_insn), prev,
3622 INSN_LOCATION (in_seq_insn));
3625 insn = emit_jump_insn_after_setloc (PATTERN (jump_insn), prev,
3626 INSN_LOCATION (jump_insn));
3627 emit_barrier_after (insn);
3629 if (slots)
3630 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3632 else
3633 /* It is probably more efficient to keep this with its current
3634 delay slot as a branch to a RETURN. */
3635 reorg_redirect_jump (jump_insn, real_label);
3638 /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3639 new delay slots we have created. */
3640 if (real_return_label != NULL_RTX && --LABEL_NUSES (real_return_label) == 0)
3641 delete_related_insns (real_return_label);
3642 if (real_simple_return_label != NULL_RTX
3643 && --LABEL_NUSES (real_simple_return_label) == 0)
3644 delete_related_insns (real_simple_return_label);
3646 fill_simple_delay_slots (1);
3647 fill_simple_delay_slots (0);
3650 /* Try to find insns to place in delay slots. */
3652 static void
3653 dbr_schedule (rtx_insn *first)
3655 rtx_insn *insn, *next, *epilogue_insn = 0;
3656 int i;
3657 bool need_return_insns;
3659 /* If the current function has no insns other than the prologue and
3660 epilogue, then do not try to fill any delay slots. */
3661 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
3662 return;
3664 /* Find the highest INSN_UID and allocate and initialize our map from
3665 INSN_UID's to position in code. */
3666 for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3668 if (INSN_UID (insn) > max_uid)
3669 max_uid = INSN_UID (insn);
3670 if (NOTE_P (insn)
3671 && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
3672 epilogue_insn = insn;
3675 uid_to_ruid = XNEWVEC (int, max_uid + 1);
3676 for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3677 uid_to_ruid[INSN_UID (insn)] = i;
3679 /* Initialize the list of insns that need filling. */
3680 if (unfilled_firstobj == 0)
3682 gcc_obstack_init (&unfilled_slots_obstack);
3683 unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3686 for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3688 rtx target;
3690 /* Skip vector tables. We can't get attributes for them. */
3691 if (JUMP_TABLE_DATA_P (insn))
3692 continue;
3694 if (JUMP_P (insn))
3695 INSN_ANNULLED_BRANCH_P (insn) = 0;
3696 INSN_FROM_TARGET_P (insn) = 0;
3698 if (num_delay_slots (insn) > 0)
3699 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3701 /* Ensure all jumps go to the last of a set of consecutive labels. */
3702 if (JUMP_P (insn)
3703 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3704 && !ANY_RETURN_P (JUMP_LABEL (insn))
3705 && ((target = skip_consecutive_labels (JUMP_LABEL (insn)))
3706 != JUMP_LABEL (insn)))
3707 redirect_jump (as_a <rtx_jump_insn *> (insn), target, 1);
3710 init_resource_info (epilogue_insn);
3712 /* Show we haven't computed an end-of-function label yet. */
3713 function_return_label = function_simple_return_label = NULL;
3715 /* Initialize the statistics for this function. */
3716 memset (num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3717 memset (num_filled_delays, 0, sizeof num_filled_delays);
3719 /* Now do the delay slot filling. Try everything twice in case earlier
3720 changes make more slots fillable. */
3722 for (reorg_pass_number = 0;
3723 reorg_pass_number < MAX_REORG_PASSES;
3724 reorg_pass_number++)
3726 fill_simple_delay_slots (1);
3727 fill_simple_delay_slots (0);
3728 if (!targetm.no_speculation_in_delay_slots_p ())
3729 fill_eager_delay_slots ();
3730 relax_delay_slots (first);
3733 /* If we made an end of function label, indicate that it is now
3734 safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3735 If it is now unused, delete it. */
3736 if (function_return_label && --LABEL_NUSES (function_return_label) == 0)
3737 delete_related_insns (function_return_label);
3738 if (function_simple_return_label
3739 && --LABEL_NUSES (function_simple_return_label) == 0)
3740 delete_related_insns (function_simple_return_label);
3742 need_return_insns = false;
3743 need_return_insns |= targetm.have_return () && function_return_label != 0;
3744 need_return_insns |= (targetm.have_simple_return ()
3745 && function_simple_return_label != 0);
3746 if (need_return_insns)
3747 make_return_insns (first);
3749 /* Delete any USE insns made by update_block; subsequent passes don't need
3750 them or know how to deal with them. */
3751 for (insn = first; insn; insn = next)
3753 next = NEXT_INSN (insn);
3755 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
3756 && INSN_P (XEXP (PATTERN (insn), 0)))
3757 next = delete_related_insns (insn);
3760 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3762 /* It is not clear why the line below is needed, but it does seem to be. */
3763 unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3765 if (dump_file)
3767 int i, j, need_comma;
3768 int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3769 int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
3771 for (reorg_pass_number = 0;
3772 reorg_pass_number < MAX_REORG_PASSES;
3773 reorg_pass_number++)
3775 fprintf (dump_file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
3776 for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
3778 need_comma = 0;
3779 fprintf (dump_file, ";; Reorg function #%d\n", i);
3781 fprintf (dump_file, ";; %d insns needing delay slots\n;; ",
3782 num_insns_needing_delays[i][reorg_pass_number]);
3784 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3785 if (num_filled_delays[i][j][reorg_pass_number])
3787 if (need_comma)
3788 fprintf (dump_file, ", ");
3789 need_comma = 1;
3790 fprintf (dump_file, "%d got %d delays",
3791 num_filled_delays[i][j][reorg_pass_number], j);
3793 fprintf (dump_file, "\n");
3796 memset (total_delay_slots, 0, sizeof total_delay_slots);
3797 memset (total_annul_slots, 0, sizeof total_annul_slots);
3798 for (insn = first; insn; insn = NEXT_INSN (insn))
3800 if (! insn->deleted ()
3801 && NONJUMP_INSN_P (insn)
3802 && GET_CODE (PATTERN (insn)) != USE
3803 && GET_CODE (PATTERN (insn)) != CLOBBER)
3805 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3807 rtx control;
3808 j = XVECLEN (PATTERN (insn), 0) - 1;
3809 if (j > MAX_DELAY_HISTOGRAM)
3810 j = MAX_DELAY_HISTOGRAM;
3811 control = XVECEXP (PATTERN (insn), 0, 0);
3812 if (JUMP_P (control) && INSN_ANNULLED_BRANCH_P (control))
3813 total_annul_slots[j]++;
3814 else
3815 total_delay_slots[j]++;
3817 else if (num_delay_slots (insn) > 0)
3818 total_delay_slots[0]++;
3821 fprintf (dump_file, ";; Reorg totals: ");
3822 need_comma = 0;
3823 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3825 if (total_delay_slots[j])
3827 if (need_comma)
3828 fprintf (dump_file, ", ");
3829 need_comma = 1;
3830 fprintf (dump_file, "%d got %d delays", total_delay_slots[j], j);
3833 fprintf (dump_file, "\n");
3835 if (ANNUL_IFTRUE_SLOTS || ANNUL_IFFALSE_SLOTS)
3837 fprintf (dump_file, ";; Reorg annuls: ");
3838 need_comma = 0;
3839 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3841 if (total_annul_slots[j])
3843 if (need_comma)
3844 fprintf (dump_file, ", ");
3845 need_comma = 1;
3846 fprintf (dump_file, "%d got %d delays", total_annul_slots[j], j);
3849 fprintf (dump_file, "\n");
3852 fprintf (dump_file, "\n");
3855 if (!sibling_labels.is_empty ())
3857 update_alignments (sibling_labels);
3858 sibling_labels.release ();
3861 free_resource_info ();
3862 free (uid_to_ruid);
3863 crtl->dbr_scheduled_p = true;
3866 /* Run delay slot optimization. */
3867 static unsigned int
3868 rest_of_handle_delay_slots (void)
3870 if (DELAY_SLOTS)
3871 dbr_schedule (get_insns ());
3873 return 0;
3876 namespace {
3878 const pass_data pass_data_delay_slots =
3880 RTL_PASS, /* type */
3881 "dbr", /* name */
3882 OPTGROUP_NONE, /* optinfo_flags */
3883 TV_DBR_SCHED, /* tv_id */
3884 0, /* properties_required */
3885 0, /* properties_provided */
3886 0, /* properties_destroyed */
3887 0, /* todo_flags_start */
3888 0, /* todo_flags_finish */
3891 class pass_delay_slots : public rtl_opt_pass
3893 public:
3894 pass_delay_slots (gcc::context *ctxt)
3895 : rtl_opt_pass (pass_data_delay_slots, ctxt)
3898 /* opt_pass methods: */
3899 virtual bool gate (function *);
3900 virtual unsigned int execute (function *)
3902 return rest_of_handle_delay_slots ();
3905 }; // class pass_delay_slots
3907 bool
3908 pass_delay_slots::gate (function *)
3910 /* At -O0 dataflow info isn't updated after RA. */
3911 if (DELAY_SLOTS)
3912 return optimize > 0 && flag_delayed_branch && !crtl->dbr_scheduled_p;
3914 return false;
3917 } // anon namespace
3919 rtl_opt_pass *
3920 make_pass_delay_slots (gcc::context *ctxt)
3922 return new pass_delay_slots (ctxt);
3925 /* Machine dependent reorg pass. */
3927 namespace {
3929 const pass_data pass_data_machine_reorg =
3931 RTL_PASS, /* type */
3932 "mach", /* name */
3933 OPTGROUP_NONE, /* optinfo_flags */
3934 TV_MACH_DEP, /* tv_id */
3935 0, /* properties_required */
3936 0, /* properties_provided */
3937 0, /* properties_destroyed */
3938 0, /* todo_flags_start */
3939 0, /* todo_flags_finish */
3942 class pass_machine_reorg : public rtl_opt_pass
3944 public:
3945 pass_machine_reorg (gcc::context *ctxt)
3946 : rtl_opt_pass (pass_data_machine_reorg, ctxt)
3949 /* opt_pass methods: */
3950 virtual bool gate (function *)
3952 return targetm.machine_dependent_reorg != 0;
3955 virtual unsigned int execute (function *)
3957 targetm.machine_dependent_reorg ();
3958 return 0;
3961 }; // class pass_machine_reorg
3963 } // anon namespace
3965 rtl_opt_pass *
3966 make_pass_machine_reorg (gcc::context *ctxt)
3968 return new pass_machine_reorg (ctxt);