]> gcc.gnu.org Git - gcc.git/blob - gcc/sched.c
Comment change.
[gcc.git] / gcc / sched.c
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)
5
6 This file is part of GNU CC.
7
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)
11 any later version.
12
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.
17
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. */
21
22 /* Instruction scheduling pass.
23
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:
27
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.
36
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.
41
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
55 remaining slots.
56
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.
65
66 The following list shows the order in which we want to break ties
67 among insns in the ready list:
68
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.
74
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.
81
82 Dependencies set up by memory references are treated in exactly the
83 same way as other dependencies, by using LOG_LINKS.
84
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
92 utilization.
93
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.
98
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.
103
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.
107
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,
111 basic_block_end.
112
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. */
116 \f
117 #include <stdio.h>
118 #include "config.h"
119 #include "rtl.h"
120 #include "basic-block.h"
121 #include "regs.h"
122 #include "hard-reg-set.h"
123 #include "flags.h"
124 #include "insn-config.h"
125 #include "insn-attr.h"
126
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
131 unscheduled code.
132
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;
138
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;
145
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)])
149
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)])
153
154 static short *insn_costs;
155 #define INSN_COST(INSN) insn_costs[INSN_UID (INSN)]
156
157 /* Vector indexed by INSN_UID giving an encoding of the function units
158 used. */
159 static short *insn_units;
160 #define INSN_UNIT(INSN) insn_units[INSN_UID (INSN)]
161
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)]
166 #define UNIT_BITS 5
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)
176
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))
180
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)
187
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)])
191
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
194 reused. */
195 static rtx *line_note;
196 #define LINE_NOTE(INSN) (line_note[INSN_UID (INSN)])
197
198 /* Vector indexed by basic block number giving the starting line-number
199 for each basic block. */
200 static rtx *line_note_head;
201
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;
205
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;
211
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;
217
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;
223
224 /* Queues, etc. */
225
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:
230
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
234 time has passed.
235 (R) the "Ready" list of unscheduled, uncommitted insns.
236 (S) the "Scheduled" list of insns.
237
238 Initially, all insns are either "Pending" or "Ready" depending on
239 whether their dependencies are satisfied.
240
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.
249
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
254 `n_ready'.
255 The "Scheduled" list (S) is the new insn chain built by this pass.
256
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
261 committed insns.
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. */
266
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))
276
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)])
282
283 /* Forward declarations. */
284 static void sched_analyze_2 ();
285 static void schedule_block ();
286
287 /* Main entry point of this file. */
288 void schedule_insns ();
289 #endif /* INSN_SCHEDULING */
290 \f
291 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
292
293 /* Vector indexed by N giving the initial (unchanging) value known
294 for pseudo-register N. */
295 static rtx *reg_known_value;
296
297 /* Vector recording for each reg_known_value whether it is due to a
298 REG_EQUIV note. Future passes (viz., reload) may replace the
299 pseudo with the equivalent expression and so we account for the
300 dependences that would be introduced if that happens. */
301 /* ??? This is a problem only on the Convex. The REG_EQUIV notes created in
302 assign_parms mention the arg pointer, and there are explicit insns in the
303 RTL that modify the arg pointer. Thus we must ensure that such insns don't
304 get scheduled across each other because that would invalidate the REG_EQUIV
305 notes. One could argue that the REG_EQUIV notes are wrong, but solving
306 the problem in the scheduler will likely give better code, so we do it
307 here. */
308 static char *reg_known_equiv_p;
309
310 /* Indicates number of valid entries in reg_known_value. */
311 static int reg_known_value_size;
312
313 static rtx
314 canon_rtx (x)
315 rtx x;
316 {
317 if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
318 && REGNO (x) <= reg_known_value_size)
319 return reg_known_value[REGNO (x)];
320 else if (GET_CODE (x) == PLUS)
321 {
322 rtx x0 = canon_rtx (XEXP (x, 0));
323 rtx x1 = canon_rtx (XEXP (x, 1));
324
325 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
326 {
327 /* We can tolerate LO_SUMs being offset here; these
328 rtl are used for nothing other than comparisons. */
329 if (GET_CODE (x0) == CONST_INT)
330 return plus_constant_for_output (x1, INTVAL (x0));
331 else if (GET_CODE (x1) == CONST_INT)
332 return plus_constant_for_output (x0, INTVAL (x1));
333 return gen_rtx (PLUS, GET_MODE (x), x0, x1);
334 }
335 }
336 return x;
337 }
338
339 /* Set up all info needed to perform alias analysis on memory references. */
340
341 void
342 init_alias_analysis ()
343 {
344 int maxreg = max_reg_num ();
345 rtx insn;
346 rtx note;
347 rtx set;
348
349 reg_known_value_size = maxreg;
350
351 reg_known_value
352 = (rtx *) oballoc ((maxreg-FIRST_PSEUDO_REGISTER) * sizeof (rtx))
353 - FIRST_PSEUDO_REGISTER;
354 bzero (reg_known_value+FIRST_PSEUDO_REGISTER,
355 (maxreg-FIRST_PSEUDO_REGISTER) * sizeof (rtx));
356
357 reg_known_equiv_p
358 = (char *) oballoc ((maxreg-FIRST_PSEUDO_REGISTER) * sizeof (char))
359 - FIRST_PSEUDO_REGISTER;
360 bzero (reg_known_equiv_p+FIRST_PSEUDO_REGISTER,
361 (maxreg-FIRST_PSEUDO_REGISTER) * sizeof (char));
362
363 /* Fill in the entries with known constant values. */
364 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
365 if ((set = single_set (insn)) != 0
366 && GET_CODE (SET_DEST (set)) == REG
367 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
368 && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
369 && reg_n_sets[REGNO (SET_DEST (set))] == 1)
370 || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
371 && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
372 {
373 int regno = REGNO (SET_DEST (set));
374 reg_known_value[regno] = XEXP (note, 0);
375 reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
376 }
377
378 /* Fill in the remaining entries. */
379 while (--maxreg >= FIRST_PSEUDO_REGISTER)
380 if (reg_known_value[maxreg] == 0)
381 reg_known_value[maxreg] = regno_reg_rtx[maxreg];
382 }
383
384 /* Return 1 if X and Y are identical-looking rtx's.
385
386 We use the data in reg_known_value above to see if two registers with
387 different numbers are, in fact, equivalent. */
388
389 static int
390 rtx_equal_for_memref_p (x, y)
391 rtx x, y;
392 {
393 register int i;
394 register int j;
395 register enum rtx_code code;
396 register char *fmt;
397
398 if (x == 0 && y == 0)
399 return 1;
400 if (x == 0 || y == 0)
401 return 0;
402 x = canon_rtx (x);
403 y = canon_rtx (y);
404
405 if (x == y)
406 return 1;
407
408 code = GET_CODE (x);
409 /* Rtx's of different codes cannot be equal. */
410 if (code != GET_CODE (y))
411 return 0;
412
413 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
414 (REG:SI x) and (REG:HI x) are NOT equivalent. */
415
416 if (GET_MODE (x) != GET_MODE (y))
417 return 0;
418
419 /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively. */
420
421 if (code == REG)
422 return REGNO (x) == REGNO (y);
423 if (code == LABEL_REF)
424 return XEXP (x, 0) == XEXP (y, 0);
425 if (code == SYMBOL_REF)
426 return XSTR (x, 0) == XSTR (y, 0);
427
428 /* Compare the elements. If any pair of corresponding elements
429 fail to match, return 0 for the whole things. */
430
431 fmt = GET_RTX_FORMAT (code);
432 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
433 {
434 switch (fmt[i])
435 {
436 case 'w':
437 if (XWINT (x, i) != XWINT (y, i))
438 return 0;
439 break;
440
441 case 'n':
442 case 'i':
443 if (XINT (x, i) != XINT (y, i))
444 return 0;
445 break;
446
447 case 'V':
448 case 'E':
449 /* Two vectors must have the same length. */
450 if (XVECLEN (x, i) != XVECLEN (y, i))
451 return 0;
452
453 /* And the corresponding elements must match. */
454 for (j = 0; j < XVECLEN (x, i); j++)
455 if (rtx_equal_for_memref_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
456 return 0;
457 break;
458
459 case 'e':
460 if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
461 return 0;
462 break;
463
464 case 'S':
465 case 's':
466 if (strcmp (XSTR (x, i), XSTR (y, i)))
467 return 0;
468 break;
469
470 case 'u':
471 /* These are just backpointers, so they don't matter. */
472 break;
473
474 case '0':
475 break;
476
477 /* It is believed that rtx's at this level will never
478 contain anything but integers and other rtx's,
479 except for within LABEL_REFs and SYMBOL_REFs. */
480 default:
481 abort ();
482 }
483 }
484 return 1;
485 }
486
487 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
488 X and return it, or return 0 if none found. */
489
490 static rtx
491 find_symbolic_term (x)
492 rtx x;
493 {
494 register int i;
495 register enum rtx_code code;
496 register char *fmt;
497
498 code = GET_CODE (x);
499 if (code == SYMBOL_REF || code == LABEL_REF)
500 return x;
501 if (GET_RTX_CLASS (code) == 'o')
502 return 0;
503
504 fmt = GET_RTX_FORMAT (code);
505 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
506 {
507 rtx t;
508
509 if (fmt[i] == 'e')
510 {
511 t = find_symbolic_term (XEXP (x, i));
512 if (t != 0)
513 return t;
514 }
515 else if (fmt[i] == 'E')
516 break;
517 }
518 return 0;
519 }
520
521 /* Return nonzero if X and Y (memory addresses) could reference the
522 same location in memory. C is an offset accumulator. When
523 C is nonzero, we are testing aliases between X and Y + C.
524 XSIZE is the size in bytes of the X reference,
525 similarly YSIZE is the size in bytes for Y.
526
527 If XSIZE or YSIZE is zero, we do not know the amount of memory being
528 referenced (the reference was BLKmode), so make the most pessimistic
529 assumptions.
530
531 We recognize the following cases of non-conflicting memory:
532
533 (1) addresses involving the frame pointer cannot conflict
534 with addresses involving static variables.
535 (2) static variables with different addresses cannot conflict.
536
537 Nice to notice that varying addresses cannot conflict with fp if no
538 local variables had their addresses taken, but that's too hard now. */
539
540 /* ??? In Fortran, references to a array parameter can never conflict with
541 another array parameter. */
542
543 static int
544 memrefs_conflict_p (xsize, x, ysize, y, c)
545 rtx x, y;
546 int xsize, ysize;
547 HOST_WIDE_INT c;
548 {
549 if (GET_CODE (x) == HIGH)
550 x = XEXP (x, 0);
551 else if (GET_CODE (x) == LO_SUM)
552 x = XEXP (x, 1);
553 else
554 x = canon_rtx (x);
555 if (GET_CODE (y) == HIGH)
556 y = XEXP (y, 0);
557 else if (GET_CODE (y) == LO_SUM)
558 y = XEXP (y, 1);
559 else
560 y = canon_rtx (y);
561
562 if (rtx_equal_for_memref_p (x, y))
563 return (xsize == 0 || ysize == 0 ||
564 (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
565
566 if (y == frame_pointer_rtx || y == stack_pointer_rtx)
567 {
568 rtx t = y;
569 int tsize = ysize;
570 y = x; ysize = xsize;
571 x = t; xsize = tsize;
572 }
573
574 if (x == frame_pointer_rtx || x == stack_pointer_rtx)
575 {
576 rtx y1;
577
578 if (CONSTANT_P (y))
579 return 0;
580
581 if (GET_CODE (y) == PLUS
582 && canon_rtx (XEXP (y, 0)) == x
583 && (y1 = canon_rtx (XEXP (y, 1)))
584 && GET_CODE (y1) == CONST_INT)
585 {
586 c += INTVAL (y1);
587 return (xsize == 0 || ysize == 0
588 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
589 }
590
591 if (GET_CODE (y) == PLUS
592 && (y1 = canon_rtx (XEXP (y, 0)))
593 && CONSTANT_P (y1))
594 return 0;
595
596 return 1;
597 }
598
599 if (GET_CODE (x) == PLUS)
600 {
601 /* The fact that X is canonicalized means that this
602 PLUS rtx is canonicalized. */
603 rtx x0 = XEXP (x, 0);
604 rtx x1 = XEXP (x, 1);
605
606 if (GET_CODE (y) == PLUS)
607 {
608 /* The fact that Y is canonicalized means that this
609 PLUS rtx is canonicalized. */
610 rtx y0 = XEXP (y, 0);
611 rtx y1 = XEXP (y, 1);
612
613 if (rtx_equal_for_memref_p (x1, y1))
614 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
615 if (rtx_equal_for_memref_p (x0, y0))
616 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
617 if (GET_CODE (x1) == CONST_INT)
618 if (GET_CODE (y1) == CONST_INT)
619 return memrefs_conflict_p (xsize, x0, ysize, y0,
620 c - INTVAL (x1) + INTVAL (y1));
621 else
622 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
623 else if (GET_CODE (y1) == CONST_INT)
624 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
625
626 /* Handle case where we cannot understand iteration operators,
627 but we notice that the base addresses are distinct objects. */
628 x = find_symbolic_term (x);
629 if (x == 0)
630 return 1;
631 y = find_symbolic_term (y);
632 if (y == 0)
633 return 1;
634 return rtx_equal_for_memref_p (x, y);
635 }
636 else if (GET_CODE (x1) == CONST_INT)
637 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
638 }
639 else if (GET_CODE (y) == PLUS)
640 {
641 /* The fact that Y is canonicalized means that this
642 PLUS rtx is canonicalized. */
643 rtx y0 = XEXP (y, 0);
644 rtx y1 = XEXP (y, 1);
645
646 if (GET_CODE (y1) == CONST_INT)
647 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
648 else
649 return 1;
650 }
651
652 if (GET_CODE (x) == GET_CODE (y))
653 switch (GET_CODE (x))
654 {
655 case MULT:
656 {
657 /* Handle cases where we expect the second operands to be the
658 same, and check only whether the first operand would conflict
659 or not. */
660 rtx x0, y0;
661 rtx x1 = canon_rtx (XEXP (x, 1));
662 rtx y1 = canon_rtx (XEXP (y, 1));
663 if (! rtx_equal_for_memref_p (x1, y1))
664 return 1;
665 x0 = canon_rtx (XEXP (x, 0));
666 y0 = canon_rtx (XEXP (y, 0));
667 if (rtx_equal_for_memref_p (x0, y0))
668 return (xsize == 0 || ysize == 0
669 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
670
671 /* Can't properly adjust our sizes. */
672 if (GET_CODE (x1) != CONST_INT)
673 return 1;
674 xsize /= INTVAL (x1);
675 ysize /= INTVAL (x1);
676 c /= INTVAL (x1);
677 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
678 }
679 }
680
681 if (CONSTANT_P (x))
682 {
683 if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
684 {
685 c += (INTVAL (y) - INTVAL (x));
686 return (xsize == 0 || ysize == 0
687 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
688 }
689
690 if (GET_CODE (x) == CONST)
691 {
692 if (GET_CODE (y) == CONST)
693 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
694 ysize, canon_rtx (XEXP (y, 0)), c);
695 else
696 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
697 ysize, y, c);
698 }
699 if (GET_CODE (y) == CONST)
700 return memrefs_conflict_p (xsize, x, ysize,
701 canon_rtx (XEXP (y, 0)), c);
702
703 if (CONSTANT_P (y))
704 return (rtx_equal_for_memref_p (x, y)
705 && (xsize == 0 || ysize == 0
706 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0)));
707
708 return 1;
709 }
710 return 1;
711 }
712
713 /* Functions to compute memory dependencies.
714
715 Since we process the insns in execution order, we can build tables
716 to keep track of what registers are fixed (and not aliased), what registers
717 are varying in known ways, and what registers are varying in unknown
718 ways.
719
720 If both memory references are volatile, then there must always be a
721 dependence between the two references, since their order can not be
722 changed. A volatile and non-volatile reference can be interchanged
723 though.
724
725 A MEM_IN_STRUCT reference at a non-QImode varying address can never
726 conflict with a non-MEM_IN_STRUCT reference at a fixed address. We must
727 allow QImode aliasing because the ANSI C standard allows character
728 pointers to alias anything. We are assuming that characters are
729 always QImode here. */
730
731 /* Read dependence: X is read after read in MEM takes place. There can
732 only be a dependence here if both reads are volatile. */
733
734 int
735 read_dependence (mem, x)
736 rtx mem;
737 rtx x;
738 {
739 return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
740 }
741
742 /* True dependence: X is read after store in MEM takes place. */
743
744 int
745 true_dependence (mem, x)
746 rtx mem;
747 rtx x;
748 {
749 /* If X is an unchanging read, then it can't possibly conflict with any
750 non-unchanging store. It may conflict with an unchanging write though,
751 because there may be a single store to this address to initialize it.
752 Just fall through to the code below to resolve the case where we have
753 both an unchanging read and an unchanging write. This won't handle all
754 cases optimally, but the possible performance loss should be
755 negligible. */
756 if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
757 return 0;
758
759 return ((MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
760 || (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
761 SIZE_FOR_MODE (x), XEXP (x, 0), 0)
762 && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
763 && GET_MODE (mem) != QImode
764 && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
765 && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
766 && GET_MODE (x) != QImode
767 && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem))));
768 }
769
770 /* Anti dependence: X is written after read in MEM takes place. */
771
772 int
773 anti_dependence (mem, x)
774 rtx mem;
775 rtx x;
776 {
777 /* If MEM is an unchanging read, then it can't possibly conflict with
778 the store to X, because there is at most one store to MEM, and it must
779 have occured somewhere before MEM. */
780 if (RTX_UNCHANGING_P (mem))
781 return 0;
782
783 return ((MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
784 || (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
785 SIZE_FOR_MODE (x), XEXP (x, 0), 0)
786 && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
787 && GET_MODE (mem) != QImode
788 && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
789 && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
790 && GET_MODE (x) != QImode
791 && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem))));
792 }
793
794 /* Output dependence: X is written after store in MEM takes place. */
795
796 int
797 output_dependence (mem, x)
798 rtx mem;
799 rtx x;
800 {
801 return ((MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
802 || (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
803 SIZE_FOR_MODE (x), XEXP (x, 0), 0)
804 && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
805 && GET_MODE (mem) != QImode
806 && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
807 && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
808 && GET_MODE (x) != QImode
809 && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem))));
810 }
811 \f
812 /* Helper functions for instruction scheduling. */
813
814 /* Add ELEM wrapped in an INSN_LIST with reg note kind DEP_TYPE to the
815 LOG_LINKS of INSN, if not already there. DEP_TYPE indicates the type
816 of dependence that this link represents. */
817
818 void
819 add_dependence (insn, elem, dep_type)
820 rtx insn;
821 rtx elem;
822 enum reg_note dep_type;
823 {
824 rtx link, next;
825
826 /* Don't depend an insn on itself. */
827 if (insn == elem)
828 return;
829
830 /* If elem is part of a sequence that must be scheduled together, then
831 make the dependence point to the last insn of the sequence.
832 When HAVE_cc0, it is possible for NOTEs to exist between users and
833 setters of the condition codes, so we must skip past notes here.
834 Otherwise, NOTEs are impossible here. */
835
836 next = NEXT_INSN (elem);
837
838 #ifdef HAVE_cc0
839 while (next && GET_CODE (next) == NOTE)
840 next = NEXT_INSN (next);
841 #endif
842
843 if (next && SCHED_GROUP_P (next))
844 {
845 /* Notes will never intervene here though, so don't bother checking
846 for them. */
847 while (NEXT_INSN (next) && SCHED_GROUP_P (NEXT_INSN (next)))
848 next = NEXT_INSN (next);
849
850 /* Again, don't depend an insn on itself. */
851 if (insn == next)
852 return;
853
854 /* Make the dependence to NEXT, the last insn of the group, instead
855 of the original ELEM. */
856 elem = next;
857 }
858
859 /* Check that we don't already have this dependence. */
860 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
861 if (XEXP (link, 0) == elem)
862 {
863 /* If this is a more restrictive type of dependence than the existing
864 one, then change the existing dependence to this type. */
865 if ((int) dep_type < (int) REG_NOTE_KIND (link))
866 PUT_REG_NOTE_KIND (link, dep_type);
867 return;
868 }
869 /* Might want to check one level of transitivity to save conses. */
870
871 link = rtx_alloc (INSN_LIST);
872 /* Insn dependency, not data dependency. */
873 PUT_REG_NOTE_KIND (link, dep_type);
874 XEXP (link, 0) = elem;
875 XEXP (link, 1) = LOG_LINKS (insn);
876 LOG_LINKS (insn) = link;
877 }
878
879 /* Remove ELEM wrapped in an INSN_LIST from the LOG_LINKS
880 of INSN. Abort if not found. */
881 void
882 remove_dependence (insn, elem)
883 rtx insn;
884 rtx elem;
885 {
886 rtx prev, link;
887 int found = 0;
888
889 for (prev = 0, link = LOG_LINKS (insn); link;
890 prev = link, link = XEXP (link, 1))
891 {
892 if (XEXP (link, 0) == elem)
893 {
894 if (prev)
895 XEXP (prev, 1) = XEXP (link, 1);
896 else
897 LOG_LINKS (insn) = XEXP (link, 1);
898 found = 1;
899 }
900 }
901
902 if (! found)
903 abort ();
904 return;
905 }
906 \f
907 #ifndef INSN_SCHEDULING
908 void schedule_insns () {}
909 #else
910 #ifndef __GNUC__
911 #define __inline
912 #endif
913
914 /* Computation of memory dependencies. */
915
916 /* The *_insns and *_mems are paired lists. Each pending memory operation
917 will have a pointer to the MEM rtx on one list and a pointer to the
918 containing insn on the other list in the same place in the list. */
919
920 /* We can't use add_dependence like the old code did, because a single insn
921 may have multiple memory accesses, and hence needs to be on the list
922 once for each memory access. Add_dependence won't let you add an insn
923 to a list more than once. */
924
925 /* An INSN_LIST containing all insns with pending read operations. */
926 static rtx pending_read_insns;
927
928 /* An EXPR_LIST containing all MEM rtx's which are pending reads. */
929 static rtx pending_read_mems;
930
931 /* An INSN_LIST containing all insns with pending write operations. */
932 static rtx pending_write_insns;
933
934 /* An EXPR_LIST containing all MEM rtx's which are pending writes. */
935 static rtx pending_write_mems;
936
937 /* Indicates the combined length of the two pending lists. We must prevent
938 these lists from ever growing too large since the number of dependencies
939 produced is at least O(N*N), and execution time is at least O(4*N*N), as
940 a function of the length of these pending lists. */
941
942 static int pending_lists_length;
943
944 /* An INSN_LIST containing all INSN_LISTs allocated but currently unused. */
945
946 static rtx unused_insn_list;
947
948 /* An EXPR_LIST containing all EXPR_LISTs allocated but currently unused. */
949
950 static rtx unused_expr_list;
951
952 /* The last insn upon which all memory references must depend.
953 This is an insn which flushed the pending lists, creating a dependency
954 between it and all previously pending memory references. This creates
955 a barrier (or a checkpoint) which no memory reference is allowed to cross.
956
957 This includes all non constant CALL_INSNs. When we do interprocedural
958 alias analysis, this restriction can be relaxed.
959 This may also be an INSN that writes memory if the pending lists grow
960 too large. */
961
962 static rtx last_pending_memory_flush;
963
964 /* The last function call we have seen. All hard regs, and, of course,
965 the last function call, must depend on this. */
966
967 static rtx last_function_call;
968
969 /* The LOG_LINKS field of this is a list of insns which use a pseudo register
970 that does not already cross a call. We create dependencies between each
971 of those insn and the next call insn, to ensure that they won't cross a call
972 after scheduling is done. */
973
974 static rtx sched_before_next_call;
975
976 /* Pointer to the last instruction scheduled. Used by rank_for_schedule,
977 so that insns independent of the last scheduled insn will be preferred
978 over dependent instructions. */
979
980 static rtx last_scheduled_insn;
981
982 /* Process an insn's memory dependencies. There are four kinds of
983 dependencies:
984
985 (0) read dependence: read follows read
986 (1) true dependence: read follows write
987 (2) anti dependence: write follows read
988 (3) output dependence: write follows write
989
990 We are careful to build only dependencies which actually exist, and
991 use transitivity to avoid building too many links. */
992 \f
993 /* Return the INSN_LIST containing INSN in LIST, or NULL
994 if LIST does not contain INSN. */
995
996 __inline static rtx
997 find_insn_list (insn, list)
998 rtx insn;
999 rtx list;
1000 {
1001 while (list)
1002 {
1003 if (XEXP (list, 0) == insn)
1004 return list;
1005 list = XEXP (list, 1);
1006 }
1007 return 0;
1008 }
1009
1010 /* Compute the function units used by INSN. This caches the value
1011 returned by function_units_used. A function unit is encoded as the
1012 unit number if the value is non-negative and the compliment of a
1013 mask if the value is negative. A function unit index is the
1014 non-negative encoding. */
1015
1016 __inline static int
1017 insn_unit (insn)
1018 rtx insn;
1019 {
1020 register int unit = INSN_UNIT (insn);
1021
1022 if (unit == 0)
1023 {
1024 recog_memoized (insn);
1025
1026 /* A USE insn, or something else we don't need to understand.
1027 We can't pass these directly to function_units_used because it will
1028 trigger a fatal error for unrecognizable insns. */
1029 if (INSN_CODE (insn) < 0)
1030 unit = -1;
1031 else
1032 {
1033 unit = function_units_used (insn);
1034 /* Increment non-negative values so we can cache zero. */
1035 if (unit >= 0) unit++;
1036 }
1037 /* We only cache 16 bits of the result, so if the value is out of
1038 range, don't cache it. */
1039 if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
1040 || unit >= 0
1041 || (~unit & ((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
1042 INSN_UNIT (insn) = unit;
1043 }
1044 return (unit > 0 ? unit - 1 : unit);
1045 }
1046
1047 /* Compute the blockage range for executing INSN on UNIT. This caches
1048 the value returned by the blockage_range_function for the unit.
1049 These values are encoded in an int where the upper half gives the
1050 minimum value and the lower half gives the maximum value. */
1051
1052 __inline static unsigned int
1053 blockage_range (unit, insn)
1054 int unit;
1055 rtx insn;
1056 {
1057 unsigned int blockage = INSN_BLOCKAGE (insn);
1058 unsigned int range;
1059
1060 if (UNIT_BLOCKED (blockage) != unit + 1)
1061 {
1062 range = function_units[unit].blockage_range_function (insn);
1063 /* We only cache the blockage range for one unit and then only if
1064 the values fit. */
1065 if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
1066 INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
1067 }
1068 else
1069 range = BLOCKAGE_RANGE (blockage);
1070
1071 return range;
1072 }
1073
1074 /* A vector indexed by function unit instance giving the last insn to use
1075 the unit. The value of the function unit instance index for unit U
1076 instance I is (U + I * FUNCTION_UNITS_SIZE). */
1077 static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
1078
1079 /* A vector indexed by function unit instance giving the minimum time when
1080 the unit will unblock based on the maximum blockage cost. */
1081 static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
1082
1083 /* A vector indexed by function unit number giving the number of insns
1084 that remain to use the unit. */
1085 static int unit_n_insns[FUNCTION_UNITS_SIZE];
1086
1087 /* Reset the function unit state to the null state. */
1088
1089 static void
1090 clear_units ()
1091 {
1092 int unit;
1093
1094 bzero (unit_last_insn, sizeof (unit_last_insn));
1095 bzero (unit_tick, sizeof (unit_tick));
1096 bzero (unit_n_insns, sizeof (unit_n_insns));
1097 }
1098
1099 /* Record an insn as one that will use the units encoded by UNIT. */
1100
1101 __inline static void
1102 prepare_unit (unit)
1103 int unit;
1104 {
1105 int i;
1106
1107 if (unit >= 0)
1108 unit_n_insns[unit]++;
1109 else
1110 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
1111 if ((unit & 1) != 0)
1112 prepare_unit (i);
1113 }
1114
1115 /* Return the actual hazard cost of executing INSN on the unit UNIT,
1116 instance INSTANCE at time CLOCK if the previous actual hazard cost
1117 was COST. */
1118
1119 __inline static int
1120 actual_hazard_this_instance (unit, instance, insn, clock, cost)
1121 int unit, instance, clock, cost;
1122 rtx insn;
1123 {
1124 int i;
1125 int tick = unit_tick[instance];
1126
1127 if (tick - clock > cost)
1128 {
1129 /* The scheduler is operating in reverse, so INSN is the executing
1130 insn and the unit's last insn is the candidate insn. We want a
1131 more exact measure of the blockage if we execute INSN at CLOCK
1132 given when we committed the execution of the unit's last insn.
1133
1134 The blockage value is given by either the unit's max blockage
1135 constant, blockage range function, or blockage function. Use
1136 the most exact form for the given unit. */
1137
1138 if (function_units[unit].blockage_range_function)
1139 {
1140 if (function_units[unit].blockage_function)
1141 tick += (function_units[unit].blockage_function
1142 (insn, unit_last_insn[instance])
1143 - function_units[unit].max_blockage);
1144 else
1145 tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
1146 - function_units[unit].max_blockage);
1147 }
1148 if (tick - clock > cost)
1149 cost = tick - clock;
1150 }
1151 return cost;
1152 }
1153
1154 /* Record INSN as having begun execution on the units encoded by UNIT at
1155 time CLOCK. */
1156
1157 __inline static void
1158 schedule_unit (unit, insn, clock)
1159 int unit, clock;
1160 rtx insn;
1161 {
1162 int i;
1163
1164 if (unit >= 0)
1165 {
1166 int instance = unit;
1167 #if MAX_MULTIPLICITY > 1
1168 /* Find the first free instance of the function unit and use that
1169 one. We assume that one is free. */
1170 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
1171 {
1172 if (! actual_hazard_this_instance (unit, instance, insn, clock, 0))
1173 break;
1174 instance += FUNCTION_UNITS_SIZE;
1175 }
1176 #endif
1177 unit_last_insn[instance] = insn;
1178 unit_tick[instance] = (clock + function_units[unit].max_blockage);
1179 }
1180 else
1181 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
1182 if ((unit & 1) != 0)
1183 schedule_unit (i, insn, clock);
1184 }
1185
1186 /* Return the actual hazard cost of executing INSN on the units encoded by
1187 UNIT at time CLOCK if the previous actual hazard cost was COST. */
1188
1189 __inline static int
1190 actual_hazard (unit, insn, clock, cost)
1191 int unit, clock, cost;
1192 rtx insn;
1193 {
1194 int i;
1195
1196 if (unit >= 0)
1197 {
1198 /* Find the instance of the function unit with the minimum hazard. */
1199 int instance = unit;
1200 int best = instance;
1201 int best_cost = actual_hazard_this_instance (unit, instance, insn,
1202 clock, cost);
1203 int this_cost;
1204
1205 #if MAX_MULTIPLICITY > 1
1206 if (best_cost > cost)
1207 {
1208 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
1209 {
1210 instance += FUNCTION_UNITS_SIZE;
1211 this_cost = actual_hazard_this_instance (unit, instance, insn,
1212 clock, cost);
1213 if (this_cost < best_cost)
1214 {
1215 best = instance;
1216 best_cost = this_cost;
1217 if (this_cost <= cost)
1218 break;
1219 }
1220 }
1221 }
1222 #endif
1223 cost = MAX (cost, best_cost);
1224 }
1225 else
1226 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
1227 if ((unit & 1) != 0)
1228 cost = actual_hazard (i, insn, clock, cost);
1229
1230 return cost;
1231 }
1232
1233 /* Return the potential hazard cost of executing an instruction on the
1234 units encoded by UNIT if the previous potential hazard cost was COST.
1235 An insn with a large blockage time is chosen in preference to one
1236 with a smaller time; an insn that uses a unit that is more likely
1237 to be used is chosen in preference to one with a unit that is less
1238 used. We are trying to minimize a subsequent actual hazard. */
1239
1240 __inline static int
1241 potential_hazard (unit, insn, cost)
1242 int unit, cost;
1243 rtx insn;
1244 {
1245 int i, ncost;
1246 unsigned int minb, maxb;
1247
1248 if (unit >= 0)
1249 {
1250 minb = maxb = function_units[unit].max_blockage;
1251 if (maxb > 1)
1252 {
1253 if (function_units[unit].blockage_range_function)
1254 {
1255 maxb = minb = blockage_range (unit, insn);
1256 maxb = MAX_BLOCKAGE_COST (maxb);
1257 minb = MIN_BLOCKAGE_COST (minb);
1258 }
1259
1260 if (maxb > 1)
1261 {
1262 /* Make the number of instructions left dominate. Make the
1263 minimum delay dominate the maximum delay. If all these
1264 are the same, use the unit number to add an arbitrary
1265 ordering. Other terms can be added. */
1266 ncost = minb * 0x40 + maxb;
1267 ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
1268 if (ncost > cost)
1269 cost = ncost;
1270 }
1271 }
1272 }
1273 else
1274 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
1275 if ((unit & 1) != 0)
1276 cost = potential_hazard (i, insn, cost);
1277
1278 return cost;
1279 }
1280
1281 /* Compute cost of executing INSN given the dependence LINK on the insn USED.
1282 This is the number of virtual cycles taken between instruction issue and
1283 instruction results. */
1284
1285 __inline static int
1286 insn_cost (insn, link, used)
1287 rtx insn, link, used;
1288 {
1289 register int cost = INSN_COST (insn);
1290
1291 if (cost == 0)
1292 {
1293 recog_memoized (insn);
1294
1295 /* A USE insn, or something else we don't need to understand.
1296 We can't pass these directly to result_ready_cost because it will
1297 trigger a fatal error for unrecognizable insns. */
1298 if (INSN_CODE (insn) < 0)
1299 {
1300 INSN_COST (insn) = 1;
1301 return 1;
1302 }
1303 else
1304 {
1305 cost = result_ready_cost (insn);
1306
1307 if (cost < 1)
1308 cost = 1;
1309
1310 INSN_COST (insn) = cost;
1311 }
1312 }
1313
1314 /* A USE insn should never require the value used to be computed. This
1315 allows the computation of a function's result and parameter values to
1316 overlap the return and call. */
1317 recog_memoized (used);
1318 if (INSN_CODE (used) < 0)
1319 LINK_COST_FREE (link) = 1;
1320
1321 /* If some dependencies vary the cost, compute the adjustment. Most
1322 commonly, the adjustment is complete: either the cost is ignored
1323 (in the case of an output- or anti-dependence), or the cost is
1324 unchanged. These values are cached in the link as LINK_COST_FREE
1325 and LINK_COST_ZERO. */
1326
1327 if (LINK_COST_FREE (link))
1328 cost = 1;
1329 #ifdef ADJUST_COST
1330 else if (! LINK_COST_ZERO (link))
1331 {
1332 int ncost = cost;
1333
1334 ADJUST_COST (used, link, insn, ncost);
1335 if (ncost <= 1)
1336 LINK_COST_FREE (link) = ncost = 1;
1337 if (cost == ncost)
1338 LINK_COST_ZERO (link) = 1;
1339 cost = ncost;
1340 }
1341 #endif
1342 return cost;
1343 }
1344
1345 /* Compute the priority number for INSN. */
1346
1347 static int
1348 priority (insn)
1349 rtx insn;
1350 {
1351 if (insn && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1352 {
1353 int prev_priority;
1354 int max_priority;
1355 int this_priority = INSN_PRIORITY (insn);
1356 rtx prev;
1357
1358 if (this_priority > 0)
1359 return this_priority;
1360
1361 max_priority = 1;
1362
1363 /* Nonzero if these insns must be scheduled together. */
1364 if (SCHED_GROUP_P (insn))
1365 {
1366 prev = insn;
1367 while (SCHED_GROUP_P (prev))
1368 {
1369 prev = PREV_INSN (prev);
1370 INSN_REF_COUNT (prev) += 1;
1371 }
1372 }
1373
1374 for (prev = LOG_LINKS (insn); prev; prev = XEXP (prev, 1))
1375 {
1376 rtx x = XEXP (prev, 0);
1377
1378 /* A dependence pointing to a note is always obsolete, because
1379 sched_analyze_insn will have created any necessary new dependences
1380 which replace it. Notes can be created when instructions are
1381 deleted by insn splitting, or by register allocation. */
1382 if (GET_CODE (x) == NOTE)
1383 {
1384 remove_dependence (insn, x);
1385 continue;
1386 }
1387
1388 /* Clear the link cost adjustment bits. */
1389 LINK_COST_FREE (prev) = 0;
1390 #ifdef ADJUST_COST
1391 LINK_COST_ZERO (prev) = 0;
1392 #endif
1393
1394 /* This priority calculation was chosen because it results in the
1395 least instruction movement, and does not hurt the performance
1396 of the resulting code compared to the old algorithm.
1397 This makes the sched algorithm more stable, which results
1398 in better code, because there is less register pressure,
1399 cross jumping is more likely to work, and debugging is easier.
1400
1401 When all instructions have a latency of 1, there is no need to
1402 move any instructions. Subtracting one here ensures that in such
1403 cases all instructions will end up with a priority of one, and
1404 hence no scheduling will be done.
1405
1406 The original code did not subtract the one, and added the
1407 insn_cost of the current instruction to its priority (e.g.
1408 move the insn_cost call down to the end). */
1409
1410 if (REG_NOTE_KIND (prev) == 0)
1411 /* Data dependence. */
1412 prev_priority = priority (x) + insn_cost (x, prev, insn) - 1;
1413 else
1414 /* Anti or output dependence. Don't add the latency of this
1415 insn's result, because it isn't being used. */
1416 prev_priority = priority (x);
1417
1418 if (prev_priority > max_priority)
1419 max_priority = prev_priority;
1420 INSN_REF_COUNT (x) += 1;
1421 }
1422
1423 prepare_unit (insn_unit (insn));
1424 INSN_PRIORITY (insn) = max_priority;
1425 return INSN_PRIORITY (insn);
1426 }
1427 return 0;
1428 }
1429 \f
1430 /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
1431 them to the unused_*_list variables, so that they can be reused. */
1432
1433 static void
1434 free_pending_lists ()
1435 {
1436 register rtx link, prev_link;
1437
1438 if (pending_read_insns)
1439 {
1440 prev_link = pending_read_insns;
1441 link = XEXP (prev_link, 1);
1442
1443 while (link)
1444 {
1445 prev_link = link;
1446 link = XEXP (link, 1);
1447 }
1448
1449 XEXP (prev_link, 1) = unused_insn_list;
1450 unused_insn_list = pending_read_insns;
1451 pending_read_insns = 0;
1452 }
1453
1454 if (pending_write_insns)
1455 {
1456 prev_link = pending_write_insns;
1457 link = XEXP (prev_link, 1);
1458
1459 while (link)
1460 {
1461 prev_link = link;
1462 link = XEXP (link, 1);
1463 }
1464
1465 XEXP (prev_link, 1) = unused_insn_list;
1466 unused_insn_list = pending_write_insns;
1467 pending_write_insns = 0;
1468 }
1469
1470 if (pending_read_mems)
1471 {
1472 prev_link = pending_read_mems;
1473 link = XEXP (prev_link, 1);
1474
1475 while (link)
1476 {
1477 prev_link = link;
1478 link = XEXP (link, 1);
1479 }
1480
1481 XEXP (prev_link, 1) = unused_expr_list;
1482 unused_expr_list = pending_read_mems;
1483 pending_read_mems = 0;
1484 }
1485
1486 if (pending_write_mems)
1487 {
1488 prev_link = pending_write_mems;
1489 link = XEXP (prev_link, 1);
1490
1491 while (link)
1492 {
1493 prev_link = link;
1494 link = XEXP (link, 1);
1495 }
1496
1497 XEXP (prev_link, 1) = unused_expr_list;
1498 unused_expr_list = pending_write_mems;
1499 pending_write_mems = 0;
1500 }
1501 }
1502
1503 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1504 The MEM is a memory reference contained within INSN, which we are saving
1505 so that we can do memory aliasing on it. */
1506
1507 static void
1508 add_insn_mem_dependence (insn_list, mem_list, insn, mem)
1509 rtx *insn_list, *mem_list, insn, mem;
1510 {
1511 register rtx link;
1512
1513 if (unused_insn_list)
1514 {
1515 link = unused_insn_list;
1516 unused_insn_list = XEXP (link, 1);
1517 }
1518 else
1519 link = rtx_alloc (INSN_LIST);
1520 XEXP (link, 0) = insn;
1521 XEXP (link, 1) = *insn_list;
1522 *insn_list = link;
1523
1524 if (unused_expr_list)
1525 {
1526 link = unused_expr_list;
1527 unused_expr_list = XEXP (link, 1);
1528 }
1529 else
1530 link = rtx_alloc (EXPR_LIST);
1531 XEXP (link, 0) = mem;
1532 XEXP (link, 1) = *mem_list;
1533 *mem_list = link;
1534
1535 pending_lists_length++;
1536 }
1537 \f
1538 /* Make a dependency between every memory reference on the pending lists
1539 and INSN, thus flushing the pending lists. */
1540
1541 static void
1542 flush_pending_lists (insn)
1543 rtx insn;
1544 {
1545 rtx link;
1546
1547 while (pending_read_insns)
1548 {
1549 add_dependence (insn, XEXP (pending_read_insns, 0), REG_DEP_ANTI);
1550
1551 link = pending_read_insns;
1552 pending_read_insns = XEXP (pending_read_insns, 1);
1553 XEXP (link, 1) = unused_insn_list;
1554 unused_insn_list = link;
1555
1556 link = pending_read_mems;
1557 pending_read_mems = XEXP (pending_read_mems, 1);
1558 XEXP (link, 1) = unused_expr_list;
1559 unused_expr_list = link;
1560 }
1561 while (pending_write_insns)
1562 {
1563 add_dependence (insn, XEXP (pending_write_insns, 0), REG_DEP_ANTI);
1564
1565 link = pending_write_insns;
1566 pending_write_insns = XEXP (pending_write_insns, 1);
1567 XEXP (link, 1) = unused_insn_list;
1568 unused_insn_list = link;
1569
1570 link = pending_write_mems;
1571 pending_write_mems = XEXP (pending_write_mems, 1);
1572 XEXP (link, 1) = unused_expr_list;
1573 unused_expr_list = link;
1574 }
1575 pending_lists_length = 0;
1576
1577 if (last_pending_memory_flush)
1578 add_dependence (insn, last_pending_memory_flush, REG_DEP_ANTI);
1579
1580 last_pending_memory_flush = insn;
1581 }
1582
1583 /* Analyze a single SET or CLOBBER rtx, X, creating all dependencies generated
1584 by the write to the destination of X, and reads of everything mentioned. */
1585
1586 static void
1587 sched_analyze_1 (x, insn)
1588 rtx x;
1589 rtx insn;
1590 {
1591 register int regno;
1592 register rtx dest = SET_DEST (x);
1593
1594 if (dest == 0)
1595 return;
1596
1597 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
1598 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
1599 {
1600 if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
1601 {
1602 /* The second and third arguments are values read by this insn. */
1603 sched_analyze_2 (XEXP (dest, 1), insn);
1604 sched_analyze_2 (XEXP (dest, 2), insn);
1605 }
1606 dest = SUBREG_REG (dest);
1607 }
1608
1609 if (GET_CODE (dest) == REG)
1610 {
1611 register int offset, bit, i;
1612
1613 regno = REGNO (dest);
1614
1615 /* A hard reg in a wide mode may really be multiple registers.
1616 If so, mark all of them just like the first. */
1617 if (regno < FIRST_PSEUDO_REGISTER)
1618 {
1619 i = HARD_REGNO_NREGS (regno, GET_MODE (dest));
1620 while (--i >= 0)
1621 {
1622 rtx u;
1623
1624 for (u = reg_last_uses[regno+i]; u; u = XEXP (u, 1))
1625 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1626 reg_last_uses[regno + i] = 0;
1627 if (reg_last_sets[regno + i])
1628 add_dependence (insn, reg_last_sets[regno + i],
1629 REG_DEP_OUTPUT);
1630 reg_last_sets[regno + i] = insn;
1631 if ((call_used_regs[i] || global_regs[i])
1632 && last_function_call)
1633 /* Function calls clobber all call_used regs. */
1634 add_dependence (insn, last_function_call, REG_DEP_ANTI);
1635 }
1636 }
1637 else
1638 {
1639 rtx u;
1640
1641 for (u = reg_last_uses[regno]; u; u = XEXP (u, 1))
1642 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1643 reg_last_uses[regno] = 0;
1644 if (reg_last_sets[regno])
1645 add_dependence (insn, reg_last_sets[regno], REG_DEP_OUTPUT);
1646 reg_last_sets[regno] = insn;
1647
1648 /* Pseudos that are REG_EQUIV to something may be replaced
1649 by that during reloading. We need only add dependencies for
1650 the address in the REG_EQUIV note. */
1651 if (! reload_completed
1652 && reg_known_equiv_p[regno]
1653 && GET_CODE (reg_known_value[regno]) == MEM)
1654 sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
1655
1656 /* Don't let it cross a call after scheduling if it doesn't
1657 already cross one. */
1658 if (reg_n_calls_crossed[regno] == 0 && last_function_call)
1659 add_dependence (insn, last_function_call, REG_DEP_ANTI);
1660 }
1661 }
1662 else if (GET_CODE (dest) == MEM)
1663 {
1664 /* Writing memory. */
1665
1666 if (pending_lists_length > 32)
1667 {
1668 /* Flush all pending reads and writes to prevent the pending lists
1669 from getting any larger. Insn scheduling runs too slowly when
1670 these lists get long. The number 32 was chosen because it
1671 seems like a reasonable number. When compiling GCC with itself,
1672 this flush occurs 8 times for sparc, and 10 times for m88k using
1673 the number 32. */
1674 flush_pending_lists (insn);
1675 }
1676 else
1677 {
1678 rtx pending, pending_mem;
1679
1680 pending = pending_read_insns;
1681 pending_mem = pending_read_mems;
1682 while (pending)
1683 {
1684 /* If a dependency already exists, don't create a new one. */
1685 if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
1686 if (anti_dependence (XEXP (pending_mem, 0), dest))
1687 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1688
1689 pending = XEXP (pending, 1);
1690 pending_mem = XEXP (pending_mem, 1);
1691 }
1692
1693 pending = pending_write_insns;
1694 pending_mem = pending_write_mems;
1695 while (pending)
1696 {
1697 /* If a dependency already exists, don't create a new one. */
1698 if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
1699 if (output_dependence (XEXP (pending_mem, 0), dest))
1700 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1701
1702 pending = XEXP (pending, 1);
1703 pending_mem = XEXP (pending_mem, 1);
1704 }
1705
1706 if (last_pending_memory_flush)
1707 add_dependence (insn, last_pending_memory_flush, REG_DEP_ANTI);
1708
1709 add_insn_mem_dependence (&pending_write_insns, &pending_write_mems,
1710 insn, dest);
1711 }
1712 sched_analyze_2 (XEXP (dest, 0), insn);
1713 }
1714
1715 /* Analyze reads. */
1716 if (GET_CODE (x) == SET)
1717 sched_analyze_2 (SET_SRC (x), insn);
1718 }
1719
1720 /* Analyze the uses of memory and registers in rtx X in INSN. */
1721
1722 static void
1723 sched_analyze_2 (x, insn)
1724 rtx x;
1725 rtx insn;
1726 {
1727 register int i;
1728 register int j;
1729 register enum rtx_code code;
1730 register char *fmt;
1731
1732 if (x == 0)
1733 return;
1734
1735 code = GET_CODE (x);
1736
1737 switch (code)
1738 {
1739 case CONST_INT:
1740 case CONST_DOUBLE:
1741 case SYMBOL_REF:
1742 case CONST:
1743 case LABEL_REF:
1744 /* Ignore constants. Note that we must handle CONST_DOUBLE here
1745 because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
1746 this does not mean that this insn is using cc0. */
1747 return;
1748
1749 #ifdef HAVE_cc0
1750 case CC0:
1751 {
1752 rtx link, prev;
1753
1754 /* There may be a note before this insn now, but all notes will
1755 be removed before we actually try to schedule the insns, so
1756 it won't cause a problem later. We must avoid it here though. */
1757
1758 /* User of CC0 depends on immediately preceding insn. */
1759 SCHED_GROUP_P (insn) = 1;
1760
1761 /* Make a copy of all dependencies on the immediately previous insn,
1762 and add to this insn. This is so that all the dependencies will
1763 apply to the group. Remove an explicit dependence on this insn
1764 as SCHED_GROUP_P now represents it. */
1765
1766 prev = PREV_INSN (insn);
1767 while (GET_CODE (prev) == NOTE)
1768 prev = PREV_INSN (prev);
1769
1770 if (find_insn_list (prev, LOG_LINKS (insn)))
1771 remove_dependence (insn, prev);
1772
1773 for (link = LOG_LINKS (prev); link; link = XEXP (link, 1))
1774 add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
1775
1776 return;
1777 }
1778 #endif
1779
1780 case REG:
1781 {
1782 int regno = REGNO (x);
1783 if (regno < FIRST_PSEUDO_REGISTER)
1784 {
1785 int i;
1786
1787 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
1788 while (--i >= 0)
1789 {
1790 reg_last_uses[regno + i]
1791 = gen_rtx (INSN_LIST, VOIDmode,
1792 insn, reg_last_uses[regno + i]);
1793 if (reg_last_sets[regno + i])
1794 add_dependence (insn, reg_last_sets[regno + i], 0);
1795 if ((call_used_regs[regno + i] || global_regs[regno + i])
1796 && last_function_call)
1797 /* Function calls clobber all call_used regs. */
1798 add_dependence (insn, last_function_call, REG_DEP_ANTI);
1799 }
1800 }
1801 else
1802 {
1803 reg_last_uses[regno]
1804 = gen_rtx (INSN_LIST, VOIDmode, insn, reg_last_uses[regno]);
1805 if (reg_last_sets[regno])
1806 add_dependence (insn, reg_last_sets[regno], 0);
1807
1808 /* Pseudos that are REG_EQUIV to something may be replaced
1809 by that during reloading. We need only add dependencies for
1810 the address in the REG_EQUIV note. */
1811 if (! reload_completed
1812 && reg_known_equiv_p[regno]
1813 && GET_CODE (reg_known_value[regno]) == MEM)
1814 sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
1815
1816 /* If the register does not already cross any calls, then add this
1817 insn to the sched_before_next_call list so that it will still
1818 not cross calls after scheduling. */
1819 if (reg_n_calls_crossed[regno] == 0)
1820 add_dependence (sched_before_next_call, insn, REG_DEP_ANTI);
1821 }
1822 return;
1823 }
1824
1825 case MEM:
1826 {
1827 /* Reading memory. */
1828
1829 rtx pending, pending_mem;
1830
1831 pending = pending_read_insns;
1832 pending_mem = pending_read_mems;
1833 while (pending)
1834 {
1835 /* If a dependency already exists, don't create a new one. */
1836 if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
1837 if (read_dependence (XEXP (pending_mem, 0), x))
1838 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1839
1840 pending = XEXP (pending, 1);
1841 pending_mem = XEXP (pending_mem, 1);
1842 }
1843
1844 pending = pending_write_insns;
1845 pending_mem = pending_write_mems;
1846 while (pending)
1847 {
1848 /* If a dependency already exists, don't create a new one. */
1849 if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
1850 if (true_dependence (XEXP (pending_mem, 0), x))
1851 add_dependence (insn, XEXP (pending, 0), 0);
1852
1853 pending = XEXP (pending, 1);
1854 pending_mem = XEXP (pending_mem, 1);
1855 }
1856 if (last_pending_memory_flush)
1857 add_dependence (insn, last_pending_memory_flush, REG_DEP_ANTI);
1858
1859 /* Always add these dependencies to pending_reads, since
1860 this insn may be followed by a write. */
1861 add_insn_mem_dependence (&pending_read_insns, &pending_read_mems,
1862 insn, x);
1863
1864 /* Take advantage of tail recursion here. */
1865 sched_analyze_2 (XEXP (x, 0), insn);
1866 return;
1867 }
1868
1869 case ASM_OPERANDS:
1870 case ASM_INPUT:
1871 case UNSPEC_VOLATILE:
1872 case TRAP_IF:
1873 {
1874 rtx u;
1875
1876 /* Traditional and volatile asm instructions must be considered to use
1877 and clobber all hard registers and all of memory. So must
1878 TRAP_IF and UNSPEC_VOLATILE operations. */
1879 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
1880 {
1881 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1882 {
1883 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
1884 if (GET_CODE (PATTERN (XEXP (u, 0))) != USE)
1885 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1886 reg_last_uses[i] = 0;
1887 if (reg_last_sets[i]
1888 && GET_CODE (PATTERN (reg_last_sets[i])) != USE)
1889 add_dependence (insn, reg_last_sets[i], 0);
1890 reg_last_sets[i] = insn;
1891 }
1892
1893 flush_pending_lists (insn);
1894 }
1895
1896 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1897 We can not just fall through here since then we would be confused
1898 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1899 traditional asms unlike their normal usage. */
1900
1901 if (code == ASM_OPERANDS)
1902 {
1903 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
1904 sched_analyze_2 (ASM_OPERANDS_INPUT (x, j), insn);
1905 return;
1906 }
1907 break;
1908 }
1909
1910 case PRE_DEC:
1911 case POST_DEC:
1912 case PRE_INC:
1913 case POST_INC:
1914 /* These both read and modify the result. We must handle them as writes
1915 to get proper dependencies for following instructions. We must handle
1916 them as reads to get proper dependencies from this to previous
1917 instructions. Thus we need to pass them to both sched_analyze_1
1918 and sched_analyze_2. We must call sched_analyze_2 first in order
1919 to get the proper antecedent for the read. */
1920 sched_analyze_2 (XEXP (x, 0), insn);
1921 sched_analyze_1 (x, insn);
1922 return;
1923 }
1924
1925 /* Other cases: walk the insn. */
1926 fmt = GET_RTX_FORMAT (code);
1927 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1928 {
1929 if (fmt[i] == 'e')
1930 sched_analyze_2 (XEXP (x, i), insn);
1931 else if (fmt[i] == 'E')
1932 for (j = 0; j < XVECLEN (x, i); j++)
1933 sched_analyze_2 (XVECEXP (x, i, j), insn);
1934 }
1935 }
1936
1937 /* Analyze an INSN with pattern X to find all dependencies. */
1938
1939 static void
1940 sched_analyze_insn (x, insn)
1941 rtx x, insn;
1942 {
1943 register RTX_CODE code = GET_CODE (x);
1944 rtx link;
1945
1946 if (code == SET || code == CLOBBER)
1947 sched_analyze_1 (x, insn);
1948 else if (code == PARALLEL)
1949 {
1950 register int i;
1951 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1952 {
1953 code = GET_CODE (XVECEXP (x, 0, i));
1954 if (code == SET || code == CLOBBER)
1955 sched_analyze_1 (XVECEXP (x, 0, i), insn);
1956 else
1957 sched_analyze_2 (XVECEXP (x, 0, i), insn);
1958 }
1959 }
1960 else
1961 sched_analyze_2 (x, insn);
1962
1963 /* Handle function calls. */
1964 if (GET_CODE (insn) == CALL_INSN)
1965 {
1966 rtx dep_insn;
1967 rtx prev_dep_insn;
1968
1969 /* When scheduling instructions, we make sure calls don't lose their
1970 accompanying USE insns by depending them one on another in order. */
1971
1972 prev_dep_insn = insn;
1973 dep_insn = PREV_INSN (insn);
1974 while (GET_CODE (dep_insn) == INSN
1975 && GET_CODE (PATTERN (dep_insn)) == USE)
1976 {
1977 SCHED_GROUP_P (prev_dep_insn) = 1;
1978
1979 /* Make a copy of all dependencies on dep_insn, and add to insn.
1980 This is so that all of the dependencies will apply to the
1981 group. */
1982
1983 for (link = LOG_LINKS (dep_insn); link; link = XEXP (link, 1))
1984 add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
1985
1986 prev_dep_insn = dep_insn;
1987 dep_insn = PREV_INSN (dep_insn);
1988 }
1989 }
1990 }
1991
1992 /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
1993 for every dependency. */
1994
1995 static int
1996 sched_analyze (head, tail)
1997 rtx head, tail;
1998 {
1999 register rtx insn;
2000 register int n_insns = 0;
2001 register rtx u;
2002 register int luid = 0;
2003
2004 for (insn = head; ; insn = NEXT_INSN (insn))
2005 {
2006 INSN_LUID (insn) = luid++;
2007
2008 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
2009 {
2010 sched_analyze_insn (PATTERN (insn), insn);
2011 n_insns += 1;
2012 }
2013 else if (GET_CODE (insn) == CALL_INSN)
2014 {
2015 rtx dest = 0;
2016 rtx x;
2017 register int i;
2018
2019 /* Any instruction using a hard register which may get clobbered
2020 by a call needs to be marked as dependent on this call.
2021 This prevents a use of a hard return reg from being moved
2022 past a void call (i.e. it does not explicitly set the hard
2023 return reg). */
2024
2025 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2026 if (call_used_regs[i] || global_regs[i])
2027 {
2028 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
2029 if (GET_CODE (PATTERN (XEXP (u, 0))) != USE)
2030 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
2031 reg_last_uses[i] = 0;
2032 if (reg_last_sets[i]
2033 && GET_CODE (PATTERN (reg_last_sets[i])) != USE)
2034 add_dependence (insn, reg_last_sets[i], REG_DEP_ANTI);
2035 reg_last_sets[i] = insn;
2036 /* Insn, being a CALL_INSN, magically depends on
2037 `last_function_call' already. */
2038 }
2039
2040 /* For each insn which shouldn't cross a call, add a dependence
2041 between that insn and this call insn. */
2042 x = LOG_LINKS (sched_before_next_call);
2043 while (x)
2044 {
2045 add_dependence (insn, XEXP (x, 0), REG_DEP_ANTI);
2046 x = XEXP (x, 1);
2047 }
2048 LOG_LINKS (sched_before_next_call) = 0;
2049
2050 sched_analyze_insn (PATTERN (insn), insn);
2051
2052 /* We don't need to flush memory for a function call which does
2053 not involve memory. */
2054 if (! CONST_CALL_P (insn))
2055 {
2056 /* In the absence of interprocedural alias analysis,
2057 we must flush all pending reads and writes, and
2058 start new dependencies starting from here. */
2059 flush_pending_lists (insn);
2060 }
2061
2062 /* Depend this function call (actually, the user of this
2063 function call) on all hard register clobberage. */
2064 last_function_call = insn;
2065 n_insns += 1;
2066 }
2067
2068 if (insn == tail)
2069 return n_insns;
2070 }
2071 }
2072 \f
2073 /* Called when we see a set of a register. If death is true, then we are
2074 scanning backwards. Mark that register as unborn. If nobody says
2075 otherwise, that is how things will remain. If death is false, then we
2076 are scanning forwards. Mark that register as being born. */
2077
2078 static void
2079 sched_note_set (b, x, death)
2080 int b;
2081 rtx x;
2082 int death;
2083 {
2084 register int regno, j;
2085 register rtx reg = SET_DEST (x);
2086 int subreg_p = 0;
2087
2088 if (reg == 0)
2089 return;
2090
2091 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == STRICT_LOW_PART
2092 || GET_CODE (reg) == SIGN_EXTRACT || GET_CODE (reg) == ZERO_EXTRACT)
2093 {
2094 /* Must treat modification of just one hardware register of a multi-reg
2095 value or just a byte field of a register exactly the same way that
2096 mark_set_1 in flow.c does, i.e. anything except a paradoxical subreg
2097 does not kill the entire register. */
2098 if (GET_CODE (reg) != SUBREG
2099 || REG_SIZE (SUBREG_REG (reg)) > REG_SIZE (reg))
2100 subreg_p = 1;
2101
2102 reg = SUBREG_REG (reg);
2103 }
2104
2105 if (GET_CODE (reg) != REG)
2106 return;
2107
2108 /* Global registers are always live, so the code below does not apply
2109 to them. */
2110
2111 regno = REGNO (reg);
2112 if (regno >= FIRST_PSEUDO_REGISTER || ! global_regs[regno])
2113 {
2114 register int offset = regno / REGSET_ELT_BITS;
2115 register REGSET_ELT_TYPE bit
2116 = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
2117
2118 if (death)
2119 {
2120 /* If we only set part of the register, then this set does not
2121 kill it. */
2122 if (subreg_p)
2123 return;
2124
2125 /* Try killing this register. */
2126 if (regno < FIRST_PSEUDO_REGISTER)
2127 {
2128 int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2129 while (--j >= 0)
2130 {
2131 offset = (regno + j) / REGSET_ELT_BITS;
2132 bit = (REGSET_ELT_TYPE) 1 << ((regno + j) % REGSET_ELT_BITS);
2133
2134 bb_live_regs[offset] &= ~bit;
2135 bb_dead_regs[offset] |= bit;
2136 }
2137 }
2138 else
2139 {
2140 bb_live_regs[offset] &= ~bit;
2141 bb_dead_regs[offset] |= bit;
2142 }
2143 }
2144 else
2145 {
2146 /* Make the register live again. */
2147 if (regno < FIRST_PSEUDO_REGISTER)
2148 {
2149 int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2150 while (--j >= 0)
2151 {
2152 offset = (regno + j) / REGSET_ELT_BITS;
2153 bit = (REGSET_ELT_TYPE) 1 << ((regno + j) % REGSET_ELT_BITS);
2154
2155 bb_live_regs[offset] |= bit;
2156 bb_dead_regs[offset] &= ~bit;
2157 }
2158 }
2159 else
2160 {
2161 bb_live_regs[offset] |= bit;
2162 bb_dead_regs[offset] &= ~bit;
2163 }
2164 }
2165 }
2166 }
2167 \f
2168 /* Macros and functions for keeping the priority queue sorted, and
2169 dealing with queueing and unqueueing of instructions. */
2170
2171 #define SCHED_SORT(READY, NEW_READY, OLD_READY) \
2172 do { if ((NEW_READY) - (OLD_READY) == 1) \
2173 swap_sort (READY, NEW_READY); \
2174 else if ((NEW_READY) - (OLD_READY) > 1) \
2175 qsort (READY, NEW_READY, sizeof (rtx), rank_for_schedule); } \
2176 while (0)
2177
2178 /* Returns a positive value if y is preferred; returns a negative value if
2179 x is preferred. Should never return 0, since that will make the sort
2180 unstable. */
2181
2182 static int
2183 rank_for_schedule (x, y)
2184 rtx *x, *y;
2185 {
2186 rtx tmp = *y;
2187 rtx tmp2 = *x;
2188 rtx link;
2189 int tmp_class, tmp2_class;
2190 int value;
2191
2192 /* Choose the instruction with the highest priority, if different. */
2193 if (value = INSN_PRIORITY (tmp) - INSN_PRIORITY (tmp2))
2194 return value;
2195
2196 if (last_scheduled_insn)
2197 {
2198 /* Classify the instructions into three classes:
2199 1) Data dependent on last schedule insn.
2200 2) Anti/Output dependent on last scheduled insn.
2201 3) Independent of last scheduled insn, or has latency of one.
2202 Choose the insn from the highest numbered class if different. */
2203 link = find_insn_list (tmp, LOG_LINKS (last_scheduled_insn));
2204 if (link == 0 || insn_cost (tmp, link, last_scheduled_insn) == 1)
2205 tmp_class = 3;
2206 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
2207 tmp_class = 1;
2208 else
2209 tmp_class = 2;
2210
2211 link = find_insn_list (tmp2, LOG_LINKS (last_scheduled_insn));
2212 if (link == 0 || insn_cost (tmp2, link, last_scheduled_insn) == 1)
2213 tmp2_class = 3;
2214 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
2215 tmp2_class = 1;
2216 else
2217 tmp2_class = 2;
2218
2219 if (value = tmp_class - tmp2_class)
2220 return value;
2221 }
2222
2223 /* If insns are equally good, sort by INSN_LUID (original insn order),
2224 so that we make the sort stable. This minimizes instruction movement,
2225 thus minimizing sched's effect on debugging and cross-jumping. */
2226 return INSN_LUID (tmp) - INSN_LUID (tmp2);
2227 }
2228
2229 /* Resort the array A in which only element at index N may be out of order. */
2230
2231 __inline static void
2232 swap_sort (a, n)
2233 rtx *a;
2234 int n;
2235 {
2236 rtx insn = a[n-1];
2237 int i = n-2;
2238
2239 while (i >= 0 && rank_for_schedule (a+i, &insn) >= 0)
2240 {
2241 a[i+1] = a[i];
2242 i -= 1;
2243 }
2244 a[i+1] = insn;
2245 }
2246
2247 static int max_priority;
2248
2249 /* Add INSN to the insn queue so that it fires at least N_CYCLES
2250 before the currently executing insn. */
2251
2252 __inline static void
2253 queue_insn (insn, n_cycles)
2254 rtx insn;
2255 int n_cycles;
2256 {
2257 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
2258 NEXT_INSN (insn) = insn_queue[next_q];
2259 insn_queue[next_q] = insn;
2260 q_size += 1;
2261 }
2262
2263 /* Return nonzero if PAT is the pattern of an insn which makes a
2264 register live. */
2265
2266 __inline static int
2267 birthing_insn_p (pat)
2268 rtx pat;
2269 {
2270 int j;
2271
2272 if (reload_completed == 1)
2273 return 0;
2274
2275 if (GET_CODE (pat) == SET
2276 && GET_CODE (SET_DEST (pat)) == REG)
2277 {
2278 rtx dest = SET_DEST (pat);
2279 int i = REGNO (dest);
2280 int offset = i / REGSET_ELT_BITS;
2281 REGSET_ELT_TYPE bit = (REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS);
2282
2283 /* It would be more accurate to use refers_to_regno_p or
2284 reg_mentioned_p to determine when the dest is not live before this
2285 insn. */
2286
2287 if (bb_live_regs[offset] & bit)
2288 return (reg_n_sets[i] == 1);
2289
2290 return 0;
2291 }
2292 if (GET_CODE (pat) == PARALLEL)
2293 {
2294 for (j = 0; j < XVECLEN (pat, 0); j++)
2295 if (birthing_insn_p (XVECEXP (pat, 0, j)))
2296 return 1;
2297 }
2298 return 0;
2299 }
2300
2301 /* PREV is an insn that is ready to execute. Adjust its priority if that
2302 will help shorten register lifetimes. */
2303
2304 __inline static void
2305 adjust_priority (prev)
2306 rtx prev;
2307 {
2308 /* Trying to shorten register lives after reload has completed
2309 is useless and wrong. It gives inaccurate schedules. */
2310 if (reload_completed == 0)
2311 {
2312 rtx note;
2313 int n_deaths = 0;
2314
2315 /* ??? This code has no effect, because REG_DEAD notes are removed
2316 before we ever get here. */
2317 for (note = REG_NOTES (prev); note; note = XEXP (note, 1))
2318 if (REG_NOTE_KIND (note) == REG_DEAD)
2319 n_deaths += 1;
2320
2321 /* Defer scheduling insns which kill registers, since that
2322 shortens register lives. Prefer scheduling insns which
2323 make registers live for the same reason. */
2324 switch (n_deaths)
2325 {
2326 default:
2327 INSN_PRIORITY (prev) >>= 3;
2328 break;
2329 case 3:
2330 INSN_PRIORITY (prev) >>= 2;
2331 break;
2332 case 2:
2333 case 1:
2334 INSN_PRIORITY (prev) >>= 1;
2335 break;
2336 case 0:
2337 if (birthing_insn_p (PATTERN (prev)))
2338 {
2339 int max = max_priority;
2340
2341 if (max > INSN_PRIORITY (prev))
2342 INSN_PRIORITY (prev) = max;
2343 }
2344 break;
2345 }
2346 }
2347 }
2348
2349 /* INSN is the "currently executing insn". Launch each insn which was
2350 waiting on INSN (in the backwards dataflow sense). READY is a
2351 vector of insns which are ready to fire. N_READY is the number of
2352 elements in READY. CLOCK is the current virtual cycle. */
2353
2354 static int
2355 schedule_insn (insn, ready, n_ready, clock)
2356 rtx insn;
2357 rtx *ready;
2358 int n_ready;
2359 int clock;
2360 {
2361 rtx link;
2362 int new_ready = n_ready;
2363
2364 if (MAX_BLOCKAGE > 1)
2365 schedule_unit (insn_unit (insn), insn, clock);
2366
2367 if (LOG_LINKS (insn) == 0)
2368 return n_ready;
2369
2370 /* This is used by the function adjust_priority above. */
2371 if (n_ready > 0)
2372 max_priority = MAX (INSN_PRIORITY (ready[0]), INSN_PRIORITY (insn));
2373 else
2374 max_priority = INSN_PRIORITY (insn);
2375
2376 for (link = LOG_LINKS (insn); link != 0; link = XEXP (link, 1))
2377 {
2378 rtx prev = XEXP (link, 0);
2379 int cost = insn_cost (prev, link, insn);
2380
2381 if ((INSN_REF_COUNT (prev) -= 1) != 0)
2382 {
2383 /* We satisfied one requirement to fire PREV. Record the earliest
2384 time when PREV can fire. No need to do this if the cost is 1,
2385 because PREV can fire no sooner than the next cycle. */
2386 if (cost > 1)
2387 INSN_TICK (prev) = MAX (INSN_TICK (prev), clock + cost);
2388 }
2389 else
2390 {
2391 /* We satisfied the last requirement to fire PREV. Ensure that all
2392 timing requirements are satisfied. */
2393 if (INSN_TICK (prev) - clock > cost)
2394 cost = INSN_TICK (prev) - clock;
2395
2396 /* Adjust the priority of PREV and either put it on the ready
2397 list or queue it. */
2398 adjust_priority (prev);
2399 if (cost <= 1)
2400 ready[new_ready++] = prev;
2401 else
2402 queue_insn (prev, cost);
2403 }
2404 }
2405
2406 return new_ready;
2407 }
2408
2409 /* Given N_READY insns in the ready list READY at time CLOCK, queue
2410 those that are blocked due to function unit hazards and rearrange
2411 the remaining ones to minimize subsequent function unit hazards. */
2412
2413 static int
2414 schedule_select (ready, n_ready, clock, file)
2415 rtx *ready;
2416 int n_ready, clock;
2417 FILE *file;
2418 {
2419 int pri = INSN_PRIORITY (ready[0]);
2420 int i, j, k, q, cost, best_cost, best_insn = 0, new_ready = n_ready;
2421 rtx insn;
2422
2423 /* Work down the ready list in groups of instructions with the same
2424 priority value. Queue insns in the group that are blocked and
2425 select among those that remain for the one with the largest
2426 potential hazard. */
2427 for (i = 0; i < n_ready; i = j)
2428 {
2429 int opri = pri;
2430 for (j = i + 1; j < n_ready; j++)
2431 if ((pri = INSN_PRIORITY (ready[j])) != opri)
2432 break;
2433
2434 /* Queue insns in the group that are blocked. */
2435 for (k = i, q = 0; k < j; k++)
2436 {
2437 insn = ready[k];
2438 if ((cost = actual_hazard (insn_unit (insn), insn, clock, 0)) != 0)
2439 {
2440 q++;
2441 ready[k] = 0;
2442 queue_insn (insn, cost);
2443 if (file)
2444 fprintf (file, "\n;; blocking insn %d for %d cycles",
2445 INSN_UID (insn), cost);
2446 }
2447 }
2448 new_ready -= q;
2449
2450 /* Check the next group if all insns were queued. */
2451 if (j - i - q == 0)
2452 continue;
2453
2454 /* If more than one remains, select the first one with the largest
2455 potential hazard. */
2456 else if (j - i - q > 1)
2457 {
2458 best_cost = -1;
2459 for (k = i; k < j; k++)
2460 {
2461 if ((insn = ready[k]) == 0)
2462 continue;
2463 if ((cost = potential_hazard (insn_unit (insn), insn, 0))
2464 > best_cost)
2465 {
2466 best_cost = cost;
2467 best_insn = k;
2468 }
2469 }
2470 }
2471 /* We have found a suitable insn to schedule. */
2472 break;
2473 }
2474
2475 /* Move the best insn to be front of the ready list. */
2476 if (best_insn != 0)
2477 {
2478 if (file)
2479 {
2480 fprintf (file, ", now");
2481 for (i = 0; i < n_ready; i++)
2482 if (ready[i])
2483 fprintf (file, " %d", INSN_UID (ready[i]));
2484 fprintf (file, "\n;; insn %d has a greater potential hazard",
2485 INSN_UID (ready[best_insn]));
2486 }
2487 for (i = best_insn; i > 0; i--)
2488 {
2489 insn = ready[i-1];
2490 ready[i-1] = ready[i];
2491 ready[i] = insn;
2492 }
2493 }
2494
2495 /* Compact the ready list. */
2496 if (new_ready < n_ready)
2497 for (i = j = 0; i < n_ready; i++)
2498 if (ready[i])
2499 ready[j++] = ready[i];
2500
2501 return new_ready;
2502 }
2503
2504 /* Add a REG_DEAD note for REG to INSN, reusing a REG_DEAD note from the
2505 dead_notes list. */
2506
2507 static void
2508 create_reg_dead_note (reg, insn)
2509 rtx reg, insn;
2510 {
2511 rtx link = dead_notes;
2512
2513 if (link == 0)
2514 /* In theory, we should not end up with more REG_DEAD reg notes than we
2515 started with. In practice, this can occur as the result of bugs in
2516 flow, combine and/or sched. */
2517 {
2518 #if 1
2519 abort ();
2520 #else
2521 link = rtx_alloc (EXPR_LIST);
2522 PUT_REG_NOTE_KIND (link, REG_DEAD);
2523 #endif
2524 }
2525 else
2526 dead_notes = XEXP (dead_notes, 1);
2527
2528 XEXP (link, 0) = reg;
2529 XEXP (link, 1) = REG_NOTES (insn);
2530 REG_NOTES (insn) = link;
2531 }
2532
2533 /* Subroutine on attach_deaths_insn--handles the recursive search
2534 through INSN. If SET_P is true, then x is being modified by the insn. */
2535
2536 static void
2537 attach_deaths (x, insn, set_p)
2538 rtx x;
2539 rtx insn;
2540 int set_p;
2541 {
2542 register int i;
2543 register int j;
2544 register enum rtx_code code;
2545 register char *fmt;
2546
2547 if (x == 0)
2548 return;
2549
2550 code = GET_CODE (x);
2551
2552 switch (code)
2553 {
2554 case CONST_INT:
2555 case CONST_DOUBLE:
2556 case LABEL_REF:
2557 case SYMBOL_REF:
2558 case CONST:
2559 case CODE_LABEL:
2560 case PC:
2561 case CC0:
2562 /* Get rid of the easy cases first. */
2563 return;
2564
2565 case REG:
2566 {
2567 /* If the register dies in this insn, queue that note, and mark
2568 this register as needing to die. */
2569 /* This code is very similar to mark_used_1 (if set_p is false)
2570 and mark_set_1 (if set_p is true) in flow.c. */
2571
2572 register int regno = REGNO (x);
2573 register int offset = regno / REGSET_ELT_BITS;
2574 register REGSET_ELT_TYPE bit
2575 = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
2576 REGSET_ELT_TYPE all_needed = (old_live_regs[offset] & bit);
2577 REGSET_ELT_TYPE some_needed = (old_live_regs[offset] & bit);
2578
2579 if (set_p)
2580 return;
2581
2582 if (regno < FIRST_PSEUDO_REGISTER)
2583 {
2584 int n;
2585
2586 n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2587 while (--n > 0)
2588 {
2589 some_needed |= (old_live_regs[(regno + n) / REGSET_ELT_BITS]
2590 & ((REGSET_ELT_TYPE) 1
2591 << ((regno + n) % REGSET_ELT_BITS)));
2592 all_needed &= (old_live_regs[(regno + n) / REGSET_ELT_BITS]
2593 & ((REGSET_ELT_TYPE) 1
2594 << ((regno + n) % REGSET_ELT_BITS)));
2595 }
2596 }
2597
2598 /* If it wasn't live before we started, then add a REG_DEAD note.
2599 We must check the previous lifetime info not the current info,
2600 because we may have to execute this code several times, e.g.
2601 once for a clobber (which doesn't add a note) and later
2602 for a use (which does add a note).
2603
2604 Always make the register live. We must do this even if it was
2605 live before, because this may be an insn which sets and uses
2606 the same register, in which case the register has already been
2607 killed, so we must make it live again.
2608
2609 Global registers are always live, and should never have a REG_DEAD
2610 note added for them, so none of the code below applies to them. */
2611
2612 if (regno >= FIRST_PSEUDO_REGISTER || ! global_regs[regno])
2613 {
2614 /* Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2615 STACK_POINTER_REGNUM, since these are always considered to be
2616 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2617 if (regno != FRAME_POINTER_REGNUM
2618 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2619 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2620 #endif
2621 && regno != STACK_POINTER_REGNUM)
2622 {
2623 if (! all_needed && ! dead_or_set_p (insn, x))
2624 {
2625 /* If none of the words in X is needed, make a REG_DEAD
2626 note. Otherwise, we must make partial REG_DEAD
2627 notes. */
2628 if (! some_needed)
2629 create_reg_dead_note (x, insn);
2630 else
2631 {
2632 int i;
2633
2634 /* Don't make a REG_DEAD note for a part of a
2635 register that is set in the insn. */
2636 for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
2637 i >= 0; i--)
2638 if ((old_live_regs[(regno + i) / REGSET_ELT_BITS]
2639 & ((REGSET_ELT_TYPE) 1
2640 << ((regno +i) % REGSET_ELT_BITS))) == 0
2641 && ! dead_or_set_regno_p (insn, regno + i))
2642 create_reg_dead_note (gen_rtx (REG, word_mode,
2643 regno + i),
2644 insn);
2645 }
2646 }
2647 }
2648
2649 if (regno < FIRST_PSEUDO_REGISTER)
2650 {
2651 int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
2652 while (--j >= 0)
2653 {
2654 offset = (regno + j) / REGSET_ELT_BITS;
2655 bit
2656 = (REGSET_ELT_TYPE) 1 << ((regno + j) % REGSET_ELT_BITS);
2657
2658 bb_dead_regs[offset] &= ~bit;
2659 bb_live_regs[offset] |= bit;
2660 }
2661 }
2662 else
2663 {
2664 bb_dead_regs[offset] &= ~bit;
2665 bb_live_regs[offset] |= bit;
2666 }
2667 }
2668 return;
2669 }
2670
2671 case MEM:
2672 /* Handle tail-recursive case. */
2673 attach_deaths (XEXP (x, 0), insn, 0);
2674 return;
2675
2676 case SUBREG:
2677 case STRICT_LOW_PART:
2678 /* These two cases preserve the value of SET_P, so handle them
2679 separately. */
2680 attach_deaths (XEXP (x, 0), insn, set_p);
2681 return;
2682
2683 case ZERO_EXTRACT:
2684 case SIGN_EXTRACT:
2685 /* This case preserves the value of SET_P for the first operand, but
2686 clears it for the other two. */
2687 attach_deaths (XEXP (x, 0), insn, set_p);
2688 attach_deaths (XEXP (x, 1), insn, 0);
2689 attach_deaths (XEXP (x, 2), insn, 0);
2690 return;
2691
2692 default:
2693 /* Other cases: walk the insn. */
2694 fmt = GET_RTX_FORMAT (code);
2695 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2696 {
2697 if (fmt[i] == 'e')
2698 attach_deaths (XEXP (x, i), insn, 0);
2699 else if (fmt[i] == 'E')
2700 for (j = 0; j < XVECLEN (x, i); j++)
2701 attach_deaths (XVECEXP (x, i, j), insn, 0);
2702 }
2703 }
2704 }
2705
2706 /* After INSN has executed, add register death notes for each register
2707 that is dead after INSN. */
2708
2709 static void
2710 attach_deaths_insn (insn)
2711 rtx insn;
2712 {
2713 rtx x = PATTERN (insn);
2714 register RTX_CODE code = GET_CODE (x);
2715
2716 if (code == SET)
2717 {
2718 attach_deaths (SET_SRC (x), insn, 0);
2719
2720 /* A register might die here even if it is the destination, e.g.
2721 it is the target of a volatile read and is otherwise unused.
2722 Hence we must always call attach_deaths for the SET_DEST. */
2723 attach_deaths (SET_DEST (x), insn, 1);
2724 }
2725 else if (code == PARALLEL)
2726 {
2727 register int i;
2728 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
2729 {
2730 code = GET_CODE (XVECEXP (x, 0, i));
2731 if (code == SET)
2732 {
2733 attach_deaths (SET_SRC (XVECEXP (x, 0, i)), insn, 0);
2734
2735 attach_deaths (SET_DEST (XVECEXP (x, 0, i)), insn, 1);
2736 }
2737 /* Flow does not add REG_DEAD notes to registers that die in
2738 clobbers, so we can't either. */
2739 else if (code != CLOBBER)
2740 attach_deaths (XVECEXP (x, 0, i), insn, 0);
2741 }
2742 }
2743 /* Flow does not add REG_DEAD notes to registers that die in
2744 clobbers, so we can't either. */
2745 else if (code != CLOBBER)
2746 attach_deaths (x, insn, 0);
2747 }
2748
2749 /* Delete notes beginning with INSN and maybe put them in the chain
2750 of notes ended by NOTE_LIST.
2751 Returns the insn following the notes. */
2752
2753 static rtx
2754 unlink_notes (insn, tail)
2755 rtx insn, tail;
2756 {
2757 rtx prev = PREV_INSN (insn);
2758
2759 while (insn != tail && GET_CODE (insn) == NOTE)
2760 {
2761 rtx next = NEXT_INSN (insn);
2762 /* Delete the note from its current position. */
2763 if (prev)
2764 NEXT_INSN (prev) = next;
2765 if (next)
2766 PREV_INSN (next) = prev;
2767
2768 if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
2769 /* Record line-number notes so they can be reused. */
2770 LINE_NOTE (insn) = insn;
2771 else
2772 {
2773 /* Insert the note at the end of the notes list. */
2774 PREV_INSN (insn) = note_list;
2775 if (note_list)
2776 NEXT_INSN (note_list) = insn;
2777 note_list = insn;
2778 }
2779
2780 insn = next;
2781 }
2782 return insn;
2783 }
2784
2785 /* Data structure for keeping track of register information
2786 during that register's life. */
2787
2788 struct sometimes
2789 {
2790 short offset; short bit;
2791 short live_length; short calls_crossed;
2792 };
2793
2794 /* Constructor for `sometimes' data structure. */
2795
2796 static int
2797 new_sometimes_live (regs_sometimes_live, offset, bit, sometimes_max)
2798 struct sometimes *regs_sometimes_live;
2799 int offset, bit;
2800 int sometimes_max;
2801 {
2802 register struct sometimes *p;
2803 register int regno = offset * REGSET_ELT_BITS + bit;
2804 int i;
2805
2806 /* There should never be a register greater than max_regno here. If there
2807 is, it means that a define_split has created a new pseudo reg. This
2808 is not allowed, since there will not be flow info available for any
2809 new register, so catch the error here. */
2810 if (regno >= max_regno)
2811 abort ();
2812
2813 p = &regs_sometimes_live[sometimes_max];
2814 p->offset = offset;
2815 p->bit = bit;
2816 p->live_length = 0;
2817 p->calls_crossed = 0;
2818 sometimes_max++;
2819 return sometimes_max;
2820 }
2821
2822 /* Count lengths of all regs we are currently tracking,
2823 and find new registers no longer live. */
2824
2825 static void
2826 finish_sometimes_live (regs_sometimes_live, sometimes_max)
2827 struct sometimes *regs_sometimes_live;
2828 int sometimes_max;
2829 {
2830 int i;
2831
2832 for (i = 0; i < sometimes_max; i++)
2833 {
2834 register struct sometimes *p = &regs_sometimes_live[i];
2835 int regno;
2836
2837 regno = p->offset * REGSET_ELT_BITS + p->bit;
2838
2839 sched_reg_live_length[regno] += p->live_length;
2840 sched_reg_n_calls_crossed[regno] += p->calls_crossed;
2841 }
2842 }
2843
2844 /* Use modified list scheduling to rearrange insns in basic block
2845 B. FILE, if nonzero, is where we dump interesting output about
2846 this pass. */
2847
2848 static void
2849 schedule_block (b, file)
2850 int b;
2851 FILE *file;
2852 {
2853 rtx insn, last;
2854 rtx last_note = 0;
2855 rtx *ready, link;
2856 int i, j, n_ready = 0, new_ready, n_insns = 0;
2857 int sched_n_insns = 0;
2858 int clock;
2859 #define NEED_NOTHING 0
2860 #define NEED_HEAD 1
2861 #define NEED_TAIL 2
2862 int new_needs;
2863
2864 /* HEAD and TAIL delimit the region being scheduled. */
2865 rtx head = basic_block_head[b];
2866 rtx tail = basic_block_end[b];
2867 /* PREV_HEAD and NEXT_TAIL are the boundaries of the insns
2868 being scheduled. When the insns have been ordered,
2869 these insns delimit where the new insns are to be
2870 spliced back into the insn chain. */
2871 rtx next_tail;
2872 rtx prev_head;
2873
2874 /* Keep life information accurate. */
2875 register struct sometimes *regs_sometimes_live;
2876 int sometimes_max;
2877
2878 if (file)
2879 fprintf (file, ";;\t -- basic block number %d from %d to %d --\n",
2880 b, INSN_UID (basic_block_head[b]), INSN_UID (basic_block_end[b]));
2881
2882 i = max_reg_num ();
2883 reg_last_uses = (rtx *) alloca (i * sizeof (rtx));
2884 bzero (reg_last_uses, i * sizeof (rtx));
2885 reg_last_sets = (rtx *) alloca (i * sizeof (rtx));
2886 bzero (reg_last_sets, i * sizeof (rtx));
2887 clear_units ();
2888
2889 /* Remove certain insns at the beginning from scheduling,
2890 by advancing HEAD. */
2891
2892 /* At the start of a function, before reload has run, don't delay getting
2893 parameters from hard registers into pseudo registers. */
2894 if (reload_completed == 0 && b == 0)
2895 {
2896 while (head != tail
2897 && GET_CODE (head) == NOTE
2898 && NOTE_LINE_NUMBER (head) != NOTE_INSN_FUNCTION_BEG)
2899 head = NEXT_INSN (head);
2900 while (head != tail
2901 && GET_CODE (head) == INSN
2902 && GET_CODE (PATTERN (head)) == SET)
2903 {
2904 rtx src = SET_SRC (PATTERN (head));
2905 while (GET_CODE (src) == SUBREG
2906 || GET_CODE (src) == SIGN_EXTEND
2907 || GET_CODE (src) == ZERO_EXTEND
2908 || GET_CODE (src) == SIGN_EXTRACT
2909 || GET_CODE (src) == ZERO_EXTRACT)
2910 src = XEXP (src, 0);
2911 if (GET_CODE (src) != REG
2912 || REGNO (src) >= FIRST_PSEUDO_REGISTER)
2913 break;
2914 /* Keep this insn from ever being scheduled. */
2915 INSN_REF_COUNT (head) = 1;
2916 head = NEXT_INSN (head);
2917 }
2918 }
2919
2920 /* Don't include any notes or labels at the beginning of the
2921 basic block, or notes at the ends of basic blocks. */
2922 while (head != tail)
2923 {
2924 if (GET_CODE (head) == NOTE)
2925 head = NEXT_INSN (head);
2926 else if (GET_CODE (tail) == NOTE)
2927 tail = PREV_INSN (tail);
2928 else if (GET_CODE (head) == CODE_LABEL)
2929 head = NEXT_INSN (head);
2930 else break;
2931 }
2932 /* If the only insn left is a NOTE or a CODE_LABEL, then there is no need
2933 to schedule this block. */
2934 if (head == tail
2935 && (GET_CODE (head) == NOTE || GET_CODE (head) == CODE_LABEL))
2936 return;
2937
2938 #if 0
2939 /* This short-cut doesn't work. It does not count call insns crossed by
2940 registers in reg_sometimes_live. It does not mark these registers as
2941 dead if they die in this block. It does not mark these registers live
2942 (or create new reg_sometimes_live entries if necessary) if they are born
2943 in this block.
2944
2945 The easy solution is to just always schedule a block. This block only
2946 has one insn, so this won't slow down this pass by much. */
2947
2948 if (head == tail)
2949 return;
2950 #endif
2951
2952 /* Now HEAD through TAIL are the insns actually to be rearranged;
2953 Let PREV_HEAD and NEXT_TAIL enclose them. */
2954 prev_head = PREV_INSN (head);
2955 next_tail = NEXT_INSN (tail);
2956
2957 /* Initialize basic block data structures. */
2958 dead_notes = 0;
2959 pending_read_insns = 0;
2960 pending_read_mems = 0;
2961 pending_write_insns = 0;
2962 pending_write_mems = 0;
2963 pending_lists_length = 0;
2964 last_pending_memory_flush = 0;
2965 last_function_call = 0;
2966 last_scheduled_insn = 0;
2967
2968 LOG_LINKS (sched_before_next_call) = 0;
2969
2970 n_insns += sched_analyze (head, tail);
2971 if (n_insns == 0)
2972 {
2973 free_pending_lists ();
2974 return;
2975 }
2976
2977 /* Allocate vector to hold insns to be rearranged (except those
2978 insns which are controlled by an insn with SCHED_GROUP_P set).
2979 All these insns are included between ORIG_HEAD and ORIG_TAIL,
2980 as those variables ultimately are set up. */
2981 ready = (rtx *) alloca ((n_insns+1) * sizeof (rtx));
2982
2983 /* TAIL is now the last of the insns to be rearranged.
2984 Put those insns into the READY vector. */
2985 insn = tail;
2986
2987 /* For all branches, calls, uses, and cc0 setters, force them to remain
2988 in order at the end of the block by adding dependencies and giving
2989 the last a high priority. There may be notes present, and prev_head
2990 may also be a note.
2991
2992 Branches must obviously remain at the end. Calls should remain at the
2993 end since moving them results in worse register allocation. Uses remain
2994 at the end to ensure proper register allocation. cc0 setters remaim
2995 at the end because they can't be moved away from their cc0 user. */
2996 last = 0;
2997 while (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
2998 || (GET_CODE (insn) == INSN
2999 && (GET_CODE (PATTERN (insn)) == USE
3000 #ifdef HAVE_cc0
3001 || sets_cc0_p (PATTERN (insn))
3002 #endif
3003 ))
3004 || GET_CODE (insn) == NOTE)
3005 {
3006 if (GET_CODE (insn) != NOTE)
3007 {
3008 priority (insn);
3009 if (last == 0)
3010 {
3011 ready[n_ready++] = insn;
3012 INSN_PRIORITY (insn) = TAIL_PRIORITY - i;
3013 INSN_REF_COUNT (insn) = 0;
3014 }
3015 else if (! find_insn_list (insn, LOG_LINKS (last)))
3016 {
3017 add_dependence (last, insn, REG_DEP_ANTI);
3018 INSN_REF_COUNT (insn)++;
3019 }
3020 last = insn;
3021
3022 /* Skip over insns that are part of a group. */
3023 while (SCHED_GROUP_P (insn))
3024 {
3025 insn = prev_nonnote_insn (insn);
3026 priority (insn);
3027 }
3028 }
3029
3030 insn = PREV_INSN (insn);
3031 /* Don't overrun the bounds of the basic block. */
3032 if (insn == prev_head)
3033 break;
3034 }
3035
3036 /* Assign priorities to instructions. Also check whether they
3037 are in priority order already. If so then I will be nonnegative.
3038 We use this shortcut only before reloading. */
3039 #if 0
3040 i = reload_completed ? DONE_PRIORITY : MAX_PRIORITY;
3041 #endif
3042
3043 for (; insn != prev_head; insn = PREV_INSN (insn))
3044 {
3045 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3046 {
3047 priority (insn);
3048 if (INSN_REF_COUNT (insn) == 0)
3049 {
3050 if (last == 0)
3051 ready[n_ready++] = insn;
3052 else
3053 {
3054 /* Make this dependent on the last of the instructions
3055 that must remain in order at the end of the block. */
3056 add_dependence (last, insn, REG_DEP_ANTI);
3057 INSN_REF_COUNT (insn) = 1;
3058 }
3059 }
3060 if (SCHED_GROUP_P (insn))
3061 {
3062 while (SCHED_GROUP_P (insn))
3063 {
3064 insn = PREV_INSN (insn);
3065 while (GET_CODE (insn) == NOTE)
3066 insn = PREV_INSN (insn);
3067 priority (insn);
3068 }
3069 continue;
3070 }
3071 #if 0
3072 if (i < 0)
3073 continue;
3074 if (INSN_PRIORITY (insn) < i)
3075 i = INSN_PRIORITY (insn);
3076 else if (INSN_PRIORITY (insn) > i)
3077 i = DONE_PRIORITY;
3078 #endif
3079 }
3080 }
3081
3082 #if 0
3083 /* This short-cut doesn't work. It does not count call insns crossed by
3084 registers in reg_sometimes_live. It does not mark these registers as
3085 dead if they die in this block. It does not mark these registers live
3086 (or create new reg_sometimes_live entries if necessary) if they are born
3087 in this block.
3088
3089 The easy solution is to just always schedule a block. These blocks tend
3090 to be very short, so this doesn't slow down this pass by much. */
3091
3092 /* If existing order is good, don't bother to reorder. */
3093 if (i != DONE_PRIORITY)
3094 {
3095 if (file)
3096 fprintf (file, ";; already scheduled\n");
3097
3098 if (reload_completed == 0)
3099 {
3100 for (i = 0; i < sometimes_max; i++)
3101 regs_sometimes_live[i].live_length += n_insns;
3102
3103 finish_sometimes_live (regs_sometimes_live, sometimes_max);
3104 }
3105 free_pending_lists ();
3106 return;
3107 }
3108 #endif
3109
3110 /* Scan all the insns to be scheduled, removing NOTE insns
3111 and register death notes.
3112 Line number NOTE insns end up in NOTE_LIST.
3113 Register death notes end up in DEAD_NOTES.
3114
3115 Recreate the register life information for the end of this basic
3116 block. */
3117
3118 if (reload_completed == 0)
3119 {
3120 bcopy (basic_block_live_at_start[b], bb_live_regs, regset_bytes);
3121 bzero (bb_dead_regs, regset_bytes);
3122
3123 if (b == 0)
3124 {
3125 /* This is the first block in the function. There may be insns
3126 before head that we can't schedule. We still need to examine
3127 them though for accurate register lifetime analysis. */
3128
3129 /* We don't want to remove any REG_DEAD notes as the code below
3130 does. */
3131
3132 for (insn = basic_block_head[b]; insn != head;
3133 insn = NEXT_INSN (insn))
3134 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3135 {
3136 /* See if the register gets born here. */
3137 /* We must check for registers being born before we check for
3138 registers dying. It is possible for a register to be born
3139 and die in the same insn, e.g. reading from a volatile
3140 memory location into an otherwise unused register. Such
3141 a register must be marked as dead after this insn. */
3142 if (GET_CODE (PATTERN (insn)) == SET
3143 || GET_CODE (PATTERN (insn)) == CLOBBER)
3144 sched_note_set (b, PATTERN (insn), 0);
3145 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3146 {
3147 int j;
3148 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
3149 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
3150 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
3151 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
3152
3153 /* ??? This code is obsolete and should be deleted. It
3154 is harmless though, so we will leave it in for now. */
3155 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
3156 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
3157 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
3158 }
3159
3160 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
3161 {
3162 if ((REG_NOTE_KIND (link) == REG_DEAD
3163 || REG_NOTE_KIND (link) == REG_UNUSED)
3164 /* Verify that the REG_NOTE has a legal value. */
3165 && GET_CODE (XEXP (link, 0)) == REG)
3166 {
3167 register int regno = REGNO (XEXP (link, 0));
3168 register int offset = regno / REGSET_ELT_BITS;
3169 register REGSET_ELT_TYPE bit
3170 = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
3171
3172 if (regno < FIRST_PSEUDO_REGISTER)
3173 {
3174 int j = HARD_REGNO_NREGS (regno,
3175 GET_MODE (XEXP (link, 0)));
3176 while (--j >= 0)
3177 {
3178 offset = (regno + j) / REGSET_ELT_BITS;
3179 bit = ((REGSET_ELT_TYPE) 1
3180 << ((regno + j) % REGSET_ELT_BITS));
3181
3182 bb_live_regs[offset] &= ~bit;
3183 bb_dead_regs[offset] |= bit;
3184 }
3185 }
3186 else
3187 {
3188 bb_live_regs[offset] &= ~bit;
3189 bb_dead_regs[offset] |= bit;
3190 }
3191 }
3192 }
3193 }
3194 }
3195 }
3196
3197 /* If debugging information is being produced, keep track of the line
3198 number notes for each insn. */
3199 if (write_symbols != NO_DEBUG)
3200 {
3201 /* We must use the true line number for the first insn in the block
3202 that was computed and saved at the start of this pass. We can't
3203 use the current line number, because scheduling of the previous
3204 block may have changed the current line number. */
3205 rtx line = line_note_head[b];
3206
3207 for (insn = basic_block_head[b];
3208 insn != next_tail;
3209 insn = NEXT_INSN (insn))
3210 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
3211 line = insn;
3212 else
3213 LINE_NOTE (insn) = line;
3214 }
3215
3216 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3217 {
3218 rtx prev, next, link;
3219
3220 /* Farm out notes. This is needed to keep the debugger from
3221 getting completely deranged. */
3222 if (GET_CODE (insn) == NOTE)
3223 {
3224 prev = insn;
3225 insn = unlink_notes (insn, next_tail);
3226 if (prev == tail)
3227 abort ();
3228 if (prev == head)
3229 abort ();
3230 if (insn == next_tail)
3231 abort ();
3232 }
3233
3234 if (reload_completed == 0
3235 && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3236 {
3237 /* See if the register gets born here. */
3238 /* We must check for registers being born before we check for
3239 registers dying. It is possible for a register to be born and
3240 die in the same insn, e.g. reading from a volatile memory
3241 location into an otherwise unused register. Such a register
3242 must be marked as dead after this insn. */
3243 if (GET_CODE (PATTERN (insn)) == SET
3244 || GET_CODE (PATTERN (insn)) == CLOBBER)
3245 sched_note_set (b, PATTERN (insn), 0);
3246 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3247 {
3248 int j;
3249 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
3250 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
3251 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
3252 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
3253
3254 /* ??? This code is obsolete and should be deleted. It
3255 is harmless though, so we will leave it in for now. */
3256 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
3257 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
3258 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
3259 }
3260
3261 /* Need to know what registers this insn kills. */
3262 for (prev = 0, link = REG_NOTES (insn); link; link = next)
3263 {
3264 int regno;
3265
3266 next = XEXP (link, 1);
3267 if ((REG_NOTE_KIND (link) == REG_DEAD
3268 || REG_NOTE_KIND (link) == REG_UNUSED)
3269 /* Verify that the REG_NOTE has a legal value. */
3270 && GET_CODE (XEXP (link, 0)) == REG)
3271 {
3272 register int regno = REGNO (XEXP (link, 0));
3273 register int offset = regno / REGSET_ELT_BITS;
3274 register REGSET_ELT_TYPE bit
3275 = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
3276
3277 /* Only unlink REG_DEAD notes; leave REG_UNUSED notes
3278 alone. */
3279 if (REG_NOTE_KIND (link) == REG_DEAD)
3280 {
3281 if (prev)
3282 XEXP (prev, 1) = next;
3283 else
3284 REG_NOTES (insn) = next;
3285 XEXP (link, 1) = dead_notes;
3286 dead_notes = link;
3287 }
3288 else
3289 prev = link;
3290
3291 if (regno < FIRST_PSEUDO_REGISTER)
3292 {
3293 int j = HARD_REGNO_NREGS (regno,
3294 GET_MODE (XEXP (link, 0)));
3295 while (--j >= 0)
3296 {
3297 offset = (regno + j) / REGSET_ELT_BITS;
3298 bit = ((REGSET_ELT_TYPE) 1
3299 << ((regno + j) % REGSET_ELT_BITS));
3300
3301 bb_live_regs[offset] &= ~bit;
3302 bb_dead_regs[offset] |= bit;
3303 }
3304 }
3305 else
3306 {
3307 bb_live_regs[offset] &= ~bit;
3308 bb_dead_regs[offset] |= bit;
3309 }
3310 }
3311 else
3312 prev = link;
3313 }
3314 }
3315 }
3316
3317 if (reload_completed == 0)
3318 {
3319 /* Keep track of register lives. */
3320 old_live_regs = (regset) alloca (regset_bytes);
3321 regs_sometimes_live
3322 = (struct sometimes *) alloca (max_regno * sizeof (struct sometimes));
3323 sometimes_max = 0;
3324
3325 /* Start with registers live at end. */
3326 for (j = 0; j < regset_size; j++)
3327 {
3328 REGSET_ELT_TYPE live = bb_live_regs[j];
3329 old_live_regs[j] = live;
3330 if (live)
3331 {
3332 register REGSET_ELT_TYPE bit;
3333 for (bit = 0; bit < REGSET_ELT_BITS; bit++)
3334 if (live & ((REGSET_ELT_TYPE) 1 << bit))
3335 sometimes_max = new_sometimes_live (regs_sometimes_live, j,
3336 bit, sometimes_max);
3337 }
3338 }
3339 }
3340
3341 SCHED_SORT (ready, n_ready, 1);
3342
3343 if (file)
3344 {
3345 fprintf (file, ";; ready list initially:\n;; ");
3346 for (i = 0; i < n_ready; i++)
3347 fprintf (file, "%d ", INSN_UID (ready[i]));
3348 fprintf (file, "\n\n");
3349
3350 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3351 if (INSN_PRIORITY (insn) > 0)
3352 fprintf (file, ";; insn[%4d]: priority = %4d, ref_count = %4d\n",
3353 INSN_UID (insn), INSN_PRIORITY (insn),
3354 INSN_REF_COUNT (insn));
3355 }
3356
3357 /* Now HEAD and TAIL are going to become disconnected
3358 entirely from the insn chain. */
3359 tail = 0;
3360
3361 /* Q_SIZE will always be zero here. */
3362 q_ptr = 0; clock = 0;
3363 bzero (insn_queue, sizeof (insn_queue));
3364
3365 /* Now, perform list scheduling. */
3366
3367 /* Where we start inserting insns is after TAIL. */
3368 last = next_tail;
3369
3370 new_needs = (NEXT_INSN (prev_head) == basic_block_head[b]
3371 ? NEED_HEAD : NEED_NOTHING);
3372 if (PREV_INSN (next_tail) == basic_block_end[b])
3373 new_needs |= NEED_TAIL;
3374
3375 new_ready = n_ready;
3376 while (sched_n_insns < n_insns)
3377 {
3378 q_ptr = NEXT_Q (q_ptr); clock++;
3379
3380 /* Add all pending insns that can be scheduled without stalls to the
3381 ready list. */
3382 for (insn = insn_queue[q_ptr]; insn; insn = NEXT_INSN (insn))
3383 {
3384 if (file)
3385 fprintf (file, ";; launching %d before %d with no stalls at T-%d\n",
3386 INSN_UID (insn), INSN_UID (last), clock);
3387 ready[new_ready++] = insn;
3388 q_size -= 1;
3389 }
3390 insn_queue[q_ptr] = 0;
3391
3392 /* If there are no ready insns, stall until one is ready and add all
3393 of the pending insns at that point to the ready list. */
3394 if (new_ready == 0)
3395 {
3396 register int stalls;
3397
3398 for (stalls = 1; stalls < INSN_QUEUE_SIZE; stalls++)
3399 if (insn = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)])
3400 {
3401 for (; insn; insn = NEXT_INSN (insn))
3402 {
3403 if (file)
3404 fprintf (file, ";; launching %d before %d with %d stalls at T-%d\n",
3405 INSN_UID (insn), INSN_UID (last), stalls, clock);
3406 ready[new_ready++] = insn;
3407 q_size -= 1;
3408 }
3409 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
3410 break;
3411 }
3412
3413 q_ptr = NEXT_Q_AFTER (q_ptr, stalls); clock += stalls;
3414 }
3415
3416 /* There should be some instructions waiting to fire. */
3417 if (new_ready == 0)
3418 abort ();
3419
3420 if (file)
3421 {
3422 fprintf (file, ";; ready list at T-%d:", clock);
3423 for (i = 0; i < new_ready; i++)
3424 fprintf (file, " %d (%x)",
3425 INSN_UID (ready[i]), INSN_PRIORITY (ready[i]));
3426 }
3427
3428 /* Sort the ready list and choose the best insn to schedule. Select
3429 which insn should issue in this cycle and queue those that are
3430 blocked by function unit hazards.
3431
3432 N_READY holds the number of items that were scheduled the last time,
3433 minus the one instruction scheduled on the last loop iteration; it
3434 is not modified for any other reason in this loop. */
3435
3436 SCHED_SORT (ready, new_ready, n_ready);
3437 if (MAX_BLOCKAGE > 1)
3438 {
3439 new_ready = schedule_select (ready, new_ready, clock, file);
3440 if (new_ready == 0)
3441 {
3442 if (file)
3443 fprintf (file, "\n");
3444 /* We must set n_ready here, to ensure that sorting always
3445 occurs when we come back to the SCHED_SORT line above. */
3446 n_ready = 0;
3447 continue;
3448 }
3449 }
3450 n_ready = new_ready;
3451 last_scheduled_insn = insn = ready[0];
3452
3453 /* The first insn scheduled becomes the new tail. */
3454 if (tail == 0)
3455 tail = insn;
3456
3457 if (file)
3458 {
3459 fprintf (file, ", now");
3460 for (i = 0; i < n_ready; i++)
3461 fprintf (file, " %d", INSN_UID (ready[i]));
3462 fprintf (file, "\n");
3463 }
3464
3465 if (DONE_PRIORITY_P (insn))
3466 abort ();
3467
3468 if (reload_completed == 0)
3469 {
3470 /* Process this insn, and each insn linked to this one which must
3471 be immediately output after this insn. */
3472 do
3473 {
3474 /* First we kill registers set by this insn, and then we
3475 make registers used by this insn live. This is the opposite
3476 order used above because we are traversing the instructions
3477 backwards. */
3478
3479 /* Strictly speaking, we should scan REG_UNUSED notes and make
3480 every register mentioned there live, however, we will just
3481 kill them again immediately below, so there doesn't seem to
3482 be any reason why we bother to do this. */
3483
3484 /* See if this is the last notice we must take of a register. */
3485 if (GET_CODE (PATTERN (insn)) == SET
3486 || GET_CODE (PATTERN (insn)) == CLOBBER)
3487 sched_note_set (b, PATTERN (insn), 1);
3488 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3489 {
3490 int j;
3491 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
3492 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
3493 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
3494 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 1);
3495 }
3496
3497 /* This code keeps life analysis information up to date. */
3498 if (GET_CODE (insn) == CALL_INSN)
3499 {
3500 register struct sometimes *p;
3501
3502 /* A call kills all call used and global registers, except
3503 for those mentioned in the call pattern which will be
3504 made live again later. */
3505 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3506 if (call_used_regs[i] || global_regs[i])
3507 {
3508 register int offset = i / REGSET_ELT_BITS;
3509 register REGSET_ELT_TYPE bit
3510 = (REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS);
3511
3512 bb_live_regs[offset] &= ~bit;
3513 bb_dead_regs[offset] |= bit;
3514 }
3515
3516 /* Regs live at the time of a call instruction must not
3517 go in a register clobbered by calls. Record this for
3518 all regs now live. Note that insns which are born or
3519 die in a call do not cross a call, so this must be done
3520 after the killings (above) and before the births
3521 (below). */
3522 p = regs_sometimes_live;
3523 for (i = 0; i < sometimes_max; i++, p++)
3524 if (bb_live_regs[p->offset]
3525 & ((REGSET_ELT_TYPE) 1 << p->bit))
3526 p->calls_crossed += 1;
3527 }
3528
3529 /* Make every register used live, and add REG_DEAD notes for
3530 registers which were not live before we started. */
3531 attach_deaths_insn (insn);
3532
3533 /* Find registers now made live by that instruction. */
3534 for (i = 0; i < regset_size; i++)
3535 {
3536 REGSET_ELT_TYPE diff = bb_live_regs[i] & ~old_live_regs[i];
3537 if (diff)
3538 {
3539 register int bit;
3540 old_live_regs[i] |= diff;
3541 for (bit = 0; bit < REGSET_ELT_BITS; bit++)
3542 if (diff & ((REGSET_ELT_TYPE) 1 << bit))
3543 sometimes_max
3544 = new_sometimes_live (regs_sometimes_live, i, bit,
3545 sometimes_max);
3546 }
3547 }
3548
3549 /* Count lengths of all regs we are worrying about now,
3550 and handle registers no longer live. */
3551
3552 for (i = 0; i < sometimes_max; i++)
3553 {
3554 register struct sometimes *p = &regs_sometimes_live[i];
3555 int regno = p->offset*REGSET_ELT_BITS + p->bit;
3556
3557 p->live_length += 1;
3558
3559 if ((bb_live_regs[p->offset]
3560 & ((REGSET_ELT_TYPE) 1 << p->bit)) == 0)
3561 {
3562 /* This is the end of one of this register's lifetime
3563 segments. Save the lifetime info collected so far,
3564 and clear its bit in the old_live_regs entry. */
3565 sched_reg_live_length[regno] += p->live_length;
3566 sched_reg_n_calls_crossed[regno] += p->calls_crossed;
3567 old_live_regs[p->offset]
3568 &= ~((REGSET_ELT_TYPE) 1 << p->bit);
3569
3570 /* Delete the reg_sometimes_live entry for this reg by
3571 copying the last entry over top of it. */
3572 *p = regs_sometimes_live[--sometimes_max];
3573 /* ...and decrement i so that this newly copied entry
3574 will be processed. */
3575 i--;
3576 }
3577 }
3578
3579 link = insn;
3580 insn = PREV_INSN (insn);
3581 }
3582 while (SCHED_GROUP_P (link));
3583
3584 /* Set INSN back to the insn we are scheduling now. */
3585 insn = ready[0];
3586 }
3587
3588 /* Schedule INSN. Remove it from the ready list. */
3589 ready += 1;
3590 n_ready -= 1;
3591
3592 sched_n_insns += 1;
3593 NEXT_INSN (insn) = last;
3594 PREV_INSN (last) = insn;
3595 last = insn;
3596
3597 /* Everything that precedes INSN now either becomes "ready", if
3598 it can execute immediately before INSN, or "pending", if
3599 there must be a delay. Give INSN high enough priority that
3600 at least one (maybe more) reg-killing insns can be launched
3601 ahead of all others. Mark INSN as scheduled by changing its
3602 priority to -1. */
3603 INSN_PRIORITY (insn) = LAUNCH_PRIORITY;
3604 new_ready = schedule_insn (insn, ready, n_ready, clock);
3605 INSN_PRIORITY (insn) = DONE_PRIORITY;
3606
3607 /* Schedule all prior insns that must not be moved. */
3608 if (SCHED_GROUP_P (insn))
3609 {
3610 /* Disable these insns from being launched. */
3611 link = insn;
3612 while (SCHED_GROUP_P (link))
3613 {
3614 /* Disable these insns from being launched by anybody. */
3615 link = PREV_INSN (link);
3616 INSN_REF_COUNT (link) = 0;
3617 }
3618
3619 /* None of these insns can move forward into delay slots. */
3620 while (SCHED_GROUP_P (insn))
3621 {
3622 insn = PREV_INSN (insn);
3623 new_ready = schedule_insn (insn, ready, new_ready, clock);
3624 INSN_PRIORITY (insn) = DONE_PRIORITY;
3625
3626 sched_n_insns += 1;
3627 NEXT_INSN (insn) = last;
3628 PREV_INSN (last) = insn;
3629 last = insn;
3630 }
3631 }
3632 }
3633 if (q_size != 0)
3634 abort ();
3635
3636 if (reload_completed == 0)
3637 finish_sometimes_live (regs_sometimes_live, sometimes_max);
3638
3639 /* HEAD is now the first insn in the chain of insns that
3640 been scheduled by the loop above.
3641 TAIL is the last of those insns. */
3642 head = insn;
3643
3644 /* NOTE_LIST is the end of a chain of notes previously found
3645 among the insns. Insert them at the beginning of the insns. */
3646 if (note_list != 0)
3647 {
3648 rtx note_head = note_list;
3649 while (PREV_INSN (note_head))
3650 note_head = PREV_INSN (note_head);
3651
3652 PREV_INSN (head) = note_list;
3653 NEXT_INSN (note_list) = head;
3654 head = note_head;
3655 }
3656
3657 /* In theory, there should be no REG_DEAD notes leftover at the end.
3658 In practice, this can occur as the result of bugs in flow, combine.c,
3659 and/or sched.c. The values of the REG_DEAD notes remaining are
3660 meaningless, because dead_notes is just used as a free list. */
3661 #if 1
3662 if (dead_notes != 0)
3663 abort ();
3664 #endif
3665
3666 if (new_needs & NEED_HEAD)
3667 basic_block_head[b] = head;
3668 PREV_INSN (head) = prev_head;
3669 NEXT_INSN (prev_head) = head;
3670
3671 if (new_needs & NEED_TAIL)
3672 basic_block_end[b] = tail;
3673 NEXT_INSN (tail) = next_tail;
3674 PREV_INSN (next_tail) = tail;
3675
3676 /* Restore the line-number notes of each insn. */
3677 if (write_symbols != NO_DEBUG)
3678 {
3679 rtx line, note, prev, new;
3680 int notes = 0;
3681
3682 head = basic_block_head[b];
3683 next_tail = NEXT_INSN (basic_block_end[b]);
3684
3685 /* Determine the current line-number. We want to know the current
3686 line number of the first insn of the block here, in case it is
3687 different from the true line number that was saved earlier. If
3688 different, then we need a line number note before the first insn
3689 of this block. If it happens to be the same, then we don't want to
3690 emit another line number note here. */
3691 for (line = head; line; line = PREV_INSN (line))
3692 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
3693 break;
3694
3695 /* Walk the insns keeping track of the current line-number and inserting
3696 the line-number notes as needed. */
3697 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3698 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
3699 line = insn;
3700 else if (! (GET_CODE (insn) == NOTE
3701 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
3702 && (note = LINE_NOTE (insn)) != 0
3703 && note != line
3704 && (line == 0
3705 || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
3706 || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
3707 {
3708 line = note;
3709 prev = PREV_INSN (insn);
3710 if (LINE_NOTE (note))
3711 {
3712 /* Re-use the original line-number note. */
3713 LINE_NOTE (note) = 0;
3714 PREV_INSN (note) = prev;
3715 NEXT_INSN (prev) = note;
3716 PREV_INSN (insn) = note;
3717 NEXT_INSN (note) = insn;
3718 }
3719 else
3720 {
3721 notes++;
3722 new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
3723 NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
3724 }
3725 }
3726 if (file && notes)
3727 fprintf (file, ";; added %d line-number notes\n", notes);
3728 }
3729
3730 if (file)
3731 {
3732 fprintf (file, ";; total time = %d\n;; new basic block head = %d\n;; new basic block end = %d\n\n",
3733 clock, INSN_UID (basic_block_head[b]), INSN_UID (basic_block_end[b]));
3734 }
3735
3736 /* Yow! We're done! */
3737 free_pending_lists ();
3738
3739 return;
3740 }
3741 \f
3742 /* Subroutine of split_hard_reg_notes. Searches X for any reference to
3743 REGNO, returning the rtx of the reference found if any. Otherwise,
3744 returns 0. */
3745
3746 rtx
3747 regno_use_in (regno, x)
3748 int regno;
3749 rtx x;
3750 {
3751 register char *fmt;
3752 int i, j;
3753 rtx tem;
3754
3755 if (GET_CODE (x) == REG && REGNO (x) == regno)
3756 return x;
3757
3758 fmt = GET_RTX_FORMAT (GET_CODE (x));
3759 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3760 {
3761 if (fmt[i] == 'e')
3762 {
3763 if (tem = regno_use_in (regno, XEXP (x, i)))
3764 return tem;
3765 }
3766 else if (fmt[i] == 'E')
3767 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3768 if (tem = regno_use_in (regno , XVECEXP (x, i, j)))
3769 return tem;
3770 }
3771
3772 return 0;
3773 }
3774
3775 /* Subroutine of update_flow_info. Determines whether any new REG_NOTEs are
3776 needed for the hard register mentioned in the note. This can happen
3777 if the reference to the hard register in the original insn was split into
3778 several smaller hard register references in the split insns. */
3779
3780 static void
3781 split_hard_reg_notes (note, first, last, orig_insn)
3782 rtx note, first, last, orig_insn;
3783 {
3784 rtx reg, temp, link;
3785 int n_regs, i, new_reg;
3786 rtx insn;
3787
3788 /* Assume that this is a REG_DEAD note. */
3789 if (REG_NOTE_KIND (note) != REG_DEAD)
3790 abort ();
3791
3792 reg = XEXP (note, 0);
3793
3794 n_regs = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
3795
3796 /* ??? Could add check here to see whether, the hard register is referenced
3797 in the same mode as in the original insn. If so, then it has not been
3798 split, and the rest of the code below is unnecessary. */
3799
3800 for (i = 1; i < n_regs; i++)
3801 {
3802 new_reg = REGNO (reg) + i;
3803
3804 /* Check for references to new_reg in the split insns. */
3805 for (insn = last; ; insn = PREV_INSN (insn))
3806 {
3807 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
3808 && (temp = regno_use_in (new_reg, PATTERN (insn))))
3809 {
3810 /* Create a new reg dead note here. */
3811 link = rtx_alloc (EXPR_LIST);
3812 PUT_REG_NOTE_KIND (link, REG_DEAD);
3813 XEXP (link, 0) = temp;
3814 XEXP (link, 1) = REG_NOTES (insn);
3815 REG_NOTES (insn) = link;
3816 break;
3817 }
3818 /* It isn't mentioned anywhere, so no new reg note is needed for
3819 this register. */
3820 if (insn == first)
3821 break;
3822 }
3823 }
3824 }
3825
3826 /* Subroutine of update_flow_info. Determines whether a SET or CLOBBER in an
3827 insn created by splitting needs a REG_DEAD or REG_UNUSED note added. */
3828
3829 static void
3830 new_insn_dead_notes (pat, insn, last, orig_insn)
3831 rtx pat, insn, last, orig_insn;
3832 {
3833 rtx dest, tem, set;
3834
3835 /* PAT is either a CLOBBER or a SET here. */
3836 dest = XEXP (pat, 0);
3837
3838 while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
3839 || GET_CODE (dest) == STRICT_LOW_PART
3840 || GET_CODE (dest) == SIGN_EXTRACT)
3841 dest = XEXP (dest, 0);
3842
3843 if (GET_CODE (dest) == REG)
3844 {
3845 for (tem = last; tem != insn; tem = PREV_INSN (tem))
3846 {
3847 if (GET_RTX_CLASS (GET_CODE (tem)) == 'i'
3848 && reg_overlap_mentioned_p (dest, PATTERN (tem))
3849 && (set = single_set (tem)))
3850 {
3851 rtx tem_dest = SET_DEST (set);
3852
3853 while (GET_CODE (tem_dest) == ZERO_EXTRACT
3854 || GET_CODE (tem_dest) == SUBREG
3855 || GET_CODE (tem_dest) == STRICT_LOW_PART
3856 || GET_CODE (tem_dest) == SIGN_EXTRACT)
3857 tem_dest = XEXP (tem_dest, 0);
3858
3859 if (tem_dest != dest)
3860 {
3861 /* Use the same scheme as combine.c, don't put both REG_DEAD
3862 and REG_UNUSED notes on the same insn. */
3863 if (! find_regno_note (tem, REG_UNUSED, REGNO (dest))
3864 && ! find_regno_note (tem, REG_DEAD, REGNO (dest)))
3865 {
3866 rtx note = rtx_alloc (EXPR_LIST);
3867 PUT_REG_NOTE_KIND (note, REG_DEAD);
3868 XEXP (note, 0) = dest;
3869 XEXP (note, 1) = REG_NOTES (tem);
3870 REG_NOTES (tem) = note;
3871 }
3872 /* The reg only dies in one insn, the last one that uses
3873 it. */
3874 break;
3875 }
3876 else if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
3877 /* We found an instruction that both uses the register,
3878 and sets it, so no new REG_NOTE is needed for this set. */
3879 break;
3880 }
3881 }
3882 /* If this is a set, it must die somewhere, unless it is the dest of
3883 the original insn, and hence is live after the original insn. Abort
3884 if it isn't supposed to be live after the original insn.
3885
3886 If this is a clobber, then just add a REG_UNUSED note. */
3887 if (tem == insn)
3888 {
3889 int live_after_orig_insn = 0;
3890 rtx pattern = PATTERN (orig_insn);
3891 int i;
3892
3893 if (GET_CODE (pat) == CLOBBER)
3894 {
3895 rtx note = rtx_alloc (EXPR_LIST);
3896 PUT_REG_NOTE_KIND (note, REG_UNUSED);
3897 XEXP (note, 0) = dest;
3898 XEXP (note, 1) = REG_NOTES (insn);
3899 REG_NOTES (insn) = note;
3900 return;
3901 }
3902
3903 /* The original insn could have multiple sets, so search the
3904 insn for all sets. */
3905 if (GET_CODE (pattern) == SET)
3906 {
3907 if (reg_overlap_mentioned_p (dest, SET_DEST (pattern)))
3908 live_after_orig_insn = 1;
3909 }
3910 else if (GET_CODE (pattern) == PARALLEL)
3911 {
3912 for (i = 0; i < XVECLEN (pattern, 0); i++)
3913 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET
3914 && reg_overlap_mentioned_p (dest,
3915 SET_DEST (XVECEXP (pattern,
3916 0, i))))
3917 live_after_orig_insn = 1;
3918 }
3919
3920 if (! live_after_orig_insn)
3921 abort ();
3922 }
3923 }
3924 }
3925
3926 /* Subroutine of update_flow_info. Update the value of reg_n_sets for all
3927 registers modified by X. INC is -1 if the containing insn is being deleted,
3928 and is 1 if the containing insn is a newly generated insn. */
3929
3930 static void
3931 update_n_sets (x, inc)
3932 rtx x;
3933 int inc;
3934 {
3935 rtx dest = SET_DEST (x);
3936
3937 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
3938 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
3939 dest = SUBREG_REG (dest);
3940
3941 if (GET_CODE (dest) == REG)
3942 {
3943 int regno = REGNO (dest);
3944
3945 if (regno < FIRST_PSEUDO_REGISTER)
3946 {
3947 register int i;
3948 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (dest));
3949
3950 for (i = regno; i < endregno; i++)
3951 reg_n_sets[i] += inc;
3952 }
3953 else
3954 reg_n_sets[regno] += inc;
3955 }
3956 }
3957
3958 /* Updates all flow-analysis related quantities (including REG_NOTES) for
3959 the insns from FIRST to LAST inclusive that were created by splitting
3960 ORIG_INSN. NOTES are the original REG_NOTES. */
3961
3962 static void
3963 update_flow_info (notes, first, last, orig_insn)
3964 rtx notes;
3965 rtx first, last;
3966 rtx orig_insn;
3967 {
3968 rtx insn, note;
3969 rtx next;
3970 rtx orig_dest, temp;
3971 rtx set;
3972
3973 /* Get and save the destination set by the original insn. */
3974
3975 orig_dest = single_set (orig_insn);
3976 if (orig_dest)
3977 orig_dest = SET_DEST (orig_dest);
3978
3979 /* Move REG_NOTES from the original insn to where they now belong. */
3980
3981 for (note = notes; note; note = next)
3982 {
3983 next = XEXP (note, 1);
3984 switch (REG_NOTE_KIND (note))
3985 {
3986 case REG_DEAD:
3987 case REG_UNUSED:
3988 /* Move these notes from the original insn to the last new insn where
3989 the register is now set. */
3990
3991 for (insn = last; ; insn = PREV_INSN (insn))
3992 {
3993 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
3994 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
3995 {
3996 XEXP (note, 1) = REG_NOTES (insn);
3997 REG_NOTES (insn) = note;
3998
3999 /* Sometimes need to convert REG_UNUSED notes to REG_DEAD
4000 notes. */
4001 /* ??? This won't handle multiple word registers correctly,
4002 but should be good enough for now. */
4003 if (REG_NOTE_KIND (note) == REG_UNUSED
4004 && ! dead_or_set_p (insn, XEXP (note, 0)))
4005 PUT_REG_NOTE_KIND (note, REG_DEAD);
4006
4007 /* The reg only dies in one insn, the last one that uses
4008 it. */
4009 break;
4010 }
4011 /* It must die somewhere, fail it we couldn't find where it died.
4012
4013 If this is a REG_UNUSED note, then it must be a temporary
4014 register that was not needed by this instantiation of the
4015 pattern, so we can safely ignore it. */
4016 if (insn == first)
4017 {
4018 if (REG_NOTE_KIND (note) != REG_UNUSED)
4019 abort ();
4020
4021 break;
4022 }
4023 }
4024
4025 /* If this note refers to a multiple word hard register, it may
4026 have been split into several smaller hard register references.
4027 Check to see if there are any new register references that
4028 need REG_NOTES added for them. */
4029 temp = XEXP (note, 0);
4030 if (REG_NOTE_KIND (note) == REG_DEAD
4031 && GET_CODE (temp) == REG
4032 && REGNO (temp) < FIRST_PSEUDO_REGISTER
4033 && HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)))
4034 split_hard_reg_notes (note, first, last, orig_insn);
4035 break;
4036
4037 case REG_WAS_0:
4038 /* This note applies to the dest of the original insn. Find the
4039 first new insn that now has the same dest, and move the note
4040 there. */
4041
4042 if (! orig_dest)
4043 abort ();
4044
4045 for (insn = first; ; insn = NEXT_INSN (insn))
4046 {
4047 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
4048 && (temp = single_set (insn))
4049 && rtx_equal_p (SET_DEST (temp), orig_dest))
4050 {
4051 XEXP (note, 1) = REG_NOTES (insn);
4052 REG_NOTES (insn) = note;
4053 /* The reg is only zero before one insn, the first that
4054 uses it. */
4055 break;
4056 }
4057 /* It must be set somewhere, fail if we couldn't find where it
4058 was set. */
4059 if (insn == last)
4060 abort ();
4061 }
4062 break;
4063
4064 case REG_EQUAL:
4065 case REG_EQUIV:
4066 /* A REG_EQUIV or REG_EQUAL note on an insn with more than one
4067 set is meaningless. Just drop the note. */
4068 if (! orig_dest)
4069 break;
4070
4071 case REG_NO_CONFLICT:
4072 /* These notes apply to the dest of the original insn. Find the last
4073 new insn that now has the same dest, and move the note there. */
4074
4075 if (! orig_dest)
4076 abort ();
4077
4078 for (insn = last; ; insn = PREV_INSN (insn))
4079 {
4080 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
4081 && (temp = single_set (insn))
4082 && rtx_equal_p (SET_DEST (temp), orig_dest))
4083 {
4084 XEXP (note, 1) = REG_NOTES (insn);
4085 REG_NOTES (insn) = note;
4086 /* Only put this note on one of the new insns. */
4087 break;
4088 }
4089
4090 /* The original dest must still be set someplace. Abort if we
4091 couldn't find it. */
4092 if (insn == first)
4093 abort ();
4094 }
4095 break;
4096
4097 case REG_LIBCALL:
4098 /* Move a REG_LIBCALL note to the first insn created, and update
4099 the corresponding REG_RETVAL note. */
4100 XEXP (note, 1) = REG_NOTES (first);
4101 REG_NOTES (first) = note;
4102
4103 insn = XEXP (note, 0);
4104 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
4105 if (note)
4106 XEXP (note, 0) = first;
4107 break;
4108
4109 case REG_RETVAL:
4110 /* Move a REG_RETVAL note to the last insn created, and update
4111 the corresponding REG_LIBCALL note. */
4112 XEXP (note, 1) = REG_NOTES (last);
4113 REG_NOTES (last) = note;
4114
4115 insn = XEXP (note, 0);
4116 note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
4117 if (note)
4118 XEXP (note, 0) = last;
4119 break;
4120
4121 case REG_NONNEG:
4122 /* This should be moved to whichever instruction is a JUMP_INSN. */
4123
4124 for (insn = last; ; insn = PREV_INSN (insn))
4125 {
4126 if (GET_CODE (insn) == JUMP_INSN)
4127 {
4128 XEXP (note, 1) = REG_NOTES (insn);
4129 REG_NOTES (insn) = note;
4130 /* Only put this note on one of the new insns. */
4131 break;
4132 }
4133 /* Fail if we couldn't find a JUMP_INSN. */
4134 if (insn == first)
4135 abort ();
4136 }
4137 break;
4138
4139 case REG_INC:
4140 /* This should be moved to whichever instruction now has the
4141 increment operation. */
4142 abort ();
4143
4144 case REG_LABEL:
4145 /* Should be moved to the new insn(s) which use the label. */
4146 for (insn = first; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
4147 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
4148 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
4149 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL,
4150 XEXP (note, 0), REG_NOTES (insn));
4151 break;
4152
4153 case REG_CC_SETTER:
4154 case REG_CC_USER:
4155 /* These two notes will never appear until after reorg, so we don't
4156 have to handle them here. */
4157 default:
4158 abort ();
4159 }
4160 }
4161
4162 /* Each new insn created, except the last, has a new set. If the destination
4163 is a register, then this reg is now live across several insns, whereas
4164 previously the dest reg was born and died within the same insn. To
4165 reflect this, we now need a REG_DEAD note on the insn where this
4166 dest reg dies.
4167
4168 Similarly, the new insns may have clobbers that need REG_UNUSED notes. */
4169
4170 for (insn = first; insn != last; insn = NEXT_INSN (insn))
4171 {
4172 rtx pat;
4173 int i;
4174
4175 pat = PATTERN (insn);
4176 if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
4177 new_insn_dead_notes (pat, insn, last, orig_insn);
4178 else if (GET_CODE (pat) == PARALLEL)
4179 {
4180 for (i = 0; i < XVECLEN (pat, 0); i++)
4181 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
4182 || GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER)
4183 new_insn_dead_notes (XVECEXP (pat, 0, i), insn, last, orig_insn);
4184 }
4185 }
4186
4187 /* If any insn, except the last, uses the register set by the last insn,
4188 then we need a new REG_DEAD note on that insn. In this case, there
4189 would not have been a REG_DEAD note for this register in the original
4190 insn because it was used and set within one insn.
4191
4192 There is no new REG_DEAD note needed if the last insn uses the register
4193 that it is setting. */
4194
4195 set = single_set (last);
4196 if (set)
4197 {
4198 rtx dest = SET_DEST (set);
4199
4200 while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
4201 || GET_CODE (dest) == STRICT_LOW_PART
4202 || GET_CODE (dest) == SIGN_EXTRACT)
4203 dest = XEXP (dest, 0);
4204
4205 if (GET_CODE (dest) == REG
4206 && ! reg_overlap_mentioned_p (dest, SET_SRC (set)))
4207 {
4208 for (insn = PREV_INSN (last); ; insn = PREV_INSN (insn))
4209 {
4210 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
4211 && reg_mentioned_p (dest, PATTERN (insn))
4212 && (set = single_set (insn)))
4213 {
4214 rtx insn_dest = SET_DEST (set);
4215
4216 while (GET_CODE (insn_dest) == ZERO_EXTRACT
4217 || GET_CODE (insn_dest) == SUBREG
4218 || GET_CODE (insn_dest) == STRICT_LOW_PART
4219 || GET_CODE (insn_dest) == SIGN_EXTRACT)
4220 insn_dest = XEXP (insn_dest, 0);
4221
4222 if (insn_dest != dest)
4223 {
4224 note = rtx_alloc (EXPR_LIST);
4225 PUT_REG_NOTE_KIND (note, REG_DEAD);
4226 XEXP (note, 0) = dest;
4227 XEXP (note, 1) = REG_NOTES (insn);
4228 REG_NOTES (insn) = note;
4229 /* The reg only dies in one insn, the last one
4230 that uses it. */
4231 break;
4232 }
4233 }
4234 if (insn == first)
4235 break;
4236 }
4237 }
4238 }
4239
4240 /* If the original dest is modifying a multiple register target, and the
4241 original instruction was split such that the original dest is now set
4242 by two or more SUBREG sets, then the split insns no longer kill the
4243 destination of the original insn.
4244
4245 In this case, if there exists an instruction in the same basic block,
4246 before the split insn, which uses the original dest, and this use is
4247 killed by the original insn, then we must remove the REG_DEAD note on
4248 this insn, because it is now superfluous.
4249
4250 This does not apply when a hard register gets split, because the code
4251 knows how to handle overlapping hard registers properly. */
4252 if (orig_dest && GET_CODE (orig_dest) == REG)
4253 {
4254 int found_orig_dest = 0;
4255 int found_split_dest = 0;
4256
4257 for (insn = first; ; insn = NEXT_INSN (insn))
4258 {
4259 set = single_set (insn);
4260 if (set)
4261 {
4262 if (GET_CODE (SET_DEST (set)) == REG
4263 && REGNO (SET_DEST (set)) == REGNO (orig_dest))
4264 {
4265 found_orig_dest = 1;
4266 break;
4267 }
4268 else if (GET_CODE (SET_DEST (set)) == SUBREG
4269 && SUBREG_REG (SET_DEST (set)) == orig_dest)
4270 {
4271 found_split_dest = 1;
4272 break;
4273 }
4274 }
4275
4276 if (insn == last)
4277 break;
4278 }
4279
4280 if (found_split_dest)
4281 {
4282 /* Search backwards from FIRST, looking for the first insn that uses
4283 the original dest. Stop if we pass a CODE_LABEL or a JUMP_INSN.
4284 If we find an insn, and it has a REG_DEAD note, then delete the
4285 note. */
4286
4287 for (insn = first; insn; insn = PREV_INSN (insn))
4288 {
4289 if (GET_CODE (insn) == CODE_LABEL
4290 || GET_CODE (insn) == JUMP_INSN)
4291 break;
4292 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
4293 && reg_mentioned_p (orig_dest, insn))
4294 {
4295 note = find_regno_note (insn, REG_DEAD, REGNO (orig_dest));
4296 if (note)
4297 remove_note (insn, note);
4298 }
4299 }
4300 }
4301 else if (! found_orig_dest)
4302 {
4303 /* This should never happen. */
4304 abort ();
4305 }
4306 }
4307
4308 /* Update reg_n_sets. This is necessary to prevent local alloc from
4309 converting REG_EQUAL notes to REG_EQUIV when splitting has modified
4310 a reg from set once to set multiple times. */
4311
4312 {
4313 rtx x = PATTERN (orig_insn);
4314 RTX_CODE code = GET_CODE (x);
4315
4316 if (code == SET || code == CLOBBER)
4317 update_n_sets (x, -1);
4318 else if (code == PARALLEL)
4319 {
4320 int i;
4321 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4322 {
4323 code = GET_CODE (XVECEXP (x, 0, i));
4324 if (code == SET || code == CLOBBER)
4325 update_n_sets (XVECEXP (x, 0, i), -1);
4326 }
4327 }
4328
4329 for (insn = first; ; insn = NEXT_INSN (insn))
4330 {
4331 x = PATTERN (insn);
4332 code = GET_CODE (x);
4333
4334 if (code == SET || code == CLOBBER)
4335 update_n_sets (x, 1);
4336 else if (code == PARALLEL)
4337 {
4338 int i;
4339 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4340 {
4341 code = GET_CODE (XVECEXP (x, 0, i));
4342 if (code == SET || code == CLOBBER)
4343 update_n_sets (XVECEXP (x, 0, i), 1);
4344 }
4345 }
4346
4347 if (insn == last)
4348 break;
4349 }
4350 }
4351 }
4352
4353 /* The one entry point in this file. DUMP_FILE is the dump file for
4354 this pass. */
4355
4356 void
4357 schedule_insns (dump_file)
4358 FILE *dump_file;
4359 {
4360 int max_uid = MAX_INSNS_PER_SPLIT * (get_max_uid () + 1);
4361 int i, b;
4362 rtx insn;
4363
4364 /* Taking care of this degenerate case makes the rest of
4365 this code simpler. */
4366 if (n_basic_blocks == 0)
4367 return;
4368
4369 /* Create an insn here so that we can hang dependencies off of it later. */
4370 sched_before_next_call
4371 = gen_rtx (INSN, VOIDmode, 0, NULL_RTX, NULL_RTX,
4372 NULL_RTX, 0, NULL_RTX, 0);
4373
4374 /* Initialize the unused_*_lists. We can't use the ones left over from
4375 the previous function, because gcc has freed that memory. We can use
4376 the ones left over from the first sched pass in the second pass however,
4377 so only clear them on the first sched pass. The first pass is before
4378 reload if flag_schedule_insns is set, otherwise it is afterwards. */
4379
4380 if (reload_completed == 0 || ! flag_schedule_insns)
4381 {
4382 unused_insn_list = 0;
4383 unused_expr_list = 0;
4384 }
4385
4386 /* We create no insns here, only reorder them, so we
4387 remember how far we can cut back the stack on exit. */
4388
4389 /* Allocate data for this pass. See comments, above,
4390 for what these vectors do. */
4391 insn_luid = (int *) alloca (max_uid * sizeof (int));
4392 insn_priority = (int *) alloca (max_uid * sizeof (int));
4393 insn_tick = (int *) alloca (max_uid * sizeof (int));
4394 insn_costs = (short *) alloca (max_uid * sizeof (short));
4395 insn_units = (short *) alloca (max_uid * sizeof (short));
4396 insn_blockage = (unsigned int *) alloca (max_uid * sizeof (unsigned int));
4397 insn_ref_count = (int *) alloca (max_uid * sizeof (int));
4398
4399 if (reload_completed == 0)
4400 {
4401 sched_reg_n_deaths = (short *) alloca (max_regno * sizeof (short));
4402 sched_reg_n_calls_crossed = (int *) alloca (max_regno * sizeof (int));
4403 sched_reg_live_length = (int *) alloca (max_regno * sizeof (int));
4404 bb_dead_regs = (regset) alloca (regset_bytes);
4405 bb_live_regs = (regset) alloca (regset_bytes);
4406 bzero (sched_reg_n_calls_crossed, max_regno * sizeof (int));
4407 bzero (sched_reg_live_length, max_regno * sizeof (int));
4408 bcopy (reg_n_deaths, sched_reg_n_deaths, max_regno * sizeof (short));
4409 init_alias_analysis ();
4410 }
4411 else
4412 {
4413 sched_reg_n_deaths = 0;
4414 sched_reg_n_calls_crossed = 0;
4415 sched_reg_live_length = 0;
4416 bb_dead_regs = 0;
4417 bb_live_regs = 0;
4418 if (! flag_schedule_insns)
4419 init_alias_analysis ();
4420 }
4421
4422 if (write_symbols != NO_DEBUG)
4423 {
4424 rtx line;
4425
4426 line_note = (rtx *) alloca (max_uid * sizeof (rtx));
4427 bzero (line_note, max_uid * sizeof (rtx));
4428 line_note_head = (rtx *) alloca (n_basic_blocks * sizeof (rtx));
4429 bzero (line_note_head, n_basic_blocks * sizeof (rtx));
4430
4431 /* Determine the line-number at the start of each basic block.
4432 This must be computed and saved now, because after a basic block's
4433 predecessor has been scheduled, it is impossible to accurately
4434 determine the correct line number for the first insn of the block. */
4435
4436 for (b = 0; b < n_basic_blocks; b++)
4437 for (line = basic_block_head[b]; line; line = PREV_INSN (line))
4438 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
4439 {
4440 line_note_head[b] = line;
4441 break;
4442 }
4443 }
4444
4445 bzero (insn_luid, max_uid * sizeof (int));
4446 bzero (insn_priority, max_uid * sizeof (int));
4447 bzero (insn_tick, max_uid * sizeof (int));
4448 bzero (insn_costs, max_uid * sizeof (short));
4449 bzero (insn_units, max_uid * sizeof (short));
4450 bzero (insn_blockage, max_uid * sizeof (unsigned int));
4451 bzero (insn_ref_count, max_uid * sizeof (int));
4452
4453 /* Schedule each basic block, block by block. */
4454
4455 if (NEXT_INSN (basic_block_end[n_basic_blocks-1]) == 0
4456 || (GET_CODE (basic_block_end[n_basic_blocks-1]) != NOTE
4457 && GET_CODE (basic_block_end[n_basic_blocks-1]) != CODE_LABEL))
4458 emit_note_after (NOTE_INSN_DELETED, basic_block_end[n_basic_blocks-1]);
4459
4460 for (b = 0; b < n_basic_blocks; b++)
4461 {
4462 rtx insn, next;
4463 rtx insns;
4464
4465 note_list = 0;
4466
4467 for (insn = basic_block_head[b]; ; insn = next)
4468 {
4469 rtx prev;
4470 rtx set;
4471
4472 /* Can't use `next_real_insn' because that
4473 might go across CODE_LABELS and short-out basic blocks. */
4474 next = NEXT_INSN (insn);
4475 if (GET_CODE (insn) != INSN)
4476 {
4477 if (insn == basic_block_end[b])
4478 break;
4479
4480 continue;
4481 }
4482
4483 /* Don't split no-op move insns. These should silently disappear
4484 later in final. Splitting such insns would break the code
4485 that handles REG_NO_CONFLICT blocks. */
4486 set = single_set (insn);
4487 if (set && rtx_equal_p (SET_SRC (set), SET_DEST (set)))
4488 {
4489 if (insn == basic_block_end[b])
4490 break;
4491
4492 /* Nops get in the way while scheduling, so delete them now if
4493 register allocation has already been done. It is too risky
4494 to try to do this before register allocation, and there are
4495 unlikely to be very many nops then anyways. */
4496 if (reload_completed)
4497 {
4498 PUT_CODE (insn, NOTE);
4499 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4500 NOTE_SOURCE_FILE (insn) = 0;
4501 }
4502
4503 continue;
4504 }
4505
4506 /* Split insns here to get max fine-grain parallelism. */
4507 prev = PREV_INSN (insn);
4508 if (reload_completed == 0)
4509 {
4510 rtx last, first = PREV_INSN (insn);
4511 rtx notes = REG_NOTES (insn);
4512
4513 last = try_split (PATTERN (insn), insn, 1);
4514 if (last != insn)
4515 {
4516 /* try_split returns the NOTE that INSN became. */
4517 first = NEXT_INSN (first);
4518 update_flow_info (notes, first, last, insn);
4519
4520 PUT_CODE (insn, NOTE);
4521 NOTE_SOURCE_FILE (insn) = 0;
4522 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4523 if (insn == basic_block_head[b])
4524 basic_block_head[b] = first;
4525 if (insn == basic_block_end[b])
4526 {
4527 basic_block_end[b] = last;
4528 break;
4529 }
4530 }
4531 }
4532
4533 if (insn == basic_block_end[b])
4534 break;
4535 }
4536
4537 schedule_block (b, dump_file);
4538
4539 #ifdef USE_C_ALLOCA
4540 alloca (0);
4541 #endif
4542 }
4543
4544 /* Reposition the prologue and epilogue notes in case we moved the
4545 prologue/epilogue insns. */
4546 if (reload_completed)
4547 reposition_prologue_and_epilogue_notes (get_insns ());
4548
4549 if (write_symbols != NO_DEBUG)
4550 {
4551 rtx line = 0;
4552 rtx insn = get_insns ();
4553 int active_insn = 0;
4554 int notes = 0;
4555
4556 /* Walk the insns deleting redundant line-number notes. Many of these
4557 are already present. The remainder tend to occur at basic
4558 block boundaries. */
4559 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
4560 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
4561 {
4562 /* If there are no active insns following, INSN is redundant. */
4563 if (active_insn == 0)
4564 {
4565 notes++;
4566 NOTE_SOURCE_FILE (insn) = 0;
4567 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4568 }
4569 /* If the line number is unchanged, LINE is redundant. */
4570 else if (line
4571 && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
4572 && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
4573 {
4574 notes++;
4575 NOTE_SOURCE_FILE (line) = 0;
4576 NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
4577 line = insn;
4578 }
4579 else
4580 line = insn;
4581 active_insn = 0;
4582 }
4583 else if (! ((GET_CODE (insn) == NOTE
4584 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
4585 || (GET_CODE (insn) == INSN
4586 && (GET_CODE (PATTERN (insn)) == USE
4587 || GET_CODE (PATTERN (insn)) == CLOBBER))))
4588 active_insn++;
4589
4590 if (dump_file && notes)
4591 fprintf (dump_file, ";; deleted %d line-number notes\n", notes);
4592 }
4593
4594 if (reload_completed == 0)
4595 {
4596 int regno;
4597 for (regno = 0; regno < max_regno; regno++)
4598 if (sched_reg_live_length[regno])
4599 {
4600 if (dump_file)
4601 {
4602 if (reg_live_length[regno] > sched_reg_live_length[regno])
4603 fprintf (dump_file,
4604 ";; register %d life shortened from %d to %d\n",
4605 regno, reg_live_length[regno],
4606 sched_reg_live_length[regno]);
4607 /* Negative values are special; don't overwrite the current
4608 reg_live_length value if it is negative. */
4609 else if (reg_live_length[regno] < sched_reg_live_length[regno]
4610 && reg_live_length[regno] >= 0)
4611 fprintf (dump_file,
4612 ";; register %d life extended from %d to %d\n",
4613 regno, reg_live_length[regno],
4614 sched_reg_live_length[regno]);
4615
4616 if (reg_n_calls_crossed[regno]
4617 && ! sched_reg_n_calls_crossed[regno])
4618 fprintf (dump_file,
4619 ";; register %d no longer crosses calls\n", regno);
4620 else if (! reg_n_calls_crossed[regno]
4621 && sched_reg_n_calls_crossed[regno])
4622 fprintf (dump_file,
4623 ";; register %d now crosses calls\n", regno);
4624 }
4625 /* Negative values are special; don't overwrite the current
4626 reg_live_length value if it is negative. */
4627 if (reg_live_length[regno] >= 0)
4628 reg_live_length[regno] = sched_reg_live_length[regno];
4629 reg_n_calls_crossed[regno] = sched_reg_n_calls_crossed[regno];
4630 }
4631 }
4632 }
4633 #endif /* INSN_SCHEDULING */
This page took 0.246895 seconds and 5 git commands to generate.