Line data Source code
1 : /* Perform instruction reorganizations for delay slot filling.
2 : Copyright (C) 1992-2026 Free Software Foundation, Inc.
3 : Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu).
4 : Hacked by Michael Tiemann (tiemann@cygnus.com).
5 :
6 : This file is part of GCC.
7 :
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.
12 :
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.
17 :
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/>. */
21 :
22 : /* Instruction reorganization pass.
23 :
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').
31 :
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:
36 :
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.
41 :
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.
47 :
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.
54 :
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.
58 :
59 : Three techniques for filling delay slots have been implemented so far:
60 :
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.
69 :
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.
82 :
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. */
92 :
93 : #include "config.h"
94 : #include "system.h"
95 : #include "coretypes.h"
96 : #include "backend.h"
97 : #include "target.h"
98 : #include "rtl.h"
99 : #include "tree.h"
100 : #include "predict.h"
101 : #include "memmodel.h"
102 : #include "tm_p.h"
103 : #include "expmed.h"
104 : #include "insn-config.h"
105 : #include "emit-rtl.h"
106 : #include "recog.h"
107 : #include "insn-attr.h"
108 : #include "resource.h"
109 : #include "tree-pass.h"
110 :
111 :
112 : /* First, some functions that were used before GCC got a control flow graph.
113 : These functions are now only used here in reorg.cc, and have therefore
114 : been moved here to avoid inadvertent misuse elsewhere in the compiler. */
115 :
116 : /* Return the last label to mark the same position as LABEL. Return LABEL
117 : itself if it is null or any return rtx. */
118 :
119 : static rtx
120 0 : skip_consecutive_labels (rtx label_or_return)
121 : {
122 0 : rtx_insn *insn;
123 :
124 0 : if (label_or_return && ANY_RETURN_P (label_or_return))
125 : return label_or_return;
126 :
127 0 : rtx_insn *label = as_a <rtx_insn *> (label_or_return);
128 :
129 : /* __builtin_unreachable can create a CODE_LABEL followed by a BARRIER.
130 :
131 : Since reaching the CODE_LABEL is undefined behavior, we can return
132 : any code label and we're OK at run time.
133 :
134 : However, if we return a CODE_LABEL which leads to a shrink-wrapped
135 : epilogue, but the path does not have a prologue, then we will trip
136 : a sanity check in the dwarf2 cfi code which wants to verify that
137 : the CFIs are all the same on the traces leading to the epilogue.
138 :
139 : So we explicitly disallow looking through BARRIERS here. */
140 0 : for (insn = label;
141 0 : insn != 0 && !NONDEBUG_INSN_P (insn) && !BARRIER_P (insn);
142 0 : insn = NEXT_INSN (insn))
143 0 : if (LABEL_P (insn))
144 0 : label = insn;
145 :
146 : return label;
147 : }
148 :
149 : /* Insns which have delay slots that have not yet been filled. */
150 :
151 : static struct obstack unfilled_slots_obstack;
152 : static rtx *unfilled_firstobj;
153 :
154 : /* Define macros to refer to the first and last slot containing unfilled
155 : insns. These are used because the list may move and its address
156 : should be recomputed at each use. */
157 :
158 : #define unfilled_slots_base \
159 : ((rtx_insn **) obstack_base (&unfilled_slots_obstack))
160 :
161 : #define unfilled_slots_next \
162 : ((rtx_insn **) obstack_next_free (&unfilled_slots_obstack))
163 :
164 : /* Points to the label before the end of the function, or before a
165 : return insn. */
166 : static rtx_code_label *function_return_label;
167 : /* Likewise for a simple_return. */
168 : static rtx_code_label *function_simple_return_label;
169 :
170 : /* Mapping between INSN_UID's and position in the code since INSN_UID's do
171 : not always monotonically increase. */
172 : static int *uid_to_ruid;
173 :
174 : /* Highest valid index in `uid_to_ruid'. */
175 : static int max_uid;
176 :
177 : static bool stop_search_p (rtx_insn *, bool);
178 : static bool resource_conflicts_p (struct resources *, struct resources *);
179 : static bool insn_references_resource_p (rtx, struct resources *, bool);
180 : static bool insn_sets_resource_p (rtx, struct resources *, bool);
181 : static rtx_code_label *find_end_label (rtx);
182 : static rtx_insn *emit_delay_sequence (rtx_insn *, const vec<rtx_insn *> &,
183 : int);
184 : static void add_to_delay_list (rtx_insn *, vec<rtx_insn *> *);
185 : static rtx_insn *delete_from_delay_slot (rtx_insn *);
186 : static void delete_scheduled_jump (rtx_insn *);
187 : static void note_delay_statistics (int, int);
188 : static int get_jump_flags (const rtx_insn *, rtx);
189 : static int mostly_true_jump (rtx_insn *);
190 : static rtx get_branch_condition (const rtx_jump_insn *, rtx);
191 : static bool condition_dominates_p (rtx, const rtx_jump_insn *);
192 : static bool redirect_with_delay_slots_safe_p (rtx_jump_insn *,
193 : rtx, rtx_insn *);
194 : static bool redirect_with_delay_list_safe_p (rtx_jump_insn *, rtx,
195 : const vec<rtx_insn *> &);
196 : static bool check_annul_list_true_false (bool, const vec<rtx_insn *> &);
197 : static void steal_delay_list_from_target (rtx_insn *, rtx, rtx_sequence *,
198 : vec<rtx_insn *> *,
199 : struct resources *,
200 : struct resources *,
201 : struct resources *,
202 : int, int *, bool *,
203 : rtx *);
204 : static void steal_delay_list_from_fallthrough (rtx_insn *, rtx, rtx_sequence *,
205 : vec<rtx_insn *> *,
206 : struct resources *,
207 : struct resources *,
208 : struct resources *,
209 : int, int *, bool *);
210 : static void try_merge_delay_insns (rtx_insn *, rtx_insn *);
211 : static rtx_insn *redundant_insn (rtx_insn *, rtx_insn *,
212 : const vec<rtx_insn *> &);
213 : static bool own_thread_p (rtx, rtx, bool);
214 : static void update_block (rtx_insn *, rtx_insn *);
215 : static bool reorg_redirect_jump (rtx_jump_insn *, rtx);
216 : static void update_reg_dead_notes (rtx_insn *, rtx_insn *);
217 : static void fix_reg_dead_note (rtx_insn *, rtx_insn *);
218 : static void update_reg_unused_notes (rtx_insn *, rtx_insn *);
219 : static void fill_simple_delay_slots (bool);
220 : static void fill_slots_from_thread (rtx_jump_insn *, rtx, rtx, rtx,
221 : bool, bool, bool, int,
222 : int *, vec<rtx_insn *> *);
223 : static void fill_eager_delay_slots (void);
224 : static void relax_delay_slots (rtx_insn *);
225 : static void make_return_insns (rtx_insn *);
226 :
227 : /* A wrapper around next_active_insn which takes care to return ret_rtx
228 : unchanged. */
229 :
230 : static rtx
231 0 : first_active_target_insn (rtx insn)
232 : {
233 0 : if (ANY_RETURN_P (insn))
234 : return insn;
235 0 : return next_active_insn (as_a <rtx_insn *> (insn));
236 : }
237 :
238 : /* Return true iff INSN is a simplejump, or any kind of return insn. */
239 :
240 : static bool
241 0 : simplejump_or_return_p (rtx insn)
242 : {
243 0 : return (JUMP_P (insn)
244 0 : && (simplejump_p (as_a <rtx_insn *> (insn))
245 0 : || ANY_RETURN_P (PATTERN (insn))));
246 : }
247 :
248 : /* Return TRUE if this insn should stop the search for insn to fill delay
249 : slots. LABELS_P indicates that labels should terminate the search.
250 : In all cases, jumps terminate the search. */
251 :
252 : static bool
253 0 : stop_search_p (rtx_insn *insn, bool labels_p)
254 : {
255 0 : if (insn == 0)
256 : return true;
257 :
258 : /* If the insn can throw an exception that is caught within the function,
259 : it may effectively perform a jump from the viewpoint of the function.
260 : Therefore act like for a jump. */
261 0 : if (can_throw_internal (insn))
262 : return true;
263 :
264 0 : switch (GET_CODE (insn))
265 : {
266 : case NOTE:
267 : case CALL_INSN:
268 : case DEBUG_INSN:
269 : return false;
270 :
271 : case CODE_LABEL:
272 : return labels_p;
273 :
274 : case JUMP_INSN:
275 : case BARRIER:
276 : return true;
277 :
278 0 : case INSN:
279 : /* OK unless it contains a delay slot or is an `asm' insn of some type.
280 : We don't know anything about these. */
281 0 : return (is_a <rtx_sequence *> (PATTERN (insn))
282 0 : || GET_CODE (PATTERN (insn)) == ASM_INPUT
283 0 : || asm_noperands (PATTERN (insn)) >= 0);
284 :
285 0 : default:
286 0 : gcc_unreachable ();
287 : }
288 : }
289 :
290 : /* Return TRUE if any resources are marked in both RES1 and RES2 or if either
291 : resource set contains a volatile memory reference. Otherwise, return FALSE. */
292 :
293 : static bool
294 0 : resource_conflicts_p (struct resources *res1, struct resources *res2)
295 : {
296 0 : if ((res1->cc && res2->cc) || (res1->memory && res2->memory)
297 0 : || res1->volatil || res2->volatil)
298 : return true;
299 :
300 0 : return hard_reg_set_intersect_p (res1->regs, res2->regs);
301 : }
302 :
303 : /* Return TRUE if any resource marked in RES, a `struct resources', is
304 : referenced by INSN. If INCLUDE_DELAYED_EFFECTS is set, return if the called
305 : routine is using those resources.
306 :
307 : We compute this by computing all the resources referenced by INSN and
308 : seeing if this conflicts with RES. It might be faster to directly check
309 : ourselves, and this is the way it used to work, but it means duplicating
310 : a large block of complex code. */
311 :
312 : static bool
313 0 : insn_references_resource_p (rtx insn, struct resources *res,
314 : bool include_delayed_effects)
315 : {
316 0 : struct resources insn_res;
317 :
318 0 : CLEAR_RESOURCE (&insn_res);
319 0 : mark_referenced_resources (insn, &insn_res, include_delayed_effects);
320 0 : return resource_conflicts_p (&insn_res, res);
321 : }
322 :
323 : /* Return TRUE if INSN modifies resources that are marked in RES.
324 : INCLUDE_DELAYED_EFFECTS is set if the actions of that routine should be
325 : included. */
326 :
327 : static bool
328 0 : insn_sets_resource_p (rtx insn, struct resources *res,
329 : bool include_delayed_effects)
330 : {
331 0 : struct resources insn_sets;
332 :
333 0 : CLEAR_RESOURCE (&insn_sets);
334 0 : mark_set_resources (insn, &insn_sets, 0,
335 : (include_delayed_effects
336 : ? MARK_SRC_DEST_CALL
337 : : MARK_SRC_DEST));
338 0 : return resource_conflicts_p (&insn_sets, res);
339 : }
340 :
341 : /* Find a label before a RETURN. If there is none, try to make one; if this
342 : fails, return 0. KIND is either ret_rtx or simple_return_rtx, indicating
343 : which type of RETURN we're looking for.
344 :
345 : The property of the label is that it is placed just before a bare RETURN
346 : insn, so that another bare RETURN can be turned into a jump to the label
347 : unconditionally. In particular, the label cannot be placed before a
348 : RETURN insn with a filled delay slot.
349 :
350 : ??? There may be a problem with the current implementation. Suppose
351 : we start with a bare RETURN insn and call find_end_label. It may set
352 : function_return_label just before the RETURN. Suppose the machinery
353 : is able to fill the delay slot of the RETURN insn afterwards. Then
354 : function_return_label is no longer valid according to the property
355 : described above and find_end_label will still return it unmodified.
356 : Note that this is probably mitigated by the following observation:
357 : once function_return_label is made, it is very likely the target of
358 : a jump, so filling the delay slot of the RETURN will be much more
359 : difficult. */
360 :
361 : static rtx_code_label *
362 0 : find_end_label (rtx kind)
363 : {
364 0 : rtx_insn *insn;
365 0 : rtx_code_label **plabel;
366 :
367 0 : if (kind == ret_rtx)
368 0 : plabel = &function_return_label;
369 : else
370 : {
371 0 : gcc_assert (kind == simple_return_rtx);
372 0 : plabel = &function_simple_return_label;
373 : }
374 :
375 : /* If we found one previously, return it. */
376 0 : if (*plabel)
377 : return *plabel;
378 :
379 : /* Otherwise, scan the insns backward from the end of the function. */
380 0 : insn = get_last_insn ();
381 0 : while (NOTE_P (insn)
382 0 : || (NONJUMP_INSN_P (insn)
383 0 : && (GET_CODE (PATTERN (insn)) == USE
384 0 : || GET_CODE (PATTERN (insn)) == CLOBBER)))
385 0 : insn = PREV_INSN (insn);
386 :
387 : /* First, see if there is a RETURN at the end of the function. If so,
388 : put the label before it. */
389 0 : if (BARRIER_P (insn)
390 0 : && JUMP_P (PREV_INSN (insn))
391 0 : && PATTERN (PREV_INSN (insn)) == kind)
392 : {
393 0 : rtx_insn *temp = PREV_INSN (PREV_INSN (insn));
394 0 : rtx_code_label *label = gen_label_rtx ();
395 0 : LABEL_NUSES (label) = 0;
396 :
397 : /* Put the label before any USE insns that may precede the
398 : RETURN insn. */
399 0 : while (GET_CODE (temp) == USE)
400 0 : temp = PREV_INSN (temp);
401 :
402 0 : emit_label_after (label, temp);
403 0 : *plabel = label;
404 : }
405 :
406 : /* If the basic block reordering pass has moved the return insn to some
407 : other place, try to locate it again and put the label there. */
408 : else
409 : {
410 0 : rtx_code_label *label = gen_label_rtx ();
411 0 : LABEL_NUSES (label) = 0;
412 0 : while (insn && ! (JUMP_P (insn) && (PATTERN (insn) == kind)))
413 0 : insn = PREV_INSN (insn);
414 0 : if (insn)
415 : {
416 0 : insn = PREV_INSN (insn);
417 :
418 : /* Put the label before any USE insns that may precede the
419 : RETURN insn. */
420 0 : while (GET_CODE (insn) == USE)
421 0 : insn = PREV_INSN (insn);
422 :
423 0 : emit_label_after (label, insn);
424 : }
425 : else
426 : {
427 0 : if (targetm.have_epilogue () && ! targetm.have_return ())
428 : /* The RETURN insn has its delay slot filled so we cannot
429 : emit the label just before it. Since we already have
430 : an epilogue and cannot emit a new RETURN, we cannot
431 : emit the label at all. */
432 : return NULL;
433 :
434 : /* Otherwise, make a new label and emit a RETURN and BARRIER,
435 : if needed. */
436 0 : emit_label (label);
437 0 : if (targetm.have_return ())
438 : {
439 : /* The return we make may have delay slots too. */
440 0 : rtx_insn *pat = targetm.gen_return ();
441 0 : rtx_insn *insn = emit_jump_insn (pat);
442 0 : set_return_jump_label (insn);
443 0 : emit_barrier ();
444 0 : if (num_delay_slots (insn) > 0)
445 0 : obstack_ptr_grow (&unfilled_slots_obstack, insn);
446 : }
447 : }
448 0 : *plabel = label;
449 : }
450 :
451 : /* Show one additional use for this label so it won't go away until
452 : we are done. */
453 0 : ++LABEL_NUSES (*plabel);
454 :
455 0 : return *plabel;
456 : }
457 :
458 : /* Put INSN and LIST together in a SEQUENCE rtx of LENGTH, and replace
459 : the pattern of INSN with the SEQUENCE.
460 :
461 : Returns the insn containing the SEQUENCE that replaces INSN. */
462 :
463 : static rtx_insn *
464 0 : emit_delay_sequence (rtx_insn *insn, const vec<rtx_insn *> &list, int length)
465 : {
466 : /* Allocate the rtvec to hold the insns and the SEQUENCE. */
467 0 : rtvec seqv = rtvec_alloc (length + 1);
468 0 : rtx seq = gen_rtx_SEQUENCE (VOIDmode, seqv);
469 0 : rtx_insn *seq_insn = make_insn_raw (seq);
470 :
471 : /* If DELAY_INSN has a location, use it for SEQ_INSN. If DELAY_INSN does
472 : not have a location, but one of the delayed insns does, we pick up a
473 : location from there later. */
474 0 : INSN_LOCATION (seq_insn) = INSN_LOCATION (insn);
475 :
476 : /* Unlink INSN from the insn chain, so that we can put it into
477 : the SEQUENCE. Remember where we want to emit SEQUENCE in AFTER. */
478 0 : rtx_insn *after = PREV_INSN (insn);
479 0 : remove_insn (insn);
480 0 : SET_NEXT_INSN (insn) = SET_PREV_INSN (insn) = NULL;
481 :
482 : /* Build our SEQUENCE and rebuild the insn chain. */
483 0 : start_sequence ();
484 0 : XVECEXP (seq, 0, 0) = emit_insn (insn);
485 :
486 0 : unsigned int delay_insns = list.length ();
487 0 : gcc_assert (delay_insns == (unsigned int) length);
488 0 : for (unsigned int i = 0; i < delay_insns; i++)
489 : {
490 0 : rtx_insn *tem = list[i];
491 0 : rtx note, next;
492 :
493 : /* Show that this copy of the insn isn't deleted. */
494 0 : tem->set_undeleted ();
495 :
496 : /* Unlink insn from its original place, and re-emit it into
497 : the sequence. */
498 0 : SET_NEXT_INSN (tem) = SET_PREV_INSN (tem) = NULL;
499 0 : XVECEXP (seq, 0, i + 1) = emit_insn (tem);
500 :
501 : /* SPARC assembler, for instance, emit warning when debug info is output
502 : into the delay slot. */
503 0 : if (INSN_LOCATION (tem) && !INSN_LOCATION (seq_insn))
504 0 : INSN_LOCATION (seq_insn) = INSN_LOCATION (tem);
505 0 : INSN_LOCATION (tem) = 0;
506 :
507 0 : for (note = REG_NOTES (tem); note; note = next)
508 : {
509 0 : next = XEXP (note, 1);
510 0 : switch (REG_NOTE_KIND (note))
511 : {
512 0 : case REG_DEAD:
513 : /* Remove any REG_DEAD notes because we can't rely on them now
514 : that the insn has been moved. */
515 0 : remove_note (tem, note);
516 0 : break;
517 :
518 0 : case REG_LABEL_OPERAND:
519 0 : case REG_LABEL_TARGET:
520 : /* Keep the label reference count up to date. */
521 0 : if (LABEL_P (XEXP (note, 0)))
522 0 : LABEL_NUSES (XEXP (note, 0)) ++;
523 : break;
524 :
525 : default:
526 : break;
527 : }
528 : }
529 : }
530 0 : end_sequence ();
531 :
532 : /* Splice our SEQUENCE into the insn stream where INSN used to be. */
533 0 : add_insn_after (seq_insn, after, NULL);
534 :
535 0 : return seq_insn;
536 : }
537 :
538 : /* Add INSN to DELAY_LIST and return the head of the new list. The list must
539 : be in the order in which the insns are to be executed. */
540 :
541 : static void
542 0 : add_to_delay_list (rtx_insn *insn, vec<rtx_insn *> *delay_list)
543 : {
544 : /* If INSN has its block number recorded, clear it since we may
545 : be moving the insn to a new block. */
546 0 : clear_hashed_info_for_insn (insn);
547 :
548 0 : delay_list->safe_push (insn);
549 0 : }
550 :
551 : /* Delete INSN from the delay slot of the insn that it is in, which may
552 : produce an insn with no delay slots. Return the new insn. */
553 :
554 : static rtx_insn *
555 0 : delete_from_delay_slot (rtx_insn *insn)
556 : {
557 0 : rtx_insn *trial, *seq_insn, *prev;
558 0 : rtx_sequence *seq;
559 0 : bool had_barrier = false;
560 0 : int i;
561 :
562 : /* We first must find the insn containing the SEQUENCE with INSN in its
563 : delay slot. Do this by finding an insn, TRIAL, where
564 : PREV_INSN (NEXT_INSN (TRIAL)) != TRIAL. */
565 :
566 0 : for (trial = insn;
567 0 : PREV_INSN (NEXT_INSN (trial)) == trial;
568 : trial = NEXT_INSN (trial))
569 : ;
570 :
571 0 : seq_insn = PREV_INSN (NEXT_INSN (trial));
572 0 : seq = as_a <rtx_sequence *> (PATTERN (seq_insn));
573 :
574 0 : if (NEXT_INSN (seq_insn) && BARRIER_P (NEXT_INSN (seq_insn)))
575 : had_barrier = true;
576 :
577 : /* Create a delay list consisting of all the insns other than the one
578 : we are deleting (unless we were the only one). */
579 0 : auto_vec<rtx_insn *, 5> delay_list;
580 0 : if (seq->len () > 2)
581 0 : for (i = 1; i < seq->len (); i++)
582 0 : if (seq->insn (i) != insn)
583 0 : add_to_delay_list (seq->insn (i), &delay_list);
584 :
585 : /* Delete the old SEQUENCE, re-emit the insn that used to have the delay
586 : list, and rebuild the delay list if non-empty. */
587 0 : prev = PREV_INSN (seq_insn);
588 0 : trial = seq->insn (0);
589 0 : delete_related_insns (seq_insn);
590 0 : add_insn_after (trial, prev, NULL);
591 :
592 : /* If there was a barrier after the old SEQUENCE, remit it. */
593 0 : if (had_barrier)
594 0 : emit_barrier_after (trial);
595 :
596 : /* If there are any delay insns, remit them. Otherwise clear the
597 : annul flag. */
598 0 : if (!delay_list.is_empty ())
599 0 : trial = emit_delay_sequence (trial, delay_list, XVECLEN (seq, 0) - 2);
600 0 : else if (JUMP_P (trial))
601 0 : INSN_ANNULLED_BRANCH_P (trial) = 0;
602 :
603 0 : set_insn_from_target (insn, false);
604 :
605 : /* Show we need to fill this insn again. */
606 0 : obstack_ptr_grow (&unfilled_slots_obstack, trial);
607 :
608 0 : return trial;
609 0 : }
610 :
611 : /* Delete INSN, a JUMP_INSN. */
612 :
613 : static void
614 0 : delete_scheduled_jump (rtx_insn *insn)
615 : {
616 0 : delete_related_insns (insn);
617 0 : }
618 :
619 : /* Counters for delay-slot filling. */
620 :
621 : #define NUM_REORG_FUNCTIONS 2
622 : #define MAX_DELAY_HISTOGRAM 3
623 : #define MAX_REORG_PASSES 2
624 :
625 : static int num_insns_needing_delays[NUM_REORG_FUNCTIONS][MAX_REORG_PASSES];
626 :
627 : static int num_filled_delays[NUM_REORG_FUNCTIONS][MAX_DELAY_HISTOGRAM+1][MAX_REORG_PASSES];
628 :
629 : static int reorg_pass_number;
630 :
631 : static void
632 0 : note_delay_statistics (int slots_filled, int index)
633 : {
634 0 : num_insns_needing_delays[index][reorg_pass_number]++;
635 0 : if (slots_filled > MAX_DELAY_HISTOGRAM)
636 : slots_filled = MAX_DELAY_HISTOGRAM;
637 0 : num_filled_delays[index][slots_filled][reorg_pass_number]++;
638 0 : }
639 :
640 : /* Optimize the following cases:
641 :
642 : 1. When a conditional branch skips over only one instruction,
643 : use an annulling branch and put that insn in the delay slot.
644 : Use either a branch that annuls when the condition if true or
645 : invert the test with a branch that annuls when the condition is
646 : false. This saves insns, since otherwise we must copy an insn
647 : from the L1 target.
648 :
649 : (orig) (skip) (otherwise)
650 : Bcc.n L1 Bcc',a L1 Bcc,a L1'
651 : insn insn insn2
652 : L1: L1: L1:
653 : insn2 insn2 insn2
654 : insn3 insn3 L1':
655 : insn3
656 :
657 : 2. When a conditional branch skips over only one instruction,
658 : and after that, it unconditionally branches somewhere else,
659 : perform the similar optimization. This saves executing the
660 : second branch in the case where the inverted condition is true.
661 :
662 : Bcc.n L1 Bcc',a L2
663 : insn insn
664 : L1: L1:
665 : Bra L2 Bra L2
666 :
667 : INSN is a JUMP_INSN.
668 :
669 : This should be expanded to skip over N insns, where N is the number
670 : of delay slots required. */
671 :
672 : static void
673 0 : optimize_skip (rtx_jump_insn *insn, vec<rtx_insn *> *delay_list)
674 : {
675 0 : rtx_insn *trial = next_nonnote_nondebug_insn (insn);
676 0 : rtx_insn *next_trial = next_active_insn (trial);
677 0 : int flags;
678 :
679 0 : flags = get_jump_flags (insn, JUMP_LABEL (insn));
680 :
681 0 : if (trial == 0
682 0 : || !NONJUMP_INSN_P (trial)
683 0 : || is_a <rtx_sequence *> (PATTERN (trial))
684 0 : || recog_memoized (trial) < 0
685 0 : || (! eligible_for_annul_false (insn, 0, trial, flags)
686 0 : && ! eligible_for_annul_true (insn, 0, trial, flags))
687 0 : || RTX_FRAME_RELATED_P (trial)
688 0 : || can_throw_internal (trial))
689 : return;
690 :
691 : /* There are two cases where we are just executing one insn (we assume
692 : here that a branch requires only one insn; this should be generalized
693 : at some point): Where the branch goes around a single insn or where
694 : we have one insn followed by a branch to the same label we branch to.
695 : In both of these cases, inverting the jump and annulling the delay
696 : slot give the same effect in fewer insns. */
697 0 : if (next_trial == next_active_insn (JUMP_LABEL_AS_INSN (insn))
698 0 : || (next_trial != 0
699 0 : && simplejump_or_return_p (next_trial)
700 0 : && JUMP_LABEL (insn) == JUMP_LABEL (next_trial)))
701 : {
702 0 : if (eligible_for_annul_false (insn, 0, trial, flags))
703 : {
704 0 : if (invert_jump (insn, JUMP_LABEL (insn), 1))
705 0 : set_insn_from_target (trial, true);
706 0 : else if (! eligible_for_annul_true (insn, 0, trial, flags))
707 : return;
708 : }
709 :
710 0 : add_to_delay_list (trial, delay_list);
711 0 : next_trial = next_active_insn (trial);
712 0 : update_block (trial, trial);
713 0 : delete_related_insns (trial);
714 :
715 : /* Also, if we are targeting an unconditional
716 : branch, thread our jump to the target of that branch. Don't
717 : change this into a RETURN here, because it may not accept what
718 : we have in the delay slot. We'll fix this up later. */
719 0 : if (next_trial && simplejump_or_return_p (next_trial))
720 : {
721 0 : rtx target_label = JUMP_LABEL (next_trial);
722 0 : if (ANY_RETURN_P (target_label))
723 0 : target_label = find_end_label (target_label);
724 :
725 0 : if (target_label)
726 : {
727 : /* Recompute the flags based on TARGET_LABEL since threading
728 : the jump to TARGET_LABEL may change the direction of the
729 : jump (which may change the circumstances in which the
730 : delay slot is nullified). */
731 0 : flags = get_jump_flags (insn, target_label);
732 0 : if (eligible_for_annul_true (insn, 0, trial, flags))
733 0 : reorg_redirect_jump (insn, target_label);
734 : }
735 : }
736 :
737 0 : INSN_ANNULLED_BRANCH_P (insn) = 1;
738 : }
739 : }
740 :
741 : /* Encode and return branch direction and prediction information for
742 : INSN assuming it will jump to LABEL.
743 :
744 : Non conditional branches return no direction information and
745 : are predicted as very likely taken. */
746 :
747 : static int
748 0 : get_jump_flags (const rtx_insn *insn, rtx label)
749 : {
750 0 : int flags;
751 :
752 : /* get_jump_flags can be passed any insn with delay slots, these may
753 : be INSNs, CALL_INSNs, or JUMP_INSNs. Only JUMP_INSNs have branch
754 : direction information, and only if they are conditional jumps.
755 :
756 : If LABEL is a return, then there is no way to determine the branch
757 : direction. */
758 0 : if (JUMP_P (insn)
759 0 : && (condjump_p (insn) || condjump_in_parallel_p (insn))
760 0 : && !ANY_RETURN_P (label)
761 0 : && INSN_UID (insn) <= max_uid
762 0 : && INSN_UID (label) <= max_uid)
763 0 : flags
764 0 : = (uid_to_ruid[INSN_UID (label)] > uid_to_ruid[INSN_UID (insn)])
765 0 : ? ATTR_FLAG_forward : ATTR_FLAG_backward;
766 : /* No valid direction information. */
767 : else
768 : flags = 0;
769 :
770 0 : return flags;
771 : }
772 :
773 : /* Return truth value of the statement that this branch
774 : is mostly taken. If we think that the branch is extremely likely
775 : to be taken, we return 2. If the branch is slightly more likely to be
776 : taken, return 1. If the branch is slightly less likely to be taken,
777 : return 0 and if the branch is highly unlikely to be taken, return -1. */
778 :
779 : static int
780 0 : mostly_true_jump (rtx_insn *jump_insn)
781 : {
782 : /* If branch probabilities are available, then use that number since it
783 : always gives a correct answer. */
784 0 : rtx note = find_reg_note (jump_insn, REG_BR_PROB, 0);
785 0 : if (note)
786 : {
787 0 : int prob = profile_probability::from_reg_br_prob_note (XINT (note, 0))
788 0 : .to_reg_br_prob_base ();
789 :
790 0 : if (prob >= REG_BR_PROB_BASE * 9 / 10)
791 : return 2;
792 0 : else if (prob >= REG_BR_PROB_BASE / 2)
793 : return 1;
794 0 : else if (prob >= REG_BR_PROB_BASE / 10)
795 : return 0;
796 : else
797 0 : return -1;
798 : }
799 :
800 : /* If there is no note, assume branches are not taken.
801 : This should be rare. */
802 : return 0;
803 : }
804 :
805 : /* Return the condition under which INSN will branch to TARGET. If TARGET
806 : is zero, return the condition under which INSN will return. If INSN is
807 : an unconditional branch, return const_true_rtx. If INSN isn't a simple
808 : type of jump, or it doesn't go to TARGET, return 0. */
809 :
810 : static rtx
811 0 : get_branch_condition (const rtx_jump_insn *insn, rtx target)
812 : {
813 0 : rtx pat = PATTERN (insn);
814 0 : rtx src;
815 :
816 0 : if (condjump_in_parallel_p (insn))
817 0 : pat = XVECEXP (pat, 0, 0);
818 :
819 0 : if (ANY_RETURN_P (pat) && pat == target)
820 0 : return const_true_rtx;
821 :
822 0 : if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
823 : return 0;
824 :
825 0 : src = SET_SRC (pat);
826 0 : if (GET_CODE (src) == LABEL_REF && label_ref_label (src) == target)
827 0 : return const_true_rtx;
828 :
829 0 : else if (GET_CODE (src) == IF_THEN_ELSE
830 0 : && XEXP (src, 2) == pc_rtx
831 0 : && ((GET_CODE (XEXP (src, 1)) == LABEL_REF
832 0 : && label_ref_label (XEXP (src, 1)) == target)
833 0 : || (ANY_RETURN_P (XEXP (src, 1)) && XEXP (src, 1) == target)))
834 0 : return XEXP (src, 0);
835 :
836 0 : else if (GET_CODE (src) == IF_THEN_ELSE
837 0 : && XEXP (src, 1) == pc_rtx
838 0 : && ((GET_CODE (XEXP (src, 2)) == LABEL_REF
839 0 : && label_ref_label (XEXP (src, 2)) == target)
840 0 : || (ANY_RETURN_P (XEXP (src, 2)) && XEXP (src, 2) == target)))
841 : {
842 0 : enum rtx_code rev;
843 0 : rev = reversed_comparison_code (XEXP (src, 0), insn);
844 0 : if (rev != UNKNOWN)
845 0 : return gen_rtx_fmt_ee (rev, GET_MODE (XEXP (src, 0)),
846 : XEXP (XEXP (src, 0), 0),
847 : XEXP (XEXP (src, 0), 1));
848 : }
849 :
850 : return 0;
851 : }
852 :
853 : /* Return true if CONDITION is more strict than the condition of
854 : INSN, i.e., if INSN will always branch if CONDITION is true. */
855 :
856 : static bool
857 0 : condition_dominates_p (rtx condition, const rtx_jump_insn *insn)
858 : {
859 0 : rtx other_condition = get_branch_condition (insn, JUMP_LABEL (insn));
860 0 : enum rtx_code code = GET_CODE (condition);
861 0 : enum rtx_code other_code;
862 :
863 0 : if (rtx_equal_p (condition, other_condition)
864 0 : || other_condition == const_true_rtx)
865 : return true;
866 :
867 0 : else if (condition == const_true_rtx || other_condition == 0)
868 : return false;
869 :
870 0 : other_code = GET_CODE (other_condition);
871 0 : if (GET_RTX_LENGTH (code) != 2 || GET_RTX_LENGTH (other_code) != 2
872 0 : || ! rtx_equal_p (XEXP (condition, 0), XEXP (other_condition, 0))
873 0 : || ! rtx_equal_p (XEXP (condition, 1), XEXP (other_condition, 1)))
874 : return false;
875 :
876 0 : return comparison_dominates_p (code, other_code);
877 : }
878 :
879 : /* Return true if redirecting JUMP to NEWLABEL does not invalidate
880 : any insns already in the delay slot of JUMP. */
881 :
882 : static bool
883 0 : redirect_with_delay_slots_safe_p (rtx_jump_insn *jump, rtx newlabel,
884 : rtx_insn *seq)
885 : {
886 0 : int flags, i;
887 0 : rtx_sequence *pat = as_a <rtx_sequence *> (PATTERN (seq));
888 :
889 : /* Make sure all the delay slots of this jump would still
890 : be valid after threading the jump. If they are still
891 : valid, then return nonzero. */
892 :
893 0 : flags = get_jump_flags (jump, newlabel);
894 0 : for (i = 1; i < pat->len (); i++)
895 0 : if (! (
896 : #if ANNUL_IFFALSE_SLOTS
897 : (INSN_ANNULLED_BRANCH_P (jump)
898 : && INSN_FROM_TARGET_P (pat->insn (i)))
899 : ? eligible_for_annul_false (jump, i - 1, pat->insn (i), flags) :
900 : #endif
901 : #if ANNUL_IFTRUE_SLOTS
902 : (INSN_ANNULLED_BRANCH_P (jump)
903 : && ! INSN_FROM_TARGET_P (pat->insn (i)))
904 : ? eligible_for_annul_true (jump, i - 1, pat->insn (i), flags) :
905 : #endif
906 0 : eligible_for_delay (jump, i - 1, pat->insn (i), flags)))
907 : break;
908 :
909 0 : return (i == pat->len ());
910 : }
911 :
912 : /* Return true if redirecting JUMP to NEWLABEL does not invalidate
913 : any insns we wish to place in the delay slot of JUMP. */
914 :
915 : static bool
916 0 : redirect_with_delay_list_safe_p (rtx_jump_insn *jump, rtx newlabel,
917 : const vec<rtx_insn *> &delay_list)
918 : {
919 : /* Make sure all the insns in DELAY_LIST would still be
920 : valid after threading the jump. If they are still
921 : valid, then return true. */
922 :
923 0 : int flags = get_jump_flags (jump, newlabel);
924 0 : unsigned int delay_insns = delay_list.length ();
925 0 : unsigned int i = 0;
926 0 : for (; i < delay_insns; i++)
927 0 : if (! (
928 : #if ANNUL_IFFALSE_SLOTS
929 : (INSN_ANNULLED_BRANCH_P (jump)
930 : && INSN_FROM_TARGET_P (delay_list[i]))
931 : ? eligible_for_annul_false (jump, i, delay_list[i], flags) :
932 : #endif
933 : #if ANNUL_IFTRUE_SLOTS
934 : (INSN_ANNULLED_BRANCH_P (jump)
935 : && ! INSN_FROM_TARGET_P (delay_list[i]))
936 : ? eligible_for_annul_true (jump, i, delay_list[i], flags) :
937 : #endif
938 0 : eligible_for_delay (jump, i, delay_list[i], flags)))
939 : break;
940 :
941 0 : return i == delay_insns;
942 : }
943 :
944 : /* DELAY_LIST is a list of insns that have already been placed into delay
945 : slots. See if all of them have the same annulling status as ANNUL_TRUE_P.
946 : If not, return false; otherwise return true. */
947 :
948 : static bool
949 0 : check_annul_list_true_false (bool annul_true_p,
950 : const vec<rtx_insn *> &delay_list)
951 : {
952 0 : rtx_insn *trial;
953 0 : unsigned int i;
954 0 : FOR_EACH_VEC_ELT (delay_list, i, trial)
955 0 : if ((annul_true_p && INSN_FROM_TARGET_P (trial))
956 0 : || (!annul_true_p && !INSN_FROM_TARGET_P (trial)))
957 : return false;
958 :
959 : return true;
960 : }
961 :
962 : /* INSN branches to an insn whose pattern SEQ is a SEQUENCE. Given that
963 : the condition tested by INSN is CONDITION and the resources shown in
964 : OTHER_NEEDED are needed after INSN, see whether INSN can take all the insns
965 : from SEQ's delay list, in addition to whatever insns it may execute
966 : (in DELAY_LIST). SETS and NEEDED are denote resources already set and
967 : needed while searching for delay slot insns. Return the concatenated
968 : delay list if possible, otherwise, return 0.
969 :
970 : SLOTS_TO_FILL is the total number of slots required by INSN, and
971 : PSLOTS_FILLED points to the number filled so far (also the number of
972 : insns in DELAY_LIST). It is updated with the number that have been
973 : filled from the SEQUENCE, if any.
974 :
975 : PANNUL_P points to a nonzero value if we already know that we need
976 : to annul INSN. If this routine determines that annulling is needed,
977 : it may set that value to true.
978 :
979 : PNEW_THREAD points to a location that is to receive the place at which
980 : execution should continue. */
981 :
982 : static void
983 0 : steal_delay_list_from_target (rtx_insn *insn, rtx condition, rtx_sequence *seq,
984 : vec<rtx_insn *> *delay_list,
985 : struct resources *sets,
986 : struct resources *needed,
987 : struct resources *other_needed,
988 : int slots_to_fill, int *pslots_filled,
989 : bool *pannul_p, rtx *pnew_thread)
990 : {
991 0 : int slots_remaining = slots_to_fill - *pslots_filled;
992 0 : int total_slots_filled = *pslots_filled;
993 0 : auto_vec<rtx_insn *, 5> new_delay_list;
994 0 : bool must_annul = *pannul_p;
995 0 : bool used_annul = false;
996 0 : int i;
997 0 : struct resources cc_set;
998 0 : rtx_insn **redundant;
999 :
1000 : /* We can't do anything if there are more delay slots in SEQ than we
1001 : can handle, or if we don't know that it will be a taken branch.
1002 : We know that it will be a taken branch if it is either an unconditional
1003 : branch or a conditional branch with a stricter branch condition.
1004 :
1005 : Also, exit if the branch has more than one set, since then it is computing
1006 : other results that can't be ignored, e.g. the HPPA mov&branch instruction.
1007 : ??? It may be possible to move other sets into INSN in addition to
1008 : moving the instructions in the delay slots.
1009 :
1010 : We cannot steal the delay list if one of the instructions in the
1011 : current delay_list modifies the condition codes and the jump in the
1012 : sequence is a conditional jump. We cannot do this because we cannot
1013 : change the direction of the jump because the condition codes
1014 : will effect the direction of the jump in the sequence. */
1015 :
1016 0 : CLEAR_RESOURCE (&cc_set);
1017 :
1018 : rtx_insn *trial;
1019 0 : FOR_EACH_VEC_ELT (*delay_list, i, trial)
1020 : {
1021 0 : mark_set_resources (trial, &cc_set, 0, MARK_SRC_DEST_CALL);
1022 0 : if (insn_references_resource_p (seq->insn (0), &cc_set, false))
1023 : return;
1024 : }
1025 :
1026 0 : if (XVECLEN (seq, 0) - 1 > slots_remaining
1027 0 : || ! condition_dominates_p (condition,
1028 0 : as_a<rtx_jump_insn *> (seq->insn (0)))
1029 0 : || ! single_set (seq->insn (0)))
1030 : return;
1031 :
1032 : /* On some targets, branches with delay slots can have a limited
1033 : displacement. Give the back end a chance to tell us we can't do
1034 : this. */
1035 0 : if (! targetm.can_follow_jump (insn, seq->insn (0)))
1036 : return;
1037 :
1038 0 : redundant = XALLOCAVEC (rtx_insn *, XVECLEN (seq, 0));
1039 0 : for (i = 1; i < seq->len (); i++)
1040 : {
1041 0 : rtx_insn *trial = seq->insn (i);
1042 0 : int flags;
1043 :
1044 0 : if (insn_references_resource_p (trial, sets, false)
1045 0 : || insn_sets_resource_p (trial, needed, false)
1046 0 : || insn_sets_resource_p (trial, sets, false)
1047 : /* If TRIAL is from the fallthrough code of an annulled branch insn
1048 : in SEQ, we cannot use it. */
1049 0 : || (INSN_ANNULLED_BRANCH_P (seq->insn (0))
1050 0 : && ! INSN_FROM_TARGET_P (trial)))
1051 : return;
1052 :
1053 : /* If this insn was already done (usually in a previous delay slot),
1054 : pretend we put it in our delay slot. */
1055 0 : redundant[i] = redundant_insn (trial, insn, new_delay_list);
1056 0 : if (redundant[i])
1057 0 : continue;
1058 :
1059 : /* We will end up re-vectoring this branch, so compute flags
1060 : based on jumping to the new label. */
1061 0 : flags = get_jump_flags (insn, JUMP_LABEL (seq->insn (0)));
1062 :
1063 0 : if (! must_annul
1064 0 : && ((condition == const_true_rtx
1065 0 : || (! insn_sets_resource_p (trial, other_needed, false)
1066 0 : && ! may_trap_or_fault_p (PATTERN (trial)))))
1067 0 : ? eligible_for_delay (insn, total_slots_filled, trial, flags)
1068 0 : : (must_annul || (delay_list->is_empty () && new_delay_list.is_empty ()))
1069 0 : && (must_annul = true,
1070 0 : check_annul_list_true_false (false, *delay_list)
1071 0 : && check_annul_list_true_false (false, new_delay_list)
1072 0 : && eligible_for_annul_false (insn, total_slots_filled,
1073 : trial, flags)))
1074 : {
1075 0 : if (must_annul)
1076 : {
1077 : /* Frame related instructions cannot go into annulled delay
1078 : slots, it messes up the dwarf info. */
1079 0 : if (RTX_FRAME_RELATED_P (trial))
1080 : return;
1081 : used_annul = true;
1082 : }
1083 0 : rtx_insn *temp = copy_delay_slot_insn (trial);
1084 0 : set_insn_from_target (temp, true);
1085 0 : add_to_delay_list (temp, &new_delay_list);
1086 0 : total_slots_filled++;
1087 :
1088 0 : if (--slots_remaining == 0)
1089 : break;
1090 : }
1091 : else
1092 : return;
1093 : }
1094 :
1095 : /* Record the effect of the instructions that were redundant and which
1096 : we therefore decided not to copy. */
1097 0 : for (i = 1; i < seq->len (); i++)
1098 0 : if (redundant[i])
1099 : {
1100 0 : fix_reg_dead_note (redundant[i], insn);
1101 0 : update_block (seq->insn (i), insn);
1102 : }
1103 :
1104 : /* Show the place to which we will be branching. */
1105 0 : *pnew_thread = first_active_target_insn (JUMP_LABEL (seq->insn (0)));
1106 :
1107 : /* Add any new insns to the delay list and update the count of the
1108 : number of slots filled. */
1109 0 : *pslots_filled = total_slots_filled;
1110 0 : if (used_annul)
1111 0 : *pannul_p = true;
1112 :
1113 0 : rtx_insn *temp;
1114 0 : FOR_EACH_VEC_ELT (new_delay_list, i, temp)
1115 0 : add_to_delay_list (temp, delay_list);
1116 0 : }
1117 :
1118 : /* Similar to steal_delay_list_from_target except that SEQ is on the
1119 : fallthrough path of INSN. Here we only do something if the delay insn
1120 : of SEQ is an unconditional branch. In that case we steal its delay slot
1121 : for INSN since unconditional branches are much easier to fill. */
1122 :
1123 : static void
1124 0 : steal_delay_list_from_fallthrough (rtx_insn *insn, rtx condition,
1125 : rtx_sequence *seq,
1126 : vec<rtx_insn *> *delay_list,
1127 : struct resources *sets,
1128 : struct resources *needed,
1129 : struct resources *other_needed,
1130 : int slots_to_fill, int *pslots_filled,
1131 : bool *pannul_p)
1132 : {
1133 0 : int i;
1134 0 : int flags;
1135 0 : bool must_annul = *pannul_p;
1136 0 : bool used_annul = false;
1137 :
1138 0 : flags = get_jump_flags (insn, JUMP_LABEL (insn));
1139 :
1140 : /* We can't do anything if SEQ's delay insn isn't an
1141 : unconditional branch. */
1142 :
1143 0 : if (! simplejump_or_return_p (seq->insn (0)))
1144 : return;
1145 :
1146 0 : for (i = 1; i < seq->len (); i++)
1147 : {
1148 0 : rtx_insn *trial = seq->insn (i);
1149 0 : rtx_insn *prior_insn;
1150 :
1151 0 : if (insn_references_resource_p (trial, sets, false)
1152 0 : || insn_sets_resource_p (trial, needed, false)
1153 0 : || insn_sets_resource_p (trial, sets, false))
1154 : break;
1155 :
1156 : /* If this insn was already done, we don't need it. */
1157 0 : if ((prior_insn = redundant_insn (trial, insn, *delay_list)))
1158 : {
1159 0 : fix_reg_dead_note (prior_insn, insn);
1160 0 : update_block (trial, insn);
1161 0 : delete_from_delay_slot (trial);
1162 0 : continue;
1163 : }
1164 :
1165 0 : if (! must_annul
1166 0 : && ((condition == const_true_rtx
1167 0 : || (! insn_sets_resource_p (trial, other_needed, false)
1168 0 : && ! may_trap_or_fault_p (PATTERN (trial)))))
1169 0 : ? eligible_for_delay (insn, *pslots_filled, trial, flags)
1170 0 : : (must_annul || delay_list->is_empty ()) && (must_annul = true,
1171 0 : check_annul_list_true_false (true, *delay_list)
1172 0 : && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
1173 : {
1174 0 : if (must_annul)
1175 0 : used_annul = true;
1176 0 : delete_from_delay_slot (trial);
1177 0 : add_to_delay_list (trial, delay_list);
1178 :
1179 0 : if (++(*pslots_filled) == slots_to_fill)
1180 : break;
1181 : }
1182 : else
1183 : break;
1184 : }
1185 :
1186 0 : if (used_annul)
1187 0 : *pannul_p = true;
1188 : }
1189 :
1190 : /* Try merging insns starting at THREAD which match exactly the insns in
1191 : INSN's delay list.
1192 :
1193 : If all insns were matched and the insn was previously annulling, the
1194 : annul bit will be cleared.
1195 :
1196 : For each insn that is merged, if the branch is or will be non-annulling,
1197 : we delete the merged insn. */
1198 :
1199 : static void
1200 0 : try_merge_delay_insns (rtx_insn *insn, rtx_insn *thread)
1201 : {
1202 0 : rtx_insn *trial, *next_trial;
1203 0 : rtx_sequence *insn_pat = as_a <rtx_sequence *> (PATTERN (insn));
1204 0 : rtx_insn *delay_insn = insn_pat->insn (0);
1205 0 : bool annul_p = JUMP_P (delay_insn) && INSN_ANNULLED_BRANCH_P (delay_insn);
1206 0 : int slot_number = 1;
1207 0 : int num_slots = insn_pat->len ();
1208 0 : rtx_insn *next_to_match = insn_pat->insn (slot_number);
1209 0 : struct resources set, needed, modified;
1210 0 : auto_vec<std::pair<rtx_insn *, bool>, 10> merged_insns;
1211 0 : int flags;
1212 :
1213 0 : flags = get_jump_flags (delay_insn, JUMP_LABEL (delay_insn));
1214 :
1215 0 : CLEAR_RESOURCE (&needed);
1216 0 : CLEAR_RESOURCE (&set);
1217 :
1218 : /* If this is not an annulling branch, take into account anything needed in
1219 : INSN's delay slot. This prevents two increments from being incorrectly
1220 : folded into one. If we are annulling, this would be the correct
1221 : thing to do. (The alternative, looking at things set in NEXT_TO_MATCH
1222 : will essentially disable this optimization. This method is somewhat of
1223 : a kludge, but I don't see a better way.) */
1224 0 : if (! annul_p)
1225 0 : for (int i = 1; i < num_slots; i++)
1226 0 : if (insn_pat->insn (i))
1227 0 : mark_referenced_resources (insn_pat->insn (i), &needed, true);
1228 :
1229 0 : for (trial = thread; !stop_search_p (trial, true); trial = next_trial)
1230 : {
1231 0 : rtx pat = PATTERN (trial);
1232 0 : rtx_insn *oldtrial = trial;
1233 :
1234 0 : next_trial = next_nonnote_insn (trial);
1235 :
1236 : /* TRIAL must be a CALL_INSN or INSN. Skip USE and CLOBBER. */
1237 0 : if (NONJUMP_INSN_P (trial)
1238 0 : && (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER))
1239 0 : continue;
1240 :
1241 0 : if (GET_CODE (next_to_match) == GET_CODE (trial)
1242 0 : && ! insn_references_resource_p (trial, &set, true)
1243 0 : && ! insn_sets_resource_p (trial, &set, true)
1244 0 : && ! insn_sets_resource_p (trial, &needed, true)
1245 0 : && (trial = try_split (pat, trial, 0)) != 0
1246 : /* Update next_trial, in case try_split succeeded. */
1247 0 : && (next_trial = next_nonnote_insn (trial))
1248 : /* Likewise THREAD. */
1249 0 : && (thread = oldtrial == thread ? trial : thread)
1250 0 : && rtx_equal_p (PATTERN (next_to_match), PATTERN (trial))
1251 : /* Have to test this condition if annul condition is different
1252 : from (and less restrictive than) non-annulling one. */
1253 0 : && eligible_for_delay (delay_insn, slot_number - 1, trial, flags))
1254 : {
1255 :
1256 0 : if (! annul_p)
1257 : {
1258 0 : update_block (trial, thread);
1259 0 : if (trial == thread)
1260 0 : thread = next_active_insn (thread);
1261 :
1262 0 : delete_related_insns (trial);
1263 0 : set_insn_from_target (next_to_match, false);
1264 : }
1265 : else
1266 0 : merged_insns.safe_push (std::pair<rtx_insn *, bool> (trial, false));
1267 :
1268 0 : if (++slot_number == num_slots)
1269 : break;
1270 :
1271 0 : next_to_match = insn_pat->insn (slot_number);
1272 : }
1273 :
1274 0 : mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1275 0 : mark_referenced_resources (trial, &needed, true);
1276 : }
1277 :
1278 : /* See if we stopped on a filled insn. If we did, try to see if its
1279 : delay slots match. */
1280 0 : rtx_sequence *pat;
1281 0 : if (slot_number != num_slots
1282 0 : && trial && NONJUMP_INSN_P (trial)
1283 0 : && (pat = dyn_cast <rtx_sequence *> (PATTERN (trial)))
1284 0 : && !(JUMP_P (pat->insn (0))
1285 0 : && INSN_ANNULLED_BRANCH_P (pat->insn (0))))
1286 : {
1287 0 : rtx_insn *filled_insn = pat->insn (0);
1288 :
1289 : /* Account for resources set/needed by the filled insn. */
1290 0 : mark_set_resources (filled_insn, &set, 0, MARK_SRC_DEST_CALL);
1291 0 : mark_referenced_resources (filled_insn, &needed, true);
1292 :
1293 0 : for (int i = 1; i < pat->len (); i++)
1294 : {
1295 0 : rtx_insn *dtrial = pat->insn (i);
1296 :
1297 0 : CLEAR_RESOURCE (&modified);
1298 : /* Account for resources set by the insn following NEXT_TO_MATCH
1299 : inside INSN's delay list. */
1300 0 : for (int j = 1; slot_number + j < num_slots; j++)
1301 0 : mark_set_resources (insn_pat->insn (slot_number + j),
1302 : &modified, 0, MARK_SRC_DEST_CALL);
1303 : /* Account for resources set by the insn before DTRIAL and inside
1304 : TRIAL's delay list. */
1305 0 : for (int j = 1; j < i; j++)
1306 0 : mark_set_resources (pat->insn (j),
1307 : &modified, 0, MARK_SRC_DEST_CALL);
1308 0 : if (! insn_references_resource_p (dtrial, &set, true)
1309 0 : && ! insn_sets_resource_p (dtrial, &set, true)
1310 0 : && ! insn_sets_resource_p (dtrial, &needed, true)
1311 0 : && rtx_equal_p (PATTERN (next_to_match), PATTERN (dtrial))
1312 : /* Check that DTRIAL and NEXT_TO_MATCH does not reference a
1313 : resource modified between them (only dtrial is checked because
1314 : next_to_match and dtrial shall to be equal in order to hit
1315 : this line) */
1316 0 : && ! insn_references_resource_p (dtrial, &modified, true)
1317 0 : && eligible_for_delay (delay_insn, slot_number - 1, dtrial, flags))
1318 : {
1319 0 : if (! annul_p)
1320 : {
1321 0 : rtx_insn *new_rtx;
1322 :
1323 0 : update_block (dtrial, thread);
1324 0 : new_rtx = delete_from_delay_slot (dtrial);
1325 0 : if (thread->deleted ())
1326 0 : thread = new_rtx;
1327 0 : set_insn_from_target (next_to_match, false);
1328 : }
1329 : else
1330 0 : merged_insns.safe_push (std::pair<rtx_insn *, bool> (dtrial,
1331 0 : true));
1332 :
1333 0 : if (++slot_number == num_slots)
1334 : break;
1335 :
1336 0 : next_to_match = insn_pat->insn (slot_number);
1337 : }
1338 : else
1339 : {
1340 : /* Keep track of the set/referenced resources for the delay
1341 : slots of any trial insns we encounter. */
1342 0 : mark_set_resources (dtrial, &set, 0, MARK_SRC_DEST_CALL);
1343 0 : mark_referenced_resources (dtrial, &needed, true);
1344 : }
1345 : }
1346 : }
1347 :
1348 : /* If all insns in the delay slot have been matched and we were previously
1349 : annulling the branch, we need not any more. In that case delete all the
1350 : merged insns. Also clear the INSN_FROM_TARGET_P bit of each insn in
1351 : the delay list so that we know that it isn't only being used at the
1352 : target. */
1353 0 : if (slot_number == num_slots && annul_p)
1354 : {
1355 0 : unsigned int len = merged_insns.length ();
1356 0 : for (unsigned int i = len - 1; i < len; i--)
1357 0 : if (merged_insns[i].second)
1358 : {
1359 0 : update_block (merged_insns[i].first, thread);
1360 0 : rtx_insn *new_rtx = delete_from_delay_slot (merged_insns[i].first);
1361 0 : if (thread->deleted ())
1362 0 : thread = new_rtx;
1363 : }
1364 : else
1365 : {
1366 0 : update_block (merged_insns[i].first, thread);
1367 0 : delete_related_insns (merged_insns[i].first);
1368 : }
1369 :
1370 0 : INSN_ANNULLED_BRANCH_P (delay_insn) = 0;
1371 :
1372 0 : for (int i = 0; i < insn_pat->len (); i++)
1373 0 : set_insn_from_target (insn_pat->insn (i), false);
1374 : }
1375 0 : }
1376 :
1377 : /* See if INSN is redundant with an insn in front of TARGET. Often this
1378 : is called when INSN is a candidate for a delay slot of TARGET.
1379 : DELAY_LIST are insns that will be placed in delay slots of TARGET in front
1380 : of INSN. Often INSN will be redundant with an insn in a delay slot of
1381 : some previous insn. This happens when we have a series of branches to the
1382 : same label; in that case the first insn at the target might want to go
1383 : into each of the delay slots.
1384 :
1385 : If we are not careful, this routine can take up a significant fraction
1386 : of the total compilation time (4%), but only wins rarely. Hence we
1387 : speed this routine up by making two passes. The first pass goes back
1388 : until it hits a label and sees if it finds an insn with an identical
1389 : pattern. Only in this (relatively rare) event does it check for
1390 : data conflicts.
1391 :
1392 : We do not split insns we encounter. This could cause us not to find a
1393 : redundant insn, but the cost of splitting seems greater than the possible
1394 : gain in rare cases. */
1395 :
1396 : static rtx_insn *
1397 0 : redundant_insn (rtx_insn *insn, rtx_insn *target,
1398 : const vec<rtx_insn *> &delay_list)
1399 : {
1400 0 : rtx_insn *target_main = target;
1401 0 : rtx ipat = PATTERN (insn);
1402 0 : rtx_insn *trial;
1403 0 : rtx pat;
1404 0 : struct resources needed, set;
1405 0 : int i;
1406 0 : unsigned insns_to_search;
1407 :
1408 : /* If INSN has any REG_UNUSED notes, it can't match anything since we
1409 : are allowed to not actually assign to such a register. */
1410 0 : if (find_reg_note (insn, REG_UNUSED, NULL_RTX) != 0)
1411 : return 0;
1412 :
1413 : /* Scan backwards looking for a match. */
1414 0 : for (trial = PREV_INSN (target),
1415 0 : insns_to_search = param_max_delay_slot_insn_search;
1416 0 : trial && insns_to_search > 0;
1417 0 : trial = PREV_INSN (trial))
1418 : {
1419 : /* (use (insn))s can come immediately after a barrier if the
1420 : label that used to precede them has been deleted as dead.
1421 : See delete_related_insns. */
1422 0 : if (LABEL_P (trial) || BARRIER_P (trial))
1423 : return 0;
1424 :
1425 0 : if (!NONDEBUG_INSN_P (trial))
1426 0 : continue;
1427 0 : --insns_to_search;
1428 :
1429 0 : pat = PATTERN (trial);
1430 0 : if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1431 0 : continue;
1432 :
1433 0 : if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (pat))
1434 : {
1435 : /* Stop for a CALL and its delay slots because it is difficult to
1436 : track its resource needs correctly. */
1437 0 : if (CALL_P (seq->element (0)))
1438 : return 0;
1439 :
1440 : /* Stop for an INSN or JUMP_INSN with delayed effects and its delay
1441 : slots because it is difficult to track its resource needs
1442 : correctly. */
1443 :
1444 0 : if (INSN_SETS_ARE_DELAYED (seq->insn (0)))
1445 : return 0;
1446 :
1447 0 : if (INSN_REFERENCES_ARE_DELAYED (seq->insn (0)))
1448 : return 0;
1449 :
1450 : /* See if any of the insns in the delay slot match, updating
1451 : resource requirements as we go. */
1452 0 : for (i = seq->len () - 1; i > 0; i--)
1453 0 : if (GET_CODE (seq->element (i)) == GET_CODE (insn)
1454 0 : && rtx_equal_p (PATTERN (seq->insn (i)), ipat)
1455 0 : && ! find_reg_note (seq->insn (i), REG_UNUSED, NULL_RTX))
1456 : break;
1457 :
1458 : /* If found a match, exit this loop early. */
1459 0 : if (i > 0)
1460 : break;
1461 : }
1462 :
1463 0 : else if (GET_CODE (trial) == GET_CODE (insn) && rtx_equal_p (pat, ipat)
1464 0 : && ! find_reg_note (trial, REG_UNUSED, NULL_RTX))
1465 : break;
1466 : }
1467 :
1468 : /* If we didn't find an insn that matches, return 0. */
1469 0 : if (trial == 0)
1470 : return 0;
1471 :
1472 : /* See what resources this insn sets and needs. If they overlap, it
1473 : can't be redundant. */
1474 :
1475 0 : CLEAR_RESOURCE (&needed);
1476 0 : CLEAR_RESOURCE (&set);
1477 0 : mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1478 0 : mark_referenced_resources (insn, &needed, true);
1479 :
1480 : /* If TARGET is a SEQUENCE, get the main insn. */
1481 0 : rtx_sequence *target_seq = nullptr;
1482 0 : if (NONJUMP_INSN_P (target))
1483 0 : target_seq = dyn_cast <rtx_sequence *> (PATTERN (target));
1484 0 : if (target_seq)
1485 0 : target_main = target_seq->insn (0);
1486 :
1487 0 : if (resource_conflicts_p (&needed, &set)
1488 : /* The insn requiring the delay may not set anything needed or set by
1489 : INSN. */
1490 0 : || insn_sets_resource_p (target_main, &needed, true)
1491 0 : || insn_sets_resource_p (target_main, &set, true))
1492 : return 0;
1493 :
1494 : /* Insns we pass may not set either NEEDED or SET, so merge them for
1495 : simpler tests. */
1496 0 : needed.memory |= set.memory;
1497 0 : needed.regs |= set.regs;
1498 :
1499 : /* This insn isn't redundant if it conflicts with an insn that either is
1500 : or will be in a delay slot of TARGET. */
1501 :
1502 : unsigned int j;
1503 : rtx_insn *temp;
1504 0 : FOR_EACH_VEC_ELT (delay_list, j, temp)
1505 0 : if (insn_sets_resource_p (temp, &needed, true))
1506 : return 0;
1507 :
1508 0 : if (target_seq)
1509 0 : for (i = 1; i < target_seq->len (); i++)
1510 0 : if (insn_sets_resource_p (target_seq->insn (i), &needed, true))
1511 : return 0;
1512 :
1513 : /* Scan backwards until we reach a label or an insn that uses something
1514 : INSN sets or sets something insn uses or sets. */
1515 :
1516 0 : for (trial = PREV_INSN (target),
1517 0 : insns_to_search = param_max_delay_slot_insn_search;
1518 0 : trial && !LABEL_P (trial) && insns_to_search > 0;
1519 0 : trial = PREV_INSN (trial))
1520 : {
1521 0 : if (!NONDEBUG_INSN_P (trial))
1522 0 : continue;
1523 0 : --insns_to_search;
1524 :
1525 0 : pat = PATTERN (trial);
1526 0 : if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1527 0 : continue;
1528 :
1529 0 : if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (pat))
1530 : {
1531 0 : bool annul_p = false;
1532 0 : rtx_insn *control = seq->insn (0);
1533 :
1534 : /* If this is a CALL_INSN and its delay slots, it is hard to track
1535 : the resource needs properly, so give up. */
1536 0 : if (CALL_P (control))
1537 : return 0;
1538 :
1539 : /* If this is an INSN or JUMP_INSN with delayed effects, it
1540 : is hard to track the resource needs properly, so give up. */
1541 :
1542 0 : if (INSN_SETS_ARE_DELAYED (control))
1543 : return 0;
1544 :
1545 0 : if (INSN_REFERENCES_ARE_DELAYED (control))
1546 : return 0;
1547 :
1548 0 : if (JUMP_P (control))
1549 0 : annul_p = INSN_ANNULLED_BRANCH_P (control);
1550 :
1551 : /* See if any of the insns in the delay slot match, updating
1552 : resource requirements as we go. */
1553 0 : for (i = seq->len () - 1; i > 0; i--)
1554 : {
1555 0 : rtx_insn *candidate = seq->insn (i);
1556 :
1557 : /* If an insn will be annulled if the branch is false, it isn't
1558 : considered as a possible duplicate insn. */
1559 0 : if (rtx_equal_p (PATTERN (candidate), ipat)
1560 0 : && ! (annul_p && INSN_FROM_TARGET_P (candidate)))
1561 : {
1562 : /* Show that this insn will be used in the sequel. */
1563 0 : set_insn_from_target (candidate, false);
1564 0 : return candidate;
1565 : }
1566 :
1567 : /* Unless this is an annulled insn from the target of a branch,
1568 : we must stop if it sets anything needed or set by INSN. */
1569 0 : if ((!annul_p || !INSN_FROM_TARGET_P (candidate))
1570 0 : && insn_sets_resource_p (candidate, &needed, true))
1571 : return 0;
1572 : }
1573 :
1574 : /* If the insn requiring the delay slot conflicts with INSN, we
1575 : must stop. */
1576 0 : if (insn_sets_resource_p (control, &needed, true))
1577 : return 0;
1578 : }
1579 : else
1580 : {
1581 : /* See if TRIAL is the same as INSN. */
1582 0 : pat = PATTERN (trial);
1583 0 : if (rtx_equal_p (pat, ipat))
1584 : return trial;
1585 :
1586 : /* Can't go any further if TRIAL conflicts with INSN. */
1587 0 : if (insn_sets_resource_p (trial, &needed, true))
1588 : return 0;
1589 : }
1590 : }
1591 :
1592 : return 0;
1593 : }
1594 :
1595 : /* Return true if THREAD can only be executed in one way. If LABEL is nonzero,
1596 : it is the target of the branch insn being scanned. If ALLOW_FALLTHROUGH
1597 : is true, we are allowed to fall into this thread; otherwise, we are not.
1598 :
1599 : If LABEL is used more than one or we pass a label other than LABEL before
1600 : finding an active insn, we do not own this thread. */
1601 :
1602 : static bool
1603 0 : own_thread_p (rtx thread, rtx label, bool allow_fallthrough)
1604 : {
1605 0 : rtx_insn *active_insn;
1606 0 : rtx_insn *insn;
1607 :
1608 : /* We don't own the function end. */
1609 0 : if (thread == 0 || ANY_RETURN_P (thread))
1610 : return false;
1611 :
1612 : /* We have a non-NULL insn. */
1613 0 : rtx_insn *thread_insn = as_a <rtx_insn *> (thread);
1614 :
1615 : /* Get the first active insn, or THREAD_INSN, if it is an active insn. */
1616 0 : active_insn = next_active_insn (PREV_INSN (thread_insn));
1617 :
1618 0 : for (insn = thread_insn; insn != active_insn; insn = NEXT_INSN (insn))
1619 0 : if (LABEL_P (insn)
1620 0 : && (insn != label || LABEL_NUSES (insn) != 1))
1621 : return false;
1622 :
1623 0 : if (allow_fallthrough)
1624 : return true;
1625 :
1626 : /* Ensure that we reach a BARRIER before any insn or label. */
1627 0 : for (insn = prev_nonnote_insn (thread_insn);
1628 0 : insn == 0 || !BARRIER_P (insn);
1629 0 : insn = prev_nonnote_insn (insn))
1630 0 : if (insn == 0
1631 0 : || LABEL_P (insn)
1632 0 : || (NONJUMP_INSN_P (insn)
1633 0 : && GET_CODE (PATTERN (insn)) != USE
1634 0 : && GET_CODE (PATTERN (insn)) != CLOBBER))
1635 : return false;
1636 :
1637 : return true;
1638 : }
1639 :
1640 : /* Called when INSN is being moved from a location near the target of a jump.
1641 : We leave a marker of the form (use (INSN)) immediately in front of WHERE
1642 : for mark_target_live_regs. These markers will be deleted at the end.
1643 :
1644 : We used to try to update the live status of registers if WHERE is at
1645 : the start of a basic block, but that can't work since we may remove a
1646 : BARRIER in relax_delay_slots. */
1647 :
1648 : static void
1649 0 : update_block (rtx_insn *insn, rtx_insn *where)
1650 : {
1651 0 : emit_insn_before (gen_rtx_USE (VOIDmode, insn), where);
1652 :
1653 : /* INSN might be making a value live in a block where it didn't use to
1654 : be. So recompute liveness information for this block. */
1655 0 : incr_ticks_for_insn (insn);
1656 0 : }
1657 :
1658 : /* Similar to REDIRECT_JUMP except that we update the BB_TICKS entry for
1659 : the basic block containing the jump. */
1660 :
1661 : static bool
1662 0 : reorg_redirect_jump (rtx_jump_insn *jump, rtx nlabel)
1663 : {
1664 0 : incr_ticks_for_insn (jump);
1665 0 : return redirect_jump (jump, nlabel, 1);
1666 : }
1667 :
1668 : /* Called when INSN is being moved forward into a delay slot of DELAYED_INSN.
1669 : We check every instruction between INSN and DELAYED_INSN for REG_DEAD notes
1670 : that reference values used in INSN. If we find one, then we move the
1671 : REG_DEAD note to INSN.
1672 :
1673 : This is needed to handle the case where a later insn (after INSN) has a
1674 : REG_DEAD note for a register used by INSN, and this later insn subsequently
1675 : gets moved before a CODE_LABEL because it is a redundant insn. In this
1676 : case, mark_target_live_regs may be confused into thinking the register
1677 : is dead because it sees a REG_DEAD note immediately before a CODE_LABEL. */
1678 :
1679 : static void
1680 0 : update_reg_dead_notes (rtx_insn *insn, rtx_insn *delayed_insn)
1681 : {
1682 0 : rtx link, next;
1683 0 : rtx_insn *p;
1684 :
1685 0 : for (p = next_nonnote_insn (insn); p != delayed_insn;
1686 0 : p = next_nonnote_insn (p))
1687 0 : for (link = REG_NOTES (p); link; link = next)
1688 : {
1689 0 : next = XEXP (link, 1);
1690 :
1691 0 : if (REG_NOTE_KIND (link) != REG_DEAD
1692 0 : || !REG_P (XEXP (link, 0)))
1693 0 : continue;
1694 :
1695 0 : if (reg_referenced_p (XEXP (link, 0), PATTERN (insn)))
1696 : {
1697 : /* Move the REG_DEAD note from P to INSN. */
1698 0 : remove_note (p, link);
1699 0 : XEXP (link, 1) = REG_NOTES (insn);
1700 0 : REG_NOTES (insn) = link;
1701 : }
1702 : }
1703 0 : }
1704 :
1705 : /* Called when an insn redundant with start_insn is deleted. If there
1706 : is a REG_DEAD note for the target of start_insn between start_insn
1707 : and stop_insn, then the REG_DEAD note needs to be deleted since the
1708 : value no longer dies there.
1709 :
1710 : If the REG_DEAD note isn't deleted, then mark_target_live_regs may be
1711 : confused into thinking the register is dead. */
1712 :
1713 : static void
1714 0 : fix_reg_dead_note (rtx_insn *start_insn, rtx_insn *stop_insn)
1715 : {
1716 0 : rtx link, next;
1717 0 : rtx_insn *p;
1718 :
1719 0 : for (p = next_nonnote_insn (start_insn); p != stop_insn;
1720 0 : p = next_nonnote_insn (p))
1721 0 : for (link = REG_NOTES (p); link; link = next)
1722 : {
1723 0 : next = XEXP (link, 1);
1724 :
1725 0 : if (REG_NOTE_KIND (link) != REG_DEAD
1726 0 : || !REG_P (XEXP (link, 0)))
1727 0 : continue;
1728 :
1729 0 : if (reg_set_p (XEXP (link, 0), PATTERN (start_insn)))
1730 : {
1731 0 : remove_note (p, link);
1732 0 : return;
1733 : }
1734 : }
1735 : }
1736 :
1737 : /* Delete any REG_UNUSED notes that exist on INSN but not on OTHER_INSN.
1738 :
1739 : This handles the case of udivmodXi4 instructions which optimize their
1740 : output depending on whether any REG_UNUSED notes are present. We must
1741 : make sure that INSN calculates as many results as OTHER_INSN does. */
1742 :
1743 : static void
1744 0 : update_reg_unused_notes (rtx_insn *insn, rtx_insn *other_insn)
1745 : {
1746 0 : rtx link, next;
1747 :
1748 0 : for (link = REG_NOTES (insn); link; link = next)
1749 : {
1750 0 : next = XEXP (link, 1);
1751 :
1752 0 : if (REG_NOTE_KIND (link) != REG_UNUSED
1753 0 : || !REG_P (XEXP (link, 0)))
1754 0 : continue;
1755 :
1756 0 : if (!find_regno_note (other_insn, REG_UNUSED, REGNO (XEXP (link, 0))))
1757 0 : remove_note (insn, link);
1758 : }
1759 0 : }
1760 :
1761 : static vec <rtx> sibling_labels;
1762 :
1763 : /* Return the label before INSN, or put a new label there. If SIBLING is
1764 : non-zero, it is another label associated with the new label (if any),
1765 : typically the former target of the jump that will be redirected to
1766 : the new label. */
1767 :
1768 : static rtx_insn *
1769 0 : get_label_before (rtx_insn *insn, rtx sibling)
1770 : {
1771 0 : rtx_insn *label;
1772 :
1773 : /* Find an existing label at this point
1774 : or make a new one if there is none. */
1775 0 : label = prev_nonnote_nondebug_insn (insn);
1776 :
1777 0 : if (label == 0 || !LABEL_P (label))
1778 : {
1779 0 : rtx_insn *prev = PREV_INSN (insn);
1780 :
1781 0 : label = gen_label_rtx ();
1782 0 : emit_label_after (label, prev);
1783 0 : LABEL_NUSES (label) = 0;
1784 0 : if (sibling)
1785 : {
1786 0 : sibling_labels.safe_push (label);
1787 0 : sibling_labels.safe_push (sibling);
1788 : }
1789 : }
1790 0 : return label;
1791 : }
1792 :
1793 : /* Scan a function looking for insns that need a delay slot and find insns to
1794 : put into the delay slot.
1795 :
1796 : NON_JUMPS_P is true if we are to only try to fill non-jump insns (such
1797 : as calls). We do these first since we don't want jump insns (that are
1798 : easier to fill) to get the only insns that could be used for non-jump insns.
1799 : When it is zero, only try to fill JUMP_INSNs.
1800 :
1801 : When slots are filled in this manner, the insns (including the
1802 : delay_insn) are put together in a SEQUENCE rtx. In this fashion,
1803 : it is possible to tell whether a delay slot has really been filled
1804 : or not. `final' knows how to deal with this, by communicating
1805 : through FINAL_SEQUENCE. */
1806 :
1807 : static void
1808 0 : fill_simple_delay_slots (bool non_jumps_p)
1809 : {
1810 0 : rtx_insn *insn, *trial, *next_trial;
1811 0 : rtx pat;
1812 0 : int i;
1813 0 : int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
1814 0 : struct resources needed, set;
1815 0 : int slots_to_fill, slots_filled;
1816 0 : auto_vec<rtx_insn *, 5> delay_list;
1817 :
1818 0 : for (i = 0; i < num_unfilled_slots; i++)
1819 : {
1820 0 : int flags;
1821 : /* Get the next insn to fill. If it has already had any slots assigned,
1822 : we can't do anything with it. Maybe we'll improve this later. */
1823 :
1824 0 : insn = unfilled_slots_base[i];
1825 0 : if (insn == 0
1826 0 : || insn->deleted ()
1827 0 : || (NONJUMP_INSN_P (insn)
1828 0 : && is_a <rtx_sequence *> (PATTERN (insn)))
1829 0 : || (JUMP_P (insn) && non_jumps_p)
1830 0 : || (!JUMP_P (insn) && ! non_jumps_p))
1831 0 : continue;
1832 :
1833 : /* It may have been that this insn used to need delay slots, but
1834 : now doesn't; ignore in that case. This can happen, for example,
1835 : on the HP PA RISC, where the number of delay slots depends on
1836 : what insns are nearby. */
1837 0 : slots_to_fill = num_delay_slots (insn);
1838 :
1839 : /* Some machine description have defined instructions to have
1840 : delay slots only in certain circumstances which may depend on
1841 : nearby insns (which change due to reorg's actions).
1842 :
1843 : For example, the PA port normally has delay slots for unconditional
1844 : jumps.
1845 :
1846 : However, the PA port claims such jumps do not have a delay slot
1847 : if they are immediate successors of certain CALL_INSNs. This
1848 : allows the port to favor filling the delay slot of the call with
1849 : the unconditional jump. */
1850 0 : if (slots_to_fill == 0)
1851 0 : continue;
1852 :
1853 : /* This insn needs, or can use, some delay slots. SLOTS_TO_FILL
1854 : says how many. After initialization, first try optimizing
1855 :
1856 : call _foo call _foo
1857 : nop add %o7,.-L1,%o7
1858 : b,a L1
1859 : nop
1860 :
1861 : If this case applies, the delay slot of the call is filled with
1862 : the unconditional jump. This is done first to avoid having the
1863 : delay slot of the call filled in the backward scan. Also, since
1864 : the unconditional jump is likely to also have a delay slot, that
1865 : insn must exist when it is subsequently scanned.
1866 :
1867 : This is tried on each insn with delay slots as some machines
1868 : have insns which perform calls, but are not represented as
1869 : CALL_INSNs. */
1870 :
1871 0 : slots_filled = 0;
1872 0 : delay_list.truncate (0);
1873 :
1874 0 : if (JUMP_P (insn))
1875 0 : flags = get_jump_flags (insn, JUMP_LABEL (insn));
1876 : else
1877 0 : flags = get_jump_flags (insn, NULL_RTX);
1878 :
1879 0 : if ((trial = next_active_insn (insn))
1880 0 : && JUMP_P (trial)
1881 0 : && simplejump_p (trial)
1882 0 : && eligible_for_delay (insn, slots_filled, trial, flags)
1883 0 : && no_labels_between_p (insn, trial)
1884 0 : && ! can_throw_internal (trial))
1885 : {
1886 0 : rtx_insn **tmp;
1887 0 : slots_filled++;
1888 0 : add_to_delay_list (trial, &delay_list);
1889 :
1890 : /* TRIAL may have had its delay slot filled, then unfilled. When
1891 : the delay slot is unfilled, TRIAL is placed back on the unfilled
1892 : slots obstack. Unfortunately, it is placed on the end of the
1893 : obstack, not in its original location. Therefore, we must search
1894 : from entry i + 1 to the end of the unfilled slots obstack to
1895 : try and find TRIAL. */
1896 0 : tmp = &unfilled_slots_base[i + 1];
1897 0 : while (*tmp != trial && tmp != unfilled_slots_next)
1898 0 : tmp++;
1899 :
1900 : /* Remove the unconditional jump from consideration for delay slot
1901 : filling and unthread it. */
1902 0 : if (*tmp == trial)
1903 0 : *tmp = 0;
1904 0 : {
1905 0 : rtx_insn *next = NEXT_INSN (trial);
1906 0 : rtx_insn *prev = PREV_INSN (trial);
1907 0 : if (prev)
1908 0 : SET_NEXT_INSN (prev) = next;
1909 0 : if (next)
1910 0 : SET_PREV_INSN (next) = prev;
1911 : }
1912 : }
1913 :
1914 : /* Now, scan backwards from the insn to search for a potential
1915 : delay-slot candidate. Stop searching when a label or jump is hit.
1916 :
1917 : For each candidate, if it is to go into the delay slot (moved
1918 : forward in execution sequence), it must not need or set any resources
1919 : that were set by later insns and must not set any resources that
1920 : are needed for those insns.
1921 :
1922 : The delay slot insn itself sets resources unless it is a call
1923 : (in which case the called routine, not the insn itself, is doing
1924 : the setting). */
1925 :
1926 0 : if (slots_filled < slots_to_fill)
1927 : {
1928 : /* If the flags register is dead after the insn, then we want to be
1929 : able to accept a candidate that clobbers it. For this purpose,
1930 : we need to filter the flags register during life analysis, so
1931 : that it doesn't create RAW and WAW dependencies, while still
1932 : creating the necessary WAR dependencies. */
1933 0 : bool filter_flags
1934 : = (slots_to_fill == 1
1935 0 : && targetm.flags_regnum != INVALID_REGNUM
1936 0 : && find_regno_note (insn, REG_DEAD, targetm.flags_regnum));
1937 0 : struct resources fset;
1938 0 : CLEAR_RESOURCE (&needed);
1939 0 : CLEAR_RESOURCE (&set);
1940 0 : mark_set_resources (insn, &set, 0, MARK_SRC_DEST);
1941 0 : if (filter_flags)
1942 : {
1943 0 : CLEAR_RESOURCE (&fset);
1944 0 : mark_set_resources (insn, &fset, 0, MARK_SRC_DEST);
1945 : }
1946 0 : mark_referenced_resources (insn, &needed, false);
1947 :
1948 0 : for (trial = prev_nonnote_insn (insn); ! stop_search_p (trial, true);
1949 0 : trial = next_trial)
1950 : {
1951 0 : next_trial = prev_nonnote_insn (trial);
1952 :
1953 : /* This must be an INSN or CALL_INSN. */
1954 0 : pat = PATTERN (trial);
1955 :
1956 : /* Stand-alone USE and CLOBBER are just for flow. */
1957 0 : if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
1958 0 : continue;
1959 :
1960 : /* And DEBUG_INSNs never go into delay slots. */
1961 0 : if (GET_CODE (trial) == DEBUG_INSN)
1962 0 : continue;
1963 :
1964 : /* Check for resource conflict first, to avoid unnecessary
1965 : splitting. */
1966 0 : if (! insn_references_resource_p (trial, &set, true)
1967 0 : && ! insn_sets_resource_p (trial,
1968 : filter_flags ? &fset : &set,
1969 : true)
1970 0 : && ! insn_sets_resource_p (trial, &needed, true)
1971 0 : && ! can_throw_internal (trial))
1972 : {
1973 0 : trial = try_split (pat, trial, 1);
1974 0 : next_trial = prev_nonnote_insn (trial);
1975 0 : if (eligible_for_delay (insn, slots_filled, trial, flags))
1976 : {
1977 : /* In this case, we are searching backward, so if we
1978 : find insns to put on the delay list, we want
1979 : to put them at the head, rather than the
1980 : tail, of the list. */
1981 :
1982 0 : update_reg_dead_notes (trial, insn);
1983 0 : delay_list.safe_insert (0, trial);
1984 0 : update_block (trial, trial);
1985 0 : delete_related_insns (trial);
1986 0 : if (slots_to_fill == ++slots_filled)
1987 : break;
1988 0 : continue;
1989 : }
1990 : }
1991 :
1992 0 : mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
1993 0 : if (filter_flags)
1994 : {
1995 0 : mark_set_resources (trial, &fset, 0, MARK_SRC_DEST_CALL);
1996 : /* If the flags register is set, then it doesn't create RAW
1997 : dependencies any longer and it also doesn't create WAW
1998 : dependencies since it's dead after the original insn. */
1999 0 : if (TEST_HARD_REG_BIT (fset.regs, targetm.flags_regnum))
2000 : {
2001 0 : CLEAR_HARD_REG_BIT (needed.regs, targetm.flags_regnum);
2002 0 : CLEAR_HARD_REG_BIT (fset.regs, targetm.flags_regnum);
2003 : }
2004 : }
2005 0 : mark_referenced_resources (trial, &needed, true);
2006 : }
2007 : }
2008 :
2009 : /* If all needed slots haven't been filled, we come here. */
2010 :
2011 : /* Try to optimize case of jumping around a single insn. */
2012 0 : if ((ANNUL_IFTRUE_SLOTS || ANNUL_IFFALSE_SLOTS)
2013 : && slots_filled != slots_to_fill
2014 : && delay_list.is_empty ()
2015 : && JUMP_P (insn)
2016 : && (condjump_p (insn) || condjump_in_parallel_p (insn))
2017 : && !ANY_RETURN_P (JUMP_LABEL (insn)))
2018 : {
2019 : optimize_skip (as_a <rtx_jump_insn *> (insn), &delay_list);
2020 : if (!delay_list.is_empty ())
2021 : slots_filled += 1;
2022 : }
2023 :
2024 : /* Try to get insns from beyond the insn needing the delay slot.
2025 : These insns can neither set or reference resources set in insns being
2026 : skipped, cannot set resources in the insn being skipped, and, if this
2027 : is a CALL_INSN (or a CALL_INSN is passed), cannot trap (because the
2028 : call might not return).
2029 :
2030 : There used to be code which continued past the target label if
2031 : we saw all uses of the target label. This code did not work,
2032 : because it failed to account for some instructions which were
2033 : both annulled and marked as from the target. This can happen as a
2034 : result of optimize_skip. Since this code was redundant with
2035 : fill_eager_delay_slots anyways, it was just deleted. */
2036 :
2037 0 : if (slots_filled != slots_to_fill
2038 : /* If this instruction could throw an exception which is
2039 : caught in the same function, then it's not safe to fill
2040 : the delay slot with an instruction from beyond this
2041 : point. For example, consider:
2042 :
2043 : int i = 2;
2044 :
2045 : try {
2046 : f();
2047 : i = 3;
2048 : } catch (...) {}
2049 :
2050 : return i;
2051 :
2052 : Even though `i' is a local variable, we must be sure not
2053 : to put `i = 3' in the delay slot if `f' might throw an
2054 : exception.
2055 :
2056 : Presumably, we should also check to see if we could get
2057 : back to this function via `setjmp'. */
2058 0 : && ! can_throw_internal (insn)
2059 0 : && !JUMP_P (insn))
2060 : {
2061 0 : bool maybe_never = false;
2062 0 : rtx pat, trial_delay;
2063 :
2064 0 : CLEAR_RESOURCE (&needed);
2065 0 : CLEAR_RESOURCE (&set);
2066 0 : mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
2067 0 : mark_referenced_resources (insn, &needed, true);
2068 :
2069 0 : if (CALL_P (insn))
2070 0 : maybe_never = true;
2071 :
2072 0 : for (trial = next_nonnote_insn (insn); !stop_search_p (trial, true);
2073 0 : trial = next_trial)
2074 : {
2075 0 : next_trial = next_nonnote_insn (trial);
2076 :
2077 : /* This must be an INSN or CALL_INSN. */
2078 0 : pat = PATTERN (trial);
2079 :
2080 : /* Stand-alone USE and CLOBBER are just for flow. */
2081 0 : if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2082 0 : continue;
2083 :
2084 : /* And DEBUG_INSNs do not go in delay slots. */
2085 0 : if (GET_CODE (trial) == DEBUG_INSN)
2086 0 : continue;
2087 :
2088 : /* If this already has filled delay slots, get the insn needing
2089 : the delay slots. */
2090 0 : rtx_sequence *delay_seq = dyn_cast <rtx_sequence *> (pat);
2091 0 : if (delay_seq)
2092 0 : trial_delay = delay_seq->insn (0);
2093 : else
2094 : trial_delay = trial;
2095 :
2096 : /* Stop our search when seeing a jump. */
2097 0 : if (JUMP_P (trial_delay))
2098 : break;
2099 :
2100 : /* See if we have a resource problem before we try to split. */
2101 0 : if (!delay_seq
2102 0 : && ! insn_references_resource_p (trial, &set, true)
2103 0 : && ! insn_sets_resource_p (trial, &set, true)
2104 0 : && ! insn_sets_resource_p (trial, &needed, true)
2105 0 : && ! (maybe_never && may_trap_or_fault_p (pat))
2106 0 : && (trial = try_split (pat, trial, 0))
2107 0 : && eligible_for_delay (insn, slots_filled, trial, flags)
2108 0 : && ! can_throw_internal (trial))
2109 : {
2110 0 : next_trial = next_nonnote_insn (trial);
2111 0 : add_to_delay_list (trial, &delay_list);
2112 :
2113 0 : delete_related_insns (trial);
2114 0 : if (slots_to_fill == ++slots_filled)
2115 : break;
2116 0 : continue;
2117 : }
2118 :
2119 0 : mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2120 0 : mark_referenced_resources (trial, &needed, true);
2121 :
2122 : /* Ensure we don't put insns between the setting of cc and the
2123 : comparison by moving a setting of cc into an earlier delay
2124 : slot since these insns could clobber the condition code. */
2125 0 : set.cc = 1;
2126 :
2127 : /* If this is a call, we might not get here. */
2128 0 : if (CALL_P (trial_delay))
2129 0 : maybe_never = true;
2130 : }
2131 :
2132 : /* If there are slots left to fill and our search was stopped by an
2133 : unconditional branch, try the insn at the branch target. We can
2134 : redirect the branch if it works.
2135 :
2136 : Don't do this if the insn at the branch target is a branch. */
2137 0 : if (slots_to_fill != slots_filled
2138 0 : && trial
2139 0 : && jump_to_label_p (trial)
2140 0 : && simplejump_p (trial)
2141 0 : && (next_trial = next_active_insn (JUMP_LABEL_AS_INSN (trial))) != 0
2142 0 : && ! (NONJUMP_INSN_P (next_trial)
2143 0 : && is_a <rtx_sequence *> (PATTERN (next_trial)))
2144 0 : && !JUMP_P (next_trial)
2145 0 : && ! insn_references_resource_p (next_trial, &set, true)
2146 0 : && ! insn_sets_resource_p (next_trial, &set, true)
2147 0 : && ! insn_sets_resource_p (next_trial, &needed, true)
2148 0 : && ! (maybe_never && may_trap_or_fault_p (PATTERN (next_trial)))
2149 0 : && (next_trial = try_split (PATTERN (next_trial), next_trial, 0))
2150 0 : && eligible_for_delay (insn, slots_filled, next_trial, flags)
2151 0 : && ! can_throw_internal (trial))
2152 : {
2153 : /* See comment in relax_delay_slots about necessity of using
2154 : next_real_nondebug_insn here. */
2155 0 : rtx_insn *new_label = next_real_nondebug_insn (next_trial);
2156 :
2157 0 : if (new_label != 0)
2158 0 : new_label = get_label_before (new_label, JUMP_LABEL (trial));
2159 : else
2160 0 : new_label = find_end_label (simple_return_rtx);
2161 :
2162 0 : if (new_label)
2163 : {
2164 0 : add_to_delay_list (copy_delay_slot_insn (next_trial),
2165 : &delay_list);
2166 0 : slots_filled++;
2167 0 : reorg_redirect_jump (as_a <rtx_jump_insn *> (trial),
2168 : new_label);
2169 : }
2170 : }
2171 : }
2172 :
2173 : /* If this is an unconditional jump, then try to get insns from the
2174 : target of the jump. */
2175 0 : rtx_jump_insn *jump_insn;
2176 0 : if ((jump_insn = dyn_cast <rtx_jump_insn *> (insn))
2177 0 : && simplejump_p (jump_insn)
2178 0 : && slots_filled != slots_to_fill)
2179 0 : fill_slots_from_thread (jump_insn, const_true_rtx,
2180 0 : next_active_insn (JUMP_LABEL_AS_INSN (insn)),
2181 : NULL, 1, 1, own_thread_p (JUMP_LABEL (insn),
2182 : JUMP_LABEL (insn), false),
2183 : slots_to_fill, &slots_filled, &delay_list);
2184 :
2185 0 : if (!delay_list.is_empty ())
2186 0 : unfilled_slots_base[i]
2187 0 : = emit_delay_sequence (insn, delay_list, slots_filled);
2188 :
2189 0 : if (slots_to_fill == slots_filled)
2190 0 : unfilled_slots_base[i] = 0;
2191 :
2192 0 : note_delay_statistics (slots_filled, 0);
2193 : }
2194 0 : }
2195 :
2196 : /* Follow any unconditional jump at LABEL, for the purpose of redirecting JUMP;
2197 : return the ultimate label reached by any such chain of jumps.
2198 : Return a suitable return rtx if the chain ultimately leads to a
2199 : return instruction.
2200 : If LABEL is not followed by a jump, return LABEL.
2201 : If the chain loops or we can't find end, return LABEL,
2202 : since that tells caller to avoid changing the insn.
2203 : If the returned label is obtained by following a crossing jump,
2204 : set *CROSSING to true, otherwise set it to false. */
2205 :
2206 : static rtx
2207 0 : follow_jumps (rtx label, rtx_insn *jump, bool *crossing)
2208 : {
2209 0 : rtx_insn *insn;
2210 0 : rtx_insn *next;
2211 0 : int depth;
2212 :
2213 0 : *crossing = false;
2214 0 : if (ANY_RETURN_P (label))
2215 : return label;
2216 :
2217 0 : rtx_insn *value = as_a <rtx_insn *> (label);
2218 :
2219 0 : for (depth = 0;
2220 : (depth < 10
2221 0 : && (insn = next_active_insn (value)) != 0
2222 0 : && JUMP_P (insn)
2223 0 : && JUMP_LABEL (insn) != NULL_RTX
2224 0 : && ((any_uncondjump_p (insn) && onlyjump_p (insn))
2225 0 : || ANY_RETURN_P (PATTERN (insn)))
2226 0 : && (next = NEXT_INSN (insn))
2227 0 : && BARRIER_P (next));
2228 : depth++)
2229 : {
2230 0 : rtx this_label_or_return = JUMP_LABEL (insn);
2231 :
2232 : /* If we have found a cycle, make the insn jump to itself. */
2233 0 : if (this_label_or_return == label)
2234 : return label;
2235 :
2236 : /* Cannot follow returns and cannot look through tablejumps. */
2237 0 : if (ANY_RETURN_P (this_label_or_return))
2238 : return this_label_or_return;
2239 :
2240 0 : rtx_insn *this_label = as_a <rtx_insn *> (this_label_or_return);
2241 0 : if (NEXT_INSN (this_label)
2242 0 : && JUMP_TABLE_DATA_P (NEXT_INSN (this_label)))
2243 : break;
2244 :
2245 0 : if (!targetm.can_follow_jump (jump, insn))
2246 : break;
2247 0 : if (!*crossing)
2248 0 : *crossing = CROSSING_JUMP_P (jump);
2249 0 : value = this_label;
2250 : }
2251 0 : if (depth == 10)
2252 0 : return label;
2253 : return value;
2254 : }
2255 :
2256 : /* Try to find insns to place in delay slots.
2257 :
2258 : INSN is the jump needing SLOTS_TO_FILL delay slots. It tests CONDITION
2259 : or is an unconditional branch if CONDITION is const_true_rtx.
2260 : *PSLOTS_FILLED is updated with the number of slots that we have filled.
2261 :
2262 : THREAD is a flow-of-control, either the insns to be executed if the
2263 : branch is true or if the branch is false, THREAD_IF_TRUE says which.
2264 :
2265 : OPPOSITE_THREAD is the thread in the opposite direction. It is used
2266 : to see if any potential delay slot insns set things needed there.
2267 :
2268 : LIKELY is true if it is extremely likely that the branch will be
2269 : taken and THREAD_IF_TRUE is set. This is used for the branch at the
2270 : end of a loop back up to the top.
2271 :
2272 : OWN_THREAD is true if we are the only user of the thread, i.e. it is
2273 : the target of the jump when we are the only jump going there.
2274 :
2275 : If OWN_THREAD is false, it must be the "true" thread of a jump. In that
2276 : case, we can only take insns from the head of the thread for our delay
2277 : slot. We then adjust the jump to point after the insns we have taken. */
2278 :
2279 : static void
2280 0 : fill_slots_from_thread (rtx_jump_insn *insn, rtx condition,
2281 : rtx thread_or_return, rtx opposite_thread, bool likely,
2282 : bool thread_if_true, bool own_thread, int slots_to_fill,
2283 : int *pslots_filled, vec<rtx_insn *> *delay_list)
2284 : {
2285 0 : rtx_insn *new_thread;
2286 0 : struct resources opposite_needed, set, needed;
2287 0 : rtx_insn *trial;
2288 0 : bool lose = false;
2289 0 : bool must_annul = false;
2290 0 : int flags;
2291 :
2292 : /* Validate our arguments. */
2293 0 : gcc_assert (condition != const_true_rtx || thread_if_true);
2294 0 : gcc_assert (own_thread || thread_if_true);
2295 :
2296 0 : flags = get_jump_flags (insn, JUMP_LABEL (insn));
2297 :
2298 : /* If our thread is the end of subroutine, we can't get any delay
2299 : insns from that. */
2300 0 : if (thread_or_return == NULL_RTX || ANY_RETURN_P (thread_or_return))
2301 0 : return;
2302 :
2303 0 : rtx_insn *thread = as_a <rtx_insn *> (thread_or_return);
2304 :
2305 : /* If this is an unconditional branch, nothing is needed at the
2306 : opposite thread. Otherwise, compute what is needed there. */
2307 0 : if (condition == const_true_rtx)
2308 0 : CLEAR_RESOURCE (&opposite_needed);
2309 : else
2310 0 : mark_target_live_regs (get_insns (), opposite_thread, &opposite_needed);
2311 :
2312 : /* If the insn at THREAD can be split, do it here to avoid having to
2313 : update THREAD and NEW_THREAD if it is done in the loop below. Also
2314 : initialize NEW_THREAD. */
2315 :
2316 0 : new_thread = thread = try_split (PATTERN (thread), thread, 0);
2317 :
2318 : /* Scan insns at THREAD. We are looking for an insn that can be removed
2319 : from THREAD (it neither sets nor references resources that were set
2320 : ahead of it and it doesn't set anything needs by the insns ahead of
2321 : it) and that either can be placed in an annulling insn or aren't
2322 : needed at OPPOSITE_THREAD. */
2323 :
2324 0 : CLEAR_RESOURCE (&needed);
2325 0 : CLEAR_RESOURCE (&set);
2326 :
2327 : /* Handle the flags register specially, to be able to accept a
2328 : candidate that clobbers it. See also fill_simple_delay_slots. */
2329 0 : bool filter_flags
2330 : = (slots_to_fill == 1
2331 0 : && targetm.flags_regnum != INVALID_REGNUM
2332 0 : && find_regno_note (insn, REG_DEAD, targetm.flags_regnum));
2333 0 : struct resources fset;
2334 0 : struct resources flags_res;
2335 0 : if (filter_flags)
2336 : {
2337 0 : CLEAR_RESOURCE (&fset);
2338 0 : CLEAR_RESOURCE (&flags_res);
2339 0 : SET_HARD_REG_BIT (flags_res.regs, targetm.flags_regnum);
2340 : }
2341 :
2342 : /* If we do not own this thread, we must stop as soon as we find
2343 : something that we can't put in a delay slot, since all we can do
2344 : is branch into THREAD at a later point. Therefore, labels stop
2345 : the search if this is not the `true' thread. */
2346 :
2347 0 : for (trial = thread;
2348 0 : ! stop_search_p (trial, ! thread_if_true) && (! lose || own_thread);
2349 0 : trial = next_nonnote_insn (trial))
2350 : {
2351 0 : rtx pat;
2352 0 : rtx_insn *old_trial;
2353 :
2354 : /* If we have passed a label, we no longer own this thread. */
2355 0 : if (LABEL_P (trial))
2356 : {
2357 0 : own_thread = 0;
2358 0 : continue;
2359 : }
2360 :
2361 0 : pat = PATTERN (trial);
2362 0 : if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
2363 0 : continue;
2364 :
2365 0 : if (GET_CODE (trial) == DEBUG_INSN)
2366 0 : continue;
2367 :
2368 : /* If TRIAL conflicts with the insns ahead of it, we lose. */
2369 0 : if (! insn_references_resource_p (trial, &set, true)
2370 0 : && ! insn_sets_resource_p (trial, filter_flags ? &fset : &set, true)
2371 0 : && ! insn_sets_resource_p (trial, &needed, true)
2372 : /* If we're handling sets to the flags register specially, we
2373 : only allow an insn into a delay-slot, if it either:
2374 : - doesn't set the flags register,
2375 : - the "set" of the flags register isn't used (clobbered),
2376 : - insns between the delay-slot insn and the trial-insn
2377 : as accounted in "set", have not affected the flags register. */
2378 0 : && (! filter_flags
2379 0 : || ! insn_sets_resource_p (trial, &flags_res, true)
2380 0 : || find_regno_note (trial, REG_UNUSED, targetm.flags_regnum)
2381 0 : || ! TEST_HARD_REG_BIT (set.regs, targetm.flags_regnum))
2382 0 : && ! can_throw_internal (trial))
2383 : {
2384 0 : rtx_insn *prior_insn;
2385 :
2386 : /* If TRIAL is redundant with some insn before INSN, we don't
2387 : actually need to add it to the delay list; we can merely pretend
2388 : we did. */
2389 0 : if ((prior_insn = redundant_insn (trial, insn, *delay_list)))
2390 : {
2391 0 : fix_reg_dead_note (prior_insn, insn);
2392 0 : if (own_thread)
2393 : {
2394 0 : update_block (trial, thread);
2395 0 : if (trial == thread)
2396 : {
2397 0 : thread = next_active_insn (thread);
2398 0 : if (new_thread == trial)
2399 0 : new_thread = thread;
2400 : }
2401 :
2402 0 : delete_related_insns (trial);
2403 : }
2404 : else
2405 : {
2406 0 : update_reg_unused_notes (prior_insn, trial);
2407 0 : new_thread = next_active_insn (trial);
2408 : }
2409 :
2410 0 : continue;
2411 : }
2412 :
2413 : /* There are two ways we can win: If TRIAL doesn't set anything
2414 : needed at the opposite thread and can't trap, or if it can
2415 : go into an annulled delay slot. But we want neither to copy
2416 : nor to speculate frame-related insns. */
2417 0 : if (!must_annul
2418 0 : && ((condition == const_true_rtx
2419 0 : && (own_thread || !RTX_FRAME_RELATED_P (trial)))
2420 0 : || (! insn_sets_resource_p (trial, &opposite_needed, true)
2421 0 : && ! may_trap_or_fault_p (pat)
2422 0 : && ! RTX_FRAME_RELATED_P (trial))))
2423 : {
2424 0 : old_trial = trial;
2425 0 : trial = try_split (pat, trial, 0);
2426 0 : if (new_thread == old_trial)
2427 0 : new_thread = trial;
2428 0 : if (thread == old_trial)
2429 0 : thread = trial;
2430 0 : pat = PATTERN (trial);
2431 0 : if (eligible_for_delay (insn, *pslots_filled, trial, flags))
2432 0 : goto winner;
2433 : }
2434 0 : else if (!RTX_FRAME_RELATED_P (trial)
2435 : && ((ANNUL_IFTRUE_SLOTS && ! thread_if_true)
2436 : || (ANNUL_IFFALSE_SLOTS && thread_if_true)))
2437 : {
2438 : old_trial = trial;
2439 : trial = try_split (pat, trial, 0);
2440 : if (new_thread == old_trial)
2441 : new_thread = trial;
2442 : if (thread == old_trial)
2443 : thread = trial;
2444 : pat = PATTERN (trial);
2445 : if ((must_annul || delay_list->is_empty ()) && (thread_if_true
2446 : ? check_annul_list_true_false (false, *delay_list)
2447 : && eligible_for_annul_false (insn, *pslots_filled, trial, flags)
2448 : : check_annul_list_true_false (true, *delay_list)
2449 : && eligible_for_annul_true (insn, *pslots_filled, trial, flags)))
2450 : {
2451 : rtx_insn *temp;
2452 :
2453 : must_annul = true;
2454 0 : winner:
2455 :
2456 : /* If we own this thread, delete the insn. If this is the
2457 : destination of a branch, show that a basic block status
2458 : may have been updated. In any case, mark the new
2459 : starting point of this thread. */
2460 0 : if (own_thread)
2461 : {
2462 0 : rtx note;
2463 :
2464 0 : update_block (trial, thread);
2465 0 : if (trial == thread)
2466 : {
2467 0 : thread = next_active_insn (thread);
2468 0 : if (new_thread == trial)
2469 0 : new_thread = thread;
2470 : }
2471 :
2472 : /* We are moving this insn, not deleting it. We must
2473 : temporarily increment the use count on any referenced
2474 : label lest it be deleted by delete_related_insns. */
2475 0 : for (note = REG_NOTES (trial);
2476 0 : note != NULL_RTX;
2477 0 : note = XEXP (note, 1))
2478 0 : if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2479 0 : || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2480 : {
2481 : /* REG_LABEL_OPERAND could be
2482 : NOTE_INSN_DELETED_LABEL too. */
2483 0 : if (LABEL_P (XEXP (note, 0)))
2484 0 : LABEL_NUSES (XEXP (note, 0))++;
2485 : else
2486 0 : gcc_assert (REG_NOTE_KIND (note)
2487 : == REG_LABEL_OPERAND);
2488 : }
2489 0 : if (jump_to_label_p (trial))
2490 0 : LABEL_NUSES (JUMP_LABEL (trial))++;
2491 :
2492 0 : delete_related_insns (trial);
2493 :
2494 0 : for (note = REG_NOTES (trial);
2495 0 : note != NULL_RTX;
2496 0 : note = XEXP (note, 1))
2497 0 : if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
2498 0 : || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
2499 : {
2500 : /* REG_LABEL_OPERAND could be
2501 : NOTE_INSN_DELETED_LABEL too. */
2502 0 : if (LABEL_P (XEXP (note, 0)))
2503 0 : LABEL_NUSES (XEXP (note, 0))--;
2504 : else
2505 0 : gcc_assert (REG_NOTE_KIND (note)
2506 : == REG_LABEL_OPERAND);
2507 : }
2508 0 : if (jump_to_label_p (trial))
2509 0 : LABEL_NUSES (JUMP_LABEL (trial))--;
2510 : }
2511 : else
2512 0 : new_thread = next_active_insn (trial);
2513 :
2514 0 : temp = own_thread ? trial : copy_delay_slot_insn (trial);
2515 0 : if (thread_if_true)
2516 0 : set_insn_from_target (temp, true);
2517 :
2518 0 : add_to_delay_list (temp, delay_list);
2519 :
2520 0 : if (slots_to_fill == ++(*pslots_filled))
2521 : {
2522 : /* Even though we have filled all the slots, we
2523 : may be branching to a location that has a
2524 : redundant insn. Skip any if so. */
2525 0 : while (new_thread && ! own_thread
2526 0 : && ! insn_sets_resource_p (new_thread, &set, true)
2527 0 : && ! insn_sets_resource_p (new_thread, &needed,
2528 : true)
2529 0 : && ! insn_references_resource_p (new_thread,
2530 : &set, true)
2531 0 : && (prior_insn
2532 0 : = redundant_insn (new_thread, insn,
2533 : *delay_list)))
2534 : {
2535 : /* We know we do not own the thread, so no need
2536 : to call update_block and delete_insn. */
2537 0 : fix_reg_dead_note (prior_insn, insn);
2538 0 : update_reg_unused_notes (prior_insn, new_thread);
2539 0 : new_thread = next_active_insn (new_thread);
2540 : }
2541 : break;
2542 : }
2543 :
2544 0 : continue;
2545 0 : }
2546 : }
2547 : }
2548 :
2549 : /* This insn can't go into a delay slot. */
2550 0 : lose = true;
2551 0 : mark_set_resources (trial, &set, 0, MARK_SRC_DEST_CALL);
2552 0 : mark_referenced_resources (trial, &needed, true);
2553 0 : if (filter_flags)
2554 : {
2555 0 : mark_set_resources (trial, &fset, 0, MARK_SRC_DEST_CALL);
2556 :
2557 : /* Groups of flags-register setters with users should not
2558 : affect opportunities to move flags-register-setting insns
2559 : (clobbers) into the delay-slot. */
2560 0 : CLEAR_HARD_REG_BIT (needed.regs, targetm.flags_regnum);
2561 0 : CLEAR_HARD_REG_BIT (fset.regs, targetm.flags_regnum);
2562 : }
2563 :
2564 : /* Ensure we don't put insns between the setting of cc and the comparison
2565 : by moving a setting of cc into an earlier delay slot since these insns
2566 : could clobber the condition code. */
2567 0 : set.cc = 1;
2568 :
2569 : /* If this insn is a register-register copy and the next insn has
2570 : a use of our destination, change it to use our source. That way,
2571 : it will become a candidate for our delay slot the next time
2572 : through this loop. This case occurs commonly in loops that
2573 : scan a list.
2574 :
2575 : We could check for more complex cases than those tested below,
2576 : but it doesn't seem worth it. It might also be a good idea to try
2577 : to swap the two insns. That might do better.
2578 :
2579 : We can't do this if the next insn modifies our destination, because
2580 : that would make the replacement into the insn invalid. We also can't
2581 : do this if it modifies our source, because it might be an earlyclobber
2582 : operand. This latter test also prevents updating the contents of
2583 : a PRE_INC. We also can't do this if there's overlap of source and
2584 : destination. Overlap may happen for larger-than-register-size modes. */
2585 :
2586 0 : if (NONJUMP_INSN_P (trial) && GET_CODE (pat) == SET
2587 0 : && REG_P (SET_SRC (pat))
2588 0 : && REG_P (SET_DEST (pat))
2589 0 : && !reg_overlap_mentioned_p (SET_DEST (pat), SET_SRC (pat)))
2590 : {
2591 0 : rtx_insn *next = next_nonnote_nondebug_insn (trial);
2592 :
2593 0 : if (next && NONJUMP_INSN_P (next)
2594 0 : && GET_CODE (PATTERN (next)) != USE
2595 0 : && ! reg_set_p (SET_DEST (pat), next)
2596 0 : && ! reg_set_p (SET_SRC (pat), next)
2597 0 : && reg_referenced_p (SET_DEST (pat), PATTERN (next))
2598 0 : && ! modified_in_p (SET_DEST (pat), next))
2599 0 : validate_replace_rtx (SET_DEST (pat), SET_SRC (pat), next);
2600 : }
2601 : }
2602 :
2603 : /* Past here the thread can be a RETURN or a label. */
2604 0 : rtx thread_tail = new_thread;
2605 : /* If we stopped on a branch insn that has delay slots, see if we can
2606 : steal some of the insns in those slots. */
2607 0 : rtx_sequence *sequence;
2608 0 : if (trial && NONJUMP_INSN_P (trial)
2609 0 : && (sequence = dyn_cast <rtx_sequence *> (PATTERN (trial)))
2610 0 : && JUMP_P (sequence->insn (0)))
2611 : {
2612 : /* If this is the `true' thread, we will want to follow the jump,
2613 : so we can only do this if we have taken everything up to here. */
2614 0 : if (thread_if_true && trial == thread_tail)
2615 : {
2616 0 : steal_delay_list_from_target (insn, condition, sequence,
2617 : delay_list, &set, &needed,
2618 : &opposite_needed, slots_to_fill,
2619 : pslots_filled, &must_annul,
2620 : &thread_tail);
2621 : /* If we owned the thread and are told that it branched
2622 : elsewhere, make sure we own the thread at the new location. */
2623 0 : if (own_thread && trial != thread_tail)
2624 0 : own_thread = own_thread_p (thread_tail, thread_tail, false);
2625 : }
2626 : else if (! thread_if_true)
2627 0 : steal_delay_list_from_fallthrough (insn, condition, sequence,
2628 : delay_list, &set, &needed,
2629 : &opposite_needed, slots_to_fill,
2630 : pslots_filled, &must_annul);
2631 : }
2632 :
2633 : /* If we haven't found anything for this delay slot and it is very
2634 : likely that the branch will be taken, see if the insn at our target
2635 : increments or decrements a register with an increment that does not
2636 : depend on the destination register. If so, try to place the opposite
2637 : arithmetic insn after the jump insn and put the arithmetic insn in the
2638 : delay slot. If we can't do this, return. */
2639 0 : if (delay_list->is_empty () && likely
2640 0 : && thread_tail
2641 0 : && !ANY_RETURN_P (thread_tail)
2642 0 : && NONJUMP_INSN_P (thread_tail)
2643 0 : && !RTX_FRAME_RELATED_P (thread_tail)
2644 0 : && GET_CODE (PATTERN (thread_tail)) != ASM_INPUT
2645 0 : && asm_noperands (PATTERN (thread_tail)) < 0)
2646 : {
2647 0 : rtx dest;
2648 0 : rtx src;
2649 :
2650 : /* We know "thread_tail" is an insn due to NONJUMP_INSN_P (thread_tail)
2651 : above. */
2652 0 : trial = as_a <rtx_insn *> (thread_tail);
2653 0 : rtx pat = PATTERN (trial);
2654 :
2655 0 : if (!NONJUMP_INSN_P (trial)
2656 0 : || GET_CODE (pat) != SET
2657 0 : || ! eligible_for_delay (insn, 0, trial, flags)
2658 0 : || can_throw_internal (trial))
2659 : return;
2660 :
2661 0 : dest = SET_DEST (pat), src = SET_SRC (pat);
2662 0 : if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
2663 0 : && rtx_equal_p (XEXP (src, 0), dest)
2664 0 : && (!FLOAT_MODE_P (GET_MODE (src))
2665 0 : || flag_unsafe_math_optimizations)
2666 0 : && ! reg_overlap_mentioned_p (dest, XEXP (src, 1))
2667 0 : && ! side_effects_p (pat))
2668 : {
2669 0 : rtx other = XEXP (src, 1);
2670 0 : rtx new_arith;
2671 0 : rtx_insn *ninsn;
2672 :
2673 : /* If this is a constant adjustment, use the same code with
2674 : the negated constant. Otherwise, reverse the sense of the
2675 : arithmetic. */
2676 0 : if (CONST_INT_P (other))
2677 0 : new_arith = gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src), dest,
2678 : negate_rtx (GET_MODE (src), other));
2679 : else
2680 0 : new_arith = gen_rtx_fmt_ee (GET_CODE (src) == PLUS ? MINUS : PLUS,
2681 : GET_MODE (src), dest, other);
2682 :
2683 0 : ninsn = emit_insn_after (gen_rtx_SET (dest, new_arith), insn);
2684 :
2685 0 : if (recog_memoized (ninsn) < 0
2686 0 : || (extract_insn (ninsn),
2687 0 : !constrain_operands (1, get_preferred_alternatives (ninsn))))
2688 : {
2689 0 : delete_related_insns (ninsn);
2690 0 : return;
2691 : }
2692 :
2693 0 : if (own_thread)
2694 : {
2695 0 : update_block (trial, thread);
2696 0 : if (trial == thread)
2697 : {
2698 0 : thread = next_active_insn (thread);
2699 0 : if (thread_tail == trial)
2700 0 : thread_tail = thread;
2701 : }
2702 0 : delete_related_insns (trial);
2703 : }
2704 : else
2705 0 : thread_tail = next_active_insn (trial);
2706 :
2707 0 : ninsn = own_thread ? trial : copy_delay_slot_insn (trial);
2708 0 : if (thread_if_true)
2709 0 : set_insn_from_target (ninsn, true);
2710 :
2711 0 : add_to_delay_list (ninsn, delay_list);
2712 0 : (*pslots_filled)++;
2713 : }
2714 : }
2715 :
2716 0 : if (!delay_list->is_empty () && must_annul)
2717 0 : INSN_ANNULLED_BRANCH_P (insn) = 1;
2718 :
2719 : /* If we are to branch into the middle of this thread, find an appropriate
2720 : label or make a new one if none, and redirect INSN to it. If we hit the
2721 : end of the function, use the end-of-function label. */
2722 0 : if (thread_tail != thread)
2723 : {
2724 0 : rtx label;
2725 0 : bool crossing = false;
2726 :
2727 0 : gcc_assert (thread_if_true);
2728 :
2729 0 : if (thread_tail
2730 0 : && simplejump_or_return_p (thread_tail)
2731 0 : && redirect_with_delay_list_safe_p (insn,
2732 : JUMP_LABEL (thread_tail),
2733 : *delay_list))
2734 0 : thread_tail = follow_jumps (JUMP_LABEL (thread_tail), insn, &crossing);
2735 :
2736 0 : if (!thread_tail)
2737 0 : label = find_end_label (simple_return_rtx);
2738 0 : else if (ANY_RETURN_P (thread_tail))
2739 0 : label = find_end_label (thread_tail);
2740 0 : else if (LABEL_P (thread_tail))
2741 : label = thread_tail;
2742 : else
2743 0 : label = get_label_before (as_a <rtx_insn *> (thread_tail),
2744 : JUMP_LABEL (insn));
2745 :
2746 0 : if (label)
2747 : {
2748 0 : reorg_redirect_jump (insn, label);
2749 0 : if (crossing)
2750 0 : set_crossing_jump (insn, true);
2751 : }
2752 : }
2753 : }
2754 :
2755 : /* Make another attempt to find insns to place in delay slots.
2756 :
2757 : We previously looked for insns located in front of the delay insn
2758 : and, for non-jump delay insns, located behind the delay insn.
2759 :
2760 : Here only try to schedule jump insns and try to move insns from either
2761 : the target or the following insns into the delay slot. If annulling is
2762 : supported, we will be likely to do this. Otherwise, we can do this only
2763 : if safe. */
2764 :
2765 : static void
2766 0 : fill_eager_delay_slots (void)
2767 : {
2768 0 : rtx_insn *insn;
2769 0 : int i;
2770 0 : int num_unfilled_slots = unfilled_slots_next - unfilled_slots_base;
2771 :
2772 0 : for (i = 0; i < num_unfilled_slots; i++)
2773 : {
2774 0 : rtx condition;
2775 0 : rtx target_label, insn_at_target;
2776 0 : rtx_insn *fallthrough_insn;
2777 0 : auto_vec<rtx_insn *, 5> delay_list;
2778 0 : rtx_jump_insn *jump_insn;
2779 0 : bool own_target;
2780 0 : bool own_fallthrough;
2781 0 : int prediction, slots_to_fill, slots_filled;
2782 :
2783 0 : insn = unfilled_slots_base[i];
2784 0 : if (insn == 0
2785 0 : || insn->deleted ()
2786 0 : || ! (jump_insn = dyn_cast <rtx_jump_insn *> (insn))
2787 0 : || ! (condjump_p (jump_insn) || condjump_in_parallel_p (jump_insn)))
2788 0 : continue;
2789 :
2790 0 : slots_to_fill = num_delay_slots (jump_insn);
2791 : /* Some machine description have defined instructions to have
2792 : delay slots only in certain circumstances which may depend on
2793 : nearby insns (which change due to reorg's actions).
2794 :
2795 : For example, the PA port normally has delay slots for unconditional
2796 : jumps.
2797 :
2798 : However, the PA port claims such jumps do not have a delay slot
2799 : if they are immediate successors of certain CALL_INSNs. This
2800 : allows the port to favor filling the delay slot of the call with
2801 : the unconditional jump. */
2802 0 : if (slots_to_fill == 0)
2803 0 : continue;
2804 :
2805 0 : slots_filled = 0;
2806 0 : target_label = JUMP_LABEL (jump_insn);
2807 0 : condition = get_branch_condition (jump_insn, target_label);
2808 :
2809 0 : if (condition == 0)
2810 0 : continue;
2811 :
2812 : /* Get the next active fallthrough and target insns and see if we own
2813 : them. Then see whether the branch is likely true. We don't need
2814 : to do a lot of this for unconditional branches. */
2815 :
2816 0 : insn_at_target = first_active_target_insn (target_label);
2817 0 : own_target = own_thread_p (target_label, target_label, false);
2818 :
2819 0 : if (condition == const_true_rtx)
2820 : {
2821 : own_fallthrough = false;
2822 : fallthrough_insn = 0;
2823 : prediction = 2;
2824 : }
2825 : else
2826 : {
2827 0 : fallthrough_insn = next_active_insn (jump_insn);
2828 0 : own_fallthrough = own_thread_p (NEXT_INSN (jump_insn),
2829 : NULL_RTX, true);
2830 0 : prediction = mostly_true_jump (jump_insn);
2831 : }
2832 :
2833 : /* If this insn is expected to branch, first try to get insns from our
2834 : target, then our fallthrough insns. If it is not expected to branch,
2835 : try the other order. */
2836 :
2837 0 : if (prediction > 0)
2838 : {
2839 0 : fill_slots_from_thread (jump_insn, condition, insn_at_target,
2840 : fallthrough_insn, prediction == 2, true,
2841 : own_target, slots_to_fill,
2842 : &slots_filled, &delay_list);
2843 :
2844 0 : if (delay_list.is_empty () && own_fallthrough)
2845 : {
2846 : /* Even though we didn't find anything for delay slots,
2847 : we might have found a redundant insn which we deleted
2848 : from the thread that was filled. So we have to recompute
2849 : the next insn at the target. */
2850 0 : target_label = JUMP_LABEL (jump_insn);
2851 0 : insn_at_target = first_active_target_insn (target_label);
2852 :
2853 0 : fill_slots_from_thread (jump_insn, condition, fallthrough_insn,
2854 : insn_at_target, false, false,
2855 : own_fallthrough, slots_to_fill,
2856 : &slots_filled, &delay_list);
2857 : }
2858 : }
2859 : else
2860 : {
2861 0 : if (own_fallthrough)
2862 0 : fill_slots_from_thread (jump_insn, condition, fallthrough_insn,
2863 : insn_at_target, false, false,
2864 : own_fallthrough, slots_to_fill,
2865 : &slots_filled, &delay_list);
2866 :
2867 0 : if (delay_list.is_empty ())
2868 0 : fill_slots_from_thread (jump_insn, condition, insn_at_target,
2869 0 : next_active_insn (insn), false, true,
2870 : own_target, slots_to_fill,
2871 : &slots_filled, &delay_list);
2872 : }
2873 :
2874 0 : if (!delay_list.is_empty ())
2875 0 : unfilled_slots_base[i]
2876 0 : = emit_delay_sequence (jump_insn, delay_list, slots_filled);
2877 :
2878 0 : if (slots_to_fill == slots_filled)
2879 0 : unfilled_slots_base[i] = 0;
2880 :
2881 0 : note_delay_statistics (slots_filled, 1);
2882 0 : }
2883 0 : }
2884 :
2885 : static void delete_computation (rtx_insn *insn);
2886 :
2887 : /* Recursively delete prior insns that compute the value (used only by INSN
2888 : which the caller is deleting) stored in the register mentioned by NOTE
2889 : which is a REG_DEAD note associated with INSN. */
2890 :
2891 : static void
2892 0 : delete_prior_computation (rtx note, rtx_insn *insn)
2893 : {
2894 0 : rtx_insn *our_prev;
2895 0 : rtx reg = XEXP (note, 0);
2896 :
2897 0 : for (our_prev = prev_nonnote_nondebug_insn (insn);
2898 0 : our_prev && (NONJUMP_INSN_P (our_prev)
2899 0 : || CALL_P (our_prev));
2900 0 : our_prev = prev_nonnote_nondebug_insn (our_prev))
2901 : {
2902 0 : rtx pat = PATTERN (our_prev);
2903 :
2904 : /* If we reach a CALL which is not calling a const function
2905 : or the callee pops the arguments, then give up. */
2906 0 : auto call = dyn_cast<rtx_call_insn *> (our_prev);
2907 0 : if (call
2908 0 : && (! RTL_CONST_CALL_P (call)
2909 0 : || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
2910 : break;
2911 :
2912 : /* If we reach a SEQUENCE, it is too complex to try to
2913 : do anything with it, so give up. We can be run during
2914 : and after reorg, so SEQUENCE rtl can legitimately show
2915 : up here. */
2916 0 : if (GET_CODE (pat) == SEQUENCE)
2917 : break;
2918 :
2919 0 : if (GET_CODE (pat) == USE
2920 0 : && NONJUMP_INSN_P (XEXP (pat, 0)))
2921 : /* reorg creates USEs that look like this. We leave them
2922 : alone because reorg needs them for its own purposes. */
2923 : break;
2924 :
2925 0 : if (reg_set_p (reg, pat))
2926 : {
2927 0 : if (side_effects_p (pat) && !CALL_P (our_prev))
2928 : break;
2929 :
2930 0 : if (GET_CODE (pat) == PARALLEL)
2931 : {
2932 : /* If we find a SET of something else, we can't
2933 : delete the insn. */
2934 :
2935 : int i;
2936 :
2937 0 : for (i = 0; i < XVECLEN (pat, 0); i++)
2938 : {
2939 0 : rtx part = XVECEXP (pat, 0, i);
2940 :
2941 0 : if (GET_CODE (part) == SET
2942 0 : && SET_DEST (part) != reg)
2943 : break;
2944 : }
2945 :
2946 0 : if (i == XVECLEN (pat, 0))
2947 0 : delete_computation (our_prev);
2948 : }
2949 0 : else if (GET_CODE (pat) == SET
2950 0 : && REG_P (SET_DEST (pat)))
2951 : {
2952 0 : int dest_regno = REGNO (SET_DEST (pat));
2953 0 : int dest_endregno = END_REGNO (SET_DEST (pat));
2954 0 : int regno = REGNO (reg);
2955 0 : int endregno = END_REGNO (reg);
2956 :
2957 0 : if (dest_regno >= regno
2958 0 : && dest_endregno <= endregno)
2959 0 : delete_computation (our_prev);
2960 :
2961 : /* We may have a multi-word hard register and some, but not
2962 : all, of the words of the register are needed in subsequent
2963 : insns. Write REG_UNUSED notes for those parts that were not
2964 : needed. */
2965 0 : else if (dest_regno <= regno
2966 0 : && dest_endregno >= endregno)
2967 : {
2968 0 : int i;
2969 :
2970 0 : add_reg_note (our_prev, REG_UNUSED, reg);
2971 :
2972 0 : for (i = dest_regno; i < dest_endregno; i++)
2973 0 : if (! find_regno_note (our_prev, REG_UNUSED, i))
2974 : break;
2975 :
2976 0 : if (i == dest_endregno)
2977 0 : delete_computation (our_prev);
2978 : }
2979 : }
2980 :
2981 : break;
2982 : }
2983 :
2984 : /* If PAT references the register that dies here, it is an
2985 : additional use. Hence any prior SET isn't dead. However, this
2986 : insn becomes the new place for the REG_DEAD note. */
2987 0 : if (reg_overlap_mentioned_p (reg, pat))
2988 : {
2989 0 : XEXP (note, 1) = REG_NOTES (our_prev);
2990 0 : REG_NOTES (our_prev) = note;
2991 0 : break;
2992 : }
2993 : }
2994 0 : }
2995 :
2996 : /* Delete INSN and recursively delete insns that compute values used only
2997 : by INSN. This uses the REG_DEAD notes computed during flow analysis.
2998 :
2999 : Look at all our REG_DEAD notes. If a previous insn does nothing other
3000 : than set a register that dies in this insn, we can delete that insn
3001 : as well. */
3002 :
3003 : static void
3004 0 : delete_computation (rtx_insn *insn)
3005 : {
3006 0 : rtx note, next;
3007 :
3008 0 : for (note = REG_NOTES (insn); note; note = next)
3009 : {
3010 0 : next = XEXP (note, 1);
3011 :
3012 0 : if (REG_NOTE_KIND (note) != REG_DEAD
3013 : /* Verify that the REG_NOTE is legitimate. */
3014 0 : || !REG_P (XEXP (note, 0)))
3015 0 : continue;
3016 :
3017 0 : delete_prior_computation (note, insn);
3018 : }
3019 :
3020 0 : delete_related_insns (insn);
3021 0 : }
3022 :
3023 : /* If all INSN does is set the pc, delete it,
3024 : and delete the insn that set the condition codes for it
3025 : if that's what the previous thing was. */
3026 :
3027 : static void
3028 0 : delete_jump (rtx_insn *insn)
3029 : {
3030 0 : rtx set = single_set (insn);
3031 :
3032 0 : if (set && GET_CODE (SET_DEST (set)) == PC)
3033 0 : delete_computation (insn);
3034 0 : }
3035 :
3036 : static rtx_insn *
3037 0 : label_before_next_insn (rtx_insn *x, rtx_insn *scan_limit)
3038 : {
3039 0 : rtx_insn *insn = next_active_insn (x);
3040 0 : while (insn)
3041 : {
3042 0 : insn = PREV_INSN (insn);
3043 0 : if (insn == scan_limit || insn == NULL_RTX)
3044 : return NULL;
3045 0 : if (LABEL_P (insn))
3046 : break;
3047 : }
3048 : return insn;
3049 : }
3050 :
3051 : /* Return TRUE if there is a NOTE_INSN_SWITCH_TEXT_SECTIONS note in between
3052 : BEG and END. */
3053 :
3054 : static bool
3055 0 : switch_text_sections_between_p (const rtx_insn *beg, const rtx_insn *end)
3056 : {
3057 0 : const rtx_insn *p;
3058 0 : for (p = beg; p != end; p = NEXT_INSN (p))
3059 0 : if (NOTE_P (p) && NOTE_KIND (p) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
3060 : return true;
3061 : return false;
3062 : }
3063 :
3064 :
3065 : /* Once we have tried two ways to fill a delay slot, make a pass over the
3066 : code to try to improve the results and to do such things as more jump
3067 : threading. */
3068 :
3069 : static void
3070 0 : relax_delay_slots (rtx_insn *first)
3071 : {
3072 0 : rtx_insn *insn, *next;
3073 0 : rtx_sequence *pat;
3074 0 : rtx_insn *delay_insn;
3075 0 : rtx target_label;
3076 :
3077 : /* Look at every JUMP_INSN and see if we can improve it. */
3078 0 : for (insn = first; insn; insn = next)
3079 : {
3080 0 : rtx_insn *other, *prior_insn;
3081 0 : bool crossing;
3082 :
3083 0 : next = next_active_insn (insn);
3084 :
3085 : /* If this is a jump insn, see if it now jumps to a jump, jumps to
3086 : the next insn, or jumps to a label that is not the last of a
3087 : group of consecutive labels. */
3088 0 : if (is_a <rtx_jump_insn *> (insn)
3089 0 : && (condjump_p (insn) || condjump_in_parallel_p (insn))
3090 0 : && !ANY_RETURN_P (target_label = JUMP_LABEL (insn)))
3091 : {
3092 0 : rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (insn);
3093 0 : target_label
3094 0 : = skip_consecutive_labels (follow_jumps (target_label, jump_insn,
3095 : &crossing));
3096 0 : if (ANY_RETURN_P (target_label))
3097 0 : target_label = find_end_label (target_label);
3098 :
3099 0 : if (target_label
3100 0 : && next_active_insn (as_a<rtx_insn *> (target_label)) == next
3101 0 : && ! condjump_in_parallel_p (jump_insn)
3102 0 : && ! (next && switch_text_sections_between_p (jump_insn, next)))
3103 : {
3104 0 : rtx_insn *direct_label = as_a<rtx_insn *> (JUMP_LABEL (insn));
3105 0 : rtx_insn *prev = prev_nonnote_nondebug_insn (direct_label);
3106 :
3107 : /* If the insn jumps over a BARRIER and is the only way to reach
3108 : its target, then we need to delete the BARRIER before the jump
3109 : because, otherwise, the target may end up being considered as
3110 : unreachable and thus also deleted. */
3111 0 : if (BARRIER_P (prev) && LABEL_NUSES (direct_label) == 1)
3112 : {
3113 0 : delete_related_insns (prev);
3114 :
3115 : /* We have just removed a BARRIER, which means that the block
3116 : number of the next insns has effectively been changed (see
3117 : find_basic_block in resource.cc), so clear it. */
3118 0 : clear_hashed_info_until_next_barrier (direct_label);
3119 : }
3120 :
3121 0 : delete_jump (jump_insn);
3122 0 : continue;
3123 0 : }
3124 :
3125 0 : if (target_label && target_label != JUMP_LABEL (jump_insn))
3126 : {
3127 0 : reorg_redirect_jump (jump_insn, target_label);
3128 0 : if (crossing)
3129 0 : set_crossing_jump (jump_insn, true);
3130 : }
3131 :
3132 : /* See if this jump conditionally branches around an unconditional
3133 : jump. If so, invert this jump and point it to the target of the
3134 : second jump. Check if it's possible on the target. */
3135 0 : if (next && simplejump_or_return_p (next)
3136 0 : && any_condjump_p (jump_insn)
3137 0 : && target_label
3138 0 : && (next_active_insn (as_a<rtx_insn *> (target_label))
3139 0 : == next_active_insn (next))
3140 0 : && no_labels_between_p (jump_insn, next)
3141 0 : && targetm.can_follow_jump (jump_insn, next))
3142 : {
3143 0 : rtx label = JUMP_LABEL (next);
3144 :
3145 : /* Be careful how we do this to avoid deleting code or
3146 : labels that are momentarily dead. See similar optimization
3147 : in jump.cc.
3148 :
3149 : We also need to ensure we properly handle the case when
3150 : invert_jump fails. */
3151 :
3152 0 : ++LABEL_NUSES (target_label);
3153 0 : if (!ANY_RETURN_P (label))
3154 0 : ++LABEL_NUSES (label);
3155 :
3156 0 : if (invert_jump (jump_insn, label, 1))
3157 : {
3158 0 : rtx_insn *from = delete_related_insns (next);
3159 :
3160 : /* We have just removed a BARRIER, which means that the block
3161 : number of the next insns has effectively been changed (see
3162 : find_basic_block in resource.cc), so clear it. */
3163 0 : if (from)
3164 0 : clear_hashed_info_until_next_barrier (from);
3165 :
3166 : next = jump_insn;
3167 : }
3168 :
3169 0 : if (!ANY_RETURN_P (label))
3170 0 : --LABEL_NUSES (label);
3171 :
3172 0 : if (--LABEL_NUSES (target_label) == 0)
3173 0 : delete_related_insns (target_label);
3174 :
3175 0 : continue;
3176 0 : }
3177 : }
3178 :
3179 : /* If this is an unconditional jump and the previous insn is a
3180 : conditional jump, try reversing the condition of the previous
3181 : insn and swapping our targets. The next pass might be able to
3182 : fill the slots.
3183 :
3184 : Don't do this if we expect the conditional branch to be true, because
3185 : we would then be making the more common case longer. */
3186 :
3187 0 : if (simplejump_or_return_p (insn)
3188 0 : && (other = prev_active_insn (insn)) != 0
3189 0 : && any_condjump_p (other)
3190 0 : && no_labels_between_p (other, insn)
3191 0 : && mostly_true_jump (other) < 0)
3192 : {
3193 0 : rtx other_target = JUMP_LABEL (other);
3194 0 : target_label = JUMP_LABEL (insn);
3195 :
3196 0 : if (invert_jump (as_a <rtx_jump_insn *> (other), target_label, 0))
3197 0 : reorg_redirect_jump (as_a <rtx_jump_insn *> (insn), other_target);
3198 : }
3199 :
3200 : /* Now look only at cases where we have a filled delay slot. */
3201 0 : if (!NONJUMP_INSN_P (insn)
3202 0 : || !(pat = dyn_cast <rtx_sequence *> (PATTERN (insn))))
3203 0 : continue;
3204 0 : delay_insn = pat->insn (0);
3205 :
3206 : /* See if the first insn in the delay slot is redundant with some
3207 : previous insn. Remove it from the delay slot if so; then set up
3208 : to reprocess this insn. */
3209 0 : if ((prior_insn = redundant_insn (pat->insn (1), delay_insn, vNULL)))
3210 : {
3211 0 : fix_reg_dead_note (prior_insn, insn);
3212 0 : update_block (pat->insn (1), insn);
3213 0 : delete_from_delay_slot (pat->insn (1));
3214 0 : next = prev_active_insn (next);
3215 0 : continue;
3216 : }
3217 :
3218 : /* See if we have a RETURN insn with a filled delay slot followed
3219 : by a RETURN insn with an unfilled a delay slot. If so, we can delete
3220 : the first RETURN (but not its delay insn). This gives the same
3221 : effect in fewer instructions.
3222 :
3223 : Only do so if optimizing for size since this results in slower, but
3224 : smaller code. */
3225 0 : if (optimize_function_for_size_p (cfun)
3226 0 : && ANY_RETURN_P (PATTERN (delay_insn))
3227 0 : && next
3228 0 : && JUMP_P (next)
3229 0 : && PATTERN (next) == PATTERN (delay_insn))
3230 : {
3231 : rtx_insn *after;
3232 : int i;
3233 :
3234 : /* Delete the RETURN and just execute the delay list insns.
3235 :
3236 : We do this by deleting the INSN containing the SEQUENCE, then
3237 : re-emitting the insns separately, and then deleting the RETURN.
3238 : This allows the count of the jump target to be properly
3239 : decremented.
3240 :
3241 : Note that we need to change the INSN_UID of the re-emitted insns
3242 : since it is used to hash the insns for mark_target_live_regs and
3243 : the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3244 :
3245 : Clear the from target bit, since these insns are no longer
3246 : in delay slots. */
3247 0 : for (i = 0; i < pat->len (); i++)
3248 0 : set_insn_from_target (pat->insn (i), false);
3249 :
3250 0 : rtx_insn *prev = PREV_INSN (insn);
3251 0 : delete_related_insns (insn);
3252 0 : gcc_assert (GET_CODE (pat) == SEQUENCE);
3253 0 : add_insn_after (delay_insn, prev, NULL);
3254 0 : after = delay_insn;
3255 0 : for (i = 1; i < pat->len (); i++)
3256 0 : after = emit_copy_of_insn_after (pat->insn (i), after);
3257 0 : delete_scheduled_jump (delay_insn);
3258 0 : continue;
3259 0 : }
3260 :
3261 : /* Now look only at the cases where we have a filled JUMP_INSN. */
3262 0 : rtx_jump_insn *delay_jump_insn =
3263 0 : dyn_cast <rtx_jump_insn *> (delay_insn);
3264 0 : if (! delay_jump_insn || !(condjump_p (delay_jump_insn)
3265 0 : || condjump_in_parallel_p (delay_jump_insn)))
3266 0 : continue;
3267 :
3268 0 : target_label = JUMP_LABEL (delay_jump_insn);
3269 0 : if (target_label && ANY_RETURN_P (target_label))
3270 0 : continue;
3271 :
3272 : /* If this jump goes to another unconditional jump, thread it, but
3273 : don't convert a jump into a RETURN here. */
3274 0 : rtx trial = skip_consecutive_labels (follow_jumps (target_label,
3275 : delay_jump_insn,
3276 : &crossing));
3277 0 : if (ANY_RETURN_P (trial))
3278 0 : trial = find_end_label (trial);
3279 :
3280 0 : if (trial && trial != target_label
3281 0 : && redirect_with_delay_slots_safe_p (delay_jump_insn, trial, insn))
3282 : {
3283 0 : reorg_redirect_jump (delay_jump_insn, trial);
3284 0 : target_label = trial;
3285 0 : if (crossing)
3286 0 : set_crossing_jump (delay_jump_insn, true);
3287 : }
3288 :
3289 : /* If the first insn at TARGET_LABEL is redundant with a previous
3290 : insn, redirect the jump to the following insn and process again.
3291 : We use next_real_nondebug_insn instead of next_active_insn so we
3292 : don't skip USE-markers, or we'll end up with incorrect
3293 : liveness info. */
3294 0 : rtx_insn *next_insn
3295 0 : = next_real_nondebug_insn (as_a<rtx_insn *> (target_label));
3296 0 : if (next_insn && GET_CODE (PATTERN (next_insn)) != SEQUENCE
3297 0 : && redundant_insn (next_insn, insn, vNULL)
3298 0 : && ! can_throw_internal (next_insn))
3299 : {
3300 : /* Figure out where to emit the special USE insn so we don't
3301 : later incorrectly compute register live/death info. */
3302 0 : rtx_insn *tmp = next_active_insn (next_insn);
3303 0 : if (tmp == 0)
3304 0 : tmp = find_end_label (simple_return_rtx);
3305 :
3306 0 : if (tmp)
3307 : {
3308 : /* Insert the special USE insn and update dataflow info.
3309 : We know "next_insn" is an insn here as it is the output of
3310 : next_real_nondebug_insn () above. */
3311 0 : update_block (next_insn, tmp);
3312 :
3313 : /* Now emit a label before the special USE insn, and
3314 : redirect our jump to the new label. */
3315 0 : target_label = get_label_before (PREV_INSN (tmp), target_label);
3316 0 : reorg_redirect_jump (delay_jump_insn, target_label);
3317 0 : next = insn;
3318 0 : continue;
3319 : }
3320 : }
3321 :
3322 : /* Similarly, if it is an unconditional jump with one insn in its
3323 : delay list and that insn is redundant, thread the jump. */
3324 0 : rtx_sequence *trial_seq =
3325 0 : next_insn ? dyn_cast <rtx_sequence *> (PATTERN (next_insn)) : NULL;
3326 0 : if (trial_seq
3327 0 : && trial_seq->len () == 2
3328 0 : && JUMP_P (trial_seq->insn (0))
3329 0 : && simplejump_or_return_p (trial_seq->insn (0))
3330 0 : && redundant_insn (trial_seq->insn (1), insn, vNULL))
3331 : {
3332 0 : rtx temp_label = JUMP_LABEL (trial_seq->insn (0));
3333 0 : if (ANY_RETURN_P (temp_label))
3334 0 : temp_label = find_end_label (temp_label);
3335 :
3336 0 : if (temp_label
3337 0 : && redirect_with_delay_slots_safe_p (delay_jump_insn,
3338 : temp_label, insn))
3339 : {
3340 0 : update_block (trial_seq->insn (1), insn);
3341 0 : reorg_redirect_jump (delay_jump_insn, temp_label);
3342 0 : next = insn;
3343 0 : continue;
3344 : }
3345 : }
3346 :
3347 : /* See if we have a simple (conditional) jump that is useless. */
3348 0 : if (!CROSSING_JUMP_P (delay_jump_insn)
3349 0 : && !INSN_ANNULLED_BRANCH_P (delay_jump_insn)
3350 0 : && !condjump_in_parallel_p (delay_jump_insn)
3351 0 : && prev_active_insn (as_a<rtx_insn *> (target_label)) == insn
3352 0 : && !BARRIER_P (prev_nonnote_nondebug_insn
3353 : (as_a<rtx_insn *> (target_label))))
3354 : {
3355 : rtx_insn *after;
3356 : int i;
3357 :
3358 : /* All this insn does is execute its delay list and jump to the
3359 : following insn. So delete the jump and just execute the delay
3360 : list insns.
3361 :
3362 : We do this by deleting the INSN containing the SEQUENCE, then
3363 : re-emitting the insns separately, and then deleting the jump.
3364 : This allows the count of the jump target to be properly
3365 : decremented.
3366 :
3367 : Note that we need to change the INSN_UID of the re-emitted insns
3368 : since it is used to hash the insns for mark_target_live_regs and
3369 : the re-emitted insns will no longer be wrapped up in a SEQUENCE.
3370 :
3371 : Clear the from target bit, since these insns are no longer
3372 : in delay slots. */
3373 0 : for (i = 0; i < pat->len (); i++)
3374 0 : set_insn_from_target (pat->insn (i), false);
3375 :
3376 0 : rtx_insn *prev = PREV_INSN (insn);
3377 0 : delete_related_insns (insn);
3378 0 : gcc_assert (GET_CODE (pat) == SEQUENCE);
3379 0 : add_insn_after (delay_jump_insn, prev, NULL);
3380 0 : after = delay_jump_insn;
3381 0 : for (i = 1; i < pat->len (); i++)
3382 0 : after = emit_copy_of_insn_after (pat->insn (i), after);
3383 0 : delete_scheduled_jump (delay_jump_insn);
3384 0 : continue;
3385 0 : }
3386 :
3387 : /* See if this is an unconditional jump around a single insn which is
3388 : identical to the one in its delay slot. In this case, we can just
3389 : delete the branch and the insn in its delay slot. */
3390 0 : if (next && NONJUMP_INSN_P (next)
3391 0 : && label_before_next_insn (next, insn) == target_label
3392 0 : && simplejump_p (insn)
3393 0 : && pat->len () == 2
3394 0 : && rtx_equal_p (PATTERN (next), PATTERN (pat->insn (1))))
3395 : {
3396 0 : delete_related_insns (insn);
3397 0 : continue;
3398 : }
3399 :
3400 : /* See if this jump (with its delay slots) conditionally branches
3401 : around an unconditional jump (without delay slots). If so, invert
3402 : this jump and point it to the target of the second jump. We cannot
3403 : do this for annulled jumps, though. Again, don't convert a jump to
3404 : a RETURN here. */
3405 0 : if (! INSN_ANNULLED_BRANCH_P (delay_jump_insn)
3406 0 : && any_condjump_p (delay_jump_insn)
3407 0 : && next && simplejump_or_return_p (next)
3408 0 : && (next_active_insn (as_a<rtx_insn *> (target_label))
3409 0 : == next_active_insn (next))
3410 0 : && no_labels_between_p (insn, next)
3411 0 : && !switch_text_sections_between_p (insn, next_active_insn (next)))
3412 : {
3413 0 : rtx label = JUMP_LABEL (next);
3414 0 : rtx old_label = JUMP_LABEL (delay_jump_insn);
3415 :
3416 0 : if (ANY_RETURN_P (label))
3417 0 : label = find_end_label (label);
3418 :
3419 : /* find_end_label can generate a new label. Check this first. */
3420 0 : if (label
3421 0 : && no_labels_between_p (insn, next)
3422 0 : && redirect_with_delay_slots_safe_p (delay_jump_insn,
3423 : label, insn))
3424 : {
3425 : /* Be careful how we do this to avoid deleting code or labels
3426 : that are momentarily dead. See similar optimization in
3427 : jump.cc */
3428 0 : if (old_label)
3429 0 : ++LABEL_NUSES (old_label);
3430 :
3431 0 : if (invert_jump (delay_jump_insn, label, 1))
3432 : {
3433 : /* Must update the INSN_FROM_TARGET_P bits now that
3434 : the branch is reversed, so that mark_target_live_regs
3435 : will handle the delay slot insn correctly. */
3436 0 : for (int i = 1; i < pat->len (); i++)
3437 : {
3438 0 : rtx_insn *slot = pat->insn (i);
3439 0 : set_insn_from_target (slot, ! INSN_FROM_TARGET_P (slot));
3440 : }
3441 :
3442 : /* We have just removed a BARRIER, which means that the block
3443 : number of the next insns has effectively been changed (see
3444 : find_basic_block in resource.cc), so clear it. */
3445 0 : rtx_insn *from = delete_related_insns (next);
3446 0 : if (from)
3447 0 : clear_hashed_info_until_next_barrier (from);
3448 :
3449 : next = insn;
3450 : }
3451 :
3452 0 : if (old_label && --LABEL_NUSES (old_label) == 0)
3453 0 : delete_related_insns (old_label);
3454 0 : continue;
3455 0 : }
3456 : }
3457 :
3458 : /* If we own the thread opposite the way this insn branches, see if we
3459 : can merge its delay slots with following insns. */
3460 0 : if (INSN_FROM_TARGET_P (pat->insn (1))
3461 0 : && own_thread_p (NEXT_INSN (insn), 0, true))
3462 0 : try_merge_delay_insns (insn, next);
3463 0 : else if (! INSN_FROM_TARGET_P (pat->insn (1))
3464 0 : && own_thread_p (target_label, target_label, false))
3465 0 : try_merge_delay_insns (insn,
3466 : next_active_insn (as_a<rtx_insn *> (target_label)));
3467 :
3468 : /* If we get here, we haven't deleted INSN. But we may have deleted
3469 : NEXT, so recompute it. */
3470 0 : next = next_active_insn (insn);
3471 : }
3472 0 : }
3473 :
3474 :
3475 : /* Look for filled jumps to the end of function label. We can try to convert
3476 : them into RETURN insns if the insns in the delay slot are valid for the
3477 : RETURN as well. */
3478 :
3479 : static void
3480 0 : make_return_insns (rtx_insn *first)
3481 : {
3482 0 : rtx_insn *insn;
3483 0 : rtx_jump_insn *jump_insn;
3484 0 : rtx_insn *real_return_label = function_return_label;
3485 0 : rtx_insn *real_simple_return_label = function_simple_return_label;
3486 0 : int slots, i;
3487 :
3488 : /* See if there is a RETURN insn in the function other than the one we
3489 : made for END_OF_FUNCTION_LABEL. If so, set up anything we can't change
3490 : into a RETURN to jump to it. */
3491 0 : for (insn = first; insn; insn = NEXT_INSN (insn))
3492 0 : if (JUMP_P (insn) && ANY_RETURN_P (PATTERN (insn)))
3493 : {
3494 0 : rtx_insn *t = get_label_before (insn, NULL_RTX);
3495 0 : if (PATTERN (insn) == ret_rtx)
3496 : real_return_label = t;
3497 : else
3498 : real_simple_return_label = t;
3499 : break;
3500 : }
3501 :
3502 : /* Show an extra usage of REAL_RETURN_LABEL so it won't go away if it
3503 : was equal to END_OF_FUNCTION_LABEL. */
3504 0 : if (real_return_label)
3505 0 : LABEL_NUSES (real_return_label)++;
3506 0 : if (real_simple_return_label)
3507 0 : LABEL_NUSES (real_simple_return_label)++;
3508 :
3509 : /* Clear the list of insns to fill so we can use it. */
3510 0 : obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3511 :
3512 0 : for (insn = first; insn; insn = NEXT_INSN (insn))
3513 : {
3514 0 : int flags;
3515 0 : rtx kind;
3516 0 : rtx_insn *real_label;
3517 :
3518 : /* Only look at filled JUMP_INSNs that go to the end of function
3519 : label. */
3520 0 : if (!NONJUMP_INSN_P (insn))
3521 0 : continue;
3522 :
3523 0 : rtx_sequence *pat = dyn_cast <rtx_sequence *> (PATTERN (insn));
3524 0 : if (!pat)
3525 0 : continue;
3526 :
3527 0 : if (!jump_to_label_p (pat->insn (0)))
3528 0 : continue;
3529 :
3530 0 : if (JUMP_LABEL (pat->insn (0)) == function_return_label)
3531 : {
3532 0 : kind = ret_rtx;
3533 0 : real_label = real_return_label;
3534 : }
3535 0 : else if (JUMP_LABEL (pat->insn (0)) == function_simple_return_label)
3536 : {
3537 0 : kind = simple_return_rtx;
3538 0 : real_label = real_simple_return_label;
3539 : }
3540 : else
3541 0 : continue;
3542 :
3543 0 : jump_insn = as_a <rtx_jump_insn *> (pat->insn (0));
3544 :
3545 : /* If we can't make the jump into a RETURN, try to redirect it to the best
3546 : RETURN and go on to the next insn. */
3547 0 : if (!reorg_redirect_jump (jump_insn, kind))
3548 : {
3549 : /* Make sure redirecting the jump will not invalidate the delay
3550 : slot insns. */
3551 0 : if (redirect_with_delay_slots_safe_p (jump_insn, real_label, insn))
3552 0 : reorg_redirect_jump (jump_insn, real_label);
3553 0 : continue;
3554 : }
3555 :
3556 : /* See if this RETURN can accept the insns current in its delay slot.
3557 : It can if it has more or an equal number of slots and the contents
3558 : of each is valid. */
3559 :
3560 0 : flags = get_jump_flags (jump_insn, JUMP_LABEL (jump_insn));
3561 0 : slots = num_delay_slots (jump_insn);
3562 0 : if (slots >= pat->len () - 1)
3563 : {
3564 0 : for (i = 1; i < XVECLEN (pat, 0); i++)
3565 0 : if (! (
3566 : #if ANNUL_IFFALSE_SLOTS
3567 : (INSN_ANNULLED_BRANCH_P (jump_insn)
3568 : && INSN_FROM_TARGET_P (pat->insn (i)))
3569 : ? eligible_for_annul_false (jump_insn, i - 1,
3570 : pat->insn (i), flags) :
3571 : #endif
3572 : #if ANNUL_IFTRUE_SLOTS
3573 : (INSN_ANNULLED_BRANCH_P (jump_insn)
3574 : && ! INSN_FROM_TARGET_P (pat->insn (i)))
3575 : ? eligible_for_annul_true (jump_insn, i - 1,
3576 : pat->insn (i), flags) :
3577 : #endif
3578 0 : eligible_for_delay (jump_insn, i - 1,
3579 : pat->insn (i), flags)))
3580 : break;
3581 : }
3582 : else
3583 : i = 0;
3584 :
3585 0 : if (i == XVECLEN (pat, 0))
3586 0 : continue;
3587 :
3588 : /* We have to do something with this insn. If it is an unconditional
3589 : RETURN, delete the SEQUENCE and output the individual insns,
3590 : followed by the RETURN. Then set things up so we try to find
3591 : insns for its delay slots, if it needs some. */
3592 0 : if (ANY_RETURN_P (PATTERN (jump_insn)))
3593 : {
3594 0 : rtx_insn *after = PREV_INSN (insn);
3595 :
3596 0 : delete_related_insns (insn);
3597 0 : insn = jump_insn;
3598 0 : for (i = 1; i < pat->len (); i++)
3599 0 : after = emit_copy_of_insn_after (pat->insn (i), after);
3600 0 : add_insn_after (insn, after, NULL);
3601 0 : emit_barrier_after (insn);
3602 :
3603 0 : if (slots)
3604 0 : obstack_ptr_grow (&unfilled_slots_obstack, insn);
3605 : }
3606 : else
3607 : /* It is probably more efficient to keep this with its current
3608 : delay slot as a branch to a RETURN. */
3609 0 : reorg_redirect_jump (jump_insn, real_label);
3610 : }
3611 :
3612 : /* Now delete REAL_RETURN_LABEL if we never used it. Then try to fill any
3613 : new delay slots we have created. */
3614 0 : if (real_return_label != NULL_RTX && --LABEL_NUSES (real_return_label) == 0)
3615 0 : delete_related_insns (real_return_label);
3616 0 : if (real_simple_return_label != NULL_RTX
3617 0 : && --LABEL_NUSES (real_simple_return_label) == 0)
3618 0 : delete_related_insns (real_simple_return_label);
3619 :
3620 0 : fill_simple_delay_slots (true);
3621 0 : fill_simple_delay_slots (false);
3622 0 : }
3623 :
3624 : /* Try to find insns to place in delay slots. */
3625 :
3626 : static void
3627 0 : dbr_schedule (rtx_insn *first)
3628 : {
3629 0 : rtx_insn *insn, *next, *epilogue_insn = 0;
3630 0 : bool need_return_insns;
3631 0 : int i;
3632 :
3633 : /* If the current function has no insns other than the prologue and
3634 : epilogue, then do not try to fill any delay slots. */
3635 0 : if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
3636 : return;
3637 :
3638 : /* Find the highest INSN_UID and allocate and initialize our map from
3639 : INSN_UID's to position in code. */
3640 0 : for (max_uid = 0, insn = first; insn; insn = NEXT_INSN (insn))
3641 : {
3642 0 : if (INSN_UID (insn) > max_uid)
3643 0 : max_uid = INSN_UID (insn);
3644 0 : if (NOTE_P (insn)
3645 0 : && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
3646 0 : epilogue_insn = insn;
3647 : }
3648 :
3649 0 : uid_to_ruid = XNEWVEC (int, max_uid + 1);
3650 0 : for (i = 0, insn = first; insn; i++, insn = NEXT_INSN (insn))
3651 0 : uid_to_ruid[INSN_UID (insn)] = i;
3652 :
3653 : /* Initialize the list of insns that need filling. */
3654 0 : if (unfilled_firstobj == 0)
3655 : {
3656 0 : gcc_obstack_init (&unfilled_slots_obstack);
3657 0 : unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3658 : }
3659 :
3660 0 : for (insn = next_active_insn (first); insn; insn = next_active_insn (insn))
3661 : {
3662 0 : rtx target;
3663 :
3664 : /* Skip vector tables. We can't get attributes for them. */
3665 0 : if (JUMP_TABLE_DATA_P (insn))
3666 0 : continue;
3667 :
3668 0 : if (JUMP_P (insn))
3669 0 : INSN_ANNULLED_BRANCH_P (insn) = 0;
3670 0 : set_insn_from_target (insn, false);
3671 :
3672 0 : if (num_delay_slots (insn) > 0)
3673 0 : obstack_ptr_grow (&unfilled_slots_obstack, insn);
3674 :
3675 : /* Ensure all jumps go to the last of a set of consecutive labels. */
3676 0 : if (JUMP_P (insn)
3677 0 : && (condjump_p (insn) || condjump_in_parallel_p (insn))
3678 0 : && !ANY_RETURN_P (JUMP_LABEL (insn))
3679 0 : && ((target = skip_consecutive_labels (JUMP_LABEL (insn)))
3680 : != JUMP_LABEL (insn)))
3681 0 : redirect_jump (as_a <rtx_jump_insn *> (insn), target, 1);
3682 : }
3683 :
3684 0 : init_resource_info (epilogue_insn);
3685 :
3686 : /* Show we haven't computed an end-of-function label yet. */
3687 0 : function_return_label = function_simple_return_label = NULL;
3688 :
3689 : /* Initialize the statistics for this function. */
3690 0 : memset (num_insns_needing_delays, 0, sizeof num_insns_needing_delays);
3691 0 : memset (num_filled_delays, 0, sizeof num_filled_delays);
3692 :
3693 : /* Now do the delay slot filling. Try everything twice in case earlier
3694 : changes make more slots fillable. */
3695 :
3696 0 : for (reorg_pass_number = 0;
3697 0 : reorg_pass_number < MAX_REORG_PASSES;
3698 : reorg_pass_number++)
3699 : {
3700 0 : fill_simple_delay_slots (true);
3701 0 : fill_simple_delay_slots (false);
3702 0 : if (!targetm.no_speculation_in_delay_slots_p ())
3703 0 : fill_eager_delay_slots ();
3704 0 : relax_delay_slots (first);
3705 : }
3706 :
3707 : /* If we made an end of function label, indicate that it is now
3708 : safe to delete it by undoing our prior adjustment to LABEL_NUSES.
3709 : If it is now unused, delete it. */
3710 0 : if (function_return_label && --LABEL_NUSES (function_return_label) == 0)
3711 0 : delete_related_insns (function_return_label);
3712 0 : if (function_simple_return_label
3713 0 : && --LABEL_NUSES (function_simple_return_label) == 0)
3714 0 : delete_related_insns (function_simple_return_label);
3715 :
3716 0 : need_return_insns = false;
3717 0 : need_return_insns |= targetm.have_return () && function_return_label != 0;
3718 0 : need_return_insns |= (targetm.have_simple_return ()
3719 0 : && function_simple_return_label != 0);
3720 0 : if (need_return_insns)
3721 0 : make_return_insns (first);
3722 :
3723 : /* Delete any USE insns made by update_block; subsequent passes don't need
3724 : them or know how to deal with them. */
3725 0 : for (insn = first; insn; insn = next)
3726 : {
3727 0 : next = NEXT_INSN (insn);
3728 :
3729 0 : if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
3730 0 : && INSN_P (XEXP (PATTERN (insn), 0)))
3731 0 : next = delete_related_insns (insn);
3732 : }
3733 :
3734 0 : obstack_free (&unfilled_slots_obstack, unfilled_firstobj);
3735 :
3736 : /* It is not clear why the line below is needed, but it does seem to be. */
3737 0 : unfilled_firstobj = XOBNEWVAR (&unfilled_slots_obstack, rtx, 0);
3738 :
3739 0 : if (dump_file)
3740 : {
3741 0 : int i, j, need_comma;
3742 0 : int total_delay_slots[MAX_DELAY_HISTOGRAM + 1];
3743 0 : int total_annul_slots[MAX_DELAY_HISTOGRAM + 1];
3744 :
3745 0 : for (reorg_pass_number = 0;
3746 0 : reorg_pass_number < MAX_REORG_PASSES;
3747 : reorg_pass_number++)
3748 : {
3749 0 : fprintf (dump_file, ";; Reorg pass #%d:\n", reorg_pass_number + 1);
3750 0 : for (i = 0; i < NUM_REORG_FUNCTIONS; i++)
3751 : {
3752 0 : need_comma = 0;
3753 0 : fprintf (dump_file, ";; Reorg function #%d\n", i);
3754 :
3755 0 : fprintf (dump_file, ";; %d insns needing delay slots\n;; ",
3756 : num_insns_needing_delays[i][reorg_pass_number]);
3757 :
3758 0 : for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3759 0 : if (num_filled_delays[i][j][reorg_pass_number])
3760 : {
3761 0 : if (need_comma)
3762 0 : fprintf (dump_file, ", ");
3763 0 : need_comma = 1;
3764 0 : fprintf (dump_file, "%d got %d delays",
3765 : num_filled_delays[i][j][reorg_pass_number], j);
3766 : }
3767 0 : fprintf (dump_file, "\n");
3768 : }
3769 : }
3770 0 : memset (total_delay_slots, 0, sizeof total_delay_slots);
3771 0 : memset (total_annul_slots, 0, sizeof total_annul_slots);
3772 0 : for (insn = first; insn; insn = NEXT_INSN (insn))
3773 : {
3774 0 : if (! insn->deleted ()
3775 0 : && NONJUMP_INSN_P (insn)
3776 0 : && GET_CODE (PATTERN (insn)) != USE
3777 0 : && GET_CODE (PATTERN (insn)) != CLOBBER)
3778 : {
3779 0 : if (auto *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3780 : {
3781 0 : rtx_insn *control = seq->insn (0);
3782 0 : j = seq->len () - 1;
3783 0 : if (j > MAX_DELAY_HISTOGRAM)
3784 : j = MAX_DELAY_HISTOGRAM;
3785 0 : if (JUMP_P (control) && INSN_ANNULLED_BRANCH_P (control))
3786 0 : total_annul_slots[j]++;
3787 : else
3788 0 : total_delay_slots[j]++;
3789 : }
3790 0 : else if (num_delay_slots (insn) > 0)
3791 0 : total_delay_slots[0]++;
3792 : }
3793 : }
3794 0 : fprintf (dump_file, ";; Reorg totals: ");
3795 0 : need_comma = 0;
3796 0 : for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3797 : {
3798 0 : if (total_delay_slots[j])
3799 : {
3800 0 : if (need_comma)
3801 0 : fprintf (dump_file, ", ");
3802 0 : need_comma = 1;
3803 0 : fprintf (dump_file, "%d got %d delays", total_delay_slots[j], j);
3804 : }
3805 : }
3806 0 : fprintf (dump_file, "\n");
3807 :
3808 0 : if (ANNUL_IFTRUE_SLOTS || ANNUL_IFFALSE_SLOTS)
3809 : {
3810 : fprintf (dump_file, ";; Reorg annuls: ");
3811 : need_comma = 0;
3812 : for (j = 0; j < MAX_DELAY_HISTOGRAM + 1; j++)
3813 : {
3814 : if (total_annul_slots[j])
3815 : {
3816 : if (need_comma)
3817 : fprintf (dump_file, ", ");
3818 : need_comma = 1;
3819 : fprintf (dump_file, "%d got %d delays", total_annul_slots[j], j);
3820 : }
3821 : }
3822 : fprintf (dump_file, "\n");
3823 : }
3824 :
3825 0 : fprintf (dump_file, "\n");
3826 : }
3827 :
3828 0 : if (!sibling_labels.is_empty ())
3829 : {
3830 0 : update_alignments (sibling_labels);
3831 0 : sibling_labels.release ();
3832 : }
3833 :
3834 0 : free_resource_info ();
3835 0 : free (uid_to_ruid);
3836 0 : crtl->dbr_scheduled_p = true;
3837 : }
3838 :
3839 : /* Run delay slot optimization. */
3840 : static void
3841 0 : rest_of_handle_delay_slots (void)
3842 : {
3843 0 : if (DELAY_SLOTS)
3844 : dbr_schedule (get_insns ());
3845 0 : }
3846 :
3847 : namespace {
3848 :
3849 : const pass_data pass_data_delay_slots =
3850 : {
3851 : RTL_PASS, /* type */
3852 : "dbr", /* name */
3853 : OPTGROUP_NONE, /* optinfo_flags */
3854 : TV_DBR_SCHED, /* tv_id */
3855 : 0, /* properties_required */
3856 : 0, /* properties_provided */
3857 : 0, /* properties_destroyed */
3858 : 0, /* todo_flags_start */
3859 : 0, /* todo_flags_finish */
3860 : };
3861 :
3862 : class pass_delay_slots : public rtl_opt_pass
3863 : {
3864 : public:
3865 297180 : pass_delay_slots (gcc::context *ctxt)
3866 594360 : : rtl_opt_pass (pass_data_delay_slots, ctxt)
3867 : {}
3868 :
3869 : /* opt_pass methods: */
3870 : bool gate (function *) final override;
3871 0 : unsigned int execute (function *) final override
3872 : {
3873 0 : rest_of_handle_delay_slots ();
3874 0 : return 0;
3875 : }
3876 :
3877 : }; // class pass_delay_slots
3878 :
3879 : bool
3880 1517135 : pass_delay_slots::gate (function *)
3881 : {
3882 : /* At -O0 dataflow info isn't updated after RA. */
3883 1517135 : if (DELAY_SLOTS)
3884 : return optimize > 0 && flag_delayed_branch && !crtl->dbr_scheduled_p;
3885 :
3886 1517135 : return false;
3887 : }
3888 :
3889 : } // anon namespace
3890 :
3891 : rtl_opt_pass *
3892 297180 : make_pass_delay_slots (gcc::context *ctxt)
3893 : {
3894 297180 : return new pass_delay_slots (ctxt);
3895 : }
3896 :
3897 : /* Machine dependent reorg pass. */
3898 :
3899 : namespace {
3900 :
3901 : const pass_data pass_data_machine_reorg =
3902 : {
3903 : RTL_PASS, /* type */
3904 : "mach", /* name */
3905 : OPTGROUP_NONE, /* optinfo_flags */
3906 : TV_MACH_DEP, /* tv_id */
3907 : 0, /* properties_required */
3908 : 0, /* properties_provided */
3909 : 0, /* properties_destroyed */
3910 : 0, /* todo_flags_start */
3911 : 0, /* todo_flags_finish */
3912 : };
3913 :
3914 : class pass_machine_reorg : public rtl_opt_pass
3915 : {
3916 : public:
3917 297180 : pass_machine_reorg (gcc::context *ctxt)
3918 594360 : : rtl_opt_pass (pass_data_machine_reorg, ctxt)
3919 : {}
3920 :
3921 : /* opt_pass methods: */
3922 1517135 : bool gate (function *) final override
3923 : {
3924 1517135 : return targetm.machine_dependent_reorg != 0;
3925 : }
3926 :
3927 1517128 : unsigned int execute (function *) final override
3928 : {
3929 1517128 : targetm.machine_dependent_reorg ();
3930 1517128 : return 0;
3931 : }
3932 :
3933 : }; // class pass_machine_reorg
3934 :
3935 : } // anon namespace
3936 :
3937 : rtl_opt_pass *
3938 297180 : make_pass_machine_reorg (gcc::context *ctxt)
3939 : {
3940 297180 : return new pass_machine_reorg (ctxt);
3941 : }
|