OSDN Git Service

gcc/testsuite/
[pf3gnuchains/gcc-fork.git] / gcc / sched-deps.c
1 /* Instruction scheduling pass.  This file computes dependencies between
2    instructions.
3    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5    Free Software Foundation, Inc.
6    Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
7    and currently maintained by, Jim Wilson (wilson@cygnus.com)
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24 \f
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "toplev.h"
30 #include "rtl.h"
31 #include "tm_p.h"
32 #include "hard-reg-set.h"
33 #include "regs.h"
34 #include "function.h"
35 #include "flags.h"
36 #include "insn-config.h"
37 #include "insn-attr.h"
38 #include "except.h"
39 #include "toplev.h"
40 #include "recog.h"
41 #include "sched-int.h"
42 #include "params.h"
43 #include "cselib.h"
44
45 #ifdef ENABLE_CHECKING
46 #define CHECK (true)
47 #else
48 #define CHECK (false)
49 #endif
50
51 /* Return the major type present in the DS.  */
52 enum reg_note
53 ds_to_dk (ds_t ds)
54 {
55   if (ds & DEP_TRUE)
56     return REG_DEP_TRUE;
57
58   if (ds & DEP_OUTPUT)
59     return REG_DEP_OUTPUT;
60
61   gcc_assert (ds & DEP_ANTI);
62
63   return REG_DEP_ANTI;
64 }
65
66 /* Return equivalent dep_status.  */
67 ds_t
68 dk_to_ds (enum reg_note dk)
69 {
70   switch (dk)
71     {
72     case REG_DEP_TRUE:
73       return DEP_TRUE;
74
75     case REG_DEP_OUTPUT:
76       return DEP_OUTPUT;
77
78     default:
79       gcc_assert (dk == REG_DEP_ANTI);
80       return DEP_ANTI;
81     }
82 }
83
84 /* Functions to operate with dependence information container - dep_t.  */
85
86 /* Init DEP with the arguments.  */
87 void
88 init_dep_1 (dep_t dep, rtx pro, rtx con, enum reg_note type, ds_t ds)
89 {
90   DEP_PRO (dep) = pro;
91   DEP_CON (dep) = con;
92   DEP_TYPE (dep) = type;
93   DEP_STATUS (dep) = ds;
94 }
95
96 /* Init DEP with the arguments.
97    While most of the scheduler (including targets) only need the major type
98    of the dependency, it is convenient to hide full dep_status from them.  */
99 void
100 init_dep (dep_t dep, rtx pro, rtx con, enum reg_note kind)
101 {
102   ds_t ds;
103
104   if ((current_sched_info->flags & USE_DEPS_LIST))
105     ds = dk_to_ds (kind);
106   else
107     ds = -1;
108
109   init_dep_1 (dep, pro, con, kind, ds);
110 }
111
112 /* Make a copy of FROM in TO.  */
113 static void
114 copy_dep (dep_t to, dep_t from)
115 {
116   memcpy (to, from, sizeof (*to));
117 }
118
119 static void dump_ds (FILE *, ds_t);
120
121 /* Define flags for dump_dep ().  */
122
123 /* Dump producer of the dependence.  */
124 #define DUMP_DEP_PRO (2)
125
126 /* Dump consumer of the dependence.  */
127 #define DUMP_DEP_CON (4)
128
129 /* Dump type of the dependence.  */
130 #define DUMP_DEP_TYPE (8)
131
132 /* Dump status of the dependence.  */
133 #define DUMP_DEP_STATUS (16)
134
135 /* Dump all information about the dependence.  */
136 #define DUMP_DEP_ALL (DUMP_DEP_PRO | DUMP_DEP_CON | DUMP_DEP_TYPE       \
137                       |DUMP_DEP_STATUS)
138
139 /* Dump DEP to DUMP.
140    FLAGS is a bit mask specifying what information about DEP needs
141    to be printed.
142    If FLAGS has the very first bit set, then dump all information about DEP
143    and propagate this bit into the callee dump functions.  */
144 static void
145 dump_dep (FILE *dump, dep_t dep, int flags)
146 {
147   if (flags & 1)
148     flags |= DUMP_DEP_ALL;
149
150   fprintf (dump, "<");
151
152   if (flags & DUMP_DEP_PRO)
153     fprintf (dump, "%d; ", INSN_UID (DEP_PRO (dep)));
154
155   if (flags & DUMP_DEP_CON)
156     fprintf (dump, "%d; ", INSN_UID (DEP_CON (dep)));
157
158   if (flags & DUMP_DEP_TYPE)
159     {
160       char t;
161       enum reg_note type = DEP_TYPE (dep);
162
163       switch (type)
164         {
165         case REG_DEP_TRUE:
166           t = 't';
167           break;
168
169         case REG_DEP_OUTPUT:
170           t = 'o';
171           break;
172
173         case REG_DEP_ANTI:
174           t = 'a';
175           break;
176
177         default:
178           gcc_unreachable ();
179           break;
180         }
181
182       fprintf (dump, "%c; ", t);
183     }
184
185   if (flags & DUMP_DEP_STATUS)
186     {
187       if (current_sched_info->flags & USE_DEPS_LIST)
188         dump_ds (dump, DEP_STATUS (dep));
189     }
190
191   fprintf (dump, ">");
192 }
193
194 /* Default flags for dump_dep ().  */
195 static int dump_dep_flags = (DUMP_DEP_PRO | DUMP_DEP_CON);
196
197 /* Dump all fields of DEP to STDERR.  */
198 void
199 sd_debug_dep (dep_t dep)
200 {
201   dump_dep (stderr, dep, 1);
202   fprintf (stderr, "\n");
203 }
204
205 /* Functions to operate with a single link from the dependencies lists -
206    dep_link_t.  */
207
208 /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
209    PREV_NEXT_P.  */
210 static void
211 attach_dep_link (dep_link_t l, dep_link_t *prev_nextp)
212 {
213   dep_link_t next = *prev_nextp;
214
215   gcc_assert (DEP_LINK_PREV_NEXTP (l) == NULL
216               && DEP_LINK_NEXT (l) == NULL);
217
218   /* Init node being inserted.  */
219   DEP_LINK_PREV_NEXTP (l) = prev_nextp;
220   DEP_LINK_NEXT (l) = next;
221
222   /* Fix next node.  */
223   if (next != NULL)
224     {
225       gcc_assert (DEP_LINK_PREV_NEXTP (next) == prev_nextp);
226
227       DEP_LINK_PREV_NEXTP (next) = &DEP_LINK_NEXT (l);
228     }
229
230   /* Fix prev node.  */
231   *prev_nextp = l;
232 }
233
234 /* Add dep_link LINK to deps_list L.  */
235 static void
236 add_to_deps_list (dep_link_t link, deps_list_t l)
237 {
238   attach_dep_link (link, &DEPS_LIST_FIRST (l));
239
240   ++DEPS_LIST_N_LINKS (l);
241 }
242
243 /* Detach dep_link L from the list.  */
244 static void
245 detach_dep_link (dep_link_t l)
246 {
247   dep_link_t *prev_nextp = DEP_LINK_PREV_NEXTP (l);
248   dep_link_t next = DEP_LINK_NEXT (l);
249
250   *prev_nextp = next;
251
252   if (next != NULL)
253     DEP_LINK_PREV_NEXTP (next) = prev_nextp;
254
255   DEP_LINK_PREV_NEXTP (l) = NULL;
256   DEP_LINK_NEXT (l) = NULL;
257 }
258
259 /* Remove link LINK from list LIST.  */
260 static void
261 remove_from_deps_list (dep_link_t link, deps_list_t list)
262 {
263   detach_dep_link (link);
264
265   --DEPS_LIST_N_LINKS (list);
266 }
267
268 /* Move link LINK from list FROM to list TO.  */
269 static void
270 move_dep_link (dep_link_t link, deps_list_t from, deps_list_t to)
271 {
272   remove_from_deps_list (link, from);
273   add_to_deps_list (link, to);
274 }
275
276 /* Return true of LINK is not attached to any list.  */
277 static bool
278 dep_link_is_detached_p (dep_link_t link)
279 {
280   return DEP_LINK_PREV_NEXTP (link) == NULL;
281 }
282
283 /* Pool to hold all dependency nodes (dep_node_t).  */
284 static alloc_pool dn_pool;
285
286 /* Number of dep_nodes out there.  */
287 static int dn_pool_diff = 0;
288
289 /* Create a dep_node.  */
290 static dep_node_t
291 create_dep_node (void)
292 {
293   dep_node_t n = (dep_node_t) pool_alloc (dn_pool);
294   dep_link_t back = DEP_NODE_BACK (n);
295   dep_link_t forw = DEP_NODE_FORW (n);
296
297   DEP_LINK_NODE (back) = n;
298   DEP_LINK_NEXT (back) = NULL;
299   DEP_LINK_PREV_NEXTP (back) = NULL;
300
301   DEP_LINK_NODE (forw) = n;
302   DEP_LINK_NEXT (forw) = NULL;
303   DEP_LINK_PREV_NEXTP (forw) = NULL;
304
305   ++dn_pool_diff;
306
307   return n;
308 }
309
310 /* Delete dep_node N.  N must not be connected to any deps_list.  */
311 static void
312 delete_dep_node (dep_node_t n)
313 {
314   gcc_assert (dep_link_is_detached_p (DEP_NODE_BACK (n))
315               && dep_link_is_detached_p (DEP_NODE_FORW (n)));
316
317   --dn_pool_diff;
318
319   pool_free (dn_pool, n);
320 }
321
322 /* Pool to hold dependencies lists (deps_list_t).  */
323 static alloc_pool dl_pool;
324
325 /* Number of deps_lists out there.  */
326 static int dl_pool_diff = 0;
327
328 /* Functions to operate with dependences lists - deps_list_t.  */
329
330 /* Return true if list L is empty.  */
331 static bool
332 deps_list_empty_p (deps_list_t l)
333 {
334   return DEPS_LIST_N_LINKS (l) == 0;
335 }
336
337 /* Create a new deps_list.  */
338 static deps_list_t
339 create_deps_list (void)
340 {
341   deps_list_t l = (deps_list_t) pool_alloc (dl_pool);
342
343   DEPS_LIST_FIRST (l) = NULL;
344   DEPS_LIST_N_LINKS (l) = 0;
345
346   ++dl_pool_diff;
347   return l;
348 }
349
350 /* Free deps_list L.  */
351 static void
352 free_deps_list (deps_list_t l)
353 {
354   gcc_assert (deps_list_empty_p (l));
355
356   --dl_pool_diff;
357
358   pool_free (dl_pool, l);
359 }
360
361 /* Return true if there is no dep_nodes and deps_lists out there.
362    After the region is scheduled all the dependency nodes and lists
363    should [generally] be returned to pool.  */
364 bool
365 deps_pools_are_empty_p (void)
366 {
367   return dn_pool_diff == 0 && dl_pool_diff == 0;
368 }
369
370 /* Remove all elements from L.  */
371 static void
372 clear_deps_list (deps_list_t l)
373 {
374   do
375     {
376       dep_link_t link = DEPS_LIST_FIRST (l);
377
378       if (link == NULL)
379         break;
380
381       remove_from_deps_list (link, l);
382     }
383   while (1);
384 }
385
386 static regset reg_pending_sets;
387 static regset reg_pending_clobbers;
388 static regset reg_pending_uses;
389
390 /* The following enumeration values tell us what dependencies we
391    should use to implement the barrier.  We use true-dependencies for
392    TRUE_BARRIER and anti-dependencies for MOVE_BARRIER.  */
393 enum reg_pending_barrier_mode
394 {
395   NOT_A_BARRIER = 0,
396   MOVE_BARRIER,
397   TRUE_BARRIER
398 };
399
400 static enum reg_pending_barrier_mode reg_pending_barrier;
401
402 /* To speed up the test for duplicate dependency links we keep a
403    record of dependencies created by add_dependence when the average
404    number of instructions in a basic block is very large.
405
406    Studies have shown that there is typically around 5 instructions between
407    branches for typical C code.  So we can make a guess that the average
408    basic block is approximately 5 instructions long; we will choose 100X
409    the average size as a very large basic block.
410
411    Each insn has associated bitmaps for its dependencies.  Each bitmap
412    has enough entries to represent a dependency on any other insn in
413    the insn chain.  All bitmap for true dependencies cache is
414    allocated then the rest two ones are also allocated.  */
415 static bitmap_head *true_dependency_cache;
416 static bitmap_head *output_dependency_cache;
417 static bitmap_head *anti_dependency_cache;
418 static bitmap_head *spec_dependency_cache;
419 static int cache_size;
420
421 static int deps_may_trap_p (const_rtx);
422 static void add_dependence_list (rtx, rtx, int, enum reg_note);
423 static void add_dependence_list_and_free (rtx, rtx *, int, enum reg_note);
424 static void delete_all_dependences (rtx);
425 static void fixup_sched_groups (rtx);
426
427 static void flush_pending_lists (struct deps *, rtx, int, int);
428 static void sched_analyze_1 (struct deps *, rtx, rtx);
429 static void sched_analyze_2 (struct deps *, rtx, rtx);
430 static void sched_analyze_insn (struct deps *, rtx, rtx);
431
432 static rtx sched_get_condition (const_rtx);
433 static int conditions_mutex_p (const_rtx, const_rtx);
434
435 static enum DEPS_ADJUST_RESULT maybe_add_or_update_dep_1 (dep_t, bool,
436                                                           rtx, rtx);
437 static enum DEPS_ADJUST_RESULT add_or_update_dep_1 (dep_t, bool, rtx, rtx);
438
439 static dw_t estimate_dep_weak (rtx, rtx);
440 #ifdef INSN_SCHEDULING
441 #ifdef ENABLE_CHECKING
442 static void check_dep (dep_t, bool);
443 #endif
444 #endif
445 \f
446 /* Return nonzero if a load of the memory reference MEM can cause a trap.  */
447
448 static int
449 deps_may_trap_p (const_rtx mem)
450 {
451   const_rtx addr = XEXP (mem, 0);
452
453   if (REG_P (addr) && REGNO (addr) >= FIRST_PSEUDO_REGISTER)
454     {
455       const_rtx t = get_reg_known_value (REGNO (addr));
456       if (t)
457         addr = t;
458     }
459   return rtx_addr_can_trap_p (addr);
460 }
461 \f
462 /* Find the condition under which INSN is executed.  */
463
464 static rtx
465 sched_get_condition (const_rtx insn)
466 {
467   rtx pat = PATTERN (insn);
468   rtx src;
469
470   if (pat == 0)
471     return 0;
472
473   if (GET_CODE (pat) == COND_EXEC)
474     return COND_EXEC_TEST (pat);
475
476   if (!any_condjump_p (insn) || !onlyjump_p (insn))
477     return 0;
478
479   src = SET_SRC (pc_set (insn));
480
481   if (XEXP (src, 2) == pc_rtx)
482     return XEXP (src, 0);
483   else if (XEXP (src, 1) == pc_rtx)
484     {
485       rtx cond = XEXP (src, 0);
486       enum rtx_code revcode = reversed_comparison_code (cond, insn);
487
488       if (revcode == UNKNOWN)
489         return 0;
490       return gen_rtx_fmt_ee (revcode, GET_MODE (cond), XEXP (cond, 0),
491                              XEXP (cond, 1));
492     }
493
494   return 0;
495 }
496
497 \f
498 /* Return nonzero if conditions COND1 and COND2 can never be both true.  */
499
500 static int
501 conditions_mutex_p (const_rtx cond1, const_rtx cond2)
502 {
503   if (COMPARISON_P (cond1)
504       && COMPARISON_P (cond2)
505       && GET_CODE (cond1) == reversed_comparison_code (cond2, NULL)
506       && XEXP (cond1, 0) == XEXP (cond2, 0)
507       && XEXP (cond1, 1) == XEXP (cond2, 1))
508     return 1;
509   return 0;
510 }
511
512 /* Return true if insn1 and insn2 can never depend on one another because
513    the conditions under which they are executed are mutually exclusive.  */
514 bool
515 sched_insns_conditions_mutex_p (const_rtx insn1, const_rtx insn2)
516 {
517   rtx cond1, cond2;
518
519   /* df doesn't handle conditional lifetimes entirely correctly;
520      calls mess up the conditional lifetimes.  */
521   if (!CALL_P (insn1) && !CALL_P (insn2))
522     {
523       cond1 = sched_get_condition (insn1);
524       cond2 = sched_get_condition (insn2);
525       if (cond1 && cond2
526           && conditions_mutex_p (cond1, cond2)
527           /* Make sure first instruction doesn't affect condition of second
528              instruction if switched.  */
529           && !modified_in_p (cond1, insn2)
530           /* Make sure second instruction doesn't affect condition of first
531              instruction if switched.  */
532           && !modified_in_p (cond2, insn1))
533         return true;
534     }
535   return false;
536 }
537 \f
538
539 /* Initialize LIST_PTR to point to one of the lists present in TYPES_PTR,
540    initialize RESOLVED_P_PTR with true if that list consists of resolved deps,
541    and remove the type of returned [through LIST_PTR] list from TYPES_PTR.
542    This function is used to switch sd_iterator to the next list.
543    !!! For internal use only.  Might consider moving it to sched-int.h.  */
544 void
545 sd_next_list (const_rtx insn, sd_list_types_def *types_ptr,
546               deps_list_t *list_ptr, bool *resolved_p_ptr)
547 {
548   sd_list_types_def types = *types_ptr;
549
550   if (types & SD_LIST_HARD_BACK)
551     {
552       *list_ptr = INSN_HARD_BACK_DEPS (insn);
553       *resolved_p_ptr = false;
554       *types_ptr = types & ~SD_LIST_HARD_BACK;
555     }
556   else if (types & SD_LIST_SPEC_BACK)
557     {
558       *list_ptr = INSN_SPEC_BACK_DEPS (insn);
559       *resolved_p_ptr = false;
560       *types_ptr = types & ~SD_LIST_SPEC_BACK;
561     }
562   else if (types & SD_LIST_FORW)
563     {
564       *list_ptr = INSN_FORW_DEPS (insn);
565       *resolved_p_ptr = false;
566       *types_ptr = types & ~SD_LIST_FORW;
567     }
568   else if (types & SD_LIST_RES_BACK)
569     {
570       *list_ptr = INSN_RESOLVED_BACK_DEPS (insn);
571       *resolved_p_ptr = true;
572       *types_ptr = types & ~SD_LIST_RES_BACK;
573     }
574   else if (types & SD_LIST_RES_FORW)
575     {
576       *list_ptr = INSN_RESOLVED_FORW_DEPS (insn);
577       *resolved_p_ptr = true;
578       *types_ptr = types & ~SD_LIST_RES_FORW;
579     }
580   else
581     {
582       *list_ptr = NULL;
583       *resolved_p_ptr = false;
584       *types_ptr = SD_LIST_NONE;
585     }
586 }
587
588 /* Return the summary size of INSN's lists defined by LIST_TYPES.  */
589 int
590 sd_lists_size (const_rtx insn, sd_list_types_def list_types)
591 {
592   int size = 0;
593
594   while (list_types != SD_LIST_NONE)
595     {
596       deps_list_t list;
597       bool resolved_p;
598
599       sd_next_list (insn, &list_types, &list, &resolved_p);
600       size += DEPS_LIST_N_LINKS (list);
601     }
602
603   return size;
604 }
605
606 /* Return true if INSN's lists defined by LIST_TYPES are all empty.  */
607 bool
608 sd_lists_empty_p (const_rtx insn, sd_list_types_def list_types)
609 {
610   return sd_lists_size (insn, list_types) == 0;
611 }
612
613 /* Initialize data for INSN.  */
614 void
615 sd_init_insn (rtx insn)
616 {
617   INSN_HARD_BACK_DEPS (insn) = create_deps_list ();
618   INSN_SPEC_BACK_DEPS (insn) = create_deps_list ();
619   INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list ();
620   INSN_FORW_DEPS (insn) = create_deps_list ();
621   INSN_RESOLVED_FORW_DEPS (insn) = create_deps_list ();
622
623   /* ??? It would be nice to allocate dependency caches here.  */
624 }
625
626 /* Free data for INSN.  */
627 void
628 sd_finish_insn (rtx insn)
629 {
630   /* ??? It would be nice to deallocate dependency caches here.  */
631
632   free_deps_list (INSN_HARD_BACK_DEPS (insn));
633   INSN_HARD_BACK_DEPS (insn) = NULL;
634
635   free_deps_list (INSN_SPEC_BACK_DEPS (insn));
636   INSN_SPEC_BACK_DEPS (insn) = NULL;
637
638   free_deps_list (INSN_RESOLVED_BACK_DEPS (insn));
639   INSN_RESOLVED_BACK_DEPS (insn) = NULL;
640
641   free_deps_list (INSN_FORW_DEPS (insn));
642   INSN_FORW_DEPS (insn) = NULL;
643
644   free_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
645   INSN_RESOLVED_FORW_DEPS (insn) = NULL;
646 }
647
648 /* Find a dependency between producer PRO and consumer CON.
649    Search through resolved dependency lists if RESOLVED_P is true.
650    If no such dependency is found return NULL,
651    otherwise return the dependency and initialize SD_IT_PTR [if it is nonnull]
652    with an iterator pointing to it.  */
653 static dep_t
654 sd_find_dep_between_no_cache (rtx pro, rtx con, bool resolved_p,
655                               sd_iterator_def *sd_it_ptr)
656 {
657   sd_list_types_def pro_list_type;
658   sd_list_types_def con_list_type;
659   sd_iterator_def sd_it;
660   dep_t dep;
661   bool found_p = false;
662
663   if (resolved_p)
664     {
665       pro_list_type = SD_LIST_RES_FORW;
666       con_list_type = SD_LIST_RES_BACK;
667     }
668   else
669     {
670       pro_list_type = SD_LIST_FORW;
671       con_list_type = SD_LIST_BACK;
672     }
673
674   /* Walk through either back list of INSN or forw list of ELEM
675      depending on which one is shorter.  */
676   if (sd_lists_size (con, con_list_type) < sd_lists_size (pro, pro_list_type))
677     {
678       /* Find the dep_link with producer PRO in consumer's back_deps.  */
679       FOR_EACH_DEP (con, con_list_type, sd_it, dep)
680         if (DEP_PRO (dep) == pro)
681           {
682             found_p = true;
683             break;
684           }
685     }
686   else
687     {
688       /* Find the dep_link with consumer CON in producer's forw_deps.  */
689       FOR_EACH_DEP (pro, pro_list_type, sd_it, dep)
690         if (DEP_CON (dep) == con)
691           {
692             found_p = true;
693             break;
694           }
695     }
696
697   if (found_p)
698     {
699       if (sd_it_ptr != NULL)
700         *sd_it_ptr = sd_it;
701
702       return dep;
703     }
704
705   return NULL;
706 }
707
708 /* Find a dependency between producer PRO and consumer CON.
709    Use dependency [if available] to check if dependency is present at all.
710    Search through resolved dependency lists if RESOLVED_P is true.
711    If the dependency or NULL if none found.  */
712 dep_t
713 sd_find_dep_between (rtx pro, rtx con, bool resolved_p)
714 {
715   if (true_dependency_cache != NULL)
716     /* Avoiding the list walk below can cut compile times dramatically
717        for some code.  */
718     {
719       int elem_luid = INSN_LUID (pro);
720       int insn_luid = INSN_LUID (con);
721
722       gcc_assert (output_dependency_cache != NULL
723                   && anti_dependency_cache != NULL);
724
725       if (!bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid)
726           && !bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid)
727           && !bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
728         return NULL;
729     }
730
731   return sd_find_dep_between_no_cache (pro, con, resolved_p, NULL);
732 }
733
734 /* Add or update  a dependence described by DEP.
735    MEM1 and MEM2, if non-null, correspond to memory locations in case of
736    data speculation.
737
738    The function returns a value indicating if an old entry has been changed
739    or a new entry has been added to insn's backward deps.
740
741    This function merely checks if producer and consumer is the same insn
742    and doesn't create a dep in this case.  Actual manipulation of
743    dependence data structures is performed in add_or_update_dep_1.  */
744 static enum DEPS_ADJUST_RESULT
745 maybe_add_or_update_dep_1 (dep_t dep, bool resolved_p, rtx mem1, rtx mem2)
746 {
747   rtx elem = DEP_PRO (dep);
748   rtx insn = DEP_CON (dep);
749
750   gcc_assert (INSN_P (insn) && INSN_P (elem));
751
752   /* Don't depend an insn on itself.  */
753   if (insn == elem)
754     {
755 #ifdef INSN_SCHEDULING
756       if (current_sched_info->flags & DO_SPECULATION)
757         /* INSN has an internal dependence, which we can't overcome.  */
758         HAS_INTERNAL_DEP (insn) = 1;
759 #endif
760
761       return DEP_NODEP;
762     }
763
764   return add_or_update_dep_1 (dep, resolved_p, mem1, mem2);
765 }
766
767 #ifdef INSN_SCHEDULING
768 /* Ask dependency caches what needs to be done for dependence DEP.
769    Return DEP_CREATED if new dependence should be created and there is no
770    need to try to find one searching the dependencies lists.
771    Return DEP_PRESENT if there already is a dependence described by DEP and
772    hence nothing is to be done.
773    Return DEP_CHANGED if there already is a dependence, but it should be
774    updated to incorporate additional information from DEP.  */
775 static enum DEPS_ADJUST_RESULT
776 ask_dependency_caches (dep_t dep)
777 {
778   int elem_luid = INSN_LUID (DEP_PRO (dep));
779   int insn_luid = INSN_LUID (DEP_CON (dep));
780
781   gcc_assert (true_dependency_cache != NULL
782               && output_dependency_cache != NULL
783               && anti_dependency_cache != NULL);
784
785   if (!(current_sched_info->flags & USE_DEPS_LIST))
786     {          
787       enum reg_note present_dep_type;
788
789       if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
790         present_dep_type = REG_DEP_TRUE;
791       else if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
792         present_dep_type = REG_DEP_OUTPUT;
793       else if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
794         present_dep_type = REG_DEP_ANTI;
795       else
796         /* There is no existing dep so it should be created.  */
797         return DEP_CREATED;
798
799       if ((int) DEP_TYPE (dep) >= (int) present_dep_type)
800         /* DEP does not add anything to the existing dependence.  */
801         return DEP_PRESENT;
802     }
803   else
804     {      
805       ds_t present_dep_types = 0;
806           
807       if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
808         present_dep_types |= DEP_TRUE;
809       if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
810         present_dep_types |= DEP_OUTPUT;
811       if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
812         present_dep_types |= DEP_ANTI;
813
814       if (present_dep_types == 0)
815         /* There is no existing dep so it should be created.  */
816         return DEP_CREATED;
817
818       if (!(current_sched_info->flags & DO_SPECULATION)
819           || !bitmap_bit_p (&spec_dependency_cache[insn_luid], elem_luid))
820         {
821           if ((present_dep_types | (DEP_STATUS (dep) & DEP_TYPES))
822               == present_dep_types)
823             /* DEP does not add anything to the existing dependence.  */
824             return DEP_PRESENT;
825         }
826       else
827         {
828           /* Only true dependencies can be data speculative and
829              only anti dependencies can be control speculative.  */
830           gcc_assert ((present_dep_types & (DEP_TRUE | DEP_ANTI))
831                       == present_dep_types);
832
833           /* if (DEP is SPECULATIVE) then
834              ..we should update DEP_STATUS
835              else
836              ..we should reset existing dep to non-speculative.  */
837         }
838     }
839
840   return DEP_CHANGED;
841 }
842
843 /* Set dependency caches according to DEP.  */
844 static void
845 set_dependency_caches (dep_t dep)
846 {
847   int elem_luid = INSN_LUID (DEP_PRO (dep));
848   int insn_luid = INSN_LUID (DEP_CON (dep));
849
850   if (!(current_sched_info->flags & USE_DEPS_LIST))
851     {
852       switch (DEP_TYPE (dep))
853         {
854         case REG_DEP_TRUE:
855           bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
856           break;
857
858         case REG_DEP_OUTPUT:
859           bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
860           break;
861
862         case REG_DEP_ANTI:
863           bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
864           break;
865
866         default:
867           gcc_unreachable ();
868         }
869     }
870   else
871     {
872       ds_t ds = DEP_STATUS (dep);
873
874       if (ds & DEP_TRUE)
875         bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
876       if (ds & DEP_OUTPUT)
877         bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
878       if (ds & DEP_ANTI)
879         bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
880
881       if (ds & SPECULATIVE)
882         {
883           gcc_assert (current_sched_info->flags & DO_SPECULATION);
884           bitmap_set_bit (&spec_dependency_cache[insn_luid], elem_luid);
885         }
886     }
887 }
888
889 /* Type of dependence DEP have changed from OLD_TYPE.  Update dependency
890    caches accordingly.  */
891 static void
892 update_dependency_caches (dep_t dep, enum reg_note old_type)
893 {
894   int elem_luid = INSN_LUID (DEP_PRO (dep));
895   int insn_luid = INSN_LUID (DEP_CON (dep));
896
897   /* Clear corresponding cache entry because type of the link
898      may have changed.  Keep them if we use_deps_list.  */
899   if (!(current_sched_info->flags & USE_DEPS_LIST))
900     {
901       switch (old_type)
902         {
903         case REG_DEP_OUTPUT:
904           bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
905           break;
906
907         case REG_DEP_ANTI:
908           bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
909           break;
910
911         default:
912           gcc_unreachable ();                        
913         }
914     }
915
916   set_dependency_caches (dep);
917 }
918
919 /* Convert a dependence pointed to by SD_IT to be non-speculative.  */
920 static void
921 change_spec_dep_to_hard (sd_iterator_def sd_it)
922 {
923   dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
924   dep_link_t link = DEP_NODE_BACK (node);
925   dep_t dep = DEP_NODE_DEP (node);
926   rtx elem = DEP_PRO (dep);
927   rtx insn = DEP_CON (dep);
928
929   move_dep_link (link, INSN_SPEC_BACK_DEPS (insn), INSN_HARD_BACK_DEPS (insn));
930
931   DEP_STATUS (dep) &= ~SPECULATIVE;
932
933   if (true_dependency_cache != NULL)
934     /* Clear the cache entry.  */
935     bitmap_clear_bit (&spec_dependency_cache[INSN_LUID (insn)],
936                       INSN_LUID (elem));
937 }
938 #endif
939
940 /* Update DEP to incorporate information from NEW_DEP.
941    SD_IT points to DEP in case it should be moved to another list.
942    MEM1 and MEM2, if nonnull, correspond to memory locations in case if
943    data-speculative dependence should be updated.  */
944 static enum DEPS_ADJUST_RESULT
945 update_dep (dep_t dep, dep_t new_dep,
946             sd_iterator_def sd_it ATTRIBUTE_UNUSED,
947             rtx mem1 ATTRIBUTE_UNUSED,
948             rtx mem2 ATTRIBUTE_UNUSED)
949 {
950   enum DEPS_ADJUST_RESULT res = DEP_PRESENT;
951   enum reg_note old_type = DEP_TYPE (dep);
952
953   /* If this is a more restrictive type of dependence than the
954      existing one, then change the existing dependence to this
955      type.  */
956   if ((int) DEP_TYPE (new_dep) < (int) old_type)
957     {
958       DEP_TYPE (dep) = DEP_TYPE (new_dep);
959       res = DEP_CHANGED;
960     }
961
962 #ifdef INSN_SCHEDULING
963   if (current_sched_info->flags & USE_DEPS_LIST)
964     /* Update DEP_STATUS.  */
965     {
966       ds_t dep_status = DEP_STATUS (dep);
967       ds_t ds = DEP_STATUS (new_dep);
968       ds_t new_status = ds | dep_status;
969
970       if (new_status & SPECULATIVE)
971         /* Either existing dep or a dep we're adding or both are
972            speculative.  */
973         {
974           if (!(ds & SPECULATIVE)
975               || !(dep_status & SPECULATIVE))
976             /* The new dep can't be speculative.  */
977             {
978               new_status &= ~SPECULATIVE;
979
980               if (dep_status & SPECULATIVE)
981                 /* The old dep was speculative, but now it
982                    isn't.  */
983                 change_spec_dep_to_hard (sd_it);
984             }
985           else
986             {
987               /* Both are speculative.  Merge probabilities.  */
988               if (mem1 != NULL)
989                 {
990                   dw_t dw;
991
992                   dw = estimate_dep_weak (mem1, mem2);
993                   ds = set_dep_weak (ds, BEGIN_DATA, dw);
994                 }
995                                                          
996               new_status = ds_merge (dep_status, ds);
997             }
998         }
999
1000       ds = new_status;
1001
1002       if (dep_status != ds)
1003         {
1004           DEP_STATUS (dep) = ds;
1005           res = DEP_CHANGED;
1006         }
1007     }
1008
1009   if (true_dependency_cache != NULL
1010       && res == DEP_CHANGED)
1011     update_dependency_caches (dep, old_type);
1012 #endif
1013
1014   return res;
1015 }
1016
1017 /* Add or update  a dependence described by DEP.
1018    MEM1 and MEM2, if non-null, correspond to memory locations in case of
1019    data speculation.
1020
1021    The function returns a value indicating if an old entry has been changed
1022    or a new entry has been added to insn's backward deps or nothing has
1023    been updated at all.  */
1024 static enum DEPS_ADJUST_RESULT
1025 add_or_update_dep_1 (dep_t new_dep, bool resolved_p,
1026                      rtx mem1 ATTRIBUTE_UNUSED, rtx mem2 ATTRIBUTE_UNUSED)
1027 {
1028   bool maybe_present_p = true;
1029   bool present_p = false;
1030
1031   gcc_assert (INSN_P (DEP_PRO (new_dep)) && INSN_P (DEP_CON (new_dep))
1032               && DEP_PRO (new_dep) != DEP_CON (new_dep));
1033   
1034 #ifdef INSN_SCHEDULING
1035
1036 #ifdef ENABLE_CHECKING
1037   check_dep (new_dep, mem1 != NULL);
1038 #endif
1039
1040   if (true_dependency_cache != NULL)
1041     {
1042       switch (ask_dependency_caches (new_dep))
1043         {
1044         case DEP_PRESENT:
1045           return DEP_PRESENT;
1046
1047         case DEP_CHANGED:
1048           maybe_present_p = true;
1049           present_p = true;
1050           break;
1051
1052         case DEP_CREATED:
1053           maybe_present_p = false;
1054           present_p = false;
1055           break;
1056
1057         default:
1058           gcc_unreachable ();
1059           break;
1060         }
1061     }
1062 #endif
1063
1064   /* Check that we don't already have this dependence.  */
1065   if (maybe_present_p)
1066     {
1067       dep_t present_dep;
1068       sd_iterator_def sd_it;
1069
1070       gcc_assert (true_dependency_cache == NULL || present_p);
1071
1072       present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
1073                                                   DEP_CON (new_dep),
1074                                                   resolved_p, &sd_it);
1075
1076       if (present_dep != NULL)
1077         /* We found an existing dependency between ELEM and INSN.  */
1078         return update_dep (present_dep, new_dep, sd_it, mem1, mem2);
1079       else
1080         /* We didn't find a dep, it shouldn't present in the cache.  */
1081         gcc_assert (!present_p);
1082     }
1083
1084   /* Might want to check one level of transitivity to save conses.
1085      This check should be done in maybe_add_or_update_dep_1.
1086      Since we made it to add_or_update_dep_1, we must create
1087      (or update) a link.  */
1088
1089   if (mem1 != NULL_RTX)
1090     {
1091       gcc_assert (current_sched_info->flags & DO_SPECULATION);
1092       DEP_STATUS (new_dep) = set_dep_weak (DEP_STATUS (new_dep), BEGIN_DATA,
1093                                            estimate_dep_weak (mem1, mem2));
1094     }
1095
1096   sd_add_dep (new_dep, resolved_p);
1097   
1098   return DEP_CREATED;
1099 }
1100
1101 /* Initialize BACK_LIST_PTR with consumer's backward list and
1102    FORW_LIST_PTR with producer's forward list.  If RESOLVED_P is true
1103    initialize with lists that hold resolved deps.  */
1104 static void
1105 get_back_and_forw_lists (dep_t dep, bool resolved_p,
1106                          deps_list_t *back_list_ptr,
1107                          deps_list_t *forw_list_ptr)
1108 {
1109   rtx con = DEP_CON (dep);
1110
1111   if (!resolved_p)
1112     {
1113       if ((current_sched_info->flags & DO_SPECULATION)
1114           && (DEP_STATUS (dep) & SPECULATIVE))
1115         *back_list_ptr = INSN_SPEC_BACK_DEPS (con);
1116       else
1117         *back_list_ptr = INSN_HARD_BACK_DEPS (con);
1118
1119       *forw_list_ptr = INSN_FORW_DEPS (DEP_PRO (dep));
1120     }
1121   else
1122     {
1123       *back_list_ptr = INSN_RESOLVED_BACK_DEPS (con);
1124       *forw_list_ptr = INSN_RESOLVED_FORW_DEPS (DEP_PRO (dep));
1125     }
1126 }
1127
1128 /* Add dependence described by DEP.
1129    If RESOLVED_P is true treat the dependence as a resolved one.  */
1130 void
1131 sd_add_dep (dep_t dep, bool resolved_p)
1132 {
1133   dep_node_t n = create_dep_node ();
1134   deps_list_t con_back_deps;
1135   deps_list_t pro_forw_deps;
1136   rtx elem = DEP_PRO (dep);
1137   rtx insn = DEP_CON (dep);
1138
1139   gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
1140
1141   if ((current_sched_info->flags & DO_SPECULATION)
1142       && !sched_insn_is_legitimate_for_speculation_p (insn, DEP_STATUS (dep)))
1143     DEP_STATUS (dep) &= ~SPECULATIVE;
1144
1145   copy_dep (DEP_NODE_DEP (n), dep);
1146
1147   get_back_and_forw_lists (dep, resolved_p, &con_back_deps, &pro_forw_deps);
1148
1149   add_to_deps_list (DEP_NODE_BACK (n), con_back_deps);
1150
1151 #ifdef INSN_SCHEDULING
1152 #ifdef ENABLE_CHECKING
1153   check_dep (dep, false);
1154 #endif
1155
1156   add_to_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1157
1158   /* If we are adding a dependency to INSN's LOG_LINKs, then note that
1159      in the bitmap caches of dependency information.  */
1160   if (true_dependency_cache != NULL)
1161     set_dependency_caches (dep);
1162 #endif
1163 }
1164
1165 /* Add or update backward dependence between INSN and ELEM
1166    with given type DEP_TYPE and dep_status DS.
1167    This function is a convenience wrapper.  */
1168 enum DEPS_ADJUST_RESULT
1169 sd_add_or_update_dep (dep_t dep, bool resolved_p)
1170 {
1171   return add_or_update_dep_1 (dep, resolved_p, NULL_RTX, NULL_RTX);
1172 }
1173
1174 /* Resolved dependence pointed to by SD_IT.
1175    SD_IT will advance to the next element.  */
1176 void
1177 sd_resolve_dep (sd_iterator_def sd_it)
1178 {
1179   dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
1180   dep_t dep = DEP_NODE_DEP (node);
1181   rtx pro = DEP_PRO (dep);
1182   rtx con = DEP_CON (dep);
1183
1184   if ((current_sched_info->flags & DO_SPECULATION)
1185       && (DEP_STATUS (dep) & SPECULATIVE))
1186     move_dep_link (DEP_NODE_BACK (node), INSN_SPEC_BACK_DEPS (con),
1187                    INSN_RESOLVED_BACK_DEPS (con));
1188   else
1189     move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
1190                    INSN_RESOLVED_BACK_DEPS (con));
1191
1192   move_dep_link (DEP_NODE_FORW (node), INSN_FORW_DEPS (pro),
1193                  INSN_RESOLVED_FORW_DEPS (pro));
1194 }
1195
1196 /* Make TO depend on all the FROM's producers.
1197    If RESOLVED_P is true add dependencies to the resolved lists.  */
1198 void
1199 sd_copy_back_deps (rtx to, rtx from, bool resolved_p)
1200 {
1201   sd_list_types_def list_type;
1202   sd_iterator_def sd_it;
1203   dep_t dep;
1204
1205   list_type = resolved_p ? SD_LIST_RES_BACK : SD_LIST_BACK;
1206
1207   FOR_EACH_DEP (from, list_type, sd_it, dep)
1208     {
1209       dep_def _new_dep, *new_dep = &_new_dep;
1210
1211       copy_dep (new_dep, dep);
1212       DEP_CON (new_dep) = to;
1213       sd_add_dep (new_dep, resolved_p);
1214     }
1215 }
1216
1217 /* Remove a dependency referred to by SD_IT.
1218    SD_IT will point to the next dependence after removal.  */
1219 void
1220 sd_delete_dep (sd_iterator_def sd_it)
1221 {
1222   dep_node_t n = DEP_LINK_NODE (*sd_it.linkp);
1223   dep_t dep = DEP_NODE_DEP (n);
1224   rtx pro = DEP_PRO (dep);
1225   rtx con = DEP_CON (dep);
1226   deps_list_t con_back_deps;
1227   deps_list_t pro_forw_deps;
1228
1229   if (true_dependency_cache != NULL)
1230     {
1231       int elem_luid = INSN_LUID (pro);
1232       int insn_luid = INSN_LUID (con);
1233
1234       bitmap_clear_bit (&true_dependency_cache[insn_luid], elem_luid);
1235       bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
1236       bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
1237
1238       if (current_sched_info->flags & DO_SPECULATION)
1239         bitmap_clear_bit (&spec_dependency_cache[insn_luid], elem_luid);
1240     }
1241
1242   get_back_and_forw_lists (dep, sd_it.resolved_p,
1243                            &con_back_deps, &pro_forw_deps);
1244
1245   remove_from_deps_list (DEP_NODE_BACK (n), con_back_deps);
1246   remove_from_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
1247
1248   delete_dep_node (n);
1249 }
1250
1251 /* Dump size of the lists.  */
1252 #define DUMP_LISTS_SIZE (2)
1253
1254 /* Dump dependencies of the lists.  */
1255 #define DUMP_LISTS_DEPS (4)
1256
1257 /* Dump all information about the lists.  */
1258 #define DUMP_LISTS_ALL (DUMP_LISTS_SIZE | DUMP_LISTS_DEPS)
1259
1260 /* Dump deps_lists of INSN specified by TYPES to DUMP.
1261    FLAGS is a bit mask specifying what information about the lists needs
1262    to be printed.
1263    If FLAGS has the very first bit set, then dump all information about
1264    the lists and propagate this bit into the callee dump functions.  */
1265 static void
1266 dump_lists (FILE *dump, rtx insn, sd_list_types_def types, int flags)
1267 {
1268   sd_iterator_def sd_it;
1269   dep_t dep;
1270   int all;
1271
1272   all = (flags & 1);
1273
1274   if (all)
1275     flags |= DUMP_LISTS_ALL;
1276
1277   fprintf (dump, "[");
1278
1279   if (flags & DUMP_LISTS_SIZE)
1280     fprintf (dump, "%d; ", sd_lists_size (insn, types));
1281
1282   if (flags & DUMP_LISTS_DEPS)
1283     {
1284       FOR_EACH_DEP (insn, types, sd_it, dep)
1285         {
1286           dump_dep (dump, dep, dump_dep_flags | all);
1287           fprintf (dump, " ");
1288         }
1289     }
1290 }
1291
1292 /* Dump all information about deps_lists of INSN specified by TYPES
1293    to STDERR.  */
1294 void
1295 sd_debug_lists (rtx insn, sd_list_types_def types)
1296 {
1297   dump_lists (stderr, insn, types, 1);
1298   fprintf (stderr, "\n");
1299 }
1300
1301 /* A convenience wrapper to operate on an entire list.  */
1302
1303 static void
1304 add_dependence_list (rtx insn, rtx list, int uncond, enum reg_note dep_type)
1305 {
1306   for (; list; list = XEXP (list, 1))
1307     {
1308       if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1309         add_dependence (insn, XEXP (list, 0), dep_type);
1310     }
1311 }
1312
1313 /* Similar, but free *LISTP at the same time.  */
1314
1315 static void
1316 add_dependence_list_and_free (rtx insn, rtx *listp, int uncond,
1317                               enum reg_note dep_type)
1318 {
1319   rtx list, next;
1320   for (list = *listp, *listp = NULL; list ; list = next)
1321     {
1322       next = XEXP (list, 1);
1323       if (uncond || ! sched_insns_conditions_mutex_p (insn, XEXP (list, 0)))
1324         add_dependence (insn, XEXP (list, 0), dep_type);
1325       free_INSN_LIST_node (list);
1326     }
1327 }
1328
1329 /* Clear all dependencies for an insn.  */
1330 static void
1331 delete_all_dependences (rtx insn)
1332 {
1333   sd_iterator_def sd_it;
1334   dep_t dep;
1335
1336   /* The below cycle can be optimized to clear the caches and back_deps
1337      in one call but that would provoke duplication of code from
1338      delete_dep ().  */
1339
1340   for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1341        sd_iterator_cond (&sd_it, &dep);)
1342     sd_delete_dep (sd_it);
1343 }
1344
1345 /* All insns in a scheduling group except the first should only have
1346    dependencies on the previous insn in the group.  So we find the
1347    first instruction in the scheduling group by walking the dependence
1348    chains backwards. Then we add the dependencies for the group to
1349    the previous nonnote insn.  */
1350
1351 static void
1352 fixup_sched_groups (rtx insn)
1353 {
1354   sd_iterator_def sd_it;
1355   dep_t dep;
1356   rtx prev_nonnote;
1357
1358   FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
1359     {
1360       rtx i = insn;
1361       rtx pro = DEP_PRO (dep);
1362
1363       do
1364         {
1365           i = prev_nonnote_insn (i);
1366
1367           if (pro == i)
1368             goto next_link;
1369         } while (SCHED_GROUP_P (i));
1370
1371       if (! sched_insns_conditions_mutex_p (i, pro))
1372         add_dependence (i, pro, DEP_TYPE (dep));
1373     next_link:;
1374     }
1375
1376   delete_all_dependences (insn);
1377
1378   prev_nonnote = prev_nonnote_insn (insn);
1379   if (BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (prev_nonnote)
1380       && ! sched_insns_conditions_mutex_p (insn, prev_nonnote))
1381     add_dependence (insn, prev_nonnote, REG_DEP_ANTI);
1382 }
1383 \f
1384 /* Process an insn's memory dependencies.  There are four kinds of
1385    dependencies:
1386
1387    (0) read dependence: read follows read
1388    (1) true dependence: read follows write
1389    (2) output dependence: write follows write
1390    (3) anti dependence: write follows read
1391
1392    We are careful to build only dependencies which actually exist, and
1393    use transitivity to avoid building too many links.  */
1394
1395 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1396    The MEM is a memory reference contained within INSN, which we are saving
1397    so that we can do memory aliasing on it.  */
1398
1399 static void
1400 add_insn_mem_dependence (struct deps *deps, bool read_p,
1401                          rtx insn, rtx mem)
1402 {
1403   rtx *insn_list;
1404   rtx *mem_list;
1405   rtx link;
1406
1407   if (read_p)
1408     {
1409       insn_list = &deps->pending_read_insns;
1410       mem_list = &deps->pending_read_mems;
1411       deps->pending_read_list_length++;
1412     }
1413   else
1414     {
1415       insn_list = &deps->pending_write_insns;
1416       mem_list = &deps->pending_write_mems;
1417       deps->pending_write_list_length++;
1418     }
1419
1420   link = alloc_INSN_LIST (insn, *insn_list);
1421   *insn_list = link;
1422
1423   if (current_sched_info->use_cselib)
1424     {
1425       mem = shallow_copy_rtx (mem);
1426       XEXP (mem, 0) = cselib_subst_to_values (XEXP (mem, 0));
1427     }
1428   link = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
1429   *mem_list = link;
1430 }
1431
1432 /* Make a dependency between every memory reference on the pending lists
1433    and INSN, thus flushing the pending lists.  FOR_READ is true if emitting
1434    dependencies for a read operation, similarly with FOR_WRITE.  */
1435
1436 static void
1437 flush_pending_lists (struct deps *deps, rtx insn, int for_read,
1438                      int for_write)
1439 {
1440   if (for_write)
1441     {
1442       add_dependence_list_and_free (insn, &deps->pending_read_insns, 1,
1443                                     REG_DEP_ANTI);
1444       free_EXPR_LIST_list (&deps->pending_read_mems);
1445       deps->pending_read_list_length = 0;
1446     }
1447
1448   add_dependence_list_and_free (insn, &deps->pending_write_insns, 1,
1449                                 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1450   free_EXPR_LIST_list (&deps->pending_write_mems);
1451   deps->pending_write_list_length = 0;
1452
1453   add_dependence_list_and_free (insn, &deps->last_pending_memory_flush, 1,
1454                                 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
1455   deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
1456   deps->pending_flush_length = 1;
1457 }
1458 \f
1459 /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
1460    The type of the reference is specified by REF and can be SET,
1461    CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE.  */
1462
1463 static void
1464 sched_analyze_reg (struct deps *deps, int regno, enum machine_mode mode,
1465                    enum rtx_code ref, rtx insn)
1466 {
1467   /* A hard reg in a wide mode may really be multiple registers.
1468      If so, mark all of them just like the first.  */
1469   if (regno < FIRST_PSEUDO_REGISTER)
1470     {
1471       int i = hard_regno_nregs[regno][mode];
1472       if (ref == SET)
1473         {
1474           while (--i >= 0)
1475             SET_REGNO_REG_SET (reg_pending_sets, regno + i);
1476         }
1477       else if (ref == USE)
1478         {
1479           while (--i >= 0)
1480             SET_REGNO_REG_SET (reg_pending_uses, regno + i);
1481         }
1482       else
1483         {
1484           while (--i >= 0)
1485             SET_REGNO_REG_SET (reg_pending_clobbers, regno + i);
1486         }
1487     }
1488
1489   /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
1490      it does not reload.  Ignore these as they have served their
1491      purpose already.  */
1492   else if (regno >= deps->max_reg)
1493     {
1494       enum rtx_code code = GET_CODE (PATTERN (insn));
1495       gcc_assert (code == USE || code == CLOBBER);
1496     }
1497
1498   else
1499     {
1500       if (ref == SET)
1501         SET_REGNO_REG_SET (reg_pending_sets, regno);
1502       else if (ref == USE)
1503         SET_REGNO_REG_SET (reg_pending_uses, regno);
1504       else
1505         SET_REGNO_REG_SET (reg_pending_clobbers, regno);
1506
1507       /* Pseudos that are REG_EQUIV to something may be replaced
1508          by that during reloading.  We need only add dependencies for
1509         the address in the REG_EQUIV note.  */
1510       if (!reload_completed && get_reg_known_equiv_p (regno))
1511         {
1512           rtx t = get_reg_known_value (regno);
1513           if (MEM_P (t))
1514             sched_analyze_2 (deps, XEXP (t, 0), insn);
1515         }
1516
1517       /* Don't let it cross a call after scheduling if it doesn't
1518          already cross one.  */
1519       if (REG_N_CALLS_CROSSED (regno) == 0)
1520         {
1521           if (ref == USE)
1522             deps->sched_before_next_call
1523               = alloc_INSN_LIST (insn, deps->sched_before_next_call);
1524           else
1525             add_dependence_list (insn, deps->last_function_call, 1,
1526                                  REG_DEP_ANTI);
1527         }
1528     }
1529 }
1530
1531 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
1532    rtx, X, creating all dependencies generated by the write to the
1533    destination of X, and reads of everything mentioned.  */
1534
1535 static void
1536 sched_analyze_1 (struct deps *deps, rtx x, rtx insn)
1537 {
1538   rtx dest = XEXP (x, 0);
1539   enum rtx_code code = GET_CODE (x);
1540
1541   if (dest == 0)
1542     return;
1543
1544   if (GET_CODE (dest) == PARALLEL)
1545     {
1546       int i;
1547
1548       for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1549         if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
1550           sched_analyze_1 (deps,
1551                            gen_rtx_CLOBBER (VOIDmode,
1552                                             XEXP (XVECEXP (dest, 0, i), 0)),
1553                            insn);
1554
1555       if (GET_CODE (x) == SET)
1556         sched_analyze_2 (deps, SET_SRC (x), insn);
1557       return;
1558     }
1559
1560   while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
1561          || GET_CODE (dest) == ZERO_EXTRACT)
1562     {
1563       if (GET_CODE (dest) == STRICT_LOW_PART
1564          || GET_CODE (dest) == ZERO_EXTRACT
1565          || df_read_modify_subreg_p (dest))
1566         {
1567           /* These both read and modify the result.  We must handle
1568              them as writes to get proper dependencies for following
1569              instructions.  We must handle them as reads to get proper
1570              dependencies from this to previous instructions.
1571              Thus we need to call sched_analyze_2.  */
1572
1573           sched_analyze_2 (deps, XEXP (dest, 0), insn);
1574         }
1575       if (GET_CODE (dest) == ZERO_EXTRACT)
1576         {
1577           /* The second and third arguments are values read by this insn.  */
1578           sched_analyze_2 (deps, XEXP (dest, 1), insn);
1579           sched_analyze_2 (deps, XEXP (dest, 2), insn);
1580         }
1581       dest = XEXP (dest, 0);
1582     }
1583
1584   if (REG_P (dest))
1585     {
1586       int regno = REGNO (dest);
1587       enum machine_mode mode = GET_MODE (dest);
1588
1589       sched_analyze_reg (deps, regno, mode, code, insn);
1590
1591 #ifdef STACK_REGS
1592       /* Treat all writes to a stack register as modifying the TOS.  */
1593       if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
1594         {
1595           /* Avoid analyzing the same register twice.  */
1596           if (regno != FIRST_STACK_REG)
1597             sched_analyze_reg (deps, FIRST_STACK_REG, mode, code, insn);
1598           sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
1599         }
1600 #endif
1601     }
1602   else if (MEM_P (dest))
1603     {
1604       /* Writing memory.  */
1605       rtx t = dest;
1606
1607       if (current_sched_info->use_cselib)
1608         {
1609           t = shallow_copy_rtx (dest);
1610           cselib_lookup (XEXP (t, 0), Pmode, 1);
1611           XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
1612         }
1613       t = canon_rtx (t);
1614
1615       if ((deps->pending_read_list_length + deps->pending_write_list_length)
1616           > MAX_PENDING_LIST_LENGTH)
1617         {
1618           /* Flush all pending reads and writes to prevent the pending lists
1619              from getting any larger.  Insn scheduling runs too slowly when
1620              these lists get long.  When compiling GCC with itself,
1621              this flush occurs 8 times for sparc, and 10 times for m88k using
1622              the default value of 32.  */
1623           flush_pending_lists (deps, insn, false, true);
1624         }
1625       else
1626         {
1627           rtx pending, pending_mem;
1628
1629           pending = deps->pending_read_insns;
1630           pending_mem = deps->pending_read_mems;
1631           while (pending)
1632             {
1633               if (anti_dependence (XEXP (pending_mem, 0), t)
1634                   && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1635                 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1636
1637               pending = XEXP (pending, 1);
1638               pending_mem = XEXP (pending_mem, 1);
1639             }
1640
1641           pending = deps->pending_write_insns;
1642           pending_mem = deps->pending_write_mems;
1643           while (pending)
1644             {
1645               if (output_dependence (XEXP (pending_mem, 0), t)
1646                   && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1647                 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1648
1649               pending = XEXP (pending, 1);
1650               pending_mem = XEXP (pending_mem, 1);
1651             }
1652
1653           add_dependence_list (insn, deps->last_pending_memory_flush, 1,
1654                                REG_DEP_ANTI);
1655
1656           add_insn_mem_dependence (deps, false, insn, dest);
1657         }
1658       sched_analyze_2 (deps, XEXP (dest, 0), insn);
1659     }
1660
1661   /* Analyze reads.  */
1662   if (GET_CODE (x) == SET)
1663     sched_analyze_2 (deps, SET_SRC (x), insn);
1664 }
1665
1666 /* Analyze the uses of memory and registers in rtx X in INSN.  */
1667
1668 static void
1669 sched_analyze_2 (struct deps *deps, rtx x, rtx insn)
1670 {
1671   int i;
1672   int j;
1673   enum rtx_code code;
1674   const char *fmt;
1675
1676   if (x == 0)
1677     return;
1678
1679   code = GET_CODE (x);
1680
1681   switch (code)
1682     {
1683     case CONST_INT:
1684     case CONST_DOUBLE:
1685     case CONST_FIXED:
1686     case CONST_VECTOR:
1687     case SYMBOL_REF:
1688     case CONST:
1689     case LABEL_REF:
1690       /* Ignore constants.  Note that we must handle CONST_DOUBLE here
1691          because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
1692          this does not mean that this insn is using cc0.  */
1693       return;
1694
1695 #ifdef HAVE_cc0
1696     case CC0:
1697       /* User of CC0 depends on immediately preceding insn.  */
1698       SCHED_GROUP_P (insn) = 1;
1699        /* Don't move CC0 setter to another block (it can set up the
1700         same flag for previous CC0 users which is safe).  */
1701       CANT_MOVE (prev_nonnote_insn (insn)) = 1;
1702       return;
1703 #endif
1704
1705     case REG:
1706       {
1707         int regno = REGNO (x);
1708         enum machine_mode mode = GET_MODE (x);
1709
1710         sched_analyze_reg (deps, regno, mode, USE, insn);
1711
1712 #ifdef STACK_REGS
1713       /* Treat all reads of a stack register as modifying the TOS.  */
1714       if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
1715         {
1716           /* Avoid analyzing the same register twice.  */
1717           if (regno != FIRST_STACK_REG)
1718             sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
1719           sched_analyze_reg (deps, FIRST_STACK_REG, mode, SET, insn);
1720         }
1721 #endif
1722         return;
1723       }
1724
1725     case MEM:
1726       {
1727         /* Reading memory.  */
1728         rtx u;
1729         rtx pending, pending_mem;
1730         rtx t = x;
1731
1732         if (current_sched_info->use_cselib)
1733           {
1734             t = shallow_copy_rtx (t);
1735             cselib_lookup (XEXP (t, 0), Pmode, 1);
1736             XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
1737           }
1738         t = canon_rtx (t);
1739         pending = deps->pending_read_insns;
1740         pending_mem = deps->pending_read_mems;
1741         while (pending)
1742           {
1743             if (read_dependence (XEXP (pending_mem, 0), t)
1744                 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1745               add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1746
1747             pending = XEXP (pending, 1);
1748             pending_mem = XEXP (pending_mem, 1);
1749           }
1750
1751         pending = deps->pending_write_insns;
1752         pending_mem = deps->pending_write_mems;
1753         while (pending)
1754           {
1755             if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
1756                                  t, rtx_varies_p)
1757                 && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1758               {
1759                 if ((current_sched_info->flags & DO_SPECULATION)
1760                     && (spec_info->mask & BEGIN_DATA))
1761                   /* Create a data-speculative dependence between producer
1762                      and consumer.  */
1763                   {
1764                     dep_def _dep, *dep = &_dep;
1765
1766                     init_dep_1 (dep, XEXP (pending, 0), insn, REG_DEP_TRUE,
1767                                 BEGIN_DATA | DEP_TRUE);
1768
1769                     maybe_add_or_update_dep_1 (dep, false,
1770                                                XEXP (pending_mem, 0), t);
1771                   }
1772                 else
1773                   add_dependence (insn, XEXP (pending, 0), REG_DEP_TRUE);
1774               }
1775
1776             pending = XEXP (pending, 1);
1777             pending_mem = XEXP (pending_mem, 1);
1778           }
1779
1780         for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
1781           if (! JUMP_P (XEXP (u, 0)) || deps_may_trap_p (x))
1782             add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1783
1784         /* Always add these dependencies to pending_reads, since
1785            this insn may be followed by a write.  */
1786         add_insn_mem_dependence (deps, true, insn, x);
1787
1788         /* Take advantage of tail recursion here.  */
1789         sched_analyze_2 (deps, XEXP (x, 0), insn);
1790         return;
1791       }
1792
1793     /* Force pending stores to memory in case a trap handler needs them.  */
1794     case TRAP_IF:
1795       flush_pending_lists (deps, insn, true, false);
1796       break;
1797
1798     case ASM_OPERANDS:
1799     case ASM_INPUT:
1800     case UNSPEC_VOLATILE:
1801       {
1802         /* Traditional and volatile asm instructions must be considered to use
1803            and clobber all hard registers, all pseudo-registers and all of
1804            memory.  So must TRAP_IF and UNSPEC_VOLATILE operations.
1805
1806            Consider for instance a volatile asm that changes the fpu rounding
1807            mode.  An insn should not be moved across this even if it only uses
1808            pseudo-regs because it might give an incorrectly rounded result.  */
1809         if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
1810           reg_pending_barrier = TRUE_BARRIER;
1811
1812         /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1813            We can not just fall through here since then we would be confused
1814            by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1815            traditional asms unlike their normal usage.  */
1816
1817         if (code == ASM_OPERANDS)
1818           {
1819             for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
1820               sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
1821             return;
1822           }
1823         break;
1824       }
1825
1826     case PRE_DEC:
1827     case POST_DEC:
1828     case PRE_INC:
1829     case POST_INC:
1830       /* These both read and modify the result.  We must handle them as writes
1831          to get proper dependencies for following instructions.  We must handle
1832          them as reads to get proper dependencies from this to previous
1833          instructions.  Thus we need to pass them to both sched_analyze_1
1834          and sched_analyze_2.  We must call sched_analyze_2 first in order
1835          to get the proper antecedent for the read.  */
1836       sched_analyze_2 (deps, XEXP (x, 0), insn);
1837       sched_analyze_1 (deps, x, insn);
1838       return;
1839
1840     case POST_MODIFY:
1841     case PRE_MODIFY:
1842       /* op0 = op0 + op1 */
1843       sched_analyze_2 (deps, XEXP (x, 0), insn);
1844       sched_analyze_2 (deps, XEXP (x, 1), insn);
1845       sched_analyze_1 (deps, x, insn);
1846       return;
1847
1848     default:
1849       break;
1850     }
1851
1852   /* Other cases: walk the insn.  */
1853   fmt = GET_RTX_FORMAT (code);
1854   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1855     {
1856       if (fmt[i] == 'e')
1857         sched_analyze_2 (deps, XEXP (x, i), insn);
1858       else if (fmt[i] == 'E')
1859         for (j = 0; j < XVECLEN (x, i); j++)
1860           sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
1861     }
1862 }
1863
1864 /* Analyze an INSN with pattern X to find all dependencies.  */
1865
1866 static void
1867 sched_analyze_insn (struct deps *deps, rtx x, rtx insn)
1868 {
1869   RTX_CODE code = GET_CODE (x);
1870   rtx link;
1871   unsigned i;
1872   reg_set_iterator rsi;
1873
1874   if (code == COND_EXEC)
1875     {
1876       sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
1877
1878       /* ??? Should be recording conditions so we reduce the number of
1879          false dependencies.  */
1880       x = COND_EXEC_CODE (x);
1881       code = GET_CODE (x);
1882     }
1883   if (code == SET || code == CLOBBER)
1884     {
1885       sched_analyze_1 (deps, x, insn);
1886
1887       /* Bare clobber insns are used for letting life analysis, reg-stack
1888          and others know that a value is dead.  Depend on the last call
1889          instruction so that reg-stack won't get confused.  */
1890       if (code == CLOBBER)
1891         add_dependence_list (insn, deps->last_function_call, 1, REG_DEP_OUTPUT);
1892     }
1893   else if (code == PARALLEL)
1894     {
1895       for (i = XVECLEN (x, 0); i--;)
1896         {
1897           rtx sub = XVECEXP (x, 0, i);
1898           code = GET_CODE (sub);
1899
1900           if (code == COND_EXEC)
1901             {
1902               sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
1903               sub = COND_EXEC_CODE (sub);
1904               code = GET_CODE (sub);
1905             }
1906           if (code == SET || code == CLOBBER)
1907             sched_analyze_1 (deps, sub, insn);
1908           else
1909             sched_analyze_2 (deps, sub, insn);
1910         }
1911     }
1912   else
1913     sched_analyze_2 (deps, x, insn);
1914
1915   /* Mark registers CLOBBERED or used by called function.  */
1916   if (CALL_P (insn))
1917     {
1918       for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1919         {
1920           if (GET_CODE (XEXP (link, 0)) == CLOBBER)
1921             sched_analyze_1 (deps, XEXP (link, 0), insn);
1922           else
1923             sched_analyze_2 (deps, XEXP (link, 0), insn);
1924         }
1925       if (find_reg_note (insn, REG_SETJMP, NULL))
1926         reg_pending_barrier = MOVE_BARRIER;
1927     }
1928
1929   if (JUMP_P (insn))
1930     {
1931       rtx next;
1932       next = next_nonnote_insn (insn);
1933       if (next && BARRIER_P (next))
1934         reg_pending_barrier = TRUE_BARRIER;
1935       else
1936         {
1937           rtx pending, pending_mem;
1938           regset_head tmp_uses, tmp_sets;
1939           INIT_REG_SET (&tmp_uses);
1940           INIT_REG_SET (&tmp_sets);
1941
1942           (*current_sched_info->compute_jump_reg_dependencies)
1943             (insn, &deps->reg_conditional_sets, &tmp_uses, &tmp_sets);
1944           /* Make latency of jump equal to 0 by using anti-dependence.  */
1945           EXECUTE_IF_SET_IN_REG_SET (&tmp_uses, 0, i, rsi)
1946             {
1947               struct deps_reg *reg_last = &deps->reg_last[i];
1948               add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI);
1949               add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI);
1950               reg_last->uses_length++;
1951               reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1952             }
1953           IOR_REG_SET (reg_pending_sets, &tmp_sets);
1954
1955           CLEAR_REG_SET (&tmp_uses);
1956           CLEAR_REG_SET (&tmp_sets);
1957
1958           /* All memory writes and volatile reads must happen before the
1959              jump.  Non-volatile reads must happen before the jump iff
1960              the result is needed by the above register used mask.  */
1961
1962           pending = deps->pending_write_insns;
1963           pending_mem = deps->pending_write_mems;
1964           while (pending)
1965             {
1966               if (! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1967                 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1968               pending = XEXP (pending, 1);
1969               pending_mem = XEXP (pending_mem, 1);
1970             }
1971
1972           pending = deps->pending_read_insns;
1973           pending_mem = deps->pending_read_mems;
1974           while (pending)
1975             {
1976               if (MEM_VOLATILE_P (XEXP (pending_mem, 0))
1977                   && ! sched_insns_conditions_mutex_p (insn, XEXP (pending, 0)))
1978                 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1979               pending = XEXP (pending, 1);
1980               pending_mem = XEXP (pending_mem, 1);
1981             }
1982
1983           add_dependence_list (insn, deps->last_pending_memory_flush, 1,
1984                                REG_DEP_ANTI);
1985         }
1986     }
1987
1988   /* If this instruction can throw an exception, then moving it changes
1989      where block boundaries fall.  This is mighty confusing elsewhere.
1990      Therefore, prevent such an instruction from being moved.  Same for
1991      non-jump instructions that define block boundaries.
1992      ??? Unclear whether this is still necessary in EBB mode.  If not,
1993      add_branch_dependences should be adjusted for RGN mode instead.  */
1994   if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
1995       || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
1996     reg_pending_barrier = MOVE_BARRIER;
1997
1998   /* Add dependencies if a scheduling barrier was found.  */
1999   if (reg_pending_barrier)
2000     {
2001       /* In the case of barrier the most added dependencies are not
2002          real, so we use anti-dependence here.  */
2003       if (sched_get_condition (insn))
2004         {
2005           EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2006             {
2007               struct deps_reg *reg_last = &deps->reg_last[i];
2008               add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2009               add_dependence_list
2010                 (insn, reg_last->sets, 0,
2011                  reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2012               add_dependence_list
2013                 (insn, reg_last->clobbers, 0,
2014                  reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2015             }
2016         }
2017       else
2018         {
2019           EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2020             {
2021               struct deps_reg *reg_last = &deps->reg_last[i];
2022               add_dependence_list_and_free (insn, &reg_last->uses, 0,
2023                                             REG_DEP_ANTI);
2024               add_dependence_list_and_free
2025                 (insn, &reg_last->sets, 0,
2026                  reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2027               add_dependence_list_and_free
2028                 (insn, &reg_last->clobbers, 0,
2029                  reg_pending_barrier == TRUE_BARRIER ? REG_DEP_TRUE : REG_DEP_ANTI);
2030               reg_last->uses_length = 0;
2031               reg_last->clobbers_length = 0;
2032             }
2033         }
2034
2035       for (i = 0; i < (unsigned)deps->max_reg; i++)
2036         {
2037           struct deps_reg *reg_last = &deps->reg_last[i];
2038           reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2039           SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
2040         }
2041
2042       flush_pending_lists (deps, insn, true, true);
2043       CLEAR_REG_SET (&deps->reg_conditional_sets);
2044       reg_pending_barrier = NOT_A_BARRIER;
2045     }
2046   else
2047     {
2048       /* If the current insn is conditional, we can't free any
2049          of the lists.  */
2050       if (sched_get_condition (insn))
2051         {
2052           EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
2053             {
2054               struct deps_reg *reg_last = &deps->reg_last[i];
2055               add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
2056               add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
2057               reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2058               reg_last->uses_length++;
2059             }
2060           EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
2061             {
2062               struct deps_reg *reg_last = &deps->reg_last[i];
2063               add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2064               add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2065               reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
2066               reg_last->clobbers_length++;
2067             }
2068           EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
2069             {
2070               struct deps_reg *reg_last = &deps->reg_last[i];
2071               add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2072               add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT);
2073               add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2074               reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2075               SET_REGNO_REG_SET (&deps->reg_conditional_sets, i);
2076             }
2077         }
2078       else
2079         {
2080           EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
2081             {
2082               struct deps_reg *reg_last = &deps->reg_last[i];
2083               add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE);
2084               add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE);
2085               reg_last->uses_length++;
2086               reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
2087             }
2088           EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
2089             {
2090               struct deps_reg *reg_last = &deps->reg_last[i];
2091               if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
2092                   || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
2093                 {
2094                   add_dependence_list_and_free (insn, &reg_last->sets, 0,
2095                                                 REG_DEP_OUTPUT);
2096                   add_dependence_list_and_free (insn, &reg_last->uses, 0,
2097                                                 REG_DEP_ANTI);
2098                   add_dependence_list_and_free (insn, &reg_last->clobbers, 0,
2099                                                 REG_DEP_OUTPUT);
2100                   reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2101                   reg_last->clobbers_length = 0;
2102                   reg_last->uses_length = 0;
2103                 }
2104               else
2105                 {
2106                   add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT);
2107                   add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI);
2108                 }
2109               reg_last->clobbers_length++;
2110               reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
2111             }
2112           EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
2113             {
2114               struct deps_reg *reg_last = &deps->reg_last[i];
2115               add_dependence_list_and_free (insn, &reg_last->sets, 0,
2116                                             REG_DEP_OUTPUT);
2117               add_dependence_list_and_free (insn, &reg_last->clobbers, 0,
2118                                             REG_DEP_OUTPUT);
2119               add_dependence_list_and_free (insn, &reg_last->uses, 0,
2120                                             REG_DEP_ANTI);
2121               reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
2122               reg_last->uses_length = 0;
2123               reg_last->clobbers_length = 0;
2124               CLEAR_REGNO_REG_SET (&deps->reg_conditional_sets, i);
2125             }
2126         }
2127
2128       IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
2129       IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
2130       IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
2131     }
2132   CLEAR_REG_SET (reg_pending_uses);
2133   CLEAR_REG_SET (reg_pending_clobbers);
2134   CLEAR_REG_SET (reg_pending_sets);
2135
2136   /* If we are currently in a libcall scheduling group, then mark the
2137      current insn as being in a scheduling group and that it can not
2138      be moved into a different basic block.  */
2139
2140   if (deps->libcall_block_tail_insn)
2141     {
2142       SCHED_GROUP_P (insn) = 1;
2143       CANT_MOVE (insn) = 1;
2144     }
2145
2146   /* If a post-call group is still open, see if it should remain so.
2147      This insn must be a simple move of a hard reg to a pseudo or
2148      vice-versa.
2149
2150      We must avoid moving these insns for correctness on
2151      SMALL_REGISTER_CLASS machines, and for special registers like
2152      PIC_OFFSET_TABLE_REGNUM.  For simplicity, extend this to all
2153      hard regs for all targets.  */
2154
2155   if (deps->in_post_call_group_p)
2156     {
2157       rtx tmp, set = single_set (insn);
2158       int src_regno, dest_regno;
2159
2160       if (set == NULL)
2161         goto end_call_group;
2162
2163       tmp = SET_DEST (set);
2164       if (GET_CODE (tmp) == SUBREG)
2165         tmp = SUBREG_REG (tmp);
2166       if (REG_P (tmp))
2167         dest_regno = REGNO (tmp);
2168       else
2169         goto end_call_group;
2170
2171       tmp = SET_SRC (set);
2172       if (GET_CODE (tmp) == SUBREG)
2173         tmp = SUBREG_REG (tmp);
2174       if ((GET_CODE (tmp) == PLUS
2175            || GET_CODE (tmp) == MINUS)
2176           && REG_P (XEXP (tmp, 0))
2177           && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
2178           && dest_regno == STACK_POINTER_REGNUM)
2179         src_regno = STACK_POINTER_REGNUM;
2180       else if (REG_P (tmp))
2181         src_regno = REGNO (tmp);
2182       else
2183         goto end_call_group;
2184
2185       if (src_regno < FIRST_PSEUDO_REGISTER
2186           || dest_regno < FIRST_PSEUDO_REGISTER)
2187         {
2188           if (deps->in_post_call_group_p == post_call_initial)
2189             deps->in_post_call_group_p = post_call;
2190
2191           SCHED_GROUP_P (insn) = 1;
2192           CANT_MOVE (insn) = 1;
2193         }
2194       else
2195         {
2196         end_call_group:
2197           deps->in_post_call_group_p = not_post_call;
2198         }
2199     }
2200
2201   /* Fixup the dependencies in the sched group.  */
2202   if (SCHED_GROUP_P (insn))
2203     fixup_sched_groups (insn);
2204
2205 #ifdef INSN_SCHEDULING
2206   if ((current_sched_info->flags & DO_SPECULATION)
2207       && !sched_insn_is_legitimate_for_speculation_p (insn, 0))
2208     /* INSN has an internal dependency (e.g. r14 = [r14]) and thus cannot
2209        be speculated.  */
2210     {
2211       sd_iterator_def sd_it;
2212       dep_t dep;
2213
2214       for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
2215            sd_iterator_cond (&sd_it, &dep);)
2216         change_spec_dep_to_hard (sd_it);
2217     }
2218 #endif
2219 }
2220
2221 /* Analyze every insn between HEAD and TAIL inclusive, creating backward
2222    dependencies for each insn.  */
2223
2224 void
2225 sched_analyze (struct deps *deps, rtx head, rtx tail)
2226 {
2227   rtx insn;
2228
2229   if (current_sched_info->use_cselib)
2230     cselib_init (true);
2231
2232   /* Before reload, if the previous block ended in a call, show that
2233      we are inside a post-call group, so as to keep the lifetimes of
2234      hard registers correct.  */
2235   if (! reload_completed && !LABEL_P (head))
2236     {
2237       insn = prev_nonnote_insn (head);
2238       if (insn && CALL_P (insn))
2239         deps->in_post_call_group_p = post_call_initial;
2240     }
2241   for (insn = head;; insn = NEXT_INSN (insn))
2242     {
2243       rtx link, end_seq, r0, set;
2244
2245       if (INSN_P (insn))
2246         {
2247           /* And initialize deps_lists.  */
2248           sd_init_insn (insn);
2249         }
2250
2251       if (NONJUMP_INSN_P (insn) || JUMP_P (insn))
2252         {
2253           /* Make each JUMP_INSN a scheduling barrier for memory
2254              references.  */
2255           if (JUMP_P (insn))
2256             {
2257               /* Keep the list a reasonable size.  */
2258               if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
2259                 flush_pending_lists (deps, insn, true, true);
2260               else
2261                 deps->last_pending_memory_flush
2262                   = alloc_INSN_LIST (insn, deps->last_pending_memory_flush);
2263             }
2264           sched_analyze_insn (deps, PATTERN (insn), insn);
2265         }
2266       else if (CALL_P (insn))
2267         {
2268           int i;
2269
2270           CANT_MOVE (insn) = 1;
2271
2272           if (find_reg_note (insn, REG_SETJMP, NULL))
2273             {
2274               /* This is setjmp.  Assume that all registers, not just
2275                  hard registers, may be clobbered by this call.  */
2276               reg_pending_barrier = MOVE_BARRIER;
2277             }
2278           else
2279             {
2280               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2281                 /* A call may read and modify global register variables.  */
2282                 if (global_regs[i])
2283                   {
2284                     SET_REGNO_REG_SET (reg_pending_sets, i);
2285                     SET_REGNO_REG_SET (reg_pending_uses, i);
2286                   }
2287                 /* Other call-clobbered hard regs may be clobbered.
2288                    Since we only have a choice between 'might be clobbered'
2289                    and 'definitely not clobbered', we must include all
2290                    partly call-clobbered registers here.  */
2291                 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
2292                          || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
2293                   SET_REGNO_REG_SET (reg_pending_clobbers, i);
2294                 /* We don't know what set of fixed registers might be used
2295                    by the function, but it is certain that the stack pointer
2296                    is among them, but be conservative.  */
2297                 else if (fixed_regs[i])
2298                   SET_REGNO_REG_SET (reg_pending_uses, i);
2299                 /* The frame pointer is normally not used by the function
2300                    itself, but by the debugger.  */
2301                 /* ??? MIPS o32 is an exception.  It uses the frame pointer
2302                    in the macro expansion of jal but does not represent this
2303                    fact in the call_insn rtl.  */
2304                 else if (i == FRAME_POINTER_REGNUM
2305                          || (i == HARD_FRAME_POINTER_REGNUM
2306                              && (! reload_completed || frame_pointer_needed)))
2307                   SET_REGNO_REG_SET (reg_pending_uses, i);
2308             }
2309
2310           /* For each insn which shouldn't cross a call, add a dependence
2311              between that insn and this call insn.  */
2312           add_dependence_list_and_free (insn, &deps->sched_before_next_call, 1,
2313                                         REG_DEP_ANTI);
2314
2315           sched_analyze_insn (deps, PATTERN (insn), insn);
2316
2317           /* In the absence of interprocedural alias analysis, we must flush
2318              all pending reads and writes, and start new dependencies starting
2319              from here.  But only flush writes for constant calls (which may
2320              be passed a pointer to something we haven't written yet).  */
2321           flush_pending_lists (deps, insn, true, !CONST_OR_PURE_CALL_P (insn));
2322
2323           /* Remember the last function call for limiting lifetimes.  */
2324           free_INSN_LIST_list (&deps->last_function_call);
2325           deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
2326
2327           /* Before reload, begin a post-call group, so as to keep the
2328              lifetimes of hard registers correct.  */
2329           if (! reload_completed)
2330             deps->in_post_call_group_p = post_call;
2331         }
2332
2333       /* EH_REGION insn notes can not appear until well after we complete
2334          scheduling.  */
2335       if (NOTE_P (insn))
2336         gcc_assert (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
2337                     && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END);
2338
2339       if (current_sched_info->use_cselib)
2340         cselib_process_insn (insn);
2341
2342       /* Now that we have completed handling INSN, check and see if it is
2343          a CLOBBER beginning a libcall block.   If it is, record the
2344          end of the libcall sequence.
2345
2346          We want to schedule libcall blocks as a unit before reload.  While
2347          this restricts scheduling, it preserves the meaning of a libcall
2348          block.
2349
2350          As a side effect, we may get better code due to decreased register
2351          pressure as well as less chance of a foreign insn appearing in
2352          a libcall block.  */
2353       if (!reload_completed
2354           /* Note we may have nested libcall sequences.  We only care about
2355              the outermost libcall sequence.  */
2356           && deps->libcall_block_tail_insn == 0
2357           /* The sequence must start with a clobber of a register.  */
2358           && NONJUMP_INSN_P (insn)
2359           && GET_CODE (PATTERN (insn)) == CLOBBER
2360           && (r0 = XEXP (PATTERN (insn), 0), REG_P (r0))
2361           && REG_P (XEXP (PATTERN (insn), 0))
2362           /* The CLOBBER must also have a REG_LIBCALL note attached.  */
2363           && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
2364           && (end_seq = XEXP (link, 0)) != 0
2365           /* The insn referenced by the REG_LIBCALL note must be a
2366              simple nop copy with the same destination as the register
2367              mentioned in the clobber.  */
2368           && (set = single_set (end_seq)) != 0
2369           && SET_DEST (set) == r0 && SET_SRC (set) == r0
2370           /* And finally the insn referenced by the REG_LIBCALL must
2371              also contain a REG_EQUAL note and a REG_RETVAL note.  */
2372           && find_reg_note (end_seq, REG_EQUAL, NULL_RTX) != 0
2373           && find_reg_note (end_seq, REG_RETVAL, NULL_RTX) != 0)
2374         deps->libcall_block_tail_insn = XEXP (link, 0);
2375
2376       /* If we have reached the end of a libcall block, then close the
2377          block.  */
2378       if (deps->libcall_block_tail_insn == insn)
2379         deps->libcall_block_tail_insn = 0;
2380
2381       if (insn == tail)
2382         {
2383           if (current_sched_info->use_cselib)
2384             cselib_finish ();
2385           return;
2386         }
2387     }
2388   gcc_unreachable ();
2389 }
2390
2391 /* Helper for sched_free_deps ().
2392    Delete INSN's (RESOLVED_P) backward dependencies.  */
2393 static void
2394 delete_dep_nodes_in_back_deps (rtx insn, bool resolved_p)
2395 {
2396   sd_iterator_def sd_it;
2397   dep_t dep;
2398   sd_list_types_def types;
2399
2400   if (resolved_p)
2401     types = SD_LIST_RES_BACK;
2402   else
2403     types = SD_LIST_BACK;
2404
2405   for (sd_it = sd_iterator_start (insn, types);
2406        sd_iterator_cond (&sd_it, &dep);)
2407     {
2408       dep_link_t link = *sd_it.linkp;
2409       dep_node_t node = DEP_LINK_NODE (link);
2410       deps_list_t back_list;
2411       deps_list_t forw_list;
2412
2413       get_back_and_forw_lists (dep, resolved_p, &back_list, &forw_list);
2414       remove_from_deps_list (link, back_list);
2415       delete_dep_node (node);
2416     }
2417 }
2418
2419 /* Delete (RESOLVED_P) dependencies between HEAD and TAIL together with
2420    deps_lists.  */
2421 void
2422 sched_free_deps (rtx head, rtx tail, bool resolved_p)
2423 {
2424   rtx insn;
2425   rtx next_tail = NEXT_INSN (tail);
2426
2427   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
2428     if (INSN_P (insn) && INSN_LUID (insn) > 0)
2429       {
2430         /* Clear resolved back deps together with its dep_nodes.  */
2431         delete_dep_nodes_in_back_deps (insn, resolved_p);
2432
2433         /* Clear forward deps and leave the dep_nodes to the
2434            corresponding back_deps list.  */
2435         if (resolved_p)
2436           clear_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
2437         else
2438           clear_deps_list (INSN_FORW_DEPS (insn));
2439
2440         sd_finish_insn (insn);
2441       }
2442 }
2443 \f
2444 /* Initialize variables for region data dependence analysis.
2445    n_bbs is the number of region blocks.  */
2446
2447 void
2448 init_deps (struct deps *deps)
2449 {
2450   int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
2451
2452   deps->max_reg = max_reg;
2453   deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
2454   INIT_REG_SET (&deps->reg_last_in_use);
2455   INIT_REG_SET (&deps->reg_conditional_sets);
2456
2457   deps->pending_read_insns = 0;
2458   deps->pending_read_mems = 0;
2459   deps->pending_write_insns = 0;
2460   deps->pending_write_mems = 0;
2461   deps->pending_read_list_length = 0;
2462   deps->pending_write_list_length = 0;
2463   deps->pending_flush_length = 0;
2464   deps->last_pending_memory_flush = 0;
2465   deps->last_function_call = 0;
2466   deps->sched_before_next_call = 0;
2467   deps->in_post_call_group_p = not_post_call;
2468   deps->libcall_block_tail_insn = 0;
2469 }
2470
2471 /* Free insn lists found in DEPS.  */
2472
2473 void
2474 free_deps (struct deps *deps)
2475 {
2476   unsigned i;
2477   reg_set_iterator rsi;
2478
2479   free_INSN_LIST_list (&deps->pending_read_insns);
2480   free_EXPR_LIST_list (&deps->pending_read_mems);
2481   free_INSN_LIST_list (&deps->pending_write_insns);
2482   free_EXPR_LIST_list (&deps->pending_write_mems);
2483   free_INSN_LIST_list (&deps->last_pending_memory_flush);
2484
2485   /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
2486      times.  For a testcase with 42000 regs and 8000 small basic blocks,
2487      this loop accounted for nearly 60% (84 sec) of the total -O2 runtime.  */
2488   EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
2489     {
2490       struct deps_reg *reg_last = &deps->reg_last[i];
2491       if (reg_last->uses)
2492         free_INSN_LIST_list (&reg_last->uses);
2493       if (reg_last->sets)
2494         free_INSN_LIST_list (&reg_last->sets);
2495       if (reg_last->clobbers)
2496         free_INSN_LIST_list (&reg_last->clobbers);
2497     }
2498   CLEAR_REG_SET (&deps->reg_last_in_use);
2499   CLEAR_REG_SET (&deps->reg_conditional_sets);
2500
2501   free (deps->reg_last);
2502 }
2503
2504 /* If it is profitable to use them, initialize caches for tracking
2505    dependency information.  LUID is the number of insns to be scheduled,
2506    it is used in the estimate of profitability.  */
2507
2508 void
2509 init_dependency_caches (int luid)
2510 {
2511   /* Average number of insns in the basic block.
2512      '+ 1' is used to make it nonzero.  */
2513   int insns_in_block = luid / n_basic_blocks + 1;
2514
2515   /* ?!? We could save some memory by computing a per-region luid mapping
2516      which could reduce both the number of vectors in the cache and the size
2517      of each vector.  Instead we just avoid the cache entirely unless the
2518      average number of instructions in a basic block is very high.  See
2519      the comment before the declaration of true_dependency_cache for
2520      what we consider "very high".  */
2521   if (insns_in_block > 100 * 5)
2522     {
2523       cache_size = 0;
2524       extend_dependency_caches (luid, true);
2525     }
2526
2527   dl_pool = create_alloc_pool ("deps_list", sizeof (struct _deps_list),
2528                                /* Allocate lists for one block at a time.  */
2529                                insns_in_block);
2530
2531   dn_pool = create_alloc_pool ("dep_node", sizeof (struct _dep_node),
2532                                /* Allocate nodes for one block at a time.
2533                                   We assume that average insn has
2534                                   5 producers.  */
2535                                5 * insns_in_block);
2536 }
2537
2538 /* Create or extend (depending on CREATE_P) dependency caches to
2539    size N.  */
2540 void
2541 extend_dependency_caches (int n, bool create_p)
2542 {
2543   if (create_p || true_dependency_cache)
2544     {
2545       int i, luid = cache_size + n;
2546
2547       true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
2548                                           luid);
2549       output_dependency_cache = XRESIZEVEC (bitmap_head,
2550                                             output_dependency_cache, luid);
2551       anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
2552                                           luid);
2553
2554       if (current_sched_info->flags & DO_SPECULATION)
2555         spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
2556                                             luid);
2557
2558       for (i = cache_size; i < luid; i++)
2559         {
2560           bitmap_initialize (&true_dependency_cache[i], 0);
2561           bitmap_initialize (&output_dependency_cache[i], 0);
2562           bitmap_initialize (&anti_dependency_cache[i], 0);
2563
2564           if (current_sched_info->flags & DO_SPECULATION)
2565             bitmap_initialize (&spec_dependency_cache[i], 0);
2566         }
2567       cache_size = luid;
2568     }
2569 }
2570
2571 /* Free the caches allocated in init_dependency_caches.  */
2572
2573 void
2574 free_dependency_caches (void)
2575 {
2576   gcc_assert (deps_pools_are_empty_p ());
2577   free_alloc_pool_if_empty (&dn_pool);
2578   free_alloc_pool_if_empty (&dl_pool);
2579   gcc_assert (dn_pool == NULL && dl_pool == NULL);
2580
2581   if (true_dependency_cache)
2582     {
2583       int i;
2584
2585       for (i = 0; i < cache_size; i++)
2586         {
2587           bitmap_clear (&true_dependency_cache[i]);
2588           bitmap_clear (&output_dependency_cache[i]);
2589           bitmap_clear (&anti_dependency_cache[i]);
2590
2591           if (current_sched_info->flags & DO_SPECULATION)
2592             bitmap_clear (&spec_dependency_cache[i]);
2593         }
2594       free (true_dependency_cache);
2595       true_dependency_cache = NULL;
2596       free (output_dependency_cache);
2597       output_dependency_cache = NULL;
2598       free (anti_dependency_cache);
2599       anti_dependency_cache = NULL;
2600
2601       if (current_sched_info->flags & DO_SPECULATION)
2602         {
2603           free (spec_dependency_cache);
2604           spec_dependency_cache = NULL;
2605         }
2606     }
2607 }
2608
2609 /* Initialize some global variables needed by the dependency analysis
2610    code.  */
2611
2612 void
2613 init_deps_global (void)
2614 {
2615   reg_pending_sets = ALLOC_REG_SET (&reg_obstack);
2616   reg_pending_clobbers = ALLOC_REG_SET (&reg_obstack);
2617   reg_pending_uses = ALLOC_REG_SET (&reg_obstack);
2618   reg_pending_barrier = NOT_A_BARRIER;
2619 }
2620
2621 /* Free everything used by the dependency analysis code.  */
2622
2623 void
2624 finish_deps_global (void)
2625 {
2626   FREE_REG_SET (reg_pending_sets);
2627   FREE_REG_SET (reg_pending_clobbers);
2628   FREE_REG_SET (reg_pending_uses);
2629 }
2630
2631 /* Estimate the weakness of dependence between MEM1 and MEM2.  */
2632 static dw_t
2633 estimate_dep_weak (rtx mem1, rtx mem2)
2634 {
2635   rtx r1, r2;
2636
2637   if (mem1 == mem2)
2638     /* MEMs are the same - don't speculate.  */
2639     return MIN_DEP_WEAK;
2640
2641   r1 = XEXP (mem1, 0);
2642   r2 = XEXP (mem2, 0);
2643
2644   if (r1 == r2
2645       || (REG_P (r1) && REG_P (r2)
2646           && REGNO (r1) == REGNO (r2)))
2647     /* Again, MEMs are the same.  */
2648     return MIN_DEP_WEAK;
2649   else if ((REG_P (r1) && !REG_P (r2))
2650            || (!REG_P (r1) && REG_P (r2)))
2651     /* Different addressing modes - reason to be more speculative,
2652        than usual.  */
2653     return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
2654   else
2655     /* We can't say anything about the dependence.  */
2656     return UNCERTAIN_DEP_WEAK;
2657 }
2658
2659 /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
2660    This function can handle same INSN and ELEM (INSN == ELEM).
2661    It is a convenience wrapper.  */
2662 void
2663 add_dependence (rtx insn, rtx elem, enum reg_note dep_type)
2664 {
2665   dep_def _dep, *dep = &_dep;
2666
2667   init_dep (dep, elem, insn, dep_type);
2668   maybe_add_or_update_dep_1 (dep, false, NULL_RTX, NULL_RTX);
2669 }
2670
2671 /* Return weakness of speculative type TYPE in the dep_status DS.  */
2672 static dw_t
2673 get_dep_weak_1 (ds_t ds, ds_t type)
2674 {
2675   ds = ds & type;
2676   switch (type)
2677     {
2678     case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
2679     case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
2680     case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
2681     case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
2682     default: gcc_unreachable ();
2683     }
2684
2685   return (dw_t) ds;
2686 }
2687
2688 /* Return weakness of speculative type TYPE in the dep_status DS.  */
2689 dw_t
2690 get_dep_weak (ds_t ds, ds_t type)
2691 {
2692   dw_t dw = get_dep_weak_1 (ds, type);
2693
2694   gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
2695
2696   return dw;
2697 }
2698
2699 /* Return the dep_status, which has the same parameters as DS, except for
2700    speculative type TYPE, that will have weakness DW.  */
2701 ds_t
2702 set_dep_weak (ds_t ds, ds_t type, dw_t dw)
2703 {
2704   gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
2705
2706   ds &= ~type;
2707   switch (type)
2708     {
2709     case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
2710     case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
2711     case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
2712     case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
2713     default: gcc_unreachable ();
2714     }
2715   return ds;
2716 }
2717
2718 /* Return the join of two dep_statuses DS1 and DS2.  */
2719 ds_t
2720 ds_merge (ds_t ds1, ds_t ds2)
2721 {
2722   ds_t ds, t;
2723
2724   gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
2725
2726   ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
2727
2728   t = FIRST_SPEC_TYPE;
2729   do
2730     {
2731       if ((ds1 & t) && !(ds2 & t))
2732         ds |= ds1 & t;
2733       else if (!(ds1 & t) && (ds2 & t))
2734         ds |= ds2 & t;
2735       else if ((ds1 & t) && (ds2 & t))
2736         {
2737           ds_t dw;
2738
2739           dw = ((ds_t) get_dep_weak (ds1, t)) * ((ds_t) get_dep_weak (ds2, t));
2740           dw /= MAX_DEP_WEAK;
2741           if (dw < MIN_DEP_WEAK)
2742             dw = MIN_DEP_WEAK;
2743
2744           ds = set_dep_weak (ds, t, (dw_t) dw);
2745         }
2746
2747       if (t == LAST_SPEC_TYPE)
2748         break;
2749       t <<= SPEC_TYPE_SHIFT;
2750     }
2751   while (1);
2752
2753   return ds;
2754 }
2755
2756 /* Dump information about the dependence status S.  */
2757 static void
2758 dump_ds (FILE *f, ds_t s)
2759 {
2760   fprintf (f, "{");
2761
2762   if (s & BEGIN_DATA)
2763     fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak_1 (s, BEGIN_DATA));
2764   if (s & BE_IN_DATA)
2765     fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak_1 (s, BE_IN_DATA));
2766   if (s & BEGIN_CONTROL)
2767     fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak_1 (s, BEGIN_CONTROL));
2768   if (s & BE_IN_CONTROL)
2769     fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak_1 (s, BE_IN_CONTROL));
2770
2771   if (s & HARD_DEP)
2772     fprintf (f, "HARD_DEP; ");
2773
2774   if (s & DEP_TRUE)
2775     fprintf (f, "DEP_TRUE; ");
2776   if (s & DEP_ANTI)
2777     fprintf (f, "DEP_ANTI; ");
2778   if (s & DEP_OUTPUT)
2779     fprintf (f, "DEP_OUTPUT; ");
2780
2781   fprintf (f, "}");
2782 }
2783
2784 void
2785 debug_ds (ds_t s)
2786 {
2787   dump_ds (stderr, s);
2788   fprintf (stderr, "\n");
2789 }
2790
2791 #ifdef INSN_SCHEDULING
2792 #ifdef ENABLE_CHECKING
2793 /* Verify that dependence type and status are consistent.
2794    If RELAXED_P is true, then skip dep_weakness checks.  */
2795 static void
2796 check_dep (dep_t dep, bool relaxed_p)
2797 {
2798   enum reg_note dt = DEP_TYPE (dep);
2799   ds_t ds = DEP_STATUS (dep);
2800
2801   gcc_assert (DEP_PRO (dep) != DEP_CON (dep));
2802
2803   if (!(current_sched_info->flags & USE_DEPS_LIST))
2804     {
2805       gcc_assert (ds == -1);
2806       return;
2807     }
2808
2809   /* Check that dependence type contains the same bits as the status.  */
2810   if (dt == REG_DEP_TRUE)
2811     gcc_assert (ds & DEP_TRUE);
2812   else if (dt == REG_DEP_OUTPUT)
2813     gcc_assert ((ds & DEP_OUTPUT)
2814                 && !(ds & DEP_TRUE));    
2815   else 
2816     gcc_assert ((dt == REG_DEP_ANTI)
2817                 && (ds & DEP_ANTI)
2818                 && !(ds & (DEP_OUTPUT | DEP_TRUE)));
2819
2820   /* HARD_DEP can not appear in dep_status of a link.  */
2821   gcc_assert (!(ds & HARD_DEP));          
2822
2823   /* Check that dependence status is set correctly when speculation is not
2824      supported.  */
2825   if (!(current_sched_info->flags & DO_SPECULATION))
2826     gcc_assert (!(ds & SPECULATIVE));
2827   else if (ds & SPECULATIVE)
2828     {
2829       if (!relaxed_p)
2830         {
2831           ds_t type = FIRST_SPEC_TYPE;
2832
2833           /* Check that dependence weakness is in proper range.  */
2834           do
2835             {
2836               if (ds & type)
2837                 get_dep_weak (ds, type);
2838
2839               if (type == LAST_SPEC_TYPE)
2840                 break;
2841               type <<= SPEC_TYPE_SHIFT;
2842             }
2843           while (1);
2844         }
2845
2846       if (ds & BEGIN_SPEC)
2847         {
2848           /* Only true dependence can be data speculative.  */
2849           if (ds & BEGIN_DATA)
2850             gcc_assert (ds & DEP_TRUE);
2851
2852           /* Control dependencies in the insn scheduler are represented by
2853              anti-dependencies, therefore only anti dependence can be
2854              control speculative.  */
2855           if (ds & BEGIN_CONTROL)
2856             gcc_assert (ds & DEP_ANTI);
2857         }
2858       else
2859         {
2860           /* Subsequent speculations should resolve true dependencies.  */
2861           gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
2862         }
2863           
2864       /* Check that true and anti dependencies can't have other speculative 
2865          statuses.  */
2866       if (ds & DEP_TRUE)
2867         gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
2868       /* An output dependence can't be speculative at all.  */
2869       gcc_assert (!(ds & DEP_OUTPUT));
2870       if (ds & DEP_ANTI)
2871         gcc_assert (ds & BEGIN_CONTROL);
2872     }
2873 }
2874 #endif
2875 #endif