OSDN Git Service

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