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