kernel - Fix some rare pmap races in i386 and x86_64.
[dragonfly.git] / contrib / gcc-4.1 / gcc / reg-stack.c
blob131f7b0c1ff4655fcc841a072113fe0ba458bbb0
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 gcc_assert (hard_regno >= FIRST_STACK_REG);
811 if (hard_regno == FIRST_STACK_REG)
812 return;
814 other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
816 tmp = regstack->reg[other_reg];
817 regstack->reg[other_reg] = regstack->reg[regstack->top];
818 regstack->reg[regstack->top] = tmp;
820 /* Find the previous insn involving stack regs, but don't pass a
821 block boundary. */
822 i1 = NULL;
823 if (current_block && insn != BB_HEAD (current_block))
825 rtx tmp = PREV_INSN (insn);
826 rtx limit = PREV_INSN (BB_HEAD (current_block));
827 while (tmp != limit)
829 if (LABEL_P (tmp)
830 || CALL_P (tmp)
831 || NOTE_INSN_BASIC_BLOCK_P (tmp)
832 || (NONJUMP_INSN_P (tmp)
833 && stack_regs_mentioned (tmp)))
835 i1 = tmp;
836 break;
838 tmp = PREV_INSN (tmp);
842 if (i1 != NULL_RTX
843 && (i1set = single_set (i1)) != NULL_RTX)
845 rtx i1src = *get_true_reg (&SET_SRC (i1set));
846 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
848 /* If the previous register stack push was from the reg we are to
849 swap with, omit the swap. */
851 if (REG_P (i1dest) && REGNO (i1dest) == FIRST_STACK_REG
852 && REG_P (i1src)
853 && REGNO (i1src) == (unsigned) hard_regno - 1
854 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
855 return;
857 /* If the previous insn wrote to the reg we are to swap with,
858 omit the swap. */
860 if (REG_P (i1dest) && REGNO (i1dest) == (unsigned) hard_regno
861 && REG_P (i1src) && REGNO (i1src) == FIRST_STACK_REG
862 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
863 return;
866 /* Avoid emitting the swap if this is the first register stack insn
867 of the current_block. Instead update the current_block's stack_in
868 and let compensate edges take care of this for us. */
869 if (current_block && starting_stack_p)
871 BLOCK_INFO (current_block)->stack_in = *regstack;
872 starting_stack_p = false;
873 return;
876 swap_rtx = gen_swapxf (FP_MODE_REG (hard_regno, XFmode),
877 FP_MODE_REG (FIRST_STACK_REG, XFmode));
879 if (i1)
880 emit_insn_after (swap_rtx, i1);
881 else if (current_block)
882 emit_insn_before (swap_rtx, BB_HEAD (current_block));
883 else
884 emit_insn_before (swap_rtx, insn);
887 /* Emit an insns before INSN to swap virtual register SRC1 with
888 the top of stack and virtual register SRC2 with second stack
889 slot. REGSTACK is the stack state before the swaps, and
890 is updated to reflect the swaps. A swap insn is represented as a
891 PARALLEL of two patterns: each pattern moves one reg to the other.
893 If SRC1 and/or SRC2 are already at the right place, no swap insn
894 is emitted. */
896 static void
897 swap_to_top (rtx insn, stack regstack, rtx src1, rtx src2)
899 struct stack_def temp_stack;
900 int regno, j, k, temp;
902 temp_stack = *regstack;
904 /* Place operand 1 at the top of stack. */
905 regno = get_hard_regnum (&temp_stack, src1);
906 gcc_assert (regno >= 0);
907 if (regno != FIRST_STACK_REG)
909 k = temp_stack.top - (regno - FIRST_STACK_REG);
910 j = temp_stack.top;
912 temp = temp_stack.reg[k];
913 temp_stack.reg[k] = temp_stack.reg[j];
914 temp_stack.reg[j] = temp;
917 /* Place operand 2 next on the stack. */
918 regno = get_hard_regnum (&temp_stack, src2);
919 gcc_assert (regno >= 0);
920 if (regno != FIRST_STACK_REG + 1)
922 k = temp_stack.top - (regno - FIRST_STACK_REG);
923 j = temp_stack.top - 1;
925 temp = temp_stack.reg[k];
926 temp_stack.reg[k] = temp_stack.reg[j];
927 temp_stack.reg[j] = temp;
930 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
933 /* Handle a move to or from a stack register in PAT, which is in INSN.
934 REGSTACK is the current stack. Return whether a control flow insn
935 was deleted in the process. */
937 static bool
938 move_for_stack_reg (rtx insn, stack regstack, rtx pat)
940 rtx *psrc = get_true_reg (&SET_SRC (pat));
941 rtx *pdest = get_true_reg (&SET_DEST (pat));
942 rtx src, dest;
943 rtx note;
944 bool control_flow_insn_deleted = false;
946 src = *psrc; dest = *pdest;
948 if (STACK_REG_P (src) && STACK_REG_P (dest))
950 /* Write from one stack reg to another. If SRC dies here, then
951 just change the register mapping and delete the insn. */
953 note = find_regno_note (insn, REG_DEAD, REGNO (src));
954 if (note)
956 int i;
958 /* If this is a no-op move, there must not be a REG_DEAD note. */
959 gcc_assert (REGNO (src) != REGNO (dest));
961 for (i = regstack->top; i >= 0; i--)
962 if (regstack->reg[i] == REGNO (src))
963 break;
965 /* The destination must be dead, or life analysis is borked. */
966 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
968 /* If the source is not live, this is yet another case of
969 uninitialized variables. Load up a NaN instead. */
970 if (i < 0)
971 return move_nan_for_stack_reg (insn, regstack, dest);
973 /* It is possible that the dest is unused after this insn.
974 If so, just pop the src. */
976 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
977 emit_pop_insn (insn, regstack, src, EMIT_AFTER);
978 else
980 regstack->reg[i] = REGNO (dest);
981 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
982 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
985 control_flow_insn_deleted |= control_flow_insn_p (insn);
986 delete_insn (insn);
987 return control_flow_insn_deleted;
990 /* The source reg does not die. */
992 /* If this appears to be a no-op move, delete it, or else it
993 will confuse the machine description output patterns. But if
994 it is REG_UNUSED, we must pop the reg now, as per-insn processing
995 for REG_UNUSED will not work for deleted insns. */
997 if (REGNO (src) == REGNO (dest))
999 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1000 emit_pop_insn (insn, regstack, dest, EMIT_AFTER);
1002 control_flow_insn_deleted |= control_flow_insn_p (insn);
1003 delete_insn (insn);
1004 return control_flow_insn_deleted;
1007 /* The destination ought to be dead. */
1008 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
1010 replace_reg (psrc, get_hard_regnum (regstack, src));
1012 regstack->reg[++regstack->top] = REGNO (dest);
1013 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1014 replace_reg (pdest, FIRST_STACK_REG);
1016 else if (STACK_REG_P (src))
1018 /* Save from a stack reg to MEM, or possibly integer reg. Since
1019 only top of stack may be saved, emit an exchange first if
1020 needs be. */
1022 emit_swap_insn (insn, regstack, src);
1024 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1025 if (note)
1027 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1028 regstack->top--;
1029 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1031 else if ((GET_MODE (src) == XFmode)
1032 && regstack->top < REG_STACK_SIZE - 1)
1034 /* A 387 cannot write an XFmode value to a MEM without
1035 clobbering the source reg. The output code can handle
1036 this by reading back the value from the MEM.
1037 But it is more efficient to use a temp register if one is
1038 available. Push the source value here if the register
1039 stack is not full, and then write the value to memory via
1040 a pop. */
1041 rtx push_rtx;
1042 rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, GET_MODE (src));
1044 push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
1045 emit_insn_before (push_rtx, insn);
1046 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD, top_stack_reg,
1047 REG_NOTES (insn));
1050 replace_reg (psrc, FIRST_STACK_REG);
1052 else
1054 gcc_assert (STACK_REG_P (dest));
1056 /* Load from MEM, or possibly integer REG or constant, into the
1057 stack regs. The actual target is always the top of the
1058 stack. The stack mapping is changed to reflect that DEST is
1059 now at top of stack. */
1061 /* The destination ought to be dead. */
1062 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
1064 gcc_assert (regstack->top < REG_STACK_SIZE);
1066 regstack->reg[++regstack->top] = REGNO (dest);
1067 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1068 replace_reg (pdest, FIRST_STACK_REG);
1071 return control_flow_insn_deleted;
1074 /* A helper function which replaces INSN with a pattern that loads up
1075 a NaN into DEST, then invokes move_for_stack_reg. */
1077 static bool
1078 move_nan_for_stack_reg (rtx insn, stack regstack, rtx dest)
1080 rtx pat;
1082 dest = FP_MODE_REG (REGNO (dest), SFmode);
1083 pat = gen_rtx_SET (VOIDmode, dest, not_a_num);
1084 PATTERN (insn) = pat;
1085 INSN_CODE (insn) = -1;
1087 return move_for_stack_reg (insn, regstack, pat);
1090 /* Swap the condition on a branch, if there is one. Return true if we
1091 found a condition to swap. False if the condition was not used as
1092 such. */
1094 static int
1095 swap_rtx_condition_1 (rtx pat)
1097 const char *fmt;
1098 int i, r = 0;
1100 if (COMPARISON_P (pat))
1102 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1103 r = 1;
1105 else
1107 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1108 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1110 if (fmt[i] == 'E')
1112 int j;
1114 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1115 r |= swap_rtx_condition_1 (XVECEXP (pat, i, j));
1117 else if (fmt[i] == 'e')
1118 r |= swap_rtx_condition_1 (XEXP (pat, i));
1122 return r;
1125 static int
1126 swap_rtx_condition (rtx insn)
1128 rtx pat = PATTERN (insn);
1130 /* We're looking for a single set to cc0 or an HImode temporary. */
1132 if (GET_CODE (pat) == SET
1133 && REG_P (SET_DEST (pat))
1134 && REGNO (SET_DEST (pat)) == FLAGS_REG)
1136 insn = next_flags_user (insn);
1137 if (insn == NULL_RTX)
1138 return 0;
1139 pat = PATTERN (insn);
1142 /* See if this is, or ends in, a fnstsw. If so, we're not doing anything
1143 with the cc value right now. We may be able to search for one
1144 though. */
1146 if (GET_CODE (pat) == SET
1147 && GET_CODE (SET_SRC (pat)) == UNSPEC
1148 && XINT (SET_SRC (pat), 1) == UNSPEC_FNSTSW)
1150 rtx dest = SET_DEST (pat);
1152 /* Search forward looking for the first use of this value.
1153 Stop at block boundaries. */
1154 while (insn != BB_END (current_block))
1156 insn = NEXT_INSN (insn);
1157 if (INSN_P (insn) && reg_mentioned_p (dest, insn))
1158 break;
1159 if (CALL_P (insn))
1160 return 0;
1163 /* We haven't found it. */
1164 if (insn == BB_END (current_block))
1165 return 0;
1167 /* So we've found the insn using this value. If it is anything
1168 other than sahf or the value does not die (meaning we'd have
1169 to search further), then we must give up. */
1170 pat = PATTERN (insn);
1171 if (GET_CODE (pat) != SET
1172 || GET_CODE (SET_SRC (pat)) != UNSPEC
1173 || XINT (SET_SRC (pat), 1) != UNSPEC_SAHF
1174 || ! dead_or_set_p (insn, dest))
1175 return 0;
1177 /* Now we are prepared to handle this as a normal cc0 setter. */
1178 insn = next_flags_user (insn);
1179 if (insn == NULL_RTX)
1180 return 0;
1181 pat = PATTERN (insn);
1184 if (swap_rtx_condition_1 (pat))
1186 int fail = 0;
1187 INSN_CODE (insn) = -1;
1188 if (recog_memoized (insn) == -1)
1189 fail = 1;
1190 /* In case the flags don't die here, recurse to try fix
1191 following user too. */
1192 else if (! dead_or_set_p (insn, ix86_flags_rtx))
1194 insn = next_flags_user (insn);
1195 if (!insn || !swap_rtx_condition (insn))
1196 fail = 1;
1198 if (fail)
1200 swap_rtx_condition_1 (pat);
1201 return 0;
1203 return 1;
1205 return 0;
1208 /* Handle a comparison. Special care needs to be taken to avoid
1209 causing comparisons that a 387 cannot do correctly, such as EQ.
1211 Also, a pop insn may need to be emitted. The 387 does have an
1212 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1213 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1214 set up. */
1216 static void
1217 compare_for_stack_reg (rtx insn, stack regstack, rtx pat_src)
1219 rtx *src1, *src2;
1220 rtx src1_note, src2_note;
1222 src1 = get_true_reg (&XEXP (pat_src, 0));
1223 src2 = get_true_reg (&XEXP (pat_src, 1));
1225 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1226 registers that die in this insn - move those to stack top first. */
1227 if ((! STACK_REG_P (*src1)
1228 || (STACK_REG_P (*src2)
1229 && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
1230 && swap_rtx_condition (insn))
1232 rtx temp;
1233 temp = XEXP (pat_src, 0);
1234 XEXP (pat_src, 0) = XEXP (pat_src, 1);
1235 XEXP (pat_src, 1) = temp;
1237 src1 = get_true_reg (&XEXP (pat_src, 0));
1238 src2 = get_true_reg (&XEXP (pat_src, 1));
1240 INSN_CODE (insn) = -1;
1243 /* We will fix any death note later. */
1245 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1247 if (STACK_REG_P (*src2))
1248 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1249 else
1250 src2_note = NULL_RTX;
1252 emit_swap_insn (insn, regstack, *src1);
1254 replace_reg (src1, FIRST_STACK_REG);
1256 if (STACK_REG_P (*src2))
1257 replace_reg (src2, get_hard_regnum (regstack, *src2));
1259 if (src1_note)
1261 pop_stack (regstack, REGNO (XEXP (src1_note, 0)));
1262 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1265 /* If the second operand dies, handle that. But if the operands are
1266 the same stack register, don't bother, because only one death is
1267 needed, and it was just handled. */
1269 if (src2_note
1270 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
1271 && REGNO (*src1) == REGNO (*src2)))
1273 /* As a special case, two regs may die in this insn if src2 is
1274 next to top of stack and the top of stack also dies. Since
1275 we have already popped src1, "next to top of stack" is really
1276 at top (FIRST_STACK_REG) now. */
1278 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1279 && src1_note)
1281 pop_stack (regstack, REGNO (XEXP (src2_note, 0)));
1282 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1284 else
1286 /* The 386 can only represent death of the first operand in
1287 the case handled above. In all other cases, emit a separate
1288 pop and remove the death note from here. */
1290 /* link_cc0_insns (insn); */
1292 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
1294 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
1295 EMIT_AFTER);
1300 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1301 is the current register layout. Return whether a control flow insn
1302 was deleted in the process. */
1304 static bool
1305 subst_stack_regs_pat (rtx insn, stack regstack, rtx pat)
1307 rtx *dest, *src;
1308 bool control_flow_insn_deleted = false;
1310 switch (GET_CODE (pat))
1312 case USE:
1313 /* Deaths in USE insns can happen in non optimizing compilation.
1314 Handle them by popping the dying register. */
1315 src = get_true_reg (&XEXP (pat, 0));
1316 if (STACK_REG_P (*src)
1317 && find_regno_note (insn, REG_DEAD, REGNO (*src)))
1319 emit_pop_insn (insn, regstack, *src, EMIT_AFTER);
1320 return control_flow_insn_deleted;
1322 /* ??? Uninitialized USE should not happen. */
1323 else
1324 gcc_assert (get_hard_regnum (regstack, *src) != -1);
1325 break;
1327 case CLOBBER:
1329 rtx note;
1331 dest = get_true_reg (&XEXP (pat, 0));
1332 if (STACK_REG_P (*dest))
1334 note = find_reg_note (insn, REG_DEAD, *dest);
1336 if (pat != PATTERN (insn))
1338 /* The fix_truncdi_1 pattern wants to be able to allocate
1339 its own scratch register. It does this by clobbering
1340 an fp reg so that it is assured of an empty reg-stack
1341 register. If the register is live, kill it now.
1342 Remove the DEAD/UNUSED note so we don't try to kill it
1343 later too. */
1345 if (note)
1346 emit_pop_insn (insn, regstack, *dest, EMIT_BEFORE);
1347 else
1349 note = find_reg_note (insn, REG_UNUSED, *dest);
1350 gcc_assert (note);
1352 remove_note (insn, note);
1353 replace_reg (dest, FIRST_STACK_REG + 1);
1355 else
1357 /* A top-level clobber with no REG_DEAD, and no hard-regnum
1358 indicates an uninitialized value. Because reload removed
1359 all other clobbers, this must be due to a function
1360 returning without a value. Load up a NaN. */
1362 if (!note)
1364 rtx t = *dest;
1365 if (get_hard_regnum (regstack, t) == -1)
1366 control_flow_insn_deleted
1367 |= move_nan_for_stack_reg (insn, regstack, t);
1368 if (COMPLEX_MODE_P (GET_MODE (t)))
1370 t = FP_MODE_REG (REGNO (t) + 1, DFmode);
1371 if (get_hard_regnum (regstack, t) == -1)
1372 control_flow_insn_deleted
1373 |= move_nan_for_stack_reg (insn, regstack, t);
1378 break;
1381 case SET:
1383 rtx *src1 = (rtx *) 0, *src2;
1384 rtx src1_note, src2_note;
1385 rtx pat_src;
1387 dest = get_true_reg (&SET_DEST (pat));
1388 src = get_true_reg (&SET_SRC (pat));
1389 pat_src = SET_SRC (pat);
1391 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1392 if (STACK_REG_P (*src)
1393 || (STACK_REG_P (*dest)
1394 && (REG_P (*src) || MEM_P (*src)
1395 || GET_CODE (*src) == CONST_DOUBLE)))
1397 control_flow_insn_deleted |= move_for_stack_reg (insn, regstack, pat);
1398 break;
1401 switch (GET_CODE (pat_src))
1403 case COMPARE:
1404 compare_for_stack_reg (insn, regstack, pat_src);
1405 break;
1407 case CALL:
1409 int count;
1410 for (count = hard_regno_nregs[REGNO (*dest)][GET_MODE (*dest)];
1411 --count >= 0;)
1413 regstack->reg[++regstack->top] = REGNO (*dest) + count;
1414 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
1417 replace_reg (dest, FIRST_STACK_REG);
1418 break;
1420 case REG:
1421 /* This is a `tstM2' case. */
1422 gcc_assert (*dest == cc0_rtx);
1423 src1 = src;
1425 /* Fall through. */
1427 case FLOAT_TRUNCATE:
1428 case SQRT:
1429 case ABS:
1430 case NEG:
1431 /* These insns only operate on the top of the stack. DEST might
1432 be cc0_rtx if we're processing a tstM pattern. Also, it's
1433 possible that the tstM case results in a REG_DEAD note on the
1434 source. */
1436 if (src1 == 0)
1437 src1 = get_true_reg (&XEXP (pat_src, 0));
1439 emit_swap_insn (insn, regstack, *src1);
1441 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1443 if (STACK_REG_P (*dest))
1444 replace_reg (dest, FIRST_STACK_REG);
1446 if (src1_note)
1448 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1449 regstack->top--;
1450 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1453 replace_reg (src1, FIRST_STACK_REG);
1454 break;
1456 case MINUS:
1457 case DIV:
1458 /* On i386, reversed forms of subM3 and divM3 exist for
1459 MODE_FLOAT, so the same code that works for addM3 and mulM3
1460 can be used. */
1461 case MULT:
1462 case PLUS:
1463 /* These insns can accept the top of stack as a destination
1464 from a stack reg or mem, or can use the top of stack as a
1465 source and some other stack register (possibly top of stack)
1466 as a destination. */
1468 src1 = get_true_reg (&XEXP (pat_src, 0));
1469 src2 = get_true_reg (&XEXP (pat_src, 1));
1471 /* We will fix any death note later. */
1473 if (STACK_REG_P (*src1))
1474 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1475 else
1476 src1_note = NULL_RTX;
1477 if (STACK_REG_P (*src2))
1478 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1479 else
1480 src2_note = NULL_RTX;
1482 /* If either operand is not a stack register, then the dest
1483 must be top of stack. */
1485 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
1486 emit_swap_insn (insn, regstack, *dest);
1487 else
1489 /* Both operands are REG. If neither operand is already
1490 at the top of stack, choose to make the one that is the dest
1491 the new top of stack. */
1493 int src1_hard_regnum, src2_hard_regnum;
1495 src1_hard_regnum = get_hard_regnum (regstack, *src1);
1496 src2_hard_regnum = get_hard_regnum (regstack, *src2);
1497 gcc_assert (src1_hard_regnum != -1);
1498 gcc_assert (src2_hard_regnum != -1);
1500 if (src1_hard_regnum != FIRST_STACK_REG
1501 && src2_hard_regnum != FIRST_STACK_REG)
1502 emit_swap_insn (insn, regstack, *dest);
1505 if (STACK_REG_P (*src1))
1506 replace_reg (src1, get_hard_regnum (regstack, *src1));
1507 if (STACK_REG_P (*src2))
1508 replace_reg (src2, get_hard_regnum (regstack, *src2));
1510 if (src1_note)
1512 rtx src1_reg = XEXP (src1_note, 0);
1514 /* If the register that dies is at the top of stack, then
1515 the destination is somewhere else - merely substitute it.
1516 But if the reg that dies is not at top of stack, then
1517 move the top of stack to the dead reg, as though we had
1518 done the insn and then a store-with-pop. */
1520 if (REGNO (src1_reg) == regstack->reg[regstack->top])
1522 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1523 replace_reg (dest, get_hard_regnum (regstack, *dest));
1525 else
1527 int regno = get_hard_regnum (regstack, src1_reg);
1529 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1530 replace_reg (dest, regno);
1532 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1533 = regstack->reg[regstack->top];
1536 CLEAR_HARD_REG_BIT (regstack->reg_set,
1537 REGNO (XEXP (src1_note, 0)));
1538 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1539 regstack->top--;
1541 else if (src2_note)
1543 rtx src2_reg = XEXP (src2_note, 0);
1544 if (REGNO (src2_reg) == regstack->reg[regstack->top])
1546 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1547 replace_reg (dest, get_hard_regnum (regstack, *dest));
1549 else
1551 int regno = get_hard_regnum (regstack, src2_reg);
1553 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1554 replace_reg (dest, regno);
1556 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1557 = regstack->reg[regstack->top];
1560 CLEAR_HARD_REG_BIT (regstack->reg_set,
1561 REGNO (XEXP (src2_note, 0)));
1562 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
1563 regstack->top--;
1565 else
1567 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1568 replace_reg (dest, get_hard_regnum (regstack, *dest));
1571 /* Keep operand 1 matching with destination. */
1572 if (COMMUTATIVE_ARITH_P (pat_src)
1573 && REG_P (*src1) && REG_P (*src2)
1574 && REGNO (*src1) != REGNO (*dest))
1576 int tmp = REGNO (*src1);
1577 replace_reg (src1, REGNO (*src2));
1578 replace_reg (src2, tmp);
1580 break;
1582 case UNSPEC:
1583 switch (XINT (pat_src, 1))
1585 case UNSPEC_FIST:
1587 case UNSPEC_FIST_FLOOR:
1588 case UNSPEC_FIST_CEIL:
1590 /* These insns only operate on the top of the stack. */
1592 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1593 emit_swap_insn (insn, regstack, *src1);
1595 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1597 if (STACK_REG_P (*dest))
1598 replace_reg (dest, FIRST_STACK_REG);
1600 if (src1_note)
1602 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1603 regstack->top--;
1604 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1607 replace_reg (src1, FIRST_STACK_REG);
1608 break;
1610 case UNSPEC_SIN:
1611 case UNSPEC_COS:
1612 case UNSPEC_FRNDINT:
1613 case UNSPEC_F2XM1:
1615 case UNSPEC_FRNDINT_FLOOR:
1616 case UNSPEC_FRNDINT_CEIL:
1617 case UNSPEC_FRNDINT_TRUNC:
1618 case UNSPEC_FRNDINT_MASK_PM:
1620 /* These insns only operate on the top of the stack. */
1622 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1624 emit_swap_insn (insn, regstack, *src1);
1626 /* Input should never die, it is
1627 replaced with output. */
1628 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1629 gcc_assert (!src1_note);
1631 if (STACK_REG_P (*dest))
1632 replace_reg (dest, FIRST_STACK_REG);
1634 replace_reg (src1, FIRST_STACK_REG);
1635 break;
1637 case UNSPEC_FPATAN:
1638 case UNSPEC_FYL2X:
1639 case UNSPEC_FYL2XP1:
1640 /* These insns operate on the top two stack slots. */
1642 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1643 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1645 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1646 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1648 swap_to_top (insn, regstack, *src1, *src2);
1650 replace_reg (src1, FIRST_STACK_REG);
1651 replace_reg (src2, FIRST_STACK_REG + 1);
1653 if (src1_note)
1654 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1655 if (src2_note)
1656 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1658 /* Pop both input operands from the stack. */
1659 CLEAR_HARD_REG_BIT (regstack->reg_set,
1660 regstack->reg[regstack->top]);
1661 CLEAR_HARD_REG_BIT (regstack->reg_set,
1662 regstack->reg[regstack->top - 1]);
1663 regstack->top -= 2;
1665 /* Push the result back onto the stack. */
1666 regstack->reg[++regstack->top] = REGNO (*dest);
1667 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1668 replace_reg (dest, FIRST_STACK_REG);
1669 break;
1671 case UNSPEC_FSCALE_FRACT:
1672 case UNSPEC_FPREM_F:
1673 case UNSPEC_FPREM1_F:
1674 /* These insns operate on the top two stack slots.
1675 first part of double input, double output insn. */
1677 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1678 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1680 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1681 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1683 /* Inputs should never die, they are
1684 replaced with outputs. */
1685 gcc_assert (!src1_note);
1686 gcc_assert (!src2_note);
1688 swap_to_top (insn, regstack, *src1, *src2);
1690 /* Push the result back onto stack. Empty stack slot
1691 will be filled in second part of insn. */
1692 if (STACK_REG_P (*dest)) {
1693 regstack->reg[regstack->top] = REGNO (*dest);
1694 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1695 replace_reg (dest, FIRST_STACK_REG);
1698 replace_reg (src1, FIRST_STACK_REG);
1699 replace_reg (src2, FIRST_STACK_REG + 1);
1700 break;
1702 case UNSPEC_FSCALE_EXP:
1703 case UNSPEC_FPREM_U:
1704 case UNSPEC_FPREM1_U:
1705 /* These insns operate on the top two stack slots./
1706 second part of double input, double output insn. */
1708 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1709 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1711 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1712 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1714 /* Inputs should never die, they are
1715 replaced with outputs. */
1716 gcc_assert (!src1_note);
1717 gcc_assert (!src2_note);
1719 swap_to_top (insn, regstack, *src1, *src2);
1721 /* Push the result back onto stack. Fill empty slot from
1722 first part of insn and fix top of stack pointer. */
1723 if (STACK_REG_P (*dest)) {
1724 regstack->reg[regstack->top - 1] = REGNO (*dest);
1725 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1726 replace_reg (dest, FIRST_STACK_REG + 1);
1729 replace_reg (src1, FIRST_STACK_REG);
1730 replace_reg (src2, FIRST_STACK_REG + 1);
1731 break;
1733 case UNSPEC_SINCOS_COS:
1734 case UNSPEC_TAN_ONE:
1735 case UNSPEC_XTRACT_FRACT:
1736 /* These insns operate on the top two stack slots,
1737 first part of one input, double output insn. */
1739 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1741 emit_swap_insn (insn, regstack, *src1);
1743 /* Input should never die, it is
1744 replaced with output. */
1745 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1746 gcc_assert (!src1_note);
1748 /* Push the result back onto stack. Empty stack slot
1749 will be filled in second part of insn. */
1750 if (STACK_REG_P (*dest)) {
1751 regstack->reg[regstack->top + 1] = REGNO (*dest);
1752 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1753 replace_reg (dest, FIRST_STACK_REG);
1756 replace_reg (src1, FIRST_STACK_REG);
1757 break;
1759 case UNSPEC_SINCOS_SIN:
1760 case UNSPEC_TAN_TAN:
1761 case UNSPEC_XTRACT_EXP:
1762 /* These insns operate on the top two stack slots,
1763 second part of one input, double output insn. */
1765 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1767 emit_swap_insn (insn, regstack, *src1);
1769 /* Input should never die, it is
1770 replaced with output. */
1771 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1772 gcc_assert (!src1_note);
1774 /* Push the result back onto stack. Fill empty slot from
1775 first part of insn and fix top of stack pointer. */
1776 if (STACK_REG_P (*dest)) {
1777 regstack->reg[regstack->top] = REGNO (*dest);
1778 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1779 replace_reg (dest, FIRST_STACK_REG + 1);
1781 regstack->top++;
1784 replace_reg (src1, FIRST_STACK_REG);
1785 break;
1787 case UNSPEC_SAHF:
1788 /* (unspec [(unspec [(compare)] UNSPEC_FNSTSW)] UNSPEC_SAHF)
1789 The combination matches the PPRO fcomi instruction. */
1791 pat_src = XVECEXP (pat_src, 0, 0);
1792 gcc_assert (GET_CODE (pat_src) == UNSPEC);
1793 gcc_assert (XINT (pat_src, 1) == UNSPEC_FNSTSW);
1794 /* Fall through. */
1796 case UNSPEC_FNSTSW:
1797 /* Combined fcomp+fnstsw generated for doing well with
1798 CSE. When optimizing this would have been broken
1799 up before now. */
1801 pat_src = XVECEXP (pat_src, 0, 0);
1802 gcc_assert (GET_CODE (pat_src) == COMPARE);
1804 compare_for_stack_reg (insn, regstack, pat_src);
1805 break;
1807 default:
1808 gcc_unreachable ();
1810 break;
1812 case IF_THEN_ELSE:
1813 /* This insn requires the top of stack to be the destination. */
1815 src1 = get_true_reg (&XEXP (pat_src, 1));
1816 src2 = get_true_reg (&XEXP (pat_src, 2));
1818 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1819 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1821 /* If the comparison operator is an FP comparison operator,
1822 it is handled correctly by compare_for_stack_reg () who
1823 will move the destination to the top of stack. But if the
1824 comparison operator is not an FP comparison operator, we
1825 have to handle it here. */
1826 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
1827 && REGNO (*dest) != regstack->reg[regstack->top])
1829 /* In case one of operands is the top of stack and the operands
1830 dies, it is safe to make it the destination operand by
1831 reversing the direction of cmove and avoid fxch. */
1832 if ((REGNO (*src1) == regstack->reg[regstack->top]
1833 && src1_note)
1834 || (REGNO (*src2) == regstack->reg[regstack->top]
1835 && src2_note))
1837 int idx1 = (get_hard_regnum (regstack, *src1)
1838 - FIRST_STACK_REG);
1839 int idx2 = (get_hard_regnum (regstack, *src2)
1840 - FIRST_STACK_REG);
1842 /* Make reg-stack believe that the operands are already
1843 swapped on the stack */
1844 regstack->reg[regstack->top - idx1] = REGNO (*src2);
1845 regstack->reg[regstack->top - idx2] = REGNO (*src1);
1847 /* Reverse condition to compensate the operand swap.
1848 i386 do have comparison always reversible. */
1849 PUT_CODE (XEXP (pat_src, 0),
1850 reversed_comparison_code (XEXP (pat_src, 0), insn));
1852 else
1853 emit_swap_insn (insn, regstack, *dest);
1857 rtx src_note [3];
1858 int i;
1860 src_note[0] = 0;
1861 src_note[1] = src1_note;
1862 src_note[2] = src2_note;
1864 if (STACK_REG_P (*src1))
1865 replace_reg (src1, get_hard_regnum (regstack, *src1));
1866 if (STACK_REG_P (*src2))
1867 replace_reg (src2, get_hard_regnum (regstack, *src2));
1869 for (i = 1; i <= 2; i++)
1870 if (src_note [i])
1872 int regno = REGNO (XEXP (src_note[i], 0));
1874 /* If the register that dies is not at the top of
1875 stack, then move the top of stack to the dead reg.
1876 Top of stack should never die, as it is the
1877 destination. */
1878 gcc_assert (regno != regstack->reg[regstack->top]);
1879 remove_regno_note (insn, REG_DEAD, regno);
1880 emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
1881 EMIT_AFTER);
1885 /* Make dest the top of stack. Add dest to regstack if
1886 not present. */
1887 if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
1888 regstack->reg[++regstack->top] = REGNO (*dest);
1889 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1890 replace_reg (dest, FIRST_STACK_REG);
1891 break;
1893 default:
1894 gcc_unreachable ();
1896 break;
1899 default:
1900 break;
1903 return control_flow_insn_deleted;
1906 /* Substitute hard regnums for any stack regs in INSN, which has
1907 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
1908 before the insn, and is updated with changes made here.
1910 There are several requirements and assumptions about the use of
1911 stack-like regs in asm statements. These rules are enforced by
1912 record_asm_stack_regs; see comments there for details. Any
1913 asm_operands left in the RTL at this point may be assume to meet the
1914 requirements, since record_asm_stack_regs removes any problem asm. */
1916 static void
1917 subst_asm_stack_regs (rtx insn, stack regstack)
1919 rtx body = PATTERN (insn);
1920 int alt;
1922 rtx *note_reg; /* Array of note contents */
1923 rtx **note_loc; /* Address of REG field of each note */
1924 enum reg_note *note_kind; /* The type of each note */
1926 rtx *clobber_reg = 0;
1927 rtx **clobber_loc = 0;
1929 struct stack_def temp_stack;
1930 int n_notes;
1931 int n_clobbers;
1932 rtx note;
1933 int i;
1934 int n_inputs, n_outputs;
1936 if (! check_asm_stack_operands (insn))
1937 return;
1939 /* Find out what the constraints required. If no constraint
1940 alternative matches, that is a compiler bug: we should have caught
1941 such an insn in check_asm_stack_operands. */
1942 extract_insn (insn);
1943 constrain_operands (1);
1944 alt = which_alternative;
1946 preprocess_constraints ();
1948 n_inputs = get_asm_operand_n_inputs (body);
1949 n_outputs = recog_data.n_operands - n_inputs;
1951 gcc_assert (alt >= 0);
1953 /* Strip SUBREGs here to make the following code simpler. */
1954 for (i = 0; i < recog_data.n_operands; i++)
1955 if (GET_CODE (recog_data.operand[i]) == SUBREG
1956 && REG_P (SUBREG_REG (recog_data.operand[i])))
1958 recog_data.operand_loc[i] = & SUBREG_REG (recog_data.operand[i]);
1959 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
1962 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
1964 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
1965 i++;
1967 note_reg = alloca (i * sizeof (rtx));
1968 note_loc = alloca (i * sizeof (rtx *));
1969 note_kind = alloca (i * sizeof (enum reg_note));
1971 n_notes = 0;
1972 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1974 rtx reg = XEXP (note, 0);
1975 rtx *loc = & XEXP (note, 0);
1977 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
1979 loc = & SUBREG_REG (reg);
1980 reg = SUBREG_REG (reg);
1983 if (STACK_REG_P (reg)
1984 && (REG_NOTE_KIND (note) == REG_DEAD
1985 || REG_NOTE_KIND (note) == REG_UNUSED))
1987 note_reg[n_notes] = reg;
1988 note_loc[n_notes] = loc;
1989 note_kind[n_notes] = REG_NOTE_KIND (note);
1990 n_notes++;
1994 /* Set up CLOBBER_REG and CLOBBER_LOC. */
1996 n_clobbers = 0;
1998 if (GET_CODE (body) == PARALLEL)
2000 clobber_reg = alloca (XVECLEN (body, 0) * sizeof (rtx));
2001 clobber_loc = alloca (XVECLEN (body, 0) * sizeof (rtx *));
2003 for (i = 0; i < XVECLEN (body, 0); i++)
2004 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
2006 rtx clobber = XVECEXP (body, 0, i);
2007 rtx reg = XEXP (clobber, 0);
2008 rtx *loc = & XEXP (clobber, 0);
2010 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
2012 loc = & SUBREG_REG (reg);
2013 reg = SUBREG_REG (reg);
2016 if (STACK_REG_P (reg))
2018 clobber_reg[n_clobbers] = reg;
2019 clobber_loc[n_clobbers] = loc;
2020 n_clobbers++;
2025 temp_stack = *regstack;
2027 /* Put the input regs into the desired place in TEMP_STACK. */
2029 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2030 if (STACK_REG_P (recog_data.operand[i])
2031 && reg_class_subset_p (recog_op_alt[i][alt].cl,
2032 FLOAT_REGS)
2033 && recog_op_alt[i][alt].cl != FLOAT_REGS)
2035 /* If an operand needs to be in a particular reg in
2036 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2037 these constraints are for single register classes, and
2038 reload guaranteed that operand[i] is already in that class,
2039 we can just use REGNO (recog_data.operand[i]) to know which
2040 actual reg this operand needs to be in. */
2042 int regno = get_hard_regnum (&temp_stack, recog_data.operand[i]);
2044 gcc_assert (regno >= 0);
2046 if ((unsigned int) regno != REGNO (recog_data.operand[i]))
2048 /* recog_data.operand[i] is not in the right place. Find
2049 it and swap it with whatever is already in I's place.
2050 K is where recog_data.operand[i] is now. J is where it
2051 should be. */
2052 int j, k, temp;
2054 k = temp_stack.top - (regno - FIRST_STACK_REG);
2055 j = (temp_stack.top
2056 - (REGNO (recog_data.operand[i]) - FIRST_STACK_REG));
2058 temp = temp_stack.reg[k];
2059 temp_stack.reg[k] = temp_stack.reg[j];
2060 temp_stack.reg[j] = temp;
2064 /* Emit insns before INSN to make sure the reg-stack is in the right
2065 order. */
2067 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
2069 /* Make the needed input register substitutions. Do death notes and
2070 clobbers too, because these are for inputs, not outputs. */
2072 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2073 if (STACK_REG_P (recog_data.operand[i]))
2075 int regnum = get_hard_regnum (regstack, recog_data.operand[i]);
2077 gcc_assert (regnum >= 0);
2079 replace_reg (recog_data.operand_loc[i], regnum);
2082 for (i = 0; i < n_notes; i++)
2083 if (note_kind[i] == REG_DEAD)
2085 int regnum = get_hard_regnum (regstack, note_reg[i]);
2087 gcc_assert (regnum >= 0);
2089 replace_reg (note_loc[i], regnum);
2092 for (i = 0; i < n_clobbers; i++)
2094 /* It's OK for a CLOBBER to reference a reg that is not live.
2095 Don't try to replace it in that case. */
2096 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2098 if (regnum >= 0)
2100 /* Sigh - clobbers always have QImode. But replace_reg knows
2101 that these regs can't be MODE_INT and will assert. Just put
2102 the right reg there without calling replace_reg. */
2104 *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
2108 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2110 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2111 if (STACK_REG_P (recog_data.operand[i]))
2113 /* An input reg is implicitly popped if it is tied to an
2114 output, or if there is a CLOBBER for it. */
2115 int j;
2117 for (j = 0; j < n_clobbers; j++)
2118 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
2119 break;
2121 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
2123 /* recog_data.operand[i] might not be at the top of stack.
2124 But that's OK, because all we need to do is pop the
2125 right number of regs off of the top of the reg-stack.
2126 record_asm_stack_regs guaranteed that all implicitly
2127 popped regs were grouped at the top of the reg-stack. */
2129 CLEAR_HARD_REG_BIT (regstack->reg_set,
2130 regstack->reg[regstack->top]);
2131 regstack->top--;
2135 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2136 Note that there isn't any need to substitute register numbers.
2137 ??? Explain why this is true. */
2139 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2141 /* See if there is an output for this hard reg. */
2142 int j;
2144 for (j = 0; j < n_outputs; j++)
2145 if (STACK_REG_P (recog_data.operand[j])
2146 && REGNO (recog_data.operand[j]) == (unsigned) i)
2148 regstack->reg[++regstack->top] = i;
2149 SET_HARD_REG_BIT (regstack->reg_set, i);
2150 break;
2154 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2155 input that the asm didn't implicitly pop. If the asm didn't
2156 implicitly pop an input reg, that reg will still be live.
2158 Note that we can't use find_regno_note here: the register numbers
2159 in the death notes have already been substituted. */
2161 for (i = 0; i < n_outputs; i++)
2162 if (STACK_REG_P (recog_data.operand[i]))
2164 int j;
2166 for (j = 0; j < n_notes; j++)
2167 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2168 && note_kind[j] == REG_UNUSED)
2170 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2171 EMIT_AFTER);
2172 break;
2176 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2177 if (STACK_REG_P (recog_data.operand[i]))
2179 int j;
2181 for (j = 0; j < n_notes; j++)
2182 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2183 && note_kind[j] == REG_DEAD
2184 && TEST_HARD_REG_BIT (regstack->reg_set,
2185 REGNO (recog_data.operand[i])))
2187 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2188 EMIT_AFTER);
2189 break;
2194 /* Substitute stack hard reg numbers for stack virtual registers in
2195 INSN. Non-stack register numbers are not changed. REGSTACK is the
2196 current stack content. Insns may be emitted as needed to arrange the
2197 stack for the 387 based on the contents of the insn. Return whether
2198 a control flow insn was deleted in the process. */
2200 static bool
2201 subst_stack_regs (rtx insn, stack regstack)
2203 rtx *note_link, note;
2204 bool control_flow_insn_deleted = false;
2205 int i;
2207 if (CALL_P (insn))
2209 int top = regstack->top;
2211 /* If there are any floating point parameters to be passed in
2212 registers for this call, make sure they are in the right
2213 order. */
2215 if (top >= 0)
2217 straighten_stack (insn, regstack);
2219 /* Now mark the arguments as dead after the call. */
2221 while (regstack->top >= 0)
2223 CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
2224 regstack->top--;
2229 /* Do the actual substitution if any stack regs are mentioned.
2230 Since we only record whether entire insn mentions stack regs, and
2231 subst_stack_regs_pat only works for patterns that contain stack regs,
2232 we must check each pattern in a parallel here. A call_value_pop could
2233 fail otherwise. */
2235 if (stack_regs_mentioned (insn))
2237 int n_operands = asm_noperands (PATTERN (insn));
2238 if (n_operands >= 0)
2240 /* This insn is an `asm' with operands. Decode the operands,
2241 decide how many are inputs, and do register substitution.
2242 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2244 subst_asm_stack_regs (insn, regstack);
2245 return control_flow_insn_deleted;
2248 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2249 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2251 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2253 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
2254 XVECEXP (PATTERN (insn), 0, i)
2255 = shallow_copy_rtx (XVECEXP (PATTERN (insn), 0, i));
2256 control_flow_insn_deleted
2257 |= subst_stack_regs_pat (insn, regstack,
2258 XVECEXP (PATTERN (insn), 0, i));
2261 else
2262 control_flow_insn_deleted
2263 |= subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2266 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2267 REG_UNUSED will already have been dealt with, so just return. */
2269 if (NOTE_P (insn) || INSN_DELETED_P (insn))
2270 return control_flow_insn_deleted;
2272 /* If this a noreturn call, we can't insert pop insns after it.
2273 Instead, reset the stack state to empty. */
2274 if (CALL_P (insn)
2275 && find_reg_note (insn, REG_NORETURN, NULL))
2277 regstack->top = -1;
2278 CLEAR_HARD_REG_SET (regstack->reg_set);
2279 return control_flow_insn_deleted;
2282 /* If there is a REG_UNUSED note on a stack register on this insn,
2283 the indicated reg must be popped. The REG_UNUSED note is removed,
2284 since the form of the newly emitted pop insn references the reg,
2285 making it no longer `unset'. */
2287 note_link = &REG_NOTES (insn);
2288 for (note = *note_link; note; note = XEXP (note, 1))
2289 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2291 *note_link = XEXP (note, 1);
2292 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), EMIT_AFTER);
2294 else
2295 note_link = &XEXP (note, 1);
2297 return control_flow_insn_deleted;
2300 /* Change the organization of the stack so that it fits a new basic
2301 block. Some registers might have to be popped, but there can never be
2302 a register live in the new block that is not now live.
2304 Insert any needed insns before or after INSN, as indicated by
2305 WHERE. OLD is the original stack layout, and NEW is the desired
2306 form. OLD is updated to reflect the code emitted, i.e., it will be
2307 the same as NEW upon return.
2309 This function will not preserve block_end[]. But that information
2310 is no longer needed once this has executed. */
2312 static void
2313 change_stack (rtx insn, stack old, stack new, enum emit_where where)
2315 int reg;
2316 int update_end = 0;
2318 /* Stack adjustments for the first insn in a block update the
2319 current_block's stack_in instead of inserting insns directly.
2320 compensate_edges will add the necessary code later. */
2321 if (current_block
2322 && starting_stack_p
2323 && where == EMIT_BEFORE)
2325 BLOCK_INFO (current_block)->stack_in = *new;
2326 starting_stack_p = false;
2327 *old = *new;
2328 return;
2331 /* We will be inserting new insns "backwards". If we are to insert
2332 after INSN, find the next insn, and insert before it. */
2334 if (where == EMIT_AFTER)
2336 if (current_block && BB_END (current_block) == insn)
2337 update_end = 1;
2338 insn = NEXT_INSN (insn);
2341 /* Pop any registers that are not needed in the new block. */
2343 /* If the destination block's stack already has a specified layout
2344 and contains two or more registers, use a more intelligent algorithm
2345 to pop registers that minimizes the number number of fxchs below. */
2346 if (new->top > 0)
2348 bool slots[REG_STACK_SIZE];
2349 int pops[REG_STACK_SIZE];
2350 int next, dest, topsrc;
2352 /* First pass to determine the free slots. */
2353 for (reg = 0; reg <= new->top; reg++)
2354 slots[reg] = TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]);
2356 /* Second pass to allocate preferred slots. */
2357 topsrc = -1;
2358 for (reg = old->top; reg > new->top; reg--)
2359 if (TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2361 dest = -1;
2362 for (next = 0; next <= new->top; next++)
2363 if (!slots[next] && new->reg[next] == old->reg[reg])
2365 /* If this is a preference for the new top of stack, record
2366 the fact by remembering it's old->reg in topsrc. */
2367 if (next == new->top)
2368 topsrc = reg;
2369 slots[next] = true;
2370 dest = next;
2371 break;
2373 pops[reg] = dest;
2375 else
2376 pops[reg] = reg;
2378 /* Intentionally, avoid placing the top of stack in it's correct
2379 location, if we still need to permute the stack below and we
2380 can usefully place it somewhere else. This is the case if any
2381 slot is still unallocated, in which case we should place the
2382 top of stack there. */
2383 if (topsrc != -1)
2384 for (reg = 0; reg < new->top; reg++)
2385 if (!slots[reg])
2387 pops[topsrc] = reg;
2388 slots[new->top] = false;
2389 slots[reg] = true;
2390 break;
2393 /* Third pass allocates remaining slots and emits pop insns. */
2394 next = new->top;
2395 for (reg = old->top; reg > new->top; reg--)
2397 dest = pops[reg];
2398 if (dest == -1)
2400 /* Find next free slot. */
2401 while (slots[next])
2402 next--;
2403 dest = next--;
2405 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[dest], DFmode),
2406 EMIT_BEFORE);
2409 else
2411 /* The following loop attempts to maximize the number of times we
2412 pop the top of the stack, as this permits the use of the faster
2413 ffreep instruction on platforms that support it. */
2414 int live, next;
2416 live = 0;
2417 for (reg = 0; reg <= old->top; reg++)
2418 if (TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2419 live++;
2421 next = live;
2422 while (old->top >= live)
2423 if (TEST_HARD_REG_BIT (new->reg_set, old->reg[old->top]))
2425 while (TEST_HARD_REG_BIT (new->reg_set, old->reg[next]))
2426 next--;
2427 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[next], DFmode),
2428 EMIT_BEFORE);
2430 else
2431 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[old->top], DFmode),
2432 EMIT_BEFORE);
2435 if (new->top == -2)
2437 /* If the new block has never been processed, then it can inherit
2438 the old stack order. */
2440 new->top = old->top;
2441 memcpy (new->reg, old->reg, sizeof (new->reg));
2443 else
2445 /* This block has been entered before, and we must match the
2446 previously selected stack order. */
2448 /* By now, the only difference should be the order of the stack,
2449 not their depth or liveliness. */
2451 GO_IF_HARD_REG_EQUAL (old->reg_set, new->reg_set, win);
2452 gcc_unreachable ();
2453 win:
2454 gcc_assert (old->top == new->top);
2456 /* If the stack is not empty (new->top != -1), loop here emitting
2457 swaps until the stack is correct.
2459 The worst case number of swaps emitted is N + 2, where N is the
2460 depth of the stack. In some cases, the reg at the top of
2461 stack may be correct, but swapped anyway in order to fix
2462 other regs. But since we never swap any other reg away from
2463 its correct slot, this algorithm will converge. */
2465 if (new->top != -1)
2468 /* Swap the reg at top of stack into the position it is
2469 supposed to be in, until the correct top of stack appears. */
2471 while (old->reg[old->top] != new->reg[new->top])
2473 for (reg = new->top; reg >= 0; reg--)
2474 if (new->reg[reg] == old->reg[old->top])
2475 break;
2477 gcc_assert (reg != -1);
2479 emit_swap_insn (insn, old,
2480 FP_MODE_REG (old->reg[reg], DFmode));
2483 /* See if any regs remain incorrect. If so, bring an
2484 incorrect reg to the top of stack, and let the while loop
2485 above fix it. */
2487 for (reg = new->top; reg >= 0; reg--)
2488 if (new->reg[reg] != old->reg[reg])
2490 emit_swap_insn (insn, old,
2491 FP_MODE_REG (old->reg[reg], DFmode));
2492 break;
2494 } while (reg >= 0);
2496 /* At this point there must be no differences. */
2498 for (reg = old->top; reg >= 0; reg--)
2499 gcc_assert (old->reg[reg] == new->reg[reg]);
2502 if (update_end)
2503 BB_END (current_block) = PREV_INSN (insn);
2506 /* Print stack configuration. */
2508 static void
2509 print_stack (FILE *file, stack s)
2511 if (! file)
2512 return;
2514 if (s->top == -2)
2515 fprintf (file, "uninitialized\n");
2516 else if (s->top == -1)
2517 fprintf (file, "empty\n");
2518 else
2520 int i;
2521 fputs ("[ ", file);
2522 for (i = 0; i <= s->top; ++i)
2523 fprintf (file, "%d ", s->reg[i]);
2524 fputs ("]\n", file);
2528 /* This function was doing life analysis. We now let the regular live
2529 code do it's job, so we only need to check some extra invariants
2530 that reg-stack expects. Primary among these being that all registers
2531 are initialized before use.
2533 The function returns true when code was emitted to CFG edges and
2534 commit_edge_insertions needs to be called. */
2536 static int
2537 convert_regs_entry (void)
2539 int inserted = 0;
2540 edge e;
2541 edge_iterator ei;
2543 /* Load something into each stack register live at function entry.
2544 Such live registers can be caused by uninitialized variables or
2545 functions not returning values on all paths. In order to keep
2546 the push/pop code happy, and to not scrog the register stack, we
2547 must put something in these registers. Use a QNaN.
2549 Note that we are inserting converted code here. This code is
2550 never seen by the convert_regs pass. */
2552 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2554 basic_block block = e->dest;
2555 block_info bi = BLOCK_INFO (block);
2556 int reg, top = -1;
2558 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2559 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2561 rtx init;
2563 bi->stack_in.reg[++top] = reg;
2565 init = gen_rtx_SET (VOIDmode,
2566 FP_MODE_REG (FIRST_STACK_REG, SFmode),
2567 not_a_num);
2568 insert_insn_on_edge (init, e);
2569 inserted = 1;
2572 bi->stack_in.top = top;
2575 return inserted;
2578 /* Construct the desired stack for function exit. This will either
2579 be `empty', or the function return value at top-of-stack. */
2581 static void
2582 convert_regs_exit (void)
2584 int value_reg_low, value_reg_high;
2585 stack output_stack;
2586 rtx retvalue;
2588 retvalue = stack_result (current_function_decl);
2589 value_reg_low = value_reg_high = -1;
2590 if (retvalue)
2592 value_reg_low = REGNO (retvalue);
2593 value_reg_high = value_reg_low
2594 + hard_regno_nregs[value_reg_low][GET_MODE (retvalue)] - 1;
2597 output_stack = &BLOCK_INFO (EXIT_BLOCK_PTR)->stack_in;
2598 if (value_reg_low == -1)
2599 output_stack->top = -1;
2600 else
2602 int reg;
2604 output_stack->top = value_reg_high - value_reg_low;
2605 for (reg = value_reg_low; reg <= value_reg_high; ++reg)
2607 output_stack->reg[value_reg_high - reg] = reg;
2608 SET_HARD_REG_BIT (output_stack->reg_set, reg);
2613 /* Copy the stack info from the end of edge E's source block to the
2614 start of E's destination block. */
2616 static void
2617 propagate_stack (edge e)
2619 stack src_stack = &BLOCK_INFO (e->src)->stack_out;
2620 stack dest_stack = &BLOCK_INFO (e->dest)->stack_in;
2621 int reg;
2623 /* Preserve the order of the original stack, but check whether
2624 any pops are needed. */
2625 dest_stack->top = -1;
2626 for (reg = 0; reg <= src_stack->top; ++reg)
2627 if (TEST_HARD_REG_BIT (dest_stack->reg_set, src_stack->reg[reg]))
2628 dest_stack->reg[++dest_stack->top] = src_stack->reg[reg];
2632 /* Adjust the stack of edge E's source block on exit to match the stack
2633 of it's target block upon input. The stack layouts of both blocks
2634 should have been defined by now. */
2636 static bool
2637 compensate_edge (edge e, FILE *file)
2639 basic_block source = e->src, target = e->dest;
2640 stack target_stack = &BLOCK_INFO (target)->stack_in;
2641 stack source_stack = &BLOCK_INFO (source)->stack_out;
2642 struct stack_def regstack;
2643 int reg;
2645 if (file)
2646 fprintf (file, "Edge %d->%d: ", source->index, target->index);
2648 gcc_assert (target_stack->top != -2);
2650 /* Check whether stacks are identical. */
2651 if (target_stack->top == source_stack->top)
2653 for (reg = target_stack->top; reg >= 0; --reg)
2654 if (target_stack->reg[reg] != source_stack->reg[reg])
2655 break;
2657 if (reg == -1)
2659 if (file)
2660 fprintf (file, "no changes needed\n");
2661 return false;
2665 if (file)
2667 fprintf (file, "correcting stack to ");
2668 print_stack (file, target_stack);
2671 /* Abnormal calls may appear to have values live in st(0), but the
2672 abnormal return path will not have actually loaded the values. */
2673 if (e->flags & EDGE_ABNORMAL_CALL)
2675 /* Assert that the lifetimes are as we expect -- one value
2676 live at st(0) on the end of the source block, and no
2677 values live at the beginning of the destination block.
2678 For complex return values, we may have st(1) live as well. */
2679 gcc_assert (source_stack->top == 0 || source_stack->top == 1);
2680 gcc_assert (target_stack->top == -1);
2681 return false;
2684 /* Handle non-call EH edges specially. The normal return path have
2685 values in registers. These will be popped en masse by the unwind
2686 library. */
2687 if (e->flags & EDGE_EH)
2689 gcc_assert (target_stack->top == -1);
2690 return false;
2693 /* We don't support abnormal edges. Global takes care to
2694 avoid any live register across them, so we should never
2695 have to insert instructions on such edges. */
2696 gcc_assert (! (e->flags & EDGE_ABNORMAL));
2698 /* Make a copy of source_stack as change_stack is destructive. */
2699 regstack = *source_stack;
2701 /* It is better to output directly to the end of the block
2702 instead of to the edge, because emit_swap can do minimal
2703 insn scheduling. We can do this when there is only one
2704 edge out, and it is not abnormal. */
2705 if (EDGE_COUNT (source->succs) == 1)
2707 current_block = source;
2708 change_stack (BB_END (source), &regstack, target_stack,
2709 (JUMP_P (BB_END (source)) ? EMIT_BEFORE : EMIT_AFTER));
2711 else
2713 rtx seq, after;
2715 current_block = NULL;
2716 start_sequence ();
2718 /* ??? change_stack needs some point to emit insns after. */
2719 after = emit_note (NOTE_INSN_DELETED);
2721 change_stack (after, &regstack, target_stack, EMIT_BEFORE);
2723 seq = get_insns ();
2724 end_sequence ();
2726 insert_insn_on_edge (seq, e);
2727 return true;
2729 return false;
2732 /* Traverse all non-entry edges in the CFG, and emit the necessary
2733 edge compensation code to change the stack from stack_out of the
2734 source block to the stack_in of the destination block. */
2736 static bool
2737 compensate_edges (FILE *file)
2739 bool inserted = false;
2740 basic_block bb;
2742 starting_stack_p = false;
2744 FOR_EACH_BB (bb)
2745 if (bb != ENTRY_BLOCK_PTR)
2747 edge e;
2748 edge_iterator ei;
2750 FOR_EACH_EDGE (e, ei, bb->succs)
2751 inserted |= compensate_edge (e, file);
2753 return inserted;
2756 /* Select the better of two edges E1 and E2 to use to determine the
2757 stack layout for their shared destination basic block. This is
2758 typically the more frequently executed. The edge E1 may be NULL
2759 (in which case E2 is returned), but E2 is always non-NULL. */
2761 static edge
2762 better_edge (edge e1, edge e2)
2764 if (!e1)
2765 return e2;
2767 if (EDGE_FREQUENCY (e1) > EDGE_FREQUENCY (e2))
2768 return e1;
2769 if (EDGE_FREQUENCY (e1) < EDGE_FREQUENCY (e2))
2770 return e2;
2772 if (e1->count > e2->count)
2773 return e1;
2774 if (e1->count < e2->count)
2775 return e2;
2777 /* Prefer critical edges to minimize inserting compensation code on
2778 critical edges. */
2780 if (EDGE_CRITICAL_P (e1) != EDGE_CRITICAL_P (e2))
2781 return EDGE_CRITICAL_P (e1) ? e1 : e2;
2783 /* Avoid non-deterministic behavior. */
2784 return (e1->src->index < e2->src->index) ? e1 : e2;
2787 /* Convert stack register references in one block. */
2789 static void
2790 convert_regs_1 (FILE *file, basic_block block)
2792 struct stack_def regstack;
2793 block_info bi = BLOCK_INFO (block);
2794 int reg;
2795 rtx insn, next;
2796 bool control_flow_insn_deleted = false;
2798 any_malformed_asm = false;
2800 /* Choose an initial stack layout, if one hasn't already been chosen. */
2801 if (bi->stack_in.top == -2)
2803 edge e, beste = NULL;
2804 edge_iterator ei;
2806 /* Select the best incoming edge (typically the most frequent) to
2807 use as a template for this basic block. */
2808 FOR_EACH_EDGE (e, ei, block->preds)
2809 if (BLOCK_INFO (e->src)->done)
2810 beste = better_edge (beste, e);
2812 if (beste)
2813 propagate_stack (beste);
2814 else
2816 /* No predecessors. Create an arbitrary input stack. */
2817 bi->stack_in.top = -1;
2818 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2819 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2820 bi->stack_in.reg[++bi->stack_in.top] = reg;
2824 if (file)
2826 fprintf (file, "\nBasic block %d\nInput stack: ", block->index);
2827 print_stack (file, &bi->stack_in);
2830 /* Process all insns in this block. Keep track of NEXT so that we
2831 don't process insns emitted while substituting in INSN. */
2832 current_block = block;
2833 next = BB_HEAD (block);
2834 regstack = bi->stack_in;
2835 starting_stack_p = true;
2839 insn = next;
2840 next = NEXT_INSN (insn);
2842 /* Ensure we have not missed a block boundary. */
2843 gcc_assert (next);
2844 if (insn == BB_END (block))
2845 next = NULL;
2847 /* Don't bother processing unless there is a stack reg
2848 mentioned or if it's a CALL_INSN. */
2849 if (stack_regs_mentioned (insn)
2850 || CALL_P (insn))
2852 if (file)
2854 fprintf (file, " insn %d input stack: ",
2855 INSN_UID (insn));
2856 print_stack (file, &regstack);
2858 control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
2859 starting_stack_p = false;
2862 while (next);
2864 if (file)
2866 fprintf (file, "Expected live registers [");
2867 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
2868 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg))
2869 fprintf (file, " %d", reg);
2870 fprintf (file, " ]\nOutput stack: ");
2871 print_stack (file, &regstack);
2874 insn = BB_END (block);
2875 if (JUMP_P (insn))
2876 insn = PREV_INSN (insn);
2878 /* If the function is declared to return a value, but it returns one
2879 in only some cases, some registers might come live here. Emit
2880 necessary moves for them. */
2882 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
2884 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg)
2885 && ! TEST_HARD_REG_BIT (regstack.reg_set, reg))
2887 rtx set;
2889 if (file)
2890 fprintf (file, "Emitting insn initializing reg %d\n", reg);
2892 set = gen_rtx_SET (VOIDmode, FP_MODE_REG (reg, SFmode), not_a_num);
2893 insn = emit_insn_after (set, insn);
2894 control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
2898 /* Amongst the insns possibly deleted during the substitution process above,
2899 might have been the only trapping insn in the block. We purge the now
2900 possibly dead EH edges here to avoid an ICE from fixup_abnormal_edges,
2901 called at the end of convert_regs. The order in which we process the
2902 blocks ensures that we never delete an already processed edge.
2904 Note that, at this point, the CFG may have been damaged by the emission
2905 of instructions after an abnormal call, which moves the basic block end
2906 (and is the reason why we call fixup_abnormal_edges later). So we must
2907 be sure that the trapping insn has been deleted before trying to purge
2908 dead edges, otherwise we risk purging valid edges.
2910 ??? We are normally supposed not to delete trapping insns, so we pretend
2911 that the insns deleted above don't actually trap. It would have been
2912 better to detect this earlier and avoid creating the EH edge in the first
2913 place, still, but we don't have enough information at that time. */
2915 if (control_flow_insn_deleted)
2916 purge_dead_edges (block);
2918 /* Something failed if the stack lives don't match. If we had malformed
2919 asms, we zapped the instruction itself, but that didn't produce the
2920 same pattern of register kills as before. */
2921 GO_IF_HARD_REG_EQUAL (regstack.reg_set, bi->out_reg_set, win);
2922 gcc_assert (any_malformed_asm);
2923 win:
2924 bi->stack_out = regstack;
2925 bi->done = true;
2928 /* Convert registers in all blocks reachable from BLOCK. */
2930 static void
2931 convert_regs_2 (FILE *file, basic_block block)
2933 basic_block *stack, *sp;
2935 /* We process the blocks in a top-down manner, in a way such that one block
2936 is only processed after all its predecessors. The number of predecessors
2937 of every block has already been computed. */
2939 stack = xmalloc (sizeof (*stack) * n_basic_blocks);
2940 sp = stack;
2942 *sp++ = block;
2946 edge e;
2947 edge_iterator ei;
2949 block = *--sp;
2951 /* Processing BLOCK is achieved by convert_regs_1, which may purge
2952 some dead EH outgoing edge after the deletion of the trapping
2953 insn inside the block. Since the number of predecessors of
2954 BLOCK's successors was computed based on the initial edge set,
2955 we check the necessity to process some of these successors
2956 before such an edge deletion may happen. However, there is
2957 a pitfall: if BLOCK is the only predecessor of a successor and
2958 the edge between them happens to be deleted, the successor
2959 becomes unreachable and should not be processed. The problem
2960 is that there is no way to preventively detect this case so we
2961 stack the successor in all cases and hand over the task of
2962 fixing up the discrepancy to convert_regs_1. */
2964 FOR_EACH_EDGE (e, ei, block->succs)
2965 if (! (e->flags & EDGE_DFS_BACK))
2967 BLOCK_INFO (e->dest)->predecessors--;
2968 if (!BLOCK_INFO (e->dest)->predecessors)
2969 *sp++ = e->dest;
2972 convert_regs_1 (file, block);
2974 while (sp != stack);
2976 free (stack);
2979 /* Traverse all basic blocks in a function, converting the register
2980 references in each insn from the "flat" register file that gcc uses,
2981 to the stack-like registers the 387 uses. */
2983 static void
2984 convert_regs (FILE *file)
2986 int inserted;
2987 basic_block b;
2988 edge e;
2989 edge_iterator ei;
2991 /* Initialize uninitialized registers on function entry. */
2992 inserted = convert_regs_entry ();
2994 /* Construct the desired stack for function exit. */
2995 convert_regs_exit ();
2996 BLOCK_INFO (EXIT_BLOCK_PTR)->done = 1;
2998 /* ??? Future: process inner loops first, and give them arbitrary
2999 initial stacks which emit_swap_insn can modify. This ought to
3000 prevent double fxch that often appears at the head of a loop. */
3002 /* Process all blocks reachable from all entry points. */
3003 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3004 convert_regs_2 (file, e->dest);
3006 /* ??? Process all unreachable blocks. Though there's no excuse
3007 for keeping these even when not optimizing. */
3008 FOR_EACH_BB (b)
3010 block_info bi = BLOCK_INFO (b);
3012 if (! bi->done)
3013 convert_regs_2 (file, b);
3016 inserted |= compensate_edges (file);
3018 clear_aux_for_blocks ();
3020 fixup_abnormal_edges ();
3021 if (inserted)
3022 commit_edge_insertions ();
3024 if (file)
3025 fputc ('\n', file);
3028 /* Convert register usage from "flat" register file usage to a "stack
3029 register file. FILE is the dump file, if used.
3031 Construct a CFG and run life analysis. Then convert each insn one
3032 by one. Run a last cleanup_cfg pass, if optimizing, to eliminate
3033 code duplication created when the converter inserts pop insns on
3034 the edges. */
3036 bool
3037 reg_to_stack (FILE *file)
3039 basic_block bb;
3040 int i;
3041 int max_uid;
3043 /* Clean up previous run. */
3044 stack_regs_mentioned_data = 0;
3046 /* See if there is something to do. Flow analysis is quite
3047 expensive so we might save some compilation time. */
3048 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
3049 if (regs_ever_live[i])
3050 break;
3051 if (i > LAST_STACK_REG)
3052 return false;
3054 /* Ok, floating point instructions exist. If not optimizing,
3055 build the CFG and run life analysis.
3056 Also need to rebuild life when superblock scheduling is done
3057 as it don't update liveness yet. */
3058 if (!optimize
3059 || ((flag_sched2_use_superblocks || flag_sched2_use_traces)
3060 && flag_schedule_insns_after_reload))
3062 count_or_remove_death_notes (NULL, 1);
3063 life_analysis (file, PROP_DEATH_NOTES);
3065 mark_dfs_back_edges ();
3067 /* Set up block info for each basic block. */
3068 alloc_aux_for_blocks (sizeof (struct block_info_def));
3069 FOR_EACH_BB (bb)
3071 block_info bi = BLOCK_INFO (bb);
3072 edge_iterator ei;
3073 edge e;
3074 int reg;
3076 FOR_EACH_EDGE (e, ei, bb->preds)
3077 if (!(e->flags & EDGE_DFS_BACK)
3078 && e->src != ENTRY_BLOCK_PTR)
3079 bi->predecessors++;
3081 /* Set current register status at last instruction `uninitialized'. */
3082 bi->stack_in.top = -2;
3084 /* Copy live_at_end and live_at_start into temporaries. */
3085 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
3087 if (REGNO_REG_SET_P (bb->il.rtl->global_live_at_end, reg))
3088 SET_HARD_REG_BIT (bi->out_reg_set, reg);
3089 if (REGNO_REG_SET_P (bb->il.rtl->global_live_at_start, reg))
3090 SET_HARD_REG_BIT (bi->stack_in.reg_set, reg);
3094 /* Create the replacement registers up front. */
3095 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
3097 enum machine_mode mode;
3098 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3099 mode != VOIDmode;
3100 mode = GET_MODE_WIDER_MODE (mode))
3101 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
3102 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT);
3103 mode != VOIDmode;
3104 mode = GET_MODE_WIDER_MODE (mode))
3105 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
3108 ix86_flags_rtx = gen_rtx_REG (CCmode, FLAGS_REG);
3110 /* A QNaN for initializing uninitialized variables.
3112 ??? We can't load from constant memory in PIC mode, because
3113 we're inserting these instructions before the prologue and
3114 the PIC register hasn't been set up. In that case, fall back
3115 on zero, which we can get from `ldz'. */
3117 if (flag_pic)
3118 not_a_num = CONST0_RTX (SFmode);
3119 else
3121 not_a_num = gen_lowpart (SFmode, GEN_INT (0x7fc00000));
3122 not_a_num = force_const_mem (SFmode, not_a_num);
3125 /* Allocate a cache for stack_regs_mentioned. */
3126 max_uid = get_max_uid ();
3127 VARRAY_CHAR_INIT (stack_regs_mentioned_data, max_uid + 1,
3128 "stack_regs_mentioned cache");
3130 convert_regs (file);
3132 free_aux_for_blocks ();
3133 return true;
3135 #endif /* STACK_REGS */
3137 static bool
3138 gate_handle_stack_regs (void)
3140 #ifdef STACK_REGS
3141 return 1;
3142 #else
3143 return 0;
3144 #endif
3147 /* Convert register usage from flat register file usage to a stack
3148 register file. */
3149 static void
3150 rest_of_handle_stack_regs (void)
3152 #ifdef STACK_REGS
3153 if (reg_to_stack (dump_file) && optimize)
3155 if (cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK
3156 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0))
3157 && (flag_reorder_blocks || flag_reorder_blocks_and_partition))
3159 reorder_basic_blocks (0);
3160 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK);
3163 #endif
3166 struct tree_opt_pass pass_stack_regs =
3168 "stack", /* name */
3169 gate_handle_stack_regs, /* gate */
3170 rest_of_handle_stack_regs, /* execute */
3171 NULL, /* sub */
3172 NULL, /* next */
3173 0, /* static_pass_number */
3174 TV_REG_STACK, /* tv_id */
3175 0, /* properties_required */
3176 0, /* properties_provided */
3177 0, /* properties_destroyed */
3178 0, /* todo_flags_start */
3179 TODO_dump_func |
3180 TODO_ggc_collect, /* todo_flags_finish */
3181 'k' /* letter */
3184 #include "gt-reg-stack.h"