[AArch64] Fix vqtb[lx][234] on big-endian
[official-gcc.git] / gcc / reorg.c
blobcc68d6bc5402df2a9ef9785f1b8a206d66e2f2b9
1 /* Perform instruction reorganizations for delay slot filling.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu).
4 Hacked by Michael Tiemann (tiemann@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Instruction reorganization pass.
24 This pass runs after register allocation and final jump
25 optimization. It should be the last pass to run before peephole.
26 It serves primarily to fill delay slots of insns, typically branch
27 and call insns. Other insns typically involve more complicated
28 interactions of data dependencies and resource constraints, and
29 are better handled by scheduling before register allocation (by the
30 function `schedule_insns').
32 The Branch Penalty is the number of extra cycles that are needed to
33 execute a branch insn. On an ideal machine, branches take a single
34 cycle, and the Branch Penalty is 0. Several RISC machines approach
35 branch delays differently:
37 The MIPS has a single branch delay slot. Most insns
38 (except other branches) can be used to fill this slot. When the
39 slot is filled, two insns execute in two cycles, reducing the
40 branch penalty to zero.
42 The SPARC always has a branch delay slot, but its effects can be
43 annulled when the branch is not taken. This means that failing to
44 find other sources of insns, we can hoist an insn from the branch
45 target that would only be safe to execute knowing that the branch
46 is taken.
48 The HP-PA always has a branch delay slot. For unconditional branches
49 its effects can be annulled when the branch is taken. The effects
50 of the delay slot in a conditional branch can be nullified for forward
51 taken branches, or for untaken backward branches. This means
52 we can hoist insns from the fall-through path for forward branches or
53 steal insns from the target of backward branches.
55 The TMS320C3x and C4x have three branch delay slots. When the three
56 slots are filled, the branch penalty is zero. Most insns can fill the
57 delay slots except jump insns.
59 Three techniques for filling delay slots have been implemented so far:
61 (1) `fill_simple_delay_slots' is the simplest, most efficient way
62 to fill delay slots. This pass first looks for insns which come
63 from before the branch and which are safe to execute after the
64 branch. Then it searches after the insn requiring delay slots or,
65 in the case of a branch, for insns that are after the point at
66 which the branch merges into the fallthrough code, if such a point
67 exists. When such insns are found, the branch penalty decreases
68 and no code expansion takes place.
70 (2) `fill_eager_delay_slots' is more complicated: it is used for
71 scheduling conditional jumps, or for scheduling jumps which cannot
72 be filled using (1). A machine need not have annulled jumps to use
73 this strategy, but it helps (by keeping more options open).
74 `fill_eager_delay_slots' tries to guess the direction the branch
75 will go; if it guesses right 100% of the time, it can reduce the
76 branch penalty as much as `fill_simple_delay_slots' does. If it
77 guesses wrong 100% of the time, it might as well schedule nops. When
78 `fill_eager_delay_slots' takes insns from the fall-through path of
79 the jump, usually there is no code expansion; when it takes insns
80 from the branch target, there is code expansion if it is not the
81 only way to reach that target.
83 (3) `relax_delay_slots' uses a set of rules to simplify code that
84 has been reorganized by (1) and (2). It finds cases where
85 conditional test can be eliminated, jumps can be threaded, extra
86 insns can be eliminated, etc. It is the job of (1) and (2) to do a
87 good job of scheduling locally; `relax_delay_slots' takes care of
88 making the various individual schedules work well together. It is
89 especially tuned to handle the control flow interactions of branch
90 insns. It does nothing for insns with delay slots that do not
91 branch.
93 On machines that use CC0, we are very conservative. We will not make
94 a copy of an insn involving CC0 since we want to maintain a 1-1
95 correspondence between the insn that sets and uses CC0. The insns are
96 allowed to be separated by placing an insn that sets CC0 (but not an insn
97 that uses CC0; we could do this, but it doesn't seem worthwhile) in a
98 delay slot. In that case, we point each insn at the other with REG_CC_USER
99 and REG_CC_SETTER notes. Note that these restrictions affect very few
100 machines because most RISC machines with delay slots will not use CC0
101 (the RT is the only known exception at this point). */
103 #include "config.h"
104 #include "system.h"
105 #include "coretypes.h"
106 #include "backend.h"
107 #include "target.h"
108 #include "rtl.h"
109 #include "tree.h"
110 #include "predict.h"
111 #include "tm_p.h"
112 #include "expmed.h"
113 #include "insn-config.h"
114 #include "emit-rtl.h"
115 #include "recog.h"
116 #include "insn-attr.h"
117 #include "resource.h"
118 #include "params.h"
119 #include "tree-pass.h"
122 /* First, some functions that were used before GCC got a control flow graph.
123 These functions are now only used here in reorg.c, and have therefore
124 been moved here to avoid inadvertent misuse elsewhere in the compiler. */
126 /* Return the last label to mark the same position as LABEL. Return LABEL
127 itself if it is null or any return rtx. */
129 static rtx
130 skip_consecutive_labels (rtx label_or_return)
132 rtx_insn *insn;
134 if (label_or_return && ANY_RETURN_P (label_or_return))
135 return label_or_return;
137 rtx_insn *label = as_a <rtx_insn *> (label_or_return);
139 for (insn = label; insn != 0 && !INSN_P (insn); insn = NEXT_INSN (insn))
140 if (LABEL_P (insn))
141 label = insn;
143 return label;
146 /* INSN uses CC0 and is being moved into a delay slot. Set up REG_CC_SETTER
147 and REG_CC_USER notes so we can find it. */
149 static void
150 link_cc0_insns (rtx_insn *insn)
152 rtx user = next_nonnote_insn (insn);
154 if (NONJUMP_INSN_P (user) && GET_CODE (PATTERN (user)) == SEQUENCE)
155 user = XVECEXP (PATTERN (user), 0, 0);
157 add_reg_note (user, REG_CC_SETTER, insn);
158 add_reg_note (insn, REG_CC_USER, user);
161 /* Insns which have delay slots that have not yet been filled. */
163 static struct obstack unfilled_slots_obstack;
164 static rtx *unfilled_firstobj;
166 /* Define macros to refer to the first and last slot containing unfilled
167 insns. These are used because the list may move and its address
168 should be recomputed at each use. */
170 #define unfilled_slots_base \
171 ((rtx_insn **) obstack_base (&unfilled_slots_obstack))
173 #define unfilled_slots_next \
174 ((rtx_insn **) obstack_next_free (&unfilled_slots_obstack))
176 /* Points to the label before the end of the function, or before a
177 return insn. */
178 static rtx_code_label *function_return_label;
179 /* Likewise for a simple_return. */
180 static rtx_code_label *function_simple_return_label;
182 /* Mapping between INSN_UID's and position in the code since INSN_UID's do
183 not always monotonically increase. */
184 static int *uid_to_ruid;
186 /* Highest valid index in `uid_to_ruid'. */
187 static int max_uid;
189 static int stop_search_p (rtx_insn *, int);
190 static int resource_conflicts_p (struct resources *, struct resources *);
191 static int insn_references_resource_p (rtx, struct resources *, bool);
192 static int insn_sets_resource_p (rtx, struct resources *, bool);
193 static rtx_code_label *find_end_label (rtx);
194 static rtx_insn *emit_delay_sequence (rtx_insn *, const vec<rtx_insn *> &,
195 int);
196 static void add_to_delay_list (rtx_insn *, vec<rtx_insn *> *);
197 static rtx_insn *delete_from_delay_slot (rtx_insn *);
198 static void delete_scheduled_jump (rtx_insn *);
199 static void note_delay_statistics (int, int);
200 static int get_jump_flags (const rtx_insn *, rtx);
201 static int mostly_true_jump (rtx);
202 static rtx get_branch_condition (const rtx_insn *, rtx);
203 static int condition_dominates_p (rtx, const rtx_insn *);
204 static int redirect_with_delay_slots_safe_p (rtx_insn *, rtx, rtx);
205 static int redirect_with_delay_list_safe_p (rtx_insn *, rtx,
206 const vec<rtx_insn *> &);
207 static int check_annul_list_true_false (int, const vec<rtx_insn *> &);
208 static void steal_delay_list_from_target (rtx_insn *, rtx, rtx_sequence *,
209 vec<rtx_insn *> *,
210 struct resources *,
211 struct resources *,
212 struct resources *,
213 int, int *, int *,
214 rtx *);
215 static void steal_delay_list_from_fallthrough (rtx_insn *, rtx, rtx_sequence *,
216 vec<rtx_insn *> *,
217 struct resources *,
218 struct resources *,
219 struct resources *,
220 int, int *, int *);
221 static void try_merge_delay_insns (rtx_insn *, rtx_insn *);
222 static rtx redundant_insn (rtx, rtx_insn *, const vec<rtx_insn *> &);
223 static int own_thread_p (rtx, rtx, int);
224 static void update_block (rtx_insn *, rtx);
225 static int reorg_redirect_jump (rtx_jump_insn *, rtx);
226 static void update_reg_dead_notes (rtx_insn *, rtx_insn *);
227 static void fix_reg_dead_note (rtx, rtx);
228 static void update_reg_unused_notes (rtx, rtx);
229 static void fill_simple_delay_slots (int);
230 static void fill_slots_from_thread (rtx_jump_insn *, rtx, rtx, rtx,
231 int, int, int, int,
232 int *, vec<rtx_insn *> *);
233 static void fill_eager_delay_slots (void);
234 static void relax_delay_slots (rtx_insn *);
235 static void make_return_insns (rtx_insn *);
237 /* A wrapper around next_active_insn which takes care to return ret_rtx
238 unchanged. */
240 static rtx
241 first_active_target_insn (rtx insn)
243 if (ANY_RETURN_P (insn))
244 return insn;
245 return next_active_insn (as_a <rtx_insn *> (insn));
248 /* Return true iff INSN is a simplejump, or any kind of return insn. */
250 static bool
251 simplejump_or_return_p (rtx insn)
253 return (JUMP_P (insn)
254 && (simplejump_p (as_a <rtx_insn *> (insn))
255 || ANY_RETURN_P (PATTERN (insn))));
258 /* Return TRUE if this insn should stop the search for insn to fill delay
259 slots. LABELS_P indicates that labels should terminate the search.
260 In all cases, jumps terminate the search. */
262 static int
263 stop_search_p (rtx_insn *insn, int labels_p)
265 if (insn == 0)
266 return 1;
268 /* If the insn can throw an exception that is caught within the function,
269 it may effectively perform a jump from the viewpoint of the function.
270 Therefore act like for a jump. */
271 if (can_throw_internal (insn))
272 return 1;
274 switch (GET_CODE (insn))
276 case NOTE:
277 case CALL_INSN:
278 return 0;
280 case CODE_LABEL:
281 return labels_p;
283 case JUMP_INSN:
284 case BARRIER:
285 return 1;
287 case INSN:
288 /* OK unless it contains a delay slot or is an `asm' insn of some type.
289 We don't know anything about these. */
290 return (GET_CODE (PATTERN (insn)) == SEQUENCE
291 || GET_CODE (PATTERN (insn)) == ASM_INPUT
292 || asm_noperands (PATTERN (insn)) >= 0);
294 default:
295 gcc_unreachable ();
299 /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
300 resource set contains a volatile memory reference. Otherwise, return FALSE. */
302 static int
303 resource_conflicts_p (struct resources *res1, struct resources *res2)
305 if ((res1->cc && res2->cc) || (res1->memory && res2->memory)
306 || res1->volatil || res2->volatil)
307 return 1;
309 return hard_reg_set_intersect_p (res1->regs, res2->regs);
312 /* Return TRUE if any resource marked in RES, a `struct resources', is
313 referenced by INSN. If INCLUDE_DELAYED_EFFECTS is set, return if the called
314 routine is using those resources.
316 We compute this by computing all the resources referenced by INSN and
317 seeing if this conflicts with RES. It might be faster to directly check
318 ourselves, and this is the way it used to work, but it means duplicating
319 a large block of complex code. */
321 static int
322 insn_references_resource_p (rtx insn, struct resources *res,
323 bool include_delayed_effects)
325 struct resources insn_res;
327 CLEAR_RESOURCE (&insn_res);
328 mark_referenced_resources (insn, &insn_res, include_delayed_effects);
329 return resource_conflicts_p (&insn_res, res);
332 /* Return TRUE if INSN modifies resources that are marked in RES.
333 INCLUDE_DELAYED_EFFECTS is set if the actions of that routine should be
334 included. CC0 is only modified if it is explicitly set; see comments
335 in front of mark_set_resources for details. */
337 static int
338 insn_sets_resource_p (rtx insn, struct resources *res,
339 bool include_delayed_effects)
341 struct resources insn_sets;
343 CLEAR_RESOURCE (&insn_sets);
344 mark_set_resources (insn, &insn_sets, 0,
345 (include_delayed_effects
346 ? MARK_SRC_DEST_CALL
347 : MARK_SRC_DEST));
348 return resource_conflicts_p (&insn_sets, res);
351 /* Find a label at the end of the function or before a RETURN. If there
352 is none, try to make one. If that fails, returns 0.
354 The property of such a label is that it is placed just before the
355 epilogue or a bare RETURN insn, so that another bare RETURN can be
356 turned into a jump to the label unconditionally. In particular, the
357 label cannot be placed before a RETURN insn with a filled delay slot.
359 ??? There may be a problem with the current implementation. Suppose
360 we start with a bare RETURN insn and call find_end_label. It may set
361 function_return_label just before the RETURN. Suppose the machinery
362 is able to fill the delay slot of the RETURN insn afterwards. Then
363 function_return_label is no longer valid according to the property
364 described above and find_end_label will still return it unmodified.
365 Note that this is probably mitigated by the following observation:
366 once function_return_label is made, it is very likely the target of
367 a jump, so filling the delay slot of the RETURN will be much more
368 difficult.
369 KIND is either simple_return_rtx or ret_rtx, indicating which type of
370 return we're looking for. */
372 static rtx_code_label *
373 find_end_label (rtx kind)
375 rtx_insn *insn;
376 rtx_code_label **plabel;
378 if (kind == ret_rtx)
379 plabel = &function_return_label;
380 else
382 gcc_assert (kind == simple_return_rtx);
383 plabel = &function_simple_return_label;
386 /* If we found one previously, return it. */
387 if (*plabel)
388 return *plabel;
390 /* Otherwise, see if there is a label at the end of the function. If there
391 is, it must be that RETURN insns aren't needed, so that is our return
392 label and we don't have to do anything else. */
394 insn = get_last_insn ();
395 while (NOTE_P (insn)
396 || (NONJUMP_INSN_P (insn)
397 && (GET_CODE (PATTERN (insn)) == USE
398 || GET_CODE (PATTERN (insn)) == CLOBBER)))
399 insn = PREV_INSN (insn);
401 /* When a target threads its epilogue we might already have a
402 suitable return insn. If so put a label before it for the
403 function_return_label. */
404 if (BARRIER_P (insn)
405 && JUMP_P (PREV_INSN (insn))
406 && PATTERN (PREV_INSN (insn)) == kind)
408 rtx_insn *temp = PREV_INSN (PREV_INSN (insn));
409 rtx_code_label *label = gen_label_rtx ();
410 LABEL_NUSES (label) = 0;
412 /* Put the label before any USE insns that may precede the RETURN
413 insn. */
414 while (GET_CODE (temp) == USE)
415 temp = PREV_INSN (temp);
417 emit_label_after (label, temp);
418 *plabel = label;
421 else if (LABEL_P (insn))
422 *plabel = as_a <rtx_code_label *> (insn);
423 else
425 rtx_code_label *label = gen_label_rtx ();
426 LABEL_NUSES (label) = 0;
427 /* If the basic block reorder pass moves the return insn to
428 some other place try to locate it again and put our
429 function_return_label there. */
430 while (insn && ! (JUMP_P (insn) && (PATTERN (insn) == kind)))
431 insn = PREV_INSN (insn);
432 if (insn)
434 insn = PREV_INSN (insn);
436 /* Put the label before any USE insns that may precede the
437 RETURN insn. */
438 while (GET_CODE (insn) == USE)
439 insn = PREV_INSN (insn);
441 emit_label_after (label, insn);
443 else
445 if (targetm.have_epilogue () && ! targetm.have_return ())
446 /* The RETURN insn has its delay slot filled so we cannot
447 emit the label just before it. Since we already have
448 an epilogue and cannot emit a new RETURN, we cannot
449 emit the label at all. */
450 return NULL;
452 /* Otherwise, make a new label and emit a RETURN and BARRIER,
453 if needed. */
454 emit_label (label);
455 if (targetm.have_return ())
457 /* The return we make may have delay slots too. */
458 rtx_insn *pat = targetm.gen_return ();
459 rtx_insn *insn = emit_jump_insn (pat);
460 set_return_jump_label (insn);
461 emit_barrier ();
462 if (num_delay_slots (insn) > 0)
463 obstack_ptr_grow (&unfilled_slots_obstack, insn);
466 *plabel = label;
469 /* Show one additional use for this label so it won't go away until
470 we are done. */
471 ++LABEL_NUSES (*plabel);
473 return *plabel;
476 /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
477 the pattern of INSN with the SEQUENCE.
479 Returns the insn containing the SEQUENCE that replaces INSN. */
481 static rtx_insn *
482 emit_delay_sequence (rtx_insn *insn, const vec<rtx_insn *> &list, int length)
484 /* Allocate the rtvec to hold the insns and the SEQUENCE. */
485 rtvec seqv = rtvec_alloc (length + 1);
486 rtx seq = gen_rtx_SEQUENCE (VOIDmode, seqv);
487 rtx_insn *seq_insn = make_insn_raw (seq);
489 /* If DELAY_INSN has a location, use it for SEQ_INSN. If DELAY_INSN does
490 not have a location, but one of the delayed insns does, we pick up a
491 location from there later. */
492 INSN_LOCATION (seq_insn) = INSN_LOCATION (insn);
494 /* Unlink INSN from the insn chain, so that we can put it into
495 the SEQUENCE. Remember where we want to emit SEQUENCE in AFTER. */
496 rtx_insn *after = PREV_INSN (insn);
497 remove_insn (insn);
498 SET_NEXT_INSN (insn) = SET_PREV_INSN (insn) = NULL;
500 /* Build our SEQUENCE and rebuild the insn chain. */
501 start_sequence ();
502 XVECEXP (seq, 0, 0) = emit_insn (insn);
504 unsigned int delay_insns = list.length ();
505 gcc_assert (delay_insns == (unsigned int) length);
506 for (unsigned int i = 0; i < delay_insns; i++)
508 rtx_insn *tem = list[i];
509 rtx note, next;
511 /* Show that this copy of the insn isn't deleted. */
512 tem->set_undeleted ();
514 /* Unlink insn from its original place, and re-emit it into
515 the sequence. */
516 SET_NEXT_INSN (tem) = SET_PREV_INSN (tem) = NULL;
517 XVECEXP (seq, 0, i + 1) = emit_insn (tem);
519 /* SPARC assembler, for instance, emit warning when debug info is output
520 into the delay slot. */
521 if (INSN_LOCATION (tem) && !INSN_LOCATION (seq_insn))
522 INSN_LOCATION (seq_insn) = INSN_LOCATION (tem);
523 INSN_LOCATION (tem) = 0;
525 for (note = REG_NOTES (tem); note; note = next)
527 next = XEXP (note, 1);
528 switch (REG_NOTE_KIND (note))
530 case REG_DEAD:
531 /* Remove any REG_DEAD notes because we can't rely on them now
532 that the insn has been moved. */
533 remove_note (tem, note);
534 break;
536 case REG_LABEL_OPERAND:
537 case REG_LABEL_TARGET:
538 /* Keep the label reference count up to date. */
539 if (LABEL_P (XEXP (note, 0)))
540 LABEL_NUSES (XEXP (note, 0)) ++;
541 break;
543 default:
544 break;
548 end_sequence ();
550 /* Splice our SEQUENCE into the insn stream where INSN used to be. */
551 add_insn_after (seq_insn, after, NULL);
553 return seq_insn;
556 /* Add INSN to DELAY_LIST and return the head of the new list. The list must
557 be in the order in which the insns are to be executed. */
559 static void
560 add_to_delay_list (rtx_insn *insn, vec<rtx_insn *> *delay_list)
562 /* If INSN has its block number recorded, clear it since we may
563 be moving the insn to a new block. */
564 clear_hashed_info_for_insn (insn);
565 delay_list->safe_push (insn);
568 /* Delete INSN from the delay slot of the insn that it is in, which may
569 produce an insn with no delay slots. Return the new insn. */
571 static rtx_insn *
572 delete_from_delay_slot (rtx_insn *insn)
574 rtx_insn *trial, *seq_insn, *prev;
575 rtx_sequence *seq;
576 int i;
577 int had_barrier = 0;
579 /* We first must find the insn containing the SEQUENCE with INSN in its
580 delay slot. Do this by finding an insn, TRIAL, where
581 PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
583 for (trial = insn;
584 PREV_INSN (NEXT_INSN (trial)) == trial;
585 trial = NEXT_INSN (trial))
588 seq_insn = PREV_INSN (NEXT_INSN (trial));
589 seq = as_a <rtx_sequence *> (PATTERN (seq_insn));
591 if (NEXT_INSN (seq_insn) && BARRIER_P (NEXT_INSN (seq_insn)))
592 had_barrier = 1;
594 /* Create a delay list consisting of all the insns other than the one
595 we are deleting (unless we were the only one). */
596 auto_vec<rtx_insn *, 5> delay_list;
597 if (seq->len () > 2)
598 for (i = 1; i < seq->len (); i++)
599 if (seq->insn (i) != insn)
600 add_to_delay_list (seq->insn (i), &delay_list);
602 /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
603 list, and rebuild the delay list if non-empty. */
604 prev = PREV_INSN (seq_insn);
605 trial = seq->insn (0);
606 delete_related_insns (seq_insn);
607 add_insn_after (trial, prev, NULL);
609 /* If there was a barrier after the old SEQUENCE, remit it. */
610 if (had_barrier)
611 emit_barrier_after (trial);
613 /* If there are any delay insns, remit them. Otherwise clear the
614 annul flag. */
615 if (!delay_list.is_empty ())
616 trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2);
617 else if (JUMP_P (trial))
618 INSN_ANNULLED_BRANCH_P (trial) = 0;
620 INSN_FROM_TARGET_P (insn) = 0;
622 /* Show we need to fill this insn again. */
623 obstack_ptr_grow (&unfilled_slots_obstack, trial);
625 return trial;
628 /* Delete INSN, a JUMP_INSN. If it is a conditional jump, we must track down
629 the insn that sets CC0 for it and delete it too. */
631 static void
632 delete_scheduled_jump (rtx_insn *insn)
634 /* Delete the insn that sets cc0 for us. On machines without cc0, we could
635 delete the insn that sets the condition code, but it is hard to find it.
636 Since this case is rare anyway, don't bother trying; there would likely
637 be other insns that became dead anyway, which we wouldn't know to
638 delete. */
640 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, insn))
642 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
644 /* If a reg-note was found, it points to an insn to set CC0. This
645 insn is in the delay list of some other insn. So delete it from
646 the delay list it was in. */
647 if (note)
649 if (! FIND_REG_INC_NOTE (XEXP (note, 0), NULL_RTX)
650 && sets_cc0_p (PATTERN (XEXP (note, 0))) == 1)
651 delete_from_delay_slot (as_a <rtx_insn *> (XEXP (note, 0)));
653 else
655 /* The insn setting CC0 is our previous insn, but it may be in
656 a delay slot. It will be the last insn in the delay slot, if
657 it is. */
658 rtx_insn *trial = previous_insn (insn);
659 if (NOTE_P (trial))
660 trial = prev_nonnote_insn (trial);
661 if (sets_cc0_p (PATTERN (trial)) != 1
662 || FIND_REG_INC_NOTE (trial, NULL_RTX))
663 return;
664 if (PREV_INSN (NEXT_INSN (trial)) == trial)
665 delete_related_insns (trial);
666 else
667 delete_from_delay_slot (trial);
671 delete_related_insns (insn);
674 /* Counters for delay-slot filling. */
676 #define NUM_REORG_FUNCTIONS 2
677 #define MAX_DELAY_HISTOGRAM 3
678 #define MAX_REORG_PASSES 2
680 static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
682 static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
684 static int reorg_pass_number;
686 static void
687 note_delay_statistics (int slots_filled, int index)
689 num_insns_needing_delays[index][reorg_pass_number]++;
690 if (slots_filled > MAX_DELAY_HISTOGRAM)
691 slots_filled = MAX_DELAY_HISTOGRAM;
692 num_filled_delays[index][slots_filled][reorg_pass_number]++;
695 /* Optimize the following cases:
697 1. When a conditional branch skips over only one instruction,
698 use an annulling branch and put that insn in the delay slot.
699 Use either a branch that annuls when the condition if true or
700 invert the test with a branch that annuls when the condition is
701 false. This saves insns, since otherwise we must copy an insn
702 from the L1 target.
704 (orig) (skip) (otherwise)
705 Bcc.n L1 Bcc',a L1 Bcc,a L1'
706 insn insn insn2
707 L1: L1: L1:
708 insn2 insn2 insn2
709 insn3 insn3 L1':
710 insn3
712 2. When a conditional branch skips over only one instruction,
713 and after that, it unconditionally branches somewhere else,
714 perform the similar optimization. This saves executing the
715 second branch in the case where the inverted condition is true.
717 Bcc.n L1 Bcc',a L2
718 insn insn
719 L1: L1:
720 Bra L2 Bra L2
722 INSN is a JUMP_INSN.
724 This should be expanded to skip over N insns, where N is the number
725 of delay slots required. */
727 static void
728 optimize_skip (rtx_jump_insn *insn, vec<rtx_insn *> *delay_list)
730 rtx_insn *trial = next_nonnote_insn (insn);
731 rtx_insn *next_trial = next_active_insn (trial);
732 int flags;
734 flags = get_jump_flags (insn, JUMP_LABEL (insn));
736 if (trial == 0
737 || !NONJUMP_INSN_P (trial)
738 || GET_CODE (PATTERN (trial)) == SEQUENCE
739 || recog_memoized (trial) < 0
740 || (! eligible_for_annul_false (insn, 0, trial, flags)
741 && ! eligible_for_annul_true (insn, 0, trial, flags))
742 || can_throw_internal (trial))
743 return;
745 /* There are two cases where we are just executing one insn (we assume
746 here that a branch requires only one insn; this should be generalized
747 at some point): Where the branch goes around a single insn or where
748 we have one insn followed by a branch to the same label we branch to.
749 In both of these cases, inverting the jump and annulling the delay
750 slot give the same effect in fewer insns. */
751 if (next_trial == next_active_insn (JUMP_LABEL (insn))
752 || (next_trial != 0
753 && simplejump_or_return_p (next_trial)
754 && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)))
756 if (eligible_for_annul_false (insn, 0, trial, flags))
758 if (invert_jump (insn, JUMP_LABEL (insn), 1))
759 INSN_FROM_TARGET_P (trial) = 1;
760 else if (! eligible_for_annul_true (insn, 0, trial, flags))
761 return;
764 add_to_delay_list (trial, delay_list);
765 next_trial = next_active_insn (trial);
766 update_block (trial, trial);
767 delete_related_insns (trial);
769 /* Also, if we are targeting an unconditional
770 branch, thread our jump to the target of that branch. Don't
771 change this into a RETURN here, because it may not accept what
772 we have in the delay slot. We'll fix this up later. */
773 if (next_trial && simplejump_or_return_p (next_trial))
775 rtx target_label = JUMP_LABEL (next_trial);
776 if (ANY_RETURN_P (target_label))
777 target_label = find_end_label (target_label);
779 if (target_label)
781 /* Recompute the flags based on TARGET_LABEL since threading
782 the jump to TARGET_LABEL may change the direction of the
783 jump (which may change the circumstances in which the
784 delay slot is nullified). */
785 flags = get_jump_flags (insn, target_label);
786 if (eligible_for_annul_true (insn, 0, trial, flags))
787 reorg_redirect_jump (insn, target_label);
791 INSN_ANNULLED_BRANCH_P (insn) = 1;
795 /* Encode and return branch direction and prediction information for
796 INSN assuming it will jump to LABEL.
798 Non conditional branches return no direction information and
799 are predicted as very likely taken. */
801 static int
802 get_jump_flags (const rtx_insn *insn, rtx label)
804 int flags;
806 /* get_jump_flags can be passed any insn with delay slots, these may
807 be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
808 direction information, and only if they are conditional jumps.
810 If LABEL is a return, then there is no way to determine the branch
811 direction. */
812 if (JUMP_P (insn)
813 && (condjump_p (insn) || condjump_in_parallel_p (insn))
814 && !ANY_RETURN_P (label)
815 && INSN_UID (insn) <= max_uid
816 && INSN_UID (label) <= max_uid)
817 flags
818 = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
819 ? ATTR_FLAG_forward : ATTR_FLAG_backward;
820 /* No valid direction information. */
821 else
822 flags = 0;
824 return flags;
827 /* Return truth value of the statement that this branch
828 is mostly taken. If we think that the branch is extremely likely
829 to be taken, we return 2. If the branch is slightly more likely to be
830 taken, return 1. If the branch is slightly less likely to be taken,
831 return 0 and if the branch is highly unlikely to be taken, return -1. */
833 static int
834 mostly_true_jump (rtx jump_insn)
836 /* If branch probabilities are available, then use that number since it
837 always gives a correct answer. */
838 rtx note = find_reg_note (jump_insn, REG_BR_PROB, 0);
839 if (note)
841 int prob = XINT (note, 0);
843 if (prob >= REG_BR_PROB_BASE * 9 / 10)
844 return 2;
845 else if (prob >= REG_BR_PROB_BASE / 2)
846 return 1;
847 else if (prob >= REG_BR_PROB_BASE / 10)
848 return 0;
849 else
850 return -1;
853 /* If there is no note, assume branches are not taken.
854 This should be rare. */
855 return 0;
858 /* Return the condition under which INSN will branch to TARGET. If TARGET
859 is zero, return the condition under which INSN will return. If INSN is
860 an unconditional branch, return const_true_rtx. If INSN isn't a simple
861 type of jump, or it doesn't go to TARGET, return 0. */
863 static rtx
864 get_branch_condition (const rtx_insn *insn, rtx target)
866 rtx pat = PATTERN (insn);
867 rtx src;
869 if (condjump_in_parallel_p (insn))
870 pat = XVECEXP (pat, 0, 0);
872 if (ANY_RETURN_P (pat) && pat == target)
873 return const_true_rtx;
875 if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
876 return 0;
878 src = SET_SRC (pat);
879 if (GET_CODE (src) == LABEL_REF && LABEL_REF_LABEL (src) == target)
880 return const_true_rtx;
882 else if (GET_CODE (src) == IF_THEN_ELSE
883 && XEXP (src, 2) == pc_rtx
884 && ((GET_CODE (XEXP (src, 1)) == LABEL_REF
885 && LABEL_REF_LABEL (XEXP (src, 1)) == target)
886 || (ANY_RETURN_P (XEXP (src, 1)) && XEXP (src, 1) == target)))
887 return XEXP (src, 0);
889 else if (GET_CODE (src) == IF_THEN_ELSE
890 && XEXP (src, 1) == pc_rtx
891 && ((GET_CODE (XEXP (src, 2)) == LABEL_REF
892 && LABEL_REF_LABEL (XEXP (src, 2)) == target)
893 || (ANY_RETURN_P (XEXP (src, 2)) && XEXP (src, 2) == target)))
895 enum rtx_code rev;
896 rev = reversed_comparison_code (XEXP (src, 0), insn);
897 if (rev != UNKNOWN)
898 return gen_rtx_fmt_ee (rev, GET_MODE (XEXP (src, 0)),
899 XEXP (XEXP (src, 0), 0),
900 XEXP (XEXP (src, 0), 1));
903 return 0;
906 /* Return nonzero if CONDITION is more strict than the condition of
907 INSN, i.e., if INSN will always branch if CONDITION is true. */
909 static int
910 condition_dominates_p (rtx condition, const rtx_insn *insn)
912 rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
913 enum rtx_code code = GET_CODE (condition);
914 enum rtx_code other_code;
916 if (rtx_equal_p (condition, other_condition)
917 || other_condition == const_true_rtx)
918 return 1;
920 else if (condition == const_true_rtx || other_condition == 0)
921 return 0;
923 other_code = GET_CODE (other_condition);
924 if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
925 || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
926 || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
927 return 0;
929 return comparison_dominates_p (code, other_code);
932 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
933 any insns already in the delay slot of JUMP. */
935 static int
936 redirect_with_delay_slots_safe_p (rtx_insn *jump, rtx newlabel, rtx seq)
938 int flags, i;
939 rtx_sequence *pat = as_a <rtx_sequence *> (PATTERN (seq));
941 /* Make sure all the delay slots of this jump would still
942 be valid after threading the jump. If they are still
943 valid, then return nonzero. */
945 flags = get_jump_flags (jump, newlabel);
946 for (i = 1; i < pat->len (); i++)
947 if (! (
948 #if ANNUL_IFFALSE_SLOTS
949 (INSN_ANNULLED_BRANCH_P (jump)
950 && INSN_FROM_TARGET_P (pat->insn (i)))
951 ? eligible_for_annul_false (jump, i - 1, pat->insn (i), flags) :
952 #endif
953 #if ANNUL_IFTRUE_SLOTS
954 (INSN_ANNULLED_BRANCH_P (jump)
955 && ! INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)))
956 ? eligible_for_annul_true (jump, i - 1, pat->insn (i), flags) :
957 #endif
958 eligible_for_delay (jump, i - 1, pat->insn (i), flags)))
959 break;
961 return (i == pat->len ());
964 /* Return nonzero if redirecting JUMP to NEWLABEL does not invalidate
965 any insns we wish to place in the delay slot of JUMP. */
967 static int
968 redirect_with_delay_list_safe_p (rtx_insn *jump, rtx newlabel,
969 const vec<rtx_insn *> &delay_list)
971 /* Make sure all the insns in DELAY_LIST would still be
972 valid after threading the jump. If they are still
973 valid, then return nonzero. */
975 int flags = get_jump_flags (jump, newlabel);
976 unsigned int delay_insns = delay_list.length ();
977 unsigned int i = 0;
978 for (; i < delay_insns; i++)
979 if (! (
980 #if ANNUL_IFFALSE_SLOTS
981 (INSN_ANNULLED_BRANCH_P (jump)
982 && INSN_FROM_TARGET_P (delay_list[i]))
983 ? eligible_for_annul_false (jump, i, delay_list[i], flags) :
984 #endif
985 #if ANNUL_IFTRUE_SLOTS
986 (INSN_ANNULLED_BRANCH_P (jump)
987 && ! INSN_FROM_TARGET_P (delay_list[i]))
988 ? eligible_for_annul_true (jump, i, delay_list[i], flags) :
989 #endif
990 eligible_for_delay (jump, i, delay_list[i], flags)))
991 break;
993 return i == delay_insns;
996 /* DELAY_LIST is a list of insns that have already been placed into delay
997 slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
998 If not, return 0; otherwise return 1. */
1000 static int
1001 check_annul_list_true_false (int annul_true_p,
1002 const vec<rtx_insn *> &delay_list)
1004 rtx_insn *trial;
1005 unsigned int i;
1006 FOR_EACH_VEC_ELT (delay_list, i, trial)
1007 if ((annul_true_p && INSN_FROM_TARGET_P (trial))
1008 || (!annul_true_p && !INSN_FROM_TARGET_P (trial)))
1009 return 0;
1011 return 1;
1014 /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
1015 the condition tested by INSN is CONDITION and the resources shown in
1016 OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
1017 from SEQ's delay list, in addition to whatever insns it may execute
1018 (in DELAY_LIST). SETS and NEEDED are denote resources already set and
1019 needed while searching for delay slot insns. Return the concatenated
1020 delay list if possible, otherwise, return 0.
1022 SLOTS_TO_FILL is the total number of slots required by INSN, and
1023 PSLOTS_FILLED points to the number filled so far (also the number of
1024 insns in DELAY_LIST). It is updated with the number that have been
1025 filled from the SEQUENCE, if any.
1027 PANNUL_P points to a nonzero value if we already know that we need
1028 to annul INSN. If this routine determines that annulling is needed,
1029 it may set that value nonzero.
1031 PNEW_THREAD points to a location that is to receive the place at which
1032 execution should continue. */
1034 static void
1035 steal_delay_list_from_target (rtx_insn *insn, rtx condition, rtx_sequence *seq,
1036 vec<rtx_insn *> *delay_list, resources *sets,
1037 struct resources *needed,
1038 struct resources *other_needed,
1039 int slots_to_fill, int *pslots_filled,
1040 int *pannul_p, rtx *pnew_thread)
1042 int slots_remaining = slots_to_fill - *pslots_filled;
1043 int total_slots_filled = *pslots_filled;
1044 auto_vec<rtx_insn *, 5> new_delay_list;
1045 int must_annul = *pannul_p;
1046 int used_annul = 0;
1047 int i;
1048 struct resources cc_set;
1049 bool *redundant;
1051 /* We can't do anything if there are more delay slots in SEQ than we
1052 can handle, or if we don't know that it will be a taken branch.
1053 We know that it will be a taken branch if it is either an unconditional
1054 branch or a conditional branch with a stricter branch condition.
1056 Also, exit if the branch has more than one set, since then it is computing
1057 other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1058 ??? It may be possible to move other sets into INSN in addition to
1059 moving the instructions in the delay slots.
1061 We can not steal the delay list if one of the instructions in the
1062 current delay_list modifies the condition codes and the jump in the
1063 sequence is a conditional jump. We can not do this because we can
1064 not change the direction of the jump because the condition codes
1065 will effect the direction of the jump in the sequence. */
1067 CLEAR_RESOURCE (&cc_set);
1069 rtx_insn *trial;
1070 FOR_EACH_VEC_ELT (*delay_list, i, trial)
1072 mark_set_resources (trial, &cc_set, 0, MARK_SRC_DEST_CALL);
1073 if (insn_references_resource_p (seq->insn (0), &cc_set, false))
1074 return;
1077 if (XVECLEN (seq, 0) - 1 > slots_remaining
1078 || ! condition_dominates_p (condition, seq->insn (0))
1079 || ! single_set (seq->insn (0)))
1080 return;
1082 /* On some targets, branches with delay slots can have a limited
1083 displacement. Give the back end a chance to tell us we can't do
1084 this. */
1085 if (! targetm.can_follow_jump (insn, seq->insn (0)))
1086 return;
1088 redundant = XALLOCAVEC (bool, XVECLEN (seq, 0));
1089 for (i = 1; i < seq->len (); i++)
1091 rtx_insn *trial = seq->insn (i);
1092 int flags;
1094 if (insn_references_resource_p (trial, sets, false)
1095 || insn_sets_resource_p (trial, needed, false)
1096 || insn_sets_resource_p (trial, sets, false)
1097 /* If TRIAL sets CC0, we can't copy it, so we can't steal this
1098 delay list. */
1099 || (HAVE_cc0 && find_reg_note (trial, REG_CC_USER, NULL_RTX))
1100 /* If TRIAL is from the fallthrough code of an annulled branch insn
1101 in SEQ, we cannot use it. */
1102 || (INSN_ANNULLED_BRANCH_P (seq->insn (0))
1103 && ! INSN_FROM_TARGET_P (trial)))
1104 return;
1106 /* If this insn was already done (usually in a previous delay slot),
1107 pretend we put it in our delay slot. */
1108 redundant[i] = redundant_insn (trial, insn, new_delay_list);
1109 if (redundant[i])
1110 continue;
1112 /* We will end up re-vectoring this branch, so compute flags
1113 based on jumping to the new label. */
1114 flags = get_jump_flags (insn, JUMP_LABEL (seq->insn (0)));
1116 if (! must_annul
1117 && ((condition == const_true_rtx
1118 || (! insn_sets_resource_p (trial, other_needed, false)
1119 && ! may_trap_or_fault_p (PATTERN (trial)))))
1120 ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1121 : (must_annul || (delay_list->is_empty () && new_delay_list.is_empty ()))
1122 && (must_annul = 1,
1123 check_annul_list_true_false (0, *delay_list)
1124 && check_annul_list_true_false (0, new_delay_list)
1125 && eligible_for_annul_false (insn, total_slots_filled,
1126 trial, flags)))
1128 if (must_annul)
1129 used_annul = 1;
1130 rtx_insn *temp = copy_delay_slot_insn (trial);
1131 INSN_FROM_TARGET_P (temp) = 1;
1132 add_to_delay_list (temp, &new_delay_list);
1133 total_slots_filled++;
1135 if (--slots_remaining == 0)
1136 break;
1138 else
1139 return;
1142 /* Record the effect of the instructions that were redundant and which
1143 we therefore decided not to copy. */
1144 for (i = 1; i < seq->len (); i++)
1145 if (redundant[i])
1146 update_block (seq->insn (i), insn);
1148 /* Show the place to which we will be branching. */
1149 *pnew_thread = first_active_target_insn (JUMP_LABEL (seq->insn (0)));
1151 /* Add any new insns to the delay list and update the count of the
1152 number of slots filled. */
1153 *pslots_filled = total_slots_filled;
1154 if (used_annul)
1155 *pannul_p = 1;
1157 rtx_insn *temp;
1158 FOR_EACH_VEC_ELT (new_delay_list, i, temp)
1159 add_to_delay_list (temp, delay_list);
1162 /* Similar to steal_delay_list_from_target except that SEQ is on the
1163 fallthrough path of INSN. Here we only do something if the delay insn
1164 of SEQ is an unconditional branch. In that case we steal its delay slot
1165 for INSN since unconditional branches are much easier to fill. */
1167 static void
1168 steal_delay_list_from_fallthrough (rtx_insn *insn, rtx condition,
1169 rtx_sequence *seq,
1170 vec<rtx_insn *> *delay_list,
1171 struct resources *sets,
1172 struct resources *needed,
1173 struct resources *other_needed,
1174 int slots_to_fill, int *pslots_filled,
1175 int *pannul_p)
1177 int i;
1178 int flags;
1179 int must_annul = *pannul_p;
1180 int used_annul = 0;
1182 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1184 /* We can't do anything if SEQ's delay insn isn't an
1185 unconditional branch. */
1187 if (! simplejump_or_return_p (seq->insn (0)))
1188 return;
1190 for (i = 1; i < seq->len (); i++)
1192 rtx_insn *trial = seq->insn (i);
1194 /* If TRIAL sets CC0, stealing it will move it too far from the use
1195 of CC0. */
1196 if (insn_references_resource_p (trial, sets, false)
1197 || insn_sets_resource_p (trial, needed, false)
1198 || insn_sets_resource_p (trial, sets, false)
1199 || (HAVE_cc0 && sets_cc0_p (PATTERN (trial))))
1201 break;
1203 /* If this insn was already done, we don't need it. */
1204 if (redundant_insn (trial, insn, *delay_list))
1206 update_block (trial, insn);
1207 delete_from_delay_slot (trial);
1208 continue;
1211 if (! must_annul
1212 && ((condition == const_true_rtx
1213 || (! insn_sets_resource_p (trial, other_needed, false)
1214 && ! may_trap_or_fault_p (PATTERN (trial)))))
1215 ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1216 : (must_annul || delay_list->is_empty ()) && (must_annul = 1,
1217 check_annul_list_true_false (1, *delay_list)
1218 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1220 if (must_annul)
1221 used_annul = 1;
1222 delete_from_delay_slot (trial);
1223 add_to_delay_list (trial, delay_list);
1225 if (++(*pslots_filled) == slots_to_fill)
1226 break;
1228 else
1229 break;
1232 if (used_annul)
1233 *pannul_p = 1;
1236 /* Try merging insns starting at THREAD which match exactly the insns in
1237 INSN's delay list.
1239 If all insns were matched and the insn was previously annulling, the
1240 annul bit will be cleared.
1242 For each insn that is merged, if the branch is or will be non-annulling,
1243 we delete the merged insn. */
1245 static void
1246 try_merge_delay_insns (rtx_insn *insn, rtx_insn *thread)
1248 rtx_insn *trial, *next_trial;
1249 rtx_insn *delay_insn = as_a <rtx_insn *> (XVECEXP (PATTERN (insn), 0, 0));
1250 int annul_p = JUMP_P (delay_insn) && INSN_ANNULLED_BRANCH_P (delay_insn);
1251 int slot_number = 1;
1252 int num_slots = XVECLEN (PATTERN (insn), 0);
1253 rtx next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1254 struct resources set, needed, modified;
1255 rtx_insn_list *merged_insns = 0;
1256 int i, j;
1257 int flags;
1259 flags = get_jump_flags (delay_insn, JUMP_LABEL (delay_insn));
1261 CLEAR_RESOURCE (&needed);
1262 CLEAR_RESOURCE (&set);
1264 /* If this is not an annulling branch, take into account anything needed in
1265 INSN's delay slot. This prevents two increments from being incorrectly
1266 folded into one. If we are annulling, this would be the correct
1267 thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1268 will essentially disable this optimization. This method is somewhat of
1269 a kludge, but I don't see a better way.) */
1270 if (! annul_p)
1271 for (i = 1 ; i < num_slots; i++)
1272 if (XVECEXP (PATTERN (insn), 0, i))
1273 mark_referenced_resources (XVECEXP (PATTERN (insn), 0, i), &needed,
1274 true);
1276 for (trial = thread; !stop_search_p (trial, 1); trial = next_trial)
1278 rtx pat = PATTERN (trial);
1279 rtx oldtrial = trial;
1281 next_trial = next_nonnote_insn (trial);
1283 /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1284 if (NONJUMP_INSN_P (trial)
1285 && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1286 continue;
1288 if (GET_CODE (next_to_match) == GET_CODE (trial)
1289 /* We can't share an insn that sets cc0. */
1290 && (!HAVE_cc0 || ! sets_cc0_p (pat))
1291 && ! insn_references_resource_p (trial, &set, true)
1292 && ! insn_sets_resource_p (trial, &set, true)
1293 && ! insn_sets_resource_p (trial, &needed, true)
1294 && (trial = try_split (pat, trial, 0)) != 0
1295 /* Update next_trial, in case try_split succeeded. */
1296 && (next_trial = next_nonnote_insn (trial))
1297 /* Likewise THREAD. */
1298 && (thread = oldtrial == thread ? trial : thread)
1299 && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1300 /* Have to test this condition if annul condition is different
1301 from (and less restrictive than) non-annulling one. */
1302 && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1305 if (! annul_p)
1307 update_block (trial, thread);
1308 if (trial == thread)
1309 thread = next_active_insn (thread);
1311 delete_related_insns (trial);
1312 INSN_FROM_TARGET_P (next_to_match) = 0;
1314 else
1315 merged_insns = gen_rtx_INSN_LIST (VOIDmode, trial, merged_insns);
1317 if (++slot_number == num_slots)
1318 break;
1320 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1323 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1324 mark_referenced_resources (trial, &needed, true);
1327 /* See if we stopped on a filled insn. If we did, try to see if its
1328 delay slots match. */
1329 if (slot_number != num_slots
1330 && trial && NONJUMP_INSN_P (trial)
1331 && GET_CODE (PATTERN (trial)) == SEQUENCE
1332 && !(JUMP_P (XVECEXP (PATTERN (trial), 0, 0))
1333 && INSN_ANNULLED_BRANCH_P (XVECEXP (PATTERN (trial), 0, 0))))
1335 rtx_sequence *pat = as_a <rtx_sequence *> (PATTERN (trial));
1336 rtx filled_insn = XVECEXP (pat, 0, 0);
1338 /* Account for resources set/needed by the filled insn. */
1339 mark_set_resources (filled_insn, &set, 0, MARK_SRC_DEST_CALL);
1340 mark_referenced_resources (filled_insn, &needed, true);
1342 for (i = 1; i < pat->len (); i++)
1344 rtx_insn *dtrial = pat->insn (i);
1346 CLEAR_RESOURCE (&modified);
1347 /* Account for resources set by the insn following NEXT_TO_MATCH
1348 inside INSN's delay list. */
1349 for (j = 1; slot_number + j < num_slots; j++)
1350 mark_set_resources (XVECEXP (PATTERN (insn), 0, slot_number + j),
1351 &modified, 0, MARK_SRC_DEST_CALL);
1352 /* Account for resources set by the insn before DTRIAL and inside
1353 TRIAL's delay list. */
1354 for (j = 1; j < i; j++)
1355 mark_set_resources (XVECEXP (pat, 0, j),
1356 &modified, 0, MARK_SRC_DEST_CALL);
1357 if (! insn_references_resource_p (dtrial, &set, true)
1358 && ! insn_sets_resource_p (dtrial, &set, true)
1359 && ! insn_sets_resource_p (dtrial, &needed, true)
1360 && (!HAVE_cc0 || ! sets_cc0_p (PATTERN (dtrial)))
1361 && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1362 /* Check that DTRIAL and NEXT_TO_MATCH does not reference a
1363 resource modified between them (only dtrial is checked because
1364 next_to_match and dtrial shall to be equal in order to hit
1365 this line) */
1366 && ! insn_references_resource_p (dtrial, &modified, true)
1367 && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1369 if (! annul_p)
1371 rtx_insn *new_rtx;
1373 update_block (dtrial, thread);
1374 new_rtx = delete_from_delay_slot (dtrial);
1375 if (thread->deleted ())
1376 thread = new_rtx;
1377 INSN_FROM_TARGET_P (next_to_match) = 0;
1379 else
1380 merged_insns = gen_rtx_INSN_LIST (SImode, dtrial,
1381 merged_insns);
1383 if (++slot_number == num_slots)
1384 break;
1386 next_to_match = XVECEXP (PATTERN (insn), 0, slot_number);
1388 else
1390 /* Keep track of the set/referenced resources for the delay
1391 slots of any trial insns we encounter. */
1392 mark_set_resources (dtrial, &set, 0, MARK_SRC_DEST_CALL);
1393 mark_referenced_resources (dtrial, &needed, true);
1398 /* If all insns in the delay slot have been matched and we were previously
1399 annulling the branch, we need not any more. In that case delete all the
1400 merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1401 the delay list so that we know that it isn't only being used at the
1402 target. */
1403 if (slot_number == num_slots && annul_p)
1405 for (; merged_insns; merged_insns = merged_insns->next ())
1407 if (GET_MODE (merged_insns) == SImode)
1409 rtx_insn *new_rtx;
1411 update_block (merged_insns->insn (), thread);
1412 new_rtx = delete_from_delay_slot (merged_insns->insn ());
1413 if (thread->deleted ())
1414 thread = new_rtx;
1416 else
1418 update_block (merged_insns->insn (), thread);
1419 delete_related_insns (merged_insns->insn ());
1423 INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1425 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1426 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i)) = 0;
1430 /* See if INSN is redundant with an insn in front of TARGET. Often this
1431 is called when INSN is a candidate for a delay slot of TARGET.
1432 DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1433 of INSN. Often INSN will be redundant with an insn in a delay slot of
1434 some previous insn. This happens when we have a series of branches to the
1435 same label; in that case the first insn at the target might want to go
1436 into each of the delay slots.
1438 If we are not careful, this routine can take up a significant fraction
1439 of the total compilation time (4%), but only wins rarely. Hence we
1440 speed this routine up by making two passes. The first pass goes back
1441 until it hits a label and sees if it finds an insn with an identical
1442 pattern. Only in this (relatively rare) event does it check for
1443 data conflicts.
1445 We do not split insns we encounter. This could cause us not to find a
1446 redundant insn, but the cost of splitting seems greater than the possible
1447 gain in rare cases. */
1449 static rtx
1450 redundant_insn (rtx insn, rtx_insn *target, const vec<rtx_insn *> &delay_list)
1452 rtx target_main = target;
1453 rtx ipat = PATTERN (insn);
1454 rtx_insn *trial;
1455 rtx pat;
1456 struct resources needed, set;
1457 int i;
1458 unsigned insns_to_search;
1460 /* If INSN has any REG_UNUSED notes, it can't match anything since we
1461 are allowed to not actually assign to such a register. */
1462 if (find_reg_note (insn, REG_UNUSED, NULL_RTX) != 0)
1463 return 0;
1465 /* Scan backwards looking for a match. */
1466 for (trial = PREV_INSN (target),
1467 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1468 trial && insns_to_search > 0;
1469 trial = PREV_INSN (trial))
1471 /* (use (insn))s can come immediately after a barrier if the
1472 label that used to precede them has been deleted as dead.
1473 See delete_related_insns. */
1474 if (LABEL_P (trial) || BARRIER_P (trial))
1475 return 0;
1477 if (!INSN_P (trial))
1478 continue;
1479 --insns_to_search;
1481 pat = PATTERN (trial);
1482 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1483 continue;
1485 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (pat))
1487 /* Stop for a CALL and its delay slots because it is difficult to
1488 track its resource needs correctly. */
1489 if (CALL_P (seq->element (0)))
1490 return 0;
1492 /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1493 slots because it is difficult to track its resource needs
1494 correctly. */
1496 if (INSN_SETS_ARE_DELAYED (seq->insn (0)))
1497 return 0;
1499 if (INSN_REFERENCES_ARE_DELAYED (seq->insn (0)))
1500 return 0;
1502 /* See if any of the insns in the delay slot match, updating
1503 resource requirements as we go. */
1504 for (i = seq->len () - 1; i > 0; i--)
1505 if (GET_CODE (seq->element (i)) == GET_CODE (insn)
1506 && rtx_equal_p (PATTERN (seq->element (i)), ipat)
1507 && ! find_reg_note (seq->element (i), REG_UNUSED, NULL_RTX))
1508 break;
1510 /* If found a match, exit this loop early. */
1511 if (i > 0)
1512 break;
1515 else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat)
1516 && ! find_reg_note (trial, REG_UNUSED, NULL_RTX))
1517 break;
1520 /* If we didn't find an insn that matches, return 0. */
1521 if (trial == 0)
1522 return 0;
1524 /* See what resources this insn sets and needs. If they overlap, or
1525 if this insn references CC0, it can't be redundant. */
1527 CLEAR_RESOURCE (&needed);
1528 CLEAR_RESOURCE (&set);
1529 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1530 mark_referenced_resources (insn, &needed, true);
1532 /* If TARGET is a SEQUENCE, get the main insn. */
1533 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1534 target_main = XVECEXP (PATTERN (target), 0, 0);
1536 if (resource_conflicts_p (&needed, &set)
1537 || (HAVE_cc0 && reg_mentioned_p (cc0_rtx, ipat))
1538 /* The insn requiring the delay may not set anything needed or set by
1539 INSN. */
1540 || insn_sets_resource_p (target_main, &needed, true)
1541 || insn_sets_resource_p (target_main, &set, true))
1542 return 0;
1544 /* Insns we pass may not set either NEEDED or SET, so merge them for
1545 simpler tests. */
1546 needed.memory |= set.memory;
1547 IOR_HARD_REG_SET (needed.regs, set.regs);
1549 /* This insn isn't redundant if it conflicts with an insn that either is
1550 or will be in a delay slot of TARGET. */
1552 unsigned int j;
1553 rtx_insn *temp;
1554 FOR_EACH_VEC_ELT (delay_list, j, temp)
1555 if (insn_sets_resource_p (temp, &needed, true))
1556 return 0;
1558 if (NONJUMP_INSN_P (target) && GET_CODE (PATTERN (target)) == SEQUENCE)
1559 for (i = 1; i < XVECLEN (PATTERN (target), 0); i++)
1560 if (insn_sets_resource_p (XVECEXP (PATTERN (target), 0, i), &needed,
1561 true))
1562 return 0;
1564 /* Scan backwards until we reach a label or an insn that uses something
1565 INSN sets or sets something insn uses or sets. */
1567 for (trial = PREV_INSN (target),
1568 insns_to_search = MAX_DELAY_SLOT_INSN_SEARCH;
1569 trial && !LABEL_P (trial) && insns_to_search > 0;
1570 trial = PREV_INSN (trial))
1572 if (!INSN_P (trial))
1573 continue;
1574 --insns_to_search;
1576 pat = PATTERN (trial);
1577 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1578 continue;
1580 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (pat))
1582 bool annul_p = false;
1583 rtx_insn *control = seq->insn (0);
1585 /* If this is a CALL_INSN and its delay slots, it is hard to track
1586 the resource needs properly, so give up. */
1587 if (CALL_P (control))
1588 return 0;
1590 /* If this is an INSN or JUMP_INSN with delayed effects, it
1591 is hard to track the resource needs properly, so give up. */
1593 if (INSN_SETS_ARE_DELAYED (control))
1594 return 0;
1596 if (INSN_REFERENCES_ARE_DELAYED (control))
1597 return 0;
1599 if (JUMP_P (control))
1600 annul_p = INSN_ANNULLED_BRANCH_P (control);
1602 /* See if any of the insns in the delay slot match, updating
1603 resource requirements as we go. */
1604 for (i = seq->len () - 1; i > 0; i--)
1606 rtx candidate = seq->element (i);
1608 /* If an insn will be annulled if the branch is false, it isn't
1609 considered as a possible duplicate insn. */
1610 if (rtx_equal_p (PATTERN (candidate), ipat)
1611 && ! (annul_p && INSN_FROM_TARGET_P (candidate)))
1613 /* Show that this insn will be used in the sequel. */
1614 INSN_FROM_TARGET_P (candidate) = 0;
1615 return candidate;
1618 /* Unless this is an annulled insn from the target of a branch,
1619 we must stop if it sets anything needed or set by INSN. */
1620 if ((!annul_p || !INSN_FROM_TARGET_P (candidate))
1621 && insn_sets_resource_p (candidate, &needed, true))
1622 return 0;
1625 /* If the insn requiring the delay slot conflicts with INSN, we
1626 must stop. */
1627 if (insn_sets_resource_p (control, &needed, true))
1628 return 0;
1630 else
1632 /* See if TRIAL is the same as INSN. */
1633 pat = PATTERN (trial);
1634 if (rtx_equal_p (pat, ipat))
1635 return trial;
1637 /* Can't go any further if TRIAL conflicts with INSN. */
1638 if (insn_sets_resource_p (trial, &needed, true))
1639 return 0;
1643 return 0;
1646 /* Return 1 if THREAD can only be executed in one way. If LABEL is nonzero,
1647 it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1648 is nonzero, we are allowed to fall into this thread; otherwise, we are
1649 not.
1651 If LABEL is used more than one or we pass a label other than LABEL before
1652 finding an active insn, we do not own this thread. */
1654 static int
1655 own_thread_p (rtx thread, rtx label, int allow_fallthrough)
1657 rtx_insn *active_insn;
1658 rtx_insn *insn;
1660 /* We don't own the function end. */
1661 if (thread == 0 || ANY_RETURN_P (thread))
1662 return 0;
1664 /* We have a non-NULL insn. */
1665 rtx_insn *thread_insn = as_a <rtx_insn *> (thread);
1667 /* Get the first active insn, or THREAD_INSN, if it is an active insn. */
1668 active_insn = next_active_insn (PREV_INSN (thread_insn));
1670 for (insn = thread_insn; insn != active_insn; insn = NEXT_INSN (insn))
1671 if (LABEL_P (insn)
1672 && (insn != label || LABEL_NUSES (insn) != 1))
1673 return 0;
1675 if (allow_fallthrough)
1676 return 1;
1678 /* Ensure that we reach a BARRIER before any insn or label. */
1679 for (insn = prev_nonnote_insn (thread_insn);
1680 insn == 0 || !BARRIER_P (insn);
1681 insn = prev_nonnote_insn (insn))
1682 if (insn == 0
1683 || LABEL_P (insn)
1684 || (NONJUMP_INSN_P (insn)
1685 && GET_CODE (PATTERN (insn)) != USE
1686 && GET_CODE (PATTERN (insn)) != CLOBBER))
1687 return 0;
1689 return 1;
1692 /* Called when INSN is being moved from a location near the target of a jump.
1693 We leave a marker of the form (use (INSN)) immediately in front
1694 of WHERE for mark_target_live_regs. These markers will be deleted when
1695 reorg finishes.
1697 We used to try to update the live status of registers if WHERE is at
1698 the start of a basic block, but that can't work since we may remove a
1699 BARRIER in relax_delay_slots. */
1701 static void
1702 update_block (rtx_insn *insn, rtx where)
1704 /* Ignore if this was in a delay slot and it came from the target of
1705 a branch. */
1706 if (INSN_FROM_TARGET_P (insn))
1707 return;
1709 emit_insn_before (gen_rtx_USE (VOIDmode, insn), where);
1711 /* INSN might be making a value live in a block where it didn't use to
1712 be. So recompute liveness information for this block. */
1714 incr_ticks_for_insn (insn);
1717 /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1718 the basic block containing the jump. */
1720 static int
1721 reorg_redirect_jump (rtx_jump_insn *jump, rtx nlabel)
1723 incr_ticks_for_insn (jump);
1724 return redirect_jump (jump, nlabel, 1);
1727 /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1728 We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1729 that reference values used in INSN. If we find one, then we move the
1730 REG_DEAD note to INSN.
1732 This is needed to handle the case where a later insn (after INSN) has a
1733 REG_DEAD note for a register used by INSN, and this later insn subsequently
1734 gets moved before a CODE_LABEL because it is a redundant insn. In this
1735 case, mark_target_live_regs may be confused into thinking the register
1736 is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1738 static void
1739 update_reg_dead_notes (rtx_insn *insn, rtx_insn *delayed_insn)
1741 rtx link, next;
1742 rtx_insn *p;
1744 for (p = next_nonnote_insn (insn); p != delayed_insn;
1745 p = next_nonnote_insn (p))
1746 for (link = REG_NOTES (p); link; link = next)
1748 next = XEXP (link, 1);
1750 if (REG_NOTE_KIND (link) != REG_DEAD
1751 || !REG_P (XEXP (link, 0)))
1752 continue;
1754 if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
1756 /* Move the REG_DEAD note from P to INSN. */
1757 remove_note (p, link);
1758 XEXP (link, 1) = REG_NOTES (insn);
1759 REG_NOTES (insn) = link;
1764 /* Called when an insn redundant with start_insn is deleted. If there
1765 is a REG_DEAD note for the target of start_insn between start_insn
1766 and stop_insn, then the REG_DEAD note needs to be deleted since the
1767 value no longer dies there.
1769 If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1770 confused into thinking the register is dead. */
1772 static void
1773 fix_reg_dead_note (rtx start_insn, rtx stop_insn)
1775 rtx link, next;
1776 rtx_insn *p;
1778 for (p = next_nonnote_insn (start_insn); p != stop_insn;
1779 p = next_nonnote_insn (p))
1780 for (link = REG_NOTES (p); link; link = next)
1782 next = XEXP (link, 1);
1784 if (REG_NOTE_KIND (link) != REG_DEAD
1785 || !REG_P (XEXP (link, 0)))
1786 continue;
1788 if (reg_set_p (XEXP (link, 0), PATTERN (start_insn)))
1790 remove_note (p, link);
1791 return;
1796 /* Delete any REG_UNUSED notes that exist on INSN but not on REDUNDANT_INSN.
1798 This handles the case of udivmodXi4 instructions which optimize their
1799 output depending on whether any REG_UNUSED notes are present.
1800 we must make sure that INSN calculates as many results as REDUNDANT_INSN
1801 does. */
1803 static void
1804 update_reg_unused_notes (rtx insn, rtx redundant_insn)
1806 rtx link, next;
1808 for (link = REG_NOTES (insn); link; link = next)
1810 next = XEXP (link, 1);
1812 if (REG_NOTE_KIND (link) != REG_UNUSED
1813 || !REG_P (XEXP (link, 0)))
1814 continue;
1816 if (! find_regno_note (redundant_insn, REG_UNUSED,
1817 REGNO (XEXP (link, 0))))
1818 remove_note (insn, link);
1822 static vec <rtx> sibling_labels;
1824 /* Return the label before INSN, or put a new label there. If SIBLING is
1825 non-zero, it is another label associated with the new label (if any),
1826 typically the former target of the jump that will be redirected to
1827 the new label. */
1829 static rtx_insn *
1830 get_label_before (rtx_insn *insn, rtx sibling)
1832 rtx_insn *label;
1834 /* Find an existing label at this point
1835 or make a new one if there is none. */
1836 label = prev_nonnote_insn (insn);
1838 if (label == 0 || !LABEL_P (label))
1840 rtx_insn *prev = PREV_INSN (insn);
1842 label = gen_label_rtx ();
1843 emit_label_after (label, prev);
1844 LABEL_NUSES (label) = 0;
1845 if (sibling)
1847 sibling_labels.safe_push (label);
1848 sibling_labels.safe_push (sibling);
1851 return label;
1854 /* Scan a function looking for insns that need a delay slot and find insns to
1855 put into the delay slot.
1857 NON_JUMPS_P is nonzero if we are to only try to fill non-jump insns (such
1858 as calls). We do these first since we don't want jump insns (that are
1859 easier to fill) to get the only insns that could be used for non-jump insns.
1860 When it is zero, only try to fill JUMP_INSNs.
1862 When slots are filled in this manner, the insns (including the
1863 delay_insn) are put together in a SEQUENCE rtx. In this fashion,
1864 it is possible to tell whether a delay slot has really been filled
1865 or not. `final' knows how to deal with this, by communicating
1866 through FINAL_SEQUENCE. */
1868 static void
1869 fill_simple_delay_slots (int non_jumps_p)
1871 rtx_insn *insn, *trial, *next_trial;
1872 rtx pat;
1873 int i;
1874 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
1875 struct resources needed, set;
1876 int slots_to_fill, slots_filled;
1877 auto_vec<rtx_insn *, 5> delay_list;
1879 for (i = 0; i < num_unfilled_slots; i++)
1881 int flags;
1882 /* Get the next insn to fill. If it has already had any slots assigned,
1883 we can't do anything with it. Maybe we'll improve this later. */
1885 insn = unfilled_slots_base[i];
1886 if (insn == 0
1887 || insn->deleted ()
1888 || (NONJUMP_INSN_P (insn)
1889 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1890 || (JUMP_P (insn) && non_jumps_p)
1891 || (!JUMP_P (insn) && ! non_jumps_p))
1892 continue;
1894 /* It may have been that this insn used to need delay slots, but
1895 now doesn't; ignore in that case. This can happen, for example,
1896 on the HP PA RISC, where the number of delay slots depends on
1897 what insns are nearby. */
1898 slots_to_fill = num_delay_slots (insn);
1900 /* Some machine description have defined instructions to have
1901 delay slots only in certain circumstances which may depend on
1902 nearby insns (which change due to reorg's actions).
1904 For example, the PA port normally has delay slots for unconditional
1905 jumps.
1907 However, the PA port claims such jumps do not have a delay slot
1908 if they are immediate successors of certain CALL_INSNs. This
1909 allows the port to favor filling the delay slot of the call with
1910 the unconditional jump. */
1911 if (slots_to_fill == 0)
1912 continue;
1914 /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
1915 says how many. After initialization, first try optimizing
1917 call _foo call _foo
1918 nop add %o7,.-L1,%o7
1919 b,a L1
1922 If this case applies, the delay slot of the call is filled with
1923 the unconditional jump. This is done first to avoid having the
1924 delay slot of the call filled in the backward scan. Also, since
1925 the unconditional jump is likely to also have a delay slot, that
1926 insn must exist when it is subsequently scanned.
1928 This is tried on each insn with delay slots as some machines
1929 have insns which perform calls, but are not represented as
1930 CALL_INSNs. */
1932 slots_filled = 0;
1933 delay_list.truncate (0);
1935 if (JUMP_P (insn))
1936 flags = get_jump_flags (insn, JUMP_LABEL (insn));
1937 else
1938 flags = get_jump_flags (insn, NULL_RTX);
1940 if ((trial = next_active_insn (insn))
1941 && JUMP_P (trial)
1942 && simplejump_p (trial)
1943 && eligible_for_delay (insn, slots_filled, trial, flags)
1944 && no_labels_between_p (insn, trial)
1945 && ! can_throw_internal (trial))
1947 rtx_insn **tmp;
1948 slots_filled++;
1949 add_to_delay_list (trial, &delay_list);
1951 /* TRIAL may have had its delay slot filled, then unfilled. When
1952 the delay slot is unfilled, TRIAL is placed back on the unfilled
1953 slots obstack. Unfortunately, it is placed on the end of the
1954 obstack, not in its original location. Therefore, we must search
1955 from entry i + 1 to the end of the unfilled slots obstack to
1956 try and find TRIAL. */
1957 tmp = &unfilled_slots_base[i + 1];
1958 while (*tmp != trial && tmp != unfilled_slots_next)
1959 tmp++;
1961 /* Remove the unconditional jump from consideration for delay slot
1962 filling and unthread it. */
1963 if (*tmp == trial)
1964 *tmp = 0;
1966 rtx_insn *next = NEXT_INSN (trial);
1967 rtx_insn *prev = PREV_INSN (trial);
1968 if (prev)
1969 SET_NEXT_INSN (prev) = next;
1970 if (next)
1971 SET_PREV_INSN (next) = prev;
1975 /* Now, scan backwards from the insn to search for a potential
1976 delay-slot candidate. Stop searching when a label or jump is hit.
1978 For each candidate, if it is to go into the delay slot (moved
1979 forward in execution sequence), it must not need or set any resources
1980 that were set by later insns and must not set any resources that
1981 are needed for those insns.
1983 The delay slot insn itself sets resources unless it is a call
1984 (in which case the called routine, not the insn itself, is doing
1985 the setting). */
1987 if (slots_filled < slots_to_fill)
1989 /* If the flags register is dead after the insn, then we want to be
1990 able to accept a candidate that clobbers it. For this purpose,
1991 we need to filter the flags register during life analysis, so
1992 that it doesn't create RAW and WAW dependencies, while still
1993 creating the necessary WAR dependencies. */
1994 bool filter_flags
1995 = (slots_to_fill == 1
1996 && targetm.flags_regnum != INVALID_REGNUM
1997 && find_regno_note (insn, REG_DEAD, targetm.flags_regnum));
1998 struct resources fset;
1999 CLEAR_RESOURCE (&needed);
2000 CLEAR_RESOURCE (&set);
2001 mark_set_resources (insn, &set, 0, MARK_SRC_DEST);
2002 if (filter_flags)
2004 CLEAR_RESOURCE (&fset);
2005 mark_set_resources (insn, &fset, 0, MARK_SRC_DEST);
2007 mark_referenced_resources (insn, &needed, false);
2009 for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, 1);
2010 trial = next_trial)
2012 next_trial = prev_nonnote_insn (trial);
2014 /* This must be an INSN or CALL_INSN. */
2015 pat = PATTERN (trial);
2017 /* Stand-alone USE and CLOBBER are just for flow. */
2018 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2019 continue;
2021 /* Check for resource conflict first, to avoid unnecessary
2022 splitting. */
2023 if (! insn_references_resource_p (trial, &set, true)
2024 && ! insn_sets_resource_p (trial,
2025 filter_flags ? &fset : &set,
2026 true)
2027 && ! insn_sets_resource_p (trial, &needed, true)
2028 /* Can't separate set of cc0 from its use. */
2029 && (!HAVE_cc0 || ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat)))
2030 && ! can_throw_internal (trial))
2032 trial = try_split (pat, trial, 1);
2033 next_trial = prev_nonnote_insn (trial);
2034 if (eligible_for_delay (insn, slots_filled, trial, flags))
2036 /* In this case, we are searching backward, so if we
2037 find insns to put on the delay list, we want
2038 to put them at the head, rather than the
2039 tail, of the list. */
2041 update_reg_dead_notes (trial, insn);
2042 delay_list.safe_insert (0, trial);
2043 update_block (trial, trial);
2044 delete_related_insns (trial);
2045 if (slots_to_fill == ++slots_filled)
2046 break;
2047 continue;
2051 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2052 if (filter_flags)
2054 mark_set_resources (trial, &fset, 0, MARK_SRC_DEST_CALL);
2055 /* If the flags register is set, then it doesn't create RAW
2056 dependencies any longer and it also doesn't create WAW
2057 dependencies since it's dead after the original insn. */
2058 if (TEST_HARD_REG_BIT (fset.regs, targetm.flags_regnum))
2060 CLEAR_HARD_REG_BIT (needed.regs, targetm.flags_regnum);
2061 CLEAR_HARD_REG_BIT (fset.regs, targetm.flags_regnum);
2064 mark_referenced_resources (trial, &needed, true);
2068 /* If all needed slots haven't been filled, we come here. */
2070 /* Try to optimize case of jumping around a single insn. */
2071 if ((ANNUL_IFTRUE_SLOTS || ANNUL_IFFALSE_SLOTS)
2072 && slots_filled != slots_to_fill
2073 && delay_list.is_empty ()
2074 && JUMP_P (insn)
2075 && (condjump_p (insn) || condjump_in_parallel_p (insn))
2076 && !ANY_RETURN_P (JUMP_LABEL (insn)))
2078 optimize_skip (as_a <rtx_jump_insn *> (insn), &delay_list);
2079 if (!delay_list.is_empty ())
2080 slots_filled += 1;
2083 /* Try to get insns from beyond the insn needing the delay slot.
2084 These insns can neither set or reference resources set in insns being
2085 skipped, cannot set resources in the insn being skipped, and, if this
2086 is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2087 call might not return).
2089 There used to be code which continued past the target label if
2090 we saw all uses of the target label. This code did not work,
2091 because it failed to account for some instructions which were
2092 both annulled and marked as from the target. This can happen as a
2093 result of optimize_skip. Since this code was redundant with
2094 fill_eager_delay_slots anyways, it was just deleted. */
2096 if (slots_filled != slots_to_fill
2097 /* If this instruction could throw an exception which is
2098 caught in the same function, then it's not safe to fill
2099 the delay slot with an instruction from beyond this
2100 point. For example, consider:
2102 int i = 2;
2104 try {
2105 f();
2106 i = 3;
2107 } catch (...) {}
2109 return i;
2111 Even though `i' is a local variable, we must be sure not
2112 to put `i = 3' in the delay slot if `f' might throw an
2113 exception.
2115 Presumably, we should also check to see if we could get
2116 back to this function via `setjmp'. */
2117 && ! can_throw_internal (insn)
2118 && !JUMP_P (insn))
2120 int maybe_never = 0;
2121 rtx pat, trial_delay;
2123 CLEAR_RESOURCE (&needed);
2124 CLEAR_RESOURCE (&set);
2125 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2126 mark_referenced_resources (insn, &needed, true);
2128 if (CALL_P (insn))
2129 maybe_never = 1;
2131 for (trial = next_nonnote_insn (insn); !stop_search_p (trial, 1);
2132 trial = next_trial)
2134 next_trial = next_nonnote_insn (trial);
2136 /* This must be an INSN or CALL_INSN. */
2137 pat = PATTERN (trial);
2139 /* Stand-alone USE and CLOBBER are just for flow. */
2140 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2141 continue;
2143 /* If this already has filled delay slots, get the insn needing
2144 the delay slots. */
2145 if (GET_CODE (pat) == SEQUENCE)
2146 trial_delay = XVECEXP (pat, 0, 0);
2147 else
2148 trial_delay = trial;
2150 /* Stop our search when seeing a jump. */
2151 if (JUMP_P (trial_delay))
2152 break;
2154 /* See if we have a resource problem before we try to split. */
2155 if (GET_CODE (pat) != SEQUENCE
2156 && ! insn_references_resource_p (trial, &set, true)
2157 && ! insn_sets_resource_p (trial, &set, true)
2158 && ! insn_sets_resource_p (trial, &needed, true)
2159 && (!HAVE_cc0 && ! (reg_mentioned_p (cc0_rtx, pat) && ! sets_cc0_p (pat)))
2160 && ! (maybe_never && may_trap_or_fault_p (pat))
2161 && (trial = try_split (pat, trial, 0))
2162 && eligible_for_delay (insn, slots_filled, trial, flags)
2163 && ! can_throw_internal (trial))
2165 next_trial = next_nonnote_insn (trial);
2166 add_to_delay_list (trial, &delay_list);
2167 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, pat))
2168 link_cc0_insns (trial);
2170 delete_related_insns (trial);
2171 if (slots_to_fill == ++slots_filled)
2172 break;
2173 continue;
2176 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2177 mark_referenced_resources (trial, &needed, true);
2179 /* Ensure we don't put insns between the setting of cc and the
2180 comparison by moving a setting of cc into an earlier delay
2181 slot since these insns could clobber the condition code. */
2182 set.cc = 1;
2184 /* If this is a call, we might not get here. */
2185 if (CALL_P (trial_delay))
2186 maybe_never = 1;
2189 /* If there are slots left to fill and our search was stopped by an
2190 unconditional branch, try the insn at the branch target. We can
2191 redirect the branch if it works.
2193 Don't do this if the insn at the branch target is a branch. */
2194 if (slots_to_fill != slots_filled
2195 && trial
2196 && jump_to_label_p (trial)
2197 && simplejump_p (trial)
2198 && (next_trial = next_active_insn (JUMP_LABEL (trial))) != 0
2199 && ! (NONJUMP_INSN_P (next_trial)
2200 && GET_CODE (PATTERN (next_trial)) == SEQUENCE)
2201 && !JUMP_P (next_trial)
2202 && ! insn_references_resource_p (next_trial, &set, true)
2203 && ! insn_sets_resource_p (next_trial, &set, true)
2204 && ! insn_sets_resource_p (next_trial, &needed, true)
2205 && (!HAVE_cc0 || ! reg_mentioned_p (cc0_rtx, PATTERN (next_trial)))
2206 && ! (maybe_never && may_trap_or_fault_p (PATTERN (next_trial)))
2207 && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2208 && eligible_for_delay (insn, slots_filled, next_trial, flags)
2209 && ! can_throw_internal (trial))
2211 /* See comment in relax_delay_slots about necessity of using
2212 next_real_insn here. */
2213 rtx_insn *new_label = next_real_insn (next_trial);
2215 if (new_label != 0)
2216 new_label = get_label_before (new_label, JUMP_LABEL (trial));
2217 else
2218 new_label = find_end_label (simple_return_rtx);
2220 if (new_label)
2222 add_to_delay_list (copy_delay_slot_insn (next_trial),
2223 &delay_list);
2224 slots_filled++;
2225 reorg_redirect_jump (as_a <rtx_jump_insn *> (trial),
2226 new_label);
2231 /* If this is an unconditional jump, then try to get insns from the
2232 target of the jump. */
2233 rtx_jump_insn *jump_insn;
2234 if ((jump_insn = dyn_cast <rtx_jump_insn *> (insn))
2235 && simplejump_p (jump_insn)
2236 && slots_filled != slots_to_fill)
2237 fill_slots_from_thread (jump_insn, const_true_rtx,
2238 next_active_insn (JUMP_LABEL (insn)), NULL, 1,
2239 1, own_thread_p (JUMP_LABEL (insn),
2240 JUMP_LABEL (insn), 0),
2241 slots_to_fill, &slots_filled, &delay_list);
2243 if (!delay_list.is_empty ())
2244 unfilled_slots_base[i]
2245 = emit_delay_sequence (insn, delay_list, slots_filled);
2247 if (slots_to_fill == slots_filled)
2248 unfilled_slots_base[i] = 0;
2250 note_delay_statistics (slots_filled, 0);
2254 /* Follow any unconditional jump at LABEL, for the purpose of redirecting JUMP;
2255 return the ultimate label reached by any such chain of jumps.
2256 Return a suitable return rtx if the chain ultimately leads to a
2257 return instruction.
2258 If LABEL is not followed by a jump, return LABEL.
2259 If the chain loops or we can't find end, return LABEL,
2260 since that tells caller to avoid changing the insn.
2261 If the returned label is obtained by following a crossing jump,
2262 set *CROSSING to true, otherwise set it to false. */
2264 static rtx
2265 follow_jumps (rtx label, rtx_insn *jump, bool *crossing)
2267 rtx_insn *insn;
2268 rtx_insn *next;
2269 int depth;
2271 *crossing = false;
2272 if (ANY_RETURN_P (label))
2273 return label;
2275 rtx_insn *value = as_a <rtx_insn *> (label);
2277 for (depth = 0;
2278 (depth < 10
2279 && (insn = next_active_insn (value)) != 0
2280 && JUMP_P (insn)
2281 && JUMP_LABEL (insn) != NULL_RTX
2282 && ((any_uncondjump_p (insn) && onlyjump_p (insn))
2283 || ANY_RETURN_P (PATTERN (insn)))
2284 && (next = NEXT_INSN (insn))
2285 && BARRIER_P (next));
2286 depth++)
2288 rtx this_label_or_return = JUMP_LABEL (insn);
2290 /* If we have found a cycle, make the insn jump to itself. */
2291 if (this_label_or_return == label)
2292 return label;
2294 /* Cannot follow returns and cannot look through tablejumps. */
2295 if (ANY_RETURN_P (this_label_or_return))
2296 return this_label_or_return;
2298 rtx_insn *this_label = as_a <rtx_insn *> (this_label_or_return);
2299 if (NEXT_INSN (this_label)
2300 && JUMP_TABLE_DATA_P (NEXT_INSN (this_label)))
2301 break;
2303 if (!targetm.can_follow_jump (jump, insn))
2304 break;
2305 if (!*crossing)
2306 *crossing = CROSSING_JUMP_P (jump);
2307 value = this_label;
2309 if (depth == 10)
2310 return label;
2311 return value;
2314 /* Try to find insns to place in delay slots.
2316 INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2317 or is an unconditional branch if CONDITION is const_true_rtx.
2318 *PSLOTS_FILLED is updated with the number of slots that we have filled.
2320 THREAD is a flow-of-control, either the insns to be executed if the
2321 branch is true or if the branch is false, THREAD_IF_TRUE says which.
2323 OPPOSITE_THREAD is the thread in the opposite direction. It is used
2324 to see if any potential delay slot insns set things needed there.
2326 LIKELY is nonzero if it is extremely likely that the branch will be
2327 taken and THREAD_IF_TRUE is set. This is used for the branch at the
2328 end of a loop back up to the top.
2330 OWN_THREAD and OWN_OPPOSITE_THREAD are true if we are the only user of the
2331 thread. I.e., it is the fallthrough code of our jump or the target of the
2332 jump when we are the only jump going there.
2334 If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2335 case, we can only take insns from the head of the thread for our delay
2336 slot. We then adjust the jump to point after the insns we have taken. */
2338 static void
2339 fill_slots_from_thread (rtx_jump_insn *insn, rtx condition,
2340 rtx thread_or_return, rtx opposite_thread, int likely,
2341 int thread_if_true, int own_thread, int slots_to_fill,
2342 int *pslots_filled, vec<rtx_insn *> *delay_list)
2344 rtx new_thread;
2345 struct resources opposite_needed, set, needed;
2346 rtx_insn *trial;
2347 int lose = 0;
2348 int must_annul = 0;
2349 int flags;
2351 /* Validate our arguments. */
2352 gcc_assert (condition != const_true_rtx || thread_if_true);
2353 gcc_assert (own_thread || thread_if_true);
2355 flags = get_jump_flags (insn, JUMP_LABEL (insn));
2357 /* If our thread is the end of subroutine, we can't get any delay
2358 insns from that. */
2359 if (thread_or_return == NULL_RTX || ANY_RETURN_P (thread_or_return))
2360 return;
2362 rtx_insn *thread = as_a <rtx_insn *> (thread_or_return);
2364 /* If this is an unconditional branch, nothing is needed at the
2365 opposite thread. Otherwise, compute what is needed there. */
2366 if (condition == const_true_rtx)
2367 CLEAR_RESOURCE (&opposite_needed);
2368 else
2369 mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2371 /* If the insn at THREAD can be split, do it here to avoid having to
2372 update THREAD and NEW_THREAD if it is done in the loop below. Also
2373 initialize NEW_THREAD. */
2375 new_thread = thread = try_split (PATTERN (thread), thread, 0);
2377 /* Scan insns at THREAD. We are looking for an insn that can be removed
2378 from THREAD (it neither sets nor references resources that were set
2379 ahead of it and it doesn't set anything needs by the insns ahead of
2380 it) and that either can be placed in an annulling insn or aren't
2381 needed at OPPOSITE_THREAD. */
2383 CLEAR_RESOURCE (&needed);
2384 CLEAR_RESOURCE (&set);
2386 /* If we do not own this thread, we must stop as soon as we find
2387 something that we can't put in a delay slot, since all we can do
2388 is branch into THREAD at a later point. Therefore, labels stop
2389 the search if this is not the `true' thread. */
2391 for (trial = thread;
2392 ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2393 trial = next_nonnote_insn (trial))
2395 rtx pat, old_trial;
2397 /* If we have passed a label, we no longer own this thread. */
2398 if (LABEL_P (trial))
2400 own_thread = 0;
2401 continue;
2404 pat = PATTERN (trial);
2405 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2406 continue;
2408 /* If TRIAL conflicts with the insns ahead of it, we lose. Also,
2409 don't separate or copy insns that set and use CC0. */
2410 if (! insn_references_resource_p (trial, &set, true)
2411 && ! insn_sets_resource_p (trial, &set, true)
2412 && ! insn_sets_resource_p (trial, &needed, true)
2413 && (!HAVE_cc0 || (! (reg_mentioned_p (cc0_rtx, pat)
2414 && (! own_thread || ! sets_cc0_p (pat)))))
2415 && ! can_throw_internal (trial))
2417 rtx prior_insn;
2419 /* If TRIAL is redundant with some insn before INSN, we don't
2420 actually need to add it to the delay list; we can merely pretend
2421 we did. */
2422 if ((prior_insn = redundant_insn (trial, insn, *delay_list)))
2424 fix_reg_dead_note (prior_insn, insn);
2425 if (own_thread)
2427 update_block (trial, thread);
2428 if (trial == thread)
2430 thread = next_active_insn (thread);
2431 if (new_thread == trial)
2432 new_thread = thread;
2435 delete_related_insns (trial);
2437 else
2439 update_reg_unused_notes (prior_insn, trial);
2440 new_thread = next_active_insn (trial);
2443 continue;
2446 /* There are two ways we can win: If TRIAL doesn't set anything
2447 needed at the opposite thread and can't trap, or if it can
2448 go into an annulled delay slot. But we want neither to copy
2449 nor to speculate frame-related insns. */
2450 if (!must_annul
2451 && ((condition == const_true_rtx
2452 && (own_thread || !RTX_FRAME_RELATED_P (trial)))
2453 || (! insn_sets_resource_p (trial, &opposite_needed, true)
2454 && ! may_trap_or_fault_p (pat)
2455 && ! RTX_FRAME_RELATED_P (trial))))
2457 old_trial = trial;
2458 trial = try_split (pat, trial, 0);
2459 if (new_thread == old_trial)
2460 new_thread = trial;
2461 if (thread == old_trial)
2462 thread = trial;
2463 pat = PATTERN (trial);
2464 if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2465 goto winner;
2467 else if (0
2468 || (ANNUL_IFTRUE_SLOTS && ! thread_if_true)
2469 || (ANNUL_IFFALSE_SLOTS && thread_if_true))
2471 old_trial = trial;
2472 trial = try_split (pat, trial, 0);
2473 if (new_thread == old_trial)
2474 new_thread = trial;
2475 if (thread == old_trial)
2476 thread = trial;
2477 pat = PATTERN (trial);
2478 if ((must_annul || delay_list->is_empty ()) && (thread_if_true
2479 ? check_annul_list_true_false (0, *delay_list)
2480 && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2481 : check_annul_list_true_false (1, *delay_list)
2482 && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2484 rtx_insn *temp;
2486 must_annul = 1;
2487 winner:
2489 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, pat))
2490 link_cc0_insns (trial);
2492 /* If we own this thread, delete the insn. If this is the
2493 destination of a branch, show that a basic block status
2494 may have been updated. In any case, mark the new
2495 starting point of this thread. */
2496 if (own_thread)
2498 rtx note;
2500 update_block (trial, thread);
2501 if (trial == thread)
2503 thread = next_active_insn (thread);
2504 if (new_thread == trial)
2505 new_thread = thread;
2508 /* We are moving this insn, not deleting it. We must
2509 temporarily increment the use count on any referenced
2510 label lest it be deleted by delete_related_insns. */
2511 for (note = REG_NOTES (trial);
2512 note != NULL_RTX;
2513 note = XEXP (note, 1))
2514 if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2515 || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2517 /* REG_LABEL_OPERAND could be
2518 NOTE_INSN_DELETED_LABEL too. */
2519 if (LABEL_P (XEXP (note, 0)))
2520 LABEL_NUSES (XEXP (note, 0))++;
2521 else
2522 gcc_assert (REG_NOTE_KIND (note)
2523 == REG_LABEL_OPERAND);
2525 if (jump_to_label_p (trial))
2526 LABEL_NUSES (JUMP_LABEL (trial))++;
2528 delete_related_insns (trial);
2530 for (note = REG_NOTES (trial);
2531 note != NULL_RTX;
2532 note = XEXP (note, 1))
2533 if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2534 || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2536 /* REG_LABEL_OPERAND could be
2537 NOTE_INSN_DELETED_LABEL too. */
2538 if (LABEL_P (XEXP (note, 0)))
2539 LABEL_NUSES (XEXP (note, 0))--;
2540 else
2541 gcc_assert (REG_NOTE_KIND (note)
2542 == REG_LABEL_OPERAND);
2544 if (jump_to_label_p (trial))
2545 LABEL_NUSES (JUMP_LABEL (trial))--;
2547 else
2548 new_thread = next_active_insn (trial);
2550 temp = own_thread ? trial : copy_delay_slot_insn (trial);
2551 if (thread_if_true)
2552 INSN_FROM_TARGET_P (temp) = 1;
2554 add_to_delay_list (temp, delay_list);
2556 if (slots_to_fill == ++(*pslots_filled))
2558 /* Even though we have filled all the slots, we
2559 may be branching to a location that has a
2560 redundant insn. Skip any if so. */
2561 while (new_thread && ! own_thread
2562 && ! insn_sets_resource_p (new_thread, &set, true)
2563 && ! insn_sets_resource_p (new_thread, &needed,
2564 true)
2565 && ! insn_references_resource_p (new_thread,
2566 &set, true)
2567 && (prior_insn
2568 = redundant_insn (new_thread, insn,
2569 *delay_list)))
2571 /* We know we do not own the thread, so no need
2572 to call update_block and delete_insn. */
2573 fix_reg_dead_note (prior_insn, insn);
2574 update_reg_unused_notes (prior_insn, new_thread);
2575 new_thread = next_active_insn (new_thread);
2577 break;
2580 continue;
2585 /* This insn can't go into a delay slot. */
2586 lose = 1;
2587 mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2588 mark_referenced_resources (trial, &needed, true);
2590 /* Ensure we don't put insns between the setting of cc and the comparison
2591 by moving a setting of cc into an earlier delay slot since these insns
2592 could clobber the condition code. */
2593 set.cc = 1;
2595 /* If this insn is a register-register copy and the next insn has
2596 a use of our destination, change it to use our source. That way,
2597 it will become a candidate for our delay slot the next time
2598 through this loop. This case occurs commonly in loops that
2599 scan a list.
2601 We could check for more complex cases than those tested below,
2602 but it doesn't seem worth it. It might also be a good idea to try
2603 to swap the two insns. That might do better.
2605 We can't do this if the next insn modifies our destination, because
2606 that would make the replacement into the insn invalid. We also can't
2607 do this if it modifies our source, because it might be an earlyclobber
2608 operand. This latter test also prevents updating the contents of
2609 a PRE_INC. We also can't do this if there's overlap of source and
2610 destination. Overlap may happen for larger-than-register-size modes. */
2612 if (NONJUMP_INSN_P (trial) && GET_CODE (pat) == SET
2613 && REG_P (SET_SRC (pat))
2614 && REG_P (SET_DEST (pat))
2615 && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2617 rtx_insn *next = next_nonnote_insn (trial);
2619 if (next && NONJUMP_INSN_P (next)
2620 && GET_CODE (PATTERN (next)) != USE
2621 && ! reg_set_p (SET_DEST (pat), next)
2622 && ! reg_set_p (SET_SRC (pat), next)
2623 && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2624 && ! modified_in_p (SET_DEST (pat), next))
2625 validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2629 /* If we stopped on a branch insn that has delay slots, see if we can
2630 steal some of the insns in those slots. */
2631 if (trial && NONJUMP_INSN_P (trial)
2632 && GET_CODE (PATTERN (trial)) == SEQUENCE
2633 && JUMP_P (XVECEXP (PATTERN (trial), 0, 0)))
2635 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (trial));
2636 /* If this is the `true' thread, we will want to follow the jump,
2637 so we can only do this if we have taken everything up to here. */
2638 if (thread_if_true && trial == new_thread)
2640 steal_delay_list_from_target (insn, condition, sequence,
2641 delay_list, &set, &needed,
2642 &opposite_needed, slots_to_fill,
2643 pslots_filled, &must_annul,
2644 &new_thread);
2645 /* If we owned the thread and are told that it branched
2646 elsewhere, make sure we own the thread at the new location. */
2647 if (own_thread && trial != new_thread)
2648 own_thread = own_thread_p (new_thread, new_thread, 0);
2650 else if (! thread_if_true)
2651 steal_delay_list_from_fallthrough (insn, condition, sequence,
2652 delay_list, &set, &needed,
2653 &opposite_needed, slots_to_fill,
2654 pslots_filled, &must_annul);
2657 /* If we haven't found anything for this delay slot and it is very
2658 likely that the branch will be taken, see if the insn at our target
2659 increments or decrements a register with an increment that does not
2660 depend on the destination register. If so, try to place the opposite
2661 arithmetic insn after the jump insn and put the arithmetic insn in the
2662 delay slot. If we can't do this, return. */
2663 if (delay_list->is_empty () && likely
2664 && new_thread && !ANY_RETURN_P (new_thread)
2665 && NONJUMP_INSN_P (new_thread)
2666 && !RTX_FRAME_RELATED_P (new_thread)
2667 && GET_CODE (PATTERN (new_thread)) != ASM_INPUT
2668 && asm_noperands (PATTERN (new_thread)) < 0)
2670 rtx pat = PATTERN (new_thread);
2671 rtx dest;
2672 rtx src;
2674 /* We know "new_thread" is an insn due to NONJUMP_INSN_P (new_thread)
2675 above. */
2676 trial = as_a <rtx_insn *> (new_thread);
2677 pat = PATTERN (trial);
2679 if (!NONJUMP_INSN_P (trial)
2680 || GET_CODE (pat) != SET
2681 || ! eligible_for_delay (insn, 0, trial, flags)
2682 || can_throw_internal (trial))
2683 return;
2685 dest = SET_DEST (pat), src = SET_SRC (pat);
2686 if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2687 && rtx_equal_p (XEXP (src, 0), dest)
2688 && (!FLOAT_MODE_P (GET_MODE (src))
2689 || flag_unsafe_math_optimizations)
2690 && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2691 && ! side_effects_p (pat))
2693 rtx other = XEXP (src, 1);
2694 rtx new_arith;
2695 rtx_insn *ninsn;
2697 /* If this is a constant adjustment, use the same code with
2698 the negated constant. Otherwise, reverse the sense of the
2699 arithmetic. */
2700 if (CONST_INT_P (other))
2701 new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2702 negate_rtx (GET_MODE (src), other));
2703 else
2704 new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2705 GET_MODE (src), dest, other);
2707 ninsn = emit_insn_after (gen_rtx_SET (dest, new_arith), insn);
2709 if (recog_memoized (ninsn) < 0
2710 || (extract_insn (ninsn),
2711 !constrain_operands (1, get_preferred_alternatives (ninsn))))
2713 delete_related_insns (ninsn);
2714 return;
2717 if (own_thread)
2719 update_block (trial, thread);
2720 if (trial == thread)
2722 thread = next_active_insn (thread);
2723 if (new_thread == trial)
2724 new_thread = thread;
2726 delete_related_insns (trial);
2728 else
2729 new_thread = next_active_insn (trial);
2731 ninsn = own_thread ? trial : copy_delay_slot_insn (trial);
2732 if (thread_if_true)
2733 INSN_FROM_TARGET_P (ninsn) = 1;
2735 add_to_delay_list (ninsn, delay_list);
2736 (*pslots_filled)++;
2740 if (!delay_list->is_empty () && must_annul)
2741 INSN_ANNULLED_BRANCH_P (insn) = 1;
2743 /* If we are to branch into the middle of this thread, find an appropriate
2744 label or make a new one if none, and redirect INSN to it. If we hit the
2745 end of the function, use the end-of-function label. */
2746 if (new_thread != thread)
2748 rtx label;
2749 bool crossing = false;
2751 gcc_assert (thread_if_true);
2753 if (new_thread && simplejump_or_return_p (new_thread)
2754 && redirect_with_delay_list_safe_p (insn,
2755 JUMP_LABEL (new_thread),
2756 *delay_list))
2757 new_thread = follow_jumps (JUMP_LABEL (new_thread), insn,
2758 &crossing);
2760 if (ANY_RETURN_P (new_thread))
2761 label = find_end_label (new_thread);
2762 else if (LABEL_P (new_thread))
2763 label = new_thread;
2764 else
2765 label = get_label_before (as_a <rtx_insn *> (new_thread),
2766 JUMP_LABEL (insn));
2768 if (label)
2770 reorg_redirect_jump (insn, label);
2771 if (crossing)
2772 CROSSING_JUMP_P (insn) = 1;
2777 /* Make another attempt to find insns to place in delay slots.
2779 We previously looked for insns located in front of the delay insn
2780 and, for non-jump delay insns, located behind the delay insn.
2782 Here only try to schedule jump insns and try to move insns from either
2783 the target or the following insns into the delay slot. If annulling is
2784 supported, we will be likely to do this. Otherwise, we can do this only
2785 if safe. */
2787 static void
2788 fill_eager_delay_slots (void)
2790 rtx_insn *insn;
2791 int i;
2792 int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2794 for (i = 0; i < num_unfilled_slots; i++)
2796 rtx condition;
2797 rtx target_label, insn_at_target;
2798 rtx_insn *fallthrough_insn;
2799 auto_vec<rtx_insn *, 5> delay_list;
2800 rtx_jump_insn *jump_insn;
2801 int own_target;
2802 int own_fallthrough;
2803 int prediction, slots_to_fill, slots_filled;
2805 insn = unfilled_slots_base[i];
2806 if (insn == 0
2807 || insn->deleted ()
2808 || ! (jump_insn = dyn_cast <rtx_jump_insn *> (insn))
2809 || ! (condjump_p (jump_insn) || condjump_in_parallel_p (jump_insn)))
2810 continue;
2812 slots_to_fill = num_delay_slots (jump_insn);
2813 /* Some machine description have defined instructions to have
2814 delay slots only in certain circumstances which may depend on
2815 nearby insns (which change due to reorg's actions).
2817 For example, the PA port normally has delay slots for unconditional
2818 jumps.
2820 However, the PA port claims such jumps do not have a delay slot
2821 if they are immediate successors of certain CALL_INSNs. This
2822 allows the port to favor filling the delay slot of the call with
2823 the unconditional jump. */
2824 if (slots_to_fill == 0)
2825 continue;
2827 slots_filled = 0;
2828 target_label = JUMP_LABEL (jump_insn);
2829 condition = get_branch_condition (jump_insn, target_label);
2831 if (condition == 0)
2832 continue;
2834 /* Get the next active fallthrough and target insns and see if we own
2835 them. Then see whether the branch is likely true. We don't need
2836 to do a lot of this for unconditional branches. */
2838 insn_at_target = first_active_target_insn (target_label);
2839 own_target = own_thread_p (target_label, target_label, 0);
2841 if (condition == const_true_rtx)
2843 own_fallthrough = 0;
2844 fallthrough_insn = 0;
2845 prediction = 2;
2847 else
2849 fallthrough_insn = next_active_insn (jump_insn);
2850 own_fallthrough = own_thread_p (NEXT_INSN (jump_insn), NULL_RTX, 1);
2851 prediction = mostly_true_jump (jump_insn);
2854 /* If this insn is expected to branch, first try to get insns from our
2855 target, then our fallthrough insns. If it is not expected to branch,
2856 try the other order. */
2858 if (prediction > 0)
2860 fill_slots_from_thread (jump_insn, condition, insn_at_target,
2861 fallthrough_insn, prediction == 2, 1,
2862 own_target,
2863 slots_to_fill, &slots_filled, &delay_list);
2865 if (delay_list.is_empty () && own_fallthrough)
2867 /* Even though we didn't find anything for delay slots,
2868 we might have found a redundant insn which we deleted
2869 from the thread that was filled. So we have to recompute
2870 the next insn at the target. */
2871 target_label = JUMP_LABEL (jump_insn);
2872 insn_at_target = first_active_target_insn (target_label);
2874 fill_slots_from_thread (jump_insn, condition, fallthrough_insn,
2875 insn_at_target, 0, 0, own_fallthrough,
2876 slots_to_fill, &slots_filled,
2877 &delay_list);
2880 else
2882 if (own_fallthrough)
2883 fill_slots_from_thread (jump_insn, condition, fallthrough_insn,
2884 insn_at_target, 0, 0, own_fallthrough,
2885 slots_to_fill, &slots_filled, &delay_list);
2887 if (delay_list.is_empty ())
2888 fill_slots_from_thread (jump_insn, condition, insn_at_target,
2889 next_active_insn (insn), 0, 1, own_target,
2890 slots_to_fill, &slots_filled, &delay_list);
2893 if (!delay_list.is_empty ())
2894 unfilled_slots_base[i]
2895 = emit_delay_sequence (jump_insn, delay_list, slots_filled);
2897 if (slots_to_fill == slots_filled)
2898 unfilled_slots_base[i] = 0;
2900 note_delay_statistics (slots_filled, 1);
2904 static void delete_computation (rtx insn);
2906 /* Recursively delete prior insns that compute the value (used only by INSN
2907 which the caller is deleting) stored in the register mentioned by NOTE
2908 which is a REG_DEAD note associated with INSN. */
2910 static void
2911 delete_prior_computation (rtx note, rtx insn)
2913 rtx our_prev;
2914 rtx reg = XEXP (note, 0);
2916 for (our_prev = prev_nonnote_insn (insn);
2917 our_prev && (NONJUMP_INSN_P (our_prev)
2918 || CALL_P (our_prev));
2919 our_prev = prev_nonnote_insn (our_prev))
2921 rtx pat = PATTERN (our_prev);
2923 /* If we reach a CALL which is not calling a const function
2924 or the callee pops the arguments, then give up. */
2925 if (CALL_P (our_prev)
2926 && (! RTL_CONST_CALL_P (our_prev)
2927 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
2928 break;
2930 /* If we reach a SEQUENCE, it is too complex to try to
2931 do anything with it, so give up. We can be run during
2932 and after reorg, so SEQUENCE rtl can legitimately show
2933 up here. */
2934 if (GET_CODE (pat) == SEQUENCE)
2935 break;
2937 if (GET_CODE (pat) == USE
2938 && NONJUMP_INSN_P (XEXP (pat, 0)))
2939 /* reorg creates USEs that look like this. We leave them
2940 alone because reorg needs them for its own purposes. */
2941 break;
2943 if (reg_set_p (reg, pat))
2945 if (side_effects_p (pat) && !CALL_P (our_prev))
2946 break;
2948 if (GET_CODE (pat) == PARALLEL)
2950 /* If we find a SET of something else, we can't
2951 delete the insn. */
2953 int i;
2955 for (i = 0; i < XVECLEN (pat, 0); i++)
2957 rtx part = XVECEXP (pat, 0, i);
2959 if (GET_CODE (part) == SET
2960 && SET_DEST (part) != reg)
2961 break;
2964 if (i == XVECLEN (pat, 0))
2965 delete_computation (our_prev);
2967 else if (GET_CODE (pat) == SET
2968 && REG_P (SET_DEST (pat)))
2970 int dest_regno = REGNO (SET_DEST (pat));
2971 int dest_endregno = END_REGNO (SET_DEST (pat));
2972 int regno = REGNO (reg);
2973 int endregno = END_REGNO (reg);
2975 if (dest_regno >= regno
2976 && dest_endregno <= endregno)
2977 delete_computation (our_prev);
2979 /* We may have a multi-word hard register and some, but not
2980 all, of the words of the register are needed in subsequent
2981 insns. Write REG_UNUSED notes for those parts that were not
2982 needed. */
2983 else if (dest_regno <= regno
2984 && dest_endregno >= endregno)
2986 int i;
2988 add_reg_note (our_prev, REG_UNUSED, reg);
2990 for (i = dest_regno; i < dest_endregno; i++)
2991 if (! find_regno_note (our_prev, REG_UNUSED, i))
2992 break;
2994 if (i == dest_endregno)
2995 delete_computation (our_prev);
2999 break;
3002 /* If PAT references the register that dies here, it is an
3003 additional use. Hence any prior SET isn't dead. However, this
3004 insn becomes the new place for the REG_DEAD note. */
3005 if (reg_overlap_mentioned_p (reg, pat))
3007 XEXP (note, 1) = REG_NOTES (our_prev);
3008 REG_NOTES (our_prev) = note;
3009 break;
3014 /* Delete INSN and recursively delete insns that compute values used only
3015 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3017 Look at all our REG_DEAD notes. If a previous insn does nothing other
3018 than set a register that dies in this insn, we can delete that insn
3019 as well.
3021 On machines with CC0, if CC0 is used in this insn, we may be able to
3022 delete the insn that set it. */
3024 static void
3025 delete_computation (rtx insn)
3027 rtx note, next;
3029 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
3031 rtx_insn *prev = prev_nonnote_insn (insn);
3032 /* We assume that at this stage
3033 CC's are always set explicitly
3034 and always immediately before the jump that
3035 will use them. So if the previous insn
3036 exists to set the CC's, delete it
3037 (unless it performs auto-increments, etc.). */
3038 if (prev && NONJUMP_INSN_P (prev)
3039 && sets_cc0_p (PATTERN (prev)))
3041 if (sets_cc0_p (PATTERN (prev)) > 0
3042 && ! side_effects_p (PATTERN (prev)))
3043 delete_computation (prev);
3044 else
3045 /* Otherwise, show that cc0 won't be used. */
3046 add_reg_note (prev, REG_UNUSED, cc0_rtx);
3050 for (note = REG_NOTES (insn); note; note = next)
3052 next = XEXP (note, 1);
3054 if (REG_NOTE_KIND (note) != REG_DEAD
3055 /* Verify that the REG_NOTE is legitimate. */
3056 || !REG_P (XEXP (note, 0)))
3057 continue;
3059 delete_prior_computation (note, insn);
3062 delete_related_insns (insn);
3065 /* If all INSN does is set the pc, delete it,
3066 and delete the insn that set the condition codes for it
3067 if that's what the previous thing was. */
3069 static void
3070 delete_jump (rtx_insn *insn)
3072 rtx set = single_set (insn);
3074 if (set && GET_CODE (SET_DEST (set)) == PC)
3075 delete_computation (insn);
3078 static rtx_insn *
3079 label_before_next_insn (rtx x, rtx scan_limit)
3081 rtx_insn *insn = next_active_insn (x);
3082 while (insn)
3084 insn = PREV_INSN (insn);
3085 if (insn == scan_limit || insn == NULL_RTX)
3086 return NULL;
3087 if (LABEL_P (insn))
3088 break;
3090 return insn;
3093 /* Return TRUE if there is a NOTE_INSN_SWITCH_TEXT_SECTIONS note in between
3094 BEG and END. */
3096 static bool
3097 switch_text_sections_between_p (const rtx_insn *beg, const rtx_insn *end)
3099 const rtx_insn *p;
3100 for (p = beg; p != end; p = NEXT_INSN (p))
3101 if (NOTE_P (p) && NOTE_KIND (p) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
3102 return true;
3103 return false;
3107 /* Once we have tried two ways to fill a delay slot, make a pass over the
3108 code to try to improve the results and to do such things as more jump
3109 threading. */
3111 static void
3112 relax_delay_slots (rtx_insn *first)
3114 rtx_insn *insn, *next;
3115 rtx_sequence *pat;
3116 rtx trial;
3117 rtx_insn *delay_insn;
3118 rtx target_label;
3120 /* Look at every JUMP_INSN and see if we can improve it. */
3121 for (insn = first; insn; insn = next)
3123 rtx_insn *other;
3124 bool crossing;
3126 next = next_active_insn (insn);
3128 /* If this is a jump insn, see if it now jumps to a jump, jumps to
3129 the next insn, or jumps to a label that is not the last of a
3130 group of consecutive labels. */
3131 if (is_a <rtx_jump_insn *> (insn)
3132 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3133 && !ANY_RETURN_P (target_label = JUMP_LABEL (insn)))
3135 rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (insn);
3136 target_label
3137 = skip_consecutive_labels (follow_jumps (target_label, jump_insn,
3138 &crossing));
3139 if (ANY_RETURN_P (target_label))
3140 target_label = find_end_label (target_label);
3142 if (target_label && next_active_insn (target_label) == next
3143 && ! condjump_in_parallel_p (jump_insn)
3144 && ! (next && switch_text_sections_between_p (jump_insn, next)))
3146 delete_jump (jump_insn);
3147 continue;
3150 if (target_label && target_label != JUMP_LABEL (jump_insn))
3152 reorg_redirect_jump (jump_insn, target_label);
3153 if (crossing)
3154 CROSSING_JUMP_P (jump_insn) = 1;
3157 /* See if this jump conditionally branches around an unconditional
3158 jump. If so, invert this jump and point it to the target of the
3159 second jump. Check if it's possible on the target. */
3160 if (next && simplejump_or_return_p (next)
3161 && any_condjump_p (jump_insn)
3162 && target_label
3163 && next_active_insn (target_label) == next_active_insn (next)
3164 && no_labels_between_p (jump_insn, next)
3165 && targetm.can_follow_jump (jump_insn, next))
3167 rtx label = JUMP_LABEL (next);
3169 /* Be careful how we do this to avoid deleting code or
3170 labels that are momentarily dead. See similar optimization
3171 in jump.c.
3173 We also need to ensure we properly handle the case when
3174 invert_jump fails. */
3176 ++LABEL_NUSES (target_label);
3177 if (!ANY_RETURN_P (label))
3178 ++LABEL_NUSES (label);
3180 if (invert_jump (jump_insn, label, 1))
3182 delete_related_insns (next);
3183 next = jump_insn;
3186 if (!ANY_RETURN_P (label))
3187 --LABEL_NUSES (label);
3189 if (--LABEL_NUSES (target_label) == 0)
3190 delete_related_insns (target_label);
3192 continue;
3196 /* If this is an unconditional jump and the previous insn is a
3197 conditional jump, try reversing the condition of the previous
3198 insn and swapping our targets. The next pass might be able to
3199 fill the slots.
3201 Don't do this if we expect the conditional branch to be true, because
3202 we would then be making the more common case longer. */
3204 if (simplejump_or_return_p (insn)
3205 && (other = prev_active_insn (insn)) != 0
3206 && any_condjump_p (other)
3207 && no_labels_between_p (other, insn)
3208 && 0 > mostly_true_jump (other))
3210 rtx other_target = JUMP_LABEL (other);
3211 target_label = JUMP_LABEL (insn);
3213 if (invert_jump (as_a <rtx_jump_insn *> (other), target_label, 0))
3214 reorg_redirect_jump (as_a <rtx_jump_insn *> (insn), other_target);
3217 /* Now look only at cases where we have a filled delay slot. */
3218 if (!NONJUMP_INSN_P (insn) || GET_CODE (PATTERN (insn)) != SEQUENCE)
3219 continue;
3221 pat = as_a <rtx_sequence *> (PATTERN (insn));
3222 delay_insn = pat->insn (0);
3224 /* See if the first insn in the delay slot is redundant with some
3225 previous insn. Remove it from the delay slot if so; then set up
3226 to reprocess this insn. */
3227 if (redundant_insn (pat->insn (1), delay_insn, vNULL))
3229 update_block (pat->insn (1), insn);
3230 delete_from_delay_slot (pat->insn (1));
3231 next = prev_active_insn (next);
3232 continue;
3235 /* See if we have a RETURN insn with a filled delay slot followed
3236 by a RETURN insn with an unfilled a delay slot. If so, we can delete
3237 the first RETURN (but not its delay insn). This gives the same
3238 effect in fewer instructions.
3240 Only do so if optimizing for size since this results in slower, but
3241 smaller code. */
3242 if (optimize_function_for_size_p (cfun)
3243 && ANY_RETURN_P (PATTERN (delay_insn))
3244 && next
3245 && JUMP_P (next)
3246 && PATTERN (next) == PATTERN (delay_insn))
3248 rtx_insn *after;
3249 int i;
3251 /* Delete the RETURN and just execute the delay list insns.
3253 We do this by deleting the INSN containing the SEQUENCE, then
3254 re-emitting the insns separately, and then deleting the RETURN.
3255 This allows the count of the jump target to be properly
3256 decremented.
3258 Note that we need to change the INSN_UID of the re-emitted insns
3259 since it is used to hash the insns for mark_target_live_regs and
3260 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3262 Clear the from target bit, since these insns are no longer
3263 in delay slots. */
3264 for (i = 0; i < XVECLEN (pat, 0); i++)
3265 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3267 trial = PREV_INSN (insn);
3268 delete_related_insns (insn);
3269 gcc_assert (GET_CODE (pat) == SEQUENCE);
3270 add_insn_after (delay_insn, trial, NULL);
3271 after = delay_insn;
3272 for (i = 1; i < pat->len (); i++)
3273 after = emit_copy_of_insn_after (pat->insn (i), after);
3274 delete_scheduled_jump (delay_insn);
3275 continue;
3278 /* Now look only at the cases where we have a filled JUMP_INSN. */
3279 rtx_jump_insn *delay_jump_insn =
3280 dyn_cast <rtx_jump_insn *> (delay_insn);
3281 if (! delay_jump_insn || !(condjump_p (delay_jump_insn)
3282 || condjump_in_parallel_p (delay_jump_insn)))
3283 continue;
3285 target_label = JUMP_LABEL (delay_jump_insn);
3286 if (target_label && ANY_RETURN_P (target_label))
3287 continue;
3289 /* If this jump goes to another unconditional jump, thread it, but
3290 don't convert a jump into a RETURN here. */
3291 trial = skip_consecutive_labels (follow_jumps (target_label,
3292 delay_jump_insn,
3293 &crossing));
3294 if (ANY_RETURN_P (trial))
3295 trial = find_end_label (trial);
3297 if (trial && trial != target_label
3298 && redirect_with_delay_slots_safe_p (delay_jump_insn, trial, insn))
3300 reorg_redirect_jump (delay_jump_insn, trial);
3301 target_label = trial;
3302 if (crossing)
3303 CROSSING_JUMP_P (insn) = 1;
3306 /* If the first insn at TARGET_LABEL is redundant with a previous
3307 insn, redirect the jump to the following insn and process again.
3308 We use next_real_insn instead of next_active_insn so we
3309 don't skip USE-markers, or we'll end up with incorrect
3310 liveness info. */
3311 trial = next_real_insn (target_label);
3312 if (trial && GET_CODE (PATTERN (trial)) != SEQUENCE
3313 && redundant_insn (trial, insn, vNULL)
3314 && ! can_throw_internal (trial))
3316 /* Figure out where to emit the special USE insn so we don't
3317 later incorrectly compute register live/death info. */
3318 rtx_insn *tmp = next_active_insn (trial);
3319 if (tmp == 0)
3320 tmp = find_end_label (simple_return_rtx);
3322 if (tmp)
3324 /* Insert the special USE insn and update dataflow info.
3325 We know "trial" is an insn here as it is the output of
3326 next_real_insn () above. */
3327 update_block (as_a <rtx_insn *> (trial), tmp);
3329 /* Now emit a label before the special USE insn, and
3330 redirect our jump to the new label. */
3331 target_label = get_label_before (PREV_INSN (tmp), target_label);
3332 reorg_redirect_jump (delay_jump_insn, target_label);
3333 next = insn;
3334 continue;
3338 /* Similarly, if it is an unconditional jump with one insn in its
3339 delay list and that insn is redundant, thread the jump. */
3340 rtx_sequence *trial_seq =
3341 trial ? dyn_cast <rtx_sequence *> (PATTERN (trial)) : NULL;
3342 if (trial_seq
3343 && trial_seq->len () == 2
3344 && JUMP_P (trial_seq->insn (0))
3345 && simplejump_or_return_p (trial_seq->insn (0))
3346 && redundant_insn (trial_seq->insn (1), insn, vNULL))
3348 target_label = JUMP_LABEL (trial_seq->insn (0));
3349 if (ANY_RETURN_P (target_label))
3350 target_label = find_end_label (target_label);
3352 if (target_label
3353 && redirect_with_delay_slots_safe_p (delay_jump_insn,
3354 target_label, insn))
3356 update_block (trial_seq->insn (1), insn);
3357 reorg_redirect_jump (delay_jump_insn, target_label);
3358 next = insn;
3359 continue;
3363 /* See if we have a simple (conditional) jump that is useless. */
3364 if (! INSN_ANNULLED_BRANCH_P (delay_jump_insn)
3365 && ! condjump_in_parallel_p (delay_jump_insn)
3366 && prev_active_insn (target_label) == insn
3367 && ! BARRIER_P (prev_nonnote_insn (target_label))
3368 /* If the last insn in the delay slot sets CC0 for some insn,
3369 various code assumes that it is in a delay slot. We could
3370 put it back where it belonged and delete the register notes,
3371 but it doesn't seem worthwhile in this uncommon case. */
3372 && (!HAVE_cc0
3373 || ! find_reg_note (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1),
3374 REG_CC_USER, NULL_RTX)))
3376 rtx_insn *after;
3377 int i;
3379 /* All this insn does is execute its delay list and jump to the
3380 following insn. So delete the jump and just execute the delay
3381 list insns.
3383 We do this by deleting the INSN containing the SEQUENCE, then
3384 re-emitting the insns separately, and then deleting the jump.
3385 This allows the count of the jump target to be properly
3386 decremented.
3388 Note that we need to change the INSN_UID of the re-emitted insns
3389 since it is used to hash the insns for mark_target_live_regs and
3390 the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3392 Clear the from target bit, since these insns are no longer
3393 in delay slots. */
3394 for (i = 0; i < XVECLEN (pat, 0); i++)
3395 INSN_FROM_TARGET_P (XVECEXP (pat, 0, i)) = 0;
3397 trial = PREV_INSN (insn);
3398 delete_related_insns (insn);
3399 gcc_assert (GET_CODE (pat) == SEQUENCE);
3400 add_insn_after (delay_jump_insn, trial, NULL);
3401 after = delay_jump_insn;
3402 for (i = 1; i < pat->len (); i++)
3403 after = emit_copy_of_insn_after (pat->insn (i), after);
3404 delete_scheduled_jump (delay_jump_insn);
3405 continue;
3408 /* See if this is an unconditional jump around a single insn which is
3409 identical to the one in its delay slot. In this case, we can just
3410 delete the branch and the insn in its delay slot. */
3411 if (next && NONJUMP_INSN_P (next)
3412 && label_before_next_insn (next, insn) == target_label
3413 && simplejump_p (insn)
3414 && XVECLEN (pat, 0) == 2
3415 && rtx_equal_p (PATTERN (next), PATTERN (pat->insn (1))))
3417 delete_related_insns (insn);
3418 continue;
3421 /* See if this jump (with its delay slots) conditionally branches
3422 around an unconditional jump (without delay slots). If so, invert
3423 this jump and point it to the target of the second jump. We cannot
3424 do this for annulled jumps, though. Again, don't convert a jump to
3425 a RETURN here. */
3426 if (! INSN_ANNULLED_BRANCH_P (delay_jump_insn)
3427 && any_condjump_p (delay_jump_insn)
3428 && next && simplejump_or_return_p (next)
3429 && next_active_insn (target_label) == next_active_insn (next)
3430 && no_labels_between_p (insn, next))
3432 rtx label = JUMP_LABEL (next);
3433 rtx old_label = JUMP_LABEL (delay_jump_insn);
3435 if (ANY_RETURN_P (label))
3436 label = find_end_label (label);
3438 /* find_end_label can generate a new label. Check this first. */
3439 if (label
3440 && no_labels_between_p (insn, next)
3441 && redirect_with_delay_slots_safe_p (delay_jump_insn,
3442 label, insn))
3444 /* Be careful how we do this to avoid deleting code or labels
3445 that are momentarily dead. See similar optimization in
3446 jump.c */
3447 if (old_label)
3448 ++LABEL_NUSES (old_label);
3450 if (invert_jump (delay_jump_insn, label, 1))
3452 int i;
3454 /* Must update the INSN_FROM_TARGET_P bits now that
3455 the branch is reversed, so that mark_target_live_regs
3456 will handle the delay slot insn correctly. */
3457 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
3459 rtx slot = XVECEXP (PATTERN (insn), 0, i);
3460 INSN_FROM_TARGET_P (slot) = ! INSN_FROM_TARGET_P (slot);
3463 delete_related_insns (next);
3464 next = insn;
3467 if (old_label && --LABEL_NUSES (old_label) == 0)
3468 delete_related_insns (old_label);
3469 continue;
3473 /* If we own the thread opposite the way this insn branches, see if we
3474 can merge its delay slots with following insns. */
3475 if (INSN_FROM_TARGET_P (pat->insn (1))
3476 && own_thread_p (NEXT_INSN (insn), 0, 1))
3477 try_merge_delay_insns (insn, next);
3478 else if (! INSN_FROM_TARGET_P (pat->insn (1))
3479 && own_thread_p (target_label, target_label, 0))
3480 try_merge_delay_insns (insn, next_active_insn (target_label));
3482 /* If we get here, we haven't deleted INSN. But we may have deleted
3483 NEXT, so recompute it. */
3484 next = next_active_insn (insn);
3489 /* Look for filled jumps to the end of function label. We can try to convert
3490 them into RETURN insns if the insns in the delay slot are valid for the
3491 RETURN as well. */
3493 static void
3494 make_return_insns (rtx_insn *first)
3496 rtx_insn *insn;
3497 rtx_jump_insn *jump_insn;
3498 rtx real_return_label = function_return_label;
3499 rtx real_simple_return_label = function_simple_return_label;
3500 int slots, i;
3502 /* See if there is a RETURN insn in the function other than the one we
3503 made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3504 into a RETURN to jump to it. */
3505 for (insn = first; insn; insn = NEXT_INSN (insn))
3506 if (JUMP_P (insn) && ANY_RETURN_P (PATTERN (insn)))
3508 rtx t = get_label_before (insn, NULL_RTX);
3509 if (PATTERN (insn) == ret_rtx)
3510 real_return_label = t;
3511 else
3512 real_simple_return_label = t;
3513 break;
3516 /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3517 was equal to END_OF_FUNCTION_LABEL. */
3518 if (real_return_label)
3519 LABEL_NUSES (real_return_label)++;
3520 if (real_simple_return_label)
3521 LABEL_NUSES (real_simple_return_label)++;
3523 /* Clear the list of insns to fill so we can use it. */
3524 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3526 for (insn = first; insn; insn = NEXT_INSN (insn))
3528 int flags;
3529 rtx kind, real_label;
3531 /* Only look at filled JUMP_INSNs that go to the end of function
3532 label. */
3533 if (!NONJUMP_INSN_P (insn))
3534 continue;
3536 if (GET_CODE (PATTERN (insn)) != SEQUENCE)
3537 continue;
3539 rtx_sequence *pat = as_a <rtx_sequence *> (PATTERN (insn));
3541 if (!jump_to_label_p (pat->insn (0)))
3542 continue;
3544 if (JUMP_LABEL (pat->insn (0)) == function_return_label)
3546 kind = ret_rtx;
3547 real_label = real_return_label;
3549 else if (JUMP_LABEL (pat->insn (0)) == function_simple_return_label)
3551 kind = simple_return_rtx;
3552 real_label = real_simple_return_label;
3554 else
3555 continue;
3557 jump_insn = as_a <rtx_jump_insn *> (pat->insn (0));
3559 /* If we can't make the jump into a RETURN, try to redirect it to the best
3560 RETURN and go on to the next insn. */
3561 if (!reorg_redirect_jump (jump_insn, kind))
3563 /* Make sure redirecting the jump will not invalidate the delay
3564 slot insns. */
3565 if (redirect_with_delay_slots_safe_p (jump_insn, real_label, insn))
3566 reorg_redirect_jump (jump_insn, real_label);
3567 continue;
3570 /* See if this RETURN can accept the insns current in its delay slot.
3571 It can if it has more or an equal number of slots and the contents
3572 of each is valid. */
3574 flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3575 slots = num_delay_slots (jump_insn);
3576 if (slots >= XVECLEN (pat, 0) - 1)
3578 for (i = 1; i < XVECLEN (pat, 0); i++)
3579 if (! (
3580 #if ANNUL_IFFALSE_SLOTS
3581 (INSN_ANNULLED_BRANCH_P (jump_insn)
3582 && INSN_FROM_TARGET_P (pat->insn (i)))
3583 ? eligible_for_annul_false (jump_insn, i - 1,
3584 pat->insn (i), flags) :
3585 #endif
3586 #if ANNUL_IFTRUE_SLOTS
3587 (INSN_ANNULLED_BRANCH_P (jump_insn)
3588 && ! INSN_FROM_TARGET_P (pat->insn (i)))
3589 ? eligible_for_annul_true (jump_insn, i - 1,
3590 pat->insn (i), flags) :
3591 #endif
3592 eligible_for_delay (jump_insn, i - 1,
3593 pat->insn (i), flags)))
3594 break;
3596 else
3597 i = 0;
3599 if (i == XVECLEN (pat, 0))
3600 continue;
3602 /* We have to do something with this insn. If it is an unconditional
3603 RETURN, delete the SEQUENCE and output the individual insns,
3604 followed by the RETURN. Then set things up so we try to find
3605 insns for its delay slots, if it needs some. */
3606 if (ANY_RETURN_P (PATTERN (jump_insn)))
3608 rtx_insn *prev = PREV_INSN (insn);
3610 delete_related_insns (insn);
3611 for (i = 1; i < XVECLEN (pat, 0); i++)
3612 prev = emit_insn_after (PATTERN (XVECEXP (pat, 0, i)), prev);
3614 insn = emit_jump_insn_after (PATTERN (jump_insn), prev);
3615 emit_barrier_after (insn);
3617 if (slots)
3618 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3620 else
3621 /* It is probably more efficient to keep this with its current
3622 delay slot as a branch to a RETURN. */
3623 reorg_redirect_jump (jump_insn, real_label);
3626 /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3627 new delay slots we have created. */
3628 if (real_return_label != NULL_RTX && --LABEL_NUSES (real_return_label) == 0)
3629 delete_related_insns (real_return_label);
3630 if (real_simple_return_label != NULL_RTX
3631 && --LABEL_NUSES (real_simple_return_label) == 0)
3632 delete_related_insns (real_simple_return_label);
3634 fill_simple_delay_slots (1);
3635 fill_simple_delay_slots (0);
3638 /* Try to find insns to place in delay slots. */
3640 static void
3641 dbr_schedule (rtx_insn *first)
3643 rtx_insn *insn, *next, *epilogue_insn = 0;
3644 int i;
3645 bool need_return_insns;
3647 /* If the current function has no insns other than the prologue and
3648 epilogue, then do not try to fill any delay slots. */
3649 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
3650 return;
3652 /* Find the highest INSN_UID and allocate and initialize our map from
3653 INSN_UID's to position in code. */
3654 for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3656 if (INSN_UID (insn) > max_uid)
3657 max_uid = INSN_UID (insn);
3658 if (NOTE_P (insn)
3659 && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
3660 epilogue_insn = insn;
3663 uid_to_ruid = XNEWVEC (int, max_uid + 1);
3664 for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3665 uid_to_ruid[INSN_UID (insn)] = i;
3667 /* Initialize the list of insns that need filling. */
3668 if (unfilled_firstobj == 0)
3670 gcc_obstack_init (&unfilled_slots_obstack);
3671 unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3674 for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3676 rtx target;
3678 /* Skip vector tables. We can't get attributes for them. */
3679 if (JUMP_TABLE_DATA_P (insn))
3680 continue;
3682 if (JUMP_P (insn))
3683 INSN_ANNULLED_BRANCH_P (insn) = 0;
3684 INSN_FROM_TARGET_P (insn) = 0;
3686 if (num_delay_slots (insn) > 0)
3687 obstack_ptr_grow (&unfilled_slots_obstack, insn);
3689 /* Ensure all jumps go to the last of a set of consecutive labels. */
3690 if (JUMP_P (insn)
3691 && (condjump_p (insn) || condjump_in_parallel_p (insn))
3692 && !ANY_RETURN_P (JUMP_LABEL (insn))
3693 && ((target = skip_consecutive_labels (JUMP_LABEL (insn)))
3694 != JUMP_LABEL (insn)))
3695 redirect_jump (as_a <rtx_jump_insn *> (insn), target, 1);
3698 init_resource_info (epilogue_insn);
3700 /* Show we haven't computed an end-of-function label yet. */
3701 function_return_label = function_simple_return_label = NULL;
3703 /* Initialize the statistics for this function. */
3704 memset (num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3705 memset (num_filled_delays, 0, sizeof num_filled_delays);
3707 /* Now do the delay slot filling. Try everything twice in case earlier
3708 changes make more slots fillable. */
3710 for (reorg_pass_number = 0;
3711 reorg_pass_number < MAX_REORG_PASSES;
3712 reorg_pass_number++)
3714 fill_simple_delay_slots (1);
3715 fill_simple_delay_slots (0);
3716 if (!targetm.no_speculation_in_delay_slots_p ())
3717 fill_eager_delay_slots ();
3718 relax_delay_slots (first);
3721 /* If we made an end of function label, indicate that it is now
3722 safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3723 If it is now unused, delete it. */
3724 if (function_return_label && --LABEL_NUSES (function_return_label) == 0)
3725 delete_related_insns (function_return_label);
3726 if (function_simple_return_label
3727 && --LABEL_NUSES (function_simple_return_label) == 0)
3728 delete_related_insns (function_simple_return_label);
3730 need_return_insns = false;
3731 need_return_insns |= targetm.have_return () && function_return_label != 0;
3732 need_return_insns |= (targetm.have_simple_return ()
3733 && function_simple_return_label != 0);
3734 if (need_return_insns)
3735 make_return_insns (first);
3737 /* Delete any USE insns made by update_block; subsequent passes don't need
3738 them or know how to deal with them. */
3739 for (insn = first; insn; insn = next)
3741 next = NEXT_INSN (insn);
3743 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
3744 && INSN_P (XEXP (PATTERN (insn), 0)))
3745 next = delete_related_insns (insn);
3748 obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3750 /* It is not clear why the line below is needed, but it does seem to be. */
3751 unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3753 if (dump_file)
3755 int i, j, need_comma;
3756 int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3757 int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
3759 for (reorg_pass_number = 0;
3760 reorg_pass_number < MAX_REORG_PASSES;
3761 reorg_pass_number++)
3763 fprintf (dump_file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
3764 for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
3766 need_comma = 0;
3767 fprintf (dump_file, ";; Reorg function #%d\n", i);
3769 fprintf (dump_file, ";; %d insns needing delay slots\n;; ",
3770 num_insns_needing_delays[i][reorg_pass_number]);
3772 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3773 if (num_filled_delays[i][j][reorg_pass_number])
3775 if (need_comma)
3776 fprintf (dump_file, ", ");
3777 need_comma = 1;
3778 fprintf (dump_file, "%d got %d delays",
3779 num_filled_delays[i][j][reorg_pass_number], j);
3781 fprintf (dump_file, "\n");
3784 memset (total_delay_slots, 0, sizeof total_delay_slots);
3785 memset (total_annul_slots, 0, sizeof total_annul_slots);
3786 for (insn = first; insn; insn = NEXT_INSN (insn))
3788 if (! insn->deleted ()
3789 && NONJUMP_INSN_P (insn)
3790 && GET_CODE (PATTERN (insn)) != USE
3791 && GET_CODE (PATTERN (insn)) != CLOBBER)
3793 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3795 rtx control;
3796 j = XVECLEN (PATTERN (insn), 0) - 1;
3797 if (j > MAX_DELAY_HISTOGRAM)
3798 j = MAX_DELAY_HISTOGRAM;
3799 control = XVECEXP (PATTERN (insn), 0, 0);
3800 if (JUMP_P (control) && INSN_ANNULLED_BRANCH_P (control))
3801 total_annul_slots[j]++;
3802 else
3803 total_delay_slots[j]++;
3805 else if (num_delay_slots (insn) > 0)
3806 total_delay_slots[0]++;
3809 fprintf (dump_file, ";; Reorg totals: ");
3810 need_comma = 0;
3811 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3813 if (total_delay_slots[j])
3815 if (need_comma)
3816 fprintf (dump_file, ", ");
3817 need_comma = 1;
3818 fprintf (dump_file, "%d got %d delays", total_delay_slots[j], j);
3821 fprintf (dump_file, "\n");
3823 if (ANNUL_IFTRUE_SLOTS || ANNUL_IFFALSE_SLOTS)
3825 fprintf (dump_file, ";; Reorg annuls: ");
3826 need_comma = 0;
3827 for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3829 if (total_annul_slots[j])
3831 if (need_comma)
3832 fprintf (dump_file, ", ");
3833 need_comma = 1;
3834 fprintf (dump_file, "%d got %d delays", total_annul_slots[j], j);
3837 fprintf (dump_file, "\n");
3840 fprintf (dump_file, "\n");
3843 if (!sibling_labels.is_empty ())
3845 update_alignments (sibling_labels);
3846 sibling_labels.release ();
3849 free_resource_info ();
3850 free (uid_to_ruid);
3851 crtl->dbr_scheduled_p = true;
3854 /* Run delay slot optimization. */
3855 static unsigned int
3856 rest_of_handle_delay_slots (void)
3858 if (DELAY_SLOTS)
3859 dbr_schedule (get_insns ());
3861 return 0;
3864 namespace {
3866 const pass_data pass_data_delay_slots =
3868 RTL_PASS, /* type */
3869 "dbr", /* name */
3870 OPTGROUP_NONE, /* optinfo_flags */
3871 TV_DBR_SCHED, /* tv_id */
3872 0, /* properties_required */
3873 0, /* properties_provided */
3874 0, /* properties_destroyed */
3875 0, /* todo_flags_start */
3876 0, /* todo_flags_finish */
3879 class pass_delay_slots : public rtl_opt_pass
3881 public:
3882 pass_delay_slots (gcc::context *ctxt)
3883 : rtl_opt_pass (pass_data_delay_slots, ctxt)
3886 /* opt_pass methods: */
3887 virtual bool gate (function *);
3888 virtual unsigned int execute (function *)
3890 return rest_of_handle_delay_slots ();
3893 }; // class pass_delay_slots
3895 bool
3896 pass_delay_slots::gate (function *)
3898 /* At -O0 dataflow info isn't updated after RA. */
3899 if (DELAY_SLOTS)
3900 return optimize > 0 && flag_delayed_branch && !crtl->dbr_scheduled_p;
3902 return false;
3905 } // anon namespace
3907 rtl_opt_pass *
3908 make_pass_delay_slots (gcc::context *ctxt)
3910 return new pass_delay_slots (ctxt);
3913 /* Machine dependent reorg pass. */
3915 namespace {
3917 const pass_data pass_data_machine_reorg =
3919 RTL_PASS, /* type */
3920 "mach", /* name */
3921 OPTGROUP_NONE, /* optinfo_flags */
3922 TV_MACH_DEP, /* tv_id */
3923 0, /* properties_required */
3924 0, /* properties_provided */
3925 0, /* properties_destroyed */
3926 0, /* todo_flags_start */
3927 0, /* todo_flags_finish */
3930 class pass_machine_reorg : public rtl_opt_pass
3932 public:
3933 pass_machine_reorg (gcc::context *ctxt)
3934 : rtl_opt_pass (pass_data_machine_reorg, ctxt)
3937 /* opt_pass methods: */
3938 virtual bool gate (function *)
3940 return targetm.machine_dependent_reorg != 0;
3943 virtual unsigned int execute (function *)
3945 targetm.machine_dependent_reorg ();
3946 return 0;
3949 }; // class pass_machine_reorg
3951 } // anon namespace
3953 rtl_opt_pass *
3954 make_pass_machine_reorg (gcc::context *ctxt)
3956 return new pass_machine_reorg (ctxt);