1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1992, 93-98, 1999 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.
79 There are several rules on the usage of stack-like regs in
80 asm_operands insns. These rules apply only to the operands that are
83 1. Given a set of input regs that die in an asm_operands, it is
84 necessary to know which are implicitly popped by the asm, and
85 which must be explicitly popped by gcc.
87 An input reg that is implicitly popped by the asm must be
88 explicitly clobbered, unless it is constrained to match an
91 2. For any input reg that is implicitly popped by an asm, it is
92 necessary to know how to adjust the stack to compensate for the pop.
93 If any non-popped input is closer to the top of the reg-stack than
94 the implicitly popped reg, it would not be possible to know what the
95 stack looked like - it's not clear how the rest of the stack "slides
98 All implicitly popped input regs must be closer to the top of
99 the reg-stack than any input that is not implicitly popped.
101 3. It is possible that if an input dies in an insn, reload might
102 use the input reg for an output reload. Consider this example:
104 asm ("foo" : "=t" (a) : "f" (b));
106 This asm says that input B is not popped by the asm, and that
107 the asm pushes a result onto the reg-stack, ie, the stack is one
108 deeper after the asm than it was before. But, it is possible that
109 reload will think that it can use the same reg for both the input and
110 the output, if input B dies in this insn.
112 If any input operand uses the "f" constraint, all output reg
113 constraints must use the "&" earlyclobber.
115 The asm above would be written as
117 asm ("foo" : "=&t" (a) : "f" (b));
119 4. Some operands need to be in particular places on the stack. All
120 output operands fall in this category - there is no other way to
121 know which regs the outputs appear in unless the user indicates
122 this in the constraints.
124 Output operands must specifically indicate which reg an output
125 appears in after an asm. "=f" is not allowed: the operand
126 constraints must select a class with a single reg.
128 5. Output operands may not be "inserted" between existing stack regs.
129 Since no 387 opcode uses a read/write operand, all output operands
130 are dead before the asm_operands, and are pushed by the asm_operands.
131 It makes no sense to push anywhere but the top of the reg-stack.
133 Output operands must start at the top of the reg-stack: output
134 operands may not "skip" a reg.
136 6. Some asm statements may need extra stack space for internal
137 calculations. This can be guaranteed by clobbering stack registers
138 unrelated to the inputs and outputs.
140 Here are a couple of reasonable asms to want to write. This asm
141 takes one input, which is internally popped, and produces two outputs.
143 asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
145 This asm takes two inputs, which are popped by the fyl2xp1 opcode,
146 and replaces them with one output. The user must code the "st(1)"
147 clobber for reg-stack.c to know that fyl2xp1 pops both inputs.
149 asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
158 #include "function.h"
159 #include "insn-config.h"
161 #include "hard-reg-set.h"
163 #include "insn-flags.h"
170 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
172 /* This is the basic stack record. TOP is an index into REG[] such
173 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
175 If TOP is -2, REG[] is not yet initialized. Stack initialization
176 consists of placing each live reg in array `reg' and setting `top'
179 REG_SET indicates which registers are live. */
181 typedef struct stack_def
183 int top
; /* index to top stack element */
184 HARD_REG_SET reg_set
; /* set of live registers */
185 char reg
[REG_STACK_SIZE
]; /* register - stack mapping */
188 /* highest instruction uid */
189 static int max_uid
= 0;
191 /* Number of basic blocks in the current function. */
194 /* Element N is first insn in basic block N.
195 This info lasts until we finish compiling the function. */
196 static rtx
*block_begin
;
198 /* Element N is last insn in basic block N.
199 This info lasts until we finish compiling the function. */
200 static rtx
*block_end
;
202 /* Element N is nonzero if control can drop into basic block N */
203 static char *block_drops_in
;
205 /* Element N says all about the stack at entry block N */
206 static stack block_stack_in
;
208 /* Element N says all about the stack life at the end of block N */
209 static HARD_REG_SET
*block_out_reg_set
;
211 /* This is where the BLOCK_NUM values are really stored. This is set
212 up by find_blocks and used there and in life_analysis. It can be used
213 later, but only to look up an insn that is the head or tail of some
214 block. life_analysis and the stack register conversion process can
215 add insns within a block. */
216 static int *block_number
;
218 /* We use this array to cache info about insns, because otherwise we
219 spend too much time in stack_regs_mentioned_p.
221 Indexed by insn UIDs. A value of zero is uninitialized, one indicates
222 the insn uses stack registers, two indicates the insn does not use
224 static varray_type stack_regs_mentioned_data
;
226 /* This is the register file for all register after conversion */
228 FP_mode_reg
[LAST_STACK_REG
+1-FIRST_STACK_REG
][(int) MAX_MACHINE_MODE
];
230 #define FP_MODE_REG(regno,mode) \
231 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int)(mode)])
233 /* Get the basic block number of an insn. See note at block_number
234 definition are validity of this information. */
236 #define BLOCK_NUM(INSN) \
237 ((INSN_UID (INSN) > max_uid) \
238 ? (abort() , -1) : block_number[INSN_UID (INSN)])
240 /* Forward declarations */
242 static int stack_regs_mentioned_p
PROTO((rtx pat
));
243 static void mark_regs_pat
PROTO((rtx
, HARD_REG_SET
*));
244 static void straighten_stack
PROTO((rtx
, stack
));
245 static void pop_stack
PROTO((stack
, int));
246 static void record_label_references
PROTO((rtx
, rtx
));
247 static rtx
*get_true_reg
PROTO((rtx
*));
249 static void record_asm_reg_life
PROTO((rtx
, stack
));
250 static void record_reg_life_pat
PROTO((rtx
, HARD_REG_SET
*,
251 HARD_REG_SET
*, int));
252 static int get_asm_operand_n_inputs
PROTO((rtx
));
253 static void record_reg_life
PROTO((rtx
, int, stack
));
254 static void find_blocks
PROTO((rtx
));
255 static rtx stack_result
PROTO((tree
));
256 static void stack_reg_life_analysis
PROTO((rtx
, HARD_REG_SET
*));
257 static void replace_reg
PROTO((rtx
*, int));
258 static void remove_regno_note
PROTO((rtx
, enum reg_note
, int));
259 static int get_hard_regnum
PROTO((stack
, rtx
));
260 static void delete_insn_for_stacker
PROTO((rtx
));
261 static rtx emit_pop_insn
PROTO((rtx
, stack
, rtx
, rtx (*) ()));
262 static void emit_swap_insn
PROTO((rtx
, stack
, rtx
));
263 static void move_for_stack_reg
PROTO((rtx
, stack
, rtx
));
264 static int swap_rtx_condition_1
PROTO((rtx
));
265 static int swap_rtx_condition
PROTO((rtx
));
266 static void compare_for_stack_reg
PROTO((rtx
, stack
, rtx
));
267 static void subst_stack_regs_pat
PROTO((rtx
, stack
, rtx
));
268 static void subst_asm_stack_regs
PROTO((rtx
, stack
));
269 static void subst_stack_regs
PROTO((rtx
, stack
));
270 static void change_stack
PROTO((rtx
, stack
, stack
, rtx (*) ()));
272 static void goto_block_pat
PROTO((rtx
, stack
, rtx
));
273 static void convert_regs
PROTO((void));
274 static void print_blocks
PROTO((FILE *, rtx
, rtx
));
275 static void dump_stack_info
PROTO((FILE *));
277 /* Return non-zero if any stack register is mentioned somewhere within PAT. */
280 stack_regs_mentioned_p (pat
)
283 register const char *fmt
;
286 if (STACK_REG_P (pat
))
289 fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
290 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
296 for (j
= XVECLEN (pat
, i
) - 1; j
>= 0; j
--)
297 if (stack_regs_mentioned_p (XVECEXP (pat
, i
, j
)))
300 else if (fmt
[i
] == 'e' && stack_regs_mentioned_p (XEXP (pat
, i
)))
307 /* Return nonzero if INSN mentions stacked registers, else return zero. */
310 stack_regs_mentioned (insn
)
313 unsigned int uid
, max
;
316 if (GET_RTX_CLASS (GET_CODE (insn
)) != 'i')
319 uid
= INSN_UID (insn
);
320 max
= VARRAY_SIZE (stack_regs_mentioned_data
);
323 /* Allocate some extra size to avoid too many reallocs, but
324 do not grow too quickly. */
325 max
= uid
+ uid
/ 20;
326 VARRAY_GROW (stack_regs_mentioned_data
, max
);
329 test
= VARRAY_CHAR (stack_regs_mentioned_data
, uid
);
332 /* This insn has yet to be examined. Do so now. */
333 test
= stack_regs_mentioned_p (PATTERN (insn
)) ? 1 : 2;
334 VARRAY_CHAR (stack_regs_mentioned_data
, uid
) = test
;
340 static rtx ix86_flags_rtx
;
343 next_flags_user (insn
)
346 /* Search forward looking for the first use of this value.
347 Stop at block boundaries. */
348 /* ??? This really cries for BLOCK_END! */
352 insn
= NEXT_INSN (insn
);
356 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i'
357 && reg_mentioned_p (ix86_flags_rtx
, PATTERN (insn
)))
360 if (GET_CODE (insn
) == JUMP_INSN
361 || GET_CODE (insn
) == CODE_LABEL
362 || GET_CODE (insn
) == CALL_INSN
)
367 /* Mark all registers needed for this pattern. */
370 mark_regs_pat (pat
, set
)
374 enum machine_mode mode
;
378 if (GET_CODE (pat
) == SUBREG
)
380 mode
= GET_MODE (pat
);
381 regno
= SUBREG_WORD (pat
);
382 regno
+= REGNO (SUBREG_REG (pat
));
385 regno
= REGNO (pat
), mode
= GET_MODE (pat
);
387 for (count
= HARD_REGNO_NREGS (regno
, mode
);
388 count
; count
--, regno
++)
389 SET_HARD_REG_BIT (*set
, regno
);
392 /* Reorganise the stack into ascending numbers,
396 straighten_stack (insn
, regstack
)
400 struct stack_def temp_stack
;
403 /* If there is only a single register on the stack, then the stack is
404 already in increasing order and no reorganization is needed.
406 Similarly if the stack is empty. */
407 if (regstack
->top
<= 0)
410 COPY_HARD_REG_SET (temp_stack
.reg_set
, regstack
->reg_set
);
412 for (top
= temp_stack
.top
= regstack
->top
; top
>= 0; top
--)
413 temp_stack
.reg
[top
] = FIRST_STACK_REG
+ temp_stack
.top
- top
;
415 change_stack (insn
, regstack
, &temp_stack
, emit_insn_after
);
418 /* Pop a register from the stack */
421 pop_stack (regstack
, regno
)
425 int top
= regstack
->top
;
427 CLEAR_HARD_REG_BIT (regstack
->reg_set
, regno
);
429 /* If regno was not at the top of stack then adjust stack */
430 if (regstack
->reg
[top
] != regno
)
433 for (i
= regstack
->top
; i
>= 0; i
--)
434 if (regstack
->reg
[i
] == regno
)
437 for (j
= i
; j
< top
; j
++)
438 regstack
->reg
[j
] = regstack
->reg
[j
+ 1];
444 /* Convert register usage from "flat" register file usage to a "stack
445 register file. FIRST is the first insn in the function, FILE is the
448 First compute the beginning and end of each basic block. Do a
449 register life analysis on the stack registers, recording the result
450 for the head and tail of each basic block. The convert each insn one
451 by one. Run a last jump_optimize() pass, if optimizing, to eliminate
452 any cross-jumping created when the converter inserts pop insns.*/
455 reg_to_stack (first
, file
)
461 int stack_reg_seen
= 0;
462 enum machine_mode mode
;
463 HARD_REG_SET stackentry
;
465 ix86_flags_rtx
= gen_rtx_REG (CCmode
, FLAGS_REG
);
467 max_uid
= get_max_uid ();
468 VARRAY_CHAR_INIT (stack_regs_mentioned_data
, max_uid
+ 1,
469 "stack_regs_mentioned cache");
471 CLEAR_HARD_REG_SET (stackentry
);
474 static int initialised
;
478 initialised
= 1; /* This array can not have been previously
479 initialised, because the rtx's are
480 thrown away between compilations of
483 for (i
= FIRST_STACK_REG
; i
<= LAST_STACK_REG
; i
++)
485 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_FLOAT
); mode
!= VOIDmode
;
486 mode
= GET_MODE_WIDER_MODE (mode
))
487 FP_MODE_REG (i
, mode
) = gen_rtx_REG (mode
, i
);
488 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT
); mode
!= VOIDmode
;
489 mode
= GET_MODE_WIDER_MODE (mode
))
490 FP_MODE_REG (i
, mode
) = gen_rtx_REG (mode
, i
);
495 /* Count the basic blocks. Also find maximum insn uid. */
497 register RTX_CODE prev_code
= BARRIER
;
498 register RTX_CODE code
;
499 register int before_function_beg
= 1;
503 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
505 /* Note that this loop must select the same block boundaries
506 as code in find_blocks. Also note that this code is not the
507 same as that used in flow.c. */
509 if (INSN_UID (insn
) > max_uid
)
510 max_uid
= INSN_UID (insn
);
512 code
= GET_CODE (insn
);
514 if (code
== CODE_LABEL
515 || (prev_code
!= INSN
516 && prev_code
!= CALL_INSN
517 && prev_code
!= CODE_LABEL
518 && GET_RTX_CLASS (code
) == 'i'))
521 if (code
== NOTE
&& NOTE_LINE_NUMBER (insn
) == NOTE_INSN_FUNCTION_BEG
)
522 before_function_beg
= 0;
524 /* Remember whether or not this insn mentions an FP regs.
525 Check JUMP_INSNs too, in case someone creates a funny PARALLEL. */
527 if (GET_RTX_CLASS (code
) == 'i'
528 && stack_regs_mentioned_p (PATTERN (insn
)))
531 VARRAY_CHAR (stack_regs_mentioned_data
, INSN_UID (insn
)) = 1;
533 /* Note any register passing parameters. */
535 if (before_function_beg
&& code
== INSN
536 && GET_CODE (PATTERN (insn
)) == USE
)
537 record_reg_life_pat (PATTERN (insn
), (HARD_REG_SET
*) 0,
541 VARRAY_CHAR (stack_regs_mentioned_data
, INSN_UID (insn
)) = 2;
543 if (code
== CODE_LABEL
)
544 LABEL_REFS (insn
) = insn
; /* delete old chain */
551 /* If no stack register reference exists in this insn, there isn't
552 anything to convert. */
554 if (! stack_reg_seen
)
556 VARRAY_FREE (stack_regs_mentioned_data
);
560 /* If there are stack registers, there must be at least one block. */
565 /* Allocate some tables that last till end of compiling this function
566 and some needed only in find_blocks and life_analysis. */
568 block_begin
= (rtx
*) alloca (blocks
* sizeof (rtx
));
569 block_end
= (rtx
*) alloca (blocks
* sizeof (rtx
));
570 block_drops_in
= (char *) alloca (blocks
);
572 block_stack_in
= (stack
) alloca (blocks
* sizeof (struct stack_def
));
573 block_out_reg_set
= (HARD_REG_SET
*) alloca (blocks
* sizeof (HARD_REG_SET
));
574 bzero ((char *) block_stack_in
, blocks
* sizeof (struct stack_def
));
575 bzero ((char *) block_out_reg_set
, blocks
* sizeof (HARD_REG_SET
));
577 block_number
= (int *) alloca ((max_uid
+ 1) * sizeof (int));
580 stack_reg_life_analysis (first
, &stackentry
);
582 /* Dump the life analysis debug information before jump
583 optimization, as that will destroy the LABEL_REFS we keep the
587 dump_stack_info (file
);
592 jump_optimize (first
, 2, 0, 0);
594 VARRAY_FREE (stack_regs_mentioned_data
);
597 /* Check PAT, which is in INSN, for LABEL_REFs. Add INSN to the
598 label's chain of references, and note which insn contains each
602 record_label_references (insn
, pat
)
605 register enum rtx_code code
= GET_CODE (pat
);
607 register const char *fmt
;
609 if (code
== LABEL_REF
)
611 register rtx label
= XEXP (pat
, 0);
614 if (GET_CODE (label
) != CODE_LABEL
)
617 /* If this is an undefined label, LABEL_REFS (label) contains
619 if (INSN_UID (label
) == 0)
622 /* Don't make a duplicate in the code_label's chain. */
624 for (ref
= LABEL_REFS (label
);
626 ref
= LABEL_NEXTREF (ref
))
627 if (CONTAINING_INSN (ref
) == insn
)
630 CONTAINING_INSN (pat
) = insn
;
631 LABEL_NEXTREF (pat
) = LABEL_REFS (label
);
632 LABEL_REFS (label
) = pat
;
637 fmt
= GET_RTX_FORMAT (code
);
638 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
641 record_label_references (insn
, XEXP (pat
, i
));
645 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
646 record_label_references (insn
, XVECEXP (pat
, i
, j
));
651 /* Return a pointer to the REG expression within PAT. If PAT is not a
652 REG, possible enclosed by a conversion rtx, return the inner part of
653 PAT that stopped the search. */
660 switch (GET_CODE (*pat
))
663 /* Eliminate FP subregister accesses in favour of the
664 actual FP register in use. */
667 if (FP_REG_P (subreg
= SUBREG_REG (*pat
)))
669 *pat
= FP_MODE_REG (REGNO (subreg
) + SUBREG_WORD (*pat
),
678 pat
= & XEXP (*pat
, 0);
682 /* Record the life info of each stack reg in INSN, updating REGSTACK.
683 N_INPUTS is the number of inputs; N_OUTPUTS the outputs.
685 There are many rules that an asm statement for stack-like regs must
686 follow. Those rules are explained at the top of this file: the rule
687 numbers below refer to that explanation. */
690 record_asm_reg_life (insn
, regstack
)
696 int malformed_asm
= 0;
697 rtx body
= PATTERN (insn
);
699 int reg_used_as_output
[FIRST_PSEUDO_REGISTER
];
700 int implicitly_dies
[FIRST_PSEUDO_REGISTER
];
704 int n_inputs
, n_outputs
;
706 /* Find out what the constraints require. If no constraint
707 alternative matches, this asm is malformed. */
709 constrain_operands (1);
710 alt
= which_alternative
;
712 preprocess_constraints ();
714 n_inputs
= get_asm_operand_n_inputs (body
);
715 n_outputs
= recog_data
.n_operands
- n_inputs
;
720 /* Avoid further trouble with this insn. */
721 PATTERN (insn
) = gen_rtx_USE (VOIDmode
, const0_rtx
);
722 PUT_MODE (insn
, VOIDmode
);
726 /* Strip SUBREGs here to make the following code simpler. */
727 for (i
= 0; i
< recog_data
.n_operands
; i
++)
728 if (GET_CODE (recog_data
.operand
[i
]) == SUBREG
729 && GET_CODE (SUBREG_REG (recog_data
.operand
[i
])) == REG
)
730 recog_data
.operand
[i
] = SUBREG_REG (recog_data
.operand
[i
]);
732 /* Set up CLOBBER_REG. */
736 if (GET_CODE (body
) == PARALLEL
)
738 clobber_reg
= (rtx
*) alloca (XVECLEN (body
, 0) * sizeof (rtx
));
740 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
741 if (GET_CODE (XVECEXP (body
, 0, i
)) == CLOBBER
)
743 rtx clobber
= XVECEXP (body
, 0, i
);
744 rtx reg
= XEXP (clobber
, 0);
746 if (GET_CODE (reg
) == SUBREG
&& GET_CODE (SUBREG_REG (reg
)) == REG
)
747 reg
= SUBREG_REG (reg
);
749 if (STACK_REG_P (reg
))
751 clobber_reg
[n_clobbers
] = reg
;
757 /* Enforce rule #4: Output operands must specifically indicate which
758 reg an output appears in after an asm. "=f" is not allowed: the
759 operand constraints must select a class with a single reg.
761 Also enforce rule #5: Output operands must start at the top of
762 the reg-stack: output operands may not "skip" a reg. */
764 bzero ((char *) reg_used_as_output
, sizeof (reg_used_as_output
));
765 for (i
= 0; i
< n_outputs
; i
++)
766 if (STACK_REG_P (recog_data
.operand
[i
]))
768 if (reg_class_size
[(int) recog_op_alt
[i
][alt
].class] != 1)
770 error_for_asm (insn
, "Output constraint %d must specify a single register", i
);
774 reg_used_as_output
[REGNO (recog_data
.operand
[i
])] = 1;
778 /* Search for first non-popped reg. */
779 for (i
= FIRST_STACK_REG
; i
< LAST_STACK_REG
+ 1; i
++)
780 if (! reg_used_as_output
[i
])
783 /* If there are any other popped regs, that's an error. */
784 for (; i
< LAST_STACK_REG
+ 1; i
++)
785 if (reg_used_as_output
[i
])
788 if (i
!= LAST_STACK_REG
+ 1)
790 error_for_asm (insn
, "Output regs must be grouped at top of stack");
794 /* Enforce rule #2: All implicitly popped input regs must be closer
795 to the top of the reg-stack than any input that is not implicitly
798 bzero ((char *) implicitly_dies
, sizeof (implicitly_dies
));
799 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
800 if (STACK_REG_P (recog_data
.operand
[i
]))
802 /* An input reg is implicitly popped if it is tied to an
803 output, or if there is a CLOBBER for it. */
806 for (j
= 0; j
< n_clobbers
; j
++)
807 if (operands_match_p (clobber_reg
[j
], recog_data
.operand
[i
]))
810 if (j
< n_clobbers
|| recog_op_alt
[i
][alt
].matches
>= 0)
811 implicitly_dies
[REGNO (recog_data
.operand
[i
])] = 1;
814 /* Search for first non-popped reg. */
815 for (i
= FIRST_STACK_REG
; i
< LAST_STACK_REG
+ 1; i
++)
816 if (! implicitly_dies
[i
])
819 /* If there are any other popped regs, that's an error. */
820 for (; i
< LAST_STACK_REG
+ 1; i
++)
821 if (implicitly_dies
[i
])
824 if (i
!= LAST_STACK_REG
+ 1)
827 "Implicitly popped regs must be grouped at top of stack");
831 /* Enfore rule #3: If any input operand uses the "f" constraint, all
832 output constraints must use the "&" earlyclobber.
834 ??? Detect this more deterministically by having constraint_asm_operands
835 record any earlyclobber. */
837 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
838 if (recog_op_alt
[i
][alt
].matches
== -1)
842 for (j
= 0; j
< n_outputs
; j
++)
843 if (operands_match_p (recog_data
.operand
[j
], recog_data
.operand
[i
]))
846 "Output operand %d must use `&' constraint", j
);
853 /* Avoid further trouble with this insn. */
854 PATTERN (insn
) = gen_rtx_USE (VOIDmode
, const0_rtx
);
855 VARRAY_CHAR (stack_regs_mentioned_data
, INSN_UID (insn
)) = 2;
859 /* Process all outputs */
860 for (i
= 0; i
< n_outputs
; i
++)
862 rtx op
= recog_data
.operand
[i
];
864 if (! STACK_REG_P (op
))
866 if (stack_regs_mentioned_p (op
))
872 /* Each destination is dead before this insn. If the
873 destination is not used after this insn, record this with
876 if (! TEST_HARD_REG_BIT (regstack
->reg_set
, REGNO (op
)))
877 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_UNUSED
, op
,
880 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (op
));
883 /* Process all inputs */
884 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
886 rtx op
= recog_data
.operand
[i
];
887 if (! STACK_REG_P (op
))
889 if (stack_regs_mentioned_p (op
))
895 /* If an input is dead after the insn, record a death note.
896 But don't record a death note if there is already a death note,
897 or if the input is also an output. */
899 if (! TEST_HARD_REG_BIT (regstack
->reg_set
, REGNO (op
))
900 && recog_op_alt
[i
][alt
].matches
== -1
901 && find_regno_note (insn
, REG_DEAD
, REGNO (op
)) == NULL_RTX
)
902 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_DEAD
, op
, REG_NOTES (insn
));
904 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (op
));
908 /* Scan PAT, which is part of INSN, and record registers appearing in
909 a SET_DEST in DEST, and other registers in SRC.
911 This function does not know about SET_DESTs that are both input and
912 output (such as ZERO_EXTRACT) - this cannot happen on a 387. */
915 record_reg_life_pat (pat
, src
, dest
, douse
)
917 HARD_REG_SET
*src
, *dest
;
920 register const char *fmt
;
923 if (STACK_REG_P (pat
)
924 || (GET_CODE (pat
) == SUBREG
&& STACK_REG_P (SUBREG_REG (pat
))))
927 mark_regs_pat (pat
, src
);
930 mark_regs_pat (pat
, dest
);
935 if (GET_CODE (pat
) == SET
)
937 record_reg_life_pat (XEXP (pat
, 0), NULL_PTR
, dest
, 0);
938 record_reg_life_pat (XEXP (pat
, 1), src
, NULL_PTR
, 0);
942 /* We don't need to consider either of these cases. */
943 if ((GET_CODE (pat
) == USE
&& !douse
) || GET_CODE (pat
) == CLOBBER
)
946 fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
947 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
953 for (j
= XVECLEN (pat
, i
) - 1; j
>= 0; j
--)
954 record_reg_life_pat (XVECEXP (pat
, i
, j
), src
, dest
, 0);
956 else if (fmt
[i
] == 'e')
957 record_reg_life_pat (XEXP (pat
, i
), src
, dest
, 0);
961 /* Calculate the number of inputs and outputs in BODY, an
962 asm_operands. N_OPERANDS is the total number of operands, and
963 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
967 get_asm_operand_n_inputs (body
)
970 if (GET_CODE (body
) == SET
&& GET_CODE (SET_SRC (body
)) == ASM_OPERANDS
)
971 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body
));
973 else if (GET_CODE (body
) == ASM_OPERANDS
)
974 return ASM_OPERANDS_INPUT_LENGTH (body
);
976 else if (GET_CODE (body
) == PARALLEL
977 && GET_CODE (XVECEXP (body
, 0, 0)) == SET
)
978 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (XVECEXP (body
, 0, 0)));
980 else if (GET_CODE (body
) == PARALLEL
981 && GET_CODE (XVECEXP (body
, 0, 0)) == ASM_OPERANDS
)
982 return ASM_OPERANDS_INPUT_LENGTH (XVECEXP (body
, 0, 0));
987 /* Scan INSN, which is in BLOCK, and record the life & death of stack
988 registers in REGSTACK. This function is called to process insns from
989 the last insn in a block to the first. The actual scanning is done in
992 If a register is live after a CALL_INSN, but is not a value return
993 register for that CALL_INSN, then code is emitted to initialize that
994 register. The block_end[] data is kept accurate.
996 Existing death and unset notes for stack registers are deleted
997 before processing the insn. */
1000 record_reg_life (insn
, block
, regstack
)
1005 rtx note
, *note_link
;
1008 if ((GET_CODE (insn
) != INSN
&& GET_CODE (insn
) != CALL_INSN
)
1009 || INSN_DELETED_P (insn
))
1012 /* Strip death notes for stack regs from this insn */
1014 note_link
= ®_NOTES(insn
);
1015 for (note
= *note_link
; note
; note
= XEXP (note
, 1))
1016 if (STACK_REG_P (XEXP (note
, 0))
1017 && (REG_NOTE_KIND (note
) == REG_DEAD
1018 || REG_NOTE_KIND (note
) == REG_UNUSED
))
1019 *note_link
= XEXP (note
, 1);
1021 note_link
= &XEXP (note
, 1);
1023 /* Process all patterns in the insn. */
1025 n_operands
= asm_noperands (PATTERN (insn
));
1026 if (n_operands
>= 0)
1028 record_asm_reg_life (insn
, regstack
);
1033 HARD_REG_SET src
, dest
;
1036 CLEAR_HARD_REG_SET (src
);
1037 CLEAR_HARD_REG_SET (dest
);
1039 if (GET_CODE (insn
) == CALL_INSN
)
1040 for (note
= CALL_INSN_FUNCTION_USAGE (insn
);
1042 note
= XEXP (note
, 1))
1043 if (GET_CODE (XEXP (note
, 0)) == USE
)
1044 record_reg_life_pat (SET_DEST (XEXP (note
, 0)), &src
, NULL_PTR
, 0);
1046 record_reg_life_pat (PATTERN (insn
), &src
, &dest
, 0);
1047 for (regno
= FIRST_STACK_REG
; regno
<= LAST_STACK_REG
; regno
++)
1048 if (! TEST_HARD_REG_BIT (regstack
->reg_set
, regno
))
1050 if (TEST_HARD_REG_BIT (src
, regno
)
1051 && ! TEST_HARD_REG_BIT (dest
, regno
))
1052 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_DEAD
,
1053 FP_MODE_REG (regno
, DFmode
),
1055 else if (TEST_HARD_REG_BIT (dest
, regno
))
1056 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_UNUSED
,
1057 FP_MODE_REG (regno
, DFmode
),
1061 if (GET_CODE (insn
) == CALL_INSN
)
1065 /* There might be a reg that is live after a function call.
1066 Initialize it to zero so that the program does not crash. See
1067 comment towards the end of stack_reg_life_analysis(). */
1069 for (reg
= FIRST_STACK_REG
; reg
<= LAST_STACK_REG
; reg
++)
1070 if (! TEST_HARD_REG_BIT (dest
, reg
)
1071 && TEST_HARD_REG_BIT (regstack
->reg_set
, reg
))
1075 /* The insn will use virtual register numbers, and so
1076 convert_regs is expected to process these. But BLOCK_NUM
1077 cannot be used on these insns, because they do not appear in
1080 pat
= gen_rtx_SET (VOIDmode
, FP_MODE_REG (reg
, DFmode
),
1081 CONST0_RTX (DFmode
));
1082 init
= emit_insn_after (pat
, insn
);
1084 CLEAR_HARD_REG_BIT (regstack
->reg_set
, reg
);
1086 /* If the CALL_INSN was the end of a block, move the
1087 block_end to point to the new insn. */
1089 if (block_end
[block
] == insn
)
1090 block_end
[block
] = init
;
1093 /* Some regs do not survive a CALL */
1094 AND_COMPL_HARD_REG_SET (regstack
->reg_set
, call_used_reg_set
);
1097 AND_COMPL_HARD_REG_SET (regstack
->reg_set
, dest
);
1098 IOR_HARD_REG_SET (regstack
->reg_set
, src
);
1102 /* Find all basic blocks of the function, which starts with FIRST.
1103 For each JUMP_INSN, build the chain of LABEL_REFS on each CODE_LABEL. */
1111 register RTX_CODE prev_code
= BARRIER
;
1112 register RTX_CODE code
;
1113 rtx label_value_list
= 0;
1115 /* Record where all the blocks start and end.
1116 Record which basic blocks control can drop in to. */
1119 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
1121 /* Note that this loop must select the same block boundaries
1122 as code in reg_to_stack, but that these are not the same
1123 as those selected in flow.c. */
1125 code
= GET_CODE (insn
);
1127 if (code
== CODE_LABEL
1128 || (prev_code
!= INSN
1129 && prev_code
!= CALL_INSN
1130 && prev_code
!= CODE_LABEL
1131 && GET_RTX_CLASS (code
) == 'i'))
1133 block_begin
[++block
] = insn
;
1134 block_end
[block
] = insn
;
1135 block_drops_in
[block
] = prev_code
!= BARRIER
;
1137 else if (GET_RTX_CLASS (code
) == 'i')
1138 block_end
[block
] = insn
;
1140 if (GET_RTX_CLASS (code
) == 'i')
1144 /* Make a list of all labels referred to other than by jumps. */
1145 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
1146 if (REG_NOTE_KIND (note
) == REG_LABEL
)
1147 label_value_list
= gen_rtx_EXPR_LIST (VOIDmode
, XEXP (note
, 0),
1151 block_number
[INSN_UID (insn
)] = block
;
1157 if (block
+ 1 != blocks
)
1160 /* generate all label references to the corresponding jump insn */
1161 for (block
= 0; block
< blocks
; block
++)
1163 insn
= block_end
[block
];
1165 if (GET_CODE (insn
) == JUMP_INSN
)
1167 rtx pat
= PATTERN (insn
);
1170 if (computed_jump_p (insn
))
1172 for (x
= label_value_list
; x
; x
= XEXP (x
, 1))
1173 record_label_references (insn
,
1174 gen_rtx_LABEL_REF (VOIDmode
,
1177 for (x
= forced_labels
; x
; x
= XEXP (x
, 1))
1178 record_label_references (insn
,
1179 gen_rtx_LABEL_REF (VOIDmode
,
1183 record_label_references (insn
, pat
);
1188 /* If current function returns its result in an fp stack register,
1189 return the REG. Otherwise, return 0. */
1197 /* If the value is supposed to be returned in memory, then clearly
1198 it is not returned in a stack register. */
1199 if (aggregate_value_p (DECL_RESULT (decl
)))
1202 result
= DECL_RTL (DECL_RESULT (decl
));
1203 /* ?!? What is this code supposed to do? Can this code actually
1204 trigger if we kick out aggregates above? */
1206 && ! (GET_CODE (result
) == REG
1207 && REGNO (result
) < FIRST_PSEUDO_REGISTER
))
1209 #ifdef FUNCTION_OUTGOING_VALUE
1211 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (decl
)), decl
);
1213 result
= FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (decl
)), decl
);
1217 return result
!= 0 && STACK_REG_P (result
) ? result
: 0;
1220 /* Determine the which registers are live at the start of each basic
1221 block of the function whose first insn is FIRST.
1223 First, if the function returns a real_type, mark the function
1224 return type as live at each return point, as the RTL may not give any
1225 hint that the register is live.
1227 Then, start with the last block and work back to the first block.
1228 Similarly, work backwards within each block, insn by insn, recording
1229 which regs are dead and which are used (and therefore live) in the
1230 hard reg set of block_stack_in[].
1232 After processing each basic block, if there is a label at the start
1233 of the block, propagate the live registers to all jumps to this block.
1235 As a special case, if there are regs live in this block, that are
1236 not live in a block containing a jump to this label, and the block
1237 containing the jump has already been processed, we must propagate this
1238 block's entry register life back to the block containing the jump, and
1239 restart life analysis from there.
1241 In the worst case, this function may traverse the insns
1242 REG_STACK_SIZE times. This is necessary, since a jump towards the end
1243 of the insns may not know that a reg is live at a target that is early
1244 in the insns. So we back up and start over with the new reg live.
1246 If there are registers that are live at the start of the function,
1247 insns are emitted to initialize these registers. Something similar is
1248 done after CALL_INSNs in record_reg_life. */
1251 stack_reg_life_analysis (first
, stackentry
)
1253 HARD_REG_SET
*stackentry
;
1256 struct stack_def regstack
;
1261 if ((retvalue
= stack_result (current_function_decl
)))
1263 /* Find all RETURN insns and mark them. */
1265 for (block
= blocks
- 1; --block
>= 0;)
1266 if (GET_CODE (block_end
[block
]) == JUMP_INSN
1267 && returnjump_p (block_end
[block
]))
1268 mark_regs_pat (retvalue
, block_out_reg_set
+block
);
1270 /* Mark off the end of last block if we "fall off" the end of the
1271 function into the epilogue. */
1273 if (GET_CODE (block_end
[blocks
-1]) != JUMP_INSN
1274 || GET_CODE (PATTERN (block_end
[blocks
-1])) == RETURN
)
1275 mark_regs_pat (retvalue
, block_out_reg_set
+blocks
-1);
1279 /* now scan all blocks backward for stack register use */
1284 register rtx insn
, prev
;
1286 /* current register status at last instruction */
1288 COPY_HARD_REG_SET (regstack
.reg_set
, block_out_reg_set
[block
]);
1290 prev
= block_end
[block
];
1294 prev
= PREV_INSN (insn
);
1296 /* If the insn is a CALL_INSN, we need to ensure that
1297 everything dies. But otherwise don't process unless there
1298 are some stack regs present. */
1300 if (stack_regs_mentioned (insn
) || GET_CODE (insn
) == CALL_INSN
)
1301 record_reg_life (insn
, block
, ®stack
);
1303 } while (insn
!= block_begin
[block
]);
1305 /* Set the state at the start of the block. Mark that no
1306 register mapping information known yet. */
1308 COPY_HARD_REG_SET (block_stack_in
[block
].reg_set
, regstack
.reg_set
);
1309 block_stack_in
[block
].top
= -2;
1311 /* If there is a label, propagate our register life to all jumps
1314 if (GET_CODE (insn
) == CODE_LABEL
)
1317 int must_restart
= 0;
1319 for (label
= LABEL_REFS (insn
); label
!= insn
;
1320 label
= LABEL_NEXTREF (label
))
1322 int jump_block
= BLOCK_NUM (CONTAINING_INSN (label
));
1324 if (jump_block
< block
)
1325 IOR_HARD_REG_SET (block_out_reg_set
[jump_block
],
1326 block_stack_in
[block
].reg_set
);
1329 /* The block containing the jump has already been
1330 processed. If there are registers that were not known
1331 to be live then, but are live now, we must back up
1332 and restart life analysis from that point with the new
1333 life information. */
1335 GO_IF_HARD_REG_SUBSET (block_stack_in
[block
].reg_set
,
1336 block_out_reg_set
[jump_block
],
1339 IOR_HARD_REG_SET (block_out_reg_set
[jump_block
],
1340 block_stack_in
[block
].reg_set
);
1354 if (block_drops_in
[block
])
1355 IOR_HARD_REG_SET (block_out_reg_set
[block
-1],
1356 block_stack_in
[block
].reg_set
);
1361 /* If any reg is live at the start of the first block of a
1362 function, then we must guarantee that the reg holds some value by
1363 generating our own "load" of that register. Otherwise a 387 would
1364 fault trying to access an empty register. */
1366 /* Load zero into each live register. The fact that a register
1367 appears live at the function start necessarily implies an error
1368 in the user program: it means that (unless the offending code is *never*
1369 executed) this program is using uninitialised floating point
1370 variables. In order to keep broken code like this happy, we initialise
1371 those variables with zero.
1373 Note that we are inserting virtual register references here:
1374 these insns must be processed by convert_regs later. Also, these
1375 insns will not be in block_number, so BLOCK_NUM() will fail for them. */
1377 for (reg
= LAST_STACK_REG
; reg
>= FIRST_STACK_REG
; reg
--)
1378 if (TEST_HARD_REG_BIT (block_stack_in
[0].reg_set
, reg
)
1379 && ! TEST_HARD_REG_BIT (*stackentry
, reg
))
1383 init_rtx
= gen_rtx_SET (VOIDmode
, FP_MODE_REG(reg
, DFmode
),
1384 CONST0_RTX (DFmode
));
1385 block_begin
[0] = emit_insn_after (init_rtx
, first
);
1387 CLEAR_HARD_REG_BIT (block_stack_in
[0].reg_set
, reg
);
1392 * This section deals with stack register substitution, and forms the second
1393 * pass over the RTL.
1396 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
1397 the desired hard REGNO. */
1400 replace_reg (reg
, regno
)
1404 if (regno
< FIRST_STACK_REG
|| regno
> LAST_STACK_REG
1405 || ! STACK_REG_P (*reg
))
1408 switch (GET_MODE_CLASS (GET_MODE (*reg
)))
1412 case MODE_COMPLEX_FLOAT
:;
1415 *reg
= FP_MODE_REG (regno
, GET_MODE (*reg
));
1418 /* Remove a note of type NOTE, which must be found, for register
1419 number REGNO from INSN. Remove only one such note. */
1422 remove_regno_note (insn
, note
, regno
)
1427 register rtx
*note_link
, this;
1429 note_link
= ®_NOTES(insn
);
1430 for (this = *note_link
; this; this = XEXP (this, 1))
1431 if (REG_NOTE_KIND (this) == note
1432 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno
)
1434 *note_link
= XEXP (this, 1);
1438 note_link
= &XEXP (this, 1);
1443 /* Find the hard register number of virtual register REG in REGSTACK.
1444 The hard register number is relative to the top of the stack. -1 is
1445 returned if the register is not found. */
1448 get_hard_regnum (regstack
, reg
)
1454 if (! STACK_REG_P (reg
))
1457 for (i
= regstack
->top
; i
>= 0; i
--)
1458 if (regstack
->reg
[i
] == REGNO (reg
))
1461 return i
>= 0 ? (FIRST_STACK_REG
+ regstack
->top
- i
) : -1;
1464 /* Delete INSN from the RTL. Mark the insn, but don't remove it from
1465 the chain of insns. Doing so could confuse block_begin and block_end
1466 if this were the only insn in the block. */
1469 delete_insn_for_stacker (insn
)
1472 PUT_CODE (insn
, NOTE
);
1473 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
1474 NOTE_SOURCE_FILE (insn
) = 0;
1477 /* Emit an insn to pop virtual register REG before or after INSN.
1478 REGSTACK is the stack state after INSN and is updated to reflect this
1479 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
1480 is represented as a SET whose destination is the register to be popped
1481 and source is the top of stack. A death note for the top of stack
1482 cases the movdf pattern to pop. */
1485 emit_pop_insn (insn
, regstack
, reg
, when
)
1491 rtx pop_insn
, pop_rtx
;
1494 hard_regno
= get_hard_regnum (regstack
, reg
);
1496 if (hard_regno
< FIRST_STACK_REG
)
1499 pop_rtx
= gen_rtx_SET (VOIDmode
, FP_MODE_REG (hard_regno
, DFmode
),
1500 FP_MODE_REG (FIRST_STACK_REG
, DFmode
));
1502 pop_insn
= (*when
) (pop_rtx
, insn
);
1504 REG_NOTES (pop_insn
)
1505 = gen_rtx_EXPR_LIST (REG_DEAD
, FP_MODE_REG (FIRST_STACK_REG
, DFmode
),
1506 REG_NOTES (pop_insn
));
1508 regstack
->reg
[regstack
->top
- (hard_regno
- FIRST_STACK_REG
)]
1509 = regstack
->reg
[regstack
->top
];
1511 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (reg
));
1516 /* Emit an insn before or after INSN to swap virtual register REG with the
1517 top of stack. WHEN should be `emit_insn_before' or `emit_insn_before'
1518 REGSTACK is the stack state before the swap, and is updated to reflect
1519 the swap. A swap insn is represented as a PARALLEL of two patterns:
1520 each pattern moves one reg to the other.
1522 If REG is already at the top of the stack, no insn is emitted. */
1525 emit_swap_insn (insn
, regstack
, reg
)
1531 rtx swap_rtx
, swap_insn
;
1532 int tmp
, other_reg
; /* swap regno temps */
1533 rtx i1
; /* the stack-reg insn prior to INSN */
1534 rtx i1set
= NULL_RTX
; /* the SET rtx within I1 */
1536 hard_regno
= get_hard_regnum (regstack
, reg
);
1538 if (hard_regno
< FIRST_STACK_REG
)
1540 if (hard_regno
== FIRST_STACK_REG
)
1543 other_reg
= regstack
->top
- (hard_regno
- FIRST_STACK_REG
);
1545 tmp
= regstack
->reg
[other_reg
];
1546 regstack
->reg
[other_reg
] = regstack
->reg
[regstack
->top
];
1547 regstack
->reg
[regstack
->top
] = tmp
;
1549 /* Find the previous insn involving stack regs, but don't go past
1550 any labels, calls or jumps. */
1551 i1
= prev_nonnote_insn (insn
);
1552 while (i1
&& GET_CODE (i1
) == INSN
&& !stack_regs_mentioned (i1
))
1553 i1
= prev_nonnote_insn (i1
);
1556 i1set
= single_set (i1
);
1560 rtx i1src
= *get_true_reg (&SET_SRC (i1set
));
1561 rtx i1dest
= *get_true_reg (&SET_DEST (i1set
));
1563 /* If the previous register stack push was from the reg we are to
1564 swap with, omit the swap. */
1566 if (GET_CODE (i1dest
) == REG
&& REGNO (i1dest
) == FIRST_STACK_REG
1567 && GET_CODE (i1src
) == REG
&& REGNO (i1src
) == hard_regno
- 1
1568 && find_regno_note (i1
, REG_DEAD
, FIRST_STACK_REG
) == NULL_RTX
)
1571 /* If the previous insn wrote to the reg we are to swap with,
1574 if (GET_CODE (i1dest
) == REG
&& REGNO (i1dest
) == hard_regno
1575 && GET_CODE (i1src
) == REG
&& REGNO (i1src
) == FIRST_STACK_REG
1576 && find_regno_note (i1
, REG_DEAD
, FIRST_STACK_REG
) == NULL_RTX
)
1580 swap_rtx
= gen_swapxf (FP_MODE_REG (hard_regno
, XFmode
),
1581 FP_MODE_REG (FIRST_STACK_REG
, XFmode
));
1582 swap_insn
= emit_insn_after (swap_rtx
, i1
);
1585 /* Handle a move to or from a stack register in PAT, which is in INSN.
1586 REGSTACK is the current stack. */
1589 move_for_stack_reg (insn
, regstack
, pat
)
1594 rtx
*psrc
= get_true_reg (&SET_SRC (pat
));
1595 rtx
*pdest
= get_true_reg (&SET_DEST (pat
));
1599 src
= *psrc
; dest
= *pdest
;
1601 if (STACK_REG_P (src
) && STACK_REG_P (dest
))
1603 /* Write from one stack reg to another. If SRC dies here, then
1604 just change the register mapping and delete the insn. */
1606 note
= find_regno_note (insn
, REG_DEAD
, REGNO (src
));
1611 /* If this is a no-op move, there must not be a REG_DEAD note. */
1612 if (REGNO (src
) == REGNO (dest
))
1615 for (i
= regstack
->top
; i
>= 0; i
--)
1616 if (regstack
->reg
[i
] == REGNO (src
))
1619 /* The source must be live, and the dest must be dead. */
1620 if (i
< 0 || get_hard_regnum (regstack
, dest
) >= FIRST_STACK_REG
)
1623 /* It is possible that the dest is unused after this insn.
1624 If so, just pop the src. */
1626 if (find_regno_note (insn
, REG_UNUSED
, REGNO (dest
)))
1628 emit_pop_insn (insn
, regstack
, src
, emit_insn_after
);
1630 delete_insn_for_stacker (insn
);
1634 regstack
->reg
[i
] = REGNO (dest
);
1636 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (dest
));
1637 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (src
));
1639 delete_insn_for_stacker (insn
);
1644 /* The source reg does not die. */
1646 /* If this appears to be a no-op move, delete it, or else it
1647 will confuse the machine description output patterns. But if
1648 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1649 for REG_UNUSED will not work for deleted insns. */
1651 if (REGNO (src
) == REGNO (dest
))
1653 if (find_regno_note (insn
, REG_UNUSED
, REGNO (dest
)))
1654 emit_pop_insn (insn
, regstack
, dest
, emit_insn_after
);
1656 delete_insn_for_stacker (insn
);
1660 /* The destination ought to be dead */
1661 if (get_hard_regnum (regstack
, dest
) >= FIRST_STACK_REG
)
1664 replace_reg (psrc
, get_hard_regnum (regstack
, src
));
1666 regstack
->reg
[++regstack
->top
] = REGNO (dest
);
1667 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (dest
));
1668 replace_reg (pdest
, FIRST_STACK_REG
);
1670 else if (STACK_REG_P (src
))
1672 /* Save from a stack reg to MEM, or possibly integer reg. Since
1673 only top of stack may be saved, emit an exchange first if
1676 emit_swap_insn (insn
, regstack
, src
);
1678 note
= find_regno_note (insn
, REG_DEAD
, REGNO (src
));
1681 replace_reg (&XEXP (note
, 0), FIRST_STACK_REG
);
1683 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (src
));
1685 else if (GET_MODE (src
) == XFmode
&& regstack
->top
< REG_STACK_SIZE
- 1)
1687 /* A 387 cannot write an XFmode value to a MEM without
1688 clobbering the source reg. The output code can handle
1689 this by reading back the value from the MEM.
1690 But it is more efficient to use a temp register if one is
1691 available. Push the source value here if the register
1692 stack is not full, and then write the value to memory via
1694 rtx push_rtx
, push_insn
;
1695 rtx top_stack_reg
= FP_MODE_REG (FIRST_STACK_REG
, XFmode
);
1697 push_rtx
= gen_movxf (top_stack_reg
, top_stack_reg
);
1698 push_insn
= emit_insn_before (push_rtx
, insn
);
1699 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_DEAD
, top_stack_reg
,
1703 replace_reg (psrc
, FIRST_STACK_REG
);
1705 else if (STACK_REG_P (dest
))
1707 /* Load from MEM, or possibly integer REG or constant, into the
1708 stack regs. The actual target is always the top of the
1709 stack. The stack mapping is changed to reflect that DEST is
1710 now at top of stack. */
1712 /* The destination ought to be dead */
1713 if (get_hard_regnum (regstack
, dest
) >= FIRST_STACK_REG
)
1716 if (regstack
->top
>= REG_STACK_SIZE
)
1719 regstack
->reg
[++regstack
->top
] = REGNO (dest
);
1720 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (dest
));
1721 replace_reg (pdest
, FIRST_STACK_REG
);
1727 /* Swap the condition on a branch, if there is one. Return true if we
1728 found a condition to swap. False if the condition was not used as
1732 swap_rtx_condition_1 (pat
)
1735 register const char *fmt
;
1736 register int i
, r
= 0;
1738 if (GET_RTX_CLASS (GET_CODE (pat
)) == '<')
1740 PUT_CODE (pat
, swap_condition (GET_CODE (pat
)));
1745 fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
1746 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
1752 for (j
= XVECLEN (pat
, i
) - 1; j
>= 0; j
--)
1753 r
|= swap_rtx_condition_1 (XVECEXP (pat
, i
, j
));
1755 else if (fmt
[i
] == 'e')
1756 r
|= swap_rtx_condition_1 (XEXP (pat
, i
));
1764 swap_rtx_condition (insn
)
1767 rtx pat
= PATTERN (insn
);
1769 /* We're looking for a single set to cc0 or an HImode temporary. */
1771 if (GET_CODE (pat
) == SET
1772 && GET_CODE (SET_DEST (pat
)) == REG
1773 && REGNO (SET_DEST (pat
)) == FLAGS_REG
)
1775 insn
= next_flags_user (insn
);
1776 if (insn
== NULL_RTX
)
1778 pat
= PATTERN (insn
);
1781 /* See if this is, or ends in, a fnstsw, aka unspec 9. If so, we're
1782 not doing anything with the cc value right now. We may be able to
1783 search for one though. */
1785 if (GET_CODE (pat
) == SET
1786 && GET_CODE (SET_SRC (pat
)) == UNSPEC
1787 && XINT (SET_SRC (pat
), 1) == 9)
1789 rtx dest
= SET_DEST (pat
);
1791 /* Search forward looking for the first use of this value.
1792 Stop at block boundaries. */
1793 /* ??? This really cries for BLOCK_END! */
1796 insn
= NEXT_INSN (insn
);
1797 if (insn
== NULL_RTX
)
1799 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i'
1800 && reg_mentioned_p (dest
, insn
))
1802 if (GET_CODE (insn
) == JUMP_INSN
)
1804 if (GET_CODE (insn
) == CODE_LABEL
)
1808 /* So we've found the insn using this value. If it is anything
1809 other than sahf, aka unspec 10, or the value does not die
1810 (meaning we'd have to search further), then we must give up. */
1811 pat
= PATTERN (insn
);
1812 if (GET_CODE (pat
) != SET
1813 || GET_CODE (SET_SRC (pat
)) != UNSPEC
1814 || XINT (SET_SRC (pat
), 1) != 10
1815 || ! dead_or_set_p (insn
, dest
))
1818 /* Now we are prepared to handle this as a normal cc0 setter. */
1819 insn
= next_flags_user (insn
);
1820 if (insn
== NULL_RTX
)
1822 pat
= PATTERN (insn
);
1825 return swap_rtx_condition_1 (pat
);
1828 /* Handle a comparison. Special care needs to be taken to avoid
1829 causing comparisons that a 387 cannot do correctly, such as EQ.
1831 Also, a pop insn may need to be emitted. The 387 does have an
1832 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1833 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1837 compare_for_stack_reg (insn
, regstack
, pat_src
)
1843 rtx src1_note
, src2_note
;
1846 src1
= get_true_reg (&XEXP (pat_src
, 0));
1847 src2
= get_true_reg (&XEXP (pat_src
, 1));
1848 flags_user
= next_flags_user (insn
);
1850 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1851 registers that die in this insn - move those to stack top first. */
1852 if ((! STACK_REG_P (*src1
)
1853 || (STACK_REG_P (*src2
)
1854 && get_hard_regnum (regstack
, *src2
) == FIRST_STACK_REG
))
1855 && swap_rtx_condition (insn
))
1858 temp
= XEXP (pat_src
, 0);
1859 XEXP (pat_src
, 0) = XEXP (pat_src
, 1);
1860 XEXP (pat_src
, 1) = temp
;
1862 src1
= get_true_reg (&XEXP (pat_src
, 0));
1863 src2
= get_true_reg (&XEXP (pat_src
, 1));
1865 INSN_CODE (insn
) = -1;
1868 /* We will fix any death note later. */
1870 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1872 if (STACK_REG_P (*src2
))
1873 src2_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src2
));
1875 src2_note
= NULL_RTX
;
1877 emit_swap_insn (insn
, regstack
, *src1
);
1879 replace_reg (src1
, FIRST_STACK_REG
);
1881 if (STACK_REG_P (*src2
))
1882 replace_reg (src2
, get_hard_regnum (regstack
, *src2
));
1886 pop_stack (regstack
, REGNO (XEXP (src1_note
, 0)));
1887 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
1890 /* If the second operand dies, handle that. But if the operands are
1891 the same stack register, don't bother, because only one death is
1892 needed, and it was just handled. */
1895 && ! (STACK_REG_P (*src1
) && STACK_REG_P (*src2
)
1896 && REGNO (*src1
) == REGNO (*src2
)))
1898 /* As a special case, two regs may die in this insn if src2 is
1899 next to top of stack and the top of stack also dies. Since
1900 we have already popped src1, "next to top of stack" is really
1901 at top (FIRST_STACK_REG) now. */
1903 if (get_hard_regnum (regstack
, XEXP (src2_note
, 0)) == FIRST_STACK_REG
1906 pop_stack (regstack
, REGNO (XEXP (src2_note
, 0)));
1907 replace_reg (&XEXP (src2_note
, 0), FIRST_STACK_REG
+ 1);
1911 /* The 386 can only represent death of the first operand in
1912 the case handled above. In all other cases, emit a separate
1913 pop and remove the death note from here. */
1915 /* link_cc0_insns (insn); */
1917 remove_regno_note (insn
, REG_DEAD
, REGNO (XEXP (src2_note
, 0)));
1919 emit_pop_insn (insn
, regstack
, XEXP (src2_note
, 0),
1925 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1926 is the current register layout. */
1929 subst_stack_regs_pat (insn
, regstack
, pat
)
1935 rtx
*src1
= (rtx
*) NULL_PTR
, *src2
;
1936 rtx src1_note
, src2_note
;
1939 if (GET_CODE (pat
) != SET
)
1942 dest
= get_true_reg (&SET_DEST (pat
));
1943 src
= get_true_reg (&SET_SRC (pat
));
1944 pat_src
= SET_SRC (pat
);
1946 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1948 if (*dest
!= cc0_rtx
1949 && (STACK_REG_P (*src
)
1950 || (STACK_REG_P (*dest
)
1951 && (GET_CODE (*src
) == REG
|| GET_CODE (*src
) == MEM
1952 || GET_CODE (*src
) == CONST_DOUBLE
))))
1953 move_for_stack_reg (insn
, regstack
, pat
);
1955 switch (GET_CODE (pat_src
))
1958 compare_for_stack_reg (insn
, regstack
, pat_src
);
1964 for (count
= HARD_REGNO_NREGS (REGNO (*dest
), GET_MODE (*dest
));
1967 regstack
->reg
[++regstack
->top
] = REGNO (*dest
) + count
;
1968 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
) + count
);
1971 replace_reg (dest
, FIRST_STACK_REG
);
1975 /* This is a `tstM2' case. */
1976 if (*dest
!= cc0_rtx
)
1983 case FLOAT_TRUNCATE
:
1987 /* These insns only operate on the top of the stack. DEST might
1988 be cc0_rtx if we're processing a tstM pattern. Also, it's
1989 possible that the tstM case results in a REG_DEAD note on the
1993 src1
= get_true_reg (&XEXP (pat_src
, 0));
1995 emit_swap_insn (insn
, regstack
, *src1
);
1997 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
1999 if (STACK_REG_P (*dest
))
2000 replace_reg (dest
, FIRST_STACK_REG
);
2004 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
2006 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (*src1
));
2009 replace_reg (src1
, FIRST_STACK_REG
);
2015 /* On i386, reversed forms of subM3 and divM3 exist for
2016 MODE_FLOAT, so the same code that works for addM3 and mulM3
2020 /* These insns can accept the top of stack as a destination
2021 from a stack reg or mem, or can use the top of stack as a
2022 source and some other stack register (possibly top of stack)
2023 as a destination. */
2025 src1
= get_true_reg (&XEXP (pat_src
, 0));
2026 src2
= get_true_reg (&XEXP (pat_src
, 1));
2028 /* We will fix any death note later. */
2030 if (STACK_REG_P (*src1
))
2031 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
2033 src1_note
= NULL_RTX
;
2034 if (STACK_REG_P (*src2
))
2035 src2_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src2
));
2037 src2_note
= NULL_RTX
;
2039 /* If either operand is not a stack register, then the dest
2040 must be top of stack. */
2042 if (! STACK_REG_P (*src1
) || ! STACK_REG_P (*src2
))
2043 emit_swap_insn (insn
, regstack
, *dest
);
2046 /* Both operands are REG. If neither operand is already
2047 at the top of stack, choose to make the one that is the dest
2048 the new top of stack. */
2050 int src1_hard_regnum
, src2_hard_regnum
;
2052 src1_hard_regnum
= get_hard_regnum (regstack
, *src1
);
2053 src2_hard_regnum
= get_hard_regnum (regstack
, *src2
);
2054 if (src1_hard_regnum
== -1 || src2_hard_regnum
== -1)
2057 if (src1_hard_regnum
!= FIRST_STACK_REG
2058 && src2_hard_regnum
!= FIRST_STACK_REG
)
2059 emit_swap_insn (insn
, regstack
, *dest
);
2062 if (STACK_REG_P (*src1
))
2063 replace_reg (src1
, get_hard_regnum (regstack
, *src1
));
2064 if (STACK_REG_P (*src2
))
2065 replace_reg (src2
, get_hard_regnum (regstack
, *src2
));
2069 /* If the register that dies is at the top of stack, then
2070 the destination is somewhere else - merely substitute it.
2071 But if the reg that dies is not at top of stack, then
2072 move the top of stack to the dead reg, as though we had
2073 done the insn and then a store-with-pop. */
2075 if (REGNO (XEXP (src1_note
, 0)) == regstack
->reg
[regstack
->top
])
2077 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
2078 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
2082 int regno
= get_hard_regnum (regstack
, XEXP (src1_note
, 0));
2084 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
2085 replace_reg (dest
, regno
);
2087 regstack
->reg
[regstack
->top
- (regno
- FIRST_STACK_REG
)]
2088 = regstack
->reg
[regstack
->top
];
2091 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
2092 REGNO (XEXP (src1_note
, 0)));
2093 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
2098 if (REGNO (XEXP (src2_note
, 0)) == regstack
->reg
[regstack
->top
])
2100 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
2101 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
2105 int regno
= get_hard_regnum (regstack
, XEXP (src2_note
, 0));
2107 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
2108 replace_reg (dest
, regno
);
2110 regstack
->reg
[regstack
->top
- (regno
- FIRST_STACK_REG
)]
2111 = regstack
->reg
[regstack
->top
];
2114 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
2115 REGNO (XEXP (src2_note
, 0)));
2116 replace_reg (&XEXP (src2_note
, 0), FIRST_STACK_REG
);
2121 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
2122 replace_reg (dest
, get_hard_regnum (regstack
, *dest
));
2128 switch (XINT (pat_src
, 1))
2132 /* These insns only operate on the top of the stack. */
2134 src1
= get_true_reg (&XVECEXP (pat_src
, 0, 0));
2136 emit_swap_insn (insn
, regstack
, *src1
);
2138 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
2140 if (STACK_REG_P (*dest
))
2141 replace_reg (dest
, FIRST_STACK_REG
);
2145 replace_reg (&XEXP (src1_note
, 0), FIRST_STACK_REG
);
2147 CLEAR_HARD_REG_BIT (regstack
->reg_set
, REGNO (*src1
));
2150 replace_reg (src1
, FIRST_STACK_REG
);
2155 /* (unspec [(unspec [(compare ..)] 9)] 10)
2156 Unspec 9 is fnstsw; unspec 10 is sahf. The combination
2157 matches the PPRO fcomi instruction. */
2159 pat_src
= XVECEXP (pat_src
, 0, 0);
2160 if (GET_CODE (pat_src
) != UNSPEC
2161 || XINT (pat_src
, 1) != 9)
2166 /* (unspec [(compare ..)] 9)
2167 Combined fcomp+fnstsw generated for doing well with CSE.
2168 When optimizing this would have been broken up before now. */
2170 pat_src
= XVECEXP (pat_src
, 0, 0);
2171 if (GET_CODE (pat_src
) != COMPARE
)
2174 compare_for_stack_reg (insn
, regstack
, pat_src
);
2183 /* This insn requires the top of stack to be the destination. */
2185 /* If the comparison operator is an FP comparison operator,
2186 it is handled correctly by compare_for_stack_reg () who
2187 will move the destination to the top of stack. But if the
2188 comparison operator is not an FP comparison operator, we
2189 have to handle it here. */
2190 if (get_hard_regnum (regstack
, *dest
) >= FIRST_STACK_REG
2191 && REGNO (*dest
) != regstack
->reg
[regstack
->top
])
2192 emit_swap_insn (insn
, regstack
, *dest
);
2194 src1
= get_true_reg (&XEXP (pat_src
, 1));
2195 src2
= get_true_reg (&XEXP (pat_src
, 2));
2197 src1_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src1
));
2198 src2_note
= find_regno_note (insn
, REG_DEAD
, REGNO (*src2
));
2205 src_note
[1] = src1_note
;
2206 src_note
[2] = src2_note
;
2208 if (STACK_REG_P (*src1
))
2209 replace_reg (src1
, get_hard_regnum (regstack
, *src1
));
2210 if (STACK_REG_P (*src2
))
2211 replace_reg (src2
, get_hard_regnum (regstack
, *src2
));
2213 for (i
= 1; i
<= 2; i
++)
2216 /* If the register that dies is not at the top of stack, then
2217 move the top of stack to the dead reg */
2218 if (REGNO (XEXP (src_note
[i
], 0))
2219 != regstack
->reg
[regstack
->top
])
2221 remove_regno_note (insn
, REG_DEAD
,
2222 REGNO (XEXP (src_note
[i
], 0)));
2223 emit_pop_insn (insn
, regstack
, XEXP (src_note
[i
], 0),
2228 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
2229 REGNO (XEXP (src_note
[i
], 0)));
2230 replace_reg (&XEXP (src_note
[i
], 0), FIRST_STACK_REG
);
2236 /* Make dest the top of stack. Add dest to regstack if not present. */
2237 if (get_hard_regnum (regstack
, *dest
) < FIRST_STACK_REG
)
2238 regstack
->reg
[++regstack
->top
] = REGNO (*dest
);
2239 SET_HARD_REG_BIT (regstack
->reg_set
, REGNO (*dest
));
2240 replace_reg (dest
, FIRST_STACK_REG
);
2249 /* Substitute hard regnums for any stack regs in INSN, which has
2250 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
2251 before the insn, and is updated with changes made here.
2253 There are several requirements and assumptions about the use of
2254 stack-like regs in asm statements. These rules are enforced by
2255 record_asm_stack_regs; see comments there for details. Any
2256 asm_operands left in the RTL at this point may be assume to meet the
2257 requirements, since record_asm_stack_regs removes any problem asm. */
2260 subst_asm_stack_regs (insn
, regstack
)
2264 rtx body
= PATTERN (insn
);
2267 rtx
*note_reg
; /* Array of note contents */
2268 rtx
**note_loc
; /* Address of REG field of each note */
2269 enum reg_note
*note_kind
; /* The type of each note */
2274 struct stack_def temp_stack
;
2279 int n_inputs
, n_outputs
;
2281 /* Find out what the constraints required. If no constraint
2282 alternative matches, that is a compiler bug: we should have caught
2283 such an insn during the life analysis pass (and reload should have
2284 caught it regardless). */
2285 extract_insn (insn
);
2286 constrain_operands (1);
2287 alt
= which_alternative
;
2289 preprocess_constraints ();
2291 n_inputs
= get_asm_operand_n_inputs (body
);
2292 n_outputs
= recog_data
.n_operands
- n_inputs
;
2297 /* Strip SUBREGs here to make the following code simpler. */
2298 for (i
= 0; i
< recog_data
.n_operands
; i
++)
2299 if (GET_CODE (recog_data
.operand
[i
]) == SUBREG
2300 && GET_CODE (SUBREG_REG (recog_data
.operand
[i
])) == REG
)
2302 recog_data
.operand_loc
[i
] = & SUBREG_REG (recog_data
.operand
[i
]);
2303 recog_data
.operand
[i
] = SUBREG_REG (recog_data
.operand
[i
]);
2306 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
2308 for (i
= 0, note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
2311 note_reg
= (rtx
*) alloca (i
* sizeof (rtx
));
2312 note_loc
= (rtx
**) alloca (i
* sizeof (rtx
*));
2313 note_kind
= (enum reg_note
*) alloca (i
* sizeof (enum reg_note
));
2316 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
2318 rtx reg
= XEXP (note
, 0);
2319 rtx
*loc
= & XEXP (note
, 0);
2321 if (GET_CODE (reg
) == SUBREG
&& GET_CODE (SUBREG_REG (reg
)) == REG
)
2323 loc
= & SUBREG_REG (reg
);
2324 reg
= SUBREG_REG (reg
);
2327 if (STACK_REG_P (reg
)
2328 && (REG_NOTE_KIND (note
) == REG_DEAD
2329 || REG_NOTE_KIND (note
) == REG_UNUSED
))
2331 note_reg
[n_notes
] = reg
;
2332 note_loc
[n_notes
] = loc
;
2333 note_kind
[n_notes
] = REG_NOTE_KIND (note
);
2338 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2342 if (GET_CODE (body
) == PARALLEL
)
2344 clobber_reg
= (rtx
*) alloca (XVECLEN (body
, 0) * sizeof (rtx
));
2345 clobber_loc
= (rtx
**) alloca (XVECLEN (body
, 0) * sizeof (rtx
*));
2347 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2348 if (GET_CODE (XVECEXP (body
, 0, i
)) == CLOBBER
)
2350 rtx clobber
= XVECEXP (body
, 0, i
);
2351 rtx reg
= XEXP (clobber
, 0);
2352 rtx
*loc
= & XEXP (clobber
, 0);
2354 if (GET_CODE (reg
) == SUBREG
&& GET_CODE (SUBREG_REG (reg
)) == REG
)
2356 loc
= & SUBREG_REG (reg
);
2357 reg
= SUBREG_REG (reg
);
2360 if (STACK_REG_P (reg
))
2362 clobber_reg
[n_clobbers
] = reg
;
2363 clobber_loc
[n_clobbers
] = loc
;
2369 bcopy ((char *) regstack
, (char *) &temp_stack
, sizeof (temp_stack
));
2371 /* Put the input regs into the desired place in TEMP_STACK. */
2373 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
2374 if (STACK_REG_P (recog_data
.operand
[i
])
2375 && reg_class_subset_p (recog_op_alt
[i
][alt
].class,
2377 && recog_op_alt
[i
][alt
].class != FLOAT_REGS
)
2379 /* If an operand needs to be in a particular reg in
2380 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2381 these constraints are for single register classes, and
2382 reload guaranteed that operand[i] is already in that class,
2383 we can just use REGNO (recog_data.operand[i]) to know which
2384 actual reg this operand needs to be in. */
2386 int regno
= get_hard_regnum (&temp_stack
, recog_data
.operand
[i
]);
2391 if (regno
!= REGNO (recog_data
.operand
[i
]))
2393 /* recog_data.operand[i] is not in the right place. Find
2394 it and swap it with whatever is already in I's place.
2395 K is where recog_data.operand[i] is now. J is where it
2399 k
= temp_stack
.top
- (regno
- FIRST_STACK_REG
);
2401 - (REGNO (recog_data
.operand
[i
]) - FIRST_STACK_REG
));
2403 temp
= temp_stack
.reg
[k
];
2404 temp_stack
.reg
[k
] = temp_stack
.reg
[j
];
2405 temp_stack
.reg
[j
] = temp
;
2409 /* emit insns before INSN to make sure the reg-stack is in the right
2412 change_stack (insn
, regstack
, &temp_stack
, emit_insn_before
);
2414 /* Make the needed input register substitutions. Do death notes and
2415 clobbers too, because these are for inputs, not outputs. */
2417 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
2418 if (STACK_REG_P (recog_data
.operand
[i
]))
2420 int regnum
= get_hard_regnum (regstack
, recog_data
.operand
[i
]);
2425 replace_reg (recog_data
.operand_loc
[i
], regnum
);
2428 for (i
= 0; i
< n_notes
; i
++)
2429 if (note_kind
[i
] == REG_DEAD
)
2431 int regnum
= get_hard_regnum (regstack
, note_reg
[i
]);
2436 replace_reg (note_loc
[i
], regnum
);
2439 for (i
= 0; i
< n_clobbers
; i
++)
2441 /* It's OK for a CLOBBER to reference a reg that is not live.
2442 Don't try to replace it in that case. */
2443 int regnum
= get_hard_regnum (regstack
, clobber_reg
[i
]);
2447 /* Sigh - clobbers always have QImode. But replace_reg knows
2448 that these regs can't be MODE_INT and will abort. Just put
2449 the right reg there without calling replace_reg. */
2451 *clobber_loc
[i
] = FP_MODE_REG (regnum
, DFmode
);
2455 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2457 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
2458 if (STACK_REG_P (recog_data
.operand
[i
]))
2460 /* An input reg is implicitly popped if it is tied to an
2461 output, or if there is a CLOBBER for it. */
2464 for (j
= 0; j
< n_clobbers
; j
++)
2465 if (operands_match_p (clobber_reg
[j
], recog_data
.operand
[i
]))
2468 if (j
< n_clobbers
|| recog_op_alt
[i
][alt
].matches
>= 0)
2470 /* recog_data.operand[i] might not be at the top of stack.
2471 But that's OK, because all we need to do is pop the
2472 right number of regs off of the top of the reg-stack.
2473 record_asm_stack_regs guaranteed that all implicitly
2474 popped regs were grouped at the top of the reg-stack. */
2476 CLEAR_HARD_REG_BIT (regstack
->reg_set
,
2477 regstack
->reg
[regstack
->top
]);
2482 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2483 Note that there isn't any need to substitute register numbers.
2484 ??? Explain why this is true. */
2486 for (i
= LAST_STACK_REG
; i
>= FIRST_STACK_REG
; i
--)
2488 /* See if there is an output for this hard reg. */
2491 for (j
= 0; j
< n_outputs
; j
++)
2492 if (STACK_REG_P (recog_data
.operand
[j
])
2493 && REGNO (recog_data
.operand
[j
]) == i
)
2495 regstack
->reg
[++regstack
->top
] = i
;
2496 SET_HARD_REG_BIT (regstack
->reg_set
, i
);
2501 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2502 input that the asm didn't implicitly pop. If the asm didn't
2503 implicitly pop an input reg, that reg will still be live.
2505 Note that we can't use find_regno_note here: the register numbers
2506 in the death notes have already been substituted. */
2508 for (i
= 0; i
< n_outputs
; i
++)
2509 if (STACK_REG_P (recog_data
.operand
[i
]))
2513 for (j
= 0; j
< n_notes
; j
++)
2514 if (REGNO (recog_data
.operand
[i
]) == REGNO (note_reg
[j
])
2515 && note_kind
[j
] == REG_UNUSED
)
2517 insn
= emit_pop_insn (insn
, regstack
, recog_data
.operand
[i
],
2523 for (i
= n_outputs
; i
< n_outputs
+ n_inputs
; i
++)
2524 if (STACK_REG_P (recog_data
.operand
[i
]))
2528 for (j
= 0; j
< n_notes
; j
++)
2529 if (REGNO (recog_data
.operand
[i
]) == REGNO (note_reg
[j
])
2530 && note_kind
[j
] == REG_DEAD
2531 && TEST_HARD_REG_BIT (regstack
->reg_set
,
2532 REGNO (recog_data
.operand
[i
])))
2534 insn
= emit_pop_insn (insn
, regstack
, recog_data
.operand
[i
],
2541 /* Substitute stack hard reg numbers for stack virtual registers in
2542 INSN. Non-stack register numbers are not changed. REGSTACK is the
2543 current stack content. Insns may be emitted as needed to arrange the
2544 stack for the 387 based on the contents of the insn. */
2547 subst_stack_regs (insn
, regstack
)
2551 register rtx
*note_link
, note
;
2554 if (GET_CODE (insn
) == CALL_INSN
)
2556 int top
= regstack
->top
;
2558 /* If there are any floating point parameters to be passed in
2559 registers for this call, make sure they are in the right
2564 straighten_stack (PREV_INSN (insn
), regstack
);
2566 /* Now mark the arguments as dead after the call. */
2568 while (regstack
->top
>= 0)
2570 CLEAR_HARD_REG_BIT (regstack
->reg_set
, FIRST_STACK_REG
+ regstack
->top
);
2576 /* Do the actual substitution if any stack regs are mentioned.
2577 Since we only record whether entire insn mentions stack regs, and
2578 subst_stack_regs_pat only works for patterns that contain stack regs,
2579 we must check each pattern in a parallel here. A call_value_pop could
2582 if (stack_regs_mentioned (insn
))
2584 int n_operands
= asm_noperands (PATTERN (insn
));
2585 if (n_operands
>= 0)
2587 /* This insn is an `asm' with operands. Decode the operands,
2588 decide how many are inputs, and do register substitution.
2589 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2591 subst_asm_stack_regs (insn
, regstack
);
2595 if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
2596 for (i
= 0; i
< XVECLEN (PATTERN (insn
), 0); i
++)
2598 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn
), 0, i
)))
2599 subst_stack_regs_pat (insn
, regstack
,
2600 XVECEXP (PATTERN (insn
), 0, i
));
2603 subst_stack_regs_pat (insn
, regstack
, PATTERN (insn
));
2606 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2607 REG_UNUSED will already have been dealt with, so just return. */
2609 if (GET_CODE (insn
) == NOTE
)
2612 /* If there is a REG_UNUSED note on a stack register on this insn,
2613 the indicated reg must be popped. The REG_UNUSED note is removed,
2614 since the form of the newly emitted pop insn references the reg,
2615 making it no longer `unset'. */
2617 note_link
= ®_NOTES(insn
);
2618 for (note
= *note_link
; note
; note
= XEXP (note
, 1))
2619 if (REG_NOTE_KIND (note
) == REG_UNUSED
&& STACK_REG_P (XEXP (note
, 0)))
2621 *note_link
= XEXP (note
, 1);
2622 insn
= emit_pop_insn (insn
, regstack
, XEXP (note
, 0), emit_insn_after
);
2625 note_link
= &XEXP (note
, 1);
2628 /* Change the organization of the stack so that it fits a new basic
2629 block. Some registers might have to be popped, but there can never be
2630 a register live in the new block that is not now live.
2632 Insert any needed insns before or after INSN. WHEN is emit_insn_before
2633 or emit_insn_after. OLD is the original stack layout, and NEW is
2634 the desired form. OLD is updated to reflect the code emitted, ie, it
2635 will be the same as NEW upon return.
2637 This function will not preserve block_end[]. But that information
2638 is no longer needed once this has executed. */
2641 change_stack (insn
, old
, new, when
)
2649 /* We will be inserting new insns "backwards", by calling emit_insn_before.
2650 If we are to insert after INSN, find the next insn, and insert before
2653 if (when
== emit_insn_after
)
2654 insn
= NEXT_INSN (insn
);
2656 /* Pop any registers that are not needed in the new block. */
2658 for (reg
= old
->top
; reg
>= 0; reg
--)
2659 if (! TEST_HARD_REG_BIT (new->reg_set
, old
->reg
[reg
]))
2660 emit_pop_insn (insn
, old
, FP_MODE_REG (old
->reg
[reg
], DFmode
),
2665 /* If the new block has never been processed, then it can inherit
2666 the old stack order. */
2668 new->top
= old
->top
;
2669 bcopy (old
->reg
, new->reg
, sizeof (new->reg
));
2673 /* This block has been entered before, and we must match the
2674 previously selected stack order. */
2676 /* By now, the only difference should be the order of the stack,
2677 not their depth or liveliness. */
2679 GO_IF_HARD_REG_EQUAL (old
->reg_set
, new->reg_set
, win
);
2685 if (old
->top
!= new->top
)
2688 /* If the stack is not empty (new->top != -1), loop here emitting
2689 swaps until the stack is correct.
2691 The worst case number of swaps emitted is N + 2, where N is the
2692 depth of the stack. In some cases, the reg at the top of
2693 stack may be correct, but swapped anyway in order to fix
2694 other regs. But since we never swap any other reg away from
2695 its correct slot, this algorithm will converge. */
2700 /* Swap the reg at top of stack into the position it is
2701 supposed to be in, until the correct top of stack appears. */
2703 while (old
->reg
[old
->top
] != new->reg
[new->top
])
2705 for (reg
= new->top
; reg
>= 0; reg
--)
2706 if (new->reg
[reg
] == old
->reg
[old
->top
])
2712 emit_swap_insn (insn
, old
,
2713 FP_MODE_REG (old
->reg
[reg
], DFmode
));
2716 /* See if any regs remain incorrect. If so, bring an
2717 incorrect reg to the top of stack, and let the while loop
2720 for (reg
= new->top
; reg
>= 0; reg
--)
2721 if (new->reg
[reg
] != old
->reg
[reg
])
2723 emit_swap_insn (insn
, old
,
2724 FP_MODE_REG (old
->reg
[reg
], DFmode
));
2729 /* At this point there must be no differences. */
2731 for (reg
= old
->top
; reg
>= 0; reg
--)
2732 if (old
->reg
[reg
] != new->reg
[reg
])
2737 /* Check PAT, which points to RTL in INSN, for a LABEL_REF. If it is
2738 found, ensure that a jump from INSN to the code_label to which the
2739 label_ref points ends up with the same stack as that at the
2740 code_label. Do this by inserting insns just before the code_label to
2741 pop and rotate the stack until it is in the correct order. REGSTACK
2742 is the order of the register stack in INSN.
2744 Any code that is emitted here must not be later processed as part
2745 of any block, as it will already contain hard register numbers. */
2748 goto_block_pat (insn
, regstack
, pat
)
2754 rtx new_jump
, new_label
, new_barrier
;
2757 struct stack_def temp_stack
;
2760 switch (GET_CODE (pat
))
2763 straighten_stack (PREV_INSN (insn
), regstack
);
2768 const char *fmt
= GET_RTX_FORMAT (GET_CODE (pat
));
2770 for (i
= GET_RTX_LENGTH (GET_CODE (pat
)) - 1; i
>= 0; i
--)
2773 goto_block_pat (insn
, regstack
, XEXP (pat
, i
));
2775 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
2776 goto_block_pat (insn
, regstack
, XVECEXP (pat
, i
, j
));
2783 label
= XEXP (pat
, 0);
2784 if (GET_CODE (label
) != CODE_LABEL
)
2787 /* First, see if in fact anything needs to be done to the stack at all. */
2788 if (INSN_UID (label
) <= 0)
2791 label_stack
= &block_stack_in
[BLOCK_NUM (label
)];
2793 if (label_stack
->top
== -2)
2795 /* If the target block hasn't had a stack order selected, then
2796 we need merely ensure that no pops are needed. */
2798 for (reg
= regstack
->top
; reg
>= 0; reg
--)
2799 if (! TEST_HARD_REG_BIT (label_stack
->reg_set
, regstack
->reg
[reg
]))
2804 /* change_stack will not emit any code in this case. */
2806 change_stack (label
, regstack
, label_stack
, emit_insn_after
);
2810 else if (label_stack
->top
== regstack
->top
)
2812 for (reg
= label_stack
->top
; reg
>= 0; reg
--)
2813 if (label_stack
->reg
[reg
] != regstack
->reg
[reg
])
2820 /* At least one insn will need to be inserted before label. Insert
2821 a jump around the code we are about to emit. Emit a label for the new
2822 code, and point the original insn at this new label. We can't use
2823 redirect_jump here, because we're using fld[4] of the code labels as
2824 LABEL_REF chains, no NUSES counters. */
2826 new_jump
= emit_jump_insn_before (gen_jump (label
), label
);
2827 record_label_references (new_jump
, PATTERN (new_jump
));
2828 JUMP_LABEL (new_jump
) = label
;
2830 new_barrier
= emit_barrier_after (new_jump
);
2832 new_label
= gen_label_rtx ();
2833 emit_label_after (new_label
, new_barrier
);
2834 LABEL_REFS (new_label
) = new_label
;
2836 /* The old label_ref will no longer point to the code_label if now uses,
2837 so strip the label_ref from the code_label's chain of references. */
2839 for (ref
= &LABEL_REFS (label
); *ref
!= label
; ref
= &LABEL_NEXTREF (*ref
))
2846 *ref
= LABEL_NEXTREF (*ref
);
2848 XEXP (pat
, 0) = new_label
;
2849 record_label_references (insn
, PATTERN (insn
));
2851 if (JUMP_LABEL (insn
) == label
)
2852 JUMP_LABEL (insn
) = new_label
;
2854 /* Now emit the needed code. */
2856 temp_stack
= *regstack
;
2858 change_stack (new_label
, &temp_stack
, label_stack
, emit_insn_after
);
2861 /* Traverse all basic blocks in a function, converting the register
2862 references in each insn from the "flat" register file that gcc uses, to
2863 the stack-like registers the 387 uses. */
2868 register int block
, reg
;
2869 register rtx insn
, next
;
2870 struct stack_def regstack
;
2872 for (block
= 0; block
< blocks
; block
++)
2874 if (block_stack_in
[block
].top
== -2)
2876 /* This block has not been previously encountered. Choose a
2877 default mapping for any stack regs live on entry */
2879 block_stack_in
[block
].top
= -1;
2881 for (reg
= LAST_STACK_REG
; reg
>= FIRST_STACK_REG
; reg
--)
2882 if (TEST_HARD_REG_BIT (block_stack_in
[block
].reg_set
, reg
))
2883 block_stack_in
[block
].reg
[++block_stack_in
[block
].top
] = reg
;
2886 /* Process all insns in this block. Keep track of `next' here,
2887 so that we don't process any insns emitted while making
2888 substitutions in INSN. */
2890 next
= block_begin
[block
];
2891 regstack
= block_stack_in
[block
];
2895 next
= NEXT_INSN (insn
);
2897 /* Don't bother processing unless there is a stack reg
2898 mentioned or if it's a CALL_INSN (register passing of
2899 floating point values). */
2901 if (stack_regs_mentioned (insn
) || GET_CODE (insn
) == CALL_INSN
)
2902 subst_stack_regs (insn
, ®stack
);
2904 } while (insn
!= block_end
[block
]);
2906 /* For all further actions, INSN needs to be the last insn in
2907 this basic block. If subst_stack_regs inserted additional
2908 instructions after INSN, it is no longer the last one at
2910 next
= PREV_INSN (next
);
2912 /* If subst_stack_regs inserted something after a JUMP_INSN, that
2913 is almost certainly a bug. */
2914 if (GET_CODE (insn
) == JUMP_INSN
&& insn
!= next
)
2918 /* Something failed if the stack life doesn't match. */
2920 GO_IF_HARD_REG_EQUAL (regstack
.reg_set
, block_out_reg_set
[block
], win
);
2926 /* Adjust the stack of this block on exit to match the stack of
2927 the target block, or copy stack information into stack of
2928 jump target if the target block's stack order hasn't been set
2931 if (GET_CODE (insn
) == JUMP_INSN
)
2932 goto_block_pat (insn
, ®stack
, PATTERN (insn
));
2934 /* Likewise handle the case where we fall into the next block. */
2936 if ((block
< blocks
- 1) && block_drops_in
[block
+1])
2937 change_stack (insn
, ®stack
, &block_stack_in
[block
+1],
2941 /* If the last basic block is the end of a loop, and that loop has
2942 regs live at its start, then the last basic block will have regs live
2943 at its end that need to be popped before the function returns. */
2946 int value_reg_low
, value_reg_high
;
2947 value_reg_low
= value_reg_high
= -1;
2950 if ((retvalue
= stack_result (current_function_decl
)))
2952 value_reg_low
= REGNO (retvalue
);
2953 value_reg_high
= value_reg_low
+
2954 HARD_REGNO_NREGS (value_reg_low
, GET_MODE (retvalue
)) - 1;
2958 for (reg
= regstack
.top
; reg
>= 0; reg
--)
2959 if (regstack
.reg
[reg
] < value_reg_low
2960 || regstack
.reg
[reg
] > value_reg_high
)
2961 insn
= emit_pop_insn (insn
, ®stack
,
2962 FP_MODE_REG (regstack
.reg
[reg
], DFmode
),
2965 straighten_stack (insn
, ®stack
);
2968 /* Check expression PAT, which is in INSN, for label references. if
2969 one is found, print the block number of destination to FILE. */
2972 print_blocks (file
, insn
, pat
)
2976 register RTX_CODE code
= GET_CODE (pat
);
2978 register const char *fmt
;
2980 if (code
== LABEL_REF
)
2982 register rtx label
= XEXP (pat
, 0);
2984 if (GET_CODE (label
) != CODE_LABEL
)
2987 fprintf (file
, " %d", BLOCK_NUM (label
));
2992 fmt
= GET_RTX_FORMAT (code
);
2993 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2996 print_blocks (file
, insn
, XEXP (pat
, i
));
3000 for (j
= 0; j
< XVECLEN (pat
, i
); j
++)
3001 print_blocks (file
, insn
, XVECEXP (pat
, i
, j
));
3006 /* Write information about stack registers and stack blocks into FILE.
3007 This is part of making a debugging dump. */
3010 dump_stack_info (file
)
3015 fprintf (file
, "\n%d stack blocks.\n", blocks
);
3016 for (block
= 0; block
< blocks
; block
++)
3018 register rtx head
, jump
, end
;
3021 fprintf (file
, "\nStack block %d: first insn %d, last %d.\n",
3022 block
, INSN_UID (block_begin
[block
]),
3023 INSN_UID (block_end
[block
]));
3025 head
= block_begin
[block
];
3027 fprintf (file
, "Reached from blocks: ");
3028 if (GET_CODE (head
) == CODE_LABEL
)
3029 for (jump
= LABEL_REFS (head
);
3031 jump
= LABEL_NEXTREF (jump
))
3033 register int from_block
= BLOCK_NUM (CONTAINING_INSN (jump
));
3034 fprintf (file
, " %d", from_block
);
3036 if (block_drops_in
[block
])
3037 fprintf (file
, " previous");
3039 fprintf (file
, "\nlive stack registers on block entry: ");
3040 for (regno
= FIRST_STACK_REG
; regno
<= LAST_STACK_REG
; regno
++)
3042 if (TEST_HARD_REG_BIT (block_stack_in
[block
].reg_set
, regno
))
3043 fprintf (file
, "%d ", regno
);
3046 fprintf (file
, "\nlive stack registers on block exit: ");
3047 for (regno
= FIRST_STACK_REG
; regno
<= LAST_STACK_REG
; regno
++)
3049 if (TEST_HARD_REG_BIT (block_out_reg_set
[block
], regno
))
3050 fprintf (file
, "%d ", regno
);
3053 end
= block_end
[block
];
3055 fprintf (file
, "\nJumps to blocks: ");
3056 if (GET_CODE (end
) == JUMP_INSN
)
3057 print_blocks (file
, end
, PATTERN (end
));
3059 if (block
+ 1 < blocks
&& block_drops_in
[block
+1])
3060 fprintf (file
, " next");
3061 else if (block
+ 1 == blocks
3062 || (GET_CODE (end
) == JUMP_INSN
3063 && GET_CODE (PATTERN (end
)) == RETURN
))
3064 fprintf (file
, " return");
3066 fprintf (file
, "\n");
3069 #endif /* STACK_REGS */