OSDN Git Service

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