* config/xtensa/crti.asm (_init, _fini): Increase frame size to 64.
[official-gcc.git] / gcc / sched-deps.c
blob1a41e9989c905947f88ad2719e6a75cdd8194983
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 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
6 and currently maintained by, Jim Wilson (wilson@cygnus.com)
8 This file is part of GCC.
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.
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.
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. */
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"
47 extern char *reg_known_equiv_p;
48 extern rtx *reg_known_value;
50 static regset_head reg_pending_sets_head;
51 static regset_head reg_pending_clobbers_head;
52 static regset_head reg_pending_uses_head;
54 static regset reg_pending_sets;
55 static regset reg_pending_clobbers;
56 static regset reg_pending_uses;
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
63 NOT_A_BARRIER = 0,
64 MOVE_BARRIER,
65 TRUE_BARRIER
68 static enum reg_pending_barrier_mode reg_pending_barrier;
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.
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.
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 sbitmap *true_dependency_cache;
84 static sbitmap *anti_dependency_cache;
85 static sbitmap *output_dependency_cache;
87 /* To speed up checking consistency of formed forward insn
88 dependencies we use the following cache. Another possible solution
89 could be switching off checking duplication of insns in forward
90 dependencies. */
91 #ifdef ENABLE_CHECKING
92 static sbitmap *forward_dependency_cache;
93 #endif
95 static int deps_may_trap_p (rtx);
96 static void add_dependence_list (rtx, rtx, enum reg_note);
97 static void add_dependence_list_and_free (rtx, rtx *, enum reg_note);
98 static void set_sched_group_p (rtx);
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);
105 static rtx get_condition (rtx);
106 static int conditions_mutex_p (rtx, rtx);
108 /* Return nonzero if a load of the memory reference MEM can cause a trap. */
110 static int
111 deps_may_trap_p (rtx mem)
113 rtx addr = XEXP (mem, 0);
115 if (REG_P (addr)
116 && REGNO (addr) >= FIRST_PSEUDO_REGISTER
117 && reg_known_value[REGNO (addr)])
118 addr = reg_known_value[REGNO (addr)];
119 return rtx_addr_can_trap_p (addr);
122 /* Return the INSN_LIST containing INSN in LIST, or NULL
123 if LIST does not contain INSN. */
126 find_insn_list (rtx insn, rtx list)
128 while (list)
130 if (XEXP (list, 0) == insn)
131 return list;
132 list = XEXP (list, 1);
134 return 0;
137 /* Find the condition under which INSN is executed. */
139 static rtx
140 get_condition (rtx insn)
142 rtx pat = PATTERN (insn);
143 rtx cond;
145 if (pat == 0)
146 return 0;
147 if (GET_CODE (pat) == COND_EXEC)
148 return COND_EXEC_TEST (pat);
149 if (GET_CODE (insn) != JUMP_INSN)
150 return 0;
151 if (GET_CODE (pat) != SET || SET_SRC (pat) != pc_rtx)
152 return 0;
153 if (GET_CODE (SET_DEST (pat)) != IF_THEN_ELSE)
154 return 0;
155 pat = SET_DEST (pat);
156 cond = XEXP (pat, 0);
157 if (GET_CODE (XEXP (cond, 1)) == LABEL_REF
158 && XEXP (cond, 2) == pc_rtx)
159 return cond;
160 else if (GET_CODE (XEXP (cond, 2)) == LABEL_REF
161 && XEXP (cond, 1) == pc_rtx)
162 return gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)), GET_MODE (cond),
163 XEXP (cond, 0), XEXP (cond, 1));
164 else
165 return 0;
168 /* Return nonzero if conditions COND1 and COND2 can never be both true. */
170 static int
171 conditions_mutex_p (rtx cond1, rtx cond2)
173 if (GET_RTX_CLASS (GET_CODE (cond1)) == '<'
174 && GET_RTX_CLASS (GET_CODE (cond2)) == '<'
175 && GET_CODE (cond1) == reverse_condition (GET_CODE (cond2))
176 && XEXP (cond1, 0) == XEXP (cond2, 0)
177 && XEXP (cond1, 1) == XEXP (cond2, 1))
178 return 1;
179 return 0;
182 /* Add ELEM wrapped in an INSN_LIST with reg note kind DEP_TYPE to the
183 LOG_LINKS of INSN, if not already there. DEP_TYPE indicates the
184 type of dependence that this link represents. The function returns
185 nonzero if a new entry has been added to insn's LOG_LINK. */
188 add_dependence (rtx insn, rtx elem, enum reg_note dep_type)
190 rtx link;
191 int present_p;
192 rtx cond1, cond2;
194 /* Don't depend an insn on itself. */
195 if (insn == elem)
196 return 0;
198 /* We can get a dependency on deleted insns due to optimizations in
199 the register allocation and reloading or due to splitting. Any
200 such dependency is useless and can be ignored. */
201 if (GET_CODE (elem) == NOTE)
202 return 0;
204 /* flow.c doesn't handle conditional lifetimes entirely correctly;
205 calls mess up the conditional lifetimes. */
206 /* ??? add_dependence is the wrong place to be eliding dependencies,
207 as that forgets that the condition expressions themselves may
208 be dependent. */
209 if (GET_CODE (insn) != CALL_INSN && GET_CODE (elem) != CALL_INSN)
211 cond1 = get_condition (insn);
212 cond2 = get_condition (elem);
213 if (cond1 && cond2
214 && conditions_mutex_p (cond1, cond2)
215 /* Make sure first instruction doesn't affect condition of second
216 instruction if switched. */
217 && !modified_in_p (cond1, elem)
218 /* Make sure second instruction doesn't affect condition of first
219 instruction if switched. */
220 && !modified_in_p (cond2, insn))
221 return 0;
224 present_p = 1;
225 #ifdef INSN_SCHEDULING
226 /* ??? No good way to tell from here whether we're doing interblock
227 scheduling. Possibly add another callback. */
228 #if 0
229 /* (This code is guarded by INSN_SCHEDULING, otherwise INSN_BB is undefined.)
230 No need for interblock dependences with calls, since
231 calls are not moved between blocks. Note: the edge where
232 elem is a CALL is still required. */
233 if (GET_CODE (insn) == CALL_INSN
234 && (INSN_BB (elem) != INSN_BB (insn)))
235 return 0;
236 #endif
238 /* If we already have a dependency for ELEM, then we do not need to
239 do anything. Avoiding the list walk below can cut compile times
240 dramatically for some code. */
241 if (true_dependency_cache != NULL)
243 enum reg_note present_dep_type = 0;
245 if (anti_dependency_cache == NULL || output_dependency_cache == NULL)
246 abort ();
247 if (TEST_BIT (true_dependency_cache[INSN_LUID (insn)], INSN_LUID (elem)))
248 /* Do nothing (present_set_type is already 0). */
250 else if (TEST_BIT (anti_dependency_cache[INSN_LUID (insn)],
251 INSN_LUID (elem)))
252 present_dep_type = REG_DEP_ANTI;
253 else if (TEST_BIT (output_dependency_cache[INSN_LUID (insn)],
254 INSN_LUID (elem)))
255 present_dep_type = REG_DEP_OUTPUT;
256 else
257 present_p = 0;
258 if (present_p && (int) dep_type >= (int) present_dep_type)
259 return 0;
261 #endif
263 /* Check that we don't already have this dependence. */
264 if (present_p)
265 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
266 if (XEXP (link, 0) == elem)
268 #ifdef INSN_SCHEDULING
269 /* Clear corresponding cache entry because type of the link
270 may be changed. */
271 if (true_dependency_cache != NULL)
273 if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
274 RESET_BIT (anti_dependency_cache[INSN_LUID (insn)],
275 INSN_LUID (elem));
276 else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
277 && output_dependency_cache)
278 RESET_BIT (output_dependency_cache[INSN_LUID (insn)],
279 INSN_LUID (elem));
280 else
281 abort ();
283 #endif
285 /* If this is a more restrictive type of dependence than the existing
286 one, then change the existing dependence to this type. */
287 if ((int) dep_type < (int) REG_NOTE_KIND (link))
288 PUT_REG_NOTE_KIND (link, dep_type);
290 #ifdef INSN_SCHEDULING
291 /* If we are adding a dependency to INSN's LOG_LINKs, then
292 note that in the bitmap caches of dependency information. */
293 if (true_dependency_cache != NULL)
295 if ((int) REG_NOTE_KIND (link) == 0)
296 SET_BIT (true_dependency_cache[INSN_LUID (insn)],
297 INSN_LUID (elem));
298 else if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
299 SET_BIT (anti_dependency_cache[INSN_LUID (insn)],
300 INSN_LUID (elem));
301 else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
302 SET_BIT (output_dependency_cache[INSN_LUID (insn)],
303 INSN_LUID (elem));
305 #endif
306 return 0;
308 /* Might want to check one level of transitivity to save conses. */
310 link = alloc_INSN_LIST (elem, LOG_LINKS (insn));
311 LOG_LINKS (insn) = link;
313 /* Insn dependency, not data dependency. */
314 PUT_REG_NOTE_KIND (link, dep_type);
316 #ifdef INSN_SCHEDULING
317 /* If we are adding a dependency to INSN's LOG_LINKs, then note that
318 in the bitmap caches of dependency information. */
319 if (true_dependency_cache != NULL)
321 if ((int) dep_type == 0)
322 SET_BIT (true_dependency_cache[INSN_LUID (insn)], INSN_LUID (elem));
323 else if (dep_type == REG_DEP_ANTI)
324 SET_BIT (anti_dependency_cache[INSN_LUID (insn)], INSN_LUID (elem));
325 else if (dep_type == REG_DEP_OUTPUT)
326 SET_BIT (output_dependency_cache[INSN_LUID (insn)], INSN_LUID (elem));
328 #endif
329 return 1;
332 /* A convenience wrapper to operate on an entire list. */
334 static void
335 add_dependence_list (rtx insn, rtx list, enum reg_note dep_type)
337 for (; list; list = XEXP (list, 1))
338 add_dependence (insn, XEXP (list, 0), dep_type);
341 /* Similar, but free *LISTP at the same time. */
343 static void
344 add_dependence_list_and_free (rtx insn, rtx *listp, enum reg_note dep_type)
346 rtx list, next;
347 for (list = *listp, *listp = NULL; list ; list = next)
349 next = XEXP (list, 1);
350 add_dependence (insn, XEXP (list, 0), dep_type);
351 free_INSN_LIST_node (list);
355 /* Set SCHED_GROUP_P and care for the rest of the bookkeeping that
356 goes along with that. */
358 static void
359 set_sched_group_p (rtx insn)
361 rtx prev;
363 SCHED_GROUP_P (insn) = 1;
365 prev = prev_nonnote_insn (insn);
366 add_dependence (insn, prev, REG_DEP_ANTI);
369 /* Process an insn's memory dependencies. There are four kinds of
370 dependencies:
372 (0) read dependence: read follows read
373 (1) true dependence: read follows write
374 (2) anti dependence: write follows read
375 (3) output dependence: write follows write
377 We are careful to build only dependencies which actually exist, and
378 use transitivity to avoid building too many links. */
380 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
381 The MEM is a memory reference contained within INSN, which we are saving
382 so that we can do memory aliasing on it. */
384 void
385 add_insn_mem_dependence (struct deps *deps, rtx *insn_list, rtx *mem_list,
386 rtx insn, rtx mem)
388 rtx link;
390 link = alloc_INSN_LIST (insn, *insn_list);
391 *insn_list = link;
393 if (current_sched_info->use_cselib)
395 mem = shallow_copy_rtx (mem);
396 XEXP (mem, 0) = cselib_subst_to_values (XEXP (mem, 0));
398 link = alloc_EXPR_LIST (VOIDmode, mem, *mem_list);
399 *mem_list = link;
401 deps->pending_lists_length++;
404 /* Make a dependency between every memory reference on the pending lists
405 and INSN, thus flushing the pending lists. FOR_READ is true if emitting
406 dependencies for a read operation, similarly with FOR_WRITE. */
408 static void
409 flush_pending_lists (struct deps *deps, rtx insn, int for_read,
410 int for_write)
412 if (for_write)
414 add_dependence_list_and_free (insn, &deps->pending_read_insns,
415 REG_DEP_ANTI);
416 free_EXPR_LIST_list (&deps->pending_read_mems);
419 add_dependence_list_and_free (insn, &deps->pending_write_insns,
420 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
421 free_EXPR_LIST_list (&deps->pending_write_mems);
422 deps->pending_lists_length = 0;
424 add_dependence_list_and_free (insn, &deps->last_pending_memory_flush,
425 for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT);
426 deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
427 deps->pending_flush_length = 1;
430 /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
431 rtx, X, creating all dependencies generated by the write to the
432 destination of X, and reads of everything mentioned. */
434 static void
435 sched_analyze_1 (struct deps *deps, rtx x, rtx insn)
437 int regno;
438 rtx dest = XEXP (x, 0);
439 enum rtx_code code = GET_CODE (x);
441 if (dest == 0)
442 return;
444 if (GET_CODE (dest) == PARALLEL)
446 int i;
448 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
449 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
450 sched_analyze_1 (deps,
451 gen_rtx_CLOBBER (VOIDmode,
452 XEXP (XVECEXP (dest, 0, i), 0)),
453 insn);
455 if (GET_CODE (x) == SET)
456 sched_analyze_2 (deps, SET_SRC (x), insn);
457 return;
460 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
461 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
463 if (GET_CODE (dest) == STRICT_LOW_PART
464 || GET_CODE (dest) == ZERO_EXTRACT
465 || GET_CODE (dest) == SIGN_EXTRACT
466 || read_modify_subreg_p (dest))
468 /* These both read and modify the result. We must handle
469 them as writes to get proper dependencies for following
470 instructions. We must handle them as reads to get proper
471 dependencies from this to previous instructions.
472 Thus we need to call sched_analyze_2. */
474 sched_analyze_2 (deps, XEXP (dest, 0), insn);
476 if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
478 /* The second and third arguments are values read by this insn. */
479 sched_analyze_2 (deps, XEXP (dest, 1), insn);
480 sched_analyze_2 (deps, XEXP (dest, 2), insn);
482 dest = XEXP (dest, 0);
485 if (GET_CODE (dest) == REG)
487 regno = REGNO (dest);
489 /* A hard reg in a wide mode may really be multiple registers.
490 If so, mark all of them just like the first. */
491 if (regno < FIRST_PSEUDO_REGISTER)
493 int i = HARD_REGNO_NREGS (regno, GET_MODE (dest));
494 if (code == SET)
496 while (--i >= 0)
497 SET_REGNO_REG_SET (reg_pending_sets, regno + i);
499 else
501 while (--i >= 0)
502 SET_REGNO_REG_SET (reg_pending_clobbers, regno + i);
505 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
506 it does not reload. Ignore these as they have served their
507 purpose already. */
508 else if (regno >= deps->max_reg)
510 if (GET_CODE (PATTERN (insn)) != USE
511 && GET_CODE (PATTERN (insn)) != CLOBBER)
512 abort ();
514 else
516 if (code == SET)
517 SET_REGNO_REG_SET (reg_pending_sets, regno);
518 else
519 SET_REGNO_REG_SET (reg_pending_clobbers, regno);
521 /* Pseudos that are REG_EQUIV to something may be replaced
522 by that during reloading. We need only add dependencies for
523 the address in the REG_EQUIV note. */
524 if (!reload_completed
525 && reg_known_equiv_p[regno]
526 && GET_CODE (reg_known_value[regno]) == MEM)
527 sched_analyze_2 (deps, XEXP (reg_known_value[regno], 0), insn);
529 /* Don't let it cross a call after scheduling if it doesn't
530 already cross one. */
531 if (REG_N_CALLS_CROSSED (regno) == 0)
532 add_dependence_list (insn, deps->last_function_call, REG_DEP_ANTI);
535 else if (GET_CODE (dest) == MEM)
537 /* Writing memory. */
538 rtx t = dest;
540 if (current_sched_info->use_cselib)
542 t = shallow_copy_rtx (dest);
543 cselib_lookup (XEXP (t, 0), Pmode, 1);
544 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
547 if (deps->pending_lists_length > MAX_PENDING_LIST_LENGTH)
549 /* Flush all pending reads and writes to prevent the pending lists
550 from getting any larger. Insn scheduling runs too slowly when
551 these lists get long. When compiling GCC with itself,
552 this flush occurs 8 times for sparc, and 10 times for m88k using
553 the default value of 32. */
554 flush_pending_lists (deps, insn, false, true);
556 else
558 rtx pending, pending_mem;
560 pending = deps->pending_read_insns;
561 pending_mem = deps->pending_read_mems;
562 while (pending)
564 if (anti_dependence (XEXP (pending_mem, 0), t))
565 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
567 pending = XEXP (pending, 1);
568 pending_mem = XEXP (pending_mem, 1);
571 pending = deps->pending_write_insns;
572 pending_mem = deps->pending_write_mems;
573 while (pending)
575 if (output_dependence (XEXP (pending_mem, 0), t))
576 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
578 pending = XEXP (pending, 1);
579 pending_mem = XEXP (pending_mem, 1);
582 add_dependence_list (insn, deps->last_pending_memory_flush,
583 REG_DEP_ANTI);
585 add_insn_mem_dependence (deps, &deps->pending_write_insns,
586 &deps->pending_write_mems, insn, dest);
588 sched_analyze_2 (deps, XEXP (dest, 0), insn);
591 /* Analyze reads. */
592 if (GET_CODE (x) == SET)
593 sched_analyze_2 (deps, SET_SRC (x), insn);
596 /* Analyze the uses of memory and registers in rtx X in INSN. */
598 static void
599 sched_analyze_2 (struct deps *deps, rtx x, rtx insn)
601 int i;
602 int j;
603 enum rtx_code code;
604 const char *fmt;
606 if (x == 0)
607 return;
609 code = GET_CODE (x);
611 switch (code)
613 case CONST_INT:
614 case CONST_DOUBLE:
615 case CONST_VECTOR:
616 case SYMBOL_REF:
617 case CONST:
618 case LABEL_REF:
619 /* Ignore constants. Note that we must handle CONST_DOUBLE here
620 because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
621 this does not mean that this insn is using cc0. */
622 return;
624 #ifdef HAVE_cc0
625 case CC0:
626 /* User of CC0 depends on immediately preceding insn. */
627 set_sched_group_p (insn);
628 return;
629 #endif
631 case REG:
633 int regno = REGNO (x);
634 if (regno < FIRST_PSEUDO_REGISTER)
636 int i = HARD_REGNO_NREGS (regno, GET_MODE (x));
637 while (--i >= 0)
638 SET_REGNO_REG_SET (reg_pending_uses, regno + i);
640 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
641 it does not reload. Ignore these as they have served their
642 purpose already. */
643 else if (regno >= deps->max_reg)
645 if (GET_CODE (PATTERN (insn)) != USE
646 && GET_CODE (PATTERN (insn)) != CLOBBER)
647 abort ();
649 else
651 SET_REGNO_REG_SET (reg_pending_uses, regno);
653 /* Pseudos that are REG_EQUIV to something may be replaced
654 by that during reloading. We need only add dependencies for
655 the address in the REG_EQUIV note. */
656 if (!reload_completed
657 && reg_known_equiv_p[regno]
658 && GET_CODE (reg_known_value[regno]) == MEM)
659 sched_analyze_2 (deps, XEXP (reg_known_value[regno], 0), insn);
661 /* If the register does not already cross any calls, then add this
662 insn to the sched_before_next_call list so that it will still
663 not cross calls after scheduling. */
664 if (REG_N_CALLS_CROSSED (regno) == 0)
665 deps->sched_before_next_call
666 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
668 return;
671 case MEM:
673 /* Reading memory. */
674 rtx u;
675 rtx pending, pending_mem;
676 rtx t = x;
678 if (current_sched_info->use_cselib)
680 t = shallow_copy_rtx (t);
681 cselib_lookup (XEXP (t, 0), Pmode, 1);
682 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
684 pending = deps->pending_read_insns;
685 pending_mem = deps->pending_read_mems;
686 while (pending)
688 if (read_dependence (XEXP (pending_mem, 0), t))
689 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
691 pending = XEXP (pending, 1);
692 pending_mem = XEXP (pending_mem, 1);
695 pending = deps->pending_write_insns;
696 pending_mem = deps->pending_write_mems;
697 while (pending)
699 if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
700 t, rtx_varies_p))
701 add_dependence (insn, XEXP (pending, 0), 0);
703 pending = XEXP (pending, 1);
704 pending_mem = XEXP (pending_mem, 1);
707 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
708 if (GET_CODE (XEXP (u, 0)) != JUMP_INSN
709 || deps_may_trap_p (x))
710 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
712 /* Always add these dependencies to pending_reads, since
713 this insn may be followed by a write. */
714 add_insn_mem_dependence (deps, &deps->pending_read_insns,
715 &deps->pending_read_mems, insn, x);
717 /* Take advantage of tail recursion here. */
718 sched_analyze_2 (deps, XEXP (x, 0), insn);
719 return;
722 /* Force pending stores to memory in case a trap handler needs them. */
723 case TRAP_IF:
724 flush_pending_lists (deps, insn, true, false);
725 break;
727 case ASM_OPERANDS:
728 case ASM_INPUT:
729 case UNSPEC_VOLATILE:
731 /* Traditional and volatile asm instructions must be considered to use
732 and clobber all hard registers, all pseudo-registers and all of
733 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
735 Consider for instance a volatile asm that changes the fpu rounding
736 mode. An insn should not be moved across this even if it only uses
737 pseudo-regs because it might give an incorrectly rounded result. */
738 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
739 reg_pending_barrier = TRUE_BARRIER;
741 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
742 We can not just fall through here since then we would be confused
743 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
744 traditional asms unlike their normal usage. */
746 if (code == ASM_OPERANDS)
748 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
749 sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
750 return;
752 break;
755 case PRE_DEC:
756 case POST_DEC:
757 case PRE_INC:
758 case POST_INC:
759 /* These both read and modify the result. We must handle them as writes
760 to get proper dependencies for following instructions. We must handle
761 them as reads to get proper dependencies from this to previous
762 instructions. Thus we need to pass them to both sched_analyze_1
763 and sched_analyze_2. We must call sched_analyze_2 first in order
764 to get the proper antecedent for the read. */
765 sched_analyze_2 (deps, XEXP (x, 0), insn);
766 sched_analyze_1 (deps, x, insn);
767 return;
769 case POST_MODIFY:
770 case PRE_MODIFY:
771 /* op0 = op0 + op1 */
772 sched_analyze_2 (deps, XEXP (x, 0), insn);
773 sched_analyze_2 (deps, XEXP (x, 1), insn);
774 sched_analyze_1 (deps, x, insn);
775 return;
777 default:
778 break;
781 /* Other cases: walk the insn. */
782 fmt = GET_RTX_FORMAT (code);
783 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
785 if (fmt[i] == 'e')
786 sched_analyze_2 (deps, XEXP (x, i), insn);
787 else if (fmt[i] == 'E')
788 for (j = 0; j < XVECLEN (x, i); j++)
789 sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
793 /* Analyze an INSN with pattern X to find all dependencies. */
795 static void
796 sched_analyze_insn (struct deps *deps, rtx x, rtx insn, rtx loop_notes)
798 RTX_CODE code = GET_CODE (x);
799 rtx link;
800 int i;
802 if (code == COND_EXEC)
804 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
806 /* ??? Should be recording conditions so we reduce the number of
807 false dependencies. */
808 x = COND_EXEC_CODE (x);
809 code = GET_CODE (x);
811 if (code == SET || code == CLOBBER)
813 sched_analyze_1 (deps, x, insn);
815 /* Bare clobber insns are used for letting life analysis, reg-stack
816 and others know that a value is dead. Depend on the last call
817 instruction so that reg-stack won't get confused. */
818 if (code == CLOBBER)
819 add_dependence_list (insn, deps->last_function_call, REG_DEP_OUTPUT);
821 else if (code == PARALLEL)
823 int i;
824 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
826 rtx sub = XVECEXP (x, 0, i);
827 code = GET_CODE (sub);
829 if (code == COND_EXEC)
831 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
832 sub = COND_EXEC_CODE (sub);
833 code = GET_CODE (sub);
835 if (code == SET || code == CLOBBER)
836 sched_analyze_1 (deps, sub, insn);
837 else
838 sched_analyze_2 (deps, sub, insn);
841 else
842 sched_analyze_2 (deps, x, insn);
844 /* Mark registers CLOBBERED or used by called function. */
845 if (GET_CODE (insn) == CALL_INSN)
847 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
849 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
850 sched_analyze_1 (deps, XEXP (link, 0), insn);
851 else
852 sched_analyze_2 (deps, XEXP (link, 0), insn);
854 if (find_reg_note (insn, REG_SETJMP, NULL))
855 reg_pending_barrier = MOVE_BARRIER;
858 if (GET_CODE (insn) == JUMP_INSN)
860 rtx next;
861 next = next_nonnote_insn (insn);
862 if (next && GET_CODE (next) == BARRIER)
863 reg_pending_barrier = TRUE_BARRIER;
864 else
866 rtx pending, pending_mem;
867 regset_head tmp_uses, tmp_sets;
868 INIT_REG_SET (&tmp_uses);
869 INIT_REG_SET (&tmp_sets);
871 (*current_sched_info->compute_jump_reg_dependencies)
872 (insn, &deps->reg_conditional_sets, &tmp_uses, &tmp_sets);
873 /* Make latency of jump equal to 0 by using anti-dependence. */
874 EXECUTE_IF_SET_IN_REG_SET (&tmp_uses, 0, i,
876 struct deps_reg *reg_last = &deps->reg_last[i];
877 add_dependence_list (insn, reg_last->sets, REG_DEP_ANTI);
878 add_dependence_list (insn, reg_last->clobbers, REG_DEP_ANTI);
879 reg_last->uses_length++;
880 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
882 IOR_REG_SET (reg_pending_sets, &tmp_sets);
884 CLEAR_REG_SET (&tmp_uses);
885 CLEAR_REG_SET (&tmp_sets);
887 /* All memory writes and volatile reads must happen before the
888 jump. Non-volatile reads must happen before the jump iff
889 the result is needed by the above register used mask. */
891 pending = deps->pending_write_insns;
892 pending_mem = deps->pending_write_mems;
893 while (pending)
895 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
896 pending = XEXP (pending, 1);
897 pending_mem = XEXP (pending_mem, 1);
900 pending = deps->pending_read_insns;
901 pending_mem = deps->pending_read_mems;
902 while (pending)
904 if (MEM_VOLATILE_P (XEXP (pending_mem, 0)))
905 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
906 pending = XEXP (pending, 1);
907 pending_mem = XEXP (pending_mem, 1);
910 add_dependence_list (insn, deps->last_pending_memory_flush,
911 REG_DEP_ANTI);
915 /* If there is a {LOOP,EHREGION}_{BEG,END} note in the middle of a basic
916 block, then we must be sure that no instructions are scheduled across it.
917 Otherwise, the reg_n_refs info (which depends on loop_depth) would
918 become incorrect. */
919 if (loop_notes)
921 rtx link;
923 /* Update loop_notes with any notes from this insn. Also determine
924 if any of the notes on the list correspond to instruction scheduling
925 barriers (loop, eh & setjmp notes, but not range notes). */
926 link = loop_notes;
927 while (XEXP (link, 1))
929 if (INTVAL (XEXP (link, 0)) == NOTE_INSN_LOOP_BEG
930 || INTVAL (XEXP (link, 0)) == NOTE_INSN_LOOP_END
931 || INTVAL (XEXP (link, 0)) == NOTE_INSN_EH_REGION_BEG
932 || INTVAL (XEXP (link, 0)) == NOTE_INSN_EH_REGION_END)
933 reg_pending_barrier = MOVE_BARRIER;
935 link = XEXP (link, 1);
937 XEXP (link, 1) = REG_NOTES (insn);
938 REG_NOTES (insn) = loop_notes;
941 /* If this instruction can throw an exception, then moving it changes
942 where block boundaries fall. This is mighty confusing elsewhere.
943 Therefore, prevent such an instruction from being moved. */
944 if (can_throw_internal (insn))
945 reg_pending_barrier = MOVE_BARRIER;
947 /* Add dependencies if a scheduling barrier was found. */
948 if (reg_pending_barrier)
950 /* In the case of barrier the most added dependencies are not
951 real, so we use anti-dependence here. */
952 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
954 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
956 struct deps_reg *reg_last = &deps->reg_last[i];
957 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
958 add_dependence_list
959 (insn, reg_last->sets,
960 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
961 add_dependence_list
962 (insn, reg_last->clobbers,
963 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
966 else
968 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
970 struct deps_reg *reg_last = &deps->reg_last[i];
971 add_dependence_list_and_free (insn, &reg_last->uses,
972 REG_DEP_ANTI);
973 add_dependence_list_and_free
974 (insn, &reg_last->sets,
975 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
976 add_dependence_list_and_free
977 (insn, &reg_last->clobbers,
978 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
979 reg_last->uses_length = 0;
980 reg_last->clobbers_length = 0;
984 for (i = 0; i < deps->max_reg; i++)
986 struct deps_reg *reg_last = &deps->reg_last[i];
987 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
988 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
991 flush_pending_lists (deps, insn, true, true);
992 CLEAR_REG_SET (&deps->reg_conditional_sets);
993 reg_pending_barrier = NOT_A_BARRIER;
995 else
997 /* If the current insn is conditional, we can't free any
998 of the lists. */
999 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1001 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i,
1003 struct deps_reg *reg_last = &deps->reg_last[i];
1004 add_dependence_list (insn, reg_last->sets, 0);
1005 add_dependence_list (insn, reg_last->clobbers, 0);
1006 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1007 reg_last->uses_length++;
1009 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i,
1011 struct deps_reg *reg_last = &deps->reg_last[i];
1012 add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1013 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1014 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
1015 reg_last->clobbers_length++;
1017 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
1019 struct deps_reg *reg_last = &deps->reg_last[i];
1020 add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1021 add_dependence_list (insn, reg_last->clobbers, REG_DEP_OUTPUT);
1022 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1023 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1024 SET_REGNO_REG_SET (&deps->reg_conditional_sets, i);
1027 else
1029 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i,
1031 struct deps_reg *reg_last = &deps->reg_last[i];
1032 add_dependence_list (insn, reg_last->sets, 0);
1033 add_dependence_list (insn, reg_last->clobbers, 0);
1034 reg_last->uses_length++;
1035 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1037 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i,
1039 struct deps_reg *reg_last = &deps->reg_last[i];
1040 if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
1041 || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
1043 add_dependence_list_and_free (insn, &reg_last->sets,
1044 REG_DEP_OUTPUT);
1045 add_dependence_list_and_free (insn, &reg_last->uses,
1046 REG_DEP_ANTI);
1047 add_dependence_list_and_free (insn, &reg_last->clobbers,
1048 REG_DEP_OUTPUT);
1049 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1050 reg_last->clobbers_length = 0;
1051 reg_last->uses_length = 0;
1053 else
1055 add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1056 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1058 reg_last->clobbers_length++;
1059 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
1061 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
1063 struct deps_reg *reg_last = &deps->reg_last[i];
1064 add_dependence_list_and_free (insn, &reg_last->sets,
1065 REG_DEP_OUTPUT);
1066 add_dependence_list_and_free (insn, &reg_last->clobbers,
1067 REG_DEP_OUTPUT);
1068 add_dependence_list_and_free (insn, &reg_last->uses,
1069 REG_DEP_ANTI);
1070 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1071 reg_last->uses_length = 0;
1072 reg_last->clobbers_length = 0;
1073 CLEAR_REGNO_REG_SET (&deps->reg_conditional_sets, i);
1077 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
1078 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
1079 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
1081 CLEAR_REG_SET (reg_pending_uses);
1082 CLEAR_REG_SET (reg_pending_clobbers);
1083 CLEAR_REG_SET (reg_pending_sets);
1085 /* If we are currently in a libcall scheduling group, then mark the
1086 current insn as being in a scheduling group and that it can not
1087 be moved into a different basic block. */
1089 if (deps->libcall_block_tail_insn)
1091 set_sched_group_p (insn);
1092 CANT_MOVE (insn) = 1;
1095 /* If a post-call group is still open, see if it should remain so.
1096 This insn must be a simple move of a hard reg to a pseudo or
1097 vice-versa.
1099 We must avoid moving these insns for correctness on
1100 SMALL_REGISTER_CLASS machines, and for special registers like
1101 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
1102 hard regs for all targets. */
1104 if (deps->in_post_call_group_p)
1106 rtx tmp, set = single_set (insn);
1107 int src_regno, dest_regno;
1109 if (set == NULL)
1110 goto end_call_group;
1112 tmp = SET_DEST (set);
1113 if (GET_CODE (tmp) == SUBREG)
1114 tmp = SUBREG_REG (tmp);
1115 if (GET_CODE (tmp) == REG)
1116 dest_regno = REGNO (tmp);
1117 else
1118 goto end_call_group;
1120 tmp = SET_SRC (set);
1121 if (GET_CODE (tmp) == SUBREG)
1122 tmp = SUBREG_REG (tmp);
1123 if (GET_CODE (tmp) == REG)
1124 src_regno = REGNO (tmp);
1125 else
1126 goto end_call_group;
1128 if (src_regno < FIRST_PSEUDO_REGISTER
1129 || dest_regno < FIRST_PSEUDO_REGISTER)
1131 set_sched_group_p (insn);
1132 CANT_MOVE (insn) = 1;
1134 else
1136 end_call_group:
1137 deps->in_post_call_group_p = false;
1142 /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
1143 for every dependency. */
1145 void
1146 sched_analyze (struct deps *deps, rtx head, rtx tail)
1148 rtx insn;
1149 rtx loop_notes = 0;
1151 if (current_sched_info->use_cselib)
1152 cselib_init ();
1154 for (insn = head;; insn = NEXT_INSN (insn))
1156 rtx link, end_seq, r0, set;
1158 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
1160 /* Clear out the stale LOG_LINKS from flow. */
1161 free_INSN_LIST_list (&LOG_LINKS (insn));
1163 /* Make each JUMP_INSN a scheduling barrier for memory
1164 references. */
1165 if (GET_CODE (insn) == JUMP_INSN)
1167 /* Keep the list a reasonable size. */
1168 if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
1169 flush_pending_lists (deps, insn, true, true);
1170 else
1171 deps->last_pending_memory_flush
1172 = alloc_INSN_LIST (insn, deps->last_pending_memory_flush);
1174 sched_analyze_insn (deps, PATTERN (insn), insn, loop_notes);
1175 loop_notes = 0;
1177 else if (GET_CODE (insn) == CALL_INSN)
1179 int i;
1181 CANT_MOVE (insn) = 1;
1183 /* Clear out the stale LOG_LINKS from flow. */
1184 free_INSN_LIST_list (&LOG_LINKS (insn));
1186 if (find_reg_note (insn, REG_SETJMP, NULL))
1188 /* This is setjmp. Assume that all registers, not just
1189 hard registers, may be clobbered by this call. */
1190 reg_pending_barrier = MOVE_BARRIER;
1192 else
1194 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1195 /* A call may read and modify global register variables. */
1196 if (global_regs[i])
1198 SET_REGNO_REG_SET (reg_pending_sets, i);
1199 SET_REGNO_REG_SET (reg_pending_uses, i);
1201 /* Other call-clobbered hard regs may be clobbered.
1202 Since we only have a choice between 'might be clobbered'
1203 and 'definitely not clobbered', we must include all
1204 partly call-clobbered registers here. */
1205 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
1206 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1207 SET_REGNO_REG_SET (reg_pending_clobbers, i);
1208 /* We don't know what set of fixed registers might be used
1209 by the function, but it is certain that the stack pointer
1210 is among them, but be conservative. */
1211 else if (fixed_regs[i])
1212 SET_REGNO_REG_SET (reg_pending_uses, i);
1213 /* The frame pointer is normally not used by the function
1214 itself, but by the debugger. */
1215 /* ??? MIPS o32 is an exception. It uses the frame pointer
1216 in the macro expansion of jal but does not represent this
1217 fact in the call_insn rtl. */
1218 else if (i == FRAME_POINTER_REGNUM
1219 || (i == HARD_FRAME_POINTER_REGNUM
1220 && (! reload_completed || frame_pointer_needed)))
1221 SET_REGNO_REG_SET (reg_pending_uses, i);
1224 /* For each insn which shouldn't cross a call, add a dependence
1225 between that insn and this call insn. */
1226 add_dependence_list_and_free (insn, &deps->sched_before_next_call,
1227 REG_DEP_ANTI);
1229 sched_analyze_insn (deps, PATTERN (insn), insn, loop_notes);
1230 loop_notes = 0;
1232 /* In the absence of interprocedural alias analysis, we must flush
1233 all pending reads and writes, and start new dependencies starting
1234 from here. But only flush writes for constant calls (which may
1235 be passed a pointer to something we haven't written yet). */
1236 flush_pending_lists (deps, insn, true, !CONST_OR_PURE_CALL_P (insn));
1238 /* Remember the last function call for limiting lifetimes. */
1239 free_INSN_LIST_list (&deps->last_function_call);
1240 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
1242 /* Before reload, begin a post-call group, so as to keep the
1243 lifetimes of hard registers correct. */
1244 if (! reload_completed)
1245 deps->in_post_call_group_p = true;
1248 /* See comments on reemit_notes as to why we do this.
1249 ??? Actually, the reemit_notes just say what is done, not why. */
1251 if (GET_CODE (insn) == NOTE
1252 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
1253 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
1254 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1255 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1257 rtx rtx_region;
1259 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1260 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
1261 rtx_region = GEN_INT (NOTE_EH_HANDLER (insn));
1262 else
1263 rtx_region = GEN_INT (0);
1265 loop_notes = alloc_EXPR_LIST (REG_SAVE_NOTE,
1266 rtx_region,
1267 loop_notes);
1268 loop_notes = alloc_EXPR_LIST (REG_SAVE_NOTE,
1269 GEN_INT (NOTE_LINE_NUMBER (insn)),
1270 loop_notes);
1271 CONST_OR_PURE_CALL_P (loop_notes) = CONST_OR_PURE_CALL_P (insn);
1274 if (current_sched_info->use_cselib)
1275 cselib_process_insn (insn);
1277 /* Now that we have completed handling INSN, check and see if it is
1278 a CLOBBER beginning a libcall block. If it is, record the
1279 end of the libcall sequence.
1281 We want to schedule libcall blocks as a unit before reload. While
1282 this restricts scheduling, it preserves the meaning of a libcall
1283 block.
1285 As a side effect, we may get better code due to decreased register
1286 pressure as well as less chance of a foreign insn appearing in
1287 a libcall block. */
1288 if (!reload_completed
1289 /* Note we may have nested libcall sequences. We only care about
1290 the outermost libcall sequence. */
1291 && deps->libcall_block_tail_insn == 0
1292 /* The sequence must start with a clobber of a register. */
1293 && GET_CODE (insn) == INSN
1294 && GET_CODE (PATTERN (insn)) == CLOBBER
1295 && (r0 = XEXP (PATTERN (insn), 0), GET_CODE (r0) == REG)
1296 && GET_CODE (XEXP (PATTERN (insn), 0)) == REG
1297 /* The CLOBBER must also have a REG_LIBCALL note attached. */
1298 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1299 && (end_seq = XEXP (link, 0)) != 0
1300 /* The insn referenced by the REG_LIBCALL note must be a
1301 simple nop copy with the same destination as the register
1302 mentioned in the clobber. */
1303 && (set = single_set (end_seq)) != 0
1304 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1305 /* And finally the insn referenced by the REG_LIBCALL must
1306 also contain a REG_EQUAL note and a REG_RETVAL note. */
1307 && find_reg_note (end_seq, REG_EQUAL, NULL_RTX) != 0
1308 && find_reg_note (end_seq, REG_RETVAL, NULL_RTX) != 0)
1309 deps->libcall_block_tail_insn = XEXP (link, 0);
1311 /* If we have reached the end of a libcall block, then close the
1312 block. */
1313 if (deps->libcall_block_tail_insn == insn)
1314 deps->libcall_block_tail_insn = 0;
1316 if (insn == tail)
1318 if (current_sched_info->use_cselib)
1319 cselib_finish ();
1320 return;
1323 abort ();
1327 /* The following function adds forward dependence (FROM, TO) with
1328 given DEP_TYPE. The forward dependence should be not exist before. */
1330 void
1331 add_forward_dependence (rtx from, rtx to, enum reg_note dep_type)
1333 rtx new_link;
1335 #ifdef ENABLE_CHECKING
1336 /* If add_dependence is working properly there should never
1337 be notes, deleted insns or duplicates in the backward
1338 links. Thus we need not check for them here.
1340 However, if we have enabled checking we might as well go
1341 ahead and verify that add_dependence worked properly. */
1342 if (GET_CODE (from) == NOTE
1343 || INSN_DELETED_P (from)
1344 || (forward_dependency_cache != NULL
1345 && TEST_BIT (forward_dependency_cache[INSN_LUID (from)],
1346 INSN_LUID (to)))
1347 || (forward_dependency_cache == NULL
1348 && find_insn_list (to, INSN_DEPEND (from))))
1349 abort ();
1350 if (forward_dependency_cache != NULL)
1351 SET_BIT (forward_dependency_cache[INSN_LUID (from)],
1352 INSN_LUID (to));
1353 #endif
1355 new_link = alloc_INSN_LIST (to, INSN_DEPEND (from));
1357 PUT_REG_NOTE_KIND (new_link, dep_type);
1359 INSN_DEPEND (from) = new_link;
1360 INSN_DEP_COUNT (to) += 1;
1363 /* Examine insns in the range [ HEAD, TAIL ] and Use the backward
1364 dependences from LOG_LINKS to build forward dependences in
1365 INSN_DEPEND. */
1367 void
1368 compute_forward_dependences (rtx head, rtx tail)
1370 rtx insn, link;
1371 rtx next_tail;
1373 next_tail = NEXT_INSN (tail);
1374 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1376 if (! INSN_P (insn))
1377 continue;
1379 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1380 add_forward_dependence (XEXP (link, 0), insn, REG_NOTE_KIND (link));
1384 /* Initialize variables for region data dependence analysis.
1385 n_bbs is the number of region blocks. */
1387 void
1388 init_deps (struct deps *deps)
1390 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
1392 deps->max_reg = max_reg;
1393 deps->reg_last = xcalloc (max_reg, sizeof (struct deps_reg));
1394 INIT_REG_SET (&deps->reg_last_in_use);
1395 INIT_REG_SET (&deps->reg_conditional_sets);
1397 deps->pending_read_insns = 0;
1398 deps->pending_read_mems = 0;
1399 deps->pending_write_insns = 0;
1400 deps->pending_write_mems = 0;
1401 deps->pending_lists_length = 0;
1402 deps->pending_flush_length = 0;
1403 deps->last_pending_memory_flush = 0;
1404 deps->last_function_call = 0;
1405 deps->sched_before_next_call = 0;
1406 deps->in_post_call_group_p = false;
1407 deps->libcall_block_tail_insn = 0;
1410 /* Free insn lists found in DEPS. */
1412 void
1413 free_deps (struct deps *deps)
1415 int i;
1417 free_INSN_LIST_list (&deps->pending_read_insns);
1418 free_EXPR_LIST_list (&deps->pending_read_mems);
1419 free_INSN_LIST_list (&deps->pending_write_insns);
1420 free_EXPR_LIST_list (&deps->pending_write_mems);
1421 free_INSN_LIST_list (&deps->last_pending_memory_flush);
1423 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
1424 times. For a test case with 42000 regs and 8000 small basic blocks,
1425 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
1426 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
1428 struct deps_reg *reg_last = &deps->reg_last[i];
1429 if (reg_last->uses)
1430 free_INSN_LIST_list (&reg_last->uses);
1431 if (reg_last->sets)
1432 free_INSN_LIST_list (&reg_last->sets);
1433 if (reg_last->clobbers)
1434 free_INSN_LIST_list (&reg_last->clobbers);
1436 CLEAR_REG_SET (&deps->reg_last_in_use);
1437 CLEAR_REG_SET (&deps->reg_conditional_sets);
1439 free (deps->reg_last);
1442 /* If it is profitable to use them, initialize caches for tracking
1443 dependency information. LUID is the number of insns to be scheduled,
1444 it is used in the estimate of profitability. */
1446 void
1447 init_dependency_caches (int luid)
1449 /* ?!? We could save some memory by computing a per-region luid mapping
1450 which could reduce both the number of vectors in the cache and the size
1451 of each vector. Instead we just avoid the cache entirely unless the
1452 average number of instructions in a basic block is very high. See
1453 the comment before the declaration of true_dependency_cache for
1454 what we consider "very high". */
1455 if (luid / n_basic_blocks > 100 * 5)
1457 true_dependency_cache = sbitmap_vector_alloc (luid, luid);
1458 sbitmap_vector_zero (true_dependency_cache, luid);
1459 anti_dependency_cache = sbitmap_vector_alloc (luid, luid);
1460 sbitmap_vector_zero (anti_dependency_cache, luid);
1461 output_dependency_cache = sbitmap_vector_alloc (luid, luid);
1462 sbitmap_vector_zero (output_dependency_cache, luid);
1463 #ifdef ENABLE_CHECKING
1464 forward_dependency_cache = sbitmap_vector_alloc (luid, luid);
1465 sbitmap_vector_zero (forward_dependency_cache, luid);
1466 #endif
1470 /* Free the caches allocated in init_dependency_caches. */
1472 void
1473 free_dependency_caches (void)
1475 if (true_dependency_cache)
1477 sbitmap_vector_free (true_dependency_cache);
1478 true_dependency_cache = NULL;
1479 sbitmap_vector_free (anti_dependency_cache);
1480 anti_dependency_cache = NULL;
1481 sbitmap_vector_free (output_dependency_cache);
1482 output_dependency_cache = NULL;
1483 #ifdef ENABLE_CHECKING
1484 sbitmap_vector_free (forward_dependency_cache);
1485 forward_dependency_cache = NULL;
1486 #endif
1490 /* Initialize some global variables needed by the dependency analysis
1491 code. */
1493 void
1494 init_deps_global (void)
1496 reg_pending_sets = INITIALIZE_REG_SET (reg_pending_sets_head);
1497 reg_pending_clobbers = INITIALIZE_REG_SET (reg_pending_clobbers_head);
1498 reg_pending_uses = INITIALIZE_REG_SET (reg_pending_uses_head);
1499 reg_pending_barrier = NOT_A_BARRIER;
1502 /* Free everything used by the dependency analysis code. */
1504 void
1505 finish_deps_global (void)
1507 FREE_REG_SET (reg_pending_sets);
1508 FREE_REG_SET (reg_pending_clobbers);
1509 FREE_REG_SET (reg_pending_uses);