Daily bump.
[official-gcc.git] / gcc / reg-stack.c
blobdc7550a1cee6e5b490446d139536250379e377b5
1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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-error.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 "recog.h"
167 #include "basic-block.h"
168 #include "reload.h"
169 #include "ggc.h"
170 #include "tree-pass.h"
171 #include "target.h"
172 #include "df.h"
173 #include "vecprim.h"
174 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
176 #ifdef STACK_REGS
178 /* We use this array to cache info about insns, because otherwise we
179 spend too much time in stack_regs_mentioned_p.
181 Indexed by insn UIDs. A value of zero is uninitialized, one indicates
182 the insn uses stack registers, two indicates the insn does not use
183 stack registers. */
184 static VEC(char,heap) *stack_regs_mentioned_data;
186 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
188 int regstack_completed = 0;
190 /* This is the basic stack record. TOP is an index into REG[] such
191 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
193 If TOP is -2, REG[] is not yet initialized. Stack initialization
194 consists of placing each live reg in array `reg' and setting `top'
195 appropriately.
197 REG_SET indicates which registers are live. */
199 typedef struct stack_def
201 int top; /* index to top stack element */
202 HARD_REG_SET reg_set; /* set of live registers */
203 unsigned char reg[REG_STACK_SIZE];/* register - stack mapping */
204 } *stack_ptr;
206 /* This is used to carry information about basic blocks. It is
207 attached to the AUX field of the standard CFG block. */
209 typedef struct block_info_def
211 struct stack_def stack_in; /* Input stack configuration. */
212 struct stack_def stack_out; /* Output stack configuration. */
213 HARD_REG_SET out_reg_set; /* Stack regs live on output. */
214 int done; /* True if block already converted. */
215 int predecessors; /* Number of predecessors that need
216 to be visited. */
217 } *block_info;
219 #define BLOCK_INFO(B) ((block_info) (B)->aux)
221 /* Passed to change_stack to indicate where to emit insns. */
222 enum emit_where
224 EMIT_AFTER,
225 EMIT_BEFORE
228 /* The block we're currently working on. */
229 static basic_block current_block;
231 /* In the current_block, whether we're processing the first register
232 stack or call instruction, i.e. the regstack is currently the
233 same as BLOCK_INFO(current_block)->stack_in. */
234 static bool starting_stack_p;
236 /* This is the register file for all register after conversion. */
237 static rtx
238 FP_mode_reg[LAST_STACK_REG+1-FIRST_STACK_REG][(int) MAX_MACHINE_MODE];
240 #define FP_MODE_REG(regno,mode) \
241 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int) (mode)])
243 /* Used to initialize uninitialized registers. */
244 static rtx not_a_num;
246 /* Forward declarations */
248 static int stack_regs_mentioned_p (const_rtx pat);
249 static void pop_stack (stack_ptr, int);
250 static rtx *get_true_reg (rtx *);
252 static int check_asm_stack_operands (rtx);
253 static void get_asm_operands_in_out (rtx, int *, int *);
254 static rtx stack_result (tree);
255 static void replace_reg (rtx *, int);
256 static void remove_regno_note (rtx, enum reg_note, unsigned int);
257 static int get_hard_regnum (stack_ptr, rtx);
258 static rtx emit_pop_insn (rtx, stack_ptr, rtx, enum emit_where);
259 static void swap_to_top(rtx, stack_ptr, rtx, rtx);
260 static bool move_for_stack_reg (rtx, stack_ptr, rtx);
261 static bool move_nan_for_stack_reg (rtx, stack_ptr, rtx);
262 static int swap_rtx_condition_1 (rtx);
263 static int swap_rtx_condition (rtx);
264 static void compare_for_stack_reg (rtx, stack_ptr, rtx);
265 static bool subst_stack_regs_pat (rtx, stack_ptr, rtx);
266 static void subst_asm_stack_regs (rtx, stack_ptr);
267 static bool subst_stack_regs (rtx, stack_ptr);
268 static void change_stack (rtx, stack_ptr, stack_ptr, enum emit_where);
269 static void print_stack (FILE *, stack_ptr);
270 static rtx next_flags_user (rtx);
272 /* Return nonzero if any stack register is mentioned somewhere within PAT. */
274 static int
275 stack_regs_mentioned_p (const_rtx pat)
277 const char *fmt;
278 int i;
280 if (STACK_REG_P (pat))
281 return 1;
283 fmt = GET_RTX_FORMAT (GET_CODE (pat));
284 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
286 if (fmt[i] == 'E')
288 int j;
290 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
291 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
292 return 1;
294 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
295 return 1;
298 return 0;
301 /* Return nonzero if INSN mentions stacked registers, else return zero. */
304 stack_regs_mentioned (const_rtx insn)
306 unsigned int uid, max;
307 int test;
309 if (! INSN_P (insn) || !stack_regs_mentioned_data)
310 return 0;
312 uid = INSN_UID (insn);
313 max = VEC_length (char, stack_regs_mentioned_data);
314 if (uid >= max)
316 /* Allocate some extra size to avoid too many reallocs, but
317 do not grow too quickly. */
318 max = uid + uid / 20 + 1;
319 VEC_safe_grow_cleared (char, heap, stack_regs_mentioned_data, max);
322 test = VEC_index (char, stack_regs_mentioned_data, uid);
323 if (test == 0)
325 /* This insn has yet to be examined. Do so now. */
326 test = stack_regs_mentioned_p (PATTERN (insn)) ? 1 : 2;
327 VEC_replace (char, stack_regs_mentioned_data, uid, test);
330 return test == 1;
333 static rtx ix86_flags_rtx;
335 static rtx
336 next_flags_user (rtx insn)
338 /* Search forward looking for the first use of this value.
339 Stop at block boundaries. */
341 while (insn != BB_END (current_block))
343 insn = NEXT_INSN (insn);
345 if (INSN_P (insn) && reg_mentioned_p (ix86_flags_rtx, PATTERN (insn)))
346 return insn;
348 if (CALL_P (insn))
349 return NULL_RTX;
351 return NULL_RTX;
354 /* Reorganize the stack into ascending numbers, before this insn. */
356 static void
357 straighten_stack (rtx insn, stack_ptr regstack)
359 struct stack_def temp_stack;
360 int top;
362 /* If there is only a single register on the stack, then the stack is
363 already in increasing order and no reorganization is needed.
365 Similarly if the stack is empty. */
366 if (regstack->top <= 0)
367 return;
369 COPY_HARD_REG_SET (temp_stack.reg_set, regstack->reg_set);
371 for (top = temp_stack.top = regstack->top; top >= 0; top--)
372 temp_stack.reg[top] = FIRST_STACK_REG + temp_stack.top - top;
374 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
377 /* Pop a register from the stack. */
379 static void
380 pop_stack (stack_ptr regstack, int regno)
382 int top = regstack->top;
384 CLEAR_HARD_REG_BIT (regstack->reg_set, regno);
385 regstack->top--;
386 /* If regno was not at the top of stack then adjust stack. */
387 if (regstack->reg [top] != regno)
389 int i;
390 for (i = regstack->top; i >= 0; i--)
391 if (regstack->reg [i] == regno)
393 int j;
394 for (j = i; j < top; j++)
395 regstack->reg [j] = regstack->reg [j + 1];
396 break;
401 /* Return a pointer to the REG expression within PAT. If PAT is not a
402 REG, possible enclosed by a conversion rtx, return the inner part of
403 PAT that stopped the search. */
405 static rtx *
406 get_true_reg (rtx *pat)
408 for (;;)
409 switch (GET_CODE (*pat))
411 case SUBREG:
412 /* Eliminate FP subregister accesses in favor of the
413 actual FP register in use. */
415 rtx subreg;
416 if (FP_REG_P (subreg = SUBREG_REG (*pat)))
418 int regno_off = subreg_regno_offset (REGNO (subreg),
419 GET_MODE (subreg),
420 SUBREG_BYTE (*pat),
421 GET_MODE (*pat));
422 *pat = FP_MODE_REG (REGNO (subreg) + regno_off,
423 GET_MODE (subreg));
424 return pat;
427 case FLOAT:
428 case FIX:
429 case FLOAT_EXTEND:
430 pat = & XEXP (*pat, 0);
431 break;
433 case UNSPEC:
434 if (XINT (*pat, 1) == UNSPEC_TRUNC_NOOP
435 || XINT (*pat, 1) == UNSPEC_LDA)
436 pat = & XVECEXP (*pat, 0, 0);
437 return pat;
439 case FLOAT_TRUNCATE:
440 if (!flag_unsafe_math_optimizations)
441 return pat;
442 pat = & XEXP (*pat, 0);
443 break;
445 default:
446 return pat;
450 /* Set if we find any malformed asms in a block. */
451 static bool any_malformed_asm;
453 /* There are many rules that an asm statement for stack-like regs must
454 follow. Those rules are explained at the top of this file: the rule
455 numbers below refer to that explanation. */
457 static int
458 check_asm_stack_operands (rtx insn)
460 int i;
461 int n_clobbers;
462 int malformed_asm = 0;
463 rtx body = PATTERN (insn);
465 char reg_used_as_output[FIRST_PSEUDO_REGISTER];
466 char implicitly_dies[FIRST_PSEUDO_REGISTER];
467 int alt;
469 rtx *clobber_reg = 0;
470 int n_inputs, n_outputs;
472 /* Find out what the constraints require. If no constraint
473 alternative matches, this asm is malformed. */
474 extract_insn (insn);
475 constrain_operands (1);
476 alt = which_alternative;
478 preprocess_constraints ();
480 get_asm_operands_in_out (body, &n_outputs, &n_inputs);
482 if (alt < 0)
484 malformed_asm = 1;
485 /* Avoid further trouble with this insn. */
486 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
487 return 0;
490 /* Strip SUBREGs here to make the following code simpler. */
491 for (i = 0; i < recog_data.n_operands; i++)
492 if (GET_CODE (recog_data.operand[i]) == SUBREG
493 && REG_P (SUBREG_REG (recog_data.operand[i])))
494 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
496 /* Set up CLOBBER_REG. */
498 n_clobbers = 0;
500 if (GET_CODE (body) == PARALLEL)
502 clobber_reg = XALLOCAVEC (rtx, XVECLEN (body, 0));
504 for (i = 0; i < XVECLEN (body, 0); i++)
505 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
507 rtx clobber = XVECEXP (body, 0, i);
508 rtx reg = XEXP (clobber, 0);
510 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
511 reg = SUBREG_REG (reg);
513 if (STACK_REG_P (reg))
515 clobber_reg[n_clobbers] = reg;
516 n_clobbers++;
521 /* Enforce rule #4: Output operands must specifically indicate which
522 reg an output appears in after an asm. "=f" is not allowed: the
523 operand constraints must select a class with a single reg.
525 Also enforce rule #5: Output operands must start at the top of
526 the reg-stack: output operands may not "skip" a reg. */
528 memset (reg_used_as_output, 0, sizeof (reg_used_as_output));
529 for (i = 0; i < n_outputs; i++)
530 if (STACK_REG_P (recog_data.operand[i]))
532 if (reg_class_size[(int) recog_op_alt[i][alt].cl] != 1)
534 error_for_asm (insn, "output constraint %d must specify a single register", i);
535 malformed_asm = 1;
537 else
539 int j;
541 for (j = 0; j < n_clobbers; j++)
542 if (REGNO (recog_data.operand[i]) == REGNO (clobber_reg[j]))
544 error_for_asm (insn, "output constraint %d cannot be specified together with \"%s\" clobber",
545 i, reg_names [REGNO (clobber_reg[j])]);
546 malformed_asm = 1;
547 break;
549 if (j == n_clobbers)
550 reg_used_as_output[REGNO (recog_data.operand[i])] = 1;
555 /* Search for first non-popped reg. */
556 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
557 if (! reg_used_as_output[i])
558 break;
560 /* If there are any other popped regs, that's an error. */
561 for (; i < LAST_STACK_REG + 1; i++)
562 if (reg_used_as_output[i])
563 break;
565 if (i != LAST_STACK_REG + 1)
567 error_for_asm (insn, "output regs must be grouped at top of stack");
568 malformed_asm = 1;
571 /* Enforce rule #2: All implicitly popped input regs must be closer
572 to the top of the reg-stack than any input that is not implicitly
573 popped. */
575 memset (implicitly_dies, 0, sizeof (implicitly_dies));
576 for (i = n_outputs; i < n_outputs + n_inputs; i++)
577 if (STACK_REG_P (recog_data.operand[i]))
579 /* An input reg is implicitly popped if it is tied to an
580 output, or if there is a CLOBBER for it. */
581 int j;
583 for (j = 0; j < n_clobbers; j++)
584 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
585 break;
587 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
588 implicitly_dies[REGNO (recog_data.operand[i])] = 1;
591 /* Search for first non-popped reg. */
592 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
593 if (! implicitly_dies[i])
594 break;
596 /* If there are any other popped regs, that's an error. */
597 for (; i < LAST_STACK_REG + 1; i++)
598 if (implicitly_dies[i])
599 break;
601 if (i != LAST_STACK_REG + 1)
603 error_for_asm (insn,
604 "implicitly popped regs must be grouped at top of stack");
605 malformed_asm = 1;
608 /* Enforce rule #3: If any input operand uses the "f" constraint, all
609 output constraints must use the "&" earlyclobber.
611 ??? Detect this more deterministically by having constrain_asm_operands
612 record any earlyclobber. */
614 for (i = n_outputs; i < n_outputs + n_inputs; i++)
615 if (recog_op_alt[i][alt].matches == -1)
617 int j;
619 for (j = 0; j < n_outputs; j++)
620 if (operands_match_p (recog_data.operand[j], recog_data.operand[i]))
622 error_for_asm (insn,
623 "output operand %d must use %<&%> constraint", j);
624 malformed_asm = 1;
628 if (malformed_asm)
630 /* Avoid further trouble with this insn. */
631 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
632 any_malformed_asm = true;
633 return 0;
636 return 1;
639 /* Calculate the number of inputs and outputs in BODY, an
640 asm_operands. N_OPERANDS is the total number of operands, and
641 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
642 placed. */
644 static void
645 get_asm_operands_in_out (rtx body, int *pout, int *pin)
647 rtx asmop = extract_asm_operands (body);
649 *pin = ASM_OPERANDS_INPUT_LENGTH (asmop);
650 *pout = (recog_data.n_operands
651 - ASM_OPERANDS_INPUT_LENGTH (asmop)
652 - ASM_OPERANDS_LABEL_LENGTH (asmop));
655 /* If current function returns its result in an fp stack register,
656 return the REG. Otherwise, return 0. */
658 static rtx
659 stack_result (tree decl)
661 rtx result;
663 /* If the value is supposed to be returned in memory, then clearly
664 it is not returned in a stack register. */
665 if (aggregate_value_p (DECL_RESULT (decl), decl))
666 return 0;
668 result = DECL_RTL_IF_SET (DECL_RESULT (decl));
669 if (result != 0)
670 result = targetm.calls.function_value (TREE_TYPE (DECL_RESULT (decl)),
671 decl, true);
673 return result != 0 && STACK_REG_P (result) ? result : 0;
678 * This section deals with stack register substitution, and forms the second
679 * pass over the RTL.
682 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
683 the desired hard REGNO. */
685 static void
686 replace_reg (rtx *reg, int regno)
688 gcc_assert (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG));
689 gcc_assert (STACK_REG_P (*reg));
691 gcc_assert (SCALAR_FLOAT_MODE_P (GET_MODE (*reg))
692 || GET_MODE_CLASS (GET_MODE (*reg)) == MODE_COMPLEX_FLOAT);
694 *reg = FP_MODE_REG (regno, GET_MODE (*reg));
697 /* Remove a note of type NOTE, which must be found, for register
698 number REGNO from INSN. Remove only one such note. */
700 static void
701 remove_regno_note (rtx insn, enum reg_note note, unsigned int regno)
703 rtx *note_link, this_rtx;
705 note_link = &REG_NOTES (insn);
706 for (this_rtx = *note_link; this_rtx; this_rtx = XEXP (this_rtx, 1))
707 if (REG_NOTE_KIND (this_rtx) == note
708 && REG_P (XEXP (this_rtx, 0)) && REGNO (XEXP (this_rtx, 0)) == regno)
710 *note_link = XEXP (this_rtx, 1);
711 return;
713 else
714 note_link = &XEXP (this_rtx, 1);
716 gcc_unreachable ();
719 /* Find the hard register number of virtual register REG in REGSTACK.
720 The hard register number is relative to the top of the stack. -1 is
721 returned if the register is not found. */
723 static int
724 get_hard_regnum (stack_ptr regstack, rtx reg)
726 int i;
728 gcc_assert (STACK_REG_P (reg));
730 for (i = regstack->top; i >= 0; i--)
731 if (regstack->reg[i] == REGNO (reg))
732 break;
734 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
737 /* Emit an insn to pop virtual register REG before or after INSN.
738 REGSTACK is the stack state after INSN and is updated to reflect this
739 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
740 is represented as a SET whose destination is the register to be popped
741 and source is the top of stack. A death note for the top of stack
742 cases the movdf pattern to pop. */
744 static rtx
745 emit_pop_insn (rtx insn, stack_ptr regstack, rtx reg, enum emit_where where)
747 rtx pop_insn, pop_rtx;
748 int hard_regno;
750 /* For complex types take care to pop both halves. These may survive in
751 CLOBBER and USE expressions. */
752 if (COMPLEX_MODE_P (GET_MODE (reg)))
754 rtx reg1 = FP_MODE_REG (REGNO (reg), DFmode);
755 rtx reg2 = FP_MODE_REG (REGNO (reg) + 1, DFmode);
757 pop_insn = NULL_RTX;
758 if (get_hard_regnum (regstack, reg1) >= 0)
759 pop_insn = emit_pop_insn (insn, regstack, reg1, where);
760 if (get_hard_regnum (regstack, reg2) >= 0)
761 pop_insn = emit_pop_insn (insn, regstack, reg2, where);
762 gcc_assert (pop_insn);
763 return pop_insn;
766 hard_regno = get_hard_regnum (regstack, reg);
768 gcc_assert (hard_regno >= FIRST_STACK_REG);
770 pop_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG (hard_regno, DFmode),
771 FP_MODE_REG (FIRST_STACK_REG, DFmode));
773 if (where == EMIT_AFTER)
774 pop_insn = emit_insn_after (pop_rtx, insn);
775 else
776 pop_insn = emit_insn_before (pop_rtx, insn);
778 add_reg_note (pop_insn, REG_DEAD, FP_MODE_REG (FIRST_STACK_REG, DFmode));
780 regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
781 = regstack->reg[regstack->top];
782 regstack->top -= 1;
783 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
785 return pop_insn;
788 /* Emit an insn before or after INSN to swap virtual register REG with
789 the top of stack. REGSTACK is the stack state before the swap, and
790 is updated to reflect the swap. A swap insn is represented as a
791 PARALLEL of two patterns: each pattern moves one reg to the other.
793 If REG is already at the top of the stack, no insn is emitted. */
795 static void
796 emit_swap_insn (rtx insn, stack_ptr regstack, rtx reg)
798 int hard_regno;
799 rtx swap_rtx;
800 int tmp, other_reg; /* swap regno temps */
801 rtx i1; /* the stack-reg insn prior to INSN */
802 rtx i1set = NULL_RTX; /* the SET rtx within I1 */
804 hard_regno = get_hard_regnum (regstack, reg);
806 if (hard_regno == FIRST_STACK_REG)
807 return;
808 if (hard_regno == -1)
810 /* Something failed if the register wasn't on the stack. If we had
811 malformed asms, we zapped the instruction itself, but that didn't
812 produce the same pattern of register sets as before. To prevent
813 further failure, adjust REGSTACK to include REG at TOP. */
814 gcc_assert (any_malformed_asm);
815 regstack->reg[++regstack->top] = REGNO (reg);
816 return;
818 gcc_assert (hard_regno >= FIRST_STACK_REG);
820 other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
822 tmp = regstack->reg[other_reg];
823 regstack->reg[other_reg] = regstack->reg[regstack->top];
824 regstack->reg[regstack->top] = tmp;
826 /* Find the previous insn involving stack regs, but don't pass a
827 block boundary. */
828 i1 = NULL;
829 if (current_block && insn != BB_HEAD (current_block))
831 rtx tmp = PREV_INSN (insn);
832 rtx limit = PREV_INSN (BB_HEAD (current_block));
833 while (tmp != limit)
835 if (LABEL_P (tmp)
836 || CALL_P (tmp)
837 || NOTE_INSN_BASIC_BLOCK_P (tmp)
838 || (NONJUMP_INSN_P (tmp)
839 && stack_regs_mentioned (tmp)))
841 i1 = tmp;
842 break;
844 tmp = PREV_INSN (tmp);
848 if (i1 != NULL_RTX
849 && (i1set = single_set (i1)) != NULL_RTX)
851 rtx i1src = *get_true_reg (&SET_SRC (i1set));
852 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
854 /* If the previous register stack push was from the reg we are to
855 swap with, omit the swap. */
857 if (REG_P (i1dest) && REGNO (i1dest) == FIRST_STACK_REG
858 && REG_P (i1src)
859 && REGNO (i1src) == (unsigned) hard_regno - 1
860 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
861 return;
863 /* If the previous insn wrote to the reg we are to swap with,
864 omit the swap. */
866 if (REG_P (i1dest) && REGNO (i1dest) == (unsigned) hard_regno
867 && REG_P (i1src) && REGNO (i1src) == FIRST_STACK_REG
868 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
869 return;
872 /* Avoid emitting the swap if this is the first register stack insn
873 of the current_block. Instead update the current_block's stack_in
874 and let compensate edges take care of this for us. */
875 if (current_block && starting_stack_p)
877 BLOCK_INFO (current_block)->stack_in = *regstack;
878 starting_stack_p = false;
879 return;
882 swap_rtx = gen_swapxf (FP_MODE_REG (hard_regno, XFmode),
883 FP_MODE_REG (FIRST_STACK_REG, XFmode));
885 if (i1)
886 emit_insn_after (swap_rtx, i1);
887 else if (current_block)
888 emit_insn_before (swap_rtx, BB_HEAD (current_block));
889 else
890 emit_insn_before (swap_rtx, insn);
893 /* Emit an insns before INSN to swap virtual register SRC1 with
894 the top of stack and virtual register SRC2 with second stack
895 slot. REGSTACK is the stack state before the swaps, and
896 is updated to reflect the swaps. A swap insn is represented as a
897 PARALLEL of two patterns: each pattern moves one reg to the other.
899 If SRC1 and/or SRC2 are already at the right place, no swap insn
900 is emitted. */
902 static void
903 swap_to_top (rtx insn, stack_ptr regstack, rtx src1, rtx src2)
905 struct stack_def temp_stack;
906 int regno, j, k, temp;
908 temp_stack = *regstack;
910 /* Place operand 1 at the top of stack. */
911 regno = get_hard_regnum (&temp_stack, src1);
912 gcc_assert (regno >= 0);
913 if (regno != FIRST_STACK_REG)
915 k = temp_stack.top - (regno - FIRST_STACK_REG);
916 j = temp_stack.top;
918 temp = temp_stack.reg[k];
919 temp_stack.reg[k] = temp_stack.reg[j];
920 temp_stack.reg[j] = temp;
923 /* Place operand 2 next on the stack. */
924 regno = get_hard_regnum (&temp_stack, src2);
925 gcc_assert (regno >= 0);
926 if (regno != FIRST_STACK_REG + 1)
928 k = temp_stack.top - (regno - FIRST_STACK_REG);
929 j = temp_stack.top - 1;
931 temp = temp_stack.reg[k];
932 temp_stack.reg[k] = temp_stack.reg[j];
933 temp_stack.reg[j] = temp;
936 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
939 /* Handle a move to or from a stack register in PAT, which is in INSN.
940 REGSTACK is the current stack. Return whether a control flow insn
941 was deleted in the process. */
943 static bool
944 move_for_stack_reg (rtx insn, stack_ptr regstack, rtx pat)
946 rtx *psrc = get_true_reg (&SET_SRC (pat));
947 rtx *pdest = get_true_reg (&SET_DEST (pat));
948 rtx src, dest;
949 rtx note;
950 bool control_flow_insn_deleted = false;
952 src = *psrc; dest = *pdest;
954 if (STACK_REG_P (src) && STACK_REG_P (dest))
956 /* Write from one stack reg to another. If SRC dies here, then
957 just change the register mapping and delete the insn. */
959 note = find_regno_note (insn, REG_DEAD, REGNO (src));
960 if (note)
962 int i;
964 /* If this is a no-op move, there must not be a REG_DEAD note. */
965 gcc_assert (REGNO (src) != REGNO (dest));
967 for (i = regstack->top; i >= 0; i--)
968 if (regstack->reg[i] == REGNO (src))
969 break;
971 /* The destination must be dead, or life analysis is borked. */
972 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
974 /* If the source is not live, this is yet another case of
975 uninitialized variables. Load up a NaN instead. */
976 if (i < 0)
977 return move_nan_for_stack_reg (insn, regstack, dest);
979 /* It is possible that the dest is unused after this insn.
980 If so, just pop the src. */
982 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
983 emit_pop_insn (insn, regstack, src, EMIT_AFTER);
984 else
986 regstack->reg[i] = REGNO (dest);
987 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
988 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
991 control_flow_insn_deleted |= control_flow_insn_p (insn);
992 delete_insn (insn);
993 return control_flow_insn_deleted;
996 /* The source reg does not die. */
998 /* If this appears to be a no-op move, delete it, or else it
999 will confuse the machine description output patterns. But if
1000 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1001 for REG_UNUSED will not work for deleted insns. */
1003 if (REGNO (src) == REGNO (dest))
1005 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1006 emit_pop_insn (insn, regstack, dest, EMIT_AFTER);
1008 control_flow_insn_deleted |= control_flow_insn_p (insn);
1009 delete_insn (insn);
1010 return control_flow_insn_deleted;
1013 /* The destination ought to be dead. */
1014 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
1016 replace_reg (psrc, get_hard_regnum (regstack, src));
1018 regstack->reg[++regstack->top] = REGNO (dest);
1019 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1020 replace_reg (pdest, FIRST_STACK_REG);
1022 else if (STACK_REG_P (src))
1024 /* Save from a stack reg to MEM, or possibly integer reg. Since
1025 only top of stack may be saved, emit an exchange first if
1026 needs be. */
1028 emit_swap_insn (insn, regstack, src);
1030 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1031 if (note)
1033 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1034 regstack->top--;
1035 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1037 else if ((GET_MODE (src) == XFmode)
1038 && regstack->top < REG_STACK_SIZE - 1)
1040 /* A 387 cannot write an XFmode value to a MEM without
1041 clobbering the source reg. The output code can handle
1042 this by reading back the value from the MEM.
1043 But it is more efficient to use a temp register if one is
1044 available. Push the source value here if the register
1045 stack is not full, and then write the value to memory via
1046 a pop. */
1047 rtx push_rtx;
1048 rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, GET_MODE (src));
1050 push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
1051 emit_insn_before (push_rtx, insn);
1052 add_reg_note (insn, REG_DEAD, top_stack_reg);
1055 replace_reg (psrc, FIRST_STACK_REG);
1057 else
1059 rtx pat = PATTERN (insn);
1061 gcc_assert (STACK_REG_P (dest));
1063 /* Load from MEM, or possibly integer REG or constant, into the
1064 stack regs. The actual target is always the top of the
1065 stack. The stack mapping is changed to reflect that DEST is
1066 now at top of stack. */
1068 /* The destination ought to be dead. However, there is a
1069 special case with i387 UNSPEC_TAN, where destination is live
1070 (an argument to fptan) but inherent load of 1.0 is modelled
1071 as a load from a constant. */
1072 if (GET_CODE (pat) == PARALLEL
1073 && XVECLEN (pat, 0) == 2
1074 && GET_CODE (XVECEXP (pat, 0, 1)) == SET
1075 && GET_CODE (SET_SRC (XVECEXP (pat, 0, 1))) == UNSPEC
1076 && XINT (SET_SRC (XVECEXP (pat, 0, 1)), 1) == UNSPEC_TAN)
1077 emit_swap_insn (insn, regstack, dest);
1078 else
1079 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
1081 gcc_assert (regstack->top < REG_STACK_SIZE);
1083 regstack->reg[++regstack->top] = REGNO (dest);
1084 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1085 replace_reg (pdest, FIRST_STACK_REG);
1088 return control_flow_insn_deleted;
1091 /* A helper function which replaces INSN with a pattern that loads up
1092 a NaN into DEST, then invokes move_for_stack_reg. */
1094 static bool
1095 move_nan_for_stack_reg (rtx insn, stack_ptr regstack, rtx dest)
1097 rtx pat;
1099 dest = FP_MODE_REG (REGNO (dest), SFmode);
1100 pat = gen_rtx_SET (VOIDmode, dest, not_a_num);
1101 PATTERN (insn) = pat;
1102 INSN_CODE (insn) = -1;
1104 return move_for_stack_reg (insn, regstack, pat);
1107 /* Swap the condition on a branch, if there is one. Return true if we
1108 found a condition to swap. False if the condition was not used as
1109 such. */
1111 static int
1112 swap_rtx_condition_1 (rtx pat)
1114 const char *fmt;
1115 int i, r = 0;
1117 if (COMPARISON_P (pat))
1119 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1120 r = 1;
1122 else
1124 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1125 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1127 if (fmt[i] == 'E')
1129 int j;
1131 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1132 r |= swap_rtx_condition_1 (XVECEXP (pat, i, j));
1134 else if (fmt[i] == 'e')
1135 r |= swap_rtx_condition_1 (XEXP (pat, i));
1139 return r;
1142 static int
1143 swap_rtx_condition (rtx insn)
1145 rtx pat = PATTERN (insn);
1147 /* We're looking for a single set to cc0 or an HImode temporary. */
1149 if (GET_CODE (pat) == SET
1150 && REG_P (SET_DEST (pat))
1151 && REGNO (SET_DEST (pat)) == FLAGS_REG)
1153 insn = next_flags_user (insn);
1154 if (insn == NULL_RTX)
1155 return 0;
1156 pat = PATTERN (insn);
1159 /* See if this is, or ends in, a fnstsw. If so, we're not doing anything
1160 with the cc value right now. We may be able to search for one
1161 though. */
1163 if (GET_CODE (pat) == SET
1164 && GET_CODE (SET_SRC (pat)) == UNSPEC
1165 && XINT (SET_SRC (pat), 1) == UNSPEC_FNSTSW)
1167 rtx dest = SET_DEST (pat);
1169 /* Search forward looking for the first use of this value.
1170 Stop at block boundaries. */
1171 while (insn != BB_END (current_block))
1173 insn = NEXT_INSN (insn);
1174 if (INSN_P (insn) && reg_mentioned_p (dest, insn))
1175 break;
1176 if (CALL_P (insn))
1177 return 0;
1180 /* We haven't found it. */
1181 if (insn == BB_END (current_block))
1182 return 0;
1184 /* So we've found the insn using this value. If it is anything
1185 other than sahf or the value does not die (meaning we'd have
1186 to search further), then we must give up. */
1187 pat = PATTERN (insn);
1188 if (GET_CODE (pat) != SET
1189 || GET_CODE (SET_SRC (pat)) != UNSPEC
1190 || XINT (SET_SRC (pat), 1) != UNSPEC_SAHF
1191 || ! dead_or_set_p (insn, dest))
1192 return 0;
1194 /* Now we are prepared to handle this as a normal cc0 setter. */
1195 insn = next_flags_user (insn);
1196 if (insn == NULL_RTX)
1197 return 0;
1198 pat = PATTERN (insn);
1201 if (swap_rtx_condition_1 (pat))
1203 int fail = 0;
1204 INSN_CODE (insn) = -1;
1205 if (recog_memoized (insn) == -1)
1206 fail = 1;
1207 /* In case the flags don't die here, recurse to try fix
1208 following user too. */
1209 else if (! dead_or_set_p (insn, ix86_flags_rtx))
1211 insn = next_flags_user (insn);
1212 if (!insn || !swap_rtx_condition (insn))
1213 fail = 1;
1215 if (fail)
1217 swap_rtx_condition_1 (pat);
1218 return 0;
1220 return 1;
1222 return 0;
1225 /* Handle a comparison. Special care needs to be taken to avoid
1226 causing comparisons that a 387 cannot do correctly, such as EQ.
1228 Also, a pop insn may need to be emitted. The 387 does have an
1229 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1230 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1231 set up. */
1233 static void
1234 compare_for_stack_reg (rtx insn, stack_ptr regstack, rtx pat_src)
1236 rtx *src1, *src2;
1237 rtx src1_note, src2_note;
1239 src1 = get_true_reg (&XEXP (pat_src, 0));
1240 src2 = get_true_reg (&XEXP (pat_src, 1));
1242 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1243 registers that die in this insn - move those to stack top first. */
1244 if ((! STACK_REG_P (*src1)
1245 || (STACK_REG_P (*src2)
1246 && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
1247 && swap_rtx_condition (insn))
1249 rtx temp;
1250 temp = XEXP (pat_src, 0);
1251 XEXP (pat_src, 0) = XEXP (pat_src, 1);
1252 XEXP (pat_src, 1) = temp;
1254 src1 = get_true_reg (&XEXP (pat_src, 0));
1255 src2 = get_true_reg (&XEXP (pat_src, 1));
1257 INSN_CODE (insn) = -1;
1260 /* We will fix any death note later. */
1262 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1264 if (STACK_REG_P (*src2))
1265 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1266 else
1267 src2_note = NULL_RTX;
1269 emit_swap_insn (insn, regstack, *src1);
1271 replace_reg (src1, FIRST_STACK_REG);
1273 if (STACK_REG_P (*src2))
1274 replace_reg (src2, get_hard_regnum (regstack, *src2));
1276 if (src1_note)
1278 pop_stack (regstack, REGNO (XEXP (src1_note, 0)));
1279 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1282 /* If the second operand dies, handle that. But if the operands are
1283 the same stack register, don't bother, because only one death is
1284 needed, and it was just handled. */
1286 if (src2_note
1287 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
1288 && REGNO (*src1) == REGNO (*src2)))
1290 /* As a special case, two regs may die in this insn if src2 is
1291 next to top of stack and the top of stack also dies. Since
1292 we have already popped src1, "next to top of stack" is really
1293 at top (FIRST_STACK_REG) now. */
1295 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1296 && src1_note)
1298 pop_stack (regstack, REGNO (XEXP (src2_note, 0)));
1299 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1301 else
1303 /* The 386 can only represent death of the first operand in
1304 the case handled above. In all other cases, emit a separate
1305 pop and remove the death note from here. */
1307 /* link_cc0_insns (insn); */
1309 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
1311 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
1312 EMIT_AFTER);
1317 /* Substitute new registers in LOC, which is part of a debug insn.
1318 REGSTACK is the current register layout. */
1320 static int
1321 subst_stack_regs_in_debug_insn (rtx *loc, void *data)
1323 stack_ptr regstack = (stack_ptr)data;
1324 int hard_regno;
1326 if (!STACK_REG_P (*loc))
1327 return 0;
1329 hard_regno = get_hard_regnum (regstack, *loc);
1331 /* If we can't find an active register, reset this debug insn. */
1332 if (hard_regno == -1)
1333 return 1;
1335 gcc_assert (hard_regno >= FIRST_STACK_REG);
1337 replace_reg (loc, hard_regno);
1339 return -1;
1342 /* Substitute hardware stack regs in debug insn INSN, using stack
1343 layout REGSTACK. If we can't find a hardware stack reg for any of
1344 the REGs in it, reset the debug insn. */
1346 static void
1347 subst_all_stack_regs_in_debug_insn (rtx insn, struct stack_def *regstack)
1349 int ret = for_each_rtx (&INSN_VAR_LOCATION_LOC (insn),
1350 subst_stack_regs_in_debug_insn,
1351 regstack);
1353 if (ret == 1)
1354 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
1355 else
1356 gcc_checking_assert (ret == 0);
1359 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1360 is the current register layout. Return whether a control flow insn
1361 was deleted in the process. */
1363 static bool
1364 subst_stack_regs_pat (rtx insn, stack_ptr regstack, rtx pat)
1366 rtx *dest, *src;
1367 bool control_flow_insn_deleted = false;
1369 switch (GET_CODE (pat))
1371 case USE:
1372 /* Deaths in USE insns can happen in non optimizing compilation.
1373 Handle them by popping the dying register. */
1374 src = get_true_reg (&XEXP (pat, 0));
1375 if (STACK_REG_P (*src)
1376 && find_regno_note (insn, REG_DEAD, REGNO (*src)))
1378 /* USEs are ignored for liveness information so USEs of dead
1379 register might happen. */
1380 if (TEST_HARD_REG_BIT (regstack->reg_set, REGNO (*src)))
1381 emit_pop_insn (insn, regstack, *src, EMIT_AFTER);
1382 return control_flow_insn_deleted;
1384 /* Uninitialized USE might happen for functions returning uninitialized
1385 value. We will properly initialize the USE on the edge to EXIT_BLOCK,
1386 so it is safe to ignore the use here. This is consistent with behavior
1387 of dataflow analyzer that ignores USE too. (This also imply that
1388 forcibly initializing the register to NaN here would lead to ICE later,
1389 since the REG_DEAD notes are not issued.) */
1390 break;
1392 case VAR_LOCATION:
1393 gcc_unreachable ();
1395 case CLOBBER:
1397 rtx note;
1399 dest = get_true_reg (&XEXP (pat, 0));
1400 if (STACK_REG_P (*dest))
1402 note = find_reg_note (insn, REG_DEAD, *dest);
1404 if (pat != PATTERN (insn))
1406 /* The fix_truncdi_1 pattern wants to be able to
1407 allocate its own scratch register. It does this by
1408 clobbering an fp reg so that it is assured of an
1409 empty reg-stack register. If the register is live,
1410 kill it now. Remove the DEAD/UNUSED note so we
1411 don't try to kill it later too.
1413 In reality the UNUSED note can be absent in some
1414 complicated cases when the register is reused for
1415 partially set variable. */
1417 if (note)
1418 emit_pop_insn (insn, regstack, *dest, EMIT_BEFORE);
1419 else
1420 note = find_reg_note (insn, REG_UNUSED, *dest);
1421 if (note)
1422 remove_note (insn, note);
1423 replace_reg (dest, FIRST_STACK_REG + 1);
1425 else
1427 /* A top-level clobber with no REG_DEAD, and no hard-regnum
1428 indicates an uninitialized value. Because reload removed
1429 all other clobbers, this must be due to a function
1430 returning without a value. Load up a NaN. */
1432 if (!note)
1434 rtx t = *dest;
1435 if (COMPLEX_MODE_P (GET_MODE (t)))
1437 rtx u = FP_MODE_REG (REGNO (t) + 1, SFmode);
1438 if (get_hard_regnum (regstack, u) == -1)
1440 rtx pat2 = gen_rtx_CLOBBER (VOIDmode, u);
1441 rtx insn2 = emit_insn_before (pat2, insn);
1442 control_flow_insn_deleted
1443 |= move_nan_for_stack_reg (insn2, regstack, u);
1446 if (get_hard_regnum (regstack, t) == -1)
1447 control_flow_insn_deleted
1448 |= move_nan_for_stack_reg (insn, regstack, t);
1452 break;
1455 case SET:
1457 rtx *src1 = (rtx *) 0, *src2;
1458 rtx src1_note, src2_note;
1459 rtx pat_src;
1461 dest = get_true_reg (&SET_DEST (pat));
1462 src = get_true_reg (&SET_SRC (pat));
1463 pat_src = SET_SRC (pat);
1465 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1466 if (STACK_REG_P (*src)
1467 || (STACK_REG_P (*dest)
1468 && (REG_P (*src) || MEM_P (*src)
1469 || CONST_DOUBLE_P (*src))))
1471 control_flow_insn_deleted |= move_for_stack_reg (insn, regstack, pat);
1472 break;
1475 switch (GET_CODE (pat_src))
1477 case COMPARE:
1478 compare_for_stack_reg (insn, regstack, pat_src);
1479 break;
1481 case CALL:
1483 int count;
1484 for (count = hard_regno_nregs[REGNO (*dest)][GET_MODE (*dest)];
1485 --count >= 0;)
1487 regstack->reg[++regstack->top] = REGNO (*dest) + count;
1488 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
1491 replace_reg (dest, FIRST_STACK_REG);
1492 break;
1494 case REG:
1495 /* This is a `tstM2' case. */
1496 gcc_assert (*dest == cc0_rtx);
1497 src1 = src;
1499 /* Fall through. */
1501 case FLOAT_TRUNCATE:
1502 case SQRT:
1503 case ABS:
1504 case NEG:
1505 /* These insns only operate on the top of the stack. DEST might
1506 be cc0_rtx if we're processing a tstM pattern. Also, it's
1507 possible that the tstM case results in a REG_DEAD note on the
1508 source. */
1510 if (src1 == 0)
1511 src1 = get_true_reg (&XEXP (pat_src, 0));
1513 emit_swap_insn (insn, regstack, *src1);
1515 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1517 if (STACK_REG_P (*dest))
1518 replace_reg (dest, FIRST_STACK_REG);
1520 if (src1_note)
1522 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1523 regstack->top--;
1524 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1527 replace_reg (src1, FIRST_STACK_REG);
1528 break;
1530 case MINUS:
1531 case DIV:
1532 /* On i386, reversed forms of subM3 and divM3 exist for
1533 MODE_FLOAT, so the same code that works for addM3 and mulM3
1534 can be used. */
1535 case MULT:
1536 case PLUS:
1537 /* These insns can accept the top of stack as a destination
1538 from a stack reg or mem, or can use the top of stack as a
1539 source and some other stack register (possibly top of stack)
1540 as a destination. */
1542 src1 = get_true_reg (&XEXP (pat_src, 0));
1543 src2 = get_true_reg (&XEXP (pat_src, 1));
1545 /* We will fix any death note later. */
1547 if (STACK_REG_P (*src1))
1548 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1549 else
1550 src1_note = NULL_RTX;
1551 if (STACK_REG_P (*src2))
1552 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1553 else
1554 src2_note = NULL_RTX;
1556 /* If either operand is not a stack register, then the dest
1557 must be top of stack. */
1559 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
1560 emit_swap_insn (insn, regstack, *dest);
1561 else
1563 /* Both operands are REG. If neither operand is already
1564 at the top of stack, choose to make the one that is the
1565 dest the new top of stack. */
1567 int src1_hard_regnum, src2_hard_regnum;
1569 src1_hard_regnum = get_hard_regnum (regstack, *src1);
1570 src2_hard_regnum = get_hard_regnum (regstack, *src2);
1572 /* If the source is not live, this is yet another case of
1573 uninitialized variables. Load up a NaN instead. */
1574 if (src1_hard_regnum == -1)
1576 rtx pat2 = gen_rtx_CLOBBER (VOIDmode, *src1);
1577 rtx insn2 = emit_insn_before (pat2, insn);
1578 control_flow_insn_deleted
1579 |= move_nan_for_stack_reg (insn2, regstack, *src1);
1581 if (src2_hard_regnum == -1)
1583 rtx pat2 = gen_rtx_CLOBBER (VOIDmode, *src2);
1584 rtx insn2 = emit_insn_before (pat2, insn);
1585 control_flow_insn_deleted
1586 |= move_nan_for_stack_reg (insn2, regstack, *src2);
1589 if (src1_hard_regnum != FIRST_STACK_REG
1590 && src2_hard_regnum != FIRST_STACK_REG)
1591 emit_swap_insn (insn, regstack, *dest);
1594 if (STACK_REG_P (*src1))
1595 replace_reg (src1, get_hard_regnum (regstack, *src1));
1596 if (STACK_REG_P (*src2))
1597 replace_reg (src2, get_hard_regnum (regstack, *src2));
1599 if (src1_note)
1601 rtx src1_reg = XEXP (src1_note, 0);
1603 /* If the register that dies is at the top of stack, then
1604 the destination is somewhere else - merely substitute it.
1605 But if the reg that dies is not at top of stack, then
1606 move the top of stack to the dead reg, as though we had
1607 done the insn and then a store-with-pop. */
1609 if (REGNO (src1_reg) == regstack->reg[regstack->top])
1611 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1612 replace_reg (dest, get_hard_regnum (regstack, *dest));
1614 else
1616 int regno = get_hard_regnum (regstack, src1_reg);
1618 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1619 replace_reg (dest, regno);
1621 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1622 = regstack->reg[regstack->top];
1625 CLEAR_HARD_REG_BIT (regstack->reg_set,
1626 REGNO (XEXP (src1_note, 0)));
1627 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1628 regstack->top--;
1630 else if (src2_note)
1632 rtx src2_reg = XEXP (src2_note, 0);
1633 if (REGNO (src2_reg) == regstack->reg[regstack->top])
1635 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1636 replace_reg (dest, get_hard_regnum (regstack, *dest));
1638 else
1640 int regno = get_hard_regnum (regstack, src2_reg);
1642 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1643 replace_reg (dest, regno);
1645 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1646 = regstack->reg[regstack->top];
1649 CLEAR_HARD_REG_BIT (regstack->reg_set,
1650 REGNO (XEXP (src2_note, 0)));
1651 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
1652 regstack->top--;
1654 else
1656 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1657 replace_reg (dest, get_hard_regnum (regstack, *dest));
1660 /* Keep operand 1 matching with destination. */
1661 if (COMMUTATIVE_ARITH_P (pat_src)
1662 && REG_P (*src1) && REG_P (*src2)
1663 && REGNO (*src1) != REGNO (*dest))
1665 int tmp = REGNO (*src1);
1666 replace_reg (src1, REGNO (*src2));
1667 replace_reg (src2, tmp);
1669 break;
1671 case UNSPEC:
1672 switch (XINT (pat_src, 1))
1674 case UNSPEC_STA:
1675 case UNSPEC_FIST:
1677 case UNSPEC_FIST_FLOOR:
1678 case UNSPEC_FIST_CEIL:
1680 /* These insns only operate on the top of the stack. */
1682 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1683 emit_swap_insn (insn, regstack, *src1);
1685 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1687 if (STACK_REG_P (*dest))
1688 replace_reg (dest, FIRST_STACK_REG);
1690 if (src1_note)
1692 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1693 regstack->top--;
1694 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1697 replace_reg (src1, FIRST_STACK_REG);
1698 break;
1700 case UNSPEC_FXAM:
1702 /* This insn only operate on the top of the stack. */
1704 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1705 emit_swap_insn (insn, regstack, *src1);
1707 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1709 replace_reg (src1, FIRST_STACK_REG);
1711 if (src1_note)
1713 remove_regno_note (insn, REG_DEAD,
1714 REGNO (XEXP (src1_note, 0)));
1715 emit_pop_insn (insn, regstack, XEXP (src1_note, 0),
1716 EMIT_AFTER);
1719 break;
1721 case UNSPEC_SIN:
1722 case UNSPEC_COS:
1723 case UNSPEC_FRNDINT:
1724 case UNSPEC_F2XM1:
1726 case UNSPEC_FRNDINT_FLOOR:
1727 case UNSPEC_FRNDINT_CEIL:
1728 case UNSPEC_FRNDINT_TRUNC:
1729 case UNSPEC_FRNDINT_MASK_PM:
1731 /* Above insns operate on the top of the stack. */
1733 case UNSPEC_SINCOS_COS:
1734 case UNSPEC_XTRACT_FRACT:
1736 /* Above 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 replaced with output. */
1744 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1745 gcc_assert (!src1_note);
1747 if (STACK_REG_P (*dest))
1748 replace_reg (dest, FIRST_STACK_REG);
1750 replace_reg (src1, FIRST_STACK_REG);
1751 break;
1753 case UNSPEC_SINCOS_SIN:
1754 case UNSPEC_XTRACT_EXP:
1756 /* These insns operate on the top two stack slots,
1757 second part of one input, double output insn. */
1759 regstack->top++;
1760 /* FALLTHRU */
1762 case UNSPEC_TAN:
1764 /* For UNSPEC_TAN, regstack->top is already increased
1765 by inherent load of constant 1.0. */
1767 /* Output value is generated in the second stack slot.
1768 Move current value from second slot to the top. */
1769 regstack->reg[regstack->top]
1770 = regstack->reg[regstack->top - 1];
1772 gcc_assert (STACK_REG_P (*dest));
1774 regstack->reg[regstack->top - 1] = REGNO (*dest);
1775 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1776 replace_reg (dest, FIRST_STACK_REG + 1);
1778 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1780 replace_reg (src1, FIRST_STACK_REG);
1781 break;
1783 case UNSPEC_FPATAN:
1784 case UNSPEC_FYL2X:
1785 case UNSPEC_FYL2XP1:
1786 /* These insns operate on the top two stack slots. */
1788 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1789 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1791 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1792 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1794 swap_to_top (insn, regstack, *src1, *src2);
1796 replace_reg (src1, FIRST_STACK_REG);
1797 replace_reg (src2, FIRST_STACK_REG + 1);
1799 if (src1_note)
1800 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1801 if (src2_note)
1802 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1804 /* Pop both input operands from the stack. */
1805 CLEAR_HARD_REG_BIT (regstack->reg_set,
1806 regstack->reg[regstack->top]);
1807 CLEAR_HARD_REG_BIT (regstack->reg_set,
1808 regstack->reg[regstack->top - 1]);
1809 regstack->top -= 2;
1811 /* Push the result back onto the stack. */
1812 regstack->reg[++regstack->top] = REGNO (*dest);
1813 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1814 replace_reg (dest, FIRST_STACK_REG);
1815 break;
1817 case UNSPEC_FSCALE_FRACT:
1818 case UNSPEC_FPREM_F:
1819 case UNSPEC_FPREM1_F:
1820 /* These insns operate on the top two stack slots,
1821 first part of double input, double output insn. */
1823 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1824 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1826 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1827 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1829 /* Inputs should never die, they are
1830 replaced with outputs. */
1831 gcc_assert (!src1_note);
1832 gcc_assert (!src2_note);
1834 swap_to_top (insn, regstack, *src1, *src2);
1836 /* Push the result back onto stack. Empty stack slot
1837 will be filled in second part of insn. */
1838 if (STACK_REG_P (*dest))
1840 regstack->reg[regstack->top] = REGNO (*dest);
1841 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1842 replace_reg (dest, FIRST_STACK_REG);
1845 replace_reg (src1, FIRST_STACK_REG);
1846 replace_reg (src2, FIRST_STACK_REG + 1);
1847 break;
1849 case UNSPEC_FSCALE_EXP:
1850 case UNSPEC_FPREM_U:
1851 case UNSPEC_FPREM1_U:
1852 /* These insns operate on the top two stack slots,
1853 second part of double input, double output insn. */
1855 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1856 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1858 /* Push the result back onto stack. Fill empty slot from
1859 first part of insn and fix top of stack pointer. */
1860 if (STACK_REG_P (*dest))
1862 regstack->reg[regstack->top - 1] = REGNO (*dest);
1863 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1864 replace_reg (dest, FIRST_STACK_REG + 1);
1867 replace_reg (src1, FIRST_STACK_REG);
1868 replace_reg (src2, FIRST_STACK_REG + 1);
1869 break;
1871 case UNSPEC_C2_FLAG:
1872 /* This insn operates on the top two stack slots,
1873 third part of C2 setting double input insn. */
1875 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1876 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1878 replace_reg (src1, FIRST_STACK_REG);
1879 replace_reg (src2, FIRST_STACK_REG + 1);
1880 break;
1882 case UNSPEC_SAHF:
1883 /* (unspec [(unspec [(compare)] UNSPEC_FNSTSW)] UNSPEC_SAHF)
1884 The combination matches the PPRO fcomi instruction. */
1886 pat_src = XVECEXP (pat_src, 0, 0);
1887 gcc_assert (GET_CODE (pat_src) == UNSPEC);
1888 gcc_assert (XINT (pat_src, 1) == UNSPEC_FNSTSW);
1889 /* Fall through. */
1891 case UNSPEC_FNSTSW:
1892 /* Combined fcomp+fnstsw generated for doing well with
1893 CSE. When optimizing this would have been broken
1894 up before now. */
1896 pat_src = XVECEXP (pat_src, 0, 0);
1897 gcc_assert (GET_CODE (pat_src) == COMPARE);
1899 compare_for_stack_reg (insn, regstack, pat_src);
1900 break;
1902 default:
1903 gcc_unreachable ();
1905 break;
1907 case IF_THEN_ELSE:
1908 /* This insn requires the top of stack to be the destination. */
1910 src1 = get_true_reg (&XEXP (pat_src, 1));
1911 src2 = get_true_reg (&XEXP (pat_src, 2));
1913 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1914 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1916 /* If the comparison operator is an FP comparison operator,
1917 it is handled correctly by compare_for_stack_reg () who
1918 will move the destination to the top of stack. But if the
1919 comparison operator is not an FP comparison operator, we
1920 have to handle it here. */
1921 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
1922 && REGNO (*dest) != regstack->reg[regstack->top])
1924 /* In case one of operands is the top of stack and the operands
1925 dies, it is safe to make it the destination operand by
1926 reversing the direction of cmove and avoid fxch. */
1927 if ((REGNO (*src1) == regstack->reg[regstack->top]
1928 && src1_note)
1929 || (REGNO (*src2) == regstack->reg[regstack->top]
1930 && src2_note))
1932 int idx1 = (get_hard_regnum (regstack, *src1)
1933 - FIRST_STACK_REG);
1934 int idx2 = (get_hard_regnum (regstack, *src2)
1935 - FIRST_STACK_REG);
1937 /* Make reg-stack believe that the operands are already
1938 swapped on the stack */
1939 regstack->reg[regstack->top - idx1] = REGNO (*src2);
1940 regstack->reg[regstack->top - idx2] = REGNO (*src1);
1942 /* Reverse condition to compensate the operand swap.
1943 i386 do have comparison always reversible. */
1944 PUT_CODE (XEXP (pat_src, 0),
1945 reversed_comparison_code (XEXP (pat_src, 0), insn));
1947 else
1948 emit_swap_insn (insn, regstack, *dest);
1952 rtx src_note [3];
1953 int i;
1955 src_note[0] = 0;
1956 src_note[1] = src1_note;
1957 src_note[2] = src2_note;
1959 if (STACK_REG_P (*src1))
1960 replace_reg (src1, get_hard_regnum (regstack, *src1));
1961 if (STACK_REG_P (*src2))
1962 replace_reg (src2, get_hard_regnum (regstack, *src2));
1964 for (i = 1; i <= 2; i++)
1965 if (src_note [i])
1967 int regno = REGNO (XEXP (src_note[i], 0));
1969 /* If the register that dies is not at the top of
1970 stack, then move the top of stack to the dead reg.
1971 Top of stack should never die, as it is the
1972 destination. */
1973 gcc_assert (regno != regstack->reg[regstack->top]);
1974 remove_regno_note (insn, REG_DEAD, regno);
1975 emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
1976 EMIT_AFTER);
1980 /* Make dest the top of stack. Add dest to regstack if
1981 not present. */
1982 if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
1983 regstack->reg[++regstack->top] = REGNO (*dest);
1984 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1985 replace_reg (dest, FIRST_STACK_REG);
1986 break;
1988 default:
1989 gcc_unreachable ();
1991 break;
1994 default:
1995 break;
1998 return control_flow_insn_deleted;
2001 /* Substitute hard regnums for any stack regs in INSN, which has
2002 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
2003 before the insn, and is updated with changes made here.
2005 There are several requirements and assumptions about the use of
2006 stack-like regs in asm statements. These rules are enforced by
2007 record_asm_stack_regs; see comments there for details. Any
2008 asm_operands left in the RTL at this point may be assume to meet the
2009 requirements, since record_asm_stack_regs removes any problem asm. */
2011 static void
2012 subst_asm_stack_regs (rtx insn, stack_ptr regstack)
2014 rtx body = PATTERN (insn);
2015 int alt;
2017 rtx *note_reg; /* Array of note contents */
2018 rtx **note_loc; /* Address of REG field of each note */
2019 enum reg_note *note_kind; /* The type of each note */
2021 rtx *clobber_reg = 0;
2022 rtx **clobber_loc = 0;
2024 struct stack_def temp_stack;
2025 int n_notes;
2026 int n_clobbers;
2027 rtx note;
2028 int i;
2029 int n_inputs, n_outputs;
2031 if (! check_asm_stack_operands (insn))
2032 return;
2034 /* Find out what the constraints required. If no constraint
2035 alternative matches, that is a compiler bug: we should have caught
2036 such an insn in check_asm_stack_operands. */
2037 extract_insn (insn);
2038 constrain_operands (1);
2039 alt = which_alternative;
2041 preprocess_constraints ();
2043 get_asm_operands_in_out (body, &n_outputs, &n_inputs);
2045 gcc_assert (alt >= 0);
2047 /* Strip SUBREGs here to make the following code simpler. */
2048 for (i = 0; i < recog_data.n_operands; i++)
2049 if (GET_CODE (recog_data.operand[i]) == SUBREG
2050 && REG_P (SUBREG_REG (recog_data.operand[i])))
2052 recog_data.operand_loc[i] = & SUBREG_REG (recog_data.operand[i]);
2053 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
2056 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
2058 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
2059 i++;
2061 note_reg = XALLOCAVEC (rtx, i);
2062 note_loc = XALLOCAVEC (rtx *, i);
2063 note_kind = XALLOCAVEC (enum reg_note, i);
2065 n_notes = 0;
2066 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2068 rtx reg = XEXP (note, 0);
2069 rtx *loc = & XEXP (note, 0);
2071 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
2073 loc = & SUBREG_REG (reg);
2074 reg = SUBREG_REG (reg);
2077 if (STACK_REG_P (reg)
2078 && (REG_NOTE_KIND (note) == REG_DEAD
2079 || REG_NOTE_KIND (note) == REG_UNUSED))
2081 note_reg[n_notes] = reg;
2082 note_loc[n_notes] = loc;
2083 note_kind[n_notes] = REG_NOTE_KIND (note);
2084 n_notes++;
2088 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2090 n_clobbers = 0;
2092 if (GET_CODE (body) == PARALLEL)
2094 clobber_reg = XALLOCAVEC (rtx, XVECLEN (body, 0));
2095 clobber_loc = XALLOCAVEC (rtx *, XVECLEN (body, 0));
2097 for (i = 0; i < XVECLEN (body, 0); i++)
2098 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
2100 rtx clobber = XVECEXP (body, 0, i);
2101 rtx reg = XEXP (clobber, 0);
2102 rtx *loc = & XEXP (clobber, 0);
2104 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
2106 loc = & SUBREG_REG (reg);
2107 reg = SUBREG_REG (reg);
2110 if (STACK_REG_P (reg))
2112 clobber_reg[n_clobbers] = reg;
2113 clobber_loc[n_clobbers] = loc;
2114 n_clobbers++;
2119 temp_stack = *regstack;
2121 /* Put the input regs into the desired place in TEMP_STACK. */
2123 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2124 if (STACK_REG_P (recog_data.operand[i])
2125 && reg_class_subset_p (recog_op_alt[i][alt].cl,
2126 FLOAT_REGS)
2127 && recog_op_alt[i][alt].cl != FLOAT_REGS)
2129 /* If an operand needs to be in a particular reg in
2130 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2131 these constraints are for single register classes, and
2132 reload guaranteed that operand[i] is already in that class,
2133 we can just use REGNO (recog_data.operand[i]) to know which
2134 actual reg this operand needs to be in. */
2136 int regno = get_hard_regnum (&temp_stack, recog_data.operand[i]);
2138 gcc_assert (regno >= 0);
2140 if ((unsigned int) regno != REGNO (recog_data.operand[i]))
2142 /* recog_data.operand[i] is not in the right place. Find
2143 it and swap it with whatever is already in I's place.
2144 K is where recog_data.operand[i] is now. J is where it
2145 should be. */
2146 int j, k, temp;
2148 k = temp_stack.top - (regno - FIRST_STACK_REG);
2149 j = (temp_stack.top
2150 - (REGNO (recog_data.operand[i]) - FIRST_STACK_REG));
2152 temp = temp_stack.reg[k];
2153 temp_stack.reg[k] = temp_stack.reg[j];
2154 temp_stack.reg[j] = temp;
2158 /* Emit insns before INSN to make sure the reg-stack is in the right
2159 order. */
2161 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
2163 /* Make the needed input register substitutions. Do death notes and
2164 clobbers too, because these are for inputs, not outputs. */
2166 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2167 if (STACK_REG_P (recog_data.operand[i]))
2169 int regnum = get_hard_regnum (regstack, recog_data.operand[i]);
2171 gcc_assert (regnum >= 0);
2173 replace_reg (recog_data.operand_loc[i], regnum);
2176 for (i = 0; i < n_notes; i++)
2177 if (note_kind[i] == REG_DEAD)
2179 int regnum = get_hard_regnum (regstack, note_reg[i]);
2181 gcc_assert (regnum >= 0);
2183 replace_reg (note_loc[i], regnum);
2186 for (i = 0; i < n_clobbers; i++)
2188 /* It's OK for a CLOBBER to reference a reg that is not live.
2189 Don't try to replace it in that case. */
2190 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2192 if (regnum >= 0)
2194 /* Sigh - clobbers always have QImode. But replace_reg knows
2195 that these regs can't be MODE_INT and will assert. Just put
2196 the right reg there without calling replace_reg. */
2198 *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
2202 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2204 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2205 if (STACK_REG_P (recog_data.operand[i]))
2207 /* An input reg is implicitly popped if it is tied to an
2208 output, or if there is a CLOBBER for it. */
2209 int j;
2211 for (j = 0; j < n_clobbers; j++)
2212 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
2213 break;
2215 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
2217 /* recog_data.operand[i] might not be at the top of stack.
2218 But that's OK, because all we need to do is pop the
2219 right number of regs off of the top of the reg-stack.
2220 record_asm_stack_regs guaranteed that all implicitly
2221 popped regs were grouped at the top of the reg-stack. */
2223 CLEAR_HARD_REG_BIT (regstack->reg_set,
2224 regstack->reg[regstack->top]);
2225 regstack->top--;
2229 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2230 Note that there isn't any need to substitute register numbers.
2231 ??? Explain why this is true. */
2233 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2235 /* See if there is an output for this hard reg. */
2236 int j;
2238 for (j = 0; j < n_outputs; j++)
2239 if (STACK_REG_P (recog_data.operand[j])
2240 && REGNO (recog_data.operand[j]) == (unsigned) i)
2242 regstack->reg[++regstack->top] = i;
2243 SET_HARD_REG_BIT (regstack->reg_set, i);
2244 break;
2248 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2249 input that the asm didn't implicitly pop. If the asm didn't
2250 implicitly pop an input reg, that reg will still be live.
2252 Note that we can't use find_regno_note here: the register numbers
2253 in the death notes have already been substituted. */
2255 for (i = 0; i < n_outputs; i++)
2256 if (STACK_REG_P (recog_data.operand[i]))
2258 int j;
2260 for (j = 0; j < n_notes; j++)
2261 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2262 && note_kind[j] == REG_UNUSED)
2264 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2265 EMIT_AFTER);
2266 break;
2270 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2271 if (STACK_REG_P (recog_data.operand[i]))
2273 int j;
2275 for (j = 0; j < n_notes; j++)
2276 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2277 && note_kind[j] == REG_DEAD
2278 && TEST_HARD_REG_BIT (regstack->reg_set,
2279 REGNO (recog_data.operand[i])))
2281 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2282 EMIT_AFTER);
2283 break;
2288 /* Substitute stack hard reg numbers for stack virtual registers in
2289 INSN. Non-stack register numbers are not changed. REGSTACK is the
2290 current stack content. Insns may be emitted as needed to arrange the
2291 stack for the 387 based on the contents of the insn. Return whether
2292 a control flow insn was deleted in the process. */
2294 static bool
2295 subst_stack_regs (rtx insn, stack_ptr regstack)
2297 rtx *note_link, note;
2298 bool control_flow_insn_deleted = false;
2299 int i;
2301 if (CALL_P (insn))
2303 int top = regstack->top;
2305 /* If there are any floating point parameters to be passed in
2306 registers for this call, make sure they are in the right
2307 order. */
2309 if (top >= 0)
2311 straighten_stack (insn, regstack);
2313 /* Now mark the arguments as dead after the call. */
2315 while (regstack->top >= 0)
2317 CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
2318 regstack->top--;
2323 /* Do the actual substitution if any stack regs are mentioned.
2324 Since we only record whether entire insn mentions stack regs, and
2325 subst_stack_regs_pat only works for patterns that contain stack regs,
2326 we must check each pattern in a parallel here. A call_value_pop could
2327 fail otherwise. */
2329 if (stack_regs_mentioned (insn))
2331 int n_operands = asm_noperands (PATTERN (insn));
2332 if (n_operands >= 0)
2334 /* This insn is an `asm' with operands. Decode the operands,
2335 decide how many are inputs, and do register substitution.
2336 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2338 subst_asm_stack_regs (insn, regstack);
2339 return control_flow_insn_deleted;
2342 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2343 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2345 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2347 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
2348 XVECEXP (PATTERN (insn), 0, i)
2349 = shallow_copy_rtx (XVECEXP (PATTERN (insn), 0, i));
2350 control_flow_insn_deleted
2351 |= subst_stack_regs_pat (insn, regstack,
2352 XVECEXP (PATTERN (insn), 0, i));
2355 else
2356 control_flow_insn_deleted
2357 |= subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2360 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2361 REG_UNUSED will already have been dealt with, so just return. */
2363 if (NOTE_P (insn) || INSN_DELETED_P (insn))
2364 return control_flow_insn_deleted;
2366 /* If this a noreturn call, we can't insert pop insns after it.
2367 Instead, reset the stack state to empty. */
2368 if (CALL_P (insn)
2369 && find_reg_note (insn, REG_NORETURN, NULL))
2371 regstack->top = -1;
2372 CLEAR_HARD_REG_SET (regstack->reg_set);
2373 return control_flow_insn_deleted;
2376 /* If there is a REG_UNUSED note on a stack register on this insn,
2377 the indicated reg must be popped. The REG_UNUSED note is removed,
2378 since the form of the newly emitted pop insn references the reg,
2379 making it no longer `unset'. */
2381 note_link = &REG_NOTES (insn);
2382 for (note = *note_link; note; note = XEXP (note, 1))
2383 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2385 *note_link = XEXP (note, 1);
2386 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), EMIT_AFTER);
2388 else
2389 note_link = &XEXP (note, 1);
2391 return control_flow_insn_deleted;
2394 /* Change the organization of the stack so that it fits a new basic
2395 block. Some registers might have to be popped, but there can never be
2396 a register live in the new block that is not now live.
2398 Insert any needed insns before or after INSN, as indicated by
2399 WHERE. OLD is the original stack layout, and NEW is the desired
2400 form. OLD is updated to reflect the code emitted, i.e., it will be
2401 the same as NEW upon return.
2403 This function will not preserve block_end[]. But that information
2404 is no longer needed once this has executed. */
2406 static void
2407 change_stack (rtx insn, stack_ptr old, stack_ptr new_stack, enum emit_where where)
2409 int reg;
2410 int update_end = 0;
2411 int i;
2413 /* Stack adjustments for the first insn in a block update the
2414 current_block's stack_in instead of inserting insns directly.
2415 compensate_edges will add the necessary code later. */
2416 if (current_block
2417 && starting_stack_p
2418 && where == EMIT_BEFORE)
2420 BLOCK_INFO (current_block)->stack_in = *new_stack;
2421 starting_stack_p = false;
2422 *old = *new_stack;
2423 return;
2426 /* We will be inserting new insns "backwards". If we are to insert
2427 after INSN, find the next insn, and insert before it. */
2429 if (where == EMIT_AFTER)
2431 if (current_block && BB_END (current_block) == insn)
2432 update_end = 1;
2433 insn = NEXT_INSN (insn);
2436 /* Initialize partially dead variables. */
2437 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
2438 if (TEST_HARD_REG_BIT (new_stack->reg_set, i)
2439 && !TEST_HARD_REG_BIT (old->reg_set, i))
2441 old->reg[++old->top] = i;
2442 SET_HARD_REG_BIT (old->reg_set, i);
2443 emit_insn_before (gen_rtx_SET (VOIDmode,
2444 FP_MODE_REG (i, SFmode), not_a_num), insn);
2447 /* Pop any registers that are not needed in the new block. */
2449 /* If the destination block's stack already has a specified layout
2450 and contains two or more registers, use a more intelligent algorithm
2451 to pop registers that minimizes the number number of fxchs below. */
2452 if (new_stack->top > 0)
2454 bool slots[REG_STACK_SIZE];
2455 int pops[REG_STACK_SIZE];
2456 int next, dest, topsrc;
2458 /* First pass to determine the free slots. */
2459 for (reg = 0; reg <= new_stack->top; reg++)
2460 slots[reg] = TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[reg]);
2462 /* Second pass to allocate preferred slots. */
2463 topsrc = -1;
2464 for (reg = old->top; reg > new_stack->top; reg--)
2465 if (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[reg]))
2467 dest = -1;
2468 for (next = 0; next <= new_stack->top; next++)
2469 if (!slots[next] && new_stack->reg[next] == old->reg[reg])
2471 /* If this is a preference for the new top of stack, record
2472 the fact by remembering it's old->reg in topsrc. */
2473 if (next == new_stack->top)
2474 topsrc = reg;
2475 slots[next] = true;
2476 dest = next;
2477 break;
2479 pops[reg] = dest;
2481 else
2482 pops[reg] = reg;
2484 /* Intentionally, avoid placing the top of stack in it's correct
2485 location, if we still need to permute the stack below and we
2486 can usefully place it somewhere else. This is the case if any
2487 slot is still unallocated, in which case we should place the
2488 top of stack there. */
2489 if (topsrc != -1)
2490 for (reg = 0; reg < new_stack->top; reg++)
2491 if (!slots[reg])
2493 pops[topsrc] = reg;
2494 slots[new_stack->top] = false;
2495 slots[reg] = true;
2496 break;
2499 /* Third pass allocates remaining slots and emits pop insns. */
2500 next = new_stack->top;
2501 for (reg = old->top; reg > new_stack->top; reg--)
2503 dest = pops[reg];
2504 if (dest == -1)
2506 /* Find next free slot. */
2507 while (slots[next])
2508 next--;
2509 dest = next--;
2511 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[dest], DFmode),
2512 EMIT_BEFORE);
2515 else
2517 /* The following loop attempts to maximize the number of times we
2518 pop the top of the stack, as this permits the use of the faster
2519 ffreep instruction on platforms that support it. */
2520 int live, next;
2522 live = 0;
2523 for (reg = 0; reg <= old->top; reg++)
2524 if (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[reg]))
2525 live++;
2527 next = live;
2528 while (old->top >= live)
2529 if (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[old->top]))
2531 while (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[next]))
2532 next--;
2533 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[next], DFmode),
2534 EMIT_BEFORE);
2536 else
2537 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[old->top], DFmode),
2538 EMIT_BEFORE);
2541 if (new_stack->top == -2)
2543 /* If the new block has never been processed, then it can inherit
2544 the old stack order. */
2546 new_stack->top = old->top;
2547 memcpy (new_stack->reg, old->reg, sizeof (new_stack->reg));
2549 else
2551 /* This block has been entered before, and we must match the
2552 previously selected stack order. */
2554 /* By now, the only difference should be the order of the stack,
2555 not their depth or liveliness. */
2557 gcc_assert (hard_reg_set_equal_p (old->reg_set, new_stack->reg_set));
2558 gcc_assert (old->top == new_stack->top);
2560 /* If the stack is not empty (new_stack->top != -1), loop here emitting
2561 swaps until the stack is correct.
2563 The worst case number of swaps emitted is N + 2, where N is the
2564 depth of the stack. In some cases, the reg at the top of
2565 stack may be correct, but swapped anyway in order to fix
2566 other regs. But since we never swap any other reg away from
2567 its correct slot, this algorithm will converge. */
2569 if (new_stack->top != -1)
2572 /* Swap the reg at top of stack into the position it is
2573 supposed to be in, until the correct top of stack appears. */
2575 while (old->reg[old->top] != new_stack->reg[new_stack->top])
2577 for (reg = new_stack->top; reg >= 0; reg--)
2578 if (new_stack->reg[reg] == old->reg[old->top])
2579 break;
2581 gcc_assert (reg != -1);
2583 emit_swap_insn (insn, old,
2584 FP_MODE_REG (old->reg[reg], DFmode));
2587 /* See if any regs remain incorrect. If so, bring an
2588 incorrect reg to the top of stack, and let the while loop
2589 above fix it. */
2591 for (reg = new_stack->top; reg >= 0; reg--)
2592 if (new_stack->reg[reg] != old->reg[reg])
2594 emit_swap_insn (insn, old,
2595 FP_MODE_REG (old->reg[reg], DFmode));
2596 break;
2598 } while (reg >= 0);
2600 /* At this point there must be no differences. */
2602 for (reg = old->top; reg >= 0; reg--)
2603 gcc_assert (old->reg[reg] == new_stack->reg[reg]);
2606 if (update_end)
2607 BB_END (current_block) = PREV_INSN (insn);
2610 /* Print stack configuration. */
2612 static void
2613 print_stack (FILE *file, stack_ptr s)
2615 if (! file)
2616 return;
2618 if (s->top == -2)
2619 fprintf (file, "uninitialized\n");
2620 else if (s->top == -1)
2621 fprintf (file, "empty\n");
2622 else
2624 int i;
2625 fputs ("[ ", file);
2626 for (i = 0; i <= s->top; ++i)
2627 fprintf (file, "%d ", s->reg[i]);
2628 fputs ("]\n", file);
2632 /* This function was doing life analysis. We now let the regular live
2633 code do it's job, so we only need to check some extra invariants
2634 that reg-stack expects. Primary among these being that all registers
2635 are initialized before use.
2637 The function returns true when code was emitted to CFG edges and
2638 commit_edge_insertions needs to be called. */
2640 static int
2641 convert_regs_entry (void)
2643 int inserted = 0;
2644 edge e;
2645 edge_iterator ei;
2647 /* Load something into each stack register live at function entry.
2648 Such live registers can be caused by uninitialized variables or
2649 functions not returning values on all paths. In order to keep
2650 the push/pop code happy, and to not scrog the register stack, we
2651 must put something in these registers. Use a QNaN.
2653 Note that we are inserting converted code here. This code is
2654 never seen by the convert_regs pass. */
2656 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2658 basic_block block = e->dest;
2659 block_info bi = BLOCK_INFO (block);
2660 int reg, top = -1;
2662 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2663 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2665 rtx init;
2667 bi->stack_in.reg[++top] = reg;
2669 init = gen_rtx_SET (VOIDmode,
2670 FP_MODE_REG (FIRST_STACK_REG, SFmode),
2671 not_a_num);
2672 insert_insn_on_edge (init, e);
2673 inserted = 1;
2676 bi->stack_in.top = top;
2679 return inserted;
2682 /* Construct the desired stack for function exit. This will either
2683 be `empty', or the function return value at top-of-stack. */
2685 static void
2686 convert_regs_exit (void)
2688 int value_reg_low, value_reg_high;
2689 stack_ptr output_stack;
2690 rtx retvalue;
2692 retvalue = stack_result (current_function_decl);
2693 value_reg_low = value_reg_high = -1;
2694 if (retvalue)
2696 value_reg_low = REGNO (retvalue);
2697 value_reg_high = END_HARD_REGNO (retvalue) - 1;
2700 output_stack = &BLOCK_INFO (EXIT_BLOCK_PTR)->stack_in;
2701 if (value_reg_low == -1)
2702 output_stack->top = -1;
2703 else
2705 int reg;
2707 output_stack->top = value_reg_high - value_reg_low;
2708 for (reg = value_reg_low; reg <= value_reg_high; ++reg)
2710 output_stack->reg[value_reg_high - reg] = reg;
2711 SET_HARD_REG_BIT (output_stack->reg_set, reg);
2716 /* Copy the stack info from the end of edge E's source block to the
2717 start of E's destination block. */
2719 static void
2720 propagate_stack (edge e)
2722 stack_ptr src_stack = &BLOCK_INFO (e->src)->stack_out;
2723 stack_ptr dest_stack = &BLOCK_INFO (e->dest)->stack_in;
2724 int reg;
2726 /* Preserve the order of the original stack, but check whether
2727 any pops are needed. */
2728 dest_stack->top = -1;
2729 for (reg = 0; reg <= src_stack->top; ++reg)
2730 if (TEST_HARD_REG_BIT (dest_stack->reg_set, src_stack->reg[reg]))
2731 dest_stack->reg[++dest_stack->top] = src_stack->reg[reg];
2733 /* Push in any partially dead values. */
2734 for (reg = FIRST_STACK_REG; reg < LAST_STACK_REG + 1; reg++)
2735 if (TEST_HARD_REG_BIT (dest_stack->reg_set, reg)
2736 && !TEST_HARD_REG_BIT (src_stack->reg_set, reg))
2737 dest_stack->reg[++dest_stack->top] = reg;
2741 /* Adjust the stack of edge E's source block on exit to match the stack
2742 of it's target block upon input. The stack layouts of both blocks
2743 should have been defined by now. */
2745 static bool
2746 compensate_edge (edge e)
2748 basic_block source = e->src, target = e->dest;
2749 stack_ptr target_stack = &BLOCK_INFO (target)->stack_in;
2750 stack_ptr source_stack = &BLOCK_INFO (source)->stack_out;
2751 struct stack_def regstack;
2752 int reg;
2754 if (dump_file)
2755 fprintf (dump_file, "Edge %d->%d: ", source->index, target->index);
2757 gcc_assert (target_stack->top != -2);
2759 /* Check whether stacks are identical. */
2760 if (target_stack->top == source_stack->top)
2762 for (reg = target_stack->top; reg >= 0; --reg)
2763 if (target_stack->reg[reg] != source_stack->reg[reg])
2764 break;
2766 if (reg == -1)
2768 if (dump_file)
2769 fprintf (dump_file, "no changes needed\n");
2770 return false;
2774 if (dump_file)
2776 fprintf (dump_file, "correcting stack to ");
2777 print_stack (dump_file, target_stack);
2780 /* Abnormal calls may appear to have values live in st(0), but the
2781 abnormal return path will not have actually loaded the values. */
2782 if (e->flags & EDGE_ABNORMAL_CALL)
2784 /* Assert that the lifetimes are as we expect -- one value
2785 live at st(0) on the end of the source block, and no
2786 values live at the beginning of the destination block.
2787 For complex return values, we may have st(1) live as well. */
2788 gcc_assert (source_stack->top == 0 || source_stack->top == 1);
2789 gcc_assert (target_stack->top == -1);
2790 return false;
2793 /* Handle non-call EH edges specially. The normal return path have
2794 values in registers. These will be popped en masse by the unwind
2795 library. */
2796 if (e->flags & EDGE_EH)
2798 gcc_assert (target_stack->top == -1);
2799 return false;
2802 /* We don't support abnormal edges. Global takes care to
2803 avoid any live register across them, so we should never
2804 have to insert instructions on such edges. */
2805 gcc_assert (! (e->flags & EDGE_ABNORMAL));
2807 /* Make a copy of source_stack as change_stack is destructive. */
2808 regstack = *source_stack;
2810 /* It is better to output directly to the end of the block
2811 instead of to the edge, because emit_swap can do minimal
2812 insn scheduling. We can do this when there is only one
2813 edge out, and it is not abnormal. */
2814 if (EDGE_COUNT (source->succs) == 1)
2816 current_block = source;
2817 change_stack (BB_END (source), &regstack, target_stack,
2818 (JUMP_P (BB_END (source)) ? EMIT_BEFORE : EMIT_AFTER));
2820 else
2822 rtx seq, after;
2824 current_block = NULL;
2825 start_sequence ();
2827 /* ??? change_stack needs some point to emit insns after. */
2828 after = emit_note (NOTE_INSN_DELETED);
2830 change_stack (after, &regstack, target_stack, EMIT_BEFORE);
2832 seq = get_insns ();
2833 end_sequence ();
2835 insert_insn_on_edge (seq, e);
2836 return true;
2838 return false;
2841 /* Traverse all non-entry edges in the CFG, and emit the necessary
2842 edge compensation code to change the stack from stack_out of the
2843 source block to the stack_in of the destination block. */
2845 static bool
2846 compensate_edges (void)
2848 bool inserted = false;
2849 basic_block bb;
2851 starting_stack_p = false;
2853 FOR_EACH_BB (bb)
2854 if (bb != ENTRY_BLOCK_PTR)
2856 edge e;
2857 edge_iterator ei;
2859 FOR_EACH_EDGE (e, ei, bb->succs)
2860 inserted |= compensate_edge (e);
2862 return inserted;
2865 /* Select the better of two edges E1 and E2 to use to determine the
2866 stack layout for their shared destination basic block. This is
2867 typically the more frequently executed. The edge E1 may be NULL
2868 (in which case E2 is returned), but E2 is always non-NULL. */
2870 static edge
2871 better_edge (edge e1, edge e2)
2873 if (!e1)
2874 return e2;
2876 if (EDGE_FREQUENCY (e1) > EDGE_FREQUENCY (e2))
2877 return e1;
2878 if (EDGE_FREQUENCY (e1) < EDGE_FREQUENCY (e2))
2879 return e2;
2881 if (e1->count > e2->count)
2882 return e1;
2883 if (e1->count < e2->count)
2884 return e2;
2886 /* Prefer critical edges to minimize inserting compensation code on
2887 critical edges. */
2889 if (EDGE_CRITICAL_P (e1) != EDGE_CRITICAL_P (e2))
2890 return EDGE_CRITICAL_P (e1) ? e1 : e2;
2892 /* Avoid non-deterministic behavior. */
2893 return (e1->src->index < e2->src->index) ? e1 : e2;
2896 /* Convert stack register references in one block. Return true if the CFG
2897 has been modified in the process. */
2899 static bool
2900 convert_regs_1 (basic_block block)
2902 struct stack_def regstack;
2903 block_info bi = BLOCK_INFO (block);
2904 int reg;
2905 rtx insn, next;
2906 bool control_flow_insn_deleted = false;
2907 bool cfg_altered = false;
2908 int debug_insns_with_starting_stack = 0;
2910 any_malformed_asm = false;
2912 /* Choose an initial stack layout, if one hasn't already been chosen. */
2913 if (bi->stack_in.top == -2)
2915 edge e, beste = NULL;
2916 edge_iterator ei;
2918 /* Select the best incoming edge (typically the most frequent) to
2919 use as a template for this basic block. */
2920 FOR_EACH_EDGE (e, ei, block->preds)
2921 if (BLOCK_INFO (e->src)->done)
2922 beste = better_edge (beste, e);
2924 if (beste)
2925 propagate_stack (beste);
2926 else
2928 /* No predecessors. Create an arbitrary input stack. */
2929 bi->stack_in.top = -1;
2930 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2931 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2932 bi->stack_in.reg[++bi->stack_in.top] = reg;
2936 if (dump_file)
2938 fprintf (dump_file, "\nBasic block %d\nInput stack: ", block->index);
2939 print_stack (dump_file, &bi->stack_in);
2942 /* Process all insns in this block. Keep track of NEXT so that we
2943 don't process insns emitted while substituting in INSN. */
2944 current_block = block;
2945 next = BB_HEAD (block);
2946 regstack = bi->stack_in;
2947 starting_stack_p = true;
2951 insn = next;
2952 next = NEXT_INSN (insn);
2954 /* Ensure we have not missed a block boundary. */
2955 gcc_assert (next);
2956 if (insn == BB_END (block))
2957 next = NULL;
2959 /* Don't bother processing unless there is a stack reg
2960 mentioned or if it's a CALL_INSN. */
2961 if (DEBUG_INSN_P (insn))
2963 if (starting_stack_p)
2964 debug_insns_with_starting_stack++;
2965 else
2967 subst_all_stack_regs_in_debug_insn (insn, &regstack);
2969 /* Nothing must ever die at a debug insn. If something
2970 is referenced in it that becomes dead, it should have
2971 died before and the reference in the debug insn
2972 should have been removed so as to avoid changing code
2973 generation. */
2974 gcc_assert (!find_reg_note (insn, REG_DEAD, NULL));
2977 else if (stack_regs_mentioned (insn)
2978 || CALL_P (insn))
2980 if (dump_file)
2982 fprintf (dump_file, " insn %d input stack: ",
2983 INSN_UID (insn));
2984 print_stack (dump_file, &regstack);
2986 control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
2987 starting_stack_p = false;
2990 while (next);
2992 if (debug_insns_with_starting_stack)
2994 /* Since it's the first non-debug instruction that determines
2995 the stack requirements of the current basic block, we refrain
2996 from updating debug insns before it in the loop above, and
2997 fix them up here. */
2998 for (insn = BB_HEAD (block); debug_insns_with_starting_stack;
2999 insn = NEXT_INSN (insn))
3001 if (!DEBUG_INSN_P (insn))
3002 continue;
3004 debug_insns_with_starting_stack--;
3005 subst_all_stack_regs_in_debug_insn (insn, &bi->stack_in);
3009 if (dump_file)
3011 fprintf (dump_file, "Expected live registers [");
3012 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
3013 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg))
3014 fprintf (dump_file, " %d", reg);
3015 fprintf (dump_file, " ]\nOutput stack: ");
3016 print_stack (dump_file, &regstack);
3019 insn = BB_END (block);
3020 if (JUMP_P (insn))
3021 insn = PREV_INSN (insn);
3023 /* If the function is declared to return a value, but it returns one
3024 in only some cases, some registers might come live here. Emit
3025 necessary moves for them. */
3027 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
3029 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg)
3030 && ! TEST_HARD_REG_BIT (regstack.reg_set, reg))
3032 rtx set;
3034 if (dump_file)
3035 fprintf (dump_file, "Emitting insn initializing reg %d\n", reg);
3037 set = gen_rtx_SET (VOIDmode, FP_MODE_REG (reg, SFmode), not_a_num);
3038 insn = emit_insn_after (set, insn);
3039 control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
3043 /* Amongst the insns possibly deleted during the substitution process above,
3044 might have been the only trapping insn in the block. We purge the now
3045 possibly dead EH edges here to avoid an ICE from fixup_abnormal_edges,
3046 called at the end of convert_regs. The order in which we process the
3047 blocks ensures that we never delete an already processed edge.
3049 Note that, at this point, the CFG may have been damaged by the emission
3050 of instructions after an abnormal call, which moves the basic block end
3051 (and is the reason why we call fixup_abnormal_edges later). So we must
3052 be sure that the trapping insn has been deleted before trying to purge
3053 dead edges, otherwise we risk purging valid edges.
3055 ??? We are normally supposed not to delete trapping insns, so we pretend
3056 that the insns deleted above don't actually trap. It would have been
3057 better to detect this earlier and avoid creating the EH edge in the first
3058 place, still, but we don't have enough information at that time. */
3060 if (control_flow_insn_deleted)
3061 cfg_altered |= purge_dead_edges (block);
3063 /* Something failed if the stack lives don't match. If we had malformed
3064 asms, we zapped the instruction itself, but that didn't produce the
3065 same pattern of register kills as before. */
3067 gcc_assert (hard_reg_set_equal_p (regstack.reg_set, bi->out_reg_set)
3068 || any_malformed_asm);
3069 bi->stack_out = regstack;
3070 bi->done = true;
3072 return cfg_altered;
3075 /* Convert registers in all blocks reachable from BLOCK. Return true if the
3076 CFG has been modified in the process. */
3078 static bool
3079 convert_regs_2 (basic_block block)
3081 basic_block *stack, *sp;
3082 bool cfg_altered = false;
3084 /* We process the blocks in a top-down manner, in a way such that one block
3085 is only processed after all its predecessors. The number of predecessors
3086 of every block has already been computed. */
3088 stack = XNEWVEC (basic_block, n_basic_blocks);
3089 sp = stack;
3091 *sp++ = block;
3095 edge e;
3096 edge_iterator ei;
3098 block = *--sp;
3100 /* Processing BLOCK is achieved by convert_regs_1, which may purge
3101 some dead EH outgoing edge after the deletion of the trapping
3102 insn inside the block. Since the number of predecessors of
3103 BLOCK's successors was computed based on the initial edge set,
3104 we check the necessity to process some of these successors
3105 before such an edge deletion may happen. However, there is
3106 a pitfall: if BLOCK is the only predecessor of a successor and
3107 the edge between them happens to be deleted, the successor
3108 becomes unreachable and should not be processed. The problem
3109 is that there is no way to preventively detect this case so we
3110 stack the successor in all cases and hand over the task of
3111 fixing up the discrepancy to convert_regs_1. */
3113 FOR_EACH_EDGE (e, ei, block->succs)
3114 if (! (e->flags & EDGE_DFS_BACK))
3116 BLOCK_INFO (e->dest)->predecessors--;
3117 if (!BLOCK_INFO (e->dest)->predecessors)
3118 *sp++ = e->dest;
3121 cfg_altered |= convert_regs_1 (block);
3123 while (sp != stack);
3125 free (stack);
3127 return cfg_altered;
3130 /* Traverse all basic blocks in a function, converting the register
3131 references in each insn from the "flat" register file that gcc uses,
3132 to the stack-like registers the 387 uses. */
3134 static void
3135 convert_regs (void)
3137 bool cfg_altered = false;
3138 int inserted;
3139 basic_block b;
3140 edge e;
3141 edge_iterator ei;
3143 /* Initialize uninitialized registers on function entry. */
3144 inserted = convert_regs_entry ();
3146 /* Construct the desired stack for function exit. */
3147 convert_regs_exit ();
3148 BLOCK_INFO (EXIT_BLOCK_PTR)->done = 1;
3150 /* ??? Future: process inner loops first, and give them arbitrary
3151 initial stacks which emit_swap_insn can modify. This ought to
3152 prevent double fxch that often appears at the head of a loop. */
3154 /* Process all blocks reachable from all entry points. */
3155 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3156 cfg_altered |= convert_regs_2 (e->dest);
3158 /* ??? Process all unreachable blocks. Though there's no excuse
3159 for keeping these even when not optimizing. */
3160 FOR_EACH_BB (b)
3162 block_info bi = BLOCK_INFO (b);
3164 if (! bi->done)
3165 cfg_altered |= convert_regs_2 (b);
3168 /* We must fix up abnormal edges before inserting compensation code
3169 because both mechanisms insert insns on edges. */
3170 inserted |= fixup_abnormal_edges ();
3172 inserted |= compensate_edges ();
3174 clear_aux_for_blocks ();
3176 if (inserted)
3177 commit_edge_insertions ();
3179 if (cfg_altered)
3180 cleanup_cfg (0);
3182 if (dump_file)
3183 fputc ('\n', dump_file);
3186 /* Convert register usage from "flat" register file usage to a "stack
3187 register file. FILE is the dump file, if used.
3189 Construct a CFG and run life analysis. Then convert each insn one
3190 by one. Run a last cleanup_cfg pass, if optimizing, to eliminate
3191 code duplication created when the converter inserts pop insns on
3192 the edges. */
3194 static bool
3195 reg_to_stack (void)
3197 basic_block bb;
3198 int i;
3199 int max_uid;
3201 /* Clean up previous run. */
3202 if (stack_regs_mentioned_data != NULL)
3203 VEC_free (char, heap, stack_regs_mentioned_data);
3205 /* See if there is something to do. Flow analysis is quite
3206 expensive so we might save some compilation time. */
3207 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
3208 if (df_regs_ever_live_p (i))
3209 break;
3210 if (i > LAST_STACK_REG)
3211 return false;
3213 df_note_add_problem ();
3214 df_analyze ();
3216 mark_dfs_back_edges ();
3218 /* Set up block info for each basic block. */
3219 alloc_aux_for_blocks (sizeof (struct block_info_def));
3220 FOR_EACH_BB (bb)
3222 block_info bi = BLOCK_INFO (bb);
3223 edge_iterator ei;
3224 edge e;
3225 int reg;
3227 FOR_EACH_EDGE (e, ei, bb->preds)
3228 if (!(e->flags & EDGE_DFS_BACK)
3229 && e->src != ENTRY_BLOCK_PTR)
3230 bi->predecessors++;
3232 /* Set current register status at last instruction `uninitialized'. */
3233 bi->stack_in.top = -2;
3235 /* Copy live_at_end and live_at_start into temporaries. */
3236 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
3238 if (REGNO_REG_SET_P (DF_LR_OUT (bb), reg))
3239 SET_HARD_REG_BIT (bi->out_reg_set, reg);
3240 if (REGNO_REG_SET_P (DF_LR_IN (bb), reg))
3241 SET_HARD_REG_BIT (bi->stack_in.reg_set, reg);
3245 /* Create the replacement registers up front. */
3246 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
3248 enum machine_mode mode;
3249 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3250 mode != VOIDmode;
3251 mode = GET_MODE_WIDER_MODE (mode))
3252 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
3253 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT);
3254 mode != VOIDmode;
3255 mode = GET_MODE_WIDER_MODE (mode))
3256 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
3259 ix86_flags_rtx = gen_rtx_REG (CCmode, FLAGS_REG);
3261 /* A QNaN for initializing uninitialized variables.
3263 ??? We can't load from constant memory in PIC mode, because
3264 we're inserting these instructions before the prologue and
3265 the PIC register hasn't been set up. In that case, fall back
3266 on zero, which we can get from `fldz'. */
3268 if ((flag_pic && !TARGET_64BIT)
3269 || ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC)
3270 not_a_num = CONST0_RTX (SFmode);
3271 else
3273 REAL_VALUE_TYPE r;
3275 real_nan (&r, "", 1, SFmode);
3276 not_a_num = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
3277 not_a_num = force_const_mem (SFmode, not_a_num);
3280 /* Allocate a cache for stack_regs_mentioned. */
3281 max_uid = get_max_uid ();
3282 stack_regs_mentioned_data = VEC_alloc (char, heap, max_uid + 1);
3283 memset (VEC_address (char, stack_regs_mentioned_data),
3284 0, sizeof (char) * (max_uid + 1));
3286 convert_regs ();
3288 free_aux_for_blocks ();
3289 return true;
3291 #endif /* STACK_REGS */
3293 static bool
3294 gate_handle_stack_regs (void)
3296 #ifdef STACK_REGS
3297 return 1;
3298 #else
3299 return 0;
3300 #endif
3303 struct rtl_opt_pass pass_stack_regs =
3306 RTL_PASS,
3307 "*stack_regs", /* name */
3308 gate_handle_stack_regs, /* gate */
3309 NULL, /* execute */
3310 NULL, /* sub */
3311 NULL, /* next */
3312 0, /* static_pass_number */
3313 TV_REG_STACK, /* tv_id */
3314 0, /* properties_required */
3315 0, /* properties_provided */
3316 0, /* properties_destroyed */
3317 0, /* todo_flags_start */
3318 0 /* todo_flags_finish */
3322 /* Convert register usage from flat register file usage to a stack
3323 register file. */
3324 static unsigned int
3325 rest_of_handle_stack_regs (void)
3327 #ifdef STACK_REGS
3328 reg_to_stack ();
3329 regstack_completed = 1;
3330 #endif
3331 return 0;
3334 struct rtl_opt_pass pass_stack_regs_run =
3337 RTL_PASS,
3338 "stack", /* name */
3339 NULL, /* gate */
3340 rest_of_handle_stack_regs, /* execute */
3341 NULL, /* sub */
3342 NULL, /* next */
3343 0, /* static_pass_number */
3344 TV_REG_STACK, /* tv_id */
3345 0, /* properties_required */
3346 0, /* properties_provided */
3347 0, /* properties_destroyed */
3348 0, /* todo_flags_start */
3349 TODO_df_finish | TODO_verify_rtl_sharing |
3350 TODO_ggc_collect /* todo_flags_finish */