* events.c (hash_param_callback): Read and pass parameter type.
[official-gcc.git] / gcc / sched-deps.c
blob1f1a76c10901496c3deeccfd8e77098bb89ccff7
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, 2008, 2009
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"
44 #include "ira.h"
45 #include "target.h"
47 #ifdef INSN_SCHEDULING
49 #ifdef ENABLE_CHECKING
50 #define CHECK (true)
51 #else
52 #define CHECK (false)
53 #endif
55 /* Holds current parameters for the dependency analyzer. */
56 struct sched_deps_info_def *sched_deps_info;
58 /* The data is specific to the Haifa scheduler. */
59 VEC(haifa_deps_insn_data_def, heap) *h_d_i_d = NULL;
61 /* Return the major type present in the DS. */
62 enum reg_note
63 ds_to_dk (ds_t ds)
65 if (ds & DEP_TRUE)
66 return REG_DEP_TRUE;
68 if (ds & DEP_OUTPUT)
69 return REG_DEP_OUTPUT;
71 gcc_assert (ds & DEP_ANTI);
73 return REG_DEP_ANTI;
76 /* Return equivalent dep_status. */
77 ds_t
78 dk_to_ds (enum reg_note dk)
80 switch (dk)
82 case REG_DEP_TRUE:
83 return DEP_TRUE;
85 case REG_DEP_OUTPUT:
86 return DEP_OUTPUT;
88 default:
89 gcc_assert (dk == REG_DEP_ANTI);
90 return DEP_ANTI;
94 /* Functions to operate with dependence information container - dep_t. */
96 /* Init DEP with the arguments. */
97 void
98 init_dep_1 (dep_t dep, rtx pro, rtx con, enum reg_note type, ds_t ds)
100 DEP_PRO (dep) = pro;
101 DEP_CON (dep) = con;
102 DEP_TYPE (dep) = type;
103 DEP_STATUS (dep) = ds;
106 /* Init DEP with the arguments.
107 While most of the scheduler (including targets) only need the major type
108 of the dependency, it is convenient to hide full dep_status from them. */
109 void
110 init_dep (dep_t dep, rtx pro, rtx con, enum reg_note kind)
112 ds_t ds;
114 if ((current_sched_info->flags & USE_DEPS_LIST))
115 ds = dk_to_ds (kind);
116 else
117 ds = -1;
119 init_dep_1 (dep, pro, con, kind, ds);
122 /* Make a copy of FROM in TO. */
123 static void
124 copy_dep (dep_t to, dep_t from)
126 memcpy (to, from, sizeof (*to));
129 static void dump_ds (FILE *, ds_t);
131 /* Define flags for dump_dep (). */
133 /* Dump producer of the dependence. */
134 #define DUMP_DEP_PRO (2)
136 /* Dump consumer of the dependence. */
137 #define DUMP_DEP_CON (4)
139 /* Dump type of the dependence. */
140 #define DUMP_DEP_TYPE (8)
142 /* Dump status of the dependence. */
143 #define DUMP_DEP_STATUS (16)
145 /* Dump all information about the dependence. */
146 #define DUMP_DEP_ALL (DUMP_DEP_PRO | DUMP_DEP_CON | DUMP_DEP_TYPE \
147 |DUMP_DEP_STATUS)
149 /* Dump DEP to DUMP.
150 FLAGS is a bit mask specifying what information about DEP needs
151 to be printed.
152 If FLAGS has the very first bit set, then dump all information about DEP
153 and propagate this bit into the callee dump functions. */
154 static void
155 dump_dep (FILE *dump, dep_t dep, int flags)
157 if (flags & 1)
158 flags |= DUMP_DEP_ALL;
160 fprintf (dump, "<");
162 if (flags & DUMP_DEP_PRO)
163 fprintf (dump, "%d; ", INSN_UID (DEP_PRO (dep)));
165 if (flags & DUMP_DEP_CON)
166 fprintf (dump, "%d; ", INSN_UID (DEP_CON (dep)));
168 if (flags & DUMP_DEP_TYPE)
170 char t;
171 enum reg_note type = DEP_TYPE (dep);
173 switch (type)
175 case REG_DEP_TRUE:
176 t = 't';
177 break;
179 case REG_DEP_OUTPUT:
180 t = 'o';
181 break;
183 case REG_DEP_ANTI:
184 t = 'a';
185 break;
187 default:
188 gcc_unreachable ();
189 break;
192 fprintf (dump, "%c; ", t);
195 if (flags & DUMP_DEP_STATUS)
197 if (current_sched_info->flags & USE_DEPS_LIST)
198 dump_ds (dump, DEP_STATUS (dep));
201 fprintf (dump, ">");
204 /* Default flags for dump_dep (). */
205 static int dump_dep_flags = (DUMP_DEP_PRO | DUMP_DEP_CON);
207 /* Dump all fields of DEP to STDERR. */
208 void
209 sd_debug_dep (dep_t dep)
211 dump_dep (stderr, dep, 1);
212 fprintf (stderr, "\n");
215 /* Determine whether DEP is a dependency link of a non-debug insn on a
216 debug insn. */
218 static inline bool
219 depl_on_debug_p (dep_link_t dep)
221 return (DEBUG_INSN_P (DEP_LINK_PRO (dep))
222 && !DEBUG_INSN_P (DEP_LINK_CON (dep)));
225 /* Functions to operate with a single link from the dependencies lists -
226 dep_link_t. */
228 /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
229 PREV_NEXT_P. */
230 static void
231 attach_dep_link (dep_link_t l, dep_link_t *prev_nextp)
233 dep_link_t next = *prev_nextp;
235 gcc_assert (DEP_LINK_PREV_NEXTP (l) == NULL
236 && DEP_LINK_NEXT (l) == NULL);
238 /* Init node being inserted. */
239 DEP_LINK_PREV_NEXTP (l) = prev_nextp;
240 DEP_LINK_NEXT (l) = next;
242 /* Fix next node. */
243 if (next != NULL)
245 gcc_assert (DEP_LINK_PREV_NEXTP (next) == prev_nextp);
247 DEP_LINK_PREV_NEXTP (next) = &DEP_LINK_NEXT (l);
250 /* Fix prev node. */
251 *prev_nextp = l;
254 /* Add dep_link LINK to deps_list L. */
255 static void
256 add_to_deps_list (dep_link_t link, deps_list_t l)
258 attach_dep_link (link, &DEPS_LIST_FIRST (l));
260 /* Don't count debug deps. */
261 if (!depl_on_debug_p (link))
262 ++DEPS_LIST_N_LINKS (l);
265 /* Detach dep_link L from the list. */
266 static void
267 detach_dep_link (dep_link_t l)
269 dep_link_t *prev_nextp = DEP_LINK_PREV_NEXTP (l);
270 dep_link_t next = DEP_LINK_NEXT (l);
272 *prev_nextp = next;
274 if (next != NULL)
275 DEP_LINK_PREV_NEXTP (next) = prev_nextp;
277 DEP_LINK_PREV_NEXTP (l) = NULL;
278 DEP_LINK_NEXT (l) = NULL;
281 /* Remove link LINK from list LIST. */
282 static void
283 remove_from_deps_list (dep_link_t link, deps_list_t list)
285 detach_dep_link (link);
287 /* Don't count debug deps. */
288 if (!depl_on_debug_p (link))
289 --DEPS_LIST_N_LINKS (list);
292 /* Move link LINK from list FROM to list TO. */
293 static void
294 move_dep_link (dep_link_t link, deps_list_t from, deps_list_t to)
296 remove_from_deps_list (link, from);
297 add_to_deps_list (link, to);
300 /* Return true of LINK is not attached to any list. */
301 static bool
302 dep_link_is_detached_p (dep_link_t link)
304 return DEP_LINK_PREV_NEXTP (link) == NULL;
307 /* Pool to hold all dependency nodes (dep_node_t). */
308 static alloc_pool dn_pool;
310 /* Number of dep_nodes out there. */
311 static int dn_pool_diff = 0;
313 /* Create a dep_node. */
314 static dep_node_t
315 create_dep_node (void)
317 dep_node_t n = (dep_node_t) pool_alloc (dn_pool);
318 dep_link_t back = DEP_NODE_BACK (n);
319 dep_link_t forw = DEP_NODE_FORW (n);
321 DEP_LINK_NODE (back) = n;
322 DEP_LINK_NEXT (back) = NULL;
323 DEP_LINK_PREV_NEXTP (back) = NULL;
325 DEP_LINK_NODE (forw) = n;
326 DEP_LINK_NEXT (forw) = NULL;
327 DEP_LINK_PREV_NEXTP (forw) = NULL;
329 ++dn_pool_diff;
331 return n;
334 /* Delete dep_node N. N must not be connected to any deps_list. */
335 static void
336 delete_dep_node (dep_node_t n)
338 gcc_assert (dep_link_is_detached_p (DEP_NODE_BACK (n))
339 && dep_link_is_detached_p (DEP_NODE_FORW (n)));
341 --dn_pool_diff;
343 pool_free (dn_pool, n);
346 /* Pool to hold dependencies lists (deps_list_t). */
347 static alloc_pool dl_pool;
349 /* Number of deps_lists out there. */
350 static int dl_pool_diff = 0;
352 /* Functions to operate with dependences lists - deps_list_t. */
354 /* Return true if list L is empty. */
355 static bool
356 deps_list_empty_p (deps_list_t l)
358 return DEPS_LIST_N_LINKS (l) == 0;
361 /* Create a new deps_list. */
362 static deps_list_t
363 create_deps_list (void)
365 deps_list_t l = (deps_list_t) pool_alloc (dl_pool);
367 DEPS_LIST_FIRST (l) = NULL;
368 DEPS_LIST_N_LINKS (l) = 0;
370 ++dl_pool_diff;
371 return l;
374 /* Free deps_list L. */
375 static void
376 free_deps_list (deps_list_t l)
378 gcc_assert (deps_list_empty_p (l));
380 --dl_pool_diff;
382 pool_free (dl_pool, l);
385 /* Return true if there is no dep_nodes and deps_lists out there.
386 After the region is scheduled all the dependency nodes and lists
387 should [generally] be returned to pool. */
388 bool
389 deps_pools_are_empty_p (void)
391 return dn_pool_diff == 0 && dl_pool_diff == 0;
394 /* Remove all elements from L. */
395 static void
396 clear_deps_list (deps_list_t l)
400 dep_link_t link = DEPS_LIST_FIRST (l);
402 if (link == NULL)
403 break;
405 remove_from_deps_list (link, l);
407 while (1);
410 static regset reg_pending_sets;
411 static regset reg_pending_clobbers;
412 static regset reg_pending_uses;
413 static enum reg_pending_barrier_mode reg_pending_barrier;
415 /* Hard registers implicitly clobbered or used (or may be implicitly
416 clobbered or used) by the currently analyzed insn. For example,
417 insn in its constraint has one register class. Even if there is
418 currently no hard register in the insn, the particular hard
419 register will be in the insn after reload pass because the
420 constraint requires it. */
421 static HARD_REG_SET implicit_reg_pending_clobbers;
422 static HARD_REG_SET implicit_reg_pending_uses;
424 /* To speed up the test for duplicate dependency links we keep a
425 record of dependencies created by add_dependence when the average
426 number of instructions in a basic block is very large.
428 Studies have shown that there is typically around 5 instructions between
429 branches for typical C code. So we can make a guess that the average
430 basic block is approximately 5 instructions long; we will choose 100X
431 the average size as a very large basic block.
433 Each insn has associated bitmaps for its dependencies. Each bitmap
434 has enough entries to represent a dependency on any other insn in
435 the insn chain. All bitmap for true dependencies cache is
436 allocated then the rest two ones are also allocated. */
437 static bitmap_head *true_dependency_cache = NULL;
438 static bitmap_head *output_dependency_cache = NULL;
439 static bitmap_head *anti_dependency_cache = NULL;
440 static bitmap_head *spec_dependency_cache = NULL;
441 static int cache_size;
443 static int deps_may_trap_p (const_rtx);
444 static void add_dependence_list (rtx, rtx, int, enum reg_note);
445 static void add_dependence_list_and_free (struct deps *, rtx,
446 rtx *, int, enum reg_note);
447 static void delete_all_dependences (rtx);
448 static void fixup_sched_groups (rtx);
450 static void flush_pending_lists (struct deps *, rtx, int, int);
451 static void sched_analyze_1 (struct deps *, rtx, rtx);
452 static void sched_analyze_2 (struct deps *, rtx, rtx);
453 static void sched_analyze_insn (struct deps *, rtx, rtx);
455 static bool sched_has_condition_p (const_rtx);
456 static int conditions_mutex_p (const_rtx, const_rtx, bool, bool);
458 static enum DEPS_ADJUST_RESULT maybe_add_or_update_dep_1 (dep_t, bool,
459 rtx, rtx);
460 static enum DEPS_ADJUST_RESULT add_or_update_dep_1 (dep_t, bool, rtx, rtx);
462 #ifdef ENABLE_CHECKING
463 static void check_dep (dep_t, bool);
464 #endif
466 /* Return nonzero if a load of the memory reference MEM can cause a trap. */
468 static int
469 deps_may_trap_p (const_rtx mem)
471 const_rtx addr = XEXP (mem, 0);
473 if (REG_P (addr) && REGNO (addr) >= FIRST_PSEUDO_REGISTER)
475 const_rtx t = get_reg_known_value (REGNO (addr));
476 if (t)
477 addr = t;
479 return rtx_addr_can_trap_p (addr);
483 /* Find the condition under which INSN is executed. If REV is not NULL,
484 it is set to TRUE when the returned comparison should be reversed
485 to get the actual condition. */
486 static rtx
487 sched_get_condition_with_rev (const_rtx insn, bool *rev)
489 rtx pat = PATTERN (insn);
490 rtx src;
492 if (pat == 0)
493 return 0;
495 if (rev)
496 *rev = false;
498 if (GET_CODE (pat) == COND_EXEC)
499 return COND_EXEC_TEST (pat);
501 if (!any_condjump_p (insn) || !onlyjump_p (insn))
502 return 0;
504 src = SET_SRC (pc_set (insn));
506 if (XEXP (src, 2) == pc_rtx)
507 return XEXP (src, 0);
508 else if (XEXP (src, 1) == pc_rtx)
510 rtx cond = XEXP (src, 0);
511 enum rtx_code revcode = reversed_comparison_code (cond, insn);
513 if (revcode == UNKNOWN)
514 return 0;
516 if (rev)
517 *rev = true;
518 return cond;
521 return 0;
524 /* True when we can find a condition under which INSN is executed. */
525 static bool
526 sched_has_condition_p (const_rtx insn)
528 return !! sched_get_condition_with_rev (insn, NULL);
533 /* Return nonzero if conditions COND1 and COND2 can never be both true. */
534 static int
535 conditions_mutex_p (const_rtx cond1, const_rtx cond2, bool rev1, bool rev2)
537 if (COMPARISON_P (cond1)
538 && COMPARISON_P (cond2)
539 && GET_CODE (cond1) ==
540 (rev1==rev2
541 ? reversed_comparison_code (cond2, NULL)
542 : GET_CODE (cond2))
543 && XEXP (cond1, 0) == XEXP (cond2, 0)
544 && XEXP (cond1, 1) == XEXP (cond2, 1))
545 return 1;
546 return 0;
549 /* Return true if insn1 and insn2 can never depend on one another because
550 the conditions under which they are executed are mutually exclusive. */
551 bool
552 sched_insns_conditions_mutex_p (const_rtx insn1, const_rtx insn2)
554 rtx cond1, cond2;
555 bool rev1 = false, rev2 = false;
557 /* df doesn't handle conditional lifetimes entirely correctly;
558 calls mess up the conditional lifetimes. */
559 if (!CALL_P (insn1) && !CALL_P (insn2))
561 cond1 = sched_get_condition_with_rev (insn1, &rev1);
562 cond2 = sched_get_condition_with_rev (insn2, &rev2);
563 if (cond1 && cond2
564 && conditions_mutex_p (cond1, cond2, rev1, rev2)
565 /* Make sure first instruction doesn't affect condition of second
566 instruction if switched. */
567 && !modified_in_p (cond1, insn2)
568 /* Make sure second instruction doesn't affect condition of first
569 instruction if switched. */
570 && !modified_in_p (cond2, insn1))
571 return true;
573 return false;
577 /* Return true if INSN can potentially be speculated with type DS. */
578 bool
579 sched_insn_is_legitimate_for_speculation_p (const_rtx insn, ds_t ds)
581 if (HAS_INTERNAL_DEP (insn))
582 return false;
584 if (!NONJUMP_INSN_P (insn))
585 return false;
587 if (SCHED_GROUP_P (insn))
588 return false;
590 if (IS_SPECULATION_CHECK_P (CONST_CAST_RTX (insn)))
591 return false;
593 if (side_effects_p (PATTERN (insn)))
594 return false;
596 if (ds & BE_IN_SPEC)
597 /* The following instructions, which depend on a speculatively scheduled
598 instruction, cannot be speculatively scheduled along. */
600 if (may_trap_p (PATTERN (insn)))
601 /* If instruction might trap, it cannot be speculatively scheduled.
602 For control speculation it's obvious why and for data speculation
603 it's because the insn might get wrong input if speculation
604 wasn't successful. */
605 return false;
607 if ((ds & BE_IN_DATA)
608 && sched_has_condition_p (insn))
609 /* If this is a predicated instruction, then it cannot be
610 speculatively scheduled. See PR35659. */
611 return false;
614 return true;
617 /* Initialize LIST_PTR to point to one of the lists present in TYPES_PTR,
618 initialize RESOLVED_P_PTR with true if that list consists of resolved deps,
619 and remove the type of returned [through LIST_PTR] list from TYPES_PTR.
620 This function is used to switch sd_iterator to the next list.
621 !!! For internal use only. Might consider moving it to sched-int.h. */
622 void
623 sd_next_list (const_rtx insn, sd_list_types_def *types_ptr,
624 deps_list_t *list_ptr, bool *resolved_p_ptr)
626 sd_list_types_def types = *types_ptr;
628 if (types & SD_LIST_HARD_BACK)
630 *list_ptr = INSN_HARD_BACK_DEPS (insn);
631 *resolved_p_ptr = false;
632 *types_ptr = types & ~SD_LIST_HARD_BACK;
634 else if (types & SD_LIST_SPEC_BACK)
636 *list_ptr = INSN_SPEC_BACK_DEPS (insn);
637 *resolved_p_ptr = false;
638 *types_ptr = types & ~SD_LIST_SPEC_BACK;
640 else if (types & SD_LIST_FORW)
642 *list_ptr = INSN_FORW_DEPS (insn);
643 *resolved_p_ptr = false;
644 *types_ptr = types & ~SD_LIST_FORW;
646 else if (types & SD_LIST_RES_BACK)
648 *list_ptr = INSN_RESOLVED_BACK_DEPS (insn);
649 *resolved_p_ptr = true;
650 *types_ptr = types & ~SD_LIST_RES_BACK;
652 else if (types & SD_LIST_RES_FORW)
654 *list_ptr = INSN_RESOLVED_FORW_DEPS (insn);
655 *resolved_p_ptr = true;
656 *types_ptr = types & ~SD_LIST_RES_FORW;
658 else
660 *list_ptr = NULL;
661 *resolved_p_ptr = false;
662 *types_ptr = SD_LIST_NONE;
666 /* Return the summary size of INSN's lists defined by LIST_TYPES. */
668 sd_lists_size (const_rtx insn, sd_list_types_def list_types)
670 int size = 0;
672 while (list_types != SD_LIST_NONE)
674 deps_list_t list;
675 bool resolved_p;
677 sd_next_list (insn, &list_types, &list, &resolved_p);
678 if (list)
679 size += DEPS_LIST_N_LINKS (list);
682 return size;
685 /* Return true if INSN's lists defined by LIST_TYPES are all empty. */
687 bool
688 sd_lists_empty_p (const_rtx insn, sd_list_types_def list_types)
690 while (list_types != SD_LIST_NONE)
692 deps_list_t list;
693 bool resolved_p;
695 sd_next_list (insn, &list_types, &list, &resolved_p);
696 if (!deps_list_empty_p (list))
697 return false;
700 return true;
703 /* Initialize data for INSN. */
704 void
705 sd_init_insn (rtx insn)
707 INSN_HARD_BACK_DEPS (insn) = create_deps_list ();
708 INSN_SPEC_BACK_DEPS (insn) = create_deps_list ();
709 INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list ();
710 INSN_FORW_DEPS (insn) = create_deps_list ();
711 INSN_RESOLVED_FORW_DEPS (insn) = create_deps_list ();
713 if (DEBUG_INSN_P (insn))
714 DEBUG_INSN_SCHED_P (insn) = TRUE;
716 /* ??? It would be nice to allocate dependency caches here. */
719 /* Free data for INSN. */
720 void
721 sd_finish_insn (rtx insn)
723 /* ??? It would be nice to deallocate dependency caches here. */
725 if (DEBUG_INSN_P (insn))
727 gcc_assert (DEBUG_INSN_SCHED_P (insn));
728 DEBUG_INSN_SCHED_P (insn) = FALSE;
731 free_deps_list (INSN_HARD_BACK_DEPS (insn));
732 INSN_HARD_BACK_DEPS (insn) = NULL;
734 free_deps_list (INSN_SPEC_BACK_DEPS (insn));
735 INSN_SPEC_BACK_DEPS (insn) = NULL;
737 free_deps_list (INSN_RESOLVED_BACK_DEPS (insn));
738 INSN_RESOLVED_BACK_DEPS (insn) = NULL;
740 free_deps_list (INSN_FORW_DEPS (insn));
741 INSN_FORW_DEPS (insn) = NULL;
743 free_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
744 INSN_RESOLVED_FORW_DEPS (insn) = NULL;
747 /* Find a dependency between producer PRO and consumer CON.
748 Search through resolved dependency lists if RESOLVED_P is true.
749 If no such dependency is found return NULL,
750 otherwise return the dependency and initialize SD_IT_PTR [if it is nonnull]
751 with an iterator pointing to it. */
752 static dep_t
753 sd_find_dep_between_no_cache (rtx pro, rtx con, bool resolved_p,
754 sd_iterator_def *sd_it_ptr)
756 sd_list_types_def pro_list_type;
757 sd_list_types_def con_list_type;
758 sd_iterator_def sd_it;
759 dep_t dep;
760 bool found_p = false;
762 if (resolved_p)
764 pro_list_type = SD_LIST_RES_FORW;
765 con_list_type = SD_LIST_RES_BACK;
767 else
769 pro_list_type = SD_LIST_FORW;
770 con_list_type = SD_LIST_BACK;
773 /* Walk through either back list of INSN or forw list of ELEM
774 depending on which one is shorter. */
775 if (sd_lists_size (con, con_list_type) < sd_lists_size (pro, pro_list_type))
777 /* Find the dep_link with producer PRO in consumer's back_deps. */
778 FOR_EACH_DEP (con, con_list_type, sd_it, dep)
779 if (DEP_PRO (dep) == pro)
781 found_p = true;
782 break;
785 else
787 /* Find the dep_link with consumer CON in producer's forw_deps. */
788 FOR_EACH_DEP (pro, pro_list_type, sd_it, dep)
789 if (DEP_CON (dep) == con)
791 found_p = true;
792 break;
796 if (found_p)
798 if (sd_it_ptr != NULL)
799 *sd_it_ptr = sd_it;
801 return dep;
804 return NULL;
807 /* Find a dependency between producer PRO and consumer CON.
808 Use dependency [if available] to check if dependency is present at all.
809 Search through resolved dependency lists if RESOLVED_P is true.
810 If the dependency or NULL if none found. */
811 dep_t
812 sd_find_dep_between (rtx pro, rtx con, bool resolved_p)
814 if (true_dependency_cache != NULL)
815 /* Avoiding the list walk below can cut compile times dramatically
816 for some code. */
818 int elem_luid = INSN_LUID (pro);
819 int insn_luid = INSN_LUID (con);
821 gcc_assert (output_dependency_cache != NULL
822 && anti_dependency_cache != NULL);
824 if (!bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid)
825 && !bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid)
826 && !bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
827 return NULL;
830 return sd_find_dep_between_no_cache (pro, con, resolved_p, NULL);
833 /* Add or update a dependence described by DEP.
834 MEM1 and MEM2, if non-null, correspond to memory locations in case of
835 data speculation.
837 The function returns a value indicating if an old entry has been changed
838 or a new entry has been added to insn's backward deps.
840 This function merely checks if producer and consumer is the same insn
841 and doesn't create a dep in this case. Actual manipulation of
842 dependence data structures is performed in add_or_update_dep_1. */
843 static enum DEPS_ADJUST_RESULT
844 maybe_add_or_update_dep_1 (dep_t dep, bool resolved_p, rtx mem1, rtx mem2)
846 rtx elem = DEP_PRO (dep);
847 rtx insn = DEP_CON (dep);
849 gcc_assert (INSN_P (insn) && INSN_P (elem));
851 /* Don't depend an insn on itself. */
852 if (insn == elem)
854 if (sched_deps_info->generate_spec_deps)
855 /* INSN has an internal dependence, which we can't overcome. */
856 HAS_INTERNAL_DEP (insn) = 1;
858 return DEP_NODEP;
861 return add_or_update_dep_1 (dep, resolved_p, mem1, mem2);
864 /* Ask dependency caches what needs to be done for dependence DEP.
865 Return DEP_CREATED if new dependence should be created and there is no
866 need to try to find one searching the dependencies lists.
867 Return DEP_PRESENT if there already is a dependence described by DEP and
868 hence nothing is to be done.
869 Return DEP_CHANGED if there already is a dependence, but it should be
870 updated to incorporate additional information from DEP. */
871 static enum DEPS_ADJUST_RESULT
872 ask_dependency_caches (dep_t dep)
874 int elem_luid = INSN_LUID (DEP_PRO (dep));
875 int insn_luid = INSN_LUID (DEP_CON (dep));
877 gcc_assert (true_dependency_cache != NULL
878 && output_dependency_cache != NULL
879 && anti_dependency_cache != NULL);
881 if (!(current_sched_info->flags & USE_DEPS_LIST))
883 enum reg_note present_dep_type;
885 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
886 present_dep_type = REG_DEP_TRUE;
887 else if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
888 present_dep_type = REG_DEP_OUTPUT;
889 else if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
890 present_dep_type = REG_DEP_ANTI;
891 else
892 /* There is no existing dep so it should be created. */
893 return DEP_CREATED;
895 if ((int) DEP_TYPE (dep) >= (int) present_dep_type)
896 /* DEP does not add anything to the existing dependence. */
897 return DEP_PRESENT;
899 else
901 ds_t present_dep_types = 0;
903 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
904 present_dep_types |= DEP_TRUE;
905 if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
906 present_dep_types |= DEP_OUTPUT;
907 if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
908 present_dep_types |= DEP_ANTI;
910 if (present_dep_types == 0)
911 /* There is no existing dep so it should be created. */
912 return DEP_CREATED;
914 if (!(current_sched_info->flags & DO_SPECULATION)
915 || !bitmap_bit_p (&spec_dependency_cache[insn_luid], elem_luid))
917 if ((present_dep_types | (DEP_STATUS (dep) & DEP_TYPES))
918 == present_dep_types)
919 /* DEP does not add anything to the existing dependence. */
920 return DEP_PRESENT;
922 else
924 /* Only true dependencies can be data speculative and
925 only anti dependencies can be control speculative. */
926 gcc_assert ((present_dep_types & (DEP_TRUE | DEP_ANTI))
927 == present_dep_types);
929 /* if (DEP is SPECULATIVE) then
930 ..we should update DEP_STATUS
931 else
932 ..we should reset existing dep to non-speculative. */
936 return DEP_CHANGED;
939 /* Set dependency caches according to DEP. */
940 static void
941 set_dependency_caches (dep_t dep)
943 int elem_luid = INSN_LUID (DEP_PRO (dep));
944 int insn_luid = INSN_LUID (DEP_CON (dep));
946 if (!(current_sched_info->flags & USE_DEPS_LIST))
948 switch (DEP_TYPE (dep))
950 case REG_DEP_TRUE:
951 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
952 break;
954 case REG_DEP_OUTPUT:
955 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
956 break;
958 case REG_DEP_ANTI:
959 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
960 break;
962 default:
963 gcc_unreachable ();
966 else
968 ds_t ds = DEP_STATUS (dep);
970 if (ds & DEP_TRUE)
971 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
972 if (ds & DEP_OUTPUT)
973 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
974 if (ds & DEP_ANTI)
975 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
977 if (ds & SPECULATIVE)
979 gcc_assert (current_sched_info->flags & DO_SPECULATION);
980 bitmap_set_bit (&spec_dependency_cache[insn_luid], elem_luid);
985 /* Type of dependence DEP have changed from OLD_TYPE. Update dependency
986 caches accordingly. */
987 static void
988 update_dependency_caches (dep_t dep, enum reg_note old_type)
990 int elem_luid = INSN_LUID (DEP_PRO (dep));
991 int insn_luid = INSN_LUID (DEP_CON (dep));
993 /* Clear corresponding cache entry because type of the link
994 may have changed. Keep them if we use_deps_list. */
995 if (!(current_sched_info->flags & USE_DEPS_LIST))
997 switch (old_type)
999 case REG_DEP_OUTPUT:
1000 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1001 break;
1003 case REG_DEP_ANTI:
1004 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1005 break;
1007 default:
1008 gcc_unreachable ();
1012 set_dependency_caches (dep);
1015 /* Convert a dependence pointed to by SD_IT to be non-speculative. */
1016 static void
1017 change_spec_dep_to_hard (sd_iterator_def sd_it)
1019 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1020 dep_link_t link = DEP_NODE_BACK (node);
1021 dep_t dep = DEP_NODE_DEP (node);
1022 rtx elem = DEP_PRO (dep);
1023 rtx insn = DEP_CON (dep);
1025 move_dep_link (link, INSN_SPEC_BACK_DEPS (insn), INSN_HARD_BACK_DEPS (insn));
1027 DEP_STATUS (dep) &= ~SPECULATIVE;
1029 if (true_dependency_cache != NULL)
1030 /* Clear the cache entry. */
1031 bitmap_clear_bit (&spec_dependency_cache[INSN_LUID (insn)],
1032 INSN_LUID (elem));
1035 /* Update DEP to incorporate information from NEW_DEP.
1036 SD_IT points to DEP in case it should be moved to another list.
1037 MEM1 and MEM2, if nonnull, correspond to memory locations in case if
1038 data-speculative dependence should be updated. */
1039 static enum DEPS_ADJUST_RESULT
1040 update_dep (dep_t dep, dep_t new_dep,
1041 sd_iterator_def sd_it ATTRIBUTE_UNUSED,
1042 rtx mem1 ATTRIBUTE_UNUSED,
1043 rtx mem2 ATTRIBUTE_UNUSED)
1045 enum DEPS_ADJUST_RESULT res = DEP_PRESENT;
1046 enum reg_note old_type = DEP_TYPE (dep);
1048 /* If this is a more restrictive type of dependence than the
1049 existing one, then change the existing dependence to this
1050 type. */
1051 if ((int) DEP_TYPE (new_dep) < (int) old_type)
1053 DEP_TYPE (dep) = DEP_TYPE (new_dep);
1054 res = DEP_CHANGED;
1057 if (current_sched_info->flags & USE_DEPS_LIST)
1058 /* Update DEP_STATUS. */
1060 ds_t dep_status = DEP_STATUS (dep);
1061 ds_t ds = DEP_STATUS (new_dep);
1062 ds_t new_status = ds | dep_status;
1064 if (new_status & SPECULATIVE)
1065 /* Either existing dep or a dep we're adding or both are
1066 speculative. */
1068 if (!(ds & SPECULATIVE)
1069 || !(dep_status & SPECULATIVE))
1070 /* The new dep can't be speculative. */
1072 new_status &= ~SPECULATIVE;
1074 if (dep_status & SPECULATIVE)
1075 /* The old dep was speculative, but now it
1076 isn't. */
1077 change_spec_dep_to_hard (sd_it);
1079 else
1081 /* Both are speculative. Merge probabilities. */
1082 if (mem1 != NULL)
1084 dw_t dw;
1086 dw = estimate_dep_weak (mem1, mem2);
1087 ds = set_dep_weak (ds, BEGIN_DATA, dw);
1090 new_status = ds_merge (dep_status, ds);
1094 ds = new_status;
1096 if (dep_status != ds)
1098 DEP_STATUS (dep) = ds;
1099 res = DEP_CHANGED;
1103 if (true_dependency_cache != NULL
1104 && res == DEP_CHANGED)
1105 update_dependency_caches (dep, old_type);
1107 return res;
1110 /* Add or update a dependence described by DEP.
1111 MEM1 and MEM2, if non-null, correspond to memory locations in case of
1112 data speculation.
1114 The function returns a value indicating if an old entry has been changed
1115 or a new entry has been added to insn's backward deps or nothing has
1116 been updated at all. */
1117 static enum DEPS_ADJUST_RESULT
1118 add_or_update_dep_1 (dep_t new_dep, bool resolved_p,
1119 rtx mem1 ATTRIBUTE_UNUSED, rtx mem2 ATTRIBUTE_UNUSED)
1121 bool maybe_present_p = true;
1122 bool present_p = false;
1124 gcc_assert (INSN_P (DEP_PRO (new_dep)) && INSN_P (DEP_CON (new_dep))
1125 && DEP_PRO (new_dep) != DEP_CON (new_dep));
1127 #ifdef ENABLE_CHECKING
1128 check_dep (new_dep, mem1 != NULL);
1129 #endif
1131 if (true_dependency_cache != NULL)
1133 switch (ask_dependency_caches (new_dep))
1135 case DEP_PRESENT:
1136 return DEP_PRESENT;
1138 case DEP_CHANGED:
1139 maybe_present_p = true;
1140 present_p = true;
1141 break;
1143 case DEP_CREATED:
1144 maybe_present_p = false;
1145 present_p = false;
1146 break;
1148 default:
1149 gcc_unreachable ();
1150 break;
1154 /* Check that we don't already have this dependence. */
1155 if (maybe_present_p)
1157 dep_t present_dep;
1158 sd_iterator_def sd_it;
1160 gcc_assert (true_dependency_cache == NULL || present_p);
1162 present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
1163 DEP_CON (new_dep),
1164 resolved_p, &sd_it);
1166 if (present_dep != NULL)
1167 /* We found an existing dependency between ELEM and INSN. */
1168 return update_dep (present_dep, new_dep, sd_it, mem1, mem2);
1169 else
1170 /* We didn't find a dep, it shouldn't present in the cache. */
1171 gcc_assert (!present_p);
1174 /* Might want to check one level of transitivity to save conses.
1175 This check should be done in maybe_add_or_update_dep_1.
1176 Since we made it to add_or_update_dep_1, we must create
1177 (or update) a link. */
1179 if (mem1 != NULL_RTX)
1181 gcc_assert (sched_deps_info->generate_spec_deps);
1182 DEP_STATUS (new_dep) = set_dep_weak (DEP_STATUS (new_dep), BEGIN_DATA,
1183 estimate_dep_weak (mem1, mem2));
1186 sd_add_dep (new_dep, resolved_p);
1188 return DEP_CREATED;
1191 /* Initialize BACK_LIST_PTR with consumer's backward list and
1192 FORW_LIST_PTR with producer's forward list. If RESOLVED_P is true
1193 initialize with lists that hold resolved deps. */
1194 static void
1195 get_back_and_forw_lists (dep_t dep, bool resolved_p,
1196 deps_list_t *back_list_ptr,
1197 deps_list_t *forw_list_ptr)
1199 rtx con = DEP_CON (dep);
1201 if (!resolved_p)
1203 if ((current_sched_info->flags & DO_SPECULATION)
1204 && (DEP_STATUS (dep) & SPECULATIVE))
1205 *back_list_ptr = INSN_SPEC_BACK_DEPS (con);
1206 else
1207 *back_list_ptr = INSN_HARD_BACK_DEPS (con);
1209 *forw_list_ptr = INSN_FORW_DEPS (DEP_PRO (dep));
1211 else
1213 *back_list_ptr = INSN_RESOLVED_BACK_DEPS (con);
1214 *forw_list_ptr = INSN_RESOLVED_FORW_DEPS (DEP_PRO (dep));
1218 /* Add dependence described by DEP.
1219 If RESOLVED_P is true treat the dependence as a resolved one. */
1220 void
1221 sd_add_dep (dep_t dep, bool resolved_p)
1223 dep_node_t n = create_dep_node ();
1224 deps_list_t con_back_deps;
1225 deps_list_t pro_forw_deps;
1226 rtx elem = DEP_PRO (dep);
1227 rtx insn = DEP_CON (dep);
1229 gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
1231 if ((current_sched_info->flags & DO_SPECULATION)
1232 && !sched_insn_is_legitimate_for_speculation_p (insn, DEP_STATUS (dep)))
1233 DEP_STATUS (dep) &= ~SPECULATIVE;
1235 copy_dep (DEP_NODE_DEP (n), dep);
1237 get_back_and_forw_lists (dep, resolved_p, &con_back_deps, &pro_forw_deps);
1239 add_to_deps_list (DEP_NODE_BACK (n), con_back_deps);
1241 #ifdef ENABLE_CHECKING
1242 check_dep (dep, false);
1243 #endif
1245 add_to_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1247 /* If we are adding a dependency to INSN's LOG_LINKs, then note that
1248 in the bitmap caches of dependency information. */
1249 if (true_dependency_cache != NULL)
1250 set_dependency_caches (dep);
1253 /* Add or update backward dependence between INSN and ELEM
1254 with given type DEP_TYPE and dep_status DS.
1255 This function is a convenience wrapper. */
1256 enum DEPS_ADJUST_RESULT
1257 sd_add_or_update_dep (dep_t dep, bool resolved_p)
1259 return add_or_update_dep_1 (dep, resolved_p, NULL_RTX, NULL_RTX);
1262 /* Resolved dependence pointed to by SD_IT.
1263 SD_IT will advance to the next element. */
1264 void
1265 sd_resolve_dep (sd_iterator_def sd_it)
1267 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1268 dep_t dep = DEP_NODE_DEP (node);
1269 rtx pro = DEP_PRO (dep);
1270 rtx con = DEP_CON (dep);
1272 if ((current_sched_info->flags & DO_SPECULATION)
1273 && (DEP_STATUS (dep) & SPECULATIVE))
1274 move_dep_link (DEP_NODE_BACK (node), INSN_SPEC_BACK_DEPS (con),
1275 INSN_RESOLVED_BACK_DEPS (con));
1276 else
1277 move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
1278 INSN_RESOLVED_BACK_DEPS (con));
1280 move_dep_link (DEP_NODE_FORW (node), INSN_FORW_DEPS (pro),
1281 INSN_RESOLVED_FORW_DEPS (pro));
1284 /* Make TO depend on all the FROM's producers.
1285 If RESOLVED_P is true add dependencies to the resolved lists. */
1286 void
1287 sd_copy_back_deps (rtx to, rtx from, bool resolved_p)
1289 sd_list_types_def list_type;
1290 sd_iterator_def sd_it;
1291 dep_t dep;
1293 list_type = resolved_p ? SD_LIST_RES_BACK : SD_LIST_BACK;
1295 FOR_EACH_DEP (from, list_type, sd_it, dep)
1297 dep_def _new_dep, *new_dep = &_new_dep;
1299 copy_dep (new_dep, dep);
1300 DEP_CON (new_dep) = to;
1301 sd_add_dep (new_dep, resolved_p);
1305 /* Remove a dependency referred to by SD_IT.
1306 SD_IT will point to the next dependence after removal. */
1307 void
1308 sd_delete_dep (sd_iterator_def sd_it)
1310 dep_node_t n = DEP_LINK_NODE (*sd_it.linkp);
1311 dep_t dep = DEP_NODE_DEP (n);
1312 rtx pro = DEP_PRO (dep);
1313 rtx con = DEP_CON (dep);
1314 deps_list_t con_back_deps;
1315 deps_list_t pro_forw_deps;
1317 if (true_dependency_cache != NULL)
1319 int elem_luid = INSN_LUID (pro);
1320 int insn_luid = INSN_LUID (con);
1322 bitmap_clear_bit (&true_dependency_cache[insn_luid], elem_luid);
1323 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1324 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1326 if (current_sched_info->flags & DO_SPECULATION)
1327 bitmap_clear_bit (&spec_dependency_cache[insn_luid], elem_luid);
1330 get_back_and_forw_lists (dep, sd_it.resolved_p,
1331 &con_back_deps, &pro_forw_deps);
1333 remove_from_deps_list (DEP_NODE_BACK (n), con_back_deps);
1334 remove_from_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1336 delete_dep_node (n);
1339 /* Dump size of the lists. */
1340 #define DUMP_LISTS_SIZE (2)
1342 /* Dump dependencies of the lists. */
1343 #define DUMP_LISTS_DEPS (4)
1345 /* Dump all information about the lists. */
1346 #define DUMP_LISTS_ALL (DUMP_LISTS_SIZE | DUMP_LISTS_DEPS)
1348 /* Dump deps_lists of INSN specified by TYPES to DUMP.
1349 FLAGS is a bit mask specifying what information about the lists needs
1350 to be printed.
1351 If FLAGS has the very first bit set, then dump all information about
1352 the lists and propagate this bit into the callee dump functions. */
1353 static void
1354 dump_lists (FILE *dump, rtx insn, sd_list_types_def types, int flags)
1356 sd_iterator_def sd_it;
1357 dep_t dep;
1358 int all;
1360 all = (flags & 1);
1362 if (all)
1363 flags |= DUMP_LISTS_ALL;
1365 fprintf (dump, "[");
1367 if (flags & DUMP_LISTS_SIZE)
1368 fprintf (dump, "%d; ", sd_lists_size (insn, types));
1370 if (flags & DUMP_LISTS_DEPS)
1372 FOR_EACH_DEP (insn, types, sd_it, dep)
1374 dump_dep (dump, dep, dump_dep_flags | all);
1375 fprintf (dump, " ");
1380 /* Dump all information about deps_lists of INSN specified by TYPES
1381 to STDERR. */
1382 void
1383 sd_debug_lists (rtx insn, sd_list_types_def types)
1385 dump_lists (stderr, insn, types, 1);
1386 fprintf (stderr, "\n");
1389 /* A convenience wrapper to operate on an entire list. */
1391 static void
1392 add_dependence_list (rtx insn, rtx list, int uncond, enum reg_note dep_type)
1394 for (; list; list = XEXP (list, 1))
1396 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1397 add_dependence (insn, XEXP (list, 0), dep_type);
1401 /* Similar, but free *LISTP at the same time, when the context
1402 is not readonly. */
1404 static void
1405 add_dependence_list_and_free (struct deps *deps, rtx insn, rtx *listp,
1406 int uncond, enum reg_note dep_type)
1408 rtx list, next;
1410 if (deps->readonly)
1412 add_dependence_list (insn, *listp, uncond, dep_type);
1413 return;
1416 for (list = *listp, *listp = NULL; list ; list = next)
1418 next = XEXP (list, 1);
1419 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1420 add_dependence (insn, XEXP (list, 0), dep_type);
1421 free_INSN_LIST_node (list);
1425 /* Remove all occurences of INSN from LIST. Return the number of
1426 occurences removed. */
1428 static int
1429 remove_from_dependence_list (rtx insn, rtx* listp)
1431 int removed = 0;
1433 while (*listp)
1435 if (XEXP (*listp, 0) == insn)
1437 remove_free_INSN_LIST_node (listp);
1438 removed++;
1439 continue;
1442 listp = &XEXP (*listp, 1);
1445 return removed;
1448 /* Same as above, but process two lists at once. */
1449 static int
1450 remove_from_both_dependence_lists (rtx insn, rtx *listp, rtx *exprp)
1452 int removed = 0;
1454 while (*listp)
1456 if (XEXP (*listp, 0) == insn)
1458 remove_free_INSN_LIST_node (listp);
1459 remove_free_EXPR_LIST_node (exprp);
1460 removed++;
1461 continue;
1464 listp = &XEXP (*listp, 1);
1465 exprp = &XEXP (*exprp, 1);
1468 return removed;
1471 /* Clear all dependencies for an insn. */
1472 static void
1473 delete_all_dependences (rtx insn)
1475 sd_iterator_def sd_it;
1476 dep_t dep;
1478 /* The below cycle can be optimized to clear the caches and back_deps
1479 in one call but that would provoke duplication of code from
1480 delete_dep (). */
1482 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1483 sd_iterator_cond (&sd_it, &dep);)
1484 sd_delete_dep (sd_it);
1487 /* All insns in a scheduling group except the first should only have
1488 dependencies on the previous insn in the group. So we find the
1489 first instruction in the scheduling group by walking the dependence
1490 chains backwards. Then we add the dependencies for the group to
1491 the previous nonnote insn. */
1493 static void
1494 fixup_sched_groups (rtx insn)
1496 sd_iterator_def sd_it;
1497 dep_t dep;
1498 rtx prev_nonnote;
1500 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
1502 rtx i = insn;
1503 rtx pro = DEP_PRO (dep);
1507 i = prev_nonnote_insn (i);
1509 if (pro == i)
1510 goto next_link;
1511 } while (SCHED_GROUP_P (i) || DEBUG_INSN_P (i));
1513 if (! sched_insns_conditions_mutex_p (i, pro))
1514 add_dependence (i, pro, DEP_TYPE (dep));
1515 next_link:;
1518 delete_all_dependences (insn);
1520 prev_nonnote = prev_nonnote_insn (insn);
1521 while (DEBUG_INSN_P (prev_nonnote))
1522 prev_nonnote = prev_nonnote_insn (prev_nonnote);
1523 if (BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (prev_nonnote)
1524 && ! sched_insns_conditions_mutex_p (insn, prev_nonnote))
1525 add_dependence (insn, prev_nonnote, REG_DEP_ANTI);
1528 /* Process an insn's memory dependencies. There are four kinds of
1529 dependencies:
1531 (0) read dependence: read follows read
1532 (1) true dependence: read follows write
1533 (2) output dependence: write follows write
1534 (3) anti dependence: write follows read
1536 We are careful to build only dependencies which actually exist, and
1537 use transitivity to avoid building too many links. */
1539 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1540 The MEM is a memory reference contained within INSN, which we are saving
1541 so that we can do memory aliasing on it. */
1543 static void
1544 add_insn_mem_dependence (struct deps *deps, bool read_p,
1545 rtx insn, rtx mem)
1547 rtx *insn_list;
1548 rtx *mem_list;
1549 rtx link;
1551 gcc_assert (!deps->readonly);
1552 if (read_p)
1554 insn_list = &deps->pending_read_insns;
1555 mem_list = &deps->pending_read_mems;
1556 if (!DEBUG_INSN_P (insn))
1557 deps->pending_read_list_length++;
1559 else
1561 insn_list = &deps->pending_write_insns;
1562 mem_list = &deps->pending_write_mems;
1563 deps->pending_write_list_length++;
1566 link = alloc_INSN_LIST (insn, *insn_list);
1567 *insn_list = link;
1569 if (sched_deps_info->use_cselib)
1571 mem = shallow_copy_rtx (mem);
1572 XEXP (mem, 0) = cselib_subst_to_values (XEXP (mem, 0));
1574 link = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
1575 *mem_list = link;
1578 /* Make a dependency between every memory reference on the pending lists
1579 and INSN, thus flushing the pending lists. FOR_READ is true if emitting
1580 dependencies for a read operation, similarly with FOR_WRITE. */
1582 static void
1583 flush_pending_lists (struct deps *deps, rtx insn, int for_read,
1584 int for_write)
1586 if (for_write)
1588 add_dependence_list_and_free (deps, insn, &deps->pending_read_insns,
1589 1, REG_DEP_ANTI);
1590 if (!deps->readonly)
1592 free_EXPR_LIST_list (&deps->pending_read_mems);
1593 deps->pending_read_list_length = 0;
1597 add_dependence_list_and_free (deps, insn, &deps->pending_write_insns, 1,
1598 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1600 add_dependence_list_and_free (deps, insn,
1601 &deps->last_pending_memory_flush, 1,
1602 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1603 if (!deps->readonly)
1605 free_EXPR_LIST_list (&deps->pending_write_mems);
1606 deps->pending_write_list_length = 0;
1608 deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
1609 deps->pending_flush_length = 1;
1613 /* Instruction which dependencies we are analyzing. */
1614 static rtx cur_insn = NULL_RTX;
1616 /* Implement hooks for haifa scheduler. */
1618 static void
1619 haifa_start_insn (rtx insn)
1621 gcc_assert (insn && !cur_insn);
1623 cur_insn = insn;
1626 static void
1627 haifa_finish_insn (void)
1629 cur_insn = NULL;
1632 void
1633 haifa_note_reg_set (int regno)
1635 SET_REGNO_REG_SET (reg_pending_sets, regno);
1638 void
1639 haifa_note_reg_clobber (int regno)
1641 SET_REGNO_REG_SET (reg_pending_clobbers, regno);
1644 void
1645 haifa_note_reg_use (int regno)
1647 SET_REGNO_REG_SET (reg_pending_uses, regno);
1650 static void
1651 haifa_note_mem_dep (rtx mem, rtx pending_mem, rtx pending_insn, ds_t ds)
1653 if (!(ds & SPECULATIVE))
1655 mem = NULL_RTX;
1656 pending_mem = NULL_RTX;
1658 else
1659 gcc_assert (ds & BEGIN_DATA);
1662 dep_def _dep, *dep = &_dep;
1664 init_dep_1 (dep, pending_insn, cur_insn, ds_to_dt (ds),
1665 current_sched_info->flags & USE_DEPS_LIST ? ds : -1);
1666 maybe_add_or_update_dep_1 (dep, false, pending_mem, mem);
1671 static void
1672 haifa_note_dep (rtx elem, ds_t ds)
1674 dep_def _dep;
1675 dep_t dep = &_dep;
1677 init_dep (dep, elem, cur_insn, ds_to_dt (ds));
1678 maybe_add_or_update_dep_1 (dep, false, NULL_RTX, NULL_RTX);
1681 static void
1682 note_reg_use (int r)
1684 if (sched_deps_info->note_reg_use)
1685 sched_deps_info->note_reg_use (r);
1688 static void
1689 note_reg_set (int r)
1691 if (sched_deps_info->note_reg_set)
1692 sched_deps_info->note_reg_set (r);
1695 static void
1696 note_reg_clobber (int r)
1698 if (sched_deps_info->note_reg_clobber)
1699 sched_deps_info->note_reg_clobber (r);
1702 static void
1703 note_mem_dep (rtx m1, rtx m2, rtx e, ds_t ds)
1705 if (sched_deps_info->note_mem_dep)
1706 sched_deps_info->note_mem_dep (m1, m2, e, ds);
1709 static void
1710 note_dep (rtx e, ds_t ds)
1712 if (sched_deps_info->note_dep)
1713 sched_deps_info->note_dep (e, ds);
1716 /* Return corresponding to DS reg_note. */
1717 enum reg_note
1718 ds_to_dt (ds_t ds)
1720 if (ds & DEP_TRUE)
1721 return REG_DEP_TRUE;
1722 else if (ds & DEP_OUTPUT)
1723 return REG_DEP_OUTPUT;
1724 else
1726 gcc_assert (ds & DEP_ANTI);
1727 return REG_DEP_ANTI;
1733 /* Functions for computation of info needed for register pressure
1734 sensitive insn scheduling. */
1737 /* Allocate and return reg_use_data structure for REGNO and INSN. */
1738 static struct reg_use_data *
1739 create_insn_reg_use (int regno, rtx insn)
1741 struct reg_use_data *use;
1743 use = (struct reg_use_data *) xmalloc (sizeof (struct reg_use_data));
1744 use->regno = regno;
1745 use->insn = insn;
1746 use->next_insn_use = INSN_REG_USE_LIST (insn);
1747 INSN_REG_USE_LIST (insn) = use;
1748 return use;
1751 /* Allocate and return reg_set_data structure for REGNO and INSN. */
1752 static struct reg_set_data *
1753 create_insn_reg_set (int regno, rtx insn)
1755 struct reg_set_data *set;
1757 set = (struct reg_set_data *) xmalloc (sizeof (struct reg_set_data));
1758 set->regno = regno;
1759 set->insn = insn;
1760 set->next_insn_set = INSN_REG_SET_LIST (insn);
1761 INSN_REG_SET_LIST (insn) = set;
1762 return set;
1765 /* Set up insn register uses for INSN and dependency context DEPS. */
1766 static void
1767 setup_insn_reg_uses (struct deps *deps, rtx insn)
1769 unsigned i;
1770 reg_set_iterator rsi;
1771 rtx list;
1772 struct reg_use_data *use, *use2, *next;
1773 struct deps_reg *reg_last;
1775 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
1777 if (i < FIRST_PSEUDO_REGISTER
1778 && TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
1779 continue;
1781 if (find_regno_note (insn, REG_DEAD, i) == NULL_RTX
1782 && ! REGNO_REG_SET_P (reg_pending_sets, i)
1783 && ! REGNO_REG_SET_P (reg_pending_clobbers, i))
1784 /* Ignore use which is not dying. */
1785 continue;
1787 use = create_insn_reg_use (i, insn);
1788 use->next_regno_use = use;
1789 reg_last = &deps->reg_last[i];
1791 /* Create the cycle list of uses. */
1792 for (list = reg_last->uses; list; list = XEXP (list, 1))
1794 use2 = create_insn_reg_use (i, XEXP (list, 0));
1795 next = use->next_regno_use;
1796 use->next_regno_use = use2;
1797 use2->next_regno_use = next;
1802 /* Register pressure info for the currently processed insn. */
1803 static struct reg_pressure_data reg_pressure_info[N_REG_CLASSES];
1805 /* Return TRUE if INSN has the use structure for REGNO. */
1806 static bool
1807 insn_use_p (rtx insn, int regno)
1809 struct reg_use_data *use;
1811 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1812 if (use->regno == regno)
1813 return true;
1814 return false;
1817 /* Update the register pressure info after birth of pseudo register REGNO
1818 in INSN. Arguments CLOBBER_P and UNUSED_P say correspondingly that
1819 the register is in clobber or unused after the insn. */
1820 static void
1821 mark_insn_pseudo_birth (rtx insn, int regno, bool clobber_p, bool unused_p)
1823 int incr, new_incr;
1824 enum reg_class cl;
1826 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
1827 cl = sched_regno_cover_class[regno];
1828 if (cl != NO_REGS)
1830 incr = ira_reg_class_nregs[cl][PSEUDO_REGNO_MODE (regno)];
1831 if (clobber_p)
1833 new_incr = reg_pressure_info[cl].clobber_increase + incr;
1834 reg_pressure_info[cl].clobber_increase = new_incr;
1836 else if (unused_p)
1838 new_incr = reg_pressure_info[cl].unused_set_increase + incr;
1839 reg_pressure_info[cl].unused_set_increase = new_incr;
1841 else
1843 new_incr = reg_pressure_info[cl].set_increase + incr;
1844 reg_pressure_info[cl].set_increase = new_incr;
1845 if (! insn_use_p (insn, regno))
1846 reg_pressure_info[cl].change += incr;
1847 create_insn_reg_set (regno, insn);
1849 gcc_assert (new_incr < (1 << INCREASE_BITS));
1853 /* Like mark_insn_pseudo_regno_birth except that NREGS saying how many
1854 hard registers involved in the birth. */
1855 static void
1856 mark_insn_hard_regno_birth (rtx insn, int regno, int nregs,
1857 bool clobber_p, bool unused_p)
1859 enum reg_class cl;
1860 int new_incr, last = regno + nregs;
1862 while (regno < last)
1864 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
1865 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
1867 cl = sched_regno_cover_class[regno];
1868 if (cl != NO_REGS)
1870 if (clobber_p)
1872 new_incr = reg_pressure_info[cl].clobber_increase + 1;
1873 reg_pressure_info[cl].clobber_increase = new_incr;
1875 else if (unused_p)
1877 new_incr = reg_pressure_info[cl].unused_set_increase + 1;
1878 reg_pressure_info[cl].unused_set_increase = new_incr;
1880 else
1882 new_incr = reg_pressure_info[cl].set_increase + 1;
1883 reg_pressure_info[cl].set_increase = new_incr;
1884 if (! insn_use_p (insn, regno))
1885 reg_pressure_info[cl].change += 1;
1886 create_insn_reg_set (regno, insn);
1888 gcc_assert (new_incr < (1 << INCREASE_BITS));
1891 regno++;
1895 /* Update the register pressure info after birth of pseudo or hard
1896 register REG in INSN. Arguments CLOBBER_P and UNUSED_P say
1897 correspondingly that the register is in clobber or unused after the
1898 insn. */
1899 static void
1900 mark_insn_reg_birth (rtx insn, rtx reg, bool clobber_p, bool unused_p)
1902 int regno;
1904 if (GET_CODE (reg) == SUBREG)
1905 reg = SUBREG_REG (reg);
1907 if (! REG_P (reg))
1908 return;
1910 regno = REGNO (reg);
1911 if (regno < FIRST_PSEUDO_REGISTER)
1912 mark_insn_hard_regno_birth (insn, regno,
1913 hard_regno_nregs[regno][GET_MODE (reg)],
1914 clobber_p, unused_p);
1915 else
1916 mark_insn_pseudo_birth (insn, regno, clobber_p, unused_p);
1919 /* Update the register pressure info after death of pseudo register
1920 REGNO. */
1921 static void
1922 mark_pseudo_death (int regno)
1924 int incr;
1925 enum reg_class cl;
1927 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
1928 cl = sched_regno_cover_class[regno];
1929 if (cl != NO_REGS)
1931 incr = ira_reg_class_nregs[cl][PSEUDO_REGNO_MODE (regno)];
1932 reg_pressure_info[cl].change -= incr;
1936 /* Like mark_pseudo_death except that NREGS saying how many hard
1937 registers involved in the death. */
1938 static void
1939 mark_hard_regno_death (int regno, int nregs)
1941 enum reg_class cl;
1942 int last = regno + nregs;
1944 while (regno < last)
1946 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
1947 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
1949 cl = sched_regno_cover_class[regno];
1950 if (cl != NO_REGS)
1951 reg_pressure_info[cl].change -= 1;
1953 regno++;
1957 /* Update the register pressure info after death of pseudo or hard
1958 register REG. */
1959 static void
1960 mark_reg_death (rtx reg)
1962 int regno;
1964 if (GET_CODE (reg) == SUBREG)
1965 reg = SUBREG_REG (reg);
1967 if (! REG_P (reg))
1968 return;
1970 regno = REGNO (reg);
1971 if (regno < FIRST_PSEUDO_REGISTER)
1972 mark_hard_regno_death (regno, hard_regno_nregs[regno][GET_MODE (reg)]);
1973 else
1974 mark_pseudo_death (regno);
1977 /* Process SETTER of REG. DATA is an insn containing the setter. */
1978 static void
1979 mark_insn_reg_store (rtx reg, const_rtx setter, void *data)
1981 if (setter != NULL_RTX && GET_CODE (setter) != SET)
1982 return;
1983 mark_insn_reg_birth
1984 ((rtx) data, reg, false,
1985 find_reg_note ((const_rtx) data, REG_UNUSED, reg) != NULL_RTX);
1988 /* Like mark_insn_reg_store except notice just CLOBBERs; ignore SETs. */
1989 static void
1990 mark_insn_reg_clobber (rtx reg, const_rtx setter, void *data)
1992 if (GET_CODE (setter) == CLOBBER)
1993 mark_insn_reg_birth ((rtx) data, reg, true, false);
1996 /* Set up reg pressure info related to INSN. */
1997 static void
1998 setup_insn_reg_pressure_info (rtx insn)
2000 int i, len;
2001 enum reg_class cl;
2002 static struct reg_pressure_data *pressure_info;
2003 rtx link;
2005 gcc_assert (sched_pressure_p);
2007 if (! INSN_P (insn))
2008 return;
2010 for (i = 0; i < ira_reg_class_cover_size; i++)
2012 cl = ira_reg_class_cover[i];
2013 reg_pressure_info[cl].clobber_increase = 0;
2014 reg_pressure_info[cl].set_increase = 0;
2015 reg_pressure_info[cl].unused_set_increase = 0;
2016 reg_pressure_info[cl].change = 0;
2019 note_stores (PATTERN (insn), mark_insn_reg_clobber, insn);
2021 note_stores (PATTERN (insn), mark_insn_reg_store, insn);
2023 #ifdef AUTO_INC_DEC
2024 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2025 if (REG_NOTE_KIND (link) == REG_INC)
2026 mark_insn_reg_store (XEXP (link, 0), NULL_RTX, insn);
2027 #endif
2029 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2030 if (REG_NOTE_KIND (link) == REG_DEAD)
2031 mark_reg_death (XEXP (link, 0));
2033 len = sizeof (struct reg_pressure_data) * ira_reg_class_cover_size;
2034 pressure_info
2035 = INSN_REG_PRESSURE (insn) = (struct reg_pressure_data *) xmalloc (len);
2036 INSN_MAX_REG_PRESSURE (insn) = (int *) xmalloc (ira_reg_class_cover_size
2037 * sizeof (int));
2038 for (i = 0; i < ira_reg_class_cover_size; i++)
2040 cl = ira_reg_class_cover[i];
2041 pressure_info[i].clobber_increase
2042 = reg_pressure_info[cl].clobber_increase;
2043 pressure_info[i].set_increase = reg_pressure_info[cl].set_increase;
2044 pressure_info[i].unused_set_increase
2045 = reg_pressure_info[cl].unused_set_increase;
2046 pressure_info[i].change = reg_pressure_info[cl].change;
2053 /* Internal variable for sched_analyze_[12] () functions.
2054 If it is nonzero, this means that sched_analyze_[12] looks
2055 at the most toplevel SET. */
2056 static bool can_start_lhs_rhs_p;
2058 /* Extend reg info for the deps context DEPS given that
2059 we have just generated a register numbered REGNO. */
2060 static void
2061 extend_deps_reg_info (struct deps *deps, int regno)
2063 int max_regno = regno + 1;
2065 gcc_assert (!reload_completed);
2067 /* In a readonly context, it would not hurt to extend info,
2068 but it should not be needed. */
2069 if (reload_completed && deps->readonly)
2071 deps->max_reg = max_regno;
2072 return;
2075 if (max_regno > deps->max_reg)
2077 deps->reg_last = XRESIZEVEC (struct deps_reg, deps->reg_last,
2078 max_regno);
2079 memset (&deps->reg_last[deps->max_reg],
2080 0, (max_regno - deps->max_reg)
2081 * sizeof (struct deps_reg));
2082 deps->max_reg = max_regno;
2086 /* Extends REG_INFO_P if needed. */
2087 void
2088 maybe_extend_reg_info_p (void)
2090 /* Extend REG_INFO_P, if needed. */
2091 if ((unsigned int)max_regno - 1 >= reg_info_p_size)
2093 size_t new_reg_info_p_size = max_regno + 128;
2095 gcc_assert (!reload_completed && sel_sched_p ());
2097 reg_info_p = (struct reg_info_t *) xrecalloc (reg_info_p,
2098 new_reg_info_p_size,
2099 reg_info_p_size,
2100 sizeof (*reg_info_p));
2101 reg_info_p_size = new_reg_info_p_size;
2105 /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
2106 The type of the reference is specified by REF and can be SET,
2107 CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE. */
2109 static void
2110 sched_analyze_reg (struct deps *deps, int regno, enum machine_mode mode,
2111 enum rtx_code ref, rtx insn)
2113 /* We could emit new pseudos in renaming. Extend the reg structures. */
2114 if (!reload_completed && sel_sched_p ()
2115 && (regno >= max_reg_num () - 1 || regno >= deps->max_reg))
2116 extend_deps_reg_info (deps, regno);
2118 maybe_extend_reg_info_p ();
2120 /* A hard reg in a wide mode may really be multiple registers.
2121 If so, mark all of them just like the first. */
2122 if (regno < FIRST_PSEUDO_REGISTER)
2124 int i = hard_regno_nregs[regno][mode];
2125 if (ref == SET)
2127 while (--i >= 0)
2128 note_reg_set (regno + i);
2130 else if (ref == USE)
2132 while (--i >= 0)
2133 note_reg_use (regno + i);
2135 else
2137 while (--i >= 0)
2138 note_reg_clobber (regno + i);
2142 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
2143 it does not reload. Ignore these as they have served their
2144 purpose already. */
2145 else if (regno >= deps->max_reg)
2147 enum rtx_code code = GET_CODE (PATTERN (insn));
2148 gcc_assert (code == USE || code == CLOBBER);
2151 else
2153 if (ref == SET)
2154 note_reg_set (regno);
2155 else if (ref == USE)
2156 note_reg_use (regno);
2157 else
2158 note_reg_clobber (regno);
2160 /* Pseudos that are REG_EQUIV to something may be replaced
2161 by that during reloading. We need only add dependencies for
2162 the address in the REG_EQUIV note. */
2163 if (!reload_completed && get_reg_known_equiv_p (regno))
2165 rtx t = get_reg_known_value (regno);
2166 if (MEM_P (t))
2167 sched_analyze_2 (deps, XEXP (t, 0), insn);
2170 /* Don't let it cross a call after scheduling if it doesn't
2171 already cross one. */
2172 if (REG_N_CALLS_CROSSED (regno) == 0)
2174 if (!deps->readonly && ref == USE && !DEBUG_INSN_P (insn))
2175 deps->sched_before_next_call
2176 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
2177 else
2178 add_dependence_list (insn, deps->last_function_call, 1,
2179 REG_DEP_ANTI);
2184 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
2185 rtx, X, creating all dependencies generated by the write to the
2186 destination of X, and reads of everything mentioned. */
2188 static void
2189 sched_analyze_1 (struct deps *deps, rtx x, rtx insn)
2191 rtx dest = XEXP (x, 0);
2192 enum rtx_code code = GET_CODE (x);
2193 bool cslr_p = can_start_lhs_rhs_p;
2195 can_start_lhs_rhs_p = false;
2197 gcc_assert (dest);
2198 if (dest == 0)
2199 return;
2201 if (cslr_p && sched_deps_info->start_lhs)
2202 sched_deps_info->start_lhs (dest);
2204 if (GET_CODE (dest) == PARALLEL)
2206 int i;
2208 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
2209 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
2210 sched_analyze_1 (deps,
2211 gen_rtx_CLOBBER (VOIDmode,
2212 XEXP (XVECEXP (dest, 0, i), 0)),
2213 insn);
2215 if (cslr_p && sched_deps_info->finish_lhs)
2216 sched_deps_info->finish_lhs ();
2218 if (code == SET)
2220 can_start_lhs_rhs_p = cslr_p;
2222 sched_analyze_2 (deps, SET_SRC (x), insn);
2224 can_start_lhs_rhs_p = false;
2227 return;
2230 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
2231 || GET_CODE (dest) == ZERO_EXTRACT)
2233 if (GET_CODE (dest) == STRICT_LOW_PART
2234 || GET_CODE (dest) == ZERO_EXTRACT
2235 || df_read_modify_subreg_p (dest))
2237 /* These both read and modify the result. We must handle
2238 them as writes to get proper dependencies for following
2239 instructions. We must handle them as reads to get proper
2240 dependencies from this to previous instructions.
2241 Thus we need to call sched_analyze_2. */
2243 sched_analyze_2 (deps, XEXP (dest, 0), insn);
2245 if (GET_CODE (dest) == ZERO_EXTRACT)
2247 /* The second and third arguments are values read by this insn. */
2248 sched_analyze_2 (deps, XEXP (dest, 1), insn);
2249 sched_analyze_2 (deps, XEXP (dest, 2), insn);
2251 dest = XEXP (dest, 0);
2254 if (REG_P (dest))
2256 int regno = REGNO (dest);
2257 enum machine_mode mode = GET_MODE (dest);
2259 sched_analyze_reg (deps, regno, mode, code, insn);
2261 #ifdef STACK_REGS
2262 /* Treat all writes to a stack register as modifying the TOS. */
2263 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
2265 int nregs;
2267 /* Avoid analyzing the same register twice. */
2268 if (regno != FIRST_STACK_REG)
2269 sched_analyze_reg (deps, FIRST_STACK_REG, mode, code, insn);
2271 nregs = hard_regno_nregs[FIRST_STACK_REG][mode];
2272 while (--nregs >= 0)
2273 SET_HARD_REG_BIT (implicit_reg_pending_uses,
2274 FIRST_STACK_REG + nregs);
2276 #endif
2278 else if (MEM_P (dest))
2280 /* Writing memory. */
2281 rtx t = dest;
2283 if (sched_deps_info->use_cselib)
2285 enum machine_mode address_mode
2286 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (dest));
2288 t = shallow_copy_rtx (dest);
2289 cselib_lookup (XEXP (t, 0), address_mode, 1);
2290 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
2292 t = canon_rtx (t);
2294 /* Pending lists can't get larger with a readonly context. */
2295 if (!deps->readonly
2296 && ((deps->pending_read_list_length + deps->pending_write_list_length)
2297 > MAX_PENDING_LIST_LENGTH))
2299 /* Flush all pending reads and writes to prevent the pending lists
2300 from getting any larger. Insn scheduling runs too slowly when
2301 these lists get long. When compiling GCC with itself,
2302 this flush occurs 8 times for sparc, and 10 times for m88k using
2303 the default value of 32. */
2304 flush_pending_lists (deps, insn, false, true);
2306 else
2308 rtx pending, pending_mem;
2310 pending = deps->pending_read_insns;
2311 pending_mem = deps->pending_read_mems;
2312 while (pending)
2314 if (anti_dependence (XEXP (pending_mem, 0), t)
2315 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2316 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2317 DEP_ANTI);
2319 pending = XEXP (pending, 1);
2320 pending_mem = XEXP (pending_mem, 1);
2323 pending = deps->pending_write_insns;
2324 pending_mem = deps->pending_write_mems;
2325 while (pending)
2327 if (output_dependence (XEXP (pending_mem, 0), t)
2328 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2329 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2330 DEP_OUTPUT);
2332 pending = XEXP (pending, 1);
2333 pending_mem = XEXP (pending_mem, 1);
2336 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
2337 REG_DEP_ANTI);
2339 if (!deps->readonly)
2340 add_insn_mem_dependence (deps, false, insn, dest);
2342 sched_analyze_2 (deps, XEXP (dest, 0), insn);
2345 if (cslr_p && sched_deps_info->finish_lhs)
2346 sched_deps_info->finish_lhs ();
2348 /* Analyze reads. */
2349 if (GET_CODE (x) == SET)
2351 can_start_lhs_rhs_p = cslr_p;
2353 sched_analyze_2 (deps, SET_SRC (x), insn);
2355 can_start_lhs_rhs_p = false;
2359 /* Analyze the uses of memory and registers in rtx X in INSN. */
2360 static void
2361 sched_analyze_2 (struct deps *deps, rtx x, rtx insn)
2363 int i;
2364 int j;
2365 enum rtx_code code;
2366 const char *fmt;
2367 bool cslr_p = can_start_lhs_rhs_p;
2369 can_start_lhs_rhs_p = false;
2371 gcc_assert (x);
2372 if (x == 0)
2373 return;
2375 if (cslr_p && sched_deps_info->start_rhs)
2376 sched_deps_info->start_rhs (x);
2378 code = GET_CODE (x);
2380 switch (code)
2382 case CONST_INT:
2383 case CONST_DOUBLE:
2384 case CONST_FIXED:
2385 case CONST_VECTOR:
2386 case SYMBOL_REF:
2387 case CONST:
2388 case LABEL_REF:
2389 /* Ignore constants. */
2390 if (cslr_p && sched_deps_info->finish_rhs)
2391 sched_deps_info->finish_rhs ();
2393 return;
2395 #ifdef HAVE_cc0
2396 case CC0:
2397 /* User of CC0 depends on immediately preceding insn. */
2398 SCHED_GROUP_P (insn) = 1;
2399 /* Don't move CC0 setter to another block (it can set up the
2400 same flag for previous CC0 users which is safe). */
2401 CANT_MOVE (prev_nonnote_insn (insn)) = 1;
2403 if (cslr_p && sched_deps_info->finish_rhs)
2404 sched_deps_info->finish_rhs ();
2406 return;
2407 #endif
2409 case REG:
2411 int regno = REGNO (x);
2412 enum machine_mode mode = GET_MODE (x);
2414 sched_analyze_reg (deps, regno, mode, USE, insn);
2416 #ifdef STACK_REGS
2417 /* Treat all reads of a stack register as modifying the TOS. */
2418 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
2420 /* Avoid analyzing the same register twice. */
2421 if (regno != FIRST_STACK_REG)
2422 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
2423 sched_analyze_reg (deps, FIRST_STACK_REG, mode, SET, insn);
2425 #endif
2427 if (cslr_p && sched_deps_info->finish_rhs)
2428 sched_deps_info->finish_rhs ();
2430 return;
2433 case MEM:
2435 /* Reading memory. */
2436 rtx u;
2437 rtx pending, pending_mem;
2438 rtx t = x;
2440 if (sched_deps_info->use_cselib)
2442 enum machine_mode address_mode
2443 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (t));
2445 t = shallow_copy_rtx (t);
2446 cselib_lookup (XEXP (t, 0), address_mode, 1);
2447 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
2450 if (!DEBUG_INSN_P (insn))
2452 t = canon_rtx (t);
2453 pending = deps->pending_read_insns;
2454 pending_mem = deps->pending_read_mems;
2455 while (pending)
2457 if (read_dependence (XEXP (pending_mem, 0), t)
2458 && ! sched_insns_conditions_mutex_p (insn,
2459 XEXP (pending, 0)))
2460 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2461 DEP_ANTI);
2463 pending = XEXP (pending, 1);
2464 pending_mem = XEXP (pending_mem, 1);
2467 pending = deps->pending_write_insns;
2468 pending_mem = deps->pending_write_mems;
2469 while (pending)
2471 if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
2472 t, rtx_varies_p)
2473 && ! sched_insns_conditions_mutex_p (insn,
2474 XEXP (pending, 0)))
2475 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2476 sched_deps_info->generate_spec_deps
2477 ? BEGIN_DATA | DEP_TRUE : DEP_TRUE);
2479 pending = XEXP (pending, 1);
2480 pending_mem = XEXP (pending_mem, 1);
2483 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
2485 if (! JUMP_P (XEXP (u, 0)))
2486 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
2487 else if (deps_may_trap_p (x))
2489 if ((sched_deps_info->generate_spec_deps)
2490 && sel_sched_p () && (spec_info->mask & BEGIN_CONTROL))
2492 ds_t ds = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
2493 MAX_DEP_WEAK);
2495 note_dep (XEXP (u, 0), ds);
2497 else
2498 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
2503 /* Always add these dependencies to pending_reads, since
2504 this insn may be followed by a write. */
2505 if (!deps->readonly)
2506 add_insn_mem_dependence (deps, true, insn, x);
2508 sched_analyze_2 (deps, XEXP (x, 0), insn);
2510 if (cslr_p && sched_deps_info->finish_rhs)
2511 sched_deps_info->finish_rhs ();
2513 return;
2516 /* Force pending stores to memory in case a trap handler needs them. */
2517 case TRAP_IF:
2518 flush_pending_lists (deps, insn, true, false);
2519 break;
2521 case PREFETCH:
2522 if (PREFETCH_SCHEDULE_BARRIER_P (x))
2523 reg_pending_barrier = TRUE_BARRIER;
2524 break;
2526 case UNSPEC_VOLATILE:
2527 flush_pending_lists (deps, insn, true, true);
2528 /* FALLTHRU */
2530 case ASM_OPERANDS:
2531 case ASM_INPUT:
2533 /* Traditional and volatile asm instructions must be considered to use
2534 and clobber all hard registers, all pseudo-registers and all of
2535 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
2537 Consider for instance a volatile asm that changes the fpu rounding
2538 mode. An insn should not be moved across this even if it only uses
2539 pseudo-regs because it might give an incorrectly rounded result. */
2540 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
2541 reg_pending_barrier = TRUE_BARRIER;
2543 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
2544 We can not just fall through here since then we would be confused
2545 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
2546 traditional asms unlike their normal usage. */
2548 if (code == ASM_OPERANDS)
2550 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
2551 sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
2553 if (cslr_p && sched_deps_info->finish_rhs)
2554 sched_deps_info->finish_rhs ();
2556 return;
2558 break;
2561 case PRE_DEC:
2562 case POST_DEC:
2563 case PRE_INC:
2564 case POST_INC:
2565 /* These both read and modify the result. We must handle them as writes
2566 to get proper dependencies for following instructions. We must handle
2567 them as reads to get proper dependencies from this to previous
2568 instructions. Thus we need to pass them to both sched_analyze_1
2569 and sched_analyze_2. We must call sched_analyze_2 first in order
2570 to get the proper antecedent for the read. */
2571 sched_analyze_2 (deps, XEXP (x, 0), insn);
2572 sched_analyze_1 (deps, x, insn);
2574 if (cslr_p && sched_deps_info->finish_rhs)
2575 sched_deps_info->finish_rhs ();
2577 return;
2579 case POST_MODIFY:
2580 case PRE_MODIFY:
2581 /* op0 = op0 + op1 */
2582 sched_analyze_2 (deps, XEXP (x, 0), insn);
2583 sched_analyze_2 (deps, XEXP (x, 1), insn);
2584 sched_analyze_1 (deps, x, insn);
2586 if (cslr_p && sched_deps_info->finish_rhs)
2587 sched_deps_info->finish_rhs ();
2589 return;
2591 default:
2592 break;
2595 /* Other cases: walk the insn. */
2596 fmt = GET_RTX_FORMAT (code);
2597 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2599 if (fmt[i] == 'e')
2600 sched_analyze_2 (deps, XEXP (x, i), insn);
2601 else if (fmt[i] == 'E')
2602 for (j = 0; j < XVECLEN (x, i); j++)
2603 sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
2606 if (cslr_p && sched_deps_info->finish_rhs)
2607 sched_deps_info->finish_rhs ();
2610 /* Analyze an INSN with pattern X to find all dependencies. */
2611 static void
2612 sched_analyze_insn (struct deps *deps, rtx x, rtx insn)
2614 RTX_CODE code = GET_CODE (x);
2615 rtx link;
2616 unsigned i;
2617 reg_set_iterator rsi;
2619 if (! reload_completed)
2621 HARD_REG_SET temp;
2623 extract_insn (insn);
2624 preprocess_constraints ();
2625 ira_implicitly_set_insn_hard_regs (&temp);
2626 IOR_HARD_REG_SET (implicit_reg_pending_clobbers, temp);
2629 can_start_lhs_rhs_p = (NONJUMP_INSN_P (insn)
2630 && code == SET);
2632 if (may_trap_p (x))
2633 /* Avoid moving trapping instructions accross function calls that might
2634 not always return. */
2635 add_dependence_list (insn, deps->last_function_call_may_noreturn,
2636 1, REG_DEP_ANTI);
2638 if (code == COND_EXEC)
2640 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
2642 /* ??? Should be recording conditions so we reduce the number of
2643 false dependencies. */
2644 x = COND_EXEC_CODE (x);
2645 code = GET_CODE (x);
2647 if (code == SET || code == CLOBBER)
2649 sched_analyze_1 (deps, x, insn);
2651 /* Bare clobber insns are used for letting life analysis, reg-stack
2652 and others know that a value is dead. Depend on the last call
2653 instruction so that reg-stack won't get confused. */
2654 if (code == CLOBBER)
2655 add_dependence_list (insn, deps->last_function_call, 1,
2656 REG_DEP_OUTPUT);
2658 else if (code == PARALLEL)
2660 for (i = XVECLEN (x, 0); i--;)
2662 rtx sub = XVECEXP (x, 0, i);
2663 code = GET_CODE (sub);
2665 if (code == COND_EXEC)
2667 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
2668 sub = COND_EXEC_CODE (sub);
2669 code = GET_CODE (sub);
2671 if (code == SET || code == CLOBBER)
2672 sched_analyze_1 (deps, sub, insn);
2673 else
2674 sched_analyze_2 (deps, sub, insn);
2677 else
2678 sched_analyze_2 (deps, x, insn);
2680 /* Mark registers CLOBBERED or used by called function. */
2681 if (CALL_P (insn))
2683 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2685 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
2686 sched_analyze_1 (deps, XEXP (link, 0), insn);
2687 else
2688 sched_analyze_2 (deps, XEXP (link, 0), insn);
2690 if (find_reg_note (insn, REG_SETJMP, NULL))
2691 reg_pending_barrier = MOVE_BARRIER;
2694 if (JUMP_P (insn))
2696 rtx next;
2697 next = next_nonnote_insn (insn);
2698 while (next && DEBUG_INSN_P (next))
2699 next = next_nonnote_insn (next);
2700 if (next && BARRIER_P (next))
2701 reg_pending_barrier = MOVE_BARRIER;
2702 else
2704 rtx pending, pending_mem;
2706 if (sched_deps_info->compute_jump_reg_dependencies)
2708 regset_head tmp_uses, tmp_sets;
2709 INIT_REG_SET (&tmp_uses);
2710 INIT_REG_SET (&tmp_sets);
2712 (*sched_deps_info->compute_jump_reg_dependencies)
2713 (insn, &deps->reg_conditional_sets, &tmp_uses, &tmp_sets);
2714 /* Make latency of jump equal to 0 by using anti-dependence. */
2715 EXECUTE_IF_SET_IN_REG_SET (&tmp_uses, 0, i, rsi)
2717 struct deps_reg *reg_last = &deps->reg_last[i];
2718 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI);
2719 add_dependence_list (insn, reg_last->implicit_sets,
2720 0, REG_DEP_ANTI);
2721 add_dependence_list (insn, reg_last->clobbers, 0,
2722 REG_DEP_ANTI);
2724 if (!deps->readonly)
2726 reg_last->uses_length++;
2727 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2730 IOR_REG_SET (reg_pending_sets, &tmp_sets);
2732 CLEAR_REG_SET (&tmp_uses);
2733 CLEAR_REG_SET (&tmp_sets);
2736 /* All memory writes and volatile reads must happen before the
2737 jump. Non-volatile reads must happen before the jump iff
2738 the result is needed by the above register used mask. */
2740 pending = deps->pending_write_insns;
2741 pending_mem = deps->pending_write_mems;
2742 while (pending)
2744 if (! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2745 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
2746 pending = XEXP (pending, 1);
2747 pending_mem = XEXP (pending_mem, 1);
2750 pending = deps->pending_read_insns;
2751 pending_mem = deps->pending_read_mems;
2752 while (pending)
2754 if (MEM_VOLATILE_P (XEXP (pending_mem, 0))
2755 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2756 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
2757 pending = XEXP (pending, 1);
2758 pending_mem = XEXP (pending_mem, 1);
2761 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
2762 REG_DEP_ANTI);
2766 /* If this instruction can throw an exception, then moving it changes
2767 where block boundaries fall. This is mighty confusing elsewhere.
2768 Therefore, prevent such an instruction from being moved. Same for
2769 non-jump instructions that define block boundaries.
2770 ??? Unclear whether this is still necessary in EBB mode. If not,
2771 add_branch_dependences should be adjusted for RGN mode instead. */
2772 if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
2773 || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
2774 reg_pending_barrier = MOVE_BARRIER;
2776 if (sched_pressure_p)
2778 setup_insn_reg_uses (deps, insn);
2779 setup_insn_reg_pressure_info (insn);
2782 /* Add register dependencies for insn. */
2783 if (DEBUG_INSN_P (insn))
2785 rtx prev = deps->last_debug_insn;
2786 rtx u;
2788 if (!deps->readonly)
2789 deps->last_debug_insn = insn;
2791 if (prev)
2792 add_dependence (insn, prev, REG_DEP_ANTI);
2794 add_dependence_list (insn, deps->last_function_call, 1,
2795 REG_DEP_ANTI);
2797 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
2798 if (! JUMP_P (XEXP (u, 0))
2799 || !sel_sched_p ())
2800 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
2802 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
2804 struct deps_reg *reg_last = &deps->reg_last[i];
2805 add_dependence_list (insn, reg_last->sets, 1, REG_DEP_ANTI);
2806 add_dependence_list (insn, reg_last->clobbers, 1, REG_DEP_ANTI);
2808 if (!deps->readonly)
2809 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2811 CLEAR_REG_SET (reg_pending_uses);
2813 /* Quite often, a debug insn will refer to stuff in the
2814 previous instruction, but the reason we want this
2815 dependency here is to make sure the scheduler doesn't
2816 gratuitously move a debug insn ahead. This could dirty
2817 DF flags and cause additional analysis that wouldn't have
2818 occurred in compilation without debug insns, and such
2819 additional analysis can modify the generated code. */
2820 prev = PREV_INSN (insn);
2822 if (prev && NONDEBUG_INSN_P (prev))
2823 add_dependence (insn, prev, REG_DEP_ANTI);
2825 else
2827 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
2829 struct deps_reg *reg_last = &deps->reg_last[i];
2830 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
2831 add_dependence_list (insn, reg_last->implicit_sets, 0, REG_DEP_ANTI);
2832 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
2834 if (!deps->readonly)
2836 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2837 reg_last->uses_length++;
2841 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2842 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i))
2844 struct deps_reg *reg_last = &deps->reg_last[i];
2845 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
2846 add_dependence_list (insn, reg_last->implicit_sets, 0,
2847 REG_DEP_ANTI);
2848 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
2850 if (!deps->readonly)
2852 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2853 reg_last->uses_length++;
2857 /* If the current insn is conditional, we can't free any
2858 of the lists. */
2859 if (sched_has_condition_p (insn))
2861 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
2863 struct deps_reg *reg_last = &deps->reg_last[i];
2864 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2865 add_dependence_list (insn, reg_last->implicit_sets, 0,
2866 REG_DEP_ANTI);
2867 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2869 if (!deps->readonly)
2871 reg_last->clobbers
2872 = alloc_INSN_LIST (insn, reg_last->clobbers);
2873 reg_last->clobbers_length++;
2876 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
2878 struct deps_reg *reg_last = &deps->reg_last[i];
2879 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2880 add_dependence_list (insn, reg_last->implicit_sets, 0,
2881 REG_DEP_ANTI);
2882 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT);
2883 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2885 if (!deps->readonly)
2887 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2888 SET_REGNO_REG_SET (&deps->reg_conditional_sets, i);
2892 else
2894 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
2896 struct deps_reg *reg_last = &deps->reg_last[i];
2897 if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
2898 || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
2900 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
2901 REG_DEP_OUTPUT);
2902 add_dependence_list_and_free (deps, insn,
2903 &reg_last->implicit_sets, 0,
2904 REG_DEP_ANTI);
2905 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
2906 REG_DEP_ANTI);
2907 add_dependence_list_and_free
2908 (deps, insn, &reg_last->clobbers, 0, REG_DEP_OUTPUT);
2910 if (!deps->readonly)
2912 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2913 reg_last->clobbers_length = 0;
2914 reg_last->uses_length = 0;
2917 else
2919 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2920 add_dependence_list (insn, reg_last->implicit_sets, 0,
2921 REG_DEP_ANTI);
2922 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2925 if (!deps->readonly)
2927 reg_last->clobbers_length++;
2928 reg_last->clobbers
2929 = alloc_INSN_LIST (insn, reg_last->clobbers);
2932 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
2934 struct deps_reg *reg_last = &deps->reg_last[i];
2936 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
2937 REG_DEP_OUTPUT);
2938 add_dependence_list_and_free (deps, insn,
2939 &reg_last->implicit_sets,
2940 0, REG_DEP_ANTI);
2941 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
2942 REG_DEP_OUTPUT);
2943 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
2944 REG_DEP_ANTI);
2946 if (!deps->readonly)
2948 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2949 reg_last->uses_length = 0;
2950 reg_last->clobbers_length = 0;
2951 CLEAR_REGNO_REG_SET (&deps->reg_conditional_sets, i);
2957 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2958 if (TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
2960 struct deps_reg *reg_last = &deps->reg_last[i];
2961 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI);
2962 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI);
2963 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2965 if (!deps->readonly)
2966 reg_last->implicit_sets
2967 = alloc_INSN_LIST (insn, reg_last->implicit_sets);
2970 if (!deps->readonly)
2972 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
2973 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
2974 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
2975 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2976 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i)
2977 || TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
2978 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
2980 /* Set up the pending barrier found. */
2981 deps->last_reg_pending_barrier = reg_pending_barrier;
2984 CLEAR_REG_SET (reg_pending_uses);
2985 CLEAR_REG_SET (reg_pending_clobbers);
2986 CLEAR_REG_SET (reg_pending_sets);
2987 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
2988 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
2990 /* Add dependencies if a scheduling barrier was found. */
2991 if (reg_pending_barrier)
2993 /* In the case of barrier the most added dependencies are not
2994 real, so we use anti-dependence here. */
2995 if (sched_has_condition_p (insn))
2997 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2999 struct deps_reg *reg_last = &deps->reg_last[i];
3000 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
3001 add_dependence_list (insn, reg_last->sets, 0,
3002 reg_pending_barrier == TRUE_BARRIER
3003 ? REG_DEP_TRUE : REG_DEP_ANTI);
3004 add_dependence_list (insn, reg_last->implicit_sets, 0,
3005 REG_DEP_ANTI);
3006 add_dependence_list (insn, reg_last->clobbers, 0,
3007 reg_pending_barrier == TRUE_BARRIER
3008 ? REG_DEP_TRUE : REG_DEP_ANTI);
3011 else
3013 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3015 struct deps_reg *reg_last = &deps->reg_last[i];
3016 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3017 REG_DEP_ANTI);
3018 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3019 reg_pending_barrier == TRUE_BARRIER
3020 ? REG_DEP_TRUE : REG_DEP_ANTI);
3021 add_dependence_list_and_free (deps, insn,
3022 &reg_last->implicit_sets, 0,
3023 REG_DEP_ANTI);
3024 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
3025 reg_pending_barrier == TRUE_BARRIER
3026 ? REG_DEP_TRUE : REG_DEP_ANTI);
3028 if (!deps->readonly)
3030 reg_last->uses_length = 0;
3031 reg_last->clobbers_length = 0;
3036 if (!deps->readonly)
3037 for (i = 0; i < (unsigned)deps->max_reg; i++)
3039 struct deps_reg *reg_last = &deps->reg_last[i];
3040 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3041 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
3044 /* Flush pending lists on jumps, but not on speculative checks. */
3045 if (JUMP_P (insn) && !(sel_sched_p ()
3046 && sel_insn_is_speculation_check (insn)))
3047 flush_pending_lists (deps, insn, true, true);
3049 if (!deps->readonly)
3050 CLEAR_REG_SET (&deps->reg_conditional_sets);
3051 reg_pending_barrier = NOT_A_BARRIER;
3054 /* If a post-call group is still open, see if it should remain so.
3055 This insn must be a simple move of a hard reg to a pseudo or
3056 vice-versa.
3058 We must avoid moving these insns for correctness on
3059 SMALL_REGISTER_CLASS machines, and for special registers like
3060 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
3061 hard regs for all targets. */
3063 if (deps->in_post_call_group_p)
3065 rtx tmp, set = single_set (insn);
3066 int src_regno, dest_regno;
3068 if (set == NULL)
3070 if (DEBUG_INSN_P (insn))
3071 /* We don't want to mark debug insns as part of the same
3072 sched group. We know they really aren't, but if we use
3073 debug insns to tell that a call group is over, we'll
3074 get different code if debug insns are not there and
3075 instructions that follow seem like they should be part
3076 of the call group.
3078 Also, if we did, fixup_sched_groups() would move the
3079 deps of the debug insn to the call insn, modifying
3080 non-debug post-dependency counts of the debug insn
3081 dependencies and otherwise messing with the scheduling
3082 order.
3084 Instead, let such debug insns be scheduled freely, but
3085 keep the call group open in case there are insns that
3086 should be part of it afterwards. Since we grant debug
3087 insns higher priority than even sched group insns, it
3088 will all turn out all right. */
3089 goto debug_dont_end_call_group;
3090 else
3091 goto end_call_group;
3094 tmp = SET_DEST (set);
3095 if (GET_CODE (tmp) == SUBREG)
3096 tmp = SUBREG_REG (tmp);
3097 if (REG_P (tmp))
3098 dest_regno = REGNO (tmp);
3099 else
3100 goto end_call_group;
3102 tmp = SET_SRC (set);
3103 if (GET_CODE (tmp) == SUBREG)
3104 tmp = SUBREG_REG (tmp);
3105 if ((GET_CODE (tmp) == PLUS
3106 || GET_CODE (tmp) == MINUS)
3107 && REG_P (XEXP (tmp, 0))
3108 && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
3109 && dest_regno == STACK_POINTER_REGNUM)
3110 src_regno = STACK_POINTER_REGNUM;
3111 else if (REG_P (tmp))
3112 src_regno = REGNO (tmp);
3113 else
3114 goto end_call_group;
3116 if (src_regno < FIRST_PSEUDO_REGISTER
3117 || dest_regno < FIRST_PSEUDO_REGISTER)
3119 if (!deps->readonly
3120 && deps->in_post_call_group_p == post_call_initial)
3121 deps->in_post_call_group_p = post_call;
3123 if (!sel_sched_p () || sched_emulate_haifa_p)
3125 SCHED_GROUP_P (insn) = 1;
3126 CANT_MOVE (insn) = 1;
3129 else
3131 end_call_group:
3132 if (!deps->readonly)
3133 deps->in_post_call_group_p = not_post_call;
3137 debug_dont_end_call_group:
3138 if ((current_sched_info->flags & DO_SPECULATION)
3139 && !sched_insn_is_legitimate_for_speculation_p (insn, 0))
3140 /* INSN has an internal dependency (e.g. r14 = [r14]) and thus cannot
3141 be speculated. */
3143 if (sel_sched_p ())
3144 sel_mark_hard_insn (insn);
3145 else
3147 sd_iterator_def sd_it;
3148 dep_t dep;
3150 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3151 sd_iterator_cond (&sd_it, &dep);)
3152 change_spec_dep_to_hard (sd_it);
3157 /* Return TRUE if INSN might not always return normally (e.g. call exit,
3158 longjmp, loop forever, ...). */
3159 static bool
3160 call_may_noreturn_p (rtx insn)
3162 rtx call;
3164 /* const or pure calls that aren't looping will always return. */
3165 if (RTL_CONST_OR_PURE_CALL_P (insn)
3166 && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn))
3167 return false;
3169 call = PATTERN (insn);
3170 if (GET_CODE (call) == PARALLEL)
3171 call = XVECEXP (call, 0, 0);
3172 if (GET_CODE (call) == SET)
3173 call = SET_SRC (call);
3174 if (GET_CODE (call) == CALL
3175 && MEM_P (XEXP (call, 0))
3176 && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
3178 rtx symbol = XEXP (XEXP (call, 0), 0);
3179 if (SYMBOL_REF_DECL (symbol)
3180 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
3182 if (DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
3183 == BUILT_IN_NORMAL)
3184 switch (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol)))
3186 case BUILT_IN_BCMP:
3187 case BUILT_IN_BCOPY:
3188 case BUILT_IN_BZERO:
3189 case BUILT_IN_INDEX:
3190 case BUILT_IN_MEMCHR:
3191 case BUILT_IN_MEMCMP:
3192 case BUILT_IN_MEMCPY:
3193 case BUILT_IN_MEMMOVE:
3194 case BUILT_IN_MEMPCPY:
3195 case BUILT_IN_MEMSET:
3196 case BUILT_IN_RINDEX:
3197 case BUILT_IN_STPCPY:
3198 case BUILT_IN_STPNCPY:
3199 case BUILT_IN_STRCAT:
3200 case BUILT_IN_STRCHR:
3201 case BUILT_IN_STRCMP:
3202 case BUILT_IN_STRCPY:
3203 case BUILT_IN_STRCSPN:
3204 case BUILT_IN_STRLEN:
3205 case BUILT_IN_STRNCAT:
3206 case BUILT_IN_STRNCMP:
3207 case BUILT_IN_STRNCPY:
3208 case BUILT_IN_STRPBRK:
3209 case BUILT_IN_STRRCHR:
3210 case BUILT_IN_STRSPN:
3211 case BUILT_IN_STRSTR:
3212 /* Assume certain string/memory builtins always return. */
3213 return false;
3214 default:
3215 break;
3220 /* For all other calls assume that they might not always return. */
3221 return true;
3224 /* Analyze INSN with DEPS as a context. */
3225 void
3226 deps_analyze_insn (struct deps *deps, rtx insn)
3228 if (sched_deps_info->start_insn)
3229 sched_deps_info->start_insn (insn);
3231 if (NONJUMP_INSN_P (insn) || DEBUG_INSN_P (insn) || JUMP_P (insn))
3233 /* Make each JUMP_INSN (but not a speculative check)
3234 a scheduling barrier for memory references. */
3235 if (!deps->readonly
3236 && JUMP_P (insn)
3237 && !(sel_sched_p ()
3238 && sel_insn_is_speculation_check (insn)))
3240 /* Keep the list a reasonable size. */
3241 if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
3242 flush_pending_lists (deps, insn, true, true);
3243 else
3244 deps->last_pending_memory_flush
3245 = alloc_INSN_LIST (insn, deps->last_pending_memory_flush);
3248 sched_analyze_insn (deps, PATTERN (insn), insn);
3250 else if (CALL_P (insn))
3252 int i;
3254 CANT_MOVE (insn) = 1;
3256 if (find_reg_note (insn, REG_SETJMP, NULL))
3258 /* This is setjmp. Assume that all registers, not just
3259 hard registers, may be clobbered by this call. */
3260 reg_pending_barrier = MOVE_BARRIER;
3262 else
3264 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3265 /* A call may read and modify global register variables. */
3266 if (global_regs[i])
3268 SET_REGNO_REG_SET (reg_pending_sets, i);
3269 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3271 /* Other call-clobbered hard regs may be clobbered.
3272 Since we only have a choice between 'might be clobbered'
3273 and 'definitely not clobbered', we must include all
3274 partly call-clobbered registers here. */
3275 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
3276 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
3277 SET_REGNO_REG_SET (reg_pending_clobbers, i);
3278 /* We don't know what set of fixed registers might be used
3279 by the function, but it is certain that the stack pointer
3280 is among them, but be conservative. */
3281 else if (fixed_regs[i])
3282 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3283 /* The frame pointer is normally not used by the function
3284 itself, but by the debugger. */
3285 /* ??? MIPS o32 is an exception. It uses the frame pointer
3286 in the macro expansion of jal but does not represent this
3287 fact in the call_insn rtl. */
3288 else if (i == FRAME_POINTER_REGNUM
3289 || (i == HARD_FRAME_POINTER_REGNUM
3290 && (! reload_completed || frame_pointer_needed)))
3291 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3294 /* For each insn which shouldn't cross a call, add a dependence
3295 between that insn and this call insn. */
3296 add_dependence_list_and_free (deps, insn,
3297 &deps->sched_before_next_call, 1,
3298 REG_DEP_ANTI);
3300 sched_analyze_insn (deps, PATTERN (insn), insn);
3302 /* If CALL would be in a sched group, then this will violate
3303 convention that sched group insns have dependencies only on the
3304 previous instruction.
3306 Of course one can say: "Hey! What about head of the sched group?"
3307 And I will answer: "Basic principles (one dep per insn) are always
3308 the same." */
3309 gcc_assert (!SCHED_GROUP_P (insn));
3311 /* In the absence of interprocedural alias analysis, we must flush
3312 all pending reads and writes, and start new dependencies starting
3313 from here. But only flush writes for constant calls (which may
3314 be passed a pointer to something we haven't written yet). */
3315 flush_pending_lists (deps, insn, true, ! RTL_CONST_OR_PURE_CALL_P (insn));
3317 if (!deps->readonly)
3319 /* Remember the last function call for limiting lifetimes. */
3320 free_INSN_LIST_list (&deps->last_function_call);
3321 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
3323 if (call_may_noreturn_p (insn))
3325 /* Remember the last function call that might not always return
3326 normally for limiting moves of trapping insns. */
3327 free_INSN_LIST_list (&deps->last_function_call_may_noreturn);
3328 deps->last_function_call_may_noreturn
3329 = alloc_INSN_LIST (insn, NULL_RTX);
3332 /* Before reload, begin a post-call group, so as to keep the
3333 lifetimes of hard registers correct. */
3334 if (! reload_completed)
3335 deps->in_post_call_group_p = post_call;
3339 if (sched_deps_info->use_cselib)
3340 cselib_process_insn (insn);
3342 /* EH_REGION insn notes can not appear until well after we complete
3343 scheduling. */
3344 if (NOTE_P (insn))
3345 gcc_assert (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
3346 && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END);
3348 if (sched_deps_info->finish_insn)
3349 sched_deps_info->finish_insn ();
3351 /* Fixup the dependencies in the sched group. */
3352 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
3353 && SCHED_GROUP_P (insn) && !sel_sched_p ())
3354 fixup_sched_groups (insn);
3357 /* Initialize DEPS for the new block beginning with HEAD. */
3358 void
3359 deps_start_bb (struct deps *deps, rtx head)
3361 gcc_assert (!deps->readonly);
3363 /* Before reload, if the previous block ended in a call, show that
3364 we are inside a post-call group, so as to keep the lifetimes of
3365 hard registers correct. */
3366 if (! reload_completed && !LABEL_P (head))
3368 rtx insn = prev_nonnote_insn (head);
3370 while (insn && DEBUG_INSN_P (insn))
3371 insn = prev_nonnote_insn (insn);
3372 if (insn && CALL_P (insn))
3373 deps->in_post_call_group_p = post_call_initial;
3377 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
3378 dependencies for each insn. */
3379 void
3380 sched_analyze (struct deps *deps, rtx head, rtx tail)
3382 rtx insn;
3384 if (sched_deps_info->use_cselib)
3385 cselib_init (true);
3387 deps_start_bb (deps, head);
3389 for (insn = head;; insn = NEXT_INSN (insn))
3392 if (INSN_P (insn))
3394 /* And initialize deps_lists. */
3395 sd_init_insn (insn);
3398 deps_analyze_insn (deps, insn);
3400 if (insn == tail)
3402 if (sched_deps_info->use_cselib)
3403 cselib_finish ();
3404 return;
3407 gcc_unreachable ();
3410 /* Helper for sched_free_deps ().
3411 Delete INSN's (RESOLVED_P) backward dependencies. */
3412 static void
3413 delete_dep_nodes_in_back_deps (rtx insn, bool resolved_p)
3415 sd_iterator_def sd_it;
3416 dep_t dep;
3417 sd_list_types_def types;
3419 if (resolved_p)
3420 types = SD_LIST_RES_BACK;
3421 else
3422 types = SD_LIST_BACK;
3424 for (sd_it = sd_iterator_start (insn, types);
3425 sd_iterator_cond (&sd_it, &dep);)
3427 dep_link_t link = *sd_it.linkp;
3428 dep_node_t node = DEP_LINK_NODE (link);
3429 deps_list_t back_list;
3430 deps_list_t forw_list;
3432 get_back_and_forw_lists (dep, resolved_p, &back_list, &forw_list);
3433 remove_from_deps_list (link, back_list);
3434 delete_dep_node (node);
3438 /* Delete (RESOLVED_P) dependencies between HEAD and TAIL together with
3439 deps_lists. */
3440 void
3441 sched_free_deps (rtx head, rtx tail, bool resolved_p)
3443 rtx insn;
3444 rtx next_tail = NEXT_INSN (tail);
3446 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3447 if (INSN_P (insn) && INSN_LUID (insn) > 0)
3449 /* Clear resolved back deps together with its dep_nodes. */
3450 delete_dep_nodes_in_back_deps (insn, resolved_p);
3452 /* Clear forward deps and leave the dep_nodes to the
3453 corresponding back_deps list. */
3454 if (resolved_p)
3455 clear_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
3456 else
3457 clear_deps_list (INSN_FORW_DEPS (insn));
3459 sd_finish_insn (insn);
3463 /* Initialize variables for region data dependence analysis.
3464 n_bbs is the number of region blocks. */
3466 void
3467 init_deps (struct deps *deps)
3469 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
3471 deps->max_reg = max_reg;
3472 deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
3473 INIT_REG_SET (&deps->reg_last_in_use);
3474 INIT_REG_SET (&deps->reg_conditional_sets);
3476 deps->pending_read_insns = 0;
3477 deps->pending_read_mems = 0;
3478 deps->pending_write_insns = 0;
3479 deps->pending_write_mems = 0;
3480 deps->pending_read_list_length = 0;
3481 deps->pending_write_list_length = 0;
3482 deps->pending_flush_length = 0;
3483 deps->last_pending_memory_flush = 0;
3484 deps->last_function_call = 0;
3485 deps->last_function_call_may_noreturn = 0;
3486 deps->sched_before_next_call = 0;
3487 deps->in_post_call_group_p = not_post_call;
3488 deps->last_debug_insn = 0;
3489 deps->last_reg_pending_barrier = NOT_A_BARRIER;
3490 deps->readonly = 0;
3493 /* Free insn lists found in DEPS. */
3495 void
3496 free_deps (struct deps *deps)
3498 unsigned i;
3499 reg_set_iterator rsi;
3501 free_INSN_LIST_list (&deps->pending_read_insns);
3502 free_EXPR_LIST_list (&deps->pending_read_mems);
3503 free_INSN_LIST_list (&deps->pending_write_insns);
3504 free_EXPR_LIST_list (&deps->pending_write_mems);
3505 free_INSN_LIST_list (&deps->last_pending_memory_flush);
3507 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
3508 times. For a testcase with 42000 regs and 8000 small basic blocks,
3509 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
3510 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3512 struct deps_reg *reg_last = &deps->reg_last[i];
3513 if (reg_last->uses)
3514 free_INSN_LIST_list (&reg_last->uses);
3515 if (reg_last->sets)
3516 free_INSN_LIST_list (&reg_last->sets);
3517 if (reg_last->implicit_sets)
3518 free_INSN_LIST_list (&reg_last->implicit_sets);
3519 if (reg_last->clobbers)
3520 free_INSN_LIST_list (&reg_last->clobbers);
3522 CLEAR_REG_SET (&deps->reg_last_in_use);
3523 CLEAR_REG_SET (&deps->reg_conditional_sets);
3525 free (deps->reg_last);
3526 deps->reg_last = NULL;
3528 deps = NULL;
3531 /* Remove INSN from dependence contexts DEPS. Caution: reg_conditional_sets
3532 is not handled. */
3533 void
3534 remove_from_deps (struct deps *deps, rtx insn)
3536 int removed;
3537 unsigned i;
3538 reg_set_iterator rsi;
3540 removed = remove_from_both_dependence_lists (insn, &deps->pending_read_insns,
3541 &deps->pending_read_mems);
3542 if (!DEBUG_INSN_P (insn))
3543 deps->pending_read_list_length -= removed;
3544 removed = remove_from_both_dependence_lists (insn, &deps->pending_write_insns,
3545 &deps->pending_write_mems);
3546 deps->pending_write_list_length -= removed;
3547 removed = remove_from_dependence_list (insn, &deps->last_pending_memory_flush);
3548 deps->pending_flush_length -= removed;
3550 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3552 struct deps_reg *reg_last = &deps->reg_last[i];
3553 if (reg_last->uses)
3554 remove_from_dependence_list (insn, &reg_last->uses);
3555 if (reg_last->sets)
3556 remove_from_dependence_list (insn, &reg_last->sets);
3557 if (reg_last->implicit_sets)
3558 remove_from_dependence_list (insn, &reg_last->implicit_sets);
3559 if (reg_last->clobbers)
3560 remove_from_dependence_list (insn, &reg_last->clobbers);
3561 if (!reg_last->uses && !reg_last->sets && !reg_last->implicit_sets
3562 && !reg_last->clobbers)
3563 CLEAR_REGNO_REG_SET (&deps->reg_last_in_use, i);
3566 if (CALL_P (insn))
3568 remove_from_dependence_list (insn, &deps->last_function_call);
3569 remove_from_dependence_list (insn,
3570 &deps->last_function_call_may_noreturn);
3572 remove_from_dependence_list (insn, &deps->sched_before_next_call);
3575 /* Init deps data vector. */
3576 static void
3577 init_deps_data_vector (void)
3579 int reserve = (sched_max_luid + 1
3580 - VEC_length (haifa_deps_insn_data_def, h_d_i_d));
3581 if (reserve > 0
3582 && ! VEC_space (haifa_deps_insn_data_def, h_d_i_d, reserve))
3583 VEC_safe_grow_cleared (haifa_deps_insn_data_def, heap, h_d_i_d,
3584 3 * sched_max_luid / 2);
3587 /* If it is profitable to use them, initialize or extend (depending on
3588 GLOBAL_P) dependency data. */
3589 void
3590 sched_deps_init (bool global_p)
3592 /* Average number of insns in the basic block.
3593 '+ 1' is used to make it nonzero. */
3594 int insns_in_block = sched_max_luid / n_basic_blocks + 1;
3596 init_deps_data_vector ();
3598 /* We use another caching mechanism for selective scheduling, so
3599 we don't use this one. */
3600 if (!sel_sched_p () && global_p && insns_in_block > 100 * 5)
3602 /* ?!? We could save some memory by computing a per-region luid mapping
3603 which could reduce both the number of vectors in the cache and the
3604 size of each vector. Instead we just avoid the cache entirely unless
3605 the average number of instructions in a basic block is very high. See
3606 the comment before the declaration of true_dependency_cache for
3607 what we consider "very high". */
3608 cache_size = 0;
3609 extend_dependency_caches (sched_max_luid, true);
3612 if (global_p)
3614 dl_pool = create_alloc_pool ("deps_list", sizeof (struct _deps_list),
3615 /* Allocate lists for one block at a time. */
3616 insns_in_block);
3617 dn_pool = create_alloc_pool ("dep_node", sizeof (struct _dep_node),
3618 /* Allocate nodes for one block at a time.
3619 We assume that average insn has
3620 5 producers. */
3621 5 * insns_in_block);
3626 /* Create or extend (depending on CREATE_P) dependency caches to
3627 size N. */
3628 void
3629 extend_dependency_caches (int n, bool create_p)
3631 if (create_p || true_dependency_cache)
3633 int i, luid = cache_size + n;
3635 true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
3636 luid);
3637 output_dependency_cache = XRESIZEVEC (bitmap_head,
3638 output_dependency_cache, luid);
3639 anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
3640 luid);
3642 if (current_sched_info->flags & DO_SPECULATION)
3643 spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
3644 luid);
3646 for (i = cache_size; i < luid; i++)
3648 bitmap_initialize (&true_dependency_cache[i], 0);
3649 bitmap_initialize (&output_dependency_cache[i], 0);
3650 bitmap_initialize (&anti_dependency_cache[i], 0);
3652 if (current_sched_info->flags & DO_SPECULATION)
3653 bitmap_initialize (&spec_dependency_cache[i], 0);
3655 cache_size = luid;
3659 /* Finalize dependency information for the whole function. */
3660 void
3661 sched_deps_finish (void)
3663 gcc_assert (deps_pools_are_empty_p ());
3664 free_alloc_pool_if_empty (&dn_pool);
3665 free_alloc_pool_if_empty (&dl_pool);
3666 gcc_assert (dn_pool == NULL && dl_pool == NULL);
3668 VEC_free (haifa_deps_insn_data_def, heap, h_d_i_d);
3669 cache_size = 0;
3671 if (true_dependency_cache)
3673 int i;
3675 for (i = 0; i < cache_size; i++)
3677 bitmap_clear (&true_dependency_cache[i]);
3678 bitmap_clear (&output_dependency_cache[i]);
3679 bitmap_clear (&anti_dependency_cache[i]);
3681 if (sched_deps_info->generate_spec_deps)
3682 bitmap_clear (&spec_dependency_cache[i]);
3684 free (true_dependency_cache);
3685 true_dependency_cache = NULL;
3686 free (output_dependency_cache);
3687 output_dependency_cache = NULL;
3688 free (anti_dependency_cache);
3689 anti_dependency_cache = NULL;
3691 if (sched_deps_info->generate_spec_deps)
3693 free (spec_dependency_cache);
3694 spec_dependency_cache = NULL;
3700 /* Initialize some global variables needed by the dependency analysis
3701 code. */
3703 void
3704 init_deps_global (void)
3706 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
3707 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
3708 reg_pending_sets = ALLOC_REG_SET (&reg_obstack);
3709 reg_pending_clobbers = ALLOC_REG_SET (&reg_obstack);
3710 reg_pending_uses = ALLOC_REG_SET (&reg_obstack);
3711 reg_pending_barrier = NOT_A_BARRIER;
3713 if (!sel_sched_p () || sched_emulate_haifa_p)
3715 sched_deps_info->start_insn = haifa_start_insn;
3716 sched_deps_info->finish_insn = haifa_finish_insn;
3718 sched_deps_info->note_reg_set = haifa_note_reg_set;
3719 sched_deps_info->note_reg_clobber = haifa_note_reg_clobber;
3720 sched_deps_info->note_reg_use = haifa_note_reg_use;
3722 sched_deps_info->note_mem_dep = haifa_note_mem_dep;
3723 sched_deps_info->note_dep = haifa_note_dep;
3727 /* Free everything used by the dependency analysis code. */
3729 void
3730 finish_deps_global (void)
3732 FREE_REG_SET (reg_pending_sets);
3733 FREE_REG_SET (reg_pending_clobbers);
3734 FREE_REG_SET (reg_pending_uses);
3737 /* Estimate the weakness of dependence between MEM1 and MEM2. */
3738 dw_t
3739 estimate_dep_weak (rtx mem1, rtx mem2)
3741 rtx r1, r2;
3743 if (mem1 == mem2)
3744 /* MEMs are the same - don't speculate. */
3745 return MIN_DEP_WEAK;
3747 r1 = XEXP (mem1, 0);
3748 r2 = XEXP (mem2, 0);
3750 if (r1 == r2
3751 || (REG_P (r1) && REG_P (r2)
3752 && REGNO (r1) == REGNO (r2)))
3753 /* Again, MEMs are the same. */
3754 return MIN_DEP_WEAK;
3755 else if ((REG_P (r1) && !REG_P (r2))
3756 || (!REG_P (r1) && REG_P (r2)))
3757 /* Different addressing modes - reason to be more speculative,
3758 than usual. */
3759 return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
3760 else
3761 /* We can't say anything about the dependence. */
3762 return UNCERTAIN_DEP_WEAK;
3765 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
3766 This function can handle same INSN and ELEM (INSN == ELEM).
3767 It is a convenience wrapper. */
3768 void
3769 add_dependence (rtx insn, rtx elem, enum reg_note dep_type)
3771 ds_t ds;
3772 bool internal;
3774 if (dep_type == REG_DEP_TRUE)
3775 ds = DEP_TRUE;
3776 else if (dep_type == REG_DEP_OUTPUT)
3777 ds = DEP_OUTPUT;
3778 else
3780 gcc_assert (dep_type == REG_DEP_ANTI);
3781 ds = DEP_ANTI;
3784 /* When add_dependence is called from inside sched-deps.c, we expect
3785 cur_insn to be non-null. */
3786 internal = cur_insn != NULL;
3787 if (internal)
3788 gcc_assert (insn == cur_insn);
3789 else
3790 cur_insn = insn;
3792 note_dep (elem, ds);
3793 if (!internal)
3794 cur_insn = NULL;
3797 /* Return weakness of speculative type TYPE in the dep_status DS. */
3798 dw_t
3799 get_dep_weak_1 (ds_t ds, ds_t type)
3801 ds = ds & type;
3803 switch (type)
3805 case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
3806 case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
3807 case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
3808 case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
3809 default: gcc_unreachable ();
3812 return (dw_t) ds;
3815 dw_t
3816 get_dep_weak (ds_t ds, ds_t type)
3818 dw_t dw = get_dep_weak_1 (ds, type);
3820 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
3821 return dw;
3824 /* Return the dep_status, which has the same parameters as DS, except for
3825 speculative type TYPE, that will have weakness DW. */
3826 ds_t
3827 set_dep_weak (ds_t ds, ds_t type, dw_t dw)
3829 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
3831 ds &= ~type;
3832 switch (type)
3834 case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
3835 case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
3836 case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
3837 case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
3838 default: gcc_unreachable ();
3840 return ds;
3843 /* Return the join of two dep_statuses DS1 and DS2.
3844 If MAX_P is true then choose the greater probability,
3845 otherwise multiply probabilities.
3846 This function assumes that both DS1 and DS2 contain speculative bits. */
3847 static ds_t
3848 ds_merge_1 (ds_t ds1, ds_t ds2, bool max_p)
3850 ds_t ds, t;
3852 gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
3854 ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
3856 t = FIRST_SPEC_TYPE;
3859 if ((ds1 & t) && !(ds2 & t))
3860 ds |= ds1 & t;
3861 else if (!(ds1 & t) && (ds2 & t))
3862 ds |= ds2 & t;
3863 else if ((ds1 & t) && (ds2 & t))
3865 dw_t dw1 = get_dep_weak (ds1, t);
3866 dw_t dw2 = get_dep_weak (ds2, t);
3867 ds_t dw;
3869 if (!max_p)
3871 dw = ((ds_t) dw1) * ((ds_t) dw2);
3872 dw /= MAX_DEP_WEAK;
3873 if (dw < MIN_DEP_WEAK)
3874 dw = MIN_DEP_WEAK;
3876 else
3878 if (dw1 >= dw2)
3879 dw = dw1;
3880 else
3881 dw = dw2;
3884 ds = set_dep_weak (ds, t, (dw_t) dw);
3887 if (t == LAST_SPEC_TYPE)
3888 break;
3889 t <<= SPEC_TYPE_SHIFT;
3891 while (1);
3893 return ds;
3896 /* Return the join of two dep_statuses DS1 and DS2.
3897 This function assumes that both DS1 and DS2 contain speculative bits. */
3898 ds_t
3899 ds_merge (ds_t ds1, ds_t ds2)
3901 return ds_merge_1 (ds1, ds2, false);
3904 /* Return the join of two dep_statuses DS1 and DS2. */
3905 ds_t
3906 ds_full_merge (ds_t ds, ds_t ds2, rtx mem1, rtx mem2)
3908 ds_t new_status = ds | ds2;
3910 if (new_status & SPECULATIVE)
3912 if ((ds && !(ds & SPECULATIVE))
3913 || (ds2 && !(ds2 & SPECULATIVE)))
3914 /* Then this dep can't be speculative. */
3915 new_status &= ~SPECULATIVE;
3916 else
3918 /* Both are speculative. Merging probabilities. */
3919 if (mem1)
3921 dw_t dw;
3923 dw = estimate_dep_weak (mem1, mem2);
3924 ds = set_dep_weak (ds, BEGIN_DATA, dw);
3927 if (!ds)
3928 new_status = ds2;
3929 else if (!ds2)
3930 new_status = ds;
3931 else
3932 new_status = ds_merge (ds2, ds);
3936 return new_status;
3939 /* Return the join of DS1 and DS2. Use maximum instead of multiplying
3940 probabilities. */
3941 ds_t
3942 ds_max_merge (ds_t ds1, ds_t ds2)
3944 if (ds1 == 0 && ds2 == 0)
3945 return 0;
3947 if (ds1 == 0 && ds2 != 0)
3948 return ds2;
3950 if (ds1 != 0 && ds2 == 0)
3951 return ds1;
3953 return ds_merge_1 (ds1, ds2, true);
3956 /* Return the probability of speculation success for the speculation
3957 status DS. */
3958 dw_t
3959 ds_weak (ds_t ds)
3961 ds_t res = 1, dt;
3962 int n = 0;
3964 dt = FIRST_SPEC_TYPE;
3967 if (ds & dt)
3969 res *= (ds_t) get_dep_weak (ds, dt);
3970 n++;
3973 if (dt == LAST_SPEC_TYPE)
3974 break;
3975 dt <<= SPEC_TYPE_SHIFT;
3977 while (1);
3979 gcc_assert (n);
3980 while (--n)
3981 res /= MAX_DEP_WEAK;
3983 if (res < MIN_DEP_WEAK)
3984 res = MIN_DEP_WEAK;
3986 gcc_assert (res <= MAX_DEP_WEAK);
3988 return (dw_t) res;
3991 /* Return a dep status that contains all speculation types of DS. */
3992 ds_t
3993 ds_get_speculation_types (ds_t ds)
3995 if (ds & BEGIN_DATA)
3996 ds |= BEGIN_DATA;
3997 if (ds & BE_IN_DATA)
3998 ds |= BE_IN_DATA;
3999 if (ds & BEGIN_CONTROL)
4000 ds |= BEGIN_CONTROL;
4001 if (ds & BE_IN_CONTROL)
4002 ds |= BE_IN_CONTROL;
4004 return ds & SPECULATIVE;
4007 /* Return a dep status that contains maximal weakness for each speculation
4008 type present in DS. */
4009 ds_t
4010 ds_get_max_dep_weak (ds_t ds)
4012 if (ds & BEGIN_DATA)
4013 ds = set_dep_weak (ds, BEGIN_DATA, MAX_DEP_WEAK);
4014 if (ds & BE_IN_DATA)
4015 ds = set_dep_weak (ds, BE_IN_DATA, MAX_DEP_WEAK);
4016 if (ds & BEGIN_CONTROL)
4017 ds = set_dep_weak (ds, BEGIN_CONTROL, MAX_DEP_WEAK);
4018 if (ds & BE_IN_CONTROL)
4019 ds = set_dep_weak (ds, BE_IN_CONTROL, MAX_DEP_WEAK);
4021 return ds;
4024 /* Dump information about the dependence status S. */
4025 static void
4026 dump_ds (FILE *f, ds_t s)
4028 fprintf (f, "{");
4030 if (s & BEGIN_DATA)
4031 fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak_1 (s, BEGIN_DATA));
4032 if (s & BE_IN_DATA)
4033 fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak_1 (s, BE_IN_DATA));
4034 if (s & BEGIN_CONTROL)
4035 fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak_1 (s, BEGIN_CONTROL));
4036 if (s & BE_IN_CONTROL)
4037 fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak_1 (s, BE_IN_CONTROL));
4039 if (s & HARD_DEP)
4040 fprintf (f, "HARD_DEP; ");
4042 if (s & DEP_TRUE)
4043 fprintf (f, "DEP_TRUE; ");
4044 if (s & DEP_ANTI)
4045 fprintf (f, "DEP_ANTI; ");
4046 if (s & DEP_OUTPUT)
4047 fprintf (f, "DEP_OUTPUT; ");
4049 fprintf (f, "}");
4052 void
4053 debug_ds (ds_t s)
4055 dump_ds (stderr, s);
4056 fprintf (stderr, "\n");
4059 #ifdef ENABLE_CHECKING
4060 /* Verify that dependence type and status are consistent.
4061 If RELAXED_P is true, then skip dep_weakness checks. */
4062 static void
4063 check_dep (dep_t dep, bool relaxed_p)
4065 enum reg_note dt = DEP_TYPE (dep);
4066 ds_t ds = DEP_STATUS (dep);
4068 gcc_assert (DEP_PRO (dep) != DEP_CON (dep));
4070 if (!(current_sched_info->flags & USE_DEPS_LIST))
4072 gcc_assert (ds == -1);
4073 return;
4076 /* Check that dependence type contains the same bits as the status. */
4077 if (dt == REG_DEP_TRUE)
4078 gcc_assert (ds & DEP_TRUE);
4079 else if (dt == REG_DEP_OUTPUT)
4080 gcc_assert ((ds & DEP_OUTPUT)
4081 && !(ds & DEP_TRUE));
4082 else
4083 gcc_assert ((dt == REG_DEP_ANTI)
4084 && (ds & DEP_ANTI)
4085 && !(ds & (DEP_OUTPUT | DEP_TRUE)));
4087 /* HARD_DEP can not appear in dep_status of a link. */
4088 gcc_assert (!(ds & HARD_DEP));
4090 /* Check that dependence status is set correctly when speculation is not
4091 supported. */
4092 if (!sched_deps_info->generate_spec_deps)
4093 gcc_assert (!(ds & SPECULATIVE));
4094 else if (ds & SPECULATIVE)
4096 if (!relaxed_p)
4098 ds_t type = FIRST_SPEC_TYPE;
4100 /* Check that dependence weakness is in proper range. */
4103 if (ds & type)
4104 get_dep_weak (ds, type);
4106 if (type == LAST_SPEC_TYPE)
4107 break;
4108 type <<= SPEC_TYPE_SHIFT;
4110 while (1);
4113 if (ds & BEGIN_SPEC)
4115 /* Only true dependence can be data speculative. */
4116 if (ds & BEGIN_DATA)
4117 gcc_assert (ds & DEP_TRUE);
4119 /* Control dependencies in the insn scheduler are represented by
4120 anti-dependencies, therefore only anti dependence can be
4121 control speculative. */
4122 if (ds & BEGIN_CONTROL)
4123 gcc_assert (ds & DEP_ANTI);
4125 else
4127 /* Subsequent speculations should resolve true dependencies. */
4128 gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
4131 /* Check that true and anti dependencies can't have other speculative
4132 statuses. */
4133 if (ds & DEP_TRUE)
4134 gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
4135 /* An output dependence can't be speculative at all. */
4136 gcc_assert (!(ds & DEP_OUTPUT));
4137 if (ds & DEP_ANTI)
4138 gcc_assert (ds & BEGIN_CONTROL);
4141 #endif /* ENABLE_CHECKING */
4143 #endif /* INSN_SCHEDULING */