1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 1998 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This pass converts stack-like registers from the "flat register
22 file" model that gcc uses, to a stack convention that the 387 uses.
24 * The form of the input:
26 On input, the function consists of insn that have had their
27 registers fully allocated to a set of "virtual" registers. Note that
28 the word "virtual" is used differently here than elsewhere in gcc: for
29 each virtual stack reg, there is a hard reg, but the mapping between
30 them is not known until this pass is run. On output, hard register
31 numbers have been substituted, and various pop and exchange insns have
32 been emitted. The hard register numbers and the virtual register
33 numbers completely overlap - before this pass, all stack register
34 numbers are virtual, and afterward they are all hard.
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 the SET_SRC and SET_DEST are both 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 There are several rules on the usage of stack-like regs in
87 asm_operands insns. These rules apply only to the operands that are
90 1. Given a set of input regs that die in an asm_operands, it is
91 necessary to know which are implicitly popped by the asm, and
92 which must be explicitly popped by gcc.
94 An input reg that is implicitly popped by the asm must be
95 explicitly clobbered, unless it is constrained to match an
98 2. For any input reg that is implicitly popped by an asm, it is
99 necessary to know how to adjust the stack to compensate for the pop.
100 If any non-popped input is closer to the top of the reg-stack than
101 the implicitly popped reg, it would not be possible to know what the
102 stack looked like - it's not clear how the rest of the stack "slides
105 All implicitly popped input regs must be closer to the top of
106 the reg-stack than any input that is not implicitly popped.
108 3. It is possible that if an input dies in an insn, reload might
109 use the input reg for an output reload. Consider this example:
111 asm ("foo" : "=t" (a) : "f" (b));
113 This asm says that input B is not popped by the asm, and that
114 the asm pushes a result onto the reg-stack, ie, the stack is one
115 deeper after the asm than it was before. But, it is possible that
116 reload will think that it can use the same reg for both the input and
117 the output, if input B dies in this insn.
119 If any input operand uses the "f" constraint, all output reg
120 constraints must use the "&" earlyclobber.
122 The asm above would be written as
124 asm ("foo" : "=&t" (a) : "f" (b));
126 4. Some operands need to be in particular places on the stack. All
127 output operands fall in this category - there is no other way to
128 know which regs the outputs appear in unless the user indicates
129 this in the constraints.
131 Output operands must specifically indicate which reg an output
132 appears in after an asm. "=f" is not allowed: the operand
133 constraints must select a class with a single reg.
135 5. Output operands may not be "inserted" between existing stack regs.
136 Since no 387 opcode uses a read/write operand, all output operands
137 are dead before the asm_operands, and are pushed by the asm_operands.
138 It makes no sense to push anywhere but the top of the reg-stack.
140 Output operands must start at the top of the reg-stack: output
141 operands may not "skip" a reg.
143 6. Some asm statements may need extra stack space for internal
144 calculations. This can be guaranteed by clobbering stack registers
145 unrelated to the inputs and outputs.
147 Here are a couple of reasonable asms to want to write. This asm
148 takes one input, which is internally popped, and produces two outputs.
150 asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
152 This asm takes two inputs, which are popped by the fyl2xp1 opcode,
153 and replaces them with one output. The user must code the "st(1)"
154 clobber for reg-stack.c to know that fyl2xp1 pops both inputs.
156 asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
164 #include "insn-config.h"
166 #include "hard-reg-set.h"
168 #include "insn-flags.h"
174 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
176 /* This is the basic stack record. TOP is an index into REG[] such
177 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
179 If TOP is -2, REG[] is not yet initialized. Stack initialization
180 consists of placing each live reg in array `reg' and setting `top'
183 REG_SET indicates which registers are live. */
185 typedef struct stack_def
187 int top
; /* index to top stack element */
188 HARD_REG_SET reg_set
; /* set of live registers */
189 char reg
[REG_STACK_SIZE
]; /* register - stack mapping */
192 /* highest instruction uid */
193 static int max_uid
= 0;
195 /* Number of basic blocks in the current function. */
198 /* Element N is first insn in basic block N.
199 This info lasts until we finish compiling the function. */
200 static rtx
*block_begin
;
202 /* Element N is last insn in basic block N.
203 This info lasts until we finish compiling the function. */
204 static rtx
*block_end
;
206 /* Element N is nonzero if control can drop into basic block N */
207 static char *block_drops_in
;
209 /* Element N says all about the stack at entry block N */
210 static stack block_stack_in
;
212 /* Element N says all about the stack life at the end of block N */
213 static HARD_REG_SET
*block_out_reg_set
;
215 /* This is where the BLOCK_NUM values are really stored. This is set
216 up by find_blocks and used there and in life_analysis. It can be used
217 later, but only to look up an insn that is the head or tail of some
218 block. life_analysis and the stack register conversion process can
219 add insns within a block. */
220 static int *block_number
;
222 /* This is the register file for all register after conversion */
224 FP_mode_reg
[LAST_STACK_REG
+1-FIRST_STACK_REG
][(int) MAX_MACHINE_MODE
];
226 #define FP_MODE_REG(regno,mode) \
227 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int)(mode)])
229 /* Get the basic block number of an insn. See note at block_number
230 definition are validity of this information. */
232 #define BLOCK_NUM(INSN) \
233 ((INSN_UID (INSN) > max_uid) \
234 ? (abort() , -1) : block_number[INSN_UID (INSN)])
236 extern rtx forced_labels
;
238 /* Forward declarations */
240 static void mark_regs_pat
PROTO((rtx
, HARD_REG_SET
*));
241 static void straighten_stack
PROTO((rtx
, stack
));
242 static void pop_stack
PROTO((stack
, int));
243 static void record_label_references
PROTO((rtx
, rtx
));
244 static rtx
*get_true_reg
PROTO((rtx
*));
246 static void record_asm_reg_life
PROTO((rtx
, stack
));
247 static void record_reg_life_pat
PROTO((rtx
, HARD_REG_SET
*,
248 HARD_REG_SET
*, int));
249 static int get_asm_operand_n_inputs
PROTO((rtx
));
250 static void record_reg_life
PROTO((rtx
, int, stack
));
251 static void find_blocks
PROTO((rtx
));
252 static rtx stack_result
PROTO((tree
));
253 static void stack_reg_life_analysis
PROTO((rtx
, HARD_REG_SET
*));
254 static void replace_reg
PROTO((rtx
*, int));
255 static void remove_regno_note
PROTO((rtx
, enum reg_note
, int));
256 static int get_hard_regnum
PROTO((stack
, rtx
));
257 static void delete_insn_for_stacker
PROTO((rtx
));
258 static rtx emit_pop_insn
PROTO((rtx
, stack
, rtx
, rtx (*) ()));
259 static void emit_swap_insn
PROTO((rtx
, stack
, rtx
));
260 static void move_for_stack_reg
PROTO((rtx
, stack
, rtx
));
261 static void swap_rtx_condition
PROTO((rtx
));
262 static void compare_for_stack_reg
PROTO((rtx
, stack
, rtx
));
263 static void subst_stack_regs_pat
PROTO((rtx
, stack
, rtx
));
264 static void subst_asm_stack_regs
PROTO((rtx
, stack
));
265 static void subst_stack_regs
PROTO((rtx
, stack
));
266 static void change_stack
PROTO((rtx
, stack
, stack
, rtx (*) ()));
268 static void goto_block_pat
PROTO((rtx
, stack
, rtx
));
269 static void convert_regs
PROTO((void));
270 static void print_blocks
PROTO((FILE *, rtx
, rtx
));
271 static void dump_stack_info
PROTO((FILE *));
273 /* Mark all registers needed for this pattern. */
276 mark_regs_pat (pat
, set
)
280 enum machine_mode mode
;
284 if (GET_CODE (pat
) == SUBREG
)
286 mode
= GET_MODE (pat
);
287 regno
= SUBREG_WORD (pat
);
288 regno
+= REGNO (SUBREG_REG (pat
));
291 regno
= REGNO (pat
), mode
= GET_MODE (pat
);
293 for (count
= HARD_REGNO_NREGS (regno
, mode
);
294 count
; count
--, regno
++)
295 SET_HARD_REG_BIT (*set
, regno
);
298 /* Reorganise the stack into ascending numbers,
302 straighten_stack (insn
, regstack
)
306 struct stack_def temp_stack
;
309 /* If there is only a single register on the stack, then the stack is
310 already in increasing order and no reorganization is needed.
312 Similarly if the stack is empty. */
313 if (regstack
->top
<= 0)
316 temp_stack
.reg_set
= regstack
->reg_set
;
318 for (top
= temp_stack
.top
= regstack
->top
; top
>= 0; top
--)
319 temp_stack
.reg
[top
] = FIRST_STACK_REG
+ temp_stack
.top
- top
;
321 change_stack (insn
, regstack
, &temp_stack
, emit_insn_after
);
324 /* Pop a register from the stack */
327 pop_stack (regstack
, regno
)
331 int top
= regstack
->top
;
333 CLEAR_HARD_REG_BIT (regstack
->reg_set
, regno
);
335 /* If regno was not at the top of stack then adjust stack */
336 if (regstack
->reg
[top
] != regno
)
339 for (i
= regstack
->top
; i
>= 0; i
--)
340 if (regstack
->reg
[i
] == regno
)
343 for (j
= i
; j
< top
; j
++)
344 regstack
->reg
[j
] = regstack
->reg
[j
+ 1];
350 /* Return non-zero if any stack register is mentioned somewhere within PAT. */
353 stack_regs_mentioned_p (pat
)
359 if (STACK_REG_P (pat
))
362 fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
363 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
369 for (j
= XVECLEN (pat
, i
) - 1; j
>= 0; j
--)
370 if (stack_regs_mentioned_p (XVECEXP (pat
, i
, j
)))
373 else if (fmt
[i
] == 'e' && stack_regs_mentioned_p (XEXP (pat
, i
)))
380 /* Convert register usage from "flat" register file usage to a "stack
381 register file. FIRST is the first insn in the function, FILE is the
384 First compute the beginning and end of each basic block. Do a
385 register life analysis on the stack registers, recording the result
386 for the head and tail of each basic block. The convert each insn one
387 by one. Run a last jump_optimize() pass, if optimizing, to eliminate
388 any cross-jumping created when the converter inserts pop insns.*/
391 reg_to_stack (first
, file
)
397 int stack_reg_seen
= 0;
398 enum machine_mode mode
;
399 HARD_REG_SET stackentry
;
401 CLEAR_HARD_REG_SET (stackentry
);
404 static int initialised
;
408 initialised
= 1; /* This array can not have been previously
409 initialised, because the rtx's are
410 thrown away between compilations of
413 for (i
= FIRST_STACK_REG
; i
<= LAST_STACK_REG
; i
++)
415 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_FLOAT
); mode
!= VOIDmode
;
416 mode
= GET_MODE_WIDER_MODE (mode
))
417 FP_MODE_REG (i
, mode
) = gen_rtx_REG (mode
, i
);
418 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT
); mode
!= VOIDmode
;
419 mode
= GET_MODE_WIDER_MODE (mode
))
420 FP_MODE_REG (i
, mode
) = gen_rtx_REG (mode
, i
);
425 /* Count the basic blocks. Also find maximum insn uid. */
427 register RTX_CODE prev_code
= BARRIER
;
428 register RTX_CODE code
;
429 register int before_function_beg
= 1;
433 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
435 /* Note that this loop must select the same block boundaries
436 as code in find_blocks. Also note that this code is not the
437 same as that used in flow.c. */
439 if (INSN_UID (insn
) > max_uid
)
440 max_uid
= INSN_UID (insn
);
442 code
= GET_CODE (insn
);
444 if (code
== CODE_LABEL
445 || (prev_code
!= INSN
446 && prev_code
!= CALL_INSN
447 && prev_code
!= CODE_LABEL
448 && GET_RTX_CLASS (code
) == 'i'))
451 if (code
== NOTE
&& NOTE_LINE_NUMBER (insn
) == NOTE_INSN_FUNCTION_BEG
)
452 before_function_beg
= 0;
454 /* Remember whether or not this insn mentions an FP regs.
455 Check JUMP_INSNs too, in case someone creates a funny PARALLEL. */
457 if (GET_RTX_CLASS (code
) == 'i'
458 && stack_regs_mentioned_p (PATTERN (insn
)))
461 PUT_MODE (insn
, QImode
);
463 /* Note any register passing parameters. */
465 if (before_function_beg
&& code
== INSN
466 && GET_CODE (PATTERN (insn
)) == USE
)
467 record_reg_life_pat (PATTERN (insn
), (HARD_REG_SET
*) 0,
471 PUT_MODE (insn
, VOIDmode
);
473 if (code
== CODE_LABEL
)
474 LABEL_REFS (insn
) = insn
; /* delete old chain */
481 /* If no stack register reference exists in this insn, there isn't
482 anything to convert. */
484 if (! stack_reg_seen
)
487 /* If there are stack registers, there must be at least one block. */
492 /* Allocate some tables that last till end of compiling this function
493 and some needed only in find_blocks and life_analysis. */
495 block_begin
= (rtx
*) alloca (blocks
* sizeof (rtx
));
496 block_end
= (rtx
*) alloca (blocks
* sizeof (rtx
));
497 block_drops_in
= (char *) alloca (blocks
);
499 block_stack_in
= (stack
) alloca (blocks
* sizeof (struct stack_def
));
500 block_out_reg_set
= (HARD_REG_SET
*) alloca (blocks
* sizeof (HARD_REG_SET
));
501 bzero ((char *) block_stack_in
, blocks
* sizeof (struct stack_def
));
502 bzero ((char *) block_out_reg_set
, blocks
* sizeof (HARD_REG_SET
));
504 block_number
= (int *) alloca ((max_uid
+ 1) * sizeof (int));
507 stack_reg_life_analysis (first
, &stackentry
);
509 /* Dump the life analysis debug information before jump
510 optimization, as that will destroy the LABEL_REFS we keep the
514 dump_stack_info (file
);
519 jump_optimize (first
, 2, 0, 0);
522 /* Check PAT, which is in INSN, for LABEL_REFs. Add INSN to the
523 label's chain of references, and note which insn contains each
527 record_label_references (insn
, pat
)
530 register enum rtx_code code
= GET_CODE (pat
);
534 if (code
== LABEL_REF
)
536 register rtx label
= XEXP (pat
, 0);
539 if (GET_CODE (label
) != CODE_LABEL
)
542 /* If this is an undefined label, LABEL_REFS (label) contains
544 if (INSN_UID (label
) == 0)
547 /* Don't make a duplicate in the code_label's chain. */
549 for (ref
= LABEL_REFS (label
);
551 ref
= LABEL_NEXTREF (ref
))
552 if (CONTAINING_INSN (ref
) == insn
)
555 CONTAINING_INSN (pat
) = insn
;
556 LABEL_NEXTREF (pat
) = LABEL_REFS (label
);
557 LABEL_REFS (label
) = pat
;
562 fmt
= GET_RTX_FORMAT (code
);
563 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
566 record_label_references (insn
, XEXP (pat
, i
));
570 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
571 record_label_references (insn
, XVECEXP (pat
, i
, j
));
576 /* Return a pointer to the REG expression within PAT. If PAT is not a
577 REG, possible enclosed by a conversion rtx, return the inner part of
578 PAT that stopped the search. */
585 switch (GET_CODE (*pat
))
588 /* eliminate FP subregister accesses in favour of the
589 actual FP register in use. */
592 if (FP_REG_P (subreg
= SUBREG_REG (*pat
)))
594 *pat
= FP_MODE_REG (REGNO (subreg
) + SUBREG_WORD (*pat
),
603 pat
= & XEXP (*pat
, 0);
607 /* Record the life info of each stack reg in INSN, updating REGSTACK.
608 N_INPUTS is the number of inputs; N_OUTPUTS the outputs.
609 OPERANDS is an array of all operands for the insn, and is assumed to
610 contain all output operands, then all inputs operands.
612 There are many rules that an asm statement for stack-like regs must
613 follow. Those rules are explained at the top of this file: the rule
614 numbers below refer to that explanation. */
617 record_asm_reg_life (insn
, regstack
)
623 int malformed_asm
= 0;
624 rtx body
= PATTERN (insn
);
626 int reg_used_as_output
[FIRST_PSEUDO_REGISTER
];
627 int implicitly_dies
[FIRST_PSEUDO_REGISTER
];
631 int n_inputs
, n_outputs
;
633 /* Find out what the constraints require. If no constraint
634 alternative matches, this asm is malformed. */
636 constrain_operands (1);
637 alt
= which_alternative
;
639 preprocess_constraints ();
641 n_inputs
= get_asm_operand_n_inputs (body
);
642 n_outputs
= recog_n_operands
- n_inputs
;
647 /* Avoid further trouble with this insn. */
648 PATTERN (insn
) = gen_rtx_USE (VOIDmode
, const0_rtx
);
649 PUT_MODE (insn
, VOIDmode
);
653 /* Strip SUBREGs here to make the following code simpler. */
654 for (i
= 0; i
< recog_n_operands
; i
++)
655 if (GET_CODE (recog_operand
[i
]) == SUBREG
656 && GET_CODE (SUBREG_REG (recog_operand
[i
])) == REG
)
657 recog_operand
[i
] = SUBREG_REG (recog_operand
[i
]);
659 /* Set up CLOBBER_REG. */
663 if (GET_CODE (body
) == PARALLEL
)
665 clobber_reg
= (rtx
*) alloca (XVECLEN (body
, 0) * sizeof (rtx
));
667 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
668 if (GET_CODE (XVECEXP (body
, 0, i
)) == CLOBBER
)
670 rtx clobber
= XVECEXP (body
, 0, i
);
671 rtx reg
= XEXP (clobber
, 0);
673 if (GET_CODE (reg
) == SUBREG
&& GET_CODE (SUBREG_REG (reg
)) == REG
)
674 reg
= SUBREG_REG (reg
);
676 if (STACK_REG_P (reg
))
678 clobber_reg
[n_clobbers
] = reg
;
684 /* Enforce rule #4: Output operands must specifically indicate which
685 reg an output appears in after an asm. "=f" is not allowed: the
686 operand constraints must select a class with a single reg.
688 Also enforce rule #5: Output operands must start at the top of
689 the reg-stack: output operands may not "skip" a reg. */
691 bzero ((char *) reg_used_as_output
, sizeof (reg_used_as_output
));
692 for (i
= 0; i
< n_outputs
; i
++)
693 if (STACK_REG_P (recog_operand
[i
]))
695 if (reg_class_size
[(int) recog_op_alt
[i
][alt
].class] != 1)
697 error_for_asm (insn
, "Output constraint %d must specify a single register", i
);
701 reg_used_as_output
[REGNO (recog_operand
[i
])] = 1;
705 /* Search for first non-popped reg. */
706 for (i
= FIRST_STACK_REG
; i
< LAST_STACK_REG
+ 1; i
++)
707 if (! reg_used_as_output
[i
])
710 /* If there are any other popped regs, that's an error. */
711 for (; i
< LAST_STACK_REG
+ 1; i
++)
712 if (reg_used_as_output
[i
])
715 if (i
!= LAST_STACK_REG
+ 1)
717 error_for_asm (insn
, "Output regs must be grouped at top of stack");
721 /* Enforce rule #2: All implicitly popped input regs must be closer
722 to the top of the reg-stack than any input that is not implicitly
725 bzero ((char *) implicitly_dies
, sizeof (implicitly_dies
));
726 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
727 if (STACK_REG_P (recog_operand
[i
]))
729 /* An input reg is implicitly popped if it is tied to an
730 output, or if there is a CLOBBER for it. */
733 for (j
= 0; j
< n_clobbers
; j
++)
734 if (operands_match_p (clobber_reg
[j
], recog_operand
[i
]))
737 if (j
< n_clobbers
|| recog_op_alt
[i
][alt
].matches
>= 0)
738 implicitly_dies
[REGNO (recog_operand
[i
])] = 1;
741 /* Search for first non-popped reg. */
742 for (i
= FIRST_STACK_REG
; i
< LAST_STACK_REG
+ 1; i
++)
743 if (! implicitly_dies
[i
])
746 /* If there are any other popped regs, that's an error. */
747 for (; i
< LAST_STACK_REG
+ 1; i
++)
748 if (implicitly_dies
[i
])
751 if (i
!= LAST_STACK_REG
+ 1)
754 "Implicitly popped regs must be grouped at top of stack");
758 /* Enfore rule #3: If any input operand uses the "f" constraint, all
759 output constraints must use the "&" earlyclobber.
761 ??? Detect this more deterministically by having constraint_asm_operands
762 record any earlyclobber. */
764 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
765 if (recog_op_alt
[i
][alt
].matches
== -1)
769 for (j
= 0; j
< n_outputs
; j
++)
770 if (operands_match_p (recog_operand
[j
], recog_operand
[i
]))
773 "Output operand %d must use `&' constraint", j
);
780 /* Avoid further trouble with this insn. */
781 PATTERN (insn
) = gen_rtx_USE (VOIDmode
, const0_rtx
);
782 PUT_MODE (insn
, VOIDmode
);
786 /* Process all outputs */
787 for (i
= 0; i
< n_outputs
; i
++)
789 rtx op
= recog_operand
[i
];
791 if (! STACK_REG_P (op
))
793 if (stack_regs_mentioned_p (op
))
799 /* Each destination is dead before this insn. If the
800 destination is not used after this insn, record this with
803 if (! TEST_HARD_REG_BIT (regstack
->reg_set
, REGNO (op
)))
804 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_UNUSED
, op
,
807 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (op
));
810 /* Process all inputs */
811 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
813 rtx op
= recog_operand
[i
];
814 if (! STACK_REG_P (op
))
816 if (stack_regs_mentioned_p (op
))
822 /* If an input is dead after the insn, record a death note.
823 But don't record a death note if there is already a death note,
824 or if the input is also an output. */
826 if (! TEST_HARD_REG_BIT (regstack
->reg_set
, REGNO (op
))
827 && recog_op_alt
[i
][alt
].matches
== -1
828 && find_regno_note (insn
, REG_DEAD
, REGNO (op
)) == NULL_RTX
)
829 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_DEAD
, op
, REG_NOTES (insn
));
831 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (op
));
835 /* Scan PAT, which is part of INSN, and record registers appearing in
836 a SET_DEST in DEST, and other registers in SRC.
838 This function does not know about SET_DESTs that are both input and
839 output (such as ZERO_EXTRACT) - this cannot happen on a 387. */
842 record_reg_life_pat (pat
, src
, dest
, douse
)
844 HARD_REG_SET
*src
, *dest
;
850 if (STACK_REG_P (pat
)
851 || (GET_CODE (pat
) == SUBREG
&& STACK_REG_P (SUBREG_REG (pat
))))
854 mark_regs_pat (pat
, src
);
857 mark_regs_pat (pat
, dest
);
862 if (GET_CODE (pat
) == SET
)
864 record_reg_life_pat (XEXP (pat
, 0), NULL_PTR
, dest
, 0);
865 record_reg_life_pat (XEXP (pat
, 1), src
, NULL_PTR
, 0);
869 /* We don't need to consider either of these cases. */
870 if ((GET_CODE (pat
) == USE
&& !douse
) || GET_CODE (pat
) == CLOBBER
)
873 fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
874 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
880 for (j
= XVECLEN (pat
, i
) - 1; j
>= 0; j
--)
881 record_reg_life_pat (XVECEXP (pat
, i
, j
), src
, dest
, 0);
883 else if (fmt
[i
] == 'e')
884 record_reg_life_pat (XEXP (pat
, i
), src
, dest
, 0);
888 /* Calculate the number of inputs and outputs in BODY, an
889 asm_operands. N_OPERANDS is the total number of operands, and
890 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
894 get_asm_operand_n_inputs (body
)
897 if (GET_CODE (body
) == SET
&& GET_CODE (SET_SRC (body
)) == ASM_OPERANDS
)
898 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body
));
900 else if (GET_CODE (body
) == ASM_OPERANDS
)
901 return ASM_OPERANDS_INPUT_LENGTH (body
);
903 else if (GET_CODE (body
) == PARALLEL
904 && GET_CODE (XVECEXP (body
, 0, 0)) == SET
)
905 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (XVECEXP (body
, 0, 0)));
907 else if (GET_CODE (body
) == PARALLEL
908 && GET_CODE (XVECEXP (body
, 0, 0)) == ASM_OPERANDS
)
909 return ASM_OPERANDS_INPUT_LENGTH (XVECEXP (body
, 0, 0));
914 /* Scan INSN, which is in BLOCK, and record the life & death of stack
915 registers in REGSTACK. This function is called to process insns from
916 the last insn in a block to the first. The actual scanning is done in
919 If a register is live after a CALL_INSN, but is not a value return
920 register for that CALL_INSN, then code is emitted to initialize that
921 register. The block_end[] data is kept accurate.
923 Existing death and unset notes for stack registers are deleted
924 before processing the insn. */
927 record_reg_life (insn
, block
, regstack
)
932 rtx note
, *note_link
;
935 if ((GET_CODE (insn
) != INSN
&& GET_CODE (insn
) != CALL_INSN
)
936 || INSN_DELETED_P (insn
))
939 /* Strip death notes for stack regs from this insn */
941 note_link
= ®_NOTES(insn
);
942 for (note
= *note_link
; note
; note
= XEXP (note
, 1))
943 if (STACK_REG_P (XEXP (note
, 0))
944 && (REG_NOTE_KIND (note
) == REG_DEAD
945 || REG_NOTE_KIND (note
) == REG_UNUSED
))
946 *note_link
= XEXP (note
, 1);
948 note_link
= &XEXP (note
, 1);
950 /* Process all patterns in the insn. */
952 n_operands
= asm_noperands (PATTERN (insn
));
955 record_asm_reg_life (insn
, regstack
);
960 HARD_REG_SET src
, dest
;
963 CLEAR_HARD_REG_SET (src
);
964 CLEAR_HARD_REG_SET (dest
);
966 if (GET_CODE (insn
) == CALL_INSN
)
967 for (note
= CALL_INSN_FUNCTION_USAGE (insn
);
969 note
= XEXP (note
, 1))
970 if (GET_CODE (XEXP (note
, 0)) == USE
)
971 record_reg_life_pat (SET_DEST (XEXP (note
, 0)), &src
, NULL_PTR
, 0);
973 record_reg_life_pat (PATTERN (insn
), &src
, &dest
, 0);
974 for (regno
= FIRST_STACK_REG
; regno
<= LAST_STACK_REG
; regno
++)
975 if (! TEST_HARD_REG_BIT (regstack
->reg_set
, regno
))
977 if (TEST_HARD_REG_BIT (src
, regno
)
978 && ! TEST_HARD_REG_BIT (dest
, regno
))
979 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_DEAD
,
980 FP_MODE_REG (regno
, DFmode
),
982 else if (TEST_HARD_REG_BIT (dest
, regno
))
983 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_UNUSED
,
984 FP_MODE_REG (regno
, DFmode
),
988 if (GET_CODE (insn
) == CALL_INSN
)
992 /* There might be a reg that is live after a function call.
993 Initialize it to zero so that the program does not crash. See
994 comment towards the end of stack_reg_life_analysis(). */
996 for (reg
= FIRST_STACK_REG
; reg
<= LAST_STACK_REG
; reg
++)
997 if (! TEST_HARD_REG_BIT (dest
, reg
)
998 && TEST_HARD_REG_BIT (regstack
->reg_set
, reg
))
1002 /* The insn will use virtual register numbers, and so
1003 convert_regs is expected to process these. But BLOCK_NUM
1004 cannot be used on these insns, because they do not appear in
1007 pat
= gen_rtx_SET (VOIDmode
, FP_MODE_REG (reg
, DFmode
),
1008 CONST0_RTX (DFmode
));
1009 init
= emit_insn_after (pat
, insn
);
1010 PUT_MODE (init
, QImode
);
1012 CLEAR_HARD_REG_BIT (regstack
->reg_set
, reg
);
1014 /* If the CALL_INSN was the end of a block, move the
1015 block_end to point to the new insn. */
1017 if (block_end
[block
] == insn
)
1018 block_end
[block
] = init
;
1021 /* Some regs do not survive a CALL */
1022 AND_COMPL_HARD_REG_SET (regstack
->reg_set
, call_used_reg_set
);
1025 AND_COMPL_HARD_REG_SET (regstack
->reg_set
, dest
);
1026 IOR_HARD_REG_SET (regstack
->reg_set
, src
);
1030 /* Find all basic blocks of the function, which starts with FIRST.
1031 For each JUMP_INSN, build the chain of LABEL_REFS on each CODE_LABEL. */
1039 register RTX_CODE prev_code
= BARRIER
;
1040 register RTX_CODE code
;
1041 rtx label_value_list
= 0;
1043 /* Record where all the blocks start and end.
1044 Record which basic blocks control can drop in to. */
1047 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
1049 /* Note that this loop must select the same block boundaries
1050 as code in reg_to_stack, but that these are not the same
1051 as those selected in flow.c. */
1053 code
= GET_CODE (insn
);
1055 if (code
== CODE_LABEL
1056 || (prev_code
!= INSN
1057 && prev_code
!= CALL_INSN
1058 && prev_code
!= CODE_LABEL
1059 && GET_RTX_CLASS (code
) == 'i'))
1061 block_begin
[++block
] = insn
;
1062 block_end
[block
] = insn
;
1063 block_drops_in
[block
] = prev_code
!= BARRIER
;
1065 else if (GET_RTX_CLASS (code
) == 'i')
1066 block_end
[block
] = insn
;
1068 if (GET_RTX_CLASS (code
) == 'i')
1072 /* Make a list of all labels referred to other than by jumps. */
1073 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
1074 if (REG_NOTE_KIND (note
) == REG_LABEL
)
1075 label_value_list
= gen_rtx_EXPR_LIST (VOIDmode
, XEXP (note
, 0),
1079 block_number
[INSN_UID (insn
)] = block
;
1085 if (block
+ 1 != blocks
)
1088 /* generate all label references to the corresponding jump insn */
1089 for (block
= 0; block
< blocks
; block
++)
1091 insn
= block_end
[block
];
1093 if (GET_CODE (insn
) == JUMP_INSN
)
1095 rtx pat
= PATTERN (insn
);
1098 if (computed_jump_p (insn
))
1100 for (x
= label_value_list
; x
; x
= XEXP (x
, 1))
1101 record_label_references (insn
,
1102 gen_rtx_LABEL_REF (VOIDmode
,
1105 for (x
= forced_labels
; x
; x
= XEXP (x
, 1))
1106 record_label_references (insn
,
1107 gen_rtx_LABEL_REF (VOIDmode
,
1111 record_label_references (insn
, pat
);
1116 /* If current function returns its result in an fp stack register,
1117 return the REG. Otherwise, return 0. */
1123 rtx result
= DECL_RTL (DECL_RESULT (decl
));
1126 && ! (GET_CODE (result
) == REG
1127 && REGNO (result
) < FIRST_PSEUDO_REGISTER
))
1129 #ifdef FUNCTION_OUTGOING_VALUE
1131 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (decl
)), decl
);
1133 result
= FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (decl
)), decl
);
1137 return result
!= 0 && STACK_REG_P (result
) ? result
: 0;
1140 /* Determine the which registers are live at the start of each basic
1141 block of the function whose first insn is FIRST.
1143 First, if the function returns a real_type, mark the function
1144 return type as live at each return point, as the RTL may not give any
1145 hint that the register is live.
1147 Then, start with the last block and work back to the first block.
1148 Similarly, work backwards within each block, insn by insn, recording
1149 which regs are dead and which are used (and therefore live) in the
1150 hard reg set of block_stack_in[].
1152 After processing each basic block, if there is a label at the start
1153 of the block, propagate the live registers to all jumps to this block.
1155 As a special case, if there are regs live in this block, that are
1156 not live in a block containing a jump to this label, and the block
1157 containing the jump has already been processed, we must propagate this
1158 block's entry register life back to the block containing the jump, and
1159 restart life analysis from there.
1161 In the worst case, this function may traverse the insns
1162 REG_STACK_SIZE times. This is necessary, since a jump towards the end
1163 of the insns may not know that a reg is live at a target that is early
1164 in the insns. So we back up and start over with the new reg live.
1166 If there are registers that are live at the start of the function,
1167 insns are emitted to initialize these registers. Something similar is
1168 done after CALL_INSNs in record_reg_life. */
1171 stack_reg_life_analysis (first
, stackentry
)
1173 HARD_REG_SET
*stackentry
;
1176 struct stack_def regstack
;
1181 if ((retvalue
= stack_result (current_function_decl
)))
1183 /* Find all RETURN insns and mark them. */
1185 for (block
= blocks
- 1; --block
>= 0;)
1186 if (GET_CODE (block_end
[block
]) == JUMP_INSN
1187 && GET_CODE (PATTERN (block_end
[block
])) == RETURN
)
1188 mark_regs_pat (retvalue
, block_out_reg_set
+block
);
1190 /* Mark off the end of last block if we "fall off" the end of the
1191 function into the epilogue. */
1193 if (GET_CODE (block_end
[blocks
-1]) != JUMP_INSN
1194 || GET_CODE (PATTERN (block_end
[blocks
-1])) == RETURN
)
1195 mark_regs_pat (retvalue
, block_out_reg_set
+blocks
-1);
1199 /* now scan all blocks backward for stack register use */
1204 register rtx insn
, prev
;
1206 /* current register status at last instruction */
1208 COPY_HARD_REG_SET (regstack
.reg_set
, block_out_reg_set
[block
]);
1210 prev
= block_end
[block
];
1214 prev
= PREV_INSN (insn
);
1216 /* If the insn is a CALL_INSN, we need to ensure that
1217 everything dies. But otherwise don't process unless there
1218 are some stack regs present. */
1220 if (GET_MODE (insn
) == QImode
|| GET_CODE (insn
) == CALL_INSN
)
1221 record_reg_life (insn
, block
, ®stack
);
1223 } while (insn
!= block_begin
[block
]);
1225 /* Set the state at the start of the block. Mark that no
1226 register mapping information known yet. */
1228 COPY_HARD_REG_SET (block_stack_in
[block
].reg_set
, regstack
.reg_set
);
1229 block_stack_in
[block
].top
= -2;
1231 /* If there is a label, propagate our register life to all jumps
1234 if (GET_CODE (insn
) == CODE_LABEL
)
1237 int must_restart
= 0;
1239 for (label
= LABEL_REFS (insn
); label
!= insn
;
1240 label
= LABEL_NEXTREF (label
))
1242 int jump_block
= BLOCK_NUM (CONTAINING_INSN (label
));
1244 if (jump_block
< block
)
1245 IOR_HARD_REG_SET (block_out_reg_set
[jump_block
],
1246 block_stack_in
[block
].reg_set
);
1249 /* The block containing the jump has already been
1250 processed. If there are registers that were not known
1251 to be live then, but are live now, we must back up
1252 and restart life analysis from that point with the new
1253 life information. */
1255 GO_IF_HARD_REG_SUBSET (block_stack_in
[block
].reg_set
,
1256 block_out_reg_set
[jump_block
],
1259 IOR_HARD_REG_SET (block_out_reg_set
[jump_block
],
1260 block_stack_in
[block
].reg_set
);
1274 if (block_drops_in
[block
])
1275 IOR_HARD_REG_SET (block_out_reg_set
[block
-1],
1276 block_stack_in
[block
].reg_set
);
1281 /* If any reg is live at the start of the first block of a
1282 function, then we must guarantee that the reg holds some value by
1283 generating our own "load" of that register. Otherwise a 387 would
1284 fault trying to access an empty register. */
1286 /* Load zero into each live register. The fact that a register
1287 appears live at the function start necessarily implies an error
1288 in the user program: it means that (unless the offending code is *never*
1289 executed) this program is using uninitialised floating point
1290 variables. In order to keep broken code like this happy, we initialise
1291 those variables with zero.
1293 Note that we are inserting virtual register references here:
1294 these insns must be processed by convert_regs later. Also, these
1295 insns will not be in block_number, so BLOCK_NUM() will fail for them. */
1297 for (reg
= LAST_STACK_REG
; reg
>= FIRST_STACK_REG
; reg
--)
1298 if (TEST_HARD_REG_BIT (block_stack_in
[0].reg_set
, reg
)
1299 && ! TEST_HARD_REG_BIT (*stackentry
, reg
))
1303 init_rtx
= gen_rtx_SET (VOIDmode
, FP_MODE_REG(reg
, DFmode
),
1304 CONST0_RTX (DFmode
));
1305 block_begin
[0] = emit_insn_after (init_rtx
, first
);
1306 PUT_MODE (block_begin
[0], QImode
);
1308 CLEAR_HARD_REG_BIT (block_stack_in
[0].reg_set
, reg
);
1312 /*****************************************************************************
1313 This section deals with stack register substitution, and forms the second
1315 *****************************************************************************/
1317 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
1318 the desired hard REGNO. */
1321 replace_reg (reg
, regno
)
1325 if (regno
< FIRST_STACK_REG
|| regno
> LAST_STACK_REG
1326 || ! STACK_REG_P (*reg
))
1329 switch (GET_MODE_CLASS (GET_MODE (*reg
)))
1333 case MODE_COMPLEX_FLOAT
:;
1336 *reg
= FP_MODE_REG (regno
, GET_MODE (*reg
));
1339 /* Remove a note of type NOTE, which must be found, for register
1340 number REGNO from INSN. Remove only one such note. */
1343 remove_regno_note (insn
, note
, regno
)
1348 register rtx
*note_link
, this;
1350 note_link
= ®_NOTES(insn
);
1351 for (this = *note_link
; this; this = XEXP (this, 1))
1352 if (REG_NOTE_KIND (this) == note
1353 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno
)
1355 *note_link
= XEXP (this, 1);
1359 note_link
= &XEXP (this, 1);
1364 /* Find the hard register number of virtual register REG in REGSTACK.
1365 The hard register number is relative to the top of the stack. -1 is
1366 returned if the register is not found. */
1369 get_hard_regnum (regstack
, reg
)
1375 if (! STACK_REG_P (reg
))
1378 for (i
= regstack
->top
; i
>= 0; i
--)
1379 if (regstack
->reg
[i
] == REGNO (reg
))
1382 return i
>= 0 ? (FIRST_STACK_REG
+ regstack
->top
- i
) : -1;
1385 /* Delete INSN from the RTL. Mark the insn, but don't remove it from
1386 the chain of insns. Doing so could confuse block_begin and block_end
1387 if this were the only insn in the block. */
1390 delete_insn_for_stacker (insn
)
1393 PUT_CODE (insn
, NOTE
);
1394 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
1395 NOTE_SOURCE_FILE (insn
) = 0;
1398 /* Emit an insn to pop virtual register REG before or after INSN.
1399 REGSTACK is the stack state after INSN and is updated to reflect this
1400 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
1401 is represented as a SET whose destination is the register to be popped
1402 and source is the top of stack. A death note for the top of stack
1403 cases the movdf pattern to pop. */
1406 emit_pop_insn (insn
, regstack
, reg
, when
)
1412 rtx pop_insn
, pop_rtx
;
1415 hard_regno
= get_hard_regnum (regstack
, reg
);
1417 if (hard_regno
< FIRST_STACK_REG
)
1420 pop_rtx
= gen_rtx_SET (VOIDmode
, FP_MODE_REG (hard_regno
, DFmode
),
1421 FP_MODE_REG (FIRST_STACK_REG
, DFmode
));
1423 pop_insn
= (*when
) (pop_rtx
, insn
);
1424 /* ??? This used to be VOIDmode, but that seems wrong. */
1425 PUT_MODE (pop_insn
, QImode
);
1427 REG_NOTES (pop_insn
) = gen_rtx_EXPR_LIST (REG_DEAD
,
1428 FP_MODE_REG (FIRST_STACK_REG
, DFmode
),
1429 REG_NOTES (pop_insn
));
1431 regstack
->reg
[regstack
->top
- (hard_regno
- FIRST_STACK_REG
)]
1432 = regstack
->reg
[regstack
->top
];
1434 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (reg
));
1439 /* Emit an insn before or after INSN to swap virtual register REG with the
1440 top of stack. WHEN should be `emit_insn_before' or `emit_insn_before'
1441 REGSTACK is the stack state before the swap, and is updated to reflect
1442 the swap. A swap insn is represented as a PARALLEL of two patterns:
1443 each pattern moves one reg to the other.
1445 If REG is already at the top of the stack, no insn is emitted. */
1448 emit_swap_insn (insn
, regstack
, reg
)
1455 rtx swap_rtx
, swap_insn
;
1456 int tmp
, other_reg
; /* swap regno temps */
1457 rtx i1
; /* the stack-reg insn prior to INSN */
1458 rtx i1set
= NULL_RTX
; /* the SET rtx within I1 */
1460 hard_regno
= get_hard_regnum (regstack
, reg
);
1462 if (hard_regno
< FIRST_STACK_REG
)
1464 if (hard_regno
== FIRST_STACK_REG
)
1467 other_reg
= regstack
->top
- (hard_regno
- FIRST_STACK_REG
);
1469 tmp
= regstack
->reg
[other_reg
];
1470 regstack
->reg
[other_reg
] = regstack
->reg
[regstack
->top
];
1471 regstack
->reg
[regstack
->top
] = tmp
;
1473 /* Find the previous insn involving stack regs, but don't go past
1474 any labels, calls or jumps. */
1475 i1
= prev_nonnote_insn (insn
);
1476 while (i1
&& GET_CODE (i1
) == INSN
&& GET_MODE (i1
) != QImode
)
1477 i1
= prev_nonnote_insn (i1
);
1480 i1set
= single_set (i1
);
1484 rtx i1src
= *get_true_reg (&SET_SRC (i1set
));
1485 rtx i1dest
= *get_true_reg (&SET_DEST (i1set
));
1487 /* If the previous register stack push was from the reg we are to
1488 swap with, omit the swap. */
1490 if (GET_CODE (i1dest
) == REG
&& REGNO (i1dest
) == FIRST_STACK_REG
1491 && GET_CODE (i1src
) == REG
&& REGNO (i1src
) == hard_regno
- 1
1492 && find_regno_note (i1
, REG_DEAD
, FIRST_STACK_REG
) == NULL_RTX
)
1495 /* If the previous insn wrote to the reg we are to swap with,
1498 if (GET_CODE (i1dest
) == REG
&& REGNO (i1dest
) == hard_regno
1499 && GET_CODE (i1src
) == REG
&& REGNO (i1src
) == FIRST_STACK_REG
1500 && find_regno_note (i1
, REG_DEAD
, FIRST_STACK_REG
) == NULL_RTX
)
1504 if (GET_RTX_CLASS (GET_CODE (i1
)) == 'i' && sets_cc0_p (PATTERN (i1
)))
1506 i1
= next_nonnote_insn (i1
);
1511 swap_rtx
= gen_swapdf (FP_MODE_REG (hard_regno
, DFmode
),
1512 FP_MODE_REG (FIRST_STACK_REG
, DFmode
));
1513 swap_insn
= emit_insn_after (swap_rtx
, i1
);
1514 /* ??? This used to be VOIDmode, but that seems wrong. */
1515 PUT_MODE (swap_insn
, QImode
);
1518 /* Handle a move to or from a stack register in PAT, which is in INSN.
1519 REGSTACK is the current stack. */
1522 move_for_stack_reg (insn
, regstack
, pat
)
1527 rtx
*psrc
= get_true_reg (&SET_SRC (pat
));
1528 rtx
*pdest
= get_true_reg (&SET_DEST (pat
));
1532 src
= *psrc
; dest
= *pdest
;
1534 if (STACK_REG_P (src
) && STACK_REG_P (dest
))
1536 /* Write from one stack reg to another. If SRC dies here, then
1537 just change the register mapping and delete the insn. */
1539 note
= find_regno_note (insn
, REG_DEAD
, REGNO (src
));
1544 /* If this is a no-op move, there must not be a REG_DEAD note. */
1545 if (REGNO (src
) == REGNO (dest
))
1548 for (i
= regstack
->top
; i
>= 0; i
--)
1549 if (regstack
->reg
[i
] == REGNO (src
))
1552 /* The source must be live, and the dest must be dead. */
1553 if (i
< 0 || get_hard_regnum (regstack
, dest
) >= FIRST_STACK_REG
)
1556 /* It is possible that the dest is unused after this insn.
1557 If so, just pop the src. */
1559 if (find_regno_note (insn
, REG_UNUSED
, REGNO (dest
)))
1561 emit_pop_insn (insn
, regstack
, src
, emit_insn_after
);
1563 delete_insn_for_stacker (insn
);
1567 regstack
->reg
[i
] = REGNO (dest
);
1569 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (dest
));
1570 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (src
));
1572 delete_insn_for_stacker (insn
);
1577 /* The source reg does not die. */
1579 /* If this appears to be a no-op move, delete it, or else it
1580 will confuse the machine description output patterns. But if
1581 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1582 for REG_UNUSED will not work for deleted insns. */
1584 if (REGNO (src
) == REGNO (dest
))
1586 if (find_regno_note (insn
, REG_UNUSED
, REGNO (dest
)))
1587 emit_pop_insn (insn
, regstack
, dest
, emit_insn_after
);
1589 delete_insn_for_stacker (insn
);
1593 /* The destination ought to be dead */
1594 if (get_hard_regnum (regstack
, dest
) >= FIRST_STACK_REG
)
1597 replace_reg (psrc
, get_hard_regnum (regstack
, src
));
1599 regstack
->reg
[++regstack
->top
] = REGNO (dest
);
1600 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (dest
));
1601 replace_reg (pdest
, FIRST_STACK_REG
);
1603 else if (STACK_REG_P (src
))
1605 /* Save from a stack reg to MEM, or possibly integer reg. Since
1606 only top of stack may be saved, emit an exchange first if
1609 emit_swap_insn (insn
, regstack
, src
);
1611 note
= find_regno_note (insn
, REG_DEAD
, REGNO (src
));
1614 replace_reg (&XEXP (note
, 0), FIRST_STACK_REG
);
1616 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (src
));
1618 else if (GET_MODE (src
) == XFmode
&& regstack
->top
< REG_STACK_SIZE
- 1)
1620 /* A 387 cannot write an XFmode value to a MEM without
1621 clobbering the source reg. The output code can handle
1622 this by reading back the value from the MEM.
1623 But it is more efficient to use a temp register if one is
1624 available. Push the source value here if the register
1625 stack is not full, and then write the value to memory via
1627 rtx push_rtx
, push_insn
;
1628 rtx top_stack_reg
= FP_MODE_REG (FIRST_STACK_REG
, XFmode
);
1630 push_rtx
= gen_movxf (top_stack_reg
, top_stack_reg
);
1631 push_insn
= emit_insn_before (push_rtx
, insn
);
1632 PUT_MODE (push_insn
, QImode
);
1633 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_DEAD
, top_stack_reg
,
1637 replace_reg (psrc
, FIRST_STACK_REG
);
1639 else if (STACK_REG_P (dest
))
1641 /* Load from MEM, or possibly integer REG or constant, into the
1642 stack regs. The actual target is always the top of the
1643 stack. The stack mapping is changed to reflect that DEST is
1644 now at top of stack. */
1646 /* The destination ought to be dead */
1647 if (get_hard_regnum (regstack
, dest
) >= FIRST_STACK_REG
)
1650 if (regstack
->top
>= REG_STACK_SIZE
)
1653 regstack
->reg
[++regstack
->top
] = REGNO (dest
);
1654 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (dest
));
1655 replace_reg (pdest
, FIRST_STACK_REG
);
1662 swap_rtx_condition (pat
)
1668 if (GET_RTX_CLASS (GET_CODE (pat
)) == '<')
1670 PUT_CODE (pat
, swap_condition (GET_CODE (pat
)));
1674 fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
1675 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
1681 for (j
= XVECLEN (pat
, i
) - 1; j
>= 0; j
--)
1682 swap_rtx_condition (XVECEXP (pat
, i
, j
));
1684 else if (fmt
[i
] == 'e')
1685 swap_rtx_condition (XEXP (pat
, i
));
1689 /* Handle a comparison. Special care needs to be taken to avoid
1690 causing comparisons that a 387 cannot do correctly, such as EQ.
1692 Also, a pop insn may need to be emitted. The 387 does have an
1693 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1694 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1698 compare_for_stack_reg (insn
, regstack
, pat
)
1704 rtx src1_note
, src2_note
;
1708 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 0));
1709 src2
= get_true_reg (&XEXP (SET_SRC (pat
), 1));
1710 cc0_user
= next_cc0_user (insn
);
1712 /* If the insn that uses cc0 is an FP-conditional move, then the destination
1713 must be the top of stack */
1714 if (GET_CODE (PATTERN (cc0_user
)) == SET
1715 && SET_DEST (PATTERN (cc0_user
)) != pc_rtx
1716 && GET_CODE (SET_SRC (PATTERN (cc0_user
))) == IF_THEN_ELSE
1717 && (GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (cc0_user
))))
1722 dest
= get_true_reg (&SET_DEST (PATTERN (cc0_user
)));
1725 if (get_hard_regnum (regstack
, *dest
) >= FIRST_STACK_REG
1726 && REGNO (*dest
) != regstack
->reg
[regstack
->top
])
1728 emit_swap_insn (insn
, regstack
, *dest
);
1734 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1735 registers that die in this insn - move those to stack top first. */
1736 if (! STACK_REG_P (*src1
)
1737 || (STACK_REG_P (*src2
)
1738 && get_hard_regnum (regstack
, *src2
) == FIRST_STACK_REG
))
1742 temp
= XEXP (SET_SRC (pat
), 0);
1743 XEXP (SET_SRC (pat
), 0) = XEXP (SET_SRC (pat
), 1);
1744 XEXP (SET_SRC (pat
), 1) = temp
;
1746 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 0));
1747 src2
= get_true_reg (&XEXP (SET_SRC (pat
), 1));
1749 next
= next_cc0_user (insn
);
1750 if (next
== NULL_RTX
)
1753 swap_rtx_condition (PATTERN (next
));
1754 INSN_CODE (next
) = -1;
1755 INSN_CODE (insn
) = -1;
1758 /* We will fix any death note later. */
1760 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1762 if (STACK_REG_P (*src2
))
1763 src2_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src2
));
1765 src2_note
= NULL_RTX
;
1768 emit_swap_insn (insn
, regstack
, *src1
);
1770 replace_reg (src1
, FIRST_STACK_REG
);
1772 if (STACK_REG_P (*src2
))
1773 replace_reg (src2
, get_hard_regnum (regstack
, *src2
));
1777 pop_stack (regstack
, REGNO (XEXP (src1_note
, 0)));
1778 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
1781 /* If the second operand dies, handle that. But if the operands are
1782 the same stack register, don't bother, because only one death is
1783 needed, and it was just handled. */
1786 && ! (STACK_REG_P (*src1
) && STACK_REG_P (*src2
)
1787 && REGNO (*src1
) == REGNO (*src2
)))
1789 /* As a special case, two regs may die in this insn if src2 is
1790 next to top of stack and the top of stack also dies. Since
1791 we have already popped src1, "next to top of stack" is really
1792 at top (FIRST_STACK_REG) now. */
1794 if (get_hard_regnum (regstack
, XEXP (src2_note
, 0)) == FIRST_STACK_REG
1797 pop_stack (regstack
, REGNO (XEXP (src2_note
, 0)));
1798 replace_reg (&XEXP (src2_note
, 0), FIRST_STACK_REG
+ 1);
1802 /* The 386 can only represent death of the first operand in
1803 the case handled above. In all other cases, emit a separate
1804 pop and remove the death note from here. */
1806 link_cc0_insns (insn
);
1808 remove_regno_note (insn
, REG_DEAD
, REGNO (XEXP (src2_note
, 0)));
1810 emit_pop_insn (insn
, regstack
, XEXP (src2_note
, 0),
1816 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1817 is the current register layout. */
1820 subst_stack_regs_pat (insn
, regstack
, pat
)
1826 rtx
*src1
= (rtx
*) NULL_PTR
, *src2
;
1827 rtx src1_note
, src2_note
;
1829 if (GET_CODE (pat
) != SET
)
1832 dest
= get_true_reg (&SET_DEST (pat
));
1833 src
= get_true_reg (&SET_SRC (pat
));
1835 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1837 if (*dest
!= cc0_rtx
1838 && (STACK_REG_P (*src
)
1839 || (STACK_REG_P (*dest
)
1840 && (GET_CODE (*src
) == REG
|| GET_CODE (*src
) == MEM
1841 || GET_CODE (*src
) == CONST_DOUBLE
))))
1842 move_for_stack_reg (insn
, regstack
, pat
);
1844 switch (GET_CODE (SET_SRC (pat
)))
1847 compare_for_stack_reg (insn
, regstack
, pat
);
1853 for (count
= HARD_REGNO_NREGS (REGNO (*dest
), GET_MODE (*dest
));
1856 regstack
->reg
[++regstack
->top
] = REGNO (*dest
) + count
;
1857 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
) + count
);
1860 replace_reg (dest
, FIRST_STACK_REG
);
1864 /* This is a `tstM2' case. */
1865 if (*dest
!= cc0_rtx
)
1872 case FLOAT_TRUNCATE
:
1876 /* These insns only operate on the top of the stack. DEST might
1877 be cc0_rtx if we're processing a tstM pattern. Also, it's
1878 possible that the tstM case results in a REG_DEAD note on the
1882 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 0));
1884 emit_swap_insn (insn
, regstack
, *src1
);
1886 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1888 if (STACK_REG_P (*dest
))
1889 replace_reg (dest
, FIRST_STACK_REG
);
1893 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
1895 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (*src1
));
1898 replace_reg (src1
, FIRST_STACK_REG
);
1904 /* On i386, reversed forms of subM3 and divM3 exist for
1905 MODE_FLOAT, so the same code that works for addM3 and mulM3
1909 /* These insns can accept the top of stack as a destination
1910 from a stack reg or mem, or can use the top of stack as a
1911 source and some other stack register (possibly top of stack)
1912 as a destination. */
1914 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 0));
1915 src2
= get_true_reg (&XEXP (SET_SRC (pat
), 1));
1917 /* We will fix any death note later. */
1919 if (STACK_REG_P (*src1
))
1920 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1922 src1_note
= NULL_RTX
;
1923 if (STACK_REG_P (*src2
))
1924 src2_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src2
));
1926 src2_note
= NULL_RTX
;
1928 /* If either operand is not a stack register, then the dest
1929 must be top of stack. */
1931 if (! STACK_REG_P (*src1
) || ! STACK_REG_P (*src2
))
1932 emit_swap_insn (insn
, regstack
, *dest
);
1935 /* Both operands are REG. If neither operand is already
1936 at the top of stack, choose to make the one that is the dest
1937 the new top of stack. */
1939 int src1_hard_regnum
, src2_hard_regnum
;
1941 src1_hard_regnum
= get_hard_regnum (regstack
, *src1
);
1942 src2_hard_regnum
= get_hard_regnum (regstack
, *src2
);
1943 if (src1_hard_regnum
== -1 || src2_hard_regnum
== -1)
1946 if (src1_hard_regnum
!= FIRST_STACK_REG
1947 && src2_hard_regnum
!= FIRST_STACK_REG
)
1948 emit_swap_insn (insn
, regstack
, *dest
);
1951 if (STACK_REG_P (*src1
))
1952 replace_reg (src1
, get_hard_regnum (regstack
, *src1
));
1953 if (STACK_REG_P (*src2
))
1954 replace_reg (src2
, get_hard_regnum (regstack
, *src2
));
1958 /* If the register that dies is at the top of stack, then
1959 the destination is somewhere else - merely substitute it.
1960 But if the reg that dies is not at top of stack, then
1961 move the top of stack to the dead reg, as though we had
1962 done the insn and then a store-with-pop. */
1964 if (REGNO (XEXP (src1_note
, 0)) == regstack
->reg
[regstack
->top
])
1966 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1967 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
1971 int regno
= get_hard_regnum (regstack
, XEXP (src1_note
, 0));
1973 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1974 replace_reg (dest
, regno
);
1976 regstack
->reg
[regstack
->top
- (regno
- FIRST_STACK_REG
)]
1977 = regstack
->reg
[regstack
->top
];
1980 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
1981 REGNO (XEXP (src1_note
, 0)));
1982 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
1987 if (REGNO (XEXP (src2_note
, 0)) == regstack
->reg
[regstack
->top
])
1989 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1990 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
1994 int regno
= get_hard_regnum (regstack
, XEXP (src2_note
, 0));
1996 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
1997 replace_reg (dest
, regno
);
1999 regstack
->reg
[regstack
->top
- (regno
- FIRST_STACK_REG
)]
2000 = regstack
->reg
[regstack
->top
];
2003 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
2004 REGNO (XEXP (src2_note
, 0)));
2005 replace_reg (&XEXP (src2_note
, 0), FIRST_STACK_REG
);
2010 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
2011 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
2017 switch (XINT (SET_SRC (pat
), 1))
2021 /* These insns only operate on the top of the stack. */
2023 src1
= get_true_reg (&XVECEXP (SET_SRC (pat
), 0, 0));
2025 emit_swap_insn (insn
, regstack
, *src1
);
2027 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
2029 if (STACK_REG_P (*dest
))
2030 replace_reg (dest
, FIRST_STACK_REG
);
2034 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
2036 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (*src1
));
2039 replace_reg (src1
, FIRST_STACK_REG
);
2049 /* This insn requires the top of stack to be the destination. */
2051 /* If the comparison operator is an FP comparison operator,
2052 it is handled correctly by compare_for_stack_reg () who
2053 will move the destination to the top of stack. But if the
2054 comparison operator is not an FP comparison operator, we
2055 have to handle it here. */
2056 if (get_hard_regnum (regstack
, *dest
) >= FIRST_STACK_REG
2057 && REGNO (*dest
) != regstack
->reg
[regstack
->top
])
2058 emit_swap_insn (insn
, regstack
, *dest
);
2060 src1
= get_true_reg (&XEXP (SET_SRC (pat
), 1));
2061 src2
= get_true_reg (&XEXP (SET_SRC (pat
), 2));
2063 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
2064 src2_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src2
));
2071 src_note
[1] = src1_note
;
2072 src_note
[2] = src2_note
;
2074 if (STACK_REG_P (*src1
))
2075 replace_reg (src1
, get_hard_regnum (regstack
, *src1
));
2076 if (STACK_REG_P (*src2
))
2077 replace_reg (src2
, get_hard_regnum (regstack
, *src2
));
2079 for (i
= 1; i
<= 2; i
++)
2082 /* If the register that dies is not at the top of stack, then
2083 move the top of stack to the dead reg */
2084 if (REGNO (XEXP (src_note
[i
], 0))
2085 != regstack
->reg
[regstack
->top
])
2087 remove_regno_note (insn
, REG_DEAD
,
2088 REGNO (XEXP (src_note
[i
], 0)));
2089 emit_pop_insn (insn
, regstack
, XEXP (src_note
[i
], 0),
2094 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
2095 REGNO (XEXP (src_note
[i
], 0)));
2096 replace_reg (&XEXP (src_note
[i
], 0), FIRST_STACK_REG
);
2102 /* Make dest the top of stack. Add dest to regstack if not present. */
2103 if (get_hard_regnum (regstack
, *dest
) < FIRST_STACK_REG
)
2104 regstack
->reg
[++regstack
->top
] = REGNO (*dest
);
2105 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
2106 replace_reg (dest
, FIRST_STACK_REG
);
2115 /* Substitute hard regnums for any stack regs in INSN, which has
2116 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
2117 before the insn, and is updated with changes made here.
2119 There are several requirements and assumptions about the use of
2120 stack-like regs in asm statements. These rules are enforced by
2121 record_asm_stack_regs; see comments there for details. Any
2122 asm_operands left in the RTL at this point may be assume to meet the
2123 requirements, since record_asm_stack_regs removes any problem asm. */
2126 subst_asm_stack_regs (insn
, regstack
)
2130 rtx body
= PATTERN (insn
);
2133 rtx
*note_reg
; /* Array of note contents */
2134 rtx
**note_loc
; /* Address of REG field of each note */
2135 enum reg_note
*note_kind
; /* The type of each note */
2140 struct stack_def temp_stack
;
2145 int n_inputs
, n_outputs
;
2147 /* Find out what the constraints required. If no constraint
2148 alternative matches, that is a compiler bug: we should have caught
2149 such an insn during the life analysis pass (and reload should have
2150 caught it regardless). */
2151 extract_insn (insn
);
2152 constrain_operands (1);
2153 alt
= which_alternative
;
2155 preprocess_constraints ();
2157 n_inputs
= get_asm_operand_n_inputs (body
);
2158 n_outputs
= recog_n_operands
- n_inputs
;
2163 /* Strip SUBREGs here to make the following code simpler. */
2164 for (i
= 0; i
< recog_n_operands
; i
++)
2165 if (GET_CODE (recog_operand
[i
]) == SUBREG
2166 && GET_CODE (SUBREG_REG (recog_operand
[i
])) == REG
)
2168 recog_operand_loc
[i
] = & SUBREG_REG (recog_operand
[i
]);
2169 recog_operand
[i
] = SUBREG_REG (recog_operand
[i
]);
2172 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
2174 for (i
= 0, note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
2177 note_reg
= (rtx
*) alloca (i
* sizeof (rtx
));
2178 note_loc
= (rtx
**) alloca (i
* sizeof (rtx
*));
2179 note_kind
= (enum reg_note
*) alloca (i
* sizeof (enum reg_note
));
2182 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
2184 rtx reg
= XEXP (note
, 0);
2185 rtx
*loc
= & XEXP (note
, 0);
2187 if (GET_CODE (reg
) == SUBREG
&& GET_CODE (SUBREG_REG (reg
)) == REG
)
2189 loc
= & SUBREG_REG (reg
);
2190 reg
= SUBREG_REG (reg
);
2193 if (STACK_REG_P (reg
)
2194 && (REG_NOTE_KIND (note
) == REG_DEAD
2195 || REG_NOTE_KIND (note
) == REG_UNUSED
))
2197 note_reg
[n_notes
] = reg
;
2198 note_loc
[n_notes
] = loc
;
2199 note_kind
[n_notes
] = REG_NOTE_KIND (note
);
2204 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2208 if (GET_CODE (body
) == PARALLEL
)
2210 clobber_reg
= (rtx
*) alloca (XVECLEN (body
, 0) * sizeof (rtx
));
2211 clobber_loc
= (rtx
**) alloca (XVECLEN (body
, 0) * sizeof (rtx
*));
2213 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2214 if (GET_CODE (XVECEXP (body
, 0, i
)) == CLOBBER
)
2216 rtx clobber
= XVECEXP (body
, 0, i
);
2217 rtx reg
= XEXP (clobber
, 0);
2218 rtx
*loc
= & XEXP (clobber
, 0);
2220 if (GET_CODE (reg
) == SUBREG
&& GET_CODE (SUBREG_REG (reg
)) == REG
)
2222 loc
= & SUBREG_REG (reg
);
2223 reg
= SUBREG_REG (reg
);
2226 if (STACK_REG_P (reg
))
2228 clobber_reg
[n_clobbers
] = reg
;
2229 clobber_loc
[n_clobbers
] = loc
;
2235 bcopy ((char *) regstack
, (char *) &temp_stack
, sizeof (temp_stack
));
2237 /* Put the input regs into the desired place in TEMP_STACK. */
2239 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
2240 if (STACK_REG_P (recog_operand
[i
])
2241 && reg_class_subset_p (recog_op_alt
[i
][alt
].class,
2243 && recog_op_alt
[i
][alt
].class != FLOAT_REGS
)
2245 /* If an operand needs to be in a particular reg in
2246 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2247 these constraints are for single register classes, and reload
2248 guaranteed that operand[i] is already in that class, we can
2249 just use REGNO (recog_operand[i]) to know which actual reg this
2250 operand needs to be in. */
2252 int regno
= get_hard_regnum (&temp_stack
, recog_operand
[i
]);
2257 if (regno
!= REGNO (recog_operand
[i
]))
2259 /* recog_operand[i] is not in the right place. Find it
2260 and swap it with whatever is already in I's place.
2261 K is where recog_operand[i] is now. J is where it should
2265 k
= temp_stack
.top
- (regno
- FIRST_STACK_REG
);
2267 - (REGNO (recog_operand
[i
]) - FIRST_STACK_REG
));
2269 temp
= temp_stack
.reg
[k
];
2270 temp_stack
.reg
[k
] = temp_stack
.reg
[j
];
2271 temp_stack
.reg
[j
] = temp
;
2275 /* emit insns before INSN to make sure the reg-stack is in the right
2278 change_stack (insn
, regstack
, &temp_stack
, emit_insn_before
);
2280 /* Make the needed input register substitutions. Do death notes and
2281 clobbers too, because these are for inputs, not outputs. */
2283 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
2284 if (STACK_REG_P (recog_operand
[i
]))
2286 int regnum
= get_hard_regnum (regstack
, recog_operand
[i
]);
2291 replace_reg (recog_operand_loc
[i
], regnum
);
2294 for (i
= 0; i
< n_notes
; i
++)
2295 if (note_kind
[i
] == REG_DEAD
)
2297 int regnum
= get_hard_regnum (regstack
, note_reg
[i
]);
2302 replace_reg (note_loc
[i
], regnum
);
2305 for (i
= 0; i
< n_clobbers
; i
++)
2307 /* It's OK for a CLOBBER to reference a reg that is not live.
2308 Don't try to replace it in that case. */
2309 int regnum
= get_hard_regnum (regstack
, clobber_reg
[i
]);
2313 /* Sigh - clobbers always have QImode. But replace_reg knows
2314 that these regs can't be MODE_INT and will abort. Just put
2315 the right reg there without calling replace_reg. */
2317 *clobber_loc
[i
] = FP_MODE_REG (regnum
, DFmode
);
2321 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2323 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
2324 if (STACK_REG_P (recog_operand
[i
]))
2326 /* An input reg is implicitly popped if it is tied to an
2327 output, or if there is a CLOBBER for it. */
2330 for (j
= 0; j
< n_clobbers
; j
++)
2331 if (operands_match_p (clobber_reg
[j
], recog_operand
[i
]))
2334 if (j
< n_clobbers
|| recog_op_alt
[i
][alt
].matches
>= 0)
2336 /* recog_operand[i] might not be at the top of stack. But that's
2337 OK, because all we need to do is pop the right number of regs
2338 off of the top of the reg-stack. record_asm_stack_regs
2339 guaranteed that all implicitly popped regs were grouped
2340 at the top of the reg-stack. */
2342 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
2343 regstack
->reg
[regstack
->top
]);
2348 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2349 Note that there isn't any need to substitute register numbers.
2350 ??? Explain why this is true. */
2352 for (i
= LAST_STACK_REG
; i
>= FIRST_STACK_REG
; i
--)
2354 /* See if there is an output for this hard reg. */
2357 for (j
= 0; j
< n_outputs
; j
++)
2358 if (STACK_REG_P (recog_operand
[j
]) && REGNO (recog_operand
[j
]) == i
)
2360 regstack
->reg
[++regstack
->top
] = i
;
2361 SET_HARD_REG_BIT (regstack
->reg_set
, i
);
2366 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2367 input that the asm didn't implicitly pop. If the asm didn't
2368 implicitly pop an input reg, that reg will still be live.
2370 Note that we can't use find_regno_note here: the register numbers
2371 in the death notes have already been substituted. */
2373 for (i
= 0; i
< n_outputs
; i
++)
2374 if (STACK_REG_P (recog_operand
[i
]))
2378 for (j
= 0; j
< n_notes
; j
++)
2379 if (REGNO (recog_operand
[i
]) == REGNO (note_reg
[j
])
2380 && note_kind
[j
] == REG_UNUSED
)
2382 insn
= emit_pop_insn (insn
, regstack
, recog_operand
[i
],
2388 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
2389 if (STACK_REG_P (recog_operand
[i
]))
2393 for (j
= 0; j
< n_notes
; j
++)
2394 if (REGNO (recog_operand
[i
]) == REGNO (note_reg
[j
])
2395 && note_kind
[j
] == REG_DEAD
2396 && TEST_HARD_REG_BIT (regstack
->reg_set
,
2397 REGNO (recog_operand
[i
])))
2399 insn
= emit_pop_insn (insn
, regstack
, recog_operand
[i
],
2406 /* Substitute stack hard reg numbers for stack virtual registers in
2407 INSN. Non-stack register numbers are not changed. REGSTACK is the
2408 current stack content. Insns may be emitted as needed to arrange the
2409 stack for the 387 based on the contents of the insn. */
2412 subst_stack_regs (insn
, regstack
)
2416 register rtx
*note_link
, note
;
2419 if (GET_CODE (insn
) == CALL_INSN
)
2421 int top
= regstack
->top
;
2423 /* If there are any floating point parameters to be passed in
2424 registers for this call, make sure they are in the right
2429 straighten_stack (PREV_INSN (insn
), regstack
);
2431 /* Now mark the arguments as dead after the call. */
2433 while (regstack
->top
>= 0)
2435 CLEAR_HARD_REG_BIT (regstack
->reg_set
, FIRST_STACK_REG
+ regstack
->top
);
2441 /* Do the actual substitution if any stack regs are mentioned.
2442 Since we only record whether entire insn mentions stack regs, and
2443 subst_stack_regs_pat only works for patterns that contain stack regs,
2444 we must check each pattern in a parallel here. A call_value_pop could
2447 if (GET_MODE (insn
) == QImode
)
2449 int n_operands
= asm_noperands (PATTERN (insn
));
2450 if (n_operands
>= 0)
2452 /* This insn is an `asm' with operands. Decode the operands,
2453 decide how many are inputs, and do register substitution.
2454 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2456 subst_asm_stack_regs (insn
, regstack
);
2460 if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
2461 for (i
= 0; i
< XVECLEN (PATTERN (insn
), 0); i
++)
2463 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn
), 0, i
)))
2464 subst_stack_regs_pat (insn
, regstack
,
2465 XVECEXP (PATTERN (insn
), 0, i
));
2468 subst_stack_regs_pat (insn
, regstack
, PATTERN (insn
));
2471 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2472 REG_UNUSED will already have been dealt with, so just return. */
2474 if (GET_CODE (insn
) == NOTE
)
2477 /* If there is a REG_UNUSED note on a stack register on this insn,
2478 the indicated reg must be popped. The REG_UNUSED note is removed,
2479 since the form of the newly emitted pop insn references the reg,
2480 making it no longer `unset'. */
2482 note_link
= ®_NOTES(insn
);
2483 for (note
= *note_link
; note
; note
= XEXP (note
, 1))
2484 if (REG_NOTE_KIND (note
) == REG_UNUSED
&& STACK_REG_P (XEXP (note
, 0)))
2486 *note_link
= XEXP (note
, 1);
2487 insn
= emit_pop_insn (insn
, regstack
, XEXP (note
, 0), emit_insn_after
);
2490 note_link
= &XEXP (note
, 1);
2493 /* Change the organization of the stack so that it fits a new basic
2494 block. Some registers might have to be popped, but there can never be
2495 a register live in the new block that is not now live.
2497 Insert any needed insns before or after INSN. WHEN is emit_insn_before
2498 or emit_insn_after. OLD is the original stack layout, and NEW is
2499 the desired form. OLD is updated to reflect the code emitted, ie, it
2500 will be the same as NEW upon return.
2502 This function will not preserve block_end[]. But that information
2503 is no longer needed once this has executed. */
2506 change_stack (insn
, old
, new, when
)
2514 /* We will be inserting new insns "backwards", by calling emit_insn_before.
2515 If we are to insert after INSN, find the next insn, and insert before
2518 if (when
== emit_insn_after
)
2519 insn
= NEXT_INSN (insn
);
2521 /* Pop any registers that are not needed in the new block. */
2523 for (reg
= old
->top
; reg
>= 0; reg
--)
2524 if (! TEST_HARD_REG_BIT (new->reg_set
, old
->reg
[reg
]))
2525 emit_pop_insn (insn
, old
, FP_MODE_REG (old
->reg
[reg
], DFmode
),
2530 /* If the new block has never been processed, then it can inherit
2531 the old stack order. */
2533 new->top
= old
->top
;
2534 bcopy (old
->reg
, new->reg
, sizeof (new->reg
));
2538 /* This block has been entered before, and we must match the
2539 previously selected stack order. */
2541 /* By now, the only difference should be the order of the stack,
2542 not their depth or liveliness. */
2544 GO_IF_HARD_REG_EQUAL (old
->reg_set
, new->reg_set
, win
);
2550 if (old
->top
!= new->top
)
2553 /* Loop here emitting swaps until the stack is correct. The
2554 worst case number of swaps emitted is N + 2, where N is the
2555 depth of the stack. In some cases, the reg at the top of
2556 stack may be correct, but swapped anyway in order to fix
2557 other regs. But since we never swap any other reg away from
2558 its correct slot, this algorithm will converge. */
2562 /* Swap the reg at top of stack into the position it is
2563 supposed to be in, until the correct top of stack appears. */
2565 while (old
->reg
[old
->top
] != new->reg
[new->top
])
2567 for (reg
= new->top
; reg
>= 0; reg
--)
2568 if (new->reg
[reg
] == old
->reg
[old
->top
])
2574 emit_swap_insn (insn
, old
,
2575 FP_MODE_REG (old
->reg
[reg
], DFmode
));
2578 /* See if any regs remain incorrect. If so, bring an
2579 incorrect reg to the top of stack, and let the while loop
2582 for (reg
= new->top
; reg
>= 0; reg
--)
2583 if (new->reg
[reg
] != old
->reg
[reg
])
2585 emit_swap_insn (insn
, old
,
2586 FP_MODE_REG (old
->reg
[reg
], DFmode
));
2591 /* At this point there must be no differences. */
2593 for (reg
= old
->top
; reg
>= 0; reg
--)
2594 if (old
->reg
[reg
] != new->reg
[reg
])
2599 /* Check PAT, which points to RTL in INSN, for a LABEL_REF. If it is
2600 found, ensure that a jump from INSN to the code_label to which the
2601 label_ref points ends up with the same stack as that at the
2602 code_label. Do this by inserting insns just before the code_label to
2603 pop and rotate the stack until it is in the correct order. REGSTACK
2604 is the order of the register stack in INSN.
2606 Any code that is emitted here must not be later processed as part
2607 of any block, as it will already contain hard register numbers. */
2610 goto_block_pat (insn
, regstack
, pat
)
2616 rtx new_jump
, new_label
, new_barrier
;
2619 struct stack_def temp_stack
;
2622 switch (GET_CODE (pat
))
2625 straighten_stack (PREV_INSN (insn
), regstack
);
2630 char *fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
2632 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
2635 goto_block_pat (insn
, regstack
, XEXP (pat
, i
));
2637 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
2638 goto_block_pat (insn
, regstack
, XVECEXP (pat
, i
, j
));
2645 label
= XEXP (pat
, 0);
2646 if (GET_CODE (label
) != CODE_LABEL
)
2649 /* First, see if in fact anything needs to be done to the stack at all. */
2650 if (INSN_UID (label
) <= 0)
2653 label_stack
= &block_stack_in
[BLOCK_NUM (label
)];
2655 if (label_stack
->top
== -2)
2657 /* If the target block hasn't had a stack order selected, then
2658 we need merely ensure that no pops are needed. */
2660 for (reg
= regstack
->top
; reg
>= 0; reg
--)
2661 if (! TEST_HARD_REG_BIT (label_stack
->reg_set
, regstack
->reg
[reg
]))
2666 /* change_stack will not emit any code in this case. */
2668 change_stack (label
, regstack
, label_stack
, emit_insn_after
);
2672 else if (label_stack
->top
== regstack
->top
)
2674 for (reg
= label_stack
->top
; reg
>= 0; reg
--)
2675 if (label_stack
->reg
[reg
] != regstack
->reg
[reg
])
2682 /* At least one insn will need to be inserted before label. Insert
2683 a jump around the code we are about to emit. Emit a label for the new
2684 code, and point the original insn at this new label. We can't use
2685 redirect_jump here, because we're using fld[4] of the code labels as
2686 LABEL_REF chains, no NUSES counters. */
2688 new_jump
= emit_jump_insn_before (gen_jump (label
), label
);
2689 record_label_references (new_jump
, PATTERN (new_jump
));
2690 JUMP_LABEL (new_jump
) = label
;
2692 new_barrier
= emit_barrier_after (new_jump
);
2694 new_label
= gen_label_rtx ();
2695 emit_label_after (new_label
, new_barrier
);
2696 LABEL_REFS (new_label
) = new_label
;
2698 /* The old label_ref will no longer point to the code_label if now uses,
2699 so strip the label_ref from the code_label's chain of references. */
2701 for (ref
= &LABEL_REFS (label
); *ref
!= label
; ref
= &LABEL_NEXTREF (*ref
))
2708 *ref
= LABEL_NEXTREF (*ref
);
2710 XEXP (pat
, 0) = new_label
;
2711 record_label_references (insn
, PATTERN (insn
));
2713 if (JUMP_LABEL (insn
) == label
)
2714 JUMP_LABEL (insn
) = new_label
;
2716 /* Now emit the needed code. */
2718 temp_stack
= *regstack
;
2720 change_stack (new_label
, &temp_stack
, label_stack
, emit_insn_after
);
2723 /* Traverse all basic blocks in a function, converting the register
2724 references in each insn from the "flat" register file that gcc uses, to
2725 the stack-like registers the 387 uses. */
2730 register int block
, reg
;
2731 register rtx insn
, next
;
2732 struct stack_def regstack
;
2734 for (block
= 0; block
< blocks
; block
++)
2736 if (block_stack_in
[block
].top
== -2)
2738 /* This block has not been previously encountered. Choose a
2739 default mapping for any stack regs live on entry */
2741 block_stack_in
[block
].top
= -1;
2743 for (reg
= LAST_STACK_REG
; reg
>= FIRST_STACK_REG
; reg
--)
2744 if (TEST_HARD_REG_BIT (block_stack_in
[block
].reg_set
, reg
))
2745 block_stack_in
[block
].reg
[++block_stack_in
[block
].top
] = reg
;
2748 /* Process all insns in this block. Keep track of `next' here,
2749 so that we don't process any insns emitted while making
2750 substitutions in INSN. */
2752 next
= block_begin
[block
];
2753 regstack
= block_stack_in
[block
];
2757 next
= NEXT_INSN (insn
);
2759 /* Don't bother processing unless there is a stack reg
2760 mentioned or if it's a CALL_INSN (register passing of
2761 floating point values). */
2763 if (GET_MODE (insn
) == QImode
|| GET_CODE (insn
) == CALL_INSN
)
2764 subst_stack_regs (insn
, ®stack
);
2766 } while (insn
!= block_end
[block
]);
2768 /* For all further actions, INSN needs to be the last insn in
2769 this basic block. If subst_stack_regs inserted additional
2770 instructions after INSN, it is no longer the last one at
2772 next
= PREV_INSN (next
);
2774 /* If subst_stack_regs inserted something after a JUMP_INSN, that
2775 is almost certainly a bug. */
2776 if (GET_CODE (insn
) == JUMP_INSN
&& insn
!= next
)
2780 /* Something failed if the stack life doesn't match. */
2782 GO_IF_HARD_REG_EQUAL (regstack
.reg_set
, block_out_reg_set
[block
], win
);
2788 /* Adjust the stack of this block on exit to match the stack of
2789 the target block, or copy stack information into stack of
2790 jump target if the target block's stack order hasn't been set
2793 if (GET_CODE (insn
) == JUMP_INSN
)
2794 goto_block_pat (insn
, ®stack
, PATTERN (insn
));
2796 /* Likewise handle the case where we fall into the next block. */
2798 if ((block
< blocks
- 1) && block_drops_in
[block
+1])
2799 change_stack (insn
, ®stack
, &block_stack_in
[block
+1],
2803 /* If the last basic block is the end of a loop, and that loop has
2804 regs live at its start, then the last basic block will have regs live
2805 at its end that need to be popped before the function returns. */
2808 int value_reg_low
, value_reg_high
;
2809 value_reg_low
= value_reg_high
= -1;
2812 if ((retvalue
= stack_result (current_function_decl
)))
2814 value_reg_low
= REGNO (retvalue
);
2815 value_reg_high
= value_reg_low
+
2816 HARD_REGNO_NREGS (value_reg_low
, GET_MODE (retvalue
)) - 1;
2820 for (reg
= regstack
.top
; reg
>= 0; reg
--)
2821 if (regstack
.reg
[reg
] < value_reg_low
2822 || regstack
.reg
[reg
] > value_reg_high
)
2823 insn
= emit_pop_insn (insn
, ®stack
,
2824 FP_MODE_REG (regstack
.reg
[reg
], DFmode
),
2827 straighten_stack (insn
, ®stack
);
2830 /* Check expression PAT, which is in INSN, for label references. if
2831 one is found, print the block number of destination to FILE. */
2834 print_blocks (file
, insn
, pat
)
2838 register RTX_CODE code
= GET_CODE (pat
);
2842 if (code
== LABEL_REF
)
2844 register rtx label
= XEXP (pat
, 0);
2846 if (GET_CODE (label
) != CODE_LABEL
)
2849 fprintf (file
, " %d", BLOCK_NUM (label
));
2854 fmt
= GET_RTX_FORMAT (code
);
2855 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2858 print_blocks (file
, insn
, XEXP (pat
, i
));
2862 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
2863 print_blocks (file
, insn
, XVECEXP (pat
, i
, j
));
2868 /* Write information about stack registers and stack blocks into FILE.
2869 This is part of making a debugging dump. */
2872 dump_stack_info (file
)
2877 fprintf (file
, "\n%d stack blocks.\n", blocks
);
2878 for (block
= 0; block
< blocks
; block
++)
2880 register rtx head
, jump
, end
;
2883 fprintf (file
, "\nStack block %d: first insn %d, last %d.\n",
2884 block
, INSN_UID (block_begin
[block
]),
2885 INSN_UID (block_end
[block
]));
2887 head
= block_begin
[block
];
2889 fprintf (file
, "Reached from blocks: ");
2890 if (GET_CODE (head
) == CODE_LABEL
)
2891 for (jump
= LABEL_REFS (head
);
2893 jump
= LABEL_NEXTREF (jump
))
2895 register int from_block
= BLOCK_NUM (CONTAINING_INSN (jump
));
2896 fprintf (file
, " %d", from_block
);
2898 if (block_drops_in
[block
])
2899 fprintf (file
, " previous");
2901 fprintf (file
, "\nlive stack registers on block entry: ");
2902 for (regno
= FIRST_STACK_REG
; regno
<= LAST_STACK_REG
; regno
++)
2904 if (TEST_HARD_REG_BIT (block_stack_in
[block
].reg_set
, regno
))
2905 fprintf (file
, "%d ", regno
);
2908 fprintf (file
, "\nlive stack registers on block exit: ");
2909 for (regno
= FIRST_STACK_REG
; regno
<= LAST_STACK_REG
; regno
++)
2911 if (TEST_HARD_REG_BIT (block_out_reg_set
[block
], regno
))
2912 fprintf (file
, "%d ", regno
);
2915 end
= block_end
[block
];
2917 fprintf (file
, "\nJumps to blocks: ");
2918 if (GET_CODE (end
) == JUMP_INSN
)
2919 print_blocks (file
, end
, PATTERN (end
));
2921 if (block
+ 1 < blocks
&& block_drops_in
[block
+1])
2922 fprintf (file
, " next");
2923 else if (block
+ 1 == blocks
2924 || (GET_CODE (end
) == JUMP_INSN
2925 && GET_CODE (PATTERN (end
)) == RETURN
))
2926 fprintf (file
, " return");
2928 fprintf (file
, "\n");
2931 #endif /* STACK_REGS */