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