* basic-block.h (FOR_EACH_EDGE): Record initial edge count.
[official-gcc.git] / gcc / reg-stack.c
blobfdb1a6c5e41b0b3cb5ce3fe986258ccf4b675cc6
1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This pass converts stack-like registers from the "flat register
23 file" model that gcc uses, to a stack convention that the 387 uses.
25 * The form of the input:
27 On input, the function consists of insn that have had their
28 registers fully allocated to a set of "virtual" registers. Note that
29 the word "virtual" is used differently here than elsewhere in gcc: for
30 each virtual stack reg, there is a hard reg, but the mapping between
31 them is not known until this pass is run. On output, hard register
32 numbers have been substituted, and various pop and exchange insns have
33 been emitted. The hard register numbers and the virtual register
34 numbers completely overlap - before this pass, all stack register
35 numbers are virtual, and afterward they are all hard.
37 The virtual registers can be manipulated normally by gcc, and their
38 semantics are the same as for normal registers. After the hard
39 register numbers are substituted, the semantics of an insn containing
40 stack-like regs are not the same as for an insn with normal regs: for
41 instance, it is not safe to delete an insn that appears to be a no-op
42 move. In general, no insn containing hard regs should be changed
43 after this pass is done.
45 * The form of the output:
47 After this pass, hard register numbers represent the distance from
48 the current top of stack to the desired register. A reference to
49 FIRST_STACK_REG references the top of stack, FIRST_STACK_REG + 1,
50 represents the register just below that, and so forth. Also, REG_DEAD
51 notes indicate whether or not a stack register should be popped.
53 A "swap" insn looks like a parallel of two patterns, where each
54 pattern is a SET: one sets A to B, the other B to A.
56 A "push" or "load" insn is a SET whose SET_DEST is FIRST_STACK_REG
57 and whose SET_DEST is REG or MEM. Any other SET_DEST, such as PLUS,
58 will replace the existing stack top, not push a new value.
60 A store insn is a SET whose SET_DEST is FIRST_STACK_REG, and whose
61 SET_SRC is REG or MEM.
63 The case where the SET_SRC and SET_DEST are both FIRST_STACK_REG
64 appears ambiguous. As a special case, the presence of a REG_DEAD note
65 for FIRST_STACK_REG differentiates between a load insn and a pop.
67 If a REG_DEAD is present, the insn represents a "pop" that discards
68 the top of the register stack. If there is no REG_DEAD note, then the
69 insn represents a "dup" or a push of the current top of stack onto the
70 stack.
72 * Methodology:
74 Existing REG_DEAD and REG_UNUSED notes for stack registers are
75 deleted and recreated from scratch. REG_DEAD is never created for a
76 SET_DEST, only REG_UNUSED.
78 * asm_operands:
80 There are several rules on the usage of stack-like regs in
81 asm_operands insns. These rules apply only to the operands that are
82 stack-like regs:
84 1. Given a set of input regs that die in an asm_operands, it is
85 necessary to know which are implicitly popped by the asm, and
86 which must be explicitly popped by gcc.
88 An input reg that is implicitly popped by the asm must be
89 explicitly clobbered, unless it is constrained to match an
90 output operand.
92 2. For any input reg that is implicitly popped by an asm, it is
93 necessary to know how to adjust the stack to compensate for the pop.
94 If any non-popped input is closer to the top of the reg-stack than
95 the implicitly popped reg, it would not be possible to know what the
96 stack looked like - it's not clear how the rest of the stack "slides
97 up".
99 All implicitly popped input regs must be closer to the top of
100 the reg-stack than any input that is not implicitly popped.
102 3. It is possible that if an input dies in an insn, reload might
103 use the input reg for an output reload. Consider this example:
105 asm ("foo" : "=t" (a) : "f" (b));
107 This asm says that input B is not popped by the asm, and that
108 the asm pushes a result onto the reg-stack, ie, the stack is one
109 deeper after the asm than it was before. But, it is possible that
110 reload will think that it can use the same reg for both the input and
111 the output, if input B dies in this insn.
113 If any input operand uses the "f" constraint, all output reg
114 constraints must use the "&" earlyclobber.
116 The asm above would be written as
118 asm ("foo" : "=&t" (a) : "f" (b));
120 4. Some operands need to be in particular places on the stack. All
121 output operands fall in this category - there is no other way to
122 know which regs the outputs appear in unless the user indicates
123 this in the constraints.
125 Output operands must specifically indicate which reg an output
126 appears in after an asm. "=f" is not allowed: the operand
127 constraints must select a class with a single reg.
129 5. Output operands may not be "inserted" between existing stack regs.
130 Since no 387 opcode uses a read/write operand, all output operands
131 are dead before the asm_operands, and are pushed by the asm_operands.
132 It makes no sense to push anywhere but the top of the reg-stack.
134 Output operands must start at the top of the reg-stack: output
135 operands may not "skip" a reg.
137 6. Some asm statements may need extra stack space for internal
138 calculations. This can be guaranteed by clobbering stack registers
139 unrelated to the inputs and outputs.
141 Here are a couple of reasonable asms to want to write. This asm
142 takes one input, which is internally popped, and produces two outputs.
144 asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
146 This asm takes two inputs, which are popped by the fyl2xp1 opcode,
147 and replaces them with one output. The user must code the "st(1)"
148 clobber for reg-stack.c to know that fyl2xp1 pops both inputs.
150 asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
154 #include "config.h"
155 #include "system.h"
156 #include "coretypes.h"
157 #include "tm.h"
158 #include "tree.h"
159 #include "rtl.h"
160 #include "tm_p.h"
161 #include "function.h"
162 #include "insn-config.h"
163 #include "regs.h"
164 #include "hard-reg-set.h"
165 #include "flags.h"
166 #include "toplev.h"
167 #include "recog.h"
168 #include "output.h"
169 #include "basic-block.h"
170 #include "varray.h"
171 #include "reload.h"
172 #include "ggc.h"
174 /* We use this array to cache info about insns, because otherwise we
175 spend too much time in stack_regs_mentioned_p.
177 Indexed by insn UIDs. A value of zero is uninitialized, one indicates
178 the insn uses stack registers, two indicates the insn does not use
179 stack registers. */
180 static GTY(()) varray_type stack_regs_mentioned_data;
182 #ifdef STACK_REGS
184 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
186 /* This is the basic stack record. TOP is an index into REG[] such
187 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
189 If TOP is -2, REG[] is not yet initialized. Stack initialization
190 consists of placing each live reg in array `reg' and setting `top'
191 appropriately.
193 REG_SET indicates which registers are live. */
195 typedef struct stack_def
197 int top; /* index to top stack element */
198 HARD_REG_SET reg_set; /* set of live registers */
199 unsigned char reg[REG_STACK_SIZE];/* register - stack mapping */
200 } *stack;
202 /* This is used to carry information about basic blocks. It is
203 attached to the AUX field of the standard CFG block. */
205 typedef struct block_info_def
207 struct stack_def stack_in; /* Input stack configuration. */
208 struct stack_def stack_out; /* Output stack configuration. */
209 HARD_REG_SET out_reg_set; /* Stack regs live on output. */
210 int done; /* True if block already converted. */
211 int predecessors; /* Number of predecessors that needs
212 to be visited. */
213 } *block_info;
215 #define BLOCK_INFO(B) ((block_info) (B)->aux)
217 /* Passed to change_stack to indicate where to emit insns. */
218 enum emit_where
220 EMIT_AFTER,
221 EMIT_BEFORE
224 /* The block we're currently working on. */
225 static basic_block current_block;
227 /* This is the register file for all register after conversion. */
228 static rtx
229 FP_mode_reg[LAST_STACK_REG+1-FIRST_STACK_REG][(int) MAX_MACHINE_MODE];
231 #define FP_MODE_REG(regno,mode) \
232 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int) (mode)])
234 /* Used to initialize uninitialized registers. */
235 static rtx not_a_num;
237 /* Forward declarations */
239 static int stack_regs_mentioned_p (rtx pat);
240 static void straighten_stack (rtx, stack);
241 static void pop_stack (stack, int);
242 static rtx *get_true_reg (rtx *);
244 static int check_asm_stack_operands (rtx);
245 static int get_asm_operand_n_inputs (rtx);
246 static rtx stack_result (tree);
247 static void replace_reg (rtx *, int);
248 static void remove_regno_note (rtx, enum reg_note, unsigned int);
249 static int get_hard_regnum (stack, rtx);
250 static rtx emit_pop_insn (rtx, stack, rtx, enum emit_where);
251 static void emit_swap_insn (rtx, stack, rtx);
252 static void swap_to_top(rtx, stack, rtx, rtx);
253 static bool move_for_stack_reg (rtx, stack, rtx);
254 static int swap_rtx_condition_1 (rtx);
255 static int swap_rtx_condition (rtx);
256 static void compare_for_stack_reg (rtx, stack, rtx);
257 static bool subst_stack_regs_pat (rtx, stack, rtx);
258 static void subst_asm_stack_regs (rtx, stack);
259 static bool subst_stack_regs (rtx, stack);
260 static void change_stack (rtx, stack, stack, enum emit_where);
261 static int convert_regs_entry (void);
262 static void convert_regs_exit (void);
263 static int convert_regs_1 (FILE *, basic_block);
264 static int convert_regs_2 (FILE *, basic_block);
265 static int convert_regs (FILE *);
266 static void print_stack (FILE *, stack);
267 static rtx next_flags_user (rtx);
268 static void record_label_references (rtx, rtx);
269 static bool compensate_edge (edge, FILE *);
271 /* Return nonzero if any stack register is mentioned somewhere within PAT. */
273 static int
274 stack_regs_mentioned_p (rtx pat)
276 const char *fmt;
277 int i;
279 if (STACK_REG_P (pat))
280 return 1;
282 fmt = GET_RTX_FORMAT (GET_CODE (pat));
283 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
285 if (fmt[i] == 'E')
287 int j;
289 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
290 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
291 return 1;
293 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
294 return 1;
297 return 0;
300 /* Return nonzero if INSN mentions stacked registers, else return zero. */
303 stack_regs_mentioned (rtx insn)
305 unsigned int uid, max;
306 int test;
308 if (! INSN_P (insn) || !stack_regs_mentioned_data)
309 return 0;
311 uid = INSN_UID (insn);
312 max = VARRAY_SIZE (stack_regs_mentioned_data);
313 if (uid >= max)
315 /* Allocate some extra size to avoid too many reallocs, but
316 do not grow too quickly. */
317 max = uid + uid / 20;
318 VARRAY_GROW (stack_regs_mentioned_data, max);
321 test = VARRAY_CHAR (stack_regs_mentioned_data, uid);
322 if (test == 0)
324 /* This insn has yet to be examined. Do so now. */
325 test = stack_regs_mentioned_p (PATTERN (insn)) ? 1 : 2;
326 VARRAY_CHAR (stack_regs_mentioned_data, uid) = test;
329 return test == 1;
332 static rtx ix86_flags_rtx;
334 static rtx
335 next_flags_user (rtx insn)
337 /* Search forward looking for the first use of this value.
338 Stop at block boundaries. */
340 while (insn != BB_END (current_block))
342 insn = NEXT_INSN (insn);
344 if (INSN_P (insn) && reg_mentioned_p (ix86_flags_rtx, PATTERN (insn)))
345 return insn;
347 if (CALL_P (insn))
348 return NULL_RTX;
350 return NULL_RTX;
353 /* Reorganize the stack into ascending numbers,
354 after this insn. */
356 static void
357 straighten_stack (rtx insn, stack regstack)
359 struct stack_def temp_stack;
360 int top;
362 /* If there is only a single register on the stack, then the stack is
363 already in increasing order and no reorganization is needed.
365 Similarly if the stack is empty. */
366 if (regstack->top <= 0)
367 return;
369 COPY_HARD_REG_SET (temp_stack.reg_set, regstack->reg_set);
371 for (top = temp_stack.top = regstack->top; top >= 0; top--)
372 temp_stack.reg[top] = FIRST_STACK_REG + temp_stack.top - top;
374 change_stack (insn, regstack, &temp_stack, EMIT_AFTER);
377 /* Pop a register from the stack. */
379 static void
380 pop_stack (stack regstack, int regno)
382 int top = regstack->top;
384 CLEAR_HARD_REG_BIT (regstack->reg_set, regno);
385 regstack->top--;
386 /* If regno was not at the top of stack then adjust stack. */
387 if (regstack->reg [top] != regno)
389 int i;
390 for (i = regstack->top; i >= 0; i--)
391 if (regstack->reg [i] == regno)
393 int j;
394 for (j = i; j < top; j++)
395 regstack->reg [j] = regstack->reg [j + 1];
396 break;
401 /* Convert register usage from "flat" register file usage to a "stack
402 register file. FILE is the dump file, if used.
404 Construct a CFG and run life analysis. Then convert each insn one
405 by one. Run a last cleanup_cfg pass, if optimizing, to eliminate
406 code duplication created when the converter inserts pop insns on
407 the edges. */
409 bool
410 reg_to_stack (FILE *file)
412 basic_block bb;
413 int i;
414 int max_uid;
416 /* Clean up previous run. */
417 stack_regs_mentioned_data = 0;
419 /* See if there is something to do. Flow analysis is quite
420 expensive so we might save some compilation time. */
421 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
422 if (regs_ever_live[i])
423 break;
424 if (i > LAST_STACK_REG)
425 return false;
427 /* Ok, floating point instructions exist. If not optimizing,
428 build the CFG and run life analysis.
429 Also need to rebuild life when superblock scheduling is done
430 as it don't update liveness yet. */
431 if (!optimize
432 || (flag_sched2_use_superblocks
433 && flag_schedule_insns_after_reload))
435 count_or_remove_death_notes (NULL, 1);
436 life_analysis (file, PROP_DEATH_NOTES);
438 mark_dfs_back_edges ();
440 /* Set up block info for each basic block. */
441 alloc_aux_for_blocks (sizeof (struct block_info_def));
442 FOR_EACH_BB_REVERSE (bb)
444 edge e;
446 FOR_EACH_EDGE (e, bb->preds)
448 if (!(e->flags & EDGE_DFS_BACK)
449 && e->src != ENTRY_BLOCK_PTR)
450 BLOCK_INFO (bb)->predecessors++;
452 END_FOR_EACH_EDGE;
455 /* Create the replacement registers up front. */
456 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
458 enum machine_mode mode;
459 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
460 mode != VOIDmode;
461 mode = GET_MODE_WIDER_MODE (mode))
462 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
463 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT);
464 mode != VOIDmode;
465 mode = GET_MODE_WIDER_MODE (mode))
466 FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
469 ix86_flags_rtx = gen_rtx_REG (CCmode, FLAGS_REG);
471 /* A QNaN for initializing uninitialized variables.
473 ??? We can't load from constant memory in PIC mode, because
474 we're inserting these instructions before the prologue and
475 the PIC register hasn't been set up. In that case, fall back
476 on zero, which we can get from `ldz'. */
478 if (flag_pic)
479 not_a_num = CONST0_RTX (SFmode);
480 else
482 not_a_num = gen_lowpart (SFmode, GEN_INT (0x7fc00000));
483 not_a_num = force_const_mem (SFmode, not_a_num);
486 /* Allocate a cache for stack_regs_mentioned. */
487 max_uid = get_max_uid ();
488 VARRAY_CHAR_INIT (stack_regs_mentioned_data, max_uid + 1,
489 "stack_regs_mentioned cache");
491 convert_regs (file);
493 free_aux_for_blocks ();
494 return true;
497 /* Check PAT, which is in INSN, for LABEL_REFs. Add INSN to the
498 label's chain of references, and note which insn contains each
499 reference. */
501 static void
502 record_label_references (rtx insn, rtx pat)
504 enum rtx_code code = GET_CODE (pat);
505 int i;
506 const char *fmt;
508 if (code == LABEL_REF)
510 rtx label = XEXP (pat, 0);
511 rtx ref;
513 if (!LABEL_P (label))
514 abort ();
516 /* If this is an undefined label, LABEL_REFS (label) contains
517 garbage. */
518 if (INSN_UID (label) == 0)
519 return;
521 /* Don't make a duplicate in the code_label's chain. */
523 for (ref = LABEL_REFS (label);
524 ref && ref != label;
525 ref = LABEL_NEXTREF (ref))
526 if (CONTAINING_INSN (ref) == insn)
527 return;
529 CONTAINING_INSN (pat) = insn;
530 LABEL_NEXTREF (pat) = LABEL_REFS (label);
531 LABEL_REFS (label) = pat;
533 return;
536 fmt = GET_RTX_FORMAT (code);
537 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
539 if (fmt[i] == 'e')
540 record_label_references (insn, XEXP (pat, i));
541 if (fmt[i] == 'E')
543 int j;
544 for (j = 0; j < XVECLEN (pat, i); j++)
545 record_label_references (insn, XVECEXP (pat, i, j));
550 /* Return a pointer to the REG expression within PAT. If PAT is not a
551 REG, possible enclosed by a conversion rtx, return the inner part of
552 PAT that stopped the search. */
554 static rtx *
555 get_true_reg (rtx *pat)
557 for (;;)
558 switch (GET_CODE (*pat))
560 case SUBREG:
561 /* Eliminate FP subregister accesses in favor of the
562 actual FP register in use. */
564 rtx subreg;
565 if (FP_REG_P (subreg = SUBREG_REG (*pat)))
567 int regno_off = subreg_regno_offset (REGNO (subreg),
568 GET_MODE (subreg),
569 SUBREG_BYTE (*pat),
570 GET_MODE (*pat));
571 *pat = FP_MODE_REG (REGNO (subreg) + regno_off,
572 GET_MODE (subreg));
573 default:
574 return pat;
577 case FLOAT:
578 case FIX:
579 case FLOAT_EXTEND:
580 pat = & XEXP (*pat, 0);
581 break;
583 case FLOAT_TRUNCATE:
584 if (!flag_unsafe_math_optimizations)
585 return pat;
586 pat = & XEXP (*pat, 0);
587 break;
591 /* Set if we find any malformed asms in a block. */
592 static bool any_malformed_asm;
594 /* There are many rules that an asm statement for stack-like regs must
595 follow. Those rules are explained at the top of this file: the rule
596 numbers below refer to that explanation. */
598 static int
599 check_asm_stack_operands (rtx insn)
601 int i;
602 int n_clobbers;
603 int malformed_asm = 0;
604 rtx body = PATTERN (insn);
606 char reg_used_as_output[FIRST_PSEUDO_REGISTER];
607 char implicitly_dies[FIRST_PSEUDO_REGISTER];
608 int alt;
610 rtx *clobber_reg = 0;
611 int n_inputs, n_outputs;
613 /* Find out what the constraints require. If no constraint
614 alternative matches, this asm is malformed. */
615 extract_insn (insn);
616 constrain_operands (1);
617 alt = which_alternative;
619 preprocess_constraints ();
621 n_inputs = get_asm_operand_n_inputs (body);
622 n_outputs = recog_data.n_operands - n_inputs;
624 if (alt < 0)
626 malformed_asm = 1;
627 /* Avoid further trouble with this insn. */
628 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
629 return 0;
632 /* Strip SUBREGs here to make the following code simpler. */
633 for (i = 0; i < recog_data.n_operands; i++)
634 if (GET_CODE (recog_data.operand[i]) == SUBREG
635 && REG_P (SUBREG_REG (recog_data.operand[i])))
636 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
638 /* Set up CLOBBER_REG. */
640 n_clobbers = 0;
642 if (GET_CODE (body) == PARALLEL)
644 clobber_reg = alloca (XVECLEN (body, 0) * sizeof (rtx));
646 for (i = 0; i < XVECLEN (body, 0); i++)
647 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
649 rtx clobber = XVECEXP (body, 0, i);
650 rtx reg = XEXP (clobber, 0);
652 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
653 reg = SUBREG_REG (reg);
655 if (STACK_REG_P (reg))
657 clobber_reg[n_clobbers] = reg;
658 n_clobbers++;
663 /* Enforce rule #4: Output operands must specifically indicate which
664 reg an output appears in after an asm. "=f" is not allowed: the
665 operand constraints must select a class with a single reg.
667 Also enforce rule #5: Output operands must start at the top of
668 the reg-stack: output operands may not "skip" a reg. */
670 memset (reg_used_as_output, 0, sizeof (reg_used_as_output));
671 for (i = 0; i < n_outputs; i++)
672 if (STACK_REG_P (recog_data.operand[i]))
674 if (reg_class_size[(int) recog_op_alt[i][alt].cl] != 1)
676 error_for_asm (insn, "output constraint %d must specify a single register", i);
677 malformed_asm = 1;
679 else
681 int j;
683 for (j = 0; j < n_clobbers; j++)
684 if (REGNO (recog_data.operand[i]) == REGNO (clobber_reg[j]))
686 error_for_asm (insn, "output constraint %d cannot be specified together with \"%s\" clobber",
687 i, reg_names [REGNO (clobber_reg[j])]);
688 malformed_asm = 1;
689 break;
691 if (j == n_clobbers)
692 reg_used_as_output[REGNO (recog_data.operand[i])] = 1;
697 /* Search for first non-popped reg. */
698 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
699 if (! reg_used_as_output[i])
700 break;
702 /* If there are any other popped regs, that's an error. */
703 for (; i < LAST_STACK_REG + 1; i++)
704 if (reg_used_as_output[i])
705 break;
707 if (i != LAST_STACK_REG + 1)
709 error_for_asm (insn, "output regs must be grouped at top of stack");
710 malformed_asm = 1;
713 /* Enforce rule #2: All implicitly popped input regs must be closer
714 to the top of the reg-stack than any input that is not implicitly
715 popped. */
717 memset (implicitly_dies, 0, sizeof (implicitly_dies));
718 for (i = n_outputs; i < n_outputs + n_inputs; i++)
719 if (STACK_REG_P (recog_data.operand[i]))
721 /* An input reg is implicitly popped if it is tied to an
722 output, or if there is a CLOBBER for it. */
723 int j;
725 for (j = 0; j < n_clobbers; j++)
726 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
727 break;
729 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
730 implicitly_dies[REGNO (recog_data.operand[i])] = 1;
733 /* Search for first non-popped reg. */
734 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
735 if (! implicitly_dies[i])
736 break;
738 /* If there are any other popped regs, that's an error. */
739 for (; i < LAST_STACK_REG + 1; i++)
740 if (implicitly_dies[i])
741 break;
743 if (i != LAST_STACK_REG + 1)
745 error_for_asm (insn,
746 "implicitly popped regs must be grouped at top of stack");
747 malformed_asm = 1;
750 /* Enforce rule #3: If any input operand uses the "f" constraint, all
751 output constraints must use the "&" earlyclobber.
753 ??? Detect this more deterministically by having constrain_asm_operands
754 record any earlyclobber. */
756 for (i = n_outputs; i < n_outputs + n_inputs; i++)
757 if (recog_op_alt[i][alt].matches == -1)
759 int j;
761 for (j = 0; j < n_outputs; j++)
762 if (operands_match_p (recog_data.operand[j], recog_data.operand[i]))
764 error_for_asm (insn,
765 "output operand %d must use `&' constraint", j);
766 malformed_asm = 1;
770 if (malformed_asm)
772 /* Avoid further trouble with this insn. */
773 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
774 any_malformed_asm = true;
775 return 0;
778 return 1;
781 /* Calculate the number of inputs and outputs in BODY, an
782 asm_operands. N_OPERANDS is the total number of operands, and
783 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
784 placed. */
786 static int
787 get_asm_operand_n_inputs (rtx body)
789 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
790 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body));
792 else if (GET_CODE (body) == ASM_OPERANDS)
793 return ASM_OPERANDS_INPUT_LENGTH (body);
795 else if (GET_CODE (body) == PARALLEL
796 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
797 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (XVECEXP (body, 0, 0)));
799 else if (GET_CODE (body) == PARALLEL
800 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
801 return ASM_OPERANDS_INPUT_LENGTH (XVECEXP (body, 0, 0));
803 abort ();
806 /* If current function returns its result in an fp stack register,
807 return the REG. Otherwise, return 0. */
809 static rtx
810 stack_result (tree decl)
812 rtx result;
814 /* If the value is supposed to be returned in memory, then clearly
815 it is not returned in a stack register. */
816 if (aggregate_value_p (DECL_RESULT (decl), decl))
817 return 0;
819 result = DECL_RTL_IF_SET (DECL_RESULT (decl));
820 if (result != 0)
822 #ifdef FUNCTION_OUTGOING_VALUE
823 result
824 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
825 #else
826 result = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
827 #endif
830 return result != 0 && STACK_REG_P (result) ? result : 0;
835 * This section deals with stack register substitution, and forms the second
836 * pass over the RTL.
839 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
840 the desired hard REGNO. */
842 static void
843 replace_reg (rtx *reg, int regno)
845 if (regno < FIRST_STACK_REG || regno > LAST_STACK_REG
846 || ! STACK_REG_P (*reg))
847 abort ();
849 switch (GET_MODE_CLASS (GET_MODE (*reg)))
851 default: abort ();
852 case MODE_FLOAT:
853 case MODE_COMPLEX_FLOAT:;
856 *reg = FP_MODE_REG (regno, GET_MODE (*reg));
859 /* Remove a note of type NOTE, which must be found, for register
860 number REGNO from INSN. Remove only one such note. */
862 static void
863 remove_regno_note (rtx insn, enum reg_note note, unsigned int regno)
865 rtx *note_link, this;
867 note_link = &REG_NOTES (insn);
868 for (this = *note_link; this; this = XEXP (this, 1))
869 if (REG_NOTE_KIND (this) == note
870 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno)
872 *note_link = XEXP (this, 1);
873 return;
875 else
876 note_link = &XEXP (this, 1);
878 abort ();
881 /* Find the hard register number of virtual register REG in REGSTACK.
882 The hard register number is relative to the top of the stack. -1 is
883 returned if the register is not found. */
885 static int
886 get_hard_regnum (stack regstack, rtx reg)
888 int i;
890 if (! STACK_REG_P (reg))
891 abort ();
893 for (i = regstack->top; i >= 0; i--)
894 if (regstack->reg[i] == REGNO (reg))
895 break;
897 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
900 /* Emit an insn to pop virtual register REG before or after INSN.
901 REGSTACK is the stack state after INSN and is updated to reflect this
902 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
903 is represented as a SET whose destination is the register to be popped
904 and source is the top of stack. A death note for the top of stack
905 cases the movdf pattern to pop. */
907 static rtx
908 emit_pop_insn (rtx insn, stack regstack, rtx reg, enum emit_where where)
910 rtx pop_insn, pop_rtx;
911 int hard_regno;
913 /* For complex types take care to pop both halves. These may survive in
914 CLOBBER and USE expressions. */
915 if (COMPLEX_MODE_P (GET_MODE (reg)))
917 rtx reg1 = FP_MODE_REG (REGNO (reg), DFmode);
918 rtx reg2 = FP_MODE_REG (REGNO (reg) + 1, DFmode);
920 pop_insn = NULL_RTX;
921 if (get_hard_regnum (regstack, reg1) >= 0)
922 pop_insn = emit_pop_insn (insn, regstack, reg1, where);
923 if (get_hard_regnum (regstack, reg2) >= 0)
924 pop_insn = emit_pop_insn (insn, regstack, reg2, where);
925 if (!pop_insn)
926 abort ();
927 return pop_insn;
930 hard_regno = get_hard_regnum (regstack, reg);
932 if (hard_regno < FIRST_STACK_REG)
933 abort ();
935 pop_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG (hard_regno, DFmode),
936 FP_MODE_REG (FIRST_STACK_REG, DFmode));
938 if (where == EMIT_AFTER)
939 pop_insn = emit_insn_after (pop_rtx, insn);
940 else
941 pop_insn = emit_insn_before (pop_rtx, insn);
943 REG_NOTES (pop_insn)
944 = gen_rtx_EXPR_LIST (REG_DEAD, FP_MODE_REG (FIRST_STACK_REG, DFmode),
945 REG_NOTES (pop_insn));
947 regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
948 = regstack->reg[regstack->top];
949 regstack->top -= 1;
950 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
952 return pop_insn;
955 /* Emit an insn before or after INSN to swap virtual register REG with
956 the top of stack. REGSTACK is the stack state before the swap, and
957 is updated to reflect the swap. A swap insn is represented as a
958 PARALLEL of two patterns: each pattern moves one reg to the other.
960 If REG is already at the top of the stack, no insn is emitted. */
962 static void
963 emit_swap_insn (rtx insn, stack regstack, rtx reg)
965 int hard_regno;
966 rtx swap_rtx;
967 int tmp, other_reg; /* swap regno temps */
968 rtx i1; /* the stack-reg insn prior to INSN */
969 rtx i1set = NULL_RTX; /* the SET rtx within I1 */
971 hard_regno = get_hard_regnum (regstack, reg);
973 if (hard_regno < FIRST_STACK_REG)
974 abort ();
975 if (hard_regno == FIRST_STACK_REG)
976 return;
978 other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
980 tmp = regstack->reg[other_reg];
981 regstack->reg[other_reg] = regstack->reg[regstack->top];
982 regstack->reg[regstack->top] = tmp;
984 /* Find the previous insn involving stack regs, but don't pass a
985 block boundary. */
986 i1 = NULL;
987 if (current_block && insn != BB_HEAD (current_block))
989 rtx tmp = PREV_INSN (insn);
990 rtx limit = PREV_INSN (BB_HEAD (current_block));
991 while (tmp != limit)
993 if (LABEL_P (tmp)
994 || CALL_P (tmp)
995 || NOTE_INSN_BASIC_BLOCK_P (tmp)
996 || (NONJUMP_INSN_P (tmp)
997 && stack_regs_mentioned (tmp)))
999 i1 = tmp;
1000 break;
1002 tmp = PREV_INSN (tmp);
1006 if (i1 != NULL_RTX
1007 && (i1set = single_set (i1)) != NULL_RTX)
1009 rtx i1src = *get_true_reg (&SET_SRC (i1set));
1010 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
1012 /* If the previous register stack push was from the reg we are to
1013 swap with, omit the swap. */
1015 if (REG_P (i1dest) && REGNO (i1dest) == FIRST_STACK_REG
1016 && REG_P (i1src)
1017 && REGNO (i1src) == (unsigned) hard_regno - 1
1018 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
1019 return;
1021 /* If the previous insn wrote to the reg we are to swap with,
1022 omit the swap. */
1024 if (REG_P (i1dest) && REGNO (i1dest) == (unsigned) hard_regno
1025 && REG_P (i1src) && REGNO (i1src) == FIRST_STACK_REG
1026 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
1027 return;
1030 swap_rtx = gen_swapxf (FP_MODE_REG (hard_regno, XFmode),
1031 FP_MODE_REG (FIRST_STACK_REG, XFmode));
1033 if (i1)
1034 emit_insn_after (swap_rtx, i1);
1035 else if (current_block)
1036 emit_insn_before (swap_rtx, BB_HEAD (current_block));
1037 else
1038 emit_insn_before (swap_rtx, insn);
1041 /* Emit an insns before INSN to swap virtual register SRC1 with
1042 the top of stack and virtual register SRC2 with second stack
1043 slot. REGSTACK is the stack state before the swaps, and
1044 is updated to reflect the swaps. A swap insn is represented as a
1045 PARALLEL of two patterns: each pattern moves one reg to the other.
1047 If SRC1 and/or SRC2 are already at the right place, no swap insn
1048 is emitted. */
1050 static void
1051 swap_to_top (rtx insn, stack regstack, rtx src1, rtx src2)
1053 struct stack_def temp_stack;
1054 int regno, j, k, temp;
1056 temp_stack = *regstack;
1058 /* Place operand 1 at the top of stack. */
1059 regno = get_hard_regnum (&temp_stack, src1);
1060 if (regno < 0)
1061 abort ();
1062 if (regno != FIRST_STACK_REG)
1064 k = temp_stack.top - (regno - FIRST_STACK_REG);
1065 j = temp_stack.top;
1067 temp = temp_stack.reg[k];
1068 temp_stack.reg[k] = temp_stack.reg[j];
1069 temp_stack.reg[j] = temp;
1072 /* Place operand 2 next on the stack. */
1073 regno = get_hard_regnum (&temp_stack, src2);
1074 if (regno < 0)
1075 abort ();
1076 if (regno != FIRST_STACK_REG + 1)
1078 k = temp_stack.top - (regno - FIRST_STACK_REG);
1079 j = temp_stack.top - 1;
1081 temp = temp_stack.reg[k];
1082 temp_stack.reg[k] = temp_stack.reg[j];
1083 temp_stack.reg[j] = temp;
1086 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
1089 /* Handle a move to or from a stack register in PAT, which is in INSN.
1090 REGSTACK is the current stack. Return whether a control flow insn
1091 was deleted in the process. */
1093 static bool
1094 move_for_stack_reg (rtx insn, stack regstack, rtx pat)
1096 rtx *psrc = get_true_reg (&SET_SRC (pat));
1097 rtx *pdest = get_true_reg (&SET_DEST (pat));
1098 rtx src, dest;
1099 rtx note;
1100 bool control_flow_insn_deleted = false;
1102 src = *psrc; dest = *pdest;
1104 if (STACK_REG_P (src) && STACK_REG_P (dest))
1106 /* Write from one stack reg to another. If SRC dies here, then
1107 just change the register mapping and delete the insn. */
1109 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1110 if (note)
1112 int i;
1114 /* If this is a no-op move, there must not be a REG_DEAD note. */
1115 if (REGNO (src) == REGNO (dest))
1116 abort ();
1118 for (i = regstack->top; i >= 0; i--)
1119 if (regstack->reg[i] == REGNO (src))
1120 break;
1122 /* The source must be live, and the dest must be dead. */
1123 if (i < 0 || get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1124 abort ();
1126 /* It is possible that the dest is unused after this insn.
1127 If so, just pop the src. */
1129 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1130 emit_pop_insn (insn, regstack, src, EMIT_AFTER);
1131 else
1133 regstack->reg[i] = REGNO (dest);
1134 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1135 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1138 control_flow_insn_deleted |= control_flow_insn_p (insn);
1139 delete_insn (insn);
1140 return control_flow_insn_deleted;
1143 /* The source reg does not die. */
1145 /* If this appears to be a no-op move, delete it, or else it
1146 will confuse the machine description output patterns. But if
1147 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1148 for REG_UNUSED will not work for deleted insns. */
1150 if (REGNO (src) == REGNO (dest))
1152 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1153 emit_pop_insn (insn, regstack, dest, EMIT_AFTER);
1155 control_flow_insn_deleted |= control_flow_insn_p (insn);
1156 delete_insn (insn);
1157 return control_flow_insn_deleted;
1160 /* The destination ought to be dead. */
1161 if (get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1162 abort ();
1164 replace_reg (psrc, get_hard_regnum (regstack, src));
1166 regstack->reg[++regstack->top] = REGNO (dest);
1167 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1168 replace_reg (pdest, FIRST_STACK_REG);
1170 else if (STACK_REG_P (src))
1172 /* Save from a stack reg to MEM, or possibly integer reg. Since
1173 only top of stack may be saved, emit an exchange first if
1174 needs be. */
1176 emit_swap_insn (insn, regstack, src);
1178 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1179 if (note)
1181 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1182 regstack->top--;
1183 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1185 else if ((GET_MODE (src) == XFmode)
1186 && regstack->top < REG_STACK_SIZE - 1)
1188 /* A 387 cannot write an XFmode value to a MEM without
1189 clobbering the source reg. The output code can handle
1190 this by reading back the value from the MEM.
1191 But it is more efficient to use a temp register if one is
1192 available. Push the source value here if the register
1193 stack is not full, and then write the value to memory via
1194 a pop. */
1195 rtx push_rtx, push_insn;
1196 rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, GET_MODE (src));
1198 push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
1199 push_insn = emit_insn_before (push_rtx, insn);
1200 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD, top_stack_reg,
1201 REG_NOTES (insn));
1204 replace_reg (psrc, FIRST_STACK_REG);
1206 else if (STACK_REG_P (dest))
1208 /* Load from MEM, or possibly integer REG or constant, into the
1209 stack regs. The actual target is always the top of the
1210 stack. The stack mapping is changed to reflect that DEST is
1211 now at top of stack. */
1213 /* The destination ought to be dead. */
1214 if (get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1215 abort ();
1217 if (regstack->top >= REG_STACK_SIZE)
1218 abort ();
1220 regstack->reg[++regstack->top] = REGNO (dest);
1221 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1222 replace_reg (pdest, FIRST_STACK_REG);
1224 else
1225 abort ();
1227 return control_flow_insn_deleted;
1230 /* Swap the condition on a branch, if there is one. Return true if we
1231 found a condition to swap. False if the condition was not used as
1232 such. */
1234 static int
1235 swap_rtx_condition_1 (rtx pat)
1237 const char *fmt;
1238 int i, r = 0;
1240 if (COMPARISON_P (pat))
1242 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1243 r = 1;
1245 else
1247 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1248 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1250 if (fmt[i] == 'E')
1252 int j;
1254 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1255 r |= swap_rtx_condition_1 (XVECEXP (pat, i, j));
1257 else if (fmt[i] == 'e')
1258 r |= swap_rtx_condition_1 (XEXP (pat, i));
1262 return r;
1265 static int
1266 swap_rtx_condition (rtx insn)
1268 rtx pat = PATTERN (insn);
1270 /* We're looking for a single set to cc0 or an HImode temporary. */
1272 if (GET_CODE (pat) == SET
1273 && REG_P (SET_DEST (pat))
1274 && REGNO (SET_DEST (pat)) == FLAGS_REG)
1276 insn = next_flags_user (insn);
1277 if (insn == NULL_RTX)
1278 return 0;
1279 pat = PATTERN (insn);
1282 /* See if this is, or ends in, a fnstsw, aka unspec 9. If so, we're
1283 not doing anything with the cc value right now. We may be able to
1284 search for one though. */
1286 if (GET_CODE (pat) == SET
1287 && GET_CODE (SET_SRC (pat)) == UNSPEC
1288 && XINT (SET_SRC (pat), 1) == UNSPEC_FNSTSW)
1290 rtx dest = SET_DEST (pat);
1292 /* Search forward looking for the first use of this value.
1293 Stop at block boundaries. */
1294 while (insn != BB_END (current_block))
1296 insn = NEXT_INSN (insn);
1297 if (INSN_P (insn) && reg_mentioned_p (dest, insn))
1298 break;
1299 if (CALL_P (insn))
1300 return 0;
1303 /* So we've found the insn using this value. If it is anything
1304 other than sahf, aka unspec 10, or the value does not die
1305 (meaning we'd have to search further), then we must give up. */
1306 pat = PATTERN (insn);
1307 if (GET_CODE (pat) != SET
1308 || GET_CODE (SET_SRC (pat)) != UNSPEC
1309 || XINT (SET_SRC (pat), 1) != UNSPEC_SAHF
1310 || ! dead_or_set_p (insn, dest))
1311 return 0;
1313 /* Now we are prepared to handle this as a normal cc0 setter. */
1314 insn = next_flags_user (insn);
1315 if (insn == NULL_RTX)
1316 return 0;
1317 pat = PATTERN (insn);
1320 if (swap_rtx_condition_1 (pat))
1322 int fail = 0;
1323 INSN_CODE (insn) = -1;
1324 if (recog_memoized (insn) == -1)
1325 fail = 1;
1326 /* In case the flags don't die here, recurse to try fix
1327 following user too. */
1328 else if (! dead_or_set_p (insn, ix86_flags_rtx))
1330 insn = next_flags_user (insn);
1331 if (!insn || !swap_rtx_condition (insn))
1332 fail = 1;
1334 if (fail)
1336 swap_rtx_condition_1 (pat);
1337 return 0;
1339 return 1;
1341 return 0;
1344 /* Handle a comparison. Special care needs to be taken to avoid
1345 causing comparisons that a 387 cannot do correctly, such as EQ.
1347 Also, a pop insn may need to be emitted. The 387 does have an
1348 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1349 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1350 set up. */
1352 static void
1353 compare_for_stack_reg (rtx insn, stack regstack, rtx pat_src)
1355 rtx *src1, *src2;
1356 rtx src1_note, src2_note;
1357 rtx flags_user;
1359 src1 = get_true_reg (&XEXP (pat_src, 0));
1360 src2 = get_true_reg (&XEXP (pat_src, 1));
1361 flags_user = next_flags_user (insn);
1363 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1364 registers that die in this insn - move those to stack top first. */
1365 if ((! STACK_REG_P (*src1)
1366 || (STACK_REG_P (*src2)
1367 && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
1368 && swap_rtx_condition (insn))
1370 rtx temp;
1371 temp = XEXP (pat_src, 0);
1372 XEXP (pat_src, 0) = XEXP (pat_src, 1);
1373 XEXP (pat_src, 1) = temp;
1375 src1 = get_true_reg (&XEXP (pat_src, 0));
1376 src2 = get_true_reg (&XEXP (pat_src, 1));
1378 INSN_CODE (insn) = -1;
1381 /* We will fix any death note later. */
1383 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1385 if (STACK_REG_P (*src2))
1386 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1387 else
1388 src2_note = NULL_RTX;
1390 emit_swap_insn (insn, regstack, *src1);
1392 replace_reg (src1, FIRST_STACK_REG);
1394 if (STACK_REG_P (*src2))
1395 replace_reg (src2, get_hard_regnum (regstack, *src2));
1397 if (src1_note)
1399 pop_stack (regstack, REGNO (XEXP (src1_note, 0)));
1400 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1403 /* If the second operand dies, handle that. But if the operands are
1404 the same stack register, don't bother, because only one death is
1405 needed, and it was just handled. */
1407 if (src2_note
1408 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
1409 && REGNO (*src1) == REGNO (*src2)))
1411 /* As a special case, two regs may die in this insn if src2 is
1412 next to top of stack and the top of stack also dies. Since
1413 we have already popped src1, "next to top of stack" is really
1414 at top (FIRST_STACK_REG) now. */
1416 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1417 && src1_note)
1419 pop_stack (regstack, REGNO (XEXP (src2_note, 0)));
1420 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1422 else
1424 /* The 386 can only represent death of the first operand in
1425 the case handled above. In all other cases, emit a separate
1426 pop and remove the death note from here. */
1428 /* link_cc0_insns (insn); */
1430 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
1432 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
1433 EMIT_AFTER);
1438 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1439 is the current register layout. Return whether a control flow insn
1440 was deleted in the process. */
1442 static bool
1443 subst_stack_regs_pat (rtx insn, stack regstack, rtx pat)
1445 rtx *dest, *src;
1446 bool control_flow_insn_deleted = false;
1448 switch (GET_CODE (pat))
1450 case USE:
1451 /* Deaths in USE insns can happen in non optimizing compilation.
1452 Handle them by popping the dying register. */
1453 src = get_true_reg (&XEXP (pat, 0));
1454 if (STACK_REG_P (*src)
1455 && find_regno_note (insn, REG_DEAD, REGNO (*src)))
1457 emit_pop_insn (insn, regstack, *src, EMIT_AFTER);
1458 return control_flow_insn_deleted;
1460 /* ??? Uninitialized USE should not happen. */
1461 else if (get_hard_regnum (regstack, *src) == -1)
1462 abort ();
1463 break;
1465 case CLOBBER:
1467 rtx note;
1469 dest = get_true_reg (&XEXP (pat, 0));
1470 if (STACK_REG_P (*dest))
1472 note = find_reg_note (insn, REG_DEAD, *dest);
1474 if (pat != PATTERN (insn))
1476 /* The fix_truncdi_1 pattern wants to be able to allocate
1477 it's own scratch register. It does this by clobbering
1478 an fp reg so that it is assured of an empty reg-stack
1479 register. If the register is live, kill it now.
1480 Remove the DEAD/UNUSED note so we don't try to kill it
1481 later too. */
1483 if (note)
1484 emit_pop_insn (insn, regstack, *dest, EMIT_BEFORE);
1485 else
1487 note = find_reg_note (insn, REG_UNUSED, *dest);
1488 if (!note)
1489 abort ();
1491 remove_note (insn, note);
1492 replace_reg (dest, FIRST_STACK_REG + 1);
1494 else
1496 /* A top-level clobber with no REG_DEAD, and no hard-regnum
1497 indicates an uninitialized value. Because reload removed
1498 all other clobbers, this must be due to a function
1499 returning without a value. Load up a NaN. */
1501 if (! note
1502 && get_hard_regnum (regstack, *dest) == -1)
1504 pat = gen_rtx_SET (VOIDmode,
1505 FP_MODE_REG (REGNO (*dest), SFmode),
1506 not_a_num);
1507 PATTERN (insn) = pat;
1508 control_flow_insn_deleted |= move_for_stack_reg (insn, regstack, pat);
1510 if (! note && COMPLEX_MODE_P (GET_MODE (*dest))
1511 && get_hard_regnum (regstack, FP_MODE_REG (REGNO (*dest), DFmode)) == -1)
1513 pat = gen_rtx_SET (VOIDmode,
1514 FP_MODE_REG (REGNO (*dest) + 1, SFmode),
1515 not_a_num);
1516 PATTERN (insn) = pat;
1517 control_flow_insn_deleted |= move_for_stack_reg (insn, regstack, pat);
1521 break;
1524 case SET:
1526 rtx *src1 = (rtx *) 0, *src2;
1527 rtx src1_note, src2_note;
1528 rtx pat_src;
1530 dest = get_true_reg (&SET_DEST (pat));
1531 src = get_true_reg (&SET_SRC (pat));
1532 pat_src = SET_SRC (pat);
1534 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1535 if (STACK_REG_P (*src)
1536 || (STACK_REG_P (*dest)
1537 && (REG_P (*src) || MEM_P (*src)
1538 || GET_CODE (*src) == CONST_DOUBLE)))
1540 control_flow_insn_deleted |= move_for_stack_reg (insn, regstack, pat);
1541 break;
1544 switch (GET_CODE (pat_src))
1546 case COMPARE:
1547 compare_for_stack_reg (insn, regstack, pat_src);
1548 break;
1550 case CALL:
1552 int count;
1553 for (count = hard_regno_nregs[REGNO (*dest)][GET_MODE (*dest)];
1554 --count >= 0;)
1556 regstack->reg[++regstack->top] = REGNO (*dest) + count;
1557 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
1560 replace_reg (dest, FIRST_STACK_REG);
1561 break;
1563 case REG:
1564 /* This is a `tstM2' case. */
1565 if (*dest != cc0_rtx)
1566 abort ();
1567 src1 = src;
1569 /* Fall through. */
1571 case FLOAT_TRUNCATE:
1572 case SQRT:
1573 case ABS:
1574 case NEG:
1575 /* These insns only operate on the top of the stack. DEST might
1576 be cc0_rtx if we're processing a tstM pattern. Also, it's
1577 possible that the tstM case results in a REG_DEAD note on the
1578 source. */
1580 if (src1 == 0)
1581 src1 = get_true_reg (&XEXP (pat_src, 0));
1583 emit_swap_insn (insn, regstack, *src1);
1585 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1587 if (STACK_REG_P (*dest))
1588 replace_reg (dest, FIRST_STACK_REG);
1590 if (src1_note)
1592 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1593 regstack->top--;
1594 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1597 replace_reg (src1, FIRST_STACK_REG);
1598 break;
1600 case MINUS:
1601 case DIV:
1602 /* On i386, reversed forms of subM3 and divM3 exist for
1603 MODE_FLOAT, so the same code that works for addM3 and mulM3
1604 can be used. */
1605 case MULT:
1606 case PLUS:
1607 /* These insns can accept the top of stack as a destination
1608 from a stack reg or mem, or can use the top of stack as a
1609 source and some other stack register (possibly top of stack)
1610 as a destination. */
1612 src1 = get_true_reg (&XEXP (pat_src, 0));
1613 src2 = get_true_reg (&XEXP (pat_src, 1));
1615 /* We will fix any death note later. */
1617 if (STACK_REG_P (*src1))
1618 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1619 else
1620 src1_note = NULL_RTX;
1621 if (STACK_REG_P (*src2))
1622 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1623 else
1624 src2_note = NULL_RTX;
1626 /* If either operand is not a stack register, then the dest
1627 must be top of stack. */
1629 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
1630 emit_swap_insn (insn, regstack, *dest);
1631 else
1633 /* Both operands are REG. If neither operand is already
1634 at the top of stack, choose to make the one that is the dest
1635 the new top of stack. */
1637 int src1_hard_regnum, src2_hard_regnum;
1639 src1_hard_regnum = get_hard_regnum (regstack, *src1);
1640 src2_hard_regnum = get_hard_regnum (regstack, *src2);
1641 if (src1_hard_regnum == -1 || src2_hard_regnum == -1)
1642 abort ();
1644 if (src1_hard_regnum != FIRST_STACK_REG
1645 && src2_hard_regnum != FIRST_STACK_REG)
1646 emit_swap_insn (insn, regstack, *dest);
1649 if (STACK_REG_P (*src1))
1650 replace_reg (src1, get_hard_regnum (regstack, *src1));
1651 if (STACK_REG_P (*src2))
1652 replace_reg (src2, get_hard_regnum (regstack, *src2));
1654 if (src1_note)
1656 rtx src1_reg = XEXP (src1_note, 0);
1658 /* If the register that dies is at the top of stack, then
1659 the destination is somewhere else - merely substitute it.
1660 But if the reg that dies is not at top of stack, then
1661 move the top of stack to the dead reg, as though we had
1662 done the insn and then a store-with-pop. */
1664 if (REGNO (src1_reg) == regstack->reg[regstack->top])
1666 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1667 replace_reg (dest, get_hard_regnum (regstack, *dest));
1669 else
1671 int regno = get_hard_regnum (regstack, src1_reg);
1673 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1674 replace_reg (dest, regno);
1676 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1677 = regstack->reg[regstack->top];
1680 CLEAR_HARD_REG_BIT (regstack->reg_set,
1681 REGNO (XEXP (src1_note, 0)));
1682 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1683 regstack->top--;
1685 else if (src2_note)
1687 rtx src2_reg = XEXP (src2_note, 0);
1688 if (REGNO (src2_reg) == regstack->reg[regstack->top])
1690 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1691 replace_reg (dest, get_hard_regnum (regstack, *dest));
1693 else
1695 int regno = get_hard_regnum (regstack, src2_reg);
1697 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1698 replace_reg (dest, regno);
1700 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1701 = regstack->reg[regstack->top];
1704 CLEAR_HARD_REG_BIT (regstack->reg_set,
1705 REGNO (XEXP (src2_note, 0)));
1706 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
1707 regstack->top--;
1709 else
1711 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1712 replace_reg (dest, get_hard_regnum (regstack, *dest));
1715 /* Keep operand 1 matching with destination. */
1716 if (COMMUTATIVE_ARITH_P (pat_src)
1717 && REG_P (*src1) && REG_P (*src2)
1718 && REGNO (*src1) != REGNO (*dest))
1720 int tmp = REGNO (*src1);
1721 replace_reg (src1, REGNO (*src2));
1722 replace_reg (src2, tmp);
1724 break;
1726 case UNSPEC:
1727 switch (XINT (pat_src, 1))
1729 case UNSPEC_SIN:
1730 case UNSPEC_COS:
1731 case UNSPEC_FRNDINT:
1732 case UNSPEC_F2XM1:
1733 /* These insns only operate on the top of the stack. */
1735 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1737 emit_swap_insn (insn, regstack, *src1);
1739 /* Input should never die, it is
1740 replaced with output. */
1741 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1742 if (src1_note)
1743 abort();
1745 if (STACK_REG_P (*dest))
1746 replace_reg (dest, FIRST_STACK_REG);
1748 replace_reg (src1, FIRST_STACK_REG);
1749 break;
1751 case UNSPEC_FPATAN:
1752 case UNSPEC_FYL2X:
1753 case UNSPEC_FYL2XP1:
1754 /* These insns operate on the top two stack slots. */
1756 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1757 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1759 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1760 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1762 swap_to_top (insn, regstack, *src1, *src2);
1764 replace_reg (src1, FIRST_STACK_REG);
1765 replace_reg (src2, FIRST_STACK_REG + 1);
1767 if (src1_note)
1768 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1769 if (src2_note)
1770 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1772 /* Pop both input operands from the stack. */
1773 CLEAR_HARD_REG_BIT (regstack->reg_set,
1774 regstack->reg[regstack->top]);
1775 CLEAR_HARD_REG_BIT (regstack->reg_set,
1776 regstack->reg[regstack->top - 1]);
1777 regstack->top -= 2;
1779 /* Push the result back onto the stack. */
1780 regstack->reg[++regstack->top] = REGNO (*dest);
1781 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1782 replace_reg (dest, FIRST_STACK_REG);
1783 break;
1785 case UNSPEC_FSCALE_FRACT:
1786 case UNSPEC_FPREM_F:
1787 case UNSPEC_FPREM1_F:
1788 /* These insns operate on the top two stack slots.
1789 first part of double input, double output insn. */
1791 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1792 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1794 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1795 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1797 /* Inputs should never die, they are
1798 replaced with outputs. */
1799 if ((src1_note) || (src2_note))
1800 abort();
1802 swap_to_top (insn, regstack, *src1, *src2);
1804 /* Push the result back onto stack. Empty stack slot
1805 will be filled in second part of insn. */
1806 if (STACK_REG_P (*dest)) {
1807 regstack->reg[regstack->top] = REGNO (*dest);
1808 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1809 replace_reg (dest, FIRST_STACK_REG);
1812 replace_reg (src1, FIRST_STACK_REG);
1813 replace_reg (src2, FIRST_STACK_REG + 1);
1814 break;
1816 case UNSPEC_FSCALE_EXP:
1817 case UNSPEC_FPREM_U:
1818 case UNSPEC_FPREM1_U:
1819 /* These insns operate on the top two stack slots./
1820 second part of double input, double output insn. */
1822 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1823 src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
1825 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1826 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1828 /* Inputs should never die, they are
1829 replaced with outputs. */
1830 if ((src1_note) || (src2_note))
1831 abort();
1833 swap_to_top (insn, regstack, *src1, *src2);
1835 /* Push the result back onto stack. Fill empty slot from
1836 first part of insn and fix top of stack pointer. */
1837 if (STACK_REG_P (*dest)) {
1838 regstack->reg[regstack->top - 1] = REGNO (*dest);
1839 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1840 replace_reg (dest, FIRST_STACK_REG + 1);
1843 replace_reg (src1, FIRST_STACK_REG);
1844 replace_reg (src2, FIRST_STACK_REG + 1);
1845 break;
1847 case UNSPEC_SINCOS_COS:
1848 case UNSPEC_TAN_ONE:
1849 case UNSPEC_XTRACT_FRACT:
1850 /* These insns operate on the top two stack slots,
1851 first part of one input, double output insn. */
1853 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1855 emit_swap_insn (insn, regstack, *src1);
1857 /* Input should never die, it is
1858 replaced with output. */
1859 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1860 if (src1_note)
1861 abort();
1863 /* Push the result back onto stack. Empty stack slot
1864 will be filled in second part of insn. */
1865 if (STACK_REG_P (*dest)) {
1866 regstack->reg[regstack->top + 1] = REGNO (*dest);
1867 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1868 replace_reg (dest, FIRST_STACK_REG);
1871 replace_reg (src1, FIRST_STACK_REG);
1872 break;
1874 case UNSPEC_SINCOS_SIN:
1875 case UNSPEC_TAN_TAN:
1876 case UNSPEC_XTRACT_EXP:
1877 /* These insns operate on the top two stack slots,
1878 second part of one input, double output insn. */
1880 src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
1882 emit_swap_insn (insn, regstack, *src1);
1884 /* Input should never die, it is
1885 replaced with output. */
1886 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1887 if (src1_note)
1888 abort();
1890 /* Push the result back onto stack. Fill empty slot from
1891 first part of insn and fix top of stack pointer. */
1892 if (STACK_REG_P (*dest)) {
1893 regstack->reg[regstack->top] = REGNO (*dest);
1894 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1895 replace_reg (dest, FIRST_STACK_REG + 1);
1897 regstack->top++;
1900 replace_reg (src1, FIRST_STACK_REG);
1901 break;
1903 case UNSPEC_SAHF:
1904 /* (unspec [(unspec [(compare)] UNSPEC_FNSTSW)] UNSPEC_SAHF)
1905 The combination matches the PPRO fcomi instruction. */
1907 pat_src = XVECEXP (pat_src, 0, 0);
1908 if (GET_CODE (pat_src) != UNSPEC
1909 || XINT (pat_src, 1) != UNSPEC_FNSTSW)
1910 abort ();
1911 /* Fall through. */
1913 case UNSPEC_FNSTSW:
1914 /* Combined fcomp+fnstsw generated for doing well with
1915 CSE. When optimizing this would have been broken
1916 up before now. */
1918 pat_src = XVECEXP (pat_src, 0, 0);
1919 if (GET_CODE (pat_src) != COMPARE)
1920 abort ();
1922 compare_for_stack_reg (insn, regstack, pat_src);
1923 break;
1925 default:
1926 abort ();
1928 break;
1930 case IF_THEN_ELSE:
1931 /* This insn requires the top of stack to be the destination. */
1933 src1 = get_true_reg (&XEXP (pat_src, 1));
1934 src2 = get_true_reg (&XEXP (pat_src, 2));
1936 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1937 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1939 /* If the comparison operator is an FP comparison operator,
1940 it is handled correctly by compare_for_stack_reg () who
1941 will move the destination to the top of stack. But if the
1942 comparison operator is not an FP comparison operator, we
1943 have to handle it here. */
1944 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
1945 && REGNO (*dest) != regstack->reg[regstack->top])
1947 /* In case one of operands is the top of stack and the operands
1948 dies, it is safe to make it the destination operand by
1949 reversing the direction of cmove and avoid fxch. */
1950 if ((REGNO (*src1) == regstack->reg[regstack->top]
1951 && src1_note)
1952 || (REGNO (*src2) == regstack->reg[regstack->top]
1953 && src2_note))
1955 int idx1 = (get_hard_regnum (regstack, *src1)
1956 - FIRST_STACK_REG);
1957 int idx2 = (get_hard_regnum (regstack, *src2)
1958 - FIRST_STACK_REG);
1960 /* Make reg-stack believe that the operands are already
1961 swapped on the stack */
1962 regstack->reg[regstack->top - idx1] = REGNO (*src2);
1963 regstack->reg[regstack->top - idx2] = REGNO (*src1);
1965 /* Reverse condition to compensate the operand swap.
1966 i386 do have comparison always reversible. */
1967 PUT_CODE (XEXP (pat_src, 0),
1968 reversed_comparison_code (XEXP (pat_src, 0), insn));
1970 else
1971 emit_swap_insn (insn, regstack, *dest);
1975 rtx src_note [3];
1976 int i;
1978 src_note[0] = 0;
1979 src_note[1] = src1_note;
1980 src_note[2] = src2_note;
1982 if (STACK_REG_P (*src1))
1983 replace_reg (src1, get_hard_regnum (regstack, *src1));
1984 if (STACK_REG_P (*src2))
1985 replace_reg (src2, get_hard_regnum (regstack, *src2));
1987 for (i = 1; i <= 2; i++)
1988 if (src_note [i])
1990 int regno = REGNO (XEXP (src_note[i], 0));
1992 /* If the register that dies is not at the top of
1993 stack, then move the top of stack to the dead reg */
1994 if (regno != regstack->reg[regstack->top])
1996 remove_regno_note (insn, REG_DEAD, regno);
1997 emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
1998 EMIT_AFTER);
2000 else
2001 /* Top of stack never dies, as it is the
2002 destination. */
2003 abort ();
2007 /* Make dest the top of stack. Add dest to regstack if
2008 not present. */
2009 if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
2010 regstack->reg[++regstack->top] = REGNO (*dest);
2011 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2012 replace_reg (dest, FIRST_STACK_REG);
2013 break;
2015 default:
2016 abort ();
2018 break;
2021 default:
2022 break;
2025 return control_flow_insn_deleted;
2028 /* Substitute hard regnums for any stack regs in INSN, which has
2029 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
2030 before the insn, and is updated with changes made here.
2032 There are several requirements and assumptions about the use of
2033 stack-like regs in asm statements. These rules are enforced by
2034 record_asm_stack_regs; see comments there for details. Any
2035 asm_operands left in the RTL at this point may be assume to meet the
2036 requirements, since record_asm_stack_regs removes any problem asm. */
2038 static void
2039 subst_asm_stack_regs (rtx insn, stack regstack)
2041 rtx body = PATTERN (insn);
2042 int alt;
2044 rtx *note_reg; /* Array of note contents */
2045 rtx **note_loc; /* Address of REG field of each note */
2046 enum reg_note *note_kind; /* The type of each note */
2048 rtx *clobber_reg = 0;
2049 rtx **clobber_loc = 0;
2051 struct stack_def temp_stack;
2052 int n_notes;
2053 int n_clobbers;
2054 rtx note;
2055 int i;
2056 int n_inputs, n_outputs;
2058 if (! check_asm_stack_operands (insn))
2059 return;
2061 /* Find out what the constraints required. If no constraint
2062 alternative matches, that is a compiler bug: we should have caught
2063 such an insn in check_asm_stack_operands. */
2064 extract_insn (insn);
2065 constrain_operands (1);
2066 alt = which_alternative;
2068 preprocess_constraints ();
2070 n_inputs = get_asm_operand_n_inputs (body);
2071 n_outputs = recog_data.n_operands - n_inputs;
2073 if (alt < 0)
2074 abort ();
2076 /* Strip SUBREGs here to make the following code simpler. */
2077 for (i = 0; i < recog_data.n_operands; i++)
2078 if (GET_CODE (recog_data.operand[i]) == SUBREG
2079 && REG_P (SUBREG_REG (recog_data.operand[i])))
2081 recog_data.operand_loc[i] = & SUBREG_REG (recog_data.operand[i]);
2082 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
2085 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
2087 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
2088 i++;
2090 note_reg = alloca (i * sizeof (rtx));
2091 note_loc = alloca (i * sizeof (rtx *));
2092 note_kind = alloca (i * sizeof (enum reg_note));
2094 n_notes = 0;
2095 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2097 rtx reg = XEXP (note, 0);
2098 rtx *loc = & XEXP (note, 0);
2100 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
2102 loc = & SUBREG_REG (reg);
2103 reg = SUBREG_REG (reg);
2106 if (STACK_REG_P (reg)
2107 && (REG_NOTE_KIND (note) == REG_DEAD
2108 || REG_NOTE_KIND (note) == REG_UNUSED))
2110 note_reg[n_notes] = reg;
2111 note_loc[n_notes] = loc;
2112 note_kind[n_notes] = REG_NOTE_KIND (note);
2113 n_notes++;
2117 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2119 n_clobbers = 0;
2121 if (GET_CODE (body) == PARALLEL)
2123 clobber_reg = alloca (XVECLEN (body, 0) * sizeof (rtx));
2124 clobber_loc = alloca (XVECLEN (body, 0) * sizeof (rtx *));
2126 for (i = 0; i < XVECLEN (body, 0); i++)
2127 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
2129 rtx clobber = XVECEXP (body, 0, i);
2130 rtx reg = XEXP (clobber, 0);
2131 rtx *loc = & XEXP (clobber, 0);
2133 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
2135 loc = & SUBREG_REG (reg);
2136 reg = SUBREG_REG (reg);
2139 if (STACK_REG_P (reg))
2141 clobber_reg[n_clobbers] = reg;
2142 clobber_loc[n_clobbers] = loc;
2143 n_clobbers++;
2148 temp_stack = *regstack;
2150 /* Put the input regs into the desired place in TEMP_STACK. */
2152 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2153 if (STACK_REG_P (recog_data.operand[i])
2154 && reg_class_subset_p (recog_op_alt[i][alt].cl,
2155 FLOAT_REGS)
2156 && recog_op_alt[i][alt].cl != FLOAT_REGS)
2158 /* If an operand needs to be in a particular reg in
2159 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2160 these constraints are for single register classes, and
2161 reload guaranteed that operand[i] is already in that class,
2162 we can just use REGNO (recog_data.operand[i]) to know which
2163 actual reg this operand needs to be in. */
2165 int regno = get_hard_regnum (&temp_stack, recog_data.operand[i]);
2167 if (regno < 0)
2168 abort ();
2170 if ((unsigned int) regno != REGNO (recog_data.operand[i]))
2172 /* recog_data.operand[i] is not in the right place. Find
2173 it and swap it with whatever is already in I's place.
2174 K is where recog_data.operand[i] is now. J is where it
2175 should be. */
2176 int j, k, temp;
2178 k = temp_stack.top - (regno - FIRST_STACK_REG);
2179 j = (temp_stack.top
2180 - (REGNO (recog_data.operand[i]) - FIRST_STACK_REG));
2182 temp = temp_stack.reg[k];
2183 temp_stack.reg[k] = temp_stack.reg[j];
2184 temp_stack.reg[j] = temp;
2188 /* Emit insns before INSN to make sure the reg-stack is in the right
2189 order. */
2191 change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
2193 /* Make the needed input register substitutions. Do death notes and
2194 clobbers too, because these are for inputs, not outputs. */
2196 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2197 if (STACK_REG_P (recog_data.operand[i]))
2199 int regnum = get_hard_regnum (regstack, recog_data.operand[i]);
2201 if (regnum < 0)
2202 abort ();
2204 replace_reg (recog_data.operand_loc[i], regnum);
2207 for (i = 0; i < n_notes; i++)
2208 if (note_kind[i] == REG_DEAD)
2210 int regnum = get_hard_regnum (regstack, note_reg[i]);
2212 if (regnum < 0)
2213 abort ();
2215 replace_reg (note_loc[i], regnum);
2218 for (i = 0; i < n_clobbers; i++)
2220 /* It's OK for a CLOBBER to reference a reg that is not live.
2221 Don't try to replace it in that case. */
2222 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2224 if (regnum >= 0)
2226 /* Sigh - clobbers always have QImode. But replace_reg knows
2227 that these regs can't be MODE_INT and will abort. Just put
2228 the right reg there without calling replace_reg. */
2230 *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
2234 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2236 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2237 if (STACK_REG_P (recog_data.operand[i]))
2239 /* An input reg is implicitly popped if it is tied to an
2240 output, or if there is a CLOBBER for it. */
2241 int j;
2243 for (j = 0; j < n_clobbers; j++)
2244 if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
2245 break;
2247 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
2249 /* recog_data.operand[i] might not be at the top of stack.
2250 But that's OK, because all we need to do is pop the
2251 right number of regs off of the top of the reg-stack.
2252 record_asm_stack_regs guaranteed that all implicitly
2253 popped regs were grouped at the top of the reg-stack. */
2255 CLEAR_HARD_REG_BIT (regstack->reg_set,
2256 regstack->reg[regstack->top]);
2257 regstack->top--;
2261 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2262 Note that there isn't any need to substitute register numbers.
2263 ??? Explain why this is true. */
2265 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2267 /* See if there is an output for this hard reg. */
2268 int j;
2270 for (j = 0; j < n_outputs; j++)
2271 if (STACK_REG_P (recog_data.operand[j])
2272 && REGNO (recog_data.operand[j]) == (unsigned) i)
2274 regstack->reg[++regstack->top] = i;
2275 SET_HARD_REG_BIT (regstack->reg_set, i);
2276 break;
2280 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2281 input that the asm didn't implicitly pop. If the asm didn't
2282 implicitly pop an input reg, that reg will still be live.
2284 Note that we can't use find_regno_note here: the register numbers
2285 in the death notes have already been substituted. */
2287 for (i = 0; i < n_outputs; i++)
2288 if (STACK_REG_P (recog_data.operand[i]))
2290 int j;
2292 for (j = 0; j < n_notes; j++)
2293 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2294 && note_kind[j] == REG_UNUSED)
2296 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2297 EMIT_AFTER);
2298 break;
2302 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2303 if (STACK_REG_P (recog_data.operand[i]))
2305 int j;
2307 for (j = 0; j < n_notes; j++)
2308 if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
2309 && note_kind[j] == REG_DEAD
2310 && TEST_HARD_REG_BIT (regstack->reg_set,
2311 REGNO (recog_data.operand[i])))
2313 insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
2314 EMIT_AFTER);
2315 break;
2320 /* Substitute stack hard reg numbers for stack virtual registers in
2321 INSN. Non-stack register numbers are not changed. REGSTACK is the
2322 current stack content. Insns may be emitted as needed to arrange the
2323 stack for the 387 based on the contents of the insn. Return whether
2324 a control flow insn was deleted in the process. */
2326 static bool
2327 subst_stack_regs (rtx insn, stack regstack)
2329 rtx *note_link, note;
2330 bool control_flow_insn_deleted = false;
2331 int i;
2333 if (CALL_P (insn))
2335 int top = regstack->top;
2337 /* If there are any floating point parameters to be passed in
2338 registers for this call, make sure they are in the right
2339 order. */
2341 if (top >= 0)
2343 straighten_stack (PREV_INSN (insn), regstack);
2345 /* Now mark the arguments as dead after the call. */
2347 while (regstack->top >= 0)
2349 CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
2350 regstack->top--;
2355 /* Do the actual substitution if any stack regs are mentioned.
2356 Since we only record whether entire insn mentions stack regs, and
2357 subst_stack_regs_pat only works for patterns that contain stack regs,
2358 we must check each pattern in a parallel here. A call_value_pop could
2359 fail otherwise. */
2361 if (stack_regs_mentioned (insn))
2363 int n_operands = asm_noperands (PATTERN (insn));
2364 if (n_operands >= 0)
2366 /* This insn is an `asm' with operands. Decode the operands,
2367 decide how many are inputs, and do register substitution.
2368 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2370 subst_asm_stack_regs (insn, regstack);
2371 return control_flow_insn_deleted;
2374 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2375 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2377 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2379 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
2380 XVECEXP (PATTERN (insn), 0, i)
2381 = shallow_copy_rtx (XVECEXP (PATTERN (insn), 0, i));
2382 control_flow_insn_deleted
2383 |= subst_stack_regs_pat (insn, regstack,
2384 XVECEXP (PATTERN (insn), 0, i));
2387 else
2388 control_flow_insn_deleted
2389 |= subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2392 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2393 REG_UNUSED will already have been dealt with, so just return. */
2395 if (NOTE_P (insn) || INSN_DELETED_P (insn))
2396 return control_flow_insn_deleted;
2398 /* If there is a REG_UNUSED note on a stack register on this insn,
2399 the indicated reg must be popped. The REG_UNUSED note is removed,
2400 since the form of the newly emitted pop insn references the reg,
2401 making it no longer `unset'. */
2403 note_link = &REG_NOTES (insn);
2404 for (note = *note_link; note; note = XEXP (note, 1))
2405 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2407 *note_link = XEXP (note, 1);
2408 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), EMIT_AFTER);
2410 else
2411 note_link = &XEXP (note, 1);
2413 return control_flow_insn_deleted;
2416 /* Change the organization of the stack so that it fits a new basic
2417 block. Some registers might have to be popped, but there can never be
2418 a register live in the new block that is not now live.
2420 Insert any needed insns before or after INSN, as indicated by
2421 WHERE. OLD is the original stack layout, and NEW is the desired
2422 form. OLD is updated to reflect the code emitted, ie, it will be
2423 the same as NEW upon return.
2425 This function will not preserve block_end[]. But that information
2426 is no longer needed once this has executed. */
2428 static void
2429 change_stack (rtx insn, stack old, stack new, enum emit_where where)
2431 int reg;
2432 int update_end = 0;
2434 /* We will be inserting new insns "backwards". If we are to insert
2435 after INSN, find the next insn, and insert before it. */
2437 if (where == EMIT_AFTER)
2439 if (current_block && BB_END (current_block) == insn)
2440 update_end = 1;
2441 insn = NEXT_INSN (insn);
2444 /* Pop any registers that are not needed in the new block. */
2446 for (reg = old->top; reg >= 0; reg--)
2447 if (! TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2448 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[reg], DFmode),
2449 EMIT_BEFORE);
2451 if (new->top == -2)
2453 /* If the new block has never been processed, then it can inherit
2454 the old stack order. */
2456 new->top = old->top;
2457 memcpy (new->reg, old->reg, sizeof (new->reg));
2459 else
2461 /* This block has been entered before, and we must match the
2462 previously selected stack order. */
2464 /* By now, the only difference should be the order of the stack,
2465 not their depth or liveliness. */
2467 GO_IF_HARD_REG_EQUAL (old->reg_set, new->reg_set, win);
2468 abort ();
2469 win:
2470 if (old->top != new->top)
2471 abort ();
2473 /* If the stack is not empty (new->top != -1), loop here emitting
2474 swaps until the stack is correct.
2476 The worst case number of swaps emitted is N + 2, where N is the
2477 depth of the stack. In some cases, the reg at the top of
2478 stack may be correct, but swapped anyway in order to fix
2479 other regs. But since we never swap any other reg away from
2480 its correct slot, this algorithm will converge. */
2482 if (new->top != -1)
2485 /* Swap the reg at top of stack into the position it is
2486 supposed to be in, until the correct top of stack appears. */
2488 while (old->reg[old->top] != new->reg[new->top])
2490 for (reg = new->top; reg >= 0; reg--)
2491 if (new->reg[reg] == old->reg[old->top])
2492 break;
2494 if (reg == -1)
2495 abort ();
2497 emit_swap_insn (insn, old,
2498 FP_MODE_REG (old->reg[reg], DFmode));
2501 /* See if any regs remain incorrect. If so, bring an
2502 incorrect reg to the top of stack, and let the while loop
2503 above fix it. */
2505 for (reg = new->top; reg >= 0; reg--)
2506 if (new->reg[reg] != old->reg[reg])
2508 emit_swap_insn (insn, old,
2509 FP_MODE_REG (old->reg[reg], DFmode));
2510 break;
2512 } while (reg >= 0);
2514 /* At this point there must be no differences. */
2516 for (reg = old->top; reg >= 0; reg--)
2517 if (old->reg[reg] != new->reg[reg])
2518 abort ();
2521 if (update_end)
2522 BB_END (current_block) = PREV_INSN (insn);
2525 /* Print stack configuration. */
2527 static void
2528 print_stack (FILE *file, stack s)
2530 if (! file)
2531 return;
2533 if (s->top == -2)
2534 fprintf (file, "uninitialized\n");
2535 else if (s->top == -1)
2536 fprintf (file, "empty\n");
2537 else
2539 int i;
2540 fputs ("[ ", file);
2541 for (i = 0; i <= s->top; ++i)
2542 fprintf (file, "%d ", s->reg[i]);
2543 fputs ("]\n", file);
2547 /* This function was doing life analysis. We now let the regular live
2548 code do it's job, so we only need to check some extra invariants
2549 that reg-stack expects. Primary among these being that all registers
2550 are initialized before use.
2552 The function returns true when code was emitted to CFG edges and
2553 commit_edge_insertions needs to be called. */
2555 static int
2556 convert_regs_entry (void)
2558 int inserted = 0;
2559 edge e;
2560 basic_block block;
2562 FOR_EACH_BB_REVERSE (block)
2564 block_info bi = BLOCK_INFO (block);
2565 int reg;
2567 /* Set current register status at last instruction `uninitialized'. */
2568 bi->stack_in.top = -2;
2570 /* Copy live_at_end and live_at_start into temporaries. */
2571 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
2573 if (REGNO_REG_SET_P (block->global_live_at_end, reg))
2574 SET_HARD_REG_BIT (bi->out_reg_set, reg);
2575 if (REGNO_REG_SET_P (block->global_live_at_start, reg))
2576 SET_HARD_REG_BIT (bi->stack_in.reg_set, reg);
2580 /* Load something into each stack register live at function entry.
2581 Such live registers can be caused by uninitialized variables or
2582 functions not returning values on all paths. In order to keep
2583 the push/pop code happy, and to not scrog the register stack, we
2584 must put something in these registers. Use a QNaN.
2586 Note that we are inserting converted code here. This code is
2587 never seen by the convert_regs pass. */
2589 FOR_EACH_EDGE (e, ENTRY_BLOCK_PTR->succs)
2591 basic_block block = e->dest;
2592 block_info bi = BLOCK_INFO (block);
2593 int reg, top = -1;
2595 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2596 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2598 rtx init;
2600 bi->stack_in.reg[++top] = reg;
2602 init = gen_rtx_SET (VOIDmode,
2603 FP_MODE_REG (FIRST_STACK_REG, SFmode),
2604 not_a_num);
2605 insert_insn_on_edge (init, e);
2606 inserted = 1;
2609 bi->stack_in.top = top;
2611 END_FOR_EACH_EDGE;
2613 return inserted;
2616 /* Construct the desired stack for function exit. This will either
2617 be `empty', or the function return value at top-of-stack. */
2619 static void
2620 convert_regs_exit (void)
2622 int value_reg_low, value_reg_high;
2623 stack output_stack;
2624 rtx retvalue;
2626 retvalue = stack_result (current_function_decl);
2627 value_reg_low = value_reg_high = -1;
2628 if (retvalue)
2630 value_reg_low = REGNO (retvalue);
2631 value_reg_high = value_reg_low
2632 + hard_regno_nregs[value_reg_low][GET_MODE (retvalue)] - 1;
2635 output_stack = &BLOCK_INFO (EXIT_BLOCK_PTR)->stack_in;
2636 if (value_reg_low == -1)
2637 output_stack->top = -1;
2638 else
2640 int reg;
2642 output_stack->top = value_reg_high - value_reg_low;
2643 for (reg = value_reg_low; reg <= value_reg_high; ++reg)
2645 output_stack->reg[value_reg_high - reg] = reg;
2646 SET_HARD_REG_BIT (output_stack->reg_set, reg);
2651 /* Adjust the stack of this block on exit to match the stack of the
2652 target block, or copy stack info into the stack of the successor
2653 of the successor hasn't been processed yet. */
2654 static bool
2655 compensate_edge (edge e, FILE *file)
2657 basic_block block = e->src, target = e->dest;
2658 block_info bi = BLOCK_INFO (block);
2659 struct stack_def regstack, tmpstack;
2660 stack target_stack = &BLOCK_INFO (target)->stack_in;
2661 int reg;
2663 current_block = block;
2664 regstack = bi->stack_out;
2665 if (file)
2666 fprintf (file, "Edge %d->%d: ", block->index, target->index);
2668 if (target_stack->top == -2)
2670 /* The target block hasn't had a stack order selected.
2671 We need merely ensure that no pops are needed. */
2672 for (reg = regstack.top; reg >= 0; --reg)
2673 if (!TEST_HARD_REG_BIT (target_stack->reg_set, regstack.reg[reg]))
2674 break;
2676 if (reg == -1)
2678 if (file)
2679 fprintf (file, "new block; copying stack position\n");
2681 /* change_stack kills values in regstack. */
2682 tmpstack = regstack;
2684 change_stack (BB_END (block), &tmpstack, target_stack, EMIT_AFTER);
2685 return false;
2688 if (file)
2689 fprintf (file, "new block; pops needed\n");
2691 else
2693 if (target_stack->top == regstack.top)
2695 for (reg = target_stack->top; reg >= 0; --reg)
2696 if (target_stack->reg[reg] != regstack.reg[reg])
2697 break;
2699 if (reg == -1)
2701 if (file)
2702 fprintf (file, "no changes needed\n");
2703 return false;
2707 if (file)
2709 fprintf (file, "correcting stack to ");
2710 print_stack (file, target_stack);
2714 /* Care for non-call EH edges specially. The normal return path have
2715 values in registers. These will be popped en masse by the unwind
2716 library. */
2717 if ((e->flags & (EDGE_EH | EDGE_ABNORMAL_CALL)) == EDGE_EH)
2718 target_stack->top = -1;
2720 /* Other calls may appear to have values live in st(0), but the
2721 abnormal return path will not have actually loaded the values. */
2722 else if (e->flags & EDGE_ABNORMAL_CALL)
2724 /* Assert that the lifetimes are as we expect -- one value
2725 live at st(0) on the end of the source block, and no
2726 values live at the beginning of the destination block. */
2727 HARD_REG_SET tmp;
2729 CLEAR_HARD_REG_SET (tmp);
2730 GO_IF_HARD_REG_EQUAL (target_stack->reg_set, tmp, eh1);
2731 abort ();
2732 eh1:
2734 /* We are sure that there is st(0) live, otherwise we won't compensate.
2735 For complex return values, we may have st(1) live as well. */
2736 SET_HARD_REG_BIT (tmp, FIRST_STACK_REG);
2737 if (TEST_HARD_REG_BIT (regstack.reg_set, FIRST_STACK_REG + 1))
2738 SET_HARD_REG_BIT (tmp, FIRST_STACK_REG + 1);
2739 GO_IF_HARD_REG_EQUAL (regstack.reg_set, tmp, eh2);
2740 abort ();
2741 eh2:
2743 target_stack->top = -1;
2746 /* It is better to output directly to the end of the block
2747 instead of to the edge, because emit_swap can do minimal
2748 insn scheduling. We can do this when there is only one
2749 edge out, and it is not abnormal. */
2750 else if (EDGE_COUNT (block->succs) == 1 && !(e->flags & EDGE_ABNORMAL))
2752 /* change_stack kills values in regstack. */
2753 tmpstack = regstack;
2755 change_stack (BB_END (block), &tmpstack, target_stack,
2756 (JUMP_P (BB_END (block))
2757 ? EMIT_BEFORE : EMIT_AFTER));
2759 else
2761 rtx seq, after;
2763 /* We don't support abnormal edges. Global takes care to
2764 avoid any live register across them, so we should never
2765 have to insert instructions on such edges. */
2766 if (e->flags & EDGE_ABNORMAL)
2767 abort ();
2769 current_block = NULL;
2770 start_sequence ();
2772 /* ??? change_stack needs some point to emit insns after. */
2773 after = emit_note (NOTE_INSN_DELETED);
2775 tmpstack = regstack;
2776 change_stack (after, &tmpstack, target_stack, EMIT_BEFORE);
2778 seq = get_insns ();
2779 end_sequence ();
2781 insert_insn_on_edge (seq, e);
2782 return true;
2784 return false;
2787 /* Convert stack register references in one block. */
2789 static int
2790 convert_regs_1 (FILE *file, basic_block block)
2792 struct stack_def regstack;
2793 block_info bi = BLOCK_INFO (block);
2794 int deleted, inserted, reg;
2795 rtx insn, next;
2796 edge e, beste = NULL;
2797 bool control_flow_insn_deleted = false;
2799 inserted = 0;
2800 deleted = 0;
2801 any_malformed_asm = false;
2803 /* Find the edge we will copy stack from. It should be the most frequent
2804 one as it will get cheapest after compensation code is generated,
2805 if multiple such exists, take one with largest count, prefer critical
2806 one (as splitting critical edges is more expensive), or one with lowest
2807 index, to avoid random changes with different orders of the edges. */
2808 FOR_EACH_EDGE (e, block->preds)
2810 if (e->flags & EDGE_DFS_BACK)
2812 else if (! beste)
2813 beste = e;
2814 else if (EDGE_FREQUENCY (beste) < EDGE_FREQUENCY (e))
2815 beste = e;
2816 else if (EDGE_FREQUENCY (beste) > EDGE_FREQUENCY (e))
2818 else if (beste->count < e->count)
2819 beste = e;
2820 else if (beste->count > e->count)
2822 else if ((EDGE_CRITICAL_P (e) != 0)
2823 != (EDGE_CRITICAL_P (beste) != 0))
2825 if (EDGE_CRITICAL_P (e))
2826 beste = e;
2828 else if (e->src->index < beste->src->index)
2829 beste = e;
2831 END_FOR_EACH_EDGE;
2833 /* Initialize stack at block entry. */
2834 if (bi->stack_in.top == -2)
2836 if (beste)
2837 inserted |= compensate_edge (beste, file);
2838 else
2840 /* No predecessors. Create an arbitrary input stack. */
2841 int reg;
2843 bi->stack_in.top = -1;
2844 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
2845 if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
2846 bi->stack_in.reg[++bi->stack_in.top] = reg;
2849 else
2850 /* Entry blocks do have stack already initialized. */
2851 beste = NULL;
2853 current_block = block;
2855 if (file)
2857 fprintf (file, "\nBasic block %d\nInput stack: ", block->index);
2858 print_stack (file, &bi->stack_in);
2861 /* Process all insns in this block. Keep track of NEXT so that we
2862 don't process insns emitted while substituting in INSN. */
2863 next = BB_HEAD (block);
2864 regstack = bi->stack_in;
2867 insn = next;
2868 next = NEXT_INSN (insn);
2870 /* Ensure we have not missed a block boundary. */
2871 if (next == NULL)
2872 abort ();
2873 if (insn == BB_END (block))
2874 next = NULL;
2876 /* Don't bother processing unless there is a stack reg
2877 mentioned or if it's a CALL_INSN. */
2878 if (stack_regs_mentioned (insn)
2879 || CALL_P (insn))
2881 if (file)
2883 fprintf (file, " insn %d input stack: ",
2884 INSN_UID (insn));
2885 print_stack (file, &regstack);
2887 control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
2890 while (next);
2892 if (file)
2894 fprintf (file, "Expected live registers [");
2895 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
2896 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg))
2897 fprintf (file, " %d", reg);
2898 fprintf (file, " ]\nOutput stack: ");
2899 print_stack (file, &regstack);
2902 insn = BB_END (block);
2903 if (JUMP_P (insn))
2904 insn = PREV_INSN (insn);
2906 /* If the function is declared to return a value, but it returns one
2907 in only some cases, some registers might come live here. Emit
2908 necessary moves for them. */
2910 for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
2912 if (TEST_HARD_REG_BIT (bi->out_reg_set, reg)
2913 && ! TEST_HARD_REG_BIT (regstack.reg_set, reg))
2915 rtx set;
2917 if (file)
2919 fprintf (file, "Emitting insn initializing reg %d\n",
2920 reg);
2923 set = gen_rtx_SET (VOIDmode, FP_MODE_REG (reg, SFmode),
2924 not_a_num);
2925 insn = emit_insn_after (set, insn);
2926 control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
2930 /* Amongst the insns possibly deleted during the substitution process above,
2931 might have been the only trapping insn in the block. We purge the now
2932 possibly dead EH edges here to avoid an ICE from fixup_abnormal_edges,
2933 called at the end of convert_regs. The order in which we process the
2934 blocks ensures that we never delete an already processed edge.
2936 Note that, at this point, the CFG may have been damaged by the emission
2937 of instructions after an abnormal call, which moves the basic block end
2938 (and is the reason why we call fixup_abnormal_edges later). So we must
2939 be sure that the trapping insn has been deleted before trying to purge
2940 dead edges, otherwise we risk purging valid edges.
2942 ??? We are normally supposed not to delete trapping insns, so we pretend
2943 that the insns deleted above don't actually trap. It would have been
2944 better to detect this earlier and avoid creating the EH edge in the first
2945 place, still, but we don't have enough information at that time. */
2947 if (control_flow_insn_deleted)
2948 purge_dead_edges (block);
2950 /* Something failed if the stack lives don't match. If we had malformed
2951 asms, we zapped the instruction itself, but that didn't produce the
2952 same pattern of register kills as before. */
2953 GO_IF_HARD_REG_EQUAL (regstack.reg_set, bi->out_reg_set, win);
2954 if (!any_malformed_asm)
2955 abort ();
2956 win:
2957 bi->stack_out = regstack;
2959 /* Compensate the back edges, as those wasn't visited yet. */
2960 FOR_EACH_EDGE (e, block->succs)
2962 if (e->flags & EDGE_DFS_BACK
2963 || (e->dest == EXIT_BLOCK_PTR))
2965 if (!BLOCK_INFO (e->dest)->done
2966 && e->dest != block)
2967 abort ();
2968 inserted |= compensate_edge (e, file);
2971 END_FOR_EACH_EDGE;
2973 FOR_EACH_EDGE (e, block->preds)
2975 if (e != beste && !(e->flags & EDGE_DFS_BACK)
2976 && e->src != ENTRY_BLOCK_PTR)
2978 if (!BLOCK_INFO (e->src)->done)
2979 abort ();
2980 inserted |= compensate_edge (e, file);
2983 END_FOR_EACH_EDGE;
2985 return inserted;
2988 /* Convert registers in all blocks reachable from BLOCK. */
2990 static int
2991 convert_regs_2 (FILE *file, basic_block block)
2993 basic_block *stack, *sp;
2994 int inserted;
2996 /* We process the blocks in a top-down manner, in a way such that one block
2997 is only processed after all its predecessors. The number of predecessors
2998 of every block has already been computed. */
3000 stack = xmalloc (sizeof (*stack) * n_basic_blocks);
3001 sp = stack;
3003 *sp++ = block;
3005 inserted = 0;
3008 edge e;
3010 block = *--sp;
3012 /* Processing BLOCK is achieved by convert_regs_1, which may purge
3013 some dead EH outgoing edge after the deletion of the trapping
3014 insn inside the block. Since the number of predecessors of
3015 BLOCK's successors was computed based on the initial edge set,
3016 we check the necessity to process some of these successors
3017 before such an edge deletion may happen. However, there is
3018 a pitfall: if BLOCK is the only predecessor of a successor and
3019 the edge between them happens to be deleted, the successor
3020 becomes unreachable and should not be processed. The problem
3021 is that there is no way to preventively detect this case so we
3022 stack the successor in all cases and hand over the task of
3023 fixing up the discrepancy to convert_regs_1. */
3025 FOR_EACH_EDGE (e, block->succs)
3027 if (! (e->flags & EDGE_DFS_BACK))
3029 BLOCK_INFO (e->dest)->predecessors--;
3030 if (!BLOCK_INFO (e->dest)->predecessors)
3031 *sp++ = e->dest;
3034 END_FOR_EACH_EDGE;
3036 inserted |= convert_regs_1 (file, block);
3037 BLOCK_INFO (block)->done = 1;
3039 while (sp != stack);
3041 return inserted;
3044 /* Traverse all basic blocks in a function, converting the register
3045 references in each insn from the "flat" register file that gcc uses,
3046 to the stack-like registers the 387 uses. */
3048 static int
3049 convert_regs (FILE *file)
3051 int inserted;
3052 basic_block b;
3053 edge e;
3055 /* Initialize uninitialized registers on function entry. */
3056 inserted = convert_regs_entry ();
3058 /* Construct the desired stack for function exit. */
3059 convert_regs_exit ();
3060 BLOCK_INFO (EXIT_BLOCK_PTR)->done = 1;
3062 /* ??? Future: process inner loops first, and give them arbitrary
3063 initial stacks which emit_swap_insn can modify. This ought to
3064 prevent double fxch that often appears at the head of a loop. */
3066 /* Process all blocks reachable from all entry points. */
3068 FOR_EACH_EDGE (e, ENTRY_BLOCK_PTR->succs)
3070 inserted |= convert_regs_2 (file, e->dest);
3072 END_FOR_EACH_EDGE;
3074 /* ??? Process all unreachable blocks. Though there's no excuse
3075 for keeping these even when not optimizing. */
3076 FOR_EACH_BB (b)
3078 block_info bi = BLOCK_INFO (b);
3080 if (! bi->done)
3081 inserted |= convert_regs_2 (file, b);
3083 clear_aux_for_blocks ();
3085 fixup_abnormal_edges ();
3086 if (inserted)
3087 commit_edge_insertions ();
3089 if (file)
3090 fputc ('\n', file);
3092 return inserted;
3094 #endif /* STACK_REGS */
3096 #include "gt-reg-stack.h"