2015-08-04 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / gcc / sched-deps.c
blob3ac66e8eeba107df19a06effd8bf85c770a5c3ee
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 "recog.h"
39 #include "emit-rtl.h"
40 #include "cfgbuild.h"
41 #include "sched-int.h"
42 #include "params.h"
43 #include "alloc-pool.h"
44 #include "cselib.h"
45 #include "ira.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 ira_implicitly_set_insn_hard_regs (&temp);
2895 AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
2896 IOR_HARD_REG_SET (implicit_reg_pending_clobbers, temp);
2899 can_start_lhs_rhs_p = (NONJUMP_INSN_P (insn)
2900 && code == SET);
2902 /* Group compare and branch insns for macro-fusion. */
2903 if (targetm.sched.macro_fusion_p
2904 && targetm.sched.macro_fusion_p ())
2905 sched_macro_fuse_insns (insn);
2907 if (may_trap_p (x))
2908 /* Avoid moving trapping instructions across function calls that might
2909 not always return. */
2910 add_dependence_list (insn, deps->last_function_call_may_noreturn,
2911 1, REG_DEP_ANTI, true);
2913 /* We must avoid creating a situation in which two successors of the
2914 current block have different unwind info after scheduling. If at any
2915 point the two paths re-join this leads to incorrect unwind info. */
2916 /* ??? There are certain situations involving a forced frame pointer in
2917 which, with extra effort, we could fix up the unwind info at a later
2918 CFG join. However, it seems better to notice these cases earlier
2919 during prologue generation and avoid marking the frame pointer setup
2920 as frame-related at all. */
2921 if (RTX_FRAME_RELATED_P (insn))
2923 /* Make sure prologue insn is scheduled before next jump. */
2924 deps->sched_before_next_jump
2925 = alloc_INSN_LIST (insn, deps->sched_before_next_jump);
2927 /* Make sure epilogue insn is scheduled after preceding jumps. */
2928 add_dependence_list (insn, deps->pending_jump_insns, 1, REG_DEP_ANTI,
2929 true);
2932 if (code == COND_EXEC)
2934 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
2936 /* ??? Should be recording conditions so we reduce the number of
2937 false dependencies. */
2938 x = COND_EXEC_CODE (x);
2939 code = GET_CODE (x);
2941 if (code == SET || code == CLOBBER)
2943 sched_analyze_1 (deps, x, insn);
2945 /* Bare clobber insns are used for letting life analysis, reg-stack
2946 and others know that a value is dead. Depend on the last call
2947 instruction so that reg-stack won't get confused. */
2948 if (code == CLOBBER)
2949 add_dependence_list (insn, deps->last_function_call, 1,
2950 REG_DEP_OUTPUT, true);
2952 else if (code == PARALLEL)
2954 for (i = XVECLEN (x, 0); i--;)
2956 rtx sub = XVECEXP (x, 0, i);
2957 code = GET_CODE (sub);
2959 if (code == COND_EXEC)
2961 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
2962 sub = COND_EXEC_CODE (sub);
2963 code = GET_CODE (sub);
2965 if (code == SET || code == CLOBBER)
2966 sched_analyze_1 (deps, sub, insn);
2967 else
2968 sched_analyze_2 (deps, sub, insn);
2971 else
2972 sched_analyze_2 (deps, x, insn);
2974 /* Mark registers CLOBBERED or used by called function. */
2975 if (CALL_P (insn))
2977 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2979 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
2980 sched_analyze_1 (deps, XEXP (link, 0), insn);
2981 else if (GET_CODE (XEXP (link, 0)) != SET)
2982 sched_analyze_2 (deps, XEXP (link, 0), insn);
2984 /* Don't schedule anything after a tail call, tail call needs
2985 to use at least all call-saved registers. */
2986 if (SIBLING_CALL_P (insn))
2987 reg_pending_barrier = TRUE_BARRIER;
2988 else if (find_reg_note (insn, REG_SETJMP, NULL))
2989 reg_pending_barrier = MOVE_BARRIER;
2992 if (JUMP_P (insn))
2994 rtx_insn *next = next_nonnote_nondebug_insn (insn);
2995 if (next && BARRIER_P (next))
2996 reg_pending_barrier = MOVE_BARRIER;
2997 else
2999 rtx_insn_list *pending;
3000 rtx_expr_list *pending_mem;
3002 if (sched_deps_info->compute_jump_reg_dependencies)
3004 (*sched_deps_info->compute_jump_reg_dependencies)
3005 (insn, reg_pending_control_uses);
3007 /* Make latency of jump equal to 0 by using anti-dependence. */
3008 EXECUTE_IF_SET_IN_REG_SET (reg_pending_control_uses, 0, i, rsi)
3010 struct deps_reg *reg_last = &deps->reg_last[i];
3011 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI,
3012 false);
3013 add_dependence_list (insn, reg_last->implicit_sets,
3014 0, REG_DEP_ANTI, false);
3015 add_dependence_list (insn, reg_last->clobbers, 0,
3016 REG_DEP_ANTI, false);
3020 /* All memory writes and volatile reads must happen before the
3021 jump. Non-volatile reads must happen before the jump iff
3022 the result is needed by the above register used mask. */
3024 pending = deps->pending_write_insns;
3025 pending_mem = deps->pending_write_mems;
3026 while (pending)
3028 if (! sched_insns_conditions_mutex_p (insn, pending->insn ()))
3029 add_dependence (insn, pending->insn (),
3030 REG_DEP_OUTPUT);
3031 pending = pending->next ();
3032 pending_mem = pending_mem->next ();
3035 pending = deps->pending_read_insns;
3036 pending_mem = deps->pending_read_mems;
3037 while (pending)
3039 if (MEM_VOLATILE_P (pending_mem->element ())
3040 && ! sched_insns_conditions_mutex_p (insn, pending->insn ()))
3041 add_dependence (insn, pending->insn (),
3042 REG_DEP_OUTPUT);
3043 pending = pending->next ();
3044 pending_mem = pending_mem->next ();
3047 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
3048 REG_DEP_ANTI, true);
3049 add_dependence_list (insn, deps->pending_jump_insns, 1,
3050 REG_DEP_ANTI, true);
3054 /* If this instruction can throw an exception, then moving it changes
3055 where block boundaries fall. This is mighty confusing elsewhere.
3056 Therefore, prevent such an instruction from being moved. Same for
3057 non-jump instructions that define block boundaries.
3058 ??? Unclear whether this is still necessary in EBB mode. If not,
3059 add_branch_dependences should be adjusted for RGN mode instead. */
3060 if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
3061 || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
3062 reg_pending_barrier = MOVE_BARRIER;
3064 if (sched_pressure != SCHED_PRESSURE_NONE)
3066 setup_insn_reg_uses (deps, insn);
3067 init_insn_reg_pressure_info (insn);
3070 /* Add register dependencies for insn. */
3071 if (DEBUG_INSN_P (insn))
3073 rtx_insn *prev = deps->last_debug_insn;
3074 rtx_insn_list *u;
3076 if (!deps->readonly)
3077 deps->last_debug_insn = insn;
3079 if (prev)
3080 add_dependence (insn, prev, REG_DEP_ANTI);
3082 add_dependence_list (insn, deps->last_function_call, 1,
3083 REG_DEP_ANTI, false);
3085 if (!sel_sched_p ())
3086 for (u = deps->last_pending_memory_flush; u; u = u->next ())
3087 add_dependence (insn, u->insn (), REG_DEP_ANTI);
3089 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
3091 struct deps_reg *reg_last = &deps->reg_last[i];
3092 add_dependence_list (insn, reg_last->sets, 1, REG_DEP_ANTI, false);
3093 /* There's no point in making REG_DEP_CONTROL dependencies for
3094 debug insns. */
3095 add_dependence_list (insn, reg_last->clobbers, 1, REG_DEP_ANTI,
3096 false);
3098 if (!deps->readonly)
3099 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3101 CLEAR_REG_SET (reg_pending_uses);
3103 /* Quite often, a debug insn will refer to stuff in the
3104 previous instruction, but the reason we want this
3105 dependency here is to make sure the scheduler doesn't
3106 gratuitously move a debug insn ahead. This could dirty
3107 DF flags and cause additional analysis that wouldn't have
3108 occurred in compilation without debug insns, and such
3109 additional analysis can modify the generated code. */
3110 prev = PREV_INSN (insn);
3112 if (prev && NONDEBUG_INSN_P (prev))
3113 add_dependence (insn, prev, REG_DEP_ANTI);
3115 else
3117 regset_head set_or_clobbered;
3119 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
3121 struct deps_reg *reg_last = &deps->reg_last[i];
3122 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE, false);
3123 add_dependence_list (insn, reg_last->implicit_sets, 0, REG_DEP_ANTI,
3124 false);
3125 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE,
3126 false);
3128 if (!deps->readonly)
3130 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3131 reg_last->uses_length++;
3135 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3136 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i))
3138 struct deps_reg *reg_last = &deps->reg_last[i];
3139 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE, false);
3140 add_dependence_list (insn, reg_last->implicit_sets, 0,
3141 REG_DEP_ANTI, false);
3142 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE,
3143 false);
3145 if (!deps->readonly)
3147 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3148 reg_last->uses_length++;
3152 if (targetm.sched.exposed_pipeline)
3154 INIT_REG_SET (&set_or_clobbered);
3155 bitmap_ior (&set_or_clobbered, reg_pending_clobbers,
3156 reg_pending_sets);
3157 EXECUTE_IF_SET_IN_REG_SET (&set_or_clobbered, 0, i, rsi)
3159 struct deps_reg *reg_last = &deps->reg_last[i];
3160 rtx list;
3161 for (list = reg_last->uses; list; list = XEXP (list, 1))
3163 rtx other = XEXP (list, 0);
3164 if (INSN_CACHED_COND (other) != const_true_rtx
3165 && refers_to_regno_p (i, INSN_CACHED_COND (other)))
3166 INSN_CACHED_COND (other) = const_true_rtx;
3171 /* If the current insn is conditional, we can't free any
3172 of the lists. */
3173 if (sched_has_condition_p (insn))
3175 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
3177 struct deps_reg *reg_last = &deps->reg_last[i];
3178 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
3179 false);
3180 add_dependence_list (insn, reg_last->implicit_sets, 0,
3181 REG_DEP_ANTI, false);
3182 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3183 false);
3184 add_dependence_list (insn, reg_last->control_uses, 0,
3185 REG_DEP_CONTROL, false);
3187 if (!deps->readonly)
3189 reg_last->clobbers
3190 = alloc_INSN_LIST (insn, reg_last->clobbers);
3191 reg_last->clobbers_length++;
3194 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
3196 struct deps_reg *reg_last = &deps->reg_last[i];
3197 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
3198 false);
3199 add_dependence_list (insn, reg_last->implicit_sets, 0,
3200 REG_DEP_ANTI, false);
3201 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT,
3202 false);
3203 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3204 false);
3205 add_dependence_list (insn, reg_last->control_uses, 0,
3206 REG_DEP_CONTROL, false);
3208 if (!deps->readonly)
3209 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3212 else
3214 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
3216 struct deps_reg *reg_last = &deps->reg_last[i];
3217 if (reg_last->uses_length >= MAX_PENDING_LIST_LENGTH
3218 || reg_last->clobbers_length >= MAX_PENDING_LIST_LENGTH)
3220 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3221 REG_DEP_OUTPUT, false);
3222 add_dependence_list_and_free (deps, insn,
3223 &reg_last->implicit_sets, 0,
3224 REG_DEP_ANTI, false);
3225 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3226 REG_DEP_ANTI, false);
3227 add_dependence_list_and_free (deps, insn,
3228 &reg_last->control_uses, 0,
3229 REG_DEP_ANTI, false);
3230 add_dependence_list_and_free (deps, insn,
3231 &reg_last->clobbers, 0,
3232 REG_DEP_OUTPUT, false);
3234 if (!deps->readonly)
3236 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3237 reg_last->clobbers_length = 0;
3238 reg_last->uses_length = 0;
3241 else
3243 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
3244 false);
3245 add_dependence_list (insn, reg_last->implicit_sets, 0,
3246 REG_DEP_ANTI, false);
3247 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3248 false);
3249 add_dependence_list (insn, reg_last->control_uses, 0,
3250 REG_DEP_CONTROL, false);
3253 if (!deps->readonly)
3255 reg_last->clobbers_length++;
3256 reg_last->clobbers
3257 = alloc_INSN_LIST (insn, reg_last->clobbers);
3260 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
3262 struct deps_reg *reg_last = &deps->reg_last[i];
3264 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3265 REG_DEP_OUTPUT, false);
3266 add_dependence_list_and_free (deps, insn,
3267 &reg_last->implicit_sets,
3268 0, REG_DEP_ANTI, false);
3269 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
3270 REG_DEP_OUTPUT, false);
3271 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3272 REG_DEP_ANTI, false);
3273 add_dependence_list (insn, reg_last->control_uses, 0,
3274 REG_DEP_CONTROL, false);
3276 if (!deps->readonly)
3278 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3279 reg_last->uses_length = 0;
3280 reg_last->clobbers_length = 0;
3284 if (!deps->readonly)
3286 EXECUTE_IF_SET_IN_REG_SET (reg_pending_control_uses, 0, i, rsi)
3288 struct deps_reg *reg_last = &deps->reg_last[i];
3289 reg_last->control_uses
3290 = alloc_INSN_LIST (insn, reg_last->control_uses);
3295 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3296 if (TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
3298 struct deps_reg *reg_last = &deps->reg_last[i];
3299 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI, false);
3300 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI, false);
3301 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI, false);
3302 add_dependence_list (insn, reg_last->control_uses, 0, REG_DEP_ANTI,
3303 false);
3305 if (!deps->readonly)
3306 reg_last->implicit_sets
3307 = alloc_INSN_LIST (insn, reg_last->implicit_sets);
3310 if (!deps->readonly)
3312 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
3313 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
3314 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
3315 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3316 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i)
3317 || TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
3318 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
3320 /* Set up the pending barrier found. */
3321 deps->last_reg_pending_barrier = reg_pending_barrier;
3324 CLEAR_REG_SET (reg_pending_uses);
3325 CLEAR_REG_SET (reg_pending_clobbers);
3326 CLEAR_REG_SET (reg_pending_sets);
3327 CLEAR_REG_SET (reg_pending_control_uses);
3328 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
3329 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
3331 /* Add dependencies if a scheduling barrier was found. */
3332 if (reg_pending_barrier)
3334 /* In the case of barrier the most added dependencies are not
3335 real, so we use anti-dependence here. */
3336 if (sched_has_condition_p (insn))
3338 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3340 struct deps_reg *reg_last = &deps->reg_last[i];
3341 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
3342 true);
3343 add_dependence_list (insn, reg_last->sets, 0,
3344 reg_pending_barrier == TRUE_BARRIER
3345 ? REG_DEP_TRUE : REG_DEP_ANTI, true);
3346 add_dependence_list (insn, reg_last->implicit_sets, 0,
3347 REG_DEP_ANTI, true);
3348 add_dependence_list (insn, reg_last->clobbers, 0,
3349 reg_pending_barrier == TRUE_BARRIER
3350 ? REG_DEP_TRUE : REG_DEP_ANTI, true);
3353 else
3355 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3357 struct deps_reg *reg_last = &deps->reg_last[i];
3358 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3359 REG_DEP_ANTI, true);
3360 add_dependence_list_and_free (deps, insn,
3361 &reg_last->control_uses, 0,
3362 REG_DEP_CONTROL, true);
3363 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3364 reg_pending_barrier == TRUE_BARRIER
3365 ? REG_DEP_TRUE : REG_DEP_ANTI,
3366 true);
3367 add_dependence_list_and_free (deps, insn,
3368 &reg_last->implicit_sets, 0,
3369 REG_DEP_ANTI, true);
3370 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
3371 reg_pending_barrier == TRUE_BARRIER
3372 ? REG_DEP_TRUE : REG_DEP_ANTI,
3373 true);
3375 if (!deps->readonly)
3377 reg_last->uses_length = 0;
3378 reg_last->clobbers_length = 0;
3383 if (!deps->readonly)
3384 for (i = 0; i < (unsigned)deps->max_reg; i++)
3386 struct deps_reg *reg_last = &deps->reg_last[i];
3387 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3388 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
3391 /* Don't flush pending lists on speculative checks for
3392 selective scheduling. */
3393 if (!sel_sched_p () || !sel_insn_is_speculation_check (insn))
3394 flush_pending_lists (deps, insn, true, true);
3396 reg_pending_barrier = NOT_A_BARRIER;
3399 /* If a post-call group is still open, see if it should remain so.
3400 This insn must be a simple move of a hard reg to a pseudo or
3401 vice-versa.
3403 We must avoid moving these insns for correctness on targets
3404 with small register classes, and for special registers like
3405 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
3406 hard regs for all targets. */
3408 if (deps->in_post_call_group_p)
3410 rtx tmp, set = single_set (insn);
3411 int src_regno, dest_regno;
3413 if (set == NULL)
3415 if (DEBUG_INSN_P (insn))
3416 /* We don't want to mark debug insns as part of the same
3417 sched group. We know they really aren't, but if we use
3418 debug insns to tell that a call group is over, we'll
3419 get different code if debug insns are not there and
3420 instructions that follow seem like they should be part
3421 of the call group.
3423 Also, if we did, chain_to_prev_insn would move the
3424 deps of the debug insn to the call insn, modifying
3425 non-debug post-dependency counts of the debug insn
3426 dependencies and otherwise messing with the scheduling
3427 order.
3429 Instead, let such debug insns be scheduled freely, but
3430 keep the call group open in case there are insns that
3431 should be part of it afterwards. Since we grant debug
3432 insns higher priority than even sched group insns, it
3433 will all turn out all right. */
3434 goto debug_dont_end_call_group;
3435 else
3436 goto end_call_group;
3439 tmp = SET_DEST (set);
3440 if (GET_CODE (tmp) == SUBREG)
3441 tmp = SUBREG_REG (tmp);
3442 if (REG_P (tmp))
3443 dest_regno = REGNO (tmp);
3444 else
3445 goto end_call_group;
3447 tmp = SET_SRC (set);
3448 if (GET_CODE (tmp) == SUBREG)
3449 tmp = SUBREG_REG (tmp);
3450 if ((GET_CODE (tmp) == PLUS
3451 || GET_CODE (tmp) == MINUS)
3452 && REG_P (XEXP (tmp, 0))
3453 && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
3454 && dest_regno == STACK_POINTER_REGNUM)
3455 src_regno = STACK_POINTER_REGNUM;
3456 else if (REG_P (tmp))
3457 src_regno = REGNO (tmp);
3458 else
3459 goto end_call_group;
3461 if (src_regno < FIRST_PSEUDO_REGISTER
3462 || dest_regno < FIRST_PSEUDO_REGISTER)
3464 if (!deps->readonly
3465 && deps->in_post_call_group_p == post_call_initial)
3466 deps->in_post_call_group_p = post_call;
3468 if (!sel_sched_p () || sched_emulate_haifa_p)
3470 SCHED_GROUP_P (insn) = 1;
3471 CANT_MOVE (insn) = 1;
3474 else
3476 end_call_group:
3477 if (!deps->readonly)
3478 deps->in_post_call_group_p = not_post_call;
3482 debug_dont_end_call_group:
3483 if ((current_sched_info->flags & DO_SPECULATION)
3484 && !sched_insn_is_legitimate_for_speculation_p (insn, 0))
3485 /* INSN has an internal dependency (e.g. r14 = [r14]) and thus cannot
3486 be speculated. */
3488 if (sel_sched_p ())
3489 sel_mark_hard_insn (insn);
3490 else
3492 sd_iterator_def sd_it;
3493 dep_t dep;
3495 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3496 sd_iterator_cond (&sd_it, &dep);)
3497 change_spec_dep_to_hard (sd_it);
3501 /* We do not yet have code to adjust REG_ARGS_SIZE, therefore we must
3502 honor their original ordering. */
3503 if (find_reg_note (insn, REG_ARGS_SIZE, NULL))
3505 if (deps->last_args_size)
3506 add_dependence (insn, deps->last_args_size, REG_DEP_OUTPUT);
3507 deps->last_args_size = insn;
3511 /* Return TRUE if INSN might not always return normally (e.g. call exit,
3512 longjmp, loop forever, ...). */
3513 /* FIXME: Why can't this function just use flags_from_decl_or_type and
3514 test for ECF_NORETURN? */
3515 static bool
3516 call_may_noreturn_p (rtx_insn *insn)
3518 rtx call;
3520 /* const or pure calls that aren't looping will always return. */
3521 if (RTL_CONST_OR_PURE_CALL_P (insn)
3522 && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn))
3523 return false;
3525 call = get_call_rtx_from (insn);
3526 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
3528 rtx symbol = XEXP (XEXP (call, 0), 0);
3529 if (SYMBOL_REF_DECL (symbol)
3530 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
3532 if (DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
3533 == BUILT_IN_NORMAL)
3534 switch (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol)))
3536 case BUILT_IN_BCMP:
3537 case BUILT_IN_BCOPY:
3538 case BUILT_IN_BZERO:
3539 case BUILT_IN_INDEX:
3540 case BUILT_IN_MEMCHR:
3541 case BUILT_IN_MEMCMP:
3542 case BUILT_IN_MEMCPY:
3543 case BUILT_IN_MEMMOVE:
3544 case BUILT_IN_MEMPCPY:
3545 case BUILT_IN_MEMSET:
3546 case BUILT_IN_RINDEX:
3547 case BUILT_IN_STPCPY:
3548 case BUILT_IN_STPNCPY:
3549 case BUILT_IN_STRCAT:
3550 case BUILT_IN_STRCHR:
3551 case BUILT_IN_STRCMP:
3552 case BUILT_IN_STRCPY:
3553 case BUILT_IN_STRCSPN:
3554 case BUILT_IN_STRLEN:
3555 case BUILT_IN_STRNCAT:
3556 case BUILT_IN_STRNCMP:
3557 case BUILT_IN_STRNCPY:
3558 case BUILT_IN_STRPBRK:
3559 case BUILT_IN_STRRCHR:
3560 case BUILT_IN_STRSPN:
3561 case BUILT_IN_STRSTR:
3562 /* Assume certain string/memory builtins always return. */
3563 return false;
3564 default:
3565 break;
3570 /* For all other calls assume that they might not always return. */
3571 return true;
3574 /* Return true if INSN should be made dependent on the previous instruction
3575 group, and if all INSN's dependencies should be moved to the first
3576 instruction of that group. */
3578 static bool
3579 chain_to_prev_insn_p (rtx_insn *insn)
3581 /* INSN forms a group with the previous instruction. */
3582 if (SCHED_GROUP_P (insn))
3583 return true;
3585 /* If the previous instruction clobbers a register R and this one sets
3586 part of R, the clobber was added specifically to help us track the
3587 liveness of R. There's no point scheduling the clobber and leaving
3588 INSN behind, especially if we move the clobber to another block. */
3589 rtx_insn *prev = prev_nonnote_nondebug_insn (insn);
3590 if (prev
3591 && INSN_P (prev)
3592 && BLOCK_FOR_INSN (prev) == BLOCK_FOR_INSN (insn)
3593 && GET_CODE (PATTERN (prev)) == CLOBBER)
3595 rtx x = XEXP (PATTERN (prev), 0);
3596 if (set_of (x, insn))
3597 return true;
3600 return false;
3603 /* Analyze INSN with DEPS as a context. */
3604 void
3605 deps_analyze_insn (struct deps_desc *deps, rtx_insn *insn)
3607 if (sched_deps_info->start_insn)
3608 sched_deps_info->start_insn (insn);
3610 /* Record the condition for this insn. */
3611 if (NONDEBUG_INSN_P (insn))
3613 rtx t;
3614 sched_get_condition_with_rev (insn, NULL);
3615 t = INSN_CACHED_COND (insn);
3616 INSN_COND_DEPS (insn) = NULL;
3617 if (reload_completed
3618 && (current_sched_info->flags & DO_PREDICATION)
3619 && COMPARISON_P (t)
3620 && REG_P (XEXP (t, 0))
3621 && CONSTANT_P (XEXP (t, 1)))
3623 unsigned int regno;
3624 int nregs;
3625 rtx_insn_list *cond_deps = NULL;
3626 t = XEXP (t, 0);
3627 regno = REGNO (t);
3628 nregs = REG_NREGS (t);
3629 while (nregs-- > 0)
3631 struct deps_reg *reg_last = &deps->reg_last[regno + nregs];
3632 cond_deps = concat_INSN_LIST (reg_last->sets, cond_deps);
3633 cond_deps = concat_INSN_LIST (reg_last->clobbers, cond_deps);
3634 cond_deps = concat_INSN_LIST (reg_last->implicit_sets, cond_deps);
3636 INSN_COND_DEPS (insn) = cond_deps;
3640 if (JUMP_P (insn))
3642 /* Make each JUMP_INSN (but not a speculative check)
3643 a scheduling barrier for memory references. */
3644 if (!deps->readonly
3645 && !(sel_sched_p ()
3646 && sel_insn_is_speculation_check (insn)))
3648 /* Keep the list a reasonable size. */
3649 if (deps->pending_flush_length++ >= MAX_PENDING_LIST_LENGTH)
3650 flush_pending_lists (deps, insn, true, true);
3651 else
3652 deps->pending_jump_insns
3653 = alloc_INSN_LIST (insn, deps->pending_jump_insns);
3656 /* For each insn which shouldn't cross a jump, add a dependence. */
3657 add_dependence_list_and_free (deps, insn,
3658 &deps->sched_before_next_jump, 1,
3659 REG_DEP_ANTI, true);
3661 sched_analyze_insn (deps, PATTERN (insn), insn);
3663 else if (NONJUMP_INSN_P (insn) || DEBUG_INSN_P (insn))
3665 sched_analyze_insn (deps, PATTERN (insn), insn);
3667 else if (CALL_P (insn))
3669 int i;
3671 CANT_MOVE (insn) = 1;
3673 if (find_reg_note (insn, REG_SETJMP, NULL))
3675 /* This is setjmp. Assume that all registers, not just
3676 hard registers, may be clobbered by this call. */
3677 reg_pending_barrier = MOVE_BARRIER;
3679 else
3681 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3682 /* A call may read and modify global register variables. */
3683 if (global_regs[i])
3685 SET_REGNO_REG_SET (reg_pending_sets, i);
3686 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3688 /* Other call-clobbered hard regs may be clobbered.
3689 Since we only have a choice between 'might be clobbered'
3690 and 'definitely not clobbered', we must include all
3691 partly call-clobbered registers here. */
3692 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
3693 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
3694 SET_REGNO_REG_SET (reg_pending_clobbers, i);
3695 /* We don't know what set of fixed registers might be used
3696 by the function, but it is certain that the stack pointer
3697 is among them, but be conservative. */
3698 else if (fixed_regs[i])
3699 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3700 /* The frame pointer is normally not used by the function
3701 itself, but by the debugger. */
3702 /* ??? MIPS o32 is an exception. It uses the frame pointer
3703 in the macro expansion of jal but does not represent this
3704 fact in the call_insn rtl. */
3705 else if (i == FRAME_POINTER_REGNUM
3706 || (i == HARD_FRAME_POINTER_REGNUM
3707 && (! reload_completed || frame_pointer_needed)))
3708 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3711 /* For each insn which shouldn't cross a call, add a dependence
3712 between that insn and this call insn. */
3713 add_dependence_list_and_free (deps, insn,
3714 &deps->sched_before_next_call, 1,
3715 REG_DEP_ANTI, true);
3717 sched_analyze_insn (deps, PATTERN (insn), insn);
3719 /* If CALL would be in a sched group, then this will violate
3720 convention that sched group insns have dependencies only on the
3721 previous instruction.
3723 Of course one can say: "Hey! What about head of the sched group?"
3724 And I will answer: "Basic principles (one dep per insn) are always
3725 the same." */
3726 gcc_assert (!SCHED_GROUP_P (insn));
3728 /* In the absence of interprocedural alias analysis, we must flush
3729 all pending reads and writes, and start new dependencies starting
3730 from here. But only flush writes for constant calls (which may
3731 be passed a pointer to something we haven't written yet). */
3732 flush_pending_lists (deps, insn, true, ! RTL_CONST_OR_PURE_CALL_P (insn));
3734 if (!deps->readonly)
3736 /* Remember the last function call for limiting lifetimes. */
3737 free_INSN_LIST_list (&deps->last_function_call);
3738 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
3740 if (call_may_noreturn_p (insn))
3742 /* Remember the last function call that might not always return
3743 normally for limiting moves of trapping insns. */
3744 free_INSN_LIST_list (&deps->last_function_call_may_noreturn);
3745 deps->last_function_call_may_noreturn
3746 = alloc_INSN_LIST (insn, NULL_RTX);
3749 /* Before reload, begin a post-call group, so as to keep the
3750 lifetimes of hard registers correct. */
3751 if (! reload_completed)
3752 deps->in_post_call_group_p = post_call;
3756 if (sched_deps_info->use_cselib)
3757 cselib_process_insn (insn);
3759 if (sched_deps_info->finish_insn)
3760 sched_deps_info->finish_insn ();
3762 /* Fixup the dependencies in the sched group. */
3763 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
3764 && chain_to_prev_insn_p (insn)
3765 && !sel_sched_p ())
3766 chain_to_prev_insn (insn);
3769 /* Initialize DEPS for the new block beginning with HEAD. */
3770 void
3771 deps_start_bb (struct deps_desc *deps, rtx_insn *head)
3773 gcc_assert (!deps->readonly);
3775 /* Before reload, if the previous block ended in a call, show that
3776 we are inside a post-call group, so as to keep the lifetimes of
3777 hard registers correct. */
3778 if (! reload_completed && !LABEL_P (head))
3780 rtx_insn *insn = prev_nonnote_nondebug_insn (head);
3782 if (insn && CALL_P (insn))
3783 deps->in_post_call_group_p = post_call_initial;
3787 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
3788 dependencies for each insn. */
3789 void
3790 sched_analyze (struct deps_desc *deps, rtx_insn *head, rtx_insn *tail)
3792 rtx_insn *insn;
3794 if (sched_deps_info->use_cselib)
3795 cselib_init (CSELIB_RECORD_MEMORY);
3797 deps_start_bb (deps, head);
3799 for (insn = head;; insn = NEXT_INSN (insn))
3802 if (INSN_P (insn))
3804 /* And initialize deps_lists. */
3805 sd_init_insn (insn);
3806 /* Clean up SCHED_GROUP_P which may be set by last
3807 scheduler pass. */
3808 if (SCHED_GROUP_P (insn))
3809 SCHED_GROUP_P (insn) = 0;
3812 deps_analyze_insn (deps, insn);
3814 if (insn == tail)
3816 if (sched_deps_info->use_cselib)
3817 cselib_finish ();
3818 return;
3821 gcc_unreachable ();
3824 /* Helper for sched_free_deps ().
3825 Delete INSN's (RESOLVED_P) backward dependencies. */
3826 static void
3827 delete_dep_nodes_in_back_deps (rtx_insn *insn, bool resolved_p)
3829 sd_iterator_def sd_it;
3830 dep_t dep;
3831 sd_list_types_def types;
3833 if (resolved_p)
3834 types = SD_LIST_RES_BACK;
3835 else
3836 types = SD_LIST_BACK;
3838 for (sd_it = sd_iterator_start (insn, types);
3839 sd_iterator_cond (&sd_it, &dep);)
3841 dep_link_t link = *sd_it.linkp;
3842 dep_node_t node = DEP_LINK_NODE (link);
3843 deps_list_t back_list;
3844 deps_list_t forw_list;
3846 get_back_and_forw_lists (dep, resolved_p, &back_list, &forw_list);
3847 remove_from_deps_list (link, back_list);
3848 delete_dep_node (node);
3852 /* Delete (RESOLVED_P) dependencies between HEAD and TAIL together with
3853 deps_lists. */
3854 void
3855 sched_free_deps (rtx_insn *head, rtx_insn *tail, bool resolved_p)
3857 rtx_insn *insn;
3858 rtx_insn *next_tail = NEXT_INSN (tail);
3860 /* We make two passes since some insns may be scheduled before their
3861 dependencies are resolved. */
3862 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3863 if (INSN_P (insn) && INSN_LUID (insn) > 0)
3865 /* Clear forward deps and leave the dep_nodes to the
3866 corresponding back_deps list. */
3867 if (resolved_p)
3868 clear_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
3869 else
3870 clear_deps_list (INSN_FORW_DEPS (insn));
3872 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3873 if (INSN_P (insn) && INSN_LUID (insn) > 0)
3875 /* Clear resolved back deps together with its dep_nodes. */
3876 delete_dep_nodes_in_back_deps (insn, resolved_p);
3878 sd_finish_insn (insn);
3882 /* Initialize variables for region data dependence analysis.
3883 When LAZY_REG_LAST is true, do not allocate reg_last array
3884 of struct deps_desc immediately. */
3886 void
3887 init_deps (struct deps_desc *deps, bool lazy_reg_last)
3889 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
3891 deps->max_reg = max_reg;
3892 if (lazy_reg_last)
3893 deps->reg_last = NULL;
3894 else
3895 deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
3896 INIT_REG_SET (&deps->reg_last_in_use);
3898 deps->pending_read_insns = 0;
3899 deps->pending_read_mems = 0;
3900 deps->pending_write_insns = 0;
3901 deps->pending_write_mems = 0;
3902 deps->pending_jump_insns = 0;
3903 deps->pending_read_list_length = 0;
3904 deps->pending_write_list_length = 0;
3905 deps->pending_flush_length = 0;
3906 deps->last_pending_memory_flush = 0;
3907 deps->last_function_call = 0;
3908 deps->last_function_call_may_noreturn = 0;
3909 deps->sched_before_next_call = 0;
3910 deps->sched_before_next_jump = 0;
3911 deps->in_post_call_group_p = not_post_call;
3912 deps->last_debug_insn = 0;
3913 deps->last_args_size = 0;
3914 deps->last_reg_pending_barrier = NOT_A_BARRIER;
3915 deps->readonly = 0;
3918 /* Init only reg_last field of DEPS, which was not allocated before as
3919 we inited DEPS lazily. */
3920 void
3921 init_deps_reg_last (struct deps_desc *deps)
3923 gcc_assert (deps && deps->max_reg > 0);
3924 gcc_assert (deps->reg_last == NULL);
3926 deps->reg_last = XCNEWVEC (struct deps_reg, deps->max_reg);
3930 /* Free insn lists found in DEPS. */
3932 void
3933 free_deps (struct deps_desc *deps)
3935 unsigned i;
3936 reg_set_iterator rsi;
3938 /* We set max_reg to 0 when this context was already freed. */
3939 if (deps->max_reg == 0)
3941 gcc_assert (deps->reg_last == NULL);
3942 return;
3944 deps->max_reg = 0;
3946 free_INSN_LIST_list (&deps->pending_read_insns);
3947 free_EXPR_LIST_list (&deps->pending_read_mems);
3948 free_INSN_LIST_list (&deps->pending_write_insns);
3949 free_EXPR_LIST_list (&deps->pending_write_mems);
3950 free_INSN_LIST_list (&deps->last_pending_memory_flush);
3952 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
3953 times. For a testcase with 42000 regs and 8000 small basic blocks,
3954 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
3955 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3957 struct deps_reg *reg_last = &deps->reg_last[i];
3958 if (reg_last->uses)
3959 free_INSN_LIST_list (&reg_last->uses);
3960 if (reg_last->sets)
3961 free_INSN_LIST_list (&reg_last->sets);
3962 if (reg_last->implicit_sets)
3963 free_INSN_LIST_list (&reg_last->implicit_sets);
3964 if (reg_last->control_uses)
3965 free_INSN_LIST_list (&reg_last->control_uses);
3966 if (reg_last->clobbers)
3967 free_INSN_LIST_list (&reg_last->clobbers);
3969 CLEAR_REG_SET (&deps->reg_last_in_use);
3971 /* As we initialize reg_last lazily, it is possible that we didn't allocate
3972 it at all. */
3973 free (deps->reg_last);
3974 deps->reg_last = NULL;
3976 deps = NULL;
3979 /* Remove INSN from dependence contexts DEPS. */
3980 void
3981 remove_from_deps (struct deps_desc *deps, rtx_insn *insn)
3983 int removed;
3984 unsigned i;
3985 reg_set_iterator rsi;
3987 removed = remove_from_both_dependence_lists (insn, &deps->pending_read_insns,
3988 &deps->pending_read_mems);
3989 if (!DEBUG_INSN_P (insn))
3990 deps->pending_read_list_length -= removed;
3991 removed = remove_from_both_dependence_lists (insn, &deps->pending_write_insns,
3992 &deps->pending_write_mems);
3993 deps->pending_write_list_length -= removed;
3995 removed = remove_from_dependence_list (insn, &deps->pending_jump_insns);
3996 deps->pending_flush_length -= removed;
3997 removed = remove_from_dependence_list (insn, &deps->last_pending_memory_flush);
3998 deps->pending_flush_length -= removed;
4000 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
4002 struct deps_reg *reg_last = &deps->reg_last[i];
4003 if (reg_last->uses)
4004 remove_from_dependence_list (insn, &reg_last->uses);
4005 if (reg_last->sets)
4006 remove_from_dependence_list (insn, &reg_last->sets);
4007 if (reg_last->implicit_sets)
4008 remove_from_dependence_list (insn, &reg_last->implicit_sets);
4009 if (reg_last->clobbers)
4010 remove_from_dependence_list (insn, &reg_last->clobbers);
4011 if (!reg_last->uses && !reg_last->sets && !reg_last->implicit_sets
4012 && !reg_last->clobbers)
4013 CLEAR_REGNO_REG_SET (&deps->reg_last_in_use, i);
4016 if (CALL_P (insn))
4018 remove_from_dependence_list (insn, &deps->last_function_call);
4019 remove_from_dependence_list (insn,
4020 &deps->last_function_call_may_noreturn);
4022 remove_from_dependence_list (insn, &deps->sched_before_next_call);
4025 /* Init deps data vector. */
4026 static void
4027 init_deps_data_vector (void)
4029 int reserve = (sched_max_luid + 1 - h_d_i_d.length ());
4030 if (reserve > 0 && ! h_d_i_d.space (reserve))
4031 h_d_i_d.safe_grow_cleared (3 * sched_max_luid / 2);
4034 /* If it is profitable to use them, initialize or extend (depending on
4035 GLOBAL_P) dependency data. */
4036 void
4037 sched_deps_init (bool global_p)
4039 /* Average number of insns in the basic block.
4040 '+ 1' is used to make it nonzero. */
4041 int insns_in_block = sched_max_luid / n_basic_blocks_for_fn (cfun) + 1;
4043 init_deps_data_vector ();
4045 /* We use another caching mechanism for selective scheduling, so
4046 we don't use this one. */
4047 if (!sel_sched_p () && global_p && insns_in_block > 100 * 5)
4049 /* ?!? We could save some memory by computing a per-region luid mapping
4050 which could reduce both the number of vectors in the cache and the
4051 size of each vector. Instead we just avoid the cache entirely unless
4052 the average number of instructions in a basic block is very high. See
4053 the comment before the declaration of true_dependency_cache for
4054 what we consider "very high". */
4055 cache_size = 0;
4056 extend_dependency_caches (sched_max_luid, true);
4059 if (global_p)
4061 dl_pool = new object_allocator<_deps_list> ("deps_list",
4062 /* Allocate lists for one block at a time. */
4063 insns_in_block);
4064 dn_pool = new object_allocator<_dep_node> ("dep_node",
4065 /* Allocate nodes for one block at a time.
4066 We assume that average insn has
4067 5 producers. */
4068 5 * insns_in_block);
4073 /* Create or extend (depending on CREATE_P) dependency caches to
4074 size N. */
4075 void
4076 extend_dependency_caches (int n, bool create_p)
4078 if (create_p || true_dependency_cache)
4080 int i, luid = cache_size + n;
4082 true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
4083 luid);
4084 output_dependency_cache = XRESIZEVEC (bitmap_head,
4085 output_dependency_cache, luid);
4086 anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
4087 luid);
4088 control_dependency_cache = XRESIZEVEC (bitmap_head, control_dependency_cache,
4089 luid);
4091 if (current_sched_info->flags & DO_SPECULATION)
4092 spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
4093 luid);
4095 for (i = cache_size; i < luid; i++)
4097 bitmap_initialize (&true_dependency_cache[i], 0);
4098 bitmap_initialize (&output_dependency_cache[i], 0);
4099 bitmap_initialize (&anti_dependency_cache[i], 0);
4100 bitmap_initialize (&control_dependency_cache[i], 0);
4102 if (current_sched_info->flags & DO_SPECULATION)
4103 bitmap_initialize (&spec_dependency_cache[i], 0);
4105 cache_size = luid;
4109 /* Finalize dependency information for the whole function. */
4110 void
4111 sched_deps_finish (void)
4113 gcc_assert (deps_pools_are_empty_p ());
4114 dn_pool->release_if_empty ();
4115 dn_pool = NULL;
4116 dl_pool->release_if_empty ();
4117 dl_pool = NULL;
4119 h_d_i_d.release ();
4120 cache_size = 0;
4122 if (true_dependency_cache)
4124 int i;
4126 for (i = 0; i < cache_size; i++)
4128 bitmap_clear (&true_dependency_cache[i]);
4129 bitmap_clear (&output_dependency_cache[i]);
4130 bitmap_clear (&anti_dependency_cache[i]);
4131 bitmap_clear (&control_dependency_cache[i]);
4133 if (sched_deps_info->generate_spec_deps)
4134 bitmap_clear (&spec_dependency_cache[i]);
4136 free (true_dependency_cache);
4137 true_dependency_cache = NULL;
4138 free (output_dependency_cache);
4139 output_dependency_cache = NULL;
4140 free (anti_dependency_cache);
4141 anti_dependency_cache = NULL;
4142 free (control_dependency_cache);
4143 control_dependency_cache = NULL;
4145 if (sched_deps_info->generate_spec_deps)
4147 free (spec_dependency_cache);
4148 spec_dependency_cache = NULL;
4154 /* Initialize some global variables needed by the dependency analysis
4155 code. */
4157 void
4158 init_deps_global (void)
4160 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
4161 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
4162 reg_pending_sets = ALLOC_REG_SET (&reg_obstack);
4163 reg_pending_clobbers = ALLOC_REG_SET (&reg_obstack);
4164 reg_pending_uses = ALLOC_REG_SET (&reg_obstack);
4165 reg_pending_control_uses = ALLOC_REG_SET (&reg_obstack);
4166 reg_pending_barrier = NOT_A_BARRIER;
4168 if (!sel_sched_p () || sched_emulate_haifa_p)
4170 sched_deps_info->start_insn = haifa_start_insn;
4171 sched_deps_info->finish_insn = haifa_finish_insn;
4173 sched_deps_info->note_reg_set = haifa_note_reg_set;
4174 sched_deps_info->note_reg_clobber = haifa_note_reg_clobber;
4175 sched_deps_info->note_reg_use = haifa_note_reg_use;
4177 sched_deps_info->note_mem_dep = haifa_note_mem_dep;
4178 sched_deps_info->note_dep = haifa_note_dep;
4182 /* Free everything used by the dependency analysis code. */
4184 void
4185 finish_deps_global (void)
4187 FREE_REG_SET (reg_pending_sets);
4188 FREE_REG_SET (reg_pending_clobbers);
4189 FREE_REG_SET (reg_pending_uses);
4190 FREE_REG_SET (reg_pending_control_uses);
4193 /* Estimate the weakness of dependence between MEM1 and MEM2. */
4194 dw_t
4195 estimate_dep_weak (rtx mem1, rtx mem2)
4197 rtx r1, r2;
4199 if (mem1 == mem2)
4200 /* MEMs are the same - don't speculate. */
4201 return MIN_DEP_WEAK;
4203 r1 = XEXP (mem1, 0);
4204 r2 = XEXP (mem2, 0);
4206 if (r1 == r2
4207 || (REG_P (r1) && REG_P (r2)
4208 && REGNO (r1) == REGNO (r2)))
4209 /* Again, MEMs are the same. */
4210 return MIN_DEP_WEAK;
4211 else if ((REG_P (r1) && !REG_P (r2))
4212 || (!REG_P (r1) && REG_P (r2)))
4213 /* Different addressing modes - reason to be more speculative,
4214 than usual. */
4215 return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
4216 else
4217 /* We can't say anything about the dependence. */
4218 return UNCERTAIN_DEP_WEAK;
4221 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
4222 This function can handle same INSN and ELEM (INSN == ELEM).
4223 It is a convenience wrapper. */
4224 static void
4225 add_dependence_1 (rtx_insn *insn, rtx_insn *elem, enum reg_note dep_type)
4227 ds_t ds;
4228 bool internal;
4230 if (dep_type == REG_DEP_TRUE)
4231 ds = DEP_TRUE;
4232 else if (dep_type == REG_DEP_OUTPUT)
4233 ds = DEP_OUTPUT;
4234 else if (dep_type == REG_DEP_CONTROL)
4235 ds = DEP_CONTROL;
4236 else
4238 gcc_assert (dep_type == REG_DEP_ANTI);
4239 ds = DEP_ANTI;
4242 /* When add_dependence is called from inside sched-deps.c, we expect
4243 cur_insn to be non-null. */
4244 internal = cur_insn != NULL;
4245 if (internal)
4246 gcc_assert (insn == cur_insn);
4247 else
4248 cur_insn = insn;
4250 note_dep (elem, ds);
4251 if (!internal)
4252 cur_insn = NULL;
4255 /* Return weakness of speculative type TYPE in the dep_status DS,
4256 without checking to prevent ICEs on malformed input. */
4257 static dw_t
4258 get_dep_weak_1 (ds_t ds, ds_t type)
4260 ds = ds & type;
4262 switch (type)
4264 case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
4265 case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
4266 case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
4267 case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
4268 default: gcc_unreachable ();
4271 return (dw_t) ds;
4274 /* Return weakness of speculative type TYPE in the dep_status DS. */
4275 dw_t
4276 get_dep_weak (ds_t ds, ds_t type)
4278 dw_t dw = get_dep_weak_1 (ds, type);
4280 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
4281 return dw;
4284 /* Return the dep_status, which has the same parameters as DS, except for
4285 speculative type TYPE, that will have weakness DW. */
4286 ds_t
4287 set_dep_weak (ds_t ds, ds_t type, dw_t dw)
4289 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
4291 ds &= ~type;
4292 switch (type)
4294 case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
4295 case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
4296 case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
4297 case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
4298 default: gcc_unreachable ();
4300 return ds;
4303 /* Return the join of two dep_statuses DS1 and DS2.
4304 If MAX_P is true then choose the greater probability,
4305 otherwise multiply probabilities.
4306 This function assumes that both DS1 and DS2 contain speculative bits. */
4307 static ds_t
4308 ds_merge_1 (ds_t ds1, ds_t ds2, bool max_p)
4310 ds_t ds, t;
4312 gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
4314 ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
4316 t = FIRST_SPEC_TYPE;
4319 if ((ds1 & t) && !(ds2 & t))
4320 ds |= ds1 & t;
4321 else if (!(ds1 & t) && (ds2 & t))
4322 ds |= ds2 & t;
4323 else if ((ds1 & t) && (ds2 & t))
4325 dw_t dw1 = get_dep_weak (ds1, t);
4326 dw_t dw2 = get_dep_weak (ds2, t);
4327 ds_t dw;
4329 if (!max_p)
4331 dw = ((ds_t) dw1) * ((ds_t) dw2);
4332 dw /= MAX_DEP_WEAK;
4333 if (dw < MIN_DEP_WEAK)
4334 dw = MIN_DEP_WEAK;
4336 else
4338 if (dw1 >= dw2)
4339 dw = dw1;
4340 else
4341 dw = dw2;
4344 ds = set_dep_weak (ds, t, (dw_t) dw);
4347 if (t == LAST_SPEC_TYPE)
4348 break;
4349 t <<= SPEC_TYPE_SHIFT;
4351 while (1);
4353 return ds;
4356 /* Return the join of two dep_statuses DS1 and DS2.
4357 This function assumes that both DS1 and DS2 contain speculative bits. */
4358 ds_t
4359 ds_merge (ds_t ds1, ds_t ds2)
4361 return ds_merge_1 (ds1, ds2, false);
4364 /* Return the join of two dep_statuses DS1 and DS2. */
4365 ds_t
4366 ds_full_merge (ds_t ds, ds_t ds2, rtx mem1, rtx mem2)
4368 ds_t new_status = ds | ds2;
4370 if (new_status & SPECULATIVE)
4372 if ((ds && !(ds & SPECULATIVE))
4373 || (ds2 && !(ds2 & SPECULATIVE)))
4374 /* Then this dep can't be speculative. */
4375 new_status &= ~SPECULATIVE;
4376 else
4378 /* Both are speculative. Merging probabilities. */
4379 if (mem1)
4381 dw_t dw;
4383 dw = estimate_dep_weak (mem1, mem2);
4384 ds = set_dep_weak (ds, BEGIN_DATA, dw);
4387 if (!ds)
4388 new_status = ds2;
4389 else if (!ds2)
4390 new_status = ds;
4391 else
4392 new_status = ds_merge (ds2, ds);
4396 return new_status;
4399 /* Return the join of DS1 and DS2. Use maximum instead of multiplying
4400 probabilities. */
4401 ds_t
4402 ds_max_merge (ds_t ds1, ds_t ds2)
4404 if (ds1 == 0 && ds2 == 0)
4405 return 0;
4407 if (ds1 == 0 && ds2 != 0)
4408 return ds2;
4410 if (ds1 != 0 && ds2 == 0)
4411 return ds1;
4413 return ds_merge_1 (ds1, ds2, true);
4416 /* Return the probability of speculation success for the speculation
4417 status DS. */
4418 dw_t
4419 ds_weak (ds_t ds)
4421 ds_t res = 1, dt;
4422 int n = 0;
4424 dt = FIRST_SPEC_TYPE;
4427 if (ds & dt)
4429 res *= (ds_t) get_dep_weak (ds, dt);
4430 n++;
4433 if (dt == LAST_SPEC_TYPE)
4434 break;
4435 dt <<= SPEC_TYPE_SHIFT;
4437 while (1);
4439 gcc_assert (n);
4440 while (--n)
4441 res /= MAX_DEP_WEAK;
4443 if (res < MIN_DEP_WEAK)
4444 res = MIN_DEP_WEAK;
4446 gcc_assert (res <= MAX_DEP_WEAK);
4448 return (dw_t) res;
4451 /* Return a dep status that contains all speculation types of DS. */
4452 ds_t
4453 ds_get_speculation_types (ds_t ds)
4455 if (ds & BEGIN_DATA)
4456 ds |= BEGIN_DATA;
4457 if (ds & BE_IN_DATA)
4458 ds |= BE_IN_DATA;
4459 if (ds & BEGIN_CONTROL)
4460 ds |= BEGIN_CONTROL;
4461 if (ds & BE_IN_CONTROL)
4462 ds |= BE_IN_CONTROL;
4464 return ds & SPECULATIVE;
4467 /* Return a dep status that contains maximal weakness for each speculation
4468 type present in DS. */
4469 ds_t
4470 ds_get_max_dep_weak (ds_t ds)
4472 if (ds & BEGIN_DATA)
4473 ds = set_dep_weak (ds, BEGIN_DATA, MAX_DEP_WEAK);
4474 if (ds & BE_IN_DATA)
4475 ds = set_dep_weak (ds, BE_IN_DATA, MAX_DEP_WEAK);
4476 if (ds & BEGIN_CONTROL)
4477 ds = set_dep_weak (ds, BEGIN_CONTROL, MAX_DEP_WEAK);
4478 if (ds & BE_IN_CONTROL)
4479 ds = set_dep_weak (ds, BE_IN_CONTROL, MAX_DEP_WEAK);
4481 return ds;
4484 /* Dump information about the dependence status S. */
4485 static void
4486 dump_ds (FILE *f, ds_t s)
4488 fprintf (f, "{");
4490 if (s & BEGIN_DATA)
4491 fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak_1 (s, BEGIN_DATA));
4492 if (s & BE_IN_DATA)
4493 fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak_1 (s, BE_IN_DATA));
4494 if (s & BEGIN_CONTROL)
4495 fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak_1 (s, BEGIN_CONTROL));
4496 if (s & BE_IN_CONTROL)
4497 fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak_1 (s, BE_IN_CONTROL));
4499 if (s & HARD_DEP)
4500 fprintf (f, "HARD_DEP; ");
4502 if (s & DEP_TRUE)
4503 fprintf (f, "DEP_TRUE; ");
4504 if (s & DEP_OUTPUT)
4505 fprintf (f, "DEP_OUTPUT; ");
4506 if (s & DEP_ANTI)
4507 fprintf (f, "DEP_ANTI; ");
4508 if (s & DEP_CONTROL)
4509 fprintf (f, "DEP_CONTROL; ");
4511 fprintf (f, "}");
4514 DEBUG_FUNCTION void
4515 debug_ds (ds_t s)
4517 dump_ds (stderr, s);
4518 fprintf (stderr, "\n");
4521 #ifdef ENABLE_CHECKING
4522 /* Verify that dependence type and status are consistent.
4523 If RELAXED_P is true, then skip dep_weakness checks. */
4524 static void
4525 check_dep (dep_t dep, bool relaxed_p)
4527 enum reg_note dt = DEP_TYPE (dep);
4528 ds_t ds = DEP_STATUS (dep);
4530 gcc_assert (DEP_PRO (dep) != DEP_CON (dep));
4532 if (!(current_sched_info->flags & USE_DEPS_LIST))
4534 gcc_assert (ds == 0);
4535 return;
4538 /* Check that dependence type contains the same bits as the status. */
4539 if (dt == REG_DEP_TRUE)
4540 gcc_assert (ds & DEP_TRUE);
4541 else if (dt == REG_DEP_OUTPUT)
4542 gcc_assert ((ds & DEP_OUTPUT)
4543 && !(ds & DEP_TRUE));
4544 else if (dt == REG_DEP_ANTI)
4545 gcc_assert ((ds & DEP_ANTI)
4546 && !(ds & (DEP_OUTPUT | DEP_TRUE)));
4547 else
4548 gcc_assert (dt == REG_DEP_CONTROL
4549 && (ds & DEP_CONTROL)
4550 && !(ds & (DEP_OUTPUT | DEP_ANTI | DEP_TRUE)));
4552 /* HARD_DEP can not appear in dep_status of a link. */
4553 gcc_assert (!(ds & HARD_DEP));
4555 /* Check that dependence status is set correctly when speculation is not
4556 supported. */
4557 if (!sched_deps_info->generate_spec_deps)
4558 gcc_assert (!(ds & SPECULATIVE));
4559 else if (ds & SPECULATIVE)
4561 if (!relaxed_p)
4563 ds_t type = FIRST_SPEC_TYPE;
4565 /* Check that dependence weakness is in proper range. */
4568 if (ds & type)
4569 get_dep_weak (ds, type);
4571 if (type == LAST_SPEC_TYPE)
4572 break;
4573 type <<= SPEC_TYPE_SHIFT;
4575 while (1);
4578 if (ds & BEGIN_SPEC)
4580 /* Only true dependence can be data speculative. */
4581 if (ds & BEGIN_DATA)
4582 gcc_assert (ds & DEP_TRUE);
4584 /* Control dependencies in the insn scheduler are represented by
4585 anti-dependencies, therefore only anti dependence can be
4586 control speculative. */
4587 if (ds & BEGIN_CONTROL)
4588 gcc_assert (ds & DEP_ANTI);
4590 else
4592 /* Subsequent speculations should resolve true dependencies. */
4593 gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
4596 /* Check that true and anti dependencies can't have other speculative
4597 statuses. */
4598 if (ds & DEP_TRUE)
4599 gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
4600 /* An output dependence can't be speculative at all. */
4601 gcc_assert (!(ds & DEP_OUTPUT));
4602 if (ds & DEP_ANTI)
4603 gcc_assert (ds & BEGIN_CONTROL);
4606 #endif /* ENABLE_CHECKING */
4608 /* The following code discovers opportunities to switch a memory reference
4609 and an increment by modifying the address. We ensure that this is done
4610 only for dependencies that are only used to show a single register
4611 dependence (using DEP_NONREG and DEP_MULTIPLE), and so that every memory
4612 instruction involved is subject to only one dep that can cause a pattern
4613 change.
4615 When we discover a suitable dependency, we fill in the dep_replacement
4616 structure to show how to modify the memory reference. */
4618 /* Holds information about a pair of memory reference and register increment
4619 insns which depend on each other, but could possibly be interchanged. */
4620 struct mem_inc_info
4622 rtx_insn *inc_insn;
4623 rtx_insn *mem_insn;
4625 rtx *mem_loc;
4626 /* A register occurring in the memory address for which we wish to break
4627 the dependence. This must be identical to the destination register of
4628 the increment. */
4629 rtx mem_reg0;
4630 /* Any kind of index that is added to that register. */
4631 rtx mem_index;
4632 /* The constant offset used in the memory address. */
4633 HOST_WIDE_INT mem_constant;
4634 /* The constant added in the increment insn. Negated if the increment is
4635 after the memory address. */
4636 HOST_WIDE_INT inc_constant;
4637 /* The source register used in the increment. May be different from mem_reg0
4638 if the increment occurs before the memory address. */
4639 rtx inc_input;
4642 /* Verify that the memory location described in MII can be replaced with
4643 one using NEW_ADDR. Return the new memory reference or NULL_RTX. The
4644 insn remains unchanged by this function. */
4646 static rtx
4647 attempt_change (struct mem_inc_info *mii, rtx new_addr)
4649 rtx mem = *mii->mem_loc;
4650 rtx new_mem;
4652 /* Jump through a lot of hoops to keep the attributes up to date. We
4653 do not want to call one of the change address variants that take
4654 an offset even though we know the offset in many cases. These
4655 assume you are changing where the address is pointing by the
4656 offset. */
4657 new_mem = replace_equiv_address_nv (mem, new_addr);
4658 if (! validate_change (mii->mem_insn, mii->mem_loc, new_mem, 0))
4660 if (sched_verbose >= 5)
4661 fprintf (sched_dump, "validation failure\n");
4662 return NULL_RTX;
4665 /* Put back the old one. */
4666 validate_change (mii->mem_insn, mii->mem_loc, mem, 0);
4668 return new_mem;
4671 /* Return true if INSN is of a form "a = b op c" where a and b are
4672 regs. op is + if c is a reg and +|- if c is a const. Fill in
4673 informantion in MII about what is found.
4674 BEFORE_MEM indicates whether the increment is found before or after
4675 a corresponding memory reference. */
4677 static bool
4678 parse_add_or_inc (struct mem_inc_info *mii, rtx_insn *insn, bool before_mem)
4680 rtx pat = single_set (insn);
4681 rtx src, cst;
4682 bool regs_equal;
4684 if (RTX_FRAME_RELATED_P (insn) || !pat)
4685 return false;
4687 /* Result must be single reg. */
4688 if (!REG_P (SET_DEST (pat)))
4689 return false;
4691 if (GET_CODE (SET_SRC (pat)) != PLUS)
4692 return false;
4694 mii->inc_insn = insn;
4695 src = SET_SRC (pat);
4696 mii->inc_input = XEXP (src, 0);
4698 if (!REG_P (XEXP (src, 0)))
4699 return false;
4701 if (!rtx_equal_p (SET_DEST (pat), mii->mem_reg0))
4702 return false;
4704 cst = XEXP (src, 1);
4705 if (!CONST_INT_P (cst))
4706 return false;
4707 mii->inc_constant = INTVAL (cst);
4709 regs_equal = rtx_equal_p (mii->inc_input, mii->mem_reg0);
4711 if (!before_mem)
4713 mii->inc_constant = -mii->inc_constant;
4714 if (!regs_equal)
4715 return false;
4718 if (regs_equal && REGNO (SET_DEST (pat)) == STACK_POINTER_REGNUM)
4720 /* Note that the sign has already been reversed for !before_mem. */
4721 if (STACK_GROWS_DOWNWARD)
4722 return mii->inc_constant > 0;
4723 else
4724 return mii->inc_constant < 0;
4726 return true;
4729 /* Once a suitable mem reference has been found and the corresponding data
4730 in MII has been filled in, this function is called to find a suitable
4731 add or inc insn involving the register we found in the memory
4732 reference. */
4734 static bool
4735 find_inc (struct mem_inc_info *mii, bool backwards)
4737 sd_iterator_def sd_it;
4738 dep_t dep;
4740 sd_it = sd_iterator_start (mii->mem_insn,
4741 backwards ? SD_LIST_HARD_BACK : SD_LIST_FORW);
4742 while (sd_iterator_cond (&sd_it, &dep))
4744 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
4745 rtx_insn *pro = DEP_PRO (dep);
4746 rtx_insn *con = DEP_CON (dep);
4747 rtx_insn *inc_cand = backwards ? pro : con;
4748 if (DEP_NONREG (dep) || DEP_MULTIPLE (dep))
4749 goto next;
4750 if (parse_add_or_inc (mii, inc_cand, backwards))
4752 struct dep_replacement *desc;
4753 df_ref def;
4754 rtx newaddr, newmem;
4756 if (sched_verbose >= 5)
4757 fprintf (sched_dump, "candidate mem/inc pair: %d %d\n",
4758 INSN_UID (mii->mem_insn), INSN_UID (inc_cand));
4760 /* Need to assure that none of the operands of the inc
4761 instruction are assigned to by the mem insn. */
4762 FOR_EACH_INSN_DEF (def, mii->mem_insn)
4763 if (reg_overlap_mentioned_p (DF_REF_REG (def), mii->inc_input)
4764 || reg_overlap_mentioned_p (DF_REF_REG (def), mii->mem_reg0))
4766 if (sched_verbose >= 5)
4767 fprintf (sched_dump,
4768 "inc conflicts with store failure.\n");
4769 goto next;
4772 newaddr = mii->inc_input;
4773 if (mii->mem_index != NULL_RTX)
4774 newaddr = gen_rtx_PLUS (GET_MODE (newaddr), newaddr,
4775 mii->mem_index);
4776 newaddr = plus_constant (GET_MODE (newaddr), newaddr,
4777 mii->mem_constant + mii->inc_constant);
4778 newmem = attempt_change (mii, newaddr);
4779 if (newmem == NULL_RTX)
4780 goto next;
4781 if (sched_verbose >= 5)
4782 fprintf (sched_dump, "successful address replacement\n");
4783 desc = XCNEW (struct dep_replacement);
4784 DEP_REPLACE (dep) = desc;
4785 desc->loc = mii->mem_loc;
4786 desc->newval = newmem;
4787 desc->orig = *desc->loc;
4788 desc->insn = mii->mem_insn;
4789 move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
4790 INSN_SPEC_BACK_DEPS (con));
4791 if (backwards)
4793 FOR_EACH_DEP (mii->inc_insn, SD_LIST_BACK, sd_it, dep)
4794 add_dependence_1 (mii->mem_insn, DEP_PRO (dep),
4795 REG_DEP_TRUE);
4797 else
4799 FOR_EACH_DEP (mii->inc_insn, SD_LIST_FORW, sd_it, dep)
4800 add_dependence_1 (DEP_CON (dep), mii->mem_insn,
4801 REG_DEP_ANTI);
4803 return true;
4805 next:
4806 sd_iterator_next (&sd_it);
4808 return false;
4811 /* A recursive function that walks ADDRESS_OF_X to find memory references
4812 which could be modified during scheduling. We call find_inc for each
4813 one we find that has a recognizable form. MII holds information about
4814 the pair of memory/increment instructions.
4815 We ensure that every instruction with a memory reference (which will be
4816 the location of the replacement) is assigned at most one breakable
4817 dependency. */
4819 static bool
4820 find_mem (struct mem_inc_info *mii, rtx *address_of_x)
4822 rtx x = *address_of_x;
4823 enum rtx_code code = GET_CODE (x);
4824 const char *const fmt = GET_RTX_FORMAT (code);
4825 int i;
4827 if (code == MEM)
4829 rtx reg0 = XEXP (x, 0);
4831 mii->mem_loc = address_of_x;
4832 mii->mem_index = NULL_RTX;
4833 mii->mem_constant = 0;
4834 if (GET_CODE (reg0) == PLUS && CONST_INT_P (XEXP (reg0, 1)))
4836 mii->mem_constant = INTVAL (XEXP (reg0, 1));
4837 reg0 = XEXP (reg0, 0);
4839 if (GET_CODE (reg0) == PLUS)
4841 mii->mem_index = XEXP (reg0, 1);
4842 reg0 = XEXP (reg0, 0);
4844 if (REG_P (reg0))
4846 df_ref use;
4847 int occurrences = 0;
4849 /* Make sure this reg appears only once in this insn. Can't use
4850 count_occurrences since that only works for pseudos. */
4851 FOR_EACH_INSN_USE (use, mii->mem_insn)
4852 if (reg_overlap_mentioned_p (reg0, DF_REF_REG (use)))
4853 if (++occurrences > 1)
4855 if (sched_verbose >= 5)
4856 fprintf (sched_dump, "mem count failure\n");
4857 return false;
4860 mii->mem_reg0 = reg0;
4861 return find_inc (mii, true) || find_inc (mii, false);
4863 return false;
4866 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4868 /* If REG occurs inside a MEM used in a bit-field reference,
4869 that is unacceptable. */
4870 return false;
4873 /* Time for some deep diving. */
4874 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4876 if (fmt[i] == 'e')
4878 if (find_mem (mii, &XEXP (x, i)))
4879 return true;
4881 else if (fmt[i] == 'E')
4883 int j;
4884 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4885 if (find_mem (mii, &XVECEXP (x, i, j)))
4886 return true;
4889 return false;
4893 /* Examine the instructions between HEAD and TAIL and try to find
4894 dependencies that can be broken by modifying one of the patterns. */
4896 void
4897 find_modifiable_mems (rtx_insn *head, rtx_insn *tail)
4899 rtx_insn *insn, *next_tail = NEXT_INSN (tail);
4900 int success_in_block = 0;
4902 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4904 struct mem_inc_info mii;
4906 if (!NONDEBUG_INSN_P (insn) || RTX_FRAME_RELATED_P (insn))
4907 continue;
4909 mii.mem_insn = insn;
4910 if (find_mem (&mii, &PATTERN (insn)))
4911 success_in_block++;
4913 if (success_in_block && sched_verbose >= 5)
4914 fprintf (sched_dump, "%d candidates for address modification found.\n",
4915 success_in_block);
4918 #endif /* INSN_SCHEDULING */