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 INSN_SCHEDULING
47 #ifdef ENABLE_CHECKING
53 /* Return the major type present in the DS. */
61 return REG_DEP_OUTPUT;
63 gcc_assert (ds & DEP_ANTI);
68 /* Return equivalent dep_status. */
70 dk_to_ds (enum reg_note dk)
81 gcc_assert (dk == REG_DEP_ANTI);
86 /* Functions to operate with dependence information container - dep_t. */
88 /* Init DEP with the arguments. */
90 init_dep_1 (dep_t dep, rtx pro, rtx con, enum reg_note type, ds_t ds)
94 DEP_TYPE (dep) = type;
95 DEP_STATUS (dep) = ds;
98 /* Init DEP with the arguments.
99 While most of the scheduler (including targets) only need the major type
100 of the dependency, it is convenient to hide full dep_status from them. */
102 init_dep (dep_t dep, rtx pro, rtx con, enum reg_note kind)
106 if ((current_sched_info->flags & USE_DEPS_LIST))
107 ds = dk_to_ds (kind);
111 init_dep_1 (dep, pro, con, kind, ds);
114 /* Make a copy of FROM in TO. */
116 copy_dep (dep_t to, dep_t from)
118 memcpy (to, from, sizeof (*to));
121 static void dump_ds (FILE *, ds_t);
123 /* Define flags for dump_dep (). */
125 /* Dump producer of the dependence. */
126 #define DUMP_DEP_PRO (2)
128 /* Dump consumer of the dependence. */
129 #define DUMP_DEP_CON (4)
131 /* Dump type of the dependence. */
132 #define DUMP_DEP_TYPE (8)
134 /* Dump status of the dependence. */
135 #define DUMP_DEP_STATUS (16)
137 /* Dump all information about the dependence. */
138 #define DUMP_DEP_ALL (DUMP_DEP_PRO | DUMP_DEP_CON | DUMP_DEP_TYPE \
142 FLAGS is a bit mask specifying what information about DEP needs
144 If FLAGS has the very first bit set, then dump all information about DEP
145 and propagate this bit into the callee dump functions. */
147 dump_dep (FILE *dump, dep_t dep, int flags)
150 flags |= DUMP_DEP_ALL;
154 if (flags & DUMP_DEP_PRO)
155 fprintf (dump, "%d; ", INSN_UID (DEP_PRO (dep)));
157 if (flags & DUMP_DEP_CON)
158 fprintf (dump, "%d; ", INSN_UID (DEP_CON (dep)));
160 if (flags & DUMP_DEP_TYPE)
163 enum reg_note type = DEP_TYPE (dep);
184 fprintf (dump, "%c; ", t);
187 if (flags & DUMP_DEP_STATUS)
189 if (current_sched_info->flags & USE_DEPS_LIST)
190 dump_ds (dump, DEP_STATUS (dep));
196 /* Default flags for dump_dep (). */
197 static int dump_dep_flags = (DUMP_DEP_PRO | DUMP_DEP_CON);
199 /* Dump all fields of DEP to STDERR. */
201 sd_debug_dep (dep_t dep)
203 dump_dep (stderr, dep, 1);
204 fprintf (stderr, "\n");
207 /* Functions to operate with a single link from the dependencies lists -
210 /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
213 attach_dep_link (dep_link_t l, dep_link_t *prev_nextp)
215 dep_link_t next = *prev_nextp;
217 gcc_assert (DEP_LINK_PREV_NEXTP (l) == NULL
218 && DEP_LINK_NEXT (l) == NULL);
220 /* Init node being inserted. */
221 DEP_LINK_PREV_NEXTP (l) = prev_nextp;
222 DEP_LINK_NEXT (l) = next;
227 gcc_assert (DEP_LINK_PREV_NEXTP (next) == prev_nextp);
229 DEP_LINK_PREV_NEXTP (next) = &DEP_LINK_NEXT (l);
236 /* Add dep_link LINK to deps_list L. */
238 add_to_deps_list (dep_link_t link, deps_list_t l)
240 attach_dep_link (link, &DEPS_LIST_FIRST (l));
242 ++DEPS_LIST_N_LINKS (l);
245 /* Detach dep_link L from the list. */
247 detach_dep_link (dep_link_t l)
249 dep_link_t *prev_nextp = DEP_LINK_PREV_NEXTP (l);
250 dep_link_t next = DEP_LINK_NEXT (l);
255 DEP_LINK_PREV_NEXTP (next) = prev_nextp;
257 DEP_LINK_PREV_NEXTP (l) = NULL;
258 DEP_LINK_NEXT (l) = NULL;
261 /* Remove link LINK from list LIST. */
263 remove_from_deps_list (dep_link_t link, deps_list_t list)
265 detach_dep_link (link);
267 --DEPS_LIST_N_LINKS (list);
270 /* Move link LINK from list FROM to list TO. */
272 move_dep_link (dep_link_t link, deps_list_t from, deps_list_t to)
274 remove_from_deps_list (link, from);
275 add_to_deps_list (link, to);
278 /* Return true of LINK is not attached to any list. */
280 dep_link_is_detached_p (dep_link_t link)
282 return DEP_LINK_PREV_NEXTP (link) == NULL;
285 /* Pool to hold all dependency nodes (dep_node_t). */
286 static alloc_pool dn_pool;
288 /* Number of dep_nodes out there. */
289 static int dn_pool_diff = 0;
291 /* Create a dep_node. */
293 create_dep_node (void)
295 dep_node_t n = (dep_node_t) pool_alloc (dn_pool);
296 dep_link_t back = DEP_NODE_BACK (n);
297 dep_link_t forw = DEP_NODE_FORW (n);
299 DEP_LINK_NODE (back) = n;
300 DEP_LINK_NEXT (back) = NULL;
301 DEP_LINK_PREV_NEXTP (back) = NULL;
303 DEP_LINK_NODE (forw) = n;
304 DEP_LINK_NEXT (forw) = NULL;
305 DEP_LINK_PREV_NEXTP (forw) = NULL;
312 /* Delete dep_node N. N must not be connected to any deps_list. */
314 delete_dep_node (dep_node_t n)
316 gcc_assert (dep_link_is_detached_p (DEP_NODE_BACK (n))
317 && dep_link_is_detached_p (DEP_NODE_FORW (n)));
321 pool_free (dn_pool, n);
324 /* Pool to hold dependencies lists (deps_list_t). */
325 static alloc_pool dl_pool;
327 /* Number of deps_lists out there. */
328 static int dl_pool_diff = 0;
330 /* Functions to operate with dependences lists - deps_list_t. */
332 /* Return true if list L is empty. */
334 deps_list_empty_p (deps_list_t l)
336 return DEPS_LIST_N_LINKS (l) == 0;
339 /* Create a new deps_list. */
341 create_deps_list (void)
343 deps_list_t l = (deps_list_t) pool_alloc (dl_pool);
345 DEPS_LIST_FIRST (l) = NULL;
346 DEPS_LIST_N_LINKS (l) = 0;
352 /* Free deps_list L. */
354 free_deps_list (deps_list_t l)
356 gcc_assert (deps_list_empty_p (l));
360 pool_free (dl_pool, l);
363 /* Return true if there is no dep_nodes and deps_lists out there.
364 After the region is scheduled all the dependency nodes and lists
365 should [generally] be returned to pool. */
367 deps_pools_are_empty_p (void)
369 return dn_pool_diff == 0 && dl_pool_diff == 0;
372 /* Remove all elements from L. */
374 clear_deps_list (deps_list_t l)
378 dep_link_t link = DEPS_LIST_FIRST (l);
383 remove_from_deps_list (link, l);
388 static regset reg_pending_sets;
389 static regset reg_pending_clobbers;
390 static regset reg_pending_uses;
392 /* The following enumeration values tell us what dependencies we
393 should use to implement the barrier. We use true-dependencies for
394 TRUE_BARRIER and anti-dependencies for MOVE_BARRIER. */
395 enum reg_pending_barrier_mode
402 static enum reg_pending_barrier_mode reg_pending_barrier;
404 /* To speed up the test for duplicate dependency links we keep a
405 record of dependencies created by add_dependence when the average
406 number of instructions in a basic block is very large.
408 Studies have shown that there is typically around 5 instructions between
409 branches for typical C code. So we can make a guess that the average
410 basic block is approximately 5 instructions long; we will choose 100X
411 the average size as a very large basic block.
413 Each insn has associated bitmaps for its dependencies. Each bitmap
414 has enough entries to represent a dependency on any other insn in
415 the insn chain. All bitmap for true dependencies cache is
416 allocated then the rest two ones are also allocated. */
417 static bitmap_head *true_dependency_cache;
418 static bitmap_head *output_dependency_cache;
419 static bitmap_head *anti_dependency_cache;
420 static bitmap_head *spec_dependency_cache;
421 static int cache_size;
423 static int deps_may_trap_p (const_rtx);
424 static void add_dependence_list (rtx, rtx, int, enum reg_note);
425 static void add_dependence_list_and_free (rtx, rtx *, int, enum reg_note);
426 static void delete_all_dependences (rtx);
427 static void fixup_sched_groups (rtx);
429 static void flush_pending_lists (struct deps *, rtx, int, int);
430 static void sched_analyze_1 (struct deps *, rtx, rtx);
431 static void sched_analyze_2 (struct deps *, rtx, rtx);
432 static void sched_analyze_insn (struct deps *, rtx, rtx);
434 static rtx sched_get_condition (const_rtx);
435 static int conditions_mutex_p (const_rtx, const_rtx);
437 static enum DEPS_ADJUST_RESULT maybe_add_or_update_dep_1 (dep_t, bool,
439 static enum DEPS_ADJUST_RESULT add_or_update_dep_1 (dep_t, bool, rtx, rtx);
441 static dw_t estimate_dep_weak (rtx, rtx);
442 #ifdef ENABLE_CHECKING
443 static void check_dep (dep_t, bool);
446 /* Return nonzero if a load of the memory reference MEM can cause a trap. */
449 deps_may_trap_p (const_rtx mem)
451 const_rtx addr = XEXP (mem, 0);
453 if (REG_P (addr) && REGNO (addr) >= FIRST_PSEUDO_REGISTER)
455 const_rtx t = get_reg_known_value (REGNO (addr));
459 return rtx_addr_can_trap_p (addr);
462 /* Find the condition under which INSN is executed. */
465 sched_get_condition (const_rtx insn)
467 rtx pat = PATTERN (insn);
473 if (GET_CODE (pat) == COND_EXEC)
474 return COND_EXEC_TEST (pat);
476 if (!any_condjump_p (insn) || !onlyjump_p (insn))
479 src = SET_SRC (pc_set (insn));
481 if (XEXP (src, 2) == pc_rtx)
482 return XEXP (src, 0);
483 else if (XEXP (src, 1) == pc_rtx)
485 rtx cond = XEXP (src, 0);
486 enum rtx_code revcode = reversed_comparison_code (cond, insn);
488 if (revcode == UNKNOWN)
490 return gen_rtx_fmt_ee (revcode, GET_MODE (cond), XEXP (cond, 0),
498 /* Return nonzero if conditions COND1 and COND2 can never be both true. */
501 conditions_mutex_p (const_rtx cond1, const_rtx cond2)
503 if (COMPARISON_P (cond1)
504 && COMPARISON_P (cond2)
505 && GET_CODE (cond1) == reversed_comparison_code (cond2, NULL)
506 && XEXP (cond1, 0) == XEXP (cond2, 0)
507 && XEXP (cond1, 1) == XEXP (cond2, 1))
512 /* Return true if insn1 and insn2 can never depend on one another because
513 the conditions under which they are executed are mutually exclusive. */
515 sched_insns_conditions_mutex_p (const_rtx insn1, const_rtx insn2)
519 /* df doesn't handle conditional lifetimes entirely correctly;
520 calls mess up the conditional lifetimes. */
521 if (!CALL_P (insn1) && !CALL_P (insn2))
523 cond1 = sched_get_condition (insn1);
524 cond2 = sched_get_condition (insn2);
526 && conditions_mutex_p (cond1, cond2)
527 /* Make sure first instruction doesn't affect condition of second
528 instruction if switched. */
529 && !modified_in_p (cond1, insn2)
530 /* Make sure second instruction doesn't affect condition of first
531 instruction if switched. */
532 && !modified_in_p (cond2, insn1))
539 /* Return true if INSN can potentially be speculated with type DS. */
541 sched_insn_is_legitimate_for_speculation_p (const_rtx insn, ds_t ds)
543 if (HAS_INTERNAL_DEP (insn))
546 if (!NONJUMP_INSN_P (insn))
549 if (SCHED_GROUP_P (insn))
552 if (IS_SPECULATION_CHECK_P (insn))
555 if (side_effects_p (PATTERN (insn)))
559 /* The following instructions, which depend on a speculatively scheduled
560 instruction, cannot be speculatively scheduled along. */
562 if (may_trap_p (PATTERN (insn)))
563 /* If instruction might trap, it cannot be speculatively scheduled.
564 For control speculation it's obvious why and for data speculation
565 it's because the insn might get wrong input if speculation
566 wasn't successful. */
569 if ((ds & BE_IN_DATA)
570 && sched_get_condition (insn) != NULL_RTX)
571 /* If this is a predicated instruction, then it cannot be
572 speculatively scheduled. See PR35659. */
579 /* Initialize LIST_PTR to point to one of the lists present in TYPES_PTR,
580 initialize RESOLVED_P_PTR with true if that list consists of resolved deps,
581 and remove the type of returned [through LIST_PTR] list from TYPES_PTR.
582 This function is used to switch sd_iterator to the next list.
583 !!! For internal use only. Might consider moving it to sched-int.h. */
585 sd_next_list (const_rtx insn, sd_list_types_def *types_ptr,
586 deps_list_t *list_ptr, bool *resolved_p_ptr)
588 sd_list_types_def types = *types_ptr;
590 if (types & SD_LIST_HARD_BACK)
592 *list_ptr = INSN_HARD_BACK_DEPS (insn);
593 *resolved_p_ptr = false;
594 *types_ptr = types & ~SD_LIST_HARD_BACK;
596 else if (types & SD_LIST_SPEC_BACK)
598 *list_ptr = INSN_SPEC_BACK_DEPS (insn);
599 *resolved_p_ptr = false;
600 *types_ptr = types & ~SD_LIST_SPEC_BACK;
602 else if (types & SD_LIST_FORW)
604 *list_ptr = INSN_FORW_DEPS (insn);
605 *resolved_p_ptr = false;
606 *types_ptr = types & ~SD_LIST_FORW;
608 else if (types & SD_LIST_RES_BACK)
610 *list_ptr = INSN_RESOLVED_BACK_DEPS (insn);
611 *resolved_p_ptr = true;
612 *types_ptr = types & ~SD_LIST_RES_BACK;
614 else if (types & SD_LIST_RES_FORW)
616 *list_ptr = INSN_RESOLVED_FORW_DEPS (insn);
617 *resolved_p_ptr = true;
618 *types_ptr = types & ~SD_LIST_RES_FORW;
623 *resolved_p_ptr = false;
624 *types_ptr = SD_LIST_NONE;
628 /* Return the summary size of INSN's lists defined by LIST_TYPES. */
630 sd_lists_size (const_rtx insn, sd_list_types_def list_types)
634 while (list_types != SD_LIST_NONE)
639 sd_next_list (insn, &list_types, &list, &resolved_p);
640 size += DEPS_LIST_N_LINKS (list);
646 /* Return true if INSN's lists defined by LIST_TYPES are all empty. */
648 sd_lists_empty_p (const_rtx insn, sd_list_types_def list_types)
650 return sd_lists_size (insn, list_types) == 0;
653 /* Initialize data for INSN. */
655 sd_init_insn (rtx insn)
657 INSN_HARD_BACK_DEPS (insn) = create_deps_list ();
658 INSN_SPEC_BACK_DEPS (insn) = create_deps_list ();
659 INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list ();
660 INSN_FORW_DEPS (insn) = create_deps_list ();
661 INSN_RESOLVED_FORW_DEPS (insn) = create_deps_list ();
663 /* ??? It would be nice to allocate dependency caches here. */
666 /* Free data for INSN. */
668 sd_finish_insn (rtx insn)
670 /* ??? It would be nice to deallocate dependency caches here. */
672 free_deps_list (INSN_HARD_BACK_DEPS (insn));
673 INSN_HARD_BACK_DEPS (insn) = NULL;
675 free_deps_list (INSN_SPEC_BACK_DEPS (insn));
676 INSN_SPEC_BACK_DEPS (insn) = NULL;
678 free_deps_list (INSN_RESOLVED_BACK_DEPS (insn));
679 INSN_RESOLVED_BACK_DEPS (insn) = NULL;
681 free_deps_list (INSN_FORW_DEPS (insn));
682 INSN_FORW_DEPS (insn) = NULL;
684 free_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
685 INSN_RESOLVED_FORW_DEPS (insn) = NULL;
688 /* Find a dependency between producer PRO and consumer CON.
689 Search through resolved dependency lists if RESOLVED_P is true.
690 If no such dependency is found return NULL,
691 otherwise return the dependency and initialize SD_IT_PTR [if it is nonnull]
692 with an iterator pointing to it. */
694 sd_find_dep_between_no_cache (rtx pro, rtx con, bool resolved_p,
695 sd_iterator_def *sd_it_ptr)
697 sd_list_types_def pro_list_type;
698 sd_list_types_def con_list_type;
699 sd_iterator_def sd_it;
701 bool found_p = false;
705 pro_list_type = SD_LIST_RES_FORW;
706 con_list_type = SD_LIST_RES_BACK;
710 pro_list_type = SD_LIST_FORW;
711 con_list_type = SD_LIST_BACK;
714 /* Walk through either back list of INSN or forw list of ELEM
715 depending on which one is shorter. */
716 if (sd_lists_size (con, con_list_type) < sd_lists_size (pro, pro_list_type))
718 /* Find the dep_link with producer PRO in consumer's back_deps. */
719 FOR_EACH_DEP (con, con_list_type, sd_it, dep)
720 if (DEP_PRO (dep) == pro)
728 /* Find the dep_link with consumer CON in producer's forw_deps. */
729 FOR_EACH_DEP (pro, pro_list_type, sd_it, dep)
730 if (DEP_CON (dep) == con)
739 if (sd_it_ptr != NULL)
748 /* Find a dependency between producer PRO and consumer CON.
749 Use dependency [if available] to check if dependency is present at all.
750 Search through resolved dependency lists if RESOLVED_P is true.
751 If the dependency or NULL if none found. */
753 sd_find_dep_between (rtx pro, rtx con, bool resolved_p)
755 if (true_dependency_cache != NULL)
756 /* Avoiding the list walk below can cut compile times dramatically
759 int elem_luid = INSN_LUID (pro);
760 int insn_luid = INSN_LUID (con);
762 gcc_assert (output_dependency_cache != NULL
763 && anti_dependency_cache != NULL);
765 if (!bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid)
766 && !bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid)
767 && !bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
771 return sd_find_dep_between_no_cache (pro, con, resolved_p, NULL);
774 /* Add or update a dependence described by DEP.
775 MEM1 and MEM2, if non-null, correspond to memory locations in case of
778 The function returns a value indicating if an old entry has been changed
779 or a new entry has been added to insn's backward deps.
781 This function merely checks if producer and consumer is the same insn
782 and doesn't create a dep in this case. Actual manipulation of
783 dependence data structures is performed in add_or_update_dep_1. */
784 static enum DEPS_ADJUST_RESULT
785 maybe_add_or_update_dep_1 (dep_t dep, bool resolved_p, rtx mem1, rtx mem2)
787 rtx elem = DEP_PRO (dep);
788 rtx insn = DEP_CON (dep);
790 gcc_assert (INSN_P (insn) && INSN_P (elem));
792 /* Don't depend an insn on itself. */
795 if (current_sched_info->flags & DO_SPECULATION)
796 /* INSN has an internal dependence, which we can't overcome. */
797 HAS_INTERNAL_DEP (insn) = 1;
802 return add_or_update_dep_1 (dep, resolved_p, mem1, mem2);
805 /* Ask dependency caches what needs to be done for dependence DEP.
806 Return DEP_CREATED if new dependence should be created and there is no
807 need to try to find one searching the dependencies lists.
808 Return DEP_PRESENT if there already is a dependence described by DEP and
809 hence nothing is to be done.
810 Return DEP_CHANGED if there already is a dependence, but it should be
811 updated to incorporate additional information from DEP. */
812 static enum DEPS_ADJUST_RESULT
813 ask_dependency_caches (dep_t dep)
815 int elem_luid = INSN_LUID (DEP_PRO (dep));
816 int insn_luid = INSN_LUID (DEP_CON (dep));
818 gcc_assert (true_dependency_cache != NULL
819 && output_dependency_cache != NULL
820 && anti_dependency_cache != NULL);
822 if (!(current_sched_info->flags & USE_DEPS_LIST))
824 enum reg_note present_dep_type;
826 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
827 present_dep_type = REG_DEP_TRUE;
828 else if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
829 present_dep_type = REG_DEP_OUTPUT;
830 else if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
831 present_dep_type = REG_DEP_ANTI;
833 /* There is no existing dep so it should be created. */
836 if ((int) DEP_TYPE (dep) >= (int) present_dep_type)
837 /* DEP does not add anything to the existing dependence. */
842 ds_t present_dep_types = 0;
844 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
845 present_dep_types |= DEP_TRUE;
846 if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
847 present_dep_types |= DEP_OUTPUT;
848 if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
849 present_dep_types |= DEP_ANTI;
851 if (present_dep_types == 0)
852 /* There is no existing dep so it should be created. */
855 if (!(current_sched_info->flags & DO_SPECULATION)
856 || !bitmap_bit_p (&spec_dependency_cache[insn_luid], elem_luid))
858 if ((present_dep_types | (DEP_STATUS (dep) & DEP_TYPES))
859 == present_dep_types)
860 /* DEP does not add anything to the existing dependence. */
865 /* Only true dependencies can be data speculative and
866 only anti dependencies can be control speculative. */
867 gcc_assert ((present_dep_types & (DEP_TRUE | DEP_ANTI))
868 == present_dep_types);
870 /* if (DEP is SPECULATIVE) then
871 ..we should update DEP_STATUS
873 ..we should reset existing dep to non-speculative. */
880 /* Set dependency caches according to DEP. */
882 set_dependency_caches (dep_t dep)
884 int elem_luid = INSN_LUID (DEP_PRO (dep));
885 int insn_luid = INSN_LUID (DEP_CON (dep));
887 if (!(current_sched_info->flags & USE_DEPS_LIST))
889 switch (DEP_TYPE (dep))
892 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
896 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
900 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
909 ds_t ds = DEP_STATUS (dep);
912 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
914 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
916 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
918 if (ds & SPECULATIVE)
920 gcc_assert (current_sched_info->flags & DO_SPECULATION);
921 bitmap_set_bit (&spec_dependency_cache[insn_luid], elem_luid);
926 /* Type of dependence DEP have changed from OLD_TYPE. Update dependency
927 caches accordingly. */
929 update_dependency_caches (dep_t dep, enum reg_note old_type)
931 int elem_luid = INSN_LUID (DEP_PRO (dep));
932 int insn_luid = INSN_LUID (DEP_CON (dep));
934 /* Clear corresponding cache entry because type of the link
935 may have changed. Keep them if we use_deps_list. */
936 if (!(current_sched_info->flags & USE_DEPS_LIST))
941 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
945 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
953 set_dependency_caches (dep);
956 /* Convert a dependence pointed to by SD_IT to be non-speculative. */
958 change_spec_dep_to_hard (sd_iterator_def sd_it)
960 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
961 dep_link_t link = DEP_NODE_BACK (node);
962 dep_t dep = DEP_NODE_DEP (node);
963 rtx elem = DEP_PRO (dep);
964 rtx insn = DEP_CON (dep);
966 move_dep_link (link, INSN_SPEC_BACK_DEPS (insn), INSN_HARD_BACK_DEPS (insn));
968 DEP_STATUS (dep) &= ~SPECULATIVE;
970 if (true_dependency_cache != NULL)
971 /* Clear the cache entry. */
972 bitmap_clear_bit (&spec_dependency_cache[INSN_LUID (insn)],
976 /* Update DEP to incorporate information from NEW_DEP.
977 SD_IT points to DEP in case it should be moved to another list.
978 MEM1 and MEM2, if nonnull, correspond to memory locations in case if
979 data-speculative dependence should be updated. */
980 static enum DEPS_ADJUST_RESULT
981 update_dep (dep_t dep, dep_t new_dep,
982 sd_iterator_def sd_it ATTRIBUTE_UNUSED,
983 rtx mem1 ATTRIBUTE_UNUSED,
984 rtx mem2 ATTRIBUTE_UNUSED)
986 enum DEPS_ADJUST_RESULT res = DEP_PRESENT;
987 enum reg_note old_type = DEP_TYPE (dep);
989 /* If this is a more restrictive type of dependence than the
990 existing one, then change the existing dependence to this
992 if ((int) DEP_TYPE (new_dep) < (int) old_type)
994 DEP_TYPE (dep) = DEP_TYPE (new_dep);
998 if (current_sched_info->flags & USE_DEPS_LIST)
999 /* Update DEP_STATUS. */
1001 ds_t dep_status = DEP_STATUS (dep);
1002 ds_t ds = DEP_STATUS (new_dep);
1003 ds_t new_status = ds | dep_status;
1005 if (new_status & SPECULATIVE)
1006 /* Either existing dep or a dep we're adding or both are
1009 if (!(ds & SPECULATIVE)
1010 || !(dep_status & SPECULATIVE))
1011 /* The new dep can't be speculative. */
1013 new_status &= ~SPECULATIVE;
1015 if (dep_status & SPECULATIVE)
1016 /* The old dep was speculative, but now it
1018 change_spec_dep_to_hard (sd_it);
1022 /* Both are speculative. Merge probabilities. */
1027 dw = estimate_dep_weak (mem1, mem2);
1028 ds = set_dep_weak (ds, BEGIN_DATA, dw);
1031 new_status = ds_merge (dep_status, ds);
1037 if (dep_status != ds)
1039 DEP_STATUS (dep) = ds;
1044 if (true_dependency_cache != NULL
1045 && res == DEP_CHANGED)
1046 update_dependency_caches (dep, old_type);
1051 /* Add or update a dependence described by DEP.
1052 MEM1 and MEM2, if non-null, correspond to memory locations in case of
1055 The function returns a value indicating if an old entry has been changed
1056 or a new entry has been added to insn's backward deps or nothing has
1057 been updated at all. */
1058 static enum DEPS_ADJUST_RESULT
1059 add_or_update_dep_1 (dep_t new_dep, bool resolved_p,
1060 rtx mem1 ATTRIBUTE_UNUSED, rtx mem2 ATTRIBUTE_UNUSED)
1062 bool maybe_present_p = true;
1063 bool present_p = false;
1065 gcc_assert (INSN_P (DEP_PRO (new_dep)) && INSN_P (DEP_CON (new_dep))
1066 && DEP_PRO (new_dep) != DEP_CON (new_dep));
1068 #ifdef ENABLE_CHECKING
1069 check_dep (new_dep, mem1 != NULL);
1072 if (true_dependency_cache != NULL)
1074 switch (ask_dependency_caches (new_dep))
1080 maybe_present_p = true;
1085 maybe_present_p = false;
1095 /* Check that we don't already have this dependence. */
1096 if (maybe_present_p)
1099 sd_iterator_def sd_it;
1101 gcc_assert (true_dependency_cache == NULL || present_p);
1103 present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
1105 resolved_p, &sd_it);
1107 if (present_dep != NULL)
1108 /* We found an existing dependency between ELEM and INSN. */
1109 return update_dep (present_dep, new_dep, sd_it, mem1, mem2);
1111 /* We didn't find a dep, it shouldn't present in the cache. */
1112 gcc_assert (!present_p);
1115 /* Might want to check one level of transitivity to save conses.
1116 This check should be done in maybe_add_or_update_dep_1.
1117 Since we made it to add_or_update_dep_1, we must create
1118 (or update) a link. */
1120 if (mem1 != NULL_RTX)
1122 gcc_assert (current_sched_info->flags & DO_SPECULATION);
1123 DEP_STATUS (new_dep) = set_dep_weak (DEP_STATUS (new_dep), BEGIN_DATA,
1124 estimate_dep_weak (mem1, mem2));
1127 sd_add_dep (new_dep, resolved_p);
1132 /* Initialize BACK_LIST_PTR with consumer's backward list and
1133 FORW_LIST_PTR with producer's forward list. If RESOLVED_P is true
1134 initialize with lists that hold resolved deps. */
1136 get_back_and_forw_lists (dep_t dep, bool resolved_p,
1137 deps_list_t *back_list_ptr,
1138 deps_list_t *forw_list_ptr)
1140 rtx con = DEP_CON (dep);
1144 if ((current_sched_info->flags & DO_SPECULATION)
1145 && (DEP_STATUS (dep) & SPECULATIVE))
1146 *back_list_ptr = INSN_SPEC_BACK_DEPS (con);
1148 *back_list_ptr = INSN_HARD_BACK_DEPS (con);
1150 *forw_list_ptr = INSN_FORW_DEPS (DEP_PRO (dep));
1154 *back_list_ptr = INSN_RESOLVED_BACK_DEPS (con);
1155 *forw_list_ptr = INSN_RESOLVED_FORW_DEPS (DEP_PRO (dep));
1159 /* Add dependence described by DEP.
1160 If RESOLVED_P is true treat the dependence as a resolved one. */
1162 sd_add_dep (dep_t dep, bool resolved_p)
1164 dep_node_t n = create_dep_node ();
1165 deps_list_t con_back_deps;
1166 deps_list_t pro_forw_deps;
1167 rtx elem = DEP_PRO (dep);
1168 rtx insn = DEP_CON (dep);
1170 gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
1172 if ((current_sched_info->flags & DO_SPECULATION)
1173 && !sched_insn_is_legitimate_for_speculation_p (insn, DEP_STATUS (dep)))
1174 DEP_STATUS (dep) &= ~SPECULATIVE;
1176 copy_dep (DEP_NODE_DEP (n), dep);
1178 get_back_and_forw_lists (dep, resolved_p, &con_back_deps, &pro_forw_deps);
1180 add_to_deps_list (DEP_NODE_BACK (n), con_back_deps);
1182 #ifdef ENABLE_CHECKING
1183 check_dep (dep, false);
1186 add_to_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1188 /* If we are adding a dependency to INSN's LOG_LINKs, then note that
1189 in the bitmap caches of dependency information. */
1190 if (true_dependency_cache != NULL)
1191 set_dependency_caches (dep);
1194 /* Add or update backward dependence between INSN and ELEM
1195 with given type DEP_TYPE and dep_status DS.
1196 This function is a convenience wrapper. */
1197 enum DEPS_ADJUST_RESULT
1198 sd_add_or_update_dep (dep_t dep, bool resolved_p)
1200 return add_or_update_dep_1 (dep, resolved_p, NULL_RTX, NULL_RTX);
1203 /* Resolved dependence pointed to by SD_IT.
1204 SD_IT will advance to the next element. */
1206 sd_resolve_dep (sd_iterator_def sd_it)
1208 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1209 dep_t dep = DEP_NODE_DEP (node);
1210 rtx pro = DEP_PRO (dep);
1211 rtx con = DEP_CON (dep);
1213 if ((current_sched_info->flags & DO_SPECULATION)
1214 && (DEP_STATUS (dep) & SPECULATIVE))
1215 move_dep_link (DEP_NODE_BACK (node), INSN_SPEC_BACK_DEPS (con),
1216 INSN_RESOLVED_BACK_DEPS (con));
1218 move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
1219 INSN_RESOLVED_BACK_DEPS (con));
1221 move_dep_link (DEP_NODE_FORW (node), INSN_FORW_DEPS (pro),
1222 INSN_RESOLVED_FORW_DEPS (pro));
1225 /* Make TO depend on all the FROM's producers.
1226 If RESOLVED_P is true add dependencies to the resolved lists. */
1228 sd_copy_back_deps (rtx to, rtx from, bool resolved_p)
1230 sd_list_types_def list_type;
1231 sd_iterator_def sd_it;
1234 list_type = resolved_p ? SD_LIST_RES_BACK : SD_LIST_BACK;
1236 FOR_EACH_DEP (from, list_type, sd_it, dep)
1238 dep_def _new_dep, *new_dep = &_new_dep;
1240 copy_dep (new_dep, dep);
1241 DEP_CON (new_dep) = to;
1242 sd_add_dep (new_dep, resolved_p);
1246 /* Remove a dependency referred to by SD_IT.
1247 SD_IT will point to the next dependence after removal. */
1249 sd_delete_dep (sd_iterator_def sd_it)
1251 dep_node_t n = DEP_LINK_NODE (*sd_it.linkp);
1252 dep_t dep = DEP_NODE_DEP (n);
1253 rtx pro = DEP_PRO (dep);
1254 rtx con = DEP_CON (dep);
1255 deps_list_t con_back_deps;
1256 deps_list_t pro_forw_deps;
1258 if (true_dependency_cache != NULL)
1260 int elem_luid = INSN_LUID (pro);
1261 int insn_luid = INSN_LUID (con);
1263 bitmap_clear_bit (&true_dependency_cache[insn_luid], elem_luid);
1264 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1265 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1267 if (current_sched_info->flags & DO_SPECULATION)
1268 bitmap_clear_bit (&spec_dependency_cache[insn_luid], elem_luid);
1271 get_back_and_forw_lists (dep, sd_it.resolved_p,
1272 &con_back_deps, &pro_forw_deps);
1274 remove_from_deps_list (DEP_NODE_BACK (n), con_back_deps);
1275 remove_from_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1277 delete_dep_node (n);
1280 /* Dump size of the lists. */
1281 #define DUMP_LISTS_SIZE (2)
1283 /* Dump dependencies of the lists. */
1284 #define DUMP_LISTS_DEPS (4)
1286 /* Dump all information about the lists. */
1287 #define DUMP_LISTS_ALL (DUMP_LISTS_SIZE | DUMP_LISTS_DEPS)
1289 /* Dump deps_lists of INSN specified by TYPES to DUMP.
1290 FLAGS is a bit mask specifying what information about the lists needs
1292 If FLAGS has the very first bit set, then dump all information about
1293 the lists and propagate this bit into the callee dump functions. */
1295 dump_lists (FILE *dump, rtx insn, sd_list_types_def types, int flags)
1297 sd_iterator_def sd_it;
1304 flags |= DUMP_LISTS_ALL;
1306 fprintf (dump, "[");
1308 if (flags & DUMP_LISTS_SIZE)
1309 fprintf (dump, "%d; ", sd_lists_size (insn, types));
1311 if (flags & DUMP_LISTS_DEPS)
1313 FOR_EACH_DEP (insn, types, sd_it, dep)
1315 dump_dep (dump, dep, dump_dep_flags | all);
1316 fprintf (dump, " ");
1321 /* Dump all information about deps_lists of INSN specified by TYPES
1324 sd_debug_lists (rtx insn, sd_list_types_def types)
1326 dump_lists (stderr, insn, types, 1);
1327 fprintf (stderr, "\n");
1330 /* A convenience wrapper to operate on an entire list. */
1333 add_dependence_list (rtx insn, rtx list, int uncond, enum reg_note dep_type)
1335 for (; list; list = XEXP (list, 1))
1337 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1338 add_dependence (insn, XEXP (list, 0), dep_type);
1342 /* Similar, but free *LISTP at the same time. */
1345 add_dependence_list_and_free (rtx insn, rtx *listp, int uncond,
1346 enum reg_note dep_type)
1349 for (list = *listp, *listp = NULL; list ; list = next)
1351 next = XEXP (list, 1);
1352 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1353 add_dependence (insn, XEXP (list, 0), dep_type);
1354 free_INSN_LIST_node (list);
1358 /* Clear all dependencies for an insn. */
1360 delete_all_dependences (rtx insn)
1362 sd_iterator_def sd_it;
1365 /* The below cycle can be optimized to clear the caches and back_deps
1366 in one call but that would provoke duplication of code from
1369 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1370 sd_iterator_cond (&sd_it, &dep);)
1371 sd_delete_dep (sd_it);
1374 /* All insns in a scheduling group except the first should only have
1375 dependencies on the previous insn in the group. So we find the
1376 first instruction in the scheduling group by walking the dependence
1377 chains backwards. Then we add the dependencies for the group to
1378 the previous nonnote insn. */
1381 fixup_sched_groups (rtx insn)
1383 sd_iterator_def sd_it;
1387 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
1390 rtx pro = DEP_PRO (dep);
1394 i = prev_nonnote_insn (i);
1398 } while (SCHED_GROUP_P (i));
1400 if (! sched_insns_conditions_mutex_p (i, pro))
1401 add_dependence (i, pro, DEP_TYPE (dep));
1405 delete_all_dependences (insn);
1407 prev_nonnote = prev_nonnote_insn (insn);
1408 if (BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (prev_nonnote)
1409 && ! sched_insns_conditions_mutex_p (insn, prev_nonnote))
1410 add_dependence (insn, prev_nonnote, REG_DEP_ANTI);
1413 /* Process an insn's memory dependencies. There are four kinds of
1416 (0) read dependence: read follows read
1417 (1) true dependence: read follows write
1418 (2) output dependence: write follows write
1419 (3) anti dependence: write follows read
1421 We are careful to build only dependencies which actually exist, and
1422 use transitivity to avoid building too many links. */
1424 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1425 The MEM is a memory reference contained within INSN, which we are saving
1426 so that we can do memory aliasing on it. */
1429 add_insn_mem_dependence (struct deps *deps, bool read_p,
1438 insn_list = &deps->pending_read_insns;
1439 mem_list = &deps->pending_read_mems;
1440 deps->pending_read_list_length++;
1444 insn_list = &deps->pending_write_insns;
1445 mem_list = &deps->pending_write_mems;
1446 deps->pending_write_list_length++;
1449 link = alloc_INSN_LIST (insn, *insn_list);
1452 if (current_sched_info->use_cselib)
1454 mem = shallow_copy_rtx (mem);
1455 XEXP (mem, 0) = cselib_subst_to_values (XEXP (mem, 0));
1457 link = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
1461 /* Make a dependency between every memory reference on the pending lists
1462 and INSN, thus flushing the pending lists. FOR_READ is true if emitting
1463 dependencies for a read operation, similarly with FOR_WRITE. */
1466 flush_pending_lists (struct deps *deps, rtx insn, int for_read,
1471 add_dependence_list_and_free (insn, &deps->pending_read_insns, 1,
1473 free_EXPR_LIST_list (&deps->pending_read_mems);
1474 deps->pending_read_list_length = 0;
1477 add_dependence_list_and_free (insn, &deps->pending_write_insns, 1,
1478 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1479 free_EXPR_LIST_list (&deps->pending_write_mems);
1480 deps->pending_write_list_length = 0;
1482 add_dependence_list_and_free (insn, &deps->last_pending_memory_flush, 1,
1483 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1484 deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
1485 deps->pending_flush_length = 1;
1488 /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
1489 The type of the reference is specified by REF and can be SET,
1490 CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE. */
1493 sched_analyze_reg (struct deps *deps, int regno, enum machine_mode mode,
1494 enum rtx_code ref, rtx insn)
1496 /* A hard reg in a wide mode may really be multiple registers.
1497 If so, mark all of them just like the first. */
1498 if (regno < FIRST_PSEUDO_REGISTER)
1500 int i = hard_regno_nregs[regno][mode];
1504 SET_REGNO_REG_SET (reg_pending_sets, regno + i);
1506 else if (ref == USE)
1509 SET_REGNO_REG_SET (reg_pending_uses, regno + i);
1514 SET_REGNO_REG_SET (reg_pending_clobbers, regno + i);
1518 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
1519 it does not reload. Ignore these as they have served their
1521 else if (regno >= deps->max_reg)
1523 enum rtx_code code = GET_CODE (PATTERN (insn));
1524 gcc_assert (code == USE || code == CLOBBER);
1530 SET_REGNO_REG_SET (reg_pending_sets, regno);
1531 else if (ref == USE)
1532 SET_REGNO_REG_SET (reg_pending_uses, regno);
1534 SET_REGNO_REG_SET (reg_pending_clobbers, regno);
1536 /* Pseudos that are REG_EQUIV to something may be replaced
1537 by that during reloading. We need only add dependencies for
1538 the address in the REG_EQUIV note. */
1539 if (!reload_completed && get_reg_known_equiv_p (regno))
1541 rtx t = get_reg_known_value (regno);
1543 sched_analyze_2 (deps, XEXP (t, 0), insn);
1546 /* Don't let it cross a call after scheduling if it doesn't
1547 already cross one. */
1548 if (REG_N_CALLS_CROSSED (regno) == 0)
1551 deps->sched_before_next_call
1552 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
1554 add_dependence_list (insn, deps->last_function_call, 1,
1560 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
1561 rtx, X, creating all dependencies generated by the write to the
1562 destination of X, and reads of everything mentioned. */
1565 sched_analyze_1 (struct deps *deps, rtx x, rtx insn)
1567 rtx dest = XEXP (x, 0);
1568 enum rtx_code code = GET_CODE (x);
1573 if (GET_CODE (dest) == PARALLEL)
1577 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1578 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
1579 sched_analyze_1 (deps,
1580 gen_rtx_CLOBBER (VOIDmode,
1581 XEXP (XVECEXP (dest, 0, i), 0)),
1584 if (GET_CODE (x) == SET)
1585 sched_analyze_2 (deps, SET_SRC (x), insn);
1589 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
1590 || GET_CODE (dest) == ZERO_EXTRACT)
1592 if (GET_CODE (dest) == STRICT_LOW_PART
1593 || GET_CODE (dest) == ZERO_EXTRACT
1594 || df_read_modify_subreg_p (dest))
1596 /* These both read and modify the result. We must handle
1597 them as writes to get proper dependencies for following
1598 instructions. We must handle them as reads to get proper
1599 dependencies from this to previous instructions.
1600 Thus we need to call sched_analyze_2. */
1602 sched_analyze_2 (deps, XEXP (dest, 0), insn);
1604 if (GET_CODE (dest) == ZERO_EXTRACT)
1606 /* The second and third arguments are values read by this insn. */
1607 sched_analyze_2 (deps, XEXP (dest, 1), insn);
1608 sched_analyze_2 (deps, XEXP (dest, 2), insn);
1610 dest = XEXP (dest, 0);
1615 int regno = REGNO (dest);
1616 enum machine_mode mode = GET_MODE (dest);
1618 sched_analyze_reg (deps, regno, mode, code, insn);
1621 /* Treat all writes to a stack register as modifying the TOS. */
1622 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
1624 /* Avoid analyzing the same register twice. */
1625 if (regno != FIRST_STACK_REG)
1626 sched_analyze_reg (deps, FIRST_STACK_REG, mode, code, insn);
1627 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
1631 else if (MEM_P (dest))
1633 /* Writing memory. */
1636 if (current_sched_info->use_cselib)
1638 t = shallow_copy_rtx (dest);
1639 cselib_lookup (XEXP (t, 0), Pmode, 1);
1640 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
1644 if ((deps->pending_read_list_length + deps->pending_write_list_length)
1645 > MAX_PENDING_LIST_LENGTH)
1647 /* Flush all pending reads and writes to prevent the pending lists
1648 from getting any larger. Insn scheduling runs too slowly when
1649 these lists get long. When compiling GCC with itself,
1650 this flush occurs 8 times for sparc, and 10 times for m88k using
1651 the default value of 32. */
1652 flush_pending_lists (deps, insn, false, true);
1656 rtx pending, pending_mem;
1658 pending = deps->pending_read_insns;
1659 pending_mem = deps->pending_read_mems;
1662 if (anti_dependence (XEXP (pending_mem, 0), t)
1663 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1664 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1666 pending = XEXP (pending, 1);
1667 pending_mem = XEXP (pending_mem, 1);
1670 pending = deps->pending_write_insns;
1671 pending_mem = deps->pending_write_mems;
1674 if (output_dependence (XEXP (pending_mem, 0), t)
1675 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1676 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1678 pending = XEXP (pending, 1);
1679 pending_mem = XEXP (pending_mem, 1);
1682 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
1685 add_insn_mem_dependence (deps, false, insn, dest);
1687 sched_analyze_2 (deps, XEXP (dest, 0), insn);
1690 /* Analyze reads. */
1691 if (GET_CODE (x) == SET)
1692 sched_analyze_2 (deps, SET_SRC (x), insn);
1695 /* Analyze the uses of memory and registers in rtx X in INSN. */
1698 sched_analyze_2 (struct deps *deps, rtx x, rtx insn)
1708 code = GET_CODE (x);
1719 /* Ignore constants. Note that we must handle CONST_DOUBLE here
1720 because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
1721 this does not mean that this insn is using cc0. */
1726 /* User of CC0 depends on immediately preceding insn. */
1727 SCHED_GROUP_P (insn) = 1;
1728 /* Don't move CC0 setter to another block (it can set up the
1729 same flag for previous CC0 users which is safe). */
1730 CANT_MOVE (prev_nonnote_insn (insn)) = 1;
1736 int regno = REGNO (x);
1737 enum machine_mode mode = GET_MODE (x);
1739 sched_analyze_reg (deps, regno, mode, USE, insn);
1742 /* Treat all reads of a stack register as modifying the TOS. */
1743 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
1745 /* Avoid analyzing the same register twice. */
1746 if (regno != FIRST_STACK_REG)
1747 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
1748 sched_analyze_reg (deps, FIRST_STACK_REG, mode, SET, insn);
1756 /* Reading memory. */
1758 rtx pending, pending_mem;
1761 if (current_sched_info->use_cselib)
1763 t = shallow_copy_rtx (t);
1764 cselib_lookup (XEXP (t, 0), Pmode, 1);
1765 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
1768 pending = deps->pending_read_insns;
1769 pending_mem = deps->pending_read_mems;
1772 if (read_dependence (XEXP (pending_mem, 0), t)
1773 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1774 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1776 pending = XEXP (pending, 1);
1777 pending_mem = XEXP (pending_mem, 1);
1780 pending = deps->pending_write_insns;
1781 pending_mem = deps->pending_write_mems;
1784 if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
1786 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1788 if ((current_sched_info->flags & DO_SPECULATION)
1789 && (spec_info->mask & BEGIN_DATA))
1790 /* Create a data-speculative dependence between producer
1793 dep_def _dep, *dep = &_dep;
1795 init_dep_1 (dep, XEXP (pending, 0), insn, REG_DEP_TRUE,
1796 BEGIN_DATA | DEP_TRUE);
1798 maybe_add_or_update_dep_1 (dep, false,
1799 XEXP (pending_mem, 0), t);
1802 add_dependence (insn, XEXP (pending, 0), REG_DEP_TRUE);
1805 pending = XEXP (pending, 1);
1806 pending_mem = XEXP (pending_mem, 1);
1809 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
1810 if (! JUMP_P (XEXP (u, 0)) || deps_may_trap_p (x))
1811 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1813 /* Always add these dependencies to pending_reads, since
1814 this insn may be followed by a write. */
1815 add_insn_mem_dependence (deps, true, insn, x);
1817 /* Take advantage of tail recursion here. */
1818 sched_analyze_2 (deps, XEXP (x, 0), insn);
1822 /* Force pending stores to memory in case a trap handler needs them. */
1824 flush_pending_lists (deps, insn, true, false);
1829 case UNSPEC_VOLATILE:
1831 /* Traditional and volatile asm instructions must be considered to use
1832 and clobber all hard registers, all pseudo-registers and all of
1833 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
1835 Consider for instance a volatile asm that changes the fpu rounding
1836 mode. An insn should not be moved across this even if it only uses
1837 pseudo-regs because it might give an incorrectly rounded result. */
1838 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
1839 reg_pending_barrier = TRUE_BARRIER;
1841 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1842 We can not just fall through here since then we would be confused
1843 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1844 traditional asms unlike their normal usage. */
1846 if (code == ASM_OPERANDS)
1848 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
1849 sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
1859 /* These both read and modify the result. We must handle them as writes
1860 to get proper dependencies for following instructions. We must handle
1861 them as reads to get proper dependencies from this to previous
1862 instructions. Thus we need to pass them to both sched_analyze_1
1863 and sched_analyze_2. We must call sched_analyze_2 first in order
1864 to get the proper antecedent for the read. */
1865 sched_analyze_2 (deps, XEXP (x, 0), insn);
1866 sched_analyze_1 (deps, x, insn);
1871 /* op0 = op0 + op1 */
1872 sched_analyze_2 (deps, XEXP (x, 0), insn);
1873 sched_analyze_2 (deps, XEXP (x, 1), insn);
1874 sched_analyze_1 (deps, x, insn);
1881 /* Other cases: walk the insn. */
1882 fmt = GET_RTX_FORMAT (code);
1883 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1886 sched_analyze_2 (deps, XEXP (x, i), insn);
1887 else if (fmt[i] == 'E')
1888 for (j = 0; j < XVECLEN (x, i); j++)
1889 sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
1893 /* Analyze an INSN with pattern X to find all dependencies. */
1896 sched_analyze_insn (struct deps *deps, rtx x, rtx insn)
1898 RTX_CODE code = GET_CODE (x);
1901 reg_set_iterator rsi;
1903 if (code == COND_EXEC)
1905 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
1907 /* ??? Should be recording conditions so we reduce the number of
1908 false dependencies. */
1909 x = COND_EXEC_CODE (x);
1910 code = GET_CODE (x);
1912 if (code == SET || code == CLOBBER)
1914 sched_analyze_1 (deps, x, insn);
1916 /* Bare clobber insns are used for letting life analysis, reg-stack
1917 and others know that a value is dead. Depend on the last call
1918 instruction so that reg-stack won't get confused. */
1919 if (code == CLOBBER)
1920 add_dependence_list (insn, deps->last_function_call, 1, REG_DEP_OUTPUT);
1922 else if (code == PARALLEL)
1924 for (i = XVECLEN (x, 0); i--;)
1926 rtx sub = XVECEXP (x, 0, i);
1927 code = GET_CODE (sub);
1929 if (code == COND_EXEC)
1931 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
1932 sub = COND_EXEC_CODE (sub);
1933 code = GET_CODE (sub);
1935 if (code == SET || code == CLOBBER)
1936 sched_analyze_1 (deps, sub, insn);
1938 sched_analyze_2 (deps, sub, insn);
1942 sched_analyze_2 (deps, x, insn);
1944 /* Mark registers CLOBBERED or used by called function. */
1947 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1949 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
1950 sched_analyze_1 (deps, XEXP (link, 0), insn);
1952 sched_analyze_2 (deps, XEXP (link, 0), insn);
1954 if (find_reg_note (insn, REG_SETJMP, NULL))
1955 reg_pending_barrier = MOVE_BARRIER;
1961 next = next_nonnote_insn (insn);
1962 if (next && BARRIER_P (next))
1963 reg_pending_barrier = MOVE_BARRIER;
1966 rtx pending, pending_mem;
1967 regset_head tmp_uses, tmp_sets;
1968 INIT_REG_SET (&tmp_uses);
1969 INIT_REG_SET (&tmp_sets);
1971 (*current_sched_info->compute_jump_reg_dependencies)
1972 (insn, &deps->reg_conditional_sets, &tmp_uses, &tmp_sets);
1973 /* Make latency of jump equal to 0 by using anti-dependence. */
1974 EXECUTE_IF_SET_IN_REG_SET (&tmp_uses, 0, i, rsi)
1976 struct deps_reg *reg_last = &deps->reg_last[i];
1977 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI);
1978 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI);
1979 reg_last->uses_length++;
1980 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1982 IOR_REG_SET (reg_pending_sets, &tmp_sets);
1984 CLEAR_REG_SET (&tmp_uses);
1985 CLEAR_REG_SET (&tmp_sets);
1987 /* All memory writes and volatile reads must happen before the
1988 jump. Non-volatile reads must happen before the jump iff
1989 the result is needed by the above register used mask. */
1991 pending = deps->pending_write_insns;
1992 pending_mem = deps->pending_write_mems;
1995 if (! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1996 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1997 pending = XEXP (pending, 1);
1998 pending_mem = XEXP (pending_mem, 1);
2001 pending = deps->pending_read_insns;
2002 pending_mem = deps->pending_read_mems;
2005 if (MEM_VOLATILE_P (XEXP (pending_mem, 0))
2006 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2007 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
2008 pending = XEXP (pending, 1);
2009 pending_mem = XEXP (pending_mem, 1);
2012 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
2017 /* If this instruction can throw an exception, then moving it changes
2018 where block boundaries fall. This is mighty confusing elsewhere.
2019 Therefore, prevent such an instruction from being moved. Same for
2020 non-jump instructions that define block boundaries.
2021 ??? Unclear whether this is still necessary in EBB mode. If not,
2022 add_branch_dependences should be adjusted for RGN mode instead. */
2023 if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
2024 || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
2025 reg_pending_barrier = MOVE_BARRIER;
2027 /* Add register dependencies for insn.
2028 If the current insn is conditional, we can't free any of the lists. */
2029 if (sched_get_condition (insn))
2031 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
2033 struct deps_reg *reg_last = &deps->reg_last[i];
2034 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
2035 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
2036 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2037 reg_last->uses_length++;
2039 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
2041 struct deps_reg *reg_last = &deps->reg_last[i];
2042 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2043 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2044 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
2045 reg_last->clobbers_length++;
2047 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
2049 struct deps_reg *reg_last = &deps->reg_last[i];
2050 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2051 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT);
2052 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2053 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2054 SET_REGNO_REG_SET (&deps->reg_conditional_sets, i);
2059 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
2061 struct deps_reg *reg_last = &deps->reg_last[i];
2062 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
2063 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
2064 reg_last->uses_length++;
2065 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2067 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
2069 struct deps_reg *reg_last = &deps->reg_last[i];
2070 if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
2071 || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
2073 add_dependence_list_and_free (insn, ®_last->sets, 0,
2075 add_dependence_list_and_free (insn, ®_last->uses, 0,
2077 add_dependence_list_and_free (insn, ®_last->clobbers, 0,
2079 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2080 reg_last->clobbers_length = 0;
2081 reg_last->uses_length = 0;
2085 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2086 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2088 reg_last->clobbers_length++;
2089 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
2091 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
2093 struct deps_reg *reg_last = &deps->reg_last[i];
2094 add_dependence_list_and_free (insn, ®_last->sets, 0,
2096 add_dependence_list_and_free (insn, ®_last->clobbers, 0,
2098 add_dependence_list_and_free (insn, ®_last->uses, 0,
2100 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2101 reg_last->uses_length = 0;
2102 reg_last->clobbers_length = 0;
2103 CLEAR_REGNO_REG_SET (&deps->reg_conditional_sets, i);
2107 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
2108 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
2109 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
2111 CLEAR_REG_SET (reg_pending_uses);
2112 CLEAR_REG_SET (reg_pending_clobbers);
2113 CLEAR_REG_SET (reg_pending_sets);
2115 /* Add dependencies if a scheduling barrier was found. */
2116 if (reg_pending_barrier)
2118 /* In the case of barrier the most added dependencies are not
2119 real, so we use anti-dependence here. */
2120 if (sched_get_condition (insn))
2122 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2124 struct deps_reg *reg_last = &deps->reg_last[i];
2125 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2127 (insn, reg_last->sets, 0,
2128 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2130 (insn, reg_last->clobbers, 0,
2131 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2136 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2138 struct deps_reg *reg_last = &deps->reg_last[i];
2139 add_dependence_list_and_free (insn, ®_last->uses, 0,
2141 add_dependence_list_and_free
2142 (insn, ®_last->sets, 0,
2143 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2144 add_dependence_list_and_free
2145 (insn, ®_last->clobbers, 0,
2146 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2147 reg_last->uses_length = 0;
2148 reg_last->clobbers_length = 0;
2152 for (i = 0; i < (unsigned)deps->max_reg; i++)
2154 struct deps_reg *reg_last = &deps->reg_last[i];
2155 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2156 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
2159 flush_pending_lists (deps, insn, true, true);
2160 CLEAR_REG_SET (&deps->reg_conditional_sets);
2161 reg_pending_barrier = NOT_A_BARRIER;
2164 /* If a post-call group is still open, see if it should remain so.
2165 This insn must be a simple move of a hard reg to a pseudo or
2168 We must avoid moving these insns for correctness on
2169 SMALL_REGISTER_CLASS machines, and for special registers like
2170 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
2171 hard regs for all targets. */
2173 if (deps->in_post_call_group_p)
2175 rtx tmp, set = single_set (insn);
2176 int src_regno, dest_regno;
2179 goto end_call_group;
2181 tmp = SET_DEST (set);
2182 if (GET_CODE (tmp) == SUBREG)
2183 tmp = SUBREG_REG (tmp);
2185 dest_regno = REGNO (tmp);
2187 goto end_call_group;
2189 tmp = SET_SRC (set);
2190 if (GET_CODE (tmp) == SUBREG)
2191 tmp = SUBREG_REG (tmp);
2192 if ((GET_CODE (tmp) == PLUS
2193 || GET_CODE (tmp) == MINUS)
2194 && REG_P (XEXP (tmp, 0))
2195 && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
2196 && dest_regno == STACK_POINTER_REGNUM)
2197 src_regno = STACK_POINTER_REGNUM;
2198 else if (REG_P (tmp))
2199 src_regno = REGNO (tmp);
2201 goto end_call_group;
2203 if (src_regno < FIRST_PSEUDO_REGISTER
2204 || dest_regno < FIRST_PSEUDO_REGISTER)
2206 if (deps->in_post_call_group_p == post_call_initial)
2207 deps->in_post_call_group_p = post_call;
2209 SCHED_GROUP_P (insn) = 1;
2210 CANT_MOVE (insn) = 1;
2215 deps->in_post_call_group_p = not_post_call;
2219 /* Fixup the dependencies in the sched group. */
2220 if (SCHED_GROUP_P (insn))
2221 fixup_sched_groups (insn);
2223 if ((current_sched_info->flags & DO_SPECULATION)
2224 && !sched_insn_is_legitimate_for_speculation_p (insn, 0))
2225 /* INSN has an internal dependency (e.g. r14 = [r14]) and thus cannot
2228 sd_iterator_def sd_it;
2231 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
2232 sd_iterator_cond (&sd_it, &dep);)
2233 change_spec_dep_to_hard (sd_it);
2237 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
2238 dependencies for each insn. */
2241 sched_analyze (struct deps *deps, rtx head, rtx tail)
2245 if (current_sched_info->use_cselib)
2248 /* Before reload, if the previous block ended in a call, show that
2249 we are inside a post-call group, so as to keep the lifetimes of
2250 hard registers correct. */
2251 if (! reload_completed && !LABEL_P (head))
2253 insn = prev_nonnote_insn (head);
2254 if (insn && CALL_P (insn))
2255 deps->in_post_call_group_p = post_call_initial;
2257 for (insn = head;; insn = NEXT_INSN (insn))
2261 /* And initialize deps_lists. */
2262 sd_init_insn (insn);
2265 if (NONJUMP_INSN_P (insn) || JUMP_P (insn))
2267 /* Make each JUMP_INSN a scheduling barrier for memory
2271 /* Keep the list a reasonable size. */
2272 if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
2273 flush_pending_lists (deps, insn, true, true);
2275 deps->last_pending_memory_flush
2276 = alloc_INSN_LIST (insn, deps->last_pending_memory_flush);
2278 sched_analyze_insn (deps, PATTERN (insn), insn);
2280 else if (CALL_P (insn))
2284 CANT_MOVE (insn) = 1;
2286 if (find_reg_note (insn, REG_SETJMP, NULL))
2288 /* This is setjmp. Assume that all registers, not just
2289 hard registers, may be clobbered by this call. */
2290 reg_pending_barrier = MOVE_BARRIER;
2294 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2295 /* A call may read and modify global register variables. */
2298 SET_REGNO_REG_SET (reg_pending_sets, i);
2299 SET_REGNO_REG_SET (reg_pending_uses, i);
2301 /* Other call-clobbered hard regs may be clobbered.
2302 Since we only have a choice between 'might be clobbered'
2303 and 'definitely not clobbered', we must include all
2304 partly call-clobbered registers here. */
2305 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
2306 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
2307 SET_REGNO_REG_SET (reg_pending_clobbers, i);
2308 /* We don't know what set of fixed registers might be used
2309 by the function, but it is certain that the stack pointer
2310 is among them, but be conservative. */
2311 else if (fixed_regs[i])
2312 SET_REGNO_REG_SET (reg_pending_uses, i);
2313 /* The frame pointer is normally not used by the function
2314 itself, but by the debugger. */
2315 /* ??? MIPS o32 is an exception. It uses the frame pointer
2316 in the macro expansion of jal but does not represent this
2317 fact in the call_insn rtl. */
2318 else if (i == FRAME_POINTER_REGNUM
2319 || (i == HARD_FRAME_POINTER_REGNUM
2320 && (! reload_completed || frame_pointer_needed)))
2321 SET_REGNO_REG_SET (reg_pending_uses, i);
2324 /* For each insn which shouldn't cross a call, add a dependence
2325 between that insn and this call insn. */
2326 add_dependence_list_and_free (insn, &deps->sched_before_next_call, 1,
2329 sched_analyze_insn (deps, PATTERN (insn), insn);
2331 /* In the absence of interprocedural alias analysis, we must flush
2332 all pending reads and writes, and start new dependencies starting
2333 from here. But only flush writes for constant calls (which may
2334 be passed a pointer to something we haven't written yet). */
2335 flush_pending_lists (deps, insn, true,
2336 ! RTL_CONST_OR_PURE_CALL_P (insn));
2338 /* Remember the last function call for limiting lifetimes. */
2339 free_INSN_LIST_list (&deps->last_function_call);
2340 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
2342 /* Before reload, begin a post-call group, so as to keep the
2343 lifetimes of hard registers correct. */
2344 if (! reload_completed)
2345 deps->in_post_call_group_p = post_call;
2348 /* EH_REGION insn notes can not appear until well after we complete
2351 gcc_assert (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
2352 && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END);
2354 if (current_sched_info->use_cselib)
2355 cselib_process_insn (insn);
2359 if (current_sched_info->use_cselib)
2367 /* Helper for sched_free_deps ().
2368 Delete INSN's (RESOLVED_P) backward dependencies. */
2370 delete_dep_nodes_in_back_deps (rtx insn, bool resolved_p)
2372 sd_iterator_def sd_it;
2374 sd_list_types_def types;
2377 types = SD_LIST_RES_BACK;
2379 types = SD_LIST_BACK;
2381 for (sd_it = sd_iterator_start (insn, types);
2382 sd_iterator_cond (&sd_it, &dep);)
2384 dep_link_t link = *sd_it.linkp;
2385 dep_node_t node = DEP_LINK_NODE (link);
2386 deps_list_t back_list;
2387 deps_list_t forw_list;
2389 get_back_and_forw_lists (dep, resolved_p, &back_list, &forw_list);
2390 remove_from_deps_list (link, back_list);
2391 delete_dep_node (node);
2395 /* Delete (RESOLVED_P) dependencies between HEAD and TAIL together with
2398 sched_free_deps (rtx head, rtx tail, bool resolved_p)
2401 rtx next_tail = NEXT_INSN (tail);
2403 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
2404 if (INSN_P (insn) && INSN_LUID (insn) > 0)
2406 /* Clear resolved back deps together with its dep_nodes. */
2407 delete_dep_nodes_in_back_deps (insn, resolved_p);
2409 /* Clear forward deps and leave the dep_nodes to the
2410 corresponding back_deps list. */
2412 clear_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
2414 clear_deps_list (INSN_FORW_DEPS (insn));
2416 sd_finish_insn (insn);
2420 /* Initialize variables for region data dependence analysis.
2421 n_bbs is the number of region blocks. */
2424 init_deps (struct deps *deps)
2426 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
2428 deps->max_reg = max_reg;
2429 deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
2430 INIT_REG_SET (&deps->reg_last_in_use);
2431 INIT_REG_SET (&deps->reg_conditional_sets);
2433 deps->pending_read_insns = 0;
2434 deps->pending_read_mems = 0;
2435 deps->pending_write_insns = 0;
2436 deps->pending_write_mems = 0;
2437 deps->pending_read_list_length = 0;
2438 deps->pending_write_list_length = 0;
2439 deps->pending_flush_length = 0;
2440 deps->last_pending_memory_flush = 0;
2441 deps->last_function_call = 0;
2442 deps->sched_before_next_call = 0;
2443 deps->in_post_call_group_p = not_post_call;
2446 /* Free insn lists found in DEPS. */
2449 free_deps (struct deps *deps)
2452 reg_set_iterator rsi;
2454 free_INSN_LIST_list (&deps->pending_read_insns);
2455 free_EXPR_LIST_list (&deps->pending_read_mems);
2456 free_INSN_LIST_list (&deps->pending_write_insns);
2457 free_EXPR_LIST_list (&deps->pending_write_mems);
2458 free_INSN_LIST_list (&deps->last_pending_memory_flush);
2460 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
2461 times. For a testcase with 42000 regs and 8000 small basic blocks,
2462 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
2463 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2465 struct deps_reg *reg_last = &deps->reg_last[i];
2467 free_INSN_LIST_list (®_last->uses);
2469 free_INSN_LIST_list (®_last->sets);
2470 if (reg_last->clobbers)
2471 free_INSN_LIST_list (®_last->clobbers);
2473 CLEAR_REG_SET (&deps->reg_last_in_use);
2474 CLEAR_REG_SET (&deps->reg_conditional_sets);
2476 free (deps->reg_last);
2479 /* If it is profitable to use them, initialize caches for tracking
2480 dependency information. LUID is the number of insns to be scheduled,
2481 it is used in the estimate of profitability. */
2484 init_dependency_caches (int luid)
2486 /* Average number of insns in the basic block.
2487 '+ 1' is used to make it nonzero. */
2488 int insns_in_block = luid / n_basic_blocks + 1;
2490 /* ?!? We could save some memory by computing a per-region luid mapping
2491 which could reduce both the number of vectors in the cache and the size
2492 of each vector. Instead we just avoid the cache entirely unless the
2493 average number of instructions in a basic block is very high. See
2494 the comment before the declaration of true_dependency_cache for
2495 what we consider "very high". */
2496 if (insns_in_block > 100 * 5)
2499 extend_dependency_caches (luid, true);
2502 dl_pool = create_alloc_pool ("deps_list", sizeof (struct _deps_list),
2503 /* Allocate lists for one block at a time. */
2506 dn_pool = create_alloc_pool ("dep_node", sizeof (struct _dep_node),
2507 /* Allocate nodes for one block at a time.
2508 We assume that average insn has
2510 5 * insns_in_block);
2513 /* Create or extend (depending on CREATE_P) dependency caches to
2516 extend_dependency_caches (int n, bool create_p)
2518 if (create_p || true_dependency_cache)
2520 int i, luid = cache_size + n;
2522 true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
2524 output_dependency_cache = XRESIZEVEC (bitmap_head,
2525 output_dependency_cache, luid);
2526 anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
2529 if (current_sched_info->flags & DO_SPECULATION)
2530 spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
2533 for (i = cache_size; i < luid; i++)
2535 bitmap_initialize (&true_dependency_cache[i], 0);
2536 bitmap_initialize (&output_dependency_cache[i], 0);
2537 bitmap_initialize (&anti_dependency_cache[i], 0);
2539 if (current_sched_info->flags & DO_SPECULATION)
2540 bitmap_initialize (&spec_dependency_cache[i], 0);
2546 /* Free the caches allocated in init_dependency_caches. */
2549 free_dependency_caches (void)
2551 gcc_assert (deps_pools_are_empty_p ());
2552 free_alloc_pool_if_empty (&dn_pool);
2553 free_alloc_pool_if_empty (&dl_pool);
2554 gcc_assert (dn_pool == NULL && dl_pool == NULL);
2556 if (true_dependency_cache)
2560 for (i = 0; i < cache_size; i++)
2562 bitmap_clear (&true_dependency_cache[i]);
2563 bitmap_clear (&output_dependency_cache[i]);
2564 bitmap_clear (&anti_dependency_cache[i]);
2566 if (current_sched_info->flags & DO_SPECULATION)
2567 bitmap_clear (&spec_dependency_cache[i]);
2569 free (true_dependency_cache);
2570 true_dependency_cache = NULL;
2571 free (output_dependency_cache);
2572 output_dependency_cache = NULL;
2573 free (anti_dependency_cache);
2574 anti_dependency_cache = NULL;
2576 if (current_sched_info->flags & DO_SPECULATION)
2578 free (spec_dependency_cache);
2579 spec_dependency_cache = NULL;
2584 /* Initialize some global variables needed by the dependency analysis
2588 init_deps_global (void)
2590 reg_pending_sets = ALLOC_REG_SET (®_obstack);
2591 reg_pending_clobbers = ALLOC_REG_SET (®_obstack);
2592 reg_pending_uses = ALLOC_REG_SET (®_obstack);
2593 reg_pending_barrier = NOT_A_BARRIER;
2596 /* Free everything used by the dependency analysis code. */
2599 finish_deps_global (void)
2601 FREE_REG_SET (reg_pending_sets);
2602 FREE_REG_SET (reg_pending_clobbers);
2603 FREE_REG_SET (reg_pending_uses);
2606 /* Estimate the weakness of dependence between MEM1 and MEM2. */
2608 estimate_dep_weak (rtx mem1, rtx mem2)
2613 /* MEMs are the same - don't speculate. */
2614 return MIN_DEP_WEAK;
2616 r1 = XEXP (mem1, 0);
2617 r2 = XEXP (mem2, 0);
2620 || (REG_P (r1) && REG_P (r2)
2621 && REGNO (r1) == REGNO (r2)))
2622 /* Again, MEMs are the same. */
2623 return MIN_DEP_WEAK;
2624 else if ((REG_P (r1) && !REG_P (r2))
2625 || (!REG_P (r1) && REG_P (r2)))
2626 /* Different addressing modes - reason to be more speculative,
2628 return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
2630 /* We can't say anything about the dependence. */
2631 return UNCERTAIN_DEP_WEAK;
2634 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
2635 This function can handle same INSN and ELEM (INSN == ELEM).
2636 It is a convenience wrapper. */
2638 add_dependence (rtx insn, rtx elem, enum reg_note dep_type)
2640 dep_def _dep, *dep = &_dep;
2642 init_dep (dep, elem, insn, dep_type);
2643 maybe_add_or_update_dep_1 (dep, false, NULL_RTX, NULL_RTX);
2646 /* Return weakness of speculative type TYPE in the dep_status DS. */
2648 get_dep_weak_1 (ds_t ds, ds_t type)
2653 case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
2654 case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
2655 case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
2656 case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
2657 default: gcc_unreachable ();
2663 /* Return weakness of speculative type TYPE in the dep_status DS. */
2665 get_dep_weak (ds_t ds, ds_t type)
2667 dw_t dw = get_dep_weak_1 (ds, type);
2669 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
2674 /* Return the dep_status, which has the same parameters as DS, except for
2675 speculative type TYPE, that will have weakness DW. */
2677 set_dep_weak (ds_t ds, ds_t type, dw_t dw)
2679 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
2684 case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
2685 case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
2686 case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
2687 case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
2688 default: gcc_unreachable ();
2693 /* Return the join of two dep_statuses DS1 and DS2. */
2695 ds_merge (ds_t ds1, ds_t ds2)
2699 gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
2701 ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
2703 t = FIRST_SPEC_TYPE;
2706 if ((ds1 & t) && !(ds2 & t))
2708 else if (!(ds1 & t) && (ds2 & t))
2710 else if ((ds1 & t) && (ds2 & t))
2714 dw = ((ds_t) get_dep_weak (ds1, t)) * ((ds_t) get_dep_weak (ds2, t));
2716 if (dw < MIN_DEP_WEAK)
2719 ds = set_dep_weak (ds, t, (dw_t) dw);
2722 if (t == LAST_SPEC_TYPE)
2724 t <<= SPEC_TYPE_SHIFT;
2731 /* Dump information about the dependence status S. */
2733 dump_ds (FILE *f, ds_t s)
2738 fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak_1 (s, BEGIN_DATA));
2740 fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak_1 (s, BE_IN_DATA));
2741 if (s & BEGIN_CONTROL)
2742 fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak_1 (s, BEGIN_CONTROL));
2743 if (s & BE_IN_CONTROL)
2744 fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak_1 (s, BE_IN_CONTROL));
2747 fprintf (f, "HARD_DEP; ");
2750 fprintf (f, "DEP_TRUE; ");
2752 fprintf (f, "DEP_ANTI; ");
2754 fprintf (f, "DEP_OUTPUT; ");
2762 dump_ds (stderr, s);
2763 fprintf (stderr, "\n");
2766 #ifdef ENABLE_CHECKING
2767 /* Verify that dependence type and status are consistent.
2768 If RELAXED_P is true, then skip dep_weakness checks. */
2770 check_dep (dep_t dep, bool relaxed_p)
2772 enum reg_note dt = DEP_TYPE (dep);
2773 ds_t ds = DEP_STATUS (dep);
2775 gcc_assert (DEP_PRO (dep) != DEP_CON (dep));
2777 if (!(current_sched_info->flags & USE_DEPS_LIST))
2779 gcc_assert (ds == -1);
2783 /* Check that dependence type contains the same bits as the status. */
2784 if (dt == REG_DEP_TRUE)
2785 gcc_assert (ds & DEP_TRUE);
2786 else if (dt == REG_DEP_OUTPUT)
2787 gcc_assert ((ds & DEP_OUTPUT)
2788 && !(ds & DEP_TRUE));
2790 gcc_assert ((dt == REG_DEP_ANTI)
2792 && !(ds & (DEP_OUTPUT | DEP_TRUE)));
2794 /* HARD_DEP can not appear in dep_status of a link. */
2795 gcc_assert (!(ds & HARD_DEP));
2797 /* Check that dependence status is set correctly when speculation is not
2799 if (!(current_sched_info->flags & DO_SPECULATION))
2800 gcc_assert (!(ds & SPECULATIVE));
2801 else if (ds & SPECULATIVE)
2805 ds_t type = FIRST_SPEC_TYPE;
2807 /* Check that dependence weakness is in proper range. */
2811 get_dep_weak (ds, type);
2813 if (type == LAST_SPEC_TYPE)
2815 type <<= SPEC_TYPE_SHIFT;
2820 if (ds & BEGIN_SPEC)
2822 /* Only true dependence can be data speculative. */
2823 if (ds & BEGIN_DATA)
2824 gcc_assert (ds & DEP_TRUE);
2826 /* Control dependencies in the insn scheduler are represented by
2827 anti-dependencies, therefore only anti dependence can be
2828 control speculative. */
2829 if (ds & BEGIN_CONTROL)
2830 gcc_assert (ds & DEP_ANTI);
2834 /* Subsequent speculations should resolve true dependencies. */
2835 gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
2838 /* Check that true and anti dependencies can't have other speculative
2841 gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
2842 /* An output dependence can't be speculative at all. */
2843 gcc_assert (!(ds & DEP_OUTPUT));
2845 gcc_assert (ds & BEGIN_CONTROL);
2848 #endif /* ENABLE_CHECKING */
2850 #endif /* INSN_SCHEDULING */