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