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 FP_mode_reg
[FIRST_PSEUDO_REGISTER
][(int) MAX_MACHINE_MODE
];
150 /* ??? set of register to delete after ASM_OPERAND */
151 HARD_REG_SET asm_regs
;
153 /* Get the basic block number of an insn. See note at block_number
154 definition are validity of this information. */
156 #define BLOCK_NUM(INSN) \
157 (((INSN_UID (INSN) > max_uid) \
158 ? (short *)(abort() , 0) \
159 : block_number)[INSN_UID (INSN)])
161 extern rtx
gen_jump ();
162 extern rtx
gen_movdf ();
163 extern rtx
find_regno_note ();
164 extern rtx
emit_jump_insn_before ();
165 extern rtx
emit_label_after ();
167 /* Forward declarations */
169 static void find_blocks ();
170 static void stack_reg_life_analysis ();
171 static void convert_regs ();
172 static void dump_stack_info ();
173 static void fatal_for_asm ();
176 /* Return non-zero if any stack register is mentioned somewhere within
180 stack_regs_mentioned_p (pat
)
186 if (STACK_REG_P (pat
))
189 fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
190 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
196 for (j
= XVECLEN (pat
, i
) - 1; j
>= 0; j
--)
197 if (stack_regs_mentioned_p (XVECEXP (pat
, i
, j
)))
200 else if (fmt
[i
] == 'e' && stack_regs_mentioned_p (XEXP (pat
, i
)))
207 /* Convert register usage from "flat" register file usage to a "stack
208 register file. FIRST is the first insn in the function, FILE is the
211 First compute the beginning and end of each basic block. Do a
212 register life analysis on the stack registers, recording the result
213 for the head and tail of each basic block. The convert each insn one
214 by one. Run a last jump_optimize() pass, if optimizing, to eliminate
215 any cross-jumping created when the converter inserts pop insns.*/
218 reg_to_stack (first
, file
)
224 int stack_reg_seen
= 0;
225 enum machine_mode mode
;
227 current_function_returns_real
228 = TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl
))) == REAL_TYPE
;
230 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_FLOAT
); mode
!= VOIDmode
;
231 mode
= GET_MODE_WIDER_MODE (mode
))
232 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
233 FP_mode_reg
[i
][(int) mode
] = gen_rtx (REG
, mode
, i
);
235 /* Count the basic blocks. Also find maximum insn uid. */
237 register RTX_CODE prev_code
= JUMP_INSN
;
238 register RTX_CODE code
;
242 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
244 /* Note that this loop must select the same block boundaries
245 as code in find_blocks. */
247 if (INSN_UID (insn
) > max_uid
)
248 max_uid
= INSN_UID (insn
);
250 code
= GET_CODE (insn
);
252 if (code
== CODE_LABEL
253 || (prev_code
!= INSN
254 && prev_code
!= CALL_INSN
255 && prev_code
!= CODE_LABEL
256 && (code
== INSN
|| code
== CALL_INSN
|| code
== JUMP_INSN
)))
259 /* Remember whether or not this insn mentions an FP regs.
260 Check JUMP_INSNs too, in case someone creates a funny PARALLEL. */
262 if ((GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == CALL_INSN
263 || GET_CODE (insn
) == JUMP_INSN
)
264 && stack_regs_mentioned_p (PATTERN (insn
)))
267 PUT_MODE (insn
, QImode
);
270 PUT_MODE (insn
, VOIDmode
);
277 /* If no stack register reference exists in this insn, there isn't
278 anything to convert. */
280 if (! stack_reg_seen
)
283 /* If there are stack registers, there must be at least one block. */
288 /* Allocate some tables that last till end of compiling this function
289 and some needed only in find_blocks and life_analysis. */
291 block_begin
= (rtx
*) alloca (blocks
* sizeof (rtx
));
292 block_end
= (rtx
*) alloca (blocks
* sizeof (rtx
));
293 block_drops_in
= (char *) alloca (blocks
);
295 block_stack_in
= (stack
) alloca (blocks
* sizeof (struct stack_def
));
296 block_out_reg_set
= (HARD_REG_SET
*) alloca (blocks
* sizeof (HARD_REG_SET
));
297 bzero (block_stack_in
, blocks
* sizeof (struct stack_def
));
298 bzero (block_out_reg_set
, blocks
* sizeof (HARD_REG_SET
));
300 block_number
= (short *) alloca ((max_uid
+ 1) * sizeof (short));
303 stack_reg_life_analysis (first
);
305 /* Dump the life analysis debug information before jump
306 optimization, as that will destroy the LABEL_REFS we keep the
310 dump_stack_info (file
);
315 jump_optimize (first
, 2, 0, 0);
318 /* Check PAT, which is in INSN, for LABEL_REFs. Add INSN to the
319 label's chain of references, and note which insn contains each
323 record_label_references (insn
, pat
)
326 register enum rtx_code code
= GET_CODE (pat
);
330 if (code
== LABEL_REF
)
332 register rtx label
= XEXP (pat
, 0);
335 if (GET_CODE (label
) != CODE_LABEL
)
338 /* Don't make a duplicate in the code_label's chain. */
340 for (ref
= LABEL_REFS (label
); ref
!= label
; ref
= LABEL_NEXTREF (ref
))
341 if (CONTAINING_INSN (ref
) == insn
)
344 CONTAINING_INSN (pat
) = insn
;
345 LABEL_NEXTREF (pat
) = LABEL_REFS (label
);
346 LABEL_REFS (label
) = pat
;
351 fmt
= GET_RTX_FORMAT (code
);
352 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
355 record_label_references (insn
, XEXP (pat
, i
));
359 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
360 record_label_references (insn
, XVECEXP (pat
, i
, j
));
365 /* Return a pointer to the REG expression within PAT. If PAT is not a
366 REG, possible enclosed by a conversion rtx, return the inner part of
367 PAT that stopped the search. */
373 while (GET_CODE (*pat
) == SUBREG
374 || GET_CODE (*pat
) == FLOAT
375 || GET_CODE (*pat
) == FIX
376 || GET_CODE (*pat
) == FLOAT_EXTEND
377 || GET_CODE (*pat
) == FLOAT_TRUNCATE
)
378 pat
= & XEXP (*pat
, 0);
383 /* If REG is a stack register that is marked dead in REGSTACK, then
384 record that it is now live. If REG is not DEST, add a death note to
385 INSN if there isn't one already. If DEST is not a reg, it is safe to
386 assume that it does not mention a reg anywhere within. */
389 record_note_if_dead (insn
, regstack
, reg
, dest
)
394 reg
= * get_true_reg (& reg
);
396 if (STACK_REG_P (reg
))
398 if (! TEST_HARD_REG_BIT (regstack
->reg_set
, REGNO (reg
)))
400 if ((! REG_P (dest
) || REGNO (dest
) != REGNO (reg
))
401 && ! find_regno_note (insn
, REG_DEAD
, REGNO (reg
)))
402 REG_NOTES (insn
) = gen_rtx (EXPR_LIST
,
403 REG_DEAD
, reg
, REG_NOTES (insn
));
405 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (reg
));
409 if (stack_regs_mentioned_p (reg
))
413 /* Scan PAT, which is part of INSN, and record the life & death of
414 stack registers in REGSTACK. If a register was dead, but is an input
415 operand in this insn, then mark the register live and record a death
418 If a register is dead after this insn, but is an output operand in
419 this insn, record a REG_UNUSED note.
421 This function does not know about SET_DESTs that are both input and
422 output (such as ZERO_EXTRACT) - this cannot happen on a 387. */
425 record_reg_life_pat (insn
, regstack
, pat
)
432 if (GET_CODE (pat
) == CLOBBER
433 && GET_CODE (PATTERN (insn
)) == PARALLEL
434 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn
), 0, 0))) == ASM_OPERANDS
)
436 if (STACK_REG_P (XEXP (pat
, 0)))
441 if (GET_CODE (pat
) != SET
)
444 dest
= * get_true_reg (& SET_DEST (pat
));
446 /* The destination is dead before this insn. If the destination is
447 not used after this insn, record this with REG_UNUSED. */
449 if (STACK_REG_P (dest
))
451 /* ??? This check is unnecessary. */
453 if (find_regno_note (insn
, REG_UNUSED
, REGNO (dest
)))
456 if (! TEST_HARD_REG_BIT (regstack
->reg_set
, REGNO (dest
)))
457 REG_NOTES (insn
) = gen_rtx (EXPR_LIST
, REG_UNUSED
, dest
,
460 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (dest
));
463 if (dest
!= cc0_rtx
&& stack_regs_mentioned_p (dest
))
466 src
= * get_true_reg (& SET_SRC (pat
));
468 switch (GET_CODE (src
))
470 /* ??? get_true_reg will make some of these cases redundant. */
477 record_note_if_dead (insn
, regstack
, XEXP (src
, 0), dest
);
478 record_note_if_dead (insn
, regstack
, XEXP (src
, 1), dest
);
488 record_note_if_dead (insn
, regstack
, XEXP (src
, 0), dest
);
494 if (GET_CODE (src
) == FIX
)
495 record_note_if_dead (insn
, regstack
, XEXP (src
, 0), dest
);
497 record_note_if_dead (insn
, regstack
, src
, dest
);
504 /* ??? This needs much improvement */
506 if (stack_regs_mentioned_p (pat
))
509 for (j
= 0; j
< XVECLEN (src
, 3); j
++)
510 record_note_if_dead (insn
, regstack
, XVECEXP (src
, 3, j
), dest
);
515 record_note_if_dead (insn
, regstack
, src
, dest
);
519 /* If a stack register appears in the src RTL, it is a bug, and
520 code should be added above to handle it. */
522 if (stack_regs_mentioned_p (src
))
527 /* Scan INSN, which is in BLOCK, and record the life & death of stack
528 registers in REGSTACK. This function is called to process insns from
529 the last insn in a block to the first. The actual scanning is done in
532 If a register is live after a CALL_INSN, but is not a value return
533 register for that CALL_INSN, then code is emitted to initialize that
534 register. The block_end[] data is kept accurate.
536 Existing death and unset notes for stack registers are deleted
537 before processing the insn. */
540 record_reg_life (insn
, block
, regstack
)
545 rtx note
, *note_link
;
547 if ((GET_CODE (insn
) != INSN
&& GET_CODE (insn
) != CALL_INSN
)
548 || INSN_DELETED_P (insn
))
551 /* Strip death notes for stack regs from this insn */
553 note_link
= ®_NOTES(insn
);
554 for (note
= *note_link
; note
; note
= XEXP (note
, 1))
555 if (STACK_REG_P (XEXP (note
, 0))
556 && (REG_NOTE_KIND (note
) == REG_DEAD
557 || REG_NOTE_KIND (note
) == REG_UNUSED
))
558 *note_link
= XEXP (note
, 1);
560 note_link
= &XEXP (note
, 1);
562 /* Process all patterns in the insn. */
564 if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
568 for (i
= 0; i
< XVECLEN (PATTERN (insn
), 0); i
++)
569 record_reg_life_pat (insn
, regstack
, XVECEXP (PATTERN (insn
), 0, i
));
571 else if (GET_MODE (insn
) == QImode
)
572 record_reg_life_pat (insn
, regstack
, PATTERN (insn
));
574 /* There might be a reg that is live after a function call.
575 Initialize it to zero so that the program does not crash. See comment
576 towards the end of stack_reg_life_analysis(). */
578 if (GET_CODE (insn
) == CALL_INSN
)
580 int reg
= FIRST_FLOAT_REG
;
582 /* If a stack reg is mentioned in a CALL_INSN, it must be as the
583 return value; conversely, if a float is returned, a stack reg
584 must be mentioned. */
586 if (stack_regs_mentioned_p (PATTERN (insn
)))
589 for (; reg
<= LAST_STACK_REG
; reg
++)
590 if (TEST_HARD_REG_BIT (regstack
->reg_set
, reg
))
594 /* The insn will use virtual register numbers, and so
595 convert_regs is expected to process these. But BLOCK_NUM
596 cannot be used on these insns, because they do not appear in
599 pat
= gen_rtx (SET
, VOIDmode
, FP_mode_reg
[reg
][(int) DFmode
],
600 CONST0_RTX (DFmode
));
601 init
= emit_insn_after (pat
, insn
);
602 PUT_MODE (init
, QImode
);
604 CLEAR_HARD_REG_BIT (regstack
->reg_set
, reg
);
606 /* If the CALL_INSN was the end of a block, move the
607 block_end to point to the new insn. */
609 if (block_end
[block
] == insn
)
610 block_end
[block
] = init
;
613 /* Some regs do not survive a CALL */
615 AND_COMPL_HARD_REG_SET (regstack
->reg_set
, call_used_reg_set
);
619 /* Find all basic blocks of the function, which starts with FIRST.
620 For each JUMP_INSN, build the chain of LABEL_REFS on each CODE_LABEL. */
628 register RTX_CODE prev_code
= BARRIER
;
629 register RTX_CODE code
;
631 /* Record where all the blocks start and end.
632 Record which basic blocks control can drop in to. */
635 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
637 /* Note that this loop must select the same block boundaries
638 as code in reg_to_stack. */
640 code
= GET_CODE (insn
);
642 if (code
== CODE_LABEL
643 || (prev_code
!= INSN
644 && prev_code
!= CALL_INSN
645 && prev_code
!= CODE_LABEL
646 && (code
== INSN
|| code
== CALL_INSN
|| code
== JUMP_INSN
)))
648 block_begin
[++block
] = insn
;
649 block_end
[block
] = insn
;
650 block_drops_in
[block
] = prev_code
!= BARRIER
;
652 else if (code
== INSN
|| code
== CALL_INSN
|| code
== JUMP_INSN
)
653 block_end
[block
] = insn
;
655 BLOCK_NUM (insn
) = block
;
657 if (code
== CODE_LABEL
)
658 LABEL_REFS (insn
) = insn
; /* delete old chain */
664 if (block
+ 1 != blocks
)
667 /* generate all label references to the correspondending jump insn */
668 for (block
= 0; block
< blocks
; block
++)
670 insn
= block_end
[block
];
672 if (GET_CODE (insn
) == JUMP_INSN
)
673 record_label_references (insn
, PATTERN (insn
));
677 /* Determine the which registers are live at the start of each basic
678 block of the function whose first insn is FIRST.
680 First, if the function returns a real_type, mark the function
681 return type as live at each return point, as the RTL may not give any
682 hint that the register is live.
684 Then, start with the last block and work back to the first block.
685 Similarly, work backwards within each block, insn by insn, recording
686 which regs are die and which are used (and therefore live) in the
687 hard reg set of block_stack_in[].
689 After processing each basic block, if there is a label at the start
690 of the block, propagate the live registers to all jumps to this block.
692 As a special case, if there are regs live in this block, that are
693 not live in a block containing a jump to this label, and the block
694 containing the jump has already been processed, we must propagate this
695 block's entry register life back to the block containing the jump, and
696 restart life analysis from there.
698 In the worst case, this function may traverse the insns
699 REG_STACK_SIZE times. This is necessary, since a jump towards the end
700 of the insns may not know that a reg is live at a target that is early
701 in the insns. So we back up and start over with the new reg live.
703 If there are registers that are live at the start of the function,
704 insns are emitted to initialize these registers. Something similar is
705 done after CALL_INSNs in record_reg_life. */
708 stack_reg_life_analysis (first
)
712 struct stack_def regstack
;
714 if (current_function_returns_real
)
716 /* Find all RETURN insns and mark them. */
718 for (block
= blocks
- 1; block
>= 0; block
--)
719 if (GET_CODE (block_end
[block
]) == JUMP_INSN
720 && GET_CODE (PATTERN (block_end
[block
])) == RETURN
)
721 SET_HARD_REG_BIT (block_out_reg_set
[block
], FIRST_STACK_REG
);
723 /* Mark of the end of last block if we "fall off" the end of the
724 function into the epilogue. */
726 if (GET_CODE (block_end
[blocks
-1]) != JUMP_INSN
727 || GET_CODE (PATTERN (block_end
[blocks
-1])) == RETURN
)
728 SET_HARD_REG_BIT (block_out_reg_set
[blocks
-1], FIRST_STACK_REG
);
731 /* now scan all blocks backward for stack register use */
736 register rtx insn
, prev
;
738 /* current register status at last instruction */
740 COPY_HARD_REG_SET (regstack
.reg_set
, block_out_reg_set
[block
]);
742 prev
= block_end
[block
];
746 prev
= PREV_INSN (insn
);
748 /* If the insn is a CALL_INSN, we need to ensure that
749 everything dies. But otherwise don't process unless there
750 are some stack regs present. */
752 if (GET_MODE (insn
) == QImode
|| GET_CODE (insn
) == CALL_INSN
)
753 record_reg_life (insn
, block
, ®stack
);
755 } while (insn
!= block_begin
[block
]);
757 /* Set the state at the start of the block. Mark that no
758 register mapping information known yet. */
760 COPY_HARD_REG_SET (block_stack_in
[block
].reg_set
, regstack
.reg_set
);
761 block_stack_in
[block
].top
= -2;
763 /* If there is a label, propagate our register life to all jumps
766 if (GET_CODE (insn
) == CODE_LABEL
)
769 int must_restart
= 0;
771 for (label
= LABEL_REFS (insn
); label
!= insn
;
772 label
= LABEL_NEXTREF (label
))
774 int jump_block
= BLOCK_NUM (CONTAINING_INSN (label
));
776 if (jump_block
< block
)
777 IOR_HARD_REG_SET (block_out_reg_set
[jump_block
],
778 block_stack_in
[block
].reg_set
);
781 /* The block containing the jump has already been
782 processed. If there are registers that were not known
783 to be live then, but are live now, we must back up
784 and restart life analysis from that point with the new
787 GO_IF_HARD_REG_SUBSET (block_stack_in
[block
].reg_set
,
788 block_out_reg_set
[jump_block
],
791 IOR_HARD_REG_SET (block_out_reg_set
[jump_block
],
792 block_stack_in
[block
].reg_set
);
805 if (block_drops_in
[block
])
806 IOR_HARD_REG_SET (block_out_reg_set
[block
-1],
807 block_stack_in
[block
].reg_set
);
813 /* If any reg is live at the start of the first block of a
814 function, then we must guarantee that the reg holds some value by
815 generating our own "load" of that register. Otherwise a 387 would
816 fault trying to access an empty register. */
818 HARD_REG_SET empty_regs
;
819 CLEAR_HARD_REG_SET (empty_regs
);
820 GO_IF_HARD_REG_SUBSET (block_stack_in
[0].reg_set
, empty_regs
,
824 /* Load zero into each live register. The fact that a register
825 appears live at the function start does not necessarily imply an error
826 in the user program: it merely means that we could not determine that
827 there wasn't such an error, just as -Wunused sometimes gives
828 "incorrect" warnings. In those cases, these initializations will do
831 Note that we are inserting virtual register references here:
832 these insns must be processed by convert_regs later. Also, these
833 insns will not be in block_number, so BLOCK_NUM() will fail for them. */
835 for (reg
= LAST_STACK_REG
; reg
>= FIRST_STACK_REG
; reg
--)
836 if (TEST_HARD_REG_BIT (block_stack_in
[0].reg_set
, reg
))
840 init_rtx
= gen_rtx (SET
, VOIDmode
, FP_mode_reg
[reg
][(int) DFmode
],
841 CONST0_RTX (DFmode
));
842 block_begin
[0] = emit_insn_after (init_rtx
, first
);
843 PUT_MODE (block_begin
[0], QImode
);
845 CLEAR_HARD_REG_BIT (block_stack_in
[0].reg_set
, reg
);
852 /*****************************************************************************
853 This section deals with stack register substition, and forms the second
855 *****************************************************************************/
857 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
858 the desired hard REGNO. */
861 replace_reg (reg
, regno
)
865 if (regno
< FIRST_STACK_REG
|| regno
> LAST_STACK_REG
866 || ! STACK_REG_P (*reg
))
869 if (GET_MODE_CLASS (GET_MODE (*reg
)) != MODE_FLOAT
)
872 *reg
= FP_mode_reg
[regno
][(int) GET_MODE (*reg
)];
875 /* Remove a note of type NOTE, which must be found, for register
876 number REGNO from INSN. Remove only one such note. */
879 remove_regno_note (insn
, note
, regno
)
884 register rtx
*note_link
, this;
886 note_link
= ®_NOTES(insn
);
887 for (this = *note_link
; this; this = XEXP (this, 1))
888 if (REG_NOTE_KIND (this) == note
889 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno
)
891 *note_link
= XEXP (this, 1);
895 note_link
= &XEXP (this, 1);
900 /* Find the hard register number of virtual register REG in REGSTACK.
901 The hard register number is relative to the top of the stack. -1 is
902 returned if the register is not found. */
905 get_hard_regnum (regstack
, reg
)
911 if (! STACK_REG_P (reg
))
914 for (i
= regstack
->top
; i
>= 0; i
--)
915 if (regstack
->reg
[i
] == REGNO (reg
))
918 return i
>= 0 ? (FIRST_STACK_REG
+ regstack
->top
- i
) : -1;
921 /* Delete INSN from the RTL. Mark the insn, but don't remove it from
922 the chain of insns. Doing so could confuse block_begin and block_end
923 if this were the only insn in the block. */
926 delete_insn_for_stacker (insn
)
929 PUT_CODE (insn
, NOTE
);
930 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
931 NOTE_SOURCE_FILE (insn
) = 0;
932 INSN_DELETED_P (insn
) = 1;
935 /* Emit an insn to pop virtual register REG before or after INSN.
936 REGSTACK is the stack state after INSN and is updated to reflect this
937 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
938 is represented as a SET whose destination is the register to be popped
939 and source is the top of stack. A death note for the top of stack
940 cases the movdf pattern to pop. */
943 emit_pop_insn (insn
, regstack
, reg
, when
)
949 rtx pop_insn
, pop_rtx
;
952 hard_regno
= get_hard_regnum (regstack
, reg
);
954 if (hard_regno
< FIRST_STACK_REG
)
957 pop_rtx
= gen_rtx (SET
, VOIDmode
, FP_mode_reg
[hard_regno
][(int) DFmode
],
958 FP_mode_reg
[FIRST_STACK_REG
][(int) DFmode
]);
960 pop_insn
= (*when
) (pop_rtx
, insn
);
961 PUT_MODE (pop_insn
, VOIDmode
);
963 REG_NOTES (pop_insn
) = gen_rtx (EXPR_LIST
, REG_DEAD
,
964 FP_mode_reg
[FIRST_STACK_REG
][(int) DFmode
],
965 REG_NOTES (pop_insn
));
967 regstack
->reg
[regstack
->top
- (hard_regno
- FIRST_STACK_REG
)]
968 = regstack
->reg
[regstack
->top
];
970 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (reg
));
975 /* Emit an insn before or after INSN to swap virtual register REG with the
976 top of stack. WHEN should be `emit_insn_before' or `emit_insn_before'
977 REGSTACK is the stack state before the swap, and is updated to reflect
978 the swap. A swap insn is represented as a PARALLEL of two patterns:
979 each pattern moves one reg to the other.
981 If REG is already at the top of the stack, no insn is emitted. */
984 emit_hard_swap_insn (insn
, regstack
, hard_regno
, when
)
991 rtx swap_rtx
, swap_insn
;
994 if (hard_regno
== FIRST_STACK_REG
)
997 swap_rtx
= gen_swapdf (FP_mode_reg
[hard_regno
][(int) DFmode
],
998 FP_mode_reg
[FIRST_STACK_REG
][(int) DFmode
]);
999 swap_insn
= (*when
) (swap_rtx
, insn
);
1000 PUT_MODE (swap_insn
, VOIDmode
);
1002 other
= regstack
->top
- (hard_regno
- FIRST_STACK_REG
);
1004 tmp
= regstack
->reg
[other
];
1005 regstack
->reg
[other
] = regstack
->reg
[regstack
->top
];
1006 regstack
->reg
[regstack
->top
] = tmp
;
1009 /* Emit an insn before or after INSN to swap virtual register REG with the
1010 top of stack. See comments before emit_hard_swap_insn. */
1013 emit_swap_insn (insn
, regstack
, reg
, when
)
1021 hard_regno
= get_hard_regnum (regstack
, reg
);
1023 emit_hard_swap_insn (insn
, regstack
, hard_regno
, when
);
1026 /* Handle a move to or from a stack register in PAT, which is in INSN.
1027 REGSTACK is the current stack. */
1030 move_for_stack_reg (insn
, regstack
, pat
)
1035 rtx
*src
= get_true_reg (&SET_SRC (pat
));
1036 rtx
*dest
= get_true_reg (&SET_DEST (pat
));
1039 if (STACK_REG_P (*src
) && STACK_REG_P (*dest
))
1041 /* Write from one stack reg to another. If SRC dies here, then
1042 just change the register mapping and delete the insn. */
1044 note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src
));
1049 /* If this is a no-op move, there must not be a REG_DEAD note. */
1050 if (REGNO (*src
) == REGNO (*dest
))
1053 for (i
= regstack
->top
; i
>= 0; i
--)
1054 if (regstack
->reg
[i
] == REGNO (*src
))
1057 /* The source must be live, and the dest must be dead. */
1058 if (i
< 0 || get_hard_regnum (regstack
, *dest
) >= FIRST_STACK_REG
)
1061 /* It is possible that the dest is unused after this insn.
1062 If so, just pop the src. */
1064 if (find_regno_note (insn
, REG_UNUSED
, REGNO (*dest
)))
1066 emit_pop_insn (insn
, regstack
, *src
, emit_insn_after
);
1068 delete_insn_for_stacker (insn
);
1072 regstack
->reg
[i
] = REGNO (*dest
);
1074 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1075 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (*src
));
1077 delete_insn_for_stacker (insn
);
1082 /* The source reg does not die. */
1084 /* If this appears to be a no-op move, delete it, or else it
1085 will confuse the machine description output patterns. But if
1086 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1087 for REG_UNUSED will not work for deleted insns. */
1089 if (REGNO (*src
) == REGNO (*dest
))
1091 if (find_regno_note (insn
, REG_UNUSED
, REGNO (*dest
)))
1092 emit_pop_insn (insn
, regstack
, *dest
, emit_insn_after
);
1094 delete_insn_for_stacker (insn
);
1098 /* The destination ought to be dead */
1099 if (get_hard_regnum (regstack
, *dest
) >= FIRST_STACK_REG
)
1102 replace_reg (src
, get_hard_regnum (regstack
, *src
));
1104 regstack
->reg
[++regstack
->top
] = REGNO (*dest
);
1105 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1106 replace_reg (dest
, FIRST_STACK_REG
);
1108 else if (STACK_REG_P (*src
))
1110 /* Save from a stack reg to MEM, or possibly integer reg. Since
1111 only top of stack may be saved, emit an exchange first if
1114 emit_swap_insn (insn
, regstack
, *src
, emit_insn_before
);
1116 note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src
));
1119 replace_reg (&XEXP (note
, 0), FIRST_STACK_REG
);
1121 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (*src
));
1124 replace_reg (src
, FIRST_STACK_REG
);
1126 else if (STACK_REG_P (*dest
))
1128 /* Load from MEM, or possibly integer REG or constant, into the
1129 stack regs. The actual target is always the top of the
1130 stack. The stack mapping is changed to reflect that DEST is
1131 now at top of stack. */
1133 /* The destination ought to be dead */
1134 if (get_hard_regnum (regstack
, *dest
) >= FIRST_STACK_REG
)
1137 if (regstack
->top
>= REG_STACK_SIZE
)
1140 regstack
->reg
[++regstack
->top
] = REGNO (*dest
);
1141 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1142 replace_reg (dest
, FIRST_STACK_REG
);
1148 /* Handle a comparison. Special care needs to be taken to avoid
1149 causing comparisons that a 387 cannot do correctly, such as EQ.
1151 Also, a pop insn may need to be emitted. The 387 does have an
1152 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1153 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1157 compare_for_stack_reg (insn
, regstack
, pat
)
1163 rtx src1_note
, src2_note
;
1165 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 0));
1166 src2
= get_true_reg (&XEXP (SET_SRC (pat
), 1));
1168 /* The first argument must always be a stack reg. */
1171 if (! STACK_REG_P (*src1
))
1174 /* We will fix any death note later. */
1176 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1178 if (STACK_REG_P (*src2
))
1179 src2_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src2
));
1183 emit_swap_insn (insn
, regstack
, *src1
, emit_insn_before
);
1185 replace_reg (src1
, FIRST_STACK_REG
);
1187 if (STACK_REG_P (*src2
))
1188 replace_reg (src2
, get_hard_regnum (regstack
, *src2
));
1192 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (XEXP (src1_note
, 0)));
1193 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
1197 /* If the second operand dies, handle that. But if the operands are
1198 the same stack register, don't bother, because only one death is
1199 needed, and it was just handled. */
1202 && ! (STACK_REG_P (*src1
)
1203 && STACK_REG_P (*src2
)
1204 && REGNO (*src1
) == REGNO (*src2
)))
1206 /* As a special case, two regs may die in this insn if src2 is
1207 next to top of stack and the top of stack also dies. Since
1208 we have already popped src1, "next to top of stack" is really
1209 at top (FIRST_STACK_REG) now. */
1211 if (get_hard_regnum (regstack
, XEXP (src2_note
, 0)) == FIRST_STACK_REG
1214 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (XEXP (src2_note
, 0)));
1215 replace_reg (&XEXP (src2_note
, 0), FIRST_STACK_REG
+ 1);
1220 /* The 386 can only represent death of the first operand in
1221 the case handled above. In all other cases, emit a separate
1222 pop and remove the death note from here. */
1224 remove_regno_note (insn
, REG_DEAD
, REGNO (XEXP (src2_note
, 0)));
1226 emit_pop_insn (insn
, regstack
, XEXP (src2_note
, 0),
1232 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1233 is the current register layout. */
1236 subst_stack_regs_pat (insn
, regstack
, pat
)
1242 rtx
*src1
= 0, *src2
;
1243 rtx src1_note
, src2_note
;
1245 if (GET_CODE (pat
) != SET
)
1248 dest
= get_true_reg (&SET_DEST (pat
));
1249 src
= get_true_reg (&SET_SRC (pat
));
1251 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1253 if (*dest
!= cc0_rtx
1254 && (STACK_REG_P (*src
)
1255 || (STACK_REG_P (*dest
)
1256 && (GET_CODE (*src
) == REG
|| GET_CODE (*src
) == MEM
1257 || GET_CODE (*src
) == CONST_DOUBLE
))))
1258 move_for_stack_reg (insn
, regstack
, pat
);
1260 switch (GET_CODE (SET_SRC (pat
)))
1263 compare_for_stack_reg (insn
, regstack
, pat
);
1267 regstack
->reg
[++regstack
->top
] = REGNO (*dest
);
1268 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1269 replace_reg (dest
, FIRST_STACK_REG
);
1273 /* This is a `tstM2' case. */
1274 if (*dest
!= cc0_rtx
)
1284 /* These insns only operate on the top of the stack. DEST might
1285 be cc0_rtx if we're processing a tstM pattern. Also, it's
1286 possible that the tstM case results in a REG_DEAD note on the
1290 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 0));
1292 emit_swap_insn (insn
, regstack
, *src1
, emit_insn_before
);
1294 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1296 if (STACK_REG_P (*dest
))
1297 replace_reg (dest
, FIRST_STACK_REG
);
1301 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
1303 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (*src1
));
1306 replace_reg (src1
, FIRST_STACK_REG
);
1312 /* On i386, reversed forms of subM3 and divM3 exist for
1313 MODE_FLOAT, so the same code that works for addM3 and mulM3
1317 /* These insns can accept the top of stack as a destination
1318 from a stack reg or mem, or can use the top of stack as a
1319 source and some other stack register (possibly top of stack)
1320 as a destination. */
1322 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 0));
1323 src2
= get_true_reg (&XEXP (SET_SRC (pat
), 1));
1325 /* We will fix any death note later. */
1327 if (STACK_REG_P (*src1
))
1328 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1331 if (STACK_REG_P (*src2
))
1332 src2_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src2
));
1336 /* If either operand is not a stack register, then the dest
1337 must be top of stack. */
1339 if (! STACK_REG_P (*src1
) || ! STACK_REG_P (*src2
))
1340 emit_swap_insn (insn
, regstack
, *dest
, emit_insn_before
);
1343 /* Both operands are REG. If neither operand is already
1344 at the top of stack, choose to make the one that is the dest
1345 the new top of stack.
1347 ??? A later optimization here would be to look forward
1348 in the insns and see which source reg will be needed at top
1349 of stack soonest. */
1351 int src1_hard_regnum
, src2_hard_regnum
;
1353 src1_hard_regnum
= get_hard_regnum (regstack
, *src1
);
1354 src2_hard_regnum
= get_hard_regnum (regstack
, *src2
);
1355 if (src1_hard_regnum
== -1 || src2_hard_regnum
== -1)
1358 if (src1_hard_regnum
!= FIRST_STACK_REG
1359 && src2_hard_regnum
!= FIRST_STACK_REG
)
1360 emit_swap_insn (insn
, regstack
, *dest
, emit_insn_before
);
1363 if (STACK_REG_P (*src1
))
1364 replace_reg (src1
, get_hard_regnum (regstack
, *src1
));
1365 if (STACK_REG_P (*src2
))
1366 replace_reg (src2
, get_hard_regnum (regstack
, *src2
));
1370 /* If the register that dies is at the top of stack, then
1371 the destination is somewhere else - merely substitute it.
1372 But if the reg that dies is not at top of stack, then
1373 move the top of stack to the dead reg, as though we had
1374 done the insn and then a store-with-pop. */
1376 if (REGNO (XEXP (src1_note
, 0)) == regstack
->reg
[regstack
->top
])
1378 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1379 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
1383 int regno
= get_hard_regnum (regstack
, XEXP (src1_note
, 0));
1385 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1386 replace_reg (dest
, regno
);
1388 regstack
->reg
[regstack
->top
- (regno
- FIRST_STACK_REG
)]
1389 = regstack
->reg
[regstack
->top
];
1392 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
1393 REGNO (XEXP (src1_note
, 0)));
1394 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
1399 if (REGNO (XEXP (src2_note
, 0)) == regstack
->reg
[regstack
->top
])
1401 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1402 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
1406 int regno
= get_hard_regnum (regstack
, XEXP (src2_note
, 0));
1408 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1409 replace_reg (dest
, regno
);
1411 regstack
->reg
[regstack
->top
- (regno
- FIRST_STACK_REG
)]
1412 = regstack
->reg
[regstack
->top
];
1415 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
1416 REGNO (XEXP (src2_note
, 0)));
1417 replace_reg (&XEXP (src2_note
, 0), FIRST_STACK_REG
);
1422 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1423 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
1433 /* Substitute stack hard reg numbers for stack virtual registers in
1434 INSN. Non-stack register numbers are not changed. REGSTACK is the
1435 current stack content. Insns may be emitted as needed to arrange the
1436 stack for the 387 based on the contents of the insn. */
1439 subst_stack_regs (insn
, regstack
)
1443 register rtx
*note_link
, note
;
1446 if ((GET_CODE (insn
) != INSN
&& GET_CODE (insn
) != CALL_INSN
)
1447 || INSN_DELETED_P (insn
))
1450 /* The stack should be empty at a call. */
1452 if (GET_CODE (insn
) == CALL_INSN
)
1453 for (i
= FIRST_STACK_REG
; i
<= LAST_STACK_REG
; i
++)
1454 if (TEST_HARD_REG_BIT (regstack
->reg_set
, i
))
1457 /* Do the actual substitution if any stack regs are mentioned.
1458 Since we only record whether entire insn mentions stack regs, and
1459 subst_stack_regs_pat only works for patterns that contain stack regs,
1460 we must check each pattern in a parallel here. A call_value_pop could
1463 if (GET_MODE (insn
) == QImode
)
1465 if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
1466 for (i
= 0; i
< XVECLEN (PATTERN (insn
) , 0); i
++)
1468 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn
), 0, i
)))
1469 subst_stack_regs_pat (insn
, regstack
,
1470 XVECEXP (PATTERN (insn
), 0, i
));
1473 subst_stack_regs_pat (insn
, regstack
, PATTERN (insn
));
1476 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
1477 REG_UNUSED will already have been dealt with, so just return. */
1479 if (INSN_DELETED_P (insn
))
1482 /* If there is a REG_UNUSED note on a stack register on this insn,
1483 the indicated reg must be popped. The REG_UNUSED note is removed,
1484 since the form of the newly emitted pop insn references the reg,
1485 making it no longer `unset'. */
1487 note_link
= ®_NOTES(insn
);
1488 for (note
= *note_link
; note
; note
= XEXP (note
, 1))
1489 if (REG_NOTE_KIND (note
) == REG_UNUSED
&& STACK_REG_P (XEXP (note
, 0)))
1491 *note_link
= XEXP (note
, 1);
1492 emit_pop_insn (insn
, regstack
, XEXP (note
, 0),
1496 note_link
= &XEXP (note
, 1);
1499 /* Change the organization of the stack so that it fits a new basic
1500 block. Some registers might have to be popped, but there can never be
1501 a register live in the new block that is not now live.
1503 Insert any needed insns after INSN. OLD is the original stack
1504 layout, and NEW is the desired form. OLD is updated to reflect the
1505 code emitted, ie, it will be the same as NEW upon return.
1507 This function will not preserve block_end[]. But that information
1508 is no longer needed once this has executed. */
1511 change_stack (insn
, old
, new)
1518 /* We will be inserting new insns after INSN, by first finding the
1519 next insn, and inserting before it. */
1521 insn
= NEXT_INSN (insn
);
1523 /* Pop any registers that are not needed in the new block. */
1525 for (reg
= old
->top
; reg
>= 0; reg
--)
1526 if (! TEST_HARD_REG_BIT (new->reg_set
, old
->reg
[reg
]))
1527 emit_pop_insn (insn
, old
, FP_mode_reg
[old
->reg
[reg
]][(int) DFmode
],
1532 /* If the new block has never been processed, then it can inherit
1533 the old stack order. */
1535 new->top
= old
->top
;
1536 bcopy (old
->reg
, new->reg
, sizeof (new->reg
));
1540 /* This block has been entered before, and we must match the
1541 previously selected stack order. */
1543 /* By now, the only difference should be the order of the stack,
1544 not their depth or liveliness. */
1546 GO_IF_HARD_REG_EQUAL (old
->reg_set
, new->reg_set
, win
);
1552 if (old
->top
!= new->top
)
1555 /* Loop here emitting swaps until the stack is correct. The
1556 worst case number of swaps emitted is N + 2, where N is the
1557 depth of the stack. In some cases, the reg at the top of
1558 stack may be correct, but swapped anyway in order to fix
1559 other regs. But since we never swap any other reg away from
1560 its correct slot, this algorithm will converge. */
1564 /* Swap the reg at top of stack into the position it is
1565 supposed to be in, until the correct top of stack appears. */
1567 while (old
->reg
[old
->top
] != new->reg
[new->top
])
1569 for (reg
= new->top
; reg
>= 0; reg
--)
1570 if (new->reg
[reg
] == old
->reg
[old
->top
])
1576 emit_swap_insn (insn
, old
,
1577 FP_mode_reg
[old
->reg
[reg
]][(int) DFmode
],
1581 /* See if any regs remain incorrect. If so, bring an
1582 incorrect reg to the top of stack, and let the while loop
1585 for (reg
= new->top
; reg
>= 0; reg
--)
1586 if (new->reg
[reg
] != old
->reg
[reg
])
1588 emit_swap_insn (insn
, old
,
1589 FP_mode_reg
[old
->reg
[reg
]][(int) DFmode
],
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
,
1795 FP_mode_reg
[regstack
.reg
[reg
]][(int) DFmode
],
1799 /* Check expression PAT, which is in INSN, for label references. if
1800 one is found, print the block number of destination to FILE. */
1803 print_blocks (file
, insn
, pat
)
1807 register RTX_CODE code
= GET_CODE (pat
);
1811 if (code
== LABEL_REF
)
1813 register rtx label
= XEXP (pat
, 0);
1815 if (GET_CODE (label
) != CODE_LABEL
)
1818 fprintf (file
, " %d", BLOCK_NUM (label
));
1823 fmt
= GET_RTX_FORMAT (code
);
1824 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1827 print_blocks (file
, insn
, XEXP (pat
, i
));
1831 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
1832 print_blocks (file
, insn
, XVECEXP (pat
, i
, j
));
1837 /* Write information about stack registers and stack blocks into FILE.
1838 This is part of making a debugging dump. */
1840 dump_stack_info (file
)
1845 fprintf (file
, "\n%d stack blocks.\n", blocks
);
1846 for (block
= 0; block
< blocks
; block
++)
1848 register rtx head
, jump
, end
;
1851 fprintf (file
, "\nStack block %d: first insn %d, last %d.\n",
1852 block
, INSN_UID (block_begin
[block
]),
1853 INSN_UID (block_end
[block
]));
1855 head
= block_begin
[block
];
1857 fprintf (file
, "Reached from blocks: ");
1858 if (GET_CODE (head
) == CODE_LABEL
)
1859 for (jump
= LABEL_REFS (head
);
1861 jump
= LABEL_NEXTREF (jump
))
1863 register int from_block
= BLOCK_NUM (CONTAINING_INSN (jump
));
1864 fprintf (file
, " %d", from_block
);
1866 if (block_drops_in
[block
])
1867 fprintf (file
, " previous");
1869 fprintf (file
, "\nlive stack registers on block entry: ");
1870 for (regno
= FIRST_STACK_REG
; regno
<= LAST_STACK_REG
; regno
++)
1872 if (TEST_HARD_REG_BIT (block_stack_in
[block
].reg_set
, regno
))
1873 fprintf (file
, "%d ", regno
);
1876 fprintf (file
, "\nlive stack registers on block exit: ");
1877 for (regno
= FIRST_STACK_REG
; regno
<= LAST_STACK_REG
; regno
++)
1879 if (TEST_HARD_REG_BIT (block_out_reg_set
[block
], regno
))
1880 fprintf (file
, "%d ", regno
);
1883 end
= block_end
[block
];
1885 fprintf (file
, "\nJumps to blocks: ");
1886 if (GET_CODE (end
) == JUMP_INSN
)
1887 print_blocks (file
, end
, PATTERN (end
));
1889 if (block
+ 1 < blocks
&& block_drops_in
[block
+1])
1890 fprintf (file
, " next");
1891 else if (block
+ 1 == blocks
1892 || (GET_CODE (end
) == JUMP_INSN
1893 && GET_CODE (PATTERN (end
)) == RETURN
))
1894 fprintf (file
, " return");
1896 fprintf (file
, "\n");
1900 /* Report an error at line LINE of file FILE.
1901 S is a string and an arg for `printf'. */
1903 /* Report an fatal error at the line number of the insn INSN (ASM_OPERAND).
1904 S1, S2 is a string and an arg for `printf'. */
1907 fatal_for_asm (insn
, s1
, s2
)
1913 rtx body
= PATTERN (insn
);
1916 /* Find the (or one of the) ASM_OPERANDS in the insn. */
1917 if (GET_CODE (body
) == SET
&& GET_CODE (SET_SRC (body
)) == ASM_OPERANDS
)
1918 asmop
= SET_SRC (body
);
1919 else if (GET_CODE (body
) == ASM_OPERANDS
)
1921 else if (GET_CODE (body
) == PARALLEL
1922 && GET_CODE (XVECEXP (body
, 0, 0)) == SET
)
1923 asmop
= SET_SRC (XVECEXP (body
, 0, 0));
1924 else if (GET_CODE (body
) == PARALLEL
1925 && GET_CODE (XVECEXP (body
, 0, 0)) == ASM_OPERANDS
)
1926 asmop
= XVECEXP (body
, 0, 0);
1930 filename
= ASM_OPERANDS_SOURCE_FILE (asmop
);
1931 line
= ASM_OPERANDS_SOURCE_LINE (asmop
);
1933 fprintf (stderr
, s1
);
1936 error_with_file_and_line (filename
, line
, s2
, NULL
, NULL
);
1939 #endif /* STACK_REGS */