ChangeLog/libgcc
[official-gcc.git] / gcc / sched-deps.c
blobcf63a91cfd3aa180fa61d5a41d8e96d756789e9b
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 2, 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 COPYING. If not, write to the Free
23 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 02110-1301, USA. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "toplev.h"
31 #include "rtl.h"
32 #include "tm_p.h"
33 #include "hard-reg-set.h"
34 #include "regs.h"
35 #include "function.h"
36 #include "flags.h"
37 #include "insn-config.h"
38 #include "insn-attr.h"
39 #include "except.h"
40 #include "toplev.h"
41 #include "recog.h"
42 #include "sched-int.h"
43 #include "params.h"
44 #include "cselib.h"
46 #ifdef ENABLE_CHECKING
47 #define CHECK (true)
48 #else
49 #define CHECK (false)
50 #endif
52 /* Return the major type present in the DS. */
53 enum reg_note
54 ds_to_dk (ds_t ds)
56 if (ds & DEP_TRUE)
57 return REG_DEP_TRUE;
59 if (ds & DEP_OUTPUT)
60 return REG_DEP_OUTPUT;
62 gcc_assert (ds & DEP_ANTI);
64 return REG_DEP_ANTI;
67 /* Return equivalent dep_status. */
68 ds_t
69 dk_to_ds (enum reg_note dk)
71 switch (dk)
73 case REG_DEP_TRUE:
74 return DEP_TRUE;
76 case REG_DEP_OUTPUT:
77 return DEP_OUTPUT;
79 default:
80 gcc_assert (dk == REG_DEP_ANTI);
81 return DEP_ANTI;
85 /* Functions to operate with dependence information container - dep_t. */
87 /* Init DEP with the arguments. */
88 static void
89 init_dep_1 (dep_t dep, rtx pro, rtx con, enum reg_note kind, ds_t ds)
91 DEP_PRO (dep) = pro;
92 DEP_CON (dep) = con;
93 DEP_KIND (dep) = kind;
94 DEP_STATUS (dep) = ds;
97 /* Init DEP with the arguments.
98 While most of the scheduler (including targets) only need the major type
99 of the dependency, it is convenient to hide full dep_status from them. */
100 void
101 init_dep (dep_t dep, rtx pro, rtx con, enum reg_note kind)
103 ds_t ds;
105 if ((current_sched_info->flags & USE_DEPS_LIST) != 0)
106 ds = dk_to_ds (kind);
107 else
108 ds = -1;
110 init_dep_1 (dep, pro, con, kind, ds);
113 /* Make a copy of FROM in TO. */
114 static void
115 copy_dep (dep_t to, dep_t from)
117 memcpy (to, from, sizeof (*to));
120 /* Functions to operate with a single link from the dependencies lists -
121 dep_link_t. */
123 /* Return true if dep_link L is consistent. */
124 static bool
125 dep_link_consistent_p (dep_link_t l)
127 dep_link_t next = DEP_LINK_NEXT (l);
129 return (next == NULL
130 || &DEP_LINK_NEXT (l) == DEP_LINK_PREV_NEXTP (next));
133 /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
134 PREV_NEXT_P. */
135 static void
136 attach_dep_link (dep_link_t l, dep_link_t *prev_nextp)
138 dep_link_t next = *prev_nextp;
140 gcc_assert (DEP_LINK_PREV_NEXTP (l) == NULL
141 && DEP_LINK_NEXT (l) == NULL);
143 /* Init node being inserted. */
144 DEP_LINK_PREV_NEXTP (l) = prev_nextp;
145 DEP_LINK_NEXT (l) = next;
147 /* Fix next node. */
148 if (next != NULL)
150 gcc_assert (DEP_LINK_PREV_NEXTP (next) == prev_nextp);
152 DEP_LINK_PREV_NEXTP (next) = &DEP_LINK_NEXT (l);
155 /* Fix prev node. */
156 *prev_nextp = l;
159 /* Add dep_link LINK to deps_list L. */
160 static void
161 add_to_deps_list (dep_link_t link, deps_list_t l)
163 attach_dep_link (link, &DEPS_LIST_FIRST (l));
166 /* Detach dep_link L from the list. */
167 static void
168 detach_dep_link (dep_link_t l)
170 dep_link_t *prev_nextp = DEP_LINK_PREV_NEXTP (l);
171 dep_link_t next = DEP_LINK_NEXT (l);
173 *prev_nextp = next;
175 if (next != NULL)
176 DEP_LINK_PREV_NEXTP (next) = prev_nextp;
178 /* Though this is property is not used anywhere but in the assert in
179 attach_dep_link (), this can prevent latent errors. */
180 DEP_LINK_PREV_NEXTP (l) = NULL;
181 DEP_LINK_NEXT (l) = NULL;
184 /* Move LINK from whatever list it is now to L. */
185 void
186 move_dep_link (dep_link_t link, deps_list_t l)
188 detach_dep_link (link);
189 add_to_deps_list (link, l);
192 /* Check L's and its successors' consistency.
193 This is, potentially, an expensive check, hence it should be guarded by
194 ENABLE_CHECKING at all times. */
195 static bool
196 dep_links_consistent_p (dep_link_t l)
198 while (l != NULL)
200 if (dep_link_consistent_p (l))
201 l = DEP_LINK_NEXT (l);
202 else
203 return false;
206 return true;
209 /* Dump dep_nodes starting from l. */
210 static void
211 dump_dep_links (FILE *dump, dep_link_t l)
213 while (l != NULL)
215 dep_t d = DEP_LINK_DEP (l);
217 fprintf (dump, "%d%c>%d ", INSN_UID (DEP_PRO (d)),
218 dep_link_consistent_p (l) ? '-' : '!', INSN_UID (DEP_CON (d)));
220 l = DEP_LINK_NEXT (l);
223 fprintf (dump, "\n");
226 /* Dump dep_nodes starting from L to stderr. */
227 void
228 debug_dep_links (dep_link_t l)
230 dump_dep_links (stderr, l);
233 /* Obstack to allocate dep_nodes and deps_lists on. */
234 static struct obstack deps_obstack;
236 /* Obstack to hold forward dependencies lists (deps_list_t). */
237 static struct obstack *dl_obstack = &deps_obstack;
239 /* Obstack to hold all dependency nodes (dep_node_t). */
240 static struct obstack *dn_obstack = &deps_obstack;
242 /* Functions to operate with dependences lists - deps_list_t. */
244 /* Allocate deps_list.
246 If ON_OBSTACK_P is true, allocate the list on the obstack. This is done for
247 INSN_FORW_DEPS lists because they should live till the end of scheduling.
249 INSN_BACK_DEPS and INSN_RESOLVED_BACK_DEPS lists are allocated on the free
250 store and are being freed in haifa-sched.c: schedule_insn (). */
251 static deps_list_t
252 alloc_deps_list (bool on_obstack_p)
254 if (on_obstack_p)
255 return obstack_alloc (dl_obstack, sizeof (struct _deps_list));
256 else
257 return xmalloc (sizeof (struct _deps_list));
260 /* Initialize deps_list L. */
261 static void
262 init_deps_list (deps_list_t l)
264 DEPS_LIST_FIRST (l) = NULL;
267 /* Create (allocate and init) deps_list.
268 The meaning of ON_OBSTACK_P is the same as in alloc_deps_list (). */
269 deps_list_t
270 create_deps_list (bool on_obstack_p)
272 deps_list_t l = alloc_deps_list (on_obstack_p);
274 init_deps_list (l);
275 return l;
278 /* Free dep_data_nodes that present in L. */
279 static void
280 clear_deps_list (deps_list_t l)
282 /* All dep_nodes are allocated on the dn_obstack. They'll be freed with
283 the obstack. */
285 DEPS_LIST_FIRST (l) = NULL;
288 /* Free deps_list L. */
289 void
290 free_deps_list (deps_list_t l)
292 gcc_assert (deps_list_empty_p (l));
293 free (l);
296 /* Delete (clear and free) deps_list L. */
297 void
298 delete_deps_list (deps_list_t l)
300 clear_deps_list (l);
301 free_deps_list (l);
304 /* Return true if L is empty. */
305 bool
306 deps_list_empty_p (deps_list_t l)
308 return DEPS_LIST_FIRST (l) == NULL;
311 /* Check L's consistency.
312 This is, potentially, an expensive check, hence it should be guarded by
313 ENABLE_CHECKING at all times. */
314 static bool
315 deps_list_consistent_p (deps_list_t l)
317 dep_link_t first = DEPS_LIST_FIRST (l);
319 return (first == NULL
320 || (&DEPS_LIST_FIRST (l) == DEP_LINK_PREV_NEXTP (first)
321 && dep_links_consistent_p (first)));
324 /* Dump L to F. */
325 static void
326 dump_deps_list (FILE *f, deps_list_t l)
328 dump_dep_links (f, DEPS_LIST_FIRST (l));
331 /* Dump L to STDERR. */
332 void
333 debug_deps_list (deps_list_t l)
335 dump_deps_list (stderr, l);
338 /* Add a dependency described by DEP to the list L.
339 L should be either INSN_BACK_DEPS or INSN_RESOLVED_BACK_DEPS. */
340 void
341 add_back_dep_to_deps_list (deps_list_t l, dep_t dep_from)
343 dep_node_t n = (dep_node_t) obstack_alloc (dn_obstack,
344 sizeof (*n));
345 dep_t dep_to = DEP_NODE_DEP (n);
346 dep_link_t back = DEP_NODE_BACK (n);
347 dep_link_t forw = DEP_NODE_FORW (n);
349 copy_dep (dep_to, dep_from);
351 DEP_LINK_NODE (back) = n;
352 DEP_LINK_NODE (forw) = n;
354 /* There is no particular need to initialize these four fields except to make
355 assert in attach_dep_link () happy. */
356 DEP_LINK_NEXT (back) = NULL;
357 DEP_LINK_PREV_NEXTP (back) = NULL;
358 DEP_LINK_NEXT (forw) = NULL;
359 DEP_LINK_PREV_NEXTP (forw) = NULL;
361 add_to_deps_list (back, l);
364 /* Find the dep_link with producer PRO in deps_list L. */
365 dep_link_t
366 find_link_by_pro_in_deps_list (deps_list_t l, rtx pro)
368 dep_link_t link;
370 FOR_EACH_DEP_LINK (link, l)
371 if (DEP_LINK_PRO (link) == pro)
372 return link;
374 return NULL;
377 /* Find the dep_link with consumer CON in deps_list L. */
378 dep_link_t
379 find_link_by_con_in_deps_list (deps_list_t l, rtx con)
381 dep_link_t link;
383 FOR_EACH_DEP_LINK (link, l)
384 if (DEP_LINK_CON (link) == con)
385 return link;
387 return NULL;
390 /* Make a copy of FROM in TO with substituting consumer with CON.
391 TO and FROM should be RESOLVED_BACK_DEPS lists. */
392 void
393 copy_deps_list_change_con (deps_list_t to, deps_list_t from, rtx con)
395 dep_link_t l;
397 gcc_assert (deps_list_empty_p (to));
399 FOR_EACH_DEP_LINK (l, from)
401 add_back_dep_to_deps_list (to, DEP_LINK_DEP (l));
402 DEP_LINK_CON (DEPS_LIST_FIRST (to)) = con;
406 static regset reg_pending_sets;
407 static regset reg_pending_clobbers;
408 static regset reg_pending_uses;
410 /* The following enumeration values tell us what dependencies we
411 should use to implement the barrier. We use true-dependencies for
412 TRUE_BARRIER and anti-dependencies for MOVE_BARRIER. */
413 enum reg_pending_barrier_mode
415 NOT_A_BARRIER = 0,
416 MOVE_BARRIER,
417 TRUE_BARRIER
420 static enum reg_pending_barrier_mode reg_pending_barrier;
422 /* To speed up the test for duplicate dependency links we keep a
423 record of dependencies created by add_dependence when the average
424 number of instructions in a basic block is very large.
426 Studies have shown that there is typically around 5 instructions between
427 branches for typical C code. So we can make a guess that the average
428 basic block is approximately 5 instructions long; we will choose 100X
429 the average size as a very large basic block.
431 Each insn has associated bitmaps for its dependencies. Each bitmap
432 has enough entries to represent a dependency on any other insn in
433 the insn chain. All bitmap for true dependencies cache is
434 allocated then the rest two ones are also allocated. */
435 static bitmap_head *true_dependency_cache;
436 static bitmap_head *output_dependency_cache;
437 static bitmap_head *anti_dependency_cache;
438 static bitmap_head *spec_dependency_cache;
439 static int cache_size;
441 /* To speed up checking consistency of formed forward insn
442 dependencies we use the following cache. Another possible solution
443 could be switching off checking duplication of insns in forward
444 dependencies. */
445 #ifdef ENABLE_CHECKING
446 static bitmap_head *forward_dependency_cache;
447 #endif
449 static int deps_may_trap_p (rtx);
450 static void add_dependence_list (rtx, rtx, int, enum reg_note);
451 static void add_dependence_list_and_free (rtx, rtx *, int, enum reg_note);
452 static void delete_all_dependences (rtx);
453 static void fixup_sched_groups (rtx);
455 static void flush_pending_lists (struct deps *, rtx, int, int);
456 static void sched_analyze_1 (struct deps *, rtx, rtx);
457 static void sched_analyze_2 (struct deps *, rtx, rtx);
458 static void sched_analyze_insn (struct deps *, rtx, rtx);
460 static rtx sched_get_condition (rtx);
461 static int conditions_mutex_p (rtx, rtx);
463 static enum DEPS_ADJUST_RESULT maybe_add_or_update_back_dep_1 (rtx, rtx,
464 enum reg_note, ds_t, rtx, rtx, dep_link_t **);
465 static enum DEPS_ADJUST_RESULT add_or_update_back_dep_1 (rtx, rtx,
466 enum reg_note, ds_t, rtx, rtx, dep_link_t **);
467 static void add_back_dep (rtx, rtx, enum reg_note, ds_t);
469 static void adjust_add_sorted_back_dep (rtx, dep_link_t, dep_link_t *);
470 static void adjust_back_add_forw_dep (rtx, dep_link_t *);
471 static void delete_forw_dep (dep_link_t);
472 static dw_t estimate_dep_weak (rtx, rtx);
473 #ifdef INSN_SCHEDULING
474 #ifdef ENABLE_CHECKING
475 static void check_dep_status (enum reg_note, ds_t, bool);
476 #endif
477 #endif
479 /* Return nonzero if a load of the memory reference MEM can cause a trap. */
481 static int
482 deps_may_trap_p (rtx mem)
484 rtx addr = XEXP (mem, 0);
486 if (REG_P (addr) && REGNO (addr) >= FIRST_PSEUDO_REGISTER)
488 rtx t = get_reg_known_value (REGNO (addr));
489 if (t)
490 addr = t;
492 return rtx_addr_can_trap_p (addr);
495 /* Find the condition under which INSN is executed. */
497 static rtx
498 sched_get_condition (rtx insn)
500 rtx pat = PATTERN (insn);
501 rtx src;
503 if (pat == 0)
504 return 0;
506 if (GET_CODE (pat) == COND_EXEC)
507 return COND_EXEC_TEST (pat);
509 if (!any_condjump_p (insn) || !onlyjump_p (insn))
510 return 0;
512 src = SET_SRC (pc_set (insn));
514 if (XEXP (src, 2) == pc_rtx)
515 return XEXP (src, 0);
516 else if (XEXP (src, 1) == pc_rtx)
518 rtx cond = XEXP (src, 0);
519 enum rtx_code revcode = reversed_comparison_code (cond, insn);
521 if (revcode == UNKNOWN)
522 return 0;
523 return gen_rtx_fmt_ee (revcode, GET_MODE (cond), XEXP (cond, 0),
524 XEXP (cond, 1));
527 return 0;
531 /* Return nonzero if conditions COND1 and COND2 can never be both true. */
533 static int
534 conditions_mutex_p (rtx cond1, rtx cond2)
536 if (COMPARISON_P (cond1)
537 && COMPARISON_P (cond2)
538 && GET_CODE (cond1) == reversed_comparison_code (cond2, NULL)
539 && XEXP (cond1, 0) == XEXP (cond2, 0)
540 && XEXP (cond1, 1) == XEXP (cond2, 1))
541 return 1;
542 return 0;
545 /* Return true if insn1 and insn2 can never depend on one another because
546 the conditions under which they are executed are mutually exclusive. */
547 bool
548 sched_insns_conditions_mutex_p (rtx insn1, rtx insn2)
550 rtx cond1, cond2;
552 /* df doesn't handle conditional lifetimes entirely correctly;
553 calls mess up the conditional lifetimes. */
554 if (!CALL_P (insn1) && !CALL_P (insn2))
556 cond1 = sched_get_condition (insn1);
557 cond2 = sched_get_condition (insn2);
558 if (cond1 && cond2
559 && conditions_mutex_p (cond1, cond2)
560 /* Make sure first instruction doesn't affect condition of second
561 instruction if switched. */
562 && !modified_in_p (cond1, insn2)
563 /* Make sure second instruction doesn't affect condition of first
564 instruction if switched. */
565 && !modified_in_p (cond2, insn1))
566 return true;
568 return false;
571 /* Add ELEM wrapped in an dep_link with reg note kind DEP_TYPE to the
572 INSN_BACK_DEPS (INSN), if it is not already there. DEP_TYPE indicates the
573 type of dependence that this link represents. DS, if nonzero,
574 indicates speculations, through which this dependence can be overcome.
575 MEM1 and MEM2, if non-null, corresponds to memory locations in case of
576 data speculation. The function returns a value indicating if an old entry
577 has been changed or a new entry has been added to insn's backward deps.
578 In case of changed entry CHANGED_LINKPP sets to its address.
579 See also the definition of enum DEPS_ADJUST_RESULT in sched-int.h.
580 Actual manipulation of dependence data structures is performed in
581 add_or_update_back_dep_1. */
583 static enum DEPS_ADJUST_RESULT
584 maybe_add_or_update_back_dep_1 (rtx insn, rtx elem, enum reg_note dep_type,
585 ds_t ds, rtx mem1, rtx mem2,
586 dep_link_t **changed_linkpp)
588 gcc_assert (INSN_P (insn) && INSN_P (elem));
590 /* Don't depend an insn on itself. */
591 if (insn == elem)
593 #ifdef INSN_SCHEDULING
594 if (current_sched_info->flags & DO_SPECULATION)
595 /* INSN has an internal dependence, which we can't overcome. */
596 HAS_INTERNAL_DEP (insn) = 1;
597 #endif
598 return 0;
601 return add_or_update_back_dep_1 (insn, elem, dep_type,
602 ds, mem1, mem2, changed_linkpp);
605 /* This function has the same meaning of parameters and return values
606 as maybe_add_or_update_back_dep_1. The only difference between these
607 two functions is that INSN and ELEM are guaranteed not to be the same
608 in this one. */
609 static enum DEPS_ADJUST_RESULT
610 add_or_update_back_dep_1 (rtx insn, rtx elem, enum reg_note dep_type,
611 ds_t ds ATTRIBUTE_UNUSED,
612 rtx mem1 ATTRIBUTE_UNUSED, rtx mem2 ATTRIBUTE_UNUSED,
613 dep_link_t **changed_linkpp ATTRIBUTE_UNUSED)
615 bool maybe_present_p = true, present_p = false;
617 gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
619 #ifdef INSN_SCHEDULING
621 #ifdef ENABLE_CHECKING
622 check_dep_status (dep_type, ds, mem1 != NULL);
623 #endif
625 /* If we already have a dependency for ELEM, then we do not need to
626 do anything. Avoiding the list walk below can cut compile times
627 dramatically for some code. */
628 if (true_dependency_cache != NULL)
630 enum reg_note present_dep_type;
632 gcc_assert (output_dependency_cache);
633 gcc_assert (anti_dependency_cache);
634 if (!(current_sched_info->flags & USE_DEPS_LIST))
636 if (bitmap_bit_p (&true_dependency_cache[INSN_LUID (insn)],
637 INSN_LUID (elem)))
638 present_dep_type = REG_DEP_TRUE;
639 else if (bitmap_bit_p (&output_dependency_cache[INSN_LUID (insn)],
640 INSN_LUID (elem)))
641 present_dep_type = REG_DEP_OUTPUT;
642 else if (bitmap_bit_p (&anti_dependency_cache[INSN_LUID (insn)],
643 INSN_LUID (elem)))
644 present_dep_type = REG_DEP_ANTI;
645 else
646 maybe_present_p = false;
648 if (maybe_present_p)
650 if ((int) dep_type >= (int) present_dep_type)
651 return DEP_PRESENT;
653 present_p = true;
656 else
658 ds_t present_dep_types = 0;
660 if (bitmap_bit_p (&true_dependency_cache[INSN_LUID (insn)],
661 INSN_LUID (elem)))
662 present_dep_types |= DEP_TRUE;
663 if (bitmap_bit_p (&output_dependency_cache[INSN_LUID (insn)],
664 INSN_LUID (elem)))
665 present_dep_types |= DEP_OUTPUT;
666 if (bitmap_bit_p (&anti_dependency_cache[INSN_LUID (insn)],
667 INSN_LUID (elem)))
668 present_dep_types |= DEP_ANTI;
670 if (present_dep_types)
672 if (!(current_sched_info->flags & DO_SPECULATION)
673 || !bitmap_bit_p (&spec_dependency_cache[INSN_LUID (insn)],
674 INSN_LUID (elem)))
676 if ((present_dep_types | (ds & DEP_TYPES))
677 == present_dep_types)
678 /* We already have all these bits. */
679 return DEP_PRESENT;
681 else
683 /* Only true dependencies can be data speculative and
684 only anti dependencies can be control speculative. */
685 gcc_assert ((present_dep_types & (DEP_TRUE | DEP_ANTI))
686 == present_dep_types);
688 /* if (additional dep is SPECULATIVE) then
689 we should update DEP_STATUS
690 else
691 we should reset existing dep to non-speculative. */
694 present_p = true;
696 else
697 maybe_present_p = false;
700 #endif
702 /* Check that we don't already have this dependence. */
703 if (maybe_present_p)
705 dep_link_t *linkp;
707 for (linkp = &DEPS_LIST_FIRST (INSN_BACK_DEPS (insn));
708 *linkp != NULL;
709 linkp = &DEP_LINK_NEXT (*linkp))
711 dep_t link = DEP_LINK_DEP (*linkp);
713 gcc_assert (true_dependency_cache == 0 || present_p);
715 if (DEP_PRO (link) == elem)
717 enum DEPS_ADJUST_RESULT changed_p = DEP_PRESENT;
719 #ifdef INSN_SCHEDULING
720 if (current_sched_info->flags & USE_DEPS_LIST)
722 ds_t new_status = ds | DEP_STATUS (link);
724 if (new_status & SPECULATIVE)
726 if (!(ds & SPECULATIVE)
727 || !(DEP_STATUS (link) & SPECULATIVE))
728 /* Then this dep can't be speculative. */
730 new_status &= ~SPECULATIVE;
731 if (true_dependency_cache
732 && (DEP_STATUS (link) & SPECULATIVE))
733 bitmap_clear_bit (&spec_dependency_cache
734 [INSN_LUID (insn)],
735 INSN_LUID (elem));
737 else
739 /* Both are speculative. Merging probabilities. */
740 if (mem1)
742 dw_t dw;
744 dw = estimate_dep_weak (mem1, mem2);
745 ds = set_dep_weak (ds, BEGIN_DATA, dw);
748 new_status = ds_merge (DEP_STATUS (link), ds);
752 ds = new_status;
755 /* Clear corresponding cache entry because type of the link
756 may have changed. Keep them if we use_deps_list. */
757 if (true_dependency_cache != NULL
758 && !(current_sched_info->flags & USE_DEPS_LIST))
760 enum reg_note kind = DEP_KIND (link);
762 switch (kind)
764 case REG_DEP_OUTPUT:
765 bitmap_clear_bit (&output_dependency_cache
766 [INSN_LUID (insn)], INSN_LUID (elem));
767 break;
768 case REG_DEP_ANTI:
769 bitmap_clear_bit (&anti_dependency_cache
770 [INSN_LUID (insn)], INSN_LUID (elem));
771 break;
772 default:
773 gcc_unreachable ();
777 if ((current_sched_info->flags & USE_DEPS_LIST)
778 && DEP_STATUS (link) != ds)
780 DEP_STATUS (link) = ds;
781 changed_p = DEP_CHANGED;
783 #endif
785 /* If this is a more restrictive type of dependence than the
786 existing one, then change the existing dependence to this
787 type. */
788 if ((int) dep_type < (int) DEP_KIND (link))
790 DEP_KIND (link) = dep_type;
791 changed_p = DEP_CHANGED;
794 #ifdef INSN_SCHEDULING
795 /* If we are adding a dependency to INSN's LOG_LINKs, then
796 note that in the bitmap caches of dependency information. */
797 if (true_dependency_cache != NULL)
799 if (!(current_sched_info->flags & USE_DEPS_LIST))
801 if (DEP_KIND (link) == REG_DEP_TRUE)
802 bitmap_set_bit (&true_dependency_cache
803 [INSN_LUID (insn)], INSN_LUID (elem));
804 else if (DEP_KIND (link) == REG_DEP_OUTPUT)
805 bitmap_set_bit (&output_dependency_cache
806 [INSN_LUID (insn)], INSN_LUID (elem));
807 else if (DEP_KIND (link) == REG_DEP_ANTI)
808 bitmap_set_bit (&anti_dependency_cache
809 [INSN_LUID (insn)], INSN_LUID (elem));
811 else
813 if (ds & DEP_TRUE)
814 bitmap_set_bit (&true_dependency_cache
815 [INSN_LUID (insn)], INSN_LUID (elem));
816 if (ds & DEP_OUTPUT)
817 bitmap_set_bit (&output_dependency_cache
818 [INSN_LUID (insn)], INSN_LUID (elem));
819 if (ds & DEP_ANTI)
820 bitmap_set_bit (&anti_dependency_cache
821 [INSN_LUID (insn)], INSN_LUID (elem));
822 /* Note, that dep can become speculative only
823 at the moment of creation. Thus, we don't need to
824 check for it here. */
828 if (changed_linkpp && changed_p == DEP_CHANGED)
829 *changed_linkpp = linkp;
830 #endif
831 return changed_p;
834 /* We didn't find a dep. It shouldn't be present in the cache. */
835 gcc_assert (!present_p);
838 /* Might want to check one level of transitivity to save conses.
839 This check should be done in maybe_add_or_update_back_dep_1.
840 Since we made it to add_or_update_back_dep_1, we must create
841 (or update) a link. */
843 if (mem1)
845 gcc_assert (current_sched_info->flags & DO_SPECULATION);
846 ds = set_dep_weak (ds, BEGIN_DATA, estimate_dep_weak (mem1, mem2));
849 add_back_dep (insn, elem, dep_type, ds);
851 return DEP_CREATED;
854 /* This function creates a link between INSN and ELEM under any
855 conditions. DS describes speculative status of the link. */
856 static void
857 add_back_dep (rtx insn, rtx elem, enum reg_note dep_type, ds_t ds)
859 struct _dep _dep, *dep = &_dep;
861 gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
863 if (current_sched_info->flags & USE_DEPS_LIST)
864 init_dep_1 (dep, elem, insn, dep_type, ds);
865 else
866 init_dep_1 (dep, elem, insn, dep_type, -1);
868 add_back_dep_to_deps_list (INSN_BACK_DEPS (insn), dep);
870 #ifdef INSN_SCHEDULING
871 #ifdef ENABLE_CHECKING
872 check_dep_status (dep_type, ds, false);
873 #endif
875 /* If we are adding a dependency to INSN's LOG_LINKs, then note that
876 in the bitmap caches of dependency information. */
877 if (true_dependency_cache != NULL)
879 if (!(current_sched_info->flags & USE_DEPS_LIST))
881 if (dep_type == REG_DEP_TRUE)
882 bitmap_set_bit (&true_dependency_cache[INSN_LUID (insn)],
883 INSN_LUID (elem));
884 else if (dep_type == REG_DEP_OUTPUT)
885 bitmap_set_bit (&output_dependency_cache[INSN_LUID (insn)],
886 INSN_LUID (elem));
887 else if (dep_type == REG_DEP_ANTI)
888 bitmap_set_bit (&anti_dependency_cache[INSN_LUID (insn)],
889 INSN_LUID (elem));
891 else
893 if (ds & DEP_TRUE)
894 bitmap_set_bit (&true_dependency_cache[INSN_LUID (insn)],
895 INSN_LUID (elem));
896 if (ds & DEP_OUTPUT)
897 bitmap_set_bit (&output_dependency_cache[INSN_LUID (insn)],
898 INSN_LUID (elem));
899 if (ds & DEP_ANTI)
900 bitmap_set_bit (&anti_dependency_cache[INSN_LUID (insn)],
901 INSN_LUID (elem));
902 if (ds & SPECULATIVE)
904 gcc_assert (current_sched_info->flags & DO_SPECULATION);
905 bitmap_set_bit (&spec_dependency_cache[INSN_LUID (insn)],
906 INSN_LUID (elem));
910 #endif
913 /* A convenience wrapper to operate on an entire list. */
915 static void
916 add_dependence_list (rtx insn, rtx list, int uncond, enum reg_note dep_type)
918 for (; list; list = XEXP (list, 1))
920 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
921 add_dependence (insn, XEXP (list, 0), dep_type);
925 /* Similar, but free *LISTP at the same time. */
927 static void
928 add_dependence_list_and_free (rtx insn, rtx *listp, int uncond,
929 enum reg_note dep_type)
931 rtx list, next;
932 for (list = *listp, *listp = NULL; list ; list = next)
934 next = XEXP (list, 1);
935 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
936 add_dependence (insn, XEXP (list, 0), dep_type);
937 free_INSN_LIST_node (list);
941 /* Clear all dependencies for an insn. */
943 static void
944 delete_all_dependences (rtx insn)
946 /* Clear caches, if they exist, as well as free the dependence. */
948 #ifdef INSN_SCHEDULING
949 if (true_dependency_cache != NULL)
951 bitmap_clear (&true_dependency_cache[INSN_LUID (insn)]);
952 bitmap_clear (&output_dependency_cache[INSN_LUID (insn)]);
953 bitmap_clear (&anti_dependency_cache[INSN_LUID (insn)]);
954 /* We don't have to clear forward_dependency_cache here,
955 because it is formed later. */
956 if (current_sched_info->flags & DO_SPECULATION)
957 bitmap_clear (&spec_dependency_cache[INSN_LUID (insn)]);
959 #endif
961 clear_deps_list (INSN_BACK_DEPS (insn));
964 /* All insns in a scheduling group except the first should only have
965 dependencies on the previous insn in the group. So we find the
966 first instruction in the scheduling group by walking the dependence
967 chains backwards. Then we add the dependencies for the group to
968 the previous nonnote insn. */
970 static void
971 fixup_sched_groups (rtx insn)
973 dep_link_t link;
974 rtx prev_nonnote;
976 FOR_EACH_DEP_LINK (link, INSN_BACK_DEPS (insn))
978 rtx i = insn;
979 dep_t dep = DEP_LINK_DEP (link);
980 rtx pro = DEP_PRO (dep);
984 i = prev_nonnote_insn (i);
986 if (pro == i)
987 goto next_link;
988 } while (SCHED_GROUP_P (i));
990 if (! sched_insns_conditions_mutex_p (i, pro))
991 add_dependence (i, pro, DEP_KIND (dep));
992 next_link:;
995 delete_all_dependences (insn);
997 prev_nonnote = prev_nonnote_insn (insn);
998 if (BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (prev_nonnote)
999 && ! sched_insns_conditions_mutex_p (insn, prev_nonnote))
1000 add_dependence (insn, prev_nonnote, REG_DEP_ANTI);
1003 /* Process an insn's memory dependencies. There are four kinds of
1004 dependencies:
1006 (0) read dependence: read follows read
1007 (1) true dependence: read follows write
1008 (2) output dependence: write follows write
1009 (3) anti dependence: write follows read
1011 We are careful to build only dependencies which actually exist, and
1012 use transitivity to avoid building too many links. */
1014 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1015 The MEM is a memory reference contained within INSN, which we are saving
1016 so that we can do memory aliasing on it. */
1018 static void
1019 add_insn_mem_dependence (struct deps *deps, bool read_p,
1020 rtx insn, rtx mem)
1022 rtx *insn_list;
1023 rtx *mem_list;
1024 rtx link;
1026 if (read_p)
1028 insn_list = &deps->pending_read_insns;
1029 mem_list = &deps->pending_read_mems;
1030 deps->pending_read_list_length++;
1032 else
1034 insn_list = &deps->pending_write_insns;
1035 mem_list = &deps->pending_write_mems;
1036 deps->pending_write_list_length++;
1039 link = alloc_INSN_LIST (insn, *insn_list);
1040 *insn_list = link;
1042 if (current_sched_info->use_cselib)
1044 mem = shallow_copy_rtx (mem);
1045 XEXP (mem, 0) = cselib_subst_to_values (XEXP (mem, 0));
1047 link = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
1048 *mem_list = link;
1051 /* Make a dependency between every memory reference on the pending lists
1052 and INSN, thus flushing the pending lists. FOR_READ is true if emitting
1053 dependencies for a read operation, similarly with FOR_WRITE. */
1055 static void
1056 flush_pending_lists (struct deps *deps, rtx insn, int for_read,
1057 int for_write)
1059 if (for_write)
1061 add_dependence_list_and_free (insn, &deps->pending_read_insns, 1,
1062 REG_DEP_ANTI);
1063 free_EXPR_LIST_list (&deps->pending_read_mems);
1064 deps->pending_read_list_length = 0;
1067 add_dependence_list_and_free (insn, &deps->pending_write_insns, 1,
1068 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1069 free_EXPR_LIST_list (&deps->pending_write_mems);
1070 deps->pending_write_list_length = 0;
1072 add_dependence_list_and_free (insn, &deps->last_pending_memory_flush, 1,
1073 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1074 deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
1075 deps->pending_flush_length = 1;
1078 /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
1079 The type of the reference is specified by REF and can be SET,
1080 CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE. */
1082 static void
1083 sched_analyze_reg (struct deps *deps, int regno, enum machine_mode mode,
1084 enum rtx_code ref, rtx insn)
1086 /* A hard reg in a wide mode may really be multiple registers.
1087 If so, mark all of them just like the first. */
1088 if (regno < FIRST_PSEUDO_REGISTER)
1090 int i = hard_regno_nregs[regno][mode];
1091 if (ref == SET)
1093 while (--i >= 0)
1094 SET_REGNO_REG_SET (reg_pending_sets, regno + i);
1096 else if (ref == USE)
1098 while (--i >= 0)
1099 SET_REGNO_REG_SET (reg_pending_uses, regno + i);
1101 else
1103 while (--i >= 0)
1104 SET_REGNO_REG_SET (reg_pending_clobbers, regno + i);
1108 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
1109 it does not reload. Ignore these as they have served their
1110 purpose already. */
1111 else if (regno >= deps->max_reg)
1113 enum rtx_code code = GET_CODE (PATTERN (insn));
1114 gcc_assert (code == USE || code == CLOBBER);
1117 else
1119 if (ref == SET)
1120 SET_REGNO_REG_SET (reg_pending_sets, regno);
1121 else if (ref == USE)
1122 SET_REGNO_REG_SET (reg_pending_uses, regno);
1123 else
1124 SET_REGNO_REG_SET (reg_pending_clobbers, regno);
1126 /* Pseudos that are REG_EQUIV to something may be replaced
1127 by that during reloading. We need only add dependencies for
1128 the address in the REG_EQUIV note. */
1129 if (!reload_completed && get_reg_known_equiv_p (regno))
1131 rtx t = get_reg_known_value (regno);
1132 if (MEM_P (t))
1133 sched_analyze_2 (deps, XEXP (t, 0), insn);
1136 /* Don't let it cross a call after scheduling if it doesn't
1137 already cross one. */
1138 if (REG_N_CALLS_CROSSED (regno) == 0)
1140 if (ref == USE)
1141 deps->sched_before_next_call
1142 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
1143 else
1144 add_dependence_list (insn, deps->last_function_call, 1,
1145 REG_DEP_ANTI);
1150 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
1151 rtx, X, creating all dependencies generated by the write to the
1152 destination of X, and reads of everything mentioned. */
1154 static void
1155 sched_analyze_1 (struct deps *deps, rtx x, rtx insn)
1157 rtx dest = XEXP (x, 0);
1158 enum rtx_code code = GET_CODE (x);
1160 if (dest == 0)
1161 return;
1163 if (GET_CODE (dest) == PARALLEL)
1165 int i;
1167 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1168 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
1169 sched_analyze_1 (deps,
1170 gen_rtx_CLOBBER (VOIDmode,
1171 XEXP (XVECEXP (dest, 0, i), 0)),
1172 insn);
1174 if (GET_CODE (x) == SET)
1175 sched_analyze_2 (deps, SET_SRC (x), insn);
1176 return;
1179 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
1180 || GET_CODE (dest) == ZERO_EXTRACT)
1182 if (GET_CODE (dest) == STRICT_LOW_PART
1183 || GET_CODE (dest) == ZERO_EXTRACT
1184 || df_read_modify_subreg_p (dest))
1186 /* These both read and modify the result. We must handle
1187 them as writes to get proper dependencies for following
1188 instructions. We must handle them as reads to get proper
1189 dependencies from this to previous instructions.
1190 Thus we need to call sched_analyze_2. */
1192 sched_analyze_2 (deps, XEXP (dest, 0), insn);
1194 if (GET_CODE (dest) == ZERO_EXTRACT)
1196 /* The second and third arguments are values read by this insn. */
1197 sched_analyze_2 (deps, XEXP (dest, 1), insn);
1198 sched_analyze_2 (deps, XEXP (dest, 2), insn);
1200 dest = XEXP (dest, 0);
1203 if (REG_P (dest))
1205 int regno = REGNO (dest);
1206 enum machine_mode mode = GET_MODE (dest);
1208 sched_analyze_reg (deps, regno, mode, code, insn);
1210 #ifdef STACK_REGS
1211 /* Treat all writes to a stack register as modifying the TOS. */
1212 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
1214 /* Avoid analyzing the same register twice. */
1215 if (regno != FIRST_STACK_REG)
1216 sched_analyze_reg (deps, FIRST_STACK_REG, mode, code, insn);
1217 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
1219 #endif
1221 else if (MEM_P (dest))
1223 /* Writing memory. */
1224 rtx t = dest;
1226 if (current_sched_info->use_cselib)
1228 t = shallow_copy_rtx (dest);
1229 cselib_lookup (XEXP (t, 0), Pmode, 1);
1230 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
1232 t = canon_rtx (t);
1234 if ((deps->pending_read_list_length + deps->pending_write_list_length)
1235 > MAX_PENDING_LIST_LENGTH)
1237 /* Flush all pending reads and writes to prevent the pending lists
1238 from getting any larger. Insn scheduling runs too slowly when
1239 these lists get long. When compiling GCC with itself,
1240 this flush occurs 8 times for sparc, and 10 times for m88k using
1241 the default value of 32. */
1242 flush_pending_lists (deps, insn, false, true);
1244 else
1246 rtx pending, pending_mem;
1248 pending = deps->pending_read_insns;
1249 pending_mem = deps->pending_read_mems;
1250 while (pending)
1252 if (anti_dependence (XEXP (pending_mem, 0), t)
1253 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1254 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1256 pending = XEXP (pending, 1);
1257 pending_mem = XEXP (pending_mem, 1);
1260 pending = deps->pending_write_insns;
1261 pending_mem = deps->pending_write_mems;
1262 while (pending)
1264 if (output_dependence (XEXP (pending_mem, 0), t)
1265 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1266 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1268 pending = XEXP (pending, 1);
1269 pending_mem = XEXP (pending_mem, 1);
1272 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
1273 REG_DEP_ANTI);
1275 add_insn_mem_dependence (deps, false, insn, dest);
1277 sched_analyze_2 (deps, XEXP (dest, 0), insn);
1280 /* Analyze reads. */
1281 if (GET_CODE (x) == SET)
1282 sched_analyze_2 (deps, SET_SRC (x), insn);
1285 /* Analyze the uses of memory and registers in rtx X in INSN. */
1287 static void
1288 sched_analyze_2 (struct deps *deps, rtx x, rtx insn)
1290 int i;
1291 int j;
1292 enum rtx_code code;
1293 const char *fmt;
1295 if (x == 0)
1296 return;
1298 code = GET_CODE (x);
1300 switch (code)
1302 case CONST_INT:
1303 case CONST_DOUBLE:
1304 case CONST_VECTOR:
1305 case SYMBOL_REF:
1306 case CONST:
1307 case LABEL_REF:
1308 /* Ignore constants. Note that we must handle CONST_DOUBLE here
1309 because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
1310 this does not mean that this insn is using cc0. */
1311 return;
1313 #ifdef HAVE_cc0
1314 case CC0:
1315 /* User of CC0 depends on immediately preceding insn. */
1316 SCHED_GROUP_P (insn) = 1;
1317 /* Don't move CC0 setter to another block (it can set up the
1318 same flag for previous CC0 users which is safe). */
1319 CANT_MOVE (prev_nonnote_insn (insn)) = 1;
1320 return;
1321 #endif
1323 case REG:
1325 int regno = REGNO (x);
1326 enum machine_mode mode = GET_MODE (x);
1328 sched_analyze_reg (deps, regno, mode, USE, insn);
1330 #ifdef STACK_REGS
1331 /* Treat all reads of a stack register as modifying the TOS. */
1332 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
1334 /* Avoid analyzing the same register twice. */
1335 if (regno != FIRST_STACK_REG)
1336 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
1337 sched_analyze_reg (deps, FIRST_STACK_REG, mode, SET, insn);
1339 #endif
1340 return;
1343 case MEM:
1345 /* Reading memory. */
1346 rtx u;
1347 rtx pending, pending_mem;
1348 rtx t = x;
1350 if (current_sched_info->use_cselib)
1352 t = shallow_copy_rtx (t);
1353 cselib_lookup (XEXP (t, 0), Pmode, 1);
1354 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
1356 t = canon_rtx (t);
1357 pending = deps->pending_read_insns;
1358 pending_mem = deps->pending_read_mems;
1359 while (pending)
1361 if (read_dependence (XEXP (pending_mem, 0), t)
1362 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1363 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1365 pending = XEXP (pending, 1);
1366 pending_mem = XEXP (pending_mem, 1);
1369 pending = deps->pending_write_insns;
1370 pending_mem = deps->pending_write_mems;
1371 while (pending)
1373 if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
1374 t, rtx_varies_p)
1375 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1377 if (current_sched_info->flags & DO_SPECULATION)
1378 maybe_add_or_update_back_dep_1 (insn, XEXP (pending, 0),
1379 REG_DEP_TRUE,
1380 BEGIN_DATA | DEP_TRUE,
1381 XEXP (pending_mem, 0), t, 0);
1382 else
1383 add_dependence (insn, XEXP (pending, 0), REG_DEP_TRUE);
1386 pending = XEXP (pending, 1);
1387 pending_mem = XEXP (pending_mem, 1);
1390 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
1391 if (! JUMP_P (XEXP (u, 0)) || deps_may_trap_p (x))
1392 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1394 /* Always add these dependencies to pending_reads, since
1395 this insn may be followed by a write. */
1396 add_insn_mem_dependence (deps, true, insn, x);
1398 /* Take advantage of tail recursion here. */
1399 sched_analyze_2 (deps, XEXP (x, 0), insn);
1400 return;
1403 /* Force pending stores to memory in case a trap handler needs them. */
1404 case TRAP_IF:
1405 flush_pending_lists (deps, insn, true, false);
1406 break;
1408 case ASM_OPERANDS:
1409 case ASM_INPUT:
1410 case UNSPEC_VOLATILE:
1412 /* Traditional and volatile asm instructions must be considered to use
1413 and clobber all hard registers, all pseudo-registers and all of
1414 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
1416 Consider for instance a volatile asm that changes the fpu rounding
1417 mode. An insn should not be moved across this even if it only uses
1418 pseudo-regs because it might give an incorrectly rounded result. */
1419 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
1420 reg_pending_barrier = TRUE_BARRIER;
1422 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1423 We can not just fall through here since then we would be confused
1424 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1425 traditional asms unlike their normal usage. */
1427 if (code == ASM_OPERANDS)
1429 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
1430 sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
1431 return;
1433 break;
1436 case PRE_DEC:
1437 case POST_DEC:
1438 case PRE_INC:
1439 case POST_INC:
1440 /* These both read and modify the result. We must handle them as writes
1441 to get proper dependencies for following instructions. We must handle
1442 them as reads to get proper dependencies from this to previous
1443 instructions. Thus we need to pass them to both sched_analyze_1
1444 and sched_analyze_2. We must call sched_analyze_2 first in order
1445 to get the proper antecedent for the read. */
1446 sched_analyze_2 (deps, XEXP (x, 0), insn);
1447 sched_analyze_1 (deps, x, insn);
1448 return;
1450 case POST_MODIFY:
1451 case PRE_MODIFY:
1452 /* op0 = op0 + op1 */
1453 sched_analyze_2 (deps, XEXP (x, 0), insn);
1454 sched_analyze_2 (deps, XEXP (x, 1), insn);
1455 sched_analyze_1 (deps, x, insn);
1456 return;
1458 default:
1459 break;
1462 /* Other cases: walk the insn. */
1463 fmt = GET_RTX_FORMAT (code);
1464 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1466 if (fmt[i] == 'e')
1467 sched_analyze_2 (deps, XEXP (x, i), insn);
1468 else if (fmt[i] == 'E')
1469 for (j = 0; j < XVECLEN (x, i); j++)
1470 sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
1474 /* Analyze an INSN with pattern X to find all dependencies. */
1476 static void
1477 sched_analyze_insn (struct deps *deps, rtx x, rtx insn)
1479 RTX_CODE code = GET_CODE (x);
1480 rtx link;
1481 unsigned i;
1482 reg_set_iterator rsi;
1484 if (code == COND_EXEC)
1486 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
1488 /* ??? Should be recording conditions so we reduce the number of
1489 false dependencies. */
1490 x = COND_EXEC_CODE (x);
1491 code = GET_CODE (x);
1493 if (code == SET || code == CLOBBER)
1495 sched_analyze_1 (deps, x, insn);
1497 /* Bare clobber insns are used for letting life analysis, reg-stack
1498 and others know that a value is dead. Depend on the last call
1499 instruction so that reg-stack won't get confused. */
1500 if (code == CLOBBER)
1501 add_dependence_list (insn, deps->last_function_call, 1, REG_DEP_OUTPUT);
1503 else if (code == PARALLEL)
1505 for (i = XVECLEN (x, 0); i--;)
1507 rtx sub = XVECEXP (x, 0, i);
1508 code = GET_CODE (sub);
1510 if (code == COND_EXEC)
1512 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
1513 sub = COND_EXEC_CODE (sub);
1514 code = GET_CODE (sub);
1516 if (code == SET || code == CLOBBER)
1517 sched_analyze_1 (deps, sub, insn);
1518 else
1519 sched_analyze_2 (deps, sub, insn);
1522 else
1523 sched_analyze_2 (deps, x, insn);
1525 /* Mark registers CLOBBERED or used by called function. */
1526 if (CALL_P (insn))
1528 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1530 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
1531 sched_analyze_1 (deps, XEXP (link, 0), insn);
1532 else
1533 sched_analyze_2 (deps, XEXP (link, 0), insn);
1535 if (find_reg_note (insn, REG_SETJMP, NULL))
1536 reg_pending_barrier = MOVE_BARRIER;
1539 if (JUMP_P (insn))
1541 rtx next;
1542 next = next_nonnote_insn (insn);
1543 if (next && BARRIER_P (next))
1544 reg_pending_barrier = TRUE_BARRIER;
1545 else
1547 rtx pending, pending_mem;
1548 regset_head tmp_uses, tmp_sets;
1549 INIT_REG_SET (&tmp_uses);
1550 INIT_REG_SET (&tmp_sets);
1552 (*current_sched_info->compute_jump_reg_dependencies)
1553 (insn, &deps->reg_conditional_sets, &tmp_uses, &tmp_sets);
1554 /* Make latency of jump equal to 0 by using anti-dependence. */
1555 EXECUTE_IF_SET_IN_REG_SET (&tmp_uses, 0, i, rsi)
1557 struct deps_reg *reg_last = &deps->reg_last[i];
1558 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI);
1559 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI);
1560 reg_last->uses_length++;
1561 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1563 IOR_REG_SET (reg_pending_sets, &tmp_sets);
1565 CLEAR_REG_SET (&tmp_uses);
1566 CLEAR_REG_SET (&tmp_sets);
1568 /* All memory writes and volatile reads must happen before the
1569 jump. Non-volatile reads must happen before the jump iff
1570 the result is needed by the above register used mask. */
1572 pending = deps->pending_write_insns;
1573 pending_mem = deps->pending_write_mems;
1574 while (pending)
1576 if (! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1577 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1578 pending = XEXP (pending, 1);
1579 pending_mem = XEXP (pending_mem, 1);
1582 pending = deps->pending_read_insns;
1583 pending_mem = deps->pending_read_mems;
1584 while (pending)
1586 if (MEM_VOLATILE_P (XEXP (pending_mem, 0))
1587 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1588 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1589 pending = XEXP (pending, 1);
1590 pending_mem = XEXP (pending_mem, 1);
1593 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
1594 REG_DEP_ANTI);
1598 /* If this instruction can throw an exception, then moving it changes
1599 where block boundaries fall. This is mighty confusing elsewhere.
1600 Therefore, prevent such an instruction from being moved. Same for
1601 non-jump instructions that define block boundaries.
1602 ??? Unclear whether this is still necessary in EBB mode. If not,
1603 add_branch_dependences should be adjusted for RGN mode instead. */
1604 if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
1605 || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
1606 reg_pending_barrier = MOVE_BARRIER;
1608 /* Add dependencies if a scheduling barrier was found. */
1609 if (reg_pending_barrier)
1611 /* In the case of barrier the most added dependencies are not
1612 real, so we use anti-dependence here. */
1613 if (sched_get_condition (insn))
1615 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
1617 struct deps_reg *reg_last = &deps->reg_last[i];
1618 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
1619 add_dependence_list
1620 (insn, reg_last->sets, 0,
1621 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
1622 add_dependence_list
1623 (insn, reg_last->clobbers, 0,
1624 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
1627 else
1629 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
1631 struct deps_reg *reg_last = &deps->reg_last[i];
1632 add_dependence_list_and_free (insn, &reg_last->uses, 0,
1633 REG_DEP_ANTI);
1634 add_dependence_list_and_free
1635 (insn, &reg_last->sets, 0,
1636 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
1637 add_dependence_list_and_free
1638 (insn, &reg_last->clobbers, 0,
1639 reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
1640 reg_last->uses_length = 0;
1641 reg_last->clobbers_length = 0;
1645 for (i = 0; i < (unsigned)deps->max_reg; i++)
1647 struct deps_reg *reg_last = &deps->reg_last[i];
1648 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1649 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
1652 flush_pending_lists (deps, insn, true, true);
1653 CLEAR_REG_SET (&deps->reg_conditional_sets);
1654 reg_pending_barrier = NOT_A_BARRIER;
1656 else
1658 /* If the current insn is conditional, we can't free any
1659 of the lists. */
1660 if (sched_get_condition (insn))
1662 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
1664 struct deps_reg *reg_last = &deps->reg_last[i];
1665 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
1666 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
1667 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1668 reg_last->uses_length++;
1670 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
1672 struct deps_reg *reg_last = &deps->reg_last[i];
1673 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
1674 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
1675 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
1676 reg_last->clobbers_length++;
1678 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
1680 struct deps_reg *reg_last = &deps->reg_last[i];
1681 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
1682 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT);
1683 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
1684 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1685 SET_REGNO_REG_SET (&deps->reg_conditional_sets, i);
1688 else
1690 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
1692 struct deps_reg *reg_last = &deps->reg_last[i];
1693 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
1694 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
1695 reg_last->uses_length++;
1696 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1698 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
1700 struct deps_reg *reg_last = &deps->reg_last[i];
1701 if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
1702 || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
1704 add_dependence_list_and_free (insn, &reg_last->sets, 0,
1705 REG_DEP_OUTPUT);
1706 add_dependence_list_and_free (insn, &reg_last->uses, 0,
1707 REG_DEP_ANTI);
1708 add_dependence_list_and_free (insn, &reg_last->clobbers, 0,
1709 REG_DEP_OUTPUT);
1710 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1711 reg_last->clobbers_length = 0;
1712 reg_last->uses_length = 0;
1714 else
1716 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
1717 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
1719 reg_last->clobbers_length++;
1720 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
1722 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
1724 struct deps_reg *reg_last = &deps->reg_last[i];
1725 add_dependence_list_and_free (insn, &reg_last->sets, 0,
1726 REG_DEP_OUTPUT);
1727 add_dependence_list_and_free (insn, &reg_last->clobbers, 0,
1728 REG_DEP_OUTPUT);
1729 add_dependence_list_and_free (insn, &reg_last->uses, 0,
1730 REG_DEP_ANTI);
1731 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1732 reg_last->uses_length = 0;
1733 reg_last->clobbers_length = 0;
1734 CLEAR_REGNO_REG_SET (&deps->reg_conditional_sets, i);
1738 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
1739 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
1740 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
1742 CLEAR_REG_SET (reg_pending_uses);
1743 CLEAR_REG_SET (reg_pending_clobbers);
1744 CLEAR_REG_SET (reg_pending_sets);
1746 /* If we are currently in a libcall scheduling group, then mark the
1747 current insn as being in a scheduling group and that it can not
1748 be moved into a different basic block. */
1750 if (deps->libcall_block_tail_insn)
1752 SCHED_GROUP_P (insn) = 1;
1753 CANT_MOVE (insn) = 1;
1756 /* If a post-call group is still open, see if it should remain so.
1757 This insn must be a simple move of a hard reg to a pseudo or
1758 vice-versa.
1760 We must avoid moving these insns for correctness on
1761 SMALL_REGISTER_CLASS machines, and for special registers like
1762 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
1763 hard regs for all targets. */
1765 if (deps->in_post_call_group_p)
1767 rtx tmp, set = single_set (insn);
1768 int src_regno, dest_regno;
1770 if (set == NULL)
1771 goto end_call_group;
1773 tmp = SET_DEST (set);
1774 if (GET_CODE (tmp) == SUBREG)
1775 tmp = SUBREG_REG (tmp);
1776 if (REG_P (tmp))
1777 dest_regno = REGNO (tmp);
1778 else
1779 goto end_call_group;
1781 tmp = SET_SRC (set);
1782 if (GET_CODE (tmp) == SUBREG)
1783 tmp = SUBREG_REG (tmp);
1784 if ((GET_CODE (tmp) == PLUS
1785 || GET_CODE (tmp) == MINUS)
1786 && REG_P (XEXP (tmp, 0))
1787 && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
1788 && dest_regno == STACK_POINTER_REGNUM)
1789 src_regno = STACK_POINTER_REGNUM;
1790 else if (REG_P (tmp))
1791 src_regno = REGNO (tmp);
1792 else
1793 goto end_call_group;
1795 if (src_regno < FIRST_PSEUDO_REGISTER
1796 || dest_regno < FIRST_PSEUDO_REGISTER)
1798 if (deps->in_post_call_group_p == post_call_initial)
1799 deps->in_post_call_group_p = post_call;
1801 SCHED_GROUP_P (insn) = 1;
1802 CANT_MOVE (insn) = 1;
1804 else
1806 end_call_group:
1807 deps->in_post_call_group_p = not_post_call;
1811 /* Fixup the dependencies in the sched group. */
1812 if (SCHED_GROUP_P (insn))
1813 fixup_sched_groups (insn);
1816 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
1817 dependencies for each insn. */
1819 void
1820 sched_analyze (struct deps *deps, rtx head, rtx tail)
1822 rtx insn;
1824 if (current_sched_info->use_cselib)
1825 cselib_init (true);
1827 /* Before reload, if the previous block ended in a call, show that
1828 we are inside a post-call group, so as to keep the lifetimes of
1829 hard registers correct. */
1830 if (! reload_completed && !LABEL_P (head))
1832 insn = prev_nonnote_insn (head);
1833 if (insn && CALL_P (insn))
1834 deps->in_post_call_group_p = post_call_initial;
1836 for (insn = head;; insn = NEXT_INSN (insn))
1838 rtx link, end_seq, r0, set;
1840 if (INSN_P (insn))
1842 /* These two lists will be freed in schedule_insn (). */
1843 INSN_BACK_DEPS (insn) = create_deps_list (false);
1844 INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list (false);
1846 /* This one should be allocated on the obstack because it should live
1847 till the scheduling ends. */
1848 INSN_FORW_DEPS (insn) = create_deps_list (true);
1851 if (NONJUMP_INSN_P (insn) || JUMP_P (insn))
1853 /* Make each JUMP_INSN a scheduling barrier for memory
1854 references. */
1855 if (JUMP_P (insn))
1857 /* Keep the list a reasonable size. */
1858 if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
1859 flush_pending_lists (deps, insn, true, true);
1860 else
1861 deps->last_pending_memory_flush
1862 = alloc_INSN_LIST (insn, deps->last_pending_memory_flush);
1864 sched_analyze_insn (deps, PATTERN (insn), insn);
1866 else if (CALL_P (insn))
1868 int i;
1870 CANT_MOVE (insn) = 1;
1872 if (find_reg_note (insn, REG_SETJMP, NULL))
1874 /* This is setjmp. Assume that all registers, not just
1875 hard registers, may be clobbered by this call. */
1876 reg_pending_barrier = MOVE_BARRIER;
1878 else
1880 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1881 /* A call may read and modify global register variables. */
1882 if (global_regs[i])
1884 SET_REGNO_REG_SET (reg_pending_sets, i);
1885 SET_REGNO_REG_SET (reg_pending_uses, i);
1887 /* Other call-clobbered hard regs may be clobbered.
1888 Since we only have a choice between 'might be clobbered'
1889 and 'definitely not clobbered', we must include all
1890 partly call-clobbered registers here. */
1891 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
1892 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1893 SET_REGNO_REG_SET (reg_pending_clobbers, i);
1894 /* We don't know what set of fixed registers might be used
1895 by the function, but it is certain that the stack pointer
1896 is among them, but be conservative. */
1897 else if (fixed_regs[i])
1898 SET_REGNO_REG_SET (reg_pending_uses, i);
1899 /* The frame pointer is normally not used by the function
1900 itself, but by the debugger. */
1901 /* ??? MIPS o32 is an exception. It uses the frame pointer
1902 in the macro expansion of jal but does not represent this
1903 fact in the call_insn rtl. */
1904 else if (i == FRAME_POINTER_REGNUM
1905 || (i == HARD_FRAME_POINTER_REGNUM
1906 && (! reload_completed || frame_pointer_needed)))
1907 SET_REGNO_REG_SET (reg_pending_uses, i);
1910 /* For each insn which shouldn't cross a call, add a dependence
1911 between that insn and this call insn. */
1912 add_dependence_list_and_free (insn, &deps->sched_before_next_call, 1,
1913 REG_DEP_ANTI);
1915 sched_analyze_insn (deps, PATTERN (insn), insn);
1917 /* In the absence of interprocedural alias analysis, we must flush
1918 all pending reads and writes, and start new dependencies starting
1919 from here. But only flush writes for constant calls (which may
1920 be passed a pointer to something we haven't written yet). */
1921 flush_pending_lists (deps, insn, true, !CONST_OR_PURE_CALL_P (insn));
1923 /* Remember the last function call for limiting lifetimes. */
1924 free_INSN_LIST_list (&deps->last_function_call);
1925 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
1927 /* Before reload, begin a post-call group, so as to keep the
1928 lifetimes of hard registers correct. */
1929 if (! reload_completed)
1930 deps->in_post_call_group_p = post_call;
1933 /* EH_REGION insn notes can not appear until well after we complete
1934 scheduling. */
1935 if (NOTE_P (insn))
1936 gcc_assert (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
1937 && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END);
1939 if (current_sched_info->use_cselib)
1940 cselib_process_insn (insn);
1942 /* Now that we have completed handling INSN, check and see if it is
1943 a CLOBBER beginning a libcall block. If it is, record the
1944 end of the libcall sequence.
1946 We want to schedule libcall blocks as a unit before reload. While
1947 this restricts scheduling, it preserves the meaning of a libcall
1948 block.
1950 As a side effect, we may get better code due to decreased register
1951 pressure as well as less chance of a foreign insn appearing in
1952 a libcall block. */
1953 if (!reload_completed
1954 /* Note we may have nested libcall sequences. We only care about
1955 the outermost libcall sequence. */
1956 && deps->libcall_block_tail_insn == 0
1957 /* The sequence must start with a clobber of a register. */
1958 && NONJUMP_INSN_P (insn)
1959 && GET_CODE (PATTERN (insn)) == CLOBBER
1960 && (r0 = XEXP (PATTERN (insn), 0), REG_P (r0))
1961 && REG_P (XEXP (PATTERN (insn), 0))
1962 /* The CLOBBER must also have a REG_LIBCALL note attached. */
1963 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1964 && (end_seq = XEXP (link, 0)) != 0
1965 /* The insn referenced by the REG_LIBCALL note must be a
1966 simple nop copy with the same destination as the register
1967 mentioned in the clobber. */
1968 && (set = single_set (end_seq)) != 0
1969 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1970 /* And finally the insn referenced by the REG_LIBCALL must
1971 also contain a REG_EQUAL note and a REG_RETVAL note. */
1972 && find_reg_note (end_seq, REG_EQUAL, NULL_RTX) != 0
1973 && find_reg_note (end_seq, REG_RETVAL, NULL_RTX) != 0)
1974 deps->libcall_block_tail_insn = XEXP (link, 0);
1976 /* If we have reached the end of a libcall block, then close the
1977 block. */
1978 if (deps->libcall_block_tail_insn == insn)
1979 deps->libcall_block_tail_insn = 0;
1981 if (insn == tail)
1983 if (current_sched_info->use_cselib)
1984 cselib_finish ();
1985 return;
1988 gcc_unreachable ();
1992 /* The following function adds forward dependence (FROM, TO) with
1993 given DEP_TYPE. The forward dependence should be not exist before. */
1995 void
1996 add_forw_dep (dep_link_t link)
1998 dep_t dep = DEP_LINK_DEP (link);
1999 rtx to = DEP_CON (dep);
2000 rtx from = DEP_PRO (dep);
2002 #ifdef ENABLE_CHECKING
2003 /* If add_dependence is working properly there should never
2004 be notes, deleted insns or duplicates in the backward
2005 links. Thus we need not check for them here.
2007 However, if we have enabled checking we might as well go
2008 ahead and verify that add_dependence worked properly. */
2009 gcc_assert (INSN_P (from));
2010 gcc_assert (!INSN_DELETED_P (from));
2011 if (true_dependency_cache)
2013 gcc_assert (!bitmap_bit_p (&forward_dependency_cache[INSN_LUID (from)],
2014 INSN_LUID (to)));
2015 bitmap_set_bit (&forward_dependency_cache[INSN_LUID (from)],
2016 INSN_LUID (to));
2019 gcc_assert (find_link_by_con_in_deps_list (INSN_FORW_DEPS (from), to)
2020 == NULL);
2021 #endif
2023 add_to_deps_list (DEP_NODE_FORW (DEP_LINK_NODE (link)),
2024 INSN_FORW_DEPS (from));
2026 INSN_DEP_COUNT (to) += 1;
2029 /* Examine insns in the range [ HEAD, TAIL ] and Use the backward
2030 dependences from INSN_BACK_DEPS list to build forward dependences in
2031 INSN_FORW_DEPS. */
2033 void
2034 compute_forward_dependences (rtx head, rtx tail)
2036 rtx insn;
2037 rtx next_tail;
2039 next_tail = NEXT_INSN (tail);
2040 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
2042 dep_link_t link;
2044 if (! INSN_P (insn))
2045 continue;
2047 if (current_sched_info->flags & DO_SPECULATION)
2049 /* We will add links, preserving order, from INSN_BACK_DEPS to
2050 NEW. */
2051 dep_link_t new = NULL;
2053 link = DEPS_LIST_FIRST (INSN_BACK_DEPS (insn));
2055 while (link != NULL)
2057 dep_link_t next = DEP_LINK_NEXT (link);
2059 detach_dep_link (link);
2060 adjust_add_sorted_back_dep (insn, link, &new);
2062 link = next;
2065 /* Attach NEW to be the list of backward dependencies. */
2066 if (new != NULL)
2068 DEP_LINK_PREV_NEXTP (new)
2069 = &DEPS_LIST_FIRST (INSN_BACK_DEPS (insn));
2071 DEPS_LIST_FIRST (INSN_BACK_DEPS (insn)) = new;
2075 FOR_EACH_DEP_LINK (link, INSN_BACK_DEPS (insn))
2076 add_forw_dep (link);
2080 /* Initialize variables for region data dependence analysis.
2081 n_bbs is the number of region blocks. */
2083 void
2084 init_deps (struct deps *deps)
2086 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
2088 deps->max_reg = max_reg;
2089 deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
2090 INIT_REG_SET (&deps->reg_last_in_use);
2091 INIT_REG_SET (&deps->reg_conditional_sets);
2093 deps->pending_read_insns = 0;
2094 deps->pending_read_mems = 0;
2095 deps->pending_write_insns = 0;
2096 deps->pending_write_mems = 0;
2097 deps->pending_read_list_length = 0;
2098 deps->pending_write_list_length = 0;
2099 deps->pending_flush_length = 0;
2100 deps->last_pending_memory_flush = 0;
2101 deps->last_function_call = 0;
2102 deps->sched_before_next_call = 0;
2103 deps->in_post_call_group_p = not_post_call;
2104 deps->libcall_block_tail_insn = 0;
2107 /* Free insn lists found in DEPS. */
2109 void
2110 free_deps (struct deps *deps)
2112 unsigned i;
2113 reg_set_iterator rsi;
2115 free_INSN_LIST_list (&deps->pending_read_insns);
2116 free_EXPR_LIST_list (&deps->pending_read_mems);
2117 free_INSN_LIST_list (&deps->pending_write_insns);
2118 free_EXPR_LIST_list (&deps->pending_write_mems);
2119 free_INSN_LIST_list (&deps->last_pending_memory_flush);
2121 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
2122 times. For a testcase with 42000 regs and 8000 small basic blocks,
2123 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
2124 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2126 struct deps_reg *reg_last = &deps->reg_last[i];
2127 if (reg_last->uses)
2128 free_INSN_LIST_list (&reg_last->uses);
2129 if (reg_last->sets)
2130 free_INSN_LIST_list (&reg_last->sets);
2131 if (reg_last->clobbers)
2132 free_INSN_LIST_list (&reg_last->clobbers);
2134 CLEAR_REG_SET (&deps->reg_last_in_use);
2135 CLEAR_REG_SET (&deps->reg_conditional_sets);
2137 free (deps->reg_last);
2140 /* If it is profitable to use them, initialize caches for tracking
2141 dependency information. LUID is the number of insns to be scheduled,
2142 it is used in the estimate of profitability. */
2144 void
2145 init_dependency_caches (int luid)
2147 /* ?!? We could save some memory by computing a per-region luid mapping
2148 which could reduce both the number of vectors in the cache and the size
2149 of each vector. Instead we just avoid the cache entirely unless the
2150 average number of instructions in a basic block is very high. See
2151 the comment before the declaration of true_dependency_cache for
2152 what we consider "very high". */
2153 if (luid / n_basic_blocks > 100 * 5)
2155 cache_size = 0;
2156 extend_dependency_caches (luid, true);
2159 /* Lifetime of this obstack is whole function scheduling (not single region
2160 scheduling) because some dependencies can be manually generated for
2161 outside regions. See dont_calc_deps in sched-{rgn, ebb}.c .
2163 Possible solution would be to have two obstacks:
2164 * the big one for regular dependencies with region scheduling lifetime,
2165 * and the small one for manually generated dependencies with function
2166 scheduling lifetime. */
2167 gcc_obstack_init (&deps_obstack);
2170 /* Create or extend (depending on CREATE_P) dependency caches to
2171 size N. */
2172 void
2173 extend_dependency_caches (int n, bool create_p)
2175 if (create_p || true_dependency_cache)
2177 int i, luid = cache_size + n;
2179 true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
2180 luid);
2181 output_dependency_cache = XRESIZEVEC (bitmap_head,
2182 output_dependency_cache, luid);
2183 anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
2184 luid);
2185 #ifdef ENABLE_CHECKING
2186 forward_dependency_cache = XRESIZEVEC (bitmap_head,
2187 forward_dependency_cache, luid);
2188 #endif
2189 if (current_sched_info->flags & DO_SPECULATION)
2190 spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
2191 luid);
2193 for (i = cache_size; i < luid; i++)
2195 bitmap_initialize (&true_dependency_cache[i], 0);
2196 bitmap_initialize (&output_dependency_cache[i], 0);
2197 bitmap_initialize (&anti_dependency_cache[i], 0);
2198 #ifdef ENABLE_CHECKING
2199 bitmap_initialize (&forward_dependency_cache[i], 0);
2200 #endif
2201 if (current_sched_info->flags & DO_SPECULATION)
2202 bitmap_initialize (&spec_dependency_cache[i], 0);
2204 cache_size = luid;
2208 /* Free the caches allocated in init_dependency_caches. */
2210 void
2211 free_dependency_caches (void)
2213 obstack_free (&deps_obstack, NULL);
2215 if (true_dependency_cache)
2217 int i;
2219 for (i = 0; i < cache_size; i++)
2221 bitmap_clear (&true_dependency_cache[i]);
2222 bitmap_clear (&output_dependency_cache[i]);
2223 bitmap_clear (&anti_dependency_cache[i]);
2224 #ifdef ENABLE_CHECKING
2225 bitmap_clear (&forward_dependency_cache[i]);
2226 #endif
2227 if (current_sched_info->flags & DO_SPECULATION)
2228 bitmap_clear (&spec_dependency_cache[i]);
2230 free (true_dependency_cache);
2231 true_dependency_cache = NULL;
2232 free (output_dependency_cache);
2233 output_dependency_cache = NULL;
2234 free (anti_dependency_cache);
2235 anti_dependency_cache = NULL;
2236 #ifdef ENABLE_CHECKING
2237 free (forward_dependency_cache);
2238 forward_dependency_cache = NULL;
2239 #endif
2240 if (current_sched_info->flags & DO_SPECULATION)
2242 free (spec_dependency_cache);
2243 spec_dependency_cache = NULL;
2248 /* Initialize some global variables needed by the dependency analysis
2249 code. */
2251 void
2252 init_deps_global (void)
2254 reg_pending_sets = ALLOC_REG_SET (&reg_obstack);
2255 reg_pending_clobbers = ALLOC_REG_SET (&reg_obstack);
2256 reg_pending_uses = ALLOC_REG_SET (&reg_obstack);
2257 reg_pending_barrier = NOT_A_BARRIER;
2260 /* Free everything used by the dependency analysis code. */
2262 void
2263 finish_deps_global (void)
2265 FREE_REG_SET (reg_pending_sets);
2266 FREE_REG_SET (reg_pending_clobbers);
2267 FREE_REG_SET (reg_pending_uses);
2270 /* Insert LINK into the dependence chain pointed to by LINKP and
2271 maintain the sort order. */
2272 static void
2273 adjust_add_sorted_back_dep (rtx insn, dep_link_t link, dep_link_t *linkp)
2275 gcc_assert (current_sched_info->flags & DO_SPECULATION);
2277 /* If the insn cannot move speculatively, but the link is speculative,
2278 make it hard dependence. */
2279 if (HAS_INTERNAL_DEP (insn)
2280 && (DEP_LINK_STATUS (link) & SPECULATIVE))
2282 DEP_LINK_STATUS (link) &= ~SPECULATIVE;
2284 if (true_dependency_cache)
2285 bitmap_clear_bit (&spec_dependency_cache[INSN_LUID (insn)],
2286 INSN_LUID (DEP_LINK_PRO (link)));
2289 /* Non-speculative links go at the head of deps_list, followed by
2290 speculative links. */
2291 if (DEP_LINK_STATUS (link) & SPECULATIVE)
2292 while (*linkp && !(DEP_LINK_STATUS (*linkp) & SPECULATIVE))
2293 linkp = &DEP_LINK_NEXT (*linkp);
2295 attach_dep_link (link, linkp);
2297 if (CHECK)
2298 gcc_assert (deps_list_consistent_p (INSN_BACK_DEPS (insn)));
2301 /* Move the dependence pointed to by LINKP to the back dependencies
2302 of INSN, and also add this dependence to the forward ones. All dep_links,
2303 except one pointed to by LINKP, must be sorted. */
2304 static void
2305 adjust_back_add_forw_dep (rtx insn, dep_link_t *linkp)
2307 dep_link_t link;
2309 gcc_assert (current_sched_info->flags & DO_SPECULATION);
2311 link = *linkp;
2312 detach_dep_link (link);
2314 adjust_add_sorted_back_dep (insn, link,
2315 &DEPS_LIST_FIRST (INSN_BACK_DEPS (insn)));
2316 add_forw_dep (link);
2319 /* Remove forward dependence described by L. */
2320 static void
2321 delete_forw_dep (dep_link_t l)
2323 gcc_assert (current_sched_info->flags & DO_SPECULATION);
2325 #ifdef ENABLE_CHECKING
2326 if (true_dependency_cache)
2327 bitmap_clear_bit (&forward_dependency_cache[INSN_LUID (DEP_LINK_PRO (l))],
2328 INSN_LUID (DEP_LINK_CON (l)));
2329 #endif
2331 detach_dep_link (l);
2333 INSN_DEP_COUNT (DEP_LINK_CON (l))--;
2336 /* Estimate the weakness of dependence between MEM1 and MEM2. */
2337 static dw_t
2338 estimate_dep_weak (rtx mem1, rtx mem2)
2340 rtx r1, r2;
2342 if (mem1 == mem2)
2343 /* MEMs are the same - don't speculate. */
2344 return MIN_DEP_WEAK;
2346 r1 = XEXP (mem1, 0);
2347 r2 = XEXP (mem2, 0);
2349 if (r1 == r2
2350 || (REG_P (r1) && REG_P (r2)
2351 && REGNO (r1) == REGNO (r2)))
2352 /* Again, MEMs are the same. */
2353 return MIN_DEP_WEAK;
2354 else if ((REG_P (r1) && !REG_P (r2))
2355 || (!REG_P (r1) && REG_P (r2)))
2356 /* Different addressing modes - reason to be more speculative,
2357 than usual. */
2358 return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
2359 else
2360 /* We can't say anything about the dependence. */
2361 return UNCERTAIN_DEP_WEAK;
2364 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
2365 This function can handle same INSN and ELEM (INSN == ELEM).
2366 It is a convenience wrapper. */
2367 void
2368 add_dependence (rtx insn, rtx elem, enum reg_note dep_type)
2370 ds_t ds;
2372 if (dep_type == REG_DEP_TRUE)
2373 ds = DEP_TRUE;
2374 else if (dep_type == REG_DEP_OUTPUT)
2375 ds = DEP_OUTPUT;
2376 else if (dep_type == REG_DEP_ANTI)
2377 ds = DEP_ANTI;
2378 else
2379 gcc_unreachable ();
2381 maybe_add_or_update_back_dep_1 (insn, elem, dep_type, ds, 0, 0, 0);
2384 /* Add or update backward dependence between INSN and ELEM
2385 with given type DEP_TYPE and dep_status DS.
2386 This function is a convenience wrapper. */
2387 enum DEPS_ADJUST_RESULT
2388 add_or_update_back_dep (rtx insn, rtx elem, enum reg_note dep_type, ds_t ds)
2390 return add_or_update_back_dep_1 (insn, elem, dep_type, ds, 0, 0, 0);
2393 /* Add or update both backward and forward dependencies between INSN and ELEM
2394 with given type DEP_TYPE and dep_status DS. */
2395 void
2396 add_or_update_back_forw_dep (rtx insn, rtx elem, enum reg_note dep_type,
2397 ds_t ds)
2399 enum DEPS_ADJUST_RESULT res;
2400 dep_link_t *linkp;
2402 res = add_or_update_back_dep_1 (insn, elem, dep_type, ds, 0, 0, &linkp);
2404 if (res == DEP_CHANGED || res == DEP_CREATED)
2406 if (res == DEP_CHANGED)
2407 delete_forw_dep (DEP_NODE_FORW (DEP_LINK_NODE (*linkp)));
2408 else if (res == DEP_CREATED)
2409 linkp = &DEPS_LIST_FIRST (INSN_BACK_DEPS (insn));
2411 adjust_back_add_forw_dep (insn, linkp);
2415 /* Add both backward and forward dependencies between INSN and ELEM
2416 with given type DEP_TYPE and dep_status DS. */
2417 void
2418 add_back_forw_dep (rtx insn, rtx elem, enum reg_note dep_type, ds_t ds)
2420 add_back_dep (insn, elem, dep_type, ds);
2421 adjust_back_add_forw_dep (insn, &DEPS_LIST_FIRST (INSN_BACK_DEPS (insn)));
2423 if (CHECK)
2424 gcc_assert (deps_list_consistent_p (INSN_BACK_DEPS (insn)));
2427 /* Remove a dependency referred to by L. */
2428 void
2429 delete_back_forw_dep (dep_link_t l)
2431 dep_node_t n = DEP_LINK_NODE (l);
2433 gcc_assert (current_sched_info->flags & DO_SPECULATION);
2435 if (true_dependency_cache != NULL)
2437 dep_t dep = DEP_NODE_DEP (n);
2438 int elem_luid = INSN_LUID (DEP_PRO (dep));
2439 int insn_luid = INSN_LUID (DEP_CON (dep));
2441 bitmap_clear_bit (&true_dependency_cache[insn_luid], elem_luid);
2442 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
2443 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
2444 bitmap_clear_bit (&spec_dependency_cache[insn_luid], elem_luid);
2447 delete_forw_dep (DEP_NODE_FORW (n));
2448 detach_dep_link (DEP_NODE_BACK (n));
2451 /* Return weakness of speculative type TYPE in the dep_status DS. */
2452 dw_t
2453 get_dep_weak (ds_t ds, ds_t type)
2455 ds = ds & type;
2456 switch (type)
2458 case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
2459 case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
2460 case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
2461 case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
2462 default: gcc_unreachable ();
2465 gcc_assert (MIN_DEP_WEAK <= ds && ds <= MAX_DEP_WEAK);
2466 return (dw_t) ds;
2469 /* Return the dep_status, which has the same parameters as DS, except for
2470 speculative type TYPE, that will have weakness DW. */
2471 ds_t
2472 set_dep_weak (ds_t ds, ds_t type, dw_t dw)
2474 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
2476 ds &= ~type;
2477 switch (type)
2479 case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
2480 case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
2481 case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
2482 case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
2483 default: gcc_unreachable ();
2485 return ds;
2488 /* Return the join of two dep_statuses DS1 and DS2. */
2489 ds_t
2490 ds_merge (ds_t ds1, ds_t ds2)
2492 ds_t ds, t;
2494 gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
2496 ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
2498 t = FIRST_SPEC_TYPE;
2501 if ((ds1 & t) && !(ds2 & t))
2502 ds |= ds1 & t;
2503 else if (!(ds1 & t) && (ds2 & t))
2504 ds |= ds2 & t;
2505 else if ((ds1 & t) && (ds2 & t))
2507 ds_t dw;
2509 dw = ((ds_t) get_dep_weak (ds1, t)) * ((ds_t) get_dep_weak (ds2, t));
2510 dw /= MAX_DEP_WEAK;
2511 if (dw < MIN_DEP_WEAK)
2512 dw = MIN_DEP_WEAK;
2514 ds = set_dep_weak (ds, t, (dw_t) dw);
2517 if (t == LAST_SPEC_TYPE)
2518 break;
2519 t <<= SPEC_TYPE_SHIFT;
2521 while (1);
2523 return ds;
2526 #ifdef INSN_SCHEDULING
2527 #ifdef ENABLE_CHECKING
2528 /* Verify that dependence type and status are consistent.
2529 If RELAXED_P is true, then skip dep_weakness checks. */
2530 static void
2531 check_dep_status (enum reg_note dt, ds_t ds, bool relaxed_p)
2533 /* Check that dependence type contains the same bits as the status. */
2534 if (dt == REG_DEP_TRUE)
2535 gcc_assert (ds & DEP_TRUE);
2536 else if (dt == REG_DEP_OUTPUT)
2537 gcc_assert ((ds & DEP_OUTPUT)
2538 && !(ds & DEP_TRUE));
2539 else
2540 gcc_assert ((dt == REG_DEP_ANTI)
2541 && (ds & DEP_ANTI)
2542 && !(ds & (DEP_OUTPUT | DEP_TRUE)));
2544 /* HARD_DEP can not appear in dep_status of a link. */
2545 gcc_assert (!(ds & HARD_DEP));
2547 /* Check that dependence status is set correctly when speculation is not
2548 supported. */
2549 if (!(current_sched_info->flags & DO_SPECULATION))
2550 gcc_assert (!(ds & SPECULATIVE));
2551 else if (ds & SPECULATIVE)
2553 if (!relaxed_p)
2555 ds_t type = FIRST_SPEC_TYPE;
2557 /* Check that dependence weakness is in proper range. */
2560 if (ds & type)
2561 get_dep_weak (ds, type);
2563 if (type == LAST_SPEC_TYPE)
2564 break;
2565 type <<= SPEC_TYPE_SHIFT;
2567 while (1);
2570 if (ds & BEGIN_SPEC)
2572 /* Only true dependence can be data speculative. */
2573 if (ds & BEGIN_DATA)
2574 gcc_assert (ds & DEP_TRUE);
2576 /* Control dependencies in the insn scheduler are represented by
2577 anti-dependencies, therefore only anti dependence can be
2578 control speculative. */
2579 if (ds & BEGIN_CONTROL)
2580 gcc_assert (ds & DEP_ANTI);
2582 else
2584 /* Subsequent speculations should resolve true dependencies. */
2585 gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
2588 /* Check that true and anti dependencies can't have other speculative
2589 statuses. */
2590 if (ds & DEP_TRUE)
2591 gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
2592 /* An output dependence can't be speculative at all. */
2593 gcc_assert (!(ds & DEP_OUTPUT));
2594 if (ds & DEP_ANTI)
2595 gcc_assert (ds & BEGIN_CONTROL);
2598 #endif
2599 #endif