Daily bump.
[official-gcc.git] / gcc / sched-deps.c
blob09bf65d9093d3170d0aacde0e3318bca222ac8b7
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, 2010,
5 2011
6 Free Software Foundation, Inc.
7 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
8 and currently maintained by, Jim Wilson (wilson@cygnus.com)
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free
14 Software Foundation; either version 3, or (at your option) any later
15 version.
17 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "diagnostic-core.h"
31 #include "rtl.h"
32 #include "tm_p.h"
33 #include "hard-reg-set.h"
34 #include "regs.h"
35 #include "function.h"
36 #include "flags.h"
37 #include "insn-config.h"
38 #include "insn-attr.h"
39 #include "except.h"
40 #include "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 /* In deps->last_pending_memory_flush marks JUMP_INSNs that weren't
56 added to the list because of flush_pending_lists, stands just
57 for itself and not for any other pending memory reads/writes. */
58 #define NON_FLUSH_JUMP_KIND REG_DEP_ANTI
59 #define NON_FLUSH_JUMP_P(x) (REG_NOTE_KIND (x) == NON_FLUSH_JUMP_KIND)
61 /* Holds current parameters for the dependency analyzer. */
62 struct sched_deps_info_def *sched_deps_info;
64 /* The data is specific to the Haifa scheduler. */
65 VEC(haifa_deps_insn_data_def, heap) *h_d_i_d = NULL;
67 /* Return the major type present in the DS. */
68 enum reg_note
69 ds_to_dk (ds_t ds)
71 if (ds & DEP_TRUE)
72 return REG_DEP_TRUE;
74 if (ds & DEP_OUTPUT)
75 return REG_DEP_OUTPUT;
77 gcc_assert (ds & DEP_ANTI);
79 return REG_DEP_ANTI;
82 /* Return equivalent dep_status. */
83 ds_t
84 dk_to_ds (enum reg_note dk)
86 switch (dk)
88 case REG_DEP_TRUE:
89 return DEP_TRUE;
91 case REG_DEP_OUTPUT:
92 return DEP_OUTPUT;
94 default:
95 gcc_assert (dk == REG_DEP_ANTI);
96 return DEP_ANTI;
100 /* Functions to operate with dependence information container - dep_t. */
102 /* Init DEP with the arguments. */
103 void
104 init_dep_1 (dep_t dep, rtx pro, rtx con, enum reg_note type, ds_t ds)
106 DEP_PRO (dep) = pro;
107 DEP_CON (dep) = con;
108 DEP_TYPE (dep) = type;
109 DEP_STATUS (dep) = ds;
112 /* Init DEP with the arguments.
113 While most of the scheduler (including targets) only need the major type
114 of the dependency, it is convenient to hide full dep_status from them. */
115 void
116 init_dep (dep_t dep, rtx pro, rtx con, enum reg_note kind)
118 ds_t ds;
120 if ((current_sched_info->flags & USE_DEPS_LIST))
121 ds = dk_to_ds (kind);
122 else
123 ds = -1;
125 init_dep_1 (dep, pro, con, kind, ds);
128 /* Make a copy of FROM in TO. */
129 static void
130 copy_dep (dep_t to, dep_t from)
132 memcpy (to, from, sizeof (*to));
135 static void dump_ds (FILE *, ds_t);
137 /* Define flags for dump_dep (). */
139 /* Dump producer of the dependence. */
140 #define DUMP_DEP_PRO (2)
142 /* Dump consumer of the dependence. */
143 #define DUMP_DEP_CON (4)
145 /* Dump type of the dependence. */
146 #define DUMP_DEP_TYPE (8)
148 /* Dump status of the dependence. */
149 #define DUMP_DEP_STATUS (16)
151 /* Dump all information about the dependence. */
152 #define DUMP_DEP_ALL (DUMP_DEP_PRO | DUMP_DEP_CON | DUMP_DEP_TYPE \
153 |DUMP_DEP_STATUS)
155 /* Dump DEP to DUMP.
156 FLAGS is a bit mask specifying what information about DEP needs
157 to be printed.
158 If FLAGS has the very first bit set, then dump all information about DEP
159 and propagate this bit into the callee dump functions. */
160 static void
161 dump_dep (FILE *dump, dep_t dep, int flags)
163 if (flags & 1)
164 flags |= DUMP_DEP_ALL;
166 fprintf (dump, "<");
168 if (flags & DUMP_DEP_PRO)
169 fprintf (dump, "%d; ", INSN_UID (DEP_PRO (dep)));
171 if (flags & DUMP_DEP_CON)
172 fprintf (dump, "%d; ", INSN_UID (DEP_CON (dep)));
174 if (flags & DUMP_DEP_TYPE)
176 char t;
177 enum reg_note type = DEP_TYPE (dep);
179 switch (type)
181 case REG_DEP_TRUE:
182 t = 't';
183 break;
185 case REG_DEP_OUTPUT:
186 t = 'o';
187 break;
189 case REG_DEP_ANTI:
190 t = 'a';
191 break;
193 default:
194 gcc_unreachable ();
195 break;
198 fprintf (dump, "%c; ", t);
201 if (flags & DUMP_DEP_STATUS)
203 if (current_sched_info->flags & USE_DEPS_LIST)
204 dump_ds (dump, DEP_STATUS (dep));
207 fprintf (dump, ">");
210 /* Default flags for dump_dep (). */
211 static int dump_dep_flags = (DUMP_DEP_PRO | DUMP_DEP_CON);
213 /* Dump all fields of DEP to STDERR. */
214 void
215 sd_debug_dep (dep_t dep)
217 dump_dep (stderr, dep, 1);
218 fprintf (stderr, "\n");
221 /* Determine whether DEP is a dependency link of a non-debug insn on a
222 debug insn. */
224 static inline bool
225 depl_on_debug_p (dep_link_t dep)
227 return (DEBUG_INSN_P (DEP_LINK_PRO (dep))
228 && !DEBUG_INSN_P (DEP_LINK_CON (dep)));
231 /* Functions to operate with a single link from the dependencies lists -
232 dep_link_t. */
234 /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
235 PREV_NEXT_P. */
236 static void
237 attach_dep_link (dep_link_t l, dep_link_t *prev_nextp)
239 dep_link_t next = *prev_nextp;
241 gcc_assert (DEP_LINK_PREV_NEXTP (l) == NULL
242 && DEP_LINK_NEXT (l) == NULL);
244 /* Init node being inserted. */
245 DEP_LINK_PREV_NEXTP (l) = prev_nextp;
246 DEP_LINK_NEXT (l) = next;
248 /* Fix next node. */
249 if (next != NULL)
251 gcc_assert (DEP_LINK_PREV_NEXTP (next) == prev_nextp);
253 DEP_LINK_PREV_NEXTP (next) = &DEP_LINK_NEXT (l);
256 /* Fix prev node. */
257 *prev_nextp = l;
260 /* Add dep_link LINK to deps_list L. */
261 static void
262 add_to_deps_list (dep_link_t link, deps_list_t l)
264 attach_dep_link (link, &DEPS_LIST_FIRST (l));
266 /* Don't count debug deps. */
267 if (!depl_on_debug_p (link))
268 ++DEPS_LIST_N_LINKS (l);
271 /* Detach dep_link L from the list. */
272 static void
273 detach_dep_link (dep_link_t l)
275 dep_link_t *prev_nextp = DEP_LINK_PREV_NEXTP (l);
276 dep_link_t next = DEP_LINK_NEXT (l);
278 *prev_nextp = next;
280 if (next != NULL)
281 DEP_LINK_PREV_NEXTP (next) = prev_nextp;
283 DEP_LINK_PREV_NEXTP (l) = NULL;
284 DEP_LINK_NEXT (l) = NULL;
287 /* Remove link LINK from list LIST. */
288 static void
289 remove_from_deps_list (dep_link_t link, deps_list_t list)
291 detach_dep_link (link);
293 /* Don't count debug deps. */
294 if (!depl_on_debug_p (link))
295 --DEPS_LIST_N_LINKS (list);
298 /* Move link LINK from list FROM to list TO. */
299 static void
300 move_dep_link (dep_link_t link, deps_list_t from, deps_list_t to)
302 remove_from_deps_list (link, from);
303 add_to_deps_list (link, to);
306 /* Return true of LINK is not attached to any list. */
307 static bool
308 dep_link_is_detached_p (dep_link_t link)
310 return DEP_LINK_PREV_NEXTP (link) == NULL;
313 /* Pool to hold all dependency nodes (dep_node_t). */
314 static alloc_pool dn_pool;
316 /* Number of dep_nodes out there. */
317 static int dn_pool_diff = 0;
319 /* Create a dep_node. */
320 static dep_node_t
321 create_dep_node (void)
323 dep_node_t n = (dep_node_t) pool_alloc (dn_pool);
324 dep_link_t back = DEP_NODE_BACK (n);
325 dep_link_t forw = DEP_NODE_FORW (n);
327 DEP_LINK_NODE (back) = n;
328 DEP_LINK_NEXT (back) = NULL;
329 DEP_LINK_PREV_NEXTP (back) = NULL;
331 DEP_LINK_NODE (forw) = n;
332 DEP_LINK_NEXT (forw) = NULL;
333 DEP_LINK_PREV_NEXTP (forw) = NULL;
335 ++dn_pool_diff;
337 return n;
340 /* Delete dep_node N. N must not be connected to any deps_list. */
341 static void
342 delete_dep_node (dep_node_t n)
344 gcc_assert (dep_link_is_detached_p (DEP_NODE_BACK (n))
345 && dep_link_is_detached_p (DEP_NODE_FORW (n)));
347 --dn_pool_diff;
349 pool_free (dn_pool, n);
352 /* Pool to hold dependencies lists (deps_list_t). */
353 static alloc_pool dl_pool;
355 /* Number of deps_lists out there. */
356 static int dl_pool_diff = 0;
358 /* Functions to operate with dependences lists - deps_list_t. */
360 /* Return true if list L is empty. */
361 static bool
362 deps_list_empty_p (deps_list_t l)
364 return DEPS_LIST_N_LINKS (l) == 0;
367 /* Create a new deps_list. */
368 static deps_list_t
369 create_deps_list (void)
371 deps_list_t l = (deps_list_t) pool_alloc (dl_pool);
373 DEPS_LIST_FIRST (l) = NULL;
374 DEPS_LIST_N_LINKS (l) = 0;
376 ++dl_pool_diff;
377 return l;
380 /* Free deps_list L. */
381 static void
382 free_deps_list (deps_list_t l)
384 gcc_assert (deps_list_empty_p (l));
386 --dl_pool_diff;
388 pool_free (dl_pool, l);
391 /* Return true if there is no dep_nodes and deps_lists out there.
392 After the region is scheduled all the dependency nodes and lists
393 should [generally] be returned to pool. */
394 bool
395 deps_pools_are_empty_p (void)
397 return dn_pool_diff == 0 && dl_pool_diff == 0;
400 /* Remove all elements from L. */
401 static void
402 clear_deps_list (deps_list_t l)
406 dep_link_t link = DEPS_LIST_FIRST (l);
408 if (link == NULL)
409 break;
411 remove_from_deps_list (link, l);
413 while (1);
416 static regset reg_pending_sets;
417 static regset reg_pending_clobbers;
418 static regset reg_pending_uses;
419 static enum reg_pending_barrier_mode reg_pending_barrier;
421 /* Hard registers implicitly clobbered or used (or may be implicitly
422 clobbered or used) by the currently analyzed insn. For example,
423 insn in its constraint has one register class. Even if there is
424 currently no hard register in the insn, the particular hard
425 register will be in the insn after reload pass because the
426 constraint requires it. */
427 static HARD_REG_SET implicit_reg_pending_clobbers;
428 static HARD_REG_SET implicit_reg_pending_uses;
430 /* To speed up the test for duplicate dependency links we keep a
431 record of dependencies created by add_dependence when the average
432 number of instructions in a basic block is very large.
434 Studies have shown that there is typically around 5 instructions between
435 branches for typical C code. So we can make a guess that the average
436 basic block is approximately 5 instructions long; we will choose 100X
437 the average size as a very large basic block.
439 Each insn has associated bitmaps for its dependencies. Each bitmap
440 has enough entries to represent a dependency on any other insn in
441 the insn chain. All bitmap for true dependencies cache is
442 allocated then the rest two ones are also allocated. */
443 static bitmap_head *true_dependency_cache = NULL;
444 static bitmap_head *output_dependency_cache = NULL;
445 static bitmap_head *anti_dependency_cache = NULL;
446 static bitmap_head *spec_dependency_cache = NULL;
447 static int cache_size;
449 static int deps_may_trap_p (const_rtx);
450 static void add_dependence_list (rtx, rtx, int, enum reg_note);
451 static void add_dependence_list_and_free (struct deps_desc *, rtx,
452 rtx *, int, enum reg_note);
453 static void delete_all_dependences (rtx);
454 static void fixup_sched_groups (rtx);
456 static void flush_pending_lists (struct deps_desc *, rtx, int, int);
457 static void sched_analyze_1 (struct deps_desc *, rtx, rtx);
458 static void sched_analyze_2 (struct deps_desc *, rtx, rtx);
459 static void sched_analyze_insn (struct deps_desc *, rtx, rtx);
461 static bool sched_has_condition_p (const_rtx);
462 static int conditions_mutex_p (const_rtx, const_rtx, bool, bool);
464 static enum DEPS_ADJUST_RESULT maybe_add_or_update_dep_1 (dep_t, bool,
465 rtx, rtx);
466 static enum DEPS_ADJUST_RESULT add_or_update_dep_1 (dep_t, bool, rtx, rtx);
468 #ifdef ENABLE_CHECKING
469 static void check_dep (dep_t, bool);
470 #endif
472 /* Return nonzero if a load of the memory reference MEM can cause a trap. */
474 static int
475 deps_may_trap_p (const_rtx mem)
477 const_rtx addr = XEXP (mem, 0);
479 if (REG_P (addr) && REGNO (addr) >= FIRST_PSEUDO_REGISTER)
481 const_rtx t = get_reg_known_value (REGNO (addr));
482 if (t)
483 addr = t;
485 return rtx_addr_can_trap_p (addr);
489 /* Find the condition under which INSN is executed. If REV is not NULL,
490 it is set to TRUE when the returned comparison should be reversed
491 to get the actual condition. */
492 static rtx
493 sched_get_condition_with_rev (const_rtx insn, bool *rev)
495 rtx pat = PATTERN (insn);
496 rtx src;
498 if (pat == 0)
499 return 0;
501 if (rev)
502 *rev = false;
504 if (GET_CODE (pat) == COND_EXEC)
505 return COND_EXEC_TEST (pat);
507 if (!any_condjump_p (insn) || !onlyjump_p (insn))
508 return 0;
510 src = SET_SRC (pc_set (insn));
512 if (XEXP (src, 2) == pc_rtx)
513 return XEXP (src, 0);
514 else if (XEXP (src, 1) == pc_rtx)
516 rtx cond = XEXP (src, 0);
517 enum rtx_code revcode = reversed_comparison_code (cond, insn);
519 if (revcode == UNKNOWN)
520 return 0;
522 if (rev)
523 *rev = true;
524 return cond;
527 return 0;
530 /* True when we can find a condition under which INSN is executed. */
531 static bool
532 sched_has_condition_p (const_rtx insn)
534 return !! sched_get_condition_with_rev (insn, NULL);
539 /* Return nonzero if conditions COND1 and COND2 can never be both true. */
540 static int
541 conditions_mutex_p (const_rtx cond1, const_rtx cond2, bool rev1, bool rev2)
543 if (COMPARISON_P (cond1)
544 && COMPARISON_P (cond2)
545 && GET_CODE (cond1) ==
546 (rev1==rev2
547 ? reversed_comparison_code (cond2, NULL)
548 : GET_CODE (cond2))
549 && XEXP (cond1, 0) == XEXP (cond2, 0)
550 && XEXP (cond1, 1) == XEXP (cond2, 1))
551 return 1;
552 return 0;
555 /* Return true if insn1 and insn2 can never depend on one another because
556 the conditions under which they are executed are mutually exclusive. */
557 bool
558 sched_insns_conditions_mutex_p (const_rtx insn1, const_rtx insn2)
560 rtx cond1, cond2;
561 bool rev1 = false, rev2 = false;
563 /* df doesn't handle conditional lifetimes entirely correctly;
564 calls mess up the conditional lifetimes. */
565 if (!CALL_P (insn1) && !CALL_P (insn2))
567 cond1 = sched_get_condition_with_rev (insn1, &rev1);
568 cond2 = sched_get_condition_with_rev (insn2, &rev2);
569 if (cond1 && cond2
570 && conditions_mutex_p (cond1, cond2, rev1, rev2)
571 /* Make sure first instruction doesn't affect condition of second
572 instruction if switched. */
573 && !modified_in_p (cond1, insn2)
574 /* Make sure second instruction doesn't affect condition of first
575 instruction if switched. */
576 && !modified_in_p (cond2, insn1))
577 return true;
579 return false;
583 /* Return true if INSN can potentially be speculated with type DS. */
584 bool
585 sched_insn_is_legitimate_for_speculation_p (const_rtx insn, ds_t ds)
587 if (HAS_INTERNAL_DEP (insn))
588 return false;
590 if (!NONJUMP_INSN_P (insn))
591 return false;
593 if (SCHED_GROUP_P (insn))
594 return false;
596 if (IS_SPECULATION_CHECK_P (CONST_CAST_RTX (insn)))
597 return false;
599 if (side_effects_p (PATTERN (insn)))
600 return false;
602 if (ds & BE_IN_SPEC)
603 /* The following instructions, which depend on a speculatively scheduled
604 instruction, cannot be speculatively scheduled along. */
606 if (may_trap_or_fault_p (PATTERN (insn)))
607 /* If instruction might fault, it cannot be speculatively scheduled.
608 For control speculation it's obvious why and for data speculation
609 it's because the insn might get wrong input if speculation
610 wasn't successful. */
611 return false;
613 if ((ds & BE_IN_DATA)
614 && sched_has_condition_p (insn))
615 /* If this is a predicated instruction, then it cannot be
616 speculatively scheduled. See PR35659. */
617 return false;
620 return true;
623 /* Initialize LIST_PTR to point to one of the lists present in TYPES_PTR,
624 initialize RESOLVED_P_PTR with true if that list consists of resolved deps,
625 and remove the type of returned [through LIST_PTR] list from TYPES_PTR.
626 This function is used to switch sd_iterator to the next list.
627 !!! For internal use only. Might consider moving it to sched-int.h. */
628 void
629 sd_next_list (const_rtx insn, sd_list_types_def *types_ptr,
630 deps_list_t *list_ptr, bool *resolved_p_ptr)
632 sd_list_types_def types = *types_ptr;
634 if (types & SD_LIST_HARD_BACK)
636 *list_ptr = INSN_HARD_BACK_DEPS (insn);
637 *resolved_p_ptr = false;
638 *types_ptr = types & ~SD_LIST_HARD_BACK;
640 else if (types & SD_LIST_SPEC_BACK)
642 *list_ptr = INSN_SPEC_BACK_DEPS (insn);
643 *resolved_p_ptr = false;
644 *types_ptr = types & ~SD_LIST_SPEC_BACK;
646 else if (types & SD_LIST_FORW)
648 *list_ptr = INSN_FORW_DEPS (insn);
649 *resolved_p_ptr = false;
650 *types_ptr = types & ~SD_LIST_FORW;
652 else if (types & SD_LIST_RES_BACK)
654 *list_ptr = INSN_RESOLVED_BACK_DEPS (insn);
655 *resolved_p_ptr = true;
656 *types_ptr = types & ~SD_LIST_RES_BACK;
658 else if (types & SD_LIST_RES_FORW)
660 *list_ptr = INSN_RESOLVED_FORW_DEPS (insn);
661 *resolved_p_ptr = true;
662 *types_ptr = types & ~SD_LIST_RES_FORW;
664 else
666 *list_ptr = NULL;
667 *resolved_p_ptr = false;
668 *types_ptr = SD_LIST_NONE;
672 /* Return the summary size of INSN's lists defined by LIST_TYPES. */
674 sd_lists_size (const_rtx insn, sd_list_types_def list_types)
676 int size = 0;
678 while (list_types != SD_LIST_NONE)
680 deps_list_t list;
681 bool resolved_p;
683 sd_next_list (insn, &list_types, &list, &resolved_p);
684 if (list)
685 size += DEPS_LIST_N_LINKS (list);
688 return size;
691 /* Return true if INSN's lists defined by LIST_TYPES are all empty. */
693 bool
694 sd_lists_empty_p (const_rtx insn, sd_list_types_def list_types)
696 while (list_types != SD_LIST_NONE)
698 deps_list_t list;
699 bool resolved_p;
701 sd_next_list (insn, &list_types, &list, &resolved_p);
702 if (!deps_list_empty_p (list))
703 return false;
706 return true;
709 /* Initialize data for INSN. */
710 void
711 sd_init_insn (rtx insn)
713 INSN_HARD_BACK_DEPS (insn) = create_deps_list ();
714 INSN_SPEC_BACK_DEPS (insn) = create_deps_list ();
715 INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list ();
716 INSN_FORW_DEPS (insn) = create_deps_list ();
717 INSN_RESOLVED_FORW_DEPS (insn) = create_deps_list ();
719 /* ??? It would be nice to allocate dependency caches here. */
722 /* Free data for INSN. */
723 void
724 sd_finish_insn (rtx insn)
726 /* ??? It would be nice to deallocate dependency caches here. */
728 free_deps_list (INSN_HARD_BACK_DEPS (insn));
729 INSN_HARD_BACK_DEPS (insn) = NULL;
731 free_deps_list (INSN_SPEC_BACK_DEPS (insn));
732 INSN_SPEC_BACK_DEPS (insn) = NULL;
734 free_deps_list (INSN_RESOLVED_BACK_DEPS (insn));
735 INSN_RESOLVED_BACK_DEPS (insn) = NULL;
737 free_deps_list (INSN_FORW_DEPS (insn));
738 INSN_FORW_DEPS (insn) = NULL;
740 free_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
741 INSN_RESOLVED_FORW_DEPS (insn) = NULL;
744 /* Find a dependency between producer PRO and consumer CON.
745 Search through resolved dependency lists if RESOLVED_P is true.
746 If no such dependency is found return NULL,
747 otherwise return the dependency and initialize SD_IT_PTR [if it is nonnull]
748 with an iterator pointing to it. */
749 static dep_t
750 sd_find_dep_between_no_cache (rtx pro, rtx con, bool resolved_p,
751 sd_iterator_def *sd_it_ptr)
753 sd_list_types_def pro_list_type;
754 sd_list_types_def con_list_type;
755 sd_iterator_def sd_it;
756 dep_t dep;
757 bool found_p = false;
759 if (resolved_p)
761 pro_list_type = SD_LIST_RES_FORW;
762 con_list_type = SD_LIST_RES_BACK;
764 else
766 pro_list_type = SD_LIST_FORW;
767 con_list_type = SD_LIST_BACK;
770 /* Walk through either back list of INSN or forw list of ELEM
771 depending on which one is shorter. */
772 if (sd_lists_size (con, con_list_type) < sd_lists_size (pro, pro_list_type))
774 /* Find the dep_link with producer PRO in consumer's back_deps. */
775 FOR_EACH_DEP (con, con_list_type, sd_it, dep)
776 if (DEP_PRO (dep) == pro)
778 found_p = true;
779 break;
782 else
784 /* Find the dep_link with consumer CON in producer's forw_deps. */
785 FOR_EACH_DEP (pro, pro_list_type, sd_it, dep)
786 if (DEP_CON (dep) == con)
788 found_p = true;
789 break;
793 if (found_p)
795 if (sd_it_ptr != NULL)
796 *sd_it_ptr = sd_it;
798 return dep;
801 return NULL;
804 /* Find a dependency between producer PRO and consumer CON.
805 Use dependency [if available] to check if dependency is present at all.
806 Search through resolved dependency lists if RESOLVED_P is true.
807 If the dependency or NULL if none found. */
808 dep_t
809 sd_find_dep_between (rtx pro, rtx con, bool resolved_p)
811 if (true_dependency_cache != NULL)
812 /* Avoiding the list walk below can cut compile times dramatically
813 for some code. */
815 int elem_luid = INSN_LUID (pro);
816 int insn_luid = INSN_LUID (con);
818 gcc_assert (output_dependency_cache != NULL
819 && anti_dependency_cache != NULL);
821 if (!bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid)
822 && !bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid)
823 && !bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
824 return NULL;
827 return sd_find_dep_between_no_cache (pro, con, resolved_p, NULL);
830 /* Add or update a dependence described by DEP.
831 MEM1 and MEM2, if non-null, correspond to memory locations in case of
832 data speculation.
834 The function returns a value indicating if an old entry has been changed
835 or a new entry has been added to insn's backward deps.
837 This function merely checks if producer and consumer is the same insn
838 and doesn't create a dep in this case. Actual manipulation of
839 dependence data structures is performed in add_or_update_dep_1. */
840 static enum DEPS_ADJUST_RESULT
841 maybe_add_or_update_dep_1 (dep_t dep, bool resolved_p, rtx mem1, rtx mem2)
843 rtx elem = DEP_PRO (dep);
844 rtx insn = DEP_CON (dep);
846 gcc_assert (INSN_P (insn) && INSN_P (elem));
848 /* Don't depend an insn on itself. */
849 if (insn == elem)
851 if (sched_deps_info->generate_spec_deps)
852 /* INSN has an internal dependence, which we can't overcome. */
853 HAS_INTERNAL_DEP (insn) = 1;
855 return DEP_NODEP;
858 return add_or_update_dep_1 (dep, resolved_p, mem1, mem2);
861 /* Ask dependency caches what needs to be done for dependence DEP.
862 Return DEP_CREATED if new dependence should be created and there is no
863 need to try to find one searching the dependencies lists.
864 Return DEP_PRESENT if there already is a dependence described by DEP and
865 hence nothing is to be done.
866 Return DEP_CHANGED if there already is a dependence, but it should be
867 updated to incorporate additional information from DEP. */
868 static enum DEPS_ADJUST_RESULT
869 ask_dependency_caches (dep_t dep)
871 int elem_luid = INSN_LUID (DEP_PRO (dep));
872 int insn_luid = INSN_LUID (DEP_CON (dep));
874 gcc_assert (true_dependency_cache != NULL
875 && output_dependency_cache != NULL
876 && anti_dependency_cache != NULL);
878 if (!(current_sched_info->flags & USE_DEPS_LIST))
880 enum reg_note present_dep_type;
882 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
883 present_dep_type = REG_DEP_TRUE;
884 else if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
885 present_dep_type = REG_DEP_OUTPUT;
886 else if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
887 present_dep_type = REG_DEP_ANTI;
888 else
889 /* There is no existing dep so it should be created. */
890 return DEP_CREATED;
892 if ((int) DEP_TYPE (dep) >= (int) present_dep_type)
893 /* DEP does not add anything to the existing dependence. */
894 return DEP_PRESENT;
896 else
898 ds_t present_dep_types = 0;
900 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
901 present_dep_types |= DEP_TRUE;
902 if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
903 present_dep_types |= DEP_OUTPUT;
904 if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
905 present_dep_types |= DEP_ANTI;
907 if (present_dep_types == 0)
908 /* There is no existing dep so it should be created. */
909 return DEP_CREATED;
911 if (!(current_sched_info->flags & DO_SPECULATION)
912 || !bitmap_bit_p (&spec_dependency_cache[insn_luid], elem_luid))
914 if ((present_dep_types | (DEP_STATUS (dep) & DEP_TYPES))
915 == present_dep_types)
916 /* DEP does not add anything to the existing dependence. */
917 return DEP_PRESENT;
919 else
921 /* Only true dependencies can be data speculative and
922 only anti dependencies can be control speculative. */
923 gcc_assert ((present_dep_types & (DEP_TRUE | DEP_ANTI))
924 == present_dep_types);
926 /* if (DEP is SPECULATIVE) then
927 ..we should update DEP_STATUS
928 else
929 ..we should reset existing dep to non-speculative. */
933 return DEP_CHANGED;
936 /* Set dependency caches according to DEP. */
937 static void
938 set_dependency_caches (dep_t dep)
940 int elem_luid = INSN_LUID (DEP_PRO (dep));
941 int insn_luid = INSN_LUID (DEP_CON (dep));
943 if (!(current_sched_info->flags & USE_DEPS_LIST))
945 switch (DEP_TYPE (dep))
947 case REG_DEP_TRUE:
948 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
949 break;
951 case REG_DEP_OUTPUT:
952 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
953 break;
955 case REG_DEP_ANTI:
956 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
957 break;
959 default:
960 gcc_unreachable ();
963 else
965 ds_t ds = DEP_STATUS (dep);
967 if (ds & DEP_TRUE)
968 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
969 if (ds & DEP_OUTPUT)
970 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
971 if (ds & DEP_ANTI)
972 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
974 if (ds & SPECULATIVE)
976 gcc_assert (current_sched_info->flags & DO_SPECULATION);
977 bitmap_set_bit (&spec_dependency_cache[insn_luid], elem_luid);
982 /* Type of dependence DEP have changed from OLD_TYPE. Update dependency
983 caches accordingly. */
984 static void
985 update_dependency_caches (dep_t dep, enum reg_note old_type)
987 int elem_luid = INSN_LUID (DEP_PRO (dep));
988 int insn_luid = INSN_LUID (DEP_CON (dep));
990 /* Clear corresponding cache entry because type of the link
991 may have changed. Keep them if we use_deps_list. */
992 if (!(current_sched_info->flags & USE_DEPS_LIST))
994 switch (old_type)
996 case REG_DEP_OUTPUT:
997 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
998 break;
1000 case REG_DEP_ANTI:
1001 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1002 break;
1004 default:
1005 gcc_unreachable ();
1009 set_dependency_caches (dep);
1012 /* Convert a dependence pointed to by SD_IT to be non-speculative. */
1013 static void
1014 change_spec_dep_to_hard (sd_iterator_def sd_it)
1016 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1017 dep_link_t link = DEP_NODE_BACK (node);
1018 dep_t dep = DEP_NODE_DEP (node);
1019 rtx elem = DEP_PRO (dep);
1020 rtx insn = DEP_CON (dep);
1022 move_dep_link (link, INSN_SPEC_BACK_DEPS (insn), INSN_HARD_BACK_DEPS (insn));
1024 DEP_STATUS (dep) &= ~SPECULATIVE;
1026 if (true_dependency_cache != NULL)
1027 /* Clear the cache entry. */
1028 bitmap_clear_bit (&spec_dependency_cache[INSN_LUID (insn)],
1029 INSN_LUID (elem));
1032 /* Update DEP to incorporate information from NEW_DEP.
1033 SD_IT points to DEP in case it should be moved to another list.
1034 MEM1 and MEM2, if nonnull, correspond to memory locations in case if
1035 data-speculative dependence should be updated. */
1036 static enum DEPS_ADJUST_RESULT
1037 update_dep (dep_t dep, dep_t new_dep,
1038 sd_iterator_def sd_it ATTRIBUTE_UNUSED,
1039 rtx mem1 ATTRIBUTE_UNUSED,
1040 rtx mem2 ATTRIBUTE_UNUSED)
1042 enum DEPS_ADJUST_RESULT res = DEP_PRESENT;
1043 enum reg_note old_type = DEP_TYPE (dep);
1045 /* If this is a more restrictive type of dependence than the
1046 existing one, then change the existing dependence to this
1047 type. */
1048 if ((int) DEP_TYPE (new_dep) < (int) old_type)
1050 DEP_TYPE (dep) = DEP_TYPE (new_dep);
1051 res = DEP_CHANGED;
1054 if (current_sched_info->flags & USE_DEPS_LIST)
1055 /* Update DEP_STATUS. */
1057 ds_t dep_status = DEP_STATUS (dep);
1058 ds_t ds = DEP_STATUS (new_dep);
1059 ds_t new_status = ds | dep_status;
1061 if (new_status & SPECULATIVE)
1062 /* Either existing dep or a dep we're adding or both are
1063 speculative. */
1065 if (!(ds & SPECULATIVE)
1066 || !(dep_status & SPECULATIVE))
1067 /* The new dep can't be speculative. */
1069 new_status &= ~SPECULATIVE;
1071 if (dep_status & SPECULATIVE)
1072 /* The old dep was speculative, but now it
1073 isn't. */
1074 change_spec_dep_to_hard (sd_it);
1076 else
1078 /* Both are speculative. Merge probabilities. */
1079 if (mem1 != NULL)
1081 dw_t dw;
1083 dw = estimate_dep_weak (mem1, mem2);
1084 ds = set_dep_weak (ds, BEGIN_DATA, dw);
1087 new_status = ds_merge (dep_status, ds);
1091 ds = new_status;
1093 if (dep_status != ds)
1095 DEP_STATUS (dep) = ds;
1096 res = DEP_CHANGED;
1100 if (true_dependency_cache != NULL
1101 && res == DEP_CHANGED)
1102 update_dependency_caches (dep, old_type);
1104 return res;
1107 /* Add or update a dependence described by DEP.
1108 MEM1 and MEM2, if non-null, correspond to memory locations in case of
1109 data speculation.
1111 The function returns a value indicating if an old entry has been changed
1112 or a new entry has been added to insn's backward deps or nothing has
1113 been updated at all. */
1114 static enum DEPS_ADJUST_RESULT
1115 add_or_update_dep_1 (dep_t new_dep, bool resolved_p,
1116 rtx mem1 ATTRIBUTE_UNUSED, rtx mem2 ATTRIBUTE_UNUSED)
1118 bool maybe_present_p = true;
1119 bool present_p = false;
1121 gcc_assert (INSN_P (DEP_PRO (new_dep)) && INSN_P (DEP_CON (new_dep))
1122 && DEP_PRO (new_dep) != DEP_CON (new_dep));
1124 #ifdef ENABLE_CHECKING
1125 check_dep (new_dep, mem1 != NULL);
1126 #endif
1128 if (true_dependency_cache != NULL)
1130 switch (ask_dependency_caches (new_dep))
1132 case DEP_PRESENT:
1133 return DEP_PRESENT;
1135 case DEP_CHANGED:
1136 maybe_present_p = true;
1137 present_p = true;
1138 break;
1140 case DEP_CREATED:
1141 maybe_present_p = false;
1142 present_p = false;
1143 break;
1145 default:
1146 gcc_unreachable ();
1147 break;
1151 /* Check that we don't already have this dependence. */
1152 if (maybe_present_p)
1154 dep_t present_dep;
1155 sd_iterator_def sd_it;
1157 gcc_assert (true_dependency_cache == NULL || present_p);
1159 present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
1160 DEP_CON (new_dep),
1161 resolved_p, &sd_it);
1163 if (present_dep != NULL)
1164 /* We found an existing dependency between ELEM and INSN. */
1165 return update_dep (present_dep, new_dep, sd_it, mem1, mem2);
1166 else
1167 /* We didn't find a dep, it shouldn't present in the cache. */
1168 gcc_assert (!present_p);
1171 /* Might want to check one level of transitivity to save conses.
1172 This check should be done in maybe_add_or_update_dep_1.
1173 Since we made it to add_or_update_dep_1, we must create
1174 (or update) a link. */
1176 if (mem1 != NULL_RTX)
1178 gcc_assert (sched_deps_info->generate_spec_deps);
1179 DEP_STATUS (new_dep) = set_dep_weak (DEP_STATUS (new_dep), BEGIN_DATA,
1180 estimate_dep_weak (mem1, mem2));
1183 sd_add_dep (new_dep, resolved_p);
1185 return DEP_CREATED;
1188 /* Initialize BACK_LIST_PTR with consumer's backward list and
1189 FORW_LIST_PTR with producer's forward list. If RESOLVED_P is true
1190 initialize with lists that hold resolved deps. */
1191 static void
1192 get_back_and_forw_lists (dep_t dep, bool resolved_p,
1193 deps_list_t *back_list_ptr,
1194 deps_list_t *forw_list_ptr)
1196 rtx con = DEP_CON (dep);
1198 if (!resolved_p)
1200 if ((current_sched_info->flags & DO_SPECULATION)
1201 && (DEP_STATUS (dep) & SPECULATIVE))
1202 *back_list_ptr = INSN_SPEC_BACK_DEPS (con);
1203 else
1204 *back_list_ptr = INSN_HARD_BACK_DEPS (con);
1206 *forw_list_ptr = INSN_FORW_DEPS (DEP_PRO (dep));
1208 else
1210 *back_list_ptr = INSN_RESOLVED_BACK_DEPS (con);
1211 *forw_list_ptr = INSN_RESOLVED_FORW_DEPS (DEP_PRO (dep));
1215 /* Add dependence described by DEP.
1216 If RESOLVED_P is true treat the dependence as a resolved one. */
1217 void
1218 sd_add_dep (dep_t dep, bool resolved_p)
1220 dep_node_t n = create_dep_node ();
1221 deps_list_t con_back_deps;
1222 deps_list_t pro_forw_deps;
1223 rtx elem = DEP_PRO (dep);
1224 rtx insn = DEP_CON (dep);
1226 gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
1228 if ((current_sched_info->flags & DO_SPECULATION)
1229 && !sched_insn_is_legitimate_for_speculation_p (insn, DEP_STATUS (dep)))
1230 DEP_STATUS (dep) &= ~SPECULATIVE;
1232 copy_dep (DEP_NODE_DEP (n), dep);
1234 get_back_and_forw_lists (dep, resolved_p, &con_back_deps, &pro_forw_deps);
1236 add_to_deps_list (DEP_NODE_BACK (n), con_back_deps);
1238 #ifdef ENABLE_CHECKING
1239 check_dep (dep, false);
1240 #endif
1242 add_to_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1244 /* If we are adding a dependency to INSN's LOG_LINKs, then note that
1245 in the bitmap caches of dependency information. */
1246 if (true_dependency_cache != NULL)
1247 set_dependency_caches (dep);
1250 /* Add or update backward dependence between INSN and ELEM
1251 with given type DEP_TYPE and dep_status DS.
1252 This function is a convenience wrapper. */
1253 enum DEPS_ADJUST_RESULT
1254 sd_add_or_update_dep (dep_t dep, bool resolved_p)
1256 return add_or_update_dep_1 (dep, resolved_p, NULL_RTX, NULL_RTX);
1259 /* Resolved dependence pointed to by SD_IT.
1260 SD_IT will advance to the next element. */
1261 void
1262 sd_resolve_dep (sd_iterator_def sd_it)
1264 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1265 dep_t dep = DEP_NODE_DEP (node);
1266 rtx pro = DEP_PRO (dep);
1267 rtx con = DEP_CON (dep);
1269 if ((current_sched_info->flags & DO_SPECULATION)
1270 && (DEP_STATUS (dep) & SPECULATIVE))
1271 move_dep_link (DEP_NODE_BACK (node), INSN_SPEC_BACK_DEPS (con),
1272 INSN_RESOLVED_BACK_DEPS (con));
1273 else
1274 move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
1275 INSN_RESOLVED_BACK_DEPS (con));
1277 move_dep_link (DEP_NODE_FORW (node), INSN_FORW_DEPS (pro),
1278 INSN_RESOLVED_FORW_DEPS (pro));
1281 /* Make TO depend on all the FROM's producers.
1282 If RESOLVED_P is true add dependencies to the resolved lists. */
1283 void
1284 sd_copy_back_deps (rtx to, rtx from, bool resolved_p)
1286 sd_list_types_def list_type;
1287 sd_iterator_def sd_it;
1288 dep_t dep;
1290 list_type = resolved_p ? SD_LIST_RES_BACK : SD_LIST_BACK;
1292 FOR_EACH_DEP (from, list_type, sd_it, dep)
1294 dep_def _new_dep, *new_dep = &_new_dep;
1296 copy_dep (new_dep, dep);
1297 DEP_CON (new_dep) = to;
1298 sd_add_dep (new_dep, resolved_p);
1302 /* Remove a dependency referred to by SD_IT.
1303 SD_IT will point to the next dependence after removal. */
1304 void
1305 sd_delete_dep (sd_iterator_def sd_it)
1307 dep_node_t n = DEP_LINK_NODE (*sd_it.linkp);
1308 dep_t dep = DEP_NODE_DEP (n);
1309 rtx pro = DEP_PRO (dep);
1310 rtx con = DEP_CON (dep);
1311 deps_list_t con_back_deps;
1312 deps_list_t pro_forw_deps;
1314 if (true_dependency_cache != NULL)
1316 int elem_luid = INSN_LUID (pro);
1317 int insn_luid = INSN_LUID (con);
1319 bitmap_clear_bit (&true_dependency_cache[insn_luid], elem_luid);
1320 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1321 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1323 if (current_sched_info->flags & DO_SPECULATION)
1324 bitmap_clear_bit (&spec_dependency_cache[insn_luid], elem_luid);
1327 get_back_and_forw_lists (dep, sd_it.resolved_p,
1328 &con_back_deps, &pro_forw_deps);
1330 remove_from_deps_list (DEP_NODE_BACK (n), con_back_deps);
1331 remove_from_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1333 delete_dep_node (n);
1336 /* Dump size of the lists. */
1337 #define DUMP_LISTS_SIZE (2)
1339 /* Dump dependencies of the lists. */
1340 #define DUMP_LISTS_DEPS (4)
1342 /* Dump all information about the lists. */
1343 #define DUMP_LISTS_ALL (DUMP_LISTS_SIZE | DUMP_LISTS_DEPS)
1345 /* Dump deps_lists of INSN specified by TYPES to DUMP.
1346 FLAGS is a bit mask specifying what information about the lists needs
1347 to be printed.
1348 If FLAGS has the very first bit set, then dump all information about
1349 the lists and propagate this bit into the callee dump functions. */
1350 static void
1351 dump_lists (FILE *dump, rtx insn, sd_list_types_def types, int flags)
1353 sd_iterator_def sd_it;
1354 dep_t dep;
1355 int all;
1357 all = (flags & 1);
1359 if (all)
1360 flags |= DUMP_LISTS_ALL;
1362 fprintf (dump, "[");
1364 if (flags & DUMP_LISTS_SIZE)
1365 fprintf (dump, "%d; ", sd_lists_size (insn, types));
1367 if (flags & DUMP_LISTS_DEPS)
1369 FOR_EACH_DEP (insn, types, sd_it, dep)
1371 dump_dep (dump, dep, dump_dep_flags | all);
1372 fprintf (dump, " ");
1377 /* Dump all information about deps_lists of INSN specified by TYPES
1378 to STDERR. */
1379 void
1380 sd_debug_lists (rtx insn, sd_list_types_def types)
1382 dump_lists (stderr, insn, types, 1);
1383 fprintf (stderr, "\n");
1386 /* A convenience wrapper to operate on an entire list. */
1388 static void
1389 add_dependence_list (rtx insn, rtx list, int uncond, enum reg_note dep_type)
1391 for (; list; list = XEXP (list, 1))
1393 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1394 add_dependence (insn, XEXP (list, 0), dep_type);
1398 /* Similar, but free *LISTP at the same time, when the context
1399 is not readonly. */
1401 static void
1402 add_dependence_list_and_free (struct deps_desc *deps, rtx insn, rtx *listp,
1403 int uncond, enum reg_note dep_type)
1405 rtx list, next;
1407 /* We don't want to short-circuit dependencies involving debug
1408 insns, because they may cause actual dependencies to be
1409 disregarded. */
1410 if (deps->readonly || DEBUG_INSN_P (insn))
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_nondebug_insn (insn);
1521 if (BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (prev_nonnote)
1522 && ! sched_insns_conditions_mutex_p (insn, prev_nonnote))
1523 add_dependence (insn, prev_nonnote, REG_DEP_ANTI);
1526 /* Process an insn's memory dependencies. There are four kinds of
1527 dependencies:
1529 (0) read dependence: read follows read
1530 (1) true dependence: read follows write
1531 (2) output dependence: write follows write
1532 (3) anti dependence: write follows read
1534 We are careful to build only dependencies which actually exist, and
1535 use transitivity to avoid building too many links. */
1537 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1538 The MEM is a memory reference contained within INSN, which we are saving
1539 so that we can do memory aliasing on it. */
1541 static void
1542 add_insn_mem_dependence (struct deps_desc *deps, bool read_p,
1543 rtx insn, rtx mem)
1545 rtx *insn_list;
1546 rtx *mem_list;
1547 rtx link;
1549 gcc_assert (!deps->readonly);
1550 if (read_p)
1552 insn_list = &deps->pending_read_insns;
1553 mem_list = &deps->pending_read_mems;
1554 if (!DEBUG_INSN_P (insn))
1555 deps->pending_read_list_length++;
1557 else
1559 insn_list = &deps->pending_write_insns;
1560 mem_list = &deps->pending_write_mems;
1561 deps->pending_write_list_length++;
1564 link = alloc_INSN_LIST (insn, *insn_list);
1565 *insn_list = link;
1567 if (sched_deps_info->use_cselib)
1569 mem = shallow_copy_rtx (mem);
1570 XEXP (mem, 0) = cselib_subst_to_values (XEXP (mem, 0), GET_MODE (mem));
1572 link = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
1573 *mem_list = link;
1576 /* Make a dependency between every memory reference on the pending lists
1577 and INSN, thus flushing the pending lists. FOR_READ is true if emitting
1578 dependencies for a read operation, similarly with FOR_WRITE. */
1580 static void
1581 flush_pending_lists (struct deps_desc *deps, rtx insn, int for_read,
1582 int for_write)
1584 if (for_write)
1586 add_dependence_list_and_free (deps, insn, &deps->pending_read_insns,
1587 1, REG_DEP_ANTI);
1588 if (!deps->readonly)
1590 free_EXPR_LIST_list (&deps->pending_read_mems);
1591 deps->pending_read_list_length = 0;
1595 add_dependence_list_and_free (deps, insn, &deps->pending_write_insns, 1,
1596 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1598 add_dependence_list_and_free (deps, insn,
1599 &deps->last_pending_memory_flush, 1,
1600 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1601 if (!deps->readonly)
1603 free_EXPR_LIST_list (&deps->pending_write_mems);
1604 deps->pending_write_list_length = 0;
1606 deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
1607 deps->pending_flush_length = 1;
1611 /* Instruction which dependencies we are analyzing. */
1612 static rtx cur_insn = NULL_RTX;
1614 /* Implement hooks for haifa scheduler. */
1616 static void
1617 haifa_start_insn (rtx insn)
1619 gcc_assert (insn && !cur_insn);
1621 cur_insn = insn;
1624 static void
1625 haifa_finish_insn (void)
1627 cur_insn = NULL;
1630 void
1631 haifa_note_reg_set (int regno)
1633 SET_REGNO_REG_SET (reg_pending_sets, regno);
1636 void
1637 haifa_note_reg_clobber (int regno)
1639 SET_REGNO_REG_SET (reg_pending_clobbers, regno);
1642 void
1643 haifa_note_reg_use (int regno)
1645 SET_REGNO_REG_SET (reg_pending_uses, regno);
1648 static void
1649 haifa_note_mem_dep (rtx mem, rtx pending_mem, rtx pending_insn, ds_t ds)
1651 if (!(ds & SPECULATIVE))
1653 mem = NULL_RTX;
1654 pending_mem = NULL_RTX;
1656 else
1657 gcc_assert (ds & BEGIN_DATA);
1660 dep_def _dep, *dep = &_dep;
1662 init_dep_1 (dep, pending_insn, cur_insn, ds_to_dt (ds),
1663 current_sched_info->flags & USE_DEPS_LIST ? ds : -1);
1664 maybe_add_or_update_dep_1 (dep, false, pending_mem, mem);
1669 static void
1670 haifa_note_dep (rtx elem, ds_t ds)
1672 dep_def _dep;
1673 dep_t dep = &_dep;
1675 init_dep (dep, elem, cur_insn, ds_to_dt (ds));
1676 maybe_add_or_update_dep_1 (dep, false, NULL_RTX, NULL_RTX);
1679 static void
1680 note_reg_use (int r)
1682 if (sched_deps_info->note_reg_use)
1683 sched_deps_info->note_reg_use (r);
1686 static void
1687 note_reg_set (int r)
1689 if (sched_deps_info->note_reg_set)
1690 sched_deps_info->note_reg_set (r);
1693 static void
1694 note_reg_clobber (int r)
1696 if (sched_deps_info->note_reg_clobber)
1697 sched_deps_info->note_reg_clobber (r);
1700 static void
1701 note_mem_dep (rtx m1, rtx m2, rtx e, ds_t ds)
1703 if (sched_deps_info->note_mem_dep)
1704 sched_deps_info->note_mem_dep (m1, m2, e, ds);
1707 static void
1708 note_dep (rtx e, ds_t ds)
1710 if (sched_deps_info->note_dep)
1711 sched_deps_info->note_dep (e, ds);
1714 /* Return corresponding to DS reg_note. */
1715 enum reg_note
1716 ds_to_dt (ds_t ds)
1718 if (ds & DEP_TRUE)
1719 return REG_DEP_TRUE;
1720 else if (ds & DEP_OUTPUT)
1721 return REG_DEP_OUTPUT;
1722 else
1724 gcc_assert (ds & DEP_ANTI);
1725 return REG_DEP_ANTI;
1731 /* Functions for computation of info needed for register pressure
1732 sensitive insn scheduling. */
1735 /* Allocate and return reg_use_data structure for REGNO and INSN. */
1736 static struct reg_use_data *
1737 create_insn_reg_use (int regno, rtx insn)
1739 struct reg_use_data *use;
1741 use = (struct reg_use_data *) xmalloc (sizeof (struct reg_use_data));
1742 use->regno = regno;
1743 use->insn = insn;
1744 use->next_insn_use = INSN_REG_USE_LIST (insn);
1745 INSN_REG_USE_LIST (insn) = use;
1746 return use;
1749 /* Allocate and return reg_set_data structure for REGNO and INSN. */
1750 static struct reg_set_data *
1751 create_insn_reg_set (int regno, rtx insn)
1753 struct reg_set_data *set;
1755 set = (struct reg_set_data *) xmalloc (sizeof (struct reg_set_data));
1756 set->regno = regno;
1757 set->insn = insn;
1758 set->next_insn_set = INSN_REG_SET_LIST (insn);
1759 INSN_REG_SET_LIST (insn) = set;
1760 return set;
1763 /* Set up insn register uses for INSN and dependency context DEPS. */
1764 static void
1765 setup_insn_reg_uses (struct deps_desc *deps, rtx insn)
1767 unsigned i;
1768 reg_set_iterator rsi;
1769 rtx list;
1770 struct reg_use_data *use, *use2, *next;
1771 struct deps_reg *reg_last;
1773 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
1775 if (i < FIRST_PSEUDO_REGISTER
1776 && TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
1777 continue;
1779 if (find_regno_note (insn, REG_DEAD, i) == NULL_RTX
1780 && ! REGNO_REG_SET_P (reg_pending_sets, i)
1781 && ! REGNO_REG_SET_P (reg_pending_clobbers, i))
1782 /* Ignore use which is not dying. */
1783 continue;
1785 use = create_insn_reg_use (i, insn);
1786 use->next_regno_use = use;
1787 reg_last = &deps->reg_last[i];
1789 /* Create the cycle list of uses. */
1790 for (list = reg_last->uses; list; list = XEXP (list, 1))
1792 use2 = create_insn_reg_use (i, XEXP (list, 0));
1793 next = use->next_regno_use;
1794 use->next_regno_use = use2;
1795 use2->next_regno_use = next;
1800 /* Register pressure info for the currently processed insn. */
1801 static struct reg_pressure_data reg_pressure_info[N_REG_CLASSES];
1803 /* Return TRUE if INSN has the use structure for REGNO. */
1804 static bool
1805 insn_use_p (rtx insn, int regno)
1807 struct reg_use_data *use;
1809 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1810 if (use->regno == regno)
1811 return true;
1812 return false;
1815 /* Update the register pressure info after birth of pseudo register REGNO
1816 in INSN. Arguments CLOBBER_P and UNUSED_P say correspondingly that
1817 the register is in clobber or unused after the insn. */
1818 static void
1819 mark_insn_pseudo_birth (rtx insn, int regno, bool clobber_p, bool unused_p)
1821 int incr, new_incr;
1822 enum reg_class cl;
1824 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
1825 cl = sched_regno_pressure_class[regno];
1826 if (cl != NO_REGS)
1828 incr = ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (regno)];
1829 if (clobber_p)
1831 new_incr = reg_pressure_info[cl].clobber_increase + incr;
1832 reg_pressure_info[cl].clobber_increase = new_incr;
1834 else if (unused_p)
1836 new_incr = reg_pressure_info[cl].unused_set_increase + incr;
1837 reg_pressure_info[cl].unused_set_increase = new_incr;
1839 else
1841 new_incr = reg_pressure_info[cl].set_increase + incr;
1842 reg_pressure_info[cl].set_increase = new_incr;
1843 if (! insn_use_p (insn, regno))
1844 reg_pressure_info[cl].change += incr;
1845 create_insn_reg_set (regno, insn);
1847 gcc_assert (new_incr < (1 << INCREASE_BITS));
1851 /* Like mark_insn_pseudo_regno_birth except that NREGS saying how many
1852 hard registers involved in the birth. */
1853 static void
1854 mark_insn_hard_regno_birth (rtx insn, int regno, int nregs,
1855 bool clobber_p, bool unused_p)
1857 enum reg_class cl;
1858 int new_incr, last = regno + nregs;
1860 while (regno < last)
1862 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
1863 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
1865 cl = sched_regno_pressure_class[regno];
1866 if (cl != NO_REGS)
1868 if (clobber_p)
1870 new_incr = reg_pressure_info[cl].clobber_increase + 1;
1871 reg_pressure_info[cl].clobber_increase = new_incr;
1873 else if (unused_p)
1875 new_incr = reg_pressure_info[cl].unused_set_increase + 1;
1876 reg_pressure_info[cl].unused_set_increase = new_incr;
1878 else
1880 new_incr = reg_pressure_info[cl].set_increase + 1;
1881 reg_pressure_info[cl].set_increase = new_incr;
1882 if (! insn_use_p (insn, regno))
1883 reg_pressure_info[cl].change += 1;
1884 create_insn_reg_set (regno, insn);
1886 gcc_assert (new_incr < (1 << INCREASE_BITS));
1889 regno++;
1893 /* Update the register pressure info after birth of pseudo or hard
1894 register REG in INSN. Arguments CLOBBER_P and UNUSED_P say
1895 correspondingly that the register is in clobber or unused after the
1896 insn. */
1897 static void
1898 mark_insn_reg_birth (rtx insn, rtx reg, bool clobber_p, bool unused_p)
1900 int regno;
1902 if (GET_CODE (reg) == SUBREG)
1903 reg = SUBREG_REG (reg);
1905 if (! REG_P (reg))
1906 return;
1908 regno = REGNO (reg);
1909 if (regno < FIRST_PSEUDO_REGISTER)
1910 mark_insn_hard_regno_birth (insn, regno,
1911 hard_regno_nregs[regno][GET_MODE (reg)],
1912 clobber_p, unused_p);
1913 else
1914 mark_insn_pseudo_birth (insn, regno, clobber_p, unused_p);
1917 /* Update the register pressure info after death of pseudo register
1918 REGNO. */
1919 static void
1920 mark_pseudo_death (int regno)
1922 int incr;
1923 enum reg_class cl;
1925 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
1926 cl = sched_regno_pressure_class[regno];
1927 if (cl != NO_REGS)
1929 incr = ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (regno)];
1930 reg_pressure_info[cl].change -= incr;
1934 /* Like mark_pseudo_death except that NREGS saying how many hard
1935 registers involved in the death. */
1936 static void
1937 mark_hard_regno_death (int regno, int nregs)
1939 enum reg_class cl;
1940 int last = regno + nregs;
1942 while (regno < last)
1944 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
1945 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
1947 cl = sched_regno_pressure_class[regno];
1948 if (cl != NO_REGS)
1949 reg_pressure_info[cl].change -= 1;
1951 regno++;
1955 /* Update the register pressure info after death of pseudo or hard
1956 register REG. */
1957 static void
1958 mark_reg_death (rtx reg)
1960 int regno;
1962 if (GET_CODE (reg) == SUBREG)
1963 reg = SUBREG_REG (reg);
1965 if (! REG_P (reg))
1966 return;
1968 regno = REGNO (reg);
1969 if (regno < FIRST_PSEUDO_REGISTER)
1970 mark_hard_regno_death (regno, hard_regno_nregs[regno][GET_MODE (reg)]);
1971 else
1972 mark_pseudo_death (regno);
1975 /* Process SETTER of REG. DATA is an insn containing the setter. */
1976 static void
1977 mark_insn_reg_store (rtx reg, const_rtx setter, void *data)
1979 if (setter != NULL_RTX && GET_CODE (setter) != SET)
1980 return;
1981 mark_insn_reg_birth
1982 ((rtx) data, reg, false,
1983 find_reg_note ((const_rtx) data, REG_UNUSED, reg) != NULL_RTX);
1986 /* Like mark_insn_reg_store except notice just CLOBBERs; ignore SETs. */
1987 static void
1988 mark_insn_reg_clobber (rtx reg, const_rtx setter, void *data)
1990 if (GET_CODE (setter) == CLOBBER)
1991 mark_insn_reg_birth ((rtx) data, reg, true, false);
1994 /* Set up reg pressure info related to INSN. */
1995 void
1996 init_insn_reg_pressure_info (rtx insn)
1998 int i, len;
1999 enum reg_class cl;
2000 static struct reg_pressure_data *pressure_info;
2001 rtx link;
2003 gcc_assert (sched_pressure_p);
2005 if (! INSN_P (insn))
2006 return;
2008 for (i = 0; i < ira_pressure_classes_num; i++)
2010 cl = ira_pressure_classes[i];
2011 reg_pressure_info[cl].clobber_increase = 0;
2012 reg_pressure_info[cl].set_increase = 0;
2013 reg_pressure_info[cl].unused_set_increase = 0;
2014 reg_pressure_info[cl].change = 0;
2017 note_stores (PATTERN (insn), mark_insn_reg_clobber, insn);
2019 note_stores (PATTERN (insn), mark_insn_reg_store, insn);
2021 #ifdef AUTO_INC_DEC
2022 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2023 if (REG_NOTE_KIND (link) == REG_INC)
2024 mark_insn_reg_store (XEXP (link, 0), NULL_RTX, insn);
2025 #endif
2027 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2028 if (REG_NOTE_KIND (link) == REG_DEAD)
2029 mark_reg_death (XEXP (link, 0));
2031 len = sizeof (struct reg_pressure_data) * ira_pressure_classes_num;
2032 pressure_info
2033 = INSN_REG_PRESSURE (insn) = (struct reg_pressure_data *) xmalloc (len);
2034 INSN_MAX_REG_PRESSURE (insn) = (int *) xcalloc (ira_pressure_classes_num
2035 * sizeof (int), 1);
2036 for (i = 0; i < ira_pressure_classes_num; i++)
2038 cl = ira_pressure_classes[i];
2039 pressure_info[i].clobber_increase
2040 = reg_pressure_info[cl].clobber_increase;
2041 pressure_info[i].set_increase = reg_pressure_info[cl].set_increase;
2042 pressure_info[i].unused_set_increase
2043 = reg_pressure_info[cl].unused_set_increase;
2044 pressure_info[i].change = reg_pressure_info[cl].change;
2051 /* Internal variable for sched_analyze_[12] () functions.
2052 If it is nonzero, this means that sched_analyze_[12] looks
2053 at the most toplevel SET. */
2054 static bool can_start_lhs_rhs_p;
2056 /* Extend reg info for the deps context DEPS given that
2057 we have just generated a register numbered REGNO. */
2058 static void
2059 extend_deps_reg_info (struct deps_desc *deps, int regno)
2061 int max_regno = regno + 1;
2063 gcc_assert (!reload_completed);
2065 /* In a readonly context, it would not hurt to extend info,
2066 but it should not be needed. */
2067 if (reload_completed && deps->readonly)
2069 deps->max_reg = max_regno;
2070 return;
2073 if (max_regno > deps->max_reg)
2075 deps->reg_last = XRESIZEVEC (struct deps_reg, deps->reg_last,
2076 max_regno);
2077 memset (&deps->reg_last[deps->max_reg],
2078 0, (max_regno - deps->max_reg)
2079 * sizeof (struct deps_reg));
2080 deps->max_reg = max_regno;
2084 /* Extends REG_INFO_P if needed. */
2085 void
2086 maybe_extend_reg_info_p (void)
2088 /* Extend REG_INFO_P, if needed. */
2089 if ((unsigned int)max_regno - 1 >= reg_info_p_size)
2091 size_t new_reg_info_p_size = max_regno + 128;
2093 gcc_assert (!reload_completed && sel_sched_p ());
2095 reg_info_p = (struct reg_info_t *) xrecalloc (reg_info_p,
2096 new_reg_info_p_size,
2097 reg_info_p_size,
2098 sizeof (*reg_info_p));
2099 reg_info_p_size = new_reg_info_p_size;
2103 /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
2104 The type of the reference is specified by REF and can be SET,
2105 CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE. */
2107 static void
2108 sched_analyze_reg (struct deps_desc *deps, int regno, enum machine_mode mode,
2109 enum rtx_code ref, rtx insn)
2111 /* We could emit new pseudos in renaming. Extend the reg structures. */
2112 if (!reload_completed && sel_sched_p ()
2113 && (regno >= max_reg_num () - 1 || regno >= deps->max_reg))
2114 extend_deps_reg_info (deps, regno);
2116 maybe_extend_reg_info_p ();
2118 /* A hard reg in a wide mode may really be multiple registers.
2119 If so, mark all of them just like the first. */
2120 if (regno < FIRST_PSEUDO_REGISTER)
2122 int i = hard_regno_nregs[regno][mode];
2123 if (ref == SET)
2125 while (--i >= 0)
2126 note_reg_set (regno + i);
2128 else if (ref == USE)
2130 while (--i >= 0)
2131 note_reg_use (regno + i);
2133 else
2135 while (--i >= 0)
2136 note_reg_clobber (regno + i);
2140 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
2141 it does not reload. Ignore these as they have served their
2142 purpose already. */
2143 else if (regno >= deps->max_reg)
2145 enum rtx_code code = GET_CODE (PATTERN (insn));
2146 gcc_assert (code == USE || code == CLOBBER);
2149 else
2151 if (ref == SET)
2152 note_reg_set (regno);
2153 else if (ref == USE)
2154 note_reg_use (regno);
2155 else
2156 note_reg_clobber (regno);
2158 /* Pseudos that are REG_EQUIV to something may be replaced
2159 by that during reloading. We need only add dependencies for
2160 the address in the REG_EQUIV note. */
2161 if (!reload_completed && get_reg_known_equiv_p (regno))
2163 rtx t = get_reg_known_value (regno);
2164 if (MEM_P (t))
2165 sched_analyze_2 (deps, XEXP (t, 0), insn);
2168 /* Don't let it cross a call after scheduling if it doesn't
2169 already cross one. */
2170 if (REG_N_CALLS_CROSSED (regno) == 0)
2172 if (!deps->readonly && ref == USE && !DEBUG_INSN_P (insn))
2173 deps->sched_before_next_call
2174 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
2175 else
2176 add_dependence_list (insn, deps->last_function_call, 1,
2177 REG_DEP_ANTI);
2182 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
2183 rtx, X, creating all dependencies generated by the write to the
2184 destination of X, and reads of everything mentioned. */
2186 static void
2187 sched_analyze_1 (struct deps_desc *deps, rtx x, rtx insn)
2189 rtx dest = XEXP (x, 0);
2190 enum rtx_code code = GET_CODE (x);
2191 bool cslr_p = can_start_lhs_rhs_p;
2193 can_start_lhs_rhs_p = false;
2195 gcc_assert (dest);
2196 if (dest == 0)
2197 return;
2199 if (cslr_p && sched_deps_info->start_lhs)
2200 sched_deps_info->start_lhs (dest);
2202 if (GET_CODE (dest) == PARALLEL)
2204 int i;
2206 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
2207 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
2208 sched_analyze_1 (deps,
2209 gen_rtx_CLOBBER (VOIDmode,
2210 XEXP (XVECEXP (dest, 0, i), 0)),
2211 insn);
2213 if (cslr_p && sched_deps_info->finish_lhs)
2214 sched_deps_info->finish_lhs ();
2216 if (code == SET)
2218 can_start_lhs_rhs_p = cslr_p;
2220 sched_analyze_2 (deps, SET_SRC (x), insn);
2222 can_start_lhs_rhs_p = false;
2225 return;
2228 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
2229 || GET_CODE (dest) == ZERO_EXTRACT)
2231 if (GET_CODE (dest) == STRICT_LOW_PART
2232 || GET_CODE (dest) == ZERO_EXTRACT
2233 || df_read_modify_subreg_p (dest))
2235 /* These both read and modify the result. We must handle
2236 them as writes to get proper dependencies for following
2237 instructions. We must handle them as reads to get proper
2238 dependencies from this to previous instructions.
2239 Thus we need to call sched_analyze_2. */
2241 sched_analyze_2 (deps, XEXP (dest, 0), insn);
2243 if (GET_CODE (dest) == ZERO_EXTRACT)
2245 /* The second and third arguments are values read by this insn. */
2246 sched_analyze_2 (deps, XEXP (dest, 1), insn);
2247 sched_analyze_2 (deps, XEXP (dest, 2), insn);
2249 dest = XEXP (dest, 0);
2252 if (REG_P (dest))
2254 int regno = REGNO (dest);
2255 enum machine_mode mode = GET_MODE (dest);
2257 sched_analyze_reg (deps, regno, mode, code, insn);
2259 #ifdef STACK_REGS
2260 /* Treat all writes to a stack register as modifying the TOS. */
2261 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
2263 /* Avoid analyzing the same register twice. */
2264 if (regno != FIRST_STACK_REG)
2265 sched_analyze_reg (deps, FIRST_STACK_REG, mode, code, insn);
2267 add_to_hard_reg_set (&implicit_reg_pending_uses, mode,
2268 FIRST_STACK_REG);
2270 #endif
2272 else if (MEM_P (dest))
2274 /* Writing memory. */
2275 rtx t = dest;
2277 if (sched_deps_info->use_cselib)
2279 enum machine_mode address_mode
2280 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (dest));
2282 t = shallow_copy_rtx (dest);
2283 cselib_lookup_from_insn (XEXP (t, 0), address_mode, 1,
2284 GET_MODE (t), insn);
2285 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0), GET_MODE (t));
2287 t = canon_rtx (t);
2289 /* Pending lists can't get larger with a readonly context. */
2290 if (!deps->readonly
2291 && ((deps->pending_read_list_length + deps->pending_write_list_length)
2292 > MAX_PENDING_LIST_LENGTH))
2294 /* Flush all pending reads and writes to prevent the pending lists
2295 from getting any larger. Insn scheduling runs too slowly when
2296 these lists get long. When compiling GCC with itself,
2297 this flush occurs 8 times for sparc, and 10 times for m88k using
2298 the default value of 32. */
2299 flush_pending_lists (deps, insn, false, true);
2301 else
2303 rtx pending, pending_mem;
2305 pending = deps->pending_read_insns;
2306 pending_mem = deps->pending_read_mems;
2307 while (pending)
2309 if (anti_dependence (XEXP (pending_mem, 0), t)
2310 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2311 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2312 DEP_ANTI);
2314 pending = XEXP (pending, 1);
2315 pending_mem = XEXP (pending_mem, 1);
2318 pending = deps->pending_write_insns;
2319 pending_mem = deps->pending_write_mems;
2320 while (pending)
2322 if (output_dependence (XEXP (pending_mem, 0), t)
2323 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2324 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2325 DEP_OUTPUT);
2327 pending = XEXP (pending, 1);
2328 pending_mem = XEXP (pending_mem, 1);
2331 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
2332 REG_DEP_ANTI);
2334 if (!deps->readonly)
2335 add_insn_mem_dependence (deps, false, insn, dest);
2337 sched_analyze_2 (deps, XEXP (dest, 0), insn);
2340 if (cslr_p && sched_deps_info->finish_lhs)
2341 sched_deps_info->finish_lhs ();
2343 /* Analyze reads. */
2344 if (GET_CODE (x) == SET)
2346 can_start_lhs_rhs_p = cslr_p;
2348 sched_analyze_2 (deps, SET_SRC (x), insn);
2350 can_start_lhs_rhs_p = false;
2354 /* Analyze the uses of memory and registers in rtx X in INSN. */
2355 static void
2356 sched_analyze_2 (struct deps_desc *deps, rtx x, rtx insn)
2358 int i;
2359 int j;
2360 enum rtx_code code;
2361 const char *fmt;
2362 bool cslr_p = can_start_lhs_rhs_p;
2364 can_start_lhs_rhs_p = false;
2366 gcc_assert (x);
2367 if (x == 0)
2368 return;
2370 if (cslr_p && sched_deps_info->start_rhs)
2371 sched_deps_info->start_rhs (x);
2373 code = GET_CODE (x);
2375 switch (code)
2377 case CONST_INT:
2378 case CONST_DOUBLE:
2379 case CONST_FIXED:
2380 case CONST_VECTOR:
2381 case SYMBOL_REF:
2382 case CONST:
2383 case LABEL_REF:
2384 /* Ignore constants. */
2385 if (cslr_p && sched_deps_info->finish_rhs)
2386 sched_deps_info->finish_rhs ();
2388 return;
2390 #ifdef HAVE_cc0
2391 case CC0:
2392 /* User of CC0 depends on immediately preceding insn. */
2393 SCHED_GROUP_P (insn) = 1;
2394 /* Don't move CC0 setter to another block (it can set up the
2395 same flag for previous CC0 users which is safe). */
2396 CANT_MOVE (prev_nonnote_insn (insn)) = 1;
2398 if (cslr_p && sched_deps_info->finish_rhs)
2399 sched_deps_info->finish_rhs ();
2401 return;
2402 #endif
2404 case REG:
2406 int regno = REGNO (x);
2407 enum machine_mode mode = GET_MODE (x);
2409 sched_analyze_reg (deps, regno, mode, USE, insn);
2411 #ifdef STACK_REGS
2412 /* Treat all reads of a stack register as modifying the TOS. */
2413 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
2415 /* Avoid analyzing the same register twice. */
2416 if (regno != FIRST_STACK_REG)
2417 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
2418 sched_analyze_reg (deps, FIRST_STACK_REG, mode, SET, insn);
2420 #endif
2422 if (cslr_p && sched_deps_info->finish_rhs)
2423 sched_deps_info->finish_rhs ();
2425 return;
2428 case MEM:
2430 /* Reading memory. */
2431 rtx u;
2432 rtx pending, pending_mem;
2433 rtx t = x;
2435 if (sched_deps_info->use_cselib)
2437 enum machine_mode address_mode
2438 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (t));
2440 t = shallow_copy_rtx (t);
2441 cselib_lookup_from_insn (XEXP (t, 0), address_mode, 1,
2442 GET_MODE (t), insn);
2443 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0), GET_MODE (t));
2446 if (!DEBUG_INSN_P (insn))
2448 t = canon_rtx (t);
2449 pending = deps->pending_read_insns;
2450 pending_mem = deps->pending_read_mems;
2451 while (pending)
2453 if (read_dependence (XEXP (pending_mem, 0), t)
2454 && ! sched_insns_conditions_mutex_p (insn,
2455 XEXP (pending, 0)))
2456 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2457 DEP_ANTI);
2459 pending = XEXP (pending, 1);
2460 pending_mem = XEXP (pending_mem, 1);
2463 pending = deps->pending_write_insns;
2464 pending_mem = deps->pending_write_mems;
2465 while (pending)
2467 if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
2468 t, rtx_varies_p)
2469 && ! sched_insns_conditions_mutex_p (insn,
2470 XEXP (pending, 0)))
2471 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2472 sched_deps_info->generate_spec_deps
2473 ? BEGIN_DATA | DEP_TRUE : DEP_TRUE);
2475 pending = XEXP (pending, 1);
2476 pending_mem = XEXP (pending_mem, 1);
2479 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
2481 if (! NON_FLUSH_JUMP_P (u))
2482 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
2483 else if (deps_may_trap_p (x))
2485 if ((sched_deps_info->generate_spec_deps)
2486 && sel_sched_p () && (spec_info->mask & BEGIN_CONTROL))
2488 ds_t ds = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
2489 MAX_DEP_WEAK);
2491 note_dep (XEXP (u, 0), ds);
2493 else
2494 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
2499 /* Always add these dependencies to pending_reads, since
2500 this insn may be followed by a write. */
2501 if (!deps->readonly)
2502 add_insn_mem_dependence (deps, true, insn, x);
2504 sched_analyze_2 (deps, XEXP (x, 0), insn);
2506 if (cslr_p && sched_deps_info->finish_rhs)
2507 sched_deps_info->finish_rhs ();
2509 return;
2512 /* Force pending stores to memory in case a trap handler needs them. */
2513 case TRAP_IF:
2514 flush_pending_lists (deps, insn, true, false);
2515 break;
2517 case PREFETCH:
2518 if (PREFETCH_SCHEDULE_BARRIER_P (x))
2519 reg_pending_barrier = TRUE_BARRIER;
2520 break;
2522 case UNSPEC_VOLATILE:
2523 flush_pending_lists (deps, insn, true, true);
2524 /* FALLTHRU */
2526 case ASM_OPERANDS:
2527 case ASM_INPUT:
2529 /* Traditional and volatile asm instructions must be considered to use
2530 and clobber all hard registers, all pseudo-registers and all of
2531 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
2533 Consider for instance a volatile asm that changes the fpu rounding
2534 mode. An insn should not be moved across this even if it only uses
2535 pseudo-regs because it might give an incorrectly rounded result. */
2536 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
2537 reg_pending_barrier = TRUE_BARRIER;
2539 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
2540 We can not just fall through here since then we would be confused
2541 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
2542 traditional asms unlike their normal usage. */
2544 if (code == ASM_OPERANDS)
2546 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
2547 sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
2549 if (cslr_p && sched_deps_info->finish_rhs)
2550 sched_deps_info->finish_rhs ();
2552 return;
2554 break;
2557 case PRE_DEC:
2558 case POST_DEC:
2559 case PRE_INC:
2560 case POST_INC:
2561 /* These both read and modify the result. We must handle them as writes
2562 to get proper dependencies for following instructions. We must handle
2563 them as reads to get proper dependencies from this to previous
2564 instructions. Thus we need to pass them to both sched_analyze_1
2565 and sched_analyze_2. We must call sched_analyze_2 first in order
2566 to get the proper antecedent for the read. */
2567 sched_analyze_2 (deps, XEXP (x, 0), insn);
2568 sched_analyze_1 (deps, x, insn);
2570 if (cslr_p && sched_deps_info->finish_rhs)
2571 sched_deps_info->finish_rhs ();
2573 return;
2575 case POST_MODIFY:
2576 case PRE_MODIFY:
2577 /* op0 = op0 + op1 */
2578 sched_analyze_2 (deps, XEXP (x, 0), insn);
2579 sched_analyze_2 (deps, XEXP (x, 1), insn);
2580 sched_analyze_1 (deps, x, insn);
2582 if (cslr_p && sched_deps_info->finish_rhs)
2583 sched_deps_info->finish_rhs ();
2585 return;
2587 default:
2588 break;
2591 /* Other cases: walk the insn. */
2592 fmt = GET_RTX_FORMAT (code);
2593 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2595 if (fmt[i] == 'e')
2596 sched_analyze_2 (deps, XEXP (x, i), insn);
2597 else if (fmt[i] == 'E')
2598 for (j = 0; j < XVECLEN (x, i); j++)
2599 sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
2602 if (cslr_p && sched_deps_info->finish_rhs)
2603 sched_deps_info->finish_rhs ();
2606 /* Analyze an INSN with pattern X to find all dependencies. */
2607 static void
2608 sched_analyze_insn (struct deps_desc *deps, rtx x, rtx insn)
2610 RTX_CODE code = GET_CODE (x);
2611 rtx link;
2612 unsigned i;
2613 reg_set_iterator rsi;
2615 if (! reload_completed)
2617 HARD_REG_SET temp;
2619 extract_insn (insn);
2620 preprocess_constraints ();
2621 ira_implicitly_set_insn_hard_regs (&temp);
2622 AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
2623 IOR_HARD_REG_SET (implicit_reg_pending_clobbers, temp);
2626 can_start_lhs_rhs_p = (NONJUMP_INSN_P (insn)
2627 && code == SET);
2629 if (may_trap_p (x))
2630 /* Avoid moving trapping instructions accross function calls that might
2631 not always return. */
2632 add_dependence_list (insn, deps->last_function_call_may_noreturn,
2633 1, REG_DEP_ANTI);
2635 if (code == COND_EXEC)
2637 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
2639 /* ??? Should be recording conditions so we reduce the number of
2640 false dependencies. */
2641 x = COND_EXEC_CODE (x);
2642 code = GET_CODE (x);
2644 if (code == SET || code == CLOBBER)
2646 sched_analyze_1 (deps, x, insn);
2648 /* Bare clobber insns are used for letting life analysis, reg-stack
2649 and others know that a value is dead. Depend on the last call
2650 instruction so that reg-stack won't get confused. */
2651 if (code == CLOBBER)
2652 add_dependence_list (insn, deps->last_function_call, 1,
2653 REG_DEP_OUTPUT);
2655 else if (code == PARALLEL)
2657 for (i = XVECLEN (x, 0); i--;)
2659 rtx sub = XVECEXP (x, 0, i);
2660 code = GET_CODE (sub);
2662 if (code == COND_EXEC)
2664 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
2665 sub = COND_EXEC_CODE (sub);
2666 code = GET_CODE (sub);
2668 if (code == SET || code == CLOBBER)
2669 sched_analyze_1 (deps, sub, insn);
2670 else
2671 sched_analyze_2 (deps, sub, insn);
2674 else
2675 sched_analyze_2 (deps, x, insn);
2677 /* Mark registers CLOBBERED or used by called function. */
2678 if (CALL_P (insn))
2680 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2682 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
2683 sched_analyze_1 (deps, XEXP (link, 0), insn);
2684 else
2685 sched_analyze_2 (deps, XEXP (link, 0), insn);
2687 if (find_reg_note (insn, REG_SETJMP, NULL))
2688 reg_pending_barrier = MOVE_BARRIER;
2691 if (JUMP_P (insn))
2693 rtx next;
2694 next = next_nonnote_nondebug_insn (insn);
2695 if (next && BARRIER_P (next))
2696 reg_pending_barrier = MOVE_BARRIER;
2697 else
2699 rtx pending, pending_mem;
2701 if (sched_deps_info->compute_jump_reg_dependencies)
2703 regset_head tmp_uses, tmp_sets;
2704 INIT_REG_SET (&tmp_uses);
2705 INIT_REG_SET (&tmp_sets);
2707 (*sched_deps_info->compute_jump_reg_dependencies)
2708 (insn, &deps->reg_conditional_sets, &tmp_uses, &tmp_sets);
2709 /* Make latency of jump equal to 0 by using anti-dependence. */
2710 EXECUTE_IF_SET_IN_REG_SET (&tmp_uses, 0, i, rsi)
2712 struct deps_reg *reg_last = &deps->reg_last[i];
2713 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI);
2714 add_dependence_list (insn, reg_last->implicit_sets,
2715 0, REG_DEP_ANTI);
2716 add_dependence_list (insn, reg_last->clobbers, 0,
2717 REG_DEP_ANTI);
2719 if (!deps->readonly)
2721 reg_last->uses_length++;
2722 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2725 IOR_REG_SET (reg_pending_sets, &tmp_sets);
2727 CLEAR_REG_SET (&tmp_uses);
2728 CLEAR_REG_SET (&tmp_sets);
2731 /* All memory writes and volatile reads must happen before the
2732 jump. Non-volatile reads must happen before the jump iff
2733 the result is needed by the above register used mask. */
2735 pending = deps->pending_write_insns;
2736 pending_mem = deps->pending_write_mems;
2737 while (pending)
2739 if (! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2740 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
2741 pending = XEXP (pending, 1);
2742 pending_mem = XEXP (pending_mem, 1);
2745 pending = deps->pending_read_insns;
2746 pending_mem = deps->pending_read_mems;
2747 while (pending)
2749 if (MEM_VOLATILE_P (XEXP (pending_mem, 0))
2750 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2751 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
2752 pending = XEXP (pending, 1);
2753 pending_mem = XEXP (pending_mem, 1);
2756 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
2757 REG_DEP_ANTI);
2761 /* If this instruction can throw an exception, then moving it changes
2762 where block boundaries fall. This is mighty confusing elsewhere.
2763 Therefore, prevent such an instruction from being moved. Same for
2764 non-jump instructions that define block boundaries.
2765 ??? Unclear whether this is still necessary in EBB mode. If not,
2766 add_branch_dependences should be adjusted for RGN mode instead. */
2767 if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
2768 || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
2769 reg_pending_barrier = MOVE_BARRIER;
2771 if (sched_pressure_p)
2773 setup_insn_reg_uses (deps, insn);
2774 init_insn_reg_pressure_info (insn);
2777 /* Add register dependencies for insn. */
2778 if (DEBUG_INSN_P (insn))
2780 rtx prev = deps->last_debug_insn;
2781 rtx u;
2783 if (!deps->readonly)
2784 deps->last_debug_insn = insn;
2786 if (prev)
2787 add_dependence (insn, prev, REG_DEP_ANTI);
2789 add_dependence_list (insn, deps->last_function_call, 1,
2790 REG_DEP_ANTI);
2792 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
2793 if (! NON_FLUSH_JUMP_P (u) || !sel_sched_p ())
2794 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
2796 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
2798 struct deps_reg *reg_last = &deps->reg_last[i];
2799 add_dependence_list (insn, reg_last->sets, 1, REG_DEP_ANTI);
2800 add_dependence_list (insn, reg_last->clobbers, 1, REG_DEP_ANTI);
2802 if (!deps->readonly)
2803 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2805 CLEAR_REG_SET (reg_pending_uses);
2807 /* Quite often, a debug insn will refer to stuff in the
2808 previous instruction, but the reason we want this
2809 dependency here is to make sure the scheduler doesn't
2810 gratuitously move a debug insn ahead. This could dirty
2811 DF flags and cause additional analysis that wouldn't have
2812 occurred in compilation without debug insns, and such
2813 additional analysis can modify the generated code. */
2814 prev = PREV_INSN (insn);
2816 if (prev && NONDEBUG_INSN_P (prev))
2817 add_dependence (insn, prev, REG_DEP_ANTI);
2819 else
2821 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
2823 struct deps_reg *reg_last = &deps->reg_last[i];
2824 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
2825 add_dependence_list (insn, reg_last->implicit_sets, 0, REG_DEP_ANTI);
2826 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
2828 if (!deps->readonly)
2830 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2831 reg_last->uses_length++;
2835 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2836 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i))
2838 struct deps_reg *reg_last = &deps->reg_last[i];
2839 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
2840 add_dependence_list (insn, reg_last->implicit_sets, 0,
2841 REG_DEP_ANTI);
2842 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
2844 if (!deps->readonly)
2846 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2847 reg_last->uses_length++;
2851 /* If the current insn is conditional, we can't free any
2852 of the lists. */
2853 if (sched_has_condition_p (insn))
2855 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
2857 struct deps_reg *reg_last = &deps->reg_last[i];
2858 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2859 add_dependence_list (insn, reg_last->implicit_sets, 0,
2860 REG_DEP_ANTI);
2861 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2863 if (!deps->readonly)
2865 reg_last->clobbers
2866 = alloc_INSN_LIST (insn, reg_last->clobbers);
2867 reg_last->clobbers_length++;
2870 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
2872 struct deps_reg *reg_last = &deps->reg_last[i];
2873 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2874 add_dependence_list (insn, reg_last->implicit_sets, 0,
2875 REG_DEP_ANTI);
2876 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT);
2877 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2879 if (!deps->readonly)
2881 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2882 SET_REGNO_REG_SET (&deps->reg_conditional_sets, i);
2886 else
2888 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
2890 struct deps_reg *reg_last = &deps->reg_last[i];
2891 if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
2892 || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
2894 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
2895 REG_DEP_OUTPUT);
2896 add_dependence_list_and_free (deps, insn,
2897 &reg_last->implicit_sets, 0,
2898 REG_DEP_ANTI);
2899 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
2900 REG_DEP_ANTI);
2901 add_dependence_list_and_free
2902 (deps, insn, &reg_last->clobbers, 0, REG_DEP_OUTPUT);
2904 if (!deps->readonly)
2906 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2907 reg_last->clobbers_length = 0;
2908 reg_last->uses_length = 0;
2911 else
2913 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2914 add_dependence_list (insn, reg_last->implicit_sets, 0,
2915 REG_DEP_ANTI);
2916 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2919 if (!deps->readonly)
2921 reg_last->clobbers_length++;
2922 reg_last->clobbers
2923 = alloc_INSN_LIST (insn, reg_last->clobbers);
2926 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
2928 struct deps_reg *reg_last = &deps->reg_last[i];
2930 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
2931 REG_DEP_OUTPUT);
2932 add_dependence_list_and_free (deps, insn,
2933 &reg_last->implicit_sets,
2934 0, REG_DEP_ANTI);
2935 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
2936 REG_DEP_OUTPUT);
2937 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
2938 REG_DEP_ANTI);
2940 if (!deps->readonly)
2942 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2943 reg_last->uses_length = 0;
2944 reg_last->clobbers_length = 0;
2945 CLEAR_REGNO_REG_SET (&deps->reg_conditional_sets, i);
2951 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2952 if (TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
2954 struct deps_reg *reg_last = &deps->reg_last[i];
2955 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI);
2956 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI);
2957 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2959 if (!deps->readonly)
2960 reg_last->implicit_sets
2961 = alloc_INSN_LIST (insn, reg_last->implicit_sets);
2964 if (!deps->readonly)
2966 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
2967 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
2968 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
2969 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2970 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i)
2971 || TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
2972 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
2974 /* Set up the pending barrier found. */
2975 deps->last_reg_pending_barrier = reg_pending_barrier;
2978 CLEAR_REG_SET (reg_pending_uses);
2979 CLEAR_REG_SET (reg_pending_clobbers);
2980 CLEAR_REG_SET (reg_pending_sets);
2981 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
2982 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
2984 /* Add dependencies if a scheduling barrier was found. */
2985 if (reg_pending_barrier)
2987 /* In the case of barrier the most added dependencies are not
2988 real, so we use anti-dependence here. */
2989 if (sched_has_condition_p (insn))
2991 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2993 struct deps_reg *reg_last = &deps->reg_last[i];
2994 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2995 add_dependence_list (insn, reg_last->sets, 0,
2996 reg_pending_barrier == TRUE_BARRIER
2997 ? REG_DEP_TRUE : REG_DEP_ANTI);
2998 add_dependence_list (insn, reg_last->implicit_sets, 0,
2999 REG_DEP_ANTI);
3000 add_dependence_list (insn, reg_last->clobbers, 0,
3001 reg_pending_barrier == TRUE_BARRIER
3002 ? REG_DEP_TRUE : REG_DEP_ANTI);
3005 else
3007 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3009 struct deps_reg *reg_last = &deps->reg_last[i];
3010 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3011 REG_DEP_ANTI);
3012 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3013 reg_pending_barrier == TRUE_BARRIER
3014 ? REG_DEP_TRUE : REG_DEP_ANTI);
3015 add_dependence_list_and_free (deps, insn,
3016 &reg_last->implicit_sets, 0,
3017 REG_DEP_ANTI);
3018 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
3019 reg_pending_barrier == TRUE_BARRIER
3020 ? REG_DEP_TRUE : REG_DEP_ANTI);
3022 if (!deps->readonly)
3024 reg_last->uses_length = 0;
3025 reg_last->clobbers_length = 0;
3030 if (!deps->readonly)
3031 for (i = 0; i < (unsigned)deps->max_reg; i++)
3033 struct deps_reg *reg_last = &deps->reg_last[i];
3034 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3035 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
3038 /* Flush pending lists on jumps, but not on speculative checks. */
3039 if (JUMP_P (insn) && !(sel_sched_p ()
3040 && sel_insn_is_speculation_check (insn)))
3041 flush_pending_lists (deps, insn, true, true);
3043 if (!deps->readonly)
3044 CLEAR_REG_SET (&deps->reg_conditional_sets);
3045 reg_pending_barrier = NOT_A_BARRIER;
3048 /* If a post-call group is still open, see if it should remain so.
3049 This insn must be a simple move of a hard reg to a pseudo or
3050 vice-versa.
3052 We must avoid moving these insns for correctness on targets
3053 with small register classes, and for special registers like
3054 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
3055 hard regs for all targets. */
3057 if (deps->in_post_call_group_p)
3059 rtx tmp, set = single_set (insn);
3060 int src_regno, dest_regno;
3062 if (set == NULL)
3064 if (DEBUG_INSN_P (insn))
3065 /* We don't want to mark debug insns as part of the same
3066 sched group. We know they really aren't, but if we use
3067 debug insns to tell that a call group is over, we'll
3068 get different code if debug insns are not there and
3069 instructions that follow seem like they should be part
3070 of the call group.
3072 Also, if we did, fixup_sched_groups() would move the
3073 deps of the debug insn to the call insn, modifying
3074 non-debug post-dependency counts of the debug insn
3075 dependencies and otherwise messing with the scheduling
3076 order.
3078 Instead, let such debug insns be scheduled freely, but
3079 keep the call group open in case there are insns that
3080 should be part of it afterwards. Since we grant debug
3081 insns higher priority than even sched group insns, it
3082 will all turn out all right. */
3083 goto debug_dont_end_call_group;
3084 else
3085 goto end_call_group;
3088 tmp = SET_DEST (set);
3089 if (GET_CODE (tmp) == SUBREG)
3090 tmp = SUBREG_REG (tmp);
3091 if (REG_P (tmp))
3092 dest_regno = REGNO (tmp);
3093 else
3094 goto end_call_group;
3096 tmp = SET_SRC (set);
3097 if (GET_CODE (tmp) == SUBREG)
3098 tmp = SUBREG_REG (tmp);
3099 if ((GET_CODE (tmp) == PLUS
3100 || GET_CODE (tmp) == MINUS)
3101 && REG_P (XEXP (tmp, 0))
3102 && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
3103 && dest_regno == STACK_POINTER_REGNUM)
3104 src_regno = STACK_POINTER_REGNUM;
3105 else if (REG_P (tmp))
3106 src_regno = REGNO (tmp);
3107 else
3108 goto end_call_group;
3110 if (src_regno < FIRST_PSEUDO_REGISTER
3111 || dest_regno < FIRST_PSEUDO_REGISTER)
3113 if (!deps->readonly
3114 && deps->in_post_call_group_p == post_call_initial)
3115 deps->in_post_call_group_p = post_call;
3117 if (!sel_sched_p () || sched_emulate_haifa_p)
3119 SCHED_GROUP_P (insn) = 1;
3120 CANT_MOVE (insn) = 1;
3123 else
3125 end_call_group:
3126 if (!deps->readonly)
3127 deps->in_post_call_group_p = not_post_call;
3131 debug_dont_end_call_group:
3132 if ((current_sched_info->flags & DO_SPECULATION)
3133 && !sched_insn_is_legitimate_for_speculation_p (insn, 0))
3134 /* INSN has an internal dependency (e.g. r14 = [r14]) and thus cannot
3135 be speculated. */
3137 if (sel_sched_p ())
3138 sel_mark_hard_insn (insn);
3139 else
3141 sd_iterator_def sd_it;
3142 dep_t dep;
3144 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3145 sd_iterator_cond (&sd_it, &dep);)
3146 change_spec_dep_to_hard (sd_it);
3151 /* Return TRUE if INSN might not always return normally (e.g. call exit,
3152 longjmp, loop forever, ...). */
3153 static bool
3154 call_may_noreturn_p (rtx insn)
3156 rtx call;
3158 /* const or pure calls that aren't looping will always return. */
3159 if (RTL_CONST_OR_PURE_CALL_P (insn)
3160 && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn))
3161 return false;
3163 call = PATTERN (insn);
3164 if (GET_CODE (call) == PARALLEL)
3165 call = XVECEXP (call, 0, 0);
3166 if (GET_CODE (call) == SET)
3167 call = SET_SRC (call);
3168 if (GET_CODE (call) == CALL
3169 && MEM_P (XEXP (call, 0))
3170 && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
3172 rtx symbol = XEXP (XEXP (call, 0), 0);
3173 if (SYMBOL_REF_DECL (symbol)
3174 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
3176 if (DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
3177 == BUILT_IN_NORMAL)
3178 switch (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol)))
3180 case BUILT_IN_BCMP:
3181 case BUILT_IN_BCOPY:
3182 case BUILT_IN_BZERO:
3183 case BUILT_IN_INDEX:
3184 case BUILT_IN_MEMCHR:
3185 case BUILT_IN_MEMCMP:
3186 case BUILT_IN_MEMCPY:
3187 case BUILT_IN_MEMMOVE:
3188 case BUILT_IN_MEMPCPY:
3189 case BUILT_IN_MEMSET:
3190 case BUILT_IN_RINDEX:
3191 case BUILT_IN_STPCPY:
3192 case BUILT_IN_STPNCPY:
3193 case BUILT_IN_STRCAT:
3194 case BUILT_IN_STRCHR:
3195 case BUILT_IN_STRCMP:
3196 case BUILT_IN_STRCPY:
3197 case BUILT_IN_STRCSPN:
3198 case BUILT_IN_STRLEN:
3199 case BUILT_IN_STRNCAT:
3200 case BUILT_IN_STRNCMP:
3201 case BUILT_IN_STRNCPY:
3202 case BUILT_IN_STRPBRK:
3203 case BUILT_IN_STRRCHR:
3204 case BUILT_IN_STRSPN:
3205 case BUILT_IN_STRSTR:
3206 /* Assume certain string/memory builtins always return. */
3207 return false;
3208 default:
3209 break;
3214 /* For all other calls assume that they might not always return. */
3215 return true;
3218 /* Analyze INSN with DEPS as a context. */
3219 void
3220 deps_analyze_insn (struct deps_desc *deps, rtx insn)
3222 if (sched_deps_info->start_insn)
3223 sched_deps_info->start_insn (insn);
3225 if (NONJUMP_INSN_P (insn) || DEBUG_INSN_P (insn) || JUMP_P (insn))
3227 /* Make each JUMP_INSN (but not a speculative check)
3228 a scheduling barrier for memory references. */
3229 if (!deps->readonly
3230 && JUMP_P (insn)
3231 && !(sel_sched_p ()
3232 && sel_insn_is_speculation_check (insn)))
3234 /* Keep the list a reasonable size. */
3235 if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
3236 flush_pending_lists (deps, insn, true, true);
3237 else
3239 deps->last_pending_memory_flush
3240 = alloc_INSN_LIST (insn, deps->last_pending_memory_flush);
3241 /* Signal to sched_analyze_insn that this jump stands
3242 just for its own, not any other pending memory
3243 reads/writes flush_pending_lists had to flush. */
3244 PUT_REG_NOTE_KIND (deps->last_pending_memory_flush,
3245 NON_FLUSH_JUMP_KIND);
3249 sched_analyze_insn (deps, PATTERN (insn), insn);
3251 else if (CALL_P (insn))
3253 int i;
3255 CANT_MOVE (insn) = 1;
3257 if (find_reg_note (insn, REG_SETJMP, NULL))
3259 /* This is setjmp. Assume that all registers, not just
3260 hard registers, may be clobbered by this call. */
3261 reg_pending_barrier = MOVE_BARRIER;
3263 else
3265 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3266 /* A call may read and modify global register variables. */
3267 if (global_regs[i])
3269 SET_REGNO_REG_SET (reg_pending_sets, i);
3270 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3272 /* Other call-clobbered hard regs may be clobbered.
3273 Since we only have a choice between 'might be clobbered'
3274 and 'definitely not clobbered', we must include all
3275 partly call-clobbered registers here. */
3276 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
3277 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
3278 SET_REGNO_REG_SET (reg_pending_clobbers, i);
3279 /* We don't know what set of fixed registers might be used
3280 by the function, but it is certain that the stack pointer
3281 is among them, but be conservative. */
3282 else if (fixed_regs[i])
3283 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3284 /* The frame pointer is normally not used by the function
3285 itself, but by the debugger. */
3286 /* ??? MIPS o32 is an exception. It uses the frame pointer
3287 in the macro expansion of jal but does not represent this
3288 fact in the call_insn rtl. */
3289 else if (i == FRAME_POINTER_REGNUM
3290 || (i == HARD_FRAME_POINTER_REGNUM
3291 && (! reload_completed || frame_pointer_needed)))
3292 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3295 /* For each insn which shouldn't cross a call, add a dependence
3296 between that insn and this call insn. */
3297 add_dependence_list_and_free (deps, insn,
3298 &deps->sched_before_next_call, 1,
3299 REG_DEP_ANTI);
3301 sched_analyze_insn (deps, PATTERN (insn), insn);
3303 /* If CALL would be in a sched group, then this will violate
3304 convention that sched group insns have dependencies only on the
3305 previous instruction.
3307 Of course one can say: "Hey! What about head of the sched group?"
3308 And I will answer: "Basic principles (one dep per insn) are always
3309 the same." */
3310 gcc_assert (!SCHED_GROUP_P (insn));
3312 /* In the absence of interprocedural alias analysis, we must flush
3313 all pending reads and writes, and start new dependencies starting
3314 from here. But only flush writes for constant calls (which may
3315 be passed a pointer to something we haven't written yet). */
3316 flush_pending_lists (deps, insn, true, ! RTL_CONST_OR_PURE_CALL_P (insn));
3318 if (!deps->readonly)
3320 /* Remember the last function call for limiting lifetimes. */
3321 free_INSN_LIST_list (&deps->last_function_call);
3322 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
3324 if (call_may_noreturn_p (insn))
3326 /* Remember the last function call that might not always return
3327 normally for limiting moves of trapping insns. */
3328 free_INSN_LIST_list (&deps->last_function_call_may_noreturn);
3329 deps->last_function_call_may_noreturn
3330 = alloc_INSN_LIST (insn, NULL_RTX);
3333 /* Before reload, begin a post-call group, so as to keep the
3334 lifetimes of hard registers correct. */
3335 if (! reload_completed)
3336 deps->in_post_call_group_p = post_call;
3340 if (sched_deps_info->use_cselib)
3341 cselib_process_insn (insn);
3343 /* EH_REGION insn notes can not appear until well after we complete
3344 scheduling. */
3345 if (NOTE_P (insn))
3346 gcc_assert (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
3347 && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END);
3349 if (sched_deps_info->finish_insn)
3350 sched_deps_info->finish_insn ();
3352 /* Fixup the dependencies in the sched group. */
3353 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
3354 && SCHED_GROUP_P (insn) && !sel_sched_p ())
3355 fixup_sched_groups (insn);
3358 /* Initialize DEPS for the new block beginning with HEAD. */
3359 void
3360 deps_start_bb (struct deps_desc *deps, rtx head)
3362 gcc_assert (!deps->readonly);
3364 /* Before reload, if the previous block ended in a call, show that
3365 we are inside a post-call group, so as to keep the lifetimes of
3366 hard registers correct. */
3367 if (! reload_completed && !LABEL_P (head))
3369 rtx insn = prev_nonnote_nondebug_insn (head);
3371 if (insn && CALL_P (insn))
3372 deps->in_post_call_group_p = post_call_initial;
3376 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
3377 dependencies for each insn. */
3378 void
3379 sched_analyze (struct deps_desc *deps, rtx head, rtx tail)
3381 rtx insn;
3383 if (sched_deps_info->use_cselib)
3384 cselib_init (CSELIB_RECORD_MEMORY);
3386 deps_start_bb (deps, head);
3388 for (insn = head;; insn = NEXT_INSN (insn))
3391 if (INSN_P (insn))
3393 /* And initialize deps_lists. */
3394 sd_init_insn (insn);
3397 deps_analyze_insn (deps, insn);
3399 if (insn == tail)
3401 if (sched_deps_info->use_cselib)
3402 cselib_finish ();
3403 return;
3406 gcc_unreachable ();
3409 /* Helper for sched_free_deps ().
3410 Delete INSN's (RESOLVED_P) backward dependencies. */
3411 static void
3412 delete_dep_nodes_in_back_deps (rtx insn, bool resolved_p)
3414 sd_iterator_def sd_it;
3415 dep_t dep;
3416 sd_list_types_def types;
3418 if (resolved_p)
3419 types = SD_LIST_RES_BACK;
3420 else
3421 types = SD_LIST_BACK;
3423 for (sd_it = sd_iterator_start (insn, types);
3424 sd_iterator_cond (&sd_it, &dep);)
3426 dep_link_t link = *sd_it.linkp;
3427 dep_node_t node = DEP_LINK_NODE (link);
3428 deps_list_t back_list;
3429 deps_list_t forw_list;
3431 get_back_and_forw_lists (dep, resolved_p, &back_list, &forw_list);
3432 remove_from_deps_list (link, back_list);
3433 delete_dep_node (node);
3437 /* Delete (RESOLVED_P) dependencies between HEAD and TAIL together with
3438 deps_lists. */
3439 void
3440 sched_free_deps (rtx head, rtx tail, bool resolved_p)
3442 rtx insn;
3443 rtx next_tail = NEXT_INSN (tail);
3445 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3446 if (INSN_P (insn) && INSN_LUID (insn) > 0)
3448 /* Clear resolved back deps together with its dep_nodes. */
3449 delete_dep_nodes_in_back_deps (insn, resolved_p);
3451 /* Clear forward deps and leave the dep_nodes to the
3452 corresponding back_deps list. */
3453 if (resolved_p)
3454 clear_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
3455 else
3456 clear_deps_list (INSN_FORW_DEPS (insn));
3458 sd_finish_insn (insn);
3462 /* Initialize variables for region data dependence analysis.
3463 When LAZY_REG_LAST is true, do not allocate reg_last array
3464 of struct deps_desc immediately. */
3466 void
3467 init_deps (struct deps_desc *deps, bool lazy_reg_last)
3469 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
3471 deps->max_reg = max_reg;
3472 if (lazy_reg_last)
3473 deps->reg_last = NULL;
3474 else
3475 deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
3476 INIT_REG_SET (&deps->reg_last_in_use);
3477 INIT_REG_SET (&deps->reg_conditional_sets);
3479 deps->pending_read_insns = 0;
3480 deps->pending_read_mems = 0;
3481 deps->pending_write_insns = 0;
3482 deps->pending_write_mems = 0;
3483 deps->pending_read_list_length = 0;
3484 deps->pending_write_list_length = 0;
3485 deps->pending_flush_length = 0;
3486 deps->last_pending_memory_flush = 0;
3487 deps->last_function_call = 0;
3488 deps->last_function_call_may_noreturn = 0;
3489 deps->sched_before_next_call = 0;
3490 deps->in_post_call_group_p = not_post_call;
3491 deps->last_debug_insn = 0;
3492 deps->last_reg_pending_barrier = NOT_A_BARRIER;
3493 deps->readonly = 0;
3496 /* Init only reg_last field of DEPS, which was not allocated before as
3497 we inited DEPS lazily. */
3498 void
3499 init_deps_reg_last (struct deps_desc *deps)
3501 gcc_assert (deps && deps->max_reg > 0);
3502 gcc_assert (deps->reg_last == NULL);
3504 deps->reg_last = XCNEWVEC (struct deps_reg, deps->max_reg);
3508 /* Free insn lists found in DEPS. */
3510 void
3511 free_deps (struct deps_desc *deps)
3513 unsigned i;
3514 reg_set_iterator rsi;
3516 /* We set max_reg to 0 when this context was already freed. */
3517 if (deps->max_reg == 0)
3519 gcc_assert (deps->reg_last == NULL);
3520 return;
3522 deps->max_reg = 0;
3524 free_INSN_LIST_list (&deps->pending_read_insns);
3525 free_EXPR_LIST_list (&deps->pending_read_mems);
3526 free_INSN_LIST_list (&deps->pending_write_insns);
3527 free_EXPR_LIST_list (&deps->pending_write_mems);
3528 free_INSN_LIST_list (&deps->last_pending_memory_flush);
3530 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
3531 times. For a testcase with 42000 regs and 8000 small basic blocks,
3532 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
3533 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3535 struct deps_reg *reg_last = &deps->reg_last[i];
3536 if (reg_last->uses)
3537 free_INSN_LIST_list (&reg_last->uses);
3538 if (reg_last->sets)
3539 free_INSN_LIST_list (&reg_last->sets);
3540 if (reg_last->implicit_sets)
3541 free_INSN_LIST_list (&reg_last->implicit_sets);
3542 if (reg_last->clobbers)
3543 free_INSN_LIST_list (&reg_last->clobbers);
3545 CLEAR_REG_SET (&deps->reg_last_in_use);
3546 CLEAR_REG_SET (&deps->reg_conditional_sets);
3548 /* As we initialize reg_last lazily, it is possible that we didn't allocate
3549 it at all. */
3550 free (deps->reg_last);
3551 deps->reg_last = NULL;
3553 deps = NULL;
3556 /* Remove INSN from dependence contexts DEPS. Caution: reg_conditional_sets
3557 is not handled. */
3558 void
3559 remove_from_deps (struct deps_desc *deps, rtx insn)
3561 int removed;
3562 unsigned i;
3563 reg_set_iterator rsi;
3565 removed = remove_from_both_dependence_lists (insn, &deps->pending_read_insns,
3566 &deps->pending_read_mems);
3567 if (!DEBUG_INSN_P (insn))
3568 deps->pending_read_list_length -= removed;
3569 removed = remove_from_both_dependence_lists (insn, &deps->pending_write_insns,
3570 &deps->pending_write_mems);
3571 deps->pending_write_list_length -= removed;
3572 removed = remove_from_dependence_list (insn, &deps->last_pending_memory_flush);
3573 deps->pending_flush_length -= removed;
3575 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3577 struct deps_reg *reg_last = &deps->reg_last[i];
3578 if (reg_last->uses)
3579 remove_from_dependence_list (insn, &reg_last->uses);
3580 if (reg_last->sets)
3581 remove_from_dependence_list (insn, &reg_last->sets);
3582 if (reg_last->implicit_sets)
3583 remove_from_dependence_list (insn, &reg_last->implicit_sets);
3584 if (reg_last->clobbers)
3585 remove_from_dependence_list (insn, &reg_last->clobbers);
3586 if (!reg_last->uses && !reg_last->sets && !reg_last->implicit_sets
3587 && !reg_last->clobbers)
3588 CLEAR_REGNO_REG_SET (&deps->reg_last_in_use, i);
3591 if (CALL_P (insn))
3593 remove_from_dependence_list (insn, &deps->last_function_call);
3594 remove_from_dependence_list (insn,
3595 &deps->last_function_call_may_noreturn);
3597 remove_from_dependence_list (insn, &deps->sched_before_next_call);
3600 /* Init deps data vector. */
3601 static void
3602 init_deps_data_vector (void)
3604 int reserve = (sched_max_luid + 1
3605 - VEC_length (haifa_deps_insn_data_def, h_d_i_d));
3606 if (reserve > 0
3607 && ! VEC_space (haifa_deps_insn_data_def, h_d_i_d, reserve))
3608 VEC_safe_grow_cleared (haifa_deps_insn_data_def, heap, h_d_i_d,
3609 3 * sched_max_luid / 2);
3612 /* If it is profitable to use them, initialize or extend (depending on
3613 GLOBAL_P) dependency data. */
3614 void
3615 sched_deps_init (bool global_p)
3617 /* Average number of insns in the basic block.
3618 '+ 1' is used to make it nonzero. */
3619 int insns_in_block = sched_max_luid / n_basic_blocks + 1;
3621 init_deps_data_vector ();
3623 /* We use another caching mechanism for selective scheduling, so
3624 we don't use this one. */
3625 if (!sel_sched_p () && global_p && insns_in_block > 100 * 5)
3627 /* ?!? We could save some memory by computing a per-region luid mapping
3628 which could reduce both the number of vectors in the cache and the
3629 size of each vector. Instead we just avoid the cache entirely unless
3630 the average number of instructions in a basic block is very high. See
3631 the comment before the declaration of true_dependency_cache for
3632 what we consider "very high". */
3633 cache_size = 0;
3634 extend_dependency_caches (sched_max_luid, true);
3637 if (global_p)
3639 dl_pool = create_alloc_pool ("deps_list", sizeof (struct _deps_list),
3640 /* Allocate lists for one block at a time. */
3641 insns_in_block);
3642 dn_pool = create_alloc_pool ("dep_node", sizeof (struct _dep_node),
3643 /* Allocate nodes for one block at a time.
3644 We assume that average insn has
3645 5 producers. */
3646 5 * insns_in_block);
3651 /* Create or extend (depending on CREATE_P) dependency caches to
3652 size N. */
3653 void
3654 extend_dependency_caches (int n, bool create_p)
3656 if (create_p || true_dependency_cache)
3658 int i, luid = cache_size + n;
3660 true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
3661 luid);
3662 output_dependency_cache = XRESIZEVEC (bitmap_head,
3663 output_dependency_cache, luid);
3664 anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
3665 luid);
3667 if (current_sched_info->flags & DO_SPECULATION)
3668 spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
3669 luid);
3671 for (i = cache_size; i < luid; i++)
3673 bitmap_initialize (&true_dependency_cache[i], 0);
3674 bitmap_initialize (&output_dependency_cache[i], 0);
3675 bitmap_initialize (&anti_dependency_cache[i], 0);
3677 if (current_sched_info->flags & DO_SPECULATION)
3678 bitmap_initialize (&spec_dependency_cache[i], 0);
3680 cache_size = luid;
3684 /* Finalize dependency information for the whole function. */
3685 void
3686 sched_deps_finish (void)
3688 gcc_assert (deps_pools_are_empty_p ());
3689 free_alloc_pool_if_empty (&dn_pool);
3690 free_alloc_pool_if_empty (&dl_pool);
3691 gcc_assert (dn_pool == NULL && dl_pool == NULL);
3693 VEC_free (haifa_deps_insn_data_def, heap, h_d_i_d);
3694 cache_size = 0;
3696 if (true_dependency_cache)
3698 int i;
3700 for (i = 0; i < cache_size; i++)
3702 bitmap_clear (&true_dependency_cache[i]);
3703 bitmap_clear (&output_dependency_cache[i]);
3704 bitmap_clear (&anti_dependency_cache[i]);
3706 if (sched_deps_info->generate_spec_deps)
3707 bitmap_clear (&spec_dependency_cache[i]);
3709 free (true_dependency_cache);
3710 true_dependency_cache = NULL;
3711 free (output_dependency_cache);
3712 output_dependency_cache = NULL;
3713 free (anti_dependency_cache);
3714 anti_dependency_cache = NULL;
3716 if (sched_deps_info->generate_spec_deps)
3718 free (spec_dependency_cache);
3719 spec_dependency_cache = NULL;
3725 /* Initialize some global variables needed by the dependency analysis
3726 code. */
3728 void
3729 init_deps_global (void)
3731 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
3732 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
3733 reg_pending_sets = ALLOC_REG_SET (&reg_obstack);
3734 reg_pending_clobbers = ALLOC_REG_SET (&reg_obstack);
3735 reg_pending_uses = ALLOC_REG_SET (&reg_obstack);
3736 reg_pending_barrier = NOT_A_BARRIER;
3738 if (!sel_sched_p () || sched_emulate_haifa_p)
3740 sched_deps_info->start_insn = haifa_start_insn;
3741 sched_deps_info->finish_insn = haifa_finish_insn;
3743 sched_deps_info->note_reg_set = haifa_note_reg_set;
3744 sched_deps_info->note_reg_clobber = haifa_note_reg_clobber;
3745 sched_deps_info->note_reg_use = haifa_note_reg_use;
3747 sched_deps_info->note_mem_dep = haifa_note_mem_dep;
3748 sched_deps_info->note_dep = haifa_note_dep;
3752 /* Free everything used by the dependency analysis code. */
3754 void
3755 finish_deps_global (void)
3757 FREE_REG_SET (reg_pending_sets);
3758 FREE_REG_SET (reg_pending_clobbers);
3759 FREE_REG_SET (reg_pending_uses);
3762 /* Estimate the weakness of dependence between MEM1 and MEM2. */
3763 dw_t
3764 estimate_dep_weak (rtx mem1, rtx mem2)
3766 rtx r1, r2;
3768 if (mem1 == mem2)
3769 /* MEMs are the same - don't speculate. */
3770 return MIN_DEP_WEAK;
3772 r1 = XEXP (mem1, 0);
3773 r2 = XEXP (mem2, 0);
3775 if (r1 == r2
3776 || (REG_P (r1) && REG_P (r2)
3777 && REGNO (r1) == REGNO (r2)))
3778 /* Again, MEMs are the same. */
3779 return MIN_DEP_WEAK;
3780 else if ((REG_P (r1) && !REG_P (r2))
3781 || (!REG_P (r1) && REG_P (r2)))
3782 /* Different addressing modes - reason to be more speculative,
3783 than usual. */
3784 return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
3785 else
3786 /* We can't say anything about the dependence. */
3787 return UNCERTAIN_DEP_WEAK;
3790 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
3791 This function can handle same INSN and ELEM (INSN == ELEM).
3792 It is a convenience wrapper. */
3793 void
3794 add_dependence (rtx insn, rtx elem, enum reg_note dep_type)
3796 ds_t ds;
3797 bool internal;
3799 if (dep_type == REG_DEP_TRUE)
3800 ds = DEP_TRUE;
3801 else if (dep_type == REG_DEP_OUTPUT)
3802 ds = DEP_OUTPUT;
3803 else
3805 gcc_assert (dep_type == REG_DEP_ANTI);
3806 ds = DEP_ANTI;
3809 /* When add_dependence is called from inside sched-deps.c, we expect
3810 cur_insn to be non-null. */
3811 internal = cur_insn != NULL;
3812 if (internal)
3813 gcc_assert (insn == cur_insn);
3814 else
3815 cur_insn = insn;
3817 note_dep (elem, ds);
3818 if (!internal)
3819 cur_insn = NULL;
3822 /* Return weakness of speculative type TYPE in the dep_status DS. */
3823 dw_t
3824 get_dep_weak_1 (ds_t ds, ds_t type)
3826 ds = ds & type;
3828 switch (type)
3830 case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
3831 case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
3832 case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
3833 case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
3834 default: gcc_unreachable ();
3837 return (dw_t) ds;
3840 dw_t
3841 get_dep_weak (ds_t ds, ds_t type)
3843 dw_t dw = get_dep_weak_1 (ds, type);
3845 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
3846 return dw;
3849 /* Return the dep_status, which has the same parameters as DS, except for
3850 speculative type TYPE, that will have weakness DW. */
3851 ds_t
3852 set_dep_weak (ds_t ds, ds_t type, dw_t dw)
3854 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
3856 ds &= ~type;
3857 switch (type)
3859 case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
3860 case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
3861 case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
3862 case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
3863 default: gcc_unreachable ();
3865 return ds;
3868 /* Return the join of two dep_statuses DS1 and DS2.
3869 If MAX_P is true then choose the greater probability,
3870 otherwise multiply probabilities.
3871 This function assumes that both DS1 and DS2 contain speculative bits. */
3872 static ds_t
3873 ds_merge_1 (ds_t ds1, ds_t ds2, bool max_p)
3875 ds_t ds, t;
3877 gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
3879 ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
3881 t = FIRST_SPEC_TYPE;
3884 if ((ds1 & t) && !(ds2 & t))
3885 ds |= ds1 & t;
3886 else if (!(ds1 & t) && (ds2 & t))
3887 ds |= ds2 & t;
3888 else if ((ds1 & t) && (ds2 & t))
3890 dw_t dw1 = get_dep_weak (ds1, t);
3891 dw_t dw2 = get_dep_weak (ds2, t);
3892 ds_t dw;
3894 if (!max_p)
3896 dw = ((ds_t) dw1) * ((ds_t) dw2);
3897 dw /= MAX_DEP_WEAK;
3898 if (dw < MIN_DEP_WEAK)
3899 dw = MIN_DEP_WEAK;
3901 else
3903 if (dw1 >= dw2)
3904 dw = dw1;
3905 else
3906 dw = dw2;
3909 ds = set_dep_weak (ds, t, (dw_t) dw);
3912 if (t == LAST_SPEC_TYPE)
3913 break;
3914 t <<= SPEC_TYPE_SHIFT;
3916 while (1);
3918 return ds;
3921 /* Return the join of two dep_statuses DS1 and DS2.
3922 This function assumes that both DS1 and DS2 contain speculative bits. */
3923 ds_t
3924 ds_merge (ds_t ds1, ds_t ds2)
3926 return ds_merge_1 (ds1, ds2, false);
3929 /* Return the join of two dep_statuses DS1 and DS2. */
3930 ds_t
3931 ds_full_merge (ds_t ds, ds_t ds2, rtx mem1, rtx mem2)
3933 ds_t new_status = ds | ds2;
3935 if (new_status & SPECULATIVE)
3937 if ((ds && !(ds & SPECULATIVE))
3938 || (ds2 && !(ds2 & SPECULATIVE)))
3939 /* Then this dep can't be speculative. */
3940 new_status &= ~SPECULATIVE;
3941 else
3943 /* Both are speculative. Merging probabilities. */
3944 if (mem1)
3946 dw_t dw;
3948 dw = estimate_dep_weak (mem1, mem2);
3949 ds = set_dep_weak (ds, BEGIN_DATA, dw);
3952 if (!ds)
3953 new_status = ds2;
3954 else if (!ds2)
3955 new_status = ds;
3956 else
3957 new_status = ds_merge (ds2, ds);
3961 return new_status;
3964 /* Return the join of DS1 and DS2. Use maximum instead of multiplying
3965 probabilities. */
3966 ds_t
3967 ds_max_merge (ds_t ds1, ds_t ds2)
3969 if (ds1 == 0 && ds2 == 0)
3970 return 0;
3972 if (ds1 == 0 && ds2 != 0)
3973 return ds2;
3975 if (ds1 != 0 && ds2 == 0)
3976 return ds1;
3978 return ds_merge_1 (ds1, ds2, true);
3981 /* Return the probability of speculation success for the speculation
3982 status DS. */
3983 dw_t
3984 ds_weak (ds_t ds)
3986 ds_t res = 1, dt;
3987 int n = 0;
3989 dt = FIRST_SPEC_TYPE;
3992 if (ds & dt)
3994 res *= (ds_t) get_dep_weak (ds, dt);
3995 n++;
3998 if (dt == LAST_SPEC_TYPE)
3999 break;
4000 dt <<= SPEC_TYPE_SHIFT;
4002 while (1);
4004 gcc_assert (n);
4005 while (--n)
4006 res /= MAX_DEP_WEAK;
4008 if (res < MIN_DEP_WEAK)
4009 res = MIN_DEP_WEAK;
4011 gcc_assert (res <= MAX_DEP_WEAK);
4013 return (dw_t) res;
4016 /* Return a dep status that contains all speculation types of DS. */
4017 ds_t
4018 ds_get_speculation_types (ds_t ds)
4020 if (ds & BEGIN_DATA)
4021 ds |= BEGIN_DATA;
4022 if (ds & BE_IN_DATA)
4023 ds |= BE_IN_DATA;
4024 if (ds & BEGIN_CONTROL)
4025 ds |= BEGIN_CONTROL;
4026 if (ds & BE_IN_CONTROL)
4027 ds |= BE_IN_CONTROL;
4029 return ds & SPECULATIVE;
4032 /* Return a dep status that contains maximal weakness for each speculation
4033 type present in DS. */
4034 ds_t
4035 ds_get_max_dep_weak (ds_t ds)
4037 if (ds & BEGIN_DATA)
4038 ds = set_dep_weak (ds, BEGIN_DATA, MAX_DEP_WEAK);
4039 if (ds & BE_IN_DATA)
4040 ds = set_dep_weak (ds, BE_IN_DATA, MAX_DEP_WEAK);
4041 if (ds & BEGIN_CONTROL)
4042 ds = set_dep_weak (ds, BEGIN_CONTROL, MAX_DEP_WEAK);
4043 if (ds & BE_IN_CONTROL)
4044 ds = set_dep_weak (ds, BE_IN_CONTROL, MAX_DEP_WEAK);
4046 return ds;
4049 /* Dump information about the dependence status S. */
4050 static void
4051 dump_ds (FILE *f, ds_t s)
4053 fprintf (f, "{");
4055 if (s & BEGIN_DATA)
4056 fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak_1 (s, BEGIN_DATA));
4057 if (s & BE_IN_DATA)
4058 fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak_1 (s, BE_IN_DATA));
4059 if (s & BEGIN_CONTROL)
4060 fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak_1 (s, BEGIN_CONTROL));
4061 if (s & BE_IN_CONTROL)
4062 fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak_1 (s, BE_IN_CONTROL));
4064 if (s & HARD_DEP)
4065 fprintf (f, "HARD_DEP; ");
4067 if (s & DEP_TRUE)
4068 fprintf (f, "DEP_TRUE; ");
4069 if (s & DEP_ANTI)
4070 fprintf (f, "DEP_ANTI; ");
4071 if (s & DEP_OUTPUT)
4072 fprintf (f, "DEP_OUTPUT; ");
4074 fprintf (f, "}");
4077 DEBUG_FUNCTION void
4078 debug_ds (ds_t s)
4080 dump_ds (stderr, s);
4081 fprintf (stderr, "\n");
4084 #ifdef ENABLE_CHECKING
4085 /* Verify that dependence type and status are consistent.
4086 If RELAXED_P is true, then skip dep_weakness checks. */
4087 static void
4088 check_dep (dep_t dep, bool relaxed_p)
4090 enum reg_note dt = DEP_TYPE (dep);
4091 ds_t ds = DEP_STATUS (dep);
4093 gcc_assert (DEP_PRO (dep) != DEP_CON (dep));
4095 if (!(current_sched_info->flags & USE_DEPS_LIST))
4097 gcc_assert (ds == -1);
4098 return;
4101 /* Check that dependence type contains the same bits as the status. */
4102 if (dt == REG_DEP_TRUE)
4103 gcc_assert (ds & DEP_TRUE);
4104 else if (dt == REG_DEP_OUTPUT)
4105 gcc_assert ((ds & DEP_OUTPUT)
4106 && !(ds & DEP_TRUE));
4107 else
4108 gcc_assert ((dt == REG_DEP_ANTI)
4109 && (ds & DEP_ANTI)
4110 && !(ds & (DEP_OUTPUT | DEP_TRUE)));
4112 /* HARD_DEP can not appear in dep_status of a link. */
4113 gcc_assert (!(ds & HARD_DEP));
4115 /* Check that dependence status is set correctly when speculation is not
4116 supported. */
4117 if (!sched_deps_info->generate_spec_deps)
4118 gcc_assert (!(ds & SPECULATIVE));
4119 else if (ds & SPECULATIVE)
4121 if (!relaxed_p)
4123 ds_t type = FIRST_SPEC_TYPE;
4125 /* Check that dependence weakness is in proper range. */
4128 if (ds & type)
4129 get_dep_weak (ds, type);
4131 if (type == LAST_SPEC_TYPE)
4132 break;
4133 type <<= SPEC_TYPE_SHIFT;
4135 while (1);
4138 if (ds & BEGIN_SPEC)
4140 /* Only true dependence can be data speculative. */
4141 if (ds & BEGIN_DATA)
4142 gcc_assert (ds & DEP_TRUE);
4144 /* Control dependencies in the insn scheduler are represented by
4145 anti-dependencies, therefore only anti dependence can be
4146 control speculative. */
4147 if (ds & BEGIN_CONTROL)
4148 gcc_assert (ds & DEP_ANTI);
4150 else
4152 /* Subsequent speculations should resolve true dependencies. */
4153 gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
4156 /* Check that true and anti dependencies can't have other speculative
4157 statuses. */
4158 if (ds & DEP_TRUE)
4159 gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
4160 /* An output dependence can't be speculative at all. */
4161 gcc_assert (!(ds & DEP_OUTPUT));
4162 if (ds & DEP_ANTI)
4163 gcc_assert (ds & BEGIN_CONTROL);
4166 #endif /* ENABLE_CHECKING */
4168 #endif /* INSN_SCHEDULING */