Initial revision
[gcc.git] / gcc / reg-stack.c
1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1990 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* This pass converts stack-like registers from the "flat register
21 file" model that gcc uses, to a stack convention that the 387 uses.
22
23 * The form of the input:
24
25 On input, the function consists of insn that have had their
26 registers fully allocated to a set of "virtual" registers. Note that
27 the word "virtual" is used differently here than elsewhere in gcc: for
28 each virtual stack reg, there is a hard reg, but the mapping between
29 them is not known until this pass is run. On output, hard register
30 numbers have been substituted, and various pop and exchange insns have
31 been emitted. The hard register numbers and the virtual register
32 numbers completely overlap - before this pass, all stack register
33 numbers are virtual, and afterward they are all hard, with the
34 exception of ASM_OPERANDS, which are discussed below.
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 both the SET_SRC and SET_DEST FIRST_STACK_REG
63 appears ambiguous. As a special case, the presence of a REG_DEAD note
64 for FIRST_STACK_REG differentiates between a load insn and a pop.
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 * Limitations:
85
86 Inline assembly isn't handled yet. */
87 \f
88 #include <stdio.h>
89 #include "config.h"
90 #include "tree.h"
91 #include "rtl.h"
92 #include "regs.h"
93 #include "hard-reg-set.h"
94 #include "flags.h"
95
96 #ifdef STACK_REGS
97
98 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
99
100 /* True if the current function returns a real value. */
101 static int current_function_returns_real;
102
103 /* This is the basic stack record. TOP is an index into REG[] such
104 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
105
106 If TOP is -2 the stack is not yet initialized: reg_set indicates
107 which registers are live. Stack initialization consists of placing
108 each live reg in array `reg' and setting `top' appropriately. */
109
110 typedef struct stack_def
111 {
112 int top; /* index to top stack element */
113 HARD_REG_SET reg_set; /* set of live registers */
114 char reg[REG_STACK_SIZE]; /* register - stack mapping */
115 } *stack;
116
117 /* highest instruction uid */
118 static int max_uid = 0;
119
120 /* Number of basic blocks in the current function. */
121 static int blocks;
122
123 /* Element N is first insn in basic block N.
124 This info lasts until we finish compiling the function. */
125 static rtx *block_begin;
126
127 /* Element N is last insn in basic block N.
128 This info lasts until we finish compiling the function. */
129 static rtx *block_end;
130
131 /* Element N is nonzero if control can drop into basic block N */
132 static char *block_drops_in;
133
134 /* Element N says all about the stack at entry block N */
135 static stack block_stack_in;
136
137 /* Element N says all about the stack life at the end of block N */
138 static HARD_REG_SET *block_out_reg_set;
139
140 /* This is where the BLOCK_NUM values are really stored. This is set
141 up by find_blocks and used there and in life_analysis. It can be used
142 later, but only to look up an insn that is the head or tail of some
143 block. life_analysis and the stack register conversion process can
144 add insns within a block. */
145 static short *block_number;
146
147 /* This is the register file for all register after conversion */
148 static rtx SFmode_reg[FIRST_PSEUDO_REGISTER];
149 static rtx DFmode_reg[FIRST_PSEUDO_REGISTER];
150
151 /* ??? set of register to delete after ASM_OPERAND */
152 HARD_REG_SET asm_regs;
153
154 /* Get the basic block number of an insn. See note at block_number
155 definition are validity of this information. */
156
157 #define BLOCK_NUM(INSN) \
158 (((INSN_UID (INSN) > max_uid) \
159 ? (short *)(abort() , 0) \
160 : block_number)[INSN_UID (INSN)])
161
162 extern rtx gen_jump ();
163 extern rtx gen_movdf ();
164 extern rtx find_regno_note ();
165 extern rtx emit_jump_insn_before ();
166 extern rtx emit_label_after ();
167
168 extern rtx dconst0_rtx;
169
170 /* Forward declarations */
171
172 static void find_blocks ();
173 static void stack_reg_life_analysis ();
174 static void convert_regs ();
175 static void dump_stack_info ();
176 static void fatal_for_asm ();
177 \f
178 \f
179 /* Return non-zero if any stack register is mentioned somewhere within
180 PAT. */
181
182 static int
183 stack_regs_mentioned_p (pat)
184 register rtx pat;
185 {
186 register char *fmt;
187 register int i;
188
189 if (STACK_REG_P (pat))
190 return 1;
191
192 fmt = GET_RTX_FORMAT (GET_CODE (pat));
193 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
194 {
195 if (fmt[i] == 'E')
196 {
197 register int j;
198
199 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
200 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
201 return 1;
202 }
203 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
204 return 1;
205 }
206
207 return 0;
208 }
209 \f
210 /* Convert register usage from "flat" register file usage to a "stack
211 register file. FIRST is the first insn in the function, FILE is the
212 dump file, if used.
213
214 First compute the beginning and end of each basic block. Do a
215 register life analysis on the stack registers, recording the result
216 for the head and tail of each basic block. The convert each insn one
217 by one. Run a last jump_optimize() pass, if optimizing, to eliminate
218 any cross-jumping created when the converter inserts pop insns.*/
219
220 void
221 reg_to_stack (first, file)
222 rtx first;
223 FILE *file;
224 {
225 register rtx insn;
226 register int i;
227 int stack_reg_seen = 0;
228
229 current_function_returns_real
230 = TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl))) == REAL_TYPE;
231
232 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
233 {
234 SFmode_reg[i] = gen_rtx (REG, SFmode, i);
235 DFmode_reg[i] = gen_rtx (REG, DFmode, i);
236 }
237
238 /* Count the basic blocks. Also find maximum insn uid. */
239 {
240 register RTX_CODE prev_code = JUMP_INSN;
241 register RTX_CODE code;
242
243 max_uid = 0;
244 blocks = 0;
245 for (insn = first; insn; insn = NEXT_INSN (insn))
246 {
247 /* Note that this loop must select the same block boundaries
248 as code in find_blocks. */
249
250 if (INSN_UID (insn) > max_uid)
251 max_uid = INSN_UID (insn);
252
253 code = GET_CODE (insn);
254
255 if (code == CODE_LABEL
256 || (prev_code != INSN
257 && prev_code != CALL_INSN
258 && prev_code != CODE_LABEL
259 && (code == INSN || code == CALL_INSN || code == JUMP_INSN)))
260 blocks++;
261
262 /* Remember whether or not this insn mentions an FP regs.
263 Check JUMP_INSNs too, in case someone creates a funny PARALLEL. */
264
265 if ((GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
266 || GET_CODE (insn) == JUMP_INSN)
267 && stack_regs_mentioned_p (PATTERN (insn)))
268 {
269 stack_reg_seen = 1;
270 PUT_MODE (insn, QImode);
271 }
272 else
273 PUT_MODE (insn, VOIDmode);
274
275 if (code != NOTE)
276 prev_code = code;
277 }
278 }
279
280 /* If no stack register reference exists in this insn, there isn't
281 anything to convert. */
282
283 if (! stack_reg_seen)
284 return;
285
286 /* If there are stack registers, there must be at least one block. */
287
288 if (! blocks)
289 abort ();
290
291 /* Allocate some tables that last till end of compiling this function
292 and some needed only in find_blocks and life_analysis. */
293
294 block_begin = (rtx *) alloca (blocks * sizeof (rtx));
295 block_end = (rtx *) alloca (blocks * sizeof (rtx));
296 block_drops_in = (char *) alloca (blocks);
297
298 block_stack_in = (stack) alloca (blocks * sizeof (struct stack_def));
299 block_out_reg_set = (HARD_REG_SET *) alloca (blocks * sizeof (HARD_REG_SET));
300 bzero (block_stack_in, blocks * sizeof (struct stack_def));
301 bzero (block_out_reg_set, blocks * sizeof (HARD_REG_SET));
302
303 block_number = (short *) alloca ((max_uid + 1) * sizeof (short));
304
305 find_blocks (first);
306 stack_reg_life_analysis (first);
307
308 /* Dump the life analysis debug information before jump
309 optimization, as that will destroy the LABEL_REFS we keep the
310 information in. */
311
312 if (file)
313 dump_stack_info (file);
314
315 convert_regs ();
316
317 if (optimize)
318 jump_optimize (first, 2, 0, 0);
319 }
320 \f
321 /* Check PAT, which is in INSN, for LABEL_REFs. Add INSN to the
322 label's chain of references, and note which insn contains each
323 reference. */
324
325 static void
326 record_label_references (insn, pat)
327 rtx insn, pat;
328 {
329 register enum rtx_code code = GET_CODE (pat);
330 register int i;
331 register char *fmt;
332
333 if (code == LABEL_REF)
334 {
335 register rtx label = XEXP (pat, 0);
336 register rtx ref;
337
338 if (GET_CODE (label) != CODE_LABEL)
339 abort ();
340
341 /* Don't make a duplicate in the code_label's chain. */
342
343 for (ref = LABEL_REFS (label); ref != label; ref = LABEL_NEXTREF (ref))
344 if (CONTAINING_INSN (ref) == insn)
345 return;
346
347 CONTAINING_INSN (pat) = insn;
348 LABEL_NEXTREF (pat) = LABEL_REFS (label);
349 LABEL_REFS (label) = pat;
350
351 return;
352 }
353
354 fmt = GET_RTX_FORMAT (code);
355 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
356 {
357 if (fmt[i] == 'e')
358 record_label_references (insn, XEXP (pat, i));
359 if (fmt[i] == 'E')
360 {
361 register int j;
362 for (j = 0; j < XVECLEN (pat, i); j++)
363 record_label_references (insn, XVECEXP (pat, i, j));
364 }
365 }
366 }
367 \f
368 /* Return a pointer to the REG expression within PAT. If PAT is not a
369 REG, possible enclosed by a conversion rtx, return the inner part of
370 PAT that stopped the search. */
371
372 static rtx *
373 get_true_reg (pat)
374 rtx *pat;
375 {
376 while (GET_CODE (*pat) == SUBREG
377 || GET_CODE (*pat) == FLOAT
378 || GET_CODE (*pat) == FIX
379 || GET_CODE (*pat) == FLOAT_EXTEND
380 || GET_CODE (*pat) == FLOAT_TRUNCATE)
381 pat = & XEXP (*pat, 0);
382
383 return pat;
384 }
385
386 /* If REG is a stack register that is marked dead in REGSTACK, then
387 record that it is now live. If REG is not DEST, add a death note to
388 INSN if there isn't one already. If DEST is not a reg, it is safe to
389 assume that it does not mention a reg anywhere within. */
390
391 static void
392 record_note_if_dead (insn, regstack, reg, dest)
393 rtx insn;
394 stack regstack;
395 rtx reg, dest;
396 {
397 reg = * get_true_reg (& reg);
398
399 if (STACK_REG_P (reg))
400 {
401 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (reg)))
402 {
403 if ((! REG_P (dest) || REGNO (dest) != REGNO (reg))
404 && ! find_regno_note (insn, REG_DEAD, REGNO (reg)))
405 REG_NOTES (insn) = gen_rtx (EXPR_LIST,
406 REG_DEAD, reg, REG_NOTES (insn));
407
408 SET_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
409 }
410 }
411 else
412 if (stack_regs_mentioned_p (reg))
413 abort ();
414 }
415 \f
416 /* Scan PAT, which is part of INSN, and record the life & death of
417 stack registers in REGSTACK. If a register was dead, but is an input
418 operand in this insn, then mark the register live and record a death
419 note.
420
421 If a register is dead after this insn, but is an output operand in
422 this insn, record a REG_UNUSED note.
423
424 This function does not know about SET_DESTs that are both input and
425 output (such as ZERO_EXTRACT) - this cannot happen on a 387. */
426
427 static void
428 record_reg_life_pat (insn, regstack, pat)
429 rtx insn;
430 stack regstack;
431 rtx pat;
432 {
433 rtx src, dest;
434
435 if (GET_CODE (pat) == CLOBBER
436 && GET_CODE (PATTERN (insn)) == PARALLEL
437 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == ASM_OPERANDS)
438 {
439 if (STACK_REG_P (XEXP (pat, 0)))
440 abort ();
441 return;
442 }
443
444 if (GET_CODE (pat) != SET)
445 return;
446
447 dest = * get_true_reg (& SET_DEST (pat));
448
449 /* The destination is dead before this insn. If the destination is
450 not used after this insn, record this with REG_UNUSED. */
451
452 if (STACK_REG_P (dest))
453 {
454 /* ??? This check is unnecessary. */
455
456 if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
457 abort ();
458
459 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (dest)))
460 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_UNUSED, dest,
461 REG_NOTES (insn));
462
463 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
464 }
465 else
466 if (dest != cc0_rtx && stack_regs_mentioned_p (dest))
467 abort ();
468
469 src = * get_true_reg (& SET_SRC (pat));
470
471 switch (GET_CODE (src))
472 {
473 /* ??? get_true_reg will make some of these cases redundant. */
474
475 case PLUS:
476 case MINUS:
477 case MULT:
478 case DIV:
479 case COMPARE:
480 record_note_if_dead (insn, regstack, XEXP (src, 0), dest);
481 record_note_if_dead (insn, regstack, XEXP (src, 1), dest);
482 break;
483
484 case ABS:
485 case NEG:
486 case SQRT:
487 case FLOAT_EXTEND:
488 case FLOAT_TRUNCATE:
489 case FLOAT:
490 case UNSIGNED_FLOAT:
491 record_note_if_dead (insn, regstack, XEXP (src, 0), dest);
492 break;
493
494 case UNSIGNED_FIX:
495 case FIX:
496 src = XEXP (src, 0);
497 if (GET_CODE (src) == FIX)
498 record_note_if_dead (insn, regstack, XEXP (src, 0), dest);
499 else
500 record_note_if_dead (insn, regstack, src, dest);
501 break;
502
503 case ASM_OPERANDS:
504 {
505 register int j;
506
507 /* ??? This needs much improvement */
508
509 if (stack_regs_mentioned_p (pat))
510 abort ();
511
512 for (j = 0; j < XVECLEN (src, 3); j++)
513 record_note_if_dead (insn, regstack, XVECEXP (src, 3, j), dest);
514 }
515 break;
516
517 case REG:
518 record_note_if_dead (insn, regstack, src, dest);
519 break;
520
521 default:
522 /* If a stack register appears in the src RTL, it is a bug, and
523 code should be added above to handle it. */
524
525 if (stack_regs_mentioned_p (src))
526 abort ();
527 }
528 }
529 \f
530 /* Scan INSN, which is in BLOCK, and record the life & death of stack
531 registers in REGSTACK. This function is called to process insns from
532 the last insn in a block to the first. The actual scanning is done in
533 record_reg_life_pat.
534
535 If a register is live after a CALL_INSN, but is not a value return
536 register for that CALL_INSN, then code is emitted to initialize that
537 register. The block_end[] data is kept accurate.
538
539 Existing death and unset notes for stack registers are deleted
540 before processing the insn. */
541
542 static void
543 record_reg_life (insn, block, regstack)
544 rtx insn;
545 int block;
546 stack regstack;
547 {
548 rtx note, *note_link;
549
550 if ((GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
551 || INSN_DELETED_P (insn))
552 return;
553
554 /* Strip death notes for stack regs from this insn */
555
556 note_link = &REG_NOTES(insn);
557 for (note = *note_link; note; note = XEXP (note, 1))
558 if (STACK_REG_P (XEXP (note, 0))
559 && (REG_NOTE_KIND (note) == REG_DEAD
560 || REG_NOTE_KIND (note) == REG_UNUSED))
561 *note_link = XEXP (note, 1);
562 else
563 note_link = &XEXP (note, 1);
564
565 /* Process all patterns in the insn. */
566
567 if (GET_CODE (PATTERN (insn)) == PARALLEL)
568 {
569 register int i;
570
571 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
572 record_reg_life_pat (insn, regstack, XVECEXP (PATTERN (insn), 0, i));
573 }
574 else if (GET_MODE (insn) == QImode)
575 record_reg_life_pat (insn, regstack, PATTERN (insn));
576
577 /* There might be a reg that is live after a function call.
578 Initialize it to zero so that the program does not crash. See comment
579 towards the end of stack_reg_life_analysis(). */
580
581 if (GET_CODE (insn) == CALL_INSN)
582 {
583 int reg = FIRST_FLOAT_REG;
584
585 /* If a stack reg is mentioned in a CALL_INSN, it must be as the
586 return value; conversely, if a float is returned, a stack reg
587 must be mentioned. */
588
589 if (stack_regs_mentioned_p (PATTERN (insn)))
590 reg++;
591
592 for (; reg <= LAST_STACK_REG; reg++)
593 if (TEST_HARD_REG_BIT (regstack->reg_set, reg))
594 {
595 rtx init, pat;
596
597 /* The insn will use virtual register numbers, and so
598 convert_regs is expected to process these. But BLOCK_NUM
599 cannot be used on these insns, because they do not appear in
600 block_number[]. */
601
602 pat = gen_rtx (SET, VOIDmode, DFmode_reg[reg], dconst0_rtx);
603 init = emit_insn_after (pat, insn);
604 PUT_MODE (init, QImode);
605
606 CLEAR_HARD_REG_BIT (regstack->reg_set, reg);
607
608 /* If the CALL_INSN was the end of a block, move the
609 block_end to point to the new insn. */
610
611 if (block_end[block] == insn)
612 block_end[block] = init;
613 }
614
615 /* Some regs do not survive a CALL */
616
617 AND_COMPL_HARD_REG_SET (regstack->reg_set, call_used_reg_set);
618 }
619 }
620 \f
621 /* Find all basic blocks of the function, which starts with FIRST.
622 For each JUMP_INSN, build the chain of LABEL_REFS on each CODE_LABEL. */
623
624 static void
625 find_blocks (first)
626 rtx first;
627 {
628 register rtx insn;
629 register int block;
630 register RTX_CODE prev_code = BARRIER;
631 register RTX_CODE code;
632
633 /* Record where all the blocks start and end.
634 Record which basic blocks control can drop in to. */
635
636 block = -1;
637 for (insn = first; insn; insn = NEXT_INSN (insn))
638 {
639 /* Note that this loop must select the same block boundaries
640 as code in reg_to_stack. */
641
642 code = GET_CODE (insn);
643
644 if (code == CODE_LABEL
645 || (prev_code != INSN
646 && prev_code != CALL_INSN
647 && prev_code != CODE_LABEL
648 && (code == INSN || code == CALL_INSN || code == JUMP_INSN)))
649 {
650 block_begin[++block] = insn;
651 block_end[block] = insn;
652 block_drops_in[block] = prev_code != BARRIER;
653 }
654 else if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
655 block_end[block] = insn;
656
657 BLOCK_NUM (insn) = block;
658
659 if (code == CODE_LABEL)
660 LABEL_REFS (insn) = insn; /* delete old chain */
661
662 if (code != NOTE)
663 prev_code = code;
664 }
665
666 if (block + 1 != blocks)
667 abort ();
668
669 /* generate all label references to the correspondending jump insn */
670 for (block = 0; block < blocks; block++)
671 {
672 insn = block_end[block];
673
674 if (GET_CODE (insn) == JUMP_INSN)
675 record_label_references (insn, PATTERN (insn));
676 }
677 }
678 \f
679 /* Determine the which registers are live at the start of each basic
680 block of the function whose first insn is FIRST.
681
682 First, if the function returns a real_type, mark the function
683 return type as live at each return point, as the RTL may not give any
684 hint that the register is live.
685
686 Then, start with the last block and work back to the first block.
687 Similarly, work backwards within each block, insn by insn, recording
688 which regs are die and which are used (and therefore live) in the
689 hard reg set of block_stack_in[].
690
691 After processing each basic block, if there is a label at the start
692 of the block, propagate the live registers to all jumps to this block.
693
694 As a special case, if there are regs live in this block, that are
695 not live in a block containing a jump to this label, and the block
696 containing the jump has already been processed, we must propagate this
697 block's entry register life back to the block containing the jump, and
698 restart life analysis from there.
699
700 In the worst case, this function may traverse the insns
701 REG_STACK_SIZE times. This is necessary, since a jump towards the end
702 of the insns may not know that a reg is live at a target that is early
703 in the insns. So we back up and start over with the new reg live.
704
705 If there are registers that are live at the start of the function,
706 insns are emitted to initialize these registers. Something similar is
707 done after CALL_INSNs in record_reg_life. */
708
709 static void
710 stack_reg_life_analysis (first)
711 rtx first;
712 {
713 int reg, block;
714 struct stack_def regstack;
715
716 if (current_function_returns_real)
717 {
718 /* Find all RETURN insns and mark them. */
719
720 for (block = blocks - 1; block >= 0; block--)
721 if (GET_CODE (block_end[block]) == JUMP_INSN
722 && GET_CODE (PATTERN (block_end[block])) == RETURN)
723 SET_HARD_REG_BIT (block_out_reg_set[block], FIRST_STACK_REG);
724
725 /* Mark of the end of last block if we "fall off" the end of the
726 function into the epilogue. */
727
728 if (GET_CODE (block_end[blocks-1]) != JUMP_INSN
729 || GET_CODE (PATTERN (block_end[blocks-1])) == RETURN)
730 SET_HARD_REG_BIT (block_out_reg_set[blocks-1], FIRST_STACK_REG);
731 }
732
733 /* now scan all blocks backward for stack register use */
734
735 block = blocks - 1;
736 while (block >= 0)
737 {
738 register rtx insn, prev;
739
740 /* current register status at last instruction */
741
742 COPY_HARD_REG_SET (regstack.reg_set, block_out_reg_set[block]);
743
744 prev = block_end[block];
745 do
746 {
747 insn = prev;
748 prev = PREV_INSN (insn);
749
750 /* If the insn is a CALL_INSN, we need to ensure that
751 everything dies. But otherwise don't process unless there
752 are some stack regs present. */
753
754 if (GET_MODE (insn) == QImode || GET_CODE (insn) == CALL_INSN)
755 record_reg_life (insn, block, &regstack);
756
757 } while (insn != block_begin[block]);
758
759 /* Set the state at the start of the block. Mark that no
760 register mapping information known yet. */
761
762 COPY_HARD_REG_SET (block_stack_in[block].reg_set, regstack.reg_set);
763 block_stack_in[block].top = -2;
764
765 /* If there is a label, propagate our register life to all jumps
766 to this label. */
767
768 if (GET_CODE (insn) == CODE_LABEL)
769 {
770 register rtx label;
771 int must_restart = 0;
772
773 for (label = LABEL_REFS (insn); label != insn;
774 label = LABEL_NEXTREF (label))
775 {
776 int jump_block = BLOCK_NUM (CONTAINING_INSN (label));
777
778 if (jump_block < block)
779 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
780 block_stack_in[block].reg_set);
781 else
782 {
783 /* The block containing the jump has already been
784 processed. If there are registers that were not known
785 to be live then, but are live now, we must back up
786 and restart life analysis from that point with the new
787 life information. */
788
789 GO_IF_HARD_REG_SUBSET (block_stack_in[block].reg_set,
790 block_out_reg_set[jump_block],
791 win);
792
793 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
794 block_stack_in[block].reg_set);
795
796 block = jump_block;
797 must_restart = 1;
798
799 win:
800 ;
801 }
802 }
803 if (must_restart)
804 continue;
805 }
806
807 if (block_drops_in[block])
808 IOR_HARD_REG_SET (block_out_reg_set[block-1],
809 block_stack_in[block].reg_set);
810
811 block -= 1;
812 }
813
814 {
815 /* If any reg is live at the start of the first block of a
816 function, then we must guarantee that the reg holds some value by
817 generating our own "load" of that register. Otherwise a 387 would
818 fault trying to access an empty register. */
819
820 HARD_REG_SET empty_regs;
821 CLEAR_HARD_REG_SET (empty_regs);
822 GO_IF_HARD_REG_SUBSET (block_stack_in[0].reg_set, empty_regs,
823 no_live_regs);
824 }
825
826 /* Load zero into each live register. The fact that a register
827 appears live at the function start does not necessarily imply an error
828 in the user program: it merely means that we could not determine that
829 there wasn't such an error, just as -Wunused sometimes gives
830 "incorrect" warnings. In those cases, these initializations will do
831 no harm.
832
833 Note that we are inserting virtual register references here:
834 these insns must be processed by convert_regs later. Also, these
835 insns will not be in block_number, so BLOCK_NUM() will fail for them. */
836
837 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
838 if (TEST_HARD_REG_BIT (block_stack_in[0].reg_set, reg))
839 {
840 rtx init_rtx;
841
842 init_rtx = gen_rtx (SET, VOIDmode, DFmode_reg[reg], dconst0_rtx);
843 block_begin[0] = emit_insn_after (init_rtx, first);
844 PUT_MODE (block_begin[0], QImode);
845
846 CLEAR_HARD_REG_BIT (block_stack_in[0].reg_set, reg);
847 }
848
849 no_live_regs:
850 ;
851 }
852 \f
853 /*****************************************************************************
854 This section deals with stack register substition, and forms the second
855 pass over the RTL.
856 *****************************************************************************/
857
858 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
859 the desired hard REGNO. */
860
861 static void
862 replace_reg (reg, regno)
863 rtx *reg;
864 int regno;
865 {
866 if (regno < FIRST_STACK_REG || regno > LAST_STACK_REG
867 || ! STACK_REG_P (*reg))
868 abort ();
869
870 if (GET_MODE (*reg) == DFmode)
871 *reg = DFmode_reg[regno];
872 else if (GET_MODE (*reg) == SFmode)
873 *reg = SFmode_reg[regno];
874 else
875 abort ();
876 }
877
878 /* Remove a note of type NOTE, which must be found, for register
879 number REGNO from INSN. Remove only one such note. */
880
881 static void
882 remove_regno_note (insn, note, regno)
883 rtx insn;
884 enum reg_note note;
885 int regno;
886 {
887 register rtx *note_link, this;
888
889 note_link = &REG_NOTES(insn);
890 for (this = *note_link; this; this = XEXP (this, 1))
891 if (REG_NOTE_KIND (this) == note
892 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno)
893 {
894 *note_link = XEXP (this, 1);
895 return;
896 }
897 else
898 note_link = &XEXP (this, 1);
899
900 abort ();
901 }
902
903 /* Find the hard register number of virtual register REG in REGSTACK.
904 The hard register number is relative to the top of the stack. -1 is
905 returned if the register is not found. */
906
907 static int
908 get_hard_regnum (regstack, reg)
909 stack regstack;
910 rtx reg;
911 {
912 int i;
913
914 if (! STACK_REG_P (reg))
915 abort ();
916
917 for (i = regstack->top; i >= 0; i--)
918 if (regstack->reg[i] == REGNO (reg))
919 break;
920
921 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
922 }
923
924 /* Delete INSN from the RTL. Mark the insn, but don't remove it from
925 the chain of insns. Doing so could confuse block_begin and block_end
926 if this were the only insn in the block. */
927
928 static void
929 delete_insn_for_stacker (insn)
930 rtx insn;
931 {
932 PUT_CODE (insn, NOTE);
933 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
934 NOTE_SOURCE_FILE (insn) = 0;
935 INSN_DELETED_P (insn) = 1;
936 }
937 \f
938 /* Emit an insn to pop virtual register REG before or after INSN.
939 REGSTACK is the stack state after INSN and is updated to reflect this
940 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
941 is represented as a SET whose destination is the register to be popped
942 and source is the top of stack. A death note for the top of stack
943 cases the movdf pattern to pop. */
944
945 static rtx
946 emit_pop_insn (insn, regstack, reg, when)
947 rtx insn;
948 stack regstack;
949 rtx reg;
950 rtx (*when)();
951 {
952 rtx pop_insn, pop_rtx;
953 int hard_regno;
954
955 hard_regno = get_hard_regnum (regstack, reg);
956
957 if (hard_regno < FIRST_STACK_REG)
958 abort ();
959
960 pop_rtx = gen_rtx (SET, VOIDmode, DFmode_reg[hard_regno],
961 DFmode_reg[FIRST_STACK_REG]);
962
963 pop_insn = (*when) (pop_rtx, insn);
964 PUT_MODE (pop_insn, VOIDmode);
965
966 REG_NOTES (pop_insn) = gen_rtx (EXPR_LIST,
967 REG_DEAD, DFmode_reg[FIRST_STACK_REG],
968 REG_NOTES (pop_insn));
969
970 regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
971 = regstack->reg[regstack->top];
972 regstack->top -= 1;
973 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
974
975 return pop_insn;
976 }
977 \f
978 /* Emit an insn before or after INSN to swap virtual register REG with the
979 top of stack. WHEN should be `emit_insn_before' or `emit_insn_before'
980 REGSTACK is the stack state before the swap, and is updated to reflect
981 the swap. A swap insn is represented as a PARALLEL of two patterns:
982 each pattern moves one reg to the other.
983
984 If REG is already at the top of the stack, no insn is emitted. */
985
986 static void
987 emit_hard_swap_insn (insn, regstack, hard_regno, when)
988 rtx insn;
989 stack regstack;
990 int hard_regno;
991 rtx (*when)();
992 {
993 rtx gen_swapdf();
994 rtx swap_rtx, swap_insn;
995 int tmp, other;
996
997 if (hard_regno == FIRST_STACK_REG)
998 return;
999
1000 swap_rtx = gen_swapdf (DFmode_reg[hard_regno], DFmode_reg[FIRST_STACK_REG]);
1001 swap_insn = (*when) (swap_rtx, insn);
1002 PUT_MODE (swap_insn, VOIDmode);
1003
1004 other = regstack->top - (hard_regno - FIRST_STACK_REG);
1005
1006 tmp = regstack->reg[other];
1007 regstack->reg[other] = regstack->reg[regstack->top];
1008 regstack->reg[regstack->top] = tmp;
1009 }
1010
1011 /* Emit an insn before or after INSN to swap virtual register REG with the
1012 top of stack. See comments before emit_hard_swap_insn. */
1013
1014 static void
1015 emit_swap_insn (insn, regstack, reg, when)
1016 rtx insn;
1017 stack regstack;
1018 rtx reg;
1019 rtx (*when)();
1020 {
1021 int hard_regno;
1022
1023 hard_regno = get_hard_regnum (regstack, reg);
1024
1025 emit_hard_swap_insn (insn, regstack, hard_regno, when);
1026 }
1027 \f
1028 /* Handle a move to or from a stack register in PAT, which is in INSN.
1029 REGSTACK is the current stack. */
1030
1031 static void
1032 move_for_stack_reg (insn, regstack, pat)
1033 rtx insn;
1034 stack regstack;
1035 rtx pat;
1036 {
1037 rtx *src = get_true_reg (&SET_SRC (pat));
1038 rtx *dest = get_true_reg (&SET_DEST (pat));
1039 rtx note;
1040
1041 if (STACK_REG_P (*src) && STACK_REG_P (*dest))
1042 {
1043 /* Write from one stack reg to another. If SRC dies here, then
1044 just change the register mapping and delete the insn. */
1045
1046 note = find_regno_note (insn, REG_DEAD, REGNO (*src));
1047 if (note)
1048 {
1049 int i;
1050
1051 /* If this is a no-op move, there must not be a REG_DEAD note. */
1052 if (REGNO (*src) == REGNO (*dest))
1053 abort ();
1054
1055 for (i = regstack->top; i >= 0; i--)
1056 if (regstack->reg[i] == REGNO (*src))
1057 break;
1058
1059 /* The source must be live, and the dest must be dead. */
1060 if (i < 0 || get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG)
1061 abort ();
1062
1063 /* It is possible that the dest is unused after this insn.
1064 If so, just pop the src. */
1065
1066 if (find_regno_note (insn, REG_UNUSED, REGNO (*dest)))
1067 {
1068 emit_pop_insn (insn, regstack, *src, emit_insn_after);
1069
1070 delete_insn_for_stacker (insn);
1071 return;
1072 }
1073
1074 regstack->reg[i] = REGNO (*dest);
1075
1076 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1077 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src));
1078
1079 delete_insn_for_stacker (insn);
1080
1081 return;
1082 }
1083
1084 /* The source reg does not die. */
1085
1086 /* If this appears to be a no-op move, delete it, or else it
1087 will confuse the machine description output patterns. But if
1088 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1089 for REG_UNUSED will not work for deleted insns. */
1090
1091 if (REGNO (*src) == REGNO (*dest))
1092 {
1093 if (find_regno_note (insn, REG_UNUSED, REGNO (*dest)))
1094 emit_pop_insn (insn, regstack, *dest, emit_insn_after);
1095
1096 delete_insn_for_stacker (insn);
1097 return;
1098 }
1099
1100 /* The destination ought to be dead */
1101 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG)
1102 abort ();
1103
1104 replace_reg (src, get_hard_regnum (regstack, *src));
1105
1106 regstack->reg[++regstack->top] = REGNO (*dest);
1107 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1108 replace_reg (dest, FIRST_STACK_REG);
1109 }
1110 else if (STACK_REG_P (*src))
1111 {
1112 /* Save from a stack reg to MEM, or possibly integer reg. Since
1113 only top of stack may be saved, emit an exchange first if
1114 needs be. */
1115
1116 emit_swap_insn (insn, regstack, *src, emit_insn_before);
1117
1118 note = find_regno_note (insn, REG_DEAD, REGNO (*src));
1119 if (note)
1120 {
1121 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1122 regstack->top--;
1123 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src));
1124 }
1125
1126 replace_reg (src, FIRST_STACK_REG);
1127 }
1128 else if (STACK_REG_P (*dest))
1129 {
1130 /* Load from MEM, or possibly integer REG or constant, into the
1131 stack regs. The actual target is always the top of the
1132 stack. The stack mapping is changed to reflect that DEST is
1133 now at top of stack. */
1134
1135 /* The destination ought to be dead */
1136 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG)
1137 abort ();
1138
1139 if (regstack->top >= REG_STACK_SIZE)
1140 abort ();
1141
1142 regstack->reg[++regstack->top] = REGNO (*dest);
1143 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1144 replace_reg (dest, FIRST_STACK_REG);
1145 }
1146 else
1147 abort ();
1148 }
1149 \f
1150 /* Handle a comparison. Special care needs to be taken to avoid
1151 causing comparisons that a 387 cannot do correctly, such as EQ.
1152
1153 Also, a pop insn may need to be emitted. The 387 does have an
1154 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1155 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1156 set up. */
1157
1158 static void
1159 compare_for_stack_reg (insn, regstack, pat)
1160 rtx insn;
1161 stack regstack;
1162 rtx pat;
1163 {
1164 rtx *src1, *src2;
1165 rtx src1_note, src2_note;
1166
1167 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1168 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
1169
1170 /* The first argument must always be a stack reg. */
1171 /* ??? why? */
1172
1173 if (! STACK_REG_P (*src1))
1174 abort ();
1175
1176 /* We will fix any death note later. */
1177
1178 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1179
1180 if (STACK_REG_P (*src2))
1181 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1182 else
1183 src2_note = 0;
1184
1185 emit_swap_insn (insn, regstack, *src1, emit_insn_before);
1186
1187 replace_reg (src1, FIRST_STACK_REG);
1188
1189 if (STACK_REG_P (*src2))
1190 replace_reg (src2, get_hard_regnum (regstack, *src2));
1191
1192 if (src1_note)
1193 {
1194 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (XEXP (src1_note, 0)));
1195 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1196 regstack->top--;
1197 }
1198
1199 /* If the second operand dies, handle that. But if the operands are
1200 the same stack register, don't bother, because only one death is
1201 needed, and it was just handled. */
1202
1203 if (src2_note
1204 && ! (STACK_REG_P (*src1)
1205 && STACK_REG_P (*src2)
1206 && REGNO (*src1) == REGNO (*src2)))
1207 {
1208 /* As a special case, two regs may die in this insn if src2 is
1209 next to top of stack and the top of stack also dies. Since
1210 we have already popped src1, "next to top of stack" is really
1211 at top (FIRST_STACK_REG) now. */
1212
1213 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1214 && src1_note)
1215 {
1216 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (XEXP (src2_note, 0)));
1217 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1218 regstack->top--;
1219 }
1220 else
1221 {
1222 /* The 386 can only represent death of the first operand in
1223 the case handled above. In all other cases, emit a separate
1224 pop and remove the death note from here. */
1225
1226 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
1227
1228 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
1229 emit_insn_after);
1230 }
1231 }
1232 }
1233 \f
1234 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1235 is the current register layout. */
1236
1237 static void
1238 subst_stack_regs_pat (insn, regstack, pat)
1239 rtx insn;
1240 stack regstack;
1241 rtx pat;
1242 {
1243 rtx *dest, *src;
1244 rtx *src1 = 0, *src2;
1245 rtx src1_note, src2_note;
1246
1247 if (GET_CODE (pat) != SET)
1248 return;
1249
1250 dest = get_true_reg (&SET_DEST (pat));
1251 src = get_true_reg (&SET_SRC (pat));
1252
1253 /* See if this is a `movM' pattern, and handle elsewhere if so. */
1254
1255 if (*dest != cc0_rtx
1256 && (STACK_REG_P (*src)
1257 || (STACK_REG_P (*dest)
1258 && (GET_CODE (*src) == REG || GET_CODE (*src) == MEM
1259 || GET_CODE (*src) == CONST_DOUBLE))))
1260 move_for_stack_reg (insn, regstack, pat);
1261 else
1262 switch (GET_CODE (SET_SRC (pat)))
1263 {
1264 case COMPARE:
1265 compare_for_stack_reg (insn, regstack, pat);
1266 break;
1267
1268 case CALL:
1269 regstack->reg[++regstack->top] = REGNO (*dest);
1270 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1271 replace_reg (dest, FIRST_STACK_REG);
1272 break;
1273
1274 case REG:
1275 /* This is a `tstM2' case. */
1276 if (*dest != cc0_rtx)
1277 abort ();
1278
1279 src1 = src;
1280
1281 /* Fall through. */
1282
1283 case SQRT:
1284 case ABS:
1285 case NEG:
1286 /* These insns only operate on the top of the stack. DEST might
1287 be cc0_rtx if we're processing a tstM pattern. Also, it's
1288 possible that the tstM case results in a REG_DEAD note on the
1289 source. */
1290
1291 if (src1 == 0)
1292 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1293
1294 emit_swap_insn (insn, regstack, *src1, emit_insn_before);
1295
1296 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1297
1298 if (STACK_REG_P (*dest))
1299 replace_reg (dest, FIRST_STACK_REG);
1300
1301 if (src1_note)
1302 {
1303 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1304 regstack->top--;
1305 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
1306 }
1307
1308 replace_reg (src1, FIRST_STACK_REG);
1309
1310 break;
1311
1312 case MINUS:
1313 case DIV:
1314 /* On i386, reversed forms of subM3 and divM3 exist for
1315 MODE_FLOAT, so the same code that works for addM3 and mulM3
1316 can be used. */
1317 case MULT:
1318 case PLUS:
1319 /* These insns can accept the top of stack as a destination
1320 from a stack reg or mem, or can use the top of stack as a
1321 source and some other stack register (possibly top of stack)
1322 as a destination. */
1323
1324 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1325 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
1326
1327 /* We will fix any death note later. */
1328
1329 if (STACK_REG_P (*src1))
1330 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1331 else
1332 src1_note = 0;
1333 if (STACK_REG_P (*src2))
1334 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1335 else
1336 src2_note = 0;
1337
1338 /* If either operand is not a stack register, then the dest
1339 must be top of stack. */
1340
1341 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
1342 emit_swap_insn (insn, regstack, *dest, emit_insn_before);
1343 else
1344 {
1345 /* Both operands are REG. If neither operand is already
1346 at the top of stack, choose to make the one that is the dest
1347 the new top of stack.
1348
1349 ??? A later optimization here would be to look forward
1350 in the insns and see which source reg will be needed at top
1351 of stack soonest. */
1352
1353 int src1_hard_regnum, src2_hard_regnum;
1354
1355 src1_hard_regnum = get_hard_regnum (regstack, *src1);
1356 src2_hard_regnum = get_hard_regnum (regstack, *src2);
1357 if (src1_hard_regnum == -1 || src2_hard_regnum == -1)
1358 abort ();
1359
1360 if (src1_hard_regnum != FIRST_STACK_REG
1361 && src2_hard_regnum != FIRST_STACK_REG)
1362 emit_swap_insn (insn, regstack, *dest, emit_insn_before);
1363 }
1364
1365 if (STACK_REG_P (*src1))
1366 replace_reg (src1, get_hard_regnum (regstack, *src1));
1367 if (STACK_REG_P (*src2))
1368 replace_reg (src2, get_hard_regnum (regstack, *src2));
1369
1370 if (src1_note)
1371 {
1372 /* If the register that dies is at the top of stack, then
1373 the destination is somewhere else - merely substitute it.
1374 But if the reg that dies is not at top of stack, then
1375 move the top of stack to the dead reg, as though we had
1376 done the insn and then a store-with-pop. */
1377
1378 if (REGNO (XEXP (src1_note, 0)) == regstack->reg[regstack->top])
1379 {
1380 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1381 replace_reg (dest, get_hard_regnum (regstack, *dest));
1382 }
1383 else
1384 {
1385 int regno = get_hard_regnum (regstack, XEXP (src1_note, 0));
1386
1387 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1388 replace_reg (dest, regno);
1389
1390 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1391 = regstack->reg[regstack->top];
1392 }
1393
1394 CLEAR_HARD_REG_BIT (regstack->reg_set,
1395 REGNO (XEXP (src1_note, 0)));
1396 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1397 regstack->top--;
1398 }
1399 else if (src2_note)
1400 {
1401 if (REGNO (XEXP (src2_note, 0)) == regstack->reg[regstack->top])
1402 {
1403 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1404 replace_reg (dest, get_hard_regnum (regstack, *dest));
1405 }
1406 else
1407 {
1408 int regno = get_hard_regnum (regstack, XEXP (src2_note, 0));
1409
1410 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1411 replace_reg (dest, regno);
1412
1413 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
1414 = regstack->reg[regstack->top];
1415 }
1416
1417 CLEAR_HARD_REG_BIT (regstack->reg_set,
1418 REGNO (XEXP (src2_note, 0)));
1419 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
1420 regstack->top--;
1421 }
1422 else
1423 {
1424 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1425 replace_reg (dest, get_hard_regnum (regstack, *dest));
1426 }
1427
1428 break;
1429
1430 default:
1431 abort ();
1432 }
1433 }
1434 \f
1435 /* Substitute stack hard reg numbers for stack virtual registers in
1436 INSN. Non-stack register numbers are not changed. REGSTACK is the
1437 current stack content. Insns may be emitted as needed to arrange the
1438 stack for the 387 based on the contents of the insn. */
1439
1440 static void
1441 subst_stack_regs (insn, regstack)
1442 rtx insn;
1443 stack regstack;
1444 {
1445 register rtx *note_link, note;
1446 register int i;
1447
1448 if ((GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
1449 || INSN_DELETED_P (insn))
1450 return;
1451
1452 /* The stack should be empty at a call. */
1453
1454 if (GET_CODE (insn) == CALL_INSN)
1455 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
1456 if (TEST_HARD_REG_BIT (regstack->reg_set, i))
1457 abort ();
1458
1459 /* Do the actual substitution if any stack regs are mentioned.
1460 Since we only record whether entire insn mentions stack regs, and
1461 subst_stack_regs_pat only works for patterns that contain stack regs,
1462 we must check each pattern in a parallel here. A call_value_pop could
1463 fail otherwise. */
1464
1465 if (GET_MODE (insn) == QImode)
1466 {
1467 if (GET_CODE (PATTERN (insn)) == PARALLEL)
1468 for (i = 0; i < XVECLEN (PATTERN (insn) , 0); i++)
1469 {
1470 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
1471 subst_stack_regs_pat (insn, regstack,
1472 XVECEXP (PATTERN (insn), 0, i));
1473 }
1474 else
1475 subst_stack_regs_pat (insn, regstack, PATTERN (insn));
1476 }
1477
1478 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
1479 REG_UNUSED will already have been dealt with, so just return. */
1480
1481 if (INSN_DELETED_P (insn))
1482 return;
1483
1484 /* If there is a REG_UNUSED note on a stack register on this insn,
1485 the indicated reg must be popped. The REG_UNUSED note is removed,
1486 since the form of the newly emitted pop insn references the reg,
1487 making it no longer `unset'. */
1488
1489 note_link = &REG_NOTES(insn);
1490 for (note = *note_link; note; note = XEXP (note, 1))
1491 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
1492 {
1493 *note_link = XEXP (note, 1);
1494 emit_pop_insn (insn, regstack, XEXP (note, 0),
1495 emit_insn_after);
1496 }
1497 else
1498 note_link = &XEXP (note, 1);
1499 }
1500 \f
1501 /* Change the organization of the stack so that it fits a new basic
1502 block. Some registers might have to be popped, but there can never be
1503 a register live in the new block that is not now live.
1504
1505 Insert any needed insns after INSN. OLD is the original stack
1506 layout, and NEW is the desired form. OLD is updated to reflect the
1507 code emitted, ie, it will be the same as NEW upon return.
1508
1509 This function will not preserve block_end[]. But that information
1510 is no longer needed once this has executed. */
1511
1512 static void
1513 change_stack (insn, old, new)
1514 rtx insn;
1515 stack old;
1516 stack new;
1517 {
1518 int reg;
1519
1520 /* We will be inserting new insns after INSN, by first finding the
1521 next insn, and inserting before it. */
1522
1523 insn = NEXT_INSN (insn);
1524
1525 /* Pop any registers that are not needed in the new block. */
1526
1527 for (reg = old->top; reg >= 0; reg--)
1528 if (! TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
1529 emit_pop_insn (insn, old, DFmode_reg[old->reg[reg]],
1530 emit_insn_before);
1531
1532 if (new->top == -2)
1533 {
1534 /* If the new block has never been processed, then it can inherit
1535 the old stack order. */
1536
1537 new->top = old->top;
1538 bcopy (old->reg, new->reg, sizeof (new->reg));
1539 }
1540 else
1541 {
1542 /* This block has been entered before, and we must match the
1543 previously selected stack order. */
1544
1545 /* By now, the only difference should be the order of the stack,
1546 not their depth or liveliness. */
1547
1548 GO_IF_HARD_REG_EQUAL (old->reg_set, new->reg_set, win);
1549
1550 abort ();
1551
1552 win:
1553
1554 if (old->top != new->top)
1555 abort ();
1556
1557 /* Loop here emitting swaps until the stack is correct. The
1558 worst case number of swaps emitted is N + 2, where N is the
1559 depth of the stack. In some cases, the reg at the top of
1560 stack may be correct, but swapped anyway in order to fix
1561 other regs. But since we never swap any other reg away from
1562 its correct slot, this algorithm will converge. */
1563
1564 do
1565 {
1566 /* Swap the reg at top of stack into the position it is
1567 supposed to be in, until the correct top of stack appears. */
1568
1569 while (old->reg[old->top] != new->reg[new->top])
1570 {
1571 for (reg = new->top; reg >= 0; reg--)
1572 if (new->reg[reg] == old->reg[old->top])
1573 break;
1574
1575 if (reg == -1)
1576 abort ();
1577
1578 emit_swap_insn (insn, old, DFmode_reg[old->reg[reg]],
1579 emit_insn_before);
1580 }
1581
1582 /* See if any regs remain incorrect. If so, bring an
1583 incorrect reg to the top of stack, and let the while loop
1584 above fix it. */
1585
1586 for (reg = new->top; reg >= 0; reg--)
1587 if (new->reg[reg] != old->reg[reg])
1588 {
1589 emit_swap_insn (insn, old, DFmode_reg[old->reg[reg]],
1590 emit_insn_before);
1591 break;
1592 }
1593 } while (reg >= 0);
1594
1595 /* At this point there must be no differences. */
1596
1597 for (reg = old->top; reg >= 0; reg--)
1598 if (old->reg[reg] != new->reg[reg])
1599 abort ();
1600 }
1601 }
1602 \f
1603 /* Check PAT, which points to RTL in INSN, for a LABEL_REF. If it is
1604 found, ensure that a jump from INSN to the code_label to which the
1605 label_ref points ends up with the same stack as that at the
1606 code_label. Do this by inserting insns just before the code_label to
1607 pop and rotate the stack until it is in the correct order. REGSTACK
1608 is the order of the register stack in INSN.
1609
1610 Any code that is emitted here must not be later processed as part
1611 of any block, as it will already contain hard register numbers. */
1612
1613 static void
1614 goto_block_pat (insn, regstack, pat)
1615 rtx insn;
1616 stack regstack;
1617 rtx pat;
1618 {
1619 rtx label;
1620 rtx new_jump, new_label, new_barrier;
1621 rtx *ref;
1622 stack label_stack;
1623 struct stack_def temp_stack;
1624 int reg;
1625
1626 if (GET_CODE (pat) != LABEL_REF)
1627 {
1628 int i, j;
1629 char *fmt = GET_RTX_FORMAT (GET_CODE (pat));
1630
1631 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1632 {
1633 if (fmt[i] == 'e')
1634 goto_block_pat (insn, regstack, XEXP (pat, i));
1635 if (fmt[i] == 'E')
1636 for (j = 0; j < XVECLEN (pat, i); j++)
1637 goto_block_pat (insn, regstack, XVECEXP (pat, i, j));
1638 }
1639 return;
1640 }
1641
1642 label = XEXP (pat, 0);
1643 if (GET_CODE (label) != CODE_LABEL)
1644 abort ();
1645
1646 /* First, see if in fact anything needs to be done to the stack at all. */
1647
1648 label_stack = &block_stack_in[BLOCK_NUM (label)];
1649
1650 if (label_stack->top == -2)
1651 {
1652 /* If the target block hasn't had a stack order selected, then
1653 we need merely ensure that no pops are needed. */
1654
1655 for (reg = regstack->top; reg >= 0; reg--)
1656 if (! TEST_HARD_REG_BIT (label_stack->reg_set, regstack->reg[reg]))
1657 break;
1658
1659 if (reg == -1)
1660 {
1661 /* change_stack will not emit any code in this case. */
1662
1663 change_stack (label, regstack, label_stack);
1664 return;
1665 }
1666 }
1667 else if (label_stack->top == regstack->top)
1668 {
1669 for (reg = label_stack->top; reg >= 0; reg--)
1670 if (label_stack->reg[reg] != regstack->reg[reg])
1671 break;
1672
1673 if (reg == -1)
1674 return;
1675 }
1676
1677 /* At least one insn will need to be inserted before label. Insert
1678 a jump around the code we are about to emit. Emit a label for the new
1679 code, and point the original insn at this new label. We can't use
1680 redirect_jump here, because we're using fld[4] of the code labels as
1681 LABEL_REF chains, no NUSES counters. */
1682
1683 new_jump = emit_jump_insn_before (gen_jump (label), label);
1684 record_label_references (new_jump, PATTERN (new_jump));
1685 JUMP_LABEL (new_jump) = label;
1686
1687 new_barrier = emit_barrier_after (new_jump);
1688
1689 new_label = gen_label_rtx ();
1690 emit_label_after (new_label, new_barrier);
1691 LABEL_REFS (new_label) = new_label;
1692
1693 /* The old label_ref will no longer point to the code_label if now uses,
1694 so strip the label_ref from the code_label's chain of references. */
1695
1696 for (ref = &LABEL_REFS (label); *ref != label; ref = &LABEL_NEXTREF (*ref))
1697 if (*ref == pat)
1698 break;
1699
1700 if (*ref == label)
1701 abort ();
1702
1703 *ref = LABEL_NEXTREF (*ref);
1704
1705 XEXP (pat, 0) = new_label;
1706 record_label_references (insn, PATTERN (insn));
1707
1708 if (JUMP_LABEL (insn) == label)
1709 JUMP_LABEL (insn) = new_label;
1710
1711 /* Now emit the needed code. */
1712
1713 temp_stack = *regstack;
1714
1715 change_stack (new_label, &temp_stack, label_stack);
1716 }
1717 \f
1718 /* Traverse all basic blocks in a function, converting the register
1719 refereces in each insn from the "flat" register file that gcc uses, to
1720 the stack-like registers the 387 uses. */
1721
1722 static void
1723 convert_regs ()
1724 {
1725 register int block, reg;
1726 register rtx insn, next;
1727 struct stack_def regstack;
1728
1729 for (block = 0; block < blocks; block++)
1730 {
1731 if (block_stack_in[block].top == -2)
1732 {
1733 /* This block has not been previously encountered. Choose a
1734 default mapping for any stack regs live on entry */
1735
1736 block_stack_in[block].top = -1;
1737
1738 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
1739 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, reg))
1740 block_stack_in[block].reg[++block_stack_in[block].top] = reg;
1741 }
1742
1743 /* Process all insns in this block. Keep track of `next' here,
1744 so that we don't process any insns emitted while making
1745 substitutions in INSN. */
1746
1747 next = block_begin[block];
1748 regstack = block_stack_in[block];
1749 do
1750 {
1751 insn = next;
1752 next = NEXT_INSN (insn);
1753
1754 /* Don't bother processing unless there is a stack reg
1755 mentioned.
1756
1757 ??? For now, process CALL_INSNs too to make sure that the
1758 stack regs are dead after a call. Remove this eventually. */
1759
1760 if (GET_MODE (insn) == QImode || GET_CODE (insn) == CALL_INSN)
1761 subst_stack_regs (insn, &regstack);
1762
1763 } while (insn != block_end[block]);
1764
1765 /* Something failed if the stack life doesn't match. */
1766
1767 GO_IF_HARD_REG_EQUAL (regstack.reg_set, block_out_reg_set[block], win);
1768
1769 abort ();
1770
1771 win:
1772
1773 /* Adjust the stack of this block on exit to match the stack of
1774 the target block, or copy stack information into stack of
1775 jump target if the target block's stack order hasn't been set
1776 yet. */
1777
1778 if (GET_CODE (insn) == JUMP_INSN)
1779 goto_block_pat (insn, &regstack, PATTERN (insn));
1780
1781 /* Likewise handle the case where we fall into the next block. */
1782
1783 if ((block < blocks - 1) && block_drops_in[block+1])
1784 change_stack (insn, &regstack, &block_stack_in[block+1]);
1785 }
1786
1787 /* If the last basic block is the end of a loop, and that loop has
1788 regs live at its start, then the last basic block will have regs live
1789 at its end that need to be popped before the function returns. */
1790
1791 for (reg = regstack.top; reg >= 0; reg--)
1792 if (! current_function_returns_real
1793 || regstack.reg[reg] != FIRST_STACK_REG)
1794 insn = emit_pop_insn (insn, &regstack, DFmode_reg[regstack.reg[reg]],
1795 emit_insn_after);
1796 }
1797 \f
1798 /* Check expression PAT, which is in INSN, for label references. if
1799 one is found, print the block number of destination to FILE. */
1800
1801 static void
1802 print_blocks (file, insn, pat)
1803 FILE *file;
1804 rtx insn, pat;
1805 {
1806 register RTX_CODE code = GET_CODE (pat);
1807 register int i;
1808 register char *fmt;
1809
1810 if (code == LABEL_REF)
1811 {
1812 register rtx label = XEXP (pat, 0);
1813
1814 if (GET_CODE (label) != CODE_LABEL)
1815 abort ();
1816
1817 fprintf (file, " %d", BLOCK_NUM (label));
1818
1819 return;
1820 }
1821
1822 fmt = GET_RTX_FORMAT (code);
1823 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1824 {
1825 if (fmt[i] == 'e')
1826 print_blocks (file, insn, XEXP (pat, i));
1827 if (fmt[i] == 'E')
1828 {
1829 register int j;
1830 for (j = 0; j < XVECLEN (pat, i); j++)
1831 print_blocks (file, insn, XVECEXP (pat, i, j));
1832 }
1833 }
1834 }
1835 \f
1836 /* Write information about stack registers and stack blocks into FILE.
1837 This is part of making a debugging dump. */
1838 static void
1839 dump_stack_info (file)
1840 FILE *file;
1841 {
1842 register int block;
1843
1844 fprintf (file, "\n%d stack blocks.\n", blocks);
1845 for (block = 0; block < blocks; block++)
1846 {
1847 register rtx head, jump, end;
1848 register int regno;
1849
1850 fprintf (file, "\nStack block %d: first insn %d, last %d.\n",
1851 block, INSN_UID (block_begin[block]),
1852 INSN_UID (block_end[block]));
1853
1854 head = block_begin[block];
1855
1856 fprintf (file, "Reached from blocks: ");
1857 if (GET_CODE (head) == CODE_LABEL)
1858 for (jump = LABEL_REFS (head);
1859 jump != head;
1860 jump = LABEL_NEXTREF (jump))
1861 {
1862 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
1863 fprintf (file, " %d", from_block);
1864 }
1865 if (block_drops_in[block])
1866 fprintf (file, " previous");
1867
1868 fprintf (file, "\nlive stack registers on block entry: ");
1869 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG ; regno++)
1870 {
1871 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, regno))
1872 fprintf (file, "%d ", regno);
1873 }
1874
1875 fprintf (file, "\nlive stack registers on block exit: ");
1876 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG ; regno++)
1877 {
1878 if (TEST_HARD_REG_BIT (block_out_reg_set[block], regno))
1879 fprintf (file, "%d ", regno);
1880 }
1881
1882 end = block_end[block];
1883
1884 fprintf (file, "\nJumps to blocks: ");
1885 if (GET_CODE (end) == JUMP_INSN)
1886 print_blocks (file, end, PATTERN (end));
1887
1888 if (block + 1 < blocks && block_drops_in[block+1])
1889 fprintf (file, " next");
1890 else if (block + 1 == blocks
1891 || (GET_CODE (end) == JUMP_INSN
1892 && GET_CODE (PATTERN (end)) == RETURN))
1893 fprintf (file, " return");
1894
1895 fprintf (file, "\n");
1896 }
1897 }
1898 \f
1899 /* Report an error at line LINE of file FILE.
1900 S is a string and an arg for `printf'. */
1901
1902 /* Report an fatal error at the line number of the insn INSN (ASM_OPERAND).
1903 S1, S2 is a string and an arg for `printf'. */
1904
1905 static void
1906 fatal_for_asm (insn, s1, s2)
1907 rtx insn;
1908 char *s1, *s2;
1909 {
1910 char *filename;
1911 int line;
1912 rtx body = PATTERN (insn);
1913 rtx asmop = 0;
1914
1915 /* Find the (or one of the) ASM_OPERANDS in the insn. */
1916 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
1917 asmop = SET_SRC (body);
1918 else if (GET_CODE (body) == ASM_OPERANDS)
1919 asmop = body;
1920 else if (GET_CODE (body) == PARALLEL
1921 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
1922 asmop = SET_SRC (XVECEXP (body, 0, 0));
1923 else if (GET_CODE (body) == PARALLEL
1924 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
1925 asmop = XVECEXP (body, 0, 0);
1926 else
1927 abort ();
1928
1929 filename = ASM_OPERANDS_SOURCE_FILE (asmop);
1930 line = ASM_OPERANDS_SOURCE_LINE (asmop);
1931
1932 fprintf (stderr, s1);
1933 debug_rtx (insn);
1934
1935 error_with_file_and_line (filename, line, s2, NULL, NULL);
1936 exit (34);
1937 }
1938 #endif /* STACK_REGS */