1 /* Instruction scheduling pass. This file computes dependencies between
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5 Free Software Foundation, Inc.
6 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
7 and currently maintained by, Jim Wilson (wilson@cygnus.com)
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
27 #include "coretypes.h"
32 #include "hard-reg-set.h"
36 #include "insn-config.h"
37 #include "insn-attr.h"
41 #include "sched-int.h"
45 #ifdef ENABLE_CHECKING
51 /* Return the major type present in the DS. */
59 return REG_DEP_OUTPUT
;
61 gcc_assert (ds
& DEP_ANTI
);
66 /* Return equivalent dep_status. */
68 dk_to_ds (enum reg_note dk
)
79 gcc_assert (dk
== REG_DEP_ANTI
);
84 /* Functions to operate with dependence information container - dep_t. */
86 /* Init DEP with the arguments. */
88 init_dep_1 (dep_t dep
, rtx pro
, rtx con
, enum reg_note kind
, ds_t ds
)
92 DEP_KIND (dep
) = kind
;
93 DEP_STATUS (dep
) = ds
;
96 /* Init DEP with the arguments.
97 While most of the scheduler (including targets) only need the major type
98 of the dependency, it is convenient to hide full dep_status from them. */
100 init_dep (dep_t dep
, rtx pro
, rtx con
, enum reg_note kind
)
104 if ((current_sched_info
->flags
& USE_DEPS_LIST
) != 0)
105 ds
= dk_to_ds (kind
);
109 init_dep_1 (dep
, pro
, con
, kind
, ds
);
112 /* Make a copy of FROM in TO. */
114 copy_dep (dep_t to
, dep_t from
)
116 memcpy (to
, from
, sizeof (*to
));
119 /* Functions to operate with a single link from the dependencies lists -
122 /* Return true if dep_link L is consistent. */
124 dep_link_consistent_p (dep_link_t l
)
126 dep_link_t next
= DEP_LINK_NEXT (l
);
129 || &DEP_LINK_NEXT (l
) == DEP_LINK_PREV_NEXTP (next
));
132 /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
135 attach_dep_link (dep_link_t l
, dep_link_t
*prev_nextp
)
137 dep_link_t next
= *prev_nextp
;
139 gcc_assert (DEP_LINK_PREV_NEXTP (l
) == NULL
140 && DEP_LINK_NEXT (l
) == NULL
);
142 /* Init node being inserted. */
143 DEP_LINK_PREV_NEXTP (l
) = prev_nextp
;
144 DEP_LINK_NEXT (l
) = next
;
149 gcc_assert (DEP_LINK_PREV_NEXTP (next
) == prev_nextp
);
151 DEP_LINK_PREV_NEXTP (next
) = &DEP_LINK_NEXT (l
);
158 /* Add dep_link LINK to deps_list L. */
160 add_to_deps_list (dep_link_t link
, deps_list_t l
)
162 attach_dep_link (link
, &DEPS_LIST_FIRST (l
));
165 /* Detach dep_link L from the list. */
167 detach_dep_link (dep_link_t l
)
169 dep_link_t
*prev_nextp
= DEP_LINK_PREV_NEXTP (l
);
170 dep_link_t next
= DEP_LINK_NEXT (l
);
175 DEP_LINK_PREV_NEXTP (next
) = prev_nextp
;
177 /* Though this is property is not used anywhere but in the assert in
178 attach_dep_link (), this can prevent latent errors. */
179 DEP_LINK_PREV_NEXTP (l
) = NULL
;
180 DEP_LINK_NEXT (l
) = NULL
;
183 /* Move LINK from whatever list it is now to L. */
185 move_dep_link (dep_link_t link
, deps_list_t l
)
187 detach_dep_link (link
);
188 add_to_deps_list (link
, l
);
191 /* Check L's and its successors' consistency.
192 This is, potentially, an expensive check, hence it should be guarded by
193 ENABLE_CHECKING at all times. */
195 dep_links_consistent_p (dep_link_t l
)
199 if (dep_link_consistent_p (l
))
200 l
= DEP_LINK_NEXT (l
);
208 /* Dump dep_nodes starting from l. */
210 dump_dep_links (FILE *dump
, dep_link_t l
)
214 dep_t d
= DEP_LINK_DEP (l
);
216 fprintf (dump
, "%d%c>%d ", INSN_UID (DEP_PRO (d
)),
217 dep_link_consistent_p (l
) ? '-' : '!', INSN_UID (DEP_CON (d
)));
219 l
= DEP_LINK_NEXT (l
);
222 fprintf (dump
, "\n");
225 /* Dump dep_nodes starting from L to stderr. */
227 debug_dep_links (dep_link_t l
)
229 dump_dep_links (stderr
, l
);
232 /* Obstack to allocate dep_nodes and deps_lists on. */
233 static struct obstack deps_obstack
;
235 /* Obstack to hold forward dependencies lists (deps_list_t). */
236 static struct obstack
*dl_obstack
= &deps_obstack
;
238 /* Obstack to hold all dependency nodes (dep_node_t). */
239 static struct obstack
*dn_obstack
= &deps_obstack
;
241 /* Functions to operate with dependences lists - deps_list_t. */
243 /* Allocate deps_list.
245 If ON_OBSTACK_P is true, allocate the list on the obstack. This is done for
246 INSN_FORW_DEPS lists because they should live till the end of scheduling.
248 INSN_BACK_DEPS and INSN_RESOLVED_BACK_DEPS lists are allocated on the free
249 store and are being freed in haifa-sched.c: schedule_insn (). */
251 alloc_deps_list (bool on_obstack_p
)
254 return obstack_alloc (dl_obstack
, sizeof (struct _deps_list
));
256 return xmalloc (sizeof (struct _deps_list
));
259 /* Initialize deps_list L. */
261 init_deps_list (deps_list_t l
)
263 DEPS_LIST_FIRST (l
) = NULL
;
266 /* Create (allocate and init) deps_list.
267 The meaning of ON_OBSTACK_P is the same as in alloc_deps_list (). */
269 create_deps_list (bool on_obstack_p
)
271 deps_list_t l
= alloc_deps_list (on_obstack_p
);
277 /* Free dep_data_nodes that present in L. */
279 clear_deps_list (deps_list_t l
)
281 /* All dep_nodes are allocated on the dn_obstack. They'll be freed with
284 DEPS_LIST_FIRST (l
) = NULL
;
287 /* Free deps_list L. */
289 free_deps_list (deps_list_t l
)
291 gcc_assert (deps_list_empty_p (l
));
295 /* Delete (clear and free) deps_list L. */
297 delete_deps_list (deps_list_t l
)
303 /* Return true if L is empty. */
305 deps_list_empty_p (deps_list_t l
)
307 return DEPS_LIST_FIRST (l
) == NULL
;
310 /* Check L's consistency.
311 This is, potentially, an expensive check, hence it should be guarded by
312 ENABLE_CHECKING at all times. */
314 deps_list_consistent_p (deps_list_t l
)
316 dep_link_t first
= DEPS_LIST_FIRST (l
);
318 return (first
== NULL
319 || (&DEPS_LIST_FIRST (l
) == DEP_LINK_PREV_NEXTP (first
)
320 && dep_links_consistent_p (first
)));
325 dump_deps_list (FILE *f
, deps_list_t l
)
327 dump_dep_links (f
, DEPS_LIST_FIRST (l
));
330 /* Dump L to STDERR. */
332 debug_deps_list (deps_list_t l
)
334 dump_deps_list (stderr
, l
);
337 /* Add a dependency described by DEP to the list L.
338 L should be either INSN_BACK_DEPS or INSN_RESOLVED_BACK_DEPS. */
340 add_back_dep_to_deps_list (deps_list_t l
, dep_t dep_from
)
342 dep_node_t n
= (dep_node_t
) obstack_alloc (dn_obstack
,
344 dep_t dep_to
= DEP_NODE_DEP (n
);
345 dep_link_t back
= DEP_NODE_BACK (n
);
346 dep_link_t forw
= DEP_NODE_FORW (n
);
348 copy_dep (dep_to
, dep_from
);
350 DEP_LINK_NODE (back
) = n
;
351 DEP_LINK_NODE (forw
) = n
;
353 /* There is no particular need to initialize these four fields except to make
354 assert in attach_dep_link () happy. */
355 DEP_LINK_NEXT (back
) = NULL
;
356 DEP_LINK_PREV_NEXTP (back
) = NULL
;
357 DEP_LINK_NEXT (forw
) = NULL
;
358 DEP_LINK_PREV_NEXTP (forw
) = NULL
;
360 add_to_deps_list (back
, l
);
363 /* Find the dep_link with producer PRO in deps_list L. */
365 find_link_by_pro_in_deps_list (deps_list_t l
, rtx pro
)
369 FOR_EACH_DEP_LINK (link
, l
)
370 if (DEP_LINK_PRO (link
) == pro
)
376 /* Find the dep_link with consumer CON in deps_list L. */
378 find_link_by_con_in_deps_list (deps_list_t l
, rtx con
)
382 FOR_EACH_DEP_LINK (link
, l
)
383 if (DEP_LINK_CON (link
) == con
)
389 /* Make a copy of FROM in TO with substituting consumer with CON.
390 TO and FROM should be RESOLVED_BACK_DEPS lists. */
392 copy_deps_list_change_con (deps_list_t to
, deps_list_t from
, rtx con
)
396 gcc_assert (deps_list_empty_p (to
));
398 FOR_EACH_DEP_LINK (l
, from
)
400 add_back_dep_to_deps_list (to
, DEP_LINK_DEP (l
));
401 DEP_LINK_CON (DEPS_LIST_FIRST (to
)) = con
;
405 static regset reg_pending_sets
;
406 static regset reg_pending_clobbers
;
407 static regset reg_pending_uses
;
409 /* The following enumeration values tell us what dependencies we
410 should use to implement the barrier. We use true-dependencies for
411 TRUE_BARRIER and anti-dependencies for MOVE_BARRIER. */
412 enum reg_pending_barrier_mode
419 static enum reg_pending_barrier_mode reg_pending_barrier
;
421 /* To speed up the test for duplicate dependency links we keep a
422 record of dependencies created by add_dependence when the average
423 number of instructions in a basic block is very large.
425 Studies have shown that there is typically around 5 instructions between
426 branches for typical C code. So we can make a guess that the average
427 basic block is approximately 5 instructions long; we will choose 100X
428 the average size as a very large basic block.
430 Each insn has associated bitmaps for its dependencies. Each bitmap
431 has enough entries to represent a dependency on any other insn in
432 the insn chain. All bitmap for true dependencies cache is
433 allocated then the rest two ones are also allocated. */
434 static bitmap_head
*true_dependency_cache
;
435 static bitmap_head
*output_dependency_cache
;
436 static bitmap_head
*anti_dependency_cache
;
437 static bitmap_head
*spec_dependency_cache
;
438 static int cache_size
;
440 /* To speed up checking consistency of formed forward insn
441 dependencies we use the following cache. Another possible solution
442 could be switching off checking duplication of insns in forward
444 #ifdef ENABLE_CHECKING
445 static bitmap_head
*forward_dependency_cache
;
448 static int deps_may_trap_p (rtx
);
449 static void add_dependence_list (rtx
, rtx
, int, enum reg_note
);
450 static void add_dependence_list_and_free (rtx
, rtx
*, int, enum reg_note
);
451 static void delete_all_dependences (rtx
);
452 static void fixup_sched_groups (rtx
);
454 static void flush_pending_lists (struct deps
*, rtx
, int, int);
455 static void sched_analyze_1 (struct deps
*, rtx
, rtx
);
456 static void sched_analyze_2 (struct deps
*, rtx
, rtx
);
457 static void sched_analyze_insn (struct deps
*, rtx
, rtx
);
459 static rtx
sched_get_condition (rtx
);
460 static int conditions_mutex_p (rtx
, rtx
);
462 static enum DEPS_ADJUST_RESULT
maybe_add_or_update_back_dep_1 (rtx
, rtx
,
463 enum reg_note
, ds_t
, rtx
, rtx
, dep_link_t
**);
464 static enum DEPS_ADJUST_RESULT
add_or_update_back_dep_1 (rtx
, rtx
,
465 enum reg_note
, ds_t
, rtx
, rtx
, dep_link_t
**);
466 static void add_back_dep (rtx
, rtx
, enum reg_note
, ds_t
);
468 static void adjust_add_sorted_back_dep (rtx
, dep_link_t
, dep_link_t
*);
469 static void adjust_back_add_forw_dep (rtx
, dep_link_t
*);
470 static void delete_forw_dep (dep_link_t
);
471 static dw_t
estimate_dep_weak (rtx
, rtx
);
472 #ifdef INSN_SCHEDULING
473 #ifdef ENABLE_CHECKING
474 static void check_dep_status (enum reg_note
, ds_t
, bool);
478 /* Return nonzero if a load of the memory reference MEM can cause a trap. */
481 deps_may_trap_p (rtx mem
)
483 rtx addr
= XEXP (mem
, 0);
485 if (REG_P (addr
) && REGNO (addr
) >= FIRST_PSEUDO_REGISTER
)
487 rtx t
= get_reg_known_value (REGNO (addr
));
491 return rtx_addr_can_trap_p (addr
);
494 /* Find the condition under which INSN is executed. */
497 sched_get_condition (rtx insn
)
499 rtx pat
= PATTERN (insn
);
505 if (GET_CODE (pat
) == COND_EXEC
)
506 return COND_EXEC_TEST (pat
);
508 if (!any_condjump_p (insn
) || !onlyjump_p (insn
))
511 src
= SET_SRC (pc_set (insn
));
513 if (XEXP (src
, 2) == pc_rtx
)
514 return XEXP (src
, 0);
515 else if (XEXP (src
, 1) == pc_rtx
)
517 rtx cond
= XEXP (src
, 0);
518 enum rtx_code revcode
= reversed_comparison_code (cond
, insn
);
520 if (revcode
== UNKNOWN
)
522 return gen_rtx_fmt_ee (revcode
, GET_MODE (cond
), XEXP (cond
, 0),
530 /* Return nonzero if conditions COND1 and COND2 can never be both true. */
533 conditions_mutex_p (rtx cond1
, rtx cond2
)
535 if (COMPARISON_P (cond1
)
536 && COMPARISON_P (cond2
)
537 && GET_CODE (cond1
) == reversed_comparison_code (cond2
, NULL
)
538 && XEXP (cond1
, 0) == XEXP (cond2
, 0)
539 && XEXP (cond1
, 1) == XEXP (cond2
, 1))
544 /* Return true if insn1 and insn2 can never depend on one another because
545 the conditions under which they are executed are mutually exclusive. */
547 sched_insns_conditions_mutex_p (rtx insn1
, rtx insn2
)
551 /* df doesn't handle conditional lifetimes entirely correctly;
552 calls mess up the conditional lifetimes. */
553 if (!CALL_P (insn1
) && !CALL_P (insn2
))
555 cond1
= sched_get_condition (insn1
);
556 cond2
= sched_get_condition (insn2
);
558 && conditions_mutex_p (cond1
, cond2
)
559 /* Make sure first instruction doesn't affect condition of second
560 instruction if switched. */
561 && !modified_in_p (cond1
, insn2
)
562 /* Make sure second instruction doesn't affect condition of first
563 instruction if switched. */
564 && !modified_in_p (cond2
, insn1
))
570 /* Add ELEM wrapped in an dep_link with reg note kind DEP_TYPE to the
571 INSN_BACK_DEPS (INSN), if it is not already there. DEP_TYPE indicates the
572 type of dependence that this link represents. DS, if nonzero,
573 indicates speculations, through which this dependence can be overcome.
574 MEM1 and MEM2, if non-null, corresponds to memory locations in case of
575 data speculation. The function returns a value indicating if an old entry
576 has been changed or a new entry has been added to insn's backward deps.
577 In case of changed entry CHANGED_LINKPP sets to its address.
578 See also the definition of enum DEPS_ADJUST_RESULT in sched-int.h.
579 Actual manipulation of dependence data structures is performed in
580 add_or_update_back_dep_1. */
582 static enum DEPS_ADJUST_RESULT
583 maybe_add_or_update_back_dep_1 (rtx insn
, rtx elem
, enum reg_note dep_type
,
584 ds_t ds
, rtx mem1
, rtx mem2
,
585 dep_link_t
**changed_linkpp
)
587 gcc_assert (INSN_P (insn
) && INSN_P (elem
));
589 /* Don't depend an insn on itself. */
592 #ifdef INSN_SCHEDULING
593 if (current_sched_info
->flags
& DO_SPECULATION
)
594 /* INSN has an internal dependence, which we can't overcome. */
595 HAS_INTERNAL_DEP (insn
) = 1;
600 return add_or_update_back_dep_1 (insn
, elem
, dep_type
,
601 ds
, mem1
, mem2
, changed_linkpp
);
604 /* This function has the same meaning of parameters and return values
605 as maybe_add_or_update_back_dep_1. The only difference between these
606 two functions is that INSN and ELEM are guaranteed not to be the same
608 static enum DEPS_ADJUST_RESULT
609 add_or_update_back_dep_1 (rtx insn
, rtx elem
, enum reg_note dep_type
,
610 ds_t ds ATTRIBUTE_UNUSED
,
611 rtx mem1 ATTRIBUTE_UNUSED
, rtx mem2 ATTRIBUTE_UNUSED
,
612 dep_link_t
**changed_linkpp ATTRIBUTE_UNUSED
)
614 bool maybe_present_p
= true, present_p
= false;
616 gcc_assert (INSN_P (insn
) && INSN_P (elem
) && insn
!= elem
);
618 #ifdef INSN_SCHEDULING
620 #ifdef ENABLE_CHECKING
621 check_dep_status (dep_type
, ds
, mem1
!= NULL
);
624 /* If we already have a dependency for ELEM, then we do not need to
625 do anything. Avoiding the list walk below can cut compile times
626 dramatically for some code. */
627 if (true_dependency_cache
!= NULL
)
629 enum reg_note present_dep_type
;
631 gcc_assert (output_dependency_cache
);
632 gcc_assert (anti_dependency_cache
);
633 if (!(current_sched_info
->flags
& USE_DEPS_LIST
))
635 if (bitmap_bit_p (&true_dependency_cache
[INSN_LUID (insn
)],
637 present_dep_type
= REG_DEP_TRUE
;
638 else if (bitmap_bit_p (&output_dependency_cache
[INSN_LUID (insn
)],
640 present_dep_type
= REG_DEP_OUTPUT
;
641 else if (bitmap_bit_p (&anti_dependency_cache
[INSN_LUID (insn
)],
643 present_dep_type
= REG_DEP_ANTI
;
645 maybe_present_p
= false;
649 if ((int) dep_type
>= (int) present_dep_type
)
657 ds_t present_dep_types
= 0;
659 if (bitmap_bit_p (&true_dependency_cache
[INSN_LUID (insn
)],
661 present_dep_types
|= DEP_TRUE
;
662 if (bitmap_bit_p (&output_dependency_cache
[INSN_LUID (insn
)],
664 present_dep_types
|= DEP_OUTPUT
;
665 if (bitmap_bit_p (&anti_dependency_cache
[INSN_LUID (insn
)],
667 present_dep_types
|= DEP_ANTI
;
669 if (present_dep_types
)
671 if (!(current_sched_info
->flags
& DO_SPECULATION
)
672 || !bitmap_bit_p (&spec_dependency_cache
[INSN_LUID (insn
)],
675 if ((present_dep_types
| (ds
& DEP_TYPES
))
676 == present_dep_types
)
677 /* We already have all these bits. */
682 /* Only true dependencies can be data speculative and
683 only anti dependencies can be control speculative. */
684 gcc_assert ((present_dep_types
& (DEP_TRUE
| DEP_ANTI
))
685 == present_dep_types
);
687 /* if (additional dep is SPECULATIVE) then
688 we should update DEP_STATUS
690 we should reset existing dep to non-speculative. */
696 maybe_present_p
= false;
701 /* Check that we don't already have this dependence. */
706 for (linkp
= &DEPS_LIST_FIRST (INSN_BACK_DEPS (insn
));
708 linkp
= &DEP_LINK_NEXT (*linkp
))
710 dep_t link
= DEP_LINK_DEP (*linkp
);
712 gcc_assert (true_dependency_cache
== 0 || present_p
);
714 if (DEP_PRO (link
) == elem
)
716 enum DEPS_ADJUST_RESULT changed_p
= DEP_PRESENT
;
718 #ifdef INSN_SCHEDULING
719 if (current_sched_info
->flags
& USE_DEPS_LIST
)
721 ds_t new_status
= ds
| DEP_STATUS (link
);
723 if (new_status
& SPECULATIVE
)
725 if (!(ds
& SPECULATIVE
)
726 || !(DEP_STATUS (link
) & SPECULATIVE
))
727 /* Then this dep can't be speculative. */
729 new_status
&= ~SPECULATIVE
;
730 if (true_dependency_cache
731 && (DEP_STATUS (link
) & SPECULATIVE
))
732 bitmap_clear_bit (&spec_dependency_cache
738 /* Both are speculative. Merging probabilities. */
743 dw
= estimate_dep_weak (mem1
, mem2
);
744 ds
= set_dep_weak (ds
, BEGIN_DATA
, dw
);
747 new_status
= ds_merge (DEP_STATUS (link
), ds
);
754 /* Clear corresponding cache entry because type of the link
755 may have changed. Keep them if we use_deps_list. */
756 if (true_dependency_cache
!= NULL
757 && !(current_sched_info
->flags
& USE_DEPS_LIST
))
759 enum reg_note kind
= DEP_KIND (link
);
764 bitmap_clear_bit (&output_dependency_cache
765 [INSN_LUID (insn
)], INSN_LUID (elem
));
768 bitmap_clear_bit (&anti_dependency_cache
769 [INSN_LUID (insn
)], INSN_LUID (elem
));
776 if ((current_sched_info
->flags
& USE_DEPS_LIST
)
777 && DEP_STATUS (link
) != ds
)
779 DEP_STATUS (link
) = ds
;
780 changed_p
= DEP_CHANGED
;
784 /* If this is a more restrictive type of dependence than the
785 existing one, then change the existing dependence to this
787 if ((int) dep_type
< (int) DEP_KIND (link
))
789 DEP_KIND (link
) = dep_type
;
790 changed_p
= DEP_CHANGED
;
793 #ifdef INSN_SCHEDULING
794 /* If we are adding a dependency to INSN's LOG_LINKs, then
795 note that in the bitmap caches of dependency information. */
796 if (true_dependency_cache
!= NULL
)
798 if (!(current_sched_info
->flags
& USE_DEPS_LIST
))
800 if (DEP_KIND (link
) == REG_DEP_TRUE
)
801 bitmap_set_bit (&true_dependency_cache
802 [INSN_LUID (insn
)], INSN_LUID (elem
));
803 else if (DEP_KIND (link
) == REG_DEP_OUTPUT
)
804 bitmap_set_bit (&output_dependency_cache
805 [INSN_LUID (insn
)], INSN_LUID (elem
));
806 else if (DEP_KIND (link
) == REG_DEP_ANTI
)
807 bitmap_set_bit (&anti_dependency_cache
808 [INSN_LUID (insn
)], INSN_LUID (elem
));
813 bitmap_set_bit (&true_dependency_cache
814 [INSN_LUID (insn
)], INSN_LUID (elem
));
816 bitmap_set_bit (&output_dependency_cache
817 [INSN_LUID (insn
)], INSN_LUID (elem
));
819 bitmap_set_bit (&anti_dependency_cache
820 [INSN_LUID (insn
)], INSN_LUID (elem
));
821 /* Note, that dep can become speculative only
822 at the moment of creation. Thus, we don't need to
823 check for it here. */
827 if (changed_linkpp
&& changed_p
== DEP_CHANGED
)
828 *changed_linkpp
= linkp
;
833 /* We didn't find a dep. It shouldn't be present in the cache. */
834 gcc_assert (!present_p
);
837 /* Might want to check one level of transitivity to save conses.
838 This check should be done in maybe_add_or_update_back_dep_1.
839 Since we made it to add_or_update_back_dep_1, we must create
840 (or update) a link. */
844 gcc_assert (current_sched_info
->flags
& DO_SPECULATION
);
845 ds
= set_dep_weak (ds
, BEGIN_DATA
, estimate_dep_weak (mem1
, mem2
));
848 add_back_dep (insn
, elem
, dep_type
, ds
);
853 /* This function creates a link between INSN and ELEM under any
854 conditions. DS describes speculative status of the link. */
856 add_back_dep (rtx insn
, rtx elem
, enum reg_note dep_type
, ds_t ds
)
858 struct _dep _dep
, *dep
= &_dep
;
860 gcc_assert (INSN_P (insn
) && INSN_P (elem
) && insn
!= elem
);
862 if (current_sched_info
->flags
& USE_DEPS_LIST
)
863 init_dep_1 (dep
, elem
, insn
, dep_type
, ds
);
865 init_dep_1 (dep
, elem
, insn
, dep_type
, -1);
867 add_back_dep_to_deps_list (INSN_BACK_DEPS (insn
), dep
);
869 #ifdef INSN_SCHEDULING
870 #ifdef ENABLE_CHECKING
871 check_dep_status (dep_type
, ds
, false);
874 /* If we are adding a dependency to INSN's LOG_LINKs, then note that
875 in the bitmap caches of dependency information. */
876 if (true_dependency_cache
!= NULL
)
878 if (!(current_sched_info
->flags
& USE_DEPS_LIST
))
880 if (dep_type
== REG_DEP_TRUE
)
881 bitmap_set_bit (&true_dependency_cache
[INSN_LUID (insn
)],
883 else if (dep_type
== REG_DEP_OUTPUT
)
884 bitmap_set_bit (&output_dependency_cache
[INSN_LUID (insn
)],
886 else if (dep_type
== REG_DEP_ANTI
)
887 bitmap_set_bit (&anti_dependency_cache
[INSN_LUID (insn
)],
893 bitmap_set_bit (&true_dependency_cache
[INSN_LUID (insn
)],
896 bitmap_set_bit (&output_dependency_cache
[INSN_LUID (insn
)],
899 bitmap_set_bit (&anti_dependency_cache
[INSN_LUID (insn
)],
901 if (ds
& SPECULATIVE
)
903 gcc_assert (current_sched_info
->flags
& DO_SPECULATION
);
904 bitmap_set_bit (&spec_dependency_cache
[INSN_LUID (insn
)],
912 /* A convenience wrapper to operate on an entire list. */
915 add_dependence_list (rtx insn
, rtx list
, int uncond
, enum reg_note dep_type
)
917 for (; list
; list
= XEXP (list
, 1))
919 if (uncond
|| ! sched_insns_conditions_mutex_p (insn
, XEXP (list
, 0)))
920 add_dependence (insn
, XEXP (list
, 0), dep_type
);
924 /* Similar, but free *LISTP at the same time. */
927 add_dependence_list_and_free (rtx insn
, rtx
*listp
, int uncond
,
928 enum reg_note dep_type
)
931 for (list
= *listp
, *listp
= NULL
; list
; list
= next
)
933 next
= XEXP (list
, 1);
934 if (uncond
|| ! sched_insns_conditions_mutex_p (insn
, XEXP (list
, 0)))
935 add_dependence (insn
, XEXP (list
, 0), dep_type
);
936 free_INSN_LIST_node (list
);
940 /* Clear all dependencies for an insn. */
943 delete_all_dependences (rtx insn
)
945 /* Clear caches, if they exist, as well as free the dependence. */
947 #ifdef INSN_SCHEDULING
948 if (true_dependency_cache
!= NULL
)
950 bitmap_clear (&true_dependency_cache
[INSN_LUID (insn
)]);
951 bitmap_clear (&output_dependency_cache
[INSN_LUID (insn
)]);
952 bitmap_clear (&anti_dependency_cache
[INSN_LUID (insn
)]);
953 /* We don't have to clear forward_dependency_cache here,
954 because it is formed later. */
955 if (current_sched_info
->flags
& DO_SPECULATION
)
956 bitmap_clear (&spec_dependency_cache
[INSN_LUID (insn
)]);
960 clear_deps_list (INSN_BACK_DEPS (insn
));
963 /* All insns in a scheduling group except the first should only have
964 dependencies on the previous insn in the group. So we find the
965 first instruction in the scheduling group by walking the dependence
966 chains backwards. Then we add the dependencies for the group to
967 the previous nonnote insn. */
970 fixup_sched_groups (rtx insn
)
975 FOR_EACH_DEP_LINK (link
, INSN_BACK_DEPS (insn
))
978 dep_t dep
= DEP_LINK_DEP (link
);
979 rtx pro
= DEP_PRO (dep
);
983 i
= prev_nonnote_insn (i
);
987 } while (SCHED_GROUP_P (i
));
989 if (! sched_insns_conditions_mutex_p (i
, pro
))
990 add_dependence (i
, pro
, DEP_KIND (dep
));
994 delete_all_dependences (insn
);
996 prev_nonnote
= prev_nonnote_insn (insn
);
997 if (BLOCK_FOR_INSN (insn
) == BLOCK_FOR_INSN (prev_nonnote
)
998 && ! sched_insns_conditions_mutex_p (insn
, prev_nonnote
))
999 add_dependence (insn
, prev_nonnote
, REG_DEP_ANTI
);
1002 /* Process an insn's memory dependencies. There are four kinds of
1005 (0) read dependence: read follows read
1006 (1) true dependence: read follows write
1007 (2) output dependence: write follows write
1008 (3) anti dependence: write follows read
1010 We are careful to build only dependencies which actually exist, and
1011 use transitivity to avoid building too many links. */
1013 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1014 The MEM is a memory reference contained within INSN, which we are saving
1015 so that we can do memory aliasing on it. */
1018 add_insn_mem_dependence (struct deps
*deps
, bool read_p
,
1027 insn_list
= &deps
->pending_read_insns
;
1028 mem_list
= &deps
->pending_read_mems
;
1029 deps
->pending_read_list_length
++;
1033 insn_list
= &deps
->pending_write_insns
;
1034 mem_list
= &deps
->pending_write_mems
;
1035 deps
->pending_write_list_length
++;
1038 link
= alloc_INSN_LIST (insn
, *insn_list
);
1041 if (current_sched_info
->use_cselib
)
1043 mem
= shallow_copy_rtx (mem
);
1044 XEXP (mem
, 0) = cselib_subst_to_values (XEXP (mem
, 0));
1046 link
= alloc_EXPR_LIST (VOIDmode
, canon_rtx (mem
), *mem_list
);
1050 /* Make a dependency between every memory reference on the pending lists
1051 and INSN, thus flushing the pending lists. FOR_READ is true if emitting
1052 dependencies for a read operation, similarly with FOR_WRITE. */
1055 flush_pending_lists (struct deps
*deps
, rtx insn
, int for_read
,
1060 add_dependence_list_and_free (insn
, &deps
->pending_read_insns
, 1,
1062 free_EXPR_LIST_list (&deps
->pending_read_mems
);
1063 deps
->pending_read_list_length
= 0;
1066 add_dependence_list_and_free (insn
, &deps
->pending_write_insns
, 1,
1067 for_read
? REG_DEP_ANTI
: REG_DEP_OUTPUT
);
1068 free_EXPR_LIST_list (&deps
->pending_write_mems
);
1069 deps
->pending_write_list_length
= 0;
1071 add_dependence_list_and_free (insn
, &deps
->last_pending_memory_flush
, 1,
1072 for_read
? REG_DEP_ANTI
: REG_DEP_OUTPUT
);
1073 deps
->last_pending_memory_flush
= alloc_INSN_LIST (insn
, NULL_RTX
);
1074 deps
->pending_flush_length
= 1;
1077 /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
1078 The type of the reference is specified by REF and can be SET,
1079 CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE. */
1082 sched_analyze_reg (struct deps
*deps
, int regno
, enum machine_mode mode
,
1083 enum rtx_code ref
, rtx insn
)
1085 /* A hard reg in a wide mode may really be multiple registers.
1086 If so, mark all of them just like the first. */
1087 if (regno
< FIRST_PSEUDO_REGISTER
)
1089 int i
= hard_regno_nregs
[regno
][mode
];
1093 SET_REGNO_REG_SET (reg_pending_sets
, regno
+ i
);
1095 else if (ref
== USE
)
1098 SET_REGNO_REG_SET (reg_pending_uses
, regno
+ i
);
1103 SET_REGNO_REG_SET (reg_pending_clobbers
, regno
+ i
);
1107 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
1108 it does not reload. Ignore these as they have served their
1110 else if (regno
>= deps
->max_reg
)
1112 enum rtx_code code
= GET_CODE (PATTERN (insn
));
1113 gcc_assert (code
== USE
|| code
== CLOBBER
);
1119 SET_REGNO_REG_SET (reg_pending_sets
, regno
);
1120 else if (ref
== USE
)
1121 SET_REGNO_REG_SET (reg_pending_uses
, regno
);
1123 SET_REGNO_REG_SET (reg_pending_clobbers
, regno
);
1125 /* Pseudos that are REG_EQUIV to something may be replaced
1126 by that during reloading. We need only add dependencies for
1127 the address in the REG_EQUIV note. */
1128 if (!reload_completed
&& get_reg_known_equiv_p (regno
))
1130 rtx t
= get_reg_known_value (regno
);
1132 sched_analyze_2 (deps
, XEXP (t
, 0), insn
);
1135 /* Don't let it cross a call after scheduling if it doesn't
1136 already cross one. */
1137 if (REG_N_CALLS_CROSSED (regno
) == 0)
1140 deps
->sched_before_next_call
1141 = alloc_INSN_LIST (insn
, deps
->sched_before_next_call
);
1143 add_dependence_list (insn
, deps
->last_function_call
, 1,
1149 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
1150 rtx, X, creating all dependencies generated by the write to the
1151 destination of X, and reads of everything mentioned. */
1154 sched_analyze_1 (struct deps
*deps
, rtx x
, rtx insn
)
1156 rtx dest
= XEXP (x
, 0);
1157 enum rtx_code code
= GET_CODE (x
);
1162 if (GET_CODE (dest
) == PARALLEL
)
1166 for (i
= XVECLEN (dest
, 0) - 1; i
>= 0; i
--)
1167 if (XEXP (XVECEXP (dest
, 0, i
), 0) != 0)
1168 sched_analyze_1 (deps
,
1169 gen_rtx_CLOBBER (VOIDmode
,
1170 XEXP (XVECEXP (dest
, 0, i
), 0)),
1173 if (GET_CODE (x
) == SET
)
1174 sched_analyze_2 (deps
, SET_SRC (x
), insn
);
1178 while (GET_CODE (dest
) == STRICT_LOW_PART
|| GET_CODE (dest
) == SUBREG
1179 || GET_CODE (dest
) == ZERO_EXTRACT
)
1181 if (GET_CODE (dest
) == STRICT_LOW_PART
1182 || GET_CODE (dest
) == ZERO_EXTRACT
1183 || df_read_modify_subreg_p (dest
))
1185 /* These both read and modify the result. We must handle
1186 them as writes to get proper dependencies for following
1187 instructions. We must handle them as reads to get proper
1188 dependencies from this to previous instructions.
1189 Thus we need to call sched_analyze_2. */
1191 sched_analyze_2 (deps
, XEXP (dest
, 0), insn
);
1193 if (GET_CODE (dest
) == ZERO_EXTRACT
)
1195 /* The second and third arguments are values read by this insn. */
1196 sched_analyze_2 (deps
, XEXP (dest
, 1), insn
);
1197 sched_analyze_2 (deps
, XEXP (dest
, 2), insn
);
1199 dest
= XEXP (dest
, 0);
1204 int regno
= REGNO (dest
);
1205 enum machine_mode mode
= GET_MODE (dest
);
1207 sched_analyze_reg (deps
, regno
, mode
, code
, insn
);
1210 /* Treat all writes to a stack register as modifying the TOS. */
1211 if (regno
>= FIRST_STACK_REG
&& regno
<= LAST_STACK_REG
)
1213 /* Avoid analyzing the same register twice. */
1214 if (regno
!= FIRST_STACK_REG
)
1215 sched_analyze_reg (deps
, FIRST_STACK_REG
, mode
, code
, insn
);
1216 sched_analyze_reg (deps
, FIRST_STACK_REG
, mode
, USE
, insn
);
1220 else if (MEM_P (dest
))
1222 /* Writing memory. */
1225 if (current_sched_info
->use_cselib
)
1227 t
= shallow_copy_rtx (dest
);
1228 cselib_lookup (XEXP (t
, 0), Pmode
, 1);
1229 XEXP (t
, 0) = cselib_subst_to_values (XEXP (t
, 0));
1233 if ((deps
->pending_read_list_length
+ deps
->pending_write_list_length
)
1234 > MAX_PENDING_LIST_LENGTH
)
1236 /* Flush all pending reads and writes to prevent the pending lists
1237 from getting any larger. Insn scheduling runs too slowly when
1238 these lists get long. When compiling GCC with itself,
1239 this flush occurs 8 times for sparc, and 10 times for m88k using
1240 the default value of 32. */
1241 flush_pending_lists (deps
, insn
, false, true);
1245 rtx pending
, pending_mem
;
1247 pending
= deps
->pending_read_insns
;
1248 pending_mem
= deps
->pending_read_mems
;
1251 if (anti_dependence (XEXP (pending_mem
, 0), t
)
1252 && ! sched_insns_conditions_mutex_p (insn
, XEXP (pending
, 0)))
1253 add_dependence (insn
, XEXP (pending
, 0), REG_DEP_ANTI
);
1255 pending
= XEXP (pending
, 1);
1256 pending_mem
= XEXP (pending_mem
, 1);
1259 pending
= deps
->pending_write_insns
;
1260 pending_mem
= deps
->pending_write_mems
;
1263 if (output_dependence (XEXP (pending_mem
, 0), t
)
1264 && ! sched_insns_conditions_mutex_p (insn
, XEXP (pending
, 0)))
1265 add_dependence (insn
, XEXP (pending
, 0), REG_DEP_OUTPUT
);
1267 pending
= XEXP (pending
, 1);
1268 pending_mem
= XEXP (pending_mem
, 1);
1271 add_dependence_list (insn
, deps
->last_pending_memory_flush
, 1,
1274 add_insn_mem_dependence (deps
, false, insn
, dest
);
1276 sched_analyze_2 (deps
, XEXP (dest
, 0), insn
);
1279 /* Analyze reads. */
1280 if (GET_CODE (x
) == SET
)
1281 sched_analyze_2 (deps
, SET_SRC (x
), insn
);
1284 /* Analyze the uses of memory and registers in rtx X in INSN. */
1287 sched_analyze_2 (struct deps
*deps
, rtx x
, rtx insn
)
1297 code
= GET_CODE (x
);
1307 /* Ignore constants. Note that we must handle CONST_DOUBLE here
1308 because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
1309 this does not mean that this insn is using cc0. */
1314 /* User of CC0 depends on immediately preceding insn. */
1315 SCHED_GROUP_P (insn
) = 1;
1316 /* Don't move CC0 setter to another block (it can set up the
1317 same flag for previous CC0 users which is safe). */
1318 CANT_MOVE (prev_nonnote_insn (insn
)) = 1;
1324 int regno
= REGNO (x
);
1325 enum machine_mode mode
= GET_MODE (x
);
1327 sched_analyze_reg (deps
, regno
, mode
, USE
, insn
);
1330 /* Treat all reads of a stack register as modifying the TOS. */
1331 if (regno
>= FIRST_STACK_REG
&& regno
<= LAST_STACK_REG
)
1333 /* Avoid analyzing the same register twice. */
1334 if (regno
!= FIRST_STACK_REG
)
1335 sched_analyze_reg (deps
, FIRST_STACK_REG
, mode
, USE
, insn
);
1336 sched_analyze_reg (deps
, FIRST_STACK_REG
, mode
, SET
, insn
);
1344 /* Reading memory. */
1346 rtx pending
, pending_mem
;
1349 if (current_sched_info
->use_cselib
)
1351 t
= shallow_copy_rtx (t
);
1352 cselib_lookup (XEXP (t
, 0), Pmode
, 1);
1353 XEXP (t
, 0) = cselib_subst_to_values (XEXP (t
, 0));
1356 pending
= deps
->pending_read_insns
;
1357 pending_mem
= deps
->pending_read_mems
;
1360 if (read_dependence (XEXP (pending_mem
, 0), t
)
1361 && ! sched_insns_conditions_mutex_p (insn
, XEXP (pending
, 0)))
1362 add_dependence (insn
, XEXP (pending
, 0), REG_DEP_ANTI
);
1364 pending
= XEXP (pending
, 1);
1365 pending_mem
= XEXP (pending_mem
, 1);
1368 pending
= deps
->pending_write_insns
;
1369 pending_mem
= deps
->pending_write_mems
;
1372 if (true_dependence (XEXP (pending_mem
, 0), VOIDmode
,
1374 && ! sched_insns_conditions_mutex_p (insn
, XEXP (pending
, 0)))
1376 if (current_sched_info
->flags
& DO_SPECULATION
)
1377 maybe_add_or_update_back_dep_1 (insn
, XEXP (pending
, 0),
1379 BEGIN_DATA
| DEP_TRUE
,
1380 XEXP (pending_mem
, 0), t
, 0);
1382 add_dependence (insn
, XEXP (pending
, 0), REG_DEP_TRUE
);
1385 pending
= XEXP (pending
, 1);
1386 pending_mem
= XEXP (pending_mem
, 1);
1389 for (u
= deps
->last_pending_memory_flush
; u
; u
= XEXP (u
, 1))
1390 if (! JUMP_P (XEXP (u
, 0)) || deps_may_trap_p (x
))
1391 add_dependence (insn
, XEXP (u
, 0), REG_DEP_ANTI
);
1393 /* Always add these dependencies to pending_reads, since
1394 this insn may be followed by a write. */
1395 add_insn_mem_dependence (deps
, true, insn
, x
);
1397 /* Take advantage of tail recursion here. */
1398 sched_analyze_2 (deps
, XEXP (x
, 0), insn
);
1402 /* Force pending stores to memory in case a trap handler needs them. */
1404 flush_pending_lists (deps
, insn
, true, false);
1409 case UNSPEC_VOLATILE
:
1411 /* Traditional and volatile asm instructions must be considered to use
1412 and clobber all hard registers, all pseudo-registers and all of
1413 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
1415 Consider for instance a volatile asm that changes the fpu rounding
1416 mode. An insn should not be moved across this even if it only uses
1417 pseudo-regs because it might give an incorrectly rounded result. */
1418 if (code
!= ASM_OPERANDS
|| MEM_VOLATILE_P (x
))
1419 reg_pending_barrier
= TRUE_BARRIER
;
1421 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1422 We can not just fall through here since then we would be confused
1423 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1424 traditional asms unlike their normal usage. */
1426 if (code
== ASM_OPERANDS
)
1428 for (j
= 0; j
< ASM_OPERANDS_INPUT_LENGTH (x
); j
++)
1429 sched_analyze_2 (deps
, ASM_OPERANDS_INPUT (x
, j
), insn
);
1439 /* These both read and modify the result. We must handle them as writes
1440 to get proper dependencies for following instructions. We must handle
1441 them as reads to get proper dependencies from this to previous
1442 instructions. Thus we need to pass them to both sched_analyze_1
1443 and sched_analyze_2. We must call sched_analyze_2 first in order
1444 to get the proper antecedent for the read. */
1445 sched_analyze_2 (deps
, XEXP (x
, 0), insn
);
1446 sched_analyze_1 (deps
, x
, insn
);
1451 /* op0 = op0 + op1 */
1452 sched_analyze_2 (deps
, XEXP (x
, 0), insn
);
1453 sched_analyze_2 (deps
, XEXP (x
, 1), insn
);
1454 sched_analyze_1 (deps
, x
, insn
);
1461 /* Other cases: walk the insn. */
1462 fmt
= GET_RTX_FORMAT (code
);
1463 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1466 sched_analyze_2 (deps
, XEXP (x
, i
), insn
);
1467 else if (fmt
[i
] == 'E')
1468 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1469 sched_analyze_2 (deps
, XVECEXP (x
, i
, j
), insn
);
1473 /* Analyze an INSN with pattern X to find all dependencies. */
1476 sched_analyze_insn (struct deps
*deps
, rtx x
, rtx insn
)
1478 RTX_CODE code
= GET_CODE (x
);
1481 reg_set_iterator rsi
;
1483 if (code
== COND_EXEC
)
1485 sched_analyze_2 (deps
, COND_EXEC_TEST (x
), insn
);
1487 /* ??? Should be recording conditions so we reduce the number of
1488 false dependencies. */
1489 x
= COND_EXEC_CODE (x
);
1490 code
= GET_CODE (x
);
1492 if (code
== SET
|| code
== CLOBBER
)
1494 sched_analyze_1 (deps
, x
, insn
);
1496 /* Bare clobber insns are used for letting life analysis, reg-stack
1497 and others know that a value is dead. Depend on the last call
1498 instruction so that reg-stack won't get confused. */
1499 if (code
== CLOBBER
)
1500 add_dependence_list (insn
, deps
->last_function_call
, 1, REG_DEP_OUTPUT
);
1502 else if (code
== PARALLEL
)
1504 for (i
= XVECLEN (x
, 0); i
--;)
1506 rtx sub
= XVECEXP (x
, 0, i
);
1507 code
= GET_CODE (sub
);
1509 if (code
== COND_EXEC
)
1511 sched_analyze_2 (deps
, COND_EXEC_TEST (sub
), insn
);
1512 sub
= COND_EXEC_CODE (sub
);
1513 code
= GET_CODE (sub
);
1515 if (code
== SET
|| code
== CLOBBER
)
1516 sched_analyze_1 (deps
, sub
, insn
);
1518 sched_analyze_2 (deps
, sub
, insn
);
1522 sched_analyze_2 (deps
, x
, insn
);
1524 /* Mark registers CLOBBERED or used by called function. */
1527 for (link
= CALL_INSN_FUNCTION_USAGE (insn
); link
; link
= XEXP (link
, 1))
1529 if (GET_CODE (XEXP (link
, 0)) == CLOBBER
)
1530 sched_analyze_1 (deps
, XEXP (link
, 0), insn
);
1532 sched_analyze_2 (deps
, XEXP (link
, 0), insn
);
1534 if (find_reg_note (insn
, REG_SETJMP
, NULL
))
1535 reg_pending_barrier
= MOVE_BARRIER
;
1541 next
= next_nonnote_insn (insn
);
1542 if (next
&& BARRIER_P (next
))
1543 reg_pending_barrier
= TRUE_BARRIER
;
1546 rtx pending
, pending_mem
;
1547 regset_head tmp_uses
, tmp_sets
;
1548 INIT_REG_SET (&tmp_uses
);
1549 INIT_REG_SET (&tmp_sets
);
1551 (*current_sched_info
->compute_jump_reg_dependencies
)
1552 (insn
, &deps
->reg_conditional_sets
, &tmp_uses
, &tmp_sets
);
1553 /* Make latency of jump equal to 0 by using anti-dependence. */
1554 EXECUTE_IF_SET_IN_REG_SET (&tmp_uses
, 0, i
, rsi
)
1556 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
1557 add_dependence_list (insn
, reg_last
->sets
, 0, REG_DEP_ANTI
);
1558 add_dependence_list (insn
, reg_last
->clobbers
, 0, REG_DEP_ANTI
);
1559 reg_last
->uses_length
++;
1560 reg_last
->uses
= alloc_INSN_LIST (insn
, reg_last
->uses
);
1562 IOR_REG_SET (reg_pending_sets
, &tmp_sets
);
1564 CLEAR_REG_SET (&tmp_uses
);
1565 CLEAR_REG_SET (&tmp_sets
);
1567 /* All memory writes and volatile reads must happen before the
1568 jump. Non-volatile reads must happen before the jump iff
1569 the result is needed by the above register used mask. */
1571 pending
= deps
->pending_write_insns
;
1572 pending_mem
= deps
->pending_write_mems
;
1575 if (! sched_insns_conditions_mutex_p (insn
, XEXP (pending
, 0)))
1576 add_dependence (insn
, XEXP (pending
, 0), REG_DEP_OUTPUT
);
1577 pending
= XEXP (pending
, 1);
1578 pending_mem
= XEXP (pending_mem
, 1);
1581 pending
= deps
->pending_read_insns
;
1582 pending_mem
= deps
->pending_read_mems
;
1585 if (MEM_VOLATILE_P (XEXP (pending_mem
, 0))
1586 && ! sched_insns_conditions_mutex_p (insn
, XEXP (pending
, 0)))
1587 add_dependence (insn
, XEXP (pending
, 0), REG_DEP_OUTPUT
);
1588 pending
= XEXP (pending
, 1);
1589 pending_mem
= XEXP (pending_mem
, 1);
1592 add_dependence_list (insn
, deps
->last_pending_memory_flush
, 1,
1597 /* If this instruction can throw an exception, then moving it changes
1598 where block boundaries fall. This is mighty confusing elsewhere.
1599 Therefore, prevent such an instruction from being moved. Same for
1600 non-jump instructions that define block boundaries.
1601 ??? Unclear whether this is still necessary in EBB mode. If not,
1602 add_branch_dependences should be adjusted for RGN mode instead. */
1603 if (((CALL_P (insn
) || JUMP_P (insn
)) && can_throw_internal (insn
))
1604 || (NONJUMP_INSN_P (insn
) && control_flow_insn_p (insn
)))
1605 reg_pending_barrier
= MOVE_BARRIER
;
1607 /* Add dependencies if a scheduling barrier was found. */
1608 if (reg_pending_barrier
)
1610 /* In the case of barrier the most added dependencies are not
1611 real, so we use anti-dependence here. */
1612 if (sched_get_condition (insn
))
1614 EXECUTE_IF_SET_IN_REG_SET (&deps
->reg_last_in_use
, 0, i
, rsi
)
1616 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
1617 add_dependence_list (insn
, reg_last
->uses
, 0, REG_DEP_ANTI
);
1619 (insn
, reg_last
->sets
, 0,
1620 reg_pending_barrier
== TRUE_BARRIER
? REG_DEP_TRUE
: REG_DEP_ANTI
);
1622 (insn
, reg_last
->clobbers
, 0,
1623 reg_pending_barrier
== TRUE_BARRIER
? REG_DEP_TRUE
: REG_DEP_ANTI
);
1628 EXECUTE_IF_SET_IN_REG_SET (&deps
->reg_last_in_use
, 0, i
, rsi
)
1630 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
1631 add_dependence_list_and_free (insn
, ®_last
->uses
, 0,
1633 add_dependence_list_and_free
1634 (insn
, ®_last
->sets
, 0,
1635 reg_pending_barrier
== TRUE_BARRIER
? REG_DEP_TRUE
: REG_DEP_ANTI
);
1636 add_dependence_list_and_free
1637 (insn
, ®_last
->clobbers
, 0,
1638 reg_pending_barrier
== TRUE_BARRIER
? REG_DEP_TRUE
: REG_DEP_ANTI
);
1639 reg_last
->uses_length
= 0;
1640 reg_last
->clobbers_length
= 0;
1644 for (i
= 0; i
< (unsigned)deps
->max_reg
; i
++)
1646 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
1647 reg_last
->sets
= alloc_INSN_LIST (insn
, reg_last
->sets
);
1648 SET_REGNO_REG_SET (&deps
->reg_last_in_use
, i
);
1651 flush_pending_lists (deps
, insn
, true, true);
1652 CLEAR_REG_SET (&deps
->reg_conditional_sets
);
1653 reg_pending_barrier
= NOT_A_BARRIER
;
1657 /* If the current insn is conditional, we can't free any
1659 if (sched_get_condition (insn
))
1661 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses
, 0, i
, rsi
)
1663 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
1664 add_dependence_list (insn
, reg_last
->sets
, 0, REG_DEP_TRUE
);
1665 add_dependence_list (insn
, reg_last
->clobbers
, 0, REG_DEP_TRUE
);
1666 reg_last
->uses
= alloc_INSN_LIST (insn
, reg_last
->uses
);
1667 reg_last
->uses_length
++;
1669 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers
, 0, i
, rsi
)
1671 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
1672 add_dependence_list (insn
, reg_last
->sets
, 0, REG_DEP_OUTPUT
);
1673 add_dependence_list (insn
, reg_last
->uses
, 0, REG_DEP_ANTI
);
1674 reg_last
->clobbers
= alloc_INSN_LIST (insn
, reg_last
->clobbers
);
1675 reg_last
->clobbers_length
++;
1677 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets
, 0, i
, rsi
)
1679 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
1680 add_dependence_list (insn
, reg_last
->sets
, 0, REG_DEP_OUTPUT
);
1681 add_dependence_list (insn
, reg_last
->clobbers
, 0, REG_DEP_OUTPUT
);
1682 add_dependence_list (insn
, reg_last
->uses
, 0, REG_DEP_ANTI
);
1683 reg_last
->sets
= alloc_INSN_LIST (insn
, reg_last
->sets
);
1684 SET_REGNO_REG_SET (&deps
->reg_conditional_sets
, i
);
1689 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses
, 0, i
, rsi
)
1691 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
1692 add_dependence_list (insn
, reg_last
->sets
, 0, REG_DEP_TRUE
);
1693 add_dependence_list (insn
, reg_last
->clobbers
, 0, REG_DEP_TRUE
);
1694 reg_last
->uses_length
++;
1695 reg_last
->uses
= alloc_INSN_LIST (insn
, reg_last
->uses
);
1697 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers
, 0, i
, rsi
)
1699 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
1700 if (reg_last
->uses_length
> MAX_PENDING_LIST_LENGTH
1701 || reg_last
->clobbers_length
> MAX_PENDING_LIST_LENGTH
)
1703 add_dependence_list_and_free (insn
, ®_last
->sets
, 0,
1705 add_dependence_list_and_free (insn
, ®_last
->uses
, 0,
1707 add_dependence_list_and_free (insn
, ®_last
->clobbers
, 0,
1709 reg_last
->sets
= alloc_INSN_LIST (insn
, reg_last
->sets
);
1710 reg_last
->clobbers_length
= 0;
1711 reg_last
->uses_length
= 0;
1715 add_dependence_list (insn
, reg_last
->sets
, 0, REG_DEP_OUTPUT
);
1716 add_dependence_list (insn
, reg_last
->uses
, 0, REG_DEP_ANTI
);
1718 reg_last
->clobbers_length
++;
1719 reg_last
->clobbers
= alloc_INSN_LIST (insn
, reg_last
->clobbers
);
1721 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets
, 0, i
, rsi
)
1723 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
1724 add_dependence_list_and_free (insn
, ®_last
->sets
, 0,
1726 add_dependence_list_and_free (insn
, ®_last
->clobbers
, 0,
1728 add_dependence_list_and_free (insn
, ®_last
->uses
, 0,
1730 reg_last
->sets
= alloc_INSN_LIST (insn
, reg_last
->sets
);
1731 reg_last
->uses_length
= 0;
1732 reg_last
->clobbers_length
= 0;
1733 CLEAR_REGNO_REG_SET (&deps
->reg_conditional_sets
, i
);
1737 IOR_REG_SET (&deps
->reg_last_in_use
, reg_pending_uses
);
1738 IOR_REG_SET (&deps
->reg_last_in_use
, reg_pending_clobbers
);
1739 IOR_REG_SET (&deps
->reg_last_in_use
, reg_pending_sets
);
1741 CLEAR_REG_SET (reg_pending_uses
);
1742 CLEAR_REG_SET (reg_pending_clobbers
);
1743 CLEAR_REG_SET (reg_pending_sets
);
1745 /* If we are currently in a libcall scheduling group, then mark the
1746 current insn as being in a scheduling group and that it can not
1747 be moved into a different basic block. */
1749 if (deps
->libcall_block_tail_insn
)
1751 SCHED_GROUP_P (insn
) = 1;
1752 CANT_MOVE (insn
) = 1;
1755 /* If a post-call group is still open, see if it should remain so.
1756 This insn must be a simple move of a hard reg to a pseudo or
1759 We must avoid moving these insns for correctness on
1760 SMALL_REGISTER_CLASS machines, and for special registers like
1761 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
1762 hard regs for all targets. */
1764 if (deps
->in_post_call_group_p
)
1766 rtx tmp
, set
= single_set (insn
);
1767 int src_regno
, dest_regno
;
1770 goto end_call_group
;
1772 tmp
= SET_DEST (set
);
1773 if (GET_CODE (tmp
) == SUBREG
)
1774 tmp
= SUBREG_REG (tmp
);
1776 dest_regno
= REGNO (tmp
);
1778 goto end_call_group
;
1780 tmp
= SET_SRC (set
);
1781 if (GET_CODE (tmp
) == SUBREG
)
1782 tmp
= SUBREG_REG (tmp
);
1783 if ((GET_CODE (tmp
) == PLUS
1784 || GET_CODE (tmp
) == MINUS
)
1785 && REG_P (XEXP (tmp
, 0))
1786 && REGNO (XEXP (tmp
, 0)) == STACK_POINTER_REGNUM
1787 && dest_regno
== STACK_POINTER_REGNUM
)
1788 src_regno
= STACK_POINTER_REGNUM
;
1789 else if (REG_P (tmp
))
1790 src_regno
= REGNO (tmp
);
1792 goto end_call_group
;
1794 if (src_regno
< FIRST_PSEUDO_REGISTER
1795 || dest_regno
< FIRST_PSEUDO_REGISTER
)
1797 if (deps
->in_post_call_group_p
== post_call_initial
)
1798 deps
->in_post_call_group_p
= post_call
;
1800 SCHED_GROUP_P (insn
) = 1;
1801 CANT_MOVE (insn
) = 1;
1806 deps
->in_post_call_group_p
= not_post_call
;
1810 /* Fixup the dependencies in the sched group. */
1811 if (SCHED_GROUP_P (insn
))
1812 fixup_sched_groups (insn
);
1815 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
1816 dependencies for each insn. */
1819 sched_analyze (struct deps
*deps
, rtx head
, rtx tail
)
1823 if (current_sched_info
->use_cselib
)
1826 /* Before reload, if the previous block ended in a call, show that
1827 we are inside a post-call group, so as to keep the lifetimes of
1828 hard registers correct. */
1829 if (! reload_completed
&& !LABEL_P (head
))
1831 insn
= prev_nonnote_insn (head
);
1832 if (insn
&& CALL_P (insn
))
1833 deps
->in_post_call_group_p
= post_call_initial
;
1835 for (insn
= head
;; insn
= NEXT_INSN (insn
))
1837 rtx link
, end_seq
, r0
, set
;
1841 /* These two lists will be freed in schedule_insn (). */
1842 INSN_BACK_DEPS (insn
) = create_deps_list (false);
1843 INSN_RESOLVED_BACK_DEPS (insn
) = create_deps_list (false);
1845 /* This one should be allocated on the obstack because it should live
1846 till the scheduling ends. */
1847 INSN_FORW_DEPS (insn
) = create_deps_list (true);
1850 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
))
1852 /* Make each JUMP_INSN a scheduling barrier for memory
1856 /* Keep the list a reasonable size. */
1857 if (deps
->pending_flush_length
++ > MAX_PENDING_LIST_LENGTH
)
1858 flush_pending_lists (deps
, insn
, true, true);
1860 deps
->last_pending_memory_flush
1861 = alloc_INSN_LIST (insn
, deps
->last_pending_memory_flush
);
1863 sched_analyze_insn (deps
, PATTERN (insn
), insn
);
1865 else if (CALL_P (insn
))
1869 CANT_MOVE (insn
) = 1;
1871 if (find_reg_note (insn
, REG_SETJMP
, NULL
))
1873 /* This is setjmp. Assume that all registers, not just
1874 hard registers, may be clobbered by this call. */
1875 reg_pending_barrier
= MOVE_BARRIER
;
1879 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1880 /* A call may read and modify global register variables. */
1883 SET_REGNO_REG_SET (reg_pending_sets
, i
);
1884 SET_REGNO_REG_SET (reg_pending_uses
, i
);
1886 /* Other call-clobbered hard regs may be clobbered.
1887 Since we only have a choice between 'might be clobbered'
1888 and 'definitely not clobbered', we must include all
1889 partly call-clobbered registers here. */
1890 else if (HARD_REGNO_CALL_PART_CLOBBERED (i
, reg_raw_mode
[i
])
1891 || TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
))
1892 SET_REGNO_REG_SET (reg_pending_clobbers
, i
);
1893 /* We don't know what set of fixed registers might be used
1894 by the function, but it is certain that the stack pointer
1895 is among them, but be conservative. */
1896 else if (fixed_regs
[i
])
1897 SET_REGNO_REG_SET (reg_pending_uses
, i
);
1898 /* The frame pointer is normally not used by the function
1899 itself, but by the debugger. */
1900 /* ??? MIPS o32 is an exception. It uses the frame pointer
1901 in the macro expansion of jal but does not represent this
1902 fact in the call_insn rtl. */
1903 else if (i
== FRAME_POINTER_REGNUM
1904 || (i
== HARD_FRAME_POINTER_REGNUM
1905 && (! reload_completed
|| frame_pointer_needed
)))
1906 SET_REGNO_REG_SET (reg_pending_uses
, i
);
1909 /* For each insn which shouldn't cross a call, add a dependence
1910 between that insn and this call insn. */
1911 add_dependence_list_and_free (insn
, &deps
->sched_before_next_call
, 1,
1914 sched_analyze_insn (deps
, PATTERN (insn
), insn
);
1916 /* In the absence of interprocedural alias analysis, we must flush
1917 all pending reads and writes, and start new dependencies starting
1918 from here. But only flush writes for constant calls (which may
1919 be passed a pointer to something we haven't written yet). */
1920 flush_pending_lists (deps
, insn
, true, !CONST_OR_PURE_CALL_P (insn
));
1922 /* Remember the last function call for limiting lifetimes. */
1923 free_INSN_LIST_list (&deps
->last_function_call
);
1924 deps
->last_function_call
= alloc_INSN_LIST (insn
, NULL_RTX
);
1926 /* Before reload, begin a post-call group, so as to keep the
1927 lifetimes of hard registers correct. */
1928 if (! reload_completed
)
1929 deps
->in_post_call_group_p
= post_call
;
1932 /* EH_REGION insn notes can not appear until well after we complete
1935 gcc_assert (NOTE_KIND (insn
) != NOTE_INSN_EH_REGION_BEG
1936 && NOTE_KIND (insn
) != NOTE_INSN_EH_REGION_END
);
1938 if (current_sched_info
->use_cselib
)
1939 cselib_process_insn (insn
);
1941 /* Now that we have completed handling INSN, check and see if it is
1942 a CLOBBER beginning a libcall block. If it is, record the
1943 end of the libcall sequence.
1945 We want to schedule libcall blocks as a unit before reload. While
1946 this restricts scheduling, it preserves the meaning of a libcall
1949 As a side effect, we may get better code due to decreased register
1950 pressure as well as less chance of a foreign insn appearing in
1952 if (!reload_completed
1953 /* Note we may have nested libcall sequences. We only care about
1954 the outermost libcall sequence. */
1955 && deps
->libcall_block_tail_insn
== 0
1956 /* The sequence must start with a clobber of a register. */
1957 && NONJUMP_INSN_P (insn
)
1958 && GET_CODE (PATTERN (insn
)) == CLOBBER
1959 && (r0
= XEXP (PATTERN (insn
), 0), REG_P (r0
))
1960 && REG_P (XEXP (PATTERN (insn
), 0))
1961 /* The CLOBBER must also have a REG_LIBCALL note attached. */
1962 && (link
= find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
)) != 0
1963 && (end_seq
= XEXP (link
, 0)) != 0
1964 /* The insn referenced by the REG_LIBCALL note must be a
1965 simple nop copy with the same destination as the register
1966 mentioned in the clobber. */
1967 && (set
= single_set (end_seq
)) != 0
1968 && SET_DEST (set
) == r0
&& SET_SRC (set
) == r0
1969 /* And finally the insn referenced by the REG_LIBCALL must
1970 also contain a REG_EQUAL note and a REG_RETVAL note. */
1971 && find_reg_note (end_seq
, REG_EQUAL
, NULL_RTX
) != 0
1972 && find_reg_note (end_seq
, REG_RETVAL
, NULL_RTX
) != 0)
1973 deps
->libcall_block_tail_insn
= XEXP (link
, 0);
1975 /* If we have reached the end of a libcall block, then close the
1977 if (deps
->libcall_block_tail_insn
== insn
)
1978 deps
->libcall_block_tail_insn
= 0;
1982 if (current_sched_info
->use_cselib
)
1991 /* The following function adds forward dependence (FROM, TO) with
1992 given DEP_TYPE. The forward dependence should be not exist before. */
1995 add_forw_dep (dep_link_t link
)
1997 dep_t dep
= DEP_LINK_DEP (link
);
1998 rtx to
= DEP_CON (dep
);
1999 rtx from
= DEP_PRO (dep
);
2001 #ifdef ENABLE_CHECKING
2002 /* If add_dependence is working properly there should never
2003 be notes, deleted insns or duplicates in the backward
2004 links. Thus we need not check for them here.
2006 However, if we have enabled checking we might as well go
2007 ahead and verify that add_dependence worked properly. */
2008 gcc_assert (INSN_P (from
));
2009 gcc_assert (!INSN_DELETED_P (from
));
2010 if (true_dependency_cache
)
2012 gcc_assert (!bitmap_bit_p (&forward_dependency_cache
[INSN_LUID (from
)],
2014 bitmap_set_bit (&forward_dependency_cache
[INSN_LUID (from
)],
2018 gcc_assert (find_link_by_con_in_deps_list (INSN_FORW_DEPS (from
), to
)
2022 add_to_deps_list (DEP_NODE_FORW (DEP_LINK_NODE (link
)),
2023 INSN_FORW_DEPS (from
));
2025 INSN_DEP_COUNT (to
) += 1;
2028 /* Examine insns in the range [ HEAD, TAIL ] and Use the backward
2029 dependences from INSN_BACK_DEPS list to build forward dependences in
2033 compute_forward_dependences (rtx head
, rtx tail
)
2038 next_tail
= NEXT_INSN (tail
);
2039 for (insn
= head
; insn
!= next_tail
; insn
= NEXT_INSN (insn
))
2043 if (! INSN_P (insn
))
2046 if (current_sched_info
->flags
& DO_SPECULATION
)
2048 /* We will add links, preserving order, from INSN_BACK_DEPS to
2050 dep_link_t
new = NULL
;
2052 link
= DEPS_LIST_FIRST (INSN_BACK_DEPS (insn
));
2054 while (link
!= NULL
)
2056 dep_link_t next
= DEP_LINK_NEXT (link
);
2058 detach_dep_link (link
);
2059 adjust_add_sorted_back_dep (insn
, link
, &new);
2064 /* Attach NEW to be the list of backward dependencies. */
2067 DEP_LINK_PREV_NEXTP (new)
2068 = &DEPS_LIST_FIRST (INSN_BACK_DEPS (insn
));
2070 DEPS_LIST_FIRST (INSN_BACK_DEPS (insn
)) = new;
2074 FOR_EACH_DEP_LINK (link
, INSN_BACK_DEPS (insn
))
2075 add_forw_dep (link
);
2079 /* Initialize variables for region data dependence analysis.
2080 n_bbs is the number of region blocks. */
2083 init_deps (struct deps
*deps
)
2085 int max_reg
= (reload_completed
? FIRST_PSEUDO_REGISTER
: max_reg_num ());
2087 deps
->max_reg
= max_reg
;
2088 deps
->reg_last
= XCNEWVEC (struct deps_reg
, max_reg
);
2089 INIT_REG_SET (&deps
->reg_last_in_use
);
2090 INIT_REG_SET (&deps
->reg_conditional_sets
);
2092 deps
->pending_read_insns
= 0;
2093 deps
->pending_read_mems
= 0;
2094 deps
->pending_write_insns
= 0;
2095 deps
->pending_write_mems
= 0;
2096 deps
->pending_read_list_length
= 0;
2097 deps
->pending_write_list_length
= 0;
2098 deps
->pending_flush_length
= 0;
2099 deps
->last_pending_memory_flush
= 0;
2100 deps
->last_function_call
= 0;
2101 deps
->sched_before_next_call
= 0;
2102 deps
->in_post_call_group_p
= not_post_call
;
2103 deps
->libcall_block_tail_insn
= 0;
2106 /* Free insn lists found in DEPS. */
2109 free_deps (struct deps
*deps
)
2112 reg_set_iterator rsi
;
2114 free_INSN_LIST_list (&deps
->pending_read_insns
);
2115 free_EXPR_LIST_list (&deps
->pending_read_mems
);
2116 free_INSN_LIST_list (&deps
->pending_write_insns
);
2117 free_EXPR_LIST_list (&deps
->pending_write_mems
);
2118 free_INSN_LIST_list (&deps
->last_pending_memory_flush
);
2120 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
2121 times. For a testcase with 42000 regs and 8000 small basic blocks,
2122 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
2123 EXECUTE_IF_SET_IN_REG_SET (&deps
->reg_last_in_use
, 0, i
, rsi
)
2125 struct deps_reg
*reg_last
= &deps
->reg_last
[i
];
2127 free_INSN_LIST_list (®_last
->uses
);
2129 free_INSN_LIST_list (®_last
->sets
);
2130 if (reg_last
->clobbers
)
2131 free_INSN_LIST_list (®_last
->clobbers
);
2133 CLEAR_REG_SET (&deps
->reg_last_in_use
);
2134 CLEAR_REG_SET (&deps
->reg_conditional_sets
);
2136 free (deps
->reg_last
);
2139 /* If it is profitable to use them, initialize caches for tracking
2140 dependency information. LUID is the number of insns to be scheduled,
2141 it is used in the estimate of profitability. */
2144 init_dependency_caches (int luid
)
2146 /* ?!? We could save some memory by computing a per-region luid mapping
2147 which could reduce both the number of vectors in the cache and the size
2148 of each vector. Instead we just avoid the cache entirely unless the
2149 average number of instructions in a basic block is very high. See
2150 the comment before the declaration of true_dependency_cache for
2151 what we consider "very high". */
2152 if (luid
/ n_basic_blocks
> 100 * 5)
2155 extend_dependency_caches (luid
, true);
2158 /* Lifetime of this obstack is whole function scheduling (not single region
2159 scheduling) because some dependencies can be manually generated for
2160 outside regions. See dont_calc_deps in sched-{rgn, ebb}.c .
2162 Possible solution would be to have two obstacks:
2163 * the big one for regular dependencies with region scheduling lifetime,
2164 * and the small one for manually generated dependencies with function
2165 scheduling lifetime. */
2166 gcc_obstack_init (&deps_obstack
);
2169 /* Create or extend (depending on CREATE_P) dependency caches to
2172 extend_dependency_caches (int n
, bool create_p
)
2174 if (create_p
|| true_dependency_cache
)
2176 int i
, luid
= cache_size
+ n
;
2178 true_dependency_cache
= XRESIZEVEC (bitmap_head
, true_dependency_cache
,
2180 output_dependency_cache
= XRESIZEVEC (bitmap_head
,
2181 output_dependency_cache
, luid
);
2182 anti_dependency_cache
= XRESIZEVEC (bitmap_head
, anti_dependency_cache
,
2184 #ifdef ENABLE_CHECKING
2185 forward_dependency_cache
= XRESIZEVEC (bitmap_head
,
2186 forward_dependency_cache
, luid
);
2188 if (current_sched_info
->flags
& DO_SPECULATION
)
2189 spec_dependency_cache
= XRESIZEVEC (bitmap_head
, spec_dependency_cache
,
2192 for (i
= cache_size
; i
< luid
; i
++)
2194 bitmap_initialize (&true_dependency_cache
[i
], 0);
2195 bitmap_initialize (&output_dependency_cache
[i
], 0);
2196 bitmap_initialize (&anti_dependency_cache
[i
], 0);
2197 #ifdef ENABLE_CHECKING
2198 bitmap_initialize (&forward_dependency_cache
[i
], 0);
2200 if (current_sched_info
->flags
& DO_SPECULATION
)
2201 bitmap_initialize (&spec_dependency_cache
[i
], 0);
2207 /* Free the caches allocated in init_dependency_caches. */
2210 free_dependency_caches (void)
2212 obstack_free (&deps_obstack
, NULL
);
2214 if (true_dependency_cache
)
2218 for (i
= 0; i
< cache_size
; i
++)
2220 bitmap_clear (&true_dependency_cache
[i
]);
2221 bitmap_clear (&output_dependency_cache
[i
]);
2222 bitmap_clear (&anti_dependency_cache
[i
]);
2223 #ifdef ENABLE_CHECKING
2224 bitmap_clear (&forward_dependency_cache
[i
]);
2226 if (current_sched_info
->flags
& DO_SPECULATION
)
2227 bitmap_clear (&spec_dependency_cache
[i
]);
2229 free (true_dependency_cache
);
2230 true_dependency_cache
= NULL
;
2231 free (output_dependency_cache
);
2232 output_dependency_cache
= NULL
;
2233 free (anti_dependency_cache
);
2234 anti_dependency_cache
= NULL
;
2235 #ifdef ENABLE_CHECKING
2236 free (forward_dependency_cache
);
2237 forward_dependency_cache
= NULL
;
2239 if (current_sched_info
->flags
& DO_SPECULATION
)
2241 free (spec_dependency_cache
);
2242 spec_dependency_cache
= NULL
;
2247 /* Initialize some global variables needed by the dependency analysis
2251 init_deps_global (void)
2253 reg_pending_sets
= ALLOC_REG_SET (®_obstack
);
2254 reg_pending_clobbers
= ALLOC_REG_SET (®_obstack
);
2255 reg_pending_uses
= ALLOC_REG_SET (®_obstack
);
2256 reg_pending_barrier
= NOT_A_BARRIER
;
2259 /* Free everything used by the dependency analysis code. */
2262 finish_deps_global (void)
2264 FREE_REG_SET (reg_pending_sets
);
2265 FREE_REG_SET (reg_pending_clobbers
);
2266 FREE_REG_SET (reg_pending_uses
);
2269 /* Insert LINK into the dependence chain pointed to by LINKP and
2270 maintain the sort order. */
2272 adjust_add_sorted_back_dep (rtx insn
, dep_link_t link
, dep_link_t
*linkp
)
2274 gcc_assert (current_sched_info
->flags
& DO_SPECULATION
);
2276 /* If the insn cannot move speculatively, but the link is speculative,
2277 make it hard dependence. */
2278 if (HAS_INTERNAL_DEP (insn
)
2279 && (DEP_LINK_STATUS (link
) & SPECULATIVE
))
2281 DEP_LINK_STATUS (link
) &= ~SPECULATIVE
;
2283 if (true_dependency_cache
)
2284 bitmap_clear_bit (&spec_dependency_cache
[INSN_LUID (insn
)],
2285 INSN_LUID (DEP_LINK_PRO (link
)));
2288 /* Non-speculative links go at the head of deps_list, followed by
2289 speculative links. */
2290 if (DEP_LINK_STATUS (link
) & SPECULATIVE
)
2291 while (*linkp
&& !(DEP_LINK_STATUS (*linkp
) & SPECULATIVE
))
2292 linkp
= &DEP_LINK_NEXT (*linkp
);
2294 attach_dep_link (link
, linkp
);
2297 gcc_assert (deps_list_consistent_p (INSN_BACK_DEPS (insn
)));
2300 /* Move the dependence pointed to by LINKP to the back dependencies
2301 of INSN, and also add this dependence to the forward ones. All dep_links,
2302 except one pointed to by LINKP, must be sorted. */
2304 adjust_back_add_forw_dep (rtx insn
, dep_link_t
*linkp
)
2308 gcc_assert (current_sched_info
->flags
& DO_SPECULATION
);
2311 detach_dep_link (link
);
2313 adjust_add_sorted_back_dep (insn
, link
,
2314 &DEPS_LIST_FIRST (INSN_BACK_DEPS (insn
)));
2315 add_forw_dep (link
);
2318 /* Remove forward dependence described by L. */
2320 delete_forw_dep (dep_link_t l
)
2322 gcc_assert (current_sched_info
->flags
& DO_SPECULATION
);
2324 #ifdef ENABLE_CHECKING
2325 if (true_dependency_cache
)
2326 bitmap_clear_bit (&forward_dependency_cache
[INSN_LUID (DEP_LINK_PRO (l
))],
2327 INSN_LUID (DEP_LINK_CON (l
)));
2330 detach_dep_link (l
);
2332 INSN_DEP_COUNT (DEP_LINK_CON (l
))--;
2335 /* Estimate the weakness of dependence between MEM1 and MEM2. */
2337 estimate_dep_weak (rtx mem1
, rtx mem2
)
2342 /* MEMs are the same - don't speculate. */
2343 return MIN_DEP_WEAK
;
2345 r1
= XEXP (mem1
, 0);
2346 r2
= XEXP (mem2
, 0);
2349 || (REG_P (r1
) && REG_P (r2
)
2350 && REGNO (r1
) == REGNO (r2
)))
2351 /* Again, MEMs are the same. */
2352 return MIN_DEP_WEAK
;
2353 else if ((REG_P (r1
) && !REG_P (r2
))
2354 || (!REG_P (r1
) && REG_P (r2
)))
2355 /* Different addressing modes - reason to be more speculative,
2357 return NO_DEP_WEAK
- (NO_DEP_WEAK
- UNCERTAIN_DEP_WEAK
) / 2;
2359 /* We can't say anything about the dependence. */
2360 return UNCERTAIN_DEP_WEAK
;
2363 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
2364 This function can handle same INSN and ELEM (INSN == ELEM).
2365 It is a convenience wrapper. */
2367 add_dependence (rtx insn
, rtx elem
, enum reg_note dep_type
)
2371 if (dep_type
== REG_DEP_TRUE
)
2373 else if (dep_type
== REG_DEP_OUTPUT
)
2375 else if (dep_type
== REG_DEP_ANTI
)
2380 maybe_add_or_update_back_dep_1 (insn
, elem
, dep_type
, ds
, 0, 0, 0);
2383 /* Add or update backward dependence between INSN and ELEM
2384 with given type DEP_TYPE and dep_status DS.
2385 This function is a convenience wrapper. */
2386 enum DEPS_ADJUST_RESULT
2387 add_or_update_back_dep (rtx insn
, rtx elem
, enum reg_note dep_type
, ds_t ds
)
2389 return add_or_update_back_dep_1 (insn
, elem
, dep_type
, ds
, 0, 0, 0);
2392 /* Add or update both backward and forward dependencies between INSN and ELEM
2393 with given type DEP_TYPE and dep_status DS. */
2395 add_or_update_back_forw_dep (rtx insn
, rtx elem
, enum reg_note dep_type
,
2398 enum DEPS_ADJUST_RESULT res
;
2401 res
= add_or_update_back_dep_1 (insn
, elem
, dep_type
, ds
, 0, 0, &linkp
);
2403 if (res
== DEP_CHANGED
|| res
== DEP_CREATED
)
2405 if (res
== DEP_CHANGED
)
2406 delete_forw_dep (DEP_NODE_FORW (DEP_LINK_NODE (*linkp
)));
2407 else if (res
== DEP_CREATED
)
2408 linkp
= &DEPS_LIST_FIRST (INSN_BACK_DEPS (insn
));
2410 adjust_back_add_forw_dep (insn
, linkp
);
2414 /* Add both backward and forward dependencies between INSN and ELEM
2415 with given type DEP_TYPE and dep_status DS. */
2417 add_back_forw_dep (rtx insn
, rtx elem
, enum reg_note dep_type
, ds_t ds
)
2419 add_back_dep (insn
, elem
, dep_type
, ds
);
2420 adjust_back_add_forw_dep (insn
, &DEPS_LIST_FIRST (INSN_BACK_DEPS (insn
)));
2423 gcc_assert (deps_list_consistent_p (INSN_BACK_DEPS (insn
)));
2426 /* Remove a dependency referred to by L. */
2428 delete_back_forw_dep (dep_link_t l
)
2430 dep_node_t n
= DEP_LINK_NODE (l
);
2432 gcc_assert (current_sched_info
->flags
& DO_SPECULATION
);
2434 if (true_dependency_cache
!= NULL
)
2436 dep_t dep
= DEP_NODE_DEP (n
);
2437 int elem_luid
= INSN_LUID (DEP_PRO (dep
));
2438 int insn_luid
= INSN_LUID (DEP_CON (dep
));
2440 bitmap_clear_bit (&true_dependency_cache
[insn_luid
], elem_luid
);
2441 bitmap_clear_bit (&anti_dependency_cache
[insn_luid
], elem_luid
);
2442 bitmap_clear_bit (&output_dependency_cache
[insn_luid
], elem_luid
);
2443 bitmap_clear_bit (&spec_dependency_cache
[insn_luid
], elem_luid
);
2446 delete_forw_dep (DEP_NODE_FORW (n
));
2447 detach_dep_link (DEP_NODE_BACK (n
));
2450 /* Return weakness of speculative type TYPE in the dep_status DS. */
2452 get_dep_weak (ds_t ds
, ds_t type
)
2457 case BEGIN_DATA
: ds
>>= BEGIN_DATA_BITS_OFFSET
; break;
2458 case BE_IN_DATA
: ds
>>= BE_IN_DATA_BITS_OFFSET
; break;
2459 case BEGIN_CONTROL
: ds
>>= BEGIN_CONTROL_BITS_OFFSET
; break;
2460 case BE_IN_CONTROL
: ds
>>= BE_IN_CONTROL_BITS_OFFSET
; break;
2461 default: gcc_unreachable ();
2464 gcc_assert (MIN_DEP_WEAK
<= ds
&& ds
<= MAX_DEP_WEAK
);
2468 /* Return the dep_status, which has the same parameters as DS, except for
2469 speculative type TYPE, that will have weakness DW. */
2471 set_dep_weak (ds_t ds
, ds_t type
, dw_t dw
)
2473 gcc_assert (MIN_DEP_WEAK
<= dw
&& dw
<= MAX_DEP_WEAK
);
2478 case BEGIN_DATA
: ds
|= ((ds_t
) dw
) << BEGIN_DATA_BITS_OFFSET
; break;
2479 case BE_IN_DATA
: ds
|= ((ds_t
) dw
) << BE_IN_DATA_BITS_OFFSET
; break;
2480 case BEGIN_CONTROL
: ds
|= ((ds_t
) dw
) << BEGIN_CONTROL_BITS_OFFSET
; break;
2481 case BE_IN_CONTROL
: ds
|= ((ds_t
) dw
) << BE_IN_CONTROL_BITS_OFFSET
; break;
2482 default: gcc_unreachable ();
2487 /* Return the join of two dep_statuses DS1 and DS2. */
2489 ds_merge (ds_t ds1
, ds_t ds2
)
2493 gcc_assert ((ds1
& SPECULATIVE
) && (ds2
& SPECULATIVE
));
2495 ds
= (ds1
& DEP_TYPES
) | (ds2
& DEP_TYPES
);
2497 t
= FIRST_SPEC_TYPE
;
2500 if ((ds1
& t
) && !(ds2
& t
))
2502 else if (!(ds1
& t
) && (ds2
& t
))
2504 else if ((ds1
& t
) && (ds2
& t
))
2508 dw
= ((ds_t
) get_dep_weak (ds1
, t
)) * ((ds_t
) get_dep_weak (ds2
, t
));
2510 if (dw
< MIN_DEP_WEAK
)
2513 ds
= set_dep_weak (ds
, t
, (dw_t
) dw
);
2516 if (t
== LAST_SPEC_TYPE
)
2518 t
<<= SPEC_TYPE_SHIFT
;
2525 #ifdef INSN_SCHEDULING
2526 #ifdef ENABLE_CHECKING
2527 /* Verify that dependence type and status are consistent.
2528 If RELAXED_P is true, then skip dep_weakness checks. */
2530 check_dep_status (enum reg_note dt
, ds_t ds
, bool relaxed_p
)
2532 /* Check that dependence type contains the same bits as the status. */
2533 if (dt
== REG_DEP_TRUE
)
2534 gcc_assert (ds
& DEP_TRUE
);
2535 else if (dt
== REG_DEP_OUTPUT
)
2536 gcc_assert ((ds
& DEP_OUTPUT
)
2537 && !(ds
& DEP_TRUE
));
2539 gcc_assert ((dt
== REG_DEP_ANTI
)
2541 && !(ds
& (DEP_OUTPUT
| DEP_TRUE
)));
2543 /* HARD_DEP can not appear in dep_status of a link. */
2544 gcc_assert (!(ds
& HARD_DEP
));
2546 /* Check that dependence status is set correctly when speculation is not
2548 if (!(current_sched_info
->flags
& DO_SPECULATION
))
2549 gcc_assert (!(ds
& SPECULATIVE
));
2550 else if (ds
& SPECULATIVE
)
2554 ds_t type
= FIRST_SPEC_TYPE
;
2556 /* Check that dependence weakness is in proper range. */
2560 get_dep_weak (ds
, type
);
2562 if (type
== LAST_SPEC_TYPE
)
2564 type
<<= SPEC_TYPE_SHIFT
;
2569 if (ds
& BEGIN_SPEC
)
2571 /* Only true dependence can be data speculative. */
2572 if (ds
& BEGIN_DATA
)
2573 gcc_assert (ds
& DEP_TRUE
);
2575 /* Control dependencies in the insn scheduler are represented by
2576 anti-dependencies, therefore only anti dependence can be
2577 control speculative. */
2578 if (ds
& BEGIN_CONTROL
)
2579 gcc_assert (ds
& DEP_ANTI
);
2583 /* Subsequent speculations should resolve true dependencies. */
2584 gcc_assert ((ds
& DEP_TYPES
) == DEP_TRUE
);
2587 /* Check that true and anti dependencies can't have other speculative
2590 gcc_assert (ds
& (BEGIN_DATA
| BE_IN_SPEC
));
2591 /* An output dependence can't be speculative at all. */
2592 gcc_assert (!(ds
& DEP_OUTPUT
));
2594 gcc_assert (ds
& BEGIN_CONTROL
);