basic-block.h (basic_block_head): Rename to x_basic_block_head.
[gcc.git] / gcc / local-alloc.c
1 /* Allocate registers within a basic block, for GNU compiler.
2 Copyright (C) 1987, 88, 91, 93-97, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 /* Allocation of hard register numbers to pseudo registers is done in
23 two passes. In this pass we consider only regs that are born and
24 die once within one basic block. We do this one basic block at a
25 time. Then the next pass allocates the registers that remain.
26 Two passes are used because this pass uses methods that work only
27 on linear code, but that do a better job than the general methods
28 used in global_alloc, and more quickly too.
29
30 The assignments made are recorded in the vector reg_renumber
31 whose space is allocated here. The rtl code itself is not altered.
32
33 We assign each instruction in the basic block a number
34 which is its order from the beginning of the block.
35 Then we can represent the lifetime of a pseudo register with
36 a pair of numbers, and check for conflicts easily.
37 We can record the availability of hard registers with a
38 HARD_REG_SET for each instruction. The HARD_REG_SET
39 contains 0 or 1 for each hard reg.
40
41 To avoid register shuffling, we tie registers together when one
42 dies by being copied into another, or dies in an instruction that
43 does arithmetic to produce another. The tied registers are
44 allocated as one. Registers with different reg class preferences
45 can never be tied unless the class preferred by one is a subclass
46 of the one preferred by the other.
47
48 Tying is represented with "quantity numbers".
49 A non-tied register is given a new quantity number.
50 Tied registers have the same quantity number.
51
52 We have provision to exempt registers, even when they are contained
53 within the block, that can be tied to others that are not contained in it.
54 This is so that global_alloc could process them both and tie them then.
55 But this is currently disabled since tying in global_alloc is not
56 yet implemented. */
57
58 /* Pseudos allocated here can be reallocated by global.c if the hard register
59 is used as a spill register. Currently we don't allocate such pseudos
60 here if their preferred class is likely to be used by spills. */
61
62 #include "config.h"
63 #include "system.h"
64 #include "rtl.h"
65 #include "flags.h"
66 #include "basic-block.h"
67 #include "regs.h"
68 #include "hard-reg-set.h"
69 #include "insn-config.h"
70 #include "insn-attr.h"
71 #include "recog.h"
72 #include "output.h"
73 #include "toplev.h"
74 \f
75 /* Next quantity number available for allocation. */
76
77 static int next_qty;
78
79 /* In all the following vectors indexed by quantity number. */
80
81 /* Element Q is the hard reg number chosen for quantity Q,
82 or -1 if none was found. */
83
84 static short *qty_phys_reg;
85
86 /* We maintain two hard register sets that indicate suggested hard registers
87 for each quantity. The first, qty_phys_copy_sugg, contains hard registers
88 that are tied to the quantity by a simple copy. The second contains all
89 hard registers that are tied to the quantity via an arithmetic operation.
90
91 The former register set is given priority for allocation. This tends to
92 eliminate copy insns. */
93
94 /* Element Q is a set of hard registers that are suggested for quantity Q by
95 copy insns. */
96
97 static HARD_REG_SET *qty_phys_copy_sugg;
98
99 /* Element Q is a set of hard registers that are suggested for quantity Q by
100 arithmetic insns. */
101
102 static HARD_REG_SET *qty_phys_sugg;
103
104 /* Element Q is the number of suggested registers in qty_phys_copy_sugg. */
105
106 static short *qty_phys_num_copy_sugg;
107
108 /* Element Q is the number of suggested registers in qty_phys_sugg. */
109
110 static short *qty_phys_num_sugg;
111
112 /* Element Q is the number of refs to quantity Q. */
113
114 static int *qty_n_refs;
115
116 /* Element Q is a reg class contained in (smaller than) the
117 preferred classes of all the pseudo regs that are tied in quantity Q.
118 This is the preferred class for allocating that quantity. */
119
120 static enum reg_class *qty_min_class;
121
122 /* Insn number (counting from head of basic block)
123 where quantity Q was born. -1 if birth has not been recorded. */
124
125 static int *qty_birth;
126
127 /* Insn number (counting from head of basic block)
128 where quantity Q died. Due to the way tying is done,
129 and the fact that we consider in this pass only regs that die but once,
130 a quantity can die only once. Each quantity's life span
131 is a set of consecutive insns. -1 if death has not been recorded. */
132
133 static int *qty_death;
134
135 /* Number of words needed to hold the data in quantity Q.
136 This depends on its machine mode. It is used for these purposes:
137 1. It is used in computing the relative importances of qtys,
138 which determines the order in which we look for regs for them.
139 2. It is used in rules that prevent tying several registers of
140 different sizes in a way that is geometrically impossible
141 (see combine_regs). */
142
143 static int *qty_size;
144
145 /* This holds the mode of the registers that are tied to qty Q,
146 or VOIDmode if registers with differing modes are tied together. */
147
148 static enum machine_mode *qty_mode;
149
150 /* Number of times a reg tied to qty Q lives across a CALL_INSN. */
151
152 static int *qty_n_calls_crossed;
153
154 /* Register class within which we allocate qty Q if we can't get
155 its preferred class. */
156
157 static enum reg_class *qty_alternate_class;
158
159 /* Element Q is nonzero if this quantity has been used in a SUBREG
160 that changes its size. */
161
162 static char *qty_changes_size;
163
164 /* Element Q is the register number of one pseudo register whose
165 reg_qty value is Q. This register should be the head of the chain
166 maintained in reg_next_in_qty. */
167
168 static int *qty_first_reg;
169
170 /* If (REG N) has been assigned a quantity number, is a register number
171 of another register assigned the same quantity number, or -1 for the
172 end of the chain. qty_first_reg point to the head of this chain. */
173
174 static int *reg_next_in_qty;
175
176 /* reg_qty[N] (where N is a pseudo reg number) is the qty number of that reg
177 if it is >= 0,
178 of -1 if this register cannot be allocated by local-alloc,
179 or -2 if not known yet.
180
181 Note that if we see a use or death of pseudo register N with
182 reg_qty[N] == -2, register N must be local to the current block. If
183 it were used in more than one block, we would have reg_qty[N] == -1.
184 This relies on the fact that if reg_basic_block[N] is >= 0, register N
185 will not appear in any other block. We save a considerable number of
186 tests by exploiting this.
187
188 If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is undefined and should not
189 be referenced. */
190
191 static int *reg_qty;
192
193 /* The offset (in words) of register N within its quantity.
194 This can be nonzero if register N is SImode, and has been tied
195 to a subreg of a DImode register. */
196
197 static char *reg_offset;
198
199 /* Vector of substitutions of register numbers,
200 used to map pseudo regs into hardware regs.
201 This is set up as a result of register allocation.
202 Element N is the hard reg assigned to pseudo reg N,
203 or is -1 if no hard reg was assigned.
204 If N is a hard reg number, element N is N. */
205
206 short *reg_renumber;
207
208 /* Set of hard registers live at the current point in the scan
209 of the instructions in a basic block. */
210
211 static HARD_REG_SET regs_live;
212
213 /* Each set of hard registers indicates registers live at a particular
214 point in the basic block. For N even, regs_live_at[N] says which
215 hard registers are needed *after* insn N/2 (i.e., they may not
216 conflict with the outputs of insn N/2 or the inputs of insn N/2 + 1.
217
218 If an object is to conflict with the inputs of insn J but not the
219 outputs of insn J + 1, we say it is born at index J*2 - 1. Similarly,
220 if it is to conflict with the outputs of insn J but not the inputs of
221 insn J + 1, it is said to die at index J*2 + 1. */
222
223 static HARD_REG_SET *regs_live_at;
224
225 /* Communicate local vars `insn_number' and `insn'
226 from `block_alloc' to `reg_is_set', `wipe_dead_reg', and `alloc_qty'. */
227 static int this_insn_number;
228 static rtx this_insn;
229
230 /* Used to communicate changes made by update_equiv_regs to
231 memref_referenced_p. reg_equiv_replacement is set for any REG_EQUIV note
232 found or created, so that we can keep track of what memory accesses might
233 be created later, e.g. by reload. */
234
235 static rtx *reg_equiv_replacement;
236
237 /* Used for communication between update_equiv_regs and no_equiv. */
238 static rtx *reg_equiv_init_insns;
239
240 static void alloc_qty PROTO((int, enum machine_mode, int, int));
241 static void validate_equiv_mem_from_store PROTO((rtx, rtx));
242 static int validate_equiv_mem PROTO((rtx, rtx, rtx));
243 static int contains_replace_regs PROTO((rtx, char *));
244 static int memref_referenced_p PROTO((rtx, rtx));
245 static int memref_used_between_p PROTO((rtx, rtx, rtx));
246 static void update_equiv_regs PROTO((void));
247 static void no_equiv PROTO((rtx, rtx));
248 static void block_alloc PROTO((int));
249 static int qty_sugg_compare PROTO((int, int));
250 static int qty_sugg_compare_1 PROTO((const GENERIC_PTR, const GENERIC_PTR));
251 static int qty_compare PROTO((int, int));
252 static int qty_compare_1 PROTO((const GENERIC_PTR, const GENERIC_PTR));
253 static int combine_regs PROTO((rtx, rtx, int, int, rtx, int));
254 static int reg_meets_class_p PROTO((int, enum reg_class));
255 static void update_qty_class PROTO((int, int));
256 static void reg_is_set PROTO((rtx, rtx));
257 static void reg_is_born PROTO((rtx, int));
258 static void wipe_dead_reg PROTO((rtx, int));
259 static int find_free_reg PROTO((enum reg_class, enum machine_mode,
260 int, int, int, int, int));
261 static void mark_life PROTO((int, enum machine_mode, int));
262 static void post_mark_life PROTO((int, enum machine_mode, int, int, int));
263 static int no_conflict_p PROTO((rtx, rtx, rtx));
264 static int requires_inout PROTO((char *));
265 \f
266 /* Allocate a new quantity (new within current basic block)
267 for register number REGNO which is born at index BIRTH
268 within the block. MODE and SIZE are info on reg REGNO. */
269
270 static void
271 alloc_qty (regno, mode, size, birth)
272 int regno;
273 enum machine_mode mode;
274 int size, birth;
275 {
276 register int qty = next_qty++;
277
278 reg_qty[regno] = qty;
279 reg_offset[regno] = 0;
280 reg_next_in_qty[regno] = -1;
281
282 qty_first_reg[qty] = regno;
283 qty_size[qty] = size;
284 qty_mode[qty] = mode;
285 qty_birth[qty] = birth;
286 qty_n_calls_crossed[qty] = REG_N_CALLS_CROSSED (regno);
287 qty_min_class[qty] = reg_preferred_class (regno);
288 qty_alternate_class[qty] = reg_alternate_class (regno);
289 qty_n_refs[qty] = REG_N_REFS (regno);
290 qty_changes_size[qty] = REG_CHANGES_SIZE (regno);
291 }
292 \f
293 /* Main entry point of this file. */
294
295 void
296 local_alloc ()
297 {
298 register int b, i;
299 int max_qty;
300
301 /* Leaf functions and non-leaf functions have different needs.
302 If defined, let the machine say what kind of ordering we
303 should use. */
304 #ifdef ORDER_REGS_FOR_LOCAL_ALLOC
305 ORDER_REGS_FOR_LOCAL_ALLOC;
306 #endif
307
308 /* Promote REG_EQUAL notes to REG_EQUIV notes and adjust status of affected
309 registers. */
310 update_equiv_regs ();
311
312 /* This sets the maximum number of quantities we can have. Quantity
313 numbers start at zero and we can have one for each pseudo. */
314 max_qty = (max_regno - FIRST_PSEUDO_REGISTER);
315
316 /* Allocate vectors of temporary data.
317 See the declarations of these variables, above,
318 for what they mean. */
319
320 qty_phys_reg = (short *) alloca (max_qty * sizeof (short));
321 qty_phys_copy_sugg
322 = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
323 qty_phys_num_copy_sugg = (short *) alloca (max_qty * sizeof (short));
324 qty_phys_sugg = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
325 qty_phys_num_sugg = (short *) alloca (max_qty * sizeof (short));
326 qty_birth = (int *) alloca (max_qty * sizeof (int));
327 qty_death = (int *) alloca (max_qty * sizeof (int));
328 qty_first_reg = (int *) alloca (max_qty * sizeof (int));
329 qty_size = (int *) alloca (max_qty * sizeof (int));
330 qty_mode
331 = (enum machine_mode *) alloca (max_qty * sizeof (enum machine_mode));
332 qty_n_calls_crossed = (int *) alloca (max_qty * sizeof (int));
333 qty_min_class
334 = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
335 qty_alternate_class
336 = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
337 qty_n_refs = (int *) alloca (max_qty * sizeof (int));
338 qty_changes_size = (char *) alloca (max_qty * sizeof (char));
339
340 reg_qty = (int *) xmalloc (max_regno * sizeof (int));
341 reg_offset = (char *) xmalloc (max_regno * sizeof (char));
342 reg_next_in_qty = (int *) xmalloc(max_regno * sizeof (int));
343
344 /* Allocate the reg_renumber array */
345 allocate_reg_info (max_regno, FALSE, TRUE);
346
347 /* Determine which pseudo-registers can be allocated by local-alloc.
348 In general, these are the registers used only in a single block and
349 which only die once. However, if a register's preferred class has only
350 a few entries, don't allocate this register here unless it is preferred
351 or nothing since retry_global_alloc won't be able to move it to
352 GENERAL_REGS if a reload register of this class is needed.
353
354 We need not be concerned with which block actually uses the register
355 since we will never see it outside that block. */
356
357 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
358 {
359 if (REG_BASIC_BLOCK (i) >= 0 && REG_N_DEATHS (i) == 1
360 && (reg_alternate_class (i) == NO_REGS
361 || ! CLASS_LIKELY_SPILLED_P (reg_preferred_class (i))))
362 reg_qty[i] = -2;
363 else
364 reg_qty[i] = -1;
365 }
366
367 /* Force loop below to initialize entire quantity array. */
368 next_qty = max_qty;
369
370 /* Allocate each block's local registers, block by block. */
371
372 for (b = 0; b < n_basic_blocks; b++)
373 {
374 /* NEXT_QTY indicates which elements of the `qty_...'
375 vectors might need to be initialized because they were used
376 for the previous block; it is set to the entire array before
377 block 0. Initialize those, with explicit loop if there are few,
378 else with bzero and bcopy. Do not initialize vectors that are
379 explicit set by `alloc_qty'. */
380
381 if (next_qty < 6)
382 {
383 for (i = 0; i < next_qty; i++)
384 {
385 CLEAR_HARD_REG_SET (qty_phys_copy_sugg[i]);
386 qty_phys_num_copy_sugg[i] = 0;
387 CLEAR_HARD_REG_SET (qty_phys_sugg[i]);
388 qty_phys_num_sugg[i] = 0;
389 }
390 }
391 else
392 {
393 #define CLEAR(vector) \
394 bzero ((char *) (vector), (sizeof (*(vector))) * next_qty);
395
396 CLEAR (qty_phys_copy_sugg);
397 CLEAR (qty_phys_num_copy_sugg);
398 CLEAR (qty_phys_sugg);
399 CLEAR (qty_phys_num_sugg);
400 }
401
402 next_qty = 0;
403
404 block_alloc (b);
405 #ifdef USE_C_ALLOCA
406 alloca (0);
407 #endif
408 }
409
410 free (reg_qty);
411 free (reg_offset);
412 free (reg_next_in_qty);
413 }
414 \f
415 /* Depth of loops we are in while in update_equiv_regs. */
416 static int loop_depth;
417
418 /* Used for communication between the following two functions: contains
419 a MEM that we wish to ensure remains unchanged. */
420 static rtx equiv_mem;
421
422 /* Set nonzero if EQUIV_MEM is modified. */
423 static int equiv_mem_modified;
424
425 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
426 Called via note_stores. */
427
428 static void
429 validate_equiv_mem_from_store (dest, set)
430 rtx dest;
431 rtx set ATTRIBUTE_UNUSED;
432 {
433 if ((GET_CODE (dest) == REG
434 && reg_overlap_mentioned_p (dest, equiv_mem))
435 || (GET_CODE (dest) == MEM
436 && true_dependence (dest, VOIDmode, equiv_mem, rtx_varies_p)))
437 equiv_mem_modified = 1;
438 }
439
440 /* Verify that no store between START and the death of REG invalidates
441 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
442 by storing into an overlapping memory location, or with a non-const
443 CALL_INSN.
444
445 Return 1 if MEMREF remains valid. */
446
447 static int
448 validate_equiv_mem (start, reg, memref)
449 rtx start;
450 rtx reg;
451 rtx memref;
452 {
453 rtx insn;
454 rtx note;
455
456 equiv_mem = memref;
457 equiv_mem_modified = 0;
458
459 /* If the memory reference has side effects or is volatile, it isn't a
460 valid equivalence. */
461 if (side_effects_p (memref))
462 return 0;
463
464 for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
465 {
466 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
467 continue;
468
469 if (find_reg_note (insn, REG_DEAD, reg))
470 return 1;
471
472 if (GET_CODE (insn) == CALL_INSN && ! RTX_UNCHANGING_P (memref)
473 && ! CONST_CALL_P (insn))
474 return 0;
475
476 note_stores (PATTERN (insn), validate_equiv_mem_from_store);
477
478 /* If a register mentioned in MEMREF is modified via an
479 auto-increment, we lose the equivalence. Do the same if one
480 dies; although we could extend the life, it doesn't seem worth
481 the trouble. */
482
483 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
484 if ((REG_NOTE_KIND (note) == REG_INC
485 || REG_NOTE_KIND (note) == REG_DEAD)
486 && GET_CODE (XEXP (note, 0)) == REG
487 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
488 return 0;
489 }
490
491 return 0;
492 }
493
494 /* TRUE if X uses any registers for which reg_equiv_replace is true. */
495
496 static int
497 contains_replace_regs (x, reg_equiv_replace)
498 rtx x;
499 char *reg_equiv_replace;
500 {
501 int i, j;
502 char *fmt;
503 enum rtx_code code = GET_CODE (x);
504
505 switch (code)
506 {
507 case CONST_INT:
508 case CONST:
509 case LABEL_REF:
510 case SYMBOL_REF:
511 case CONST_DOUBLE:
512 case PC:
513 case CC0:
514 case HIGH:
515 case LO_SUM:
516 return 0;
517
518 case REG:
519 return reg_equiv_replace[REGNO (x)];
520
521 default:
522 break;
523 }
524
525 fmt = GET_RTX_FORMAT (code);
526 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
527 switch (fmt[i])
528 {
529 case 'e':
530 if (contains_replace_regs (XEXP (x, i), reg_equiv_replace))
531 return 1;
532 break;
533 case 'E':
534 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
535 if (contains_replace_regs (XVECEXP (x, i, j), reg_equiv_replace))
536 return 1;
537 break;
538 }
539
540 return 0;
541 }
542 \f
543 /* TRUE if X references a memory location that would be affected by a store
544 to MEMREF. */
545
546 static int
547 memref_referenced_p (memref, x)
548 rtx x;
549 rtx memref;
550 {
551 int i, j;
552 char *fmt;
553 enum rtx_code code = GET_CODE (x);
554
555 switch (code)
556 {
557 case CONST_INT:
558 case CONST:
559 case LABEL_REF:
560 case SYMBOL_REF:
561 case CONST_DOUBLE:
562 case PC:
563 case CC0:
564 case HIGH:
565 case LO_SUM:
566 return 0;
567
568 case REG:
569 return (reg_equiv_replacement[REGNO (x)]
570 && memref_referenced_p (memref,
571 reg_equiv_replacement[REGNO (x)]));
572
573 case MEM:
574 if (true_dependence (memref, VOIDmode, x, rtx_varies_p))
575 return 1;
576 break;
577
578 case SET:
579 /* If we are setting a MEM, it doesn't count (its address does), but any
580 other SET_DEST that has a MEM in it is referencing the MEM. */
581 if (GET_CODE (SET_DEST (x)) == MEM)
582 {
583 if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
584 return 1;
585 }
586 else if (memref_referenced_p (memref, SET_DEST (x)))
587 return 1;
588
589 return memref_referenced_p (memref, SET_SRC (x));
590
591 default:
592 break;
593 }
594
595 fmt = GET_RTX_FORMAT (code);
596 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
597 switch (fmt[i])
598 {
599 case 'e':
600 if (memref_referenced_p (memref, XEXP (x, i)))
601 return 1;
602 break;
603 case 'E':
604 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
605 if (memref_referenced_p (memref, XVECEXP (x, i, j)))
606 return 1;
607 break;
608 }
609
610 return 0;
611 }
612
613 /* TRUE if some insn in the range (START, END] references a memory location
614 that would be affected by a store to MEMREF. */
615
616 static int
617 memref_used_between_p (memref, start, end)
618 rtx memref;
619 rtx start;
620 rtx end;
621 {
622 rtx insn;
623
624 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
625 insn = NEXT_INSN (insn))
626 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
627 && memref_referenced_p (memref, PATTERN (insn)))
628 return 1;
629
630 return 0;
631 }
632 \f
633 /* Return nonzero if the rtx X is invariant over the current function. */
634 int
635 function_invariant_p (x)
636 rtx x;
637 {
638 if (CONSTANT_P (x))
639 return 1;
640 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
641 return 1;
642 if (GET_CODE (x) == PLUS
643 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
644 && CONSTANT_P (XEXP (x, 1)))
645 return 1;
646 return 0;
647 }
648
649 /* Find registers that are equivalent to a single value throughout the
650 compilation (either because they can be referenced in memory or are set once
651 from a single constant). Lower their priority for a register.
652
653 If such a register is only referenced once, try substituting its value
654 into the using insn. If it succeeds, we can eliminate the register
655 completely. */
656
657 static void
658 update_equiv_regs ()
659 {
660 /* Set when an attempt should be made to replace a register with the
661 associated reg_equiv_replacement entry at the end of this function. */
662 char *reg_equiv_replace
663 = (char *) alloca (max_regno * sizeof *reg_equiv_replace);
664 rtx insn;
665 int block, depth;
666
667 reg_equiv_init_insns = (rtx *) alloca (max_regno * sizeof (rtx));
668 reg_equiv_replacement = (rtx *) alloca (max_regno * sizeof (rtx));
669
670 bzero ((char *) reg_equiv_init_insns, max_regno * sizeof (rtx));
671 bzero ((char *) reg_equiv_replacement, max_regno * sizeof (rtx));
672 bzero ((char *) reg_equiv_replace, max_regno * sizeof *reg_equiv_replace);
673
674 init_alias_analysis ();
675
676 loop_depth = 1;
677
678 /* Scan the insns and find which registers have equivalences. Do this
679 in a separate scan of the insns because (due to -fcse-follow-jumps)
680 a register can be set below its use. */
681 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
682 {
683 rtx note;
684 rtx set;
685 rtx dest, src;
686 int regno;
687
688 if (GET_CODE (insn) == NOTE)
689 {
690 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
691 loop_depth++;
692 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
693 loop_depth--;
694 }
695
696 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
697 continue;
698
699 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
700 if (REG_NOTE_KIND (note) == REG_INC)
701 no_equiv (XEXP (note, 0), note);
702
703 set = single_set (insn);
704
705 /* If this insn contains more (or less) than a single SET,
706 only mark all destinations as having no known equivalence. */
707 if (set == 0)
708 {
709 note_stores (PATTERN (insn), no_equiv);
710 continue;
711 }
712 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
713 {
714 int i;
715
716 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
717 {
718 rtx part = XVECEXP (PATTERN (insn), 0, i);
719 if (part != set)
720 note_stores (part, no_equiv);
721 }
722 }
723
724 dest = SET_DEST (set);
725 src = SET_SRC (set);
726
727 /* If this sets a MEM to the contents of a REG that is only used
728 in a single basic block, see if the register is always equivalent
729 to that memory location and if moving the store from INSN to the
730 insn that set REG is safe. If so, put a REG_EQUIV note on the
731 initializing insn.
732
733 Don't add a REG_EQUIV note if the insn already has one. The existing
734 REG_EQUIV is likely more useful than the one we are adding.
735
736 If one of the regs in the address is marked as reg_equiv_replace,
737 then we can't add this REG_EQUIV note. The reg_equiv_replace
738 optimization may move the set of this register immediately before
739 insn, which puts it after reg_equiv_init_insns[regno], and hence
740 the mention in the REG_EQUIV note would be to an uninitialized
741 pseudo. */
742 /* ????? This test isn't good enough; we might see a MEM with a use of
743 a pseudo register before we see its setting insn that will cause
744 reg_equiv_replace for that pseudo to be set.
745 Equivalences to MEMs should be made in another pass, after the
746 reg_equiv_replace information has been gathered. */
747
748 if (GET_CODE (dest) == MEM && GET_CODE (src) == REG
749 && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
750 && REG_BASIC_BLOCK (regno) >= 0
751 && REG_N_SETS (regno) == 1
752 && reg_equiv_init_insns[regno] != 0
753 && reg_equiv_init_insns[regno] != const0_rtx
754 && ! find_reg_note (insn, REG_EQUIV, NULL_RTX)
755 && ! contains_replace_regs (XEXP (dest, 0), reg_equiv_replace))
756 {
757 rtx init_insn = XEXP (reg_equiv_init_insns[regno], 0);
758 if (validate_equiv_mem (init_insn, src, dest)
759 && ! memref_used_between_p (dest, init_insn, insn))
760 REG_NOTES (init_insn)
761 = gen_rtx_EXPR_LIST (REG_EQUIV, dest, REG_NOTES (init_insn));
762 }
763
764 /* We only handle the case of a pseudo register being set
765 once, or always to the same value. */
766 /* ??? The mn10200 port breaks if we add equivalences for
767 values that need an ADDRESS_REGS register and set them equivalent
768 to a MEM of a pseudo. The actual problem is in the over-conservative
769 handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
770 calculate_needs, but we traditionally work around this problem
771 here by rejecting equivalences when the destination is in a register
772 that's likely spilled. This is fragile, of course, since the
773 preferred class of a pseudo depends on all instructions that set
774 or use it. */
775
776 if (GET_CODE (dest) != REG
777 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
778 || reg_equiv_init_insns[regno] == const0_rtx
779 || (CLASS_LIKELY_SPILLED_P (reg_preferred_class (regno))
780 && GET_CODE (src) == MEM))
781 {
782 /* This might be seting a SUBREG of a pseudo, a pseudo that is
783 also set somewhere else to a constant. */
784 note_stores (set, no_equiv);
785 continue;
786 }
787 /* Don't handle the equivalence if the source is in a register
788 class that's likely to be spilled. */
789 if (GET_CODE (src) == REG
790 && REGNO (src) >= FIRST_PSEUDO_REGISTER
791 && CLASS_LIKELY_SPILLED_P (reg_preferred_class (REGNO (src))))
792 {
793 no_equiv (dest, set);
794 continue;
795 }
796
797 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
798
799 if (REG_N_SETS (regno) != 1
800 && (! note
801 || ! function_invariant_p (XEXP (note, 0))
802 || (reg_equiv_replacement[regno]
803 && ! rtx_equal_p (XEXP (note, 0),
804 reg_equiv_replacement[regno]))))
805 {
806 no_equiv (dest, set);
807 continue;
808 }
809 /* Record this insn as initializing this register. */
810 reg_equiv_init_insns[regno]
811 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init_insns[regno]);
812
813 /* If this register is known to be equal to a constant, record that
814 it is always equivalent to the constant. */
815 if (note && function_invariant_p (XEXP (note, 0)))
816 PUT_MODE (note, (enum machine_mode) REG_EQUIV);
817
818 /* If this insn introduces a "constant" register, decrease the priority
819 of that register. Record this insn if the register is only used once
820 more and the equivalence value is the same as our source.
821
822 The latter condition is checked for two reasons: First, it is an
823 indication that it may be more efficient to actually emit the insn
824 as written (if no registers are available, reload will substitute
825 the equivalence). Secondly, it avoids problems with any registers
826 dying in this insn whose death notes would be missed.
827
828 If we don't have a REG_EQUIV note, see if this insn is loading
829 a register used only in one basic block from a MEM. If so, and the
830 MEM remains unchanged for the life of the register, add a REG_EQUIV
831 note. */
832
833 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
834
835 if (note == 0 && REG_BASIC_BLOCK (regno) >= 0
836 && GET_CODE (SET_SRC (set)) == MEM
837 && validate_equiv_mem (insn, dest, SET_SRC (set)))
838 REG_NOTES (insn) = note = gen_rtx_EXPR_LIST (REG_EQUIV, SET_SRC (set),
839 REG_NOTES (insn));
840
841 if (note)
842 {
843 int regno = REGNO (dest);
844
845 reg_equiv_replacement[regno] = XEXP (note, 0);
846
847 /* Don't mess with things live during setjmp. */
848 if (REG_LIVE_LENGTH (regno) >= 0)
849 {
850 /* Note that the statement below does not affect the priority
851 in local-alloc! */
852 REG_LIVE_LENGTH (regno) *= 2;
853
854
855 /* If the register is referenced exactly twice, meaning it is
856 set once and used once, indicate that the reference may be
857 replaced by the equivalence we computed above. If the
858 register is only used in one basic block, this can't succeed
859 or combine would have done it.
860
861 It would be nice to use "loop_depth * 2" in the compare
862 below. Unfortunately, LOOP_DEPTH need not be constant within
863 a basic block so this would be too complicated.
864
865 This case normally occurs when a parameter is read from
866 memory and then used exactly once, not in a loop. */
867
868 if (REG_N_REFS (regno) == 2
869 && REG_BASIC_BLOCK (regno) < 0
870 && rtx_equal_p (XEXP (note, 0), SET_SRC (set)))
871 reg_equiv_replace[regno] = 1;
872 }
873 }
874 }
875
876 /* Now scan all regs killed in an insn to see if any of them are
877 registers only used that once. If so, see if we can replace the
878 reference with the equivalent from. If we can, delete the
879 initializing reference and this register will go away. If we
880 can't replace the reference, and the instruction is not in a
881 loop, then move the register initialization just before the use,
882 so that they are in the same basic block. */
883 block = -1;
884 depth = 0;
885 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
886 {
887 rtx link;
888
889 /* Keep track of which basic block we are in. */
890 if (block + 1 < n_basic_blocks
891 && BLOCK_HEAD (block + 1) == insn)
892 ++block;
893
894 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
895 {
896 if (GET_CODE (insn) == NOTE)
897 {
898 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
899 ++depth;
900 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
901 {
902 --depth;
903 if (depth < 0)
904 abort ();
905 }
906 }
907
908 continue;
909 }
910
911 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
912 {
913 if (REG_NOTE_KIND (link) == REG_DEAD
914 /* Make sure this insn still refers to the register. */
915 && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
916 {
917 int regno = REGNO (XEXP (link, 0));
918 rtx equiv_insn;
919
920 if (! reg_equiv_replace[regno])
921 continue;
922
923 /* reg_equiv_replace[REGNO] gets set only when
924 REG_N_REFS[REGNO] is 2, i.e. the register is set
925 once and used once. (If it were only set, but not used,
926 flow would have deleted the setting insns.) Hence
927 there can only be one insn in reg_equiv_init_insns. */
928 equiv_insn = XEXP (reg_equiv_init_insns[regno], 0);
929
930 if (validate_replace_rtx (regno_reg_rtx[regno],
931 reg_equiv_replacement[regno], insn))
932 {
933 remove_death (regno, insn);
934 REG_N_REFS (regno) = 0;
935 PUT_CODE (equiv_insn, NOTE);
936 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
937 NOTE_SOURCE_FILE (equiv_insn) = 0;
938 }
939 /* If we aren't in a loop, and there are no calls in
940 INSN or in the initialization of the register, then
941 move the initialization of the register to just
942 before INSN. Update the flow information. */
943 else if (depth == 0
944 && GET_CODE (equiv_insn) == INSN
945 && GET_CODE (insn) == INSN
946 && REG_BASIC_BLOCK (regno) < 0)
947 {
948 int l;
949
950 emit_insn_before (copy_rtx (PATTERN (equiv_insn)), insn);
951 REG_NOTES (PREV_INSN (insn)) = REG_NOTES (equiv_insn);
952
953 PUT_CODE (equiv_insn, NOTE);
954 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
955 NOTE_SOURCE_FILE (equiv_insn) = 0;
956 REG_NOTES (equiv_insn) = 0;
957
958 if (block < 0)
959 REG_BASIC_BLOCK (regno) = 0;
960 else
961 REG_BASIC_BLOCK (regno) = block;
962 REG_N_CALLS_CROSSED (regno) = 0;
963 REG_LIVE_LENGTH (regno) = 2;
964
965 if (block >= 0 && insn == BLOCK_HEAD (block))
966 BLOCK_HEAD (block) = PREV_INSN (insn);
967
968 for (l = 0; l < n_basic_blocks; l++)
969 CLEAR_REGNO_REG_SET (basic_block_live_at_start[l], regno);
970 }
971 }
972 }
973 }
974 }
975
976 /* Mark REG as having no known equivalence.
977 Some instructions might have been proceessed before and furnished
978 with REG_EQUIV notes for this register; these notes will have to be
979 removed.
980 STORE is the piece of RTL that does the non-constant / conflicting
981 assignment - a SET, CLOBBER or REG_INC note. It is currently not used,
982 but needs to be there because this function is called from note_stores. */
983 static void
984 no_equiv (reg, store)
985 rtx reg, store;
986 {
987 int regno;
988 rtx list;
989
990 if (GET_CODE (reg) != REG)
991 return;
992 regno = REGNO (reg);
993 list = reg_equiv_init_insns[regno];
994 if (list == const0_rtx)
995 return;
996 for (; list; list = XEXP (list, 1))
997 {
998 rtx insn = XEXP (list, 0);
999 remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
1000 }
1001 reg_equiv_init_insns[regno] = const0_rtx;
1002 reg_equiv_replacement[regno] = NULL_RTX;
1003 }
1004 \f
1005 /* Allocate hard regs to the pseudo regs used only within block number B.
1006 Only the pseudos that die but once can be handled. */
1007
1008 static void
1009 block_alloc (b)
1010 int b;
1011 {
1012 register int i, q;
1013 register rtx insn;
1014 rtx note;
1015 int insn_number = 0;
1016 int insn_count = 0;
1017 int max_uid = get_max_uid ();
1018 int *qty_order;
1019 int no_conflict_combined_regno = -1;
1020
1021 /* Count the instructions in the basic block. */
1022
1023 insn = BLOCK_END (b);
1024 while (1)
1025 {
1026 if (GET_CODE (insn) != NOTE)
1027 if (++insn_count > max_uid)
1028 abort ();
1029 if (insn == BLOCK_HEAD (b))
1030 break;
1031 insn = PREV_INSN (insn);
1032 }
1033
1034 /* +2 to leave room for a post_mark_life at the last insn and for
1035 the birth of a CLOBBER in the first insn. */
1036 regs_live_at = (HARD_REG_SET *) alloca ((2 * insn_count + 2)
1037 * sizeof (HARD_REG_SET));
1038 bzero ((char *) regs_live_at, (2 * insn_count + 2) * sizeof (HARD_REG_SET));
1039
1040 /* Initialize table of hardware registers currently live. */
1041
1042 REG_SET_TO_HARD_REG_SET (regs_live, basic_block_live_at_start[b]);
1043
1044 /* This loop scans the instructions of the basic block
1045 and assigns quantities to registers.
1046 It computes which registers to tie. */
1047
1048 insn = BLOCK_HEAD (b);
1049 while (1)
1050 {
1051 register rtx body = PATTERN (insn);
1052
1053 if (GET_CODE (insn) != NOTE)
1054 insn_number++;
1055
1056 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1057 {
1058 register rtx link, set;
1059 register int win = 0;
1060 register rtx r0, r1;
1061 int combined_regno = -1;
1062 int i;
1063
1064 this_insn_number = insn_number;
1065 this_insn = insn;
1066
1067 extract_insn (insn);
1068 which_alternative = -1;
1069
1070 /* Is this insn suitable for tying two registers?
1071 If so, try doing that.
1072 Suitable insns are those with at least two operands and where
1073 operand 0 is an output that is a register that is not
1074 earlyclobber.
1075
1076 We can tie operand 0 with some operand that dies in this insn.
1077 First look for operands that are required to be in the same
1078 register as operand 0. If we find such, only try tying that
1079 operand or one that can be put into that operand if the
1080 operation is commutative. If we don't find an operand
1081 that is required to be in the same register as operand 0,
1082 we can tie with any operand.
1083
1084 Subregs in place of regs are also ok.
1085
1086 If tying is done, WIN is set nonzero. */
1087
1088 if (1
1089 #ifdef REGISTER_CONSTRAINTS
1090 && recog_n_operands > 1
1091 && recog_constraints[0][0] == '='
1092 && recog_constraints[0][1] != '&'
1093 #else
1094 && GET_CODE (PATTERN (insn)) == SET
1095 && rtx_equal_p (SET_DEST (PATTERN (insn)), recog_operand[0])
1096 #endif
1097 )
1098 {
1099 #ifdef REGISTER_CONSTRAINTS
1100 /* If non-negative, is an operand that must match operand 0. */
1101 int must_match_0 = -1;
1102 /* Counts number of alternatives that require a match with
1103 operand 0. */
1104 int n_matching_alts = 0;
1105
1106 for (i = 1; i < recog_n_operands; i++)
1107 {
1108 char *p = recog_constraints[i];
1109 int this_match = (requires_inout (p));
1110
1111 n_matching_alts += this_match;
1112 if (this_match == recog_n_alternatives)
1113 must_match_0 = i;
1114 }
1115 #endif
1116
1117 r0 = recog_operand[0];
1118 for (i = 1; i < recog_n_operands; i++)
1119 {
1120 #ifdef REGISTER_CONSTRAINTS
1121 /* Skip this operand if we found an operand that
1122 must match operand 0 and this operand isn't it
1123 and can't be made to be it by commutativity. */
1124
1125 if (must_match_0 >= 0 && i != must_match_0
1126 && ! (i == must_match_0 + 1
1127 && recog_constraints[i-1][0] == '%')
1128 && ! (i == must_match_0 - 1
1129 && recog_constraints[i][0] == '%'))
1130 continue;
1131
1132 /* Likewise if each alternative has some operand that
1133 must match operand zero. In that case, skip any
1134 operand that doesn't list operand 0 since we know that
1135 the operand always conflicts with operand 0. We
1136 ignore commutatity in this case to keep things simple. */
1137 if (n_matching_alts == recog_n_alternatives
1138 && 0 == requires_inout (recog_constraints[i]))
1139 continue;
1140 #endif
1141
1142 r1 = recog_operand[i];
1143
1144 /* If the operand is an address, find a register in it.
1145 There may be more than one register, but we only try one
1146 of them. */
1147 if (
1148 #ifdef REGISTER_CONSTRAINTS
1149 recog_constraints[i][0] == 'p'
1150 #else
1151 recog_operand_address_p[i]
1152 #endif
1153 )
1154 while (GET_CODE (r1) == PLUS || GET_CODE (r1) == MULT)
1155 r1 = XEXP (r1, 0);
1156
1157 if (GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
1158 {
1159 /* We have two priorities for hard register preferences.
1160 If we have a move insn or an insn whose first input
1161 can only be in the same register as the output, give
1162 priority to an equivalence found from that insn. */
1163 int may_save_copy
1164 = ((SET_DEST (body) == r0 && SET_SRC (body) == r1)
1165 #ifdef REGISTER_CONSTRAINTS
1166 || (r1 == recog_operand[i] && must_match_0 >= 0)
1167 #endif
1168 );
1169
1170 if (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1171 win = combine_regs (r1, r0, may_save_copy,
1172 insn_number, insn, 0);
1173 }
1174 if (win)
1175 break;
1176 }
1177 }
1178
1179 /* Recognize an insn sequence with an ultimate result
1180 which can safely overlap one of the inputs.
1181 The sequence begins with a CLOBBER of its result,
1182 and ends with an insn that copies the result to itself
1183 and has a REG_EQUAL note for an equivalent formula.
1184 That note indicates what the inputs are.
1185 The result and the input can overlap if each insn in
1186 the sequence either doesn't mention the input
1187 or has a REG_NO_CONFLICT note to inhibit the conflict.
1188
1189 We do the combining test at the CLOBBER so that the
1190 destination register won't have had a quantity number
1191 assigned, since that would prevent combining. */
1192
1193 if (GET_CODE (PATTERN (insn)) == CLOBBER
1194 && (r0 = XEXP (PATTERN (insn), 0),
1195 GET_CODE (r0) == REG)
1196 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1197 && XEXP (link, 0) != 0
1198 && GET_CODE (XEXP (link, 0)) == INSN
1199 && (set = single_set (XEXP (link, 0))) != 0
1200 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1201 && (note = find_reg_note (XEXP (link, 0), REG_EQUAL,
1202 NULL_RTX)) != 0)
1203 {
1204 if (r1 = XEXP (note, 0), GET_CODE (r1) == REG
1205 /* Check that we have such a sequence. */
1206 && no_conflict_p (insn, r0, r1))
1207 win = combine_regs (r1, r0, 1, insn_number, insn, 1);
1208 else if (GET_RTX_FORMAT (GET_CODE (XEXP (note, 0)))[0] == 'e'
1209 && (r1 = XEXP (XEXP (note, 0), 0),
1210 GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1211 && no_conflict_p (insn, r0, r1))
1212 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1213
1214 /* Here we care if the operation to be computed is
1215 commutative. */
1216 else if ((GET_CODE (XEXP (note, 0)) == EQ
1217 || GET_CODE (XEXP (note, 0)) == NE
1218 || GET_RTX_CLASS (GET_CODE (XEXP (note, 0))) == 'c')
1219 && (r1 = XEXP (XEXP (note, 0), 1),
1220 (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
1221 && no_conflict_p (insn, r0, r1))
1222 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1223
1224 /* If we did combine something, show the register number
1225 in question so that we know to ignore its death. */
1226 if (win)
1227 no_conflict_combined_regno = REGNO (r1);
1228 }
1229
1230 /* If registers were just tied, set COMBINED_REGNO
1231 to the number of the register used in this insn
1232 that was tied to the register set in this insn.
1233 This register's qty should not be "killed". */
1234
1235 if (win)
1236 {
1237 while (GET_CODE (r1) == SUBREG)
1238 r1 = SUBREG_REG (r1);
1239 combined_regno = REGNO (r1);
1240 }
1241
1242 /* Mark the death of everything that dies in this instruction,
1243 except for anything that was just combined. */
1244
1245 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1246 if (REG_NOTE_KIND (link) == REG_DEAD
1247 && GET_CODE (XEXP (link, 0)) == REG
1248 && combined_regno != REGNO (XEXP (link, 0))
1249 && (no_conflict_combined_regno != REGNO (XEXP (link, 0))
1250 || ! find_reg_note (insn, REG_NO_CONFLICT, XEXP (link, 0))))
1251 wipe_dead_reg (XEXP (link, 0), 0);
1252
1253 /* Allocate qty numbers for all registers local to this block
1254 that are born (set) in this instruction.
1255 A pseudo that already has a qty is not changed. */
1256
1257 note_stores (PATTERN (insn), reg_is_set);
1258
1259 /* If anything is set in this insn and then unused, mark it as dying
1260 after this insn, so it will conflict with our outputs. This
1261 can't match with something that combined, and it doesn't matter
1262 if it did. Do this after the calls to reg_is_set since these
1263 die after, not during, the current insn. */
1264
1265 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1266 if (REG_NOTE_KIND (link) == REG_UNUSED
1267 && GET_CODE (XEXP (link, 0)) == REG)
1268 wipe_dead_reg (XEXP (link, 0), 1);
1269
1270 /* If this is an insn that has a REG_RETVAL note pointing at a
1271 CLOBBER insn, we have reached the end of a REG_NO_CONFLICT
1272 block, so clear any register number that combined within it. */
1273 if ((note = find_reg_note (insn, REG_RETVAL, NULL_RTX)) != 0
1274 && GET_CODE (XEXP (note, 0)) == INSN
1275 && GET_CODE (PATTERN (XEXP (note, 0))) == CLOBBER)
1276 no_conflict_combined_regno = -1;
1277 }
1278
1279 /* Set the registers live after INSN_NUMBER. Note that we never
1280 record the registers live before the block's first insn, since no
1281 pseudos we care about are live before that insn. */
1282
1283 IOR_HARD_REG_SET (regs_live_at[2 * insn_number], regs_live);
1284 IOR_HARD_REG_SET (regs_live_at[2 * insn_number + 1], regs_live);
1285
1286 if (insn == BLOCK_END (b))
1287 break;
1288
1289 insn = NEXT_INSN (insn);
1290 }
1291
1292 /* Now every register that is local to this basic block
1293 should have been given a quantity, or else -1 meaning ignore it.
1294 Every quantity should have a known birth and death.
1295
1296 Order the qtys so we assign them registers in order of the
1297 number of suggested registers they need so we allocate those with
1298 the most restrictive needs first. */
1299
1300 qty_order = (int *) alloca (next_qty * sizeof (int));
1301 for (i = 0; i < next_qty; i++)
1302 qty_order[i] = i;
1303
1304 #define EXCHANGE(I1, I2) \
1305 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1306
1307 switch (next_qty)
1308 {
1309 case 3:
1310 /* Make qty_order[2] be the one to allocate last. */
1311 if (qty_sugg_compare (0, 1) > 0)
1312 EXCHANGE (0, 1);
1313 if (qty_sugg_compare (1, 2) > 0)
1314 EXCHANGE (2, 1);
1315
1316 /* ... Fall through ... */
1317 case 2:
1318 /* Put the best one to allocate in qty_order[0]. */
1319 if (qty_sugg_compare (0, 1) > 0)
1320 EXCHANGE (0, 1);
1321
1322 /* ... Fall through ... */
1323
1324 case 1:
1325 case 0:
1326 /* Nothing to do here. */
1327 break;
1328
1329 default:
1330 qsort (qty_order, next_qty, sizeof (int), qty_sugg_compare_1);
1331 }
1332
1333 /* Try to put each quantity in a suggested physical register, if it has one.
1334 This may cause registers to be allocated that otherwise wouldn't be, but
1335 this seems acceptable in local allocation (unlike global allocation). */
1336 for (i = 0; i < next_qty; i++)
1337 {
1338 q = qty_order[i];
1339 if (qty_phys_num_sugg[q] != 0 || qty_phys_num_copy_sugg[q] != 0)
1340 qty_phys_reg[q] = find_free_reg (qty_min_class[q], qty_mode[q], q,
1341 0, 1, qty_birth[q], qty_death[q]);
1342 else
1343 qty_phys_reg[q] = -1;
1344 }
1345
1346 /* Order the qtys so we assign them registers in order of
1347 decreasing length of life. Normally call qsort, but if we
1348 have only a very small number of quantities, sort them ourselves. */
1349
1350 for (i = 0; i < next_qty; i++)
1351 qty_order[i] = i;
1352
1353 #define EXCHANGE(I1, I2) \
1354 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1355
1356 switch (next_qty)
1357 {
1358 case 3:
1359 /* Make qty_order[2] be the one to allocate last. */
1360 if (qty_compare (0, 1) > 0)
1361 EXCHANGE (0, 1);
1362 if (qty_compare (1, 2) > 0)
1363 EXCHANGE (2, 1);
1364
1365 /* ... Fall through ... */
1366 case 2:
1367 /* Put the best one to allocate in qty_order[0]. */
1368 if (qty_compare (0, 1) > 0)
1369 EXCHANGE (0, 1);
1370
1371 /* ... Fall through ... */
1372
1373 case 1:
1374 case 0:
1375 /* Nothing to do here. */
1376 break;
1377
1378 default:
1379 qsort (qty_order, next_qty, sizeof (int), qty_compare_1);
1380 }
1381
1382 /* Now for each qty that is not a hardware register,
1383 look for a hardware register to put it in.
1384 First try the register class that is cheapest for this qty,
1385 if there is more than one class. */
1386
1387 for (i = 0; i < next_qty; i++)
1388 {
1389 q = qty_order[i];
1390 if (qty_phys_reg[q] < 0)
1391 {
1392 #ifdef INSN_SCHEDULING
1393 /* These values represent the adjusted lifetime of a qty so
1394 that it conflicts with qtys which appear near the start/end
1395 of this qty's lifetime.
1396
1397 The purpose behind extending the lifetime of this qty is to
1398 discourage the register allocator from creating false
1399 dependencies.
1400
1401 The adjustment value is choosen to indicate that this qty
1402 conflicts with all the qtys in the instructions immediately
1403 before and after the lifetime of this qty.
1404
1405 Experiments have shown that higher values tend to hurt
1406 overall code performance.
1407
1408 If allocation using the extended lifetime fails we will try
1409 again with the qty's unadjusted lifetime. */
1410 int fake_birth = MAX (0, qty_birth[q] - 2 + qty_birth[q] % 2);
1411 int fake_death = MIN (insn_number * 2 + 1,
1412 qty_death[q] + 2 - qty_death[q] % 2);
1413 #endif
1414
1415 if (N_REG_CLASSES > 1)
1416 {
1417 #ifdef INSN_SCHEDULING
1418 /* We try to avoid using hard registers allocated to qtys which
1419 are born immediately after this qty or die immediately before
1420 this qty.
1421
1422 This optimization is only appropriate when we will run
1423 a scheduling pass after reload and we are not optimizing
1424 for code size. */
1425 if (flag_schedule_insns_after_reload
1426 && !optimize_size
1427 && !SMALL_REGISTER_CLASSES)
1428 {
1429
1430 qty_phys_reg[q] = find_free_reg (qty_min_class[q],
1431 qty_mode[q], q, 0, 0,
1432 fake_birth, fake_death);
1433 if (qty_phys_reg[q] >= 0)
1434 continue;
1435 }
1436 #endif
1437 qty_phys_reg[q] = find_free_reg (qty_min_class[q],
1438 qty_mode[q], q, 0, 0,
1439 qty_birth[q], qty_death[q]);
1440 if (qty_phys_reg[q] >= 0)
1441 continue;
1442 }
1443
1444 #ifdef INSN_SCHEDULING
1445 /* Similarly, avoid false dependencies. */
1446 if (flag_schedule_insns_after_reload
1447 && !optimize_size
1448 && !SMALL_REGISTER_CLASSES
1449 && qty_alternate_class[q] != NO_REGS)
1450 qty_phys_reg[q] = find_free_reg (qty_alternate_class[q],
1451 qty_mode[q], q, 0, 0,
1452 fake_birth, fake_death);
1453 #endif
1454 if (qty_alternate_class[q] != NO_REGS)
1455 qty_phys_reg[q] = find_free_reg (qty_alternate_class[q],
1456 qty_mode[q], q, 0, 0,
1457 qty_birth[q], qty_death[q]);
1458 }
1459 }
1460
1461 /* Now propagate the register assignments
1462 to the pseudo regs belonging to the qtys. */
1463
1464 for (q = 0; q < next_qty; q++)
1465 if (qty_phys_reg[q] >= 0)
1466 {
1467 for (i = qty_first_reg[q]; i >= 0; i = reg_next_in_qty[i])
1468 reg_renumber[i] = qty_phys_reg[q] + reg_offset[i];
1469 }
1470 }
1471 \f
1472 /* Compare two quantities' priority for getting real registers.
1473 We give shorter-lived quantities higher priority.
1474 Quantities with more references are also preferred, as are quantities that
1475 require multiple registers. This is the identical prioritization as
1476 done by global-alloc.
1477
1478 We used to give preference to registers with *longer* lives, but using
1479 the same algorithm in both local- and global-alloc can speed up execution
1480 of some programs by as much as a factor of three! */
1481
1482 /* Note that the quotient will never be bigger than
1483 the value of floor_log2 times the maximum number of
1484 times a register can occur in one insn (surely less than 100).
1485 Multiplying this by 10000 can't overflow.
1486 QTY_CMP_PRI is also used by qty_sugg_compare. */
1487
1488 #define QTY_CMP_PRI(q) \
1489 ((int) (((double) (floor_log2 (qty_n_refs[q]) * qty_n_refs[q] * qty_size[q]) \
1490 / (qty_death[q] - qty_birth[q])) * 10000))
1491
1492 static int
1493 qty_compare (q1, q2)
1494 int q1, q2;
1495 {
1496 return QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1497 }
1498
1499 static int
1500 qty_compare_1 (q1p, q2p)
1501 const GENERIC_PTR q1p;
1502 const GENERIC_PTR q2p;
1503 {
1504 register int q1 = *(int *)q1p, q2 = *(int *)q2p;
1505 register int tem = QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1506
1507 if (tem != 0)
1508 return tem;
1509
1510 /* If qtys are equally good, sort by qty number,
1511 so that the results of qsort leave nothing to chance. */
1512 return q1 - q2;
1513 }
1514 \f
1515 /* Compare two quantities' priority for getting real registers. This version
1516 is called for quantities that have suggested hard registers. First priority
1517 goes to quantities that have copy preferences, then to those that have
1518 normal preferences. Within those groups, quantities with the lower
1519 number of preferences have the highest priority. Of those, we use the same
1520 algorithm as above. */
1521
1522 #define QTY_CMP_SUGG(q) \
1523 (qty_phys_num_copy_sugg[q] \
1524 ? qty_phys_num_copy_sugg[q] \
1525 : qty_phys_num_sugg[q] * FIRST_PSEUDO_REGISTER)
1526
1527 static int
1528 qty_sugg_compare (q1, q2)
1529 int q1, q2;
1530 {
1531 register int tem = QTY_CMP_SUGG (q1) - QTY_CMP_SUGG (q2);
1532
1533 if (tem != 0)
1534 return tem;
1535
1536 return QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1537 }
1538
1539 static int
1540 qty_sugg_compare_1 (q1p, q2p)
1541 const GENERIC_PTR q1p;
1542 const GENERIC_PTR q2p;
1543 {
1544 register int q1 = *(int *)q1p, q2 = *(int *)q2p;
1545 register int tem = QTY_CMP_SUGG (q1) - QTY_CMP_SUGG (q2);
1546
1547 if (tem != 0)
1548 return tem;
1549
1550 tem = QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1551 if (tem != 0)
1552 return tem;
1553
1554 /* If qtys are equally good, sort by qty number,
1555 so that the results of qsort leave nothing to chance. */
1556 return q1 - q2;
1557 }
1558
1559 #undef QTY_CMP_SUGG
1560 #undef QTY_CMP_PRI
1561 \f
1562 /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
1563 Returns 1 if have done so, or 0 if cannot.
1564
1565 Combining registers means marking them as having the same quantity
1566 and adjusting the offsets within the quantity if either of
1567 them is a SUBREG).
1568
1569 We don't actually combine a hard reg with a pseudo; instead
1570 we just record the hard reg as the suggestion for the pseudo's quantity.
1571 If we really combined them, we could lose if the pseudo lives
1572 across an insn that clobbers the hard reg (eg, movstr).
1573
1574 ALREADY_DEAD is non-zero if USEDREG is known to be dead even though
1575 there is no REG_DEAD note on INSN. This occurs during the processing
1576 of REG_NO_CONFLICT blocks.
1577
1578 MAY_SAVE_COPYCOPY is non-zero if this insn is simply copying USEDREG to
1579 SETREG or if the input and output must share a register.
1580 In that case, we record a hard reg suggestion in QTY_PHYS_COPY_SUGG.
1581
1582 There are elaborate checks for the validity of combining. */
1583
1584
1585 static int
1586 combine_regs (usedreg, setreg, may_save_copy, insn_number, insn, already_dead)
1587 rtx usedreg, setreg;
1588 int may_save_copy;
1589 int insn_number;
1590 rtx insn;
1591 int already_dead;
1592 {
1593 register int ureg, sreg;
1594 register int offset = 0;
1595 int usize, ssize;
1596 register int sqty;
1597
1598 /* Determine the numbers and sizes of registers being used. If a subreg
1599 is present that does not change the entire register, don't consider
1600 this a copy insn. */
1601
1602 while (GET_CODE (usedreg) == SUBREG)
1603 {
1604 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (usedreg))) > UNITS_PER_WORD)
1605 may_save_copy = 0;
1606 offset += SUBREG_WORD (usedreg);
1607 usedreg = SUBREG_REG (usedreg);
1608 }
1609 if (GET_CODE (usedreg) != REG)
1610 return 0;
1611 ureg = REGNO (usedreg);
1612 usize = REG_SIZE (usedreg);
1613
1614 while (GET_CODE (setreg) == SUBREG)
1615 {
1616 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (setreg))) > UNITS_PER_WORD)
1617 may_save_copy = 0;
1618 offset -= SUBREG_WORD (setreg);
1619 setreg = SUBREG_REG (setreg);
1620 }
1621 if (GET_CODE (setreg) != REG)
1622 return 0;
1623 sreg = REGNO (setreg);
1624 ssize = REG_SIZE (setreg);
1625
1626 /* If UREG is a pseudo-register that hasn't already been assigned a
1627 quantity number, it means that it is not local to this block or dies
1628 more than once. In either event, we can't do anything with it. */
1629 if ((ureg >= FIRST_PSEUDO_REGISTER && reg_qty[ureg] < 0)
1630 /* Do not combine registers unless one fits within the other. */
1631 || (offset > 0 && usize + offset > ssize)
1632 || (offset < 0 && usize + offset < ssize)
1633 /* Do not combine with a smaller already-assigned object
1634 if that smaller object is already combined with something bigger. */
1635 || (ssize > usize && ureg >= FIRST_PSEUDO_REGISTER
1636 && usize < qty_size[reg_qty[ureg]])
1637 /* Can't combine if SREG is not a register we can allocate. */
1638 || (sreg >= FIRST_PSEUDO_REGISTER && reg_qty[sreg] == -1)
1639 /* Don't combine with a pseudo mentioned in a REG_NO_CONFLICT note.
1640 These have already been taken care of. This probably wouldn't
1641 combine anyway, but don't take any chances. */
1642 || (ureg >= FIRST_PSEUDO_REGISTER
1643 && find_reg_note (insn, REG_NO_CONFLICT, usedreg))
1644 /* Don't tie something to itself. In most cases it would make no
1645 difference, but it would screw up if the reg being tied to itself
1646 also dies in this insn. */
1647 || ureg == sreg
1648 /* Don't try to connect two different hardware registers. */
1649 || (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
1650 /* Don't connect two different machine modes if they have different
1651 implications as to which registers may be used. */
1652 || !MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
1653 return 0;
1654
1655 /* Now, if UREG is a hard reg and SREG is a pseudo, record the hard reg in
1656 qty_phys_sugg for the pseudo instead of tying them.
1657
1658 Return "failure" so that the lifespan of UREG is terminated here;
1659 that way the two lifespans will be disjoint and nothing will prevent
1660 the pseudo reg from being given this hard reg. */
1661
1662 if (ureg < FIRST_PSEUDO_REGISTER)
1663 {
1664 /* Allocate a quantity number so we have a place to put our
1665 suggestions. */
1666 if (reg_qty[sreg] == -2)
1667 reg_is_born (setreg, 2 * insn_number);
1668
1669 if (reg_qty[sreg] >= 0)
1670 {
1671 if (may_save_copy
1672 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg))
1673 {
1674 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg);
1675 qty_phys_num_copy_sugg[reg_qty[sreg]]++;
1676 }
1677 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg))
1678 {
1679 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg);
1680 qty_phys_num_sugg[reg_qty[sreg]]++;
1681 }
1682 }
1683 return 0;
1684 }
1685
1686 /* Similarly for SREG a hard register and UREG a pseudo register. */
1687
1688 if (sreg < FIRST_PSEUDO_REGISTER)
1689 {
1690 if (may_save_copy
1691 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg))
1692 {
1693 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg);
1694 qty_phys_num_copy_sugg[reg_qty[ureg]]++;
1695 }
1696 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg))
1697 {
1698 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg);
1699 qty_phys_num_sugg[reg_qty[ureg]]++;
1700 }
1701 return 0;
1702 }
1703
1704 /* At this point we know that SREG and UREG are both pseudos.
1705 Do nothing if SREG already has a quantity or is a register that we
1706 don't allocate. */
1707 if (reg_qty[sreg] >= -1
1708 /* If we are not going to let any regs live across calls,
1709 don't tie a call-crossing reg to a non-call-crossing reg. */
1710 || (current_function_has_nonlocal_label
1711 && ((REG_N_CALLS_CROSSED (ureg) > 0)
1712 != (REG_N_CALLS_CROSSED (sreg) > 0))))
1713 return 0;
1714
1715 /* We don't already know about SREG, so tie it to UREG
1716 if this is the last use of UREG, provided the classes they want
1717 are compatible. */
1718
1719 if ((already_dead || find_regno_note (insn, REG_DEAD, ureg))
1720 && reg_meets_class_p (sreg, qty_min_class[reg_qty[ureg]]))
1721 {
1722 /* Add SREG to UREG's quantity. */
1723 sqty = reg_qty[ureg];
1724 reg_qty[sreg] = sqty;
1725 reg_offset[sreg] = reg_offset[ureg] + offset;
1726 reg_next_in_qty[sreg] = qty_first_reg[sqty];
1727 qty_first_reg[sqty] = sreg;
1728
1729 /* If SREG's reg class is smaller, set qty_min_class[SQTY]. */
1730 update_qty_class (sqty, sreg);
1731
1732 /* Update info about quantity SQTY. */
1733 qty_n_calls_crossed[sqty] += REG_N_CALLS_CROSSED (sreg);
1734 qty_n_refs[sqty] += REG_N_REFS (sreg);
1735 if (usize < ssize)
1736 {
1737 register int i;
1738
1739 for (i = qty_first_reg[sqty]; i >= 0; i = reg_next_in_qty[i])
1740 reg_offset[i] -= offset;
1741
1742 qty_size[sqty] = ssize;
1743 qty_mode[sqty] = GET_MODE (setreg);
1744 }
1745 }
1746 else
1747 return 0;
1748
1749 return 1;
1750 }
1751 \f
1752 /* Return 1 if the preferred class of REG allows it to be tied
1753 to a quantity or register whose class is CLASS.
1754 True if REG's reg class either contains or is contained in CLASS. */
1755
1756 static int
1757 reg_meets_class_p (reg, class)
1758 int reg;
1759 enum reg_class class;
1760 {
1761 register enum reg_class rclass = reg_preferred_class (reg);
1762 return (reg_class_subset_p (rclass, class)
1763 || reg_class_subset_p (class, rclass));
1764 }
1765
1766 /* Update the class of QTY assuming that REG is being tied to it. */
1767
1768 static void
1769 update_qty_class (qty, reg)
1770 int qty;
1771 int reg;
1772 {
1773 enum reg_class rclass = reg_preferred_class (reg);
1774 if (reg_class_subset_p (rclass, qty_min_class[qty]))
1775 qty_min_class[qty] = rclass;
1776
1777 rclass = reg_alternate_class (reg);
1778 if (reg_class_subset_p (rclass, qty_alternate_class[qty]))
1779 qty_alternate_class[qty] = rclass;
1780
1781 if (REG_CHANGES_SIZE (reg))
1782 qty_changes_size[qty] = 1;
1783 }
1784 \f
1785 /* Handle something which alters the value of an rtx REG.
1786
1787 REG is whatever is set or clobbered. SETTER is the rtx that
1788 is modifying the register.
1789
1790 If it is not really a register, we do nothing.
1791 The file-global variables `this_insn' and `this_insn_number'
1792 carry info from `block_alloc'. */
1793
1794 static void
1795 reg_is_set (reg, setter)
1796 rtx reg;
1797 rtx setter;
1798 {
1799 /* Note that note_stores will only pass us a SUBREG if it is a SUBREG of
1800 a hard register. These may actually not exist any more. */
1801
1802 if (GET_CODE (reg) != SUBREG
1803 && GET_CODE (reg) != REG)
1804 return;
1805
1806 /* Mark this register as being born. If it is used in a CLOBBER, mark
1807 it as being born halfway between the previous insn and this insn so that
1808 it conflicts with our inputs but not the outputs of the previous insn. */
1809
1810 reg_is_born (reg, 2 * this_insn_number - (GET_CODE (setter) == CLOBBER));
1811 }
1812 \f
1813 /* Handle beginning of the life of register REG.
1814 BIRTH is the index at which this is happening. */
1815
1816 static void
1817 reg_is_born (reg, birth)
1818 rtx reg;
1819 int birth;
1820 {
1821 register int regno;
1822
1823 if (GET_CODE (reg) == SUBREG)
1824 regno = REGNO (SUBREG_REG (reg)) + SUBREG_WORD (reg);
1825 else
1826 regno = REGNO (reg);
1827
1828 if (regno < FIRST_PSEUDO_REGISTER)
1829 {
1830 mark_life (regno, GET_MODE (reg), 1);
1831
1832 /* If the register was to have been born earlier that the present
1833 insn, mark it as live where it is actually born. */
1834 if (birth < 2 * this_insn_number)
1835 post_mark_life (regno, GET_MODE (reg), 1, birth, 2 * this_insn_number);
1836 }
1837 else
1838 {
1839 if (reg_qty[regno] == -2)
1840 alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), birth);
1841
1842 /* If this register has a quantity number, show that it isn't dead. */
1843 if (reg_qty[regno] >= 0)
1844 qty_death[reg_qty[regno]] = -1;
1845 }
1846 }
1847
1848 /* Record the death of REG in the current insn. If OUTPUT_P is non-zero,
1849 REG is an output that is dying (i.e., it is never used), otherwise it
1850 is an input (the normal case).
1851 If OUTPUT_P is 1, then we extend the life past the end of this insn. */
1852
1853 static void
1854 wipe_dead_reg (reg, output_p)
1855 register rtx reg;
1856 int output_p;
1857 {
1858 register int regno = REGNO (reg);
1859
1860 /* If this insn has multiple results,
1861 and the dead reg is used in one of the results,
1862 extend its life to after this insn,
1863 so it won't get allocated together with any other result of this insn.
1864
1865 It is unsafe to use !single_set here since it will ignore an unused
1866 output. Just because an output is unused does not mean the compiler
1867 can assume the side effect will not occur. Consider if REG appears
1868 in the address of an output and we reload the output. If we allocate
1869 REG to the same hard register as an unused output we could set the hard
1870 register before the output reload insn. */
1871 if (GET_CODE (PATTERN (this_insn)) == PARALLEL
1872 && multiple_sets (this_insn))
1873 {
1874 int i;
1875 for (i = XVECLEN (PATTERN (this_insn), 0) - 1; i >= 0; i--)
1876 {
1877 rtx set = XVECEXP (PATTERN (this_insn), 0, i);
1878 if (GET_CODE (set) == SET
1879 && GET_CODE (SET_DEST (set)) != REG
1880 && !rtx_equal_p (reg, SET_DEST (set))
1881 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
1882 output_p = 1;
1883 }
1884 }
1885
1886 /* If this register is used in an auto-increment address, then extend its
1887 life to after this insn, so that it won't get allocated together with
1888 the result of this insn. */
1889 if (! output_p && find_regno_note (this_insn, REG_INC, regno))
1890 output_p = 1;
1891
1892 if (regno < FIRST_PSEUDO_REGISTER)
1893 {
1894 mark_life (regno, GET_MODE (reg), 0);
1895
1896 /* If a hard register is dying as an output, mark it as in use at
1897 the beginning of this insn (the above statement would cause this
1898 not to happen). */
1899 if (output_p)
1900 post_mark_life (regno, GET_MODE (reg), 1,
1901 2 * this_insn_number, 2 * this_insn_number+ 1);
1902 }
1903
1904 else if (reg_qty[regno] >= 0)
1905 qty_death[reg_qty[regno]] = 2 * this_insn_number + output_p;
1906 }
1907 \f
1908 /* Find a block of SIZE words of hard regs in reg_class CLASS
1909 that can hold something of machine-mode MODE
1910 (but actually we test only the first of the block for holding MODE)
1911 and still free between insn BORN_INDEX and insn DEAD_INDEX,
1912 and return the number of the first of them.
1913 Return -1 if such a block cannot be found.
1914 If QTY crosses calls, insist on a register preserved by calls,
1915 unless ACCEPT_CALL_CLOBBERED is nonzero.
1916
1917 If JUST_TRY_SUGGESTED is non-zero, only try to see if the suggested
1918 register is available. If not, return -1. */
1919
1920 static int
1921 find_free_reg (class, mode, qty, accept_call_clobbered, just_try_suggested,
1922 born_index, dead_index)
1923 enum reg_class class;
1924 enum machine_mode mode;
1925 int qty;
1926 int accept_call_clobbered;
1927 int just_try_suggested;
1928 int born_index, dead_index;
1929 {
1930 register int i, ins;
1931 #ifdef HARD_REG_SET
1932 register /* Declare it register if it's a scalar. */
1933 #endif
1934 HARD_REG_SET used, first_used;
1935 #ifdef ELIMINABLE_REGS
1936 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
1937 #endif
1938
1939 /* Validate our parameters. */
1940 if (born_index < 0 || born_index > dead_index)
1941 abort ();
1942
1943 /* Don't let a pseudo live in a reg across a function call
1944 if we might get a nonlocal goto. */
1945 if (current_function_has_nonlocal_label
1946 && qty_n_calls_crossed[qty] > 0)
1947 return -1;
1948
1949 if (accept_call_clobbered)
1950 COPY_HARD_REG_SET (used, call_fixed_reg_set);
1951 else if (qty_n_calls_crossed[qty] == 0)
1952 COPY_HARD_REG_SET (used, fixed_reg_set);
1953 else
1954 COPY_HARD_REG_SET (used, call_used_reg_set);
1955
1956 if (accept_call_clobbered)
1957 IOR_HARD_REG_SET (used, losing_caller_save_reg_set);
1958
1959 for (ins = born_index; ins < dead_index; ins++)
1960 IOR_HARD_REG_SET (used, regs_live_at[ins]);
1961
1962 IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
1963
1964 /* Don't use the frame pointer reg in local-alloc even if
1965 we may omit the frame pointer, because if we do that and then we
1966 need a frame pointer, reload won't know how to move the pseudo
1967 to another hard reg. It can move only regs made by global-alloc.
1968
1969 This is true of any register that can be eliminated. */
1970 #ifdef ELIMINABLE_REGS
1971 for (i = 0; i < (int)(sizeof eliminables / sizeof eliminables[0]); i++)
1972 SET_HARD_REG_BIT (used, eliminables[i].from);
1973 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1974 /* If FRAME_POINTER_REGNUM is not a real register, then protect the one
1975 that it might be eliminated into. */
1976 SET_HARD_REG_BIT (used, HARD_FRAME_POINTER_REGNUM);
1977 #endif
1978 #else
1979 SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
1980 #endif
1981
1982 #ifdef CLASS_CANNOT_CHANGE_SIZE
1983 if (qty_changes_size[qty])
1984 IOR_HARD_REG_SET (used,
1985 reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
1986 #endif
1987
1988 /* Normally, the registers that can be used for the first register in
1989 a multi-register quantity are the same as those that can be used for
1990 subsequent registers. However, if just trying suggested registers,
1991 restrict our consideration to them. If there are copy-suggested
1992 register, try them. Otherwise, try the arithmetic-suggested
1993 registers. */
1994 COPY_HARD_REG_SET (first_used, used);
1995
1996 if (just_try_suggested)
1997 {
1998 if (qty_phys_num_copy_sugg[qty] != 0)
1999 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_copy_sugg[qty]);
2000 else
2001 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_sugg[qty]);
2002 }
2003
2004 /* If all registers are excluded, we can't do anything. */
2005 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) ALL_REGS], first_used, fail);
2006
2007 /* If at least one would be suitable, test each hard reg. */
2008
2009 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2010 {
2011 #ifdef REG_ALLOC_ORDER
2012 int regno = reg_alloc_order[i];
2013 #else
2014 int regno = i;
2015 #endif
2016 if (! TEST_HARD_REG_BIT (first_used, regno)
2017 && HARD_REGNO_MODE_OK (regno, mode)
2018 && (qty_n_calls_crossed[qty] == 0
2019 || accept_call_clobbered
2020 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
2021 {
2022 register int j;
2023 register int size1 = HARD_REGNO_NREGS (regno, mode);
2024 for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
2025 if (j == size1)
2026 {
2027 /* Mark that this register is in use between its birth and death
2028 insns. */
2029 post_mark_life (regno, mode, 1, born_index, dead_index);
2030 return regno;
2031 }
2032 #ifndef REG_ALLOC_ORDER
2033 i += j; /* Skip starting points we know will lose */
2034 #endif
2035 }
2036 }
2037
2038 fail:
2039
2040 /* If we are just trying suggested register, we have just tried copy-
2041 suggested registers, and there are arithmetic-suggested registers,
2042 try them. */
2043
2044 /* If it would be profitable to allocate a call-clobbered register
2045 and save and restore it around calls, do that. */
2046 if (just_try_suggested && qty_phys_num_copy_sugg[qty] != 0
2047 && qty_phys_num_sugg[qty] != 0)
2048 {
2049 /* Don't try the copy-suggested regs again. */
2050 qty_phys_num_copy_sugg[qty] = 0;
2051 return find_free_reg (class, mode, qty, accept_call_clobbered, 1,
2052 born_index, dead_index);
2053 }
2054
2055 /* We need not check to see if the current function has nonlocal
2056 labels because we don't put any pseudos that are live over calls in
2057 registers in that case. */
2058
2059 if (! accept_call_clobbered
2060 && flag_caller_saves
2061 && ! just_try_suggested
2062 && qty_n_calls_crossed[qty] != 0
2063 && CALLER_SAVE_PROFITABLE (qty_n_refs[qty], qty_n_calls_crossed[qty]))
2064 {
2065 i = find_free_reg (class, mode, qty, 1, 0, born_index, dead_index);
2066 if (i >= 0)
2067 caller_save_needed = 1;
2068 return i;
2069 }
2070 return -1;
2071 }
2072 \f
2073 /* Mark that REGNO with machine-mode MODE is live starting from the current
2074 insn (if LIFE is non-zero) or dead starting at the current insn (if LIFE
2075 is zero). */
2076
2077 static void
2078 mark_life (regno, mode, life)
2079 register int regno;
2080 enum machine_mode mode;
2081 int life;
2082 {
2083 register int j = HARD_REGNO_NREGS (regno, mode);
2084 if (life)
2085 while (--j >= 0)
2086 SET_HARD_REG_BIT (regs_live, regno + j);
2087 else
2088 while (--j >= 0)
2089 CLEAR_HARD_REG_BIT (regs_live, regno + j);
2090 }
2091
2092 /* Mark register number REGNO (with machine-mode MODE) as live (if LIFE
2093 is non-zero) or dead (if LIFE is zero) from insn number BIRTH (inclusive)
2094 to insn number DEATH (exclusive). */
2095
2096 static void
2097 post_mark_life (regno, mode, life, birth, death)
2098 int regno;
2099 enum machine_mode mode;
2100 int life, birth, death;
2101 {
2102 register int j = HARD_REGNO_NREGS (regno, mode);
2103 #ifdef HARD_REG_SET
2104 register /* Declare it register if it's a scalar. */
2105 #endif
2106 HARD_REG_SET this_reg;
2107
2108 CLEAR_HARD_REG_SET (this_reg);
2109 while (--j >= 0)
2110 SET_HARD_REG_BIT (this_reg, regno + j);
2111
2112 if (life)
2113 while (birth < death)
2114 {
2115 IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
2116 birth++;
2117 }
2118 else
2119 while (birth < death)
2120 {
2121 AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
2122 birth++;
2123 }
2124 }
2125 \f
2126 /* INSN is the CLOBBER insn that starts a REG_NO_NOCONFLICT block, R0
2127 is the register being clobbered, and R1 is a register being used in
2128 the equivalent expression.
2129
2130 If R1 dies in the block and has a REG_NO_CONFLICT note on every insn
2131 in which it is used, return 1.
2132
2133 Otherwise, return 0. */
2134
2135 static int
2136 no_conflict_p (insn, r0, r1)
2137 rtx insn, r0, r1;
2138 {
2139 int ok = 0;
2140 rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
2141 rtx p, last;
2142
2143 /* If R1 is a hard register, return 0 since we handle this case
2144 when we scan the insns that actually use it. */
2145
2146 if (note == 0
2147 || (GET_CODE (r1) == REG && REGNO (r1) < FIRST_PSEUDO_REGISTER)
2148 || (GET_CODE (r1) == SUBREG && GET_CODE (SUBREG_REG (r1)) == REG
2149 && REGNO (SUBREG_REG (r1)) < FIRST_PSEUDO_REGISTER))
2150 return 0;
2151
2152 last = XEXP (note, 0);
2153
2154 for (p = NEXT_INSN (insn); p && p != last; p = NEXT_INSN (p))
2155 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
2156 {
2157 if (find_reg_note (p, REG_DEAD, r1))
2158 ok = 1;
2159
2160 /* There must be a REG_NO_CONFLICT note on every insn, otherwise
2161 some earlier optimization pass has inserted instructions into
2162 the sequence, and it is not safe to perform this optimization.
2163 Note that emit_no_conflict_block always ensures that this is
2164 true when these sequences are created. */
2165 if (! find_reg_note (p, REG_NO_CONFLICT, r1))
2166 return 0;
2167 }
2168
2169 return ok;
2170 }
2171 \f
2172 #ifdef REGISTER_CONSTRAINTS
2173
2174 /* Return the number of alternatives for which the constraint string P
2175 indicates that the operand must be equal to operand 0 and that no register
2176 is acceptable. */
2177
2178 static int
2179 requires_inout (p)
2180 char *p;
2181 {
2182 char c;
2183 int found_zero = 0;
2184 int reg_allowed = 0;
2185 int num_matching_alts = 0;
2186
2187 while ((c = *p++))
2188 switch (c)
2189 {
2190 case '=': case '+': case '?':
2191 case '#': case '&': case '!':
2192 case '*': case '%':
2193 case '1': case '2': case '3': case '4':
2194 case 'm': case '<': case '>': case 'V': case 'o':
2195 case 'E': case 'F': case 'G': case 'H':
2196 case 's': case 'i': case 'n':
2197 case 'I': case 'J': case 'K': case 'L':
2198 case 'M': case 'N': case 'O': case 'P':
2199 #ifdef EXTRA_CONSTRAINT
2200 case 'Q': case 'R': case 'S': case 'T': case 'U':
2201 #endif
2202 case 'X':
2203 /* These don't say anything we care about. */
2204 break;
2205
2206 case ',':
2207 if (found_zero && ! reg_allowed)
2208 num_matching_alts++;
2209
2210 found_zero = reg_allowed = 0;
2211 break;
2212
2213 case '0':
2214 found_zero = 1;
2215 break;
2216
2217 case 'p':
2218 case 'g': case 'r':
2219 default:
2220 reg_allowed = 1;
2221 break;
2222 }
2223
2224 if (found_zero && ! reg_allowed)
2225 num_matching_alts++;
2226
2227 return num_matching_alts;
2228 }
2229 #endif /* REGISTER_CONSTRAINTS */
2230 \f
2231 void
2232 dump_local_alloc (file)
2233 FILE *file;
2234 {
2235 register int i;
2236 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2237 if (reg_renumber[i] != -1)
2238 fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);
2239 }