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