OSDN Git Service

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