Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / reg-stack.c
blob4a6faf2b544acfafbd800d1a1c3c27f60303bcf2
1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This pass converts stack-like registers from the "flat register
22 file" model that gcc uses, to a stack convention that the 387 uses.
24 * The form of the input:
26 On input, the function consists of insn that have had their
27 registers fully allocated to a set of "virtual" registers. Note that
28 the word "virtual" is used differently here than elsewhere in gcc: for
29 each virtual stack reg, there is a hard reg, but the mapping between
30 them is not known until this pass is run. On output, hard register
31 numbers have been substituted, and various pop and exchange insns have
32 been emitted. The hard register numbers and the virtual register
33 numbers completely overlap - before this pass, all stack register
34 numbers are virtual, and afterward they are all hard.
36 The virtual registers can be manipulated normally by gcc, and their
37 semantics are the same as for normal registers. After the hard
38 register numbers are substituted, the semantics of an insn containing
39 stack-like regs are not the same as for an insn with normal regs: for
40 instance, it is not safe to delete an insn that appears to be a no-op
41 move. In general, no insn containing hard regs should be changed
42 after this pass is done.
44 * The form of the output:
46 After this pass, hard register numbers represent the distance from
47 the current top of stack to the desired register. A reference to
48 FIRST_STACK_REG references the top of stack, FIRST_STACK_REG + 1,
49 represents the register just below that, and so forth. Also, REG_DEAD
50 notes indicate whether or not a stack register should be popped.
52 A "swap" insn looks like a parallel of two patterns, where each
53 pattern is a SET: one sets A to B, the other B to A.
55 A "push" or "load" insn is a SET whose SET_DEST is FIRST_STACK_REG
56 and whose SET_DEST is REG or MEM. Any other SET_DEST, such as PLUS,
57 will replace the existing stack top, not push a new value.
59 A store insn is a SET whose SET_DEST is FIRST_STACK_REG, and whose
60 SET_SRC is REG or MEM.
62 The case where the SET_SRC and SET_DEST are both FIRST_STACK_REG
63 appears ambiguous. As a special case, the presence of a REG_DEAD note
64 for FIRST_STACK_REG differentiates between a load insn and a pop.
66 If a REG_DEAD is present, the insn represents a "pop" that discards
67 the top of the register stack. If there is no REG_DEAD note, then the
68 insn represents a "dup" or a push of the current top of stack onto the
69 stack.
71 * Methodology:
73 Existing REG_DEAD and REG_UNUSED notes for stack registers are
74 deleted and recreated from scratch. REG_DEAD is never created for a
75 SET_DEST, only REG_UNUSED.
77 Before life analysis, the mode of each insn is set based on whether
78 or not any stack registers are mentioned within that insn. VOIDmode
79 means that no regs are mentioned anyway, and QImode means that at
80 least one pattern within the insn mentions stack registers. This
81 information is valid until after reg_to_stack returns, and is used
82 from jump_optimize.
84 * asm_operands:
86 There are several rules on the usage of stack-like regs in
87 asm_operands insns. These rules apply only to the operands that are
88 stack-like regs:
90 1. Given a set of input regs that die in an asm_operands, it is
91 necessary to know which are implicitly popped by the asm, and
92 which must be explicitly popped by gcc.
94 An input reg that is implicitly popped by the asm must be
95 explicitly clobbered, unless it is constrained to match an
96 output operand.
98 2. For any input reg that is implicitly popped by an asm, it is
99 necessary to know how to adjust the stack to compensate for the pop.
100 If any non-popped input is closer to the top of the reg-stack than
101 the implicitly popped reg, it would not be possible to know what the
102 stack looked like - it's not clear how the rest of the stack "slides
103 up".
105 All implicitly popped input regs must be closer to the top of
106 the reg-stack than any input that is not implicitly popped.
108 3. It is possible that if an input dies in an insn, reload might
109 use the input reg for an output reload. Consider this example:
111 asm ("foo" : "=t" (a) : "f" (b));
113 This asm says that input B is not popped by the asm, and that
114 the asm pushes a result onto the reg-stack, ie, the stack is one
115 deeper after the asm than it was before. But, it is possible that
116 reload will think that it can use the same reg for both the input and
117 the output, if input B dies in this insn.
119 If any input operand uses the "f" constraint, all output reg
120 constraints must use the "&" earlyclobber.
122 The asm above would be written as
124 asm ("foo" : "=&t" (a) : "f" (b));
126 4. Some operands need to be in particular places on the stack. All
127 output operands fall in this category - there is no other way to
128 know which regs the outputs appear in unless the user indicates
129 this in the constraints.
131 Output operands must specifically indicate which reg an output
132 appears in after an asm. "=f" is not allowed: the operand
133 constraints must select a class with a single reg.
135 5. Output operands may not be "inserted" between existing stack regs.
136 Since no 387 opcode uses a read/write operand, all output operands
137 are dead before the asm_operands, and are pushed by the asm_operands.
138 It makes no sense to push anywhere but the top of the reg-stack.
140 Output operands must start at the top of the reg-stack: output
141 operands may not "skip" a reg.
143 6. Some asm statements may need extra stack space for internal
144 calculations. This can be guaranteed by clobbering stack registers
145 unrelated to the inputs and outputs.
147 Here are a couple of reasonable asms to want to write. This asm
148 takes one input, which is internally popped, and produces two outputs.
150 asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
152 This asm takes two inputs, which are popped by the fyl2xp1 opcode,
153 and replaces them with one output. The user must code the "st(1)"
154 clobber for reg-stack.c to know that fyl2xp1 pops both inputs.
156 asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
160 #include "config.h"
161 #include "system.h"
162 #include "tree.h"
163 #include "rtl.h"
164 #include "insn-config.h"
165 #include "regs.h"
166 #include "hard-reg-set.h"
167 #include "flags.h"
168 #include "insn-flags.h"
170 #ifdef STACK_REGS
172 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
174 /* This is the basic stack record. TOP is an index into REG[] such
175 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
177 If TOP is -2, REG[] is not yet initialized. Stack initialization
178 consists of placing each live reg in array `reg' and setting `top'
179 appropriately.
181 REG_SET indicates which registers are live. */
183 typedef struct stack_def
185 int top; /* index to top stack element */
186 HARD_REG_SET reg_set; /* set of live registers */
187 char reg[REG_STACK_SIZE]; /* register - stack mapping */
188 } *stack;
190 /* highest instruction uid */
191 static int max_uid = 0;
193 /* Number of basic blocks in the current function. */
194 static int blocks;
196 /* Element N is first insn in basic block N.
197 This info lasts until we finish compiling the function. */
198 static rtx *block_begin;
200 /* Element N is last insn in basic block N.
201 This info lasts until we finish compiling the function. */
202 static rtx *block_end;
204 /* Element N is nonzero if control can drop into basic block N */
205 static char *block_drops_in;
207 /* Element N says all about the stack at entry block N */
208 static stack block_stack_in;
210 /* Element N says all about the stack life at the end of block N */
211 static HARD_REG_SET *block_out_reg_set;
213 /* This is where the BLOCK_NUM values are really stored. This is set
214 up by find_blocks and used there and in life_analysis. It can be used
215 later, but only to look up an insn that is the head or tail of some
216 block. life_analysis and the stack register conversion process can
217 add insns within a block. */
218 static int *block_number;
220 /* This is the register file for all register after conversion */
221 static rtx
222 FP_mode_reg[LAST_STACK_REG+1-FIRST_STACK_REG][(int) MAX_MACHINE_MODE];
224 #define FP_MODE_REG(regno,mode) \
225 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int)(mode)])
227 /* Get the basic block number of an insn. See note at block_number
228 definition are validity of this information. */
230 #define BLOCK_NUM(INSN) \
231 ((INSN_UID (INSN) > max_uid) \
232 ? (abort() , -1) : block_number[INSN_UID (INSN)])
234 extern rtx forced_labels;
236 /* Forward declarations */
238 static void mark_regs_pat PROTO((rtx, HARD_REG_SET *));
239 static void straighten_stack PROTO((rtx, stack));
240 static void record_label_references PROTO((rtx, rtx));
241 static rtx *get_true_reg PROTO((rtx *));
242 static int constrain_asm_operands PROTO((int, rtx *, char **, int *,
243 enum reg_class *));
245 static void record_asm_reg_life PROTO((rtx,stack, rtx *, char **,
246 int, int));
247 static void record_reg_life_pat PROTO((rtx, HARD_REG_SET *,
248 HARD_REG_SET *, int));
249 static void get_asm_operand_length PROTO((rtx, int, int *, int *));
250 static void record_reg_life PROTO((rtx, int, stack));
251 static void find_blocks PROTO((rtx));
252 static int uses_reg_or_mem PROTO((rtx));
253 static rtx stack_result PROTO((tree));
254 static void stack_reg_life_analysis PROTO((rtx, HARD_REG_SET *));
255 static void replace_reg PROTO((rtx *, int));
256 static void remove_regno_note PROTO((rtx, enum reg_note, int));
257 static int get_hard_regnum PROTO((stack, rtx));
258 static void delete_insn_for_stacker PROTO((rtx));
259 static rtx emit_pop_insn PROTO((rtx, stack, rtx, rtx (*) ()));
260 static void emit_swap_insn PROTO((rtx, stack, rtx));
261 static void move_for_stack_reg PROTO((rtx, stack, rtx));
262 static void swap_rtx_condition PROTO((rtx));
263 static void compare_for_stack_reg PROTO((rtx, stack, rtx));
264 static void subst_stack_regs_pat PROTO((rtx, stack, rtx));
265 static void subst_asm_stack_regs PROTO((rtx, stack, rtx *, rtx **,
266 char **, int, int));
267 static void subst_stack_regs PROTO((rtx, stack));
268 static void change_stack PROTO((rtx, stack, stack, rtx (*) ()));
270 static void goto_block_pat PROTO((rtx, stack, rtx));
271 static void convert_regs PROTO((void));
272 static void print_blocks PROTO((FILE *, rtx, rtx));
273 static void dump_stack_info PROTO((FILE *));
275 /* Mark all registers needed for this pattern. */
277 static void
278 mark_regs_pat (pat, set)
279 rtx pat;
280 HARD_REG_SET *set;
282 enum machine_mode mode;
283 register int regno;
284 register int count;
286 if (GET_CODE (pat) == SUBREG)
288 mode = GET_MODE (pat);
289 regno = SUBREG_WORD (pat);
290 regno += REGNO (SUBREG_REG (pat));
292 else
293 regno = REGNO (pat), mode = GET_MODE (pat);
295 for (count = HARD_REGNO_NREGS (regno, mode);
296 count; count--, regno++)
297 SET_HARD_REG_BIT (*set, regno);
300 /* Reorganise the stack into ascending numbers,
301 after this insn. */
303 static void
304 straighten_stack (insn, regstack)
305 rtx insn;
306 stack regstack;
308 struct stack_def temp_stack;
309 int top;
311 temp_stack.reg_set = regstack->reg_set;
313 for (top = temp_stack.top = regstack->top; top >= 0; top--)
314 temp_stack.reg[top] = FIRST_STACK_REG + temp_stack.top - top;
316 change_stack (insn, regstack, &temp_stack, emit_insn_after);
319 /* Return non-zero if any stack register is mentioned somewhere within PAT. */
322 stack_regs_mentioned_p (pat)
323 rtx pat;
325 register char *fmt;
326 register int i;
328 if (STACK_REG_P (pat))
329 return 1;
331 fmt = GET_RTX_FORMAT (GET_CODE (pat));
332 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
334 if (fmt[i] == 'E')
336 register int j;
338 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
339 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
340 return 1;
342 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
343 return 1;
346 return 0;
349 /* Convert register usage from "flat" register file usage to a "stack
350 register file. FIRST is the first insn in the function, FILE is the
351 dump file, if used.
353 First compute the beginning and end of each basic block. Do a
354 register life analysis on the stack registers, recording the result
355 for the head and tail of each basic block. The convert each insn one
356 by one. Run a last jump_optimize() pass, if optimizing, to eliminate
357 any cross-jumping created when the converter inserts pop insns.*/
359 void
360 reg_to_stack (first, file)
361 rtx first;
362 FILE *file;
364 register rtx insn;
365 register int i;
366 int stack_reg_seen = 0;
367 enum machine_mode mode;
368 HARD_REG_SET stackentry;
370 CLEAR_HARD_REG_SET (stackentry);
373 static initialised;
374 if (!initialised)
376 #if 0
377 initialised = 1; /* This array can not have been previously
378 initialised, because the rtx's are
379 thrown away between compilations of
380 functions. */
381 #endif
382 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
384 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
385 mode = GET_MODE_WIDER_MODE (mode))
386 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
387 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT); mode != VOIDmode;
388 mode = GET_MODE_WIDER_MODE (mode))
389 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
394 /* Count the basic blocks. Also find maximum insn uid. */
396 register RTX_CODE prev_code = BARRIER;
397 register RTX_CODE code;
398 register before_function_beg = 1;
400 max_uid = 0;
401 blocks = 0;
402 for (insn = first; insn; insn = NEXT_INSN (insn))
404 /* Note that this loop must select the same block boundaries
405 as code in find_blocks. Also note that this code is not the
406 same as that used in flow.c. */
408 if (INSN_UID (insn) > max_uid)
409 max_uid = INSN_UID (insn);
411 code = GET_CODE (insn);
413 if (code == CODE_LABEL
414 || (prev_code != INSN
415 && prev_code != CALL_INSN
416 && prev_code != CODE_LABEL
417 && GET_RTX_CLASS (code) == 'i'))
418 blocks++;
420 if (code == NOTE && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
421 before_function_beg = 0;
423 /* Remember whether or not this insn mentions an FP regs.
424 Check JUMP_INSNs too, in case someone creates a funny PARALLEL. */
426 if (GET_RTX_CLASS (code) == 'i'
427 && stack_regs_mentioned_p (PATTERN (insn)))
429 stack_reg_seen = 1;
430 PUT_MODE (insn, QImode);
432 /* Note any register passing parameters. */
434 if (before_function_beg && code == INSN
435 && GET_CODE (PATTERN (insn)) == USE)
436 record_reg_life_pat (PATTERN (insn), (HARD_REG_SET *) 0,
437 &stackentry, 1);
439 else
440 PUT_MODE (insn, VOIDmode);
442 if (code == CODE_LABEL)
443 LABEL_REFS (insn) = insn; /* delete old chain */
445 if (code != NOTE)
446 prev_code = code;
450 /* If no stack register reference exists in this insn, there isn't
451 anything to convert. */
453 if (! stack_reg_seen)
454 return;
456 /* If there are stack registers, there must be at least one block. */
458 if (! blocks)
459 abort ();
461 /* Allocate some tables that last till end of compiling this function
462 and some needed only in find_blocks and life_analysis. */
464 block_begin = (rtx *) alloca (blocks * sizeof (rtx));
465 block_end = (rtx *) alloca (blocks * sizeof (rtx));
466 block_drops_in = (char *) alloca (blocks);
468 block_stack_in = (stack) alloca (blocks * sizeof (struct stack_def));
469 block_out_reg_set = (HARD_REG_SET *) alloca (blocks * sizeof (HARD_REG_SET));
470 bzero ((char *) block_stack_in, blocks * sizeof (struct stack_def));
471 bzero ((char *) block_out_reg_set, blocks * sizeof (HARD_REG_SET));
473 block_number = (int *) alloca ((max_uid + 1) * sizeof (int));
475 find_blocks (first);
476 stack_reg_life_analysis (first, &stackentry);
478 /* Dump the life analysis debug information before jump
479 optimization, as that will destroy the LABEL_REFS we keep the
480 information in. */
482 if (file)
483 dump_stack_info (file);
485 convert_regs ();
487 if (optimize)
488 jump_optimize (first, 2, 0, 0);
491 /* Check PAT, which is in INSN, for LABEL_REFs. Add INSN to the
492 label's chain of references, and note which insn contains each
493 reference. */
495 static void
496 record_label_references (insn, pat)
497 rtx insn, pat;
499 register enum rtx_code code = GET_CODE (pat);
500 register int i;
501 register char *fmt;
503 if (code == LABEL_REF)
505 register rtx label = XEXP (pat, 0);
506 register rtx ref;
508 if (GET_CODE (label) != CODE_LABEL)
509 abort ();
511 /* If this is an undefined label, LABEL_REFS (label) contains
512 garbage. */
513 if (INSN_UID (label) == 0)
514 return;
516 /* Don't make a duplicate in the code_label's chain. */
518 for (ref = LABEL_REFS (label);
519 ref && ref != label;
520 ref = LABEL_NEXTREF (ref))
521 if (CONTAINING_INSN (ref) == insn)
522 return;
524 CONTAINING_INSN (pat) = insn;
525 LABEL_NEXTREF (pat) = LABEL_REFS (label);
526 LABEL_REFS (label) = pat;
528 return;
531 fmt = GET_RTX_FORMAT (code);
532 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
534 if (fmt[i] == 'e')
535 record_label_references (insn, XEXP (pat, i));
536 if (fmt[i] == 'E')
538 register int j;
539 for (j = 0; j < XVECLEN (pat, i); j++)
540 record_label_references (insn, XVECEXP (pat, i, j));
545 /* Return a pointer to the REG expression within PAT. If PAT is not a
546 REG, possible enclosed by a conversion rtx, return the inner part of
547 PAT that stopped the search. */
549 static rtx *
550 get_true_reg (pat)
551 rtx *pat;
553 for (;;)
554 switch (GET_CODE (*pat))
556 case SUBREG:
557 /* eliminate FP subregister accesses in favour of the
558 actual FP register in use. */
560 rtx subreg;
561 if (FP_REG_P (subreg = SUBREG_REG (*pat)))
563 *pat = FP_MODE_REG (REGNO (subreg) + SUBREG_WORD (*pat),
564 GET_MODE (subreg));
565 default:
566 return pat;
569 case FLOAT:
570 case FIX:
571 case FLOAT_EXTEND:
572 pat = & XEXP (*pat, 0);
576 /* Scan the OPERANDS and OPERAND_CONSTRAINTS of an asm_operands.
577 N_OPERANDS is the total number of operands. Return which alternative
578 matched, or -1 is no alternative matches.
580 OPERAND_MATCHES is an array which indicates which operand this
581 operand matches due to the constraints, or -1 if no match is required.
582 If two operands match by coincidence, but are not required to match by
583 the constraints, -1 is returned.
585 OPERAND_CLASS is an array which indicates the smallest class
586 required by the constraints. If the alternative that matches calls
587 for some class `class', and the operand matches a subclass of `class',
588 OPERAND_CLASS is set to `class' as required by the constraints, not to
589 the subclass. If an alternative allows more than one class,
590 OPERAND_CLASS is set to the smallest class that is a union of the
591 allowed classes. */
593 static int
594 constrain_asm_operands (n_operands, operands, operand_constraints,
595 operand_matches, operand_class)
596 int n_operands;
597 rtx *operands;
598 char **operand_constraints;
599 int *operand_matches;
600 enum reg_class *operand_class;
602 char **constraints = (char **) alloca (n_operands * sizeof (char *));
603 char *q;
604 int this_alternative, this_operand;
605 int n_alternatives;
606 int j;
608 for (j = 0; j < n_operands; j++)
609 constraints[j] = operand_constraints[j];
611 /* Compute the number of alternatives in the operands. reload has
612 already guaranteed that all operands have the same number of
613 alternatives. */
615 n_alternatives = 1;
616 for (q = constraints[0]; *q; q++)
617 n_alternatives += (*q == ',');
619 this_alternative = 0;
620 while (this_alternative < n_alternatives)
622 int lose = 0;
623 int i;
625 /* No operands match, no narrow class requirements yet. */
626 for (i = 0; i < n_operands; i++)
628 operand_matches[i] = -1;
629 operand_class[i] = NO_REGS;
632 for (this_operand = 0; this_operand < n_operands; this_operand++)
634 rtx op = operands[this_operand];
635 enum machine_mode mode = GET_MODE (op);
636 char *p = constraints[this_operand];
637 int offset = 0;
638 int win = 0;
639 int c;
641 if (GET_CODE (op) == SUBREG)
643 if (GET_CODE (SUBREG_REG (op)) == REG
644 && REGNO (SUBREG_REG (op)) < FIRST_PSEUDO_REGISTER)
645 offset = SUBREG_WORD (op);
646 op = SUBREG_REG (op);
649 /* An empty constraint or empty alternative
650 allows anything which matched the pattern. */
651 if (*p == 0 || *p == ',')
652 win = 1;
654 while (*p && (c = *p++) != ',')
655 switch (c)
657 case '=': case '+': case '?': case '&': case '!':
658 case '*': case '%':
659 /* Ignore these. */
660 break;
662 case '#':
663 /* Ignore rest of this alternative. */
664 while (*p && *p != ',') p++;
665 break;
667 case '0': case '1': case '2': case '3': case '4':
668 case '5': case '6': case '7': case '8': case '9':
670 /* This operand must be the same as a previous one.
671 This kind of constraint is used for instructions such
672 as add when they take only two operands.
674 Note that the lower-numbered operand is passed first. */
676 if (operands_match_p (operands[c - '0'],
677 operands[this_operand]))
679 operand_matches[this_operand] = c - '0';
680 win = 1;
682 break;
684 case 'p':
685 /* p is used for address_operands. Since this is an asm,
686 just to make sure that the operand is valid for Pmode. */
688 if (strict_memory_address_p (Pmode, op))
689 win = 1;
690 break;
692 case 'g':
693 /* Anything goes unless it is a REG and really has a hard reg
694 but the hard reg is not in the class GENERAL_REGS. */
695 if (GENERAL_REGS == ALL_REGS
696 || GET_CODE (op) != REG
697 || reg_fits_class_p (op, GENERAL_REGS, offset, mode))
699 if (GET_CODE (op) == REG)
700 operand_class[this_operand]
701 = reg_class_subunion[(int) operand_class[this_operand]][(int) GENERAL_REGS];
702 win = 1;
704 break;
706 case 'r':
707 if (GET_CODE (op) == REG
708 && (GENERAL_REGS == ALL_REGS
709 || reg_fits_class_p (op, GENERAL_REGS, offset, mode)))
711 operand_class[this_operand]
712 = reg_class_subunion[(int) operand_class[this_operand]][(int) GENERAL_REGS];
713 win = 1;
715 break;
717 case 'X':
718 /* This is used for a MATCH_SCRATCH in the cases when we
719 don't actually need anything. So anything goes any time. */
720 win = 1;
721 break;
723 case 'm':
724 if (GET_CODE (op) == MEM)
725 win = 1;
726 break;
728 case '<':
729 if (GET_CODE (op) == MEM
730 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
731 || GET_CODE (XEXP (op, 0)) == POST_DEC))
732 win = 1;
733 break;
735 case '>':
736 if (GET_CODE (op) == MEM
737 && (GET_CODE (XEXP (op, 0)) == PRE_INC
738 || GET_CODE (XEXP (op, 0)) == POST_INC))
739 win = 1;
740 break;
742 case 'E':
743 /* Match any CONST_DOUBLE, but only if
744 we can examine the bits of it reliably. */
745 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
746 || HOST_BITS_PER_WIDE_INT != BITS_PER_WORD)
747 && GET_CODE (op) != VOIDmode && ! flag_pretend_float)
748 break;
749 if (GET_CODE (op) == CONST_DOUBLE)
750 win = 1;
751 break;
753 case 'F':
754 if (GET_CODE (op) == CONST_DOUBLE)
755 win = 1;
756 break;
758 case 'G':
759 case 'H':
760 if (GET_CODE (op) == CONST_DOUBLE
761 && CONST_DOUBLE_OK_FOR_LETTER_P (op, c))
762 win = 1;
763 break;
765 case 's':
766 if (GET_CODE (op) == CONST_INT
767 || (GET_CODE (op) == CONST_DOUBLE
768 && GET_MODE (op) == VOIDmode))
769 break;
770 /* Fall through */
771 case 'i':
772 if (CONSTANT_P (op))
773 win = 1;
774 break;
776 case 'n':
777 if (GET_CODE (op) == CONST_INT
778 || (GET_CODE (op) == CONST_DOUBLE
779 && GET_MODE (op) == VOIDmode))
780 win = 1;
781 break;
783 case 'I':
784 case 'J':
785 case 'K':
786 case 'L':
787 case 'M':
788 case 'N':
789 case 'O':
790 case 'P':
791 if (GET_CODE (op) == CONST_INT
792 && CONST_OK_FOR_LETTER_P (INTVAL (op), c))
793 win = 1;
794 break;
796 #ifdef EXTRA_CONSTRAINT
797 case 'Q':
798 case 'R':
799 case 'S':
800 case 'T':
801 case 'U':
802 if (EXTRA_CONSTRAINT (op, c))
803 win = 1;
804 break;
805 #endif
807 case 'V':
808 if (GET_CODE (op) == MEM && ! offsettable_memref_p (op))
809 win = 1;
810 break;
812 case 'o':
813 if (offsettable_memref_p (op))
814 win = 1;
815 break;
817 default:
818 if (GET_CODE (op) == REG
819 && reg_fits_class_p (op, REG_CLASS_FROM_LETTER (c),
820 offset, mode))
822 operand_class[this_operand]
823 = reg_class_subunion[(int)operand_class[this_operand]][(int) REG_CLASS_FROM_LETTER (c)];
824 win = 1;
828 constraints[this_operand] = p;
829 /* If this operand did not win somehow,
830 this alternative loses. */
831 if (! win)
832 lose = 1;
834 /* This alternative won; the operands are ok.
835 Change whichever operands this alternative says to change. */
836 if (! lose)
837 break;
839 this_alternative++;
842 /* For operands constrained to match another operand, copy the other
843 operand's class to this operand's class. */
844 for (j = 0; j < n_operands; j++)
845 if (operand_matches[j] >= 0)
846 operand_class[j] = operand_class[operand_matches[j]];
848 return this_alternative == n_alternatives ? -1 : this_alternative;
851 /* Record the life info of each stack reg in INSN, updating REGSTACK.
852 N_INPUTS is the number of inputs; N_OUTPUTS the outputs. CONSTRAINTS
853 is an array of the constraint strings used in the asm statement.
854 OPERANDS is an array of all operands for the insn, and is assumed to
855 contain all output operands, then all inputs operands.
857 There are many rules that an asm statement for stack-like regs must
858 follow. Those rules are explained at the top of this file: the rule
859 numbers below refer to that explanation. */
861 static void
862 record_asm_reg_life (insn, regstack, operands, constraints,
863 n_inputs, n_outputs)
864 rtx insn;
865 stack regstack;
866 rtx *operands;
867 char **constraints;
868 int n_inputs, n_outputs;
870 int i;
871 int n_operands = n_inputs + n_outputs;
872 int first_input = n_outputs;
873 int n_clobbers;
874 int malformed_asm = 0;
875 rtx body = PATTERN (insn);
877 int *operand_matches = (int *) alloca (n_operands * sizeof (int *));
879 enum reg_class *operand_class
880 = (enum reg_class *) alloca (n_operands * sizeof (enum reg_class *));
882 int reg_used_as_output[FIRST_PSEUDO_REGISTER];
883 int implicitly_dies[FIRST_PSEUDO_REGISTER];
885 rtx *clobber_reg;
887 /* Find out what the constraints require. If no constraint
888 alternative matches, this asm is malformed. */
889 i = constrain_asm_operands (n_operands, operands, constraints,
890 operand_matches, operand_class);
891 if (i < 0)
892 malformed_asm = 1;
894 /* Strip SUBREGs here to make the following code simpler. */
895 for (i = 0; i < n_operands; i++)
896 if (GET_CODE (operands[i]) == SUBREG
897 && GET_CODE (SUBREG_REG (operands[i])) == REG)
898 operands[i] = SUBREG_REG (operands[i]);
900 /* Set up CLOBBER_REG. */
902 n_clobbers = 0;
904 if (GET_CODE (body) == PARALLEL)
906 clobber_reg = (rtx *) alloca (XVECLEN (body, 0) * sizeof (rtx *));
908 for (i = 0; i < XVECLEN (body, 0); i++)
909 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
911 rtx clobber = XVECEXP (body, 0, i);
912 rtx reg = XEXP (clobber, 0);
914 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
915 reg = SUBREG_REG (reg);
917 if (STACK_REG_P (reg))
919 clobber_reg[n_clobbers] = reg;
920 n_clobbers++;
925 /* Enforce rule #4: Output operands must specifically indicate which
926 reg an output appears in after an asm. "=f" is not allowed: the
927 operand constraints must select a class with a single reg.
929 Also enforce rule #5: Output operands must start at the top of
930 the reg-stack: output operands may not "skip" a reg. */
932 bzero ((char *) reg_used_as_output, sizeof (reg_used_as_output));
933 for (i = 0; i < n_outputs; i++)
934 if (STACK_REG_P (operands[i]))
935 if (reg_class_size[(int) operand_class[i]] != 1)
937 error_for_asm
938 (insn, "Output constraint %d must specify a single register", i);
939 malformed_asm = 1;
941 else
942 reg_used_as_output[REGNO (operands[i])] = 1;
945 /* Search for first non-popped reg. */
946 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
947 if (! reg_used_as_output[i])
948 break;
950 /* If there are any other popped regs, that's an error. */
951 for (; i < LAST_STACK_REG + 1; i++)
952 if (reg_used_as_output[i])
953 break;
955 if (i != LAST_STACK_REG + 1)
957 error_for_asm (insn, "Output regs must be grouped at top of stack");
958 malformed_asm = 1;
961 /* Enforce rule #2: All implicitly popped input regs must be closer
962 to the top of the reg-stack than any input that is not implicitly
963 popped. */
965 bzero ((char *) implicitly_dies, sizeof (implicitly_dies));
966 for (i = first_input; i < first_input + n_inputs; i++)
967 if (STACK_REG_P (operands[i]))
969 /* An input reg is implicitly popped if it is tied to an
970 output, or if there is a CLOBBER for it. */
971 int j;
973 for (j = 0; j < n_clobbers; j++)
974 if (operands_match_p (clobber_reg[j], operands[i]))
975 break;
977 if (j < n_clobbers || operand_matches[i] >= 0)
978 implicitly_dies[REGNO (operands[i])] = 1;
981 /* Search for first non-popped reg. */
982 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
983 if (! implicitly_dies[i])
984 break;
986 /* If there are any other popped regs, that's an error. */
987 for (; i < LAST_STACK_REG + 1; i++)
988 if (implicitly_dies[i])
989 break;
991 if (i != LAST_STACK_REG + 1)
993 error_for_asm (insn,
994 "Implicitly popped regs must be grouped at top of stack");
995 malformed_asm = 1;
998 /* Enfore rule #3: If any input operand uses the "f" constraint, all
999 output constraints must use the "&" earlyclobber.
1001 ??? Detect this more deterministically by having constraint_asm_operands
1002 record any earlyclobber. */
1004 for (i = first_input; i < first_input + n_inputs; i++)
1005 if (operand_matches[i] == -1)
1007 int j;
1009 for (j = 0; j < n_outputs; j++)
1010 if (operands_match_p (operands[j], operands[i]))
1012 error_for_asm (insn,
1013 "Output operand %d must use `&' constraint", j);
1014 malformed_asm = 1;
1018 if (malformed_asm)
1020 /* Avoid further trouble with this insn. */
1021 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
1022 PUT_MODE (insn, VOIDmode);
1023 return;
1026 /* Process all outputs */
1027 for (i = 0; i < n_outputs; i++)
1029 rtx op = operands[i];
1031 if (! STACK_REG_P (op))
1032 if (stack_regs_mentioned_p (op))
1033 abort ();
1034 else
1035 continue;
1037 /* Each destination is dead before this insn. If the
1038 destination is not used after this insn, record this with
1039 REG_UNUSED. */
1041 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (op)))
1042 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_UNUSED, op,
1043 REG_NOTES (insn));
1045 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (op));
1048 /* Process all inputs */
1049 for (i = first_input; i < first_input + n_inputs; i++)
1051 if (! STACK_REG_P (operands[i]))
1052 if (stack_regs_mentioned_p (operands[i]))
1053 abort ();
1054 else
1055 continue;
1057 /* If an input is dead after the insn, record a death note.
1058 But don't record a death note if there is already a death note,
1059 or if the input is also an output. */
1061 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (operands[i]))
1062 && operand_matches[i] == -1
1063 && find_regno_note (insn, REG_DEAD, REGNO (operands[i])) == NULL_RTX)
1064 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD, operands[i],
1065 REG_NOTES (insn));
1067 SET_HARD_REG_BIT (regstack->reg_set, REGNO (operands[i]));
1071 /* Scan PAT, which is part of INSN, and record registers appearing in
1072 a SET_DEST in DEST, and other registers in SRC.
1074 This function does not know about SET_DESTs that are both input and
1075 output (such as ZERO_EXTRACT) - this cannot happen on a 387. */
1077 static void
1078 record_reg_life_pat (pat, src, dest, douse)
1079 rtx pat;
1080 HARD_REG_SET *src, *dest;
1081 int douse;
1083 register char *fmt;
1084 register int i;
1086 if (STACK_REG_P (pat)
1087 || (GET_CODE (pat) == SUBREG && STACK_REG_P (SUBREG_REG (pat))))
1089 if (src)
1090 mark_regs_pat (pat, src);
1092 if (dest)
1093 mark_regs_pat (pat, dest);
1095 return;
1098 if (GET_CODE (pat) == SET)
1100 record_reg_life_pat (XEXP (pat, 0), NULL_PTR, dest, 0);
1101 record_reg_life_pat (XEXP (pat, 1), src, NULL_PTR, 0);
1102 return;
1105 /* We don't need to consider either of these cases. */
1106 if (GET_CODE (pat) == USE && !douse || GET_CODE (pat) == CLOBBER)
1107 return;
1109 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1110 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1112 if (fmt[i] == 'E')
1114 register int j;
1116 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1117 record_reg_life_pat (XVECEXP (pat, i, j), src, dest, 0);
1119 else if (fmt[i] == 'e')
1120 record_reg_life_pat (XEXP (pat, i), src, dest, 0);
1124 /* Calculate the number of inputs and outputs in BODY, an
1125 asm_operands. N_OPERANDS is the total number of operands, and
1126 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
1127 placed. */
1129 static void
1130 get_asm_operand_lengths (body, n_operands, n_inputs, n_outputs)
1131 rtx body;
1132 int n_operands;
1133 int *n_inputs, *n_outputs;
1135 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
1136 *n_inputs = ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body));
1138 else if (GET_CODE (body) == ASM_OPERANDS)
1139 *n_inputs = ASM_OPERANDS_INPUT_LENGTH (body);
1141 else if (GET_CODE (body) == PARALLEL
1142 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
1143 *n_inputs = ASM_OPERANDS_INPUT_LENGTH (SET_SRC (XVECEXP (body, 0, 0)));
1145 else if (GET_CODE (body) == PARALLEL
1146 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
1147 *n_inputs = ASM_OPERANDS_INPUT_LENGTH (XVECEXP (body, 0, 0));
1148 else
1149 abort ();
1151 *n_outputs = n_operands - *n_inputs;
1154 /* Scan INSN, which is in BLOCK, and record the life & death of stack
1155 registers in REGSTACK. This function is called to process insns from
1156 the last insn in a block to the first. The actual scanning is done in
1157 record_reg_life_pat.
1159 If a register is live after a CALL_INSN, but is not a value return
1160 register for that CALL_INSN, then code is emitted to initialize that
1161 register. The block_end[] data is kept accurate.
1163 Existing death and unset notes for stack registers are deleted
1164 before processing the insn. */
1166 static void
1167 record_reg_life (insn, block, regstack)
1168 rtx insn;
1169 int block;
1170 stack regstack;
1172 rtx note, *note_link;
1173 int n_operands;
1175 if ((GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
1176 || INSN_DELETED_P (insn))
1177 return;
1179 /* Strip death notes for stack regs from this insn */
1181 note_link = &REG_NOTES(insn);
1182 for (note = *note_link; note; note = XEXP (note, 1))
1183 if (STACK_REG_P (XEXP (note, 0))
1184 && (REG_NOTE_KIND (note) == REG_DEAD
1185 || REG_NOTE_KIND (note) == REG_UNUSED))
1186 *note_link = XEXP (note, 1);
1187 else
1188 note_link = &XEXP (note, 1);
1190 /* Process all patterns in the insn. */
1192 n_operands = asm_noperands (PATTERN (insn));
1193 if (n_operands >= 0)
1195 /* This insn is an `asm' with operands. Decode the operands,
1196 decide how many are inputs, and record the life information. */
1198 rtx operands[MAX_RECOG_OPERANDS];
1199 rtx body = PATTERN (insn);
1200 int n_inputs, n_outputs;
1201 char **constraints = (char **) alloca (n_operands * sizeof (char *));
1203 decode_asm_operands (body, operands, NULL_PTR, constraints, NULL_PTR);
1204 get_asm_operand_lengths (body, n_operands, &n_inputs, &n_outputs);
1205 record_asm_reg_life (insn, regstack, operands, constraints,
1206 n_inputs, n_outputs);
1207 return;
1211 HARD_REG_SET src, dest;
1212 int regno;
1214 CLEAR_HARD_REG_SET (src);
1215 CLEAR_HARD_REG_SET (dest);
1217 if (GET_CODE (insn) == CALL_INSN)
1218 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1219 note;
1220 note = XEXP (note, 1))
1221 if (GET_CODE (XEXP (note, 0)) == USE)
1222 record_reg_life_pat (SET_DEST (XEXP (note, 0)), &src, NULL_PTR, 0);
1224 record_reg_life_pat (PATTERN (insn), &src, &dest, 0);
1225 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
1226 if (! TEST_HARD_REG_BIT (regstack->reg_set, regno))
1228 if (TEST_HARD_REG_BIT (src, regno)
1229 && ! TEST_HARD_REG_BIT (dest, regno))
1230 REG_NOTES (insn)
1231 = gen_rtx_EXPR_LIST (REG_DEAD, FP_MODE_REG (regno, DFmode),
1232 REG_NOTES (insn));
1233 else if (TEST_HARD_REG_BIT (dest, regno))
1234 REG_NOTES (insn)
1235 = gen_rtx_EXPR_LIST (REG_UNUSED, FP_MODE_REG (regno, DFmode),
1236 REG_NOTES (insn));
1239 if (GET_CODE (insn) == CALL_INSN)
1241 int reg;
1243 /* There might be a reg that is live after a function call.
1244 Initialize it to zero so that the program does not crash. See
1245 comment towards the end of stack_reg_life_analysis(). */
1247 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
1248 if (! TEST_HARD_REG_BIT (dest, reg)
1249 && TEST_HARD_REG_BIT (regstack->reg_set, reg))
1251 rtx init, pat;
1253 /* The insn will use virtual register numbers, and so
1254 convert_regs is expected to process these. But BLOCK_NUM
1255 cannot be used on these insns, because they do not appear in
1256 block_number[]. */
1258 pat = gen_rtx_SET (VOIDmode, FP_MODE_REG (reg, DFmode),
1259 CONST0_RTX (DFmode));
1260 init = emit_insn_after (pat, insn);
1261 PUT_MODE (init, QImode);
1263 CLEAR_HARD_REG_BIT (regstack->reg_set, reg);
1265 /* If the CALL_INSN was the end of a block, move the
1266 block_end to point to the new insn. */
1268 if (block_end[block] == insn)
1269 block_end[block] = init;
1272 /* Some regs do not survive a CALL */
1273 AND_COMPL_HARD_REG_SET (regstack->reg_set, call_used_reg_set);
1276 AND_COMPL_HARD_REG_SET (regstack->reg_set, dest);
1277 IOR_HARD_REG_SET (regstack->reg_set, src);
1281 /* Find all basic blocks of the function, which starts with FIRST.
1282 For each JUMP_INSN, build the chain of LABEL_REFS on each CODE_LABEL. */
1284 static void
1285 find_blocks (first)
1286 rtx first;
1288 register rtx insn;
1289 register int block;
1290 register RTX_CODE prev_code = BARRIER;
1291 register RTX_CODE code;
1292 rtx label_value_list = 0;
1294 /* Record where all the blocks start and end.
1295 Record which basic blocks control can drop in to. */
1297 block = -1;
1298 for (insn = first; insn; insn = NEXT_INSN (insn))
1300 /* Note that this loop must select the same block boundaries
1301 as code in reg_to_stack, but that these are not the same
1302 as those selected in flow.c. */
1304 code = GET_CODE (insn);
1306 if (code == CODE_LABEL
1307 || (prev_code != INSN
1308 && prev_code != CALL_INSN
1309 && prev_code != CODE_LABEL
1310 && GET_RTX_CLASS (code) == 'i'))
1312 block_begin[++block] = insn;
1313 block_end[block] = insn;
1314 block_drops_in[block] = prev_code != BARRIER;
1316 else if (GET_RTX_CLASS (code) == 'i')
1317 block_end[block] = insn;
1319 if (GET_RTX_CLASS (code) == 'i')
1321 rtx note;
1323 /* Make a list of all labels referred to other than by jumps. */
1324 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1325 if (REG_NOTE_KIND (note) == REG_LABEL)
1326 label_value_list = gen_rtx_EXPR_LIST (VOIDmode, XEXP (note, 0),
1327 label_value_list);
1330 block_number[INSN_UID (insn)] = block;
1332 if (code != NOTE)
1333 prev_code = code;
1336 if (block + 1 != blocks)
1337 abort ();
1339 /* generate all label references to the corresponding jump insn */
1340 for (block = 0; block < blocks; block++)
1342 insn = block_end[block];
1344 if (GET_CODE (insn) == JUMP_INSN)
1346 rtx pat = PATTERN (insn);
1347 rtx x;
1349 if (computed_jump_p (insn))
1351 for (x = label_value_list; x; x = XEXP (x, 1))
1352 record_label_references (insn,
1353 gen_rtx_LABEL_REF (VOIDmode,
1354 XEXP (x, 0)));
1356 for (x = forced_labels; x; x = XEXP (x, 1))
1357 record_label_references (insn,
1358 gen_rtx_LABEL_REF (VOIDmode,
1359 XEXP (x, 0)));
1362 record_label_references (insn, pat);
1367 /* Return 1 if X contain a REG or MEM that is not in the constant pool. */
1369 static int
1370 uses_reg_or_mem (x)
1371 rtx x;
1373 enum rtx_code code = GET_CODE (x);
1374 int i, j;
1375 char *fmt;
1377 if (code == REG
1378 || (code == MEM
1379 && ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1380 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))))
1381 return 1;
1383 fmt = GET_RTX_FORMAT (code);
1384 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1386 if (fmt[i] == 'e'
1387 && uses_reg_or_mem (XEXP (x, i)))
1388 return 1;
1390 if (fmt[i] == 'E')
1391 for (j = 0; j < XVECLEN (x, i); j++)
1392 if (uses_reg_or_mem (XVECEXP (x, i, j)))
1393 return 1;
1396 return 0;
1399 /* If current function returns its result in an fp stack register,
1400 return the REG. Otherwise, return 0. */
1402 static rtx
1403 stack_result (decl)
1404 tree decl;
1406 rtx result = DECL_RTL (DECL_RESULT (decl));
1408 if (result != 0
1409 && ! (GET_CODE (result) == REG
1410 && REGNO (result) < FIRST_PSEUDO_REGISTER))
1412 #ifdef FUNCTION_OUTGOING_VALUE
1413 result
1414 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
1415 #else
1416 result = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
1417 #endif
1420 return result != 0 && STACK_REG_P (result) ? result : 0;
1423 /* Determine the which registers are live at the start of each basic
1424 block of the function whose first insn is FIRST.
1426 First, if the function returns a real_type, mark the function
1427 return type as live at each return point, as the RTL may not give any
1428 hint that the register is live.
1430 Then, start with the last block and work back to the first block.
1431 Similarly, work backwards within each block, insn by insn, recording
1432 which regs are dead and which are used (and therefore live) in the
1433 hard reg set of block_stack_in[].
1435 After processing each basic block, if there is a label at the start
1436 of the block, propagate the live registers to all jumps to this block.
1438 As a special case, if there are regs live in this block, that are
1439 not live in a block containing a jump to this label, and the block
1440 containing the jump has already been processed, we must propagate this
1441 block's entry register life back to the block containing the jump, and
1442 restart life analysis from there.
1444 In the worst case, this function may traverse the insns
1445 REG_STACK_SIZE times. This is necessary, since a jump towards the end
1446 of the insns may not know that a reg is live at a target that is early
1447 in the insns. So we back up and start over with the new reg live.
1449 If there are registers that are live at the start of the function,
1450 insns are emitted to initialize these registers. Something similar is
1451 done after CALL_INSNs in record_reg_life. */
1453 static void
1454 stack_reg_life_analysis (first, stackentry)
1455 rtx first;
1456 HARD_REG_SET *stackentry;
1458 int reg, block;
1459 struct stack_def regstack;
1462 rtx retvalue;
1464 if (retvalue = stack_result (current_function_decl))
1466 /* Find all RETURN insns and mark them. */
1468 for (block = blocks - 1; --block >= 0;)
1469 if (GET_CODE (block_end[block]) == JUMP_INSN
1470 && GET_CODE (PATTERN (block_end[block])) == RETURN)
1471 mark_regs_pat (retvalue, block_out_reg_set+block);
1473 /* Mark off the end of last block if we "fall off" the end of the
1474 function into the epilogue. */
1476 if (GET_CODE (block_end[blocks-1]) != JUMP_INSN
1477 || GET_CODE (PATTERN (block_end[blocks-1])) == RETURN)
1478 mark_regs_pat (retvalue, block_out_reg_set+blocks-1);
1482 /* now scan all blocks backward for stack register use */
1484 block = blocks - 1;
1485 while (block >= 0)
1487 register rtx insn, prev;
1489 /* current register status at last instruction */
1491 COPY_HARD_REG_SET (regstack.reg_set, block_out_reg_set[block]);
1493 prev = block_end[block];
1496 insn = prev;
1497 prev = PREV_INSN (insn);
1499 /* If the insn is a CALL_INSN, we need to ensure that
1500 everything dies. But otherwise don't process unless there
1501 are some stack regs present. */
1503 if (GET_MODE (insn) == QImode || GET_CODE (insn) == CALL_INSN)
1504 record_reg_life (insn, block, &regstack);
1506 } while (insn != block_begin[block]);
1508 /* Set the state at the start of the block. Mark that no
1509 register mapping information known yet. */
1511 COPY_HARD_REG_SET (block_stack_in[block].reg_set, regstack.reg_set);
1512 block_stack_in[block].top = -2;
1514 /* If there is a label, propagate our register life to all jumps
1515 to this label. */
1517 if (GET_CODE (insn) == CODE_LABEL)
1519 register rtx label;
1520 int must_restart = 0;
1522 for (label = LABEL_REFS (insn); label != insn;
1523 label = LABEL_NEXTREF (label))
1525 int jump_block = BLOCK_NUM (CONTAINING_INSN (label));
1527 if (jump_block < block)
1528 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
1529 block_stack_in[block].reg_set);
1530 else
1532 /* The block containing the jump has already been
1533 processed. If there are registers that were not known
1534 to be live then, but are live now, we must back up
1535 and restart life analysis from that point with the new
1536 life information. */
1538 GO_IF_HARD_REG_SUBSET (block_stack_in[block].reg_set,
1539 block_out_reg_set[jump_block],
1540 win);
1542 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
1543 block_stack_in[block].reg_set);
1545 block = jump_block;
1546 must_restart = 1;
1548 win:
1552 if (must_restart)
1553 continue;
1556 if (block_drops_in[block])
1557 IOR_HARD_REG_SET (block_out_reg_set[block-1],
1558 block_stack_in[block].reg_set);
1560 block -= 1;
1563 /* If any reg is live at the start of the first block of a
1564 function, then we must guarantee that the reg holds some value by
1565 generating our own "load" of that register. Otherwise a 387 would
1566 fault trying to access an empty register. */
1568 /* Load zero into each live register. The fact that a register
1569 appears live at the function start necessarily implies an error
1570 in the user program: it means that (unless the offending code is *never*
1571 executed) this program is using uninitialised floating point
1572 variables. In order to keep broken code like this happy, we initialise
1573 those variables with zero.
1575 Note that we are inserting virtual register references here:
1576 these insns must be processed by convert_regs later. Also, these
1577 insns will not be in block_number, so BLOCK_NUM() will fail for them. */
1579 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
1580 if (TEST_HARD_REG_BIT (block_stack_in[0].reg_set, reg)
1581 && ! TEST_HARD_REG_BIT (*stackentry, reg))
1583 rtx init_rtx;
1585 init_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG(reg, DFmode),
1586 CONST0_RTX (DFmode));
1587 block_begin[0] = emit_insn_after (init_rtx, first);
1588 PUT_MODE (block_begin[0], QImode);
1590 CLEAR_HARD_REG_BIT (block_stack_in[0].reg_set, reg);
1594 /*****************************************************************************
1595 This section deals with stack register substitution, and forms the second
1596 pass over the RTL.
1597 *****************************************************************************/
1599 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
1600 the desired hard REGNO. */
1602 static void
1603 replace_reg (reg, regno)
1604 rtx *reg;
1605 int regno;
1607 if (regno < FIRST_STACK_REG || regno > LAST_STACK_REG
1608 || ! STACK_REG_P (*reg))
1609 abort ();
1611 switch (GET_MODE_CLASS (GET_MODE (*reg)))
1613 default: abort ();
1614 case MODE_FLOAT:
1615 case MODE_COMPLEX_FLOAT:;
1618 *reg = FP_MODE_REG (regno, GET_MODE (*reg));
1621 /* Remove a note of type NOTE, which must be found, for register
1622 number REGNO from INSN. Remove only one such note. */
1624 static void
1625 remove_regno_note (insn, note, regno)
1626 rtx insn;
1627 enum reg_note note;
1628 int regno;
1630 register rtx *note_link, this;
1632 note_link = &REG_NOTES(insn);
1633 for (this = *note_link; this; this = XEXP (this, 1))
1634 if (REG_NOTE_KIND (this) == note
1635 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno)
1637 *note_link = XEXP (this, 1);
1638 return;
1640 else
1641 note_link = &XEXP (this, 1);
1643 abort ();
1646 /* Find the hard register number of virtual register REG in REGSTACK.
1647 The hard register number is relative to the top of the stack. -1 is
1648 returned if the register is not found. */
1650 static int
1651 get_hard_regnum (regstack, reg)
1652 stack regstack;
1653 rtx reg;
1655 int i;
1657 if (! STACK_REG_P (reg))
1658 abort ();
1660 for (i = regstack->top; i >= 0; i--)
1661 if (regstack->reg[i] == REGNO (reg))
1662 break;
1664 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
1667 /* Delete INSN from the RTL. Mark the insn, but don't remove it from
1668 the chain of insns. Doing so could confuse block_begin and block_end
1669 if this were the only insn in the block. */
1671 static void
1672 delete_insn_for_stacker (insn)
1673 rtx insn;
1675 PUT_CODE (insn, NOTE);
1676 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1677 NOTE_SOURCE_FILE (insn) = 0;
1680 /* Emit an insn to pop virtual register REG before or after INSN.
1681 REGSTACK is the stack state after INSN and is updated to reflect this
1682 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
1683 is represented as a SET whose destination is the register to be popped
1684 and source is the top of stack. A death note for the top of stack
1685 cases the movdf pattern to pop. */
1687 static rtx
1688 emit_pop_insn (insn, regstack, reg, when)
1689 rtx insn;
1690 stack regstack;
1691 rtx reg;
1692 rtx (*when)();
1694 rtx pop_insn, pop_rtx;
1695 int hard_regno;
1697 hard_regno = get_hard_regnum (regstack, reg);
1699 if (hard_regno < FIRST_STACK_REG)
1700 abort ();
1702 pop_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG (hard_regno, DFmode),
1703 FP_MODE_REG (FIRST_STACK_REG, DFmode));
1705 pop_insn = (*when) (pop_rtx, insn);
1706 /* ??? This used to be VOIDmode, but that seems wrong. */
1707 PUT_MODE (pop_insn, QImode);
1709 REG_NOTES (pop_insn)
1710 = gen_rtx_EXPR_LIST (REG_DEAD, FP_MODE_REG (FIRST_STACK_REG, DFmode),
1711 REG_NOTES (pop_insn));
1713 regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
1714 = regstack->reg[regstack->top];
1715 regstack->top -= 1;
1716 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
1718 return pop_insn;
1721 /* Emit an insn before or after INSN to swap virtual register REG with the
1722 top of stack. WHEN should be `emit_insn_before' or `emit_insn_before'
1723 REGSTACK is the stack state before the swap, and is updated to reflect
1724 the swap. A swap insn is represented as a PARALLEL of two patterns:
1725 each pattern moves one reg to the other.
1727 If REG is already at the top of the stack, no insn is emitted. */
1729 static void
1730 emit_swap_insn (insn, regstack, reg)
1731 rtx insn;
1732 stack regstack;
1733 rtx reg;
1735 int hard_regno;
1736 rtx gen_swapdf();
1737 rtx swap_rtx, swap_insn;
1738 int tmp, other_reg; /* swap regno temps */
1739 rtx i1; /* the stack-reg insn prior to INSN */
1740 rtx i1set = NULL_RTX; /* the SET rtx within I1 */
1742 hard_regno = get_hard_regnum (regstack, reg);
1744 if (hard_regno < FIRST_STACK_REG)
1745 abort ();
1746 if (hard_regno == FIRST_STACK_REG)
1747 return;
1749 other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
1751 tmp = regstack->reg[other_reg];
1752 regstack->reg[other_reg] = regstack->reg[regstack->top];
1753 regstack->reg[regstack->top] = tmp;
1755 /* Find the previous insn involving stack regs, but don't go past
1756 any labels, calls or jumps. */
1757 i1 = prev_nonnote_insn (insn);
1758 while (i1 && GET_CODE (i1) == INSN && GET_MODE (i1) != QImode)
1759 i1 = prev_nonnote_insn (i1);
1761 if (i1)
1762 i1set = single_set (i1);
1764 if (i1set)
1766 rtx i2; /* the stack-reg insn prior to I1 */
1767 rtx i1src = *get_true_reg (&SET_SRC (i1set));
1768 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
1770 /* If the previous register stack push was from the reg we are to
1771 swap with, omit the swap. */
1773 if (GET_CODE (i1dest) == REG && REGNO (i1dest) == FIRST_STACK_REG
1774 && GET_CODE (i1src) == REG && REGNO (i1src) == hard_regno - 1
1775 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
1776 return;
1778 /* If the previous insn wrote to the reg we are to swap with,
1779 omit the swap. */
1781 if (GET_CODE (i1dest) == REG && REGNO (i1dest) == hard_regno
1782 && GET_CODE (i1src) == REG && REGNO (i1src) == FIRST_STACK_REG
1783 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
1784 return;
1787 if (GET_RTX_CLASS (GET_CODE (i1)) == 'i' && sets_cc0_p (PATTERN (i1)))
1789 i1 = next_nonnote_insn (i1);
1790 if (i1 == insn)
1791 abort ();
1794 swap_rtx = gen_swapdf (FP_MODE_REG (hard_regno, DFmode),
1795 FP_MODE_REG (FIRST_STACK_REG, DFmode));
1796 swap_insn = emit_insn_after (swap_rtx, i1);
1797 /* ??? This used to be VOIDmode, but that seems wrong. */
1798 PUT_MODE (swap_insn, QImode);
1801 /* Handle a move to or from a stack register in PAT, which is in INSN.
1802 REGSTACK is the current stack. */
1804 static void
1805 move_for_stack_reg (insn, regstack, pat)
1806 rtx insn;
1807 stack regstack;
1808 rtx pat;
1810 rtx *psrc = get_true_reg (&SET_SRC (pat));
1811 rtx *pdest = get_true_reg (&SET_DEST (pat));
1812 rtx src, dest;
1813 rtx note;
1815 src = *psrc; dest = *pdest;
1817 if (STACK_REG_P (src) && STACK_REG_P (dest))
1819 /* Write from one stack reg to another. If SRC dies here, then
1820 just change the register mapping and delete the insn. */
1822 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1823 if (note)
1825 int i;
1827 /* If this is a no-op move, there must not be a REG_DEAD note. */
1828 if (REGNO (src) == REGNO (dest))
1829 abort ();
1831 for (i = regstack->top; i >= 0; i--)
1832 if (regstack->reg[i] == REGNO (src))
1833 break;
1835 /* The source must be live, and the dest must be dead. */
1836 if (i < 0 || get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1837 abort ();
1839 /* It is possible that the dest is unused after this insn.
1840 If so, just pop the src. */
1842 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1844 emit_pop_insn (insn, regstack, src, emit_insn_after);
1846 delete_insn_for_stacker (insn);
1847 return;
1850 regstack->reg[i] = REGNO (dest);
1852 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1853 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1855 delete_insn_for_stacker (insn);
1857 return;
1860 /* The source reg does not die. */
1862 /* If this appears to be a no-op move, delete it, or else it
1863 will confuse the machine description output patterns. But if
1864 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1865 for REG_UNUSED will not work for deleted insns. */
1867 if (REGNO (src) == REGNO (dest))
1869 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1870 emit_pop_insn (insn, regstack, dest, emit_insn_after);
1872 delete_insn_for_stacker (insn);
1873 return;
1876 /* The destination ought to be dead */
1877 if (get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1878 abort ();
1880 replace_reg (psrc, get_hard_regnum (regstack, src));
1882 regstack->reg[++regstack->top] = REGNO (dest);
1883 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1884 replace_reg (pdest, FIRST_STACK_REG);
1886 else if (STACK_REG_P (src))
1888 /* Save from a stack reg to MEM, or possibly integer reg. Since
1889 only top of stack may be saved, emit an exchange first if
1890 needs be. */
1892 emit_swap_insn (insn, regstack, src);
1894 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1895 if (note)
1897 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1898 regstack->top--;
1899 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1901 else if (GET_MODE (src) == XFmode && regstack->top < REG_STACK_SIZE - 1)
1903 /* A 387 cannot write an XFmode value to a MEM without
1904 clobbering the source reg. The output code can handle
1905 this by reading back the value from the MEM.
1906 But it is more efficient to use a temp register if one is
1907 available. Push the source value here if the register
1908 stack is not full, and then write the value to memory via
1909 a pop. */
1910 rtx push_rtx, push_insn;
1911 rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, XFmode);
1913 push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
1914 push_insn = emit_insn_before (push_rtx, insn);
1915 PUT_MODE (push_insn, QImode);
1916 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD, top_stack_reg,
1917 REG_NOTES (insn));
1920 replace_reg (psrc, FIRST_STACK_REG);
1922 else if (STACK_REG_P (dest))
1924 /* Load from MEM, or possibly integer REG or constant, into the
1925 stack regs. The actual target is always the top of the
1926 stack. The stack mapping is changed to reflect that DEST is
1927 now at top of stack. */
1929 /* The destination ought to be dead */
1930 if (get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1931 abort ();
1933 if (regstack->top >= REG_STACK_SIZE)
1934 abort ();
1936 regstack->reg[++regstack->top] = REGNO (dest);
1937 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1938 replace_reg (pdest, FIRST_STACK_REG);
1940 else
1941 abort ();
1944 static void
1945 swap_rtx_condition (pat)
1946 rtx pat;
1948 register char *fmt;
1949 register int i;
1951 if (GET_RTX_CLASS (GET_CODE (pat)) == '<')
1953 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1954 return;
1957 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1958 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1960 if (fmt[i] == 'E')
1962 register int j;
1964 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1965 swap_rtx_condition (XVECEXP (pat, i, j));
1967 else if (fmt[i] == 'e')
1968 swap_rtx_condition (XEXP (pat, i));
1972 /* Handle a comparison. Special care needs to be taken to avoid
1973 causing comparisons that a 387 cannot do correctly, such as EQ.
1975 Also, a pop insn may need to be emitted. The 387 does have an
1976 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1977 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1978 set up. */
1980 static void
1981 compare_for_stack_reg (insn, regstack, pat)
1982 rtx insn;
1983 stack regstack;
1984 rtx pat;
1986 rtx *src1, *src2;
1987 rtx src1_note, src2_note;
1988 rtx cc0_user;
1989 int have_cmove;
1991 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1992 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
1993 cc0_user = next_cc0_user (insn);
1995 /* If the insn that uses cc0 is an FP-conditional move, then the destination
1996 must be the top of stack */
1997 if (GET_CODE (PATTERN (cc0_user)) == SET
1998 && SET_DEST (PATTERN (cc0_user)) != pc_rtx
1999 && GET_CODE (SET_SRC (PATTERN (cc0_user))) == IF_THEN_ELSE
2000 && (GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (cc0_user))))
2001 == MODE_FLOAT))
2003 rtx *dest, src_note;
2005 dest = get_true_reg (&SET_DEST (PATTERN (cc0_user)));
2007 have_cmove = 1;
2008 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
2009 && REGNO (*dest) != regstack->reg[regstack->top])
2011 emit_swap_insn (insn, regstack, *dest);
2014 else
2015 have_cmove = 0;
2017 /* ??? If fxch turns out to be cheaper than fstp, give priority to
2018 registers that die in this insn - move those to stack top first. */
2019 if (! STACK_REG_P (*src1)
2020 || (STACK_REG_P (*src2)
2021 && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
2023 rtx temp, next;
2025 temp = XEXP (SET_SRC (pat), 0);
2026 XEXP (SET_SRC (pat), 0) = XEXP (SET_SRC (pat), 1);
2027 XEXP (SET_SRC (pat), 1) = temp;
2029 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
2030 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
2032 next = next_cc0_user (insn);
2033 if (next == NULL_RTX)
2034 abort ();
2036 swap_rtx_condition (PATTERN (next));
2037 INSN_CODE (next) = -1;
2038 INSN_CODE (insn) = -1;
2041 /* We will fix any death note later. */
2043 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2045 if (STACK_REG_P (*src2))
2046 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
2047 else
2048 src2_note = NULL_RTX;
2050 if (! have_cmove)
2051 emit_swap_insn (insn, regstack, *src1);
2053 replace_reg (src1, FIRST_STACK_REG);
2055 if (STACK_REG_P (*src2))
2056 replace_reg (src2, get_hard_regnum (regstack, *src2));
2058 if (src1_note)
2060 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (XEXP (src1_note, 0)));
2061 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2062 regstack->top--;
2065 /* If the second operand dies, handle that. But if the operands are
2066 the same stack register, don't bother, because only one death is
2067 needed, and it was just handled. */
2069 if (src2_note
2070 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
2071 && REGNO (*src1) == REGNO (*src2)))
2073 /* As a special case, two regs may die in this insn if src2 is
2074 next to top of stack and the top of stack also dies. Since
2075 we have already popped src1, "next to top of stack" is really
2076 at top (FIRST_STACK_REG) now. */
2078 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
2079 && src1_note)
2081 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (XEXP (src2_note, 0)));
2082 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
2083 regstack->top--;
2085 else
2087 /* The 386 can only represent death of the first operand in
2088 the case handled above. In all other cases, emit a separate
2089 pop and remove the death note from here. */
2091 link_cc0_insns (insn);
2093 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
2095 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
2096 emit_insn_after);
2101 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
2102 is the current register layout. */
2104 static void
2105 subst_stack_regs_pat (insn, regstack, pat)
2106 rtx insn;
2107 stack regstack;
2108 rtx pat;
2110 rtx *dest, *src;
2111 rtx *src1 = (rtx *) NULL_PTR, *src2;
2112 rtx src1_note, src2_note;
2114 if (GET_CODE (pat) != SET)
2115 return;
2117 dest = get_true_reg (&SET_DEST (pat));
2118 src = get_true_reg (&SET_SRC (pat));
2120 /* See if this is a `movM' pattern, and handle elsewhere if so. */
2122 if (*dest != cc0_rtx
2123 && (STACK_REG_P (*src)
2124 || (STACK_REG_P (*dest)
2125 && (GET_CODE (*src) == REG || GET_CODE (*src) == MEM
2126 || GET_CODE (*src) == CONST_DOUBLE))))
2127 move_for_stack_reg (insn, regstack, pat);
2128 else
2129 switch (GET_CODE (SET_SRC (pat)))
2131 case COMPARE:
2132 compare_for_stack_reg (insn, regstack, pat);
2133 break;
2135 case CALL:
2137 int count;
2138 for (count = HARD_REGNO_NREGS (REGNO (*dest), GET_MODE (*dest));
2139 --count >= 0;)
2141 regstack->reg[++regstack->top] = REGNO (*dest) + count;
2142 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
2145 replace_reg (dest, FIRST_STACK_REG);
2146 break;
2148 case REG:
2149 /* This is a `tstM2' case. */
2150 if (*dest != cc0_rtx)
2151 abort ();
2153 src1 = src;
2155 /* Fall through. */
2157 case FLOAT_TRUNCATE:
2158 case SQRT:
2159 case ABS:
2160 case NEG:
2161 /* These insns only operate on the top of the stack. DEST might
2162 be cc0_rtx if we're processing a tstM pattern. Also, it's
2163 possible that the tstM case results in a REG_DEAD note on the
2164 source. */
2166 if (src1 == 0)
2167 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
2169 emit_swap_insn (insn, regstack, *src1);
2171 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2173 if (STACK_REG_P (*dest))
2174 replace_reg (dest, FIRST_STACK_REG);
2176 if (src1_note)
2178 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2179 regstack->top--;
2180 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
2183 replace_reg (src1, FIRST_STACK_REG);
2185 break;
2187 case MINUS:
2188 case DIV:
2189 /* On i386, reversed forms of subM3 and divM3 exist for
2190 MODE_FLOAT, so the same code that works for addM3 and mulM3
2191 can be used. */
2192 case MULT:
2193 case PLUS:
2194 /* These insns can accept the top of stack as a destination
2195 from a stack reg or mem, or can use the top of stack as a
2196 source and some other stack register (possibly top of stack)
2197 as a destination. */
2199 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
2200 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
2202 /* We will fix any death note later. */
2204 if (STACK_REG_P (*src1))
2205 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2206 else
2207 src1_note = NULL_RTX;
2208 if (STACK_REG_P (*src2))
2209 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
2210 else
2211 src2_note = NULL_RTX;
2213 /* If either operand is not a stack register, then the dest
2214 must be top of stack. */
2216 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
2217 emit_swap_insn (insn, regstack, *dest);
2218 else
2220 /* Both operands are REG. If neither operand is already
2221 at the top of stack, choose to make the one that is the dest
2222 the new top of stack. */
2224 int src1_hard_regnum, src2_hard_regnum;
2226 src1_hard_regnum = get_hard_regnum (regstack, *src1);
2227 src2_hard_regnum = get_hard_regnum (regstack, *src2);
2228 if (src1_hard_regnum == -1 || src2_hard_regnum == -1)
2229 abort ();
2231 if (src1_hard_regnum != FIRST_STACK_REG
2232 && src2_hard_regnum != FIRST_STACK_REG)
2233 emit_swap_insn (insn, regstack, *dest);
2236 if (STACK_REG_P (*src1))
2237 replace_reg (src1, get_hard_regnum (regstack, *src1));
2238 if (STACK_REG_P (*src2))
2239 replace_reg (src2, get_hard_regnum (regstack, *src2));
2241 if (src1_note)
2243 /* If the register that dies is at the top of stack, then
2244 the destination is somewhere else - merely substitute it.
2245 But if the reg that dies is not at top of stack, then
2246 move the top of stack to the dead reg, as though we had
2247 done the insn and then a store-with-pop. */
2249 if (REGNO (XEXP (src1_note, 0)) == regstack->reg[regstack->top])
2251 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2252 replace_reg (dest, get_hard_regnum (regstack, *dest));
2254 else
2256 int regno = get_hard_regnum (regstack, XEXP (src1_note, 0));
2258 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2259 replace_reg (dest, regno);
2261 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
2262 = regstack->reg[regstack->top];
2265 CLEAR_HARD_REG_BIT (regstack->reg_set,
2266 REGNO (XEXP (src1_note, 0)));
2267 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2268 regstack->top--;
2270 else if (src2_note)
2272 if (REGNO (XEXP (src2_note, 0)) == regstack->reg[regstack->top])
2274 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2275 replace_reg (dest, get_hard_regnum (regstack, *dest));
2277 else
2279 int regno = get_hard_regnum (regstack, XEXP (src2_note, 0));
2281 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2282 replace_reg (dest, regno);
2284 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
2285 = regstack->reg[regstack->top];
2288 CLEAR_HARD_REG_BIT (regstack->reg_set,
2289 REGNO (XEXP (src2_note, 0)));
2290 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
2291 regstack->top--;
2293 else
2295 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2296 replace_reg (dest, get_hard_regnum (regstack, *dest));
2299 break;
2301 case UNSPEC:
2302 switch (XINT (SET_SRC (pat), 1))
2304 case 1: /* sin */
2305 case 2: /* cos */
2306 /* These insns only operate on the top of the stack. */
2308 src1 = get_true_reg (&XVECEXP (SET_SRC (pat), 0, 0));
2310 emit_swap_insn (insn, regstack, *src1);
2312 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2314 if (STACK_REG_P (*dest))
2315 replace_reg (dest, FIRST_STACK_REG);
2317 if (src1_note)
2319 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2320 regstack->top--;
2321 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
2324 replace_reg (src1, FIRST_STACK_REG);
2326 break;
2328 default:
2329 abort ();
2331 break;
2333 case IF_THEN_ELSE:
2334 /* This insn requires the top of stack to be the destination. */
2336 src1 = get_true_reg (&XEXP (SET_SRC (pat), 1));
2337 src2 = get_true_reg (&XEXP (SET_SRC (pat), 2));
2339 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2340 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
2343 rtx src_note [3];
2344 int i;
2346 src_note[0] = 0;
2347 src_note[1] = src1_note;
2348 src_note[2] = src2_note;
2350 if (STACK_REG_P (*src1))
2351 replace_reg (src1, get_hard_regnum (regstack, *src1));
2352 if (STACK_REG_P (*src2))
2353 replace_reg (src2, get_hard_regnum (regstack, *src2));
2355 for (i = 1; i <= 2; i++)
2356 if (src_note [i])
2358 /* If the register that dies is not at the top of stack, then
2359 move the top of stack to the dead reg */
2360 if (REGNO (XEXP (src_note[i], 0))
2361 != regstack->reg[regstack->top])
2363 remove_regno_note (insn, REG_DEAD,
2364 REGNO (XEXP (src_note [i], 0)));
2365 emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
2366 emit_insn_after);
2368 else
2370 CLEAR_HARD_REG_BIT (regstack->reg_set,
2371 REGNO (XEXP (src_note[i], 0)));
2372 replace_reg (&XEXP (src_note[i], 0), FIRST_STACK_REG);
2373 regstack->top--;
2378 /* Make dest the top of stack. Add dest to regstack if not present. */
2379 if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
2380 regstack->reg[++regstack->top] = REGNO (*dest);
2381 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2382 replace_reg (dest, FIRST_STACK_REG);
2384 break;
2386 default:
2387 abort ();
2391 /* Substitute hard regnums for any stack regs in INSN, which has
2392 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
2393 before the insn, and is updated with changes made here. CONSTRAINTS is
2394 an array of the constraint strings used in the asm statement.
2396 OPERANDS is an array of the operands, and OPERANDS_LOC is a
2397 parallel array of where the operands were found. The output operands
2398 all precede the input operands.
2400 There are several requirements and assumptions about the use of
2401 stack-like regs in asm statements. These rules are enforced by
2402 record_asm_stack_regs; see comments there for details. Any
2403 asm_operands left in the RTL at this point may be assume to meet the
2404 requirements, since record_asm_stack_regs removes any problem asm. */
2406 static void
2407 subst_asm_stack_regs (insn, regstack, operands, operands_loc, constraints,
2408 n_inputs, n_outputs)
2409 rtx insn;
2410 stack regstack;
2411 rtx *operands, **operands_loc;
2412 char **constraints;
2413 int n_inputs, n_outputs;
2415 int n_operands = n_inputs + n_outputs;
2416 int first_input = n_outputs;
2417 rtx body = PATTERN (insn);
2419 int *operand_matches = (int *) alloca (n_operands * sizeof (int *));
2420 enum reg_class *operand_class
2421 = (enum reg_class *) alloca (n_operands * sizeof (enum reg_class *));
2423 rtx *note_reg; /* Array of note contents */
2424 rtx **note_loc; /* Address of REG field of each note */
2425 enum reg_note *note_kind; /* The type of each note */
2427 rtx *clobber_reg;
2428 rtx **clobber_loc;
2430 struct stack_def temp_stack;
2431 int n_notes;
2432 int n_clobbers;
2433 rtx note;
2434 int i;
2436 /* Find out what the constraints required. If no constraint
2437 alternative matches, that is a compiler bug: we should have caught
2438 such an insn during the life analysis pass (and reload should have
2439 caught it regardless). */
2441 i = constrain_asm_operands (n_operands, operands, constraints,
2442 operand_matches, operand_class);
2443 if (i < 0)
2444 abort ();
2446 /* Strip SUBREGs here to make the following code simpler. */
2447 for (i = 0; i < n_operands; i++)
2448 if (GET_CODE (operands[i]) == SUBREG
2449 && GET_CODE (SUBREG_REG (operands[i])) == REG)
2451 operands_loc[i] = & SUBREG_REG (operands[i]);
2452 operands[i] = SUBREG_REG (operands[i]);
2455 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
2457 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
2458 i++;
2460 note_reg = (rtx *) alloca (i * sizeof (rtx));
2461 note_loc = (rtx **) alloca (i * sizeof (rtx *));
2462 note_kind = (enum reg_note *) alloca (i * sizeof (enum reg_note));
2464 n_notes = 0;
2465 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2467 rtx reg = XEXP (note, 0);
2468 rtx *loc = & XEXP (note, 0);
2470 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
2472 loc = & SUBREG_REG (reg);
2473 reg = SUBREG_REG (reg);
2476 if (STACK_REG_P (reg)
2477 && (REG_NOTE_KIND (note) == REG_DEAD
2478 || REG_NOTE_KIND (note) == REG_UNUSED))
2480 note_reg[n_notes] = reg;
2481 note_loc[n_notes] = loc;
2482 note_kind[n_notes] = REG_NOTE_KIND (note);
2483 n_notes++;
2487 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2489 n_clobbers = 0;
2491 if (GET_CODE (body) == PARALLEL)
2493 clobber_reg = (rtx *) alloca (XVECLEN (body, 0) * sizeof (rtx *));
2494 clobber_loc = (rtx **) alloca (XVECLEN (body, 0) * sizeof (rtx **));
2496 for (i = 0; i < XVECLEN (body, 0); i++)
2497 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
2499 rtx clobber = XVECEXP (body, 0, i);
2500 rtx reg = XEXP (clobber, 0);
2501 rtx *loc = & XEXP (clobber, 0);
2503 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
2505 loc = & SUBREG_REG (reg);
2506 reg = SUBREG_REG (reg);
2509 if (STACK_REG_P (reg))
2511 clobber_reg[n_clobbers] = reg;
2512 clobber_loc[n_clobbers] = loc;
2513 n_clobbers++;
2518 bcopy ((char *) regstack, (char *) &temp_stack, sizeof (temp_stack));
2520 /* Put the input regs into the desired place in TEMP_STACK. */
2522 for (i = first_input; i < first_input + n_inputs; i++)
2523 if (STACK_REG_P (operands[i])
2524 && reg_class_subset_p (operand_class[i], FLOAT_REGS)
2525 && operand_class[i] != FLOAT_REGS)
2527 /* If an operand needs to be in a particular reg in
2528 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2529 these constraints are for single register classes, and reload
2530 guaranteed that operand[i] is already in that class, we can
2531 just use REGNO (operands[i]) to know which actual reg this
2532 operand needs to be in. */
2534 int regno = get_hard_regnum (&temp_stack, operands[i]);
2536 if (regno < 0)
2537 abort ();
2539 if (regno != REGNO (operands[i]))
2541 /* operands[i] is not in the right place. Find it
2542 and swap it with whatever is already in I's place.
2543 K is where operands[i] is now. J is where it should
2544 be. */
2545 int j, k, temp;
2547 k = temp_stack.top - (regno - FIRST_STACK_REG);
2548 j = (temp_stack.top
2549 - (REGNO (operands[i]) - FIRST_STACK_REG));
2551 temp = temp_stack.reg[k];
2552 temp_stack.reg[k] = temp_stack.reg[j];
2553 temp_stack.reg[j] = temp;
2557 /* emit insns before INSN to make sure the reg-stack is in the right
2558 order. */
2560 change_stack (insn, regstack, &temp_stack, emit_insn_before);
2562 /* Make the needed input register substitutions. Do death notes and
2563 clobbers too, because these are for inputs, not outputs. */
2565 for (i = first_input; i < first_input + n_inputs; i++)
2566 if (STACK_REG_P (operands[i]))
2568 int regnum = get_hard_regnum (regstack, operands[i]);
2570 if (regnum < 0)
2571 abort ();
2573 replace_reg (operands_loc[i], regnum);
2576 for (i = 0; i < n_notes; i++)
2577 if (note_kind[i] == REG_DEAD)
2579 int regnum = get_hard_regnum (regstack, note_reg[i]);
2581 if (regnum < 0)
2582 abort ();
2584 replace_reg (note_loc[i], regnum);
2587 for (i = 0; i < n_clobbers; i++)
2589 /* It's OK for a CLOBBER to reference a reg that is not live.
2590 Don't try to replace it in that case. */
2591 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2593 if (regnum >= 0)
2595 /* Sigh - clobbers always have QImode. But replace_reg knows
2596 that these regs can't be MODE_INT and will abort. Just put
2597 the right reg there without calling replace_reg. */
2599 *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
2603 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2605 for (i = first_input; i < first_input + n_inputs; i++)
2606 if (STACK_REG_P (operands[i]))
2608 /* An input reg is implicitly popped if it is tied to an
2609 output, or if there is a CLOBBER for it. */
2610 int j;
2612 for (j = 0; j < n_clobbers; j++)
2613 if (operands_match_p (clobber_reg[j], operands[i]))
2614 break;
2616 if (j < n_clobbers || operand_matches[i] >= 0)
2618 /* operands[i] might not be at the top of stack. But that's OK,
2619 because all we need to do is pop the right number of regs
2620 off of the top of the reg-stack. record_asm_stack_regs
2621 guaranteed that all implicitly popped regs were grouped
2622 at the top of the reg-stack. */
2624 CLEAR_HARD_REG_BIT (regstack->reg_set,
2625 regstack->reg[regstack->top]);
2626 regstack->top--;
2630 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2631 Note that there isn't any need to substitute register numbers.
2632 ??? Explain why this is true. */
2634 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2636 /* See if there is an output for this hard reg. */
2637 int j;
2639 for (j = 0; j < n_outputs; j++)
2640 if (STACK_REG_P (operands[j]) && REGNO (operands[j]) == i)
2642 regstack->reg[++regstack->top] = i;
2643 SET_HARD_REG_BIT (regstack->reg_set, i);
2644 break;
2648 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2649 input that the asm didn't implicitly pop. If the asm didn't
2650 implicitly pop an input reg, that reg will still be live.
2652 Note that we can't use find_regno_note here: the register numbers
2653 in the death notes have already been substituted. */
2655 for (i = 0; i < n_outputs; i++)
2656 if (STACK_REG_P (operands[i]))
2658 int j;
2660 for (j = 0; j < n_notes; j++)
2661 if (REGNO (operands[i]) == REGNO (note_reg[j])
2662 && note_kind[j] == REG_UNUSED)
2664 insn = emit_pop_insn (insn, regstack, operands[i],
2665 emit_insn_after);
2666 break;
2670 for (i = first_input; i < first_input + n_inputs; i++)
2671 if (STACK_REG_P (operands[i]))
2673 int j;
2675 for (j = 0; j < n_notes; j++)
2676 if (REGNO (operands[i]) == REGNO (note_reg[j])
2677 && note_kind[j] == REG_DEAD
2678 && TEST_HARD_REG_BIT (regstack->reg_set, REGNO (operands[i])))
2680 insn = emit_pop_insn (insn, regstack, operands[i],
2681 emit_insn_after);
2682 break;
2687 /* Substitute stack hard reg numbers for stack virtual registers in
2688 INSN. Non-stack register numbers are not changed. REGSTACK is the
2689 current stack content. Insns may be emitted as needed to arrange the
2690 stack for the 387 based on the contents of the insn. */
2692 static void
2693 subst_stack_regs (insn, regstack)
2694 rtx insn;
2695 stack regstack;
2697 register rtx *note_link, note;
2698 register int i;
2699 rtx head, jump, pat, cipat;
2700 int n_operands;
2702 if (GET_CODE (insn) == CALL_INSN)
2704 int top = regstack->top;
2706 /* If there are any floating point parameters to be passed in
2707 registers for this call, make sure they are in the right
2708 order. */
2710 if (top >= 0)
2712 straighten_stack (PREV_INSN (insn), regstack);
2714 /* Now mark the arguments as dead after the call. */
2716 while (regstack->top >= 0)
2718 CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
2719 regstack->top--;
2724 /* Do the actual substitution if any stack regs are mentioned.
2725 Since we only record whether entire insn mentions stack regs, and
2726 subst_stack_regs_pat only works for patterns that contain stack regs,
2727 we must check each pattern in a parallel here. A call_value_pop could
2728 fail otherwise. */
2730 if (GET_MODE (insn) == QImode)
2732 n_operands = asm_noperands (PATTERN (insn));
2733 if (n_operands >= 0)
2735 /* This insn is an `asm' with operands. Decode the operands,
2736 decide how many are inputs, and do register substitution.
2737 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2739 rtx operands[MAX_RECOG_OPERANDS];
2740 rtx *operands_loc[MAX_RECOG_OPERANDS];
2741 rtx body = PATTERN (insn);
2742 int n_inputs, n_outputs;
2743 char **constraints
2744 = (char **) alloca (n_operands * sizeof (char *));
2746 decode_asm_operands (body, operands, operands_loc,
2747 constraints, NULL_PTR);
2748 get_asm_operand_lengths (body, n_operands, &n_inputs, &n_outputs);
2749 subst_asm_stack_regs (insn, regstack, operands, operands_loc,
2750 constraints, n_inputs, n_outputs);
2751 return;
2754 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2755 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2757 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2758 subst_stack_regs_pat (insn, regstack,
2759 XVECEXP (PATTERN (insn), 0, i));
2761 else
2762 subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2765 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2766 REG_UNUSED will already have been dealt with, so just return. */
2768 if (GET_CODE (insn) == NOTE)
2769 return;
2771 /* If we are reached by a computed goto which sets this same stack register,
2772 then pop this stack register, but maintain regstack. */
2774 pat = single_set (insn);
2775 if (pat != 0
2776 && INSN_UID (insn) <= max_uid
2777 && GET_CODE (block_begin[BLOCK_NUM(insn)]) == CODE_LABEL
2778 && GET_CODE (pat) == SET && STACK_REG_P (SET_DEST (pat)))
2779 for (head = block_begin[BLOCK_NUM(insn)], jump = LABEL_REFS (head);
2780 jump != head;
2781 jump = LABEL_NEXTREF (jump))
2783 cipat = single_set (CONTAINING_INSN (jump));
2784 if (cipat != 0
2785 && GET_CODE (cipat) == SET
2786 && SET_DEST (cipat) == pc_rtx
2787 && uses_reg_or_mem (SET_SRC (cipat))
2788 && INSN_UID (CONTAINING_INSN (jump)) <= max_uid)
2790 int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
2791 if (TEST_HARD_REG_BIT (block_out_reg_set[from_block],
2792 REGNO (SET_DEST (pat))))
2794 struct stack_def old;
2795 bcopy (regstack->reg, old.reg, sizeof (old.reg));
2796 emit_pop_insn (insn, regstack, SET_DEST (pat), emit_insn_before);
2797 regstack->top += 1;
2798 bcopy (old.reg, regstack->reg, sizeof (old.reg));
2799 SET_HARD_REG_BIT (regstack->reg_set, REGNO (SET_DEST (pat)));
2804 /* If there is a REG_UNUSED note on a stack register on this insn,
2805 the indicated reg must be popped. The REG_UNUSED note is removed,
2806 since the form of the newly emitted pop insn references the reg,
2807 making it no longer `unset'. */
2809 note_link = &REG_NOTES(insn);
2810 for (note = *note_link; note; note = XEXP (note, 1))
2811 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2813 *note_link = XEXP (note, 1);
2814 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), emit_insn_after);
2816 else
2817 note_link = &XEXP (note, 1);
2820 /* Change the organization of the stack so that it fits a new basic
2821 block. Some registers might have to be popped, but there can never be
2822 a register live in the new block that is not now live.
2824 Insert any needed insns before or after INSN. WHEN is emit_insn_before
2825 or emit_insn_after. OLD is the original stack layout, and NEW is
2826 the desired form. OLD is updated to reflect the code emitted, ie, it
2827 will be the same as NEW upon return.
2829 This function will not preserve block_end[]. But that information
2830 is no longer needed once this has executed. */
2832 static void
2833 change_stack (insn, old, new, when)
2834 rtx insn;
2835 stack old;
2836 stack new;
2837 rtx (*when)();
2839 int reg;
2841 /* We will be inserting new insns "backwards", by calling emit_insn_before.
2842 If we are to insert after INSN, find the next insn, and insert before
2843 it. */
2845 if (when == emit_insn_after)
2846 insn = NEXT_INSN (insn);
2848 /* Pop any registers that are not needed in the new block. */
2850 for (reg = old->top; reg >= 0; reg--)
2851 if (! TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2852 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[reg], DFmode),
2853 emit_insn_before);
2855 if (new->top == -2)
2857 /* If the new block has never been processed, then it can inherit
2858 the old stack order. */
2860 new->top = old->top;
2861 bcopy (old->reg, new->reg, sizeof (new->reg));
2863 else
2865 /* This block has been entered before, and we must match the
2866 previously selected stack order. */
2868 /* By now, the only difference should be the order of the stack,
2869 not their depth or liveliness. */
2871 GO_IF_HARD_REG_EQUAL (old->reg_set, new->reg_set, win);
2873 abort ();
2875 win:
2877 if (old->top != new->top)
2878 abort ();
2880 /* Loop here emitting swaps until the stack is correct. The
2881 worst case number of swaps emitted is N + 2, where N is the
2882 depth of the stack. In some cases, the reg at the top of
2883 stack may be correct, but swapped anyway in order to fix
2884 other regs. But since we never swap any other reg away from
2885 its correct slot, this algorithm will converge. */
2889 /* Swap the reg at top of stack into the position it is
2890 supposed to be in, until the correct top of stack appears. */
2892 while (old->reg[old->top] != new->reg[new->top])
2894 for (reg = new->top; reg >= 0; reg--)
2895 if (new->reg[reg] == old->reg[old->top])
2896 break;
2898 if (reg == -1)
2899 abort ();
2901 emit_swap_insn (insn, old,
2902 FP_MODE_REG (old->reg[reg], DFmode));
2905 /* See if any regs remain incorrect. If so, bring an
2906 incorrect reg to the top of stack, and let the while loop
2907 above fix it. */
2909 for (reg = new->top; reg >= 0; reg--)
2910 if (new->reg[reg] != old->reg[reg])
2912 emit_swap_insn (insn, old,
2913 FP_MODE_REG (old->reg[reg], DFmode));
2914 break;
2916 } while (reg >= 0);
2918 /* At this point there must be no differences. */
2920 for (reg = old->top; reg >= 0; reg--)
2921 if (old->reg[reg] != new->reg[reg])
2922 abort ();
2926 /* Check PAT, which points to RTL in INSN, for a LABEL_REF. If it is
2927 found, ensure that a jump from INSN to the code_label to which the
2928 label_ref points ends up with the same stack as that at the
2929 code_label. Do this by inserting insns just before the code_label to
2930 pop and rotate the stack until it is in the correct order. REGSTACK
2931 is the order of the register stack in INSN.
2933 Any code that is emitted here must not be later processed as part
2934 of any block, as it will already contain hard register numbers. */
2936 static void
2937 goto_block_pat (insn, regstack, pat)
2938 rtx insn;
2939 stack regstack;
2940 rtx pat;
2942 rtx label;
2943 rtx new_jump, new_label, new_barrier;
2944 rtx *ref;
2945 stack label_stack;
2946 struct stack_def temp_stack;
2947 int reg;
2949 switch (GET_CODE (pat))
2951 case RETURN:
2952 straighten_stack (PREV_INSN (insn), regstack);
2953 return;
2954 default:
2956 int i, j;
2957 char *fmt = GET_RTX_FORMAT (GET_CODE (pat));
2959 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
2961 if (fmt[i] == 'e')
2962 goto_block_pat (insn, regstack, XEXP (pat, i));
2963 if (fmt[i] == 'E')
2964 for (j = 0; j < XVECLEN (pat, i); j++)
2965 goto_block_pat (insn, regstack, XVECEXP (pat, i, j));
2967 return;
2969 case LABEL_REF:;
2972 label = XEXP (pat, 0);
2973 if (GET_CODE (label) != CODE_LABEL)
2974 abort ();
2976 /* First, see if in fact anything needs to be done to the stack at all. */
2977 if (INSN_UID (label) <= 0)
2978 return;
2980 label_stack = &block_stack_in[BLOCK_NUM (label)];
2982 if (label_stack->top == -2)
2984 /* If the target block hasn't had a stack order selected, then
2985 we need merely ensure that no pops are needed. */
2987 for (reg = regstack->top; reg >= 0; reg--)
2988 if (! TEST_HARD_REG_BIT (label_stack->reg_set, regstack->reg[reg]))
2989 break;
2991 if (reg == -1)
2993 /* change_stack will not emit any code in this case. */
2995 change_stack (label, regstack, label_stack, emit_insn_after);
2996 return;
2999 else if (label_stack->top == regstack->top)
3001 for (reg = label_stack->top; reg >= 0; reg--)
3002 if (label_stack->reg[reg] != regstack->reg[reg])
3003 break;
3005 if (reg == -1)
3006 return;
3009 /* At least one insn will need to be inserted before label. Insert
3010 a jump around the code we are about to emit. Emit a label for the new
3011 code, and point the original insn at this new label. We can't use
3012 redirect_jump here, because we're using fld[4] of the code labels as
3013 LABEL_REF chains, no NUSES counters. */
3015 new_jump = emit_jump_insn_before (gen_jump (label), label);
3016 record_label_references (new_jump, PATTERN (new_jump));
3017 JUMP_LABEL (new_jump) = label;
3019 new_barrier = emit_barrier_after (new_jump);
3021 new_label = gen_label_rtx ();
3022 emit_label_after (new_label, new_barrier);
3023 LABEL_REFS (new_label) = new_label;
3025 /* The old label_ref will no longer point to the code_label if now uses,
3026 so strip the label_ref from the code_label's chain of references. */
3028 for (ref = &LABEL_REFS (label); *ref != label; ref = &LABEL_NEXTREF (*ref))
3029 if (*ref == pat)
3030 break;
3032 if (*ref == label)
3033 abort ();
3035 *ref = LABEL_NEXTREF (*ref);
3037 XEXP (pat, 0) = new_label;
3038 record_label_references (insn, PATTERN (insn));
3040 if (JUMP_LABEL (insn) == label)
3041 JUMP_LABEL (insn) = new_label;
3043 /* Now emit the needed code. */
3045 temp_stack = *regstack;
3047 change_stack (new_label, &temp_stack, label_stack, emit_insn_after);
3050 /* Traverse all basic blocks in a function, converting the register
3051 references in each insn from the "flat" register file that gcc uses, to
3052 the stack-like registers the 387 uses. */
3054 static void
3055 convert_regs ()
3057 register int block, reg;
3058 register rtx insn, next;
3059 struct stack_def regstack;
3061 for (block = 0; block < blocks; block++)
3063 if (block_stack_in[block].top == -2)
3065 /* This block has not been previously encountered. Choose a
3066 default mapping for any stack regs live on entry */
3068 block_stack_in[block].top = -1;
3070 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
3071 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, reg))
3072 block_stack_in[block].reg[++block_stack_in[block].top] = reg;
3075 /* Process all insns in this block. Keep track of `next' here,
3076 so that we don't process any insns emitted while making
3077 substitutions in INSN. */
3079 next = block_begin[block];
3080 regstack = block_stack_in[block];
3083 insn = next;
3084 next = NEXT_INSN (insn);
3086 /* Don't bother processing unless there is a stack reg
3087 mentioned or if it's a CALL_INSN (register passing of
3088 floating point values). */
3090 if (GET_MODE (insn) == QImode || GET_CODE (insn) == CALL_INSN)
3091 subst_stack_regs (insn, &regstack);
3093 } while (insn != block_end[block]);
3095 /* Something failed if the stack life doesn't match. */
3097 GO_IF_HARD_REG_EQUAL (regstack.reg_set, block_out_reg_set[block], win);
3099 abort ();
3101 win:
3103 /* Adjust the stack of this block on exit to match the stack of
3104 the target block, or copy stack information into stack of
3105 jump target if the target block's stack order hasn't been set
3106 yet. */
3108 if (GET_CODE (insn) == JUMP_INSN)
3109 goto_block_pat (insn, &regstack, PATTERN (insn));
3111 /* Likewise handle the case where we fall into the next block. */
3113 if ((block < blocks - 1) && block_drops_in[block+1])
3114 change_stack (insn, &regstack, &block_stack_in[block+1],
3115 emit_insn_after);
3118 /* If the last basic block is the end of a loop, and that loop has
3119 regs live at its start, then the last basic block will have regs live
3120 at its end that need to be popped before the function returns. */
3123 int value_reg_low, value_reg_high;
3124 value_reg_low = value_reg_high = -1;
3126 rtx retvalue;
3127 if (retvalue = stack_result (current_function_decl))
3129 value_reg_low = REGNO (retvalue);
3130 value_reg_high = value_reg_low +
3131 HARD_REGNO_NREGS (value_reg_low, GET_MODE (retvalue)) - 1;
3135 for (reg = regstack.top; reg >= 0; reg--)
3136 if (regstack.reg[reg] < value_reg_low
3137 || regstack.reg[reg] > value_reg_high)
3138 insn = emit_pop_insn (insn, &regstack,
3139 FP_MODE_REG (regstack.reg[reg], DFmode),
3140 emit_insn_after);
3142 straighten_stack (insn, &regstack);
3145 /* Check expression PAT, which is in INSN, for label references. if
3146 one is found, print the block number of destination to FILE. */
3148 static void
3149 print_blocks (file, insn, pat)
3150 FILE *file;
3151 rtx insn, pat;
3153 register RTX_CODE code = GET_CODE (pat);
3154 register int i;
3155 register char *fmt;
3157 if (code == LABEL_REF)
3159 register rtx label = XEXP (pat, 0);
3161 if (GET_CODE (label) != CODE_LABEL)
3162 abort ();
3164 fprintf (file, " %d", BLOCK_NUM (label));
3166 return;
3169 fmt = GET_RTX_FORMAT (code);
3170 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3172 if (fmt[i] == 'e')
3173 print_blocks (file, insn, XEXP (pat, i));
3174 if (fmt[i] == 'E')
3176 register int j;
3177 for (j = 0; j < XVECLEN (pat, i); j++)
3178 print_blocks (file, insn, XVECEXP (pat, i, j));
3183 /* Write information about stack registers and stack blocks into FILE.
3184 This is part of making a debugging dump. */
3186 static void
3187 dump_stack_info (file)
3188 FILE *file;
3190 register int block;
3192 fprintf (file, "\n%d stack blocks.\n", blocks);
3193 for (block = 0; block < blocks; block++)
3195 register rtx head, jump, end;
3196 register int regno;
3198 fprintf (file, "\nStack block %d: first insn %d, last %d.\n",
3199 block, INSN_UID (block_begin[block]),
3200 INSN_UID (block_end[block]));
3202 head = block_begin[block];
3204 fprintf (file, "Reached from blocks: ");
3205 if (GET_CODE (head) == CODE_LABEL)
3206 for (jump = LABEL_REFS (head);
3207 jump != head;
3208 jump = LABEL_NEXTREF (jump))
3210 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
3211 fprintf (file, " %d", from_block);
3213 if (block_drops_in[block])
3214 fprintf (file, " previous");
3216 fprintf (file, "\nlive stack registers on block entry: ");
3217 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
3219 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, regno))
3220 fprintf (file, "%d ", regno);
3223 fprintf (file, "\nlive stack registers on block exit: ");
3224 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
3226 if (TEST_HARD_REG_BIT (block_out_reg_set[block], regno))
3227 fprintf (file, "%d ", regno);
3230 end = block_end[block];
3232 fprintf (file, "\nJumps to blocks: ");
3233 if (GET_CODE (end) == JUMP_INSN)
3234 print_blocks (file, end, PATTERN (end));
3236 if (block + 1 < blocks && block_drops_in[block+1])
3237 fprintf (file, " next");
3238 else if (block + 1 == blocks
3239 || (GET_CODE (end) == JUMP_INSN
3240 && GET_CODE (PATTERN (end)) == RETURN))
3241 fprintf (file, " return");
3243 fprintf (file, "\n");
3246 #endif /* STACK_REGS */