Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / reg-stack.c
blob1002d690804ab6428cd4cea04292668d635e0245
1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* This pass converts stack-like registers from the "flat register
23 file" model that gcc uses, to a stack convention that the 387 uses.
25 * The form of the input:
27 On input, the function consists of insn that have had their
28 registers fully allocated to a set of "virtual" registers. Note that
29 the word "virtual" is used differently here than elsewhere in gcc: for
30 each virtual stack reg, there is a hard reg, but the mapping between
31 them is not known until this pass is run. On output, hard register
32 numbers have been substituted, and various pop and exchange insns have
33 been emitted. The hard register numbers and the virtual register
34 numbers completely overlap - before this pass, all stack register
35 numbers are virtual, and afterward they are all hard.
37 The virtual registers can be manipulated normally by gcc, and their
38 semantics are the same as for normal registers. After the hard
39 register numbers are substituted, the semantics of an insn containing
40 stack-like regs are not the same as for an insn with normal regs: for
41 instance, it is not safe to delete an insn that appears to be a no-op
42 move. In general, no insn containing hard regs should be changed
43 after this pass is done.
45 * The form of the output:
47 After this pass, hard register numbers represent the distance from
48 the current top of stack to the desired register. A reference to
49 FIRST_STACK_REG references the top of stack, FIRST_STACK_REG + 1,
50 represents the register just below that, and so forth. Also, REG_DEAD
51 notes indicate whether or not a stack register should be popped.
53 A "swap" insn looks like a parallel of two patterns, where each
54 pattern is a SET: one sets A to B, the other B to A.
56 A "push" or "load" insn is a SET whose SET_DEST is FIRST_STACK_REG
57 and whose SET_DEST is REG or MEM. Any other SET_DEST, such as PLUS,
58 will replace the existing stack top, not push a new value.
60 A store insn is a SET whose SET_DEST is FIRST_STACK_REG, and whose
61 SET_SRC is REG or MEM.
63 The case where the SET_SRC and SET_DEST are both FIRST_STACK_REG
64 appears ambiguous. As a special case, the presence of a REG_DEAD note
65 for FIRST_STACK_REG differentiates between a load insn and a pop.
67 If a REG_DEAD is present, the insn represents a "pop" that discards
68 the top of the register stack. If there is no REG_DEAD note, then the
69 insn represents a "dup" or a push of the current top of stack onto the
70 stack.
72 * Methodology:
74 Existing REG_DEAD and REG_UNUSED notes for stack registers are
75 deleted and recreated from scratch. REG_DEAD is never created for a
76 SET_DEST, only REG_UNUSED.
78 * asm_operands:
80 There are several rules on the usage of stack-like regs in
81 asm_operands insns. These rules apply only to the operands that are
82 stack-like regs:
84 1. Given a set of input regs that die in an asm_operands, it is
85 necessary to know which are implicitly popped by the asm, and
86 which must be explicitly popped by gcc.
88 An input reg that is implicitly popped by the asm must be
89 explicitly clobbered, unless it is constrained to match an
90 output operand.
92 2. For any input reg that is implicitly popped by an asm, it is
93 necessary to know how to adjust the stack to compensate for the pop.
94 If any non-popped input is closer to the top of the reg-stack than
95 the implicitly popped reg, it would not be possible to know what the
96 stack looked like - it's not clear how the rest of the stack "slides
97 up".
99 All implicitly popped input regs must be closer to the top of
100 the reg-stack than any input that is not implicitly popped.
102 3. It is possible that if an input dies in an insn, reload might
103 use the input reg for an output reload. Consider this example:
105 asm ("foo" : "=t" (a) : "f" (b));
107 This asm says that input B is not popped by the asm, and that
108 the asm pushes a result onto the reg-stack, i.e., the stack is one
109 deeper after the asm than it was before. But, it is possible that
110 reload will think that it can use the same reg for both the input and
111 the output, if input B dies in this insn.
113 If any input operand uses the "f" constraint, all output reg
114 constraints must use the "&" earlyclobber.
116 The asm above would be written as
118 asm ("foo" : "=&t" (a) : "f" (b));
120 4. Some operands need to be in particular places on the stack. All
121 output operands fall in this category - there is no other way to
122 know which regs the outputs appear in unless the user indicates
123 this in the constraints.
125 Output operands must specifically indicate which reg an output
126 appears in after an asm. "=f" is not allowed: the operand
127 constraints must select a class with a single reg.
129 5. Output operands may not be "inserted" between existing stack regs.
130 Since no 387 opcode uses a read/write operand, all output operands
131 are dead before the asm_operands, and are pushed by the asm_operands.
132 It makes no sense to push anywhere but the top of the reg-stack.
134 Output operands must start at the top of the reg-stack: output
135 operands may not "skip" a reg.
137 6. Some asm statements may need extra stack space for internal
138 calculations. This can be guaranteed by clobbering stack registers
139 unrelated to the inputs and outputs.
141 Here are a couple of reasonable asms to want to write. This asm
142 takes one input, which is internally popped, and produces two outputs.
144 asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
146 This asm takes two inputs, which are popped by the fyl2xp1 opcode,
147 and replaces them with one output. The user must code the "st(1)"
148 clobber for reg-stack.c to know that fyl2xp1 pops both inputs.
150 asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
154 #include "config.h"
155 #include "system.h"
156 #include "coretypes.h"
157 #include "tm.h"
158 #include "tree.h"
159 #include "rtl.h"
160 #include "tm_p.h"
161 #include "function.h"
162 #include "insn-config.h"
163 #include "regs.h"
164 #include "hard-reg-set.h"
165 #include "flags.h"
166 #include "toplev.h"
167 #include "recog.h"
168 #include "output.h"
169 #include "basic-block.h"
170 #include "varray.h"
171 #include "reload.h"
172 #include "ggc.h"
173 #include "timevar.h"
174 #include "tree-pass.h"
175 #include "target.h"
177 /* We use this array to cache info about insns, because otherwise we
178 spend too much time in stack_regs_mentioned_p.
180 Indexed by insn UIDs. A value of zero is uninitialized, one indicates
181 the insn uses stack registers, two indicates the insn does not use
182 stack registers. */
183 static GTY(()) varray_type stack_regs_mentioned_data;
185 #ifdef STACK_REGS
187 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
189 /* This is the basic stack record. TOP is an index into REG[] such
190 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
192 If TOP is -2, REG[] is not yet initialized. Stack initialization
193 consists of placing each live reg in array `reg' and setting `top'
194 appropriately.
196 REG_SET indicates which registers are live. */
198 typedef struct stack_def
200 int top; /* index to top stack element */
201 HARD_REG_SET reg_set; /* set of live registers */
202 unsigned char reg[REG_STACK_SIZE];/* register - stack mapping */
203 } *stack;
205 /* This is used to carry information about basic blocks. It is
206 attached to the AUX field of the standard CFG block. */
208 typedef struct block_info_def
210 struct stack_def stack_in; /* Input stack configuration. */
211 struct stack_def stack_out; /* Output stack configuration. */
212 HARD_REG_SET out_reg_set; /* Stack regs live on output. */
213 int done; /* True if block already converted. */
214 int predecessors; /* Number of predecessors that need
215 to be visited. */
216 } *block_info;
218 #define BLOCK_INFO(B) ((block_info) (B)->aux)
220 /* Passed to change_stack to indicate where to emit insns. */
221 enum emit_where
223 EMIT_AFTER,
224 EMIT_BEFORE
227 /* The block we're currently working on. */
228 static basic_block current_block;
230 /* In the current_block, whether we're processing the first register
231 stack or call instruction, i.e. the regstack is currently the
232 same as BLOCK_INFO(current_block)->stack_in. */
233 static bool starting_stack_p;
235 /* This is the register file for all register after conversion. */
236 static rtx
237 FP_mode_reg[LAST_STACK_REG+1-FIRST_STACK_REG][(int) MAX_MACHINE_MODE];
239 #define FP_MODE_REG(regno,mode) \
240 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int) (mode)])
242 /* Used to initialize uninitialized registers. */
243 static rtx not_a_num;
245 /* Forward declarations */
247 static int stack_regs_mentioned_p (rtx pat);
248 static void pop_stack (stack, int);
249 static rtx *get_true_reg (rtx *);
251 static int check_asm_stack_operands (rtx);
252 static int get_asm_operand_n_inputs (rtx);
253 static rtx stack_result (tree);
254 static void replace_reg (rtx *, int);
255 static void remove_regno_note (rtx, enum reg_note, unsigned int);
256 static int get_hard_regnum (stack, rtx);
257 static rtx emit_pop_insn (rtx, stack, rtx, enum emit_where);
258 static void swap_to_top(rtx, stack, rtx, rtx);
259 static bool move_for_stack_reg (rtx, stack, rtx);
260 static bool move_nan_for_stack_reg (rtx, stack, rtx);
261 static int swap_rtx_condition_1 (rtx);
262 static int swap_rtx_condition (rtx);
263 static void compare_for_stack_reg (rtx, stack, rtx);
264 static bool subst_stack_regs_pat (rtx, stack, rtx);
265 static void subst_asm_stack_regs (rtx, stack);
266 static bool subst_stack_regs (rtx, stack);
267 static void change_stack (rtx, stack, stack, enum emit_where);
268 static void print_stack (FILE *, stack);
269 static rtx next_flags_user (rtx);
271 /* Return nonzero if any stack register is mentioned somewhere within PAT. */
273 static int
274 stack_regs_mentioned_p (rtx pat)
276 const char *fmt;
277 int i;
279 if (STACK_REG_P (pat))
280 return 1;
282 fmt = GET_RTX_FORMAT (GET_CODE (pat));
283 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
285 if (fmt[i] == 'E')
287 int j;
289 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
290 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
291 return 1;
293 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
294 return 1;
297 return 0;
300 /* Return nonzero if INSN mentions stacked registers, else return zero. */
303 stack_regs_mentioned (rtx insn)
305 unsigned int uid, max;
306 int test;
308 if (! INSN_P (insn) || !stack_regs_mentioned_data)
309 return 0;
311 uid = INSN_UID (insn);
312 max = VARRAY_SIZE (stack_regs_mentioned_data);
313 if (uid >= max)
315 /* Allocate some extra size to avoid too many reallocs, but
316 do not grow too quickly. */
317 max = uid + uid / 20;
318 VARRAY_GROW (stack_regs_mentioned_data, max);
321 test = VARRAY_CHAR (stack_regs_mentioned_data, uid);
322 if (test == 0)
324 /* This insn has yet to be examined. Do so now. */
325 test = stack_regs_mentioned_p (PATTERN (insn)) ? 1 : 2;
326 VARRAY_CHAR (stack_regs_mentioned_data, uid) = test;
329 return test == 1;
332 static rtx ix86_flags_rtx;
334 static rtx
335 next_flags_user (rtx insn)
337 /* Search forward looking for the first use of this value.
338 Stop at block boundaries. */
340 while (insn != BB_END (current_block))
342 insn = NEXT_INSN (insn);
344 if (INSN_P (insn) && reg_mentioned_p (ix86_flags_rtx, PATTERN (insn)))
345 return insn;
347 if (CALL_P (insn))
348 return NULL_RTX;
350 return NULL_RTX;
353 /* Reorganize the stack into ascending numbers, before this insn. */
355 static void
356 straighten_stack (rtx insn, stack regstack)
358 struct stack_def temp_stack;
359 int top;
361 /* If there is only a single register on the stack, then the stack is
362 already in increasing order and no reorganization is needed.
364 Similarly if the stack is empty. */
365 if (regstack->top <= 0)
366 return;
368 COPY_HARD_REG_SET (temp_stack.reg_set, regstack->reg_set);
370 for (top = temp_stack.top = regstack->top; top >= 0; top--)
371 temp_stack.reg[top] = FIRST_STACK_REG + temp_stack.top - top;
373 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
376 /* Pop a register from the stack. */
378 static void
379 pop_stack (stack regstack, int regno)
381 int top = regstack->top;
383 CLEAR_HARD_REG_BIT (regstack->reg_set, regno);
384 regstack->top--;
385 /* If regno was not at the top of stack then adjust stack. */
386 if (regstack->reg [top] != regno)
388 int i;
389 for (i = regstack->top; i >= 0; i--)
390 if (regstack->reg [i] == regno)
392 int j;
393 for (j = i; j < top; j++)
394 regstack->reg [j] = regstack->reg [j + 1];
395 break;
400 /* Return a pointer to the REG expression within PAT. If PAT is not a
401 REG, possible enclosed by a conversion rtx, return the inner part of
402 PAT that stopped the search. */
404 static rtx *
405 get_true_reg (rtx *pat)
407 for (;;)
408 switch (GET_CODE (*pat))
410 case SUBREG:
411 /* Eliminate FP subregister accesses in favor of the
412 actual FP register in use. */
414 rtx subreg;
415 if (FP_REG_P (subreg = SUBREG_REG (*pat)))
417 int regno_off = subreg_regno_offset (REGNO (subreg),
418 GET_MODE (subreg),
419 SUBREG_BYTE (*pat),
420 GET_MODE (*pat));
421 *pat = FP_MODE_REG (REGNO (subreg) + regno_off,
422 GET_MODE (subreg));
423 default:
424 return pat;
427 case FLOAT:
428 case FIX:
429 case FLOAT_EXTEND:
430 pat = & XEXP (*pat, 0);
431 break;
433 case FLOAT_TRUNCATE:
434 if (!flag_unsafe_math_optimizations)
435 return pat;
436 pat = & XEXP (*pat, 0);
437 break;
441 /* Set if we find any malformed asms in a block. */
442 static bool any_malformed_asm;
444 /* There are many rules that an asm statement for stack-like regs must
445 follow. Those rules are explained at the top of this file: the rule
446 numbers below refer to that explanation. */
448 static int
449 check_asm_stack_operands (rtx insn)
451 int i;
452 int n_clobbers;
453 int malformed_asm = 0;
454 rtx body = PATTERN (insn);
456 char reg_used_as_output[FIRST_PSEUDO_REGISTER];
457 char implicitly_dies[FIRST_PSEUDO_REGISTER];
458 int alt;
460 rtx *clobber_reg = 0;
461 int n_inputs, n_outputs;
463 /* Find out what the constraints require. If no constraint
464 alternative matches, this asm is malformed. */
465 extract_insn (insn);
466 constrain_operands (1);
467 alt = which_alternative;
469 preprocess_constraints ();
471 n_inputs = get_asm_operand_n_inputs (body);
472 n_outputs = recog_data.n_operands - n_inputs;
474 if (alt < 0)
476 malformed_asm = 1;
477 /* Avoid further trouble with this insn. */
478 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
479 return 0;
482 /* Strip SUBREGs here to make the following code simpler. */
483 for (i = 0; i < recog_data.n_operands; i++)
484 if (GET_CODE (recog_data.operand[i]) == SUBREG
485 && REG_P (SUBREG_REG (recog_data.operand[i])))
486 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
488 /* Set up CLOBBER_REG. */
490 n_clobbers = 0;
492 if (GET_CODE (body) == PARALLEL)
494 clobber_reg = alloca (XVECLEN (body, 0) * sizeof (rtx));
496 for (i = 0; i < XVECLEN (body, 0); i++)
497 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
499 rtx clobber = XVECEXP (body, 0, i);
500 rtx reg = XEXP (clobber, 0);
502 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
503 reg = SUBREG_REG (reg);
505 if (STACK_REG_P (reg))
507 clobber_reg[n_clobbers] = reg;
508 n_clobbers++;
513 /* Enforce rule #4: Output operands must specifically indicate which
514 reg an output appears in after an asm. "=f" is not allowed: the
515 operand constraints must select a class with a single reg.
517 Also enforce rule #5: Output operands must start at the top of
518 the reg-stack: output operands may not "skip" a reg. */
520 memset (reg_used_as_output, 0, sizeof (reg_used_as_output));
521 for (i = 0; i < n_outputs; i++)
522 if (STACK_REG_P (recog_data.operand[i]))
524 if (reg_class_size[(int) recog_op_alt[i][alt].cl] != 1)
526 error_for_asm (insn, "output constraint %d must specify a single register", i);
527 malformed_asm = 1;
529 else
531 int j;
533 for (j = 0; j < n_clobbers; j++)
534 if (REGNO (recog_data.operand[i]) == REGNO (clobber_reg[j]))
536 error_for_asm (insn, "output constraint %d cannot be specified together with \"%s\" clobber",
537 i, reg_names [REGNO (clobber_reg[j])]);
538 malformed_asm = 1;
539 break;
541 if (j == n_clobbers)
542 reg_used_as_output[REGNO (recog_data.operand[i])] = 1;
547 /* Search for first non-popped reg. */
548 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
549 if (! reg_used_as_output[i])
550 break;
552 /* If there are any other popped regs, that's an error. */
553 for (; i < LAST_STACK_REG + 1; i++)
554 if (reg_used_as_output[i])
555 break;
557 if (i != LAST_STACK_REG + 1)
559 error_for_asm (insn, "output regs must be grouped at top of stack");
560 malformed_asm = 1;
563 /* Enforce rule #2: All implicitly popped input regs must be closer
564 to the top of the reg-stack than any input that is not implicitly
565 popped. */
567 memset (implicitly_dies, 0, sizeof (implicitly_dies));
568 for (i = n_outputs; i < n_outputs + n_inputs; i++)
569 if (STACK_REG_P (recog_data.operand[i]))
571 /* An input reg is implicitly popped if it is tied to an
572 output, or if there is a CLOBBER for it. */
573 int j;
575 for (j = 0; j < n_clobbers; j++)
576 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
577 break;
579 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
580 implicitly_dies[REGNO (recog_data.operand[i])] = 1;
583 /* Search for first non-popped reg. */
584 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
585 if (! implicitly_dies[i])
586 break;
588 /* If there are any other popped regs, that's an error. */
589 for (; i < LAST_STACK_REG + 1; i++)
590 if (implicitly_dies[i])
591 break;
593 if (i != LAST_STACK_REG + 1)
595 error_for_asm (insn,
596 "implicitly popped regs must be grouped at top of stack");
597 malformed_asm = 1;
600 /* Enforce rule #3: If any input operand uses the "f" constraint, all
601 output constraints must use the "&" earlyclobber.
603 ??? Detect this more deterministically by having constrain_asm_operands
604 record any earlyclobber. */
606 for (i = n_outputs; i < n_outputs + n_inputs; i++)
607 if (recog_op_alt[i][alt].matches == -1)
609 int j;
611 for (j = 0; j < n_outputs; j++)
612 if (operands_match_p (recog_data.operand[j], recog_data.operand[i]))
614 error_for_asm (insn,
615 "output operand %d must use %<&%> constraint", j);
616 malformed_asm = 1;
620 if (malformed_asm)
622 /* Avoid further trouble with this insn. */
623 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
624 any_malformed_asm = true;
625 return 0;
628 return 1;
631 /* Calculate the number of inputs and outputs in BODY, an
632 asm_operands. N_OPERANDS is the total number of operands, and
633 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
634 placed. */
636 static int
637 get_asm_operand_n_inputs (rtx body)
639 switch (GET_CODE (body))
641 case SET:
642 gcc_assert (GET_CODE (SET_SRC (body)) == ASM_OPERANDS);
643 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body));
645 case ASM_OPERANDS:
646 return ASM_OPERANDS_INPUT_LENGTH (body);
648 case PARALLEL:
649 return get_asm_operand_n_inputs (XVECEXP (body, 0, 0));
651 default:
652 gcc_unreachable ();
656 /* If current function returns its result in an fp stack register,
657 return the REG. Otherwise, return 0. */
659 static rtx
660 stack_result (tree decl)
662 rtx result;
664 /* If the value is supposed to be returned in memory, then clearly
665 it is not returned in a stack register. */
666 if (aggregate_value_p (DECL_RESULT (decl), decl))
667 return 0;
669 result = DECL_RTL_IF_SET (DECL_RESULT (decl));
670 if (result != 0)
671 result = targetm.calls.function_value (TREE_TYPE (DECL_RESULT (decl)),
672 decl, true);
674 return result != 0 && STACK_REG_P (result) ? result : 0;
679 * This section deals with stack register substitution, and forms the second
680 * pass over the RTL.
683 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
684 the desired hard REGNO. */
686 static void
687 replace_reg (rtx *reg, int regno)
689 gcc_assert (regno >= FIRST_STACK_REG);
690 gcc_assert (regno <= LAST_STACK_REG);
691 gcc_assert (STACK_REG_P (*reg));
693 gcc_assert (GET_MODE_CLASS (GET_MODE (*reg)) == MODE_FLOAT
694 || GET_MODE_CLASS (GET_MODE (*reg)) == MODE_COMPLEX_FLOAT);
696 *reg = FP_MODE_REG (regno, GET_MODE (*reg));
699 /* Remove a note of type NOTE, which must be found, for register
700 number REGNO from INSN. Remove only one such note. */
702 static void
703 remove_regno_note (rtx insn, enum reg_note note, unsigned int regno)
705 rtx *note_link, this;
707 note_link = &REG_NOTES (insn);
708 for (this = *note_link; this; this = XEXP (this, 1))
709 if (REG_NOTE_KIND (this) == note
710 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno)
712 *note_link = XEXP (this, 1);
713 return;
715 else
716 note_link = &XEXP (this, 1);
718 gcc_unreachable ();
721 /* Find the hard register number of virtual register REG in REGSTACK.
722 The hard register number is relative to the top of the stack. -1 is
723 returned if the register is not found. */
725 static int
726 get_hard_regnum (stack regstack, rtx reg)
728 int i;
730 gcc_assert (STACK_REG_P (reg));
732 for (i = regstack->top; i >= 0; i--)
733 if (regstack->reg[i] == REGNO (reg))
734 break;
736 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
739 /* Emit an insn to pop virtual register REG before or after INSN.
740 REGSTACK is the stack state after INSN and is updated to reflect this
741 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
742 is represented as a SET whose destination is the register to be popped
743 and source is the top of stack. A death note for the top of stack
744 cases the movdf pattern to pop. */
746 static rtx
747 emit_pop_insn (rtx insn, stack regstack, rtx reg, enum emit_where where)
749 rtx pop_insn, pop_rtx;
750 int hard_regno;
752 /* For complex types take care to pop both halves. These may survive in
753 CLOBBER and USE expressions. */
754 if (COMPLEX_MODE_P (GET_MODE (reg)))
756 rtx reg1 = FP_MODE_REG (REGNO (reg), DFmode);
757 rtx reg2 = FP_MODE_REG (REGNO (reg) + 1, DFmode);
759 pop_insn = NULL_RTX;
760 if (get_hard_regnum (regstack, reg1) >= 0)
761 pop_insn = emit_pop_insn (insn, regstack, reg1, where);
762 if (get_hard_regnum (regstack, reg2) >= 0)
763 pop_insn = emit_pop_insn (insn, regstack, reg2, where);
764 gcc_assert (pop_insn);
765 return pop_insn;
768 hard_regno = get_hard_regnum (regstack, reg);
770 gcc_assert (hard_regno >= FIRST_STACK_REG);
772 pop_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG (hard_regno, DFmode),
773 FP_MODE_REG (FIRST_STACK_REG, DFmode));
775 if (where == EMIT_AFTER)
776 pop_insn = emit_insn_after (pop_rtx, insn);
777 else
778 pop_insn = emit_insn_before (pop_rtx, insn);
780 REG_NOTES (pop_insn)
781 = gen_rtx_EXPR_LIST (REG_DEAD, FP_MODE_REG (FIRST_STACK_REG, DFmode),
782 REG_NOTES (pop_insn));
784 regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
785 = regstack->reg[regstack->top];
786 regstack->top -= 1;
787 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
789 return pop_insn;
792 /* Emit an insn before or after INSN to swap virtual register REG with
793 the top of stack. REGSTACK is the stack state before the swap, and
794 is updated to reflect the swap. A swap insn is represented as a
795 PARALLEL of two patterns: each pattern moves one reg to the other.
797 If REG is already at the top of the stack, no insn is emitted. */
799 static void
800 emit_swap_insn (rtx insn, stack regstack, rtx reg)
802 int hard_regno;
803 rtx swap_rtx;
804 int tmp, other_reg; /* swap regno temps */
805 rtx i1; /* the stack-reg insn prior to INSN */
806 rtx i1set = NULL_RTX; /* the SET rtx within I1 */
808 hard_regno = get_hard_regnum (regstack, reg);
810 if (hard_regno == FIRST_STACK_REG)
811 return;
812 if (hard_regno == -1)
814 /* Something failed if the register wasn't on the stack. If we had
815 malformed asms, we zapped the instruction itself, but that didn't
816 produce the same pattern of register sets as before. To prevent
817 further failure, adjust REGSTACK to include REG at TOP. */
818 gcc_assert (any_malformed_asm);
819 regstack->reg[++regstack->top] = REGNO (reg);
820 return;
822 gcc_assert (hard_regno >= FIRST_STACK_REG);
824 other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
826 tmp = regstack->reg[other_reg];
827 regstack->reg[other_reg] = regstack->reg[regstack->top];
828 regstack->reg[regstack->top] = tmp;
830 /* Find the previous insn involving stack regs, but don't pass a
831 block boundary. */
832 i1 = NULL;
833 if (current_block && insn != BB_HEAD (current_block))
835 rtx tmp = PREV_INSN (insn);
836 rtx limit = PREV_INSN (BB_HEAD (current_block));
837 while (tmp != limit)
839 if (LABEL_P (tmp)
840 || CALL_P (tmp)
841 || NOTE_INSN_BASIC_BLOCK_P (tmp)
842 || (NONJUMP_INSN_P (tmp)
843 && stack_regs_mentioned (tmp)))
845 i1 = tmp;
846 break;
848 tmp = PREV_INSN (tmp);
852 if (i1 != NULL_RTX
853 && (i1set = single_set (i1)) != NULL_RTX)
855 rtx i1src = *get_true_reg (&SET_SRC (i1set));
856 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
858 /* If the previous register stack push was from the reg we are to
859 swap with, omit the swap. */
861 if (REG_P (i1dest) && REGNO (i1dest) == FIRST_STACK_REG
862 && REG_P (i1src)
863 && REGNO (i1src) == (unsigned) hard_regno - 1
864 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
865 return;
867 /* If the previous insn wrote to the reg we are to swap with,
868 omit the swap. */
870 if (REG_P (i1dest) && REGNO (i1dest) == (unsigned) hard_regno
871 && REG_P (i1src) && REGNO (i1src) == FIRST_STACK_REG
872 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
873 return;
876 /* Avoid emitting the swap if this is the first register stack insn
877 of the current_block. Instead update the current_block's stack_in
878 and let compensate edges take care of this for us. */
879 if (current_block && starting_stack_p)
881 BLOCK_INFO (current_block)->stack_in = *regstack;
882 starting_stack_p = false;
883 return;
886 swap_rtx = gen_swapxf (FP_MODE_REG (hard_regno, XFmode),
887 FP_MODE_REG (FIRST_STACK_REG, XFmode));
889 if (i1)
890 emit_insn_after (swap_rtx, i1);
891 else if (current_block)
892 emit_insn_before (swap_rtx, BB_HEAD (current_block));
893 else
894 emit_insn_before (swap_rtx, insn);
897 /* Emit an insns before INSN to swap virtual register SRC1 with
898 the top of stack and virtual register SRC2 with second stack
899 slot. REGSTACK is the stack state before the swaps, and
900 is updated to reflect the swaps. A swap insn is represented as a
901 PARALLEL of two patterns: each pattern moves one reg to the other.
903 If SRC1 and/or SRC2 are already at the right place, no swap insn
904 is emitted. */
906 static void
907 swap_to_top (rtx insn, stack regstack, rtx src1, rtx src2)
909 struct stack_def temp_stack;
910 int regno, j, k, temp;
912 temp_stack = *regstack;
914 /* Place operand 1 at the top of stack. */
915 regno = get_hard_regnum (&temp_stack, src1);
916 gcc_assert (regno >= 0);
917 if (regno != FIRST_STACK_REG)
919 k = temp_stack.top - (regno - FIRST_STACK_REG);
920 j = temp_stack.top;
922 temp = temp_stack.reg[k];
923 temp_stack.reg[k] = temp_stack.reg[j];
924 temp_stack.reg[j] = temp;
927 /* Place operand 2 next on the stack. */
928 regno = get_hard_regnum (&temp_stack, src2);
929 gcc_assert (regno >= 0);
930 if (regno != FIRST_STACK_REG + 1)
932 k = temp_stack.top - (regno - FIRST_STACK_REG);
933 j = temp_stack.top - 1;
935 temp = temp_stack.reg[k];
936 temp_stack.reg[k] = temp_stack.reg[j];
937 temp_stack.reg[j] = temp;
940 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
943 /* Handle a move to or from a stack register in PAT, which is in INSN.
944 REGSTACK is the current stack. Return whether a control flow insn
945 was deleted in the process. */
947 static bool
948 move_for_stack_reg (rtx insn, stack regstack, rtx pat)
950 rtx *psrc = get_true_reg (&SET_SRC (pat));
951 rtx *pdest = get_true_reg (&SET_DEST (pat));
952 rtx src, dest;
953 rtx note;
954 bool control_flow_insn_deleted = false;
956 src = *psrc; dest = *pdest;
958 if (STACK_REG_P (src) && STACK_REG_P (dest))
960 /* Write from one stack reg to another. If SRC dies here, then
961 just change the register mapping and delete the insn. */
963 note = find_regno_note (insn, REG_DEAD, REGNO (src));
964 if (note)
966 int i;
968 /* If this is a no-op move, there must not be a REG_DEAD note. */
969 gcc_assert (REGNO (src) != REGNO (dest));
971 for (i = regstack->top; i >= 0; i--)
972 if (regstack->reg[i] == REGNO (src))
973 break;
975 /* The destination must be dead, or life analysis is borked. */
976 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
978 /* If the source is not live, this is yet another case of
979 uninitialized variables. Load up a NaN instead. */
980 if (i < 0)
981 return move_nan_for_stack_reg (insn, regstack, dest);
983 /* It is possible that the dest is unused after this insn.
984 If so, just pop the src. */
986 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
987 emit_pop_insn (insn, regstack, src, EMIT_AFTER);
988 else
990 regstack->reg[i] = REGNO (dest);
991 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
992 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
995 control_flow_insn_deleted |= control_flow_insn_p (insn);
996 delete_insn (insn);
997 return control_flow_insn_deleted;
1000 /* The source reg does not die. */
1002 /* If this appears to be a no-op move, delete it, or else it
1003 will confuse the machine description output patterns. But if
1004 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1005 for REG_UNUSED will not work for deleted insns. */
1007 if (REGNO (src) == REGNO (dest))
1009 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1010 emit_pop_insn (insn, regstack, dest, EMIT_AFTER);
1012 control_flow_insn_deleted |= control_flow_insn_p (insn);
1013 delete_insn (insn);
1014 return control_flow_insn_deleted;
1017 /* The destination ought to be dead. */
1018 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
1020 replace_reg (psrc, get_hard_regnum (regstack, src));
1022 regstack->reg[++regstack->top] = REGNO (dest);
1023 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1024 replace_reg (pdest, FIRST_STACK_REG);
1026 else if (STACK_REG_P (src))
1028 /* Save from a stack reg to MEM, or possibly integer reg. Since
1029 only top of stack may be saved, emit an exchange first if
1030 needs be. */
1032 emit_swap_insn (insn, regstack, src);
1034 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1035 if (note)
1037 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1038 regstack->top--;
1039 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1041 else if ((GET_MODE (src) == XFmode)
1042 && regstack->top < REG_STACK_SIZE - 1)
1044 /* A 387 cannot write an XFmode value to a MEM without
1045 clobbering the source reg. The output code can handle
1046 this by reading back the value from the MEM.
1047 But it is more efficient to use a temp register if one is
1048 available. Push the source value here if the register
1049 stack is not full, and then write the value to memory via
1050 a pop. */
1051 rtx push_rtx;
1052 rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, GET_MODE (src));
1054 push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
1055 emit_insn_before (push_rtx, insn);
1056 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD, top_stack_reg,
1057 REG_NOTES (insn));
1060 replace_reg (psrc, FIRST_STACK_REG);
1062 else
1064 gcc_assert (STACK_REG_P (dest));
1066 /* Load from MEM, or possibly integer REG or constant, into the
1067 stack regs. The actual target is always the top of the
1068 stack. The stack mapping is changed to reflect that DEST is
1069 now at top of stack. */
1071 /* The destination ought to be dead. */
1072 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
1074 gcc_assert (regstack->top < REG_STACK_SIZE);
1076 regstack->reg[++regstack->top] = REGNO (dest);
1077 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1078 replace_reg (pdest, FIRST_STACK_REG);
1081 return control_flow_insn_deleted;
1084 /* A helper function which replaces INSN with a pattern that loads up
1085 a NaN into DEST, then invokes move_for_stack_reg. */
1087 static bool
1088 move_nan_for_stack_reg (rtx insn, stack regstack, rtx dest)
1090 rtx pat;
1092 dest = FP_MODE_REG (REGNO (dest), SFmode);
1093 pat = gen_rtx_SET (VOIDmode, dest, not_a_num);
1094 PATTERN (insn) = pat;
1095 INSN_CODE (insn) = -1;
1097 return move_for_stack_reg (insn, regstack, pat);
1100 /* Swap the condition on a branch, if there is one. Return true if we
1101 found a condition to swap. False if the condition was not used as
1102 such. */
1104 static int
1105 swap_rtx_condition_1 (rtx pat)
1107 const char *fmt;
1108 int i, r = 0;
1110 if (COMPARISON_P (pat))
1112 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1113 r = 1;
1115 else
1117 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1118 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1120 if (fmt[i] == 'E')
1122 int j;
1124 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1125 r |= swap_rtx_condition_1 (XVECEXP (pat, i, j));
1127 else if (fmt[i] == 'e')
1128 r |= swap_rtx_condition_1 (XEXP (pat, i));
1132 return r;
1135 static int
1136 swap_rtx_condition (rtx insn)
1138 rtx pat = PATTERN (insn);
1140 /* We're looking for a single set to cc0 or an HImode temporary. */
1142 if (GET_CODE (pat) == SET
1143 && REG_P (SET_DEST (pat))
1144 && REGNO (SET_DEST (pat)) == FLAGS_REG)
1146 insn = next_flags_user (insn);
1147 if (insn == NULL_RTX)
1148 return 0;
1149 pat = PATTERN (insn);
1152 /* See if this is, or ends in, a fnstsw. If so, we're not doing anything
1153 with the cc value right now. We may be able to search for one
1154 though. */
1156 if (GET_CODE (pat) == SET
1157 && GET_CODE (SET_SRC (pat)) == UNSPEC
1158 && XINT (SET_SRC (pat), 1) == UNSPEC_FNSTSW)
1160 rtx dest = SET_DEST (pat);
1162 /* Search forward looking for the first use of this value.
1163 Stop at block boundaries. */
1164 while (insn != BB_END (current_block))
1166 insn = NEXT_INSN (insn);
1167 if (INSN_P (insn) && reg_mentioned_p (dest, insn))
1168 break;
1169 if (CALL_P (insn))
1170 return 0;
1173 /* We haven't found it. */
1174 if (insn == BB_END (current_block))
1175 return 0;
1177 /* So we've found the insn using this value. If it is anything
1178 other than sahf or the value does not die (meaning we'd have
1179 to search further), then we must give up. */
1180 pat = PATTERN (insn);
1181 if (GET_CODE (pat) != SET
1182 || GET_CODE (SET_SRC (pat)) != UNSPEC
1183 || XINT (SET_SRC (pat), 1) != UNSPEC_SAHF
1184 || ! dead_or_set_p (insn, dest))
1185 return 0;
1187 /* Now we are prepared to handle this as a normal cc0 setter. */
1188 insn = next_flags_user (insn);
1189 if (insn == NULL_RTX)
1190 return 0;
1191 pat = PATTERN (insn);
1194 if (swap_rtx_condition_1 (pat))
1196 int fail = 0;
1197 INSN_CODE (insn) = -1;
1198 if (recog_memoized (insn) == -1)
1199 fail = 1;
1200 /* In case the flags don't die here, recurse to try fix
1201 following user too. */
1202 else if (! dead_or_set_p (insn, ix86_flags_rtx))
1204 insn = next_flags_user (insn);
1205 if (!insn || !swap_rtx_condition (insn))
1206 fail = 1;
1208 if (fail)
1210 swap_rtx_condition_1 (pat);
1211 return 0;
1213 return 1;
1215 return 0;
1218 /* Handle a comparison. Special care needs to be taken to avoid
1219 causing comparisons that a 387 cannot do correctly, such as EQ.
1221 Also, a pop insn may need to be emitted. The 387 does have an
1222 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1223 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1224 set up. */
1226 static void
1227 compare_for_stack_reg (rtx insn, stack regstack, rtx pat_src)
1229 rtx *src1, *src2;
1230 rtx src1_note, src2_note;
1232 src1 = get_true_reg (&XEXP (pat_src, 0));
1233 src2 = get_true_reg (&XEXP (pat_src, 1));
1235 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1236 registers that die in this insn - move those to stack top first. */
1237 if ((! STACK_REG_P (*src1)
1238 || (STACK_REG_P (*src2)
1239 && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
1240 && swap_rtx_condition (insn))
1242 rtx temp;
1243 temp = XEXP (pat_src, 0);
1244 XEXP (pat_src, 0) = XEXP (pat_src, 1);
1245 XEXP (pat_src, 1) = temp;
1247 src1 = get_true_reg (&XEXP (pat_src, 0));
1248 src2 = get_true_reg (&XEXP (pat_src, 1));
1250 INSN_CODE (insn) = -1;
1253 /* We will fix any death note later. */
1255 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1257 if (STACK_REG_P (*src2))
1258 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1259 else
1260 src2_note = NULL_RTX;
1262 emit_swap_insn (insn, regstack, *src1);
1264 replace_reg (src1, FIRST_STACK_REG);
1266 if (STACK_REG_P (*src2))
1267 replace_reg (src2, get_hard_regnum (regstack, *src2));
1269 if (src1_note)
1271 pop_stack (regstack, REGNO (XEXP (src1_note, 0)));
1272 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1275 /* If the second operand dies, handle that. But if the operands are
1276 the same stack register, don't bother, because only one death is
1277 needed, and it was just handled. */
1279 if (src2_note
1280 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
1281 && REGNO (*src1) == REGNO (*src2)))
1283 /* As a special case, two regs may die in this insn if src2 is
1284 next to top of stack and the top of stack also dies. Since
1285 we have already popped src1, "next to top of stack" is really
1286 at top (FIRST_STACK_REG) now. */
1288 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1289 && src1_note)
1291 pop_stack (regstack, REGNO (XEXP (src2_note, 0)));
1292 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1294 else
1296 /* The 386 can only represent death of the first operand in
1297 the case handled above. In all other cases, emit a separate
1298 pop and remove the death note from here. */
1300 /* link_cc0_insns (insn); */
1302 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
1304 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
1305 EMIT_AFTER);
1310 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1311 is the current register layout. Return whether a control flow insn
1312 was deleted in the process. */
1314 static bool
1315 subst_stack_regs_pat (rtx insn, stack regstack, rtx pat)
1317 rtx *dest, *src;
1318 bool control_flow_insn_deleted = false;
1320 switch (GET_CODE (pat))
1322 case USE:
1323 /* Deaths in USE insns can happen in non optimizing compilation.
1324 Handle them by popping the dying register. */
1325 src = get_true_reg (&XEXP (pat, 0));
1326 if (STACK_REG_P (*src)
1327 && find_regno_note (insn, REG_DEAD, REGNO (*src)))
1329 emit_pop_insn (insn, regstack, *src, EMIT_AFTER);
1330 return control_flow_insn_deleted;
1332 /* ??? Uninitialized USE should not happen. */
1333 else
1334 gcc_assert (get_hard_regnum (regstack, *src) != -1);
1335 break;
1337 case CLOBBER:
1339 rtx note;
1341 dest = get_true_reg (&XEXP (pat, 0));
1342 if (STACK_REG_P (*dest))
1344 note = find_reg_note (insn, REG_DEAD, *dest);
1346 if (pat != PATTERN (insn))
1348 /* The fix_truncdi_1 pattern wants to be able to allocate
1349 its own scratch register. It does this by clobbering
1350 an fp reg so that it is assured of an empty reg-stack
1351 register. If the register is live, kill it now.
1352 Remove the DEAD/UNUSED note so we don't try to kill it
1353 later too. */
1355 if (note)
1356 emit_pop_insn (insn, regstack, *dest, EMIT_BEFORE);
1357 else
1359 note = find_reg_note (insn, REG_UNUSED, *dest);
1360 gcc_assert (note);
1362 remove_note (insn, note);
1363 replace_reg (dest, FIRST_STACK_REG + 1);
1365 else
1367 /* A top-level clobber with no REG_DEAD, and no hard-regnum
1368 indicates an uninitialized value. Because reload removed
1369 all other clobbers, this must be due to a function
1370 returning without a value. Load up a NaN. */
1372 if (!note)
1374 rtx t = *dest;
1375 if (get_hard_regnum (regstack, t) == -1)
1376 control_flow_insn_deleted
1377 |= move_nan_for_stack_reg (insn, regstack, t);
1378 if (COMPLEX_MODE_P (GET_MODE (t)))
1380 t = FP_MODE_REG (REGNO (t) + 1, DFmode);
1381 if (get_hard_regnum (regstack, t) == -1)
1382 control_flow_insn_deleted
1383 |= move_nan_for_stack_reg (insn, regstack, t);
1388 break;
1391 case SET:
1393 rtx *src1 = (rtx *) 0, *src2;
1394 rtx src1_note, src2_note;
1395 rtx pat_src;
1397 dest = get_true_reg (&SET_DEST (pat));
1398 src = get_true_reg (&SET_SRC (pat));
1399 pat_src = SET_SRC (pat);
1401 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1402 if (STACK_REG_P (*src)
1403 || (STACK_REG_P (*dest)
1404 && (REG_P (*src) || MEM_P (*src)
1405 || GET_CODE (*src) == CONST_DOUBLE)))
1407 control_flow_insn_deleted |= move_for_stack_reg (insn, regstack, pat);
1408 break;
1411 switch (GET_CODE (pat_src))
1413 case COMPARE:
1414 compare_for_stack_reg (insn, regstack, pat_src);
1415 break;
1417 case CALL:
1419 int count;
1420 for (count = hard_regno_nregs[REGNO (*dest)][GET_MODE (*dest)];
1421 --count >= 0;)
1423 regstack->reg[++regstack->top] = REGNO (*dest) + count;
1424 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
1427 replace_reg (dest, FIRST_STACK_REG);
1428 break;
1430 case REG:
1431 /* This is a `tstM2' case. */
1432 gcc_assert (*dest == cc0_rtx);
1433 src1 = src;
1435 /* Fall through. */
1437 case FLOAT_TRUNCATE:
1438 case SQRT:
1439 case ABS:
1440 case NEG:
1441 /* These insns only operate on the top of the stack. DEST might
1442 be cc0_rtx if we're processing a tstM pattern. Also, it's
1443 possible that the tstM case results in a REG_DEAD note on the
1444 source. */
1446 if (src1 == 0)
1447 src1 = get_true_reg (&XEXP (pat_src, 0));
1449 emit_swap_insn (insn, regstack, *src1);
1451 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1453 if (STACK_REG_P (*dest))
1454 replace_reg (dest, FIRST_STACK_REG);
1456 if (src1_note)
1458 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1459 regstack->top--;
1460 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1463 replace_reg (src1, FIRST_STACK_REG);
1464 break;
1466 case MINUS:
1467 case DIV:
1468 /* On i386, reversed forms of subM3 and divM3 exist for
1469 MODE_FLOAT, so the same code that works for addM3 and mulM3
1470 can be used. */
1471 case MULT:
1472 case PLUS:
1473 /* These insns can accept the top of stack as a destination
1474 from a stack reg or mem, or can use the top of stack as a
1475 source and some other stack register (possibly top of stack)
1476 as a destination. */
1478 src1 = get_true_reg (&XEXP (pat_src, 0));
1479 src2 = get_true_reg (&XEXP (pat_src, 1));
1481 /* We will fix any death note later. */
1483 if (STACK_REG_P (*src1))
1484 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1485 else
1486 src1_note = NULL_RTX;
1487 if (STACK_REG_P (*src2))
1488 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1489 else
1490 src2_note = NULL_RTX;
1492 /* If either operand is not a stack register, then the dest
1493 must be top of stack. */
1495 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
1496 emit_swap_insn (insn, regstack, *dest);
1497 else
1499 /* Both operands are REG. If neither operand is already
1500 at the top of stack, choose to make the one that is the dest
1501 the new top of stack. */
1503 int src1_hard_regnum, src2_hard_regnum;
1505 src1_hard_regnum = get_hard_regnum (regstack, *src1);
1506 src2_hard_regnum = get_hard_regnum (regstack, *src2);
1507 gcc_assert (src1_hard_regnum != -1);
1508 gcc_assert (src2_hard_regnum != -1);
1510 if (src1_hard_regnum != FIRST_STACK_REG
1511 && src2_hard_regnum != FIRST_STACK_REG)
1512 emit_swap_insn (insn, regstack, *dest);
1515 if (STACK_REG_P (*src1))
1516 replace_reg (src1, get_hard_regnum (regstack, *src1));
1517 if (STACK_REG_P (*src2))
1518 replace_reg (src2, get_hard_regnum (regstack, *src2));
1520 if (src1_note)
1522 rtx src1_reg = XEXP (src1_note, 0);
1524 /* If the register that dies is at the top of stack, then
1525 the destination is somewhere else - merely substitute it.
1526 But if the reg that dies is not at top of stack, then
1527 move the top of stack to the dead reg, as though we had
1528 done the insn and then a store-with-pop. */
1530 if (REGNO (src1_reg) == regstack->reg[regstack->top])
1532 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1533 replace_reg (dest, get_hard_regnum (regstack, *dest));
1535 else
1537 int regno = get_hard_regnum (regstack, src1_reg);
1539 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1540 replace_reg (dest, regno);
1542 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1543 = regstack->reg[regstack->top];
1546 CLEAR_HARD_REG_BIT (regstack->reg_set,
1547 REGNO (XEXP (src1_note, 0)));
1548 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1549 regstack->top--;
1551 else if (src2_note)
1553 rtx src2_reg = XEXP (src2_note, 0);
1554 if (REGNO (src2_reg) == regstack->reg[regstack->top])
1556 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1557 replace_reg (dest, get_hard_regnum (regstack, *dest));
1559 else
1561 int regno = get_hard_regnum (regstack, src2_reg);
1563 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1564 replace_reg (dest, regno);
1566 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1567 = regstack->reg[regstack->top];
1570 CLEAR_HARD_REG_BIT (regstack->reg_set,
1571 REGNO (XEXP (src2_note, 0)));
1572 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
1573 regstack->top--;
1575 else
1577 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1578 replace_reg (dest, get_hard_regnum (regstack, *dest));
1581 /* Keep operand 1 matching with destination. */
1582 if (COMMUTATIVE_ARITH_P (pat_src)
1583 && REG_P (*src1) && REG_P (*src2)
1584 && REGNO (*src1) != REGNO (*dest))
1586 int tmp = REGNO (*src1);
1587 replace_reg (src1, REGNO (*src2));
1588 replace_reg (src2, tmp);
1590 break;
1592 case UNSPEC:
1593 switch (XINT (pat_src, 1))
1595 case UNSPEC_FIST:
1597 case UNSPEC_FIST_FLOOR:
1598 case UNSPEC_FIST_CEIL:
1600 /* These insns only operate on the top of the stack. */
1602 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1603 emit_swap_insn (insn, regstack, *src1);
1605 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1607 if (STACK_REG_P (*dest))
1608 replace_reg (dest, FIRST_STACK_REG);
1610 if (src1_note)
1612 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1613 regstack->top--;
1614 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1617 replace_reg (src1, FIRST_STACK_REG);
1618 break;
1620 case UNSPEC_SIN:
1621 case UNSPEC_COS:
1622 case UNSPEC_FRNDINT:
1623 case UNSPEC_F2XM1:
1625 case UNSPEC_FRNDINT_FLOOR:
1626 case UNSPEC_FRNDINT_CEIL:
1627 case UNSPEC_FRNDINT_TRUNC:
1628 case UNSPEC_FRNDINT_MASK_PM:
1630 /* These insns only operate on the top of the stack. */
1632 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1634 emit_swap_insn (insn, regstack, *src1);
1636 /* Input should never die, it is
1637 replaced with output. */
1638 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1639 gcc_assert (!src1_note);
1641 if (STACK_REG_P (*dest))
1642 replace_reg (dest, FIRST_STACK_REG);
1644 replace_reg (src1, FIRST_STACK_REG);
1645 break;
1647 case UNSPEC_FPATAN:
1648 case UNSPEC_FYL2X:
1649 case UNSPEC_FYL2XP1:
1650 /* These insns operate on the top two stack slots. */
1652 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1653 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1655 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1656 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1658 swap_to_top (insn, regstack, *src1, *src2);
1660 replace_reg (src1, FIRST_STACK_REG);
1661 replace_reg (src2, FIRST_STACK_REG + 1);
1663 if (src1_note)
1664 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1665 if (src2_note)
1666 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1668 /* Pop both input operands from the stack. */
1669 CLEAR_HARD_REG_BIT (regstack->reg_set,
1670 regstack->reg[regstack->top]);
1671 CLEAR_HARD_REG_BIT (regstack->reg_set,
1672 regstack->reg[regstack->top - 1]);
1673 regstack->top -= 2;
1675 /* Push the result back onto the stack. */
1676 regstack->reg[++regstack->top] = REGNO (*dest);
1677 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1678 replace_reg (dest, FIRST_STACK_REG);
1679 break;
1681 case UNSPEC_FSCALE_FRACT:
1682 case UNSPEC_FPREM_F:
1683 case UNSPEC_FPREM1_F:
1684 /* These insns operate on the top two stack slots.
1685 first part of double input, double output insn. */
1687 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1688 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1690 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1691 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1693 /* Inputs should never die, they are
1694 replaced with outputs. */
1695 gcc_assert (!src1_note);
1696 gcc_assert (!src2_note);
1698 swap_to_top (insn, regstack, *src1, *src2);
1700 /* Push the result back onto stack. Empty stack slot
1701 will be filled in second part of insn. */
1702 if (STACK_REG_P (*dest)) {
1703 regstack->reg[regstack->top] = REGNO (*dest);
1704 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1705 replace_reg (dest, FIRST_STACK_REG);
1708 replace_reg (src1, FIRST_STACK_REG);
1709 replace_reg (src2, FIRST_STACK_REG + 1);
1710 break;
1712 case UNSPEC_FSCALE_EXP:
1713 case UNSPEC_FPREM_U:
1714 case UNSPEC_FPREM1_U:
1715 /* These insns operate on the top two stack slots./
1716 second part of double input, double output insn. */
1718 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1719 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1721 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1722 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1724 /* Inputs should never die, they are
1725 replaced with outputs. */
1726 gcc_assert (!src1_note);
1727 gcc_assert (!src2_note);
1729 swap_to_top (insn, regstack, *src1, *src2);
1731 /* Push the result back onto stack. Fill empty slot from
1732 first part of insn and fix top of stack pointer. */
1733 if (STACK_REG_P (*dest)) {
1734 regstack->reg[regstack->top - 1] = REGNO (*dest);
1735 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1736 replace_reg (dest, FIRST_STACK_REG + 1);
1739 replace_reg (src1, FIRST_STACK_REG);
1740 replace_reg (src2, FIRST_STACK_REG + 1);
1741 break;
1743 case UNSPEC_SINCOS_COS:
1744 case UNSPEC_TAN_ONE:
1745 case UNSPEC_XTRACT_FRACT:
1746 /* These insns operate on the top two stack slots,
1747 first part of one input, double output insn. */
1749 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1751 emit_swap_insn (insn, regstack, *src1);
1753 /* Input should never die, it is
1754 replaced with output. */
1755 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1756 gcc_assert (!src1_note);
1758 /* Push the result back onto stack. Empty stack slot
1759 will be filled in second part of insn. */
1760 if (STACK_REG_P (*dest)) {
1761 regstack->reg[regstack->top + 1] = REGNO (*dest);
1762 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1763 replace_reg (dest, FIRST_STACK_REG);
1766 replace_reg (src1, FIRST_STACK_REG);
1767 break;
1769 case UNSPEC_SINCOS_SIN:
1770 case UNSPEC_TAN_TAN:
1771 case UNSPEC_XTRACT_EXP:
1772 /* These insns operate on the top two stack slots,
1773 second part of one input, double output insn. */
1775 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1777 emit_swap_insn (insn, regstack, *src1);
1779 /* Input should never die, it is
1780 replaced with output. */
1781 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1782 gcc_assert (!src1_note);
1784 /* Push the result back onto stack. Fill empty slot from
1785 first part of insn and fix top of stack pointer. */
1786 if (STACK_REG_P (*dest)) {
1787 regstack->reg[regstack->top] = REGNO (*dest);
1788 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1789 replace_reg (dest, FIRST_STACK_REG + 1);
1791 regstack->top++;
1794 replace_reg (src1, FIRST_STACK_REG);
1795 break;
1797 case UNSPEC_SAHF:
1798 /* (unspec [(unspec [(compare)] UNSPEC_FNSTSW)] UNSPEC_SAHF)
1799 The combination matches the PPRO fcomi instruction. */
1801 pat_src = XVECEXP (pat_src, 0, 0);
1802 gcc_assert (GET_CODE (pat_src) == UNSPEC);
1803 gcc_assert (XINT (pat_src, 1) == UNSPEC_FNSTSW);
1804 /* Fall through. */
1806 case UNSPEC_FNSTSW:
1807 /* Combined fcomp+fnstsw generated for doing well with
1808 CSE. When optimizing this would have been broken
1809 up before now. */
1811 pat_src = XVECEXP (pat_src, 0, 0);
1812 gcc_assert (GET_CODE (pat_src) == COMPARE);
1814 compare_for_stack_reg (insn, regstack, pat_src);
1815 break;
1817 default:
1818 gcc_unreachable ();
1820 break;
1822 case IF_THEN_ELSE:
1823 /* This insn requires the top of stack to be the destination. */
1825 src1 = get_true_reg (&XEXP (pat_src, 1));
1826 src2 = get_true_reg (&XEXP (pat_src, 2));
1828 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1829 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1831 /* If the comparison operator is an FP comparison operator,
1832 it is handled correctly by compare_for_stack_reg () who
1833 will move the destination to the top of stack. But if the
1834 comparison operator is not an FP comparison operator, we
1835 have to handle it here. */
1836 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
1837 && REGNO (*dest) != regstack->reg[regstack->top])
1839 /* In case one of operands is the top of stack and the operands
1840 dies, it is safe to make it the destination operand by
1841 reversing the direction of cmove and avoid fxch. */
1842 if ((REGNO (*src1) == regstack->reg[regstack->top]
1843 && src1_note)
1844 || (REGNO (*src2) == regstack->reg[regstack->top]
1845 && src2_note))
1847 int idx1 = (get_hard_regnum (regstack, *src1)
1848 - FIRST_STACK_REG);
1849 int idx2 = (get_hard_regnum (regstack, *src2)
1850 - FIRST_STACK_REG);
1852 /* Make reg-stack believe that the operands are already
1853 swapped on the stack */
1854 regstack->reg[regstack->top - idx1] = REGNO (*src2);
1855 regstack->reg[regstack->top - idx2] = REGNO (*src1);
1857 /* Reverse condition to compensate the operand swap.
1858 i386 do have comparison always reversible. */
1859 PUT_CODE (XEXP (pat_src, 0),
1860 reversed_comparison_code (XEXP (pat_src, 0), insn));
1862 else
1863 emit_swap_insn (insn, regstack, *dest);
1867 rtx src_note [3];
1868 int i;
1870 src_note[0] = 0;
1871 src_note[1] = src1_note;
1872 src_note[2] = src2_note;
1874 if (STACK_REG_P (*src1))
1875 replace_reg (src1, get_hard_regnum (regstack, *src1));
1876 if (STACK_REG_P (*src2))
1877 replace_reg (src2, get_hard_regnum (regstack, *src2));
1879 for (i = 1; i <= 2; i++)
1880 if (src_note [i])
1882 int regno = REGNO (XEXP (src_note[i], 0));
1884 /* If the register that dies is not at the top of
1885 stack, then move the top of stack to the dead reg.
1886 Top of stack should never die, as it is the
1887 destination. */
1888 gcc_assert (regno != regstack->reg[regstack->top]);
1889 remove_regno_note (insn, REG_DEAD, regno);
1890 emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
1891 EMIT_AFTER);
1895 /* Make dest the top of stack. Add dest to regstack if
1896 not present. */
1897 if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
1898 regstack->reg[++regstack->top] = REGNO (*dest);
1899 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1900 replace_reg (dest, FIRST_STACK_REG);
1901 break;
1903 default:
1904 gcc_unreachable ();
1906 break;
1909 default:
1910 break;
1913 return control_flow_insn_deleted;
1916 /* Substitute hard regnums for any stack regs in INSN, which has
1917 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
1918 before the insn, and is updated with changes made here.
1920 There are several requirements and assumptions about the use of
1921 stack-like regs in asm statements. These rules are enforced by
1922 record_asm_stack_regs; see comments there for details. Any
1923 asm_operands left in the RTL at this point may be assume to meet the
1924 requirements, since record_asm_stack_regs removes any problem asm. */
1926 static void
1927 subst_asm_stack_regs (rtx insn, stack regstack)
1929 rtx body = PATTERN (insn);
1930 int alt;
1932 rtx *note_reg; /* Array of note contents */
1933 rtx **note_loc; /* Address of REG field of each note */
1934 enum reg_note *note_kind; /* The type of each note */
1936 rtx *clobber_reg = 0;
1937 rtx **clobber_loc = 0;
1939 struct stack_def temp_stack;
1940 int n_notes;
1941 int n_clobbers;
1942 rtx note;
1943 int i;
1944 int n_inputs, n_outputs;
1946 if (! check_asm_stack_operands (insn))
1947 return;
1949 /* Find out what the constraints required. If no constraint
1950 alternative matches, that is a compiler bug: we should have caught
1951 such an insn in check_asm_stack_operands. */
1952 extract_insn (insn);
1953 constrain_operands (1);
1954 alt = which_alternative;
1956 preprocess_constraints ();
1958 n_inputs = get_asm_operand_n_inputs (body);
1959 n_outputs = recog_data.n_operands - n_inputs;
1961 gcc_assert (alt >= 0);
1963 /* Strip SUBREGs here to make the following code simpler. */
1964 for (i = 0; i < recog_data.n_operands; i++)
1965 if (GET_CODE (recog_data.operand[i]) == SUBREG
1966 && REG_P (SUBREG_REG (recog_data.operand[i])))
1968 recog_data.operand_loc[i] = & SUBREG_REG (recog_data.operand[i]);
1969 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
1972 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
1974 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
1975 i++;
1977 note_reg = alloca (i * sizeof (rtx));
1978 note_loc = alloca (i * sizeof (rtx *));
1979 note_kind = alloca (i * sizeof (enum reg_note));
1981 n_notes = 0;
1982 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1984 rtx reg = XEXP (note, 0);
1985 rtx *loc = & XEXP (note, 0);
1987 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
1989 loc = & SUBREG_REG (reg);
1990 reg = SUBREG_REG (reg);
1993 if (STACK_REG_P (reg)
1994 && (REG_NOTE_KIND (note) == REG_DEAD
1995 || REG_NOTE_KIND (note) == REG_UNUSED))
1997 note_reg[n_notes] = reg;
1998 note_loc[n_notes] = loc;
1999 note_kind[n_notes] = REG_NOTE_KIND (note);
2000 n_notes++;
2004 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2006 n_clobbers = 0;
2008 if (GET_CODE (body) == PARALLEL)
2010 clobber_reg = alloca (XVECLEN (body, 0) * sizeof (rtx));
2011 clobber_loc = alloca (XVECLEN (body, 0) * sizeof (rtx *));
2013 for (i = 0; i < XVECLEN (body, 0); i++)
2014 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
2016 rtx clobber = XVECEXP (body, 0, i);
2017 rtx reg = XEXP (clobber, 0);
2018 rtx *loc = & XEXP (clobber, 0);
2020 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
2022 loc = & SUBREG_REG (reg);
2023 reg = SUBREG_REG (reg);
2026 if (STACK_REG_P (reg))
2028 clobber_reg[n_clobbers] = reg;
2029 clobber_loc[n_clobbers] = loc;
2030 n_clobbers++;
2035 temp_stack = *regstack;
2037 /* Put the input regs into the desired place in TEMP_STACK. */
2039 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2040 if (STACK_REG_P (recog_data.operand[i])
2041 && reg_class_subset_p (recog_op_alt[i][alt].cl,
2042 FLOAT_REGS)
2043 && recog_op_alt[i][alt].cl != FLOAT_REGS)
2045 /* If an operand needs to be in a particular reg in
2046 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2047 these constraints are for single register classes, and
2048 reload guaranteed that operand[i] is already in that class,
2049 we can just use REGNO (recog_data.operand[i]) to know which
2050 actual reg this operand needs to be in. */
2052 int regno = get_hard_regnum (&temp_stack, recog_data.operand[i]);
2054 gcc_assert (regno >= 0);
2056 if ((unsigned int) regno != REGNO (recog_data.operand[i]))
2058 /* recog_data.operand[i] is not in the right place. Find
2059 it and swap it with whatever is already in I's place.
2060 K is where recog_data.operand[i] is now. J is where it
2061 should be. */
2062 int j, k, temp;
2064 k = temp_stack.top - (regno - FIRST_STACK_REG);
2065 j = (temp_stack.top
2066 - (REGNO (recog_data.operand[i]) - FIRST_STACK_REG));
2068 temp = temp_stack.reg[k];
2069 temp_stack.reg[k] = temp_stack.reg[j];
2070 temp_stack.reg[j] = temp;
2074 /* Emit insns before INSN to make sure the reg-stack is in the right
2075 order. */
2077 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
2079 /* Make the needed input register substitutions. Do death notes and
2080 clobbers too, because these are for inputs, not outputs. */
2082 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2083 if (STACK_REG_P (recog_data.operand[i]))
2085 int regnum = get_hard_regnum (regstack, recog_data.operand[i]);
2087 gcc_assert (regnum >= 0);
2089 replace_reg (recog_data.operand_loc[i], regnum);
2092 for (i = 0; i < n_notes; i++)
2093 if (note_kind[i] == REG_DEAD)
2095 int regnum = get_hard_regnum (regstack, note_reg[i]);
2097 gcc_assert (regnum >= 0);
2099 replace_reg (note_loc[i], regnum);
2102 for (i = 0; i < n_clobbers; i++)
2104 /* It's OK for a CLOBBER to reference a reg that is not live.
2105 Don't try to replace it in that case. */
2106 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2108 if (regnum >= 0)
2110 /* Sigh - clobbers always have QImode. But replace_reg knows
2111 that these regs can't be MODE_INT and will assert. Just put
2112 the right reg there without calling replace_reg. */
2114 *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
2118 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2120 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2121 if (STACK_REG_P (recog_data.operand[i]))
2123 /* An input reg is implicitly popped if it is tied to an
2124 output, or if there is a CLOBBER for it. */
2125 int j;
2127 for (j = 0; j < n_clobbers; j++)
2128 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
2129 break;
2131 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
2133 /* recog_data.operand[i] might not be at the top of stack.
2134 But that's OK, because all we need to do is pop the
2135 right number of regs off of the top of the reg-stack.
2136 record_asm_stack_regs guaranteed that all implicitly
2137 popped regs were grouped at the top of the reg-stack. */
2139 CLEAR_HARD_REG_BIT (regstack->reg_set,
2140 regstack->reg[regstack->top]);
2141 regstack->top--;
2145 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2146 Note that there isn't any need to substitute register numbers.
2147 ??? Explain why this is true. */
2149 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2151 /* See if there is an output for this hard reg. */
2152 int j;
2154 for (j = 0; j < n_outputs; j++)
2155 if (STACK_REG_P (recog_data.operand[j])
2156 && REGNO (recog_data.operand[j]) == (unsigned) i)
2158 regstack->reg[++regstack->top] = i;
2159 SET_HARD_REG_BIT (regstack->reg_set, i);
2160 break;
2164 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2165 input that the asm didn't implicitly pop. If the asm didn't
2166 implicitly pop an input reg, that reg will still be live.
2168 Note that we can't use find_regno_note here: the register numbers
2169 in the death notes have already been substituted. */
2171 for (i = 0; i < n_outputs; i++)
2172 if (STACK_REG_P (recog_data.operand[i]))
2174 int j;
2176 for (j = 0; j < n_notes; j++)
2177 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2178 && note_kind[j] == REG_UNUSED)
2180 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2181 EMIT_AFTER);
2182 break;
2186 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2187 if (STACK_REG_P (recog_data.operand[i]))
2189 int j;
2191 for (j = 0; j < n_notes; j++)
2192 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2193 && note_kind[j] == REG_DEAD
2194 && TEST_HARD_REG_BIT (regstack->reg_set,
2195 REGNO (recog_data.operand[i])))
2197 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2198 EMIT_AFTER);
2199 break;
2204 /* Substitute stack hard reg numbers for stack virtual registers in
2205 INSN. Non-stack register numbers are not changed. REGSTACK is the
2206 current stack content. Insns may be emitted as needed to arrange the
2207 stack for the 387 based on the contents of the insn. Return whether
2208 a control flow insn was deleted in the process. */
2210 static bool
2211 subst_stack_regs (rtx insn, stack regstack)
2213 rtx *note_link, note;
2214 bool control_flow_insn_deleted = false;
2215 int i;
2217 if (CALL_P (insn))
2219 int top = regstack->top;
2221 /* If there are any floating point parameters to be passed in
2222 registers for this call, make sure they are in the right
2223 order. */
2225 if (top >= 0)
2227 straighten_stack (insn, regstack);
2229 /* Now mark the arguments as dead after the call. */
2231 while (regstack->top >= 0)
2233 CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
2234 regstack->top--;
2239 /* Do the actual substitution if any stack regs are mentioned.
2240 Since we only record whether entire insn mentions stack regs, and
2241 subst_stack_regs_pat only works for patterns that contain stack regs,
2242 we must check each pattern in a parallel here. A call_value_pop could
2243 fail otherwise. */
2245 if (stack_regs_mentioned (insn))
2247 int n_operands = asm_noperands (PATTERN (insn));
2248 if (n_operands >= 0)
2250 /* This insn is an `asm' with operands. Decode the operands,
2251 decide how many are inputs, and do register substitution.
2252 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2254 subst_asm_stack_regs (insn, regstack);
2255 return control_flow_insn_deleted;
2258 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2259 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2261 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2263 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
2264 XVECEXP (PATTERN (insn), 0, i)
2265 = shallow_copy_rtx (XVECEXP (PATTERN (insn), 0, i));
2266 control_flow_insn_deleted
2267 |= subst_stack_regs_pat (insn, regstack,
2268 XVECEXP (PATTERN (insn), 0, i));
2271 else
2272 control_flow_insn_deleted
2273 |= subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2276 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2277 REG_UNUSED will already have been dealt with, so just return. */
2279 if (NOTE_P (insn) || INSN_DELETED_P (insn))
2280 return control_flow_insn_deleted;
2282 /* If this a noreturn call, we can't insert pop insns after it.
2283 Instead, reset the stack state to empty. */
2284 if (CALL_P (insn)
2285 && find_reg_note (insn, REG_NORETURN, NULL))
2287 regstack->top = -1;
2288 CLEAR_HARD_REG_SET (regstack->reg_set);
2289 return control_flow_insn_deleted;
2292 /* If there is a REG_UNUSED note on a stack register on this insn,
2293 the indicated reg must be popped. The REG_UNUSED note is removed,
2294 since the form of the newly emitted pop insn references the reg,
2295 making it no longer `unset'. */
2297 note_link = &REG_NOTES (insn);
2298 for (note = *note_link; note; note = XEXP (note, 1))
2299 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2301 *note_link = XEXP (note, 1);
2302 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), EMIT_AFTER);
2304 else
2305 note_link = &XEXP (note, 1);
2307 return control_flow_insn_deleted;
2310 /* Change the organization of the stack so that it fits a new basic
2311 block. Some registers might have to be popped, but there can never be
2312 a register live in the new block that is not now live.
2314 Insert any needed insns before or after INSN, as indicated by
2315 WHERE. OLD is the original stack layout, and NEW is the desired
2316 form. OLD is updated to reflect the code emitted, i.e., it will be
2317 the same as NEW upon return.
2319 This function will not preserve block_end[]. But that information
2320 is no longer needed once this has executed. */
2322 static void
2323 change_stack (rtx insn, stack old, stack new, enum emit_where where)
2325 int reg;
2326 int update_end = 0;
2328 /* Stack adjustments for the first insn in a block update the
2329 current_block's stack_in instead of inserting insns directly.
2330 compensate_edges will add the necessary code later. */
2331 if (current_block
2332 && starting_stack_p
2333 && where == EMIT_BEFORE)
2335 BLOCK_INFO (current_block)->stack_in = *new;
2336 starting_stack_p = false;
2337 *old = *new;
2338 return;
2341 /* We will be inserting new insns "backwards". If we are to insert
2342 after INSN, find the next insn, and insert before it. */
2344 if (where == EMIT_AFTER)
2346 if (current_block && BB_END (current_block) == insn)
2347 update_end = 1;
2348 insn = NEXT_INSN (insn);
2351 /* Pop any registers that are not needed in the new block. */
2353 /* If the destination block's stack already has a specified layout
2354 and contains two or more registers, use a more intelligent algorithm
2355 to pop registers that minimizes the number number of fxchs below. */
2356 if (new->top > 0)
2358 bool slots[REG_STACK_SIZE];
2359 int pops[REG_STACK_SIZE];
2360 int next, dest, topsrc;
2362 /* First pass to determine the free slots. */
2363 for (reg = 0; reg <= new->top; reg++)
2364 slots[reg] = TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]);
2366 /* Second pass to allocate preferred slots. */
2367 topsrc = -1;
2368 for (reg = old->top; reg > new->top; reg--)
2369 if (TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2371 dest = -1;
2372 for (next = 0; next <= new->top; next++)
2373 if (!slots[next] && new->reg[next] == old->reg[reg])
2375 /* If this is a preference for the new top of stack, record
2376 the fact by remembering it's old->reg in topsrc. */
2377 if (next == new->top)
2378 topsrc = reg;
2379 slots[next] = true;
2380 dest = next;
2381 break;
2383 pops[reg] = dest;
2385 else
2386 pops[reg] = reg;
2388 /* Intentionally, avoid placing the top of stack in it's correct
2389 location, if we still need to permute the stack below and we
2390 can usefully place it somewhere else. This is the case if any
2391 slot is still unallocated, in which case we should place the
2392 top of stack there. */
2393 if (topsrc != -1)
2394 for (reg = 0; reg < new->top; reg++)
2395 if (!slots[reg])
2397 pops[topsrc] = reg;
2398 slots[new->top] = false;
2399 slots[reg] = true;
2400 break;
2403 /* Third pass allocates remaining slots and emits pop insns. */
2404 next = new->top;
2405 for (reg = old->top; reg > new->top; reg--)
2407 dest = pops[reg];
2408 if (dest == -1)
2410 /* Find next free slot. */
2411 while (slots[next])
2412 next--;
2413 dest = next--;
2415 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[dest], DFmode),
2416 EMIT_BEFORE);
2419 else
2421 /* The following loop attempts to maximize the number of times we
2422 pop the top of the stack, as this permits the use of the faster
2423 ffreep instruction on platforms that support it. */
2424 int live, next;
2426 live = 0;
2427 for (reg = 0; reg <= old->top; reg++)
2428 if (TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2429 live++;
2431 next = live;
2432 while (old->top >= live)
2433 if (TEST_HARD_REG_BIT (new->reg_set, old->reg[old->top]))
2435 while (TEST_HARD_REG_BIT (new->reg_set, old->reg[next]))
2436 next--;
2437 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[next], DFmode),
2438 EMIT_BEFORE);
2440 else
2441 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[old->top], DFmode),
2442 EMIT_BEFORE);
2445 if (new->top == -2)
2447 /* If the new block has never been processed, then it can inherit
2448 the old stack order. */
2450 new->top = old->top;
2451 memcpy (new->reg, old->reg, sizeof (new->reg));
2453 else
2455 /* This block has been entered before, and we must match the
2456 previously selected stack order. */
2458 /* By now, the only difference should be the order of the stack,
2459 not their depth or liveliness. */
2461 GO_IF_HARD_REG_EQUAL (old->reg_set, new->reg_set, win);
2462 gcc_unreachable ();
2463 win:
2464 gcc_assert (old->top == new->top);
2466 /* If the stack is not empty (new->top != -1), loop here emitting
2467 swaps until the stack is correct.
2469 The worst case number of swaps emitted is N + 2, where N is the
2470 depth of the stack. In some cases, the reg at the top of
2471 stack may be correct, but swapped anyway in order to fix
2472 other regs. But since we never swap any other reg away from
2473 its correct slot, this algorithm will converge. */
2475 if (new->top != -1)
2478 /* Swap the reg at top of stack into the position it is
2479 supposed to be in, until the correct top of stack appears. */
2481 while (old->reg[old->top] != new->reg[new->top])
2483 for (reg = new->top; reg >= 0; reg--)
2484 if (new->reg[reg] == old->reg[old->top])
2485 break;
2487 gcc_assert (reg != -1);
2489 emit_swap_insn (insn, old,
2490 FP_MODE_REG (old->reg[reg], DFmode));
2493 /* See if any regs remain incorrect. If so, bring an
2494 incorrect reg to the top of stack, and let the while loop
2495 above fix it. */
2497 for (reg = new->top; reg >= 0; reg--)
2498 if (new->reg[reg] != old->reg[reg])
2500 emit_swap_insn (insn, old,
2501 FP_MODE_REG (old->reg[reg], DFmode));
2502 break;
2504 } while (reg >= 0);
2506 /* At this point there must be no differences. */
2508 for (reg = old->top; reg >= 0; reg--)
2509 gcc_assert (old->reg[reg] == new->reg[reg]);
2512 if (update_end)
2513 BB_END (current_block) = PREV_INSN (insn);
2516 /* Print stack configuration. */
2518 static void
2519 print_stack (FILE *file, stack s)
2521 if (! file)
2522 return;
2524 if (s->top == -2)
2525 fprintf (file, "uninitialized\n");
2526 else if (s->top == -1)
2527 fprintf (file, "empty\n");
2528 else
2530 int i;
2531 fputs ("[ ", file);
2532 for (i = 0; i <= s->top; ++i)
2533 fprintf (file, "%d ", s->reg[i]);
2534 fputs ("]\n", file);
2538 /* This function was doing life analysis. We now let the regular live
2539 code do it's job, so we only need to check some extra invariants
2540 that reg-stack expects. Primary among these being that all registers
2541 are initialized before use.
2543 The function returns true when code was emitted to CFG edges and
2544 commit_edge_insertions needs to be called. */
2546 static int
2547 convert_regs_entry (void)
2549 int inserted = 0;
2550 edge e;
2551 edge_iterator ei;
2553 /* Load something into each stack register live at function entry.
2554 Such live registers can be caused by uninitialized variables or
2555 functions not returning values on all paths. In order to keep
2556 the push/pop code happy, and to not scrog the register stack, we
2557 must put something in these registers. Use a QNaN.
2559 Note that we are inserting converted code here. This code is
2560 never seen by the convert_regs pass. */
2562 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2564 basic_block block = e->dest;
2565 block_info bi = BLOCK_INFO (block);
2566 int reg, top = -1;
2568 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2569 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2571 rtx init;
2573 bi->stack_in.reg[++top] = reg;
2575 init = gen_rtx_SET (VOIDmode,
2576 FP_MODE_REG (FIRST_STACK_REG, SFmode),
2577 not_a_num);
2578 insert_insn_on_edge (init, e);
2579 inserted = 1;
2582 bi->stack_in.top = top;
2585 return inserted;
2588 /* Construct the desired stack for function exit. This will either
2589 be `empty', or the function return value at top-of-stack. */
2591 static void
2592 convert_regs_exit (void)
2594 int value_reg_low, value_reg_high;
2595 stack output_stack;
2596 rtx retvalue;
2598 retvalue = stack_result (current_function_decl);
2599 value_reg_low = value_reg_high = -1;
2600 if (retvalue)
2602 value_reg_low = REGNO (retvalue);
2603 value_reg_high = value_reg_low
2604 + hard_regno_nregs[value_reg_low][GET_MODE (retvalue)] - 1;
2607 output_stack = &BLOCK_INFO (EXIT_BLOCK_PTR)->stack_in;
2608 if (value_reg_low == -1)
2609 output_stack->top = -1;
2610 else
2612 int reg;
2614 output_stack->top = value_reg_high - value_reg_low;
2615 for (reg = value_reg_low; reg <= value_reg_high; ++reg)
2617 output_stack->reg[value_reg_high - reg] = reg;
2618 SET_HARD_REG_BIT (output_stack->reg_set, reg);
2623 /* Copy the stack info from the end of edge E's source block to the
2624 start of E's destination block. */
2626 static void
2627 propagate_stack (edge e)
2629 stack src_stack = &BLOCK_INFO (e->src)->stack_out;
2630 stack dest_stack = &BLOCK_INFO (e->dest)->stack_in;
2631 int reg;
2633 /* Preserve the order of the original stack, but check whether
2634 any pops are needed. */
2635 dest_stack->top = -1;
2636 for (reg = 0; reg <= src_stack->top; ++reg)
2637 if (TEST_HARD_REG_BIT (dest_stack->reg_set, src_stack->reg[reg]))
2638 dest_stack->reg[++dest_stack->top] = src_stack->reg[reg];
2642 /* Adjust the stack of edge E's source block on exit to match the stack
2643 of it's target block upon input. The stack layouts of both blocks
2644 should have been defined by now. */
2646 static bool
2647 compensate_edge (edge e, FILE *file)
2649 basic_block source = e->src, target = e->dest;
2650 stack target_stack = &BLOCK_INFO (target)->stack_in;
2651 stack source_stack = &BLOCK_INFO (source)->stack_out;
2652 struct stack_def regstack;
2653 int reg;
2655 if (file)
2656 fprintf (file, "Edge %d->%d: ", source->index, target->index);
2658 gcc_assert (target_stack->top != -2);
2660 /* Check whether stacks are identical. */
2661 if (target_stack->top == source_stack->top)
2663 for (reg = target_stack->top; reg >= 0; --reg)
2664 if (target_stack->reg[reg] != source_stack->reg[reg])
2665 break;
2667 if (reg == -1)
2669 if (file)
2670 fprintf (file, "no changes needed\n");
2671 return false;
2675 if (file)
2677 fprintf (file, "correcting stack to ");
2678 print_stack (file, target_stack);
2681 /* Abnormal calls may appear to have values live in st(0), but the
2682 abnormal return path will not have actually loaded the values. */
2683 if (e->flags & EDGE_ABNORMAL_CALL)
2685 /* Assert that the lifetimes are as we expect -- one value
2686 live at st(0) on the end of the source block, and no
2687 values live at the beginning of the destination block.
2688 For complex return values, we may have st(1) live as well. */
2689 gcc_assert (source_stack->top == 0 || source_stack->top == 1);
2690 gcc_assert (target_stack->top == -1);
2691 return false;
2694 /* Handle non-call EH edges specially. The normal return path have
2695 values in registers. These will be popped en masse by the unwind
2696 library. */
2697 if (e->flags & EDGE_EH)
2699 gcc_assert (target_stack->top == -1);
2700 return false;
2703 /* We don't support abnormal edges. Global takes care to
2704 avoid any live register across them, so we should never
2705 have to insert instructions on such edges. */
2706 gcc_assert (! (e->flags & EDGE_ABNORMAL));
2708 /* Make a copy of source_stack as change_stack is destructive. */
2709 regstack = *source_stack;
2711 /* It is better to output directly to the end of the block
2712 instead of to the edge, because emit_swap can do minimal
2713 insn scheduling. We can do this when there is only one
2714 edge out, and it is not abnormal. */
2715 if (EDGE_COUNT (source->succs) == 1)
2717 current_block = source;
2718 change_stack (BB_END (source), &regstack, target_stack,
2719 (JUMP_P (BB_END (source)) ? EMIT_BEFORE : EMIT_AFTER));
2721 else
2723 rtx seq, after;
2725 current_block = NULL;
2726 start_sequence ();
2728 /* ??? change_stack needs some point to emit insns after. */
2729 after = emit_note (NOTE_INSN_DELETED);
2731 change_stack (after, &regstack, target_stack, EMIT_BEFORE);
2733 seq = get_insns ();
2734 end_sequence ();
2736 insert_insn_on_edge (seq, e);
2737 return true;
2739 return false;
2742 /* Traverse all non-entry edges in the CFG, and emit the necessary
2743 edge compensation code to change the stack from stack_out of the
2744 source block to the stack_in of the destination block. */
2746 static bool
2747 compensate_edges (FILE *file)
2749 bool inserted = false;
2750 basic_block bb;
2752 starting_stack_p = false;
2754 FOR_EACH_BB (bb)
2755 if (bb != ENTRY_BLOCK_PTR)
2757 edge e;
2758 edge_iterator ei;
2760 FOR_EACH_EDGE (e, ei, bb->succs)
2761 inserted |= compensate_edge (e, file);
2763 return inserted;
2766 /* Select the better of two edges E1 and E2 to use to determine the
2767 stack layout for their shared destination basic block. This is
2768 typically the more frequently executed. The edge E1 may be NULL
2769 (in which case E2 is returned), but E2 is always non-NULL. */
2771 static edge
2772 better_edge (edge e1, edge e2)
2774 if (!e1)
2775 return e2;
2777 if (EDGE_FREQUENCY (e1) > EDGE_FREQUENCY (e2))
2778 return e1;
2779 if (EDGE_FREQUENCY (e1) < EDGE_FREQUENCY (e2))
2780 return e2;
2782 if (e1->count > e2->count)
2783 return e1;
2784 if (e1->count < e2->count)
2785 return e2;
2787 /* Prefer critical edges to minimize inserting compensation code on
2788 critical edges. */
2790 if (EDGE_CRITICAL_P (e1) != EDGE_CRITICAL_P (e2))
2791 return EDGE_CRITICAL_P (e1) ? e1 : e2;
2793 /* Avoid non-deterministic behavior. */
2794 return (e1->src->index < e2->src->index) ? e1 : e2;
2797 /* Convert stack register references in one block. */
2799 static void
2800 convert_regs_1 (FILE *file, basic_block block)
2802 struct stack_def regstack;
2803 block_info bi = BLOCK_INFO (block);
2804 int reg;
2805 rtx insn, next;
2806 bool control_flow_insn_deleted = false;
2808 any_malformed_asm = false;
2810 /* Choose an initial stack layout, if one hasn't already been chosen. */
2811 if (bi->stack_in.top == -2)
2813 edge e, beste = NULL;
2814 edge_iterator ei;
2816 /* Select the best incoming edge (typically the most frequent) to
2817 use as a template for this basic block. */
2818 FOR_EACH_EDGE (e, ei, block->preds)
2819 if (BLOCK_INFO (e->src)->done)
2820 beste = better_edge (beste, e);
2822 if (beste)
2823 propagate_stack (beste);
2824 else
2826 /* No predecessors. Create an arbitrary input stack. */
2827 bi->stack_in.top = -1;
2828 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2829 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2830 bi->stack_in.reg[++bi->stack_in.top] = reg;
2834 if (file)
2836 fprintf (file, "\nBasic block %d\nInput stack: ", block->index);
2837 print_stack (file, &bi->stack_in);
2840 /* Process all insns in this block. Keep track of NEXT so that we
2841 don't process insns emitted while substituting in INSN. */
2842 current_block = block;
2843 next = BB_HEAD (block);
2844 regstack = bi->stack_in;
2845 starting_stack_p = true;
2849 insn = next;
2850 next = NEXT_INSN (insn);
2852 /* Ensure we have not missed a block boundary. */
2853 gcc_assert (next);
2854 if (insn == BB_END (block))
2855 next = NULL;
2857 /* Don't bother processing unless there is a stack reg
2858 mentioned or if it's a CALL_INSN. */
2859 if (stack_regs_mentioned (insn)
2860 || CALL_P (insn))
2862 if (file)
2864 fprintf (file, " insn %d input stack: ",
2865 INSN_UID (insn));
2866 print_stack (file, &regstack);
2868 control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
2869 starting_stack_p = false;
2872 while (next);
2874 if (file)
2876 fprintf (file, "Expected live registers [");
2877 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
2878 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg))
2879 fprintf (file, " %d", reg);
2880 fprintf (file, " ]\nOutput stack: ");
2881 print_stack (file, &regstack);
2884 insn = BB_END (block);
2885 if (JUMP_P (insn))
2886 insn = PREV_INSN (insn);
2888 /* If the function is declared to return a value, but it returns one
2889 in only some cases, some registers might come live here. Emit
2890 necessary moves for them. */
2892 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
2894 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg)
2895 && ! TEST_HARD_REG_BIT (regstack.reg_set, reg))
2897 rtx set;
2899 if (file)
2900 fprintf (file, "Emitting insn initializing reg %d\n", reg);
2902 set = gen_rtx_SET (VOIDmode, FP_MODE_REG (reg, SFmode), not_a_num);
2903 insn = emit_insn_after (set, insn);
2904 control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
2908 /* Amongst the insns possibly deleted during the substitution process above,
2909 might have been the only trapping insn in the block. We purge the now
2910 possibly dead EH edges here to avoid an ICE from fixup_abnormal_edges,
2911 called at the end of convert_regs. The order in which we process the
2912 blocks ensures that we never delete an already processed edge.
2914 Note that, at this point, the CFG may have been damaged by the emission
2915 of instructions after an abnormal call, which moves the basic block end
2916 (and is the reason why we call fixup_abnormal_edges later). So we must
2917 be sure that the trapping insn has been deleted before trying to purge
2918 dead edges, otherwise we risk purging valid edges.
2920 ??? We are normally supposed not to delete trapping insns, so we pretend
2921 that the insns deleted above don't actually trap. It would have been
2922 better to detect this earlier and avoid creating the EH edge in the first
2923 place, still, but we don't have enough information at that time. */
2925 if (control_flow_insn_deleted)
2926 purge_dead_edges (block);
2928 /* Something failed if the stack lives don't match. If we had malformed
2929 asms, we zapped the instruction itself, but that didn't produce the
2930 same pattern of register kills as before. */
2931 GO_IF_HARD_REG_EQUAL (regstack.reg_set, bi->out_reg_set, win);
2932 gcc_assert (any_malformed_asm);
2933 win:
2934 bi->stack_out = regstack;
2935 bi->done = true;
2938 /* Convert registers in all blocks reachable from BLOCK. */
2940 static void
2941 convert_regs_2 (FILE *file, basic_block block)
2943 basic_block *stack, *sp;
2945 /* We process the blocks in a top-down manner, in a way such that one block
2946 is only processed after all its predecessors. The number of predecessors
2947 of every block has already been computed. */
2949 stack = xmalloc (sizeof (*stack) * n_basic_blocks);
2950 sp = stack;
2952 *sp++ = block;
2956 edge e;
2957 edge_iterator ei;
2959 block = *--sp;
2961 /* Processing BLOCK is achieved by convert_regs_1, which may purge
2962 some dead EH outgoing edge after the deletion of the trapping
2963 insn inside the block. Since the number of predecessors of
2964 BLOCK's successors was computed based on the initial edge set,
2965 we check the necessity to process some of these successors
2966 before such an edge deletion may happen. However, there is
2967 a pitfall: if BLOCK is the only predecessor of a successor and
2968 the edge between them happens to be deleted, the successor
2969 becomes unreachable and should not be processed. The problem
2970 is that there is no way to preventively detect this case so we
2971 stack the successor in all cases and hand over the task of
2972 fixing up the discrepancy to convert_regs_1. */
2974 FOR_EACH_EDGE (e, ei, block->succs)
2975 if (! (e->flags & EDGE_DFS_BACK))
2977 BLOCK_INFO (e->dest)->predecessors--;
2978 if (!BLOCK_INFO (e->dest)->predecessors)
2979 *sp++ = e->dest;
2982 convert_regs_1 (file, block);
2984 while (sp != stack);
2986 free (stack);
2989 /* Traverse all basic blocks in a function, converting the register
2990 references in each insn from the "flat" register file that gcc uses,
2991 to the stack-like registers the 387 uses. */
2993 static void
2994 convert_regs (FILE *file)
2996 int inserted;
2997 basic_block b;
2998 edge e;
2999 edge_iterator ei;
3001 /* Initialize uninitialized registers on function entry. */
3002 inserted = convert_regs_entry ();
3004 /* Construct the desired stack for function exit. */
3005 convert_regs_exit ();
3006 BLOCK_INFO (EXIT_BLOCK_PTR)->done = 1;
3008 /* ??? Future: process inner loops first, and give them arbitrary
3009 initial stacks which emit_swap_insn can modify. This ought to
3010 prevent double fxch that often appears at the head of a loop. */
3012 /* Process all blocks reachable from all entry points. */
3013 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3014 convert_regs_2 (file, e->dest);
3016 /* ??? Process all unreachable blocks. Though there's no excuse
3017 for keeping these even when not optimizing. */
3018 FOR_EACH_BB (b)
3020 block_info bi = BLOCK_INFO (b);
3022 if (! bi->done)
3023 convert_regs_2 (file, b);
3026 inserted |= compensate_edges (file);
3028 clear_aux_for_blocks ();
3030 fixup_abnormal_edges ();
3031 if (inserted)
3032 commit_edge_insertions ();
3034 if (file)
3035 fputc ('\n', file);
3038 /* Convert register usage from "flat" register file usage to a "stack
3039 register file. FILE is the dump file, if used.
3041 Construct a CFG and run life analysis. Then convert each insn one
3042 by one. Run a last cleanup_cfg pass, if optimizing, to eliminate
3043 code duplication created when the converter inserts pop insns on
3044 the edges. */
3046 bool
3047 reg_to_stack (FILE *file)
3049 basic_block bb;
3050 int i;
3051 int max_uid;
3053 /* Clean up previous run. */
3054 stack_regs_mentioned_data = 0;
3056 /* See if there is something to do. Flow analysis is quite
3057 expensive so we might save some compilation time. */
3058 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
3059 if (regs_ever_live[i])
3060 break;
3061 if (i > LAST_STACK_REG)
3062 return false;
3064 /* Ok, floating point instructions exist. If not optimizing,
3065 build the CFG and run life analysis.
3066 Also need to rebuild life when superblock scheduling is done
3067 as it don't update liveness yet. */
3068 if (!optimize
3069 || ((flag_sched2_use_superblocks || flag_sched2_use_traces)
3070 && flag_schedule_insns_after_reload))
3072 count_or_remove_death_notes (NULL, 1);
3073 life_analysis (file, PROP_DEATH_NOTES);
3075 mark_dfs_back_edges ();
3077 /* Set up block info for each basic block. */
3078 alloc_aux_for_blocks (sizeof (struct block_info_def));
3079 FOR_EACH_BB (bb)
3081 block_info bi = BLOCK_INFO (bb);
3082 edge_iterator ei;
3083 edge e;
3084 int reg;
3086 FOR_EACH_EDGE (e, ei, bb->preds)
3087 if (!(e->flags & EDGE_DFS_BACK)
3088 && e->src != ENTRY_BLOCK_PTR)
3089 bi->predecessors++;
3091 /* Set current register status at last instruction `uninitialized'. */
3092 bi->stack_in.top = -2;
3094 /* Copy live_at_end and live_at_start into temporaries. */
3095 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
3097 if (REGNO_REG_SET_P (bb->il.rtl->global_live_at_end, reg))
3098 SET_HARD_REG_BIT (bi->out_reg_set, reg);
3099 if (REGNO_REG_SET_P (bb->il.rtl->global_live_at_start, reg))
3100 SET_HARD_REG_BIT (bi->stack_in.reg_set, reg);
3104 /* Create the replacement registers up front. */
3105 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
3107 enum machine_mode mode;
3108 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3109 mode != VOIDmode;
3110 mode = GET_MODE_WIDER_MODE (mode))
3111 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
3112 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT);
3113 mode != VOIDmode;
3114 mode = GET_MODE_WIDER_MODE (mode))
3115 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
3118 ix86_flags_rtx = gen_rtx_REG (CCmode, FLAGS_REG);
3120 /* A QNaN for initializing uninitialized variables.
3122 ??? We can't load from constant memory in PIC mode, because
3123 we're inserting these instructions before the prologue and
3124 the PIC register hasn't been set up. In that case, fall back
3125 on zero, which we can get from `ldz'. */
3127 if (flag_pic)
3128 not_a_num = CONST0_RTX (SFmode);
3129 else
3131 not_a_num = gen_lowpart (SFmode, GEN_INT (0x7fc00000));
3132 not_a_num = force_const_mem (SFmode, not_a_num);
3135 /* Allocate a cache for stack_regs_mentioned. */
3136 max_uid = get_max_uid ();
3137 VARRAY_CHAR_INIT (stack_regs_mentioned_data, max_uid + 1,
3138 "stack_regs_mentioned cache");
3140 convert_regs (file);
3142 free_aux_for_blocks ();
3143 return true;
3145 #endif /* STACK_REGS */
3147 static bool
3148 gate_handle_stack_regs (void)
3150 #ifdef STACK_REGS
3151 return 1;
3152 #else
3153 return 0;
3154 #endif
3157 /* Convert register usage from flat register file usage to a stack
3158 register file. */
3159 static void
3160 rest_of_handle_stack_regs (void)
3162 #ifdef STACK_REGS
3163 if (reg_to_stack (dump_file) && optimize)
3165 if (cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK
3166 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0))
3167 && (flag_reorder_blocks || flag_reorder_blocks_and_partition))
3169 reorder_basic_blocks (0);
3170 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK);
3173 #endif
3176 struct tree_opt_pass pass_stack_regs =
3178 "stack", /* name */
3179 gate_handle_stack_regs, /* gate */
3180 rest_of_handle_stack_regs, /* execute */
3181 NULL, /* sub */
3182 NULL, /* next */
3183 0, /* static_pass_number */
3184 TV_REG_STACK, /* tv_id */
3185 0, /* properties_required */
3186 0, /* properties_provided */
3187 0, /* properties_destroyed */
3188 0, /* todo_flags_start */
3189 TODO_dump_func |
3190 TODO_ggc_collect, /* todo_flags_finish */
3191 'k' /* letter */
3194 #include "gt-reg-stack.h"