Declare malloc, free, and atexit if inhibit_libc is defined.
[official-gcc.git] / gcc / reg-stack.c
blob0cbc5da21247228a7d39bb2518c0a4accc7ed4e2
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)
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 * asm_operands:
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
81 stack-like regs:
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
89 output operand.
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
96 up".
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)");
153 #include "config.h"
154 #include "system.h"
155 #include "tree.h"
156 #include "rtl.h"
157 #include "tm_p.h"
158 #include "function.h"
159 #include "insn-config.h"
160 #include "regs.h"
161 #include "hard-reg-set.h"
162 #include "flags.h"
163 #include "insn-flags.h"
164 #include "toplev.h"
165 #include "recog.h"
166 #include "varray.h"
168 #ifdef STACK_REGS
170 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
172 /* This is the basic stack record. TOP is an index into REG[] such
173 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
175 If TOP is -2, REG[] is not yet initialized. Stack initialization
176 consists of placing each live reg in array `reg' and setting `top'
177 appropriately.
179 REG_SET indicates which registers are live. */
181 typedef struct stack_def
183 int top; /* index to top stack element */
184 HARD_REG_SET reg_set; /* set of live registers */
185 char reg[REG_STACK_SIZE]; /* register - stack mapping */
186 } *stack;
188 /* highest instruction uid */
189 static int max_uid = 0;
191 /* Number of basic blocks in the current function. */
192 static int blocks;
194 /* Element N is first insn in basic block N.
195 This info lasts until we finish compiling the function. */
196 static rtx *block_begin;
198 /* Element N is last insn in basic block N.
199 This info lasts until we finish compiling the function. */
200 static rtx *block_end;
202 /* Element N is nonzero if control can drop into basic block N */
203 static char *block_drops_in;
205 /* Element N says all about the stack at entry block N */
206 static stack block_stack_in;
208 /* Element N says all about the stack life at the end of block N */
209 static HARD_REG_SET *block_out_reg_set;
211 /* This is where the BLOCK_NUM values are really stored. This is set
212 up by find_blocks and used there and in life_analysis. It can be used
213 later, but only to look up an insn that is the head or tail of some
214 block. life_analysis and the stack register conversion process can
215 add insns within a block. */
216 static int *block_number;
218 /* We use this array to cache info about insns, because otherwise we
219 spend too much time in stack_regs_mentioned_p.
221 Indexed by insn UIDs. A value of zero is uninitialized, one indicates
222 the insn uses stack registers, two indicates the insn does not use
223 stack registers. */
224 static varray_type stack_regs_mentioned_data;
226 /* This is the register file for all register after conversion */
227 static rtx
228 FP_mode_reg[LAST_STACK_REG+1-FIRST_STACK_REG][(int) MAX_MACHINE_MODE];
230 #define FP_MODE_REG(regno,mode) \
231 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int)(mode)])
233 /* Get the basic block number of an insn. See note at block_number
234 definition are validity of this information. */
236 #define BLOCK_NUM(INSN) \
237 ((INSN_UID (INSN) > max_uid) \
238 ? (abort() , -1) : block_number[INSN_UID (INSN)])
240 /* Forward declarations */
242 static int stack_regs_mentioned_p PROTO((rtx pat));
243 static void mark_regs_pat PROTO((rtx, HARD_REG_SET *));
244 static void straighten_stack PROTO((rtx, stack));
245 static void pop_stack PROTO((stack, int));
246 static void record_label_references PROTO((rtx, rtx));
247 static rtx *get_true_reg PROTO((rtx *));
249 static void record_asm_reg_life PROTO((rtx, stack));
250 static void record_reg_life_pat PROTO((rtx, HARD_REG_SET *,
251 HARD_REG_SET *, int));
252 static int get_asm_operand_n_inputs PROTO((rtx));
253 static void record_reg_life PROTO((rtx, int, stack));
254 static void find_blocks PROTO((rtx));
255 static rtx stack_result PROTO((tree));
256 static void stack_reg_life_analysis PROTO((rtx, HARD_REG_SET *));
257 static void replace_reg PROTO((rtx *, int));
258 static void remove_regno_note PROTO((rtx, enum reg_note, int));
259 static int get_hard_regnum PROTO((stack, rtx));
260 static void delete_insn_for_stacker PROTO((rtx));
261 static rtx emit_pop_insn PROTO((rtx, stack, rtx, rtx (*) ()));
262 static void emit_swap_insn PROTO((rtx, stack, rtx));
263 static void move_for_stack_reg PROTO((rtx, stack, rtx));
264 static int swap_rtx_condition_1 PROTO((rtx));
265 static int swap_rtx_condition PROTO((rtx));
266 static void compare_for_stack_reg PROTO((rtx, stack, rtx));
267 static void subst_stack_regs_pat PROTO((rtx, stack, rtx));
268 static void subst_asm_stack_regs PROTO((rtx, stack));
269 static void subst_stack_regs PROTO((rtx, stack));
270 static void change_stack PROTO((rtx, stack, stack, rtx (*) ()));
272 static void goto_block_pat PROTO((rtx, stack, rtx));
273 static void convert_regs PROTO((void));
274 static void print_blocks PROTO((FILE *, rtx, rtx));
275 static void dump_stack_info PROTO((FILE *));
277 /* Return non-zero if any stack register is mentioned somewhere within PAT. */
279 static int
280 stack_regs_mentioned_p (pat)
281 rtx pat;
283 register const char *fmt;
284 register int i;
286 if (STACK_REG_P (pat))
287 return 1;
289 fmt = GET_RTX_FORMAT (GET_CODE (pat));
290 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
292 if (fmt[i] == 'E')
294 register int j;
296 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
297 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
298 return 1;
300 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
301 return 1;
304 return 0;
307 /* Return nonzero if INSN mentions stacked registers, else return zero. */
310 stack_regs_mentioned (insn)
311 rtx insn;
313 unsigned int uid, max;
314 int test;
316 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
317 return 0;
319 uid = INSN_UID (insn);
320 max = VARRAY_SIZE (stack_regs_mentioned_data);
321 if (uid >= max)
323 /* Allocate some extra size to avoid too many reallocs, but
324 do not grow too quickly. */
325 max = uid + uid / 20;
326 VARRAY_GROW (stack_regs_mentioned_data, max);
329 test = VARRAY_CHAR (stack_regs_mentioned_data, uid);
330 if (test == 0)
332 /* This insn has yet to be examined. Do so now. */
333 test = stack_regs_mentioned_p (PATTERN (insn)) ? 1 : 2;
334 VARRAY_CHAR (stack_regs_mentioned_data, uid) = test;
337 return test == 1;
340 static rtx ix86_flags_rtx;
342 static rtx
343 next_flags_user (insn)
344 rtx insn;
346 /* Search forward looking for the first use of this value.
347 Stop at block boundaries. */
348 /* ??? This really cries for BLOCK_END! */
350 while (1)
352 insn = NEXT_INSN (insn);
353 if (!insn)
354 return NULL_RTX;
356 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
357 && reg_mentioned_p (ix86_flags_rtx, PATTERN (insn)))
358 return insn;
360 if (GET_CODE (insn) == JUMP_INSN
361 || GET_CODE (insn) == CODE_LABEL
362 || GET_CODE (insn) == CALL_INSN)
363 return NULL_RTX;
367 /* Mark all registers needed for this pattern. */
369 static void
370 mark_regs_pat (pat, set)
371 rtx pat;
372 HARD_REG_SET *set;
374 enum machine_mode mode;
375 register int regno;
376 register int count;
378 if (GET_CODE (pat) == SUBREG)
380 mode = GET_MODE (pat);
381 regno = SUBREG_WORD (pat);
382 regno += REGNO (SUBREG_REG (pat));
384 else
385 regno = REGNO (pat), mode = GET_MODE (pat);
387 for (count = HARD_REGNO_NREGS (regno, mode);
388 count; count--, regno++)
389 SET_HARD_REG_BIT (*set, regno);
392 /* Reorganise the stack into ascending numbers,
393 after this insn. */
395 static void
396 straighten_stack (insn, regstack)
397 rtx insn;
398 stack regstack;
400 struct stack_def temp_stack;
401 int top;
403 /* If there is only a single register on the stack, then the stack is
404 already in increasing order and no reorganization is needed.
406 Similarly if the stack is empty. */
407 if (regstack->top <= 0)
408 return;
410 COPY_HARD_REG_SET (temp_stack.reg_set, regstack->reg_set);
412 for (top = temp_stack.top = regstack->top; top >= 0; top--)
413 temp_stack.reg[top] = FIRST_STACK_REG + temp_stack.top - top;
415 change_stack (insn, regstack, &temp_stack, emit_insn_after);
418 /* Pop a register from the stack */
420 static void
421 pop_stack (regstack, regno)
422 stack regstack;
423 int regno;
425 int top = regstack->top;
427 CLEAR_HARD_REG_BIT (regstack->reg_set, regno);
428 regstack->top--;
429 /* If regno was not at the top of stack then adjust stack */
430 if (regstack->reg [top] != regno)
432 int i;
433 for (i = regstack->top; i >= 0; i--)
434 if (regstack->reg [i] == regno)
436 int j;
437 for (j = i; j < top; j++)
438 regstack->reg [j] = regstack->reg [j + 1];
439 break;
444 /* Convert register usage from "flat" register file usage to a "stack
445 register file. FIRST is the first insn in the function, FILE is the
446 dump file, if used.
448 First compute the beginning and end of each basic block. Do a
449 register life analysis on the stack registers, recording the result
450 for the head and tail of each basic block. The convert each insn one
451 by one. Run a last jump_optimize() pass, if optimizing, to eliminate
452 any cross-jumping created when the converter inserts pop insns.*/
454 void
455 reg_to_stack (first, file)
456 rtx first;
457 FILE *file;
459 register rtx insn;
460 register int i;
461 int stack_reg_seen = 0;
462 enum machine_mode mode;
463 HARD_REG_SET stackentry;
465 ix86_flags_rtx = gen_rtx_REG (CCmode, FLAGS_REG);
467 max_uid = get_max_uid ();
468 VARRAY_CHAR_INIT (stack_regs_mentioned_data, max_uid + 1,
469 "stack_regs_mentioned cache");
471 CLEAR_HARD_REG_SET (stackentry);
474 static int initialised;
475 if (!initialised)
477 #if 0
478 initialised = 1; /* This array can not have been previously
479 initialised, because the rtx's are
480 thrown away between compilations of
481 functions. */
482 #endif
483 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
485 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
486 mode = GET_MODE_WIDER_MODE (mode))
487 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
488 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT); mode != VOIDmode;
489 mode = GET_MODE_WIDER_MODE (mode))
490 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
495 /* Count the basic blocks. Also find maximum insn uid. */
497 register RTX_CODE prev_code = BARRIER;
498 register RTX_CODE code;
499 register int before_function_beg = 1;
501 max_uid = 0;
502 blocks = 0;
503 for (insn = first; insn; insn = NEXT_INSN (insn))
505 /* Note that this loop must select the same block boundaries
506 as code in find_blocks. Also note that this code is not the
507 same as that used in flow.c. */
509 if (INSN_UID (insn) > max_uid)
510 max_uid = INSN_UID (insn);
512 code = GET_CODE (insn);
514 if (code == CODE_LABEL
515 || (prev_code != INSN
516 && prev_code != CALL_INSN
517 && prev_code != CODE_LABEL
518 && GET_RTX_CLASS (code) == 'i'))
519 blocks++;
521 if (code == NOTE && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
522 before_function_beg = 0;
524 /* Remember whether or not this insn mentions an FP regs.
525 Check JUMP_INSNs too, in case someone creates a funny PARALLEL. */
527 if (GET_RTX_CLASS (code) == 'i'
528 && stack_regs_mentioned_p (PATTERN (insn)))
530 stack_reg_seen = 1;
531 VARRAY_CHAR (stack_regs_mentioned_data, INSN_UID (insn)) = 1;
533 /* Note any register passing parameters. */
535 if (before_function_beg && code == INSN
536 && GET_CODE (PATTERN (insn)) == USE)
537 record_reg_life_pat (PATTERN (insn), (HARD_REG_SET *) 0,
538 &stackentry, 1);
540 else
541 VARRAY_CHAR (stack_regs_mentioned_data, INSN_UID (insn)) = 2;
543 if (code == CODE_LABEL)
544 LABEL_REFS (insn) = insn; /* delete old chain */
546 if (code != NOTE)
547 prev_code = code;
551 /* If no stack register reference exists in this insn, there isn't
552 anything to convert. */
554 if (! stack_reg_seen)
556 VARRAY_FREE (stack_regs_mentioned_data);
557 return;
560 /* If there are stack registers, there must be at least one block. */
562 if (! blocks)
563 abort ();
565 /* Allocate some tables that last till end of compiling this function
566 and some needed only in find_blocks and life_analysis. */
568 block_begin = (rtx *) alloca (blocks * sizeof (rtx));
569 block_end = (rtx *) alloca (blocks * sizeof (rtx));
570 block_drops_in = (char *) alloca (blocks);
572 block_stack_in = (stack) alloca (blocks * sizeof (struct stack_def));
573 block_out_reg_set = (HARD_REG_SET *) alloca (blocks * sizeof (HARD_REG_SET));
574 bzero ((char *) block_stack_in, blocks * sizeof (struct stack_def));
575 bzero ((char *) block_out_reg_set, blocks * sizeof (HARD_REG_SET));
577 block_number = (int *) alloca ((max_uid + 1) * sizeof (int));
579 find_blocks (first);
580 stack_reg_life_analysis (first, &stackentry);
582 /* Dump the life analysis debug information before jump
583 optimization, as that will destroy the LABEL_REFS we keep the
584 information in. */
586 if (file)
587 dump_stack_info (file);
589 convert_regs ();
591 if (optimize)
592 jump_optimize (first, 2, 0, 0);
594 VARRAY_FREE (stack_regs_mentioned_data);
597 /* Check PAT, which is in INSN, for LABEL_REFs. Add INSN to the
598 label's chain of references, and note which insn contains each
599 reference. */
601 static void
602 record_label_references (insn, pat)
603 rtx insn, pat;
605 register enum rtx_code code = GET_CODE (pat);
606 register int i;
607 register const char *fmt;
609 if (code == LABEL_REF)
611 register rtx label = XEXP (pat, 0);
612 register rtx ref;
614 if (GET_CODE (label) != CODE_LABEL)
615 abort ();
617 /* If this is an undefined label, LABEL_REFS (label) contains
618 garbage. */
619 if (INSN_UID (label) == 0)
620 return;
622 /* Don't make a duplicate in the code_label's chain. */
624 for (ref = LABEL_REFS (label);
625 ref && ref != label;
626 ref = LABEL_NEXTREF (ref))
627 if (CONTAINING_INSN (ref) == insn)
628 return;
630 CONTAINING_INSN (pat) = insn;
631 LABEL_NEXTREF (pat) = LABEL_REFS (label);
632 LABEL_REFS (label) = pat;
634 return;
637 fmt = GET_RTX_FORMAT (code);
638 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
640 if (fmt[i] == 'e')
641 record_label_references (insn, XEXP (pat, i));
642 if (fmt[i] == 'E')
644 register int j;
645 for (j = 0; j < XVECLEN (pat, i); j++)
646 record_label_references (insn, XVECEXP (pat, i, j));
651 /* Return a pointer to the REG expression within PAT. If PAT is not a
652 REG, possible enclosed by a conversion rtx, return the inner part of
653 PAT that stopped the search. */
655 static rtx *
656 get_true_reg (pat)
657 rtx *pat;
659 for (;;)
660 switch (GET_CODE (*pat))
662 case SUBREG:
663 /* Eliminate FP subregister accesses in favour of the
664 actual FP register in use. */
666 rtx subreg;
667 if (FP_REG_P (subreg = SUBREG_REG (*pat)))
669 *pat = FP_MODE_REG (REGNO (subreg) + SUBREG_WORD (*pat),
670 GET_MODE (subreg));
671 default:
672 return pat;
675 case FLOAT:
676 case FIX:
677 case FLOAT_EXTEND:
678 pat = & XEXP (*pat, 0);
682 /* Record the life info of each stack reg in INSN, updating REGSTACK.
683 N_INPUTS is the number of inputs; N_OUTPUTS the outputs.
685 There are many rules that an asm statement for stack-like regs must
686 follow. Those rules are explained at the top of this file: the rule
687 numbers below refer to that explanation. */
689 static void
690 record_asm_reg_life (insn, regstack)
691 rtx insn;
692 stack regstack;
694 int i;
695 int n_clobbers;
696 int malformed_asm = 0;
697 rtx body = PATTERN (insn);
699 int reg_used_as_output[FIRST_PSEUDO_REGISTER];
700 int implicitly_dies[FIRST_PSEUDO_REGISTER];
701 int alt;
703 rtx *clobber_reg;
704 int n_inputs, n_outputs;
706 /* Find out what the constraints require. If no constraint
707 alternative matches, this asm is malformed. */
708 extract_insn (insn);
709 constrain_operands (1);
710 alt = which_alternative;
712 preprocess_constraints ();
714 n_inputs = get_asm_operand_n_inputs (body);
715 n_outputs = recog_data.n_operands - n_inputs;
717 if (alt < 0)
719 malformed_asm = 1;
720 /* Avoid further trouble with this insn. */
721 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
722 PUT_MODE (insn, VOIDmode);
723 return;
726 /* Strip SUBREGs here to make the following code simpler. */
727 for (i = 0; i < recog_data.n_operands; i++)
728 if (GET_CODE (recog_data.operand[i]) == SUBREG
729 && GET_CODE (SUBREG_REG (recog_data.operand[i])) == REG)
730 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
732 /* Set up CLOBBER_REG. */
734 n_clobbers = 0;
736 if (GET_CODE (body) == PARALLEL)
738 clobber_reg = (rtx *) alloca (XVECLEN (body, 0) * sizeof (rtx));
740 for (i = 0; i < XVECLEN (body, 0); i++)
741 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
743 rtx clobber = XVECEXP (body, 0, i);
744 rtx reg = XEXP (clobber, 0);
746 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
747 reg = SUBREG_REG (reg);
749 if (STACK_REG_P (reg))
751 clobber_reg[n_clobbers] = reg;
752 n_clobbers++;
757 /* Enforce rule #4: Output operands must specifically indicate which
758 reg an output appears in after an asm. "=f" is not allowed: the
759 operand constraints must select a class with a single reg.
761 Also enforce rule #5: Output operands must start at the top of
762 the reg-stack: output operands may not "skip" a reg. */
764 bzero ((char *) reg_used_as_output, sizeof (reg_used_as_output));
765 for (i = 0; i < n_outputs; i++)
766 if (STACK_REG_P (recog_data.operand[i]))
768 if (reg_class_size[(int) recog_op_alt[i][alt].class] != 1)
770 error_for_asm (insn, "Output constraint %d must specify a single register", i);
771 malformed_asm = 1;
773 else
774 reg_used_as_output[REGNO (recog_data.operand[i])] = 1;
778 /* Search for first non-popped reg. */
779 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
780 if (! reg_used_as_output[i])
781 break;
783 /* If there are any other popped regs, that's an error. */
784 for (; i < LAST_STACK_REG + 1; i++)
785 if (reg_used_as_output[i])
786 break;
788 if (i != LAST_STACK_REG + 1)
790 error_for_asm (insn, "Output regs must be grouped at top of stack");
791 malformed_asm = 1;
794 /* Enforce rule #2: All implicitly popped input regs must be closer
795 to the top of the reg-stack than any input that is not implicitly
796 popped. */
798 bzero ((char *) implicitly_dies, sizeof (implicitly_dies));
799 for (i = n_outputs; i < n_outputs + n_inputs; i++)
800 if (STACK_REG_P (recog_data.operand[i]))
802 /* An input reg is implicitly popped if it is tied to an
803 output, or if there is a CLOBBER for it. */
804 int j;
806 for (j = 0; j < n_clobbers; j++)
807 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
808 break;
810 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
811 implicitly_dies[REGNO (recog_data.operand[i])] = 1;
814 /* Search for first non-popped reg. */
815 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
816 if (! implicitly_dies[i])
817 break;
819 /* If there are any other popped regs, that's an error. */
820 for (; i < LAST_STACK_REG + 1; i++)
821 if (implicitly_dies[i])
822 break;
824 if (i != LAST_STACK_REG + 1)
826 error_for_asm (insn,
827 "Implicitly popped regs must be grouped at top of stack");
828 malformed_asm = 1;
831 /* Enfore rule #3: If any input operand uses the "f" constraint, all
832 output constraints must use the "&" earlyclobber.
834 ??? Detect this more deterministically by having constraint_asm_operands
835 record any earlyclobber. */
837 for (i = n_outputs; i < n_outputs + n_inputs; i++)
838 if (recog_op_alt[i][alt].matches == -1)
840 int j;
842 for (j = 0; j < n_outputs; j++)
843 if (operands_match_p (recog_data.operand[j], recog_data.operand[i]))
845 error_for_asm (insn,
846 "Output operand %d must use `&' constraint", j);
847 malformed_asm = 1;
851 if (malformed_asm)
853 /* Avoid further trouble with this insn. */
854 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
855 VARRAY_CHAR (stack_regs_mentioned_data, INSN_UID (insn)) = 2;
856 return;
859 /* Process all outputs */
860 for (i = 0; i < n_outputs; i++)
862 rtx op = recog_data.operand[i];
864 if (! STACK_REG_P (op))
866 if (stack_regs_mentioned_p (op))
867 abort ();
868 else
869 continue;
872 /* Each destination is dead before this insn. If the
873 destination is not used after this insn, record this with
874 REG_UNUSED. */
876 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (op)))
877 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_UNUSED, op,
878 REG_NOTES (insn));
880 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (op));
883 /* Process all inputs */
884 for (i = n_outputs; i < n_outputs + n_inputs; i++)
886 rtx op = recog_data.operand[i];
887 if (! STACK_REG_P (op))
889 if (stack_regs_mentioned_p (op))
890 abort ();
891 else
892 continue;
895 /* If an input is dead after the insn, record a death note.
896 But don't record a death note if there is already a death note,
897 or if the input is also an output. */
899 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (op))
900 && recog_op_alt[i][alt].matches == -1
901 && find_regno_note (insn, REG_DEAD, REGNO (op)) == NULL_RTX)
902 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD, op, REG_NOTES (insn));
904 SET_HARD_REG_BIT (regstack->reg_set, REGNO (op));
908 /* Scan PAT, which is part of INSN, and record registers appearing in
909 a SET_DEST in DEST, and other registers in SRC.
911 This function does not know about SET_DESTs that are both input and
912 output (such as ZERO_EXTRACT) - this cannot happen on a 387. */
914 static void
915 record_reg_life_pat (pat, src, dest, douse)
916 rtx pat;
917 HARD_REG_SET *src, *dest;
918 int douse;
920 register const char *fmt;
921 register int i;
923 if (STACK_REG_P (pat)
924 || (GET_CODE (pat) == SUBREG && STACK_REG_P (SUBREG_REG (pat))))
926 if (src)
927 mark_regs_pat (pat, src);
929 if (dest)
930 mark_regs_pat (pat, dest);
932 return;
935 if (GET_CODE (pat) == SET)
937 record_reg_life_pat (XEXP (pat, 0), NULL_PTR, dest, 0);
938 record_reg_life_pat (XEXP (pat, 1), src, NULL_PTR, 0);
939 return;
942 /* We don't need to consider either of these cases. */
943 if ((GET_CODE (pat) == USE && !douse) || GET_CODE (pat) == CLOBBER)
944 return;
946 fmt = GET_RTX_FORMAT (GET_CODE (pat));
947 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
949 if (fmt[i] == 'E')
951 register int j;
953 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
954 record_reg_life_pat (XVECEXP (pat, i, j), src, dest, 0);
956 else if (fmt[i] == 'e')
957 record_reg_life_pat (XEXP (pat, i), src, dest, 0);
961 /* Calculate the number of inputs and outputs in BODY, an
962 asm_operands. N_OPERANDS is the total number of operands, and
963 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
964 placed. */
966 static int
967 get_asm_operand_n_inputs (body)
968 rtx body;
970 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
971 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body));
973 else if (GET_CODE (body) == ASM_OPERANDS)
974 return ASM_OPERANDS_INPUT_LENGTH (body);
976 else if (GET_CODE (body) == PARALLEL
977 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
978 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (XVECEXP (body, 0, 0)));
980 else if (GET_CODE (body) == PARALLEL
981 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
982 return ASM_OPERANDS_INPUT_LENGTH (XVECEXP (body, 0, 0));
984 abort ();
987 /* Scan INSN, which is in BLOCK, and record the life & death of stack
988 registers in REGSTACK. This function is called to process insns from
989 the last insn in a block to the first. The actual scanning is done in
990 record_reg_life_pat.
992 If a register is live after a CALL_INSN, but is not a value return
993 register for that CALL_INSN, then code is emitted to initialize that
994 register. The block_end[] data is kept accurate.
996 Existing death and unset notes for stack registers are deleted
997 before processing the insn. */
999 static void
1000 record_reg_life (insn, block, regstack)
1001 rtx insn;
1002 int block;
1003 stack regstack;
1005 rtx note, *note_link;
1006 int n_operands;
1008 if ((GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
1009 || INSN_DELETED_P (insn))
1010 return;
1012 /* Strip death notes for stack regs from this insn */
1014 note_link = &REG_NOTES(insn);
1015 for (note = *note_link; note; note = XEXP (note, 1))
1016 if (STACK_REG_P (XEXP (note, 0))
1017 && (REG_NOTE_KIND (note) == REG_DEAD
1018 || REG_NOTE_KIND (note) == REG_UNUSED))
1019 *note_link = XEXP (note, 1);
1020 else
1021 note_link = &XEXP (note, 1);
1023 /* Process all patterns in the insn. */
1025 n_operands = asm_noperands (PATTERN (insn));
1026 if (n_operands >= 0)
1028 record_asm_reg_life (insn, regstack);
1029 return;
1033 HARD_REG_SET src, dest;
1034 int regno;
1036 CLEAR_HARD_REG_SET (src);
1037 CLEAR_HARD_REG_SET (dest);
1039 if (GET_CODE (insn) == CALL_INSN)
1040 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1041 note;
1042 note = XEXP (note, 1))
1043 if (GET_CODE (XEXP (note, 0)) == USE)
1044 record_reg_life_pat (SET_DEST (XEXP (note, 0)), &src, NULL_PTR, 0);
1046 record_reg_life_pat (PATTERN (insn), &src, &dest, 0);
1047 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
1048 if (! TEST_HARD_REG_BIT (regstack->reg_set, regno))
1050 if (TEST_HARD_REG_BIT (src, regno)
1051 && ! TEST_HARD_REG_BIT (dest, regno))
1052 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD,
1053 FP_MODE_REG (regno, DFmode),
1054 REG_NOTES (insn));
1055 else if (TEST_HARD_REG_BIT (dest, regno))
1056 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_UNUSED,
1057 FP_MODE_REG (regno, DFmode),
1058 REG_NOTES (insn));
1061 if (GET_CODE (insn) == CALL_INSN)
1063 int reg;
1065 /* There might be a reg that is live after a function call.
1066 Initialize it to zero so that the program does not crash. See
1067 comment towards the end of stack_reg_life_analysis(). */
1069 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
1070 if (! TEST_HARD_REG_BIT (dest, reg)
1071 && TEST_HARD_REG_BIT (regstack->reg_set, reg))
1073 rtx init, pat;
1075 /* The insn will use virtual register numbers, and so
1076 convert_regs is expected to process these. But BLOCK_NUM
1077 cannot be used on these insns, because they do not appear in
1078 block_number[]. */
1080 pat = gen_rtx_SET (VOIDmode, FP_MODE_REG (reg, DFmode),
1081 CONST0_RTX (DFmode));
1082 init = emit_insn_after (pat, insn);
1084 CLEAR_HARD_REG_BIT (regstack->reg_set, reg);
1086 /* If the CALL_INSN was the end of a block, move the
1087 block_end to point to the new insn. */
1089 if (block_end[block] == insn)
1090 block_end[block] = init;
1093 /* Some regs do not survive a CALL */
1094 AND_COMPL_HARD_REG_SET (regstack->reg_set, call_used_reg_set);
1097 AND_COMPL_HARD_REG_SET (regstack->reg_set, dest);
1098 IOR_HARD_REG_SET (regstack->reg_set, src);
1102 /* Find all basic blocks of the function, which starts with FIRST.
1103 For each JUMP_INSN, build the chain of LABEL_REFS on each CODE_LABEL. */
1105 static void
1106 find_blocks (first)
1107 rtx first;
1109 register rtx insn;
1110 register int block;
1111 register RTX_CODE prev_code = BARRIER;
1112 register RTX_CODE code;
1113 rtx label_value_list = 0;
1115 /* Record where all the blocks start and end.
1116 Record which basic blocks control can drop in to. */
1118 block = -1;
1119 for (insn = first; insn; insn = NEXT_INSN (insn))
1121 /* Note that this loop must select the same block boundaries
1122 as code in reg_to_stack, but that these are not the same
1123 as those selected in flow.c. */
1125 code = GET_CODE (insn);
1127 if (code == CODE_LABEL
1128 || (prev_code != INSN
1129 && prev_code != CALL_INSN
1130 && prev_code != CODE_LABEL
1131 && GET_RTX_CLASS (code) == 'i'))
1133 block_begin[++block] = insn;
1134 block_end[block] = insn;
1135 block_drops_in[block] = prev_code != BARRIER;
1137 else if (GET_RTX_CLASS (code) == 'i')
1138 block_end[block] = insn;
1140 if (GET_RTX_CLASS (code) == 'i')
1142 rtx note;
1144 /* Make a list of all labels referred to other than by jumps. */
1145 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1146 if (REG_NOTE_KIND (note) == REG_LABEL)
1147 label_value_list = gen_rtx_EXPR_LIST (VOIDmode, XEXP (note, 0),
1148 label_value_list);
1151 block_number[INSN_UID (insn)] = block;
1153 if (code != NOTE)
1154 prev_code = code;
1157 if (block + 1 != blocks)
1158 abort ();
1160 /* generate all label references to the corresponding jump insn */
1161 for (block = 0; block < blocks; block++)
1163 insn = block_end[block];
1165 if (GET_CODE (insn) == JUMP_INSN)
1167 rtx pat = PATTERN (insn);
1168 rtx x;
1170 if (computed_jump_p (insn))
1172 for (x = label_value_list; x; x = XEXP (x, 1))
1173 record_label_references (insn,
1174 gen_rtx_LABEL_REF (VOIDmode,
1175 XEXP (x, 0)));
1177 for (x = forced_labels; x; x = XEXP (x, 1))
1178 record_label_references (insn,
1179 gen_rtx_LABEL_REF (VOIDmode,
1180 XEXP (x, 0)));
1183 record_label_references (insn, pat);
1188 /* If current function returns its result in an fp stack register,
1189 return the REG. Otherwise, return 0. */
1191 static rtx
1192 stack_result (decl)
1193 tree decl;
1195 rtx result = DECL_RTL (DECL_RESULT (decl));
1197 if (result != 0
1198 && ! (GET_CODE (result) == REG
1199 && REGNO (result) < FIRST_PSEUDO_REGISTER))
1201 #ifdef FUNCTION_OUTGOING_VALUE
1202 result
1203 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
1204 #else
1205 result = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
1206 #endif
1209 return result != 0 && STACK_REG_P (result) ? result : 0;
1212 /* Determine the which registers are live at the start of each basic
1213 block of the function whose first insn is FIRST.
1215 First, if the function returns a real_type, mark the function
1216 return type as live at each return point, as the RTL may not give any
1217 hint that the register is live.
1219 Then, start with the last block and work back to the first block.
1220 Similarly, work backwards within each block, insn by insn, recording
1221 which regs are dead and which are used (and therefore live) in the
1222 hard reg set of block_stack_in[].
1224 After processing each basic block, if there is a label at the start
1225 of the block, propagate the live registers to all jumps to this block.
1227 As a special case, if there are regs live in this block, that are
1228 not live in a block containing a jump to this label, and the block
1229 containing the jump has already been processed, we must propagate this
1230 block's entry register life back to the block containing the jump, and
1231 restart life analysis from there.
1233 In the worst case, this function may traverse the insns
1234 REG_STACK_SIZE times. This is necessary, since a jump towards the end
1235 of the insns may not know that a reg is live at a target that is early
1236 in the insns. So we back up and start over with the new reg live.
1238 If there are registers that are live at the start of the function,
1239 insns are emitted to initialize these registers. Something similar is
1240 done after CALL_INSNs in record_reg_life. */
1242 static void
1243 stack_reg_life_analysis (first, stackentry)
1244 rtx first;
1245 HARD_REG_SET *stackentry;
1247 int reg, block;
1248 struct stack_def regstack;
1251 rtx retvalue;
1253 if ((retvalue = stack_result (current_function_decl)))
1255 /* Find all RETURN insns and mark them. */
1257 for (block = blocks - 1; --block >= 0;)
1258 if (GET_CODE (block_end[block]) == JUMP_INSN
1259 && returnjump_p (block_end[block]))
1260 mark_regs_pat (retvalue, block_out_reg_set+block);
1262 /* Mark off the end of last block if we "fall off" the end of the
1263 function into the epilogue. */
1265 if (GET_CODE (block_end[blocks-1]) != JUMP_INSN
1266 || GET_CODE (PATTERN (block_end[blocks-1])) == RETURN)
1267 mark_regs_pat (retvalue, block_out_reg_set+blocks-1);
1271 /* now scan all blocks backward for stack register use */
1273 block = blocks - 1;
1274 while (block >= 0)
1276 register rtx insn, prev;
1278 /* current register status at last instruction */
1280 COPY_HARD_REG_SET (regstack.reg_set, block_out_reg_set[block]);
1282 prev = block_end[block];
1285 insn = prev;
1286 prev = PREV_INSN (insn);
1288 /* If the insn is a CALL_INSN, we need to ensure that
1289 everything dies. But otherwise don't process unless there
1290 are some stack regs present. */
1292 if (stack_regs_mentioned (insn) || GET_CODE (insn) == CALL_INSN)
1293 record_reg_life (insn, block, &regstack);
1295 } while (insn != block_begin[block]);
1297 /* Set the state at the start of the block. Mark that no
1298 register mapping information known yet. */
1300 COPY_HARD_REG_SET (block_stack_in[block].reg_set, regstack.reg_set);
1301 block_stack_in[block].top = -2;
1303 /* If there is a label, propagate our register life to all jumps
1304 to this label. */
1306 if (GET_CODE (insn) == CODE_LABEL)
1308 register rtx label;
1309 int must_restart = 0;
1311 for (label = LABEL_REFS (insn); label != insn;
1312 label = LABEL_NEXTREF (label))
1314 int jump_block = BLOCK_NUM (CONTAINING_INSN (label));
1316 if (jump_block < block)
1317 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
1318 block_stack_in[block].reg_set);
1319 else
1321 /* The block containing the jump has already been
1322 processed. If there are registers that were not known
1323 to be live then, but are live now, we must back up
1324 and restart life analysis from that point with the new
1325 life information. */
1327 GO_IF_HARD_REG_SUBSET (block_stack_in[block].reg_set,
1328 block_out_reg_set[jump_block],
1329 win);
1331 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
1332 block_stack_in[block].reg_set);
1334 block = jump_block;
1335 must_restart = 1;
1336 break;
1338 win:
1342 if (must_restart)
1343 continue;
1346 if (block_drops_in[block])
1347 IOR_HARD_REG_SET (block_out_reg_set[block-1],
1348 block_stack_in[block].reg_set);
1350 block -= 1;
1353 /* If any reg is live at the start of the first block of a
1354 function, then we must guarantee that the reg holds some value by
1355 generating our own "load" of that register. Otherwise a 387 would
1356 fault trying to access an empty register. */
1358 /* Load zero into each live register. The fact that a register
1359 appears live at the function start necessarily implies an error
1360 in the user program: it means that (unless the offending code is *never*
1361 executed) this program is using uninitialised floating point
1362 variables. In order to keep broken code like this happy, we initialise
1363 those variables with zero.
1365 Note that we are inserting virtual register references here:
1366 these insns must be processed by convert_regs later. Also, these
1367 insns will not be in block_number, so BLOCK_NUM() will fail for them. */
1369 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
1370 if (TEST_HARD_REG_BIT (block_stack_in[0].reg_set, reg)
1371 && ! TEST_HARD_REG_BIT (*stackentry, reg))
1373 rtx init_rtx;
1375 init_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG(reg, DFmode),
1376 CONST0_RTX (DFmode));
1377 block_begin[0] = emit_insn_after (init_rtx, first);
1379 CLEAR_HARD_REG_BIT (block_stack_in[0].reg_set, reg);
1384 * This section deals with stack register substitution, and forms the second
1385 * pass over the RTL.
1388 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
1389 the desired hard REGNO. */
1391 static void
1392 replace_reg (reg, regno)
1393 rtx *reg;
1394 int regno;
1396 if (regno < FIRST_STACK_REG || regno > LAST_STACK_REG
1397 || ! STACK_REG_P (*reg))
1398 abort ();
1400 switch (GET_MODE_CLASS (GET_MODE (*reg)))
1402 default: abort ();
1403 case MODE_FLOAT:
1404 case MODE_COMPLEX_FLOAT:;
1407 *reg = FP_MODE_REG (regno, GET_MODE (*reg));
1410 /* Remove a note of type NOTE, which must be found, for register
1411 number REGNO from INSN. Remove only one such note. */
1413 static void
1414 remove_regno_note (insn, note, regno)
1415 rtx insn;
1416 enum reg_note note;
1417 int regno;
1419 register rtx *note_link, this;
1421 note_link = &REG_NOTES(insn);
1422 for (this = *note_link; this; this = XEXP (this, 1))
1423 if (REG_NOTE_KIND (this) == note
1424 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno)
1426 *note_link = XEXP (this, 1);
1427 return;
1429 else
1430 note_link = &XEXP (this, 1);
1432 abort ();
1435 /* Find the hard register number of virtual register REG in REGSTACK.
1436 The hard register number is relative to the top of the stack. -1 is
1437 returned if the register is not found. */
1439 static int
1440 get_hard_regnum (regstack, reg)
1441 stack regstack;
1442 rtx reg;
1444 int i;
1446 if (! STACK_REG_P (reg))
1447 abort ();
1449 for (i = regstack->top; i >= 0; i--)
1450 if (regstack->reg[i] == REGNO (reg))
1451 break;
1453 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
1456 /* Delete INSN from the RTL. Mark the insn, but don't remove it from
1457 the chain of insns. Doing so could confuse block_begin and block_end
1458 if this were the only insn in the block. */
1460 static void
1461 delete_insn_for_stacker (insn)
1462 rtx insn;
1464 PUT_CODE (insn, NOTE);
1465 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1466 NOTE_SOURCE_FILE (insn) = 0;
1469 /* Emit an insn to pop virtual register REG before or after INSN.
1470 REGSTACK is the stack state after INSN and is updated to reflect this
1471 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
1472 is represented as a SET whose destination is the register to be popped
1473 and source is the top of stack. A death note for the top of stack
1474 cases the movdf pattern to pop. */
1476 static rtx
1477 emit_pop_insn (insn, regstack, reg, when)
1478 rtx insn;
1479 stack regstack;
1480 rtx reg;
1481 rtx (*when)();
1483 rtx pop_insn, pop_rtx;
1484 int hard_regno;
1486 hard_regno = get_hard_regnum (regstack, reg);
1488 if (hard_regno < FIRST_STACK_REG)
1489 abort ();
1491 pop_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG (hard_regno, DFmode),
1492 FP_MODE_REG (FIRST_STACK_REG, DFmode));
1494 pop_insn = (*when) (pop_rtx, insn);
1496 REG_NOTES (pop_insn)
1497 = gen_rtx_EXPR_LIST (REG_DEAD, FP_MODE_REG (FIRST_STACK_REG, DFmode),
1498 REG_NOTES (pop_insn));
1500 regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
1501 = regstack->reg[regstack->top];
1502 regstack->top -= 1;
1503 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
1505 return pop_insn;
1508 /* Emit an insn before or after INSN to swap virtual register REG with the
1509 top of stack. WHEN should be `emit_insn_before' or `emit_insn_before'
1510 REGSTACK is the stack state before the swap, and is updated to reflect
1511 the swap. A swap insn is represented as a PARALLEL of two patterns:
1512 each pattern moves one reg to the other.
1514 If REG is already at the top of the stack, no insn is emitted. */
1516 static void
1517 emit_swap_insn (insn, regstack, reg)
1518 rtx insn;
1519 stack regstack;
1520 rtx reg;
1522 int hard_regno;
1523 rtx swap_rtx, swap_insn;
1524 int tmp, other_reg; /* swap regno temps */
1525 rtx i1; /* the stack-reg insn prior to INSN */
1526 rtx i1set = NULL_RTX; /* the SET rtx within I1 */
1528 hard_regno = get_hard_regnum (regstack, reg);
1530 if (hard_regno < FIRST_STACK_REG)
1531 abort ();
1532 if (hard_regno == FIRST_STACK_REG)
1533 return;
1535 other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
1537 tmp = regstack->reg[other_reg];
1538 regstack->reg[other_reg] = regstack->reg[regstack->top];
1539 regstack->reg[regstack->top] = tmp;
1541 /* Find the previous insn involving stack regs, but don't go past
1542 any labels, calls or jumps. */
1543 i1 = prev_nonnote_insn (insn);
1544 while (i1 && GET_CODE (i1) == INSN && !stack_regs_mentioned (i1))
1545 i1 = prev_nonnote_insn (i1);
1547 if (i1)
1548 i1set = single_set (i1);
1550 if (i1set)
1552 rtx i1src = *get_true_reg (&SET_SRC (i1set));
1553 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
1555 /* If the previous register stack push was from the reg we are to
1556 swap with, omit the swap. */
1558 if (GET_CODE (i1dest) == REG && REGNO (i1dest) == FIRST_STACK_REG
1559 && GET_CODE (i1src) == REG && REGNO (i1src) == hard_regno - 1
1560 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
1561 return;
1563 /* If the previous insn wrote to the reg we are to swap with,
1564 omit the swap. */
1566 if (GET_CODE (i1dest) == REG && REGNO (i1dest) == hard_regno
1567 && GET_CODE (i1src) == REG && REGNO (i1src) == FIRST_STACK_REG
1568 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
1569 return;
1572 swap_rtx = gen_swapxf (FP_MODE_REG (hard_regno, XFmode),
1573 FP_MODE_REG (FIRST_STACK_REG, XFmode));
1574 swap_insn = emit_insn_after (swap_rtx, i1);
1577 /* Handle a move to or from a stack register in PAT, which is in INSN.
1578 REGSTACK is the current stack. */
1580 static void
1581 move_for_stack_reg (insn, regstack, pat)
1582 rtx insn;
1583 stack regstack;
1584 rtx pat;
1586 rtx *psrc = get_true_reg (&SET_SRC (pat));
1587 rtx *pdest = get_true_reg (&SET_DEST (pat));
1588 rtx src, dest;
1589 rtx note;
1591 src = *psrc; dest = *pdest;
1593 if (STACK_REG_P (src) && STACK_REG_P (dest))
1595 /* Write from one stack reg to another. If SRC dies here, then
1596 just change the register mapping and delete the insn. */
1598 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1599 if (note)
1601 int i;
1603 /* If this is a no-op move, there must not be a REG_DEAD note. */
1604 if (REGNO (src) == REGNO (dest))
1605 abort ();
1607 for (i = regstack->top; i >= 0; i--)
1608 if (regstack->reg[i] == REGNO (src))
1609 break;
1611 /* The source must be live, and the dest must be dead. */
1612 if (i < 0 || get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1613 abort ();
1615 /* It is possible that the dest is unused after this insn.
1616 If so, just pop the src. */
1618 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1620 emit_pop_insn (insn, regstack, src, emit_insn_after);
1622 delete_insn_for_stacker (insn);
1623 return;
1626 regstack->reg[i] = REGNO (dest);
1628 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1629 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1631 delete_insn_for_stacker (insn);
1633 return;
1636 /* The source reg does not die. */
1638 /* If this appears to be a no-op move, delete it, or else it
1639 will confuse the machine description output patterns. But if
1640 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1641 for REG_UNUSED will not work for deleted insns. */
1643 if (REGNO (src) == REGNO (dest))
1645 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1646 emit_pop_insn (insn, regstack, dest, emit_insn_after);
1648 delete_insn_for_stacker (insn);
1649 return;
1652 /* The destination ought to be dead */
1653 if (get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1654 abort ();
1656 replace_reg (psrc, get_hard_regnum (regstack, src));
1658 regstack->reg[++regstack->top] = REGNO (dest);
1659 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1660 replace_reg (pdest, FIRST_STACK_REG);
1662 else if (STACK_REG_P (src))
1664 /* Save from a stack reg to MEM, or possibly integer reg. Since
1665 only top of stack may be saved, emit an exchange first if
1666 needs be. */
1668 emit_swap_insn (insn, regstack, src);
1670 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1671 if (note)
1673 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1674 regstack->top--;
1675 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1677 else if (GET_MODE (src) == XFmode && regstack->top < REG_STACK_SIZE - 1)
1679 /* A 387 cannot write an XFmode value to a MEM without
1680 clobbering the source reg. The output code can handle
1681 this by reading back the value from the MEM.
1682 But it is more efficient to use a temp register if one is
1683 available. Push the source value here if the register
1684 stack is not full, and then write the value to memory via
1685 a pop. */
1686 rtx push_rtx, push_insn;
1687 rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, XFmode);
1689 push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
1690 push_insn = emit_insn_before (push_rtx, insn);
1691 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD, top_stack_reg,
1692 REG_NOTES (insn));
1695 replace_reg (psrc, FIRST_STACK_REG);
1697 else if (STACK_REG_P (dest))
1699 /* Load from MEM, or possibly integer REG or constant, into the
1700 stack regs. The actual target is always the top of the
1701 stack. The stack mapping is changed to reflect that DEST is
1702 now at top of stack. */
1704 /* The destination ought to be dead */
1705 if (get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1706 abort ();
1708 if (regstack->top >= REG_STACK_SIZE)
1709 abort ();
1711 regstack->reg[++regstack->top] = REGNO (dest);
1712 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1713 replace_reg (pdest, FIRST_STACK_REG);
1715 else
1716 abort ();
1719 /* Swap the condition on a branch, if there is one. Return true if we
1720 found a condition to swap. False if the condition was not used as
1721 such. */
1723 static int
1724 swap_rtx_condition_1 (pat)
1725 rtx pat;
1727 register const char *fmt;
1728 register int i, r = 0;
1730 if (GET_RTX_CLASS (GET_CODE (pat)) == '<')
1732 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1733 r = 1;
1735 else
1737 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1738 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1740 if (fmt[i] == 'E')
1742 register int j;
1744 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1745 r |= swap_rtx_condition_1 (XVECEXP (pat, i, j));
1747 else if (fmt[i] == 'e')
1748 r |= swap_rtx_condition_1 (XEXP (pat, i));
1752 return r;
1755 static int
1756 swap_rtx_condition (insn)
1757 rtx insn;
1759 rtx pat = PATTERN (insn);
1761 /* We're looking for a single set to cc0 or an HImode temporary. */
1763 if (GET_CODE (pat) == SET
1764 && GET_CODE (SET_DEST (pat)) == REG
1765 && REGNO (SET_DEST (pat)) == FLAGS_REG)
1767 insn = next_flags_user (insn);
1768 if (insn == NULL_RTX)
1769 return 0;
1770 pat = PATTERN (insn);
1773 /* See if this is, or ends in, a fnstsw, aka unspec 9. If so, we're
1774 not doing anything with the cc value right now. We may be able to
1775 search for one though. */
1777 if (GET_CODE (pat) == SET
1778 && GET_CODE (SET_SRC (pat)) == UNSPEC
1779 && XINT (SET_SRC (pat), 1) == 9)
1781 rtx dest = SET_DEST (pat);
1783 /* Search forward looking for the first use of this value.
1784 Stop at block boundaries. */
1785 /* ??? This really cries for BLOCK_END! */
1786 while (1)
1788 insn = NEXT_INSN (insn);
1789 if (insn == NULL_RTX)
1790 return 0;
1791 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1792 && reg_mentioned_p (dest, insn))
1793 break;
1794 if (GET_CODE (insn) == JUMP_INSN)
1795 return 0;
1796 if (GET_CODE (insn) == CODE_LABEL)
1797 return 0;
1800 /* So we've found the insn using this value. If it is anything
1801 other than sahf, aka unspec 10, or the value does not die
1802 (meaning we'd have to search further), then we must give up. */
1803 pat = PATTERN (insn);
1804 if (GET_CODE (pat) != SET
1805 || GET_CODE (SET_SRC (pat)) != UNSPEC
1806 || XINT (SET_SRC (pat), 1) != 10
1807 || ! dead_or_set_p (insn, dest))
1808 return 0;
1810 /* Now we are prepared to handle this as a normal cc0 setter. */
1811 insn = next_flags_user (insn);
1812 if (insn == NULL_RTX)
1813 return 0;
1814 pat = PATTERN (insn);
1817 return swap_rtx_condition_1 (pat);
1820 /* Handle a comparison. Special care needs to be taken to avoid
1821 causing comparisons that a 387 cannot do correctly, such as EQ.
1823 Also, a pop insn may need to be emitted. The 387 does have an
1824 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1825 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1826 set up. */
1828 static void
1829 compare_for_stack_reg (insn, regstack, pat_src)
1830 rtx insn;
1831 stack regstack;
1832 rtx pat_src;
1834 rtx *src1, *src2;
1835 rtx src1_note, src2_note;
1836 rtx flags_user;
1838 src1 = get_true_reg (&XEXP (pat_src, 0));
1839 src2 = get_true_reg (&XEXP (pat_src, 1));
1840 flags_user = next_flags_user (insn);
1842 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1843 registers that die in this insn - move those to stack top first. */
1844 if ((! STACK_REG_P (*src1)
1845 || (STACK_REG_P (*src2)
1846 && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
1847 && swap_rtx_condition (insn))
1849 rtx temp;
1850 temp = XEXP (pat_src, 0);
1851 XEXP (pat_src, 0) = XEXP (pat_src, 1);
1852 XEXP (pat_src, 1) = temp;
1854 src1 = get_true_reg (&XEXP (pat_src, 0));
1855 src2 = get_true_reg (&XEXP (pat_src, 1));
1857 INSN_CODE (insn) = -1;
1860 /* We will fix any death note later. */
1862 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1864 if (STACK_REG_P (*src2))
1865 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1866 else
1867 src2_note = NULL_RTX;
1869 emit_swap_insn (insn, regstack, *src1);
1871 replace_reg (src1, FIRST_STACK_REG);
1873 if (STACK_REG_P (*src2))
1874 replace_reg (src2, get_hard_regnum (regstack, *src2));
1876 if (src1_note)
1878 pop_stack (regstack, REGNO (XEXP (src1_note, 0)));
1879 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1882 /* If the second operand dies, handle that. But if the operands are
1883 the same stack register, don't bother, because only one death is
1884 needed, and it was just handled. */
1886 if (src2_note
1887 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
1888 && REGNO (*src1) == REGNO (*src2)))
1890 /* As a special case, two regs may die in this insn if src2 is
1891 next to top of stack and the top of stack also dies. Since
1892 we have already popped src1, "next to top of stack" is really
1893 at top (FIRST_STACK_REG) now. */
1895 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1896 && src1_note)
1898 pop_stack (regstack, REGNO (XEXP (src2_note, 0)));
1899 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1901 else
1903 /* The 386 can only represent death of the first operand in
1904 the case handled above. In all other cases, emit a separate
1905 pop and remove the death note from here. */
1907 /* link_cc0_insns (insn); */
1909 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
1911 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
1912 emit_insn_after);
1917 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1918 is the current register layout. */
1920 static void
1921 subst_stack_regs_pat (insn, regstack, pat)
1922 rtx insn;
1923 stack regstack;
1924 rtx pat;
1926 rtx *dest, *src;
1927 rtx *src1 = (rtx *) NULL_PTR, *src2;
1928 rtx src1_note, src2_note;
1929 rtx pat_src;
1931 if (GET_CODE (pat) != SET)
1932 return;
1934 dest = get_true_reg (&SET_DEST (pat));
1935 src = get_true_reg (&SET_SRC (pat));
1936 pat_src = SET_SRC (pat);
1938 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1940 if (*dest != cc0_rtx
1941 && (STACK_REG_P (*src)
1942 || (STACK_REG_P (*dest)
1943 && (GET_CODE (*src) == REG || GET_CODE (*src) == MEM
1944 || GET_CODE (*src) == CONST_DOUBLE))))
1945 move_for_stack_reg (insn, regstack, pat);
1946 else
1947 switch (GET_CODE (pat_src))
1949 case COMPARE:
1950 compare_for_stack_reg (insn, regstack, pat_src);
1951 break;
1953 case CALL:
1955 int count;
1956 for (count = HARD_REGNO_NREGS (REGNO (*dest), GET_MODE (*dest));
1957 --count >= 0;)
1959 regstack->reg[++regstack->top] = REGNO (*dest) + count;
1960 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
1963 replace_reg (dest, FIRST_STACK_REG);
1964 break;
1966 case REG:
1967 /* This is a `tstM2' case. */
1968 if (*dest != cc0_rtx)
1969 abort ();
1971 src1 = src;
1973 /* Fall through. */
1975 case FLOAT_TRUNCATE:
1976 case SQRT:
1977 case ABS:
1978 case NEG:
1979 /* These insns only operate on the top of the stack. DEST might
1980 be cc0_rtx if we're processing a tstM pattern. Also, it's
1981 possible that the tstM case results in a REG_DEAD note on the
1982 source. */
1984 if (src1 == 0)
1985 src1 = get_true_reg (&XEXP (pat_src, 0));
1987 emit_swap_insn (insn, regstack, *src1);
1989 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1991 if (STACK_REG_P (*dest))
1992 replace_reg (dest, FIRST_STACK_REG);
1994 if (src1_note)
1996 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1997 regstack->top--;
1998 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
2001 replace_reg (src1, FIRST_STACK_REG);
2003 break;
2005 case MINUS:
2006 case DIV:
2007 /* On i386, reversed forms of subM3 and divM3 exist for
2008 MODE_FLOAT, so the same code that works for addM3 and mulM3
2009 can be used. */
2010 case MULT:
2011 case PLUS:
2012 /* These insns can accept the top of stack as a destination
2013 from a stack reg or mem, or can use the top of stack as a
2014 source and some other stack register (possibly top of stack)
2015 as a destination. */
2017 src1 = get_true_reg (&XEXP (pat_src, 0));
2018 src2 = get_true_reg (&XEXP (pat_src, 1));
2020 /* We will fix any death note later. */
2022 if (STACK_REG_P (*src1))
2023 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2024 else
2025 src1_note = NULL_RTX;
2026 if (STACK_REG_P (*src2))
2027 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
2028 else
2029 src2_note = NULL_RTX;
2031 /* If either operand is not a stack register, then the dest
2032 must be top of stack. */
2034 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
2035 emit_swap_insn (insn, regstack, *dest);
2036 else
2038 /* Both operands are REG. If neither operand is already
2039 at the top of stack, choose to make the one that is the dest
2040 the new top of stack. */
2042 int src1_hard_regnum, src2_hard_regnum;
2044 src1_hard_regnum = get_hard_regnum (regstack, *src1);
2045 src2_hard_regnum = get_hard_regnum (regstack, *src2);
2046 if (src1_hard_regnum == -1 || src2_hard_regnum == -1)
2047 abort ();
2049 if (src1_hard_regnum != FIRST_STACK_REG
2050 && src2_hard_regnum != FIRST_STACK_REG)
2051 emit_swap_insn (insn, regstack, *dest);
2054 if (STACK_REG_P (*src1))
2055 replace_reg (src1, get_hard_regnum (regstack, *src1));
2056 if (STACK_REG_P (*src2))
2057 replace_reg (src2, get_hard_regnum (regstack, *src2));
2059 if (src1_note)
2061 /* If the register that dies is at the top of stack, then
2062 the destination is somewhere else - merely substitute it.
2063 But if the reg that dies is not at top of stack, then
2064 move the top of stack to the dead reg, as though we had
2065 done the insn and then a store-with-pop. */
2067 if (REGNO (XEXP (src1_note, 0)) == regstack->reg[regstack->top])
2069 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2070 replace_reg (dest, get_hard_regnum (regstack, *dest));
2072 else
2074 int regno = get_hard_regnum (regstack, XEXP (src1_note, 0));
2076 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2077 replace_reg (dest, regno);
2079 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
2080 = regstack->reg[regstack->top];
2083 CLEAR_HARD_REG_BIT (regstack->reg_set,
2084 REGNO (XEXP (src1_note, 0)));
2085 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2086 regstack->top--;
2088 else if (src2_note)
2090 if (REGNO (XEXP (src2_note, 0)) == regstack->reg[regstack->top])
2092 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2093 replace_reg (dest, get_hard_regnum (regstack, *dest));
2095 else
2097 int regno = get_hard_regnum (regstack, XEXP (src2_note, 0));
2099 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2100 replace_reg (dest, regno);
2102 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
2103 = regstack->reg[regstack->top];
2106 CLEAR_HARD_REG_BIT (regstack->reg_set,
2107 REGNO (XEXP (src2_note, 0)));
2108 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
2109 regstack->top--;
2111 else
2113 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2114 replace_reg (dest, get_hard_regnum (regstack, *dest));
2117 break;
2119 case UNSPEC:
2120 switch (XINT (pat_src, 1))
2122 case 1: /* sin */
2123 case 2: /* cos */
2124 /* These insns only operate on the top of the stack. */
2126 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
2128 emit_swap_insn (insn, regstack, *src1);
2130 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2132 if (STACK_REG_P (*dest))
2133 replace_reg (dest, FIRST_STACK_REG);
2135 if (src1_note)
2137 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2138 regstack->top--;
2139 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
2142 replace_reg (src1, FIRST_STACK_REG);
2144 break;
2146 case 10:
2147 /* (unspec [(unspec [(compare ..)] 9)] 10)
2148 Unspec 9 is fnstsw; unspec 10 is sahf. The combination
2149 matches the PPRO fcomi instruction. */
2151 pat_src = XVECEXP (pat_src, 0, 0);
2152 if (GET_CODE (pat_src) != UNSPEC
2153 || XINT (pat_src, 1) != 9)
2154 abort ();
2155 /* FALLTHRU */
2157 case 9:
2158 /* (unspec [(compare ..)] 9)
2159 Combined fcomp+fnstsw generated for doing well with CSE.
2160 When optimizing this would have been broken up before now. */
2162 pat_src = XVECEXP (pat_src, 0, 0);
2163 if (GET_CODE (pat_src) != COMPARE)
2164 abort ();
2166 compare_for_stack_reg (insn, regstack, pat_src);
2167 break;
2169 default:
2170 abort ();
2172 break;
2174 case IF_THEN_ELSE:
2175 /* This insn requires the top of stack to be the destination. */
2177 /* If the comparison operator is an FP comparison operator,
2178 it is handled correctly by compare_for_stack_reg () who
2179 will move the destination to the top of stack. But if the
2180 comparison operator is not an FP comparison operator, we
2181 have to handle it here. */
2182 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
2183 && REGNO (*dest) != regstack->reg[regstack->top])
2184 emit_swap_insn (insn, regstack, *dest);
2186 src1 = get_true_reg (&XEXP (pat_src, 1));
2187 src2 = get_true_reg (&XEXP (pat_src, 2));
2189 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2190 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
2193 rtx src_note [3];
2194 int i;
2196 src_note[0] = 0;
2197 src_note[1] = src1_note;
2198 src_note[2] = src2_note;
2200 if (STACK_REG_P (*src1))
2201 replace_reg (src1, get_hard_regnum (regstack, *src1));
2202 if (STACK_REG_P (*src2))
2203 replace_reg (src2, get_hard_regnum (regstack, *src2));
2205 for (i = 1; i <= 2; i++)
2206 if (src_note [i])
2208 /* If the register that dies is not at the top of stack, then
2209 move the top of stack to the dead reg */
2210 if (REGNO (XEXP (src_note[i], 0))
2211 != regstack->reg[regstack->top])
2213 remove_regno_note (insn, REG_DEAD,
2214 REGNO (XEXP (src_note [i], 0)));
2215 emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
2216 emit_insn_after);
2218 else
2220 CLEAR_HARD_REG_BIT (regstack->reg_set,
2221 REGNO (XEXP (src_note[i], 0)));
2222 replace_reg (&XEXP (src_note[i], 0), FIRST_STACK_REG);
2223 regstack->top--;
2228 /* Make dest the top of stack. Add dest to regstack if not present. */
2229 if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
2230 regstack->reg[++regstack->top] = REGNO (*dest);
2231 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2232 replace_reg (dest, FIRST_STACK_REG);
2234 break;
2236 default:
2237 abort ();
2241 /* Substitute hard regnums for any stack regs in INSN, which has
2242 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
2243 before the insn, and is updated with changes made here.
2245 There are several requirements and assumptions about the use of
2246 stack-like regs in asm statements. These rules are enforced by
2247 record_asm_stack_regs; see comments there for details. Any
2248 asm_operands left in the RTL at this point may be assume to meet the
2249 requirements, since record_asm_stack_regs removes any problem asm. */
2251 static void
2252 subst_asm_stack_regs (insn, regstack)
2253 rtx insn;
2254 stack regstack;
2256 rtx body = PATTERN (insn);
2257 int alt;
2259 rtx *note_reg; /* Array of note contents */
2260 rtx **note_loc; /* Address of REG field of each note */
2261 enum reg_note *note_kind; /* The type of each note */
2263 rtx *clobber_reg;
2264 rtx **clobber_loc;
2266 struct stack_def temp_stack;
2267 int n_notes;
2268 int n_clobbers;
2269 rtx note;
2270 int i;
2271 int n_inputs, n_outputs;
2273 /* Find out what the constraints required. If no constraint
2274 alternative matches, that is a compiler bug: we should have caught
2275 such an insn during the life analysis pass (and reload should have
2276 caught it regardless). */
2277 extract_insn (insn);
2278 constrain_operands (1);
2279 alt = which_alternative;
2281 preprocess_constraints ();
2283 n_inputs = get_asm_operand_n_inputs (body);
2284 n_outputs = recog_data.n_operands - n_inputs;
2286 if (alt < 0)
2287 abort ();
2289 /* Strip SUBREGs here to make the following code simpler. */
2290 for (i = 0; i < recog_data.n_operands; i++)
2291 if (GET_CODE (recog_data.operand[i]) == SUBREG
2292 && GET_CODE (SUBREG_REG (recog_data.operand[i])) == REG)
2294 recog_data.operand_loc[i] = & SUBREG_REG (recog_data.operand[i]);
2295 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
2298 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
2300 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
2301 i++;
2303 note_reg = (rtx *) alloca (i * sizeof (rtx));
2304 note_loc = (rtx **) alloca (i * sizeof (rtx *));
2305 note_kind = (enum reg_note *) alloca (i * sizeof (enum reg_note));
2307 n_notes = 0;
2308 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2310 rtx reg = XEXP (note, 0);
2311 rtx *loc = & XEXP (note, 0);
2313 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
2315 loc = & SUBREG_REG (reg);
2316 reg = SUBREG_REG (reg);
2319 if (STACK_REG_P (reg)
2320 && (REG_NOTE_KIND (note) == REG_DEAD
2321 || REG_NOTE_KIND (note) == REG_UNUSED))
2323 note_reg[n_notes] = reg;
2324 note_loc[n_notes] = loc;
2325 note_kind[n_notes] = REG_NOTE_KIND (note);
2326 n_notes++;
2330 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2332 n_clobbers = 0;
2334 if (GET_CODE (body) == PARALLEL)
2336 clobber_reg = (rtx *) alloca (XVECLEN (body, 0) * sizeof (rtx));
2337 clobber_loc = (rtx **) alloca (XVECLEN (body, 0) * sizeof (rtx *));
2339 for (i = 0; i < XVECLEN (body, 0); i++)
2340 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
2342 rtx clobber = XVECEXP (body, 0, i);
2343 rtx reg = XEXP (clobber, 0);
2344 rtx *loc = & XEXP (clobber, 0);
2346 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
2348 loc = & SUBREG_REG (reg);
2349 reg = SUBREG_REG (reg);
2352 if (STACK_REG_P (reg))
2354 clobber_reg[n_clobbers] = reg;
2355 clobber_loc[n_clobbers] = loc;
2356 n_clobbers++;
2361 bcopy ((char *) regstack, (char *) &temp_stack, sizeof (temp_stack));
2363 /* Put the input regs into the desired place in TEMP_STACK. */
2365 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2366 if (STACK_REG_P (recog_data.operand[i])
2367 && reg_class_subset_p (recog_op_alt[i][alt].class,
2368 FLOAT_REGS)
2369 && recog_op_alt[i][alt].class != FLOAT_REGS)
2371 /* If an operand needs to be in a particular reg in
2372 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2373 these constraints are for single register classes, and
2374 reload guaranteed that operand[i] is already in that class,
2375 we can just use REGNO (recog_data.operand[i]) to know which
2376 actual reg this operand needs to be in. */
2378 int regno = get_hard_regnum (&temp_stack, recog_data.operand[i]);
2380 if (regno < 0)
2381 abort ();
2383 if (regno != REGNO (recog_data.operand[i]))
2385 /* recog_data.operand[i] is not in the right place. Find
2386 it and swap it with whatever is already in I's place.
2387 K is where recog_data.operand[i] is now. J is where it
2388 should be. */
2389 int j, k, temp;
2391 k = temp_stack.top - (regno - FIRST_STACK_REG);
2392 j = (temp_stack.top
2393 - (REGNO (recog_data.operand[i]) - FIRST_STACK_REG));
2395 temp = temp_stack.reg[k];
2396 temp_stack.reg[k] = temp_stack.reg[j];
2397 temp_stack.reg[j] = temp;
2401 /* emit insns before INSN to make sure the reg-stack is in the right
2402 order. */
2404 change_stack (insn, regstack, &temp_stack, emit_insn_before);
2406 /* Make the needed input register substitutions. Do death notes and
2407 clobbers too, because these are for inputs, not outputs. */
2409 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2410 if (STACK_REG_P (recog_data.operand[i]))
2412 int regnum = get_hard_regnum (regstack, recog_data.operand[i]);
2414 if (regnum < 0)
2415 abort ();
2417 replace_reg (recog_data.operand_loc[i], regnum);
2420 for (i = 0; i < n_notes; i++)
2421 if (note_kind[i] == REG_DEAD)
2423 int regnum = get_hard_regnum (regstack, note_reg[i]);
2425 if (regnum < 0)
2426 abort ();
2428 replace_reg (note_loc[i], regnum);
2431 for (i = 0; i < n_clobbers; i++)
2433 /* It's OK for a CLOBBER to reference a reg that is not live.
2434 Don't try to replace it in that case. */
2435 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2437 if (regnum >= 0)
2439 /* Sigh - clobbers always have QImode. But replace_reg knows
2440 that these regs can't be MODE_INT and will abort. Just put
2441 the right reg there without calling replace_reg. */
2443 *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
2447 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2449 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2450 if (STACK_REG_P (recog_data.operand[i]))
2452 /* An input reg is implicitly popped if it is tied to an
2453 output, or if there is a CLOBBER for it. */
2454 int j;
2456 for (j = 0; j < n_clobbers; j++)
2457 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
2458 break;
2460 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
2462 /* recog_data.operand[i] might not be at the top of stack.
2463 But that's OK, because all we need to do is pop the
2464 right number of regs off of the top of the reg-stack.
2465 record_asm_stack_regs guaranteed that all implicitly
2466 popped regs were grouped at the top of the reg-stack. */
2468 CLEAR_HARD_REG_BIT (regstack->reg_set,
2469 regstack->reg[regstack->top]);
2470 regstack->top--;
2474 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2475 Note that there isn't any need to substitute register numbers.
2476 ??? Explain why this is true. */
2478 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2480 /* See if there is an output for this hard reg. */
2481 int j;
2483 for (j = 0; j < n_outputs; j++)
2484 if (STACK_REG_P (recog_data.operand[j])
2485 && REGNO (recog_data.operand[j]) == i)
2487 regstack->reg[++regstack->top] = i;
2488 SET_HARD_REG_BIT (regstack->reg_set, i);
2489 break;
2493 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2494 input that the asm didn't implicitly pop. If the asm didn't
2495 implicitly pop an input reg, that reg will still be live.
2497 Note that we can't use find_regno_note here: the register numbers
2498 in the death notes have already been substituted. */
2500 for (i = 0; i < n_outputs; i++)
2501 if (STACK_REG_P (recog_data.operand[i]))
2503 int j;
2505 for (j = 0; j < n_notes; j++)
2506 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2507 && note_kind[j] == REG_UNUSED)
2509 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2510 emit_insn_after);
2511 break;
2515 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2516 if (STACK_REG_P (recog_data.operand[i]))
2518 int j;
2520 for (j = 0; j < n_notes; j++)
2521 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2522 && note_kind[j] == REG_DEAD
2523 && TEST_HARD_REG_BIT (regstack->reg_set,
2524 REGNO (recog_data.operand[i])))
2526 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2527 emit_insn_after);
2528 break;
2533 /* Substitute stack hard reg numbers for stack virtual registers in
2534 INSN. Non-stack register numbers are not changed. REGSTACK is the
2535 current stack content. Insns may be emitted as needed to arrange the
2536 stack for the 387 based on the contents of the insn. */
2538 static void
2539 subst_stack_regs (insn, regstack)
2540 rtx insn;
2541 stack regstack;
2543 register rtx *note_link, note;
2544 register int i;
2546 if (GET_CODE (insn) == CALL_INSN)
2548 int top = regstack->top;
2550 /* If there are any floating point parameters to be passed in
2551 registers for this call, make sure they are in the right
2552 order. */
2554 if (top >= 0)
2556 straighten_stack (PREV_INSN (insn), regstack);
2558 /* Now mark the arguments as dead after the call. */
2560 while (regstack->top >= 0)
2562 CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
2563 regstack->top--;
2568 /* Do the actual substitution if any stack regs are mentioned.
2569 Since we only record whether entire insn mentions stack regs, and
2570 subst_stack_regs_pat only works for patterns that contain stack regs,
2571 we must check each pattern in a parallel here. A call_value_pop could
2572 fail otherwise. */
2574 if (stack_regs_mentioned (insn))
2576 int n_operands = asm_noperands (PATTERN (insn));
2577 if (n_operands >= 0)
2579 /* This insn is an `asm' with operands. Decode the operands,
2580 decide how many are inputs, and do register substitution.
2581 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2583 subst_asm_stack_regs (insn, regstack);
2584 return;
2587 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2588 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2590 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2591 subst_stack_regs_pat (insn, regstack,
2592 XVECEXP (PATTERN (insn), 0, i));
2594 else
2595 subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2598 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2599 REG_UNUSED will already have been dealt with, so just return. */
2601 if (GET_CODE (insn) == NOTE)
2602 return;
2604 /* If there is a REG_UNUSED note on a stack register on this insn,
2605 the indicated reg must be popped. The REG_UNUSED note is removed,
2606 since the form of the newly emitted pop insn references the reg,
2607 making it no longer `unset'. */
2609 note_link = &REG_NOTES(insn);
2610 for (note = *note_link; note; note = XEXP (note, 1))
2611 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2613 *note_link = XEXP (note, 1);
2614 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), emit_insn_after);
2616 else
2617 note_link = &XEXP (note, 1);
2620 /* Change the organization of the stack so that it fits a new basic
2621 block. Some registers might have to be popped, but there can never be
2622 a register live in the new block that is not now live.
2624 Insert any needed insns before or after INSN. WHEN is emit_insn_before
2625 or emit_insn_after. OLD is the original stack layout, and NEW is
2626 the desired form. OLD is updated to reflect the code emitted, ie, it
2627 will be the same as NEW upon return.
2629 This function will not preserve block_end[]. But that information
2630 is no longer needed once this has executed. */
2632 static void
2633 change_stack (insn, old, new, when)
2634 rtx insn;
2635 stack old;
2636 stack new;
2637 rtx (*when)();
2639 int reg;
2641 /* We will be inserting new insns "backwards", by calling emit_insn_before.
2642 If we are to insert after INSN, find the next insn, and insert before
2643 it. */
2645 if (when == emit_insn_after)
2646 insn = NEXT_INSN (insn);
2648 /* Pop any registers that are not needed in the new block. */
2650 for (reg = old->top; reg >= 0; reg--)
2651 if (! TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2652 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[reg], DFmode),
2653 emit_insn_before);
2655 if (new->top == -2)
2657 /* If the new block has never been processed, then it can inherit
2658 the old stack order. */
2660 new->top = old->top;
2661 bcopy (old->reg, new->reg, sizeof (new->reg));
2663 else
2665 /* This block has been entered before, and we must match the
2666 previously selected stack order. */
2668 /* By now, the only difference should be the order of the stack,
2669 not their depth or liveliness. */
2671 GO_IF_HARD_REG_EQUAL (old->reg_set, new->reg_set, win);
2673 abort ();
2675 win:
2677 if (old->top != new->top)
2678 abort ();
2680 /* If the stack is not empty (new->top != -1), loop here emitting
2681 swaps until the stack is correct.
2683 The worst case number of swaps emitted is N + 2, where N is the
2684 depth of the stack. In some cases, the reg at the top of
2685 stack may be correct, but swapped anyway in order to fix
2686 other regs. But since we never swap any other reg away from
2687 its correct slot, this algorithm will converge. */
2689 if (new->top != -1)
2692 /* Swap the reg at top of stack into the position it is
2693 supposed to be in, until the correct top of stack appears. */
2695 while (old->reg[old->top] != new->reg[new->top])
2697 for (reg = new->top; reg >= 0; reg--)
2698 if (new->reg[reg] == old->reg[old->top])
2699 break;
2701 if (reg == -1)
2702 abort ();
2704 emit_swap_insn (insn, old,
2705 FP_MODE_REG (old->reg[reg], DFmode));
2708 /* See if any regs remain incorrect. If so, bring an
2709 incorrect reg to the top of stack, and let the while loop
2710 above fix it. */
2712 for (reg = new->top; reg >= 0; reg--)
2713 if (new->reg[reg] != old->reg[reg])
2715 emit_swap_insn (insn, old,
2716 FP_MODE_REG (old->reg[reg], DFmode));
2717 break;
2719 } while (reg >= 0);
2721 /* At this point there must be no differences. */
2723 for (reg = old->top; reg >= 0; reg--)
2724 if (old->reg[reg] != new->reg[reg])
2725 abort ();
2729 /* Check PAT, which points to RTL in INSN, for a LABEL_REF. If it is
2730 found, ensure that a jump from INSN to the code_label to which the
2731 label_ref points ends up with the same stack as that at the
2732 code_label. Do this by inserting insns just before the code_label to
2733 pop and rotate the stack until it is in the correct order. REGSTACK
2734 is the order of the register stack in INSN.
2736 Any code that is emitted here must not be later processed as part
2737 of any block, as it will already contain hard register numbers. */
2739 static void
2740 goto_block_pat (insn, regstack, pat)
2741 rtx insn;
2742 stack regstack;
2743 rtx pat;
2745 rtx label;
2746 rtx new_jump, new_label, new_barrier;
2747 rtx *ref;
2748 stack label_stack;
2749 struct stack_def temp_stack;
2750 int reg;
2752 switch (GET_CODE (pat))
2754 case RETURN:
2755 straighten_stack (PREV_INSN (insn), regstack);
2756 return;
2757 default:
2759 int i, j;
2760 const char *fmt = GET_RTX_FORMAT (GET_CODE (pat));
2762 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
2764 if (fmt[i] == 'e')
2765 goto_block_pat (insn, regstack, XEXP (pat, i));
2766 if (fmt[i] == 'E')
2767 for (j = 0; j < XVECLEN (pat, i); j++)
2768 goto_block_pat (insn, regstack, XVECEXP (pat, i, j));
2770 return;
2772 case LABEL_REF:;
2775 label = XEXP (pat, 0);
2776 if (GET_CODE (label) != CODE_LABEL)
2777 abort ();
2779 /* First, see if in fact anything needs to be done to the stack at all. */
2780 if (INSN_UID (label) <= 0)
2781 return;
2783 label_stack = &block_stack_in[BLOCK_NUM (label)];
2785 if (label_stack->top == -2)
2787 /* If the target block hasn't had a stack order selected, then
2788 we need merely ensure that no pops are needed. */
2790 for (reg = regstack->top; reg >= 0; reg--)
2791 if (! TEST_HARD_REG_BIT (label_stack->reg_set, regstack->reg[reg]))
2792 break;
2794 if (reg == -1)
2796 /* change_stack will not emit any code in this case. */
2798 change_stack (label, regstack, label_stack, emit_insn_after);
2799 return;
2802 else if (label_stack->top == regstack->top)
2804 for (reg = label_stack->top; reg >= 0; reg--)
2805 if (label_stack->reg[reg] != regstack->reg[reg])
2806 break;
2808 if (reg == -1)
2809 return;
2812 /* At least one insn will need to be inserted before label. Insert
2813 a jump around the code we are about to emit. Emit a label for the new
2814 code, and point the original insn at this new label. We can't use
2815 redirect_jump here, because we're using fld[4] of the code labels as
2816 LABEL_REF chains, no NUSES counters. */
2818 new_jump = emit_jump_insn_before (gen_jump (label), label);
2819 record_label_references (new_jump, PATTERN (new_jump));
2820 JUMP_LABEL (new_jump) = label;
2822 new_barrier = emit_barrier_after (new_jump);
2824 new_label = gen_label_rtx ();
2825 emit_label_after (new_label, new_barrier);
2826 LABEL_REFS (new_label) = new_label;
2828 /* The old label_ref will no longer point to the code_label if now uses,
2829 so strip the label_ref from the code_label's chain of references. */
2831 for (ref = &LABEL_REFS (label); *ref != label; ref = &LABEL_NEXTREF (*ref))
2832 if (*ref == pat)
2833 break;
2835 if (*ref == label)
2836 abort ();
2838 *ref = LABEL_NEXTREF (*ref);
2840 XEXP (pat, 0) = new_label;
2841 record_label_references (insn, PATTERN (insn));
2843 if (JUMP_LABEL (insn) == label)
2844 JUMP_LABEL (insn) = new_label;
2846 /* Now emit the needed code. */
2848 temp_stack = *regstack;
2850 change_stack (new_label, &temp_stack, label_stack, emit_insn_after);
2853 /* Traverse all basic blocks in a function, converting the register
2854 references in each insn from the "flat" register file that gcc uses, to
2855 the stack-like registers the 387 uses. */
2857 static void
2858 convert_regs ()
2860 register int block, reg;
2861 register rtx insn, next;
2862 struct stack_def regstack;
2864 for (block = 0; block < blocks; block++)
2866 if (block_stack_in[block].top == -2)
2868 /* This block has not been previously encountered. Choose a
2869 default mapping for any stack regs live on entry */
2871 block_stack_in[block].top = -1;
2873 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
2874 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, reg))
2875 block_stack_in[block].reg[++block_stack_in[block].top] = reg;
2878 /* Process all insns in this block. Keep track of `next' here,
2879 so that we don't process any insns emitted while making
2880 substitutions in INSN. */
2882 next = block_begin[block];
2883 regstack = block_stack_in[block];
2886 insn = next;
2887 next = NEXT_INSN (insn);
2889 /* Don't bother processing unless there is a stack reg
2890 mentioned or if it's a CALL_INSN (register passing of
2891 floating point values). */
2893 if (stack_regs_mentioned (insn) || GET_CODE (insn) == CALL_INSN)
2894 subst_stack_regs (insn, &regstack);
2896 } while (insn != block_end[block]);
2898 /* For all further actions, INSN needs to be the last insn in
2899 this basic block. If subst_stack_regs inserted additional
2900 instructions after INSN, it is no longer the last one at
2901 this point. */
2902 next = PREV_INSN (next);
2904 /* If subst_stack_regs inserted something after a JUMP_INSN, that
2905 is almost certainly a bug. */
2906 if (GET_CODE (insn) == JUMP_INSN && insn != next)
2907 abort ();
2908 insn = next;
2910 /* Something failed if the stack life doesn't match. */
2912 GO_IF_HARD_REG_EQUAL (regstack.reg_set, block_out_reg_set[block], win);
2914 abort ();
2916 win:
2918 /* Adjust the stack of this block on exit to match the stack of
2919 the target block, or copy stack information into stack of
2920 jump target if the target block's stack order hasn't been set
2921 yet. */
2923 if (GET_CODE (insn) == JUMP_INSN)
2924 goto_block_pat (insn, &regstack, PATTERN (insn));
2926 /* Likewise handle the case where we fall into the next block. */
2928 if ((block < blocks - 1) && block_drops_in[block+1])
2929 change_stack (insn, &regstack, &block_stack_in[block+1],
2930 emit_insn_after);
2933 /* If the last basic block is the end of a loop, and that loop has
2934 regs live at its start, then the last basic block will have regs live
2935 at its end that need to be popped before the function returns. */
2938 int value_reg_low, value_reg_high;
2939 value_reg_low = value_reg_high = -1;
2941 rtx retvalue;
2942 if ((retvalue = stack_result (current_function_decl)))
2944 value_reg_low = REGNO (retvalue);
2945 value_reg_high = value_reg_low +
2946 HARD_REGNO_NREGS (value_reg_low, GET_MODE (retvalue)) - 1;
2950 for (reg = regstack.top; reg >= 0; reg--)
2951 if (regstack.reg[reg] < value_reg_low
2952 || regstack.reg[reg] > value_reg_high)
2953 insn = emit_pop_insn (insn, &regstack,
2954 FP_MODE_REG (regstack.reg[reg], DFmode),
2955 emit_insn_after);
2957 straighten_stack (insn, &regstack);
2960 /* Check expression PAT, which is in INSN, for label references. if
2961 one is found, print the block number of destination to FILE. */
2963 static void
2964 print_blocks (file, insn, pat)
2965 FILE *file;
2966 rtx insn, pat;
2968 register RTX_CODE code = GET_CODE (pat);
2969 register int i;
2970 register const char *fmt;
2972 if (code == LABEL_REF)
2974 register rtx label = XEXP (pat, 0);
2976 if (GET_CODE (label) != CODE_LABEL)
2977 abort ();
2979 fprintf (file, " %d", BLOCK_NUM (label));
2981 return;
2984 fmt = GET_RTX_FORMAT (code);
2985 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2987 if (fmt[i] == 'e')
2988 print_blocks (file, insn, XEXP (pat, i));
2989 if (fmt[i] == 'E')
2991 register int j;
2992 for (j = 0; j < XVECLEN (pat, i); j++)
2993 print_blocks (file, insn, XVECEXP (pat, i, j));
2998 /* Write information about stack registers and stack blocks into FILE.
2999 This is part of making a debugging dump. */
3001 static void
3002 dump_stack_info (file)
3003 FILE *file;
3005 register int block;
3007 fprintf (file, "\n%d stack blocks.\n", blocks);
3008 for (block = 0; block < blocks; block++)
3010 register rtx head, jump, end;
3011 register int regno;
3013 fprintf (file, "\nStack block %d: first insn %d, last %d.\n",
3014 block, INSN_UID (block_begin[block]),
3015 INSN_UID (block_end[block]));
3017 head = block_begin[block];
3019 fprintf (file, "Reached from blocks: ");
3020 if (GET_CODE (head) == CODE_LABEL)
3021 for (jump = LABEL_REFS (head);
3022 jump != head;
3023 jump = LABEL_NEXTREF (jump))
3025 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
3026 fprintf (file, " %d", from_block);
3028 if (block_drops_in[block])
3029 fprintf (file, " previous");
3031 fprintf (file, "\nlive stack registers on block entry: ");
3032 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
3034 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, regno))
3035 fprintf (file, "%d ", regno);
3038 fprintf (file, "\nlive stack registers on block exit: ");
3039 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
3041 if (TEST_HARD_REG_BIT (block_out_reg_set[block], regno))
3042 fprintf (file, "%d ", regno);
3045 end = block_end[block];
3047 fprintf (file, "\nJumps to blocks: ");
3048 if (GET_CODE (end) == JUMP_INSN)
3049 print_blocks (file, end, PATTERN (end));
3051 if (block + 1 < blocks && block_drops_in[block+1])
3052 fprintf (file, " next");
3053 else if (block + 1 == blocks
3054 || (GET_CODE (end) == JUMP_INSN
3055 && GET_CODE (PATTERN (end)) == RETURN))
3056 fprintf (file, " return");
3058 fprintf (file, "\n");
3061 #endif /* STACK_REGS */