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