Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / sched-deps.c
blobb077ff0e649c7124c3210bab8e117a9006c8a0bf
1 /* Instruction scheduling pass. This file computes dependencies between
2 instructions.
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
14 version.
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
19 for more details.
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/>. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "toplev.h"
30 #include "rtl.h"
31 #include "tm_p.h"
32 #include "hard-reg-set.h"
33 #include "regs.h"
34 #include "function.h"
35 #include "flags.h"
36 #include "insn-config.h"
37 #include "insn-attr.h"
38 #include "except.h"
39 #include "toplev.h"
40 #include "recog.h"
41 #include "sched-int.h"
42 #include "params.h"
43 #include "cselib.h"
45 #ifdef INSN_SCHEDULING
47 #ifdef ENABLE_CHECKING
48 #define CHECK (true)
49 #else
50 #define CHECK (false)
51 #endif
53 /* Return the major type present in the DS. */
54 enum reg_note
55 ds_to_dk (ds_t ds)
57 if (ds & DEP_TRUE)
58 return REG_DEP_TRUE;
60 if (ds & DEP_OUTPUT)
61 return REG_DEP_OUTPUT;
63 gcc_assert (ds & DEP_ANTI);
65 return REG_DEP_ANTI;
68 /* Return equivalent dep_status. */
69 ds_t
70 dk_to_ds (enum reg_note dk)
72 switch (dk)
74 case REG_DEP_TRUE:
75 return DEP_TRUE;
77 case REG_DEP_OUTPUT:
78 return DEP_OUTPUT;
80 default:
81 gcc_assert (dk == REG_DEP_ANTI);
82 return DEP_ANTI;
86 /* Functions to operate with dependence information container - dep_t. */
88 /* Init DEP with the arguments. */
89 void
90 init_dep_1 (dep_t dep, rtx pro, rtx con, enum reg_note type, ds_t ds)
92 DEP_PRO (dep) = pro;
93 DEP_CON (dep) = con;
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. */
101 void
102 init_dep (dep_t dep, rtx pro, rtx con, enum reg_note kind)
104 ds_t ds;
106 if ((current_sched_info->flags & USE_DEPS_LIST))
107 ds = dk_to_ds (kind);
108 else
109 ds = -1;
111 init_dep_1 (dep, pro, con, kind, ds);
114 /* Make a copy of FROM in TO. */
115 static void
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 \
139 |DUMP_DEP_STATUS)
141 /* Dump DEP to DUMP.
142 FLAGS is a bit mask specifying what information about DEP needs
143 to be printed.
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. */
146 static void
147 dump_dep (FILE *dump, dep_t dep, int flags)
149 if (flags & 1)
150 flags |= DUMP_DEP_ALL;
152 fprintf (dump, "<");
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)
162 char t;
163 enum reg_note type = DEP_TYPE (dep);
165 switch (type)
167 case REG_DEP_TRUE:
168 t = 't';
169 break;
171 case REG_DEP_OUTPUT:
172 t = 'o';
173 break;
175 case REG_DEP_ANTI:
176 t = 'a';
177 break;
179 default:
180 gcc_unreachable ();
181 break;
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));
193 fprintf (dump, ">");
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. */
200 void
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 -
208 dep_link_t. */
210 /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
211 PREV_NEXT_P. */
212 static void
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;
224 /* Fix next node. */
225 if (next != NULL)
227 gcc_assert (DEP_LINK_PREV_NEXTP (next) == prev_nextp);
229 DEP_LINK_PREV_NEXTP (next) = &DEP_LINK_NEXT (l);
232 /* Fix prev node. */
233 *prev_nextp = l;
236 /* Add dep_link LINK to deps_list L. */
237 static void
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. */
246 static void
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);
252 *prev_nextp = next;
254 if (next != NULL)
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. */
262 static void
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. */
271 static void
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. */
279 static bool
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. */
292 static dep_node_t
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;
307 ++dn_pool_diff;
309 return n;
312 /* Delete dep_node N. N must not be connected to any deps_list. */
313 static void
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)));
319 --dn_pool_diff;
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. */
333 static bool
334 deps_list_empty_p (deps_list_t l)
336 return DEPS_LIST_N_LINKS (l) == 0;
339 /* Create a new deps_list. */
340 static deps_list_t
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;
348 ++dl_pool_diff;
349 return l;
352 /* Free deps_list L. */
353 static void
354 free_deps_list (deps_list_t l)
356 gcc_assert (deps_list_empty_p (l));
358 --dl_pool_diff;
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. */
366 bool
367 deps_pools_are_empty_p (void)
369 return dn_pool_diff == 0 && dl_pool_diff == 0;
372 /* Remove all elements from L. */
373 static void
374 clear_deps_list (deps_list_t l)
378 dep_link_t link = DEPS_LIST_FIRST (l);
380 if (link == NULL)
381 break;
383 remove_from_deps_list (link, l);
385 while (1);
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
397 NOT_A_BARRIER = 0,
398 MOVE_BARRIER,
399 TRUE_BARRIER
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,
438 rtx, rtx);
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);
444 #endif
446 /* Return nonzero if a load of the memory reference MEM can cause a trap. */
448 static int
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));
456 if (t)
457 addr = t;
459 return rtx_addr_can_trap_p (addr);
462 /* Find the condition under which INSN is executed. */
464 static rtx
465 sched_get_condition (const_rtx insn)
467 rtx pat = PATTERN (insn);
468 rtx src;
470 if (pat == 0)
471 return 0;
473 if (GET_CODE (pat) == COND_EXEC)
474 return COND_EXEC_TEST (pat);
476 if (!any_condjump_p (insn) || !onlyjump_p (insn))
477 return 0;
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)
489 return 0;
490 return gen_rtx_fmt_ee (revcode, GET_MODE (cond), XEXP (cond, 0),
491 XEXP (cond, 1));
494 return 0;
498 /* Return nonzero if conditions COND1 and COND2 can never be both true. */
500 static int
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))
508 return 1;
509 return 0;
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. */
514 bool
515 sched_insns_conditions_mutex_p (const_rtx insn1, const_rtx insn2)
517 rtx cond1, cond2;
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);
525 if (cond1 && cond2
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))
533 return true;
535 return false;
539 /* Initialize LIST_PTR to point to one of the lists present in TYPES_PTR,
540 initialize RESOLVED_P_PTR with true if that list consists of resolved deps,
541 and remove the type of returned [through LIST_PTR] list from TYPES_PTR.
542 This function is used to switch sd_iterator to the next list.
543 !!! For internal use only. Might consider moving it to sched-int.h. */
544 void
545 sd_next_list (const_rtx insn, sd_list_types_def *types_ptr,
546 deps_list_t *list_ptr, bool *resolved_p_ptr)
548 sd_list_types_def types = *types_ptr;
550 if (types & SD_LIST_HARD_BACK)
552 *list_ptr = INSN_HARD_BACK_DEPS (insn);
553 *resolved_p_ptr = false;
554 *types_ptr = types & ~SD_LIST_HARD_BACK;
556 else if (types & SD_LIST_SPEC_BACK)
558 *list_ptr = INSN_SPEC_BACK_DEPS (insn);
559 *resolved_p_ptr = false;
560 *types_ptr = types & ~SD_LIST_SPEC_BACK;
562 else if (types & SD_LIST_FORW)
564 *list_ptr = INSN_FORW_DEPS (insn);
565 *resolved_p_ptr = false;
566 *types_ptr = types & ~SD_LIST_FORW;
568 else if (types & SD_LIST_RES_BACK)
570 *list_ptr = INSN_RESOLVED_BACK_DEPS (insn);
571 *resolved_p_ptr = true;
572 *types_ptr = types & ~SD_LIST_RES_BACK;
574 else if (types & SD_LIST_RES_FORW)
576 *list_ptr = INSN_RESOLVED_FORW_DEPS (insn);
577 *resolved_p_ptr = true;
578 *types_ptr = types & ~SD_LIST_RES_FORW;
580 else
582 *list_ptr = NULL;
583 *resolved_p_ptr = false;
584 *types_ptr = SD_LIST_NONE;
588 /* Return the summary size of INSN's lists defined by LIST_TYPES. */
590 sd_lists_size (const_rtx insn, sd_list_types_def list_types)
592 int size = 0;
594 while (list_types != SD_LIST_NONE)
596 deps_list_t list;
597 bool resolved_p;
599 sd_next_list (insn, &list_types, &list, &resolved_p);
600 size += DEPS_LIST_N_LINKS (list);
603 return size;
606 /* Return true if INSN's lists defined by LIST_TYPES are all empty. */
607 bool
608 sd_lists_empty_p (const_rtx insn, sd_list_types_def list_types)
610 return sd_lists_size (insn, list_types) == 0;
613 /* Initialize data for INSN. */
614 void
615 sd_init_insn (rtx insn)
617 INSN_HARD_BACK_DEPS (insn) = create_deps_list ();
618 INSN_SPEC_BACK_DEPS (insn) = create_deps_list ();
619 INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list ();
620 INSN_FORW_DEPS (insn) = create_deps_list ();
621 INSN_RESOLVED_FORW_DEPS (insn) = create_deps_list ();
623 /* ??? It would be nice to allocate dependency caches here. */
626 /* Free data for INSN. */
627 void
628 sd_finish_insn (rtx insn)
630 /* ??? It would be nice to deallocate dependency caches here. */
632 free_deps_list (INSN_HARD_BACK_DEPS (insn));
633 INSN_HARD_BACK_DEPS (insn) = NULL;
635 free_deps_list (INSN_SPEC_BACK_DEPS (insn));
636 INSN_SPEC_BACK_DEPS (insn) = NULL;
638 free_deps_list (INSN_RESOLVED_BACK_DEPS (insn));
639 INSN_RESOLVED_BACK_DEPS (insn) = NULL;
641 free_deps_list (INSN_FORW_DEPS (insn));
642 INSN_FORW_DEPS (insn) = NULL;
644 free_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
645 INSN_RESOLVED_FORW_DEPS (insn) = NULL;
648 /* Find a dependency between producer PRO and consumer CON.
649 Search through resolved dependency lists if RESOLVED_P is true.
650 If no such dependency is found return NULL,
651 otherwise return the dependency and initialize SD_IT_PTR [if it is nonnull]
652 with an iterator pointing to it. */
653 static dep_t
654 sd_find_dep_between_no_cache (rtx pro, rtx con, bool resolved_p,
655 sd_iterator_def *sd_it_ptr)
657 sd_list_types_def pro_list_type;
658 sd_list_types_def con_list_type;
659 sd_iterator_def sd_it;
660 dep_t dep;
661 bool found_p = false;
663 if (resolved_p)
665 pro_list_type = SD_LIST_RES_FORW;
666 con_list_type = SD_LIST_RES_BACK;
668 else
670 pro_list_type = SD_LIST_FORW;
671 con_list_type = SD_LIST_BACK;
674 /* Walk through either back list of INSN or forw list of ELEM
675 depending on which one is shorter. */
676 if (sd_lists_size (con, con_list_type) < sd_lists_size (pro, pro_list_type))
678 /* Find the dep_link with producer PRO in consumer's back_deps. */
679 FOR_EACH_DEP (con, con_list_type, sd_it, dep)
680 if (DEP_PRO (dep) == pro)
682 found_p = true;
683 break;
686 else
688 /* Find the dep_link with consumer CON in producer's forw_deps. */
689 FOR_EACH_DEP (pro, pro_list_type, sd_it, dep)
690 if (DEP_CON (dep) == con)
692 found_p = true;
693 break;
697 if (found_p)
699 if (sd_it_ptr != NULL)
700 *sd_it_ptr = sd_it;
702 return dep;
705 return NULL;
708 /* Find a dependency between producer PRO and consumer CON.
709 Use dependency [if available] to check if dependency is present at all.
710 Search through resolved dependency lists if RESOLVED_P is true.
711 If the dependency or NULL if none found. */
712 dep_t
713 sd_find_dep_between (rtx pro, rtx con, bool resolved_p)
715 if (true_dependency_cache != NULL)
716 /* Avoiding the list walk below can cut compile times dramatically
717 for some code. */
719 int elem_luid = INSN_LUID (pro);
720 int insn_luid = INSN_LUID (con);
722 gcc_assert (output_dependency_cache != NULL
723 && anti_dependency_cache != NULL);
725 if (!bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid)
726 && !bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid)
727 && !bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
728 return NULL;
731 return sd_find_dep_between_no_cache (pro, con, resolved_p, NULL);
734 /* Add or update a dependence described by DEP.
735 MEM1 and MEM2, if non-null, correspond to memory locations in case of
736 data speculation.
738 The function returns a value indicating if an old entry has been changed
739 or a new entry has been added to insn's backward deps.
741 This function merely checks if producer and consumer is the same insn
742 and doesn't create a dep in this case. Actual manipulation of
743 dependence data structures is performed in add_or_update_dep_1. */
744 static enum DEPS_ADJUST_RESULT
745 maybe_add_or_update_dep_1 (dep_t dep, bool resolved_p, rtx mem1, rtx mem2)
747 rtx elem = DEP_PRO (dep);
748 rtx insn = DEP_CON (dep);
750 gcc_assert (INSN_P (insn) && INSN_P (elem));
752 /* Don't depend an insn on itself. */
753 if (insn == elem)
755 if (current_sched_info->flags & DO_SPECULATION)
756 /* INSN has an internal dependence, which we can't overcome. */
757 HAS_INTERNAL_DEP (insn) = 1;
759 return DEP_NODEP;
762 return add_or_update_dep_1 (dep, resolved_p, mem1, mem2);
765 /* Ask dependency caches what needs to be done for dependence DEP.
766 Return DEP_CREATED if new dependence should be created and there is no
767 need to try to find one searching the dependencies lists.
768 Return DEP_PRESENT if there already is a dependence described by DEP and
769 hence nothing is to be done.
770 Return DEP_CHANGED if there already is a dependence, but it should be
771 updated to incorporate additional information from DEP. */
772 static enum DEPS_ADJUST_RESULT
773 ask_dependency_caches (dep_t dep)
775 int elem_luid = INSN_LUID (DEP_PRO (dep));
776 int insn_luid = INSN_LUID (DEP_CON (dep));
778 gcc_assert (true_dependency_cache != NULL
779 && output_dependency_cache != NULL
780 && anti_dependency_cache != NULL);
782 if (!(current_sched_info->flags & USE_DEPS_LIST))
784 enum reg_note present_dep_type;
786 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
787 present_dep_type = REG_DEP_TRUE;
788 else if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
789 present_dep_type = REG_DEP_OUTPUT;
790 else if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
791 present_dep_type = REG_DEP_ANTI;
792 else
793 /* There is no existing dep so it should be created. */
794 return DEP_CREATED;
796 if ((int) DEP_TYPE (dep) >= (int) present_dep_type)
797 /* DEP does not add anything to the existing dependence. */
798 return DEP_PRESENT;
800 else
802 ds_t present_dep_types = 0;
804 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
805 present_dep_types |= DEP_TRUE;
806 if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
807 present_dep_types |= DEP_OUTPUT;
808 if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
809 present_dep_types |= DEP_ANTI;
811 if (present_dep_types == 0)
812 /* There is no existing dep so it should be created. */
813 return DEP_CREATED;
815 if (!(current_sched_info->flags & DO_SPECULATION)
816 || !bitmap_bit_p (&spec_dependency_cache[insn_luid], elem_luid))
818 if ((present_dep_types | (DEP_STATUS (dep) & DEP_TYPES))
819 == present_dep_types)
820 /* DEP does not add anything to the existing dependence. */
821 return DEP_PRESENT;
823 else
825 /* Only true dependencies can be data speculative and
826 only anti dependencies can be control speculative. */
827 gcc_assert ((present_dep_types & (DEP_TRUE | DEP_ANTI))
828 == present_dep_types);
830 /* if (DEP is SPECULATIVE) then
831 ..we should update DEP_STATUS
832 else
833 ..we should reset existing dep to non-speculative. */
837 return DEP_CHANGED;
840 /* Set dependency caches according to DEP. */
841 static void
842 set_dependency_caches (dep_t dep)
844 int elem_luid = INSN_LUID (DEP_PRO (dep));
845 int insn_luid = INSN_LUID (DEP_CON (dep));
847 if (!(current_sched_info->flags & USE_DEPS_LIST))
849 switch (DEP_TYPE (dep))
851 case REG_DEP_TRUE:
852 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
853 break;
855 case REG_DEP_OUTPUT:
856 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
857 break;
859 case REG_DEP_ANTI:
860 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
861 break;
863 default:
864 gcc_unreachable ();
867 else
869 ds_t ds = DEP_STATUS (dep);
871 if (ds & DEP_TRUE)
872 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
873 if (ds & DEP_OUTPUT)
874 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
875 if (ds & DEP_ANTI)
876 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
878 if (ds & SPECULATIVE)
880 gcc_assert (current_sched_info->flags & DO_SPECULATION);
881 bitmap_set_bit (&spec_dependency_cache[insn_luid], elem_luid);
886 /* Type of dependence DEP have changed from OLD_TYPE. Update dependency
887 caches accordingly. */
888 static void
889 update_dependency_caches (dep_t dep, enum reg_note old_type)
891 int elem_luid = INSN_LUID (DEP_PRO (dep));
892 int insn_luid = INSN_LUID (DEP_CON (dep));
894 /* Clear corresponding cache entry because type of the link
895 may have changed. Keep them if we use_deps_list. */
896 if (!(current_sched_info->flags & USE_DEPS_LIST))
898 switch (old_type)
900 case REG_DEP_OUTPUT:
901 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
902 break;
904 case REG_DEP_ANTI:
905 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
906 break;
908 default:
909 gcc_unreachable ();
913 set_dependency_caches (dep);
916 /* Convert a dependence pointed to by SD_IT to be non-speculative. */
917 static void
918 change_spec_dep_to_hard (sd_iterator_def sd_it)
920 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
921 dep_link_t link = DEP_NODE_BACK (node);
922 dep_t dep = DEP_NODE_DEP (node);
923 rtx elem = DEP_PRO (dep);
924 rtx insn = DEP_CON (dep);
926 move_dep_link (link, INSN_SPEC_BACK_DEPS (insn), INSN_HARD_BACK_DEPS (insn));
928 DEP_STATUS (dep) &= ~SPECULATIVE;
930 if (true_dependency_cache != NULL)
931 /* Clear the cache entry. */
932 bitmap_clear_bit (&spec_dependency_cache[INSN_LUID (insn)],
933 INSN_LUID (elem));
936 /* Update DEP to incorporate information from NEW_DEP.
937 SD_IT points to DEP in case it should be moved to another list.
938 MEM1 and MEM2, if nonnull, correspond to memory locations in case if
939 data-speculative dependence should be updated. */
940 static enum DEPS_ADJUST_RESULT
941 update_dep (dep_t dep, dep_t new_dep,
942 sd_iterator_def sd_it ATTRIBUTE_UNUSED,
943 rtx mem1 ATTRIBUTE_UNUSED,
944 rtx mem2 ATTRIBUTE_UNUSED)
946 enum DEPS_ADJUST_RESULT res = DEP_PRESENT;
947 enum reg_note old_type = DEP_TYPE (dep);
949 /* If this is a more restrictive type of dependence than the
950 existing one, then change the existing dependence to this
951 type. */
952 if ((int) DEP_TYPE (new_dep) < (int) old_type)
954 DEP_TYPE (dep) = DEP_TYPE (new_dep);
955 res = DEP_CHANGED;
958 if (current_sched_info->flags & USE_DEPS_LIST)
959 /* Update DEP_STATUS. */
961 ds_t dep_status = DEP_STATUS (dep);
962 ds_t ds = DEP_STATUS (new_dep);
963 ds_t new_status = ds | dep_status;
965 if (new_status & SPECULATIVE)
966 /* Either existing dep or a dep we're adding or both are
967 speculative. */
969 if (!(ds & SPECULATIVE)
970 || !(dep_status & SPECULATIVE))
971 /* The new dep can't be speculative. */
973 new_status &= ~SPECULATIVE;
975 if (dep_status & SPECULATIVE)
976 /* The old dep was speculative, but now it
977 isn't. */
978 change_spec_dep_to_hard (sd_it);
980 else
982 /* Both are speculative. Merge probabilities. */
983 if (mem1 != NULL)
985 dw_t dw;
987 dw = estimate_dep_weak (mem1, mem2);
988 ds = set_dep_weak (ds, BEGIN_DATA, dw);
991 new_status = ds_merge (dep_status, ds);
995 ds = new_status;
997 if (dep_status != ds)
999 DEP_STATUS (dep) = ds;
1000 res = DEP_CHANGED;
1004 if (true_dependency_cache != NULL
1005 && res == DEP_CHANGED)
1006 update_dependency_caches (dep, old_type);
1008 return res;
1011 /* Add or update a dependence described by DEP.
1012 MEM1 and MEM2, if non-null, correspond to memory locations in case of
1013 data speculation.
1015 The function returns a value indicating if an old entry has been changed
1016 or a new entry has been added to insn's backward deps or nothing has
1017 been updated at all. */
1018 static enum DEPS_ADJUST_RESULT
1019 add_or_update_dep_1 (dep_t new_dep, bool resolved_p,
1020 rtx mem1 ATTRIBUTE_UNUSED, rtx mem2 ATTRIBUTE_UNUSED)
1022 bool maybe_present_p = true;
1023 bool present_p = false;
1025 gcc_assert (INSN_P (DEP_PRO (new_dep)) && INSN_P (DEP_CON (new_dep))
1026 && DEP_PRO (new_dep) != DEP_CON (new_dep));
1028 #ifdef ENABLE_CHECKING
1029 check_dep (new_dep, mem1 != NULL);
1030 #endif
1032 if (true_dependency_cache != NULL)
1034 switch (ask_dependency_caches (new_dep))
1036 case DEP_PRESENT:
1037 return DEP_PRESENT;
1039 case DEP_CHANGED:
1040 maybe_present_p = true;
1041 present_p = true;
1042 break;
1044 case DEP_CREATED:
1045 maybe_present_p = false;
1046 present_p = false;
1047 break;
1049 default:
1050 gcc_unreachable ();
1051 break;
1055 /* Check that we don't already have this dependence. */
1056 if (maybe_present_p)
1058 dep_t present_dep;
1059 sd_iterator_def sd_it;
1061 gcc_assert (true_dependency_cache == NULL || present_p);
1063 present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
1064 DEP_CON (new_dep),
1065 resolved_p, &sd_it);
1067 if (present_dep != NULL)
1068 /* We found an existing dependency between ELEM and INSN. */
1069 return update_dep (present_dep, new_dep, sd_it, mem1, mem2);
1070 else
1071 /* We didn't find a dep, it shouldn't present in the cache. */
1072 gcc_assert (!present_p);
1075 /* Might want to check one level of transitivity to save conses.
1076 This check should be done in maybe_add_or_update_dep_1.
1077 Since we made it to add_or_update_dep_1, we must create
1078 (or update) a link. */
1080 if (mem1 != NULL_RTX)
1082 gcc_assert (current_sched_info->flags & DO_SPECULATION);
1083 DEP_STATUS (new_dep) = set_dep_weak (DEP_STATUS (new_dep), BEGIN_DATA,
1084 estimate_dep_weak (mem1, mem2));
1087 sd_add_dep (new_dep, resolved_p);
1089 return DEP_CREATED;
1092 /* Initialize BACK_LIST_PTR with consumer's backward list and
1093 FORW_LIST_PTR with producer's forward list. If RESOLVED_P is true
1094 initialize with lists that hold resolved deps. */
1095 static void
1096 get_back_and_forw_lists (dep_t dep, bool resolved_p,
1097 deps_list_t *back_list_ptr,
1098 deps_list_t *forw_list_ptr)
1100 rtx con = DEP_CON (dep);
1102 if (!resolved_p)
1104 if ((current_sched_info->flags & DO_SPECULATION)
1105 && (DEP_STATUS (dep) & SPECULATIVE))
1106 *back_list_ptr = INSN_SPEC_BACK_DEPS (con);
1107 else
1108 *back_list_ptr = INSN_HARD_BACK_DEPS (con);
1110 *forw_list_ptr = INSN_FORW_DEPS (DEP_PRO (dep));
1112 else
1114 *back_list_ptr = INSN_RESOLVED_BACK_DEPS (con);
1115 *forw_list_ptr = INSN_RESOLVED_FORW_DEPS (DEP_PRO (dep));
1119 /* Add dependence described by DEP.
1120 If RESOLVED_P is true treat the dependence as a resolved one. */
1121 void
1122 sd_add_dep (dep_t dep, bool resolved_p)
1124 dep_node_t n = create_dep_node ();
1125 deps_list_t con_back_deps;
1126 deps_list_t pro_forw_deps;
1127 rtx elem = DEP_PRO (dep);
1128 rtx insn = DEP_CON (dep);
1130 gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
1132 if ((current_sched_info->flags & DO_SPECULATION)
1133 && !sched_insn_is_legitimate_for_speculation_p (insn, DEP_STATUS (dep)))
1134 DEP_STATUS (dep) &= ~SPECULATIVE;
1136 copy_dep (DEP_NODE_DEP (n), dep);
1138 get_back_and_forw_lists (dep, resolved_p, &con_back_deps, &pro_forw_deps);
1140 add_to_deps_list (DEP_NODE_BACK (n), con_back_deps);
1142 #ifdef ENABLE_CHECKING
1143 check_dep (dep, false);
1144 #endif
1146 add_to_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1148 /* If we are adding a dependency to INSN's LOG_LINKs, then note that
1149 in the bitmap caches of dependency information. */
1150 if (true_dependency_cache != NULL)
1151 set_dependency_caches (dep);
1154 /* Add or update backward dependence between INSN and ELEM
1155 with given type DEP_TYPE and dep_status DS.
1156 This function is a convenience wrapper. */
1157 enum DEPS_ADJUST_RESULT
1158 sd_add_or_update_dep (dep_t dep, bool resolved_p)
1160 return add_or_update_dep_1 (dep, resolved_p, NULL_RTX, NULL_RTX);
1163 /* Resolved dependence pointed to by SD_IT.
1164 SD_IT will advance to the next element. */
1165 void
1166 sd_resolve_dep (sd_iterator_def sd_it)
1168 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1169 dep_t dep = DEP_NODE_DEP (node);
1170 rtx pro = DEP_PRO (dep);
1171 rtx con = DEP_CON (dep);
1173 if ((current_sched_info->flags & DO_SPECULATION)
1174 && (DEP_STATUS (dep) & SPECULATIVE))
1175 move_dep_link (DEP_NODE_BACK (node), INSN_SPEC_BACK_DEPS (con),
1176 INSN_RESOLVED_BACK_DEPS (con));
1177 else
1178 move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
1179 INSN_RESOLVED_BACK_DEPS (con));
1181 move_dep_link (DEP_NODE_FORW (node), INSN_FORW_DEPS (pro),
1182 INSN_RESOLVED_FORW_DEPS (pro));
1185 /* Make TO depend on all the FROM's producers.
1186 If RESOLVED_P is true add dependencies to the resolved lists. */
1187 void
1188 sd_copy_back_deps (rtx to, rtx from, bool resolved_p)
1190 sd_list_types_def list_type;
1191 sd_iterator_def sd_it;
1192 dep_t dep;
1194 list_type = resolved_p ? SD_LIST_RES_BACK : SD_LIST_BACK;
1196 FOR_EACH_DEP (from, list_type, sd_it, dep)
1198 dep_def _new_dep, *new_dep = &_new_dep;
1200 copy_dep (new_dep, dep);
1201 DEP_CON (new_dep) = to;
1202 sd_add_dep (new_dep, resolved_p);
1206 /* Remove a dependency referred to by SD_IT.
1207 SD_IT will point to the next dependence after removal. */
1208 void
1209 sd_delete_dep (sd_iterator_def sd_it)
1211 dep_node_t n = DEP_LINK_NODE (*sd_it.linkp);
1212 dep_t dep = DEP_NODE_DEP (n);
1213 rtx pro = DEP_PRO (dep);
1214 rtx con = DEP_CON (dep);
1215 deps_list_t con_back_deps;
1216 deps_list_t pro_forw_deps;
1218 if (true_dependency_cache != NULL)
1220 int elem_luid = INSN_LUID (pro);
1221 int insn_luid = INSN_LUID (con);
1223 bitmap_clear_bit (&true_dependency_cache[insn_luid], elem_luid);
1224 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1225 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1227 if (current_sched_info->flags & DO_SPECULATION)
1228 bitmap_clear_bit (&spec_dependency_cache[insn_luid], elem_luid);
1231 get_back_and_forw_lists (dep, sd_it.resolved_p,
1232 &con_back_deps, &pro_forw_deps);
1234 remove_from_deps_list (DEP_NODE_BACK (n), con_back_deps);
1235 remove_from_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1237 delete_dep_node (n);
1240 /* Dump size of the lists. */
1241 #define DUMP_LISTS_SIZE (2)
1243 /* Dump dependencies of the lists. */
1244 #define DUMP_LISTS_DEPS (4)
1246 /* Dump all information about the lists. */
1247 #define DUMP_LISTS_ALL (DUMP_LISTS_SIZE | DUMP_LISTS_DEPS)
1249 /* Dump deps_lists of INSN specified by TYPES to DUMP.
1250 FLAGS is a bit mask specifying what information about the lists needs
1251 to be printed.
1252 If FLAGS has the very first bit set, then dump all information about
1253 the lists and propagate this bit into the callee dump functions. */
1254 static void
1255 dump_lists (FILE *dump, rtx insn, sd_list_types_def types, int flags)
1257 sd_iterator_def sd_it;
1258 dep_t dep;
1259 int all;
1261 all = (flags & 1);
1263 if (all)
1264 flags |= DUMP_LISTS_ALL;
1266 fprintf (dump, "[");
1268 if (flags & DUMP_LISTS_SIZE)
1269 fprintf (dump, "%d; ", sd_lists_size (insn, types));
1271 if (flags & DUMP_LISTS_DEPS)
1273 FOR_EACH_DEP (insn, types, sd_it, dep)
1275 dump_dep (dump, dep, dump_dep_flags | all);
1276 fprintf (dump, " ");
1281 /* Dump all information about deps_lists of INSN specified by TYPES
1282 to STDERR. */
1283 void
1284 sd_debug_lists (rtx insn, sd_list_types_def types)
1286 dump_lists (stderr, insn, types, 1);
1287 fprintf (stderr, "\n");
1290 /* A convenience wrapper to operate on an entire list. */
1292 static void
1293 add_dependence_list (rtx insn, rtx list, int uncond, enum reg_note dep_type)
1295 for (; list; list = XEXP (list, 1))
1297 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1298 add_dependence (insn, XEXP (list, 0), dep_type);
1302 /* Similar, but free *LISTP at the same time. */
1304 static void
1305 add_dependence_list_and_free (rtx insn, rtx *listp, int uncond,
1306 enum reg_note dep_type)
1308 rtx list, next;
1309 for (list = *listp, *listp = NULL; list ; list = next)
1311 next = XEXP (list, 1);
1312 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1313 add_dependence (insn, XEXP (list, 0), dep_type);
1314 free_INSN_LIST_node (list);
1318 /* Clear all dependencies for an insn. */
1319 static void
1320 delete_all_dependences (rtx insn)
1322 sd_iterator_def sd_it;
1323 dep_t dep;
1325 /* The below cycle can be optimized to clear the caches and back_deps
1326 in one call but that would provoke duplication of code from
1327 delete_dep (). */
1329 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1330 sd_iterator_cond (&sd_it, &dep);)
1331 sd_delete_dep (sd_it);
1334 /* All insns in a scheduling group except the first should only have
1335 dependencies on the previous insn in the group. So we find the
1336 first instruction in the scheduling group by walking the dependence
1337 chains backwards. Then we add the dependencies for the group to
1338 the previous nonnote insn. */
1340 static void
1341 fixup_sched_groups (rtx insn)
1343 sd_iterator_def sd_it;
1344 dep_t dep;
1345 rtx prev_nonnote;
1347 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
1349 rtx i = insn;
1350 rtx pro = DEP_PRO (dep);
1354 i = prev_nonnote_insn (i);
1356 if (pro == i)
1357 goto next_link;
1358 } while (SCHED_GROUP_P (i));
1360 if (! sched_insns_conditions_mutex_p (i, pro))
1361 add_dependence (i, pro, DEP_TYPE (dep));
1362 next_link:;
1365 delete_all_dependences (insn);
1367 prev_nonnote = prev_nonnote_insn (insn);
1368 if (BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (prev_nonnote)
1369 && ! sched_insns_conditions_mutex_p (insn, prev_nonnote))
1370 add_dependence (insn, prev_nonnote, REG_DEP_ANTI);
1373 /* Process an insn's memory dependencies. There are four kinds of
1374 dependencies:
1376 (0) read dependence: read follows read
1377 (1) true dependence: read follows write
1378 (2) output dependence: write follows write
1379 (3) anti dependence: write follows read
1381 We are careful to build only dependencies which actually exist, and
1382 use transitivity to avoid building too many links. */
1384 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1385 The MEM is a memory reference contained within INSN, which we are saving
1386 so that we can do memory aliasing on it. */
1388 static void
1389 add_insn_mem_dependence (struct deps *deps, bool read_p,
1390 rtx insn, rtx mem)
1392 rtx *insn_list;
1393 rtx *mem_list;
1394 rtx link;
1396 if (read_p)
1398 insn_list = &deps->pending_read_insns;
1399 mem_list = &deps->pending_read_mems;
1400 deps->pending_read_list_length++;
1402 else
1404 insn_list = &deps->pending_write_insns;
1405 mem_list = &deps->pending_write_mems;
1406 deps->pending_write_list_length++;
1409 link = alloc_INSN_LIST (insn, *insn_list);
1410 *insn_list = link;
1412 if (current_sched_info->use_cselib)
1414 mem = shallow_copy_rtx (mem);
1415 XEXP (mem, 0) = cselib_subst_to_values (XEXP (mem, 0));
1417 link = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
1418 *mem_list = link;
1421 /* Make a dependency between every memory reference on the pending lists
1422 and INSN, thus flushing the pending lists. FOR_READ is true if emitting
1423 dependencies for a read operation, similarly with FOR_WRITE. */
1425 static void
1426 flush_pending_lists (struct deps *deps, rtx insn, int for_read,
1427 int for_write)
1429 if (for_write)
1431 add_dependence_list_and_free (insn, &deps->pending_read_insns, 1,
1432 REG_DEP_ANTI);
1433 free_EXPR_LIST_list (&deps->pending_read_mems);
1434 deps->pending_read_list_length = 0;
1437 add_dependence_list_and_free (insn, &deps->pending_write_insns, 1,
1438 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1439 free_EXPR_LIST_list (&deps->pending_write_mems);
1440 deps->pending_write_list_length = 0;
1442 add_dependence_list_and_free (insn, &deps->last_pending_memory_flush, 1,
1443 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1444 deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
1445 deps->pending_flush_length = 1;
1448 /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
1449 The type of the reference is specified by REF and can be SET,
1450 CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE. */
1452 static void
1453 sched_analyze_reg (struct deps *deps, int regno, enum machine_mode mode,
1454 enum rtx_code ref, rtx insn)
1456 /* A hard reg in a wide mode may really be multiple registers.
1457 If so, mark all of them just like the first. */
1458 if (regno < FIRST_PSEUDO_REGISTER)
1460 int i = hard_regno_nregs[regno][mode];
1461 if (ref == SET)
1463 while (--i >= 0)
1464 SET_REGNO_REG_SET (reg_pending_sets, regno + i);
1466 else if (ref == USE)
1468 while (--i >= 0)
1469 SET_REGNO_REG_SET (reg_pending_uses, regno + i);
1471 else
1473 while (--i >= 0)
1474 SET_REGNO_REG_SET (reg_pending_clobbers, regno + i);
1478 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
1479 it does not reload. Ignore these as they have served their
1480 purpose already. */
1481 else if (regno >= deps->max_reg)
1483 enum rtx_code code = GET_CODE (PATTERN (insn));
1484 gcc_assert (code == USE || code == CLOBBER);
1487 else
1489 if (ref == SET)
1490 SET_REGNO_REG_SET (reg_pending_sets, regno);
1491 else if (ref == USE)
1492 SET_REGNO_REG_SET (reg_pending_uses, regno);
1493 else
1494 SET_REGNO_REG_SET (reg_pending_clobbers, regno);
1496 /* Pseudos that are REG_EQUIV to something may be replaced
1497 by that during reloading. We need only add dependencies for
1498 the address in the REG_EQUIV note. */
1499 if (!reload_completed && get_reg_known_equiv_p (regno))
1501 rtx t = get_reg_known_value (regno);
1502 if (MEM_P (t))
1503 sched_analyze_2 (deps, XEXP (t, 0), insn);
1506 /* Don't let it cross a call after scheduling if it doesn't
1507 already cross one. */
1508 if (REG_N_CALLS_CROSSED (regno) == 0)
1510 if (ref == USE)
1511 deps->sched_before_next_call
1512 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
1513 else
1514 add_dependence_list (insn, deps->last_function_call, 1,
1515 REG_DEP_ANTI);
1520 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
1521 rtx, X, creating all dependencies generated by the write to the
1522 destination of X, and reads of everything mentioned. */
1524 static void
1525 sched_analyze_1 (struct deps *deps, rtx x, rtx insn)
1527 rtx dest = XEXP (x, 0);
1528 enum rtx_code code = GET_CODE (x);
1530 if (dest == 0)
1531 return;
1533 if (GET_CODE (dest) == PARALLEL)
1535 int i;
1537 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1538 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
1539 sched_analyze_1 (deps,
1540 gen_rtx_CLOBBER (VOIDmode,
1541 XEXP (XVECEXP (dest, 0, i), 0)),
1542 insn);
1544 if (GET_CODE (x) == SET)
1545 sched_analyze_2 (deps, SET_SRC (x), insn);
1546 return;
1549 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
1550 || GET_CODE (dest) == ZERO_EXTRACT)
1552 if (GET_CODE (dest) == STRICT_LOW_PART
1553 || GET_CODE (dest) == ZERO_EXTRACT
1554 || df_read_modify_subreg_p (dest))
1556 /* These both read and modify the result. We must handle
1557 them as writes to get proper dependencies for following
1558 instructions. We must handle them as reads to get proper
1559 dependencies from this to previous instructions.
1560 Thus we need to call sched_analyze_2. */
1562 sched_analyze_2 (deps, XEXP (dest, 0), insn);
1564 if (GET_CODE (dest) == ZERO_EXTRACT)
1566 /* The second and third arguments are values read by this insn. */
1567 sched_analyze_2 (deps, XEXP (dest, 1), insn);
1568 sched_analyze_2 (deps, XEXP (dest, 2), insn);
1570 dest = XEXP (dest, 0);
1573 if (REG_P (dest))
1575 int regno = REGNO (dest);
1576 enum machine_mode mode = GET_MODE (dest);
1578 sched_analyze_reg (deps, regno, mode, code, insn);
1580 #ifdef STACK_REGS
1581 /* Treat all writes to a stack register as modifying the TOS. */
1582 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
1584 /* Avoid analyzing the same register twice. */
1585 if (regno != FIRST_STACK_REG)
1586 sched_analyze_reg (deps, FIRST_STACK_REG, mode, code, insn);
1587 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
1589 #endif
1591 else if (MEM_P (dest))
1593 /* Writing memory. */
1594 rtx t = dest;
1596 if (current_sched_info->use_cselib)
1598 t = shallow_copy_rtx (dest);
1599 cselib_lookup (XEXP (t, 0), Pmode, 1);
1600 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
1602 t = canon_rtx (t);
1604 if ((deps->pending_read_list_length + deps->pending_write_list_length)
1605 > MAX_PENDING_LIST_LENGTH)
1607 /* Flush all pending reads and writes to prevent the pending lists
1608 from getting any larger. Insn scheduling runs too slowly when
1609 these lists get long. When compiling GCC with itself,
1610 this flush occurs 8 times for sparc, and 10 times for m88k using
1611 the default value of 32. */
1612 flush_pending_lists (deps, insn, false, true);
1614 else
1616 rtx pending, pending_mem;
1618 pending = deps->pending_read_insns;
1619 pending_mem = deps->pending_read_mems;
1620 while (pending)
1622 if (anti_dependence (XEXP (pending_mem, 0), t)
1623 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1624 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1626 pending = XEXP (pending, 1);
1627 pending_mem = XEXP (pending_mem, 1);
1630 pending = deps->pending_write_insns;
1631 pending_mem = deps->pending_write_mems;
1632 while (pending)
1634 if (output_dependence (XEXP (pending_mem, 0), t)
1635 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1636 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1638 pending = XEXP (pending, 1);
1639 pending_mem = XEXP (pending_mem, 1);
1642 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
1643 REG_DEP_ANTI);
1645 add_insn_mem_dependence (deps, false, insn, dest);
1647 sched_analyze_2 (deps, XEXP (dest, 0), insn);
1650 /* Analyze reads. */
1651 if (GET_CODE (x) == SET)
1652 sched_analyze_2 (deps, SET_SRC (x), insn);
1655 /* Analyze the uses of memory and registers in rtx X in INSN. */
1657 static void
1658 sched_analyze_2 (struct deps *deps, rtx x, rtx insn)
1660 int i;
1661 int j;
1662 enum rtx_code code;
1663 const char *fmt;
1665 if (x == 0)
1666 return;
1668 code = GET_CODE (x);
1670 switch (code)
1672 case CONST_INT:
1673 case CONST_DOUBLE:
1674 case CONST_FIXED:
1675 case CONST_VECTOR:
1676 case SYMBOL_REF:
1677 case CONST:
1678 case LABEL_REF:
1679 /* Ignore constants. Note that we must handle CONST_DOUBLE here
1680 because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
1681 this does not mean that this insn is using cc0. */
1682 return;
1684 #ifdef HAVE_cc0
1685 case CC0:
1686 /* User of CC0 depends on immediately preceding insn. */
1687 SCHED_GROUP_P (insn) = 1;
1688 /* Don't move CC0 setter to another block (it can set up the
1689 same flag for previous CC0 users which is safe). */
1690 CANT_MOVE (prev_nonnote_insn (insn)) = 1;
1691 return;
1692 #endif
1694 case REG:
1696 int regno = REGNO (x);
1697 enum machine_mode mode = GET_MODE (x);
1699 sched_analyze_reg (deps, regno, mode, USE, insn);
1701 #ifdef STACK_REGS
1702 /* Treat all reads of a stack register as modifying the TOS. */
1703 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
1705 /* Avoid analyzing the same register twice. */
1706 if (regno != FIRST_STACK_REG)
1707 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
1708 sched_analyze_reg (deps, FIRST_STACK_REG, mode, SET, insn);
1710 #endif
1711 return;
1714 case MEM:
1716 /* Reading memory. */
1717 rtx u;
1718 rtx pending, pending_mem;
1719 rtx t = x;
1721 if (current_sched_info->use_cselib)
1723 t = shallow_copy_rtx (t);
1724 cselib_lookup (XEXP (t, 0), Pmode, 1);
1725 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
1727 t = canon_rtx (t);
1728 pending = deps->pending_read_insns;
1729 pending_mem = deps->pending_read_mems;
1730 while (pending)
1732 if (read_dependence (XEXP (pending_mem, 0), t)
1733 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1734 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1736 pending = XEXP (pending, 1);
1737 pending_mem = XEXP (pending_mem, 1);
1740 pending = deps->pending_write_insns;
1741 pending_mem = deps->pending_write_mems;
1742 while (pending)
1744 if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
1745 t, rtx_varies_p)
1746 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1748 if ((current_sched_info->flags & DO_SPECULATION)
1749 && (spec_info->mask & BEGIN_DATA))
1750 /* Create a data-speculative dependence between producer
1751 and consumer. */
1753 dep_def _dep, *dep = &_dep;
1755 init_dep_1 (dep, XEXP (pending, 0), insn, REG_DEP_TRUE,
1756 BEGIN_DATA | DEP_TRUE);
1758 maybe_add_or_update_dep_1 (dep, false,
1759 XEXP (pending_mem, 0), t);
1761 else
1762 add_dependence (insn, XEXP (pending, 0), REG_DEP_TRUE);
1765 pending = XEXP (pending, 1);
1766 pending_mem = XEXP (pending_mem, 1);
1769 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
1770 if (! JUMP_P (XEXP (u, 0)) || deps_may_trap_p (x))
1771 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1773 /* Always add these dependencies to pending_reads, since
1774 this insn may be followed by a write. */
1775 add_insn_mem_dependence (deps, true, insn, x);
1777 /* Take advantage of tail recursion here. */
1778 sched_analyze_2 (deps, XEXP (x, 0), insn);
1779 return;
1782 /* Force pending stores to memory in case a trap handler needs them. */
1783 case TRAP_IF:
1784 flush_pending_lists (deps, insn, true, false);
1785 break;
1787 case ASM_OPERANDS:
1788 case ASM_INPUT:
1789 case UNSPEC_VOLATILE:
1791 /* Traditional and volatile asm instructions must be considered to use
1792 and clobber all hard registers, all pseudo-registers and all of
1793 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
1795 Consider for instance a volatile asm that changes the fpu rounding
1796 mode. An insn should not be moved across this even if it only uses
1797 pseudo-regs because it might give an incorrectly rounded result. */
1798 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
1799 reg_pending_barrier = TRUE_BARRIER;
1801 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1802 We can not just fall through here since then we would be confused
1803 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1804 traditional asms unlike their normal usage. */
1806 if (code == ASM_OPERANDS)
1808 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
1809 sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
1810 return;
1812 break;
1815 case PRE_DEC:
1816 case POST_DEC:
1817 case PRE_INC:
1818 case POST_INC:
1819 /* These both read and modify the result. We must handle them as writes
1820 to get proper dependencies for following instructions. We must handle
1821 them as reads to get proper dependencies from this to previous
1822 instructions. Thus we need to pass them to both sched_analyze_1
1823 and sched_analyze_2. We must call sched_analyze_2 first in order
1824 to get the proper antecedent for the read. */
1825 sched_analyze_2 (deps, XEXP (x, 0), insn);
1826 sched_analyze_1 (deps, x, insn);
1827 return;
1829 case POST_MODIFY:
1830 case PRE_MODIFY:
1831 /* op0 = op0 + op1 */
1832 sched_analyze_2 (deps, XEXP (x, 0), insn);
1833 sched_analyze_2 (deps, XEXP (x, 1), insn);
1834 sched_analyze_1 (deps, x, insn);
1835 return;
1837 default:
1838 break;
1841 /* Other cases: walk the insn. */
1842 fmt = GET_RTX_FORMAT (code);
1843 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1845 if (fmt[i] == 'e')
1846 sched_analyze_2 (deps, XEXP (x, i), insn);
1847 else if (fmt[i] == 'E')
1848 for (j = 0; j < XVECLEN (x, i); j++)
1849 sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
1853 /* Analyze an INSN with pattern X to find all dependencies. */
1855 static void
1856 sched_analyze_insn (struct deps *deps, rtx x, rtx insn)
1858 RTX_CODE code = GET_CODE (x);
1859 rtx link;
1860 unsigned i;
1861 reg_set_iterator rsi;
1863 if (code == COND_EXEC)
1865 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
1867 /* ??? Should be recording conditions so we reduce the number of
1868 false dependencies. */
1869 x = COND_EXEC_CODE (x);
1870 code = GET_CODE (x);
1872 if (code == SET || code == CLOBBER)
1874 sched_analyze_1 (deps, x, insn);
1876 /* Bare clobber insns are used for letting life analysis, reg-stack
1877 and others know that a value is dead. Depend on the last call
1878 instruction so that reg-stack won't get confused. */
1879 if (code == CLOBBER)
1880 add_dependence_list (insn, deps->last_function_call, 1, REG_DEP_OUTPUT);
1882 else if (code == PARALLEL)
1884 for (i = XVECLEN (x, 0); i--;)
1886 rtx sub = XVECEXP (x, 0, i);
1887 code = GET_CODE (sub);
1889 if (code == COND_EXEC)
1891 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
1892 sub = COND_EXEC_CODE (sub);
1893 code = GET_CODE (sub);
1895 if (code == SET || code == CLOBBER)
1896 sched_analyze_1 (deps, sub, insn);
1897 else
1898 sched_analyze_2 (deps, sub, insn);
1901 else
1902 sched_analyze_2 (deps, x, insn);
1904 /* Mark registers CLOBBERED or used by called function. */
1905 if (CALL_P (insn))
1907 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1909 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
1910 sched_analyze_1 (deps, XEXP (link, 0), insn);
1911 else
1912 sched_analyze_2 (deps, XEXP (link, 0), insn);
1914 if (find_reg_note (insn, REG_SETJMP, NULL))
1915 reg_pending_barrier = MOVE_BARRIER;
1918 if (JUMP_P (insn))
1920 rtx next;
1921 next = next_nonnote_insn (insn);
1922 if (next && BARRIER_P (next))
1923 reg_pending_barrier = MOVE_BARRIER;
1924 else
1926 rtx pending, pending_mem;
1927 regset_head tmp_uses, tmp_sets;
1928 INIT_REG_SET (&tmp_uses);
1929 INIT_REG_SET (&tmp_sets);
1931 (*current_sched_info->compute_jump_reg_dependencies)
1932 (insn, &deps->reg_conditional_sets, &tmp_uses, &tmp_sets);
1933 /* Make latency of jump equal to 0 by using anti-dependence. */
1934 EXECUTE_IF_SET_IN_REG_SET (&tmp_uses, 0, i, rsi)
1936 struct deps_reg *reg_last = &deps->reg_last[i];
1937 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI);
1938 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI);
1939 reg_last->uses_length++;
1940 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1942 IOR_REG_SET (reg_pending_sets, &tmp_sets);
1944 CLEAR_REG_SET (&tmp_uses);
1945 CLEAR_REG_SET (&tmp_sets);
1947 /* All memory writes and volatile reads must happen before the
1948 jump. Non-volatile reads must happen before the jump iff
1949 the result is needed by the above register used mask. */
1951 pending = deps->pending_write_insns;
1952 pending_mem = deps->pending_write_mems;
1953 while (pending)
1955 if (! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1956 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1957 pending = XEXP (pending, 1);
1958 pending_mem = XEXP (pending_mem, 1);
1961 pending = deps->pending_read_insns;
1962 pending_mem = deps->pending_read_mems;
1963 while (pending)
1965 if (MEM_VOLATILE_P (XEXP (pending_mem, 0))
1966 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1967 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1968 pending = XEXP (pending, 1);
1969 pending_mem = XEXP (pending_mem, 1);
1972 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
1973 REG_DEP_ANTI);
1977 /* If this instruction can throw an exception, then moving it changes
1978 where block boundaries fall. This is mighty confusing elsewhere.
1979 Therefore, prevent such an instruction from being moved. Same for
1980 non-jump instructions that define block boundaries.
1981 ??? Unclear whether this is still necessary in EBB mode. If not,
1982 add_branch_dependences should be adjusted for RGN mode instead. */
1983 if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
1984 || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
1985 reg_pending_barrier = MOVE_BARRIER;
1987 /* Add register dependencies for insn.
1988 If the current insn is conditional, we can't free any of the lists. */
1989 if (sched_get_condition (insn))
1991 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
1993 struct deps_reg *reg_last = &deps->reg_last[i];
1994 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
1995 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
1996 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1997 reg_last->uses_length++;
1999 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
2001 struct deps_reg *reg_last = &deps->reg_last[i];
2002 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2003 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2004 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
2005 reg_last->clobbers_length++;
2007 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
2009 struct deps_reg *reg_last = &deps->reg_last[i];
2010 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2011 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT);
2012 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2013 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2014 SET_REGNO_REG_SET (&deps->reg_conditional_sets, i);
2017 else
2019 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
2021 struct deps_reg *reg_last = &deps->reg_last[i];
2022 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
2023 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
2024 reg_last->uses_length++;
2025 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2027 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
2029 struct deps_reg *reg_last = &deps->reg_last[i];
2030 if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
2031 || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
2033 add_dependence_list_and_free (insn, &reg_last->sets, 0,
2034 REG_DEP_OUTPUT);
2035 add_dependence_list_and_free (insn, &reg_last->uses, 0,
2036 REG_DEP_ANTI);
2037 add_dependence_list_and_free (insn, &reg_last->clobbers, 0,
2038 REG_DEP_OUTPUT);
2039 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2040 reg_last->clobbers_length = 0;
2041 reg_last->uses_length = 0;
2043 else
2045 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2046 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2048 reg_last->clobbers_length++;
2049 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
2051 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
2053 struct deps_reg *reg_last = &deps->reg_last[i];
2054 add_dependence_list_and_free (insn, &reg_last->sets, 0,
2055 REG_DEP_OUTPUT);
2056 add_dependence_list_and_free (insn, &reg_last->clobbers, 0,
2057 REG_DEP_OUTPUT);
2058 add_dependence_list_and_free (insn, &reg_last->uses, 0,
2059 REG_DEP_ANTI);
2060 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2061 reg_last->uses_length = 0;
2062 reg_last->clobbers_length = 0;
2063 CLEAR_REGNO_REG_SET (&deps->reg_conditional_sets, i);
2067 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
2068 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
2069 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
2071 CLEAR_REG_SET (reg_pending_uses);
2072 CLEAR_REG_SET (reg_pending_clobbers);
2073 CLEAR_REG_SET (reg_pending_sets);
2075 /* Add dependencies if a scheduling barrier was found. */
2076 if (reg_pending_barrier)
2078 /* In the case of barrier the most added dependencies are not
2079 real, so we use anti-dependence here. */
2080 if (sched_get_condition (insn))
2082 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2084 struct deps_reg *reg_last = &deps->reg_last[i];
2085 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2086 add_dependence_list
2087 (insn, reg_last->sets, 0,
2088 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2089 add_dependence_list
2090 (insn, reg_last->clobbers, 0,
2091 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2094 else
2096 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2098 struct deps_reg *reg_last = &deps->reg_last[i];
2099 add_dependence_list_and_free (insn, &reg_last->uses, 0,
2100 REG_DEP_ANTI);
2101 add_dependence_list_and_free
2102 (insn, &reg_last->sets, 0,
2103 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2104 add_dependence_list_and_free
2105 (insn, &reg_last->clobbers, 0,
2106 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2107 reg_last->uses_length = 0;
2108 reg_last->clobbers_length = 0;
2112 for (i = 0; i < (unsigned)deps->max_reg; i++)
2114 struct deps_reg *reg_last = &deps->reg_last[i];
2115 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2116 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
2119 flush_pending_lists (deps, insn, true, true);
2120 CLEAR_REG_SET (&deps->reg_conditional_sets);
2121 reg_pending_barrier = NOT_A_BARRIER;
2124 /* If we are currently in a libcall scheduling group, then mark the
2125 current insn as being in a scheduling group and that it can not
2126 be moved into a different basic block. */
2128 if (deps->libcall_block_tail_insn)
2130 SCHED_GROUP_P (insn) = 1;
2131 CANT_MOVE (insn) = 1;
2134 /* If a post-call group is still open, see if it should remain so.
2135 This insn must be a simple move of a hard reg to a pseudo or
2136 vice-versa.
2138 We must avoid moving these insns for correctness on
2139 SMALL_REGISTER_CLASS machines, and for special registers like
2140 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
2141 hard regs for all targets. */
2143 if (deps->in_post_call_group_p)
2145 rtx tmp, set = single_set (insn);
2146 int src_regno, dest_regno;
2148 if (set == NULL)
2149 goto end_call_group;
2151 tmp = SET_DEST (set);
2152 if (GET_CODE (tmp) == SUBREG)
2153 tmp = SUBREG_REG (tmp);
2154 if (REG_P (tmp))
2155 dest_regno = REGNO (tmp);
2156 else
2157 goto end_call_group;
2159 tmp = SET_SRC (set);
2160 if (GET_CODE (tmp) == SUBREG)
2161 tmp = SUBREG_REG (tmp);
2162 if ((GET_CODE (tmp) == PLUS
2163 || GET_CODE (tmp) == MINUS)
2164 && REG_P (XEXP (tmp, 0))
2165 && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
2166 && dest_regno == STACK_POINTER_REGNUM)
2167 src_regno = STACK_POINTER_REGNUM;
2168 else if (REG_P (tmp))
2169 src_regno = REGNO (tmp);
2170 else
2171 goto end_call_group;
2173 if (src_regno < FIRST_PSEUDO_REGISTER
2174 || dest_regno < FIRST_PSEUDO_REGISTER)
2176 if (deps->in_post_call_group_p == post_call_initial)
2177 deps->in_post_call_group_p = post_call;
2179 SCHED_GROUP_P (insn) = 1;
2180 CANT_MOVE (insn) = 1;
2182 else
2184 end_call_group:
2185 deps->in_post_call_group_p = not_post_call;
2189 /* Fixup the dependencies in the sched group. */
2190 if (SCHED_GROUP_P (insn))
2191 fixup_sched_groups (insn);
2193 if ((current_sched_info->flags & DO_SPECULATION)
2194 && !sched_insn_is_legitimate_for_speculation_p (insn, 0))
2195 /* INSN has an internal dependency (e.g. r14 = [r14]) and thus cannot
2196 be speculated. */
2198 sd_iterator_def sd_it;
2199 dep_t dep;
2201 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
2202 sd_iterator_cond (&sd_it, &dep);)
2203 change_spec_dep_to_hard (sd_it);
2207 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
2208 dependencies for each insn. */
2210 void
2211 sched_analyze (struct deps *deps, rtx head, rtx tail)
2213 rtx insn;
2215 if (current_sched_info->use_cselib)
2216 cselib_init (true);
2218 /* Before reload, if the previous block ended in a call, show that
2219 we are inside a post-call group, so as to keep the lifetimes of
2220 hard registers correct. */
2221 if (! reload_completed && !LABEL_P (head))
2223 insn = prev_nonnote_insn (head);
2224 if (insn && CALL_P (insn))
2225 deps->in_post_call_group_p = post_call_initial;
2227 for (insn = head;; insn = NEXT_INSN (insn))
2229 rtx link, end_seq, r0, set;
2231 if (INSN_P (insn))
2233 /* And initialize deps_lists. */
2234 sd_init_insn (insn);
2237 if (NONJUMP_INSN_P (insn) || JUMP_P (insn))
2239 /* Make each JUMP_INSN a scheduling barrier for memory
2240 references. */
2241 if (JUMP_P (insn))
2243 /* Keep the list a reasonable size. */
2244 if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
2245 flush_pending_lists (deps, insn, true, true);
2246 else
2247 deps->last_pending_memory_flush
2248 = alloc_INSN_LIST (insn, deps->last_pending_memory_flush);
2250 sched_analyze_insn (deps, PATTERN (insn), insn);
2252 else if (CALL_P (insn))
2254 int i;
2256 CANT_MOVE (insn) = 1;
2258 if (find_reg_note (insn, REG_SETJMP, NULL))
2260 /* This is setjmp. Assume that all registers, not just
2261 hard registers, may be clobbered by this call. */
2262 reg_pending_barrier = MOVE_BARRIER;
2264 else
2266 HARD_REG_SET clobbered_regs;
2268 get_call_invalidated_used_regs (insn, &clobbered_regs, true);
2269 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2270 /* A call may read and modify global register variables. */
2271 if (global_regs[i])
2273 SET_REGNO_REG_SET (reg_pending_sets, i);
2274 SET_REGNO_REG_SET (reg_pending_uses, i);
2276 /* Other call-clobbered hard regs may be clobbered.
2277 Since we only have a choice between 'might be clobbered'
2278 and 'definitely not clobbered', we must include all
2279 partly call-clobbered registers here. */
2280 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
2281 || TEST_HARD_REG_BIT (clobbered_regs, i))
2282 SET_REGNO_REG_SET (reg_pending_clobbers, i);
2283 /* We don't know what set of fixed registers might be used
2284 by the function, but it is certain that the stack pointer
2285 is among them, but be conservative. */
2286 else if (fixed_regs[i])
2287 SET_REGNO_REG_SET (reg_pending_uses, i);
2288 /* The frame pointer is normally not used by the function
2289 itself, but by the debugger. */
2290 /* ??? MIPS o32 is an exception. It uses the frame pointer
2291 in the macro expansion of jal but does not represent this
2292 fact in the call_insn rtl. */
2293 else if (i == FRAME_POINTER_REGNUM
2294 || (i == HARD_FRAME_POINTER_REGNUM
2295 && (! reload_completed || frame_pointer_needed)))
2296 SET_REGNO_REG_SET (reg_pending_uses, i);
2299 /* For each insn which shouldn't cross a call, add a dependence
2300 between that insn and this call insn. */
2301 add_dependence_list_and_free (insn, &deps->sched_before_next_call, 1,
2302 REG_DEP_ANTI);
2304 sched_analyze_insn (deps, PATTERN (insn), insn);
2306 /* In the absence of interprocedural alias analysis, we must flush
2307 all pending reads and writes, and start new dependencies starting
2308 from here. But only flush writes for constant calls (which may
2309 be passed a pointer to something we haven't written yet). */
2310 flush_pending_lists (deps, insn, true, !CONST_OR_PURE_CALL_P (insn));
2312 /* Remember the last function call for limiting lifetimes. */
2313 free_INSN_LIST_list (&deps->last_function_call);
2314 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
2316 /* Before reload, begin a post-call group, so as to keep the
2317 lifetimes of hard registers correct. */
2318 if (! reload_completed)
2319 deps->in_post_call_group_p = post_call;
2322 /* EH_REGION insn notes can not appear until well after we complete
2323 scheduling. */
2324 if (NOTE_P (insn))
2325 gcc_assert (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
2326 && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END);
2328 if (current_sched_info->use_cselib)
2329 cselib_process_insn (insn);
2331 /* Now that we have completed handling INSN, check and see if it is
2332 a CLOBBER beginning a libcall block. If it is, record the
2333 end of the libcall sequence.
2335 We want to schedule libcall blocks as a unit before reload. While
2336 this restricts scheduling, it preserves the meaning of a libcall
2337 block.
2339 As a side effect, we may get better code due to decreased register
2340 pressure as well as less chance of a foreign insn appearing in
2341 a libcall block. */
2342 if (!reload_completed
2343 /* Note we may have nested libcall sequences. We only care about
2344 the outermost libcall sequence. */
2345 && deps->libcall_block_tail_insn == 0
2346 /* The sequence must start with a clobber of a register. */
2347 && NONJUMP_INSN_P (insn)
2348 && GET_CODE (PATTERN (insn)) == CLOBBER
2349 && (r0 = XEXP (PATTERN (insn), 0), REG_P (r0))
2350 && REG_P (XEXP (PATTERN (insn), 0))
2351 /* The CLOBBER must also have a REG_LIBCALL note attached. */
2352 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
2353 && (end_seq = XEXP (link, 0)) != 0
2354 /* The insn referenced by the REG_LIBCALL note must be a
2355 simple nop copy with the same destination as the register
2356 mentioned in the clobber. */
2357 && (set = single_set (end_seq)) != 0
2358 && SET_DEST (set) == r0 && SET_SRC (set) == r0
2359 /* And finally the insn referenced by the REG_LIBCALL must
2360 also contain a REG_EQUAL note and a REG_RETVAL note. */
2361 && find_reg_note (end_seq, REG_EQUAL, NULL_RTX) != 0
2362 && find_reg_note (end_seq, REG_RETVAL, NULL_RTX) != 0)
2363 deps->libcall_block_tail_insn = XEXP (link, 0);
2365 /* If we have reached the end of a libcall block, then close the
2366 block. */
2367 if (deps->libcall_block_tail_insn == insn)
2368 deps->libcall_block_tail_insn = 0;
2370 if (insn == tail)
2372 if (current_sched_info->use_cselib)
2373 cselib_finish ();
2374 return;
2377 gcc_unreachable ();
2380 /* Helper for sched_free_deps ().
2381 Delete INSN's (RESOLVED_P) backward dependencies. */
2382 static void
2383 delete_dep_nodes_in_back_deps (rtx insn, bool resolved_p)
2385 sd_iterator_def sd_it;
2386 dep_t dep;
2387 sd_list_types_def types;
2389 if (resolved_p)
2390 types = SD_LIST_RES_BACK;
2391 else
2392 types = SD_LIST_BACK;
2394 for (sd_it = sd_iterator_start (insn, types);
2395 sd_iterator_cond (&sd_it, &dep);)
2397 dep_link_t link = *sd_it.linkp;
2398 dep_node_t node = DEP_LINK_NODE (link);
2399 deps_list_t back_list;
2400 deps_list_t forw_list;
2402 get_back_and_forw_lists (dep, resolved_p, &back_list, &forw_list);
2403 remove_from_deps_list (link, back_list);
2404 delete_dep_node (node);
2408 /* Delete (RESOLVED_P) dependencies between HEAD and TAIL together with
2409 deps_lists. */
2410 void
2411 sched_free_deps (rtx head, rtx tail, bool resolved_p)
2413 rtx insn;
2414 rtx next_tail = NEXT_INSN (tail);
2416 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
2417 if (INSN_P (insn) && INSN_LUID (insn) > 0)
2419 /* Clear resolved back deps together with its dep_nodes. */
2420 delete_dep_nodes_in_back_deps (insn, resolved_p);
2422 /* Clear forward deps and leave the dep_nodes to the
2423 corresponding back_deps list. */
2424 if (resolved_p)
2425 clear_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
2426 else
2427 clear_deps_list (INSN_FORW_DEPS (insn));
2429 sd_finish_insn (insn);
2433 /* Initialize variables for region data dependence analysis.
2434 n_bbs is the number of region blocks. */
2436 void
2437 init_deps (struct deps *deps)
2439 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
2441 deps->max_reg = max_reg;
2442 deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
2443 INIT_REG_SET (&deps->reg_last_in_use);
2444 INIT_REG_SET (&deps->reg_conditional_sets);
2446 deps->pending_read_insns = 0;
2447 deps->pending_read_mems = 0;
2448 deps->pending_write_insns = 0;
2449 deps->pending_write_mems = 0;
2450 deps->pending_read_list_length = 0;
2451 deps->pending_write_list_length = 0;
2452 deps->pending_flush_length = 0;
2453 deps->last_pending_memory_flush = 0;
2454 deps->last_function_call = 0;
2455 deps->sched_before_next_call = 0;
2456 deps->in_post_call_group_p = not_post_call;
2457 deps->libcall_block_tail_insn = 0;
2460 /* Free insn lists found in DEPS. */
2462 void
2463 free_deps (struct deps *deps)
2465 unsigned i;
2466 reg_set_iterator rsi;
2468 free_INSN_LIST_list (&deps->pending_read_insns);
2469 free_EXPR_LIST_list (&deps->pending_read_mems);
2470 free_INSN_LIST_list (&deps->pending_write_insns);
2471 free_EXPR_LIST_list (&deps->pending_write_mems);
2472 free_INSN_LIST_list (&deps->last_pending_memory_flush);
2474 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
2475 times. For a testcase with 42000 regs and 8000 small basic blocks,
2476 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
2477 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2479 struct deps_reg *reg_last = &deps->reg_last[i];
2480 if (reg_last->uses)
2481 free_INSN_LIST_list (&reg_last->uses);
2482 if (reg_last->sets)
2483 free_INSN_LIST_list (&reg_last->sets);
2484 if (reg_last->clobbers)
2485 free_INSN_LIST_list (&reg_last->clobbers);
2487 CLEAR_REG_SET (&deps->reg_last_in_use);
2488 CLEAR_REG_SET (&deps->reg_conditional_sets);
2490 free (deps->reg_last);
2493 /* If it is profitable to use them, initialize caches for tracking
2494 dependency information. LUID is the number of insns to be scheduled,
2495 it is used in the estimate of profitability. */
2497 void
2498 init_dependency_caches (int luid)
2500 /* Average number of insns in the basic block.
2501 '+ 1' is used to make it nonzero. */
2502 int insns_in_block = luid / n_basic_blocks + 1;
2504 /* ?!? We could save some memory by computing a per-region luid mapping
2505 which could reduce both the number of vectors in the cache and the size
2506 of each vector. Instead we just avoid the cache entirely unless the
2507 average number of instructions in a basic block is very high. See
2508 the comment before the declaration of true_dependency_cache for
2509 what we consider "very high". */
2510 if (insns_in_block > 100 * 5)
2512 cache_size = 0;
2513 extend_dependency_caches (luid, true);
2516 dl_pool = create_alloc_pool ("deps_list", sizeof (struct _deps_list),
2517 /* Allocate lists for one block at a time. */
2518 insns_in_block);
2520 dn_pool = create_alloc_pool ("dep_node", sizeof (struct _dep_node),
2521 /* Allocate nodes for one block at a time.
2522 We assume that average insn has
2523 5 producers. */
2524 5 * insns_in_block);
2527 /* Create or extend (depending on CREATE_P) dependency caches to
2528 size N. */
2529 void
2530 extend_dependency_caches (int n, bool create_p)
2532 if (create_p || true_dependency_cache)
2534 int i, luid = cache_size + n;
2536 true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
2537 luid);
2538 output_dependency_cache = XRESIZEVEC (bitmap_head,
2539 output_dependency_cache, luid);
2540 anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
2541 luid);
2543 if (current_sched_info->flags & DO_SPECULATION)
2544 spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
2545 luid);
2547 for (i = cache_size; i < luid; i++)
2549 bitmap_initialize (&true_dependency_cache[i], 0);
2550 bitmap_initialize (&output_dependency_cache[i], 0);
2551 bitmap_initialize (&anti_dependency_cache[i], 0);
2553 if (current_sched_info->flags & DO_SPECULATION)
2554 bitmap_initialize (&spec_dependency_cache[i], 0);
2556 cache_size = luid;
2560 /* Free the caches allocated in init_dependency_caches. */
2562 void
2563 free_dependency_caches (void)
2565 gcc_assert (deps_pools_are_empty_p ());
2566 free_alloc_pool_if_empty (&dn_pool);
2567 free_alloc_pool_if_empty (&dl_pool);
2568 gcc_assert (dn_pool == NULL && dl_pool == NULL);
2570 if (true_dependency_cache)
2572 int i;
2574 for (i = 0; i < cache_size; i++)
2576 bitmap_clear (&true_dependency_cache[i]);
2577 bitmap_clear (&output_dependency_cache[i]);
2578 bitmap_clear (&anti_dependency_cache[i]);
2580 if (current_sched_info->flags & DO_SPECULATION)
2581 bitmap_clear (&spec_dependency_cache[i]);
2583 free (true_dependency_cache);
2584 true_dependency_cache = NULL;
2585 free (output_dependency_cache);
2586 output_dependency_cache = NULL;
2587 free (anti_dependency_cache);
2588 anti_dependency_cache = NULL;
2590 if (current_sched_info->flags & DO_SPECULATION)
2592 free (spec_dependency_cache);
2593 spec_dependency_cache = NULL;
2598 /* Initialize some global variables needed by the dependency analysis
2599 code. */
2601 void
2602 init_deps_global (void)
2604 reg_pending_sets = ALLOC_REG_SET (&reg_obstack);
2605 reg_pending_clobbers = ALLOC_REG_SET (&reg_obstack);
2606 reg_pending_uses = ALLOC_REG_SET (&reg_obstack);
2607 reg_pending_barrier = NOT_A_BARRIER;
2610 /* Free everything used by the dependency analysis code. */
2612 void
2613 finish_deps_global (void)
2615 FREE_REG_SET (reg_pending_sets);
2616 FREE_REG_SET (reg_pending_clobbers);
2617 FREE_REG_SET (reg_pending_uses);
2620 /* Estimate the weakness of dependence between MEM1 and MEM2. */
2621 static dw_t
2622 estimate_dep_weak (rtx mem1, rtx mem2)
2624 rtx r1, r2;
2626 if (mem1 == mem2)
2627 /* MEMs are the same - don't speculate. */
2628 return MIN_DEP_WEAK;
2630 r1 = XEXP (mem1, 0);
2631 r2 = XEXP (mem2, 0);
2633 if (r1 == r2
2634 || (REG_P (r1) && REG_P (r2)
2635 && REGNO (r1) == REGNO (r2)))
2636 /* Again, MEMs are the same. */
2637 return MIN_DEP_WEAK;
2638 else if ((REG_P (r1) && !REG_P (r2))
2639 || (!REG_P (r1) && REG_P (r2)))
2640 /* Different addressing modes - reason to be more speculative,
2641 than usual. */
2642 return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
2643 else
2644 /* We can't say anything about the dependence. */
2645 return UNCERTAIN_DEP_WEAK;
2648 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
2649 This function can handle same INSN and ELEM (INSN == ELEM).
2650 It is a convenience wrapper. */
2651 void
2652 add_dependence (rtx insn, rtx elem, enum reg_note dep_type)
2654 dep_def _dep, *dep = &_dep;
2656 init_dep (dep, elem, insn, dep_type);
2657 maybe_add_or_update_dep_1 (dep, false, NULL_RTX, NULL_RTX);
2660 /* Return weakness of speculative type TYPE in the dep_status DS. */
2661 static dw_t
2662 get_dep_weak_1 (ds_t ds, ds_t type)
2664 ds = ds & type;
2665 switch (type)
2667 case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
2668 case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
2669 case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
2670 case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
2671 default: gcc_unreachable ();
2674 return (dw_t) ds;
2677 /* Return weakness of speculative type TYPE in the dep_status DS. */
2678 dw_t
2679 get_dep_weak (ds_t ds, ds_t type)
2681 dw_t dw = get_dep_weak_1 (ds, type);
2683 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
2685 return dw;
2688 /* Return the dep_status, which has the same parameters as DS, except for
2689 speculative type TYPE, that will have weakness DW. */
2690 ds_t
2691 set_dep_weak (ds_t ds, ds_t type, dw_t dw)
2693 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
2695 ds &= ~type;
2696 switch (type)
2698 case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
2699 case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
2700 case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
2701 case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
2702 default: gcc_unreachable ();
2704 return ds;
2707 /* Return the join of two dep_statuses DS1 and DS2. */
2708 ds_t
2709 ds_merge (ds_t ds1, ds_t ds2)
2711 ds_t ds, t;
2713 gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
2715 ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
2717 t = FIRST_SPEC_TYPE;
2720 if ((ds1 & t) && !(ds2 & t))
2721 ds |= ds1 & t;
2722 else if (!(ds1 & t) && (ds2 & t))
2723 ds |= ds2 & t;
2724 else if ((ds1 & t) && (ds2 & t))
2726 ds_t dw;
2728 dw = ((ds_t) get_dep_weak (ds1, t)) * ((ds_t) get_dep_weak (ds2, t));
2729 dw /= MAX_DEP_WEAK;
2730 if (dw < MIN_DEP_WEAK)
2731 dw = MIN_DEP_WEAK;
2733 ds = set_dep_weak (ds, t, (dw_t) dw);
2736 if (t == LAST_SPEC_TYPE)
2737 break;
2738 t <<= SPEC_TYPE_SHIFT;
2740 while (1);
2742 return ds;
2745 /* Dump information about the dependence status S. */
2746 static void
2747 dump_ds (FILE *f, ds_t s)
2749 fprintf (f, "{");
2751 if (s & BEGIN_DATA)
2752 fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak_1 (s, BEGIN_DATA));
2753 if (s & BE_IN_DATA)
2754 fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak_1 (s, BE_IN_DATA));
2755 if (s & BEGIN_CONTROL)
2756 fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak_1 (s, BEGIN_CONTROL));
2757 if (s & BE_IN_CONTROL)
2758 fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak_1 (s, BE_IN_CONTROL));
2760 if (s & HARD_DEP)
2761 fprintf (f, "HARD_DEP; ");
2763 if (s & DEP_TRUE)
2764 fprintf (f, "DEP_TRUE; ");
2765 if (s & DEP_ANTI)
2766 fprintf (f, "DEP_ANTI; ");
2767 if (s & DEP_OUTPUT)
2768 fprintf (f, "DEP_OUTPUT; ");
2770 fprintf (f, "}");
2773 void
2774 debug_ds (ds_t s)
2776 dump_ds (stderr, s);
2777 fprintf (stderr, "\n");
2780 #ifdef ENABLE_CHECKING
2781 /* Verify that dependence type and status are consistent.
2782 If RELAXED_P is true, then skip dep_weakness checks. */
2783 static void
2784 check_dep (dep_t dep, bool relaxed_p)
2786 enum reg_note dt = DEP_TYPE (dep);
2787 ds_t ds = DEP_STATUS (dep);
2789 gcc_assert (DEP_PRO (dep) != DEP_CON (dep));
2791 if (!(current_sched_info->flags & USE_DEPS_LIST))
2793 gcc_assert (ds == -1);
2794 return;
2797 /* Check that dependence type contains the same bits as the status. */
2798 if (dt == REG_DEP_TRUE)
2799 gcc_assert (ds & DEP_TRUE);
2800 else if (dt == REG_DEP_OUTPUT)
2801 gcc_assert ((ds & DEP_OUTPUT)
2802 && !(ds & DEP_TRUE));
2803 else
2804 gcc_assert ((dt == REG_DEP_ANTI)
2805 && (ds & DEP_ANTI)
2806 && !(ds & (DEP_OUTPUT | DEP_TRUE)));
2808 /* HARD_DEP can not appear in dep_status of a link. */
2809 gcc_assert (!(ds & HARD_DEP));
2811 /* Check that dependence status is set correctly when speculation is not
2812 supported. */
2813 if (!(current_sched_info->flags & DO_SPECULATION))
2814 gcc_assert (!(ds & SPECULATIVE));
2815 else if (ds & SPECULATIVE)
2817 if (!relaxed_p)
2819 ds_t type = FIRST_SPEC_TYPE;
2821 /* Check that dependence weakness is in proper range. */
2824 if (ds & type)
2825 get_dep_weak (ds, type);
2827 if (type == LAST_SPEC_TYPE)
2828 break;
2829 type <<= SPEC_TYPE_SHIFT;
2831 while (1);
2834 if (ds & BEGIN_SPEC)
2836 /* Only true dependence can be data speculative. */
2837 if (ds & BEGIN_DATA)
2838 gcc_assert (ds & DEP_TRUE);
2840 /* Control dependencies in the insn scheduler are represented by
2841 anti-dependencies, therefore only anti dependence can be
2842 control speculative. */
2843 if (ds & BEGIN_CONTROL)
2844 gcc_assert (ds & DEP_ANTI);
2846 else
2848 /* Subsequent speculations should resolve true dependencies. */
2849 gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
2852 /* Check that true and anti dependencies can't have other speculative
2853 statuses. */
2854 if (ds & DEP_TRUE)
2855 gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
2856 /* An output dependence can't be speculative at all. */
2857 gcc_assert (!(ds & DEP_OUTPUT));
2858 if (ds & DEP_ANTI)
2859 gcc_assert (ds & BEGIN_CONTROL);
2862 #endif /* ENABLE_CHECKING */
2864 #endif /* INSN_SCHEDULING */