basic-block.h: Re-group most prototypes per file.
[gcc.git] / gcc / sched-deps.c
1 /* Instruction scheduling pass. This file computes dependencies between
2 instructions.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
5 2011, 2012
6 Free Software Foundation, Inc.
7 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
8 and currently maintained by, Jim Wilson (wilson@cygnus.com)
9
10 This file is part of GCC.
11
12 GCC is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free
14 Software Foundation; either version 3, or (at your option) any later
15 version.
16
17 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
25 \f
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "diagnostic-core.h"
31 #include "rtl.h"
32 #include "tree.h" /* FIXME: Used by call_may_noreturn_p. */
33 #include "tm_p.h"
34 #include "hard-reg-set.h"
35 #include "regs.h"
36 #include "function.h"
37 #include "flags.h"
38 #include "insn-config.h"
39 #include "insn-attr.h"
40 #include "except.h"
41 #include "recog.h"
42 #include "sched-int.h"
43 #include "params.h"
44 #include "cselib.h"
45 #include "ira.h"
46 #include "target.h"
47
48 #ifdef INSN_SCHEDULING
49
50 #ifdef ENABLE_CHECKING
51 #define CHECK (true)
52 #else
53 #define CHECK (false)
54 #endif
55
56 /* Holds current parameters for the dependency analyzer. */
57 struct sched_deps_info_def *sched_deps_info;
58
59 /* The data is specific to the Haifa scheduler. */
60 VEC(haifa_deps_insn_data_def, heap) *h_d_i_d = NULL;
61
62 /* Return the major type present in the DS. */
63 enum reg_note
64 ds_to_dk (ds_t ds)
65 {
66 if (ds & DEP_TRUE)
67 return REG_DEP_TRUE;
68
69 if (ds & DEP_OUTPUT)
70 return REG_DEP_OUTPUT;
71
72 if (ds & DEP_CONTROL)
73 return REG_DEP_CONTROL;
74
75 gcc_assert (ds & DEP_ANTI);
76
77 return REG_DEP_ANTI;
78 }
79
80 /* Return equivalent dep_status. */
81 ds_t
82 dk_to_ds (enum reg_note dk)
83 {
84 switch (dk)
85 {
86 case REG_DEP_TRUE:
87 return DEP_TRUE;
88
89 case REG_DEP_OUTPUT:
90 return DEP_OUTPUT;
91
92 case REG_DEP_CONTROL:
93 return DEP_CONTROL;
94
95 default:
96 gcc_assert (dk == REG_DEP_ANTI);
97 return DEP_ANTI;
98 }
99 }
100
101 /* Functions to operate with dependence information container - dep_t. */
102
103 /* Init DEP with the arguments. */
104 void
105 init_dep_1 (dep_t dep, rtx pro, rtx con, enum reg_note type, ds_t ds)
106 {
107 DEP_PRO (dep) = pro;
108 DEP_CON (dep) = con;
109 DEP_TYPE (dep) = type;
110 DEP_STATUS (dep) = ds;
111 DEP_COST (dep) = UNKNOWN_DEP_COST;
112 }
113
114 /* Init DEP with the arguments.
115 While most of the scheduler (including targets) only need the major type
116 of the dependency, it is convenient to hide full dep_status from them. */
117 void
118 init_dep (dep_t dep, rtx pro, rtx con, enum reg_note kind)
119 {
120 ds_t ds;
121
122 if ((current_sched_info->flags & USE_DEPS_LIST))
123 ds = dk_to_ds (kind);
124 else
125 ds = 0;
126
127 init_dep_1 (dep, pro, con, kind, ds);
128 }
129
130 /* Make a copy of FROM in TO. */
131 static void
132 copy_dep (dep_t to, dep_t from)
133 {
134 memcpy (to, from, sizeof (*to));
135 }
136
137 static void dump_ds (FILE *, ds_t);
138
139 /* Define flags for dump_dep (). */
140
141 /* Dump producer of the dependence. */
142 #define DUMP_DEP_PRO (2)
143
144 /* Dump consumer of the dependence. */
145 #define DUMP_DEP_CON (4)
146
147 /* Dump type of the dependence. */
148 #define DUMP_DEP_TYPE (8)
149
150 /* Dump status of the dependence. */
151 #define DUMP_DEP_STATUS (16)
152
153 /* Dump all information about the dependence. */
154 #define DUMP_DEP_ALL (DUMP_DEP_PRO | DUMP_DEP_CON | DUMP_DEP_TYPE \
155 |DUMP_DEP_STATUS)
156
157 /* Dump DEP to DUMP.
158 FLAGS is a bit mask specifying what information about DEP needs
159 to be printed.
160 If FLAGS has the very first bit set, then dump all information about DEP
161 and propagate this bit into the callee dump functions. */
162 static void
163 dump_dep (FILE *dump, dep_t dep, int flags)
164 {
165 if (flags & 1)
166 flags |= DUMP_DEP_ALL;
167
168 fprintf (dump, "<");
169
170 if (flags & DUMP_DEP_PRO)
171 fprintf (dump, "%d; ", INSN_UID (DEP_PRO (dep)));
172
173 if (flags & DUMP_DEP_CON)
174 fprintf (dump, "%d; ", INSN_UID (DEP_CON (dep)));
175
176 if (flags & DUMP_DEP_TYPE)
177 {
178 char t;
179 enum reg_note type = DEP_TYPE (dep);
180
181 switch (type)
182 {
183 case REG_DEP_TRUE:
184 t = 't';
185 break;
186
187 case REG_DEP_OUTPUT:
188 t = 'o';
189 break;
190
191 case REG_DEP_CONTROL:
192 t = 'c';
193 break;
194
195 case REG_DEP_ANTI:
196 t = 'a';
197 break;
198
199 default:
200 gcc_unreachable ();
201 break;
202 }
203
204 fprintf (dump, "%c; ", t);
205 }
206
207 if (flags & DUMP_DEP_STATUS)
208 {
209 if (current_sched_info->flags & USE_DEPS_LIST)
210 dump_ds (dump, DEP_STATUS (dep));
211 }
212
213 fprintf (dump, ">");
214 }
215
216 /* Default flags for dump_dep (). */
217 static int dump_dep_flags = (DUMP_DEP_PRO | DUMP_DEP_CON);
218
219 /* Dump all fields of DEP to STDERR. */
220 void
221 sd_debug_dep (dep_t dep)
222 {
223 dump_dep (stderr, dep, 1);
224 fprintf (stderr, "\n");
225 }
226
227 /* Determine whether DEP is a dependency link of a non-debug insn on a
228 debug insn. */
229
230 static inline bool
231 depl_on_debug_p (dep_link_t dep)
232 {
233 return (DEBUG_INSN_P (DEP_LINK_PRO (dep))
234 && !DEBUG_INSN_P (DEP_LINK_CON (dep)));
235 }
236
237 /* Functions to operate with a single link from the dependencies lists -
238 dep_link_t. */
239
240 /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
241 PREV_NEXT_P. */
242 static void
243 attach_dep_link (dep_link_t l, dep_link_t *prev_nextp)
244 {
245 dep_link_t next = *prev_nextp;
246
247 gcc_assert (DEP_LINK_PREV_NEXTP (l) == NULL
248 && DEP_LINK_NEXT (l) == NULL);
249
250 /* Init node being inserted. */
251 DEP_LINK_PREV_NEXTP (l) = prev_nextp;
252 DEP_LINK_NEXT (l) = next;
253
254 /* Fix next node. */
255 if (next != NULL)
256 {
257 gcc_assert (DEP_LINK_PREV_NEXTP (next) == prev_nextp);
258
259 DEP_LINK_PREV_NEXTP (next) = &DEP_LINK_NEXT (l);
260 }
261
262 /* Fix prev node. */
263 *prev_nextp = l;
264 }
265
266 /* Add dep_link LINK to deps_list L. */
267 static void
268 add_to_deps_list (dep_link_t link, deps_list_t l)
269 {
270 attach_dep_link (link, &DEPS_LIST_FIRST (l));
271
272 /* Don't count debug deps. */
273 if (!depl_on_debug_p (link))
274 ++DEPS_LIST_N_LINKS (l);
275 }
276
277 /* Detach dep_link L from the list. */
278 static void
279 detach_dep_link (dep_link_t l)
280 {
281 dep_link_t *prev_nextp = DEP_LINK_PREV_NEXTP (l);
282 dep_link_t next = DEP_LINK_NEXT (l);
283
284 *prev_nextp = next;
285
286 if (next != NULL)
287 DEP_LINK_PREV_NEXTP (next) = prev_nextp;
288
289 DEP_LINK_PREV_NEXTP (l) = NULL;
290 DEP_LINK_NEXT (l) = NULL;
291 }
292
293 /* Remove link LINK from list LIST. */
294 static void
295 remove_from_deps_list (dep_link_t link, deps_list_t list)
296 {
297 detach_dep_link (link);
298
299 /* Don't count debug deps. */
300 if (!depl_on_debug_p (link))
301 --DEPS_LIST_N_LINKS (list);
302 }
303
304 /* Move link LINK from list FROM to list TO. */
305 static void
306 move_dep_link (dep_link_t link, deps_list_t from, deps_list_t to)
307 {
308 remove_from_deps_list (link, from);
309 add_to_deps_list (link, to);
310 }
311
312 /* Return true of LINK is not attached to any list. */
313 static bool
314 dep_link_is_detached_p (dep_link_t link)
315 {
316 return DEP_LINK_PREV_NEXTP (link) == NULL;
317 }
318
319 /* Pool to hold all dependency nodes (dep_node_t). */
320 static alloc_pool dn_pool;
321
322 /* Number of dep_nodes out there. */
323 static int dn_pool_diff = 0;
324
325 /* Create a dep_node. */
326 static dep_node_t
327 create_dep_node (void)
328 {
329 dep_node_t n = (dep_node_t) pool_alloc (dn_pool);
330 dep_link_t back = DEP_NODE_BACK (n);
331 dep_link_t forw = DEP_NODE_FORW (n);
332
333 DEP_LINK_NODE (back) = n;
334 DEP_LINK_NEXT (back) = NULL;
335 DEP_LINK_PREV_NEXTP (back) = NULL;
336
337 DEP_LINK_NODE (forw) = n;
338 DEP_LINK_NEXT (forw) = NULL;
339 DEP_LINK_PREV_NEXTP (forw) = NULL;
340
341 ++dn_pool_diff;
342
343 return n;
344 }
345
346 /* Delete dep_node N. N must not be connected to any deps_list. */
347 static void
348 delete_dep_node (dep_node_t n)
349 {
350 gcc_assert (dep_link_is_detached_p (DEP_NODE_BACK (n))
351 && dep_link_is_detached_p (DEP_NODE_FORW (n)));
352
353 --dn_pool_diff;
354
355 pool_free (dn_pool, n);
356 }
357
358 /* Pool to hold dependencies lists (deps_list_t). */
359 static alloc_pool dl_pool;
360
361 /* Number of deps_lists out there. */
362 static int dl_pool_diff = 0;
363
364 /* Functions to operate with dependences lists - deps_list_t. */
365
366 /* Return true if list L is empty. */
367 static bool
368 deps_list_empty_p (deps_list_t l)
369 {
370 return DEPS_LIST_N_LINKS (l) == 0;
371 }
372
373 /* Create a new deps_list. */
374 static deps_list_t
375 create_deps_list (void)
376 {
377 deps_list_t l = (deps_list_t) pool_alloc (dl_pool);
378
379 DEPS_LIST_FIRST (l) = NULL;
380 DEPS_LIST_N_LINKS (l) = 0;
381
382 ++dl_pool_diff;
383 return l;
384 }
385
386 /* Free deps_list L. */
387 static void
388 free_deps_list (deps_list_t l)
389 {
390 gcc_assert (deps_list_empty_p (l));
391
392 --dl_pool_diff;
393
394 pool_free (dl_pool, l);
395 }
396
397 /* Return true if there is no dep_nodes and deps_lists out there.
398 After the region is scheduled all the dependency nodes and lists
399 should [generally] be returned to pool. */
400 bool
401 deps_pools_are_empty_p (void)
402 {
403 return dn_pool_diff == 0 && dl_pool_diff == 0;
404 }
405
406 /* Remove all elements from L. */
407 static void
408 clear_deps_list (deps_list_t l)
409 {
410 do
411 {
412 dep_link_t link = DEPS_LIST_FIRST (l);
413
414 if (link == NULL)
415 break;
416
417 remove_from_deps_list (link, l);
418 }
419 while (1);
420 }
421
422 /* Decide whether a dependency should be treated as a hard or a speculative
423 dependency. */
424 static bool
425 dep_spec_p (dep_t dep)
426 {
427 if (current_sched_info->flags & DO_SPECULATION)
428 {
429 if (DEP_STATUS (dep) & SPECULATIVE)
430 return true;
431 }
432 if (current_sched_info->flags & DO_PREDICATION)
433 {
434 if (DEP_TYPE (dep) == REG_DEP_CONTROL)
435 return true;
436 }
437 return false;
438 }
439
440 static regset reg_pending_sets;
441 static regset reg_pending_clobbers;
442 static regset reg_pending_uses;
443 static regset reg_pending_control_uses;
444 static enum reg_pending_barrier_mode reg_pending_barrier;
445
446 /* Hard registers implicitly clobbered or used (or may be implicitly
447 clobbered or used) by the currently analyzed insn. For example,
448 insn in its constraint has one register class. Even if there is
449 currently no hard register in the insn, the particular hard
450 register will be in the insn after reload pass because the
451 constraint requires it. */
452 static HARD_REG_SET implicit_reg_pending_clobbers;
453 static HARD_REG_SET implicit_reg_pending_uses;
454
455 /* To speed up the test for duplicate dependency links we keep a
456 record of dependencies created by add_dependence when the average
457 number of instructions in a basic block is very large.
458
459 Studies have shown that there is typically around 5 instructions between
460 branches for typical C code. So we can make a guess that the average
461 basic block is approximately 5 instructions long; we will choose 100X
462 the average size as a very large basic block.
463
464 Each insn has associated bitmaps for its dependencies. Each bitmap
465 has enough entries to represent a dependency on any other insn in
466 the insn chain. All bitmap for true dependencies cache is
467 allocated then the rest two ones are also allocated. */
468 static bitmap_head *true_dependency_cache = NULL;
469 static bitmap_head *output_dependency_cache = NULL;
470 static bitmap_head *anti_dependency_cache = NULL;
471 static bitmap_head *control_dependency_cache = NULL;
472 static bitmap_head *spec_dependency_cache = NULL;
473 static int cache_size;
474
475 static int deps_may_trap_p (const_rtx);
476 static void add_dependence_1 (rtx, rtx, enum reg_note);
477 static void add_dependence_list (rtx, rtx, int, enum reg_note);
478 static void add_dependence_list_and_free (struct deps_desc *, rtx,
479 rtx *, int, enum reg_note);
480 static void delete_all_dependences (rtx);
481 static void chain_to_prev_insn (rtx);
482
483 static void flush_pending_lists (struct deps_desc *, rtx, int, int);
484 static void sched_analyze_1 (struct deps_desc *, rtx, rtx);
485 static void sched_analyze_2 (struct deps_desc *, rtx, rtx);
486 static void sched_analyze_insn (struct deps_desc *, rtx, rtx);
487
488 static bool sched_has_condition_p (const_rtx);
489 static int conditions_mutex_p (const_rtx, const_rtx, bool, bool);
490
491 static enum DEPS_ADJUST_RESULT maybe_add_or_update_dep_1 (dep_t, bool,
492 rtx, rtx);
493 static enum DEPS_ADJUST_RESULT add_or_update_dep_1 (dep_t, bool, rtx, rtx);
494
495 #ifdef ENABLE_CHECKING
496 static void check_dep (dep_t, bool);
497 #endif
498 \f
499 /* Return nonzero if a load of the memory reference MEM can cause a trap. */
500
501 static int
502 deps_may_trap_p (const_rtx mem)
503 {
504 const_rtx addr = XEXP (mem, 0);
505
506 if (REG_P (addr) && REGNO (addr) >= FIRST_PSEUDO_REGISTER)
507 {
508 const_rtx t = get_reg_known_value (REGNO (addr));
509 if (t)
510 addr = t;
511 }
512 return rtx_addr_can_trap_p (addr);
513 }
514 \f
515
516 /* Find the condition under which INSN is executed. If REV is not NULL,
517 it is set to TRUE when the returned comparison should be reversed
518 to get the actual condition. */
519 static rtx
520 sched_get_condition_with_rev_uncached (const_rtx insn, bool *rev)
521 {
522 rtx pat = PATTERN (insn);
523 rtx src;
524
525 if (rev)
526 *rev = false;
527
528 if (GET_CODE (pat) == COND_EXEC)
529 return COND_EXEC_TEST (pat);
530
531 if (!any_condjump_p (insn) || !onlyjump_p (insn))
532 return 0;
533
534 src = SET_SRC (pc_set (insn));
535
536 if (XEXP (src, 2) == pc_rtx)
537 return XEXP (src, 0);
538 else if (XEXP (src, 1) == pc_rtx)
539 {
540 rtx cond = XEXP (src, 0);
541 enum rtx_code revcode = reversed_comparison_code (cond, insn);
542
543 if (revcode == UNKNOWN)
544 return 0;
545
546 if (rev)
547 *rev = true;
548 return cond;
549 }
550
551 return 0;
552 }
553
554 /* Return the condition under which INSN does not execute (i.e. the
555 not-taken condition for a conditional branch), or NULL if we cannot
556 find such a condition. The caller should make a copy of the condition
557 before using it. */
558 rtx
559 sched_get_reverse_condition_uncached (const_rtx insn)
560 {
561 bool rev;
562 rtx cond = sched_get_condition_with_rev_uncached (insn, &rev);
563 if (cond == NULL_RTX)
564 return cond;
565 if (!rev)
566 {
567 enum rtx_code revcode = reversed_comparison_code (cond, insn);
568 cond = gen_rtx_fmt_ee (revcode, GET_MODE (cond),
569 XEXP (cond, 0),
570 XEXP (cond, 1));
571 }
572 return cond;
573 }
574
575 /* Caching variant of sched_get_condition_with_rev_uncached.
576 We only do actual work the first time we come here for an insn; the
577 results are cached in INSN_CACHED_COND and INSN_REVERSE_COND. */
578 static rtx
579 sched_get_condition_with_rev (const_rtx insn, bool *rev)
580 {
581 bool tmp;
582
583 if (INSN_LUID (insn) == 0)
584 return sched_get_condition_with_rev_uncached (insn, rev);
585
586 if (INSN_CACHED_COND (insn) == const_true_rtx)
587 return NULL_RTX;
588
589 if (INSN_CACHED_COND (insn) != NULL_RTX)
590 {
591 if (rev)
592 *rev = INSN_REVERSE_COND (insn);
593 return INSN_CACHED_COND (insn);
594 }
595
596 INSN_CACHED_COND (insn) = sched_get_condition_with_rev_uncached (insn, &tmp);
597 INSN_REVERSE_COND (insn) = tmp;
598
599 if (INSN_CACHED_COND (insn) == NULL_RTX)
600 {
601 INSN_CACHED_COND (insn) = const_true_rtx;
602 return NULL_RTX;
603 }
604
605 if (rev)
606 *rev = INSN_REVERSE_COND (insn);
607 return INSN_CACHED_COND (insn);
608 }
609
610 /* True when we can find a condition under which INSN is executed. */
611 static bool
612 sched_has_condition_p (const_rtx insn)
613 {
614 return !! sched_get_condition_with_rev (insn, NULL);
615 }
616
617 \f
618
619 /* Return nonzero if conditions COND1 and COND2 can never be both true. */
620 static int
621 conditions_mutex_p (const_rtx cond1, const_rtx cond2, bool rev1, bool rev2)
622 {
623 if (COMPARISON_P (cond1)
624 && COMPARISON_P (cond2)
625 && GET_CODE (cond1) ==
626 (rev1==rev2
627 ? reversed_comparison_code (cond2, NULL)
628 : GET_CODE (cond2))
629 && rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
630 && XEXP (cond1, 1) == XEXP (cond2, 1))
631 return 1;
632 return 0;
633 }
634
635 /* Return true if insn1 and insn2 can never depend on one another because
636 the conditions under which they are executed are mutually exclusive. */
637 bool
638 sched_insns_conditions_mutex_p (const_rtx insn1, const_rtx insn2)
639 {
640 rtx cond1, cond2;
641 bool rev1 = false, rev2 = false;
642
643 /* df doesn't handle conditional lifetimes entirely correctly;
644 calls mess up the conditional lifetimes. */
645 if (!CALL_P (insn1) && !CALL_P (insn2))
646 {
647 cond1 = sched_get_condition_with_rev (insn1, &rev1);
648 cond2 = sched_get_condition_with_rev (insn2, &rev2);
649 if (cond1 && cond2
650 && conditions_mutex_p (cond1, cond2, rev1, rev2)
651 /* Make sure first instruction doesn't affect condition of second
652 instruction if switched. */
653 && !modified_in_p (cond1, insn2)
654 /* Make sure second instruction doesn't affect condition of first
655 instruction if switched. */
656 && !modified_in_p (cond2, insn1))
657 return true;
658 }
659 return false;
660 }
661 \f
662
663 /* Return true if INSN can potentially be speculated with type DS. */
664 bool
665 sched_insn_is_legitimate_for_speculation_p (const_rtx insn, ds_t ds)
666 {
667 if (HAS_INTERNAL_DEP (insn))
668 return false;
669
670 if (!NONJUMP_INSN_P (insn))
671 return false;
672
673 if (SCHED_GROUP_P (insn))
674 return false;
675
676 if (IS_SPECULATION_CHECK_P (CONST_CAST_RTX (insn)))
677 return false;
678
679 if (side_effects_p (PATTERN (insn)))
680 return false;
681
682 if (ds & BE_IN_SPEC)
683 /* The following instructions, which depend on a speculatively scheduled
684 instruction, cannot be speculatively scheduled along. */
685 {
686 if (may_trap_or_fault_p (PATTERN (insn)))
687 /* If instruction might fault, it cannot be speculatively scheduled.
688 For control speculation it's obvious why and for data speculation
689 it's because the insn might get wrong input if speculation
690 wasn't successful. */
691 return false;
692
693 if ((ds & BE_IN_DATA)
694 && sched_has_condition_p (insn))
695 /* If this is a predicated instruction, then it cannot be
696 speculatively scheduled. See PR35659. */
697 return false;
698 }
699
700 return true;
701 }
702
703 /* Initialize LIST_PTR to point to one of the lists present in TYPES_PTR,
704 initialize RESOLVED_P_PTR with true if that list consists of resolved deps,
705 and remove the type of returned [through LIST_PTR] list from TYPES_PTR.
706 This function is used to switch sd_iterator to the next list.
707 !!! For internal use only. Might consider moving it to sched-int.h. */
708 void
709 sd_next_list (const_rtx insn, sd_list_types_def *types_ptr,
710 deps_list_t *list_ptr, bool *resolved_p_ptr)
711 {
712 sd_list_types_def types = *types_ptr;
713
714 if (types & SD_LIST_HARD_BACK)
715 {
716 *list_ptr = INSN_HARD_BACK_DEPS (insn);
717 *resolved_p_ptr = false;
718 *types_ptr = types & ~SD_LIST_HARD_BACK;
719 }
720 else if (types & SD_LIST_SPEC_BACK)
721 {
722 *list_ptr = INSN_SPEC_BACK_DEPS (insn);
723 *resolved_p_ptr = false;
724 *types_ptr = types & ~SD_LIST_SPEC_BACK;
725 }
726 else if (types & SD_LIST_FORW)
727 {
728 *list_ptr = INSN_FORW_DEPS (insn);
729 *resolved_p_ptr = false;
730 *types_ptr = types & ~SD_LIST_FORW;
731 }
732 else if (types & SD_LIST_RES_BACK)
733 {
734 *list_ptr = INSN_RESOLVED_BACK_DEPS (insn);
735 *resolved_p_ptr = true;
736 *types_ptr = types & ~SD_LIST_RES_BACK;
737 }
738 else if (types & SD_LIST_RES_FORW)
739 {
740 *list_ptr = INSN_RESOLVED_FORW_DEPS (insn);
741 *resolved_p_ptr = true;
742 *types_ptr = types & ~SD_LIST_RES_FORW;
743 }
744 else
745 {
746 *list_ptr = NULL;
747 *resolved_p_ptr = false;
748 *types_ptr = SD_LIST_NONE;
749 }
750 }
751
752 /* Return the summary size of INSN's lists defined by LIST_TYPES. */
753 int
754 sd_lists_size (const_rtx insn, sd_list_types_def list_types)
755 {
756 int size = 0;
757
758 while (list_types != SD_LIST_NONE)
759 {
760 deps_list_t list;
761 bool resolved_p;
762
763 sd_next_list (insn, &list_types, &list, &resolved_p);
764 if (list)
765 size += DEPS_LIST_N_LINKS (list);
766 }
767
768 return size;
769 }
770
771 /* Return true if INSN's lists defined by LIST_TYPES are all empty. */
772
773 bool
774 sd_lists_empty_p (const_rtx insn, sd_list_types_def list_types)
775 {
776 while (list_types != SD_LIST_NONE)
777 {
778 deps_list_t list;
779 bool resolved_p;
780
781 sd_next_list (insn, &list_types, &list, &resolved_p);
782 if (!deps_list_empty_p (list))
783 return false;
784 }
785
786 return true;
787 }
788
789 /* Initialize data for INSN. */
790 void
791 sd_init_insn (rtx insn)
792 {
793 INSN_HARD_BACK_DEPS (insn) = create_deps_list ();
794 INSN_SPEC_BACK_DEPS (insn) = create_deps_list ();
795 INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list ();
796 INSN_FORW_DEPS (insn) = create_deps_list ();
797 INSN_RESOLVED_FORW_DEPS (insn) = create_deps_list ();
798
799 /* ??? It would be nice to allocate dependency caches here. */
800 }
801
802 /* Free data for INSN. */
803 void
804 sd_finish_insn (rtx insn)
805 {
806 /* ??? It would be nice to deallocate dependency caches here. */
807
808 free_deps_list (INSN_HARD_BACK_DEPS (insn));
809 INSN_HARD_BACK_DEPS (insn) = NULL;
810
811 free_deps_list (INSN_SPEC_BACK_DEPS (insn));
812 INSN_SPEC_BACK_DEPS (insn) = NULL;
813
814 free_deps_list (INSN_RESOLVED_BACK_DEPS (insn));
815 INSN_RESOLVED_BACK_DEPS (insn) = NULL;
816
817 free_deps_list (INSN_FORW_DEPS (insn));
818 INSN_FORW_DEPS (insn) = NULL;
819
820 free_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
821 INSN_RESOLVED_FORW_DEPS (insn) = NULL;
822 }
823
824 /* Find a dependency between producer PRO and consumer CON.
825 Search through resolved dependency lists if RESOLVED_P is true.
826 If no such dependency is found return NULL,
827 otherwise return the dependency and initialize SD_IT_PTR [if it is nonnull]
828 with an iterator pointing to it. */
829 static dep_t
830 sd_find_dep_between_no_cache (rtx pro, rtx con, bool resolved_p,
831 sd_iterator_def *sd_it_ptr)
832 {
833 sd_list_types_def pro_list_type;
834 sd_list_types_def con_list_type;
835 sd_iterator_def sd_it;
836 dep_t dep;
837 bool found_p = false;
838
839 if (resolved_p)
840 {
841 pro_list_type = SD_LIST_RES_FORW;
842 con_list_type = SD_LIST_RES_BACK;
843 }
844 else
845 {
846 pro_list_type = SD_LIST_FORW;
847 con_list_type = SD_LIST_BACK;
848 }
849
850 /* Walk through either back list of INSN or forw list of ELEM
851 depending on which one is shorter. */
852 if (sd_lists_size (con, con_list_type) < sd_lists_size (pro, pro_list_type))
853 {
854 /* Find the dep_link with producer PRO in consumer's back_deps. */
855 FOR_EACH_DEP (con, con_list_type, sd_it, dep)
856 if (DEP_PRO (dep) == pro)
857 {
858 found_p = true;
859 break;
860 }
861 }
862 else
863 {
864 /* Find the dep_link with consumer CON in producer's forw_deps. */
865 FOR_EACH_DEP (pro, pro_list_type, sd_it, dep)
866 if (DEP_CON (dep) == con)
867 {
868 found_p = true;
869 break;
870 }
871 }
872
873 if (found_p)
874 {
875 if (sd_it_ptr != NULL)
876 *sd_it_ptr = sd_it;
877
878 return dep;
879 }
880
881 return NULL;
882 }
883
884 /* Find a dependency between producer PRO and consumer CON.
885 Use dependency [if available] to check if dependency is present at all.
886 Search through resolved dependency lists if RESOLVED_P is true.
887 If the dependency or NULL if none found. */
888 dep_t
889 sd_find_dep_between (rtx pro, rtx con, bool resolved_p)
890 {
891 if (true_dependency_cache != NULL)
892 /* Avoiding the list walk below can cut compile times dramatically
893 for some code. */
894 {
895 int elem_luid = INSN_LUID (pro);
896 int insn_luid = INSN_LUID (con);
897
898 if (!bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid)
899 && !bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid)
900 && !bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid)
901 && !bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
902 return NULL;
903 }
904
905 return sd_find_dep_between_no_cache (pro, con, resolved_p, NULL);
906 }
907
908 /* Add or update a dependence described by DEP.
909 MEM1 and MEM2, if non-null, correspond to memory locations in case of
910 data speculation.
911
912 The function returns a value indicating if an old entry has been changed
913 or a new entry has been added to insn's backward deps.
914
915 This function merely checks if producer and consumer is the same insn
916 and doesn't create a dep in this case. Actual manipulation of
917 dependence data structures is performed in add_or_update_dep_1. */
918 static enum DEPS_ADJUST_RESULT
919 maybe_add_or_update_dep_1 (dep_t dep, bool resolved_p, rtx mem1, rtx mem2)
920 {
921 rtx elem = DEP_PRO (dep);
922 rtx insn = DEP_CON (dep);
923
924 gcc_assert (INSN_P (insn) && INSN_P (elem));
925
926 /* Don't depend an insn on itself. */
927 if (insn == elem)
928 {
929 if (sched_deps_info->generate_spec_deps)
930 /* INSN has an internal dependence, which we can't overcome. */
931 HAS_INTERNAL_DEP (insn) = 1;
932
933 return DEP_NODEP;
934 }
935
936 return add_or_update_dep_1 (dep, resolved_p, mem1, mem2);
937 }
938
939 /* Ask dependency caches what needs to be done for dependence DEP.
940 Return DEP_CREATED if new dependence should be created and there is no
941 need to try to find one searching the dependencies lists.
942 Return DEP_PRESENT if there already is a dependence described by DEP and
943 hence nothing is to be done.
944 Return DEP_CHANGED if there already is a dependence, but it should be
945 updated to incorporate additional information from DEP. */
946 static enum DEPS_ADJUST_RESULT
947 ask_dependency_caches (dep_t dep)
948 {
949 int elem_luid = INSN_LUID (DEP_PRO (dep));
950 int insn_luid = INSN_LUID (DEP_CON (dep));
951
952 gcc_assert (true_dependency_cache != NULL
953 && output_dependency_cache != NULL
954 && anti_dependency_cache != NULL
955 && control_dependency_cache != NULL);
956
957 if (!(current_sched_info->flags & USE_DEPS_LIST))
958 {
959 enum reg_note present_dep_type;
960
961 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
962 present_dep_type = REG_DEP_TRUE;
963 else if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
964 present_dep_type = REG_DEP_OUTPUT;
965 else if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
966 present_dep_type = REG_DEP_ANTI;
967 else if (bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
968 present_dep_type = REG_DEP_CONTROL;
969 else
970 /* There is no existing dep so it should be created. */
971 return DEP_CREATED;
972
973 if ((int) DEP_TYPE (dep) >= (int) present_dep_type)
974 /* DEP does not add anything to the existing dependence. */
975 return DEP_PRESENT;
976 }
977 else
978 {
979 ds_t present_dep_types = 0;
980
981 if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
982 present_dep_types |= DEP_TRUE;
983 if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
984 present_dep_types |= DEP_OUTPUT;
985 if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
986 present_dep_types |= DEP_ANTI;
987 if (bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
988 present_dep_types |= DEP_CONTROL;
989
990 if (present_dep_types == 0)
991 /* There is no existing dep so it should be created. */
992 return DEP_CREATED;
993
994 if (!(current_sched_info->flags & DO_SPECULATION)
995 || !bitmap_bit_p (&spec_dependency_cache[insn_luid], elem_luid))
996 {
997 if ((present_dep_types | (DEP_STATUS (dep) & DEP_TYPES))
998 == present_dep_types)
999 /* DEP does not add anything to the existing dependence. */
1000 return DEP_PRESENT;
1001 }
1002 else
1003 {
1004 /* Only true dependencies can be data speculative and
1005 only anti dependencies can be control speculative. */
1006 gcc_assert ((present_dep_types & (DEP_TRUE | DEP_ANTI))
1007 == present_dep_types);
1008
1009 /* if (DEP is SPECULATIVE) then
1010 ..we should update DEP_STATUS
1011 else
1012 ..we should reset existing dep to non-speculative. */
1013 }
1014 }
1015
1016 return DEP_CHANGED;
1017 }
1018
1019 /* Set dependency caches according to DEP. */
1020 static void
1021 set_dependency_caches (dep_t dep)
1022 {
1023 int elem_luid = INSN_LUID (DEP_PRO (dep));
1024 int insn_luid = INSN_LUID (DEP_CON (dep));
1025
1026 if (!(current_sched_info->flags & USE_DEPS_LIST))
1027 {
1028 switch (DEP_TYPE (dep))
1029 {
1030 case REG_DEP_TRUE:
1031 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
1032 break;
1033
1034 case REG_DEP_OUTPUT:
1035 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
1036 break;
1037
1038 case REG_DEP_ANTI:
1039 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
1040 break;
1041
1042 case REG_DEP_CONTROL:
1043 bitmap_set_bit (&control_dependency_cache[insn_luid], elem_luid);
1044 break;
1045
1046 default:
1047 gcc_unreachable ();
1048 }
1049 }
1050 else
1051 {
1052 ds_t ds = DEP_STATUS (dep);
1053
1054 if (ds & DEP_TRUE)
1055 bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
1056 if (ds & DEP_OUTPUT)
1057 bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
1058 if (ds & DEP_ANTI)
1059 bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
1060 if (ds & DEP_CONTROL)
1061 bitmap_set_bit (&control_dependency_cache[insn_luid], elem_luid);
1062
1063 if (ds & SPECULATIVE)
1064 {
1065 gcc_assert (current_sched_info->flags & DO_SPECULATION);
1066 bitmap_set_bit (&spec_dependency_cache[insn_luid], elem_luid);
1067 }
1068 }
1069 }
1070
1071 /* Type of dependence DEP have changed from OLD_TYPE. Update dependency
1072 caches accordingly. */
1073 static void
1074 update_dependency_caches (dep_t dep, enum reg_note old_type)
1075 {
1076 int elem_luid = INSN_LUID (DEP_PRO (dep));
1077 int insn_luid = INSN_LUID (DEP_CON (dep));
1078
1079 /* Clear corresponding cache entry because type of the link
1080 may have changed. Keep them if we use_deps_list. */
1081 if (!(current_sched_info->flags & USE_DEPS_LIST))
1082 {
1083 switch (old_type)
1084 {
1085 case REG_DEP_OUTPUT:
1086 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1087 break;
1088
1089 case REG_DEP_ANTI:
1090 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1091 break;
1092
1093 case REG_DEP_CONTROL:
1094 bitmap_clear_bit (&control_dependency_cache[insn_luid], elem_luid);
1095 break;
1096
1097 default:
1098 gcc_unreachable ();
1099 }
1100 }
1101
1102 set_dependency_caches (dep);
1103 }
1104
1105 /* Convert a dependence pointed to by SD_IT to be non-speculative. */
1106 static void
1107 change_spec_dep_to_hard (sd_iterator_def sd_it)
1108 {
1109 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1110 dep_link_t link = DEP_NODE_BACK (node);
1111 dep_t dep = DEP_NODE_DEP (node);
1112 rtx elem = DEP_PRO (dep);
1113 rtx insn = DEP_CON (dep);
1114
1115 move_dep_link (link, INSN_SPEC_BACK_DEPS (insn), INSN_HARD_BACK_DEPS (insn));
1116
1117 DEP_STATUS (dep) &= ~SPECULATIVE;
1118
1119 if (true_dependency_cache != NULL)
1120 /* Clear the cache entry. */
1121 bitmap_clear_bit (&spec_dependency_cache[INSN_LUID (insn)],
1122 INSN_LUID (elem));
1123 }
1124
1125 /* Update DEP to incorporate information from NEW_DEP.
1126 SD_IT points to DEP in case it should be moved to another list.
1127 MEM1 and MEM2, if nonnull, correspond to memory locations in case if
1128 data-speculative dependence should be updated. */
1129 static enum DEPS_ADJUST_RESULT
1130 update_dep (dep_t dep, dep_t new_dep,
1131 sd_iterator_def sd_it ATTRIBUTE_UNUSED,
1132 rtx mem1 ATTRIBUTE_UNUSED,
1133 rtx mem2 ATTRIBUTE_UNUSED)
1134 {
1135 enum DEPS_ADJUST_RESULT res = DEP_PRESENT;
1136 enum reg_note old_type = DEP_TYPE (dep);
1137 bool was_spec = dep_spec_p (dep);
1138
1139 /* If this is a more restrictive type of dependence than the
1140 existing one, then change the existing dependence to this
1141 type. */
1142 if ((int) DEP_TYPE (new_dep) < (int) old_type)
1143 {
1144 DEP_TYPE (dep) = DEP_TYPE (new_dep);
1145 res = DEP_CHANGED;
1146 }
1147
1148 if (current_sched_info->flags & USE_DEPS_LIST)
1149 /* Update DEP_STATUS. */
1150 {
1151 ds_t dep_status = DEP_STATUS (dep);
1152 ds_t ds = DEP_STATUS (new_dep);
1153 ds_t new_status = ds | dep_status;
1154
1155 if (new_status & SPECULATIVE)
1156 {
1157 /* Either existing dep or a dep we're adding or both are
1158 speculative. */
1159 if (!(ds & SPECULATIVE)
1160 || !(dep_status & SPECULATIVE))
1161 /* The new dep can't be speculative. */
1162 new_status &= ~SPECULATIVE;
1163 else
1164 {
1165 /* Both are speculative. Merge probabilities. */
1166 if (mem1 != NULL)
1167 {
1168 dw_t dw;
1169
1170 dw = estimate_dep_weak (mem1, mem2);
1171 ds = set_dep_weak (ds, BEGIN_DATA, dw);
1172 }
1173
1174 new_status = ds_merge (dep_status, ds);
1175 }
1176 }
1177
1178 ds = new_status;
1179
1180 if (dep_status != ds)
1181 {
1182 DEP_STATUS (dep) = ds;
1183 res = DEP_CHANGED;
1184 }
1185 }
1186
1187 if (was_spec && !dep_spec_p (dep))
1188 /* The old dep was speculative, but now it isn't. */
1189 change_spec_dep_to_hard (sd_it);
1190
1191 if (true_dependency_cache != NULL
1192 && res == DEP_CHANGED)
1193 update_dependency_caches (dep, old_type);
1194
1195 return res;
1196 }
1197
1198 /* Add or update a dependence described by DEP.
1199 MEM1 and MEM2, if non-null, correspond to memory locations in case of
1200 data speculation.
1201
1202 The function returns a value indicating if an old entry has been changed
1203 or a new entry has been added to insn's backward deps or nothing has
1204 been updated at all. */
1205 static enum DEPS_ADJUST_RESULT
1206 add_or_update_dep_1 (dep_t new_dep, bool resolved_p,
1207 rtx mem1 ATTRIBUTE_UNUSED, rtx mem2 ATTRIBUTE_UNUSED)
1208 {
1209 bool maybe_present_p = true;
1210 bool present_p = false;
1211
1212 gcc_assert (INSN_P (DEP_PRO (new_dep)) && INSN_P (DEP_CON (new_dep))
1213 && DEP_PRO (new_dep) != DEP_CON (new_dep));
1214
1215 #ifdef ENABLE_CHECKING
1216 check_dep (new_dep, mem1 != NULL);
1217 #endif
1218
1219 if (true_dependency_cache != NULL)
1220 {
1221 switch (ask_dependency_caches (new_dep))
1222 {
1223 case DEP_PRESENT:
1224 return DEP_PRESENT;
1225
1226 case DEP_CHANGED:
1227 maybe_present_p = true;
1228 present_p = true;
1229 break;
1230
1231 case DEP_CREATED:
1232 maybe_present_p = false;
1233 present_p = false;
1234 break;
1235
1236 default:
1237 gcc_unreachable ();
1238 break;
1239 }
1240 }
1241
1242 /* Check that we don't already have this dependence. */
1243 if (maybe_present_p)
1244 {
1245 dep_t present_dep;
1246 sd_iterator_def sd_it;
1247
1248 gcc_assert (true_dependency_cache == NULL || present_p);
1249
1250 present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
1251 DEP_CON (new_dep),
1252 resolved_p, &sd_it);
1253
1254 if (present_dep != NULL)
1255 /* We found an existing dependency between ELEM and INSN. */
1256 return update_dep (present_dep, new_dep, sd_it, mem1, mem2);
1257 else
1258 /* We didn't find a dep, it shouldn't present in the cache. */
1259 gcc_assert (!present_p);
1260 }
1261
1262 /* Might want to check one level of transitivity to save conses.
1263 This check should be done in maybe_add_or_update_dep_1.
1264 Since we made it to add_or_update_dep_1, we must create
1265 (or update) a link. */
1266
1267 if (mem1 != NULL_RTX)
1268 {
1269 gcc_assert (sched_deps_info->generate_spec_deps);
1270 DEP_STATUS (new_dep) = set_dep_weak (DEP_STATUS (new_dep), BEGIN_DATA,
1271 estimate_dep_weak (mem1, mem2));
1272 }
1273
1274 sd_add_dep (new_dep, resolved_p);
1275
1276 return DEP_CREATED;
1277 }
1278
1279 /* Initialize BACK_LIST_PTR with consumer's backward list and
1280 FORW_LIST_PTR with producer's forward list. If RESOLVED_P is true
1281 initialize with lists that hold resolved deps. */
1282 static void
1283 get_back_and_forw_lists (dep_t dep, bool resolved_p,
1284 deps_list_t *back_list_ptr,
1285 deps_list_t *forw_list_ptr)
1286 {
1287 rtx con = DEP_CON (dep);
1288
1289 if (!resolved_p)
1290 {
1291 if (dep_spec_p (dep))
1292 *back_list_ptr = INSN_SPEC_BACK_DEPS (con);
1293 else
1294 *back_list_ptr = INSN_HARD_BACK_DEPS (con);
1295
1296 *forw_list_ptr = INSN_FORW_DEPS (DEP_PRO (dep));
1297 }
1298 else
1299 {
1300 *back_list_ptr = INSN_RESOLVED_BACK_DEPS (con);
1301 *forw_list_ptr = INSN_RESOLVED_FORW_DEPS (DEP_PRO (dep));
1302 }
1303 }
1304
1305 /* Add dependence described by DEP.
1306 If RESOLVED_P is true treat the dependence as a resolved one. */
1307 void
1308 sd_add_dep (dep_t dep, bool resolved_p)
1309 {
1310 dep_node_t n = create_dep_node ();
1311 deps_list_t con_back_deps;
1312 deps_list_t pro_forw_deps;
1313 rtx elem = DEP_PRO (dep);
1314 rtx insn = DEP_CON (dep);
1315
1316 gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
1317
1318 if ((current_sched_info->flags & DO_SPECULATION) == 0
1319 || !sched_insn_is_legitimate_for_speculation_p (insn, DEP_STATUS (dep)))
1320 DEP_STATUS (dep) &= ~SPECULATIVE;
1321
1322 copy_dep (DEP_NODE_DEP (n), dep);
1323
1324 get_back_and_forw_lists (dep, resolved_p, &con_back_deps, &pro_forw_deps);
1325
1326 add_to_deps_list (DEP_NODE_BACK (n), con_back_deps);
1327
1328 #ifdef ENABLE_CHECKING
1329 check_dep (dep, false);
1330 #endif
1331
1332 add_to_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1333
1334 /* If we are adding a dependency to INSN's LOG_LINKs, then note that
1335 in the bitmap caches of dependency information. */
1336 if (true_dependency_cache != NULL)
1337 set_dependency_caches (dep);
1338 }
1339
1340 /* Add or update backward dependence between INSN and ELEM
1341 with given type DEP_TYPE and dep_status DS.
1342 This function is a convenience wrapper. */
1343 enum DEPS_ADJUST_RESULT
1344 sd_add_or_update_dep (dep_t dep, bool resolved_p)
1345 {
1346 return add_or_update_dep_1 (dep, resolved_p, NULL_RTX, NULL_RTX);
1347 }
1348
1349 /* Resolved dependence pointed to by SD_IT.
1350 SD_IT will advance to the next element. */
1351 void
1352 sd_resolve_dep (sd_iterator_def sd_it)
1353 {
1354 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1355 dep_t dep = DEP_NODE_DEP (node);
1356 rtx pro = DEP_PRO (dep);
1357 rtx con = DEP_CON (dep);
1358
1359 if (dep_spec_p (dep))
1360 move_dep_link (DEP_NODE_BACK (node), INSN_SPEC_BACK_DEPS (con),
1361 INSN_RESOLVED_BACK_DEPS (con));
1362 else
1363 move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
1364 INSN_RESOLVED_BACK_DEPS (con));
1365
1366 move_dep_link (DEP_NODE_FORW (node), INSN_FORW_DEPS (pro),
1367 INSN_RESOLVED_FORW_DEPS (pro));
1368 }
1369
1370 /* Perform the inverse operation of sd_resolve_dep. Restore the dependence
1371 pointed to by SD_IT to unresolved state. */
1372 void
1373 sd_unresolve_dep (sd_iterator_def sd_it)
1374 {
1375 dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1376 dep_t dep = DEP_NODE_DEP (node);
1377 rtx pro = DEP_PRO (dep);
1378 rtx con = DEP_CON (dep);
1379
1380 if (dep_spec_p (dep))
1381 move_dep_link (DEP_NODE_BACK (node), INSN_RESOLVED_BACK_DEPS (con),
1382 INSN_SPEC_BACK_DEPS (con));
1383 else
1384 move_dep_link (DEP_NODE_BACK (node), INSN_RESOLVED_BACK_DEPS (con),
1385 INSN_HARD_BACK_DEPS (con));
1386
1387 move_dep_link (DEP_NODE_FORW (node), INSN_RESOLVED_FORW_DEPS (pro),
1388 INSN_FORW_DEPS (pro));
1389 }
1390
1391 /* Make TO depend on all the FROM's producers.
1392 If RESOLVED_P is true add dependencies to the resolved lists. */
1393 void
1394 sd_copy_back_deps (rtx to, rtx from, bool resolved_p)
1395 {
1396 sd_list_types_def list_type;
1397 sd_iterator_def sd_it;
1398 dep_t dep;
1399
1400 list_type = resolved_p ? SD_LIST_RES_BACK : SD_LIST_BACK;
1401
1402 FOR_EACH_DEP (from, list_type, sd_it, dep)
1403 {
1404 dep_def _new_dep, *new_dep = &_new_dep;
1405
1406 copy_dep (new_dep, dep);
1407 DEP_CON (new_dep) = to;
1408 sd_add_dep (new_dep, resolved_p);
1409 }
1410 }
1411
1412 /* Remove a dependency referred to by SD_IT.
1413 SD_IT will point to the next dependence after removal. */
1414 void
1415 sd_delete_dep (sd_iterator_def sd_it)
1416 {
1417 dep_node_t n = DEP_LINK_NODE (*sd_it.linkp);
1418 dep_t dep = DEP_NODE_DEP (n);
1419 rtx pro = DEP_PRO (dep);
1420 rtx con = DEP_CON (dep);
1421 deps_list_t con_back_deps;
1422 deps_list_t pro_forw_deps;
1423
1424 if (true_dependency_cache != NULL)
1425 {
1426 int elem_luid = INSN_LUID (pro);
1427 int insn_luid = INSN_LUID (con);
1428
1429 bitmap_clear_bit (&true_dependency_cache[insn_luid], elem_luid);
1430 bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1431 bitmap_clear_bit (&control_dependency_cache[insn_luid], elem_luid);
1432 bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1433
1434 if (current_sched_info->flags & DO_SPECULATION)
1435 bitmap_clear_bit (&spec_dependency_cache[insn_luid], elem_luid);
1436 }
1437
1438 get_back_and_forw_lists (dep, sd_it.resolved_p,
1439 &con_back_deps, &pro_forw_deps);
1440
1441 remove_from_deps_list (DEP_NODE_BACK (n), con_back_deps);
1442 remove_from_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1443
1444 delete_dep_node (n);
1445 }
1446
1447 /* Dump size of the lists. */
1448 #define DUMP_LISTS_SIZE (2)
1449
1450 /* Dump dependencies of the lists. */
1451 #define DUMP_LISTS_DEPS (4)
1452
1453 /* Dump all information about the lists. */
1454 #define DUMP_LISTS_ALL (DUMP_LISTS_SIZE | DUMP_LISTS_DEPS)
1455
1456 /* Dump deps_lists of INSN specified by TYPES to DUMP.
1457 FLAGS is a bit mask specifying what information about the lists needs
1458 to be printed.
1459 If FLAGS has the very first bit set, then dump all information about
1460 the lists and propagate this bit into the callee dump functions. */
1461 static void
1462 dump_lists (FILE *dump, rtx insn, sd_list_types_def types, int flags)
1463 {
1464 sd_iterator_def sd_it;
1465 dep_t dep;
1466 int all;
1467
1468 all = (flags & 1);
1469
1470 if (all)
1471 flags |= DUMP_LISTS_ALL;
1472
1473 fprintf (dump, "[");
1474
1475 if (flags & DUMP_LISTS_SIZE)
1476 fprintf (dump, "%d; ", sd_lists_size (insn, types));
1477
1478 if (flags & DUMP_LISTS_DEPS)
1479 {
1480 FOR_EACH_DEP (insn, types, sd_it, dep)
1481 {
1482 dump_dep (dump, dep, dump_dep_flags | all);
1483 fprintf (dump, " ");
1484 }
1485 }
1486 }
1487
1488 /* Dump all information about deps_lists of INSN specified by TYPES
1489 to STDERR. */
1490 void
1491 sd_debug_lists (rtx insn, sd_list_types_def types)
1492 {
1493 dump_lists (stderr, insn, types, 1);
1494 fprintf (stderr, "\n");
1495 }
1496
1497 /* A wrapper around add_dependence_1, to add a dependence of CON on
1498 PRO, with type DEP_TYPE. This function implements special handling
1499 for REG_DEP_CONTROL dependencies. For these, we optionally promote
1500 the type to REG_DEP_ANTI if we can determine that predication is
1501 impossible; otherwise we add additional true dependencies on the
1502 INSN_COND_DEPS list of the jump (which PRO must be). */
1503 void
1504 add_dependence (rtx con, rtx pro, enum reg_note dep_type)
1505 {
1506 if (dep_type == REG_DEP_CONTROL
1507 && !(current_sched_info->flags & DO_PREDICATION))
1508 dep_type = REG_DEP_ANTI;
1509
1510 /* A REG_DEP_CONTROL dependence may be eliminated through predication,
1511 so we must also make the insn dependent on the setter of the
1512 condition. */
1513 if (dep_type == REG_DEP_CONTROL)
1514 {
1515 rtx real_pro = pro;
1516 rtx other = real_insn_for_shadow (real_pro);
1517 rtx cond;
1518
1519 if (other != NULL_RTX)
1520 real_pro = other;
1521 cond = sched_get_reverse_condition_uncached (real_pro);
1522 /* Verify that the insn does not use a different value in
1523 the condition register than the one that was present at
1524 the jump. */
1525 if (cond == NULL_RTX)
1526 dep_type = REG_DEP_ANTI;
1527 else if (INSN_CACHED_COND (real_pro) == const_true_rtx)
1528 {
1529 HARD_REG_SET uses;
1530 CLEAR_HARD_REG_SET (uses);
1531 note_uses (&PATTERN (con), record_hard_reg_uses, &uses);
1532 if (TEST_HARD_REG_BIT (uses, REGNO (XEXP (cond, 0))))
1533 dep_type = REG_DEP_ANTI;
1534 }
1535 if (dep_type == REG_DEP_CONTROL)
1536 {
1537 if (sched_verbose >= 5)
1538 fprintf (sched_dump, "making DEP_CONTROL for %d\n",
1539 INSN_UID (real_pro));
1540 add_dependence_list (con, INSN_COND_DEPS (real_pro), 0,
1541 REG_DEP_TRUE);
1542 }
1543 }
1544
1545 add_dependence_1 (con, pro, dep_type);
1546 }
1547
1548 /* A convenience wrapper to operate on an entire list. */
1549
1550 static void
1551 add_dependence_list (rtx insn, rtx list, int uncond, enum reg_note dep_type)
1552 {
1553 for (; list; list = XEXP (list, 1))
1554 {
1555 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1556 add_dependence (insn, XEXP (list, 0), dep_type);
1557 }
1558 }
1559
1560 /* Similar, but free *LISTP at the same time, when the context
1561 is not readonly. */
1562
1563 static void
1564 add_dependence_list_and_free (struct deps_desc *deps, rtx insn, rtx *listp,
1565 int uncond, enum reg_note dep_type)
1566 {
1567 rtx list, next;
1568
1569 /* We don't want to short-circuit dependencies involving debug
1570 insns, because they may cause actual dependencies to be
1571 disregarded. */
1572 if (deps->readonly || DEBUG_INSN_P (insn))
1573 {
1574 add_dependence_list (insn, *listp, uncond, dep_type);
1575 return;
1576 }
1577
1578 for (list = *listp, *listp = NULL; list ; list = next)
1579 {
1580 next = XEXP (list, 1);
1581 if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1582 add_dependence (insn, XEXP (list, 0), dep_type);
1583 free_INSN_LIST_node (list);
1584 }
1585 }
1586
1587 /* Remove all occurrences of INSN from LIST. Return the number of
1588 occurrences removed. */
1589
1590 static int
1591 remove_from_dependence_list (rtx insn, rtx* listp)
1592 {
1593 int removed = 0;
1594
1595 while (*listp)
1596 {
1597 if (XEXP (*listp, 0) == insn)
1598 {
1599 remove_free_INSN_LIST_node (listp);
1600 removed++;
1601 continue;
1602 }
1603
1604 listp = &XEXP (*listp, 1);
1605 }
1606
1607 return removed;
1608 }
1609
1610 /* Same as above, but process two lists at once. */
1611 static int
1612 remove_from_both_dependence_lists (rtx insn, rtx *listp, rtx *exprp)
1613 {
1614 int removed = 0;
1615
1616 while (*listp)
1617 {
1618 if (XEXP (*listp, 0) == insn)
1619 {
1620 remove_free_INSN_LIST_node (listp);
1621 remove_free_EXPR_LIST_node (exprp);
1622 removed++;
1623 continue;
1624 }
1625
1626 listp = &XEXP (*listp, 1);
1627 exprp = &XEXP (*exprp, 1);
1628 }
1629
1630 return removed;
1631 }
1632
1633 /* Clear all dependencies for an insn. */
1634 static void
1635 delete_all_dependences (rtx insn)
1636 {
1637 sd_iterator_def sd_it;
1638 dep_t dep;
1639
1640 /* The below cycle can be optimized to clear the caches and back_deps
1641 in one call but that would provoke duplication of code from
1642 delete_dep (). */
1643
1644 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1645 sd_iterator_cond (&sd_it, &dep);)
1646 sd_delete_dep (sd_it);
1647 }
1648
1649 /* All insns in a scheduling group except the first should only have
1650 dependencies on the previous insn in the group. So we find the
1651 first instruction in the scheduling group by walking the dependence
1652 chains backwards. Then we add the dependencies for the group to
1653 the previous nonnote insn. */
1654
1655 static void
1656 chain_to_prev_insn (rtx insn)
1657 {
1658 sd_iterator_def sd_it;
1659 dep_t dep;
1660 rtx prev_nonnote;
1661
1662 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
1663 {
1664 rtx i = insn;
1665 rtx pro = DEP_PRO (dep);
1666
1667 do
1668 {
1669 i = prev_nonnote_insn (i);
1670
1671 if (pro == i)
1672 goto next_link;
1673 } while (SCHED_GROUP_P (i) || DEBUG_INSN_P (i));
1674
1675 if (! sched_insns_conditions_mutex_p (i, pro))
1676 add_dependence (i, pro, DEP_TYPE (dep));
1677 next_link:;
1678 }
1679
1680 delete_all_dependences (insn);
1681
1682 prev_nonnote = prev_nonnote_nondebug_insn (insn);
1683 if (BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (prev_nonnote)
1684 && ! sched_insns_conditions_mutex_p (insn, prev_nonnote))
1685 add_dependence (insn, prev_nonnote, REG_DEP_ANTI);
1686 }
1687 \f
1688 /* Process an insn's memory dependencies. There are four kinds of
1689 dependencies:
1690
1691 (0) read dependence: read follows read
1692 (1) true dependence: read follows write
1693 (2) output dependence: write follows write
1694 (3) anti dependence: write follows read
1695
1696 We are careful to build only dependencies which actually exist, and
1697 use transitivity to avoid building too many links. */
1698
1699 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1700 The MEM is a memory reference contained within INSN, which we are saving
1701 so that we can do memory aliasing on it. */
1702
1703 static void
1704 add_insn_mem_dependence (struct deps_desc *deps, bool read_p,
1705 rtx insn, rtx mem)
1706 {
1707 rtx *insn_list;
1708 rtx *mem_list;
1709 rtx link;
1710
1711 gcc_assert (!deps->readonly);
1712 if (read_p)
1713 {
1714 insn_list = &deps->pending_read_insns;
1715 mem_list = &deps->pending_read_mems;
1716 if (!DEBUG_INSN_P (insn))
1717 deps->pending_read_list_length++;
1718 }
1719 else
1720 {
1721 insn_list = &deps->pending_write_insns;
1722 mem_list = &deps->pending_write_mems;
1723 deps->pending_write_list_length++;
1724 }
1725
1726 link = alloc_INSN_LIST (insn, *insn_list);
1727 *insn_list = link;
1728
1729 if (sched_deps_info->use_cselib)
1730 {
1731 mem = shallow_copy_rtx (mem);
1732 XEXP (mem, 0) = cselib_subst_to_values_from_insn (XEXP (mem, 0),
1733 GET_MODE (mem), insn);
1734 }
1735 link = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
1736 *mem_list = link;
1737 }
1738
1739 /* Make a dependency between every memory reference on the pending lists
1740 and INSN, thus flushing the pending lists. FOR_READ is true if emitting
1741 dependencies for a read operation, similarly with FOR_WRITE. */
1742
1743 static void
1744 flush_pending_lists (struct deps_desc *deps, rtx insn, int for_read,
1745 int for_write)
1746 {
1747 if (for_write)
1748 {
1749 add_dependence_list_and_free (deps, insn, &deps->pending_read_insns,
1750 1, REG_DEP_ANTI);
1751 if (!deps->readonly)
1752 {
1753 free_EXPR_LIST_list (&deps->pending_read_mems);
1754 deps->pending_read_list_length = 0;
1755 }
1756 }
1757
1758 add_dependence_list_and_free (deps, insn, &deps->pending_write_insns, 1,
1759 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1760
1761 add_dependence_list_and_free (deps, insn,
1762 &deps->last_pending_memory_flush, 1,
1763 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1764
1765 add_dependence_list_and_free (deps, insn, &deps->pending_jump_insns, 1,
1766 REG_DEP_ANTI);
1767
1768 if (!deps->readonly)
1769 {
1770 free_EXPR_LIST_list (&deps->pending_write_mems);
1771 deps->pending_write_list_length = 0;
1772
1773 deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
1774 deps->pending_flush_length = 1;
1775 }
1776 }
1777 \f
1778 /* Instruction which dependencies we are analyzing. */
1779 static rtx cur_insn = NULL_RTX;
1780
1781 /* Implement hooks for haifa scheduler. */
1782
1783 static void
1784 haifa_start_insn (rtx insn)
1785 {
1786 gcc_assert (insn && !cur_insn);
1787
1788 cur_insn = insn;
1789 }
1790
1791 static void
1792 haifa_finish_insn (void)
1793 {
1794 cur_insn = NULL;
1795 }
1796
1797 void
1798 haifa_note_reg_set (int regno)
1799 {
1800 SET_REGNO_REG_SET (reg_pending_sets, regno);
1801 }
1802
1803 void
1804 haifa_note_reg_clobber (int regno)
1805 {
1806 SET_REGNO_REG_SET (reg_pending_clobbers, regno);
1807 }
1808
1809 void
1810 haifa_note_reg_use (int regno)
1811 {
1812 SET_REGNO_REG_SET (reg_pending_uses, regno);
1813 }
1814
1815 static void
1816 haifa_note_mem_dep (rtx mem, rtx pending_mem, rtx pending_insn, ds_t ds)
1817 {
1818 if (!(ds & SPECULATIVE))
1819 {
1820 mem = NULL_RTX;
1821 pending_mem = NULL_RTX;
1822 }
1823 else
1824 gcc_assert (ds & BEGIN_DATA);
1825
1826 {
1827 dep_def _dep, *dep = &_dep;
1828
1829 init_dep_1 (dep, pending_insn, cur_insn, ds_to_dt (ds),
1830 current_sched_info->flags & USE_DEPS_LIST ? ds : 0);
1831 maybe_add_or_update_dep_1 (dep, false, pending_mem, mem);
1832 }
1833
1834 }
1835
1836 static void
1837 haifa_note_dep (rtx elem, ds_t ds)
1838 {
1839 dep_def _dep;
1840 dep_t dep = &_dep;
1841
1842 init_dep (dep, elem, cur_insn, ds_to_dt (ds));
1843 maybe_add_or_update_dep_1 (dep, false, NULL_RTX, NULL_RTX);
1844 }
1845
1846 static void
1847 note_reg_use (int r)
1848 {
1849 if (sched_deps_info->note_reg_use)
1850 sched_deps_info->note_reg_use (r);
1851 }
1852
1853 static void
1854 note_reg_set (int r)
1855 {
1856 if (sched_deps_info->note_reg_set)
1857 sched_deps_info->note_reg_set (r);
1858 }
1859
1860 static void
1861 note_reg_clobber (int r)
1862 {
1863 if (sched_deps_info->note_reg_clobber)
1864 sched_deps_info->note_reg_clobber (r);
1865 }
1866
1867 static void
1868 note_mem_dep (rtx m1, rtx m2, rtx e, ds_t ds)
1869 {
1870 if (sched_deps_info->note_mem_dep)
1871 sched_deps_info->note_mem_dep (m1, m2, e, ds);
1872 }
1873
1874 static void
1875 note_dep (rtx e, ds_t ds)
1876 {
1877 if (sched_deps_info->note_dep)
1878 sched_deps_info->note_dep (e, ds);
1879 }
1880
1881 /* Return corresponding to DS reg_note. */
1882 enum reg_note
1883 ds_to_dt (ds_t ds)
1884 {
1885 if (ds & DEP_TRUE)
1886 return REG_DEP_TRUE;
1887 else if (ds & DEP_OUTPUT)
1888 return REG_DEP_OUTPUT;
1889 else if (ds & DEP_ANTI)
1890 return REG_DEP_ANTI;
1891 else
1892 {
1893 gcc_assert (ds & DEP_CONTROL);
1894 return REG_DEP_CONTROL;
1895 }
1896 }
1897
1898 \f
1899
1900 /* Functions for computation of info needed for register pressure
1901 sensitive insn scheduling. */
1902
1903
1904 /* Allocate and return reg_use_data structure for REGNO and INSN. */
1905 static struct reg_use_data *
1906 create_insn_reg_use (int regno, rtx insn)
1907 {
1908 struct reg_use_data *use;
1909
1910 use = (struct reg_use_data *) xmalloc (sizeof (struct reg_use_data));
1911 use->regno = regno;
1912 use->insn = insn;
1913 use->next_insn_use = INSN_REG_USE_LIST (insn);
1914 INSN_REG_USE_LIST (insn) = use;
1915 return use;
1916 }
1917
1918 /* Allocate and return reg_set_data structure for REGNO and INSN. */
1919 static struct reg_set_data *
1920 create_insn_reg_set (int regno, rtx insn)
1921 {
1922 struct reg_set_data *set;
1923
1924 set = (struct reg_set_data *) xmalloc (sizeof (struct reg_set_data));
1925 set->regno = regno;
1926 set->insn = insn;
1927 set->next_insn_set = INSN_REG_SET_LIST (insn);
1928 INSN_REG_SET_LIST (insn) = set;
1929 return set;
1930 }
1931
1932 /* Set up insn register uses for INSN and dependency context DEPS. */
1933 static void
1934 setup_insn_reg_uses (struct deps_desc *deps, rtx insn)
1935 {
1936 unsigned i;
1937 reg_set_iterator rsi;
1938 rtx list;
1939 struct reg_use_data *use, *use2, *next;
1940 struct deps_reg *reg_last;
1941
1942 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
1943 {
1944 if (i < FIRST_PSEUDO_REGISTER
1945 && TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
1946 continue;
1947
1948 if (find_regno_note (insn, REG_DEAD, i) == NULL_RTX
1949 && ! REGNO_REG_SET_P (reg_pending_sets, i)
1950 && ! REGNO_REG_SET_P (reg_pending_clobbers, i))
1951 /* Ignore use which is not dying. */
1952 continue;
1953
1954 use = create_insn_reg_use (i, insn);
1955 use->next_regno_use = use;
1956 reg_last = &deps->reg_last[i];
1957
1958 /* Create the cycle list of uses. */
1959 for (list = reg_last->uses; list; list = XEXP (list, 1))
1960 {
1961 use2 = create_insn_reg_use (i, XEXP (list, 0));
1962 next = use->next_regno_use;
1963 use->next_regno_use = use2;
1964 use2->next_regno_use = next;
1965 }
1966 }
1967 }
1968
1969 /* Register pressure info for the currently processed insn. */
1970 static struct reg_pressure_data reg_pressure_info[N_REG_CLASSES];
1971
1972 /* Return TRUE if INSN has the use structure for REGNO. */
1973 static bool
1974 insn_use_p (rtx insn, int regno)
1975 {
1976 struct reg_use_data *use;
1977
1978 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1979 if (use->regno == regno)
1980 return true;
1981 return false;
1982 }
1983
1984 /* Update the register pressure info after birth of pseudo register REGNO
1985 in INSN. Arguments CLOBBER_P and UNUSED_P say correspondingly that
1986 the register is in clobber or unused after the insn. */
1987 static void
1988 mark_insn_pseudo_birth (rtx insn, int regno, bool clobber_p, bool unused_p)
1989 {
1990 int incr, new_incr;
1991 enum reg_class cl;
1992
1993 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
1994 cl = sched_regno_pressure_class[regno];
1995 if (cl != NO_REGS)
1996 {
1997 incr = ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (regno)];
1998 if (clobber_p)
1999 {
2000 new_incr = reg_pressure_info[cl].clobber_increase + incr;
2001 reg_pressure_info[cl].clobber_increase = new_incr;
2002 }
2003 else if (unused_p)
2004 {
2005 new_incr = reg_pressure_info[cl].unused_set_increase + incr;
2006 reg_pressure_info[cl].unused_set_increase = new_incr;
2007 }
2008 else
2009 {
2010 new_incr = reg_pressure_info[cl].set_increase + incr;
2011 reg_pressure_info[cl].set_increase = new_incr;
2012 if (! insn_use_p (insn, regno))
2013 reg_pressure_info[cl].change += incr;
2014 create_insn_reg_set (regno, insn);
2015 }
2016 gcc_assert (new_incr < (1 << INCREASE_BITS));
2017 }
2018 }
2019
2020 /* Like mark_insn_pseudo_regno_birth except that NREGS saying how many
2021 hard registers involved in the birth. */
2022 static void
2023 mark_insn_hard_regno_birth (rtx insn, int regno, int nregs,
2024 bool clobber_p, bool unused_p)
2025 {
2026 enum reg_class cl;
2027 int new_incr, last = regno + nregs;
2028
2029 while (regno < last)
2030 {
2031 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2032 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
2033 {
2034 cl = sched_regno_pressure_class[regno];
2035 if (cl != NO_REGS)
2036 {
2037 if (clobber_p)
2038 {
2039 new_incr = reg_pressure_info[cl].clobber_increase + 1;
2040 reg_pressure_info[cl].clobber_increase = new_incr;
2041 }
2042 else if (unused_p)
2043 {
2044 new_incr = reg_pressure_info[cl].unused_set_increase + 1;
2045 reg_pressure_info[cl].unused_set_increase = new_incr;
2046 }
2047 else
2048 {
2049 new_incr = reg_pressure_info[cl].set_increase + 1;
2050 reg_pressure_info[cl].set_increase = new_incr;
2051 if (! insn_use_p (insn, regno))
2052 reg_pressure_info[cl].change += 1;
2053 create_insn_reg_set (regno, insn);
2054 }
2055 gcc_assert (new_incr < (1 << INCREASE_BITS));
2056 }
2057 }
2058 regno++;
2059 }
2060 }
2061
2062 /* Update the register pressure info after birth of pseudo or hard
2063 register REG in INSN. Arguments CLOBBER_P and UNUSED_P say
2064 correspondingly that the register is in clobber or unused after the
2065 insn. */
2066 static void
2067 mark_insn_reg_birth (rtx insn, rtx reg, bool clobber_p, bool unused_p)
2068 {
2069 int regno;
2070
2071 if (GET_CODE (reg) == SUBREG)
2072 reg = SUBREG_REG (reg);
2073
2074 if (! REG_P (reg))
2075 return;
2076
2077 regno = REGNO (reg);
2078 if (regno < FIRST_PSEUDO_REGISTER)
2079 mark_insn_hard_regno_birth (insn, regno,
2080 hard_regno_nregs[regno][GET_MODE (reg)],
2081 clobber_p, unused_p);
2082 else
2083 mark_insn_pseudo_birth (insn, regno, clobber_p, unused_p);
2084 }
2085
2086 /* Update the register pressure info after death of pseudo register
2087 REGNO. */
2088 static void
2089 mark_pseudo_death (int regno)
2090 {
2091 int incr;
2092 enum reg_class cl;
2093
2094 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
2095 cl = sched_regno_pressure_class[regno];
2096 if (cl != NO_REGS)
2097 {
2098 incr = ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (regno)];
2099 reg_pressure_info[cl].change -= incr;
2100 }
2101 }
2102
2103 /* Like mark_pseudo_death except that NREGS saying how many hard
2104 registers involved in the death. */
2105 static void
2106 mark_hard_regno_death (int regno, int nregs)
2107 {
2108 enum reg_class cl;
2109 int last = regno + nregs;
2110
2111 while (regno < last)
2112 {
2113 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2114 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
2115 {
2116 cl = sched_regno_pressure_class[regno];
2117 if (cl != NO_REGS)
2118 reg_pressure_info[cl].change -= 1;
2119 }
2120 regno++;
2121 }
2122 }
2123
2124 /* Update the register pressure info after death of pseudo or hard
2125 register REG. */
2126 static void
2127 mark_reg_death (rtx reg)
2128 {
2129 int regno;
2130
2131 if (GET_CODE (reg) == SUBREG)
2132 reg = SUBREG_REG (reg);
2133
2134 if (! REG_P (reg))
2135 return;
2136
2137 regno = REGNO (reg);
2138 if (regno < FIRST_PSEUDO_REGISTER)
2139 mark_hard_regno_death (regno, hard_regno_nregs[regno][GET_MODE (reg)]);
2140 else
2141 mark_pseudo_death (regno);
2142 }
2143
2144 /* Process SETTER of REG. DATA is an insn containing the setter. */
2145 static void
2146 mark_insn_reg_store (rtx reg, const_rtx setter, void *data)
2147 {
2148 if (setter != NULL_RTX && GET_CODE (setter) != SET)
2149 return;
2150 mark_insn_reg_birth
2151 ((rtx) data, reg, false,
2152 find_reg_note ((const_rtx) data, REG_UNUSED, reg) != NULL_RTX);
2153 }
2154
2155 /* Like mark_insn_reg_store except notice just CLOBBERs; ignore SETs. */
2156 static void
2157 mark_insn_reg_clobber (rtx reg, const_rtx setter, void *data)
2158 {
2159 if (GET_CODE (setter) == CLOBBER)
2160 mark_insn_reg_birth ((rtx) data, reg, true, false);
2161 }
2162
2163 /* Set up reg pressure info related to INSN. */
2164 void
2165 init_insn_reg_pressure_info (rtx insn)
2166 {
2167 int i, len;
2168 enum reg_class cl;
2169 static struct reg_pressure_data *pressure_info;
2170 rtx link;
2171
2172 gcc_assert (sched_pressure != SCHED_PRESSURE_NONE);
2173
2174 if (! INSN_P (insn))
2175 return;
2176
2177 for (i = 0; i < ira_pressure_classes_num; i++)
2178 {
2179 cl = ira_pressure_classes[i];
2180 reg_pressure_info[cl].clobber_increase = 0;
2181 reg_pressure_info[cl].set_increase = 0;
2182 reg_pressure_info[cl].unused_set_increase = 0;
2183 reg_pressure_info[cl].change = 0;
2184 }
2185
2186 note_stores (PATTERN (insn), mark_insn_reg_clobber, insn);
2187
2188 note_stores (PATTERN (insn), mark_insn_reg_store, insn);
2189
2190 #ifdef AUTO_INC_DEC
2191 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2192 if (REG_NOTE_KIND (link) == REG_INC)
2193 mark_insn_reg_store (XEXP (link, 0), NULL_RTX, insn);
2194 #endif
2195
2196 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2197 if (REG_NOTE_KIND (link) == REG_DEAD)
2198 mark_reg_death (XEXP (link, 0));
2199
2200 len = sizeof (struct reg_pressure_data) * ira_pressure_classes_num;
2201 pressure_info
2202 = INSN_REG_PRESSURE (insn) = (struct reg_pressure_data *) xmalloc (len);
2203 if (sched_pressure == SCHED_PRESSURE_WEIGHTED)
2204 INSN_MAX_REG_PRESSURE (insn) = (int *) xcalloc (ira_pressure_classes_num
2205 * sizeof (int), 1);
2206 for (i = 0; i < ira_pressure_classes_num; i++)
2207 {
2208 cl = ira_pressure_classes[i];
2209 pressure_info[i].clobber_increase
2210 = reg_pressure_info[cl].clobber_increase;
2211 pressure_info[i].set_increase = reg_pressure_info[cl].set_increase;
2212 pressure_info[i].unused_set_increase
2213 = reg_pressure_info[cl].unused_set_increase;
2214 pressure_info[i].change = reg_pressure_info[cl].change;
2215 }
2216 }
2217
2218
2219 \f
2220
2221 /* Internal variable for sched_analyze_[12] () functions.
2222 If it is nonzero, this means that sched_analyze_[12] looks
2223 at the most toplevel SET. */
2224 static bool can_start_lhs_rhs_p;
2225
2226 /* Extend reg info for the deps context DEPS given that
2227 we have just generated a register numbered REGNO. */
2228 static void
2229 extend_deps_reg_info (struct deps_desc *deps, int regno)
2230 {
2231 int max_regno = regno + 1;
2232
2233 gcc_assert (!reload_completed);
2234
2235 /* In a readonly context, it would not hurt to extend info,
2236 but it should not be needed. */
2237 if (reload_completed && deps->readonly)
2238 {
2239 deps->max_reg = max_regno;
2240 return;
2241 }
2242
2243 if (max_regno > deps->max_reg)
2244 {
2245 deps->reg_last = XRESIZEVEC (struct deps_reg, deps->reg_last,
2246 max_regno);
2247 memset (&deps->reg_last[deps->max_reg],
2248 0, (max_regno - deps->max_reg)
2249 * sizeof (struct deps_reg));
2250 deps->max_reg = max_regno;
2251 }
2252 }
2253
2254 /* Extends REG_INFO_P if needed. */
2255 void
2256 maybe_extend_reg_info_p (void)
2257 {
2258 /* Extend REG_INFO_P, if needed. */
2259 if ((unsigned int)max_regno - 1 >= reg_info_p_size)
2260 {
2261 size_t new_reg_info_p_size = max_regno + 128;
2262
2263 gcc_assert (!reload_completed && sel_sched_p ());
2264
2265 reg_info_p = (struct reg_info_t *) xrecalloc (reg_info_p,
2266 new_reg_info_p_size,
2267 reg_info_p_size,
2268 sizeof (*reg_info_p));
2269 reg_info_p_size = new_reg_info_p_size;
2270 }
2271 }
2272
2273 /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
2274 The type of the reference is specified by REF and can be SET,
2275 CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE. */
2276
2277 static void
2278 sched_analyze_reg (struct deps_desc *deps, int regno, enum machine_mode mode,
2279 enum rtx_code ref, rtx insn)
2280 {
2281 /* We could emit new pseudos in renaming. Extend the reg structures. */
2282 if (!reload_completed && sel_sched_p ()
2283 && (regno >= max_reg_num () - 1 || regno >= deps->max_reg))
2284 extend_deps_reg_info (deps, regno);
2285
2286 maybe_extend_reg_info_p ();
2287
2288 /* A hard reg in a wide mode may really be multiple registers.
2289 If so, mark all of them just like the first. */
2290 if (regno < FIRST_PSEUDO_REGISTER)
2291 {
2292 int i = hard_regno_nregs[regno][mode];
2293 if (ref == SET)
2294 {
2295 while (--i >= 0)
2296 note_reg_set (regno + i);
2297 }
2298 else if (ref == USE)
2299 {
2300 while (--i >= 0)
2301 note_reg_use (regno + i);
2302 }
2303 else
2304 {
2305 while (--i >= 0)
2306 note_reg_clobber (regno + i);
2307 }
2308 }
2309
2310 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
2311 it does not reload. Ignore these as they have served their
2312 purpose already. */
2313 else if (regno >= deps->max_reg)
2314 {
2315 enum rtx_code code = GET_CODE (PATTERN (insn));
2316 gcc_assert (code == USE || code == CLOBBER);
2317 }
2318
2319 else
2320 {
2321 if (ref == SET)
2322 note_reg_set (regno);
2323 else if (ref == USE)
2324 note_reg_use (regno);
2325 else
2326 note_reg_clobber (regno);
2327
2328 /* Pseudos that are REG_EQUIV to something may be replaced
2329 by that during reloading. We need only add dependencies for
2330 the address in the REG_EQUIV note. */
2331 if (!reload_completed && get_reg_known_equiv_p (regno))
2332 {
2333 rtx t = get_reg_known_value (regno);
2334 if (MEM_P (t))
2335 sched_analyze_2 (deps, XEXP (t, 0), insn);
2336 }
2337
2338 /* Don't let it cross a call after scheduling if it doesn't
2339 already cross one. */
2340 if (REG_N_CALLS_CROSSED (regno) == 0)
2341 {
2342 if (!deps->readonly && ref == USE && !DEBUG_INSN_P (insn))
2343 deps->sched_before_next_call
2344 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
2345 else
2346 add_dependence_list (insn, deps->last_function_call, 1,
2347 REG_DEP_ANTI);
2348 }
2349 }
2350 }
2351
2352 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
2353 rtx, X, creating all dependencies generated by the write to the
2354 destination of X, and reads of everything mentioned. */
2355
2356 static void
2357 sched_analyze_1 (struct deps_desc *deps, rtx x, rtx insn)
2358 {
2359 rtx dest = XEXP (x, 0);
2360 enum rtx_code code = GET_CODE (x);
2361 bool cslr_p = can_start_lhs_rhs_p;
2362
2363 can_start_lhs_rhs_p = false;
2364
2365 gcc_assert (dest);
2366 if (dest == 0)
2367 return;
2368
2369 if (cslr_p && sched_deps_info->start_lhs)
2370 sched_deps_info->start_lhs (dest);
2371
2372 if (GET_CODE (dest) == PARALLEL)
2373 {
2374 int i;
2375
2376 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
2377 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
2378 sched_analyze_1 (deps,
2379 gen_rtx_CLOBBER (VOIDmode,
2380 XEXP (XVECEXP (dest, 0, i), 0)),
2381 insn);
2382
2383 if (cslr_p && sched_deps_info->finish_lhs)
2384 sched_deps_info->finish_lhs ();
2385
2386 if (code == SET)
2387 {
2388 can_start_lhs_rhs_p = cslr_p;
2389
2390 sched_analyze_2 (deps, SET_SRC (x), insn);
2391
2392 can_start_lhs_rhs_p = false;
2393 }
2394
2395 return;
2396 }
2397
2398 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
2399 || GET_CODE (dest) == ZERO_EXTRACT)
2400 {
2401 if (GET_CODE (dest) == STRICT_LOW_PART
2402 || GET_CODE (dest) == ZERO_EXTRACT
2403 || df_read_modify_subreg_p (dest))
2404 {
2405 /* These both read and modify the result. We must handle
2406 them as writes to get proper dependencies for following
2407 instructions. We must handle them as reads to get proper
2408 dependencies from this to previous instructions.
2409 Thus we need to call sched_analyze_2. */
2410
2411 sched_analyze_2 (deps, XEXP (dest, 0), insn);
2412 }
2413 if (GET_CODE (dest) == ZERO_EXTRACT)
2414 {
2415 /* The second and third arguments are values read by this insn. */
2416 sched_analyze_2 (deps, XEXP (dest, 1), insn);
2417 sched_analyze_2 (deps, XEXP (dest, 2), insn);
2418 }
2419 dest = XEXP (dest, 0);
2420 }
2421
2422 if (REG_P (dest))
2423 {
2424 int regno = REGNO (dest);
2425 enum machine_mode mode = GET_MODE (dest);
2426
2427 sched_analyze_reg (deps, regno, mode, code, insn);
2428
2429 #ifdef STACK_REGS
2430 /* Treat all writes to a stack register as modifying the TOS. */
2431 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
2432 {
2433 /* Avoid analyzing the same register twice. */
2434 if (regno != FIRST_STACK_REG)
2435 sched_analyze_reg (deps, FIRST_STACK_REG, mode, code, insn);
2436
2437 add_to_hard_reg_set (&implicit_reg_pending_uses, mode,
2438 FIRST_STACK_REG);
2439 }
2440 #endif
2441 }
2442 else if (MEM_P (dest))
2443 {
2444 /* Writing memory. */
2445 rtx t = dest;
2446
2447 if (sched_deps_info->use_cselib)
2448 {
2449 enum machine_mode address_mode = get_address_mode (dest);
2450
2451 t = shallow_copy_rtx (dest);
2452 cselib_lookup_from_insn (XEXP (t, 0), address_mode, 1,
2453 GET_MODE (t), insn);
2454 XEXP (t, 0)
2455 = cselib_subst_to_values_from_insn (XEXP (t, 0), GET_MODE (t),
2456 insn);
2457 }
2458 t = canon_rtx (t);
2459
2460 /* Pending lists can't get larger with a readonly context. */
2461 if (!deps->readonly
2462 && ((deps->pending_read_list_length + deps->pending_write_list_length)
2463 > MAX_PENDING_LIST_LENGTH))
2464 {
2465 /* Flush all pending reads and writes to prevent the pending lists
2466 from getting any larger. Insn scheduling runs too slowly when
2467 these lists get long. When compiling GCC with itself,
2468 this flush occurs 8 times for sparc, and 10 times for m88k using
2469 the default value of 32. */
2470 flush_pending_lists (deps, insn, false, true);
2471 }
2472 else
2473 {
2474 rtx pending, pending_mem;
2475
2476 pending = deps->pending_read_insns;
2477 pending_mem = deps->pending_read_mems;
2478 while (pending)
2479 {
2480 if (anti_dependence (XEXP (pending_mem, 0), t)
2481 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2482 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2483 DEP_ANTI);
2484
2485 pending = XEXP (pending, 1);
2486 pending_mem = XEXP (pending_mem, 1);
2487 }
2488
2489 pending = deps->pending_write_insns;
2490 pending_mem = deps->pending_write_mems;
2491 while (pending)
2492 {
2493 if (output_dependence (XEXP (pending_mem, 0), t)
2494 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2495 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2496 DEP_OUTPUT);
2497
2498 pending = XEXP (pending, 1);
2499 pending_mem = XEXP (pending_mem, 1);
2500 }
2501
2502 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
2503 REG_DEP_ANTI);
2504 add_dependence_list (insn, deps->pending_jump_insns, 1,
2505 REG_DEP_CONTROL);
2506
2507 if (!deps->readonly)
2508 add_insn_mem_dependence (deps, false, insn, dest);
2509 }
2510 sched_analyze_2 (deps, XEXP (dest, 0), insn);
2511 }
2512
2513 if (cslr_p && sched_deps_info->finish_lhs)
2514 sched_deps_info->finish_lhs ();
2515
2516 /* Analyze reads. */
2517 if (GET_CODE (x) == SET)
2518 {
2519 can_start_lhs_rhs_p = cslr_p;
2520
2521 sched_analyze_2 (deps, SET_SRC (x), insn);
2522
2523 can_start_lhs_rhs_p = false;
2524 }
2525 }
2526
2527 /* Analyze the uses of memory and registers in rtx X in INSN. */
2528 static void
2529 sched_analyze_2 (struct deps_desc *deps, rtx x, rtx insn)
2530 {
2531 int i;
2532 int j;
2533 enum rtx_code code;
2534 const char *fmt;
2535 bool cslr_p = can_start_lhs_rhs_p;
2536
2537 can_start_lhs_rhs_p = false;
2538
2539 gcc_assert (x);
2540 if (x == 0)
2541 return;
2542
2543 if (cslr_p && sched_deps_info->start_rhs)
2544 sched_deps_info->start_rhs (x);
2545
2546 code = GET_CODE (x);
2547
2548 switch (code)
2549 {
2550 case CONST_INT:
2551 case CONST_DOUBLE:
2552 case CONST_FIXED:
2553 case CONST_VECTOR:
2554 case SYMBOL_REF:
2555 case CONST:
2556 case LABEL_REF:
2557 /* Ignore constants. */
2558 if (cslr_p && sched_deps_info->finish_rhs)
2559 sched_deps_info->finish_rhs ();
2560
2561 return;
2562
2563 #ifdef HAVE_cc0
2564 case CC0:
2565 /* User of CC0 depends on immediately preceding insn. */
2566 SCHED_GROUP_P (insn) = 1;
2567 /* Don't move CC0 setter to another block (it can set up the
2568 same flag for previous CC0 users which is safe). */
2569 CANT_MOVE (prev_nonnote_insn (insn)) = 1;
2570
2571 if (cslr_p && sched_deps_info->finish_rhs)
2572 sched_deps_info->finish_rhs ();
2573
2574 return;
2575 #endif
2576
2577 case REG:
2578 {
2579 int regno = REGNO (x);
2580 enum machine_mode mode = GET_MODE (x);
2581
2582 sched_analyze_reg (deps, regno, mode, USE, insn);
2583
2584 #ifdef STACK_REGS
2585 /* Treat all reads of a stack register as modifying the TOS. */
2586 if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
2587 {
2588 /* Avoid analyzing the same register twice. */
2589 if (regno != FIRST_STACK_REG)
2590 sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
2591 sched_analyze_reg (deps, FIRST_STACK_REG, mode, SET, insn);
2592 }
2593 #endif
2594
2595 if (cslr_p && sched_deps_info->finish_rhs)
2596 sched_deps_info->finish_rhs ();
2597
2598 return;
2599 }
2600
2601 case MEM:
2602 {
2603 /* Reading memory. */
2604 rtx u;
2605 rtx pending, pending_mem;
2606 rtx t = x;
2607
2608 if (sched_deps_info->use_cselib)
2609 {
2610 enum machine_mode address_mode = get_address_mode (t);
2611
2612 t = shallow_copy_rtx (t);
2613 cselib_lookup_from_insn (XEXP (t, 0), address_mode, 1,
2614 GET_MODE (t), insn);
2615 XEXP (t, 0)
2616 = cselib_subst_to_values_from_insn (XEXP (t, 0), GET_MODE (t),
2617 insn);
2618 }
2619
2620 if (!DEBUG_INSN_P (insn))
2621 {
2622 t = canon_rtx (t);
2623 pending = deps->pending_read_insns;
2624 pending_mem = deps->pending_read_mems;
2625 while (pending)
2626 {
2627 if (read_dependence (XEXP (pending_mem, 0), t)
2628 && ! sched_insns_conditions_mutex_p (insn,
2629 XEXP (pending, 0)))
2630 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2631 DEP_ANTI);
2632
2633 pending = XEXP (pending, 1);
2634 pending_mem = XEXP (pending_mem, 1);
2635 }
2636
2637 pending = deps->pending_write_insns;
2638 pending_mem = deps->pending_write_mems;
2639 while (pending)
2640 {
2641 if (true_dependence (XEXP (pending_mem, 0), VOIDmode, t)
2642 && ! sched_insns_conditions_mutex_p (insn,
2643 XEXP (pending, 0)))
2644 note_mem_dep (t, XEXP (pending_mem, 0), XEXP (pending, 0),
2645 sched_deps_info->generate_spec_deps
2646 ? BEGIN_DATA | DEP_TRUE : DEP_TRUE);
2647
2648 pending = XEXP (pending, 1);
2649 pending_mem = XEXP (pending_mem, 1);
2650 }
2651
2652 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
2653 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
2654
2655 for (u = deps->pending_jump_insns; u; u = XEXP (u, 1))
2656 if (deps_may_trap_p (x))
2657 {
2658 if ((sched_deps_info->generate_spec_deps)
2659 && sel_sched_p () && (spec_info->mask & BEGIN_CONTROL))
2660 {
2661 ds_t ds = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
2662 MAX_DEP_WEAK);
2663
2664 note_dep (XEXP (u, 0), ds);
2665 }
2666 else
2667 add_dependence (insn, XEXP (u, 0), REG_DEP_CONTROL);
2668 }
2669 }
2670
2671 /* Always add these dependencies to pending_reads, since
2672 this insn may be followed by a write. */
2673 if (!deps->readonly)
2674 add_insn_mem_dependence (deps, true, insn, x);
2675
2676 sched_analyze_2 (deps, XEXP (x, 0), insn);
2677
2678 if (cslr_p && sched_deps_info->finish_rhs)
2679 sched_deps_info->finish_rhs ();
2680
2681 return;
2682 }
2683
2684 /* Force pending stores to memory in case a trap handler needs them. */
2685 case TRAP_IF:
2686 flush_pending_lists (deps, insn, true, false);
2687 break;
2688
2689 case PREFETCH:
2690 if (PREFETCH_SCHEDULE_BARRIER_P (x))
2691 reg_pending_barrier = TRUE_BARRIER;
2692 break;
2693
2694 case UNSPEC_VOLATILE:
2695 flush_pending_lists (deps, insn, true, true);
2696 /* FALLTHRU */
2697
2698 case ASM_OPERANDS:
2699 case ASM_INPUT:
2700 {
2701 /* Traditional and volatile asm instructions must be considered to use
2702 and clobber all hard registers, all pseudo-registers and all of
2703 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
2704
2705 Consider for instance a volatile asm that changes the fpu rounding
2706 mode. An insn should not be moved across this even if it only uses
2707 pseudo-regs because it might give an incorrectly rounded result. */
2708 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
2709 reg_pending_barrier = TRUE_BARRIER;
2710
2711 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
2712 We can not just fall through here since then we would be confused
2713 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
2714 traditional asms unlike their normal usage. */
2715
2716 if (code == ASM_OPERANDS)
2717 {
2718 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
2719 sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
2720
2721 if (cslr_p && sched_deps_info->finish_rhs)
2722 sched_deps_info->finish_rhs ();
2723
2724 return;
2725 }
2726 break;
2727 }
2728
2729 case PRE_DEC:
2730 case POST_DEC:
2731 case PRE_INC:
2732 case POST_INC:
2733 /* These both read and modify the result. We must handle them as writes
2734 to get proper dependencies for following instructions. We must handle
2735 them as reads to get proper dependencies from this to previous
2736 instructions. Thus we need to pass them to both sched_analyze_1
2737 and sched_analyze_2. We must call sched_analyze_2 first in order
2738 to get the proper antecedent for the read. */
2739 sched_analyze_2 (deps, XEXP (x, 0), insn);
2740 sched_analyze_1 (deps, x, insn);
2741
2742 if (cslr_p && sched_deps_info->finish_rhs)
2743 sched_deps_info->finish_rhs ();
2744
2745 return;
2746
2747 case POST_MODIFY:
2748 case PRE_MODIFY:
2749 /* op0 = op0 + op1 */
2750 sched_analyze_2 (deps, XEXP (x, 0), insn);
2751 sched_analyze_2 (deps, XEXP (x, 1), insn);
2752 sched_analyze_1 (deps, x, insn);
2753
2754 if (cslr_p && sched_deps_info->finish_rhs)
2755 sched_deps_info->finish_rhs ();
2756
2757 return;
2758
2759 default:
2760 break;
2761 }
2762
2763 /* Other cases: walk the insn. */
2764 fmt = GET_RTX_FORMAT (code);
2765 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2766 {
2767 if (fmt[i] == 'e')
2768 sched_analyze_2 (deps, XEXP (x, i), insn);
2769 else if (fmt[i] == 'E')
2770 for (j = 0; j < XVECLEN (x, i); j++)
2771 sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
2772 }
2773
2774 if (cslr_p && sched_deps_info->finish_rhs)
2775 sched_deps_info->finish_rhs ();
2776 }
2777
2778 /* Analyze an INSN with pattern X to find all dependencies. */
2779 static void
2780 sched_analyze_insn (struct deps_desc *deps, rtx x, rtx insn)
2781 {
2782 RTX_CODE code = GET_CODE (x);
2783 rtx link;
2784 unsigned i;
2785 reg_set_iterator rsi;
2786
2787 if (! reload_completed)
2788 {
2789 HARD_REG_SET temp;
2790
2791 extract_insn (insn);
2792 preprocess_constraints ();
2793 ira_implicitly_set_insn_hard_regs (&temp);
2794 AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
2795 IOR_HARD_REG_SET (implicit_reg_pending_clobbers, temp);
2796 }
2797
2798 can_start_lhs_rhs_p = (NONJUMP_INSN_P (insn)
2799 && code == SET);
2800
2801 if (may_trap_p (x))
2802 /* Avoid moving trapping instructions across function calls that might
2803 not always return. */
2804 add_dependence_list (insn, deps->last_function_call_may_noreturn,
2805 1, REG_DEP_ANTI);
2806
2807 /* We must avoid creating a situation in which two successors of the
2808 current block have different unwind info after scheduling. If at any
2809 point the two paths re-join this leads to incorrect unwind info. */
2810 /* ??? There are certain situations involving a forced frame pointer in
2811 which, with extra effort, we could fix up the unwind info at a later
2812 CFG join. However, it seems better to notice these cases earlier
2813 during prologue generation and avoid marking the frame pointer setup
2814 as frame-related at all. */
2815 if (RTX_FRAME_RELATED_P (insn))
2816 {
2817 /* Make sure prologue insn is scheduled before next jump. */
2818 deps->sched_before_next_jump
2819 = alloc_INSN_LIST (insn, deps->sched_before_next_jump);
2820
2821 /* Make sure epilogue insn is scheduled after preceding jumps. */
2822 add_dependence_list (insn, deps->pending_jump_insns, 1, REG_DEP_ANTI);
2823 }
2824
2825 if (code == COND_EXEC)
2826 {
2827 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
2828
2829 /* ??? Should be recording conditions so we reduce the number of
2830 false dependencies. */
2831 x = COND_EXEC_CODE (x);
2832 code = GET_CODE (x);
2833 }
2834 if (code == SET || code == CLOBBER)
2835 {
2836 sched_analyze_1 (deps, x, insn);
2837
2838 /* Bare clobber insns are used for letting life analysis, reg-stack
2839 and others know that a value is dead. Depend on the last call
2840 instruction so that reg-stack won't get confused. */
2841 if (code == CLOBBER)
2842 add_dependence_list (insn, deps->last_function_call, 1,
2843 REG_DEP_OUTPUT);
2844 }
2845 else if (code == PARALLEL)
2846 {
2847 for (i = XVECLEN (x, 0); i--;)
2848 {
2849 rtx sub = XVECEXP (x, 0, i);
2850 code = GET_CODE (sub);
2851
2852 if (code == COND_EXEC)
2853 {
2854 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
2855 sub = COND_EXEC_CODE (sub);
2856 code = GET_CODE (sub);
2857 }
2858 if (code == SET || code == CLOBBER)
2859 sched_analyze_1 (deps, sub, insn);
2860 else
2861 sched_analyze_2 (deps, sub, insn);
2862 }
2863 }
2864 else
2865 sched_analyze_2 (deps, x, insn);
2866
2867 /* Mark registers CLOBBERED or used by called function. */
2868 if (CALL_P (insn))
2869 {
2870 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2871 {
2872 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
2873 sched_analyze_1 (deps, XEXP (link, 0), insn);
2874 else if (GET_CODE (XEXP (link, 0)) != SET)
2875 sched_analyze_2 (deps, XEXP (link, 0), insn);
2876 }
2877 /* Don't schedule anything after a tail call, tail call needs
2878 to use at least all call-saved registers. */
2879 if (SIBLING_CALL_P (insn))
2880 reg_pending_barrier = TRUE_BARRIER;
2881 else if (find_reg_note (insn, REG_SETJMP, NULL))
2882 reg_pending_barrier = MOVE_BARRIER;
2883 }
2884
2885 if (JUMP_P (insn))
2886 {
2887 rtx next;
2888 next = next_nonnote_nondebug_insn (insn);
2889 if (next && BARRIER_P (next))
2890 reg_pending_barrier = MOVE_BARRIER;
2891 else
2892 {
2893 rtx pending, pending_mem;
2894
2895 if (sched_deps_info->compute_jump_reg_dependencies)
2896 {
2897 (*sched_deps_info->compute_jump_reg_dependencies)
2898 (insn, reg_pending_control_uses);
2899
2900 /* Make latency of jump equal to 0 by using anti-dependence. */
2901 EXECUTE_IF_SET_IN_REG_SET (reg_pending_control_uses, 0, i, rsi)
2902 {
2903 struct deps_reg *reg_last = &deps->reg_last[i];
2904 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI);
2905 add_dependence_list (insn, reg_last->implicit_sets,
2906 0, REG_DEP_ANTI);
2907 add_dependence_list (insn, reg_last->clobbers, 0,
2908 REG_DEP_ANTI);
2909 }
2910 }
2911
2912 /* All memory writes and volatile reads must happen before the
2913 jump. Non-volatile reads must happen before the jump iff
2914 the result is needed by the above register used mask. */
2915
2916 pending = deps->pending_write_insns;
2917 pending_mem = deps->pending_write_mems;
2918 while (pending)
2919 {
2920 if (! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2921 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
2922 pending = XEXP (pending, 1);
2923 pending_mem = XEXP (pending_mem, 1);
2924 }
2925
2926 pending = deps->pending_read_insns;
2927 pending_mem = deps->pending_read_mems;
2928 while (pending)
2929 {
2930 if (MEM_VOLATILE_P (XEXP (pending_mem, 0))
2931 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
2932 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
2933 pending = XEXP (pending, 1);
2934 pending_mem = XEXP (pending_mem, 1);
2935 }
2936
2937 add_dependence_list (insn, deps->last_pending_memory_flush, 1,
2938 REG_DEP_ANTI);
2939 add_dependence_list (insn, deps->pending_jump_insns, 1,
2940 REG_DEP_ANTI);
2941 }
2942 }
2943
2944 /* If this instruction can throw an exception, then moving it changes
2945 where block boundaries fall. This is mighty confusing elsewhere.
2946 Therefore, prevent such an instruction from being moved. Same for
2947 non-jump instructions that define block boundaries.
2948 ??? Unclear whether this is still necessary in EBB mode. If not,
2949 add_branch_dependences should be adjusted for RGN mode instead. */
2950 if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
2951 || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
2952 reg_pending_barrier = MOVE_BARRIER;
2953
2954 if (sched_pressure != SCHED_PRESSURE_NONE)
2955 {
2956 setup_insn_reg_uses (deps, insn);
2957 init_insn_reg_pressure_info (insn);
2958 }
2959
2960 /* Add register dependencies for insn. */
2961 if (DEBUG_INSN_P (insn))
2962 {
2963 rtx prev = deps->last_debug_insn;
2964 rtx u;
2965
2966 if (!deps->readonly)
2967 deps->last_debug_insn = insn;
2968
2969 if (prev)
2970 add_dependence (insn, prev, REG_DEP_ANTI);
2971
2972 add_dependence_list (insn, deps->last_function_call, 1,
2973 REG_DEP_ANTI);
2974
2975 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
2976 if (!sel_sched_p ())
2977 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
2978
2979 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
2980 {
2981 struct deps_reg *reg_last = &deps->reg_last[i];
2982 add_dependence_list (insn, reg_last->sets, 1, REG_DEP_ANTI);
2983 /* There's no point in making REG_DEP_CONTROL dependencies for
2984 debug insns. */
2985 add_dependence_list (insn, reg_last->clobbers, 1, REG_DEP_ANTI);
2986
2987 if (!deps->readonly)
2988 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2989 }
2990 CLEAR_REG_SET (reg_pending_uses);
2991
2992 /* Quite often, a debug insn will refer to stuff in the
2993 previous instruction, but the reason we want this
2994 dependency here is to make sure the scheduler doesn't
2995 gratuitously move a debug insn ahead. This could dirty
2996 DF flags and cause additional analysis that wouldn't have
2997 occurred in compilation without debug insns, and such
2998 additional analysis can modify the generated code. */
2999 prev = PREV_INSN (insn);
3000
3001 if (prev && NONDEBUG_INSN_P (prev))
3002 add_dependence (insn, prev, REG_DEP_ANTI);
3003 }
3004 else
3005 {
3006 regset_head set_or_clobbered;
3007
3008 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
3009 {
3010 struct deps_reg *reg_last = &deps->reg_last[i];
3011 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
3012 add_dependence_list (insn, reg_last->implicit_sets, 0, REG_DEP_ANTI);
3013 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
3014
3015 if (!deps->readonly)
3016 {
3017 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3018 reg_last->uses_length++;
3019 }
3020 }
3021
3022 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3023 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i))
3024 {
3025 struct deps_reg *reg_last = &deps->reg_last[i];
3026 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
3027 add_dependence_list (insn, reg_last->implicit_sets, 0,
3028 REG_DEP_ANTI);
3029 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
3030
3031 if (!deps->readonly)
3032 {
3033 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
3034 reg_last->uses_length++;
3035 }
3036 }
3037
3038 if (targetm.sched.exposed_pipeline)
3039 {
3040 INIT_REG_SET (&set_or_clobbered);
3041 bitmap_ior (&set_or_clobbered, reg_pending_clobbers,
3042 reg_pending_sets);
3043 EXECUTE_IF_SET_IN_REG_SET (&set_or_clobbered, 0, i, rsi)
3044 {
3045 struct deps_reg *reg_last = &deps->reg_last[i];
3046 rtx list;
3047 for (list = reg_last->uses; list; list = XEXP (list, 1))
3048 {
3049 rtx other = XEXP (list, 0);
3050 if (INSN_CACHED_COND (other) != const_true_rtx
3051 && refers_to_regno_p (i, i + 1, INSN_CACHED_COND (other), NULL))
3052 INSN_CACHED_COND (other) = const_true_rtx;
3053 }
3054 }
3055 }
3056
3057 /* If the current insn is conditional, we can't free any
3058 of the lists. */
3059 if (sched_has_condition_p (insn))
3060 {
3061 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
3062 {
3063 struct deps_reg *reg_last = &deps->reg_last[i];
3064 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
3065 add_dependence_list (insn, reg_last->implicit_sets, 0,
3066 REG_DEP_ANTI);
3067 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
3068 add_dependence_list (insn, reg_last->control_uses, 0,
3069 REG_DEP_CONTROL);
3070
3071 if (!deps->readonly)
3072 {
3073 reg_last->clobbers
3074 = alloc_INSN_LIST (insn, reg_last->clobbers);
3075 reg_last->clobbers_length++;
3076 }
3077 }
3078 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
3079 {
3080 struct deps_reg *reg_last = &deps->reg_last[i];
3081 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
3082 add_dependence_list (insn, reg_last->implicit_sets, 0,
3083 REG_DEP_ANTI);
3084 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT);
3085 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
3086 add_dependence_list (insn, reg_last->control_uses, 0,
3087 REG_DEP_CONTROL);
3088
3089 if (!deps->readonly)
3090 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3091 }
3092 }
3093 else
3094 {
3095 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
3096 {
3097 struct deps_reg *reg_last = &deps->reg_last[i];
3098 if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
3099 || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
3100 {
3101 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3102 REG_DEP_OUTPUT);
3103 add_dependence_list_and_free (deps, insn,
3104 &reg_last->implicit_sets, 0,
3105 REG_DEP_ANTI);
3106 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3107 REG_DEP_ANTI);
3108 add_dependence_list_and_free (deps, insn,
3109 &reg_last->control_uses, 0,
3110 REG_DEP_ANTI);
3111 add_dependence_list_and_free
3112 (deps, insn, &reg_last->clobbers, 0, REG_DEP_OUTPUT);
3113
3114 if (!deps->readonly)
3115 {
3116 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3117 reg_last->clobbers_length = 0;
3118 reg_last->uses_length = 0;
3119 }
3120 }
3121 else
3122 {
3123 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
3124 add_dependence_list (insn, reg_last->implicit_sets, 0,
3125 REG_DEP_ANTI);
3126 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
3127 add_dependence_list (insn, reg_last->control_uses, 0,
3128 REG_DEP_CONTROL);
3129 }
3130
3131 if (!deps->readonly)
3132 {
3133 reg_last->clobbers_length++;
3134 reg_last->clobbers
3135 = alloc_INSN_LIST (insn, reg_last->clobbers);
3136 }
3137 }
3138 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
3139 {
3140 struct deps_reg *reg_last = &deps->reg_last[i];
3141
3142 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3143 REG_DEP_OUTPUT);
3144 add_dependence_list_and_free (deps, insn,
3145 &reg_last->implicit_sets,
3146 0, REG_DEP_ANTI);
3147 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
3148 REG_DEP_OUTPUT);
3149 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3150 REG_DEP_ANTI);
3151 add_dependence_list (insn, reg_last->control_uses, 0,
3152 REG_DEP_CONTROL);
3153
3154 if (!deps->readonly)
3155 {
3156 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3157 reg_last->uses_length = 0;
3158 reg_last->clobbers_length = 0;
3159 }
3160 }
3161 }
3162 if (!deps->readonly)
3163 {
3164 EXECUTE_IF_SET_IN_REG_SET (reg_pending_control_uses, 0, i, rsi)
3165 {
3166 struct deps_reg *reg_last = &deps->reg_last[i];
3167 reg_last->control_uses
3168 = alloc_INSN_LIST (insn, reg_last->control_uses);
3169 }
3170 }
3171 }
3172
3173 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3174 if (TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
3175 {
3176 struct deps_reg *reg_last = &deps->reg_last[i];
3177 add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI);
3178 add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI);
3179 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
3180 add_dependence_list (insn, reg_last->control_uses, 0, REG_DEP_ANTI);
3181
3182 if (!deps->readonly)
3183 reg_last->implicit_sets
3184 = alloc_INSN_LIST (insn, reg_last->implicit_sets);
3185 }
3186
3187 if (!deps->readonly)
3188 {
3189 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
3190 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
3191 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
3192 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3193 if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i)
3194 || TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
3195 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
3196
3197 /* Set up the pending barrier found. */
3198 deps->last_reg_pending_barrier = reg_pending_barrier;
3199 }
3200
3201 CLEAR_REG_SET (reg_pending_uses);
3202 CLEAR_REG_SET (reg_pending_clobbers);
3203 CLEAR_REG_SET (reg_pending_sets);
3204 CLEAR_REG_SET (reg_pending_control_uses);
3205 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
3206 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
3207
3208 /* Add dependencies if a scheduling barrier was found. */
3209 if (reg_pending_barrier)
3210 {
3211 /* In the case of barrier the most added dependencies are not
3212 real, so we use anti-dependence here. */
3213 if (sched_has_condition_p (insn))
3214 {
3215 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3216 {
3217 struct deps_reg *reg_last = &deps->reg_last[i];
3218 add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
3219 add_dependence_list (insn, reg_last->sets, 0,
3220 reg_pending_barrier == TRUE_BARRIER
3221 ? REG_DEP_TRUE : REG_DEP_ANTI);
3222 add_dependence_list (insn, reg_last->implicit_sets, 0,
3223 REG_DEP_ANTI);
3224 add_dependence_list (insn, reg_last->clobbers, 0,
3225 reg_pending_barrier == TRUE_BARRIER
3226 ? REG_DEP_TRUE : REG_DEP_ANTI);
3227 }
3228 }
3229 else
3230 {
3231 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3232 {
3233 struct deps_reg *reg_last = &deps->reg_last[i];
3234 add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
3235 REG_DEP_ANTI);
3236 add_dependence_list_and_free (deps, insn,
3237 &reg_last->control_uses, 0,
3238 REG_DEP_CONTROL);
3239 add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
3240 reg_pending_barrier == TRUE_BARRIER
3241 ? REG_DEP_TRUE : REG_DEP_ANTI);
3242 add_dependence_list_and_free (deps, insn,
3243 &reg_last->implicit_sets, 0,
3244 REG_DEP_ANTI);
3245 add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
3246 reg_pending_barrier == TRUE_BARRIER
3247 ? REG_DEP_TRUE : REG_DEP_ANTI);
3248
3249 if (!deps->readonly)
3250 {
3251 reg_last->uses_length = 0;
3252 reg_last->clobbers_length = 0;
3253 }
3254 }
3255 }
3256
3257 if (!deps->readonly)
3258 for (i = 0; i < (unsigned)deps->max_reg; i++)
3259 {
3260 struct deps_reg *reg_last = &deps->reg_last[i];
3261 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
3262 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
3263 }
3264
3265 /* Flush pending lists on jumps, but not on speculative checks. */
3266 if (JUMP_P (insn) && !(sel_sched_p ()
3267 && sel_insn_is_speculation_check (insn)))
3268 flush_pending_lists (deps, insn, true, true);
3269
3270 reg_pending_barrier = NOT_A_BARRIER;
3271 }
3272
3273 /* If a post-call group is still open, see if it should remain so.
3274 This insn must be a simple move of a hard reg to a pseudo or
3275 vice-versa.
3276
3277 We must avoid moving these insns for correctness on targets
3278 with small register classes, and for special registers like
3279 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
3280 hard regs for all targets. */
3281
3282 if (deps->in_post_call_group_p)
3283 {
3284 rtx tmp, set = single_set (insn);
3285 int src_regno, dest_regno;
3286
3287 if (set == NULL)
3288 {
3289 if (DEBUG_INSN_P (insn))
3290 /* We don't want to mark debug insns as part of the same
3291 sched group. We know they really aren't, but if we use
3292 debug insns to tell that a call group is over, we'll
3293 get different code if debug insns are not there and
3294 instructions that follow seem like they should be part
3295 of the call group.
3296
3297 Also, if we did, chain_to_prev_insn would move the
3298 deps of the debug insn to the call insn, modifying
3299 non-debug post-dependency counts of the debug insn
3300 dependencies and otherwise messing with the scheduling
3301 order.
3302
3303 Instead, let such debug insns be scheduled freely, but
3304 keep the call group open in case there are insns that
3305 should be part of it afterwards. Since we grant debug
3306 insns higher priority than even sched group insns, it
3307 will all turn out all right. */
3308 goto debug_dont_end_call_group;
3309 else
3310 goto end_call_group;
3311 }
3312
3313 tmp = SET_DEST (set);
3314 if (GET_CODE (tmp) == SUBREG)
3315 tmp = SUBREG_REG (tmp);
3316 if (REG_P (tmp))
3317 dest_regno = REGNO (tmp);
3318 else
3319 goto end_call_group;
3320
3321 tmp = SET_SRC (set);
3322 if (GET_CODE (tmp) == SUBREG)
3323 tmp = SUBREG_REG (tmp);
3324 if ((GET_CODE (tmp) == PLUS
3325 || GET_CODE (tmp) == MINUS)
3326 && REG_P (XEXP (tmp, 0))
3327 && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
3328 && dest_regno == STACK_POINTER_REGNUM)
3329 src_regno = STACK_POINTER_REGNUM;
3330 else if (REG_P (tmp))
3331 src_regno = REGNO (tmp);
3332 else
3333 goto end_call_group;
3334
3335 if (src_regno < FIRST_PSEUDO_REGISTER
3336 || dest_regno < FIRST_PSEUDO_REGISTER)
3337 {
3338 if (!deps->readonly
3339 && deps->in_post_call_group_p == post_call_initial)
3340 deps->in_post_call_group_p = post_call;
3341
3342 if (!sel_sched_p () || sched_emulate_haifa_p)
3343 {
3344 SCHED_GROUP_P (insn) = 1;
3345 CANT_MOVE (insn) = 1;
3346 }
3347 }
3348 else
3349 {
3350 end_call_group:
3351 if (!deps->readonly)
3352 deps->in_post_call_group_p = not_post_call;
3353 }
3354 }
3355
3356 debug_dont_end_call_group:
3357 if ((current_sched_info->flags & DO_SPECULATION)
3358 && !sched_insn_is_legitimate_for_speculation_p (insn, 0))
3359 /* INSN has an internal dependency (e.g. r14 = [r14]) and thus cannot
3360 be speculated. */
3361 {
3362 if (sel_sched_p ())
3363 sel_mark_hard_insn (insn);
3364 else
3365 {
3366 sd_iterator_def sd_it;
3367 dep_t dep;
3368
3369 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3370 sd_iterator_cond (&sd_it, &dep);)
3371 change_spec_dep_to_hard (sd_it);
3372 }
3373 }
3374 }
3375
3376 /* Return TRUE if INSN might not always return normally (e.g. call exit,
3377 longjmp, loop forever, ...). */
3378 /* FIXME: Why can't this function just use flags_from_decl_or_type and
3379 test for ECF_NORETURN? */
3380 static bool
3381 call_may_noreturn_p (rtx insn)
3382 {
3383 rtx call;
3384
3385 /* const or pure calls that aren't looping will always return. */
3386 if (RTL_CONST_OR_PURE_CALL_P (insn)
3387 && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn))
3388 return false;
3389
3390 call = PATTERN (insn);
3391 if (GET_CODE (call) == PARALLEL)
3392 call = XVECEXP (call, 0, 0);
3393 if (GET_CODE (call) == SET)
3394 call = SET_SRC (call);
3395 if (GET_CODE (call) == CALL
3396 && MEM_P (XEXP (call, 0))
3397 && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
3398 {
3399 rtx symbol = XEXP (XEXP (call, 0), 0);
3400 if (SYMBOL_REF_DECL (symbol)
3401 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
3402 {
3403 if (DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
3404 == BUILT_IN_NORMAL)
3405 switch (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol)))
3406 {
3407 case BUILT_IN_BCMP:
3408 case BUILT_IN_BCOPY:
3409 case BUILT_IN_BZERO:
3410 case BUILT_IN_INDEX:
3411 case BUILT_IN_MEMCHR:
3412 case BUILT_IN_MEMCMP:
3413 case BUILT_IN_MEMCPY:
3414 case BUILT_IN_MEMMOVE:
3415 case BUILT_IN_MEMPCPY:
3416 case BUILT_IN_MEMSET:
3417 case BUILT_IN_RINDEX:
3418 case BUILT_IN_STPCPY:
3419 case BUILT_IN_STPNCPY:
3420 case BUILT_IN_STRCAT:
3421 case BUILT_IN_STRCHR:
3422 case BUILT_IN_STRCMP:
3423 case BUILT_IN_STRCPY:
3424 case BUILT_IN_STRCSPN:
3425 case BUILT_IN_STRLEN:
3426 case BUILT_IN_STRNCAT:
3427 case BUILT_IN_STRNCMP:
3428 case BUILT_IN_STRNCPY:
3429 case BUILT_IN_STRPBRK:
3430 case BUILT_IN_STRRCHR:
3431 case BUILT_IN_STRSPN:
3432 case BUILT_IN_STRSTR:
3433 /* Assume certain string/memory builtins always return. */
3434 return false;
3435 default:
3436 break;
3437 }
3438 }
3439 }
3440
3441 /* For all other calls assume that they might not always return. */
3442 return true;
3443 }
3444
3445 /* Return true if INSN should be made dependent on the previous instruction
3446 group, and if all INSN's dependencies should be moved to the first
3447 instruction of that group. */
3448
3449 static bool
3450 chain_to_prev_insn_p (rtx insn)
3451 {
3452 rtx prev, x;
3453
3454 /* INSN forms a group with the previous instruction. */
3455 if (SCHED_GROUP_P (insn))
3456 return true;
3457
3458 /* If the previous instruction clobbers a register R and this one sets
3459 part of R, the clobber was added specifically to help us track the
3460 liveness of R. There's no point scheduling the clobber and leaving
3461 INSN behind, especially if we move the clobber to another block. */
3462 prev = prev_nonnote_nondebug_insn (insn);
3463 if (prev
3464 && INSN_P (prev)
3465 && BLOCK_FOR_INSN (prev) == BLOCK_FOR_INSN (insn)
3466 && GET_CODE (PATTERN (prev)) == CLOBBER)
3467 {
3468 x = XEXP (PATTERN (prev), 0);
3469 if (set_of (x, insn))
3470 return true;
3471 }
3472
3473 return false;
3474 }
3475
3476 /* Analyze INSN with DEPS as a context. */
3477 void
3478 deps_analyze_insn (struct deps_desc *deps, rtx insn)
3479 {
3480 if (sched_deps_info->start_insn)
3481 sched_deps_info->start_insn (insn);
3482
3483 /* Record the condition for this insn. */
3484 if (NONDEBUG_INSN_P (insn))
3485 {
3486 rtx t;
3487 sched_get_condition_with_rev (insn, NULL);
3488 t = INSN_CACHED_COND (insn);
3489 INSN_COND_DEPS (insn) = NULL_RTX;
3490 if (reload_completed
3491 && (current_sched_info->flags & DO_PREDICATION)
3492 && COMPARISON_P (t)
3493 && REG_P (XEXP (t, 0))
3494 && CONSTANT_P (XEXP (t, 1)))
3495 {
3496 unsigned int regno;
3497 int nregs;
3498 t = XEXP (t, 0);
3499 regno = REGNO (t);
3500 nregs = hard_regno_nregs[regno][GET_MODE (t)];
3501 t = NULL_RTX;
3502 while (nregs-- > 0)
3503 {
3504 struct deps_reg *reg_last = &deps->reg_last[regno + nregs];
3505 t = concat_INSN_LIST (reg_last->sets, t);
3506 t = concat_INSN_LIST (reg_last->clobbers, t);
3507 t = concat_INSN_LIST (reg_last->implicit_sets, t);
3508 }
3509 INSN_COND_DEPS (insn) = t;
3510 }
3511 }
3512
3513 if (JUMP_P (insn))
3514 {
3515 /* Make each JUMP_INSN (but not a speculative check)
3516 a scheduling barrier for memory references. */
3517 if (!deps->readonly
3518 && !(sel_sched_p ()
3519 && sel_insn_is_speculation_check (insn)))
3520 {
3521 /* Keep the list a reasonable size. */
3522 if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
3523 flush_pending_lists (deps, insn, true, true);
3524 else
3525 deps->pending_jump_insns
3526 = alloc_INSN_LIST (insn, deps->pending_jump_insns);
3527 }
3528
3529 /* For each insn which shouldn't cross a jump, add a dependence. */
3530 add_dependence_list_and_free (deps, insn,
3531 &deps->sched_before_next_jump, 1,
3532 REG_DEP_ANTI);
3533
3534 sched_analyze_insn (deps, PATTERN (insn), insn);
3535 }
3536 else if (NONJUMP_INSN_P (insn) || DEBUG_INSN_P (insn))
3537 {
3538 sched_analyze_insn (deps, PATTERN (insn), insn);
3539 }
3540 else if (CALL_P (insn))
3541 {
3542 int i;
3543
3544 CANT_MOVE (insn) = 1;
3545
3546 if (find_reg_note (insn, REG_SETJMP, NULL))
3547 {
3548 /* This is setjmp. Assume that all registers, not just
3549 hard registers, may be clobbered by this call. */
3550 reg_pending_barrier = MOVE_BARRIER;
3551 }
3552 else
3553 {
3554 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3555 /* A call may read and modify global register variables. */
3556 if (global_regs[i])
3557 {
3558 SET_REGNO_REG_SET (reg_pending_sets, i);
3559 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3560 }
3561 /* Other call-clobbered hard regs may be clobbered.
3562 Since we only have a choice between 'might be clobbered'
3563 and 'definitely not clobbered', we must include all
3564 partly call-clobbered registers here. */
3565 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
3566 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
3567 SET_REGNO_REG_SET (reg_pending_clobbers, i);
3568 /* We don't know what set of fixed registers might be used
3569 by the function, but it is certain that the stack pointer
3570 is among them, but be conservative. */
3571 else if (fixed_regs[i])
3572 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3573 /* The frame pointer is normally not used by the function
3574 itself, but by the debugger. */
3575 /* ??? MIPS o32 is an exception. It uses the frame pointer
3576 in the macro expansion of jal but does not represent this
3577 fact in the call_insn rtl. */
3578 else if (i == FRAME_POINTER_REGNUM
3579 || (i == HARD_FRAME_POINTER_REGNUM
3580 && (! reload_completed || frame_pointer_needed)))
3581 SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
3582 }
3583
3584 /* For each insn which shouldn't cross a call, add a dependence
3585 between that insn and this call insn. */
3586 add_dependence_list_and_free (deps, insn,
3587 &deps->sched_before_next_call, 1,
3588 REG_DEP_ANTI);
3589
3590 sched_analyze_insn (deps, PATTERN (insn), insn);
3591
3592 /* If CALL would be in a sched group, then this will violate
3593 convention that sched group insns have dependencies only on the
3594 previous instruction.
3595
3596 Of course one can say: "Hey! What about head of the sched group?"
3597 And I will answer: "Basic principles (one dep per insn) are always
3598 the same." */
3599 gcc_assert (!SCHED_GROUP_P (insn));
3600
3601 /* In the absence of interprocedural alias analysis, we must flush
3602 all pending reads and writes, and start new dependencies starting
3603 from here. But only flush writes for constant calls (which may
3604 be passed a pointer to something we haven't written yet). */
3605 flush_pending_lists (deps, insn, true, ! RTL_CONST_OR_PURE_CALL_P (insn));
3606
3607 if (!deps->readonly)
3608 {
3609 /* Remember the last function call for limiting lifetimes. */
3610 free_INSN_LIST_list (&deps->last_function_call);
3611 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
3612
3613 if (call_may_noreturn_p (insn))
3614 {
3615 /* Remember the last function call that might not always return
3616 normally for limiting moves of trapping insns. */
3617 free_INSN_LIST_list (&deps->last_function_call_may_noreturn);
3618 deps->last_function_call_may_noreturn
3619 = alloc_INSN_LIST (insn, NULL_RTX);
3620 }
3621
3622 /* Before reload, begin a post-call group, so as to keep the
3623 lifetimes of hard registers correct. */
3624 if (! reload_completed)
3625 deps->in_post_call_group_p = post_call;
3626 }
3627 }
3628
3629 if (sched_deps_info->use_cselib)
3630 cselib_process_insn (insn);
3631
3632 /* EH_REGION insn notes can not appear until well after we complete
3633 scheduling. */
3634 if (NOTE_P (insn))
3635 gcc_assert (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
3636 && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END);
3637
3638 if (sched_deps_info->finish_insn)
3639 sched_deps_info->finish_insn ();
3640
3641 /* Fixup the dependencies in the sched group. */
3642 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
3643 && chain_to_prev_insn_p (insn)
3644 && !sel_sched_p ())
3645 chain_to_prev_insn (insn);
3646 }
3647
3648 /* Initialize DEPS for the new block beginning with HEAD. */
3649 void
3650 deps_start_bb (struct deps_desc *deps, rtx head)
3651 {
3652 gcc_assert (!deps->readonly);
3653
3654 /* Before reload, if the previous block ended in a call, show that
3655 we are inside a post-call group, so as to keep the lifetimes of
3656 hard registers correct. */
3657 if (! reload_completed && !LABEL_P (head))
3658 {
3659 rtx insn = prev_nonnote_nondebug_insn (head);
3660
3661 if (insn && CALL_P (insn))
3662 deps->in_post_call_group_p = post_call_initial;
3663 }
3664 }
3665
3666 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
3667 dependencies for each insn. */
3668 void
3669 sched_analyze (struct deps_desc *deps, rtx head, rtx tail)
3670 {
3671 rtx insn;
3672
3673 if (sched_deps_info->use_cselib)
3674 cselib_init (CSELIB_RECORD_MEMORY);
3675
3676 deps_start_bb (deps, head);
3677
3678 for (insn = head;; insn = NEXT_INSN (insn))
3679 {
3680
3681 if (INSN_P (insn))
3682 {
3683 /* And initialize deps_lists. */
3684 sd_init_insn (insn);
3685 }
3686
3687 deps_analyze_insn (deps, insn);
3688
3689 if (insn == tail)
3690 {
3691 if (sched_deps_info->use_cselib)
3692 cselib_finish ();
3693 return;
3694 }
3695 }
3696 gcc_unreachable ();
3697 }
3698
3699 /* Helper for sched_free_deps ().
3700 Delete INSN's (RESOLVED_P) backward dependencies. */
3701 static void
3702 delete_dep_nodes_in_back_deps (rtx insn, bool resolved_p)
3703 {
3704 sd_iterator_def sd_it;
3705 dep_t dep;
3706 sd_list_types_def types;
3707
3708 if (resolved_p)
3709 types = SD_LIST_RES_BACK;
3710 else
3711 types = SD_LIST_BACK;
3712
3713 for (sd_it = sd_iterator_start (insn, types);
3714 sd_iterator_cond (&sd_it, &dep);)
3715 {
3716 dep_link_t link = *sd_it.linkp;
3717 dep_node_t node = DEP_LINK_NODE (link);
3718 deps_list_t back_list;
3719 deps_list_t forw_list;
3720
3721 get_back_and_forw_lists (dep, resolved_p, &back_list, &forw_list);
3722 remove_from_deps_list (link, back_list);
3723 delete_dep_node (node);
3724 }
3725 }
3726
3727 /* Delete (RESOLVED_P) dependencies between HEAD and TAIL together with
3728 deps_lists. */
3729 void
3730 sched_free_deps (rtx head, rtx tail, bool resolved_p)
3731 {
3732 rtx insn;
3733 rtx next_tail = NEXT_INSN (tail);
3734
3735 /* We make two passes since some insns may be scheduled before their
3736 dependencies are resolved. */
3737 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3738 if (INSN_P (insn) && INSN_LUID (insn) > 0)
3739 {
3740 /* Clear forward deps and leave the dep_nodes to the
3741 corresponding back_deps list. */
3742 if (resolved_p)
3743 clear_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
3744 else
3745 clear_deps_list (INSN_FORW_DEPS (insn));
3746 }
3747 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3748 if (INSN_P (insn) && INSN_LUID (insn) > 0)
3749 {
3750 /* Clear resolved back deps together with its dep_nodes. */
3751 delete_dep_nodes_in_back_deps (insn, resolved_p);
3752
3753 sd_finish_insn (insn);
3754 }
3755 }
3756 \f
3757 /* Initialize variables for region data dependence analysis.
3758 When LAZY_REG_LAST is true, do not allocate reg_last array
3759 of struct deps_desc immediately. */
3760
3761 void
3762 init_deps (struct deps_desc *deps, bool lazy_reg_last)
3763 {
3764 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
3765
3766 deps->max_reg = max_reg;
3767 if (lazy_reg_last)
3768 deps->reg_last = NULL;
3769 else
3770 deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
3771 INIT_REG_SET (&deps->reg_last_in_use);
3772
3773 deps->pending_read_insns = 0;
3774 deps->pending_read_mems = 0;
3775 deps->pending_write_insns = 0;
3776 deps->pending_write_mems = 0;
3777 deps->pending_jump_insns = 0;
3778 deps->pending_read_list_length = 0;
3779 deps->pending_write_list_length = 0;
3780 deps->pending_flush_length = 0;
3781 deps->last_pending_memory_flush = 0;
3782 deps->last_function_call = 0;
3783 deps->last_function_call_may_noreturn = 0;
3784 deps->sched_before_next_call = 0;
3785 deps->sched_before_next_jump = 0;
3786 deps->in_post_call_group_p = not_post_call;
3787 deps->last_debug_insn = 0;
3788 deps->last_reg_pending_barrier = NOT_A_BARRIER;
3789 deps->readonly = 0;
3790 }
3791
3792 /* Init only reg_last field of DEPS, which was not allocated before as
3793 we inited DEPS lazily. */
3794 void
3795 init_deps_reg_last (struct deps_desc *deps)
3796 {
3797 gcc_assert (deps && deps->max_reg > 0);
3798 gcc_assert (deps->reg_last == NULL);
3799
3800 deps->reg_last = XCNEWVEC (struct deps_reg, deps->max_reg);
3801 }
3802
3803
3804 /* Free insn lists found in DEPS. */
3805
3806 void
3807 free_deps (struct deps_desc *deps)
3808 {
3809 unsigned i;
3810 reg_set_iterator rsi;
3811
3812 /* We set max_reg to 0 when this context was already freed. */
3813 if (deps->max_reg == 0)
3814 {
3815 gcc_assert (deps->reg_last == NULL);
3816 return;
3817 }
3818 deps->max_reg = 0;
3819
3820 free_INSN_LIST_list (&deps->pending_read_insns);
3821 free_EXPR_LIST_list (&deps->pending_read_mems);
3822 free_INSN_LIST_list (&deps->pending_write_insns);
3823 free_EXPR_LIST_list (&deps->pending_write_mems);
3824 free_INSN_LIST_list (&deps->last_pending_memory_flush);
3825
3826 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
3827 times. For a testcase with 42000 regs and 8000 small basic blocks,
3828 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
3829 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3830 {
3831 struct deps_reg *reg_last = &deps->reg_last[i];
3832 if (reg_last->uses)
3833 free_INSN_LIST_list (&reg_last->uses);
3834 if (reg_last->sets)
3835 free_INSN_LIST_list (&reg_last->sets);
3836 if (reg_last->implicit_sets)
3837 free_INSN_LIST_list (&reg_last->implicit_sets);
3838 if (reg_last->control_uses)
3839 free_INSN_LIST_list (&reg_last->control_uses);
3840 if (reg_last->clobbers)
3841 free_INSN_LIST_list (&reg_last->clobbers);
3842 }
3843 CLEAR_REG_SET (&deps->reg_last_in_use);
3844
3845 /* As we initialize reg_last lazily, it is possible that we didn't allocate
3846 it at all. */
3847 free (deps->reg_last);
3848 deps->reg_last = NULL;
3849
3850 deps = NULL;
3851 }
3852
3853 /* Remove INSN from dependence contexts DEPS. */
3854 void
3855 remove_from_deps (struct deps_desc *deps, rtx insn)
3856 {
3857 int removed;
3858 unsigned i;
3859 reg_set_iterator rsi;
3860
3861 removed = remove_from_both_dependence_lists (insn, &deps->pending_read_insns,
3862 &deps->pending_read_mems);
3863 if (!DEBUG_INSN_P (insn))
3864 deps->pending_read_list_length -= removed;
3865 removed = remove_from_both_dependence_lists (insn, &deps->pending_write_insns,
3866 &deps->pending_write_mems);
3867 deps->pending_write_list_length -= removed;
3868
3869 removed = remove_from_dependence_list (insn, &deps->pending_jump_insns);
3870 deps->pending_flush_length -= removed;
3871 removed = remove_from_dependence_list (insn, &deps->last_pending_memory_flush);
3872 deps->pending_flush_length -= removed;
3873
3874 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
3875 {
3876 struct deps_reg *reg_last = &deps->reg_last[i];
3877 if (reg_last->uses)
3878 remove_from_dependence_list (insn, &reg_last->uses);
3879 if (reg_last->sets)
3880 remove_from_dependence_list (insn, &reg_last->sets);
3881 if (reg_last->implicit_sets)
3882 remove_from_dependence_list (insn, &reg_last->implicit_sets);
3883 if (reg_last->clobbers)
3884 remove_from_dependence_list (insn, &reg_last->clobbers);
3885 if (!reg_last->uses && !reg_last->sets && !reg_last->implicit_sets
3886 && !reg_last->clobbers)
3887 CLEAR_REGNO_REG_SET (&deps->reg_last_in_use, i);
3888 }
3889
3890 if (CALL_P (insn))
3891 {
3892 remove_from_dependence_list (insn, &deps->last_function_call);
3893 remove_from_dependence_list (insn,
3894 &deps->last_function_call_may_noreturn);
3895 }
3896 remove_from_dependence_list (insn, &deps->sched_before_next_call);
3897 }
3898
3899 /* Init deps data vector. */
3900 static void
3901 init_deps_data_vector (void)
3902 {
3903 int reserve = (sched_max_luid + 1
3904 - VEC_length (haifa_deps_insn_data_def, h_d_i_d));
3905 if (reserve > 0
3906 && ! VEC_space (haifa_deps_insn_data_def, h_d_i_d, reserve))
3907 VEC_safe_grow_cleared (haifa_deps_insn_data_def, heap, h_d_i_d,
3908 3 * sched_max_luid / 2);
3909 }
3910
3911 /* If it is profitable to use them, initialize or extend (depending on
3912 GLOBAL_P) dependency data. */
3913 void
3914 sched_deps_init (bool global_p)
3915 {
3916 /* Average number of insns in the basic block.
3917 '+ 1' is used to make it nonzero. */
3918 int insns_in_block = sched_max_luid / n_basic_blocks + 1;
3919
3920 init_deps_data_vector ();
3921
3922 /* We use another caching mechanism for selective scheduling, so
3923 we don't use this one. */
3924 if (!sel_sched_p () && global_p && insns_in_block > 100 * 5)
3925 {
3926 /* ?!? We could save some memory by computing a per-region luid mapping
3927 which could reduce both the number of vectors in the cache and the
3928 size of each vector. Instead we just avoid the cache entirely unless
3929 the average number of instructions in a basic block is very high. See
3930 the comment before the declaration of true_dependency_cache for
3931 what we consider "very high". */
3932 cache_size = 0;
3933 extend_dependency_caches (sched_max_luid, true);
3934 }
3935
3936 if (global_p)
3937 {
3938 dl_pool = create_alloc_pool ("deps_list", sizeof (struct _deps_list),
3939 /* Allocate lists for one block at a time. */
3940 insns_in_block);
3941 dn_pool = create_alloc_pool ("dep_node", sizeof (struct _dep_node),
3942 /* Allocate nodes for one block at a time.
3943 We assume that average insn has
3944 5 producers. */
3945 5 * insns_in_block);
3946 }
3947 }
3948
3949
3950 /* Create or extend (depending on CREATE_P) dependency caches to
3951 size N. */
3952 void
3953 extend_dependency_caches (int n, bool create_p)
3954 {
3955 if (create_p || true_dependency_cache)
3956 {
3957 int i, luid = cache_size + n;
3958
3959 true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
3960 luid);
3961 output_dependency_cache = XRESIZEVEC (bitmap_head,
3962 output_dependency_cache, luid);
3963 anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
3964 luid);
3965 control_dependency_cache = XRESIZEVEC (bitmap_head, control_dependency_cache,
3966 luid);
3967
3968 if (current_sched_info->flags & DO_SPECULATION)
3969 spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
3970 luid);
3971
3972 for (i = cache_size; i < luid; i++)
3973 {
3974 bitmap_initialize (&true_dependency_cache[i], 0);
3975 bitmap_initialize (&output_dependency_cache[i], 0);
3976 bitmap_initialize (&anti_dependency_cache[i], 0);
3977 bitmap_initialize (&control_dependency_cache[i], 0);
3978
3979 if (current_sched_info->flags & DO_SPECULATION)
3980 bitmap_initialize (&spec_dependency_cache[i], 0);
3981 }
3982 cache_size = luid;
3983 }
3984 }
3985
3986 /* Finalize dependency information for the whole function. */
3987 void
3988 sched_deps_finish (void)
3989 {
3990 gcc_assert (deps_pools_are_empty_p ());
3991 free_alloc_pool_if_empty (&dn_pool);
3992 free_alloc_pool_if_empty (&dl_pool);
3993 gcc_assert (dn_pool == NULL && dl_pool == NULL);
3994
3995 VEC_free (haifa_deps_insn_data_def, heap, h_d_i_d);
3996 cache_size = 0;
3997
3998 if (true_dependency_cache)
3999 {
4000 int i;
4001
4002 for (i = 0; i < cache_size; i++)
4003 {
4004 bitmap_clear (&true_dependency_cache[i]);
4005 bitmap_clear (&output_dependency_cache[i]);
4006 bitmap_clear (&anti_dependency_cache[i]);
4007 bitmap_clear (&control_dependency_cache[i]);
4008
4009 if (sched_deps_info->generate_spec_deps)
4010 bitmap_clear (&spec_dependency_cache[i]);
4011 }
4012 free (true_dependency_cache);
4013 true_dependency_cache = NULL;
4014 free (output_dependency_cache);
4015 output_dependency_cache = NULL;
4016 free (anti_dependency_cache);
4017 anti_dependency_cache = NULL;
4018 free (control_dependency_cache);
4019 control_dependency_cache = NULL;
4020
4021 if (sched_deps_info->generate_spec_deps)
4022 {
4023 free (spec_dependency_cache);
4024 spec_dependency_cache = NULL;
4025 }
4026
4027 }
4028 }
4029
4030 /* Initialize some global variables needed by the dependency analysis
4031 code. */
4032
4033 void
4034 init_deps_global (void)
4035 {
4036 CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
4037 CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
4038 reg_pending_sets = ALLOC_REG_SET (&reg_obstack);
4039 reg_pending_clobbers = ALLOC_REG_SET (&reg_obstack);
4040 reg_pending_uses = ALLOC_REG_SET (&reg_obstack);
4041 reg_pending_control_uses = ALLOC_REG_SET (&reg_obstack);
4042 reg_pending_barrier = NOT_A_BARRIER;
4043
4044 if (!sel_sched_p () || sched_emulate_haifa_p)
4045 {
4046 sched_deps_info->start_insn = haifa_start_insn;
4047 sched_deps_info->finish_insn = haifa_finish_insn;
4048
4049 sched_deps_info->note_reg_set = haifa_note_reg_set;
4050 sched_deps_info->note_reg_clobber = haifa_note_reg_clobber;
4051 sched_deps_info->note_reg_use = haifa_note_reg_use;
4052
4053 sched_deps_info->note_mem_dep = haifa_note_mem_dep;
4054 sched_deps_info->note_dep = haifa_note_dep;
4055 }
4056 }
4057
4058 /* Free everything used by the dependency analysis code. */
4059
4060 void
4061 finish_deps_global (void)
4062 {
4063 FREE_REG_SET (reg_pending_sets);
4064 FREE_REG_SET (reg_pending_clobbers);
4065 FREE_REG_SET (reg_pending_uses);
4066 FREE_REG_SET (reg_pending_control_uses);
4067 }
4068
4069 /* Estimate the weakness of dependence between MEM1 and MEM2. */
4070 dw_t
4071 estimate_dep_weak (rtx mem1, rtx mem2)
4072 {
4073 rtx r1, r2;
4074
4075 if (mem1 == mem2)
4076 /* MEMs are the same - don't speculate. */
4077 return MIN_DEP_WEAK;
4078
4079 r1 = XEXP (mem1, 0);
4080 r2 = XEXP (mem2, 0);
4081
4082 if (r1 == r2
4083 || (REG_P (r1) && REG_P (r2)
4084 && REGNO (r1) == REGNO (r2)))
4085 /* Again, MEMs are the same. */
4086 return MIN_DEP_WEAK;
4087 else if ((REG_P (r1) && !REG_P (r2))
4088 || (!REG_P (r1) && REG_P (r2)))
4089 /* Different addressing modes - reason to be more speculative,
4090 than usual. */
4091 return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
4092 else
4093 /* We can't say anything about the dependence. */
4094 return UNCERTAIN_DEP_WEAK;
4095 }
4096
4097 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
4098 This function can handle same INSN and ELEM (INSN == ELEM).
4099 It is a convenience wrapper. */
4100 static void
4101 add_dependence_1 (rtx insn, rtx elem, enum reg_note dep_type)
4102 {
4103 ds_t ds;
4104 bool internal;
4105
4106 if (dep_type == REG_DEP_TRUE)
4107 ds = DEP_TRUE;
4108 else if (dep_type == REG_DEP_OUTPUT)
4109 ds = DEP_OUTPUT;
4110 else if (dep_type == REG_DEP_CONTROL)
4111 ds = DEP_CONTROL;
4112 else
4113 {
4114 gcc_assert (dep_type == REG_DEP_ANTI);
4115 ds = DEP_ANTI;
4116 }
4117
4118 /* When add_dependence is called from inside sched-deps.c, we expect
4119 cur_insn to be non-null. */
4120 internal = cur_insn != NULL;
4121 if (internal)
4122 gcc_assert (insn == cur_insn);
4123 else
4124 cur_insn = insn;
4125
4126 note_dep (elem, ds);
4127 if (!internal)
4128 cur_insn = NULL;
4129 }
4130
4131 /* Return weakness of speculative type TYPE in the dep_status DS. */
4132 dw_t
4133 get_dep_weak_1 (ds_t ds, ds_t type)
4134 {
4135 ds = ds & type;
4136
4137 switch (type)
4138 {
4139 case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
4140 case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
4141 case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
4142 case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
4143 default: gcc_unreachable ();
4144 }
4145
4146 return (dw_t) ds;
4147 }
4148
4149 dw_t
4150 get_dep_weak (ds_t ds, ds_t type)
4151 {
4152 dw_t dw = get_dep_weak_1 (ds, type);
4153
4154 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
4155 return dw;
4156 }
4157
4158 /* Return the dep_status, which has the same parameters as DS, except for
4159 speculative type TYPE, that will have weakness DW. */
4160 ds_t
4161 set_dep_weak (ds_t ds, ds_t type, dw_t dw)
4162 {
4163 gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
4164
4165 ds &= ~type;
4166 switch (type)
4167 {
4168 case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
4169 case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
4170 case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
4171 case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
4172 default: gcc_unreachable ();
4173 }
4174 return ds;
4175 }
4176
4177 /* Return the join of two dep_statuses DS1 and DS2.
4178 If MAX_P is true then choose the greater probability,
4179 otherwise multiply probabilities.
4180 This function assumes that both DS1 and DS2 contain speculative bits. */
4181 static ds_t
4182 ds_merge_1 (ds_t ds1, ds_t ds2, bool max_p)
4183 {
4184 ds_t ds, t;
4185
4186 gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
4187
4188 ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
4189
4190 t = FIRST_SPEC_TYPE;
4191 do
4192 {
4193 if ((ds1 & t) && !(ds2 & t))
4194 ds |= ds1 & t;
4195 else if (!(ds1 & t) && (ds2 & t))
4196 ds |= ds2 & t;
4197 else if ((ds1 & t) && (ds2 & t))
4198 {
4199 dw_t dw1 = get_dep_weak (ds1, t);
4200 dw_t dw2 = get_dep_weak (ds2, t);
4201 ds_t dw;
4202
4203 if (!max_p)
4204 {
4205 dw = ((ds_t) dw1) * ((ds_t) dw2);
4206 dw /= MAX_DEP_WEAK;
4207 if (dw < MIN_DEP_WEAK)
4208 dw = MIN_DEP_WEAK;
4209 }
4210 else
4211 {
4212 if (dw1 >= dw2)
4213 dw = dw1;
4214 else
4215 dw = dw2;
4216 }
4217
4218 ds = set_dep_weak (ds, t, (dw_t) dw);
4219 }
4220
4221 if (t == LAST_SPEC_TYPE)
4222 break;
4223 t <<= SPEC_TYPE_SHIFT;
4224 }
4225 while (1);
4226
4227 return ds;
4228 }
4229
4230 /* Return the join of two dep_statuses DS1 and DS2.
4231 This function assumes that both DS1 and DS2 contain speculative bits. */
4232 ds_t
4233 ds_merge (ds_t ds1, ds_t ds2)
4234 {
4235 return ds_merge_1 (ds1, ds2, false);
4236 }
4237
4238 /* Return the join of two dep_statuses DS1 and DS2. */
4239 ds_t
4240 ds_full_merge (ds_t ds, ds_t ds2, rtx mem1, rtx mem2)
4241 {
4242 ds_t new_status = ds | ds2;
4243
4244 if (new_status & SPECULATIVE)
4245 {
4246 if ((ds && !(ds & SPECULATIVE))
4247 || (ds2 && !(ds2 & SPECULATIVE)))
4248 /* Then this dep can't be speculative. */
4249 new_status &= ~SPECULATIVE;
4250 else
4251 {
4252 /* Both are speculative. Merging probabilities. */
4253 if (mem1)
4254 {
4255 dw_t dw;
4256
4257 dw = estimate_dep_weak (mem1, mem2);
4258 ds = set_dep_weak (ds, BEGIN_DATA, dw);
4259 }
4260
4261 if (!ds)
4262 new_status = ds2;
4263 else if (!ds2)
4264 new_status = ds;
4265 else
4266 new_status = ds_merge (ds2, ds);
4267 }
4268 }
4269
4270 return new_status;
4271 }
4272
4273 /* Return the join of DS1 and DS2. Use maximum instead of multiplying
4274 probabilities. */
4275 ds_t
4276 ds_max_merge (ds_t ds1, ds_t ds2)
4277 {
4278 if (ds1 == 0 && ds2 == 0)
4279 return 0;
4280
4281 if (ds1 == 0 && ds2 != 0)
4282 return ds2;
4283
4284 if (ds1 != 0 && ds2 == 0)
4285 return ds1;
4286
4287 return ds_merge_1 (ds1, ds2, true);
4288 }
4289
4290 /* Return the probability of speculation success for the speculation
4291 status DS. */
4292 dw_t
4293 ds_weak (ds_t ds)
4294 {
4295 ds_t res = 1, dt;
4296 int n = 0;
4297
4298 dt = FIRST_SPEC_TYPE;
4299 do
4300 {
4301 if (ds & dt)
4302 {
4303 res *= (ds_t) get_dep_weak (ds, dt);
4304 n++;
4305 }
4306
4307 if (dt == LAST_SPEC_TYPE)
4308 break;
4309 dt <<= SPEC_TYPE_SHIFT;
4310 }
4311 while (1);
4312
4313 gcc_assert (n);
4314 while (--n)
4315 res /= MAX_DEP_WEAK;
4316
4317 if (res < MIN_DEP_WEAK)
4318 res = MIN_DEP_WEAK;
4319
4320 gcc_assert (res <= MAX_DEP_WEAK);
4321
4322 return (dw_t) res;
4323 }
4324
4325 /* Return a dep status that contains all speculation types of DS. */
4326 ds_t
4327 ds_get_speculation_types (ds_t ds)
4328 {
4329 if (ds & BEGIN_DATA)
4330 ds |= BEGIN_DATA;
4331 if (ds & BE_IN_DATA)
4332 ds |= BE_IN_DATA;
4333 if (ds & BEGIN_CONTROL)
4334 ds |= BEGIN_CONTROL;
4335 if (ds & BE_IN_CONTROL)
4336 ds |= BE_IN_CONTROL;
4337
4338 return ds & SPECULATIVE;
4339 }
4340
4341 /* Return a dep status that contains maximal weakness for each speculation
4342 type present in DS. */
4343 ds_t
4344 ds_get_max_dep_weak (ds_t ds)
4345 {
4346 if (ds & BEGIN_DATA)
4347 ds = set_dep_weak (ds, BEGIN_DATA, MAX_DEP_WEAK);
4348 if (ds & BE_IN_DATA)
4349 ds = set_dep_weak (ds, BE_IN_DATA, MAX_DEP_WEAK);
4350 if (ds & BEGIN_CONTROL)
4351 ds = set_dep_weak (ds, BEGIN_CONTROL, MAX_DEP_WEAK);
4352 if (ds & BE_IN_CONTROL)
4353 ds = set_dep_weak (ds, BE_IN_CONTROL, MAX_DEP_WEAK);
4354
4355 return ds;
4356 }
4357
4358 /* Dump information about the dependence status S. */
4359 static void
4360 dump_ds (FILE *f, ds_t s)
4361 {
4362 fprintf (f, "{");
4363
4364 if (s & BEGIN_DATA)
4365 fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak_1 (s, BEGIN_DATA));
4366 if (s & BE_IN_DATA)
4367 fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak_1 (s, BE_IN_DATA));
4368 if (s & BEGIN_CONTROL)
4369 fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak_1 (s, BEGIN_CONTROL));
4370 if (s & BE_IN_CONTROL)
4371 fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak_1 (s, BE_IN_CONTROL));
4372
4373 if (s & HARD_DEP)
4374 fprintf (f, "HARD_DEP; ");
4375
4376 if (s & DEP_TRUE)
4377 fprintf (f, "DEP_TRUE; ");
4378 if (s & DEP_OUTPUT)
4379 fprintf (f, "DEP_OUTPUT; ");
4380 if (s & DEP_ANTI)
4381 fprintf (f, "DEP_ANTI; ");
4382 if (s & DEP_CONTROL)
4383 fprintf (f, "DEP_CONTROL; ");
4384
4385 fprintf (f, "}");
4386 }
4387
4388 DEBUG_FUNCTION void
4389 debug_ds (ds_t s)
4390 {
4391 dump_ds (stderr, s);
4392 fprintf (stderr, "\n");
4393 }
4394
4395 #ifdef ENABLE_CHECKING
4396 /* Verify that dependence type and status are consistent.
4397 If RELAXED_P is true, then skip dep_weakness checks. */
4398 static void
4399 check_dep (dep_t dep, bool relaxed_p)
4400 {
4401 enum reg_note dt = DEP_TYPE (dep);
4402 ds_t ds = DEP_STATUS (dep);
4403
4404 gcc_assert (DEP_PRO (dep) != DEP_CON (dep));
4405
4406 if (!(current_sched_info->flags & USE_DEPS_LIST))
4407 {
4408 gcc_assert (ds == 0);
4409 return;
4410 }
4411
4412 /* Check that dependence type contains the same bits as the status. */
4413 if (dt == REG_DEP_TRUE)
4414 gcc_assert (ds & DEP_TRUE);
4415 else if (dt == REG_DEP_OUTPUT)
4416 gcc_assert ((ds & DEP_OUTPUT)
4417 && !(ds & DEP_TRUE));
4418 else if (dt == REG_DEP_ANTI)
4419 gcc_assert ((ds & DEP_ANTI)
4420 && !(ds & (DEP_OUTPUT | DEP_TRUE)));
4421 else
4422 gcc_assert (dt == REG_DEP_CONTROL
4423 && (ds & DEP_CONTROL)
4424 && !(ds & (DEP_OUTPUT | DEP_ANTI | DEP_TRUE)));
4425
4426 /* HARD_DEP can not appear in dep_status of a link. */
4427 gcc_assert (!(ds & HARD_DEP));
4428
4429 /* Check that dependence status is set correctly when speculation is not
4430 supported. */
4431 if (!sched_deps_info->generate_spec_deps)
4432 gcc_assert (!(ds & SPECULATIVE));
4433 else if (ds & SPECULATIVE)
4434 {
4435 if (!relaxed_p)
4436 {
4437 ds_t type = FIRST_SPEC_TYPE;
4438
4439 /* Check that dependence weakness is in proper range. */
4440 do
4441 {
4442 if (ds & type)
4443 get_dep_weak (ds, type);
4444
4445 if (type == LAST_SPEC_TYPE)
4446 break;
4447 type <<= SPEC_TYPE_SHIFT;
4448 }
4449 while (1);
4450 }
4451
4452 if (ds & BEGIN_SPEC)
4453 {
4454 /* Only true dependence can be data speculative. */
4455 if (ds & BEGIN_DATA)
4456 gcc_assert (ds & DEP_TRUE);
4457
4458 /* Control dependencies in the insn scheduler are represented by
4459 anti-dependencies, therefore only anti dependence can be
4460 control speculative. */
4461 if (ds & BEGIN_CONTROL)
4462 gcc_assert (ds & DEP_ANTI);
4463 }
4464 else
4465 {
4466 /* Subsequent speculations should resolve true dependencies. */
4467 gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
4468 }
4469
4470 /* Check that true and anti dependencies can't have other speculative
4471 statuses. */
4472 if (ds & DEP_TRUE)
4473 gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
4474 /* An output dependence can't be speculative at all. */
4475 gcc_assert (!(ds & DEP_OUTPUT));
4476 if (ds & DEP_ANTI)
4477 gcc_assert (ds & BEGIN_CONTROL);
4478 }
4479 }
4480 #endif /* ENABLE_CHECKING */
4481
4482 #endif /* INSN_SCHEDULING */