1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1990 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
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, with the
34 exception of ASM_OPERANDS, which are discussed below.
36 The virtual registers can be manipulated normally by gcc, and their
37 semantics are the same as for normal registers. After the hard
38 register numbers are substituted, the semantics of an insn containing
39 stack-like regs are not the same as for an insn with normal regs: for
40 instance, it is not safe to delete an insn that appears to be a no-op
41 move. In general, no insn containing hard regs should be changed
42 after this pass is done.
44 * The form of the output:
46 After this pass, hard register numbers represent the distance from
47 the current top of stack to the desired register. A reference to
48 FIRST_STACK_REG references the top of stack, FIRST_STACK_REG + 1,
49 represents the register just below that, and so forth. Also, REG_DEAD
50 notes indicate whether or not a stack register should be popped.
52 A "swap" insn looks like a parallel of two patterns, where each
53 pattern is a SET: one sets A to B, the other B to A.
55 A "push" or "load" insn is a SET whose SET_DEST is FIRST_STACK_REG
56 and whose SET_DEST is REG or MEM. Any other SET_DEST, such as PLUS,
57 will replace the existing stack top, not push a new value.
59 A store insn is a SET whose SET_DEST is FIRST_STACK_REG, and whose
60 SET_SRC is REG or MEM.
62 The case where both the SET_SRC and SET_DEST FIRST_STACK_REG
63 appears ambiguous. As a special case, the presence of a REG_DEAD note
64 for FIRST_STACK_REG differentiates between a load insn and a pop.
66 If a REG_DEAD is present, the insn represents a "pop" that discards
67 the top of the register stack. If there is no REG_DEAD note, then the
68 insn represents a "dup" or a push of the current top of stack onto the
73 Existing REG_DEAD and REG_UNUSED notes for stack registers are
74 deleted and recreated from scratch. REG_DEAD is never created for a
75 SET_DEST, only REG_UNUSED.
77 Before life analysis, the mode of each insn is set based on whether
78 or not any stack registers are mentioned within that insn. VOIDmode
79 means that no regs are mentioned anyway, and QImode means that at
80 least one pattern within the insn mentions stack registers. This
81 information is valid until after reg_to_stack returns, and is used
86 Inline assembly isn't handled yet. */
93 #include "hard-reg-set.h"
98 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
100 /* True if the current function returns a real value. */
101 static int current_function_returns_real
;
103 /* This is the basic stack record. TOP is an index into REG[] such
104 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
106 If TOP is -2 the stack is not yet initialized: reg_set indicates
107 which registers are live. Stack initialization consists of placing
108 each live reg in array `reg' and setting `top' appropriately. */
110 typedef struct stack_def
112 int top
; /* index to top stack element */
113 HARD_REG_SET reg_set
; /* set of live registers */
114 char reg
[REG_STACK_SIZE
]; /* register - stack mapping */
117 /* highest instruction uid */
118 static int max_uid
= 0;
120 /* Number of basic blocks in the current function. */
123 /* Element N is first insn in basic block N.
124 This info lasts until we finish compiling the function. */
125 static rtx
*block_begin
;
127 /* Element N is last insn in basic block N.
128 This info lasts until we finish compiling the function. */
129 static rtx
*block_end
;
131 /* Element N is nonzero if control can drop into basic block N */
132 static char *block_drops_in
;
134 /* Element N says all about the stack at entry block N */
135 static stack block_stack_in
;
137 /* Element N says all about the stack life at the end of block N */
138 static HARD_REG_SET
*block_out_reg_set
;
140 /* This is where the BLOCK_NUM values are really stored. This is set
141 up by find_blocks and used there and in life_analysis. It can be used
142 later, but only to look up an insn that is the head or tail of some
143 block. life_analysis and the stack register conversion process can
144 add insns within a block. */
145 static short *block_number
;
147 /* This is the register file for all register after conversion */
148 static rtx SFmode_reg
[FIRST_PSEUDO_REGISTER
];
149 static rtx DFmode_reg
[FIRST_PSEUDO_REGISTER
];
151 /* ??? set of register to delete after ASM_OPERAND */
152 HARD_REG_SET asm_regs
;
154 /* Get the basic block number of an insn. See note at block_number
155 definition are validity of this information. */
157 #define BLOCK_NUM(INSN) \
158 (((INSN_UID (INSN) > max_uid) \
159 ? (short *)(abort() , 0) \
160 : block_number)[INSN_UID (INSN)])
162 extern rtx
gen_jump ();
163 extern rtx
gen_movdf ();
164 extern rtx
find_regno_note ();
165 extern rtx
emit_jump_insn_before ();
166 extern rtx
emit_label_after ();
168 extern rtx dconst0_rtx
;
170 /* Forward declarations */
172 static void find_blocks ();
173 static void stack_reg_life_analysis ();
174 static void convert_regs ();
175 static void dump_stack_info ();
176 static void fatal_for_asm ();
179 /* Return non-zero if any stack register is mentioned somewhere within
183 stack_regs_mentioned_p (pat
)
189 if (STACK_REG_P (pat
))
192 fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
193 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
199 for (j
= XVECLEN (pat
, i
) - 1; j
>= 0; j
--)
200 if (stack_regs_mentioned_p (XVECEXP (pat
, i
, j
)))
203 else if (fmt
[i
] == 'e' && stack_regs_mentioned_p (XEXP (pat
, i
)))
210 /* Convert register usage from "flat" register file usage to a "stack
211 register file. FIRST is the first insn in the function, FILE is the
214 First compute the beginning and end of each basic block. Do a
215 register life analysis on the stack registers, recording the result
216 for the head and tail of each basic block. The convert each insn one
217 by one. Run a last jump_optimize() pass, if optimizing, to eliminate
218 any cross-jumping created when the converter inserts pop insns.*/
221 reg_to_stack (first
, file
)
227 int stack_reg_seen
= 0;
229 current_function_returns_real
230 = TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl
))) == REAL_TYPE
;
232 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
234 SFmode_reg
[i
] = gen_rtx (REG
, SFmode
, i
);
235 DFmode_reg
[i
] = gen_rtx (REG
, DFmode
, i
);
238 /* Count the basic blocks. Also find maximum insn uid. */
240 register RTX_CODE prev_code
= JUMP_INSN
;
241 register RTX_CODE code
;
245 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
247 /* Note that this loop must select the same block boundaries
248 as code in find_blocks. */
250 if (INSN_UID (insn
) > max_uid
)
251 max_uid
= INSN_UID (insn
);
253 code
= GET_CODE (insn
);
255 if (code
== CODE_LABEL
256 || (prev_code
!= INSN
257 && prev_code
!= CALL_INSN
258 && prev_code
!= CODE_LABEL
259 && (code
== INSN
|| code
== CALL_INSN
|| code
== JUMP_INSN
)))
262 /* Remember whether or not this insn mentions an FP regs.
263 Check JUMP_INSNs too, in case someone creates a funny PARALLEL. */
265 if ((GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == CALL_INSN
266 || GET_CODE (insn
) == JUMP_INSN
)
267 && stack_regs_mentioned_p (PATTERN (insn
)))
270 PUT_MODE (insn
, QImode
);
273 PUT_MODE (insn
, VOIDmode
);
280 /* If no stack register reference exists in this insn, there isn't
281 anything to convert. */
283 if (! stack_reg_seen
)
286 /* If there are stack registers, there must be at least one block. */
291 /* Allocate some tables that last till end of compiling this function
292 and some needed only in find_blocks and life_analysis. */
294 block_begin
= (rtx
*) alloca (blocks
* sizeof (rtx
));
295 block_end
= (rtx
*) alloca (blocks
* sizeof (rtx
));
296 block_drops_in
= (char *) alloca (blocks
);
298 block_stack_in
= (stack
) alloca (blocks
* sizeof (struct stack_def
));
299 block_out_reg_set
= (HARD_REG_SET
*) alloca (blocks
* sizeof (HARD_REG_SET
));
300 bzero (block_stack_in
, blocks
* sizeof (struct stack_def
));
301 bzero (block_out_reg_set
, blocks
* sizeof (HARD_REG_SET
));
303 block_number
= (short *) alloca ((max_uid
+ 1) * sizeof (short));
306 stack_reg_life_analysis (first
);
308 /* Dump the life analysis debug information before jump
309 optimization, as that will destroy the LABEL_REFS we keep the
313 dump_stack_info (file
);
318 jump_optimize (first
, 2, 0, 0);
321 /* Check PAT, which is in INSN, for LABEL_REFs. Add INSN to the
322 label's chain of references, and note which insn contains each
326 record_label_references (insn
, pat
)
329 register enum rtx_code code
= GET_CODE (pat
);
333 if (code
== LABEL_REF
)
335 register rtx label
= XEXP (pat
, 0);
338 if (GET_CODE (label
) != CODE_LABEL
)
341 /* Don't make a duplicate in the code_label's chain. */
343 for (ref
= LABEL_REFS (label
); ref
!= label
; ref
= LABEL_NEXTREF (ref
))
344 if (CONTAINING_INSN (ref
) == insn
)
347 CONTAINING_INSN (pat
) = insn
;
348 LABEL_NEXTREF (pat
) = LABEL_REFS (label
);
349 LABEL_REFS (label
) = pat
;
354 fmt
= GET_RTX_FORMAT (code
);
355 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
358 record_label_references (insn
, XEXP (pat
, i
));
362 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
363 record_label_references (insn
, XVECEXP (pat
, i
, j
));
368 /* Return a pointer to the REG expression within PAT. If PAT is not a
369 REG, possible enclosed by a conversion rtx, return the inner part of
370 PAT that stopped the search. */
376 while (GET_CODE (*pat
) == SUBREG
377 || GET_CODE (*pat
) == FLOAT
378 || GET_CODE (*pat
) == FIX
379 || GET_CODE (*pat
) == FLOAT_EXTEND
380 || GET_CODE (*pat
) == FLOAT_TRUNCATE
)
381 pat
= & XEXP (*pat
, 0);
386 /* If REG is a stack register that is marked dead in REGSTACK, then
387 record that it is now live. If REG is not DEST, add a death note to
388 INSN if there isn't one already. If DEST is not a reg, it is safe to
389 assume that it does not mention a reg anywhere within. */
392 record_note_if_dead (insn
, regstack
, reg
, dest
)
397 reg
= * get_true_reg (& reg
);
399 if (STACK_REG_P (reg
))
401 if (! TEST_HARD_REG_BIT (regstack
->reg_set
, REGNO (reg
)))
403 if ((! REG_P (dest
) || REGNO (dest
) != REGNO (reg
))
404 && ! find_regno_note (insn
, REG_DEAD
, REGNO (reg
)))
405 REG_NOTES (insn
) = gen_rtx (EXPR_LIST
,
406 REG_DEAD
, reg
, REG_NOTES (insn
));
408 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (reg
));
412 if (stack_regs_mentioned_p (reg
))
416 /* Scan PAT, which is part of INSN, and record the life & death of
417 stack registers in REGSTACK. If a register was dead, but is an input
418 operand in this insn, then mark the register live and record a death
421 If a register is dead after this insn, but is an output operand in
422 this insn, record a REG_UNUSED note.
424 This function does not know about SET_DESTs that are both input and
425 output (such as ZERO_EXTRACT) - this cannot happen on a 387. */
428 record_reg_life_pat (insn
, regstack
, pat
)
435 if (GET_CODE (pat
) == CLOBBER
436 && GET_CODE (PATTERN (insn
)) == PARALLEL
437 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn
), 0, 0))) == ASM_OPERANDS
)
439 if (STACK_REG_P (XEXP (pat
, 0)))
444 if (GET_CODE (pat
) != SET
)
447 dest
= * get_true_reg (& SET_DEST (pat
));
449 /* The destination is dead before this insn. If the destination is
450 not used after this insn, record this with REG_UNUSED. */
452 if (STACK_REG_P (dest
))
454 /* ??? This check is unnecessary. */
456 if (find_regno_note (insn
, REG_UNUSED
, REGNO (dest
)))
459 if (! TEST_HARD_REG_BIT (regstack
->reg_set
, REGNO (dest
)))
460 REG_NOTES (insn
) = gen_rtx (EXPR_LIST
, REG_UNUSED
, dest
,
463 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (dest
));
466 if (dest
!= cc0_rtx
&& stack_regs_mentioned_p (dest
))
469 src
= * get_true_reg (& SET_SRC (pat
));
471 switch (GET_CODE (src
))
473 /* ??? get_true_reg will make some of these cases redundant. */
480 record_note_if_dead (insn
, regstack
, XEXP (src
, 0), dest
);
481 record_note_if_dead (insn
, regstack
, XEXP (src
, 1), dest
);
491 record_note_if_dead (insn
, regstack
, XEXP (src
, 0), dest
);
497 if (GET_CODE (src
) == FIX
)
498 record_note_if_dead (insn
, regstack
, XEXP (src
, 0), dest
);
500 record_note_if_dead (insn
, regstack
, src
, dest
);
507 /* ??? This needs much improvement */
509 if (stack_regs_mentioned_p (pat
))
512 for (j
= 0; j
< XVECLEN (src
, 3); j
++)
513 record_note_if_dead (insn
, regstack
, XVECEXP (src
, 3, j
), dest
);
518 record_note_if_dead (insn
, regstack
, src
, dest
);
522 /* If a stack register appears in the src RTL, it is a bug, and
523 code should be added above to handle it. */
525 if (stack_regs_mentioned_p (src
))
530 /* Scan INSN, which is in BLOCK, and record the life & death of stack
531 registers in REGSTACK. This function is called to process insns from
532 the last insn in a block to the first. The actual scanning is done in
535 If a register is live after a CALL_INSN, but is not a value return
536 register for that CALL_INSN, then code is emitted to initialize that
537 register. The block_end[] data is kept accurate.
539 Existing death and unset notes for stack registers are deleted
540 before processing the insn. */
543 record_reg_life (insn
, block
, regstack
)
548 rtx note
, *note_link
;
550 if ((GET_CODE (insn
) != INSN
&& GET_CODE (insn
) != CALL_INSN
)
551 || INSN_DELETED_P (insn
))
554 /* Strip death notes for stack regs from this insn */
556 note_link
= ®_NOTES(insn
);
557 for (note
= *note_link
; note
; note
= XEXP (note
, 1))
558 if (STACK_REG_P (XEXP (note
, 0))
559 && (REG_NOTE_KIND (note
) == REG_DEAD
560 || REG_NOTE_KIND (note
) == REG_UNUSED
))
561 *note_link
= XEXP (note
, 1);
563 note_link
= &XEXP (note
, 1);
565 /* Process all patterns in the insn. */
567 if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
571 for (i
= 0; i
< XVECLEN (PATTERN (insn
), 0); i
++)
572 record_reg_life_pat (insn
, regstack
, XVECEXP (PATTERN (insn
), 0, i
));
574 else if (GET_MODE (insn
) == QImode
)
575 record_reg_life_pat (insn
, regstack
, PATTERN (insn
));
577 /* There might be a reg that is live after a function call.
578 Initialize it to zero so that the program does not crash. See comment
579 towards the end of stack_reg_life_analysis(). */
581 if (GET_CODE (insn
) == CALL_INSN
)
583 int reg
= FIRST_FLOAT_REG
;
585 /* If a stack reg is mentioned in a CALL_INSN, it must be as the
586 return value; conversely, if a float is returned, a stack reg
587 must be mentioned. */
589 if (stack_regs_mentioned_p (PATTERN (insn
)))
592 for (; reg
<= LAST_STACK_REG
; reg
++)
593 if (TEST_HARD_REG_BIT (regstack
->reg_set
, reg
))
597 /* The insn will use virtual register numbers, and so
598 convert_regs is expected to process these. But BLOCK_NUM
599 cannot be used on these insns, because they do not appear in
602 pat
= gen_rtx (SET
, VOIDmode
, DFmode_reg
[reg
], dconst0_rtx
);
603 init
= emit_insn_after (pat
, insn
);
604 PUT_MODE (init
, QImode
);
606 CLEAR_HARD_REG_BIT (regstack
->reg_set
, reg
);
608 /* If the CALL_INSN was the end of a block, move the
609 block_end to point to the new insn. */
611 if (block_end
[block
] == insn
)
612 block_end
[block
] = init
;
615 /* Some regs do not survive a CALL */
617 AND_COMPL_HARD_REG_SET (regstack
->reg_set
, call_used_reg_set
);
621 /* Find all basic blocks of the function, which starts with FIRST.
622 For each JUMP_INSN, build the chain of LABEL_REFS on each CODE_LABEL. */
630 register RTX_CODE prev_code
= BARRIER
;
631 register RTX_CODE code
;
633 /* Record where all the blocks start and end.
634 Record which basic blocks control can drop in to. */
637 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
639 /* Note that this loop must select the same block boundaries
640 as code in reg_to_stack. */
642 code
= GET_CODE (insn
);
644 if (code
== CODE_LABEL
645 || (prev_code
!= INSN
646 && prev_code
!= CALL_INSN
647 && prev_code
!= CODE_LABEL
648 && (code
== INSN
|| code
== CALL_INSN
|| code
== JUMP_INSN
)))
650 block_begin
[++block
] = insn
;
651 block_end
[block
] = insn
;
652 block_drops_in
[block
] = prev_code
!= BARRIER
;
654 else if (code
== INSN
|| code
== CALL_INSN
|| code
== JUMP_INSN
)
655 block_end
[block
] = insn
;
657 BLOCK_NUM (insn
) = block
;
659 if (code
== CODE_LABEL
)
660 LABEL_REFS (insn
) = insn
; /* delete old chain */
666 if (block
+ 1 != blocks
)
669 /* generate all label references to the correspondending jump insn */
670 for (block
= 0; block
< blocks
; block
++)
672 insn
= block_end
[block
];
674 if (GET_CODE (insn
) == JUMP_INSN
)
675 record_label_references (insn
, PATTERN (insn
));
679 /* Determine the which registers are live at the start of each basic
680 block of the function whose first insn is FIRST.
682 First, if the function returns a real_type, mark the function
683 return type as live at each return point, as the RTL may not give any
684 hint that the register is live.
686 Then, start with the last block and work back to the first block.
687 Similarly, work backwards within each block, insn by insn, recording
688 which regs are die and which are used (and therefore live) in the
689 hard reg set of block_stack_in[].
691 After processing each basic block, if there is a label at the start
692 of the block, propagate the live registers to all jumps to this block.
694 As a special case, if there are regs live in this block, that are
695 not live in a block containing a jump to this label, and the block
696 containing the jump has already been processed, we must propagate this
697 block's entry register life back to the block containing the jump, and
698 restart life analysis from there.
700 In the worst case, this function may traverse the insns
701 REG_STACK_SIZE times. This is necessary, since a jump towards the end
702 of the insns may not know that a reg is live at a target that is early
703 in the insns. So we back up and start over with the new reg live.
705 If there are registers that are live at the start of the function,
706 insns are emitted to initialize these registers. Something similar is
707 done after CALL_INSNs in record_reg_life. */
710 stack_reg_life_analysis (first
)
714 struct stack_def regstack
;
716 if (current_function_returns_real
)
718 /* Find all RETURN insns and mark them. */
720 for (block
= blocks
- 1; block
>= 0; block
--)
721 if (GET_CODE (block_end
[block
]) == JUMP_INSN
722 && GET_CODE (PATTERN (block_end
[block
])) == RETURN
)
723 SET_HARD_REG_BIT (block_out_reg_set
[block
], FIRST_STACK_REG
);
725 /* Mark of the end of last block if we "fall off" the end of the
726 function into the epilogue. */
728 if (GET_CODE (block_end
[blocks
-1]) != JUMP_INSN
729 || GET_CODE (PATTERN (block_end
[blocks
-1])) == RETURN
)
730 SET_HARD_REG_BIT (block_out_reg_set
[blocks
-1], FIRST_STACK_REG
);
733 /* now scan all blocks backward for stack register use */
738 register rtx insn
, prev
;
740 /* current register status at last instruction */
742 COPY_HARD_REG_SET (regstack
.reg_set
, block_out_reg_set
[block
]);
744 prev
= block_end
[block
];
748 prev
= PREV_INSN (insn
);
750 /* If the insn is a CALL_INSN, we need to ensure that
751 everything dies. But otherwise don't process unless there
752 are some stack regs present. */
754 if (GET_MODE (insn
) == QImode
|| GET_CODE (insn
) == CALL_INSN
)
755 record_reg_life (insn
, block
, ®stack
);
757 } while (insn
!= block_begin
[block
]);
759 /* Set the state at the start of the block. Mark that no
760 register mapping information known yet. */
762 COPY_HARD_REG_SET (block_stack_in
[block
].reg_set
, regstack
.reg_set
);
763 block_stack_in
[block
].top
= -2;
765 /* If there is a label, propagate our register life to all jumps
768 if (GET_CODE (insn
) == CODE_LABEL
)
771 int must_restart
= 0;
773 for (label
= LABEL_REFS (insn
); label
!= insn
;
774 label
= LABEL_NEXTREF (label
))
776 int jump_block
= BLOCK_NUM (CONTAINING_INSN (label
));
778 if (jump_block
< block
)
779 IOR_HARD_REG_SET (block_out_reg_set
[jump_block
],
780 block_stack_in
[block
].reg_set
);
783 /* The block containing the jump has already been
784 processed. If there are registers that were not known
785 to be live then, but are live now, we must back up
786 and restart life analysis from that point with the new
789 GO_IF_HARD_REG_SUBSET (block_stack_in
[block
].reg_set
,
790 block_out_reg_set
[jump_block
],
793 IOR_HARD_REG_SET (block_out_reg_set
[jump_block
],
794 block_stack_in
[block
].reg_set
);
807 if (block_drops_in
[block
])
808 IOR_HARD_REG_SET (block_out_reg_set
[block
-1],
809 block_stack_in
[block
].reg_set
);
815 /* If any reg is live at the start of the first block of a
816 function, then we must guarantee that the reg holds some value by
817 generating our own "load" of that register. Otherwise a 387 would
818 fault trying to access an empty register. */
820 HARD_REG_SET empty_regs
;
821 CLEAR_HARD_REG_SET (empty_regs
);
822 GO_IF_HARD_REG_SUBSET (block_stack_in
[0].reg_set
, empty_regs
,
826 /* Load zero into each live register. The fact that a register
827 appears live at the function start does not necessarily imply an error
828 in the user program: it merely means that we could not determine that
829 there wasn't such an error, just as -Wunused sometimes gives
830 "incorrect" warnings. In those cases, these initializations will do
833 Note that we are inserting virtual register references here:
834 these insns must be processed by convert_regs later. Also, these
835 insns will not be in block_number, so BLOCK_NUM() will fail for them. */
837 for (reg
= LAST_STACK_REG
; reg
>= FIRST_STACK_REG
; reg
--)
838 if (TEST_HARD_REG_BIT (block_stack_in
[0].reg_set
, reg
))
842 init_rtx
= gen_rtx (SET
, VOIDmode
, DFmode_reg
[reg
], dconst0_rtx
);
843 block_begin
[0] = emit_insn_after (init_rtx
, first
);
844 PUT_MODE (block_begin
[0], QImode
);
846 CLEAR_HARD_REG_BIT (block_stack_in
[0].reg_set
, reg
);
853 /*****************************************************************************
854 This section deals with stack register substition, and forms the second
856 *****************************************************************************/
858 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
859 the desired hard REGNO. */
862 replace_reg (reg
, regno
)
866 if (regno
< FIRST_STACK_REG
|| regno
> LAST_STACK_REG
867 || ! STACK_REG_P (*reg
))
870 if (GET_MODE (*reg
) == DFmode
)
871 *reg
= DFmode_reg
[regno
];
872 else if (GET_MODE (*reg
) == SFmode
)
873 *reg
= SFmode_reg
[regno
];
878 /* Remove a note of type NOTE, which must be found, for register
879 number REGNO from INSN. Remove only one such note. */
882 remove_regno_note (insn
, note
, regno
)
887 register rtx
*note_link
, this;
889 note_link
= ®_NOTES(insn
);
890 for (this = *note_link
; this; this = XEXP (this, 1))
891 if (REG_NOTE_KIND (this) == note
892 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno
)
894 *note_link
= XEXP (this, 1);
898 note_link
= &XEXP (this, 1);
903 /* Find the hard register number of virtual register REG in REGSTACK.
904 The hard register number is relative to the top of the stack. -1 is
905 returned if the register is not found. */
908 get_hard_regnum (regstack
, reg
)
914 if (! STACK_REG_P (reg
))
917 for (i
= regstack
->top
; i
>= 0; i
--)
918 if (regstack
->reg
[i
] == REGNO (reg
))
921 return i
>= 0 ? (FIRST_STACK_REG
+ regstack
->top
- i
) : -1;
924 /* Delete INSN from the RTL. Mark the insn, but don't remove it from
925 the chain of insns. Doing so could confuse block_begin and block_end
926 if this were the only insn in the block. */
929 delete_insn_for_stacker (insn
)
932 PUT_CODE (insn
, NOTE
);
933 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
934 NOTE_SOURCE_FILE (insn
) = 0;
935 INSN_DELETED_P (insn
) = 1;
938 /* Emit an insn to pop virtual register REG before or after INSN.
939 REGSTACK is the stack state after INSN and is updated to reflect this
940 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
941 is represented as a SET whose destination is the register to be popped
942 and source is the top of stack. A death note for the top of stack
943 cases the movdf pattern to pop. */
946 emit_pop_insn (insn
, regstack
, reg
, when
)
952 rtx pop_insn
, pop_rtx
;
955 hard_regno
= get_hard_regnum (regstack
, reg
);
957 if (hard_regno
< FIRST_STACK_REG
)
960 pop_rtx
= gen_rtx (SET
, VOIDmode
, DFmode_reg
[hard_regno
],
961 DFmode_reg
[FIRST_STACK_REG
]);
963 pop_insn
= (*when
) (pop_rtx
, insn
);
964 PUT_MODE (pop_insn
, VOIDmode
);
966 REG_NOTES (pop_insn
) = gen_rtx (EXPR_LIST
,
967 REG_DEAD
, DFmode_reg
[FIRST_STACK_REG
],
968 REG_NOTES (pop_insn
));
970 regstack
->reg
[regstack
->top
- (hard_regno
- FIRST_STACK_REG
)]
971 = regstack
->reg
[regstack
->top
];
973 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (reg
));
978 /* Emit an insn before or after INSN to swap virtual register REG with the
979 top of stack. WHEN should be `emit_insn_before' or `emit_insn_before'
980 REGSTACK is the stack state before the swap, and is updated to reflect
981 the swap. A swap insn is represented as a PARALLEL of two patterns:
982 each pattern moves one reg to the other.
984 If REG is already at the top of the stack, no insn is emitted. */
987 emit_hard_swap_insn (insn
, regstack
, hard_regno
, when
)
994 rtx swap_rtx
, swap_insn
;
997 if (hard_regno
== FIRST_STACK_REG
)
1000 swap_rtx
= gen_swapdf (DFmode_reg
[hard_regno
], DFmode_reg
[FIRST_STACK_REG
]);
1001 swap_insn
= (*when
) (swap_rtx
, insn
);
1002 PUT_MODE (swap_insn
, VOIDmode
);
1004 other
= regstack
->top
- (hard_regno
- FIRST_STACK_REG
);
1006 tmp
= regstack
->reg
[other
];
1007 regstack
->reg
[other
] = regstack
->reg
[regstack
->top
];
1008 regstack
->reg
[regstack
->top
] = tmp
;
1011 /* Emit an insn before or after INSN to swap virtual register REG with the
1012 top of stack. See comments before emit_hard_swap_insn. */
1015 emit_swap_insn (insn
, regstack
, reg
, when
)
1023 hard_regno
= get_hard_regnum (regstack
, reg
);
1025 emit_hard_swap_insn (insn
, regstack
, hard_regno
, when
);
1028 /* Handle a move to or from a stack register in PAT, which is in INSN.
1029 REGSTACK is the current stack. */
1032 move_for_stack_reg (insn
, regstack
, pat
)
1037 rtx
*src
= get_true_reg (&SET_SRC (pat
));
1038 rtx
*dest
= get_true_reg (&SET_DEST (pat
));
1041 if (STACK_REG_P (*src
) && STACK_REG_P (*dest
))
1043 /* Write from one stack reg to another. If SRC dies here, then
1044 just change the register mapping and delete the insn. */
1046 note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src
));
1051 /* If this is a no-op move, there must not be a REG_DEAD note. */
1052 if (REGNO (*src
) == REGNO (*dest
))
1055 for (i
= regstack
->top
; i
>= 0; i
--)
1056 if (regstack
->reg
[i
] == REGNO (*src
))
1059 /* The source must be live, and the dest must be dead. */
1060 if (i
< 0 || get_hard_regnum (regstack
, *dest
) >= FIRST_STACK_REG
)
1063 /* It is possible that the dest is unused after this insn.
1064 If so, just pop the src. */
1066 if (find_regno_note (insn
, REG_UNUSED
, REGNO (*dest
)))
1068 emit_pop_insn (insn
, regstack
, *src
, emit_insn_after
);
1070 delete_insn_for_stacker (insn
);
1074 regstack
->reg
[i
] = REGNO (*dest
);
1076 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1077 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (*src
));
1079 delete_insn_for_stacker (insn
);
1084 /* The source reg does not die. */
1086 /* If this appears to be a no-op move, delete it, or else it
1087 will confuse the machine description output patterns. But if
1088 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1089 for REG_UNUSED will not work for deleted insns. */
1091 if (REGNO (*src
) == REGNO (*dest
))
1093 if (find_regno_note (insn
, REG_UNUSED
, REGNO (*dest
)))
1094 emit_pop_insn (insn
, regstack
, *dest
, emit_insn_after
);
1096 delete_insn_for_stacker (insn
);
1100 /* The destination ought to be dead */
1101 if (get_hard_regnum (regstack
, *dest
) >= FIRST_STACK_REG
)
1104 replace_reg (src
, get_hard_regnum (regstack
, *src
));
1106 regstack
->reg
[++regstack
->top
] = REGNO (*dest
);
1107 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1108 replace_reg (dest
, FIRST_STACK_REG
);
1110 else if (STACK_REG_P (*src
))
1112 /* Save from a stack reg to MEM, or possibly integer reg. Since
1113 only top of stack may be saved, emit an exchange first if
1116 emit_swap_insn (insn
, regstack
, *src
, emit_insn_before
);
1118 note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src
));
1121 replace_reg (&XEXP (note
, 0), FIRST_STACK_REG
);
1123 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (*src
));
1126 replace_reg (src
, FIRST_STACK_REG
);
1128 else if (STACK_REG_P (*dest
))
1130 /* Load from MEM, or possibly integer REG or constant, into the
1131 stack regs. The actual target is always the top of the
1132 stack. The stack mapping is changed to reflect that DEST is
1133 now at top of stack. */
1135 /* The destination ought to be dead */
1136 if (get_hard_regnum (regstack
, *dest
) >= FIRST_STACK_REG
)
1139 if (regstack
->top
>= REG_STACK_SIZE
)
1142 regstack
->reg
[++regstack
->top
] = REGNO (*dest
);
1143 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1144 replace_reg (dest
, FIRST_STACK_REG
);
1150 /* Handle a comparison. Special care needs to be taken to avoid
1151 causing comparisons that a 387 cannot do correctly, such as EQ.
1153 Also, a pop insn may need to be emitted. The 387 does have an
1154 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1155 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1159 compare_for_stack_reg (insn
, regstack
, pat
)
1165 rtx src1_note
, src2_note
;
1167 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 0));
1168 src2
= get_true_reg (&XEXP (SET_SRC (pat
), 1));
1170 /* The first argument must always be a stack reg. */
1173 if (! STACK_REG_P (*src1
))
1176 /* We will fix any death note later. */
1178 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1180 if (STACK_REG_P (*src2
))
1181 src2_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src2
));
1185 emit_swap_insn (insn
, regstack
, *src1
, emit_insn_before
);
1187 replace_reg (src1
, FIRST_STACK_REG
);
1189 if (STACK_REG_P (*src2
))
1190 replace_reg (src2
, get_hard_regnum (regstack
, *src2
));
1194 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (XEXP (src1_note
, 0)));
1195 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
1199 /* If the second operand dies, handle that. But if the operands are
1200 the same stack register, don't bother, because only one death is
1201 needed, and it was just handled. */
1204 && ! (STACK_REG_P (*src1
)
1205 && STACK_REG_P (*src2
)
1206 && REGNO (*src1
) == REGNO (*src2
)))
1208 /* As a special case, two regs may die in this insn if src2 is
1209 next to top of stack and the top of stack also dies. Since
1210 we have already popped src1, "next to top of stack" is really
1211 at top (FIRST_STACK_REG) now. */
1213 if (get_hard_regnum (regstack
, XEXP (src2_note
, 0)) == FIRST_STACK_REG
1216 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (XEXP (src2_note
, 0)));
1217 replace_reg (&XEXP (src2_note
, 0), FIRST_STACK_REG
+ 1);
1222 /* The 386 can only represent death of the first operand in
1223 the case handled above. In all other cases, emit a separate
1224 pop and remove the death note from here. */
1226 remove_regno_note (insn
, REG_DEAD
, REGNO (XEXP (src2_note
, 0)));
1228 emit_pop_insn (insn
, regstack
, XEXP (src2_note
, 0),
1234 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1235 is the current register layout. */
1238 subst_stack_regs_pat (insn
, regstack
, pat
)
1244 rtx
*src1
= 0, *src2
;
1245 rtx src1_note
, src2_note
;
1247 if (GET_CODE (pat
) != SET
)
1250 dest
= get_true_reg (&SET_DEST (pat
));
1251 src
= get_true_reg (&SET_SRC (pat
));
1253 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1255 if (*dest
!= cc0_rtx
1256 && (STACK_REG_P (*src
)
1257 || (STACK_REG_P (*dest
)
1258 && (GET_CODE (*src
) == REG
|| GET_CODE (*src
) == MEM
1259 || GET_CODE (*src
) == CONST_DOUBLE
))))
1260 move_for_stack_reg (insn
, regstack
, pat
);
1262 switch (GET_CODE (SET_SRC (pat
)))
1265 compare_for_stack_reg (insn
, regstack
, pat
);
1269 regstack
->reg
[++regstack
->top
] = REGNO (*dest
);
1270 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1271 replace_reg (dest
, FIRST_STACK_REG
);
1275 /* This is a `tstM2' case. */
1276 if (*dest
!= cc0_rtx
)
1286 /* These insns only operate on the top of the stack. DEST might
1287 be cc0_rtx if we're processing a tstM pattern. Also, it's
1288 possible that the tstM case results in a REG_DEAD note on the
1292 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 0));
1294 emit_swap_insn (insn
, regstack
, *src1
, emit_insn_before
);
1296 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1298 if (STACK_REG_P (*dest
))
1299 replace_reg (dest
, FIRST_STACK_REG
);
1303 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
1305 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (*src1
));
1308 replace_reg (src1
, FIRST_STACK_REG
);
1314 /* On i386, reversed forms of subM3 and divM3 exist for
1315 MODE_FLOAT, so the same code that works for addM3 and mulM3
1319 /* These insns can accept the top of stack as a destination
1320 from a stack reg or mem, or can use the top of stack as a
1321 source and some other stack register (possibly top of stack)
1322 as a destination. */
1324 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 0));
1325 src2
= get_true_reg (&XEXP (SET_SRC (pat
), 1));
1327 /* We will fix any death note later. */
1329 if (STACK_REG_P (*src1
))
1330 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1333 if (STACK_REG_P (*src2
))
1334 src2_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src2
));
1338 /* If either operand is not a stack register, then the dest
1339 must be top of stack. */
1341 if (! STACK_REG_P (*src1
) || ! STACK_REG_P (*src2
))
1342 emit_swap_insn (insn
, regstack
, *dest
, emit_insn_before
);
1345 /* Both operands are REG. If neither operand is already
1346 at the top of stack, choose to make the one that is the dest
1347 the new top of stack.
1349 ??? A later optimization here would be to look forward
1350 in the insns and see which source reg will be needed at top
1351 of stack soonest. */
1353 int src1_hard_regnum
, src2_hard_regnum
;
1355 src1_hard_regnum
= get_hard_regnum (regstack
, *src1
);
1356 src2_hard_regnum
= get_hard_regnum (regstack
, *src2
);
1357 if (src1_hard_regnum
== -1 || src2_hard_regnum
== -1)
1360 if (src1_hard_regnum
!= FIRST_STACK_REG
1361 && src2_hard_regnum
!= FIRST_STACK_REG
)
1362 emit_swap_insn (insn
, regstack
, *dest
, emit_insn_before
);
1365 if (STACK_REG_P (*src1
))
1366 replace_reg (src1
, get_hard_regnum (regstack
, *src1
));
1367 if (STACK_REG_P (*src2
))
1368 replace_reg (src2
, get_hard_regnum (regstack
, *src2
));
1372 /* If the register that dies is at the top of stack, then
1373 the destination is somewhere else - merely substitute it.
1374 But if the reg that dies is not at top of stack, then
1375 move the top of stack to the dead reg, as though we had
1376 done the insn and then a store-with-pop. */
1378 if (REGNO (XEXP (src1_note
, 0)) == regstack
->reg
[regstack
->top
])
1380 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1381 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
1385 int regno
= get_hard_regnum (regstack
, XEXP (src1_note
, 0));
1387 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1388 replace_reg (dest
, regno
);
1390 regstack
->reg
[regstack
->top
- (regno
- FIRST_STACK_REG
)]
1391 = regstack
->reg
[regstack
->top
];
1394 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
1395 REGNO (XEXP (src1_note
, 0)));
1396 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
1401 if (REGNO (XEXP (src2_note
, 0)) == regstack
->reg
[regstack
->top
])
1403 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1404 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
1408 int regno
= get_hard_regnum (regstack
, XEXP (src2_note
, 0));
1410 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1411 replace_reg (dest
, regno
);
1413 regstack
->reg
[regstack
->top
- (regno
- FIRST_STACK_REG
)]
1414 = regstack
->reg
[regstack
->top
];
1417 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
1418 REGNO (XEXP (src2_note
, 0)));
1419 replace_reg (&XEXP (src2_note
, 0), FIRST_STACK_REG
);
1424 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1425 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
1435 /* Substitute stack hard reg numbers for stack virtual registers in
1436 INSN. Non-stack register numbers are not changed. REGSTACK is the
1437 current stack content. Insns may be emitted as needed to arrange the
1438 stack for the 387 based on the contents of the insn. */
1441 subst_stack_regs (insn
, regstack
)
1445 register rtx
*note_link
, note
;
1448 if ((GET_CODE (insn
) != INSN
&& GET_CODE (insn
) != CALL_INSN
)
1449 || INSN_DELETED_P (insn
))
1452 /* The stack should be empty at a call. */
1454 if (GET_CODE (insn
) == CALL_INSN
)
1455 for (i
= FIRST_STACK_REG
; i
<= LAST_STACK_REG
; i
++)
1456 if (TEST_HARD_REG_BIT (regstack
->reg_set
, i
))
1459 /* Do the actual substitution if any stack regs are mentioned.
1460 Since we only record whether entire insn mentions stack regs, and
1461 subst_stack_regs_pat only works for patterns that contain stack regs,
1462 we must check each pattern in a parallel here. A call_value_pop could
1465 if (GET_MODE (insn
) == QImode
)
1467 if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
1468 for (i
= 0; i
< XVECLEN (PATTERN (insn
) , 0); i
++)
1470 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn
), 0, i
)))
1471 subst_stack_regs_pat (insn
, regstack
,
1472 XVECEXP (PATTERN (insn
), 0, i
));
1475 subst_stack_regs_pat (insn
, regstack
, PATTERN (insn
));
1478 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
1479 REG_UNUSED will already have been dealt with, so just return. */
1481 if (INSN_DELETED_P (insn
))
1484 /* If there is a REG_UNUSED note on a stack register on this insn,
1485 the indicated reg must be popped. The REG_UNUSED note is removed,
1486 since the form of the newly emitted pop insn references the reg,
1487 making it no longer `unset'. */
1489 note_link
= ®_NOTES(insn
);
1490 for (note
= *note_link
; note
; note
= XEXP (note
, 1))
1491 if (REG_NOTE_KIND (note
) == REG_UNUSED
&& STACK_REG_P (XEXP (note
, 0)))
1493 *note_link
= XEXP (note
, 1);
1494 emit_pop_insn (insn
, regstack
, XEXP (note
, 0),
1498 note_link
= &XEXP (note
, 1);
1501 /* Change the organization of the stack so that it fits a new basic
1502 block. Some registers might have to be popped, but there can never be
1503 a register live in the new block that is not now live.
1505 Insert any needed insns after INSN. OLD is the original stack
1506 layout, and NEW is the desired form. OLD is updated to reflect the
1507 code emitted, ie, it will be the same as NEW upon return.
1509 This function will not preserve block_end[]. But that information
1510 is no longer needed once this has executed. */
1513 change_stack (insn
, old
, new)
1520 /* We will be inserting new insns after INSN, by first finding the
1521 next insn, and inserting before it. */
1523 insn
= NEXT_INSN (insn
);
1525 /* Pop any registers that are not needed in the new block. */
1527 for (reg
= old
->top
; reg
>= 0; reg
--)
1528 if (! TEST_HARD_REG_BIT (new->reg_set
, old
->reg
[reg
]))
1529 emit_pop_insn (insn
, old
, DFmode_reg
[old
->reg
[reg
]],
1534 /* If the new block has never been processed, then it can inherit
1535 the old stack order. */
1537 new->top
= old
->top
;
1538 bcopy (old
->reg
, new->reg
, sizeof (new->reg
));
1542 /* This block has been entered before, and we must match the
1543 previously selected stack order. */
1545 /* By now, the only difference should be the order of the stack,
1546 not their depth or liveliness. */
1548 GO_IF_HARD_REG_EQUAL (old
->reg_set
, new->reg_set
, win
);
1554 if (old
->top
!= new->top
)
1557 /* Loop here emitting swaps until the stack is correct. The
1558 worst case number of swaps emitted is N + 2, where N is the
1559 depth of the stack. In some cases, the reg at the top of
1560 stack may be correct, but swapped anyway in order to fix
1561 other regs. But since we never swap any other reg away from
1562 its correct slot, this algorithm will converge. */
1566 /* Swap the reg at top of stack into the position it is
1567 supposed to be in, until the correct top of stack appears. */
1569 while (old
->reg
[old
->top
] != new->reg
[new->top
])
1571 for (reg
= new->top
; reg
>= 0; reg
--)
1572 if (new->reg
[reg
] == old
->reg
[old
->top
])
1578 emit_swap_insn (insn
, old
, DFmode_reg
[old
->reg
[reg
]],
1582 /* See if any regs remain incorrect. If so, bring an
1583 incorrect reg to the top of stack, and let the while loop
1586 for (reg
= new->top
; reg
>= 0; reg
--)
1587 if (new->reg
[reg
] != old
->reg
[reg
])
1589 emit_swap_insn (insn
, old
, DFmode_reg
[old
->reg
[reg
]],
1595 /* At this point there must be no differences. */
1597 for (reg
= old
->top
; reg
>= 0; reg
--)
1598 if (old
->reg
[reg
] != new->reg
[reg
])
1603 /* Check PAT, which points to RTL in INSN, for a LABEL_REF. If it is
1604 found, ensure that a jump from INSN to the code_label to which the
1605 label_ref points ends up with the same stack as that at the
1606 code_label. Do this by inserting insns just before the code_label to
1607 pop and rotate the stack until it is in the correct order. REGSTACK
1608 is the order of the register stack in INSN.
1610 Any code that is emitted here must not be later processed as part
1611 of any block, as it will already contain hard register numbers. */
1614 goto_block_pat (insn
, regstack
, pat
)
1620 rtx new_jump
, new_label
, new_barrier
;
1623 struct stack_def temp_stack
;
1626 if (GET_CODE (pat
) != LABEL_REF
)
1629 char *fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
1631 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
1634 goto_block_pat (insn
, regstack
, XEXP (pat
, i
));
1636 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
1637 goto_block_pat (insn
, regstack
, XVECEXP (pat
, i
, j
));
1642 label
= XEXP (pat
, 0);
1643 if (GET_CODE (label
) != CODE_LABEL
)
1646 /* First, see if in fact anything needs to be done to the stack at all. */
1648 label_stack
= &block_stack_in
[BLOCK_NUM (label
)];
1650 if (label_stack
->top
== -2)
1652 /* If the target block hasn't had a stack order selected, then
1653 we need merely ensure that no pops are needed. */
1655 for (reg
= regstack
->top
; reg
>= 0; reg
--)
1656 if (! TEST_HARD_REG_BIT (label_stack
->reg_set
, regstack
->reg
[reg
]))
1661 /* change_stack will not emit any code in this case. */
1663 change_stack (label
, regstack
, label_stack
);
1667 else if (label_stack
->top
== regstack
->top
)
1669 for (reg
= label_stack
->top
; reg
>= 0; reg
--)
1670 if (label_stack
->reg
[reg
] != regstack
->reg
[reg
])
1677 /* At least one insn will need to be inserted before label. Insert
1678 a jump around the code we are about to emit. Emit a label for the new
1679 code, and point the original insn at this new label. We can't use
1680 redirect_jump here, because we're using fld[4] of the code labels as
1681 LABEL_REF chains, no NUSES counters. */
1683 new_jump
= emit_jump_insn_before (gen_jump (label
), label
);
1684 record_label_references (new_jump
, PATTERN (new_jump
));
1685 JUMP_LABEL (new_jump
) = label
;
1687 new_barrier
= emit_barrier_after (new_jump
);
1689 new_label
= gen_label_rtx ();
1690 emit_label_after (new_label
, new_barrier
);
1691 LABEL_REFS (new_label
) = new_label
;
1693 /* The old label_ref will no longer point to the code_label if now uses,
1694 so strip the label_ref from the code_label's chain of references. */
1696 for (ref
= &LABEL_REFS (label
); *ref
!= label
; ref
= &LABEL_NEXTREF (*ref
))
1703 *ref
= LABEL_NEXTREF (*ref
);
1705 XEXP (pat
, 0) = new_label
;
1706 record_label_references (insn
, PATTERN (insn
));
1708 if (JUMP_LABEL (insn
) == label
)
1709 JUMP_LABEL (insn
) = new_label
;
1711 /* Now emit the needed code. */
1713 temp_stack
= *regstack
;
1715 change_stack (new_label
, &temp_stack
, label_stack
);
1718 /* Traverse all basic blocks in a function, converting the register
1719 refereces in each insn from the "flat" register file that gcc uses, to
1720 the stack-like registers the 387 uses. */
1725 register int block
, reg
;
1726 register rtx insn
, next
;
1727 struct stack_def regstack
;
1729 for (block
= 0; block
< blocks
; block
++)
1731 if (block_stack_in
[block
].top
== -2)
1733 /* This block has not been previously encountered. Choose a
1734 default mapping for any stack regs live on entry */
1736 block_stack_in
[block
].top
= -1;
1738 for (reg
= LAST_STACK_REG
; reg
>= FIRST_STACK_REG
; reg
--)
1739 if (TEST_HARD_REG_BIT (block_stack_in
[block
].reg_set
, reg
))
1740 block_stack_in
[block
].reg
[++block_stack_in
[block
].top
] = reg
;
1743 /* Process all insns in this block. Keep track of `next' here,
1744 so that we don't process any insns emitted while making
1745 substitutions in INSN. */
1747 next
= block_begin
[block
];
1748 regstack
= block_stack_in
[block
];
1752 next
= NEXT_INSN (insn
);
1754 /* Don't bother processing unless there is a stack reg
1757 ??? For now, process CALL_INSNs too to make sure that the
1758 stack regs are dead after a call. Remove this eventually. */
1760 if (GET_MODE (insn
) == QImode
|| GET_CODE (insn
) == CALL_INSN
)
1761 subst_stack_regs (insn
, ®stack
);
1763 } while (insn
!= block_end
[block
]);
1765 /* Something failed if the stack life doesn't match. */
1767 GO_IF_HARD_REG_EQUAL (regstack
.reg_set
, block_out_reg_set
[block
], win
);
1773 /* Adjust the stack of this block on exit to match the stack of
1774 the target block, or copy stack information into stack of
1775 jump target if the target block's stack order hasn't been set
1778 if (GET_CODE (insn
) == JUMP_INSN
)
1779 goto_block_pat (insn
, ®stack
, PATTERN (insn
));
1781 /* Likewise handle the case where we fall into the next block. */
1783 if ((block
< blocks
- 1) && block_drops_in
[block
+1])
1784 change_stack (insn
, ®stack
, &block_stack_in
[block
+1]);
1787 /* If the last basic block is the end of a loop, and that loop has
1788 regs live at its start, then the last basic block will have regs live
1789 at its end that need to be popped before the function returns. */
1791 for (reg
= regstack
.top
; reg
>= 0; reg
--)
1792 if (! current_function_returns_real
1793 || regstack
.reg
[reg
] != FIRST_STACK_REG
)
1794 insn
= emit_pop_insn (insn
, ®stack
, DFmode_reg
[regstack
.reg
[reg
]],
1798 /* Check expression PAT, which is in INSN, for label references. if
1799 one is found, print the block number of destination to FILE. */
1802 print_blocks (file
, insn
, pat
)
1806 register RTX_CODE code
= GET_CODE (pat
);
1810 if (code
== LABEL_REF
)
1812 register rtx label
= XEXP (pat
, 0);
1814 if (GET_CODE (label
) != CODE_LABEL
)
1817 fprintf (file
, " %d", BLOCK_NUM (label
));
1822 fmt
= GET_RTX_FORMAT (code
);
1823 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1826 print_blocks (file
, insn
, XEXP (pat
, i
));
1830 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
1831 print_blocks (file
, insn
, XVECEXP (pat
, i
, j
));
1836 /* Write information about stack registers and stack blocks into FILE.
1837 This is part of making a debugging dump. */
1839 dump_stack_info (file
)
1844 fprintf (file
, "\n%d stack blocks.\n", blocks
);
1845 for (block
= 0; block
< blocks
; block
++)
1847 register rtx head
, jump
, end
;
1850 fprintf (file
, "\nStack block %d: first insn %d, last %d.\n",
1851 block
, INSN_UID (block_begin
[block
]),
1852 INSN_UID (block_end
[block
]));
1854 head
= block_begin
[block
];
1856 fprintf (file
, "Reached from blocks: ");
1857 if (GET_CODE (head
) == CODE_LABEL
)
1858 for (jump
= LABEL_REFS (head
);
1860 jump
= LABEL_NEXTREF (jump
))
1862 register int from_block
= BLOCK_NUM (CONTAINING_INSN (jump
));
1863 fprintf (file
, " %d", from_block
);
1865 if (block_drops_in
[block
])
1866 fprintf (file
, " previous");
1868 fprintf (file
, "\nlive stack registers on block entry: ");
1869 for (regno
= FIRST_STACK_REG
; regno
<= LAST_STACK_REG
; regno
++)
1871 if (TEST_HARD_REG_BIT (block_stack_in
[block
].reg_set
, regno
))
1872 fprintf (file
, "%d ", regno
);
1875 fprintf (file
, "\nlive stack registers on block exit: ");
1876 for (regno
= FIRST_STACK_REG
; regno
<= LAST_STACK_REG
; regno
++)
1878 if (TEST_HARD_REG_BIT (block_out_reg_set
[block
], regno
))
1879 fprintf (file
, "%d ", regno
);
1882 end
= block_end
[block
];
1884 fprintf (file
, "\nJumps to blocks: ");
1885 if (GET_CODE (end
) == JUMP_INSN
)
1886 print_blocks (file
, end
, PATTERN (end
));
1888 if (block
+ 1 < blocks
&& block_drops_in
[block
+1])
1889 fprintf (file
, " next");
1890 else if (block
+ 1 == blocks
1891 || (GET_CODE (end
) == JUMP_INSN
1892 && GET_CODE (PATTERN (end
)) == RETURN
))
1893 fprintf (file
, " return");
1895 fprintf (file
, "\n");
1899 /* Report an error at line LINE of file FILE.
1900 S is a string and an arg for `printf'. */
1902 /* Report an fatal error at the line number of the insn INSN (ASM_OPERAND).
1903 S1, S2 is a string and an arg for `printf'. */
1906 fatal_for_asm (insn
, s1
, s2
)
1912 rtx body
= PATTERN (insn
);
1915 /* Find the (or one of the) ASM_OPERANDS in the insn. */
1916 if (GET_CODE (body
) == SET
&& GET_CODE (SET_SRC (body
)) == ASM_OPERANDS
)
1917 asmop
= SET_SRC (body
);
1918 else if (GET_CODE (body
) == ASM_OPERANDS
)
1920 else if (GET_CODE (body
) == PARALLEL
1921 && GET_CODE (XVECEXP (body
, 0, 0)) == SET
)
1922 asmop
= SET_SRC (XVECEXP (body
, 0, 0));
1923 else if (GET_CODE (body
) == PARALLEL
1924 && GET_CODE (XVECEXP (body
, 0, 0)) == ASM_OPERANDS
)
1925 asmop
= XVECEXP (body
, 0, 0);
1929 filename
= ASM_OPERANDS_SOURCE_FILE (asmop
);
1930 line
= ASM_OPERANDS_SOURCE_LINE (asmop
);
1932 fprintf (stderr
, s1
);
1935 error_with_file_and_line (filename
, line
, s2
, NULL
, NULL
);
1938 #endif /* STACK_REGS */