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