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