OSDN Git Service

2c551cfabfbc3e5e1f18eaa4d60cbca6abdebf91
[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 Free Software Foundation, Inc.
5    Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
6    and currently maintained by, Jim Wilson (wilson@cygnus.com)
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING.  If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, USA.  */
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 "basic-block.h"
34 #include "regs.h"
35 #include "function.h"
36 #include "flags.h"
37 #include "insn-config.h"
38 #include "insn-attr.h"
39 #include "except.h"
40 #include "toplev.h"
41 #include "recog.h"
42 #include "sched-int.h"
43 #include "params.h"
44 #include "cselib.h"
45 #include "df.h"
46
47
48 static regset_head reg_pending_sets_head;
49 static regset_head reg_pending_clobbers_head;
50 static regset_head reg_pending_uses_head;
51
52 static regset reg_pending_sets;
53 static regset reg_pending_clobbers;
54 static regset reg_pending_uses;
55
56 /* The following enumeration values tell us what dependencies we
57    should use to implement the barrier.  We use true-dependencies for
58    TRUE_BARRIER and anti-dependencies for MOVE_BARRIER.  */
59 enum reg_pending_barrier_mode
60 {
61   NOT_A_BARRIER = 0,
62   MOVE_BARRIER,
63   TRUE_BARRIER
64 };
65
66 static enum reg_pending_barrier_mode reg_pending_barrier;
67
68 /* To speed up the test for duplicate dependency links we keep a
69    record of dependencies created by add_dependence when the average
70    number of instructions in a basic block is very large.
71
72    Studies have shown that there is typically around 5 instructions between
73    branches for typical C code.  So we can make a guess that the average
74    basic block is approximately 5 instructions long; we will choose 100X
75    the average size as a very large basic block.
76
77    Each insn has associated bitmaps for its dependencies.  Each bitmap
78    has enough entries to represent a dependency on any other insn in
79    the insn chain.  All bitmap for true dependencies cache is
80    allocated then the rest two ones are also allocated.  */
81 static bitmap_head *true_dependency_cache;
82 static bitmap_head *anti_dependency_cache;
83 static bitmap_head *output_dependency_cache;
84 int cache_size;
85
86 /* To speed up checking consistency of formed forward insn
87    dependencies we use the following cache.  Another possible solution
88    could be switching off checking duplication of insns in forward
89    dependencies.  */
90 #ifdef ENABLE_CHECKING
91 static bitmap_head *forward_dependency_cache;
92 #endif
93
94 static int deps_may_trap_p (rtx);
95 static void add_dependence_list (rtx, rtx, enum reg_note);
96 static void add_dependence_list_and_free (rtx, rtx *, enum reg_note);
97 static void set_sched_group_p (rtx);
98
99 static void flush_pending_lists (struct deps *, rtx, int, int);
100 static void sched_analyze_1 (struct deps *, rtx, rtx);
101 static void sched_analyze_2 (struct deps *, rtx, rtx);
102 static void sched_analyze_insn (struct deps *, rtx, rtx, rtx);
103
104 static rtx get_condition (rtx);
105 static int conditions_mutex_p (rtx, rtx);
106 \f
107 /* Return nonzero if a load of the memory reference MEM can cause a trap.  */
108
109 static int
110 deps_may_trap_p (rtx mem)
111 {
112   rtx addr = XEXP (mem, 0);
113
114   if (REG_P (addr) && REGNO (addr) >= FIRST_PSEUDO_REGISTER)
115     {
116       rtx t = get_reg_known_value (REGNO (addr));
117       if (t)
118         addr = t;
119     }
120   return rtx_addr_can_trap_p (addr);
121 }
122 \f
123 /* Return the INSN_LIST containing INSN in LIST, or NULL
124    if LIST does not contain INSN.  */
125
126 rtx
127 find_insn_list (rtx insn, rtx list)
128 {
129   while (list)
130     {
131       if (XEXP (list, 0) == insn)
132         return list;
133       list = XEXP (list, 1);
134     }
135   return 0;
136 }
137 \f
138 /* Find the condition under which INSN is executed.  */
139
140 static rtx
141 get_condition (rtx insn)
142 {
143   rtx pat = PATTERN (insn);
144   rtx src;
145
146   if (pat == 0)
147     return 0;
148
149   if (GET_CODE (pat) == COND_EXEC)
150     return COND_EXEC_TEST (pat);
151
152   if (!any_condjump_p (insn) || !onlyjump_p (insn))
153     return 0;
154
155   src = SET_SRC (pc_set (insn));
156   if (XEXP (src, 2) == pc_rtx)
157     return XEXP (src, 0);
158   else if (XEXP (src, 1) == pc_rtx)
159     {
160       rtx cond = XEXP (src, 0);
161       enum rtx_code revcode = reversed_comparison_code (cond, insn);
162
163       if (revcode == UNKNOWN)
164         return 0;
165       return gen_rtx_fmt_ee (revcode, GET_MODE (cond), XEXP (cond, 0),
166                              XEXP (cond, 1));
167     }
168   else
169     return 0;
170 }
171
172 /* Return nonzero if conditions COND1 and COND2 can never be both true.  */
173
174 static int
175 conditions_mutex_p (rtx cond1, rtx cond2)
176 {
177   if (COMPARISON_P (cond1)
178       && COMPARISON_P (cond2)
179       && GET_CODE (cond1) == reversed_comparison_code (cond2, NULL)
180       && XEXP (cond1, 0) == XEXP (cond2, 0)
181       && XEXP (cond1, 1) == XEXP (cond2, 1))
182     return 1;
183   return 0;
184 }
185 \f
186 /* Add ELEM wrapped in an INSN_LIST with reg note kind DEP_TYPE to the
187    LOG_LINKS of INSN, if not already there.  DEP_TYPE indicates the
188    type of dependence that this link represents.  The function returns
189    nonzero if a new entry has been added to insn's LOG_LINK.  */
190
191 int
192 add_dependence (rtx insn, rtx elem, enum reg_note dep_type)
193 {
194   rtx link;
195   int present_p;
196   rtx cond1, cond2;
197
198   /* Don't depend an insn on itself.  */
199   if (insn == elem)
200     return 0;
201
202   /* We can get a dependency on deleted insns due to optimizations in
203      the register allocation and reloading or due to splitting.  Any
204      such dependency is useless and can be ignored.  */
205   if (NOTE_P (elem))
206     return 0;
207
208   /* flow.c doesn't handle conditional lifetimes entirely correctly;
209      calls mess up the conditional lifetimes.  */
210   /* ??? add_dependence is the wrong place to be eliding dependencies,
211      as that forgets that the condition expressions themselves may
212      be dependent.  */
213   if (!CALL_P (insn) && !CALL_P (elem))
214     {
215       cond1 = get_condition (insn);
216       cond2 = get_condition (elem);
217       if (cond1 && cond2
218           && conditions_mutex_p (cond1, cond2)
219           /* Make sure first instruction doesn't affect condition of second
220              instruction if switched.  */
221           && !modified_in_p (cond1, elem)
222           /* Make sure second instruction doesn't affect condition of first
223              instruction if switched.  */
224           && !modified_in_p (cond2, insn))
225         return 0;
226     }
227
228   present_p = 1;
229 #ifdef INSN_SCHEDULING
230   /* ??? No good way to tell from here whether we're doing interblock
231      scheduling.  Possibly add another callback.  */
232 #if 0
233   /* (This code is guarded by INSN_SCHEDULING, otherwise INSN_BB is undefined.)
234      No need for interblock dependences with calls, since
235      calls are not moved between blocks.   Note: the edge where
236      elem is a CALL is still required.  */
237   if (CALL_P (insn)
238       && (INSN_BB (elem) != INSN_BB (insn)))
239     return 0;
240 #endif
241
242   /* If we already have a dependency for ELEM, then we do not need to
243      do anything.  Avoiding the list walk below can cut compile times
244      dramatically for some code.  */
245   if (true_dependency_cache != NULL)
246     {
247       enum reg_note present_dep_type = 0;
248
249       if (anti_dependency_cache == NULL || output_dependency_cache == NULL)
250         abort ();
251       if (bitmap_bit_p (&true_dependency_cache[INSN_LUID (insn)],
252                         INSN_LUID (elem)))
253         /* Do nothing (present_set_type is already 0).  */
254         ;
255       else if (bitmap_bit_p (&anti_dependency_cache[INSN_LUID (insn)],
256                          INSN_LUID (elem)))
257         present_dep_type = REG_DEP_ANTI;
258       else if (bitmap_bit_p (&output_dependency_cache[INSN_LUID (insn)],
259                          INSN_LUID (elem)))
260         present_dep_type = REG_DEP_OUTPUT;
261       else
262         present_p = 0;
263       if (present_p && (int) dep_type >= (int) present_dep_type)
264         return 0;
265     }
266 #endif
267
268   /* Check that we don't already have this dependence.  */
269   if (present_p)
270     for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
271       if (XEXP (link, 0) == elem)
272         {
273 #ifdef INSN_SCHEDULING
274           /* Clear corresponding cache entry because type of the link
275              may be changed.  */
276           if (true_dependency_cache != NULL)
277             {
278               if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
279                 bitmap_clear_bit (&anti_dependency_cache[INSN_LUID (insn)],
280                                   INSN_LUID (elem));
281               else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
282                        && output_dependency_cache)
283                 bitmap_clear_bit (&output_dependency_cache[INSN_LUID (insn)],
284                                   INSN_LUID (elem));
285               else
286                 abort ();
287             }
288 #endif
289
290           /* If this is a more restrictive type of dependence than the existing
291              one, then change the existing dependence to this type.  */
292           if ((int) dep_type < (int) REG_NOTE_KIND (link))
293             PUT_REG_NOTE_KIND (link, dep_type);
294
295 #ifdef INSN_SCHEDULING
296           /* If we are adding a dependency to INSN's LOG_LINKs, then
297              note that in the bitmap caches of dependency information.  */
298           if (true_dependency_cache != NULL)
299             {
300               if ((int) REG_NOTE_KIND (link) == 0)
301                 bitmap_set_bit (&true_dependency_cache[INSN_LUID (insn)],
302                                 INSN_LUID (elem));
303               else if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
304                 bitmap_set_bit (&anti_dependency_cache[INSN_LUID (insn)],
305                                 INSN_LUID (elem));
306               else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
307                 bitmap_set_bit (&output_dependency_cache[INSN_LUID (insn)],
308                                 INSN_LUID (elem));
309             }
310 #endif
311           return 0;
312         }
313   /* Might want to check one level of transitivity to save conses.  */
314
315   link = alloc_INSN_LIST (elem, LOG_LINKS (insn));
316   LOG_LINKS (insn) = link;
317
318   /* Insn dependency, not data dependency.  */
319   PUT_REG_NOTE_KIND (link, dep_type);
320
321 #ifdef INSN_SCHEDULING
322   /* If we are adding a dependency to INSN's LOG_LINKs, then note that
323      in the bitmap caches of dependency information.  */
324   if (true_dependency_cache != NULL)
325     {
326       if ((int) dep_type == 0)
327         bitmap_set_bit (&true_dependency_cache[INSN_LUID (insn)], INSN_LUID (elem));
328       else if (dep_type == REG_DEP_ANTI)
329         bitmap_set_bit (&anti_dependency_cache[INSN_LUID (insn)], INSN_LUID (elem));
330       else if (dep_type == REG_DEP_OUTPUT)
331         bitmap_set_bit (&output_dependency_cache[INSN_LUID (insn)], INSN_LUID (elem));
332     }
333 #endif
334   return 1;
335 }
336
337 /* A convenience wrapper to operate on an entire list.  */
338
339 static void
340 add_dependence_list (rtx insn, rtx list, enum reg_note dep_type)
341 {
342   for (; list; list = XEXP (list, 1))
343     add_dependence (insn, XEXP (list, 0), dep_type);
344 }
345
346 /* Similar, but free *LISTP at the same time.  */
347
348 static void
349 add_dependence_list_and_free (rtx insn, rtx *listp, enum reg_note dep_type)
350 {
351   rtx list, next;
352   for (list = *listp, *listp = NULL; list ; list = next)
353     {
354       next = XEXP (list, 1);
355       add_dependence (insn, XEXP (list, 0), dep_type);
356       free_INSN_LIST_node (list);
357     }
358 }
359
360 /* Set SCHED_GROUP_P and care for the rest of the bookkeeping that
361    goes along with that.  */
362
363 static void
364 set_sched_group_p (rtx insn)
365 {
366   rtx prev;
367
368   SCHED_GROUP_P (insn) = 1;
369
370   prev = prev_nonnote_insn (insn);
371   add_dependence (insn, prev, REG_DEP_ANTI);
372 }
373 \f
374 /* Process an insn's memory dependencies.  There are four kinds of
375    dependencies:
376
377    (0) read dependence: read follows read
378    (1) true dependence: read follows write
379    (2) anti dependence: write follows read
380    (3) output dependence: write follows write
381
382    We are careful to build only dependencies which actually exist, and
383    use transitivity to avoid building too many links.  */
384
385 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
386    The MEM is a memory reference contained within INSN, which we are saving
387    so that we can do memory aliasing on it.  */
388
389 void
390 add_insn_mem_dependence (struct deps *deps, rtx *insn_list, rtx *mem_list,
391                          rtx insn, rtx mem)
392 {
393   rtx link;
394
395   link = alloc_INSN_LIST (insn, *insn_list);
396   *insn_list = link;
397
398   if (current_sched_info->use_cselib)
399     {
400       mem = shallow_copy_rtx (mem);
401       XEXP (mem, 0) = cselib_subst_to_values (XEXP (mem, 0));
402     }
403   link = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
404   *mem_list = link;
405
406   deps->pending_lists_length++;
407 }
408
409 /* Make a dependency between every memory reference on the pending lists
410    and INSN, thus flushing the pending lists.  FOR_READ is true if emitting
411    dependencies for a read operation, similarly with FOR_WRITE.  */
412
413 static void
414 flush_pending_lists (struct deps *deps, rtx insn, int for_read,
415                      int for_write)
416 {
417   if (for_write)
418     {
419       add_dependence_list_and_free (insn, &deps->pending_read_insns,
420                                     REG_DEP_ANTI);
421       free_EXPR_LIST_list (&deps->pending_read_mems);
422     }
423
424   add_dependence_list_and_free (insn, &deps->pending_write_insns,
425                                 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
426   free_EXPR_LIST_list (&deps->pending_write_mems);
427   deps->pending_lists_length = 0;
428
429   add_dependence_list_and_free (insn, &deps->last_pending_memory_flush,
430                                 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
431   deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
432   deps->pending_flush_length = 1;
433 }
434 \f
435 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
436    rtx, X, creating all dependencies generated by the write to the
437    destination of X, and reads of everything mentioned.  */
438
439 static void
440 sched_analyze_1 (struct deps *deps, rtx x, rtx insn)
441 {
442   int regno;
443   rtx dest = XEXP (x, 0);
444   enum rtx_code code = GET_CODE (x);
445
446   if (dest == 0)
447     return;
448
449   if (GET_CODE (dest) == PARALLEL)
450     {
451       int i;
452
453       for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
454         if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
455           sched_analyze_1 (deps,
456                            gen_rtx_CLOBBER (VOIDmode,
457                                             XEXP (XVECEXP (dest, 0, i), 0)),
458                            insn);
459
460       if (GET_CODE (x) == SET)
461         sched_analyze_2 (deps, SET_SRC (x), insn);
462       return;
463     }
464
465   while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
466          || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
467     {
468       if (GET_CODE (dest) == STRICT_LOW_PART
469          || GET_CODE (dest) == ZERO_EXTRACT
470          || GET_CODE (dest) == SIGN_EXTRACT
471          || read_modify_subreg_p (dest))
472         {
473           /* These both read and modify the result.  We must handle
474              them as writes to get proper dependencies for following
475              instructions.  We must handle them as reads to get proper
476              dependencies from this to previous instructions.
477              Thus we need to call sched_analyze_2.  */
478
479           sched_analyze_2 (deps, XEXP (dest, 0), insn);
480         }
481       if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
482         {
483           /* The second and third arguments are values read by this insn.  */
484           sched_analyze_2 (deps, XEXP (dest, 1), insn);
485           sched_analyze_2 (deps, XEXP (dest, 2), insn);
486         }
487       dest = XEXP (dest, 0);
488     }
489
490   if (REG_P (dest))
491     {
492       regno = REGNO (dest);
493
494       /* A hard reg in a wide mode may really be multiple registers.
495          If so, mark all of them just like the first.  */
496       if (regno < FIRST_PSEUDO_REGISTER)
497         {
498           int i = hard_regno_nregs[regno][GET_MODE (dest)];
499           if (code == SET)
500             {
501               while (--i >= 0)
502                 SET_REGNO_REG_SET (reg_pending_sets, regno + i);
503             }
504           else
505             {
506               while (--i >= 0)
507                 SET_REGNO_REG_SET (reg_pending_clobbers, regno + i);
508             }
509         }
510       /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
511          it does not reload.  Ignore these as they have served their
512          purpose already.  */
513       else if (regno >= deps->max_reg)
514         {
515           if (GET_CODE (PATTERN (insn)) != USE
516               && GET_CODE (PATTERN (insn)) != CLOBBER)
517             abort ();
518         }
519       else
520         {
521           if (code == SET)
522             SET_REGNO_REG_SET (reg_pending_sets, regno);
523           else
524             SET_REGNO_REG_SET (reg_pending_clobbers, regno);
525
526           /* Pseudos that are REG_EQUIV to something may be replaced
527              by that during reloading.  We need only add dependencies for
528              the address in the REG_EQUIV note.  */
529           if (!reload_completed && get_reg_known_equiv_p (regno))
530             {
531               rtx t = get_reg_known_value (regno);
532               if (MEM_P (t))
533                 sched_analyze_2 (deps, XEXP (t, 0), insn);
534             }
535
536           /* Don't let it cross a call after scheduling if it doesn't
537              already cross one.  */
538           if (REG_N_CALLS_CROSSED (regno) == 0)
539             add_dependence_list (insn, deps->last_function_call, REG_DEP_ANTI);
540         }
541     }
542   else if (MEM_P (dest))
543     {
544       /* Writing memory.  */
545       rtx t = dest;
546
547       if (current_sched_info->use_cselib)
548         {
549           t = shallow_copy_rtx (dest);
550           cselib_lookup (XEXP (t, 0), Pmode, 1);
551           XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
552         }
553       t = canon_rtx (t);
554
555       if (deps->pending_lists_length > MAX_PENDING_LIST_LENGTH)
556         {
557           /* Flush all pending reads and writes to prevent the pending lists
558              from getting any larger.  Insn scheduling runs too slowly when
559              these lists get long.  When compiling GCC with itself,
560              this flush occurs 8 times for sparc, and 10 times for m88k using
561              the default value of 32.  */
562           flush_pending_lists (deps, insn, false, true);
563         }
564       else
565         {
566           rtx pending, pending_mem;
567
568           pending = deps->pending_read_insns;
569           pending_mem = deps->pending_read_mems;
570           while (pending)
571             {
572               if (anti_dependence (XEXP (pending_mem, 0), t))
573                 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
574
575               pending = XEXP (pending, 1);
576               pending_mem = XEXP (pending_mem, 1);
577             }
578
579           pending = deps->pending_write_insns;
580           pending_mem = deps->pending_write_mems;
581           while (pending)
582             {
583               if (output_dependence (XEXP (pending_mem, 0), t))
584                 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
585
586               pending = XEXP (pending, 1);
587               pending_mem = XEXP (pending_mem, 1);
588             }
589
590           add_dependence_list (insn, deps->last_pending_memory_flush,
591                                REG_DEP_ANTI);
592
593           add_insn_mem_dependence (deps, &deps->pending_write_insns,
594                                    &deps->pending_write_mems, insn, dest);
595         }
596       sched_analyze_2 (deps, XEXP (dest, 0), insn);
597     }
598
599   /* Analyze reads.  */
600   if (GET_CODE (x) == SET)
601     sched_analyze_2 (deps, SET_SRC (x), insn);
602 }
603
604 /* Analyze the uses of memory and registers in rtx X in INSN.  */
605
606 static void
607 sched_analyze_2 (struct deps *deps, rtx x, rtx insn)
608 {
609   int i;
610   int j;
611   enum rtx_code code;
612   const char *fmt;
613
614   if (x == 0)
615     return;
616
617   code = GET_CODE (x);
618
619   switch (code)
620     {
621     case CONST_INT:
622     case CONST_DOUBLE:
623     case CONST_VECTOR:
624     case SYMBOL_REF:
625     case CONST:
626     case LABEL_REF:
627       /* Ignore constants.  Note that we must handle CONST_DOUBLE here
628          because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
629          this does not mean that this insn is using cc0.  */
630       return;
631
632 #ifdef HAVE_cc0
633     case CC0:
634       /* User of CC0 depends on immediately preceding insn.  */
635       set_sched_group_p (insn);
636        /* Don't move CC0 setter to another block (it can set up the
637         same flag for previous CC0 users which is safe).  */
638       CANT_MOVE (prev_nonnote_insn (insn)) = 1;
639       return;
640 #endif
641
642     case REG:
643       {
644         int regno = REGNO (x);
645         if (regno < FIRST_PSEUDO_REGISTER)
646           {
647             int i = hard_regno_nregs[regno][GET_MODE (x)];
648             while (--i >= 0)
649               SET_REGNO_REG_SET (reg_pending_uses, regno + i);
650           }
651         /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
652            it does not reload.  Ignore these as they have served their
653            purpose already.  */
654         else if (regno >= deps->max_reg)
655           {
656             if (GET_CODE (PATTERN (insn)) != USE
657                 && GET_CODE (PATTERN (insn)) != CLOBBER)
658               abort ();
659           }
660         else
661           {
662             SET_REGNO_REG_SET (reg_pending_uses, regno);
663
664             /* Pseudos that are REG_EQUIV to something may be replaced
665                by that during reloading.  We need only add dependencies for
666                the address in the REG_EQUIV note.  */
667             if (!reload_completed && get_reg_known_equiv_p (regno))
668               {
669                 rtx t = get_reg_known_value (regno);
670                 if (MEM_P (t))
671                   sched_analyze_2 (deps, XEXP (t, 0), insn);
672               }
673
674             /* If the register does not already cross any calls, then add this
675                insn to the sched_before_next_call list so that it will still
676                not cross calls after scheduling.  */
677             if (REG_N_CALLS_CROSSED (regno) == 0)
678               deps->sched_before_next_call
679                 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
680           }
681         return;
682       }
683
684     case MEM:
685       {
686         /* Reading memory.  */
687         rtx u;
688         rtx pending, pending_mem;
689         rtx t = x;
690
691         if (current_sched_info->use_cselib)
692           {
693             t = shallow_copy_rtx (t);
694             cselib_lookup (XEXP (t, 0), Pmode, 1);
695             XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
696           }
697         t = canon_rtx (t);
698         pending = deps->pending_read_insns;
699         pending_mem = deps->pending_read_mems;
700         while (pending)
701           {
702             if (read_dependence (XEXP (pending_mem, 0), t))
703               add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
704
705             pending = XEXP (pending, 1);
706             pending_mem = XEXP (pending_mem, 1);
707           }
708
709         pending = deps->pending_write_insns;
710         pending_mem = deps->pending_write_mems;
711         while (pending)
712           {
713             if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
714                                  t, rtx_varies_p))
715               add_dependence (insn, XEXP (pending, 0), 0);
716
717             pending = XEXP (pending, 1);
718             pending_mem = XEXP (pending_mem, 1);
719           }
720
721         for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
722           if (!JUMP_P (XEXP (u, 0))
723               || deps_may_trap_p (x))
724             add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
725
726         /* Always add these dependencies to pending_reads, since
727            this insn may be followed by a write.  */
728         add_insn_mem_dependence (deps, &deps->pending_read_insns,
729                                  &deps->pending_read_mems, insn, x);
730
731         /* Take advantage of tail recursion here.  */
732         sched_analyze_2 (deps, XEXP (x, 0), insn);
733         return;
734       }
735
736     /* Force pending stores to memory in case a trap handler needs them.  */
737     case TRAP_IF:
738       flush_pending_lists (deps, insn, true, false);
739       break;
740
741     case ASM_OPERANDS:
742     case ASM_INPUT:
743     case UNSPEC_VOLATILE:
744       {
745         /* Traditional and volatile asm instructions must be considered to use
746            and clobber all hard registers, all pseudo-registers and all of
747            memory.  So must TRAP_IF and UNSPEC_VOLATILE operations.
748
749            Consider for instance a volatile asm that changes the fpu rounding
750            mode.  An insn should not be moved across this even if it only uses
751            pseudo-regs because it might give an incorrectly rounded result.  */
752         if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
753           reg_pending_barrier = TRUE_BARRIER;
754
755         /* For all ASM_OPERANDS, we must traverse the vector of input operands.
756            We can not just fall through here since then we would be confused
757            by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
758            traditional asms unlike their normal usage.  */
759
760         if (code == ASM_OPERANDS)
761           {
762             for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
763               sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
764             return;
765           }
766         break;
767       }
768
769     case PRE_DEC:
770     case POST_DEC:
771     case PRE_INC:
772     case POST_INC:
773       /* These both read and modify the result.  We must handle them as writes
774          to get proper dependencies for following instructions.  We must handle
775          them as reads to get proper dependencies from this to previous
776          instructions.  Thus we need to pass them to both sched_analyze_1
777          and sched_analyze_2.  We must call sched_analyze_2 first in order
778          to get the proper antecedent for the read.  */
779       sched_analyze_2 (deps, XEXP (x, 0), insn);
780       sched_analyze_1 (deps, x, insn);
781       return;
782
783     case POST_MODIFY:
784     case PRE_MODIFY:
785       /* op0 = op0 + op1 */
786       sched_analyze_2 (deps, XEXP (x, 0), insn);
787       sched_analyze_2 (deps, XEXP (x, 1), insn);
788       sched_analyze_1 (deps, x, insn);
789       return;
790
791     default:
792       break;
793     }
794
795   /* Other cases: walk the insn.  */
796   fmt = GET_RTX_FORMAT (code);
797   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
798     {
799       if (fmt[i] == 'e')
800         sched_analyze_2 (deps, XEXP (x, i), insn);
801       else if (fmt[i] == 'E')
802         for (j = 0; j < XVECLEN (x, i); j++)
803           sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
804     }
805 }
806
807 /* Analyze an INSN with pattern X to find all dependencies.  */
808
809 static void
810 sched_analyze_insn (struct deps *deps, rtx x, rtx insn, rtx loop_notes)
811 {
812   RTX_CODE code = GET_CODE (x);
813   rtx link;
814   int i;
815
816   if (code == COND_EXEC)
817     {
818       sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
819
820       /* ??? Should be recording conditions so we reduce the number of
821          false dependencies.  */
822       x = COND_EXEC_CODE (x);
823       code = GET_CODE (x);
824     }
825   if (code == SET || code == CLOBBER)
826     {
827       sched_analyze_1 (deps, x, insn);
828
829       /* Bare clobber insns are used for letting life analysis, reg-stack
830          and others know that a value is dead.  Depend on the last call
831          instruction so that reg-stack won't get confused.  */
832       if (code == CLOBBER)
833         add_dependence_list (insn, deps->last_function_call, REG_DEP_OUTPUT);
834     }
835   else if (code == PARALLEL)
836     {
837       int i;
838       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
839         {
840           rtx sub = XVECEXP (x, 0, i);
841           code = GET_CODE (sub);
842
843           if (code == COND_EXEC)
844             {
845               sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
846               sub = COND_EXEC_CODE (sub);
847               code = GET_CODE (sub);
848             }
849           if (code == SET || code == CLOBBER)
850             sched_analyze_1 (deps, sub, insn);
851           else
852             sched_analyze_2 (deps, sub, insn);
853         }
854     }
855   else
856     sched_analyze_2 (deps, x, insn);
857
858   /* Mark registers CLOBBERED or used by called function.  */
859   if (CALL_P (insn))
860     {
861       for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
862         {
863           if (GET_CODE (XEXP (link, 0)) == CLOBBER)
864             sched_analyze_1 (deps, XEXP (link, 0), insn);
865           else
866             sched_analyze_2 (deps, XEXP (link, 0), insn);
867         }
868       if (find_reg_note (insn, REG_SETJMP, NULL))
869         reg_pending_barrier = MOVE_BARRIER;
870     }
871
872   if (JUMP_P (insn))
873     {
874       rtx next;
875       next = next_nonnote_insn (insn);
876       if (next && BARRIER_P (next))
877         reg_pending_barrier = TRUE_BARRIER;
878       else
879         {
880           rtx pending, pending_mem;
881           regset_head tmp_uses, tmp_sets;
882           INIT_REG_SET (&tmp_uses);
883           INIT_REG_SET (&tmp_sets);
884
885           (*current_sched_info->compute_jump_reg_dependencies)
886             (insn, &deps->reg_conditional_sets, &tmp_uses, &tmp_sets);
887           /* Make latency of jump equal to 0 by using anti-dependence.  */
888           EXECUTE_IF_SET_IN_REG_SET (&tmp_uses, 0, i,
889             {
890               struct deps_reg *reg_last = &deps->reg_last[i];
891               add_dependence_list (insn, reg_last->sets, REG_DEP_ANTI);
892               add_dependence_list (insn, reg_last->clobbers, REG_DEP_ANTI);
893               reg_last->uses_length++;
894               reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
895             });
896           IOR_REG_SET (reg_pending_sets, &tmp_sets);
897
898           CLEAR_REG_SET (&tmp_uses);
899           CLEAR_REG_SET (&tmp_sets);
900
901           /* All memory writes and volatile reads must happen before the
902              jump.  Non-volatile reads must happen before the jump iff
903              the result is needed by the above register used mask.  */
904
905           pending = deps->pending_write_insns;
906           pending_mem = deps->pending_write_mems;
907           while (pending)
908             {
909               add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
910               pending = XEXP (pending, 1);
911               pending_mem = XEXP (pending_mem, 1);
912             }
913
914           pending = deps->pending_read_insns;
915           pending_mem = deps->pending_read_mems;
916           while (pending)
917             {
918               if (MEM_VOLATILE_P (XEXP (pending_mem, 0)))
919                 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
920               pending = XEXP (pending, 1);
921               pending_mem = XEXP (pending_mem, 1);
922             }
923
924           add_dependence_list (insn, deps->last_pending_memory_flush,
925                                REG_DEP_ANTI);
926         }
927     }
928
929   /* If there is a {LOOP,EHREGION}_{BEG,END} note in the middle of a basic
930      block, then we must be sure that no instructions are scheduled across it.
931      Otherwise, the reg_n_refs info (which depends on loop_depth) would
932      become incorrect.  */
933   if (loop_notes)
934     {
935       rtx link;
936
937       /* Update loop_notes with any notes from this insn.  Also determine
938          if any of the notes on the list correspond to instruction scheduling
939          barriers (loop, eh & setjmp notes, but not range notes).  */
940       link = loop_notes;
941       while (XEXP (link, 1))
942         {
943           if (INTVAL (XEXP (link, 0)) == NOTE_INSN_LOOP_BEG
944               || INTVAL (XEXP (link, 0)) == NOTE_INSN_LOOP_END
945               || INTVAL (XEXP (link, 0)) == NOTE_INSN_EH_REGION_BEG
946               || INTVAL (XEXP (link, 0)) == NOTE_INSN_EH_REGION_END)
947             reg_pending_barrier = MOVE_BARRIER;
948
949           link = XEXP (link, 1);
950         }
951       XEXP (link, 1) = REG_NOTES (insn);
952       REG_NOTES (insn) = loop_notes;
953     }
954
955   /* If this instruction can throw an exception, then moving it changes
956      where block boundaries fall.  This is mighty confusing elsewhere.
957      Therefore, prevent such an instruction from being moved.  */
958   if (can_throw_internal (insn))
959     reg_pending_barrier = MOVE_BARRIER;
960
961   /* Add dependencies if a scheduling barrier was found.  */
962   if (reg_pending_barrier)
963     {
964       /* In the case of barrier the most added dependencies are not
965          real, so we use anti-dependence here.  */
966       if (GET_CODE (PATTERN (insn)) == COND_EXEC)
967         {
968           EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
969             {
970               struct deps_reg *reg_last = &deps->reg_last[i];
971               add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
972               add_dependence_list
973                 (insn, reg_last->sets,
974                  reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
975               add_dependence_list
976                 (insn, reg_last->clobbers,
977                  reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
978             });
979         }
980       else
981         {
982           EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
983             {
984               struct deps_reg *reg_last = &deps->reg_last[i];
985               add_dependence_list_and_free (insn, &reg_last->uses,
986                                             REG_DEP_ANTI);
987               add_dependence_list_and_free
988                 (insn, &reg_last->sets,
989                  reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
990               add_dependence_list_and_free
991                 (insn, &reg_last->clobbers,
992                  reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
993               reg_last->uses_length = 0;
994               reg_last->clobbers_length = 0;
995             });
996         }
997
998       for (i = 0; i < deps->max_reg; i++)
999         {
1000           struct deps_reg *reg_last = &deps->reg_last[i];
1001           reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1002           SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
1003         }
1004
1005       flush_pending_lists (deps, insn, true, true);
1006       CLEAR_REG_SET (&deps->reg_conditional_sets);
1007       reg_pending_barrier = NOT_A_BARRIER;
1008     }
1009   else
1010     {
1011       /* If the current insn is conditional, we can't free any
1012          of the lists.  */
1013       if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1014         {
1015           EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i,
1016             {
1017               struct deps_reg *reg_last = &deps->reg_last[i];
1018               add_dependence_list (insn, reg_last->sets, 0);
1019               add_dependence_list (insn, reg_last->clobbers, 0);
1020               reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1021               reg_last->uses_length++;
1022             });
1023           EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i,
1024             {
1025               struct deps_reg *reg_last = &deps->reg_last[i];
1026               add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1027               add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1028               reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
1029               reg_last->clobbers_length++;
1030             });
1031           EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
1032             {
1033               struct deps_reg *reg_last = &deps->reg_last[i];
1034               add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1035               add_dependence_list (insn, reg_last->clobbers, REG_DEP_OUTPUT);
1036               add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1037               reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1038               SET_REGNO_REG_SET (&deps->reg_conditional_sets, i);
1039             });
1040         }
1041       else
1042         {
1043           EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i,
1044             {
1045               struct deps_reg *reg_last = &deps->reg_last[i];
1046               add_dependence_list (insn, reg_last->sets, 0);
1047               add_dependence_list (insn, reg_last->clobbers, 0);
1048               reg_last->uses_length++;
1049               reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1050             });
1051           EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i,
1052             {
1053               struct deps_reg *reg_last = &deps->reg_last[i];
1054               if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
1055                   || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
1056                 {
1057                   add_dependence_list_and_free (insn, &reg_last->sets,
1058                                                 REG_DEP_OUTPUT);
1059                   add_dependence_list_and_free (insn, &reg_last->uses,
1060                                                 REG_DEP_ANTI);
1061                   add_dependence_list_and_free (insn, &reg_last->clobbers,
1062                                                 REG_DEP_OUTPUT);
1063                   reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1064                   reg_last->clobbers_length = 0;
1065                   reg_last->uses_length = 0;
1066                 }
1067               else
1068                 {
1069                   add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1070                   add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1071                 }
1072               reg_last->clobbers_length++;
1073               reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
1074             });
1075           EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
1076             {
1077               struct deps_reg *reg_last = &deps->reg_last[i];
1078               add_dependence_list_and_free (insn, &reg_last->sets,
1079                                             REG_DEP_OUTPUT);
1080               add_dependence_list_and_free (insn, &reg_last->clobbers,
1081                                             REG_DEP_OUTPUT);
1082               add_dependence_list_and_free (insn, &reg_last->uses,
1083                                             REG_DEP_ANTI);
1084               reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1085               reg_last->uses_length = 0;
1086               reg_last->clobbers_length = 0;
1087               CLEAR_REGNO_REG_SET (&deps->reg_conditional_sets, i);
1088             });
1089         }
1090
1091       IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
1092       IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
1093       IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
1094     }
1095   CLEAR_REG_SET (reg_pending_uses);
1096   CLEAR_REG_SET (reg_pending_clobbers);
1097   CLEAR_REG_SET (reg_pending_sets);
1098
1099   /* If we are currently in a libcall scheduling group, then mark the
1100      current insn as being in a scheduling group and that it can not
1101      be moved into a different basic block.  */
1102
1103   if (deps->libcall_block_tail_insn)
1104     {
1105       set_sched_group_p (insn);
1106       CANT_MOVE (insn) = 1;
1107     }
1108
1109   /* If a post-call group is still open, see if it should remain so.
1110      This insn must be a simple move of a hard reg to a pseudo or
1111      vice-versa.
1112
1113      We must avoid moving these insns for correctness on
1114      SMALL_REGISTER_CLASS machines, and for special registers like
1115      PIC_OFFSET_TABLE_REGNUM.  For simplicity, extend this to all
1116      hard regs for all targets.  */
1117
1118   if (deps->in_post_call_group_p)
1119     {
1120       rtx tmp, set = single_set (insn);
1121       int src_regno, dest_regno;
1122
1123       if (set == NULL)
1124         goto end_call_group;
1125
1126       tmp = SET_DEST (set);
1127       if (GET_CODE (tmp) == SUBREG)
1128         tmp = SUBREG_REG (tmp);
1129       if (REG_P (tmp))
1130         dest_regno = REGNO (tmp);
1131       else
1132         goto end_call_group;
1133
1134       tmp = SET_SRC (set);
1135       if (GET_CODE (tmp) == SUBREG)
1136         tmp = SUBREG_REG (tmp);
1137       if ((GET_CODE (tmp) == PLUS
1138            || GET_CODE (tmp) == MINUS)
1139           && REG_P (XEXP (tmp, 0))
1140           && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
1141           && dest_regno == STACK_POINTER_REGNUM)
1142         src_regno = STACK_POINTER_REGNUM;
1143       else if (REG_P (tmp))
1144         src_regno = REGNO (tmp);
1145       else
1146         goto end_call_group;
1147
1148       if (src_regno < FIRST_PSEUDO_REGISTER
1149           || dest_regno < FIRST_PSEUDO_REGISTER)
1150         {
1151           /* If we are inside a post-call group right at the start of the
1152              scheduling region, we must not add a dependency.  */
1153           if (deps->in_post_call_group_p == post_call_initial)
1154             {
1155               SCHED_GROUP_P (insn) = 1;
1156               deps->in_post_call_group_p = post_call;
1157             }
1158           else
1159             set_sched_group_p (insn);
1160           CANT_MOVE (insn) = 1;
1161         }
1162       else
1163         {
1164         end_call_group:
1165           deps->in_post_call_group_p = not_post_call;
1166         }
1167     }
1168 }
1169
1170 /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
1171    for every dependency.  */
1172
1173 void
1174 sched_analyze (struct deps *deps, rtx head, rtx tail)
1175 {
1176   rtx insn;
1177   rtx loop_notes = 0;
1178
1179   if (current_sched_info->use_cselib)
1180     cselib_init (true);
1181
1182   /* Before reload, if the previous block ended in a call, show that
1183      we are inside a post-call group, so as to keep the lifetimes of
1184      hard registers correct.  */
1185   if (! reload_completed && !LABEL_P (head))
1186     {
1187       insn = prev_nonnote_insn (head);
1188       if (insn && CALL_P (insn))
1189         deps->in_post_call_group_p = post_call_initial;
1190     }
1191   for (insn = head;; insn = NEXT_INSN (insn))
1192     {
1193       rtx link, end_seq, r0, set;
1194
1195       if (NONJUMP_INSN_P (insn) || JUMP_P (insn))
1196         {
1197           /* Clear out the stale LOG_LINKS from flow.  */
1198           free_INSN_LIST_list (&LOG_LINKS (insn));
1199
1200           /* Make each JUMP_INSN a scheduling barrier for memory
1201              references.  */
1202           if (JUMP_P (insn))
1203             {
1204               /* Keep the list a reasonable size.  */
1205               if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
1206                 flush_pending_lists (deps, insn, true, true);
1207               else
1208                 deps->last_pending_memory_flush
1209                   = alloc_INSN_LIST (insn, deps->last_pending_memory_flush);
1210             }
1211           sched_analyze_insn (deps, PATTERN (insn), insn, loop_notes);
1212           loop_notes = 0;
1213         }
1214       else if (CALL_P (insn))
1215         {
1216           int i;
1217
1218           CANT_MOVE (insn) = 1;
1219
1220           /* Clear out the stale LOG_LINKS from flow.  */
1221           free_INSN_LIST_list (&LOG_LINKS (insn));
1222
1223           if (find_reg_note (insn, REG_SETJMP, NULL))
1224             {
1225               /* This is setjmp.  Assume that all registers, not just
1226                  hard registers, may be clobbered by this call.  */
1227               reg_pending_barrier = MOVE_BARRIER;
1228             }
1229           else
1230             {
1231               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1232                 /* A call may read and modify global register variables.  */
1233                 if (global_regs[i])
1234                   {
1235                     SET_REGNO_REG_SET (reg_pending_sets, i);
1236                     SET_REGNO_REG_SET (reg_pending_uses, i);
1237                   }
1238                 /* Other call-clobbered hard regs may be clobbered.
1239                    Since we only have a choice between 'might be clobbered'
1240                    and 'definitely not clobbered', we must include all
1241                    partly call-clobbered registers here.  */
1242                 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
1243                          || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1244                   SET_REGNO_REG_SET (reg_pending_clobbers, i);
1245                 /* We don't know what set of fixed registers might be used
1246                    by the function, but it is certain that the stack pointer
1247                    is among them, but be conservative.  */
1248                 else if (fixed_regs[i])
1249                   SET_REGNO_REG_SET (reg_pending_uses, i);
1250                 /* The frame pointer is normally not used by the function
1251                    itself, but by the debugger.  */
1252                 /* ??? MIPS o32 is an exception.  It uses the frame pointer
1253                    in the macro expansion of jal but does not represent this
1254                    fact in the call_insn rtl.  */
1255                 else if (i == FRAME_POINTER_REGNUM
1256                          || (i == HARD_FRAME_POINTER_REGNUM
1257                              && (! reload_completed || frame_pointer_needed)))
1258                   SET_REGNO_REG_SET (reg_pending_uses, i);
1259             }
1260
1261           /* For each insn which shouldn't cross a call, add a dependence
1262              between that insn and this call insn.  */
1263           add_dependence_list_and_free (insn, &deps->sched_before_next_call,
1264                                         REG_DEP_ANTI);
1265
1266           sched_analyze_insn (deps, PATTERN (insn), insn, loop_notes);
1267           loop_notes = 0;
1268
1269           /* In the absence of interprocedural alias analysis, we must flush
1270              all pending reads and writes, and start new dependencies starting
1271              from here.  But only flush writes for constant calls (which may
1272              be passed a pointer to something we haven't written yet).  */
1273           flush_pending_lists (deps, insn, true, !CONST_OR_PURE_CALL_P (insn));
1274
1275           /* Remember the last function call for limiting lifetimes.  */
1276           free_INSN_LIST_list (&deps->last_function_call);
1277           deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
1278
1279           /* Before reload, begin a post-call group, so as to keep the
1280              lifetimes of hard registers correct.  */
1281           if (! reload_completed)
1282             deps->in_post_call_group_p = post_call;
1283         }
1284
1285       /* See comments on reemit_notes as to why we do this.
1286          ??? Actually, the reemit_notes just say what is done, not why.  */
1287
1288       if (NOTE_P (insn)
1289                && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
1290                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
1291                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1292                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1293         {
1294           rtx rtx_region;
1295
1296           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1297               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
1298             rtx_region = GEN_INT (NOTE_EH_HANDLER (insn));
1299           else
1300             rtx_region = const0_rtx;
1301
1302           loop_notes = alloc_EXPR_LIST (REG_SAVE_NOTE,
1303                                         rtx_region,
1304                                         loop_notes);
1305           loop_notes = alloc_EXPR_LIST (REG_SAVE_NOTE,
1306                                         GEN_INT (NOTE_LINE_NUMBER (insn)),
1307                                         loop_notes);
1308           CONST_OR_PURE_CALL_P (loop_notes) = CONST_OR_PURE_CALL_P (insn);
1309         }
1310
1311       if (current_sched_info->use_cselib)
1312         cselib_process_insn (insn);
1313
1314       /* Now that we have completed handling INSN, check and see if it is
1315          a CLOBBER beginning a libcall block.   If it is, record the
1316          end of the libcall sequence.
1317
1318          We want to schedule libcall blocks as a unit before reload.  While
1319          this restricts scheduling, it preserves the meaning of a libcall
1320          block.
1321
1322          As a side effect, we may get better code due to decreased register
1323          pressure as well as less chance of a foreign insn appearing in
1324          a libcall block.  */
1325       if (!reload_completed
1326           /* Note we may have nested libcall sequences.  We only care about
1327              the outermost libcall sequence.  */
1328           && deps->libcall_block_tail_insn == 0
1329           /* The sequence must start with a clobber of a register.  */
1330           && NONJUMP_INSN_P (insn)
1331           && GET_CODE (PATTERN (insn)) == CLOBBER
1332           && (r0 = XEXP (PATTERN (insn), 0), REG_P (r0))
1333           && REG_P (XEXP (PATTERN (insn), 0))
1334           /* The CLOBBER must also have a REG_LIBCALL note attached.  */
1335           && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1336           && (end_seq = XEXP (link, 0)) != 0
1337           /* The insn referenced by the REG_LIBCALL note must be a
1338              simple nop copy with the same destination as the register
1339              mentioned in the clobber.  */
1340           && (set = single_set (end_seq)) != 0
1341           && SET_DEST (set) == r0 && SET_SRC (set) == r0
1342           /* And finally the insn referenced by the REG_LIBCALL must
1343              also contain a REG_EQUAL note and a REG_RETVAL note.  */
1344           && find_reg_note (end_seq, REG_EQUAL, NULL_RTX) != 0
1345           && find_reg_note (end_seq, REG_RETVAL, NULL_RTX) != 0)
1346         deps->libcall_block_tail_insn = XEXP (link, 0);
1347
1348       /* If we have reached the end of a libcall block, then close the
1349          block.  */
1350       if (deps->libcall_block_tail_insn == insn)
1351         deps->libcall_block_tail_insn = 0;
1352
1353       if (insn == tail)
1354         {
1355           if (current_sched_info->use_cselib)
1356             cselib_finish ();
1357           return;
1358         }
1359     }
1360   abort ();
1361 }
1362 \f
1363
1364 /* The following function adds forward dependence (FROM, TO) with
1365    given DEP_TYPE.  The forward dependence should be not exist before.  */
1366
1367 void
1368 add_forward_dependence (rtx from, rtx to, enum reg_note dep_type)
1369 {
1370   rtx new_link;
1371
1372 #ifdef ENABLE_CHECKING
1373   /* If add_dependence is working properly there should never
1374      be notes, deleted insns or duplicates in the backward
1375      links.  Thus we need not check for them here.
1376
1377      However, if we have enabled checking we might as well go
1378      ahead and verify that add_dependence worked properly.  */
1379   if (NOTE_P (from)
1380       || INSN_DELETED_P (from)
1381       || (forward_dependency_cache != NULL
1382           && bitmap_bit_p (&forward_dependency_cache[INSN_LUID (from)],
1383                            INSN_LUID (to)))
1384       || (forward_dependency_cache == NULL
1385           && find_insn_list (to, INSN_DEPEND (from))))
1386     abort ();
1387   if (forward_dependency_cache != NULL)
1388     bitmap_bit_p (&forward_dependency_cache[INSN_LUID (from)],
1389                   INSN_LUID (to));
1390 #endif
1391
1392   new_link = alloc_INSN_LIST (to, INSN_DEPEND (from));
1393
1394   PUT_REG_NOTE_KIND (new_link, dep_type);
1395
1396   INSN_DEPEND (from) = new_link;
1397   INSN_DEP_COUNT (to) += 1;
1398 }
1399
1400 /* Examine insns in the range [ HEAD, TAIL ] and Use the backward
1401    dependences from LOG_LINKS to build forward dependences in
1402    INSN_DEPEND.  */
1403
1404 void
1405 compute_forward_dependences (rtx head, rtx tail)
1406 {
1407   rtx insn, link;
1408   rtx next_tail;
1409
1410   next_tail = NEXT_INSN (tail);
1411   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1412     {
1413       if (! INSN_P (insn))
1414         continue;
1415
1416       for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1417         add_forward_dependence (XEXP (link, 0), insn, REG_NOTE_KIND (link));
1418     }
1419 }
1420 \f
1421 /* Initialize variables for region data dependence analysis.
1422    n_bbs is the number of region blocks.  */
1423
1424 void
1425 init_deps (struct deps *deps)
1426 {
1427   int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
1428
1429   deps->max_reg = max_reg;
1430   deps->reg_last = xcalloc (max_reg, sizeof (struct deps_reg));
1431   INIT_REG_SET (&deps->reg_last_in_use);
1432   INIT_REG_SET (&deps->reg_conditional_sets);
1433
1434   deps->pending_read_insns = 0;
1435   deps->pending_read_mems = 0;
1436   deps->pending_write_insns = 0;
1437   deps->pending_write_mems = 0;
1438   deps->pending_lists_length = 0;
1439   deps->pending_flush_length = 0;
1440   deps->last_pending_memory_flush = 0;
1441   deps->last_function_call = 0;
1442   deps->sched_before_next_call = 0;
1443   deps->in_post_call_group_p = not_post_call;
1444   deps->libcall_block_tail_insn = 0;
1445 }
1446
1447 /* Free insn lists found in DEPS.  */
1448
1449 void
1450 free_deps (struct deps *deps)
1451 {
1452   int i;
1453
1454   free_INSN_LIST_list (&deps->pending_read_insns);
1455   free_EXPR_LIST_list (&deps->pending_read_mems);
1456   free_INSN_LIST_list (&deps->pending_write_insns);
1457   free_EXPR_LIST_list (&deps->pending_write_mems);
1458   free_INSN_LIST_list (&deps->last_pending_memory_flush);
1459
1460   /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
1461      times.  For a testcase with 42000 regs and 8000 small basic blocks,
1462      this loop accounted for nearly 60% (84 sec) of the total -O2 runtime.  */
1463   EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
1464     {
1465       struct deps_reg *reg_last = &deps->reg_last[i];
1466       if (reg_last->uses)
1467         free_INSN_LIST_list (&reg_last->uses);
1468       if (reg_last->sets)
1469         free_INSN_LIST_list (&reg_last->sets);
1470       if (reg_last->clobbers)
1471         free_INSN_LIST_list (&reg_last->clobbers);
1472     });
1473   CLEAR_REG_SET (&deps->reg_last_in_use);
1474   CLEAR_REG_SET (&deps->reg_conditional_sets);
1475
1476   free (deps->reg_last);
1477 }
1478
1479 /* If it is profitable to use them, initialize caches for tracking
1480    dependency information.  LUID is the number of insns to be scheduled,
1481    it is used in the estimate of profitability.  */
1482
1483 void
1484 init_dependency_caches (int luid)
1485 {
1486   /* ?!? We could save some memory by computing a per-region luid mapping
1487      which could reduce both the number of vectors in the cache and the size
1488      of each vector.  Instead we just avoid the cache entirely unless the
1489      average number of instructions in a basic block is very high.  See
1490      the comment before the declaration of true_dependency_cache for
1491      what we consider "very high".  */
1492   if (luid / n_basic_blocks > 100 * 5)
1493     {
1494       int i;
1495       true_dependency_cache = xmalloc (luid * sizeof (bitmap_head));
1496       anti_dependency_cache = xmalloc (luid * sizeof (bitmap_head));
1497       output_dependency_cache = xmalloc (luid * sizeof (bitmap_head));
1498 #ifdef ENABLE_CHECKING
1499       forward_dependency_cache = xmalloc (luid * sizeof (bitmap_head));
1500 #endif
1501       for (i = 0; i < luid; i++)
1502         {
1503           bitmap_initialize (&true_dependency_cache[i], 0);
1504           bitmap_initialize (&anti_dependency_cache[i], 0);
1505           bitmap_initialize (&output_dependency_cache[i], 0);
1506 #ifdef ENABLE_CHECKING
1507           bitmap_initialize (&forward_dependency_cache[i], 0);
1508 #endif
1509         }
1510       cache_size = luid;
1511     }
1512 }
1513
1514 /* Free the caches allocated in init_dependency_caches.  */
1515
1516 void
1517 free_dependency_caches (void)
1518 {
1519   if (true_dependency_cache)
1520     {
1521       int i;
1522
1523       for (i = 0; i < cache_size; i++)
1524         {
1525           bitmap_clear (&true_dependency_cache[i]);
1526           bitmap_clear (&anti_dependency_cache[i]);
1527           bitmap_clear (&output_dependency_cache[i]);
1528 #ifdef ENABLE_CHECKING
1529           bitmap_clear (&forward_dependency_cache[i]);
1530 #endif
1531         }
1532       free (true_dependency_cache);
1533       true_dependency_cache = NULL;
1534       free (anti_dependency_cache);
1535       anti_dependency_cache = NULL;
1536       free (output_dependency_cache);
1537       output_dependency_cache = NULL;
1538 #ifdef ENABLE_CHECKING
1539       free (forward_dependency_cache);
1540       forward_dependency_cache = NULL;
1541 #endif
1542     }
1543 }
1544
1545 /* Initialize some global variables needed by the dependency analysis
1546    code.  */
1547
1548 void
1549 init_deps_global (void)
1550 {
1551   reg_pending_sets = INITIALIZE_REG_SET (reg_pending_sets_head);
1552   reg_pending_clobbers = INITIALIZE_REG_SET (reg_pending_clobbers_head);
1553   reg_pending_uses = INITIALIZE_REG_SET (reg_pending_uses_head);
1554   reg_pending_barrier = NOT_A_BARRIER;
1555 }
1556
1557 /* Free everything used by the dependency analysis code.  */
1558
1559 void
1560 finish_deps_global (void)
1561 {
1562   FREE_REG_SET (reg_pending_sets);
1563   FREE_REG_SET (reg_pending_clobbers);
1564   FREE_REG_SET (reg_pending_uses);
1565 }