1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1992, 93-98, 1999 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)
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
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.
79 There are several rules on the usage of stack-like regs in
80 asm_operands insns. These rules apply only to the operands that are
83 1. Given a set of input regs that die in an asm_operands, it is
84 necessary to know which are implicitly popped by the asm, and
85 which must be explicitly popped by gcc.
87 An input reg that is implicitly popped by the asm must be
88 explicitly clobbered, unless it is constrained to match an
91 2. For any input reg that is implicitly popped by an asm, it is
92 necessary to know how to adjust the stack to compensate for the pop.
93 If any non-popped input is closer to the top of the reg-stack than
94 the implicitly popped reg, it would not be possible to know what the
95 stack looked like - it's not clear how the rest of the stack "slides
98 All implicitly popped input regs must be closer to the top of
99 the reg-stack than any input that is not implicitly popped.
101 3. It is possible that if an input dies in an insn, reload might
102 use the input reg for an output reload. Consider this example:
104 asm ("foo" : "=t" (a) : "f" (b));
106 This asm says that input B is not popped by the asm, and that
107 the asm pushes a result onto the reg-stack, ie, the stack is one
108 deeper after the asm than it was before. But, it is possible that
109 reload will think that it can use the same reg for both the input and
110 the output, if input B dies in this insn.
112 If any input operand uses the "f" constraint, all output reg
113 constraints must use the "&" earlyclobber.
115 The asm above would be written as
117 asm ("foo" : "=&t" (a) : "f" (b));
119 4. Some operands need to be in particular places on the stack. All
120 output operands fall in this category - there is no other way to
121 know which regs the outputs appear in unless the user indicates
122 this in the constraints.
124 Output operands must specifically indicate which reg an output
125 appears in after an asm. "=f" is not allowed: the operand
126 constraints must select a class with a single reg.
128 5. Output operands may not be "inserted" between existing stack regs.
129 Since no 387 opcode uses a read/write operand, all output operands
130 are dead before the asm_operands, and are pushed by the asm_operands.
131 It makes no sense to push anywhere but the top of the reg-stack.
133 Output operands must start at the top of the reg-stack: output
134 operands may not "skip" a reg.
136 6. Some asm statements may need extra stack space for internal
137 calculations. This can be guaranteed by clobbering stack registers
138 unrelated to the inputs and outputs.
140 Here are a couple of reasonable asms to want to write. This asm
141 takes one input, which is internally popped, and produces two outputs.
143 asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
145 This asm takes two inputs, which are popped by the fyl2xp1 opcode,
146 and replaces them with one output. The user must code the "st(1)"
147 clobber for reg-stack.c to know that fyl2xp1 pops both inputs.
149 asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
157 #include "insn-config.h"
159 #include "hard-reg-set.h"
161 #include "insn-flags.h"
168 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
170 /* This is the basic stack record. TOP is an index into REG[] such
171 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
173 If TOP is -2, REG[] is not yet initialized. Stack initialization
174 consists of placing each live reg in array `reg' and setting `top'
177 REG_SET indicates which registers are live. */
179 typedef struct stack_def
181 int top; /* index to top stack element */
182 HARD_REG_SET reg_set; /* set of live registers */
183 char reg[REG_STACK_SIZE]; /* register - stack mapping */
186 /* highest instruction uid */
187 static int max_uid = 0;
189 /* Number of basic blocks in the current function. */
192 /* Element N is first insn in basic block N.
193 This info lasts until we finish compiling the function. */
194 static rtx *block_begin;
196 /* Element N is last insn in basic block N.
197 This info lasts until we finish compiling the function. */
198 static rtx *block_end;
200 /* Element N is nonzero if control can drop into basic block N */
201 static char *block_drops_in;
203 /* Element N says all about the stack at entry block N */
204 static stack block_stack_in;
206 /* Element N says all about the stack life at the end of block N */
207 static HARD_REG_SET *block_out_reg_set;
209 /* This is where the BLOCK_NUM values are really stored. This is set
210 up by find_blocks and used there and in life_analysis. It can be used
211 later, but only to look up an insn that is the head or tail of some
212 block. life_analysis and the stack register conversion process can
213 add insns within a block. */
214 static int *block_number;
216 /* We use this array to cache info about insns, because otherwise we
217 spend too much time in stack_regs_mentioned_p.
219 Indexed by insn UIDs. A value of zero is uninitialized, one indicates
220 the insn uses stack registers, two indicates the insn does not use
222 static varray_type stack_regs_mentioned_data;
224 /* This is the register file for all register after conversion */
226 FP_mode_reg[LAST_STACK_REG+1-FIRST_STACK_REG][(int) MAX_MACHINE_MODE];
228 #define FP_MODE_REG(regno,mode) \
229 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int)(mode)])
231 /* Get the basic block number of an insn. See note at block_number
232 definition are validity of this information. */
234 static int BLOCK_NUM PROTO((rtx));
243 int tmp = INSN_UID (insn);
246 tmp = block_number[tmp];
252 extern rtx forced_labels;
254 /* Forward declarations */
256 static void mark_regs_pat PROTO((rtx, HARD_REG_SET *));
257 static void straighten_stack PROTO((rtx, stack));
258 static void pop_stack PROTO((stack, int));
259 static void record_label_references PROTO((rtx, rtx));
260 static rtx *get_true_reg PROTO((rtx *));
262 static void record_asm_reg_life PROTO((rtx, stack));
263 static void record_reg_life_pat PROTO((rtx, HARD_REG_SET *,
264 HARD_REG_SET *, int));
265 static int get_asm_operand_n_inputs PROTO((rtx));
266 static void record_reg_life PROTO((rtx, int, stack));
267 static void find_blocks PROTO((rtx));
268 static rtx stack_result PROTO((tree));
269 static void stack_reg_life_analysis PROTO((rtx, HARD_REG_SET *));
270 static void replace_reg PROTO((rtx *, int));
271 static void remove_regno_note PROTO((rtx, enum reg_note, int));
272 static int get_hard_regnum PROTO((stack, rtx));
273 static void delete_insn_for_stacker PROTO((rtx));
274 static rtx emit_pop_insn PROTO((rtx, stack, rtx, rtx (*) ()));
275 static void emit_swap_insn PROTO((rtx, stack, rtx));
276 static void move_for_stack_reg PROTO((rtx, stack, rtx));
277 static void swap_rtx_condition PROTO((rtx));
278 static void compare_for_stack_reg PROTO((rtx, stack, rtx));
279 static void subst_stack_regs_pat PROTO((rtx, stack, rtx));
280 static void subst_asm_stack_regs PROTO((rtx, stack));
281 static void subst_stack_regs PROTO((rtx, stack));
282 static void change_stack PROTO((rtx, stack, stack, rtx (*) ()));
284 static void goto_block_pat PROTO((rtx, stack, rtx));
285 static void convert_regs PROTO((void));
286 static void print_blocks PROTO((FILE *, rtx, rtx));
287 static void dump_stack_info PROTO((FILE *));
288 static int check_stack_regs_mentioned PROTO((rtx insn));
290 /* Initialize stack_regs_mentioned_data for INSN (growing the virtual array
291 if needed. Return nonzero if INSN mentions stacked registers. */
294 check_stack_regs_mentioned (insn)
297 unsigned int uid = INSN_UID (insn);
298 if (uid >= VARRAY_SIZE (stack_regs_mentioned_data))
299 /* Allocate some extra size to avoid too many reallocs, but
300 do not grow exponentially. */
301 VARRAY_GROW (stack_regs_mentioned_data, uid + uid / 20);
302 if (stack_regs_mentioned_p (PATTERN (insn)))
304 VARRAY_CHAR (stack_regs_mentioned_data, uid) = 1;
308 VARRAY_CHAR (stack_regs_mentioned_data, uid) = 2;
312 /* Return nonzero if INSN mentions stacked registers, else return
316 stack_regs_mentioned (insn)
320 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
322 uid = INSN_UID (insn);
323 if (uid >= VARRAY_SIZE (stack_regs_mentioned_data)
324 || ! VARRAY_CHAR (stack_regs_mentioned_data, uid))
325 return (check_stack_regs_mentioned (insn));
326 return VARRAY_CHAR (stack_regs_mentioned_data, uid) == 1;
330 /* Mark all registers needed for this pattern. */
333 mark_regs_pat (pat, set)
337 enum machine_mode mode;
341 if (GET_CODE (pat) == SUBREG)
343 mode = GET_MODE (pat);
344 regno = SUBREG_WORD (pat);
345 regno += REGNO (SUBREG_REG (pat));
348 regno = REGNO (pat), mode = GET_MODE (pat);
350 for (count = HARD_REGNO_NREGS (regno, mode);
351 count; count--, regno++)
352 SET_HARD_REG_BIT (*set, regno);
355 /* Reorganise the stack into ascending numbers,
359 straighten_stack (insn, regstack)
363 struct stack_def temp_stack;
366 /* If there is only a single register on the stack, then the stack is
367 already in increasing order and no reorganization is needed.
369 Similarly if the stack is empty. */
370 if (regstack->top <= 0)
373 temp_stack.reg_set = regstack->reg_set;
375 for (top = temp_stack.top = regstack->top; top >= 0; top--)
376 temp_stack.reg[top] = FIRST_STACK_REG + temp_stack.top - top;
378 change_stack (insn, regstack, &temp_stack, emit_insn_after);
381 /* Pop a register from the stack */
384 pop_stack (regstack, regno)
388 int top = regstack->top;
390 CLEAR_HARD_REG_BIT (regstack->reg_set, regno);
392 /* If regno was not at the top of stack then adjust stack */
393 if (regstack->reg [top] != regno)
396 for (i = regstack->top; i >= 0; i--)
397 if (regstack->reg [i] == regno)
400 for (j = i; j < top; j++)
401 regstack->reg [j] = regstack->reg [j + 1];
407 /* Return non-zero if any stack register is mentioned somewhere within PAT. */
410 stack_regs_mentioned_p (pat)
416 if (STACK_REG_P (pat))
419 fmt = GET_RTX_FORMAT (GET_CODE (pat));
420 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
426 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
427 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
430 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
437 /* Convert register usage from "flat" register file usage to a "stack
438 register file. FIRST is the first insn in the function, FILE is the
441 First compute the beginning and end of each basic block. Do a
442 register life analysis on the stack registers, recording the result
443 for the head and tail of each basic block. The convert each insn one
444 by one. Run a last jump_optimize() pass, if optimizing, to eliminate
445 any cross-jumping created when the converter inserts pop insns.*/
448 reg_to_stack (first, file)
454 int stack_reg_seen = 0;
455 enum machine_mode mode;
456 HARD_REG_SET stackentry;
458 max_uid = get_max_uid ();
459 VARRAY_CHAR_INIT (stack_regs_mentioned_data, max_uid + 1,
460 "stack_regs_mentioned cache");
462 CLEAR_HARD_REG_SET (stackentry);
465 static int initialised;
469 initialised = 1; /* This array can not have been previously
470 initialised, because the rtx's are
471 thrown away between compilations of
474 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
476 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
477 mode = GET_MODE_WIDER_MODE (mode))
478 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
479 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT); mode != VOIDmode;
480 mode = GET_MODE_WIDER_MODE (mode))
481 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
486 /* Count the basic blocks. Also find maximum insn uid. */
488 register RTX_CODE prev_code = BARRIER;
489 register RTX_CODE code;
490 register int before_function_beg = 1;
494 for (insn = first; insn; insn = NEXT_INSN (insn))
496 /* Note that this loop must select the same block boundaries
497 as code in find_blocks. Also note that this code is not the
498 same as that used in flow.c. */
500 if (INSN_UID (insn) > max_uid)
501 max_uid = INSN_UID (insn);
503 code = GET_CODE (insn);
505 if (code == CODE_LABEL
506 || (prev_code != INSN
507 && prev_code != CALL_INSN
508 && prev_code != CODE_LABEL
509 && GET_RTX_CLASS (code) == 'i'))
512 if (code == NOTE && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
513 before_function_beg = 0;
515 /* Remember whether or not this insn mentions an FP regs.
516 Check JUMP_INSNs too, in case someone creates a funny PARALLEL. */
518 if (GET_RTX_CLASS (code) == 'i'
519 && stack_regs_mentioned_p (PATTERN (insn)))
522 VARRAY_CHAR (stack_regs_mentioned_data, INSN_UID (insn)) = 1;
524 /* Note any register passing parameters. */
526 if (before_function_beg && code == INSN
527 && GET_CODE (PATTERN (insn)) == USE)
528 record_reg_life_pat (PATTERN (insn), (HARD_REG_SET *) 0,
532 VARRAY_CHAR (stack_regs_mentioned_data, INSN_UID (insn)) = 2;
534 if (code == CODE_LABEL)
535 LABEL_REFS (insn) = insn; /* delete old chain */
542 /* If no stack register reference exists in this insn, there isn't
543 anything to convert. */
545 if (! stack_reg_seen)
547 VARRAY_FREE (stack_regs_mentioned_data);
551 /* If there are stack registers, there must be at least one block. */
556 /* Allocate some tables that last till end of compiling this function
557 and some needed only in find_blocks and life_analysis. */
559 block_begin = (rtx *) alloca (blocks * sizeof (rtx));
560 block_end = (rtx *) alloca (blocks * sizeof (rtx));
561 block_drops_in = (char *) alloca (blocks);
563 block_stack_in = (stack) alloca (blocks * sizeof (struct stack_def));
564 block_out_reg_set = (HARD_REG_SET *) alloca (blocks * sizeof (HARD_REG_SET));
565 bzero ((char *) block_stack_in, blocks * sizeof (struct stack_def));
566 bzero ((char *) block_out_reg_set, blocks * sizeof (HARD_REG_SET));
568 block_number = (int *) alloca ((max_uid + 1) * sizeof (int));
569 memset (block_number, -1, (max_uid + 1) * sizeof (int));
572 stack_reg_life_analysis (first, &stackentry);
574 /* Dump the life analysis debug information before jump
575 optimization, as that will destroy the LABEL_REFS we keep the
579 dump_stack_info (file);
584 jump_optimize (first, 2, 0, 0);
586 VARRAY_FREE (stack_regs_mentioned_data);
589 /* Check PAT, which is in INSN, for LABEL_REFs. Add INSN to the
590 label's chain of references, and note which insn contains each
594 record_label_references (insn, pat)
597 register enum rtx_code code = GET_CODE (pat);
601 if (code == LABEL_REF)
603 register rtx label = XEXP (pat, 0);
606 if (GET_CODE (label) != CODE_LABEL)
609 /* If this is an undefined label, LABEL_REFS (label) contains
611 if (INSN_UID (label) == 0)
614 /* Don't make a duplicate in the code_label's chain. */
616 for (ref = LABEL_REFS (label);
618 ref = LABEL_NEXTREF (ref))
619 if (CONTAINING_INSN (ref) == insn)
622 CONTAINING_INSN (pat) = insn;
623 LABEL_NEXTREF (pat) = LABEL_REFS (label);
624 LABEL_REFS (label) = pat;
629 fmt = GET_RTX_FORMAT (code);
630 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
633 record_label_references (insn, XEXP (pat, i));
637 for (j = 0; j < XVECLEN (pat, i); j++)
638 record_label_references (insn, XVECEXP (pat, i, j));
643 /* Return a pointer to the REG expression within PAT. If PAT is not a
644 REG, possible enclosed by a conversion rtx, return the inner part of
645 PAT that stopped the search. */
652 switch (GET_CODE (*pat))
655 /* eliminate FP subregister accesses in favour of the
656 actual FP register in use. */
659 if (FP_REG_P (subreg = SUBREG_REG (*pat)))
661 *pat = FP_MODE_REG (REGNO (subreg) + SUBREG_WORD (*pat),
670 pat = & XEXP (*pat, 0);
674 /* Record the life info of each stack reg in INSN, updating REGSTACK.
675 N_INPUTS is the number of inputs; N_OUTPUTS the outputs.
676 OPERANDS is an array of all operands for the insn, and is assumed to
677 contain all output operands, then all inputs operands.
679 There are many rules that an asm statement for stack-like regs must
680 follow. Those rules are explained at the top of this file: the rule
681 numbers below refer to that explanation. */
684 record_asm_reg_life (insn, regstack)
690 int malformed_asm = 0;
691 rtx body = PATTERN (insn);
693 int reg_used_as_output[FIRST_PSEUDO_REGISTER];
694 int implicitly_dies[FIRST_PSEUDO_REGISTER];
698 int n_inputs, n_outputs;
700 /* Find out what the constraints require. If no constraint
701 alternative matches, this asm is malformed. */
703 constrain_operands (1);
704 alt = which_alternative;
706 preprocess_constraints ();
708 n_inputs = get_asm_operand_n_inputs (body);
709 n_outputs = recog_n_operands - n_inputs;
714 /* Avoid further trouble with this insn. */
715 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
716 VARRAY_CHAR (stack_regs_mentioned_data, INSN_UID (insn)) = 2;
720 /* Strip SUBREGs here to make the following code simpler. */
721 for (i = 0; i < recog_n_operands; i++)
722 if (GET_CODE (recog_operand[i]) == SUBREG
723 && GET_CODE (SUBREG_REG (recog_operand[i])) == REG)
724 recog_operand[i] = SUBREG_REG (recog_operand[i]);
726 /* Set up CLOBBER_REG. */
730 if (GET_CODE (body) == PARALLEL)
732 clobber_reg = (rtx *) alloca (XVECLEN (body, 0) * sizeof (rtx));
734 for (i = 0; i < XVECLEN (body, 0); i++)
735 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
737 rtx clobber = XVECEXP (body, 0, i);
738 rtx reg = XEXP (clobber, 0);
740 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
741 reg = SUBREG_REG (reg);
743 if (STACK_REG_P (reg))
745 clobber_reg[n_clobbers] = reg;
751 /* Enforce rule #4: Output operands must specifically indicate which
752 reg an output appears in after an asm. "=f" is not allowed: the
753 operand constraints must select a class with a single reg.
755 Also enforce rule #5: Output operands must start at the top of
756 the reg-stack: output operands may not "skip" a reg. */
758 bzero ((char *) reg_used_as_output, sizeof (reg_used_as_output));
759 for (i = 0; i < n_outputs; i++)
760 if (STACK_REG_P (recog_operand[i]))
762 if (reg_class_size[(int) recog_op_alt[i][alt].class] != 1)
764 error_for_asm (insn, "Output constraint %d must specify a single register", i);
768 reg_used_as_output[REGNO (recog_operand[i])] = 1;
772 /* Search for first non-popped reg. */
773 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
774 if (! reg_used_as_output[i])
777 /* If there are any other popped regs, that's an error. */
778 for (; i < LAST_STACK_REG + 1; i++)
779 if (reg_used_as_output[i])
782 if (i != LAST_STACK_REG + 1)
784 error_for_asm (insn, "Output regs must be grouped at top of stack");
788 /* Enforce rule #2: All implicitly popped input regs must be closer
789 to the top of the reg-stack than any input that is not implicitly
792 bzero ((char *) implicitly_dies, sizeof (implicitly_dies));
793 for (i = n_outputs; i < n_outputs + n_inputs; i++)
794 if (STACK_REG_P (recog_operand[i]))
796 /* An input reg is implicitly popped if it is tied to an
797 output, or if there is a CLOBBER for it. */
800 for (j = 0; j < n_clobbers; j++)
801 if (operands_match_p (clobber_reg[j], recog_operand[i]))
804 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
805 implicitly_dies[REGNO (recog_operand[i])] = 1;
808 /* Search for first non-popped reg. */
809 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
810 if (! implicitly_dies[i])
813 /* If there are any other popped regs, that's an error. */
814 for (; i < LAST_STACK_REG + 1; i++)
815 if (implicitly_dies[i])
818 if (i != LAST_STACK_REG + 1)
821 "Implicitly popped regs must be grouped at top of stack");
825 /* Enfore rule #3: If any input operand uses the "f" constraint, all
826 output constraints must use the "&" earlyclobber.
828 ??? Detect this more deterministically by having constraint_asm_operands
829 record any earlyclobber. */
831 for (i = n_outputs; i < n_outputs + n_inputs; i++)
832 if (recog_op_alt[i][alt].matches == -1)
836 for (j = 0; j < n_outputs; j++)
837 if (operands_match_p (recog_operand[j], recog_operand[i]))
840 "Output operand %d must use `&' constraint", j);
847 /* Avoid further trouble with this insn. */
848 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
849 VARRAY_CHAR (stack_regs_mentioned_data, INSN_UID (insn)) = 2;
853 /* Process all outputs */
854 for (i = 0; i < n_outputs; i++)
856 rtx op = recog_operand[i];
858 if (! STACK_REG_P (op))
860 if (stack_regs_mentioned_p (op))
866 /* Each destination is dead before this insn. If the
867 destination is not used after this insn, record this with
870 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (op)))
871 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_UNUSED, op,
874 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (op));
877 /* Process all inputs */
878 for (i = n_outputs; i < n_outputs + n_inputs; i++)
880 rtx op = recog_operand[i];
881 if (! STACK_REG_P (op))
883 if (stack_regs_mentioned_p (op))
889 /* If an input is dead after the insn, record a death note.
890 But don't record a death note if there is already a death note,
891 or if the input is also an output. */
893 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (op))
894 && recog_op_alt[i][alt].matches == -1
895 && find_regno_note (insn, REG_DEAD, REGNO (op)) == NULL_RTX)
896 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD, op, REG_NOTES (insn));
898 SET_HARD_REG_BIT (regstack->reg_set, REGNO (op));
902 /* Scan PAT, which is part of INSN, and record registers appearing in
903 a SET_DEST in DEST, and other registers in SRC.
905 This function does not know about SET_DESTs that are both input and
906 output (such as ZERO_EXTRACT) - this cannot happen on a 387. */
909 record_reg_life_pat (pat, src, dest, douse)
911 HARD_REG_SET *src, *dest;
917 if (STACK_REG_P (pat)
918 || (GET_CODE (pat) == SUBREG && STACK_REG_P (SUBREG_REG (pat))))
921 mark_regs_pat (pat, src);
924 mark_regs_pat (pat, dest);
929 if (GET_CODE (pat) == SET)
931 record_reg_life_pat (XEXP (pat, 0), NULL_PTR, dest, 0);
932 record_reg_life_pat (XEXP (pat, 1), src, NULL_PTR, 0);
936 /* We don't need to consider either of these cases. */
937 if ((GET_CODE (pat) == USE && !douse) || GET_CODE (pat) == CLOBBER)
940 fmt = GET_RTX_FORMAT (GET_CODE (pat));
941 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
947 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
948 record_reg_life_pat (XVECEXP (pat, i, j), src, dest, 0);
950 else if (fmt[i] == 'e')
951 record_reg_life_pat (XEXP (pat, i), src, dest, 0);
955 /* Calculate the number of inputs and outputs in BODY, an
956 asm_operands. N_OPERANDS is the total number of operands, and
957 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
961 get_asm_operand_n_inputs (body)
964 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
965 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body));
967 else if (GET_CODE (body) == ASM_OPERANDS)
968 return ASM_OPERANDS_INPUT_LENGTH (body);
970 else if (GET_CODE (body) == PARALLEL
971 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
972 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (XVECEXP (body, 0, 0)));
974 else if (GET_CODE (body) == PARALLEL
975 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
976 return ASM_OPERANDS_INPUT_LENGTH (XVECEXP (body, 0, 0));
981 /* Scan INSN, which is in BLOCK, and record the life & death of stack
982 registers in REGSTACK. This function is called to process insns from
983 the last insn in a block to the first. The actual scanning is done in
986 If a register is live after a CALL_INSN, but is not a value return
987 register for that CALL_INSN, then code is emitted to initialize that
988 register. The block_end[] data is kept accurate.
990 Existing death and unset notes for stack registers are deleted
991 before processing the insn. */
994 record_reg_life (insn, block, regstack)
999 rtx note, *note_link;
1002 if ((GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
1003 || INSN_DELETED_P (insn))
1006 /* Strip death notes for stack regs from this insn */
1008 note_link = ®_NOTES(insn);
1009 for (note = *note_link; note; note = XEXP (note, 1))
1010 if (STACK_REG_P (XEXP (note, 0))
1011 && (REG_NOTE_KIND (note) == REG_DEAD
1012 || REG_NOTE_KIND (note) == REG_UNUSED))
1013 *note_link = XEXP (note, 1);
1015 note_link = &XEXP (note, 1);
1017 /* Process all patterns in the insn. */
1019 n_operands = asm_noperands (PATTERN (insn));
1020 if (n_operands >= 0)
1022 record_asm_reg_life (insn, regstack);
1027 HARD_REG_SET src, dest;
1030 CLEAR_HARD_REG_SET (src);
1031 CLEAR_HARD_REG_SET (dest);
1033 if (GET_CODE (insn) == CALL_INSN)
1034 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1036 note = XEXP (note, 1))
1037 if (GET_CODE (XEXP (note, 0)) == USE)
1038 record_reg_life_pat (SET_DEST (XEXP (note, 0)), &src, NULL_PTR, 0);
1040 record_reg_life_pat (PATTERN (insn), &src, &dest, 0);
1041 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
1042 if (! TEST_HARD_REG_BIT (regstack->reg_set, regno))
1044 if (TEST_HARD_REG_BIT (src, regno)
1045 && ! TEST_HARD_REG_BIT (dest, regno))
1046 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD,
1047 FP_MODE_REG (regno, DFmode),
1049 else if (TEST_HARD_REG_BIT (dest, regno))
1050 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_UNUSED,
1051 FP_MODE_REG (regno, DFmode),
1055 if (GET_CODE (insn) == CALL_INSN)
1059 /* There might be a reg that is live after a function call.
1060 Initialize it to zero so that the program does not crash. See
1061 comment towards the end of stack_reg_life_analysis(). */
1063 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
1064 if (! TEST_HARD_REG_BIT (dest, reg)
1065 && TEST_HARD_REG_BIT (regstack->reg_set, reg))
1069 /* The insn will use virtual register numbers, and so
1070 convert_regs is expected to process these. But BLOCK_NUM
1071 cannot be used on these insns, because they do not appear in
1074 pat = gen_rtx_SET (VOIDmode, FP_MODE_REG (reg, DFmode),
1075 CONST0_RTX (DFmode));
1076 init = emit_insn_after (pat, insn);
1078 CLEAR_HARD_REG_BIT (regstack->reg_set, reg);
1080 /* If the CALL_INSN was the end of a block, move the
1081 block_end to point to the new insn. */
1083 if (block_end[block] == insn)
1084 block_end[block] = init;
1087 /* Some regs do not survive a CALL */
1088 AND_COMPL_HARD_REG_SET (regstack->reg_set, call_used_reg_set);
1091 AND_COMPL_HARD_REG_SET (regstack->reg_set, dest);
1092 IOR_HARD_REG_SET (regstack->reg_set, src);
1096 /* Find all basic blocks of the function, which starts with FIRST.
1097 For each JUMP_INSN, build the chain of LABEL_REFS on each CODE_LABEL. */
1105 register RTX_CODE prev_code = BARRIER;
1106 register RTX_CODE code;
1107 rtx label_value_list = 0;
1109 /* Record where all the blocks start and end.
1110 Record which basic blocks control can drop in to. */
1113 for (insn = first; insn; insn = NEXT_INSN (insn))
1115 /* Note that this loop must select the same block boundaries
1116 as code in reg_to_stack, but that these are not the same
1117 as those selected in flow.c. */
1119 code = GET_CODE (insn);
1121 if (code == CODE_LABEL
1122 || (prev_code != INSN
1123 && prev_code != CALL_INSN
1124 && prev_code != CODE_LABEL
1125 && GET_RTX_CLASS (code) == 'i'))
1127 block_begin[++block] = insn;
1128 block_end[block] = insn;
1129 block_drops_in[block] = prev_code != BARRIER;
1131 else if (GET_RTX_CLASS (code) == 'i')
1132 block_end[block] = insn;
1134 if (GET_RTX_CLASS (code) == 'i')
1138 /* Make a list of all labels referred to other than by jumps. */
1139 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1140 if (REG_NOTE_KIND (note) == REG_LABEL)
1141 label_value_list = gen_rtx_EXPR_LIST (VOIDmode, XEXP (note, 0),
1145 block_number[INSN_UID (insn)] = block;
1151 if (block + 1 != blocks)
1154 /* generate all label references to the corresponding jump insn */
1155 for (block = 0; block < blocks; block++)
1157 insn = block_end[block];
1159 if (GET_CODE (insn) == JUMP_INSN)
1161 rtx pat = PATTERN (insn);
1164 if (computed_jump_p (insn))
1166 for (x = label_value_list; x; x = XEXP (x, 1))
1167 record_label_references (insn,
1168 gen_rtx_LABEL_REF (VOIDmode,
1171 for (x = forced_labels; x; x = XEXP (x, 1))
1172 record_label_references (insn,
1173 gen_rtx_LABEL_REF (VOIDmode,
1177 record_label_references (insn, pat);
1182 /* If current function returns its result in an fp stack register,
1183 return the REG. Otherwise, return 0. */
1191 /* If the value is supposed to be returned in memory, then clearly
1192 it is not returned in a stack register. */
1193 if (aggregate_value_p (DECL_RESULT (decl)))
1196 result = DECL_RTL (DECL_RESULT (decl));
1197 /* ?!? What is this code supposed to do? Can this code actually
1198 trigger if we kick out aggregates above? */
1200 && ! (GET_CODE (result) == REG
1201 && REGNO (result) < FIRST_PSEUDO_REGISTER))
1203 #ifdef FUNCTION_OUTGOING_VALUE
1205 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
1207 result = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
1211 return result != 0 && STACK_REG_P (result) ? result : 0;
1214 /* Determine the which registers are live at the start of each basic
1215 block of the function whose first insn is FIRST.
1217 First, if the function returns a real_type, mark the function
1218 return type as live at each return point, as the RTL may not give any
1219 hint that the register is live.
1221 Then, start with the last block and work back to the first block.
1222 Similarly, work backwards within each block, insn by insn, recording
1223 which regs are dead and which are used (and therefore live) in the
1224 hard reg set of block_stack_in[].
1226 After processing each basic block, if there is a label at the start
1227 of the block, propagate the live registers to all jumps to this block.
1229 As a special case, if there are regs live in this block, that are
1230 not live in a block containing a jump to this label, and the block
1231 containing the jump has already been processed, we must propagate this
1232 block's entry register life back to the block containing the jump, and
1233 restart life analysis from there.
1235 In the worst case, this function may traverse the insns
1236 REG_STACK_SIZE times. This is necessary, since a jump towards the end
1237 of the insns may not know that a reg is live at a target that is early
1238 in the insns. So we back up and start over with the new reg live.
1240 If there are registers that are live at the start of the function,
1241 insns are emitted to initialize these registers. Something similar is
1242 done after CALL_INSNs in record_reg_life. */
1245 stack_reg_life_analysis (first, stackentry)
1247 HARD_REG_SET *stackentry;
1250 struct stack_def regstack;
1255 if ((retvalue = stack_result (current_function_decl)))
1257 /* Find all RETURN insns and mark them. */
1259 for (block = blocks - 1; --block >= 0;)
1260 if (GET_CODE (block_end[block]) == JUMP_INSN
1261 && returnjump_p (block_end[block]))
1262 mark_regs_pat (retvalue, block_out_reg_set+block);
1264 /* Mark off the end of last block if we "fall off" the end of the
1265 function into the epilogue. */
1267 if (GET_CODE (block_end[blocks-1]) != JUMP_INSN
1268 || returnjump_p (block_end[blocks-1]))
1269 mark_regs_pat (retvalue, block_out_reg_set+blocks-1);
1273 /* now scan all blocks backward for stack register use */
1278 register rtx insn, prev;
1280 /* current register status at last instruction */
1282 COPY_HARD_REG_SET (regstack.reg_set, block_out_reg_set[block]);
1284 prev = block_end[block];
1288 prev = PREV_INSN (insn);
1290 /* If the insn is a CALL_INSN, we need to ensure that
1291 everything dies. But otherwise don't process unless there
1292 are some stack regs present. */
1294 if (stack_regs_mentioned (insn) || GET_CODE (insn) == CALL_INSN)
1295 record_reg_life (insn, block, ®stack);
1297 } while (insn != block_begin[block]);
1299 /* Set the state at the start of the block. Mark that no
1300 register mapping information known yet. */
1302 COPY_HARD_REG_SET (block_stack_in[block].reg_set, regstack.reg_set);
1303 block_stack_in[block].top = -2;
1305 /* If there is a label, propagate our register life to all jumps
1308 if (GET_CODE (insn) == CODE_LABEL)
1311 int must_restart = 0;
1313 for (label = LABEL_REFS (insn); label != insn;
1314 label = LABEL_NEXTREF (label))
1316 int jump_block = BLOCK_NUM (CONTAINING_INSN (label));
1318 if (jump_block < block)
1319 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
1320 block_stack_in[block].reg_set);
1323 /* The block containing the jump has already been
1324 processed. If there are registers that were not known
1325 to be live then, but are live now, we must back up
1326 and restart life analysis from that point with the new
1327 life information. */
1329 GO_IF_HARD_REG_SUBSET (block_stack_in[block].reg_set,
1330 block_out_reg_set[jump_block],
1333 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
1334 block_stack_in[block].reg_set);
1348 if (block_drops_in[block])
1349 IOR_HARD_REG_SET (block_out_reg_set[block-1],
1350 block_stack_in[block].reg_set);
1355 /* If any reg is live at the start of the first block of a
1356 function, then we must guarantee that the reg holds some value by
1357 generating our own "load" of that register. Otherwise a 387 would
1358 fault trying to access an empty register. */
1360 /* Load zero into each live register. The fact that a register
1361 appears live at the function start necessarily implies an error
1362 in the user program: it means that (unless the offending code is *never*
1363 executed) this program is using uninitialised floating point
1364 variables. In order to keep broken code like this happy, we initialise
1365 those variables with zero.
1367 Note that we are inserting virtual register references here:
1368 these insns must be processed by convert_regs later. Also, these
1369 insns will not be in block_number, so BLOCK_NUM() will fail for them. */
1371 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
1372 if (TEST_HARD_REG_BIT (block_stack_in[0].reg_set, reg)
1373 && ! TEST_HARD_REG_BIT (*stackentry, reg))
1377 init_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG(reg, DFmode),
1378 CONST0_RTX (DFmode));
1379 block_begin[0] = emit_insn_after (init_rtx, first);
1381 CLEAR_HARD_REG_BIT (block_stack_in[0].reg_set, reg);
1385 /*****************************************************************************
1386 This section deals with stack register substitution, and forms the second
1388 *****************************************************************************/
1390 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
1391 the desired hard REGNO. */
1394 replace_reg (reg, regno)
1398 if (regno < FIRST_STACK_REG || regno > LAST_STACK_REG
1399 || ! STACK_REG_P (*reg))
1402 switch (GET_MODE_CLASS (GET_MODE (*reg)))
1406 case MODE_COMPLEX_FLOAT:;
1409 *reg = FP_MODE_REG (regno, GET_MODE (*reg));
1412 /* Remove a note of type NOTE, which must be found, for register
1413 number REGNO from INSN. Remove only one such note. */
1416 remove_regno_note (insn, note, regno)
1421 register rtx *note_link, this;
1423 note_link = ®_NOTES(insn);
1424 for (this = *note_link; this; this = XEXP (this, 1))
1425 if (REG_NOTE_KIND (this) == note
1426 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno)
1428 *note_link = XEXP (this, 1);
1432 note_link = &XEXP (this, 1);
1437 /* Find the hard register number of virtual register REG in REGSTACK.
1438 The hard register number is relative to the top of the stack. -1 is
1439 returned if the register is not found. */
1442 get_hard_regnum (regstack, reg)
1448 if (! STACK_REG_P (reg))
1451 for (i = regstack->top; i >= 0; i--)
1452 if (regstack->reg[i] == REGNO (reg))
1455 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
1458 /* Delete INSN from the RTL. Mark the insn, but don't remove it from
1459 the chain of insns. Doing so could confuse block_begin and block_end
1460 if this were the only insn in the block. */
1463 delete_insn_for_stacker (insn)
1468 /* Ensure that the side effects were clobbers when deleting a PARALLEL. */
1469 if (GET_CODE (PATTERN (insn)) == PARALLEL)
1470 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
1471 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) != CLOBBER)
1474 PUT_CODE (insn, NOTE);
1475 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1476 NOTE_SOURCE_FILE (insn) = 0;
1479 /* Emit an insn to pop virtual register REG before or after INSN.
1480 REGSTACK is the stack state after INSN and is updated to reflect this
1481 pop. WHEN is either emit_insn_before, emit_insn_after or NULL.
1482 in case WHEN is NULL we don't really emit the insn, just modify stack
1483 information. Caller is expected to emit insn himself.
1485 A pop insn is represented as a SET whose destination is the register to
1486 be popped and source is the top of stack. A death note for the top of stack
1487 cases the movdf pattern to pop. */
1490 emit_pop_insn (insn, regstack, reg, when)
1496 rtx pop_insn, pop_rtx;
1499 hard_regno = get_hard_regnum (regstack, reg);
1501 if (hard_regno < FIRST_STACK_REG)
1506 pop_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG (hard_regno, DFmode),
1507 FP_MODE_REG (FIRST_STACK_REG, DFmode));
1509 pop_insn = (*when) (pop_rtx, insn);
1511 REG_NOTES (pop_insn) = gen_rtx_EXPR_LIST (REG_DEAD,
1512 FP_MODE_REG (FIRST_STACK_REG,
1514 REG_NOTES (pop_insn));
1517 regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
1518 = regstack->reg[regstack->top];
1520 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
1525 /* Emit an insn before or after INSN to swap virtual register REG with the
1526 top of stack. WHEN should be `emit_insn_before' or `emit_insn_before'
1527 REGSTACK is the stack state before the swap, and is updated to reflect
1528 the swap. A swap insn is represented as a PARALLEL of two patterns:
1529 each pattern moves one reg to the other.
1531 If REG is already at the top of the stack, no insn is emitted. */
1534 emit_swap_insn (insn, regstack, reg)
1541 rtx swap_rtx, swap_insn;
1542 int tmp, other_reg; /* swap regno temps */
1543 rtx i1; /* the stack-reg insn prior to INSN */
1544 rtx i1set = NULL_RTX; /* the SET rtx within I1 */
1546 hard_regno = get_hard_regnum (regstack, reg);
1548 if (hard_regno < FIRST_STACK_REG)
1550 if (hard_regno == FIRST_STACK_REG)
1553 other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
1555 tmp = regstack->reg[other_reg];
1556 regstack->reg[other_reg] = regstack->reg[regstack->top];
1557 regstack->reg[regstack->top] = tmp;
1559 /* Find the previous insn involving stack regs, but don't go past
1560 any labels, calls or jumps. */
1561 i1 = prev_nonnote_insn (insn);
1562 while (i1 && GET_CODE (i1) == INSN && !stack_regs_mentioned (i1))
1563 i1 = prev_nonnote_insn (i1);
1566 i1set = single_set (i1);
1570 rtx i1src = *get_true_reg (&SET_SRC (i1set));
1571 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
1573 /* If the previous register stack push was from the reg we are to
1574 swap with, omit the swap. */
1576 if (GET_CODE (i1dest) == REG && REGNO (i1dest) == FIRST_STACK_REG
1577 && GET_CODE (i1src) == REG && REGNO (i1src) == hard_regno - 1
1578 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
1581 /* If the previous insn wrote to the reg we are to swap with,
1584 if (GET_CODE (i1dest) == REG && REGNO (i1dest) == hard_regno
1585 && GET_CODE (i1src) == REG && REGNO (i1src) == FIRST_STACK_REG
1586 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
1590 if (GET_RTX_CLASS (GET_CODE (i1)) == 'i' && sets_cc0_p (PATTERN (i1)))
1592 i1 = next_nonnote_insn (i1);
1597 swap_rtx = gen_swapdf (FP_MODE_REG (hard_regno, DFmode),
1598 FP_MODE_REG (FIRST_STACK_REG, DFmode));
1599 swap_insn = emit_insn_after (swap_rtx, i1);
1602 /* Handle a move to or from a stack register in PAT, which is in INSN.
1603 REGSTACK is the current stack. */
1606 move_for_stack_reg (insn, regstack, pat)
1611 rtx *psrc = get_true_reg (&SET_SRC (pat));
1612 rtx *pdest = get_true_reg (&SET_DEST (pat));
1616 src = *psrc; dest = *pdest;
1618 if (STACK_REG_P (src) && STACK_REG_P (dest))
1620 /* Write from one stack reg to another. If SRC dies here, then
1621 just change the register mapping and delete the insn. */
1623 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1628 /* If this is a no-op move, there must not be a REG_DEAD note. */
1629 if (REGNO (src) == REGNO (dest))
1632 for (i = regstack->top; i >= 0; i--)
1633 if (regstack->reg[i] == REGNO (src))
1636 /* The source must be live, and the dest must be dead. */
1637 if (i < 0 || get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1640 /* It is possible that the dest is unused after this insn.
1641 If so, just pop the src. */
1643 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1645 emit_pop_insn (insn, regstack, src, emit_insn_after);
1647 delete_insn_for_stacker (insn);
1651 regstack->reg[i] = REGNO (dest);
1653 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1654 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1656 delete_insn_for_stacker (insn);
1661 /* The source reg does not die. */
1663 /* If this appears to be a no-op move, delete it, or else it
1664 will confuse the machine description output patterns. But if
1665 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1666 for REG_UNUSED will not work for deleted insns. */
1668 if (REGNO (src) == REGNO (dest))
1670 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1671 emit_pop_insn (insn, regstack, dest, emit_insn_after);
1673 delete_insn_for_stacker (insn);
1677 /* The destination ought to be dead */
1678 if (get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1681 replace_reg (psrc, get_hard_regnum (regstack, src));
1683 regstack->reg[++regstack->top] = REGNO (dest);
1684 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1685 replace_reg (pdest, FIRST_STACK_REG);
1687 else if (STACK_REG_P (src))
1689 /* Save from a stack reg to MEM, or possibly integer reg. Since
1690 only top of stack may be saved, emit an exchange first if
1693 emit_swap_insn (insn, regstack, src);
1695 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1698 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1700 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1702 else if (GET_MODE (src) == XFmode && regstack->top < REG_STACK_SIZE - 1)
1704 /* A 387 cannot write an XFmode value to a MEM without
1705 clobbering the source reg. The output code can handle
1706 this by reading back the value from the MEM.
1707 But it is more efficient to use a temp register if one is
1708 available. Push the source value here if the register
1709 stack is not full, and then write the value to memory via
1711 rtx push_rtx, push_insn;
1712 rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, XFmode);
1714 push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
1715 push_insn = emit_insn_before (push_rtx, insn);
1716 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD, top_stack_reg,
1720 replace_reg (psrc, FIRST_STACK_REG);
1722 else if (STACK_REG_P (dest))
1724 /* Load from MEM, or possibly integer REG or constant, into the
1725 stack regs. The actual target is always the top of the
1726 stack. The stack mapping is changed to reflect that DEST is
1727 now at top of stack. */
1729 /* The destination ought to be dead */
1730 if (get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1733 if (regstack->top >= REG_STACK_SIZE)
1736 regstack->reg[++regstack->top] = REGNO (dest);
1737 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1738 replace_reg (pdest, FIRST_STACK_REG);
1745 swap_rtx_condition (pat)
1751 if (GET_RTX_CLASS (GET_CODE (pat)) == '<')
1753 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1757 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1758 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1764 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1765 swap_rtx_condition (XVECEXP (pat, i, j));
1767 else if (fmt[i] == 'e')
1768 swap_rtx_condition (XEXP (pat, i));
1772 /* Handle a comparison. Special care needs to be taken to avoid
1773 causing comparisons that a 387 cannot do correctly, such as EQ.
1775 Also, a fstp instruction may need to be emitted. The 387 does have an
1776 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1777 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1780 We can not handle this by emiting fpop instruction after compare, because
1781 it appears between cc0 setter and user. So we emit only
1782 REG_DEAD note and handle it as a special case in machine description.
1784 This code used trick with delay_slot filling to emit pop insn after
1785 comparsion but it didn't worked because it caused confusion with cc_status
1789 compare_for_stack_reg (insn, regstack, pat)
1795 rtx src1_note, src2_note;
1800 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1801 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
1802 cc0_user = next_cc0_user (insn);
1804 /* If the insn that uses cc0 is an FP-conditional move, then the destination
1805 must be the top of stack */
1806 if (GET_CODE (PATTERN (cc0_user)) == SET
1807 && SET_DEST (PATTERN (cc0_user)) != pc_rtx
1808 && GET_CODE (SET_SRC (PATTERN (cc0_user))) == IF_THEN_ELSE
1809 && (GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (cc0_user))))
1814 dest = get_true_reg (&SET_DEST (PATTERN (cc0_user)));
1817 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
1818 && REGNO (*dest) != regstack->reg[regstack->top])
1820 emit_swap_insn (insn, regstack, *dest);
1826 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1827 registers that die in this insn - move those to stack top first. */
1828 if (! STACK_REG_P (*src1)
1829 || (STACK_REG_P (*src2)
1830 && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
1834 temp = XEXP (SET_SRC (pat), 0);
1835 XEXP (SET_SRC (pat), 0) = XEXP (SET_SRC (pat), 1);
1836 XEXP (SET_SRC (pat), 1) = temp;
1838 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1839 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
1841 next = next_cc0_user (insn);
1842 if (next == NULL_RTX)
1845 swap_rtx_condition (PATTERN (next));
1846 INSN_CODE (next) = -1;
1847 INSN_CODE (insn) = -1;
1850 /* We will fix any death note later. */
1852 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1854 if (STACK_REG_P (*src2))
1855 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1857 src2_note = NULL_RTX;
1860 emit_swap_insn (insn, regstack, *src1);
1862 replace_reg (src1, FIRST_STACK_REG);
1864 if (STACK_REG_P (*src2))
1866 hard_regno = get_hard_regnum (regstack, *src2);
1867 replace_reg (src2, hard_regno);
1872 pop_stack (regstack, REGNO (XEXP (src1_note, 0)));
1873 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1876 /* If the second operand dies, handle that. But if the operands are
1877 the same stack register, don't bother, because only one death is
1878 needed, and it was just handled. */
1881 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
1882 && REGNO (*src1) == REGNO (*src2)))
1884 /* As a special case, two regs may die in this insn if src2 is
1885 next to top of stack and the top of stack also dies. Since
1886 we have already popped src1, "next to top of stack" is really
1887 at top (FIRST_STACK_REG) now. */
1889 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1892 pop_stack (regstack, REGNO (XEXP (src2_note, 0)));
1893 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1897 /* Pop of second operand is handled using special REG_DEAD note
1898 because we can't emit pop insn after cc0 setter. */
1900 emit_pop_insn (insn, regstack, XEXP (src2_note, 0), NULL);
1901 replace_reg (&XEXP (src2_note, 0), hard_regno);
1906 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1907 is the current register layout. */
1910 subst_stack_regs_pat (insn, regstack, pat)
1916 rtx *src1 = (rtx *) NULL_PTR, *src2;
1917 rtx src1_note, src2_note;
1919 if (GET_CODE (pat) != SET)
1922 dest = get_true_reg (&SET_DEST (pat));
1923 src = get_true_reg (&SET_SRC (pat));
1925 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1927 if (*dest != cc0_rtx
1928 && (STACK_REG_P (*src)
1929 || (STACK_REG_P (*dest)
1930 && (GET_CODE (*src) == REG || GET_CODE (*src) == MEM
1931 || GET_CODE (*src) == CONST_DOUBLE))))
1932 move_for_stack_reg (insn, regstack, pat);
1934 switch (GET_CODE (SET_SRC (pat)))
1937 compare_for_stack_reg (insn, regstack, pat);
1943 for (count = HARD_REGNO_NREGS (REGNO (*dest), GET_MODE (*dest));
1946 regstack->reg[++regstack->top] = REGNO (*dest) + count;
1947 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
1950 replace_reg (dest, FIRST_STACK_REG);
1954 /* This is a `tstM2' case. */
1955 if (*dest != cc0_rtx)
1962 case FLOAT_TRUNCATE:
1966 /* These insns only operate on the top of the stack. DEST might
1967 be cc0_rtx if we're processing a tstM pattern. Also, it's
1968 possible that the tstM case results in a REG_DEAD note on the
1972 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1974 emit_swap_insn (insn, regstack, *src1);
1976 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1978 if (STACK_REG_P (*dest))
1979 replace_reg (dest, FIRST_STACK_REG);
1983 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1985 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1988 replace_reg (src1, FIRST_STACK_REG);
1994 /* On i386, reversed forms of subM3 and divM3 exist for
1995 MODE_FLOAT, so the same code that works for addM3 and mulM3
1999 /* These insns can accept the top of stack as a destination
2000 from a stack reg or mem, or can use the top of stack as a
2001 source and some other stack register (possibly top of stack)
2002 as a destination. */
2004 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
2005 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
2007 /* We will fix any death note later. */
2009 if (STACK_REG_P (*src1))
2010 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2012 src1_note = NULL_RTX;
2013 if (STACK_REG_P (*src2))
2014 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
2016 src2_note = NULL_RTX;
2018 /* If either operand is not a stack register, then the dest
2019 must be top of stack. */
2021 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
2022 emit_swap_insn (insn, regstack, *dest);
2025 /* Both operands are REG. If neither operand is already
2026 at the top of stack, choose to make the one that is the dest
2027 the new top of stack. */
2029 int src1_hard_regnum, src2_hard_regnum;
2031 src1_hard_regnum = get_hard_regnum (regstack, *src1);
2032 src2_hard_regnum = get_hard_regnum (regstack, *src2);
2033 if (src1_hard_regnum == -1 || src2_hard_regnum == -1)
2036 if (src1_hard_regnum != FIRST_STACK_REG
2037 && src2_hard_regnum != FIRST_STACK_REG)
2038 emit_swap_insn (insn, regstack, *dest);
2041 if (STACK_REG_P (*src1))
2042 replace_reg (src1, get_hard_regnum (regstack, *src1));
2043 if (STACK_REG_P (*src2))
2044 replace_reg (src2, get_hard_regnum (regstack, *src2));
2048 /* If the register that dies is at the top of stack, then
2049 the destination is somewhere else - merely substitute it.
2050 But if the reg that dies is not at top of stack, then
2051 move the top of stack to the dead reg, as though we had
2052 done the insn and then a store-with-pop. */
2054 if (REGNO (XEXP (src1_note, 0)) == regstack->reg[regstack->top])
2056 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2057 replace_reg (dest, get_hard_regnum (regstack, *dest));
2061 int regno = get_hard_regnum (regstack, XEXP (src1_note, 0));
2063 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2064 replace_reg (dest, regno);
2066 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
2067 = regstack->reg[regstack->top];
2070 CLEAR_HARD_REG_BIT (regstack->reg_set,
2071 REGNO (XEXP (src1_note, 0)));
2072 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2077 if (REGNO (XEXP (src2_note, 0)) == regstack->reg[regstack->top])
2079 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2080 replace_reg (dest, get_hard_regnum (regstack, *dest));
2084 int regno = get_hard_regnum (regstack, XEXP (src2_note, 0));
2086 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2087 replace_reg (dest, regno);
2089 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
2090 = regstack->reg[regstack->top];
2093 CLEAR_HARD_REG_BIT (regstack->reg_set,
2094 REGNO (XEXP (src2_note, 0)));
2095 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
2100 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2101 replace_reg (dest, get_hard_regnum (regstack, *dest));
2107 switch (XINT (SET_SRC (pat), 1))
2111 /* These insns only operate on the top of the stack. */
2113 src1 = get_true_reg (&XVECEXP (SET_SRC (pat), 0, 0));
2115 emit_swap_insn (insn, regstack, *src1);
2117 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2119 if (STACK_REG_P (*dest))
2120 replace_reg (dest, FIRST_STACK_REG);
2124 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2126 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
2129 replace_reg (src1, FIRST_STACK_REG);
2139 /* dest has to be on stack. */
2140 if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
2143 /* This insn requires the top of stack to be the destination. */
2145 /* If the comparison operator is an FP comparison operator,
2146 it is handled correctly by compare_for_stack_reg () who
2147 will move the destination to the top of stack. But if the
2148 comparison operator is not an FP comparison operator, we
2149 have to handle it here. */
2150 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
2151 && REGNO (*dest) != regstack->reg[regstack->top])
2152 emit_swap_insn (insn, regstack, *dest);
2154 src1 = get_true_reg (&XEXP (SET_SRC (pat), 1));
2155 src2 = get_true_reg (&XEXP (SET_SRC (pat), 2));
2157 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2158 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
2165 src_note[1] = src1_note;
2166 src_note[2] = src2_note;
2168 if (STACK_REG_P (*src1))
2169 replace_reg (src1, get_hard_regnum (regstack, *src1));
2170 if (STACK_REG_P (*src2))
2171 replace_reg (src2, get_hard_regnum (regstack, *src2));
2173 for (i = 1; i <= 2; i++)
2176 /* If the register that dies is not at the top of stack, then
2177 move the top of stack to the dead reg */
2178 if (REGNO (XEXP (src_note[i], 0))
2179 != regstack->reg[regstack->top])
2181 remove_regno_note (insn, REG_DEAD,
2182 REGNO (XEXP (src_note [i], 0)));
2183 emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
2188 CLEAR_HARD_REG_BIT (regstack->reg_set,
2189 REGNO (XEXP (src_note[i], 0)));
2190 replace_reg (&XEXP (src_note[i], 0), FIRST_STACK_REG);
2196 /* Make dest the top of stack. */
2197 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2198 replace_reg (dest, FIRST_STACK_REG);
2207 /* Substitute hard regnums for any stack regs in INSN, which has
2208 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
2209 before the insn, and is updated with changes made here.
2211 There are several requirements and assumptions about the use of
2212 stack-like regs in asm statements. These rules are enforced by
2213 record_asm_stack_regs; see comments there for details. Any
2214 asm_operands left in the RTL at this point may be assume to meet the
2215 requirements, since record_asm_stack_regs removes any problem asm. */
2218 subst_asm_stack_regs (insn, regstack)
2222 rtx body = PATTERN (insn);
2225 rtx *note_reg; /* Array of note contents */
2226 rtx **note_loc; /* Address of REG field of each note */
2227 enum reg_note *note_kind; /* The type of each note */
2232 struct stack_def temp_stack;
2237 int n_inputs, n_outputs;
2239 /* Find out what the constraints required. If no constraint
2240 alternative matches, that is a compiler bug: we should have caught
2241 such an insn during the life analysis pass (and reload should have
2242 caught it regardless). */
2243 extract_insn (insn);
2244 constrain_operands (1);
2245 alt = which_alternative;
2247 preprocess_constraints ();
2249 n_inputs = get_asm_operand_n_inputs (body);
2250 n_outputs = recog_n_operands - n_inputs;
2255 /* Strip SUBREGs here to make the following code simpler. */
2256 for (i = 0; i < recog_n_operands; i++)
2257 if (GET_CODE (recog_operand[i]) == SUBREG
2258 && GET_CODE (SUBREG_REG (recog_operand[i])) == REG)
2260 recog_operand_loc[i] = & SUBREG_REG (recog_operand[i]);
2261 recog_operand[i] = SUBREG_REG (recog_operand[i]);
2264 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
2266 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
2269 note_reg = (rtx *) alloca (i * sizeof (rtx));
2270 note_loc = (rtx **) alloca (i * sizeof (rtx *));
2271 note_kind = (enum reg_note *) alloca (i * sizeof (enum reg_note));
2274 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2276 rtx reg = XEXP (note, 0);
2277 rtx *loc = & XEXP (note, 0);
2279 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
2281 loc = & SUBREG_REG (reg);
2282 reg = SUBREG_REG (reg);
2285 if (STACK_REG_P (reg)
2286 && (REG_NOTE_KIND (note) == REG_DEAD
2287 || REG_NOTE_KIND (note) == REG_UNUSED))
2289 note_reg[n_notes] = reg;
2290 note_loc[n_notes] = loc;
2291 note_kind[n_notes] = REG_NOTE_KIND (note);
2296 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2300 if (GET_CODE (body) == PARALLEL)
2302 clobber_reg = (rtx *) alloca (XVECLEN (body, 0) * sizeof (rtx));
2303 clobber_loc = (rtx **) alloca (XVECLEN (body, 0) * sizeof (rtx *));
2305 for (i = 0; i < XVECLEN (body, 0); i++)
2306 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
2308 rtx clobber = XVECEXP (body, 0, i);
2309 rtx reg = XEXP (clobber, 0);
2310 rtx *loc = & XEXP (clobber, 0);
2312 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
2314 loc = & SUBREG_REG (reg);
2315 reg = SUBREG_REG (reg);
2318 if (STACK_REG_P (reg))
2320 clobber_reg[n_clobbers] = reg;
2321 clobber_loc[n_clobbers] = loc;
2327 bcopy ((char *) regstack, (char *) &temp_stack, sizeof (temp_stack));
2329 /* Put the input regs into the desired place in TEMP_STACK. */
2331 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2332 if (STACK_REG_P (recog_operand[i])
2333 && reg_class_subset_p (recog_op_alt[i][alt].class,
2335 && recog_op_alt[i][alt].class != FLOAT_REGS)
2337 /* If an operand needs to be in a particular reg in
2338 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2339 these constraints are for single register classes, and reload
2340 guaranteed that operand[i] is already in that class, we can
2341 just use REGNO (recog_operand[i]) to know which actual reg this
2342 operand needs to be in. */
2344 int regno = get_hard_regnum (&temp_stack, recog_operand[i]);
2349 if (regno != REGNO (recog_operand[i]))
2351 /* recog_operand[i] is not in the right place. Find it
2352 and swap it with whatever is already in I's place.
2353 K is where recog_operand[i] is now. J is where it should
2357 k = temp_stack.top - (regno - FIRST_STACK_REG);
2359 - (REGNO (recog_operand[i]) - FIRST_STACK_REG));
2361 temp = temp_stack.reg[k];
2362 temp_stack.reg[k] = temp_stack.reg[j];
2363 temp_stack.reg[j] = temp;
2367 /* emit insns before INSN to make sure the reg-stack is in the right
2370 change_stack (insn, regstack, &temp_stack, emit_insn_before);
2372 /* Make the needed input register substitutions. Do death notes and
2373 clobbers too, because these are for inputs, not outputs. */
2375 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2376 if (STACK_REG_P (recog_operand[i]))
2378 int regnum = get_hard_regnum (regstack, recog_operand[i]);
2383 replace_reg (recog_operand_loc[i], regnum);
2386 for (i = 0; i < n_notes; i++)
2387 if (note_kind[i] == REG_DEAD)
2389 int regnum = get_hard_regnum (regstack, note_reg[i]);
2394 replace_reg (note_loc[i], regnum);
2397 for (i = 0; i < n_clobbers; i++)
2399 /* It's OK for a CLOBBER to reference a reg that is not live.
2400 Don't try to replace it in that case. */
2401 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2405 /* Sigh - clobbers always have QImode. But replace_reg knows
2406 that these regs can't be MODE_INT and will abort. Just put
2407 the right reg there without calling replace_reg. */
2409 *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
2413 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2415 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2416 if (STACK_REG_P (recog_operand[i]))
2418 /* An input reg is implicitly popped if it is tied to an
2419 output, or if there is a CLOBBER for it. */
2422 for (j = 0; j < n_clobbers; j++)
2423 if (operands_match_p (clobber_reg[j], recog_operand[i]))
2426 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
2428 /* recog_operand[i] might not be at the top of stack. But that's
2429 OK, because all we need to do is pop the right number of regs
2430 off of the top of the reg-stack. record_asm_stack_regs
2431 guaranteed that all implicitly popped regs were grouped
2432 at the top of the reg-stack. */
2434 CLEAR_HARD_REG_BIT (regstack->reg_set,
2435 regstack->reg[regstack->top]);
2440 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2441 Note that there isn't any need to substitute register numbers.
2442 ??? Explain why this is true. */
2444 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2446 /* See if there is an output for this hard reg. */
2449 for (j = 0; j < n_outputs; j++)
2450 if (STACK_REG_P (recog_operand[j]) && REGNO (recog_operand[j]) == i)
2452 regstack->reg[++regstack->top] = i;
2453 SET_HARD_REG_BIT (regstack->reg_set, i);
2458 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2459 input that the asm didn't implicitly pop. If the asm didn't
2460 implicitly pop an input reg, that reg will still be live.
2462 Note that we can't use find_regno_note here: the register numbers
2463 in the death notes have already been substituted. */
2465 for (i = 0; i < n_outputs; i++)
2466 if (STACK_REG_P (recog_operand[i]))
2470 for (j = 0; j < n_notes; j++)
2471 if (REGNO (recog_operand[i]) == REGNO (note_reg[j])
2472 && note_kind[j] == REG_UNUSED)
2474 insn = emit_pop_insn (insn, regstack, recog_operand[i],
2480 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2481 if (STACK_REG_P (recog_operand[i]))
2485 for (j = 0; j < n_notes; j++)
2486 if (REGNO (recog_operand[i]) == REGNO (note_reg[j])
2487 && note_kind[j] == REG_DEAD
2488 && TEST_HARD_REG_BIT (regstack->reg_set,
2489 REGNO (recog_operand[i])))
2491 insn = emit_pop_insn (insn, regstack, recog_operand[i],
2498 /* Substitute stack hard reg numbers for stack virtual registers in
2499 INSN. Non-stack register numbers are not changed. REGSTACK is the
2500 current stack content. Insns may be emitted as needed to arrange the
2501 stack for the 387 based on the contents of the insn. */
2504 subst_stack_regs (insn, regstack)
2508 register rtx *note_link, note;
2511 if (GET_CODE (insn) == CALL_INSN)
2513 int top = regstack->top;
2515 /* If there are any floating point parameters to be passed in
2516 registers for this call, make sure they are in the right
2521 straighten_stack (PREV_INSN (insn), regstack);
2523 /* Now mark the arguments as dead after the call. */
2525 while (regstack->top >= 0)
2527 CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
2533 /* Do the actual substitution if any stack regs are mentioned.
2534 Since we only record whether entire insn mentions stack regs, and
2535 subst_stack_regs_pat only works for patterns that contain stack regs,
2536 we must check each pattern in a parallel here. A call_value_pop could
2539 if (stack_regs_mentioned (insn))
2541 int n_operands = asm_noperands (PATTERN (insn));
2542 if (n_operands >= 0)
2544 /* This insn is an `asm' with operands. Decode the operands,
2545 decide how many are inputs, and do register substitution.
2546 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2548 subst_asm_stack_regs (insn, regstack);
2552 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2553 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2555 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2557 subst_stack_regs_pat (insn, regstack,
2558 XVECEXP (PATTERN (insn), 0, i));
2560 /* subst_stack_regs_pat may have deleted a no-op insn. */
2561 if (GET_CODE (insn) == NOTE)
2566 subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2569 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2570 REG_UNUSED will already have been dealt with, so just return. */
2572 if (GET_CODE (insn) == NOTE)
2575 /* If there is a REG_UNUSED note on a stack register on this insn,
2576 the indicated reg must be popped. The REG_UNUSED note is removed,
2577 since the form of the newly emitted pop insn references the reg,
2578 making it no longer `unset'. */
2580 note_link = ®_NOTES(insn);
2581 for (note = *note_link; note; note = XEXP (note, 1))
2582 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2584 *note_link = XEXP (note, 1);
2585 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), emit_insn_after);
2588 note_link = &XEXP (note, 1);
2591 /* Change the organization of the stack so that it fits a new basic
2592 block. Some registers might have to be popped, but there can never be
2593 a register live in the new block that is not now live.
2595 Insert any needed insns before or after INSN. WHEN is emit_insn_before
2596 or emit_insn_after. OLD is the original stack layout, and NEW is
2597 the desired form. OLD is updated to reflect the code emitted, ie, it
2598 will be the same as NEW upon return.
2600 This function will not preserve block_end[]. But that information
2601 is no longer needed once this has executed. */
2604 change_stack (insn, old, new, when)
2612 /* We will be inserting new insns "backwards", by calling emit_insn_before.
2613 If we are to insert after INSN, find the next insn, and insert before
2616 if (when == emit_insn_after)
2617 insn = NEXT_INSN (insn);
2619 /* Pop any registers that are not needed in the new block. */
2621 for (reg = old->top; reg >= 0; reg--)
2622 if (! TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2623 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[reg], DFmode),
2628 /* If the new block has never been processed, then it can inherit
2629 the old stack order. */
2631 new->top = old->top;
2632 bcopy (old->reg, new->reg, sizeof (new->reg));
2636 /* This block has been entered before, and we must match the
2637 previously selected stack order. */
2639 /* By now, the only difference should be the order of the stack,
2640 not their depth or liveliness. */
2642 GO_IF_HARD_REG_EQUAL (old->reg_set, new->reg_set, win);
2648 if (old->top != new->top)
2651 /* If the stack is not empty (new->top != -1), loop here emitting
2652 swaps until the stack is correct.
2654 The worst case number of swaps emitted is N + 2, where N is the
2655 depth of the stack. In some cases, the reg at the top of
2656 stack may be correct, but swapped anyway in order to fix
2657 other regs. But since we never swap any other reg away from
2658 its correct slot, this algorithm will converge. */
2663 /* Swap the reg at top of stack into the position it is
2664 supposed to be in, until the correct top of stack appears. */
2666 while (old->reg[old->top] != new->reg[new->top])
2668 for (reg = new->top; reg >= 0; reg--)
2669 if (new->reg[reg] == old->reg[old->top])
2675 emit_swap_insn (insn, old,
2676 FP_MODE_REG (old->reg[reg], DFmode));
2679 /* See if any regs remain incorrect. If so, bring an
2680 incorrect reg to the top of stack, and let the while loop
2683 for (reg = new->top; reg >= 0; reg--)
2684 if (new->reg[reg] != old->reg[reg])
2686 emit_swap_insn (insn, old,
2687 FP_MODE_REG (old->reg[reg], DFmode));
2692 /* At this point there must be no differences. */
2694 for (reg = old->top; reg >= 0; reg--)
2695 if (old->reg[reg] != new->reg[reg])
2700 /* Check PAT, which points to RTL in INSN, for a LABEL_REF. If it is
2701 found, ensure that a jump from INSN to the code_label to which the
2702 label_ref points ends up with the same stack as that at the
2703 code_label. Do this by inserting insns just before the code_label to
2704 pop and rotate the stack until it is in the correct order. REGSTACK
2705 is the order of the register stack in INSN.
2707 Any code that is emitted here must not be later processed as part
2708 of any block, as it will already contain hard register numbers. */
2711 goto_block_pat (insn, regstack, pat)
2717 rtx new_jump, new_label, new_barrier;
2720 struct stack_def temp_stack;
2723 switch (GET_CODE (pat))
2726 straighten_stack (PREV_INSN (insn), regstack);
2731 char *fmt = GET_RTX_FORMAT (GET_CODE (pat));
2733 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
2736 goto_block_pat (insn, regstack, XEXP (pat, i));
2738 for (j = 0; j < XVECLEN (pat, i); j++)
2739 goto_block_pat (insn, regstack, XVECEXP (pat, i, j));
2746 label = XEXP (pat, 0);
2747 if (GET_CODE (label) != CODE_LABEL)
2750 /* First, see if in fact anything needs to be done to the stack at all. */
2751 if (INSN_UID (label) <= 0)
2754 label_stack = &block_stack_in[BLOCK_NUM (label)];
2756 if (label_stack->top == -2)
2758 /* If the target block hasn't had a stack order selected, then
2759 we need merely ensure that no pops are needed. */
2761 for (reg = regstack->top; reg >= 0; reg--)
2762 if (! TEST_HARD_REG_BIT (label_stack->reg_set, regstack->reg[reg]))
2767 /* change_stack will not emit any code in this case. */
2769 change_stack (label, regstack, label_stack, emit_insn_after);
2773 else if (label_stack->top == regstack->top)
2775 for (reg = label_stack->top; reg >= 0; reg--)
2776 if (label_stack->reg[reg] != regstack->reg[reg])
2783 /* At least one insn will need to be inserted before label. Insert
2784 a jump around the code we are about to emit. Emit a label for the new
2785 code, and point the original insn at this new label. We can't use
2786 redirect_jump here, because we're using fld[4] of the code labels as
2787 LABEL_REF chains, no NUSES counters. */
2789 new_jump = emit_jump_insn_before (gen_jump (label), label);
2790 record_label_references (new_jump, PATTERN (new_jump));
2791 JUMP_LABEL (new_jump) = label;
2793 new_barrier = emit_barrier_after (new_jump);
2795 new_label = gen_label_rtx ();
2796 emit_label_after (new_label, new_barrier);
2797 LABEL_REFS (new_label) = new_label;
2799 /* The old label_ref will no longer point to the code_label if now uses,
2800 so strip the label_ref from the code_label's chain of references. */
2802 for (ref = &LABEL_REFS (label); *ref != label; ref = &LABEL_NEXTREF (*ref))
2809 *ref = LABEL_NEXTREF (*ref);
2811 XEXP (pat, 0) = new_label;
2812 record_label_references (insn, PATTERN (insn));
2814 if (JUMP_LABEL (insn) == label)
2815 JUMP_LABEL (insn) = new_label;
2817 /* Now emit the needed code. */
2819 temp_stack = *regstack;
2821 change_stack (new_label, &temp_stack, label_stack, emit_insn_after);
2824 /* Traverse all basic blocks in a function, converting the register
2825 references in each insn from the "flat" register file that gcc uses, to
2826 the stack-like registers the 387 uses. */
2831 register int block, reg;
2832 register rtx insn, next;
2833 struct stack_def regstack;
2835 for (block = 0; block < blocks; block++)
2837 if (block_stack_in[block].top == -2)
2839 /* This block has not been previously encountered. Choose a
2840 default mapping for any stack regs live on entry */
2842 block_stack_in[block].top = -1;
2844 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
2845 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, reg))
2846 block_stack_in[block].reg[++block_stack_in[block].top] = reg;
2849 /* Process all insns in this block. Keep track of `next' here,
2850 so that we don't process any insns emitted while making
2851 substitutions in INSN. */
2853 next = block_begin[block];
2854 regstack = block_stack_in[block];
2858 next = NEXT_INSN (insn);
2860 /* Don't bother processing unless there is a stack reg
2861 mentioned or if it's a CALL_INSN (register passing of
2862 floating point values). */
2864 if (stack_regs_mentioned (insn) || GET_CODE (insn) == CALL_INSN)
2865 subst_stack_regs (insn, ®stack);
2867 } while (insn != block_end[block]);
2869 /* For all further actions, INSN needs to be the last insn in
2870 this basic block. If subst_stack_regs inserted additional
2871 instructions after INSN, it is no longer the last one at
2873 next = PREV_INSN (next);
2875 /* If subst_stack_regs inserted something after a JUMP_INSN, that
2876 is almost certainly a bug. */
2877 if (GET_CODE (insn) == JUMP_INSN && insn != next)
2881 /* Something failed if the stack life doesn't match. */
2883 GO_IF_HARD_REG_EQUAL (regstack.reg_set, block_out_reg_set[block], win);
2889 /* Adjust the stack of this block on exit to match the stack of
2890 the target block, or copy stack information into stack of
2891 jump target if the target block's stack order hasn't been set
2894 if (GET_CODE (insn) == JUMP_INSN)
2895 goto_block_pat (insn, ®stack, PATTERN (insn));
2897 /* Likewise handle the case where we fall into the next block. */
2899 if ((block < blocks - 1) && block_drops_in[block+1])
2900 change_stack (insn, ®stack, &block_stack_in[block+1],
2904 /* If the last basic block is the end of a loop, and that loop has
2905 regs live at its start, then the last basic block will have regs live
2906 at its end that need to be popped before the function returns. */
2909 int value_reg_low, value_reg_high;
2910 value_reg_low = value_reg_high = -1;
2913 if ((retvalue = stack_result (current_function_decl)))
2915 value_reg_low = REGNO (retvalue);
2916 value_reg_high = value_reg_low +
2917 HARD_REGNO_NREGS (value_reg_low, GET_MODE (retvalue)) - 1;
2921 for (reg = regstack.top; reg >= 0; reg--)
2922 if (regstack.reg[reg] < value_reg_low
2923 || regstack.reg[reg] > value_reg_high)
2924 insn = emit_pop_insn (insn, ®stack,
2925 FP_MODE_REG (regstack.reg[reg], DFmode),
2928 straighten_stack (insn, ®stack);
2931 /* Check expression PAT, which is in INSN, for label references. if
2932 one is found, print the block number of destination to FILE. */
2935 print_blocks (file, insn, pat)
2939 register RTX_CODE code = GET_CODE (pat);
2943 if (code == LABEL_REF)
2945 register rtx label = XEXP (pat, 0);
2947 if (GET_CODE (label) != CODE_LABEL)
2950 fprintf (file, " %d", BLOCK_NUM (label));
2955 fmt = GET_RTX_FORMAT (code);
2956 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2959 print_blocks (file, insn, XEXP (pat, i));
2963 for (j = 0; j < XVECLEN (pat, i); j++)
2964 print_blocks (file, insn, XVECEXP (pat, i, j));
2969 /* Write information about stack registers and stack blocks into FILE.
2970 This is part of making a debugging dump. */
2973 dump_stack_info (file)
2978 fprintf (file, "\n%d stack blocks.\n", blocks);
2979 for (block = 0; block < blocks; block++)
2981 register rtx head, jump, end;
2984 fprintf (file, "\nStack block %d: first insn %d, last %d.\n",
2985 block, INSN_UID (block_begin[block]),
2986 INSN_UID (block_end[block]));
2988 head = block_begin[block];
2990 fprintf (file, "Reached from blocks: ");
2991 if (GET_CODE (head) == CODE_LABEL)
2992 for (jump = LABEL_REFS (head);
2994 jump = LABEL_NEXTREF (jump))
2996 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
2997 fprintf (file, " %d", from_block);
2999 if (block_drops_in[block])
3000 fprintf (file, " previous");
3002 fprintf (file, "\nlive stack registers on block entry: ");
3003 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
3005 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, regno))
3006 fprintf (file, "%d ", regno);
3009 fprintf (file, "\nlive stack registers on block exit: ");
3010 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
3012 if (TEST_HARD_REG_BIT (block_out_reg_set[block], regno))
3013 fprintf (file, "%d ", regno);
3016 end = block_end[block];
3018 fprintf (file, "\nJumps to blocks: ");
3019 if (GET_CODE (end) == JUMP_INSN)
3020 print_blocks (file, end, PATTERN (end));
3022 if (block + 1 < blocks && block_drops_in[block+1])
3023 fprintf (file, " next");
3024 else if (block + 1 == blocks
3025 || (GET_CODE (end) == JUMP_INSN
3026 && GET_CODE (PATTERN (end)) == RETURN))
3027 fprintf (file, " return");
3029 fprintf (file, "\n");
3032 #endif /* STACK_REGS */