OSDN Git Service

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