* cgraph.c (cgraph_node_name): New function.
[official-gcc.git] / gcc / sched-deps.c
blobcb85feaae387bdd606b74d9851efd5443e7e35e0
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;
868 INIT_REG_SET (&tmp);
870 (*current_sched_info->compute_jump_reg_dependencies) (insn, &tmp);
871 /* Make latency of jump equal to 0 by using anti-dependence. */
872 EXECUTE_IF_SET_IN_REG_SET (&tmp, 0, i,
874 struct deps_reg *reg_last = &deps->reg_last[i];
875 add_dependence_list (insn, reg_last->sets, REG_DEP_ANTI);
876 add_dependence_list (insn, reg_last->clobbers, REG_DEP_ANTI);
877 reg_last->uses_length++;
878 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
880 CLEAR_REG_SET (&tmp);
882 /* All memory writes and volatile reads must happen before the
883 jump. Non-volatile reads must happen before the jump iff
884 the result is needed by the above register used mask. */
886 pending = deps->pending_write_insns;
887 pending_mem = deps->pending_write_mems;
888 while (pending)
890 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
891 pending = XEXP (pending, 1);
892 pending_mem = XEXP (pending_mem, 1);
895 pending = deps->pending_read_insns;
896 pending_mem = deps->pending_read_mems;
897 while (pending)
899 if (MEM_VOLATILE_P (XEXP (pending_mem, 0)))
900 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
901 pending = XEXP (pending, 1);
902 pending_mem = XEXP (pending_mem, 1);
905 add_dependence_list (insn, deps->last_pending_memory_flush,
906 REG_DEP_ANTI);
910 /* If there is a {LOOP,EHREGION}_{BEG,END} note in the middle of a basic
911 block, then we must be sure that no instructions are scheduled across it.
912 Otherwise, the reg_n_refs info (which depends on loop_depth) would
913 become incorrect. */
914 if (loop_notes)
916 rtx link;
918 /* Update loop_notes with any notes from this insn. Also determine
919 if any of the notes on the list correspond to instruction scheduling
920 barriers (loop, eh & setjmp notes, but not range notes). */
921 link = loop_notes;
922 while (XEXP (link, 1))
924 if (INTVAL (XEXP (link, 0)) == NOTE_INSN_LOOP_BEG
925 || INTVAL (XEXP (link, 0)) == NOTE_INSN_LOOP_END
926 || INTVAL (XEXP (link, 0)) == NOTE_INSN_EH_REGION_BEG
927 || INTVAL (XEXP (link, 0)) == NOTE_INSN_EH_REGION_END)
928 reg_pending_barrier = MOVE_BARRIER;
930 link = XEXP (link, 1);
932 XEXP (link, 1) = REG_NOTES (insn);
933 REG_NOTES (insn) = loop_notes;
936 /* If this instruction can throw an exception, then moving it changes
937 where block boundaries fall. This is mighty confusing elsewhere.
938 Therefore, prevent such an instruction from being moved. */
939 if (can_throw_internal (insn))
940 reg_pending_barrier = MOVE_BARRIER;
942 /* Add dependencies if a scheduling barrier was found. */
943 if (reg_pending_barrier)
945 /* In the case of barrier the most added dependencies are not
946 real, so we use anti-dependence here. */
947 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
949 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
951 struct deps_reg *reg_last = &deps->reg_last[i];
952 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
953 add_dependence_list
954 (insn, reg_last->sets,
955 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
956 add_dependence_list
957 (insn, reg_last->clobbers,
958 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
961 else
963 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
965 struct deps_reg *reg_last = &deps->reg_last[i];
966 add_dependence_list_and_free (insn, &reg_last->uses,
967 REG_DEP_ANTI);
968 add_dependence_list_and_free
969 (insn, &reg_last->sets,
970 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
971 add_dependence_list_and_free
972 (insn, &reg_last->clobbers,
973 reg_pending_barrier == TRUE_BARRIER ? 0 : REG_DEP_ANTI);
974 reg_last->uses_length = 0;
975 reg_last->clobbers_length = 0;
979 for (i = 0; i < deps->max_reg; i++)
981 struct deps_reg *reg_last = &deps->reg_last[i];
982 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
983 SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
986 flush_pending_lists (deps, insn, true, true);
987 reg_pending_barrier = NOT_A_BARRIER;
989 else
991 /* If the current insn is conditional, we can't free any
992 of the lists. */
993 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
995 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i,
997 struct deps_reg *reg_last = &deps->reg_last[i];
998 add_dependence_list (insn, reg_last->sets, 0);
999 add_dependence_list (insn, reg_last->clobbers, 0);
1000 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1001 reg_last->uses_length++;
1003 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i,
1005 struct deps_reg *reg_last = &deps->reg_last[i];
1006 add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1007 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1008 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
1009 reg_last->clobbers_length++;
1011 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
1013 struct deps_reg *reg_last = &deps->reg_last[i];
1014 add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1015 add_dependence_list (insn, reg_last->clobbers, REG_DEP_OUTPUT);
1016 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1017 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1020 else
1022 EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i,
1024 struct deps_reg *reg_last = &deps->reg_last[i];
1025 add_dependence_list (insn, reg_last->sets, 0);
1026 add_dependence_list (insn, reg_last->clobbers, 0);
1027 reg_last->uses_length++;
1028 reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
1030 EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i,
1032 struct deps_reg *reg_last = &deps->reg_last[i];
1033 if (reg_last->uses_length > MAX_PENDING_LIST_LENGTH
1034 || reg_last->clobbers_length > MAX_PENDING_LIST_LENGTH)
1036 add_dependence_list_and_free (insn, &reg_last->sets,
1037 REG_DEP_OUTPUT);
1038 add_dependence_list_and_free (insn, &reg_last->uses,
1039 REG_DEP_ANTI);
1040 add_dependence_list_and_free (insn, &reg_last->clobbers,
1041 REG_DEP_OUTPUT);
1042 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1043 reg_last->clobbers_length = 0;
1044 reg_last->uses_length = 0;
1046 else
1048 add_dependence_list (insn, reg_last->sets, REG_DEP_OUTPUT);
1049 add_dependence_list (insn, reg_last->uses, REG_DEP_ANTI);
1051 reg_last->clobbers_length++;
1052 reg_last->clobbers = alloc_INSN_LIST (insn, reg_last->clobbers);
1054 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
1056 struct deps_reg *reg_last = &deps->reg_last[i];
1057 add_dependence_list_and_free (insn, &reg_last->sets,
1058 REG_DEP_OUTPUT);
1059 add_dependence_list_and_free (insn, &reg_last->clobbers,
1060 REG_DEP_OUTPUT);
1061 add_dependence_list_and_free (insn, &reg_last->uses,
1062 REG_DEP_ANTI);
1063 reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
1064 reg_last->uses_length = 0;
1065 reg_last->clobbers_length = 0;
1069 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
1070 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
1071 IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
1073 CLEAR_REG_SET (reg_pending_uses);
1074 CLEAR_REG_SET (reg_pending_clobbers);
1075 CLEAR_REG_SET (reg_pending_sets);
1077 /* If we are currently in a libcall scheduling group, then mark the
1078 current insn as being in a scheduling group and that it can not
1079 be moved into a different basic block. */
1081 if (deps->libcall_block_tail_insn)
1083 set_sched_group_p (insn);
1084 CANT_MOVE (insn) = 1;
1087 /* If a post-call group is still open, see if it should remain so.
1088 This insn must be a simple move of a hard reg to a pseudo or
1089 vice-versa.
1091 We must avoid moving these insns for correctness on
1092 SMALL_REGISTER_CLASS machines, and for special registers like
1093 PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
1094 hard regs for all targets. */
1096 if (deps->in_post_call_group_p)
1098 rtx tmp, set = single_set (insn);
1099 int src_regno, dest_regno;
1101 if (set == NULL)
1102 goto end_call_group;
1104 tmp = SET_DEST (set);
1105 if (GET_CODE (tmp) == SUBREG)
1106 tmp = SUBREG_REG (tmp);
1107 if (GET_CODE (tmp) == REG)
1108 dest_regno = REGNO (tmp);
1109 else
1110 goto end_call_group;
1112 tmp = SET_SRC (set);
1113 if (GET_CODE (tmp) == SUBREG)
1114 tmp = SUBREG_REG (tmp);
1115 if (GET_CODE (tmp) == REG)
1116 src_regno = REGNO (tmp);
1117 else
1118 goto end_call_group;
1120 if (src_regno < FIRST_PSEUDO_REGISTER
1121 || dest_regno < FIRST_PSEUDO_REGISTER)
1123 set_sched_group_p (insn);
1124 CANT_MOVE (insn) = 1;
1126 else
1128 end_call_group:
1129 deps->in_post_call_group_p = false;
1134 /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
1135 for every dependency. */
1137 void
1138 sched_analyze (struct deps *deps, rtx head, rtx tail)
1140 rtx insn;
1141 rtx loop_notes = 0;
1143 if (current_sched_info->use_cselib)
1144 cselib_init ();
1146 for (insn = head;; insn = NEXT_INSN (insn))
1148 rtx link, end_seq, r0, set;
1150 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
1152 /* Clear out the stale LOG_LINKS from flow. */
1153 free_INSN_LIST_list (&LOG_LINKS (insn));
1155 /* Make each JUMP_INSN a scheduling barrier for memory
1156 references. */
1157 if (GET_CODE (insn) == JUMP_INSN)
1159 /* Keep the list a reasonable size. */
1160 if (deps->pending_flush_length++ > MAX_PENDING_LIST_LENGTH)
1161 flush_pending_lists (deps, insn, true, true);
1162 else
1163 deps->last_pending_memory_flush
1164 = alloc_INSN_LIST (insn, deps->last_pending_memory_flush);
1166 sched_analyze_insn (deps, PATTERN (insn), insn, loop_notes);
1167 loop_notes = 0;
1169 else if (GET_CODE (insn) == CALL_INSN)
1171 int i;
1173 CANT_MOVE (insn) = 1;
1175 /* Clear out the stale LOG_LINKS from flow. */
1176 free_INSN_LIST_list (&LOG_LINKS (insn));
1178 if (find_reg_note (insn, REG_SETJMP, NULL))
1180 /* This is setjmp. Assume that all registers, not just
1181 hard registers, may be clobbered by this call. */
1182 reg_pending_barrier = MOVE_BARRIER;
1184 else
1186 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1187 /* A call may read and modify global register variables. */
1188 if (global_regs[i])
1190 SET_REGNO_REG_SET (reg_pending_sets, i);
1191 SET_REGNO_REG_SET (reg_pending_uses, i);
1193 /* Other call-clobbered hard regs may be clobbered.
1194 Since we only have a choice between 'might be clobbered'
1195 and 'definitely not clobbered', we must include all
1196 partly call-clobbered registers here. */
1197 else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
1198 || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1199 SET_REGNO_REG_SET (reg_pending_clobbers, i);
1200 /* We don't know what set of fixed registers might be used
1201 by the function, but it is certain that the stack pointer
1202 is among them, but be conservative. */
1203 else if (fixed_regs[i])
1204 SET_REGNO_REG_SET (reg_pending_uses, i);
1205 /* The frame pointer is normally not used by the function
1206 itself, but by the debugger. */
1207 /* ??? MIPS o32 is an exception. It uses the frame pointer
1208 in the macro expansion of jal but does not represent this
1209 fact in the call_insn rtl. */
1210 else if (i == FRAME_POINTER_REGNUM
1211 || (i == HARD_FRAME_POINTER_REGNUM
1212 && (! reload_completed || frame_pointer_needed)))
1213 SET_REGNO_REG_SET (reg_pending_uses, i);
1216 /* For each insn which shouldn't cross a call, add a dependence
1217 between that insn and this call insn. */
1218 add_dependence_list_and_free (insn, &deps->sched_before_next_call,
1219 REG_DEP_ANTI);
1221 sched_analyze_insn (deps, PATTERN (insn), insn, loop_notes);
1222 loop_notes = 0;
1224 /* In the absence of interprocedural alias analysis, we must flush
1225 all pending reads and writes, and start new dependencies starting
1226 from here. But only flush writes for constant calls (which may
1227 be passed a pointer to something we haven't written yet). */
1228 flush_pending_lists (deps, insn, true, !CONST_OR_PURE_CALL_P (insn));
1230 /* Remember the last function call for limiting lifetimes. */
1231 free_INSN_LIST_list (&deps->last_function_call);
1232 deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
1234 /* Before reload, begin a post-call group, so as to keep the
1235 lifetimes of hard registers correct. */
1236 if (! reload_completed)
1237 deps->in_post_call_group_p = true;
1240 /* See comments on reemit_notes as to why we do this.
1241 ??? Actually, the reemit_notes just say what is done, not why. */
1243 if (GET_CODE (insn) == NOTE
1244 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
1245 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
1246 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1247 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1249 rtx rtx_region;
1251 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1252 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
1253 rtx_region = GEN_INT (NOTE_EH_HANDLER (insn));
1254 else
1255 rtx_region = GEN_INT (0);
1257 loop_notes = alloc_EXPR_LIST (REG_SAVE_NOTE,
1258 rtx_region,
1259 loop_notes);
1260 loop_notes = alloc_EXPR_LIST (REG_SAVE_NOTE,
1261 GEN_INT (NOTE_LINE_NUMBER (insn)),
1262 loop_notes);
1263 CONST_OR_PURE_CALL_P (loop_notes) = CONST_OR_PURE_CALL_P (insn);
1266 if (current_sched_info->use_cselib)
1267 cselib_process_insn (insn);
1269 /* Now that we have completed handling INSN, check and see if it is
1270 a CLOBBER beginning a libcall block. If it is, record the
1271 end of the libcall sequence.
1273 We want to schedule libcall blocks as a unit before reload. While
1274 this restricts scheduling, it preserves the meaning of a libcall
1275 block.
1277 As a side effect, we may get better code due to decreased register
1278 pressure as well as less chance of a foreign insn appearing in
1279 a libcall block. */
1280 if (!reload_completed
1281 /* Note we may have nested libcall sequences. We only care about
1282 the outermost libcall sequence. */
1283 && deps->libcall_block_tail_insn == 0
1284 /* The sequence must start with a clobber of a register. */
1285 && GET_CODE (insn) == INSN
1286 && GET_CODE (PATTERN (insn)) == CLOBBER
1287 && (r0 = XEXP (PATTERN (insn), 0), GET_CODE (r0) == REG)
1288 && GET_CODE (XEXP (PATTERN (insn), 0)) == REG
1289 /* The CLOBBER must also have a REG_LIBCALL note attached. */
1290 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1291 && (end_seq = XEXP (link, 0)) != 0
1292 /* The insn referenced by the REG_LIBCALL note must be a
1293 simple nop copy with the same destination as the register
1294 mentioned in the clobber. */
1295 && (set = single_set (end_seq)) != 0
1296 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1297 /* And finally the insn referenced by the REG_LIBCALL must
1298 also contain a REG_EQUAL note and a REG_RETVAL note. */
1299 && find_reg_note (end_seq, REG_EQUAL, NULL_RTX) != 0
1300 && find_reg_note (end_seq, REG_RETVAL, NULL_RTX) != 0)
1301 deps->libcall_block_tail_insn = XEXP (link, 0);
1303 /* If we have reached the end of a libcall block, then close the
1304 block. */
1305 if (deps->libcall_block_tail_insn == insn)
1306 deps->libcall_block_tail_insn = 0;
1308 if (insn == tail)
1310 if (current_sched_info->use_cselib)
1311 cselib_finish ();
1312 return;
1315 abort ();
1319 /* The following function adds forward dependence (FROM, TO) with
1320 given DEP_TYPE. The forward dependence should be not exist before. */
1322 void
1323 add_forward_dependence (rtx from, rtx to, enum reg_note dep_type)
1325 rtx new_link;
1327 #ifdef ENABLE_CHECKING
1328 /* If add_dependence is working properly there should never
1329 be notes, deleted insns or duplicates in the backward
1330 links. Thus we need not check for them here.
1332 However, if we have enabled checking we might as well go
1333 ahead and verify that add_dependence worked properly. */
1334 if (GET_CODE (from) == NOTE
1335 || INSN_DELETED_P (from)
1336 || (forward_dependency_cache != NULL
1337 && TEST_BIT (forward_dependency_cache[INSN_LUID (from)],
1338 INSN_LUID (to)))
1339 || (forward_dependency_cache == NULL
1340 && find_insn_list (to, INSN_DEPEND (from))))
1341 abort ();
1342 if (forward_dependency_cache != NULL)
1343 SET_BIT (forward_dependency_cache[INSN_LUID (from)],
1344 INSN_LUID (to));
1345 #endif
1347 new_link = alloc_INSN_LIST (to, INSN_DEPEND (from));
1349 PUT_REG_NOTE_KIND (new_link, dep_type);
1351 INSN_DEPEND (from) = new_link;
1352 INSN_DEP_COUNT (to) += 1;
1355 /* Examine insns in the range [ HEAD, TAIL ] and Use the backward
1356 dependences from LOG_LINKS to build forward dependences in
1357 INSN_DEPEND. */
1359 void
1360 compute_forward_dependences (rtx head, rtx tail)
1362 rtx insn, link;
1363 rtx next_tail;
1365 next_tail = NEXT_INSN (tail);
1366 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1368 if (! INSN_P (insn))
1369 continue;
1371 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1372 add_forward_dependence (XEXP (link, 0), insn, REG_NOTE_KIND (link));
1376 /* Initialize variables for region data dependence analysis.
1377 n_bbs is the number of region blocks. */
1379 void
1380 init_deps (struct deps *deps)
1382 int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
1384 deps->max_reg = max_reg;
1385 deps->reg_last = (struct deps_reg *)
1386 xcalloc (max_reg, sizeof (struct deps_reg));
1387 INIT_REG_SET (&deps->reg_last_in_use);
1389 deps->pending_read_insns = 0;
1390 deps->pending_read_mems = 0;
1391 deps->pending_write_insns = 0;
1392 deps->pending_write_mems = 0;
1393 deps->pending_lists_length = 0;
1394 deps->pending_flush_length = 0;
1395 deps->last_pending_memory_flush = 0;
1396 deps->last_function_call = 0;
1397 deps->sched_before_next_call = 0;
1398 deps->in_post_call_group_p = false;
1399 deps->libcall_block_tail_insn = 0;
1402 /* Free insn lists found in DEPS. */
1404 void
1405 free_deps (struct deps *deps)
1407 int i;
1409 free_INSN_LIST_list (&deps->pending_read_insns);
1410 free_EXPR_LIST_list (&deps->pending_read_mems);
1411 free_INSN_LIST_list (&deps->pending_write_insns);
1412 free_EXPR_LIST_list (&deps->pending_write_mems);
1413 free_INSN_LIST_list (&deps->last_pending_memory_flush);
1415 /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
1416 times. For a test case with 42000 regs and 8000 small basic blocks,
1417 this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
1418 EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i,
1420 struct deps_reg *reg_last = &deps->reg_last[i];
1421 if (reg_last->uses)
1422 free_INSN_LIST_list (&reg_last->uses);
1423 if (reg_last->sets)
1424 free_INSN_LIST_list (&reg_last->sets);
1425 if (reg_last->clobbers)
1426 free_INSN_LIST_list (&reg_last->clobbers);
1428 CLEAR_REG_SET (&deps->reg_last_in_use);
1430 free (deps->reg_last);
1433 /* If it is profitable to use them, initialize caches for tracking
1434 dependency information. LUID is the number of insns to be scheduled,
1435 it is used in the estimate of profitability. */
1437 void
1438 init_dependency_caches (int luid)
1440 /* ?!? We could save some memory by computing a per-region luid mapping
1441 which could reduce both the number of vectors in the cache and the size
1442 of each vector. Instead we just avoid the cache entirely unless the
1443 average number of instructions in a basic block is very high. See
1444 the comment before the declaration of true_dependency_cache for
1445 what we consider "very high". */
1446 if (luid / n_basic_blocks > 100 * 5)
1448 true_dependency_cache = sbitmap_vector_alloc (luid, luid);
1449 sbitmap_vector_zero (true_dependency_cache, luid);
1450 anti_dependency_cache = sbitmap_vector_alloc (luid, luid);
1451 sbitmap_vector_zero (anti_dependency_cache, luid);
1452 output_dependency_cache = sbitmap_vector_alloc (luid, luid);
1453 sbitmap_vector_zero (output_dependency_cache, luid);
1454 #ifdef ENABLE_CHECKING
1455 forward_dependency_cache = sbitmap_vector_alloc (luid, luid);
1456 sbitmap_vector_zero (forward_dependency_cache, luid);
1457 #endif
1461 /* Free the caches allocated in init_dependency_caches. */
1463 void
1464 free_dependency_caches (void)
1466 if (true_dependency_cache)
1468 sbitmap_vector_free (true_dependency_cache);
1469 true_dependency_cache = NULL;
1470 sbitmap_vector_free (anti_dependency_cache);
1471 anti_dependency_cache = NULL;
1472 sbitmap_vector_free (output_dependency_cache);
1473 output_dependency_cache = NULL;
1474 #ifdef ENABLE_CHECKING
1475 sbitmap_vector_free (forward_dependency_cache);
1476 forward_dependency_cache = NULL;
1477 #endif
1481 /* Initialize some global variables needed by the dependency analysis
1482 code. */
1484 void
1485 init_deps_global (void)
1487 reg_pending_sets = INITIALIZE_REG_SET (reg_pending_sets_head);
1488 reg_pending_clobbers = INITIALIZE_REG_SET (reg_pending_clobbers_head);
1489 reg_pending_uses = INITIALIZE_REG_SET (reg_pending_uses_head);
1490 reg_pending_barrier = NOT_A_BARRIER;
1493 /* Free everything used by the dependency analysis code. */
1495 void
1496 finish_deps_global (void)
1498 FREE_REG_SET (reg_pending_sets);
1499 FREE_REG_SET (reg_pending_clobbers);
1500 FREE_REG_SET (reg_pending_uses);