Add -fpic/-fPIC support
[official-gcc.git] / gcc / local-alloc.c
blobbde83b2e4c3f27ef2270dea11ef9840dd8b7ab1b
1 /* Allocate registers within a basic block, for GNU compiler.
2 Copyright (C) 1987, 88, 91, 93, 94, 95, 1996 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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.
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.
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. */
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.
30 The assignments made are recorded in the vector reg_renumber
31 whose space is allocated here. The rtl code itself is not altered.
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.
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.
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.
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. */
58 /* Pseudos allocated here cannot be reallocated by global.c if the hard
59 register is used as a spill register. So we don't allocate such pseudos
60 here if their preferred class is likely to be used by spills. */
62 #include <stdio.h>
63 #include "config.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 "recog.h"
71 #include "output.h"
73 /* Next quantity number available for allocation. */
75 static int next_qty;
77 /* In all the following vectors indexed by quantity number. */
79 /* Element Q is the hard reg number chosen for quantity Q,
80 or -1 if none was found. */
82 static short *qty_phys_reg;
84 /* We maintain two hard register sets that indicate suggested hard registers
85 for each quantity. The first, qty_phys_copy_sugg, contains hard registers
86 that are tied to the quantity by a simple copy. The second contains all
87 hard registers that are tied to the quantity via an arithmetic operation.
89 The former register set is given priority for allocation. This tends to
90 eliminate copy insns. */
92 /* Element Q is a set of hard registers that are suggested for quantity Q by
93 copy insns. */
95 static HARD_REG_SET *qty_phys_copy_sugg;
97 /* Element Q is a set of hard registers that are suggested for quantity Q by
98 arithmetic insns. */
100 static HARD_REG_SET *qty_phys_sugg;
102 /* Element Q is the number of suggested registers in qty_phys_copy_sugg. */
104 static short *qty_phys_num_copy_sugg;
106 /* Element Q is the number of suggested registers in qty_phys_sugg. */
108 static short *qty_phys_num_sugg;
110 /* Element Q is the number of refs to quantity Q. */
112 static int *qty_n_refs;
114 /* Element Q is a reg class contained in (smaller than) the
115 preferred classes of all the pseudo regs that are tied in quantity Q.
116 This is the preferred class for allocating that quantity. */
118 static enum reg_class *qty_min_class;
120 /* Insn number (counting from head of basic block)
121 where quantity Q was born. -1 if birth has not been recorded. */
123 static int *qty_birth;
125 /* Insn number (counting from head of basic block)
126 where quantity Q died. Due to the way tying is done,
127 and the fact that we consider in this pass only regs that die but once,
128 a quantity can die only once. Each quantity's life span
129 is a set of consecutive insns. -1 if death has not been recorded. */
131 static int *qty_death;
133 /* Number of words needed to hold the data in quantity Q.
134 This depends on its machine mode. It is used for these purposes:
135 1. It is used in computing the relative importances of qtys,
136 which determines the order in which we look for regs for them.
137 2. It is used in rules that prevent tying several registers of
138 different sizes in a way that is geometrically impossible
139 (see combine_regs). */
141 static int *qty_size;
143 /* This holds the mode of the registers that are tied to qty Q,
144 or VOIDmode if registers with differing modes are tied together. */
146 static enum machine_mode *qty_mode;
148 /* Number of times a reg tied to qty Q lives across a CALL_INSN. */
150 static int *qty_n_calls_crossed;
152 /* Register class within which we allocate qty Q if we can't get
153 its preferred class. */
155 static enum reg_class *qty_alternate_class;
157 /* Element Q is the SCRATCH expression for which this quantity is being
158 allocated or 0 if this quantity is allocating registers. */
160 static rtx *qty_scratch_rtx;
162 /* Element Q is nonzero if this quantity has been used in a SUBREG
163 that changes its size. */
165 static char *qty_changes_size;
167 /* Element Q is the register number of one pseudo register whose
168 reg_qty value is Q, or -1 is this quantity is for a SCRATCH. This
169 register should be the head of the chain maintained in reg_next_in_qty. */
171 static int *qty_first_reg;
173 /* If (REG N) has been assigned a quantity number, is a register number
174 of another register assigned the same quantity number, or -1 for the
175 end of the chain. qty_first_reg point to the head of this chain. */
177 static int *reg_next_in_qty;
179 /* reg_qty[N] (where N is a pseudo reg number) is the qty number of that reg
180 if it is >= 0,
181 of -1 if this register cannot be allocated by local-alloc,
182 or -2 if not known yet.
184 Note that if we see a use or death of pseudo register N with
185 reg_qty[N] == -2, register N must be local to the current block. If
186 it were used in more than one block, we would have reg_qty[N] == -1.
187 This relies on the fact that if reg_basic_block[N] is >= 0, register N
188 will not appear in any other block. We save a considerable number of
189 tests by exploiting this.
191 If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is undefined and should not
192 be referenced. */
194 static int *reg_qty;
196 /* The offset (in words) of register N within its quantity.
197 This can be nonzero if register N is SImode, and has been tied
198 to a subreg of a DImode register. */
200 static char *reg_offset;
202 /* Vector of substitutions of register numbers,
203 used to map pseudo regs into hardware regs.
204 This is set up as a result of register allocation.
205 Element N is the hard reg assigned to pseudo reg N,
206 or is -1 if no hard reg was assigned.
207 If N is a hard reg number, element N is N. */
209 short *reg_renumber;
211 /* Set of hard registers live at the current point in the scan
212 of the instructions in a basic block. */
214 static HARD_REG_SET regs_live;
216 /* Each set of hard registers indicates registers live at a particular
217 point in the basic block. For N even, regs_live_at[N] says which
218 hard registers are needed *after* insn N/2 (i.e., they may not
219 conflict with the outputs of insn N/2 or the inputs of insn N/2 + 1.
221 If an object is to conflict with the inputs of insn J but not the
222 outputs of insn J + 1, we say it is born at index J*2 - 1. Similarly,
223 if it is to conflict with the outputs of insn J but not the inputs of
224 insn J + 1, it is said to die at index J*2 + 1. */
226 static HARD_REG_SET *regs_live_at;
228 int *scratch_block;
229 rtx *scratch_list;
230 int scratch_list_length;
231 static int scratch_index;
233 /* Communicate local vars `insn_number' and `insn'
234 from `block_alloc' to `reg_is_set', `wipe_dead_reg', and `alloc_qty'. */
235 static int this_insn_number;
236 static rtx this_insn;
238 /* Used to communicate changes made by update_equiv_regs to
239 memref_referenced_p. */
240 static rtx *reg_equiv_replacement;
242 static void alloc_qty PROTO((int, enum machine_mode, int, int));
243 static void alloc_qty_for_scratch PROTO((rtx, int, rtx, int, int));
244 static void validate_equiv_mem_from_store PROTO((rtx, rtx));
245 static int validate_equiv_mem PROTO((rtx, rtx, rtx));
246 static int memref_referenced_p PROTO((rtx, rtx));
247 static int memref_used_between_p PROTO((rtx, rtx, rtx));
248 static void optimize_reg_copy_1 PROTO((rtx, rtx, rtx));
249 static void optimize_reg_copy_2 PROTO((rtx, rtx, rtx));
250 static void update_equiv_regs PROTO((void));
251 static void block_alloc PROTO((int));
252 static int qty_sugg_compare PROTO((int, int));
253 static int qty_sugg_compare_1 PROTO((int *, int *));
254 static int qty_compare PROTO((int, int));
255 static int qty_compare_1 PROTO((int *, int *));
256 static int combine_regs PROTO((rtx, rtx, int, int, rtx, int));
257 static int reg_meets_class_p PROTO((int, enum reg_class));
258 static int reg_classes_overlap_p PROTO((enum reg_class, enum reg_class,
259 int));
260 static void update_qty_class PROTO((int, int));
261 static void reg_is_set PROTO((rtx, rtx));
262 static void reg_is_born PROTO((rtx, int));
263 static void wipe_dead_reg PROTO((rtx, int));
264 static int find_free_reg PROTO((enum reg_class, enum machine_mode,
265 int, int, int, int, int));
266 static void mark_life PROTO((int, enum machine_mode, int));
267 static void post_mark_life PROTO((int, enum machine_mode, int, int, int));
268 static int no_conflict_p PROTO((rtx, rtx, rtx));
269 static int requires_inout PROTO((char *));
271 /* Allocate a new quantity (new within current basic block)
272 for register number REGNO which is born at index BIRTH
273 within the block. MODE and SIZE are info on reg REGNO. */
275 static void
276 alloc_qty (regno, mode, size, birth)
277 int regno;
278 enum machine_mode mode;
279 int size, birth;
281 register int qty = next_qty++;
283 reg_qty[regno] = qty;
284 reg_offset[regno] = 0;
285 reg_next_in_qty[regno] = -1;
287 qty_first_reg[qty] = regno;
288 qty_size[qty] = size;
289 qty_mode[qty] = mode;
290 qty_birth[qty] = birth;
291 qty_n_calls_crossed[qty] = reg_n_calls_crossed[regno];
292 qty_min_class[qty] = reg_preferred_class (regno);
293 qty_alternate_class[qty] = reg_alternate_class (regno);
294 qty_n_refs[qty] = reg_n_refs[regno];
295 qty_changes_size[qty] = reg_changes_size[regno];
298 /* Similar to `alloc_qty', but allocates a quantity for a SCRATCH rtx
299 used as operand N in INSN. We assume here that the SCRATCH is used in
300 a CLOBBER. */
302 static void
303 alloc_qty_for_scratch (scratch, n, insn, insn_code_num, insn_number)
304 rtx scratch;
305 int n;
306 rtx insn;
307 int insn_code_num, insn_number;
309 register int qty;
310 enum reg_class class;
311 char *p, c;
312 int i;
314 #ifdef REGISTER_CONSTRAINTS
315 /* If we haven't yet computed which alternative will be used, do so now.
316 Then set P to the constraints for that alternative. */
317 if (which_alternative == -1)
318 if (! constrain_operands (insn_code_num, 0))
319 return;
321 for (p = insn_operand_constraint[insn_code_num][n], i = 0;
322 *p && i < which_alternative; p++)
323 if (*p == ',')
324 i++;
326 /* Compute the class required for this SCRATCH. If we don't need a
327 register, the class will remain NO_REGS. If we guessed the alternative
328 number incorrectly, reload will fix things up for us. */
330 class = NO_REGS;
331 while ((c = *p++) != '\0' && c != ',')
332 switch (c)
334 case '=': case '+': case '?':
335 case '#': case '&': case '!':
336 case '*': case '%':
337 case '0': case '1': case '2': case '3': case '4':
338 case 'm': case '<': case '>': case 'V': case 'o':
339 case 'E': case 'F': case 'G': case 'H':
340 case 's': case 'i': case 'n':
341 case 'I': case 'J': case 'K': case 'L':
342 case 'M': case 'N': case 'O': case 'P':
343 #ifdef EXTRA_CONSTRAINT
344 case 'Q': case 'R': case 'S': case 'T': case 'U':
345 #endif
346 case 'p':
347 /* These don't say anything we care about. */
348 break;
350 case 'X':
351 /* We don't need to allocate this SCRATCH. */
352 return;
354 case 'g': case 'r':
355 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
356 break;
358 default:
359 class
360 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER (c)];
361 break;
364 if (class == NO_REGS)
365 return;
367 #else /* REGISTER_CONSTRAINTS */
369 class = GENERAL_REGS;
370 #endif
373 qty = next_qty++;
375 qty_first_reg[qty] = -1;
376 qty_scratch_rtx[qty] = scratch;
377 qty_size[qty] = GET_MODE_SIZE (GET_MODE (scratch));
378 qty_mode[qty] = GET_MODE (scratch);
379 qty_birth[qty] = 2 * insn_number - 1;
380 qty_death[qty] = 2 * insn_number + 1;
381 qty_n_calls_crossed[qty] = 0;
382 qty_min_class[qty] = class;
383 qty_alternate_class[qty] = NO_REGS;
384 qty_n_refs[qty] = 1;
385 qty_changes_size[qty] = 0;
388 /* Main entry point of this file. */
390 void
391 local_alloc ()
393 register int b, i;
394 int max_qty;
396 /* Leaf functions and non-leaf functions have different needs.
397 If defined, let the machine say what kind of ordering we
398 should use. */
399 #ifdef ORDER_REGS_FOR_LOCAL_ALLOC
400 ORDER_REGS_FOR_LOCAL_ALLOC;
401 #endif
403 /* Promote REG_EQUAL notes to REG_EQUIV notes and adjust status of affected
404 registers. */
405 update_equiv_regs ();
407 /* This sets the maximum number of quantities we can have. Quantity
408 numbers start at zero and we can have one for each pseudo plus the
409 number of SCRATCHes in the largest block, in the worst case. */
410 max_qty = (max_regno - FIRST_PSEUDO_REGISTER) + max_scratch;
412 /* Allocate vectors of temporary data.
413 See the declarations of these variables, above,
414 for what they mean. */
416 /* There can be up to MAX_SCRATCH * N_BASIC_BLOCKS SCRATCHes to allocate.
417 Instead of allocating this much memory from now until the end of
418 reload, only allocate space for MAX_QTY SCRATCHes. If there are more
419 reload will allocate them. */
421 scratch_list_length = max_qty;
422 scratch_list = (rtx *) xmalloc (scratch_list_length * sizeof (rtx));
423 bzero ((char *) scratch_list, scratch_list_length * sizeof (rtx));
424 scratch_block = (int *) xmalloc (scratch_list_length * sizeof (int));
425 bzero ((char *) scratch_block, scratch_list_length * sizeof (int));
426 scratch_index = 0;
428 qty_phys_reg = (short *) alloca (max_qty * sizeof (short));
429 qty_phys_copy_sugg
430 = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
431 qty_phys_num_copy_sugg = (short *) alloca (max_qty * sizeof (short));
432 qty_phys_sugg = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
433 qty_phys_num_sugg = (short *) alloca (max_qty * sizeof (short));
434 qty_birth = (int *) alloca (max_qty * sizeof (int));
435 qty_death = (int *) alloca (max_qty * sizeof (int));
436 qty_scratch_rtx = (rtx *) alloca (max_qty * sizeof (rtx));
437 qty_first_reg = (int *) alloca (max_qty * sizeof (int));
438 qty_size = (int *) alloca (max_qty * sizeof (int));
439 qty_mode
440 = (enum machine_mode *) alloca (max_qty * sizeof (enum machine_mode));
441 qty_n_calls_crossed = (int *) alloca (max_qty * sizeof (int));
442 qty_min_class
443 = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
444 qty_alternate_class
445 = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
446 qty_n_refs = (int *) alloca (max_qty * sizeof (int));
447 qty_changes_size = (char *) alloca (max_qty * sizeof (char));
449 reg_qty = (int *) alloca (max_regno * sizeof (int));
450 reg_offset = (char *) alloca (max_regno * sizeof (char));
451 reg_next_in_qty = (int *) alloca (max_regno * sizeof (int));
453 reg_renumber = (short *) oballoc (max_regno * sizeof (short));
454 for (i = 0; i < max_regno; i++)
455 reg_renumber[i] = -1;
457 /* Determine which pseudo-registers can be allocated by local-alloc.
458 In general, these are the registers used only in a single block and
459 which only die once. However, if a register's preferred class has only
460 a few entries, don't allocate this register here unless it is preferred
461 or nothing since retry_global_alloc won't be able to move it to
462 GENERAL_REGS if a reload register of this class is needed.
464 We need not be concerned with which block actually uses the register
465 since we will never see it outside that block. */
467 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
469 if (reg_basic_block[i] >= 0 && reg_n_deaths[i] == 1
470 && (reg_alternate_class (i) == NO_REGS
471 || ! CLASS_LIKELY_SPILLED_P (reg_preferred_class (i))))
472 reg_qty[i] = -2;
473 else
474 reg_qty[i] = -1;
477 /* Force loop below to initialize entire quantity array. */
478 next_qty = max_qty;
480 /* Allocate each block's local registers, block by block. */
482 for (b = 0; b < n_basic_blocks; b++)
484 /* NEXT_QTY indicates which elements of the `qty_...'
485 vectors might need to be initialized because they were used
486 for the previous block; it is set to the entire array before
487 block 0. Initialize those, with explicit loop if there are few,
488 else with bzero and bcopy. Do not initialize vectors that are
489 explicit set by `alloc_qty'. */
491 if (next_qty < 6)
493 for (i = 0; i < next_qty; i++)
495 qty_scratch_rtx[i] = 0;
496 CLEAR_HARD_REG_SET (qty_phys_copy_sugg[i]);
497 qty_phys_num_copy_sugg[i] = 0;
498 CLEAR_HARD_REG_SET (qty_phys_sugg[i]);
499 qty_phys_num_sugg[i] = 0;
502 else
504 #define CLEAR(vector) \
505 bzero ((char *) (vector), (sizeof (*(vector))) * next_qty);
507 CLEAR (qty_scratch_rtx);
508 CLEAR (qty_phys_copy_sugg);
509 CLEAR (qty_phys_num_copy_sugg);
510 CLEAR (qty_phys_sugg);
511 CLEAR (qty_phys_num_sugg);
514 next_qty = 0;
516 block_alloc (b);
517 #ifdef USE_C_ALLOCA
518 alloca (0);
519 #endif
523 /* Depth of loops we are in while in update_equiv_regs. */
524 static int loop_depth;
526 /* Used for communication between the following two functions: contains
527 a MEM that we wish to ensure remains unchanged. */
528 static rtx equiv_mem;
530 /* Set nonzero if EQUIV_MEM is modified. */
531 static int equiv_mem_modified;
533 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
534 Called via note_stores. */
536 static void
537 validate_equiv_mem_from_store (dest, set)
538 rtx dest;
539 rtx set;
541 if ((GET_CODE (dest) == REG
542 && reg_overlap_mentioned_p (dest, equiv_mem))
543 || (GET_CODE (dest) == MEM
544 && true_dependence (dest, equiv_mem)))
545 equiv_mem_modified = 1;
548 /* Verify that no store between START and the death of REG invalidates
549 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
550 by storing into an overlapping memory location, or with a non-const
551 CALL_INSN.
553 Return 1 if MEMREF remains valid. */
555 static int
556 validate_equiv_mem (start, reg, memref)
557 rtx start;
558 rtx reg;
559 rtx memref;
561 rtx insn;
562 rtx note;
564 equiv_mem = memref;
565 equiv_mem_modified = 0;
567 /* If the memory reference has side effects or is volatile, it isn't a
568 valid equivalence. */
569 if (side_effects_p (memref))
570 return 0;
572 for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
574 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
575 continue;
577 if (find_reg_note (insn, REG_DEAD, reg))
578 return 1;
580 if (GET_CODE (insn) == CALL_INSN && ! RTX_UNCHANGING_P (memref)
581 && ! CONST_CALL_P (insn))
582 return 0;
584 note_stores (PATTERN (insn), validate_equiv_mem_from_store);
586 /* If a register mentioned in MEMREF is modified via an
587 auto-increment, we lose the equivalence. Do the same if one
588 dies; although we could extend the life, it doesn't seem worth
589 the trouble. */
591 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
592 if ((REG_NOTE_KIND (note) == REG_INC
593 || REG_NOTE_KIND (note) == REG_DEAD)
594 && GET_CODE (XEXP (note, 0)) == REG
595 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
596 return 0;
599 return 0;
602 /* TRUE if X references a memory location that would be affected by a store
603 to MEMREF. */
605 static int
606 memref_referenced_p (memref, x)
607 rtx x;
608 rtx memref;
610 int i, j;
611 char *fmt;
612 enum rtx_code code = GET_CODE (x);
614 switch (code)
616 case CONST_INT:
617 case CONST:
618 case LABEL_REF:
619 case SYMBOL_REF:
620 case CONST_DOUBLE:
621 case PC:
622 case CC0:
623 case HIGH:
624 case LO_SUM:
625 return 0;
627 case REG:
628 return (reg_equiv_replacement[REGNO (x)]
629 && memref_referenced_p (memref,
630 reg_equiv_replacement[REGNO (x)]));
632 case MEM:
633 if (true_dependence (memref, x))
634 return 1;
635 break;
637 case SET:
638 /* If we are setting a MEM, it doesn't count (its address does), but any
639 other SET_DEST that has a MEM in it is referencing the MEM. */
640 if (GET_CODE (SET_DEST (x)) == MEM)
642 if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
643 return 1;
645 else if (memref_referenced_p (memref, SET_DEST (x)))
646 return 1;
648 return memref_referenced_p (memref, SET_SRC (x));
651 fmt = GET_RTX_FORMAT (code);
652 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
653 switch (fmt[i])
655 case 'e':
656 if (memref_referenced_p (memref, XEXP (x, i)))
657 return 1;
658 break;
659 case 'E':
660 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
661 if (memref_referenced_p (memref, XVECEXP (x, i, j)))
662 return 1;
663 break;
666 return 0;
669 /* TRUE if some insn in the range (START, END] references a memory location
670 that would be affected by a store to MEMREF. */
672 static int
673 memref_used_between_p (memref, start, end)
674 rtx memref;
675 rtx start;
676 rtx end;
678 rtx insn;
680 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
681 insn = NEXT_INSN (insn))
682 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
683 && memref_referenced_p (memref, PATTERN (insn)))
684 return 1;
686 return 0;
689 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
690 in INSN.
692 Search forward to see if SRC dies before either it or DEST is modified,
693 but don't scan past the end of a basic block. If so, we can replace SRC
694 with DEST and let SRC die in INSN.
696 This will reduce the number of registers live in that range and may enable
697 DEST to be tied to SRC, thus often saving one register in addition to a
698 register-register copy. */
700 static void
701 optimize_reg_copy_1 (insn, dest, src)
702 rtx insn;
703 rtx dest;
704 rtx src;
706 rtx p, q;
707 rtx note;
708 rtx dest_death = 0;
709 int sregno = REGNO (src);
710 int dregno = REGNO (dest);
712 if (sregno == dregno
713 #ifdef SMALL_REGISTER_CLASSES
714 /* We don't want to mess with hard regs if register classes are small. */
715 || sregno < FIRST_PSEUDO_REGISTER || dregno < FIRST_PSEUDO_REGISTER
716 #endif
717 /* We don't see all updates to SP if they are in an auto-inc memory
718 reference, so we must disallow this optimization on them. */
719 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
720 return;
722 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
724 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
725 || (GET_CODE (p) == NOTE
726 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
727 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
728 break;
730 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
731 continue;
733 if (reg_set_p (src, p) || reg_set_p (dest, p)
734 /* Don't change a USE of a register. */
735 || (GET_CODE (PATTERN (p)) == USE
736 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
737 break;
739 /* See if all of SRC dies in P. This test is slightly more
740 conservative than it needs to be. */
741 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
742 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
744 int failed = 0;
745 int length = 0;
746 int d_length = 0;
747 int n_calls = 0;
748 int d_n_calls = 0;
750 /* We can do the optimization. Scan forward from INSN again,
751 replacing regs as we go. Set FAILED if a replacement can't
752 be done. In that case, we can't move the death note for SRC.
753 This should be rare. */
755 /* Set to stop at next insn. */
756 for (q = next_real_insn (insn);
757 q != next_real_insn (p);
758 q = next_real_insn (q))
760 if (reg_overlap_mentioned_p (src, PATTERN (q)))
762 /* If SRC is a hard register, we might miss some
763 overlapping registers with validate_replace_rtx,
764 so we would have to undo it. We can't if DEST is
765 present in the insn, so fail in that combination
766 of cases. */
767 if (sregno < FIRST_PSEUDO_REGISTER
768 && reg_mentioned_p (dest, PATTERN (q)))
769 failed = 1;
771 /* Replace all uses and make sure that the register
772 isn't still present. */
773 else if (validate_replace_rtx (src, dest, q)
774 && (sregno >= FIRST_PSEUDO_REGISTER
775 || ! reg_overlap_mentioned_p (src,
776 PATTERN (q))))
778 /* We assume that a register is used exactly once per
779 insn in the updates below. If this is not correct,
780 no great harm is done. */
781 if (sregno >= FIRST_PSEUDO_REGISTER)
782 reg_n_refs[sregno] -= loop_depth;
783 if (dregno >= FIRST_PSEUDO_REGISTER)
784 reg_n_refs[dregno] += loop_depth;
786 else
788 validate_replace_rtx (dest, src, q);
789 failed = 1;
793 /* Count the insns and CALL_INSNs passed. If we passed the
794 death note of DEST, show increased live length. */
795 length++;
796 if (dest_death)
797 d_length++;
799 /* If the insn in which SRC dies is a CALL_INSN, don't count it
800 as a call that has been crossed. Otherwise, count it. */
801 if (q != p && GET_CODE (q) == CALL_INSN)
803 n_calls++;
804 if (dest_death)
805 d_n_calls++;
808 /* If DEST dies here, remove the death note and save it for
809 later. Make sure ALL of DEST dies here; again, this is
810 overly conservative. */
811 if (dest_death == 0
812 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0
813 && GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
814 remove_note (q, dest_death);
817 if (! failed)
819 if (sregno >= FIRST_PSEUDO_REGISTER)
821 if (reg_live_length[sregno] >= 0)
823 reg_live_length[sregno] -= length;
824 /* reg_live_length is only an approximation after
825 combine if sched is not run, so make sure that we
826 still have a reasonable value. */
827 if (reg_live_length[sregno] < 2)
828 reg_live_length[sregno] = 2;
831 reg_n_calls_crossed[sregno] -= n_calls;
834 if (dregno >= FIRST_PSEUDO_REGISTER)
836 if (reg_live_length[dregno] >= 0)
837 reg_live_length[dregno] += d_length;
839 reg_n_calls_crossed[dregno] += d_n_calls;
842 /* Move death note of SRC from P to INSN. */
843 remove_note (p, note);
844 XEXP (note, 1) = REG_NOTES (insn);
845 REG_NOTES (insn) = note;
848 /* Put death note of DEST on P if we saw it die. */
849 if (dest_death)
851 XEXP (dest_death, 1) = REG_NOTES (p);
852 REG_NOTES (p) = dest_death;
855 return;
858 /* If SRC is a hard register which is set or killed in some other
859 way, we can't do this optimization. */
860 else if (sregno < FIRST_PSEUDO_REGISTER
861 && dead_or_set_p (p, src))
862 break;
866 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
867 a sequence of insns that modify DEST followed by an insn that sets
868 SRC to DEST in which DEST dies, with no prior modification of DEST.
869 (There is no need to check if the insns in between actually modify
870 DEST. We should not have cases where DEST is not modified, but
871 the optimization is safe if no such modification is detected.)
872 In that case, we can replace all uses of DEST, starting with INSN and
873 ending with the set of SRC to DEST, with SRC. We do not do this
874 optimization if a CALL_INSN is crossed unless SRC already crosses a
875 call or if DEST dies before the copy back to SRC.
877 It is assumed that DEST and SRC are pseudos; it is too complicated to do
878 this for hard registers since the substitutions we may make might fail. */
880 static void
881 optimize_reg_copy_2 (insn, dest, src)
882 rtx insn;
883 rtx dest;
884 rtx src;
886 rtx p, q;
887 rtx set;
888 int sregno = REGNO (src);
889 int dregno = REGNO (dest);
891 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
893 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
894 || (GET_CODE (p) == NOTE
895 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
896 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
897 break;
899 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
900 continue;
902 set = single_set (p);
903 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
904 && find_reg_note (p, REG_DEAD, dest))
906 /* We can do the optimization. Scan forward from INSN again,
907 replacing regs as we go. */
909 /* Set to stop at next insn. */
910 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
911 if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
913 if (reg_mentioned_p (dest, PATTERN (q)))
915 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
917 /* We assume that a register is used exactly once per
918 insn in the updates below. If this is not correct,
919 no great harm is done. */
920 reg_n_refs[dregno] -= loop_depth;
921 reg_n_refs[sregno] += loop_depth;
925 if (GET_CODE (q) == CALL_INSN)
927 reg_n_calls_crossed[dregno]--;
928 reg_n_calls_crossed[sregno]++;
932 remove_note (p, find_reg_note (p, REG_DEAD, dest));
933 reg_n_deaths[dregno]--;
934 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
935 reg_n_deaths[sregno]--;
936 return;
939 if (reg_set_p (src, p)
940 || find_reg_note (p, REG_DEAD, dest)
941 || (GET_CODE (p) == CALL_INSN && reg_n_calls_crossed[sregno] == 0))
942 break;
946 /* Find registers that are equivalent to a single value throughout the
947 compilation (either because they can be referenced in memory or are set once
948 from a single constant). Lower their priority for a register.
950 If such a register is only referenced once, try substituting its value
951 into the using insn. If it succeeds, we can eliminate the register
952 completely. */
954 static void
955 update_equiv_regs ()
957 rtx *reg_equiv_init_insn = (rtx *) alloca (max_regno * sizeof (rtx *));
958 rtx insn;
960 reg_equiv_replacement = (rtx *) alloca (max_regno * sizeof (rtx *));
962 bzero ((char *) reg_equiv_init_insn, max_regno * sizeof (rtx *));
963 bzero ((char *) reg_equiv_replacement, max_regno * sizeof (rtx *));
965 init_alias_analysis ();
967 loop_depth = 1;
969 /* Scan the insns and find which registers have equivalences. Do this
970 in a separate scan of the insns because (due to -fcse-follow-jumps)
971 a register can be set below its use. */
972 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
974 rtx note;
975 rtx set = single_set (insn);
976 rtx dest;
977 int regno;
979 if (GET_CODE (insn) == NOTE)
981 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
982 loop_depth++;
983 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
984 loop_depth--;
987 /* If this insn contains more (or less) than a single SET, ignore it. */
988 if (set == 0)
989 continue;
991 dest = SET_DEST (set);
993 /* If this sets a MEM to the contents of a REG that is only used
994 in a single basic block, see if the register is always equivalent
995 to that memory location and if moving the store from INSN to the
996 insn that set REG is safe. If so, put a REG_EQUIV note on the
997 initializing insn. */
999 if (GET_CODE (dest) == MEM && GET_CODE (SET_SRC (set)) == REG
1000 && (regno = REGNO (SET_SRC (set))) >= FIRST_PSEUDO_REGISTER
1001 && reg_basic_block[regno] >= 0
1002 && reg_equiv_init_insn[regno] != 0
1003 && validate_equiv_mem (reg_equiv_init_insn[regno], SET_SRC (set),
1004 dest)
1005 && ! memref_used_between_p (SET_DEST (set),
1006 reg_equiv_init_insn[regno], insn))
1007 REG_NOTES (reg_equiv_init_insn[regno])
1008 = gen_rtx (EXPR_LIST, REG_EQUIV, dest,
1009 REG_NOTES (reg_equiv_init_insn[regno]));
1011 /* If this is a register-register copy where SRC is not dead, see if we
1012 can optimize it. */
1013 if (flag_expensive_optimizations && GET_CODE (dest) == REG
1014 && GET_CODE (SET_SRC (set)) == REG
1015 && ! find_reg_note (insn, REG_DEAD, SET_SRC (set)))
1016 optimize_reg_copy_1 (insn, dest, SET_SRC (set));
1018 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
1019 else if (flag_expensive_optimizations && GET_CODE (dest) == REG
1020 && REGNO (dest) >= FIRST_PSEUDO_REGISTER
1021 && GET_CODE (SET_SRC (set)) == REG
1022 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
1023 && find_reg_note (insn, REG_DEAD, SET_SRC (set)))
1024 optimize_reg_copy_2 (insn, dest, SET_SRC (set));
1026 /* Otherwise, we only handle the case of a pseudo register being set
1027 once. */
1028 if (GET_CODE (dest) != REG
1029 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
1030 || reg_n_sets[regno] != 1)
1031 continue;
1033 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1035 /* Record this insn as initializing this register. */
1036 reg_equiv_init_insn[regno] = insn;
1038 /* If this register is known to be equal to a constant, record that
1039 it is always equivalent to the constant. */
1040 if (note && CONSTANT_P (XEXP (note, 0)))
1041 PUT_MODE (note, (enum machine_mode) REG_EQUIV);
1043 /* If this insn introduces a "constant" register, decrease the priority
1044 of that register. Record this insn if the register is only used once
1045 more and the equivalence value is the same as our source.
1047 The latter condition is checked for two reasons: First, it is an
1048 indication that it may be more efficient to actually emit the insn
1049 as written (if no registers are available, reload will substitute
1050 the equivalence). Secondly, it avoids problems with any registers
1051 dying in this insn whose death notes would be missed.
1053 If we don't have a REG_EQUIV note, see if this insn is loading
1054 a register used only in one basic block from a MEM. If so, and the
1055 MEM remains unchanged for the life of the register, add a REG_EQUIV
1056 note. */
1058 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1060 if (note == 0 && reg_basic_block[regno] >= 0
1061 && GET_CODE (SET_SRC (set)) == MEM
1062 && validate_equiv_mem (insn, dest, SET_SRC (set)))
1063 REG_NOTES (insn) = note = gen_rtx (EXPR_LIST, REG_EQUIV, SET_SRC (set),
1064 REG_NOTES (insn));
1066 /* Don't mess with things live during setjmp. */
1067 if (note && reg_live_length[regno] >= 0)
1069 int regno = REGNO (dest);
1071 /* Note that the statement below does not affect the priority
1072 in local-alloc! */
1073 reg_live_length[regno] *= 2;
1075 /* If the register is referenced exactly twice, meaning it is set
1076 once and used once, indicate that the reference may be replaced
1077 by the equivalence we computed above. If the register is only
1078 used in one basic block, this can't succeed or combine would
1079 have done it.
1081 It would be nice to use "loop_depth * 2" in the compare
1082 below. Unfortunately, LOOP_DEPTH need not be constant within
1083 a basic block so this would be too complicated.
1085 This case normally occurs when a parameter is read from memory
1086 and then used exactly once, not in a loop. */
1088 if (reg_n_refs[regno] == 2
1089 && reg_basic_block[regno] < 0
1090 && rtx_equal_p (XEXP (note, 0), SET_SRC (set)))
1091 reg_equiv_replacement[regno] = SET_SRC (set);
1095 /* Now scan all regs killed in an insn to see if any of them are registers
1096 only used that once. If so, see if we can replace the reference with
1097 the equivalent from. If we can, delete the initializing reference
1098 and this register will go away. */
1099 for (insn = next_active_insn (get_insns ());
1100 insn;
1101 insn = next_active_insn (insn))
1103 rtx link;
1105 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1106 if (REG_NOTE_KIND (link) == REG_DEAD
1107 /* Make sure this insn still refers to the register. */
1108 && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
1110 int regno = REGNO (XEXP (link, 0));
1112 if (reg_equiv_replacement[regno]
1113 && validate_replace_rtx (regno_reg_rtx[regno],
1114 reg_equiv_replacement[regno], insn))
1116 rtx equiv_insn = reg_equiv_init_insn[regno];
1118 remove_death (regno, insn);
1119 reg_n_refs[regno] = 0;
1120 PUT_CODE (equiv_insn, NOTE);
1121 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1122 NOTE_SOURCE_FILE (equiv_insn) = 0;
1128 /* Allocate hard regs to the pseudo regs used only within block number B.
1129 Only the pseudos that die but once can be handled. */
1131 static void
1132 block_alloc (b)
1133 int b;
1135 register int i, q;
1136 register rtx insn;
1137 rtx note;
1138 int insn_number = 0;
1139 int insn_count = 0;
1140 int max_uid = get_max_uid ();
1141 int *qty_order;
1142 int no_conflict_combined_regno = -1;
1143 /* Counter to prevent allocating more SCRATCHes than can be stored
1144 in SCRATCH_LIST. */
1145 int scratches_allocated = scratch_index;
1147 /* Count the instructions in the basic block. */
1149 insn = basic_block_end[b];
1150 while (1)
1152 if (GET_CODE (insn) != NOTE)
1153 if (++insn_count > max_uid)
1154 abort ();
1155 if (insn == basic_block_head[b])
1156 break;
1157 insn = PREV_INSN (insn);
1160 /* +2 to leave room for a post_mark_life at the last insn and for
1161 the birth of a CLOBBER in the first insn. */
1162 regs_live_at = (HARD_REG_SET *) alloca ((2 * insn_count + 2)
1163 * sizeof (HARD_REG_SET));
1164 bzero ((char *) regs_live_at, (2 * insn_count + 2) * sizeof (HARD_REG_SET));
1166 /* Initialize table of hardware registers currently live. */
1168 #ifdef HARD_REG_SET
1169 regs_live = *basic_block_live_at_start[b];
1170 #else
1171 COPY_HARD_REG_SET (regs_live, basic_block_live_at_start[b]);
1172 #endif
1174 /* This loop scans the instructions of the basic block
1175 and assigns quantities to registers.
1176 It computes which registers to tie. */
1178 insn = basic_block_head[b];
1179 while (1)
1181 register rtx body = PATTERN (insn);
1183 if (GET_CODE (insn) != NOTE)
1184 insn_number++;
1186 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1188 register rtx link, set;
1189 register int win = 0;
1190 register rtx r0, r1;
1191 int combined_regno = -1;
1192 int i;
1193 int insn_code_number = recog_memoized (insn);
1195 this_insn_number = insn_number;
1196 this_insn = insn;
1198 if (insn_code_number >= 0)
1199 insn_extract (insn);
1200 which_alternative = -1;
1202 /* Is this insn suitable for tying two registers?
1203 If so, try doing that.
1204 Suitable insns are those with at least two operands and where
1205 operand 0 is an output that is a register that is not
1206 earlyclobber.
1208 We can tie operand 0 with some operand that dies in this insn.
1209 First look for operands that are required to be in the same
1210 register as operand 0. If we find such, only try tying that
1211 operand or one that can be put into that operand if the
1212 operation is commutative. If we don't find an operand
1213 that is required to be in the same register as operand 0,
1214 we can tie with any operand.
1216 Subregs in place of regs are also ok.
1218 If tying is done, WIN is set nonzero. */
1220 if (insn_code_number >= 0
1221 #ifdef REGISTER_CONSTRAINTS
1222 && insn_n_operands[insn_code_number] > 1
1223 && insn_operand_constraint[insn_code_number][0][0] == '='
1224 && insn_operand_constraint[insn_code_number][0][1] != '&'
1225 #else
1226 && GET_CODE (PATTERN (insn)) == SET
1227 && rtx_equal_p (SET_DEST (PATTERN (insn)), recog_operand[0])
1228 #endif
1231 #ifdef REGISTER_CONSTRAINTS
1232 /* If non-negative, is an operand that must match operand 0. */
1233 int must_match_0 = -1;
1234 /* Counts number of alternatives that require a match with
1235 operand 0. */
1236 int n_matching_alts = 0;
1238 for (i = 1; i < insn_n_operands[insn_code_number]; i++)
1240 char *p = insn_operand_constraint[insn_code_number][i];
1241 int this_match = (requires_inout (p));
1243 n_matching_alts += this_match;
1244 if (this_match == insn_n_alternatives[insn_code_number])
1245 must_match_0 = i;
1247 #endif
1249 r0 = recog_operand[0];
1250 for (i = 1; i < insn_n_operands[insn_code_number]; i++)
1252 #ifdef REGISTER_CONSTRAINTS
1253 /* Skip this operand if we found an operand that
1254 must match operand 0 and this operand isn't it
1255 and can't be made to be it by commutativity. */
1257 if (must_match_0 >= 0 && i != must_match_0
1258 && ! (i == must_match_0 + 1
1259 && insn_operand_constraint[insn_code_number][i-1][0] == '%')
1260 && ! (i == must_match_0 - 1
1261 && insn_operand_constraint[insn_code_number][i][0] == '%'))
1262 continue;
1264 /* Likewise if each alternative has some operand that
1265 must match operand zero. In that case, skip any
1266 operand that doesn't list operand 0 since we know that
1267 the operand always conflicts with operand 0. We
1268 ignore commutatity in this case to keep things simple. */
1269 if (n_matching_alts == insn_n_alternatives[insn_code_number]
1270 && (0 == requires_inout
1271 (insn_operand_constraint[insn_code_number][i])))
1272 continue;
1273 #endif
1275 r1 = recog_operand[i];
1277 /* If the operand is an address, find a register in it.
1278 There may be more than one register, but we only try one
1279 of them. */
1280 if (
1281 #ifdef REGISTER_CONSTRAINTS
1282 insn_operand_constraint[insn_code_number][i][0] == 'p'
1283 #else
1284 insn_operand_address_p[insn_code_number][i]
1285 #endif
1287 while (GET_CODE (r1) == PLUS || GET_CODE (r1) == MULT)
1288 r1 = XEXP (r1, 0);
1290 if (GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
1292 /* We have two priorities for hard register preferences.
1293 If we have a move insn or an insn whose first input
1294 can only be in the same register as the output, give
1295 priority to an equivalence found from that insn. */
1296 int may_save_copy
1297 = ((SET_DEST (body) == r0 && SET_SRC (body) == r1)
1298 #ifdef REGISTER_CONSTRAINTS
1299 || (r1 == recog_operand[i] && must_match_0 >= 0)
1300 #endif
1303 if (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1304 win = combine_regs (r1, r0, may_save_copy,
1305 insn_number, insn, 0);
1307 if (win)
1308 break;
1312 /* Recognize an insn sequence with an ultimate result
1313 which can safely overlap one of the inputs.
1314 The sequence begins with a CLOBBER of its result,
1315 and ends with an insn that copies the result to itself
1316 and has a REG_EQUAL note for an equivalent formula.
1317 That note indicates what the inputs are.
1318 The result and the input can overlap if each insn in
1319 the sequence either doesn't mention the input
1320 or has a REG_NO_CONFLICT note to inhibit the conflict.
1322 We do the combining test at the CLOBBER so that the
1323 destination register won't have had a quantity number
1324 assigned, since that would prevent combining. */
1326 if (GET_CODE (PATTERN (insn)) == CLOBBER
1327 && (r0 = XEXP (PATTERN (insn), 0),
1328 GET_CODE (r0) == REG)
1329 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1330 && XEXP (link, 0) != 0
1331 && GET_CODE (XEXP (link, 0)) == INSN
1332 && (set = single_set (XEXP (link, 0))) != 0
1333 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1334 && (note = find_reg_note (XEXP (link, 0), REG_EQUAL,
1335 NULL_RTX)) != 0)
1337 if (r1 = XEXP (note, 0), GET_CODE (r1) == REG
1338 /* Check that we have such a sequence. */
1339 && no_conflict_p (insn, r0, r1))
1340 win = combine_regs (r1, r0, 1, insn_number, insn, 1);
1341 else if (GET_RTX_FORMAT (GET_CODE (XEXP (note, 0)))[0] == 'e'
1342 && (r1 = XEXP (XEXP (note, 0), 0),
1343 GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1344 && no_conflict_p (insn, r0, r1))
1345 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1347 /* Here we care if the operation to be computed is
1348 commutative. */
1349 else if ((GET_CODE (XEXP (note, 0)) == EQ
1350 || GET_CODE (XEXP (note, 0)) == NE
1351 || GET_RTX_CLASS (GET_CODE (XEXP (note, 0))) == 'c')
1352 && (r1 = XEXP (XEXP (note, 0), 1),
1353 (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
1354 && no_conflict_p (insn, r0, r1))
1355 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1357 /* If we did combine something, show the register number
1358 in question so that we know to ignore its death. */
1359 if (win)
1360 no_conflict_combined_regno = REGNO (r1);
1363 /* If registers were just tied, set COMBINED_REGNO
1364 to the number of the register used in this insn
1365 that was tied to the register set in this insn.
1366 This register's qty should not be "killed". */
1368 if (win)
1370 while (GET_CODE (r1) == SUBREG)
1371 r1 = SUBREG_REG (r1);
1372 combined_regno = REGNO (r1);
1375 /* Mark the death of everything that dies in this instruction,
1376 except for anything that was just combined. */
1378 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1379 if (REG_NOTE_KIND (link) == REG_DEAD
1380 && GET_CODE (XEXP (link, 0)) == REG
1381 && combined_regno != REGNO (XEXP (link, 0))
1382 && (no_conflict_combined_regno != REGNO (XEXP (link, 0))
1383 || ! find_reg_note (insn, REG_NO_CONFLICT, XEXP (link, 0))))
1384 wipe_dead_reg (XEXP (link, 0), 0);
1386 /* Allocate qty numbers for all registers local to this block
1387 that are born (set) in this instruction.
1388 A pseudo that already has a qty is not changed. */
1390 note_stores (PATTERN (insn), reg_is_set);
1392 /* If anything is set in this insn and then unused, mark it as dying
1393 after this insn, so it will conflict with our outputs. This
1394 can't match with something that combined, and it doesn't matter
1395 if it did. Do this after the calls to reg_is_set since these
1396 die after, not during, the current insn. */
1398 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1399 if (REG_NOTE_KIND (link) == REG_UNUSED
1400 && GET_CODE (XEXP (link, 0)) == REG)
1401 wipe_dead_reg (XEXP (link, 0), 1);
1403 /* Allocate quantities for any SCRATCH operands of this insn. */
1405 if (insn_code_number >= 0)
1406 for (i = 0; i < insn_n_operands[insn_code_number]; i++)
1407 if (GET_CODE (recog_operand[i]) == SCRATCH
1408 && scratches_allocated++ < scratch_list_length)
1409 alloc_qty_for_scratch (recog_operand[i], i, insn,
1410 insn_code_number, insn_number);
1412 /* If this is an insn that has a REG_RETVAL note pointing at a
1413 CLOBBER insn, we have reached the end of a REG_NO_CONFLICT
1414 block, so clear any register number that combined within it. */
1415 if ((note = find_reg_note (insn, REG_RETVAL, NULL_RTX)) != 0
1416 && GET_CODE (XEXP (note, 0)) == INSN
1417 && GET_CODE (PATTERN (XEXP (note, 0))) == CLOBBER)
1418 no_conflict_combined_regno = -1;
1421 /* Set the registers live after INSN_NUMBER. Note that we never
1422 record the registers live before the block's first insn, since no
1423 pseudos we care about are live before that insn. */
1425 IOR_HARD_REG_SET (regs_live_at[2 * insn_number], regs_live);
1426 IOR_HARD_REG_SET (regs_live_at[2 * insn_number + 1], regs_live);
1428 if (insn == basic_block_end[b])
1429 break;
1431 insn = NEXT_INSN (insn);
1434 /* Now every register that is local to this basic block
1435 should have been given a quantity, or else -1 meaning ignore it.
1436 Every quantity should have a known birth and death.
1438 Order the qtys so we assign them registers in order of the
1439 number of suggested registers they need so we allocate those with
1440 the most restrictive needs first. */
1442 qty_order = (int *) alloca (next_qty * sizeof (int));
1443 for (i = 0; i < next_qty; i++)
1444 qty_order[i] = i;
1446 #define EXCHANGE(I1, I2) \
1447 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1449 switch (next_qty)
1451 case 3:
1452 /* Make qty_order[2] be the one to allocate last. */
1453 if (qty_sugg_compare (0, 1) > 0)
1454 EXCHANGE (0, 1);
1455 if (qty_sugg_compare (1, 2) > 0)
1456 EXCHANGE (2, 1);
1458 /* ... Fall through ... */
1459 case 2:
1460 /* Put the best one to allocate in qty_order[0]. */
1461 if (qty_sugg_compare (0, 1) > 0)
1462 EXCHANGE (0, 1);
1464 /* ... Fall through ... */
1466 case 1:
1467 case 0:
1468 /* Nothing to do here. */
1469 break;
1471 default:
1472 qsort (qty_order, next_qty, sizeof (int), qty_sugg_compare_1);
1475 /* Try to put each quantity in a suggested physical register, if it has one.
1476 This may cause registers to be allocated that otherwise wouldn't be, but
1477 this seems acceptable in local allocation (unlike global allocation). */
1478 for (i = 0; i < next_qty; i++)
1480 q = qty_order[i];
1481 if (qty_phys_num_sugg[q] != 0 || qty_phys_num_copy_sugg[q] != 0)
1482 qty_phys_reg[q] = find_free_reg (qty_min_class[q], qty_mode[q], q,
1483 0, 1, qty_birth[q], qty_death[q]);
1484 else
1485 qty_phys_reg[q] = -1;
1488 /* Order the qtys so we assign them registers in order of
1489 decreasing length of life. Normally call qsort, but if we
1490 have only a very small number of quantities, sort them ourselves. */
1492 for (i = 0; i < next_qty; i++)
1493 qty_order[i] = i;
1495 #define EXCHANGE(I1, I2) \
1496 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1498 switch (next_qty)
1500 case 3:
1501 /* Make qty_order[2] be the one to allocate last. */
1502 if (qty_compare (0, 1) > 0)
1503 EXCHANGE (0, 1);
1504 if (qty_compare (1, 2) > 0)
1505 EXCHANGE (2, 1);
1507 /* ... Fall through ... */
1508 case 2:
1509 /* Put the best one to allocate in qty_order[0]. */
1510 if (qty_compare (0, 1) > 0)
1511 EXCHANGE (0, 1);
1513 /* ... Fall through ... */
1515 case 1:
1516 case 0:
1517 /* Nothing to do here. */
1518 break;
1520 default:
1521 qsort (qty_order, next_qty, sizeof (int), qty_compare_1);
1524 /* Now for each qty that is not a hardware register,
1525 look for a hardware register to put it in.
1526 First try the register class that is cheapest for this qty,
1527 if there is more than one class. */
1529 for (i = 0; i < next_qty; i++)
1531 q = qty_order[i];
1532 if (qty_phys_reg[q] < 0)
1534 if (N_REG_CLASSES > 1)
1536 qty_phys_reg[q] = find_free_reg (qty_min_class[q],
1537 qty_mode[q], q, 0, 0,
1538 qty_birth[q], qty_death[q]);
1539 if (qty_phys_reg[q] >= 0)
1540 continue;
1543 if (qty_alternate_class[q] != NO_REGS)
1544 qty_phys_reg[q] = find_free_reg (qty_alternate_class[q],
1545 qty_mode[q], q, 0, 0,
1546 qty_birth[q], qty_death[q]);
1550 /* Now propagate the register assignments
1551 to the pseudo regs belonging to the qtys. */
1553 for (q = 0; q < next_qty; q++)
1554 if (qty_phys_reg[q] >= 0)
1556 for (i = qty_first_reg[q]; i >= 0; i = reg_next_in_qty[i])
1557 reg_renumber[i] = qty_phys_reg[q] + reg_offset[i];
1558 if (qty_scratch_rtx[q])
1560 if (GET_CODE (qty_scratch_rtx[q]) == REG)
1561 abort ();
1562 PUT_CODE (qty_scratch_rtx[q], REG);
1563 REGNO (qty_scratch_rtx[q]) = qty_phys_reg[q];
1565 scratch_block[scratch_index] = b;
1566 scratch_list[scratch_index++] = qty_scratch_rtx[q];
1568 /* Must clear the USED field, because it will have been set by
1569 copy_rtx_if_shared, but the leaf_register code expects that
1570 it is zero in all REG rtx. copy_rtx_if_shared does not set the
1571 used bit for REGs, but does for SCRATCHes. */
1572 qty_scratch_rtx[q]->used = 0;
1577 /* Compare two quantities' priority for getting real registers.
1578 We give shorter-lived quantities higher priority.
1579 Quantities with more references are also preferred, as are quantities that
1580 require multiple registers. This is the identical prioritization as
1581 done by global-alloc.
1583 We used to give preference to registers with *longer* lives, but using
1584 the same algorithm in both local- and global-alloc can speed up execution
1585 of some programs by as much as a factor of three! */
1587 static int
1588 qty_compare (q1, q2)
1589 int q1, q2;
1591 /* Note that the quotient will never be bigger than
1592 the value of floor_log2 times the maximum number of
1593 times a register can occur in one insn (surely less than 100).
1594 Multiplying this by 10000 can't overflow. */
1595 register int pri1
1596 = (((double) (floor_log2 (qty_n_refs[q1]) * qty_n_refs[q1] * qty_size[q1])
1597 / (qty_death[q1] - qty_birth[q1]))
1598 * 10000);
1599 register int pri2
1600 = (((double) (floor_log2 (qty_n_refs[q2]) * qty_n_refs[q2] * qty_size[q2])
1601 / (qty_death[q2] - qty_birth[q2]))
1602 * 10000);
1603 return pri2 - pri1;
1606 static int
1607 qty_compare_1 (q1, q2)
1608 int *q1, *q2;
1610 register int tem;
1612 /* Note that the quotient will never be bigger than
1613 the value of floor_log2 times the maximum number of
1614 times a register can occur in one insn (surely less than 100).
1615 Multiplying this by 10000 can't overflow. */
1616 register int pri1
1617 = (((double) (floor_log2 (qty_n_refs[*q1]) * qty_n_refs[*q1]
1618 * qty_size[*q1])
1619 / (qty_death[*q1] - qty_birth[*q1]))
1620 * 10000);
1621 register int pri2
1622 = (((double) (floor_log2 (qty_n_refs[*q2]) * qty_n_refs[*q2]
1623 * qty_size[*q2])
1624 / (qty_death[*q2] - qty_birth[*q2]))
1625 * 10000);
1627 tem = pri2 - pri1;
1628 if (tem != 0) return tem;
1629 /* If qtys are equally good, sort by qty number,
1630 so that the results of qsort leave nothing to chance. */
1631 return *q1 - *q2;
1634 /* Compare two quantities' priority for getting real registers. This version
1635 is called for quantities that have suggested hard registers. First priority
1636 goes to quantities that have copy preferences, then to those that have
1637 normal preferences. Within those groups, quantities with the lower
1638 number of preferences have the highest priority. Of those, we use the same
1639 algorithm as above. */
1641 static int
1642 qty_sugg_compare (q1, q2)
1643 int q1, q2;
1645 register int sugg1 = (qty_phys_num_copy_sugg[q1]
1646 ? qty_phys_num_copy_sugg[q1]
1647 : qty_phys_num_sugg[q1] * FIRST_PSEUDO_REGISTER);
1648 register int sugg2 = (qty_phys_num_copy_sugg[q2]
1649 ? qty_phys_num_copy_sugg[q2]
1650 : qty_phys_num_sugg[q2] * FIRST_PSEUDO_REGISTER);
1651 /* Note that the quotient will never be bigger than
1652 the value of floor_log2 times the maximum number of
1653 times a register can occur in one insn (surely less than 100).
1654 Multiplying this by 10000 can't overflow. */
1655 register int pri1
1656 = (((double) (floor_log2 (qty_n_refs[q1]) * qty_n_refs[q1] * qty_size[q1])
1657 / (qty_death[q1] - qty_birth[q1]))
1658 * 10000);
1659 register int pri2
1660 = (((double) (floor_log2 (qty_n_refs[q2]) * qty_n_refs[q2] * qty_size[q2])
1661 / (qty_death[q2] - qty_birth[q2]))
1662 * 10000);
1664 if (sugg1 != sugg2)
1665 return sugg1 - sugg2;
1667 return pri2 - pri1;
1670 static int
1671 qty_sugg_compare_1 (q1, q2)
1672 int *q1, *q2;
1674 register int sugg1 = (qty_phys_num_copy_sugg[*q1]
1675 ? qty_phys_num_copy_sugg[*q1]
1676 : qty_phys_num_sugg[*q1] * FIRST_PSEUDO_REGISTER);
1677 register int sugg2 = (qty_phys_num_copy_sugg[*q2]
1678 ? qty_phys_num_copy_sugg[*q2]
1679 : qty_phys_num_sugg[*q2] * FIRST_PSEUDO_REGISTER);
1681 /* Note that the quotient will never be bigger than
1682 the value of floor_log2 times the maximum number of
1683 times a register can occur in one insn (surely less than 100).
1684 Multiplying this by 10000 can't overflow. */
1685 register int pri1
1686 = (((double) (floor_log2 (qty_n_refs[*q1]) * qty_n_refs[*q1]
1687 * qty_size[*q1])
1688 / (qty_death[*q1] - qty_birth[*q1]))
1689 * 10000);
1690 register int pri2
1691 = (((double) (floor_log2 (qty_n_refs[*q2]) * qty_n_refs[*q2]
1692 * qty_size[*q2])
1693 / (qty_death[*q2] - qty_birth[*q2]))
1694 * 10000);
1696 if (sugg1 != sugg2)
1697 return sugg1 - sugg2;
1699 if (pri1 != pri2)
1700 return pri2 - pri1;
1702 /* If qtys are equally good, sort by qty number,
1703 so that the results of qsort leave nothing to chance. */
1704 return *q1 - *q2;
1707 /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
1708 Returns 1 if have done so, or 0 if cannot.
1710 Combining registers means marking them as having the same quantity
1711 and adjusting the offsets within the quantity if either of
1712 them is a SUBREG).
1714 We don't actually combine a hard reg with a pseudo; instead
1715 we just record the hard reg as the suggestion for the pseudo's quantity.
1716 If we really combined them, we could lose if the pseudo lives
1717 across an insn that clobbers the hard reg (eg, movstr).
1719 ALREADY_DEAD is non-zero if USEDREG is known to be dead even though
1720 there is no REG_DEAD note on INSN. This occurs during the processing
1721 of REG_NO_CONFLICT blocks.
1723 MAY_SAVE_COPYCOPY is non-zero if this insn is simply copying USEDREG to
1724 SETREG or if the input and output must share a register.
1725 In that case, we record a hard reg suggestion in QTY_PHYS_COPY_SUGG.
1727 There are elaborate checks for the validity of combining. */
1730 static int
1731 combine_regs (usedreg, setreg, may_save_copy, insn_number, insn, already_dead)
1732 rtx usedreg, setreg;
1733 int may_save_copy;
1734 int insn_number;
1735 rtx insn;
1736 int already_dead;
1738 register int ureg, sreg;
1739 register int offset = 0;
1740 int usize, ssize;
1741 register int sqty;
1743 /* Determine the numbers and sizes of registers being used. If a subreg
1744 is present that does not change the entire register, don't consider
1745 this a copy insn. */
1747 while (GET_CODE (usedreg) == SUBREG)
1749 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (usedreg))) > UNITS_PER_WORD)
1750 may_save_copy = 0;
1751 offset += SUBREG_WORD (usedreg);
1752 usedreg = SUBREG_REG (usedreg);
1754 if (GET_CODE (usedreg) != REG)
1755 return 0;
1756 ureg = REGNO (usedreg);
1757 usize = REG_SIZE (usedreg);
1759 while (GET_CODE (setreg) == SUBREG)
1761 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (setreg))) > UNITS_PER_WORD)
1762 may_save_copy = 0;
1763 offset -= SUBREG_WORD (setreg);
1764 setreg = SUBREG_REG (setreg);
1766 if (GET_CODE (setreg) != REG)
1767 return 0;
1768 sreg = REGNO (setreg);
1769 ssize = REG_SIZE (setreg);
1771 /* If UREG is a pseudo-register that hasn't already been assigned a
1772 quantity number, it means that it is not local to this block or dies
1773 more than once. In either event, we can't do anything with it. */
1774 if ((ureg >= FIRST_PSEUDO_REGISTER && reg_qty[ureg] < 0)
1775 /* Do not combine registers unless one fits within the other. */
1776 || (offset > 0 && usize + offset > ssize)
1777 || (offset < 0 && usize + offset < ssize)
1778 /* Do not combine with a smaller already-assigned object
1779 if that smaller object is already combined with something bigger. */
1780 || (ssize > usize && ureg >= FIRST_PSEUDO_REGISTER
1781 && usize < qty_size[reg_qty[ureg]])
1782 /* Can't combine if SREG is not a register we can allocate. */
1783 || (sreg >= FIRST_PSEUDO_REGISTER && reg_qty[sreg] == -1)
1784 /* Don't combine with a pseudo mentioned in a REG_NO_CONFLICT note.
1785 These have already been taken care of. This probably wouldn't
1786 combine anyway, but don't take any chances. */
1787 || (ureg >= FIRST_PSEUDO_REGISTER
1788 && find_reg_note (insn, REG_NO_CONFLICT, usedreg))
1789 /* Don't tie something to itself. In most cases it would make no
1790 difference, but it would screw up if the reg being tied to itself
1791 also dies in this insn. */
1792 || ureg == sreg
1793 /* Don't try to connect two different hardware registers. */
1794 || (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
1795 /* Don't connect two different machine modes if they have different
1796 implications as to which registers may be used. */
1797 || !MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
1798 return 0;
1800 /* Now, if UREG is a hard reg and SREG is a pseudo, record the hard reg in
1801 qty_phys_sugg for the pseudo instead of tying them.
1803 Return "failure" so that the lifespan of UREG is terminated here;
1804 that way the two lifespans will be disjoint and nothing will prevent
1805 the pseudo reg from being given this hard reg. */
1807 if (ureg < FIRST_PSEUDO_REGISTER)
1809 /* Allocate a quantity number so we have a place to put our
1810 suggestions. */
1811 if (reg_qty[sreg] == -2)
1812 reg_is_born (setreg, 2 * insn_number);
1814 if (reg_qty[sreg] >= 0)
1816 if (may_save_copy
1817 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg))
1819 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg);
1820 qty_phys_num_copy_sugg[reg_qty[sreg]]++;
1822 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg))
1824 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg);
1825 qty_phys_num_sugg[reg_qty[sreg]]++;
1828 return 0;
1831 /* Similarly for SREG a hard register and UREG a pseudo register. */
1833 if (sreg < FIRST_PSEUDO_REGISTER)
1835 if (may_save_copy
1836 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg))
1838 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg);
1839 qty_phys_num_copy_sugg[reg_qty[ureg]]++;
1841 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg))
1843 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg);
1844 qty_phys_num_sugg[reg_qty[ureg]]++;
1846 return 0;
1849 /* At this point we know that SREG and UREG are both pseudos.
1850 Do nothing if SREG already has a quantity or is a register that we
1851 don't allocate. */
1852 if (reg_qty[sreg] >= -1
1853 /* If we are not going to let any regs live across calls,
1854 don't tie a call-crossing reg to a non-call-crossing reg. */
1855 || (current_function_has_nonlocal_label
1856 && ((reg_n_calls_crossed[ureg] > 0)
1857 != (reg_n_calls_crossed[sreg] > 0))))
1858 return 0;
1860 /* We don't already know about SREG, so tie it to UREG
1861 if this is the last use of UREG, provided the classes they want
1862 are compatible. */
1864 if ((already_dead || find_regno_note (insn, REG_DEAD, ureg))
1865 && reg_meets_class_p (sreg, qty_min_class[reg_qty[ureg]]))
1867 /* Add SREG to UREG's quantity. */
1868 sqty = reg_qty[ureg];
1869 reg_qty[sreg] = sqty;
1870 reg_offset[sreg] = reg_offset[ureg] + offset;
1871 reg_next_in_qty[sreg] = qty_first_reg[sqty];
1872 qty_first_reg[sqty] = sreg;
1874 /* If SREG's reg class is smaller, set qty_min_class[SQTY]. */
1875 update_qty_class (sqty, sreg);
1877 /* Update info about quantity SQTY. */
1878 qty_n_calls_crossed[sqty] += reg_n_calls_crossed[sreg];
1879 qty_n_refs[sqty] += reg_n_refs[sreg];
1880 if (usize < ssize)
1882 register int i;
1884 for (i = qty_first_reg[sqty]; i >= 0; i = reg_next_in_qty[i])
1885 reg_offset[i] -= offset;
1887 qty_size[sqty] = ssize;
1888 qty_mode[sqty] = GET_MODE (setreg);
1891 else
1892 return 0;
1894 return 1;
1897 /* Return 1 if the preferred class of REG allows it to be tied
1898 to a quantity or register whose class is CLASS.
1899 True if REG's reg class either contains or is contained in CLASS. */
1901 static int
1902 reg_meets_class_p (reg, class)
1903 int reg;
1904 enum reg_class class;
1906 register enum reg_class rclass = reg_preferred_class (reg);
1907 return (reg_class_subset_p (rclass, class)
1908 || reg_class_subset_p (class, rclass));
1911 /* Return 1 if the two specified classes have registers in common.
1912 If CALL_SAVED, then consider only call-saved registers. */
1914 static int
1915 reg_classes_overlap_p (c1, c2, call_saved)
1916 register enum reg_class c1;
1917 register enum reg_class c2;
1918 int call_saved;
1920 HARD_REG_SET c;
1921 int i;
1923 COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
1924 AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
1926 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1927 if (TEST_HARD_REG_BIT (c, i)
1928 && (! call_saved || ! call_used_regs[i]))
1929 return 1;
1931 return 0;
1934 /* Update the class of QTY assuming that REG is being tied to it. */
1936 static void
1937 update_qty_class (qty, reg)
1938 int qty;
1939 int reg;
1941 enum reg_class rclass = reg_preferred_class (reg);
1942 if (reg_class_subset_p (rclass, qty_min_class[qty]))
1943 qty_min_class[qty] = rclass;
1945 rclass = reg_alternate_class (reg);
1946 if (reg_class_subset_p (rclass, qty_alternate_class[qty]))
1947 qty_alternate_class[qty] = rclass;
1949 if (reg_changes_size[reg])
1950 qty_changes_size[qty] = 1;
1953 /* Handle something which alters the value of an rtx REG.
1955 REG is whatever is set or clobbered. SETTER is the rtx that
1956 is modifying the register.
1958 If it is not really a register, we do nothing.
1959 The file-global variables `this_insn' and `this_insn_number'
1960 carry info from `block_alloc'. */
1962 static void
1963 reg_is_set (reg, setter)
1964 rtx reg;
1965 rtx setter;
1967 /* Note that note_stores will only pass us a SUBREG if it is a SUBREG of
1968 a hard register. These may actually not exist any more. */
1970 if (GET_CODE (reg) != SUBREG
1971 && GET_CODE (reg) != REG)
1972 return;
1974 /* Mark this register as being born. If it is used in a CLOBBER, mark
1975 it as being born halfway between the previous insn and this insn so that
1976 it conflicts with our inputs but not the outputs of the previous insn. */
1978 reg_is_born (reg, 2 * this_insn_number - (GET_CODE (setter) == CLOBBER));
1981 /* Handle beginning of the life of register REG.
1982 BIRTH is the index at which this is happening. */
1984 static void
1985 reg_is_born (reg, birth)
1986 rtx reg;
1987 int birth;
1989 register int regno;
1991 if (GET_CODE (reg) == SUBREG)
1992 regno = REGNO (SUBREG_REG (reg)) + SUBREG_WORD (reg);
1993 else
1994 regno = REGNO (reg);
1996 if (regno < FIRST_PSEUDO_REGISTER)
1998 mark_life (regno, GET_MODE (reg), 1);
2000 /* If the register was to have been born earlier that the present
2001 insn, mark it as live where it is actually born. */
2002 if (birth < 2 * this_insn_number)
2003 post_mark_life (regno, GET_MODE (reg), 1, birth, 2 * this_insn_number);
2005 else
2007 if (reg_qty[regno] == -2)
2008 alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), birth);
2010 /* If this register has a quantity number, show that it isn't dead. */
2011 if (reg_qty[regno] >= 0)
2012 qty_death[reg_qty[regno]] = -1;
2016 /* Record the death of REG in the current insn. If OUTPUT_P is non-zero,
2017 REG is an output that is dying (i.e., it is never used), otherwise it
2018 is an input (the normal case).
2019 If OUTPUT_P is 1, then we extend the life past the end of this insn. */
2021 static void
2022 wipe_dead_reg (reg, output_p)
2023 register rtx reg;
2024 int output_p;
2026 register int regno = REGNO (reg);
2028 /* If this insn has multiple results,
2029 and the dead reg is used in one of the results,
2030 extend its life to after this insn,
2031 so it won't get allocated together with any other result of this insn. */
2032 if (GET_CODE (PATTERN (this_insn)) == PARALLEL
2033 && !single_set (this_insn))
2035 int i;
2036 for (i = XVECLEN (PATTERN (this_insn), 0) - 1; i >= 0; i--)
2038 rtx set = XVECEXP (PATTERN (this_insn), 0, i);
2039 if (GET_CODE (set) == SET
2040 && GET_CODE (SET_DEST (set)) != REG
2041 && !rtx_equal_p (reg, SET_DEST (set))
2042 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2043 output_p = 1;
2047 /* If this register is used in an auto-increment address, then extend its
2048 life to after this insn, so that it won't get allocated together with
2049 the result of this insn. */
2050 if (! output_p && find_regno_note (this_insn, REG_INC, regno))
2051 output_p = 1;
2053 if (regno < FIRST_PSEUDO_REGISTER)
2055 mark_life (regno, GET_MODE (reg), 0);
2057 /* If a hard register is dying as an output, mark it as in use at
2058 the beginning of this insn (the above statement would cause this
2059 not to happen). */
2060 if (output_p)
2061 post_mark_life (regno, GET_MODE (reg), 1,
2062 2 * this_insn_number, 2 * this_insn_number+ 1);
2065 else if (reg_qty[regno] >= 0)
2066 qty_death[reg_qty[regno]] = 2 * this_insn_number + output_p;
2069 /* Find a block of SIZE words of hard regs in reg_class CLASS
2070 that can hold something of machine-mode MODE
2071 (but actually we test only the first of the block for holding MODE)
2072 and still free between insn BORN_INDEX and insn DEAD_INDEX,
2073 and return the number of the first of them.
2074 Return -1 if such a block cannot be found.
2075 If QTY crosses calls, insist on a register preserved by calls,
2076 unless ACCEPT_CALL_CLOBBERED is nonzero.
2078 If JUST_TRY_SUGGESTED is non-zero, only try to see if the suggested
2079 register is available. If not, return -1. */
2081 static int
2082 find_free_reg (class, mode, qty, accept_call_clobbered, just_try_suggested,
2083 born_index, dead_index)
2084 enum reg_class class;
2085 enum machine_mode mode;
2086 int qty;
2087 int accept_call_clobbered;
2088 int just_try_suggested;
2089 int born_index, dead_index;
2091 register int i, ins;
2092 #ifdef HARD_REG_SET
2093 register /* Declare it register if it's a scalar. */
2094 #endif
2095 HARD_REG_SET used, first_used;
2096 #ifdef ELIMINABLE_REGS
2097 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2098 #endif
2100 /* Validate our parameters. */
2101 if (born_index < 0 || born_index > dead_index)
2102 abort ();
2104 /* Don't let a pseudo live in a reg across a function call
2105 if we might get a nonlocal goto. */
2106 if (current_function_has_nonlocal_label
2107 && qty_n_calls_crossed[qty] > 0)
2108 return -1;
2110 if (accept_call_clobbered)
2111 COPY_HARD_REG_SET (used, call_fixed_reg_set);
2112 else if (qty_n_calls_crossed[qty] == 0)
2113 COPY_HARD_REG_SET (used, fixed_reg_set);
2114 else
2115 COPY_HARD_REG_SET (used, call_used_reg_set);
2117 if (accept_call_clobbered)
2118 IOR_HARD_REG_SET (used, losing_caller_save_reg_set);
2120 for (ins = born_index; ins < dead_index; ins++)
2121 IOR_HARD_REG_SET (used, regs_live_at[ins]);
2123 IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
2125 /* Don't use the frame pointer reg in local-alloc even if
2126 we may omit the frame pointer, because if we do that and then we
2127 need a frame pointer, reload won't know how to move the pseudo
2128 to another hard reg. It can move only regs made by global-alloc.
2130 This is true of any register that can be eliminated. */
2131 #ifdef ELIMINABLE_REGS
2132 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
2133 SET_HARD_REG_BIT (used, eliminables[i].from);
2134 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2135 /* If FRAME_POINTER_REGNUM is not a real register, then protect the one
2136 that it might be eliminated into. */
2137 SET_HARD_REG_BIT (used, HARD_FRAME_POINTER_REGNUM);
2138 #endif
2139 #else
2140 SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
2141 #endif
2143 #ifdef CLASS_CANNOT_CHANGE_SIZE
2144 if (qty_changes_size[qty])
2145 IOR_HARD_REG_SET (used,
2146 reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
2147 #endif
2149 /* Normally, the registers that can be used for the first register in
2150 a multi-register quantity are the same as those that can be used for
2151 subsequent registers. However, if just trying suggested registers,
2152 restrict our consideration to them. If there are copy-suggested
2153 register, try them. Otherwise, try the arithmetic-suggested
2154 registers. */
2155 COPY_HARD_REG_SET (first_used, used);
2157 if (just_try_suggested)
2159 if (qty_phys_num_copy_sugg[qty] != 0)
2160 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_copy_sugg[qty]);
2161 else
2162 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_sugg[qty]);
2165 /* If all registers are excluded, we can't do anything. */
2166 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) ALL_REGS], first_used, fail);
2168 /* If at least one would be suitable, test each hard reg. */
2170 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2172 #ifdef REG_ALLOC_ORDER
2173 int regno = reg_alloc_order[i];
2174 #else
2175 int regno = i;
2176 #endif
2177 if (! TEST_HARD_REG_BIT (first_used, regno)
2178 && HARD_REGNO_MODE_OK (regno, mode))
2180 register int j;
2181 register int size1 = HARD_REGNO_NREGS (regno, mode);
2182 for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
2183 if (j == size1)
2185 /* Mark that this register is in use between its birth and death
2186 insns. */
2187 post_mark_life (regno, mode, 1, born_index, dead_index);
2188 return regno;
2190 #ifndef REG_ALLOC_ORDER
2191 i += j; /* Skip starting points we know will lose */
2192 #endif
2196 fail:
2198 /* If we are just trying suggested register, we have just tried copy-
2199 suggested registers, and there are arithmetic-suggested registers,
2200 try them. */
2202 /* If it would be profitable to allocate a call-clobbered register
2203 and save and restore it around calls, do that. */
2204 if (just_try_suggested && qty_phys_num_copy_sugg[qty] != 0
2205 && qty_phys_num_sugg[qty] != 0)
2207 /* Don't try the copy-suggested regs again. */
2208 qty_phys_num_copy_sugg[qty] = 0;
2209 return find_free_reg (class, mode, qty, accept_call_clobbered, 1,
2210 born_index, dead_index);
2213 /* We need not check to see if the current function has nonlocal
2214 labels because we don't put any pseudos that are live over calls in
2215 registers in that case. */
2217 if (! accept_call_clobbered
2218 && flag_caller_saves
2219 && ! just_try_suggested
2220 && qty_n_calls_crossed[qty] != 0
2221 && CALLER_SAVE_PROFITABLE (qty_n_refs[qty], qty_n_calls_crossed[qty]))
2223 i = find_free_reg (class, mode, qty, 1, 0, born_index, dead_index);
2224 if (i >= 0)
2225 caller_save_needed = 1;
2226 return i;
2228 return -1;
2231 /* Mark that REGNO with machine-mode MODE is live starting from the current
2232 insn (if LIFE is non-zero) or dead starting at the current insn (if LIFE
2233 is zero). */
2235 static void
2236 mark_life (regno, mode, life)
2237 register int regno;
2238 enum machine_mode mode;
2239 int life;
2241 register int j = HARD_REGNO_NREGS (regno, mode);
2242 if (life)
2243 while (--j >= 0)
2244 SET_HARD_REG_BIT (regs_live, regno + j);
2245 else
2246 while (--j >= 0)
2247 CLEAR_HARD_REG_BIT (regs_live, regno + j);
2250 /* Mark register number REGNO (with machine-mode MODE) as live (if LIFE
2251 is non-zero) or dead (if LIFE is zero) from insn number BIRTH (inclusive)
2252 to insn number DEATH (exclusive). */
2254 static void
2255 post_mark_life (regno, mode, life, birth, death)
2256 int regno;
2257 enum machine_mode mode;
2258 int life, birth, death;
2260 register int j = HARD_REGNO_NREGS (regno, mode);
2261 #ifdef HARD_REG_SET
2262 register /* Declare it register if it's a scalar. */
2263 #endif
2264 HARD_REG_SET this_reg;
2266 CLEAR_HARD_REG_SET (this_reg);
2267 while (--j >= 0)
2268 SET_HARD_REG_BIT (this_reg, regno + j);
2270 if (life)
2271 while (birth < death)
2273 IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
2274 birth++;
2276 else
2277 while (birth < death)
2279 AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
2280 birth++;
2284 /* INSN is the CLOBBER insn that starts a REG_NO_NOCONFLICT block, R0
2285 is the register being clobbered, and R1 is a register being used in
2286 the equivalent expression.
2288 If R1 dies in the block and has a REG_NO_CONFLICT note on every insn
2289 in which it is used, return 1.
2291 Otherwise, return 0. */
2293 static int
2294 no_conflict_p (insn, r0, r1)
2295 rtx insn, r0, r1;
2297 int ok = 0;
2298 rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
2299 rtx p, last;
2301 /* If R1 is a hard register, return 0 since we handle this case
2302 when we scan the insns that actually use it. */
2304 if (note == 0
2305 || (GET_CODE (r1) == REG && REGNO (r1) < FIRST_PSEUDO_REGISTER)
2306 || (GET_CODE (r1) == SUBREG && GET_CODE (SUBREG_REG (r1)) == REG
2307 && REGNO (SUBREG_REG (r1)) < FIRST_PSEUDO_REGISTER))
2308 return 0;
2310 last = XEXP (note, 0);
2312 for (p = NEXT_INSN (insn); p && p != last; p = NEXT_INSN (p))
2313 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
2315 if (find_reg_note (p, REG_DEAD, r1))
2316 ok = 1;
2318 if (reg_mentioned_p (r1, PATTERN (p))
2319 && ! find_reg_note (p, REG_NO_CONFLICT, r1))
2320 return 0;
2323 return ok;
2326 #ifdef REGISTER_CONSTRAINTS
2328 /* Return the number of alternatives for which the constraint string P
2329 indicates that the operand must be equal to operand 0 and that no register
2330 is acceptable. */
2332 static int
2333 requires_inout (p)
2334 char *p;
2336 char c;
2337 int found_zero = 0;
2338 int reg_allowed = 0;
2339 int num_matching_alts = 0;
2341 while (c = *p++)
2342 switch (c)
2344 case '=': case '+': case '?':
2345 case '#': case '&': case '!':
2346 case '*': case '%':
2347 case '1': case '2': case '3': case '4':
2348 case 'm': case '<': case '>': case 'V': case 'o':
2349 case 'E': case 'F': case 'G': case 'H':
2350 case 's': case 'i': case 'n':
2351 case 'I': case 'J': case 'K': case 'L':
2352 case 'M': case 'N': case 'O': case 'P':
2353 #ifdef EXTRA_CONSTRAINT
2354 case 'Q': case 'R': case 'S': case 'T': case 'U':
2355 #endif
2356 case 'X':
2357 /* These don't say anything we care about. */
2358 break;
2360 case ',':
2361 if (found_zero && ! reg_allowed)
2362 num_matching_alts++;
2364 found_zero = reg_allowed = 0;
2365 break;
2367 case '0':
2368 found_zero = 1;
2369 break;
2371 case 'p':
2372 case 'g': case 'r':
2373 default:
2374 reg_allowed = 1;
2375 break;
2378 if (found_zero && ! reg_allowed)
2379 num_matching_alts++;
2381 return num_matching_alts;
2383 #endif /* REGISTER_CONSTRAINTS */
2385 void
2386 dump_local_alloc (file)
2387 FILE *file;
2389 register int i;
2390 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2391 if (reg_renumber[i] != -1)
2392 fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);