* doc/install.texi (Downloading the Source): Update references to
[official-gcc.git] / gcc / sched-deps.c
blob2b4028663b16d4315f9cbc6866d14a4e48059a45
1 /* Instruction scheduling pass. This file computes dependencies between
2 instructions.
3 Copyright (C) 1992-2013 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
5 and currently maintained by, Jim Wilson (wilson@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "diagnostic-core.h"
28 #include "rtl.h"
29 #include "tree.h" /* FIXME: Used by call_may_noreturn_p. */
30 #include "tm_p.h"
31 #include "hard-reg-set.h"
32 #include "regs.h"
33 #include "function.h"
34 #include "flags.h"
35 #include "insn-config.h"
36 #include "insn-attr.h"
37 #include "except.h"
38 #include "recog.h"
39 #include "emit-rtl.h"
40 #include "sched-int.h"
41 #include "params.h"
42 #include "cselib.h"
43 #include "ira.h"
44 #include "target.h"
46 #ifdef INSN_SCHEDULING
48 #ifdef ENABLE_CHECKING
49 #define CHECK (true)
50 #else
51 #define CHECK (false)
52 #endif
54 /* Holds current parameters for the dependency analyzer. */
55 struct sched_deps_info_def *sched_deps_info;
57 /* The data is specific to the Haifa scheduler. */
58 vec<haifa_deps_insn_data_def>
59 h_d_i_d = vNULL;
61 /* Return the major type present in the DS. */
62 enum reg_note
63 ds_to_dk (ds_t ds)
65 if (ds & DEP_TRUE)
66 return REG_DEP_TRUE;
68 if (ds & DEP_OUTPUT)
69 return REG_DEP_OUTPUT;
71 if (ds & DEP_CONTROL)
72 return REG_DEP_CONTROL;
74 gcc_assert (ds & DEP_ANTI);
76 return REG_DEP_ANTI;
79 /* Return equivalent dep_status. */
80 ds_t
81 dk_to_ds (enum reg_note dk)
83 switch (dk)
85 case REG_DEP_TRUE:
86 return DEP_TRUE;
88 case REG_DEP_OUTPUT:
89 return DEP_OUTPUT;
91 case REG_DEP_CONTROL:
92 return DEP_CONTROL;
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;
110 DEP_COST (dep) = UNKNOWN_DEP_COST;
111 DEP_NONREG (dep) = 0;
112 DEP_MULTIPLE (dep) = 0;
113 DEP_REPLACE (dep) = NULL;
116 /* Init DEP with the arguments.
117 While most of the scheduler (including targets) only need the major type
118 of the dependency, it is convenient to hide full dep_status from them. */
119 void
120 init_dep (dep_t dep, rtx pro, rtx con, enum reg_note kind)
122 ds_t ds;
124 if ((current_sched_info->flags & USE_DEPS_LIST))
125 ds = dk_to_ds (kind);
126 else
127 ds = 0;
129 init_dep_1 (dep, pro, con, kind, ds);
132 /* Make a copy of FROM in TO. */
133 static void
134 copy_dep (dep_t to, dep_t from)
136 memcpy (to, from, sizeof (*to));
139 static void dump_ds (FILE *, ds_t);
141 /* Define flags for dump_dep (). */
143 /* Dump producer of the dependence. */
144 #define DUMP_DEP_PRO (2)
146 /* Dump consumer of the dependence. */
147 #define DUMP_DEP_CON (4)
149 /* Dump type of the dependence. */
150 #define DUMP_DEP_TYPE (8)
152 /* Dump status of the dependence. */
153 #define DUMP_DEP_STATUS (16)
155 /* Dump all information about the dependence. */
156 #define DUMP_DEP_ALL (DUMP_DEP_PRO | DUMP_DEP_CON | DUMP_DEP_TYPE \
157 |DUMP_DEP_STATUS)
159 /* Dump DEP to DUMP.
160 FLAGS is a bit mask specifying what information about DEP needs
161 to be printed.
162 If FLAGS has the very first bit set, then dump all information about DEP
163 and propagate this bit into the callee dump functions. */
164 static void
165 dump_dep (FILE *dump, dep_t dep, int flags)
167 if (flags & 1)
168 flags |= DUMP_DEP_ALL;
170 fprintf (dump, "<");
172 if (flags & DUMP_DEP_PRO)
173 fprintf (dump, "%d; ", INSN_UID (DEP_PRO (dep)));
175 if (flags & DUMP_DEP_CON)
176 fprintf (dump, "%d; ", INSN_UID (DEP_CON (dep)));
178 if (flags & DUMP_DEP_TYPE)
180 char t;
181 enum reg_note type = DEP_TYPE (dep);
183 switch (type)
185 case REG_DEP_TRUE:
186 t = 't';
187 break;
189 case REG_DEP_OUTPUT:
190 t = 'o';
191 break;
193 case REG_DEP_CONTROL:
194 t = 'c';
195 break;
197 case REG_DEP_ANTI:
198 t = 'a';
199 break;
201 default:
202 gcc_unreachable ();
203 break;
206 fprintf (dump, "%c; ", t);
209 if (flags & DUMP_DEP_STATUS)
211 if (current_sched_info->flags & USE_DEPS_LIST)
212 dump_ds (dump, DEP_STATUS (dep));
215 fprintf (dump, ">");
218 /* Default flags for dump_dep (). */
219 static int dump_dep_flags = (DUMP_DEP_PRO | DUMP_DEP_CON);
221 /* Dump all fields of DEP to STDERR. */
222 void
223 sd_debug_dep (dep_t dep)
225 dump_dep (stderr, dep, 1);
226 fprintf (stderr, "\n");
229 /* Determine whether DEP is a dependency link of a non-debug insn on a
230 debug insn. */
232 static inline bool
233 depl_on_debug_p (dep_link_t dep)
235 return (DEBUG_INSN_P (DEP_LINK_PRO (dep))
236 && !DEBUG_INSN_P (DEP_LINK_CON (dep)));
239 /* Functions to operate with a single link from the dependencies lists -
240 dep_link_t. */
242 /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
243 PREV_NEXT_P. */
244 static void
245 attach_dep_link (dep_link_t l, dep_link_t *prev_nextp)
247 dep_link_t next = *prev_nextp;
249 gcc_assert (DEP_LINK_PREV_NEXTP (l) == NULL
250 && DEP_LINK_NEXT (l) == NULL);
252 /* Init node being inserted. */
253 DEP_LINK_PREV_NEXTP (l) = prev_nextp;
254 DEP_LINK_NEXT (l) = next;
256 /* Fix next node. */
257 if (next != NULL)
259 gcc_assert (DEP_LINK_PREV_NEXTP (next) == prev_nextp);
261 DEP_LINK_PREV_NEXTP (next) = &DEP_LINK_NEXT (l);
264 /* Fix prev node. */
265 *prev_nextp = l;
268 /* Add dep_link LINK to deps_list L. */
269 static void
270 add_to_deps_list (dep_link_t link, deps_list_t l)
272 attach_dep_link (link, &DEPS_LIST_FIRST (l));
274 /* Don't count debug deps. */
275 if (!depl_on_debug_p (link))
276 ++DEPS_LIST_N_LINKS (l);
279 /* Detach dep_link L from the list. */
280 static void
281 detach_dep_link (dep_link_t l)
283 dep_link_t *prev_nextp = DEP_LINK_PREV_NEXTP (l);
284 dep_link_t next = DEP_LINK_NEXT (l);
286 *prev_nextp = next;
288 if (next != NULL)
289 DEP_LINK_PREV_NEXTP (next) = prev_nextp;
291 DEP_LINK_PREV_NEXTP (l) = NULL;
292 DEP_LINK_NEXT (l) = NULL;
295 /* Remove link LINK from list LIST. */
296 static void
297 remove_from_deps_list (dep_link_t link, deps_list_t list)
299 detach_dep_link (link);
301 /* Don't count debug deps. */
302 if (!depl_on_debug_p (link))
303 --DEPS_LIST_N_LINKS (list);
306 /* Move link LINK from list FROM to list TO. */
307 static void
308 move_dep_link (dep_link_t link, deps_list_t from, deps_list_t to)
310 remove_from_deps_list (link, from);
311 add_to_deps_list (link, to);
314 /* Return true of LINK is not attached to any list. */
315 static bool
316 dep_link_is_detached_p (dep_link_t link)
318 return DEP_LINK_PREV_NEXTP (link) == NULL;
321 /* Pool to hold all dependency nodes (dep_node_t). */
322 static alloc_pool dn_pool;
324 /* Number of dep_nodes out there. */
325 static int dn_pool_diff = 0;
327 /* Create a dep_node. */
328 static dep_node_t
329 create_dep_node (void)
331 dep_node_t n = (dep_node_t) pool_alloc (dn_pool);
332 dep_link_t back = DEP_NODE_BACK (n);
333 dep_link_t forw = DEP_NODE_FORW (n);
335 DEP_LINK_NODE (back) = n;
336 DEP_LINK_NEXT (back) = NULL;
337 DEP_LINK_PREV_NEXTP (back) = NULL;
339 DEP_LINK_NODE (forw) = n;
340 DEP_LINK_NEXT (forw) = NULL;
341 DEP_LINK_PREV_NEXTP (forw) = NULL;
343 ++dn_pool_diff;
345 return n;
348 /* Delete dep_node N. N must not be connected to any deps_list. */
349 static void
350 delete_dep_node (dep_node_t n)
352 gcc_assert (dep_link_is_detached_p (DEP_NODE_BACK (n))
353 && dep_link_is_detached_p (DEP_NODE_FORW (n)));
355 --dn_pool_diff;
357 pool_free (dn_pool, n);
360 /* Pool to hold dependencies lists (deps_list_t). */
361 static alloc_pool dl_pool;
363 /* Number of deps_lists out there. */
364 static int dl_pool_diff = 0;
366 /* Functions to operate with dependences lists - deps_list_t. */
368 /* Return true if list L is empty. */
369 static bool
370 deps_list_empty_p (deps_list_t l)
372 return DEPS_LIST_N_LINKS (l) == 0;
375 /* Create a new deps_list. */
376 static deps_list_t
377 create_deps_list (void)
379 deps_list_t l = (deps_list_t) pool_alloc (dl_pool);
381 DEPS_LIST_FIRST (l) = NULL;
382 DEPS_LIST_N_LINKS (l) = 0;
384 ++dl_pool_diff;
385 return l;
388 /* Free deps_list L. */
389 static void
390 free_deps_list (deps_list_t l)
392 gcc_assert (deps_list_empty_p (l));
394 --dl_pool_diff;
396 pool_free (dl_pool, l);
399 /* Return true if there is no dep_nodes and deps_lists out there.
400 After the region is scheduled all the dependency nodes and lists
401 should [generally] be returned to pool. */
402 bool
403 deps_pools_are_empty_p (void)
405 return dn_pool_diff == 0 && dl_pool_diff == 0;
408 /* Remove all elements from L. */
409 static void
410 clear_deps_list (deps_list_t l)
414 dep_link_t link = DEPS_LIST_FIRST (l);
416 if (link == NULL)
417 break;
419 remove_from_deps_list (link, l);
421 while (1);
424 /* Decide whether a dependency should be treated as a hard or a speculative
425 dependency. */
426 static bool
427 dep_spec_p (dep_t dep)
429 if (current_sched_info->flags & DO_SPECULATION)
431 if (DEP_STATUS (dep) & SPECULATIVE)
432 return true;
434 if (current_sched_info->flags & DO_PREDICATION)
436 if (DEP_TYPE (dep) == REG_DEP_CONTROL)
437 return true;
439 if (DEP_REPLACE (dep) != NULL)
440 return true;
441 return false;
444 static regset reg_pending_sets;
445 static regset reg_pending_clobbers;
446 static regset reg_pending_uses;
447 static regset reg_pending_control_uses;
448 static enum reg_pending_barrier_mode reg_pending_barrier;
450 /* Hard registers implicitly clobbered or used (or may be implicitly
451 clobbered or used) by the currently analyzed insn. For example,
452 insn in its constraint has one register class. Even if there is
453 currently no hard register in the insn, the particular hard
454 register will be in the insn after reload pass because the
455 constraint requires it. */
456 static HARD_REG_SET implicit_reg_pending_clobbers;
457 static HARD_REG_SET implicit_reg_pending_uses;
459 /* To speed up the test for duplicate dependency links we keep a
460 record of dependencies created by add_dependence when the average
461 number of instructions in a basic block is very large.
463 Studies have shown that there is typically around 5 instructions between
464 branches for typical C code. So we can make a guess that the average
465 basic block is approximately 5 instructions long; we will choose 100X
466 the average size as a very large basic block.
468 Each insn has associated bitmaps for its dependencies. Each bitmap
469 has enough entries to represent a dependency on any other insn in
470 the insn chain. All bitmap for true dependencies cache is
471 allocated then the rest two ones are also allocated. */
472 static bitmap_head *true_dependency_cache = NULL;
473 static bitmap_head *output_dependency_cache = NULL;
474 static bitmap_head *anti_dependency_cache = NULL;
475 static bitmap_head *control_dependency_cache = NULL;
476 static bitmap_head *spec_dependency_cache = NULL;
477 static int cache_size;
479 /* True if we should mark added dependencies as a non-register deps. */
480 static bool mark_as_hard;
482 static int deps_may_trap_p (const_rtx);
483 static void add_dependence_1 (rtx, rtx, enum reg_note);
484 static void add_dependence_list (rtx, rtx, int, enum reg_note, bool);
485 static void add_dependence_list_and_free (struct deps_desc *, rtx,
486 rtx *, int, enum reg_note, bool);
487 static void delete_all_dependences (rtx);
488 static void chain_to_prev_insn (rtx);
490 static void flush_pending_lists (struct deps_desc *, rtx, int, int);
491 static void sched_analyze_1 (struct deps_desc *, rtx, rtx);
492 static void sched_analyze_2 (struct deps_desc *, rtx, rtx);
493 static void sched_analyze_insn (struct deps_desc *, rtx, rtx);
495 static bool sched_has_condition_p (const_rtx);
496 static int conditions_mutex_p (const_rtx, const_rtx, bool, bool);
498 static enum DEPS_ADJUST_RESULT maybe_add_or_update_dep_1 (dep_t, bool,
499 rtx, rtx);
500 static enum DEPS_ADJUST_RESULT add_or_update_dep_1 (dep_t, bool, rtx, rtx);
502 #ifdef ENABLE_CHECKING
503 static void check_dep (dep_t, bool);
504 #endif
506 /* Return nonzero if a load of the memory reference MEM can cause a trap. */
508 static int
509 deps_may_trap_p (const_rtx mem)
511 const_rtx addr = XEXP (mem, 0);
513 if (REG_P (addr) && REGNO (addr) >= FIRST_PSEUDO_REGISTER)
515 const_rtx t = get_reg_known_value (REGNO (addr));
516 if (t)
517 addr = t;
519 return rtx_addr_can_trap_p (addr);
523 /* Find the condition under which INSN is executed. If REV is not NULL,
524 it is set to TRUE when the returned comparison should be reversed
525 to get the actual condition. */
526 static rtx
527 sched_get_condition_with_rev_uncached (const_rtx insn, bool *rev)
529 rtx pat = PATTERN (insn);
530 rtx src;
532 if (rev)
533 *rev = false;
535 if (GET_CODE (pat) == COND_EXEC)
536 return COND_EXEC_TEST (pat);
538 if (!any_condjump_p (insn) || !onlyjump_p (insn))
539 return 0;
541 src = SET_SRC (pc_set (insn));
543 if (XEXP (src, 2) == pc_rtx)
544 return XEXP (src, 0);
545 else if (XEXP (src, 1) == pc_rtx)
547 rtx cond = XEXP (src, 0);
548 enum rtx_code revcode = reversed_comparison_code (cond, insn);
550 if (revcode == UNKNOWN)
551 return 0;
553 if (rev)
554 *rev = true;
555 return cond;
558 return 0;
561 /* Return the condition under which INSN does not execute (i.e. the
562 not-taken condition for a conditional branch), or NULL if we cannot
563 find such a condition. The caller should make a copy of the condition
564 before using it. */
566 sched_get_reverse_condition_uncached (const_rtx insn)
568 bool rev;
569 rtx cond = sched_get_condition_with_rev_uncached (insn, &rev);
570 if (cond == NULL_RTX)
571 return cond;
572 if (!rev)
574 enum rtx_code revcode = reversed_comparison_code (cond, insn);
575 cond = gen_rtx_fmt_ee (revcode, GET_MODE (cond),
576 XEXP (cond, 0),
577 XEXP (cond, 1));
579 return cond;
582 /* Caching variant of sched_get_condition_with_rev_uncached.
583 We only do actual work the first time we come here for an insn; the
584 results are cached in INSN_CACHED_COND and INSN_REVERSE_COND. */
585 static rtx
586 sched_get_condition_with_rev (const_rtx insn, bool *rev)
588 bool tmp;
590 if (INSN_LUID (insn) == 0)
591 return sched_get_condition_with_rev_uncached (insn, rev);
593 if (INSN_CACHED_COND (insn) == const_true_rtx)
594 return NULL_RTX;
596 if (INSN_CACHED_COND (insn) != NULL_RTX)
598 if (rev)
599 *rev = INSN_REVERSE_COND (insn);
600 return INSN_CACHED_COND (insn);
603 INSN_CACHED_COND (insn) = sched_get_condition_with_rev_uncached (insn, &tmp);
604 INSN_REVERSE_COND (insn) = tmp;
606 if (INSN_CACHED_COND (insn) == NULL_RTX)
608 INSN_CACHED_COND (insn) = const_true_rtx;
609 return NULL_RTX;
612 if (rev)
613 *rev = INSN_REVERSE_COND (insn);
614 return INSN_CACHED_COND (insn);
617 /* True when we can find a condition under which INSN is executed. */
618 static bool
619 sched_has_condition_p (const_rtx insn)
621 return !! sched_get_condition_with_rev (insn, NULL);
626 /* Return nonzero if conditions COND1 and COND2 can never be both true. */
627 static int
628 conditions_mutex_p (const_rtx cond1, const_rtx cond2, bool rev1, bool rev2)
630 if (COMPARISON_P (cond1)
631 && COMPARISON_P (cond2)
632 && GET_CODE (cond1) ==
633 (rev1==rev2
634 ? reversed_comparison_code (cond2, NULL)
635 : GET_CODE (cond2))
636 && rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
637 && XEXP (cond1, 1) == XEXP (cond2, 1))
638 return 1;
639 return 0;
642 /* Return true if insn1 and insn2 can never depend on one another because
643 the conditions under which they are executed are mutually exclusive. */
644 bool
645 sched_insns_conditions_mutex_p (const_rtx insn1, const_rtx insn2)
647 rtx cond1, cond2;
648 bool rev1 = false, rev2 = false;
650 /* df doesn't handle conditional lifetimes entirely correctly;
651 calls mess up the conditional lifetimes. */
652 if (!CALL_P (insn1) && !CALL_P (insn2))
654 cond1 = sched_get_condition_with_rev (insn1, &rev1);
655 cond2 = sched_get_condition_with_rev (insn2, &rev2);
656 if (cond1 && cond2
657 && conditions_mutex_p (cond1, cond2, rev1, rev2)
658 /* Make sure first instruction doesn't affect condition of second
659 instruction if switched. */
660 && !modified_in_p (cond1, insn2)
661 /* Make sure second instruction doesn't affect condition of first
662 instruction if switched. */
663 && !modified_in_p (cond2, insn1))
664 return true;
666 return false;
670 /* Return true if INSN can potentially be speculated with type DS. */
671 bool
672 sched_insn_is_legitimate_for_speculation_p (const_rtx insn, ds_t ds)
674 if (HAS_INTERNAL_DEP (insn))
675 return false;
677 if (!NONJUMP_INSN_P (insn))
678 return false;
680 if (SCHED_GROUP_P (insn))
681 return false;
683 if (IS_SPECULATION_CHECK_P (CONST_CAST_RTX (insn)))
684 return false;
686 if (side_effects_p (PATTERN (insn)))
687 return false;
689 if (ds & BE_IN_SPEC)
690 /* The following instructions, which depend on a speculatively scheduled
691 instruction, cannot be speculatively scheduled along. */
693 if (may_trap_or_fault_p (PATTERN (insn)))
694 /* If instruction might fault, it cannot be speculatively scheduled.
695 For control speculation it's obvious why and for data speculation
696 it's because the insn might get wrong input if speculation
697 wasn't successful. */
698 return false;
700 if ((ds & BE_IN_DATA)
701 && sched_has_condition_p (insn))
702 /* If this is a predicated instruction, then it cannot be
703 speculatively scheduled. See PR35659. */
704 return false;
707 return true;
710 /* Initialize LIST_PTR to point to one of the lists present in TYPES_PTR,
711 initialize RESOLVED_P_PTR with true if that list consists of resolved deps,
712 and remove the type of returned [through LIST_PTR] list from TYPES_PTR.
713 This function is used to switch sd_iterator to the next list.
714 !!! For internal use only. Might consider moving it to sched-int.h. */
715 void
716 sd_next_list (const_rtx insn, sd_list_types_def *types_ptr,
717 deps_list_t *list_ptr, bool *resolved_p_ptr)
719 sd_list_types_def types = *types_ptr;
721 if (types & SD_LIST_HARD_BACK)
723 *list_ptr = INSN_HARD_BACK_DEPS (insn);
724 *resolved_p_ptr = false;
725 *types_ptr = types & ~SD_LIST_HARD_BACK;
727 else if (types & SD_LIST_SPEC_BACK)
729 *list_ptr = INSN_SPEC_BACK_DEPS (insn);
730 *resolved_p_ptr = false;
731 *types_ptr = types & ~SD_LIST_SPEC_BACK;
733 else if (types & SD_LIST_FORW)
735 *list_ptr = INSN_FORW_DEPS (insn);
736 *resolved_p_ptr = false;
737 *types_ptr = types & ~SD_LIST_FORW;
739 else if (types & SD_LIST_RES_BACK)
741 *list_ptr = INSN_RESOLVED_BACK_DEPS (insn);
742 *resolved_p_ptr = true;
743 *types_ptr = types & ~SD_LIST_RES_BACK;
745 else if (types & SD_LIST_RES_FORW)
747 *list_ptr = INSN_RESOLVED_FORW_DEPS (insn);
748 *resolved_p_ptr = true;
749 *types_ptr = types & ~SD_LIST_RES_FORW;
751 else
753 *list_ptr = NULL;
754 *resolved_p_ptr = false;
755 *types_ptr = SD_LIST_NONE;
759 /* Return the summary size of INSN's lists defined by LIST_TYPES. */
761 sd_lists_size (const_rtx insn, sd_list_types_def list_types)
763 int size = 0;
765 while (list_types != SD_LIST_NONE)
767 deps_list_t list;
768 bool resolved_p;
770 sd_next_list (insn, &list_types, &list, &resolved_p);
771 if (list)
772 size += DEPS_LIST_N_LINKS (list);
775 return size;
778 /* Return true if INSN's lists defined by LIST_TYPES are all empty. */
780 bool
781 sd_lists_empty_p (const_rtx insn, sd_list_types_def list_types)
783 while (list_types != SD_LIST_NONE)
785 deps_list_t list;
786 bool resolved_p;
788 sd_next_list (insn, &list_types, &list, &resolved_p);
789 if (!deps_list_empty_p (list))
790 return false;
793 return true;
796 /* Initialize data for INSN. */
797 void
798 sd_init_insn (rtx insn)
800 INSN_HARD_BACK_DEPS (insn) = create_deps_list ();
801 INSN_SPEC_BACK_DEPS (insn) = create_deps_list ();
802 INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list ();
803 INSN_FORW_DEPS (insn) = create_deps_list ();
804 INSN_RESOLVED_FORW_DEPS (insn) = create_deps_list ();
806 /* ??? It would be nice to allocate dependency caches here. */
809 /* Free data for INSN. */
810 void
811 sd_finish_insn (rtx insn)
813 /* ??? It would be nice to deallocate dependency caches here. */
815 free_deps_list (INSN_HARD_BACK_DEPS (insn));
816 INSN_HARD_BACK_DEPS (insn) = NULL;
818 free_deps_list (INSN_SPEC_BACK_DEPS (insn));
819 INSN_SPEC_BACK_DEPS (insn) = NULL;
821 free_deps_list (INSN_RESOLVED_BACK_DEPS (insn));
822 INSN_RESOLVED_BACK_DEPS (insn) = NULL;
824 free_deps_list (INSN_FORW_DEPS (insn));
825 INSN_FORW_DEPS (insn) = NULL;
827 free_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
828 INSN_RESOLVED_FORW_DEPS (insn) = NULL;
831 /* Find a dependency between producer PRO and consumer CON.
832 Search through resolved dependency lists if RESOLVED_P is true.
833 If no such dependency is found return NULL,
834 otherwise return the dependency and initialize SD_IT_PTR [if it is nonnull]
835 with an iterator pointing to it. */
836 static dep_t
837 sd_find_dep_between_no_cache (rtx pro, rtx con, bool resolved_p,
838 sd_iterator_def *sd_it_ptr)
840 sd_list_types_def pro_list_type;
841 sd_list_types_def con_list_type;
842 sd_iterator_def sd_it;
843 dep_t dep;
844 bool found_p = false;
846 if (resolved_p)
848 pro_list_type = SD_LIST_RES_FORW;
849 con_list_type = SD_LIST_RES_BACK;
851 else
853 pro_list_type = SD_LIST_FORW;
854 con_list_type = SD_LIST_BACK;
857 /* Walk through either back list of INSN or forw list of ELEM
858 depending on which one is shorter. */
859 if (sd_lists_size (con, con_list_type) < sd_lists_size (pro, pro_list_type))
861 /* Find the dep_link with producer PRO in consumer's back_deps. */
862 FOR_EACH_DEP (con, con_list_type, sd_it, dep)
863 if (DEP_PRO (dep) == pro)
865 found_p = true;
866 break;
869 else
871 /* Find the dep_link with consumer CON in producer's forw_deps. */
872 FOR_EACH_DEP (pro, pro_list_type, sd_it, dep)
873 if (DEP_CON (dep) == con)
875 found_p = true;
876 break;
880 if (found_p)
882 if (sd_it_ptr != NULL)
883 *sd_it_ptr = sd_it;
885 return dep;
888 return NULL;
891 /* Find a dependency between producer PRO and consumer CON.
892 Use dependency [if available] to check if dependency is present at all.
893 Search through resolved dependency lists if RESOLVED_P is true.
894 If the dependency or NULL if none found. */
895 dep_t
896 sd_find_dep_between (rtx pro, rtx con, bool resolved_p)
898 if (true_dependency_cache != NULL)
899 /* Avoiding the list walk below can cut compile times dramatically
900 for some code. */
902 int elem_luid = INSN_LUID (pro);
903 int insn_luid = INSN_LUID (con);
905 if (!bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid)
906 && !bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid)
907 && !bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid)
908 && !bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
909 return NULL;
912 return sd_find_dep_between_no_cache (pro, con, resolved_p, NULL);
915 /* Add or update a dependence described by DEP.
916 MEM1 and MEM2, if non-null, correspond to memory locations in case of
917 data speculation.
919 The function returns a value indicating if an old entry has been changed
920 or a new entry has been added to insn's backward deps.
922 This function merely checks if producer and consumer is the same insn
923 and doesn't create a dep in this case. Actual manipulation of
924 dependence data structures is performed in add_or_update_dep_1. */
925 static enum DEPS_ADJUST_RESULT
926 maybe_add_or_update_dep_1 (dep_t dep, bool resolved_p, rtx mem1, rtx mem2)
928 rtx elem = DEP_PRO (dep);
929 rtx insn = DEP_CON (dep);
931 gcc_assert (INSN_P (insn) && INSN_P (elem));
933 /* Don't depend an insn on itself. */
934 if (insn == elem)
936 if (sched_deps_info->generate_spec_deps)
937 /* INSN has an internal dependence, which we can't overcome. */
938 HAS_INTERNAL_DEP (insn) = 1;
940 return DEP_NODEP;
943 return add_or_update_dep_1 (dep, resolved_p, mem1, mem2);
946 /* Ask dependency caches what needs to be done for dependence DEP.
947 Return DEP_CREATED if new dependence should be created and there is no
948 need to try to find one searching the dependencies lists.
949 Return DEP_PRESENT if there already is a dependence described by DEP and
950 hence nothing is to be done.
951 Return DEP_CHANGED if there already is a dependence, but it should be
952 updated to incorporate additional information from DEP. */
953 static enum DEPS_ADJUST_RESULT
954 ask_dependency_caches (dep_t dep)
956 int elem_luid = INSN_LUID (DEP_PRO (dep));
957 int insn_luid = INSN_LUID (DEP_CON (dep));
959 gcc_assert (true_dependency_cache != NULL
960 && output_dependency_cache != NULL
961 && anti_dependency_cache != NULL
962 && control_dependency_cache != NULL);
964 if (!(current_sched_info->flags & USE_DEPS_LIST))
966 enum reg_note present_dep_type;
968 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
969 present_dep_type = REG_DEP_TRUE;
970 else if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
971 present_dep_type = REG_DEP_OUTPUT;
972 else if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
973 present_dep_type = REG_DEP_ANTI;
974 else if (bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
975 present_dep_type = REG_DEP_CONTROL;
976 else
977 /* There is no existing dep so it should be created. */
978 return DEP_CREATED;
980 if ((int) DEP_TYPE (dep) >= (int) present_dep_type)
981 /* DEP does not add anything to the existing dependence. */
982 return DEP_PRESENT;
984 else
986 ds_t present_dep_types = 0;
988 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
989 present_dep_types |= DEP_TRUE;
990 if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
991 present_dep_types |= DEP_OUTPUT;
992 if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
993 present_dep_types |= DEP_ANTI;
994 if (bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
995 present_dep_types |= DEP_CONTROL;
997 if (present_dep_types == 0)
998 /* There is no existing dep so it should be created. */
999 return DEP_CREATED;
1001 if (!(current_sched_info->flags & DO_SPECULATION)
1002 || !bitmap_bit_p (&spec_dependency_cache[insn_luid], elem_luid))
1004 if ((present_dep_types | (DEP_STATUS (dep) & DEP_TYPES))
1005 == present_dep_types)
1006 /* DEP does not add anything to the existing dependence. */
1007 return DEP_PRESENT;
1009 else
1011 /* Only true dependencies can be data speculative and
1012 only anti dependencies can be control speculative. */
1013 gcc_assert ((present_dep_types & (DEP_TRUE | DEP_ANTI))
1014 == present_dep_types);
1016 /* if (DEP is SPECULATIVE) then
1017 ..we should update DEP_STATUS
1018 else
1019 ..we should reset existing dep to non-speculative. */
1023 return DEP_CHANGED;
1026 /* Set dependency caches according to DEP. */
1027 static void
1028 set_dependency_caches (dep_t dep)
1030 int elem_luid = INSN_LUID (DEP_PRO (dep));
1031 int insn_luid = INSN_LUID (DEP_CON (dep));
1033 if (!(current_sched_info->flags & USE_DEPS_LIST))
1035 switch (DEP_TYPE (dep))
1037 case REG_DEP_TRUE:
1038 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
1039 break;
1041 case REG_DEP_OUTPUT:
1042 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
1043 break;
1045 case REG_DEP_ANTI:
1046 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
1047 break;
1049 case REG_DEP_CONTROL:
1050 bitmap_set_bit (&control_dependency_cache[insn_luid], elem_luid);
1051 break;
1053 default:
1054 gcc_unreachable ();
1057 else
1059 ds_t ds = DEP_STATUS (dep);
1061 if (ds & DEP_TRUE)
1062 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
1063 if (ds & DEP_OUTPUT)
1064 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
1065 if (ds & DEP_ANTI)
1066 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
1067 if (ds & DEP_CONTROL)
1068 bitmap_set_bit (&control_dependency_cache[insn_luid], elem_luid);
1070 if (ds & SPECULATIVE)
1072 gcc_assert (current_sched_info->flags & DO_SPECULATION);
1073 bitmap_set_bit (&spec_dependency_cache[insn_luid], elem_luid);
1078 /* Type of dependence DEP have changed from OLD_TYPE. Update dependency
1079 caches accordingly. */
1080 static void
1081 update_dependency_caches (dep_t dep, enum reg_note old_type)
1083 int elem_luid = INSN_LUID (DEP_PRO (dep));
1084 int insn_luid = INSN_LUID (DEP_CON (dep));
1086 /* Clear corresponding cache entry because type of the link
1087 may have changed. Keep them if we use_deps_list. */
1088 if (!(current_sched_info->flags & USE_DEPS_LIST))
1090 switch (old_type)
1092 case REG_DEP_OUTPUT:
1093 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1094 break;
1096 case REG_DEP_ANTI:
1097 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1098 break;
1100 case REG_DEP_CONTROL:
1101 bitmap_clear_bit (&control_dependency_cache[insn_luid], elem_luid);
1102 break;
1104 default:
1105 gcc_unreachable ();
1109 set_dependency_caches (dep);
1112 /* Convert a dependence pointed to by SD_IT to be non-speculative. */
1113 static void
1114 change_spec_dep_to_hard (sd_iterator_def sd_it)
1116 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1117 dep_link_t link = DEP_NODE_BACK (node);
1118 dep_t dep = DEP_NODE_DEP (node);
1119 rtx elem = DEP_PRO (dep);
1120 rtx insn = DEP_CON (dep);
1122 move_dep_link (link, INSN_SPEC_BACK_DEPS (insn), INSN_HARD_BACK_DEPS (insn));
1124 DEP_STATUS (dep) &= ~SPECULATIVE;
1126 if (true_dependency_cache != NULL)
1127 /* Clear the cache entry. */
1128 bitmap_clear_bit (&spec_dependency_cache[INSN_LUID (insn)],
1129 INSN_LUID (elem));
1132 /* Update DEP to incorporate information from NEW_DEP.
1133 SD_IT points to DEP in case it should be moved to another list.
1134 MEM1 and MEM2, if nonnull, correspond to memory locations in case if
1135 data-speculative dependence should be updated. */
1136 static enum DEPS_ADJUST_RESULT
1137 update_dep (dep_t dep, dep_t new_dep,
1138 sd_iterator_def sd_it ATTRIBUTE_UNUSED,
1139 rtx mem1 ATTRIBUTE_UNUSED,
1140 rtx mem2 ATTRIBUTE_UNUSED)
1142 enum DEPS_ADJUST_RESULT res = DEP_PRESENT;
1143 enum reg_note old_type = DEP_TYPE (dep);
1144 bool was_spec = dep_spec_p (dep);
1146 DEP_NONREG (dep) |= DEP_NONREG (new_dep);
1147 DEP_MULTIPLE (dep) = 1;
1149 /* If this is a more restrictive type of dependence than the
1150 existing one, then change the existing dependence to this
1151 type. */
1152 if ((int) DEP_TYPE (new_dep) < (int) old_type)
1154 DEP_TYPE (dep) = DEP_TYPE (new_dep);
1155 res = DEP_CHANGED;
1158 if (current_sched_info->flags & USE_DEPS_LIST)
1159 /* Update DEP_STATUS. */
1161 ds_t dep_status = DEP_STATUS (dep);
1162 ds_t ds = DEP_STATUS (new_dep);
1163 ds_t new_status = ds | dep_status;
1165 if (new_status & SPECULATIVE)
1167 /* Either existing dep or a dep we're adding or both are
1168 speculative. */
1169 if (!(ds & SPECULATIVE)
1170 || !(dep_status & SPECULATIVE))
1171 /* The new dep can't be speculative. */
1172 new_status &= ~SPECULATIVE;
1173 else
1175 /* Both are speculative. Merge probabilities. */
1176 if (mem1 != NULL)
1178 dw_t dw;
1180 dw = estimate_dep_weak (mem1, mem2);
1181 ds = set_dep_weak (ds, BEGIN_DATA, dw);
1184 new_status = ds_merge (dep_status, ds);
1188 ds = new_status;
1190 if (dep_status != ds)
1192 DEP_STATUS (dep) = ds;
1193 res = DEP_CHANGED;
1197 if (was_spec && !dep_spec_p (dep))
1198 /* The old dep was speculative, but now it isn't. */
1199 change_spec_dep_to_hard (sd_it);
1201 if (true_dependency_cache != NULL
1202 && res == DEP_CHANGED)
1203 update_dependency_caches (dep, old_type);
1205 return res;
1208 /* Add or update a dependence described by DEP.
1209 MEM1 and MEM2, if non-null, correspond to memory locations in case of
1210 data speculation.
1212 The function returns a value indicating if an old entry has been changed
1213 or a new entry has been added to insn's backward deps or nothing has
1214 been updated at all. */
1215 static enum DEPS_ADJUST_RESULT
1216 add_or_update_dep_1 (dep_t new_dep, bool resolved_p,
1217 rtx mem1 ATTRIBUTE_UNUSED, rtx mem2 ATTRIBUTE_UNUSED)
1219 bool maybe_present_p = true;
1220 bool present_p = false;
1222 gcc_assert (INSN_P (DEP_PRO (new_dep)) && INSN_P (DEP_CON (new_dep))
1223 && DEP_PRO (new_dep) != DEP_CON (new_dep));
1225 #ifdef ENABLE_CHECKING
1226 check_dep (new_dep, mem1 != NULL);
1227 #endif
1229 if (true_dependency_cache != NULL)
1231 switch (ask_dependency_caches (new_dep))
1233 case DEP_PRESENT:
1234 return DEP_PRESENT;
1236 case DEP_CHANGED:
1237 maybe_present_p = true;
1238 present_p = true;
1239 break;
1241 case DEP_CREATED:
1242 maybe_present_p = false;
1243 present_p = false;
1244 break;
1246 default:
1247 gcc_unreachable ();
1248 break;
1252 /* Check that we don't already have this dependence. */
1253 if (maybe_present_p)
1255 dep_t present_dep;
1256 sd_iterator_def sd_it;
1258 gcc_assert (true_dependency_cache == NULL || present_p);
1260 present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
1261 DEP_CON (new_dep),
1262 resolved_p, &sd_it);
1264 if (present_dep != NULL)
1265 /* We found an existing dependency between ELEM and INSN. */
1266 return update_dep (present_dep, new_dep, sd_it, mem1, mem2);
1267 else
1268 /* We didn't find a dep, it shouldn't present in the cache. */
1269 gcc_assert (!present_p);
1272 /* Might want to check one level of transitivity to save conses.
1273 This check should be done in maybe_add_or_update_dep_1.
1274 Since we made it to add_or_update_dep_1, we must create
1275 (or update) a link. */
1277 if (mem1 != NULL_RTX)
1279 gcc_assert (sched_deps_info->generate_spec_deps);
1280 DEP_STATUS (new_dep) = set_dep_weak (DEP_STATUS (new_dep), BEGIN_DATA,
1281 estimate_dep_weak (mem1, mem2));
1284 sd_add_dep (new_dep, resolved_p);
1286 return DEP_CREATED;
1289 /* Initialize BACK_LIST_PTR with consumer's backward list and
1290 FORW_LIST_PTR with producer's forward list. If RESOLVED_P is true
1291 initialize with lists that hold resolved deps. */
1292 static void
1293 get_back_and_forw_lists (dep_t dep, bool resolved_p,
1294 deps_list_t *back_list_ptr,
1295 deps_list_t *forw_list_ptr)
1297 rtx con = DEP_CON (dep);
1299 if (!resolved_p)
1301 if (dep_spec_p (dep))
1302 *back_list_ptr = INSN_SPEC_BACK_DEPS (con);
1303 else
1304 *back_list_ptr = INSN_HARD_BACK_DEPS (con);
1306 *forw_list_ptr = INSN_FORW_DEPS (DEP_PRO (dep));
1308 else
1310 *back_list_ptr = INSN_RESOLVED_BACK_DEPS (con);
1311 *forw_list_ptr = INSN_RESOLVED_FORW_DEPS (DEP_PRO (dep));
1315 /* Add dependence described by DEP.
1316 If RESOLVED_P is true treat the dependence as a resolved one. */
1317 void
1318 sd_add_dep (dep_t dep, bool resolved_p)
1320 dep_node_t n = create_dep_node ();
1321 deps_list_t con_back_deps;
1322 deps_list_t pro_forw_deps;
1323 rtx elem = DEP_PRO (dep);
1324 rtx insn = DEP_CON (dep);
1326 gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
1328 if ((current_sched_info->flags & DO_SPECULATION) == 0
1329 || !sched_insn_is_legitimate_for_speculation_p (insn, DEP_STATUS (dep)))
1330 DEP_STATUS (dep) &= ~SPECULATIVE;
1332 copy_dep (DEP_NODE_DEP (n), dep);
1334 get_back_and_forw_lists (dep, resolved_p, &con_back_deps, &pro_forw_deps);
1336 add_to_deps_list (DEP_NODE_BACK (n), con_back_deps);
1338 #ifdef ENABLE_CHECKING
1339 check_dep (dep, false);
1340 #endif
1342 add_to_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1344 /* If we are adding a dependency to INSN's LOG_LINKs, then note that
1345 in the bitmap caches of dependency information. */
1346 if (true_dependency_cache != NULL)
1347 set_dependency_caches (dep);
1350 /* Add or update backward dependence between INSN and ELEM
1351 with given type DEP_TYPE and dep_status DS.
1352 This function is a convenience wrapper. */
1353 enum DEPS_ADJUST_RESULT
1354 sd_add_or_update_dep (dep_t dep, bool resolved_p)
1356 return add_or_update_dep_1 (dep, resolved_p, NULL_RTX, NULL_RTX);
1359 /* Resolved dependence pointed to by SD_IT.
1360 SD_IT will advance to the next element. */
1361 void
1362 sd_resolve_dep (sd_iterator_def sd_it)
1364 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1365 dep_t dep = DEP_NODE_DEP (node);
1366 rtx pro = DEP_PRO (dep);
1367 rtx con = DEP_CON (dep);
1369 if (dep_spec_p (dep))
1370 move_dep_link (DEP_NODE_BACK (node), INSN_SPEC_BACK_DEPS (con),
1371 INSN_RESOLVED_BACK_DEPS (con));
1372 else
1373 move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
1374 INSN_RESOLVED_BACK_DEPS (con));
1376 move_dep_link (DEP_NODE_FORW (node), INSN_FORW_DEPS (pro),
1377 INSN_RESOLVED_FORW_DEPS (pro));
1380 /* Perform the inverse operation of sd_resolve_dep. Restore the dependence
1381 pointed to by SD_IT to unresolved state. */
1382 void
1383 sd_unresolve_dep (sd_iterator_def sd_it)
1385 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1386 dep_t dep = DEP_NODE_DEP (node);
1387 rtx pro = DEP_PRO (dep);
1388 rtx con = DEP_CON (dep);
1390 if (dep_spec_p (dep))
1391 move_dep_link (DEP_NODE_BACK (node), INSN_RESOLVED_BACK_DEPS (con),
1392 INSN_SPEC_BACK_DEPS (con));
1393 else
1394 move_dep_link (DEP_NODE_BACK (node), INSN_RESOLVED_BACK_DEPS (con),
1395 INSN_HARD_BACK_DEPS (con));
1397 move_dep_link (DEP_NODE_FORW (node), INSN_RESOLVED_FORW_DEPS (pro),
1398 INSN_FORW_DEPS (pro));
1401 /* Make TO depend on all the FROM's producers.
1402 If RESOLVED_P is true add dependencies to the resolved lists. */
1403 void
1404 sd_copy_back_deps (rtx to, rtx from, bool resolved_p)
1406 sd_list_types_def list_type;
1407 sd_iterator_def sd_it;
1408 dep_t dep;
1410 list_type = resolved_p ? SD_LIST_RES_BACK : SD_LIST_BACK;
1412 FOR_EACH_DEP (from, list_type, sd_it, dep)
1414 dep_def _new_dep, *new_dep = &_new_dep;
1416 copy_dep (new_dep, dep);
1417 DEP_CON (new_dep) = to;
1418 sd_add_dep (new_dep, resolved_p);
1422 /* Remove a dependency referred to by SD_IT.
1423 SD_IT will point to the next dependence after removal. */
1424 void
1425 sd_delete_dep (sd_iterator_def sd_it)
1427 dep_node_t n = DEP_LINK_NODE (*sd_it.linkp);
1428 dep_t dep = DEP_NODE_DEP (n);
1429 rtx pro = DEP_PRO (dep);
1430 rtx con = DEP_CON (dep);
1431 deps_list_t con_back_deps;
1432 deps_list_t pro_forw_deps;
1434 if (true_dependency_cache != NULL)
1436 int elem_luid = INSN_LUID (pro);
1437 int insn_luid = INSN_LUID (con);
1439 bitmap_clear_bit (&true_dependency_cache[insn_luid], elem_luid);
1440 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1441 bitmap_clear_bit (&control_dependency_cache[insn_luid], elem_luid);
1442 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1444 if (current_sched_info->flags & DO_SPECULATION)
1445 bitmap_clear_bit (&spec_dependency_cache[insn_luid], elem_luid);
1448 get_back_and_forw_lists (dep, sd_it.resolved_p,
1449 &con_back_deps, &pro_forw_deps);
1451 remove_from_deps_list (DEP_NODE_BACK (n), con_back_deps);
1452 remove_from_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1454 delete_dep_node (n);
1457 /* Dump size of the lists. */
1458 #define DUMP_LISTS_SIZE (2)
1460 /* Dump dependencies of the lists. */
1461 #define DUMP_LISTS_DEPS (4)
1463 /* Dump all information about the lists. */
1464 #define DUMP_LISTS_ALL (DUMP_LISTS_SIZE | DUMP_LISTS_DEPS)
1466 /* Dump deps_lists of INSN specified by TYPES to DUMP.
1467 FLAGS is a bit mask specifying what information about the lists needs
1468 to be printed.
1469 If FLAGS has the very first bit set, then dump all information about
1470 the lists and propagate this bit into the callee dump functions. */
1471 static void
1472 dump_lists (FILE *dump, rtx insn, sd_list_types_def types, int flags)
1474 sd_iterator_def sd_it;
1475 dep_t dep;
1476 int all;
1478 all = (flags & 1);
1480 if (all)
1481 flags |= DUMP_LISTS_ALL;
1483 fprintf (dump, "[");
1485 if (flags & DUMP_LISTS_SIZE)
1486 fprintf (dump, "%d; ", sd_lists_size (insn, types));
1488 if (flags & DUMP_LISTS_DEPS)
1490 FOR_EACH_DEP (insn, types, sd_it, dep)
1492 dump_dep (dump, dep, dump_dep_flags | all);
1493 fprintf (dump, " ");
1498 /* Dump all information about deps_lists of INSN specified by TYPES
1499 to STDERR. */
1500 void
1501 sd_debug_lists (rtx insn, sd_list_types_def types)
1503 dump_lists (stderr, insn, types, 1);
1504 fprintf (stderr, "\n");
1507 /* A wrapper around add_dependence_1, to add a dependence of CON on
1508 PRO, with type DEP_TYPE. This function implements special handling
1509 for REG_DEP_CONTROL dependencies. For these, we optionally promote
1510 the type to REG_DEP_ANTI if we can determine that predication is
1511 impossible; otherwise we add additional true dependencies on the
1512 INSN_COND_DEPS list of the jump (which PRO must be). */
1513 void
1514 add_dependence (rtx con, rtx pro, enum reg_note dep_type)
1516 if (dep_type == REG_DEP_CONTROL
1517 && !(current_sched_info->flags & DO_PREDICATION))
1518 dep_type = REG_DEP_ANTI;
1520 /* A REG_DEP_CONTROL dependence may be eliminated through predication,
1521 so we must also make the insn dependent on the setter of the
1522 condition. */
1523 if (dep_type == REG_DEP_CONTROL)
1525 rtx real_pro = pro;
1526 rtx other = real_insn_for_shadow (real_pro);
1527 rtx cond;
1529 if (other != NULL_RTX)
1530 real_pro = other;
1531 cond = sched_get_reverse_condition_uncached (real_pro);
1532 /* Verify that the insn does not use a different value in
1533 the condition register than the one that was present at
1534 the jump. */
1535 if (cond == NULL_RTX)
1536 dep_type = REG_DEP_ANTI;
1537 else if (INSN_CACHED_COND (real_pro) == const_true_rtx)
1539 HARD_REG_SET uses;
1540 CLEAR_HARD_REG_SET (uses);
1541 note_uses (&PATTERN (con), record_hard_reg_uses, &uses);
1542 if (TEST_HARD_REG_BIT (uses, REGNO (XEXP (cond, 0))))
1543 dep_type = REG_DEP_ANTI;
1545 if (dep_type == REG_DEP_CONTROL)
1547 if (sched_verbose >= 5)
1548 fprintf (sched_dump, "making DEP_CONTROL for %d\n",
1549 INSN_UID (real_pro));
1550 add_dependence_list (con, INSN_COND_DEPS (real_pro), 0,
1551 REG_DEP_TRUE, false);
1555 add_dependence_1 (con, pro, dep_type);
1558 /* A convenience wrapper to operate on an entire list. HARD should be
1559 true if DEP_NONREG should be set on newly created dependencies. */
1561 static void
1562 add_dependence_list (rtx insn, rtx list, int uncond, enum reg_note dep_type,
1563 bool hard)
1565 mark_as_hard = hard;
1566 for (; list; list = XEXP (list, 1))
1568 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1569 add_dependence (insn, XEXP (list, 0), dep_type);
1571 mark_as_hard = false;
1574 /* Similar, but free *LISTP at the same time, when the context
1575 is not readonly. HARD should be true if DEP_NONREG should be set on
1576 newly created dependencies. */
1578 static void
1579 add_dependence_list_and_free (struct deps_desc *deps, rtx insn, rtx *listp,
1580 int uncond, enum reg_note dep_type, bool hard)
1582 add_dependence_list (insn, *listp, uncond, dep_type, hard);
1584 /* We don't want to short-circuit dependencies involving debug
1585 insns, because they may cause actual dependencies to be
1586 disregarded. */
1587 if (deps->readonly || DEBUG_INSN_P (insn))
1588 return;
1590 free_INSN_LIST_list (listp);
1593 /* Remove all occurrences of INSN from LIST. Return the number of
1594 occurrences removed. */
1596 static int
1597 remove_from_dependence_list (rtx insn, rtx* listp)
1599 int removed = 0;
1601 while (*listp)
1603 if (XEXP (*listp, 0) == insn)
1605 remove_free_INSN_LIST_node (listp);
1606 removed++;
1607 continue;
1610 listp = &XEXP (*listp, 1);
1613 return removed;
1616 /* Same as above, but process two lists at once. */
1617 static int
1618 remove_from_both_dependence_lists (rtx insn, rtx *listp, rtx *exprp)
1620 int removed = 0;
1622 while (*listp)
1624 if (XEXP (*listp, 0) == insn)
1626 remove_free_INSN_LIST_node (listp);
1627 remove_free_EXPR_LIST_node (exprp);
1628 removed++;
1629 continue;
1632 listp = &XEXP (*listp, 1);
1633 exprp = &XEXP (*exprp, 1);
1636 return removed;
1639 /* Clear all dependencies for an insn. */
1640 static void
1641 delete_all_dependences (rtx insn)
1643 sd_iterator_def sd_it;
1644 dep_t dep;
1646 /* The below cycle can be optimized to clear the caches and back_deps
1647 in one call but that would provoke duplication of code from
1648 delete_dep (). */
1650 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1651 sd_iterator_cond (&sd_it, &dep);)
1652 sd_delete_dep (sd_it);
1655 /* All insns in a scheduling group except the first should only have
1656 dependencies on the previous insn in the group. So we find the
1657 first instruction in the scheduling group by walking the dependence
1658 chains backwards. Then we add the dependencies for the group to
1659 the previous nonnote insn. */
1661 static void
1662 chain_to_prev_insn (rtx insn)
1664 sd_iterator_def sd_it;
1665 dep_t dep;
1666 rtx prev_nonnote;
1668 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
1670 rtx i = insn;
1671 rtx pro = DEP_PRO (dep);
1675 i = prev_nonnote_insn (i);
1677 if (pro == i)
1678 goto next_link;
1679 } while (SCHED_GROUP_P (i) || DEBUG_INSN_P (i));
1681 if (! sched_insns_conditions_mutex_p (i, pro))
1682 add_dependence (i, pro, DEP_TYPE (dep));
1683 next_link:;
1686 delete_all_dependences (insn);
1688 prev_nonnote = prev_nonnote_nondebug_insn (insn);
1689 if (BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (prev_nonnote)
1690 && ! sched_insns_conditions_mutex_p (insn, prev_nonnote))
1691 add_dependence (insn, prev_nonnote, REG_DEP_ANTI);
1694 /* Process an insn's memory dependencies. There are four kinds of
1695 dependencies:
1697 (0) read dependence: read follows read
1698 (1) true dependence: read follows write
1699 (2) output dependence: write follows write
1700 (3) anti dependence: write follows read
1702 We are careful to build only dependencies which actually exist, and
1703 use transitivity to avoid building too many links. */
1705 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1706 The MEM is a memory reference contained within INSN, which we are saving
1707 so that we can do memory aliasing on it. */
1709 static void
1710 add_insn_mem_dependence (struct deps_desc *deps, bool read_p,
1711 rtx insn, rtx mem)
1713 rtx *insn_list;
1714 rtx *mem_list;
1715 rtx link;
1717 gcc_assert (!deps->readonly);
1718 if (read_p)
1720 insn_list = &deps->pending_read_insns;
1721 mem_list = &deps->pending_read_mems;
1722 if (!DEBUG_INSN_P (insn))
1723 deps->pending_read_list_length++;
1725 else
1727 insn_list = &deps->pending_write_insns;
1728 mem_list = &deps->pending_write_mems;
1729 deps->pending_write_list_length++;
1732 link = alloc_INSN_LIST (insn, *insn_list);
1733 *insn_list = link;
1735 if (sched_deps_info->use_cselib)
1737 mem = shallow_copy_rtx (mem);
1738 XEXP (mem, 0) = cselib_subst_to_values_from_insn (XEXP (mem, 0),
1739 GET_MODE (mem), insn);
1741 link = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
1742 *mem_list = link;
1745 /* Make a dependency between every memory reference on the pending lists
1746 and INSN, thus flushing the pending lists. FOR_READ is true if emitting
1747 dependencies for a read operation, similarly with FOR_WRITE. */
1749 static void
1750 flush_pending_lists (struct deps_desc *deps, rtx insn, int for_read,
1751 int for_write)
1753 if (for_write)
1755 add_dependence_list_and_free (deps, insn, &deps->pending_read_insns,
1756 1, REG_DEP_ANTI, true);
1757 if (!deps->readonly)
1759 free_EXPR_LIST_list (&deps->pending_read_mems);
1760 deps->pending_read_list_length = 0;
1764 add_dependence_list_and_free (deps, insn, &deps->pending_write_insns, 1,
1765 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT,
1766 true);
1768 add_dependence_list_and_free (deps, insn,
1769 &deps->last_pending_memory_flush, 1,
1770 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT,
1771 true);
1773 add_dependence_list_and_free (deps, insn, &deps->pending_jump_insns, 1,
1774 REG_DEP_ANTI, true);
1776 if (DEBUG_INSN_P (insn))
1778 if (for_write)
1779 free_INSN_LIST_list (&deps->pending_read_insns);
1780 free_INSN_LIST_list (&deps->pending_write_insns);
1781 free_INSN_LIST_list (&deps->last_pending_memory_flush);
1782 free_INSN_LIST_list (&deps->pending_jump_insns);
1785 if (!deps->readonly)
1787 free_EXPR_LIST_list (&deps->pending_write_mems);
1788 deps->pending_write_list_length = 0;
1790 deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
1791 deps->pending_flush_length = 1;
1793 mark_as_hard = false;
1796 /* Instruction which dependencies we are analyzing. */
1797 static rtx cur_insn = NULL_RTX;
1799 /* Implement hooks for haifa scheduler. */
1801 static void
1802 haifa_start_insn (rtx insn)
1804 gcc_assert (insn && !cur_insn);
1806 cur_insn = insn;
1809 static void
1810 haifa_finish_insn (void)
1812 cur_insn = NULL;
1815 void
1816 haifa_note_reg_set (int regno)
1818 SET_REGNO_REG_SET (reg_pending_sets, regno);
1821 void
1822 haifa_note_reg_clobber (int regno)
1824 SET_REGNO_REG_SET (reg_pending_clobbers, regno);
1827 void
1828 haifa_note_reg_use (int regno)
1830 SET_REGNO_REG_SET (reg_pending_uses, regno);
1833 static void
1834 haifa_note_mem_dep (rtx mem, rtx pending_mem, rtx pending_insn, ds_t ds)
1836 if (!(ds & SPECULATIVE))
1838 mem = NULL_RTX;
1839 pending_mem = NULL_RTX;
1841 else
1842 gcc_assert (ds & BEGIN_DATA);
1845 dep_def _dep, *dep = &_dep;
1847 init_dep_1 (dep, pending_insn, cur_insn, ds_to_dt (ds),
1848 current_sched_info->flags & USE_DEPS_LIST ? ds : 0);
1849 DEP_NONREG (dep) = 1;
1850 maybe_add_or_update_dep_1 (dep, false, pending_mem, mem);
1855 static void
1856 haifa_note_dep (rtx elem, ds_t ds)
1858 dep_def _dep;
1859 dep_t dep = &_dep;
1861 init_dep (dep, elem, cur_insn, ds_to_dt (ds));
1862 if (mark_as_hard)
1863 DEP_NONREG (dep) = 1;
1864 maybe_add_or_update_dep_1 (dep, false, NULL_RTX, NULL_RTX);
1867 static void
1868 note_reg_use (int r)
1870 if (sched_deps_info->note_reg_use)
1871 sched_deps_info->note_reg_use (r);
1874 static void
1875 note_reg_set (int r)
1877 if (sched_deps_info->note_reg_set)
1878 sched_deps_info->note_reg_set (r);
1881 static void
1882 note_reg_clobber (int r)
1884 if (sched_deps_info->note_reg_clobber)
1885 sched_deps_info->note_reg_clobber (r);
1888 static void
1889 note_mem_dep (rtx m1, rtx m2, rtx e, ds_t ds)
1891 if (sched_deps_info->note_mem_dep)
1892 sched_deps_info->note_mem_dep (m1, m2, e, ds);
1895 static void
1896 note_dep (rtx e, ds_t ds)
1898 if (sched_deps_info->note_dep)
1899 sched_deps_info->note_dep (e, ds);
1902 /* Return corresponding to DS reg_note. */
1903 enum reg_note
1904 ds_to_dt (ds_t ds)
1906 if (ds & DEP_TRUE)
1907 return REG_DEP_TRUE;
1908 else if (ds & DEP_OUTPUT)
1909 return REG_DEP_OUTPUT;
1910 else if (ds & DEP_ANTI)
1911 return REG_DEP_ANTI;
1912 else
1914 gcc_assert (ds & DEP_CONTROL);
1915 return REG_DEP_CONTROL;
1921 /* Functions for computation of info needed for register pressure
1922 sensitive insn scheduling. */
1925 /* Allocate and return reg_use_data structure for REGNO and INSN. */
1926 static struct reg_use_data *
1927 create_insn_reg_use (int regno, rtx insn)
1929 struct reg_use_data *use;
1931 use = (struct reg_use_data *) xmalloc (sizeof (struct reg_use_data));
1932 use->regno = regno;
1933 use->insn = insn;
1934 use->next_insn_use = INSN_REG_USE_LIST (insn);
1935 INSN_REG_USE_LIST (insn) = use;
1936 return use;
1939 /* Allocate and return reg_set_data structure for REGNO and INSN. */
1940 static struct reg_set_data *
1941 create_insn_reg_set (int regno, rtx insn)
1943 struct reg_set_data *set;
1945 set = (struct reg_set_data *) xmalloc (sizeof (struct reg_set_data));
1946 set->regno = regno;
1947 set->insn = insn;
1948 set->next_insn_set = INSN_REG_SET_LIST (insn);
1949 INSN_REG_SET_LIST (insn) = set;
1950 return set;
1953 /* Set up insn register uses for INSN and dependency context DEPS. */
1954 static void
1955 setup_insn_reg_uses (struct deps_desc *deps, rtx insn)
1957 unsigned i;
1958 reg_set_iterator rsi;
1959 rtx list;
1960 struct reg_use_data *use, *use2, *next;
1961 struct deps_reg *reg_last;
1963 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
1965 if (i < FIRST_PSEUDO_REGISTER
1966 && TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
1967 continue;
1969 if (find_regno_note (insn, REG_DEAD, i) == NULL_RTX
1970 && ! REGNO_REG_SET_P (reg_pending_sets, i)
1971 && ! REGNO_REG_SET_P (reg_pending_clobbers, i))
1972 /* Ignore use which is not dying. */
1973 continue;
1975 use = create_insn_reg_use (i, insn);
1976 use->next_regno_use = use;
1977 reg_last = &deps->reg_last[i];
1979 /* Create the cycle list of uses. */
1980 for (list = reg_last->uses; list; list = XEXP (list, 1))
1982 use2 = create_insn_reg_use (i, XEXP (list, 0));
1983 next = use->next_regno_use;
1984 use->next_regno_use = use2;
1985 use2->next_regno_use = next;
1990 /* Register pressure info for the currently processed insn. */
1991 static struct reg_pressure_data reg_pressure_info[N_REG_CLASSES];
1993 /* Return TRUE if INSN has the use structure for REGNO. */
1994 static bool
1995 insn_use_p (rtx insn, int regno)
1997 struct reg_use_data *use;
1999 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
2000 if (use->regno == regno)
2001 return true;
2002 return false;
2005 /* Update the register pressure info after birth of pseudo register REGNO
2006 in INSN. Arguments CLOBBER_P and UNUSED_P say correspondingly that
2007 the register is in clobber or unused after the insn. */
2008 static void
2009 mark_insn_pseudo_birth (rtx insn, int regno, bool clobber_p, bool unused_p)
2011 int incr, new_incr;
2012 enum reg_class cl;
2014 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
2015 cl = sched_regno_pressure_class[regno];
2016 if (cl != NO_REGS)
2018 incr = ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (regno)];
2019 if (clobber_p)
2021 new_incr = reg_pressure_info[cl].clobber_increase + incr;
2022 reg_pressure_info[cl].clobber_increase = new_incr;
2024 else if (unused_p)
2026 new_incr = reg_pressure_info[cl].unused_set_increase + incr;
2027 reg_pressure_info[cl].unused_set_increase = new_incr;
2029 else
2031 new_incr = reg_pressure_info[cl].set_increase + incr;
2032 reg_pressure_info[cl].set_increase = new_incr;
2033 if (! insn_use_p (insn, regno))
2034 reg_pressure_info[cl].change += incr;
2035 create_insn_reg_set (regno, insn);
2037 gcc_assert (new_incr < (1 << INCREASE_BITS));
2041 /* Like mark_insn_pseudo_regno_birth except that NREGS saying how many
2042 hard registers involved in the birth. */
2043 static void
2044 mark_insn_hard_regno_birth (rtx insn, int regno, int nregs,
2045 bool clobber_p, bool unused_p)
2047 enum reg_class cl;
2048 int new_incr, last = regno + nregs;
2050 while (regno < last)
2052 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2053 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
2055 cl = sched_regno_pressure_class[regno];
2056 if (cl != NO_REGS)
2058 if (clobber_p)
2060 new_incr = reg_pressure_info[cl].clobber_increase + 1;
2061 reg_pressure_info[cl].clobber_increase = new_incr;
2063 else if (unused_p)
2065 new_incr = reg_pressure_info[cl].unused_set_increase + 1;
2066 reg_pressure_info[cl].unused_set_increase = new_incr;
2068 else
2070 new_incr = reg_pressure_info[cl].set_increase + 1;
2071 reg_pressure_info[cl].set_increase = new_incr;
2072 if (! insn_use_p (insn, regno))
2073 reg_pressure_info[cl].change += 1;
2074 create_insn_reg_set (regno, insn);
2076 gcc_assert (new_incr < (1 << INCREASE_BITS));
2079 regno++;
2083 /* Update the register pressure info after birth of pseudo or hard
2084 register REG in INSN. Arguments CLOBBER_P and UNUSED_P say
2085 correspondingly that the register is in clobber or unused after the
2086 insn. */
2087 static void
2088 mark_insn_reg_birth (rtx insn, rtx reg, bool clobber_p, bool unused_p)
2090 int regno;
2092 if (GET_CODE (reg) == SUBREG)
2093 reg = SUBREG_REG (reg);
2095 if (! REG_P (reg))
2096 return;
2098 regno = REGNO (reg);
2099 if (regno < FIRST_PSEUDO_REGISTER)
2100 mark_insn_hard_regno_birth (insn, regno,
2101 hard_regno_nregs[regno][GET_MODE (reg)],
2102 clobber_p, unused_p);
2103 else
2104 mark_insn_pseudo_birth (insn, regno, clobber_p, unused_p);
2107 /* Update the register pressure info after death of pseudo register
2108 REGNO. */
2109 static void
2110 mark_pseudo_death (int regno)
2112 int incr;
2113 enum reg_class cl;
2115 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
2116 cl = sched_regno_pressure_class[regno];
2117 if (cl != NO_REGS)
2119 incr = ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (regno)];
2120 reg_pressure_info[cl].change -= incr;
2124 /* Like mark_pseudo_death except that NREGS saying how many hard
2125 registers involved in the death. */
2126 static void
2127 mark_hard_regno_death (int regno, int nregs)
2129 enum reg_class cl;
2130 int last = regno + nregs;
2132 while (regno < last)
2134 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2135 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
2137 cl = sched_regno_pressure_class[regno];
2138 if (cl != NO_REGS)
2139 reg_pressure_info[cl].change -= 1;
2141 regno++;
2145 /* Update the register pressure info after death of pseudo or hard
2146 register REG. */
2147 static void
2148 mark_reg_death (rtx reg)
2150 int regno;
2152 if (GET_CODE (reg) == SUBREG)
2153 reg = SUBREG_REG (reg);
2155 if (! REG_P (reg))
2156 return;
2158 regno = REGNO (reg);
2159 if (regno < FIRST_PSEUDO_REGISTER)
2160 mark_hard_regno_death (regno, hard_regno_nregs[regno][GET_MODE (reg)]);
2161 else
2162 mark_pseudo_death (regno);
2165 /* Process SETTER of REG. DATA is an insn containing the setter. */
2166 static void
2167 mark_insn_reg_store (rtx reg, const_rtx setter, void *data)
2169 if (setter != NULL_RTX && GET_CODE (setter) != SET)
2170 return;
2171 mark_insn_reg_birth
2172 ((rtx) data, reg, false,
2173 find_reg_note ((const_rtx) data, REG_UNUSED, reg) != NULL_RTX);
2176 /* Like mark_insn_reg_store except notice just CLOBBERs; ignore SETs. */
2177 static void
2178 mark_insn_reg_clobber (rtx reg, const_rtx setter, void *data)
2180 if (GET_CODE (setter) == CLOBBER)
2181 mark_insn_reg_birth ((rtx) data, reg, true, false);
2184 /* Set up reg pressure info related to INSN. */
2185 void
2186 init_insn_reg_pressure_info (rtx insn)
2188 int i, len;
2189 enum reg_class cl;
2190 static struct reg_pressure_data *pressure_info;
2191 rtx link;
2193 gcc_assert (sched_pressure != SCHED_PRESSURE_NONE);
2195 if (! INSN_P (insn))
2196 return;
2198 for (i = 0; i < ira_pressure_classes_num; i++)
2200 cl = ira_pressure_classes[i];
2201 reg_pressure_info[cl].clobber_increase = 0;
2202 reg_pressure_info[cl].set_increase = 0;
2203 reg_pressure_info[cl].unused_set_increase = 0;
2204 reg_pressure_info[cl].change = 0;
2207 note_stores (PATTERN (insn), mark_insn_reg_clobber, insn);
2209 note_stores (PATTERN (insn), mark_insn_reg_store, insn);
2211 #ifdef AUTO_INC_DEC
2212 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2213 if (REG_NOTE_KIND (link) == REG_INC)
2214 mark_insn_reg_store (XEXP (link, 0), NULL_RTX, insn);
2215 #endif
2217 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2218 if (REG_NOTE_KIND (link) == REG_DEAD)
2219 mark_reg_death (XEXP (link, 0));
2221 len = sizeof (struct reg_pressure_data) * ira_pressure_classes_num;
2222 pressure_info
2223 = INSN_REG_PRESSURE (insn) = (struct reg_pressure_data *) xmalloc (len);
2224 if (sched_pressure == SCHED_PRESSURE_WEIGHTED)
2225 INSN_MAX_REG_PRESSURE (insn) = (int *) xcalloc (ira_pressure_classes_num
2226 * sizeof (int), 1);
2227 for (i = 0; i < ira_pressure_classes_num; i++)
2229 cl = ira_pressure_classes[i];
2230 pressure_info[i].clobber_increase
2231 = reg_pressure_info[cl].clobber_increase;
2232 pressure_info[i].set_increase = reg_pressure_info[cl].set_increase;
2233 pressure_info[i].unused_set_increase
2234 = reg_pressure_info[cl].unused_set_increase;
2235 pressure_info[i].change = reg_pressure_info[cl].change;
2242 /* Internal variable for sched_analyze_[12] () functions.
2243 If it is nonzero, this means that sched_analyze_[12] looks
2244 at the most toplevel SET. */
2245 static bool can_start_lhs_rhs_p;
2247 /* Extend reg info for the deps context DEPS given that
2248 we have just generated a register numbered REGNO. */
2249 static void
2250 extend_deps_reg_info (struct deps_desc *deps, int regno)
2252 int max_regno = regno + 1;
2254 gcc_assert (!reload_completed);
2256 /* In a readonly context, it would not hurt to extend info,
2257 but it should not be needed. */
2258 if (reload_completed && deps->readonly)
2260 deps->max_reg = max_regno;
2261 return;
2264 if (max_regno > deps->max_reg)
2266 deps->reg_last = XRESIZEVEC (struct deps_reg, deps->reg_last,
2267 max_regno);
2268 memset (&deps->reg_last[deps->max_reg],
2269 0, (max_regno - deps->max_reg)
2270 * sizeof (struct deps_reg));
2271 deps->max_reg = max_regno;
2275 /* Extends REG_INFO_P if needed. */
2276 void
2277 maybe_extend_reg_info_p (void)
2279 /* Extend REG_INFO_P, if needed. */
2280 if ((unsigned int)max_regno - 1 >= reg_info_p_size)
2282 size_t new_reg_info_p_size = max_regno + 128;
2284 gcc_assert (!reload_completed && sel_sched_p ());
2286 reg_info_p = (struct reg_info_t *) xrecalloc (reg_info_p,
2287 new_reg_info_p_size,
2288 reg_info_p_size,
2289 sizeof (*reg_info_p));
2290 reg_info_p_size = new_reg_info_p_size;
2294 /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
2295 The type of the reference is specified by REF and can be SET,
2296 CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE. */
2298 static void
2299 sched_analyze_reg (struct deps_desc *deps, int regno, enum machine_mode mode,
2300 enum rtx_code ref, rtx insn)
2302 /* We could emit new pseudos in renaming. Extend the reg structures. */
2303 if (!reload_completed && sel_sched_p ()
2304 && (regno >= max_reg_num () - 1 || regno >= deps->max_reg))
2305 extend_deps_reg_info (deps, regno);
2307 maybe_extend_reg_info_p ();
2309 /* A hard reg in a wide mode may really be multiple registers.
2310 If so, mark all of them just like the first. */
2311 if (regno < FIRST_PSEUDO_REGISTER)
2313 int i = hard_regno_nregs[regno][mode];
2314 if (ref == SET)
2316 while (--i >= 0)
2317 note_reg_set (regno + i);
2319 else if (ref == USE)
2321 while (--i >= 0)
2322 note_reg_use (regno + i);
2324 else
2326 while (--i >= 0)
2327 note_reg_clobber (regno + i);
2331 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
2332 it does not reload. Ignore these as they have served their
2333 purpose already. */
2334 else if (regno >= deps->max_reg)
2336 enum rtx_code code = GET_CODE (PATTERN (insn));
2337 gcc_assert (code == USE || code == CLOBBER);
2340 else
2342 if (ref == SET)
2343 note_reg_set (regno);
2344 else if (ref == USE)
2345 note_reg_use (regno);
2346 else
2347 note_reg_clobber (regno);
2349 /* Pseudos that are REG_EQUIV to something may be replaced
2350 by that during reloading. We need only add dependencies for
2351 the address in the REG_EQUIV note. */
2352 if (!reload_completed && get_reg_known_equiv_p (regno))
2354 rtx t = get_reg_known_value (regno);
2355 if (MEM_P (t))
2356 sched_analyze_2 (deps, XEXP (t, 0), insn);
2359 /* Don't let it cross a call after scheduling if it doesn't
2360 already cross one. */
2361 if (REG_N_CALLS_CROSSED (regno) == 0)
2363 if (!deps->readonly && ref == USE && !DEBUG_INSN_P (insn))
2364 deps->sched_before_next_call
2365 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
2366 else
2367 add_dependence_list (insn, deps->last_function_call, 1,
2368 REG_DEP_ANTI, false);
2373 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
2374 rtx, X, creating all dependencies generated by the write to the
2375 destination of X, and reads of everything mentioned. */
2377 static void
2378 sched_analyze_1 (struct deps_desc *deps, rtx x, rtx insn)
2380 rtx dest = XEXP (x, 0);
2381 enum rtx_code code = GET_CODE (x);
2382 bool cslr_p = can_start_lhs_rhs_p;
2384 can_start_lhs_rhs_p = false;
2386 gcc_assert (dest);
2387 if (dest == 0)
2388 return;
2390 if (cslr_p && sched_deps_info->start_lhs)
2391 sched_deps_info->start_lhs (dest);
2393 if (GET_CODE (dest) == PARALLEL)
2395 int i;
2397 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
2398 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
2399 sched_analyze_1 (deps,
2400 gen_rtx_CLOBBER (VOIDmode,
2401 XEXP (XVECEXP (dest, 0, i), 0)),
2402 insn);
2404 if (cslr_p && sched_deps_info->finish_lhs)
2405 sched_deps_info->finish_lhs ();
2407 if (code == SET)
2409 can_start_lhs_rhs_p = cslr_p;
2411 sched_analyze_2 (deps, SET_SRC (x), insn);
2413 can_start_lhs_rhs_p = false;
2416 return;
2419 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
2420 || GET_CODE (dest) == ZERO_EXTRACT)
2422 if (GET_CODE (dest) == STRICT_LOW_PART
2423 || GET_CODE (dest) == ZERO_EXTRACT
2424 || df_read_modify_subreg_p (dest))
2426 /* These both read and modify the result. We must handle
2427 them as writes to get proper dependencies for following
2428 instructions. We must handle them as reads to get proper
2429 dependencies from this to previous instructions.
2430 Thus we need to call sched_analyze_2. */
2432 sched_analyze_2 (deps, XEXP (dest, 0), insn);
2434 if (GET_CODE (dest) == ZERO_EXTRACT)
2436 /* The second and third arguments are values read by this insn. */
2437 sched_analyze_2 (deps, XEXP (dest, 1), insn);
2438 sched_analyze_2 (deps, XEXP (dest, 2), insn);
2440 dest = XEXP (dest, 0);
2443 if (REG_P (dest))
2445 int regno = REGNO (dest);
2446 enum machine_mode mode = GET_MODE (dest);
2448 sched_analyze_reg (deps, regno, mode, code, insn);
2450 #ifdef STACK_REGS
2451 /* Treat all writes to a stack register as modifying the TOS. */
2452 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
2454 /* Avoid analyzing the same register twice. */
2455 if (regno != FIRST_STACK_REG)
2456 sched_analyze_reg (deps, FIRST_STACK_REG, mode, code, insn);
2458 add_to_hard_reg_set (&implicit_reg_pending_uses, mode,
2459 FIRST_STACK_REG);
2461 #endif
2463 else if (MEM_P (dest))
2465 /* Writing memory. */
2466 rtx t = dest;
2468 if (sched_deps_info->use_cselib)
2470 enum machine_mode address_mode = get_address_mode (dest);
2472 t = shallow_copy_rtx (dest);
2473 cselib_lookup_from_insn (XEXP (t, 0), address_mode, 1,
2474 GET_MODE (t), insn);
2475 XEXP (t, 0)
2476 = cselib_subst_to_values_from_insn (XEXP (t, 0), GET_MODE (t),
2477 insn);
2479 t = canon_rtx (t);
2481 /* Pending lists can't get larger with a readonly context. */
2482 if (!deps->readonly
2483 && ((deps->pending_read_list_length + deps->pending_write_list_length)
2484 > MAX_PENDING_LIST_LENGTH))
2486 /* Flush all pending reads and writes to prevent the pending lists
2487 from getting any larger. Insn scheduling runs too slowly when
2488 these lists get long. When compiling GCC with itself,
2489 this flush occurs 8 times for sparc, and 10 times for m88k using
2490 the default value of 32. */
2491 flush_pending_lists (deps, insn, false, true);
2493 else
2495 rtx pending, pending_mem;
2497 pending = deps->pending_read_insns;
2498 pending_mem = deps->pending_read_mems;
2499 while (pending)
2501 if (anti_dependence (XEXP (pending_mem, 0), t)
2502 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2503 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2504 DEP_ANTI);
2506 pending = XEXP (pending, 1);
2507 pending_mem = XEXP (pending_mem, 1);
2510 pending = deps->pending_write_insns;
2511 pending_mem = deps->pending_write_mems;
2512 while (pending)
2514 if (output_dependence (XEXP (pending_mem, 0), t)
2515 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2516 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2517 DEP_OUTPUT);
2519 pending = XEXP (pending, 1);
2520 pending_mem = XEXP (pending_mem, 1);
2523 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
2524 REG_DEP_ANTI, true);
2525 add_dependence_list (insn, deps->pending_jump_insns, 1,
2526 REG_DEP_CONTROL, true);
2528 if (!deps->readonly)
2529 add_insn_mem_dependence (deps, false, insn, dest);
2531 sched_analyze_2 (deps, XEXP (dest, 0), insn);
2534 if (cslr_p && sched_deps_info->finish_lhs)
2535 sched_deps_info->finish_lhs ();
2537 /* Analyze reads. */
2538 if (GET_CODE (x) == SET)
2540 can_start_lhs_rhs_p = cslr_p;
2542 sched_analyze_2 (deps, SET_SRC (x), insn);
2544 can_start_lhs_rhs_p = false;
2548 /* Analyze the uses of memory and registers in rtx X in INSN. */
2549 static void
2550 sched_analyze_2 (struct deps_desc *deps, rtx x, rtx insn)
2552 int i;
2553 int j;
2554 enum rtx_code code;
2555 const char *fmt;
2556 bool cslr_p = can_start_lhs_rhs_p;
2558 can_start_lhs_rhs_p = false;
2560 gcc_assert (x);
2561 if (x == 0)
2562 return;
2564 if (cslr_p && sched_deps_info->start_rhs)
2565 sched_deps_info->start_rhs (x);
2567 code = GET_CODE (x);
2569 switch (code)
2571 CASE_CONST_ANY:
2572 case SYMBOL_REF:
2573 case CONST:
2574 case LABEL_REF:
2575 /* Ignore constants. */
2576 if (cslr_p && sched_deps_info->finish_rhs)
2577 sched_deps_info->finish_rhs ();
2579 return;
2581 #ifdef HAVE_cc0
2582 case CC0:
2583 /* User of CC0 depends on immediately preceding insn. */
2584 SCHED_GROUP_P (insn) = 1;
2585 /* Don't move CC0 setter to another block (it can set up the
2586 same flag for previous CC0 users which is safe). */
2587 CANT_MOVE (prev_nonnote_insn (insn)) = 1;
2589 if (cslr_p && sched_deps_info->finish_rhs)
2590 sched_deps_info->finish_rhs ();
2592 return;
2593 #endif
2595 case REG:
2597 int regno = REGNO (x);
2598 enum machine_mode mode = GET_MODE (x);
2600 sched_analyze_reg (deps, regno, mode, USE, insn);
2602 #ifdef STACK_REGS
2603 /* Treat all reads of a stack register as modifying the TOS. */
2604 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
2606 /* Avoid analyzing the same register twice. */
2607 if (regno != FIRST_STACK_REG)
2608 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
2609 sched_analyze_reg (deps, FIRST_STACK_REG, mode, SET, insn);
2611 #endif
2613 if (cslr_p && sched_deps_info->finish_rhs)
2614 sched_deps_info->finish_rhs ();
2616 return;
2619 case MEM:
2621 /* Reading memory. */
2622 rtx u;
2623 rtx pending, pending_mem;
2624 rtx t = x;
2626 if (sched_deps_info->use_cselib)
2628 enum machine_mode address_mode = get_address_mode (t);
2630 t = shallow_copy_rtx (t);
2631 cselib_lookup_from_insn (XEXP (t, 0), address_mode, 1,
2632 GET_MODE (t), insn);
2633 XEXP (t, 0)
2634 = cselib_subst_to_values_from_insn (XEXP (t, 0), GET_MODE (t),
2635 insn);
2638 if (!DEBUG_INSN_P (insn))
2640 t = canon_rtx (t);
2641 pending = deps->pending_read_insns;
2642 pending_mem = deps->pending_read_mems;
2643 while (pending)
2645 if (read_dependence (XEXP (pending_mem, 0), t)
2646 && ! sched_insns_conditions_mutex_p (insn,
2647 XEXP (pending, 0)))
2648 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2649 DEP_ANTI);
2651 pending = XEXP (pending, 1);
2652 pending_mem = XEXP (pending_mem, 1);
2655 pending = deps->pending_write_insns;
2656 pending_mem = deps->pending_write_mems;
2657 while (pending)
2659 if (true_dependence (XEXP (pending_mem, 0), VOIDmode, t)
2660 && ! sched_insns_conditions_mutex_p (insn,
2661 XEXP (pending, 0)))
2662 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2663 sched_deps_info->generate_spec_deps
2664 ? BEGIN_DATA | DEP_TRUE : DEP_TRUE);
2666 pending = XEXP (pending, 1);
2667 pending_mem = XEXP (pending_mem, 1);
2670 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
2671 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
2673 for (u = deps->pending_jump_insns; u; u = XEXP (u, 1))
2674 if (deps_may_trap_p (x))
2676 if ((sched_deps_info->generate_spec_deps)
2677 && sel_sched_p () && (spec_info->mask & BEGIN_CONTROL))
2679 ds_t ds = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
2680 MAX_DEP_WEAK);
2682 note_dep (XEXP (u, 0), ds);
2684 else
2685 add_dependence (insn, XEXP (u, 0), REG_DEP_CONTROL);
2689 /* Always add these dependencies to pending_reads, since
2690 this insn may be followed by a write. */
2691 if (!deps->readonly)
2692 add_insn_mem_dependence (deps, true, insn, x);
2694 sched_analyze_2 (deps, XEXP (x, 0), insn);
2696 if (cslr_p && sched_deps_info->finish_rhs)
2697 sched_deps_info->finish_rhs ();
2699 return;
2702 /* Force pending stores to memory in case a trap handler needs them. */
2703 case TRAP_IF:
2704 flush_pending_lists (deps, insn, true, false);
2705 break;
2707 case PREFETCH:
2708 if (PREFETCH_SCHEDULE_BARRIER_P (x))
2709 reg_pending_barrier = TRUE_BARRIER;
2710 /* Prefetch insn contains addresses only. So if the prefetch
2711 address has no registers, there will be no dependencies on
2712 the prefetch insn. This is wrong with result code
2713 correctness point of view as such prefetch can be moved below
2714 a jump insn which usually generates MOVE_BARRIER preventing
2715 to move insns containing registers or memories through the
2716 barrier. It is also wrong with generated code performance
2717 point of view as prefetch withouth dependecies will have a
2718 tendency to be issued later instead of earlier. It is hard
2719 to generate accurate dependencies for prefetch insns as
2720 prefetch has only the start address but it is better to have
2721 something than nothing. */
2722 if (!deps->readonly)
2723 add_insn_mem_dependence (deps, true, insn,
2724 gen_rtx_MEM (Pmode, XEXP (PATTERN (insn), 0)));
2725 break;
2727 case UNSPEC_VOLATILE:
2728 flush_pending_lists (deps, insn, true, true);
2729 /* FALLTHRU */
2731 case ASM_OPERANDS:
2732 case ASM_INPUT:
2734 /* Traditional and volatile asm instructions must be considered to use
2735 and clobber all hard registers, all pseudo-registers and all of
2736 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
2738 Consider for instance a volatile asm that changes the fpu rounding
2739 mode. An insn should not be moved across this even if it only uses
2740 pseudo-regs because it might give an incorrectly rounded result. */
2741 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
2742 reg_pending_barrier = TRUE_BARRIER;
2744 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
2745 We can not just fall through here since then we would be confused
2746 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
2747 traditional asms unlike their normal usage. */
2749 if (code == ASM_OPERANDS)
2751 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
2752 sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
2754 if (cslr_p && sched_deps_info->finish_rhs)
2755 sched_deps_info->finish_rhs ();
2757 return;
2759 break;
2762 case PRE_DEC:
2763 case POST_DEC:
2764 case PRE_INC:
2765 case POST_INC:
2766 /* These both read and modify the result. We must handle them as writes
2767 to get proper dependencies for following instructions. We must handle
2768 them as reads to get proper dependencies from this to previous
2769 instructions. Thus we need to pass them to both sched_analyze_1
2770 and sched_analyze_2. We must call sched_analyze_2 first in order
2771 to get the proper antecedent for the read. */
2772 sched_analyze_2 (deps, XEXP (x, 0), insn);
2773 sched_analyze_1 (deps, x, insn);
2775 if (cslr_p && sched_deps_info->finish_rhs)
2776 sched_deps_info->finish_rhs ();
2778 return;
2780 case POST_MODIFY:
2781 case PRE_MODIFY:
2782 /* op0 = op0 + op1 */
2783 sched_analyze_2 (deps, XEXP (x, 0), insn);
2784 sched_analyze_2 (deps, XEXP (x, 1), insn);
2785 sched_analyze_1 (deps, x, insn);
2787 if (cslr_p && sched_deps_info->finish_rhs)
2788 sched_deps_info->finish_rhs ();
2790 return;
2792 default:
2793 break;
2796 /* Other cases: walk the insn. */
2797 fmt = GET_RTX_FORMAT (code);
2798 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2800 if (fmt[i] == 'e')
2801 sched_analyze_2 (deps, XEXP (x, i), insn);
2802 else if (fmt[i] == 'E')
2803 for (j = 0; j < XVECLEN (x, i); j++)
2804 sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
2807 if (cslr_p && sched_deps_info->finish_rhs)
2808 sched_deps_info->finish_rhs ();
2811 /* Analyze an INSN with pattern X to find all dependencies. */
2812 static void
2813 sched_analyze_insn (struct deps_desc *deps, rtx x, rtx insn)
2815 RTX_CODE code = GET_CODE (x);
2816 rtx link;
2817 unsigned i;
2818 reg_set_iterator rsi;
2820 if (! reload_completed)
2822 HARD_REG_SET temp;
2824 extract_insn (insn);
2825 preprocess_constraints ();
2826 ira_implicitly_set_insn_hard_regs (&temp);
2827 AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
2828 IOR_HARD_REG_SET (implicit_reg_pending_clobbers, temp);
2831 can_start_lhs_rhs_p = (NONJUMP_INSN_P (insn)
2832 && code == SET);
2834 if (may_trap_p (x))
2835 /* Avoid moving trapping instructions across function calls that might
2836 not always return. */
2837 add_dependence_list (insn, deps->last_function_call_may_noreturn,
2838 1, REG_DEP_ANTI, true);
2840 /* We must avoid creating a situation in which two successors of the
2841 current block have different unwind info after scheduling. If at any
2842 point the two paths re-join this leads to incorrect unwind info. */
2843 /* ??? There are certain situations involving a forced frame pointer in
2844 which, with extra effort, we could fix up the unwind info at a later
2845 CFG join. However, it seems better to notice these cases earlier
2846 during prologue generation and avoid marking the frame pointer setup
2847 as frame-related at all. */
2848 if (RTX_FRAME_RELATED_P (insn))
2850 /* Make sure prologue insn is scheduled before next jump. */
2851 deps->sched_before_next_jump
2852 = alloc_INSN_LIST (insn, deps->sched_before_next_jump);
2854 /* Make sure epilogue insn is scheduled after preceding jumps. */
2855 add_dependence_list (insn, deps->pending_jump_insns, 1, REG_DEP_ANTI,
2856 true);
2859 if (code == COND_EXEC)
2861 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
2863 /* ??? Should be recording conditions so we reduce the number of
2864 false dependencies. */
2865 x = COND_EXEC_CODE (x);
2866 code = GET_CODE (x);
2868 if (code == SET || code == CLOBBER)
2870 sched_analyze_1 (deps, x, insn);
2872 /* Bare clobber insns are used for letting life analysis, reg-stack
2873 and others know that a value is dead. Depend on the last call
2874 instruction so that reg-stack won't get confused. */
2875 if (code == CLOBBER)
2876 add_dependence_list (insn, deps->last_function_call, 1,
2877 REG_DEP_OUTPUT, true);
2879 else if (code == PARALLEL)
2881 for (i = XVECLEN (x, 0); i--;)
2883 rtx sub = XVECEXP (x, 0, i);
2884 code = GET_CODE (sub);
2886 if (code == COND_EXEC)
2888 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
2889 sub = COND_EXEC_CODE (sub);
2890 code = GET_CODE (sub);
2892 if (code == SET || code == CLOBBER)
2893 sched_analyze_1 (deps, sub, insn);
2894 else
2895 sched_analyze_2 (deps, sub, insn);
2898 else
2899 sched_analyze_2 (deps, x, insn);
2901 /* Mark registers CLOBBERED or used by called function. */
2902 if (CALL_P (insn))
2904 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2906 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
2907 sched_analyze_1 (deps, XEXP (link, 0), insn);
2908 else if (GET_CODE (XEXP (link, 0)) != SET)
2909 sched_analyze_2 (deps, XEXP (link, 0), insn);
2911 /* Don't schedule anything after a tail call, tail call needs
2912 to use at least all call-saved registers. */
2913 if (SIBLING_CALL_P (insn))
2914 reg_pending_barrier = TRUE_BARRIER;
2915 else if (find_reg_note (insn, REG_SETJMP, NULL))
2916 reg_pending_barrier = MOVE_BARRIER;
2919 if (JUMP_P (insn))
2921 rtx next;
2922 next = next_nonnote_nondebug_insn (insn);
2923 if (next && BARRIER_P (next))
2924 reg_pending_barrier = MOVE_BARRIER;
2925 else
2927 rtx pending, pending_mem;
2929 if (sched_deps_info->compute_jump_reg_dependencies)
2931 (*sched_deps_info->compute_jump_reg_dependencies)
2932 (insn, reg_pending_control_uses);
2934 /* Make latency of jump equal to 0 by using anti-dependence. */
2935 EXECUTE_IF_SET_IN_REG_SET (reg_pending_control_uses, 0, i, rsi)
2937 struct deps_reg *reg_last = &deps->reg_last[i];
2938 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI,
2939 false);
2940 add_dependence_list (insn, reg_last->implicit_sets,
2941 0, REG_DEP_ANTI, false);
2942 add_dependence_list (insn, reg_last->clobbers, 0,
2943 REG_DEP_ANTI, false);
2947 /* All memory writes and volatile reads must happen before the
2948 jump. Non-volatile reads must happen before the jump iff
2949 the result is needed by the above register used mask. */
2951 pending = deps->pending_write_insns;
2952 pending_mem = deps->pending_write_mems;
2953 while (pending)
2955 if (! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2956 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
2957 pending = XEXP (pending, 1);
2958 pending_mem = XEXP (pending_mem, 1);
2961 pending = deps->pending_read_insns;
2962 pending_mem = deps->pending_read_mems;
2963 while (pending)
2965 if (MEM_VOLATILE_P (XEXP (pending_mem, 0))
2966 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2967 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
2968 pending = XEXP (pending, 1);
2969 pending_mem = XEXP (pending_mem, 1);
2972 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
2973 REG_DEP_ANTI, true);
2974 add_dependence_list (insn, deps->pending_jump_insns, 1,
2975 REG_DEP_ANTI, true);
2979 /* If this instruction can throw an exception, then moving it changes
2980 where block boundaries fall. This is mighty confusing elsewhere.
2981 Therefore, prevent such an instruction from being moved. Same for
2982 non-jump instructions that define block boundaries.
2983 ??? Unclear whether this is still necessary in EBB mode. If not,
2984 add_branch_dependences should be adjusted for RGN mode instead. */
2985 if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
2986 || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
2987 reg_pending_barrier = MOVE_BARRIER;
2989 if (sched_pressure != SCHED_PRESSURE_NONE)
2991 setup_insn_reg_uses (deps, insn);
2992 init_insn_reg_pressure_info (insn);
2995 /* Add register dependencies for insn. */
2996 if (DEBUG_INSN_P (insn))
2998 rtx prev = deps->last_debug_insn;
2999 rtx u;
3001 if (!deps->readonly)
3002 deps->last_debug_insn = insn;
3004 if (prev)
3005 add_dependence (insn, prev, REG_DEP_ANTI);
3007 add_dependence_list (insn, deps->last_function_call, 1,
3008 REG_DEP_ANTI, false);
3010 if (!sel_sched_p ())
3011 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
3012 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3014 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
3016 struct deps_reg *reg_last = &deps->reg_last[i];
3017 add_dependence_list (insn, reg_last->sets, 1, REG_DEP_ANTI, false);
3018 /* There's no point in making REG_DEP_CONTROL dependencies for
3019 debug insns. */
3020 add_dependence_list (insn, reg_last->clobbers, 1, REG_DEP_ANTI,
3021 false);
3023 if (!deps->readonly)
3024 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3026 CLEAR_REG_SET (reg_pending_uses);
3028 /* Quite often, a debug insn will refer to stuff in the
3029 previous instruction, but the reason we want this
3030 dependency here is to make sure the scheduler doesn't
3031 gratuitously move a debug insn ahead. This could dirty
3032 DF flags and cause additional analysis that wouldn't have
3033 occurred in compilation without debug insns, and such
3034 additional analysis can modify the generated code. */
3035 prev = PREV_INSN (insn);
3037 if (prev && NONDEBUG_INSN_P (prev))
3038 add_dependence (insn, prev, REG_DEP_ANTI);
3040 else
3042 regset_head set_or_clobbered;
3044 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
3046 struct deps_reg *reg_last = &deps->reg_last[i];
3047 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE, false);
3048 add_dependence_list (insn, reg_last->implicit_sets, 0, REG_DEP_ANTI,
3049 false);
3050 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE,
3051 false);
3053 if (!deps->readonly)
3055 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3056 reg_last->uses_length++;
3060 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3061 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i))
3063 struct deps_reg *reg_last = &deps->reg_last[i];
3064 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE, false);
3065 add_dependence_list (insn, reg_last->implicit_sets, 0,
3066 REG_DEP_ANTI, false);
3067 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE,
3068 false);
3070 if (!deps->readonly)
3072 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3073 reg_last->uses_length++;
3077 if (targetm.sched.exposed_pipeline)
3079 INIT_REG_SET (&set_or_clobbered);
3080 bitmap_ior (&set_or_clobbered, reg_pending_clobbers,
3081 reg_pending_sets);
3082 EXECUTE_IF_SET_IN_REG_SET (&set_or_clobbered, 0, i, rsi)
3084 struct deps_reg *reg_last = &deps->reg_last[i];
3085 rtx list;
3086 for (list = reg_last->uses; list; list = XEXP (list, 1))
3088 rtx other = XEXP (list, 0);
3089 if (INSN_CACHED_COND (other) != const_true_rtx
3090 && refers_to_regno_p (i, i + 1, INSN_CACHED_COND (other), NULL))
3091 INSN_CACHED_COND (other) = const_true_rtx;
3096 /* If the current insn is conditional, we can't free any
3097 of the lists. */
3098 if (sched_has_condition_p (insn))
3100 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
3102 struct deps_reg *reg_last = &deps->reg_last[i];
3103 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
3104 false);
3105 add_dependence_list (insn, reg_last->implicit_sets, 0,
3106 REG_DEP_ANTI, false);
3107 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3108 false);
3109 add_dependence_list (insn, reg_last->control_uses, 0,
3110 REG_DEP_CONTROL, false);
3112 if (!deps->readonly)
3114 reg_last->clobbers
3115 = alloc_INSN_LIST (insn, reg_last->clobbers);
3116 reg_last->clobbers_length++;
3119 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
3121 struct deps_reg *reg_last = &deps->reg_last[i];
3122 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
3123 false);
3124 add_dependence_list (insn, reg_last->implicit_sets, 0,
3125 REG_DEP_ANTI, false);
3126 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT,
3127 false);
3128 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3129 false);
3130 add_dependence_list (insn, reg_last->control_uses, 0,
3131 REG_DEP_CONTROL, false);
3133 if (!deps->readonly)
3134 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3137 else
3139 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
3141 struct deps_reg *reg_last = &deps->reg_last[i];
3142 if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
3143 || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
3145 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3146 REG_DEP_OUTPUT, false);
3147 add_dependence_list_and_free (deps, insn,
3148 &reg_last->implicit_sets, 0,
3149 REG_DEP_ANTI, false);
3150 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3151 REG_DEP_ANTI, false);
3152 add_dependence_list_and_free (deps, insn,
3153 &reg_last->control_uses, 0,
3154 REG_DEP_ANTI, false);
3155 add_dependence_list_and_free (deps, insn,
3156 &reg_last->clobbers, 0,
3157 REG_DEP_OUTPUT, false);
3159 if (!deps->readonly)
3161 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3162 reg_last->clobbers_length = 0;
3163 reg_last->uses_length = 0;
3166 else
3168 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
3169 false);
3170 add_dependence_list (insn, reg_last->implicit_sets, 0,
3171 REG_DEP_ANTI, false);
3172 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3173 false);
3174 add_dependence_list (insn, reg_last->control_uses, 0,
3175 REG_DEP_CONTROL, false);
3178 if (!deps->readonly)
3180 reg_last->clobbers_length++;
3181 reg_last->clobbers
3182 = alloc_INSN_LIST (insn, reg_last->clobbers);
3185 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
3187 struct deps_reg *reg_last = &deps->reg_last[i];
3189 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3190 REG_DEP_OUTPUT, false);
3191 add_dependence_list_and_free (deps, insn,
3192 &reg_last->implicit_sets,
3193 0, REG_DEP_ANTI, false);
3194 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
3195 REG_DEP_OUTPUT, false);
3196 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3197 REG_DEP_ANTI, false);
3198 add_dependence_list (insn, reg_last->control_uses, 0,
3199 REG_DEP_CONTROL, false);
3201 if (!deps->readonly)
3203 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3204 reg_last->uses_length = 0;
3205 reg_last->clobbers_length = 0;
3209 if (!deps->readonly)
3211 EXECUTE_IF_SET_IN_REG_SET (reg_pending_control_uses, 0, i, rsi)
3213 struct deps_reg *reg_last = &deps->reg_last[i];
3214 reg_last->control_uses
3215 = alloc_INSN_LIST (insn, reg_last->control_uses);
3220 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3221 if (TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
3223 struct deps_reg *reg_last = &deps->reg_last[i];
3224 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI, false);
3225 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI, false);
3226 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI, false);
3227 add_dependence_list (insn, reg_last->control_uses, 0, REG_DEP_ANTI,
3228 false);
3230 if (!deps->readonly)
3231 reg_last->implicit_sets
3232 = alloc_INSN_LIST (insn, reg_last->implicit_sets);
3235 if (!deps->readonly)
3237 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
3238 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
3239 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
3240 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3241 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i)
3242 || TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
3243 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
3245 /* Set up the pending barrier found. */
3246 deps->last_reg_pending_barrier = reg_pending_barrier;
3249 CLEAR_REG_SET (reg_pending_uses);
3250 CLEAR_REG_SET (reg_pending_clobbers);
3251 CLEAR_REG_SET (reg_pending_sets);
3252 CLEAR_REG_SET (reg_pending_control_uses);
3253 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
3254 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
3256 /* Add dependencies if a scheduling barrier was found. */
3257 if (reg_pending_barrier)
3259 /* In the case of barrier the most added dependencies are not
3260 real, so we use anti-dependence here. */
3261 if (sched_has_condition_p (insn))
3263 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3265 struct deps_reg *reg_last = &deps->reg_last[i];
3266 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3267 true);
3268 add_dependence_list (insn, reg_last->sets, 0,
3269 reg_pending_barrier == TRUE_BARRIER
3270 ? REG_DEP_TRUE : REG_DEP_ANTI, true);
3271 add_dependence_list (insn, reg_last->implicit_sets, 0,
3272 REG_DEP_ANTI, true);
3273 add_dependence_list (insn, reg_last->clobbers, 0,
3274 reg_pending_barrier == TRUE_BARRIER
3275 ? REG_DEP_TRUE : REG_DEP_ANTI, true);
3278 else
3280 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3282 struct deps_reg *reg_last = &deps->reg_last[i];
3283 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3284 REG_DEP_ANTI, true);
3285 add_dependence_list_and_free (deps, insn,
3286 &reg_last->control_uses, 0,
3287 REG_DEP_CONTROL, true);
3288 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3289 reg_pending_barrier == TRUE_BARRIER
3290 ? REG_DEP_TRUE : REG_DEP_ANTI,
3291 true);
3292 add_dependence_list_and_free (deps, insn,
3293 &reg_last->implicit_sets, 0,
3294 REG_DEP_ANTI, true);
3295 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
3296 reg_pending_barrier == TRUE_BARRIER
3297 ? REG_DEP_TRUE : REG_DEP_ANTI,
3298 true);
3300 if (!deps->readonly)
3302 reg_last->uses_length = 0;
3303 reg_last->clobbers_length = 0;
3308 if (!deps->readonly)
3309 for (i = 0; i < (unsigned)deps->max_reg; i++)
3311 struct deps_reg *reg_last = &deps->reg_last[i];
3312 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3313 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
3316 /* Flush pending lists on jumps, but not on speculative checks. */
3317 if (JUMP_P (insn) && !(sel_sched_p ()
3318 && sel_insn_is_speculation_check (insn)))
3319 flush_pending_lists (deps, insn, true, true);
3321 reg_pending_barrier = NOT_A_BARRIER;
3324 /* If a post-call group is still open, see if it should remain so.
3325 This insn must be a simple move of a hard reg to a pseudo or
3326 vice-versa.
3328 We must avoid moving these insns for correctness on targets
3329 with small register classes, and for special registers like
3330 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
3331 hard regs for all targets. */
3333 if (deps->in_post_call_group_p)
3335 rtx tmp, set = single_set (insn);
3336 int src_regno, dest_regno;
3338 if (set == NULL)
3340 if (DEBUG_INSN_P (insn))
3341 /* We don't want to mark debug insns as part of the same
3342 sched group. We know they really aren't, but if we use
3343 debug insns to tell that a call group is over, we'll
3344 get different code if debug insns are not there and
3345 instructions that follow seem like they should be part
3346 of the call group.
3348 Also, if we did, chain_to_prev_insn would move the
3349 deps of the debug insn to the call insn, modifying
3350 non-debug post-dependency counts of the debug insn
3351 dependencies and otherwise messing with the scheduling
3352 order.
3354 Instead, let such debug insns be scheduled freely, but
3355 keep the call group open in case there are insns that
3356 should be part of it afterwards. Since we grant debug
3357 insns higher priority than even sched group insns, it
3358 will all turn out all right. */
3359 goto debug_dont_end_call_group;
3360 else
3361 goto end_call_group;
3364 tmp = SET_DEST (set);
3365 if (GET_CODE (tmp) == SUBREG)
3366 tmp = SUBREG_REG (tmp);
3367 if (REG_P (tmp))
3368 dest_regno = REGNO (tmp);
3369 else
3370 goto end_call_group;
3372 tmp = SET_SRC (set);
3373 if (GET_CODE (tmp) == SUBREG)
3374 tmp = SUBREG_REG (tmp);
3375 if ((GET_CODE (tmp) == PLUS
3376 || GET_CODE (tmp) == MINUS)
3377 && REG_P (XEXP (tmp, 0))
3378 && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
3379 && dest_regno == STACK_POINTER_REGNUM)
3380 src_regno = STACK_POINTER_REGNUM;
3381 else if (REG_P (tmp))
3382 src_regno = REGNO (tmp);
3383 else
3384 goto end_call_group;
3386 if (src_regno < FIRST_PSEUDO_REGISTER
3387 || dest_regno < FIRST_PSEUDO_REGISTER)
3389 if (!deps->readonly
3390 && deps->in_post_call_group_p == post_call_initial)
3391 deps->in_post_call_group_p = post_call;
3393 if (!sel_sched_p () || sched_emulate_haifa_p)
3395 SCHED_GROUP_P (insn) = 1;
3396 CANT_MOVE (insn) = 1;
3399 else
3401 end_call_group:
3402 if (!deps->readonly)
3403 deps->in_post_call_group_p = not_post_call;
3407 debug_dont_end_call_group:
3408 if ((current_sched_info->flags & DO_SPECULATION)
3409 && !sched_insn_is_legitimate_for_speculation_p (insn, 0))
3410 /* INSN has an internal dependency (e.g. r14 = [r14]) and thus cannot
3411 be speculated. */
3413 if (sel_sched_p ())
3414 sel_mark_hard_insn (insn);
3415 else
3417 sd_iterator_def sd_it;
3418 dep_t dep;
3420 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3421 sd_iterator_cond (&sd_it, &dep);)
3422 change_spec_dep_to_hard (sd_it);
3427 /* Return TRUE if INSN might not always return normally (e.g. call exit,
3428 longjmp, loop forever, ...). */
3429 /* FIXME: Why can't this function just use flags_from_decl_or_type and
3430 test for ECF_NORETURN? */
3431 static bool
3432 call_may_noreturn_p (rtx insn)
3434 rtx call;
3436 /* const or pure calls that aren't looping will always return. */
3437 if (RTL_CONST_OR_PURE_CALL_P (insn)
3438 && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn))
3439 return false;
3441 call = get_call_rtx_from (insn);
3442 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
3444 rtx symbol = XEXP (XEXP (call, 0), 0);
3445 if (SYMBOL_REF_DECL (symbol)
3446 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
3448 if (DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
3449 == BUILT_IN_NORMAL)
3450 switch (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol)))
3452 case BUILT_IN_BCMP:
3453 case BUILT_IN_BCOPY:
3454 case BUILT_IN_BZERO:
3455 case BUILT_IN_INDEX:
3456 case BUILT_IN_MEMCHR:
3457 case BUILT_IN_MEMCMP:
3458 case BUILT_IN_MEMCPY:
3459 case BUILT_IN_MEMMOVE:
3460 case BUILT_IN_MEMPCPY:
3461 case BUILT_IN_MEMSET:
3462 case BUILT_IN_RINDEX:
3463 case BUILT_IN_STPCPY:
3464 case BUILT_IN_STPNCPY:
3465 case BUILT_IN_STRCAT:
3466 case BUILT_IN_STRCHR:
3467 case BUILT_IN_STRCMP:
3468 case BUILT_IN_STRCPY:
3469 case BUILT_IN_STRCSPN:
3470 case BUILT_IN_STRLEN:
3471 case BUILT_IN_STRNCAT:
3472 case BUILT_IN_STRNCMP:
3473 case BUILT_IN_STRNCPY:
3474 case BUILT_IN_STRPBRK:
3475 case BUILT_IN_STRRCHR:
3476 case BUILT_IN_STRSPN:
3477 case BUILT_IN_STRSTR:
3478 /* Assume certain string/memory builtins always return. */
3479 return false;
3480 default:
3481 break;
3486 /* For all other calls assume that they might not always return. */
3487 return true;
3490 /* Return true if INSN should be made dependent on the previous instruction
3491 group, and if all INSN's dependencies should be moved to the first
3492 instruction of that group. */
3494 static bool
3495 chain_to_prev_insn_p (rtx insn)
3497 rtx prev, x;
3499 /* INSN forms a group with the previous instruction. */
3500 if (SCHED_GROUP_P (insn))
3501 return true;
3503 /* If the previous instruction clobbers a register R and this one sets
3504 part of R, the clobber was added specifically to help us track the
3505 liveness of R. There's no point scheduling the clobber and leaving
3506 INSN behind, especially if we move the clobber to another block. */
3507 prev = prev_nonnote_nondebug_insn (insn);
3508 if (prev
3509 && INSN_P (prev)
3510 && BLOCK_FOR_INSN (prev) == BLOCK_FOR_INSN (insn)
3511 && GET_CODE (PATTERN (prev)) == CLOBBER)
3513 x = XEXP (PATTERN (prev), 0);
3514 if (set_of (x, insn))
3515 return true;
3518 return false;
3521 /* Analyze INSN with DEPS as a context. */
3522 void
3523 deps_analyze_insn (struct deps_desc *deps, rtx insn)
3525 if (sched_deps_info->start_insn)
3526 sched_deps_info->start_insn (insn);
3528 /* Record the condition for this insn. */
3529 if (NONDEBUG_INSN_P (insn))
3531 rtx t;
3532 sched_get_condition_with_rev (insn, NULL);
3533 t = INSN_CACHED_COND (insn);
3534 INSN_COND_DEPS (insn) = NULL_RTX;
3535 if (reload_completed
3536 && (current_sched_info->flags & DO_PREDICATION)
3537 && COMPARISON_P (t)
3538 && REG_P (XEXP (t, 0))
3539 && CONSTANT_P (XEXP (t, 1)))
3541 unsigned int regno;
3542 int nregs;
3543 t = XEXP (t, 0);
3544 regno = REGNO (t);
3545 nregs = hard_regno_nregs[regno][GET_MODE (t)];
3546 t = NULL_RTX;
3547 while (nregs-- > 0)
3549 struct deps_reg *reg_last = &deps->reg_last[regno + nregs];
3550 t = concat_INSN_LIST (reg_last->sets, t);
3551 t = concat_INSN_LIST (reg_last->clobbers, t);
3552 t = concat_INSN_LIST (reg_last->implicit_sets, t);
3554 INSN_COND_DEPS (insn) = t;
3558 if (JUMP_P (insn))
3560 /* Make each JUMP_INSN (but not a speculative check)
3561 a scheduling barrier for memory references. */
3562 if (!deps->readonly
3563 && !(sel_sched_p ()
3564 && sel_insn_is_speculation_check (insn)))
3566 /* Keep the list a reasonable size. */
3567 if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
3568 flush_pending_lists (deps, insn, true, true);
3569 else
3570 deps->pending_jump_insns
3571 = alloc_INSN_LIST (insn, deps->pending_jump_insns);
3574 /* For each insn which shouldn't cross a jump, add a dependence. */
3575 add_dependence_list_and_free (deps, insn,
3576 &deps->sched_before_next_jump, 1,
3577 REG_DEP_ANTI, true);
3579 sched_analyze_insn (deps, PATTERN (insn), insn);
3581 else if (NONJUMP_INSN_P (insn) || DEBUG_INSN_P (insn))
3583 sched_analyze_insn (deps, PATTERN (insn), insn);
3585 else if (CALL_P (insn))
3587 int i;
3589 CANT_MOVE (insn) = 1;
3591 if (find_reg_note (insn, REG_SETJMP, NULL))
3593 /* This is setjmp. Assume that all registers, not just
3594 hard registers, may be clobbered by this call. */
3595 reg_pending_barrier = MOVE_BARRIER;
3597 else
3599 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3600 /* A call may read and modify global register variables. */
3601 if (global_regs[i])
3603 SET_REGNO_REG_SET (reg_pending_sets, i);
3604 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3606 /* Other call-clobbered hard regs may be clobbered.
3607 Since we only have a choice between 'might be clobbered'
3608 and 'definitely not clobbered', we must include all
3609 partly call-clobbered registers here. */
3610 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
3611 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
3612 SET_REGNO_REG_SET (reg_pending_clobbers, i);
3613 /* We don't know what set of fixed registers might be used
3614 by the function, but it is certain that the stack pointer
3615 is among them, but be conservative. */
3616 else if (fixed_regs[i])
3617 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3618 /* The frame pointer is normally not used by the function
3619 itself, but by the debugger. */
3620 /* ??? MIPS o32 is an exception. It uses the frame pointer
3621 in the macro expansion of jal but does not represent this
3622 fact in the call_insn rtl. */
3623 else if (i == FRAME_POINTER_REGNUM
3624 || (i == HARD_FRAME_POINTER_REGNUM
3625 && (! reload_completed || frame_pointer_needed)))
3626 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3629 /* For each insn which shouldn't cross a call, add a dependence
3630 between that insn and this call insn. */
3631 add_dependence_list_and_free (deps, insn,
3632 &deps->sched_before_next_call, 1,
3633 REG_DEP_ANTI, true);
3635 sched_analyze_insn (deps, PATTERN (insn), insn);
3637 /* If CALL would be in a sched group, then this will violate
3638 convention that sched group insns have dependencies only on the
3639 previous instruction.
3641 Of course one can say: "Hey! What about head of the sched group?"
3642 And I will answer: "Basic principles (one dep per insn) are always
3643 the same." */
3644 gcc_assert (!SCHED_GROUP_P (insn));
3646 /* In the absence of interprocedural alias analysis, we must flush
3647 all pending reads and writes, and start new dependencies starting
3648 from here. But only flush writes for constant calls (which may
3649 be passed a pointer to something we haven't written yet). */
3650 flush_pending_lists (deps, insn, true, ! RTL_CONST_OR_PURE_CALL_P (insn));
3652 if (!deps->readonly)
3654 /* Remember the last function call for limiting lifetimes. */
3655 free_INSN_LIST_list (&deps->last_function_call);
3656 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
3658 if (call_may_noreturn_p (insn))
3660 /* Remember the last function call that might not always return
3661 normally for limiting moves of trapping insns. */
3662 free_INSN_LIST_list (&deps->last_function_call_may_noreturn);
3663 deps->last_function_call_may_noreturn
3664 = alloc_INSN_LIST (insn, NULL_RTX);
3667 /* Before reload, begin a post-call group, so as to keep the
3668 lifetimes of hard registers correct. */
3669 if (! reload_completed)
3670 deps->in_post_call_group_p = post_call;
3674 if (sched_deps_info->use_cselib)
3675 cselib_process_insn (insn);
3677 /* EH_REGION insn notes can not appear until well after we complete
3678 scheduling. */
3679 if (NOTE_P (insn))
3680 gcc_assert (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
3681 && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END);
3683 if (sched_deps_info->finish_insn)
3684 sched_deps_info->finish_insn ();
3686 /* Fixup the dependencies in the sched group. */
3687 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
3688 && chain_to_prev_insn_p (insn)
3689 && !sel_sched_p ())
3690 chain_to_prev_insn (insn);
3693 /* Initialize DEPS for the new block beginning with HEAD. */
3694 void
3695 deps_start_bb (struct deps_desc *deps, rtx head)
3697 gcc_assert (!deps->readonly);
3699 /* Before reload, if the previous block ended in a call, show that
3700 we are inside a post-call group, so as to keep the lifetimes of
3701 hard registers correct. */
3702 if (! reload_completed && !LABEL_P (head))
3704 rtx insn = prev_nonnote_nondebug_insn (head);
3706 if (insn && CALL_P (insn))
3707 deps->in_post_call_group_p = post_call_initial;
3711 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
3712 dependencies for each insn. */
3713 void
3714 sched_analyze (struct deps_desc *deps, rtx head, rtx tail)
3716 rtx insn;
3718 if (sched_deps_info->use_cselib)
3719 cselib_init (CSELIB_RECORD_MEMORY);
3721 deps_start_bb (deps, head);
3723 for (insn = head;; insn = NEXT_INSN (insn))
3726 if (INSN_P (insn))
3728 /* And initialize deps_lists. */
3729 sd_init_insn (insn);
3732 deps_analyze_insn (deps, insn);
3734 if (insn == tail)
3736 if (sched_deps_info->use_cselib)
3737 cselib_finish ();
3738 return;
3741 gcc_unreachable ();
3744 /* Helper for sched_free_deps ().
3745 Delete INSN's (RESOLVED_P) backward dependencies. */
3746 static void
3747 delete_dep_nodes_in_back_deps (rtx insn, bool resolved_p)
3749 sd_iterator_def sd_it;
3750 dep_t dep;
3751 sd_list_types_def types;
3753 if (resolved_p)
3754 types = SD_LIST_RES_BACK;
3755 else
3756 types = SD_LIST_BACK;
3758 for (sd_it = sd_iterator_start (insn, types);
3759 sd_iterator_cond (&sd_it, &dep);)
3761 dep_link_t link = *sd_it.linkp;
3762 dep_node_t node = DEP_LINK_NODE (link);
3763 deps_list_t back_list;
3764 deps_list_t forw_list;
3766 get_back_and_forw_lists (dep, resolved_p, &back_list, &forw_list);
3767 remove_from_deps_list (link, back_list);
3768 delete_dep_node (node);
3772 /* Delete (RESOLVED_P) dependencies between HEAD and TAIL together with
3773 deps_lists. */
3774 void
3775 sched_free_deps (rtx head, rtx tail, bool resolved_p)
3777 rtx insn;
3778 rtx next_tail = NEXT_INSN (tail);
3780 /* We make two passes since some insns may be scheduled before their
3781 dependencies are resolved. */
3782 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3783 if (INSN_P (insn) && INSN_LUID (insn) > 0)
3785 /* Clear forward deps and leave the dep_nodes to the
3786 corresponding back_deps list. */
3787 if (resolved_p)
3788 clear_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
3789 else
3790 clear_deps_list (INSN_FORW_DEPS (insn));
3792 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3793 if (INSN_P (insn) && INSN_LUID (insn) > 0)
3795 /* Clear resolved back deps together with its dep_nodes. */
3796 delete_dep_nodes_in_back_deps (insn, resolved_p);
3798 sd_finish_insn (insn);
3802 /* Initialize variables for region data dependence analysis.
3803 When LAZY_REG_LAST is true, do not allocate reg_last array
3804 of struct deps_desc immediately. */
3806 void
3807 init_deps (struct deps_desc *deps, bool lazy_reg_last)
3809 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
3811 deps->max_reg = max_reg;
3812 if (lazy_reg_last)
3813 deps->reg_last = NULL;
3814 else
3815 deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
3816 INIT_REG_SET (&deps->reg_last_in_use);
3818 deps->pending_read_insns = 0;
3819 deps->pending_read_mems = 0;
3820 deps->pending_write_insns = 0;
3821 deps->pending_write_mems = 0;
3822 deps->pending_jump_insns = 0;
3823 deps->pending_read_list_length = 0;
3824 deps->pending_write_list_length = 0;
3825 deps->pending_flush_length = 0;
3826 deps->last_pending_memory_flush = 0;
3827 deps->last_function_call = 0;
3828 deps->last_function_call_may_noreturn = 0;
3829 deps->sched_before_next_call = 0;
3830 deps->sched_before_next_jump = 0;
3831 deps->in_post_call_group_p = not_post_call;
3832 deps->last_debug_insn = 0;
3833 deps->last_reg_pending_barrier = NOT_A_BARRIER;
3834 deps->readonly = 0;
3837 /* Init only reg_last field of DEPS, which was not allocated before as
3838 we inited DEPS lazily. */
3839 void
3840 init_deps_reg_last (struct deps_desc *deps)
3842 gcc_assert (deps && deps->max_reg > 0);
3843 gcc_assert (deps->reg_last == NULL);
3845 deps->reg_last = XCNEWVEC (struct deps_reg, deps->max_reg);
3849 /* Free insn lists found in DEPS. */
3851 void
3852 free_deps (struct deps_desc *deps)
3854 unsigned i;
3855 reg_set_iterator rsi;
3857 /* We set max_reg to 0 when this context was already freed. */
3858 if (deps->max_reg == 0)
3860 gcc_assert (deps->reg_last == NULL);
3861 return;
3863 deps->max_reg = 0;
3865 free_INSN_LIST_list (&deps->pending_read_insns);
3866 free_EXPR_LIST_list (&deps->pending_read_mems);
3867 free_INSN_LIST_list (&deps->pending_write_insns);
3868 free_EXPR_LIST_list (&deps->pending_write_mems);
3869 free_INSN_LIST_list (&deps->last_pending_memory_flush);
3871 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
3872 times. For a testcase with 42000 regs and 8000 small basic blocks,
3873 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
3874 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3876 struct deps_reg *reg_last = &deps->reg_last[i];
3877 if (reg_last->uses)
3878 free_INSN_LIST_list (&reg_last->uses);
3879 if (reg_last->sets)
3880 free_INSN_LIST_list (&reg_last->sets);
3881 if (reg_last->implicit_sets)
3882 free_INSN_LIST_list (&reg_last->implicit_sets);
3883 if (reg_last->control_uses)
3884 free_INSN_LIST_list (&reg_last->control_uses);
3885 if (reg_last->clobbers)
3886 free_INSN_LIST_list (&reg_last->clobbers);
3888 CLEAR_REG_SET (&deps->reg_last_in_use);
3890 /* As we initialize reg_last lazily, it is possible that we didn't allocate
3891 it at all. */
3892 free (deps->reg_last);
3893 deps->reg_last = NULL;
3895 deps = NULL;
3898 /* Remove INSN from dependence contexts DEPS. */
3899 void
3900 remove_from_deps (struct deps_desc *deps, rtx insn)
3902 int removed;
3903 unsigned i;
3904 reg_set_iterator rsi;
3906 removed = remove_from_both_dependence_lists (insn, &deps->pending_read_insns,
3907 &deps->pending_read_mems);
3908 if (!DEBUG_INSN_P (insn))
3909 deps->pending_read_list_length -= removed;
3910 removed = remove_from_both_dependence_lists (insn, &deps->pending_write_insns,
3911 &deps->pending_write_mems);
3912 deps->pending_write_list_length -= removed;
3914 removed = remove_from_dependence_list (insn, &deps->pending_jump_insns);
3915 deps->pending_flush_length -= removed;
3916 removed = remove_from_dependence_list (insn, &deps->last_pending_memory_flush);
3917 deps->pending_flush_length -= removed;
3919 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3921 struct deps_reg *reg_last = &deps->reg_last[i];
3922 if (reg_last->uses)
3923 remove_from_dependence_list (insn, &reg_last->uses);
3924 if (reg_last->sets)
3925 remove_from_dependence_list (insn, &reg_last->sets);
3926 if (reg_last->implicit_sets)
3927 remove_from_dependence_list (insn, &reg_last->implicit_sets);
3928 if (reg_last->clobbers)
3929 remove_from_dependence_list (insn, &reg_last->clobbers);
3930 if (!reg_last->uses && !reg_last->sets && !reg_last->implicit_sets
3931 && !reg_last->clobbers)
3932 CLEAR_REGNO_REG_SET (&deps->reg_last_in_use, i);
3935 if (CALL_P (insn))
3937 remove_from_dependence_list (insn, &deps->last_function_call);
3938 remove_from_dependence_list (insn,
3939 &deps->last_function_call_may_noreturn);
3941 remove_from_dependence_list (insn, &deps->sched_before_next_call);
3944 /* Init deps data vector. */
3945 static void
3946 init_deps_data_vector (void)
3948 int reserve = (sched_max_luid + 1 - h_d_i_d.length ());
3949 if (reserve > 0 && ! h_d_i_d.space (reserve))
3950 h_d_i_d.safe_grow_cleared (3 * sched_max_luid / 2);
3953 /* If it is profitable to use them, initialize or extend (depending on
3954 GLOBAL_P) dependency data. */
3955 void
3956 sched_deps_init (bool global_p)
3958 /* Average number of insns in the basic block.
3959 '+ 1' is used to make it nonzero. */
3960 int insns_in_block = sched_max_luid / n_basic_blocks + 1;
3962 init_deps_data_vector ();
3964 /* We use another caching mechanism for selective scheduling, so
3965 we don't use this one. */
3966 if (!sel_sched_p () && global_p && insns_in_block > 100 * 5)
3968 /* ?!? We could save some memory by computing a per-region luid mapping
3969 which could reduce both the number of vectors in the cache and the
3970 size of each vector. Instead we just avoid the cache entirely unless
3971 the average number of instructions in a basic block is very high. See
3972 the comment before the declaration of true_dependency_cache for
3973 what we consider "very high". */
3974 cache_size = 0;
3975 extend_dependency_caches (sched_max_luid, true);
3978 if (global_p)
3980 dl_pool = create_alloc_pool ("deps_list", sizeof (struct _deps_list),
3981 /* Allocate lists for one block at a time. */
3982 insns_in_block);
3983 dn_pool = create_alloc_pool ("dep_node", sizeof (struct _dep_node),
3984 /* Allocate nodes for one block at a time.
3985 We assume that average insn has
3986 5 producers. */
3987 5 * insns_in_block);
3992 /* Create or extend (depending on CREATE_P) dependency caches to
3993 size N. */
3994 void
3995 extend_dependency_caches (int n, bool create_p)
3997 if (create_p || true_dependency_cache)
3999 int i, luid = cache_size + n;
4001 true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
4002 luid);
4003 output_dependency_cache = XRESIZEVEC (bitmap_head,
4004 output_dependency_cache, luid);
4005 anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
4006 luid);
4007 control_dependency_cache = XRESIZEVEC (bitmap_head, control_dependency_cache,
4008 luid);
4010 if (current_sched_info->flags & DO_SPECULATION)
4011 spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
4012 luid);
4014 for (i = cache_size; i < luid; i++)
4016 bitmap_initialize (&true_dependency_cache[i], 0);
4017 bitmap_initialize (&output_dependency_cache[i], 0);
4018 bitmap_initialize (&anti_dependency_cache[i], 0);
4019 bitmap_initialize (&control_dependency_cache[i], 0);
4021 if (current_sched_info->flags & DO_SPECULATION)
4022 bitmap_initialize (&spec_dependency_cache[i], 0);
4024 cache_size = luid;
4028 /* Finalize dependency information for the whole function. */
4029 void
4030 sched_deps_finish (void)
4032 gcc_assert (deps_pools_are_empty_p ());
4033 free_alloc_pool_if_empty (&dn_pool);
4034 free_alloc_pool_if_empty (&dl_pool);
4035 gcc_assert (dn_pool == NULL && dl_pool == NULL);
4037 h_d_i_d.release ();
4038 cache_size = 0;
4040 if (true_dependency_cache)
4042 int i;
4044 for (i = 0; i < cache_size; i++)
4046 bitmap_clear (&true_dependency_cache[i]);
4047 bitmap_clear (&output_dependency_cache[i]);
4048 bitmap_clear (&anti_dependency_cache[i]);
4049 bitmap_clear (&control_dependency_cache[i]);
4051 if (sched_deps_info->generate_spec_deps)
4052 bitmap_clear (&spec_dependency_cache[i]);
4054 free (true_dependency_cache);
4055 true_dependency_cache = NULL;
4056 free (output_dependency_cache);
4057 output_dependency_cache = NULL;
4058 free (anti_dependency_cache);
4059 anti_dependency_cache = NULL;
4060 free (control_dependency_cache);
4061 control_dependency_cache = NULL;
4063 if (sched_deps_info->generate_spec_deps)
4065 free (spec_dependency_cache);
4066 spec_dependency_cache = NULL;
4072 /* Initialize some global variables needed by the dependency analysis
4073 code. */
4075 void
4076 init_deps_global (void)
4078 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
4079 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
4080 reg_pending_sets = ALLOC_REG_SET (&reg_obstack);
4081 reg_pending_clobbers = ALLOC_REG_SET (&reg_obstack);
4082 reg_pending_uses = ALLOC_REG_SET (&reg_obstack);
4083 reg_pending_control_uses = ALLOC_REG_SET (&reg_obstack);
4084 reg_pending_barrier = NOT_A_BARRIER;
4086 if (!sel_sched_p () || sched_emulate_haifa_p)
4088 sched_deps_info->start_insn = haifa_start_insn;
4089 sched_deps_info->finish_insn = haifa_finish_insn;
4091 sched_deps_info->note_reg_set = haifa_note_reg_set;
4092 sched_deps_info->note_reg_clobber = haifa_note_reg_clobber;
4093 sched_deps_info->note_reg_use = haifa_note_reg_use;
4095 sched_deps_info->note_mem_dep = haifa_note_mem_dep;
4096 sched_deps_info->note_dep = haifa_note_dep;
4100 /* Free everything used by the dependency analysis code. */
4102 void
4103 finish_deps_global (void)
4105 FREE_REG_SET (reg_pending_sets);
4106 FREE_REG_SET (reg_pending_clobbers);
4107 FREE_REG_SET (reg_pending_uses);
4108 FREE_REG_SET (reg_pending_control_uses);
4111 /* Estimate the weakness of dependence between MEM1 and MEM2. */
4112 dw_t
4113 estimate_dep_weak (rtx mem1, rtx mem2)
4115 rtx r1, r2;
4117 if (mem1 == mem2)
4118 /* MEMs are the same - don't speculate. */
4119 return MIN_DEP_WEAK;
4121 r1 = XEXP (mem1, 0);
4122 r2 = XEXP (mem2, 0);
4124 if (r1 == r2
4125 || (REG_P (r1) && REG_P (r2)
4126 && REGNO (r1) == REGNO (r2)))
4127 /* Again, MEMs are the same. */
4128 return MIN_DEP_WEAK;
4129 else if ((REG_P (r1) && !REG_P (r2))
4130 || (!REG_P (r1) && REG_P (r2)))
4131 /* Different addressing modes - reason to be more speculative,
4132 than usual. */
4133 return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
4134 else
4135 /* We can't say anything about the dependence. */
4136 return UNCERTAIN_DEP_WEAK;
4139 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
4140 This function can handle same INSN and ELEM (INSN == ELEM).
4141 It is a convenience wrapper. */
4142 static void
4143 add_dependence_1 (rtx insn, rtx elem, enum reg_note dep_type)
4145 ds_t ds;
4146 bool internal;
4148 if (dep_type == REG_DEP_TRUE)
4149 ds = DEP_TRUE;
4150 else if (dep_type == REG_DEP_OUTPUT)
4151 ds = DEP_OUTPUT;
4152 else if (dep_type == REG_DEP_CONTROL)
4153 ds = DEP_CONTROL;
4154 else
4156 gcc_assert (dep_type == REG_DEP_ANTI);
4157 ds = DEP_ANTI;
4160 /* When add_dependence is called from inside sched-deps.c, we expect
4161 cur_insn to be non-null. */
4162 internal = cur_insn != NULL;
4163 if (internal)
4164 gcc_assert (insn == cur_insn);
4165 else
4166 cur_insn = insn;
4168 note_dep (elem, ds);
4169 if (!internal)
4170 cur_insn = NULL;
4173 /* Return weakness of speculative type TYPE in the dep_status DS. */
4174 dw_t
4175 get_dep_weak_1 (ds_t ds, ds_t type)
4177 ds = ds & type;
4179 switch (type)
4181 case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
4182 case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
4183 case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
4184 case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
4185 default: gcc_unreachable ();
4188 return (dw_t) ds;
4191 dw_t
4192 get_dep_weak (ds_t ds, ds_t type)
4194 dw_t dw = get_dep_weak_1 (ds, type);
4196 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
4197 return dw;
4200 /* Return the dep_status, which has the same parameters as DS, except for
4201 speculative type TYPE, that will have weakness DW. */
4202 ds_t
4203 set_dep_weak (ds_t ds, ds_t type, dw_t dw)
4205 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
4207 ds &= ~type;
4208 switch (type)
4210 case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
4211 case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
4212 case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
4213 case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
4214 default: gcc_unreachable ();
4216 return ds;
4219 /* Return the join of two dep_statuses DS1 and DS2.
4220 If MAX_P is true then choose the greater probability,
4221 otherwise multiply probabilities.
4222 This function assumes that both DS1 and DS2 contain speculative bits. */
4223 static ds_t
4224 ds_merge_1 (ds_t ds1, ds_t ds2, bool max_p)
4226 ds_t ds, t;
4228 gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
4230 ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
4232 t = FIRST_SPEC_TYPE;
4235 if ((ds1 & t) && !(ds2 & t))
4236 ds |= ds1 & t;
4237 else if (!(ds1 & t) && (ds2 & t))
4238 ds |= ds2 & t;
4239 else if ((ds1 & t) && (ds2 & t))
4241 dw_t dw1 = get_dep_weak (ds1, t);
4242 dw_t dw2 = get_dep_weak (ds2, t);
4243 ds_t dw;
4245 if (!max_p)
4247 dw = ((ds_t) dw1) * ((ds_t) dw2);
4248 dw /= MAX_DEP_WEAK;
4249 if (dw < MIN_DEP_WEAK)
4250 dw = MIN_DEP_WEAK;
4252 else
4254 if (dw1 >= dw2)
4255 dw = dw1;
4256 else
4257 dw = dw2;
4260 ds = set_dep_weak (ds, t, (dw_t) dw);
4263 if (t == LAST_SPEC_TYPE)
4264 break;
4265 t <<= SPEC_TYPE_SHIFT;
4267 while (1);
4269 return ds;
4272 /* Return the join of two dep_statuses DS1 and DS2.
4273 This function assumes that both DS1 and DS2 contain speculative bits. */
4274 ds_t
4275 ds_merge (ds_t ds1, ds_t ds2)
4277 return ds_merge_1 (ds1, ds2, false);
4280 /* Return the join of two dep_statuses DS1 and DS2. */
4281 ds_t
4282 ds_full_merge (ds_t ds, ds_t ds2, rtx mem1, rtx mem2)
4284 ds_t new_status = ds | ds2;
4286 if (new_status & SPECULATIVE)
4288 if ((ds && !(ds & SPECULATIVE))
4289 || (ds2 && !(ds2 & SPECULATIVE)))
4290 /* Then this dep can't be speculative. */
4291 new_status &= ~SPECULATIVE;
4292 else
4294 /* Both are speculative. Merging probabilities. */
4295 if (mem1)
4297 dw_t dw;
4299 dw = estimate_dep_weak (mem1, mem2);
4300 ds = set_dep_weak (ds, BEGIN_DATA, dw);
4303 if (!ds)
4304 new_status = ds2;
4305 else if (!ds2)
4306 new_status = ds;
4307 else
4308 new_status = ds_merge (ds2, ds);
4312 return new_status;
4315 /* Return the join of DS1 and DS2. Use maximum instead of multiplying
4316 probabilities. */
4317 ds_t
4318 ds_max_merge (ds_t ds1, ds_t ds2)
4320 if (ds1 == 0 && ds2 == 0)
4321 return 0;
4323 if (ds1 == 0 && ds2 != 0)
4324 return ds2;
4326 if (ds1 != 0 && ds2 == 0)
4327 return ds1;
4329 return ds_merge_1 (ds1, ds2, true);
4332 /* Return the probability of speculation success for the speculation
4333 status DS. */
4334 dw_t
4335 ds_weak (ds_t ds)
4337 ds_t res = 1, dt;
4338 int n = 0;
4340 dt = FIRST_SPEC_TYPE;
4343 if (ds & dt)
4345 res *= (ds_t) get_dep_weak (ds, dt);
4346 n++;
4349 if (dt == LAST_SPEC_TYPE)
4350 break;
4351 dt <<= SPEC_TYPE_SHIFT;
4353 while (1);
4355 gcc_assert (n);
4356 while (--n)
4357 res /= MAX_DEP_WEAK;
4359 if (res < MIN_DEP_WEAK)
4360 res = MIN_DEP_WEAK;
4362 gcc_assert (res <= MAX_DEP_WEAK);
4364 return (dw_t) res;
4367 /* Return a dep status that contains all speculation types of DS. */
4368 ds_t
4369 ds_get_speculation_types (ds_t ds)
4371 if (ds & BEGIN_DATA)
4372 ds |= BEGIN_DATA;
4373 if (ds & BE_IN_DATA)
4374 ds |= BE_IN_DATA;
4375 if (ds & BEGIN_CONTROL)
4376 ds |= BEGIN_CONTROL;
4377 if (ds & BE_IN_CONTROL)
4378 ds |= BE_IN_CONTROL;
4380 return ds & SPECULATIVE;
4383 /* Return a dep status that contains maximal weakness for each speculation
4384 type present in DS. */
4385 ds_t
4386 ds_get_max_dep_weak (ds_t ds)
4388 if (ds & BEGIN_DATA)
4389 ds = set_dep_weak (ds, BEGIN_DATA, MAX_DEP_WEAK);
4390 if (ds & BE_IN_DATA)
4391 ds = set_dep_weak (ds, BE_IN_DATA, MAX_DEP_WEAK);
4392 if (ds & BEGIN_CONTROL)
4393 ds = set_dep_weak (ds, BEGIN_CONTROL, MAX_DEP_WEAK);
4394 if (ds & BE_IN_CONTROL)
4395 ds = set_dep_weak (ds, BE_IN_CONTROL, MAX_DEP_WEAK);
4397 return ds;
4400 /* Dump information about the dependence status S. */
4401 static void
4402 dump_ds (FILE *f, ds_t s)
4404 fprintf (f, "{");
4406 if (s & BEGIN_DATA)
4407 fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak_1 (s, BEGIN_DATA));
4408 if (s & BE_IN_DATA)
4409 fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak_1 (s, BE_IN_DATA));
4410 if (s & BEGIN_CONTROL)
4411 fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak_1 (s, BEGIN_CONTROL));
4412 if (s & BE_IN_CONTROL)
4413 fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak_1 (s, BE_IN_CONTROL));
4415 if (s & HARD_DEP)
4416 fprintf (f, "HARD_DEP; ");
4418 if (s & DEP_TRUE)
4419 fprintf (f, "DEP_TRUE; ");
4420 if (s & DEP_OUTPUT)
4421 fprintf (f, "DEP_OUTPUT; ");
4422 if (s & DEP_ANTI)
4423 fprintf (f, "DEP_ANTI; ");
4424 if (s & DEP_CONTROL)
4425 fprintf (f, "DEP_CONTROL; ");
4427 fprintf (f, "}");
4430 DEBUG_FUNCTION void
4431 debug_ds (ds_t s)
4433 dump_ds (stderr, s);
4434 fprintf (stderr, "\n");
4437 #ifdef ENABLE_CHECKING
4438 /* Verify that dependence type and status are consistent.
4439 If RELAXED_P is true, then skip dep_weakness checks. */
4440 static void
4441 check_dep (dep_t dep, bool relaxed_p)
4443 enum reg_note dt = DEP_TYPE (dep);
4444 ds_t ds = DEP_STATUS (dep);
4446 gcc_assert (DEP_PRO (dep) != DEP_CON (dep));
4448 if (!(current_sched_info->flags & USE_DEPS_LIST))
4450 gcc_assert (ds == 0);
4451 return;
4454 /* Check that dependence type contains the same bits as the status. */
4455 if (dt == REG_DEP_TRUE)
4456 gcc_assert (ds & DEP_TRUE);
4457 else if (dt == REG_DEP_OUTPUT)
4458 gcc_assert ((ds & DEP_OUTPUT)
4459 && !(ds & DEP_TRUE));
4460 else if (dt == REG_DEP_ANTI)
4461 gcc_assert ((ds & DEP_ANTI)
4462 && !(ds & (DEP_OUTPUT | DEP_TRUE)));
4463 else
4464 gcc_assert (dt == REG_DEP_CONTROL
4465 && (ds & DEP_CONTROL)
4466 && !(ds & (DEP_OUTPUT | DEP_ANTI | DEP_TRUE)));
4468 /* HARD_DEP can not appear in dep_status of a link. */
4469 gcc_assert (!(ds & HARD_DEP));
4471 /* Check that dependence status is set correctly when speculation is not
4472 supported. */
4473 if (!sched_deps_info->generate_spec_deps)
4474 gcc_assert (!(ds & SPECULATIVE));
4475 else if (ds & SPECULATIVE)
4477 if (!relaxed_p)
4479 ds_t type = FIRST_SPEC_TYPE;
4481 /* Check that dependence weakness is in proper range. */
4484 if (ds & type)
4485 get_dep_weak (ds, type);
4487 if (type == LAST_SPEC_TYPE)
4488 break;
4489 type <<= SPEC_TYPE_SHIFT;
4491 while (1);
4494 if (ds & BEGIN_SPEC)
4496 /* Only true dependence can be data speculative. */
4497 if (ds & BEGIN_DATA)
4498 gcc_assert (ds & DEP_TRUE);
4500 /* Control dependencies in the insn scheduler are represented by
4501 anti-dependencies, therefore only anti dependence can be
4502 control speculative. */
4503 if (ds & BEGIN_CONTROL)
4504 gcc_assert (ds & DEP_ANTI);
4506 else
4508 /* Subsequent speculations should resolve true dependencies. */
4509 gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
4512 /* Check that true and anti dependencies can't have other speculative
4513 statuses. */
4514 if (ds & DEP_TRUE)
4515 gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
4516 /* An output dependence can't be speculative at all. */
4517 gcc_assert (!(ds & DEP_OUTPUT));
4518 if (ds & DEP_ANTI)
4519 gcc_assert (ds & BEGIN_CONTROL);
4522 #endif /* ENABLE_CHECKING */
4524 /* The following code discovers opportunities to switch a memory reference
4525 and an increment by modifying the address. We ensure that this is done
4526 only for dependencies that are only used to show a single register
4527 dependence (using DEP_NONREG and DEP_MULTIPLE), and so that every memory
4528 instruction involved is subject to only one dep that can cause a pattern
4529 change.
4531 When we discover a suitable dependency, we fill in the dep_replacement
4532 structure to show how to modify the memory reference. */
4534 /* Holds information about a pair of memory reference and register increment
4535 insns which depend on each other, but could possibly be interchanged. */
4536 struct mem_inc_info
4538 rtx inc_insn;
4539 rtx mem_insn;
4541 rtx *mem_loc;
4542 /* A register occurring in the memory address for which we wish to break
4543 the dependence. This must be identical to the destination register of
4544 the increment. */
4545 rtx mem_reg0;
4546 /* Any kind of index that is added to that register. */
4547 rtx mem_index;
4548 /* The constant offset used in the memory address. */
4549 HOST_WIDE_INT mem_constant;
4550 /* The constant added in the increment insn. Negated if the increment is
4551 after the memory address. */
4552 HOST_WIDE_INT inc_constant;
4553 /* The source register used in the increment. May be different from mem_reg0
4554 if the increment occurs before the memory address. */
4555 rtx inc_input;
4558 /* Verify that the memory location described in MII can be replaced with
4559 one using NEW_ADDR. Return the new memory reference or NULL_RTX. The
4560 insn remains unchanged by this function. */
4562 static rtx
4563 attempt_change (struct mem_inc_info *mii, rtx new_addr)
4565 rtx mem = *mii->mem_loc;
4566 rtx new_mem;
4568 /* Jump thru a lot of hoops to keep the attributes up to date. We
4569 do not want to call one of the change address variants that take
4570 an offset even though we know the offset in many cases. These
4571 assume you are changing where the address is pointing by the
4572 offset. */
4573 new_mem = replace_equiv_address_nv (mem, new_addr);
4574 if (! validate_change (mii->mem_insn, mii->mem_loc, new_mem, 0))
4576 if (sched_verbose >= 5)
4577 fprintf (sched_dump, "validation failure\n");
4578 return NULL_RTX;
4581 /* Put back the old one. */
4582 validate_change (mii->mem_insn, mii->mem_loc, mem, 0);
4584 return new_mem;
4587 /* Return true if INSN is of a form "a = b op c" where a and b are
4588 regs. op is + if c is a reg and +|- if c is a const. Fill in
4589 informantion in MII about what is found.
4590 BEFORE_MEM indicates whether the increment is found before or after
4591 a corresponding memory reference. */
4593 static bool
4594 parse_add_or_inc (struct mem_inc_info *mii, rtx insn, bool before_mem)
4596 rtx pat = single_set (insn);
4597 rtx src, cst;
4598 bool regs_equal;
4600 if (RTX_FRAME_RELATED_P (insn) || !pat)
4601 return false;
4603 /* Result must be single reg. */
4604 if (!REG_P (SET_DEST (pat)))
4605 return false;
4607 if (GET_CODE (SET_SRC (pat)) != PLUS)
4608 return false;
4610 mii->inc_insn = insn;
4611 src = SET_SRC (pat);
4612 mii->inc_input = XEXP (src, 0);
4614 if (!REG_P (XEXP (src, 0)))
4615 return false;
4617 if (!rtx_equal_p (SET_DEST (pat), mii->mem_reg0))
4618 return false;
4620 cst = XEXP (src, 1);
4621 if (!CONST_INT_P (cst))
4622 return false;
4623 mii->inc_constant = INTVAL (cst);
4625 regs_equal = rtx_equal_p (mii->inc_input, mii->mem_reg0);
4627 if (!before_mem)
4629 mii->inc_constant = -mii->inc_constant;
4630 if (!regs_equal)
4631 return false;
4634 if (regs_equal && REGNO (SET_DEST (pat)) == STACK_POINTER_REGNUM)
4636 /* Note that the sign has already been reversed for !before_mem. */
4637 #ifdef STACK_GROWS_DOWNWARD
4638 return mii->inc_constant > 0;
4639 #else
4640 return mii->inc_constant < 0;
4641 #endif
4643 return true;
4646 /* Once a suitable mem reference has been found and the corresponding data
4647 in MII has been filled in, this function is called to find a suitable
4648 add or inc insn involving the register we found in the memory
4649 reference. */
4651 static bool
4652 find_inc (struct mem_inc_info *mii, bool backwards)
4654 sd_iterator_def sd_it;
4655 dep_t dep;
4657 sd_it = sd_iterator_start (mii->mem_insn,
4658 backwards ? SD_LIST_HARD_BACK : SD_LIST_FORW);
4659 while (sd_iterator_cond (&sd_it, &dep))
4661 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
4662 rtx pro = DEP_PRO (dep);
4663 rtx con = DEP_CON (dep);
4664 rtx inc_cand = backwards ? pro : con;
4665 if (DEP_NONREG (dep) || DEP_MULTIPLE (dep))
4666 goto next;
4667 if (parse_add_or_inc (mii, inc_cand, backwards))
4669 struct dep_replacement *desc;
4670 df_ref *def_rec;
4671 rtx newaddr, newmem;
4673 if (sched_verbose >= 5)
4674 fprintf (sched_dump, "candidate mem/inc pair: %d %d\n",
4675 INSN_UID (mii->mem_insn), INSN_UID (inc_cand));
4677 /* Need to assure that none of the operands of the inc
4678 instruction are assigned to by the mem insn. */
4679 for (def_rec = DF_INSN_DEFS (mii->mem_insn); *def_rec; def_rec++)
4681 df_ref def = *def_rec;
4682 if (reg_overlap_mentioned_p (DF_REF_REG (def), mii->inc_input)
4683 || reg_overlap_mentioned_p (DF_REF_REG (def), mii->mem_reg0))
4685 if (sched_verbose >= 5)
4686 fprintf (sched_dump,
4687 "inc conflicts with store failure.\n");
4688 goto next;
4691 newaddr = mii->inc_input;
4692 if (mii->mem_index != NULL_RTX)
4693 newaddr = gen_rtx_PLUS (GET_MODE (newaddr), newaddr,
4694 mii->mem_index);
4695 newaddr = plus_constant (GET_MODE (newaddr), newaddr,
4696 mii->mem_constant + mii->inc_constant);
4697 newmem = attempt_change (mii, newaddr);
4698 if (newmem == NULL_RTX)
4699 goto next;
4700 if (sched_verbose >= 5)
4701 fprintf (sched_dump, "successful address replacement\n");
4702 desc = XCNEW (struct dep_replacement);
4703 DEP_REPLACE (dep) = desc;
4704 desc->loc = mii->mem_loc;
4705 desc->newval = newmem;
4706 desc->orig = *desc->loc;
4707 desc->insn = mii->mem_insn;
4708 move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
4709 INSN_SPEC_BACK_DEPS (con));
4710 if (backwards)
4712 FOR_EACH_DEP (mii->inc_insn, SD_LIST_BACK, sd_it, dep)
4713 add_dependence_1 (mii->mem_insn, DEP_PRO (dep),
4714 REG_DEP_TRUE);
4716 else
4718 FOR_EACH_DEP (mii->inc_insn, SD_LIST_FORW, sd_it, dep)
4719 add_dependence_1 (DEP_CON (dep), mii->mem_insn,
4720 REG_DEP_ANTI);
4722 return true;
4724 next:
4725 sd_iterator_next (&sd_it);
4727 return false;
4730 /* A recursive function that walks ADDRESS_OF_X to find memory references
4731 which could be modified during scheduling. We call find_inc for each
4732 one we find that has a recognizable form. MII holds information about
4733 the pair of memory/increment instructions.
4734 We ensure that every instruction with a memory reference (which will be
4735 the location of the replacement) is assigned at most one breakable
4736 dependency. */
4738 static bool
4739 find_mem (struct mem_inc_info *mii, rtx *address_of_x)
4741 rtx x = *address_of_x;
4742 enum rtx_code code = GET_CODE (x);
4743 const char *const fmt = GET_RTX_FORMAT (code);
4744 int i;
4746 if (code == MEM)
4748 rtx reg0 = XEXP (x, 0);
4750 mii->mem_loc = address_of_x;
4751 mii->mem_index = NULL_RTX;
4752 mii->mem_constant = 0;
4753 if (GET_CODE (reg0) == PLUS && CONST_INT_P (XEXP (reg0, 1)))
4755 mii->mem_constant = INTVAL (XEXP (reg0, 1));
4756 reg0 = XEXP (reg0, 0);
4758 if (GET_CODE (reg0) == PLUS)
4760 mii->mem_index = XEXP (reg0, 1);
4761 reg0 = XEXP (reg0, 0);
4763 if (REG_P (reg0))
4765 df_ref *def_rec;
4766 int occurrences = 0;
4768 /* Make sure this reg appears only once in this insn. Can't use
4769 count_occurrences since that only works for pseudos. */
4770 for (def_rec = DF_INSN_USES (mii->mem_insn); *def_rec; def_rec++)
4772 df_ref def = *def_rec;
4773 if (reg_overlap_mentioned_p (reg0, DF_REF_REG (def)))
4774 if (++occurrences > 1)
4776 if (sched_verbose >= 5)
4777 fprintf (sched_dump, "mem count failure\n");
4778 return false;
4782 mii->mem_reg0 = reg0;
4783 return find_inc (mii, true) || find_inc (mii, false);
4785 return false;
4788 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4790 /* If REG occurs inside a MEM used in a bit-field reference,
4791 that is unacceptable. */
4792 return false;
4795 /* Time for some deep diving. */
4796 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4798 if (fmt[i] == 'e')
4800 if (find_mem (mii, &XEXP (x, i)))
4801 return true;
4803 else if (fmt[i] == 'E')
4805 int j;
4806 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4807 if (find_mem (mii, &XVECEXP (x, i, j)))
4808 return true;
4811 return false;
4815 /* Examine the instructions between HEAD and TAIL and try to find
4816 dependencies that can be broken by modifying one of the patterns. */
4818 void
4819 find_modifiable_mems (rtx head, rtx tail)
4821 rtx insn, next_tail = NEXT_INSN (tail);
4822 int success_in_block = 0;
4824 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4826 struct mem_inc_info mii;
4828 if (!NONDEBUG_INSN_P (insn) || RTX_FRAME_RELATED_P (insn))
4829 continue;
4831 mii.mem_insn = insn;
4832 if (find_mem (&mii, &PATTERN (insn)))
4833 success_in_block++;
4835 if (success_in_block && sched_verbose >= 5)
4836 fprintf (sched_dump, "%d candidates for address modification found.\n",
4837 success_in_block);
4840 #endif /* INSN_SCHEDULING */