regmove.c (optimize_reg_copy_1): Undo Aug 18 change.
[gcc.git] / gcc / reg-stack.c
1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1992, 93-98, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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)
9 any later version.
10
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.
15
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. */
20
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.
23
24 * The form of the input:
25
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.
35
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.
43
44 * The form of the output:
45
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.
51
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.
54
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.
58
59 A store insn is a SET whose SET_DEST is FIRST_STACK_REG, and whose
60 SET_SRC is REG or MEM.
61
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.
65
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
69 stack.
70
71 * Methodology:
72
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.
76
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
82 from jump_optimize.
83
84 * asm_operands:
85
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
88 stack-like regs:
89
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.
93
94 An input reg that is implicitly popped by the asm must be
95 explicitly clobbered, unless it is constrained to match an
96 output operand.
97
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
103 up".
104
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.
107
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:
110
111 asm ("foo" : "=t" (a) : "f" (b));
112
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.
118
119 If any input operand uses the "f" constraint, all output reg
120 constraints must use the "&" earlyclobber.
121
122 The asm above would be written as
123
124 asm ("foo" : "=&t" (a) : "f" (b));
125
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.
130
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.
134
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.
139
140 Output operands must start at the top of the reg-stack: output
141 operands may not "skip" a reg.
142
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.
146
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.
149
150 asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
151
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.
155
156 asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
157
158 */
159 \f
160 #include "config.h"
161 #include "system.h"
162 #include "tree.h"
163 #include "rtl.h"
164 #include "insn-config.h"
165 #include "regs.h"
166 #include "hard-reg-set.h"
167 #include "flags.h"
168 #include "insn-flags.h"
169 #include "recog.h"
170 #include "toplev.h"
171
172 #ifdef STACK_REGS
173
174 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
175
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.
178
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'
181 appropriately.
182
183 REG_SET indicates which registers are live. */
184
185 typedef struct stack_def
186 {
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 */
190 } *stack;
191
192 /* highest instruction uid */
193 static int max_uid = 0;
194
195 /* Number of basic blocks in the current function. */
196 static int blocks;
197
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;
201
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;
205
206 /* Element N is nonzero if control can drop into basic block N */
207 static char *block_drops_in;
208
209 /* Element N says all about the stack at entry block N */
210 static stack block_stack_in;
211
212 /* Element N says all about the stack life at the end of block N */
213 static HARD_REG_SET *block_out_reg_set;
214
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;
221
222 /* This is the register file for all register after conversion */
223 static rtx
224 FP_mode_reg[LAST_STACK_REG+1-FIRST_STACK_REG][(int) MAX_MACHINE_MODE];
225
226 #define FP_MODE_REG(regno,mode) \
227 (FP_mode_reg[(regno)-FIRST_STACK_REG][(int)(mode)])
228
229 /* Get the basic block number of an insn. See note at block_number
230 definition are validity of this information. */
231
232 #define BLOCK_NUM(INSN) \
233 ((INSN_UID (INSN) > max_uid) \
234 ? (abort() , -1) : block_number[INSN_UID (INSN)])
235
236 extern rtx forced_labels;
237
238 /* Forward declarations */
239
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 *));
245
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 (*) ()));
267
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 *));
272 \f
273 /* Mark all registers needed for this pattern. */
274
275 static void
276 mark_regs_pat (pat, set)
277 rtx pat;
278 HARD_REG_SET *set;
279 {
280 enum machine_mode mode;
281 register int regno;
282 register int count;
283
284 if (GET_CODE (pat) == SUBREG)
285 {
286 mode = GET_MODE (pat);
287 regno = SUBREG_WORD (pat);
288 regno += REGNO (SUBREG_REG (pat));
289 }
290 else
291 regno = REGNO (pat), mode = GET_MODE (pat);
292
293 for (count = HARD_REGNO_NREGS (regno, mode);
294 count; count--, regno++)
295 SET_HARD_REG_BIT (*set, regno);
296 }
297 \f
298 /* Reorganise the stack into ascending numbers,
299 after this insn. */
300
301 static void
302 straighten_stack (insn, regstack)
303 rtx insn;
304 stack regstack;
305 {
306 struct stack_def temp_stack;
307 int top;
308
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.
311
312 Similarly if the stack is empty. */
313 if (regstack->top <= 0)
314 return;
315
316 temp_stack.reg_set = regstack->reg_set;
317
318 for (top = temp_stack.top = regstack->top; top >= 0; top--)
319 temp_stack.reg[top] = FIRST_STACK_REG + temp_stack.top - top;
320
321 change_stack (insn, regstack, &temp_stack, emit_insn_after);
322 }
323
324 /* Pop a register from the stack */
325
326 static void
327 pop_stack (regstack, regno)
328 stack regstack;
329 int regno;
330 {
331 int top = regstack->top;
332
333 CLEAR_HARD_REG_BIT (regstack->reg_set, regno);
334 regstack->top--;
335 /* If regno was not at the top of stack then adjust stack */
336 if (regstack->reg [top] != regno)
337 {
338 int i;
339 for (i = regstack->top; i >= 0; i--)
340 if (regstack->reg [i] == regno)
341 {
342 int j;
343 for (j = i; j < top; j++)
344 regstack->reg [j] = regstack->reg [j + 1];
345 break;
346 }
347 }
348 }
349 \f
350 /* Return non-zero if any stack register is mentioned somewhere within PAT. */
351
352 int
353 stack_regs_mentioned_p (pat)
354 rtx pat;
355 {
356 register char *fmt;
357 register int i;
358
359 if (STACK_REG_P (pat))
360 return 1;
361
362 fmt = GET_RTX_FORMAT (GET_CODE (pat));
363 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
364 {
365 if (fmt[i] == 'E')
366 {
367 register int j;
368
369 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
370 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
371 return 1;
372 }
373 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
374 return 1;
375 }
376
377 return 0;
378 }
379 \f
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
382 dump file, if used.
383
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.*/
389
390 void
391 reg_to_stack (first, file)
392 rtx first;
393 FILE *file;
394 {
395 register rtx insn;
396 register int i;
397 int stack_reg_seen = 0;
398 enum machine_mode mode;
399 HARD_REG_SET stackentry;
400
401 CLEAR_HARD_REG_SET (stackentry);
402
403 {
404 static int initialised;
405 if (!initialised)
406 {
407 #if 0
408 initialised = 1; /* This array can not have been previously
409 initialised, because the rtx's are
410 thrown away between compilations of
411 functions. */
412 #endif
413 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
414 {
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);
421 }
422 }
423 }
424
425 /* Count the basic blocks. Also find maximum insn uid. */
426 {
427 register RTX_CODE prev_code = BARRIER;
428 register RTX_CODE code;
429 register int before_function_beg = 1;
430
431 max_uid = 0;
432 blocks = 0;
433 for (insn = first; insn; insn = NEXT_INSN (insn))
434 {
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. */
438
439 if (INSN_UID (insn) > max_uid)
440 max_uid = INSN_UID (insn);
441
442 code = GET_CODE (insn);
443
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'))
449 blocks++;
450
451 if (code == NOTE && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
452 before_function_beg = 0;
453
454 /* Remember whether or not this insn mentions an FP regs.
455 Check JUMP_INSNs too, in case someone creates a funny PARALLEL. */
456
457 if (GET_RTX_CLASS (code) == 'i'
458 && stack_regs_mentioned_p (PATTERN (insn)))
459 {
460 stack_reg_seen = 1;
461 PUT_MODE (insn, QImode);
462
463 /* Note any register passing parameters. */
464
465 if (before_function_beg && code == INSN
466 && GET_CODE (PATTERN (insn)) == USE)
467 record_reg_life_pat (PATTERN (insn), (HARD_REG_SET *) 0,
468 &stackentry, 1);
469 }
470 else
471 PUT_MODE (insn, VOIDmode);
472
473 if (code == CODE_LABEL)
474 LABEL_REFS (insn) = insn; /* delete old chain */
475
476 if (code != NOTE)
477 prev_code = code;
478 }
479 }
480
481 /* If no stack register reference exists in this insn, there isn't
482 anything to convert. */
483
484 if (! stack_reg_seen)
485 return;
486
487 /* If there are stack registers, there must be at least one block. */
488
489 if (! blocks)
490 abort ();
491
492 /* Allocate some tables that last till end of compiling this function
493 and some needed only in find_blocks and life_analysis. */
494
495 block_begin = (rtx *) alloca (blocks * sizeof (rtx));
496 block_end = (rtx *) alloca (blocks * sizeof (rtx));
497 block_drops_in = (char *) alloca (blocks);
498
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));
503
504 block_number = (int *) alloca ((max_uid + 1) * sizeof (int));
505
506 find_blocks (first);
507 stack_reg_life_analysis (first, &stackentry);
508
509 /* Dump the life analysis debug information before jump
510 optimization, as that will destroy the LABEL_REFS we keep the
511 information in. */
512
513 if (file)
514 dump_stack_info (file);
515
516 convert_regs ();
517
518 if (optimize)
519 jump_optimize (first, 2, 0, 0);
520 }
521 \f
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
524 reference. */
525
526 static void
527 record_label_references (insn, pat)
528 rtx insn, pat;
529 {
530 register enum rtx_code code = GET_CODE (pat);
531 register int i;
532 register char *fmt;
533
534 if (code == LABEL_REF)
535 {
536 register rtx label = XEXP (pat, 0);
537 register rtx ref;
538
539 if (GET_CODE (label) != CODE_LABEL)
540 abort ();
541
542 /* If this is an undefined label, LABEL_REFS (label) contains
543 garbage. */
544 if (INSN_UID (label) == 0)
545 return;
546
547 /* Don't make a duplicate in the code_label's chain. */
548
549 for (ref = LABEL_REFS (label);
550 ref && ref != label;
551 ref = LABEL_NEXTREF (ref))
552 if (CONTAINING_INSN (ref) == insn)
553 return;
554
555 CONTAINING_INSN (pat) = insn;
556 LABEL_NEXTREF (pat) = LABEL_REFS (label);
557 LABEL_REFS (label) = pat;
558
559 return;
560 }
561
562 fmt = GET_RTX_FORMAT (code);
563 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
564 {
565 if (fmt[i] == 'e')
566 record_label_references (insn, XEXP (pat, i));
567 if (fmt[i] == 'E')
568 {
569 register int j;
570 for (j = 0; j < XVECLEN (pat, i); j++)
571 record_label_references (insn, XVECEXP (pat, i, j));
572 }
573 }
574 }
575 \f
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. */
579
580 static rtx *
581 get_true_reg (pat)
582 rtx *pat;
583 {
584 for (;;)
585 switch (GET_CODE (*pat))
586 {
587 case SUBREG:
588 /* eliminate FP subregister accesses in favour of the
589 actual FP register in use. */
590 {
591 rtx subreg;
592 if (FP_REG_P (subreg = SUBREG_REG (*pat)))
593 {
594 *pat = FP_MODE_REG (REGNO (subreg) + SUBREG_WORD (*pat),
595 GET_MODE (subreg));
596 default:
597 return pat;
598 }
599 }
600 case FLOAT:
601 case FIX:
602 case FLOAT_EXTEND:
603 pat = & XEXP (*pat, 0);
604 }
605 }
606 \f
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.
611
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. */
615
616 static void
617 record_asm_reg_life (insn, regstack)
618 rtx insn;
619 stack regstack;
620 {
621 int i;
622 int n_clobbers;
623 int malformed_asm = 0;
624 rtx body = PATTERN (insn);
625
626 int reg_used_as_output[FIRST_PSEUDO_REGISTER];
627 int implicitly_dies[FIRST_PSEUDO_REGISTER];
628 int alt;
629
630 rtx *clobber_reg;
631 int n_inputs, n_outputs;
632
633 /* Find out what the constraints require. If no constraint
634 alternative matches, this asm is malformed. */
635 extract_insn (insn);
636 constrain_operands (1);
637 alt = which_alternative;
638
639 preprocess_constraints ();
640
641 n_inputs = get_asm_operand_n_inputs (body);
642 n_outputs = recog_n_operands - n_inputs;
643
644 if (alt < 0)
645 {
646 malformed_asm = 1;
647 /* Avoid further trouble with this insn. */
648 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
649 PUT_MODE (insn, VOIDmode);
650 return;
651 }
652
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]);
658
659 /* Set up CLOBBER_REG. */
660
661 n_clobbers = 0;
662
663 if (GET_CODE (body) == PARALLEL)
664 {
665 clobber_reg = (rtx *) alloca (XVECLEN (body, 0) * sizeof (rtx));
666
667 for (i = 0; i < XVECLEN (body, 0); i++)
668 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
669 {
670 rtx clobber = XVECEXP (body, 0, i);
671 rtx reg = XEXP (clobber, 0);
672
673 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
674 reg = SUBREG_REG (reg);
675
676 if (STACK_REG_P (reg))
677 {
678 clobber_reg[n_clobbers] = reg;
679 n_clobbers++;
680 }
681 }
682 }
683
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.
687
688 Also enforce rule #5: Output operands must start at the top of
689 the reg-stack: output operands may not "skip" a reg. */
690
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]))
694 {
695 if (reg_class_size[(int) recog_op_alt[i][alt].class] != 1)
696 {
697 error_for_asm (insn, "Output constraint %d must specify a single register", i);
698 malformed_asm = 1;
699 }
700 else
701 reg_used_as_output[REGNO (recog_operand[i])] = 1;
702 }
703
704
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])
708 break;
709
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])
713 break;
714
715 if (i != LAST_STACK_REG + 1)
716 {
717 error_for_asm (insn, "Output regs must be grouped at top of stack");
718 malformed_asm = 1;
719 }
720
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
723 popped. */
724
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]))
728 {
729 /* An input reg is implicitly popped if it is tied to an
730 output, or if there is a CLOBBER for it. */
731 int j;
732
733 for (j = 0; j < n_clobbers; j++)
734 if (operands_match_p (clobber_reg[j], recog_operand[i]))
735 break;
736
737 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
738 implicitly_dies[REGNO (recog_operand[i])] = 1;
739 }
740
741 /* Search for first non-popped reg. */
742 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
743 if (! implicitly_dies[i])
744 break;
745
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])
749 break;
750
751 if (i != LAST_STACK_REG + 1)
752 {
753 error_for_asm (insn,
754 "Implicitly popped regs must be grouped at top of stack");
755 malformed_asm = 1;
756 }
757
758 /* Enfore rule #3: If any input operand uses the "f" constraint, all
759 output constraints must use the "&" earlyclobber.
760
761 ??? Detect this more deterministically by having constraint_asm_operands
762 record any earlyclobber. */
763
764 for (i = n_outputs; i < n_outputs + n_inputs; i++)
765 if (recog_op_alt[i][alt].matches == -1)
766 {
767 int j;
768
769 for (j = 0; j < n_outputs; j++)
770 if (operands_match_p (recog_operand[j], recog_operand[i]))
771 {
772 error_for_asm (insn,
773 "Output operand %d must use `&' constraint", j);
774 malformed_asm = 1;
775 }
776 }
777
778 if (malformed_asm)
779 {
780 /* Avoid further trouble with this insn. */
781 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
782 PUT_MODE (insn, VOIDmode);
783 return;
784 }
785
786 /* Process all outputs */
787 for (i = 0; i < n_outputs; i++)
788 {
789 rtx op = recog_operand[i];
790
791 if (! STACK_REG_P (op))
792 {
793 if (stack_regs_mentioned_p (op))
794 abort ();
795 else
796 continue;
797 }
798
799 /* Each destination is dead before this insn. If the
800 destination is not used after this insn, record this with
801 REG_UNUSED. */
802
803 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (op)))
804 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_UNUSED, op,
805 REG_NOTES (insn));
806
807 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (op));
808 }
809
810 /* Process all inputs */
811 for (i = n_outputs; i < n_outputs + n_inputs; i++)
812 {
813 rtx op = recog_operand[i];
814 if (! STACK_REG_P (op))
815 {
816 if (stack_regs_mentioned_p (op))
817 abort ();
818 else
819 continue;
820 }
821
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. */
825
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));
830
831 SET_HARD_REG_BIT (regstack->reg_set, REGNO (op));
832 }
833 }
834
835 /* Scan PAT, which is part of INSN, and record registers appearing in
836 a SET_DEST in DEST, and other registers in SRC.
837
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. */
840
841 static void
842 record_reg_life_pat (pat, src, dest, douse)
843 rtx pat;
844 HARD_REG_SET *src, *dest;
845 int douse;
846 {
847 register char *fmt;
848 register int i;
849
850 if (STACK_REG_P (pat)
851 || (GET_CODE (pat) == SUBREG && STACK_REG_P (SUBREG_REG (pat))))
852 {
853 if (src)
854 mark_regs_pat (pat, src);
855
856 if (dest)
857 mark_regs_pat (pat, dest);
858
859 return;
860 }
861
862 if (GET_CODE (pat) == SET)
863 {
864 record_reg_life_pat (XEXP (pat, 0), NULL_PTR, dest, 0);
865 record_reg_life_pat (XEXP (pat, 1), src, NULL_PTR, 0);
866 return;
867 }
868
869 /* We don't need to consider either of these cases. */
870 if ((GET_CODE (pat) == USE && !douse) || GET_CODE (pat) == CLOBBER)
871 return;
872
873 fmt = GET_RTX_FORMAT (GET_CODE (pat));
874 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
875 {
876 if (fmt[i] == 'E')
877 {
878 register int j;
879
880 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
881 record_reg_life_pat (XVECEXP (pat, i, j), src, dest, 0);
882 }
883 else if (fmt[i] == 'e')
884 record_reg_life_pat (XEXP (pat, i), src, dest, 0);
885 }
886 }
887 \f
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
891 placed. */
892
893 static int
894 get_asm_operand_n_inputs (body)
895 rtx body;
896 {
897 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
898 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body));
899
900 else if (GET_CODE (body) == ASM_OPERANDS)
901 return ASM_OPERANDS_INPUT_LENGTH (body);
902
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)));
906
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));
910
911 abort ();
912 }
913 \f
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
917 record_reg_life_pat.
918
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.
922
923 Existing death and unset notes for stack registers are deleted
924 before processing the insn. */
925
926 static void
927 record_reg_life (insn, block, regstack)
928 rtx insn;
929 int block;
930 stack regstack;
931 {
932 rtx note, *note_link;
933 int n_operands;
934
935 if ((GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
936 || INSN_DELETED_P (insn))
937 return;
938
939 /* Strip death notes for stack regs from this insn */
940
941 note_link = &REG_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);
947 else
948 note_link = &XEXP (note, 1);
949
950 /* Process all patterns in the insn. */
951
952 n_operands = asm_noperands (PATTERN (insn));
953 if (n_operands >= 0)
954 {
955 record_asm_reg_life (insn, regstack);
956 return;
957 }
958
959 {
960 HARD_REG_SET src, dest;
961 int regno;
962
963 CLEAR_HARD_REG_SET (src);
964 CLEAR_HARD_REG_SET (dest);
965
966 if (GET_CODE (insn) == CALL_INSN)
967 for (note = CALL_INSN_FUNCTION_USAGE (insn);
968 note;
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);
972
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))
976 {
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),
981 REG_NOTES (insn));
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),
985 REG_NOTES (insn));
986 }
987
988 if (GET_CODE (insn) == CALL_INSN)
989 {
990 int reg;
991
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(). */
995
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))
999 {
1000 rtx init, pat;
1001
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
1005 block_number[]. */
1006
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);
1011
1012 CLEAR_HARD_REG_BIT (regstack->reg_set, reg);
1013
1014 /* If the CALL_INSN was the end of a block, move the
1015 block_end to point to the new insn. */
1016
1017 if (block_end[block] == insn)
1018 block_end[block] = init;
1019 }
1020
1021 /* Some regs do not survive a CALL */
1022 AND_COMPL_HARD_REG_SET (regstack->reg_set, call_used_reg_set);
1023 }
1024
1025 AND_COMPL_HARD_REG_SET (regstack->reg_set, dest);
1026 IOR_HARD_REG_SET (regstack->reg_set, src);
1027 }
1028 }
1029 \f
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. */
1032
1033 static void
1034 find_blocks (first)
1035 rtx first;
1036 {
1037 register rtx insn;
1038 register int block;
1039 register RTX_CODE prev_code = BARRIER;
1040 register RTX_CODE code;
1041 rtx label_value_list = 0;
1042
1043 /* Record where all the blocks start and end.
1044 Record which basic blocks control can drop in to. */
1045
1046 block = -1;
1047 for (insn = first; insn; insn = NEXT_INSN (insn))
1048 {
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. */
1052
1053 code = GET_CODE (insn);
1054
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'))
1060 {
1061 block_begin[++block] = insn;
1062 block_end[block] = insn;
1063 block_drops_in[block] = prev_code != BARRIER;
1064 }
1065 else if (GET_RTX_CLASS (code) == 'i')
1066 block_end[block] = insn;
1067
1068 if (GET_RTX_CLASS (code) == 'i')
1069 {
1070 rtx note;
1071
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),
1076 label_value_list);
1077 }
1078
1079 block_number[INSN_UID (insn)] = block;
1080
1081 if (code != NOTE)
1082 prev_code = code;
1083 }
1084
1085 if (block + 1 != blocks)
1086 abort ();
1087
1088 /* generate all label references to the corresponding jump insn */
1089 for (block = 0; block < blocks; block++)
1090 {
1091 insn = block_end[block];
1092
1093 if (GET_CODE (insn) == JUMP_INSN)
1094 {
1095 rtx pat = PATTERN (insn);
1096 rtx x;
1097
1098 if (computed_jump_p (insn))
1099 {
1100 for (x = label_value_list; x; x = XEXP (x, 1))
1101 record_label_references (insn,
1102 gen_rtx_LABEL_REF (VOIDmode,
1103 XEXP (x, 0)));
1104
1105 for (x = forced_labels; x; x = XEXP (x, 1))
1106 record_label_references (insn,
1107 gen_rtx_LABEL_REF (VOIDmode,
1108 XEXP (x, 0)));
1109 }
1110
1111 record_label_references (insn, pat);
1112 }
1113 }
1114 }
1115
1116 /* If current function returns its result in an fp stack register,
1117 return the REG. Otherwise, return 0. */
1118
1119 static rtx
1120 stack_result (decl)
1121 tree decl;
1122 {
1123 rtx result = DECL_RTL (DECL_RESULT (decl));
1124
1125 if (result != 0
1126 && ! (GET_CODE (result) == REG
1127 && REGNO (result) < FIRST_PSEUDO_REGISTER))
1128 {
1129 #ifdef FUNCTION_OUTGOING_VALUE
1130 result
1131 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
1132 #else
1133 result = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
1134 #endif
1135 }
1136
1137 return result != 0 && STACK_REG_P (result) ? result : 0;
1138 }
1139 \f
1140 /* Determine the which registers are live at the start of each basic
1141 block of the function whose first insn is FIRST.
1142
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.
1146
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[].
1151
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.
1154
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.
1160
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.
1165
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. */
1169
1170 static void
1171 stack_reg_life_analysis (first, stackentry)
1172 rtx first;
1173 HARD_REG_SET *stackentry;
1174 {
1175 int reg, block;
1176 struct stack_def regstack;
1177
1178 {
1179 rtx retvalue;
1180
1181 if ((retvalue = stack_result (current_function_decl)))
1182 {
1183 /* Find all RETURN insns and mark them. */
1184
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);
1189
1190 /* Mark off the end of last block if we "fall off" the end of the
1191 function into the epilogue. */
1192
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);
1196 }
1197 }
1198
1199 /* now scan all blocks backward for stack register use */
1200
1201 block = blocks - 1;
1202 while (block >= 0)
1203 {
1204 register rtx insn, prev;
1205
1206 /* current register status at last instruction */
1207
1208 COPY_HARD_REG_SET (regstack.reg_set, block_out_reg_set[block]);
1209
1210 prev = block_end[block];
1211 do
1212 {
1213 insn = prev;
1214 prev = PREV_INSN (insn);
1215
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. */
1219
1220 if (GET_MODE (insn) == QImode || GET_CODE (insn) == CALL_INSN)
1221 record_reg_life (insn, block, &regstack);
1222
1223 } while (insn != block_begin[block]);
1224
1225 /* Set the state at the start of the block. Mark that no
1226 register mapping information known yet. */
1227
1228 COPY_HARD_REG_SET (block_stack_in[block].reg_set, regstack.reg_set);
1229 block_stack_in[block].top = -2;
1230
1231 /* If there is a label, propagate our register life to all jumps
1232 to this label. */
1233
1234 if (GET_CODE (insn) == CODE_LABEL)
1235 {
1236 register rtx label;
1237 int must_restart = 0;
1238
1239 for (label = LABEL_REFS (insn); label != insn;
1240 label = LABEL_NEXTREF (label))
1241 {
1242 int jump_block = BLOCK_NUM (CONTAINING_INSN (label));
1243
1244 if (jump_block < block)
1245 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
1246 block_stack_in[block].reg_set);
1247 else
1248 {
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. */
1254
1255 GO_IF_HARD_REG_SUBSET (block_stack_in[block].reg_set,
1256 block_out_reg_set[jump_block],
1257 win);
1258
1259 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
1260 block_stack_in[block].reg_set);
1261
1262 block = jump_block;
1263 must_restart = 1;
1264 break;
1265
1266 win:
1267 ;
1268 }
1269 }
1270 if (must_restart)
1271 continue;
1272 }
1273
1274 if (block_drops_in[block])
1275 IOR_HARD_REG_SET (block_out_reg_set[block-1],
1276 block_stack_in[block].reg_set);
1277
1278 block -= 1;
1279 }
1280
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. */
1285
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.
1292
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. */
1296
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))
1300 {
1301 rtx init_rtx;
1302
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);
1307
1308 CLEAR_HARD_REG_BIT (block_stack_in[0].reg_set, reg);
1309 }
1310 }
1311 \f
1312 /*****************************************************************************
1313 This section deals with stack register substitution, and forms the second
1314 pass over the RTL.
1315 *****************************************************************************/
1316
1317 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
1318 the desired hard REGNO. */
1319
1320 static void
1321 replace_reg (reg, regno)
1322 rtx *reg;
1323 int regno;
1324 {
1325 if (regno < FIRST_STACK_REG || regno > LAST_STACK_REG
1326 || ! STACK_REG_P (*reg))
1327 abort ();
1328
1329 switch (GET_MODE_CLASS (GET_MODE (*reg)))
1330 {
1331 default: abort ();
1332 case MODE_FLOAT:
1333 case MODE_COMPLEX_FLOAT:;
1334 }
1335
1336 *reg = FP_MODE_REG (regno, GET_MODE (*reg));
1337 }
1338
1339 /* Remove a note of type NOTE, which must be found, for register
1340 number REGNO from INSN. Remove only one such note. */
1341
1342 static void
1343 remove_regno_note (insn, note, regno)
1344 rtx insn;
1345 enum reg_note note;
1346 int regno;
1347 {
1348 register rtx *note_link, this;
1349
1350 note_link = &REG_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)
1354 {
1355 *note_link = XEXP (this, 1);
1356 return;
1357 }
1358 else
1359 note_link = &XEXP (this, 1);
1360
1361 abort ();
1362 }
1363
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. */
1367
1368 static int
1369 get_hard_regnum (regstack, reg)
1370 stack regstack;
1371 rtx reg;
1372 {
1373 int i;
1374
1375 if (! STACK_REG_P (reg))
1376 abort ();
1377
1378 for (i = regstack->top; i >= 0; i--)
1379 if (regstack->reg[i] == REGNO (reg))
1380 break;
1381
1382 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
1383 }
1384
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. */
1388
1389 static void
1390 delete_insn_for_stacker (insn)
1391 rtx insn;
1392 {
1393 PUT_CODE (insn, NOTE);
1394 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1395 NOTE_SOURCE_FILE (insn) = 0;
1396 }
1397 \f
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. */
1404
1405 static rtx
1406 emit_pop_insn (insn, regstack, reg, when)
1407 rtx insn;
1408 stack regstack;
1409 rtx reg;
1410 rtx (*when)();
1411 {
1412 rtx pop_insn, pop_rtx;
1413 int hard_regno;
1414
1415 hard_regno = get_hard_regnum (regstack, reg);
1416
1417 if (hard_regno < FIRST_STACK_REG)
1418 abort ();
1419
1420 pop_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG (hard_regno, DFmode),
1421 FP_MODE_REG (FIRST_STACK_REG, DFmode));
1422
1423 pop_insn = (*when) (pop_rtx, insn);
1424 /* ??? This used to be VOIDmode, but that seems wrong. */
1425 PUT_MODE (pop_insn, QImode);
1426
1427 REG_NOTES (pop_insn) = gen_rtx_EXPR_LIST (REG_DEAD,
1428 FP_MODE_REG (FIRST_STACK_REG, DFmode),
1429 REG_NOTES (pop_insn));
1430
1431 regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
1432 = regstack->reg[regstack->top];
1433 regstack->top -= 1;
1434 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
1435
1436 return pop_insn;
1437 }
1438 \f
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.
1444
1445 If REG is already at the top of the stack, no insn is emitted. */
1446
1447 static void
1448 emit_swap_insn (insn, regstack, reg)
1449 rtx insn;
1450 stack regstack;
1451 rtx reg;
1452 {
1453 int hard_regno;
1454 rtx gen_swapdf();
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 */
1459
1460 hard_regno = get_hard_regnum (regstack, reg);
1461
1462 if (hard_regno < FIRST_STACK_REG)
1463 abort ();
1464 if (hard_regno == FIRST_STACK_REG)
1465 return;
1466
1467 other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
1468
1469 tmp = regstack->reg[other_reg];
1470 regstack->reg[other_reg] = regstack->reg[regstack->top];
1471 regstack->reg[regstack->top] = tmp;
1472
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);
1478
1479 if (i1)
1480 i1set = single_set (i1);
1481
1482 if (i1set)
1483 {
1484 rtx i1src = *get_true_reg (&SET_SRC (i1set));
1485 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
1486
1487 /* If the previous register stack push was from the reg we are to
1488 swap with, omit the swap. */
1489
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)
1493 return;
1494
1495 /* If the previous insn wrote to the reg we are to swap with,
1496 omit the swap. */
1497
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)
1501 return;
1502 }
1503
1504 if (GET_RTX_CLASS (GET_CODE (i1)) == 'i' && sets_cc0_p (PATTERN (i1)))
1505 {
1506 i1 = next_nonnote_insn (i1);
1507 if (i1 == insn)
1508 abort ();
1509 }
1510
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);
1516 }
1517 \f
1518 /* Handle a move to or from a stack register in PAT, which is in INSN.
1519 REGSTACK is the current stack. */
1520
1521 static void
1522 move_for_stack_reg (insn, regstack, pat)
1523 rtx insn;
1524 stack regstack;
1525 rtx pat;
1526 {
1527 rtx *psrc = get_true_reg (&SET_SRC (pat));
1528 rtx *pdest = get_true_reg (&SET_DEST (pat));
1529 rtx src, dest;
1530 rtx note;
1531
1532 src = *psrc; dest = *pdest;
1533
1534 if (STACK_REG_P (src) && STACK_REG_P (dest))
1535 {
1536 /* Write from one stack reg to another. If SRC dies here, then
1537 just change the register mapping and delete the insn. */
1538
1539 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1540 if (note)
1541 {
1542 int i;
1543
1544 /* If this is a no-op move, there must not be a REG_DEAD note. */
1545 if (REGNO (src) == REGNO (dest))
1546 abort ();
1547
1548 for (i = regstack->top; i >= 0; i--)
1549 if (regstack->reg[i] == REGNO (src))
1550 break;
1551
1552 /* The source must be live, and the dest must be dead. */
1553 if (i < 0 || get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1554 abort ();
1555
1556 /* It is possible that the dest is unused after this insn.
1557 If so, just pop the src. */
1558
1559 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1560 {
1561 emit_pop_insn (insn, regstack, src, emit_insn_after);
1562
1563 delete_insn_for_stacker (insn);
1564 return;
1565 }
1566
1567 regstack->reg[i] = REGNO (dest);
1568
1569 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1570 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1571
1572 delete_insn_for_stacker (insn);
1573
1574 return;
1575 }
1576
1577 /* The source reg does not die. */
1578
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. */
1583
1584 if (REGNO (src) == REGNO (dest))
1585 {
1586 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
1587 emit_pop_insn (insn, regstack, dest, emit_insn_after);
1588
1589 delete_insn_for_stacker (insn);
1590 return;
1591 }
1592
1593 /* The destination ought to be dead */
1594 if (get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1595 abort ();
1596
1597 replace_reg (psrc, get_hard_regnum (regstack, src));
1598
1599 regstack->reg[++regstack->top] = REGNO (dest);
1600 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1601 replace_reg (pdest, FIRST_STACK_REG);
1602 }
1603 else if (STACK_REG_P (src))
1604 {
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
1607 needs be. */
1608
1609 emit_swap_insn (insn, regstack, src);
1610
1611 note = find_regno_note (insn, REG_DEAD, REGNO (src));
1612 if (note)
1613 {
1614 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1615 regstack->top--;
1616 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
1617 }
1618 else if (GET_MODE (src) == XFmode && regstack->top < REG_STACK_SIZE - 1)
1619 {
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
1626 a pop. */
1627 rtx push_rtx, push_insn;
1628 rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, XFmode);
1629
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,
1634 REG_NOTES (insn));
1635 }
1636
1637 replace_reg (psrc, FIRST_STACK_REG);
1638 }
1639 else if (STACK_REG_P (dest))
1640 {
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. */
1645
1646 /* The destination ought to be dead */
1647 if (get_hard_regnum (regstack, dest) >= FIRST_STACK_REG)
1648 abort ();
1649
1650 if (regstack->top >= REG_STACK_SIZE)
1651 abort ();
1652
1653 regstack->reg[++regstack->top] = REGNO (dest);
1654 SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
1655 replace_reg (pdest, FIRST_STACK_REG);
1656 }
1657 else
1658 abort ();
1659 }
1660 \f
1661 static void
1662 swap_rtx_condition (pat)
1663 rtx pat;
1664 {
1665 register char *fmt;
1666 register int i;
1667
1668 if (GET_RTX_CLASS (GET_CODE (pat)) == '<')
1669 {
1670 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1671 return;
1672 }
1673
1674 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1675 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1676 {
1677 if (fmt[i] == 'E')
1678 {
1679 register int j;
1680
1681 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1682 swap_rtx_condition (XVECEXP (pat, i, j));
1683 }
1684 else if (fmt[i] == 'e')
1685 swap_rtx_condition (XEXP (pat, i));
1686 }
1687 }
1688
1689 /* Handle a comparison. Special care needs to be taken to avoid
1690 causing comparisons that a 387 cannot do correctly, such as EQ.
1691
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
1695 set up. */
1696
1697 static void
1698 compare_for_stack_reg (insn, regstack, pat)
1699 rtx insn;
1700 stack regstack;
1701 rtx pat;
1702 {
1703 rtx *src1, *src2;
1704 rtx src1_note, src2_note;
1705 rtx cc0_user;
1706 int have_cmove;
1707
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);
1711
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))))
1718 == MODE_FLOAT))
1719 {
1720 rtx *dest;
1721
1722 dest = get_true_reg (&SET_DEST (PATTERN (cc0_user)));
1723
1724 have_cmove = 1;
1725 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
1726 && REGNO (*dest) != regstack->reg[regstack->top])
1727 {
1728 emit_swap_insn (insn, regstack, *dest);
1729 }
1730 }
1731 else
1732 have_cmove = 0;
1733
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))
1739 {
1740 rtx temp, next;
1741
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;
1745
1746 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1747 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
1748
1749 next = next_cc0_user (insn);
1750 if (next == NULL_RTX)
1751 abort ();
1752
1753 swap_rtx_condition (PATTERN (next));
1754 INSN_CODE (next) = -1;
1755 INSN_CODE (insn) = -1;
1756 }
1757
1758 /* We will fix any death note later. */
1759
1760 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1761
1762 if (STACK_REG_P (*src2))
1763 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1764 else
1765 src2_note = NULL_RTX;
1766
1767 if (! have_cmove)
1768 emit_swap_insn (insn, regstack, *src1);
1769
1770 replace_reg (src1, FIRST_STACK_REG);
1771
1772 if (STACK_REG_P (*src2))
1773 replace_reg (src2, get_hard_regnum (regstack, *src2));
1774
1775 if (src1_note)
1776 {
1777 pop_stack (regstack, REGNO (XEXP (src1_note, 0)));
1778 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1779 }
1780
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. */
1784
1785 if (src2_note
1786 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
1787 && REGNO (*src1) == REGNO (*src2)))
1788 {
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. */
1793
1794 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1795 && src1_note)
1796 {
1797 pop_stack (regstack, REGNO (XEXP (src2_note, 0)));
1798 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1799 }
1800 else
1801 {
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. */
1805
1806 link_cc0_insns (insn);
1807
1808 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
1809
1810 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
1811 emit_insn_after);
1812 }
1813 }
1814 }
1815 \f
1816 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1817 is the current register layout. */
1818
1819 static void
1820 subst_stack_regs_pat (insn, regstack, pat)
1821 rtx insn;
1822 stack regstack;
1823 rtx pat;
1824 {
1825 rtx *dest, *src;
1826 rtx *src1 = (rtx *) NULL_PTR, *src2;
1827 rtx src1_note, src2_note;
1828
1829 if (GET_CODE (pat) != SET)
1830 return;
1831
1832 dest = get_true_reg (&SET_DEST (pat));
1833 src = get_true_reg (&SET_SRC (pat));
1834
1835 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1836
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);
1843 else
1844 switch (GET_CODE (SET_SRC (pat)))
1845 {
1846 case COMPARE:
1847 compare_for_stack_reg (insn, regstack, pat);
1848 break;
1849
1850 case CALL:
1851 {
1852 int count;
1853 for (count = HARD_REGNO_NREGS (REGNO (*dest), GET_MODE (*dest));
1854 --count >= 0;)
1855 {
1856 regstack->reg[++regstack->top] = REGNO (*dest) + count;
1857 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
1858 }
1859 }
1860 replace_reg (dest, FIRST_STACK_REG);
1861 break;
1862
1863 case REG:
1864 /* This is a `tstM2' case. */
1865 if (*dest != cc0_rtx)
1866 abort ();
1867
1868 src1 = src;
1869
1870 /* Fall through. */
1871
1872 case FLOAT_TRUNCATE:
1873 case SQRT:
1874 case ABS:
1875 case NEG:
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
1879 source. */
1880
1881 if (src1 == 0)
1882 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1883
1884 emit_swap_insn (insn, regstack, *src1);
1885
1886 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1887
1888 if (STACK_REG_P (*dest))
1889 replace_reg (dest, FIRST_STACK_REG);
1890
1891 if (src1_note)
1892 {
1893 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1894 regstack->top--;
1895 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1896 }
1897
1898 replace_reg (src1, FIRST_STACK_REG);
1899
1900 break;
1901
1902 case MINUS:
1903 case DIV:
1904 /* On i386, reversed forms of subM3 and divM3 exist for
1905 MODE_FLOAT, so the same code that works for addM3 and mulM3
1906 can be used. */
1907 case MULT:
1908 case PLUS:
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. */
1913
1914 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1915 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
1916
1917 /* We will fix any death note later. */
1918
1919 if (STACK_REG_P (*src1))
1920 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1921 else
1922 src1_note = NULL_RTX;
1923 if (STACK_REG_P (*src2))
1924 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1925 else
1926 src2_note = NULL_RTX;
1927
1928 /* If either operand is not a stack register, then the dest
1929 must be top of stack. */
1930
1931 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
1932 emit_swap_insn (insn, regstack, *dest);
1933 else
1934 {
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. */
1938
1939 int src1_hard_regnum, src2_hard_regnum;
1940
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)
1944 abort ();
1945
1946 if (src1_hard_regnum != FIRST_STACK_REG
1947 && src2_hard_regnum != FIRST_STACK_REG)
1948 emit_swap_insn (insn, regstack, *dest);
1949 }
1950
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));
1955
1956 if (src1_note)
1957 {
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. */
1963
1964 if (REGNO (XEXP (src1_note, 0)) == regstack->reg[regstack->top])
1965 {
1966 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1967 replace_reg (dest, get_hard_regnum (regstack, *dest));
1968 }
1969 else
1970 {
1971 int regno = get_hard_regnum (regstack, XEXP (src1_note, 0));
1972
1973 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1974 replace_reg (dest, regno);
1975
1976 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1977 = regstack->reg[regstack->top];
1978 }
1979
1980 CLEAR_HARD_REG_BIT (regstack->reg_set,
1981 REGNO (XEXP (src1_note, 0)));
1982 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1983 regstack->top--;
1984 }
1985 else if (src2_note)
1986 {
1987 if (REGNO (XEXP (src2_note, 0)) == regstack->reg[regstack->top])
1988 {
1989 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1990 replace_reg (dest, get_hard_regnum (regstack, *dest));
1991 }
1992 else
1993 {
1994 int regno = get_hard_regnum (regstack, XEXP (src2_note, 0));
1995
1996 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1997 replace_reg (dest, regno);
1998
1999 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
2000 = regstack->reg[regstack->top];
2001 }
2002
2003 CLEAR_HARD_REG_BIT (regstack->reg_set,
2004 REGNO (XEXP (src2_note, 0)));
2005 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
2006 regstack->top--;
2007 }
2008 else
2009 {
2010 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2011 replace_reg (dest, get_hard_regnum (regstack, *dest));
2012 }
2013
2014 break;
2015
2016 case UNSPEC:
2017 switch (XINT (SET_SRC (pat), 1))
2018 {
2019 case 1: /* sin */
2020 case 2: /* cos */
2021 /* These insns only operate on the top of the stack. */
2022
2023 src1 = get_true_reg (&XVECEXP (SET_SRC (pat), 0, 0));
2024
2025 emit_swap_insn (insn, regstack, *src1);
2026
2027 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2028
2029 if (STACK_REG_P (*dest))
2030 replace_reg (dest, FIRST_STACK_REG);
2031
2032 if (src1_note)
2033 {
2034 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2035 regstack->top--;
2036 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
2037 }
2038
2039 replace_reg (src1, FIRST_STACK_REG);
2040
2041 break;
2042
2043 default:
2044 abort ();
2045 }
2046 break;
2047
2048 case IF_THEN_ELSE:
2049 /* dest has to be on stack. */
2050 if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
2051 abort ();
2052
2053 /* This insn requires the top of stack to be the destination. */
2054
2055 /* If the comparison operator is an FP comparison operator,
2056 it is handled correctly by compare_for_stack_reg () who
2057 will move the destination to the top of stack. But if the
2058 comparison operator is not an FP comparison operator, we
2059 have to handle it here. */
2060 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
2061 && REGNO (*dest) != regstack->reg[regstack->top])
2062 emit_swap_insn (insn, regstack, *dest);
2063
2064 src1 = get_true_reg (&XEXP (SET_SRC (pat), 1));
2065 src2 = get_true_reg (&XEXP (SET_SRC (pat), 2));
2066
2067 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2068 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
2069
2070 {
2071 rtx src_note [3];
2072 int i;
2073
2074 src_note[0] = 0;
2075 src_note[1] = src1_note;
2076 src_note[2] = src2_note;
2077
2078 if (STACK_REG_P (*src1))
2079 replace_reg (src1, get_hard_regnum (regstack, *src1));
2080 if (STACK_REG_P (*src2))
2081 replace_reg (src2, get_hard_regnum (regstack, *src2));
2082
2083 for (i = 1; i <= 2; i++)
2084 if (src_note [i])
2085 {
2086 /* If the register that dies is not at the top of stack, then
2087 move the top of stack to the dead reg */
2088 if (REGNO (XEXP (src_note[i], 0))
2089 != regstack->reg[regstack->top])
2090 {
2091 remove_regno_note (insn, REG_DEAD,
2092 REGNO (XEXP (src_note [i], 0)));
2093 emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
2094 emit_insn_after);
2095 }
2096 else
2097 {
2098 CLEAR_HARD_REG_BIT (regstack->reg_set,
2099 REGNO (XEXP (src_note[i], 0)));
2100 replace_reg (&XEXP (src_note[i], 0), FIRST_STACK_REG);
2101 regstack->top--;
2102 }
2103 }
2104 }
2105
2106 /* Make dest the top of stack. */
2107 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2108 replace_reg (dest, FIRST_STACK_REG);
2109
2110 break;
2111
2112 default:
2113 abort ();
2114 }
2115 }
2116 \f
2117 /* Substitute hard regnums for any stack regs in INSN, which has
2118 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
2119 before the insn, and is updated with changes made here.
2120
2121 There are several requirements and assumptions about the use of
2122 stack-like regs in asm statements. These rules are enforced by
2123 record_asm_stack_regs; see comments there for details. Any
2124 asm_operands left in the RTL at this point may be assume to meet the
2125 requirements, since record_asm_stack_regs removes any problem asm. */
2126
2127 static void
2128 subst_asm_stack_regs (insn, regstack)
2129 rtx insn;
2130 stack regstack;
2131 {
2132 rtx body = PATTERN (insn);
2133 int alt;
2134
2135 rtx *note_reg; /* Array of note contents */
2136 rtx **note_loc; /* Address of REG field of each note */
2137 enum reg_note *note_kind; /* The type of each note */
2138
2139 rtx *clobber_reg;
2140 rtx **clobber_loc;
2141
2142 struct stack_def temp_stack;
2143 int n_notes;
2144 int n_clobbers;
2145 rtx note;
2146 int i;
2147 int n_inputs, n_outputs;
2148
2149 /* Find out what the constraints required. If no constraint
2150 alternative matches, that is a compiler bug: we should have caught
2151 such an insn during the life analysis pass (and reload should have
2152 caught it regardless). */
2153 extract_insn (insn);
2154 constrain_operands (1);
2155 alt = which_alternative;
2156
2157 preprocess_constraints ();
2158
2159 n_inputs = get_asm_operand_n_inputs (body);
2160 n_outputs = recog_n_operands - n_inputs;
2161
2162 if (alt < 0)
2163 abort ();
2164
2165 /* Strip SUBREGs here to make the following code simpler. */
2166 for (i = 0; i < recog_n_operands; i++)
2167 if (GET_CODE (recog_operand[i]) == SUBREG
2168 && GET_CODE (SUBREG_REG (recog_operand[i])) == REG)
2169 {
2170 recog_operand_loc[i] = & SUBREG_REG (recog_operand[i]);
2171 recog_operand[i] = SUBREG_REG (recog_operand[i]);
2172 }
2173
2174 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
2175
2176 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
2177 i++;
2178
2179 note_reg = (rtx *) alloca (i * sizeof (rtx));
2180 note_loc = (rtx **) alloca (i * sizeof (rtx *));
2181 note_kind = (enum reg_note *) alloca (i * sizeof (enum reg_note));
2182
2183 n_notes = 0;
2184 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2185 {
2186 rtx reg = XEXP (note, 0);
2187 rtx *loc = & XEXP (note, 0);
2188
2189 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
2190 {
2191 loc = & SUBREG_REG (reg);
2192 reg = SUBREG_REG (reg);
2193 }
2194
2195 if (STACK_REG_P (reg)
2196 && (REG_NOTE_KIND (note) == REG_DEAD
2197 || REG_NOTE_KIND (note) == REG_UNUSED))
2198 {
2199 note_reg[n_notes] = reg;
2200 note_loc[n_notes] = loc;
2201 note_kind[n_notes] = REG_NOTE_KIND (note);
2202 n_notes++;
2203 }
2204 }
2205
2206 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2207
2208 n_clobbers = 0;
2209
2210 if (GET_CODE (body) == PARALLEL)
2211 {
2212 clobber_reg = (rtx *) alloca (XVECLEN (body, 0) * sizeof (rtx));
2213 clobber_loc = (rtx **) alloca (XVECLEN (body, 0) * sizeof (rtx *));
2214
2215 for (i = 0; i < XVECLEN (body, 0); i++)
2216 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
2217 {
2218 rtx clobber = XVECEXP (body, 0, i);
2219 rtx reg = XEXP (clobber, 0);
2220 rtx *loc = & XEXP (clobber, 0);
2221
2222 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
2223 {
2224 loc = & SUBREG_REG (reg);
2225 reg = SUBREG_REG (reg);
2226 }
2227
2228 if (STACK_REG_P (reg))
2229 {
2230 clobber_reg[n_clobbers] = reg;
2231 clobber_loc[n_clobbers] = loc;
2232 n_clobbers++;
2233 }
2234 }
2235 }
2236
2237 bcopy ((char *) regstack, (char *) &temp_stack, sizeof (temp_stack));
2238
2239 /* Put the input regs into the desired place in TEMP_STACK. */
2240
2241 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2242 if (STACK_REG_P (recog_operand[i])
2243 && reg_class_subset_p (recog_op_alt[i][alt].class,
2244 FLOAT_REGS)
2245 && recog_op_alt[i][alt].class != FLOAT_REGS)
2246 {
2247 /* If an operand needs to be in a particular reg in
2248 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2249 these constraints are for single register classes, and reload
2250 guaranteed that operand[i] is already in that class, we can
2251 just use REGNO (recog_operand[i]) to know which actual reg this
2252 operand needs to be in. */
2253
2254 int regno = get_hard_regnum (&temp_stack, recog_operand[i]);
2255
2256 if (regno < 0)
2257 abort ();
2258
2259 if (regno != REGNO (recog_operand[i]))
2260 {
2261 /* recog_operand[i] is not in the right place. Find it
2262 and swap it with whatever is already in I's place.
2263 K is where recog_operand[i] is now. J is where it should
2264 be. */
2265 int j, k, temp;
2266
2267 k = temp_stack.top - (regno - FIRST_STACK_REG);
2268 j = (temp_stack.top
2269 - (REGNO (recog_operand[i]) - FIRST_STACK_REG));
2270
2271 temp = temp_stack.reg[k];
2272 temp_stack.reg[k] = temp_stack.reg[j];
2273 temp_stack.reg[j] = temp;
2274 }
2275 }
2276
2277 /* emit insns before INSN to make sure the reg-stack is in the right
2278 order. */
2279
2280 change_stack (insn, regstack, &temp_stack, emit_insn_before);
2281
2282 /* Make the needed input register substitutions. Do death notes and
2283 clobbers too, because these are for inputs, not outputs. */
2284
2285 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2286 if (STACK_REG_P (recog_operand[i]))
2287 {
2288 int regnum = get_hard_regnum (regstack, recog_operand[i]);
2289
2290 if (regnum < 0)
2291 abort ();
2292
2293 replace_reg (recog_operand_loc[i], regnum);
2294 }
2295
2296 for (i = 0; i < n_notes; i++)
2297 if (note_kind[i] == REG_DEAD)
2298 {
2299 int regnum = get_hard_regnum (regstack, note_reg[i]);
2300
2301 if (regnum < 0)
2302 abort ();
2303
2304 replace_reg (note_loc[i], regnum);
2305 }
2306
2307 for (i = 0; i < n_clobbers; i++)
2308 {
2309 /* It's OK for a CLOBBER to reference a reg that is not live.
2310 Don't try to replace it in that case. */
2311 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2312
2313 if (regnum >= 0)
2314 {
2315 /* Sigh - clobbers always have QImode. But replace_reg knows
2316 that these regs can't be MODE_INT and will abort. Just put
2317 the right reg there without calling replace_reg. */
2318
2319 *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
2320 }
2321 }
2322
2323 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2324
2325 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2326 if (STACK_REG_P (recog_operand[i]))
2327 {
2328 /* An input reg is implicitly popped if it is tied to an
2329 output, or if there is a CLOBBER for it. */
2330 int j;
2331
2332 for (j = 0; j < n_clobbers; j++)
2333 if (operands_match_p (clobber_reg[j], recog_operand[i]))
2334 break;
2335
2336 if (j < n_clobbers || recog_op_alt[i][alt].matches >= 0)
2337 {
2338 /* recog_operand[i] might not be at the top of stack. But that's
2339 OK, because all we need to do is pop the right number of regs
2340 off of the top of the reg-stack. record_asm_stack_regs
2341 guaranteed that all implicitly popped regs were grouped
2342 at the top of the reg-stack. */
2343
2344 CLEAR_HARD_REG_BIT (regstack->reg_set,
2345 regstack->reg[regstack->top]);
2346 regstack->top--;
2347 }
2348 }
2349
2350 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2351 Note that there isn't any need to substitute register numbers.
2352 ??? Explain why this is true. */
2353
2354 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2355 {
2356 /* See if there is an output for this hard reg. */
2357 int j;
2358
2359 for (j = 0; j < n_outputs; j++)
2360 if (STACK_REG_P (recog_operand[j]) && REGNO (recog_operand[j]) == i)
2361 {
2362 regstack->reg[++regstack->top] = i;
2363 SET_HARD_REG_BIT (regstack->reg_set, i);
2364 break;
2365 }
2366 }
2367
2368 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2369 input that the asm didn't implicitly pop. If the asm didn't
2370 implicitly pop an input reg, that reg will still be live.
2371
2372 Note that we can't use find_regno_note here: the register numbers
2373 in the death notes have already been substituted. */
2374
2375 for (i = 0; i < n_outputs; i++)
2376 if (STACK_REG_P (recog_operand[i]))
2377 {
2378 int j;
2379
2380 for (j = 0; j < n_notes; j++)
2381 if (REGNO (recog_operand[i]) == REGNO (note_reg[j])
2382 && note_kind[j] == REG_UNUSED)
2383 {
2384 insn = emit_pop_insn (insn, regstack, recog_operand[i],
2385 emit_insn_after);
2386 break;
2387 }
2388 }
2389
2390 for (i = n_outputs; i < n_outputs + n_inputs; i++)
2391 if (STACK_REG_P (recog_operand[i]))
2392 {
2393 int j;
2394
2395 for (j = 0; j < n_notes; j++)
2396 if (REGNO (recog_operand[i]) == REGNO (note_reg[j])
2397 && note_kind[j] == REG_DEAD
2398 && TEST_HARD_REG_BIT (regstack->reg_set,
2399 REGNO (recog_operand[i])))
2400 {
2401 insn = emit_pop_insn (insn, regstack, recog_operand[i],
2402 emit_insn_after);
2403 break;
2404 }
2405 }
2406 }
2407 \f
2408 /* Substitute stack hard reg numbers for stack virtual registers in
2409 INSN. Non-stack register numbers are not changed. REGSTACK is the
2410 current stack content. Insns may be emitted as needed to arrange the
2411 stack for the 387 based on the contents of the insn. */
2412
2413 static void
2414 subst_stack_regs (insn, regstack)
2415 rtx insn;
2416 stack regstack;
2417 {
2418 register rtx *note_link, note;
2419 register int i;
2420
2421 if (GET_CODE (insn) == CALL_INSN)
2422 {
2423 int top = regstack->top;
2424
2425 /* If there are any floating point parameters to be passed in
2426 registers for this call, make sure they are in the right
2427 order. */
2428
2429 if (top >= 0)
2430 {
2431 straighten_stack (PREV_INSN (insn), regstack);
2432
2433 /* Now mark the arguments as dead after the call. */
2434
2435 while (regstack->top >= 0)
2436 {
2437 CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
2438 regstack->top--;
2439 }
2440 }
2441 }
2442
2443 /* Do the actual substitution if any stack regs are mentioned.
2444 Since we only record whether entire insn mentions stack regs, and
2445 subst_stack_regs_pat only works for patterns that contain stack regs,
2446 we must check each pattern in a parallel here. A call_value_pop could
2447 fail otherwise. */
2448
2449 if (GET_MODE (insn) == QImode)
2450 {
2451 int n_operands = asm_noperands (PATTERN (insn));
2452 if (n_operands >= 0)
2453 {
2454 /* This insn is an `asm' with operands. Decode the operands,
2455 decide how many are inputs, and do register substitution.
2456 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2457
2458 subst_asm_stack_regs (insn, regstack);
2459 return;
2460 }
2461
2462 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2463 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2464 {
2465 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2466 subst_stack_regs_pat (insn, regstack,
2467 XVECEXP (PATTERN (insn), 0, i));
2468 }
2469 else
2470 subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2471 }
2472
2473 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2474 REG_UNUSED will already have been dealt with, so just return. */
2475
2476 if (GET_CODE (insn) == NOTE)
2477 return;
2478
2479 /* If there is a REG_UNUSED note on a stack register on this insn,
2480 the indicated reg must be popped. The REG_UNUSED note is removed,
2481 since the form of the newly emitted pop insn references the reg,
2482 making it no longer `unset'. */
2483
2484 note_link = &REG_NOTES(insn);
2485 for (note = *note_link; note; note = XEXP (note, 1))
2486 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2487 {
2488 *note_link = XEXP (note, 1);
2489 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), emit_insn_after);
2490 }
2491 else
2492 note_link = &XEXP (note, 1);
2493 }
2494 \f
2495 /* Change the organization of the stack so that it fits a new basic
2496 block. Some registers might have to be popped, but there can never be
2497 a register live in the new block that is not now live.
2498
2499 Insert any needed insns before or after INSN. WHEN is emit_insn_before
2500 or emit_insn_after. OLD is the original stack layout, and NEW is
2501 the desired form. OLD is updated to reflect the code emitted, ie, it
2502 will be the same as NEW upon return.
2503
2504 This function will not preserve block_end[]. But that information
2505 is no longer needed once this has executed. */
2506
2507 static void
2508 change_stack (insn, old, new, when)
2509 rtx insn;
2510 stack old;
2511 stack new;
2512 rtx (*when)();
2513 {
2514 int reg;
2515
2516 /* We will be inserting new insns "backwards", by calling emit_insn_before.
2517 If we are to insert after INSN, find the next insn, and insert before
2518 it. */
2519
2520 if (when == emit_insn_after)
2521 insn = NEXT_INSN (insn);
2522
2523 /* Pop any registers that are not needed in the new block. */
2524
2525 for (reg = old->top; reg >= 0; reg--)
2526 if (! TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2527 emit_pop_insn (insn, old, FP_MODE_REG (old->reg[reg], DFmode),
2528 emit_insn_before);
2529
2530 if (new->top == -2)
2531 {
2532 /* If the new block has never been processed, then it can inherit
2533 the old stack order. */
2534
2535 new->top = old->top;
2536 bcopy (old->reg, new->reg, sizeof (new->reg));
2537 }
2538 else
2539 {
2540 /* This block has been entered before, and we must match the
2541 previously selected stack order. */
2542
2543 /* By now, the only difference should be the order of the stack,
2544 not their depth or liveliness. */
2545
2546 GO_IF_HARD_REG_EQUAL (old->reg_set, new->reg_set, win);
2547
2548 abort ();
2549
2550 win:
2551
2552 if (old->top != new->top)
2553 abort ();
2554
2555 /* Loop here emitting swaps until the stack is correct. The
2556 worst case number of swaps emitted is N + 2, where N is the
2557 depth of the stack. In some cases, the reg at the top of
2558 stack may be correct, but swapped anyway in order to fix
2559 other regs. But since we never swap any other reg away from
2560 its correct slot, this algorithm will converge. */
2561
2562 do
2563 {
2564 /* Swap the reg at top of stack into the position it is
2565 supposed to be in, until the correct top of stack appears. */
2566
2567 while (old->reg[old->top] != new->reg[new->top])
2568 {
2569 for (reg = new->top; reg >= 0; reg--)
2570 if (new->reg[reg] == old->reg[old->top])
2571 break;
2572
2573 if (reg == -1)
2574 abort ();
2575
2576 emit_swap_insn (insn, old,
2577 FP_MODE_REG (old->reg[reg], DFmode));
2578 }
2579
2580 /* See if any regs remain incorrect. If so, bring an
2581 incorrect reg to the top of stack, and let the while loop
2582 above fix it. */
2583
2584 for (reg = new->top; reg >= 0; reg--)
2585 if (new->reg[reg] != old->reg[reg])
2586 {
2587 emit_swap_insn (insn, old,
2588 FP_MODE_REG (old->reg[reg], DFmode));
2589 break;
2590 }
2591 } while (reg >= 0);
2592
2593 /* At this point there must be no differences. */
2594
2595 for (reg = old->top; reg >= 0; reg--)
2596 if (old->reg[reg] != new->reg[reg])
2597 abort ();
2598 }
2599 }
2600 \f
2601 /* Check PAT, which points to RTL in INSN, for a LABEL_REF. If it is
2602 found, ensure that a jump from INSN to the code_label to which the
2603 label_ref points ends up with the same stack as that at the
2604 code_label. Do this by inserting insns just before the code_label to
2605 pop and rotate the stack until it is in the correct order. REGSTACK
2606 is the order of the register stack in INSN.
2607
2608 Any code that is emitted here must not be later processed as part
2609 of any block, as it will already contain hard register numbers. */
2610
2611 static void
2612 goto_block_pat (insn, regstack, pat)
2613 rtx insn;
2614 stack regstack;
2615 rtx pat;
2616 {
2617 rtx label;
2618 rtx new_jump, new_label, new_barrier;
2619 rtx *ref;
2620 stack label_stack;
2621 struct stack_def temp_stack;
2622 int reg;
2623
2624 switch (GET_CODE (pat))
2625 {
2626 case RETURN:
2627 straighten_stack (PREV_INSN (insn), regstack);
2628 return;
2629 default:
2630 {
2631 int i, j;
2632 char *fmt = GET_RTX_FORMAT (GET_CODE (pat));
2633
2634 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
2635 {
2636 if (fmt[i] == 'e')
2637 goto_block_pat (insn, regstack, XEXP (pat, i));
2638 if (fmt[i] == 'E')
2639 for (j = 0; j < XVECLEN (pat, i); j++)
2640 goto_block_pat (insn, regstack, XVECEXP (pat, i, j));
2641 }
2642 return;
2643 }
2644 case LABEL_REF:;
2645 }
2646
2647 label = XEXP (pat, 0);
2648 if (GET_CODE (label) != CODE_LABEL)
2649 abort ();
2650
2651 /* First, see if in fact anything needs to be done to the stack at all. */
2652 if (INSN_UID (label) <= 0)
2653 return;
2654
2655 label_stack = &block_stack_in[BLOCK_NUM (label)];
2656
2657 if (label_stack->top == -2)
2658 {
2659 /* If the target block hasn't had a stack order selected, then
2660 we need merely ensure that no pops are needed. */
2661
2662 for (reg = regstack->top; reg >= 0; reg--)
2663 if (! TEST_HARD_REG_BIT (label_stack->reg_set, regstack->reg[reg]))
2664 break;
2665
2666 if (reg == -1)
2667 {
2668 /* change_stack will not emit any code in this case. */
2669
2670 change_stack (label, regstack, label_stack, emit_insn_after);
2671 return;
2672 }
2673 }
2674 else if (label_stack->top == regstack->top)
2675 {
2676 for (reg = label_stack->top; reg >= 0; reg--)
2677 if (label_stack->reg[reg] != regstack->reg[reg])
2678 break;
2679
2680 if (reg == -1)
2681 return;
2682 }
2683
2684 /* At least one insn will need to be inserted before label. Insert
2685 a jump around the code we are about to emit. Emit a label for the new
2686 code, and point the original insn at this new label. We can't use
2687 redirect_jump here, because we're using fld[4] of the code labels as
2688 LABEL_REF chains, no NUSES counters. */
2689
2690 new_jump = emit_jump_insn_before (gen_jump (label), label);
2691 record_label_references (new_jump, PATTERN (new_jump));
2692 JUMP_LABEL (new_jump) = label;
2693
2694 new_barrier = emit_barrier_after (new_jump);
2695
2696 new_label = gen_label_rtx ();
2697 emit_label_after (new_label, new_barrier);
2698 LABEL_REFS (new_label) = new_label;
2699
2700 /* The old label_ref will no longer point to the code_label if now uses,
2701 so strip the label_ref from the code_label's chain of references. */
2702
2703 for (ref = &LABEL_REFS (label); *ref != label; ref = &LABEL_NEXTREF (*ref))
2704 if (*ref == pat)
2705 break;
2706
2707 if (*ref == label)
2708 abort ();
2709
2710 *ref = LABEL_NEXTREF (*ref);
2711
2712 XEXP (pat, 0) = new_label;
2713 record_label_references (insn, PATTERN (insn));
2714
2715 if (JUMP_LABEL (insn) == label)
2716 JUMP_LABEL (insn) = new_label;
2717
2718 /* Now emit the needed code. */
2719
2720 temp_stack = *regstack;
2721
2722 change_stack (new_label, &temp_stack, label_stack, emit_insn_after);
2723 }
2724 \f
2725 /* Traverse all basic blocks in a function, converting the register
2726 references in each insn from the "flat" register file that gcc uses, to
2727 the stack-like registers the 387 uses. */
2728
2729 static void
2730 convert_regs ()
2731 {
2732 register int block, reg;
2733 register rtx insn, next;
2734 struct stack_def regstack;
2735
2736 for (block = 0; block < blocks; block++)
2737 {
2738 if (block_stack_in[block].top == -2)
2739 {
2740 /* This block has not been previously encountered. Choose a
2741 default mapping for any stack regs live on entry */
2742
2743 block_stack_in[block].top = -1;
2744
2745 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
2746 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, reg))
2747 block_stack_in[block].reg[++block_stack_in[block].top] = reg;
2748 }
2749
2750 /* Process all insns in this block. Keep track of `next' here,
2751 so that we don't process any insns emitted while making
2752 substitutions in INSN. */
2753
2754 next = block_begin[block];
2755 regstack = block_stack_in[block];
2756 do
2757 {
2758 insn = next;
2759 next = NEXT_INSN (insn);
2760
2761 /* Don't bother processing unless there is a stack reg
2762 mentioned or if it's a CALL_INSN (register passing of
2763 floating point values). */
2764
2765 if (GET_MODE (insn) == QImode || GET_CODE (insn) == CALL_INSN)
2766 subst_stack_regs (insn, &regstack);
2767
2768 } while (insn != block_end[block]);
2769
2770 /* For all further actions, INSN needs to be the last insn in
2771 this basic block. If subst_stack_regs inserted additional
2772 instructions after INSN, it is no longer the last one at
2773 this point. */
2774 next = PREV_INSN (next);
2775
2776 /* If subst_stack_regs inserted something after a JUMP_INSN, that
2777 is almost certainly a bug. */
2778 if (GET_CODE (insn) == JUMP_INSN && insn != next)
2779 abort ();
2780 insn = next;
2781
2782 /* Something failed if the stack life doesn't match. */
2783
2784 GO_IF_HARD_REG_EQUAL (regstack.reg_set, block_out_reg_set[block], win);
2785
2786 abort ();
2787
2788 win:
2789
2790 /* Adjust the stack of this block on exit to match the stack of
2791 the target block, or copy stack information into stack of
2792 jump target if the target block's stack order hasn't been set
2793 yet. */
2794
2795 if (GET_CODE (insn) == JUMP_INSN)
2796 goto_block_pat (insn, &regstack, PATTERN (insn));
2797
2798 /* Likewise handle the case where we fall into the next block. */
2799
2800 if ((block < blocks - 1) && block_drops_in[block+1])
2801 change_stack (insn, &regstack, &block_stack_in[block+1],
2802 emit_insn_after);
2803 }
2804
2805 /* If the last basic block is the end of a loop, and that loop has
2806 regs live at its start, then the last basic block will have regs live
2807 at its end that need to be popped before the function returns. */
2808
2809 {
2810 int value_reg_low, value_reg_high;
2811 value_reg_low = value_reg_high = -1;
2812 {
2813 rtx retvalue;
2814 if ((retvalue = stack_result (current_function_decl)))
2815 {
2816 value_reg_low = REGNO (retvalue);
2817 value_reg_high = value_reg_low +
2818 HARD_REGNO_NREGS (value_reg_low, GET_MODE (retvalue)) - 1;
2819 }
2820
2821 }
2822 for (reg = regstack.top; reg >= 0; reg--)
2823 if (regstack.reg[reg] < value_reg_low
2824 || regstack.reg[reg] > value_reg_high)
2825 insn = emit_pop_insn (insn, &regstack,
2826 FP_MODE_REG (regstack.reg[reg], DFmode),
2827 emit_insn_after);
2828 }
2829 straighten_stack (insn, &regstack);
2830 }
2831 \f
2832 /* Check expression PAT, which is in INSN, for label references. if
2833 one is found, print the block number of destination to FILE. */
2834
2835 static void
2836 print_blocks (file, insn, pat)
2837 FILE *file;
2838 rtx insn, pat;
2839 {
2840 register RTX_CODE code = GET_CODE (pat);
2841 register int i;
2842 register char *fmt;
2843
2844 if (code == LABEL_REF)
2845 {
2846 register rtx label = XEXP (pat, 0);
2847
2848 if (GET_CODE (label) != CODE_LABEL)
2849 abort ();
2850
2851 fprintf (file, " %d", BLOCK_NUM (label));
2852
2853 return;
2854 }
2855
2856 fmt = GET_RTX_FORMAT (code);
2857 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2858 {
2859 if (fmt[i] == 'e')
2860 print_blocks (file, insn, XEXP (pat, i));
2861 if (fmt[i] == 'E')
2862 {
2863 register int j;
2864 for (j = 0; j < XVECLEN (pat, i); j++)
2865 print_blocks (file, insn, XVECEXP (pat, i, j));
2866 }
2867 }
2868 }
2869 \f
2870 /* Write information about stack registers and stack blocks into FILE.
2871 This is part of making a debugging dump. */
2872
2873 static void
2874 dump_stack_info (file)
2875 FILE *file;
2876 {
2877 register int block;
2878
2879 fprintf (file, "\n%d stack blocks.\n", blocks);
2880 for (block = 0; block < blocks; block++)
2881 {
2882 register rtx head, jump, end;
2883 register int regno;
2884
2885 fprintf (file, "\nStack block %d: first insn %d, last %d.\n",
2886 block, INSN_UID (block_begin[block]),
2887 INSN_UID (block_end[block]));
2888
2889 head = block_begin[block];
2890
2891 fprintf (file, "Reached from blocks: ");
2892 if (GET_CODE (head) == CODE_LABEL)
2893 for (jump = LABEL_REFS (head);
2894 jump != head;
2895 jump = LABEL_NEXTREF (jump))
2896 {
2897 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
2898 fprintf (file, " %d", from_block);
2899 }
2900 if (block_drops_in[block])
2901 fprintf (file, " previous");
2902
2903 fprintf (file, "\nlive stack registers on block entry: ");
2904 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
2905 {
2906 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, regno))
2907 fprintf (file, "%d ", regno);
2908 }
2909
2910 fprintf (file, "\nlive stack registers on block exit: ");
2911 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
2912 {
2913 if (TEST_HARD_REG_BIT (block_out_reg_set[block], regno))
2914 fprintf (file, "%d ", regno);
2915 }
2916
2917 end = block_end[block];
2918
2919 fprintf (file, "\nJumps to blocks: ");
2920 if (GET_CODE (end) == JUMP_INSN)
2921 print_blocks (file, end, PATTERN (end));
2922
2923 if (block + 1 < blocks && block_drops_in[block+1])
2924 fprintf (file, " next");
2925 else if (block + 1 == blocks
2926 || (GET_CODE (end) == JUMP_INSN
2927 && GET_CODE (PATTERN (end)) == RETURN))
2928 fprintf (file, " return");
2929
2930 fprintf (file, "\n");
2931 }
2932 }
2933 #endif /* STACK_REGS */