Add C++11 header <cuchar>.
[official-gcc.git] / gcc / reg-stack.c
blob3944041d0df66b6f368532f0e5f9915ba9f07eca
1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This pass converts stack-like registers from the "flat register
21 file" model that gcc uses, to a stack convention that the 387 uses.
23 * The form of the input:
25 On input, the function consists of insn that have had their
26 registers fully allocated to a set of "virtual" registers. Note that
27 the word "virtual" is used differently here than elsewhere in gcc: for
28 each virtual stack reg, there is a hard reg, but the mapping between
29 them is not known until this pass is run. On output, hard register
30 numbers have been substituted, and various pop and exchange insns have
31 been emitted. The hard register numbers and the virtual register
32 numbers completely overlap - before this pass, all stack register
33 numbers are virtual, and afterward they are all hard.
35 The virtual registers can be manipulated normally by gcc, and their
36 semantics are the same as for normal registers. After the hard
37 register numbers are substituted, the semantics of an insn containing
38 stack-like regs are not the same as for an insn with normal regs: for
39 instance, it is not safe to delete an insn that appears to be a no-op
40 move. In general, no insn containing hard regs should be changed
41 after this pass is done.
43 * The form of the output:
45 After this pass, hard register numbers represent the distance from
46 the current top of stack to the desired register. A reference to
47 FIRST_STACK_REG references the top of stack, FIRST_STACK_REG + 1,
48 represents the register just below that, and so forth. Also, REG_DEAD
49 notes indicate whether or not a stack register should be popped.
51 A "swap" insn looks like a parallel of two patterns, where each
52 pattern is a SET: one sets A to B, the other B to A.
54 A "push" or "load" insn is a SET whose SET_DEST is FIRST_STACK_REG
55 and whose SET_DEST is REG or MEM. Any other SET_DEST, such as PLUS,
56 will replace the existing stack top, not push a new value.
58 A store insn is a SET whose SET_DEST is FIRST_STACK_REG, and whose
59 SET_SRC is REG or MEM.
61 The case where the SET_SRC and SET_DEST are both FIRST_STACK_REG
62 appears ambiguous. As a special case, the presence of a REG_DEAD note
63 for FIRST_STACK_REG differentiates between a load insn and a pop.
65 If a REG_DEAD is present, the insn represents a "pop" that discards
66 the top of the register stack. If there is no REG_DEAD note, then the
67 insn represents a "dup" or a push of the current top of stack onto the
68 stack.
70 * Methodology:
72 Existing REG_DEAD and REG_UNUSED notes for stack registers are
73 deleted and recreated from scratch. REG_DEAD is never created for a
74 SET_DEST, only REG_UNUSED.
76 * asm_operands:
78 There are several rules on the usage of stack-like regs in
79 asm_operands insns. These rules apply only to the operands that are
80 stack-like regs:
82 1. Given a set of input regs that die in an asm_operands, it is
83 necessary to know which are implicitly popped by the asm, and
84 which must be explicitly popped by gcc.
86 An input reg that is implicitly popped by the asm must be
87 explicitly clobbered, unless it is constrained to match an
88 output operand.
90 2. For any input reg that is implicitly popped by an asm, it is
91 necessary to know how to adjust the stack to compensate for the pop.
92 If any non-popped input is closer to the top of the reg-stack than
93 the implicitly popped reg, it would not be possible to know what the
94 stack looked like - it's not clear how the rest of the stack "slides
95 up".
97 All implicitly popped input regs must be closer to the top of
98 the reg-stack than any input that is not implicitly popped.
100 3. It is possible that if an input dies in an insn, reload might
101 use the input reg for an output reload. Consider this example:
103 asm ("foo" : "=t" (a) : "f" (b));
105 This asm says that input B is not popped by the asm, and that
106 the asm pushes a result onto the reg-stack, i.e., the stack is one
107 deeper after the asm than it was before. But, it is possible that
108 reload will think that it can use the same reg for both the input and
109 the output, if input B dies in this insn.
111 If any input operand uses the "f" constraint, all output reg
112 constraints must use the "&" earlyclobber.
114 The asm above would be written as
116 asm ("foo" : "=&t" (a) : "f" (b));
118 4. Some operands need to be in particular places on the stack. All
119 output operands fall in this category - there is no other way to
120 know which regs the outputs appear in unless the user indicates
121 this in the constraints.
123 Output operands must specifically indicate which reg an output
124 appears in after an asm. "=f" is not allowed: the operand
125 constraints must select a class with a single reg.
127 5. Output operands may not be "inserted" between existing stack regs.
128 Since no 387 opcode uses a read/write operand, all output operands
129 are dead before the asm_operands, and are pushed by the asm_operands.
130 It makes no sense to push anywhere but the top of the reg-stack.
132 Output operands must start at the top of the reg-stack: output
133 operands may not "skip" a reg.
135 6. Some asm statements may need extra stack space for internal
136 calculations. This can be guaranteed by clobbering stack registers
137 unrelated to the inputs and outputs.
139 Here are a couple of reasonable asms to want to write. This asm
140 takes one input, which is internally popped, and produces two outputs.
142 asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
144 This asm takes two inputs, which are popped by the fyl2xp1 opcode,
145 and replaces them with one output. The user must code the "st(1)"
146 clobber for reg-stack.c to know that fyl2xp1 pops both inputs.
148 asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
152 #include "config.h"
153 #include "system.h"
154 #include "coretypes.h"
155 #include "backend.h"
156 #include "tree.h"
157 #include "rtl.h"
158 #include "df.h"
159 #include "alias.h"
160 #include "varasm.h"
161 #include "rtl-error.h"
162 #include "tm_p.h"
163 #include "insn-config.h"
164 #include "regs.h"
165 #include "flags.h"
166 #include "recog.h"
167 #include "cfgrtl.h"
168 #include "cfganal.h"
169 #include "cfgbuild.h"
170 #include "cfgcleanup.h"
171 #include "reload.h"
172 #include "tree-pass.h"
173 #include "target.h"
174 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
175 #include "rtl-iter.h"
177 #ifdef STACK_REGS
179 /* We use this array to cache info about insns, because otherwise we
180 spend too much time in stack_regs_mentioned_p.
182 Indexed by insn UIDs. A value of zero is uninitialized, one indicates
183 the insn uses stack registers, two indicates the insn does not use
184 stack registers. */
185 static vec<char> stack_regs_mentioned_data;
187 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
189 int regstack_completed = 0;
191 /* This is the basic stack record. TOP is an index into REG[] such
192 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
194 If TOP is -2, REG[] is not yet initialized. Stack initialization
195 consists of placing each live reg in array `reg' and setting `top'
196 appropriately.
198 REG_SET indicates which registers are live. */
200 typedef struct stack_def
202 int top; /* index to top stack element */
203 HARD_REG_SET reg_set; /* set of live registers */
204 unsigned char reg[REG_STACK_SIZE];/* register - stack mapping */
205 } *stack_ptr;
207 /* This is used to carry information about basic blocks. It is
208 attached to the AUX field of the standard CFG block. */
210 typedef struct block_info_def
212 struct stack_def stack_in; /* Input stack configuration. */
213 struct stack_def stack_out; /* Output stack configuration. */
214 HARD_REG_SET out_reg_set; /* Stack regs live on output. */
215 int done; /* True if block already converted. */
216 int predecessors; /* Number of predecessors that need
217 to be visited. */
218 } *block_info;
220 #define BLOCK_INFO(B) ((block_info) (B)->aux)
222 /* Passed to change_stack to indicate where to emit insns. */
223 enum emit_where
225 EMIT_AFTER,
226 EMIT_BEFORE
229 /* The block we're currently working on. */
230 static basic_block current_block;
232 /* In the current_block, whether we're processing the first register
233 stack or call instruction, i.e. the regstack is currently the
234 same as BLOCK_INFO(current_block)->stack_in. */
235 static bool starting_stack_p;
237 /* This is the register file for all register after conversion. */
238 static rtx
239 FP_mode_reg[LAST_STACK_REG+1-FIRST_STACK_REG][(int) MAX_MACHINE_MODE];
241 #define FP_MODE_REG(regno,mode) \
242 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int) (mode)])
244 /* Used to initialize uninitialized registers. */
245 static rtx not_a_num;
247 /* Forward declarations */
249 static int stack_regs_mentioned_p (const_rtx pat);
250 static void pop_stack (stack_ptr, int);
251 static rtx *get_true_reg (rtx *);
253 static int check_asm_stack_operands (rtx_insn *);
254 static void get_asm_operands_in_out (rtx, int *, int *);
255 static rtx stack_result (tree);
256 static void replace_reg (rtx *, int);
257 static void remove_regno_note (rtx_insn *, enum reg_note, unsigned int);
258 static int get_hard_regnum (stack_ptr, rtx);
259 static rtx_insn *emit_pop_insn (rtx_insn *, stack_ptr, rtx, enum emit_where);
260 static void swap_to_top (rtx_insn *, stack_ptr, rtx, rtx);
261 static bool move_for_stack_reg (rtx_insn *, stack_ptr, rtx);
262 static bool move_nan_for_stack_reg (rtx_insn *, stack_ptr, rtx);
263 static int swap_rtx_condition_1 (rtx);
264 static int swap_rtx_condition (rtx_insn *);
265 static void compare_for_stack_reg (rtx_insn *, stack_ptr, rtx);
266 static bool subst_stack_regs_pat (rtx_insn *, stack_ptr, rtx);
267 static void subst_asm_stack_regs (rtx_insn *, stack_ptr);
268 static bool subst_stack_regs (rtx_insn *, stack_ptr);
269 static void change_stack (rtx_insn *, stack_ptr, stack_ptr, enum emit_where);
270 static void print_stack (FILE *, stack_ptr);
271 static rtx_insn *next_flags_user (rtx_insn *);
273 /* Return nonzero if any stack register is mentioned somewhere within PAT. */
275 static int
276 stack_regs_mentioned_p (const_rtx pat)
278 const char *fmt;
279 int i;
281 if (STACK_REG_P (pat))
282 return 1;
284 fmt = GET_RTX_FORMAT (GET_CODE (pat));
285 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
287 if (fmt[i] == 'E')
289 int j;
291 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
292 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
293 return 1;
295 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
296 return 1;
299 return 0;
302 /* Return nonzero if INSN mentions stacked registers, else return zero. */
305 stack_regs_mentioned (const_rtx insn)
307 unsigned int uid, max;
308 int test;
310 if (! INSN_P (insn) || !stack_regs_mentioned_data.exists ())
311 return 0;
313 uid = INSN_UID (insn);
314 max = stack_regs_mentioned_data.length ();
315 if (uid >= max)
317 /* Allocate some extra size to avoid too many reallocs, but
318 do not grow too quickly. */
319 max = uid + uid / 20 + 1;
320 stack_regs_mentioned_data.safe_grow_cleared (max);
323 test = stack_regs_mentioned_data[uid];
324 if (test == 0)
326 /* This insn has yet to be examined. Do so now. */
327 test = stack_regs_mentioned_p (PATTERN (insn)) ? 1 : 2;
328 stack_regs_mentioned_data[uid] = test;
331 return test == 1;
334 static rtx ix86_flags_rtx;
336 static rtx_insn *
337 next_flags_user (rtx_insn *insn)
339 /* Search forward looking for the first use of this value.
340 Stop at block boundaries. */
342 while (insn != BB_END (current_block))
344 insn = NEXT_INSN (insn);
346 if (INSN_P (insn) && reg_mentioned_p (ix86_flags_rtx, PATTERN (insn)))
347 return insn;
349 if (CALL_P (insn))
350 return NULL;
352 return NULL;
355 /* Reorganize the stack into ascending numbers, before this insn. */
357 static void
358 straighten_stack (rtx_insn *insn, stack_ptr regstack)
360 struct stack_def temp_stack;
361 int top;
363 /* If there is only a single register on the stack, then the stack is
364 already in increasing order and no reorganization is needed.
366 Similarly if the stack is empty. */
367 if (regstack->top <= 0)
368 return;
370 COPY_HARD_REG_SET (temp_stack.reg_set, regstack->reg_set);
372 for (top = temp_stack.top = regstack->top; top >= 0; top--)
373 temp_stack.reg[top] = FIRST_STACK_REG + temp_stack.top - top;
375 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
378 /* Pop a register from the stack. */
380 static void
381 pop_stack (stack_ptr regstack, int regno)
383 int top = regstack->top;
385 CLEAR_HARD_REG_BIT (regstack->reg_set, regno);
386 regstack->top--;
387 /* If regno was not at the top of stack then adjust stack. */
388 if (regstack->reg [top] != regno)
390 int i;
391 for (i = regstack->top; i >= 0; i--)
392 if (regstack->reg [i] == regno)
394 int j;
395 for (j = i; j < top; j++)
396 regstack->reg [j] = regstack->reg [j + 1];
397 break;
402 /* Return a pointer to the REG expression within PAT. If PAT is not a
403 REG, possible enclosed by a conversion rtx, return the inner part of
404 PAT that stopped the search. */
406 static rtx *
407 get_true_reg (rtx *pat)
409 for (;;)
410 switch (GET_CODE (*pat))
412 case SUBREG:
413 /* Eliminate FP subregister accesses in favor of the
414 actual FP register in use. */
416 rtx subreg;
417 if (STACK_REG_P (subreg = SUBREG_REG (*pat)))
419 int regno_off = subreg_regno_offset (REGNO (subreg),
420 GET_MODE (subreg),
421 SUBREG_BYTE (*pat),
422 GET_MODE (*pat));
423 *pat = FP_MODE_REG (REGNO (subreg) + regno_off,
424 GET_MODE (subreg));
425 return pat;
428 case FLOAT:
429 case FIX:
430 case FLOAT_EXTEND:
431 pat = & XEXP (*pat, 0);
432 break;
434 case UNSPEC:
435 if (XINT (*pat, 1) == UNSPEC_TRUNC_NOOP
436 || XINT (*pat, 1) == UNSPEC_FILD_ATOMIC)
437 pat = & XVECEXP (*pat, 0, 0);
438 return pat;
440 case FLOAT_TRUNCATE:
441 if (!flag_unsafe_math_optimizations)
442 return pat;
443 pat = & XEXP (*pat, 0);
444 break;
446 default:
447 return pat;
451 /* Set if we find any malformed asms in a block. */
452 static bool any_malformed_asm;
454 /* There are many rules that an asm statement for stack-like regs must
455 follow. Those rules are explained at the top of this file: the rule
456 numbers below refer to that explanation. */
458 static int
459 check_asm_stack_operands (rtx_insn *insn)
461 int i;
462 int n_clobbers;
463 int malformed_asm = 0;
464 rtx body = PATTERN (insn);
466 char reg_used_as_output[FIRST_PSEUDO_REGISTER];
467 char implicitly_dies[FIRST_PSEUDO_REGISTER];
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_constrain_insn (insn);
476 preprocess_constraints (insn);
478 get_asm_operands_in_out (body, &n_outputs, &n_inputs);
480 if (which_alternative < 0)
482 malformed_asm = 1;
483 /* Avoid further trouble with this insn. */
484 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
485 return 0;
487 const operand_alternative *op_alt = which_op_alt ();
489 /* Strip SUBREGs here to make the following code simpler. */
490 for (i = 0; i < recog_data.n_operands; i++)
491 if (GET_CODE (recog_data.operand[i]) == SUBREG
492 && REG_P (SUBREG_REG (recog_data.operand[i])))
493 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
495 /* Set up CLOBBER_REG. */
497 n_clobbers = 0;
499 if (GET_CODE (body) == PARALLEL)
501 clobber_reg = XALLOCAVEC (rtx, XVECLEN (body, 0));
503 for (i = 0; i < XVECLEN (body, 0); i++)
504 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
506 rtx clobber = XVECEXP (body, 0, i);
507 rtx reg = XEXP (clobber, 0);
509 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
510 reg = SUBREG_REG (reg);
512 if (STACK_REG_P (reg))
514 clobber_reg[n_clobbers] = reg;
515 n_clobbers++;
520 /* Enforce rule #4: Output operands must specifically indicate which
521 reg an output appears in after an asm. "=f" is not allowed: the
522 operand constraints must select a class with a single reg.
524 Also enforce rule #5: Output operands must start at the top of
525 the reg-stack: output operands may not "skip" a reg. */
527 memset (reg_used_as_output, 0, sizeof (reg_used_as_output));
528 for (i = 0; i < n_outputs; i++)
529 if (STACK_REG_P (recog_data.operand[i]))
531 if (reg_class_size[(int) op_alt[i].cl] != 1)
533 error_for_asm (insn, "output constraint %d must specify a single register", i);
534 malformed_asm = 1;
536 else
538 int j;
540 for (j = 0; j < n_clobbers; j++)
541 if (REGNO (recog_data.operand[i]) == REGNO (clobber_reg[j]))
543 error_for_asm (insn, "output constraint %d cannot be specified together with \"%s\" clobber",
544 i, reg_names [REGNO (clobber_reg[j])]);
545 malformed_asm = 1;
546 break;
548 if (j == n_clobbers)
549 reg_used_as_output[REGNO (recog_data.operand[i])] = 1;
554 /* Search for first non-popped reg. */
555 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
556 if (! reg_used_as_output[i])
557 break;
559 /* If there are any other popped regs, that's an error. */
560 for (; i < LAST_STACK_REG + 1; i++)
561 if (reg_used_as_output[i])
562 break;
564 if (i != LAST_STACK_REG + 1)
566 error_for_asm (insn, "output regs must be grouped at top of stack");
567 malformed_asm = 1;
570 /* Enforce rule #2: All implicitly popped input regs must be closer
571 to the top of the reg-stack than any input that is not implicitly
572 popped. */
574 memset (implicitly_dies, 0, sizeof (implicitly_dies));
575 for (i = n_outputs; i < n_outputs + n_inputs; i++)
576 if (STACK_REG_P (recog_data.operand[i]))
578 /* An input reg is implicitly popped if it is tied to an
579 output, or if there is a CLOBBER for it. */
580 int j;
582 for (j = 0; j < n_clobbers; j++)
583 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
584 break;
586 if (j < n_clobbers || op_alt[i].matches >= 0)
587 implicitly_dies[REGNO (recog_data.operand[i])] = 1;
590 /* Search for first non-popped reg. */
591 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
592 if (! implicitly_dies[i])
593 break;
595 /* If there are any other popped regs, that's an error. */
596 for (; i < LAST_STACK_REG + 1; i++)
597 if (implicitly_dies[i])
598 break;
600 if (i != LAST_STACK_REG + 1)
602 error_for_asm (insn,
603 "implicitly popped regs must be grouped at top of stack");
604 malformed_asm = 1;
607 /* Enforce rule #3: If any input operand uses the "f" constraint, all
608 output constraints must use the "&" earlyclobber.
610 ??? Detect this more deterministically by having constrain_asm_operands
611 record any earlyclobber. */
613 for (i = n_outputs; i < n_outputs + n_inputs; i++)
614 if (op_alt[i].matches == -1)
616 int j;
618 for (j = 0; j < n_outputs; j++)
619 if (operands_match_p (recog_data.operand[j], recog_data.operand[i]))
621 error_for_asm (insn,
622 "output operand %d must use %<&%> constraint", j);
623 malformed_asm = 1;
627 if (malformed_asm)
629 /* Avoid further trouble with this insn. */
630 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
631 any_malformed_asm = true;
632 return 0;
635 return 1;
638 /* Calculate the number of inputs and outputs in BODY, an
639 asm_operands. N_OPERANDS is the total number of operands, and
640 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
641 placed. */
643 static void
644 get_asm_operands_in_out (rtx body, int *pout, int *pin)
646 rtx asmop = extract_asm_operands (body);
648 *pin = ASM_OPERANDS_INPUT_LENGTH (asmop);
649 *pout = (recog_data.n_operands
650 - ASM_OPERANDS_INPUT_LENGTH (asmop)
651 - ASM_OPERANDS_LABEL_LENGTH (asmop));
654 /* If current function returns its result in an fp stack register,
655 return the REG. Otherwise, return 0. */
657 static rtx
658 stack_result (tree decl)
660 rtx result;
662 /* If the value is supposed to be returned in memory, then clearly
663 it is not returned in a stack register. */
664 if (aggregate_value_p (DECL_RESULT (decl), decl))
665 return 0;
667 result = DECL_RTL_IF_SET (DECL_RESULT (decl));
668 if (result != 0)
669 result = targetm.calls.function_value (TREE_TYPE (DECL_RESULT (decl)),
670 decl, true);
672 return result != 0 && STACK_REG_P (result) ? result : 0;
677 * This section deals with stack register substitution, and forms the second
678 * pass over the RTL.
681 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
682 the desired hard REGNO. */
684 static void
685 replace_reg (rtx *reg, int regno)
687 gcc_assert (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG));
688 gcc_assert (STACK_REG_P (*reg));
690 gcc_assert (SCALAR_FLOAT_MODE_P (GET_MODE (*reg))
691 || GET_MODE_CLASS (GET_MODE (*reg)) == MODE_COMPLEX_FLOAT);
693 *reg = FP_MODE_REG (regno, GET_MODE (*reg));
696 /* Remove a note of type NOTE, which must be found, for register
697 number REGNO from INSN. Remove only one such note. */
699 static void
700 remove_regno_note (rtx_insn *insn, enum reg_note note, unsigned int regno)
702 rtx *note_link, this_rtx;
704 note_link = &REG_NOTES (insn);
705 for (this_rtx = *note_link; this_rtx; this_rtx = XEXP (this_rtx, 1))
706 if (REG_NOTE_KIND (this_rtx) == note
707 && REG_P (XEXP (this_rtx, 0)) && REGNO (XEXP (this_rtx, 0)) == regno)
709 *note_link = XEXP (this_rtx, 1);
710 return;
712 else
713 note_link = &XEXP (this_rtx, 1);
715 gcc_unreachable ();
718 /* Find the hard register number of virtual register REG in REGSTACK.
719 The hard register number is relative to the top of the stack. -1 is
720 returned if the register is not found. */
722 static int
723 get_hard_regnum (stack_ptr regstack, rtx reg)
725 int i;
727 gcc_assert (STACK_REG_P (reg));
729 for (i = regstack->top; i >= 0; i--)
730 if (regstack->reg[i] == REGNO (reg))
731 break;
733 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
736 /* Emit an insn to pop virtual register REG before or after INSN.
737 REGSTACK is the stack state after INSN and is updated to reflect this
738 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
739 is represented as a SET whose destination is the register to be popped
740 and source is the top of stack. A death note for the top of stack
741 cases the movdf pattern to pop. */
743 static rtx_insn *
744 emit_pop_insn (rtx_insn *insn, stack_ptr regstack, rtx reg, enum emit_where where)
746 rtx_insn *pop_insn;
747 rtx 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;
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 (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 *insn, stack_ptr regstack, rtx reg)
798 int hard_regno;
799 rtx swap_rtx;
800 int other_reg; /* swap regno temps */
801 rtx_insn *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);
821 std::swap (regstack->reg[regstack->top], regstack->reg[other_reg]);
823 /* Find the previous insn involving stack regs, but don't pass a
824 block boundary. */
825 i1 = NULL;
826 if (current_block && insn != BB_HEAD (current_block))
828 rtx_insn *tmp = PREV_INSN (insn);
829 rtx_insn *limit = PREV_INSN (BB_HEAD (current_block));
830 while (tmp != limit)
832 if (LABEL_P (tmp)
833 || CALL_P (tmp)
834 || NOTE_INSN_BASIC_BLOCK_P (tmp)
835 || (NONJUMP_INSN_P (tmp)
836 && stack_regs_mentioned (tmp)))
838 i1 = tmp;
839 break;
841 tmp = PREV_INSN (tmp);
845 if (i1 != NULL_RTX
846 && (i1set = single_set (i1)) != NULL_RTX)
848 rtx i1src = *get_true_reg (&SET_SRC (i1set));
849 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
851 /* If the previous register stack push was from the reg we are to
852 swap with, omit the swap. */
854 if (REG_P (i1dest) && REGNO (i1dest) == FIRST_STACK_REG
855 && REG_P (i1src)
856 && REGNO (i1src) == (unsigned) hard_regno - 1
857 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
858 return;
860 /* If the previous insn wrote to the reg we are to swap with,
861 omit the swap. */
863 if (REG_P (i1dest) && REGNO (i1dest) == (unsigned) hard_regno
864 && REG_P (i1src) && REGNO (i1src) == FIRST_STACK_REG
865 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
866 return;
869 /* Avoid emitting the swap if this is the first register stack insn
870 of the current_block. Instead update the current_block's stack_in
871 and let compensate edges take care of this for us. */
872 if (current_block && starting_stack_p)
874 BLOCK_INFO (current_block)->stack_in = *regstack;
875 starting_stack_p = false;
876 return;
879 swap_rtx = gen_swapxf (FP_MODE_REG (hard_regno, XFmode),
880 FP_MODE_REG (FIRST_STACK_REG, XFmode));
882 if (i1)
883 emit_insn_after (swap_rtx, i1);
884 else if (current_block)
885 emit_insn_before (swap_rtx, BB_HEAD (current_block));
886 else
887 emit_insn_before (swap_rtx, insn);
890 /* Emit an insns before INSN to swap virtual register SRC1 with
891 the top of stack and virtual register SRC2 with second stack
892 slot. REGSTACK is the stack state before the swaps, and
893 is updated to reflect the swaps. A swap insn is represented as a
894 PARALLEL of two patterns: each pattern moves one reg to the other.
896 If SRC1 and/or SRC2 are already at the right place, no swap insn
897 is emitted. */
899 static void
900 swap_to_top (rtx_insn *insn, stack_ptr regstack, rtx src1, rtx src2)
902 struct stack_def temp_stack;
903 int regno, j, k;
905 temp_stack = *regstack;
907 /* Place operand 1 at the top of stack. */
908 regno = get_hard_regnum (&temp_stack, src1);
909 gcc_assert (regno >= 0);
910 if (regno != FIRST_STACK_REG)
912 k = temp_stack.top - (regno - FIRST_STACK_REG);
913 j = temp_stack.top;
915 std::swap (temp_stack.reg[j], temp_stack.reg[k]);
918 /* Place operand 2 next on the stack. */
919 regno = get_hard_regnum (&temp_stack, src2);
920 gcc_assert (regno >= 0);
921 if (regno != FIRST_STACK_REG + 1)
923 k = temp_stack.top - (regno - FIRST_STACK_REG);
924 j = temp_stack.top - 1;
926 std::swap (temp_stack.reg[j], temp_stack.reg[k]);
929 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
932 /* Handle a move to or from a stack register in PAT, which is in INSN.
933 REGSTACK is the current stack. Return whether a control flow insn
934 was deleted in the process. */
936 static bool
937 move_for_stack_reg (rtx_insn *insn, stack_ptr regstack, rtx pat)
939 rtx *psrc = get_true_reg (&SET_SRC (pat));
940 rtx *pdest = get_true_reg (&SET_DEST (pat));
941 rtx src, dest;
942 rtx note;
943 bool control_flow_insn_deleted = false;
945 src = *psrc; dest = *pdest;
947 if (STACK_REG_P (src) && STACK_REG_P (dest))
949 /* Write from one stack reg to another. If SRC dies here, then
950 just change the register mapping and delete the insn. */
952 note = find_regno_note (insn, REG_DEAD, REGNO (src));
953 if (note)
955 int i;
957 /* If this is a no-op move, there must not be a REG_DEAD note. */
958 gcc_assert (REGNO (src) != REGNO (dest));
960 for (i = regstack->top; i >= 0; i--)
961 if (regstack->reg[i] == REGNO (src))
962 break;
964 /* The destination must be dead, or life analysis is borked. */
965 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
967 /* If the source is not live, this is yet another case of
968 uninitialized variables. Load up a NaN instead. */
969 if (i < 0)
970 return move_nan_for_stack_reg (insn, regstack, dest);
972 /* It is possible that the dest is unused after this insn.
973 If so, just pop the src. */
975 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
976 emit_pop_insn (insn, regstack, src, EMIT_AFTER);
977 else
979 regstack->reg[i] = REGNO (dest);
980 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
981 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
984 control_flow_insn_deleted |= control_flow_insn_p (insn);
985 delete_insn (insn);
986 return control_flow_insn_deleted;
989 /* The source reg does not die. */
991 /* If this appears to be a no-op move, delete it, or else it
992 will confuse the machine description output patterns. But if
993 it is REG_UNUSED, we must pop the reg now, as per-insn processing
994 for REG_UNUSED will not work for deleted insns. */
996 if (REGNO (src) == REGNO (dest))
998 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
999 emit_pop_insn (insn, regstack, dest, EMIT_AFTER);
1001 control_flow_insn_deleted |= control_flow_insn_p (insn);
1002 delete_insn (insn);
1003 return control_flow_insn_deleted;
1006 /* The destination ought to be dead. */
1007 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
1009 replace_reg (psrc, get_hard_regnum (regstack, src));
1011 regstack->reg[++regstack->top] = REGNO (dest);
1012 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1013 replace_reg (pdest, FIRST_STACK_REG);
1015 else if (STACK_REG_P (src))
1017 /* Save from a stack reg to MEM, or possibly integer reg. Since
1018 only top of stack may be saved, emit an exchange first if
1019 needs be. */
1021 emit_swap_insn (insn, regstack, src);
1023 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1024 if (note)
1026 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1027 regstack->top--;
1028 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1030 else if ((GET_MODE (src) == XFmode)
1031 && regstack->top < REG_STACK_SIZE - 1)
1033 /* A 387 cannot write an XFmode value to a MEM without
1034 clobbering the source reg. The output code can handle
1035 this by reading back the value from the MEM.
1036 But it is more efficient to use a temp register if one is
1037 available. Push the source value here if the register
1038 stack is not full, and then write the value to memory via
1039 a pop. */
1040 rtx push_rtx;
1041 rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, GET_MODE (src));
1043 push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
1044 emit_insn_before (push_rtx, insn);
1045 add_reg_note (insn, REG_DEAD, top_stack_reg);
1048 replace_reg (psrc, FIRST_STACK_REG);
1050 else
1052 rtx pat = PATTERN (insn);
1054 gcc_assert (STACK_REG_P (dest));
1056 /* Load from MEM, or possibly integer REG or constant, into the
1057 stack regs. The actual target is always the top of the
1058 stack. The stack mapping is changed to reflect that DEST is
1059 now at top of stack. */
1061 /* The destination ought to be dead. However, there is a
1062 special case with i387 UNSPEC_TAN, where destination is live
1063 (an argument to fptan) but inherent load of 1.0 is modelled
1064 as a load from a constant. */
1065 if (GET_CODE (pat) == PARALLEL
1066 && XVECLEN (pat, 0) == 2
1067 && GET_CODE (XVECEXP (pat, 0, 1)) == SET
1068 && GET_CODE (SET_SRC (XVECEXP (pat, 0, 1))) == UNSPEC
1069 && XINT (SET_SRC (XVECEXP (pat, 0, 1)), 1) == UNSPEC_TAN)
1070 emit_swap_insn (insn, regstack, dest);
1071 else
1072 gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
1074 gcc_assert (regstack->top < REG_STACK_SIZE);
1076 regstack->reg[++regstack->top] = REGNO (dest);
1077 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1078 replace_reg (pdest, FIRST_STACK_REG);
1081 return control_flow_insn_deleted;
1084 /* A helper function which replaces INSN with a pattern that loads up
1085 a NaN into DEST, then invokes move_for_stack_reg. */
1087 static bool
1088 move_nan_for_stack_reg (rtx_insn *insn, stack_ptr regstack, rtx dest)
1090 rtx pat;
1092 dest = FP_MODE_REG (REGNO (dest), SFmode);
1093 pat = gen_rtx_SET (dest, not_a_num);
1094 PATTERN (insn) = pat;
1095 INSN_CODE (insn) = -1;
1097 return move_for_stack_reg (insn, regstack, pat);
1100 /* Swap the condition on a branch, if there is one. Return true if we
1101 found a condition to swap. False if the condition was not used as
1102 such. */
1104 static int
1105 swap_rtx_condition_1 (rtx pat)
1107 const char *fmt;
1108 int i, r = 0;
1110 if (COMPARISON_P (pat))
1112 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1113 r = 1;
1115 else
1117 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1118 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1120 if (fmt[i] == 'E')
1122 int j;
1124 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1125 r |= swap_rtx_condition_1 (XVECEXP (pat, i, j));
1127 else if (fmt[i] == 'e')
1128 r |= swap_rtx_condition_1 (XEXP (pat, i));
1132 return r;
1135 static int
1136 swap_rtx_condition (rtx_insn *insn)
1138 rtx pat = PATTERN (insn);
1140 /* We're looking for a single set to cc0 or an HImode temporary. */
1142 if (GET_CODE (pat) == SET
1143 && REG_P (SET_DEST (pat))
1144 && REGNO (SET_DEST (pat)) == FLAGS_REG)
1146 insn = next_flags_user (insn);
1147 if (insn == NULL_RTX)
1148 return 0;
1149 pat = PATTERN (insn);
1152 /* See if this is, or ends in, a fnstsw. If so, we're not doing anything
1153 with the cc value right now. We may be able to search for one
1154 though. */
1156 if (GET_CODE (pat) == SET
1157 && GET_CODE (SET_SRC (pat)) == UNSPEC
1158 && XINT (SET_SRC (pat), 1) == UNSPEC_FNSTSW)
1160 rtx dest = SET_DEST (pat);
1162 /* Search forward looking for the first use of this value.
1163 Stop at block boundaries. */
1164 while (insn != BB_END (current_block))
1166 insn = NEXT_INSN (insn);
1167 if (INSN_P (insn) && reg_mentioned_p (dest, insn))
1168 break;
1169 if (CALL_P (insn))
1170 return 0;
1173 /* We haven't found it. */
1174 if (insn == BB_END (current_block))
1175 return 0;
1177 /* So we've found the insn using this value. If it is anything
1178 other than sahf or the value does not die (meaning we'd have
1179 to search further), then we must give up. */
1180 pat = PATTERN (insn);
1181 if (GET_CODE (pat) != SET
1182 || GET_CODE (SET_SRC (pat)) != UNSPEC
1183 || XINT (SET_SRC (pat), 1) != UNSPEC_SAHF
1184 || ! dead_or_set_p (insn, dest))
1185 return 0;
1187 /* Now we are prepared to handle this as a normal cc0 setter. */
1188 insn = next_flags_user (insn);
1189 if (insn == NULL_RTX)
1190 return 0;
1191 pat = PATTERN (insn);
1194 if (swap_rtx_condition_1 (pat))
1196 int fail = 0;
1197 INSN_CODE (insn) = -1;
1198 if (recog_memoized (insn) == -1)
1199 fail = 1;
1200 /* In case the flags don't die here, recurse to try fix
1201 following user too. */
1202 else if (! dead_or_set_p (insn, ix86_flags_rtx))
1204 insn = next_flags_user (insn);
1205 if (!insn || !swap_rtx_condition (insn))
1206 fail = 1;
1208 if (fail)
1210 swap_rtx_condition_1 (pat);
1211 return 0;
1213 return 1;
1215 return 0;
1218 /* Handle a comparison. Special care needs to be taken to avoid
1219 causing comparisons that a 387 cannot do correctly, such as EQ.
1221 Also, a pop insn may need to be emitted. The 387 does have an
1222 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1223 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1224 set up. */
1226 static void
1227 compare_for_stack_reg (rtx_insn *insn, stack_ptr regstack, rtx pat_src)
1229 rtx *src1, *src2;
1230 rtx src1_note, src2_note;
1232 src1 = get_true_reg (&XEXP (pat_src, 0));
1233 src2 = get_true_reg (&XEXP (pat_src, 1));
1235 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1236 registers that die in this insn - move those to stack top first. */
1237 if ((! STACK_REG_P (*src1)
1238 || (STACK_REG_P (*src2)
1239 && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
1240 && swap_rtx_condition (insn))
1242 std::swap (XEXP (pat_src, 0), XEXP (pat_src, 1));
1244 src1 = get_true_reg (&XEXP (pat_src, 0));
1245 src2 = get_true_reg (&XEXP (pat_src, 1));
1247 INSN_CODE (insn) = -1;
1250 /* We will fix any death note later. */
1252 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1254 if (STACK_REG_P (*src2))
1255 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1256 else
1257 src2_note = NULL_RTX;
1259 emit_swap_insn (insn, regstack, *src1);
1261 replace_reg (src1, FIRST_STACK_REG);
1263 if (STACK_REG_P (*src2))
1264 replace_reg (src2, get_hard_regnum (regstack, *src2));
1266 if (src1_note)
1268 pop_stack (regstack, REGNO (XEXP (src1_note, 0)));
1269 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1272 /* If the second operand dies, handle that. But if the operands are
1273 the same stack register, don't bother, because only one death is
1274 needed, and it was just handled. */
1276 if (src2_note
1277 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
1278 && REGNO (*src1) == REGNO (*src2)))
1280 /* As a special case, two regs may die in this insn if src2 is
1281 next to top of stack and the top of stack also dies. Since
1282 we have already popped src1, "next to top of stack" is really
1283 at top (FIRST_STACK_REG) now. */
1285 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1286 && src1_note)
1288 pop_stack (regstack, REGNO (XEXP (src2_note, 0)));
1289 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1291 else
1293 /* The 386 can only represent death of the first operand in
1294 the case handled above. In all other cases, emit a separate
1295 pop and remove the death note from here. */
1296 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
1297 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
1298 EMIT_AFTER);
1303 /* Substitute hardware stack regs in debug insn INSN, using stack
1304 layout REGSTACK. If we can't find a hardware stack reg for any of
1305 the REGs in it, reset the debug insn. */
1307 static void
1308 subst_all_stack_regs_in_debug_insn (rtx_insn *insn, struct stack_def *regstack)
1310 subrtx_ptr_iterator::array_type array;
1311 FOR_EACH_SUBRTX_PTR (iter, array, &INSN_VAR_LOCATION_LOC (insn), NONCONST)
1313 rtx *loc = *iter;
1314 rtx x = *loc;
1315 if (STACK_REG_P (x))
1317 int hard_regno = get_hard_regnum (regstack, x);
1319 /* If we can't find an active register, reset this debug insn. */
1320 if (hard_regno == -1)
1322 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
1323 return;
1326 gcc_assert (hard_regno >= FIRST_STACK_REG);
1327 replace_reg (loc, hard_regno);
1328 iter.skip_subrtxes ();
1333 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1334 is the current register layout. Return whether a control flow insn
1335 was deleted in the process. */
1337 static bool
1338 subst_stack_regs_pat (rtx_insn *insn, stack_ptr regstack, rtx pat)
1340 rtx *dest, *src;
1341 bool control_flow_insn_deleted = false;
1343 switch (GET_CODE (pat))
1345 case USE:
1346 /* Deaths in USE insns can happen in non optimizing compilation.
1347 Handle them by popping the dying register. */
1348 src = get_true_reg (&XEXP (pat, 0));
1349 if (STACK_REG_P (*src)
1350 && find_regno_note (insn, REG_DEAD, REGNO (*src)))
1352 /* USEs are ignored for liveness information so USEs of dead
1353 register might happen. */
1354 if (TEST_HARD_REG_BIT (regstack->reg_set, REGNO (*src)))
1355 emit_pop_insn (insn, regstack, *src, EMIT_AFTER);
1356 return control_flow_insn_deleted;
1358 /* Uninitialized USE might happen for functions returning uninitialized
1359 value. We will properly initialize the USE on the edge to EXIT_BLOCK,
1360 so it is safe to ignore the use here. This is consistent with behavior
1361 of dataflow analyzer that ignores USE too. (This also imply that
1362 forcibly initializing the register to NaN here would lead to ICE later,
1363 since the REG_DEAD notes are not issued.) */
1364 break;
1366 case VAR_LOCATION:
1367 gcc_unreachable ();
1369 case CLOBBER:
1371 rtx note;
1373 dest = get_true_reg (&XEXP (pat, 0));
1374 if (STACK_REG_P (*dest))
1376 note = find_reg_note (insn, REG_DEAD, *dest);
1378 if (pat != PATTERN (insn))
1380 /* The fix_truncdi_1 pattern wants to be able to
1381 allocate its own scratch register. It does this by
1382 clobbering an fp reg so that it is assured of an
1383 empty reg-stack register. If the register is live,
1384 kill it now. Remove the DEAD/UNUSED note so we
1385 don't try to kill it later too.
1387 In reality the UNUSED note can be absent in some
1388 complicated cases when the register is reused for
1389 partially set variable. */
1391 if (note)
1392 emit_pop_insn (insn, regstack, *dest, EMIT_BEFORE);
1393 else
1394 note = find_reg_note (insn, REG_UNUSED, *dest);
1395 if (note)
1396 remove_note (insn, note);
1397 replace_reg (dest, FIRST_STACK_REG + 1);
1399 else
1401 /* A top-level clobber with no REG_DEAD, and no hard-regnum
1402 indicates an uninitialized value. Because reload removed
1403 all other clobbers, this must be due to a function
1404 returning without a value. Load up a NaN. */
1406 if (!note)
1408 rtx t = *dest;
1409 if (COMPLEX_MODE_P (GET_MODE (t)))
1411 rtx u = FP_MODE_REG (REGNO (t) + 1, SFmode);
1412 if (get_hard_regnum (regstack, u) == -1)
1414 rtx pat2 = gen_rtx_CLOBBER (VOIDmode, u);
1415 rtx_insn *insn2 = emit_insn_before (pat2, insn);
1416 control_flow_insn_deleted
1417 |= move_nan_for_stack_reg (insn2, regstack, u);
1420 if (get_hard_regnum (regstack, t) == -1)
1421 control_flow_insn_deleted
1422 |= move_nan_for_stack_reg (insn, regstack, t);
1426 break;
1429 case SET:
1431 rtx *src1 = (rtx *) 0, *src2;
1432 rtx src1_note, src2_note;
1433 rtx pat_src;
1435 dest = get_true_reg (&SET_DEST (pat));
1436 src = get_true_reg (&SET_SRC (pat));
1437 pat_src = SET_SRC (pat);
1439 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1440 if (STACK_REG_P (*src)
1441 || (STACK_REG_P (*dest)
1442 && (REG_P (*src) || MEM_P (*src)
1443 || CONST_DOUBLE_P (*src))))
1445 control_flow_insn_deleted |= move_for_stack_reg (insn, regstack, pat);
1446 break;
1449 switch (GET_CODE (pat_src))
1451 case COMPARE:
1452 compare_for_stack_reg (insn, regstack, pat_src);
1453 break;
1455 case CALL:
1457 int count;
1458 for (count = REG_NREGS (*dest); --count >= 0;)
1460 regstack->reg[++regstack->top] = REGNO (*dest) + count;
1461 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
1464 replace_reg (dest, FIRST_STACK_REG);
1465 break;
1467 case REG:
1468 /* This is a `tstM2' case. */
1469 gcc_assert (*dest == cc0_rtx);
1470 src1 = src;
1472 /* Fall through. */
1474 case FLOAT_TRUNCATE:
1475 case SQRT:
1476 case ABS:
1477 case NEG:
1478 /* These insns only operate on the top of the stack. DEST might
1479 be cc0_rtx if we're processing a tstM pattern. Also, it's
1480 possible that the tstM case results in a REG_DEAD note on the
1481 source. */
1483 if (src1 == 0)
1484 src1 = get_true_reg (&XEXP (pat_src, 0));
1486 emit_swap_insn (insn, regstack, *src1);
1488 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1490 if (STACK_REG_P (*dest))
1491 replace_reg (dest, FIRST_STACK_REG);
1493 if (src1_note)
1495 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1496 regstack->top--;
1497 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1500 replace_reg (src1, FIRST_STACK_REG);
1501 break;
1503 case MINUS:
1504 case DIV:
1505 /* On i386, reversed forms of subM3 and divM3 exist for
1506 MODE_FLOAT, so the same code that works for addM3 and mulM3
1507 can be used. */
1508 case MULT:
1509 case PLUS:
1510 /* These insns can accept the top of stack as a destination
1511 from a stack reg or mem, or can use the top of stack as a
1512 source and some other stack register (possibly top of stack)
1513 as a destination. */
1515 src1 = get_true_reg (&XEXP (pat_src, 0));
1516 src2 = get_true_reg (&XEXP (pat_src, 1));
1518 /* We will fix any death note later. */
1520 if (STACK_REG_P (*src1))
1521 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1522 else
1523 src1_note = NULL_RTX;
1524 if (STACK_REG_P (*src2))
1525 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1526 else
1527 src2_note = NULL_RTX;
1529 /* If either operand is not a stack register, then the dest
1530 must be top of stack. */
1532 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
1533 emit_swap_insn (insn, regstack, *dest);
1534 else
1536 /* Both operands are REG. If neither operand is already
1537 at the top of stack, choose to make the one that is the
1538 dest the new top of stack. */
1540 int src1_hard_regnum, src2_hard_regnum;
1542 src1_hard_regnum = get_hard_regnum (regstack, *src1);
1543 src2_hard_regnum = get_hard_regnum (regstack, *src2);
1545 /* If the source is not live, this is yet another case of
1546 uninitialized variables. Load up a NaN instead. */
1547 if (src1_hard_regnum == -1)
1549 rtx pat2 = gen_rtx_CLOBBER (VOIDmode, *src1);
1550 rtx_insn *insn2 = emit_insn_before (pat2, insn);
1551 control_flow_insn_deleted
1552 |= move_nan_for_stack_reg (insn2, regstack, *src1);
1554 if (src2_hard_regnum == -1)
1556 rtx pat2 = gen_rtx_CLOBBER (VOIDmode, *src2);
1557 rtx_insn *insn2 = emit_insn_before (pat2, insn);
1558 control_flow_insn_deleted
1559 |= move_nan_for_stack_reg (insn2, regstack, *src2);
1562 if (src1_hard_regnum != FIRST_STACK_REG
1563 && src2_hard_regnum != FIRST_STACK_REG)
1564 emit_swap_insn (insn, regstack, *dest);
1567 if (STACK_REG_P (*src1))
1568 replace_reg (src1, get_hard_regnum (regstack, *src1));
1569 if (STACK_REG_P (*src2))
1570 replace_reg (src2, get_hard_regnum (regstack, *src2));
1572 if (src1_note)
1574 rtx src1_reg = XEXP (src1_note, 0);
1576 /* If the register that dies is at the top of stack, then
1577 the destination is somewhere else - merely substitute it.
1578 But if the reg that dies is not at top of stack, then
1579 move the top of stack to the dead reg, as though we had
1580 done the insn and then a store-with-pop. */
1582 if (REGNO (src1_reg) == regstack->reg[regstack->top])
1584 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1585 replace_reg (dest, get_hard_regnum (regstack, *dest));
1587 else
1589 int regno = get_hard_regnum (regstack, src1_reg);
1591 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1592 replace_reg (dest, regno);
1594 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1595 = regstack->reg[regstack->top];
1598 CLEAR_HARD_REG_BIT (regstack->reg_set,
1599 REGNO (XEXP (src1_note, 0)));
1600 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1601 regstack->top--;
1603 else if (src2_note)
1605 rtx src2_reg = XEXP (src2_note, 0);
1606 if (REGNO (src2_reg) == regstack->reg[regstack->top])
1608 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1609 replace_reg (dest, get_hard_regnum (regstack, *dest));
1611 else
1613 int regno = get_hard_regnum (regstack, src2_reg);
1615 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1616 replace_reg (dest, regno);
1618 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1619 = regstack->reg[regstack->top];
1622 CLEAR_HARD_REG_BIT (regstack->reg_set,
1623 REGNO (XEXP (src2_note, 0)));
1624 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
1625 regstack->top--;
1627 else
1629 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1630 replace_reg (dest, get_hard_regnum (regstack, *dest));
1633 /* Keep operand 1 matching with destination. */
1634 if (COMMUTATIVE_ARITH_P (pat_src)
1635 && REG_P (*src1) && REG_P (*src2)
1636 && REGNO (*src1) != REGNO (*dest))
1638 int tmp = REGNO (*src1);
1639 replace_reg (src1, REGNO (*src2));
1640 replace_reg (src2, tmp);
1642 break;
1644 case UNSPEC:
1645 switch (XINT (pat_src, 1))
1647 case UNSPEC_FIST:
1648 case UNSPEC_FIST_ATOMIC:
1650 case UNSPEC_FIST_FLOOR:
1651 case UNSPEC_FIST_CEIL:
1653 /* These insns only operate on the top of the stack. */
1655 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1656 emit_swap_insn (insn, regstack, *src1);
1658 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1660 if (STACK_REG_P (*dest))
1661 replace_reg (dest, FIRST_STACK_REG);
1663 if (src1_note)
1665 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1666 regstack->top--;
1667 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1670 replace_reg (src1, FIRST_STACK_REG);
1671 break;
1673 case UNSPEC_FXAM:
1675 /* This insn only operate on the top of the stack. */
1677 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1678 emit_swap_insn (insn, regstack, *src1);
1680 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1682 replace_reg (src1, FIRST_STACK_REG);
1684 if (src1_note)
1686 remove_regno_note (insn, REG_DEAD,
1687 REGNO (XEXP (src1_note, 0)));
1688 emit_pop_insn (insn, regstack, XEXP (src1_note, 0),
1689 EMIT_AFTER);
1692 break;
1694 case UNSPEC_SIN:
1695 case UNSPEC_COS:
1696 case UNSPEC_FRNDINT:
1697 case UNSPEC_F2XM1:
1699 case UNSPEC_FRNDINT_FLOOR:
1700 case UNSPEC_FRNDINT_CEIL:
1701 case UNSPEC_FRNDINT_TRUNC:
1702 case UNSPEC_FRNDINT_MASK_PM:
1704 /* Above insns operate on the top of the stack. */
1706 case UNSPEC_SINCOS_COS:
1707 case UNSPEC_XTRACT_FRACT:
1709 /* Above insns operate on the top two stack slots,
1710 first part of one input, double output insn. */
1712 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1714 emit_swap_insn (insn, regstack, *src1);
1716 /* Input should never die, it is replaced with output. */
1717 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1718 gcc_assert (!src1_note);
1720 if (STACK_REG_P (*dest))
1721 replace_reg (dest, FIRST_STACK_REG);
1723 replace_reg (src1, FIRST_STACK_REG);
1724 break;
1726 case UNSPEC_SINCOS_SIN:
1727 case UNSPEC_XTRACT_EXP:
1729 /* These insns operate on the top two stack slots,
1730 second part of one input, double output insn. */
1732 regstack->top++;
1733 /* FALLTHRU */
1735 case UNSPEC_TAN:
1737 /* For UNSPEC_TAN, regstack->top is already increased
1738 by inherent load of constant 1.0. */
1740 /* Output value is generated in the second stack slot.
1741 Move current value from second slot to the top. */
1742 regstack->reg[regstack->top]
1743 = regstack->reg[regstack->top - 1];
1745 gcc_assert (STACK_REG_P (*dest));
1747 regstack->reg[regstack->top - 1] = REGNO (*dest);
1748 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1749 replace_reg (dest, FIRST_STACK_REG + 1);
1751 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1753 replace_reg (src1, FIRST_STACK_REG);
1754 break;
1756 case UNSPEC_FPATAN:
1757 case UNSPEC_FYL2X:
1758 case UNSPEC_FYL2XP1:
1759 /* These insns operate on the top two stack slots. */
1761 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1762 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1764 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1765 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1767 swap_to_top (insn, regstack, *src1, *src2);
1769 replace_reg (src1, FIRST_STACK_REG);
1770 replace_reg (src2, FIRST_STACK_REG + 1);
1772 if (src1_note)
1773 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1774 if (src2_note)
1775 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1777 /* Pop both input operands from the stack. */
1778 CLEAR_HARD_REG_BIT (regstack->reg_set,
1779 regstack->reg[regstack->top]);
1780 CLEAR_HARD_REG_BIT (regstack->reg_set,
1781 regstack->reg[regstack->top - 1]);
1782 regstack->top -= 2;
1784 /* Push the result back onto the stack. */
1785 regstack->reg[++regstack->top] = REGNO (*dest);
1786 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1787 replace_reg (dest, FIRST_STACK_REG);
1788 break;
1790 case UNSPEC_FSCALE_FRACT:
1791 case UNSPEC_FPREM_F:
1792 case UNSPEC_FPREM1_F:
1793 /* These insns operate on the top two stack slots,
1794 first part of double input, double output insn. */
1796 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1797 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1799 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1800 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1802 /* Inputs should never die, they are
1803 replaced with outputs. */
1804 gcc_assert (!src1_note);
1805 gcc_assert (!src2_note);
1807 swap_to_top (insn, regstack, *src1, *src2);
1809 /* Push the result back onto stack. Empty stack slot
1810 will be filled in second part of insn. */
1811 if (STACK_REG_P (*dest))
1813 regstack->reg[regstack->top] = REGNO (*dest);
1814 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1815 replace_reg (dest, FIRST_STACK_REG);
1818 replace_reg (src1, FIRST_STACK_REG);
1819 replace_reg (src2, FIRST_STACK_REG + 1);
1820 break;
1822 case UNSPEC_FSCALE_EXP:
1823 case UNSPEC_FPREM_U:
1824 case UNSPEC_FPREM1_U:
1825 /* These insns operate on the top two stack slots,
1826 second part of double input, double output insn. */
1828 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1829 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1831 /* Push the result back onto stack. Fill empty slot from
1832 first part of insn and fix top of stack pointer. */
1833 if (STACK_REG_P (*dest))
1835 regstack->reg[regstack->top - 1] = REGNO (*dest);
1836 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1837 replace_reg (dest, FIRST_STACK_REG + 1);
1840 replace_reg (src1, FIRST_STACK_REG);
1841 replace_reg (src2, FIRST_STACK_REG + 1);
1842 break;
1844 case UNSPEC_C2_FLAG:
1845 /* This insn operates on the top two stack slots,
1846 third part of C2 setting double input insn. */
1848 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1849 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1851 replace_reg (src1, FIRST_STACK_REG);
1852 replace_reg (src2, FIRST_STACK_REG + 1);
1853 break;
1855 case UNSPEC_SAHF:
1856 /* (unspec [(unspec [(compare)] UNSPEC_FNSTSW)] UNSPEC_SAHF)
1857 The combination matches the PPRO fcomi instruction. */
1859 pat_src = XVECEXP (pat_src, 0, 0);
1860 gcc_assert (GET_CODE (pat_src) == UNSPEC);
1861 gcc_assert (XINT (pat_src, 1) == UNSPEC_FNSTSW);
1862 /* Fall through. */
1864 case UNSPEC_FNSTSW:
1865 /* Combined fcomp+fnstsw generated for doing well with
1866 CSE. When optimizing this would have been broken
1867 up before now. */
1869 pat_src = XVECEXP (pat_src, 0, 0);
1870 gcc_assert (GET_CODE (pat_src) == COMPARE);
1872 compare_for_stack_reg (insn, regstack, pat_src);
1873 break;
1875 default:
1876 gcc_unreachable ();
1878 break;
1880 case IF_THEN_ELSE:
1881 /* This insn requires the top of stack to be the destination. */
1883 src1 = get_true_reg (&XEXP (pat_src, 1));
1884 src2 = get_true_reg (&XEXP (pat_src, 2));
1886 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1887 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1889 /* If the comparison operator is an FP comparison operator,
1890 it is handled correctly by compare_for_stack_reg () who
1891 will move the destination to the top of stack. But if the
1892 comparison operator is not an FP comparison operator, we
1893 have to handle it here. */
1894 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
1895 && REGNO (*dest) != regstack->reg[regstack->top])
1897 /* In case one of operands is the top of stack and the operands
1898 dies, it is safe to make it the destination operand by
1899 reversing the direction of cmove and avoid fxch. */
1900 if ((REGNO (*src1) == regstack->reg[regstack->top]
1901 && src1_note)
1902 || (REGNO (*src2) == regstack->reg[regstack->top]
1903 && src2_note))
1905 int idx1 = (get_hard_regnum (regstack, *src1)
1906 - FIRST_STACK_REG);
1907 int idx2 = (get_hard_regnum (regstack, *src2)
1908 - FIRST_STACK_REG);
1910 /* Make reg-stack believe that the operands are already
1911 swapped on the stack */
1912 regstack->reg[regstack->top - idx1] = REGNO (*src2);
1913 regstack->reg[regstack->top - idx2] = REGNO (*src1);
1915 /* Reverse condition to compensate the operand swap.
1916 i386 do have comparison always reversible. */
1917 PUT_CODE (XEXP (pat_src, 0),
1918 reversed_comparison_code (XEXP (pat_src, 0), insn));
1920 else
1921 emit_swap_insn (insn, regstack, *dest);
1925 rtx src_note [3];
1926 int i;
1928 src_note[0] = 0;
1929 src_note[1] = src1_note;
1930 src_note[2] = src2_note;
1932 if (STACK_REG_P (*src1))
1933 replace_reg (src1, get_hard_regnum (regstack, *src1));
1934 if (STACK_REG_P (*src2))
1935 replace_reg (src2, get_hard_regnum (regstack, *src2));
1937 for (i = 1; i <= 2; i++)
1938 if (src_note [i])
1940 int regno = REGNO (XEXP (src_note[i], 0));
1942 /* If the register that dies is not at the top of
1943 stack, then move the top of stack to the dead reg.
1944 Top of stack should never die, as it is the
1945 destination. */
1946 gcc_assert (regno != regstack->reg[regstack->top]);
1947 remove_regno_note (insn, REG_DEAD, regno);
1948 emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
1949 EMIT_AFTER);
1953 /* Make dest the top of stack. Add dest to regstack if
1954 not present. */
1955 if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
1956 regstack->reg[++regstack->top] = REGNO (*dest);
1957 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1958 replace_reg (dest, FIRST_STACK_REG);
1959 break;
1961 default:
1962 gcc_unreachable ();
1964 break;
1967 default:
1968 break;
1971 return control_flow_insn_deleted;
1974 /* Substitute hard regnums for any stack regs in INSN, which has
1975 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
1976 before the insn, and is updated with changes made here.
1978 There are several requirements and assumptions about the use of
1979 stack-like regs in asm statements. These rules are enforced by
1980 record_asm_stack_regs; see comments there for details. Any
1981 asm_operands left in the RTL at this point may be assume to meet the
1982 requirements, since record_asm_stack_regs removes any problem asm. */
1984 static void
1985 subst_asm_stack_regs (rtx_insn *insn, stack_ptr regstack)
1987 rtx body = PATTERN (insn);
1989 rtx *note_reg; /* Array of note contents */
1990 rtx **note_loc; /* Address of REG field of each note */
1991 enum reg_note *note_kind; /* The type of each note */
1993 rtx *clobber_reg = 0;
1994 rtx **clobber_loc = 0;
1996 struct stack_def temp_stack;
1997 int n_notes;
1998 int n_clobbers;
1999 rtx note;
2000 int i;
2001 int n_inputs, n_outputs;
2003 if (! check_asm_stack_operands (insn))
2004 return;
2006 /* Find out what the constraints required. If no constraint
2007 alternative matches, that is a compiler bug: we should have caught
2008 such an insn in check_asm_stack_operands. */
2009 extract_constrain_insn (insn);
2011 preprocess_constraints (insn);
2012 const operand_alternative *op_alt = which_op_alt ();
2014 get_asm_operands_in_out (body, &n_outputs, &n_inputs);
2016 /* Strip SUBREGs here to make the following code simpler. */
2017 for (i = 0; i < recog_data.n_operands; i++)
2018 if (GET_CODE (recog_data.operand[i]) == SUBREG
2019 && REG_P (SUBREG_REG (recog_data.operand[i])))
2021 recog_data.operand_loc[i] = & SUBREG_REG (recog_data.operand[i]);
2022 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
2025 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
2027 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
2028 i++;
2030 note_reg = XALLOCAVEC (rtx, i);
2031 note_loc = XALLOCAVEC (rtx *, i);
2032 note_kind = XALLOCAVEC (enum reg_note, i);
2034 n_notes = 0;
2035 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2037 if (GET_CODE (note) != EXPR_LIST)
2038 continue;
2039 rtx reg = XEXP (note, 0);
2040 rtx *loc = & XEXP (note, 0);
2042 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
2044 loc = & SUBREG_REG (reg);
2045 reg = SUBREG_REG (reg);
2048 if (STACK_REG_P (reg)
2049 && (REG_NOTE_KIND (note) == REG_DEAD
2050 || REG_NOTE_KIND (note) == REG_UNUSED))
2052 note_reg[n_notes] = reg;
2053 note_loc[n_notes] = loc;
2054 note_kind[n_notes] = REG_NOTE_KIND (note);
2055 n_notes++;
2059 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2061 n_clobbers = 0;
2063 if (GET_CODE (body) == PARALLEL)
2065 clobber_reg = XALLOCAVEC (rtx, XVECLEN (body, 0));
2066 clobber_loc = XALLOCAVEC (rtx *, XVECLEN (body, 0));
2068 for (i = 0; i < XVECLEN (body, 0); i++)
2069 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
2071 rtx clobber = XVECEXP (body, 0, i);
2072 rtx reg = XEXP (clobber, 0);
2073 rtx *loc = & XEXP (clobber, 0);
2075 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
2077 loc = & SUBREG_REG (reg);
2078 reg = SUBREG_REG (reg);
2081 if (STACK_REG_P (reg))
2083 clobber_reg[n_clobbers] = reg;
2084 clobber_loc[n_clobbers] = loc;
2085 n_clobbers++;
2090 temp_stack = *regstack;
2092 /* Put the input regs into the desired place in TEMP_STACK. */
2094 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2095 if (STACK_REG_P (recog_data.operand[i])
2096 && reg_class_subset_p (op_alt[i].cl, FLOAT_REGS)
2097 && op_alt[i].cl != FLOAT_REGS)
2099 /* If an operand needs to be in a particular reg in
2100 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2101 these constraints are for single register classes, and
2102 reload guaranteed that operand[i] is already in that class,
2103 we can just use REGNO (recog_data.operand[i]) to know which
2104 actual reg this operand needs to be in. */
2106 int regno = get_hard_regnum (&temp_stack, recog_data.operand[i]);
2108 gcc_assert (regno >= 0);
2110 if ((unsigned int) regno != REGNO (recog_data.operand[i]))
2112 /* recog_data.operand[i] is not in the right place. Find
2113 it and swap it with whatever is already in I's place.
2114 K is where recog_data.operand[i] is now. J is where it
2115 should be. */
2116 int j, k;
2118 k = temp_stack.top - (regno - FIRST_STACK_REG);
2119 j = (temp_stack.top
2120 - (REGNO (recog_data.operand[i]) - FIRST_STACK_REG));
2122 std::swap (temp_stack.reg[j], temp_stack.reg[k]);
2126 /* Emit insns before INSN to make sure the reg-stack is in the right
2127 order. */
2129 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
2131 /* Make the needed input register substitutions. Do death notes and
2132 clobbers too, because these are for inputs, not outputs. */
2134 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2135 if (STACK_REG_P (recog_data.operand[i]))
2137 int regnum = get_hard_regnum (regstack, recog_data.operand[i]);
2139 gcc_assert (regnum >= 0);
2141 replace_reg (recog_data.operand_loc[i], regnum);
2144 for (i = 0; i < n_notes; i++)
2145 if (note_kind[i] == REG_DEAD)
2147 int regnum = get_hard_regnum (regstack, note_reg[i]);
2149 gcc_assert (regnum >= 0);
2151 replace_reg (note_loc[i], regnum);
2154 for (i = 0; i < n_clobbers; i++)
2156 /* It's OK for a CLOBBER to reference a reg that is not live.
2157 Don't try to replace it in that case. */
2158 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2160 if (regnum >= 0)
2162 /* Sigh - clobbers always have QImode. But replace_reg knows
2163 that these regs can't be MODE_INT and will assert. Just put
2164 the right reg there without calling replace_reg. */
2166 *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
2170 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2172 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2173 if (STACK_REG_P (recog_data.operand[i]))
2175 /* An input reg is implicitly popped if it is tied to an
2176 output, or if there is a CLOBBER for it. */
2177 int j;
2179 for (j = 0; j < n_clobbers; j++)
2180 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
2181 break;
2183 if (j < n_clobbers || op_alt[i].matches >= 0)
2185 /* recog_data.operand[i] might not be at the top of stack.
2186 But that's OK, because all we need to do is pop the
2187 right number of regs off of the top of the reg-stack.
2188 record_asm_stack_regs guaranteed that all implicitly
2189 popped regs were grouped at the top of the reg-stack. */
2191 CLEAR_HARD_REG_BIT (regstack->reg_set,
2192 regstack->reg[regstack->top]);
2193 regstack->top--;
2197 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2198 Note that there isn't any need to substitute register numbers.
2199 ??? Explain why this is true. */
2201 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2203 /* See if there is an output for this hard reg. */
2204 int j;
2206 for (j = 0; j < n_outputs; j++)
2207 if (STACK_REG_P (recog_data.operand[j])
2208 && REGNO (recog_data.operand[j]) == (unsigned) i)
2210 regstack->reg[++regstack->top] = i;
2211 SET_HARD_REG_BIT (regstack->reg_set, i);
2212 break;
2216 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2217 input that the asm didn't implicitly pop. If the asm didn't
2218 implicitly pop an input reg, that reg will still be live.
2220 Note that we can't use find_regno_note here: the register numbers
2221 in the death notes have already been substituted. */
2223 for (i = 0; i < n_outputs; i++)
2224 if (STACK_REG_P (recog_data.operand[i]))
2226 int j;
2228 for (j = 0; j < n_notes; j++)
2229 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2230 && note_kind[j] == REG_UNUSED)
2232 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2233 EMIT_AFTER);
2234 break;
2238 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2239 if (STACK_REG_P (recog_data.operand[i]))
2241 int j;
2243 for (j = 0; j < n_notes; j++)
2244 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2245 && note_kind[j] == REG_DEAD
2246 && TEST_HARD_REG_BIT (regstack->reg_set,
2247 REGNO (recog_data.operand[i])))
2249 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2250 EMIT_AFTER);
2251 break;
2256 /* Substitute stack hard reg numbers for stack virtual registers in
2257 INSN. Non-stack register numbers are not changed. REGSTACK is the
2258 current stack content. Insns may be emitted as needed to arrange the
2259 stack for the 387 based on the contents of the insn. Return whether
2260 a control flow insn was deleted in the process. */
2262 static bool
2263 subst_stack_regs (rtx_insn *insn, stack_ptr regstack)
2265 rtx *note_link, note;
2266 bool control_flow_insn_deleted = false;
2267 int i;
2269 if (CALL_P (insn))
2271 int top = regstack->top;
2273 /* If there are any floating point parameters to be passed in
2274 registers for this call, make sure they are in the right
2275 order. */
2277 if (top >= 0)
2279 straighten_stack (insn, regstack);
2281 /* Now mark the arguments as dead after the call. */
2283 while (regstack->top >= 0)
2285 CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
2286 regstack->top--;
2291 /* Do the actual substitution if any stack regs are mentioned.
2292 Since we only record whether entire insn mentions stack regs, and
2293 subst_stack_regs_pat only works for patterns that contain stack regs,
2294 we must check each pattern in a parallel here. A call_value_pop could
2295 fail otherwise. */
2297 if (stack_regs_mentioned (insn))
2299 int n_operands = asm_noperands (PATTERN (insn));
2300 if (n_operands >= 0)
2302 /* This insn is an `asm' with operands. Decode the operands,
2303 decide how many are inputs, and do register substitution.
2304 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2306 subst_asm_stack_regs (insn, regstack);
2307 return control_flow_insn_deleted;
2310 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2311 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2313 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2315 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
2316 XVECEXP (PATTERN (insn), 0, i)
2317 = shallow_copy_rtx (XVECEXP (PATTERN (insn), 0, i));
2318 control_flow_insn_deleted
2319 |= subst_stack_regs_pat (insn, regstack,
2320 XVECEXP (PATTERN (insn), 0, i));
2323 else
2324 control_flow_insn_deleted
2325 |= subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2328 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2329 REG_UNUSED will already have been dealt with, so just return. */
2331 if (NOTE_P (insn) || insn->deleted ())
2332 return control_flow_insn_deleted;
2334 /* If this a noreturn call, we can't insert pop insns after it.
2335 Instead, reset the stack state to empty. */
2336 if (CALL_P (insn)
2337 && find_reg_note (insn, REG_NORETURN, NULL))
2339 regstack->top = -1;
2340 CLEAR_HARD_REG_SET (regstack->reg_set);
2341 return control_flow_insn_deleted;
2344 /* If there is a REG_UNUSED note on a stack register on this insn,
2345 the indicated reg must be popped. The REG_UNUSED note is removed,
2346 since the form of the newly emitted pop insn references the reg,
2347 making it no longer `unset'. */
2349 note_link = &REG_NOTES (insn);
2350 for (note = *note_link; note; note = XEXP (note, 1))
2351 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2353 *note_link = XEXP (note, 1);
2354 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), EMIT_AFTER);
2356 else
2357 note_link = &XEXP (note, 1);
2359 return control_flow_insn_deleted;
2362 /* Change the organization of the stack so that it fits a new basic
2363 block. Some registers might have to be popped, but there can never be
2364 a register live in the new block that is not now live.
2366 Insert any needed insns before or after INSN, as indicated by
2367 WHERE. OLD is the original stack layout, and NEW is the desired
2368 form. OLD is updated to reflect the code emitted, i.e., it will be
2369 the same as NEW upon return.
2371 This function will not preserve block_end[]. But that information
2372 is no longer needed once this has executed. */
2374 static void
2375 change_stack (rtx_insn *insn, stack_ptr old, stack_ptr new_stack,
2376 enum emit_where where)
2378 int reg;
2379 int update_end = 0;
2380 int i;
2382 /* Stack adjustments for the first insn in a block update the
2383 current_block's stack_in instead of inserting insns directly.
2384 compensate_edges will add the necessary code later. */
2385 if (current_block
2386 && starting_stack_p
2387 && where == EMIT_BEFORE)
2389 BLOCK_INFO (current_block)->stack_in = *new_stack;
2390 starting_stack_p = false;
2391 *old = *new_stack;
2392 return;
2395 /* We will be inserting new insns "backwards". If we are to insert
2396 after INSN, find the next insn, and insert before it. */
2398 if (where == EMIT_AFTER)
2400 if (current_block && BB_END (current_block) == insn)
2401 update_end = 1;
2402 insn = NEXT_INSN (insn);
2405 /* Initialize partially dead variables. */
2406 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
2407 if (TEST_HARD_REG_BIT (new_stack->reg_set, i)
2408 && !TEST_HARD_REG_BIT (old->reg_set, i))
2410 old->reg[++old->top] = i;
2411 SET_HARD_REG_BIT (old->reg_set, i);
2412 emit_insn_before (gen_rtx_SET (FP_MODE_REG (i, SFmode), not_a_num),
2413 insn);
2416 /* Pop any registers that are not needed in the new block. */
2418 /* If the destination block's stack already has a specified layout
2419 and contains two or more registers, use a more intelligent algorithm
2420 to pop registers that minimizes the number of fxchs below. */
2421 if (new_stack->top > 0)
2423 bool slots[REG_STACK_SIZE];
2424 int pops[REG_STACK_SIZE];
2425 int next, dest, topsrc;
2427 /* First pass to determine the free slots. */
2428 for (reg = 0; reg <= new_stack->top; reg++)
2429 slots[reg] = TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[reg]);
2431 /* Second pass to allocate preferred slots. */
2432 topsrc = -1;
2433 for (reg = old->top; reg > new_stack->top; reg--)
2434 if (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[reg]))
2436 dest = -1;
2437 for (next = 0; next <= new_stack->top; next++)
2438 if (!slots[next] && new_stack->reg[next] == old->reg[reg])
2440 /* If this is a preference for the new top of stack, record
2441 the fact by remembering it's old->reg in topsrc. */
2442 if (next == new_stack->top)
2443 topsrc = reg;
2444 slots[next] = true;
2445 dest = next;
2446 break;
2448 pops[reg] = dest;
2450 else
2451 pops[reg] = reg;
2453 /* Intentionally, avoid placing the top of stack in it's correct
2454 location, if we still need to permute the stack below and we
2455 can usefully place it somewhere else. This is the case if any
2456 slot is still unallocated, in which case we should place the
2457 top of stack there. */
2458 if (topsrc != -1)
2459 for (reg = 0; reg < new_stack->top; reg++)
2460 if (!slots[reg])
2462 pops[topsrc] = reg;
2463 slots[new_stack->top] = false;
2464 slots[reg] = true;
2465 break;
2468 /* Third pass allocates remaining slots and emits pop insns. */
2469 next = new_stack->top;
2470 for (reg = old->top; reg > new_stack->top; reg--)
2472 dest = pops[reg];
2473 if (dest == -1)
2475 /* Find next free slot. */
2476 while (slots[next])
2477 next--;
2478 dest = next--;
2480 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[dest], DFmode),
2481 EMIT_BEFORE);
2484 else
2486 /* The following loop attempts to maximize the number of times we
2487 pop the top of the stack, as this permits the use of the faster
2488 ffreep instruction on platforms that support it. */
2489 int live, next;
2491 live = 0;
2492 for (reg = 0; reg <= old->top; reg++)
2493 if (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[reg]))
2494 live++;
2496 next = live;
2497 while (old->top >= live)
2498 if (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[old->top]))
2500 while (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[next]))
2501 next--;
2502 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[next], DFmode),
2503 EMIT_BEFORE);
2505 else
2506 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[old->top], DFmode),
2507 EMIT_BEFORE);
2510 if (new_stack->top == -2)
2512 /* If the new block has never been processed, then it can inherit
2513 the old stack order. */
2515 new_stack->top = old->top;
2516 memcpy (new_stack->reg, old->reg, sizeof (new_stack->reg));
2518 else
2520 /* This block has been entered before, and we must match the
2521 previously selected stack order. */
2523 /* By now, the only difference should be the order of the stack,
2524 not their depth or liveliness. */
2526 gcc_assert (hard_reg_set_equal_p (old->reg_set, new_stack->reg_set));
2527 gcc_assert (old->top == new_stack->top);
2529 /* If the stack is not empty (new_stack->top != -1), loop here emitting
2530 swaps until the stack is correct.
2532 The worst case number of swaps emitted is N + 2, where N is the
2533 depth of the stack. In some cases, the reg at the top of
2534 stack may be correct, but swapped anyway in order to fix
2535 other regs. But since we never swap any other reg away from
2536 its correct slot, this algorithm will converge. */
2538 if (new_stack->top != -1)
2541 /* Swap the reg at top of stack into the position it is
2542 supposed to be in, until the correct top of stack appears. */
2544 while (old->reg[old->top] != new_stack->reg[new_stack->top])
2546 for (reg = new_stack->top; reg >= 0; reg--)
2547 if (new_stack->reg[reg] == old->reg[old->top])
2548 break;
2550 gcc_assert (reg != -1);
2552 emit_swap_insn (insn, old,
2553 FP_MODE_REG (old->reg[reg], DFmode));
2556 /* See if any regs remain incorrect. If so, bring an
2557 incorrect reg to the top of stack, and let the while loop
2558 above fix it. */
2560 for (reg = new_stack->top; reg >= 0; reg--)
2561 if (new_stack->reg[reg] != old->reg[reg])
2563 emit_swap_insn (insn, old,
2564 FP_MODE_REG (old->reg[reg], DFmode));
2565 break;
2567 } while (reg >= 0);
2569 /* At this point there must be no differences. */
2571 for (reg = old->top; reg >= 0; reg--)
2572 gcc_assert (old->reg[reg] == new_stack->reg[reg]);
2575 if (update_end)
2576 BB_END (current_block) = PREV_INSN (insn);
2579 /* Print stack configuration. */
2581 static void
2582 print_stack (FILE *file, stack_ptr s)
2584 if (! file)
2585 return;
2587 if (s->top == -2)
2588 fprintf (file, "uninitialized\n");
2589 else if (s->top == -1)
2590 fprintf (file, "empty\n");
2591 else
2593 int i;
2594 fputs ("[ ", file);
2595 for (i = 0; i <= s->top; ++i)
2596 fprintf (file, "%d ", s->reg[i]);
2597 fputs ("]\n", file);
2601 /* This function was doing life analysis. We now let the regular live
2602 code do it's job, so we only need to check some extra invariants
2603 that reg-stack expects. Primary among these being that all registers
2604 are initialized before use.
2606 The function returns true when code was emitted to CFG edges and
2607 commit_edge_insertions needs to be called. */
2609 static int
2610 convert_regs_entry (void)
2612 int inserted = 0;
2613 edge e;
2614 edge_iterator ei;
2616 /* Load something into each stack register live at function entry.
2617 Such live registers can be caused by uninitialized variables or
2618 functions not returning values on all paths. In order to keep
2619 the push/pop code happy, and to not scrog the register stack, we
2620 must put something in these registers. Use a QNaN.
2622 Note that we are inserting converted code here. This code is
2623 never seen by the convert_regs pass. */
2625 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
2627 basic_block block = e->dest;
2628 block_info bi = BLOCK_INFO (block);
2629 int reg, top = -1;
2631 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2632 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2634 rtx init;
2636 bi->stack_in.reg[++top] = reg;
2638 init = gen_rtx_SET (FP_MODE_REG (FIRST_STACK_REG, SFmode),
2639 not_a_num);
2640 insert_insn_on_edge (init, e);
2641 inserted = 1;
2644 bi->stack_in.top = top;
2647 return inserted;
2650 /* Construct the desired stack for function exit. This will either
2651 be `empty', or the function return value at top-of-stack. */
2653 static void
2654 convert_regs_exit (void)
2656 int value_reg_low, value_reg_high;
2657 stack_ptr output_stack;
2658 rtx retvalue;
2660 retvalue = stack_result (current_function_decl);
2661 value_reg_low = value_reg_high = -1;
2662 if (retvalue)
2664 value_reg_low = REGNO (retvalue);
2665 value_reg_high = END_REGNO (retvalue) - 1;
2668 output_stack = &BLOCK_INFO (EXIT_BLOCK_PTR_FOR_FN (cfun))->stack_in;
2669 if (value_reg_low == -1)
2670 output_stack->top = -1;
2671 else
2673 int reg;
2675 output_stack->top = value_reg_high - value_reg_low;
2676 for (reg = value_reg_low; reg <= value_reg_high; ++reg)
2678 output_stack->reg[value_reg_high - reg] = reg;
2679 SET_HARD_REG_BIT (output_stack->reg_set, reg);
2684 /* Copy the stack info from the end of edge E's source block to the
2685 start of E's destination block. */
2687 static void
2688 propagate_stack (edge e)
2690 stack_ptr src_stack = &BLOCK_INFO (e->src)->stack_out;
2691 stack_ptr dest_stack = &BLOCK_INFO (e->dest)->stack_in;
2692 int reg;
2694 /* Preserve the order of the original stack, but check whether
2695 any pops are needed. */
2696 dest_stack->top = -1;
2697 for (reg = 0; reg <= src_stack->top; ++reg)
2698 if (TEST_HARD_REG_BIT (dest_stack->reg_set, src_stack->reg[reg]))
2699 dest_stack->reg[++dest_stack->top] = src_stack->reg[reg];
2701 /* Push in any partially dead values. */
2702 for (reg = FIRST_STACK_REG; reg < LAST_STACK_REG + 1; reg++)
2703 if (TEST_HARD_REG_BIT (dest_stack->reg_set, reg)
2704 && !TEST_HARD_REG_BIT (src_stack->reg_set, reg))
2705 dest_stack->reg[++dest_stack->top] = reg;
2709 /* Adjust the stack of edge E's source block on exit to match the stack
2710 of it's target block upon input. The stack layouts of both blocks
2711 should have been defined by now. */
2713 static bool
2714 compensate_edge (edge e)
2716 basic_block source = e->src, target = e->dest;
2717 stack_ptr target_stack = &BLOCK_INFO (target)->stack_in;
2718 stack_ptr source_stack = &BLOCK_INFO (source)->stack_out;
2719 struct stack_def regstack;
2720 int reg;
2722 if (dump_file)
2723 fprintf (dump_file, "Edge %d->%d: ", source->index, target->index);
2725 gcc_assert (target_stack->top != -2);
2727 /* Check whether stacks are identical. */
2728 if (target_stack->top == source_stack->top)
2730 for (reg = target_stack->top; reg >= 0; --reg)
2731 if (target_stack->reg[reg] != source_stack->reg[reg])
2732 break;
2734 if (reg == -1)
2736 if (dump_file)
2737 fprintf (dump_file, "no changes needed\n");
2738 return false;
2742 if (dump_file)
2744 fprintf (dump_file, "correcting stack to ");
2745 print_stack (dump_file, target_stack);
2748 /* Abnormal calls may appear to have values live in st(0), but the
2749 abnormal return path will not have actually loaded the values. */
2750 if (e->flags & EDGE_ABNORMAL_CALL)
2752 /* Assert that the lifetimes are as we expect -- one value
2753 live at st(0) on the end of the source block, and no
2754 values live at the beginning of the destination block.
2755 For complex return values, we may have st(1) live as well. */
2756 gcc_assert (source_stack->top == 0 || source_stack->top == 1);
2757 gcc_assert (target_stack->top == -1);
2758 return false;
2761 /* Handle non-call EH edges specially. The normal return path have
2762 values in registers. These will be popped en masse by the unwind
2763 library. */
2764 if (e->flags & EDGE_EH)
2766 gcc_assert (target_stack->top == -1);
2767 return false;
2770 /* We don't support abnormal edges. Global takes care to
2771 avoid any live register across them, so we should never
2772 have to insert instructions on such edges. */
2773 gcc_assert (! (e->flags & EDGE_ABNORMAL));
2775 /* Make a copy of source_stack as change_stack is destructive. */
2776 regstack = *source_stack;
2778 /* It is better to output directly to the end of the block
2779 instead of to the edge, because emit_swap can do minimal
2780 insn scheduling. We can do this when there is only one
2781 edge out, and it is not abnormal. */
2782 if (EDGE_COUNT (source->succs) == 1)
2784 current_block = source;
2785 change_stack (BB_END (source), &regstack, target_stack,
2786 (JUMP_P (BB_END (source)) ? EMIT_BEFORE : EMIT_AFTER));
2788 else
2790 rtx_insn *seq;
2791 rtx_note *after;
2793 current_block = NULL;
2794 start_sequence ();
2796 /* ??? change_stack needs some point to emit insns after. */
2797 after = emit_note (NOTE_INSN_DELETED);
2799 change_stack (after, &regstack, target_stack, EMIT_BEFORE);
2801 seq = get_insns ();
2802 end_sequence ();
2804 insert_insn_on_edge (seq, e);
2805 return true;
2807 return false;
2810 /* Traverse all non-entry edges in the CFG, and emit the necessary
2811 edge compensation code to change the stack from stack_out of the
2812 source block to the stack_in of the destination block. */
2814 static bool
2815 compensate_edges (void)
2817 bool inserted = false;
2818 basic_block bb;
2820 starting_stack_p = false;
2822 FOR_EACH_BB_FN (bb, cfun)
2823 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
2825 edge e;
2826 edge_iterator ei;
2828 FOR_EACH_EDGE (e, ei, bb->succs)
2829 inserted |= compensate_edge (e);
2831 return inserted;
2834 /* Select the better of two edges E1 and E2 to use to determine the
2835 stack layout for their shared destination basic block. This is
2836 typically the more frequently executed. The edge E1 may be NULL
2837 (in which case E2 is returned), but E2 is always non-NULL. */
2839 static edge
2840 better_edge (edge e1, edge e2)
2842 if (!e1)
2843 return e2;
2845 if (EDGE_FREQUENCY (e1) > EDGE_FREQUENCY (e2))
2846 return e1;
2847 if (EDGE_FREQUENCY (e1) < EDGE_FREQUENCY (e2))
2848 return e2;
2850 if (e1->count > e2->count)
2851 return e1;
2852 if (e1->count < e2->count)
2853 return e2;
2855 /* Prefer critical edges to minimize inserting compensation code on
2856 critical edges. */
2858 if (EDGE_CRITICAL_P (e1) != EDGE_CRITICAL_P (e2))
2859 return EDGE_CRITICAL_P (e1) ? e1 : e2;
2861 /* Avoid non-deterministic behavior. */
2862 return (e1->src->index < e2->src->index) ? e1 : e2;
2865 /* Convert stack register references in one block. Return true if the CFG
2866 has been modified in the process. */
2868 static bool
2869 convert_regs_1 (basic_block block)
2871 struct stack_def regstack;
2872 block_info bi = BLOCK_INFO (block);
2873 int reg;
2874 rtx_insn *insn, *next;
2875 bool control_flow_insn_deleted = false;
2876 bool cfg_altered = false;
2877 int debug_insns_with_starting_stack = 0;
2879 any_malformed_asm = false;
2881 /* Choose an initial stack layout, if one hasn't already been chosen. */
2882 if (bi->stack_in.top == -2)
2884 edge e, beste = NULL;
2885 edge_iterator ei;
2887 /* Select the best incoming edge (typically the most frequent) to
2888 use as a template for this basic block. */
2889 FOR_EACH_EDGE (e, ei, block->preds)
2890 if (BLOCK_INFO (e->src)->done)
2891 beste = better_edge (beste, e);
2893 if (beste)
2894 propagate_stack (beste);
2895 else
2897 /* No predecessors. Create an arbitrary input stack. */
2898 bi->stack_in.top = -1;
2899 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2900 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2901 bi->stack_in.reg[++bi->stack_in.top] = reg;
2905 if (dump_file)
2907 fprintf (dump_file, "\nBasic block %d\nInput stack: ", block->index);
2908 print_stack (dump_file, &bi->stack_in);
2911 /* Process all insns in this block. Keep track of NEXT so that we
2912 don't process insns emitted while substituting in INSN. */
2913 current_block = block;
2914 next = BB_HEAD (block);
2915 regstack = bi->stack_in;
2916 starting_stack_p = true;
2920 insn = next;
2921 next = NEXT_INSN (insn);
2923 /* Ensure we have not missed a block boundary. */
2924 gcc_assert (next);
2925 if (insn == BB_END (block))
2926 next = NULL;
2928 /* Don't bother processing unless there is a stack reg
2929 mentioned or if it's a CALL_INSN. */
2930 if (DEBUG_INSN_P (insn))
2932 if (starting_stack_p)
2933 debug_insns_with_starting_stack++;
2934 else
2936 subst_all_stack_regs_in_debug_insn (insn, &regstack);
2938 /* Nothing must ever die at a debug insn. If something
2939 is referenced in it that becomes dead, it should have
2940 died before and the reference in the debug insn
2941 should have been removed so as to avoid changing code
2942 generation. */
2943 gcc_assert (!find_reg_note (insn, REG_DEAD, NULL));
2946 else if (stack_regs_mentioned (insn)
2947 || CALL_P (insn))
2949 if (dump_file)
2951 fprintf (dump_file, " insn %d input stack: ",
2952 INSN_UID (insn));
2953 print_stack (dump_file, &regstack);
2955 control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
2956 starting_stack_p = false;
2959 while (next);
2961 if (debug_insns_with_starting_stack)
2963 /* Since it's the first non-debug instruction that determines
2964 the stack requirements of the current basic block, we refrain
2965 from updating debug insns before it in the loop above, and
2966 fix them up here. */
2967 for (insn = BB_HEAD (block); debug_insns_with_starting_stack;
2968 insn = NEXT_INSN (insn))
2970 if (!DEBUG_INSN_P (insn))
2971 continue;
2973 debug_insns_with_starting_stack--;
2974 subst_all_stack_regs_in_debug_insn (insn, &bi->stack_in);
2978 if (dump_file)
2980 fprintf (dump_file, "Expected live registers [");
2981 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
2982 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg))
2983 fprintf (dump_file, " %d", reg);
2984 fprintf (dump_file, " ]\nOutput stack: ");
2985 print_stack (dump_file, &regstack);
2988 insn = BB_END (block);
2989 if (JUMP_P (insn))
2990 insn = PREV_INSN (insn);
2992 /* If the function is declared to return a value, but it returns one
2993 in only some cases, some registers might come live here. Emit
2994 necessary moves for them. */
2996 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
2998 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg)
2999 && ! TEST_HARD_REG_BIT (regstack.reg_set, reg))
3001 rtx set;
3003 if (dump_file)
3004 fprintf (dump_file, "Emitting insn initializing reg %d\n", reg);
3006 set = gen_rtx_SET (FP_MODE_REG (reg, SFmode), not_a_num);
3007 insn = emit_insn_after (set, insn);
3008 control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
3012 /* Amongst the insns possibly deleted during the substitution process above,
3013 might have been the only trapping insn in the block. We purge the now
3014 possibly dead EH edges here to avoid an ICE from fixup_abnormal_edges,
3015 called at the end of convert_regs. The order in which we process the
3016 blocks ensures that we never delete an already processed edge.
3018 Note that, at this point, the CFG may have been damaged by the emission
3019 of instructions after an abnormal call, which moves the basic block end
3020 (and is the reason why we call fixup_abnormal_edges later). So we must
3021 be sure that the trapping insn has been deleted before trying to purge
3022 dead edges, otherwise we risk purging valid edges.
3024 ??? We are normally supposed not to delete trapping insns, so we pretend
3025 that the insns deleted above don't actually trap. It would have been
3026 better to detect this earlier and avoid creating the EH edge in the first
3027 place, still, but we don't have enough information at that time. */
3029 if (control_flow_insn_deleted)
3030 cfg_altered |= purge_dead_edges (block);
3032 /* Something failed if the stack lives don't match. If we had malformed
3033 asms, we zapped the instruction itself, but that didn't produce the
3034 same pattern of register kills as before. */
3036 gcc_assert (hard_reg_set_equal_p (regstack.reg_set, bi->out_reg_set)
3037 || any_malformed_asm);
3038 bi->stack_out = regstack;
3039 bi->done = true;
3041 return cfg_altered;
3044 /* Convert registers in all blocks reachable from BLOCK. Return true if the
3045 CFG has been modified in the process. */
3047 static bool
3048 convert_regs_2 (basic_block block)
3050 basic_block *stack, *sp;
3051 bool cfg_altered = false;
3053 /* We process the blocks in a top-down manner, in a way such that one block
3054 is only processed after all its predecessors. The number of predecessors
3055 of every block has already been computed. */
3057 stack = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
3058 sp = stack;
3060 *sp++ = block;
3064 edge e;
3065 edge_iterator ei;
3067 block = *--sp;
3069 /* Processing BLOCK is achieved by convert_regs_1, which may purge
3070 some dead EH outgoing edge after the deletion of the trapping
3071 insn inside the block. Since the number of predecessors of
3072 BLOCK's successors was computed based on the initial edge set,
3073 we check the necessity to process some of these successors
3074 before such an edge deletion may happen. However, there is
3075 a pitfall: if BLOCK is the only predecessor of a successor and
3076 the edge between them happens to be deleted, the successor
3077 becomes unreachable and should not be processed. The problem
3078 is that there is no way to preventively detect this case so we
3079 stack the successor in all cases and hand over the task of
3080 fixing up the discrepancy to convert_regs_1. */
3082 FOR_EACH_EDGE (e, ei, block->succs)
3083 if (! (e->flags & EDGE_DFS_BACK))
3085 BLOCK_INFO (e->dest)->predecessors--;
3086 if (!BLOCK_INFO (e->dest)->predecessors)
3087 *sp++ = e->dest;
3090 cfg_altered |= convert_regs_1 (block);
3092 while (sp != stack);
3094 free (stack);
3096 return cfg_altered;
3099 /* Traverse all basic blocks in a function, converting the register
3100 references in each insn from the "flat" register file that gcc uses,
3101 to the stack-like registers the 387 uses. */
3103 static void
3104 convert_regs (void)
3106 bool cfg_altered = false;
3107 int inserted;
3108 basic_block b;
3109 edge e;
3110 edge_iterator ei;
3112 /* Initialize uninitialized registers on function entry. */
3113 inserted = convert_regs_entry ();
3115 /* Construct the desired stack for function exit. */
3116 convert_regs_exit ();
3117 BLOCK_INFO (EXIT_BLOCK_PTR_FOR_FN (cfun))->done = 1;
3119 /* ??? Future: process inner loops first, and give them arbitrary
3120 initial stacks which emit_swap_insn can modify. This ought to
3121 prevent double fxch that often appears at the head of a loop. */
3123 /* Process all blocks reachable from all entry points. */
3124 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
3125 cfg_altered |= convert_regs_2 (e->dest);
3127 /* ??? Process all unreachable blocks. Though there's no excuse
3128 for keeping these even when not optimizing. */
3129 FOR_EACH_BB_FN (b, cfun)
3131 block_info bi = BLOCK_INFO (b);
3133 if (! bi->done)
3134 cfg_altered |= convert_regs_2 (b);
3137 /* We must fix up abnormal edges before inserting compensation code
3138 because both mechanisms insert insns on edges. */
3139 inserted |= fixup_abnormal_edges ();
3141 inserted |= compensate_edges ();
3143 clear_aux_for_blocks ();
3145 if (inserted)
3146 commit_edge_insertions ();
3148 if (cfg_altered)
3149 cleanup_cfg (0);
3151 if (dump_file)
3152 fputc ('\n', dump_file);
3155 /* Convert register usage from "flat" register file usage to a "stack
3156 register file. FILE is the dump file, if used.
3158 Construct a CFG and run life analysis. Then convert each insn one
3159 by one. Run a last cleanup_cfg pass, if optimizing, to eliminate
3160 code duplication created when the converter inserts pop insns on
3161 the edges. */
3163 static bool
3164 reg_to_stack (void)
3166 basic_block bb;
3167 int i;
3168 int max_uid;
3170 /* Clean up previous run. */
3171 stack_regs_mentioned_data.release ();
3173 /* See if there is something to do. Flow analysis is quite
3174 expensive so we might save some compilation time. */
3175 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
3176 if (df_regs_ever_live_p (i))
3177 break;
3178 if (i > LAST_STACK_REG)
3179 return false;
3181 df_note_add_problem ();
3182 df_analyze ();
3184 mark_dfs_back_edges ();
3186 /* Set up block info for each basic block. */
3187 alloc_aux_for_blocks (sizeof (struct block_info_def));
3188 FOR_EACH_BB_FN (bb, cfun)
3190 block_info bi = BLOCK_INFO (bb);
3191 edge_iterator ei;
3192 edge e;
3193 int reg;
3195 FOR_EACH_EDGE (e, ei, bb->preds)
3196 if (!(e->flags & EDGE_DFS_BACK)
3197 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3198 bi->predecessors++;
3200 /* Set current register status at last instruction `uninitialized'. */
3201 bi->stack_in.top = -2;
3203 /* Copy live_at_end and live_at_start into temporaries. */
3204 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
3206 if (REGNO_REG_SET_P (DF_LR_OUT (bb), reg))
3207 SET_HARD_REG_BIT (bi->out_reg_set, reg);
3208 if (REGNO_REG_SET_P (DF_LR_IN (bb), reg))
3209 SET_HARD_REG_BIT (bi->stack_in.reg_set, reg);
3213 /* Create the replacement registers up front. */
3214 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
3216 machine_mode mode;
3217 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3218 mode != VOIDmode;
3219 mode = GET_MODE_WIDER_MODE (mode))
3220 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
3221 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT);
3222 mode != VOIDmode;
3223 mode = GET_MODE_WIDER_MODE (mode))
3224 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
3227 ix86_flags_rtx = gen_rtx_REG (CCmode, FLAGS_REG);
3229 /* A QNaN for initializing uninitialized variables.
3231 ??? We can't load from constant memory in PIC mode, because
3232 we're inserting these instructions before the prologue and
3233 the PIC register hasn't been set up. In that case, fall back
3234 on zero, which we can get from `fldz'. */
3236 if ((flag_pic && !TARGET_64BIT)
3237 || ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC)
3238 not_a_num = CONST0_RTX (SFmode);
3239 else
3241 REAL_VALUE_TYPE r;
3243 real_nan (&r, "", 1, SFmode);
3244 not_a_num = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
3245 not_a_num = force_const_mem (SFmode, not_a_num);
3248 /* Allocate a cache for stack_regs_mentioned. */
3249 max_uid = get_max_uid ();
3250 stack_regs_mentioned_data.create (max_uid + 1);
3251 memset (stack_regs_mentioned_data.address (),
3252 0, sizeof (char) * (max_uid + 1));
3254 convert_regs ();
3256 free_aux_for_blocks ();
3257 return true;
3259 #endif /* STACK_REGS */
3261 namespace {
3263 const pass_data pass_data_stack_regs =
3265 RTL_PASS, /* type */
3266 "*stack_regs", /* name */
3267 OPTGROUP_NONE, /* optinfo_flags */
3268 TV_REG_STACK, /* tv_id */
3269 0, /* properties_required */
3270 0, /* properties_provided */
3271 0, /* properties_destroyed */
3272 0, /* todo_flags_start */
3273 0, /* todo_flags_finish */
3276 class pass_stack_regs : public rtl_opt_pass
3278 public:
3279 pass_stack_regs (gcc::context *ctxt)
3280 : rtl_opt_pass (pass_data_stack_regs, ctxt)
3283 /* opt_pass methods: */
3284 virtual bool gate (function *)
3286 #ifdef STACK_REGS
3287 return true;
3288 #else
3289 return false;
3290 #endif
3293 }; // class pass_stack_regs
3295 } // anon namespace
3297 rtl_opt_pass *
3298 make_pass_stack_regs (gcc::context *ctxt)
3300 return new pass_stack_regs (ctxt);
3303 /* Convert register usage from flat register file usage to a stack
3304 register file. */
3305 static unsigned int
3306 rest_of_handle_stack_regs (void)
3308 #ifdef STACK_REGS
3309 reg_to_stack ();
3310 regstack_completed = 1;
3311 #endif
3312 return 0;
3315 namespace {
3317 const pass_data pass_data_stack_regs_run =
3319 RTL_PASS, /* type */
3320 "stack", /* name */
3321 OPTGROUP_NONE, /* optinfo_flags */
3322 TV_REG_STACK, /* tv_id */
3323 0, /* properties_required */
3324 0, /* properties_provided */
3325 0, /* properties_destroyed */
3326 0, /* todo_flags_start */
3327 TODO_df_finish, /* todo_flags_finish */
3330 class pass_stack_regs_run : public rtl_opt_pass
3332 public:
3333 pass_stack_regs_run (gcc::context *ctxt)
3334 : rtl_opt_pass (pass_data_stack_regs_run, ctxt)
3337 /* opt_pass methods: */
3338 virtual unsigned int execute (function *)
3340 return rest_of_handle_stack_regs ();
3343 }; // class pass_stack_regs_run
3345 } // anon namespace
3347 rtl_opt_pass *
3348 make_pass_stack_regs_run (gcc::context *ctxt)
3350 return new pass_stack_regs_run (ctxt);