1 /* Instruction scheduling pass.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4 Enhanced by, and currently maintained by, Jim Wilson (wilson@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 /* Instruction scheduling pass.
24 This pass implements list scheduling within basic blocks. It is
25 run after flow analysis, but before register allocation. The
26 scheduler works as follows:
28 We compute insn priorities based on data dependencies. Flow
29 analysis only creates a fraction of the data-dependencies we must
30 observe: namely, only those dependencies which the combiner can be
31 expected to use. For this pass, we must therefore create the
32 remaining dependencies we need to observe: register dependencies,
33 memory dependencies, dependencies to keep function calls in order,
34 and the dependence between a conditional branch and the setting of
35 condition codes are all dealt with here.
37 The scheduler first traverses the data flow graph, starting with
38 the last instruction, and proceeding to the first, assigning
39 values to insn_priority as it goes. This sorts the instructions
40 topologically by data dependence.
42 Once priorities have been established, we order the insns using
43 list scheduling. This works as follows: starting with a list of
44 all the ready insns, and sorted according to priority number, we
45 schedule the insn from the end of the list by placing its
46 predecessors in the list according to their priority order. We
47 consider this insn scheduled by setting the pointer to the "end" of
48 the list to point to the previous insn. When an insn has no
49 predecessors, we either queue it until sufficient time has elapsed
50 or add it to the ready list. As the instructions are scheduled or
51 when stalls are introduced, the queue advances and dumps insns into
52 the ready list. When all insns down to the lowest priority have
53 been scheduled, the critical path of the basic block has been made
54 as short as possible. The remaining insns are then scheduled in
57 Function unit conflicts are resolved during reverse list scheduling
58 by tracking the time when each insn is committed to the schedule
59 and from that, the time the function units it uses must be free.
60 As insns on the ready list are considered for scheduling, those
61 that would result in a blockage of the already committed insns are
62 queued until no blockage will result. Among the remaining insns on
63 the ready list to be considered, the first one with the largest
64 potential for causing a subsequent blockage is chosen.
66 The following list shows the order in which we want to break ties
67 among insns in the ready list:
69 1. choose insn with lowest conflict cost, ties broken by
70 2. choose insn with the longest path to end of bb, ties broken by
71 3. choose insn that kills the most registers, ties broken by
72 4. choose insn that conflicts with the most ready insns, or finally
73 5. choose insn with lowest UID.
75 Memory references complicate matters. Only if we can be certain
76 that memory references are not part of the data dependency graph
77 (via true, anti, or output dependence), can we move operations past
78 memory references. To first approximation, reads can be done
79 independently, while writes introduce dependencies. Better
80 approximations will yield fewer dependencies.
82 Dependencies set up by memory references are treated in exactly the
83 same way as other dependencies, by using LOG_LINKS.
85 Having optimized the critical path, we may have also unduly
86 extended the lifetimes of some registers. If an operation requires
87 that constants be loaded into registers, it is certainly desirable
88 to load those constants as early as necessary, but no earlier.
89 I.e., it will not do to load up a bunch of registers at the
90 beginning of a basic block only to use them at the end, if they
91 could be loaded later, since this may result in excessive register
94 Note that since branches are never in basic blocks, but only end
95 basic blocks, this pass will not do any branch scheduling. But
96 that is ok, since we can use GNU's delayed branch scheduling
97 pass to take care of this case.
99 Also note that no further optimizations based on algebraic identities
100 are performed, so this pass would be a good one to perform instruction
101 splitting, such as breaking up a multiply instruction into shifts
102 and adds where that is profitable.
104 Given the memory aliasing analysis that this pass should perform,
105 it should be possible to remove redundant stores to memory, and to
106 load values from registers instead of hitting memory.
108 This pass must update information that subsequent passes expect to be
109 correct. Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
110 reg_n_calls_crossed, and reg_live_length. Also, basic_block_head,
113 The information in the line number notes is carefully retained by this
114 pass. All other NOTE insns are grouped in their same relative order at
115 the beginning of basic blocks that have been scheduled. */
120 #include "basic-block.h"
122 #include "hard-reg-set.h"
124 #include "insn-config.h"
125 #include "insn-attr.h"
127 #ifdef INSN_SCHEDULING
128 /* Arrays set up by scheduling for the same respective purposes as
129 similar-named arrays set up by flow analysis. We work with these
130 arrays during the scheduling pass so we can compare values against
133 Values of these arrays are copied at the end of this pass into the
134 arrays set up by flow analysis. */
135 static short *sched_reg_n_deaths
;
136 static int *sched_reg_n_calls_crossed
;
137 static int *sched_reg_live_length
;
139 /* Element N is the next insn that sets (hard or pseudo) register
140 N within the current basic block; or zero, if there is no
141 such insn. Needed for new registers which may be introduced
142 by splitting insns. */
143 static rtx
*reg_last_uses
;
144 static rtx
*reg_last_sets
;
146 /* Vector indexed by INSN_UID giving the original ordering of the insns. */
147 static int *insn_luid
;
148 #define INSN_LUID(INSN) (insn_luid[INSN_UID (INSN)])
150 /* Vector indexed by INSN_UID giving each instruction a priority. */
151 static int *insn_priority
;
152 #define INSN_PRIORITY(INSN) (insn_priority[INSN_UID (INSN)])
154 static short *insn_costs
;
155 #define INSN_COST(INSN) insn_costs[INSN_UID (INSN)]
157 /* Vector indexed by INSN_UID giving an encoding of the function units
159 static short *insn_units
;
160 #define INSN_UNIT(INSN) insn_units[INSN_UID (INSN)]
162 /* Vector indexed by INSN_UID giving an encoding of the blockage range
163 function. The unit and the range are encoded. */
164 static unsigned int *insn_blockage
;
165 #define INSN_BLOCKAGE(INSN) insn_blockage[INSN_UID (INSN)]
167 #define BLOCKAGE_MASK ((1 << BLOCKAGE_BITS) - 1)
168 #define ENCODE_BLOCKAGE(U,R) \
169 ((((U) << UNIT_BITS) << BLOCKAGE_BITS \
170 | MIN_BLOCKAGE_COST (R)) << BLOCKAGE_BITS \
171 | MAX_BLOCKAGE_COST (R))
172 #define UNIT_BLOCKED(B) ((B) >> (2 * BLOCKAGE_BITS))
173 #define BLOCKAGE_RANGE(B) \
174 (((((B) >> BLOCKAGE_BITS) & BLOCKAGE_MASK) << (HOST_BITS_PER_INT / 2)) \
175 | (B) & BLOCKAGE_MASK)
177 /* Encodings of the `<name>_unit_blockage_range' function. */
178 #define MIN_BLOCKAGE_COST(R) ((R) >> (HOST_BITS_PER_INT / 2))
179 #define MAX_BLOCKAGE_COST(R) ((R) & ((1 << (HOST_BITS_PER_INT / 2)) - 1))
181 #define DONE_PRIORITY -1
182 #define MAX_PRIORITY 0x7fffffff
183 #define TAIL_PRIORITY 0x7ffffffe
184 #define LAUNCH_PRIORITY 0x7f000001
185 #define DONE_PRIORITY_P(INSN) (INSN_PRIORITY (INSN) < 0)
186 #define LOW_PRIORITY_P(INSN) ((INSN_PRIORITY (INSN) & 0x7f000000) == 0)
188 /* Vector indexed by INSN_UID giving number of insns referring to this insn. */
189 static int *insn_ref_count
;
190 #define INSN_REF_COUNT(INSN) (insn_ref_count[INSN_UID (INSN)])
192 /* Vector indexed by INSN_UID giving line-number note in effect for each
193 insn. For line-number notes, this indicates whether the note may be
195 static rtx
*line_note
;
196 #define LINE_NOTE(INSN) (line_note[INSN_UID (INSN)])
198 /* Vector indexed by basic block number giving the starting line-number
199 for each basic block. */
200 static rtx
*line_note_head
;
202 /* List of important notes we must keep around. This is a pointer to the
203 last element in the list. */
204 static rtx note_list
;
206 /* Regsets telling whether a given register is live or dead before the last
207 scheduled insn. Must scan the instructions once before scheduling to
208 determine what registers are live or dead at the end of the block. */
209 static regset bb_dead_regs
;
210 static regset bb_live_regs
;
212 /* Regset telling whether a given register is live after the insn currently
213 being scheduled. Before processing an insn, this is equal to bb_live_regs
214 above. This is used so that we can find registers that are newly born/dead
215 after processing an insn. */
216 static regset old_live_regs
;
218 /* The chain of REG_DEAD notes. REG_DEAD notes are removed from all insns
219 during the initial scan and reused later. If there are not exactly as
220 many REG_DEAD notes in the post scheduled code as there were in the
221 prescheduled code then we trigger an abort because this indicates a bug. */
222 static rtx dead_notes
;
226 /* An instruction is ready to be scheduled when all insns following it
227 have already been scheduled. It is important to ensure that all
228 insns which use its result will not be executed until its result
229 has been computed. An insn is maintained in one of four structures:
231 (P) the "Pending" set of insns which cannot be scheduled until
232 their dependencies have been satisfied.
233 (Q) the "Queued" set of insns that can be scheduled when sufficient
235 (R) the "Ready" list of unscheduled, uncommitted insns.
236 (S) the "Scheduled" list of insns.
238 Initially, all insns are either "Pending" or "Ready" depending on
239 whether their dependencies are satisfied.
241 Insns move from the "Ready" list to the "Scheduled" list as they
242 are committed to the schedule. As this occurs, the insns in the
243 "Pending" list have their dependencies satisfied and move to either
244 the "Ready" list or the "Queued" set depending on whether
245 sufficient time has passed to make them ready. As time passes,
246 insns move from the "Queued" set to the "Ready" list. Insns may
247 move from the "Ready" list to the "Queued" set if they are blocked
248 due to a function unit conflict.
250 The "Pending" list (P) are the insns in the LOG_LINKS of the unscheduled
251 insns, i.e., those that are ready, queued, and pending.
252 The "Queued" set (Q) is implemented by the variable `insn_queue'.
253 The "Ready" list (R) is implemented by the variables `ready' and
255 The "Scheduled" list (S) is the new insn chain built by this pass.
257 The transition (R->S) is implemented in the scheduling loop in
258 `schedule_block' when the best insn to schedule is chosen.
259 The transition (R->Q) is implemented in `schedule_select' when an
260 insn is found to to have a function unit conflict with the already
262 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
263 insns move from the ready list to the scheduled list.
264 The transition (Q->R) is implemented at the top of the scheduling
265 loop in `schedule_block' as time passes or stalls are introduced. */
267 /* Implement a circular buffer to delay instructions until sufficient
268 time has passed. INSN_QUEUE_SIZE is a power of two larger than
269 MAX_BLOCKAGE and MAX_READY_COST computed by genattr.c. This is the
270 longest time an isnsn may be queued. */
271 static rtx insn_queue
[INSN_QUEUE_SIZE
];
272 static int q_ptr
= 0;
273 static int q_size
= 0;
274 #define NEXT_Q(X) (((X)+1) & (INSN_QUEUE_SIZE-1))
275 #define NEXT_Q_AFTER(X,C) (((X)+C) & (INSN_QUEUE_SIZE-1))
277 /* Vector indexed by INSN_UID giving the minimum clock tick at which
278 the insn becomes ready. This is used to note timing constraints for
279 insns in the pending list. */
280 static int *insn_tick
;
281 #define INSN_TICK(INSN) (insn_tick[INSN_UID (INSN)])
283 /* Forward declarations. */
284 static void sched_analyze_2 ();
285 static void schedule_block ();
287 /* Main entry point of this file. */
288 void schedule_insns ();
289 #endif /* INSN_SCHEDULING */
291 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
293 /* Vector indexed by N giving the initial (unchanging) value known
294 for pseudo-register N. */
295 static rtx
*reg_known_value
;
297 /* Indicates number of valid entries in reg_known_value. */
298 static int reg_known_value_size
;
304 if (GET_CODE (x
) == REG
&& REGNO (x
) >= FIRST_PSEUDO_REGISTER
305 && REGNO (x
) <= reg_known_value_size
)
306 return reg_known_value
[REGNO (x
)];
307 else if (GET_CODE (x
) == PLUS
)
309 rtx x0
= canon_rtx (XEXP (x
, 0));
310 rtx x1
= canon_rtx (XEXP (x
, 1));
312 if (x0
!= XEXP (x
, 0) || x1
!= XEXP (x
, 1))
314 /* We can tolerate LO_SUMs being offset here; these
315 rtl are used for nothing other than comparisons. */
316 if (GET_CODE (x0
) == CONST_INT
)
317 return plus_constant_for_output (x1
, INTVAL (x0
));
318 else if (GET_CODE (x1
) == CONST_INT
)
319 return plus_constant_for_output (x0
, INTVAL (x1
));
320 return gen_rtx (PLUS
, GET_MODE (x
), x0
, x1
);
326 /* Set up all info needed to perform alias analysis on memory references. */
329 init_alias_analysis ()
331 int maxreg
= max_reg_num ();
336 reg_known_value_size
= maxreg
;
339 = (rtx
*) oballoc ((maxreg
-FIRST_PSEUDO_REGISTER
) * sizeof (rtx
))
340 - FIRST_PSEUDO_REGISTER
;
341 bzero (reg_known_value
+FIRST_PSEUDO_REGISTER
,
342 (maxreg
-FIRST_PSEUDO_REGISTER
) * sizeof (rtx
));
344 /* Fill in the entries with known constant values. */
345 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
346 if ((set
= single_set (insn
)) != 0
347 && GET_CODE (SET_DEST (set
)) == REG
348 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
349 && (((note
= find_reg_note (insn
, REG_EQUAL
, 0)) != 0
350 && reg_n_sets
[REGNO (SET_DEST (set
))] == 1)
351 || (note
= find_reg_note (insn
, REG_EQUIV
, NULL_RTX
)) != 0)
352 && GET_CODE (XEXP (note
, 0)) != EXPR_LIST
)
353 reg_known_value
[REGNO (SET_DEST (set
))] = XEXP (note
, 0);
355 /* Fill in the remaining entries. */
356 while (--maxreg
>= FIRST_PSEUDO_REGISTER
)
357 if (reg_known_value
[maxreg
] == 0)
358 reg_known_value
[maxreg
] = regno_reg_rtx
[maxreg
];
361 /* Return 1 if X and Y are identical-looking rtx's.
363 We use the data in reg_known_value above to see if two registers with
364 different numbers are, in fact, equivalent. */
367 rtx_equal_for_memref_p (x
, y
)
372 register enum rtx_code code
;
375 if (x
== 0 && y
== 0)
377 if (x
== 0 || y
== 0)
386 /* Rtx's of different codes cannot be equal. */
387 if (code
!= GET_CODE (y
))
390 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
391 (REG:SI x) and (REG:HI x) are NOT equivalent. */
393 if (GET_MODE (x
) != GET_MODE (y
))
396 /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively. */
399 return REGNO (x
) == REGNO (y
);
400 if (code
== LABEL_REF
)
401 return XEXP (x
, 0) == XEXP (y
, 0);
402 if (code
== SYMBOL_REF
)
403 return XSTR (x
, 0) == XSTR (y
, 0);
405 /* Compare the elements. If any pair of corresponding elements
406 fail to match, return 0 for the whole things. */
408 fmt
= GET_RTX_FORMAT (code
);
409 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
414 if (XWINT (x
, i
) != XWINT (y
, i
))
420 if (XINT (x
, i
) != XINT (y
, i
))
426 /* Two vectors must have the same length. */
427 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
430 /* And the corresponding elements must match. */
431 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
432 if (rtx_equal_for_memref_p (XVECEXP (x
, i
, j
), XVECEXP (y
, i
, j
)) == 0)
437 if (rtx_equal_for_memref_p (XEXP (x
, i
), XEXP (y
, i
)) == 0)
443 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
448 /* These are just backpointers, so they don't matter. */
454 /* It is believed that rtx's at this level will never
455 contain anything but integers and other rtx's,
456 except for within LABEL_REFs and SYMBOL_REFs. */
464 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
465 X and return it, or return 0 if none found. */
468 find_symbolic_term (x
)
472 register enum rtx_code code
;
476 if (code
== SYMBOL_REF
|| code
== LABEL_REF
)
478 if (GET_RTX_CLASS (code
) == 'o')
481 fmt
= GET_RTX_FORMAT (code
);
482 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
488 t
= find_symbolic_term (XEXP (x
, i
));
492 else if (fmt
[i
] == 'E')
498 /* Return nonzero if X and Y (memory addresses) could reference the
499 same location in memory. C is an offset accumulator. When
500 C is nonzero, we are testing aliases between X and Y + C.
501 XSIZE is the size in bytes of the X reference,
502 similarly YSIZE is the size in bytes for Y.
504 If XSIZE or YSIZE is zero, we do not know the amount of memory being
505 referenced (the reference was BLKmode), so make the most pessimistic
508 We recognize the following cases of non-conflicting memory:
510 (1) addresses involving the frame pointer cannot conflict
511 with addresses involving static variables.
512 (2) static variables with different addresses cannot conflict.
514 Nice to notice that varying addresses cannot conflict with fp if no
515 local variables had their addresses taken, but that's too hard now. */
518 memrefs_conflict_p (xsize
, x
, ysize
, y
, c
)
523 if (GET_CODE (x
) == HIGH
)
525 else if (GET_CODE (x
) == LO_SUM
)
529 if (GET_CODE (y
) == HIGH
)
531 else if (GET_CODE (y
) == LO_SUM
)
536 if (rtx_equal_for_memref_p (x
, y
))
537 return (xsize
== 0 || ysize
== 0 ||
538 (c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0));
540 if (y
== frame_pointer_rtx
|| y
== stack_pointer_rtx
)
544 y
= x
; ysize
= xsize
;
545 x
= t
; xsize
= tsize
;
548 if (x
== frame_pointer_rtx
|| x
== stack_pointer_rtx
)
555 if (GET_CODE (y
) == PLUS
556 && canon_rtx (XEXP (y
, 0)) == x
557 && (y1
= canon_rtx (XEXP (y
, 1)))
558 && GET_CODE (y1
) == CONST_INT
)
561 return (xsize
== 0 || ysize
== 0
562 || (c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0));
565 if (GET_CODE (y
) == PLUS
566 && (y1
= canon_rtx (XEXP (y
, 0)))
573 if (GET_CODE (x
) == PLUS
)
575 /* The fact that X is canonicalized means that this
576 PLUS rtx is canonicalized. */
577 rtx x0
= XEXP (x
, 0);
578 rtx x1
= XEXP (x
, 1);
580 if (GET_CODE (y
) == PLUS
)
582 /* The fact that Y is canonicalized means that this
583 PLUS rtx is canonicalized. */
584 rtx y0
= XEXP (y
, 0);
585 rtx y1
= XEXP (y
, 1);
587 if (rtx_equal_for_memref_p (x1
, y1
))
588 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
, c
);
589 if (rtx_equal_for_memref_p (x0
, y0
))
590 return memrefs_conflict_p (xsize
, x1
, ysize
, y1
, c
);
591 if (GET_CODE (x1
) == CONST_INT
)
592 if (GET_CODE (y1
) == CONST_INT
)
593 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
,
594 c
- INTVAL (x1
) + INTVAL (y1
));
596 return memrefs_conflict_p (xsize
, x0
, ysize
, y
, c
- INTVAL (x1
));
597 else if (GET_CODE (y1
) == CONST_INT
)
598 return memrefs_conflict_p (xsize
, x
, ysize
, y0
, c
+ INTVAL (y1
));
600 /* Handle case where we cannot understand iteration operators,
601 but we notice that the base addresses are distinct objects. */
602 x
= find_symbolic_term (x
);
605 y
= find_symbolic_term (y
);
608 return rtx_equal_for_memref_p (x
, y
);
610 else if (GET_CODE (x1
) == CONST_INT
)
611 return memrefs_conflict_p (xsize
, x0
, ysize
, y
, c
- INTVAL (x1
));
613 else if (GET_CODE (y
) == PLUS
)
615 /* The fact that Y is canonicalized means that this
616 PLUS rtx is canonicalized. */
617 rtx y0
= XEXP (y
, 0);
618 rtx y1
= XEXP (y
, 1);
620 if (GET_CODE (y1
) == CONST_INT
)
621 return memrefs_conflict_p (xsize
, x
, ysize
, y0
, c
+ INTVAL (y1
));
626 if (GET_CODE (x
) == GET_CODE (y
))
627 switch (GET_CODE (x
))
631 /* Handle cases where we expect the second operands to be the
632 same, and check only whether the first operand would conflict
635 rtx x1
= canon_rtx (XEXP (x
, 1));
636 rtx y1
= canon_rtx (XEXP (y
, 1));
637 if (! rtx_equal_for_memref_p (x1
, y1
))
639 x0
= canon_rtx (XEXP (x
, 0));
640 y0
= canon_rtx (XEXP (y
, 0));
641 if (rtx_equal_for_memref_p (x0
, y0
))
642 return (xsize
== 0 || ysize
== 0
643 || (c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0));
645 /* Can't properly adjust our sizes. */
646 if (GET_CODE (x1
) != CONST_INT
)
648 xsize
/= INTVAL (x1
);
649 ysize
/= INTVAL (x1
);
651 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
, c
);
657 if (GET_CODE (x
) == CONST_INT
&& GET_CODE (y
) == CONST_INT
)
659 c
+= (INTVAL (y
) - INTVAL (x
));
660 return (xsize
== 0 || ysize
== 0
661 || (c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0));
664 if (GET_CODE (x
) == CONST
)
666 if (GET_CODE (y
) == CONST
)
667 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
668 ysize
, canon_rtx (XEXP (y
, 0)), c
);
670 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
673 if (GET_CODE (y
) == CONST
)
674 return memrefs_conflict_p (xsize
, x
, ysize
,
675 canon_rtx (XEXP (y
, 0)), c
);
678 return (rtx_equal_for_memref_p (x
, y
)
679 && (xsize
== 0 || ysize
== 0
680 || (c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0)));
687 /* Functions to compute memory dependencies.
689 Since we process the insns in execution order, we can build tables
690 to keep track of what registers are fixed (and not aliased), what registers
691 are varying in known ways, and what registers are varying in unknown
694 If both memory references are volatile, then there must always be a
695 dependence between the two references, since their order can not be
696 changed. A volatile and non-volatile reference can be interchanged
699 A MEM_IN_STRUCT reference at a varying address can never conflict with a
700 non-MEM_IN_STRUCT reference at a fixed address. */
702 /* Read dependence: X is read after read in MEM takes place. There can
703 only be a dependence here if both reads are volatile. */
706 read_dependence (mem
, x
)
710 return MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
);
713 /* True dependence: X is read after store in MEM takes place. */
716 true_dependence (mem
, x
)
720 /* If X is an unchanging read, then it can't possibly conflict with any
721 non-unchanging store. It may conflict with an unchanging write though,
722 because there may be a single store to this address to initialize it.
723 Just fall through to the code below to resolve the case where we have
724 both an unchanging read and an unchanging write. This won't handle all
725 cases optimally, but the possible performance loss should be
727 if (RTX_UNCHANGING_P (x
) && ! RTX_UNCHANGING_P (mem
))
730 return ((MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
731 || (memrefs_conflict_p (SIZE_FOR_MODE (mem
), XEXP (mem
, 0),
732 SIZE_FOR_MODE (x
), XEXP (x
, 0), 0)
733 && ! (MEM_IN_STRUCT_P (mem
) && rtx_addr_varies_p (mem
)
734 && ! MEM_IN_STRUCT_P (x
) && ! rtx_addr_varies_p (x
))
735 && ! (MEM_IN_STRUCT_P (x
) && rtx_addr_varies_p (x
)
736 && ! MEM_IN_STRUCT_P (mem
) && ! rtx_addr_varies_p (mem
))));
739 /* Anti dependence: X is written after read in MEM takes place. */
742 anti_dependence (mem
, x
)
746 /* If MEM is an unchanging read, then it can't possibly conflict with
747 the store to X, because there is at most one store to MEM, and it must
748 have occured somewhere before MEM. */
749 if (RTX_UNCHANGING_P (mem
))
752 return ((MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
753 || (memrefs_conflict_p (SIZE_FOR_MODE (mem
), XEXP (mem
, 0),
754 SIZE_FOR_MODE (x
), XEXP (x
, 0), 0)
755 && ! (MEM_IN_STRUCT_P (mem
) && rtx_addr_varies_p (mem
)
756 && ! MEM_IN_STRUCT_P (x
) && ! rtx_addr_varies_p (x
))
757 && ! (MEM_IN_STRUCT_P (x
) && rtx_addr_varies_p (x
)
758 && ! MEM_IN_STRUCT_P (mem
) && ! rtx_addr_varies_p (mem
))));
761 /* Output dependence: X is written after store in MEM takes place. */
764 output_dependence (mem
, x
)
768 return ((MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
769 || (memrefs_conflict_p (SIZE_FOR_MODE (mem
), XEXP (mem
, 0),
770 SIZE_FOR_MODE (x
), XEXP (x
, 0), 0)
771 && ! (MEM_IN_STRUCT_P (mem
) && rtx_addr_varies_p (mem
)
772 && ! MEM_IN_STRUCT_P (x
) && ! rtx_addr_varies_p (x
))
773 && ! (MEM_IN_STRUCT_P (x
) && rtx_addr_varies_p (x
)
774 && ! MEM_IN_STRUCT_P (mem
) && ! rtx_addr_varies_p (mem
))));
777 /* Helper functions for instruction scheduling. */
779 /* Add ELEM wrapped in an INSN_LIST with reg note kind DEP_TYPE to the
780 LOG_LINKS of INSN, if not already there. DEP_TYPE indicates the type
781 of dependence that this link represents. */
784 add_dependence (insn
, elem
, dep_type
)
787 enum reg_note dep_type
;
791 /* Don't depend an insn on itself. */
795 /* If elem is part of a sequence that must be scheduled together, then
796 make the dependence point to the last insn of the sequence.
797 When HAVE_cc0, it is possible for NOTEs to exist between users and
798 setters of the condition codes, so we must skip past notes here.
799 Otherwise, NOTEs are impossible here. */
801 next
= NEXT_INSN (elem
);
804 while (next
&& GET_CODE (next
) == NOTE
)
805 next
= NEXT_INSN (next
);
808 if (next
&& SCHED_GROUP_P (next
))
810 /* Notes will never intervene here though, so don't bother checking
812 while (NEXT_INSN (next
) && SCHED_GROUP_P (NEXT_INSN (next
)))
813 next
= NEXT_INSN (next
);
815 /* Again, don't depend an insn on itself. */
819 /* Make the dependence to NEXT, the last insn of the group, instead
820 of the original ELEM. */
824 /* Check that we don't already have this dependence. */
825 for (link
= LOG_LINKS (insn
); link
; link
= XEXP (link
, 1))
826 if (XEXP (link
, 0) == elem
)
828 /* If this is a more restrictive type of dependence than the existing
829 one, then change the existing dependence to this type. */
830 if ((int) dep_type
< (int) REG_NOTE_KIND (link
))
831 PUT_REG_NOTE_KIND (link
, dep_type
);
834 /* Might want to check one level of transitivity to save conses. */
836 link
= rtx_alloc (INSN_LIST
);
837 /* Insn dependency, not data dependency. */
838 PUT_REG_NOTE_KIND (link
, dep_type
);
839 XEXP (link
, 0) = elem
;
840 XEXP (link
, 1) = LOG_LINKS (insn
);
841 LOG_LINKS (insn
) = link
;
844 /* Remove ELEM wrapped in an INSN_LIST from the LOG_LINKS
845 of INSN. Abort if not found. */
847 remove_dependence (insn
, elem
)
854 for (prev
= 0, link
= LOG_LINKS (insn
); link
;
855 prev
= link
, link
= XEXP (link
, 1))
857 if (XEXP (link
, 0) == elem
)
860 XEXP (prev
, 1) = XEXP (link
, 1);
862 LOG_LINKS (insn
) = XEXP (link
, 1);
872 #ifndef INSN_SCHEDULING
873 void schedule_insns () {}
879 /* Computation of memory dependencies. */
881 /* The *_insns and *_mems are paired lists. Each pending memory operation
882 will have a pointer to the MEM rtx on one list and a pointer to the
883 containing insn on the other list in the same place in the list. */
885 /* We can't use add_dependence like the old code did, because a single insn
886 may have multiple memory accesses, and hence needs to be on the list
887 once for each memory access. Add_dependence won't let you add an insn
888 to a list more than once. */
890 /* An INSN_LIST containing all insns with pending read operations. */
891 static rtx pending_read_insns
;
893 /* An EXPR_LIST containing all MEM rtx's which are pending reads. */
894 static rtx pending_read_mems
;
896 /* An INSN_LIST containing all insns with pending write operations. */
897 static rtx pending_write_insns
;
899 /* An EXPR_LIST containing all MEM rtx's which are pending writes. */
900 static rtx pending_write_mems
;
902 /* Indicates the combined length of the two pending lists. We must prevent
903 these lists from ever growing too large since the number of dependencies
904 produced is at least O(N*N), and execution time is at least O(4*N*N), as
905 a function of the length of these pending lists. */
907 static int pending_lists_length
;
909 /* An INSN_LIST containing all INSN_LISTs allocated but currently unused. */
911 static rtx unused_insn_list
;
913 /* An EXPR_LIST containing all EXPR_LISTs allocated but currently unused. */
915 static rtx unused_expr_list
;
917 /* The last insn upon which all memory references must depend.
918 This is an insn which flushed the pending lists, creating a dependency
919 between it and all previously pending memory references. This creates
920 a barrier (or a checkpoint) which no memory reference is allowed to cross.
922 This includes all non constant CALL_INSNs. When we do interprocedural
923 alias analysis, this restriction can be relaxed.
924 This may also be an INSN that writes memory if the pending lists grow
927 static rtx last_pending_memory_flush
;
929 /* The last function call we have seen. All hard regs, and, of course,
930 the last function call, must depend on this. */
932 static rtx last_function_call
;
934 /* The LOG_LINKS field of this is a list of insns which use a pseudo register
935 that does not already cross a call. We create dependencies between each
936 of those insn and the next call insn, to ensure that they won't cross a call
937 after scheduling is done. */
939 static rtx sched_before_next_call
;
941 /* Pointer to the last instruction scheduled. Used by rank_for_schedule,
942 so that insns independent of the last scheduled insn will be preferred
943 over dependent instructions. */
945 static rtx last_scheduled_insn
;
947 /* Process an insn's memory dependencies. There are four kinds of
950 (0) read dependence: read follows read
951 (1) true dependence: read follows write
952 (2) anti dependence: write follows read
953 (3) output dependence: write follows write
955 We are careful to build only dependencies which actually exist, and
956 use transitivity to avoid building too many links. */
958 /* Return the INSN_LIST containing INSN in LIST, or NULL
959 if LIST does not contain INSN. */
962 find_insn_list (insn
, list
)
968 if (XEXP (list
, 0) == insn
)
970 list
= XEXP (list
, 1);
975 /* Compute the function units used by INSN. This caches the value
976 returned by function_units_used. A function unit is encoded as the
977 unit number if the value is non-negative and the compliment of a
978 mask if the value is negative. A function unit index is the
979 non-negative encoding. */
985 register int unit
= INSN_UNIT (insn
);
989 recog_memoized (insn
);
991 /* A USE insn, or something else we don't need to understand.
992 We can't pass these directly to function_units_used because it will
993 trigger a fatal error for unrecognizable insns. */
994 if (INSN_CODE (insn
) < 0)
998 unit
= function_units_used (insn
);
999 /* Increment non-negative values so we can cache zero. */
1000 if (unit
>= 0) unit
++;
1002 /* We only cache 16 bits of the result, so if the value is out of
1003 range, don't cache it. */
1004 if (FUNCTION_UNITS_SIZE
< HOST_BITS_PER_SHORT
1006 || (~unit
& ((1 << (HOST_BITS_PER_SHORT
- 1)) - 1)) == 0)
1007 INSN_UNIT (insn
) = unit
;
1009 return (unit
> 0 ? unit
- 1 : unit
);
1012 /* Compute the blockage range for executing INSN on UNIT. This caches
1013 the value returned by the blockage_range_function for the unit.
1014 These values are encoded in an int where the upper half gives the
1015 minimum value and the lower half gives the maximum value. */
1017 __inline
static unsigned int
1018 blockage_range (unit
, insn
)
1022 unsigned int blockage
= INSN_BLOCKAGE (insn
);
1025 if (UNIT_BLOCKED (blockage
) != unit
+ 1)
1027 range
= function_units
[unit
].blockage_range_function (insn
);
1028 /* We only cache the blockage range for one unit and then only if
1030 if (HOST_BITS_PER_INT
>= UNIT_BITS
+ 2 * BLOCKAGE_BITS
)
1031 INSN_BLOCKAGE (insn
) = ENCODE_BLOCKAGE (unit
+ 1, range
);
1034 range
= BLOCKAGE_RANGE (blockage
);
1039 /* A vector indexed by function unit instance giving the last insn to use
1040 the unit. The value of the function unit instance index for unit U
1041 instance I is (U + I * FUNCTION_UNITS_SIZE). */
1042 static rtx unit_last_insn
[FUNCTION_UNITS_SIZE
* MAX_MULTIPLICITY
];
1044 /* A vector indexed by function unit instance giving the minimum time when
1045 the unit will unblock based on the maximum blockage cost. */
1046 static int unit_tick
[FUNCTION_UNITS_SIZE
* MAX_MULTIPLICITY
];
1048 /* A vector indexed by function unit number giving the number of insns
1049 that remain to use the unit. */
1050 static int unit_n_insns
[FUNCTION_UNITS_SIZE
];
1052 /* Reset the function unit state to the null state. */
1059 bzero (unit_last_insn
, sizeof (unit_last_insn
));
1060 bzero (unit_tick
, sizeof (unit_tick
));
1061 bzero (unit_n_insns
, sizeof (unit_n_insns
));
1064 /* Record an insn as one that will use the units encoded by UNIT. */
1066 __inline
static void
1073 unit_n_insns
[unit
]++;
1075 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
1076 if ((unit
& 1) != 0)
1080 /* Return the actual hazard cost of executing INSN on the unit UNIT,
1081 instance INSTANCE at time CLOCK if the previous actual hazard cost
1085 actual_hazard_this_instance (unit
, instance
, insn
, clock
, cost
)
1086 int unit
, instance
, clock
, cost
;
1090 int tick
= unit_tick
[instance
];
1092 if (tick
- clock
> cost
)
1094 /* The scheduler is operating in reverse, so INSN is the executing
1095 insn and the unit's last insn is the candidate insn. We want a
1096 more exact measure of the blockage if we execute INSN at CLOCK
1097 given when we committed the execution of the unit's last insn.
1099 The blockage value is given by either the unit's max blockage
1100 constant, blockage range function, or blockage function. Use
1101 the most exact form for the given unit. */
1103 if (function_units
[unit
].blockage_range_function
)
1105 if (function_units
[unit
].blockage_function
)
1106 tick
+= (function_units
[unit
].blockage_function
1107 (insn
, unit_last_insn
[instance
])
1108 - function_units
[unit
].max_blockage
);
1110 tick
+= ((int) MAX_BLOCKAGE_COST (blockage_range (unit
, insn
))
1111 - function_units
[unit
].max_blockage
);
1113 if (tick
- clock
> cost
)
1114 cost
= tick
- clock
;
1119 /* Record INSN as having begun execution on the units encoded by UNIT at
1122 __inline
static void
1123 schedule_unit (unit
, insn
, clock
)
1131 int instance
= unit
;
1132 #if MAX_MULTIPLICITY > 1
1133 /* Find the first free instance of the function unit and use that
1134 one. We assume that one is free. */
1135 for (i
= function_units
[unit
].multiplicity
- 1; i
> 0; i
--)
1137 if (! actual_hazard_this_instance (unit
, instance
, insn
, clock
, 0))
1139 instance
+= FUNCTION_UNITS_SIZE
;
1142 unit_last_insn
[instance
] = insn
;
1143 unit_tick
[instance
] = (clock
+ function_units
[unit
].max_blockage
);
1146 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
1147 if ((unit
& 1) != 0)
1148 schedule_unit (i
, insn
, clock
);
1151 /* Return the actual hazard cost of executing INSN on the units encoded by
1152 UNIT at time CLOCK if the previous actual hazard cost was COST. */
1155 actual_hazard (unit
, insn
, clock
, cost
)
1156 int unit
, clock
, cost
;
1163 /* Find the instance of the function unit with the minimum hazard. */
1164 int instance
= unit
;
1165 int best
= instance
;
1166 int best_cost
= actual_hazard_this_instance (unit
, instance
, insn
,
1170 #if MAX_MULTIPLICITY > 1
1171 if (best_cost
> cost
)
1173 for (i
= function_units
[unit
].multiplicity
- 1; i
> 0; i
--)
1175 instance
+= FUNCTION_UNITS_SIZE
;
1176 this_cost
= actual_hazard_this_instance (unit
, instance
, insn
,
1178 if (this_cost
< best_cost
)
1181 best_cost
= this_cost
;
1182 if (this_cost
<= cost
)
1188 cost
= MAX (cost
, best_cost
);
1191 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
1192 if ((unit
& 1) != 0)
1193 cost
= actual_hazard (i
, insn
, clock
, cost
);
1198 /* Return the potential hazard cost of executing an instruction on the
1199 units encoded by UNIT if the previous potential hazard cost was COST.
1200 An insn with a large blockage time is chosen in preference to one
1201 with a smaller time; an insn that uses a unit that is more likely
1202 to be used is chosen in preference to one with a unit that is less
1203 used. We are trying to minimize a subsequent actual hazard. */
1206 potential_hazard (unit
, insn
, cost
)
1211 unsigned int minb
, maxb
;
1215 minb
= maxb
= function_units
[unit
].max_blockage
;
1218 if (function_units
[unit
].blockage_range_function
)
1220 maxb
= minb
= blockage_range (unit
, insn
);
1221 maxb
= MAX_BLOCKAGE_COST (maxb
);
1222 minb
= MIN_BLOCKAGE_COST (minb
);
1227 /* Make the number of instructions left dominate. Make the
1228 minimum delay dominate the maximum delay. If all these
1229 are the same, use the unit number to add an arbitrary
1230 ordering. Other terms can be added. */
1231 ncost
= minb
* 0x40 + maxb
;
1232 ncost
*= (unit_n_insns
[unit
] - 1) * 0x1000 + unit
;
1239 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
1240 if ((unit
& 1) != 0)
1241 cost
= potential_hazard (i
, insn
, cost
);
1246 /* Compute cost of executing INSN given the dependence LINK on the insn USED.
1247 This is the number of virtual cycles taken between instruction issue and
1248 instruction results. */
1251 insn_cost (insn
, link
, used
)
1252 rtx insn
, link
, used
;
1254 register int cost
= INSN_COST (insn
);
1258 recog_memoized (insn
);
1260 /* A USE insn, or something else we don't need to understand.
1261 We can't pass these directly to result_ready_cost because it will
1262 trigger a fatal error for unrecognizable insns. */
1263 if (INSN_CODE (insn
) < 0)
1265 INSN_COST (insn
) = 1;
1270 cost
= result_ready_cost (insn
);
1275 INSN_COST (insn
) = cost
;
1279 /* A USE insn should never require the value used to be computed. This
1280 allows the computation of a function's result and parameter values to
1281 overlap the return and call. */
1282 recog_memoized (used
);
1283 if (INSN_CODE (used
) < 0)
1284 LINK_COST_FREE (link
) = 1;
1286 /* If some dependencies vary the cost, compute the adjustment. Most
1287 commonly, the adjustment is complete: either the cost is ignored
1288 (in the case of an output- or anti-dependence), or the cost is
1289 unchanged. These values are cached in the link as LINK_COST_FREE
1290 and LINK_COST_ZERO. */
1292 if (LINK_COST_FREE (link
))
1295 else if (! LINK_COST_ZERO (link
))
1299 ADJUST_COST (used
, link
, insn
, ncost
);
1301 LINK_COST_FREE (link
) = ncost
= 1;
1303 LINK_COST_ZERO (link
) = 1;
1310 /* Compute the priority number for INSN. */
1316 if (insn
&& GET_RTX_CLASS (GET_CODE (insn
)) == 'i')
1320 int this_priority
= INSN_PRIORITY (insn
);
1323 if (this_priority
> 0)
1324 return this_priority
;
1328 /* Nonzero if these insns must be scheduled together. */
1329 if (SCHED_GROUP_P (insn
))
1332 while (SCHED_GROUP_P (prev
))
1334 prev
= PREV_INSN (prev
);
1335 INSN_REF_COUNT (prev
) += 1;
1339 for (prev
= LOG_LINKS (insn
); prev
; prev
= XEXP (prev
, 1))
1341 rtx x
= XEXP (prev
, 0);
1343 /* A dependence pointing to a note is always obsolete, because
1344 sched_analyze_insn will have created any necessary new dependences
1345 which replace it. Notes can be created when instructions are
1346 deleted by insn splitting, or by register allocation. */
1347 if (GET_CODE (x
) == NOTE
)
1349 remove_dependence (insn
, x
);
1353 /* Clear the link cost adjustment bits. */
1354 LINK_COST_FREE (prev
) = 0;
1356 LINK_COST_ZERO (prev
) = 0;
1359 /* This priority calculation was chosen because it results in the
1360 least instruction movement, and does not hurt the performance
1361 of the resulting code compared to the old algorithm.
1362 This makes the sched algorithm more stable, which results
1363 in better code, because there is less register pressure,
1364 cross jumping is more likely to work, and debugging is easier.
1366 When all instructions have a latency of 1, there is no need to
1367 move any instructions. Subtracting one here ensures that in such
1368 cases all instructions will end up with a priority of one, and
1369 hence no scheduling will be done.
1371 The original code did not subtract the one, and added the
1372 insn_cost of the current instruction to its priority (e.g.
1373 move the insn_cost call down to the end). */
1375 if (REG_NOTE_KIND (prev
) == 0)
1376 /* Data dependence. */
1377 prev_priority
= priority (x
) + insn_cost (x
, prev
, insn
) - 1;
1379 /* Anti or output dependence. Don't add the latency of this
1380 insn's result, because it isn't being used. */
1381 prev_priority
= priority (x
);
1383 if (prev_priority
> max_priority
)
1384 max_priority
= prev_priority
;
1385 INSN_REF_COUNT (x
) += 1;
1388 prepare_unit (insn_unit (insn
));
1389 INSN_PRIORITY (insn
) = max_priority
;
1390 return INSN_PRIORITY (insn
);
1395 /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
1396 them to the unused_*_list variables, so that they can be reused. */
1399 free_pending_lists ()
1401 register rtx link
, prev_link
;
1403 if (pending_read_insns
)
1405 prev_link
= pending_read_insns
;
1406 link
= XEXP (prev_link
, 1);
1411 link
= XEXP (link
, 1);
1414 XEXP (prev_link
, 1) = unused_insn_list
;
1415 unused_insn_list
= pending_read_insns
;
1416 pending_read_insns
= 0;
1419 if (pending_write_insns
)
1421 prev_link
= pending_write_insns
;
1422 link
= XEXP (prev_link
, 1);
1427 link
= XEXP (link
, 1);
1430 XEXP (prev_link
, 1) = unused_insn_list
;
1431 unused_insn_list
= pending_write_insns
;
1432 pending_write_insns
= 0;
1435 if (pending_read_mems
)
1437 prev_link
= pending_read_mems
;
1438 link
= XEXP (prev_link
, 1);
1443 link
= XEXP (link
, 1);
1446 XEXP (prev_link
, 1) = unused_expr_list
;
1447 unused_expr_list
= pending_read_mems
;
1448 pending_read_mems
= 0;
1451 if (pending_write_mems
)
1453 prev_link
= pending_write_mems
;
1454 link
= XEXP (prev_link
, 1);
1459 link
= XEXP (link
, 1);
1462 XEXP (prev_link
, 1) = unused_expr_list
;
1463 unused_expr_list
= pending_write_mems
;
1464 pending_write_mems
= 0;
1468 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1469 The MEM is a memory reference contained within INSN, which we are saving
1470 so that we can do memory aliasing on it. */
1473 add_insn_mem_dependence (insn_list
, mem_list
, insn
, mem
)
1474 rtx
*insn_list
, *mem_list
, insn
, mem
;
1478 if (unused_insn_list
)
1480 link
= unused_insn_list
;
1481 unused_insn_list
= XEXP (link
, 1);
1484 link
= rtx_alloc (INSN_LIST
);
1485 XEXP (link
, 0) = insn
;
1486 XEXP (link
, 1) = *insn_list
;
1489 if (unused_expr_list
)
1491 link
= unused_expr_list
;
1492 unused_expr_list
= XEXP (link
, 1);
1495 link
= rtx_alloc (EXPR_LIST
);
1496 XEXP (link
, 0) = mem
;
1497 XEXP (link
, 1) = *mem_list
;
1500 pending_lists_length
++;
1503 /* Make a dependency between every memory reference on the pending lists
1504 and INSN, thus flushing the pending lists. */
1507 flush_pending_lists (insn
)
1512 while (pending_read_insns
)
1514 add_dependence (insn
, XEXP (pending_read_insns
, 0), REG_DEP_ANTI
);
1516 link
= pending_read_insns
;
1517 pending_read_insns
= XEXP (pending_read_insns
, 1);
1518 XEXP (link
, 1) = unused_insn_list
;
1519 unused_insn_list
= link
;
1521 link
= pending_read_mems
;
1522 pending_read_mems
= XEXP (pending_read_mems
, 1);
1523 XEXP (link
, 1) = unused_expr_list
;
1524 unused_expr_list
= link
;
1526 while (pending_write_insns
)
1528 add_dependence (insn
, XEXP (pending_write_insns
, 0), REG_DEP_ANTI
);
1530 link
= pending_write_insns
;
1531 pending_write_insns
= XEXP (pending_write_insns
, 1);
1532 XEXP (link
, 1) = unused_insn_list
;
1533 unused_insn_list
= link
;
1535 link
= pending_write_mems
;
1536 pending_write_mems
= XEXP (pending_write_mems
, 1);
1537 XEXP (link
, 1) = unused_expr_list
;
1538 unused_expr_list
= link
;
1540 pending_lists_length
= 0;
1542 if (last_pending_memory_flush
)
1543 add_dependence (insn
, last_pending_memory_flush
, REG_DEP_ANTI
);
1545 last_pending_memory_flush
= insn
;
1548 /* Analyze a single SET or CLOBBER rtx, X, creating all dependencies generated
1549 by the write to the destination of X, and reads of everything mentioned. */
1552 sched_analyze_1 (x
, insn
)
1557 register rtx dest
= SET_DEST (x
);
1562 while (GET_CODE (dest
) == STRICT_LOW_PART
|| GET_CODE (dest
) == SUBREG
1563 || GET_CODE (dest
) == ZERO_EXTRACT
|| GET_CODE (dest
) == SIGN_EXTRACT
)
1565 if (GET_CODE (dest
) == ZERO_EXTRACT
|| GET_CODE (dest
) == SIGN_EXTRACT
)
1567 /* The second and third arguments are values read by this insn. */
1568 sched_analyze_2 (XEXP (dest
, 1), insn
);
1569 sched_analyze_2 (XEXP (dest
, 2), insn
);
1571 dest
= SUBREG_REG (dest
);
1574 if (GET_CODE (dest
) == REG
)
1576 register int offset
, bit
, i
;
1578 regno
= REGNO (dest
);
1580 /* A hard reg in a wide mode may really be multiple registers.
1581 If so, mark all of them just like the first. */
1582 if (regno
< FIRST_PSEUDO_REGISTER
)
1584 i
= HARD_REGNO_NREGS (regno
, GET_MODE (dest
));
1589 for (u
= reg_last_uses
[regno
+i
]; u
; u
= XEXP (u
, 1))
1590 add_dependence (insn
, XEXP (u
, 0), REG_DEP_ANTI
);
1591 reg_last_uses
[regno
+ i
] = 0;
1592 if (reg_last_sets
[regno
+ i
])
1593 add_dependence (insn
, reg_last_sets
[regno
+ i
],
1595 reg_last_sets
[regno
+ i
] = insn
;
1596 if ((call_used_regs
[i
] || global_regs
[i
])
1597 && last_function_call
)
1598 /* Function calls clobber all call_used regs. */
1599 add_dependence (insn
, last_function_call
, REG_DEP_ANTI
);
1606 for (u
= reg_last_uses
[regno
]; u
; u
= XEXP (u
, 1))
1607 add_dependence (insn
, XEXP (u
, 0), REG_DEP_ANTI
);
1608 reg_last_uses
[regno
] = 0;
1609 if (reg_last_sets
[regno
])
1610 add_dependence (insn
, reg_last_sets
[regno
], REG_DEP_OUTPUT
);
1611 reg_last_sets
[regno
] = insn
;
1613 /* Pseudos that are REG_EQUIV to something may be replaced
1614 by that during reloading, so we can potentially read
1615 quantities mentioned in those addresses. */
1616 if (! reload_completed
)
1617 if (reg_known_value
[regno
] != regno_reg_rtx
[regno
])
1618 if (GET_CODE (reg_known_value
[regno
]) == MEM
)
1619 sched_analyze_2 (XEXP (reg_known_value
[regno
], 0), insn
);
1621 /* Don't let it cross a call after scheduling if it doesn't
1622 already cross one. */
1623 if (reg_n_calls_crossed
[regno
] == 0 && last_function_call
)
1624 add_dependence (insn
, last_function_call
, REG_DEP_ANTI
);
1627 else if (GET_CODE (dest
) == MEM
)
1629 /* Writing memory. */
1631 if (pending_lists_length
> 32)
1633 /* Flush all pending reads and writes to prevent the pending lists
1634 from getting any larger. Insn scheduling runs too slowly when
1635 these lists get long. The number 32 was chosen because it
1636 seems like a reasonable number. When compiling GCC with itself,
1637 this flush occurs 8 times for sparc, and 10 times for m88k using
1639 flush_pending_lists (insn
);
1643 rtx pending
, pending_mem
;
1645 pending
= pending_read_insns
;
1646 pending_mem
= pending_read_mems
;
1649 /* If a dependency already exists, don't create a new one. */
1650 if (! find_insn_list (XEXP (pending
, 0), LOG_LINKS (insn
)))
1651 if (anti_dependence (XEXP (pending_mem
, 0), dest
, insn
))
1652 add_dependence (insn
, XEXP (pending
, 0), REG_DEP_ANTI
);
1654 pending
= XEXP (pending
, 1);
1655 pending_mem
= XEXP (pending_mem
, 1);
1658 pending
= pending_write_insns
;
1659 pending_mem
= pending_write_mems
;
1662 /* If a dependency already exists, don't create a new one. */
1663 if (! find_insn_list (XEXP (pending
, 0), LOG_LINKS (insn
)))
1664 if (output_dependence (XEXP (pending_mem
, 0), dest
))
1665 add_dependence (insn
, XEXP (pending
, 0), REG_DEP_OUTPUT
);
1667 pending
= XEXP (pending
, 1);
1668 pending_mem
= XEXP (pending_mem
, 1);
1671 if (last_pending_memory_flush
)
1672 add_dependence (insn
, last_pending_memory_flush
, REG_DEP_ANTI
);
1674 add_insn_mem_dependence (&pending_write_insns
, &pending_write_mems
,
1677 sched_analyze_2 (XEXP (dest
, 0), insn
);
1680 /* Analyze reads. */
1681 if (GET_CODE (x
) == SET
)
1682 sched_analyze_2 (SET_SRC (x
), insn
);
1685 /* Analyze the uses of memory and registers in rtx X in INSN. */
1688 sched_analyze_2 (x
, insn
)
1694 register enum rtx_code code
;
1700 code
= GET_CODE (x
);
1709 /* Ignore constants. Note that we must handle CONST_DOUBLE here
1710 because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
1711 this does not mean that this insn is using cc0. */
1719 /* There may be a note before this insn now, but all notes will
1720 be removed before we actually try to schedule the insns, so
1721 it won't cause a problem later. We must avoid it here though. */
1723 /* User of CC0 depends on immediately preceding insn. */
1724 SCHED_GROUP_P (insn
) = 1;
1726 /* Make a copy of all dependencies on the immediately previous insn,
1727 and add to this insn. This is so that all the dependencies will
1728 apply to the group. Remove an explicit dependence on this insn
1729 as SCHED_GROUP_P now represents it. */
1731 prev
= PREV_INSN (insn
);
1732 while (GET_CODE (prev
) == NOTE
)
1733 prev
= PREV_INSN (prev
);
1735 if (find_insn_list (prev
, LOG_LINKS (insn
)))
1736 remove_dependence (insn
, prev
);
1738 for (link
= LOG_LINKS (prev
); link
; link
= XEXP (link
, 1))
1739 add_dependence (insn
, XEXP (link
, 0), REG_NOTE_KIND (link
));
1747 int regno
= REGNO (x
);
1748 if (regno
< FIRST_PSEUDO_REGISTER
)
1752 i
= HARD_REGNO_NREGS (regno
, GET_MODE (x
));
1755 reg_last_uses
[regno
+ i
]
1756 = gen_rtx (INSN_LIST
, VOIDmode
,
1757 insn
, reg_last_uses
[regno
+ i
]);
1758 if (reg_last_sets
[regno
+ i
])
1759 add_dependence (insn
, reg_last_sets
[regno
+ i
], 0);
1760 if ((call_used_regs
[regno
+ i
] || global_regs
[regno
+ i
])
1761 && last_function_call
)
1762 /* Function calls clobber all call_used regs. */
1763 add_dependence (insn
, last_function_call
, REG_DEP_ANTI
);
1768 reg_last_uses
[regno
]
1769 = gen_rtx (INSN_LIST
, VOIDmode
, insn
, reg_last_uses
[regno
]);
1770 if (reg_last_sets
[regno
])
1771 add_dependence (insn
, reg_last_sets
[regno
], 0);
1773 /* Pseudos that are REG_EQUIV to something may be replaced
1774 by that, so we depend on anything mentioned there too. */
1775 if (! reload_completed
)
1776 if (reg_known_value
[regno
] != regno_reg_rtx
[regno
])
1777 sched_analyze_2 (reg_known_value
[regno
], insn
);
1779 /* If the register does not already cross any calls, then add this
1780 insn to the sched_before_next_call list so that it will still
1781 not cross calls after scheduling. */
1782 if (reg_n_calls_crossed
[regno
] == 0)
1783 add_dependence (sched_before_next_call
, insn
, REG_DEP_ANTI
);
1790 /* Reading memory. */
1792 rtx pending
, pending_mem
;
1794 pending
= pending_read_insns
;
1795 pending_mem
= pending_read_mems
;
1798 /* If a dependency already exists, don't create a new one. */
1799 if (! find_insn_list (XEXP (pending
, 0), LOG_LINKS (insn
)))
1800 if (read_dependence (XEXP (pending_mem
, 0), x
))
1801 add_dependence (insn
, XEXP (pending
, 0), REG_DEP_ANTI
);
1803 pending
= XEXP (pending
, 1);
1804 pending_mem
= XEXP (pending_mem
, 1);
1807 pending
= pending_write_insns
;
1808 pending_mem
= pending_write_mems
;
1811 /* If a dependency already exists, don't create a new one. */
1812 if (! find_insn_list (XEXP (pending
, 0), LOG_LINKS (insn
)))
1813 if (true_dependence (XEXP (pending_mem
, 0), x
))
1814 add_dependence (insn
, XEXP (pending
, 0), 0);
1816 pending
= XEXP (pending
, 1);
1817 pending_mem
= XEXP (pending_mem
, 1);
1819 if (last_pending_memory_flush
)
1820 add_dependence (insn
, last_pending_memory_flush
, REG_DEP_ANTI
);
1822 /* Always add these dependencies to pending_reads, since
1823 this insn may be followed by a write. */
1824 add_insn_mem_dependence (&pending_read_insns
, &pending_read_mems
,
1827 /* Take advantage of tail recursion here. */
1828 sched_analyze_2 (XEXP (x
, 0), insn
);
1834 case UNSPEC_VOLATILE
:
1839 /* Traditional and volatile asm instructions must be considered to use
1840 and clobber all hard registers and all of memory. So must
1841 TRAP_IF and UNSPEC_VOLATILE operations. */
1842 if (code
!= ASM_OPERANDS
|| MEM_VOLATILE_P (x
))
1844 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1846 for (u
= reg_last_uses
[i
]; u
; u
= XEXP (u
, 1))
1847 if (GET_CODE (PATTERN (XEXP (u
, 0))) != USE
)
1848 add_dependence (insn
, XEXP (u
, 0), REG_DEP_ANTI
);
1849 reg_last_uses
[i
] = 0;
1850 if (reg_last_sets
[i
]
1851 && GET_CODE (PATTERN (reg_last_sets
[i
])) != USE
)
1852 add_dependence (insn
, reg_last_sets
[i
], 0);
1853 reg_last_sets
[i
] = insn
;
1856 flush_pending_lists (insn
);
1859 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1860 We can not just fall through here since then we would be confused
1861 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1862 traditional asms unlike their normal usage. */
1864 if (code
== ASM_OPERANDS
)
1866 for (j
= 0; j
< ASM_OPERANDS_INPUT_LENGTH (x
); j
++)
1867 sched_analyze_2 (ASM_OPERANDS_INPUT (x
, j
), insn
);
1877 /* These both read and modify the result. We must handle them as writes
1878 to get proper dependencies for following instructions. We must handle
1879 them as reads to get proper dependencies from this to previous
1880 instructions. Thus we need to pass them to both sched_analyze_1
1881 and sched_analyze_2. We must call sched_analyze_2 first in order
1882 to get the proper antecedent for the read. */
1883 sched_analyze_2 (XEXP (x
, 0), insn
);
1884 sched_analyze_1 (x
, insn
);
1888 /* Other cases: walk the insn. */
1889 fmt
= GET_RTX_FORMAT (code
);
1890 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1893 sched_analyze_2 (XEXP (x
, i
), insn
);
1894 else if (fmt
[i
] == 'E')
1895 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1896 sched_analyze_2 (XVECEXP (x
, i
, j
), insn
);
1900 /* Analyze an INSN with pattern X to find all dependencies. */
1903 sched_analyze_insn (x
, insn
)
1906 register RTX_CODE code
= GET_CODE (x
);
1909 if (code
== SET
|| code
== CLOBBER
)
1910 sched_analyze_1 (x
, insn
);
1911 else if (code
== PARALLEL
)
1914 for (i
= XVECLEN (x
, 0) - 1; i
>= 0; i
--)
1916 code
= GET_CODE (XVECEXP (x
, 0, i
));
1917 if (code
== SET
|| code
== CLOBBER
)
1918 sched_analyze_1 (XVECEXP (x
, 0, i
), insn
);
1920 sched_analyze_2 (XVECEXP (x
, 0, i
), insn
);
1924 sched_analyze_2 (x
, insn
);
1926 /* Handle function calls. */
1927 if (GET_CODE (insn
) == CALL_INSN
)
1932 /* When scheduling instructions, we make sure calls don't lose their
1933 accompanying USE insns by depending them one on another in order. */
1935 prev_dep_insn
= insn
;
1936 dep_insn
= PREV_INSN (insn
);
1937 while (GET_CODE (dep_insn
) == INSN
1938 && GET_CODE (PATTERN (dep_insn
)) == USE
)
1940 SCHED_GROUP_P (prev_dep_insn
) = 1;
1942 /* Make a copy of all dependencies on dep_insn, and add to insn.
1943 This is so that all of the dependencies will apply to the
1946 for (link
= LOG_LINKS (dep_insn
); link
; link
= XEXP (link
, 1))
1947 add_dependence (insn
, XEXP (link
, 0), REG_NOTE_KIND (link
));
1949 prev_dep_insn
= dep_insn
;
1950 dep_insn
= PREV_INSN (dep_insn
);
1955 /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
1956 for every dependency. */
1959 sched_analyze (head
, tail
)
1963 register int n_insns
= 0;
1965 register int luid
= 0;
1967 for (insn
= head
; ; insn
= NEXT_INSN (insn
))
1969 INSN_LUID (insn
) = luid
++;
1971 if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == JUMP_INSN
)
1973 sched_analyze_insn (PATTERN (insn
), insn
);
1976 else if (GET_CODE (insn
) == CALL_INSN
)
1982 /* Any instruction using a hard register which may get clobbered
1983 by a call needs to be marked as dependent on this call.
1984 This prevents a use of a hard return reg from being moved
1985 past a void call (i.e. it does not explicitly set the hard
1988 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1989 if (call_used_regs
[i
] || global_regs
[i
])
1991 for (u
= reg_last_uses
[i
]; u
; u
= XEXP (u
, 1))
1992 if (GET_CODE (PATTERN (XEXP (u
, 0))) != USE
)
1993 add_dependence (insn
, XEXP (u
, 0), REG_DEP_ANTI
);
1994 reg_last_uses
[i
] = 0;
1995 if (reg_last_sets
[i
]
1996 && GET_CODE (PATTERN (reg_last_sets
[i
])) != USE
)
1997 add_dependence (insn
, reg_last_sets
[i
], REG_DEP_ANTI
);
1998 reg_last_sets
[i
] = insn
;
1999 /* Insn, being a CALL_INSN, magically depends on
2000 `last_function_call' already. */
2003 /* For each insn which shouldn't cross a call, add a dependence
2004 between that insn and this call insn. */
2005 x
= LOG_LINKS (sched_before_next_call
);
2008 add_dependence (insn
, XEXP (x
, 0), REG_DEP_ANTI
);
2011 LOG_LINKS (sched_before_next_call
) = 0;
2013 sched_analyze_insn (PATTERN (insn
), insn
);
2015 /* We don't need to flush memory for a function call which does
2016 not involve memory. */
2017 if (! CONST_CALL_P (insn
))
2019 /* In the absence of interprocedural alias analysis,
2020 we must flush all pending reads and writes, and
2021 start new dependencies starting from here. */
2022 flush_pending_lists (insn
);
2025 /* Depend this function call (actually, the user of this
2026 function call) on all hard register clobberage. */
2027 last_function_call
= insn
;
2036 /* Called when we see a set of a register. If death is true, then we are
2037 scanning backwards. Mark that register as unborn. If nobody says
2038 otherwise, that is how things will remain. If death is false, then we
2039 are scanning forwards. Mark that register as being born. */
2042 sched_note_set (b
, x
, death
)
2047 register int regno
, j
;
2048 register rtx reg
= SET_DEST (x
);
2054 while (GET_CODE (reg
) == SUBREG
|| GET_CODE (reg
) == STRICT_LOW_PART
2055 || GET_CODE (reg
) == SIGN_EXTRACT
|| GET_CODE (reg
) == ZERO_EXTRACT
)
2057 /* Must treat modification of just one hardware register of a multi-reg
2058 value or just a byte field of a register exactly the same way that
2059 mark_set_1 in flow.c does, i.e. anything except a paradoxical subreg
2060 does not kill the entire register. */
2061 if (GET_CODE (reg
) != SUBREG
2062 || REG_SIZE (SUBREG_REG (reg
)) > REG_SIZE (reg
))
2065 reg
= SUBREG_REG (reg
);
2068 if (GET_CODE (reg
) != REG
)
2071 /* Global registers are always live, so the code below does not apply
2074 regno
= REGNO (reg
);
2075 if (regno
>= FIRST_PSEUDO_REGISTER
|| ! global_regs
[regno
])
2077 register int offset
= regno
/ REGSET_ELT_BITS
;
2078 register REGSET_ELT_TYPE bit
2079 = (REGSET_ELT_TYPE
) 1 << (regno
% REGSET_ELT_BITS
);
2083 /* If we only set part of the register, then this set does not
2088 /* Try killing this register. */
2089 if (regno
< FIRST_PSEUDO_REGISTER
)
2091 int j
= HARD_REGNO_NREGS (regno
, GET_MODE (reg
));
2094 offset
= (regno
+ j
) / REGSET_ELT_BITS
;
2095 bit
= (REGSET_ELT_TYPE
) 1 << ((regno
+ j
) % REGSET_ELT_BITS
);
2097 bb_live_regs
[offset
] &= ~bit
;
2098 bb_dead_regs
[offset
] |= bit
;
2103 bb_live_regs
[offset
] &= ~bit
;
2104 bb_dead_regs
[offset
] |= bit
;
2109 /* Make the register live again. */
2110 if (regno
< FIRST_PSEUDO_REGISTER
)
2112 int j
= HARD_REGNO_NREGS (regno
, GET_MODE (reg
));
2115 offset
= (regno
+ j
) / REGSET_ELT_BITS
;
2116 bit
= (REGSET_ELT_TYPE
) 1 << ((regno
+ j
) % REGSET_ELT_BITS
);
2118 bb_live_regs
[offset
] |= bit
;
2119 bb_dead_regs
[offset
] &= ~bit
;
2124 bb_live_regs
[offset
] |= bit
;
2125 bb_dead_regs
[offset
] &= ~bit
;
2131 /* Macros and functions for keeping the priority queue sorted, and
2132 dealing with queueing and unqueueing of instructions. */
2134 #define SCHED_SORT(READY, NEW_READY, OLD_READY) \
2135 do { if ((NEW_READY) - (OLD_READY) == 1) \
2136 swap_sort (READY, NEW_READY); \
2137 else if ((NEW_READY) - (OLD_READY) > 1) \
2138 qsort (READY, NEW_READY, sizeof (rtx), rank_for_schedule); } \
2141 /* Returns a positive value if y is preferred; returns a negative value if
2142 x is preferred. Should never return 0, since that will make the sort
2146 rank_for_schedule (x
, y
)
2152 int tmp_class
, tmp2_class
;
2155 /* Choose the instruction with the highest priority, if different. */
2156 if (value
= INSN_PRIORITY (tmp
) - INSN_PRIORITY (tmp2
))
2159 if (last_scheduled_insn
)
2161 /* Classify the instructions into three classes:
2162 1) Data dependent on last schedule insn.
2163 2) Anti/Output dependent on last scheduled insn.
2164 3) Independent of last scheduled insn, or has latency of one.
2165 Choose the insn from the highest numbered class if different. */
2166 link
= find_insn_list (tmp
, LOG_LINKS (last_scheduled_insn
));
2167 if (link
== 0 || insn_cost (tmp
, link
, last_scheduled_insn
) == 1)
2169 else if (REG_NOTE_KIND (link
) == 0) /* Data dependence. */
2174 link
= find_insn_list (tmp2
, LOG_LINKS (last_scheduled_insn
));
2175 if (link
== 0 || insn_cost (tmp2
, link
, last_scheduled_insn
) == 1)
2177 else if (REG_NOTE_KIND (link
) == 0) /* Data dependence. */
2182 if (value
= tmp_class
- tmp2_class
)
2186 /* If insns are equally good, sort by INSN_LUID (original insn order),
2187 so that we make the sort stable. This minimizes instruction movement,
2188 thus minimizing sched's effect on debugging and cross-jumping. */
2189 return INSN_LUID (tmp
) - INSN_LUID (tmp2
);
2192 /* Resort the array A in which only element at index N may be out of order. */
2194 __inline
static void
2202 while (i
>= 0 && rank_for_schedule (a
+i
, &insn
) >= 0)
2210 static int max_priority
;
2212 /* Add INSN to the insn queue so that it fires at least N_CYCLES
2213 before the currently executing insn. */
2215 __inline
static void
2216 queue_insn (insn
, n_cycles
)
2220 int next_q
= NEXT_Q_AFTER (q_ptr
, n_cycles
);
2221 NEXT_INSN (insn
) = insn_queue
[next_q
];
2222 insn_queue
[next_q
] = insn
;
2226 /* Return nonzero if PAT is the pattern of an insn which makes a
2230 birthing_insn_p (pat
)
2235 if (reload_completed
== 1)
2238 if (GET_CODE (pat
) == SET
2239 && GET_CODE (SET_DEST (pat
)) == REG
)
2241 rtx dest
= SET_DEST (pat
);
2242 int i
= REGNO (dest
);
2243 int offset
= i
/ REGSET_ELT_BITS
;
2244 REGSET_ELT_TYPE bit
= (REGSET_ELT_TYPE
) 1 << (i
% REGSET_ELT_BITS
);
2246 /* It would be more accurate to use refers_to_regno_p or
2247 reg_mentioned_p to determine when the dest is not live before this
2250 if (bb_live_regs
[offset
] & bit
)
2251 return (reg_n_sets
[i
] == 1);
2255 if (GET_CODE (pat
) == PARALLEL
)
2257 for (j
= 0; j
< XVECLEN (pat
, 0); j
++)
2258 if (birthing_insn_p (XVECEXP (pat
, 0, j
)))
2264 /* PREV is an insn that is ready to execute. Adjust its priority if that
2265 will help shorten register lifetimes. */
2267 __inline
static void
2268 adjust_priority (prev
)
2271 /* Trying to shorten register lives after reload has completed
2272 is useless and wrong. It gives inaccurate schedules. */
2273 if (reload_completed
== 0)
2278 for (note
= REG_NOTES (prev
); note
; note
= XEXP (note
, 1))
2279 if (REG_NOTE_KIND (note
) == REG_DEAD
)
2282 /* Defer scheduling insns which kill registers, since that
2283 shortens register lives. Prefer scheduling insns which
2284 make registers live for the same reason. */
2288 INSN_PRIORITY (prev
) >>= 3;
2291 INSN_PRIORITY (prev
) >>= 2;
2295 INSN_PRIORITY (prev
) >>= 1;
2298 if (birthing_insn_p (PATTERN (prev
)))
2300 int max
= max_priority
;
2302 if (max
> INSN_PRIORITY (prev
))
2303 INSN_PRIORITY (prev
) = max
;
2310 /* INSN is the "currently executing insn". Launch each insn which was
2311 waiting on INSN (in the backwards dataflow sense). READY is a
2312 vector of insns which are ready to fire. N_READY is the number of
2313 elements in READY. CLOCK is the current virtual cycle. */
2316 schedule_insn (insn
, ready
, n_ready
, clock
)
2323 int new_ready
= n_ready
;
2325 if (MAX_BLOCKAGE
> 1)
2326 schedule_unit (insn_unit (insn
), insn
, clock
);
2328 if (LOG_LINKS (insn
) == 0)
2331 /* This is used by the function adjust_priority above. */
2333 max_priority
= MAX (INSN_PRIORITY (ready
[0]), INSN_PRIORITY (insn
));
2335 max_priority
= INSN_PRIORITY (insn
);
2337 for (link
= LOG_LINKS (insn
); link
!= 0; link
= XEXP (link
, 1))
2339 rtx prev
= XEXP (link
, 0);
2340 int cost
= insn_cost (prev
, link
, insn
);
2342 if ((INSN_REF_COUNT (prev
) -= 1) != 0)
2344 /* We satisfied one requirement to fire PREV. Record the earliest
2345 time when PREV can fire. No need to do this if the cost is 1,
2346 because PREV can fire no sooner than the next cycle. */
2348 INSN_TICK (prev
) = MAX (INSN_TICK (prev
), clock
+ cost
);
2352 /* We satisfied the last requirement to fire PREV. Ensure that all
2353 timing requirements are satisfied. */
2354 if (INSN_TICK (prev
) - clock
> cost
)
2355 cost
= INSN_TICK (prev
) - clock
;
2357 /* Adjust the priority of PREV and either put it on the ready
2358 list or queue it. */
2359 adjust_priority (prev
);
2361 ready
[new_ready
++] = prev
;
2363 queue_insn (prev
, cost
);
2370 /* Given N_READY insns in the ready list READY at time CLOCK, queue
2371 those that are blocked due to function unit hazards and rearrange
2372 the remaining ones to minimize subsequent function unit hazards. */
2375 schedule_select (ready
, n_ready
, clock
, file
)
2380 int pri
= INSN_PRIORITY (ready
[0]);
2381 int i
, j
, k
, q
, cost
, best_cost
, best_insn
= 0, new_ready
= n_ready
;
2384 /* Work down the ready list in groups of instructions with the same
2385 priority value. Queue insns in the group that are blocked and
2386 select among those that remain for the one with the largest
2387 potential hazard. */
2388 for (i
= 0; i
< n_ready
; i
= j
)
2391 for (j
= i
+ 1; j
< n_ready
; j
++)
2392 if ((pri
= INSN_PRIORITY (ready
[j
])) != opri
)
2395 /* Queue insns in the group that are blocked. */
2396 for (k
= i
, q
= 0; k
< j
; k
++)
2399 if ((cost
= actual_hazard (insn_unit (insn
), insn
, clock
, 0)) != 0)
2403 queue_insn (insn
, cost
);
2405 fprintf (file
, "\n;; blocking insn %d for %d cycles",
2406 INSN_UID (insn
), cost
);
2411 /* Check the next group if all insns were queued. */
2415 /* If more than one remains, select the first one with the largest
2416 potential hazard. */
2417 else if (j
- i
- q
> 1)
2420 for (k
= i
; k
< j
; k
++)
2422 if ((insn
= ready
[k
]) == 0)
2424 if ((cost
= potential_hazard (insn_unit (insn
), insn
, 0))
2432 /* We have found a suitable insn to schedule. */
2436 /* Move the best insn to be front of the ready list. */
2441 fprintf (file
, ", now");
2442 for (i
= 0; i
< n_ready
; i
++)
2444 fprintf (file
, " %d", INSN_UID (ready
[i
]));
2445 fprintf (file
, "\n;; insn %d has a greater potential hazard",
2446 INSN_UID (ready
[best_insn
]));
2448 for (i
= best_insn
; i
> 0; i
--)
2451 ready
[i
-1] = ready
[i
];
2456 /* Compact the ready list. */
2457 if (new_ready
< n_ready
)
2458 for (i
= j
= 0; i
< n_ready
; i
++)
2460 ready
[j
++] = ready
[i
];
2465 /* Add a REG_DEAD note for REG to INSN, reusing a REG_DEAD note from the
2469 create_reg_dead_note (reg
, insn
)
2472 rtx link
= dead_notes
;
2475 /* In theory, we should not end up with more REG_DEAD reg notes than we
2476 started with. In practice, this can occur as the result of bugs in
2477 flow, combine and/or sched. */
2482 link
= rtx_alloc (EXPR_LIST
);
2483 PUT_REG_NOTE_KIND (link
, REG_DEAD
);
2487 dead_notes
= XEXP (dead_notes
, 1);
2489 XEXP (link
, 0) = reg
;
2490 XEXP (link
, 1) = REG_NOTES (insn
);
2491 REG_NOTES (insn
) = link
;
2494 /* Subroutine on attach_deaths_insn--handles the recursive search
2495 through INSN. If SET_P is true, then x is being modified by the insn. */
2498 attach_deaths (x
, insn
, set_p
)
2505 register enum rtx_code code
;
2511 code
= GET_CODE (x
);
2523 /* Get rid of the easy cases first. */
2528 /* If the register dies in this insn, queue that note, and mark
2529 this register as needing to die. */
2530 /* This code is very similar to mark_used_1 (if set_p is false)
2531 and mark_set_1 (if set_p is true) in flow.c. */
2533 register int regno
= REGNO (x
);
2534 register int offset
= regno
/ REGSET_ELT_BITS
;
2535 register REGSET_ELT_TYPE bit
2536 = (REGSET_ELT_TYPE
) 1 << (regno
% REGSET_ELT_BITS
);
2537 REGSET_ELT_TYPE all_needed
= (old_live_regs
[offset
] & bit
);
2538 REGSET_ELT_TYPE some_needed
= (old_live_regs
[offset
] & bit
);
2543 if (regno
< FIRST_PSEUDO_REGISTER
)
2547 n
= HARD_REGNO_NREGS (regno
, GET_MODE (x
));
2550 some_needed
|= (old_live_regs
[(regno
+ n
) / REGSET_ELT_BITS
]
2551 & ((REGSET_ELT_TYPE
) 1
2552 << ((regno
+ n
) % REGSET_ELT_BITS
)));
2553 all_needed
&= (old_live_regs
[(regno
+ n
) / REGSET_ELT_BITS
]
2554 & ((REGSET_ELT_TYPE
) 1
2555 << ((regno
+ n
) % REGSET_ELT_BITS
)));
2559 /* If it wasn't live before we started, then add a REG_DEAD note.
2560 We must check the previous lifetime info not the current info,
2561 because we may have to execute this code several times, e.g.
2562 once for a clobber (which doesn't add a note) and later
2563 for a use (which does add a note).
2565 Always make the register live. We must do this even if it was
2566 live before, because this may be an insn which sets and uses
2567 the same register, in which case the register has already been
2568 killed, so we must make it live again.
2570 Global registers are always live, and should never have a REG_DEAD
2571 note added for them, so none of the code below applies to them. */
2573 if (regno
>= FIRST_PSEUDO_REGISTER
|| ! global_regs
[regno
])
2575 /* Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2576 STACK_POINTER_REGNUM, since these are always considered to be
2577 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2578 if (regno
!= FRAME_POINTER_REGNUM
2579 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2580 && ! (regno
== ARG_POINTER_REGNUM
&& fixed_regs
[regno
])
2582 && regno
!= STACK_POINTER_REGNUM
)
2584 if (! all_needed
&& ! dead_or_set_p (insn
, x
))
2586 /* If none of the words in X is needed, make a REG_DEAD
2587 note. Otherwise, we must make partial REG_DEAD
2590 create_reg_dead_note (x
, insn
);
2595 /* Don't make a REG_DEAD note for a part of a
2596 register that is set in the insn. */
2597 for (i
= HARD_REGNO_NREGS (regno
, GET_MODE (x
)) - 1;
2599 if ((old_live_regs
[(regno
+ i
) / REGSET_ELT_BITS
]
2600 & ((REGSET_ELT_TYPE
) 1
2601 << ((regno
+i
) % REGSET_ELT_BITS
))) == 0
2602 && ! dead_or_set_regno_p (insn
, regno
+ i
))
2603 create_reg_dead_note (gen_rtx (REG
, word_mode
,
2610 if (regno
< FIRST_PSEUDO_REGISTER
)
2612 int j
= HARD_REGNO_NREGS (regno
, GET_MODE (x
));
2615 offset
= (regno
+ j
) / REGSET_ELT_BITS
;
2617 = (REGSET_ELT_TYPE
) 1 << ((regno
+ j
) % REGSET_ELT_BITS
);
2619 bb_dead_regs
[offset
] &= ~bit
;
2620 bb_live_regs
[offset
] |= bit
;
2625 bb_dead_regs
[offset
] &= ~bit
;
2626 bb_live_regs
[offset
] |= bit
;
2633 /* Handle tail-recursive case. */
2634 attach_deaths (XEXP (x
, 0), insn
, 0);
2638 case STRICT_LOW_PART
:
2639 /* These two cases preserve the value of SET_P, so handle them
2641 attach_deaths (XEXP (x
, 0), insn
, set_p
);
2646 /* This case preserves the value of SET_P for the first operand, but
2647 clears it for the other two. */
2648 attach_deaths (XEXP (x
, 0), insn
, set_p
);
2649 attach_deaths (XEXP (x
, 1), insn
, 0);
2650 attach_deaths (XEXP (x
, 2), insn
, 0);
2654 /* Other cases: walk the insn. */
2655 fmt
= GET_RTX_FORMAT (code
);
2656 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2659 attach_deaths (XEXP (x
, i
), insn
, 0);
2660 else if (fmt
[i
] == 'E')
2661 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2662 attach_deaths (XVECEXP (x
, i
, j
), insn
, 0);
2667 /* After INSN has executed, add register death notes for each register
2668 that is dead after INSN. */
2671 attach_deaths_insn (insn
)
2674 rtx x
= PATTERN (insn
);
2675 register RTX_CODE code
= GET_CODE (x
);
2679 attach_deaths (SET_SRC (x
), insn
, 0);
2681 /* A register might die here even if it is the destination, e.g.
2682 it is the target of a volatile read and is otherwise unused.
2683 Hence we must always call attach_deaths for the SET_DEST. */
2684 attach_deaths (SET_DEST (x
), insn
, 1);
2686 else if (code
== PARALLEL
)
2689 for (i
= XVECLEN (x
, 0) - 1; i
>= 0; i
--)
2691 code
= GET_CODE (XVECEXP (x
, 0, i
));
2694 attach_deaths (SET_SRC (XVECEXP (x
, 0, i
)), insn
, 0);
2696 attach_deaths (SET_DEST (XVECEXP (x
, 0, i
)), insn
, 1);
2698 /* Flow does not add REG_DEAD notes to registers that die in
2699 clobbers, so we can't either. */
2700 else if (code
!= CLOBBER
)
2701 attach_deaths (XVECEXP (x
, 0, i
), insn
, 0);
2704 /* Flow does not add REG_DEAD notes to registers that die in
2705 clobbers, so we can't either. */
2706 else if (code
!= CLOBBER
)
2707 attach_deaths (x
, insn
, 0);
2710 /* Delete notes beginning with INSN and maybe put them in the chain
2711 of notes ended by NOTE_LIST.
2712 Returns the insn following the notes. */
2715 unlink_notes (insn
, tail
)
2718 rtx prev
= PREV_INSN (insn
);
2720 while (insn
!= tail
&& GET_CODE (insn
) == NOTE
)
2722 rtx next
= NEXT_INSN (insn
);
2723 /* Delete the note from its current position. */
2725 NEXT_INSN (prev
) = next
;
2727 PREV_INSN (next
) = prev
;
2729 if (write_symbols
!= NO_DEBUG
&& NOTE_LINE_NUMBER (insn
) > 0)
2730 /* Record line-number notes so they can be reused. */
2731 LINE_NOTE (insn
) = insn
;
2734 /* Insert the note at the end of the notes list. */
2735 PREV_INSN (insn
) = note_list
;
2737 NEXT_INSN (note_list
) = insn
;
2746 /* Data structure for keeping track of register information
2747 during that register's life. */
2751 short offset
; short bit
;
2752 short live_length
; short calls_crossed
;
2755 /* Constructor for `sometimes' data structure. */
2758 new_sometimes_live (regs_sometimes_live
, offset
, bit
, sometimes_max
)
2759 struct sometimes
*regs_sometimes_live
;
2763 register struct sometimes
*p
;
2764 register int regno
= offset
* REGSET_ELT_BITS
+ bit
;
2767 /* There should never be a register greater than max_regno here. If there
2768 is, it means that a define_split has created a new pseudo reg. This
2769 is not allowed, since there will not be flow info available for any
2770 new register, so catch the error here. */
2771 if (regno
>= max_regno
)
2774 p
= ®s_sometimes_live
[sometimes_max
];
2778 p
->calls_crossed
= 0;
2780 return sometimes_max
;
2783 /* Count lengths of all regs we are currently tracking,
2784 and find new registers no longer live. */
2787 finish_sometimes_live (regs_sometimes_live
, sometimes_max
)
2788 struct sometimes
*regs_sometimes_live
;
2793 for (i
= 0; i
< sometimes_max
; i
++)
2795 register struct sometimes
*p
= ®s_sometimes_live
[i
];
2798 regno
= p
->offset
* REGSET_ELT_BITS
+ p
->bit
;
2800 sched_reg_live_length
[regno
] += p
->live_length
;
2801 sched_reg_n_calls_crossed
[regno
] += p
->calls_crossed
;
2805 /* Use modified list scheduling to rearrange insns in basic block
2806 B. FILE, if nonzero, is where we dump interesting output about
2810 schedule_block (b
, file
)
2817 int i
, j
, n_ready
= 0, new_ready
, n_insns
= 0;
2818 int sched_n_insns
= 0;
2820 #define NEED_NOTHING 0
2825 /* HEAD and TAIL delimit the region being scheduled. */
2826 rtx head
= basic_block_head
[b
];
2827 rtx tail
= basic_block_end
[b
];
2828 /* PREV_HEAD and NEXT_TAIL are the boundaries of the insns
2829 being scheduled. When the insns have been ordered,
2830 these insns delimit where the new insns are to be
2831 spliced back into the insn chain. */
2835 /* Keep life information accurate. */
2836 register struct sometimes
*regs_sometimes_live
;
2840 fprintf (file
, ";;\t -- basic block number %d from %d to %d --\n",
2841 b
, INSN_UID (basic_block_head
[b
]), INSN_UID (basic_block_end
[b
]));
2844 reg_last_uses
= (rtx
*) alloca (i
* sizeof (rtx
));
2845 bzero (reg_last_uses
, i
* sizeof (rtx
));
2846 reg_last_sets
= (rtx
*) alloca (i
* sizeof (rtx
));
2847 bzero (reg_last_sets
, i
* sizeof (rtx
));
2850 /* Remove certain insns at the beginning from scheduling,
2851 by advancing HEAD. */
2853 /* At the start of a function, before reload has run, don't delay getting
2854 parameters from hard registers into pseudo registers. */
2855 if (reload_completed
== 0 && b
== 0)
2858 && GET_CODE (head
) == NOTE
2859 && NOTE_LINE_NUMBER (head
) != NOTE_INSN_FUNCTION_BEG
)
2860 head
= NEXT_INSN (head
);
2862 && GET_CODE (head
) == INSN
2863 && GET_CODE (PATTERN (head
)) == SET
)
2865 rtx src
= SET_SRC (PATTERN (head
));
2866 while (GET_CODE (src
) == SUBREG
2867 || GET_CODE (src
) == SIGN_EXTEND
2868 || GET_CODE (src
) == ZERO_EXTEND
2869 || GET_CODE (src
) == SIGN_EXTRACT
2870 || GET_CODE (src
) == ZERO_EXTRACT
)
2871 src
= XEXP (src
, 0);
2872 if (GET_CODE (src
) != REG
2873 || REGNO (src
) >= FIRST_PSEUDO_REGISTER
)
2875 /* Keep this insn from ever being scheduled. */
2876 INSN_REF_COUNT (head
) = 1;
2877 head
= NEXT_INSN (head
);
2881 /* Don't include any notes or labels at the beginning of the
2882 basic block, or notes at the ends of basic blocks. */
2883 while (head
!= tail
)
2885 if (GET_CODE (head
) == NOTE
)
2886 head
= NEXT_INSN (head
);
2887 else if (GET_CODE (tail
) == NOTE
)
2888 tail
= PREV_INSN (tail
);
2889 else if (GET_CODE (head
) == CODE_LABEL
)
2890 head
= NEXT_INSN (head
);
2893 /* If the only insn left is a NOTE or a CODE_LABEL, then there is no need
2894 to schedule this block. */
2896 && (GET_CODE (head
) == NOTE
|| GET_CODE (head
) == CODE_LABEL
))
2900 /* This short-cut doesn't work. It does not count call insns crossed by
2901 registers in reg_sometimes_live. It does not mark these registers as
2902 dead if they die in this block. It does not mark these registers live
2903 (or create new reg_sometimes_live entries if necessary) if they are born
2906 The easy solution is to just always schedule a block. This block only
2907 has one insn, so this won't slow down this pass by much. */
2913 /* Now HEAD through TAIL are the insns actually to be rearranged;
2914 Let PREV_HEAD and NEXT_TAIL enclose them. */
2915 prev_head
= PREV_INSN (head
);
2916 next_tail
= NEXT_INSN (tail
);
2918 /* Initialize basic block data structures. */
2920 pending_read_insns
= 0;
2921 pending_read_mems
= 0;
2922 pending_write_insns
= 0;
2923 pending_write_mems
= 0;
2924 pending_lists_length
= 0;
2925 last_pending_memory_flush
= 0;
2926 last_function_call
= 0;
2927 last_scheduled_insn
= 0;
2929 LOG_LINKS (sched_before_next_call
) = 0;
2931 n_insns
+= sched_analyze (head
, tail
);
2934 free_pending_lists ();
2938 /* Allocate vector to hold insns to be rearranged (except those
2939 insns which are controlled by an insn with SCHED_GROUP_P set).
2940 All these insns are included between ORIG_HEAD and ORIG_TAIL,
2941 as those variables ultimately are set up. */
2942 ready
= (rtx
*) alloca ((n_insns
+1) * sizeof (rtx
));
2944 /* TAIL is now the last of the insns to be rearranged.
2945 Put those insns into the READY vector. */
2948 /* For all branches, calls, uses, and cc0 setters, force them to remain
2949 in order at the end of the block by adding dependencies and giving
2950 the last a high priority. There may be notes present, and prev_head
2953 Branches must obviously remain at the end. Calls should remain at the
2954 end since moving them results in worse register allocation. Uses remain
2955 at the end to ensure proper register allocation. cc0 setters remaim
2956 at the end because they can't be moved away from their cc0 user. */
2958 while (GET_CODE (insn
) == CALL_INSN
|| GET_CODE (insn
) == JUMP_INSN
2959 || (GET_CODE (insn
) == INSN
2960 && (GET_CODE (PATTERN (insn
)) == USE
2962 || sets_cc0_p (PATTERN (insn
))
2965 || GET_CODE (insn
) == NOTE
)
2967 if (GET_CODE (insn
) != NOTE
)
2972 ready
[n_ready
++] = insn
;
2973 INSN_PRIORITY (insn
) = TAIL_PRIORITY
- i
;
2974 INSN_REF_COUNT (insn
) = 0;
2976 else if (! find_insn_list (insn
, LOG_LINKS (last
)))
2978 add_dependence (last
, insn
, REG_DEP_ANTI
);
2979 INSN_REF_COUNT (insn
)++;
2983 /* Skip over insns that are part of a group. */
2984 while (SCHED_GROUP_P (insn
))
2986 insn
= prev_nonnote_insn (insn
);
2991 insn
= PREV_INSN (insn
);
2992 /* Don't overrun the bounds of the basic block. */
2993 if (insn
== prev_head
)
2997 /* Assign priorities to instructions. Also check whether they
2998 are in priority order already. If so then I will be nonnegative.
2999 We use this shortcut only before reloading. */
3001 i
= reload_completed
? DONE_PRIORITY
: MAX_PRIORITY
;
3004 for (; insn
!= prev_head
; insn
= PREV_INSN (insn
))
3006 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i')
3009 if (INSN_REF_COUNT (insn
) == 0)
3012 ready
[n_ready
++] = insn
;
3015 /* Make this dependent on the last of the instructions
3016 that must remain in order at the end of the block. */
3017 add_dependence (last
, insn
, REG_DEP_ANTI
);
3018 INSN_REF_COUNT (insn
) = 1;
3021 if (SCHED_GROUP_P (insn
))
3023 while (SCHED_GROUP_P (insn
))
3025 insn
= PREV_INSN (insn
);
3026 while (GET_CODE (insn
) == NOTE
)
3027 insn
= PREV_INSN (insn
);
3035 if (INSN_PRIORITY (insn
) < i
)
3036 i
= INSN_PRIORITY (insn
);
3037 else if (INSN_PRIORITY (insn
) > i
)
3044 /* This short-cut doesn't work. It does not count call insns crossed by
3045 registers in reg_sometimes_live. It does not mark these registers as
3046 dead if they die in this block. It does not mark these registers live
3047 (or create new reg_sometimes_live entries if necessary) if they are born
3050 The easy solution is to just always schedule a block. These blocks tend
3051 to be very short, so this doesn't slow down this pass by much. */
3053 /* If existing order is good, don't bother to reorder. */
3054 if (i
!= DONE_PRIORITY
)
3057 fprintf (file
, ";; already scheduled\n");
3059 if (reload_completed
== 0)
3061 for (i
= 0; i
< sometimes_max
; i
++)
3062 regs_sometimes_live
[i
].live_length
+= n_insns
;
3064 finish_sometimes_live (regs_sometimes_live
, sometimes_max
);
3066 free_pending_lists ();
3071 /* Scan all the insns to be scheduled, removing NOTE insns
3072 and register death notes.
3073 Line number NOTE insns end up in NOTE_LIST.
3074 Register death notes end up in DEAD_NOTES.
3076 Recreate the register life information for the end of this basic
3079 if (reload_completed
== 0)
3081 bcopy (basic_block_live_at_start
[b
], bb_live_regs
, regset_bytes
);
3082 bzero (bb_dead_regs
, regset_bytes
);
3086 /* This is the first block in the function. There may be insns
3087 before head that we can't schedule. We still need to examine
3088 them though for accurate register lifetime analysis. */
3090 /* We don't want to remove any REG_DEAD notes as the code below
3093 for (insn
= basic_block_head
[b
]; insn
!= head
;
3094 insn
= NEXT_INSN (insn
))
3095 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i')
3097 /* See if the register gets born here. */
3098 /* We must check for registers being born before we check for
3099 registers dying. It is possible for a register to be born
3100 and die in the same insn, e.g. reading from a volatile
3101 memory location into an otherwise unused register. Such
3102 a register must be marked as dead after this insn. */
3103 if (GET_CODE (PATTERN (insn
)) == SET
3104 || GET_CODE (PATTERN (insn
)) == CLOBBER
)
3105 sched_note_set (b
, PATTERN (insn
), 0);
3106 else if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
3109 for (j
= XVECLEN (PATTERN (insn
), 0) - 1; j
>= 0; j
--)
3110 if (GET_CODE (XVECEXP (PATTERN (insn
), 0, j
)) == SET
3111 || GET_CODE (XVECEXP (PATTERN (insn
), 0, j
)) == CLOBBER
)
3112 sched_note_set (b
, XVECEXP (PATTERN (insn
), 0, j
), 0);
3114 /* ??? This code is obsolete and should be deleted. It
3115 is harmless though, so we will leave it in for now. */
3116 for (j
= XVECLEN (PATTERN (insn
), 0) - 1; j
>= 0; j
--)
3117 if (GET_CODE (XVECEXP (PATTERN (insn
), 0, j
)) == USE
)
3118 sched_note_set (b
, XVECEXP (PATTERN (insn
), 0, j
), 0);
3121 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
3123 if ((REG_NOTE_KIND (link
) == REG_DEAD
3124 || REG_NOTE_KIND (link
) == REG_UNUSED
)
3125 /* Verify that the REG_NOTE has a legal value. */
3126 && GET_CODE (XEXP (link
, 0)) == REG
)
3128 register int regno
= REGNO (XEXP (link
, 0));
3129 register int offset
= regno
/ REGSET_ELT_BITS
;
3130 register REGSET_ELT_TYPE bit
3131 = (REGSET_ELT_TYPE
) 1 << (regno
% REGSET_ELT_BITS
);
3133 if (regno
< FIRST_PSEUDO_REGISTER
)
3135 int j
= HARD_REGNO_NREGS (regno
,
3136 GET_MODE (XEXP (link
, 0)));
3139 offset
= (regno
+ j
) / REGSET_ELT_BITS
;
3140 bit
= ((REGSET_ELT_TYPE
) 1
3141 << ((regno
+ j
) % REGSET_ELT_BITS
));
3143 bb_live_regs
[offset
] &= ~bit
;
3144 bb_dead_regs
[offset
] |= bit
;
3149 bb_live_regs
[offset
] &= ~bit
;
3150 bb_dead_regs
[offset
] |= bit
;
3158 /* If debugging information is being produced, keep track of the line
3159 number notes for each insn. */
3160 if (write_symbols
!= NO_DEBUG
)
3162 /* We must use the true line number for the first insn in the block
3163 that was computed and saved at the start of this pass. We can't
3164 use the current line number, because scheduling of the previous
3165 block may have changed the current line number. */
3166 rtx line
= line_note_head
[b
];
3168 for (insn
= basic_block_head
[b
];
3170 insn
= NEXT_INSN (insn
))
3171 if (GET_CODE (insn
) == NOTE
&& NOTE_LINE_NUMBER (insn
) > 0)
3174 LINE_NOTE (insn
) = line
;
3177 for (insn
= head
; insn
!= next_tail
; insn
= NEXT_INSN (insn
))
3179 rtx prev
, next
, link
;
3181 /* Farm out notes. This is needed to keep the debugger from
3182 getting completely deranged. */
3183 if (GET_CODE (insn
) == NOTE
)
3186 insn
= unlink_notes (insn
, next_tail
);
3191 if (insn
== next_tail
)
3195 if (reload_completed
== 0
3196 && GET_RTX_CLASS (GET_CODE (insn
)) == 'i')
3198 /* See if the register gets born here. */
3199 /* We must check for registers being born before we check for
3200 registers dying. It is possible for a register to be born and
3201 die in the same insn, e.g. reading from a volatile memory
3202 location into an otherwise unused register. Such a register
3203 must be marked as dead after this insn. */
3204 if (GET_CODE (PATTERN (insn
)) == SET
3205 || GET_CODE (PATTERN (insn
)) == CLOBBER
)
3206 sched_note_set (b
, PATTERN (insn
), 0);
3207 else if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
3210 for (j
= XVECLEN (PATTERN (insn
), 0) - 1; j
>= 0; j
--)
3211 if (GET_CODE (XVECEXP (PATTERN (insn
), 0, j
)) == SET
3212 || GET_CODE (XVECEXP (PATTERN (insn
), 0, j
)) == CLOBBER
)
3213 sched_note_set (b
, XVECEXP (PATTERN (insn
), 0, j
), 0);
3215 /* ??? This code is obsolete and should be deleted. It
3216 is harmless though, so we will leave it in for now. */
3217 for (j
= XVECLEN (PATTERN (insn
), 0) - 1; j
>= 0; j
--)
3218 if (GET_CODE (XVECEXP (PATTERN (insn
), 0, j
)) == USE
)
3219 sched_note_set (b
, XVECEXP (PATTERN (insn
), 0, j
), 0);
3222 /* Need to know what registers this insn kills. */
3223 for (prev
= 0, link
= REG_NOTES (insn
); link
; link
= next
)
3227 next
= XEXP (link
, 1);
3228 if ((REG_NOTE_KIND (link
) == REG_DEAD
3229 || REG_NOTE_KIND (link
) == REG_UNUSED
)
3230 /* Verify that the REG_NOTE has a legal value. */
3231 && GET_CODE (XEXP (link
, 0)) == REG
)
3233 register int regno
= REGNO (XEXP (link
, 0));
3234 register int offset
= regno
/ REGSET_ELT_BITS
;
3235 register REGSET_ELT_TYPE bit
3236 = (REGSET_ELT_TYPE
) 1 << (regno
% REGSET_ELT_BITS
);
3238 /* Only unlink REG_DEAD notes; leave REG_UNUSED notes
3240 if (REG_NOTE_KIND (link
) == REG_DEAD
)
3243 XEXP (prev
, 1) = next
;
3245 REG_NOTES (insn
) = next
;
3246 XEXP (link
, 1) = dead_notes
;
3252 if (regno
< FIRST_PSEUDO_REGISTER
)
3254 int j
= HARD_REGNO_NREGS (regno
,
3255 GET_MODE (XEXP (link
, 0)));
3258 offset
= (regno
+ j
) / REGSET_ELT_BITS
;
3259 bit
= ((REGSET_ELT_TYPE
) 1
3260 << ((regno
+ j
) % REGSET_ELT_BITS
));
3262 bb_live_regs
[offset
] &= ~bit
;
3263 bb_dead_regs
[offset
] |= bit
;
3268 bb_live_regs
[offset
] &= ~bit
;
3269 bb_dead_regs
[offset
] |= bit
;
3278 if (reload_completed
== 0)
3280 /* Keep track of register lives. */
3281 old_live_regs
= (regset
) alloca (regset_bytes
);
3283 = (struct sometimes
*) alloca (max_regno
* sizeof (struct sometimes
));
3286 /* Start with registers live at end. */
3287 for (j
= 0; j
< regset_size
; j
++)
3289 REGSET_ELT_TYPE live
= bb_live_regs
[j
];
3290 old_live_regs
[j
] = live
;
3293 register REGSET_ELT_TYPE bit
;
3294 for (bit
= 0; bit
< REGSET_ELT_BITS
; bit
++)
3295 if (live
& ((REGSET_ELT_TYPE
) 1 << bit
))
3296 sometimes_max
= new_sometimes_live (regs_sometimes_live
, j
,
3297 bit
, sometimes_max
);
3302 SCHED_SORT (ready
, n_ready
, 1);
3306 fprintf (file
, ";; ready list initially:\n;; ");
3307 for (i
= 0; i
< n_ready
; i
++)
3308 fprintf (file
, "%d ", INSN_UID (ready
[i
]));
3309 fprintf (file
, "\n\n");
3311 for (insn
= head
; insn
!= next_tail
; insn
= NEXT_INSN (insn
))
3312 if (INSN_PRIORITY (insn
) > 0)
3313 fprintf (file
, ";; insn[%4d]: priority = %4d, ref_count = %4d\n",
3314 INSN_UID (insn
), INSN_PRIORITY (insn
),
3315 INSN_REF_COUNT (insn
));
3318 /* Now HEAD and TAIL are going to become disconnected
3319 entirely from the insn chain. */
3322 /* Q_SIZE will always be zero here. */
3323 q_ptr
= 0; clock
= 0;
3324 bzero (insn_queue
, sizeof (insn_queue
));
3326 /* Now, perform list scheduling. */
3328 /* Where we start inserting insns is after TAIL. */
3331 new_needs
= (NEXT_INSN (prev_head
) == basic_block_head
[b
]
3332 ? NEED_HEAD
: NEED_NOTHING
);
3333 if (PREV_INSN (next_tail
) == basic_block_end
[b
])
3334 new_needs
|= NEED_TAIL
;
3336 new_ready
= n_ready
;
3337 while (sched_n_insns
< n_insns
)
3339 q_ptr
= NEXT_Q (q_ptr
); clock
++;
3341 /* Add all pending insns that can be scheduled without stalls to the
3343 for (insn
= insn_queue
[q_ptr
]; insn
; insn
= NEXT_INSN (insn
))
3346 fprintf (file
, ";; launching %d before %d with no stalls at T-%d\n",
3347 INSN_UID (insn
), INSN_UID (last
), clock
);
3348 ready
[new_ready
++] = insn
;
3351 insn_queue
[q_ptr
] = 0;
3353 /* If there are no ready insns, stall until one is ready and add all
3354 of the pending insns at that point to the ready list. */
3357 register int stalls
;
3359 for (stalls
= 1; stalls
< INSN_QUEUE_SIZE
; stalls
++)
3360 if (insn
= insn_queue
[NEXT_Q_AFTER (q_ptr
, stalls
)])
3362 for (; insn
; insn
= NEXT_INSN (insn
))
3365 fprintf (file
, ";; launching %d before %d with %d stalls at T-%d\n",
3366 INSN_UID (insn
), INSN_UID (last
), stalls
, clock
);
3367 ready
[new_ready
++] = insn
;
3370 insn_queue
[NEXT_Q_AFTER (q_ptr
, stalls
)] = 0;
3374 q_ptr
= NEXT_Q_AFTER (q_ptr
, stalls
); clock
+= stalls
;
3377 /* There should be some instructions waiting to fire. */
3383 fprintf (file
, ";; ready list at T-%d:", clock
);
3384 for (i
= 0; i
< new_ready
; i
++)
3385 fprintf (file
, " %d (%x)",
3386 INSN_UID (ready
[i
]), INSN_PRIORITY (ready
[i
]));
3389 /* Sort the ready list and choose the best insn to schedule. Select
3390 which insn should issue in this cycle and queue those that are
3391 blocked by function unit hazards.
3393 N_READY holds the number of items that were scheduled the last time,
3394 minus the one instruction scheduled on the last loop iteration; it
3395 is not modified for any other reason in this loop. */
3397 SCHED_SORT (ready
, new_ready
, n_ready
);
3398 if (MAX_BLOCKAGE
> 1)
3400 new_ready
= schedule_select (ready
, new_ready
, clock
, file
);
3404 fprintf (file
, "\n");
3408 n_ready
= new_ready
;
3409 last_scheduled_insn
= insn
= ready
[0];
3411 /* The first insn scheduled becomes the new tail. */
3417 fprintf (file
, ", now");
3418 for (i
= 0; i
< n_ready
; i
++)
3419 fprintf (file
, " %d", INSN_UID (ready
[i
]));
3420 fprintf (file
, "\n");
3423 if (DONE_PRIORITY_P (insn
))
3426 if (reload_completed
== 0)
3428 /* Process this insn, and each insn linked to this one which must
3429 be immediately output after this insn. */
3432 /* First we kill registers set by this insn, and then we
3433 make registers used by this insn live. This is the opposite
3434 order used above because we are traversing the instructions
3437 /* Strictly speaking, we should scan REG_UNUSED notes and make
3438 every register mentioned there live, however, we will just
3439 kill them again immediately below, so there doesn't seem to
3440 be any reason why we bother to do this. */
3442 /* See if this is the last notice we must take of a register. */
3443 if (GET_CODE (PATTERN (insn
)) == SET
3444 || GET_CODE (PATTERN (insn
)) == CLOBBER
)
3445 sched_note_set (b
, PATTERN (insn
), 1);
3446 else if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
3449 for (j
= XVECLEN (PATTERN (insn
), 0) - 1; j
>= 0; j
--)
3450 if (GET_CODE (XVECEXP (PATTERN (insn
), 0, j
)) == SET
3451 || GET_CODE (XVECEXP (PATTERN (insn
), 0, j
)) == CLOBBER
)
3452 sched_note_set (b
, XVECEXP (PATTERN (insn
), 0, j
), 1);
3455 /* This code keeps life analysis information up to date. */
3456 if (GET_CODE (insn
) == CALL_INSN
)
3458 register struct sometimes
*p
;
3460 /* A call kills all call used and global registers, except
3461 for those mentioned in the call pattern which will be
3462 made live again later. */
3463 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
3464 if (call_used_regs
[i
] || global_regs
[i
])
3466 register int offset
= i
/ REGSET_ELT_BITS
;
3467 register REGSET_ELT_TYPE bit
3468 = (REGSET_ELT_TYPE
) 1 << (i
% REGSET_ELT_BITS
);
3470 bb_live_regs
[offset
] &= ~bit
;
3471 bb_dead_regs
[offset
] |= bit
;
3474 /* Regs live at the time of a call instruction must not
3475 go in a register clobbered by calls. Record this for
3476 all regs now live. Note that insns which are born or
3477 die in a call do not cross a call, so this must be done
3478 after the killings (above) and before the births
3480 p
= regs_sometimes_live
;
3481 for (i
= 0; i
< sometimes_max
; i
++, p
++)
3482 if (bb_live_regs
[p
->offset
]
3483 & ((REGSET_ELT_TYPE
) 1 << p
->bit
))
3484 p
->calls_crossed
+= 1;
3487 /* Make every register used live, and add REG_DEAD notes for
3488 registers which were not live before we started. */
3489 attach_deaths_insn (insn
);
3491 /* Find registers now made live by that instruction. */
3492 for (i
= 0; i
< regset_size
; i
++)
3494 REGSET_ELT_TYPE diff
= bb_live_regs
[i
] & ~old_live_regs
[i
];
3498 old_live_regs
[i
] |= diff
;
3499 for (bit
= 0; bit
< REGSET_ELT_BITS
; bit
++)
3500 if (diff
& ((REGSET_ELT_TYPE
) 1 << bit
))
3502 = new_sometimes_live (regs_sometimes_live
, i
, bit
,
3507 /* Count lengths of all regs we are worrying about now,
3508 and handle registers no longer live. */
3510 for (i
= 0; i
< sometimes_max
; i
++)
3512 register struct sometimes
*p
= ®s_sometimes_live
[i
];
3513 int regno
= p
->offset
*REGSET_ELT_BITS
+ p
->bit
;
3515 p
->live_length
+= 1;
3517 if ((bb_live_regs
[p
->offset
]
3518 & ((REGSET_ELT_TYPE
) 1 << p
->bit
)) == 0)
3520 /* This is the end of one of this register's lifetime
3521 segments. Save the lifetime info collected so far,
3522 and clear its bit in the old_live_regs entry. */
3523 sched_reg_live_length
[regno
] += p
->live_length
;
3524 sched_reg_n_calls_crossed
[regno
] += p
->calls_crossed
;
3525 old_live_regs
[p
->offset
]
3526 &= ~((REGSET_ELT_TYPE
) 1 << p
->bit
);
3528 /* Delete the reg_sometimes_live entry for this reg by
3529 copying the last entry over top of it. */
3530 *p
= regs_sometimes_live
[--sometimes_max
];
3531 /* ...and decrement i so that this newly copied entry
3532 will be processed. */
3538 insn
= PREV_INSN (insn
);
3540 while (SCHED_GROUP_P (link
));
3542 /* Set INSN back to the insn we are scheduling now. */
3546 /* Schedule INSN. Remove it from the ready list. */
3551 NEXT_INSN (insn
) = last
;
3552 PREV_INSN (last
) = insn
;
3555 /* Everything that precedes INSN now either becomes "ready", if
3556 it can execute immediately before INSN, or "pending", if
3557 there must be a delay. Give INSN high enough priority that
3558 at least one (maybe more) reg-killing insns can be launched
3559 ahead of all others. Mark INSN as scheduled by changing its
3561 INSN_PRIORITY (insn
) = LAUNCH_PRIORITY
;
3562 new_ready
= schedule_insn (insn
, ready
, n_ready
, clock
);
3563 INSN_PRIORITY (insn
) = DONE_PRIORITY
;
3565 /* Schedule all prior insns that must not be moved. */
3566 if (SCHED_GROUP_P (insn
))
3568 /* Disable these insns from being launched. */
3570 while (SCHED_GROUP_P (link
))
3572 /* Disable these insns from being launched by anybody. */
3573 link
= PREV_INSN (link
);
3574 INSN_REF_COUNT (link
) = 0;
3577 /* None of these insns can move forward into delay slots. */
3578 while (SCHED_GROUP_P (insn
))
3580 insn
= PREV_INSN (insn
);
3581 new_ready
= schedule_insn (insn
, ready
, new_ready
, clock
);
3582 INSN_PRIORITY (insn
) = DONE_PRIORITY
;
3585 NEXT_INSN (insn
) = last
;
3586 PREV_INSN (last
) = insn
;
3594 if (reload_completed
== 0)
3595 finish_sometimes_live (regs_sometimes_live
, sometimes_max
);
3597 /* HEAD is now the first insn in the chain of insns that
3598 been scheduled by the loop above.
3599 TAIL is the last of those insns. */
3602 /* NOTE_LIST is the end of a chain of notes previously found
3603 among the insns. Insert them at the beginning of the insns. */
3606 rtx note_head
= note_list
;
3607 while (PREV_INSN (note_head
))
3608 note_head
= PREV_INSN (note_head
);
3610 PREV_INSN (head
) = note_list
;
3611 NEXT_INSN (note_list
) = head
;
3615 /* In theory, there should be no REG_DEAD notes leftover at the end.
3616 In practice, this can occur as the result of bugs in flow, combine.c,
3617 and/or sched.c. The values of the REG_DEAD notes remaining are
3618 meaningless, because dead_notes is just used as a free list. */
3620 if (dead_notes
!= 0)
3624 if (new_needs
& NEED_HEAD
)
3625 basic_block_head
[b
] = head
;
3626 PREV_INSN (head
) = prev_head
;
3627 NEXT_INSN (prev_head
) = head
;
3629 if (new_needs
& NEED_TAIL
)
3630 basic_block_end
[b
] = tail
;
3631 NEXT_INSN (tail
) = next_tail
;
3632 PREV_INSN (next_tail
) = tail
;
3634 /* Restore the line-number notes of each insn. */
3635 if (write_symbols
!= NO_DEBUG
)
3637 rtx line
, note
, prev
, new;
3640 head
= basic_block_head
[b
];
3641 next_tail
= NEXT_INSN (basic_block_end
[b
]);
3643 /* Determine the current line-number. We want to know the current
3644 line number of the first insn of the block here, in case it is
3645 different from the true line number that was saved earlier. If
3646 different, then we need a line number note before the first insn
3647 of this block. If it happens to be the same, then we don't want to
3648 emit another line number note here. */
3649 for (line
= head
; line
; line
= PREV_INSN (line
))
3650 if (GET_CODE (line
) == NOTE
&& NOTE_LINE_NUMBER (line
) > 0)
3653 /* Walk the insns keeping track of the current line-number and inserting
3654 the line-number notes as needed. */
3655 for (insn
= head
; insn
!= next_tail
; insn
= NEXT_INSN (insn
))
3656 if (GET_CODE (insn
) == NOTE
&& NOTE_LINE_NUMBER (insn
) > 0)
3658 else if (! (GET_CODE (insn
) == NOTE
3659 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_DELETED
)
3660 && (note
= LINE_NOTE (insn
)) != 0
3663 || NOTE_LINE_NUMBER (note
) != NOTE_LINE_NUMBER (line
)
3664 || NOTE_SOURCE_FILE (note
) != NOTE_SOURCE_FILE (line
)))
3667 prev
= PREV_INSN (insn
);
3668 if (LINE_NOTE (note
))
3670 /* Re-use the original line-number note. */
3671 LINE_NOTE (note
) = 0;
3672 PREV_INSN (note
) = prev
;
3673 NEXT_INSN (prev
) = note
;
3674 PREV_INSN (insn
) = note
;
3675 NEXT_INSN (note
) = insn
;
3680 new = emit_note_after (NOTE_LINE_NUMBER (note
), prev
);
3681 NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note
);
3685 fprintf (file
, ";; added %d line-number notes\n", notes
);
3690 fprintf (file
, ";; total time = %d\n;; new basic block head = %d\n;; new basic block end = %d\n\n",
3691 clock
, INSN_UID (basic_block_head
[b
]), INSN_UID (basic_block_end
[b
]));
3694 /* Yow! We're done! */
3695 free_pending_lists ();
3700 /* Subroutine of split_hard_reg_notes. Searches X for any reference to
3701 REGNO, returning the rtx of the reference found if any. Otherwise,
3705 regno_use_in (regno
, x
)
3713 if (GET_CODE (x
) == REG
&& REGNO (x
) == regno
)
3716 fmt
= GET_RTX_FORMAT (GET_CODE (x
));
3717 for (i
= GET_RTX_LENGTH (GET_CODE (x
)) - 1; i
>= 0; i
--)
3721 if (tem
= regno_use_in (regno
, XEXP (x
, i
)))
3724 else if (fmt
[i
] == 'E')
3725 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
3726 if (tem
= regno_use_in (regno
, XVECEXP (x
, i
, j
)))
3733 /* Subroutine of update_flow_info. Determines whether any new REG_NOTEs are
3734 needed for the hard register mentioned in the note. This can happen
3735 if the reference to the hard register in the original insn was split into
3736 several smaller hard register references in the split insns. */
3739 split_hard_reg_notes (note
, first
, last
, orig_insn
)
3740 rtx note
, first
, last
, orig_insn
;
3742 rtx reg
, temp
, link
;
3743 int n_regs
, i
, new_reg
;
3746 /* Assume that this is a REG_DEAD note. */
3747 if (REG_NOTE_KIND (note
) != REG_DEAD
)
3750 reg
= XEXP (note
, 0);
3752 n_regs
= HARD_REGNO_NREGS (REGNO (reg
), GET_MODE (reg
));
3754 /* ??? Could add check here to see whether, the hard register is referenced
3755 in the same mode as in the original insn. If so, then it has not been
3756 split, and the rest of the code below is unnecessary. */
3758 for (i
= 1; i
< n_regs
; i
++)
3760 new_reg
= REGNO (reg
) + i
;
3762 /* Check for references to new_reg in the split insns. */
3763 for (insn
= last
; ; insn
= PREV_INSN (insn
))
3765 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i'
3766 && (temp
= regno_use_in (new_reg
, PATTERN (insn
))))
3768 /* Create a new reg dead note here. */
3769 link
= rtx_alloc (EXPR_LIST
);
3770 PUT_REG_NOTE_KIND (link
, REG_DEAD
);
3771 XEXP (link
, 0) = temp
;
3772 XEXP (link
, 1) = REG_NOTES (insn
);
3773 REG_NOTES (insn
) = link
;
3776 /* It isn't mentioned anywhere, so no new reg note is needed for
3784 /* Subroutine of update_flow_info. Determines whether a SET or CLOBBER in an
3785 insn created by splitting needs a REG_DEAD or REG_UNUSED note added. */
3788 new_insn_dead_notes (pat
, insn
, last
, orig_insn
)
3789 rtx pat
, insn
, last
, orig_insn
;
3793 /* PAT is either a CLOBBER or a SET here. */
3794 dest
= XEXP (pat
, 0);
3796 while (GET_CODE (dest
) == ZERO_EXTRACT
|| GET_CODE (dest
) == SUBREG
3797 || GET_CODE (dest
) == STRICT_LOW_PART
3798 || GET_CODE (dest
) == SIGN_EXTRACT
)
3799 dest
= XEXP (dest
, 0);
3801 if (GET_CODE (dest
) == REG
)
3803 for (tem
= last
; tem
!= insn
; tem
= PREV_INSN (tem
))
3805 if (GET_RTX_CLASS (GET_CODE (tem
)) == 'i'
3806 && reg_overlap_mentioned_p (dest
, PATTERN (tem
))
3807 && (set
= single_set (tem
)))
3809 rtx tem_dest
= SET_DEST (set
);
3811 while (GET_CODE (tem_dest
) == ZERO_EXTRACT
3812 || GET_CODE (tem_dest
) == SUBREG
3813 || GET_CODE (tem_dest
) == STRICT_LOW_PART
3814 || GET_CODE (tem_dest
) == SIGN_EXTRACT
)
3815 tem_dest
= XEXP (tem_dest
, 0);
3817 if (tem_dest
!= dest
)
3819 /* Use the same scheme as combine.c, don't put both REG_DEAD
3820 and REG_UNUSED notes on the same insn. */
3821 if (! find_regno_note (tem
, REG_UNUSED
, REGNO (dest
))
3822 && ! find_regno_note (tem
, REG_DEAD
, REGNO (dest
)))
3824 rtx note
= rtx_alloc (EXPR_LIST
);
3825 PUT_REG_NOTE_KIND (note
, REG_DEAD
);
3826 XEXP (note
, 0) = dest
;
3827 XEXP (note
, 1) = REG_NOTES (tem
);
3828 REG_NOTES (tem
) = note
;
3830 /* The reg only dies in one insn, the last one that uses
3834 else if (reg_overlap_mentioned_p (dest
, SET_SRC (set
)))
3835 /* We found an instruction that both uses the register,
3836 and sets it, so no new REG_NOTE is needed for this set. */
3840 /* If this is a set, it must die somewhere, unless it is the dest of
3841 the original insn, and hence is live after the original insn. Abort
3842 if it isn't supposed to be live after the original insn.
3844 If this is a clobber, then just add a REG_UNUSED note. */
3847 int live_after_orig_insn
= 0;
3848 rtx pattern
= PATTERN (orig_insn
);
3851 if (GET_CODE (pat
) == CLOBBER
)
3853 rtx note
= rtx_alloc (EXPR_LIST
);
3854 PUT_REG_NOTE_KIND (note
, REG_UNUSED
);
3855 XEXP (note
, 0) = dest
;
3856 XEXP (note
, 1) = REG_NOTES (insn
);
3857 REG_NOTES (insn
) = note
;
3861 /* The original insn could have multiple sets, so search the
3862 insn for all sets. */
3863 if (GET_CODE (pattern
) == SET
)
3865 if (reg_overlap_mentioned_p (dest
, SET_DEST (pattern
)))
3866 live_after_orig_insn
= 1;
3868 else if (GET_CODE (pattern
) == PARALLEL
)
3870 for (i
= 0; i
< XVECLEN (pattern
, 0); i
++)
3871 if (GET_CODE (XVECEXP (pattern
, 0, i
)) == SET
3872 && reg_overlap_mentioned_p (dest
,
3873 SET_DEST (XVECEXP (pattern
,
3875 live_after_orig_insn
= 1;
3878 if (! live_after_orig_insn
)
3884 /* Subroutine of update_flow_info. Update the value of reg_n_sets for all
3885 registers modified by X. INC is -1 if the containing insn is being deleted,
3886 and is 1 if the containing insn is a newly generated insn. */
3889 update_n_sets (x
, inc
)
3893 rtx dest
= SET_DEST (x
);
3895 while (GET_CODE (dest
) == STRICT_LOW_PART
|| GET_CODE (dest
) == SUBREG
3896 || GET_CODE (dest
) == ZERO_EXTRACT
|| GET_CODE (dest
) == SIGN_EXTRACT
)
3897 dest
= SUBREG_REG (dest
);
3899 if (GET_CODE (dest
) == REG
)
3901 int regno
= REGNO (dest
);
3903 if (regno
< FIRST_PSEUDO_REGISTER
)
3906 int endregno
= regno
+ HARD_REGNO_NREGS (regno
, GET_MODE (dest
));
3908 for (i
= regno
; i
< endregno
; i
++)
3909 reg_n_sets
[i
] += inc
;
3912 reg_n_sets
[regno
] += inc
;
3916 /* Updates all flow-analysis related quantities (including REG_NOTES) for
3917 the insns from FIRST to LAST inclusive that were created by splitting
3918 ORIG_INSN. NOTES are the original REG_NOTES. */
3921 update_flow_info (notes
, first
, last
, orig_insn
)
3928 rtx orig_dest
, temp
;
3931 /* Get and save the destination set by the original insn. */
3933 orig_dest
= single_set (orig_insn
);
3935 orig_dest
= SET_DEST (orig_dest
);
3937 /* Move REG_NOTES from the original insn to where they now belong. */
3939 for (note
= notes
; note
; note
= next
)
3941 next
= XEXP (note
, 1);
3942 switch (REG_NOTE_KIND (note
))
3946 /* Move these notes from the original insn to the last new insn where
3947 the register is now set. */
3949 for (insn
= last
; ; insn
= PREV_INSN (insn
))
3951 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i'
3952 && reg_mentioned_p (XEXP (note
, 0), PATTERN (insn
)))
3954 XEXP (note
, 1) = REG_NOTES (insn
);
3955 REG_NOTES (insn
) = note
;
3957 /* Sometimes need to convert REG_UNUSED notes to REG_DEAD
3959 /* ??? This won't handle multiple word registers correctly,
3960 but should be good enough for now. */
3961 if (REG_NOTE_KIND (note
) == REG_UNUSED
3962 && ! dead_or_set_p (insn
, XEXP (note
, 0)))
3963 PUT_REG_NOTE_KIND (note
, REG_DEAD
);
3965 /* The reg only dies in one insn, the last one that uses
3969 /* It must die somewhere, fail it we couldn't find where it died.
3971 If this is a REG_UNUSED note, then it must be a temporary
3972 register that was not needed by this instantiation of the
3973 pattern, so we can safely ignore it. */
3976 if (REG_NOTE_KIND (note
) != REG_UNUSED
)
3983 /* If this note refers to a multiple word hard register, it may
3984 have been split into several smaller hard register references.
3985 Check to see if there are any new register references that
3986 need REG_NOTES added for them. */
3987 temp
= XEXP (note
, 0);
3988 if (REG_NOTE_KIND (note
) == REG_DEAD
3989 && GET_CODE (temp
) == REG
3990 && REGNO (temp
) < FIRST_PSEUDO_REGISTER
3991 && HARD_REGNO_NREGS (REGNO (temp
), GET_MODE (temp
)))
3992 split_hard_reg_notes (note
, first
, last
, orig_insn
);
3996 /* This note applies to the dest of the original insn. Find the
3997 first new insn that now has the same dest, and move the note
4003 for (insn
= first
; ; insn
= NEXT_INSN (insn
))
4005 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i'
4006 && (temp
= single_set (insn
))
4007 && rtx_equal_p (SET_DEST (temp
), orig_dest
))
4009 XEXP (note
, 1) = REG_NOTES (insn
);
4010 REG_NOTES (insn
) = note
;
4011 /* The reg is only zero before one insn, the first that
4015 /* It must be set somewhere, fail if we couldn't find where it
4024 /* A REG_EQUIV or REG_EQUAL note on an insn with more than one
4025 set is meaningless. Just drop the note. */
4029 case REG_NO_CONFLICT
:
4030 /* These notes apply to the dest of the original insn. Find the last
4031 new insn that now has the same dest, and move the note there. */
4036 for (insn
= last
; ; insn
= PREV_INSN (insn
))
4038 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i'
4039 && (temp
= single_set (insn
))
4040 && rtx_equal_p (SET_DEST (temp
), orig_dest
))
4042 XEXP (note
, 1) = REG_NOTES (insn
);
4043 REG_NOTES (insn
) = note
;
4044 /* Only put this note on one of the new insns. */
4048 /* The original dest must still be set someplace. Abort if we
4049 couldn't find it. */
4056 /* Move a REG_LIBCALL note to the first insn created, and update
4057 the corresponding REG_RETVAL note. */
4058 XEXP (note
, 1) = REG_NOTES (first
);
4059 REG_NOTES (first
) = note
;
4061 insn
= XEXP (note
, 0);
4062 note
= find_reg_note (insn
, REG_RETVAL
, NULL_RTX
);
4064 XEXP (note
, 0) = first
;
4068 /* Move a REG_RETVAL note to the last insn created, and update
4069 the corresponding REG_LIBCALL note. */
4070 XEXP (note
, 1) = REG_NOTES (last
);
4071 REG_NOTES (last
) = note
;
4073 insn
= XEXP (note
, 0);
4074 note
= find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
);
4076 XEXP (note
, 0) = last
;
4080 /* This should be moved to whichever instruction is a JUMP_INSN. */
4082 for (insn
= last
; ; insn
= PREV_INSN (insn
))
4084 if (GET_CODE (insn
) == JUMP_INSN
)
4086 XEXP (note
, 1) = REG_NOTES (insn
);
4087 REG_NOTES (insn
) = note
;
4088 /* Only put this note on one of the new insns. */
4091 /* Fail if we couldn't find a JUMP_INSN. */
4098 /* This should be moved to whichever instruction now has the
4099 increment operation. */
4103 /* Should be moved to the new insn(s) which use the label. */
4104 for (insn
= first
; insn
!= NEXT_INSN (last
); insn
= NEXT_INSN (insn
))
4105 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i'
4106 && reg_mentioned_p (XEXP (note
, 0), PATTERN (insn
)))
4107 REG_NOTES (insn
) = gen_rtx (EXPR_LIST
, REG_LABEL
,
4108 XEXP (note
, 0), REG_NOTES (insn
));
4113 /* These two notes will never appear until after reorg, so we don't
4114 have to handle them here. */
4120 /* Each new insn created, except the last, has a new set. If the destination
4121 is a register, then this reg is now live across several insns, whereas
4122 previously the dest reg was born and died within the same insn. To
4123 reflect this, we now need a REG_DEAD note on the insn where this
4126 Similarly, the new insns may have clobbers that need REG_UNUSED notes. */
4128 for (insn
= first
; insn
!= last
; insn
= NEXT_INSN (insn
))
4133 pat
= PATTERN (insn
);
4134 if (GET_CODE (pat
) == SET
|| GET_CODE (pat
) == CLOBBER
)
4135 new_insn_dead_notes (pat
, insn
, last
, orig_insn
);
4136 else if (GET_CODE (pat
) == PARALLEL
)
4138 for (i
= 0; i
< XVECLEN (pat
, 0); i
++)
4139 if (GET_CODE (XVECEXP (pat
, 0, i
)) == SET
4140 || GET_CODE (XVECEXP (pat
, 0, i
)) == CLOBBER
)
4141 new_insn_dead_notes (XVECEXP (pat
, 0, i
), insn
, last
, orig_insn
);
4145 /* If any insn, except the last, uses the register set by the last insn,
4146 then we need a new REG_DEAD note on that insn. In this case, there
4147 would not have been a REG_DEAD note for this register in the original
4148 insn because it was used and set within one insn.
4150 There is no new REG_DEAD note needed if the last insn uses the register
4151 that it is setting. */
4153 set
= single_set (last
);
4156 rtx dest
= SET_DEST (set
);
4158 while (GET_CODE (dest
) == ZERO_EXTRACT
|| GET_CODE (dest
) == SUBREG
4159 || GET_CODE (dest
) == STRICT_LOW_PART
4160 || GET_CODE (dest
) == SIGN_EXTRACT
)
4161 dest
= XEXP (dest
, 0);
4163 if (GET_CODE (dest
) == REG
4164 && ! reg_overlap_mentioned_p (dest
, SET_SRC (set
)))
4166 for (insn
= PREV_INSN (last
); ; insn
= PREV_INSN (insn
))
4168 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i'
4169 && reg_mentioned_p (dest
, PATTERN (insn
))
4170 && (set
= single_set (insn
)))
4172 rtx insn_dest
= SET_DEST (set
);
4174 while (GET_CODE (insn_dest
) == ZERO_EXTRACT
4175 || GET_CODE (insn_dest
) == SUBREG
4176 || GET_CODE (insn_dest
) == STRICT_LOW_PART
4177 || GET_CODE (insn_dest
) == SIGN_EXTRACT
)
4178 insn_dest
= XEXP (insn_dest
, 0);
4180 if (insn_dest
!= dest
)
4182 note
= rtx_alloc (EXPR_LIST
);
4183 PUT_REG_NOTE_KIND (note
, REG_DEAD
);
4184 XEXP (note
, 0) = dest
;
4185 XEXP (note
, 1) = REG_NOTES (insn
);
4186 REG_NOTES (insn
) = note
;
4187 /* The reg only dies in one insn, the last one
4198 /* If the original dest is modifying a multiple register target, and the
4199 original instruction was split such that the original dest is now set
4200 by two or more SUBREG sets, then the split insns no longer kill the
4201 destination of the original insn.
4203 In this case, if there exists an instruction in the same basic block,
4204 before the split insn, which uses the original dest, and this use is
4205 killed by the original insn, then we must remove the REG_DEAD note on
4206 this insn, because it is now superfluous.
4208 This does not apply when a hard register gets split, because the code
4209 knows how to handle overlapping hard registers properly. */
4210 if (orig_dest
&& GET_CODE (orig_dest
) == REG
)
4212 int found_orig_dest
= 0;
4213 int found_split_dest
= 0;
4215 for (insn
= first
; ; insn
= NEXT_INSN (insn
))
4217 set
= single_set (insn
);
4220 if (GET_CODE (SET_DEST (set
)) == REG
4221 && REGNO (SET_DEST (set
)) == REGNO (orig_dest
))
4223 found_orig_dest
= 1;
4226 else if (GET_CODE (SET_DEST (set
)) == SUBREG
4227 && SUBREG_REG (SET_DEST (set
)) == orig_dest
)
4229 found_split_dest
= 1;
4238 if (found_split_dest
)
4240 /* Search backwards from FIRST, looking for the first insn that uses
4241 the original dest. Stop if we pass a CODE_LABEL or a JUMP_INSN.
4242 If we find an insn, and it has a REG_DEAD note, then delete the
4245 for (insn
= first
; insn
; insn
= PREV_INSN (insn
))
4247 if (GET_CODE (insn
) == CODE_LABEL
4248 || GET_CODE (insn
) == JUMP_INSN
)
4250 else if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i'
4251 && reg_mentioned_p (orig_dest
, insn
))
4253 note
= find_regno_note (insn
, REG_DEAD
, REGNO (orig_dest
));
4255 remove_note (insn
, note
);
4259 else if (! found_orig_dest
)
4261 /* This should never happen. */
4266 /* Update reg_n_sets. This is necessary to prevent local alloc from
4267 converting REG_EQUAL notes to REG_EQUIV when splitting has modified
4268 a reg from set once to set multiple times. */
4271 rtx x
= PATTERN (orig_insn
);
4272 RTX_CODE code
= GET_CODE (x
);
4274 if (code
== SET
|| code
== CLOBBER
)
4275 update_n_sets (x
, -1);
4276 else if (code
== PARALLEL
)
4279 for (i
= XVECLEN (x
, 0) - 1; i
>= 0; i
--)
4281 code
= GET_CODE (XVECEXP (x
, 0, i
));
4282 if (code
== SET
|| code
== CLOBBER
)
4283 update_n_sets (XVECEXP (x
, 0, i
), -1);
4287 for (insn
= first
; ; insn
= NEXT_INSN (insn
))
4290 code
= GET_CODE (x
);
4292 if (code
== SET
|| code
== CLOBBER
)
4293 update_n_sets (x
, 1);
4294 else if (code
== PARALLEL
)
4297 for (i
= XVECLEN (x
, 0) - 1; i
>= 0; i
--)
4299 code
= GET_CODE (XVECEXP (x
, 0, i
));
4300 if (code
== SET
|| code
== CLOBBER
)
4301 update_n_sets (XVECEXP (x
, 0, i
), 1);
4311 /* The one entry point in this file. DUMP_FILE is the dump file for
4315 schedule_insns (dump_file
)
4318 int max_uid
= MAX_INSNS_PER_SPLIT
* (get_max_uid () + 1);
4322 /* Taking care of this degenerate case makes the rest of
4323 this code simpler. */
4324 if (n_basic_blocks
== 0)
4327 /* Create an insn here so that we can hang dependencies off of it later. */
4328 sched_before_next_call
4329 = gen_rtx (INSN
, VOIDmode
, 0, NULL_RTX
, NULL_RTX
,
4330 NULL_RTX
, 0, NULL_RTX
, 0);
4332 /* Initialize the unused_*_lists. We can't use the ones left over from
4333 the previous function, because gcc has freed that memory. We can use
4334 the ones left over from the first sched pass in the second pass however,
4335 so only clear them on the first sched pass. The first pass is before
4336 reload if flag_schedule_insns is set, otherwise it is afterwards. */
4338 if (reload_completed
== 0 || ! flag_schedule_insns
)
4340 unused_insn_list
= 0;
4341 unused_expr_list
= 0;
4344 /* We create no insns here, only reorder them, so we
4345 remember how far we can cut back the stack on exit. */
4347 /* Allocate data for this pass. See comments, above,
4348 for what these vectors do. */
4349 insn_luid
= (int *) alloca (max_uid
* sizeof (int));
4350 insn_priority
= (int *) alloca (max_uid
* sizeof (int));
4351 insn_tick
= (int *) alloca (max_uid
* sizeof (int));
4352 insn_costs
= (short *) alloca (max_uid
* sizeof (short));
4353 insn_units
= (short *) alloca (max_uid
* sizeof (short));
4354 insn_blockage
= (unsigned int *) alloca (max_uid
* sizeof (unsigned int));
4355 insn_ref_count
= (int *) alloca (max_uid
* sizeof (int));
4357 if (reload_completed
== 0)
4359 sched_reg_n_deaths
= (short *) alloca (max_regno
* sizeof (short));
4360 sched_reg_n_calls_crossed
= (int *) alloca (max_regno
* sizeof (int));
4361 sched_reg_live_length
= (int *) alloca (max_regno
* sizeof (int));
4362 bb_dead_regs
= (regset
) alloca (regset_bytes
);
4363 bb_live_regs
= (regset
) alloca (regset_bytes
);
4364 bzero (sched_reg_n_calls_crossed
, max_regno
* sizeof (int));
4365 bzero (sched_reg_live_length
, max_regno
* sizeof (int));
4366 bcopy (reg_n_deaths
, sched_reg_n_deaths
, max_regno
* sizeof (short));
4367 init_alias_analysis ();
4371 sched_reg_n_deaths
= 0;
4372 sched_reg_n_calls_crossed
= 0;
4373 sched_reg_live_length
= 0;
4376 if (! flag_schedule_insns
)
4377 init_alias_analysis ();
4380 if (write_symbols
!= NO_DEBUG
)
4384 line_note
= (rtx
*) alloca (max_uid
* sizeof (rtx
));
4385 bzero (line_note
, max_uid
* sizeof (rtx
));
4386 line_note_head
= (rtx
*) alloca (n_basic_blocks
* sizeof (rtx
));
4387 bzero (line_note_head
, n_basic_blocks
* sizeof (rtx
));
4389 /* Determine the line-number at the start of each basic block.
4390 This must be computed and saved now, because after a basic block's
4391 predecessor has been scheduled, it is impossible to accurately
4392 determine the correct line number for the first insn of the block. */
4394 for (b
= 0; b
< n_basic_blocks
; b
++)
4395 for (line
= basic_block_head
[b
]; line
; line
= PREV_INSN (line
))
4396 if (GET_CODE (line
) == NOTE
&& NOTE_LINE_NUMBER (line
) > 0)
4398 line_note_head
[b
] = line
;
4403 bzero (insn_luid
, max_uid
* sizeof (int));
4404 bzero (insn_priority
, max_uid
* sizeof (int));
4405 bzero (insn_tick
, max_uid
* sizeof (int));
4406 bzero (insn_costs
, max_uid
* sizeof (short));
4407 bzero (insn_units
, max_uid
* sizeof (short));
4408 bzero (insn_blockage
, max_uid
* sizeof (unsigned int));
4409 bzero (insn_ref_count
, max_uid
* sizeof (int));
4411 /* Schedule each basic block, block by block. */
4413 if (NEXT_INSN (basic_block_end
[n_basic_blocks
-1]) == 0
4414 || (GET_CODE (basic_block_end
[n_basic_blocks
-1]) != NOTE
4415 && GET_CODE (basic_block_end
[n_basic_blocks
-1]) != CODE_LABEL
))
4416 emit_note_after (NOTE_INSN_DELETED
, basic_block_end
[n_basic_blocks
-1]);
4418 for (b
= 0; b
< n_basic_blocks
; b
++)
4425 for (insn
= basic_block_head
[b
]; ; insn
= next
)
4430 /* Can't use `next_real_insn' because that
4431 might go across CODE_LABELS and short-out basic blocks. */
4432 next
= NEXT_INSN (insn
);
4433 if (GET_CODE (insn
) != INSN
)
4435 if (insn
== basic_block_end
[b
])
4441 /* Don't split no-op move insns. These should silently disappear
4442 later in final. Splitting such insns would break the code
4443 that handles REG_NO_CONFLICT blocks. */
4444 set
= single_set (insn
);
4445 if (set
&& rtx_equal_p (SET_SRC (set
), SET_DEST (set
)))
4447 if (insn
== basic_block_end
[b
])
4450 /* Nops get in the way while scheduling, so delete them now if
4451 register allocation has already been done. It is too risky
4452 to try to do this before register allocation, and there are
4453 unlikely to be very many nops then anyways. */
4454 if (reload_completed
)
4456 PUT_CODE (insn
, NOTE
);
4457 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
4458 NOTE_SOURCE_FILE (insn
) = 0;
4464 /* Split insns here to get max fine-grain parallelism. */
4465 prev
= PREV_INSN (insn
);
4466 if (reload_completed
== 0)
4468 rtx last
, first
= PREV_INSN (insn
);
4469 rtx notes
= REG_NOTES (insn
);
4471 last
= try_split (PATTERN (insn
), insn
, 1);
4474 /* try_split returns the NOTE that INSN became. */
4475 first
= NEXT_INSN (first
);
4476 update_flow_info (notes
, first
, last
, insn
);
4478 PUT_CODE (insn
, NOTE
);
4479 NOTE_SOURCE_FILE (insn
) = 0;
4480 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
4481 if (insn
== basic_block_head
[b
])
4482 basic_block_head
[b
] = first
;
4483 if (insn
== basic_block_end
[b
])
4485 basic_block_end
[b
] = last
;
4491 if (insn
== basic_block_end
[b
])
4495 schedule_block (b
, dump_file
);
4502 /* Reposition the prologue and epilogue notes in case we moved the
4503 prologue/epilogue insns. */
4504 if (reload_completed
)
4505 reposition_prologue_and_epilogue_notes (get_insns ());
4507 if (write_symbols
!= NO_DEBUG
)
4510 rtx insn
= get_insns ();
4511 int active_insn
= 0;
4514 /* Walk the insns deleting redundant line-number notes. Many of these
4515 are already present. The remainder tend to occur at basic
4516 block boundaries. */
4517 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
4518 if (GET_CODE (insn
) == NOTE
&& NOTE_LINE_NUMBER (insn
) > 0)
4520 /* If there are no active insns following, INSN is redundant. */
4521 if (active_insn
== 0)
4524 NOTE_SOURCE_FILE (insn
) = 0;
4525 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
4527 /* If the line number is unchanged, LINE is redundant. */
4529 && NOTE_LINE_NUMBER (line
) == NOTE_LINE_NUMBER (insn
)
4530 && NOTE_SOURCE_FILE (line
) == NOTE_SOURCE_FILE (insn
))
4533 NOTE_SOURCE_FILE (line
) = 0;
4534 NOTE_LINE_NUMBER (line
) = NOTE_INSN_DELETED
;
4541 else if (! ((GET_CODE (insn
) == NOTE
4542 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_DELETED
)
4543 || (GET_CODE (insn
) == INSN
4544 && (GET_CODE (PATTERN (insn
)) == USE
4545 || GET_CODE (PATTERN (insn
)) == CLOBBER
))))
4548 if (dump_file
&& notes
)
4549 fprintf (dump_file
, ";; deleted %d line-number notes\n", notes
);
4552 if (reload_completed
== 0)
4555 for (regno
= 0; regno
< max_regno
; regno
++)
4556 if (sched_reg_live_length
[regno
])
4560 if (reg_live_length
[regno
] > sched_reg_live_length
[regno
])
4562 ";; register %d life shortened from %d to %d\n",
4563 regno
, reg_live_length
[regno
],
4564 sched_reg_live_length
[regno
]);
4565 /* Negative values are special; don't overwrite the current
4566 reg_live_length value if it is negative. */
4567 else if (reg_live_length
[regno
] < sched_reg_live_length
[regno
]
4568 && reg_live_length
[regno
] >= 0)
4570 ";; register %d life extended from %d to %d\n",
4571 regno
, reg_live_length
[regno
],
4572 sched_reg_live_length
[regno
]);
4574 if (reg_n_calls_crossed
[regno
]
4575 && ! sched_reg_n_calls_crossed
[regno
])
4577 ";; register %d no longer crosses calls\n", regno
);
4578 else if (! reg_n_calls_crossed
[regno
]
4579 && sched_reg_n_calls_crossed
[regno
])
4581 ";; register %d now crosses calls\n", regno
);
4583 /* Negative values are special; don't overwrite the current
4584 reg_live_length value if it is negative. */
4585 if (reg_live_length
[regno
] >= 0)
4586 reg_live_length
[regno
] = sched_reg_live_length
[regno
];
4587 reg_n_calls_crossed
[regno
] = sched_reg_n_calls_crossed
[regno
];
4591 #endif /* INSN_SCHEDULING */