* config/xtensa/lib2funcs.S: Fix whitespace.
[official-gcc.git] / gcc / sched-deps.c
blob477203caa222b4826e993917ec96630f4dab4aca
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 /* Don't move CC0 setter to another block (it can set up the
629 same flag for previous CC0 users which is safe). */
630 CANT_MOVE (prev_nonnote_insn (insn)) = 1;
631 return;
632 #endif
634 case REG:
636 int regno = REGNO (x);
637 if (regno < FIRST_PSEUDO_REGISTER)
639 int i = HARD_REGNO_NREGS (regno, GET_MODE (x));
640 while (--i >= 0)
641 SET_REGNO_REG_SET (reg_pending_uses, regno + i);
643 /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
644 it does not reload. Ignore these as they have served their
645 purpose already. */
646 else if (regno >= deps->max_reg)
648 if (GET_CODE (PATTERN (insn)) != USE
649 && GET_CODE (PATTERN (insn)) != CLOBBER)
650 abort ();
652 else
654 SET_REGNO_REG_SET (reg_pending_uses, regno);
656 /* Pseudos that are REG_EQUIV to something may be replaced
657 by that during reloading. We need only add dependencies for
658 the address in the REG_EQUIV note. */
659 if (!reload_completed
660 && reg_known_equiv_p[regno]
661 && GET_CODE (reg_known_value[regno]) == MEM)
662 sched_analyze_2 (deps, XEXP (reg_known_value[regno], 0), insn);
664 /* If the register does not already cross any calls, then add this
665 insn to the sched_before_next_call list so that it will still
666 not cross calls after scheduling. */
667 if (REG_N_CALLS_CROSSED (regno) == 0)
668 deps->sched_before_next_call
669 = alloc_INSN_LIST (insn, deps->sched_before_next_call);
671 return;
674 case MEM:
676 /* Reading memory. */
677 rtx u;
678 rtx pending, pending_mem;
679 rtx t = x;
681 if (current_sched_info->use_cselib)
683 t = shallow_copy_rtx (t);
684 cselib_lookup (XEXP (t, 0), Pmode, 1);
685 XEXP (t, 0) = cselib_subst_to_values (XEXP (t, 0));
687 pending = deps->pending_read_insns;
688 pending_mem = deps->pending_read_mems;
689 while (pending)
691 if (read_dependence (XEXP (pending_mem, 0), t))
692 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
694 pending = XEXP (pending, 1);
695 pending_mem = XEXP (pending_mem, 1);
698 pending = deps->pending_write_insns;
699 pending_mem = deps->pending_write_mems;
700 while (pending)
702 if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
703 t, rtx_varies_p))
704 add_dependence (insn, XEXP (pending, 0), 0);
706 pending = XEXP (pending, 1);
707 pending_mem = XEXP (pending_mem, 1);
710 for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
711 if (GET_CODE (XEXP (u, 0)) != JUMP_INSN
712 || deps_may_trap_p (x))
713 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
715 /* Always add these dependencies to pending_reads, since
716 this insn may be followed by a write. */
717 add_insn_mem_dependence (deps, &deps->pending_read_insns,
718 &deps->pending_read_mems, insn, x);
720 /* Take advantage of tail recursion here. */
721 sched_analyze_2 (deps, XEXP (x, 0), insn);
722 return;
725 /* Force pending stores to memory in case a trap handler needs them. */
726 case TRAP_IF:
727 flush_pending_lists (deps, insn, true, false);
728 break;
730 case ASM_OPERANDS:
731 case ASM_INPUT:
732 case UNSPEC_VOLATILE:
734 /* Traditional and volatile asm instructions must be considered to use
735 and clobber all hard registers, all pseudo-registers and all of
736 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
738 Consider for instance a volatile asm that changes the fpu rounding
739 mode. An insn should not be moved across this even if it only uses
740 pseudo-regs because it might give an incorrectly rounded result. */
741 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
742 reg_pending_barrier = TRUE_BARRIER;
744 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
745 We can not just fall through here since then we would be confused
746 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
747 traditional asms unlike their normal usage. */
749 if (code == ASM_OPERANDS)
751 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
752 sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
753 return;
755 break;
758 case PRE_DEC:
759 case POST_DEC:
760 case PRE_INC:
761 case POST_INC:
762 /* These both read and modify the result. We must handle them as writes
763 to get proper dependencies for following instructions. We must handle
764 them as reads to get proper dependencies from this to previous
765 instructions. Thus we need to pass them to both sched_analyze_1
766 and sched_analyze_2. We must call sched_analyze_2 first in order
767 to get the proper antecedent for the read. */
768 sched_analyze_2 (deps, XEXP (x, 0), insn);
769 sched_analyze_1 (deps, x, insn);
770 return;
772 case POST_MODIFY:
773 case PRE_MODIFY:
774 /* op0 = op0 + op1 */
775 sched_analyze_2 (deps, XEXP (x, 0), insn);
776 sched_analyze_2 (deps, XEXP (x, 1), insn);
777 sched_analyze_1 (deps, x, insn);
778 return;
780 default:
781 break;
784 /* Other cases: walk the insn. */
785 fmt = GET_RTX_FORMAT (code);
786 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
788 if (fmt[i] == 'e')
789 sched_analyze_2 (deps, XEXP (x, i), insn);
790 else if (fmt[i] == 'E')
791 for (j = 0; j < XVECLEN (x, i); j++)
792 sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
796 /* Analyze an INSN with pattern X to find all dependencies. */
798 static void
799 sched_analyze_insn (struct deps *deps, rtx x, rtx insn, rtx loop_notes)
801 RTX_CODE code = GET_CODE (x);
802 rtx link;
803 int i;
805 if (code == COND_EXEC)
807 sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
809 /* ??? Should be recording conditions so we reduce the number of
810 false dependencies. */
811 x = COND_EXEC_CODE (x);
812 code = GET_CODE (x);
814 if (code == SET || code == CLOBBER)
816 sched_analyze_1 (deps, x, insn);
818 /* Bare clobber insns are used for letting life analysis, reg-stack
819 and others know that a value is dead. Depend on the last call
820 instruction so that reg-stack won't get confused. */
821 if (code == CLOBBER)
822 add_dependence_list (insn, deps->last_function_call, REG_DEP_OUTPUT);
824 else if (code == PARALLEL)
826 int i;
827 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
829 rtx sub = XVECEXP (x, 0, i);
830 code = GET_CODE (sub);
832 if (code == COND_EXEC)
834 sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
835 sub = COND_EXEC_CODE (sub);
836 code = GET_CODE (sub);
838 if (code == SET || code == CLOBBER)
839 sched_analyze_1 (deps, sub, insn);
840 else
841 sched_analyze_2 (deps, sub, insn);
844 else
845 sched_analyze_2 (deps, x, insn);
847 /* Mark registers CLOBBERED or used by called function. */
848 if (GET_CODE (insn) == CALL_INSN)
850 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
852 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
853 sched_analyze_1 (deps, XEXP (link, 0), insn);
854 else
855 sched_analyze_2 (deps, XEXP (link, 0), insn);
857 if (find_reg_note (insn, REG_SETJMP, NULL))
858 reg_pending_barrier = MOVE_BARRIER;
861 if (GET_CODE (insn) == JUMP_INSN)
863 rtx next;
864 next = next_nonnote_insn (insn);
865 if (next && GET_CODE (next) == BARRIER)
866 reg_pending_barrier = TRUE_BARRIER;
867 else
869 rtx pending, pending_mem;
870 regset_head tmp_uses, tmp_sets;
871 INIT_REG_SET (&tmp_uses);
872 INIT_REG_SET (&tmp_sets);
874 (*current_sched_info->compute_jump_reg_dependencies)
875 (insn, &deps->reg_conditional_sets, &tmp_uses, &tmp_sets);
876 /* Make latency of jump equal to 0 by using anti-dependence. */
877 EXECUTE_IF_SET_IN_REG_SET (&tmp_uses, 0, i,
879 struct deps_reg *reg_last = &deps->reg_last[i];
880 add_dependence_list (insn, reg_last->sets, REG_DEP_ANTI);
881 add_dependence_list (insn, reg_last->clobbers, REG_DEP_ANTI);
882 reg_last->uses_length++;
883 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
885 IOR_REG_SET (reg_pending_sets, &tmp_sets);
887 CLEAR_REG_SET (&tmp_uses);
888 CLEAR_REG_SET (&tmp_sets);
890 /* All memory writes and volatile reads must happen before the
891 jump. Non-volatile reads must happen before the jump iff
892 the result is needed by the above register used mask. */
894 pending = deps->pending_write_insns;
895 pending_mem = deps->pending_write_mems;
896 while (pending)
898 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
899 pending = XEXP (pending, 1);
900 pending_mem = XEXP (pending_mem, 1);
903 pending = deps->pending_read_insns;
904 pending_mem = deps->pending_read_mems;
905 while (pending)
907 if (MEM_VOLATILE_P (XEXP (pending_mem, 0)))
908 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
909 pending = XEXP (pending, 1);
910 pending_mem = XEXP (pending_mem, 1);
913 add_dependence_list (insn, deps->last_pending_memory_flush,
914 REG_DEP_ANTI);
918 /* If there is a {LOOP,EHREGION}_{BEG,END} note in the middle of a basic
919 block, then we must be sure that no instructions are scheduled across it.
920 Otherwise, the reg_n_refs info (which depends on loop_depth) would
921 become incorrect. */
922 if (loop_notes)
924 rtx link;
926 /* Update loop_notes with any notes from this insn. Also determine
927 if any of the notes on the list correspond to instruction scheduling
928 barriers (loop, eh & setjmp notes, but not range notes). */
929 link = loop_notes;
930 while (XEXP (link, 1))
932 if (INTVAL (XEXP (link, 0)) == NOTE_INSN_LOOP_BEG
933 || INTVAL (XEXP (link, 0)) == NOTE_INSN_LOOP_END
934 || INTVAL (XEXP (link, 0)) == NOTE_INSN_EH_REGION_BEG
935 || INTVAL (XEXP (link, 0)) == NOTE_INSN_EH_REGION_END)
936 reg_pending_barrier = MOVE_BARRIER;
938 link = XEXP (link, 1);
940 XEXP (link, 1) = REG_NOTES (insn);
941 REG_NOTES (insn) = loop_notes;
944 /* If this instruction can throw an exception, then moving it changes
945 where block boundaries fall. This is mighty confusing elsewhere.
946 Therefore, prevent such an instruction from being moved. */
947 if (can_throw_internal (insn))
948 reg_pending_barrier = MOVE_BARRIER;
950 /* Add dependencies if a scheduling barrier was found. */
951 if (reg_pending_barrier)
953 /* In the case of barrier the most added dependencies are not
954 real, so we use anti-dependence here. */
955 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
957 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
959 struct deps_reg *reg_last = &deps->reg_last[i];
960 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
961 add_dependence_list
962 (insn, reg_last->sets,
963 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
964 add_dependence_list
965 (insn, reg_last->clobbers,
966 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
969 else
971 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
973 struct deps_reg *reg_last = &deps->reg_last[i];
974 add_dependence_list_and_free (insn, &reg_last->uses,
975 REG_DEP_ANTI);
976 add_dependence_list_and_free
977 (insn, &reg_last->sets,
978 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
979 add_dependence_list_and_free
980 (insn, &reg_last->clobbers,
981 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
982 reg_last->uses_length = 0;
983 reg_last->clobbers_length = 0;
987 for (i = 0; i < deps->max_reg; i++)
989 struct deps_reg *reg_last = &deps->reg_last[i];
990 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
991 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
994 flush_pending_lists (deps, insn, true, true);
995 CLEAR_REG_SET (&deps->reg_conditional_sets);
996 reg_pending_barrier = NOT_A_BARRIER;
998 else
1000 /* If the current insn is conditional, we can't free any
1001 of the lists. */
1002 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1004 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i,
1006 struct deps_reg *reg_last = &deps->reg_last[i];
1007 add_dependence_list (insn, reg_last->sets, 0);
1008 add_dependence_list (insn, reg_last->clobbers, 0);
1009 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1010 reg_last->uses_length++;
1012 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i,
1014 struct deps_reg *reg_last = &deps->reg_last[i];
1015 add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1016 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1017 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
1018 reg_last->clobbers_length++;
1020 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
1022 struct deps_reg *reg_last = &deps->reg_last[i];
1023 add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1024 add_dependence_list (insn, reg_last->clobbers, REG_DEP_OUTPUT);
1025 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1026 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1027 SET_REGNO_REG_SET (&deps->reg_conditional_sets, i);
1030 else
1032 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i,
1034 struct deps_reg *reg_last = &deps->reg_last[i];
1035 add_dependence_list (insn, reg_last->sets, 0);
1036 add_dependence_list (insn, reg_last->clobbers, 0);
1037 reg_last->uses_length++;
1038 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1040 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i,
1042 struct deps_reg *reg_last = &deps->reg_last[i];
1043 if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
1044 || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
1046 add_dependence_list_and_free (insn, &reg_last->sets,
1047 REG_DEP_OUTPUT);
1048 add_dependence_list_and_free (insn, &reg_last->uses,
1049 REG_DEP_ANTI);
1050 add_dependence_list_and_free (insn, &reg_last->clobbers,
1051 REG_DEP_OUTPUT);
1052 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1053 reg_last->clobbers_length = 0;
1054 reg_last->uses_length = 0;
1056 else
1058 add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1059 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1061 reg_last->clobbers_length++;
1062 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
1064 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
1066 struct deps_reg *reg_last = &deps->reg_last[i];
1067 add_dependence_list_and_free (insn, &reg_last->sets,
1068 REG_DEP_OUTPUT);
1069 add_dependence_list_and_free (insn, &reg_last->clobbers,
1070 REG_DEP_OUTPUT);
1071 add_dependence_list_and_free (insn, &reg_last->uses,
1072 REG_DEP_ANTI);
1073 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1074 reg_last->uses_length = 0;
1075 reg_last->clobbers_length = 0;
1076 CLEAR_REGNO_REG_SET (&deps->reg_conditional_sets, i);
1080 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
1081 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
1082 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
1084 CLEAR_REG_SET (reg_pending_uses);
1085 CLEAR_REG_SET (reg_pending_clobbers);
1086 CLEAR_REG_SET (reg_pending_sets);
1088 /* If we are currently in a libcall scheduling group, then mark the
1089 current insn as being in a scheduling group and that it can not
1090 be moved into a different basic block. */
1092 if (deps->libcall_block_tail_insn)
1094 set_sched_group_p (insn);
1095 CANT_MOVE (insn) = 1;
1098 /* If a post-call group is still open, see if it should remain so.
1099 This insn must be a simple move of a hard reg to a pseudo or
1100 vice-versa.
1102 We must avoid moving these insns for correctness on
1103 SMALL_REGISTER_CLASS machines, and for special registers like
1104 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
1105 hard regs for all targets. */
1107 if (deps->in_post_call_group_p)
1109 rtx tmp, set = single_set (insn);
1110 int src_regno, dest_regno;
1112 if (set == NULL)
1113 goto end_call_group;
1115 tmp = SET_DEST (set);
1116 if (GET_CODE (tmp) == SUBREG)
1117 tmp = SUBREG_REG (tmp);
1118 if (GET_CODE (tmp) == REG)
1119 dest_regno = REGNO (tmp);
1120 else
1121 goto end_call_group;
1123 tmp = SET_SRC (set);
1124 if (GET_CODE (tmp) == SUBREG)
1125 tmp = SUBREG_REG (tmp);
1126 if (GET_CODE (tmp) == REG)
1127 src_regno = REGNO (tmp);
1128 else
1129 goto end_call_group;
1131 if (src_regno < FIRST_PSEUDO_REGISTER
1132 || dest_regno < FIRST_PSEUDO_REGISTER)
1134 set_sched_group_p (insn);
1135 CANT_MOVE (insn) = 1;
1137 else
1139 end_call_group:
1140 deps->in_post_call_group_p = false;
1145 /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
1146 for every dependency. */
1148 void
1149 sched_analyze (struct deps *deps, rtx head, rtx tail)
1151 rtx insn;
1152 rtx loop_notes = 0;
1154 if (current_sched_info->use_cselib)
1155 cselib_init ();
1157 for (insn = head;; insn = NEXT_INSN (insn))
1159 rtx link, end_seq, r0, set;
1161 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
1163 /* Clear out the stale LOG_LINKS from flow. */
1164 free_INSN_LIST_list (&LOG_LINKS (insn));
1166 /* Make each JUMP_INSN a scheduling barrier for memory
1167 references. */
1168 if (GET_CODE (insn) == JUMP_INSN)
1170 /* Keep the list a reasonable size. */
1171 if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
1172 flush_pending_lists (deps, insn, true, true);
1173 else
1174 deps->last_pending_memory_flush
1175 = alloc_INSN_LIST (insn, deps->last_pending_memory_flush);
1177 sched_analyze_insn (deps, PATTERN (insn), insn, loop_notes);
1178 loop_notes = 0;
1180 else if (GET_CODE (insn) == CALL_INSN)
1182 int i;
1184 CANT_MOVE (insn) = 1;
1186 /* Clear out the stale LOG_LINKS from flow. */
1187 free_INSN_LIST_list (&LOG_LINKS (insn));
1189 if (find_reg_note (insn, REG_SETJMP, NULL))
1191 /* This is setjmp. Assume that all registers, not just
1192 hard registers, may be clobbered by this call. */
1193 reg_pending_barrier = MOVE_BARRIER;
1195 else
1197 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1198 /* A call may read and modify global register variables. */
1199 if (global_regs[i])
1201 SET_REGNO_REG_SET (reg_pending_sets, i);
1202 SET_REGNO_REG_SET (reg_pending_uses, i);
1204 /* Other call-clobbered hard regs may be clobbered.
1205 Since we only have a choice between 'might be clobbered'
1206 and 'definitely not clobbered', we must include all
1207 partly call-clobbered registers here. */
1208 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
1209 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1210 SET_REGNO_REG_SET (reg_pending_clobbers, i);
1211 /* We don't know what set of fixed registers might be used
1212 by the function, but it is certain that the stack pointer
1213 is among them, but be conservative. */
1214 else if (fixed_regs[i])
1215 SET_REGNO_REG_SET (reg_pending_uses, i);
1216 /* The frame pointer is normally not used by the function
1217 itself, but by the debugger. */
1218 /* ??? MIPS o32 is an exception. It uses the frame pointer
1219 in the macro expansion of jal but does not represent this
1220 fact in the call_insn rtl. */
1221 else if (i == FRAME_POINTER_REGNUM
1222 || (i == HARD_FRAME_POINTER_REGNUM
1223 && (! reload_completed || frame_pointer_needed)))
1224 SET_REGNO_REG_SET (reg_pending_uses, i);
1227 /* For each insn which shouldn't cross a call, add a dependence
1228 between that insn and this call insn. */
1229 add_dependence_list_and_free (insn, &deps->sched_before_next_call,
1230 REG_DEP_ANTI);
1232 sched_analyze_insn (deps, PATTERN (insn), insn, loop_notes);
1233 loop_notes = 0;
1235 /* In the absence of interprocedural alias analysis, we must flush
1236 all pending reads and writes, and start new dependencies starting
1237 from here. But only flush writes for constant calls (which may
1238 be passed a pointer to something we haven't written yet). */
1239 flush_pending_lists (deps, insn, true, !CONST_OR_PURE_CALL_P (insn));
1241 /* Remember the last function call for limiting lifetimes. */
1242 free_INSN_LIST_list (&deps->last_function_call);
1243 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
1245 /* Before reload, begin a post-call group, so as to keep the
1246 lifetimes of hard registers correct. */
1247 if (! reload_completed)
1248 deps->in_post_call_group_p = true;
1251 /* See comments on reemit_notes as to why we do this.
1252 ??? Actually, the reemit_notes just say what is done, not why. */
1254 if (GET_CODE (insn) == NOTE
1255 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
1256 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
1257 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1258 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1260 rtx rtx_region;
1262 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1263 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
1264 rtx_region = GEN_INT (NOTE_EH_HANDLER (insn));
1265 else
1266 rtx_region = GEN_INT (0);
1268 loop_notes = alloc_EXPR_LIST (REG_SAVE_NOTE,
1269 rtx_region,
1270 loop_notes);
1271 loop_notes = alloc_EXPR_LIST (REG_SAVE_NOTE,
1272 GEN_INT (NOTE_LINE_NUMBER (insn)),
1273 loop_notes);
1274 CONST_OR_PURE_CALL_P (loop_notes) = CONST_OR_PURE_CALL_P (insn);
1277 if (current_sched_info->use_cselib)
1278 cselib_process_insn (insn);
1280 /* Now that we have completed handling INSN, check and see if it is
1281 a CLOBBER beginning a libcall block. If it is, record the
1282 end of the libcall sequence.
1284 We want to schedule libcall blocks as a unit before reload. While
1285 this restricts scheduling, it preserves the meaning of a libcall
1286 block.
1288 As a side effect, we may get better code due to decreased register
1289 pressure as well as less chance of a foreign insn appearing in
1290 a libcall block. */
1291 if (!reload_completed
1292 /* Note we may have nested libcall sequences. We only care about
1293 the outermost libcall sequence. */
1294 && deps->libcall_block_tail_insn == 0
1295 /* The sequence must start with a clobber of a register. */
1296 && GET_CODE (insn) == INSN
1297 && GET_CODE (PATTERN (insn)) == CLOBBER
1298 && (r0 = XEXP (PATTERN (insn), 0), GET_CODE (r0) == REG)
1299 && GET_CODE (XEXP (PATTERN (insn), 0)) == REG
1300 /* The CLOBBER must also have a REG_LIBCALL note attached. */
1301 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1302 && (end_seq = XEXP (link, 0)) != 0
1303 /* The insn referenced by the REG_LIBCALL note must be a
1304 simple nop copy with the same destination as the register
1305 mentioned in the clobber. */
1306 && (set = single_set (end_seq)) != 0
1307 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1308 /* And finally the insn referenced by the REG_LIBCALL must
1309 also contain a REG_EQUAL note and a REG_RETVAL note. */
1310 && find_reg_note (end_seq, REG_EQUAL, NULL_RTX) != 0
1311 && find_reg_note (end_seq, REG_RETVAL, NULL_RTX) != 0)
1312 deps->libcall_block_tail_insn = XEXP (link, 0);
1314 /* If we have reached the end of a libcall block, then close the
1315 block. */
1316 if (deps->libcall_block_tail_insn == insn)
1317 deps->libcall_block_tail_insn = 0;
1319 if (insn == tail)
1321 if (current_sched_info->use_cselib)
1322 cselib_finish ();
1323 return;
1326 abort ();
1330 /* The following function adds forward dependence (FROM, TO) with
1331 given DEP_TYPE. The forward dependence should be not exist before. */
1333 void
1334 add_forward_dependence (rtx from, rtx to, enum reg_note dep_type)
1336 rtx new_link;
1338 #ifdef ENABLE_CHECKING
1339 /* If add_dependence is working properly there should never
1340 be notes, deleted insns or duplicates in the backward
1341 links. Thus we need not check for them here.
1343 However, if we have enabled checking we might as well go
1344 ahead and verify that add_dependence worked properly. */
1345 if (GET_CODE (from) == NOTE
1346 || INSN_DELETED_P (from)
1347 || (forward_dependency_cache != NULL
1348 && TEST_BIT (forward_dependency_cache[INSN_LUID (from)],
1349 INSN_LUID (to)))
1350 || (forward_dependency_cache == NULL
1351 && find_insn_list (to, INSN_DEPEND (from))))
1352 abort ();
1353 if (forward_dependency_cache != NULL)
1354 SET_BIT (forward_dependency_cache[INSN_LUID (from)],
1355 INSN_LUID (to));
1356 #endif
1358 new_link = alloc_INSN_LIST (to, INSN_DEPEND (from));
1360 PUT_REG_NOTE_KIND (new_link, dep_type);
1362 INSN_DEPEND (from) = new_link;
1363 INSN_DEP_COUNT (to) += 1;
1366 /* Examine insns in the range [ HEAD, TAIL ] and Use the backward
1367 dependences from LOG_LINKS to build forward dependences in
1368 INSN_DEPEND. */
1370 void
1371 compute_forward_dependences (rtx head, rtx tail)
1373 rtx insn, link;
1374 rtx next_tail;
1376 next_tail = NEXT_INSN (tail);
1377 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1379 if (! INSN_P (insn))
1380 continue;
1382 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1383 add_forward_dependence (XEXP (link, 0), insn, REG_NOTE_KIND (link));
1387 /* Initialize variables for region data dependence analysis.
1388 n_bbs is the number of region blocks. */
1390 void
1391 init_deps (struct deps *deps)
1393 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
1395 deps->max_reg = max_reg;
1396 deps->reg_last = xcalloc (max_reg, sizeof (struct deps_reg));
1397 INIT_REG_SET (&deps->reg_last_in_use);
1398 INIT_REG_SET (&deps->reg_conditional_sets);
1400 deps->pending_read_insns = 0;
1401 deps->pending_read_mems = 0;
1402 deps->pending_write_insns = 0;
1403 deps->pending_write_mems = 0;
1404 deps->pending_lists_length = 0;
1405 deps->pending_flush_length = 0;
1406 deps->last_pending_memory_flush = 0;
1407 deps->last_function_call = 0;
1408 deps->sched_before_next_call = 0;
1409 deps->in_post_call_group_p = false;
1410 deps->libcall_block_tail_insn = 0;
1413 /* Free insn lists found in DEPS. */
1415 void
1416 free_deps (struct deps *deps)
1418 int i;
1420 free_INSN_LIST_list (&deps->pending_read_insns);
1421 free_EXPR_LIST_list (&deps->pending_read_mems);
1422 free_INSN_LIST_list (&deps->pending_write_insns);
1423 free_EXPR_LIST_list (&deps->pending_write_mems);
1424 free_INSN_LIST_list (&deps->last_pending_memory_flush);
1426 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
1427 times. For a test case with 42000 regs and 8000 small basic blocks,
1428 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
1429 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
1431 struct deps_reg *reg_last = &deps->reg_last[i];
1432 if (reg_last->uses)
1433 free_INSN_LIST_list (&reg_last->uses);
1434 if (reg_last->sets)
1435 free_INSN_LIST_list (&reg_last->sets);
1436 if (reg_last->clobbers)
1437 free_INSN_LIST_list (&reg_last->clobbers);
1439 CLEAR_REG_SET (&deps->reg_last_in_use);
1440 CLEAR_REG_SET (&deps->reg_conditional_sets);
1442 free (deps->reg_last);
1445 /* If it is profitable to use them, initialize caches for tracking
1446 dependency information. LUID is the number of insns to be scheduled,
1447 it is used in the estimate of profitability. */
1449 void
1450 init_dependency_caches (int luid)
1452 /* ?!? We could save some memory by computing a per-region luid mapping
1453 which could reduce both the number of vectors in the cache and the size
1454 of each vector. Instead we just avoid the cache entirely unless the
1455 average number of instructions in a basic block is very high. See
1456 the comment before the declaration of true_dependency_cache for
1457 what we consider "very high". */
1458 if (luid / n_basic_blocks > 100 * 5)
1460 true_dependency_cache = sbitmap_vector_alloc (luid, luid);
1461 sbitmap_vector_zero (true_dependency_cache, luid);
1462 anti_dependency_cache = sbitmap_vector_alloc (luid, luid);
1463 sbitmap_vector_zero (anti_dependency_cache, luid);
1464 output_dependency_cache = sbitmap_vector_alloc (luid, luid);
1465 sbitmap_vector_zero (output_dependency_cache, luid);
1466 #ifdef ENABLE_CHECKING
1467 forward_dependency_cache = sbitmap_vector_alloc (luid, luid);
1468 sbitmap_vector_zero (forward_dependency_cache, luid);
1469 #endif
1473 /* Free the caches allocated in init_dependency_caches. */
1475 void
1476 free_dependency_caches (void)
1478 if (true_dependency_cache)
1480 sbitmap_vector_free (true_dependency_cache);
1481 true_dependency_cache = NULL;
1482 sbitmap_vector_free (anti_dependency_cache);
1483 anti_dependency_cache = NULL;
1484 sbitmap_vector_free (output_dependency_cache);
1485 output_dependency_cache = NULL;
1486 #ifdef ENABLE_CHECKING
1487 sbitmap_vector_free (forward_dependency_cache);
1488 forward_dependency_cache = NULL;
1489 #endif
1493 /* Initialize some global variables needed by the dependency analysis
1494 code. */
1496 void
1497 init_deps_global (void)
1499 reg_pending_sets = INITIALIZE_REG_SET (reg_pending_sets_head);
1500 reg_pending_clobbers = INITIALIZE_REG_SET (reg_pending_clobbers_head);
1501 reg_pending_uses = INITIALIZE_REG_SET (reg_pending_uses_head);
1502 reg_pending_barrier = NOT_A_BARRIER;
1505 /* Free everything used by the dependency analysis code. */
1507 void
1508 finish_deps_global (void)
1510 FREE_REG_SET (reg_pending_sets);
1511 FREE_REG_SET (reg_pending_clobbers);
1512 FREE_REG_SET (reg_pending_uses);