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)
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, 59 Temple Place - Suite 330, Boston, MA
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
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.
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
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
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
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)");
156 #include "coretypes.h"
161 #include "function.h"
162 #include "insn-config.h"
164 #include "hard-reg-set.h"
169 #include "basic-block.h"
174 /* We use this array to cache info about insns, because otherwise we
175 spend too much time in stack_regs_mentioned_p.
177 Indexed by insn UIDs. A value of zero is uninitialized, one indicates
178 the insn uses stack registers, two indicates the insn does not use
180 static GTY(()) varray_type stack_regs_mentioned_data;
184 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
186 /* This is the basic stack record. TOP is an index into REG[] such
187 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
189 If TOP is -2, REG[] is not yet initialized. Stack initialization
190 consists of placing each live reg in array `reg' and setting `top'
193 REG_SET indicates which registers are live. */
195 typedef struct stack_def
197 int top; /* index to top stack element */
198 HARD_REG_SET reg_set; /* set of live registers */
199 unsigned char reg[REG_STACK_SIZE];/* register - stack mapping */
202 /* This is used to carry information about basic blocks. It is
203 attached to the AUX field of the standard CFG block. */
205 typedef struct block_info_def
207 struct stack_def stack_in; /* Input stack configuration. */
208 struct stack_def stack_out; /* Output stack configuration. */
209 HARD_REG_SET out_reg_set; /* Stack regs live on output. */
210 int done; /* True if block already converted. */
211 int predecessors; /* Number of predecessors that needs
215 #define BLOCK_INFO(B) ((block_info) (B)->aux)
217 /* Passed to change_stack to indicate where to emit insns. */
224 /* The block we're currently working on. */
225 static basic_block current_block;
227 /* This is the register file for all register after conversion. */
229 FP_mode_reg[LAST_STACK_REG+1-FIRST_STACK_REG][(int) MAX_MACHINE_MODE];
231 #define FP_MODE_REG(regno,mode) \
232 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int) (mode)])
234 /* Used to initialize uninitialized registers. */
235 static rtx not_a_num;
237 /* Forward declarations */
239 static int stack_regs_mentioned_p (rtx pat);
240 static void straighten_stack (rtx, stack);
241 static void pop_stack (stack, int);
242 static rtx *get_true_reg (rtx *);
244 static int check_asm_stack_operands (rtx);
245 static int get_asm_operand_n_inputs (rtx);
246 static rtx stack_result (tree);
247 static void replace_reg (rtx *, int);
248 static void remove_regno_note (rtx, enum reg_note, unsigned int);
249 static int get_hard_regnum (stack, rtx);
250 static rtx emit_pop_insn (rtx, stack, rtx, enum emit_where);
251 static void emit_swap_insn (rtx, stack, rtx);
252 static void swap_to_top(rtx, stack, rtx, rtx);
253 static bool move_for_stack_reg (rtx, stack, rtx);
254 static bool move_nan_for_stack_reg (rtx, stack, rtx);
255 static int swap_rtx_condition_1 (rtx);
256 static int swap_rtx_condition (rtx);
257 static void compare_for_stack_reg (rtx, stack, rtx);
258 static bool subst_stack_regs_pat (rtx, stack, rtx);
259 static void subst_asm_stack_regs (rtx, stack);
260 static bool subst_stack_regs (rtx, stack);
261 static void change_stack (rtx, stack, stack, enum emit_where);
262 static void print_stack (FILE *, stack);
263 static rtx next_flags_user (rtx);
264 static bool compensate_edge (edge, FILE *);
266 /* Return nonzero if any stack register is mentioned somewhere within PAT. */
269 stack_regs_mentioned_p (rtx pat)
274 if (STACK_REG_P (pat))
277 fmt = GET_RTX_FORMAT (GET_CODE (pat));
278 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
284 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
285 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
288 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
295 /* Return nonzero if INSN mentions stacked registers, else return zero. */
298 stack_regs_mentioned (rtx insn)
300 unsigned int uid, max;
303 if (! INSN_P (insn) || !stack_regs_mentioned_data)
306 uid = INSN_UID (insn);
307 max = VARRAY_SIZE (stack_regs_mentioned_data);
310 /* Allocate some extra size to avoid too many reallocs, but
311 do not grow too quickly. */
312 max = uid + uid / 20;
313 VARRAY_GROW (stack_regs_mentioned_data, max);
316 test = VARRAY_CHAR (stack_regs_mentioned_data, uid);
319 /* This insn has yet to be examined. Do so now. */
320 test = stack_regs_mentioned_p (PATTERN (insn)) ? 1 : 2;
321 VARRAY_CHAR (stack_regs_mentioned_data, uid) = test;
327 static rtx ix86_flags_rtx;
330 next_flags_user (rtx insn)
332 /* Search forward looking for the first use of this value.
333 Stop at block boundaries. */
335 while (insn != BB_END (current_block))
337 insn = NEXT_INSN (insn);
339 if (INSN_P (insn) && reg_mentioned_p (ix86_flags_rtx, PATTERN (insn)))
348 /* Reorganize the stack into ascending numbers,
352 straighten_stack (rtx insn, stack regstack)
354 struct stack_def temp_stack;
357 /* If there is only a single register on the stack, then the stack is
358 already in increasing order and no reorganization is needed.
360 Similarly if the stack is empty. */
361 if (regstack->top <= 0)
364 COPY_HARD_REG_SET (temp_stack.reg_set, regstack->reg_set);
366 for (top = temp_stack.top = regstack->top; top >= 0; top--)
367 temp_stack.reg[top] = FIRST_STACK_REG + temp_stack.top - top;
369 change_stack (insn, regstack, &temp_stack, EMIT_AFTER);
372 /* Pop a register from the stack. */
375 pop_stack (stack regstack, int regno)
377 int top = regstack->top;
379 CLEAR_HARD_REG_BIT (regstack->reg_set, regno);
381 /* If regno was not at the top of stack then adjust stack. */
382 if (regstack->reg [top] != regno)
385 for (i = regstack->top; i >= 0; i--)
386 if (regstack->reg [i] == regno)
389 for (j = i; j < top; j++)
390 regstack->reg [j] = regstack->reg [j + 1];
396 /* Return a pointer to the REG expression within PAT. If PAT is not a
397 REG, possible enclosed by a conversion rtx, return the inner part of
398 PAT that stopped the search. */
401 get_true_reg (rtx *pat)
404 switch (GET_CODE (*pat))
407 /* Eliminate FP subregister accesses in favor of the
408 actual FP register in use. */
411 if (FP_REG_P (subreg = SUBREG_REG (*pat)))
413 int regno_off = subreg_regno_offset (REGNO (subreg),
417 *pat = FP_MODE_REG (REGNO (subreg) + regno_off,
426 pat = & XEXP (*pat, 0);
430 if (!flag_unsafe_math_optimizations)
432 pat = & XEXP (*pat, 0);
437 /* Set if we find any malformed asms in a block. */
438 static bool any_malformed_asm;
440 /* There are many rules that an asm statement for stack-like regs must
441 follow. Those rules are explained at the top of this file: the rule
442 numbers below refer to that explanation. */
445 check_asm_stack_operands (rtx insn)
449 int malformed_asm = 0;
450 rtx body = PATTERN (insn);
452 char reg_used_as_output[FIRST_PSEUDO_REGISTER];
453 char implicitly_dies[FIRST_PSEUDO_REGISTER];
456 rtx *clobber_reg = 0;
457 int n_inputs, n_outputs;
459 /* Find out what the constraints require. If no constraint
460 alternative matches, this asm is malformed. */
462 constrain_operands (1);
463 alt = which_alternative;
465 preprocess_constraints ();
467 n_inputs = get_asm_operand_n_inputs (body);
468 n_outputs = recog_data.n_operands - n_inputs;
473 /* Avoid further trouble with this insn. */
474 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
478 /* Strip SUBREGs here to make the following code simpler. */
479 for (i = 0; i < recog_data.n_operands; i++)
480 if (GET_CODE (recog_data.operand[i]) == SUBREG
481 && REG_P (SUBREG_REG (recog_data.operand[i])))
482 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
484 /* Set up CLOBBER_REG. */
488 if (GET_CODE (body) == PARALLEL)
490 clobber_reg = alloca (XVECLEN (body, 0) * sizeof (rtx));
492 for (i = 0; i < XVECLEN (body, 0); i++)
493 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
495 rtx clobber = XVECEXP (body, 0, i);
496 rtx reg = XEXP (clobber, 0);
498 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
499 reg = SUBREG_REG (reg);
501 if (STACK_REG_P (reg))
503 clobber_reg[n_clobbers] = reg;
509 /* Enforce rule #4: Output operands must specifically indicate which
510 reg an output appears in after an asm. "=f" is not allowed: the
511 operand constraints must select a class with a single reg.
513 Also enforce rule #5: Output operands must start at the top of
514 the reg-stack: output operands may not "skip" a reg. */
516 memset (reg_used_as_output, 0, sizeof (reg_used_as_output));
517 for (i = 0; i < n_outputs; i++)
518 if (STACK_REG_P (recog_data.operand[i]))
520 if (reg_class_size[(int) recog_op_alt[i][alt].cl] != 1)
522 error_for_asm (insn, "output constraint %d must specify a single register", i);
529 for (j = 0; j < n_clobbers; j++)
530 if (REGNO (recog_data.operand[i]) == REGNO (clobber_reg[j]))
532 error_for_asm (insn, "output constraint %d cannot be specified together with \"%s\" clobber",
533 i, reg_names [REGNO (clobber_reg[j])]);
538 reg_used_as_output[REGNO (recog_data.operand[i])] = 1;
543 /* Search for first non-popped reg. */
544 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
545 if (! reg_used_as_output[i])
548 /* If there are any other popped regs, that's an error. */
549 for (; i < LAST_STACK_REG + 1; i++)
550 if (reg_used_as_output[i])
553 if (i != LAST_STACK_REG + 1)
555 error_for_asm (insn, "output regs must be grouped at top of stack");
559 /* Enforce rule #2: All implicitly popped input regs must be closer
560 to the top of the reg-stack than any input that is not implicitly
563 memset (implicitly_dies, 0, sizeof (implicitly_dies));
564 for (i = n_outputs; i < n_outputs + n_inputs; i++)
565 if (STACK_REG_P (recog_data.operand[i]))
567 /* An input reg is implicitly popped if it is tied to an
568 output, or if there is a CLOBBER for it. */
571 for (j = 0; j < n_clobbers; j++)
572 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
575 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
576 implicitly_dies[REGNO (recog_data.operand[i])] = 1;
579 /* Search for first non-popped reg. */
580 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
581 if (! implicitly_dies[i])
584 /* If there are any other popped regs, that's an error. */
585 for (; i < LAST_STACK_REG + 1; i++)
586 if (implicitly_dies[i])
589 if (i != LAST_STACK_REG + 1)
592 "implicitly popped regs must be grouped at top of stack");
596 /* Enforce rule #3: If any input operand uses the "f" constraint, all
597 output constraints must use the "&" earlyclobber.
599 ??? Detect this more deterministically by having constrain_asm_operands
600 record any earlyclobber. */
602 for (i = n_outputs; i < n_outputs + n_inputs; i++)
603 if (recog_op_alt[i][alt].matches == -1)
607 for (j = 0; j < n_outputs; j++)
608 if (operands_match_p (recog_data.operand[j], recog_data.operand[i]))
611 "output operand %d must use %<&%> constraint", j);
618 /* Avoid further trouble with this insn. */
619 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
620 any_malformed_asm = true;
627 /* Calculate the number of inputs and outputs in BODY, an
628 asm_operands. N_OPERANDS is the total number of operands, and
629 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
633 get_asm_operand_n_inputs (rtx body)
635 switch (GET_CODE (body))
638 gcc_assert (GET_CODE (SET_SRC (body)) == ASM_OPERANDS);
639 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body));
642 return ASM_OPERANDS_INPUT_LENGTH (body);
645 return get_asm_operand_n_inputs (XVECEXP (body, 0, 0));
652 /* If current function returns its result in an fp stack register,
653 return the REG. Otherwise, return 0. */
656 stack_result (tree decl)
660 /* If the value is supposed to be returned in memory, then clearly
661 it is not returned in a stack register. */
662 if (aggregate_value_p (DECL_RESULT (decl), decl))
665 result = DECL_RTL_IF_SET (DECL_RESULT (decl));
668 #ifdef FUNCTION_OUTGOING_VALUE
670 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
672 result = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
676 return result != 0 && STACK_REG_P (result) ? result : 0;
681 * This section deals with stack register substitution, and forms the second
685 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
686 the desired hard REGNO. */
689 replace_reg (rtx *reg, int regno)
691 gcc_assert (regno >= FIRST_STACK_REG);
692 gcc_assert (regno <= LAST_STACK_REG);
693 gcc_assert (STACK_REG_P (*reg));
695 gcc_assert (GET_MODE_CLASS (GET_MODE (*reg)) == MODE_FLOAT
696 || GET_MODE_CLASS (GET_MODE (*reg)) == MODE_COMPLEX_FLOAT);
698 *reg = FP_MODE_REG (regno, GET_MODE (*reg));
701 /* Remove a note of type NOTE, which must be found, for register
702 number REGNO from INSN. Remove only one such note. */
705 remove_regno_note (rtx insn, enum reg_note note, unsigned int regno)
707 rtx *note_link, this;
709 note_link = ®_NOTES (insn);
710 for (this = *note_link; this; this = XEXP (this, 1))
711 if (REG_NOTE_KIND (this) == note
712 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno)
714 *note_link = XEXP (this, 1);
718 note_link = &XEXP (this, 1);
723 /* Find the hard register number of virtual register REG in REGSTACK.
724 The hard register number is relative to the top of the stack. -1 is
725 returned if the register is not found. */
728 get_hard_regnum (stack regstack, rtx reg)
732 gcc_assert (STACK_REG_P (reg));
734 for (i = regstack->top; i >= 0; i--)
735 if (regstack->reg[i] == REGNO (reg))
738 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
741 /* Emit an insn to pop virtual register REG before or after INSN.
742 REGSTACK is the stack state after INSN and is updated to reflect this
743 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
744 is represented as a SET whose destination is the register to be popped
745 and source is the top of stack. A death note for the top of stack
746 cases the movdf pattern to pop. */
749 emit_pop_insn (rtx insn, stack regstack, rtx reg, enum emit_where where)
751 rtx pop_insn, pop_rtx;
754 /* For complex types take care to pop both halves. These may survive in
755 CLOBBER and USE expressions. */
756 if (COMPLEX_MODE_P (GET_MODE (reg)))
758 rtx reg1 = FP_MODE_REG (REGNO (reg), DFmode);
759 rtx reg2 = FP_MODE_REG (REGNO (reg) + 1, DFmode);
762 if (get_hard_regnum (regstack, reg1) >= 0)
763 pop_insn = emit_pop_insn (insn, regstack, reg1, where);
764 if (get_hard_regnum (regstack, reg2) >= 0)
765 pop_insn = emit_pop_insn (insn, regstack, reg2, where);
766 gcc_assert (pop_insn);
770 hard_regno = get_hard_regnum (regstack, reg);
772 gcc_assert (hard_regno >= FIRST_STACK_REG);
774 pop_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG (hard_regno, DFmode),
775 FP_MODE_REG (FIRST_STACK_REG, DFmode));
777 if (where == EMIT_AFTER)
778 pop_insn = emit_insn_after (pop_rtx, insn);
780 pop_insn = emit_insn_before (pop_rtx, insn);
783 = gen_rtx_EXPR_LIST (REG_DEAD, FP_MODE_REG (FIRST_STACK_REG, DFmode),
784 REG_NOTES (pop_insn));
786 regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
787 = regstack->reg[regstack->top];
789 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
794 /* Emit an insn before or after INSN to swap virtual register REG with
795 the top of stack. REGSTACK is the stack state before the swap, and
796 is updated to reflect the swap. A swap insn is represented as a
797 PARALLEL of two patterns: each pattern moves one reg to the other.
799 If REG is already at the top of the stack, no insn is emitted. */
802 emit_swap_insn (rtx insn, stack regstack, rtx reg)
806 int tmp, other_reg; /* swap regno temps */
807 rtx i1; /* the stack-reg insn prior to INSN */
808 rtx i1set = NULL_RTX; /* the SET rtx within I1 */
810 hard_regno = get_hard_regnum (regstack, reg);
812 gcc_assert (hard_regno >= FIRST_STACK_REG);
813 if (hard_regno == FIRST_STACK_REG)
816 other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
818 tmp = regstack->reg[other_reg];
819 regstack->reg[other_reg] = regstack->reg[regstack->top];
820 regstack->reg[regstack->top] = tmp;
822 /* Find the previous insn involving stack regs, but don't pass a
825 if (current_block && insn != BB_HEAD (current_block))
827 rtx tmp = PREV_INSN (insn);
828 rtx limit = PREV_INSN (BB_HEAD (current_block));
833 || NOTE_INSN_BASIC_BLOCK_P (tmp)
834 || (NONJUMP_INSN_P (tmp)
835 && stack_regs_mentioned (tmp)))
840 tmp = PREV_INSN (tmp);
845 && (i1set = single_set (i1)) != NULL_RTX)
847 rtx i1src = *get_true_reg (&SET_SRC (i1set));
848 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
850 /* If the previous register stack push was from the reg we are to
851 swap with, omit the swap. */
853 if (REG_P (i1dest) && REGNO (i1dest) == FIRST_STACK_REG
855 && REGNO (i1src) == (unsigned) hard_regno - 1
856 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
859 /* If the previous insn wrote to the reg we are to swap with,
862 if (REG_P (i1dest) && REGNO (i1dest) == (unsigned) hard_regno
863 && REG_P (i1src) && REGNO (i1src) == FIRST_STACK_REG
864 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
868 swap_rtx = gen_swapxf (FP_MODE_REG (hard_regno, XFmode),
869 FP_MODE_REG (FIRST_STACK_REG, XFmode));
872 emit_insn_after (swap_rtx, i1);
873 else if (current_block)
874 emit_insn_before (swap_rtx, BB_HEAD (current_block));
876 emit_insn_before (swap_rtx, insn);
879 /* Emit an insns before INSN to swap virtual register SRC1 with
880 the top of stack and virtual register SRC2 with second stack
881 slot. REGSTACK is the stack state before the swaps, and
882 is updated to reflect the swaps. A swap insn is represented as a
883 PARALLEL of two patterns: each pattern moves one reg to the other.
885 If SRC1 and/or SRC2 are already at the right place, no swap insn
889 swap_to_top (rtx insn, stack regstack, rtx src1, rtx src2)
891 struct stack_def temp_stack;
892 int regno, j, k, temp;
894 temp_stack = *regstack;
896 /* Place operand 1 at the top of stack. */
897 regno = get_hard_regnum (&temp_stack, src1);
898 gcc_assert (regno >= 0);
899 if (regno != FIRST_STACK_REG)
901 k = temp_stack.top - (regno - FIRST_STACK_REG);
904 temp = temp_stack.reg[k];
905 temp_stack.reg[k] = temp_stack.reg[j];
906 temp_stack.reg[j] = temp;
909 /* Place operand 2 next on the stack. */
910 regno = get_hard_regnum (&temp_stack, src2);
911 gcc_assert (regno >= 0);
912 if (regno != FIRST_STACK_REG + 1)
914 k = temp_stack.top - (regno - FIRST_STACK_REG);
915 j = temp_stack.top - 1;
917 temp = temp_stack.reg[k];
918 temp_stack.reg[k] = temp_stack.reg[j];
919 temp_stack.reg[j] = temp;
922 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
925 /* Handle a move to or from a stack register in PAT, which is in INSN.
926 REGSTACK is the current stack. Return whether a control flow insn
927 was deleted in the process. */
930 move_for_stack_reg (rtx insn, stack regstack, rtx pat)
932 rtx *psrc = get_true_reg (&SET_SRC (pat));
933 rtx *pdest = get_true_reg (&SET_DEST (pat));
936 bool control_flow_insn_deleted = false;
938 src = *psrc; dest = *pdest;
940 if (STACK_REG_P (src) && STACK_REG_P (dest))
942 /* Write from one stack reg to another. If SRC dies here, then
943 just change the register mapping and delete the insn. */
945 note = find_regno_note (insn, REG_DEAD, REGNO (src));
950 /* If this is a no-op move, there must not be a REG_DEAD note. */
951 gcc_assert (REGNO (src) != REGNO (dest));
953 for (i = regstack->top; i >= 0; i--)
954 if (regstack->reg[i] == REGNO (src))
957 /* The destination must be dead, or life analysis is borked. */
958 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
960 /* If the source is not live, this is yet another case of
961 uninitialized variables. Load up a NaN instead. */
963 return move_nan_for_stack_reg (insn, regstack, dest);
965 /* It is possible that the dest is unused after this insn.
966 If so, just pop the src. */
968 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
969 emit_pop_insn (insn, regstack, src, EMIT_AFTER);
972 regstack->reg[i] = REGNO (dest);
973 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
974 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
977 control_flow_insn_deleted |= control_flow_insn_p (insn);
979 return control_flow_insn_deleted;
982 /* The source reg does not die. */
984 /* If this appears to be a no-op move, delete it, or else it
985 will confuse the machine description output patterns. But if
986 it is REG_UNUSED, we must pop the reg now, as per-insn processing
987 for REG_UNUSED will not work for deleted insns. */
989 if (REGNO (src) == REGNO (dest))
991 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
992 emit_pop_insn (insn, regstack, dest, EMIT_AFTER);
994 control_flow_insn_deleted |= control_flow_insn_p (insn);
996 return control_flow_insn_deleted;
999 /* The destination ought to be dead. */
1000 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
1002 replace_reg (psrc, get_hard_regnum (regstack, src));
1004 regstack->reg[++regstack->top] = REGNO (dest);
1005 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1006 replace_reg (pdest, FIRST_STACK_REG);
1008 else if (STACK_REG_P (src))
1010 /* Save from a stack reg to MEM, or possibly integer reg. Since
1011 only top of stack may be saved, emit an exchange first if
1014 emit_swap_insn (insn, regstack, src);
1016 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1019 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1021 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1023 else if ((GET_MODE (src) == XFmode)
1024 && regstack->top < REG_STACK_SIZE - 1)
1026 /* A 387 cannot write an XFmode value to a MEM without
1027 clobbering the source reg. The output code can handle
1028 this by reading back the value from the MEM.
1029 But it is more efficient to use a temp register if one is
1030 available. Push the source value here if the register
1031 stack is not full, and then write the value to memory via
1034 rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, GET_MODE (src));
1036 push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
1037 emit_insn_before (push_rtx, insn);
1038 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD, top_stack_reg,
1042 replace_reg (psrc, FIRST_STACK_REG);
1046 gcc_assert (STACK_REG_P (dest));
1048 /* Load from MEM, or possibly integer REG or constant, into the
1049 stack regs. The actual target is always the top of the
1050 stack. The stack mapping is changed to reflect that DEST is
1051 now at top of stack. */
1053 /* The destination ought to be dead. */
1054 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
1056 gcc_assert (regstack->top < REG_STACK_SIZE);
1058 regstack->reg[++regstack->top] = REGNO (dest);
1059 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1060 replace_reg (pdest, FIRST_STACK_REG);
1063 return control_flow_insn_deleted;
1066 /* A helper function which replaces INSN with a pattern that loads up
1067 a NaN into DEST, then invokes move_for_stack_reg. */
1070 move_nan_for_stack_reg (rtx insn, stack regstack, rtx dest)
1074 dest = FP_MODE_REG (REGNO (dest), SFmode);
1075 pat = gen_rtx_SET (VOIDmode, dest, not_a_num);
1076 PATTERN (insn) = pat;
1077 INSN_CODE (insn) = -1;
1079 return move_for_stack_reg (insn, regstack, pat);
1082 /* Swap the condition on a branch, if there is one. Return true if we
1083 found a condition to swap. False if the condition was not used as
1087 swap_rtx_condition_1 (rtx pat)
1092 if (COMPARISON_P (pat))
1094 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1099 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1100 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1106 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1107 r |= swap_rtx_condition_1 (XVECEXP (pat, i, j));
1109 else if (fmt[i] == 'e')
1110 r |= swap_rtx_condition_1 (XEXP (pat, i));
1118 swap_rtx_condition (rtx insn)
1120 rtx pat = PATTERN (insn);
1122 /* We're looking for a single set to cc0 or an HImode temporary. */
1124 if (GET_CODE (pat) == SET
1125 && REG_P (SET_DEST (pat))
1126 && REGNO (SET_DEST (pat)) == FLAGS_REG)
1128 insn = next_flags_user (insn);
1129 if (insn == NULL_RTX)
1131 pat = PATTERN (insn);
1134 /* See if this is, or ends in, a fnstsw. If so, we're not doing anything
1135 with the cc value right now. We may be able to search for one
1138 if (GET_CODE (pat) == SET
1139 && GET_CODE (SET_SRC (pat)) == UNSPEC
1140 && XINT (SET_SRC (pat), 1) == UNSPEC_FNSTSW)
1142 rtx dest = SET_DEST (pat);
1144 /* Search forward looking for the first use of this value.
1145 Stop at block boundaries. */
1146 while (insn != BB_END (current_block))
1148 insn = NEXT_INSN (insn);
1149 if (INSN_P (insn) && reg_mentioned_p (dest, insn))
1155 /* We haven't found it. */
1156 if (insn == BB_END (current_block))
1159 /* So we've found the insn using this value. If it is anything
1160 other than sahf or the value does not die (meaning we'd have
1161 to search further), then we must give up. */
1162 pat = PATTERN (insn);
1163 if (GET_CODE (pat) != SET
1164 || GET_CODE (SET_SRC (pat)) != UNSPEC
1165 || XINT (SET_SRC (pat), 1) != UNSPEC_SAHF
1166 || ! dead_or_set_p (insn, dest))
1169 /* Now we are prepared to handle this as a normal cc0 setter. */
1170 insn = next_flags_user (insn);
1171 if (insn == NULL_RTX)
1173 pat = PATTERN (insn);
1176 if (swap_rtx_condition_1 (pat))
1179 INSN_CODE (insn) = -1;
1180 if (recog_memoized (insn) == -1)
1182 /* In case the flags don't die here, recurse to try fix
1183 following user too. */
1184 else if (! dead_or_set_p (insn, ix86_flags_rtx))
1186 insn = next_flags_user (insn);
1187 if (!insn || !swap_rtx_condition (insn))
1192 swap_rtx_condition_1 (pat);
1200 /* Handle a comparison. Special care needs to be taken to avoid
1201 causing comparisons that a 387 cannot do correctly, such as EQ.
1203 Also, a pop insn may need to be emitted. The 387 does have an
1204 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1205 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1209 compare_for_stack_reg (rtx insn, stack regstack, rtx pat_src)
1212 rtx src1_note, src2_note;
1214 src1 = get_true_reg (&XEXP (pat_src, 0));
1215 src2 = get_true_reg (&XEXP (pat_src, 1));
1217 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1218 registers that die in this insn - move those to stack top first. */
1219 if ((! STACK_REG_P (*src1)
1220 || (STACK_REG_P (*src2)
1221 && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
1222 && swap_rtx_condition (insn))
1225 temp = XEXP (pat_src, 0);
1226 XEXP (pat_src, 0) = XEXP (pat_src, 1);
1227 XEXP (pat_src, 1) = temp;
1229 src1 = get_true_reg (&XEXP (pat_src, 0));
1230 src2 = get_true_reg (&XEXP (pat_src, 1));
1232 INSN_CODE (insn) = -1;
1235 /* We will fix any death note later. */
1237 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1239 if (STACK_REG_P (*src2))
1240 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1242 src2_note = NULL_RTX;
1244 emit_swap_insn (insn, regstack, *src1);
1246 replace_reg (src1, FIRST_STACK_REG);
1248 if (STACK_REG_P (*src2))
1249 replace_reg (src2, get_hard_regnum (regstack, *src2));
1253 pop_stack (regstack, REGNO (XEXP (src1_note, 0)));
1254 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1257 /* If the second operand dies, handle that. But if the operands are
1258 the same stack register, don't bother, because only one death is
1259 needed, and it was just handled. */
1262 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
1263 && REGNO (*src1) == REGNO (*src2)))
1265 /* As a special case, two regs may die in this insn if src2 is
1266 next to top of stack and the top of stack also dies. Since
1267 we have already popped src1, "next to top of stack" is really
1268 at top (FIRST_STACK_REG) now. */
1270 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1273 pop_stack (regstack, REGNO (XEXP (src2_note, 0)));
1274 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1278 /* The 386 can only represent death of the first operand in
1279 the case handled above. In all other cases, emit a separate
1280 pop and remove the death note from here. */
1282 /* link_cc0_insns (insn); */
1284 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
1286 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
1292 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1293 is the current register layout. Return whether a control flow insn
1294 was deleted in the process. */
1297 subst_stack_regs_pat (rtx insn, stack regstack, rtx pat)
1300 bool control_flow_insn_deleted = false;
1302 switch (GET_CODE (pat))
1305 /* Deaths in USE insns can happen in non optimizing compilation.
1306 Handle them by popping the dying register. */
1307 src = get_true_reg (&XEXP (pat, 0));
1308 if (STACK_REG_P (*src)
1309 && find_regno_note (insn, REG_DEAD, REGNO (*src)))
1311 emit_pop_insn (insn, regstack, *src, EMIT_AFTER);
1312 return control_flow_insn_deleted;
1314 /* ??? Uninitialized USE should not happen. */
1316 gcc_assert (get_hard_regnum (regstack, *src) != -1);
1323 dest = get_true_reg (&XEXP (pat, 0));
1324 if (STACK_REG_P (*dest))
1326 note = find_reg_note (insn, REG_DEAD, *dest);
1328 if (pat != PATTERN (insn))
1330 /* The fix_truncdi_1 pattern wants to be able to allocate
1331 its own scratch register. It does this by clobbering
1332 an fp reg so that it is assured of an empty reg-stack
1333 register. If the register is live, kill it now.
1334 Remove the DEAD/UNUSED note so we don't try to kill it
1338 emit_pop_insn (insn, regstack, *dest, EMIT_BEFORE);
1341 note = find_reg_note (insn, REG_UNUSED, *dest);
1344 remove_note (insn, note);
1345 replace_reg (dest, FIRST_STACK_REG + 1);
1349 /* A top-level clobber with no REG_DEAD, and no hard-regnum
1350 indicates an uninitialized value. Because reload removed
1351 all other clobbers, this must be due to a function
1352 returning without a value. Load up a NaN. */
1357 if (get_hard_regnum (regstack, t) == -1)
1358 control_flow_insn_deleted
1359 |= move_nan_for_stack_reg (insn, regstack, t);
1360 if (COMPLEX_MODE_P (GET_MODE (t)))
1362 t = FP_MODE_REG (REGNO (t) + 1, DFmode);
1363 if (get_hard_regnum (regstack, t) == -1)
1364 control_flow_insn_deleted
1365 |= move_nan_for_stack_reg (insn, regstack, t);
1375 rtx *src1 = (rtx *) 0, *src2;
1376 rtx src1_note, src2_note;
1379 dest = get_true_reg (&SET_DEST (pat));
1380 src = get_true_reg (&SET_SRC (pat));
1381 pat_src = SET_SRC (pat);
1383 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1384 if (STACK_REG_P (*src)
1385 || (STACK_REG_P (*dest)
1386 && (REG_P (*src) || MEM_P (*src)
1387 || GET_CODE (*src) == CONST_DOUBLE)))
1389 control_flow_insn_deleted |= move_for_stack_reg (insn, regstack, pat);
1393 switch (GET_CODE (pat_src))
1396 compare_for_stack_reg (insn, regstack, pat_src);
1402 for (count = hard_regno_nregs[REGNO (*dest)][GET_MODE (*dest)];
1405 regstack->reg[++regstack->top] = REGNO (*dest) + count;
1406 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
1409 replace_reg (dest, FIRST_STACK_REG);
1413 /* This is a `tstM2' case. */
1414 gcc_assert (*dest == cc0_rtx);
1419 case FLOAT_TRUNCATE:
1423 /* These insns only operate on the top of the stack. DEST might
1424 be cc0_rtx if we're processing a tstM pattern. Also, it's
1425 possible that the tstM case results in a REG_DEAD note on the
1429 src1 = get_true_reg (&XEXP (pat_src, 0));
1431 emit_swap_insn (insn, regstack, *src1);
1433 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1435 if (STACK_REG_P (*dest))
1436 replace_reg (dest, FIRST_STACK_REG);
1440 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1442 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1445 replace_reg (src1, FIRST_STACK_REG);
1450 /* On i386, reversed forms of subM3 and divM3 exist for
1451 MODE_FLOAT, so the same code that works for addM3 and mulM3
1455 /* These insns can accept the top of stack as a destination
1456 from a stack reg or mem, or can use the top of stack as a
1457 source and some other stack register (possibly top of stack)
1458 as a destination. */
1460 src1 = get_true_reg (&XEXP (pat_src, 0));
1461 src2 = get_true_reg (&XEXP (pat_src, 1));
1463 /* We will fix any death note later. */
1465 if (STACK_REG_P (*src1))
1466 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1468 src1_note = NULL_RTX;
1469 if (STACK_REG_P (*src2))
1470 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1472 src2_note = NULL_RTX;
1474 /* If either operand is not a stack register, then the dest
1475 must be top of stack. */
1477 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
1478 emit_swap_insn (insn, regstack, *dest);
1481 /* Both operands are REG. If neither operand is already
1482 at the top of stack, choose to make the one that is the dest
1483 the new top of stack. */
1485 int src1_hard_regnum, src2_hard_regnum;
1487 src1_hard_regnum = get_hard_regnum (regstack, *src1);
1488 src2_hard_regnum = get_hard_regnum (regstack, *src2);
1489 gcc_assert (src1_hard_regnum != -1);
1490 gcc_assert (src2_hard_regnum != -1);
1492 if (src1_hard_regnum != FIRST_STACK_REG
1493 && src2_hard_regnum != FIRST_STACK_REG)
1494 emit_swap_insn (insn, regstack, *dest);
1497 if (STACK_REG_P (*src1))
1498 replace_reg (src1, get_hard_regnum (regstack, *src1));
1499 if (STACK_REG_P (*src2))
1500 replace_reg (src2, get_hard_regnum (regstack, *src2));
1504 rtx src1_reg = XEXP (src1_note, 0);
1506 /* If the register that dies is at the top of stack, then
1507 the destination is somewhere else - merely substitute it.
1508 But if the reg that dies is not at top of stack, then
1509 move the top of stack to the dead reg, as though we had
1510 done the insn and then a store-with-pop. */
1512 if (REGNO (src1_reg) == regstack->reg[regstack->top])
1514 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1515 replace_reg (dest, get_hard_regnum (regstack, *dest));
1519 int regno = get_hard_regnum (regstack, src1_reg);
1521 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1522 replace_reg (dest, regno);
1524 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1525 = regstack->reg[regstack->top];
1528 CLEAR_HARD_REG_BIT (regstack->reg_set,
1529 REGNO (XEXP (src1_note, 0)));
1530 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1535 rtx src2_reg = XEXP (src2_note, 0);
1536 if (REGNO (src2_reg) == regstack->reg[regstack->top])
1538 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1539 replace_reg (dest, get_hard_regnum (regstack, *dest));
1543 int regno = get_hard_regnum (regstack, src2_reg);
1545 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1546 replace_reg (dest, regno);
1548 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1549 = regstack->reg[regstack->top];
1552 CLEAR_HARD_REG_BIT (regstack->reg_set,
1553 REGNO (XEXP (src2_note, 0)));
1554 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
1559 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1560 replace_reg (dest, get_hard_regnum (regstack, *dest));
1563 /* Keep operand 1 matching with destination. */
1564 if (COMMUTATIVE_ARITH_P (pat_src)
1565 && REG_P (*src1) && REG_P (*src2)
1566 && REGNO (*src1) != REGNO (*dest))
1568 int tmp = REGNO (*src1);
1569 replace_reg (src1, REGNO (*src2));
1570 replace_reg (src2, tmp);
1575 switch (XINT (pat_src, 1))
1579 case UNSPEC_FIST_FLOOR:
1580 case UNSPEC_FIST_CEIL:
1582 /* These insns only operate on the top of the stack. */
1584 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1585 emit_swap_insn (insn, regstack, *src1);
1587 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1589 if (STACK_REG_P (*dest))
1590 replace_reg (dest, FIRST_STACK_REG);
1594 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1596 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1599 replace_reg (src1, FIRST_STACK_REG);
1604 case UNSPEC_FRNDINT:
1607 case UNSPEC_FRNDINT_FLOOR:
1608 case UNSPEC_FRNDINT_CEIL:
1609 case UNSPEC_FRNDINT_TRUNC:
1610 case UNSPEC_FRNDINT_MASK_PM:
1612 /* These insns only operate on the top of the stack. */
1614 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1616 emit_swap_insn (insn, regstack, *src1);
1618 /* Input should never die, it is
1619 replaced with output. */
1620 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1621 gcc_assert (!src1_note);
1623 if (STACK_REG_P (*dest))
1624 replace_reg (dest, FIRST_STACK_REG);
1626 replace_reg (src1, FIRST_STACK_REG);
1631 case UNSPEC_FYL2XP1:
1632 /* These insns operate on the top two stack slots. */
1634 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1635 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1637 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1638 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1640 swap_to_top (insn, regstack, *src1, *src2);
1642 replace_reg (src1, FIRST_STACK_REG);
1643 replace_reg (src2, FIRST_STACK_REG + 1);
1646 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1648 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1650 /* Pop both input operands from the stack. */
1651 CLEAR_HARD_REG_BIT (regstack->reg_set,
1652 regstack->reg[regstack->top]);
1653 CLEAR_HARD_REG_BIT (regstack->reg_set,
1654 regstack->reg[regstack->top - 1]);
1657 /* Push the result back onto the stack. */
1658 regstack->reg[++regstack->top] = REGNO (*dest);
1659 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1660 replace_reg (dest, FIRST_STACK_REG);
1663 case UNSPEC_FSCALE_FRACT:
1664 case UNSPEC_FPREM_F:
1665 case UNSPEC_FPREM1_F:
1666 /* These insns operate on the top two stack slots.
1667 first part of double input, double output insn. */
1669 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1670 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1672 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1673 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1675 /* Inputs should never die, they are
1676 replaced with outputs. */
1677 gcc_assert (!src1_note);
1678 gcc_assert (!src2_note);
1680 swap_to_top (insn, regstack, *src1, *src2);
1682 /* Push the result back onto stack. Empty stack slot
1683 will be filled in second part of insn. */
1684 if (STACK_REG_P (*dest)) {
1685 regstack->reg[regstack->top] = REGNO (*dest);
1686 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1687 replace_reg (dest, FIRST_STACK_REG);
1690 replace_reg (src1, FIRST_STACK_REG);
1691 replace_reg (src2, FIRST_STACK_REG + 1);
1694 case UNSPEC_FSCALE_EXP:
1695 case UNSPEC_FPREM_U:
1696 case UNSPEC_FPREM1_U:
1697 /* These insns operate on the top two stack slots./
1698 second part of double input, double output insn. */
1700 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1701 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1703 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1704 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1706 /* Inputs should never die, they are
1707 replaced with outputs. */
1708 gcc_assert (!src1_note);
1709 gcc_assert (!src2_note);
1711 swap_to_top (insn, regstack, *src1, *src2);
1713 /* Push the result back onto stack. Fill empty slot from
1714 first part of insn and fix top of stack pointer. */
1715 if (STACK_REG_P (*dest)) {
1716 regstack->reg[regstack->top - 1] = REGNO (*dest);
1717 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1718 replace_reg (dest, FIRST_STACK_REG + 1);
1721 replace_reg (src1, FIRST_STACK_REG);
1722 replace_reg (src2, FIRST_STACK_REG + 1);
1725 case UNSPEC_SINCOS_COS:
1726 case UNSPEC_TAN_ONE:
1727 case UNSPEC_XTRACT_FRACT:
1728 /* These insns operate on the top two stack slots,
1729 first part of one input, double output insn. */
1731 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1733 emit_swap_insn (insn, regstack, *src1);
1735 /* Input should never die, it is
1736 replaced with output. */
1737 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1738 gcc_assert (!src1_note);
1740 /* Push the result back onto stack. Empty stack slot
1741 will be filled in second part of insn. */
1742 if (STACK_REG_P (*dest)) {
1743 regstack->reg[regstack->top + 1] = REGNO (*dest);
1744 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1745 replace_reg (dest, FIRST_STACK_REG);
1748 replace_reg (src1, FIRST_STACK_REG);
1751 case UNSPEC_SINCOS_SIN:
1752 case UNSPEC_TAN_TAN:
1753 case UNSPEC_XTRACT_EXP:
1754 /* These insns operate on the top two stack slots,
1755 second part of one input, double output insn. */
1757 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1759 emit_swap_insn (insn, regstack, *src1);
1761 /* Input should never die, it is
1762 replaced with output. */
1763 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1764 gcc_assert (!src1_note);
1766 /* Push the result back onto stack. Fill empty slot from
1767 first part of insn and fix top of stack pointer. */
1768 if (STACK_REG_P (*dest)) {
1769 regstack->reg[regstack->top] = REGNO (*dest);
1770 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1771 replace_reg (dest, FIRST_STACK_REG + 1);
1776 replace_reg (src1, FIRST_STACK_REG);
1780 /* (unspec [(unspec [(compare)] UNSPEC_FNSTSW)] UNSPEC_SAHF)
1781 The combination matches the PPRO fcomi instruction. */
1783 pat_src = XVECEXP (pat_src, 0, 0);
1784 gcc_assert (GET_CODE (pat_src) == UNSPEC);
1785 gcc_assert (XINT (pat_src, 1) == UNSPEC_FNSTSW);
1789 /* Combined fcomp+fnstsw generated for doing well with
1790 CSE. When optimizing this would have been broken
1793 pat_src = XVECEXP (pat_src, 0, 0);
1794 gcc_assert (GET_CODE (pat_src) == COMPARE);
1796 compare_for_stack_reg (insn, regstack, pat_src);
1805 /* This insn requires the top of stack to be the destination. */
1807 src1 = get_true_reg (&XEXP (pat_src, 1));
1808 src2 = get_true_reg (&XEXP (pat_src, 2));
1810 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1811 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1813 /* If the comparison operator is an FP comparison operator,
1814 it is handled correctly by compare_for_stack_reg () who
1815 will move the destination to the top of stack. But if the
1816 comparison operator is not an FP comparison operator, we
1817 have to handle it here. */
1818 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
1819 && REGNO (*dest) != regstack->reg[regstack->top])
1821 /* In case one of operands is the top of stack and the operands
1822 dies, it is safe to make it the destination operand by
1823 reversing the direction of cmove and avoid fxch. */
1824 if ((REGNO (*src1) == regstack->reg[regstack->top]
1826 || (REGNO (*src2) == regstack->reg[regstack->top]
1829 int idx1 = (get_hard_regnum (regstack, *src1)
1831 int idx2 = (get_hard_regnum (regstack, *src2)
1834 /* Make reg-stack believe that the operands are already
1835 swapped on the stack */
1836 regstack->reg[regstack->top - idx1] = REGNO (*src2);
1837 regstack->reg[regstack->top - idx2] = REGNO (*src1);
1839 /* Reverse condition to compensate the operand swap.
1840 i386 do have comparison always reversible. */
1841 PUT_CODE (XEXP (pat_src, 0),
1842 reversed_comparison_code (XEXP (pat_src, 0), insn));
1845 emit_swap_insn (insn, regstack, *dest);
1853 src_note[1] = src1_note;
1854 src_note[2] = src2_note;
1856 if (STACK_REG_P (*src1))
1857 replace_reg (src1, get_hard_regnum (regstack, *src1));
1858 if (STACK_REG_P (*src2))
1859 replace_reg (src2, get_hard_regnum (regstack, *src2));
1861 for (i = 1; i <= 2; i++)
1864 int regno = REGNO (XEXP (src_note[i], 0));
1866 /* If the register that dies is not at the top of
1867 stack, then move the top of stack to the dead reg.
1868 Top of stack should never die, as it is the
1870 gcc_assert (regno != regstack->reg[regstack->top]);
1871 remove_regno_note (insn, REG_DEAD, regno);
1872 emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
1877 /* Make dest the top of stack. Add dest to regstack if
1879 if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
1880 regstack->reg[++regstack->top] = REGNO (*dest);
1881 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1882 replace_reg (dest, FIRST_STACK_REG);
1895 return control_flow_insn_deleted;
1898 /* Substitute hard regnums for any stack regs in INSN, which has
1899 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
1900 before the insn, and is updated with changes made here.
1902 There are several requirements and assumptions about the use of
1903 stack-like regs in asm statements. These rules are enforced by
1904 record_asm_stack_regs; see comments there for details. Any
1905 asm_operands left in the RTL at this point may be assume to meet the
1906 requirements, since record_asm_stack_regs removes any problem asm. */
1909 subst_asm_stack_regs (rtx insn, stack regstack)
1911 rtx body = PATTERN (insn);
1914 rtx *note_reg; /* Array of note contents */
1915 rtx **note_loc; /* Address of REG field of each note */
1916 enum reg_note *note_kind; /* The type of each note */
1918 rtx *clobber_reg = 0;
1919 rtx **clobber_loc = 0;
1921 struct stack_def temp_stack;
1926 int n_inputs, n_outputs;
1928 if (! check_asm_stack_operands (insn))
1931 /* Find out what the constraints required. If no constraint
1932 alternative matches, that is a compiler bug: we should have caught
1933 such an insn in check_asm_stack_operands. */
1934 extract_insn (insn);
1935 constrain_operands (1);
1936 alt = which_alternative;
1938 preprocess_constraints ();
1940 n_inputs = get_asm_operand_n_inputs (body);
1941 n_outputs = recog_data.n_operands - n_inputs;
1943 gcc_assert (alt >= 0);
1945 /* Strip SUBREGs here to make the following code simpler. */
1946 for (i = 0; i < recog_data.n_operands; i++)
1947 if (GET_CODE (recog_data.operand[i]) == SUBREG
1948 && REG_P (SUBREG_REG (recog_data.operand[i])))
1950 recog_data.operand_loc[i] = & SUBREG_REG (recog_data.operand[i]);
1951 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
1954 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
1956 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
1959 note_reg = alloca (i * sizeof (rtx));
1960 note_loc = alloca (i * sizeof (rtx *));
1961 note_kind = alloca (i * sizeof (enum reg_note));
1964 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1966 rtx reg = XEXP (note, 0);
1967 rtx *loc = & XEXP (note, 0);
1969 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
1971 loc = & SUBREG_REG (reg);
1972 reg = SUBREG_REG (reg);
1975 if (STACK_REG_P (reg)
1976 && (REG_NOTE_KIND (note) == REG_DEAD
1977 || REG_NOTE_KIND (note) == REG_UNUSED))
1979 note_reg[n_notes] = reg;
1980 note_loc[n_notes] = loc;
1981 note_kind[n_notes] = REG_NOTE_KIND (note);
1986 /* Set up CLOBBER_REG and CLOBBER_LOC. */
1990 if (GET_CODE (body) == PARALLEL)
1992 clobber_reg = alloca (XVECLEN (body, 0) * sizeof (rtx));
1993 clobber_loc = alloca (XVECLEN (body, 0) * sizeof (rtx *));
1995 for (i = 0; i < XVECLEN (body, 0); i++)
1996 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
1998 rtx clobber = XVECEXP (body, 0, i);
1999 rtx reg = XEXP (clobber, 0);
2000 rtx *loc = & XEXP (clobber, 0);
2002 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
2004 loc = & SUBREG_REG (reg);
2005 reg = SUBREG_REG (reg);
2008 if (STACK_REG_P (reg))
2010 clobber_reg[n_clobbers] = reg;
2011 clobber_loc[n_clobbers] = loc;
2017 temp_stack = *regstack;
2019 /* Put the input regs into the desired place in TEMP_STACK. */
2021 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2022 if (STACK_REG_P (recog_data.operand[i])
2023 && reg_class_subset_p (recog_op_alt[i][alt].cl,
2025 && recog_op_alt[i][alt].cl != FLOAT_REGS)
2027 /* If an operand needs to be in a particular reg in
2028 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2029 these constraints are for single register classes, and
2030 reload guaranteed that operand[i] is already in that class,
2031 we can just use REGNO (recog_data.operand[i]) to know which
2032 actual reg this operand needs to be in. */
2034 int regno = get_hard_regnum (&temp_stack, recog_data.operand[i]);
2036 gcc_assert (regno >= 0);
2038 if ((unsigned int) regno != REGNO (recog_data.operand[i]))
2040 /* recog_data.operand[i] is not in the right place. Find
2041 it and swap it with whatever is already in I's place.
2042 K is where recog_data.operand[i] is now. J is where it
2046 k = temp_stack.top - (regno - FIRST_STACK_REG);
2048 - (REGNO (recog_data.operand[i]) - FIRST_STACK_REG));
2050 temp = temp_stack.reg[k];
2051 temp_stack.reg[k] = temp_stack.reg[j];
2052 temp_stack.reg[j] = temp;
2056 /* Emit insns before INSN to make sure the reg-stack is in the right
2059 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
2061 /* Make the needed input register substitutions. Do death notes and
2062 clobbers too, because these are for inputs, not outputs. */
2064 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2065 if (STACK_REG_P (recog_data.operand[i]))
2067 int regnum = get_hard_regnum (regstack, recog_data.operand[i]);
2069 gcc_assert (regnum >= 0);
2071 replace_reg (recog_data.operand_loc[i], regnum);
2074 for (i = 0; i < n_notes; i++)
2075 if (note_kind[i] == REG_DEAD)
2077 int regnum = get_hard_regnum (regstack, note_reg[i]);
2079 gcc_assert (regnum >= 0);
2081 replace_reg (note_loc[i], regnum);
2084 for (i = 0; i < n_clobbers; i++)
2086 /* It's OK for a CLOBBER to reference a reg that is not live.
2087 Don't try to replace it in that case. */
2088 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2092 /* Sigh - clobbers always have QImode. But replace_reg knows
2093 that these regs can't be MODE_INT and will assert. Just put
2094 the right reg there without calling replace_reg. */
2096 *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
2100 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2102 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2103 if (STACK_REG_P (recog_data.operand[i]))
2105 /* An input reg is implicitly popped if it is tied to an
2106 output, or if there is a CLOBBER for it. */
2109 for (j = 0; j < n_clobbers; j++)
2110 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
2113 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
2115 /* recog_data.operand[i] might not be at the top of stack.
2116 But that's OK, because all we need to do is pop the
2117 right number of regs off of the top of the reg-stack.
2118 record_asm_stack_regs guaranteed that all implicitly
2119 popped regs were grouped at the top of the reg-stack. */
2121 CLEAR_HARD_REG_BIT (regstack->reg_set,
2122 regstack->reg[regstack->top]);
2127 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2128 Note that there isn't any need to substitute register numbers.
2129 ??? Explain why this is true. */
2131 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2133 /* See if there is an output for this hard reg. */
2136 for (j = 0; j < n_outputs; j++)
2137 if (STACK_REG_P (recog_data.operand[j])
2138 && REGNO (recog_data.operand[j]) == (unsigned) i)
2140 regstack->reg[++regstack->top] = i;
2141 SET_HARD_REG_BIT (regstack->reg_set, i);
2146 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2147 input that the asm didn't implicitly pop. If the asm didn't
2148 implicitly pop an input reg, that reg will still be live.
2150 Note that we can't use find_regno_note here: the register numbers
2151 in the death notes have already been substituted. */
2153 for (i = 0; i < n_outputs; i++)
2154 if (STACK_REG_P (recog_data.operand[i]))
2158 for (j = 0; j < n_notes; j++)
2159 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2160 && note_kind[j] == REG_UNUSED)
2162 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2168 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2169 if (STACK_REG_P (recog_data.operand[i]))
2173 for (j = 0; j < n_notes; j++)
2174 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2175 && note_kind[j] == REG_DEAD
2176 && TEST_HARD_REG_BIT (regstack->reg_set,
2177 REGNO (recog_data.operand[i])))
2179 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2186 /* Substitute stack hard reg numbers for stack virtual registers in
2187 INSN. Non-stack register numbers are not changed. REGSTACK is the
2188 current stack content. Insns may be emitted as needed to arrange the
2189 stack for the 387 based on the contents of the insn. Return whether
2190 a control flow insn was deleted in the process. */
2193 subst_stack_regs (rtx insn, stack regstack)
2195 rtx *note_link, note;
2196 bool control_flow_insn_deleted = false;
2201 int top = regstack->top;
2203 /* If there are any floating point parameters to be passed in
2204 registers for this call, make sure they are in the right
2209 straighten_stack (PREV_INSN (insn), regstack);
2211 /* Now mark the arguments as dead after the call. */
2213 while (regstack->top >= 0)
2215 CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
2221 /* Do the actual substitution if any stack regs are mentioned.
2222 Since we only record whether entire insn mentions stack regs, and
2223 subst_stack_regs_pat only works for patterns that contain stack regs,
2224 we must check each pattern in a parallel here. A call_value_pop could
2227 if (stack_regs_mentioned (insn))
2229 int n_operands = asm_noperands (PATTERN (insn));
2230 if (n_operands >= 0)
2232 /* This insn is an `asm' with operands. Decode the operands,
2233 decide how many are inputs, and do register substitution.
2234 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2236 subst_asm_stack_regs (insn, regstack);
2237 return control_flow_insn_deleted;
2240 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2241 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2243 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2245 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
2246 XVECEXP (PATTERN (insn), 0, i)
2247 = shallow_copy_rtx (XVECEXP (PATTERN (insn), 0, i));
2248 control_flow_insn_deleted
2249 |= subst_stack_regs_pat (insn, regstack,
2250 XVECEXP (PATTERN (insn), 0, i));
2254 control_flow_insn_deleted
2255 |= subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2258 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2259 REG_UNUSED will already have been dealt with, so just return. */
2261 if (NOTE_P (insn) || INSN_DELETED_P (insn))
2262 return control_flow_insn_deleted;
2264 /* If there is a REG_UNUSED note on a stack register on this insn,
2265 the indicated reg must be popped. The REG_UNUSED note is removed,
2266 since the form of the newly emitted pop insn references the reg,
2267 making it no longer `unset'. */
2269 note_link = ®_NOTES (insn);
2270 for (note = *note_link; note; note = XEXP (note, 1))
2271 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2273 *note_link = XEXP (note, 1);
2274 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), EMIT_AFTER);
2277 note_link = &XEXP (note, 1);
2279 return control_flow_insn_deleted;
2282 /* Change the organization of the stack so that it fits a new basic
2283 block. Some registers might have to be popped, but there can never be
2284 a register live in the new block that is not now live.
2286 Insert any needed insns before or after INSN, as indicated by
2287 WHERE. OLD is the original stack layout, and NEW is the desired
2288 form. OLD is updated to reflect the code emitted, i.e., it will be
2289 the same as NEW upon return.
2291 This function will not preserve block_end[]. But that information
2292 is no longer needed once this has executed. */
2295 change_stack (rtx insn, stack old, stack new, enum emit_where where)
2300 /* We will be inserting new insns "backwards". If we are to insert
2301 after INSN, find the next insn, and insert before it. */
2303 if (where == EMIT_AFTER)
2305 if (current_block && BB_END (current_block) == insn)
2307 insn = NEXT_INSN (insn);
2310 /* Pop any registers that are not needed in the new block. */
2312 /* If the destination block's stack already has a specified layout
2313 and contains two or more registers, use a more intelligent algorithm
2314 to pop registers that minimizes the number number of fxchs below. */
2317 bool slots[REG_STACK_SIZE];
2318 int pops[REG_STACK_SIZE];
2319 int next, dest, topsrc;
2321 /* First pass to determine the free slots. */
2322 for (reg = 0; reg <= new->top; reg++)
2323 slots[reg] = TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]);
2325 /* Second pass to allocate preferred slots. */
2327 for (reg = old->top; reg > new->top; reg--)
2328 if (TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2331 for (next = 0; next <= new->top; next++)
2332 if (!slots[next] && new->reg[next] == old->reg[reg])
2334 /* If this is a preference for the new top of stack, record
2335 the fact by remembering it's old->reg in topsrc. */
2336 if (next == new->top)
2347 /* Intentionally, avoid placing the top of stack in it's correct
2348 location, if we still need to permute the stack below and we
2349 can usefully place it somewhere else. This is the case if any
2350 slot is still unallocated, in which case we should place the
2351 top of stack there. */
2353 for (reg = 0; reg < new->top; reg++)
2357 slots[new->top] = false;
2362 /* Third pass allocates remaining slots and emits pop insns. */
2364 for (reg = old->top; reg > new->top; reg--)
2369 /* Find next free slot. */
2374 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[dest], DFmode),
2380 /* The following loop attempts to maximize the number of times we
2381 pop the top of the stack, as this permits the use of the faster
2382 ffreep instruction on platforms that support it. */
2386 for (reg = 0; reg <= old->top; reg++)
2387 if (TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2391 while (old->top >= live)
2392 if (TEST_HARD_REG_BIT (new->reg_set, old->reg[old->top]))
2394 while (TEST_HARD_REG_BIT (new->reg_set, old->reg[next]))
2396 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[next], DFmode),
2400 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[old->top], DFmode),
2406 /* If the new block has never been processed, then it can inherit
2407 the old stack order. */
2409 new->top = old->top;
2410 memcpy (new->reg, old->reg, sizeof (new->reg));
2414 /* This block has been entered before, and we must match the
2415 previously selected stack order. */
2417 /* By now, the only difference should be the order of the stack,
2418 not their depth or liveliness. */
2420 GO_IF_HARD_REG_EQUAL (old->reg_set, new->reg_set, win);
2423 gcc_assert (old->top == new->top);
2425 /* If the stack is not empty (new->top != -1), loop here emitting
2426 swaps until the stack is correct.
2428 The worst case number of swaps emitted is N + 2, where N is the
2429 depth of the stack. In some cases, the reg at the top of
2430 stack may be correct, but swapped anyway in order to fix
2431 other regs. But since we never swap any other reg away from
2432 its correct slot, this algorithm will converge. */
2437 /* Swap the reg at top of stack into the position it is
2438 supposed to be in, until the correct top of stack appears. */
2440 while (old->reg[old->top] != new->reg[new->top])
2442 for (reg = new->top; reg >= 0; reg--)
2443 if (new->reg[reg] == old->reg[old->top])
2446 gcc_assert (reg != -1);
2448 emit_swap_insn (insn, old,
2449 FP_MODE_REG (old->reg[reg], DFmode));
2452 /* See if any regs remain incorrect. If so, bring an
2453 incorrect reg to the top of stack, and let the while loop
2456 for (reg = new->top; reg >= 0; reg--)
2457 if (new->reg[reg] != old->reg[reg])
2459 emit_swap_insn (insn, old,
2460 FP_MODE_REG (old->reg[reg], DFmode));
2465 /* At this point there must be no differences. */
2467 for (reg = old->top; reg >= 0; reg--)
2468 gcc_assert (old->reg[reg] == new->reg[reg]);
2472 BB_END (current_block) = PREV_INSN (insn);
2475 /* Print stack configuration. */
2478 print_stack (FILE *file, stack s)
2484 fprintf (file, "uninitialized\n");
2485 else if (s->top == -1)
2486 fprintf (file, "empty\n");
2491 for (i = 0; i <= s->top; ++i)
2492 fprintf (file, "%d ", s->reg[i]);
2493 fputs ("]\n", file);
2497 /* This function was doing life analysis. We now let the regular live
2498 code do it's job, so we only need to check some extra invariants
2499 that reg-stack expects. Primary among these being that all registers
2500 are initialized before use.
2502 The function returns true when code was emitted to CFG edges and
2503 commit_edge_insertions needs to be called. */
2506 convert_regs_entry (void)
2512 /* Load something into each stack register live at function entry.
2513 Such live registers can be caused by uninitialized variables or
2514 functions not returning values on all paths. In order to keep
2515 the push/pop code happy, and to not scrog the register stack, we
2516 must put something in these registers. Use a QNaN.
2518 Note that we are inserting converted code here. This code is
2519 never seen by the convert_regs pass. */
2521 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2523 basic_block block = e->dest;
2524 block_info bi = BLOCK_INFO (block);
2527 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2528 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2532 bi->stack_in.reg[++top] = reg;
2534 init = gen_rtx_SET (VOIDmode,
2535 FP_MODE_REG (FIRST_STACK_REG, SFmode),
2537 insert_insn_on_edge (init, e);
2541 bi->stack_in.top = top;
2547 /* Construct the desired stack for function exit. This will either
2548 be `empty', or the function return value at top-of-stack. */
2551 convert_regs_exit (void)
2553 int value_reg_low, value_reg_high;
2557 retvalue = stack_result (current_function_decl);
2558 value_reg_low = value_reg_high = -1;
2561 value_reg_low = REGNO (retvalue);
2562 value_reg_high = value_reg_low
2563 + hard_regno_nregs[value_reg_low][GET_MODE (retvalue)] - 1;
2566 output_stack = &BLOCK_INFO (EXIT_BLOCK_PTR)->stack_in;
2567 if (value_reg_low == -1)
2568 output_stack->top = -1;
2573 output_stack->top = value_reg_high - value_reg_low;
2574 for (reg = value_reg_low; reg <= value_reg_high; ++reg)
2576 output_stack->reg[value_reg_high - reg] = reg;
2577 SET_HARD_REG_BIT (output_stack->reg_set, reg);
2582 /* Adjust the stack of this block on exit to match the stack of the
2583 target block, or copy stack info into the stack of the successor
2584 of the successor hasn't been processed yet. */
2586 compensate_edge (edge e, FILE *file)
2588 basic_block block = e->src, target = e->dest;
2589 block_info bi = BLOCK_INFO (block);
2590 struct stack_def regstack, tmpstack;
2591 stack target_stack = &BLOCK_INFO (target)->stack_in;
2594 current_block = block;
2595 regstack = bi->stack_out;
2597 fprintf (file, "Edge %d->%d: ", block->index, target->index);
2599 if (target_stack->top == -2)
2601 /* The target block hasn't had a stack order selected.
2602 We need merely ensure that no pops are needed. */
2603 for (reg = regstack.top; reg >= 0; --reg)
2604 if (!TEST_HARD_REG_BIT (target_stack->reg_set, regstack.reg[reg]))
2610 fprintf (file, "new block; copying stack position\n");
2612 /* change_stack kills values in regstack. */
2613 tmpstack = regstack;
2615 change_stack (BB_END (block), &tmpstack, target_stack, EMIT_AFTER);
2620 fprintf (file, "new block; pops needed\n");
2624 if (target_stack->top == regstack.top)
2626 for (reg = target_stack->top; reg >= 0; --reg)
2627 if (target_stack->reg[reg] != regstack.reg[reg])
2633 fprintf (file, "no changes needed\n");
2640 fprintf (file, "correcting stack to ");
2641 print_stack (file, target_stack);
2645 /* Care for non-call EH edges specially. The normal return path have
2646 values in registers. These will be popped en masse by the unwind
2648 if ((e->flags & (EDGE_EH | EDGE_ABNORMAL_CALL)) == EDGE_EH)
2649 target_stack->top = -1;
2651 /* Other calls may appear to have values live in st(0), but the
2652 abnormal return path will not have actually loaded the values. */
2653 else if (e->flags & EDGE_ABNORMAL_CALL)
2655 /* Assert that the lifetimes are as we expect -- one value
2656 live at st(0) on the end of the source block, and no
2657 values live at the beginning of the destination block. */
2660 CLEAR_HARD_REG_SET (tmp);
2661 GO_IF_HARD_REG_EQUAL (target_stack->reg_set, tmp, eh1);
2665 /* We are sure that there is st(0) live, otherwise we won't compensate.
2666 For complex return values, we may have st(1) live as well. */
2667 SET_HARD_REG_BIT (tmp, FIRST_STACK_REG);
2668 if (TEST_HARD_REG_BIT (regstack.reg_set, FIRST_STACK_REG + 1))
2669 SET_HARD_REG_BIT (tmp, FIRST_STACK_REG + 1);
2670 GO_IF_HARD_REG_EQUAL (regstack.reg_set, tmp, eh2);
2674 target_stack->top = -1;
2677 /* It is better to output directly to the end of the block
2678 instead of to the edge, because emit_swap can do minimal
2679 insn scheduling. We can do this when there is only one
2680 edge out, and it is not abnormal. */
2681 else if (EDGE_COUNT (block->succs) == 1 && !(e->flags & EDGE_ABNORMAL))
2683 /* change_stack kills values in regstack. */
2684 tmpstack = regstack;
2686 change_stack (BB_END (block), &tmpstack, target_stack,
2687 (JUMP_P (BB_END (block))
2688 ? EMIT_BEFORE : EMIT_AFTER));
2694 /* We don't support abnormal edges. Global takes care to
2695 avoid any live register across them, so we should never
2696 have to insert instructions on such edges. */
2697 gcc_assert (!(e->flags & EDGE_ABNORMAL));
2699 current_block = NULL;
2702 /* ??? change_stack needs some point to emit insns after. */
2703 after = emit_note (NOTE_INSN_DELETED);
2705 tmpstack = regstack;
2706 change_stack (after, &tmpstack, target_stack, EMIT_BEFORE);
2711 insert_insn_on_edge (seq, e);
2717 /* Convert stack register references in one block. */
2720 convert_regs_1 (FILE *file, basic_block block)
2722 struct stack_def regstack;
2723 block_info bi = BLOCK_INFO (block);
2726 edge e, beste = NULL;
2727 bool control_flow_insn_deleted = false;
2731 any_malformed_asm = false;
2733 /* Find the edge we will copy stack from. It should be the most frequent
2734 one as it will get cheapest after compensation code is generated,
2735 if multiple such exists, take one with largest count, prefer critical
2736 one (as splitting critical edges is more expensive), or one with lowest
2737 index, to avoid random changes with different orders of the edges. */
2738 FOR_EACH_EDGE (e, ei, block->preds)
2740 if (e->flags & EDGE_DFS_BACK)
2744 else if (EDGE_FREQUENCY (beste) < EDGE_FREQUENCY (e))
2746 else if (EDGE_FREQUENCY (beste) > EDGE_FREQUENCY (e))
2748 else if (beste->count < e->count)
2750 else if (beste->count > e->count)
2752 else if ((EDGE_CRITICAL_P (e) != 0)
2753 != (EDGE_CRITICAL_P (beste) != 0))
2755 if (EDGE_CRITICAL_P (e))
2758 else if (e->src->index < beste->src->index)
2762 /* Initialize stack at block entry. */
2763 if (bi->stack_in.top == -2)
2766 inserted |= compensate_edge (beste, file);
2769 /* No predecessors. Create an arbitrary input stack. */
2772 bi->stack_in.top = -1;
2773 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2774 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2775 bi->stack_in.reg[++bi->stack_in.top] = reg;
2779 /* Entry blocks do have stack already initialized. */
2782 current_block = block;
2786 fprintf (file, "\nBasic block %d\nInput stack: ", block->index);
2787 print_stack (file, &bi->stack_in);
2790 /* Process all insns in this block. Keep track of NEXT so that we
2791 don't process insns emitted while substituting in INSN. */
2792 next = BB_HEAD (block);
2793 regstack = bi->stack_in;
2797 next = NEXT_INSN (insn);
2799 /* Ensure we have not missed a block boundary. */
2801 if (insn == BB_END (block))
2804 /* Don't bother processing unless there is a stack reg
2805 mentioned or if it's a CALL_INSN. */
2806 if (stack_regs_mentioned (insn)
2811 fprintf (file, " insn %d input stack: ",
2813 print_stack (file, ®stack);
2815 control_flow_insn_deleted |= subst_stack_regs (insn, ®stack);
2822 fprintf (file, "Expected live registers [");
2823 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
2824 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg))
2825 fprintf (file, " %d", reg);
2826 fprintf (file, " ]\nOutput stack: ");
2827 print_stack (file, ®stack);
2830 insn = BB_END (block);
2832 insn = PREV_INSN (insn);
2834 /* If the function is declared to return a value, but it returns one
2835 in only some cases, some registers might come live here. Emit
2836 necessary moves for them. */
2838 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
2840 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg)
2841 && ! TEST_HARD_REG_BIT (regstack.reg_set, reg))
2846 fprintf (file, "Emitting insn initializing reg %d\n", reg);
2848 set = gen_rtx_SET (VOIDmode, FP_MODE_REG (reg, SFmode), not_a_num);
2849 insn = emit_insn_after (set, insn);
2850 control_flow_insn_deleted |= subst_stack_regs (insn, ®stack);
2854 /* Amongst the insns possibly deleted during the substitution process above,
2855 might have been the only trapping insn in the block. We purge the now
2856 possibly dead EH edges here to avoid an ICE from fixup_abnormal_edges,
2857 called at the end of convert_regs. The order in which we process the
2858 blocks ensures that we never delete an already processed edge.
2860 Note that, at this point, the CFG may have been damaged by the emission
2861 of instructions after an abnormal call, which moves the basic block end
2862 (and is the reason why we call fixup_abnormal_edges later). So we must
2863 be sure that the trapping insn has been deleted before trying to purge
2864 dead edges, otherwise we risk purging valid edges.
2866 ??? We are normally supposed not to delete trapping insns, so we pretend
2867 that the insns deleted above don't actually trap. It would have been
2868 better to detect this earlier and avoid creating the EH edge in the first
2869 place, still, but we don't have enough information at that time. */
2871 if (control_flow_insn_deleted)
2872 purge_dead_edges (block);
2874 /* Something failed if the stack lives don't match. If we had malformed
2875 asms, we zapped the instruction itself, but that didn't produce the
2876 same pattern of register kills as before. */
2877 GO_IF_HARD_REG_EQUAL (regstack.reg_set, bi->out_reg_set, win);
2878 gcc_assert (any_malformed_asm);
2880 bi->stack_out = regstack;
2882 /* Compensate the back edges, as those wasn't visited yet. */
2883 FOR_EACH_EDGE (e, ei, block->succs)
2885 if (e->flags & EDGE_DFS_BACK
2886 || (e->dest == EXIT_BLOCK_PTR))
2888 gcc_assert (BLOCK_INFO (e->dest)->done
2889 || e->dest == block);
2890 inserted |= compensate_edge (e, file);
2893 FOR_EACH_EDGE (e, ei, block->preds)
2895 if (e != beste && !(e->flags & EDGE_DFS_BACK)
2896 && e->src != ENTRY_BLOCK_PTR)
2898 gcc_assert (BLOCK_INFO (e->src)->done);
2899 inserted |= compensate_edge (e, file);
2906 /* Convert registers in all blocks reachable from BLOCK. */
2909 convert_regs_2 (FILE *file, basic_block block)
2911 basic_block *stack, *sp;
2914 /* We process the blocks in a top-down manner, in a way such that one block
2915 is only processed after all its predecessors. The number of predecessors
2916 of every block has already been computed. */
2918 stack = xmalloc (sizeof (*stack) * n_basic_blocks);
2931 /* Processing BLOCK is achieved by convert_regs_1, which may purge
2932 some dead EH outgoing edge after the deletion of the trapping
2933 insn inside the block. Since the number of predecessors of
2934 BLOCK's successors was computed based on the initial edge set,
2935 we check the necessity to process some of these successors
2936 before such an edge deletion may happen. However, there is
2937 a pitfall: if BLOCK is the only predecessor of a successor and
2938 the edge between them happens to be deleted, the successor
2939 becomes unreachable and should not be processed. The problem
2940 is that there is no way to preventively detect this case so we
2941 stack the successor in all cases and hand over the task of
2942 fixing up the discrepancy to convert_regs_1. */
2944 FOR_EACH_EDGE (e, ei, block->succs)
2945 if (! (e->flags & EDGE_DFS_BACK))
2947 BLOCK_INFO (e->dest)->predecessors--;
2948 if (!BLOCK_INFO (e->dest)->predecessors)
2952 inserted |= convert_regs_1 (file, block);
2953 BLOCK_INFO (block)->done = 1;
2955 while (sp != stack);
2962 /* Traverse all basic blocks in a function, converting the register
2963 references in each insn from the "flat" register file that gcc uses,
2964 to the stack-like registers the 387 uses. */
2967 convert_regs (FILE *file)
2974 /* Initialize uninitialized registers on function entry. */
2975 inserted = convert_regs_entry ();
2977 /* Construct the desired stack for function exit. */
2978 convert_regs_exit ();
2979 BLOCK_INFO (EXIT_BLOCK_PTR)->done = 1;
2981 /* ??? Future: process inner loops first, and give them arbitrary
2982 initial stacks which emit_swap_insn can modify. This ought to
2983 prevent double fxch that often appears at the head of a loop. */
2985 /* Process all blocks reachable from all entry points. */
2986 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2987 inserted |= convert_regs_2 (file, e->dest);
2989 /* ??? Process all unreachable blocks. Though there's no excuse
2990 for keeping these even when not optimizing. */
2993 block_info bi = BLOCK_INFO (b);
2996 inserted |= convert_regs_2 (file, b);
2998 clear_aux_for_blocks ();
3000 fixup_abnormal_edges ();
3002 commit_edge_insertions ();
3008 /* Convert register usage from "flat" register file usage to a "stack
3009 register file. FILE is the dump file, if used.
3011 Construct a CFG and run life analysis. Then convert each insn one
3012 by one. Run a last cleanup_cfg pass, if optimizing, to eliminate
3013 code duplication created when the converter inserts pop insns on
3017 reg_to_stack (FILE *file)
3023 /* Clean up previous run. */
3024 stack_regs_mentioned_data = 0;
3026 /* See if there is something to do. Flow analysis is quite
3027 expensive so we might save some compilation time. */
3028 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
3029 if (regs_ever_live[i])
3031 if (i > LAST_STACK_REG)
3034 /* Ok, floating point instructions exist. If not optimizing,
3035 build the CFG and run life analysis.
3036 Also need to rebuild life when superblock scheduling is done
3037 as it don't update liveness yet. */
3039 || (flag_sched2_use_superblocks
3040 && flag_schedule_insns_after_reload))
3042 count_or_remove_death_notes (NULL, 1);
3043 life_analysis (file, PROP_DEATH_NOTES);
3045 mark_dfs_back_edges ();
3047 /* Set up block info for each basic block. */
3048 alloc_aux_for_blocks (sizeof (struct block_info_def));
3049 FOR_EACH_BB_REVERSE (bb)
3051 block_info bi = BLOCK_INFO (bb);
3056 FOR_EACH_EDGE (e, ei, bb->preds)
3057 if (!(e->flags & EDGE_DFS_BACK)
3058 && e->src != ENTRY_BLOCK_PTR)
3061 /* Set current register status at last instruction `uninitialized'. */
3062 bi->stack_in.top = -2;
3064 /* Copy live_at_end and live_at_start into temporaries. */
3065 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
3067 if (REGNO_REG_SET_P (bb->global_live_at_end, reg))
3068 SET_HARD_REG_BIT (bi->out_reg_set, reg);
3069 if (REGNO_REG_SET_P (bb->global_live_at_start, reg))
3070 SET_HARD_REG_BIT (bi->stack_in.reg_set, reg);
3074 /* Create the replacement registers up front. */
3075 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
3077 enum machine_mode mode;
3078 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3080 mode = GET_MODE_WIDER_MODE (mode))
3081 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
3082 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT);
3084 mode = GET_MODE_WIDER_MODE (mode))
3085 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
3088 ix86_flags_rtx = gen_rtx_REG (CCmode, FLAGS_REG);
3090 /* A QNaN for initializing uninitialized variables.
3092 ??? We can't load from constant memory in PIC mode, because
3093 we're inserting these instructions before the prologue and
3094 the PIC register hasn't been set up. In that case, fall back
3095 on zero, which we can get from `ldz'. */
3098 not_a_num = CONST0_RTX (SFmode);
3101 not_a_num = gen_lowpart (SFmode, GEN_INT (0x7fc00000));
3102 not_a_num = force_const_mem (SFmode, not_a_num);
3105 /* Allocate a cache for stack_regs_mentioned. */
3106 max_uid = get_max_uid ();
3107 VARRAY_CHAR_INIT (stack_regs_mentioned_data, max_uid + 1,
3108 "stack_regs_mentioned cache");
3110 convert_regs (file);
3112 free_aux_for_blocks ();
3115 #endif /* STACK_REGS */
3117 #include "gt-reg-stack.h"