2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / sched-deps.c
blob9683055a8579cb26e6fcbbad08653f253d5c00ef
1 /* Instruction scheduling pass. This file computes dependencies between
2 instructions.
3 Copyright (C) 1992-2015 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 "backend.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "df.h"
30 #include "diagnostic-core.h"
31 #include "alias.h"
32 #include "tm_p.h"
33 #include "regs.h"
34 #include "flags.h"
35 #include "insn-config.h"
36 #include "insn-attr.h"
37 #include "except.h"
38 #include "emit-rtl.h"
39 #include "cfgbuild.h"
40 #include "sched-int.h"
41 #include "params.h"
42 #include "alloc-pool.h"
43 #include "cselib.h"
44 #include "ira.h"
45 #include "ira-int.h"
46 #include "target.h"
48 #ifdef INSN_SCHEDULING
50 #ifdef ENABLE_CHECKING
51 #define CHECK (true)
52 #else
53 #define CHECK (false)
54 #endif
56 /* Holds current parameters for the dependency analyzer. */
57 struct sched_deps_info_def *sched_deps_info;
59 /* The data is specific to the Haifa scheduler. */
60 vec<haifa_deps_insn_data_def>
61 h_d_i_d = vNULL;
63 /* Return the major type present in the DS. */
64 enum reg_note
65 ds_to_dk (ds_t ds)
67 if (ds & DEP_TRUE)
68 return REG_DEP_TRUE;
70 if (ds & DEP_OUTPUT)
71 return REG_DEP_OUTPUT;
73 if (ds & DEP_CONTROL)
74 return REG_DEP_CONTROL;
76 gcc_assert (ds & DEP_ANTI);
78 return REG_DEP_ANTI;
81 /* Return equivalent dep_status. */
82 ds_t
83 dk_to_ds (enum reg_note dk)
85 switch (dk)
87 case REG_DEP_TRUE:
88 return DEP_TRUE;
90 case REG_DEP_OUTPUT:
91 return DEP_OUTPUT;
93 case REG_DEP_CONTROL:
94 return DEP_CONTROL;
96 default:
97 gcc_assert (dk == REG_DEP_ANTI);
98 return DEP_ANTI;
102 /* Functions to operate with dependence information container - dep_t. */
104 /* Init DEP with the arguments. */
105 void
106 init_dep_1 (dep_t dep, rtx_insn *pro, rtx_insn *con, enum reg_note type, ds_t ds)
108 DEP_PRO (dep) = pro;
109 DEP_CON (dep) = con;
110 DEP_TYPE (dep) = type;
111 DEP_STATUS (dep) = ds;
112 DEP_COST (dep) = UNKNOWN_DEP_COST;
113 DEP_NONREG (dep) = 0;
114 DEP_MULTIPLE (dep) = 0;
115 DEP_REPLACE (dep) = NULL;
118 /* Init DEP with the arguments.
119 While most of the scheduler (including targets) only need the major type
120 of the dependency, it is convenient to hide full dep_status from them. */
121 void
122 init_dep (dep_t dep, rtx_insn *pro, rtx_insn *con, enum reg_note kind)
124 ds_t ds;
126 if ((current_sched_info->flags & USE_DEPS_LIST))
127 ds = dk_to_ds (kind);
128 else
129 ds = 0;
131 init_dep_1 (dep, pro, con, kind, ds);
134 /* Make a copy of FROM in TO. */
135 static void
136 copy_dep (dep_t to, dep_t from)
138 memcpy (to, from, sizeof (*to));
141 static void dump_ds (FILE *, ds_t);
143 /* Define flags for dump_dep (). */
145 /* Dump producer of the dependence. */
146 #define DUMP_DEP_PRO (2)
148 /* Dump consumer of the dependence. */
149 #define DUMP_DEP_CON (4)
151 /* Dump type of the dependence. */
152 #define DUMP_DEP_TYPE (8)
154 /* Dump status of the dependence. */
155 #define DUMP_DEP_STATUS (16)
157 /* Dump all information about the dependence. */
158 #define DUMP_DEP_ALL (DUMP_DEP_PRO | DUMP_DEP_CON | DUMP_DEP_TYPE \
159 |DUMP_DEP_STATUS)
161 /* Dump DEP to DUMP.
162 FLAGS is a bit mask specifying what information about DEP needs
163 to be printed.
164 If FLAGS has the very first bit set, then dump all information about DEP
165 and propagate this bit into the callee dump functions. */
166 static void
167 dump_dep (FILE *dump, dep_t dep, int flags)
169 if (flags & 1)
170 flags |= DUMP_DEP_ALL;
172 fprintf (dump, "<");
174 if (flags & DUMP_DEP_PRO)
175 fprintf (dump, "%d; ", INSN_UID (DEP_PRO (dep)));
177 if (flags & DUMP_DEP_CON)
178 fprintf (dump, "%d; ", INSN_UID (DEP_CON (dep)));
180 if (flags & DUMP_DEP_TYPE)
182 char t;
183 enum reg_note type = DEP_TYPE (dep);
185 switch (type)
187 case REG_DEP_TRUE:
188 t = 't';
189 break;
191 case REG_DEP_OUTPUT:
192 t = 'o';
193 break;
195 case REG_DEP_CONTROL:
196 t = 'c';
197 break;
199 case REG_DEP_ANTI:
200 t = 'a';
201 break;
203 default:
204 gcc_unreachable ();
205 break;
208 fprintf (dump, "%c; ", t);
211 if (flags & DUMP_DEP_STATUS)
213 if (current_sched_info->flags & USE_DEPS_LIST)
214 dump_ds (dump, DEP_STATUS (dep));
217 fprintf (dump, ">");
220 /* Default flags for dump_dep (). */
221 static int dump_dep_flags = (DUMP_DEP_PRO | DUMP_DEP_CON);
223 /* Dump all fields of DEP to STDERR. */
224 void
225 sd_debug_dep (dep_t dep)
227 dump_dep (stderr, dep, 1);
228 fprintf (stderr, "\n");
231 /* Determine whether DEP is a dependency link of a non-debug insn on a
232 debug insn. */
234 static inline bool
235 depl_on_debug_p (dep_link_t dep)
237 return (DEBUG_INSN_P (DEP_LINK_PRO (dep))
238 && !DEBUG_INSN_P (DEP_LINK_CON (dep)));
241 /* Functions to operate with a single link from the dependencies lists -
242 dep_link_t. */
244 /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
245 PREV_NEXT_P. */
246 static void
247 attach_dep_link (dep_link_t l, dep_link_t *prev_nextp)
249 dep_link_t next = *prev_nextp;
251 gcc_assert (DEP_LINK_PREV_NEXTP (l) == NULL
252 && DEP_LINK_NEXT (l) == NULL);
254 /* Init node being inserted. */
255 DEP_LINK_PREV_NEXTP (l) = prev_nextp;
256 DEP_LINK_NEXT (l) = next;
258 /* Fix next node. */
259 if (next != NULL)
261 gcc_assert (DEP_LINK_PREV_NEXTP (next) == prev_nextp);
263 DEP_LINK_PREV_NEXTP (next) = &DEP_LINK_NEXT (l);
266 /* Fix prev node. */
267 *prev_nextp = l;
270 /* Add dep_link LINK to deps_list L. */
271 static void
272 add_to_deps_list (dep_link_t link, deps_list_t l)
274 attach_dep_link (link, &DEPS_LIST_FIRST (l));
276 /* Don't count debug deps. */
277 if (!depl_on_debug_p (link))
278 ++DEPS_LIST_N_LINKS (l);
281 /* Detach dep_link L from the list. */
282 static void
283 detach_dep_link (dep_link_t l)
285 dep_link_t *prev_nextp = DEP_LINK_PREV_NEXTP (l);
286 dep_link_t next = DEP_LINK_NEXT (l);
288 *prev_nextp = next;
290 if (next != NULL)
291 DEP_LINK_PREV_NEXTP (next) = prev_nextp;
293 DEP_LINK_PREV_NEXTP (l) = NULL;
294 DEP_LINK_NEXT (l) = NULL;
297 /* Remove link LINK from list LIST. */
298 static void
299 remove_from_deps_list (dep_link_t link, deps_list_t list)
301 detach_dep_link (link);
303 /* Don't count debug deps. */
304 if (!depl_on_debug_p (link))
305 --DEPS_LIST_N_LINKS (list);
308 /* Move link LINK from list FROM to list TO. */
309 static void
310 move_dep_link (dep_link_t link, deps_list_t from, deps_list_t to)
312 remove_from_deps_list (link, from);
313 add_to_deps_list (link, to);
316 /* Return true of LINK is not attached to any list. */
317 static bool
318 dep_link_is_detached_p (dep_link_t link)
320 return DEP_LINK_PREV_NEXTP (link) == NULL;
323 /* Pool to hold all dependency nodes (dep_node_t). */
324 static object_allocator<_dep_node> *dn_pool;
326 /* Number of dep_nodes out there. */
327 static int dn_pool_diff = 0;
329 /* Create a dep_node. */
330 static dep_node_t
331 create_dep_node (void)
333 dep_node_t n = dn_pool->allocate ();
334 dep_link_t back = DEP_NODE_BACK (n);
335 dep_link_t forw = DEP_NODE_FORW (n);
337 DEP_LINK_NODE (back) = n;
338 DEP_LINK_NEXT (back) = NULL;
339 DEP_LINK_PREV_NEXTP (back) = NULL;
341 DEP_LINK_NODE (forw) = n;
342 DEP_LINK_NEXT (forw) = NULL;
343 DEP_LINK_PREV_NEXTP (forw) = NULL;
345 ++dn_pool_diff;
347 return n;
350 /* Delete dep_node N. N must not be connected to any deps_list. */
351 static void
352 delete_dep_node (dep_node_t n)
354 gcc_assert (dep_link_is_detached_p (DEP_NODE_BACK (n))
355 && dep_link_is_detached_p (DEP_NODE_FORW (n)));
357 XDELETE (DEP_REPLACE (DEP_NODE_DEP (n)));
359 --dn_pool_diff;
361 dn_pool->remove (n);
364 /* Pool to hold dependencies lists (deps_list_t). */
365 static object_allocator<_deps_list> *dl_pool;
367 /* Number of deps_lists out there. */
368 static int dl_pool_diff = 0;
370 /* Functions to operate with dependences lists - deps_list_t. */
372 /* Return true if list L is empty. */
373 static bool
374 deps_list_empty_p (deps_list_t l)
376 return DEPS_LIST_N_LINKS (l) == 0;
379 /* Create a new deps_list. */
380 static deps_list_t
381 create_deps_list (void)
383 deps_list_t l = dl_pool->allocate ();
385 DEPS_LIST_FIRST (l) = NULL;
386 DEPS_LIST_N_LINKS (l) = 0;
388 ++dl_pool_diff;
389 return l;
392 /* Free deps_list L. */
393 static void
394 free_deps_list (deps_list_t l)
396 gcc_assert (deps_list_empty_p (l));
398 --dl_pool_diff;
400 dl_pool->remove (l);
403 /* Return true if there is no dep_nodes and deps_lists out there.
404 After the region is scheduled all the dependency nodes and lists
405 should [generally] be returned to pool. */
406 bool
407 deps_pools_are_empty_p (void)
409 return dn_pool_diff == 0 && dl_pool_diff == 0;
412 /* Remove all elements from L. */
413 static void
414 clear_deps_list (deps_list_t l)
418 dep_link_t link = DEPS_LIST_FIRST (l);
420 if (link == NULL)
421 break;
423 remove_from_deps_list (link, l);
425 while (1);
428 /* Decide whether a dependency should be treated as a hard or a speculative
429 dependency. */
430 static bool
431 dep_spec_p (dep_t dep)
433 if (current_sched_info->flags & DO_SPECULATION)
435 if (DEP_STATUS (dep) & SPECULATIVE)
436 return true;
438 if (current_sched_info->flags & DO_PREDICATION)
440 if (DEP_TYPE (dep) == REG_DEP_CONTROL)
441 return true;
443 if (DEP_REPLACE (dep) != NULL)
444 return true;
445 return false;
448 static regset reg_pending_sets;
449 static regset reg_pending_clobbers;
450 static regset reg_pending_uses;
451 static regset reg_pending_control_uses;
452 static enum reg_pending_barrier_mode reg_pending_barrier;
454 /* Hard registers implicitly clobbered or used (or may be implicitly
455 clobbered or used) by the currently analyzed insn. For example,
456 insn in its constraint has one register class. Even if there is
457 currently no hard register in the insn, the particular hard
458 register will be in the insn after reload pass because the
459 constraint requires it. */
460 static HARD_REG_SET implicit_reg_pending_clobbers;
461 static HARD_REG_SET implicit_reg_pending_uses;
463 /* To speed up the test for duplicate dependency links we keep a
464 record of dependencies created by add_dependence when the average
465 number of instructions in a basic block is very large.
467 Studies have shown that there is typically around 5 instructions between
468 branches for typical C code. So we can make a guess that the average
469 basic block is approximately 5 instructions long; we will choose 100X
470 the average size as a very large basic block.
472 Each insn has associated bitmaps for its dependencies. Each bitmap
473 has enough entries to represent a dependency on any other insn in
474 the insn chain. All bitmap for true dependencies cache is
475 allocated then the rest two ones are also allocated. */
476 static bitmap_head *true_dependency_cache = NULL;
477 static bitmap_head *output_dependency_cache = NULL;
478 static bitmap_head *anti_dependency_cache = NULL;
479 static bitmap_head *control_dependency_cache = NULL;
480 static bitmap_head *spec_dependency_cache = NULL;
481 static int cache_size;
483 /* True if we should mark added dependencies as a non-register deps. */
484 static bool mark_as_hard;
486 static int deps_may_trap_p (const_rtx);
487 static void add_dependence_1 (rtx_insn *, rtx_insn *, enum reg_note);
488 static void add_dependence_list (rtx_insn *, rtx_insn_list *, int,
489 enum reg_note, bool);
490 static void add_dependence_list_and_free (struct deps_desc *, rtx_insn *,
491 rtx_insn_list **, int, enum reg_note,
492 bool);
493 static void delete_all_dependences (rtx_insn *);
494 static void chain_to_prev_insn (rtx_insn *);
496 static void flush_pending_lists (struct deps_desc *, rtx_insn *, int, int);
497 static void sched_analyze_1 (struct deps_desc *, rtx, rtx_insn *);
498 static void sched_analyze_2 (struct deps_desc *, rtx, rtx_insn *);
499 static void sched_analyze_insn (struct deps_desc *, rtx, rtx_insn *);
501 static bool sched_has_condition_p (const rtx_insn *);
502 static int conditions_mutex_p (const_rtx, const_rtx, bool, bool);
504 static enum DEPS_ADJUST_RESULT maybe_add_or_update_dep_1 (dep_t, bool,
505 rtx, rtx);
506 static enum DEPS_ADJUST_RESULT add_or_update_dep_1 (dep_t, bool, rtx, rtx);
508 #ifdef ENABLE_CHECKING
509 static void check_dep (dep_t, bool);
510 #endif
512 /* Return nonzero if a load of the memory reference MEM can cause a trap. */
514 static int
515 deps_may_trap_p (const_rtx mem)
517 const_rtx addr = XEXP (mem, 0);
519 if (REG_P (addr) && REGNO (addr) >= FIRST_PSEUDO_REGISTER)
521 const_rtx t = get_reg_known_value (REGNO (addr));
522 if (t)
523 addr = t;
525 return rtx_addr_can_trap_p (addr);
529 /* Find the condition under which INSN is executed. If REV is not NULL,
530 it is set to TRUE when the returned comparison should be reversed
531 to get the actual condition. */
532 static rtx
533 sched_get_condition_with_rev_uncached (const rtx_insn *insn, bool *rev)
535 rtx pat = PATTERN (insn);
536 rtx src;
538 if (rev)
539 *rev = false;
541 if (GET_CODE (pat) == COND_EXEC)
542 return COND_EXEC_TEST (pat);
544 if (!any_condjump_p (insn) || !onlyjump_p (insn))
545 return 0;
547 src = SET_SRC (pc_set (insn));
549 if (XEXP (src, 2) == pc_rtx)
550 return XEXP (src, 0);
551 else if (XEXP (src, 1) == pc_rtx)
553 rtx cond = XEXP (src, 0);
554 enum rtx_code revcode = reversed_comparison_code (cond, insn);
556 if (revcode == UNKNOWN)
557 return 0;
559 if (rev)
560 *rev = true;
561 return cond;
564 return 0;
567 /* Return the condition under which INSN does not execute (i.e. the
568 not-taken condition for a conditional branch), or NULL if we cannot
569 find such a condition. The caller should make a copy of the condition
570 before using it. */
572 sched_get_reverse_condition_uncached (const rtx_insn *insn)
574 bool rev;
575 rtx cond = sched_get_condition_with_rev_uncached (insn, &rev);
576 if (cond == NULL_RTX)
577 return cond;
578 if (!rev)
580 enum rtx_code revcode = reversed_comparison_code (cond, insn);
581 cond = gen_rtx_fmt_ee (revcode, GET_MODE (cond),
582 XEXP (cond, 0),
583 XEXP (cond, 1));
585 return cond;
588 /* Caching variant of sched_get_condition_with_rev_uncached.
589 We only do actual work the first time we come here for an insn; the
590 results are cached in INSN_CACHED_COND and INSN_REVERSE_COND. */
591 static rtx
592 sched_get_condition_with_rev (const rtx_insn *insn, bool *rev)
594 bool tmp;
596 if (INSN_LUID (insn) == 0)
597 return sched_get_condition_with_rev_uncached (insn, rev);
599 if (INSN_CACHED_COND (insn) == const_true_rtx)
600 return NULL_RTX;
602 if (INSN_CACHED_COND (insn) != NULL_RTX)
604 if (rev)
605 *rev = INSN_REVERSE_COND (insn);
606 return INSN_CACHED_COND (insn);
609 INSN_CACHED_COND (insn) = sched_get_condition_with_rev_uncached (insn, &tmp);
610 INSN_REVERSE_COND (insn) = tmp;
612 if (INSN_CACHED_COND (insn) == NULL_RTX)
614 INSN_CACHED_COND (insn) = const_true_rtx;
615 return NULL_RTX;
618 if (rev)
619 *rev = INSN_REVERSE_COND (insn);
620 return INSN_CACHED_COND (insn);
623 /* True when we can find a condition under which INSN is executed. */
624 static bool
625 sched_has_condition_p (const rtx_insn *insn)
627 return !! sched_get_condition_with_rev (insn, NULL);
632 /* Return nonzero if conditions COND1 and COND2 can never be both true. */
633 static int
634 conditions_mutex_p (const_rtx cond1, const_rtx cond2, bool rev1, bool rev2)
636 if (COMPARISON_P (cond1)
637 && COMPARISON_P (cond2)
638 && GET_CODE (cond1) ==
639 (rev1==rev2
640 ? reversed_comparison_code (cond2, NULL)
641 : GET_CODE (cond2))
642 && rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
643 && XEXP (cond1, 1) == XEXP (cond2, 1))
644 return 1;
645 return 0;
648 /* Return true if insn1 and insn2 can never depend on one another because
649 the conditions under which they are executed are mutually exclusive. */
650 bool
651 sched_insns_conditions_mutex_p (const rtx_insn *insn1, const rtx_insn *insn2)
653 rtx cond1, cond2;
654 bool rev1 = false, rev2 = false;
656 /* df doesn't handle conditional lifetimes entirely correctly;
657 calls mess up the conditional lifetimes. */
658 if (!CALL_P (insn1) && !CALL_P (insn2))
660 cond1 = sched_get_condition_with_rev (insn1, &rev1);
661 cond2 = sched_get_condition_with_rev (insn2, &rev2);
662 if (cond1 && cond2
663 && conditions_mutex_p (cond1, cond2, rev1, rev2)
664 /* Make sure first instruction doesn't affect condition of second
665 instruction if switched. */
666 && !modified_in_p (cond1, insn2)
667 /* Make sure second instruction doesn't affect condition of first
668 instruction if switched. */
669 && !modified_in_p (cond2, insn1))
670 return true;
672 return false;
676 /* Return true if INSN can potentially be speculated with type DS. */
677 bool
678 sched_insn_is_legitimate_for_speculation_p (const rtx_insn *insn, ds_t ds)
680 if (HAS_INTERNAL_DEP (insn))
681 return false;
683 if (!NONJUMP_INSN_P (insn))
684 return false;
686 if (SCHED_GROUP_P (insn))
687 return false;
689 if (IS_SPECULATION_CHECK_P (CONST_CAST_RTX_INSN (insn)))
690 return false;
692 if (side_effects_p (PATTERN (insn)))
693 return false;
695 if (ds & BE_IN_SPEC)
696 /* The following instructions, which depend on a speculatively scheduled
697 instruction, cannot be speculatively scheduled along. */
699 if (may_trap_or_fault_p (PATTERN (insn)))
700 /* If instruction might fault, it cannot be speculatively scheduled.
701 For control speculation it's obvious why and for data speculation
702 it's because the insn might get wrong input if speculation
703 wasn't successful. */
704 return false;
706 if ((ds & BE_IN_DATA)
707 && sched_has_condition_p (insn))
708 /* If this is a predicated instruction, then it cannot be
709 speculatively scheduled. See PR35659. */
710 return false;
713 return true;
716 /* Initialize LIST_PTR to point to one of the lists present in TYPES_PTR,
717 initialize RESOLVED_P_PTR with true if that list consists of resolved deps,
718 and remove the type of returned [through LIST_PTR] list from TYPES_PTR.
719 This function is used to switch sd_iterator to the next list.
720 !!! For internal use only. Might consider moving it to sched-int.h. */
721 void
722 sd_next_list (const_rtx insn, sd_list_types_def *types_ptr,
723 deps_list_t *list_ptr, bool *resolved_p_ptr)
725 sd_list_types_def types = *types_ptr;
727 if (types & SD_LIST_HARD_BACK)
729 *list_ptr = INSN_HARD_BACK_DEPS (insn);
730 *resolved_p_ptr = false;
731 *types_ptr = types & ~SD_LIST_HARD_BACK;
733 else if (types & SD_LIST_SPEC_BACK)
735 *list_ptr = INSN_SPEC_BACK_DEPS (insn);
736 *resolved_p_ptr = false;
737 *types_ptr = types & ~SD_LIST_SPEC_BACK;
739 else if (types & SD_LIST_FORW)
741 *list_ptr = INSN_FORW_DEPS (insn);
742 *resolved_p_ptr = false;
743 *types_ptr = types & ~SD_LIST_FORW;
745 else if (types & SD_LIST_RES_BACK)
747 *list_ptr = INSN_RESOLVED_BACK_DEPS (insn);
748 *resolved_p_ptr = true;
749 *types_ptr = types & ~SD_LIST_RES_BACK;
751 else if (types & SD_LIST_RES_FORW)
753 *list_ptr = INSN_RESOLVED_FORW_DEPS (insn);
754 *resolved_p_ptr = true;
755 *types_ptr = types & ~SD_LIST_RES_FORW;
757 else
759 *list_ptr = NULL;
760 *resolved_p_ptr = false;
761 *types_ptr = SD_LIST_NONE;
765 /* Return the summary size of INSN's lists defined by LIST_TYPES. */
767 sd_lists_size (const_rtx insn, sd_list_types_def list_types)
769 int size = 0;
771 while (list_types != SD_LIST_NONE)
773 deps_list_t list;
774 bool resolved_p;
776 sd_next_list (insn, &list_types, &list, &resolved_p);
777 if (list)
778 size += DEPS_LIST_N_LINKS (list);
781 return size;
784 /* Return true if INSN's lists defined by LIST_TYPES are all empty. */
786 bool
787 sd_lists_empty_p (const_rtx insn, sd_list_types_def list_types)
789 while (list_types != SD_LIST_NONE)
791 deps_list_t list;
792 bool resolved_p;
794 sd_next_list (insn, &list_types, &list, &resolved_p);
795 if (!deps_list_empty_p (list))
796 return false;
799 return true;
802 /* Initialize data for INSN. */
803 void
804 sd_init_insn (rtx_insn *insn)
806 INSN_HARD_BACK_DEPS (insn) = create_deps_list ();
807 INSN_SPEC_BACK_DEPS (insn) = create_deps_list ();
808 INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list ();
809 INSN_FORW_DEPS (insn) = create_deps_list ();
810 INSN_RESOLVED_FORW_DEPS (insn) = create_deps_list ();
812 /* ??? It would be nice to allocate dependency caches here. */
815 /* Free data for INSN. */
816 void
817 sd_finish_insn (rtx_insn *insn)
819 /* ??? It would be nice to deallocate dependency caches here. */
821 free_deps_list (INSN_HARD_BACK_DEPS (insn));
822 INSN_HARD_BACK_DEPS (insn) = NULL;
824 free_deps_list (INSN_SPEC_BACK_DEPS (insn));
825 INSN_SPEC_BACK_DEPS (insn) = NULL;
827 free_deps_list (INSN_RESOLVED_BACK_DEPS (insn));
828 INSN_RESOLVED_BACK_DEPS (insn) = NULL;
830 free_deps_list (INSN_FORW_DEPS (insn));
831 INSN_FORW_DEPS (insn) = NULL;
833 free_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
834 INSN_RESOLVED_FORW_DEPS (insn) = NULL;
837 /* Find a dependency between producer PRO and consumer CON.
838 Search through resolved dependency lists if RESOLVED_P is true.
839 If no such dependency is found return NULL,
840 otherwise return the dependency and initialize SD_IT_PTR [if it is nonnull]
841 with an iterator pointing to it. */
842 static dep_t
843 sd_find_dep_between_no_cache (rtx pro, rtx con, bool resolved_p,
844 sd_iterator_def *sd_it_ptr)
846 sd_list_types_def pro_list_type;
847 sd_list_types_def con_list_type;
848 sd_iterator_def sd_it;
849 dep_t dep;
850 bool found_p = false;
852 if (resolved_p)
854 pro_list_type = SD_LIST_RES_FORW;
855 con_list_type = SD_LIST_RES_BACK;
857 else
859 pro_list_type = SD_LIST_FORW;
860 con_list_type = SD_LIST_BACK;
863 /* Walk through either back list of INSN or forw list of ELEM
864 depending on which one is shorter. */
865 if (sd_lists_size (con, con_list_type) < sd_lists_size (pro, pro_list_type))
867 /* Find the dep_link with producer PRO in consumer's back_deps. */
868 FOR_EACH_DEP (con, con_list_type, sd_it, dep)
869 if (DEP_PRO (dep) == pro)
871 found_p = true;
872 break;
875 else
877 /* Find the dep_link with consumer CON in producer's forw_deps. */
878 FOR_EACH_DEP (pro, pro_list_type, sd_it, dep)
879 if (DEP_CON (dep) == con)
881 found_p = true;
882 break;
886 if (found_p)
888 if (sd_it_ptr != NULL)
889 *sd_it_ptr = sd_it;
891 return dep;
894 return NULL;
897 /* Find a dependency between producer PRO and consumer CON.
898 Use dependency [if available] to check if dependency is present at all.
899 Search through resolved dependency lists if RESOLVED_P is true.
900 If the dependency or NULL if none found. */
901 dep_t
902 sd_find_dep_between (rtx pro, rtx con, bool resolved_p)
904 if (true_dependency_cache != NULL)
905 /* Avoiding the list walk below can cut compile times dramatically
906 for some code. */
908 int elem_luid = INSN_LUID (pro);
909 int insn_luid = INSN_LUID (con);
911 if (!bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid)
912 && !bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid)
913 && !bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid)
914 && !bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
915 return NULL;
918 return sd_find_dep_between_no_cache (pro, con, resolved_p, NULL);
921 /* Add or update a dependence described by DEP.
922 MEM1 and MEM2, if non-null, correspond to memory locations in case of
923 data speculation.
925 The function returns a value indicating if an old entry has been changed
926 or a new entry has been added to insn's backward deps.
928 This function merely checks if producer and consumer is the same insn
929 and doesn't create a dep in this case. Actual manipulation of
930 dependence data structures is performed in add_or_update_dep_1. */
931 static enum DEPS_ADJUST_RESULT
932 maybe_add_or_update_dep_1 (dep_t dep, bool resolved_p, rtx mem1, rtx mem2)
934 rtx_insn *elem = DEP_PRO (dep);
935 rtx_insn *insn = DEP_CON (dep);
937 gcc_assert (INSN_P (insn) && INSN_P (elem));
939 /* Don't depend an insn on itself. */
940 if (insn == elem)
942 if (sched_deps_info->generate_spec_deps)
943 /* INSN has an internal dependence, which we can't overcome. */
944 HAS_INTERNAL_DEP (insn) = 1;
946 return DEP_NODEP;
949 return add_or_update_dep_1 (dep, resolved_p, mem1, mem2);
952 /* Ask dependency caches what needs to be done for dependence DEP.
953 Return DEP_CREATED if new dependence should be created and there is no
954 need to try to find one searching the dependencies lists.
955 Return DEP_PRESENT if there already is a dependence described by DEP and
956 hence nothing is to be done.
957 Return DEP_CHANGED if there already is a dependence, but it should be
958 updated to incorporate additional information from DEP. */
959 static enum DEPS_ADJUST_RESULT
960 ask_dependency_caches (dep_t dep)
962 int elem_luid = INSN_LUID (DEP_PRO (dep));
963 int insn_luid = INSN_LUID (DEP_CON (dep));
965 gcc_assert (true_dependency_cache != NULL
966 && output_dependency_cache != NULL
967 && anti_dependency_cache != NULL
968 && control_dependency_cache != NULL);
970 if (!(current_sched_info->flags & USE_DEPS_LIST))
972 enum reg_note present_dep_type;
974 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
975 present_dep_type = REG_DEP_TRUE;
976 else if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
977 present_dep_type = REG_DEP_OUTPUT;
978 else if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
979 present_dep_type = REG_DEP_ANTI;
980 else if (bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
981 present_dep_type = REG_DEP_CONTROL;
982 else
983 /* There is no existing dep so it should be created. */
984 return DEP_CREATED;
986 if ((int) DEP_TYPE (dep) >= (int) present_dep_type)
987 /* DEP does not add anything to the existing dependence. */
988 return DEP_PRESENT;
990 else
992 ds_t present_dep_types = 0;
994 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
995 present_dep_types |= DEP_TRUE;
996 if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
997 present_dep_types |= DEP_OUTPUT;
998 if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
999 present_dep_types |= DEP_ANTI;
1000 if (bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
1001 present_dep_types |= DEP_CONTROL;
1003 if (present_dep_types == 0)
1004 /* There is no existing dep so it should be created. */
1005 return DEP_CREATED;
1007 if (!(current_sched_info->flags & DO_SPECULATION)
1008 || !bitmap_bit_p (&spec_dependency_cache[insn_luid], elem_luid))
1010 if ((present_dep_types | (DEP_STATUS (dep) & DEP_TYPES))
1011 == present_dep_types)
1012 /* DEP does not add anything to the existing dependence. */
1013 return DEP_PRESENT;
1015 else
1017 /* Only true dependencies can be data speculative and
1018 only anti dependencies can be control speculative. */
1019 gcc_assert ((present_dep_types & (DEP_TRUE | DEP_ANTI))
1020 == present_dep_types);
1022 /* if (DEP is SPECULATIVE) then
1023 ..we should update DEP_STATUS
1024 else
1025 ..we should reset existing dep to non-speculative. */
1029 return DEP_CHANGED;
1032 /* Set dependency caches according to DEP. */
1033 static void
1034 set_dependency_caches (dep_t dep)
1036 int elem_luid = INSN_LUID (DEP_PRO (dep));
1037 int insn_luid = INSN_LUID (DEP_CON (dep));
1039 if (!(current_sched_info->flags & USE_DEPS_LIST))
1041 switch (DEP_TYPE (dep))
1043 case REG_DEP_TRUE:
1044 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
1045 break;
1047 case REG_DEP_OUTPUT:
1048 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
1049 break;
1051 case REG_DEP_ANTI:
1052 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
1053 break;
1055 case REG_DEP_CONTROL:
1056 bitmap_set_bit (&control_dependency_cache[insn_luid], elem_luid);
1057 break;
1059 default:
1060 gcc_unreachable ();
1063 else
1065 ds_t ds = DEP_STATUS (dep);
1067 if (ds & DEP_TRUE)
1068 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
1069 if (ds & DEP_OUTPUT)
1070 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
1071 if (ds & DEP_ANTI)
1072 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
1073 if (ds & DEP_CONTROL)
1074 bitmap_set_bit (&control_dependency_cache[insn_luid], elem_luid);
1076 if (ds & SPECULATIVE)
1078 gcc_assert (current_sched_info->flags & DO_SPECULATION);
1079 bitmap_set_bit (&spec_dependency_cache[insn_luid], elem_luid);
1084 /* Type of dependence DEP have changed from OLD_TYPE. Update dependency
1085 caches accordingly. */
1086 static void
1087 update_dependency_caches (dep_t dep, enum reg_note old_type)
1089 int elem_luid = INSN_LUID (DEP_PRO (dep));
1090 int insn_luid = INSN_LUID (DEP_CON (dep));
1092 /* Clear corresponding cache entry because type of the link
1093 may have changed. Keep them if we use_deps_list. */
1094 if (!(current_sched_info->flags & USE_DEPS_LIST))
1096 switch (old_type)
1098 case REG_DEP_OUTPUT:
1099 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1100 break;
1102 case REG_DEP_ANTI:
1103 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1104 break;
1106 case REG_DEP_CONTROL:
1107 bitmap_clear_bit (&control_dependency_cache[insn_luid], elem_luid);
1108 break;
1110 default:
1111 gcc_unreachable ();
1115 set_dependency_caches (dep);
1118 /* Convert a dependence pointed to by SD_IT to be non-speculative. */
1119 static void
1120 change_spec_dep_to_hard (sd_iterator_def sd_it)
1122 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1123 dep_link_t link = DEP_NODE_BACK (node);
1124 dep_t dep = DEP_NODE_DEP (node);
1125 rtx_insn *elem = DEP_PRO (dep);
1126 rtx_insn *insn = DEP_CON (dep);
1128 move_dep_link (link, INSN_SPEC_BACK_DEPS (insn), INSN_HARD_BACK_DEPS (insn));
1130 DEP_STATUS (dep) &= ~SPECULATIVE;
1132 if (true_dependency_cache != NULL)
1133 /* Clear the cache entry. */
1134 bitmap_clear_bit (&spec_dependency_cache[INSN_LUID (insn)],
1135 INSN_LUID (elem));
1138 /* Update DEP to incorporate information from NEW_DEP.
1139 SD_IT points to DEP in case it should be moved to another list.
1140 MEM1 and MEM2, if nonnull, correspond to memory locations in case if
1141 data-speculative dependence should be updated. */
1142 static enum DEPS_ADJUST_RESULT
1143 update_dep (dep_t dep, dep_t new_dep,
1144 sd_iterator_def sd_it ATTRIBUTE_UNUSED,
1145 rtx mem1 ATTRIBUTE_UNUSED,
1146 rtx mem2 ATTRIBUTE_UNUSED)
1148 enum DEPS_ADJUST_RESULT res = DEP_PRESENT;
1149 enum reg_note old_type = DEP_TYPE (dep);
1150 bool was_spec = dep_spec_p (dep);
1152 DEP_NONREG (dep) |= DEP_NONREG (new_dep);
1153 DEP_MULTIPLE (dep) = 1;
1155 /* If this is a more restrictive type of dependence than the
1156 existing one, then change the existing dependence to this
1157 type. */
1158 if ((int) DEP_TYPE (new_dep) < (int) old_type)
1160 DEP_TYPE (dep) = DEP_TYPE (new_dep);
1161 res = DEP_CHANGED;
1164 if (current_sched_info->flags & USE_DEPS_LIST)
1165 /* Update DEP_STATUS. */
1167 ds_t dep_status = DEP_STATUS (dep);
1168 ds_t ds = DEP_STATUS (new_dep);
1169 ds_t new_status = ds | dep_status;
1171 if (new_status & SPECULATIVE)
1173 /* Either existing dep or a dep we're adding or both are
1174 speculative. */
1175 if (!(ds & SPECULATIVE)
1176 || !(dep_status & SPECULATIVE))
1177 /* The new dep can't be speculative. */
1178 new_status &= ~SPECULATIVE;
1179 else
1181 /* Both are speculative. Merge probabilities. */
1182 if (mem1 != NULL)
1184 dw_t dw;
1186 dw = estimate_dep_weak (mem1, mem2);
1187 ds = set_dep_weak (ds, BEGIN_DATA, dw);
1190 new_status = ds_merge (dep_status, ds);
1194 ds = new_status;
1196 if (dep_status != ds)
1198 DEP_STATUS (dep) = ds;
1199 res = DEP_CHANGED;
1203 if (was_spec && !dep_spec_p (dep))
1204 /* The old dep was speculative, but now it isn't. */
1205 change_spec_dep_to_hard (sd_it);
1207 if (true_dependency_cache != NULL
1208 && res == DEP_CHANGED)
1209 update_dependency_caches (dep, old_type);
1211 return res;
1214 /* Add or update a dependence described by DEP.
1215 MEM1 and MEM2, if non-null, correspond to memory locations in case of
1216 data speculation.
1218 The function returns a value indicating if an old entry has been changed
1219 or a new entry has been added to insn's backward deps or nothing has
1220 been updated at all. */
1221 static enum DEPS_ADJUST_RESULT
1222 add_or_update_dep_1 (dep_t new_dep, bool resolved_p,
1223 rtx mem1 ATTRIBUTE_UNUSED, rtx mem2 ATTRIBUTE_UNUSED)
1225 bool maybe_present_p = true;
1226 bool present_p = false;
1228 gcc_assert (INSN_P (DEP_PRO (new_dep)) && INSN_P (DEP_CON (new_dep))
1229 && DEP_PRO (new_dep) != DEP_CON (new_dep));
1231 #ifdef ENABLE_CHECKING
1232 check_dep (new_dep, mem1 != NULL);
1233 #endif
1235 if (true_dependency_cache != NULL)
1237 switch (ask_dependency_caches (new_dep))
1239 case DEP_PRESENT:
1240 dep_t present_dep;
1241 sd_iterator_def sd_it;
1243 present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
1244 DEP_CON (new_dep),
1245 resolved_p, &sd_it);
1246 DEP_MULTIPLE (present_dep) = 1;
1247 return DEP_PRESENT;
1249 case DEP_CHANGED:
1250 maybe_present_p = true;
1251 present_p = true;
1252 break;
1254 case DEP_CREATED:
1255 maybe_present_p = false;
1256 present_p = false;
1257 break;
1259 default:
1260 gcc_unreachable ();
1261 break;
1265 /* Check that we don't already have this dependence. */
1266 if (maybe_present_p)
1268 dep_t present_dep;
1269 sd_iterator_def sd_it;
1271 gcc_assert (true_dependency_cache == NULL || present_p);
1273 present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
1274 DEP_CON (new_dep),
1275 resolved_p, &sd_it);
1277 if (present_dep != NULL)
1278 /* We found an existing dependency between ELEM and INSN. */
1279 return update_dep (present_dep, new_dep, sd_it, mem1, mem2);
1280 else
1281 /* We didn't find a dep, it shouldn't present in the cache. */
1282 gcc_assert (!present_p);
1285 /* Might want to check one level of transitivity to save conses.
1286 This check should be done in maybe_add_or_update_dep_1.
1287 Since we made it to add_or_update_dep_1, we must create
1288 (or update) a link. */
1290 if (mem1 != NULL_RTX)
1292 gcc_assert (sched_deps_info->generate_spec_deps);
1293 DEP_STATUS (new_dep) = set_dep_weak (DEP_STATUS (new_dep), BEGIN_DATA,
1294 estimate_dep_weak (mem1, mem2));
1297 sd_add_dep (new_dep, resolved_p);
1299 return DEP_CREATED;
1302 /* Initialize BACK_LIST_PTR with consumer's backward list and
1303 FORW_LIST_PTR with producer's forward list. If RESOLVED_P is true
1304 initialize with lists that hold resolved deps. */
1305 static void
1306 get_back_and_forw_lists (dep_t dep, bool resolved_p,
1307 deps_list_t *back_list_ptr,
1308 deps_list_t *forw_list_ptr)
1310 rtx_insn *con = DEP_CON (dep);
1312 if (!resolved_p)
1314 if (dep_spec_p (dep))
1315 *back_list_ptr = INSN_SPEC_BACK_DEPS (con);
1316 else
1317 *back_list_ptr = INSN_HARD_BACK_DEPS (con);
1319 *forw_list_ptr = INSN_FORW_DEPS (DEP_PRO (dep));
1321 else
1323 *back_list_ptr = INSN_RESOLVED_BACK_DEPS (con);
1324 *forw_list_ptr = INSN_RESOLVED_FORW_DEPS (DEP_PRO (dep));
1328 /* Add dependence described by DEP.
1329 If RESOLVED_P is true treat the dependence as a resolved one. */
1330 void
1331 sd_add_dep (dep_t dep, bool resolved_p)
1333 dep_node_t n = create_dep_node ();
1334 deps_list_t con_back_deps;
1335 deps_list_t pro_forw_deps;
1336 rtx_insn *elem = DEP_PRO (dep);
1337 rtx_insn *insn = DEP_CON (dep);
1339 gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
1341 if ((current_sched_info->flags & DO_SPECULATION) == 0
1342 || !sched_insn_is_legitimate_for_speculation_p (insn, DEP_STATUS (dep)))
1343 DEP_STATUS (dep) &= ~SPECULATIVE;
1345 copy_dep (DEP_NODE_DEP (n), dep);
1347 get_back_and_forw_lists (dep, resolved_p, &con_back_deps, &pro_forw_deps);
1349 add_to_deps_list (DEP_NODE_BACK (n), con_back_deps);
1351 #ifdef ENABLE_CHECKING
1352 check_dep (dep, false);
1353 #endif
1355 add_to_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1357 /* If we are adding a dependency to INSN's LOG_LINKs, then note that
1358 in the bitmap caches of dependency information. */
1359 if (true_dependency_cache != NULL)
1360 set_dependency_caches (dep);
1363 /* Add or update backward dependence between INSN and ELEM
1364 with given type DEP_TYPE and dep_status DS.
1365 This function is a convenience wrapper. */
1366 enum DEPS_ADJUST_RESULT
1367 sd_add_or_update_dep (dep_t dep, bool resolved_p)
1369 return add_or_update_dep_1 (dep, resolved_p, NULL_RTX, NULL_RTX);
1372 /* Resolved dependence pointed to by SD_IT.
1373 SD_IT will advance to the next element. */
1374 void
1375 sd_resolve_dep (sd_iterator_def sd_it)
1377 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1378 dep_t dep = DEP_NODE_DEP (node);
1379 rtx_insn *pro = DEP_PRO (dep);
1380 rtx_insn *con = DEP_CON (dep);
1382 if (dep_spec_p (dep))
1383 move_dep_link (DEP_NODE_BACK (node), INSN_SPEC_BACK_DEPS (con),
1384 INSN_RESOLVED_BACK_DEPS (con));
1385 else
1386 move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
1387 INSN_RESOLVED_BACK_DEPS (con));
1389 move_dep_link (DEP_NODE_FORW (node), INSN_FORW_DEPS (pro),
1390 INSN_RESOLVED_FORW_DEPS (pro));
1393 /* Perform the inverse operation of sd_resolve_dep. Restore the dependence
1394 pointed to by SD_IT to unresolved state. */
1395 void
1396 sd_unresolve_dep (sd_iterator_def sd_it)
1398 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1399 dep_t dep = DEP_NODE_DEP (node);
1400 rtx_insn *pro = DEP_PRO (dep);
1401 rtx_insn *con = DEP_CON (dep);
1403 if (dep_spec_p (dep))
1404 move_dep_link (DEP_NODE_BACK (node), INSN_RESOLVED_BACK_DEPS (con),
1405 INSN_SPEC_BACK_DEPS (con));
1406 else
1407 move_dep_link (DEP_NODE_BACK (node), INSN_RESOLVED_BACK_DEPS (con),
1408 INSN_HARD_BACK_DEPS (con));
1410 move_dep_link (DEP_NODE_FORW (node), INSN_RESOLVED_FORW_DEPS (pro),
1411 INSN_FORW_DEPS (pro));
1414 /* Make TO depend on all the FROM's producers.
1415 If RESOLVED_P is true add dependencies to the resolved lists. */
1416 void
1417 sd_copy_back_deps (rtx_insn *to, rtx_insn *from, bool resolved_p)
1419 sd_list_types_def list_type;
1420 sd_iterator_def sd_it;
1421 dep_t dep;
1423 list_type = resolved_p ? SD_LIST_RES_BACK : SD_LIST_BACK;
1425 FOR_EACH_DEP (from, list_type, sd_it, dep)
1427 dep_def _new_dep, *new_dep = &_new_dep;
1429 copy_dep (new_dep, dep);
1430 DEP_CON (new_dep) = to;
1431 sd_add_dep (new_dep, resolved_p);
1435 /* Remove a dependency referred to by SD_IT.
1436 SD_IT will point to the next dependence after removal. */
1437 void
1438 sd_delete_dep (sd_iterator_def sd_it)
1440 dep_node_t n = DEP_LINK_NODE (*sd_it.linkp);
1441 dep_t dep = DEP_NODE_DEP (n);
1442 rtx_insn *pro = DEP_PRO (dep);
1443 rtx_insn *con = DEP_CON (dep);
1444 deps_list_t con_back_deps;
1445 deps_list_t pro_forw_deps;
1447 if (true_dependency_cache != NULL)
1449 int elem_luid = INSN_LUID (pro);
1450 int insn_luid = INSN_LUID (con);
1452 bitmap_clear_bit (&true_dependency_cache[insn_luid], elem_luid);
1453 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1454 bitmap_clear_bit (&control_dependency_cache[insn_luid], elem_luid);
1455 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1457 if (current_sched_info->flags & DO_SPECULATION)
1458 bitmap_clear_bit (&spec_dependency_cache[insn_luid], elem_luid);
1461 get_back_and_forw_lists (dep, sd_it.resolved_p,
1462 &con_back_deps, &pro_forw_deps);
1464 remove_from_deps_list (DEP_NODE_BACK (n), con_back_deps);
1465 remove_from_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1467 delete_dep_node (n);
1470 /* Dump size of the lists. */
1471 #define DUMP_LISTS_SIZE (2)
1473 /* Dump dependencies of the lists. */
1474 #define DUMP_LISTS_DEPS (4)
1476 /* Dump all information about the lists. */
1477 #define DUMP_LISTS_ALL (DUMP_LISTS_SIZE | DUMP_LISTS_DEPS)
1479 /* Dump deps_lists of INSN specified by TYPES to DUMP.
1480 FLAGS is a bit mask specifying what information about the lists needs
1481 to be printed.
1482 If FLAGS has the very first bit set, then dump all information about
1483 the lists and propagate this bit into the callee dump functions. */
1484 static void
1485 dump_lists (FILE *dump, rtx insn, sd_list_types_def types, int flags)
1487 sd_iterator_def sd_it;
1488 dep_t dep;
1489 int all;
1491 all = (flags & 1);
1493 if (all)
1494 flags |= DUMP_LISTS_ALL;
1496 fprintf (dump, "[");
1498 if (flags & DUMP_LISTS_SIZE)
1499 fprintf (dump, "%d; ", sd_lists_size (insn, types));
1501 if (flags & DUMP_LISTS_DEPS)
1503 FOR_EACH_DEP (insn, types, sd_it, dep)
1505 dump_dep (dump, dep, dump_dep_flags | all);
1506 fprintf (dump, " ");
1511 /* Dump all information about deps_lists of INSN specified by TYPES
1512 to STDERR. */
1513 void
1514 sd_debug_lists (rtx insn, sd_list_types_def types)
1516 dump_lists (stderr, insn, types, 1);
1517 fprintf (stderr, "\n");
1520 /* A wrapper around add_dependence_1, to add a dependence of CON on
1521 PRO, with type DEP_TYPE. This function implements special handling
1522 for REG_DEP_CONTROL dependencies. For these, we optionally promote
1523 the type to REG_DEP_ANTI if we can determine that predication is
1524 impossible; otherwise we add additional true dependencies on the
1525 INSN_COND_DEPS list of the jump (which PRO must be). */
1526 void
1527 add_dependence (rtx_insn *con, rtx_insn *pro, enum reg_note dep_type)
1529 if (dep_type == REG_DEP_CONTROL
1530 && !(current_sched_info->flags & DO_PREDICATION))
1531 dep_type = REG_DEP_ANTI;
1533 /* A REG_DEP_CONTROL dependence may be eliminated through predication,
1534 so we must also make the insn dependent on the setter of the
1535 condition. */
1536 if (dep_type == REG_DEP_CONTROL)
1538 rtx_insn *real_pro = pro;
1539 rtx_insn *other = real_insn_for_shadow (real_pro);
1540 rtx cond;
1542 if (other != NULL_RTX)
1543 real_pro = other;
1544 cond = sched_get_reverse_condition_uncached (real_pro);
1545 /* Verify that the insn does not use a different value in
1546 the condition register than the one that was present at
1547 the jump. */
1548 if (cond == NULL_RTX)
1549 dep_type = REG_DEP_ANTI;
1550 else if (INSN_CACHED_COND (real_pro) == const_true_rtx)
1552 HARD_REG_SET uses;
1553 CLEAR_HARD_REG_SET (uses);
1554 note_uses (&PATTERN (con), record_hard_reg_uses, &uses);
1555 if (TEST_HARD_REG_BIT (uses, REGNO (XEXP (cond, 0))))
1556 dep_type = REG_DEP_ANTI;
1558 if (dep_type == REG_DEP_CONTROL)
1560 if (sched_verbose >= 5)
1561 fprintf (sched_dump, "making DEP_CONTROL for %d\n",
1562 INSN_UID (real_pro));
1563 add_dependence_list (con, INSN_COND_DEPS (real_pro), 0,
1564 REG_DEP_TRUE, false);
1568 add_dependence_1 (con, pro, dep_type);
1571 /* A convenience wrapper to operate on an entire list. HARD should be
1572 true if DEP_NONREG should be set on newly created dependencies. */
1574 static void
1575 add_dependence_list (rtx_insn *insn, rtx_insn_list *list, int uncond,
1576 enum reg_note dep_type, bool hard)
1578 mark_as_hard = hard;
1579 for (; list; list = list->next ())
1581 if (uncond || ! sched_insns_conditions_mutex_p (insn, list->insn ()))
1582 add_dependence (insn, list->insn (), dep_type);
1584 mark_as_hard = false;
1587 /* Similar, but free *LISTP at the same time, when the context
1588 is not readonly. HARD should be true if DEP_NONREG should be set on
1589 newly created dependencies. */
1591 static void
1592 add_dependence_list_and_free (struct deps_desc *deps, rtx_insn *insn,
1593 rtx_insn_list **listp,
1594 int uncond, enum reg_note dep_type, bool hard)
1596 add_dependence_list (insn, *listp, uncond, dep_type, hard);
1598 /* We don't want to short-circuit dependencies involving debug
1599 insns, because they may cause actual dependencies to be
1600 disregarded. */
1601 if (deps->readonly || DEBUG_INSN_P (insn))
1602 return;
1604 free_INSN_LIST_list (listp);
1607 /* Remove all occurrences of INSN from LIST. Return the number of
1608 occurrences removed. */
1610 static int
1611 remove_from_dependence_list (rtx_insn *insn, rtx_insn_list **listp)
1613 int removed = 0;
1615 while (*listp)
1617 if ((*listp)->insn () == insn)
1619 remove_free_INSN_LIST_node (listp);
1620 removed++;
1621 continue;
1624 listp = (rtx_insn_list **)&XEXP (*listp, 1);
1627 return removed;
1630 /* Same as above, but process two lists at once. */
1631 static int
1632 remove_from_both_dependence_lists (rtx_insn *insn,
1633 rtx_insn_list **listp,
1634 rtx_expr_list **exprp)
1636 int removed = 0;
1638 while (*listp)
1640 if (XEXP (*listp, 0) == insn)
1642 remove_free_INSN_LIST_node (listp);
1643 remove_free_EXPR_LIST_node (exprp);
1644 removed++;
1645 continue;
1648 listp = (rtx_insn_list **)&XEXP (*listp, 1);
1649 exprp = (rtx_expr_list **)&XEXP (*exprp, 1);
1652 return removed;
1655 /* Clear all dependencies for an insn. */
1656 static void
1657 delete_all_dependences (rtx_insn *insn)
1659 sd_iterator_def sd_it;
1660 dep_t dep;
1662 /* The below cycle can be optimized to clear the caches and back_deps
1663 in one call but that would provoke duplication of code from
1664 delete_dep (). */
1666 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1667 sd_iterator_cond (&sd_it, &dep);)
1668 sd_delete_dep (sd_it);
1671 /* All insns in a scheduling group except the first should only have
1672 dependencies on the previous insn in the group. So we find the
1673 first instruction in the scheduling group by walking the dependence
1674 chains backwards. Then we add the dependencies for the group to
1675 the previous nonnote insn. */
1677 static void
1678 chain_to_prev_insn (rtx_insn *insn)
1680 sd_iterator_def sd_it;
1681 dep_t dep;
1682 rtx_insn *prev_nonnote;
1684 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
1686 rtx_insn *i = insn;
1687 rtx_insn *pro = DEP_PRO (dep);
1691 i = prev_nonnote_insn (i);
1693 if (pro == i)
1694 goto next_link;
1695 } while (SCHED_GROUP_P (i) || DEBUG_INSN_P (i));
1697 if (! sched_insns_conditions_mutex_p (i, pro))
1698 add_dependence (i, pro, DEP_TYPE (dep));
1699 next_link:;
1702 delete_all_dependences (insn);
1704 prev_nonnote = prev_nonnote_nondebug_insn (insn);
1705 if (BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (prev_nonnote)
1706 && ! sched_insns_conditions_mutex_p (insn, prev_nonnote))
1707 add_dependence (insn, prev_nonnote, REG_DEP_ANTI);
1710 /* Process an insn's memory dependencies. There are four kinds of
1711 dependencies:
1713 (0) read dependence: read follows read
1714 (1) true dependence: read follows write
1715 (2) output dependence: write follows write
1716 (3) anti dependence: write follows read
1718 We are careful to build only dependencies which actually exist, and
1719 use transitivity to avoid building too many links. */
1721 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1722 The MEM is a memory reference contained within INSN, which we are saving
1723 so that we can do memory aliasing on it. */
1725 static void
1726 add_insn_mem_dependence (struct deps_desc *deps, bool read_p,
1727 rtx_insn *insn, rtx mem)
1729 rtx_insn_list **insn_list;
1730 rtx_insn_list *insn_node;
1731 rtx_expr_list **mem_list;
1732 rtx_expr_list *mem_node;
1734 gcc_assert (!deps->readonly);
1735 if (read_p)
1737 insn_list = &deps->pending_read_insns;
1738 mem_list = &deps->pending_read_mems;
1739 if (!DEBUG_INSN_P (insn))
1740 deps->pending_read_list_length++;
1742 else
1744 insn_list = &deps->pending_write_insns;
1745 mem_list = &deps->pending_write_mems;
1746 deps->pending_write_list_length++;
1749 insn_node = alloc_INSN_LIST (insn, *insn_list);
1750 *insn_list = insn_node;
1752 if (sched_deps_info->use_cselib)
1754 mem = shallow_copy_rtx (mem);
1755 XEXP (mem, 0) = cselib_subst_to_values_from_insn (XEXP (mem, 0),
1756 GET_MODE (mem), insn);
1758 mem_node = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
1759 *mem_list = mem_node;
1762 /* Make a dependency between every memory reference on the pending lists
1763 and INSN, thus flushing the pending lists. FOR_READ is true if emitting
1764 dependencies for a read operation, similarly with FOR_WRITE. */
1766 static void
1767 flush_pending_lists (struct deps_desc *deps, rtx_insn *insn, int for_read,
1768 int for_write)
1770 if (for_write)
1772 add_dependence_list_and_free (deps, insn, &deps->pending_read_insns,
1773 1, REG_DEP_ANTI, true);
1774 if (!deps->readonly)
1776 free_EXPR_LIST_list (&deps->pending_read_mems);
1777 deps->pending_read_list_length = 0;
1781 add_dependence_list_and_free (deps, insn, &deps->pending_write_insns, 1,
1782 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT,
1783 true);
1785 add_dependence_list_and_free (deps, insn,
1786 &deps->last_pending_memory_flush, 1,
1787 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT,
1788 true);
1790 add_dependence_list_and_free (deps, insn, &deps->pending_jump_insns, 1,
1791 REG_DEP_ANTI, true);
1793 if (DEBUG_INSN_P (insn))
1795 if (for_write)
1796 free_INSN_LIST_list (&deps->pending_read_insns);
1797 free_INSN_LIST_list (&deps->pending_write_insns);
1798 free_INSN_LIST_list (&deps->last_pending_memory_flush);
1799 free_INSN_LIST_list (&deps->pending_jump_insns);
1802 if (!deps->readonly)
1804 free_EXPR_LIST_list (&deps->pending_write_mems);
1805 deps->pending_write_list_length = 0;
1807 deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
1808 deps->pending_flush_length = 1;
1810 mark_as_hard = false;
1813 /* Instruction which dependencies we are analyzing. */
1814 static rtx_insn *cur_insn = NULL;
1816 /* Implement hooks for haifa scheduler. */
1818 static void
1819 haifa_start_insn (rtx_insn *insn)
1821 gcc_assert (insn && !cur_insn);
1823 cur_insn = insn;
1826 static void
1827 haifa_finish_insn (void)
1829 cur_insn = NULL;
1832 void
1833 haifa_note_reg_set (int regno)
1835 SET_REGNO_REG_SET (reg_pending_sets, regno);
1838 void
1839 haifa_note_reg_clobber (int regno)
1841 SET_REGNO_REG_SET (reg_pending_clobbers, regno);
1844 void
1845 haifa_note_reg_use (int regno)
1847 SET_REGNO_REG_SET (reg_pending_uses, regno);
1850 static void
1851 haifa_note_mem_dep (rtx mem, rtx pending_mem, rtx_insn *pending_insn, ds_t ds)
1853 if (!(ds & SPECULATIVE))
1855 mem = NULL_RTX;
1856 pending_mem = NULL_RTX;
1858 else
1859 gcc_assert (ds & BEGIN_DATA);
1862 dep_def _dep, *dep = &_dep;
1864 init_dep_1 (dep, pending_insn, cur_insn, ds_to_dt (ds),
1865 current_sched_info->flags & USE_DEPS_LIST ? ds : 0);
1866 DEP_NONREG (dep) = 1;
1867 maybe_add_or_update_dep_1 (dep, false, pending_mem, mem);
1872 static void
1873 haifa_note_dep (rtx_insn *elem, ds_t ds)
1875 dep_def _dep;
1876 dep_t dep = &_dep;
1878 init_dep (dep, elem, cur_insn, ds_to_dt (ds));
1879 if (mark_as_hard)
1880 DEP_NONREG (dep) = 1;
1881 maybe_add_or_update_dep_1 (dep, false, NULL_RTX, NULL_RTX);
1884 static void
1885 note_reg_use (int r)
1887 if (sched_deps_info->note_reg_use)
1888 sched_deps_info->note_reg_use (r);
1891 static void
1892 note_reg_set (int r)
1894 if (sched_deps_info->note_reg_set)
1895 sched_deps_info->note_reg_set (r);
1898 static void
1899 note_reg_clobber (int r)
1901 if (sched_deps_info->note_reg_clobber)
1902 sched_deps_info->note_reg_clobber (r);
1905 static void
1906 note_mem_dep (rtx m1, rtx m2, rtx_insn *e, ds_t ds)
1908 if (sched_deps_info->note_mem_dep)
1909 sched_deps_info->note_mem_dep (m1, m2, e, ds);
1912 static void
1913 note_dep (rtx_insn *e, ds_t ds)
1915 if (sched_deps_info->note_dep)
1916 sched_deps_info->note_dep (e, ds);
1919 /* Return corresponding to DS reg_note. */
1920 enum reg_note
1921 ds_to_dt (ds_t ds)
1923 if (ds & DEP_TRUE)
1924 return REG_DEP_TRUE;
1925 else if (ds & DEP_OUTPUT)
1926 return REG_DEP_OUTPUT;
1927 else if (ds & DEP_ANTI)
1928 return REG_DEP_ANTI;
1929 else
1931 gcc_assert (ds & DEP_CONTROL);
1932 return REG_DEP_CONTROL;
1938 /* Functions for computation of info needed for register pressure
1939 sensitive insn scheduling. */
1942 /* Allocate and return reg_use_data structure for REGNO and INSN. */
1943 static struct reg_use_data *
1944 create_insn_reg_use (int regno, rtx_insn *insn)
1946 struct reg_use_data *use;
1948 use = (struct reg_use_data *) xmalloc (sizeof (struct reg_use_data));
1949 use->regno = regno;
1950 use->insn = insn;
1951 use->next_insn_use = INSN_REG_USE_LIST (insn);
1952 INSN_REG_USE_LIST (insn) = use;
1953 return use;
1956 /* Allocate reg_set_data structure for REGNO and INSN. */
1957 static void
1958 create_insn_reg_set (int regno, rtx insn)
1960 struct reg_set_data *set;
1962 set = (struct reg_set_data *) xmalloc (sizeof (struct reg_set_data));
1963 set->regno = regno;
1964 set->insn = insn;
1965 set->next_insn_set = INSN_REG_SET_LIST (insn);
1966 INSN_REG_SET_LIST (insn) = set;
1969 /* Set up insn register uses for INSN and dependency context DEPS. */
1970 static void
1971 setup_insn_reg_uses (struct deps_desc *deps, rtx_insn *insn)
1973 unsigned i;
1974 reg_set_iterator rsi;
1975 struct reg_use_data *use, *use2, *next;
1976 struct deps_reg *reg_last;
1978 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
1980 if (i < FIRST_PSEUDO_REGISTER
1981 && TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
1982 continue;
1984 if (find_regno_note (insn, REG_DEAD, i) == NULL_RTX
1985 && ! REGNO_REG_SET_P (reg_pending_sets, i)
1986 && ! REGNO_REG_SET_P (reg_pending_clobbers, i))
1987 /* Ignore use which is not dying. */
1988 continue;
1990 use = create_insn_reg_use (i, insn);
1991 use->next_regno_use = use;
1992 reg_last = &deps->reg_last[i];
1994 /* Create the cycle list of uses. */
1995 for (rtx_insn_list *list = reg_last->uses; list; list = list->next ())
1997 use2 = create_insn_reg_use (i, list->insn ());
1998 next = use->next_regno_use;
1999 use->next_regno_use = use2;
2000 use2->next_regno_use = next;
2005 /* Register pressure info for the currently processed insn. */
2006 static struct reg_pressure_data reg_pressure_info[N_REG_CLASSES];
2008 /* Return TRUE if INSN has the use structure for REGNO. */
2009 static bool
2010 insn_use_p (rtx insn, int regno)
2012 struct reg_use_data *use;
2014 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
2015 if (use->regno == regno)
2016 return true;
2017 return false;
2020 /* Update the register pressure info after birth of pseudo register REGNO
2021 in INSN. Arguments CLOBBER_P and UNUSED_P say correspondingly that
2022 the register is in clobber or unused after the insn. */
2023 static void
2024 mark_insn_pseudo_birth (rtx insn, int regno, bool clobber_p, bool unused_p)
2026 int incr, new_incr;
2027 enum reg_class cl;
2029 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
2030 cl = sched_regno_pressure_class[regno];
2031 if (cl != NO_REGS)
2033 incr = ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (regno)];
2034 if (clobber_p)
2036 new_incr = reg_pressure_info[cl].clobber_increase + incr;
2037 reg_pressure_info[cl].clobber_increase = new_incr;
2039 else if (unused_p)
2041 new_incr = reg_pressure_info[cl].unused_set_increase + incr;
2042 reg_pressure_info[cl].unused_set_increase = new_incr;
2044 else
2046 new_incr = reg_pressure_info[cl].set_increase + incr;
2047 reg_pressure_info[cl].set_increase = new_incr;
2048 if (! insn_use_p (insn, regno))
2049 reg_pressure_info[cl].change += incr;
2050 create_insn_reg_set (regno, insn);
2052 gcc_assert (new_incr < (1 << INCREASE_BITS));
2056 /* Like mark_insn_pseudo_regno_birth except that NREGS saying how many
2057 hard registers involved in the birth. */
2058 static void
2059 mark_insn_hard_regno_birth (rtx insn, int regno, int nregs,
2060 bool clobber_p, bool unused_p)
2062 enum reg_class cl;
2063 int new_incr, last = regno + nregs;
2065 while (regno < last)
2067 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2068 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
2070 cl = sched_regno_pressure_class[regno];
2071 if (cl != NO_REGS)
2073 if (clobber_p)
2075 new_incr = reg_pressure_info[cl].clobber_increase + 1;
2076 reg_pressure_info[cl].clobber_increase = new_incr;
2078 else if (unused_p)
2080 new_incr = reg_pressure_info[cl].unused_set_increase + 1;
2081 reg_pressure_info[cl].unused_set_increase = new_incr;
2083 else
2085 new_incr = reg_pressure_info[cl].set_increase + 1;
2086 reg_pressure_info[cl].set_increase = new_incr;
2087 if (! insn_use_p (insn, regno))
2088 reg_pressure_info[cl].change += 1;
2089 create_insn_reg_set (regno, insn);
2091 gcc_assert (new_incr < (1 << INCREASE_BITS));
2094 regno++;
2098 /* Update the register pressure info after birth of pseudo or hard
2099 register REG in INSN. Arguments CLOBBER_P and UNUSED_P say
2100 correspondingly that the register is in clobber or unused after the
2101 insn. */
2102 static void
2103 mark_insn_reg_birth (rtx insn, rtx reg, bool clobber_p, bool unused_p)
2105 int regno;
2107 if (GET_CODE (reg) == SUBREG)
2108 reg = SUBREG_REG (reg);
2110 if (! REG_P (reg))
2111 return;
2113 regno = REGNO (reg);
2114 if (regno < FIRST_PSEUDO_REGISTER)
2115 mark_insn_hard_regno_birth (insn, regno, REG_NREGS (reg),
2116 clobber_p, unused_p);
2117 else
2118 mark_insn_pseudo_birth (insn, regno, clobber_p, unused_p);
2121 /* Update the register pressure info after death of pseudo register
2122 REGNO. */
2123 static void
2124 mark_pseudo_death (int regno)
2126 int incr;
2127 enum reg_class cl;
2129 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
2130 cl = sched_regno_pressure_class[regno];
2131 if (cl != NO_REGS)
2133 incr = ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (regno)];
2134 reg_pressure_info[cl].change -= incr;
2138 /* Like mark_pseudo_death except that NREGS saying how many hard
2139 registers involved in the death. */
2140 static void
2141 mark_hard_regno_death (int regno, int nregs)
2143 enum reg_class cl;
2144 int last = regno + nregs;
2146 while (regno < last)
2148 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2149 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
2151 cl = sched_regno_pressure_class[regno];
2152 if (cl != NO_REGS)
2153 reg_pressure_info[cl].change -= 1;
2155 regno++;
2159 /* Update the register pressure info after death of pseudo or hard
2160 register REG. */
2161 static void
2162 mark_reg_death (rtx reg)
2164 int regno;
2166 if (GET_CODE (reg) == SUBREG)
2167 reg = SUBREG_REG (reg);
2169 if (! REG_P (reg))
2170 return;
2172 regno = REGNO (reg);
2173 if (regno < FIRST_PSEUDO_REGISTER)
2174 mark_hard_regno_death (regno, REG_NREGS (reg));
2175 else
2176 mark_pseudo_death (regno);
2179 /* Process SETTER of REG. DATA is an insn containing the setter. */
2180 static void
2181 mark_insn_reg_store (rtx reg, const_rtx setter, void *data)
2183 if (setter != NULL_RTX && GET_CODE (setter) != SET)
2184 return;
2185 mark_insn_reg_birth
2186 ((rtx) data, reg, false,
2187 find_reg_note ((const_rtx) data, REG_UNUSED, reg) != NULL_RTX);
2190 /* Like mark_insn_reg_store except notice just CLOBBERs; ignore SETs. */
2191 static void
2192 mark_insn_reg_clobber (rtx reg, const_rtx setter, void *data)
2194 if (GET_CODE (setter) == CLOBBER)
2195 mark_insn_reg_birth ((rtx) data, reg, true, false);
2198 /* Set up reg pressure info related to INSN. */
2199 void
2200 init_insn_reg_pressure_info (rtx_insn *insn)
2202 int i, len;
2203 enum reg_class cl;
2204 static struct reg_pressure_data *pressure_info;
2205 rtx link;
2207 gcc_assert (sched_pressure != SCHED_PRESSURE_NONE);
2209 if (! INSN_P (insn))
2210 return;
2212 for (i = 0; i < ira_pressure_classes_num; i++)
2214 cl = ira_pressure_classes[i];
2215 reg_pressure_info[cl].clobber_increase = 0;
2216 reg_pressure_info[cl].set_increase = 0;
2217 reg_pressure_info[cl].unused_set_increase = 0;
2218 reg_pressure_info[cl].change = 0;
2221 note_stores (PATTERN (insn), mark_insn_reg_clobber, insn);
2223 note_stores (PATTERN (insn), mark_insn_reg_store, insn);
2225 if (AUTO_INC_DEC)
2226 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2227 if (REG_NOTE_KIND (link) == REG_INC)
2228 mark_insn_reg_store (XEXP (link, 0), NULL_RTX, insn);
2230 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2231 if (REG_NOTE_KIND (link) == REG_DEAD)
2232 mark_reg_death (XEXP (link, 0));
2234 len = sizeof (struct reg_pressure_data) * ira_pressure_classes_num;
2235 pressure_info
2236 = INSN_REG_PRESSURE (insn) = (struct reg_pressure_data *) xmalloc (len);
2237 if (sched_pressure == SCHED_PRESSURE_WEIGHTED)
2238 INSN_MAX_REG_PRESSURE (insn) = (int *) xcalloc (ira_pressure_classes_num
2239 * sizeof (int), 1);
2240 for (i = 0; i < ira_pressure_classes_num; i++)
2242 cl = ira_pressure_classes[i];
2243 pressure_info[i].clobber_increase
2244 = reg_pressure_info[cl].clobber_increase;
2245 pressure_info[i].set_increase = reg_pressure_info[cl].set_increase;
2246 pressure_info[i].unused_set_increase
2247 = reg_pressure_info[cl].unused_set_increase;
2248 pressure_info[i].change = reg_pressure_info[cl].change;
2255 /* Internal variable for sched_analyze_[12] () functions.
2256 If it is nonzero, this means that sched_analyze_[12] looks
2257 at the most toplevel SET. */
2258 static bool can_start_lhs_rhs_p;
2260 /* Extend reg info for the deps context DEPS given that
2261 we have just generated a register numbered REGNO. */
2262 static void
2263 extend_deps_reg_info (struct deps_desc *deps, int regno)
2265 int max_regno = regno + 1;
2267 gcc_assert (!reload_completed);
2269 /* In a readonly context, it would not hurt to extend info,
2270 but it should not be needed. */
2271 if (reload_completed && deps->readonly)
2273 deps->max_reg = max_regno;
2274 return;
2277 if (max_regno > deps->max_reg)
2279 deps->reg_last = XRESIZEVEC (struct deps_reg, deps->reg_last,
2280 max_regno);
2281 memset (&deps->reg_last[deps->max_reg],
2282 0, (max_regno - deps->max_reg)
2283 * sizeof (struct deps_reg));
2284 deps->max_reg = max_regno;
2288 /* Extends REG_INFO_P if needed. */
2289 void
2290 maybe_extend_reg_info_p (void)
2292 /* Extend REG_INFO_P, if needed. */
2293 if ((unsigned int)max_regno - 1 >= reg_info_p_size)
2295 size_t new_reg_info_p_size = max_regno + 128;
2297 gcc_assert (!reload_completed && sel_sched_p ());
2299 reg_info_p = (struct reg_info_t *) xrecalloc (reg_info_p,
2300 new_reg_info_p_size,
2301 reg_info_p_size,
2302 sizeof (*reg_info_p));
2303 reg_info_p_size = new_reg_info_p_size;
2307 /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
2308 The type of the reference is specified by REF and can be SET,
2309 CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE. */
2311 static void
2312 sched_analyze_reg (struct deps_desc *deps, int regno, machine_mode mode,
2313 enum rtx_code ref, rtx_insn *insn)
2315 /* We could emit new pseudos in renaming. Extend the reg structures. */
2316 if (!reload_completed && sel_sched_p ()
2317 && (regno >= max_reg_num () - 1 || regno >= deps->max_reg))
2318 extend_deps_reg_info (deps, regno);
2320 maybe_extend_reg_info_p ();
2322 /* A hard reg in a wide mode may really be multiple registers.
2323 If so, mark all of them just like the first. */
2324 if (regno < FIRST_PSEUDO_REGISTER)
2326 int i = hard_regno_nregs[regno][mode];
2327 if (ref == SET)
2329 while (--i >= 0)
2330 note_reg_set (regno + i);
2332 else if (ref == USE)
2334 while (--i >= 0)
2335 note_reg_use (regno + i);
2337 else
2339 while (--i >= 0)
2340 note_reg_clobber (regno + i);
2344 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
2345 it does not reload. Ignore these as they have served their
2346 purpose already. */
2347 else if (regno >= deps->max_reg)
2349 enum rtx_code code = GET_CODE (PATTERN (insn));
2350 gcc_assert (code == USE || code == CLOBBER);
2353 else
2355 if (ref == SET)
2356 note_reg_set (regno);
2357 else if (ref == USE)
2358 note_reg_use (regno);
2359 else
2360 note_reg_clobber (regno);
2362 /* Pseudos that are REG_EQUIV to something may be replaced
2363 by that during reloading. We need only add dependencies for
2364 the address in the REG_EQUIV note. */
2365 if (!reload_completed && get_reg_known_equiv_p (regno))
2367 rtx t = get_reg_known_value (regno);
2368 if (MEM_P (t))
2369 sched_analyze_2 (deps, XEXP (t, 0), insn);
2372 /* Don't let it cross a call after scheduling if it doesn't
2373 already cross one. */
2374 if (REG_N_CALLS_CROSSED (regno) == 0)
2376 if (!deps->readonly && ref == USE && !DEBUG_INSN_P (insn))
2377 deps->sched_before_next_call
2378 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
2379 else
2380 add_dependence_list (insn, deps->last_function_call, 1,
2381 REG_DEP_ANTI, false);
2386 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
2387 rtx, X, creating all dependencies generated by the write to the
2388 destination of X, and reads of everything mentioned. */
2390 static void
2391 sched_analyze_1 (struct deps_desc *deps, rtx x, rtx_insn *insn)
2393 rtx dest = XEXP (x, 0);
2394 enum rtx_code code = GET_CODE (x);
2395 bool cslr_p = can_start_lhs_rhs_p;
2397 can_start_lhs_rhs_p = false;
2399 gcc_assert (dest);
2400 if (dest == 0)
2401 return;
2403 if (cslr_p && sched_deps_info->start_lhs)
2404 sched_deps_info->start_lhs (dest);
2406 if (GET_CODE (dest) == PARALLEL)
2408 int i;
2410 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
2411 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
2412 sched_analyze_1 (deps,
2413 gen_rtx_CLOBBER (VOIDmode,
2414 XEXP (XVECEXP (dest, 0, i), 0)),
2415 insn);
2417 if (cslr_p && sched_deps_info->finish_lhs)
2418 sched_deps_info->finish_lhs ();
2420 if (code == SET)
2422 can_start_lhs_rhs_p = cslr_p;
2424 sched_analyze_2 (deps, SET_SRC (x), insn);
2426 can_start_lhs_rhs_p = false;
2429 return;
2432 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
2433 || GET_CODE (dest) == ZERO_EXTRACT)
2435 if (GET_CODE (dest) == STRICT_LOW_PART
2436 || GET_CODE (dest) == ZERO_EXTRACT
2437 || df_read_modify_subreg_p (dest))
2439 /* These both read and modify the result. We must handle
2440 them as writes to get proper dependencies for following
2441 instructions. We must handle them as reads to get proper
2442 dependencies from this to previous instructions.
2443 Thus we need to call sched_analyze_2. */
2445 sched_analyze_2 (deps, XEXP (dest, 0), insn);
2447 if (GET_CODE (dest) == ZERO_EXTRACT)
2449 /* The second and third arguments are values read by this insn. */
2450 sched_analyze_2 (deps, XEXP (dest, 1), insn);
2451 sched_analyze_2 (deps, XEXP (dest, 2), insn);
2453 dest = XEXP (dest, 0);
2456 if (REG_P (dest))
2458 int regno = REGNO (dest);
2459 machine_mode mode = GET_MODE (dest);
2461 sched_analyze_reg (deps, regno, mode, code, insn);
2463 #ifdef STACK_REGS
2464 /* Treat all writes to a stack register as modifying the TOS. */
2465 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
2467 /* Avoid analyzing the same register twice. */
2468 if (regno != FIRST_STACK_REG)
2469 sched_analyze_reg (deps, FIRST_STACK_REG, mode, code, insn);
2471 add_to_hard_reg_set (&implicit_reg_pending_uses, mode,
2472 FIRST_STACK_REG);
2474 #endif
2476 else if (MEM_P (dest))
2478 /* Writing memory. */
2479 rtx t = dest;
2481 if (sched_deps_info->use_cselib)
2483 machine_mode address_mode = get_address_mode (dest);
2485 t = shallow_copy_rtx (dest);
2486 cselib_lookup_from_insn (XEXP (t, 0), address_mode, 1,
2487 GET_MODE (t), insn);
2488 XEXP (t, 0)
2489 = cselib_subst_to_values_from_insn (XEXP (t, 0), GET_MODE (t),
2490 insn);
2492 t = canon_rtx (t);
2494 /* Pending lists can't get larger with a readonly context. */
2495 if (!deps->readonly
2496 && ((deps->pending_read_list_length + deps->pending_write_list_length)
2497 >= MAX_PENDING_LIST_LENGTH))
2499 /* Flush all pending reads and writes to prevent the pending lists
2500 from getting any larger. Insn scheduling runs too slowly when
2501 these lists get long. When compiling GCC with itself,
2502 this flush occurs 8 times for sparc, and 10 times for m88k using
2503 the default value of 32. */
2504 flush_pending_lists (deps, insn, false, true);
2506 else
2508 rtx_insn_list *pending;
2509 rtx_expr_list *pending_mem;
2511 pending = deps->pending_read_insns;
2512 pending_mem = deps->pending_read_mems;
2513 while (pending)
2515 if (anti_dependence (pending_mem->element (), t)
2516 && ! sched_insns_conditions_mutex_p (insn, pending->insn ()))
2517 note_mem_dep (t, pending_mem->element (), pending->insn (),
2518 DEP_ANTI);
2520 pending = pending->next ();
2521 pending_mem = pending_mem->next ();
2524 pending = deps->pending_write_insns;
2525 pending_mem = deps->pending_write_mems;
2526 while (pending)
2528 if (output_dependence (pending_mem->element (), t)
2529 && ! sched_insns_conditions_mutex_p (insn, pending->insn ()))
2530 note_mem_dep (t, pending_mem->element (),
2531 pending->insn (),
2532 DEP_OUTPUT);
2534 pending = pending->next ();
2535 pending_mem = pending_mem-> next ();
2538 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
2539 REG_DEP_ANTI, true);
2540 add_dependence_list (insn, deps->pending_jump_insns, 1,
2541 REG_DEP_CONTROL, true);
2543 if (!deps->readonly)
2544 add_insn_mem_dependence (deps, false, insn, dest);
2546 sched_analyze_2 (deps, XEXP (dest, 0), insn);
2549 if (cslr_p && sched_deps_info->finish_lhs)
2550 sched_deps_info->finish_lhs ();
2552 /* Analyze reads. */
2553 if (GET_CODE (x) == SET)
2555 can_start_lhs_rhs_p = cslr_p;
2557 sched_analyze_2 (deps, SET_SRC (x), insn);
2559 can_start_lhs_rhs_p = false;
2563 /* Analyze the uses of memory and registers in rtx X in INSN. */
2564 static void
2565 sched_analyze_2 (struct deps_desc *deps, rtx x, rtx_insn *insn)
2567 int i;
2568 int j;
2569 enum rtx_code code;
2570 const char *fmt;
2571 bool cslr_p = can_start_lhs_rhs_p;
2573 can_start_lhs_rhs_p = false;
2575 gcc_assert (x);
2576 if (x == 0)
2577 return;
2579 if (cslr_p && sched_deps_info->start_rhs)
2580 sched_deps_info->start_rhs (x);
2582 code = GET_CODE (x);
2584 switch (code)
2586 CASE_CONST_ANY:
2587 case SYMBOL_REF:
2588 case CONST:
2589 case LABEL_REF:
2590 /* Ignore constants. */
2591 if (cslr_p && sched_deps_info->finish_rhs)
2592 sched_deps_info->finish_rhs ();
2594 return;
2596 case CC0:
2597 if (!HAVE_cc0)
2598 gcc_unreachable ();
2600 /* User of CC0 depends on immediately preceding insn. */
2601 SCHED_GROUP_P (insn) = 1;
2602 /* Don't move CC0 setter to another block (it can set up the
2603 same flag for previous CC0 users which is safe). */
2604 CANT_MOVE (prev_nonnote_insn (insn)) = 1;
2606 if (cslr_p && sched_deps_info->finish_rhs)
2607 sched_deps_info->finish_rhs ();
2609 return;
2611 case REG:
2613 int regno = REGNO (x);
2614 machine_mode mode = GET_MODE (x);
2616 sched_analyze_reg (deps, regno, mode, USE, insn);
2618 #ifdef STACK_REGS
2619 /* Treat all reads of a stack register as modifying the TOS. */
2620 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
2622 /* Avoid analyzing the same register twice. */
2623 if (regno != FIRST_STACK_REG)
2624 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
2625 sched_analyze_reg (deps, FIRST_STACK_REG, mode, SET, insn);
2627 #endif
2629 if (cslr_p && sched_deps_info->finish_rhs)
2630 sched_deps_info->finish_rhs ();
2632 return;
2635 case MEM:
2637 /* Reading memory. */
2638 rtx_insn_list *u;
2639 rtx_insn_list *pending;
2640 rtx_expr_list *pending_mem;
2641 rtx t = x;
2643 if (sched_deps_info->use_cselib)
2645 machine_mode address_mode = get_address_mode (t);
2647 t = shallow_copy_rtx (t);
2648 cselib_lookup_from_insn (XEXP (t, 0), address_mode, 1,
2649 GET_MODE (t), insn);
2650 XEXP (t, 0)
2651 = cselib_subst_to_values_from_insn (XEXP (t, 0), GET_MODE (t),
2652 insn);
2655 if (!DEBUG_INSN_P (insn))
2657 t = canon_rtx (t);
2658 pending = deps->pending_read_insns;
2659 pending_mem = deps->pending_read_mems;
2660 while (pending)
2662 if (read_dependence (pending_mem->element (), t)
2663 && ! sched_insns_conditions_mutex_p (insn,
2664 pending->insn ()))
2665 note_mem_dep (t, pending_mem->element (),
2666 pending->insn (),
2667 DEP_ANTI);
2669 pending = pending->next ();
2670 pending_mem = pending_mem->next ();
2673 pending = deps->pending_write_insns;
2674 pending_mem = deps->pending_write_mems;
2675 while (pending)
2677 if (true_dependence (pending_mem->element (), VOIDmode, t)
2678 && ! sched_insns_conditions_mutex_p (insn,
2679 pending->insn ()))
2680 note_mem_dep (t, pending_mem->element (),
2681 pending->insn (),
2682 sched_deps_info->generate_spec_deps
2683 ? BEGIN_DATA | DEP_TRUE : DEP_TRUE);
2685 pending = pending->next ();
2686 pending_mem = pending_mem->next ();
2689 for (u = deps->last_pending_memory_flush; u; u = u->next ())
2690 add_dependence (insn, u->insn (), REG_DEP_ANTI);
2692 for (u = deps->pending_jump_insns; u; u = u->next ())
2693 if (deps_may_trap_p (x))
2695 if ((sched_deps_info->generate_spec_deps)
2696 && sel_sched_p () && (spec_info->mask & BEGIN_CONTROL))
2698 ds_t ds = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
2699 MAX_DEP_WEAK);
2701 note_dep (u->insn (), ds);
2703 else
2704 add_dependence (insn, u->insn (), REG_DEP_CONTROL);
2708 /* Always add these dependencies to pending_reads, since
2709 this insn may be followed by a write. */
2710 if (!deps->readonly)
2712 if ((deps->pending_read_list_length
2713 + deps->pending_write_list_length)
2714 >= MAX_PENDING_LIST_LENGTH
2715 && !DEBUG_INSN_P (insn))
2716 flush_pending_lists (deps, insn, true, true);
2717 add_insn_mem_dependence (deps, true, insn, x);
2720 sched_analyze_2 (deps, XEXP (x, 0), insn);
2722 if (cslr_p && sched_deps_info->finish_rhs)
2723 sched_deps_info->finish_rhs ();
2725 return;
2728 /* Force pending stores to memory in case a trap handler needs them. */
2729 case TRAP_IF:
2730 flush_pending_lists (deps, insn, true, false);
2731 break;
2733 case PREFETCH:
2734 if (PREFETCH_SCHEDULE_BARRIER_P (x))
2735 reg_pending_barrier = TRUE_BARRIER;
2736 /* Prefetch insn contains addresses only. So if the prefetch
2737 address has no registers, there will be no dependencies on
2738 the prefetch insn. This is wrong with result code
2739 correctness point of view as such prefetch can be moved below
2740 a jump insn which usually generates MOVE_BARRIER preventing
2741 to move insns containing registers or memories through the
2742 barrier. It is also wrong with generated code performance
2743 point of view as prefetch withouth dependecies will have a
2744 tendency to be issued later instead of earlier. It is hard
2745 to generate accurate dependencies for prefetch insns as
2746 prefetch has only the start address but it is better to have
2747 something than nothing. */
2748 if (!deps->readonly)
2750 rtx x = gen_rtx_MEM (Pmode, XEXP (PATTERN (insn), 0));
2751 if (sched_deps_info->use_cselib)
2752 cselib_lookup_from_insn (x, Pmode, true, VOIDmode, insn);
2753 add_insn_mem_dependence (deps, true, insn, x);
2755 break;
2757 case UNSPEC_VOLATILE:
2758 flush_pending_lists (deps, insn, true, true);
2759 /* FALLTHRU */
2761 case ASM_OPERANDS:
2762 case ASM_INPUT:
2764 /* Traditional and volatile asm instructions must be considered to use
2765 and clobber all hard registers, all pseudo-registers and all of
2766 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
2768 Consider for instance a volatile asm that changes the fpu rounding
2769 mode. An insn should not be moved across this even if it only uses
2770 pseudo-regs because it might give an incorrectly rounded result. */
2771 if ((code != ASM_OPERANDS || MEM_VOLATILE_P (x))
2772 && !DEBUG_INSN_P (insn))
2773 reg_pending_barrier = TRUE_BARRIER;
2775 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
2776 We can not just fall through here since then we would be confused
2777 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
2778 traditional asms unlike their normal usage. */
2780 if (code == ASM_OPERANDS)
2782 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
2783 sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
2785 if (cslr_p && sched_deps_info->finish_rhs)
2786 sched_deps_info->finish_rhs ();
2788 return;
2790 break;
2793 case PRE_DEC:
2794 case POST_DEC:
2795 case PRE_INC:
2796 case POST_INC:
2797 /* These both read and modify the result. We must handle them as writes
2798 to get proper dependencies for following instructions. We must handle
2799 them as reads to get proper dependencies from this to previous
2800 instructions. Thus we need to pass them to both sched_analyze_1
2801 and sched_analyze_2. We must call sched_analyze_2 first in order
2802 to get the proper antecedent for the read. */
2803 sched_analyze_2 (deps, XEXP (x, 0), insn);
2804 sched_analyze_1 (deps, x, insn);
2806 if (cslr_p && sched_deps_info->finish_rhs)
2807 sched_deps_info->finish_rhs ();
2809 return;
2811 case POST_MODIFY:
2812 case PRE_MODIFY:
2813 /* op0 = op0 + op1 */
2814 sched_analyze_2 (deps, XEXP (x, 0), insn);
2815 sched_analyze_2 (deps, XEXP (x, 1), insn);
2816 sched_analyze_1 (deps, x, insn);
2818 if (cslr_p && sched_deps_info->finish_rhs)
2819 sched_deps_info->finish_rhs ();
2821 return;
2823 default:
2824 break;
2827 /* Other cases: walk the insn. */
2828 fmt = GET_RTX_FORMAT (code);
2829 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2831 if (fmt[i] == 'e')
2832 sched_analyze_2 (deps, XEXP (x, i), insn);
2833 else if (fmt[i] == 'E')
2834 for (j = 0; j < XVECLEN (x, i); j++)
2835 sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
2838 if (cslr_p && sched_deps_info->finish_rhs)
2839 sched_deps_info->finish_rhs ();
2842 /* Try to group two fusible insns together to prevent scheduler
2843 from scheduling them apart. */
2845 static void
2846 sched_macro_fuse_insns (rtx_insn *insn)
2848 rtx_insn *prev;
2850 if (any_condjump_p (insn))
2852 unsigned int condreg1, condreg2;
2853 rtx cc_reg_1;
2854 targetm.fixed_condition_code_regs (&condreg1, &condreg2);
2855 cc_reg_1 = gen_rtx_REG (CCmode, condreg1);
2856 prev = prev_nonnote_nondebug_insn (insn);
2857 if (!reg_referenced_p (cc_reg_1, PATTERN (insn))
2858 || !prev
2859 || !modified_in_p (cc_reg_1, prev))
2860 return;
2862 else
2864 rtx insn_set = single_set (insn);
2866 prev = prev_nonnote_nondebug_insn (insn);
2867 if (!prev
2868 || !insn_set
2869 || !single_set (prev))
2870 return;
2874 if (targetm.sched.macro_fusion_pair_p (prev, insn))
2875 SCHED_GROUP_P (insn) = 1;
2879 /* Analyze an INSN with pattern X to find all dependencies. */
2880 static void
2881 sched_analyze_insn (struct deps_desc *deps, rtx x, rtx_insn *insn)
2883 RTX_CODE code = GET_CODE (x);
2884 rtx link;
2885 unsigned i;
2886 reg_set_iterator rsi;
2888 if (! reload_completed)
2890 HARD_REG_SET temp;
2892 extract_insn (insn);
2893 preprocess_constraints (insn);
2894 alternative_mask prefrred = get_preferred_alternatives (insn);
2895 ira_implicitly_set_insn_hard_regs (&temp, prefrred);
2896 AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
2897 IOR_HARD_REG_SET (implicit_reg_pending_clobbers, temp);
2900 can_start_lhs_rhs_p = (NONJUMP_INSN_P (insn)
2901 && code == SET);
2903 /* Group compare and branch insns for macro-fusion. */
2904 if (targetm.sched.macro_fusion_p
2905 && targetm.sched.macro_fusion_p ())
2906 sched_macro_fuse_insns (insn);
2908 if (may_trap_p (x))
2909 /* Avoid moving trapping instructions across function calls that might
2910 not always return. */
2911 add_dependence_list (insn, deps->last_function_call_may_noreturn,
2912 1, REG_DEP_ANTI, true);
2914 /* We must avoid creating a situation in which two successors of the
2915 current block have different unwind info after scheduling. If at any
2916 point the two paths re-join this leads to incorrect unwind info. */
2917 /* ??? There are certain situations involving a forced frame pointer in
2918 which, with extra effort, we could fix up the unwind info at a later
2919 CFG join. However, it seems better to notice these cases earlier
2920 during prologue generation and avoid marking the frame pointer setup
2921 as frame-related at all. */
2922 if (RTX_FRAME_RELATED_P (insn))
2924 /* Make sure prologue insn is scheduled before next jump. */
2925 deps->sched_before_next_jump
2926 = alloc_INSN_LIST (insn, deps->sched_before_next_jump);
2928 /* Make sure epilogue insn is scheduled after preceding jumps. */
2929 add_dependence_list (insn, deps->pending_jump_insns, 1, REG_DEP_ANTI,
2930 true);
2933 if (code == COND_EXEC)
2935 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
2937 /* ??? Should be recording conditions so we reduce the number of
2938 false dependencies. */
2939 x = COND_EXEC_CODE (x);
2940 code = GET_CODE (x);
2942 if (code == SET || code == CLOBBER)
2944 sched_analyze_1 (deps, x, insn);
2946 /* Bare clobber insns are used for letting life analysis, reg-stack
2947 and others know that a value is dead. Depend on the last call
2948 instruction so that reg-stack won't get confused. */
2949 if (code == CLOBBER)
2950 add_dependence_list (insn, deps->last_function_call, 1,
2951 REG_DEP_OUTPUT, true);
2953 else if (code == PARALLEL)
2955 for (i = XVECLEN (x, 0); i--;)
2957 rtx sub = XVECEXP (x, 0, i);
2958 code = GET_CODE (sub);
2960 if (code == COND_EXEC)
2962 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
2963 sub = COND_EXEC_CODE (sub);
2964 code = GET_CODE (sub);
2966 if (code == SET || code == CLOBBER)
2967 sched_analyze_1 (deps, sub, insn);
2968 else
2969 sched_analyze_2 (deps, sub, insn);
2972 else
2973 sched_analyze_2 (deps, x, insn);
2975 /* Mark registers CLOBBERED or used by called function. */
2976 if (CALL_P (insn))
2978 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2980 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
2981 sched_analyze_1 (deps, XEXP (link, 0), insn);
2982 else if (GET_CODE (XEXP (link, 0)) != SET)
2983 sched_analyze_2 (deps, XEXP (link, 0), insn);
2985 /* Don't schedule anything after a tail call, tail call needs
2986 to use at least all call-saved registers. */
2987 if (SIBLING_CALL_P (insn))
2988 reg_pending_barrier = TRUE_BARRIER;
2989 else if (find_reg_note (insn, REG_SETJMP, NULL))
2990 reg_pending_barrier = MOVE_BARRIER;
2993 if (JUMP_P (insn))
2995 rtx_insn *next = next_nonnote_nondebug_insn (insn);
2996 if (next && BARRIER_P (next))
2997 reg_pending_barrier = MOVE_BARRIER;
2998 else
3000 rtx_insn_list *pending;
3001 rtx_expr_list *pending_mem;
3003 if (sched_deps_info->compute_jump_reg_dependencies)
3005 (*sched_deps_info->compute_jump_reg_dependencies)
3006 (insn, reg_pending_control_uses);
3008 /* Make latency of jump equal to 0 by using anti-dependence. */
3009 EXECUTE_IF_SET_IN_REG_SET (reg_pending_control_uses, 0, i, rsi)
3011 struct deps_reg *reg_last = &deps->reg_last[i];
3012 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI,
3013 false);
3014 add_dependence_list (insn, reg_last->implicit_sets,
3015 0, REG_DEP_ANTI, false);
3016 add_dependence_list (insn, reg_last->clobbers, 0,
3017 REG_DEP_ANTI, false);
3021 /* All memory writes and volatile reads must happen before the
3022 jump. Non-volatile reads must happen before the jump iff
3023 the result is needed by the above register used mask. */
3025 pending = deps->pending_write_insns;
3026 pending_mem = deps->pending_write_mems;
3027 while (pending)
3029 if (! sched_insns_conditions_mutex_p (insn, pending->insn ()))
3030 add_dependence (insn, pending->insn (),
3031 REG_DEP_OUTPUT);
3032 pending = pending->next ();
3033 pending_mem = pending_mem->next ();
3036 pending = deps->pending_read_insns;
3037 pending_mem = deps->pending_read_mems;
3038 while (pending)
3040 if (MEM_VOLATILE_P (pending_mem->element ())
3041 && ! sched_insns_conditions_mutex_p (insn, pending->insn ()))
3042 add_dependence (insn, pending->insn (),
3043 REG_DEP_OUTPUT);
3044 pending = pending->next ();
3045 pending_mem = pending_mem->next ();
3048 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
3049 REG_DEP_ANTI, true);
3050 add_dependence_list (insn, deps->pending_jump_insns, 1,
3051 REG_DEP_ANTI, true);
3055 /* If this instruction can throw an exception, then moving it changes
3056 where block boundaries fall. This is mighty confusing elsewhere.
3057 Therefore, prevent such an instruction from being moved. Same for
3058 non-jump instructions that define block boundaries.
3059 ??? Unclear whether this is still necessary in EBB mode. If not,
3060 add_branch_dependences should be adjusted for RGN mode instead. */
3061 if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
3062 || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
3063 reg_pending_barrier = MOVE_BARRIER;
3065 if (sched_pressure != SCHED_PRESSURE_NONE)
3067 setup_insn_reg_uses (deps, insn);
3068 init_insn_reg_pressure_info (insn);
3071 /* Add register dependencies for insn. */
3072 if (DEBUG_INSN_P (insn))
3074 rtx_insn *prev = deps->last_debug_insn;
3075 rtx_insn_list *u;
3077 if (!deps->readonly)
3078 deps->last_debug_insn = insn;
3080 if (prev)
3081 add_dependence (insn, prev, REG_DEP_ANTI);
3083 add_dependence_list (insn, deps->last_function_call, 1,
3084 REG_DEP_ANTI, false);
3086 if (!sel_sched_p ())
3087 for (u = deps->last_pending_memory_flush; u; u = u->next ())
3088 add_dependence (insn, u->insn (), REG_DEP_ANTI);
3090 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
3092 struct deps_reg *reg_last = &deps->reg_last[i];
3093 add_dependence_list (insn, reg_last->sets, 1, REG_DEP_ANTI, false);
3094 /* There's no point in making REG_DEP_CONTROL dependencies for
3095 debug insns. */
3096 add_dependence_list (insn, reg_last->clobbers, 1, REG_DEP_ANTI,
3097 false);
3099 if (!deps->readonly)
3100 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3102 CLEAR_REG_SET (reg_pending_uses);
3104 /* Quite often, a debug insn will refer to stuff in the
3105 previous instruction, but the reason we want this
3106 dependency here is to make sure the scheduler doesn't
3107 gratuitously move a debug insn ahead. This could dirty
3108 DF flags and cause additional analysis that wouldn't have
3109 occurred in compilation without debug insns, and such
3110 additional analysis can modify the generated code. */
3111 prev = PREV_INSN (insn);
3113 if (prev && NONDEBUG_INSN_P (prev))
3114 add_dependence (insn, prev, REG_DEP_ANTI);
3116 else
3118 regset_head set_or_clobbered;
3120 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
3122 struct deps_reg *reg_last = &deps->reg_last[i];
3123 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE, false);
3124 add_dependence_list (insn, reg_last->implicit_sets, 0, REG_DEP_ANTI,
3125 false);
3126 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE,
3127 false);
3129 if (!deps->readonly)
3131 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3132 reg_last->uses_length++;
3136 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3137 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i))
3139 struct deps_reg *reg_last = &deps->reg_last[i];
3140 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE, false);
3141 add_dependence_list (insn, reg_last->implicit_sets, 0,
3142 REG_DEP_ANTI, false);
3143 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE,
3144 false);
3146 if (!deps->readonly)
3148 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3149 reg_last->uses_length++;
3153 if (targetm.sched.exposed_pipeline)
3155 INIT_REG_SET (&set_or_clobbered);
3156 bitmap_ior (&set_or_clobbered, reg_pending_clobbers,
3157 reg_pending_sets);
3158 EXECUTE_IF_SET_IN_REG_SET (&set_or_clobbered, 0, i, rsi)
3160 struct deps_reg *reg_last = &deps->reg_last[i];
3161 rtx list;
3162 for (list = reg_last->uses; list; list = XEXP (list, 1))
3164 rtx other = XEXP (list, 0);
3165 if (INSN_CACHED_COND (other) != const_true_rtx
3166 && refers_to_regno_p (i, INSN_CACHED_COND (other)))
3167 INSN_CACHED_COND (other) = const_true_rtx;
3172 /* If the current insn is conditional, we can't free any
3173 of the lists. */
3174 if (sched_has_condition_p (insn))
3176 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
3178 struct deps_reg *reg_last = &deps->reg_last[i];
3179 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
3180 false);
3181 add_dependence_list (insn, reg_last->implicit_sets, 0,
3182 REG_DEP_ANTI, false);
3183 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3184 false);
3185 add_dependence_list (insn, reg_last->control_uses, 0,
3186 REG_DEP_CONTROL, false);
3188 if (!deps->readonly)
3190 reg_last->clobbers
3191 = alloc_INSN_LIST (insn, reg_last->clobbers);
3192 reg_last->clobbers_length++;
3195 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
3197 struct deps_reg *reg_last = &deps->reg_last[i];
3198 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
3199 false);
3200 add_dependence_list (insn, reg_last->implicit_sets, 0,
3201 REG_DEP_ANTI, false);
3202 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT,
3203 false);
3204 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3205 false);
3206 add_dependence_list (insn, reg_last->control_uses, 0,
3207 REG_DEP_CONTROL, false);
3209 if (!deps->readonly)
3210 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3213 else
3215 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
3217 struct deps_reg *reg_last = &deps->reg_last[i];
3218 if (reg_last->uses_length >= MAX_PENDING_LIST_LENGTH
3219 || reg_last->clobbers_length >= MAX_PENDING_LIST_LENGTH)
3221 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3222 REG_DEP_OUTPUT, false);
3223 add_dependence_list_and_free (deps, insn,
3224 &reg_last->implicit_sets, 0,
3225 REG_DEP_ANTI, false);
3226 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3227 REG_DEP_ANTI, false);
3228 add_dependence_list_and_free (deps, insn,
3229 &reg_last->control_uses, 0,
3230 REG_DEP_ANTI, false);
3231 add_dependence_list_and_free (deps, insn,
3232 &reg_last->clobbers, 0,
3233 REG_DEP_OUTPUT, false);
3235 if (!deps->readonly)
3237 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3238 reg_last->clobbers_length = 0;
3239 reg_last->uses_length = 0;
3242 else
3244 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
3245 false);
3246 add_dependence_list (insn, reg_last->implicit_sets, 0,
3247 REG_DEP_ANTI, false);
3248 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3249 false);
3250 add_dependence_list (insn, reg_last->control_uses, 0,
3251 REG_DEP_CONTROL, false);
3254 if (!deps->readonly)
3256 reg_last->clobbers_length++;
3257 reg_last->clobbers
3258 = alloc_INSN_LIST (insn, reg_last->clobbers);
3261 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
3263 struct deps_reg *reg_last = &deps->reg_last[i];
3265 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3266 REG_DEP_OUTPUT, false);
3267 add_dependence_list_and_free (deps, insn,
3268 &reg_last->implicit_sets,
3269 0, REG_DEP_ANTI, false);
3270 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
3271 REG_DEP_OUTPUT, false);
3272 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3273 REG_DEP_ANTI, false);
3274 add_dependence_list (insn, reg_last->control_uses, 0,
3275 REG_DEP_CONTROL, false);
3277 if (!deps->readonly)
3279 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3280 reg_last->uses_length = 0;
3281 reg_last->clobbers_length = 0;
3285 if (!deps->readonly)
3287 EXECUTE_IF_SET_IN_REG_SET (reg_pending_control_uses, 0, i, rsi)
3289 struct deps_reg *reg_last = &deps->reg_last[i];
3290 reg_last->control_uses
3291 = alloc_INSN_LIST (insn, reg_last->control_uses);
3296 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3297 if (TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
3299 struct deps_reg *reg_last = &deps->reg_last[i];
3300 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI, false);
3301 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI, false);
3302 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI, false);
3303 add_dependence_list (insn, reg_last->control_uses, 0, REG_DEP_ANTI,
3304 false);
3306 if (!deps->readonly)
3307 reg_last->implicit_sets
3308 = alloc_INSN_LIST (insn, reg_last->implicit_sets);
3311 if (!deps->readonly)
3313 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
3314 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
3315 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
3316 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3317 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i)
3318 || TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
3319 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
3321 /* Set up the pending barrier found. */
3322 deps->last_reg_pending_barrier = reg_pending_barrier;
3325 CLEAR_REG_SET (reg_pending_uses);
3326 CLEAR_REG_SET (reg_pending_clobbers);
3327 CLEAR_REG_SET (reg_pending_sets);
3328 CLEAR_REG_SET (reg_pending_control_uses);
3329 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
3330 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
3332 /* Add dependencies if a scheduling barrier was found. */
3333 if (reg_pending_barrier)
3335 /* In the case of barrier the most added dependencies are not
3336 real, so we use anti-dependence here. */
3337 if (sched_has_condition_p (insn))
3339 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3341 struct deps_reg *reg_last = &deps->reg_last[i];
3342 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3343 true);
3344 add_dependence_list (insn, reg_last->sets, 0,
3345 reg_pending_barrier == TRUE_BARRIER
3346 ? REG_DEP_TRUE : REG_DEP_ANTI, true);
3347 add_dependence_list (insn, reg_last->implicit_sets, 0,
3348 REG_DEP_ANTI, true);
3349 add_dependence_list (insn, reg_last->clobbers, 0,
3350 reg_pending_barrier == TRUE_BARRIER
3351 ? REG_DEP_TRUE : REG_DEP_ANTI, true);
3354 else
3356 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3358 struct deps_reg *reg_last = &deps->reg_last[i];
3359 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3360 REG_DEP_ANTI, true);
3361 add_dependence_list_and_free (deps, insn,
3362 &reg_last->control_uses, 0,
3363 REG_DEP_CONTROL, true);
3364 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3365 reg_pending_barrier == TRUE_BARRIER
3366 ? REG_DEP_TRUE : REG_DEP_ANTI,
3367 true);
3368 add_dependence_list_and_free (deps, insn,
3369 &reg_last->implicit_sets, 0,
3370 REG_DEP_ANTI, true);
3371 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
3372 reg_pending_barrier == TRUE_BARRIER
3373 ? REG_DEP_TRUE : REG_DEP_ANTI,
3374 true);
3376 if (!deps->readonly)
3378 reg_last->uses_length = 0;
3379 reg_last->clobbers_length = 0;
3384 if (!deps->readonly)
3385 for (i = 0; i < (unsigned)deps->max_reg; i++)
3387 struct deps_reg *reg_last = &deps->reg_last[i];
3388 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3389 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
3392 /* Don't flush pending lists on speculative checks for
3393 selective scheduling. */
3394 if (!sel_sched_p () || !sel_insn_is_speculation_check (insn))
3395 flush_pending_lists (deps, insn, true, true);
3397 reg_pending_barrier = NOT_A_BARRIER;
3400 /* If a post-call group is still open, see if it should remain so.
3401 This insn must be a simple move of a hard reg to a pseudo or
3402 vice-versa.
3404 We must avoid moving these insns for correctness on targets
3405 with small register classes, and for special registers like
3406 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
3407 hard regs for all targets. */
3409 if (deps->in_post_call_group_p)
3411 rtx tmp, set = single_set (insn);
3412 int src_regno, dest_regno;
3414 if (set == NULL)
3416 if (DEBUG_INSN_P (insn))
3417 /* We don't want to mark debug insns as part of the same
3418 sched group. We know they really aren't, but if we use
3419 debug insns to tell that a call group is over, we'll
3420 get different code if debug insns are not there and
3421 instructions that follow seem like they should be part
3422 of the call group.
3424 Also, if we did, chain_to_prev_insn would move the
3425 deps of the debug insn to the call insn, modifying
3426 non-debug post-dependency counts of the debug insn
3427 dependencies and otherwise messing with the scheduling
3428 order.
3430 Instead, let such debug insns be scheduled freely, but
3431 keep the call group open in case there are insns that
3432 should be part of it afterwards. Since we grant debug
3433 insns higher priority than even sched group insns, it
3434 will all turn out all right. */
3435 goto debug_dont_end_call_group;
3436 else
3437 goto end_call_group;
3440 tmp = SET_DEST (set);
3441 if (GET_CODE (tmp) == SUBREG)
3442 tmp = SUBREG_REG (tmp);
3443 if (REG_P (tmp))
3444 dest_regno = REGNO (tmp);
3445 else
3446 goto end_call_group;
3448 tmp = SET_SRC (set);
3449 if (GET_CODE (tmp) == SUBREG)
3450 tmp = SUBREG_REG (tmp);
3451 if ((GET_CODE (tmp) == PLUS
3452 || GET_CODE (tmp) == MINUS)
3453 && REG_P (XEXP (tmp, 0))
3454 && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
3455 && dest_regno == STACK_POINTER_REGNUM)
3456 src_regno = STACK_POINTER_REGNUM;
3457 else if (REG_P (tmp))
3458 src_regno = REGNO (tmp);
3459 else
3460 goto end_call_group;
3462 if (src_regno < FIRST_PSEUDO_REGISTER
3463 || dest_regno < FIRST_PSEUDO_REGISTER)
3465 if (!deps->readonly
3466 && deps->in_post_call_group_p == post_call_initial)
3467 deps->in_post_call_group_p = post_call;
3469 if (!sel_sched_p () || sched_emulate_haifa_p)
3471 SCHED_GROUP_P (insn) = 1;
3472 CANT_MOVE (insn) = 1;
3475 else
3477 end_call_group:
3478 if (!deps->readonly)
3479 deps->in_post_call_group_p = not_post_call;
3483 debug_dont_end_call_group:
3484 if ((current_sched_info->flags & DO_SPECULATION)
3485 && !sched_insn_is_legitimate_for_speculation_p (insn, 0))
3486 /* INSN has an internal dependency (e.g. r14 = [r14]) and thus cannot
3487 be speculated. */
3489 if (sel_sched_p ())
3490 sel_mark_hard_insn (insn);
3491 else
3493 sd_iterator_def sd_it;
3494 dep_t dep;
3496 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3497 sd_iterator_cond (&sd_it, &dep);)
3498 change_spec_dep_to_hard (sd_it);
3502 /* We do not yet have code to adjust REG_ARGS_SIZE, therefore we must
3503 honor their original ordering. */
3504 if (find_reg_note (insn, REG_ARGS_SIZE, NULL))
3506 if (deps->last_args_size)
3507 add_dependence (insn, deps->last_args_size, REG_DEP_OUTPUT);
3508 deps->last_args_size = insn;
3512 /* Return TRUE if INSN might not always return normally (e.g. call exit,
3513 longjmp, loop forever, ...). */
3514 /* FIXME: Why can't this function just use flags_from_decl_or_type and
3515 test for ECF_NORETURN? */
3516 static bool
3517 call_may_noreturn_p (rtx_insn *insn)
3519 rtx call;
3521 /* const or pure calls that aren't looping will always return. */
3522 if (RTL_CONST_OR_PURE_CALL_P (insn)
3523 && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn))
3524 return false;
3526 call = get_call_rtx_from (insn);
3527 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
3529 rtx symbol = XEXP (XEXP (call, 0), 0);
3530 if (SYMBOL_REF_DECL (symbol)
3531 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
3533 if (DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
3534 == BUILT_IN_NORMAL)
3535 switch (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol)))
3537 case BUILT_IN_BCMP:
3538 case BUILT_IN_BCOPY:
3539 case BUILT_IN_BZERO:
3540 case BUILT_IN_INDEX:
3541 case BUILT_IN_MEMCHR:
3542 case BUILT_IN_MEMCMP:
3543 case BUILT_IN_MEMCPY:
3544 case BUILT_IN_MEMMOVE:
3545 case BUILT_IN_MEMPCPY:
3546 case BUILT_IN_MEMSET:
3547 case BUILT_IN_RINDEX:
3548 case BUILT_IN_STPCPY:
3549 case BUILT_IN_STPNCPY:
3550 case BUILT_IN_STRCAT:
3551 case BUILT_IN_STRCHR:
3552 case BUILT_IN_STRCMP:
3553 case BUILT_IN_STRCPY:
3554 case BUILT_IN_STRCSPN:
3555 case BUILT_IN_STRLEN:
3556 case BUILT_IN_STRNCAT:
3557 case BUILT_IN_STRNCMP:
3558 case BUILT_IN_STRNCPY:
3559 case BUILT_IN_STRPBRK:
3560 case BUILT_IN_STRRCHR:
3561 case BUILT_IN_STRSPN:
3562 case BUILT_IN_STRSTR:
3563 /* Assume certain string/memory builtins always return. */
3564 return false;
3565 default:
3566 break;
3571 /* For all other calls assume that they might not always return. */
3572 return true;
3575 /* Return true if INSN should be made dependent on the previous instruction
3576 group, and if all INSN's dependencies should be moved to the first
3577 instruction of that group. */
3579 static bool
3580 chain_to_prev_insn_p (rtx_insn *insn)
3582 /* INSN forms a group with the previous instruction. */
3583 if (SCHED_GROUP_P (insn))
3584 return true;
3586 /* If the previous instruction clobbers a register R and this one sets
3587 part of R, the clobber was added specifically to help us track the
3588 liveness of R. There's no point scheduling the clobber and leaving
3589 INSN behind, especially if we move the clobber to another block. */
3590 rtx_insn *prev = prev_nonnote_nondebug_insn (insn);
3591 if (prev
3592 && INSN_P (prev)
3593 && BLOCK_FOR_INSN (prev) == BLOCK_FOR_INSN (insn)
3594 && GET_CODE (PATTERN (prev)) == CLOBBER)
3596 rtx x = XEXP (PATTERN (prev), 0);
3597 if (set_of (x, insn))
3598 return true;
3601 return false;
3604 /* Analyze INSN with DEPS as a context. */
3605 void
3606 deps_analyze_insn (struct deps_desc *deps, rtx_insn *insn)
3608 if (sched_deps_info->start_insn)
3609 sched_deps_info->start_insn (insn);
3611 /* Record the condition for this insn. */
3612 if (NONDEBUG_INSN_P (insn))
3614 rtx t;
3615 sched_get_condition_with_rev (insn, NULL);
3616 t = INSN_CACHED_COND (insn);
3617 INSN_COND_DEPS (insn) = NULL;
3618 if (reload_completed
3619 && (current_sched_info->flags & DO_PREDICATION)
3620 && COMPARISON_P (t)
3621 && REG_P (XEXP (t, 0))
3622 && CONSTANT_P (XEXP (t, 1)))
3624 unsigned int regno;
3625 int nregs;
3626 rtx_insn_list *cond_deps = NULL;
3627 t = XEXP (t, 0);
3628 regno = REGNO (t);
3629 nregs = REG_NREGS (t);
3630 while (nregs-- > 0)
3632 struct deps_reg *reg_last = &deps->reg_last[regno + nregs];
3633 cond_deps = concat_INSN_LIST (reg_last->sets, cond_deps);
3634 cond_deps = concat_INSN_LIST (reg_last->clobbers, cond_deps);
3635 cond_deps = concat_INSN_LIST (reg_last->implicit_sets, cond_deps);
3637 INSN_COND_DEPS (insn) = cond_deps;
3641 if (JUMP_P (insn))
3643 /* Make each JUMP_INSN (but not a speculative check)
3644 a scheduling barrier for memory references. */
3645 if (!deps->readonly
3646 && !(sel_sched_p ()
3647 && sel_insn_is_speculation_check (insn)))
3649 /* Keep the list a reasonable size. */
3650 if (deps->pending_flush_length++ >= MAX_PENDING_LIST_LENGTH)
3651 flush_pending_lists (deps, insn, true, true);
3652 else
3653 deps->pending_jump_insns
3654 = alloc_INSN_LIST (insn, deps->pending_jump_insns);
3657 /* For each insn which shouldn't cross a jump, add a dependence. */
3658 add_dependence_list_and_free (deps, insn,
3659 &deps->sched_before_next_jump, 1,
3660 REG_DEP_ANTI, true);
3662 sched_analyze_insn (deps, PATTERN (insn), insn);
3664 else if (NONJUMP_INSN_P (insn) || DEBUG_INSN_P (insn))
3666 sched_analyze_insn (deps, PATTERN (insn), insn);
3668 else if (CALL_P (insn))
3670 int i;
3672 CANT_MOVE (insn) = 1;
3674 if (find_reg_note (insn, REG_SETJMP, NULL))
3676 /* This is setjmp. Assume that all registers, not just
3677 hard registers, may be clobbered by this call. */
3678 reg_pending_barrier = MOVE_BARRIER;
3680 else
3682 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3683 /* A call may read and modify global register variables. */
3684 if (global_regs[i])
3686 SET_REGNO_REG_SET (reg_pending_sets, i);
3687 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3689 /* Other call-clobbered hard regs may be clobbered.
3690 Since we only have a choice between 'might be clobbered'
3691 and 'definitely not clobbered', we must include all
3692 partly call-clobbered registers here. */
3693 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
3694 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
3695 SET_REGNO_REG_SET (reg_pending_clobbers, i);
3696 /* We don't know what set of fixed registers might be used
3697 by the function, but it is certain that the stack pointer
3698 is among them, but be conservative. */
3699 else if (fixed_regs[i])
3700 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3701 /* The frame pointer is normally not used by the function
3702 itself, but by the debugger. */
3703 /* ??? MIPS o32 is an exception. It uses the frame pointer
3704 in the macro expansion of jal but does not represent this
3705 fact in the call_insn rtl. */
3706 else if (i == FRAME_POINTER_REGNUM
3707 || (i == HARD_FRAME_POINTER_REGNUM
3708 && (! reload_completed || frame_pointer_needed)))
3709 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3712 /* For each insn which shouldn't cross a call, add a dependence
3713 between that insn and this call insn. */
3714 add_dependence_list_and_free (deps, insn,
3715 &deps->sched_before_next_call, 1,
3716 REG_DEP_ANTI, true);
3718 sched_analyze_insn (deps, PATTERN (insn), insn);
3720 /* If CALL would be in a sched group, then this will violate
3721 convention that sched group insns have dependencies only on the
3722 previous instruction.
3724 Of course one can say: "Hey! What about head of the sched group?"
3725 And I will answer: "Basic principles (one dep per insn) are always
3726 the same." */
3727 gcc_assert (!SCHED_GROUP_P (insn));
3729 /* In the absence of interprocedural alias analysis, we must flush
3730 all pending reads and writes, and start new dependencies starting
3731 from here. But only flush writes for constant calls (which may
3732 be passed a pointer to something we haven't written yet). */
3733 flush_pending_lists (deps, insn, true, ! RTL_CONST_OR_PURE_CALL_P (insn));
3735 if (!deps->readonly)
3737 /* Remember the last function call for limiting lifetimes. */
3738 free_INSN_LIST_list (&deps->last_function_call);
3739 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
3741 if (call_may_noreturn_p (insn))
3743 /* Remember the last function call that might not always return
3744 normally for limiting moves of trapping insns. */
3745 free_INSN_LIST_list (&deps->last_function_call_may_noreturn);
3746 deps->last_function_call_may_noreturn
3747 = alloc_INSN_LIST (insn, NULL_RTX);
3750 /* Before reload, begin a post-call group, so as to keep the
3751 lifetimes of hard registers correct. */
3752 if (! reload_completed)
3753 deps->in_post_call_group_p = post_call;
3757 if (sched_deps_info->use_cselib)
3758 cselib_process_insn (insn);
3760 if (sched_deps_info->finish_insn)
3761 sched_deps_info->finish_insn ();
3763 /* Fixup the dependencies in the sched group. */
3764 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
3765 && chain_to_prev_insn_p (insn)
3766 && !sel_sched_p ())
3767 chain_to_prev_insn (insn);
3770 /* Initialize DEPS for the new block beginning with HEAD. */
3771 void
3772 deps_start_bb (struct deps_desc *deps, rtx_insn *head)
3774 gcc_assert (!deps->readonly);
3776 /* Before reload, if the previous block ended in a call, show that
3777 we are inside a post-call group, so as to keep the lifetimes of
3778 hard registers correct. */
3779 if (! reload_completed && !LABEL_P (head))
3781 rtx_insn *insn = prev_nonnote_nondebug_insn (head);
3783 if (insn && CALL_P (insn))
3784 deps->in_post_call_group_p = post_call_initial;
3788 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
3789 dependencies for each insn. */
3790 void
3791 sched_analyze (struct deps_desc *deps, rtx_insn *head, rtx_insn *tail)
3793 rtx_insn *insn;
3795 if (sched_deps_info->use_cselib)
3796 cselib_init (CSELIB_RECORD_MEMORY);
3798 deps_start_bb (deps, head);
3800 for (insn = head;; insn = NEXT_INSN (insn))
3803 if (INSN_P (insn))
3805 /* And initialize deps_lists. */
3806 sd_init_insn (insn);
3807 /* Clean up SCHED_GROUP_P which may be set by last
3808 scheduler pass. */
3809 if (SCHED_GROUP_P (insn))
3810 SCHED_GROUP_P (insn) = 0;
3813 deps_analyze_insn (deps, insn);
3815 if (insn == tail)
3817 if (sched_deps_info->use_cselib)
3818 cselib_finish ();
3819 return;
3822 gcc_unreachable ();
3825 /* Helper for sched_free_deps ().
3826 Delete INSN's (RESOLVED_P) backward dependencies. */
3827 static void
3828 delete_dep_nodes_in_back_deps (rtx_insn *insn, bool resolved_p)
3830 sd_iterator_def sd_it;
3831 dep_t dep;
3832 sd_list_types_def types;
3834 if (resolved_p)
3835 types = SD_LIST_RES_BACK;
3836 else
3837 types = SD_LIST_BACK;
3839 for (sd_it = sd_iterator_start (insn, types);
3840 sd_iterator_cond (&sd_it, &dep);)
3842 dep_link_t link = *sd_it.linkp;
3843 dep_node_t node = DEP_LINK_NODE (link);
3844 deps_list_t back_list;
3845 deps_list_t forw_list;
3847 get_back_and_forw_lists (dep, resolved_p, &back_list, &forw_list);
3848 remove_from_deps_list (link, back_list);
3849 delete_dep_node (node);
3853 /* Delete (RESOLVED_P) dependencies between HEAD and TAIL together with
3854 deps_lists. */
3855 void
3856 sched_free_deps (rtx_insn *head, rtx_insn *tail, bool resolved_p)
3858 rtx_insn *insn;
3859 rtx_insn *next_tail = NEXT_INSN (tail);
3861 /* We make two passes since some insns may be scheduled before their
3862 dependencies are resolved. */
3863 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3864 if (INSN_P (insn) && INSN_LUID (insn) > 0)
3866 /* Clear forward deps and leave the dep_nodes to the
3867 corresponding back_deps list. */
3868 if (resolved_p)
3869 clear_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
3870 else
3871 clear_deps_list (INSN_FORW_DEPS (insn));
3873 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3874 if (INSN_P (insn) && INSN_LUID (insn) > 0)
3876 /* Clear resolved back deps together with its dep_nodes. */
3877 delete_dep_nodes_in_back_deps (insn, resolved_p);
3879 sd_finish_insn (insn);
3883 /* Initialize variables for region data dependence analysis.
3884 When LAZY_REG_LAST is true, do not allocate reg_last array
3885 of struct deps_desc immediately. */
3887 void
3888 init_deps (struct deps_desc *deps, bool lazy_reg_last)
3890 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
3892 deps->max_reg = max_reg;
3893 if (lazy_reg_last)
3894 deps->reg_last = NULL;
3895 else
3896 deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
3897 INIT_REG_SET (&deps->reg_last_in_use);
3899 deps->pending_read_insns = 0;
3900 deps->pending_read_mems = 0;
3901 deps->pending_write_insns = 0;
3902 deps->pending_write_mems = 0;
3903 deps->pending_jump_insns = 0;
3904 deps->pending_read_list_length = 0;
3905 deps->pending_write_list_length = 0;
3906 deps->pending_flush_length = 0;
3907 deps->last_pending_memory_flush = 0;
3908 deps->last_function_call = 0;
3909 deps->last_function_call_may_noreturn = 0;
3910 deps->sched_before_next_call = 0;
3911 deps->sched_before_next_jump = 0;
3912 deps->in_post_call_group_p = not_post_call;
3913 deps->last_debug_insn = 0;
3914 deps->last_args_size = 0;
3915 deps->last_reg_pending_barrier = NOT_A_BARRIER;
3916 deps->readonly = 0;
3919 /* Init only reg_last field of DEPS, which was not allocated before as
3920 we inited DEPS lazily. */
3921 void
3922 init_deps_reg_last (struct deps_desc *deps)
3924 gcc_assert (deps && deps->max_reg > 0);
3925 gcc_assert (deps->reg_last == NULL);
3927 deps->reg_last = XCNEWVEC (struct deps_reg, deps->max_reg);
3931 /* Free insn lists found in DEPS. */
3933 void
3934 free_deps (struct deps_desc *deps)
3936 unsigned i;
3937 reg_set_iterator rsi;
3939 /* We set max_reg to 0 when this context was already freed. */
3940 if (deps->max_reg == 0)
3942 gcc_assert (deps->reg_last == NULL);
3943 return;
3945 deps->max_reg = 0;
3947 free_INSN_LIST_list (&deps->pending_read_insns);
3948 free_EXPR_LIST_list (&deps->pending_read_mems);
3949 free_INSN_LIST_list (&deps->pending_write_insns);
3950 free_EXPR_LIST_list (&deps->pending_write_mems);
3951 free_INSN_LIST_list (&deps->last_pending_memory_flush);
3953 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
3954 times. For a testcase with 42000 regs and 8000 small basic blocks,
3955 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
3956 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3958 struct deps_reg *reg_last = &deps->reg_last[i];
3959 if (reg_last->uses)
3960 free_INSN_LIST_list (&reg_last->uses);
3961 if (reg_last->sets)
3962 free_INSN_LIST_list (&reg_last->sets);
3963 if (reg_last->implicit_sets)
3964 free_INSN_LIST_list (&reg_last->implicit_sets);
3965 if (reg_last->control_uses)
3966 free_INSN_LIST_list (&reg_last->control_uses);
3967 if (reg_last->clobbers)
3968 free_INSN_LIST_list (&reg_last->clobbers);
3970 CLEAR_REG_SET (&deps->reg_last_in_use);
3972 /* As we initialize reg_last lazily, it is possible that we didn't allocate
3973 it at all. */
3974 free (deps->reg_last);
3975 deps->reg_last = NULL;
3977 deps = NULL;
3980 /* Remove INSN from dependence contexts DEPS. */
3981 void
3982 remove_from_deps (struct deps_desc *deps, rtx_insn *insn)
3984 int removed;
3985 unsigned i;
3986 reg_set_iterator rsi;
3988 removed = remove_from_both_dependence_lists (insn, &deps->pending_read_insns,
3989 &deps->pending_read_mems);
3990 if (!DEBUG_INSN_P (insn))
3991 deps->pending_read_list_length -= removed;
3992 removed = remove_from_both_dependence_lists (insn, &deps->pending_write_insns,
3993 &deps->pending_write_mems);
3994 deps->pending_write_list_length -= removed;
3996 removed = remove_from_dependence_list (insn, &deps->pending_jump_insns);
3997 deps->pending_flush_length -= removed;
3998 removed = remove_from_dependence_list (insn, &deps->last_pending_memory_flush);
3999 deps->pending_flush_length -= removed;
4001 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
4003 struct deps_reg *reg_last = &deps->reg_last[i];
4004 if (reg_last->uses)
4005 remove_from_dependence_list (insn, &reg_last->uses);
4006 if (reg_last->sets)
4007 remove_from_dependence_list (insn, &reg_last->sets);
4008 if (reg_last->implicit_sets)
4009 remove_from_dependence_list (insn, &reg_last->implicit_sets);
4010 if (reg_last->clobbers)
4011 remove_from_dependence_list (insn, &reg_last->clobbers);
4012 if (!reg_last->uses && !reg_last->sets && !reg_last->implicit_sets
4013 && !reg_last->clobbers)
4014 CLEAR_REGNO_REG_SET (&deps->reg_last_in_use, i);
4017 if (CALL_P (insn))
4019 remove_from_dependence_list (insn, &deps->last_function_call);
4020 remove_from_dependence_list (insn,
4021 &deps->last_function_call_may_noreturn);
4023 remove_from_dependence_list (insn, &deps->sched_before_next_call);
4026 /* Init deps data vector. */
4027 static void
4028 init_deps_data_vector (void)
4030 int reserve = (sched_max_luid + 1 - h_d_i_d.length ());
4031 if (reserve > 0 && ! h_d_i_d.space (reserve))
4032 h_d_i_d.safe_grow_cleared (3 * sched_max_luid / 2);
4035 /* If it is profitable to use them, initialize or extend (depending on
4036 GLOBAL_P) dependency data. */
4037 void
4038 sched_deps_init (bool global_p)
4040 /* Average number of insns in the basic block.
4041 '+ 1' is used to make it nonzero. */
4042 int insns_in_block = sched_max_luid / n_basic_blocks_for_fn (cfun) + 1;
4044 init_deps_data_vector ();
4046 /* We use another caching mechanism for selective scheduling, so
4047 we don't use this one. */
4048 if (!sel_sched_p () && global_p && insns_in_block > 100 * 5)
4050 /* ?!? We could save some memory by computing a per-region luid mapping
4051 which could reduce both the number of vectors in the cache and the
4052 size of each vector. Instead we just avoid the cache entirely unless
4053 the average number of instructions in a basic block is very high. See
4054 the comment before the declaration of true_dependency_cache for
4055 what we consider "very high". */
4056 cache_size = 0;
4057 extend_dependency_caches (sched_max_luid, true);
4060 if (global_p)
4062 dl_pool = new object_allocator<_deps_list> ("deps_list");
4063 /* Allocate lists for one block at a time. */
4064 dn_pool = new object_allocator<_dep_node> ("dep_node");
4065 /* Allocate nodes for one block at a time. */
4070 /* Create or extend (depending on CREATE_P) dependency caches to
4071 size N. */
4072 void
4073 extend_dependency_caches (int n, bool create_p)
4075 if (create_p || true_dependency_cache)
4077 int i, luid = cache_size + n;
4079 true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
4080 luid);
4081 output_dependency_cache = XRESIZEVEC (bitmap_head,
4082 output_dependency_cache, luid);
4083 anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
4084 luid);
4085 control_dependency_cache = XRESIZEVEC (bitmap_head, control_dependency_cache,
4086 luid);
4088 if (current_sched_info->flags & DO_SPECULATION)
4089 spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
4090 luid);
4092 for (i = cache_size; i < luid; i++)
4094 bitmap_initialize (&true_dependency_cache[i], 0);
4095 bitmap_initialize (&output_dependency_cache[i], 0);
4096 bitmap_initialize (&anti_dependency_cache[i], 0);
4097 bitmap_initialize (&control_dependency_cache[i], 0);
4099 if (current_sched_info->flags & DO_SPECULATION)
4100 bitmap_initialize (&spec_dependency_cache[i], 0);
4102 cache_size = luid;
4106 /* Finalize dependency information for the whole function. */
4107 void
4108 sched_deps_finish (void)
4110 gcc_assert (deps_pools_are_empty_p ());
4111 dn_pool->release_if_empty ();
4112 dn_pool = NULL;
4113 dl_pool->release_if_empty ();
4114 dl_pool = NULL;
4116 h_d_i_d.release ();
4117 cache_size = 0;
4119 if (true_dependency_cache)
4121 int i;
4123 for (i = 0; i < cache_size; i++)
4125 bitmap_clear (&true_dependency_cache[i]);
4126 bitmap_clear (&output_dependency_cache[i]);
4127 bitmap_clear (&anti_dependency_cache[i]);
4128 bitmap_clear (&control_dependency_cache[i]);
4130 if (sched_deps_info->generate_spec_deps)
4131 bitmap_clear (&spec_dependency_cache[i]);
4133 free (true_dependency_cache);
4134 true_dependency_cache = NULL;
4135 free (output_dependency_cache);
4136 output_dependency_cache = NULL;
4137 free (anti_dependency_cache);
4138 anti_dependency_cache = NULL;
4139 free (control_dependency_cache);
4140 control_dependency_cache = NULL;
4142 if (sched_deps_info->generate_spec_deps)
4144 free (spec_dependency_cache);
4145 spec_dependency_cache = NULL;
4151 /* Initialize some global variables needed by the dependency analysis
4152 code. */
4154 void
4155 init_deps_global (void)
4157 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
4158 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
4159 reg_pending_sets = ALLOC_REG_SET (&reg_obstack);
4160 reg_pending_clobbers = ALLOC_REG_SET (&reg_obstack);
4161 reg_pending_uses = ALLOC_REG_SET (&reg_obstack);
4162 reg_pending_control_uses = ALLOC_REG_SET (&reg_obstack);
4163 reg_pending_barrier = NOT_A_BARRIER;
4165 if (!sel_sched_p () || sched_emulate_haifa_p)
4167 sched_deps_info->start_insn = haifa_start_insn;
4168 sched_deps_info->finish_insn = haifa_finish_insn;
4170 sched_deps_info->note_reg_set = haifa_note_reg_set;
4171 sched_deps_info->note_reg_clobber = haifa_note_reg_clobber;
4172 sched_deps_info->note_reg_use = haifa_note_reg_use;
4174 sched_deps_info->note_mem_dep = haifa_note_mem_dep;
4175 sched_deps_info->note_dep = haifa_note_dep;
4179 /* Free everything used by the dependency analysis code. */
4181 void
4182 finish_deps_global (void)
4184 FREE_REG_SET (reg_pending_sets);
4185 FREE_REG_SET (reg_pending_clobbers);
4186 FREE_REG_SET (reg_pending_uses);
4187 FREE_REG_SET (reg_pending_control_uses);
4190 /* Estimate the weakness of dependence between MEM1 and MEM2. */
4191 dw_t
4192 estimate_dep_weak (rtx mem1, rtx mem2)
4194 rtx r1, r2;
4196 if (mem1 == mem2)
4197 /* MEMs are the same - don't speculate. */
4198 return MIN_DEP_WEAK;
4200 r1 = XEXP (mem1, 0);
4201 r2 = XEXP (mem2, 0);
4203 if (r1 == r2
4204 || (REG_P (r1) && REG_P (r2)
4205 && REGNO (r1) == REGNO (r2)))
4206 /* Again, MEMs are the same. */
4207 return MIN_DEP_WEAK;
4208 else if ((REG_P (r1) && !REG_P (r2))
4209 || (!REG_P (r1) && REG_P (r2)))
4210 /* Different addressing modes - reason to be more speculative,
4211 than usual. */
4212 return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
4213 else
4214 /* We can't say anything about the dependence. */
4215 return UNCERTAIN_DEP_WEAK;
4218 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
4219 This function can handle same INSN and ELEM (INSN == ELEM).
4220 It is a convenience wrapper. */
4221 static void
4222 add_dependence_1 (rtx_insn *insn, rtx_insn *elem, enum reg_note dep_type)
4224 ds_t ds;
4225 bool internal;
4227 if (dep_type == REG_DEP_TRUE)
4228 ds = DEP_TRUE;
4229 else if (dep_type == REG_DEP_OUTPUT)
4230 ds = DEP_OUTPUT;
4231 else if (dep_type == REG_DEP_CONTROL)
4232 ds = DEP_CONTROL;
4233 else
4235 gcc_assert (dep_type == REG_DEP_ANTI);
4236 ds = DEP_ANTI;
4239 /* When add_dependence is called from inside sched-deps.c, we expect
4240 cur_insn to be non-null. */
4241 internal = cur_insn != NULL;
4242 if (internal)
4243 gcc_assert (insn == cur_insn);
4244 else
4245 cur_insn = insn;
4247 note_dep (elem, ds);
4248 if (!internal)
4249 cur_insn = NULL;
4252 /* Return weakness of speculative type TYPE in the dep_status DS,
4253 without checking to prevent ICEs on malformed input. */
4254 static dw_t
4255 get_dep_weak_1 (ds_t ds, ds_t type)
4257 ds = ds & type;
4259 switch (type)
4261 case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
4262 case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
4263 case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
4264 case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
4265 default: gcc_unreachable ();
4268 return (dw_t) ds;
4271 /* Return weakness of speculative type TYPE in the dep_status DS. */
4272 dw_t
4273 get_dep_weak (ds_t ds, ds_t type)
4275 dw_t dw = get_dep_weak_1 (ds, type);
4277 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
4278 return dw;
4281 /* Return the dep_status, which has the same parameters as DS, except for
4282 speculative type TYPE, that will have weakness DW. */
4283 ds_t
4284 set_dep_weak (ds_t ds, ds_t type, dw_t dw)
4286 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
4288 ds &= ~type;
4289 switch (type)
4291 case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
4292 case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
4293 case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
4294 case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
4295 default: gcc_unreachable ();
4297 return ds;
4300 /* Return the join of two dep_statuses DS1 and DS2.
4301 If MAX_P is true then choose the greater probability,
4302 otherwise multiply probabilities.
4303 This function assumes that both DS1 and DS2 contain speculative bits. */
4304 static ds_t
4305 ds_merge_1 (ds_t ds1, ds_t ds2, bool max_p)
4307 ds_t ds, t;
4309 gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
4311 ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
4313 t = FIRST_SPEC_TYPE;
4316 if ((ds1 & t) && !(ds2 & t))
4317 ds |= ds1 & t;
4318 else if (!(ds1 & t) && (ds2 & t))
4319 ds |= ds2 & t;
4320 else if ((ds1 & t) && (ds2 & t))
4322 dw_t dw1 = get_dep_weak (ds1, t);
4323 dw_t dw2 = get_dep_weak (ds2, t);
4324 ds_t dw;
4326 if (!max_p)
4328 dw = ((ds_t) dw1) * ((ds_t) dw2);
4329 dw /= MAX_DEP_WEAK;
4330 if (dw < MIN_DEP_WEAK)
4331 dw = MIN_DEP_WEAK;
4333 else
4335 if (dw1 >= dw2)
4336 dw = dw1;
4337 else
4338 dw = dw2;
4341 ds = set_dep_weak (ds, t, (dw_t) dw);
4344 if (t == LAST_SPEC_TYPE)
4345 break;
4346 t <<= SPEC_TYPE_SHIFT;
4348 while (1);
4350 return ds;
4353 /* Return the join of two dep_statuses DS1 and DS2.
4354 This function assumes that both DS1 and DS2 contain speculative bits. */
4355 ds_t
4356 ds_merge (ds_t ds1, ds_t ds2)
4358 return ds_merge_1 (ds1, ds2, false);
4361 /* Return the join of two dep_statuses DS1 and DS2. */
4362 ds_t
4363 ds_full_merge (ds_t ds, ds_t ds2, rtx mem1, rtx mem2)
4365 ds_t new_status = ds | ds2;
4367 if (new_status & SPECULATIVE)
4369 if ((ds && !(ds & SPECULATIVE))
4370 || (ds2 && !(ds2 & SPECULATIVE)))
4371 /* Then this dep can't be speculative. */
4372 new_status &= ~SPECULATIVE;
4373 else
4375 /* Both are speculative. Merging probabilities. */
4376 if (mem1)
4378 dw_t dw;
4380 dw = estimate_dep_weak (mem1, mem2);
4381 ds = set_dep_weak (ds, BEGIN_DATA, dw);
4384 if (!ds)
4385 new_status = ds2;
4386 else if (!ds2)
4387 new_status = ds;
4388 else
4389 new_status = ds_merge (ds2, ds);
4393 return new_status;
4396 /* Return the join of DS1 and DS2. Use maximum instead of multiplying
4397 probabilities. */
4398 ds_t
4399 ds_max_merge (ds_t ds1, ds_t ds2)
4401 if (ds1 == 0 && ds2 == 0)
4402 return 0;
4404 if (ds1 == 0 && ds2 != 0)
4405 return ds2;
4407 if (ds1 != 0 && ds2 == 0)
4408 return ds1;
4410 return ds_merge_1 (ds1, ds2, true);
4413 /* Return the probability of speculation success for the speculation
4414 status DS. */
4415 dw_t
4416 ds_weak (ds_t ds)
4418 ds_t res = 1, dt;
4419 int n = 0;
4421 dt = FIRST_SPEC_TYPE;
4424 if (ds & dt)
4426 res *= (ds_t) get_dep_weak (ds, dt);
4427 n++;
4430 if (dt == LAST_SPEC_TYPE)
4431 break;
4432 dt <<= SPEC_TYPE_SHIFT;
4434 while (1);
4436 gcc_assert (n);
4437 while (--n)
4438 res /= MAX_DEP_WEAK;
4440 if (res < MIN_DEP_WEAK)
4441 res = MIN_DEP_WEAK;
4443 gcc_assert (res <= MAX_DEP_WEAK);
4445 return (dw_t) res;
4448 /* Return a dep status that contains all speculation types of DS. */
4449 ds_t
4450 ds_get_speculation_types (ds_t ds)
4452 if (ds & BEGIN_DATA)
4453 ds |= BEGIN_DATA;
4454 if (ds & BE_IN_DATA)
4455 ds |= BE_IN_DATA;
4456 if (ds & BEGIN_CONTROL)
4457 ds |= BEGIN_CONTROL;
4458 if (ds & BE_IN_CONTROL)
4459 ds |= BE_IN_CONTROL;
4461 return ds & SPECULATIVE;
4464 /* Return a dep status that contains maximal weakness for each speculation
4465 type present in DS. */
4466 ds_t
4467 ds_get_max_dep_weak (ds_t ds)
4469 if (ds & BEGIN_DATA)
4470 ds = set_dep_weak (ds, BEGIN_DATA, MAX_DEP_WEAK);
4471 if (ds & BE_IN_DATA)
4472 ds = set_dep_weak (ds, BE_IN_DATA, MAX_DEP_WEAK);
4473 if (ds & BEGIN_CONTROL)
4474 ds = set_dep_weak (ds, BEGIN_CONTROL, MAX_DEP_WEAK);
4475 if (ds & BE_IN_CONTROL)
4476 ds = set_dep_weak (ds, BE_IN_CONTROL, MAX_DEP_WEAK);
4478 return ds;
4481 /* Dump information about the dependence status S. */
4482 static void
4483 dump_ds (FILE *f, ds_t s)
4485 fprintf (f, "{");
4487 if (s & BEGIN_DATA)
4488 fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak_1 (s, BEGIN_DATA));
4489 if (s & BE_IN_DATA)
4490 fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak_1 (s, BE_IN_DATA));
4491 if (s & BEGIN_CONTROL)
4492 fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak_1 (s, BEGIN_CONTROL));
4493 if (s & BE_IN_CONTROL)
4494 fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak_1 (s, BE_IN_CONTROL));
4496 if (s & HARD_DEP)
4497 fprintf (f, "HARD_DEP; ");
4499 if (s & DEP_TRUE)
4500 fprintf (f, "DEP_TRUE; ");
4501 if (s & DEP_OUTPUT)
4502 fprintf (f, "DEP_OUTPUT; ");
4503 if (s & DEP_ANTI)
4504 fprintf (f, "DEP_ANTI; ");
4505 if (s & DEP_CONTROL)
4506 fprintf (f, "DEP_CONTROL; ");
4508 fprintf (f, "}");
4511 DEBUG_FUNCTION void
4512 debug_ds (ds_t s)
4514 dump_ds (stderr, s);
4515 fprintf (stderr, "\n");
4518 #ifdef ENABLE_CHECKING
4519 /* Verify that dependence type and status are consistent.
4520 If RELAXED_P is true, then skip dep_weakness checks. */
4521 static void
4522 check_dep (dep_t dep, bool relaxed_p)
4524 enum reg_note dt = DEP_TYPE (dep);
4525 ds_t ds = DEP_STATUS (dep);
4527 gcc_assert (DEP_PRO (dep) != DEP_CON (dep));
4529 if (!(current_sched_info->flags & USE_DEPS_LIST))
4531 gcc_assert (ds == 0);
4532 return;
4535 /* Check that dependence type contains the same bits as the status. */
4536 if (dt == REG_DEP_TRUE)
4537 gcc_assert (ds & DEP_TRUE);
4538 else if (dt == REG_DEP_OUTPUT)
4539 gcc_assert ((ds & DEP_OUTPUT)
4540 && !(ds & DEP_TRUE));
4541 else if (dt == REG_DEP_ANTI)
4542 gcc_assert ((ds & DEP_ANTI)
4543 && !(ds & (DEP_OUTPUT | DEP_TRUE)));
4544 else
4545 gcc_assert (dt == REG_DEP_CONTROL
4546 && (ds & DEP_CONTROL)
4547 && !(ds & (DEP_OUTPUT | DEP_ANTI | DEP_TRUE)));
4549 /* HARD_DEP can not appear in dep_status of a link. */
4550 gcc_assert (!(ds & HARD_DEP));
4552 /* Check that dependence status is set correctly when speculation is not
4553 supported. */
4554 if (!sched_deps_info->generate_spec_deps)
4555 gcc_assert (!(ds & SPECULATIVE));
4556 else if (ds & SPECULATIVE)
4558 if (!relaxed_p)
4560 ds_t type = FIRST_SPEC_TYPE;
4562 /* Check that dependence weakness is in proper range. */
4565 if (ds & type)
4566 get_dep_weak (ds, type);
4568 if (type == LAST_SPEC_TYPE)
4569 break;
4570 type <<= SPEC_TYPE_SHIFT;
4572 while (1);
4575 if (ds & BEGIN_SPEC)
4577 /* Only true dependence can be data speculative. */
4578 if (ds & BEGIN_DATA)
4579 gcc_assert (ds & DEP_TRUE);
4581 /* Control dependencies in the insn scheduler are represented by
4582 anti-dependencies, therefore only anti dependence can be
4583 control speculative. */
4584 if (ds & BEGIN_CONTROL)
4585 gcc_assert (ds & DEP_ANTI);
4587 else
4589 /* Subsequent speculations should resolve true dependencies. */
4590 gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
4593 /* Check that true and anti dependencies can't have other speculative
4594 statuses. */
4595 if (ds & DEP_TRUE)
4596 gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
4597 /* An output dependence can't be speculative at all. */
4598 gcc_assert (!(ds & DEP_OUTPUT));
4599 if (ds & DEP_ANTI)
4600 gcc_assert (ds & BEGIN_CONTROL);
4603 #endif /* ENABLE_CHECKING */
4605 /* The following code discovers opportunities to switch a memory reference
4606 and an increment by modifying the address. We ensure that this is done
4607 only for dependencies that are only used to show a single register
4608 dependence (using DEP_NONREG and DEP_MULTIPLE), and so that every memory
4609 instruction involved is subject to only one dep that can cause a pattern
4610 change.
4612 When we discover a suitable dependency, we fill in the dep_replacement
4613 structure to show how to modify the memory reference. */
4615 /* Holds information about a pair of memory reference and register increment
4616 insns which depend on each other, but could possibly be interchanged. */
4617 struct mem_inc_info
4619 rtx_insn *inc_insn;
4620 rtx_insn *mem_insn;
4622 rtx *mem_loc;
4623 /* A register occurring in the memory address for which we wish to break
4624 the dependence. This must be identical to the destination register of
4625 the increment. */
4626 rtx mem_reg0;
4627 /* Any kind of index that is added to that register. */
4628 rtx mem_index;
4629 /* The constant offset used in the memory address. */
4630 HOST_WIDE_INT mem_constant;
4631 /* The constant added in the increment insn. Negated if the increment is
4632 after the memory address. */
4633 HOST_WIDE_INT inc_constant;
4634 /* The source register used in the increment. May be different from mem_reg0
4635 if the increment occurs before the memory address. */
4636 rtx inc_input;
4639 /* Verify that the memory location described in MII can be replaced with
4640 one using NEW_ADDR. Return the new memory reference or NULL_RTX. The
4641 insn remains unchanged by this function. */
4643 static rtx
4644 attempt_change (struct mem_inc_info *mii, rtx new_addr)
4646 rtx mem = *mii->mem_loc;
4647 rtx new_mem;
4649 /* Jump through a lot of hoops to keep the attributes up to date. We
4650 do not want to call one of the change address variants that take
4651 an offset even though we know the offset in many cases. These
4652 assume you are changing where the address is pointing by the
4653 offset. */
4654 new_mem = replace_equiv_address_nv (mem, new_addr);
4655 if (! validate_change (mii->mem_insn, mii->mem_loc, new_mem, 0))
4657 if (sched_verbose >= 5)
4658 fprintf (sched_dump, "validation failure\n");
4659 return NULL_RTX;
4662 /* Put back the old one. */
4663 validate_change (mii->mem_insn, mii->mem_loc, mem, 0);
4665 return new_mem;
4668 /* Return true if INSN is of a form "a = b op c" where a and b are
4669 regs. op is + if c is a reg and +|- if c is a const. Fill in
4670 informantion in MII about what is found.
4671 BEFORE_MEM indicates whether the increment is found before or after
4672 a corresponding memory reference. */
4674 static bool
4675 parse_add_or_inc (struct mem_inc_info *mii, rtx_insn *insn, bool before_mem)
4677 rtx pat = single_set (insn);
4678 rtx src, cst;
4679 bool regs_equal;
4681 if (RTX_FRAME_RELATED_P (insn) || !pat)
4682 return false;
4684 /* Result must be single reg. */
4685 if (!REG_P (SET_DEST (pat)))
4686 return false;
4688 if (GET_CODE (SET_SRC (pat)) != PLUS)
4689 return false;
4691 mii->inc_insn = insn;
4692 src = SET_SRC (pat);
4693 mii->inc_input = XEXP (src, 0);
4695 if (!REG_P (XEXP (src, 0)))
4696 return false;
4698 if (!rtx_equal_p (SET_DEST (pat), mii->mem_reg0))
4699 return false;
4701 cst = XEXP (src, 1);
4702 if (!CONST_INT_P (cst))
4703 return false;
4704 mii->inc_constant = INTVAL (cst);
4706 regs_equal = rtx_equal_p (mii->inc_input, mii->mem_reg0);
4708 if (!before_mem)
4710 mii->inc_constant = -mii->inc_constant;
4711 if (!regs_equal)
4712 return false;
4715 if (regs_equal && REGNO (SET_DEST (pat)) == STACK_POINTER_REGNUM)
4717 /* Note that the sign has already been reversed for !before_mem. */
4718 if (STACK_GROWS_DOWNWARD)
4719 return mii->inc_constant > 0;
4720 else
4721 return mii->inc_constant < 0;
4723 return true;
4726 /* Once a suitable mem reference has been found and the corresponding data
4727 in MII has been filled in, this function is called to find a suitable
4728 add or inc insn involving the register we found in the memory
4729 reference. */
4731 static bool
4732 find_inc (struct mem_inc_info *mii, bool backwards)
4734 sd_iterator_def sd_it;
4735 dep_t dep;
4737 sd_it = sd_iterator_start (mii->mem_insn,
4738 backwards ? SD_LIST_HARD_BACK : SD_LIST_FORW);
4739 while (sd_iterator_cond (&sd_it, &dep))
4741 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
4742 rtx_insn *pro = DEP_PRO (dep);
4743 rtx_insn *con = DEP_CON (dep);
4744 rtx_insn *inc_cand = backwards ? pro : con;
4745 if (DEP_NONREG (dep) || DEP_MULTIPLE (dep))
4746 goto next;
4747 if (parse_add_or_inc (mii, inc_cand, backwards))
4749 struct dep_replacement *desc;
4750 df_ref def;
4751 rtx newaddr, newmem;
4753 if (sched_verbose >= 5)
4754 fprintf (sched_dump, "candidate mem/inc pair: %d %d\n",
4755 INSN_UID (mii->mem_insn), INSN_UID (inc_cand));
4757 /* Need to assure that none of the operands of the inc
4758 instruction are assigned to by the mem insn. */
4759 FOR_EACH_INSN_DEF (def, mii->mem_insn)
4760 if (reg_overlap_mentioned_p (DF_REF_REG (def), mii->inc_input)
4761 || reg_overlap_mentioned_p (DF_REF_REG (def), mii->mem_reg0))
4763 if (sched_verbose >= 5)
4764 fprintf (sched_dump,
4765 "inc conflicts with store failure.\n");
4766 goto next;
4769 newaddr = mii->inc_input;
4770 if (mii->mem_index != NULL_RTX)
4771 newaddr = gen_rtx_PLUS (GET_MODE (newaddr), newaddr,
4772 mii->mem_index);
4773 newaddr = plus_constant (GET_MODE (newaddr), newaddr,
4774 mii->mem_constant + mii->inc_constant);
4775 newmem = attempt_change (mii, newaddr);
4776 if (newmem == NULL_RTX)
4777 goto next;
4778 if (sched_verbose >= 5)
4779 fprintf (sched_dump, "successful address replacement\n");
4780 desc = XCNEW (struct dep_replacement);
4781 DEP_REPLACE (dep) = desc;
4782 desc->loc = mii->mem_loc;
4783 desc->newval = newmem;
4784 desc->orig = *desc->loc;
4785 desc->insn = mii->mem_insn;
4786 move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
4787 INSN_SPEC_BACK_DEPS (con));
4788 if (backwards)
4790 FOR_EACH_DEP (mii->inc_insn, SD_LIST_BACK, sd_it, dep)
4791 add_dependence_1 (mii->mem_insn, DEP_PRO (dep),
4792 REG_DEP_TRUE);
4794 else
4796 FOR_EACH_DEP (mii->inc_insn, SD_LIST_FORW, sd_it, dep)
4797 add_dependence_1 (DEP_CON (dep), mii->mem_insn,
4798 REG_DEP_ANTI);
4800 return true;
4802 next:
4803 sd_iterator_next (&sd_it);
4805 return false;
4808 /* A recursive function that walks ADDRESS_OF_X to find memory references
4809 which could be modified during scheduling. We call find_inc for each
4810 one we find that has a recognizable form. MII holds information about
4811 the pair of memory/increment instructions.
4812 We ensure that every instruction with a memory reference (which will be
4813 the location of the replacement) is assigned at most one breakable
4814 dependency. */
4816 static bool
4817 find_mem (struct mem_inc_info *mii, rtx *address_of_x)
4819 rtx x = *address_of_x;
4820 enum rtx_code code = GET_CODE (x);
4821 const char *const fmt = GET_RTX_FORMAT (code);
4822 int i;
4824 if (code == MEM)
4826 rtx reg0 = XEXP (x, 0);
4828 mii->mem_loc = address_of_x;
4829 mii->mem_index = NULL_RTX;
4830 mii->mem_constant = 0;
4831 if (GET_CODE (reg0) == PLUS && CONST_INT_P (XEXP (reg0, 1)))
4833 mii->mem_constant = INTVAL (XEXP (reg0, 1));
4834 reg0 = XEXP (reg0, 0);
4836 if (GET_CODE (reg0) == PLUS)
4838 mii->mem_index = XEXP (reg0, 1);
4839 reg0 = XEXP (reg0, 0);
4841 if (REG_P (reg0))
4843 df_ref use;
4844 int occurrences = 0;
4846 /* Make sure this reg appears only once in this insn. Can't use
4847 count_occurrences since that only works for pseudos. */
4848 FOR_EACH_INSN_USE (use, mii->mem_insn)
4849 if (reg_overlap_mentioned_p (reg0, DF_REF_REG (use)))
4850 if (++occurrences > 1)
4852 if (sched_verbose >= 5)
4853 fprintf (sched_dump, "mem count failure\n");
4854 return false;
4857 mii->mem_reg0 = reg0;
4858 return find_inc (mii, true) || find_inc (mii, false);
4860 return false;
4863 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4865 /* If REG occurs inside a MEM used in a bit-field reference,
4866 that is unacceptable. */
4867 return false;
4870 /* Time for some deep diving. */
4871 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4873 if (fmt[i] == 'e')
4875 if (find_mem (mii, &XEXP (x, i)))
4876 return true;
4878 else if (fmt[i] == 'E')
4880 int j;
4881 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4882 if (find_mem (mii, &XVECEXP (x, i, j)))
4883 return true;
4886 return false;
4890 /* Examine the instructions between HEAD and TAIL and try to find
4891 dependencies that can be broken by modifying one of the patterns. */
4893 void
4894 find_modifiable_mems (rtx_insn *head, rtx_insn *tail)
4896 rtx_insn *insn, *next_tail = NEXT_INSN (tail);
4897 int success_in_block = 0;
4899 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4901 struct mem_inc_info mii;
4903 if (!NONDEBUG_INSN_P (insn) || RTX_FRAME_RELATED_P (insn))
4904 continue;
4906 mii.mem_insn = insn;
4907 if (find_mem (&mii, &PATTERN (insn)))
4908 success_in_block++;
4910 if (success_in_block && sched_verbose >= 5)
4911 fprintf (sched_dump, "%d candidates for address modification found.\n",
4912 success_in_block);
4915 #endif /* INSN_SCHEDULING */