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