Fix for PR1654 - implement "movstrsi" pattern to copy simple blocks of memory.
[official-gcc.git] / gcc / sched.c
blob5fbf7609f4db3a720d638401e752d2bf3f673177
1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 93-97, 1998 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4 Enhanced by, and currently maintained by, Jim Wilson (wilson@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Instruction scheduling pass.
25 This pass implements list scheduling within basic blocks. It is
26 run after flow analysis, but before register allocation. The
27 scheduler works as follows:
29 We compute insn priorities based on data dependencies. Flow
30 analysis only creates a fraction of the data-dependencies we must
31 observe: namely, only those dependencies which the combiner can be
32 expected to use. For this pass, we must therefore create the
33 remaining dependencies we need to observe: register dependencies,
34 memory dependencies, dependencies to keep function calls in order,
35 and the dependence between a conditional branch and the setting of
36 condition codes are all dealt with here.
38 The scheduler first traverses the data flow graph, starting with
39 the last instruction, and proceeding to the first, assigning
40 values to insn_priority as it goes. This sorts the instructions
41 topologically by data dependence.
43 Once priorities have been established, we order the insns using
44 list scheduling. This works as follows: starting with a list of
45 all the ready insns, and sorted according to priority number, we
46 schedule the insn from the end of the list by placing its
47 predecessors in the list according to their priority order. We
48 consider this insn scheduled by setting the pointer to the "end" of
49 the list to point to the previous insn. When an insn has no
50 predecessors, we either queue it until sufficient time has elapsed
51 or add it to the ready list. As the instructions are scheduled or
52 when stalls are introduced, the queue advances and dumps insns into
53 the ready list. When all insns down to the lowest priority have
54 been scheduled, the critical path of the basic block has been made
55 as short as possible. The remaining insns are then scheduled in
56 remaining slots.
58 Function unit conflicts are resolved during reverse list scheduling
59 by tracking the time when each insn is committed to the schedule
60 and from that, the time the function units it uses must be free.
61 As insns on the ready list are considered for scheduling, those
62 that would result in a blockage of the already committed insns are
63 queued until no blockage will result. Among the remaining insns on
64 the ready list to be considered, the first one with the largest
65 potential for causing a subsequent blockage is chosen.
67 The following list shows the order in which we want to break ties
68 among insns in the ready list:
70 1. choose insn with lowest conflict cost, ties broken by
71 2. choose insn with the longest path to end of bb, ties broken by
72 3. choose insn that kills the most registers, ties broken by
73 4. choose insn that conflicts with the most ready insns, or finally
74 5. choose insn with lowest UID.
76 Memory references complicate matters. Only if we can be certain
77 that memory references are not part of the data dependency graph
78 (via true, anti, or output dependence), can we move operations past
79 memory references. To first approximation, reads can be done
80 independently, while writes introduce dependencies. Better
81 approximations will yield fewer dependencies.
83 Dependencies set up by memory references are treated in exactly the
84 same way as other dependencies, by using LOG_LINKS.
86 Having optimized the critical path, we may have also unduly
87 extended the lifetimes of some registers. If an operation requires
88 that constants be loaded into registers, it is certainly desirable
89 to load those constants as early as necessary, but no earlier.
90 I.e., it will not do to load up a bunch of registers at the
91 beginning of a basic block only to use them at the end, if they
92 could be loaded later, since this may result in excessive register
93 utilization.
95 Note that since branches are never in basic blocks, but only end
96 basic blocks, this pass will not do any branch scheduling. But
97 that is ok, since we can use GNU's delayed branch scheduling
98 pass to take care of this case.
100 Also note that no further optimizations based on algebraic identities
101 are performed, so this pass would be a good one to perform instruction
102 splitting, such as breaking up a multiply instruction into shifts
103 and adds where that is profitable.
105 Given the memory aliasing analysis that this pass should perform,
106 it should be possible to remove redundant stores to memory, and to
107 load values from registers instead of hitting memory.
109 This pass must update information that subsequent passes expect to be
110 correct. Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
111 reg_n_calls_crossed, and reg_live_length. Also, basic_block_head,
112 basic_block_end.
114 The information in the line number notes is carefully retained by
115 this pass. Notes that refer to the starting and ending of
116 exception regions are also carefully retained by this pass. All
117 other NOTE insns are grouped in their same relative order at the
118 beginning of basic blocks that have been scheduled. */
120 #include "config.h"
121 #include "system.h"
122 #include "rtl.h"
123 #include "basic-block.h"
124 #include "regs.h"
125 #include "hard-reg-set.h"
126 #include "flags.h"
127 #include "insn-config.h"
128 #include "insn-attr.h"
130 #ifndef INSN_SCHEDULING
131 void
132 schedule_insns (dump_file)
133 FILE *dump_file ATTRIBUTE_UNUSED;
136 #else /* INSN_SCHEDULING -- rest of file */
138 extern char *reg_known_equiv_p;
139 extern rtx *reg_known_value;
141 /* Arrays set up by scheduling for the same respective purposes as
142 similar-named arrays set up by flow analysis. We work with these
143 arrays during the scheduling pass so we can compare values against
144 unscheduled code.
146 Values of these arrays are copied at the end of this pass into the
147 arrays set up by flow analysis. */
148 static int *sched_reg_n_calls_crossed;
149 static int *sched_reg_live_length;
151 /* Element N is the next insn that sets (hard or pseudo) register
152 N within the current basic block; or zero, if there is no
153 such insn. Needed for new registers which may be introduced
154 by splitting insns. */
155 static rtx *reg_last_uses;
156 static rtx *reg_last_sets;
157 static regset reg_pending_sets;
158 static int reg_pending_sets_all;
160 /* Vector indexed by INSN_UID giving the original ordering of the insns. */
161 static int *insn_luid;
162 #define INSN_LUID(INSN) (insn_luid[INSN_UID (INSN)])
164 /* Vector indexed by INSN_UID giving each instruction a priority. */
165 static int *insn_priority;
166 #define INSN_PRIORITY(INSN) (insn_priority[INSN_UID (INSN)])
168 static short *insn_costs;
169 #define INSN_COST(INSN) insn_costs[INSN_UID (INSN)]
171 /* Vector indexed by INSN_UID giving an encoding of the function units
172 used. */
173 static short *insn_units;
174 #define INSN_UNIT(INSN) insn_units[INSN_UID (INSN)]
176 /* Vector indexed by INSN_UID giving an encoding of the blockage range
177 function. The unit and the range are encoded. */
178 static unsigned int *insn_blockage;
179 #define INSN_BLOCKAGE(INSN) insn_blockage[INSN_UID (INSN)]
180 #define UNIT_BITS 5
181 #define BLOCKAGE_MASK ((1 << BLOCKAGE_BITS) - 1)
182 #define ENCODE_BLOCKAGE(U,R) \
183 ((((U) << UNIT_BITS) << BLOCKAGE_BITS \
184 | MIN_BLOCKAGE_COST (R)) << BLOCKAGE_BITS \
185 | MAX_BLOCKAGE_COST (R))
186 #define UNIT_BLOCKED(B) ((B) >> (2 * BLOCKAGE_BITS))
187 #define BLOCKAGE_RANGE(B) \
188 (((((B) >> BLOCKAGE_BITS) & BLOCKAGE_MASK) << (HOST_BITS_PER_INT / 2)) \
189 | ((B) & BLOCKAGE_MASK))
191 /* Encodings of the `<name>_unit_blockage_range' function. */
192 #define MIN_BLOCKAGE_COST(R) ((R) >> (HOST_BITS_PER_INT / 2))
193 #define MAX_BLOCKAGE_COST(R) ((R) & ((1 << (HOST_BITS_PER_INT / 2)) - 1))
195 #define DONE_PRIORITY -1
196 #define MAX_PRIORITY 0x7fffffff
197 #define TAIL_PRIORITY 0x7ffffffe
198 #define LAUNCH_PRIORITY 0x7f000001
199 #define DONE_PRIORITY_P(INSN) (INSN_PRIORITY (INSN) < 0)
200 #define LOW_PRIORITY_P(INSN) ((INSN_PRIORITY (INSN) & 0x7f000000) == 0)
202 /* Vector indexed by INSN_UID giving number of insns referring to this insn. */
203 static int *insn_ref_count;
204 #define INSN_REF_COUNT(INSN) (insn_ref_count[INSN_UID (INSN)])
206 /* Vector indexed by INSN_UID giving line-number note in effect for each
207 insn. For line-number notes, this indicates whether the note may be
208 reused. */
209 static rtx *line_note;
210 #define LINE_NOTE(INSN) (line_note[INSN_UID (INSN)])
212 /* Vector indexed by basic block number giving the starting line-number
213 for each basic block. */
214 static rtx *line_note_head;
216 /* List of important notes we must keep around. This is a pointer to the
217 last element in the list. */
218 static rtx note_list;
220 /* Regsets telling whether a given register is live or dead before the last
221 scheduled insn. Must scan the instructions once before scheduling to
222 determine what registers are live or dead at the end of the block. */
223 static regset bb_dead_regs;
224 static regset bb_live_regs;
226 /* Regset telling whether a given register is live after the insn currently
227 being scheduled. Before processing an insn, this is equal to bb_live_regs
228 above. This is used so that we can find registers that are newly born/dead
229 after processing an insn. */
230 static regset old_live_regs;
232 /* The chain of REG_DEAD notes. REG_DEAD notes are removed from all insns
233 during the initial scan and reused later. If there are not exactly as
234 many REG_DEAD notes in the post scheduled code as there were in the
235 prescheduled code then we trigger an abort because this indicates a bug. */
236 static rtx dead_notes;
238 /* Queues, etc. */
240 /* An instruction is ready to be scheduled when all insns following it
241 have already been scheduled. It is important to ensure that all
242 insns which use its result will not be executed until its result
243 has been computed. An insn is maintained in one of four structures:
245 (P) the "Pending" set of insns which cannot be scheduled until
246 their dependencies have been satisfied.
247 (Q) the "Queued" set of insns that can be scheduled when sufficient
248 time has passed.
249 (R) the "Ready" list of unscheduled, uncommitted insns.
250 (S) the "Scheduled" list of insns.
252 Initially, all insns are either "Pending" or "Ready" depending on
253 whether their dependencies are satisfied.
255 Insns move from the "Ready" list to the "Scheduled" list as they
256 are committed to the schedule. As this occurs, the insns in the
257 "Pending" list have their dependencies satisfied and move to either
258 the "Ready" list or the "Queued" set depending on whether
259 sufficient time has passed to make them ready. As time passes,
260 insns move from the "Queued" set to the "Ready" list. Insns may
261 move from the "Ready" list to the "Queued" set if they are blocked
262 due to a function unit conflict.
264 The "Pending" list (P) are the insns in the LOG_LINKS of the unscheduled
265 insns, i.e., those that are ready, queued, and pending.
266 The "Queued" set (Q) is implemented by the variable `insn_queue'.
267 The "Ready" list (R) is implemented by the variables `ready' and
268 `n_ready'.
269 The "Scheduled" list (S) is the new insn chain built by this pass.
271 The transition (R->S) is implemented in the scheduling loop in
272 `schedule_block' when the best insn to schedule is chosen.
273 The transition (R->Q) is implemented in `schedule_select' when an
274 insn is found to have a function unit conflict with the already
275 committed insns.
276 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
277 insns move from the ready list to the scheduled list.
278 The transition (Q->R) is implemented at the top of the scheduling
279 loop in `schedule_block' as time passes or stalls are introduced. */
281 /* Implement a circular buffer to delay instructions until sufficient
282 time has passed. INSN_QUEUE_SIZE is a power of two larger than
283 MAX_BLOCKAGE and MAX_READY_COST computed by genattr.c. This is the
284 longest time an isnsn may be queued. */
285 static rtx insn_queue[INSN_QUEUE_SIZE];
286 static int q_ptr = 0;
287 static int q_size = 0;
288 #define NEXT_Q(X) (((X)+1) & (INSN_QUEUE_SIZE-1))
289 #define NEXT_Q_AFTER(X,C) (((X)+C) & (INSN_QUEUE_SIZE-1))
291 /* Vector indexed by INSN_UID giving the minimum clock tick at which
292 the insn becomes ready. This is used to note timing constraints for
293 insns in the pending list. */
294 static int *insn_tick;
295 #define INSN_TICK(INSN) (insn_tick[INSN_UID (INSN)])
297 /* Data structure for keeping track of register information
298 during that register's life. */
300 struct sometimes
302 int regno;
303 int live_length;
304 int calls_crossed;
307 /* Forward declarations. */
308 static void add_dependence PROTO((rtx, rtx, enum reg_note));
309 static void remove_dependence PROTO((rtx, rtx));
310 static rtx find_insn_list PROTO((rtx, rtx));
311 static int insn_unit PROTO((rtx));
312 static unsigned int blockage_range PROTO((int, rtx));
313 static void clear_units PROTO((void));
314 static void prepare_unit PROTO((int));
315 static int actual_hazard_this_instance PROTO((int, int, rtx, int, int));
316 static void schedule_unit PROTO((int, rtx, int));
317 static int actual_hazard PROTO((int, rtx, int, int));
318 static int potential_hazard PROTO((int, rtx, int));
319 static int insn_cost PROTO((rtx, rtx, rtx));
320 static int priority PROTO((rtx));
321 static void free_pending_lists PROTO((void));
322 static void add_insn_mem_dependence PROTO((rtx *, rtx *, rtx, rtx));
323 static void flush_pending_lists PROTO((rtx, int));
324 static void sched_analyze_1 PROTO((rtx, rtx));
325 static void sched_analyze_2 PROTO((rtx, rtx));
326 static void sched_analyze_insn PROTO((rtx, rtx, rtx));
327 static int sched_analyze PROTO((rtx, rtx));
328 static void sched_note_set PROTO((int, rtx, int));
329 static int rank_for_schedule PROTO((const GENERIC_PTR, const GENERIC_PTR));
330 static void swap_sort PROTO((rtx *, int));
331 static void queue_insn PROTO((rtx, int));
332 static int birthing_insn_p PROTO((rtx));
333 static void adjust_priority PROTO((rtx));
334 static int schedule_insn PROTO((rtx, rtx *, int, int));
335 static int schedule_select PROTO((rtx *, int, int, FILE *));
336 static void create_reg_dead_note PROTO((rtx, rtx));
337 static void attach_deaths PROTO((rtx, rtx, int));
338 static void attach_deaths_insn PROTO((rtx));
339 static rtx unlink_notes PROTO((rtx, rtx));
340 static int new_sometimes_live PROTO((struct sometimes *, int, int));
341 static void finish_sometimes_live PROTO((struct sometimes *, int));
342 static rtx reemit_notes PROTO((rtx, rtx));
343 static void schedule_block PROTO((int, FILE *));
344 static rtx regno_use_in PROTO((int, rtx));
345 static void split_hard_reg_notes PROTO((rtx, rtx, rtx, rtx));
346 static void new_insn_dead_notes PROTO((rtx, rtx, rtx, rtx));
347 static void update_n_sets PROTO((rtx, int));
348 static void update_flow_info PROTO((rtx, rtx, rtx, rtx));
350 /* Main entry point of this file. */
351 void schedule_insns PROTO((FILE *));
353 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
355 /* Helper functions for instruction scheduling. */
357 /* Add ELEM wrapped in an INSN_LIST with reg note kind DEP_TYPE to the
358 LOG_LINKS of INSN, if not already there. DEP_TYPE indicates the type
359 of dependence that this link represents. */
361 static void
362 add_dependence (insn, elem, dep_type)
363 rtx insn;
364 rtx elem;
365 enum reg_note dep_type;
367 rtx link, next;
369 /* Don't depend an insn on itself. */
370 if (insn == elem)
371 return;
373 /* If elem is part of a sequence that must be scheduled together, then
374 make the dependence point to the last insn of the sequence.
375 When HAVE_cc0, it is possible for NOTEs to exist between users and
376 setters of the condition codes, so we must skip past notes here.
377 Otherwise, NOTEs are impossible here. */
379 next = NEXT_INSN (elem);
381 #ifdef HAVE_cc0
382 while (next && GET_CODE (next) == NOTE)
383 next = NEXT_INSN (next);
384 #endif
386 if (next && SCHED_GROUP_P (next)
387 && GET_CODE (next) != CODE_LABEL)
389 /* Notes will never intervene here though, so don't bother checking
390 for them. */
391 /* We must reject CODE_LABELs, so that we don't get confused by one
392 that has LABEL_PRESERVE_P set, which is represented by the same
393 bit in the rtl as SCHED_GROUP_P. A CODE_LABEL can never be
394 SCHED_GROUP_P. */
395 while (NEXT_INSN (next) && SCHED_GROUP_P (NEXT_INSN (next))
396 && GET_CODE (NEXT_INSN (next)) != CODE_LABEL)
397 next = NEXT_INSN (next);
399 /* Again, don't depend an insn on itself. */
400 if (insn == next)
401 return;
403 /* Make the dependence to NEXT, the last insn of the group, instead
404 of the original ELEM. */
405 elem = next;
408 /* Check that we don't already have this dependence. */
409 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
410 if (XEXP (link, 0) == elem)
412 /* If this is a more restrictive type of dependence than the existing
413 one, then change the existing dependence to this type. */
414 if ((int) dep_type < (int) REG_NOTE_KIND (link))
415 PUT_REG_NOTE_KIND (link, dep_type);
416 return;
418 /* Might want to check one level of transitivity to save conses. */
420 link = rtx_alloc (INSN_LIST);
421 /* Insn dependency, not data dependency. */
422 PUT_REG_NOTE_KIND (link, dep_type);
423 XEXP (link, 0) = elem;
424 XEXP (link, 1) = LOG_LINKS (insn);
425 LOG_LINKS (insn) = link;
428 /* Remove ELEM wrapped in an INSN_LIST from the LOG_LINKS
429 of INSN. Abort if not found. */
431 static void
432 remove_dependence (insn, elem)
433 rtx insn;
434 rtx elem;
436 rtx prev, link;
437 int found = 0;
439 for (prev = 0, link = LOG_LINKS (insn); link; link = XEXP (link, 1))
441 if (XEXP (link, 0) == elem)
443 RTX_INTEGRATED_P (link) = 1;
444 if (prev)
445 XEXP (prev, 1) = XEXP (link, 1);
446 else
447 LOG_LINKS (insn) = XEXP (link, 1);
448 found = 1;
450 else
451 prev = link;
454 if (! found)
455 abort ();
456 return;
459 #ifndef __GNUC__
460 #define __inline
461 #endif
463 /* Computation of memory dependencies. */
465 /* The *_insns and *_mems are paired lists. Each pending memory operation
466 will have a pointer to the MEM rtx on one list and a pointer to the
467 containing insn on the other list in the same place in the list. */
469 /* We can't use add_dependence like the old code did, because a single insn
470 may have multiple memory accesses, and hence needs to be on the list
471 once for each memory access. Add_dependence won't let you add an insn
472 to a list more than once. */
474 /* An INSN_LIST containing all insns with pending read operations. */
475 static rtx pending_read_insns;
477 /* An EXPR_LIST containing all MEM rtx's which are pending reads. */
478 static rtx pending_read_mems;
480 /* An INSN_LIST containing all insns with pending write operations. */
481 static rtx pending_write_insns;
483 /* An EXPR_LIST containing all MEM rtx's which are pending writes. */
484 static rtx pending_write_mems;
486 /* Indicates the combined length of the two pending lists. We must prevent
487 these lists from ever growing too large since the number of dependencies
488 produced is at least O(N*N), and execution time is at least O(4*N*N), as
489 a function of the length of these pending lists. */
491 static int pending_lists_length;
493 /* An INSN_LIST containing all INSN_LISTs allocated but currently unused. */
495 static rtx unused_insn_list;
497 /* An EXPR_LIST containing all EXPR_LISTs allocated but currently unused. */
499 static rtx unused_expr_list;
501 /* The last insn upon which all memory references must depend.
502 This is an insn which flushed the pending lists, creating a dependency
503 between it and all previously pending memory references. This creates
504 a barrier (or a checkpoint) which no memory reference is allowed to cross.
506 This includes all non constant CALL_INSNs. When we do interprocedural
507 alias analysis, this restriction can be relaxed.
508 This may also be an INSN that writes memory if the pending lists grow
509 too large. */
511 static rtx last_pending_memory_flush;
513 /* The last function call we have seen. All hard regs, and, of course,
514 the last function call, must depend on this. */
516 static rtx last_function_call;
518 /* The LOG_LINKS field of this is a list of insns which use a pseudo register
519 that does not already cross a call. We create dependencies between each
520 of those insn and the next call insn, to ensure that they won't cross a call
521 after scheduling is done. */
523 static rtx sched_before_next_call;
525 /* Pointer to the last instruction scheduled. Used by rank_for_schedule,
526 so that insns independent of the last scheduled insn will be preferred
527 over dependent instructions. */
529 static rtx last_scheduled_insn;
531 /* Process an insn's memory dependencies. There are four kinds of
532 dependencies:
534 (0) read dependence: read follows read
535 (1) true dependence: read follows write
536 (2) anti dependence: write follows read
537 (3) output dependence: write follows write
539 We are careful to build only dependencies which actually exist, and
540 use transitivity to avoid building too many links. */
542 /* Return the INSN_LIST containing INSN in LIST, or NULL
543 if LIST does not contain INSN. */
545 __inline static rtx
546 find_insn_list (insn, list)
547 rtx insn;
548 rtx list;
550 while (list)
552 if (XEXP (list, 0) == insn)
553 return list;
554 list = XEXP (list, 1);
556 return 0;
559 /* Compute the function units used by INSN. This caches the value
560 returned by function_units_used. A function unit is encoded as the
561 unit number if the value is non-negative and the compliment of a
562 mask if the value is negative. A function unit index is the
563 non-negative encoding. */
565 __inline static int
566 insn_unit (insn)
567 rtx insn;
569 register int unit = INSN_UNIT (insn);
571 if (unit == 0)
573 recog_memoized (insn);
575 /* A USE insn, or something else we don't need to understand.
576 We can't pass these directly to function_units_used because it will
577 trigger a fatal error for unrecognizable insns. */
578 if (INSN_CODE (insn) < 0)
579 unit = -1;
580 else
582 unit = function_units_used (insn);
583 /* Increment non-negative values so we can cache zero. */
584 if (unit >= 0) unit++;
586 /* We only cache 16 bits of the result, so if the value is out of
587 range, don't cache it. */
588 if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
589 || unit >= 0
590 || (~unit & ((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
591 INSN_UNIT (insn) = unit;
593 return (unit > 0 ? unit - 1 : unit);
596 /* Compute the blockage range for executing INSN on UNIT. This caches
597 the value returned by the blockage_range_function for the unit.
598 These values are encoded in an int where the upper half gives the
599 minimum value and the lower half gives the maximum value. */
601 __inline static unsigned int
602 blockage_range (unit, insn)
603 int unit;
604 rtx insn;
606 unsigned int blockage = INSN_BLOCKAGE (insn);
607 unsigned int range;
609 if (UNIT_BLOCKED (blockage) != unit + 1)
611 range = function_units[unit].blockage_range_function (insn);
612 /* We only cache the blockage range for one unit and then only if
613 the values fit. */
614 if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
615 INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
617 else
618 range = BLOCKAGE_RANGE (blockage);
620 return range;
623 /* A vector indexed by function unit instance giving the last insn to use
624 the unit. The value of the function unit instance index for unit U
625 instance I is (U + I * FUNCTION_UNITS_SIZE). */
626 static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
628 /* A vector indexed by function unit instance giving the minimum time when
629 the unit will unblock based on the maximum blockage cost. */
630 static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
632 /* A vector indexed by function unit number giving the number of insns
633 that remain to use the unit. */
634 static int unit_n_insns[FUNCTION_UNITS_SIZE];
636 /* Reset the function unit state to the null state. */
638 static void
639 clear_units ()
641 bzero ((char *) unit_last_insn, sizeof (unit_last_insn));
642 bzero ((char *) unit_tick, sizeof (unit_tick));
643 bzero ((char *) unit_n_insns, sizeof (unit_n_insns));
646 /* Record an insn as one that will use the units encoded by UNIT. */
648 __inline static void
649 prepare_unit (unit)
650 int unit;
652 int i;
654 if (unit >= 0)
655 unit_n_insns[unit]++;
656 else
657 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
658 if ((unit & 1) != 0)
659 prepare_unit (i);
662 /* Return the actual hazard cost of executing INSN on the unit UNIT,
663 instance INSTANCE at time CLOCK if the previous actual hazard cost
664 was COST. */
666 __inline static int
667 actual_hazard_this_instance (unit, instance, insn, clock, cost)
668 int unit, instance, clock, cost;
669 rtx insn;
671 int tick = unit_tick[instance];
673 if (tick - clock > cost)
675 /* The scheduler is operating in reverse, so INSN is the executing
676 insn and the unit's last insn is the candidate insn. We want a
677 more exact measure of the blockage if we execute INSN at CLOCK
678 given when we committed the execution of the unit's last insn.
680 The blockage value is given by either the unit's max blockage
681 constant, blockage range function, or blockage function. Use
682 the most exact form for the given unit. */
684 if (function_units[unit].blockage_range_function)
686 if (function_units[unit].blockage_function)
687 tick += (function_units[unit].blockage_function
688 (insn, unit_last_insn[instance])
689 - function_units[unit].max_blockage);
690 else
691 tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
692 - function_units[unit].max_blockage);
694 if (tick - clock > cost)
695 cost = tick - clock;
697 return cost;
700 /* Record INSN as having begun execution on the units encoded by UNIT at
701 time CLOCK. */
703 __inline static void
704 schedule_unit (unit, insn, clock)
705 int unit, clock;
706 rtx insn;
708 int i;
710 if (unit >= 0)
712 int instance = unit;
713 #if MAX_MULTIPLICITY > 1
714 /* Find the first free instance of the function unit and use that
715 one. We assume that one is free. */
716 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
718 if (! actual_hazard_this_instance (unit, instance, insn, clock, 0))
719 break;
720 instance += FUNCTION_UNITS_SIZE;
722 #endif
723 unit_last_insn[instance] = insn;
724 unit_tick[instance] = (clock + function_units[unit].max_blockage);
726 else
727 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
728 if ((unit & 1) != 0)
729 schedule_unit (i, insn, clock);
732 /* Return the actual hazard cost of executing INSN on the units encoded by
733 UNIT at time CLOCK if the previous actual hazard cost was COST. */
735 __inline static int
736 actual_hazard (unit, insn, clock, cost)
737 int unit, clock, cost;
738 rtx insn;
740 int i;
742 if (unit >= 0)
744 /* Find the instance of the function unit with the minimum hazard. */
745 int instance = unit;
746 int best_cost = actual_hazard_this_instance (unit, instance, insn,
747 clock, cost);
748 #if MAX_MULTIPLICITY > 1
749 int this_cost;
751 if (best_cost > cost)
753 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
755 instance += FUNCTION_UNITS_SIZE;
756 this_cost = actual_hazard_this_instance (unit, instance, insn,
757 clock, cost);
758 if (this_cost < best_cost)
760 best_cost = this_cost;
761 if (this_cost <= cost)
762 break;
766 #endif
767 cost = MAX (cost, best_cost);
769 else
770 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
771 if ((unit & 1) != 0)
772 cost = actual_hazard (i, insn, clock, cost);
774 return cost;
777 /* Return the potential hazard cost of executing an instruction on the
778 units encoded by UNIT if the previous potential hazard cost was COST.
779 An insn with a large blockage time is chosen in preference to one
780 with a smaller time; an insn that uses a unit that is more likely
781 to be used is chosen in preference to one with a unit that is less
782 used. We are trying to minimize a subsequent actual hazard. */
784 __inline static int
785 potential_hazard (unit, insn, cost)
786 int unit, cost;
787 rtx insn;
789 int i, ncost;
790 unsigned int minb, maxb;
792 if (unit >= 0)
794 minb = maxb = function_units[unit].max_blockage;
795 if (maxb > 1)
797 if (function_units[unit].blockage_range_function)
799 maxb = minb = blockage_range (unit, insn);
800 maxb = MAX_BLOCKAGE_COST (maxb);
801 minb = MIN_BLOCKAGE_COST (minb);
804 if (maxb > 1)
806 /* Make the number of instructions left dominate. Make the
807 minimum delay dominate the maximum delay. If all these
808 are the same, use the unit number to add an arbitrary
809 ordering. Other terms can be added. */
810 ncost = minb * 0x40 + maxb;
811 ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
812 if (ncost > cost)
813 cost = ncost;
817 else
818 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
819 if ((unit & 1) != 0)
820 cost = potential_hazard (i, insn, cost);
822 return cost;
825 /* Compute cost of executing INSN given the dependence LINK on the insn USED.
826 This is the number of virtual cycles taken between instruction issue and
827 instruction results. */
829 __inline static int
830 insn_cost (insn, link, used)
831 rtx insn, link, used;
833 register int cost = INSN_COST (insn);
835 if (cost == 0)
837 recog_memoized (insn);
839 /* A USE insn, or something else we don't need to understand.
840 We can't pass these directly to result_ready_cost because it will
841 trigger a fatal error for unrecognizable insns. */
842 if (INSN_CODE (insn) < 0)
844 INSN_COST (insn) = 1;
845 return 1;
847 else
849 cost = result_ready_cost (insn);
851 if (cost < 1)
852 cost = 1;
854 INSN_COST (insn) = cost;
858 /* A USE insn should never require the value used to be computed. This
859 allows the computation of a function's result and parameter values to
860 overlap the return and call. */
861 recog_memoized (used);
862 if (INSN_CODE (used) < 0)
863 LINK_COST_FREE (link) = 1;
865 /* If some dependencies vary the cost, compute the adjustment. Most
866 commonly, the adjustment is complete: either the cost is ignored
867 (in the case of an output- or anti-dependence), or the cost is
868 unchanged. These values are cached in the link as LINK_COST_FREE
869 and LINK_COST_ZERO. */
871 if (LINK_COST_FREE (link))
872 cost = 1;
873 #ifdef ADJUST_COST
874 else if (! LINK_COST_ZERO (link))
876 int ncost = cost;
878 ADJUST_COST (used, link, insn, ncost);
879 if (ncost <= 1)
880 LINK_COST_FREE (link) = ncost = 1;
881 if (cost == ncost)
882 LINK_COST_ZERO (link) = 1;
883 cost = ncost;
885 #endif
886 return cost;
889 /* Compute the priority number for INSN. */
891 static int
892 priority (insn)
893 rtx insn;
895 if (insn && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
897 int prev_priority;
898 int max_priority;
899 int this_priority = INSN_PRIORITY (insn);
900 rtx prev;
902 if (this_priority > 0)
903 return this_priority;
905 max_priority = 1;
907 /* Nonzero if these insns must be scheduled together. */
908 if (SCHED_GROUP_P (insn))
910 prev = insn;
911 while (SCHED_GROUP_P (prev))
913 prev = PREV_INSN (prev);
914 INSN_REF_COUNT (prev) += 1;
918 for (prev = LOG_LINKS (insn); prev; prev = XEXP (prev, 1))
920 rtx x = XEXP (prev, 0);
922 /* If this was a duplicate of a dependence we already deleted,
923 ignore it. */
924 if (RTX_INTEGRATED_P (prev))
925 continue;
927 /* A dependence pointing to a note or deleted insn is always
928 obsolete, because sched_analyze_insn will have created any
929 necessary new dependences which replace it. Notes and deleted
930 insns can be created when instructions are deleted by insn
931 splitting, or by register allocation. */
932 if (GET_CODE (x) == NOTE || INSN_DELETED_P (x))
934 remove_dependence (insn, x);
935 continue;
938 /* Clear the link cost adjustment bits. */
939 LINK_COST_FREE (prev) = 0;
940 #ifdef ADJUST_COST
941 LINK_COST_ZERO (prev) = 0;
942 #endif
944 /* This priority calculation was chosen because it results in the
945 least instruction movement, and does not hurt the performance
946 of the resulting code compared to the old algorithm.
947 This makes the sched algorithm more stable, which results
948 in better code, because there is less register pressure,
949 cross jumping is more likely to work, and debugging is easier.
951 When all instructions have a latency of 1, there is no need to
952 move any instructions. Subtracting one here ensures that in such
953 cases all instructions will end up with a priority of one, and
954 hence no scheduling will be done.
956 The original code did not subtract the one, and added the
957 insn_cost of the current instruction to its priority (e.g.
958 move the insn_cost call down to the end). */
960 prev_priority = priority (x) + insn_cost (x, prev, insn) - 1;
962 if (prev_priority > max_priority)
963 max_priority = prev_priority;
964 INSN_REF_COUNT (x) += 1;
967 prepare_unit (insn_unit (insn));
968 INSN_PRIORITY (insn) = max_priority;
969 return INSN_PRIORITY (insn);
971 return 0;
974 /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
975 them to the unused_*_list variables, so that they can be reused. */
977 static void
978 free_pending_lists ()
980 register rtx link, prev_link;
982 if (pending_read_insns)
984 prev_link = pending_read_insns;
985 link = XEXP (prev_link, 1);
987 while (link)
989 prev_link = link;
990 link = XEXP (link, 1);
993 XEXP (prev_link, 1) = unused_insn_list;
994 unused_insn_list = pending_read_insns;
995 pending_read_insns = 0;
998 if (pending_write_insns)
1000 prev_link = pending_write_insns;
1001 link = XEXP (prev_link, 1);
1003 while (link)
1005 prev_link = link;
1006 link = XEXP (link, 1);
1009 XEXP (prev_link, 1) = unused_insn_list;
1010 unused_insn_list = pending_write_insns;
1011 pending_write_insns = 0;
1014 if (pending_read_mems)
1016 prev_link = pending_read_mems;
1017 link = XEXP (prev_link, 1);
1019 while (link)
1021 prev_link = link;
1022 link = XEXP (link, 1);
1025 XEXP (prev_link, 1) = unused_expr_list;
1026 unused_expr_list = pending_read_mems;
1027 pending_read_mems = 0;
1030 if (pending_write_mems)
1032 prev_link = pending_write_mems;
1033 link = XEXP (prev_link, 1);
1035 while (link)
1037 prev_link = link;
1038 link = XEXP (link, 1);
1041 XEXP (prev_link, 1) = unused_expr_list;
1042 unused_expr_list = pending_write_mems;
1043 pending_write_mems = 0;
1047 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
1048 The MEM is a memory reference contained within INSN, which we are saving
1049 so that we can do memory aliasing on it. */
1051 static void
1052 add_insn_mem_dependence (insn_list, mem_list, insn, mem)
1053 rtx *insn_list, *mem_list, insn, mem;
1055 register rtx link;
1057 if (unused_insn_list)
1059 link = unused_insn_list;
1060 unused_insn_list = XEXP (link, 1);
1062 else
1063 link = rtx_alloc (INSN_LIST);
1064 XEXP (link, 0) = insn;
1065 XEXP (link, 1) = *insn_list;
1066 *insn_list = link;
1068 if (unused_expr_list)
1070 link = unused_expr_list;
1071 unused_expr_list = XEXP (link, 1);
1073 else
1074 link = rtx_alloc (EXPR_LIST);
1075 XEXP (link, 0) = mem;
1076 XEXP (link, 1) = *mem_list;
1077 *mem_list = link;
1079 pending_lists_length++;
1082 /* Make a dependency between every memory reference on the pending lists
1083 and INSN, thus flushing the pending lists. If ONLY_WRITE, don't flush
1084 the read list. */
1086 static void
1087 flush_pending_lists (insn, only_write)
1088 rtx insn;
1089 int only_write;
1091 rtx link;
1093 while (pending_read_insns && ! only_write)
1095 add_dependence (insn, XEXP (pending_read_insns, 0), REG_DEP_ANTI);
1097 link = pending_read_insns;
1098 pending_read_insns = XEXP (pending_read_insns, 1);
1099 XEXP (link, 1) = unused_insn_list;
1100 unused_insn_list = link;
1102 link = pending_read_mems;
1103 pending_read_mems = XEXP (pending_read_mems, 1);
1104 XEXP (link, 1) = unused_expr_list;
1105 unused_expr_list = link;
1107 while (pending_write_insns)
1109 add_dependence (insn, XEXP (pending_write_insns, 0), REG_DEP_ANTI);
1111 link = pending_write_insns;
1112 pending_write_insns = XEXP (pending_write_insns, 1);
1113 XEXP (link, 1) = unused_insn_list;
1114 unused_insn_list = link;
1116 link = pending_write_mems;
1117 pending_write_mems = XEXP (pending_write_mems, 1);
1118 XEXP (link, 1) = unused_expr_list;
1119 unused_expr_list = link;
1121 pending_lists_length = 0;
1123 if (last_pending_memory_flush)
1124 add_dependence (insn, last_pending_memory_flush, REG_DEP_ANTI);
1126 last_pending_memory_flush = insn;
1129 /* Analyze a single SET or CLOBBER rtx, X, creating all dependencies generated
1130 by the write to the destination of X, and reads of everything mentioned. */
1132 static void
1133 sched_analyze_1 (x, insn)
1134 rtx x;
1135 rtx insn;
1137 register int regno;
1138 register rtx dest = SET_DEST (x);
1140 if (dest == 0)
1141 return;
1143 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
1144 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
1146 if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
1148 /* The second and third arguments are values read by this insn. */
1149 sched_analyze_2 (XEXP (dest, 1), insn);
1150 sched_analyze_2 (XEXP (dest, 2), insn);
1152 dest = SUBREG_REG (dest);
1155 if (GET_CODE (dest) == REG)
1157 register int i;
1159 regno = REGNO (dest);
1161 /* A hard reg in a wide mode may really be multiple registers.
1162 If so, mark all of them just like the first. */
1163 if (regno < FIRST_PSEUDO_REGISTER)
1165 i = HARD_REGNO_NREGS (regno, GET_MODE (dest));
1166 while (--i >= 0)
1168 rtx u;
1170 for (u = reg_last_uses[regno+i]; u; u = XEXP (u, 1))
1171 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1172 reg_last_uses[regno + i] = 0;
1173 if (reg_last_sets[regno + i])
1174 add_dependence (insn, reg_last_sets[regno + i],
1175 REG_DEP_OUTPUT);
1176 SET_REGNO_REG_SET (reg_pending_sets, regno + i);
1177 if ((call_used_regs[i] || global_regs[i])
1178 && last_function_call)
1179 /* Function calls clobber all call_used regs. */
1180 add_dependence (insn, last_function_call, REG_DEP_ANTI);
1183 else
1185 rtx u;
1187 for (u = reg_last_uses[regno]; u; u = XEXP (u, 1))
1188 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1189 reg_last_uses[regno] = 0;
1190 if (reg_last_sets[regno])
1191 add_dependence (insn, reg_last_sets[regno], REG_DEP_OUTPUT);
1192 SET_REGNO_REG_SET (reg_pending_sets, regno);
1194 /* Pseudos that are REG_EQUIV to something may be replaced
1195 by that during reloading. We need only add dependencies for
1196 the address in the REG_EQUIV note. */
1197 if (! reload_completed
1198 && reg_known_equiv_p[regno]
1199 && GET_CODE (reg_known_value[regno]) == MEM)
1200 sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
1202 /* Don't let it cross a call after scheduling if it doesn't
1203 already cross one. */
1204 if (REG_N_CALLS_CROSSED (regno) == 0 && last_function_call)
1205 add_dependence (insn, last_function_call, REG_DEP_ANTI);
1208 else if (GET_CODE (dest) == MEM)
1210 /* Writing memory. */
1212 if (pending_lists_length > 32)
1214 /* Flush all pending reads and writes to prevent the pending lists
1215 from getting any larger. Insn scheduling runs too slowly when
1216 these lists get long. The number 32 was chosen because it
1217 seems like a reasonable number. When compiling GCC with itself,
1218 this flush occurs 8 times for sparc, and 10 times for m88k using
1219 the number 32. */
1220 flush_pending_lists (insn, 0);
1222 else
1224 rtx pending, pending_mem;
1226 pending = pending_read_insns;
1227 pending_mem = pending_read_mems;
1228 while (pending)
1230 /* If a dependency already exists, don't create a new one. */
1231 if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
1232 if (anti_dependence (XEXP (pending_mem, 0), dest))
1233 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1235 pending = XEXP (pending, 1);
1236 pending_mem = XEXP (pending_mem, 1);
1239 pending = pending_write_insns;
1240 pending_mem = pending_write_mems;
1241 while (pending)
1243 /* If a dependency already exists, don't create a new one. */
1244 if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
1245 if (output_dependence (XEXP (pending_mem, 0), dest))
1246 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
1248 pending = XEXP (pending, 1);
1249 pending_mem = XEXP (pending_mem, 1);
1252 if (last_pending_memory_flush)
1253 add_dependence (insn, last_pending_memory_flush, REG_DEP_ANTI);
1255 add_insn_mem_dependence (&pending_write_insns, &pending_write_mems,
1256 insn, dest);
1258 sched_analyze_2 (XEXP (dest, 0), insn);
1261 /* Analyze reads. */
1262 if (GET_CODE (x) == SET)
1263 sched_analyze_2 (SET_SRC (x), insn);
1266 /* Analyze the uses of memory and registers in rtx X in INSN. */
1268 static void
1269 sched_analyze_2 (x, insn)
1270 rtx x;
1271 rtx insn;
1273 register int i;
1274 register int j;
1275 register enum rtx_code code;
1276 register char *fmt;
1278 if (x == 0)
1279 return;
1281 code = GET_CODE (x);
1283 switch (code)
1285 case CONST_INT:
1286 case CONST_DOUBLE:
1287 case SYMBOL_REF:
1288 case CONST:
1289 case LABEL_REF:
1290 /* Ignore constants. Note that we must handle CONST_DOUBLE here
1291 because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
1292 this does not mean that this insn is using cc0. */
1293 return;
1295 #ifdef HAVE_cc0
1296 case CC0:
1298 rtx link, prev;
1300 /* User of CC0 depends on immediately preceding insn. */
1301 SCHED_GROUP_P (insn) = 1;
1303 /* There may be a note before this insn now, but all notes will
1304 be removed before we actually try to schedule the insns, so
1305 it won't cause a problem later. We must avoid it here though. */
1306 prev = prev_nonnote_insn (insn);
1308 /* Make a copy of all dependencies on the immediately previous insn,
1309 and add to this insn. This is so that all the dependencies will
1310 apply to the group. Remove an explicit dependence on this insn
1311 as SCHED_GROUP_P now represents it. */
1313 if (find_insn_list (prev, LOG_LINKS (insn)))
1314 remove_dependence (insn, prev);
1316 for (link = LOG_LINKS (prev); link; link = XEXP (link, 1))
1317 add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
1319 return;
1321 #endif
1323 case REG:
1325 int regno = REGNO (x);
1326 if (regno < FIRST_PSEUDO_REGISTER)
1328 int i;
1330 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
1331 while (--i >= 0)
1333 reg_last_uses[regno + i]
1334 = gen_rtx_INSN_LIST (VOIDmode,
1335 insn, reg_last_uses[regno + i]);
1336 if (reg_last_sets[regno + i])
1337 add_dependence (insn, reg_last_sets[regno + i], 0);
1338 if ((call_used_regs[regno + i] || global_regs[regno + i])
1339 && last_function_call)
1340 /* Function calls clobber all call_used regs. */
1341 add_dependence (insn, last_function_call, REG_DEP_ANTI);
1344 else
1346 reg_last_uses[regno]
1347 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_last_uses[regno]);
1348 if (reg_last_sets[regno])
1349 add_dependence (insn, reg_last_sets[regno], 0);
1351 /* Pseudos that are REG_EQUIV to something may be replaced
1352 by that during reloading. We need only add dependencies for
1353 the address in the REG_EQUIV note. */
1354 if (! reload_completed
1355 && reg_known_equiv_p[regno]
1356 && GET_CODE (reg_known_value[regno]) == MEM)
1357 sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
1359 /* If the register does not already cross any calls, then add this
1360 insn to the sched_before_next_call list so that it will still
1361 not cross calls after scheduling. */
1362 if (REG_N_CALLS_CROSSED (regno) == 0)
1363 add_dependence (sched_before_next_call, insn, REG_DEP_ANTI);
1365 return;
1368 case MEM:
1370 /* Reading memory. */
1372 rtx pending, pending_mem;
1374 pending = pending_read_insns;
1375 pending_mem = pending_read_mems;
1376 while (pending)
1378 /* If a dependency already exists, don't create a new one. */
1379 if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
1380 if (read_dependence (XEXP (pending_mem, 0), x))
1381 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
1383 pending = XEXP (pending, 1);
1384 pending_mem = XEXP (pending_mem, 1);
1387 pending = pending_write_insns;
1388 pending_mem = pending_write_mems;
1389 while (pending)
1391 /* If a dependency already exists, don't create a new one. */
1392 if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
1393 if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
1394 x, rtx_varies_p))
1395 add_dependence (insn, XEXP (pending, 0), 0);
1397 pending = XEXP (pending, 1);
1398 pending_mem = XEXP (pending_mem, 1);
1400 if (last_pending_memory_flush)
1401 add_dependence (insn, last_pending_memory_flush, REG_DEP_ANTI);
1403 /* Always add these dependencies to pending_reads, since
1404 this insn may be followed by a write. */
1405 add_insn_mem_dependence (&pending_read_insns, &pending_read_mems,
1406 insn, x);
1408 /* Take advantage of tail recursion here. */
1409 sched_analyze_2 (XEXP (x, 0), insn);
1410 return;
1413 case ASM_OPERANDS:
1414 case ASM_INPUT:
1415 case UNSPEC_VOLATILE:
1416 case TRAP_IF:
1418 rtx u;
1420 /* Traditional and volatile asm instructions must be considered to use
1421 and clobber all hard registers, all pseudo-registers and all of
1422 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
1424 Consider for instance a volatile asm that changes the fpu rounding
1425 mode. An insn should not be moved across this even if it only uses
1426 pseudo-regs because it might give an incorrectly rounded result. */
1427 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
1429 int max_reg = max_reg_num ();
1430 for (i = 0; i < max_reg; i++)
1432 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
1433 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1434 reg_last_uses[i] = 0;
1435 if (reg_last_sets[i])
1436 add_dependence (insn, reg_last_sets[i], 0);
1438 reg_pending_sets_all = 1;
1440 flush_pending_lists (insn, 0);
1443 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1444 We can not just fall through here since then we would be confused
1445 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1446 traditional asms unlike their normal usage. */
1448 if (code == ASM_OPERANDS)
1450 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
1451 sched_analyze_2 (ASM_OPERANDS_INPUT (x, j), insn);
1452 return;
1454 break;
1457 case PRE_DEC:
1458 case POST_DEC:
1459 case PRE_INC:
1460 case POST_INC:
1461 /* These both read and modify the result. We must handle them as writes
1462 to get proper dependencies for following instructions. We must handle
1463 them as reads to get proper dependencies from this to previous
1464 instructions. Thus we need to pass them to both sched_analyze_1
1465 and sched_analyze_2. We must call sched_analyze_2 first in order
1466 to get the proper antecedent for the read. */
1467 sched_analyze_2 (XEXP (x, 0), insn);
1468 sched_analyze_1 (x, insn);
1469 return;
1471 default:
1472 break;
1475 /* Other cases: walk the insn. */
1476 fmt = GET_RTX_FORMAT (code);
1477 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1479 if (fmt[i] == 'e')
1480 sched_analyze_2 (XEXP (x, i), insn);
1481 else if (fmt[i] == 'E')
1482 for (j = 0; j < XVECLEN (x, i); j++)
1483 sched_analyze_2 (XVECEXP (x, i, j), insn);
1487 /* Analyze an INSN with pattern X to find all dependencies. */
1489 static void
1490 sched_analyze_insn (x, insn, loop_notes)
1491 rtx x, insn;
1492 rtx loop_notes;
1494 register RTX_CODE code = GET_CODE (x);
1495 rtx link;
1496 int maxreg = max_reg_num ();
1497 int i;
1499 if (code == SET || code == CLOBBER)
1500 sched_analyze_1 (x, insn);
1501 else if (code == PARALLEL)
1503 register int i;
1504 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1506 code = GET_CODE (XVECEXP (x, 0, i));
1507 if (code == SET || code == CLOBBER)
1508 sched_analyze_1 (XVECEXP (x, 0, i), insn);
1509 else
1510 sched_analyze_2 (XVECEXP (x, 0, i), insn);
1513 else
1514 sched_analyze_2 (x, insn);
1516 /* Mark registers CLOBBERED or used by called function. */
1517 if (GET_CODE (insn) == CALL_INSN)
1518 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1520 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
1521 sched_analyze_1 (XEXP (link, 0), insn);
1522 else
1523 sched_analyze_2 (XEXP (link, 0), insn);
1526 /* If there is a {LOOP,EHREGION}_{BEG,END} note in the middle of a basic block, then
1527 we must be sure that no instructions are scheduled across it.
1528 Otherwise, the reg_n_refs info (which depends on loop_depth) would
1529 become incorrect. */
1531 if (loop_notes)
1533 int max_reg = max_reg_num ();
1534 rtx link;
1536 for (i = 0; i < max_reg; i++)
1538 rtx u;
1539 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
1540 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1541 reg_last_uses[i] = 0;
1542 if (reg_last_sets[i])
1543 add_dependence (insn, reg_last_sets[i], 0);
1545 reg_pending_sets_all = 1;
1547 flush_pending_lists (insn, 0);
1549 link = loop_notes;
1550 while (XEXP (link, 1))
1551 link = XEXP (link, 1);
1552 XEXP (link, 1) = REG_NOTES (insn);
1553 REG_NOTES (insn) = loop_notes;
1556 /* After reload, it is possible for an instruction to have a REG_DEAD note
1557 for a register that actually dies a few instructions earlier. For
1558 example, this can happen with SECONDARY_MEMORY_NEEDED reloads.
1559 In this case, we must consider the insn to use the register mentioned
1560 in the REG_DEAD note. Otherwise, we may accidentally move this insn
1561 after another insn that sets the register, thus getting obviously invalid
1562 rtl. This confuses reorg which believes that REG_DEAD notes are still
1563 meaningful.
1565 ??? We would get better code if we fixed reload to put the REG_DEAD
1566 notes in the right places, but that may not be worth the effort. */
1568 if (reload_completed)
1570 rtx note;
1572 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1573 if (REG_NOTE_KIND (note) == REG_DEAD)
1574 sched_analyze_2 (XEXP (note, 0), insn);
1577 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
1579 reg_last_sets[i] = insn;
1581 CLEAR_REG_SET (reg_pending_sets);
1583 if (reg_pending_sets_all)
1585 for (i = 0; i < maxreg; i++)
1586 reg_last_sets[i] = insn;
1587 reg_pending_sets_all = 0;
1590 /* Handle function calls and function returns created by the epilogue
1591 threading code. */
1592 if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
1594 rtx dep_insn;
1595 rtx prev_dep_insn;
1597 /* When scheduling instructions, we make sure calls don't lose their
1598 accompanying USE insns by depending them one on another in order.
1600 Also, we must do the same thing for returns created by the epilogue
1601 threading code. Note this code works only in this special case,
1602 because other passes make no guarantee that they will never emit
1603 an instruction between a USE and a RETURN. There is such a guarantee
1604 for USE instructions immediately before a call. */
1606 prev_dep_insn = insn;
1607 dep_insn = PREV_INSN (insn);
1608 while (GET_CODE (dep_insn) == INSN
1609 && GET_CODE (PATTERN (dep_insn)) == USE
1610 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == REG)
1612 SCHED_GROUP_P (prev_dep_insn) = 1;
1614 /* Make a copy of all dependencies on dep_insn, and add to insn.
1615 This is so that all of the dependencies will apply to the
1616 group. */
1618 for (link = LOG_LINKS (dep_insn); link; link = XEXP (link, 1))
1619 add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
1621 prev_dep_insn = dep_insn;
1622 dep_insn = PREV_INSN (dep_insn);
1627 /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
1628 for every dependency. */
1630 static int
1631 sched_analyze (head, tail)
1632 rtx head, tail;
1634 register rtx insn;
1635 register int n_insns = 0;
1636 register rtx u;
1637 register int luid = 0;
1638 rtx loop_notes = 0;
1640 for (insn = head; ; insn = NEXT_INSN (insn))
1642 INSN_LUID (insn) = luid++;
1644 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
1646 sched_analyze_insn (PATTERN (insn), insn, loop_notes);
1647 loop_notes = 0;
1648 n_insns += 1;
1650 else if (GET_CODE (insn) == CALL_INSN)
1652 rtx x;
1653 register int i;
1655 /* Any instruction using a hard register which may get clobbered
1656 by a call needs to be marked as dependent on this call.
1657 This prevents a use of a hard return reg from being moved
1658 past a void call (i.e. it does not explicitly set the hard
1659 return reg). */
1661 /* If this call is followed by a NOTE_INSN_SETJMP, then assume that
1662 all registers, not just hard registers, may be clobbered by this
1663 call. */
1665 /* Insn, being a CALL_INSN, magically depends on
1666 `last_function_call' already. */
1668 if (NEXT_INSN (insn) && GET_CODE (NEXT_INSN (insn)) == NOTE
1669 && NOTE_LINE_NUMBER (NEXT_INSN (insn)) == NOTE_INSN_SETJMP)
1671 int max_reg = max_reg_num ();
1672 for (i = 0; i < max_reg; i++)
1674 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
1675 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1676 reg_last_uses[i] = 0;
1677 if (reg_last_sets[i])
1678 add_dependence (insn, reg_last_sets[i], 0);
1680 reg_pending_sets_all = 1;
1682 /* Add a pair of fake REG_NOTEs which we will later
1683 convert back into a NOTE_INSN_SETJMP note. See
1684 reemit_notes for why we use a pair of NOTEs. */
1686 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD,
1687 GEN_INT (0),
1688 REG_NOTES (insn));
1689 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD,
1690 GEN_INT (NOTE_INSN_SETJMP),
1691 REG_NOTES (insn));
1693 else
1695 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1696 if (call_used_regs[i] || global_regs[i])
1698 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
1699 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
1700 reg_last_uses[i] = 0;
1701 if (reg_last_sets[i])
1702 add_dependence (insn, reg_last_sets[i], REG_DEP_ANTI);
1703 SET_REGNO_REG_SET (reg_pending_sets, i);
1707 /* For each insn which shouldn't cross a call, add a dependence
1708 between that insn and this call insn. */
1709 x = LOG_LINKS (sched_before_next_call);
1710 while (x)
1712 add_dependence (insn, XEXP (x, 0), REG_DEP_ANTI);
1713 x = XEXP (x, 1);
1715 LOG_LINKS (sched_before_next_call) = 0;
1717 sched_analyze_insn (PATTERN (insn), insn, loop_notes);
1718 loop_notes = 0;
1720 /* In the absence of interprocedural alias analysis, we must flush
1721 all pending reads and writes, and start new dependencies starting
1722 from here. But only flush writes for constant calls (which may
1723 be passed a pointer to something we haven't written yet). */
1724 flush_pending_lists (insn, CONST_CALL_P (insn));
1726 /* Depend this function call (actually, the user of this
1727 function call) on all hard register clobberage. */
1728 last_function_call = insn;
1729 n_insns += 1;
1732 /* See comments on reemit_notes as to why we do this. */
1733 else if (GET_CODE (insn) == NOTE
1734 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
1735 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
1736 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1737 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END
1738 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_RANGE_START
1739 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_RANGE_END
1740 || (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP
1741 && GET_CODE (PREV_INSN (insn)) != CALL_INSN)))
1743 loop_notes = gen_rtx_EXPR_LIST (REG_DEAD,
1744 GEN_INT (NOTE_BLOCK_NUMBER (insn)),
1745 loop_notes);
1746 loop_notes = gen_rtx_EXPR_LIST (REG_DEAD,
1747 GEN_INT (NOTE_LINE_NUMBER (insn)),
1748 loop_notes);
1749 CONST_CALL_P (loop_notes) = CONST_CALL_P (insn);
1752 if (insn == tail)
1753 return n_insns;
1756 abort ();
1759 /* Called when we see a set of a register. If death is true, then we are
1760 scanning backwards. Mark that register as unborn. If nobody says
1761 otherwise, that is how things will remain. If death is false, then we
1762 are scanning forwards. Mark that register as being born. */
1764 static void
1765 sched_note_set (b, x, death)
1766 int b;
1767 rtx x;
1768 int death;
1770 register int regno;
1771 register rtx reg = SET_DEST (x);
1772 int subreg_p = 0;
1774 if (reg == 0)
1775 return;
1777 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == STRICT_LOW_PART
1778 || GET_CODE (reg) == SIGN_EXTRACT || GET_CODE (reg) == ZERO_EXTRACT)
1780 /* Must treat modification of just one hardware register of a multi-reg
1781 value or just a byte field of a register exactly the same way that
1782 mark_set_1 in flow.c does, i.e. anything except a paradoxical subreg
1783 does not kill the entire register. */
1784 if (GET_CODE (reg) != SUBREG
1785 || REG_SIZE (SUBREG_REG (reg)) > REG_SIZE (reg))
1786 subreg_p = 1;
1788 reg = SUBREG_REG (reg);
1791 if (GET_CODE (reg) != REG)
1792 return;
1794 /* Global registers are always live, so the code below does not apply
1795 to them. */
1797 regno = REGNO (reg);
1798 if (regno >= FIRST_PSEUDO_REGISTER || ! global_regs[regno])
1800 if (death)
1802 /* If we only set part of the register, then this set does not
1803 kill it. */
1804 if (subreg_p)
1805 return;
1807 /* Try killing this register. */
1808 if (regno < FIRST_PSEUDO_REGISTER)
1810 int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1811 while (--j >= 0)
1813 CLEAR_REGNO_REG_SET (bb_live_regs, regno + j);
1814 SET_REGNO_REG_SET (bb_dead_regs, regno + j);
1817 else
1819 CLEAR_REGNO_REG_SET (bb_live_regs, regno);
1820 SET_REGNO_REG_SET (bb_dead_regs, regno);
1823 else
1825 /* Make the register live again. */
1826 if (regno < FIRST_PSEUDO_REGISTER)
1828 int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1829 while (--j >= 0)
1831 SET_REGNO_REG_SET (bb_live_regs, regno + j);
1832 CLEAR_REGNO_REG_SET (bb_dead_regs, regno + j);
1835 else
1837 SET_REGNO_REG_SET (bb_live_regs, regno);
1838 CLEAR_REGNO_REG_SET (bb_dead_regs, regno);
1844 /* Macros and functions for keeping the priority queue sorted, and
1845 dealing with queueing and dequeueing of instructions. */
1847 #define SCHED_SORT(READY, NEW_READY, OLD_READY) \
1848 do { if ((NEW_READY) - (OLD_READY) == 1) \
1849 swap_sort (READY, NEW_READY); \
1850 else if ((NEW_READY) - (OLD_READY) > 1) \
1851 qsort (READY, NEW_READY, sizeof (rtx), rank_for_schedule); } \
1852 while (0)
1854 /* Returns a positive value if y is preferred; returns a negative value if
1855 x is preferred. Should never return 0, since that will make the sort
1856 unstable. */
1858 static int
1859 rank_for_schedule (x, y)
1860 const GENERIC_PTR x;
1861 const GENERIC_PTR y;
1863 rtx tmp = *(rtx *)y;
1864 rtx tmp2 = *(rtx *)x;
1865 rtx link;
1866 int tmp_class, tmp2_class;
1867 int value;
1869 /* Choose the instruction with the highest priority, if different. */
1870 if ((value = INSN_PRIORITY (tmp) - INSN_PRIORITY (tmp2)))
1871 return value;
1873 if (last_scheduled_insn)
1875 /* Classify the instructions into three classes:
1876 1) Data dependent on last schedule insn.
1877 2) Anti/Output dependent on last scheduled insn.
1878 3) Independent of last scheduled insn, or has latency of one.
1879 Choose the insn from the highest numbered class if different. */
1880 link = find_insn_list (tmp, LOG_LINKS (last_scheduled_insn));
1881 if (link == 0 || insn_cost (tmp, link, last_scheduled_insn) == 1)
1882 tmp_class = 3;
1883 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
1884 tmp_class = 1;
1885 else
1886 tmp_class = 2;
1888 link = find_insn_list (tmp2, LOG_LINKS (last_scheduled_insn));
1889 if (link == 0 || insn_cost (tmp2, link, last_scheduled_insn) == 1)
1890 tmp2_class = 3;
1891 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
1892 tmp2_class = 1;
1893 else
1894 tmp2_class = 2;
1896 if ((value = tmp_class - tmp2_class))
1897 return value;
1900 /* If insns are equally good, sort by INSN_LUID (original insn order),
1901 so that we make the sort stable. This minimizes instruction movement,
1902 thus minimizing sched's effect on debugging and cross-jumping. */
1903 return INSN_LUID (tmp) - INSN_LUID (tmp2);
1906 /* Resort the array A in which only element at index N may be out of order. */
1908 __inline static void
1909 swap_sort (a, n)
1910 rtx *a;
1911 int n;
1913 rtx insn = a[n-1];
1914 int i = n-2;
1916 while (i >= 0 && rank_for_schedule (a+i, &insn) >= 0)
1918 a[i+1] = a[i];
1919 i -= 1;
1921 a[i+1] = insn;
1924 static int max_priority;
1926 /* Add INSN to the insn queue so that it fires at least N_CYCLES
1927 before the currently executing insn. */
1929 __inline static void
1930 queue_insn (insn, n_cycles)
1931 rtx insn;
1932 int n_cycles;
1934 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
1935 NEXT_INSN (insn) = insn_queue[next_q];
1936 insn_queue[next_q] = insn;
1937 q_size += 1;
1940 /* Return nonzero if PAT is the pattern of an insn which makes a
1941 register live. */
1943 __inline static int
1944 birthing_insn_p (pat)
1945 rtx pat;
1947 int j;
1949 if (reload_completed == 1)
1950 return 0;
1952 if (GET_CODE (pat) == SET
1953 && GET_CODE (SET_DEST (pat)) == REG)
1955 rtx dest = SET_DEST (pat);
1956 int i = REGNO (dest);
1958 /* It would be more accurate to use refers_to_regno_p or
1959 reg_mentioned_p to determine when the dest is not live before this
1960 insn. */
1962 if (REGNO_REG_SET_P (bb_live_regs, i))
1963 return (REG_N_SETS (i) == 1);
1965 return 0;
1967 if (GET_CODE (pat) == PARALLEL)
1969 for (j = 0; j < XVECLEN (pat, 0); j++)
1970 if (birthing_insn_p (XVECEXP (pat, 0, j)))
1971 return 1;
1973 return 0;
1976 /* PREV is an insn that is ready to execute. Adjust its priority if that
1977 will help shorten register lifetimes. */
1979 __inline static void
1980 adjust_priority (prev)
1981 rtx prev;
1983 /* Trying to shorten register lives after reload has completed
1984 is useless and wrong. It gives inaccurate schedules. */
1985 if (reload_completed == 0)
1987 rtx note;
1988 int n_deaths = 0;
1990 /* ??? This code has no effect, because REG_DEAD notes are removed
1991 before we ever get here. */
1992 for (note = REG_NOTES (prev); note; note = XEXP (note, 1))
1993 if (REG_NOTE_KIND (note) == REG_DEAD)
1994 n_deaths += 1;
1996 /* Defer scheduling insns which kill registers, since that
1997 shortens register lives. Prefer scheduling insns which
1998 make registers live for the same reason. */
1999 switch (n_deaths)
2001 default:
2002 INSN_PRIORITY (prev) >>= 3;
2003 break;
2004 case 3:
2005 INSN_PRIORITY (prev) >>= 2;
2006 break;
2007 case 2:
2008 case 1:
2009 INSN_PRIORITY (prev) >>= 1;
2010 break;
2011 case 0:
2012 if (birthing_insn_p (PATTERN (prev)))
2014 int max = max_priority;
2016 if (max > INSN_PRIORITY (prev))
2017 INSN_PRIORITY (prev) = max;
2019 break;
2021 #ifdef ADJUST_PRIORITY
2022 ADJUST_PRIORITY (prev);
2023 #endif
2027 /* INSN is the "currently executing insn". Launch each insn which was
2028 waiting on INSN (in the backwards dataflow sense). READY is a
2029 vector of insns which are ready to fire. N_READY is the number of
2030 elements in READY. CLOCK is the current virtual cycle. */
2032 static int
2033 schedule_insn (insn, ready, n_ready, clock)
2034 rtx insn;
2035 rtx *ready;
2036 int n_ready;
2037 int clock;
2039 rtx link;
2040 int new_ready = n_ready;
2042 if (MAX_BLOCKAGE > 1)
2043 schedule_unit (insn_unit (insn), insn, clock);
2045 if (LOG_LINKS (insn) == 0)
2046 return n_ready;
2048 /* This is used by the function adjust_priority above. */
2049 if (n_ready > 0)
2050 max_priority = MAX (INSN_PRIORITY (ready[0]), INSN_PRIORITY (insn));
2051 else
2052 max_priority = INSN_PRIORITY (insn);
2054 for (link = LOG_LINKS (insn); link != 0; link = XEXP (link, 1))
2056 rtx prev = XEXP (link, 0);
2057 int cost = insn_cost (prev, link, insn);
2059 if ((INSN_REF_COUNT (prev) -= 1) != 0)
2061 /* We satisfied one requirement to fire PREV. Record the earliest
2062 time when PREV can fire. No need to do this if the cost is 1,
2063 because PREV can fire no sooner than the next cycle. */
2064 if (cost > 1)
2065 INSN_TICK (prev) = MAX (INSN_TICK (prev), clock + cost);
2067 else
2069 /* We satisfied the last requirement to fire PREV. Ensure that all
2070 timing requirements are satisfied. */
2071 if (INSN_TICK (prev) - clock > cost)
2072 cost = INSN_TICK (prev) - clock;
2074 /* Adjust the priority of PREV and either put it on the ready
2075 list or queue it. */
2076 adjust_priority (prev);
2077 if (cost <= 1)
2078 ready[new_ready++] = prev;
2079 else
2080 queue_insn (prev, cost);
2084 return new_ready;
2087 /* Given N_READY insns in the ready list READY at time CLOCK, queue
2088 those that are blocked due to function unit hazards and rearrange
2089 the remaining ones to minimize subsequent function unit hazards. */
2091 static int
2092 schedule_select (ready, n_ready, clock, file)
2093 rtx *ready;
2094 int n_ready, clock;
2095 FILE *file;
2097 int pri = INSN_PRIORITY (ready[0]);
2098 int i, j, k, q, cost, best_cost, best_insn = 0, new_ready = n_ready;
2099 rtx insn;
2101 /* Work down the ready list in groups of instructions with the same
2102 priority value. Queue insns in the group that are blocked and
2103 select among those that remain for the one with the largest
2104 potential hazard. */
2105 for (i = 0; i < n_ready; i = j)
2107 int opri = pri;
2108 for (j = i + 1; j < n_ready; j++)
2109 if ((pri = INSN_PRIORITY (ready[j])) != opri)
2110 break;
2112 /* Queue insns in the group that are blocked. */
2113 for (k = i, q = 0; k < j; k++)
2115 insn = ready[k];
2116 if ((cost = actual_hazard (insn_unit (insn), insn, clock, 0)) != 0)
2118 q++;
2119 ready[k] = 0;
2120 queue_insn (insn, cost);
2121 if (file)
2122 fprintf (file, "\n;; blocking insn %d for %d cycles",
2123 INSN_UID (insn), cost);
2126 new_ready -= q;
2128 /* Check the next group if all insns were queued. */
2129 if (j - i - q == 0)
2130 continue;
2132 /* If more than one remains, select the first one with the largest
2133 potential hazard. */
2134 else if (j - i - q > 1)
2136 best_cost = -1;
2137 for (k = i; k < j; k++)
2139 if ((insn = ready[k]) == 0)
2140 continue;
2141 if ((cost = potential_hazard (insn_unit (insn), insn, 0))
2142 > best_cost)
2144 best_cost = cost;
2145 best_insn = k;
2149 /* We have found a suitable insn to schedule. */
2150 break;
2153 /* Move the best insn to be front of the ready list. */
2154 if (best_insn != 0)
2156 if (file)
2158 fprintf (file, ", now");
2159 for (i = 0; i < n_ready; i++)
2160 if (ready[i])
2161 fprintf (file, " %d", INSN_UID (ready[i]));
2162 fprintf (file, "\n;; insn %d has a greater potential hazard",
2163 INSN_UID (ready[best_insn]));
2165 for (i = best_insn; i > 0; i--)
2167 insn = ready[i-1];
2168 ready[i-1] = ready[i];
2169 ready[i] = insn;
2173 /* Compact the ready list. */
2174 if (new_ready < n_ready)
2175 for (i = j = 0; i < n_ready; i++)
2176 if (ready[i])
2177 ready[j++] = ready[i];
2179 return new_ready;
2182 /* Add a REG_DEAD note for REG to INSN, reusing a REG_DEAD note from the
2183 dead_notes list. */
2185 static void
2186 create_reg_dead_note (reg, insn)
2187 rtx reg, insn;
2189 rtx link;
2191 /* The number of registers killed after scheduling must be the same as the
2192 number of registers killed before scheduling. The number of REG_DEAD
2193 notes may not be conserved, i.e. two SImode hard register REG_DEAD notes
2194 might become one DImode hard register REG_DEAD note, but the number of
2195 registers killed will be conserved.
2197 We carefully remove REG_DEAD notes from the dead_notes list, so that
2198 there will be none left at the end. If we run out early, then there
2199 is a bug somewhere in flow, combine and/or sched. */
2201 if (dead_notes == 0)
2203 #if 1
2204 abort ();
2205 #else
2206 link = rtx_alloc (EXPR_LIST);
2207 PUT_REG_NOTE_KIND (link, REG_DEAD);
2208 #endif
2210 else
2212 /* Number of regs killed by REG. */
2213 int regs_killed = (REGNO (reg) >= FIRST_PSEUDO_REGISTER ? 1
2214 : HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)));
2215 /* Number of regs killed by REG_DEAD notes taken off the list. */
2216 int reg_note_regs;
2218 link = dead_notes;
2219 reg_note_regs = (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
2220 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
2221 GET_MODE (XEXP (link, 0))));
2222 while (reg_note_regs < regs_killed)
2224 /* LINK might be zero if we killed more registers after scheduling
2225 than before, and the last hard register we kill is actually
2226 multiple hard regs. */
2227 if (link == NULL_RTX)
2228 abort ();
2230 link = XEXP (link, 1);
2231 reg_note_regs += (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
2232 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
2233 GET_MODE (XEXP (link, 0))));
2235 dead_notes = XEXP (link, 1);
2237 /* If we took too many regs kills off, put the extra ones back. */
2238 while (reg_note_regs > regs_killed)
2240 rtx temp_reg, temp_link;
2242 temp_reg = gen_rtx_REG (word_mode, 0);
2243 temp_link = rtx_alloc (EXPR_LIST);
2244 PUT_REG_NOTE_KIND (temp_link, REG_DEAD);
2245 XEXP (temp_link, 0) = temp_reg;
2246 XEXP (temp_link, 1) = dead_notes;
2247 dead_notes = temp_link;
2248 reg_note_regs--;
2252 XEXP (link, 0) = reg;
2253 XEXP (link, 1) = REG_NOTES (insn);
2254 REG_NOTES (insn) = link;
2257 /* Subroutine on attach_deaths_insn--handles the recursive search
2258 through INSN. If SET_P is true, then x is being modified by the insn. */
2260 static void
2261 attach_deaths (x, insn, set_p)
2262 rtx x;
2263 rtx insn;
2264 int set_p;
2266 register int i;
2267 register int j;
2268 register enum rtx_code code;
2269 register char *fmt;
2271 if (x == 0)
2272 return;
2274 code = GET_CODE (x);
2276 switch (code)
2278 case CONST_INT:
2279 case CONST_DOUBLE:
2280 case LABEL_REF:
2281 case SYMBOL_REF:
2282 case CONST:
2283 case CODE_LABEL:
2284 case PC:
2285 case CC0:
2286 /* Get rid of the easy cases first. */
2287 return;
2289 case REG:
2291 /* If the register dies in this insn, queue that note, and mark
2292 this register as needing to die. */
2293 /* This code is very similar to mark_used_1 (if set_p is false)
2294 and mark_set_1 (if set_p is true) in flow.c. */
2296 register int regno;
2297 int some_needed;
2298 int all_needed;
2300 if (set_p)
2301 return;
2303 regno = REGNO (x);
2304 all_needed = some_needed = REGNO_REG_SET_P (old_live_regs, regno);
2305 if (regno < FIRST_PSEUDO_REGISTER)
2307 int n;
2309 n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2310 while (--n > 0)
2312 int needed = (REGNO_REG_SET_P (old_live_regs, regno + n));
2313 some_needed |= needed;
2314 all_needed &= needed;
2318 /* If it wasn't live before we started, then add a REG_DEAD note.
2319 We must check the previous lifetime info not the current info,
2320 because we may have to execute this code several times, e.g.
2321 once for a clobber (which doesn't add a note) and later
2322 for a use (which does add a note).
2324 Always make the register live. We must do this even if it was
2325 live before, because this may be an insn which sets and uses
2326 the same register, in which case the register has already been
2327 killed, so we must make it live again.
2329 Global registers are always live, and should never have a REG_DEAD
2330 note added for them, so none of the code below applies to them. */
2332 if (regno >= FIRST_PSEUDO_REGISTER || ! global_regs[regno])
2334 /* Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2335 STACK_POINTER_REGNUM, since these are always considered to be
2336 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2337 if (regno != FRAME_POINTER_REGNUM
2338 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2339 && ! (regno == HARD_FRAME_POINTER_REGNUM)
2340 #endif
2341 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2342 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2343 #endif
2344 && regno != STACK_POINTER_REGNUM)
2346 if (! all_needed && ! dead_or_set_p (insn, x))
2348 /* Check for the case where the register dying partially
2349 overlaps the register set by this insn. */
2350 if (regno < FIRST_PSEUDO_REGISTER
2351 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
2353 int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2354 while (--n >= 0)
2355 some_needed |= dead_or_set_regno_p (insn, regno + n);
2358 /* If none of the words in X is needed, make a REG_DEAD
2359 note. Otherwise, we must make partial REG_DEAD
2360 notes. */
2361 if (! some_needed)
2362 create_reg_dead_note (x, insn);
2363 else
2365 int i;
2367 /* Don't make a REG_DEAD note for a part of a
2368 register that is set in the insn. */
2369 for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
2370 i >= 0; i--)
2371 if (! REGNO_REG_SET_P (old_live_regs, regno + i)
2372 && ! dead_or_set_regno_p (insn, regno + i))
2373 create_reg_dead_note (gen_rtx_REG (reg_raw_mode[regno + i],
2374 regno + i),
2375 insn);
2380 if (regno < FIRST_PSEUDO_REGISTER)
2382 int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
2383 while (--j >= 0)
2385 CLEAR_REGNO_REG_SET (bb_dead_regs, regno + j);
2386 SET_REGNO_REG_SET (bb_live_regs, regno + j);
2389 else
2391 CLEAR_REGNO_REG_SET (bb_dead_regs, regno);
2392 SET_REGNO_REG_SET (bb_live_regs, regno);
2395 return;
2398 case MEM:
2399 /* Handle tail-recursive case. */
2400 attach_deaths (XEXP (x, 0), insn, 0);
2401 return;
2403 case SUBREG:
2404 attach_deaths (SUBREG_REG (x), insn,
2405 set_p && ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
2406 <= UNITS_PER_WORD)
2407 || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
2408 == GET_MODE_SIZE (GET_MODE ((x))))));
2409 return;
2411 case STRICT_LOW_PART:
2412 attach_deaths (XEXP (x, 0), insn, 0);
2413 return;
2415 case ZERO_EXTRACT:
2416 case SIGN_EXTRACT:
2417 attach_deaths (XEXP (x, 0), insn, 0);
2418 attach_deaths (XEXP (x, 1), insn, 0);
2419 attach_deaths (XEXP (x, 2), insn, 0);
2420 return;
2422 default:
2423 /* Other cases: walk the insn. */
2424 fmt = GET_RTX_FORMAT (code);
2425 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2427 if (fmt[i] == 'e')
2428 attach_deaths (XEXP (x, i), insn, 0);
2429 else if (fmt[i] == 'E')
2430 for (j = 0; j < XVECLEN (x, i); j++)
2431 attach_deaths (XVECEXP (x, i, j), insn, 0);
2436 /* After INSN has executed, add register death notes for each register
2437 that is dead after INSN. */
2439 static void
2440 attach_deaths_insn (insn)
2441 rtx insn;
2443 rtx x = PATTERN (insn);
2444 register RTX_CODE code = GET_CODE (x);
2445 rtx link;
2447 if (code == SET)
2449 attach_deaths (SET_SRC (x), insn, 0);
2451 /* A register might die here even if it is the destination, e.g.
2452 it is the target of a volatile read and is otherwise unused.
2453 Hence we must always call attach_deaths for the SET_DEST. */
2454 attach_deaths (SET_DEST (x), insn, 1);
2456 else if (code == PARALLEL)
2458 register int i;
2459 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
2461 code = GET_CODE (XVECEXP (x, 0, i));
2462 if (code == SET)
2464 attach_deaths (SET_SRC (XVECEXP (x, 0, i)), insn, 0);
2466 attach_deaths (SET_DEST (XVECEXP (x, 0, i)), insn, 1);
2468 /* Flow does not add REG_DEAD notes to registers that die in
2469 clobbers, so we can't either. */
2470 else if (code != CLOBBER)
2471 attach_deaths (XVECEXP (x, 0, i), insn, 0);
2474 /* If this is a CLOBBER, only add REG_DEAD notes to registers inside a
2475 MEM being clobbered, just like flow. */
2476 else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == MEM)
2477 attach_deaths (XEXP (XEXP (x, 0), 0), insn, 0);
2478 /* Otherwise don't add a death note to things being clobbered. */
2479 else if (code != CLOBBER)
2480 attach_deaths (x, insn, 0);
2482 /* Make death notes for things used in the called function. */
2483 if (GET_CODE (insn) == CALL_INSN)
2484 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2485 attach_deaths (XEXP (XEXP (link, 0), 0), insn,
2486 GET_CODE (XEXP (link, 0)) == CLOBBER);
2489 /* Delete notes beginning with INSN and maybe put them in the chain
2490 of notes ended by NOTE_LIST.
2491 Returns the insn following the notes. */
2493 static rtx
2494 unlink_notes (insn, tail)
2495 rtx insn, tail;
2497 rtx prev = PREV_INSN (insn);
2499 while (insn != tail && GET_CODE (insn) == NOTE)
2501 rtx next = NEXT_INSN (insn);
2502 /* Delete the note from its current position. */
2503 if (prev)
2504 NEXT_INSN (prev) = next;
2505 if (next)
2506 PREV_INSN (next) = prev;
2508 if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
2509 /* Record line-number notes so they can be reused. */
2510 LINE_NOTE (insn) = insn;
2512 /* Don't save away NOTE_INSN_SETJMPs, because they must remain
2513 immediately after the call they follow. We use a fake
2514 (REG_DEAD (const_int -1)) note to remember them.
2515 Likewise with NOTE_INSN_{LOOP,EHREGION}_{BEG, END}. */
2516 else if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_SETJMP
2517 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
2518 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
2519 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_RANGE_START
2520 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_RANGE_END
2521 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
2522 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
2524 /* Insert the note at the end of the notes list. */
2525 PREV_INSN (insn) = note_list;
2526 if (note_list)
2527 NEXT_INSN (note_list) = insn;
2528 note_list = insn;
2531 insn = next;
2533 return insn;
2536 /* Constructor for `sometimes' data structure. */
2538 static int
2539 new_sometimes_live (regs_sometimes_live, regno, sometimes_max)
2540 struct sometimes *regs_sometimes_live;
2541 int regno;
2542 int sometimes_max;
2544 register struct sometimes *p;
2546 /* There should never be a register greater than max_regno here. If there
2547 is, it means that a define_split has created a new pseudo reg. This
2548 is not allowed, since there will not be flow info available for any
2549 new register, so catch the error here. */
2550 if (regno >= max_regno)
2551 abort ();
2553 p = &regs_sometimes_live[sometimes_max];
2554 p->regno = regno;
2555 p->live_length = 0;
2556 p->calls_crossed = 0;
2557 sometimes_max++;
2558 return sometimes_max;
2561 /* Count lengths of all regs we are currently tracking,
2562 and find new registers no longer live. */
2564 static void
2565 finish_sometimes_live (regs_sometimes_live, sometimes_max)
2566 struct sometimes *regs_sometimes_live;
2567 int sometimes_max;
2569 int i;
2571 for (i = 0; i < sometimes_max; i++)
2573 register struct sometimes *p = &regs_sometimes_live[i];
2574 int regno = p->regno;
2576 sched_reg_live_length[regno] += p->live_length;
2577 sched_reg_n_calls_crossed[regno] += p->calls_crossed;
2581 /* Search INSN for fake REG_DEAD note pairs for NOTE_INSN_SETJMP,
2582 NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
2583 NOTEs. The REG_DEAD note following first one is contains the saved
2584 value for NOTE_BLOCK_NUMBER which is useful for
2585 NOTE_INSN_EH_REGION_{BEG,END} NOTEs. LAST is the last instruction
2586 output by the instruction scheduler. Return the new value of LAST. */
2588 static rtx
2589 reemit_notes (insn, last)
2590 rtx insn;
2591 rtx last;
2593 rtx note;
2595 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2597 if (REG_NOTE_KIND (note) == REG_DEAD
2598 && GET_CODE (XEXP (note, 0)) == CONST_INT)
2600 if (INTVAL (XEXP (note, 0)) == NOTE_INSN_SETJMP)
2602 CONST_CALL_P (emit_note_after (INTVAL (XEXP (note, 0)), insn))
2603 = CONST_CALL_P (note);
2604 remove_note (insn, note);
2605 note = XEXP (note, 1);
2607 else
2609 last = emit_note_before (INTVAL (XEXP (note, 0)), last);
2610 remove_note (insn, note);
2611 note = XEXP (note, 1);
2612 NOTE_BLOCK_NUMBER (last) = INTVAL (XEXP (note, 0));
2614 remove_note (insn, note);
2617 return last;
2620 /* Use modified list scheduling to rearrange insns in basic block
2621 B. FILE, if nonzero, is where we dump interesting output about
2622 this pass. */
2624 static void
2625 schedule_block (b, file)
2626 int b;
2627 FILE *file;
2629 rtx insn, last;
2630 rtx *ready, link;
2631 int i, j, n_ready = 0, new_ready, n_insns;
2632 int sched_n_insns = 0;
2633 int clock;
2634 #define NEED_NOTHING 0
2635 #define NEED_HEAD 1
2636 #define NEED_TAIL 2
2637 int new_needs;
2639 /* HEAD and TAIL delimit the region being scheduled. */
2640 rtx head = basic_block_head[b];
2641 rtx tail = basic_block_end[b];
2642 /* PREV_HEAD and NEXT_TAIL are the boundaries of the insns
2643 being scheduled. When the insns have been ordered,
2644 these insns delimit where the new insns are to be
2645 spliced back into the insn chain. */
2646 rtx next_tail;
2647 rtx prev_head;
2649 /* Keep life information accurate. */
2650 register struct sometimes *regs_sometimes_live;
2651 int sometimes_max;
2653 if (file)
2654 fprintf (file, ";;\t -- basic block number %d from %d to %d --\n",
2655 b, INSN_UID (basic_block_head[b]), INSN_UID (basic_block_end[b]));
2657 i = max_reg_num ();
2658 reg_last_uses = (rtx *) alloca (i * sizeof (rtx));
2659 bzero ((char *) reg_last_uses, i * sizeof (rtx));
2660 reg_last_sets = (rtx *) alloca (i * sizeof (rtx));
2661 bzero ((char *) reg_last_sets, i * sizeof (rtx));
2662 reg_pending_sets = ALLOCA_REG_SET ();
2663 CLEAR_REG_SET (reg_pending_sets);
2664 reg_pending_sets_all = 0;
2665 clear_units ();
2667 #if 0
2668 /* We used to have code to avoid getting parameters moved from hard
2669 argument registers into pseudos.
2671 However, it was removed when it proved to be of marginal benefit and
2672 caused problems because of different notions of what the "head" insn
2673 was. */
2675 /* Remove certain insns at the beginning from scheduling,
2676 by advancing HEAD. */
2678 /* At the start of a function, before reload has run, don't delay getting
2679 parameters from hard registers into pseudo registers. */
2680 if (reload_completed == 0 && b == 0)
2682 while (head != tail
2683 && GET_CODE (head) == NOTE
2684 && NOTE_LINE_NUMBER (head) != NOTE_INSN_FUNCTION_BEG)
2685 head = NEXT_INSN (head);
2686 while (head != tail
2687 && GET_CODE (head) == INSN
2688 && GET_CODE (PATTERN (head)) == SET)
2690 rtx src = SET_SRC (PATTERN (head));
2691 while (GET_CODE (src) == SUBREG
2692 || GET_CODE (src) == SIGN_EXTEND
2693 || GET_CODE (src) == ZERO_EXTEND
2694 || GET_CODE (src) == SIGN_EXTRACT
2695 || GET_CODE (src) == ZERO_EXTRACT)
2696 src = XEXP (src, 0);
2697 if (GET_CODE (src) != REG
2698 || REGNO (src) >= FIRST_PSEUDO_REGISTER)
2699 break;
2700 /* Keep this insn from ever being scheduled. */
2701 INSN_REF_COUNT (head) = 1;
2702 head = NEXT_INSN (head);
2705 #endif
2707 /* Don't include any notes or labels at the beginning of the
2708 basic block, or notes at the ends of basic blocks. */
2709 while (head != tail)
2711 if (GET_CODE (head) == NOTE)
2712 head = NEXT_INSN (head);
2713 else if (GET_CODE (tail) == NOTE)
2714 tail = PREV_INSN (tail);
2715 else if (GET_CODE (head) == CODE_LABEL)
2716 head = NEXT_INSN (head);
2717 else break;
2719 /* If the only insn left is a NOTE or a CODE_LABEL, then there is no need
2720 to schedule this block. */
2721 if (head == tail
2722 && (GET_CODE (head) == NOTE || GET_CODE (head) == CODE_LABEL))
2723 goto ret;
2725 #if 0
2726 /* This short-cut doesn't work. It does not count call insns crossed by
2727 registers in reg_sometimes_live. It does not mark these registers as
2728 dead if they die in this block. It does not mark these registers live
2729 (or create new reg_sometimes_live entries if necessary) if they are born
2730 in this block.
2732 The easy solution is to just always schedule a block. This block only
2733 has one insn, so this won't slow down this pass by much. */
2735 if (head == tail)
2736 goto ret;
2737 #endif
2739 /* Now HEAD through TAIL are the insns actually to be rearranged;
2740 Let PREV_HEAD and NEXT_TAIL enclose them. */
2741 prev_head = PREV_INSN (head);
2742 next_tail = NEXT_INSN (tail);
2744 /* Initialize basic block data structures. */
2745 dead_notes = 0;
2746 pending_read_insns = 0;
2747 pending_read_mems = 0;
2748 pending_write_insns = 0;
2749 pending_write_mems = 0;
2750 pending_lists_length = 0;
2751 last_pending_memory_flush = 0;
2752 last_function_call = 0;
2753 last_scheduled_insn = 0;
2755 LOG_LINKS (sched_before_next_call) = 0;
2757 n_insns = sched_analyze (head, tail);
2758 if (n_insns == 0)
2760 free_pending_lists ();
2761 goto ret;
2764 /* Allocate vector to hold insns to be rearranged (except those
2765 insns which are controlled by an insn with SCHED_GROUP_P set).
2766 All these insns are included between ORIG_HEAD and ORIG_TAIL,
2767 as those variables ultimately are set up. */
2768 ready = (rtx *) alloca ((n_insns+1) * sizeof (rtx));
2770 /* TAIL is now the last of the insns to be rearranged.
2771 Put those insns into the READY vector. */
2772 insn = tail;
2774 /* For all branches, calls, uses, and cc0 setters, force them to remain
2775 in order at the end of the block by adding dependencies and giving
2776 the last a high priority. There may be notes present, and prev_head
2777 may also be a note.
2779 Branches must obviously remain at the end. Calls should remain at the
2780 end since moving them results in worse register allocation. Uses remain
2781 at the end to ensure proper register allocation. cc0 setters remaim
2782 at the end because they can't be moved away from their cc0 user. */
2783 last = 0;
2784 while (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
2785 || (GET_CODE (insn) == INSN
2786 && (GET_CODE (PATTERN (insn)) == USE
2787 #ifdef HAVE_cc0
2788 || sets_cc0_p (PATTERN (insn))
2789 #endif
2791 || GET_CODE (insn) == NOTE)
2793 if (GET_CODE (insn) != NOTE)
2795 priority (insn);
2796 if (last == 0)
2798 ready[n_ready++] = insn;
2799 INSN_PRIORITY (insn) = TAIL_PRIORITY - i;
2800 INSN_REF_COUNT (insn) = 0;
2802 else if (! find_insn_list (insn, LOG_LINKS (last)))
2804 add_dependence (last, insn, REG_DEP_ANTI);
2805 INSN_REF_COUNT (insn)++;
2807 last = insn;
2809 /* Skip over insns that are part of a group. */
2810 while (SCHED_GROUP_P (insn))
2812 insn = prev_nonnote_insn (insn);
2813 priority (insn);
2817 insn = PREV_INSN (insn);
2818 /* Don't overrun the bounds of the basic block. */
2819 if (insn == prev_head)
2820 break;
2823 /* Assign priorities to instructions. Also check whether they
2824 are in priority order already. If so then I will be nonnegative.
2825 We use this shortcut only before reloading. */
2826 #if 0
2827 i = reload_completed ? DONE_PRIORITY : MAX_PRIORITY;
2828 #endif
2830 for (; insn != prev_head; insn = PREV_INSN (insn))
2832 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2834 priority (insn);
2835 if (INSN_REF_COUNT (insn) == 0)
2837 if (last == 0)
2838 ready[n_ready++] = insn;
2839 else
2841 /* Make this dependent on the last of the instructions
2842 that must remain in order at the end of the block. */
2843 add_dependence (last, insn, REG_DEP_ANTI);
2844 INSN_REF_COUNT (insn) = 1;
2847 if (SCHED_GROUP_P (insn))
2849 while (SCHED_GROUP_P (insn))
2851 insn = prev_nonnote_insn (insn);
2852 priority (insn);
2854 continue;
2856 #if 0
2857 if (i < 0)
2858 continue;
2859 if (INSN_PRIORITY (insn) < i)
2860 i = INSN_PRIORITY (insn);
2861 else if (INSN_PRIORITY (insn) > i)
2862 i = DONE_PRIORITY;
2863 #endif
2867 #if 0
2868 /* This short-cut doesn't work. It does not count call insns crossed by
2869 registers in reg_sometimes_live. It does not mark these registers as
2870 dead if they die in this block. It does not mark these registers live
2871 (or create new reg_sometimes_live entries if necessary) if they are born
2872 in this block.
2874 The easy solution is to just always schedule a block. These blocks tend
2875 to be very short, so this doesn't slow down this pass by much. */
2877 /* If existing order is good, don't bother to reorder. */
2878 if (i != DONE_PRIORITY)
2880 if (file)
2881 fprintf (file, ";; already scheduled\n");
2883 if (reload_completed == 0)
2885 for (i = 0; i < sometimes_max; i++)
2886 regs_sometimes_live[i].live_length += n_insns;
2888 finish_sometimes_live (regs_sometimes_live, sometimes_max);
2890 free_pending_lists ();
2891 goto ret;
2893 #endif
2895 /* Scan all the insns to be scheduled, removing NOTE insns
2896 and register death notes.
2897 Line number NOTE insns end up in NOTE_LIST.
2898 Register death notes end up in DEAD_NOTES.
2900 Recreate the register life information for the end of this basic
2901 block. */
2903 if (reload_completed == 0)
2905 COPY_REG_SET (bb_live_regs, basic_block_live_at_start[b]);
2906 CLEAR_REG_SET (bb_dead_regs);
2908 if (b == 0)
2910 /* This is the first block in the function. There may be insns
2911 before head that we can't schedule. We still need to examine
2912 them though for accurate register lifetime analysis. */
2914 /* We don't want to remove any REG_DEAD notes as the code below
2915 does. */
2917 for (insn = basic_block_head[b]; insn != head;
2918 insn = NEXT_INSN (insn))
2919 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2921 /* See if the register gets born here. */
2922 /* We must check for registers being born before we check for
2923 registers dying. It is possible for a register to be born
2924 and die in the same insn, e.g. reading from a volatile
2925 memory location into an otherwise unused register. Such
2926 a register must be marked as dead after this insn. */
2927 if (GET_CODE (PATTERN (insn)) == SET
2928 || GET_CODE (PATTERN (insn)) == CLOBBER)
2929 sched_note_set (b, PATTERN (insn), 0);
2930 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2932 int j;
2933 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
2934 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
2935 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
2936 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
2938 /* ??? This code is obsolete and should be deleted. It
2939 is harmless though, so we will leave it in for now. */
2940 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
2941 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
2942 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
2945 /* Each call clobbers (makes live) all call-clobbered regs
2946 that are not global or fixed. Note that the function-value
2947 reg is a call_clobbered reg. */
2949 if (GET_CODE (insn) == CALL_INSN)
2951 int j;
2952 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
2953 if (call_used_regs[j] && ! global_regs[j]
2954 && ! fixed_regs[j])
2956 SET_REGNO_REG_SET (bb_live_regs, j);
2957 CLEAR_REGNO_REG_SET (bb_dead_regs, j);
2961 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2963 if ((REG_NOTE_KIND (link) == REG_DEAD
2964 || REG_NOTE_KIND (link) == REG_UNUSED)
2965 /* Verify that the REG_NOTE has a valid value. */
2966 && GET_CODE (XEXP (link, 0)) == REG)
2968 register int regno = REGNO (XEXP (link, 0));
2970 if (regno < FIRST_PSEUDO_REGISTER)
2972 int j = HARD_REGNO_NREGS (regno,
2973 GET_MODE (XEXP (link, 0)));
2974 while (--j >= 0)
2976 CLEAR_REGNO_REG_SET (bb_live_regs, regno + j);
2977 SET_REGNO_REG_SET (bb_dead_regs, regno + j);
2980 else
2982 CLEAR_REGNO_REG_SET (bb_live_regs, regno);
2983 SET_REGNO_REG_SET (bb_dead_regs, regno);
2991 /* If debugging information is being produced, keep track of the line
2992 number notes for each insn. */
2993 if (write_symbols != NO_DEBUG)
2995 /* We must use the true line number for the first insn in the block
2996 that was computed and saved at the start of this pass. We can't
2997 use the current line number, because scheduling of the previous
2998 block may have changed the current line number. */
2999 rtx line = line_note_head[b];
3001 for (insn = basic_block_head[b];
3002 insn != next_tail;
3003 insn = NEXT_INSN (insn))
3004 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
3005 line = insn;
3006 else
3007 LINE_NOTE (insn) = line;
3010 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3012 rtx prev, next, link;
3014 /* Farm out notes. This is needed to keep the debugger from
3015 getting completely deranged. */
3016 if (GET_CODE (insn) == NOTE)
3018 prev = insn;
3019 insn = unlink_notes (insn, next_tail);
3020 if (prev == tail)
3021 abort ();
3022 if (prev == head)
3023 abort ();
3024 if (insn == next_tail)
3025 abort ();
3028 if (reload_completed == 0
3029 && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3031 /* See if the register gets born here. */
3032 /* We must check for registers being born before we check for
3033 registers dying. It is possible for a register to be born and
3034 die in the same insn, e.g. reading from a volatile memory
3035 location into an otherwise unused register. Such a register
3036 must be marked as dead after this insn. */
3037 if (GET_CODE (PATTERN (insn)) == SET
3038 || GET_CODE (PATTERN (insn)) == CLOBBER)
3039 sched_note_set (b, PATTERN (insn), 0);
3040 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3042 int j;
3043 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
3044 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
3045 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
3046 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
3048 /* ??? This code is obsolete and should be deleted. It
3049 is harmless though, so we will leave it in for now. */
3050 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
3051 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
3052 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
3055 /* Each call clobbers (makes live) all call-clobbered regs that are
3056 not global or fixed. Note that the function-value reg is a
3057 call_clobbered reg. */
3059 if (GET_CODE (insn) == CALL_INSN)
3061 int j;
3062 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
3063 if (call_used_regs[j] && ! global_regs[j]
3064 && ! fixed_regs[j])
3066 SET_REGNO_REG_SET (bb_live_regs, j);
3067 CLEAR_REGNO_REG_SET (bb_dead_regs, j);
3071 /* Need to know what registers this insn kills. */
3072 for (prev = 0, link = REG_NOTES (insn); link; link = next)
3074 next = XEXP (link, 1);
3075 if ((REG_NOTE_KIND (link) == REG_DEAD
3076 || REG_NOTE_KIND (link) == REG_UNUSED)
3077 /* Verify that the REG_NOTE has a valid value. */
3078 && GET_CODE (XEXP (link, 0)) == REG)
3080 register int regno = REGNO (XEXP (link, 0));
3082 /* Only unlink REG_DEAD notes; leave REG_UNUSED notes
3083 alone. */
3084 if (REG_NOTE_KIND (link) == REG_DEAD)
3086 if (prev)
3087 XEXP (prev, 1) = next;
3088 else
3089 REG_NOTES (insn) = next;
3090 XEXP (link, 1) = dead_notes;
3091 dead_notes = link;
3093 else
3094 prev = link;
3096 if (regno < FIRST_PSEUDO_REGISTER)
3098 int j = HARD_REGNO_NREGS (regno,
3099 GET_MODE (XEXP (link, 0)));
3100 while (--j >= 0)
3102 CLEAR_REGNO_REG_SET (bb_live_regs, regno + j);
3103 SET_REGNO_REG_SET (bb_dead_regs, regno + j);
3106 else
3108 CLEAR_REGNO_REG_SET (bb_live_regs, regno);
3109 SET_REGNO_REG_SET (bb_dead_regs, regno);
3112 else
3113 prev = link;
3118 if (reload_completed == 0)
3120 /* Keep track of register lives. */
3121 old_live_regs = ALLOCA_REG_SET ();
3122 regs_sometimes_live
3123 = (struct sometimes *) alloca (max_regno * sizeof (struct sometimes));
3124 sometimes_max = 0;
3126 /* Start with registers live at end. */
3127 COPY_REG_SET (old_live_regs, bb_live_regs);
3128 EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, 0, j,
3130 sometimes_max
3131 = new_sometimes_live (regs_sometimes_live,
3132 j, sometimes_max);
3136 SCHED_SORT (ready, n_ready, 1);
3138 if (file)
3140 fprintf (file, ";; ready list initially:\n;; ");
3141 for (i = 0; i < n_ready; i++)
3142 fprintf (file, "%d ", INSN_UID (ready[i]));
3143 fprintf (file, "\n\n");
3145 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3146 if (INSN_PRIORITY (insn) > 0)
3147 fprintf (file, ";; insn[%4d]: priority = %4d, ref_count = %4d\n",
3148 INSN_UID (insn), INSN_PRIORITY (insn),
3149 INSN_REF_COUNT (insn));
3152 /* Now HEAD and TAIL are going to become disconnected
3153 entirely from the insn chain. */
3154 tail = 0;
3156 /* Q_SIZE will always be zero here. */
3157 q_ptr = 0; clock = 0;
3158 bzero ((char *) insn_queue, sizeof (insn_queue));
3160 /* Now, perform list scheduling. */
3162 /* Where we start inserting insns is after TAIL. */
3163 last = next_tail;
3165 new_needs = (NEXT_INSN (prev_head) == basic_block_head[b]
3166 ? NEED_HEAD : NEED_NOTHING);
3167 if (PREV_INSN (next_tail) == basic_block_end[b])
3168 new_needs |= NEED_TAIL;
3170 new_ready = n_ready;
3171 while (sched_n_insns < n_insns)
3173 q_ptr = NEXT_Q (q_ptr); clock++;
3175 /* Add all pending insns that can be scheduled without stalls to the
3176 ready list. */
3177 for (insn = insn_queue[q_ptr]; insn; insn = NEXT_INSN (insn))
3179 if (file)
3180 fprintf (file, ";; launching %d before %d with no stalls at T-%d\n",
3181 INSN_UID (insn), INSN_UID (last), clock);
3182 ready[new_ready++] = insn;
3183 q_size -= 1;
3185 insn_queue[q_ptr] = 0;
3187 /* If there are no ready insns, stall until one is ready and add all
3188 of the pending insns at that point to the ready list. */
3189 if (new_ready == 0)
3191 register int stalls;
3193 for (stalls = 1; stalls < INSN_QUEUE_SIZE; stalls++)
3194 if ((insn = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
3196 for (; insn; insn = NEXT_INSN (insn))
3198 if (file)
3199 fprintf (file, ";; launching %d before %d with %d stalls at T-%d\n",
3200 INSN_UID (insn), INSN_UID (last), stalls, clock);
3201 ready[new_ready++] = insn;
3202 q_size -= 1;
3204 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
3205 break;
3208 q_ptr = NEXT_Q_AFTER (q_ptr, stalls); clock += stalls;
3211 /* There should be some instructions waiting to fire. */
3212 if (new_ready == 0)
3213 abort ();
3215 if (file)
3217 fprintf (file, ";; ready list at T-%d:", clock);
3218 for (i = 0; i < new_ready; i++)
3219 fprintf (file, " %d (%x)",
3220 INSN_UID (ready[i]), INSN_PRIORITY (ready[i]));
3223 /* Sort the ready list and choose the best insn to schedule. Select
3224 which insn should issue in this cycle and queue those that are
3225 blocked by function unit hazards.
3227 N_READY holds the number of items that were scheduled the last time,
3228 minus the one instruction scheduled on the last loop iteration; it
3229 is not modified for any other reason in this loop. */
3231 SCHED_SORT (ready, new_ready, n_ready);
3232 if (MAX_BLOCKAGE > 1)
3234 new_ready = schedule_select (ready, new_ready, clock, file);
3235 if (new_ready == 0)
3237 if (file)
3238 fprintf (file, "\n");
3239 /* We must set n_ready here, to ensure that sorting always
3240 occurs when we come back to the SCHED_SORT line above. */
3241 n_ready = 0;
3242 continue;
3245 n_ready = new_ready;
3246 last_scheduled_insn = insn = ready[0];
3248 /* The first insn scheduled becomes the new tail. */
3249 if (tail == 0)
3250 tail = insn;
3252 if (file)
3254 fprintf (file, ", now");
3255 for (i = 0; i < n_ready; i++)
3256 fprintf (file, " %d", INSN_UID (ready[i]));
3257 fprintf (file, "\n");
3260 if (DONE_PRIORITY_P (insn))
3261 abort ();
3263 if (reload_completed == 0)
3265 /* Process this insn, and each insn linked to this one which must
3266 be immediately output after this insn. */
3269 /* First we kill registers set by this insn, and then we
3270 make registers used by this insn live. This is the opposite
3271 order used above because we are traversing the instructions
3272 backwards. */
3274 /* Strictly speaking, we should scan REG_UNUSED notes and make
3275 every register mentioned there live, however, we will just
3276 kill them again immediately below, so there doesn't seem to
3277 be any reason why we bother to do this. */
3279 /* See if this is the last notice we must take of a register. */
3280 if (GET_CODE (PATTERN (insn)) == SET
3281 || GET_CODE (PATTERN (insn)) == CLOBBER)
3282 sched_note_set (b, PATTERN (insn), 1);
3283 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3285 int j;
3286 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
3287 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
3288 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
3289 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 1);
3292 /* This code keeps life analysis information up to date. */
3293 if (GET_CODE (insn) == CALL_INSN)
3295 register struct sometimes *p;
3297 /* A call kills all call used registers that are not
3298 global or fixed, except for those mentioned in the call
3299 pattern which will be made live again later. */
3300 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3301 if (call_used_regs[i] && ! global_regs[i]
3302 && ! fixed_regs[i])
3304 CLEAR_REGNO_REG_SET (bb_live_regs, i);
3305 SET_REGNO_REG_SET (bb_dead_regs, i);
3308 /* Regs live at the time of a call instruction must not
3309 go in a register clobbered by calls. Record this for
3310 all regs now live. Note that insns which are born or
3311 die in a call do not cross a call, so this must be done
3312 after the killings (above) and before the births
3313 (below). */
3314 p = regs_sometimes_live;
3315 for (i = 0; i < sometimes_max; i++, p++)
3316 if (REGNO_REG_SET_P (bb_live_regs, p->regno))
3317 p->calls_crossed += 1;
3320 /* Make every register used live, and add REG_DEAD notes for
3321 registers which were not live before we started. */
3322 attach_deaths_insn (insn);
3324 /* Find registers now made live by that instruction. */
3325 EXECUTE_IF_AND_COMPL_IN_REG_SET (bb_live_regs, old_live_regs, 0, i,
3327 sometimes_max
3328 = new_sometimes_live (regs_sometimes_live,
3329 i, sometimes_max);
3331 IOR_REG_SET (old_live_regs, bb_live_regs);
3333 /* Count lengths of all regs we are worrying about now,
3334 and handle registers no longer live. */
3336 for (i = 0; i < sometimes_max; i++)
3338 register struct sometimes *p = &regs_sometimes_live[i];
3339 int regno = p->regno;
3341 p->live_length += 1;
3343 if (!REGNO_REG_SET_P (bb_live_regs, p->regno))
3345 /* This is the end of one of this register's lifetime
3346 segments. Save the lifetime info collected so far,
3347 and clear its bit in the old_live_regs entry. */
3348 sched_reg_live_length[regno] += p->live_length;
3349 sched_reg_n_calls_crossed[regno] += p->calls_crossed;
3350 CLEAR_REGNO_REG_SET (old_live_regs, p->regno);
3352 /* Delete the reg_sometimes_live entry for this reg by
3353 copying the last entry over top of it. */
3354 *p = regs_sometimes_live[--sometimes_max];
3355 /* ...and decrement i so that this newly copied entry
3356 will be processed. */
3357 i--;
3361 link = insn;
3362 insn = PREV_INSN (insn);
3364 while (SCHED_GROUP_P (link));
3366 /* Set INSN back to the insn we are scheduling now. */
3367 insn = ready[0];
3370 /* Schedule INSN. Remove it from the ready list. */
3371 ready += 1;
3372 n_ready -= 1;
3374 sched_n_insns += 1;
3375 NEXT_INSN (insn) = last;
3376 PREV_INSN (last) = insn;
3378 /* Everything that precedes INSN now either becomes "ready", if
3379 it can execute immediately before INSN, or "pending", if
3380 there must be a delay. Give INSN high enough priority that
3381 at least one (maybe more) reg-killing insns can be launched
3382 ahead of all others. Mark INSN as scheduled by changing its
3383 priority to -1. */
3384 INSN_PRIORITY (insn) = LAUNCH_PRIORITY;
3385 new_ready = schedule_insn (insn, ready, n_ready, clock);
3386 INSN_PRIORITY (insn) = DONE_PRIORITY;
3388 /* Schedule all prior insns that must not be moved. */
3389 if (SCHED_GROUP_P (insn))
3391 /* Disable these insns from being launched, in case one of the
3392 insns in the group has a dependency on an earlier one. */
3393 link = insn;
3394 while (SCHED_GROUP_P (link))
3396 /* Disable these insns from being launched by anybody. */
3397 link = PREV_INSN (link);
3398 INSN_REF_COUNT (link) = 0;
3401 /* Now handle each group insn like the main insn was handled
3402 above. */
3403 link = insn;
3404 while (SCHED_GROUP_P (link))
3406 link = PREV_INSN (link);
3408 sched_n_insns += 1;
3410 /* ??? Why don't we set LAUNCH_PRIORITY here? */
3411 new_ready = schedule_insn (link, ready, new_ready, clock);
3412 INSN_PRIORITY (link) = DONE_PRIORITY;
3416 /* Put back NOTE_INSN_SETJMP,
3417 NOTE_INSN_{LOOP,EHREGION}_{BEGIN,END} notes. */
3419 /* To prime the loop. We need to handle INSN and all the insns in the
3420 sched group. */
3421 last = NEXT_INSN (insn);
3424 insn = PREV_INSN (last);
3426 /* Maintain a valid chain so emit_note_before works.
3427 This is necessary because PREV_INSN (insn) isn't valid
3428 (if ! SCHED_GROUP_P) and if it points to an insn already
3429 scheduled, a circularity will result. */
3430 if (! SCHED_GROUP_P (insn))
3432 NEXT_INSN (prev_head) = insn;
3433 PREV_INSN (insn) = prev_head;
3436 last = reemit_notes (insn, insn);
3438 while (SCHED_GROUP_P (insn));
3440 if (q_size != 0)
3441 abort ();
3443 if (reload_completed == 0)
3444 finish_sometimes_live (regs_sometimes_live, sometimes_max);
3446 /* HEAD is now the first insn in the chain of insns that
3447 been scheduled by the loop above.
3448 TAIL is the last of those insns. */
3449 head = last;
3451 /* NOTE_LIST is the end of a chain of notes previously found
3452 among the insns. Insert them at the beginning of the insns. */
3453 if (note_list != 0)
3455 rtx note_head = note_list;
3456 while (PREV_INSN (note_head))
3457 note_head = PREV_INSN (note_head);
3459 PREV_INSN (head) = note_list;
3460 NEXT_INSN (note_list) = head;
3461 head = note_head;
3464 /* There should be no REG_DEAD notes leftover at the end.
3465 In practice, this can occur as the result of bugs in flow, combine.c,
3466 and/or sched.c. The values of the REG_DEAD notes remaining are
3467 meaningless, because dead_notes is just used as a free list. */
3468 #if 1
3469 if (dead_notes != 0)
3470 abort ();
3471 #endif
3473 if (new_needs & NEED_HEAD)
3474 basic_block_head[b] = head;
3475 PREV_INSN (head) = prev_head;
3476 NEXT_INSN (prev_head) = head;
3478 if (new_needs & NEED_TAIL)
3479 basic_block_end[b] = tail;
3480 NEXT_INSN (tail) = next_tail;
3481 PREV_INSN (next_tail) = tail;
3483 /* Restore the line-number notes of each insn. */
3484 if (write_symbols != NO_DEBUG)
3486 rtx line, note, prev, new;
3487 int notes = 0;
3489 head = basic_block_head[b];
3490 next_tail = NEXT_INSN (basic_block_end[b]);
3492 /* Determine the current line-number. We want to know the current
3493 line number of the first insn of the block here, in case it is
3494 different from the true line number that was saved earlier. If
3495 different, then we need a line number note before the first insn
3496 of this block. If it happens to be the same, then we don't want to
3497 emit another line number note here. */
3498 for (line = head; line; line = PREV_INSN (line))
3499 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
3500 break;
3502 /* Walk the insns keeping track of the current line-number and inserting
3503 the line-number notes as needed. */
3504 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
3505 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
3506 line = insn;
3507 /* This used to emit line number notes before every non-deleted note.
3508 However, this confuses a debugger, because line notes not separated
3509 by real instructions all end up at the same address. I can find no
3510 use for line number notes before other notes, so none are emitted. */
3511 else if (GET_CODE (insn) != NOTE
3512 && (note = LINE_NOTE (insn)) != 0
3513 && note != line
3514 && (line == 0
3515 || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
3516 || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
3518 line = note;
3519 prev = PREV_INSN (insn);
3520 if (LINE_NOTE (note))
3522 /* Re-use the original line-number note. */
3523 LINE_NOTE (note) = 0;
3524 PREV_INSN (note) = prev;
3525 NEXT_INSN (prev) = note;
3526 PREV_INSN (insn) = note;
3527 NEXT_INSN (note) = insn;
3529 else
3531 notes++;
3532 new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
3533 NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
3534 RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
3537 if (file && notes)
3538 fprintf (file, ";; added %d line-number notes\n", notes);
3541 if (file)
3543 fprintf (file, ";; total time = %d\n;; new basic block head = %d\n;; new basic block end = %d\n\n",
3544 clock, INSN_UID (basic_block_head[b]), INSN_UID (basic_block_end[b]));
3547 /* Yow! We're done! */
3548 free_pending_lists ();
3550 ret:
3551 FREE_REG_SET (reg_pending_sets);
3552 FREE_REG_SET (old_live_regs);
3554 return;
3557 /* Subroutine of split_hard_reg_notes. Searches X for any reference to
3558 REGNO, returning the rtx of the reference found if any. Otherwise,
3559 returns 0. */
3561 static rtx
3562 regno_use_in (regno, x)
3563 int regno;
3564 rtx x;
3566 register char *fmt;
3567 int i, j;
3568 rtx tem;
3570 if (GET_CODE (x) == REG && REGNO (x) == regno)
3571 return x;
3573 fmt = GET_RTX_FORMAT (GET_CODE (x));
3574 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3576 if (fmt[i] == 'e')
3578 if ((tem = regno_use_in (regno, XEXP (x, i))))
3579 return tem;
3581 else if (fmt[i] == 'E')
3582 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3583 if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
3584 return tem;
3587 return 0;
3590 /* Subroutine of update_flow_info. Determines whether any new REG_NOTEs are
3591 needed for the hard register mentioned in the note. This can happen
3592 if the reference to the hard register in the original insn was split into
3593 several smaller hard register references in the split insns. */
3595 static void
3596 split_hard_reg_notes (note, first, last, orig_insn)
3597 rtx note, first, last, orig_insn;
3599 rtx reg, temp, link;
3600 int n_regs, i, new_reg;
3601 rtx insn;
3603 /* Assume that this is a REG_DEAD note. */
3604 if (REG_NOTE_KIND (note) != REG_DEAD)
3605 abort ();
3607 reg = XEXP (note, 0);
3609 n_regs = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
3611 for (i = 0; i < n_regs; i++)
3613 new_reg = REGNO (reg) + i;
3615 /* Check for references to new_reg in the split insns. */
3616 for (insn = last; ; insn = PREV_INSN (insn))
3618 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
3619 && (temp = regno_use_in (new_reg, PATTERN (insn))))
3621 /* Create a new reg dead note here. */
3622 link = rtx_alloc (EXPR_LIST);
3623 PUT_REG_NOTE_KIND (link, REG_DEAD);
3624 XEXP (link, 0) = temp;
3625 XEXP (link, 1) = REG_NOTES (insn);
3626 REG_NOTES (insn) = link;
3628 /* If killed multiple registers here, then add in the excess. */
3629 i += HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) - 1;
3631 break;
3633 /* It isn't mentioned anywhere, so no new reg note is needed for
3634 this register. */
3635 if (insn == first)
3636 break;
3641 /* Subroutine of update_flow_info. Determines whether a SET or CLOBBER in an
3642 insn created by splitting needs a REG_DEAD or REG_UNUSED note added. */
3644 static void
3645 new_insn_dead_notes (pat, insn, last, orig_insn)
3646 rtx pat, insn, last, orig_insn;
3648 rtx dest, tem, set;
3650 /* PAT is either a CLOBBER or a SET here. */
3651 dest = XEXP (pat, 0);
3653 while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
3654 || GET_CODE (dest) == STRICT_LOW_PART
3655 || GET_CODE (dest) == SIGN_EXTRACT)
3656 dest = XEXP (dest, 0);
3658 if (GET_CODE (dest) == REG)
3660 /* If the original insn already used this register, we may not add new
3661 notes for it. One example for a split that needs this test is
3662 when a multi-word memory access with register-indirect addressing
3663 is split into multiple memory accesses with auto-increment and
3664 one adjusting add instruction for the address register. */
3665 if (reg_referenced_p (dest, PATTERN (orig_insn)))
3666 return;
3667 for (tem = last; tem != insn; tem = PREV_INSN (tem))
3669 if (GET_RTX_CLASS (GET_CODE (tem)) == 'i'
3670 && reg_overlap_mentioned_p (dest, PATTERN (tem))
3671 && (set = single_set (tem)))
3673 rtx tem_dest = SET_DEST (set);
3675 while (GET_CODE (tem_dest) == ZERO_EXTRACT
3676 || GET_CODE (tem_dest) == SUBREG
3677 || GET_CODE (tem_dest) == STRICT_LOW_PART
3678 || GET_CODE (tem_dest) == SIGN_EXTRACT)
3679 tem_dest = XEXP (tem_dest, 0);
3681 if (! rtx_equal_p (tem_dest, dest))
3683 /* Use the same scheme as combine.c, don't put both REG_DEAD
3684 and REG_UNUSED notes on the same insn. */
3685 if (! find_regno_note (tem, REG_UNUSED, REGNO (dest))
3686 && ! find_regno_note (tem, REG_DEAD, REGNO (dest)))
3688 rtx note = rtx_alloc (EXPR_LIST);
3689 PUT_REG_NOTE_KIND (note, REG_DEAD);
3690 XEXP (note, 0) = dest;
3691 XEXP (note, 1) = REG_NOTES (tem);
3692 REG_NOTES (tem) = note;
3694 /* The reg only dies in one insn, the last one that uses
3695 it. */
3696 break;
3698 else if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
3699 /* We found an instruction that both uses the register,
3700 and sets it, so no new REG_NOTE is needed for this set. */
3701 break;
3704 /* If this is a set, it must die somewhere, unless it is the dest of
3705 the original insn, and hence is live after the original insn. Abort
3706 if it isn't supposed to be live after the original insn.
3708 If this is a clobber, then just add a REG_UNUSED note. */
3709 if (tem == insn)
3711 int live_after_orig_insn = 0;
3712 rtx pattern = PATTERN (orig_insn);
3713 int i;
3715 if (GET_CODE (pat) == CLOBBER)
3717 rtx note = rtx_alloc (EXPR_LIST);
3718 PUT_REG_NOTE_KIND (note, REG_UNUSED);
3719 XEXP (note, 0) = dest;
3720 XEXP (note, 1) = REG_NOTES (insn);
3721 REG_NOTES (insn) = note;
3722 return;
3725 /* The original insn could have multiple sets, so search the
3726 insn for all sets. */
3727 if (GET_CODE (pattern) == SET)
3729 if (reg_overlap_mentioned_p (dest, SET_DEST (pattern)))
3730 live_after_orig_insn = 1;
3732 else if (GET_CODE (pattern) == PARALLEL)
3734 for (i = 0; i < XVECLEN (pattern, 0); i++)
3735 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET
3736 && reg_overlap_mentioned_p (dest,
3737 SET_DEST (XVECEXP (pattern,
3738 0, i))))
3739 live_after_orig_insn = 1;
3742 if (! live_after_orig_insn)
3743 abort ();
3748 /* Subroutine of update_flow_info. Update the value of reg_n_sets for all
3749 registers modified by X. INC is -1 if the containing insn is being deleted,
3750 and is 1 if the containing insn is a newly generated insn. */
3752 static void
3753 update_n_sets (x, inc)
3754 rtx x;
3755 int inc;
3757 rtx dest = SET_DEST (x);
3759 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
3760 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
3761 dest = SUBREG_REG (dest);
3763 if (GET_CODE (dest) == REG)
3765 int regno = REGNO (dest);
3767 if (regno < FIRST_PSEUDO_REGISTER)
3769 register int i;
3770 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (dest));
3772 for (i = regno; i < endregno; i++)
3773 REG_N_SETS (i) += inc;
3775 else
3776 REG_N_SETS (regno) += inc;
3780 /* Updates all flow-analysis related quantities (including REG_NOTES) for
3781 the insns from FIRST to LAST inclusive that were created by splitting
3782 ORIG_INSN. NOTES are the original REG_NOTES. */
3784 static void
3785 update_flow_info (notes, first, last, orig_insn)
3786 rtx notes;
3787 rtx first, last;
3788 rtx orig_insn;
3790 rtx insn, note;
3791 rtx next;
3792 rtx orig_dest, temp;
3793 rtx set;
3795 /* Get and save the destination set by the original insn. */
3797 orig_dest = single_set (orig_insn);
3798 if (orig_dest)
3799 orig_dest = SET_DEST (orig_dest);
3801 /* Move REG_NOTES from the original insn to where they now belong. */
3803 for (note = notes; note; note = next)
3805 next = XEXP (note, 1);
3806 switch (REG_NOTE_KIND (note))
3808 case REG_DEAD:
3809 case REG_UNUSED:
3810 /* Move these notes from the original insn to the last new insn where
3811 the register is now set. */
3813 for (insn = last; ; insn = PREV_INSN (insn))
3815 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
3816 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
3818 /* If this note refers to a multiple word hard register, it
3819 may have been split into several smaller hard register
3820 references, so handle it specially. */
3821 temp = XEXP (note, 0);
3822 if (REG_NOTE_KIND (note) == REG_DEAD
3823 && GET_CODE (temp) == REG
3824 && REGNO (temp) < FIRST_PSEUDO_REGISTER
3825 && HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) > 1)
3826 split_hard_reg_notes (note, first, last, orig_insn);
3827 else
3829 XEXP (note, 1) = REG_NOTES (insn);
3830 REG_NOTES (insn) = note;
3833 /* Sometimes need to convert REG_UNUSED notes to REG_DEAD
3834 notes. */
3835 /* ??? This won't handle multiple word registers correctly,
3836 but should be good enough for now. */
3837 if (REG_NOTE_KIND (note) == REG_UNUSED
3838 && GET_CODE (XEXP (note, 0)) != SCRATCH
3839 && ! dead_or_set_p (insn, XEXP (note, 0)))
3840 PUT_REG_NOTE_KIND (note, REG_DEAD);
3842 /* The reg only dies in one insn, the last one that uses
3843 it. */
3844 break;
3846 /* It must die somewhere, fail it we couldn't find where it died.
3848 If this is a REG_UNUSED note, then it must be a temporary
3849 register that was not needed by this instantiation of the
3850 pattern, so we can safely ignore it. */
3851 if (insn == first)
3853 /* After reload, REG_DEAD notes come sometimes an
3854 instruction after the register actually dies. */
3855 if (reload_completed && REG_NOTE_KIND (note) == REG_DEAD)
3857 XEXP (note, 1) = REG_NOTES (insn);
3858 REG_NOTES (insn) = note;
3859 break;
3862 if (REG_NOTE_KIND (note) != REG_UNUSED)
3863 abort ();
3865 break;
3868 break;
3870 case REG_WAS_0:
3871 /* If the insn that set the register to 0 was deleted, this
3872 note cannot be relied on any longer. The destination might
3873 even have been moved to memory.
3874 This was observed for SH4 with execute/920501-6.c compilation,
3875 -O2 -fomit-frame-pointer -finline-functions . */
3876 if (GET_CODE (XEXP (note, 0)) == NOTE
3877 || INSN_DELETED_P (XEXP (note, 0)))
3878 break;
3879 /* This note applies to the dest of the original insn. Find the
3880 first new insn that now has the same dest, and move the note
3881 there. */
3883 if (! orig_dest)
3884 abort ();
3886 for (insn = first; ; insn = NEXT_INSN (insn))
3888 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
3889 && (temp = single_set (insn))
3890 && rtx_equal_p (SET_DEST (temp), orig_dest))
3892 XEXP (note, 1) = REG_NOTES (insn);
3893 REG_NOTES (insn) = note;
3894 /* The reg is only zero before one insn, the first that
3895 uses it. */
3896 break;
3898 /* If this note refers to a multiple word hard
3899 register, it may have been split into several smaller
3900 hard register references. We could split the notes,
3901 but simply dropping them is good enough. */
3902 if (GET_CODE (orig_dest) == REG
3903 && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
3904 && HARD_REGNO_NREGS (REGNO (orig_dest),
3905 GET_MODE (orig_dest)) > 1)
3906 break;
3907 /* It must be set somewhere, fail if we couldn't find where it
3908 was set. */
3909 if (insn == last)
3910 abort ();
3912 break;
3914 case REG_EQUAL:
3915 case REG_EQUIV:
3916 /* A REG_EQUIV or REG_EQUAL note on an insn with more than one
3917 set is meaningless. Just drop the note. */
3918 if (! orig_dest)
3919 break;
3921 case REG_NO_CONFLICT:
3922 /* These notes apply to the dest of the original insn. Find the last
3923 new insn that now has the same dest, and move the note there. */
3925 if (! orig_dest)
3926 abort ();
3928 for (insn = last; ; insn = PREV_INSN (insn))
3930 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
3931 && (temp = single_set (insn))
3932 && rtx_equal_p (SET_DEST (temp), orig_dest))
3934 XEXP (note, 1) = REG_NOTES (insn);
3935 REG_NOTES (insn) = note;
3936 /* Only put this note on one of the new insns. */
3937 break;
3940 /* The original dest must still be set someplace. Abort if we
3941 couldn't find it. */
3942 if (insn == first)
3944 /* However, if this note refers to a multiple word hard
3945 register, it may have been split into several smaller
3946 hard register references. We could split the notes,
3947 but simply dropping them is good enough. */
3948 if (GET_CODE (orig_dest) == REG
3949 && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
3950 && HARD_REGNO_NREGS (REGNO (orig_dest),
3951 GET_MODE (orig_dest)) > 1)
3952 break;
3953 /* Likewise for multi-word memory references. */
3954 if (GET_CODE (orig_dest) == MEM
3955 && SIZE_FOR_MODE (orig_dest) > MOVE_MAX)
3956 break;
3957 abort ();
3960 break;
3962 case REG_LIBCALL:
3963 /* Move a REG_LIBCALL note to the first insn created, and update
3964 the corresponding REG_RETVAL note. */
3965 XEXP (note, 1) = REG_NOTES (first);
3966 REG_NOTES (first) = note;
3968 insn = XEXP (note, 0);
3969 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
3970 if (note)
3971 XEXP (note, 0) = first;
3972 break;
3974 case REG_EXEC_COUNT:
3975 /* Move a REG_EXEC_COUNT note to the first insn created. */
3976 XEXP (note, 1) = REG_NOTES (first);
3977 REG_NOTES (first) = note;
3978 break;
3980 case REG_RETVAL:
3981 /* Move a REG_RETVAL note to the last insn created, and update
3982 the corresponding REG_LIBCALL note. */
3983 XEXP (note, 1) = REG_NOTES (last);
3984 REG_NOTES (last) = note;
3986 insn = XEXP (note, 0);
3987 note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
3988 if (note)
3989 XEXP (note, 0) = last;
3990 break;
3992 case REG_NONNEG:
3993 case REG_BR_PROB:
3994 /* This should be moved to whichever instruction is a JUMP_INSN. */
3996 for (insn = last; ; insn = PREV_INSN (insn))
3998 if (GET_CODE (insn) == JUMP_INSN)
4000 XEXP (note, 1) = REG_NOTES (insn);
4001 REG_NOTES (insn) = note;
4002 /* Only put this note on one of the new insns. */
4003 break;
4005 /* Fail if we couldn't find a JUMP_INSN. */
4006 if (insn == first)
4007 abort ();
4009 break;
4011 case REG_INC:
4012 /* reload sometimes leaves obsolete REG_INC notes around. */
4013 if (reload_completed)
4014 break;
4015 /* This should be moved to whichever instruction now has the
4016 increment operation. */
4017 abort ();
4019 case REG_LABEL:
4020 /* Should be moved to the new insn(s) which use the label. */
4021 for (insn = first; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
4022 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
4023 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
4024 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL,
4025 XEXP (note, 0),
4026 REG_NOTES (insn));
4027 break;
4029 case REG_CC_SETTER:
4030 case REG_CC_USER:
4031 /* These two notes will never appear until after reorg, so we don't
4032 have to handle them here. */
4033 default:
4034 abort ();
4038 /* Each new insn created, except the last, has a new set. If the destination
4039 is a register, then this reg is now live across several insns, whereas
4040 previously the dest reg was born and died within the same insn. To
4041 reflect this, we now need a REG_DEAD note on the insn where this
4042 dest reg dies.
4044 Similarly, the new insns may have clobbers that need REG_UNUSED notes. */
4046 for (insn = first; insn != last; insn = NEXT_INSN (insn))
4048 rtx pat;
4049 int i;
4051 pat = PATTERN (insn);
4052 if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
4053 new_insn_dead_notes (pat, insn, last, orig_insn);
4054 else if (GET_CODE (pat) == PARALLEL)
4056 for (i = 0; i < XVECLEN (pat, 0); i++)
4057 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
4058 || GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER)
4059 new_insn_dead_notes (XVECEXP (pat, 0, i), insn, last, orig_insn);
4063 /* If any insn, except the last, uses the register set by the last insn,
4064 then we need a new REG_DEAD note on that insn. In this case, there
4065 would not have been a REG_DEAD note for this register in the original
4066 insn because it was used and set within one insn. */
4068 set = single_set (last);
4069 if (set)
4071 rtx dest = SET_DEST (set);
4073 while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
4074 || GET_CODE (dest) == STRICT_LOW_PART
4075 || GET_CODE (dest) == SIGN_EXTRACT)
4076 dest = XEXP (dest, 0);
4078 if (GET_CODE (dest) == REG
4079 /* Global registers are always live, so the code below does not
4080 apply to them. */
4081 && (REGNO (dest) >= FIRST_PSEUDO_REGISTER
4082 || ! global_regs[REGNO (dest)]))
4084 rtx stop_insn = PREV_INSN (first);
4086 /* If the last insn uses the register that it is setting, then
4087 we don't want to put a REG_DEAD note there. Search backwards
4088 to find the first insn that sets but does not use DEST. */
4090 insn = last;
4091 if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
4093 for (insn = PREV_INSN (insn); insn != first;
4094 insn = PREV_INSN (insn))
4096 if ((set = single_set (insn))
4097 && reg_mentioned_p (dest, SET_DEST (set))
4098 && ! reg_overlap_mentioned_p (dest, SET_SRC (set)))
4099 break;
4103 /* Now find the first insn that uses but does not set DEST. */
4105 for (insn = PREV_INSN (insn); insn != stop_insn;
4106 insn = PREV_INSN (insn))
4108 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
4109 && reg_mentioned_p (dest, PATTERN (insn))
4110 && (set = single_set (insn)))
4112 rtx insn_dest = SET_DEST (set);
4114 while (GET_CODE (insn_dest) == ZERO_EXTRACT
4115 || GET_CODE (insn_dest) == SUBREG
4116 || GET_CODE (insn_dest) == STRICT_LOW_PART
4117 || GET_CODE (insn_dest) == SIGN_EXTRACT)
4118 insn_dest = XEXP (insn_dest, 0);
4120 if (insn_dest != dest)
4122 note = rtx_alloc (EXPR_LIST);
4123 PUT_REG_NOTE_KIND (note, REG_DEAD);
4124 XEXP (note, 0) = dest;
4125 XEXP (note, 1) = REG_NOTES (insn);
4126 REG_NOTES (insn) = note;
4127 /* The reg only dies in one insn, the last one
4128 that uses it. */
4129 break;
4136 /* If the original dest is modifying a multiple register target, and the
4137 original instruction was split such that the original dest is now set
4138 by two or more SUBREG sets, then the split insns no longer kill the
4139 destination of the original insn.
4141 In this case, if there exists an instruction in the same basic block,
4142 before the split insn, which uses the original dest, and this use is
4143 killed by the original insn, then we must remove the REG_DEAD note on
4144 this insn, because it is now superfluous.
4146 This does not apply when a hard register gets split, because the code
4147 knows how to handle overlapping hard registers properly. */
4148 if (orig_dest && GET_CODE (orig_dest) == REG)
4150 int found_orig_dest = 0;
4151 int found_split_dest = 0;
4153 for (insn = first; ; insn = NEXT_INSN (insn))
4155 rtx pat = PATTERN (insn);
4156 int i = GET_CODE (pat) == PARALLEL ? XVECLEN (pat, 0) : 0;
4157 set = pat;
4158 for (;;)
4160 if (GET_CODE (set) == SET)
4162 if (GET_CODE (SET_DEST (set)) == REG
4163 && REGNO (SET_DEST (set)) == REGNO (orig_dest))
4165 found_orig_dest = 1;
4166 break;
4168 else if (GET_CODE (SET_DEST (set)) == SUBREG
4169 && SUBREG_REG (SET_DEST (set)) == orig_dest)
4171 found_split_dest = 1;
4172 break;
4175 if (--i < 0)
4176 break;
4177 set = XVECEXP (pat, 0, i);
4180 if (insn == last)
4181 break;
4184 if (found_split_dest)
4186 /* Search backwards from FIRST, looking for the first insn that uses
4187 the original dest. Stop if we pass a CODE_LABEL or a JUMP_INSN.
4188 If we find an insn, and it has a REG_DEAD note, then delete the
4189 note. */
4191 for (insn = first; insn; insn = PREV_INSN (insn))
4193 if (GET_CODE (insn) == CODE_LABEL
4194 || GET_CODE (insn) == JUMP_INSN)
4195 break;
4196 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
4197 && reg_mentioned_p (orig_dest, insn))
4199 note = find_regno_note (insn, REG_DEAD, REGNO (orig_dest));
4200 if (note)
4201 remove_note (insn, note);
4205 else if (! found_orig_dest)
4207 /* This should never happen. */
4208 abort ();
4212 /* Update reg_n_sets. This is necessary to prevent local alloc from
4213 converting REG_EQUAL notes to REG_EQUIV when splitting has modified
4214 a reg from set once to set multiple times. */
4217 rtx x = PATTERN (orig_insn);
4218 RTX_CODE code = GET_CODE (x);
4220 if (code == SET || code == CLOBBER)
4221 update_n_sets (x, -1);
4222 else if (code == PARALLEL)
4224 int i;
4225 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4227 code = GET_CODE (XVECEXP (x, 0, i));
4228 if (code == SET || code == CLOBBER)
4229 update_n_sets (XVECEXP (x, 0, i), -1);
4233 for (insn = first; ; insn = NEXT_INSN (insn))
4235 x = PATTERN (insn);
4236 code = GET_CODE (x);
4238 if (code == SET || code == CLOBBER)
4239 update_n_sets (x, 1);
4240 else if (code == PARALLEL)
4242 int i;
4243 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4245 code = GET_CODE (XVECEXP (x, 0, i));
4246 if (code == SET || code == CLOBBER)
4247 update_n_sets (XVECEXP (x, 0, i), 1);
4251 if (insn == last)
4252 break;
4257 /* The one entry point in this file. DUMP_FILE is the dump file for
4258 this pass. */
4260 void
4261 schedule_insns (dump_file)
4262 FILE *dump_file;
4264 int max_uid = MAX_INSNS_PER_SPLIT * (get_max_uid () + 1);
4265 int b;
4266 rtx insn;
4268 /* Taking care of this degenerate case makes the rest of
4269 this code simpler. */
4270 if (n_basic_blocks == 0)
4271 return;
4273 /* Create an insn here so that we can hang dependencies off of it later. */
4274 sched_before_next_call
4275 = gen_rtx_INSN (VOIDmode, 0, NULL_RTX, NULL_RTX,
4276 NULL_RTX, 0, NULL_RTX, NULL_RTX);
4278 /* Initialize the unused_*_lists. We can't use the ones left over from
4279 the previous function, because gcc has freed that memory. We can use
4280 the ones left over from the first sched pass in the second pass however,
4281 so only clear them on the first sched pass. The first pass is before
4282 reload if flag_schedule_insns is set, otherwise it is afterwards. */
4284 if (reload_completed == 0 || ! flag_schedule_insns)
4286 unused_insn_list = 0;
4287 unused_expr_list = 0;
4290 /* We create no insns here, only reorder them, so we
4291 remember how far we can cut back the stack on exit. */
4293 /* Allocate data for this pass. See comments, above,
4294 for what these vectors do.
4296 We use xmalloc instead of alloca, because max_uid can be very large
4297 when there is a lot of function inlining. If we used alloca, we could
4298 exceed stack limits on some hosts for some inputs. */
4299 insn_luid = (int *) xmalloc (max_uid * sizeof (int));
4300 insn_priority = (int *) xmalloc (max_uid * sizeof (int));
4301 insn_tick = (int *) xmalloc (max_uid * sizeof (int));
4302 insn_costs = (short *) xmalloc (max_uid * sizeof (short));
4303 insn_units = (short *) xmalloc (max_uid * sizeof (short));
4304 insn_blockage = (unsigned int *) xmalloc (max_uid * sizeof (unsigned int));
4305 insn_ref_count = (int *) xmalloc (max_uid * sizeof (int));
4307 if (reload_completed == 0)
4309 sched_reg_n_calls_crossed = (int *) alloca (max_regno * sizeof (int));
4310 sched_reg_live_length = (int *) alloca (max_regno * sizeof (int));
4311 bb_dead_regs = ALLOCA_REG_SET ();
4312 bb_live_regs = ALLOCA_REG_SET ();
4313 bzero ((char *) sched_reg_n_calls_crossed, max_regno * sizeof (int));
4314 bzero ((char *) sched_reg_live_length, max_regno * sizeof (int));
4316 else
4318 sched_reg_n_calls_crossed = 0;
4319 sched_reg_live_length = 0;
4320 bb_dead_regs = 0;
4321 bb_live_regs = 0;
4323 init_alias_analysis ();
4325 if (write_symbols != NO_DEBUG)
4327 rtx line;
4329 line_note = (rtx *) xmalloc (max_uid * sizeof (rtx));
4330 bzero ((char *) line_note, max_uid * sizeof (rtx));
4331 line_note_head = (rtx *) alloca (n_basic_blocks * sizeof (rtx));
4332 bzero ((char *) line_note_head, n_basic_blocks * sizeof (rtx));
4334 /* Determine the line-number at the start of each basic block.
4335 This must be computed and saved now, because after a basic block's
4336 predecessor has been scheduled, it is impossible to accurately
4337 determine the correct line number for the first insn of the block. */
4339 for (b = 0; b < n_basic_blocks; b++)
4340 for (line = basic_block_head[b]; line; line = PREV_INSN (line))
4341 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
4343 line_note_head[b] = line;
4344 break;
4348 bzero ((char *) insn_luid, max_uid * sizeof (int));
4349 bzero ((char *) insn_priority, max_uid * sizeof (int));
4350 bzero ((char *) insn_tick, max_uid * sizeof (int));
4351 bzero ((char *) insn_costs, max_uid * sizeof (short));
4352 bzero ((char *) insn_units, max_uid * sizeof (short));
4353 bzero ((char *) insn_blockage, max_uid * sizeof (unsigned int));
4354 bzero ((char *) insn_ref_count, max_uid * sizeof (int));
4356 /* Schedule each basic block, block by block. */
4358 /* ??? Add a NOTE after the last insn of the last basic block. It is not
4359 known why this is done. */
4360 /* ??? Perhaps it's done to ensure NEXT_TAIL in schedule_block is a
4361 valid insn. */
4363 insn = basic_block_end[n_basic_blocks-1];
4364 if (NEXT_INSN (insn) == 0
4365 || (GET_CODE (insn) != NOTE
4366 && GET_CODE (insn) != CODE_LABEL
4367 /* Don't emit a NOTE if it would end up between an unconditional
4368 jump and a BARRIER. */
4369 && ! (GET_CODE (insn) == JUMP_INSN
4370 && GET_CODE (NEXT_INSN (insn)) == BARRIER)))
4371 emit_note_after (NOTE_INSN_DELETED, basic_block_end[n_basic_blocks-1]);
4373 for (b = 0; b < n_basic_blocks; b++)
4375 rtx insn, next;
4377 note_list = 0;
4379 for (insn = basic_block_head[b]; ; insn = next)
4381 rtx prev;
4382 rtx set;
4384 /* Can't use `next_real_insn' because that
4385 might go across CODE_LABELS and short-out basic blocks. */
4386 next = NEXT_INSN (insn);
4387 if (GET_CODE (insn) != INSN)
4389 if (insn == basic_block_end[b])
4390 break;
4392 continue;
4395 /* Don't split no-op move insns. These should silently disappear
4396 later in final. Splitting such insns would break the code
4397 that handles REG_NO_CONFLICT blocks. */
4398 set = single_set (insn);
4399 if (set && rtx_equal_p (SET_SRC (set), SET_DEST (set)))
4401 if (insn == basic_block_end[b])
4402 break;
4404 /* Nops get in the way while scheduling, so delete them now if
4405 register allocation has already been done. It is too risky
4406 to try to do this before register allocation, and there are
4407 unlikely to be very many nops then anyways. */
4408 if (reload_completed)
4410 PUT_CODE (insn, NOTE);
4411 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4412 NOTE_SOURCE_FILE (insn) = 0;
4415 continue;
4418 /* Split insns here to get max fine-grain parallelism. */
4419 prev = PREV_INSN (insn);
4420 /* It is probably not worthwhile to try to split again in the
4421 second pass. However, if flag_schedule_insns is not set,
4422 the first and only (if any) scheduling pass is after reload. */
4423 if (reload_completed == 0 || ! flag_schedule_insns)
4425 rtx last, first = PREV_INSN (insn);
4426 rtx notes = REG_NOTES (insn);
4428 last = try_split (PATTERN (insn), insn, 1);
4429 if (last != insn)
4431 /* try_split returns the NOTE that INSN became. */
4432 first = NEXT_INSN (first);
4433 update_flow_info (notes, first, last, insn);
4435 PUT_CODE (insn, NOTE);
4436 NOTE_SOURCE_FILE (insn) = 0;
4437 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4438 if (insn == basic_block_head[b])
4439 basic_block_head[b] = first;
4440 if (insn == basic_block_end[b])
4442 basic_block_end[b] = last;
4443 break;
4448 if (insn == basic_block_end[b])
4449 break;
4452 schedule_block (b, dump_file);
4454 #ifdef USE_C_ALLOCA
4455 alloca (0);
4456 #endif
4459 /* Reposition the prologue and epilogue notes in case we moved the
4460 prologue/epilogue insns. */
4461 if (reload_completed)
4462 reposition_prologue_and_epilogue_notes (get_insns ());
4464 if (write_symbols != NO_DEBUG)
4466 rtx line = 0;
4467 rtx insn = get_insns ();
4468 int active_insn = 0;
4469 int notes = 0;
4471 /* Walk the insns deleting redundant line-number notes. Many of these
4472 are already present. The remainder tend to occur at basic
4473 block boundaries. */
4474 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
4475 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
4477 /* If there are no active insns following, INSN is redundant. */
4478 if (active_insn == 0)
4480 notes++;
4481 NOTE_SOURCE_FILE (insn) = 0;
4482 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4484 /* If the line number is unchanged, LINE is redundant. */
4485 else if (line
4486 && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
4487 && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
4489 notes++;
4490 NOTE_SOURCE_FILE (line) = 0;
4491 NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
4492 line = insn;
4494 else
4495 line = insn;
4496 active_insn = 0;
4498 else if (! ((GET_CODE (insn) == NOTE
4499 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
4500 || (GET_CODE (insn) == INSN
4501 && (GET_CODE (PATTERN (insn)) == USE
4502 || GET_CODE (PATTERN (insn)) == CLOBBER))))
4503 active_insn++;
4505 if (dump_file && notes)
4506 fprintf (dump_file, ";; deleted %d line-number notes\n", notes);
4509 if (reload_completed == 0)
4511 int regno;
4512 for (regno = 0; regno < max_regno; regno++)
4513 if (sched_reg_live_length[regno])
4515 if (dump_file)
4517 if (REG_LIVE_LENGTH (regno) > sched_reg_live_length[regno])
4518 fprintf (dump_file,
4519 ";; register %d life shortened from %d to %d\n",
4520 regno, REG_LIVE_LENGTH (regno),
4521 sched_reg_live_length[regno]);
4522 /* Negative values are special; don't overwrite the current
4523 reg_live_length value if it is negative. */
4524 else if (REG_LIVE_LENGTH (regno) < sched_reg_live_length[regno]
4525 && REG_LIVE_LENGTH (regno) >= 0)
4526 fprintf (dump_file,
4527 ";; register %d life extended from %d to %d\n",
4528 regno, REG_LIVE_LENGTH (regno),
4529 sched_reg_live_length[regno]);
4531 if (! REG_N_CALLS_CROSSED (regno)
4532 && sched_reg_n_calls_crossed[regno])
4533 fprintf (dump_file,
4534 ";; register %d now crosses calls\n", regno);
4535 else if (REG_N_CALLS_CROSSED (regno)
4536 && ! sched_reg_n_calls_crossed[regno]
4537 && REG_BASIC_BLOCK (regno) != REG_BLOCK_GLOBAL)
4538 fprintf (dump_file,
4539 ";; register %d no longer crosses calls\n", regno);
4542 /* Negative values are special; don't overwrite the current
4543 reg_live_length value if it is negative. */
4544 if (REG_LIVE_LENGTH (regno) >= 0)
4545 REG_LIVE_LENGTH (regno) = sched_reg_live_length[regno];
4547 /* We can't change the value of reg_n_calls_crossed to zero for
4548 pseudos which are live in more than one block.
4550 This is because combine might have made an optimization which
4551 invalidated basic_block_live_at_start and reg_n_calls_crossed,
4552 but it does not update them. If we update reg_n_calls_crossed
4553 here, the two variables are now inconsistent, and this might
4554 confuse the caller-save code into saving a register that doesn't
4555 need to be saved. This is only a problem when we zero calls
4556 crossed for a pseudo live in multiple basic blocks.
4558 Alternatively, we could try to correctly update basic block live
4559 at start here in sched, but that seems complicated. */
4560 if (sched_reg_n_calls_crossed[regno]
4561 || REG_BASIC_BLOCK (regno) != REG_BLOCK_GLOBAL)
4562 REG_N_CALLS_CROSSED (regno) = sched_reg_n_calls_crossed[regno];
4566 free (insn_luid);
4567 free (insn_priority);
4568 free (insn_tick);
4569 free (insn_costs);
4570 free (insn_units);
4571 free (insn_blockage);
4572 free (insn_ref_count);
4574 if (write_symbols != NO_DEBUG)
4575 free (line_note);
4577 if (reload_completed == 0)
4579 FREE_REG_SET (bb_dead_regs);
4580 FREE_REG_SET (bb_live_regs);
4584 #endif /* INSN_SCHEDULING */