Debian Codegraph @

gcc/reorg.cc

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